blob: bd26ce3b1ea65b8008d1a078a9627f03fdd7fa0c [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"
25#include "media/base/fakevideocapturer.h"
26#include "media/engine/webrtcmediaengine.h"
27#include "media/sctp/sctptransportinternal.h"
28#include "modules/audio_processing/include/audio_processing.h"
29#include "p2p/base/fakeportallocator.h"
30#include "pc/audiotrack.h"
31#include "pc/mediasession.h"
32#include "pc/mediastream.h"
33#include "pc/peerconnection.h"
34#include "pc/streamcollection.h"
35#include "pc/test/fakeaudiocapturemodule.h"
36#include "pc/test/fakertccertificategenerator.h"
37#include "pc/test/fakevideotracksource.h"
38#include "pc/test/mockpeerconnectionobservers.h"
39#include "pc/test/testsdpstrings.h"
40#include "pc/videocapturertracksource.h"
41#include "pc/videotrack.h"
42#include "rtc_base/gunit.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020043#include "rtc_base/stringutils.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020044#include "rtc_base/virtualsocketserver.h"
45#include "test/gmock.h"
kwibergac9f8762016-09-30 22:29:43 -070046
47#ifdef WEBRTC_ANDROID
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020048#include "pc/test/androidtestinitializer.h"
kwibergac9f8762016-09-30 22:29:43 -070049#endif
henrike@webrtc.org28e20752013-07-10 00:45:36 +000050
51static const char kStreamLabel1[] = "local_stream_1";
52static const char kStreamLabel2[] = "local_stream_2";
53static const char kStreamLabel3[] = "local_stream_3";
54static const int kDefaultStunPort = 3478;
55static const char kStunAddressOnly[] = "stun:address";
56static const char kStunInvalidPort[] = "stun:address:-1";
57static const char kStunAddressPortAndMore1[] = "stun:address:port:more";
58static const char kStunAddressPortAndMore2[] = "stun:address:port more";
59static const char kTurnIceServerUri[] = "turn:user@turn.example.org";
60static const char kTurnUsername[] = "user";
61static const char kTurnPassword[] = "password";
62static const char kTurnHostname[] = "turn.example.org";
Peter Boström0c4e06b2015-10-07 12:23:21 +020063static const uint32_t kTimeout = 10000U;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000064
deadbeefab9b2d12015-10-14 11:33:11 -070065static const char kStreams[][8] = {"stream1", "stream2"};
66static const char kAudioTracks[][32] = {"audiotrack0", "audiotrack1"};
67static const char kVideoTracks[][32] = {"videotrack0", "videotrack1"};
68
deadbeef5e97fb52015-10-15 12:49:08 -070069static const char kRecvonly[] = "recvonly";
70static const char kSendrecv[] = "sendrecv";
71
deadbeefab9b2d12015-10-14 11:33:11 -070072// Reference SDP with a MediaStream with label "stream1" and audio track with
73// id "audio_1" and a video track with id "video_1;
74static const char kSdpStringWithStream1[] =
75 "v=0\r\n"
76 "o=- 0 0 IN IP4 127.0.0.1\r\n"
77 "s=-\r\n"
78 "t=0 0\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -080079 "m=audio 1 RTP/AVPF 103\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -070080 "a=ice-ufrag:e5785931\r\n"
81 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
82 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
83 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -070084 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -070085 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -080086 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -070087 "a=rtpmap:103 ISAC/16000\r\n"
88 "a=ssrc:1 cname:stream1\r\n"
89 "a=ssrc:1 mslabel:stream1\r\n"
90 "a=ssrc:1 label:audiotrack0\r\n"
91 "m=video 1 RTP/AVPF 120\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -080092 "a=ice-ufrag:e5785931\r\n"
93 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
94 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
95 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -070096 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -070097 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -080098 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -070099 "a=rtpmap:120 VP8/90000\r\n"
100 "a=ssrc:2 cname:stream1\r\n"
101 "a=ssrc:2 mslabel:stream1\r\n"
102 "a=ssrc:2 label:videotrack0\r\n";
103
zhihuang81c3a032016-11-17 12:06:24 -0800104// Reference SDP with a MediaStream with label "stream1" and audio track with
105// id "audio_1";
106static const char kSdpStringWithStream1AudioTrackOnly[] =
107 "v=0\r\n"
108 "o=- 0 0 IN IP4 127.0.0.1\r\n"
109 "s=-\r\n"
110 "t=0 0\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800111 "m=audio 1 RTP/AVPF 103\r\n"
zhihuang81c3a032016-11-17 12:06:24 -0800112 "a=ice-ufrag:e5785931\r\n"
113 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
114 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
115 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
zhihuang81c3a032016-11-17 12:06:24 -0800116 "a=mid:audio\r\n"
117 "a=sendrecv\r\n"
118 "a=rtpmap:103 ISAC/16000\r\n"
119 "a=ssrc:1 cname:stream1\r\n"
120 "a=ssrc:1 mslabel:stream1\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800121 "a=ssrc:1 label:audiotrack0\r\n"
122 "a=rtcp-mux\r\n";
zhihuang81c3a032016-11-17 12:06:24 -0800123
deadbeefab9b2d12015-10-14 11:33:11 -0700124// Reference SDP with two MediaStreams with label "stream1" and "stream2. Each
125// MediaStreams have one audio track and one video track.
126// This uses MSID.
127static const char kSdpStringWithStream1And2[] =
128 "v=0\r\n"
129 "o=- 0 0 IN IP4 127.0.0.1\r\n"
130 "s=-\r\n"
131 "t=0 0\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800132 "a=msid-semantic: WMS stream1 stream2\r\n"
133 "m=audio 1 RTP/AVPF 103\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700134 "a=ice-ufrag:e5785931\r\n"
135 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
136 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
137 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700138 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700139 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800140 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700141 "a=rtpmap:103 ISAC/16000\r\n"
142 "a=ssrc:1 cname:stream1\r\n"
143 "a=ssrc:1 msid:stream1 audiotrack0\r\n"
144 "a=ssrc:3 cname:stream2\r\n"
145 "a=ssrc:3 msid:stream2 audiotrack1\r\n"
146 "m=video 1 RTP/AVPF 120\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800147 "a=ice-ufrag:e5785931\r\n"
148 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
149 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
150 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700151 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700152 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800153 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700154 "a=rtpmap:120 VP8/0\r\n"
155 "a=ssrc:2 cname:stream1\r\n"
156 "a=ssrc:2 msid:stream1 videotrack0\r\n"
157 "a=ssrc:4 cname:stream2\r\n"
158 "a=ssrc:4 msid:stream2 videotrack1\r\n";
159
160// Reference SDP without MediaStreams. Msid is not supported.
161static const char kSdpStringWithoutStreams[] =
162 "v=0\r\n"
163 "o=- 0 0 IN IP4 127.0.0.1\r\n"
164 "s=-\r\n"
165 "t=0 0\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800166 "m=audio 1 RTP/AVPF 103\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700167 "a=ice-ufrag:e5785931\r\n"
168 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
169 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
170 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700171 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700172 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800173 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700174 "a=rtpmap:103 ISAC/16000\r\n"
175 "m=video 1 RTP/AVPF 120\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800176 "a=ice-ufrag:e5785931\r\n"
177 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
178 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
179 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700180 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700181 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800182 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700183 "a=rtpmap:120 VP8/90000\r\n";
184
185// Reference SDP without MediaStreams. Msid is supported.
186static const char kSdpStringWithMsidWithoutStreams[] =
187 "v=0\r\n"
188 "o=- 0 0 IN IP4 127.0.0.1\r\n"
189 "s=-\r\n"
190 "t=0 0\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800191 "a=msid-semantic: WMS\r\n"
192 "m=audio 1 RTP/AVPF 103\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700193 "a=ice-ufrag:e5785931\r\n"
194 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
195 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
196 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700197 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700198 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800199 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700200 "a=rtpmap:103 ISAC/16000\r\n"
201 "m=video 1 RTP/AVPF 120\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800202 "a=ice-ufrag:e5785931\r\n"
203 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
204 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
205 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700206 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700207 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800208 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700209 "a=rtpmap:120 VP8/90000\r\n";
210
211// Reference SDP without MediaStreams and audio only.
212static const char kSdpStringWithoutStreamsAudioOnly[] =
213 "v=0\r\n"
214 "o=- 0 0 IN IP4 127.0.0.1\r\n"
215 "s=-\r\n"
216 "t=0 0\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800217 "m=audio 1 RTP/AVPF 103\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700218 "a=ice-ufrag:e5785931\r\n"
219 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
220 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
221 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700222 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700223 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800224 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700225 "a=rtpmap:103 ISAC/16000\r\n";
226
227// Reference SENDONLY SDP without MediaStreams. Msid is not supported.
228static const char kSdpStringSendOnlyWithoutStreams[] =
229 "v=0\r\n"
230 "o=- 0 0 IN IP4 127.0.0.1\r\n"
231 "s=-\r\n"
232 "t=0 0\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800233 "m=audio 1 RTP/AVPF 103\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700234 "a=ice-ufrag:e5785931\r\n"
235 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
236 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
237 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700238 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700239 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700240 "a=sendonly\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800241 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700242 "a=rtpmap:103 ISAC/16000\r\n"
243 "m=video 1 RTP/AVPF 120\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800244 "a=ice-ufrag:e5785931\r\n"
245 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
246 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
247 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700248 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700249 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700250 "a=sendonly\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800251 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700252 "a=rtpmap:120 VP8/90000\r\n";
253
254static const char kSdpStringInit[] =
255 "v=0\r\n"
256 "o=- 0 0 IN IP4 127.0.0.1\r\n"
257 "s=-\r\n"
258 "t=0 0\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700259 "a=msid-semantic: WMS\r\n";
260
261static const char kSdpStringAudio[] =
262 "m=audio 1 RTP/AVPF 103\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800263 "a=ice-ufrag:e5785931\r\n"
264 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
265 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
266 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700267 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700268 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800269 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700270 "a=rtpmap:103 ISAC/16000\r\n";
271
272static const char kSdpStringVideo[] =
273 "m=video 1 RTP/AVPF 120\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800274 "a=ice-ufrag:e5785931\r\n"
275 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
276 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
277 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700278 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700279 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800280 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700281 "a=rtpmap:120 VP8/90000\r\n";
282
283static const char kSdpStringMs1Audio0[] =
284 "a=ssrc:1 cname:stream1\r\n"
285 "a=ssrc:1 msid:stream1 audiotrack0\r\n";
286
287static const char kSdpStringMs1Video0[] =
288 "a=ssrc:2 cname:stream1\r\n"
289 "a=ssrc:2 msid:stream1 videotrack0\r\n";
290
291static const char kSdpStringMs1Audio1[] =
292 "a=ssrc:3 cname:stream1\r\n"
293 "a=ssrc:3 msid:stream1 audiotrack1\r\n";
294
295static const char kSdpStringMs1Video1[] =
296 "a=ssrc:4 cname:stream1\r\n"
297 "a=ssrc:4 msid:stream1 videotrack1\r\n";
298
deadbeef8662f942017-01-20 21:20:51 -0800299static const char kDtlsSdesFallbackSdp[] =
300 "v=0\r\n"
301 "o=xxxxxx 7 2 IN IP4 0.0.0.0\r\n"
302 "s=-\r\n"
303 "c=IN IP4 0.0.0.0\r\n"
304 "t=0 0\r\n"
305 "a=group:BUNDLE audio\r\n"
306 "a=msid-semantic: WMS\r\n"
307 "m=audio 1 RTP/SAVPF 0\r\n"
308 "a=sendrecv\r\n"
309 "a=rtcp-mux\r\n"
310 "a=mid:audio\r\n"
311 "a=ssrc:1 cname:stream1\r\n"
312 "a=ssrc:1 mslabel:stream1\r\n"
313 "a=ssrc:1 label:audiotrack0\r\n"
314 "a=ice-ufrag:e5785931\r\n"
315 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
316 "a=rtpmap:0 pcmu/8000\r\n"
317 "a=fingerprint:sha-1 "
318 "4A:AD:B9:B1:3F:82:18:3B:54:02:12:DF:3E:5D:49:6B:19:E5:7C:AB\r\n"
319 "a=setup:actpass\r\n"
320 "a=crypto:1 AES_CM_128_HMAC_SHA1_32 "
321 "inline:NzB4d1BINUAvLEw6UzF3WSJ+PSdFcGdUJShpX1Zj|2^20|1:32 "
322 "dummy_session_params\r\n";
323
perkjd61bf802016-03-24 03:16:19 -0700324using ::testing::Exactly;
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700325using cricket::StreamParams;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000326using webrtc::AudioSourceInterface;
deadbeefab9b2d12015-10-14 11:33:11 -0700327using webrtc::AudioTrack;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000328using webrtc::AudioTrackInterface;
329using webrtc::DataBuffer;
330using webrtc::DataChannelInterface;
331using webrtc::FakeConstraints;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000332using webrtc::IceCandidateInterface;
deadbeefc80741f2015-10-22 13:14:45 -0700333using webrtc::MediaConstraintsInterface;
deadbeefab9b2d12015-10-14 11:33:11 -0700334using webrtc::MediaStream;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000335using webrtc::MediaStreamInterface;
336using webrtc::MediaStreamTrackInterface;
337using webrtc::MockCreateSessionDescriptionObserver;
338using webrtc::MockDataChannelObserver;
339using webrtc::MockSetSessionDescriptionObserver;
340using webrtc::MockStatsObserver;
perkjd61bf802016-03-24 03:16:19 -0700341using webrtc::NotifierInterface;
342using webrtc::ObserverInterface;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000343using webrtc::PeerConnectionInterface;
344using webrtc::PeerConnectionObserver;
deadbeef293e9262017-01-11 12:28:30 -0800345using webrtc::RTCError;
346using webrtc::RTCErrorType;
deadbeefab9b2d12015-10-14 11:33:11 -0700347using webrtc::RtpReceiverInterface;
348using webrtc::RtpSenderInterface;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000349using webrtc::SdpParseError;
350using webrtc::SessionDescriptionInterface;
deadbeefab9b2d12015-10-14 11:33:11 -0700351using webrtc::StreamCollection;
352using webrtc::StreamCollectionInterface;
perkja3ede6c2016-03-08 01:27:48 +0100353using webrtc::VideoTrackSourceInterface;
deadbeefab9b2d12015-10-14 11:33:11 -0700354using webrtc::VideoTrack;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000355using webrtc::VideoTrackInterface;
356
deadbeefab9b2d12015-10-14 11:33:11 -0700357typedef PeerConnectionInterface::RTCOfferAnswerOptions RTCOfferAnswerOptions;
358
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000359namespace {
360
361// Gets the first ssrc of given content type from the ContentInfo.
362bool GetFirstSsrc(const cricket::ContentInfo* content_info, int* ssrc) {
363 if (!content_info || !ssrc) {
364 return false;
365 }
366 const cricket::MediaContentDescription* media_desc =
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000367 static_cast<const cricket::MediaContentDescription*>(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000368 content_info->description);
369 if (!media_desc || media_desc->streams().empty()) {
370 return false;
371 }
372 *ssrc = media_desc->streams().begin()->first_ssrc();
373 return true;
374}
375
deadbeefd1a38b52016-12-10 13:15:33 -0800376// Get the ufrags out of an SDP blob. Useful for testing ICE restart
377// behavior.
378std::vector<std::string> GetUfrags(
379 const webrtc::SessionDescriptionInterface* desc) {
380 std::vector<std::string> ufrags;
381 for (const cricket::TransportInfo& info :
382 desc->description()->transport_infos()) {
383 ufrags.push_back(info.description.ice_ufrag);
384 }
385 return ufrags;
386}
387
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000388void SetSsrcToZero(std::string* sdp) {
389 const char kSdpSsrcAtribute[] = "a=ssrc:";
390 const char kSdpSsrcAtributeZero[] = "a=ssrc:0";
391 size_t ssrc_pos = 0;
392 while ((ssrc_pos = sdp->find(kSdpSsrcAtribute, ssrc_pos)) !=
393 std::string::npos) {
394 size_t end_ssrc = sdp->find(" ", ssrc_pos);
395 sdp->replace(ssrc_pos, end_ssrc - ssrc_pos, kSdpSsrcAtributeZero);
396 ssrc_pos = end_ssrc;
397 }
398}
399
deadbeefab9b2d12015-10-14 11:33:11 -0700400// Check if |streams| contains the specified track.
401bool ContainsTrack(const std::vector<cricket::StreamParams>& streams,
402 const std::string& stream_label,
403 const std::string& track_id) {
404 for (const cricket::StreamParams& params : streams) {
405 if (params.sync_label == stream_label && params.id == track_id) {
406 return true;
407 }
408 }
409 return false;
410}
411
412// Check if |senders| contains the specified sender, by id.
413bool ContainsSender(
414 const std::vector<rtc::scoped_refptr<RtpSenderInterface>>& senders,
415 const std::string& id) {
416 for (const auto& sender : senders) {
417 if (sender->id() == id) {
418 return true;
419 }
420 }
421 return false;
422}
423
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700424// Check if |senders| contains the specified sender, by id and stream id.
425bool ContainsSender(
426 const std::vector<rtc::scoped_refptr<RtpSenderInterface>>& senders,
427 const std::string& id,
428 const std::string& stream_id) {
429 for (const auto& sender : senders) {
deadbeefa601f5c2016-06-06 14:27:39 -0700430 if (sender->id() == id && sender->stream_ids()[0] == stream_id) {
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700431 return true;
432 }
433 }
434 return false;
435}
436
deadbeefab9b2d12015-10-14 11:33:11 -0700437// Create a collection of streams.
438// CreateStreamCollection(1) creates a collection that
439// correspond to kSdpStringWithStream1.
440// CreateStreamCollection(2) correspond to kSdpStringWithStream1And2.
441rtc::scoped_refptr<StreamCollection> CreateStreamCollection(
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700442 int number_of_streams,
443 int tracks_per_stream) {
deadbeefab9b2d12015-10-14 11:33:11 -0700444 rtc::scoped_refptr<StreamCollection> local_collection(
445 StreamCollection::Create());
446
447 for (int i = 0; i < number_of_streams; ++i) {
448 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream(
449 webrtc::MediaStream::Create(kStreams[i]));
450
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700451 for (int j = 0; j < tracks_per_stream; ++j) {
452 // Add a local audio track.
453 rtc::scoped_refptr<webrtc::AudioTrackInterface> audio_track(
454 webrtc::AudioTrack::Create(kAudioTracks[i * tracks_per_stream + j],
455 nullptr));
456 stream->AddTrack(audio_track);
deadbeefab9b2d12015-10-14 11:33:11 -0700457
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700458 // Add a local video track.
459 rtc::scoped_refptr<webrtc::VideoTrackInterface> video_track(
460 webrtc::VideoTrack::Create(kVideoTracks[i * tracks_per_stream + j],
perkj773be362017-07-31 23:22:01 -0700461 webrtc::FakeVideoTrackSource::Create(),
462 rtc::Thread::Current()));
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700463 stream->AddTrack(video_track);
464 }
deadbeefab9b2d12015-10-14 11:33:11 -0700465
466 local_collection->AddStream(stream);
467 }
468 return local_collection;
469}
470
471// Check equality of StreamCollections.
472bool CompareStreamCollections(StreamCollectionInterface* s1,
473 StreamCollectionInterface* s2) {
474 if (s1 == nullptr || s2 == nullptr || s1->count() != s2->count()) {
475 return false;
476 }
477
478 for (size_t i = 0; i != s1->count(); ++i) {
479 if (s1->at(i)->label() != s2->at(i)->label()) {
480 return false;
481 }
482 webrtc::AudioTrackVector audio_tracks1 = s1->at(i)->GetAudioTracks();
483 webrtc::AudioTrackVector audio_tracks2 = s2->at(i)->GetAudioTracks();
484 webrtc::VideoTrackVector video_tracks1 = s1->at(i)->GetVideoTracks();
485 webrtc::VideoTrackVector video_tracks2 = s2->at(i)->GetVideoTracks();
486
487 if (audio_tracks1.size() != audio_tracks2.size()) {
488 return false;
489 }
490 for (size_t j = 0; j != audio_tracks1.size(); ++j) {
491 if (audio_tracks1[j]->id() != audio_tracks2[j]->id()) {
492 return false;
493 }
494 }
495 if (video_tracks1.size() != video_tracks2.size()) {
496 return false;
497 }
498 for (size_t j = 0; j != video_tracks1.size(); ++j) {
499 if (video_tracks1[j]->id() != video_tracks2[j]->id()) {
500 return false;
501 }
502 }
503 }
504 return true;
505}
506
perkjd61bf802016-03-24 03:16:19 -0700507// Helper class to test Observer.
508class MockTrackObserver : public ObserverInterface {
509 public:
510 explicit MockTrackObserver(NotifierInterface* notifier)
511 : notifier_(notifier) {
512 notifier_->RegisterObserver(this);
513 }
514
515 ~MockTrackObserver() { Unregister(); }
516
517 void Unregister() {
518 if (notifier_) {
519 notifier_->UnregisterObserver(this);
520 notifier_ = nullptr;
521 }
522 }
523
524 MOCK_METHOD0(OnChanged, void());
525
526 private:
527 NotifierInterface* notifier_;
528};
529
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000530class MockPeerConnectionObserver : public PeerConnectionObserver {
531 public:
deadbeefab9b2d12015-10-14 11:33:11 -0700532 MockPeerConnectionObserver() : remote_streams_(StreamCollection::Create()) {}
Henrik Kjellander3fe372d2016-05-12 08:10:52 +0200533 virtual ~MockPeerConnectionObserver() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000534 }
535 void SetPeerConnectionInterface(PeerConnectionInterface* pc) {
536 pc_ = pc;
537 if (pc) {
538 state_ = pc_->signaling_state();
539 }
540 }
nisseef8b61e2016-04-29 06:09:15 -0700541 void OnSignalingChange(
542 PeerConnectionInterface::SignalingState new_state) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000543 EXPECT_EQ(pc_->signaling_state(), new_state);
544 state_ = new_state;
545 }
deadbeefab9b2d12015-10-14 11:33:11 -0700546
547 MediaStreamInterface* RemoteStream(const std::string& label) {
548 return remote_streams_->find(label);
549 }
550 StreamCollectionInterface* remote_streams() const { return remote_streams_; }
Taylor Brandstetter98cde262016-05-31 13:02:21 -0700551 void OnAddStream(rtc::scoped_refptr<MediaStreamInterface> stream) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000552 last_added_stream_ = stream;
deadbeefab9b2d12015-10-14 11:33:11 -0700553 remote_streams_->AddStream(stream);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000554 }
Taylor Brandstetter98cde262016-05-31 13:02:21 -0700555 void OnRemoveStream(
556 rtc::scoped_refptr<MediaStreamInterface> stream) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000557 last_removed_stream_ = stream;
deadbeefab9b2d12015-10-14 11:33:11 -0700558 remote_streams_->RemoveStream(stream);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000559 }
perkjdfb769d2016-02-09 03:09:43 -0800560 void OnRenegotiationNeeded() override { renegotiation_needed_ = true; }
Taylor Brandstetter98cde262016-05-31 13:02:21 -0700561 void OnDataChannel(
562 rtc::scoped_refptr<DataChannelInterface> data_channel) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000563 last_datachannel_ = data_channel;
564 }
565
perkjdfb769d2016-02-09 03:09:43 -0800566 void OnIceConnectionChange(
567 PeerConnectionInterface::IceConnectionState new_state) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000568 EXPECT_EQ(pc_->ice_connection_state(), new_state);
zhihuang81c3a032016-11-17 12:06:24 -0800569 callback_triggered_ = true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000570 }
perkjdfb769d2016-02-09 03:09:43 -0800571 void OnIceGatheringChange(
572 PeerConnectionInterface::IceGatheringState new_state) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000573 EXPECT_EQ(pc_->ice_gathering_state(), new_state);
perkjdfb769d2016-02-09 03:09:43 -0800574 ice_complete_ = new_state == PeerConnectionInterface::kIceGatheringComplete;
zhihuang81c3a032016-11-17 12:06:24 -0800575 callback_triggered_ = true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000576 }
perkjdfb769d2016-02-09 03:09:43 -0800577 void OnIceCandidate(const webrtc::IceCandidateInterface* candidate) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000578 EXPECT_NE(PeerConnectionInterface::kIceGatheringNew,
579 pc_->ice_gathering_state());
580
581 std::string sdp;
582 EXPECT_TRUE(candidate->ToString(&sdp));
583 EXPECT_LT(0u, sdp.size());
584 last_candidate_.reset(webrtc::CreateIceCandidate(candidate->sdp_mid(),
585 candidate->sdp_mline_index(), sdp, NULL));
586 EXPECT_TRUE(last_candidate_.get() != NULL);
zhihuang81c3a032016-11-17 12:06:24 -0800587 callback_triggered_ = true;
zhihuang29ff8442016-07-27 11:07:25 -0700588 }
589
590 void OnIceCandidatesRemoved(
591 const std::vector<cricket::Candidate>& candidates) override {
zhihuang81c3a032016-11-17 12:06:24 -0800592 callback_triggered_ = true;
zhihuang29ff8442016-07-27 11:07:25 -0700593 }
594
595 void OnIceConnectionReceivingChange(bool receiving) override {
zhihuang81c3a032016-11-17 12:06:24 -0800596 callback_triggered_ = true;
597 }
598
zhihuangc63b8942016-12-02 15:41:10 -0800599 void OnAddTrack(
600 rtc::scoped_refptr<webrtc::RtpReceiverInterface> receiver,
601 const std::vector<rtc::scoped_refptr<webrtc::MediaStreamInterface>>&
602 streams) override {
zhihuang81c3a032016-11-17 12:06:24 -0800603 EXPECT_TRUE(receiver != nullptr);
604 num_added_tracks_++;
605 last_added_track_label_ = receiver->id();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000606 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000607
608 // Returns the label of the last added stream.
609 // Empty string if no stream have been added.
610 std::string GetLastAddedStreamLabel() {
611 if (last_added_stream_.get())
612 return last_added_stream_->label();
613 return "";
614 }
615 std::string GetLastRemovedStreamLabel() {
616 if (last_removed_stream_.get())
617 return last_removed_stream_->label();
618 return "";
619 }
620
zhihuang9763d562016-08-05 11:14:50 -0700621 rtc::scoped_refptr<PeerConnectionInterface> pc_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000622 PeerConnectionInterface::SignalingState state_;
kwibergd1fe2812016-04-27 06:47:29 -0700623 std::unique_ptr<IceCandidateInterface> last_candidate_;
zhihuang9763d562016-08-05 11:14:50 -0700624 rtc::scoped_refptr<DataChannelInterface> last_datachannel_;
deadbeefab9b2d12015-10-14 11:33:11 -0700625 rtc::scoped_refptr<StreamCollection> remote_streams_;
626 bool renegotiation_needed_ = false;
627 bool ice_complete_ = false;
zhihuang81c3a032016-11-17 12:06:24 -0800628 bool callback_triggered_ = false;
629 int num_added_tracks_ = 0;
630 std::string last_added_track_label_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000631
632 private:
zhihuang9763d562016-08-05 11:14:50 -0700633 rtc::scoped_refptr<MediaStreamInterface> last_added_stream_;
634 rtc::scoped_refptr<MediaStreamInterface> last_removed_stream_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000635};
636
637} // namespace
deadbeefab9b2d12015-10-14 11:33:11 -0700638
nisse528b7932017-05-08 03:21:43 -0700639// The PeerConnectionMediaConfig tests below verify that configuration and
640// constraints are propagated into the PeerConnection's MediaConfig. These
641// settings are intended for MediaChannel constructors, but that is not
642// exercised by these unittest.
zhihuang29ff8442016-07-27 11:07:25 -0700643class PeerConnectionFactoryForTest : public webrtc::PeerConnectionFactory {
644 public:
zhihuang38ede132017-06-15 12:52:32 -0700645 static rtc::scoped_refptr<PeerConnectionFactoryForTest>
646 CreatePeerConnectionFactoryForTest() {
647 auto audio_encoder_factory = webrtc::CreateBuiltinAudioEncoderFactory();
648 auto audio_decoder_factory = webrtc::CreateBuiltinAudioDecoderFactory();
649
650 auto media_engine = std::unique_ptr<cricket::MediaEngineInterface>(
651 cricket::WebRtcMediaEngineFactory::Create(
652 nullptr, audio_encoder_factory, audio_decoder_factory, nullptr,
peaha9cc40b2017-06-29 08:32:09 -0700653 nullptr, nullptr, webrtc::AudioProcessing::Create()));
zhihuang38ede132017-06-15 12:52:32 -0700654
655 std::unique_ptr<webrtc::CallFactoryInterface> call_factory =
656 webrtc::CreateCallFactory();
657
658 std::unique_ptr<webrtc::RtcEventLogFactoryInterface> event_log_factory =
659 webrtc::CreateRtcEventLogFactory();
660
661 return new rtc::RefCountedObject<PeerConnectionFactoryForTest>(
662 rtc::Thread::Current(), rtc::Thread::Current(), rtc::Thread::Current(),
Magnus Jedvert835cc0c2017-09-23 16:14:25 +0200663 nullptr, nullptr, std::move(media_engine), std::move(call_factory),
zhihuang38ede132017-06-15 12:52:32 -0700664 std::move(event_log_factory));
665 }
666
667 PeerConnectionFactoryForTest(
668 rtc::Thread* network_thread,
669 rtc::Thread* worker_thread,
670 rtc::Thread* signaling_thread,
zhihuang38ede132017-06-15 12:52:32 -0700671 cricket::WebRtcVideoEncoderFactory* video_encoder_factory,
672 cricket::WebRtcVideoDecoderFactory* video_decoder_factory,
zhihuang38ede132017-06-15 12:52:32 -0700673 std::unique_ptr<cricket::MediaEngineInterface> media_engine,
674 std::unique_ptr<webrtc::CallFactoryInterface> call_factory,
675 std::unique_ptr<webrtc::RtcEventLogFactoryInterface> event_log_factory)
676 : webrtc::PeerConnectionFactory(network_thread,
677 worker_thread,
678 signaling_thread,
zhihuang38ede132017-06-15 12:52:32 -0700679 video_encoder_factory,
680 video_decoder_factory,
zhihuang38ede132017-06-15 12:52:32 -0700681 std::move(media_engine),
682 std::move(call_factory),
683 std::move(event_log_factory)) {}
kwiberg1e4e8cb2017-01-31 01:48:08 -0800684
zhihuang29ff8442016-07-27 11:07:25 -0700685 cricket::TransportController* CreateTransportController(
Honghai Zhangbfd398c2016-08-30 22:07:42 -0700686 cricket::PortAllocator* port_allocator,
687 bool redetermine_role_on_ice_restart) override {
zhihuang29ff8442016-07-27 11:07:25 -0700688 transport_controller = new cricket::TransportController(
Honghai Zhangbfd398c2016-08-30 22:07:42 -0700689 rtc::Thread::Current(), rtc::Thread::Current(), port_allocator,
deadbeef7914b8c2017-04-21 03:23:33 -0700690 redetermine_role_on_ice_restart, rtc::CryptoOptions());
zhihuang29ff8442016-07-27 11:07:25 -0700691 return transport_controller;
692 }
693
deadbeefd7850b22017-08-23 10:59:19 -0700694 rtc::scoped_refptr<FakeAudioCaptureModule> fake_audio_capture_module_;
zhihuang29ff8442016-07-27 11:07:25 -0700695 cricket::TransportController* transport_controller;
zhihuang29ff8442016-07-27 11:07:25 -0700696};
697
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000698class PeerConnectionInterfaceTest : public testing::Test {
699 protected:
deadbeef9a6f4d42017-05-15 19:43:33 -0700700 PeerConnectionInterfaceTest()
deadbeef98e186c2017-05-16 18:00:06 -0700701 : vss_(new rtc::VirtualSocketServer()), main_(vss_.get()) {
phoglund37ebcf02016-01-08 05:04:57 -0800702#ifdef WEBRTC_ANDROID
703 webrtc::InitializeAndroidObjects();
704#endif
705 }
706
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000707 virtual void SetUp() {
deadbeefd7850b22017-08-23 10:59:19 -0700708 // Use fake audio capture module since we're only testing the interface
709 // level, and using a real one could make tests flaky when run in parallel.
710 fake_audio_capture_module_ = FakeAudioCaptureModule::Create();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000711 pc_factory_ = webrtc::CreatePeerConnectionFactory(
danilchape9021a32016-05-17 01:52:02 -0700712 rtc::Thread::Current(), rtc::Thread::Current(), rtc::Thread::Current(),
deadbeefd7850b22017-08-23 10:59:19 -0700713 fake_audio_capture_module_, nullptr, nullptr);
danilchape9021a32016-05-17 01:52:02 -0700714 ASSERT_TRUE(pc_factory_);
zhihuang29ff8442016-07-27 11:07:25 -0700715 pc_factory_for_test_ =
zhihuang38ede132017-06-15 12:52:32 -0700716 PeerConnectionFactoryForTest::CreatePeerConnectionFactoryForTest();
zhihuang29ff8442016-07-27 11:07:25 -0700717 pc_factory_for_test_->Initialize();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000718 }
719
720 void CreatePeerConnection() {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700721 CreatePeerConnection(PeerConnectionInterface::RTCConfiguration(), nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000722 }
723
deadbeef293e9262017-01-11 12:28:30 -0800724 // DTLS does not work in a loopback call, so is disabled for most of the
725 // tests in this file.
726 void CreatePeerConnectionWithoutDtls() {
727 FakeConstraints no_dtls_constraints;
728 no_dtls_constraints.AddMandatory(
729 webrtc::MediaConstraintsInterface::kEnableDtlsSrtp, false);
730
731 CreatePeerConnection(PeerConnectionInterface::RTCConfiguration(),
732 &no_dtls_constraints);
733 }
734
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000735 void CreatePeerConnection(webrtc::MediaConstraintsInterface* constraints) {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700736 CreatePeerConnection(PeerConnectionInterface::RTCConfiguration(),
737 constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000738 }
739
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700740 void CreatePeerConnectionWithIceTransportsType(
741 PeerConnectionInterface::IceTransportsType type) {
742 PeerConnectionInterface::RTCConfiguration config;
743 config.type = type;
744 return CreatePeerConnection(config, nullptr);
745 }
746
747 void CreatePeerConnectionWithIceServer(const std::string& uri,
748 const std::string& password) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800749 PeerConnectionInterface::RTCConfiguration config;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000750 PeerConnectionInterface::IceServer server;
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700751 server.uri = uri;
752 server.password = password;
753 config.servers.push_back(server);
754 CreatePeerConnection(config, nullptr);
755 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000756
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700757 void CreatePeerConnection(PeerConnectionInterface::RTCConfiguration config,
758 webrtc::MediaConstraintsInterface* constraints) {
kwibergd1fe2812016-04-27 06:47:29 -0700759 std::unique_ptr<cricket::FakePortAllocator> port_allocator(
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800760 new cricket::FakePortAllocator(rtc::Thread::Current(), nullptr));
761 port_allocator_ = port_allocator.get();
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000762
deadbeef1dcb1642017-03-29 21:08:16 -0700763 // Create certificate generator unless DTLS constraint is explicitly set to
764 // false.
Henrik Boströmd79599d2016-06-01 13:58:50 +0200765 std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator;
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000766 bool dtls;
767 if (FindConstraint(constraints,
768 webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
769 &dtls,
Henrik Boström5e56c592015-08-11 10:33:13 +0200770 nullptr) && dtls) {
deadbeef8662f942017-01-20 21:20:51 -0800771 fake_certificate_generator_ = new FakeRTCCertificateGenerator();
772 cert_generator.reset(fake_certificate_generator_);
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000773 }
Henrik Boströmd79599d2016-06-01 13:58:50 +0200774 pc_ = pc_factory_->CreatePeerConnection(
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800775 config, constraints, std::move(port_allocator),
Henrik Boströmd79599d2016-06-01 13:58:50 +0200776 std::move(cert_generator), &observer_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000777 ASSERT_TRUE(pc_.get() != NULL);
778 observer_.SetPeerConnectionInterface(pc_.get());
779 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
780 }
781
deadbeef0a6c4ca2015-10-06 11:38:28 -0700782 void CreatePeerConnectionExpectFail(const std::string& uri) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800783 PeerConnectionInterface::RTCConfiguration config;
deadbeef0a6c4ca2015-10-06 11:38:28 -0700784 PeerConnectionInterface::IceServer server;
deadbeef0a6c4ca2015-10-06 11:38:28 -0700785 server.uri = uri;
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800786 config.servers.push_back(server);
deadbeef0a6c4ca2015-10-06 11:38:28 -0700787
zhihuang9763d562016-08-05 11:14:50 -0700788 rtc::scoped_refptr<PeerConnectionInterface> pc;
hbosd7973cc2016-05-27 06:08:53 -0700789 pc = pc_factory_->CreatePeerConnection(config, nullptr, nullptr, nullptr,
790 &observer_);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800791 EXPECT_EQ(nullptr, pc);
deadbeef0a6c4ca2015-10-06 11:38:28 -0700792 }
793
Steve Anton038834f2017-07-14 15:59:59 -0700794 void CreatePeerConnectionExpectFail(
795 PeerConnectionInterface::RTCConfiguration config) {
796 PeerConnectionInterface::IceServer server;
797 server.uri = kTurnIceServerUri;
798 server.password = kTurnPassword;
799 config.servers.push_back(server);
800 rtc::scoped_refptr<PeerConnectionInterface> pc =
801 pc_factory_->CreatePeerConnection(config, nullptr, nullptr, &observer_);
802 EXPECT_EQ(nullptr, pc);
803 }
804
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000805 void CreatePeerConnectionWithDifferentConfigurations() {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700806 CreatePeerConnectionWithIceServer(kStunAddressOnly, "");
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800807 EXPECT_EQ(1u, port_allocator_->stun_servers().size());
808 EXPECT_EQ(0u, port_allocator_->turn_servers().size());
809 EXPECT_EQ("address", port_allocator_->stun_servers().begin()->hostname());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000810 EXPECT_EQ(kDefaultStunPort,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800811 port_allocator_->stun_servers().begin()->port());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000812
deadbeef0a6c4ca2015-10-06 11:38:28 -0700813 CreatePeerConnectionExpectFail(kStunInvalidPort);
814 CreatePeerConnectionExpectFail(kStunAddressPortAndMore1);
815 CreatePeerConnectionExpectFail(kStunAddressPortAndMore2);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000816
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700817 CreatePeerConnectionWithIceServer(kTurnIceServerUri, kTurnPassword);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800818 EXPECT_EQ(0u, port_allocator_->stun_servers().size());
819 EXPECT_EQ(1u, port_allocator_->turn_servers().size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000820 EXPECT_EQ(kTurnUsername,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800821 port_allocator_->turn_servers()[0].credentials.username);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000822 EXPECT_EQ(kTurnPassword,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800823 port_allocator_->turn_servers()[0].credentials.password);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000824 EXPECT_EQ(kTurnHostname,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800825 port_allocator_->turn_servers()[0].ports[0].address.hostname());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000826 }
827
828 void ReleasePeerConnection() {
829 pc_ = NULL;
830 observer_.SetPeerConnectionInterface(NULL);
831 }
832
deadbeefab9b2d12015-10-14 11:33:11 -0700833 void AddVideoStream(const std::string& label) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000834 // Create a local stream.
zhihuang9763d562016-08-05 11:14:50 -0700835 rtc::scoped_refptr<MediaStreamInterface> stream(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000836 pc_factory_->CreateLocalMediaStream(label));
zhihuang9763d562016-08-05 11:14:50 -0700837 rtc::scoped_refptr<VideoTrackSourceInterface> video_source(
deadbeef112b2e92017-02-10 20:13:37 -0800838 pc_factory_->CreateVideoSource(std::unique_ptr<cricket::VideoCapturer>(
839 new cricket::FakeVideoCapturer()),
840 NULL));
zhihuang9763d562016-08-05 11:14:50 -0700841 rtc::scoped_refptr<VideoTrackInterface> video_track(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000842 pc_factory_->CreateVideoTrack(label + "v0", video_source));
843 stream->AddTrack(video_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +0000844 EXPECT_TRUE(pc_->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000845 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
846 observer_.renegotiation_needed_ = false;
847 }
848
849 void AddVoiceStream(const std::string& label) {
850 // Create a local stream.
zhihuang9763d562016-08-05 11:14:50 -0700851 rtc::scoped_refptr<MediaStreamInterface> stream(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000852 pc_factory_->CreateLocalMediaStream(label));
zhihuang9763d562016-08-05 11:14:50 -0700853 rtc::scoped_refptr<AudioTrackInterface> audio_track(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000854 pc_factory_->CreateAudioTrack(label + "a0", NULL));
855 stream->AddTrack(audio_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +0000856 EXPECT_TRUE(pc_->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000857 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
858 observer_.renegotiation_needed_ = false;
859 }
860
861 void AddAudioVideoStream(const std::string& stream_label,
862 const std::string& audio_track_label,
863 const std::string& video_track_label) {
864 // Create a local stream.
zhihuang9763d562016-08-05 11:14:50 -0700865 rtc::scoped_refptr<MediaStreamInterface> stream(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000866 pc_factory_->CreateLocalMediaStream(stream_label));
zhihuang9763d562016-08-05 11:14:50 -0700867 rtc::scoped_refptr<AudioTrackInterface> audio_track(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000868 pc_factory_->CreateAudioTrack(
869 audio_track_label, static_cast<AudioSourceInterface*>(NULL)));
870 stream->AddTrack(audio_track.get());
zhihuang9763d562016-08-05 11:14:50 -0700871 rtc::scoped_refptr<VideoTrackInterface> video_track(
nisseaf510af2016-03-21 08:20:42 -0700872 pc_factory_->CreateVideoTrack(
deadbeef112b2e92017-02-10 20:13:37 -0800873 video_track_label, pc_factory_->CreateVideoSource(
874 std::unique_ptr<cricket::VideoCapturer>(
875 new cricket::FakeVideoCapturer()))));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000876 stream->AddTrack(video_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +0000877 EXPECT_TRUE(pc_->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000878 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
879 observer_.renegotiation_needed_ = false;
880 }
881
kwibergd1fe2812016-04-27 06:47:29 -0700882 bool DoCreateOfferAnswer(std::unique_ptr<SessionDescriptionInterface>* desc,
deadbeefc80741f2015-10-22 13:14:45 -0700883 bool offer,
884 MediaConstraintsInterface* constraints) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000885 rtc::scoped_refptr<MockCreateSessionDescriptionObserver>
886 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000887 MockCreateSessionDescriptionObserver>());
888 if (offer) {
deadbeefc80741f2015-10-22 13:14:45 -0700889 pc_->CreateOffer(observer, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000890 } else {
deadbeefc80741f2015-10-22 13:14:45 -0700891 pc_->CreateAnswer(observer, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000892 }
893 EXPECT_EQ_WAIT(true, observer->called(), kTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -0700894 *desc = observer->MoveDescription();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000895 return observer->result();
896 }
897
kwibergd1fe2812016-04-27 06:47:29 -0700898 bool DoCreateOffer(std::unique_ptr<SessionDescriptionInterface>* desc,
deadbeefc80741f2015-10-22 13:14:45 -0700899 MediaConstraintsInterface* constraints) {
900 return DoCreateOfferAnswer(desc, true, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000901 }
902
kwibergd1fe2812016-04-27 06:47:29 -0700903 bool DoCreateAnswer(std::unique_ptr<SessionDescriptionInterface>* desc,
deadbeefc80741f2015-10-22 13:14:45 -0700904 MediaConstraintsInterface* constraints) {
905 return DoCreateOfferAnswer(desc, false, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000906 }
907
Steve Antondb45ca82017-09-11 18:27:34 -0700908 bool DoSetSessionDescription(
909 std::unique_ptr<SessionDescriptionInterface> desc,
910 bool local) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000911 rtc::scoped_refptr<MockSetSessionDescriptionObserver>
912 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000913 MockSetSessionDescriptionObserver>());
914 if (local) {
Steve Antondb45ca82017-09-11 18:27:34 -0700915 pc_->SetLocalDescription(observer, desc.release());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000916 } else {
Steve Antondb45ca82017-09-11 18:27:34 -0700917 pc_->SetRemoteDescription(observer, desc.release());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000918 }
zhihuang29ff8442016-07-27 11:07:25 -0700919 if (pc_->signaling_state() != PeerConnectionInterface::kClosed) {
920 EXPECT_EQ_WAIT(true, observer->called(), kTimeout);
921 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000922 return observer->result();
923 }
924
Steve Antondb45ca82017-09-11 18:27:34 -0700925 bool DoSetLocalDescription(
926 std::unique_ptr<SessionDescriptionInterface> desc) {
927 return DoSetSessionDescription(std::move(desc), true);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000928 }
929
Steve Antondb45ca82017-09-11 18:27:34 -0700930 bool DoSetRemoteDescription(
931 std::unique_ptr<SessionDescriptionInterface> desc) {
932 return DoSetSessionDescription(std::move(desc), false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000933 }
934
935 // Calls PeerConnection::GetStats and check the return value.
936 // It does not verify the values in the StatReports since a RTCP packet might
937 // be required.
938 bool DoGetStats(MediaStreamTrackInterface* track) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000939 rtc::scoped_refptr<MockStatsObserver> observer(
940 new rtc::RefCountedObject<MockStatsObserver>());
jiayl@webrtc.orgdb41b4d2014-03-03 21:30:06 +0000941 if (!pc_->GetStats(
942 observer, track, PeerConnectionInterface::kStatsOutputLevelStandard))
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000943 return false;
944 EXPECT_TRUE_WAIT(observer->called(), kTimeout);
945 return observer->called();
946 }
947
948 void InitiateCall() {
deadbeef293e9262017-01-11 12:28:30 -0800949 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000950 // Create a local stream with audio&video tracks.
951 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
952 CreateOfferReceiveAnswer();
953 }
954
955 // Verify that RTP Header extensions has been negotiated for audio and video.
956 void VerifyRemoteRtpHeaderExtensions() {
957 const cricket::MediaContentDescription* desc =
958 cricket::GetFirstAudioContentDescription(
959 pc_->remote_description()->description());
960 ASSERT_TRUE(desc != NULL);
961 EXPECT_GT(desc->rtp_header_extensions().size(), 0u);
962
963 desc = cricket::GetFirstVideoContentDescription(
964 pc_->remote_description()->description());
965 ASSERT_TRUE(desc != NULL);
966 EXPECT_GT(desc->rtp_header_extensions().size(), 0u);
967 }
968
969 void CreateOfferAsRemoteDescription() {
kwibergd1fe2812016-04-27 06:47:29 -0700970 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -0700971 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000972 std::string sdp;
973 EXPECT_TRUE(offer->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -0700974 std::unique_ptr<SessionDescriptionInterface> remote_offer(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000975 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
Steve Antondb45ca82017-09-11 18:27:34 -0700976 sdp, nullptr));
977 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_offer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000978 EXPECT_EQ(PeerConnectionInterface::kHaveRemoteOffer, observer_.state_);
979 }
980
deadbeefab9b2d12015-10-14 11:33:11 -0700981 void CreateAndSetRemoteOffer(const std::string& sdp) {
Steve Antondb45ca82017-09-11 18:27:34 -0700982 std::unique_ptr<SessionDescriptionInterface> remote_offer(
deadbeefab9b2d12015-10-14 11:33:11 -0700983 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
Steve Antondb45ca82017-09-11 18:27:34 -0700984 sdp, nullptr));
985 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_offer)));
deadbeefab9b2d12015-10-14 11:33:11 -0700986 EXPECT_EQ(PeerConnectionInterface::kHaveRemoteOffer, observer_.state_);
987 }
988
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000989 void CreateAnswerAsLocalDescription() {
kwibergd1fe2812016-04-27 06:47:29 -0700990 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -0700991 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000992
993 // TODO(perkj): Currently SetLocalDescription fails if any parameters in an
994 // audio codec change, even if the parameter has nothing to do with
995 // receiving. Not all parameters are serialized to SDP.
996 // Since CreatePrAnswerAsLocalDescription serialize/deserialize
997 // the SessionDescription, it is necessary to do that here to in order to
998 // get ReceiveOfferCreatePrAnswerAndAnswer and RenegotiateAudioOnly to pass.
999 // https://code.google.com/p/webrtc/issues/detail?id=1356
1000 std::string sdp;
1001 EXPECT_TRUE(answer->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07001002 std::unique_ptr<SessionDescriptionInterface> new_answer(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001003 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
Steve Antondb45ca82017-09-11 18:27:34 -07001004 sdp, nullptr));
1005 EXPECT_TRUE(DoSetLocalDescription(std::move(new_answer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001006 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
1007 }
1008
1009 void CreatePrAnswerAsLocalDescription() {
kwibergd1fe2812016-04-27 06:47:29 -07001010 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -07001011 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001012
1013 std::string sdp;
1014 EXPECT_TRUE(answer->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07001015 std::unique_ptr<SessionDescriptionInterface> pr_answer(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001016 webrtc::CreateSessionDescription(SessionDescriptionInterface::kPrAnswer,
Steve Antondb45ca82017-09-11 18:27:34 -07001017 sdp, nullptr));
1018 EXPECT_TRUE(DoSetLocalDescription(std::move(pr_answer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001019 EXPECT_EQ(PeerConnectionInterface::kHaveLocalPrAnswer, observer_.state_);
1020 }
1021
1022 void CreateOfferReceiveAnswer() {
1023 CreateOfferAsLocalDescription();
1024 std::string sdp;
1025 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
1026 CreateAnswerAsRemoteDescription(sdp);
1027 }
1028
1029 void CreateOfferAsLocalDescription() {
kwibergd1fe2812016-04-27 06:47:29 -07001030 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001031 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001032 // TODO(perkj): Currently SetLocalDescription fails if any parameters in an
1033 // audio codec change, even if the parameter has nothing to do with
1034 // receiving. Not all parameters are serialized to SDP.
1035 // Since CreatePrAnswerAsLocalDescription serialize/deserialize
1036 // the SessionDescription, it is necessary to do that here to in order to
1037 // get ReceiveOfferCreatePrAnswerAndAnswer and RenegotiateAudioOnly to pass.
1038 // https://code.google.com/p/webrtc/issues/detail?id=1356
1039 std::string sdp;
1040 EXPECT_TRUE(offer->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07001041 std::unique_ptr<SessionDescriptionInterface> new_offer(
1042 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
1043 sdp, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001044
Steve Antondb45ca82017-09-11 18:27:34 -07001045 EXPECT_TRUE(DoSetLocalDescription(std::move(new_offer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001046 EXPECT_EQ(PeerConnectionInterface::kHaveLocalOffer, observer_.state_);
mallinath@webrtc.org68cbd012014-01-22 00:16:46 +00001047 // Wait for the ice_complete message, so that SDP will have candidates.
1048 EXPECT_TRUE_WAIT(observer_.ice_complete_, kTimeout);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001049 }
1050
deadbeefab9b2d12015-10-14 11:33:11 -07001051 void CreateAnswerAsRemoteDescription(const std::string& sdp) {
Steve Antondb45ca82017-09-11 18:27:34 -07001052 std::unique_ptr<SessionDescriptionInterface> answer(
1053 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
1054 sdp, nullptr));
1055 ASSERT_TRUE(answer);
1056 EXPECT_TRUE(DoSetRemoteDescription(std::move(answer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001057 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
1058 }
1059
deadbeefab9b2d12015-10-14 11:33:11 -07001060 void CreatePrAnswerAndAnswerAsRemoteDescription(const std::string& sdp) {
Steve Antondb45ca82017-09-11 18:27:34 -07001061 std::unique_ptr<SessionDescriptionInterface> pr_answer(
1062 webrtc::CreateSessionDescription(SessionDescriptionInterface::kPrAnswer,
1063 sdp, nullptr));
1064 ASSERT_TRUE(pr_answer);
1065 EXPECT_TRUE(DoSetRemoteDescription(std::move(pr_answer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001066 EXPECT_EQ(PeerConnectionInterface::kHaveRemotePrAnswer, observer_.state_);
Steve Antondb45ca82017-09-11 18:27:34 -07001067 std::unique_ptr<SessionDescriptionInterface> answer(
1068 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
1069 sdp, nullptr));
1070 ASSERT_TRUE(answer);
1071 EXPECT_TRUE(DoSetRemoteDescription(std::move(answer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001072 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
1073 }
1074
1075 // Help function used for waiting until a the last signaled remote stream has
1076 // the same label as |stream_label|. In a few of the tests in this file we
1077 // answer with the same session description as we offer and thus we can
1078 // check if OnAddStream have been called with the same stream as we offer to
1079 // send.
1080 void WaitAndVerifyOnAddStream(const std::string& stream_label) {
1081 EXPECT_EQ_WAIT(stream_label, observer_.GetLastAddedStreamLabel(), kTimeout);
1082 }
1083
1084 // Creates an offer and applies it as a local session description.
1085 // Creates an answer with the same SDP an the offer but removes all lines
1086 // that start with a:ssrc"
1087 void CreateOfferReceiveAnswerWithoutSsrc() {
1088 CreateOfferAsLocalDescription();
1089 std::string sdp;
1090 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
1091 SetSsrcToZero(&sdp);
1092 CreateAnswerAsRemoteDescription(sdp);
1093 }
1094
deadbeefab9b2d12015-10-14 11:33:11 -07001095 // This function creates a MediaStream with label kStreams[0] and
1096 // |number_of_audio_tracks| and |number_of_video_tracks| tracks and the
1097 // corresponding SessionDescriptionInterface. The SessionDescriptionInterface
kwiberg2bbff992016-03-16 11:03:04 -07001098 // is returned and the MediaStream is stored in
deadbeefab9b2d12015-10-14 11:33:11 -07001099 // |reference_collection_|
kwibergd1fe2812016-04-27 06:47:29 -07001100 std::unique_ptr<SessionDescriptionInterface>
kwiberg2bbff992016-03-16 11:03:04 -07001101 CreateSessionDescriptionAndReference(size_t number_of_audio_tracks,
1102 size_t number_of_video_tracks) {
1103 EXPECT_LE(number_of_audio_tracks, 2u);
1104 EXPECT_LE(number_of_video_tracks, 2u);
deadbeefab9b2d12015-10-14 11:33:11 -07001105
1106 reference_collection_ = StreamCollection::Create();
1107 std::string sdp_ms1 = std::string(kSdpStringInit);
1108
1109 std::string mediastream_label = kStreams[0];
1110
1111 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream(
1112 webrtc::MediaStream::Create(mediastream_label));
1113 reference_collection_->AddStream(stream);
1114
1115 if (number_of_audio_tracks > 0) {
1116 sdp_ms1 += std::string(kSdpStringAudio);
1117 sdp_ms1 += std::string(kSdpStringMs1Audio0);
1118 AddAudioTrack(kAudioTracks[0], stream);
1119 }
1120 if (number_of_audio_tracks > 1) {
1121 sdp_ms1 += kSdpStringMs1Audio1;
1122 AddAudioTrack(kAudioTracks[1], stream);
1123 }
1124
1125 if (number_of_video_tracks > 0) {
1126 sdp_ms1 += std::string(kSdpStringVideo);
1127 sdp_ms1 += std::string(kSdpStringMs1Video0);
1128 AddVideoTrack(kVideoTracks[0], stream);
1129 }
1130 if (number_of_video_tracks > 1) {
1131 sdp_ms1 += kSdpStringMs1Video1;
1132 AddVideoTrack(kVideoTracks[1], stream);
1133 }
1134
kwibergd1fe2812016-04-27 06:47:29 -07001135 return std::unique_ptr<SessionDescriptionInterface>(
kwiberg2bbff992016-03-16 11:03:04 -07001136 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
1137 sdp_ms1, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07001138 }
1139
1140 void AddAudioTrack(const std::string& track_id,
1141 MediaStreamInterface* stream) {
1142 rtc::scoped_refptr<webrtc::AudioTrackInterface> audio_track(
1143 webrtc::AudioTrack::Create(track_id, nullptr));
1144 ASSERT_TRUE(stream->AddTrack(audio_track));
1145 }
1146
1147 void AddVideoTrack(const std::string& track_id,
1148 MediaStreamInterface* stream) {
1149 rtc::scoped_refptr<webrtc::VideoTrackInterface> video_track(
nisseaf510af2016-03-21 08:20:42 -07001150 webrtc::VideoTrack::Create(track_id,
perkj773be362017-07-31 23:22:01 -07001151 webrtc::FakeVideoTrackSource::Create(),
1152 rtc::Thread::Current()));
deadbeefab9b2d12015-10-14 11:33:11 -07001153 ASSERT_TRUE(stream->AddTrack(video_track));
1154 }
1155
kwibergfd8be342016-05-14 19:44:11 -07001156 std::unique_ptr<SessionDescriptionInterface> CreateOfferWithOneAudioStream() {
deadbeef293e9262017-01-11 12:28:30 -08001157 CreatePeerConnectionWithoutDtls();
zhihuang8f65cdf2016-05-06 18:40:30 -07001158 AddVoiceStream(kStreamLabel1);
kwibergfd8be342016-05-14 19:44:11 -07001159 std::unique_ptr<SessionDescriptionInterface> offer;
zhihuang8f65cdf2016-05-06 18:40:30 -07001160 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
1161 return offer;
1162 }
1163
kwibergfd8be342016-05-14 19:44:11 -07001164 std::unique_ptr<SessionDescriptionInterface>
zhihuang8f65cdf2016-05-06 18:40:30 -07001165 CreateAnswerWithOneAudioStream() {
Steve Antondb45ca82017-09-11 18:27:34 -07001166 EXPECT_TRUE(DoSetRemoteDescription(CreateOfferWithOneAudioStream()));
kwibergfd8be342016-05-14 19:44:11 -07001167 std::unique_ptr<SessionDescriptionInterface> answer;
zhihuang8f65cdf2016-05-06 18:40:30 -07001168 EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
1169 return answer;
1170 }
1171
1172 const std::string& GetFirstAudioStreamCname(
1173 const SessionDescriptionInterface* desc) {
1174 const cricket::ContentInfo* audio_content =
1175 cricket::GetFirstAudioContent(desc->description());
1176 const cricket::AudioContentDescription* audio_desc =
1177 static_cast<const cricket::AudioContentDescription*>(
1178 audio_content->description);
1179 return audio_desc->streams()[0].cname;
1180 }
1181
zhihuang1c378ed2017-08-17 14:10:50 -07001182 std::unique_ptr<SessionDescriptionInterface> CreateOfferWithOptions(
1183 const RTCOfferAnswerOptions& offer_answer_options) {
1184 RTC_DCHECK(pc_);
1185 rtc::scoped_refptr<MockCreateSessionDescriptionObserver> observer(
1186 new rtc::RefCountedObject<MockCreateSessionDescriptionObserver>());
1187 pc_->CreateOffer(observer, offer_answer_options);
1188 EXPECT_EQ_WAIT(true, observer->called(), kTimeout);
1189 return observer->MoveDescription();
1190 }
1191
1192 void CreateOfferWithOptionsAsRemoteDescription(
1193 std::unique_ptr<SessionDescriptionInterface>* desc,
1194 const RTCOfferAnswerOptions& offer_answer_options) {
1195 *desc = CreateOfferWithOptions(offer_answer_options);
1196 ASSERT_TRUE(desc != nullptr);
1197 std::string sdp;
1198 EXPECT_TRUE((*desc)->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07001199 std::unique_ptr<SessionDescriptionInterface> remote_offer(
zhihuang1c378ed2017-08-17 14:10:50 -07001200 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
Steve Antondb45ca82017-09-11 18:27:34 -07001201 sdp, nullptr));
1202 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_offer)));
zhihuang1c378ed2017-08-17 14:10:50 -07001203 EXPECT_EQ(PeerConnectionInterface::kHaveRemoteOffer, observer_.state_);
1204 }
1205
1206 void CreateOfferWithOptionsAsLocalDescription(
1207 std::unique_ptr<SessionDescriptionInterface>* desc,
1208 const RTCOfferAnswerOptions& offer_answer_options) {
1209 *desc = CreateOfferWithOptions(offer_answer_options);
1210 ASSERT_TRUE(desc != nullptr);
1211 std::string sdp;
1212 EXPECT_TRUE((*desc)->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07001213 std::unique_ptr<SessionDescriptionInterface> new_offer(
1214 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
1215 sdp, nullptr));
zhihuang1c378ed2017-08-17 14:10:50 -07001216
Steve Antondb45ca82017-09-11 18:27:34 -07001217 EXPECT_TRUE(DoSetLocalDescription(std::move(new_offer)));
zhihuang1c378ed2017-08-17 14:10:50 -07001218 EXPECT_EQ(PeerConnectionInterface::kHaveLocalOffer, observer_.state_);
1219 }
1220
1221 bool HasCNCodecs(const cricket::ContentInfo* content) {
1222 const cricket::ContentDescription* description = content->description;
1223 RTC_DCHECK(description);
1224 const cricket::AudioContentDescription* audio_content_desc =
1225 static_cast<const cricket::AudioContentDescription*>(description);
1226 RTC_DCHECK(audio_content_desc);
1227 for (size_t i = 0; i < audio_content_desc->codecs().size(); ++i) {
1228 if (audio_content_desc->codecs()[i].name == "CN")
1229 return true;
1230 }
1231 return false;
1232 }
1233
deadbeef9a6f4d42017-05-15 19:43:33 -07001234 std::unique_ptr<rtc::VirtualSocketServer> vss_;
1235 rtc::AutoSocketServerThread main_;
deadbeefd7850b22017-08-23 10:59:19 -07001236 rtc::scoped_refptr<FakeAudioCaptureModule> fake_audio_capture_module_;
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08001237 cricket::FakePortAllocator* port_allocator_ = nullptr;
deadbeef8662f942017-01-20 21:20:51 -08001238 FakeRTCCertificateGenerator* fake_certificate_generator_ = nullptr;
zhihuang9763d562016-08-05 11:14:50 -07001239 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface> pc_factory_;
1240 rtc::scoped_refptr<PeerConnectionFactoryForTest> pc_factory_for_test_;
1241 rtc::scoped_refptr<PeerConnectionInterface> pc_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001242 MockPeerConnectionObserver observer_;
deadbeefab9b2d12015-10-14 11:33:11 -07001243 rtc::scoped_refptr<StreamCollection> reference_collection_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001244};
1245
zhihuang29ff8442016-07-27 11:07:25 -07001246// Test that no callbacks on the PeerConnectionObserver are called after the
1247// PeerConnection is closed.
1248TEST_F(PeerConnectionInterfaceTest, CloseAndTestCallbackFunctions) {
zhihuang9763d562016-08-05 11:14:50 -07001249 rtc::scoped_refptr<PeerConnectionInterface> pc(
zhihuang29ff8442016-07-27 11:07:25 -07001250 pc_factory_for_test_->CreatePeerConnection(
1251 PeerConnectionInterface::RTCConfiguration(), nullptr, nullptr,
1252 nullptr, &observer_));
1253 observer_.SetPeerConnectionInterface(pc.get());
1254 pc->Close();
1255
1256 // No callbacks is expected to be called.
zhihuang81c3a032016-11-17 12:06:24 -08001257 observer_.callback_triggered_ = false;
zhihuang29ff8442016-07-27 11:07:25 -07001258 std::vector<cricket::Candidate> candidates;
1259 pc_factory_for_test_->transport_controller->SignalGatheringState(
1260 cricket::IceGatheringState{});
1261 pc_factory_for_test_->transport_controller->SignalCandidatesGathered(
1262 "", candidates);
1263 pc_factory_for_test_->transport_controller->SignalConnectionState(
1264 cricket::IceConnectionState{});
1265 pc_factory_for_test_->transport_controller->SignalCandidatesRemoved(
1266 candidates);
1267 pc_factory_for_test_->transport_controller->SignalReceiving(false);
zhihuang81c3a032016-11-17 12:06:24 -08001268 EXPECT_FALSE(observer_.callback_triggered_);
zhihuang29ff8442016-07-27 11:07:25 -07001269}
1270
zhihuang8f65cdf2016-05-06 18:40:30 -07001271// Generate different CNAMEs when PeerConnections are created.
1272// The CNAMEs are expected to be generated randomly. It is possible
1273// that the test fails, though the possibility is very low.
1274TEST_F(PeerConnectionInterfaceTest, CnameGenerationInOffer) {
kwibergfd8be342016-05-14 19:44:11 -07001275 std::unique_ptr<SessionDescriptionInterface> offer1 =
zhihuang8f65cdf2016-05-06 18:40:30 -07001276 CreateOfferWithOneAudioStream();
kwibergfd8be342016-05-14 19:44:11 -07001277 std::unique_ptr<SessionDescriptionInterface> offer2 =
zhihuang8f65cdf2016-05-06 18:40:30 -07001278 CreateOfferWithOneAudioStream();
1279 EXPECT_NE(GetFirstAudioStreamCname(offer1.get()),
1280 GetFirstAudioStreamCname(offer2.get()));
1281}
1282
1283TEST_F(PeerConnectionInterfaceTest, CnameGenerationInAnswer) {
kwibergfd8be342016-05-14 19:44:11 -07001284 std::unique_ptr<SessionDescriptionInterface> answer1 =
zhihuang8f65cdf2016-05-06 18:40:30 -07001285 CreateAnswerWithOneAudioStream();
kwibergfd8be342016-05-14 19:44:11 -07001286 std::unique_ptr<SessionDescriptionInterface> answer2 =
zhihuang8f65cdf2016-05-06 18:40:30 -07001287 CreateAnswerWithOneAudioStream();
1288 EXPECT_NE(GetFirstAudioStreamCname(answer1.get()),
1289 GetFirstAudioStreamCname(answer2.get()));
1290}
1291
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001292TEST_F(PeerConnectionInterfaceTest,
1293 CreatePeerConnectionWithDifferentConfigurations) {
1294 CreatePeerConnectionWithDifferentConfigurations();
1295}
1296
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001297TEST_F(PeerConnectionInterfaceTest,
1298 CreatePeerConnectionWithDifferentIceTransportsTypes) {
1299 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kNone);
1300 EXPECT_EQ(cricket::CF_NONE, port_allocator_->candidate_filter());
1301 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kRelay);
1302 EXPECT_EQ(cricket::CF_RELAY, port_allocator_->candidate_filter());
1303 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kNoHost);
1304 EXPECT_EQ(cricket::CF_ALL & ~cricket::CF_HOST,
1305 port_allocator_->candidate_filter());
1306 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kAll);
1307 EXPECT_EQ(cricket::CF_ALL, port_allocator_->candidate_filter());
1308}
1309
1310// Test that when a PeerConnection is created with a nonzero candidate pool
1311// size, the pooled PortAllocatorSession is created with all the attributes
1312// in the RTCConfiguration.
1313TEST_F(PeerConnectionInterfaceTest, CreatePeerConnectionWithPooledCandidates) {
1314 PeerConnectionInterface::RTCConfiguration config;
1315 PeerConnectionInterface::IceServer server;
1316 server.uri = kStunAddressOnly;
1317 config.servers.push_back(server);
1318 config.type = PeerConnectionInterface::kRelay;
1319 config.disable_ipv6 = true;
1320 config.tcp_candidate_policy =
1321 PeerConnectionInterface::kTcpCandidatePolicyDisabled;
honghaiz60347052016-05-31 18:29:12 -07001322 config.candidate_network_policy =
1323 PeerConnectionInterface::kCandidateNetworkPolicyLowCost;
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001324 config.ice_candidate_pool_size = 1;
1325 CreatePeerConnection(config, nullptr);
1326
1327 const cricket::FakePortAllocatorSession* session =
1328 static_cast<const cricket::FakePortAllocatorSession*>(
1329 port_allocator_->GetPooledSession());
1330 ASSERT_NE(nullptr, session);
1331 EXPECT_EQ(1UL, session->stun_servers().size());
1332 EXPECT_EQ(0U, session->flags() & cricket::PORTALLOCATOR_ENABLE_IPV6);
1333 EXPECT_LT(0U, session->flags() & cricket::PORTALLOCATOR_DISABLE_TCP);
honghaiz60347052016-05-31 18:29:12 -07001334 EXPECT_LT(0U,
1335 session->flags() & cricket::PORTALLOCATOR_DISABLE_COSTLY_NETWORKS);
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001336}
1337
deadbeefd21eab32017-07-26 16:50:11 -07001338// Test that network-related RTCConfiguration members are applied to the
1339// PortAllocator when CreatePeerConnection is called. Specifically:
1340// - disable_ipv6_on_wifi
1341// - max_ipv6_networks
1342// - tcp_candidate_policy
1343// - candidate_network_policy
1344// - prune_turn_ports
1345//
1346// Note that the candidate filter (RTCConfiguration::type) is already tested
1347// above.
1348TEST_F(PeerConnectionInterfaceTest,
1349 CreatePeerConnectionAppliesNetworkConfigToPortAllocator) {
1350 // Create fake port allocator.
1351 std::unique_ptr<cricket::FakePortAllocator> port_allocator(
1352 new cricket::FakePortAllocator(rtc::Thread::Current(), nullptr));
1353 cricket::FakePortAllocator* raw_port_allocator = port_allocator.get();
1354
1355 // Create RTCConfiguration with some network-related fields relevant to
1356 // PortAllocator populated.
1357 PeerConnectionInterface::RTCConfiguration config;
1358 config.disable_ipv6_on_wifi = true;
1359 config.max_ipv6_networks = 10;
1360 config.tcp_candidate_policy =
1361 PeerConnectionInterface::kTcpCandidatePolicyDisabled;
1362 config.candidate_network_policy =
1363 PeerConnectionInterface::kCandidateNetworkPolicyLowCost;
1364 config.prune_turn_ports = true;
1365
1366 // Create the PC factory and PC with the above config.
1367 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface> pc_factory(
1368 webrtc::CreatePeerConnectionFactory(
1369 rtc::Thread::Current(), rtc::Thread::Current(),
deadbeefd7850b22017-08-23 10:59:19 -07001370 rtc::Thread::Current(), fake_audio_capture_module_, nullptr,
1371 nullptr));
deadbeefd21eab32017-07-26 16:50:11 -07001372 rtc::scoped_refptr<PeerConnectionInterface> pc(
1373 pc_factory->CreatePeerConnection(
1374 config, nullptr, std::move(port_allocator), nullptr, &observer_));
1375
1376 // Now validate that the config fields set above were applied to the
1377 // PortAllocator, as flags or otherwise.
1378 EXPECT_FALSE(raw_port_allocator->flags() &
1379 cricket::PORTALLOCATOR_ENABLE_IPV6_ON_WIFI);
1380 EXPECT_EQ(10, raw_port_allocator->max_ipv6_networks());
1381 EXPECT_TRUE(raw_port_allocator->flags() & cricket::PORTALLOCATOR_DISABLE_TCP);
1382 EXPECT_TRUE(raw_port_allocator->flags() &
1383 cricket::PORTALLOCATOR_DISABLE_COSTLY_NETWORKS);
1384 EXPECT_TRUE(raw_port_allocator->prune_turn_ports());
1385}
1386
Taylor Brandstetterf8e65772016-06-27 17:20:15 -07001387// Test that the PeerConnection initializes the port allocator passed into it,
1388// and on the correct thread.
1389TEST_F(PeerConnectionInterfaceTest,
deadbeefd21eab32017-07-26 16:50:11 -07001390 CreatePeerConnectionInitializesPortAllocatorOnNetworkThread) {
tommie7251592017-07-14 14:44:46 -07001391 std::unique_ptr<rtc::Thread> network_thread(
1392 rtc::Thread::CreateWithSocketServer());
1393 network_thread->Start();
Taylor Brandstetterf8e65772016-06-27 17:20:15 -07001394 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface> pc_factory(
1395 webrtc::CreatePeerConnectionFactory(
tommie7251592017-07-14 14:44:46 -07001396 network_thread.get(), rtc::Thread::Current(), rtc::Thread::Current(),
deadbeefd7850b22017-08-23 10:59:19 -07001397 fake_audio_capture_module_, nullptr, nullptr));
Taylor Brandstetterf8e65772016-06-27 17:20:15 -07001398 std::unique_ptr<cricket::FakePortAllocator> port_allocator(
tommie7251592017-07-14 14:44:46 -07001399 new cricket::FakePortAllocator(network_thread.get(), nullptr));
Taylor Brandstetterf8e65772016-06-27 17:20:15 -07001400 cricket::FakePortAllocator* raw_port_allocator = port_allocator.get();
1401 PeerConnectionInterface::RTCConfiguration config;
1402 rtc::scoped_refptr<PeerConnectionInterface> pc(
1403 pc_factory->CreatePeerConnection(
1404 config, nullptr, std::move(port_allocator), nullptr, &observer_));
1405 // FakePortAllocator RTC_CHECKs that it's initialized on the right thread,
1406 // so all we have to do here is check that it's initialized.
1407 EXPECT_TRUE(raw_port_allocator->initialized());
1408}
1409
deadbeef46c73892016-11-16 19:42:04 -08001410// Check that GetConfiguration returns the configuration the PeerConnection was
1411// constructed with, before SetConfiguration is called.
1412TEST_F(PeerConnectionInterfaceTest, GetConfigurationAfterCreatePeerConnection) {
1413 PeerConnectionInterface::RTCConfiguration config;
1414 config.type = PeerConnectionInterface::kRelay;
1415 CreatePeerConnection(config, nullptr);
1416
1417 PeerConnectionInterface::RTCConfiguration returned_config =
1418 pc_->GetConfiguration();
1419 EXPECT_EQ(PeerConnectionInterface::kRelay, returned_config.type);
1420}
1421
1422// Check that GetConfiguration returns the last configuration passed into
1423// SetConfiguration.
1424TEST_F(PeerConnectionInterfaceTest, GetConfigurationAfterSetConfiguration) {
1425 CreatePeerConnection();
1426
1427 PeerConnectionInterface::RTCConfiguration config;
1428 config.type = PeerConnectionInterface::kRelay;
1429 EXPECT_TRUE(pc_->SetConfiguration(config));
1430
1431 PeerConnectionInterface::RTCConfiguration returned_config =
1432 pc_->GetConfiguration();
1433 EXPECT_EQ(PeerConnectionInterface::kRelay, returned_config.type);
1434}
1435
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001436TEST_F(PeerConnectionInterfaceTest, AddStreams) {
deadbeef293e9262017-01-11 12:28:30 -08001437 CreatePeerConnectionWithoutDtls();
deadbeefab9b2d12015-10-14 11:33:11 -07001438 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001439 AddVoiceStream(kStreamLabel2);
1440 ASSERT_EQ(2u, pc_->local_streams()->count());
1441
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001442 // Test we can add multiple local streams to one peerconnection.
zhihuang9763d562016-08-05 11:14:50 -07001443 rtc::scoped_refptr<MediaStreamInterface> stream(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001444 pc_factory_->CreateLocalMediaStream(kStreamLabel3));
zhihuang9763d562016-08-05 11:14:50 -07001445 rtc::scoped_refptr<AudioTrackInterface> audio_track(
1446 pc_factory_->CreateAudioTrack(kStreamLabel3,
1447 static_cast<AudioSourceInterface*>(NULL)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001448 stream->AddTrack(audio_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +00001449 EXPECT_TRUE(pc_->AddStream(stream));
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001450 EXPECT_EQ(3u, pc_->local_streams()->count());
1451
1452 // Remove the third stream.
1453 pc_->RemoveStream(pc_->local_streams()->at(2));
1454 EXPECT_EQ(2u, pc_->local_streams()->count());
1455
1456 // Remove the second stream.
1457 pc_->RemoveStream(pc_->local_streams()->at(1));
1458 EXPECT_EQ(1u, pc_->local_streams()->count());
1459
1460 // Remove the first stream.
1461 pc_->RemoveStream(pc_->local_streams()->at(0));
1462 EXPECT_EQ(0u, pc_->local_streams()->count());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001463}
1464
deadbeefab9b2d12015-10-14 11:33:11 -07001465// Test that the created offer includes streams we added.
1466TEST_F(PeerConnectionInterfaceTest, AddedStreamsPresentInOffer) {
deadbeef293e9262017-01-11 12:28:30 -08001467 CreatePeerConnectionWithoutDtls();
deadbeefab9b2d12015-10-14 11:33:11 -07001468 AddAudioVideoStream(kStreamLabel1, "audio_track", "video_track");
kwibergd1fe2812016-04-27 06:47:29 -07001469 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001470 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07001471
1472 const cricket::ContentInfo* audio_content =
1473 cricket::GetFirstAudioContent(offer->description());
1474 const cricket::AudioContentDescription* audio_desc =
1475 static_cast<const cricket::AudioContentDescription*>(
1476 audio_content->description);
1477 EXPECT_TRUE(
1478 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
1479
1480 const cricket::ContentInfo* video_content =
1481 cricket::GetFirstVideoContent(offer->description());
1482 const cricket::VideoContentDescription* video_desc =
1483 static_cast<const cricket::VideoContentDescription*>(
1484 video_content->description);
1485 EXPECT_TRUE(
1486 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1487
1488 // Add another stream and ensure the offer includes both the old and new
1489 // streams.
1490 AddAudioVideoStream(kStreamLabel2, "audio_track2", "video_track2");
kwiberg2bbff992016-03-16 11:03:04 -07001491 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07001492
1493 audio_content = cricket::GetFirstAudioContent(offer->description());
1494 audio_desc = static_cast<const cricket::AudioContentDescription*>(
1495 audio_content->description);
1496 EXPECT_TRUE(
1497 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
1498 EXPECT_TRUE(
1499 ContainsTrack(audio_desc->streams(), kStreamLabel2, "audio_track2"));
1500
1501 video_content = cricket::GetFirstVideoContent(offer->description());
1502 video_desc = static_cast<const cricket::VideoContentDescription*>(
1503 video_content->description);
1504 EXPECT_TRUE(
1505 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1506 EXPECT_TRUE(
1507 ContainsTrack(video_desc->streams(), kStreamLabel2, "video_track2"));
1508}
1509
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001510TEST_F(PeerConnectionInterfaceTest, RemoveStream) {
deadbeef293e9262017-01-11 12:28:30 -08001511 CreatePeerConnectionWithoutDtls();
deadbeefab9b2d12015-10-14 11:33:11 -07001512 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001513 ASSERT_EQ(1u, pc_->local_streams()->count());
1514 pc_->RemoveStream(pc_->local_streams()->at(0));
1515 EXPECT_EQ(0u, pc_->local_streams()->count());
1516}
1517
deadbeefe1f9d832016-01-14 15:35:42 -08001518// Test for AddTrack and RemoveTrack methods.
1519// Tests that the created offer includes tracks we added,
1520// and that the RtpSenders are created correctly.
1521// Also tests that RemoveTrack removes the tracks from subsequent offers.
1522TEST_F(PeerConnectionInterfaceTest, AddTrackRemoveTrack) {
deadbeef293e9262017-01-11 12:28:30 -08001523 CreatePeerConnectionWithoutDtls();
deadbeefe1f9d832016-01-14 15:35:42 -08001524 // Create a dummy stream, so tracks share a stream label.
zhihuang9763d562016-08-05 11:14:50 -07001525 rtc::scoped_refptr<MediaStreamInterface> stream(
deadbeefe1f9d832016-01-14 15:35:42 -08001526 pc_factory_->CreateLocalMediaStream(kStreamLabel1));
1527 std::vector<MediaStreamInterface*> stream_list;
1528 stream_list.push_back(stream.get());
zhihuang9763d562016-08-05 11:14:50 -07001529 rtc::scoped_refptr<AudioTrackInterface> audio_track(
deadbeefe1f9d832016-01-14 15:35:42 -08001530 pc_factory_->CreateAudioTrack("audio_track", nullptr));
zhihuang9763d562016-08-05 11:14:50 -07001531 rtc::scoped_refptr<VideoTrackInterface> video_track(
1532 pc_factory_->CreateVideoTrack(
deadbeef112b2e92017-02-10 20:13:37 -08001533 "video_track", pc_factory_->CreateVideoSource(
1534 std::unique_ptr<cricket::VideoCapturer>(
1535 new cricket::FakeVideoCapturer()))));
deadbeefe1f9d832016-01-14 15:35:42 -08001536 auto audio_sender = pc_->AddTrack(audio_track, stream_list);
1537 auto video_sender = pc_->AddTrack(video_track, stream_list);
deadbeefa601f5c2016-06-06 14:27:39 -07001538 EXPECT_EQ(1UL, audio_sender->stream_ids().size());
1539 EXPECT_EQ(kStreamLabel1, audio_sender->stream_ids()[0]);
deadbeefe1f9d832016-01-14 15:35:42 -08001540 EXPECT_EQ("audio_track", audio_sender->id());
1541 EXPECT_EQ(audio_track, audio_sender->track());
deadbeefa601f5c2016-06-06 14:27:39 -07001542 EXPECT_EQ(1UL, video_sender->stream_ids().size());
1543 EXPECT_EQ(kStreamLabel1, video_sender->stream_ids()[0]);
deadbeefe1f9d832016-01-14 15:35:42 -08001544 EXPECT_EQ("video_track", video_sender->id());
1545 EXPECT_EQ(video_track, video_sender->track());
1546
1547 // Now create an offer and check for the senders.
kwibergd1fe2812016-04-27 06:47:29 -07001548 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001549 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefe1f9d832016-01-14 15:35:42 -08001550
1551 const cricket::ContentInfo* audio_content =
1552 cricket::GetFirstAudioContent(offer->description());
1553 const cricket::AudioContentDescription* audio_desc =
1554 static_cast<const cricket::AudioContentDescription*>(
1555 audio_content->description);
1556 EXPECT_TRUE(
1557 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
1558
1559 const cricket::ContentInfo* video_content =
1560 cricket::GetFirstVideoContent(offer->description());
1561 const cricket::VideoContentDescription* video_desc =
1562 static_cast<const cricket::VideoContentDescription*>(
1563 video_content->description);
1564 EXPECT_TRUE(
1565 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1566
Steve Antondb45ca82017-09-11 18:27:34 -07001567 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefe1f9d832016-01-14 15:35:42 -08001568
1569 // Now try removing the tracks.
1570 EXPECT_TRUE(pc_->RemoveTrack(audio_sender));
1571 EXPECT_TRUE(pc_->RemoveTrack(video_sender));
1572
1573 // Create a new offer and ensure it doesn't contain the removed senders.
kwiberg2bbff992016-03-16 11:03:04 -07001574 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefe1f9d832016-01-14 15:35:42 -08001575
1576 audio_content = cricket::GetFirstAudioContent(offer->description());
1577 audio_desc = static_cast<const cricket::AudioContentDescription*>(
1578 audio_content->description);
1579 EXPECT_FALSE(
1580 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
1581
1582 video_content = cricket::GetFirstVideoContent(offer->description());
1583 video_desc = static_cast<const cricket::VideoContentDescription*>(
1584 video_content->description);
1585 EXPECT_FALSE(
1586 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1587
Steve Antondb45ca82017-09-11 18:27:34 -07001588 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefe1f9d832016-01-14 15:35:42 -08001589
1590 // Calling RemoveTrack on a sender no longer attached to a PeerConnection
1591 // should return false.
1592 EXPECT_FALSE(pc_->RemoveTrack(audio_sender));
1593 EXPECT_FALSE(pc_->RemoveTrack(video_sender));
1594}
1595
1596// Test creating senders without a stream specified,
1597// expecting a random stream ID to be generated.
1598TEST_F(PeerConnectionInterfaceTest, AddTrackWithoutStream) {
deadbeef293e9262017-01-11 12:28:30 -08001599 CreatePeerConnectionWithoutDtls();
deadbeefe1f9d832016-01-14 15:35:42 -08001600 // Create a dummy stream, so tracks share a stream label.
zhihuang9763d562016-08-05 11:14:50 -07001601 rtc::scoped_refptr<AudioTrackInterface> audio_track(
deadbeefe1f9d832016-01-14 15:35:42 -08001602 pc_factory_->CreateAudioTrack("audio_track", nullptr));
zhihuang9763d562016-08-05 11:14:50 -07001603 rtc::scoped_refptr<VideoTrackInterface> video_track(
1604 pc_factory_->CreateVideoTrack(
deadbeef112b2e92017-02-10 20:13:37 -08001605 "video_track", pc_factory_->CreateVideoSource(
1606 std::unique_ptr<cricket::VideoCapturer>(
1607 new cricket::FakeVideoCapturer()))));
deadbeefe1f9d832016-01-14 15:35:42 -08001608 auto audio_sender =
1609 pc_->AddTrack(audio_track, std::vector<MediaStreamInterface*>());
1610 auto video_sender =
1611 pc_->AddTrack(video_track, std::vector<MediaStreamInterface*>());
1612 EXPECT_EQ("audio_track", audio_sender->id());
1613 EXPECT_EQ(audio_track, audio_sender->track());
1614 EXPECT_EQ("video_track", video_sender->id());
1615 EXPECT_EQ(video_track, video_sender->track());
1616 // If the ID is truly a random GUID, it should be infinitely unlikely they
1617 // will be the same.
deadbeefa601f5c2016-06-06 14:27:39 -07001618 EXPECT_NE(video_sender->stream_ids(), audio_sender->stream_ids());
deadbeefe1f9d832016-01-14 15:35:42 -08001619}
1620
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001621TEST_F(PeerConnectionInterfaceTest, CreateOfferReceiveAnswer) {
1622 InitiateCall();
1623 WaitAndVerifyOnAddStream(kStreamLabel1);
1624 VerifyRemoteRtpHeaderExtensions();
1625}
1626
1627TEST_F(PeerConnectionInterfaceTest, CreateOfferReceivePrAnswerAndAnswer) {
deadbeef293e9262017-01-11 12:28:30 -08001628 CreatePeerConnectionWithoutDtls();
deadbeefab9b2d12015-10-14 11:33:11 -07001629 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001630 CreateOfferAsLocalDescription();
1631 std::string offer;
1632 EXPECT_TRUE(pc_->local_description()->ToString(&offer));
1633 CreatePrAnswerAndAnswerAsRemoteDescription(offer);
1634 WaitAndVerifyOnAddStream(kStreamLabel1);
1635}
1636
1637TEST_F(PeerConnectionInterfaceTest, ReceiveOfferCreateAnswer) {
deadbeef293e9262017-01-11 12:28:30 -08001638 CreatePeerConnectionWithoutDtls();
deadbeefab9b2d12015-10-14 11:33:11 -07001639 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001640
1641 CreateOfferAsRemoteDescription();
1642 CreateAnswerAsLocalDescription();
1643
1644 WaitAndVerifyOnAddStream(kStreamLabel1);
1645}
1646
1647TEST_F(PeerConnectionInterfaceTest, ReceiveOfferCreatePrAnswerAndAnswer) {
deadbeef293e9262017-01-11 12:28:30 -08001648 CreatePeerConnectionWithoutDtls();
deadbeefab9b2d12015-10-14 11:33:11 -07001649 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001650
1651 CreateOfferAsRemoteDescription();
1652 CreatePrAnswerAsLocalDescription();
1653 CreateAnswerAsLocalDescription();
1654
1655 WaitAndVerifyOnAddStream(kStreamLabel1);
1656}
1657
1658TEST_F(PeerConnectionInterfaceTest, Renegotiate) {
1659 InitiateCall();
1660 ASSERT_EQ(1u, pc_->remote_streams()->count());
1661 pc_->RemoveStream(pc_->local_streams()->at(0));
1662 CreateOfferReceiveAnswer();
1663 EXPECT_EQ(0u, pc_->remote_streams()->count());
deadbeefab9b2d12015-10-14 11:33:11 -07001664 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001665 CreateOfferReceiveAnswer();
1666}
1667
1668// Tests that after negotiating an audio only call, the respondent can perform a
1669// renegotiation that removes the audio stream.
1670TEST_F(PeerConnectionInterfaceTest, RenegotiateAudioOnly) {
deadbeef293e9262017-01-11 12:28:30 -08001671 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001672 AddVoiceStream(kStreamLabel1);
1673 CreateOfferAsRemoteDescription();
1674 CreateAnswerAsLocalDescription();
1675
1676 ASSERT_EQ(1u, pc_->remote_streams()->count());
1677 pc_->RemoveStream(pc_->local_streams()->at(0));
1678 CreateOfferReceiveAnswer();
1679 EXPECT_EQ(0u, pc_->remote_streams()->count());
1680}
1681
1682// Test that candidates are generated and that we can parse our own candidates.
1683TEST_F(PeerConnectionInterfaceTest, IceCandidates) {
deadbeef293e9262017-01-11 12:28:30 -08001684 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001685
1686 EXPECT_FALSE(pc_->AddIceCandidate(observer_.last_candidate_.get()));
1687 // SetRemoteDescription takes ownership of offer.
kwibergd1fe2812016-04-27 06:47:29 -07001688 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefab9b2d12015-10-14 11:33:11 -07001689 AddVideoStream(kStreamLabel1);
deadbeefc80741f2015-10-22 13:14:45 -07001690 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07001691 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001692
1693 // SetLocalDescription takes ownership of answer.
kwibergd1fe2812016-04-27 06:47:29 -07001694 std::unique_ptr<SessionDescriptionInterface> answer;
deadbeefc80741f2015-10-22 13:14:45 -07001695 EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07001696 EXPECT_TRUE(DoSetLocalDescription(std::move(answer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001697
1698 EXPECT_TRUE_WAIT(observer_.last_candidate_.get() != NULL, kTimeout);
1699 EXPECT_TRUE_WAIT(observer_.ice_complete_, kTimeout);
1700
1701 EXPECT_TRUE(pc_->AddIceCandidate(observer_.last_candidate_.get()));
1702}
1703
deadbeefab9b2d12015-10-14 11:33:11 -07001704// Test that CreateOffer and CreateAnswer will fail if the track labels are
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001705// not unique.
1706TEST_F(PeerConnectionInterfaceTest, CreateOfferAnswerWithInvalidStream) {
deadbeef293e9262017-01-11 12:28:30 -08001707 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001708 // Create a regular offer for the CreateAnswer test later.
kwibergd1fe2812016-04-27 06:47:29 -07001709 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefc80741f2015-10-22 13:14:45 -07001710 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
kwiberg2bbff992016-03-16 11:03:04 -07001711 EXPECT_TRUE(offer);
1712 offer.reset();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001713
1714 // Create a local stream with audio&video tracks having same label.
1715 AddAudioVideoStream(kStreamLabel1, "track_label", "track_label");
1716
1717 // Test CreateOffer
deadbeefc80741f2015-10-22 13:14:45 -07001718 EXPECT_FALSE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001719
1720 // Test CreateAnswer
kwibergd1fe2812016-04-27 06:47:29 -07001721 std::unique_ptr<SessionDescriptionInterface> answer;
deadbeefc80741f2015-10-22 13:14:45 -07001722 EXPECT_FALSE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001723}
1724
1725// Test that we will get different SSRCs for each tracks in the offer and answer
1726// we created.
1727TEST_F(PeerConnectionInterfaceTest, SsrcInOfferAnswer) {
deadbeef293e9262017-01-11 12:28:30 -08001728 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001729 // Create a local stream with audio&video tracks having different labels.
1730 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
1731
1732 // Test CreateOffer
kwibergd1fe2812016-04-27 06:47:29 -07001733 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001734 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001735 int audio_ssrc = 0;
1736 int video_ssrc = 0;
1737 EXPECT_TRUE(GetFirstSsrc(GetFirstAudioContent(offer->description()),
1738 &audio_ssrc));
1739 EXPECT_TRUE(GetFirstSsrc(GetFirstVideoContent(offer->description()),
1740 &video_ssrc));
1741 EXPECT_NE(audio_ssrc, video_ssrc);
1742
1743 // Test CreateAnswer
Steve Antondb45ca82017-09-11 18:27:34 -07001744 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
kwibergd1fe2812016-04-27 06:47:29 -07001745 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -07001746 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001747 audio_ssrc = 0;
1748 video_ssrc = 0;
1749 EXPECT_TRUE(GetFirstSsrc(GetFirstAudioContent(answer->description()),
1750 &audio_ssrc));
1751 EXPECT_TRUE(GetFirstSsrc(GetFirstVideoContent(answer->description()),
1752 &video_ssrc));
1753 EXPECT_NE(audio_ssrc, video_ssrc);
1754}
1755
deadbeefeb459812015-12-15 19:24:43 -08001756// Test that it's possible to call AddTrack on a MediaStream after adding
1757// the stream to a PeerConnection.
1758// TODO(deadbeef): Remove this test once this behavior is no longer supported.
1759TEST_F(PeerConnectionInterfaceTest, AddTrackAfterAddStream) {
deadbeef293e9262017-01-11 12:28:30 -08001760 CreatePeerConnectionWithoutDtls();
deadbeefeb459812015-12-15 19:24:43 -08001761 // Create audio stream and add to PeerConnection.
1762 AddVoiceStream(kStreamLabel1);
1763 MediaStreamInterface* stream = pc_->local_streams()->at(0);
1764
1765 // Add video track to the audio-only stream.
zhihuang9763d562016-08-05 11:14:50 -07001766 rtc::scoped_refptr<VideoTrackInterface> video_track(
1767 pc_factory_->CreateVideoTrack(
deadbeef112b2e92017-02-10 20:13:37 -08001768 "video_label", pc_factory_->CreateVideoSource(
1769 std::unique_ptr<cricket::VideoCapturer>(
1770 new cricket::FakeVideoCapturer()))));
deadbeefeb459812015-12-15 19:24:43 -08001771 stream->AddTrack(video_track.get());
1772
kwibergd1fe2812016-04-27 06:47:29 -07001773 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001774 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefeb459812015-12-15 19:24:43 -08001775
1776 const cricket::MediaContentDescription* video_desc =
1777 cricket::GetFirstVideoContentDescription(offer->description());
1778 EXPECT_TRUE(video_desc != nullptr);
1779}
1780
1781// Test that it's possible to call RemoveTrack on a MediaStream after adding
1782// the stream to a PeerConnection.
1783// TODO(deadbeef): Remove this test once this behavior is no longer supported.
1784TEST_F(PeerConnectionInterfaceTest, RemoveTrackAfterAddStream) {
deadbeef293e9262017-01-11 12:28:30 -08001785 CreatePeerConnectionWithoutDtls();
deadbeefeb459812015-12-15 19:24:43 -08001786 // Create audio/video stream and add to PeerConnection.
1787 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
1788 MediaStreamInterface* stream = pc_->local_streams()->at(0);
1789
1790 // Remove the video track.
1791 stream->RemoveTrack(stream->GetVideoTracks()[0]);
1792
kwibergd1fe2812016-04-27 06:47:29 -07001793 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001794 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefeb459812015-12-15 19:24:43 -08001795
1796 const cricket::MediaContentDescription* video_desc =
1797 cricket::GetFirstVideoContentDescription(offer->description());
1798 EXPECT_TRUE(video_desc == nullptr);
1799}
1800
deadbeef1dcb1642017-03-29 21:08:16 -07001801// Verify that CreateDtmfSender only succeeds if called with a valid local
1802// track. Other aspects of DtmfSenders are tested in
1803// peerconnection_integrationtest.cc.
1804TEST_F(PeerConnectionInterfaceTest, CreateDtmfSenderWithInvalidParams) {
1805 CreatePeerConnection();
1806 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
1807 EXPECT_EQ(nullptr, pc_->CreateDtmfSender(nullptr));
1808 rtc::scoped_refptr<webrtc::AudioTrackInterface> non_localtrack(
1809 pc_factory_->CreateAudioTrack("dummy_track", nullptr));
1810 EXPECT_EQ(nullptr, pc_->CreateDtmfSender(non_localtrack));
1811}
1812
deadbeefbd7d8f72015-12-18 16:58:44 -08001813// Test creating a sender with a stream ID, and ensure the ID is populated
1814// in the offer.
1815TEST_F(PeerConnectionInterfaceTest, CreateSenderWithStream) {
deadbeef293e9262017-01-11 12:28:30 -08001816 CreatePeerConnectionWithoutDtls();
deadbeefbd7d8f72015-12-18 16:58:44 -08001817 pc_->CreateSender("video", kStreamLabel1);
1818
kwibergd1fe2812016-04-27 06:47:29 -07001819 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001820 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefbd7d8f72015-12-18 16:58:44 -08001821
1822 const cricket::MediaContentDescription* video_desc =
1823 cricket::GetFirstVideoContentDescription(offer->description());
1824 ASSERT_TRUE(video_desc != nullptr);
1825 ASSERT_EQ(1u, video_desc->streams().size());
1826 EXPECT_EQ(kStreamLabel1, video_desc->streams()[0].sync_label);
1827}
1828
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001829// Test that we can specify a certain track that we want statistics about.
1830TEST_F(PeerConnectionInterfaceTest, GetStatsForSpecificTrack) {
1831 InitiateCall();
1832 ASSERT_LT(0u, pc_->remote_streams()->count());
1833 ASSERT_LT(0u, pc_->remote_streams()->at(0)->GetAudioTracks().size());
zhihuang9763d562016-08-05 11:14:50 -07001834 rtc::scoped_refptr<MediaStreamTrackInterface> remote_audio =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001835 pc_->remote_streams()->at(0)->GetAudioTracks()[0];
1836 EXPECT_TRUE(DoGetStats(remote_audio));
1837
1838 // Remove the stream. Since we are sending to our selves the local
1839 // and the remote stream is the same.
1840 pc_->RemoveStream(pc_->local_streams()->at(0));
1841 // Do a re-negotiation.
1842 CreateOfferReceiveAnswer();
1843
1844 ASSERT_EQ(0u, pc_->remote_streams()->count());
1845
1846 // Test that we still can get statistics for the old track. Even if it is not
1847 // sent any longer.
1848 EXPECT_TRUE(DoGetStats(remote_audio));
1849}
1850
1851// Test that we can get stats on a video track.
1852TEST_F(PeerConnectionInterfaceTest, GetStatsForVideoTrack) {
1853 InitiateCall();
1854 ASSERT_LT(0u, pc_->remote_streams()->count());
1855 ASSERT_LT(0u, pc_->remote_streams()->at(0)->GetVideoTracks().size());
zhihuang9763d562016-08-05 11:14:50 -07001856 rtc::scoped_refptr<MediaStreamTrackInterface> remote_video =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001857 pc_->remote_streams()->at(0)->GetVideoTracks()[0];
1858 EXPECT_TRUE(DoGetStats(remote_video));
1859}
1860
1861// Test that we don't get statistics for an invalid track.
zhihuange9e94c32016-11-04 11:38:15 -07001862TEST_F(PeerConnectionInterfaceTest, GetStatsForInvalidTrack) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001863 InitiateCall();
zhihuang9763d562016-08-05 11:14:50 -07001864 rtc::scoped_refptr<AudioTrackInterface> unknown_audio_track(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001865 pc_factory_->CreateAudioTrack("unknown track", NULL));
1866 EXPECT_FALSE(DoGetStats(unknown_audio_track));
1867}
1868
1869// This test setup two RTP data channels in loop back.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001870TEST_F(PeerConnectionInterfaceTest, TestDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001871 FakeConstraints constraints;
1872 constraints.SetAllowRtpDataChannels();
1873 CreatePeerConnection(&constraints);
zhihuang9763d562016-08-05 11:14:50 -07001874 rtc::scoped_refptr<DataChannelInterface> data1 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001875 pc_->CreateDataChannel("test1", NULL);
zhihuang9763d562016-08-05 11:14:50 -07001876 rtc::scoped_refptr<DataChannelInterface> data2 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001877 pc_->CreateDataChannel("test2", NULL);
1878 ASSERT_TRUE(data1 != NULL);
kwibergd1fe2812016-04-27 06:47:29 -07001879 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001880 new MockDataChannelObserver(data1));
kwibergd1fe2812016-04-27 06:47:29 -07001881 std::unique_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001882 new MockDataChannelObserver(data2));
1883
1884 EXPECT_EQ(DataChannelInterface::kConnecting, data1->state());
1885 EXPECT_EQ(DataChannelInterface::kConnecting, data2->state());
1886 std::string data_to_send1 = "testing testing";
1887 std::string data_to_send2 = "testing something else";
1888 EXPECT_FALSE(data1->Send(DataBuffer(data_to_send1)));
1889
1890 CreateOfferReceiveAnswer();
1891 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1892 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
1893
1894 EXPECT_EQ(DataChannelInterface::kOpen, data1->state());
1895 EXPECT_EQ(DataChannelInterface::kOpen, data2->state());
1896 EXPECT_TRUE(data1->Send(DataBuffer(data_to_send1)));
1897 EXPECT_TRUE(data2->Send(DataBuffer(data_to_send2)));
1898
1899 EXPECT_EQ_WAIT(data_to_send1, observer1->last_message(), kTimeout);
1900 EXPECT_EQ_WAIT(data_to_send2, observer2->last_message(), kTimeout);
1901
1902 data1->Close();
1903 EXPECT_EQ(DataChannelInterface::kClosing, data1->state());
1904 CreateOfferReceiveAnswer();
1905 EXPECT_FALSE(observer1->IsOpen());
1906 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
1907 EXPECT_TRUE(observer2->IsOpen());
1908
1909 data_to_send2 = "testing something else again";
1910 EXPECT_TRUE(data2->Send(DataBuffer(data_to_send2)));
1911
1912 EXPECT_EQ_WAIT(data_to_send2, observer2->last_message(), kTimeout);
1913}
1914
1915// This test verifies that sendnig binary data over RTP data channels should
1916// fail.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001917TEST_F(PeerConnectionInterfaceTest, TestSendBinaryOnRtpDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001918 FakeConstraints constraints;
1919 constraints.SetAllowRtpDataChannels();
1920 CreatePeerConnection(&constraints);
zhihuang9763d562016-08-05 11:14:50 -07001921 rtc::scoped_refptr<DataChannelInterface> data1 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001922 pc_->CreateDataChannel("test1", NULL);
zhihuang9763d562016-08-05 11:14:50 -07001923 rtc::scoped_refptr<DataChannelInterface> data2 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001924 pc_->CreateDataChannel("test2", NULL);
1925 ASSERT_TRUE(data1 != NULL);
kwibergd1fe2812016-04-27 06:47:29 -07001926 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001927 new MockDataChannelObserver(data1));
kwibergd1fe2812016-04-27 06:47:29 -07001928 std::unique_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001929 new MockDataChannelObserver(data2));
1930
1931 EXPECT_EQ(DataChannelInterface::kConnecting, data1->state());
1932 EXPECT_EQ(DataChannelInterface::kConnecting, data2->state());
1933
1934 CreateOfferReceiveAnswer();
1935 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1936 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
1937
1938 EXPECT_EQ(DataChannelInterface::kOpen, data1->state());
1939 EXPECT_EQ(DataChannelInterface::kOpen, data2->state());
1940
jbaucheec21bd2016-03-20 06:15:43 -07001941 rtc::CopyOnWriteBuffer buffer("test", 4);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001942 EXPECT_FALSE(data1->Send(DataBuffer(buffer, true)));
1943}
1944
1945// This test setup a RTP data channels in loop back and test that a channel is
1946// opened even if the remote end answer with a zero SSRC.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001947TEST_F(PeerConnectionInterfaceTest, TestSendOnlyDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001948 FakeConstraints constraints;
1949 constraints.SetAllowRtpDataChannels();
1950 CreatePeerConnection(&constraints);
zhihuang9763d562016-08-05 11:14:50 -07001951 rtc::scoped_refptr<DataChannelInterface> data1 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001952 pc_->CreateDataChannel("test1", NULL);
kwibergd1fe2812016-04-27 06:47:29 -07001953 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001954 new MockDataChannelObserver(data1));
1955
1956 CreateOfferReceiveAnswerWithoutSsrc();
1957
1958 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1959
1960 data1->Close();
1961 EXPECT_EQ(DataChannelInterface::kClosing, data1->state());
1962 CreateOfferReceiveAnswerWithoutSsrc();
1963 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
1964 EXPECT_FALSE(observer1->IsOpen());
1965}
1966
1967// This test that if a data channel is added in an answer a receive only channel
1968// channel is created.
1969TEST_F(PeerConnectionInterfaceTest, TestReceiveOnlyDataChannel) {
1970 FakeConstraints constraints;
1971 constraints.SetAllowRtpDataChannels();
1972 CreatePeerConnection(&constraints);
1973
1974 std::string offer_label = "offer_channel";
zhihuang9763d562016-08-05 11:14:50 -07001975 rtc::scoped_refptr<DataChannelInterface> offer_channel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001976 pc_->CreateDataChannel(offer_label, NULL);
1977
1978 CreateOfferAsLocalDescription();
1979
1980 // Replace the data channel label in the offer and apply it as an answer.
1981 std::string receive_label = "answer_channel";
1982 std::string sdp;
1983 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001984 rtc::replace_substrs(offer_label.c_str(), offer_label.length(),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001985 receive_label.c_str(), receive_label.length(),
1986 &sdp);
1987 CreateAnswerAsRemoteDescription(sdp);
1988
1989 // Verify that a new incoming data channel has been created and that
1990 // it is open but can't we written to.
1991 ASSERT_TRUE(observer_.last_datachannel_ != NULL);
1992 DataChannelInterface* received_channel = observer_.last_datachannel_;
1993 EXPECT_EQ(DataChannelInterface::kConnecting, received_channel->state());
1994 EXPECT_EQ(receive_label, received_channel->label());
1995 EXPECT_FALSE(received_channel->Send(DataBuffer("something")));
1996
1997 // Verify that the channel we initially offered has been rejected.
1998 EXPECT_EQ(DataChannelInterface::kClosed, offer_channel->state());
1999
2000 // Do another offer / answer exchange and verify that the data channel is
2001 // opened.
2002 CreateOfferReceiveAnswer();
2003 EXPECT_EQ_WAIT(DataChannelInterface::kOpen, received_channel->state(),
2004 kTimeout);
2005}
2006
2007// This test that no data channel is returned if a reliable channel is
2008// requested.
2009// TODO(perkj): Remove this test once reliable channels are implemented.
2010TEST_F(PeerConnectionInterfaceTest, CreateReliableRtpDataChannelShouldFail) {
2011 FakeConstraints constraints;
2012 constraints.SetAllowRtpDataChannels();
2013 CreatePeerConnection(&constraints);
2014
2015 std::string label = "test";
2016 webrtc::DataChannelInit config;
2017 config.reliable = true;
zhihuang9763d562016-08-05 11:14:50 -07002018 rtc::scoped_refptr<DataChannelInterface> channel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002019 pc_->CreateDataChannel(label, &config);
2020 EXPECT_TRUE(channel == NULL);
2021}
2022
deadbeefab9b2d12015-10-14 11:33:11 -07002023// Verifies that duplicated label is not allowed for RTP data channel.
2024TEST_F(PeerConnectionInterfaceTest, RtpDuplicatedLabelNotAllowed) {
2025 FakeConstraints constraints;
2026 constraints.SetAllowRtpDataChannels();
2027 CreatePeerConnection(&constraints);
2028
2029 std::string label = "test";
zhihuang9763d562016-08-05 11:14:50 -07002030 rtc::scoped_refptr<DataChannelInterface> channel =
deadbeefab9b2d12015-10-14 11:33:11 -07002031 pc_->CreateDataChannel(label, nullptr);
2032 EXPECT_NE(channel, nullptr);
2033
zhihuang9763d562016-08-05 11:14:50 -07002034 rtc::scoped_refptr<DataChannelInterface> dup_channel =
deadbeefab9b2d12015-10-14 11:33:11 -07002035 pc_->CreateDataChannel(label, nullptr);
2036 EXPECT_EQ(dup_channel, nullptr);
2037}
2038
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002039// This tests that a SCTP data channel is returned using different
2040// DataChannelInit configurations.
2041TEST_F(PeerConnectionInterfaceTest, CreateSctpDataChannel) {
2042 FakeConstraints constraints;
2043 constraints.SetAllowDtlsSctpDataChannels();
2044 CreatePeerConnection(&constraints);
2045
2046 webrtc::DataChannelInit config;
2047
zhihuang9763d562016-08-05 11:14:50 -07002048 rtc::scoped_refptr<DataChannelInterface> channel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002049 pc_->CreateDataChannel("1", &config);
2050 EXPECT_TRUE(channel != NULL);
2051 EXPECT_TRUE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00002052 EXPECT_TRUE(observer_.renegotiation_needed_);
2053 observer_.renegotiation_needed_ = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002054
2055 config.ordered = false;
2056 channel = pc_->CreateDataChannel("2", &config);
2057 EXPECT_TRUE(channel != NULL);
2058 EXPECT_TRUE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00002059 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002060
2061 config.ordered = true;
2062 config.maxRetransmits = 0;
2063 channel = pc_->CreateDataChannel("3", &config);
2064 EXPECT_TRUE(channel != NULL);
2065 EXPECT_FALSE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00002066 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002067
2068 config.maxRetransmits = -1;
2069 config.maxRetransmitTime = 0;
2070 channel = pc_->CreateDataChannel("4", &config);
2071 EXPECT_TRUE(channel != NULL);
2072 EXPECT_FALSE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00002073 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002074}
2075
2076// This tests that no data channel is returned if both maxRetransmits and
2077// maxRetransmitTime are set for SCTP data channels.
2078TEST_F(PeerConnectionInterfaceTest,
2079 CreateSctpDataChannelShouldFailForInvalidConfig) {
2080 FakeConstraints constraints;
2081 constraints.SetAllowDtlsSctpDataChannels();
2082 CreatePeerConnection(&constraints);
2083
2084 std::string label = "test";
2085 webrtc::DataChannelInit config;
2086 config.maxRetransmits = 0;
2087 config.maxRetransmitTime = 0;
2088
zhihuang9763d562016-08-05 11:14:50 -07002089 rtc::scoped_refptr<DataChannelInterface> channel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002090 pc_->CreateDataChannel(label, &config);
2091 EXPECT_TRUE(channel == NULL);
2092}
2093
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002094// The test verifies that creating a SCTP data channel with an id already in use
2095// or out of range should fail.
2096TEST_F(PeerConnectionInterfaceTest,
2097 CreateSctpDataChannelWithInvalidIdShouldFail) {
2098 FakeConstraints constraints;
2099 constraints.SetAllowDtlsSctpDataChannels();
2100 CreatePeerConnection(&constraints);
2101
2102 webrtc::DataChannelInit config;
zhihuang9763d562016-08-05 11:14:50 -07002103 rtc::scoped_refptr<DataChannelInterface> channel;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002104
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00002105 config.id = 1;
2106 channel = pc_->CreateDataChannel("1", &config);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002107 EXPECT_TRUE(channel != NULL);
2108 EXPECT_EQ(1, channel->id());
2109
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002110 channel = pc_->CreateDataChannel("x", &config);
2111 EXPECT_TRUE(channel == NULL);
2112
2113 config.id = cricket::kMaxSctpSid;
2114 channel = pc_->CreateDataChannel("max", &config);
2115 EXPECT_TRUE(channel != NULL);
2116 EXPECT_EQ(config.id, channel->id());
2117
2118 config.id = cricket::kMaxSctpSid + 1;
2119 channel = pc_->CreateDataChannel("x", &config);
2120 EXPECT_TRUE(channel == NULL);
2121}
2122
deadbeefab9b2d12015-10-14 11:33:11 -07002123// Verifies that duplicated label is allowed for SCTP data channel.
2124TEST_F(PeerConnectionInterfaceTest, SctpDuplicatedLabelAllowed) {
2125 FakeConstraints constraints;
2126 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2127 true);
2128 CreatePeerConnection(&constraints);
2129
2130 std::string label = "test";
zhihuang9763d562016-08-05 11:14:50 -07002131 rtc::scoped_refptr<DataChannelInterface> channel =
deadbeefab9b2d12015-10-14 11:33:11 -07002132 pc_->CreateDataChannel(label, nullptr);
2133 EXPECT_NE(channel, nullptr);
2134
zhihuang9763d562016-08-05 11:14:50 -07002135 rtc::scoped_refptr<DataChannelInterface> dup_channel =
deadbeefab9b2d12015-10-14 11:33:11 -07002136 pc_->CreateDataChannel(label, nullptr);
2137 EXPECT_NE(dup_channel, nullptr);
2138}
2139
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00002140// This test verifies that OnRenegotiationNeeded is fired for every new RTP
2141// DataChannel.
2142TEST_F(PeerConnectionInterfaceTest, RenegotiationNeededForNewRtpDataChannel) {
2143 FakeConstraints constraints;
2144 constraints.SetAllowRtpDataChannels();
2145 CreatePeerConnection(&constraints);
2146
zhihuang9763d562016-08-05 11:14:50 -07002147 rtc::scoped_refptr<DataChannelInterface> dc1 =
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00002148 pc_->CreateDataChannel("test1", NULL);
2149 EXPECT_TRUE(observer_.renegotiation_needed_);
2150 observer_.renegotiation_needed_ = false;
2151
zhihuang9763d562016-08-05 11:14:50 -07002152 rtc::scoped_refptr<DataChannelInterface> dc2 =
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00002153 pc_->CreateDataChannel("test2", NULL);
2154 EXPECT_TRUE(observer_.renegotiation_needed_);
2155}
2156
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002157// This test that a data channel closes when a PeerConnection is deleted/closed.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002158TEST_F(PeerConnectionInterfaceTest, DataChannelCloseWhenPeerConnectionClose) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002159 FakeConstraints constraints;
2160 constraints.SetAllowRtpDataChannels();
2161 CreatePeerConnection(&constraints);
2162
zhihuang9763d562016-08-05 11:14:50 -07002163 rtc::scoped_refptr<DataChannelInterface> data1 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002164 pc_->CreateDataChannel("test1", NULL);
zhihuang9763d562016-08-05 11:14:50 -07002165 rtc::scoped_refptr<DataChannelInterface> data2 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002166 pc_->CreateDataChannel("test2", NULL);
2167 ASSERT_TRUE(data1 != NULL);
kwibergd1fe2812016-04-27 06:47:29 -07002168 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002169 new MockDataChannelObserver(data1));
kwibergd1fe2812016-04-27 06:47:29 -07002170 std::unique_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002171 new MockDataChannelObserver(data2));
2172
2173 CreateOfferReceiveAnswer();
2174 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
2175 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
2176
2177 ReleasePeerConnection();
2178 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
2179 EXPECT_EQ(DataChannelInterface::kClosed, data2->state());
2180}
2181
2182// This test that data channels can be rejected in an answer.
2183TEST_F(PeerConnectionInterfaceTest, TestRejectDataChannelInAnswer) {
2184 FakeConstraints constraints;
2185 constraints.SetAllowRtpDataChannels();
2186 CreatePeerConnection(&constraints);
2187
zhihuang9763d562016-08-05 11:14:50 -07002188 rtc::scoped_refptr<DataChannelInterface> offer_channel(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002189 pc_->CreateDataChannel("offer_channel", NULL));
2190
2191 CreateOfferAsLocalDescription();
2192
2193 // Create an answer where the m-line for data channels are rejected.
2194 std::string sdp;
2195 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07002196 std::unique_ptr<SessionDescriptionInterface> answer(
2197 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
2198 sdp, nullptr));
2199 ASSERT_TRUE(answer);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002200 cricket::ContentInfo* data_info =
2201 answer->description()->GetContentByName("data");
2202 data_info->rejected = true;
2203
Steve Antondb45ca82017-09-11 18:27:34 -07002204 DoSetRemoteDescription(std::move(answer));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002205 EXPECT_EQ(DataChannelInterface::kClosed, offer_channel->state());
2206}
2207
2208// Test that we can create a session description from an SDP string from
2209// FireFox, use it as a remote session description, generate an answer and use
2210// the answer as a local description.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002211TEST_F(PeerConnectionInterfaceTest, ReceiveFireFoxOffer) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002212 FakeConstraints constraints;
2213 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2214 true);
2215 CreatePeerConnection(&constraints);
2216 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
Steve Antondb45ca82017-09-11 18:27:34 -07002217 std::unique_ptr<SessionDescriptionInterface> desc(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002218 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
Steve Antondb45ca82017-09-11 18:27:34 -07002219 webrtc::kFireFoxSdpOffer, nullptr));
2220 EXPECT_TRUE(DoSetSessionDescription(std::move(desc), false));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002221 CreateAnswerAsLocalDescription();
2222 ASSERT_TRUE(pc_->local_description() != NULL);
2223 ASSERT_TRUE(pc_->remote_description() != NULL);
2224
2225 const cricket::ContentInfo* content =
2226 cricket::GetFirstAudioContent(pc_->local_description()->description());
2227 ASSERT_TRUE(content != NULL);
2228 EXPECT_FALSE(content->rejected);
2229
2230 content =
2231 cricket::GetFirstVideoContent(pc_->local_description()->description());
2232 ASSERT_TRUE(content != NULL);
2233 EXPECT_FALSE(content->rejected);
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00002234#ifdef HAVE_SCTP
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002235 content =
2236 cricket::GetFirstDataContent(pc_->local_description()->description());
2237 ASSERT_TRUE(content != NULL);
2238 EXPECT_TRUE(content->rejected);
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00002239#endif
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002240}
2241
zhihuangb19012e2017-09-19 13:47:59 -07002242// Test that fallback from DTLS to SDES is not supported.
2243// The fallback was previously supported but was removed to simplify the code
2244// and because it's non-standard.
2245TEST_F(PeerConnectionInterfaceTest, DtlsSdesFallbackNotSupported) {
deadbeef8662f942017-01-20 21:20:51 -08002246 FakeConstraints constraints;
2247 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2248 true);
2249 CreatePeerConnection(&constraints);
2250 // Wait for fake certificate to be generated. Previously, this is what caused
2251 // the "a=crypto" lines to be rejected.
2252 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
2253 ASSERT_NE(nullptr, fake_certificate_generator_);
2254 EXPECT_EQ_WAIT(1, fake_certificate_generator_->generated_certificates(),
2255 kTimeout);
Steve Antondb45ca82017-09-11 18:27:34 -07002256 std::unique_ptr<SessionDescriptionInterface> desc(
2257 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
2258 kDtlsSdesFallbackSdp, nullptr));
zhihuangb19012e2017-09-19 13:47:59 -07002259 EXPECT_FALSE(DoSetSessionDescription(std::move(desc), false));
deadbeef8662f942017-01-20 21:20:51 -08002260}
2261
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002262// Test that we can create an audio only offer and receive an answer with a
2263// limited set of audio codecs and receive an updated offer with more audio
2264// codecs, where the added codecs are not supported.
2265TEST_F(PeerConnectionInterfaceTest, ReceiveUpdatedAudioOfferWithBadCodecs) {
deadbeef293e9262017-01-11 12:28:30 -08002266 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002267 AddVoiceStream("audio_label");
2268 CreateOfferAsLocalDescription();
2269
Steve Antondb45ca82017-09-11 18:27:34 -07002270 std::unique_ptr<SessionDescriptionInterface> answer(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002271 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
Steve Antondb45ca82017-09-11 18:27:34 -07002272 webrtc::kAudioSdp, nullptr));
2273 EXPECT_TRUE(DoSetSessionDescription(std::move(answer), false));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002274
Steve Antondb45ca82017-09-11 18:27:34 -07002275 std::unique_ptr<SessionDescriptionInterface> updated_offer(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002276 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
jbauchfabe2c92015-07-16 13:43:14 -07002277 webrtc::kAudioSdpWithUnsupportedCodecs,
Steve Antondb45ca82017-09-11 18:27:34 -07002278 nullptr));
2279 EXPECT_TRUE(DoSetSessionDescription(std::move(updated_offer), false));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002280 CreateAnswerAsLocalDescription();
2281}
2282
deadbeefc80741f2015-10-22 13:14:45 -07002283// Test that if we're receiving (but not sending) a track, subsequent offers
2284// will have m-lines with a=recvonly.
2285TEST_F(PeerConnectionInterfaceTest, CreateSubsequentRecvOnlyOffer) {
2286 FakeConstraints constraints;
2287 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2288 true);
2289 CreatePeerConnection(&constraints);
2290 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2291 CreateAnswerAsLocalDescription();
2292
2293 // At this point we should be receiving stream 1, but not sending anything.
2294 // A new offer should be recvonly.
kwibergd1fe2812016-04-27 06:47:29 -07002295 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefc80741f2015-10-22 13:14:45 -07002296 DoCreateOffer(&offer, nullptr);
2297
2298 const cricket::ContentInfo* video_content =
2299 cricket::GetFirstVideoContent(offer->description());
2300 const cricket::VideoContentDescription* video_desc =
2301 static_cast<const cricket::VideoContentDescription*>(
2302 video_content->description);
2303 ASSERT_EQ(cricket::MD_RECVONLY, video_desc->direction());
2304
2305 const cricket::ContentInfo* audio_content =
2306 cricket::GetFirstAudioContent(offer->description());
2307 const cricket::AudioContentDescription* audio_desc =
2308 static_cast<const cricket::AudioContentDescription*>(
2309 audio_content->description);
2310 ASSERT_EQ(cricket::MD_RECVONLY, audio_desc->direction());
2311}
2312
2313// Test that if we're receiving (but not sending) a track, and the
2314// offerToReceiveVideo/offerToReceiveAudio constraints are explicitly set to
2315// false, the generated m-lines will be a=inactive.
2316TEST_F(PeerConnectionInterfaceTest, CreateSubsequentInactiveOffer) {
2317 FakeConstraints constraints;
2318 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2319 true);
2320 CreatePeerConnection(&constraints);
2321 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2322 CreateAnswerAsLocalDescription();
2323
2324 // At this point we should be receiving stream 1, but not sending anything.
2325 // A new offer would be recvonly, but we'll set the "no receive" constraints
2326 // to make it inactive.
kwibergd1fe2812016-04-27 06:47:29 -07002327 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefc80741f2015-10-22 13:14:45 -07002328 FakeConstraints offer_constraints;
2329 offer_constraints.AddMandatory(
2330 webrtc::MediaConstraintsInterface::kOfferToReceiveVideo, false);
2331 offer_constraints.AddMandatory(
2332 webrtc::MediaConstraintsInterface::kOfferToReceiveAudio, false);
2333 DoCreateOffer(&offer, &offer_constraints);
2334
2335 const cricket::ContentInfo* video_content =
2336 cricket::GetFirstVideoContent(offer->description());
2337 const cricket::VideoContentDescription* video_desc =
2338 static_cast<const cricket::VideoContentDescription*>(
2339 video_content->description);
2340 ASSERT_EQ(cricket::MD_INACTIVE, video_desc->direction());
2341
2342 const cricket::ContentInfo* audio_content =
2343 cricket::GetFirstAudioContent(offer->description());
2344 const cricket::AudioContentDescription* audio_desc =
2345 static_cast<const cricket::AudioContentDescription*>(
2346 audio_content->description);
2347 ASSERT_EQ(cricket::MD_INACTIVE, audio_desc->direction());
2348}
2349
deadbeef653b8e02015-11-11 12:55:10 -08002350// Test that we can use SetConfiguration to change the ICE servers of the
2351// PortAllocator.
2352TEST_F(PeerConnectionInterfaceTest, SetConfigurationChangesIceServers) {
2353 CreatePeerConnection();
2354
2355 PeerConnectionInterface::RTCConfiguration config;
2356 PeerConnectionInterface::IceServer server;
2357 server.uri = "stun:test_hostname";
2358 config.servers.push_back(server);
2359 EXPECT_TRUE(pc_->SetConfiguration(config));
2360
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002361 EXPECT_EQ(1u, port_allocator_->stun_servers().size());
2362 EXPECT_EQ("test_hostname",
2363 port_allocator_->stun_servers().begin()->hostname());
deadbeef653b8e02015-11-11 12:55:10 -08002364}
2365
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002366TEST_F(PeerConnectionInterfaceTest, SetConfigurationChangesCandidateFilter) {
2367 CreatePeerConnection();
2368 PeerConnectionInterface::RTCConfiguration config;
2369 config.type = PeerConnectionInterface::kRelay;
2370 EXPECT_TRUE(pc_->SetConfiguration(config));
2371 EXPECT_EQ(cricket::CF_RELAY, port_allocator_->candidate_filter());
2372}
2373
deadbeef293e9262017-01-11 12:28:30 -08002374TEST_F(PeerConnectionInterfaceTest, SetConfigurationChangesPruneTurnPortsFlag) {
2375 PeerConnectionInterface::RTCConfiguration config;
2376 config.prune_turn_ports = false;
2377 CreatePeerConnection(config, nullptr);
2378 EXPECT_FALSE(port_allocator_->prune_turn_ports());
2379
2380 config.prune_turn_ports = true;
2381 EXPECT_TRUE(pc_->SetConfiguration(config));
2382 EXPECT_TRUE(port_allocator_->prune_turn_ports());
2383}
2384
skvladd1f5fda2017-02-03 16:54:05 -08002385// Test that the ice check interval can be changed. This does not verify that
2386// the setting makes it all the way to P2PTransportChannel, as that would
2387// require a very complex set of mocks.
2388TEST_F(PeerConnectionInterfaceTest, SetConfigurationChangesIceCheckInterval) {
2389 PeerConnectionInterface::RTCConfiguration config;
2390 config.ice_check_min_interval = rtc::Optional<int>();
2391 CreatePeerConnection(config, nullptr);
2392 config.ice_check_min_interval = rtc::Optional<int>(100);
2393 EXPECT_TRUE(pc_->SetConfiguration(config));
2394 PeerConnectionInterface::RTCConfiguration new_config =
2395 pc_->GetConfiguration();
2396 EXPECT_EQ(new_config.ice_check_min_interval, rtc::Optional<int>(100));
2397}
2398
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002399// Test that when SetConfiguration changes both the pool size and other
2400// attributes, the pooled session is created with the updated attributes.
2401TEST_F(PeerConnectionInterfaceTest,
2402 SetConfigurationCreatesPooledSessionCorrectly) {
2403 CreatePeerConnection();
2404 PeerConnectionInterface::RTCConfiguration config;
2405 config.ice_candidate_pool_size = 1;
2406 PeerConnectionInterface::IceServer server;
2407 server.uri = kStunAddressOnly;
2408 config.servers.push_back(server);
2409 config.type = PeerConnectionInterface::kRelay;
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07002410 EXPECT_TRUE(pc_->SetConfiguration(config));
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002411
2412 const cricket::FakePortAllocatorSession* session =
2413 static_cast<const cricket::FakePortAllocatorSession*>(
2414 port_allocator_->GetPooledSession());
2415 ASSERT_NE(nullptr, session);
2416 EXPECT_EQ(1UL, session->stun_servers().size());
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002417}
2418
deadbeef293e9262017-01-11 12:28:30 -08002419// Test that after SetLocalDescription, changing the pool size is not allowed,
2420// and an invalid modification error is returned.
deadbeef6de92f92016-12-12 18:49:32 -08002421TEST_F(PeerConnectionInterfaceTest,
2422 CantChangePoolSizeAfterSetLocalDescription) {
2423 CreatePeerConnection();
2424 // Start by setting a size of 1.
2425 PeerConnectionInterface::RTCConfiguration config;
2426 config.ice_candidate_pool_size = 1;
2427 EXPECT_TRUE(pc_->SetConfiguration(config));
2428
2429 // Set remote offer; can still change pool size at this point.
2430 CreateOfferAsRemoteDescription();
2431 config.ice_candidate_pool_size = 2;
2432 EXPECT_TRUE(pc_->SetConfiguration(config));
2433
2434 // Set local answer; now it's too late.
2435 CreateAnswerAsLocalDescription();
2436 config.ice_candidate_pool_size = 3;
deadbeef293e9262017-01-11 12:28:30 -08002437 RTCError error;
2438 EXPECT_FALSE(pc_->SetConfiguration(config, &error));
2439 EXPECT_EQ(RTCErrorType::INVALID_MODIFICATION, error.type());
2440}
2441
deadbeef42a42632017-03-10 15:18:00 -08002442// Test that after setting an answer, extra pooled sessions are discarded. The
2443// ICE candidate pool is only intended to be used for the first offer/answer.
2444TEST_F(PeerConnectionInterfaceTest,
2445 ExtraPooledSessionsDiscardedAfterApplyingAnswer) {
2446 CreatePeerConnection();
2447
2448 // Set a larger-than-necessary size.
2449 PeerConnectionInterface::RTCConfiguration config;
2450 config.ice_candidate_pool_size = 4;
2451 EXPECT_TRUE(pc_->SetConfiguration(config));
2452
2453 // Do offer/answer.
2454 CreateOfferAsRemoteDescription();
2455 CreateAnswerAsLocalDescription();
2456
2457 // Expect no pooled sessions to be left.
2458 const cricket::PortAllocatorSession* session =
2459 port_allocator_->GetPooledSession();
2460 EXPECT_EQ(nullptr, session);
2461}
2462
2463// After Close is called, pooled candidates should be discarded so as to not
2464// waste network resources.
2465TEST_F(PeerConnectionInterfaceTest, PooledSessionsDiscardedAfterClose) {
2466 CreatePeerConnection();
2467
2468 PeerConnectionInterface::RTCConfiguration config;
2469 config.ice_candidate_pool_size = 3;
2470 EXPECT_TRUE(pc_->SetConfiguration(config));
2471 pc_->Close();
2472
2473 // Expect no pooled sessions to be left.
2474 const cricket::PortAllocatorSession* session =
2475 port_allocator_->GetPooledSession();
2476 EXPECT_EQ(nullptr, session);
2477}
2478
deadbeef293e9262017-01-11 12:28:30 -08002479// Test that SetConfiguration returns an invalid modification error if
2480// modifying a field in the configuration that isn't allowed to be modified.
2481TEST_F(PeerConnectionInterfaceTest,
2482 SetConfigurationReturnsInvalidModificationError) {
2483 PeerConnectionInterface::RTCConfiguration config;
2484 config.bundle_policy = PeerConnectionInterface::kBundlePolicyBalanced;
2485 config.rtcp_mux_policy = PeerConnectionInterface::kRtcpMuxPolicyNegotiate;
2486 config.continual_gathering_policy = PeerConnectionInterface::GATHER_ONCE;
2487 CreatePeerConnection(config, nullptr);
2488
2489 PeerConnectionInterface::RTCConfiguration modified_config = config;
2490 modified_config.bundle_policy =
2491 PeerConnectionInterface::kBundlePolicyMaxBundle;
2492 RTCError error;
2493 EXPECT_FALSE(pc_->SetConfiguration(modified_config, &error));
2494 EXPECT_EQ(RTCErrorType::INVALID_MODIFICATION, error.type());
2495
2496 modified_config = config;
2497 modified_config.rtcp_mux_policy =
2498 PeerConnectionInterface::kRtcpMuxPolicyRequire;
2499 error.set_type(RTCErrorType::NONE);
2500 EXPECT_FALSE(pc_->SetConfiguration(modified_config, &error));
2501 EXPECT_EQ(RTCErrorType::INVALID_MODIFICATION, error.type());
2502
2503 modified_config = config;
2504 modified_config.continual_gathering_policy =
2505 PeerConnectionInterface::GATHER_CONTINUALLY;
2506 error.set_type(RTCErrorType::NONE);
2507 EXPECT_FALSE(pc_->SetConfiguration(modified_config, &error));
2508 EXPECT_EQ(RTCErrorType::INVALID_MODIFICATION, error.type());
2509}
2510
2511// Test that SetConfiguration returns a range error if the candidate pool size
2512// is negative or larger than allowed by the spec.
2513TEST_F(PeerConnectionInterfaceTest,
2514 SetConfigurationReturnsRangeErrorForBadCandidatePoolSize) {
2515 PeerConnectionInterface::RTCConfiguration config;
2516 CreatePeerConnection(config, nullptr);
2517
2518 config.ice_candidate_pool_size = -1;
2519 RTCError error;
2520 EXPECT_FALSE(pc_->SetConfiguration(config, &error));
2521 EXPECT_EQ(RTCErrorType::INVALID_RANGE, error.type());
2522
2523 config.ice_candidate_pool_size = INT_MAX;
2524 error.set_type(RTCErrorType::NONE);
2525 EXPECT_FALSE(pc_->SetConfiguration(config, &error));
2526 EXPECT_EQ(RTCErrorType::INVALID_RANGE, error.type());
2527}
2528
2529// Test that SetConfiguration returns a syntax error if parsing an ICE server
2530// URL failed.
2531TEST_F(PeerConnectionInterfaceTest,
2532 SetConfigurationReturnsSyntaxErrorFromBadIceUrls) {
2533 PeerConnectionInterface::RTCConfiguration config;
2534 CreatePeerConnection(config, nullptr);
2535
2536 PeerConnectionInterface::IceServer bad_server;
2537 bad_server.uri = "stunn:www.example.com";
2538 config.servers.push_back(bad_server);
2539 RTCError error;
2540 EXPECT_FALSE(pc_->SetConfiguration(config, &error));
2541 EXPECT_EQ(RTCErrorType::SYNTAX_ERROR, error.type());
2542}
2543
2544// Test that SetConfiguration returns an invalid parameter error if a TURN
2545// IceServer is missing a username or password.
2546TEST_F(PeerConnectionInterfaceTest,
2547 SetConfigurationReturnsInvalidParameterIfCredentialsMissing) {
2548 PeerConnectionInterface::RTCConfiguration config;
2549 CreatePeerConnection(config, nullptr);
2550
2551 PeerConnectionInterface::IceServer bad_server;
2552 bad_server.uri = "turn:www.example.com";
2553 // Missing password.
2554 bad_server.username = "foo";
2555 config.servers.push_back(bad_server);
2556 RTCError error;
2557 EXPECT_FALSE(pc_->SetConfiguration(config, &error));
2558 EXPECT_EQ(RTCErrorType::INVALID_PARAMETER, error.type());
deadbeef6de92f92016-12-12 18:49:32 -08002559}
2560
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002561// Test that PeerConnection::Close changes the states to closed and all remote
2562// tracks change state to ended.
2563TEST_F(PeerConnectionInterfaceTest, CloseAndTestStreamsAndStates) {
2564 // Initialize a PeerConnection and negotiate local and remote session
2565 // description.
2566 InitiateCall();
2567 ASSERT_EQ(1u, pc_->local_streams()->count());
2568 ASSERT_EQ(1u, pc_->remote_streams()->count());
2569
2570 pc_->Close();
2571
2572 EXPECT_EQ(PeerConnectionInterface::kClosed, pc_->signaling_state());
2573 EXPECT_EQ(PeerConnectionInterface::kIceConnectionClosed,
2574 pc_->ice_connection_state());
2575 EXPECT_EQ(PeerConnectionInterface::kIceGatheringComplete,
2576 pc_->ice_gathering_state());
2577
2578 EXPECT_EQ(1u, pc_->local_streams()->count());
2579 EXPECT_EQ(1u, pc_->remote_streams()->count());
2580
zhihuang9763d562016-08-05 11:14:50 -07002581 rtc::scoped_refptr<MediaStreamInterface> remote_stream =
2582 pc_->remote_streams()->at(0);
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002583 // Track state may be updated asynchronously.
perkjd61bf802016-03-24 03:16:19 -07002584 EXPECT_EQ_WAIT(MediaStreamTrackInterface::kEnded,
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002585 remote_stream->GetAudioTracks()[0]->state(), kTimeout);
2586 EXPECT_EQ_WAIT(MediaStreamTrackInterface::kEnded,
2587 remote_stream->GetVideoTracks()[0]->state(), kTimeout);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002588}
2589
2590// Test that PeerConnection methods fails gracefully after
2591// PeerConnection::Close has been called.
2592TEST_F(PeerConnectionInterfaceTest, CloseAndTestMethods) {
deadbeef293e9262017-01-11 12:28:30 -08002593 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002594 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
2595 CreateOfferAsRemoteDescription();
2596 CreateAnswerAsLocalDescription();
2597
2598 ASSERT_EQ(1u, pc_->local_streams()->count());
zhihuang9763d562016-08-05 11:14:50 -07002599 rtc::scoped_refptr<MediaStreamInterface> local_stream =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002600 pc_->local_streams()->at(0);
2601
2602 pc_->Close();
2603
2604 pc_->RemoveStream(local_stream);
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +00002605 EXPECT_FALSE(pc_->AddStream(local_stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002606
2607 ASSERT_FALSE(local_stream->GetAudioTracks().empty());
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002608 rtc::scoped_refptr<webrtc::DtmfSenderInterface> dtmf_sender(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002609 pc_->CreateDtmfSender(local_stream->GetAudioTracks()[0]));
wu@webrtc.org66037362013-08-13 00:09:35 +00002610 EXPECT_TRUE(NULL == dtmf_sender); // local stream has been removed.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002611
2612 EXPECT_TRUE(pc_->CreateDataChannel("test", NULL) == NULL);
2613
2614 EXPECT_TRUE(pc_->local_description() != NULL);
2615 EXPECT_TRUE(pc_->remote_description() != NULL);
2616
kwibergd1fe2812016-04-27 06:47:29 -07002617 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07002618 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
kwibergd1fe2812016-04-27 06:47:29 -07002619 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -07002620 EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002621
2622 std::string sdp;
2623 ASSERT_TRUE(pc_->remote_description()->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07002624 std::unique_ptr<SessionDescriptionInterface> remote_offer(
2625 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer, sdp,
2626 nullptr));
2627 EXPECT_FALSE(DoSetRemoteDescription(std::move(remote_offer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002628
2629 ASSERT_TRUE(pc_->local_description()->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07002630 std::unique_ptr<SessionDescriptionInterface> local_offer(
2631 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer, sdp,
2632 nullptr));
2633 EXPECT_FALSE(DoSetLocalDescription(std::move(local_offer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002634}
2635
2636// Test that GetStats can still be called after PeerConnection::Close.
2637TEST_F(PeerConnectionInterfaceTest, CloseAndGetStats) {
2638 InitiateCall();
2639 pc_->Close();
2640 DoGetStats(NULL);
2641}
deadbeefab9b2d12015-10-14 11:33:11 -07002642
2643// NOTE: The series of tests below come from what used to be
2644// mediastreamsignaling_unittest.cc, and are mostly aimed at testing that
2645// setting a remote or local description has the expected effects.
2646
2647// This test verifies that the remote MediaStreams corresponding to a received
2648// SDP string is created. In this test the two separate MediaStreams are
2649// signaled.
2650TEST_F(PeerConnectionInterfaceTest, UpdateRemoteStreams) {
2651 FakeConstraints constraints;
2652 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2653 true);
2654 CreatePeerConnection(&constraints);
2655 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2656
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002657 rtc::scoped_refptr<StreamCollection> reference(CreateStreamCollection(1, 1));
deadbeefab9b2d12015-10-14 11:33:11 -07002658 EXPECT_TRUE(
2659 CompareStreamCollections(observer_.remote_streams(), reference.get()));
2660 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2661 EXPECT_TRUE(remote_stream->GetVideoTracks()[0]->GetSource() != nullptr);
2662
2663 // Create a session description based on another SDP with another
2664 // MediaStream.
2665 CreateAndSetRemoteOffer(kSdpStringWithStream1And2);
2666
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002667 rtc::scoped_refptr<StreamCollection> reference2(CreateStreamCollection(2, 1));
deadbeefab9b2d12015-10-14 11:33:11 -07002668 EXPECT_TRUE(
2669 CompareStreamCollections(observer_.remote_streams(), reference2.get()));
2670}
2671
2672// This test verifies that when remote tracks are added/removed from SDP, the
2673// created remote streams are updated appropriately.
2674TEST_F(PeerConnectionInterfaceTest,
2675 AddRemoveTrackFromExistingRemoteMediaStream) {
2676 FakeConstraints constraints;
2677 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2678 true);
2679 CreatePeerConnection(&constraints);
kwibergd1fe2812016-04-27 06:47:29 -07002680 std::unique_ptr<SessionDescriptionInterface> desc_ms1 =
kwiberg2bbff992016-03-16 11:03:04 -07002681 CreateSessionDescriptionAndReference(1, 1);
Steve Antondb45ca82017-09-11 18:27:34 -07002682 EXPECT_TRUE(DoSetRemoteDescription(std::move(desc_ms1)));
deadbeefab9b2d12015-10-14 11:33:11 -07002683 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
2684 reference_collection_));
2685
2686 // Add extra audio and video tracks to the same MediaStream.
kwibergd1fe2812016-04-27 06:47:29 -07002687 std::unique_ptr<SessionDescriptionInterface> desc_ms1_two_tracks =
kwiberg2bbff992016-03-16 11:03:04 -07002688 CreateSessionDescriptionAndReference(2, 2);
Steve Antondb45ca82017-09-11 18:27:34 -07002689 EXPECT_TRUE(DoSetRemoteDescription(std::move(desc_ms1_two_tracks)));
deadbeefab9b2d12015-10-14 11:33:11 -07002690 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
2691 reference_collection_));
zhihuang9763d562016-08-05 11:14:50 -07002692 rtc::scoped_refptr<AudioTrackInterface> audio_track2 =
perkjd61bf802016-03-24 03:16:19 -07002693 observer_.remote_streams()->at(0)->GetAudioTracks()[1];
2694 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, audio_track2->state());
zhihuang9763d562016-08-05 11:14:50 -07002695 rtc::scoped_refptr<VideoTrackInterface> video_track2 =
perkjd61bf802016-03-24 03:16:19 -07002696 observer_.remote_streams()->at(0)->GetVideoTracks()[1];
2697 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, video_track2->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002698
2699 // Remove the extra audio and video tracks.
kwibergd1fe2812016-04-27 06:47:29 -07002700 std::unique_ptr<SessionDescriptionInterface> desc_ms2 =
kwiberg2bbff992016-03-16 11:03:04 -07002701 CreateSessionDescriptionAndReference(1, 1);
perkjd61bf802016-03-24 03:16:19 -07002702 MockTrackObserver audio_track_observer(audio_track2);
2703 MockTrackObserver video_track_observer(video_track2);
2704
2705 EXPECT_CALL(audio_track_observer, OnChanged()).Times(Exactly(1));
2706 EXPECT_CALL(video_track_observer, OnChanged()).Times(Exactly(1));
Steve Antondb45ca82017-09-11 18:27:34 -07002707 EXPECT_TRUE(DoSetRemoteDescription(std::move(desc_ms2)));
deadbeefab9b2d12015-10-14 11:33:11 -07002708 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
2709 reference_collection_));
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002710 // Track state may be updated asynchronously.
perkjd61bf802016-03-24 03:16:19 -07002711 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002712 audio_track2->state(), kTimeout);
2713 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
2714 video_track2->state(), kTimeout);
deadbeefab9b2d12015-10-14 11:33:11 -07002715}
2716
2717// This tests that remote tracks are ended if a local session description is set
2718// that rejects the media content type.
2719TEST_F(PeerConnectionInterfaceTest, RejectMediaContent) {
2720 FakeConstraints constraints;
2721 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2722 true);
2723 CreatePeerConnection(&constraints);
2724 // First create and set a remote offer, then reject its video content in our
2725 // answer.
2726 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2727 ASSERT_EQ(1u, observer_.remote_streams()->count());
2728 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2729 ASSERT_EQ(1u, remote_stream->GetVideoTracks().size());
2730 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2731
2732 rtc::scoped_refptr<webrtc::VideoTrackInterface> remote_video =
2733 remote_stream->GetVideoTracks()[0];
2734 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, remote_video->state());
2735 rtc::scoped_refptr<webrtc::AudioTrackInterface> remote_audio =
2736 remote_stream->GetAudioTracks()[0];
2737 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, remote_audio->state());
2738
kwibergd1fe2812016-04-27 06:47:29 -07002739 std::unique_ptr<SessionDescriptionInterface> local_answer;
kwiberg2bbff992016-03-16 11:03:04 -07002740 EXPECT_TRUE(DoCreateAnswer(&local_answer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07002741 cricket::ContentInfo* video_info =
2742 local_answer->description()->GetContentByName("video");
2743 video_info->rejected = true;
Steve Antondb45ca82017-09-11 18:27:34 -07002744 EXPECT_TRUE(DoSetLocalDescription(std::move(local_answer)));
deadbeefab9b2d12015-10-14 11:33:11 -07002745 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kEnded, remote_video->state());
2746 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, remote_audio->state());
2747
2748 // Now create an offer where we reject both video and audio.
kwibergd1fe2812016-04-27 06:47:29 -07002749 std::unique_ptr<SessionDescriptionInterface> local_offer;
kwiberg2bbff992016-03-16 11:03:04 -07002750 EXPECT_TRUE(DoCreateOffer(&local_offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07002751 video_info = local_offer->description()->GetContentByName("video");
2752 ASSERT_TRUE(video_info != nullptr);
2753 video_info->rejected = true;
2754 cricket::ContentInfo* audio_info =
2755 local_offer->description()->GetContentByName("audio");
2756 ASSERT_TRUE(audio_info != nullptr);
2757 audio_info->rejected = true;
Steve Antondb45ca82017-09-11 18:27:34 -07002758 EXPECT_TRUE(DoSetLocalDescription(std::move(local_offer)));
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002759 // Track state may be updated asynchronously.
perkjd61bf802016-03-24 03:16:19 -07002760 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002761 remote_audio->state(), kTimeout);
2762 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
2763 remote_video->state(), kTimeout);
deadbeefab9b2d12015-10-14 11:33:11 -07002764}
2765
2766// This tests that we won't crash if the remote track has been removed outside
2767// of PeerConnection and then PeerConnection tries to reject the track.
2768TEST_F(PeerConnectionInterfaceTest, RemoveTrackThenRejectMediaContent) {
2769 FakeConstraints constraints;
2770 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2771 true);
2772 CreatePeerConnection(&constraints);
2773 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2774 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2775 remote_stream->RemoveTrack(remote_stream->GetVideoTracks()[0]);
2776 remote_stream->RemoveTrack(remote_stream->GetAudioTracks()[0]);
2777
kwibergd1fe2812016-04-27 06:47:29 -07002778 std::unique_ptr<SessionDescriptionInterface> local_answer(
deadbeefab9b2d12015-10-14 11:33:11 -07002779 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
2780 kSdpStringWithStream1, nullptr));
2781 cricket::ContentInfo* video_info =
2782 local_answer->description()->GetContentByName("video");
2783 video_info->rejected = true;
2784 cricket::ContentInfo* audio_info =
2785 local_answer->description()->GetContentByName("audio");
2786 audio_info->rejected = true;
Steve Antondb45ca82017-09-11 18:27:34 -07002787 EXPECT_TRUE(DoSetLocalDescription(std::move(local_answer)));
deadbeefab9b2d12015-10-14 11:33:11 -07002788
2789 // No crash is a pass.
2790}
2791
deadbeef5e97fb52015-10-15 12:49:08 -07002792// This tests that if a recvonly remote description is set, no remote streams
2793// will be created, even if the description contains SSRCs/MSIDs.
2794// See: https://code.google.com/p/webrtc/issues/detail?id=5054
2795TEST_F(PeerConnectionInterfaceTest, RecvonlyDescriptionDoesntCreateStream) {
2796 FakeConstraints constraints;
2797 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2798 true);
2799 CreatePeerConnection(&constraints);
2800
2801 std::string recvonly_offer = kSdpStringWithStream1;
2802 rtc::replace_substrs(kSendrecv, strlen(kSendrecv), kRecvonly,
2803 strlen(kRecvonly), &recvonly_offer);
2804 CreateAndSetRemoteOffer(recvonly_offer);
2805
2806 EXPECT_EQ(0u, observer_.remote_streams()->count());
2807}
2808
deadbeefab9b2d12015-10-14 11:33:11 -07002809// This tests that a default MediaStream is created if a remote session
2810// description doesn't contain any streams and no MSID support.
2811// It also tests that the default stream is updated if a video m-line is added
2812// in a subsequent session description.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002813TEST_F(PeerConnectionInterfaceTest, SdpWithoutMsidCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002814 FakeConstraints constraints;
2815 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2816 true);
2817 CreatePeerConnection(&constraints);
2818 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2819
2820 ASSERT_EQ(1u, observer_.remote_streams()->count());
2821 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2822
2823 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2824 EXPECT_EQ(0u, remote_stream->GetVideoTracks().size());
2825 EXPECT_EQ("default", remote_stream->label());
2826
2827 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2828 ASSERT_EQ(1u, observer_.remote_streams()->count());
2829 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2830 EXPECT_EQ("defaulta0", remote_stream->GetAudioTracks()[0]->id());
deadbeef884f5852016-01-15 09:20:04 -08002831 EXPECT_EQ(MediaStreamTrackInterface::kLive,
2832 remote_stream->GetAudioTracks()[0]->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002833 ASSERT_EQ(1u, remote_stream->GetVideoTracks().size());
2834 EXPECT_EQ("defaultv0", remote_stream->GetVideoTracks()[0]->id());
deadbeef884f5852016-01-15 09:20:04 -08002835 EXPECT_EQ(MediaStreamTrackInterface::kLive,
2836 remote_stream->GetVideoTracks()[0]->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002837}
2838
2839// This tests that a default MediaStream is created if a remote session
2840// description doesn't contain any streams and media direction is send only.
2841TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002842 SendOnlySdpWithoutMsidCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002843 FakeConstraints constraints;
2844 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2845 true);
2846 CreatePeerConnection(&constraints);
2847 CreateAndSetRemoteOffer(kSdpStringSendOnlyWithoutStreams);
2848
2849 ASSERT_EQ(1u, observer_.remote_streams()->count());
2850 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2851
2852 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2853 EXPECT_EQ(1u, remote_stream->GetVideoTracks().size());
2854 EXPECT_EQ("default", remote_stream->label());
2855}
2856
2857// This tests that it won't crash when PeerConnection tries to remove
2858// a remote track that as already been removed from the MediaStream.
2859TEST_F(PeerConnectionInterfaceTest, RemoveAlreadyGoneRemoteStream) {
2860 FakeConstraints constraints;
2861 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2862 true);
2863 CreatePeerConnection(&constraints);
2864 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2865 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2866 remote_stream->RemoveTrack(remote_stream->GetAudioTracks()[0]);
2867 remote_stream->RemoveTrack(remote_stream->GetVideoTracks()[0]);
2868
2869 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2870
2871 // No crash is a pass.
2872}
2873
2874// This tests that a default MediaStream is created if the remote session
2875// description doesn't contain any streams and don't contain an indication if
2876// MSID is supported.
2877TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002878 SdpWithoutMsidAndStreamsCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002879 FakeConstraints constraints;
2880 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2881 true);
2882 CreatePeerConnection(&constraints);
2883 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2884
2885 ASSERT_EQ(1u, observer_.remote_streams()->count());
2886 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2887 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2888 EXPECT_EQ(1u, remote_stream->GetVideoTracks().size());
2889}
2890
2891// This tests that a default MediaStream is not created if the remote session
2892// description doesn't contain any streams but does support MSID.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002893TEST_F(PeerConnectionInterfaceTest, SdpWithMsidDontCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002894 FakeConstraints constraints;
2895 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2896 true);
2897 CreatePeerConnection(&constraints);
2898 CreateAndSetRemoteOffer(kSdpStringWithMsidWithoutStreams);
2899 EXPECT_EQ(0u, observer_.remote_streams()->count());
2900}
2901
deadbeefbda7e0b2015-12-08 17:13:40 -08002902// This tests that when setting a new description, the old default tracks are
2903// not destroyed and recreated.
2904// See: https://bugs.chromium.org/p/webrtc/issues/detail?id=5250
Stefan Holmer102362b2016-03-18 09:39:07 +01002905TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002906 DefaultTracksNotDestroyedAndRecreated) {
deadbeefbda7e0b2015-12-08 17:13:40 -08002907 FakeConstraints constraints;
2908 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2909 true);
2910 CreatePeerConnection(&constraints);
2911 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2912
2913 ASSERT_EQ(1u, observer_.remote_streams()->count());
2914 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2915 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2916
2917 // Set the track to "disabled", then set a new description and ensure the
2918 // track is still disabled, which ensures it hasn't been recreated.
2919 remote_stream->GetAudioTracks()[0]->set_enabled(false);
2920 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2921 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2922 EXPECT_FALSE(remote_stream->GetAudioTracks()[0]->enabled());
2923}
2924
deadbeefab9b2d12015-10-14 11:33:11 -07002925// This tests that a default MediaStream is not created if a remote session
2926// description is updated to not have any MediaStreams.
2927TEST_F(PeerConnectionInterfaceTest, VerifyDefaultStreamIsNotCreated) {
2928 FakeConstraints constraints;
2929 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2930 true);
2931 CreatePeerConnection(&constraints);
2932 CreateAndSetRemoteOffer(kSdpStringWithStream1);
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002933 rtc::scoped_refptr<StreamCollection> reference(CreateStreamCollection(1, 1));
deadbeefab9b2d12015-10-14 11:33:11 -07002934 EXPECT_TRUE(
2935 CompareStreamCollections(observer_.remote_streams(), reference.get()));
2936
2937 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2938 EXPECT_EQ(0u, observer_.remote_streams()->count());
2939}
2940
2941// This tests that an RtpSender is created when the local description is set
2942// after adding a local stream.
2943// TODO(deadbeef): This test and the one below it need to be updated when
2944// an RtpSender's lifetime isn't determined by when a local description is set.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002945TEST_F(PeerConnectionInterfaceTest, LocalDescriptionChanged) {
deadbeefab9b2d12015-10-14 11:33:11 -07002946 FakeConstraints constraints;
2947 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2948 true);
2949 CreatePeerConnection(&constraints);
deadbeefab9b2d12015-10-14 11:33:11 -07002950
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002951 // Create an offer with 1 stream with 2 tracks of each type.
2952 rtc::scoped_refptr<StreamCollection> stream_collection =
2953 CreateStreamCollection(1, 2);
2954 pc_->AddStream(stream_collection->at(0));
2955 std::unique_ptr<SessionDescriptionInterface> offer;
2956 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07002957 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07002958
deadbeefab9b2d12015-10-14 11:33:11 -07002959 auto senders = pc_->GetSenders();
2960 EXPECT_EQ(4u, senders.size());
2961 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2962 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2963 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[1]));
2964 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[1]));
2965
2966 // Remove an audio and video track.
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002967 pc_->RemoveStream(stream_collection->at(0));
2968 stream_collection = CreateStreamCollection(1, 1);
2969 pc_->AddStream(stream_collection->at(0));
2970 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07002971 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002972
deadbeefab9b2d12015-10-14 11:33:11 -07002973 senders = pc_->GetSenders();
2974 EXPECT_EQ(2u, senders.size());
2975 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2976 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2977 EXPECT_FALSE(ContainsSender(senders, kAudioTracks[1]));
2978 EXPECT_FALSE(ContainsSender(senders, kVideoTracks[1]));
2979}
2980
2981// This tests that an RtpSender is created when the local description is set
2982// before adding a local stream.
2983TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002984 AddLocalStreamAfterLocalDescriptionChanged) {
deadbeefab9b2d12015-10-14 11:33:11 -07002985 FakeConstraints constraints;
2986 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2987 true);
2988 CreatePeerConnection(&constraints);
deadbeefab9b2d12015-10-14 11:33:11 -07002989
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002990 rtc::scoped_refptr<StreamCollection> stream_collection =
2991 CreateStreamCollection(1, 2);
2992 // Add a stream to create the offer, but remove it afterwards.
2993 pc_->AddStream(stream_collection->at(0));
2994 std::unique_ptr<SessionDescriptionInterface> offer;
2995 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2996 pc_->RemoveStream(stream_collection->at(0));
deadbeefab9b2d12015-10-14 11:33:11 -07002997
Steve Antondb45ca82017-09-11 18:27:34 -07002998 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07002999 auto senders = pc_->GetSenders();
3000 EXPECT_EQ(0u, senders.size());
3001
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003002 pc_->AddStream(stream_collection->at(0));
deadbeefab9b2d12015-10-14 11:33:11 -07003003 senders = pc_->GetSenders();
3004 EXPECT_EQ(4u, senders.size());
3005 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
3006 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
3007 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[1]));
3008 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[1]));
3009}
3010
3011// This tests that the expected behavior occurs if the SSRC on a local track is
3012// changed when SetLocalDescription is called.
3013TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07003014 ChangeSsrcOnTrackInLocalSessionDescription) {
deadbeefab9b2d12015-10-14 11:33:11 -07003015 FakeConstraints constraints;
3016 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
3017 true);
3018 CreatePeerConnection(&constraints);
deadbeefab9b2d12015-10-14 11:33:11 -07003019
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003020 rtc::scoped_refptr<StreamCollection> stream_collection =
3021 CreateStreamCollection(2, 1);
3022 pc_->AddStream(stream_collection->at(0));
3023 std::unique_ptr<SessionDescriptionInterface> offer;
3024 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
3025 // Grab a copy of the offer before it gets passed into the PC.
Steve Antondb45ca82017-09-11 18:27:34 -07003026 std::unique_ptr<webrtc::JsepSessionDescription> modified_offer(
3027 new webrtc::JsepSessionDescription(SessionDescriptionInterface::kOffer));
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003028 modified_offer->Initialize(offer->description()->Copy(), offer->session_id(),
3029 offer->session_version());
Steve Antondb45ca82017-09-11 18:27:34 -07003030 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07003031
deadbeefab9b2d12015-10-14 11:33:11 -07003032 auto senders = pc_->GetSenders();
3033 EXPECT_EQ(2u, senders.size());
3034 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
3035 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
3036
3037 // Change the ssrc of the audio and video track.
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003038 cricket::MediaContentDescription* desc =
3039 cricket::GetFirstAudioContentDescription(modified_offer->description());
3040 ASSERT_TRUE(desc != NULL);
3041 for (StreamParams& stream : desc->mutable_streams()) {
3042 for (unsigned int& ssrc : stream.ssrcs) {
3043 ++ssrc;
3044 }
3045 }
deadbeefab9b2d12015-10-14 11:33:11 -07003046
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003047 desc =
3048 cricket::GetFirstVideoContentDescription(modified_offer->description());
3049 ASSERT_TRUE(desc != NULL);
3050 for (StreamParams& stream : desc->mutable_streams()) {
3051 for (unsigned int& ssrc : stream.ssrcs) {
3052 ++ssrc;
3053 }
3054 }
3055
Steve Antondb45ca82017-09-11 18:27:34 -07003056 EXPECT_TRUE(DoSetLocalDescription(std::move(modified_offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07003057 senders = pc_->GetSenders();
3058 EXPECT_EQ(2u, senders.size());
3059 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
3060 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
3061 // TODO(deadbeef): Once RtpSenders expose parameters, check that the SSRC
3062 // changed.
3063}
3064
3065// This tests that the expected behavior occurs if a new session description is
3066// set with the same tracks, but on a different MediaStream.
Stefan Holmer55d6e7c2016-03-17 16:26:40 +01003067TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07003068 SignalSameTracksInSeparateMediaStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07003069 FakeConstraints constraints;
3070 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
3071 true);
3072 CreatePeerConnection(&constraints);
deadbeefab9b2d12015-10-14 11:33:11 -07003073
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003074 rtc::scoped_refptr<StreamCollection> stream_collection =
3075 CreateStreamCollection(2, 1);
3076 pc_->AddStream(stream_collection->at(0));
3077 std::unique_ptr<SessionDescriptionInterface> offer;
3078 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07003079 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07003080
deadbeefab9b2d12015-10-14 11:33:11 -07003081 auto senders = pc_->GetSenders();
3082 EXPECT_EQ(2u, senders.size());
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003083 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0], kStreams[0]));
3084 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0], kStreams[0]));
deadbeefab9b2d12015-10-14 11:33:11 -07003085
3086 // Add a new MediaStream but with the same tracks as in the first stream.
3087 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream_1(
3088 webrtc::MediaStream::Create(kStreams[1]));
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003089 stream_1->AddTrack(stream_collection->at(0)->GetVideoTracks()[0]);
3090 stream_1->AddTrack(stream_collection->at(0)->GetAudioTracks()[0]);
deadbeefab9b2d12015-10-14 11:33:11 -07003091 pc_->AddStream(stream_1);
3092
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003093 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07003094 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07003095
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003096 auto new_senders = pc_->GetSenders();
3097 // Should be the same senders as before, but with updated stream id.
3098 // Note that this behavior is subject to change in the future.
3099 // We may decide the PC should ignore existing tracks in AddStream.
3100 EXPECT_EQ(senders, new_senders);
3101 EXPECT_TRUE(ContainsSender(new_senders, kAudioTracks[0], kStreams[1]));
3102 EXPECT_TRUE(ContainsSender(new_senders, kVideoTracks[0], kStreams[1]));
deadbeefab9b2d12015-10-14 11:33:11 -07003103}
3104
zhihuang81c3a032016-11-17 12:06:24 -08003105// This tests that PeerConnectionObserver::OnAddTrack is correctly called.
3106TEST_F(PeerConnectionInterfaceTest, OnAddTrackCallback) {
3107 FakeConstraints constraints;
3108 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
3109 true);
3110 CreatePeerConnection(&constraints);
3111 CreateAndSetRemoteOffer(kSdpStringWithStream1AudioTrackOnly);
3112 EXPECT_EQ(observer_.num_added_tracks_, 1);
3113 EXPECT_EQ(observer_.last_added_track_label_, kAudioTracks[0]);
3114
3115 // Create and set the updated remote SDP.
3116 CreateAndSetRemoteOffer(kSdpStringWithStream1);
3117 EXPECT_EQ(observer_.num_added_tracks_, 2);
3118 EXPECT_EQ(observer_.last_added_track_label_, kVideoTracks[0]);
3119}
3120
deadbeefd1a38b52016-12-10 13:15:33 -08003121// Test that when SetConfiguration is called and the configuration is
3122// changing, the next offer causes an ICE restart.
3123TEST_F(PeerConnectionInterfaceTest, SetConfigurationCausingIceRetart) {
3124 PeerConnectionInterface::RTCConfiguration config;
3125 config.type = PeerConnectionInterface::kRelay;
3126 // Need to pass default constraints to prevent disabling of DTLS...
3127 FakeConstraints default_constraints;
3128 CreatePeerConnection(config, &default_constraints);
3129 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
3130
3131 // Do initial offer/answer so there's something to restart.
3132 CreateOfferAsLocalDescription();
3133 CreateAnswerAsRemoteDescription(kSdpStringWithStream1);
3134
3135 // Grab the ufrags.
3136 std::vector<std::string> initial_ufrags = GetUfrags(pc_->local_description());
3137
3138 // Change ICE policy, which should trigger an ICE restart on the next offer.
3139 config.type = PeerConnectionInterface::kAll;
3140 EXPECT_TRUE(pc_->SetConfiguration(config));
3141 CreateOfferAsLocalDescription();
3142
3143 // Grab the new ufrags.
3144 std::vector<std::string> subsequent_ufrags =
3145 GetUfrags(pc_->local_description());
3146
3147 // Sanity check.
3148 EXPECT_EQ(initial_ufrags.size(), subsequent_ufrags.size());
3149 // Check that each ufrag is different.
3150 for (int i = 0; i < static_cast<int>(initial_ufrags.size()); ++i) {
3151 EXPECT_NE(initial_ufrags[i], subsequent_ufrags[i]);
3152 }
3153}
3154
3155// Test that when SetConfiguration is called and the configuration *isn't*
3156// changing, the next offer does *not* cause an ICE restart.
3157TEST_F(PeerConnectionInterfaceTest, SetConfigurationNotCausingIceRetart) {
3158 PeerConnectionInterface::RTCConfiguration config;
3159 config.type = PeerConnectionInterface::kRelay;
3160 // Need to pass default constraints to prevent disabling of DTLS...
3161 FakeConstraints default_constraints;
3162 CreatePeerConnection(config, &default_constraints);
3163 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
3164
3165 // Do initial offer/answer so there's something to restart.
3166 CreateOfferAsLocalDescription();
3167 CreateAnswerAsRemoteDescription(kSdpStringWithStream1);
3168
3169 // Grab the ufrags.
3170 std::vector<std::string> initial_ufrags = GetUfrags(pc_->local_description());
3171
3172 // Call SetConfiguration with a config identical to what the PC was
3173 // constructed with.
3174 EXPECT_TRUE(pc_->SetConfiguration(config));
3175 CreateOfferAsLocalDescription();
3176
3177 // Grab the new ufrags.
3178 std::vector<std::string> subsequent_ufrags =
3179 GetUfrags(pc_->local_description());
3180
3181 EXPECT_EQ(initial_ufrags, subsequent_ufrags);
3182}
3183
3184// Test for a weird corner case scenario:
3185// 1. Audio/video session established.
3186// 2. SetConfiguration changes ICE config; ICE restart needed.
3187// 3. ICE restart initiated by remote peer, but only for one m= section.
3188// 4. Next createOffer should initiate an ICE restart, but only for the other
3189// m= section; it would be pointless to do an ICE restart for the m= section
3190// that was already restarted.
3191TEST_F(PeerConnectionInterfaceTest, SetConfigurationCausingPartialIceRestart) {
3192 PeerConnectionInterface::RTCConfiguration config;
3193 config.type = PeerConnectionInterface::kRelay;
3194 // Need to pass default constraints to prevent disabling of DTLS...
3195 FakeConstraints default_constraints;
3196 CreatePeerConnection(config, &default_constraints);
3197 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
3198
3199 // Do initial offer/answer so there's something to restart.
3200 CreateOfferAsLocalDescription();
3201 CreateAnswerAsRemoteDescription(kSdpStringWithStream1);
3202
3203 // Change ICE policy, which should set the "needs-ice-restart" flag.
3204 config.type = PeerConnectionInterface::kAll;
3205 EXPECT_TRUE(pc_->SetConfiguration(config));
3206
3207 // Do ICE restart for the first m= section, initiated by remote peer.
Steve Antondb45ca82017-09-11 18:27:34 -07003208 std::unique_ptr<webrtc::SessionDescriptionInterface> remote_offer(
3209 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
3210 kSdpStringWithStream1, nullptr));
3211 ASSERT_TRUE(remote_offer);
deadbeefd1a38b52016-12-10 13:15:33 -08003212 remote_offer->description()->transport_infos()[0].description.ice_ufrag =
3213 "modified";
Steve Antondb45ca82017-09-11 18:27:34 -07003214 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_offer)));
deadbeefd1a38b52016-12-10 13:15:33 -08003215 CreateAnswerAsLocalDescription();
3216
3217 // Grab the ufrags.
3218 std::vector<std::string> initial_ufrags = GetUfrags(pc_->local_description());
3219 ASSERT_EQ(2, initial_ufrags.size());
3220
3221 // Create offer and grab the new ufrags.
3222 CreateOfferAsLocalDescription();
3223 std::vector<std::string> subsequent_ufrags =
3224 GetUfrags(pc_->local_description());
3225 ASSERT_EQ(2, subsequent_ufrags.size());
3226
3227 // Ensure that only the ufrag for the second m= section changed.
3228 EXPECT_EQ(initial_ufrags[0], subsequent_ufrags[0]);
3229 EXPECT_NE(initial_ufrags[1], subsequent_ufrags[1]);
3230}
3231
deadbeeffe4a8a42016-12-20 17:56:17 -08003232// Tests that the methods to return current/pending descriptions work as
3233// expected at different points in the offer/answer exchange. This test does
3234// one offer/answer exchange as the offerer, then another as the answerer.
3235TEST_F(PeerConnectionInterfaceTest, CurrentAndPendingDescriptions) {
3236 // This disables DTLS so we can apply an answer to ourselves.
3237 CreatePeerConnection();
3238
3239 // Create initial local offer and get SDP (which will also be used as
3240 // answer/pranswer);
Steve Antondb45ca82017-09-11 18:27:34 -07003241 std::unique_ptr<SessionDescriptionInterface> local_offer;
3242 ASSERT_TRUE(DoCreateOffer(&local_offer, nullptr));
deadbeeffe4a8a42016-12-20 17:56:17 -08003243 std::string sdp;
Steve Antondb45ca82017-09-11 18:27:34 -07003244 EXPECT_TRUE(local_offer->ToString(&sdp));
deadbeeffe4a8a42016-12-20 17:56:17 -08003245
3246 // Set local offer.
Steve Antondb45ca82017-09-11 18:27:34 -07003247 SessionDescriptionInterface* local_offer_ptr = local_offer.get();
3248 EXPECT_TRUE(DoSetLocalDescription(std::move(local_offer)));
3249 EXPECT_EQ(local_offer_ptr, pc_->pending_local_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003250 EXPECT_EQ(nullptr, pc_->pending_remote_description());
3251 EXPECT_EQ(nullptr, pc_->current_local_description());
3252 EXPECT_EQ(nullptr, pc_->current_remote_description());
3253
3254 // Set remote pranswer.
Steve Antondb45ca82017-09-11 18:27:34 -07003255 std::unique_ptr<SessionDescriptionInterface> remote_pranswer(
deadbeeffe4a8a42016-12-20 17:56:17 -08003256 webrtc::CreateSessionDescription(SessionDescriptionInterface::kPrAnswer,
Steve Antondb45ca82017-09-11 18:27:34 -07003257 sdp, nullptr));
3258 SessionDescriptionInterface* remote_pranswer_ptr = remote_pranswer.get();
3259 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_pranswer)));
3260 EXPECT_EQ(local_offer_ptr, pc_->pending_local_description());
3261 EXPECT_EQ(remote_pranswer_ptr, pc_->pending_remote_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003262 EXPECT_EQ(nullptr, pc_->current_local_description());
3263 EXPECT_EQ(nullptr, pc_->current_remote_description());
3264
3265 // Set remote answer.
Steve Antondb45ca82017-09-11 18:27:34 -07003266 std::unique_ptr<SessionDescriptionInterface> remote_answer(
3267 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
3268 sdp, nullptr));
3269 SessionDescriptionInterface* remote_answer_ptr = remote_answer.get();
3270 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_answer)));
deadbeeffe4a8a42016-12-20 17:56:17 -08003271 EXPECT_EQ(nullptr, pc_->pending_local_description());
3272 EXPECT_EQ(nullptr, pc_->pending_remote_description());
Steve Antondb45ca82017-09-11 18:27:34 -07003273 EXPECT_EQ(local_offer_ptr, pc_->current_local_description());
3274 EXPECT_EQ(remote_answer_ptr, pc_->current_remote_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003275
3276 // Set remote offer.
Steve Antondb45ca82017-09-11 18:27:34 -07003277 std::unique_ptr<SessionDescriptionInterface> remote_offer(
3278 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer, sdp,
3279 nullptr));
3280 SessionDescriptionInterface* remote_offer_ptr = remote_offer.get();
3281 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_offer)));
3282 EXPECT_EQ(remote_offer_ptr, pc_->pending_remote_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003283 EXPECT_EQ(nullptr, pc_->pending_local_description());
Steve Antondb45ca82017-09-11 18:27:34 -07003284 EXPECT_EQ(local_offer_ptr, pc_->current_local_description());
3285 EXPECT_EQ(remote_answer_ptr, pc_->current_remote_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003286
3287 // Set local pranswer.
Steve Antondb45ca82017-09-11 18:27:34 -07003288 std::unique_ptr<SessionDescriptionInterface> local_pranswer(
deadbeeffe4a8a42016-12-20 17:56:17 -08003289 webrtc::CreateSessionDescription(SessionDescriptionInterface::kPrAnswer,
Steve Antondb45ca82017-09-11 18:27:34 -07003290 sdp, nullptr));
3291 SessionDescriptionInterface* local_pranswer_ptr = local_pranswer.get();
3292 EXPECT_TRUE(DoSetLocalDescription(std::move(local_pranswer)));
3293 EXPECT_EQ(remote_offer_ptr, pc_->pending_remote_description());
3294 EXPECT_EQ(local_pranswer_ptr, pc_->pending_local_description());
3295 EXPECT_EQ(local_offer_ptr, pc_->current_local_description());
3296 EXPECT_EQ(remote_answer_ptr, pc_->current_remote_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003297
3298 // Set local answer.
Steve Antondb45ca82017-09-11 18:27:34 -07003299 std::unique_ptr<SessionDescriptionInterface> local_answer(
3300 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
3301 sdp, nullptr));
3302 SessionDescriptionInterface* local_answer_ptr = local_answer.get();
3303 EXPECT_TRUE(DoSetLocalDescription(std::move(local_answer)));
deadbeeffe4a8a42016-12-20 17:56:17 -08003304 EXPECT_EQ(nullptr, pc_->pending_remote_description());
3305 EXPECT_EQ(nullptr, pc_->pending_local_description());
Steve Antondb45ca82017-09-11 18:27:34 -07003306 EXPECT_EQ(remote_offer_ptr, pc_->current_remote_description());
3307 EXPECT_EQ(local_answer_ptr, pc_->current_local_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003308}
3309
zhihuang77985012017-02-07 15:45:16 -08003310// Tests that it won't crash when calling StartRtcEventLog or StopRtcEventLog
3311// after the PeerConnection is closed.
3312TEST_F(PeerConnectionInterfaceTest,
3313 StartAndStopLoggingAfterPeerConnectionClosed) {
3314 CreatePeerConnection();
3315 // The RtcEventLog will be reset when the PeerConnection is closed.
3316 pc_->Close();
3317
3318 rtc::PlatformFile file = 0;
3319 int64_t max_size_bytes = 1024;
3320 EXPECT_FALSE(pc_->StartRtcEventLog(file, max_size_bytes));
3321 pc_->StopRtcEventLog();
3322}
3323
deadbeef30952b42017-04-21 02:41:29 -07003324// Test that generated offers/answers include "ice-option:trickle".
3325TEST_F(PeerConnectionInterfaceTest, OffersAndAnswersHaveTrickleIceOption) {
3326 CreatePeerConnection();
3327
3328 // First, create an offer with audio/video.
3329 FakeConstraints constraints;
3330 constraints.SetMandatoryReceiveAudio(true);
3331 constraints.SetMandatoryReceiveVideo(true);
3332 std::unique_ptr<SessionDescriptionInterface> offer;
3333 ASSERT_TRUE(DoCreateOffer(&offer, &constraints));
3334 cricket::SessionDescription* desc = offer->description();
3335 ASSERT_EQ(2u, desc->transport_infos().size());
3336 EXPECT_TRUE(desc->transport_infos()[0].description.HasOption("trickle"));
3337 EXPECT_TRUE(desc->transport_infos()[1].description.HasOption("trickle"));
3338
3339 // Apply the offer as a remote description, then create an answer.
Steve Antondb45ca82017-09-11 18:27:34 -07003340 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
deadbeef30952b42017-04-21 02:41:29 -07003341 std::unique_ptr<SessionDescriptionInterface> answer;
3342 ASSERT_TRUE(DoCreateAnswer(&answer, &constraints));
3343 desc = answer->description();
3344 ASSERT_EQ(2u, desc->transport_infos().size());
3345 EXPECT_TRUE(desc->transport_infos()[0].description.HasOption("trickle"));
3346 EXPECT_TRUE(desc->transport_infos()[1].description.HasOption("trickle"));
3347}
3348
deadbeef1dcb1642017-03-29 21:08:16 -07003349// Test that ICE renomination isn't offered if it's not enabled in the PC's
3350// RTCConfiguration.
3351TEST_F(PeerConnectionInterfaceTest, IceRenominationNotOffered) {
3352 PeerConnectionInterface::RTCConfiguration config;
3353 config.enable_ice_renomination = false;
3354 CreatePeerConnection(config, nullptr);
3355 AddVoiceStream("foo");
3356
3357 std::unique_ptr<SessionDescriptionInterface> offer;
3358 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
3359 cricket::SessionDescription* desc = offer->description();
3360 EXPECT_EQ(1u, desc->transport_infos().size());
3361 EXPECT_FALSE(
3362 desc->transport_infos()[0].description.GetIceParameters().renomination);
3363}
3364
3365// Test that the ICE renomination option is present in generated offers/answers
3366// if it's enabled in the PC's RTCConfiguration.
3367TEST_F(PeerConnectionInterfaceTest, IceRenominationOptionInOfferAndAnswer) {
3368 PeerConnectionInterface::RTCConfiguration config;
3369 config.enable_ice_renomination = true;
3370 CreatePeerConnection(config, nullptr);
3371 AddVoiceStream("foo");
3372
3373 std::unique_ptr<SessionDescriptionInterface> offer;
3374 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
3375 cricket::SessionDescription* desc = offer->description();
3376 EXPECT_EQ(1u, desc->transport_infos().size());
3377 EXPECT_TRUE(
3378 desc->transport_infos()[0].description.GetIceParameters().renomination);
3379
3380 // Set the offer as a remote description, then create an answer and ensure it
3381 // has the renomination flag too.
Steve Antondb45ca82017-09-11 18:27:34 -07003382 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
deadbeef1dcb1642017-03-29 21:08:16 -07003383 std::unique_ptr<SessionDescriptionInterface> answer;
3384 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
3385 desc = answer->description();
3386 EXPECT_EQ(1u, desc->transport_infos().size());
3387 EXPECT_TRUE(
3388 desc->transport_infos()[0].description.GetIceParameters().renomination);
3389}
3390
3391// Test that if CreateOffer is called with the deprecated "offer to receive
3392// audio/video" constraints, they're processed and result in an offer with
3393// audio/video sections just as if RTCOfferAnswerOptions had been used.
3394TEST_F(PeerConnectionInterfaceTest, CreateOfferWithOfferToReceiveConstraints) {
3395 CreatePeerConnection();
3396
3397 FakeConstraints constraints;
3398 constraints.SetMandatoryReceiveAudio(true);
3399 constraints.SetMandatoryReceiveVideo(true);
3400 std::unique_ptr<SessionDescriptionInterface> offer;
3401 ASSERT_TRUE(DoCreateOffer(&offer, &constraints));
3402
3403 cricket::SessionDescription* desc = offer->description();
3404 const cricket::ContentInfo* audio = cricket::GetFirstAudioContent(desc);
3405 const cricket::ContentInfo* video = cricket::GetFirstVideoContent(desc);
3406 ASSERT_NE(nullptr, audio);
3407 ASSERT_NE(nullptr, video);
3408 EXPECT_FALSE(audio->rejected);
3409 EXPECT_FALSE(video->rejected);
3410}
3411
3412// Test that if CreateAnswer is called with the deprecated "offer to receive
3413// audio/video" constraints, they're processed and can be used to reject an
3414// offered m= section just as can be done with RTCOfferAnswerOptions;
3415TEST_F(PeerConnectionInterfaceTest, CreateAnswerWithOfferToReceiveConstraints) {
3416 CreatePeerConnection();
3417
3418 // First, create an offer with audio/video and apply it as a remote
3419 // description.
3420 FakeConstraints constraints;
3421 constraints.SetMandatoryReceiveAudio(true);
3422 constraints.SetMandatoryReceiveVideo(true);
3423 std::unique_ptr<SessionDescriptionInterface> offer;
3424 ASSERT_TRUE(DoCreateOffer(&offer, &constraints));
Steve Antondb45ca82017-09-11 18:27:34 -07003425 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
deadbeef1dcb1642017-03-29 21:08:16 -07003426
3427 // Now create answer that rejects audio/video.
3428 constraints.SetMandatoryReceiveAudio(false);
3429 constraints.SetMandatoryReceiveVideo(false);
3430 std::unique_ptr<SessionDescriptionInterface> answer;
3431 ASSERT_TRUE(DoCreateAnswer(&answer, &constraints));
3432
3433 cricket::SessionDescription* desc = answer->description();
3434 const cricket::ContentInfo* audio = cricket::GetFirstAudioContent(desc);
3435 const cricket::ContentInfo* video = cricket::GetFirstVideoContent(desc);
3436 ASSERT_NE(nullptr, audio);
3437 ASSERT_NE(nullptr, video);
3438 EXPECT_TRUE(audio->rejected);
3439 EXPECT_TRUE(video->rejected);
3440}
3441
3442#ifdef HAVE_SCTP
3443#define MAYBE_DataChannelOnlyOfferWithMaxBundlePolicy \
3444 DataChannelOnlyOfferWithMaxBundlePolicy
3445#else
3446#define MAYBE_DataChannelOnlyOfferWithMaxBundlePolicy \
3447 DISABLED_DataChannelOnlyOfferWithMaxBundlePolicy
3448#endif
3449
3450// Test that negotiation can succeed with a data channel only, and with the max
3451// bundle policy. Previously there was a bug that prevented this.
3452TEST_F(PeerConnectionInterfaceTest,
3453 MAYBE_DataChannelOnlyOfferWithMaxBundlePolicy) {
3454 PeerConnectionInterface::RTCConfiguration config;
3455 config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
3456 CreatePeerConnection(config, nullptr);
3457
3458 // First, create an offer with only a data channel and apply it as a remote
3459 // description.
3460 pc_->CreateDataChannel("test", nullptr);
3461 std::unique_ptr<SessionDescriptionInterface> offer;
3462 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07003463 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
deadbeef1dcb1642017-03-29 21:08:16 -07003464
3465 // Create and set answer as well.
3466 std::unique_ptr<SessionDescriptionInterface> answer;
3467 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07003468 EXPECT_TRUE(DoSetLocalDescription(std::move(answer)));
deadbeef1dcb1642017-03-29 21:08:16 -07003469}
3470
zstein4b979802017-06-02 14:37:37 -07003471TEST_F(PeerConnectionInterfaceTest, SetBitrateWithoutMinSucceeds) {
3472 CreatePeerConnection();
3473 PeerConnectionInterface::BitrateParameters bitrate;
3474 bitrate.current_bitrate_bps = rtc::Optional<int>(100000);
3475 EXPECT_TRUE(pc_->SetBitrate(bitrate).ok());
3476}
3477
3478TEST_F(PeerConnectionInterfaceTest, SetBitrateNegativeMinFails) {
3479 CreatePeerConnection();
3480 PeerConnectionInterface::BitrateParameters bitrate;
3481 bitrate.min_bitrate_bps = rtc::Optional<int>(-1);
3482 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3483}
3484
3485TEST_F(PeerConnectionInterfaceTest, SetBitrateCurrentLessThanMinFails) {
3486 CreatePeerConnection();
3487 PeerConnectionInterface::BitrateParameters bitrate;
3488 bitrate.min_bitrate_bps = rtc::Optional<int>(5);
3489 bitrate.current_bitrate_bps = rtc::Optional<int>(3);
3490 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3491}
3492
3493TEST_F(PeerConnectionInterfaceTest, SetBitrateCurrentNegativeFails) {
3494 CreatePeerConnection();
3495 PeerConnectionInterface::BitrateParameters bitrate;
3496 bitrate.current_bitrate_bps = rtc::Optional<int>(-1);
3497 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3498}
3499
3500TEST_F(PeerConnectionInterfaceTest, SetBitrateMaxLessThanCurrentFails) {
3501 CreatePeerConnection();
3502 PeerConnectionInterface::BitrateParameters bitrate;
3503 bitrate.current_bitrate_bps = rtc::Optional<int>(10);
3504 bitrate.max_bitrate_bps = rtc::Optional<int>(8);
3505 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3506}
3507
3508TEST_F(PeerConnectionInterfaceTest, SetBitrateMaxLessThanMinFails) {
3509 CreatePeerConnection();
3510 PeerConnectionInterface::BitrateParameters bitrate;
3511 bitrate.min_bitrate_bps = rtc::Optional<int>(10);
3512 bitrate.max_bitrate_bps = rtc::Optional<int>(8);
3513 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3514}
3515
3516TEST_F(PeerConnectionInterfaceTest, SetBitrateMaxNegativeFails) {
3517 CreatePeerConnection();
3518 PeerConnectionInterface::BitrateParameters bitrate;
3519 bitrate.max_bitrate_bps = rtc::Optional<int>(-1);
3520 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3521}
3522
Steve Anton038834f2017-07-14 15:59:59 -07003523// ice_regather_interval_range requires WebRTC to be configured for continual
3524// gathering already.
3525TEST_F(PeerConnectionInterfaceTest,
3526 SetIceRegatherIntervalRangeWithoutContinualGatheringFails) {
3527 PeerConnectionInterface::RTCConfiguration config;
3528 config.ice_regather_interval_range.emplace(1000, 2000);
3529 config.continual_gathering_policy =
3530 PeerConnectionInterface::ContinualGatheringPolicy::GATHER_ONCE;
3531 CreatePeerConnectionExpectFail(config);
3532}
3533
3534// Ensures that there is no error when ice_regather_interval_range is set with
3535// continual gathering enabled.
3536TEST_F(PeerConnectionInterfaceTest,
3537 SetIceRegatherIntervalRangeWithContinualGathering) {
3538 PeerConnectionInterface::RTCConfiguration config;
3539 config.ice_regather_interval_range.emplace(1000, 2000);
3540 config.continual_gathering_policy =
3541 PeerConnectionInterface::ContinualGatheringPolicy::GATHER_CONTINUALLY;
3542 CreatePeerConnection(config, nullptr);
3543}
3544
zstein4b979802017-06-02 14:37:37 -07003545// The current bitrate from Call's BitrateConfigMask is currently clamped by
3546// Call's BitrateConfig, which comes from the SDP or a default value. This test
3547// checks that a call to SetBitrate with a current bitrate that will be clamped
3548// succeeds.
3549TEST_F(PeerConnectionInterfaceTest, SetBitrateCurrentLessThanImplicitMin) {
3550 CreatePeerConnection();
3551 PeerConnectionInterface::BitrateParameters bitrate;
3552 bitrate.current_bitrate_bps = rtc::Optional<int>(1);
3553 EXPECT_TRUE(pc_->SetBitrate(bitrate).ok());
3554}
3555
zhihuang1c378ed2017-08-17 14:10:50 -07003556// The following tests verify that the offer can be created correctly.
3557TEST_F(PeerConnectionInterfaceTest,
3558 CreateOfferFailsWithInvalidOfferToReceiveAudio) {
3559 RTCOfferAnswerOptions rtc_options;
3560
3561 // Setting offer_to_receive_audio to a value lower than kUndefined or greater
3562 // than kMaxOfferToReceiveMedia should be treated as invalid.
3563 rtc_options.offer_to_receive_audio = RTCOfferAnswerOptions::kUndefined - 1;
3564 CreatePeerConnection();
3565 EXPECT_FALSE(CreateOfferWithOptions(rtc_options));
3566
3567 rtc_options.offer_to_receive_audio =
3568 RTCOfferAnswerOptions::kMaxOfferToReceiveMedia + 1;
3569 EXPECT_FALSE(CreateOfferWithOptions(rtc_options));
3570}
3571
3572TEST_F(PeerConnectionInterfaceTest,
3573 CreateOfferFailsWithInvalidOfferToReceiveVideo) {
3574 RTCOfferAnswerOptions rtc_options;
3575
3576 // Setting offer_to_receive_video to a value lower than kUndefined or greater
3577 // than kMaxOfferToReceiveMedia should be treated as invalid.
3578 rtc_options.offer_to_receive_video = RTCOfferAnswerOptions::kUndefined - 1;
3579 CreatePeerConnection();
3580 EXPECT_FALSE(CreateOfferWithOptions(rtc_options));
3581
3582 rtc_options.offer_to_receive_video =
3583 RTCOfferAnswerOptions::kMaxOfferToReceiveMedia + 1;
3584 EXPECT_FALSE(CreateOfferWithOptions(rtc_options));
3585}
3586
3587// Test that the audio and video content will be added to an offer if both
3588// |offer_to_receive_audio| and |offer_to_receive_video| options are 1.
3589TEST_F(PeerConnectionInterfaceTest, CreateOfferWithAudioVideoOptions) {
3590 RTCOfferAnswerOptions rtc_options;
3591 rtc_options.offer_to_receive_audio = 1;
3592 rtc_options.offer_to_receive_video = 1;
3593
3594 std::unique_ptr<SessionDescriptionInterface> offer;
3595 CreatePeerConnection();
3596 offer = CreateOfferWithOptions(rtc_options);
3597 ASSERT_TRUE(offer);
3598 EXPECT_NE(nullptr, GetFirstAudioContent(offer->description()));
3599 EXPECT_NE(nullptr, GetFirstVideoContent(offer->description()));
3600}
3601
3602// Test that only audio content will be added to the offer if only
3603// |offer_to_receive_audio| options is 1.
3604TEST_F(PeerConnectionInterfaceTest, CreateOfferWithAudioOnlyOptions) {
3605 RTCOfferAnswerOptions rtc_options;
3606 rtc_options.offer_to_receive_audio = 1;
3607 rtc_options.offer_to_receive_video = 0;
3608
3609 std::unique_ptr<SessionDescriptionInterface> offer;
3610 CreatePeerConnection();
3611 offer = CreateOfferWithOptions(rtc_options);
3612 ASSERT_TRUE(offer);
3613 EXPECT_NE(nullptr, GetFirstAudioContent(offer->description()));
3614 EXPECT_EQ(nullptr, GetFirstVideoContent(offer->description()));
3615}
3616
3617// Test that only video content will be added if only |offer_to_receive_video|
3618// options is 1.
3619TEST_F(PeerConnectionInterfaceTest, CreateOfferWithVideoOnlyOptions) {
3620 RTCOfferAnswerOptions rtc_options;
3621 rtc_options.offer_to_receive_audio = 0;
3622 rtc_options.offer_to_receive_video = 1;
3623
3624 std::unique_ptr<SessionDescriptionInterface> offer;
3625 CreatePeerConnection();
3626 offer = CreateOfferWithOptions(rtc_options);
3627 ASSERT_TRUE(offer);
3628 EXPECT_EQ(nullptr, GetFirstAudioContent(offer->description()));
3629 EXPECT_NE(nullptr, GetFirstVideoContent(offer->description()));
3630}
3631
3632// Test that if |voice_activity_detection| is false, no CN codec is added to the
3633// offer.
3634TEST_F(PeerConnectionInterfaceTest, CreateOfferWithVADOptions) {
3635 RTCOfferAnswerOptions rtc_options;
3636 rtc_options.offer_to_receive_audio = 1;
3637 rtc_options.offer_to_receive_video = 0;
3638
3639 std::unique_ptr<SessionDescriptionInterface> offer;
3640 CreatePeerConnection();
3641 offer = CreateOfferWithOptions(rtc_options);
3642 ASSERT_TRUE(offer);
3643 const cricket::ContentInfo* audio_content =
3644 offer->description()->GetContentByName(cricket::CN_AUDIO);
3645 ASSERT_TRUE(audio_content);
3646 // |voice_activity_detection| is true by default.
3647 EXPECT_TRUE(HasCNCodecs(audio_content));
3648
3649 rtc_options.voice_activity_detection = false;
3650 CreatePeerConnection();
3651 offer = CreateOfferWithOptions(rtc_options);
3652 ASSERT_TRUE(offer);
3653 audio_content = offer->description()->GetContentByName(cricket::CN_AUDIO);
3654 ASSERT_TRUE(audio_content);
3655 EXPECT_FALSE(HasCNCodecs(audio_content));
3656}
3657
3658// Test that no media content will be added to the offer if using default
3659// RTCOfferAnswerOptions.
3660TEST_F(PeerConnectionInterfaceTest, CreateOfferWithDefaultOfferAnswerOptions) {
3661 RTCOfferAnswerOptions rtc_options;
3662
3663 std::unique_ptr<SessionDescriptionInterface> offer;
3664 CreatePeerConnection();
3665 offer = CreateOfferWithOptions(rtc_options);
3666 ASSERT_TRUE(offer);
3667 EXPECT_EQ(nullptr, GetFirstAudioContent(offer->description()));
3668 EXPECT_EQ(nullptr, GetFirstVideoContent(offer->description()));
3669}
3670
3671// Test that if |ice_restart| is true, the ufrag/pwd will change, otherwise
3672// ufrag/pwd will be the same in the new offer.
3673TEST_F(PeerConnectionInterfaceTest, CreateOfferWithIceRestart) {
3674 RTCOfferAnswerOptions rtc_options;
3675 rtc_options.ice_restart = false;
3676 rtc_options.offer_to_receive_audio = 1;
3677
3678 std::unique_ptr<SessionDescriptionInterface> offer;
3679 CreatePeerConnection();
3680 CreateOfferWithOptionsAsLocalDescription(&offer, rtc_options);
3681 auto ufrag1 = offer->description()
3682 ->GetTransportInfoByName(cricket::CN_AUDIO)
3683 ->description.ice_ufrag;
3684 auto pwd1 = offer->description()
3685 ->GetTransportInfoByName(cricket::CN_AUDIO)
3686 ->description.ice_pwd;
3687
3688 // |ice_restart| is false, the ufrag/pwd shouldn't change.
3689 CreateOfferWithOptionsAsLocalDescription(&offer, rtc_options);
3690 auto ufrag2 = offer->description()
3691 ->GetTransportInfoByName(cricket::CN_AUDIO)
3692 ->description.ice_ufrag;
3693 auto pwd2 = offer->description()
3694 ->GetTransportInfoByName(cricket::CN_AUDIO)
3695 ->description.ice_pwd;
3696
3697 // |ice_restart| is true, the ufrag/pwd should change.
3698 rtc_options.ice_restart = true;
3699 CreateOfferWithOptionsAsLocalDescription(&offer, rtc_options);
3700 auto ufrag3 = offer->description()
3701 ->GetTransportInfoByName(cricket::CN_AUDIO)
3702 ->description.ice_ufrag;
3703 auto pwd3 = offer->description()
3704 ->GetTransportInfoByName(cricket::CN_AUDIO)
3705 ->description.ice_pwd;
3706
3707 EXPECT_EQ(ufrag1, ufrag2);
3708 EXPECT_EQ(pwd1, pwd2);
3709 EXPECT_NE(ufrag2, ufrag3);
3710 EXPECT_NE(pwd2, pwd3);
3711}
3712
3713// Test that if |use_rtp_mux| is true, the bundling will be enabled in the
3714// offer; if it is false, there won't be any bundle group in the offer.
3715TEST_F(PeerConnectionInterfaceTest, CreateOfferWithRtpMux) {
3716 RTCOfferAnswerOptions rtc_options;
3717 rtc_options.offer_to_receive_audio = 1;
3718 rtc_options.offer_to_receive_video = 1;
3719
3720 std::unique_ptr<SessionDescriptionInterface> offer;
3721 CreatePeerConnection();
3722
3723 rtc_options.use_rtp_mux = true;
3724 offer = CreateOfferWithOptions(rtc_options);
3725 ASSERT_TRUE(offer);
3726 EXPECT_NE(nullptr, GetFirstAudioContent(offer->description()));
3727 EXPECT_NE(nullptr, GetFirstVideoContent(offer->description()));
3728 EXPECT_TRUE(offer->description()->HasGroup(cricket::GROUP_TYPE_BUNDLE));
3729
3730 rtc_options.use_rtp_mux = false;
3731 offer = CreateOfferWithOptions(rtc_options);
3732 ASSERT_TRUE(offer);
3733 EXPECT_NE(nullptr, GetFirstAudioContent(offer->description()));
3734 EXPECT_NE(nullptr, GetFirstVideoContent(offer->description()));
3735 EXPECT_FALSE(offer->description()->HasGroup(cricket::GROUP_TYPE_BUNDLE));
3736}
3737
3738// If SetMandatoryReceiveAudio(false) and SetMandatoryReceiveVideo(false) are
3739// called for the answer constraints, but an audio and a video section were
3740// offered, there will still be an audio and a video section in the answer.
3741TEST_F(PeerConnectionInterfaceTest,
3742 RejectAudioAndVideoInAnswerWithConstraints) {
3743 // Offer both audio and video.
3744 RTCOfferAnswerOptions rtc_offer_options;
3745 rtc_offer_options.offer_to_receive_audio = 1;
3746 rtc_offer_options.offer_to_receive_video = 1;
3747
3748 CreatePeerConnection();
3749 std::unique_ptr<SessionDescriptionInterface> offer;
3750 CreateOfferWithOptionsAsRemoteDescription(&offer, rtc_offer_options);
3751 EXPECT_NE(nullptr, GetFirstAudioContent(offer->description()));
3752 EXPECT_NE(nullptr, GetFirstVideoContent(offer->description()));
3753
3754 // Since an offer has been created with both audio and video,
3755 // Answers will contain the media types that exist in the offer regardless of
3756 // the value of |answer_options.has_audio| and |answer_options.has_video|.
3757 FakeConstraints answer_c;
3758 // Reject both audio and video.
3759 answer_c.SetMandatoryReceiveAudio(false);
3760 answer_c.SetMandatoryReceiveVideo(false);
3761
3762 std::unique_ptr<SessionDescriptionInterface> answer;
3763 ASSERT_TRUE(DoCreateAnswer(&answer, &answer_c));
3764 const cricket::ContentInfo* audio_content =
3765 GetFirstAudioContent(answer->description());
3766 const cricket::ContentInfo* video_content =
3767 GetFirstVideoContent(answer->description());
3768 ASSERT_NE(nullptr, audio_content);
3769 ASSERT_NE(nullptr, video_content);
3770 EXPECT_TRUE(audio_content->rejected);
3771 EXPECT_TRUE(video_content->rejected);
3772}
3773
zhihuang141aacb2017-08-29 13:23:53 -07003774// This test ensures OnRenegotiationNeeded is called when we add track with
3775// MediaStream -> AddTrack in the same way it is called when we add track with
3776// PeerConnection -> AddTrack.
3777// The test can be removed once addStream is rewritten in terms of addTrack
3778// https://bugs.chromium.org/p/webrtc/issues/detail?id=7815
3779TEST_F(PeerConnectionInterfaceTest, MediaStreamAddTrackRemoveTrackRenegotiate) {
3780 CreatePeerConnectionWithoutDtls();
3781 rtc::scoped_refptr<MediaStreamInterface> stream(
3782 pc_factory_->CreateLocalMediaStream(kStreamLabel1));
3783 pc_->AddStream(stream);
3784 rtc::scoped_refptr<AudioTrackInterface> audio_track(
3785 pc_factory_->CreateAudioTrack("audio_track", nullptr));
3786 rtc::scoped_refptr<VideoTrackInterface> video_track(
3787 pc_factory_->CreateVideoTrack(
3788 "video_track", pc_factory_->CreateVideoSource(
3789 std::unique_ptr<cricket::VideoCapturer>(
3790 new cricket::FakeVideoCapturer()))));
3791 stream->AddTrack(audio_track);
3792 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
3793 observer_.renegotiation_needed_ = false;
3794
3795 stream->AddTrack(video_track);
3796 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
3797 observer_.renegotiation_needed_ = false;
3798
3799 stream->RemoveTrack(audio_track);
3800 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
3801 observer_.renegotiation_needed_ = false;
3802
3803 stream->RemoveTrack(video_track);
3804 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
3805 observer_.renegotiation_needed_ = false;
3806}
3807
3808// Tests that creating answer would fail gracefully without being crashed if the
3809// remote description is unset.
3810TEST_F(PeerConnectionInterfaceTest, CreateAnswerWithoutRemoteDescription) {
3811 CreatePeerConnection();
3812 // Creating answer fails because the remote description is unset.
3813 std::unique_ptr<SessionDescriptionInterface> answer;
3814 EXPECT_FALSE(DoCreateAnswer(&answer, nullptr));
3815
3816 // Createing answer succeeds when the remote description is set.
3817 CreateOfferAsRemoteDescription();
3818 EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
3819}
3820
Zhi Huang2a5e4262017-09-14 01:15:03 -07003821// Test that an error is returned if a description is applied that doesn't
3822// respect the order of existing media sections.
3823TEST_F(PeerConnectionInterfaceTest,
3824 MediaSectionOrderEnforcedForSubsequentOffers) {
3825 CreatePeerConnection();
3826 FakeConstraints constraints;
3827 constraints.SetMandatoryReceiveAudio(true);
3828 constraints.SetMandatoryReceiveVideo(true);
3829 std::unique_ptr<SessionDescriptionInterface> offer;
3830 ASSERT_TRUE(DoCreateOffer(&offer, &constraints));
3831 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
3832
3833 std::unique_ptr<SessionDescriptionInterface> answer;
3834 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
3835 EXPECT_TRUE(DoSetLocalDescription(std::move(answer)));
3836
3837 // A remote offer with different m=line order should be rejected.
3838 ASSERT_TRUE(DoCreateOffer(&offer, &constraints));
3839 std::reverse(offer->description()->contents().begin(),
3840 offer->description()->contents().end());
3841 std::reverse(offer->description()->transport_infos().begin(),
3842 offer->description()->transport_infos().end());
3843 EXPECT_FALSE(DoSetRemoteDescription(std::move(offer)));
3844
3845 // A subsequent local offer with different m=line order should be rejected.
3846 ASSERT_TRUE(DoCreateOffer(&offer, &constraints));
3847 std::reverse(offer->description()->contents().begin(),
3848 offer->description()->contents().end());
3849 std::reverse(offer->description()->transport_infos().begin(),
3850 offer->description()->transport_infos().end());
3851 EXPECT_FALSE(DoSetLocalDescription(std::move(offer)));
3852}
3853
nisse51542be2016-02-12 02:27:06 -08003854class PeerConnectionMediaConfigTest : public testing::Test {
3855 protected:
3856 void SetUp() override {
zhihuang38ede132017-06-15 12:52:32 -07003857 pcf_ = PeerConnectionFactoryForTest::CreatePeerConnectionFactoryForTest();
nisse51542be2016-02-12 02:27:06 -08003858 pcf_->Initialize();
3859 }
nisseeaabdf62017-05-05 02:23:02 -07003860 const cricket::MediaConfig TestCreatePeerConnection(
nisse51542be2016-02-12 02:27:06 -08003861 const PeerConnectionInterface::RTCConfiguration& config,
zhihuang1c378ed2017-08-17 14:10:50 -07003862 const MediaConstraintsInterface* constraints) {
zhihuang9763d562016-08-05 11:14:50 -07003863 rtc::scoped_refptr<PeerConnectionInterface> pc(pcf_->CreatePeerConnection(
3864 config, constraints, nullptr, nullptr, &observer_));
nisse51542be2016-02-12 02:27:06 -08003865 EXPECT_TRUE(pc.get());
nisseeaabdf62017-05-05 02:23:02 -07003866 return pc->GetConfiguration().media_config;
nisse51542be2016-02-12 02:27:06 -08003867 }
3868
zhihuang9763d562016-08-05 11:14:50 -07003869 rtc::scoped_refptr<PeerConnectionFactoryForTest> pcf_;
nisse51542be2016-02-12 02:27:06 -08003870 MockPeerConnectionObserver observer_;
3871};
3872
3873// This test verifies the default behaviour with no constraints and a
3874// default RTCConfiguration.
3875TEST_F(PeerConnectionMediaConfigTest, TestDefaults) {
3876 PeerConnectionInterface::RTCConfiguration config;
3877 FakeConstraints constraints;
3878
3879 const cricket::MediaConfig& media_config =
3880 TestCreatePeerConnection(config, &constraints);
3881
3882 EXPECT_FALSE(media_config.enable_dscp);
nisse0db023a2016-03-01 04:29:59 -08003883 EXPECT_TRUE(media_config.video.enable_cpu_overuse_detection);
3884 EXPECT_FALSE(media_config.video.disable_prerenderer_smoothing);
3885 EXPECT_FALSE(media_config.video.suspend_below_min_bitrate);
nisse51542be2016-02-12 02:27:06 -08003886}
3887
3888// This test verifies the DSCP constraint is recognized and passed to
nisse528b7932017-05-08 03:21:43 -07003889// the PeerConnection.
nisse51542be2016-02-12 02:27:06 -08003890TEST_F(PeerConnectionMediaConfigTest, TestDscpConstraintTrue) {
3891 PeerConnectionInterface::RTCConfiguration config;
3892 FakeConstraints constraints;
3893
3894 constraints.AddOptional(webrtc::MediaConstraintsInterface::kEnableDscp, true);
3895 const cricket::MediaConfig& media_config =
3896 TestCreatePeerConnection(config, &constraints);
3897
3898 EXPECT_TRUE(media_config.enable_dscp);
3899}
3900
3901// This test verifies the cpu overuse detection constraint is
nisse528b7932017-05-08 03:21:43 -07003902// recognized and passed to the PeerConnection.
nisse51542be2016-02-12 02:27:06 -08003903TEST_F(PeerConnectionMediaConfigTest, TestCpuOveruseConstraintFalse) {
3904 PeerConnectionInterface::RTCConfiguration config;
3905 FakeConstraints constraints;
3906
3907 constraints.AddOptional(
3908 webrtc::MediaConstraintsInterface::kCpuOveruseDetection, false);
3909 const cricket::MediaConfig media_config =
3910 TestCreatePeerConnection(config, &constraints);
3911
nisse0db023a2016-03-01 04:29:59 -08003912 EXPECT_FALSE(media_config.video.enable_cpu_overuse_detection);
nisse51542be2016-02-12 02:27:06 -08003913}
3914
3915// This test verifies that the disable_prerenderer_smoothing flag is
nisse528b7932017-05-08 03:21:43 -07003916// propagated from RTCConfiguration to the PeerConnection.
nisse51542be2016-02-12 02:27:06 -08003917TEST_F(PeerConnectionMediaConfigTest, TestDisablePrerendererSmoothingTrue) {
3918 PeerConnectionInterface::RTCConfiguration config;
3919 FakeConstraints constraints;
3920
Niels Möller71bdda02016-03-31 12:59:59 +02003921 config.set_prerenderer_smoothing(false);
nisse51542be2016-02-12 02:27:06 -08003922 const cricket::MediaConfig& media_config =
3923 TestCreatePeerConnection(config, &constraints);
3924
nisse0db023a2016-03-01 04:29:59 -08003925 EXPECT_TRUE(media_config.video.disable_prerenderer_smoothing);
3926}
3927
3928// This test verifies the suspend below min bitrate constraint is
nisse528b7932017-05-08 03:21:43 -07003929// recognized and passed to the PeerConnection.
nisse0db023a2016-03-01 04:29:59 -08003930TEST_F(PeerConnectionMediaConfigTest,
3931 TestSuspendBelowMinBitrateConstraintTrue) {
3932 PeerConnectionInterface::RTCConfiguration config;
3933 FakeConstraints constraints;
3934
3935 constraints.AddOptional(
3936 webrtc::MediaConstraintsInterface::kEnableVideoSuspendBelowMinBitrate,
3937 true);
3938 const cricket::MediaConfig media_config =
3939 TestCreatePeerConnection(config, &constraints);
3940
3941 EXPECT_TRUE(media_config.video.suspend_below_min_bitrate);
nisse51542be2016-02-12 02:27:06 -08003942}
3943
deadbeef293e9262017-01-11 12:28:30 -08003944// Tests a few random fields being different.
3945TEST(RTCConfigurationTest, ComparisonOperators) {
3946 PeerConnectionInterface::RTCConfiguration a;
3947 PeerConnectionInterface::RTCConfiguration b;
3948 EXPECT_EQ(a, b);
3949
3950 PeerConnectionInterface::RTCConfiguration c;
3951 c.servers.push_back(PeerConnectionInterface::IceServer());
3952 EXPECT_NE(a, c);
3953
3954 PeerConnectionInterface::RTCConfiguration d;
3955 d.type = PeerConnectionInterface::kRelay;
3956 EXPECT_NE(a, d);
3957
3958 PeerConnectionInterface::RTCConfiguration e;
3959 e.audio_jitter_buffer_max_packets = 5;
3960 EXPECT_NE(a, e);
3961
3962 PeerConnectionInterface::RTCConfiguration f;
3963 f.ice_connection_receiving_timeout = 1337;
3964 EXPECT_NE(a, f);
3965
3966 PeerConnectionInterface::RTCConfiguration g;
3967 g.disable_ipv6 = true;
3968 EXPECT_NE(a, g);
3969
3970 PeerConnectionInterface::RTCConfiguration h(
3971 PeerConnectionInterface::RTCConfigurationType::kAggressive);
3972 EXPECT_NE(a, h);
3973}