blob: b8bf8b8435c4cc862788b1359a5904f3926d26c1 [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 Jedvert02e7a192017-09-23 17:21:32 +0200663 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 std::unique_ptr<cricket::MediaEngineInterface> media_engine,
672 std::unique_ptr<webrtc::CallFactoryInterface> call_factory,
673 std::unique_ptr<webrtc::RtcEventLogFactoryInterface> event_log_factory)
674 : webrtc::PeerConnectionFactory(network_thread,
675 worker_thread,
676 signaling_thread,
zhihuang38ede132017-06-15 12:52:32 -0700677 std::move(media_engine),
678 std::move(call_factory),
679 std::move(event_log_factory)) {}
kwiberg1e4e8cb2017-01-31 01:48:08 -0800680
zhihuang29ff8442016-07-27 11:07:25 -0700681 cricket::TransportController* CreateTransportController(
Honghai Zhangbfd398c2016-08-30 22:07:42 -0700682 cricket::PortAllocator* port_allocator,
683 bool redetermine_role_on_ice_restart) override {
zhihuang29ff8442016-07-27 11:07:25 -0700684 transport_controller = new cricket::TransportController(
Honghai Zhangbfd398c2016-08-30 22:07:42 -0700685 rtc::Thread::Current(), rtc::Thread::Current(), port_allocator,
deadbeef7914b8c2017-04-21 03:23:33 -0700686 redetermine_role_on_ice_restart, rtc::CryptoOptions());
zhihuang29ff8442016-07-27 11:07:25 -0700687 return transport_controller;
688 }
689
deadbeefd7850b22017-08-23 10:59:19 -0700690 rtc::scoped_refptr<FakeAudioCaptureModule> fake_audio_capture_module_;
zhihuang29ff8442016-07-27 11:07:25 -0700691 cricket::TransportController* transport_controller;
zhihuang29ff8442016-07-27 11:07:25 -0700692};
693
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000694class PeerConnectionInterfaceTest : public testing::Test {
695 protected:
deadbeef9a6f4d42017-05-15 19:43:33 -0700696 PeerConnectionInterfaceTest()
deadbeef98e186c2017-05-16 18:00:06 -0700697 : vss_(new rtc::VirtualSocketServer()), main_(vss_.get()) {
phoglund37ebcf02016-01-08 05:04:57 -0800698#ifdef WEBRTC_ANDROID
699 webrtc::InitializeAndroidObjects();
700#endif
701 }
702
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000703 virtual void SetUp() {
deadbeefd7850b22017-08-23 10:59:19 -0700704 // Use fake audio capture module since we're only testing the interface
705 // level, and using a real one could make tests flaky when run in parallel.
706 fake_audio_capture_module_ = FakeAudioCaptureModule::Create();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000707 pc_factory_ = webrtc::CreatePeerConnectionFactory(
danilchape9021a32016-05-17 01:52:02 -0700708 rtc::Thread::Current(), rtc::Thread::Current(), rtc::Thread::Current(),
deadbeefd7850b22017-08-23 10:59:19 -0700709 fake_audio_capture_module_, nullptr, nullptr);
danilchape9021a32016-05-17 01:52:02 -0700710 ASSERT_TRUE(pc_factory_);
zhihuang29ff8442016-07-27 11:07:25 -0700711 pc_factory_for_test_ =
zhihuang38ede132017-06-15 12:52:32 -0700712 PeerConnectionFactoryForTest::CreatePeerConnectionFactoryForTest();
zhihuang29ff8442016-07-27 11:07:25 -0700713 pc_factory_for_test_->Initialize();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000714 }
715
716 void CreatePeerConnection() {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700717 CreatePeerConnection(PeerConnectionInterface::RTCConfiguration(), nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000718 }
719
deadbeef293e9262017-01-11 12:28:30 -0800720 // DTLS does not work in a loopback call, so is disabled for most of the
721 // tests in this file.
722 void CreatePeerConnectionWithoutDtls() {
723 FakeConstraints no_dtls_constraints;
724 no_dtls_constraints.AddMandatory(
725 webrtc::MediaConstraintsInterface::kEnableDtlsSrtp, false);
726
727 CreatePeerConnection(PeerConnectionInterface::RTCConfiguration(),
728 &no_dtls_constraints);
729 }
730
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000731 void CreatePeerConnection(webrtc::MediaConstraintsInterface* constraints) {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700732 CreatePeerConnection(PeerConnectionInterface::RTCConfiguration(),
733 constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000734 }
735
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700736 void CreatePeerConnectionWithIceTransportsType(
737 PeerConnectionInterface::IceTransportsType type) {
738 PeerConnectionInterface::RTCConfiguration config;
739 config.type = type;
740 return CreatePeerConnection(config, nullptr);
741 }
742
743 void CreatePeerConnectionWithIceServer(const std::string& uri,
744 const std::string& password) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800745 PeerConnectionInterface::RTCConfiguration config;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000746 PeerConnectionInterface::IceServer server;
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700747 server.uri = uri;
748 server.password = password;
749 config.servers.push_back(server);
750 CreatePeerConnection(config, nullptr);
751 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000752
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700753 void CreatePeerConnection(PeerConnectionInterface::RTCConfiguration config,
754 webrtc::MediaConstraintsInterface* constraints) {
kwibergd1fe2812016-04-27 06:47:29 -0700755 std::unique_ptr<cricket::FakePortAllocator> port_allocator(
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800756 new cricket::FakePortAllocator(rtc::Thread::Current(), nullptr));
757 port_allocator_ = port_allocator.get();
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000758
deadbeef1dcb1642017-03-29 21:08:16 -0700759 // Create certificate generator unless DTLS constraint is explicitly set to
760 // false.
Henrik Boströmd79599d2016-06-01 13:58:50 +0200761 std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator;
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000762 bool dtls;
763 if (FindConstraint(constraints,
764 webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
765 &dtls,
Henrik Boström5e56c592015-08-11 10:33:13 +0200766 nullptr) && dtls) {
deadbeef8662f942017-01-20 21:20:51 -0800767 fake_certificate_generator_ = new FakeRTCCertificateGenerator();
768 cert_generator.reset(fake_certificate_generator_);
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000769 }
Henrik Boströmd79599d2016-06-01 13:58:50 +0200770 pc_ = pc_factory_->CreatePeerConnection(
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800771 config, constraints, std::move(port_allocator),
Henrik Boströmd79599d2016-06-01 13:58:50 +0200772 std::move(cert_generator), &observer_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000773 ASSERT_TRUE(pc_.get() != NULL);
774 observer_.SetPeerConnectionInterface(pc_.get());
775 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
776 }
777
deadbeef0a6c4ca2015-10-06 11:38:28 -0700778 void CreatePeerConnectionExpectFail(const std::string& uri) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800779 PeerConnectionInterface::RTCConfiguration config;
deadbeef0a6c4ca2015-10-06 11:38:28 -0700780 PeerConnectionInterface::IceServer server;
deadbeef0a6c4ca2015-10-06 11:38:28 -0700781 server.uri = uri;
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800782 config.servers.push_back(server);
deadbeef0a6c4ca2015-10-06 11:38:28 -0700783
zhihuang9763d562016-08-05 11:14:50 -0700784 rtc::scoped_refptr<PeerConnectionInterface> pc;
hbosd7973cc2016-05-27 06:08:53 -0700785 pc = pc_factory_->CreatePeerConnection(config, nullptr, nullptr, nullptr,
786 &observer_);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800787 EXPECT_EQ(nullptr, pc);
deadbeef0a6c4ca2015-10-06 11:38:28 -0700788 }
789
Steve Anton038834f2017-07-14 15:59:59 -0700790 void CreatePeerConnectionExpectFail(
791 PeerConnectionInterface::RTCConfiguration config) {
792 PeerConnectionInterface::IceServer server;
793 server.uri = kTurnIceServerUri;
794 server.password = kTurnPassword;
795 config.servers.push_back(server);
796 rtc::scoped_refptr<PeerConnectionInterface> pc =
797 pc_factory_->CreatePeerConnection(config, nullptr, nullptr, &observer_);
798 EXPECT_EQ(nullptr, pc);
799 }
800
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000801 void CreatePeerConnectionWithDifferentConfigurations() {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700802 CreatePeerConnectionWithIceServer(kStunAddressOnly, "");
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800803 EXPECT_EQ(1u, port_allocator_->stun_servers().size());
804 EXPECT_EQ(0u, port_allocator_->turn_servers().size());
805 EXPECT_EQ("address", port_allocator_->stun_servers().begin()->hostname());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000806 EXPECT_EQ(kDefaultStunPort,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800807 port_allocator_->stun_servers().begin()->port());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000808
deadbeef0a6c4ca2015-10-06 11:38:28 -0700809 CreatePeerConnectionExpectFail(kStunInvalidPort);
810 CreatePeerConnectionExpectFail(kStunAddressPortAndMore1);
811 CreatePeerConnectionExpectFail(kStunAddressPortAndMore2);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000812
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700813 CreatePeerConnectionWithIceServer(kTurnIceServerUri, kTurnPassword);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800814 EXPECT_EQ(0u, port_allocator_->stun_servers().size());
815 EXPECT_EQ(1u, port_allocator_->turn_servers().size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000816 EXPECT_EQ(kTurnUsername,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800817 port_allocator_->turn_servers()[0].credentials.username);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000818 EXPECT_EQ(kTurnPassword,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800819 port_allocator_->turn_servers()[0].credentials.password);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000820 EXPECT_EQ(kTurnHostname,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800821 port_allocator_->turn_servers()[0].ports[0].address.hostname());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000822 }
823
824 void ReleasePeerConnection() {
825 pc_ = NULL;
826 observer_.SetPeerConnectionInterface(NULL);
827 }
828
deadbeefab9b2d12015-10-14 11:33:11 -0700829 void AddVideoStream(const std::string& label) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000830 // Create a local stream.
zhihuang9763d562016-08-05 11:14:50 -0700831 rtc::scoped_refptr<MediaStreamInterface> stream(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000832 pc_factory_->CreateLocalMediaStream(label));
zhihuang9763d562016-08-05 11:14:50 -0700833 rtc::scoped_refptr<VideoTrackSourceInterface> video_source(
deadbeef112b2e92017-02-10 20:13:37 -0800834 pc_factory_->CreateVideoSource(std::unique_ptr<cricket::VideoCapturer>(
835 new cricket::FakeVideoCapturer()),
836 NULL));
zhihuang9763d562016-08-05 11:14:50 -0700837 rtc::scoped_refptr<VideoTrackInterface> video_track(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000838 pc_factory_->CreateVideoTrack(label + "v0", video_source));
839 stream->AddTrack(video_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +0000840 EXPECT_TRUE(pc_->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000841 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
842 observer_.renegotiation_needed_ = false;
843 }
844
845 void AddVoiceStream(const std::string& label) {
846 // Create a local stream.
zhihuang9763d562016-08-05 11:14:50 -0700847 rtc::scoped_refptr<MediaStreamInterface> stream(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000848 pc_factory_->CreateLocalMediaStream(label));
zhihuang9763d562016-08-05 11:14:50 -0700849 rtc::scoped_refptr<AudioTrackInterface> audio_track(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000850 pc_factory_->CreateAudioTrack(label + "a0", NULL));
851 stream->AddTrack(audio_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +0000852 EXPECT_TRUE(pc_->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000853 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
854 observer_.renegotiation_needed_ = false;
855 }
856
857 void AddAudioVideoStream(const std::string& stream_label,
858 const std::string& audio_track_label,
859 const std::string& video_track_label) {
860 // Create a local stream.
zhihuang9763d562016-08-05 11:14:50 -0700861 rtc::scoped_refptr<MediaStreamInterface> stream(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000862 pc_factory_->CreateLocalMediaStream(stream_label));
zhihuang9763d562016-08-05 11:14:50 -0700863 rtc::scoped_refptr<AudioTrackInterface> audio_track(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000864 pc_factory_->CreateAudioTrack(
865 audio_track_label, static_cast<AudioSourceInterface*>(NULL)));
866 stream->AddTrack(audio_track.get());
zhihuang9763d562016-08-05 11:14:50 -0700867 rtc::scoped_refptr<VideoTrackInterface> video_track(
nisseaf510af2016-03-21 08:20:42 -0700868 pc_factory_->CreateVideoTrack(
deadbeef112b2e92017-02-10 20:13:37 -0800869 video_track_label, pc_factory_->CreateVideoSource(
870 std::unique_ptr<cricket::VideoCapturer>(
871 new cricket::FakeVideoCapturer()))));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000872 stream->AddTrack(video_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +0000873 EXPECT_TRUE(pc_->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000874 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
875 observer_.renegotiation_needed_ = false;
876 }
877
kwibergd1fe2812016-04-27 06:47:29 -0700878 bool DoCreateOfferAnswer(std::unique_ptr<SessionDescriptionInterface>* desc,
deadbeefc80741f2015-10-22 13:14:45 -0700879 bool offer,
880 MediaConstraintsInterface* constraints) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000881 rtc::scoped_refptr<MockCreateSessionDescriptionObserver>
882 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000883 MockCreateSessionDescriptionObserver>());
884 if (offer) {
deadbeefc80741f2015-10-22 13:14:45 -0700885 pc_->CreateOffer(observer, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000886 } else {
deadbeefc80741f2015-10-22 13:14:45 -0700887 pc_->CreateAnswer(observer, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000888 }
889 EXPECT_EQ_WAIT(true, observer->called(), kTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -0700890 *desc = observer->MoveDescription();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000891 return observer->result();
892 }
893
kwibergd1fe2812016-04-27 06:47:29 -0700894 bool DoCreateOffer(std::unique_ptr<SessionDescriptionInterface>* desc,
deadbeefc80741f2015-10-22 13:14:45 -0700895 MediaConstraintsInterface* constraints) {
896 return DoCreateOfferAnswer(desc, true, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000897 }
898
kwibergd1fe2812016-04-27 06:47:29 -0700899 bool DoCreateAnswer(std::unique_ptr<SessionDescriptionInterface>* desc,
deadbeefc80741f2015-10-22 13:14:45 -0700900 MediaConstraintsInterface* constraints) {
901 return DoCreateOfferAnswer(desc, false, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000902 }
903
Steve Antondb45ca82017-09-11 18:27:34 -0700904 bool DoSetSessionDescription(
905 std::unique_ptr<SessionDescriptionInterface> desc,
906 bool local) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000907 rtc::scoped_refptr<MockSetSessionDescriptionObserver>
908 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000909 MockSetSessionDescriptionObserver>());
910 if (local) {
Steve Antondb45ca82017-09-11 18:27:34 -0700911 pc_->SetLocalDescription(observer, desc.release());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000912 } else {
Steve Antondb45ca82017-09-11 18:27:34 -0700913 pc_->SetRemoteDescription(observer, desc.release());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000914 }
zhihuang29ff8442016-07-27 11:07:25 -0700915 if (pc_->signaling_state() != PeerConnectionInterface::kClosed) {
916 EXPECT_EQ_WAIT(true, observer->called(), kTimeout);
917 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000918 return observer->result();
919 }
920
Steve Antondb45ca82017-09-11 18:27:34 -0700921 bool DoSetLocalDescription(
922 std::unique_ptr<SessionDescriptionInterface> desc) {
923 return DoSetSessionDescription(std::move(desc), true);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000924 }
925
Steve Antondb45ca82017-09-11 18:27:34 -0700926 bool DoSetRemoteDescription(
927 std::unique_ptr<SessionDescriptionInterface> desc) {
928 return DoSetSessionDescription(std::move(desc), false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000929 }
930
931 // Calls PeerConnection::GetStats and check the return value.
932 // It does not verify the values in the StatReports since a RTCP packet might
933 // be required.
934 bool DoGetStats(MediaStreamTrackInterface* track) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000935 rtc::scoped_refptr<MockStatsObserver> observer(
936 new rtc::RefCountedObject<MockStatsObserver>());
jiayl@webrtc.orgdb41b4d2014-03-03 21:30:06 +0000937 if (!pc_->GetStats(
938 observer, track, PeerConnectionInterface::kStatsOutputLevelStandard))
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000939 return false;
940 EXPECT_TRUE_WAIT(observer->called(), kTimeout);
941 return observer->called();
942 }
943
944 void InitiateCall() {
deadbeef293e9262017-01-11 12:28:30 -0800945 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000946 // Create a local stream with audio&video tracks.
947 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
948 CreateOfferReceiveAnswer();
949 }
950
951 // Verify that RTP Header extensions has been negotiated for audio and video.
952 void VerifyRemoteRtpHeaderExtensions() {
953 const cricket::MediaContentDescription* desc =
954 cricket::GetFirstAudioContentDescription(
955 pc_->remote_description()->description());
956 ASSERT_TRUE(desc != NULL);
957 EXPECT_GT(desc->rtp_header_extensions().size(), 0u);
958
959 desc = cricket::GetFirstVideoContentDescription(
960 pc_->remote_description()->description());
961 ASSERT_TRUE(desc != NULL);
962 EXPECT_GT(desc->rtp_header_extensions().size(), 0u);
963 }
964
965 void CreateOfferAsRemoteDescription() {
kwibergd1fe2812016-04-27 06:47:29 -0700966 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -0700967 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000968 std::string sdp;
969 EXPECT_TRUE(offer->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -0700970 std::unique_ptr<SessionDescriptionInterface> remote_offer(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000971 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
Steve Antondb45ca82017-09-11 18:27:34 -0700972 sdp, nullptr));
973 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_offer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000974 EXPECT_EQ(PeerConnectionInterface::kHaveRemoteOffer, observer_.state_);
975 }
976
deadbeefab9b2d12015-10-14 11:33:11 -0700977 void CreateAndSetRemoteOffer(const std::string& sdp) {
Steve Antondb45ca82017-09-11 18:27:34 -0700978 std::unique_ptr<SessionDescriptionInterface> remote_offer(
deadbeefab9b2d12015-10-14 11:33:11 -0700979 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
Steve Antondb45ca82017-09-11 18:27:34 -0700980 sdp, nullptr));
981 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_offer)));
deadbeefab9b2d12015-10-14 11:33:11 -0700982 EXPECT_EQ(PeerConnectionInterface::kHaveRemoteOffer, observer_.state_);
983 }
984
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000985 void CreateAnswerAsLocalDescription() {
kwibergd1fe2812016-04-27 06:47:29 -0700986 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -0700987 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000988
989 // TODO(perkj): Currently SetLocalDescription fails if any parameters in an
990 // audio codec change, even if the parameter has nothing to do with
991 // receiving. Not all parameters are serialized to SDP.
992 // Since CreatePrAnswerAsLocalDescription serialize/deserialize
993 // the SessionDescription, it is necessary to do that here to in order to
994 // get ReceiveOfferCreatePrAnswerAndAnswer and RenegotiateAudioOnly to pass.
995 // https://code.google.com/p/webrtc/issues/detail?id=1356
996 std::string sdp;
997 EXPECT_TRUE(answer->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -0700998 std::unique_ptr<SessionDescriptionInterface> new_answer(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000999 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
Steve Antondb45ca82017-09-11 18:27:34 -07001000 sdp, nullptr));
1001 EXPECT_TRUE(DoSetLocalDescription(std::move(new_answer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001002 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
1003 }
1004
1005 void CreatePrAnswerAsLocalDescription() {
kwibergd1fe2812016-04-27 06:47:29 -07001006 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -07001007 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001008
1009 std::string sdp;
1010 EXPECT_TRUE(answer->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07001011 std::unique_ptr<SessionDescriptionInterface> pr_answer(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001012 webrtc::CreateSessionDescription(SessionDescriptionInterface::kPrAnswer,
Steve Antondb45ca82017-09-11 18:27:34 -07001013 sdp, nullptr));
1014 EXPECT_TRUE(DoSetLocalDescription(std::move(pr_answer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001015 EXPECT_EQ(PeerConnectionInterface::kHaveLocalPrAnswer, observer_.state_);
1016 }
1017
1018 void CreateOfferReceiveAnswer() {
1019 CreateOfferAsLocalDescription();
1020 std::string sdp;
1021 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
1022 CreateAnswerAsRemoteDescription(sdp);
1023 }
1024
1025 void CreateOfferAsLocalDescription() {
kwibergd1fe2812016-04-27 06:47:29 -07001026 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001027 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001028 // TODO(perkj): Currently SetLocalDescription fails if any parameters in an
1029 // audio codec change, even if the parameter has nothing to do with
1030 // receiving. Not all parameters are serialized to SDP.
1031 // Since CreatePrAnswerAsLocalDescription serialize/deserialize
1032 // the SessionDescription, it is necessary to do that here to in order to
1033 // get ReceiveOfferCreatePrAnswerAndAnswer and RenegotiateAudioOnly to pass.
1034 // https://code.google.com/p/webrtc/issues/detail?id=1356
1035 std::string sdp;
1036 EXPECT_TRUE(offer->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07001037 std::unique_ptr<SessionDescriptionInterface> new_offer(
1038 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
1039 sdp, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001040
Steve Antondb45ca82017-09-11 18:27:34 -07001041 EXPECT_TRUE(DoSetLocalDescription(std::move(new_offer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001042 EXPECT_EQ(PeerConnectionInterface::kHaveLocalOffer, observer_.state_);
mallinath@webrtc.org68cbd012014-01-22 00:16:46 +00001043 // Wait for the ice_complete message, so that SDP will have candidates.
1044 EXPECT_TRUE_WAIT(observer_.ice_complete_, kTimeout);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001045 }
1046
deadbeefab9b2d12015-10-14 11:33:11 -07001047 void CreateAnswerAsRemoteDescription(const std::string& sdp) {
Steve Antondb45ca82017-09-11 18:27:34 -07001048 std::unique_ptr<SessionDescriptionInterface> answer(
1049 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
1050 sdp, nullptr));
1051 ASSERT_TRUE(answer);
1052 EXPECT_TRUE(DoSetRemoteDescription(std::move(answer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001053 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
1054 }
1055
deadbeefab9b2d12015-10-14 11:33:11 -07001056 void CreatePrAnswerAndAnswerAsRemoteDescription(const std::string& sdp) {
Steve Antondb45ca82017-09-11 18:27:34 -07001057 std::unique_ptr<SessionDescriptionInterface> pr_answer(
1058 webrtc::CreateSessionDescription(SessionDescriptionInterface::kPrAnswer,
1059 sdp, nullptr));
1060 ASSERT_TRUE(pr_answer);
1061 EXPECT_TRUE(DoSetRemoteDescription(std::move(pr_answer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001062 EXPECT_EQ(PeerConnectionInterface::kHaveRemotePrAnswer, observer_.state_);
Steve Antondb45ca82017-09-11 18:27:34 -07001063 std::unique_ptr<SessionDescriptionInterface> answer(
1064 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
1065 sdp, nullptr));
1066 ASSERT_TRUE(answer);
1067 EXPECT_TRUE(DoSetRemoteDescription(std::move(answer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001068 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
1069 }
1070
1071 // Help function used for waiting until a the last signaled remote stream has
1072 // the same label as |stream_label|. In a few of the tests in this file we
1073 // answer with the same session description as we offer and thus we can
1074 // check if OnAddStream have been called with the same stream as we offer to
1075 // send.
1076 void WaitAndVerifyOnAddStream(const std::string& stream_label) {
1077 EXPECT_EQ_WAIT(stream_label, observer_.GetLastAddedStreamLabel(), kTimeout);
1078 }
1079
1080 // Creates an offer and applies it as a local session description.
1081 // Creates an answer with the same SDP an the offer but removes all lines
1082 // that start with a:ssrc"
1083 void CreateOfferReceiveAnswerWithoutSsrc() {
1084 CreateOfferAsLocalDescription();
1085 std::string sdp;
1086 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
1087 SetSsrcToZero(&sdp);
1088 CreateAnswerAsRemoteDescription(sdp);
1089 }
1090
deadbeefab9b2d12015-10-14 11:33:11 -07001091 // This function creates a MediaStream with label kStreams[0] and
1092 // |number_of_audio_tracks| and |number_of_video_tracks| tracks and the
1093 // corresponding SessionDescriptionInterface. The SessionDescriptionInterface
kwiberg2bbff992016-03-16 11:03:04 -07001094 // is returned and the MediaStream is stored in
deadbeefab9b2d12015-10-14 11:33:11 -07001095 // |reference_collection_|
kwibergd1fe2812016-04-27 06:47:29 -07001096 std::unique_ptr<SessionDescriptionInterface>
kwiberg2bbff992016-03-16 11:03:04 -07001097 CreateSessionDescriptionAndReference(size_t number_of_audio_tracks,
1098 size_t number_of_video_tracks) {
1099 EXPECT_LE(number_of_audio_tracks, 2u);
1100 EXPECT_LE(number_of_video_tracks, 2u);
deadbeefab9b2d12015-10-14 11:33:11 -07001101
1102 reference_collection_ = StreamCollection::Create();
1103 std::string sdp_ms1 = std::string(kSdpStringInit);
1104
1105 std::string mediastream_label = kStreams[0];
1106
1107 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream(
1108 webrtc::MediaStream::Create(mediastream_label));
1109 reference_collection_->AddStream(stream);
1110
1111 if (number_of_audio_tracks > 0) {
1112 sdp_ms1 += std::string(kSdpStringAudio);
1113 sdp_ms1 += std::string(kSdpStringMs1Audio0);
1114 AddAudioTrack(kAudioTracks[0], stream);
1115 }
1116 if (number_of_audio_tracks > 1) {
1117 sdp_ms1 += kSdpStringMs1Audio1;
1118 AddAudioTrack(kAudioTracks[1], stream);
1119 }
1120
1121 if (number_of_video_tracks > 0) {
1122 sdp_ms1 += std::string(kSdpStringVideo);
1123 sdp_ms1 += std::string(kSdpStringMs1Video0);
1124 AddVideoTrack(kVideoTracks[0], stream);
1125 }
1126 if (number_of_video_tracks > 1) {
1127 sdp_ms1 += kSdpStringMs1Video1;
1128 AddVideoTrack(kVideoTracks[1], stream);
1129 }
1130
kwibergd1fe2812016-04-27 06:47:29 -07001131 return std::unique_ptr<SessionDescriptionInterface>(
kwiberg2bbff992016-03-16 11:03:04 -07001132 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
1133 sdp_ms1, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07001134 }
1135
1136 void AddAudioTrack(const std::string& track_id,
1137 MediaStreamInterface* stream) {
1138 rtc::scoped_refptr<webrtc::AudioTrackInterface> audio_track(
1139 webrtc::AudioTrack::Create(track_id, nullptr));
1140 ASSERT_TRUE(stream->AddTrack(audio_track));
1141 }
1142
1143 void AddVideoTrack(const std::string& track_id,
1144 MediaStreamInterface* stream) {
1145 rtc::scoped_refptr<webrtc::VideoTrackInterface> video_track(
nisseaf510af2016-03-21 08:20:42 -07001146 webrtc::VideoTrack::Create(track_id,
perkj773be362017-07-31 23:22:01 -07001147 webrtc::FakeVideoTrackSource::Create(),
1148 rtc::Thread::Current()));
deadbeefab9b2d12015-10-14 11:33:11 -07001149 ASSERT_TRUE(stream->AddTrack(video_track));
1150 }
1151
kwibergfd8be342016-05-14 19:44:11 -07001152 std::unique_ptr<SessionDescriptionInterface> CreateOfferWithOneAudioStream() {
deadbeef293e9262017-01-11 12:28:30 -08001153 CreatePeerConnectionWithoutDtls();
zhihuang8f65cdf2016-05-06 18:40:30 -07001154 AddVoiceStream(kStreamLabel1);
kwibergfd8be342016-05-14 19:44:11 -07001155 std::unique_ptr<SessionDescriptionInterface> offer;
zhihuang8f65cdf2016-05-06 18:40:30 -07001156 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
1157 return offer;
1158 }
1159
kwibergfd8be342016-05-14 19:44:11 -07001160 std::unique_ptr<SessionDescriptionInterface>
zhihuang8f65cdf2016-05-06 18:40:30 -07001161 CreateAnswerWithOneAudioStream() {
Steve Antondb45ca82017-09-11 18:27:34 -07001162 EXPECT_TRUE(DoSetRemoteDescription(CreateOfferWithOneAudioStream()));
kwibergfd8be342016-05-14 19:44:11 -07001163 std::unique_ptr<SessionDescriptionInterface> answer;
zhihuang8f65cdf2016-05-06 18:40:30 -07001164 EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
1165 return answer;
1166 }
1167
1168 const std::string& GetFirstAudioStreamCname(
1169 const SessionDescriptionInterface* desc) {
1170 const cricket::ContentInfo* audio_content =
1171 cricket::GetFirstAudioContent(desc->description());
1172 const cricket::AudioContentDescription* audio_desc =
1173 static_cast<const cricket::AudioContentDescription*>(
1174 audio_content->description);
1175 return audio_desc->streams()[0].cname;
1176 }
1177
zhihuang1c378ed2017-08-17 14:10:50 -07001178 std::unique_ptr<SessionDescriptionInterface> CreateOfferWithOptions(
1179 const RTCOfferAnswerOptions& offer_answer_options) {
1180 RTC_DCHECK(pc_);
1181 rtc::scoped_refptr<MockCreateSessionDescriptionObserver> observer(
1182 new rtc::RefCountedObject<MockCreateSessionDescriptionObserver>());
1183 pc_->CreateOffer(observer, offer_answer_options);
1184 EXPECT_EQ_WAIT(true, observer->called(), kTimeout);
1185 return observer->MoveDescription();
1186 }
1187
1188 void CreateOfferWithOptionsAsRemoteDescription(
1189 std::unique_ptr<SessionDescriptionInterface>* desc,
1190 const RTCOfferAnswerOptions& offer_answer_options) {
1191 *desc = CreateOfferWithOptions(offer_answer_options);
1192 ASSERT_TRUE(desc != nullptr);
1193 std::string sdp;
1194 EXPECT_TRUE((*desc)->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07001195 std::unique_ptr<SessionDescriptionInterface> remote_offer(
zhihuang1c378ed2017-08-17 14:10:50 -07001196 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
Steve Antondb45ca82017-09-11 18:27:34 -07001197 sdp, nullptr));
1198 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_offer)));
zhihuang1c378ed2017-08-17 14:10:50 -07001199 EXPECT_EQ(PeerConnectionInterface::kHaveRemoteOffer, observer_.state_);
1200 }
1201
1202 void CreateOfferWithOptionsAsLocalDescription(
1203 std::unique_ptr<SessionDescriptionInterface>* desc,
1204 const RTCOfferAnswerOptions& offer_answer_options) {
1205 *desc = CreateOfferWithOptions(offer_answer_options);
1206 ASSERT_TRUE(desc != nullptr);
1207 std::string sdp;
1208 EXPECT_TRUE((*desc)->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07001209 std::unique_ptr<SessionDescriptionInterface> new_offer(
1210 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
1211 sdp, nullptr));
zhihuang1c378ed2017-08-17 14:10:50 -07001212
Steve Antondb45ca82017-09-11 18:27:34 -07001213 EXPECT_TRUE(DoSetLocalDescription(std::move(new_offer)));
zhihuang1c378ed2017-08-17 14:10:50 -07001214 EXPECT_EQ(PeerConnectionInterface::kHaveLocalOffer, observer_.state_);
1215 }
1216
1217 bool HasCNCodecs(const cricket::ContentInfo* content) {
1218 const cricket::ContentDescription* description = content->description;
1219 RTC_DCHECK(description);
1220 const cricket::AudioContentDescription* audio_content_desc =
1221 static_cast<const cricket::AudioContentDescription*>(description);
1222 RTC_DCHECK(audio_content_desc);
1223 for (size_t i = 0; i < audio_content_desc->codecs().size(); ++i) {
1224 if (audio_content_desc->codecs()[i].name == "CN")
1225 return true;
1226 }
1227 return false;
1228 }
1229
deadbeef9a6f4d42017-05-15 19:43:33 -07001230 std::unique_ptr<rtc::VirtualSocketServer> vss_;
1231 rtc::AutoSocketServerThread main_;
deadbeefd7850b22017-08-23 10:59:19 -07001232 rtc::scoped_refptr<FakeAudioCaptureModule> fake_audio_capture_module_;
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08001233 cricket::FakePortAllocator* port_allocator_ = nullptr;
deadbeef8662f942017-01-20 21:20:51 -08001234 FakeRTCCertificateGenerator* fake_certificate_generator_ = nullptr;
zhihuang9763d562016-08-05 11:14:50 -07001235 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface> pc_factory_;
1236 rtc::scoped_refptr<PeerConnectionFactoryForTest> pc_factory_for_test_;
1237 rtc::scoped_refptr<PeerConnectionInterface> pc_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001238 MockPeerConnectionObserver observer_;
deadbeefab9b2d12015-10-14 11:33:11 -07001239 rtc::scoped_refptr<StreamCollection> reference_collection_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001240};
1241
zhihuang29ff8442016-07-27 11:07:25 -07001242// Test that no callbacks on the PeerConnectionObserver are called after the
1243// PeerConnection is closed.
1244TEST_F(PeerConnectionInterfaceTest, CloseAndTestCallbackFunctions) {
zhihuang9763d562016-08-05 11:14:50 -07001245 rtc::scoped_refptr<PeerConnectionInterface> pc(
zhihuang29ff8442016-07-27 11:07:25 -07001246 pc_factory_for_test_->CreatePeerConnection(
1247 PeerConnectionInterface::RTCConfiguration(), nullptr, nullptr,
1248 nullptr, &observer_));
1249 observer_.SetPeerConnectionInterface(pc.get());
1250 pc->Close();
1251
1252 // No callbacks is expected to be called.
zhihuang81c3a032016-11-17 12:06:24 -08001253 observer_.callback_triggered_ = false;
zhihuang29ff8442016-07-27 11:07:25 -07001254 std::vector<cricket::Candidate> candidates;
1255 pc_factory_for_test_->transport_controller->SignalGatheringState(
1256 cricket::IceGatheringState{});
1257 pc_factory_for_test_->transport_controller->SignalCandidatesGathered(
1258 "", candidates);
1259 pc_factory_for_test_->transport_controller->SignalConnectionState(
1260 cricket::IceConnectionState{});
1261 pc_factory_for_test_->transport_controller->SignalCandidatesRemoved(
1262 candidates);
1263 pc_factory_for_test_->transport_controller->SignalReceiving(false);
zhihuang81c3a032016-11-17 12:06:24 -08001264 EXPECT_FALSE(observer_.callback_triggered_);
zhihuang29ff8442016-07-27 11:07:25 -07001265}
1266
zhihuang8f65cdf2016-05-06 18:40:30 -07001267// Generate different CNAMEs when PeerConnections are created.
1268// The CNAMEs are expected to be generated randomly. It is possible
1269// that the test fails, though the possibility is very low.
1270TEST_F(PeerConnectionInterfaceTest, CnameGenerationInOffer) {
kwibergfd8be342016-05-14 19:44:11 -07001271 std::unique_ptr<SessionDescriptionInterface> offer1 =
zhihuang8f65cdf2016-05-06 18:40:30 -07001272 CreateOfferWithOneAudioStream();
kwibergfd8be342016-05-14 19:44:11 -07001273 std::unique_ptr<SessionDescriptionInterface> offer2 =
zhihuang8f65cdf2016-05-06 18:40:30 -07001274 CreateOfferWithOneAudioStream();
1275 EXPECT_NE(GetFirstAudioStreamCname(offer1.get()),
1276 GetFirstAudioStreamCname(offer2.get()));
1277}
1278
1279TEST_F(PeerConnectionInterfaceTest, CnameGenerationInAnswer) {
kwibergfd8be342016-05-14 19:44:11 -07001280 std::unique_ptr<SessionDescriptionInterface> answer1 =
zhihuang8f65cdf2016-05-06 18:40:30 -07001281 CreateAnswerWithOneAudioStream();
kwibergfd8be342016-05-14 19:44:11 -07001282 std::unique_ptr<SessionDescriptionInterface> answer2 =
zhihuang8f65cdf2016-05-06 18:40:30 -07001283 CreateAnswerWithOneAudioStream();
1284 EXPECT_NE(GetFirstAudioStreamCname(answer1.get()),
1285 GetFirstAudioStreamCname(answer2.get()));
1286}
1287
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001288TEST_F(PeerConnectionInterfaceTest,
1289 CreatePeerConnectionWithDifferentConfigurations) {
1290 CreatePeerConnectionWithDifferentConfigurations();
1291}
1292
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001293TEST_F(PeerConnectionInterfaceTest,
1294 CreatePeerConnectionWithDifferentIceTransportsTypes) {
1295 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kNone);
1296 EXPECT_EQ(cricket::CF_NONE, port_allocator_->candidate_filter());
1297 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kRelay);
1298 EXPECT_EQ(cricket::CF_RELAY, port_allocator_->candidate_filter());
1299 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kNoHost);
1300 EXPECT_EQ(cricket::CF_ALL & ~cricket::CF_HOST,
1301 port_allocator_->candidate_filter());
1302 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kAll);
1303 EXPECT_EQ(cricket::CF_ALL, port_allocator_->candidate_filter());
1304}
1305
1306// Test that when a PeerConnection is created with a nonzero candidate pool
1307// size, the pooled PortAllocatorSession is created with all the attributes
1308// in the RTCConfiguration.
1309TEST_F(PeerConnectionInterfaceTest, CreatePeerConnectionWithPooledCandidates) {
1310 PeerConnectionInterface::RTCConfiguration config;
1311 PeerConnectionInterface::IceServer server;
1312 server.uri = kStunAddressOnly;
1313 config.servers.push_back(server);
1314 config.type = PeerConnectionInterface::kRelay;
1315 config.disable_ipv6 = true;
1316 config.tcp_candidate_policy =
1317 PeerConnectionInterface::kTcpCandidatePolicyDisabled;
honghaiz60347052016-05-31 18:29:12 -07001318 config.candidate_network_policy =
1319 PeerConnectionInterface::kCandidateNetworkPolicyLowCost;
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001320 config.ice_candidate_pool_size = 1;
1321 CreatePeerConnection(config, nullptr);
1322
1323 const cricket::FakePortAllocatorSession* session =
1324 static_cast<const cricket::FakePortAllocatorSession*>(
1325 port_allocator_->GetPooledSession());
1326 ASSERT_NE(nullptr, session);
1327 EXPECT_EQ(1UL, session->stun_servers().size());
1328 EXPECT_EQ(0U, session->flags() & cricket::PORTALLOCATOR_ENABLE_IPV6);
1329 EXPECT_LT(0U, session->flags() & cricket::PORTALLOCATOR_DISABLE_TCP);
honghaiz60347052016-05-31 18:29:12 -07001330 EXPECT_LT(0U,
1331 session->flags() & cricket::PORTALLOCATOR_DISABLE_COSTLY_NETWORKS);
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001332}
1333
deadbeefd21eab32017-07-26 16:50:11 -07001334// Test that network-related RTCConfiguration members are applied to the
1335// PortAllocator when CreatePeerConnection is called. Specifically:
1336// - disable_ipv6_on_wifi
1337// - max_ipv6_networks
1338// - tcp_candidate_policy
1339// - candidate_network_policy
1340// - prune_turn_ports
1341//
1342// Note that the candidate filter (RTCConfiguration::type) is already tested
1343// above.
1344TEST_F(PeerConnectionInterfaceTest,
1345 CreatePeerConnectionAppliesNetworkConfigToPortAllocator) {
1346 // Create fake port allocator.
1347 std::unique_ptr<cricket::FakePortAllocator> port_allocator(
1348 new cricket::FakePortAllocator(rtc::Thread::Current(), nullptr));
1349 cricket::FakePortAllocator* raw_port_allocator = port_allocator.get();
1350
1351 // Create RTCConfiguration with some network-related fields relevant to
1352 // PortAllocator populated.
1353 PeerConnectionInterface::RTCConfiguration config;
1354 config.disable_ipv6_on_wifi = true;
1355 config.max_ipv6_networks = 10;
1356 config.tcp_candidate_policy =
1357 PeerConnectionInterface::kTcpCandidatePolicyDisabled;
1358 config.candidate_network_policy =
1359 PeerConnectionInterface::kCandidateNetworkPolicyLowCost;
1360 config.prune_turn_ports = true;
1361
1362 // Create the PC factory and PC with the above config.
1363 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface> pc_factory(
1364 webrtc::CreatePeerConnectionFactory(
1365 rtc::Thread::Current(), rtc::Thread::Current(),
deadbeefd7850b22017-08-23 10:59:19 -07001366 rtc::Thread::Current(), fake_audio_capture_module_, nullptr,
1367 nullptr));
deadbeefd21eab32017-07-26 16:50:11 -07001368 rtc::scoped_refptr<PeerConnectionInterface> pc(
1369 pc_factory->CreatePeerConnection(
1370 config, nullptr, std::move(port_allocator), nullptr, &observer_));
1371
1372 // Now validate that the config fields set above were applied to the
1373 // PortAllocator, as flags or otherwise.
1374 EXPECT_FALSE(raw_port_allocator->flags() &
1375 cricket::PORTALLOCATOR_ENABLE_IPV6_ON_WIFI);
1376 EXPECT_EQ(10, raw_port_allocator->max_ipv6_networks());
1377 EXPECT_TRUE(raw_port_allocator->flags() & cricket::PORTALLOCATOR_DISABLE_TCP);
1378 EXPECT_TRUE(raw_port_allocator->flags() &
1379 cricket::PORTALLOCATOR_DISABLE_COSTLY_NETWORKS);
1380 EXPECT_TRUE(raw_port_allocator->prune_turn_ports());
1381}
1382
Taylor Brandstetterf8e65772016-06-27 17:20:15 -07001383// Test that the PeerConnection initializes the port allocator passed into it,
1384// and on the correct thread.
1385TEST_F(PeerConnectionInterfaceTest,
deadbeefd21eab32017-07-26 16:50:11 -07001386 CreatePeerConnectionInitializesPortAllocatorOnNetworkThread) {
tommie7251592017-07-14 14:44:46 -07001387 std::unique_ptr<rtc::Thread> network_thread(
1388 rtc::Thread::CreateWithSocketServer());
1389 network_thread->Start();
Taylor Brandstetterf8e65772016-06-27 17:20:15 -07001390 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface> pc_factory(
1391 webrtc::CreatePeerConnectionFactory(
tommie7251592017-07-14 14:44:46 -07001392 network_thread.get(), rtc::Thread::Current(), rtc::Thread::Current(),
deadbeefd7850b22017-08-23 10:59:19 -07001393 fake_audio_capture_module_, nullptr, nullptr));
Taylor Brandstetterf8e65772016-06-27 17:20:15 -07001394 std::unique_ptr<cricket::FakePortAllocator> port_allocator(
tommie7251592017-07-14 14:44:46 -07001395 new cricket::FakePortAllocator(network_thread.get(), nullptr));
Taylor Brandstetterf8e65772016-06-27 17:20:15 -07001396 cricket::FakePortAllocator* raw_port_allocator = port_allocator.get();
1397 PeerConnectionInterface::RTCConfiguration config;
1398 rtc::scoped_refptr<PeerConnectionInterface> pc(
1399 pc_factory->CreatePeerConnection(
1400 config, nullptr, std::move(port_allocator), nullptr, &observer_));
1401 // FakePortAllocator RTC_CHECKs that it's initialized on the right thread,
1402 // so all we have to do here is check that it's initialized.
1403 EXPECT_TRUE(raw_port_allocator->initialized());
1404}
1405
deadbeef46c73892016-11-16 19:42:04 -08001406// Check that GetConfiguration returns the configuration the PeerConnection was
1407// constructed with, before SetConfiguration is called.
1408TEST_F(PeerConnectionInterfaceTest, GetConfigurationAfterCreatePeerConnection) {
1409 PeerConnectionInterface::RTCConfiguration config;
1410 config.type = PeerConnectionInterface::kRelay;
1411 CreatePeerConnection(config, nullptr);
1412
1413 PeerConnectionInterface::RTCConfiguration returned_config =
1414 pc_->GetConfiguration();
1415 EXPECT_EQ(PeerConnectionInterface::kRelay, returned_config.type);
1416}
1417
1418// Check that GetConfiguration returns the last configuration passed into
1419// SetConfiguration.
1420TEST_F(PeerConnectionInterfaceTest, GetConfigurationAfterSetConfiguration) {
1421 CreatePeerConnection();
1422
1423 PeerConnectionInterface::RTCConfiguration config;
1424 config.type = PeerConnectionInterface::kRelay;
1425 EXPECT_TRUE(pc_->SetConfiguration(config));
1426
1427 PeerConnectionInterface::RTCConfiguration returned_config =
1428 pc_->GetConfiguration();
1429 EXPECT_EQ(PeerConnectionInterface::kRelay, returned_config.type);
1430}
1431
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001432TEST_F(PeerConnectionInterfaceTest, AddStreams) {
deadbeef293e9262017-01-11 12:28:30 -08001433 CreatePeerConnectionWithoutDtls();
deadbeefab9b2d12015-10-14 11:33:11 -07001434 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001435 AddVoiceStream(kStreamLabel2);
1436 ASSERT_EQ(2u, pc_->local_streams()->count());
1437
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001438 // Test we can add multiple local streams to one peerconnection.
zhihuang9763d562016-08-05 11:14:50 -07001439 rtc::scoped_refptr<MediaStreamInterface> stream(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001440 pc_factory_->CreateLocalMediaStream(kStreamLabel3));
zhihuang9763d562016-08-05 11:14:50 -07001441 rtc::scoped_refptr<AudioTrackInterface> audio_track(
1442 pc_factory_->CreateAudioTrack(kStreamLabel3,
1443 static_cast<AudioSourceInterface*>(NULL)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001444 stream->AddTrack(audio_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +00001445 EXPECT_TRUE(pc_->AddStream(stream));
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001446 EXPECT_EQ(3u, pc_->local_streams()->count());
1447
1448 // Remove the third stream.
1449 pc_->RemoveStream(pc_->local_streams()->at(2));
1450 EXPECT_EQ(2u, pc_->local_streams()->count());
1451
1452 // Remove the second stream.
1453 pc_->RemoveStream(pc_->local_streams()->at(1));
1454 EXPECT_EQ(1u, pc_->local_streams()->count());
1455
1456 // Remove the first stream.
1457 pc_->RemoveStream(pc_->local_streams()->at(0));
1458 EXPECT_EQ(0u, pc_->local_streams()->count());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001459}
1460
deadbeefab9b2d12015-10-14 11:33:11 -07001461// Test that the created offer includes streams we added.
1462TEST_F(PeerConnectionInterfaceTest, AddedStreamsPresentInOffer) {
deadbeef293e9262017-01-11 12:28:30 -08001463 CreatePeerConnectionWithoutDtls();
deadbeefab9b2d12015-10-14 11:33:11 -07001464 AddAudioVideoStream(kStreamLabel1, "audio_track", "video_track");
kwibergd1fe2812016-04-27 06:47:29 -07001465 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001466 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07001467
1468 const cricket::ContentInfo* audio_content =
1469 cricket::GetFirstAudioContent(offer->description());
1470 const cricket::AudioContentDescription* audio_desc =
1471 static_cast<const cricket::AudioContentDescription*>(
1472 audio_content->description);
1473 EXPECT_TRUE(
1474 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
1475
1476 const cricket::ContentInfo* video_content =
1477 cricket::GetFirstVideoContent(offer->description());
1478 const cricket::VideoContentDescription* video_desc =
1479 static_cast<const cricket::VideoContentDescription*>(
1480 video_content->description);
1481 EXPECT_TRUE(
1482 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1483
1484 // Add another stream and ensure the offer includes both the old and new
1485 // streams.
1486 AddAudioVideoStream(kStreamLabel2, "audio_track2", "video_track2");
kwiberg2bbff992016-03-16 11:03:04 -07001487 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07001488
1489 audio_content = cricket::GetFirstAudioContent(offer->description());
1490 audio_desc = static_cast<const cricket::AudioContentDescription*>(
1491 audio_content->description);
1492 EXPECT_TRUE(
1493 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
1494 EXPECT_TRUE(
1495 ContainsTrack(audio_desc->streams(), kStreamLabel2, "audio_track2"));
1496
1497 video_content = cricket::GetFirstVideoContent(offer->description());
1498 video_desc = static_cast<const cricket::VideoContentDescription*>(
1499 video_content->description);
1500 EXPECT_TRUE(
1501 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1502 EXPECT_TRUE(
1503 ContainsTrack(video_desc->streams(), kStreamLabel2, "video_track2"));
1504}
1505
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001506TEST_F(PeerConnectionInterfaceTest, RemoveStream) {
deadbeef293e9262017-01-11 12:28:30 -08001507 CreatePeerConnectionWithoutDtls();
deadbeefab9b2d12015-10-14 11:33:11 -07001508 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001509 ASSERT_EQ(1u, pc_->local_streams()->count());
1510 pc_->RemoveStream(pc_->local_streams()->at(0));
1511 EXPECT_EQ(0u, pc_->local_streams()->count());
1512}
1513
deadbeefe1f9d832016-01-14 15:35:42 -08001514// Test for AddTrack and RemoveTrack methods.
1515// Tests that the created offer includes tracks we added,
1516// and that the RtpSenders are created correctly.
1517// Also tests that RemoveTrack removes the tracks from subsequent offers.
1518TEST_F(PeerConnectionInterfaceTest, AddTrackRemoveTrack) {
deadbeef293e9262017-01-11 12:28:30 -08001519 CreatePeerConnectionWithoutDtls();
deadbeefe1f9d832016-01-14 15:35:42 -08001520 // Create a dummy stream, so tracks share a stream label.
zhihuang9763d562016-08-05 11:14:50 -07001521 rtc::scoped_refptr<MediaStreamInterface> stream(
deadbeefe1f9d832016-01-14 15:35:42 -08001522 pc_factory_->CreateLocalMediaStream(kStreamLabel1));
1523 std::vector<MediaStreamInterface*> stream_list;
1524 stream_list.push_back(stream.get());
zhihuang9763d562016-08-05 11:14:50 -07001525 rtc::scoped_refptr<AudioTrackInterface> audio_track(
deadbeefe1f9d832016-01-14 15:35:42 -08001526 pc_factory_->CreateAudioTrack("audio_track", nullptr));
zhihuang9763d562016-08-05 11:14:50 -07001527 rtc::scoped_refptr<VideoTrackInterface> video_track(
1528 pc_factory_->CreateVideoTrack(
deadbeef112b2e92017-02-10 20:13:37 -08001529 "video_track", pc_factory_->CreateVideoSource(
1530 std::unique_ptr<cricket::VideoCapturer>(
1531 new cricket::FakeVideoCapturer()))));
deadbeefe1f9d832016-01-14 15:35:42 -08001532 auto audio_sender = pc_->AddTrack(audio_track, stream_list);
1533 auto video_sender = pc_->AddTrack(video_track, stream_list);
deadbeefa601f5c2016-06-06 14:27:39 -07001534 EXPECT_EQ(1UL, audio_sender->stream_ids().size());
1535 EXPECT_EQ(kStreamLabel1, audio_sender->stream_ids()[0]);
deadbeefe1f9d832016-01-14 15:35:42 -08001536 EXPECT_EQ("audio_track", audio_sender->id());
1537 EXPECT_EQ(audio_track, audio_sender->track());
deadbeefa601f5c2016-06-06 14:27:39 -07001538 EXPECT_EQ(1UL, video_sender->stream_ids().size());
1539 EXPECT_EQ(kStreamLabel1, video_sender->stream_ids()[0]);
deadbeefe1f9d832016-01-14 15:35:42 -08001540 EXPECT_EQ("video_track", video_sender->id());
1541 EXPECT_EQ(video_track, video_sender->track());
1542
1543 // Now create an offer and check for the senders.
kwibergd1fe2812016-04-27 06:47:29 -07001544 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001545 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefe1f9d832016-01-14 15:35:42 -08001546
1547 const cricket::ContentInfo* audio_content =
1548 cricket::GetFirstAudioContent(offer->description());
1549 const cricket::AudioContentDescription* audio_desc =
1550 static_cast<const cricket::AudioContentDescription*>(
1551 audio_content->description);
1552 EXPECT_TRUE(
1553 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
1554
1555 const cricket::ContentInfo* video_content =
1556 cricket::GetFirstVideoContent(offer->description());
1557 const cricket::VideoContentDescription* video_desc =
1558 static_cast<const cricket::VideoContentDescription*>(
1559 video_content->description);
1560 EXPECT_TRUE(
1561 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1562
Steve Antondb45ca82017-09-11 18:27:34 -07001563 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefe1f9d832016-01-14 15:35:42 -08001564
1565 // Now try removing the tracks.
1566 EXPECT_TRUE(pc_->RemoveTrack(audio_sender));
1567 EXPECT_TRUE(pc_->RemoveTrack(video_sender));
1568
1569 // Create a new offer and ensure it doesn't contain the removed senders.
kwiberg2bbff992016-03-16 11:03:04 -07001570 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefe1f9d832016-01-14 15:35:42 -08001571
1572 audio_content = cricket::GetFirstAudioContent(offer->description());
1573 audio_desc = static_cast<const cricket::AudioContentDescription*>(
1574 audio_content->description);
1575 EXPECT_FALSE(
1576 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
1577
1578 video_content = cricket::GetFirstVideoContent(offer->description());
1579 video_desc = static_cast<const cricket::VideoContentDescription*>(
1580 video_content->description);
1581 EXPECT_FALSE(
1582 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1583
Steve Antondb45ca82017-09-11 18:27:34 -07001584 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefe1f9d832016-01-14 15:35:42 -08001585
1586 // Calling RemoveTrack on a sender no longer attached to a PeerConnection
1587 // should return false.
1588 EXPECT_FALSE(pc_->RemoveTrack(audio_sender));
1589 EXPECT_FALSE(pc_->RemoveTrack(video_sender));
1590}
1591
1592// Test creating senders without a stream specified,
1593// expecting a random stream ID to be generated.
1594TEST_F(PeerConnectionInterfaceTest, AddTrackWithoutStream) {
deadbeef293e9262017-01-11 12:28:30 -08001595 CreatePeerConnectionWithoutDtls();
deadbeefe1f9d832016-01-14 15:35:42 -08001596 // Create a dummy stream, so tracks share a stream label.
zhihuang9763d562016-08-05 11:14:50 -07001597 rtc::scoped_refptr<AudioTrackInterface> audio_track(
deadbeefe1f9d832016-01-14 15:35:42 -08001598 pc_factory_->CreateAudioTrack("audio_track", nullptr));
zhihuang9763d562016-08-05 11:14:50 -07001599 rtc::scoped_refptr<VideoTrackInterface> video_track(
1600 pc_factory_->CreateVideoTrack(
deadbeef112b2e92017-02-10 20:13:37 -08001601 "video_track", pc_factory_->CreateVideoSource(
1602 std::unique_ptr<cricket::VideoCapturer>(
1603 new cricket::FakeVideoCapturer()))));
deadbeefe1f9d832016-01-14 15:35:42 -08001604 auto audio_sender =
1605 pc_->AddTrack(audio_track, std::vector<MediaStreamInterface*>());
1606 auto video_sender =
1607 pc_->AddTrack(video_track, std::vector<MediaStreamInterface*>());
1608 EXPECT_EQ("audio_track", audio_sender->id());
1609 EXPECT_EQ(audio_track, audio_sender->track());
1610 EXPECT_EQ("video_track", video_sender->id());
1611 EXPECT_EQ(video_track, video_sender->track());
1612 // If the ID is truly a random GUID, it should be infinitely unlikely they
1613 // will be the same.
deadbeefa601f5c2016-06-06 14:27:39 -07001614 EXPECT_NE(video_sender->stream_ids(), audio_sender->stream_ids());
deadbeefe1f9d832016-01-14 15:35:42 -08001615}
1616
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001617TEST_F(PeerConnectionInterfaceTest, CreateOfferReceiveAnswer) {
1618 InitiateCall();
1619 WaitAndVerifyOnAddStream(kStreamLabel1);
1620 VerifyRemoteRtpHeaderExtensions();
1621}
1622
1623TEST_F(PeerConnectionInterfaceTest, CreateOfferReceivePrAnswerAndAnswer) {
deadbeef293e9262017-01-11 12:28:30 -08001624 CreatePeerConnectionWithoutDtls();
deadbeefab9b2d12015-10-14 11:33:11 -07001625 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001626 CreateOfferAsLocalDescription();
1627 std::string offer;
1628 EXPECT_TRUE(pc_->local_description()->ToString(&offer));
1629 CreatePrAnswerAndAnswerAsRemoteDescription(offer);
1630 WaitAndVerifyOnAddStream(kStreamLabel1);
1631}
1632
1633TEST_F(PeerConnectionInterfaceTest, ReceiveOfferCreateAnswer) {
deadbeef293e9262017-01-11 12:28:30 -08001634 CreatePeerConnectionWithoutDtls();
deadbeefab9b2d12015-10-14 11:33:11 -07001635 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001636
1637 CreateOfferAsRemoteDescription();
1638 CreateAnswerAsLocalDescription();
1639
1640 WaitAndVerifyOnAddStream(kStreamLabel1);
1641}
1642
1643TEST_F(PeerConnectionInterfaceTest, ReceiveOfferCreatePrAnswerAndAnswer) {
deadbeef293e9262017-01-11 12:28:30 -08001644 CreatePeerConnectionWithoutDtls();
deadbeefab9b2d12015-10-14 11:33:11 -07001645 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001646
1647 CreateOfferAsRemoteDescription();
1648 CreatePrAnswerAsLocalDescription();
1649 CreateAnswerAsLocalDescription();
1650
1651 WaitAndVerifyOnAddStream(kStreamLabel1);
1652}
1653
1654TEST_F(PeerConnectionInterfaceTest, Renegotiate) {
1655 InitiateCall();
1656 ASSERT_EQ(1u, pc_->remote_streams()->count());
1657 pc_->RemoveStream(pc_->local_streams()->at(0));
1658 CreateOfferReceiveAnswer();
1659 EXPECT_EQ(0u, pc_->remote_streams()->count());
deadbeefab9b2d12015-10-14 11:33:11 -07001660 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001661 CreateOfferReceiveAnswer();
1662}
1663
1664// Tests that after negotiating an audio only call, the respondent can perform a
1665// renegotiation that removes the audio stream.
1666TEST_F(PeerConnectionInterfaceTest, RenegotiateAudioOnly) {
deadbeef293e9262017-01-11 12:28:30 -08001667 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001668 AddVoiceStream(kStreamLabel1);
1669 CreateOfferAsRemoteDescription();
1670 CreateAnswerAsLocalDescription();
1671
1672 ASSERT_EQ(1u, pc_->remote_streams()->count());
1673 pc_->RemoveStream(pc_->local_streams()->at(0));
1674 CreateOfferReceiveAnswer();
1675 EXPECT_EQ(0u, pc_->remote_streams()->count());
1676}
1677
1678// Test that candidates are generated and that we can parse our own candidates.
1679TEST_F(PeerConnectionInterfaceTest, IceCandidates) {
deadbeef293e9262017-01-11 12:28:30 -08001680 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001681
1682 EXPECT_FALSE(pc_->AddIceCandidate(observer_.last_candidate_.get()));
1683 // SetRemoteDescription takes ownership of offer.
kwibergd1fe2812016-04-27 06:47:29 -07001684 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefab9b2d12015-10-14 11:33:11 -07001685 AddVideoStream(kStreamLabel1);
deadbeefc80741f2015-10-22 13:14:45 -07001686 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07001687 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001688
1689 // SetLocalDescription takes ownership of answer.
kwibergd1fe2812016-04-27 06:47:29 -07001690 std::unique_ptr<SessionDescriptionInterface> answer;
deadbeefc80741f2015-10-22 13:14:45 -07001691 EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07001692 EXPECT_TRUE(DoSetLocalDescription(std::move(answer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001693
1694 EXPECT_TRUE_WAIT(observer_.last_candidate_.get() != NULL, kTimeout);
1695 EXPECT_TRUE_WAIT(observer_.ice_complete_, kTimeout);
1696
1697 EXPECT_TRUE(pc_->AddIceCandidate(observer_.last_candidate_.get()));
1698}
1699
deadbeefab9b2d12015-10-14 11:33:11 -07001700// Test that CreateOffer and CreateAnswer will fail if the track labels are
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001701// not unique.
1702TEST_F(PeerConnectionInterfaceTest, CreateOfferAnswerWithInvalidStream) {
deadbeef293e9262017-01-11 12:28:30 -08001703 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001704 // Create a regular offer for the CreateAnswer test later.
kwibergd1fe2812016-04-27 06:47:29 -07001705 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefc80741f2015-10-22 13:14:45 -07001706 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
kwiberg2bbff992016-03-16 11:03:04 -07001707 EXPECT_TRUE(offer);
1708 offer.reset();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001709
1710 // Create a local stream with audio&video tracks having same label.
1711 AddAudioVideoStream(kStreamLabel1, "track_label", "track_label");
1712
1713 // Test CreateOffer
deadbeefc80741f2015-10-22 13:14:45 -07001714 EXPECT_FALSE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001715
1716 // Test CreateAnswer
kwibergd1fe2812016-04-27 06:47:29 -07001717 std::unique_ptr<SessionDescriptionInterface> answer;
deadbeefc80741f2015-10-22 13:14:45 -07001718 EXPECT_FALSE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001719}
1720
1721// Test that we will get different SSRCs for each tracks in the offer and answer
1722// we created.
1723TEST_F(PeerConnectionInterfaceTest, SsrcInOfferAnswer) {
deadbeef293e9262017-01-11 12:28:30 -08001724 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001725 // Create a local stream with audio&video tracks having different labels.
1726 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
1727
1728 // Test CreateOffer
kwibergd1fe2812016-04-27 06:47:29 -07001729 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001730 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001731 int audio_ssrc = 0;
1732 int video_ssrc = 0;
1733 EXPECT_TRUE(GetFirstSsrc(GetFirstAudioContent(offer->description()),
1734 &audio_ssrc));
1735 EXPECT_TRUE(GetFirstSsrc(GetFirstVideoContent(offer->description()),
1736 &video_ssrc));
1737 EXPECT_NE(audio_ssrc, video_ssrc);
1738
1739 // Test CreateAnswer
Steve Antondb45ca82017-09-11 18:27:34 -07001740 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
kwibergd1fe2812016-04-27 06:47:29 -07001741 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -07001742 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001743 audio_ssrc = 0;
1744 video_ssrc = 0;
1745 EXPECT_TRUE(GetFirstSsrc(GetFirstAudioContent(answer->description()),
1746 &audio_ssrc));
1747 EXPECT_TRUE(GetFirstSsrc(GetFirstVideoContent(answer->description()),
1748 &video_ssrc));
1749 EXPECT_NE(audio_ssrc, video_ssrc);
1750}
1751
deadbeefeb459812015-12-15 19:24:43 -08001752// Test that it's possible to call AddTrack on a MediaStream after adding
1753// the stream to a PeerConnection.
1754// TODO(deadbeef): Remove this test once this behavior is no longer supported.
1755TEST_F(PeerConnectionInterfaceTest, AddTrackAfterAddStream) {
deadbeef293e9262017-01-11 12:28:30 -08001756 CreatePeerConnectionWithoutDtls();
deadbeefeb459812015-12-15 19:24:43 -08001757 // Create audio stream and add to PeerConnection.
1758 AddVoiceStream(kStreamLabel1);
1759 MediaStreamInterface* stream = pc_->local_streams()->at(0);
1760
1761 // Add video track to the audio-only stream.
zhihuang9763d562016-08-05 11:14:50 -07001762 rtc::scoped_refptr<VideoTrackInterface> video_track(
1763 pc_factory_->CreateVideoTrack(
deadbeef112b2e92017-02-10 20:13:37 -08001764 "video_label", pc_factory_->CreateVideoSource(
1765 std::unique_ptr<cricket::VideoCapturer>(
1766 new cricket::FakeVideoCapturer()))));
deadbeefeb459812015-12-15 19:24:43 -08001767 stream->AddTrack(video_track.get());
1768
kwibergd1fe2812016-04-27 06:47:29 -07001769 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001770 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefeb459812015-12-15 19:24:43 -08001771
1772 const cricket::MediaContentDescription* video_desc =
1773 cricket::GetFirstVideoContentDescription(offer->description());
1774 EXPECT_TRUE(video_desc != nullptr);
1775}
1776
1777// Test that it's possible to call RemoveTrack on a MediaStream after adding
1778// the stream to a PeerConnection.
1779// TODO(deadbeef): Remove this test once this behavior is no longer supported.
1780TEST_F(PeerConnectionInterfaceTest, RemoveTrackAfterAddStream) {
deadbeef293e9262017-01-11 12:28:30 -08001781 CreatePeerConnectionWithoutDtls();
deadbeefeb459812015-12-15 19:24:43 -08001782 // Create audio/video stream and add to PeerConnection.
1783 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
1784 MediaStreamInterface* stream = pc_->local_streams()->at(0);
1785
1786 // Remove the video track.
1787 stream->RemoveTrack(stream->GetVideoTracks()[0]);
1788
kwibergd1fe2812016-04-27 06:47:29 -07001789 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001790 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefeb459812015-12-15 19:24:43 -08001791
1792 const cricket::MediaContentDescription* video_desc =
1793 cricket::GetFirstVideoContentDescription(offer->description());
1794 EXPECT_TRUE(video_desc == nullptr);
1795}
1796
deadbeef1dcb1642017-03-29 21:08:16 -07001797// Verify that CreateDtmfSender only succeeds if called with a valid local
1798// track. Other aspects of DtmfSenders are tested in
1799// peerconnection_integrationtest.cc.
1800TEST_F(PeerConnectionInterfaceTest, CreateDtmfSenderWithInvalidParams) {
1801 CreatePeerConnection();
1802 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
1803 EXPECT_EQ(nullptr, pc_->CreateDtmfSender(nullptr));
1804 rtc::scoped_refptr<webrtc::AudioTrackInterface> non_localtrack(
1805 pc_factory_->CreateAudioTrack("dummy_track", nullptr));
1806 EXPECT_EQ(nullptr, pc_->CreateDtmfSender(non_localtrack));
1807}
1808
deadbeefbd7d8f72015-12-18 16:58:44 -08001809// Test creating a sender with a stream ID, and ensure the ID is populated
1810// in the offer.
1811TEST_F(PeerConnectionInterfaceTest, CreateSenderWithStream) {
deadbeef293e9262017-01-11 12:28:30 -08001812 CreatePeerConnectionWithoutDtls();
deadbeefbd7d8f72015-12-18 16:58:44 -08001813 pc_->CreateSender("video", kStreamLabel1);
1814
kwibergd1fe2812016-04-27 06:47:29 -07001815 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001816 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefbd7d8f72015-12-18 16:58:44 -08001817
1818 const cricket::MediaContentDescription* video_desc =
1819 cricket::GetFirstVideoContentDescription(offer->description());
1820 ASSERT_TRUE(video_desc != nullptr);
1821 ASSERT_EQ(1u, video_desc->streams().size());
1822 EXPECT_EQ(kStreamLabel1, video_desc->streams()[0].sync_label);
1823}
1824
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001825// Test that we can specify a certain track that we want statistics about.
1826TEST_F(PeerConnectionInterfaceTest, GetStatsForSpecificTrack) {
1827 InitiateCall();
1828 ASSERT_LT(0u, pc_->remote_streams()->count());
1829 ASSERT_LT(0u, pc_->remote_streams()->at(0)->GetAudioTracks().size());
zhihuang9763d562016-08-05 11:14:50 -07001830 rtc::scoped_refptr<MediaStreamTrackInterface> remote_audio =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001831 pc_->remote_streams()->at(0)->GetAudioTracks()[0];
1832 EXPECT_TRUE(DoGetStats(remote_audio));
1833
1834 // Remove the stream. Since we are sending to our selves the local
1835 // and the remote stream is the same.
1836 pc_->RemoveStream(pc_->local_streams()->at(0));
1837 // Do a re-negotiation.
1838 CreateOfferReceiveAnswer();
1839
1840 ASSERT_EQ(0u, pc_->remote_streams()->count());
1841
1842 // Test that we still can get statistics for the old track. Even if it is not
1843 // sent any longer.
1844 EXPECT_TRUE(DoGetStats(remote_audio));
1845}
1846
1847// Test that we can get stats on a video track.
1848TEST_F(PeerConnectionInterfaceTest, GetStatsForVideoTrack) {
1849 InitiateCall();
1850 ASSERT_LT(0u, pc_->remote_streams()->count());
1851 ASSERT_LT(0u, pc_->remote_streams()->at(0)->GetVideoTracks().size());
zhihuang9763d562016-08-05 11:14:50 -07001852 rtc::scoped_refptr<MediaStreamTrackInterface> remote_video =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001853 pc_->remote_streams()->at(0)->GetVideoTracks()[0];
1854 EXPECT_TRUE(DoGetStats(remote_video));
1855}
1856
1857// Test that we don't get statistics for an invalid track.
zhihuange9e94c32016-11-04 11:38:15 -07001858TEST_F(PeerConnectionInterfaceTest, GetStatsForInvalidTrack) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001859 InitiateCall();
zhihuang9763d562016-08-05 11:14:50 -07001860 rtc::scoped_refptr<AudioTrackInterface> unknown_audio_track(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001861 pc_factory_->CreateAudioTrack("unknown track", NULL));
1862 EXPECT_FALSE(DoGetStats(unknown_audio_track));
1863}
1864
1865// This test setup two RTP data channels in loop back.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001866TEST_F(PeerConnectionInterfaceTest, TestDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001867 FakeConstraints constraints;
1868 constraints.SetAllowRtpDataChannels();
1869 CreatePeerConnection(&constraints);
zhihuang9763d562016-08-05 11:14:50 -07001870 rtc::scoped_refptr<DataChannelInterface> data1 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001871 pc_->CreateDataChannel("test1", NULL);
zhihuang9763d562016-08-05 11:14:50 -07001872 rtc::scoped_refptr<DataChannelInterface> data2 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001873 pc_->CreateDataChannel("test2", NULL);
1874 ASSERT_TRUE(data1 != NULL);
kwibergd1fe2812016-04-27 06:47:29 -07001875 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001876 new MockDataChannelObserver(data1));
kwibergd1fe2812016-04-27 06:47:29 -07001877 std::unique_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001878 new MockDataChannelObserver(data2));
1879
1880 EXPECT_EQ(DataChannelInterface::kConnecting, data1->state());
1881 EXPECT_EQ(DataChannelInterface::kConnecting, data2->state());
1882 std::string data_to_send1 = "testing testing";
1883 std::string data_to_send2 = "testing something else";
1884 EXPECT_FALSE(data1->Send(DataBuffer(data_to_send1)));
1885
1886 CreateOfferReceiveAnswer();
1887 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1888 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
1889
1890 EXPECT_EQ(DataChannelInterface::kOpen, data1->state());
1891 EXPECT_EQ(DataChannelInterface::kOpen, data2->state());
1892 EXPECT_TRUE(data1->Send(DataBuffer(data_to_send1)));
1893 EXPECT_TRUE(data2->Send(DataBuffer(data_to_send2)));
1894
1895 EXPECT_EQ_WAIT(data_to_send1, observer1->last_message(), kTimeout);
1896 EXPECT_EQ_WAIT(data_to_send2, observer2->last_message(), kTimeout);
1897
1898 data1->Close();
1899 EXPECT_EQ(DataChannelInterface::kClosing, data1->state());
1900 CreateOfferReceiveAnswer();
1901 EXPECT_FALSE(observer1->IsOpen());
1902 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
1903 EXPECT_TRUE(observer2->IsOpen());
1904
1905 data_to_send2 = "testing something else again";
1906 EXPECT_TRUE(data2->Send(DataBuffer(data_to_send2)));
1907
1908 EXPECT_EQ_WAIT(data_to_send2, observer2->last_message(), kTimeout);
1909}
1910
1911// This test verifies that sendnig binary data over RTP data channels should
1912// fail.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001913TEST_F(PeerConnectionInterfaceTest, TestSendBinaryOnRtpDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001914 FakeConstraints constraints;
1915 constraints.SetAllowRtpDataChannels();
1916 CreatePeerConnection(&constraints);
zhihuang9763d562016-08-05 11:14:50 -07001917 rtc::scoped_refptr<DataChannelInterface> data1 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001918 pc_->CreateDataChannel("test1", NULL);
zhihuang9763d562016-08-05 11:14:50 -07001919 rtc::scoped_refptr<DataChannelInterface> data2 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001920 pc_->CreateDataChannel("test2", NULL);
1921 ASSERT_TRUE(data1 != NULL);
kwibergd1fe2812016-04-27 06:47:29 -07001922 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001923 new MockDataChannelObserver(data1));
kwibergd1fe2812016-04-27 06:47:29 -07001924 std::unique_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001925 new MockDataChannelObserver(data2));
1926
1927 EXPECT_EQ(DataChannelInterface::kConnecting, data1->state());
1928 EXPECT_EQ(DataChannelInterface::kConnecting, data2->state());
1929
1930 CreateOfferReceiveAnswer();
1931 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1932 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
1933
1934 EXPECT_EQ(DataChannelInterface::kOpen, data1->state());
1935 EXPECT_EQ(DataChannelInterface::kOpen, data2->state());
1936
jbaucheec21bd2016-03-20 06:15:43 -07001937 rtc::CopyOnWriteBuffer buffer("test", 4);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001938 EXPECT_FALSE(data1->Send(DataBuffer(buffer, true)));
1939}
1940
1941// This test setup a RTP data channels in loop back and test that a channel is
1942// opened even if the remote end answer with a zero SSRC.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001943TEST_F(PeerConnectionInterfaceTest, TestSendOnlyDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001944 FakeConstraints constraints;
1945 constraints.SetAllowRtpDataChannels();
1946 CreatePeerConnection(&constraints);
zhihuang9763d562016-08-05 11:14:50 -07001947 rtc::scoped_refptr<DataChannelInterface> data1 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001948 pc_->CreateDataChannel("test1", NULL);
kwibergd1fe2812016-04-27 06:47:29 -07001949 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001950 new MockDataChannelObserver(data1));
1951
1952 CreateOfferReceiveAnswerWithoutSsrc();
1953
1954 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1955
1956 data1->Close();
1957 EXPECT_EQ(DataChannelInterface::kClosing, data1->state());
1958 CreateOfferReceiveAnswerWithoutSsrc();
1959 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
1960 EXPECT_FALSE(observer1->IsOpen());
1961}
1962
1963// This test that if a data channel is added in an answer a receive only channel
1964// channel is created.
1965TEST_F(PeerConnectionInterfaceTest, TestReceiveOnlyDataChannel) {
1966 FakeConstraints constraints;
1967 constraints.SetAllowRtpDataChannels();
1968 CreatePeerConnection(&constraints);
1969
1970 std::string offer_label = "offer_channel";
zhihuang9763d562016-08-05 11:14:50 -07001971 rtc::scoped_refptr<DataChannelInterface> offer_channel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001972 pc_->CreateDataChannel(offer_label, NULL);
1973
1974 CreateOfferAsLocalDescription();
1975
1976 // Replace the data channel label in the offer and apply it as an answer.
1977 std::string receive_label = "answer_channel";
1978 std::string sdp;
1979 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001980 rtc::replace_substrs(offer_label.c_str(), offer_label.length(),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001981 receive_label.c_str(), receive_label.length(),
1982 &sdp);
1983 CreateAnswerAsRemoteDescription(sdp);
1984
1985 // Verify that a new incoming data channel has been created and that
1986 // it is open but can't we written to.
1987 ASSERT_TRUE(observer_.last_datachannel_ != NULL);
1988 DataChannelInterface* received_channel = observer_.last_datachannel_;
1989 EXPECT_EQ(DataChannelInterface::kConnecting, received_channel->state());
1990 EXPECT_EQ(receive_label, received_channel->label());
1991 EXPECT_FALSE(received_channel->Send(DataBuffer("something")));
1992
1993 // Verify that the channel we initially offered has been rejected.
1994 EXPECT_EQ(DataChannelInterface::kClosed, offer_channel->state());
1995
1996 // Do another offer / answer exchange and verify that the data channel is
1997 // opened.
1998 CreateOfferReceiveAnswer();
1999 EXPECT_EQ_WAIT(DataChannelInterface::kOpen, received_channel->state(),
2000 kTimeout);
2001}
2002
2003// This test that no data channel is returned if a reliable channel is
2004// requested.
2005// TODO(perkj): Remove this test once reliable channels are implemented.
2006TEST_F(PeerConnectionInterfaceTest, CreateReliableRtpDataChannelShouldFail) {
2007 FakeConstraints constraints;
2008 constraints.SetAllowRtpDataChannels();
2009 CreatePeerConnection(&constraints);
2010
2011 std::string label = "test";
2012 webrtc::DataChannelInit config;
2013 config.reliable = true;
zhihuang9763d562016-08-05 11:14:50 -07002014 rtc::scoped_refptr<DataChannelInterface> channel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002015 pc_->CreateDataChannel(label, &config);
2016 EXPECT_TRUE(channel == NULL);
2017}
2018
deadbeefab9b2d12015-10-14 11:33:11 -07002019// Verifies that duplicated label is not allowed for RTP data channel.
2020TEST_F(PeerConnectionInterfaceTest, RtpDuplicatedLabelNotAllowed) {
2021 FakeConstraints constraints;
2022 constraints.SetAllowRtpDataChannels();
2023 CreatePeerConnection(&constraints);
2024
2025 std::string label = "test";
zhihuang9763d562016-08-05 11:14:50 -07002026 rtc::scoped_refptr<DataChannelInterface> channel =
deadbeefab9b2d12015-10-14 11:33:11 -07002027 pc_->CreateDataChannel(label, nullptr);
2028 EXPECT_NE(channel, nullptr);
2029
zhihuang9763d562016-08-05 11:14:50 -07002030 rtc::scoped_refptr<DataChannelInterface> dup_channel =
deadbeefab9b2d12015-10-14 11:33:11 -07002031 pc_->CreateDataChannel(label, nullptr);
2032 EXPECT_EQ(dup_channel, nullptr);
2033}
2034
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002035// This tests that a SCTP data channel is returned using different
2036// DataChannelInit configurations.
2037TEST_F(PeerConnectionInterfaceTest, CreateSctpDataChannel) {
2038 FakeConstraints constraints;
2039 constraints.SetAllowDtlsSctpDataChannels();
2040 CreatePeerConnection(&constraints);
2041
2042 webrtc::DataChannelInit config;
2043
zhihuang9763d562016-08-05 11:14:50 -07002044 rtc::scoped_refptr<DataChannelInterface> channel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002045 pc_->CreateDataChannel("1", &config);
2046 EXPECT_TRUE(channel != NULL);
2047 EXPECT_TRUE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00002048 EXPECT_TRUE(observer_.renegotiation_needed_);
2049 observer_.renegotiation_needed_ = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002050
2051 config.ordered = false;
2052 channel = pc_->CreateDataChannel("2", &config);
2053 EXPECT_TRUE(channel != NULL);
2054 EXPECT_TRUE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00002055 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002056
2057 config.ordered = true;
2058 config.maxRetransmits = 0;
2059 channel = pc_->CreateDataChannel("3", &config);
2060 EXPECT_TRUE(channel != NULL);
2061 EXPECT_FALSE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00002062 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002063
2064 config.maxRetransmits = -1;
2065 config.maxRetransmitTime = 0;
2066 channel = pc_->CreateDataChannel("4", &config);
2067 EXPECT_TRUE(channel != NULL);
2068 EXPECT_FALSE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00002069 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002070}
2071
2072// This tests that no data channel is returned if both maxRetransmits and
2073// maxRetransmitTime are set for SCTP data channels.
2074TEST_F(PeerConnectionInterfaceTest,
2075 CreateSctpDataChannelShouldFailForInvalidConfig) {
2076 FakeConstraints constraints;
2077 constraints.SetAllowDtlsSctpDataChannels();
2078 CreatePeerConnection(&constraints);
2079
2080 std::string label = "test";
2081 webrtc::DataChannelInit config;
2082 config.maxRetransmits = 0;
2083 config.maxRetransmitTime = 0;
2084
zhihuang9763d562016-08-05 11:14:50 -07002085 rtc::scoped_refptr<DataChannelInterface> channel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002086 pc_->CreateDataChannel(label, &config);
2087 EXPECT_TRUE(channel == NULL);
2088}
2089
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002090// The test verifies that creating a SCTP data channel with an id already in use
2091// or out of range should fail.
2092TEST_F(PeerConnectionInterfaceTest,
2093 CreateSctpDataChannelWithInvalidIdShouldFail) {
2094 FakeConstraints constraints;
2095 constraints.SetAllowDtlsSctpDataChannels();
2096 CreatePeerConnection(&constraints);
2097
2098 webrtc::DataChannelInit config;
zhihuang9763d562016-08-05 11:14:50 -07002099 rtc::scoped_refptr<DataChannelInterface> channel;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002100
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00002101 config.id = 1;
2102 channel = pc_->CreateDataChannel("1", &config);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002103 EXPECT_TRUE(channel != NULL);
2104 EXPECT_EQ(1, channel->id());
2105
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002106 channel = pc_->CreateDataChannel("x", &config);
2107 EXPECT_TRUE(channel == NULL);
2108
2109 config.id = cricket::kMaxSctpSid;
2110 channel = pc_->CreateDataChannel("max", &config);
2111 EXPECT_TRUE(channel != NULL);
2112 EXPECT_EQ(config.id, channel->id());
2113
2114 config.id = cricket::kMaxSctpSid + 1;
2115 channel = pc_->CreateDataChannel("x", &config);
2116 EXPECT_TRUE(channel == NULL);
2117}
2118
deadbeefab9b2d12015-10-14 11:33:11 -07002119// Verifies that duplicated label is allowed for SCTP data channel.
2120TEST_F(PeerConnectionInterfaceTest, SctpDuplicatedLabelAllowed) {
2121 FakeConstraints constraints;
2122 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2123 true);
2124 CreatePeerConnection(&constraints);
2125
2126 std::string label = "test";
zhihuang9763d562016-08-05 11:14:50 -07002127 rtc::scoped_refptr<DataChannelInterface> channel =
deadbeefab9b2d12015-10-14 11:33:11 -07002128 pc_->CreateDataChannel(label, nullptr);
2129 EXPECT_NE(channel, nullptr);
2130
zhihuang9763d562016-08-05 11:14:50 -07002131 rtc::scoped_refptr<DataChannelInterface> dup_channel =
deadbeefab9b2d12015-10-14 11:33:11 -07002132 pc_->CreateDataChannel(label, nullptr);
2133 EXPECT_NE(dup_channel, nullptr);
2134}
2135
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00002136// This test verifies that OnRenegotiationNeeded is fired for every new RTP
2137// DataChannel.
2138TEST_F(PeerConnectionInterfaceTest, RenegotiationNeededForNewRtpDataChannel) {
2139 FakeConstraints constraints;
2140 constraints.SetAllowRtpDataChannels();
2141 CreatePeerConnection(&constraints);
2142
zhihuang9763d562016-08-05 11:14:50 -07002143 rtc::scoped_refptr<DataChannelInterface> dc1 =
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00002144 pc_->CreateDataChannel("test1", NULL);
2145 EXPECT_TRUE(observer_.renegotiation_needed_);
2146 observer_.renegotiation_needed_ = false;
2147
zhihuang9763d562016-08-05 11:14:50 -07002148 rtc::scoped_refptr<DataChannelInterface> dc2 =
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00002149 pc_->CreateDataChannel("test2", NULL);
2150 EXPECT_TRUE(observer_.renegotiation_needed_);
2151}
2152
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002153// This test that a data channel closes when a PeerConnection is deleted/closed.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002154TEST_F(PeerConnectionInterfaceTest, DataChannelCloseWhenPeerConnectionClose) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002155 FakeConstraints constraints;
2156 constraints.SetAllowRtpDataChannels();
2157 CreatePeerConnection(&constraints);
2158
zhihuang9763d562016-08-05 11:14:50 -07002159 rtc::scoped_refptr<DataChannelInterface> data1 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002160 pc_->CreateDataChannel("test1", NULL);
zhihuang9763d562016-08-05 11:14:50 -07002161 rtc::scoped_refptr<DataChannelInterface> data2 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002162 pc_->CreateDataChannel("test2", NULL);
2163 ASSERT_TRUE(data1 != NULL);
kwibergd1fe2812016-04-27 06:47:29 -07002164 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002165 new MockDataChannelObserver(data1));
kwibergd1fe2812016-04-27 06:47:29 -07002166 std::unique_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002167 new MockDataChannelObserver(data2));
2168
2169 CreateOfferReceiveAnswer();
2170 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
2171 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
2172
2173 ReleasePeerConnection();
2174 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
2175 EXPECT_EQ(DataChannelInterface::kClosed, data2->state());
2176}
2177
2178// This test that data channels can be rejected in an answer.
2179TEST_F(PeerConnectionInterfaceTest, TestRejectDataChannelInAnswer) {
2180 FakeConstraints constraints;
2181 constraints.SetAllowRtpDataChannels();
2182 CreatePeerConnection(&constraints);
2183
zhihuang9763d562016-08-05 11:14:50 -07002184 rtc::scoped_refptr<DataChannelInterface> offer_channel(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002185 pc_->CreateDataChannel("offer_channel", NULL));
2186
2187 CreateOfferAsLocalDescription();
2188
2189 // Create an answer where the m-line for data channels are rejected.
2190 std::string sdp;
2191 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07002192 std::unique_ptr<SessionDescriptionInterface> answer(
2193 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
2194 sdp, nullptr));
2195 ASSERT_TRUE(answer);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002196 cricket::ContentInfo* data_info =
2197 answer->description()->GetContentByName("data");
2198 data_info->rejected = true;
2199
Steve Antondb45ca82017-09-11 18:27:34 -07002200 DoSetRemoteDescription(std::move(answer));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002201 EXPECT_EQ(DataChannelInterface::kClosed, offer_channel->state());
2202}
2203
2204// Test that we can create a session description from an SDP string from
2205// FireFox, use it as a remote session description, generate an answer and use
2206// the answer as a local description.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002207TEST_F(PeerConnectionInterfaceTest, ReceiveFireFoxOffer) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002208 FakeConstraints constraints;
2209 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2210 true);
2211 CreatePeerConnection(&constraints);
2212 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
Steve Antondb45ca82017-09-11 18:27:34 -07002213 std::unique_ptr<SessionDescriptionInterface> desc(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002214 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
Steve Antondb45ca82017-09-11 18:27:34 -07002215 webrtc::kFireFoxSdpOffer, nullptr));
2216 EXPECT_TRUE(DoSetSessionDescription(std::move(desc), false));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002217 CreateAnswerAsLocalDescription();
2218 ASSERT_TRUE(pc_->local_description() != NULL);
2219 ASSERT_TRUE(pc_->remote_description() != NULL);
2220
2221 const cricket::ContentInfo* content =
2222 cricket::GetFirstAudioContent(pc_->local_description()->description());
2223 ASSERT_TRUE(content != NULL);
2224 EXPECT_FALSE(content->rejected);
2225
2226 content =
2227 cricket::GetFirstVideoContent(pc_->local_description()->description());
2228 ASSERT_TRUE(content != NULL);
2229 EXPECT_FALSE(content->rejected);
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00002230#ifdef HAVE_SCTP
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002231 content =
2232 cricket::GetFirstDataContent(pc_->local_description()->description());
2233 ASSERT_TRUE(content != NULL);
2234 EXPECT_TRUE(content->rejected);
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00002235#endif
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002236}
2237
zhihuangb19012e2017-09-19 13:47:59 -07002238// Test that fallback from DTLS to SDES is not supported.
2239// The fallback was previously supported but was removed to simplify the code
2240// and because it's non-standard.
2241TEST_F(PeerConnectionInterfaceTest, DtlsSdesFallbackNotSupported) {
deadbeef8662f942017-01-20 21:20:51 -08002242 FakeConstraints constraints;
2243 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2244 true);
2245 CreatePeerConnection(&constraints);
2246 // Wait for fake certificate to be generated. Previously, this is what caused
2247 // the "a=crypto" lines to be rejected.
2248 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
2249 ASSERT_NE(nullptr, fake_certificate_generator_);
2250 EXPECT_EQ_WAIT(1, fake_certificate_generator_->generated_certificates(),
2251 kTimeout);
Steve Antondb45ca82017-09-11 18:27:34 -07002252 std::unique_ptr<SessionDescriptionInterface> desc(
2253 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
2254 kDtlsSdesFallbackSdp, nullptr));
zhihuangb19012e2017-09-19 13:47:59 -07002255 EXPECT_FALSE(DoSetSessionDescription(std::move(desc), false));
deadbeef8662f942017-01-20 21:20:51 -08002256}
2257
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002258// Test that we can create an audio only offer and receive an answer with a
2259// limited set of audio codecs and receive an updated offer with more audio
2260// codecs, where the added codecs are not supported.
2261TEST_F(PeerConnectionInterfaceTest, ReceiveUpdatedAudioOfferWithBadCodecs) {
deadbeef293e9262017-01-11 12:28:30 -08002262 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002263 AddVoiceStream("audio_label");
2264 CreateOfferAsLocalDescription();
2265
Steve Antondb45ca82017-09-11 18:27:34 -07002266 std::unique_ptr<SessionDescriptionInterface> answer(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002267 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
Steve Antondb45ca82017-09-11 18:27:34 -07002268 webrtc::kAudioSdp, nullptr));
2269 EXPECT_TRUE(DoSetSessionDescription(std::move(answer), false));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002270
Steve Antondb45ca82017-09-11 18:27:34 -07002271 std::unique_ptr<SessionDescriptionInterface> updated_offer(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002272 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
jbauchfabe2c92015-07-16 13:43:14 -07002273 webrtc::kAudioSdpWithUnsupportedCodecs,
Steve Antondb45ca82017-09-11 18:27:34 -07002274 nullptr));
2275 EXPECT_TRUE(DoSetSessionDescription(std::move(updated_offer), false));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002276 CreateAnswerAsLocalDescription();
2277}
2278
deadbeefc80741f2015-10-22 13:14:45 -07002279// Test that if we're receiving (but not sending) a track, subsequent offers
2280// will have m-lines with a=recvonly.
2281TEST_F(PeerConnectionInterfaceTest, CreateSubsequentRecvOnlyOffer) {
2282 FakeConstraints constraints;
2283 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2284 true);
2285 CreatePeerConnection(&constraints);
2286 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2287 CreateAnswerAsLocalDescription();
2288
2289 // At this point we should be receiving stream 1, but not sending anything.
2290 // A new offer should be recvonly.
kwibergd1fe2812016-04-27 06:47:29 -07002291 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefc80741f2015-10-22 13:14:45 -07002292 DoCreateOffer(&offer, nullptr);
2293
2294 const cricket::ContentInfo* video_content =
2295 cricket::GetFirstVideoContent(offer->description());
2296 const cricket::VideoContentDescription* video_desc =
2297 static_cast<const cricket::VideoContentDescription*>(
2298 video_content->description);
2299 ASSERT_EQ(cricket::MD_RECVONLY, video_desc->direction());
2300
2301 const cricket::ContentInfo* audio_content =
2302 cricket::GetFirstAudioContent(offer->description());
2303 const cricket::AudioContentDescription* audio_desc =
2304 static_cast<const cricket::AudioContentDescription*>(
2305 audio_content->description);
2306 ASSERT_EQ(cricket::MD_RECVONLY, audio_desc->direction());
2307}
2308
2309// Test that if we're receiving (but not sending) a track, and the
2310// offerToReceiveVideo/offerToReceiveAudio constraints are explicitly set to
2311// false, the generated m-lines will be a=inactive.
2312TEST_F(PeerConnectionInterfaceTest, CreateSubsequentInactiveOffer) {
2313 FakeConstraints constraints;
2314 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2315 true);
2316 CreatePeerConnection(&constraints);
2317 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2318 CreateAnswerAsLocalDescription();
2319
2320 // At this point we should be receiving stream 1, but not sending anything.
2321 // A new offer would be recvonly, but we'll set the "no receive" constraints
2322 // to make it inactive.
kwibergd1fe2812016-04-27 06:47:29 -07002323 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefc80741f2015-10-22 13:14:45 -07002324 FakeConstraints offer_constraints;
2325 offer_constraints.AddMandatory(
2326 webrtc::MediaConstraintsInterface::kOfferToReceiveVideo, false);
2327 offer_constraints.AddMandatory(
2328 webrtc::MediaConstraintsInterface::kOfferToReceiveAudio, false);
2329 DoCreateOffer(&offer, &offer_constraints);
2330
2331 const cricket::ContentInfo* video_content =
2332 cricket::GetFirstVideoContent(offer->description());
2333 const cricket::VideoContentDescription* video_desc =
2334 static_cast<const cricket::VideoContentDescription*>(
2335 video_content->description);
2336 ASSERT_EQ(cricket::MD_INACTIVE, video_desc->direction());
2337
2338 const cricket::ContentInfo* audio_content =
2339 cricket::GetFirstAudioContent(offer->description());
2340 const cricket::AudioContentDescription* audio_desc =
2341 static_cast<const cricket::AudioContentDescription*>(
2342 audio_content->description);
2343 ASSERT_EQ(cricket::MD_INACTIVE, audio_desc->direction());
2344}
2345
deadbeef653b8e02015-11-11 12:55:10 -08002346// Test that we can use SetConfiguration to change the ICE servers of the
2347// PortAllocator.
2348TEST_F(PeerConnectionInterfaceTest, SetConfigurationChangesIceServers) {
2349 CreatePeerConnection();
2350
2351 PeerConnectionInterface::RTCConfiguration config;
2352 PeerConnectionInterface::IceServer server;
2353 server.uri = "stun:test_hostname";
2354 config.servers.push_back(server);
2355 EXPECT_TRUE(pc_->SetConfiguration(config));
2356
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002357 EXPECT_EQ(1u, port_allocator_->stun_servers().size());
2358 EXPECT_EQ("test_hostname",
2359 port_allocator_->stun_servers().begin()->hostname());
deadbeef653b8e02015-11-11 12:55:10 -08002360}
2361
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002362TEST_F(PeerConnectionInterfaceTest, SetConfigurationChangesCandidateFilter) {
2363 CreatePeerConnection();
2364 PeerConnectionInterface::RTCConfiguration config;
2365 config.type = PeerConnectionInterface::kRelay;
2366 EXPECT_TRUE(pc_->SetConfiguration(config));
2367 EXPECT_EQ(cricket::CF_RELAY, port_allocator_->candidate_filter());
2368}
2369
deadbeef293e9262017-01-11 12:28:30 -08002370TEST_F(PeerConnectionInterfaceTest, SetConfigurationChangesPruneTurnPortsFlag) {
2371 PeerConnectionInterface::RTCConfiguration config;
2372 config.prune_turn_ports = false;
2373 CreatePeerConnection(config, nullptr);
2374 EXPECT_FALSE(port_allocator_->prune_turn_ports());
2375
2376 config.prune_turn_ports = true;
2377 EXPECT_TRUE(pc_->SetConfiguration(config));
2378 EXPECT_TRUE(port_allocator_->prune_turn_ports());
2379}
2380
skvladd1f5fda2017-02-03 16:54:05 -08002381// Test that the ice check interval can be changed. This does not verify that
2382// the setting makes it all the way to P2PTransportChannel, as that would
2383// require a very complex set of mocks.
2384TEST_F(PeerConnectionInterfaceTest, SetConfigurationChangesIceCheckInterval) {
2385 PeerConnectionInterface::RTCConfiguration config;
2386 config.ice_check_min_interval = rtc::Optional<int>();
2387 CreatePeerConnection(config, nullptr);
2388 config.ice_check_min_interval = rtc::Optional<int>(100);
2389 EXPECT_TRUE(pc_->SetConfiguration(config));
2390 PeerConnectionInterface::RTCConfiguration new_config =
2391 pc_->GetConfiguration();
2392 EXPECT_EQ(new_config.ice_check_min_interval, rtc::Optional<int>(100));
2393}
2394
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002395// Test that when SetConfiguration changes both the pool size and other
2396// attributes, the pooled session is created with the updated attributes.
2397TEST_F(PeerConnectionInterfaceTest,
2398 SetConfigurationCreatesPooledSessionCorrectly) {
2399 CreatePeerConnection();
2400 PeerConnectionInterface::RTCConfiguration config;
2401 config.ice_candidate_pool_size = 1;
2402 PeerConnectionInterface::IceServer server;
2403 server.uri = kStunAddressOnly;
2404 config.servers.push_back(server);
2405 config.type = PeerConnectionInterface::kRelay;
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07002406 EXPECT_TRUE(pc_->SetConfiguration(config));
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002407
2408 const cricket::FakePortAllocatorSession* session =
2409 static_cast<const cricket::FakePortAllocatorSession*>(
2410 port_allocator_->GetPooledSession());
2411 ASSERT_NE(nullptr, session);
2412 EXPECT_EQ(1UL, session->stun_servers().size());
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002413}
2414
deadbeef293e9262017-01-11 12:28:30 -08002415// Test that after SetLocalDescription, changing the pool size is not allowed,
2416// and an invalid modification error is returned.
deadbeef6de92f92016-12-12 18:49:32 -08002417TEST_F(PeerConnectionInterfaceTest,
2418 CantChangePoolSizeAfterSetLocalDescription) {
2419 CreatePeerConnection();
2420 // Start by setting a size of 1.
2421 PeerConnectionInterface::RTCConfiguration config;
2422 config.ice_candidate_pool_size = 1;
2423 EXPECT_TRUE(pc_->SetConfiguration(config));
2424
2425 // Set remote offer; can still change pool size at this point.
2426 CreateOfferAsRemoteDescription();
2427 config.ice_candidate_pool_size = 2;
2428 EXPECT_TRUE(pc_->SetConfiguration(config));
2429
2430 // Set local answer; now it's too late.
2431 CreateAnswerAsLocalDescription();
2432 config.ice_candidate_pool_size = 3;
deadbeef293e9262017-01-11 12:28:30 -08002433 RTCError error;
2434 EXPECT_FALSE(pc_->SetConfiguration(config, &error));
2435 EXPECT_EQ(RTCErrorType::INVALID_MODIFICATION, error.type());
2436}
2437
deadbeef42a42632017-03-10 15:18:00 -08002438// Test that after setting an answer, extra pooled sessions are discarded. The
2439// ICE candidate pool is only intended to be used for the first offer/answer.
2440TEST_F(PeerConnectionInterfaceTest,
2441 ExtraPooledSessionsDiscardedAfterApplyingAnswer) {
2442 CreatePeerConnection();
2443
2444 // Set a larger-than-necessary size.
2445 PeerConnectionInterface::RTCConfiguration config;
2446 config.ice_candidate_pool_size = 4;
2447 EXPECT_TRUE(pc_->SetConfiguration(config));
2448
2449 // Do offer/answer.
2450 CreateOfferAsRemoteDescription();
2451 CreateAnswerAsLocalDescription();
2452
2453 // Expect no pooled sessions to be left.
2454 const cricket::PortAllocatorSession* session =
2455 port_allocator_->GetPooledSession();
2456 EXPECT_EQ(nullptr, session);
2457}
2458
2459// After Close is called, pooled candidates should be discarded so as to not
2460// waste network resources.
2461TEST_F(PeerConnectionInterfaceTest, PooledSessionsDiscardedAfterClose) {
2462 CreatePeerConnection();
2463
2464 PeerConnectionInterface::RTCConfiguration config;
2465 config.ice_candidate_pool_size = 3;
2466 EXPECT_TRUE(pc_->SetConfiguration(config));
2467 pc_->Close();
2468
2469 // Expect no pooled sessions to be left.
2470 const cricket::PortAllocatorSession* session =
2471 port_allocator_->GetPooledSession();
2472 EXPECT_EQ(nullptr, session);
2473}
2474
deadbeef293e9262017-01-11 12:28:30 -08002475// Test that SetConfiguration returns an invalid modification error if
2476// modifying a field in the configuration that isn't allowed to be modified.
2477TEST_F(PeerConnectionInterfaceTest,
2478 SetConfigurationReturnsInvalidModificationError) {
2479 PeerConnectionInterface::RTCConfiguration config;
2480 config.bundle_policy = PeerConnectionInterface::kBundlePolicyBalanced;
2481 config.rtcp_mux_policy = PeerConnectionInterface::kRtcpMuxPolicyNegotiate;
2482 config.continual_gathering_policy = PeerConnectionInterface::GATHER_ONCE;
2483 CreatePeerConnection(config, nullptr);
2484
2485 PeerConnectionInterface::RTCConfiguration modified_config = config;
2486 modified_config.bundle_policy =
2487 PeerConnectionInterface::kBundlePolicyMaxBundle;
2488 RTCError error;
2489 EXPECT_FALSE(pc_->SetConfiguration(modified_config, &error));
2490 EXPECT_EQ(RTCErrorType::INVALID_MODIFICATION, error.type());
2491
2492 modified_config = config;
2493 modified_config.rtcp_mux_policy =
2494 PeerConnectionInterface::kRtcpMuxPolicyRequire;
2495 error.set_type(RTCErrorType::NONE);
2496 EXPECT_FALSE(pc_->SetConfiguration(modified_config, &error));
2497 EXPECT_EQ(RTCErrorType::INVALID_MODIFICATION, error.type());
2498
2499 modified_config = config;
2500 modified_config.continual_gathering_policy =
2501 PeerConnectionInterface::GATHER_CONTINUALLY;
2502 error.set_type(RTCErrorType::NONE);
2503 EXPECT_FALSE(pc_->SetConfiguration(modified_config, &error));
2504 EXPECT_EQ(RTCErrorType::INVALID_MODIFICATION, error.type());
2505}
2506
2507// Test that SetConfiguration returns a range error if the candidate pool size
2508// is negative or larger than allowed by the spec.
2509TEST_F(PeerConnectionInterfaceTest,
2510 SetConfigurationReturnsRangeErrorForBadCandidatePoolSize) {
2511 PeerConnectionInterface::RTCConfiguration config;
2512 CreatePeerConnection(config, nullptr);
2513
2514 config.ice_candidate_pool_size = -1;
2515 RTCError error;
2516 EXPECT_FALSE(pc_->SetConfiguration(config, &error));
2517 EXPECT_EQ(RTCErrorType::INVALID_RANGE, error.type());
2518
2519 config.ice_candidate_pool_size = INT_MAX;
2520 error.set_type(RTCErrorType::NONE);
2521 EXPECT_FALSE(pc_->SetConfiguration(config, &error));
2522 EXPECT_EQ(RTCErrorType::INVALID_RANGE, error.type());
2523}
2524
2525// Test that SetConfiguration returns a syntax error if parsing an ICE server
2526// URL failed.
2527TEST_F(PeerConnectionInterfaceTest,
2528 SetConfigurationReturnsSyntaxErrorFromBadIceUrls) {
2529 PeerConnectionInterface::RTCConfiguration config;
2530 CreatePeerConnection(config, nullptr);
2531
2532 PeerConnectionInterface::IceServer bad_server;
2533 bad_server.uri = "stunn:www.example.com";
2534 config.servers.push_back(bad_server);
2535 RTCError error;
2536 EXPECT_FALSE(pc_->SetConfiguration(config, &error));
2537 EXPECT_EQ(RTCErrorType::SYNTAX_ERROR, error.type());
2538}
2539
2540// Test that SetConfiguration returns an invalid parameter error if a TURN
2541// IceServer is missing a username or password.
2542TEST_F(PeerConnectionInterfaceTest,
2543 SetConfigurationReturnsInvalidParameterIfCredentialsMissing) {
2544 PeerConnectionInterface::RTCConfiguration config;
2545 CreatePeerConnection(config, nullptr);
2546
2547 PeerConnectionInterface::IceServer bad_server;
2548 bad_server.uri = "turn:www.example.com";
2549 // Missing password.
2550 bad_server.username = "foo";
2551 config.servers.push_back(bad_server);
2552 RTCError error;
2553 EXPECT_FALSE(pc_->SetConfiguration(config, &error));
2554 EXPECT_EQ(RTCErrorType::INVALID_PARAMETER, error.type());
deadbeef6de92f92016-12-12 18:49:32 -08002555}
2556
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002557// Test that PeerConnection::Close changes the states to closed and all remote
2558// tracks change state to ended.
2559TEST_F(PeerConnectionInterfaceTest, CloseAndTestStreamsAndStates) {
2560 // Initialize a PeerConnection and negotiate local and remote session
2561 // description.
2562 InitiateCall();
2563 ASSERT_EQ(1u, pc_->local_streams()->count());
2564 ASSERT_EQ(1u, pc_->remote_streams()->count());
2565
2566 pc_->Close();
2567
2568 EXPECT_EQ(PeerConnectionInterface::kClosed, pc_->signaling_state());
2569 EXPECT_EQ(PeerConnectionInterface::kIceConnectionClosed,
2570 pc_->ice_connection_state());
2571 EXPECT_EQ(PeerConnectionInterface::kIceGatheringComplete,
2572 pc_->ice_gathering_state());
2573
2574 EXPECT_EQ(1u, pc_->local_streams()->count());
2575 EXPECT_EQ(1u, pc_->remote_streams()->count());
2576
zhihuang9763d562016-08-05 11:14:50 -07002577 rtc::scoped_refptr<MediaStreamInterface> remote_stream =
2578 pc_->remote_streams()->at(0);
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002579 // Track state may be updated asynchronously.
perkjd61bf802016-03-24 03:16:19 -07002580 EXPECT_EQ_WAIT(MediaStreamTrackInterface::kEnded,
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002581 remote_stream->GetAudioTracks()[0]->state(), kTimeout);
2582 EXPECT_EQ_WAIT(MediaStreamTrackInterface::kEnded,
2583 remote_stream->GetVideoTracks()[0]->state(), kTimeout);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002584}
2585
2586// Test that PeerConnection methods fails gracefully after
2587// PeerConnection::Close has been called.
2588TEST_F(PeerConnectionInterfaceTest, CloseAndTestMethods) {
deadbeef293e9262017-01-11 12:28:30 -08002589 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002590 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
2591 CreateOfferAsRemoteDescription();
2592 CreateAnswerAsLocalDescription();
2593
2594 ASSERT_EQ(1u, pc_->local_streams()->count());
zhihuang9763d562016-08-05 11:14:50 -07002595 rtc::scoped_refptr<MediaStreamInterface> local_stream =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002596 pc_->local_streams()->at(0);
2597
2598 pc_->Close();
2599
2600 pc_->RemoveStream(local_stream);
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +00002601 EXPECT_FALSE(pc_->AddStream(local_stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002602
2603 ASSERT_FALSE(local_stream->GetAudioTracks().empty());
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002604 rtc::scoped_refptr<webrtc::DtmfSenderInterface> dtmf_sender(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002605 pc_->CreateDtmfSender(local_stream->GetAudioTracks()[0]));
wu@webrtc.org66037362013-08-13 00:09:35 +00002606 EXPECT_TRUE(NULL == dtmf_sender); // local stream has been removed.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002607
2608 EXPECT_TRUE(pc_->CreateDataChannel("test", NULL) == NULL);
2609
2610 EXPECT_TRUE(pc_->local_description() != NULL);
2611 EXPECT_TRUE(pc_->remote_description() != NULL);
2612
kwibergd1fe2812016-04-27 06:47:29 -07002613 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07002614 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
kwibergd1fe2812016-04-27 06:47:29 -07002615 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -07002616 EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002617
2618 std::string sdp;
2619 ASSERT_TRUE(pc_->remote_description()->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07002620 std::unique_ptr<SessionDescriptionInterface> remote_offer(
2621 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer, sdp,
2622 nullptr));
2623 EXPECT_FALSE(DoSetRemoteDescription(std::move(remote_offer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002624
2625 ASSERT_TRUE(pc_->local_description()->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07002626 std::unique_ptr<SessionDescriptionInterface> local_offer(
2627 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer, sdp,
2628 nullptr));
2629 EXPECT_FALSE(DoSetLocalDescription(std::move(local_offer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002630}
2631
2632// Test that GetStats can still be called after PeerConnection::Close.
2633TEST_F(PeerConnectionInterfaceTest, CloseAndGetStats) {
2634 InitiateCall();
2635 pc_->Close();
2636 DoGetStats(NULL);
2637}
deadbeefab9b2d12015-10-14 11:33:11 -07002638
2639// NOTE: The series of tests below come from what used to be
2640// mediastreamsignaling_unittest.cc, and are mostly aimed at testing that
2641// setting a remote or local description has the expected effects.
2642
2643// This test verifies that the remote MediaStreams corresponding to a received
2644// SDP string is created. In this test the two separate MediaStreams are
2645// signaled.
2646TEST_F(PeerConnectionInterfaceTest, UpdateRemoteStreams) {
2647 FakeConstraints constraints;
2648 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2649 true);
2650 CreatePeerConnection(&constraints);
2651 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2652
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002653 rtc::scoped_refptr<StreamCollection> reference(CreateStreamCollection(1, 1));
deadbeefab9b2d12015-10-14 11:33:11 -07002654 EXPECT_TRUE(
2655 CompareStreamCollections(observer_.remote_streams(), reference.get()));
2656 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2657 EXPECT_TRUE(remote_stream->GetVideoTracks()[0]->GetSource() != nullptr);
2658
2659 // Create a session description based on another SDP with another
2660 // MediaStream.
2661 CreateAndSetRemoteOffer(kSdpStringWithStream1And2);
2662
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002663 rtc::scoped_refptr<StreamCollection> reference2(CreateStreamCollection(2, 1));
deadbeefab9b2d12015-10-14 11:33:11 -07002664 EXPECT_TRUE(
2665 CompareStreamCollections(observer_.remote_streams(), reference2.get()));
2666}
2667
2668// This test verifies that when remote tracks are added/removed from SDP, the
2669// created remote streams are updated appropriately.
2670TEST_F(PeerConnectionInterfaceTest,
2671 AddRemoveTrackFromExistingRemoteMediaStream) {
2672 FakeConstraints constraints;
2673 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2674 true);
2675 CreatePeerConnection(&constraints);
kwibergd1fe2812016-04-27 06:47:29 -07002676 std::unique_ptr<SessionDescriptionInterface> desc_ms1 =
kwiberg2bbff992016-03-16 11:03:04 -07002677 CreateSessionDescriptionAndReference(1, 1);
Steve Antondb45ca82017-09-11 18:27:34 -07002678 EXPECT_TRUE(DoSetRemoteDescription(std::move(desc_ms1)));
deadbeefab9b2d12015-10-14 11:33:11 -07002679 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
2680 reference_collection_));
2681
2682 // Add extra audio and video tracks to the same MediaStream.
kwibergd1fe2812016-04-27 06:47:29 -07002683 std::unique_ptr<SessionDescriptionInterface> desc_ms1_two_tracks =
kwiberg2bbff992016-03-16 11:03:04 -07002684 CreateSessionDescriptionAndReference(2, 2);
Steve Antondb45ca82017-09-11 18:27:34 -07002685 EXPECT_TRUE(DoSetRemoteDescription(std::move(desc_ms1_two_tracks)));
deadbeefab9b2d12015-10-14 11:33:11 -07002686 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
2687 reference_collection_));
zhihuang9763d562016-08-05 11:14:50 -07002688 rtc::scoped_refptr<AudioTrackInterface> audio_track2 =
perkjd61bf802016-03-24 03:16:19 -07002689 observer_.remote_streams()->at(0)->GetAudioTracks()[1];
2690 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, audio_track2->state());
zhihuang9763d562016-08-05 11:14:50 -07002691 rtc::scoped_refptr<VideoTrackInterface> video_track2 =
perkjd61bf802016-03-24 03:16:19 -07002692 observer_.remote_streams()->at(0)->GetVideoTracks()[1];
2693 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, video_track2->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002694
2695 // Remove the extra audio and video tracks.
kwibergd1fe2812016-04-27 06:47:29 -07002696 std::unique_ptr<SessionDescriptionInterface> desc_ms2 =
kwiberg2bbff992016-03-16 11:03:04 -07002697 CreateSessionDescriptionAndReference(1, 1);
perkjd61bf802016-03-24 03:16:19 -07002698 MockTrackObserver audio_track_observer(audio_track2);
2699 MockTrackObserver video_track_observer(video_track2);
2700
2701 EXPECT_CALL(audio_track_observer, OnChanged()).Times(Exactly(1));
2702 EXPECT_CALL(video_track_observer, OnChanged()).Times(Exactly(1));
Steve Antondb45ca82017-09-11 18:27:34 -07002703 EXPECT_TRUE(DoSetRemoteDescription(std::move(desc_ms2)));
deadbeefab9b2d12015-10-14 11:33:11 -07002704 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
2705 reference_collection_));
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002706 // Track state may be updated asynchronously.
perkjd61bf802016-03-24 03:16:19 -07002707 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002708 audio_track2->state(), kTimeout);
2709 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
2710 video_track2->state(), kTimeout);
deadbeefab9b2d12015-10-14 11:33:11 -07002711}
2712
2713// This tests that remote tracks are ended if a local session description is set
2714// that rejects the media content type.
2715TEST_F(PeerConnectionInterfaceTest, RejectMediaContent) {
2716 FakeConstraints constraints;
2717 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2718 true);
2719 CreatePeerConnection(&constraints);
2720 // First create and set a remote offer, then reject its video content in our
2721 // answer.
2722 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2723 ASSERT_EQ(1u, observer_.remote_streams()->count());
2724 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2725 ASSERT_EQ(1u, remote_stream->GetVideoTracks().size());
2726 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2727
2728 rtc::scoped_refptr<webrtc::VideoTrackInterface> remote_video =
2729 remote_stream->GetVideoTracks()[0];
2730 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, remote_video->state());
2731 rtc::scoped_refptr<webrtc::AudioTrackInterface> remote_audio =
2732 remote_stream->GetAudioTracks()[0];
2733 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, remote_audio->state());
2734
kwibergd1fe2812016-04-27 06:47:29 -07002735 std::unique_ptr<SessionDescriptionInterface> local_answer;
kwiberg2bbff992016-03-16 11:03:04 -07002736 EXPECT_TRUE(DoCreateAnswer(&local_answer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07002737 cricket::ContentInfo* video_info =
2738 local_answer->description()->GetContentByName("video");
2739 video_info->rejected = true;
Steve Antondb45ca82017-09-11 18:27:34 -07002740 EXPECT_TRUE(DoSetLocalDescription(std::move(local_answer)));
deadbeefab9b2d12015-10-14 11:33:11 -07002741 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kEnded, remote_video->state());
2742 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, remote_audio->state());
2743
2744 // Now create an offer where we reject both video and audio.
kwibergd1fe2812016-04-27 06:47:29 -07002745 std::unique_ptr<SessionDescriptionInterface> local_offer;
kwiberg2bbff992016-03-16 11:03:04 -07002746 EXPECT_TRUE(DoCreateOffer(&local_offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07002747 video_info = local_offer->description()->GetContentByName("video");
2748 ASSERT_TRUE(video_info != nullptr);
2749 video_info->rejected = true;
2750 cricket::ContentInfo* audio_info =
2751 local_offer->description()->GetContentByName("audio");
2752 ASSERT_TRUE(audio_info != nullptr);
2753 audio_info->rejected = true;
Steve Antondb45ca82017-09-11 18:27:34 -07002754 EXPECT_TRUE(DoSetLocalDescription(std::move(local_offer)));
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002755 // Track state may be updated asynchronously.
perkjd61bf802016-03-24 03:16:19 -07002756 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002757 remote_audio->state(), kTimeout);
2758 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
2759 remote_video->state(), kTimeout);
deadbeefab9b2d12015-10-14 11:33:11 -07002760}
2761
2762// This tests that we won't crash if the remote track has been removed outside
2763// of PeerConnection and then PeerConnection tries to reject the track.
2764TEST_F(PeerConnectionInterfaceTest, RemoveTrackThenRejectMediaContent) {
2765 FakeConstraints constraints;
2766 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2767 true);
2768 CreatePeerConnection(&constraints);
2769 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2770 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2771 remote_stream->RemoveTrack(remote_stream->GetVideoTracks()[0]);
2772 remote_stream->RemoveTrack(remote_stream->GetAudioTracks()[0]);
2773
kwibergd1fe2812016-04-27 06:47:29 -07002774 std::unique_ptr<SessionDescriptionInterface> local_answer(
deadbeefab9b2d12015-10-14 11:33:11 -07002775 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
2776 kSdpStringWithStream1, nullptr));
2777 cricket::ContentInfo* video_info =
2778 local_answer->description()->GetContentByName("video");
2779 video_info->rejected = true;
2780 cricket::ContentInfo* audio_info =
2781 local_answer->description()->GetContentByName("audio");
2782 audio_info->rejected = true;
Steve Antondb45ca82017-09-11 18:27:34 -07002783 EXPECT_TRUE(DoSetLocalDescription(std::move(local_answer)));
deadbeefab9b2d12015-10-14 11:33:11 -07002784
2785 // No crash is a pass.
2786}
2787
deadbeef5e97fb52015-10-15 12:49:08 -07002788// This tests that if a recvonly remote description is set, no remote streams
2789// will be created, even if the description contains SSRCs/MSIDs.
2790// See: https://code.google.com/p/webrtc/issues/detail?id=5054
2791TEST_F(PeerConnectionInterfaceTest, RecvonlyDescriptionDoesntCreateStream) {
2792 FakeConstraints constraints;
2793 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2794 true);
2795 CreatePeerConnection(&constraints);
2796
2797 std::string recvonly_offer = kSdpStringWithStream1;
2798 rtc::replace_substrs(kSendrecv, strlen(kSendrecv), kRecvonly,
2799 strlen(kRecvonly), &recvonly_offer);
2800 CreateAndSetRemoteOffer(recvonly_offer);
2801
2802 EXPECT_EQ(0u, observer_.remote_streams()->count());
2803}
2804
deadbeefab9b2d12015-10-14 11:33:11 -07002805// This tests that a default MediaStream is created if a remote session
2806// description doesn't contain any streams and no MSID support.
2807// It also tests that the default stream is updated if a video m-line is added
2808// in a subsequent session description.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002809TEST_F(PeerConnectionInterfaceTest, SdpWithoutMsidCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002810 FakeConstraints constraints;
2811 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2812 true);
2813 CreatePeerConnection(&constraints);
2814 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2815
2816 ASSERT_EQ(1u, observer_.remote_streams()->count());
2817 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2818
2819 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2820 EXPECT_EQ(0u, remote_stream->GetVideoTracks().size());
2821 EXPECT_EQ("default", remote_stream->label());
2822
2823 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2824 ASSERT_EQ(1u, observer_.remote_streams()->count());
2825 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2826 EXPECT_EQ("defaulta0", remote_stream->GetAudioTracks()[0]->id());
deadbeef884f5852016-01-15 09:20:04 -08002827 EXPECT_EQ(MediaStreamTrackInterface::kLive,
2828 remote_stream->GetAudioTracks()[0]->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002829 ASSERT_EQ(1u, remote_stream->GetVideoTracks().size());
2830 EXPECT_EQ("defaultv0", remote_stream->GetVideoTracks()[0]->id());
deadbeef884f5852016-01-15 09:20:04 -08002831 EXPECT_EQ(MediaStreamTrackInterface::kLive,
2832 remote_stream->GetVideoTracks()[0]->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002833}
2834
2835// This tests that a default MediaStream is created if a remote session
2836// description doesn't contain any streams and media direction is send only.
2837TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002838 SendOnlySdpWithoutMsidCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002839 FakeConstraints constraints;
2840 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2841 true);
2842 CreatePeerConnection(&constraints);
2843 CreateAndSetRemoteOffer(kSdpStringSendOnlyWithoutStreams);
2844
2845 ASSERT_EQ(1u, observer_.remote_streams()->count());
2846 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2847
2848 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2849 EXPECT_EQ(1u, remote_stream->GetVideoTracks().size());
2850 EXPECT_EQ("default", remote_stream->label());
2851}
2852
2853// This tests that it won't crash when PeerConnection tries to remove
2854// a remote track that as already been removed from the MediaStream.
2855TEST_F(PeerConnectionInterfaceTest, RemoveAlreadyGoneRemoteStream) {
2856 FakeConstraints constraints;
2857 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2858 true);
2859 CreatePeerConnection(&constraints);
2860 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2861 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2862 remote_stream->RemoveTrack(remote_stream->GetAudioTracks()[0]);
2863 remote_stream->RemoveTrack(remote_stream->GetVideoTracks()[0]);
2864
2865 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2866
2867 // No crash is a pass.
2868}
2869
2870// This tests that a default MediaStream is created if the remote session
2871// description doesn't contain any streams and don't contain an indication if
2872// MSID is supported.
2873TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002874 SdpWithoutMsidAndStreamsCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002875 FakeConstraints constraints;
2876 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2877 true);
2878 CreatePeerConnection(&constraints);
2879 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2880
2881 ASSERT_EQ(1u, observer_.remote_streams()->count());
2882 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2883 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2884 EXPECT_EQ(1u, remote_stream->GetVideoTracks().size());
2885}
2886
2887// This tests that a default MediaStream is not created if the remote session
2888// description doesn't contain any streams but does support MSID.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002889TEST_F(PeerConnectionInterfaceTest, SdpWithMsidDontCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002890 FakeConstraints constraints;
2891 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2892 true);
2893 CreatePeerConnection(&constraints);
2894 CreateAndSetRemoteOffer(kSdpStringWithMsidWithoutStreams);
2895 EXPECT_EQ(0u, observer_.remote_streams()->count());
2896}
2897
deadbeefbda7e0b2015-12-08 17:13:40 -08002898// This tests that when setting a new description, the old default tracks are
2899// not destroyed and recreated.
2900// See: https://bugs.chromium.org/p/webrtc/issues/detail?id=5250
Stefan Holmer102362b2016-03-18 09:39:07 +01002901TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002902 DefaultTracksNotDestroyedAndRecreated) {
deadbeefbda7e0b2015-12-08 17:13:40 -08002903 FakeConstraints constraints;
2904 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2905 true);
2906 CreatePeerConnection(&constraints);
2907 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2908
2909 ASSERT_EQ(1u, observer_.remote_streams()->count());
2910 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2911 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2912
2913 // Set the track to "disabled", then set a new description and ensure the
2914 // track is still disabled, which ensures it hasn't been recreated.
2915 remote_stream->GetAudioTracks()[0]->set_enabled(false);
2916 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2917 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2918 EXPECT_FALSE(remote_stream->GetAudioTracks()[0]->enabled());
2919}
2920
deadbeefab9b2d12015-10-14 11:33:11 -07002921// This tests that a default MediaStream is not created if a remote session
2922// description is updated to not have any MediaStreams.
2923TEST_F(PeerConnectionInterfaceTest, VerifyDefaultStreamIsNotCreated) {
2924 FakeConstraints constraints;
2925 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2926 true);
2927 CreatePeerConnection(&constraints);
2928 CreateAndSetRemoteOffer(kSdpStringWithStream1);
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002929 rtc::scoped_refptr<StreamCollection> reference(CreateStreamCollection(1, 1));
deadbeefab9b2d12015-10-14 11:33:11 -07002930 EXPECT_TRUE(
2931 CompareStreamCollections(observer_.remote_streams(), reference.get()));
2932
2933 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2934 EXPECT_EQ(0u, observer_.remote_streams()->count());
2935}
2936
2937// This tests that an RtpSender is created when the local description is set
2938// after adding a local stream.
2939// TODO(deadbeef): This test and the one below it need to be updated when
2940// an RtpSender's lifetime isn't determined by when a local description is set.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002941TEST_F(PeerConnectionInterfaceTest, LocalDescriptionChanged) {
deadbeefab9b2d12015-10-14 11:33:11 -07002942 FakeConstraints constraints;
2943 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2944 true);
2945 CreatePeerConnection(&constraints);
deadbeefab9b2d12015-10-14 11:33:11 -07002946
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002947 // Create an offer with 1 stream with 2 tracks of each type.
2948 rtc::scoped_refptr<StreamCollection> stream_collection =
2949 CreateStreamCollection(1, 2);
2950 pc_->AddStream(stream_collection->at(0));
2951 std::unique_ptr<SessionDescriptionInterface> offer;
2952 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07002953 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07002954
deadbeefab9b2d12015-10-14 11:33:11 -07002955 auto senders = pc_->GetSenders();
2956 EXPECT_EQ(4u, senders.size());
2957 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2958 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2959 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[1]));
2960 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[1]));
2961
2962 // Remove an audio and video track.
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002963 pc_->RemoveStream(stream_collection->at(0));
2964 stream_collection = CreateStreamCollection(1, 1);
2965 pc_->AddStream(stream_collection->at(0));
2966 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07002967 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002968
deadbeefab9b2d12015-10-14 11:33:11 -07002969 senders = pc_->GetSenders();
2970 EXPECT_EQ(2u, senders.size());
2971 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2972 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2973 EXPECT_FALSE(ContainsSender(senders, kAudioTracks[1]));
2974 EXPECT_FALSE(ContainsSender(senders, kVideoTracks[1]));
2975}
2976
2977// This tests that an RtpSender is created when the local description is set
2978// before adding a local stream.
2979TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002980 AddLocalStreamAfterLocalDescriptionChanged) {
deadbeefab9b2d12015-10-14 11:33:11 -07002981 FakeConstraints constraints;
2982 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2983 true);
2984 CreatePeerConnection(&constraints);
deadbeefab9b2d12015-10-14 11:33:11 -07002985
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002986 rtc::scoped_refptr<StreamCollection> stream_collection =
2987 CreateStreamCollection(1, 2);
2988 // Add a stream to create the offer, but remove it afterwards.
2989 pc_->AddStream(stream_collection->at(0));
2990 std::unique_ptr<SessionDescriptionInterface> offer;
2991 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2992 pc_->RemoveStream(stream_collection->at(0));
deadbeefab9b2d12015-10-14 11:33:11 -07002993
Steve Antondb45ca82017-09-11 18:27:34 -07002994 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07002995 auto senders = pc_->GetSenders();
2996 EXPECT_EQ(0u, senders.size());
2997
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002998 pc_->AddStream(stream_collection->at(0));
deadbeefab9b2d12015-10-14 11:33:11 -07002999 senders = pc_->GetSenders();
3000 EXPECT_EQ(4u, senders.size());
3001 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
3002 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
3003 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[1]));
3004 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[1]));
3005}
3006
3007// This tests that the expected behavior occurs if the SSRC on a local track is
3008// changed when SetLocalDescription is called.
3009TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07003010 ChangeSsrcOnTrackInLocalSessionDescription) {
deadbeefab9b2d12015-10-14 11:33:11 -07003011 FakeConstraints constraints;
3012 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
3013 true);
3014 CreatePeerConnection(&constraints);
deadbeefab9b2d12015-10-14 11:33:11 -07003015
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003016 rtc::scoped_refptr<StreamCollection> stream_collection =
3017 CreateStreamCollection(2, 1);
3018 pc_->AddStream(stream_collection->at(0));
3019 std::unique_ptr<SessionDescriptionInterface> offer;
3020 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
3021 // Grab a copy of the offer before it gets passed into the PC.
Steve Antondb45ca82017-09-11 18:27:34 -07003022 std::unique_ptr<webrtc::JsepSessionDescription> modified_offer(
3023 new webrtc::JsepSessionDescription(SessionDescriptionInterface::kOffer));
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003024 modified_offer->Initialize(offer->description()->Copy(), offer->session_id(),
3025 offer->session_version());
Steve Antondb45ca82017-09-11 18:27:34 -07003026 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07003027
deadbeefab9b2d12015-10-14 11:33:11 -07003028 auto senders = pc_->GetSenders();
3029 EXPECT_EQ(2u, senders.size());
3030 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
3031 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
3032
3033 // Change the ssrc of the audio and video track.
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003034 cricket::MediaContentDescription* desc =
3035 cricket::GetFirstAudioContentDescription(modified_offer->description());
3036 ASSERT_TRUE(desc != NULL);
3037 for (StreamParams& stream : desc->mutable_streams()) {
3038 for (unsigned int& ssrc : stream.ssrcs) {
3039 ++ssrc;
3040 }
3041 }
deadbeefab9b2d12015-10-14 11:33:11 -07003042
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003043 desc =
3044 cricket::GetFirstVideoContentDescription(modified_offer->description());
3045 ASSERT_TRUE(desc != NULL);
3046 for (StreamParams& stream : desc->mutable_streams()) {
3047 for (unsigned int& ssrc : stream.ssrcs) {
3048 ++ssrc;
3049 }
3050 }
3051
Steve Antondb45ca82017-09-11 18:27:34 -07003052 EXPECT_TRUE(DoSetLocalDescription(std::move(modified_offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07003053 senders = pc_->GetSenders();
3054 EXPECT_EQ(2u, senders.size());
3055 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
3056 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
3057 // TODO(deadbeef): Once RtpSenders expose parameters, check that the SSRC
3058 // changed.
3059}
3060
3061// This tests that the expected behavior occurs if a new session description is
3062// set with the same tracks, but on a different MediaStream.
Stefan Holmer55d6e7c2016-03-17 16:26:40 +01003063TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07003064 SignalSameTracksInSeparateMediaStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07003065 FakeConstraints constraints;
3066 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
3067 true);
3068 CreatePeerConnection(&constraints);
deadbeefab9b2d12015-10-14 11:33:11 -07003069
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003070 rtc::scoped_refptr<StreamCollection> stream_collection =
3071 CreateStreamCollection(2, 1);
3072 pc_->AddStream(stream_collection->at(0));
3073 std::unique_ptr<SessionDescriptionInterface> offer;
3074 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07003075 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07003076
deadbeefab9b2d12015-10-14 11:33:11 -07003077 auto senders = pc_->GetSenders();
3078 EXPECT_EQ(2u, senders.size());
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003079 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0], kStreams[0]));
3080 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0], kStreams[0]));
deadbeefab9b2d12015-10-14 11:33:11 -07003081
3082 // Add a new MediaStream but with the same tracks as in the first stream.
3083 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream_1(
3084 webrtc::MediaStream::Create(kStreams[1]));
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003085 stream_1->AddTrack(stream_collection->at(0)->GetVideoTracks()[0]);
3086 stream_1->AddTrack(stream_collection->at(0)->GetAudioTracks()[0]);
deadbeefab9b2d12015-10-14 11:33:11 -07003087 pc_->AddStream(stream_1);
3088
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003089 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07003090 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07003091
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003092 auto new_senders = pc_->GetSenders();
3093 // Should be the same senders as before, but with updated stream id.
3094 // Note that this behavior is subject to change in the future.
3095 // We may decide the PC should ignore existing tracks in AddStream.
3096 EXPECT_EQ(senders, new_senders);
3097 EXPECT_TRUE(ContainsSender(new_senders, kAudioTracks[0], kStreams[1]));
3098 EXPECT_TRUE(ContainsSender(new_senders, kVideoTracks[0], kStreams[1]));
deadbeefab9b2d12015-10-14 11:33:11 -07003099}
3100
zhihuang81c3a032016-11-17 12:06:24 -08003101// This tests that PeerConnectionObserver::OnAddTrack is correctly called.
3102TEST_F(PeerConnectionInterfaceTest, OnAddTrackCallback) {
3103 FakeConstraints constraints;
3104 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
3105 true);
3106 CreatePeerConnection(&constraints);
3107 CreateAndSetRemoteOffer(kSdpStringWithStream1AudioTrackOnly);
3108 EXPECT_EQ(observer_.num_added_tracks_, 1);
3109 EXPECT_EQ(observer_.last_added_track_label_, kAudioTracks[0]);
3110
3111 // Create and set the updated remote SDP.
3112 CreateAndSetRemoteOffer(kSdpStringWithStream1);
3113 EXPECT_EQ(observer_.num_added_tracks_, 2);
3114 EXPECT_EQ(observer_.last_added_track_label_, kVideoTracks[0]);
3115}
3116
deadbeefd1a38b52016-12-10 13:15:33 -08003117// Test that when SetConfiguration is called and the configuration is
3118// changing, the next offer causes an ICE restart.
3119TEST_F(PeerConnectionInterfaceTest, SetConfigurationCausingIceRetart) {
3120 PeerConnectionInterface::RTCConfiguration config;
3121 config.type = PeerConnectionInterface::kRelay;
3122 // Need to pass default constraints to prevent disabling of DTLS...
3123 FakeConstraints default_constraints;
3124 CreatePeerConnection(config, &default_constraints);
3125 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
3126
3127 // Do initial offer/answer so there's something to restart.
3128 CreateOfferAsLocalDescription();
3129 CreateAnswerAsRemoteDescription(kSdpStringWithStream1);
3130
3131 // Grab the ufrags.
3132 std::vector<std::string> initial_ufrags = GetUfrags(pc_->local_description());
3133
3134 // Change ICE policy, which should trigger an ICE restart on the next offer.
3135 config.type = PeerConnectionInterface::kAll;
3136 EXPECT_TRUE(pc_->SetConfiguration(config));
3137 CreateOfferAsLocalDescription();
3138
3139 // Grab the new ufrags.
3140 std::vector<std::string> subsequent_ufrags =
3141 GetUfrags(pc_->local_description());
3142
3143 // Sanity check.
3144 EXPECT_EQ(initial_ufrags.size(), subsequent_ufrags.size());
3145 // Check that each ufrag is different.
3146 for (int i = 0; i < static_cast<int>(initial_ufrags.size()); ++i) {
3147 EXPECT_NE(initial_ufrags[i], subsequent_ufrags[i]);
3148 }
3149}
3150
3151// Test that when SetConfiguration is called and the configuration *isn't*
3152// changing, the next offer does *not* cause an ICE restart.
3153TEST_F(PeerConnectionInterfaceTest, SetConfigurationNotCausingIceRetart) {
3154 PeerConnectionInterface::RTCConfiguration config;
3155 config.type = PeerConnectionInterface::kRelay;
3156 // Need to pass default constraints to prevent disabling of DTLS...
3157 FakeConstraints default_constraints;
3158 CreatePeerConnection(config, &default_constraints);
3159 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
3160
3161 // Do initial offer/answer so there's something to restart.
3162 CreateOfferAsLocalDescription();
3163 CreateAnswerAsRemoteDescription(kSdpStringWithStream1);
3164
3165 // Grab the ufrags.
3166 std::vector<std::string> initial_ufrags = GetUfrags(pc_->local_description());
3167
3168 // Call SetConfiguration with a config identical to what the PC was
3169 // constructed with.
3170 EXPECT_TRUE(pc_->SetConfiguration(config));
3171 CreateOfferAsLocalDescription();
3172
3173 // Grab the new ufrags.
3174 std::vector<std::string> subsequent_ufrags =
3175 GetUfrags(pc_->local_description());
3176
3177 EXPECT_EQ(initial_ufrags, subsequent_ufrags);
3178}
3179
3180// Test for a weird corner case scenario:
3181// 1. Audio/video session established.
3182// 2. SetConfiguration changes ICE config; ICE restart needed.
3183// 3. ICE restart initiated by remote peer, but only for one m= section.
3184// 4. Next createOffer should initiate an ICE restart, but only for the other
3185// m= section; it would be pointless to do an ICE restart for the m= section
3186// that was already restarted.
3187TEST_F(PeerConnectionInterfaceTest, SetConfigurationCausingPartialIceRestart) {
3188 PeerConnectionInterface::RTCConfiguration config;
3189 config.type = PeerConnectionInterface::kRelay;
3190 // Need to pass default constraints to prevent disabling of DTLS...
3191 FakeConstraints default_constraints;
3192 CreatePeerConnection(config, &default_constraints);
3193 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
3194
3195 // Do initial offer/answer so there's something to restart.
3196 CreateOfferAsLocalDescription();
3197 CreateAnswerAsRemoteDescription(kSdpStringWithStream1);
3198
3199 // Change ICE policy, which should set the "needs-ice-restart" flag.
3200 config.type = PeerConnectionInterface::kAll;
3201 EXPECT_TRUE(pc_->SetConfiguration(config));
3202
3203 // Do ICE restart for the first m= section, initiated by remote peer.
Steve Antondb45ca82017-09-11 18:27:34 -07003204 std::unique_ptr<webrtc::SessionDescriptionInterface> remote_offer(
3205 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
3206 kSdpStringWithStream1, nullptr));
3207 ASSERT_TRUE(remote_offer);
deadbeefd1a38b52016-12-10 13:15:33 -08003208 remote_offer->description()->transport_infos()[0].description.ice_ufrag =
3209 "modified";
Steve Antondb45ca82017-09-11 18:27:34 -07003210 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_offer)));
deadbeefd1a38b52016-12-10 13:15:33 -08003211 CreateAnswerAsLocalDescription();
3212
3213 // Grab the ufrags.
3214 std::vector<std::string> initial_ufrags = GetUfrags(pc_->local_description());
3215 ASSERT_EQ(2, initial_ufrags.size());
3216
3217 // Create offer and grab the new ufrags.
3218 CreateOfferAsLocalDescription();
3219 std::vector<std::string> subsequent_ufrags =
3220 GetUfrags(pc_->local_description());
3221 ASSERT_EQ(2, subsequent_ufrags.size());
3222
3223 // Ensure that only the ufrag for the second m= section changed.
3224 EXPECT_EQ(initial_ufrags[0], subsequent_ufrags[0]);
3225 EXPECT_NE(initial_ufrags[1], subsequent_ufrags[1]);
3226}
3227
deadbeeffe4a8a42016-12-20 17:56:17 -08003228// Tests that the methods to return current/pending descriptions work as
3229// expected at different points in the offer/answer exchange. This test does
3230// one offer/answer exchange as the offerer, then another as the answerer.
3231TEST_F(PeerConnectionInterfaceTest, CurrentAndPendingDescriptions) {
3232 // This disables DTLS so we can apply an answer to ourselves.
3233 CreatePeerConnection();
3234
3235 // Create initial local offer and get SDP (which will also be used as
3236 // answer/pranswer);
Steve Antondb45ca82017-09-11 18:27:34 -07003237 std::unique_ptr<SessionDescriptionInterface> local_offer;
3238 ASSERT_TRUE(DoCreateOffer(&local_offer, nullptr));
deadbeeffe4a8a42016-12-20 17:56:17 -08003239 std::string sdp;
Steve Antondb45ca82017-09-11 18:27:34 -07003240 EXPECT_TRUE(local_offer->ToString(&sdp));
deadbeeffe4a8a42016-12-20 17:56:17 -08003241
3242 // Set local offer.
Steve Antondb45ca82017-09-11 18:27:34 -07003243 SessionDescriptionInterface* local_offer_ptr = local_offer.get();
3244 EXPECT_TRUE(DoSetLocalDescription(std::move(local_offer)));
3245 EXPECT_EQ(local_offer_ptr, pc_->pending_local_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003246 EXPECT_EQ(nullptr, pc_->pending_remote_description());
3247 EXPECT_EQ(nullptr, pc_->current_local_description());
3248 EXPECT_EQ(nullptr, pc_->current_remote_description());
3249
3250 // Set remote pranswer.
Steve Antondb45ca82017-09-11 18:27:34 -07003251 std::unique_ptr<SessionDescriptionInterface> remote_pranswer(
deadbeeffe4a8a42016-12-20 17:56:17 -08003252 webrtc::CreateSessionDescription(SessionDescriptionInterface::kPrAnswer,
Steve Antondb45ca82017-09-11 18:27:34 -07003253 sdp, nullptr));
3254 SessionDescriptionInterface* remote_pranswer_ptr = remote_pranswer.get();
3255 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_pranswer)));
3256 EXPECT_EQ(local_offer_ptr, pc_->pending_local_description());
3257 EXPECT_EQ(remote_pranswer_ptr, pc_->pending_remote_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003258 EXPECT_EQ(nullptr, pc_->current_local_description());
3259 EXPECT_EQ(nullptr, pc_->current_remote_description());
3260
3261 // Set remote answer.
Steve Antondb45ca82017-09-11 18:27:34 -07003262 std::unique_ptr<SessionDescriptionInterface> remote_answer(
3263 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
3264 sdp, nullptr));
3265 SessionDescriptionInterface* remote_answer_ptr = remote_answer.get();
3266 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_answer)));
deadbeeffe4a8a42016-12-20 17:56:17 -08003267 EXPECT_EQ(nullptr, pc_->pending_local_description());
3268 EXPECT_EQ(nullptr, pc_->pending_remote_description());
Steve Antondb45ca82017-09-11 18:27:34 -07003269 EXPECT_EQ(local_offer_ptr, pc_->current_local_description());
3270 EXPECT_EQ(remote_answer_ptr, pc_->current_remote_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003271
3272 // Set remote offer.
Steve Antondb45ca82017-09-11 18:27:34 -07003273 std::unique_ptr<SessionDescriptionInterface> remote_offer(
3274 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer, sdp,
3275 nullptr));
3276 SessionDescriptionInterface* remote_offer_ptr = remote_offer.get();
3277 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_offer)));
3278 EXPECT_EQ(remote_offer_ptr, pc_->pending_remote_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003279 EXPECT_EQ(nullptr, pc_->pending_local_description());
Steve Antondb45ca82017-09-11 18:27:34 -07003280 EXPECT_EQ(local_offer_ptr, pc_->current_local_description());
3281 EXPECT_EQ(remote_answer_ptr, pc_->current_remote_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003282
3283 // Set local pranswer.
Steve Antondb45ca82017-09-11 18:27:34 -07003284 std::unique_ptr<SessionDescriptionInterface> local_pranswer(
deadbeeffe4a8a42016-12-20 17:56:17 -08003285 webrtc::CreateSessionDescription(SessionDescriptionInterface::kPrAnswer,
Steve Antondb45ca82017-09-11 18:27:34 -07003286 sdp, nullptr));
3287 SessionDescriptionInterface* local_pranswer_ptr = local_pranswer.get();
3288 EXPECT_TRUE(DoSetLocalDescription(std::move(local_pranswer)));
3289 EXPECT_EQ(remote_offer_ptr, pc_->pending_remote_description());
3290 EXPECT_EQ(local_pranswer_ptr, pc_->pending_local_description());
3291 EXPECT_EQ(local_offer_ptr, pc_->current_local_description());
3292 EXPECT_EQ(remote_answer_ptr, pc_->current_remote_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003293
3294 // Set local answer.
Steve Antondb45ca82017-09-11 18:27:34 -07003295 std::unique_ptr<SessionDescriptionInterface> local_answer(
3296 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
3297 sdp, nullptr));
3298 SessionDescriptionInterface* local_answer_ptr = local_answer.get();
3299 EXPECT_TRUE(DoSetLocalDescription(std::move(local_answer)));
deadbeeffe4a8a42016-12-20 17:56:17 -08003300 EXPECT_EQ(nullptr, pc_->pending_remote_description());
3301 EXPECT_EQ(nullptr, pc_->pending_local_description());
Steve Antondb45ca82017-09-11 18:27:34 -07003302 EXPECT_EQ(remote_offer_ptr, pc_->current_remote_description());
3303 EXPECT_EQ(local_answer_ptr, pc_->current_local_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003304}
3305
zhihuang77985012017-02-07 15:45:16 -08003306// Tests that it won't crash when calling StartRtcEventLog or StopRtcEventLog
3307// after the PeerConnection is closed.
3308TEST_F(PeerConnectionInterfaceTest,
3309 StartAndStopLoggingAfterPeerConnectionClosed) {
3310 CreatePeerConnection();
3311 // The RtcEventLog will be reset when the PeerConnection is closed.
3312 pc_->Close();
3313
3314 rtc::PlatformFile file = 0;
3315 int64_t max_size_bytes = 1024;
3316 EXPECT_FALSE(pc_->StartRtcEventLog(file, max_size_bytes));
3317 pc_->StopRtcEventLog();
3318}
3319
deadbeef30952b42017-04-21 02:41:29 -07003320// Test that generated offers/answers include "ice-option:trickle".
3321TEST_F(PeerConnectionInterfaceTest, OffersAndAnswersHaveTrickleIceOption) {
3322 CreatePeerConnection();
3323
3324 // First, create an offer with audio/video.
3325 FakeConstraints constraints;
3326 constraints.SetMandatoryReceiveAudio(true);
3327 constraints.SetMandatoryReceiveVideo(true);
3328 std::unique_ptr<SessionDescriptionInterface> offer;
3329 ASSERT_TRUE(DoCreateOffer(&offer, &constraints));
3330 cricket::SessionDescription* desc = offer->description();
3331 ASSERT_EQ(2u, desc->transport_infos().size());
3332 EXPECT_TRUE(desc->transport_infos()[0].description.HasOption("trickle"));
3333 EXPECT_TRUE(desc->transport_infos()[1].description.HasOption("trickle"));
3334
3335 // Apply the offer as a remote description, then create an answer.
Steve Antondb45ca82017-09-11 18:27:34 -07003336 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
deadbeef30952b42017-04-21 02:41:29 -07003337 std::unique_ptr<SessionDescriptionInterface> answer;
3338 ASSERT_TRUE(DoCreateAnswer(&answer, &constraints));
3339 desc = answer->description();
3340 ASSERT_EQ(2u, desc->transport_infos().size());
3341 EXPECT_TRUE(desc->transport_infos()[0].description.HasOption("trickle"));
3342 EXPECT_TRUE(desc->transport_infos()[1].description.HasOption("trickle"));
3343}
3344
deadbeef1dcb1642017-03-29 21:08:16 -07003345// Test that ICE renomination isn't offered if it's not enabled in the PC's
3346// RTCConfiguration.
3347TEST_F(PeerConnectionInterfaceTest, IceRenominationNotOffered) {
3348 PeerConnectionInterface::RTCConfiguration config;
3349 config.enable_ice_renomination = false;
3350 CreatePeerConnection(config, nullptr);
3351 AddVoiceStream("foo");
3352
3353 std::unique_ptr<SessionDescriptionInterface> offer;
3354 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
3355 cricket::SessionDescription* desc = offer->description();
3356 EXPECT_EQ(1u, desc->transport_infos().size());
3357 EXPECT_FALSE(
3358 desc->transport_infos()[0].description.GetIceParameters().renomination);
3359}
3360
3361// Test that the ICE renomination option is present in generated offers/answers
3362// if it's enabled in the PC's RTCConfiguration.
3363TEST_F(PeerConnectionInterfaceTest, IceRenominationOptionInOfferAndAnswer) {
3364 PeerConnectionInterface::RTCConfiguration config;
3365 config.enable_ice_renomination = true;
3366 CreatePeerConnection(config, nullptr);
3367 AddVoiceStream("foo");
3368
3369 std::unique_ptr<SessionDescriptionInterface> offer;
3370 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
3371 cricket::SessionDescription* desc = offer->description();
3372 EXPECT_EQ(1u, desc->transport_infos().size());
3373 EXPECT_TRUE(
3374 desc->transport_infos()[0].description.GetIceParameters().renomination);
3375
3376 // Set the offer as a remote description, then create an answer and ensure it
3377 // has the renomination flag too.
Steve Antondb45ca82017-09-11 18:27:34 -07003378 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
deadbeef1dcb1642017-03-29 21:08:16 -07003379 std::unique_ptr<SessionDescriptionInterface> answer;
3380 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
3381 desc = answer->description();
3382 EXPECT_EQ(1u, desc->transport_infos().size());
3383 EXPECT_TRUE(
3384 desc->transport_infos()[0].description.GetIceParameters().renomination);
3385}
3386
3387// Test that if CreateOffer is called with the deprecated "offer to receive
3388// audio/video" constraints, they're processed and result in an offer with
3389// audio/video sections just as if RTCOfferAnswerOptions had been used.
3390TEST_F(PeerConnectionInterfaceTest, CreateOfferWithOfferToReceiveConstraints) {
3391 CreatePeerConnection();
3392
3393 FakeConstraints constraints;
3394 constraints.SetMandatoryReceiveAudio(true);
3395 constraints.SetMandatoryReceiveVideo(true);
3396 std::unique_ptr<SessionDescriptionInterface> offer;
3397 ASSERT_TRUE(DoCreateOffer(&offer, &constraints));
3398
3399 cricket::SessionDescription* desc = offer->description();
3400 const cricket::ContentInfo* audio = cricket::GetFirstAudioContent(desc);
3401 const cricket::ContentInfo* video = cricket::GetFirstVideoContent(desc);
3402 ASSERT_NE(nullptr, audio);
3403 ASSERT_NE(nullptr, video);
3404 EXPECT_FALSE(audio->rejected);
3405 EXPECT_FALSE(video->rejected);
3406}
3407
3408// Test that if CreateAnswer is called with the deprecated "offer to receive
3409// audio/video" constraints, they're processed and can be used to reject an
3410// offered m= section just as can be done with RTCOfferAnswerOptions;
3411TEST_F(PeerConnectionInterfaceTest, CreateAnswerWithOfferToReceiveConstraints) {
3412 CreatePeerConnection();
3413
3414 // First, create an offer with audio/video and apply it as a remote
3415 // description.
3416 FakeConstraints constraints;
3417 constraints.SetMandatoryReceiveAudio(true);
3418 constraints.SetMandatoryReceiveVideo(true);
3419 std::unique_ptr<SessionDescriptionInterface> offer;
3420 ASSERT_TRUE(DoCreateOffer(&offer, &constraints));
Steve Antondb45ca82017-09-11 18:27:34 -07003421 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
deadbeef1dcb1642017-03-29 21:08:16 -07003422
3423 // Now create answer that rejects audio/video.
3424 constraints.SetMandatoryReceiveAudio(false);
3425 constraints.SetMandatoryReceiveVideo(false);
3426 std::unique_ptr<SessionDescriptionInterface> answer;
3427 ASSERT_TRUE(DoCreateAnswer(&answer, &constraints));
3428
3429 cricket::SessionDescription* desc = answer->description();
3430 const cricket::ContentInfo* audio = cricket::GetFirstAudioContent(desc);
3431 const cricket::ContentInfo* video = cricket::GetFirstVideoContent(desc);
3432 ASSERT_NE(nullptr, audio);
3433 ASSERT_NE(nullptr, video);
3434 EXPECT_TRUE(audio->rejected);
3435 EXPECT_TRUE(video->rejected);
3436}
3437
3438#ifdef HAVE_SCTP
3439#define MAYBE_DataChannelOnlyOfferWithMaxBundlePolicy \
3440 DataChannelOnlyOfferWithMaxBundlePolicy
3441#else
3442#define MAYBE_DataChannelOnlyOfferWithMaxBundlePolicy \
3443 DISABLED_DataChannelOnlyOfferWithMaxBundlePolicy
3444#endif
3445
3446// Test that negotiation can succeed with a data channel only, and with the max
3447// bundle policy. Previously there was a bug that prevented this.
3448TEST_F(PeerConnectionInterfaceTest,
3449 MAYBE_DataChannelOnlyOfferWithMaxBundlePolicy) {
3450 PeerConnectionInterface::RTCConfiguration config;
3451 config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
3452 CreatePeerConnection(config, nullptr);
3453
3454 // First, create an offer with only a data channel and apply it as a remote
3455 // description.
3456 pc_->CreateDataChannel("test", nullptr);
3457 std::unique_ptr<SessionDescriptionInterface> offer;
3458 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07003459 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
deadbeef1dcb1642017-03-29 21:08:16 -07003460
3461 // Create and set answer as well.
3462 std::unique_ptr<SessionDescriptionInterface> answer;
3463 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07003464 EXPECT_TRUE(DoSetLocalDescription(std::move(answer)));
deadbeef1dcb1642017-03-29 21:08:16 -07003465}
3466
zstein4b979802017-06-02 14:37:37 -07003467TEST_F(PeerConnectionInterfaceTest, SetBitrateWithoutMinSucceeds) {
3468 CreatePeerConnection();
3469 PeerConnectionInterface::BitrateParameters bitrate;
3470 bitrate.current_bitrate_bps = rtc::Optional<int>(100000);
3471 EXPECT_TRUE(pc_->SetBitrate(bitrate).ok());
3472}
3473
3474TEST_F(PeerConnectionInterfaceTest, SetBitrateNegativeMinFails) {
3475 CreatePeerConnection();
3476 PeerConnectionInterface::BitrateParameters bitrate;
3477 bitrate.min_bitrate_bps = rtc::Optional<int>(-1);
3478 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3479}
3480
3481TEST_F(PeerConnectionInterfaceTest, SetBitrateCurrentLessThanMinFails) {
3482 CreatePeerConnection();
3483 PeerConnectionInterface::BitrateParameters bitrate;
3484 bitrate.min_bitrate_bps = rtc::Optional<int>(5);
3485 bitrate.current_bitrate_bps = rtc::Optional<int>(3);
3486 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3487}
3488
3489TEST_F(PeerConnectionInterfaceTest, SetBitrateCurrentNegativeFails) {
3490 CreatePeerConnection();
3491 PeerConnectionInterface::BitrateParameters bitrate;
3492 bitrate.current_bitrate_bps = rtc::Optional<int>(-1);
3493 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3494}
3495
3496TEST_F(PeerConnectionInterfaceTest, SetBitrateMaxLessThanCurrentFails) {
3497 CreatePeerConnection();
3498 PeerConnectionInterface::BitrateParameters bitrate;
3499 bitrate.current_bitrate_bps = rtc::Optional<int>(10);
3500 bitrate.max_bitrate_bps = rtc::Optional<int>(8);
3501 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3502}
3503
3504TEST_F(PeerConnectionInterfaceTest, SetBitrateMaxLessThanMinFails) {
3505 CreatePeerConnection();
3506 PeerConnectionInterface::BitrateParameters bitrate;
3507 bitrate.min_bitrate_bps = rtc::Optional<int>(10);
3508 bitrate.max_bitrate_bps = rtc::Optional<int>(8);
3509 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3510}
3511
3512TEST_F(PeerConnectionInterfaceTest, SetBitrateMaxNegativeFails) {
3513 CreatePeerConnection();
3514 PeerConnectionInterface::BitrateParameters bitrate;
3515 bitrate.max_bitrate_bps = rtc::Optional<int>(-1);
3516 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3517}
3518
Steve Anton038834f2017-07-14 15:59:59 -07003519// ice_regather_interval_range requires WebRTC to be configured for continual
3520// gathering already.
3521TEST_F(PeerConnectionInterfaceTest,
3522 SetIceRegatherIntervalRangeWithoutContinualGatheringFails) {
3523 PeerConnectionInterface::RTCConfiguration config;
3524 config.ice_regather_interval_range.emplace(1000, 2000);
3525 config.continual_gathering_policy =
3526 PeerConnectionInterface::ContinualGatheringPolicy::GATHER_ONCE;
3527 CreatePeerConnectionExpectFail(config);
3528}
3529
3530// Ensures that there is no error when ice_regather_interval_range is set with
3531// continual gathering enabled.
3532TEST_F(PeerConnectionInterfaceTest,
3533 SetIceRegatherIntervalRangeWithContinualGathering) {
3534 PeerConnectionInterface::RTCConfiguration config;
3535 config.ice_regather_interval_range.emplace(1000, 2000);
3536 config.continual_gathering_policy =
3537 PeerConnectionInterface::ContinualGatheringPolicy::GATHER_CONTINUALLY;
3538 CreatePeerConnection(config, nullptr);
3539}
3540
zstein4b979802017-06-02 14:37:37 -07003541// The current bitrate from Call's BitrateConfigMask is currently clamped by
3542// Call's BitrateConfig, which comes from the SDP or a default value. This test
3543// checks that a call to SetBitrate with a current bitrate that will be clamped
3544// succeeds.
3545TEST_F(PeerConnectionInterfaceTest, SetBitrateCurrentLessThanImplicitMin) {
3546 CreatePeerConnection();
3547 PeerConnectionInterface::BitrateParameters bitrate;
3548 bitrate.current_bitrate_bps = rtc::Optional<int>(1);
3549 EXPECT_TRUE(pc_->SetBitrate(bitrate).ok());
3550}
3551
zhihuang1c378ed2017-08-17 14:10:50 -07003552// The following tests verify that the offer can be created correctly.
3553TEST_F(PeerConnectionInterfaceTest,
3554 CreateOfferFailsWithInvalidOfferToReceiveAudio) {
3555 RTCOfferAnswerOptions rtc_options;
3556
3557 // Setting offer_to_receive_audio to a value lower than kUndefined or greater
3558 // than kMaxOfferToReceiveMedia should be treated as invalid.
3559 rtc_options.offer_to_receive_audio = RTCOfferAnswerOptions::kUndefined - 1;
3560 CreatePeerConnection();
3561 EXPECT_FALSE(CreateOfferWithOptions(rtc_options));
3562
3563 rtc_options.offer_to_receive_audio =
3564 RTCOfferAnswerOptions::kMaxOfferToReceiveMedia + 1;
3565 EXPECT_FALSE(CreateOfferWithOptions(rtc_options));
3566}
3567
3568TEST_F(PeerConnectionInterfaceTest,
3569 CreateOfferFailsWithInvalidOfferToReceiveVideo) {
3570 RTCOfferAnswerOptions rtc_options;
3571
3572 // Setting offer_to_receive_video to a value lower than kUndefined or greater
3573 // than kMaxOfferToReceiveMedia should be treated as invalid.
3574 rtc_options.offer_to_receive_video = RTCOfferAnswerOptions::kUndefined - 1;
3575 CreatePeerConnection();
3576 EXPECT_FALSE(CreateOfferWithOptions(rtc_options));
3577
3578 rtc_options.offer_to_receive_video =
3579 RTCOfferAnswerOptions::kMaxOfferToReceiveMedia + 1;
3580 EXPECT_FALSE(CreateOfferWithOptions(rtc_options));
3581}
3582
3583// Test that the audio and video content will be added to an offer if both
3584// |offer_to_receive_audio| and |offer_to_receive_video| options are 1.
3585TEST_F(PeerConnectionInterfaceTest, CreateOfferWithAudioVideoOptions) {
3586 RTCOfferAnswerOptions rtc_options;
3587 rtc_options.offer_to_receive_audio = 1;
3588 rtc_options.offer_to_receive_video = 1;
3589
3590 std::unique_ptr<SessionDescriptionInterface> offer;
3591 CreatePeerConnection();
3592 offer = CreateOfferWithOptions(rtc_options);
3593 ASSERT_TRUE(offer);
3594 EXPECT_NE(nullptr, GetFirstAudioContent(offer->description()));
3595 EXPECT_NE(nullptr, GetFirstVideoContent(offer->description()));
3596}
3597
3598// Test that only audio content will be added to the offer if only
3599// |offer_to_receive_audio| options is 1.
3600TEST_F(PeerConnectionInterfaceTest, CreateOfferWithAudioOnlyOptions) {
3601 RTCOfferAnswerOptions rtc_options;
3602 rtc_options.offer_to_receive_audio = 1;
3603 rtc_options.offer_to_receive_video = 0;
3604
3605 std::unique_ptr<SessionDescriptionInterface> offer;
3606 CreatePeerConnection();
3607 offer = CreateOfferWithOptions(rtc_options);
3608 ASSERT_TRUE(offer);
3609 EXPECT_NE(nullptr, GetFirstAudioContent(offer->description()));
3610 EXPECT_EQ(nullptr, GetFirstVideoContent(offer->description()));
3611}
3612
3613// Test that only video content will be added if only |offer_to_receive_video|
3614// options is 1.
3615TEST_F(PeerConnectionInterfaceTest, CreateOfferWithVideoOnlyOptions) {
3616 RTCOfferAnswerOptions rtc_options;
3617 rtc_options.offer_to_receive_audio = 0;
3618 rtc_options.offer_to_receive_video = 1;
3619
3620 std::unique_ptr<SessionDescriptionInterface> offer;
3621 CreatePeerConnection();
3622 offer = CreateOfferWithOptions(rtc_options);
3623 ASSERT_TRUE(offer);
3624 EXPECT_EQ(nullptr, GetFirstAudioContent(offer->description()));
3625 EXPECT_NE(nullptr, GetFirstVideoContent(offer->description()));
3626}
3627
3628// Test that if |voice_activity_detection| is false, no CN codec is added to the
3629// offer.
3630TEST_F(PeerConnectionInterfaceTest, CreateOfferWithVADOptions) {
3631 RTCOfferAnswerOptions rtc_options;
3632 rtc_options.offer_to_receive_audio = 1;
3633 rtc_options.offer_to_receive_video = 0;
3634
3635 std::unique_ptr<SessionDescriptionInterface> offer;
3636 CreatePeerConnection();
3637 offer = CreateOfferWithOptions(rtc_options);
3638 ASSERT_TRUE(offer);
3639 const cricket::ContentInfo* audio_content =
3640 offer->description()->GetContentByName(cricket::CN_AUDIO);
3641 ASSERT_TRUE(audio_content);
3642 // |voice_activity_detection| is true by default.
3643 EXPECT_TRUE(HasCNCodecs(audio_content));
3644
3645 rtc_options.voice_activity_detection = false;
3646 CreatePeerConnection();
3647 offer = CreateOfferWithOptions(rtc_options);
3648 ASSERT_TRUE(offer);
3649 audio_content = offer->description()->GetContentByName(cricket::CN_AUDIO);
3650 ASSERT_TRUE(audio_content);
3651 EXPECT_FALSE(HasCNCodecs(audio_content));
3652}
3653
3654// Test that no media content will be added to the offer if using default
3655// RTCOfferAnswerOptions.
3656TEST_F(PeerConnectionInterfaceTest, CreateOfferWithDefaultOfferAnswerOptions) {
3657 RTCOfferAnswerOptions rtc_options;
3658
3659 std::unique_ptr<SessionDescriptionInterface> offer;
3660 CreatePeerConnection();
3661 offer = CreateOfferWithOptions(rtc_options);
3662 ASSERT_TRUE(offer);
3663 EXPECT_EQ(nullptr, GetFirstAudioContent(offer->description()));
3664 EXPECT_EQ(nullptr, GetFirstVideoContent(offer->description()));
3665}
3666
3667// Test that if |ice_restart| is true, the ufrag/pwd will change, otherwise
3668// ufrag/pwd will be the same in the new offer.
3669TEST_F(PeerConnectionInterfaceTest, CreateOfferWithIceRestart) {
3670 RTCOfferAnswerOptions rtc_options;
3671 rtc_options.ice_restart = false;
3672 rtc_options.offer_to_receive_audio = 1;
3673
3674 std::unique_ptr<SessionDescriptionInterface> offer;
3675 CreatePeerConnection();
3676 CreateOfferWithOptionsAsLocalDescription(&offer, rtc_options);
3677 auto ufrag1 = offer->description()
3678 ->GetTransportInfoByName(cricket::CN_AUDIO)
3679 ->description.ice_ufrag;
3680 auto pwd1 = offer->description()
3681 ->GetTransportInfoByName(cricket::CN_AUDIO)
3682 ->description.ice_pwd;
3683
3684 // |ice_restart| is false, the ufrag/pwd shouldn't change.
3685 CreateOfferWithOptionsAsLocalDescription(&offer, rtc_options);
3686 auto ufrag2 = offer->description()
3687 ->GetTransportInfoByName(cricket::CN_AUDIO)
3688 ->description.ice_ufrag;
3689 auto pwd2 = offer->description()
3690 ->GetTransportInfoByName(cricket::CN_AUDIO)
3691 ->description.ice_pwd;
3692
3693 // |ice_restart| is true, the ufrag/pwd should change.
3694 rtc_options.ice_restart = true;
3695 CreateOfferWithOptionsAsLocalDescription(&offer, rtc_options);
3696 auto ufrag3 = offer->description()
3697 ->GetTransportInfoByName(cricket::CN_AUDIO)
3698 ->description.ice_ufrag;
3699 auto pwd3 = offer->description()
3700 ->GetTransportInfoByName(cricket::CN_AUDIO)
3701 ->description.ice_pwd;
3702
3703 EXPECT_EQ(ufrag1, ufrag2);
3704 EXPECT_EQ(pwd1, pwd2);
3705 EXPECT_NE(ufrag2, ufrag3);
3706 EXPECT_NE(pwd2, pwd3);
3707}
3708
3709// Test that if |use_rtp_mux| is true, the bundling will be enabled in the
3710// offer; if it is false, there won't be any bundle group in the offer.
3711TEST_F(PeerConnectionInterfaceTest, CreateOfferWithRtpMux) {
3712 RTCOfferAnswerOptions rtc_options;
3713 rtc_options.offer_to_receive_audio = 1;
3714 rtc_options.offer_to_receive_video = 1;
3715
3716 std::unique_ptr<SessionDescriptionInterface> offer;
3717 CreatePeerConnection();
3718
3719 rtc_options.use_rtp_mux = true;
3720 offer = CreateOfferWithOptions(rtc_options);
3721 ASSERT_TRUE(offer);
3722 EXPECT_NE(nullptr, GetFirstAudioContent(offer->description()));
3723 EXPECT_NE(nullptr, GetFirstVideoContent(offer->description()));
3724 EXPECT_TRUE(offer->description()->HasGroup(cricket::GROUP_TYPE_BUNDLE));
3725
3726 rtc_options.use_rtp_mux = false;
3727 offer = CreateOfferWithOptions(rtc_options);
3728 ASSERT_TRUE(offer);
3729 EXPECT_NE(nullptr, GetFirstAudioContent(offer->description()));
3730 EXPECT_NE(nullptr, GetFirstVideoContent(offer->description()));
3731 EXPECT_FALSE(offer->description()->HasGroup(cricket::GROUP_TYPE_BUNDLE));
3732}
3733
3734// If SetMandatoryReceiveAudio(false) and SetMandatoryReceiveVideo(false) are
3735// called for the answer constraints, but an audio and a video section were
3736// offered, there will still be an audio and a video section in the answer.
3737TEST_F(PeerConnectionInterfaceTest,
3738 RejectAudioAndVideoInAnswerWithConstraints) {
3739 // Offer both audio and video.
3740 RTCOfferAnswerOptions rtc_offer_options;
3741 rtc_offer_options.offer_to_receive_audio = 1;
3742 rtc_offer_options.offer_to_receive_video = 1;
3743
3744 CreatePeerConnection();
3745 std::unique_ptr<SessionDescriptionInterface> offer;
3746 CreateOfferWithOptionsAsRemoteDescription(&offer, rtc_offer_options);
3747 EXPECT_NE(nullptr, GetFirstAudioContent(offer->description()));
3748 EXPECT_NE(nullptr, GetFirstVideoContent(offer->description()));
3749
3750 // Since an offer has been created with both audio and video,
3751 // Answers will contain the media types that exist in the offer regardless of
3752 // the value of |answer_options.has_audio| and |answer_options.has_video|.
3753 FakeConstraints answer_c;
3754 // Reject both audio and video.
3755 answer_c.SetMandatoryReceiveAudio(false);
3756 answer_c.SetMandatoryReceiveVideo(false);
3757
3758 std::unique_ptr<SessionDescriptionInterface> answer;
3759 ASSERT_TRUE(DoCreateAnswer(&answer, &answer_c));
3760 const cricket::ContentInfo* audio_content =
3761 GetFirstAudioContent(answer->description());
3762 const cricket::ContentInfo* video_content =
3763 GetFirstVideoContent(answer->description());
3764 ASSERT_NE(nullptr, audio_content);
3765 ASSERT_NE(nullptr, video_content);
3766 EXPECT_TRUE(audio_content->rejected);
3767 EXPECT_TRUE(video_content->rejected);
3768}
3769
zhihuang141aacb2017-08-29 13:23:53 -07003770// This test ensures OnRenegotiationNeeded is called when we add track with
3771// MediaStream -> AddTrack in the same way it is called when we add track with
3772// PeerConnection -> AddTrack.
3773// The test can be removed once addStream is rewritten in terms of addTrack
3774// https://bugs.chromium.org/p/webrtc/issues/detail?id=7815
3775TEST_F(PeerConnectionInterfaceTest, MediaStreamAddTrackRemoveTrackRenegotiate) {
3776 CreatePeerConnectionWithoutDtls();
3777 rtc::scoped_refptr<MediaStreamInterface> stream(
3778 pc_factory_->CreateLocalMediaStream(kStreamLabel1));
3779 pc_->AddStream(stream);
3780 rtc::scoped_refptr<AudioTrackInterface> audio_track(
3781 pc_factory_->CreateAudioTrack("audio_track", nullptr));
3782 rtc::scoped_refptr<VideoTrackInterface> video_track(
3783 pc_factory_->CreateVideoTrack(
3784 "video_track", pc_factory_->CreateVideoSource(
3785 std::unique_ptr<cricket::VideoCapturer>(
3786 new cricket::FakeVideoCapturer()))));
3787 stream->AddTrack(audio_track);
3788 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
3789 observer_.renegotiation_needed_ = false;
3790
3791 stream->AddTrack(video_track);
3792 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
3793 observer_.renegotiation_needed_ = false;
3794
3795 stream->RemoveTrack(audio_track);
3796 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
3797 observer_.renegotiation_needed_ = false;
3798
3799 stream->RemoveTrack(video_track);
3800 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
3801 observer_.renegotiation_needed_ = false;
3802}
3803
3804// Tests that creating answer would fail gracefully without being crashed if the
3805// remote description is unset.
3806TEST_F(PeerConnectionInterfaceTest, CreateAnswerWithoutRemoteDescription) {
3807 CreatePeerConnection();
3808 // Creating answer fails because the remote description is unset.
3809 std::unique_ptr<SessionDescriptionInterface> answer;
3810 EXPECT_FALSE(DoCreateAnswer(&answer, nullptr));
3811
3812 // Createing answer succeeds when the remote description is set.
3813 CreateOfferAsRemoteDescription();
3814 EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
3815}
3816
Zhi Huang2a5e4262017-09-14 01:15:03 -07003817// Test that an error is returned if a description is applied that doesn't
3818// respect the order of existing media sections.
3819TEST_F(PeerConnectionInterfaceTest,
3820 MediaSectionOrderEnforcedForSubsequentOffers) {
3821 CreatePeerConnection();
3822 FakeConstraints constraints;
3823 constraints.SetMandatoryReceiveAudio(true);
3824 constraints.SetMandatoryReceiveVideo(true);
3825 std::unique_ptr<SessionDescriptionInterface> offer;
3826 ASSERT_TRUE(DoCreateOffer(&offer, &constraints));
3827 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
3828
3829 std::unique_ptr<SessionDescriptionInterface> answer;
3830 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
3831 EXPECT_TRUE(DoSetLocalDescription(std::move(answer)));
3832
3833 // A remote offer with different m=line order should be rejected.
3834 ASSERT_TRUE(DoCreateOffer(&offer, &constraints));
3835 std::reverse(offer->description()->contents().begin(),
3836 offer->description()->contents().end());
3837 std::reverse(offer->description()->transport_infos().begin(),
3838 offer->description()->transport_infos().end());
3839 EXPECT_FALSE(DoSetRemoteDescription(std::move(offer)));
3840
3841 // A subsequent local offer with different m=line order should be rejected.
3842 ASSERT_TRUE(DoCreateOffer(&offer, &constraints));
3843 std::reverse(offer->description()->contents().begin(),
3844 offer->description()->contents().end());
3845 std::reverse(offer->description()->transport_infos().begin(),
3846 offer->description()->transport_infos().end());
3847 EXPECT_FALSE(DoSetLocalDescription(std::move(offer)));
3848}
3849
nisse51542be2016-02-12 02:27:06 -08003850class PeerConnectionMediaConfigTest : public testing::Test {
3851 protected:
3852 void SetUp() override {
zhihuang38ede132017-06-15 12:52:32 -07003853 pcf_ = PeerConnectionFactoryForTest::CreatePeerConnectionFactoryForTest();
nisse51542be2016-02-12 02:27:06 -08003854 pcf_->Initialize();
3855 }
nisseeaabdf62017-05-05 02:23:02 -07003856 const cricket::MediaConfig TestCreatePeerConnection(
nisse51542be2016-02-12 02:27:06 -08003857 const PeerConnectionInterface::RTCConfiguration& config,
zhihuang1c378ed2017-08-17 14:10:50 -07003858 const MediaConstraintsInterface* constraints) {
zhihuang9763d562016-08-05 11:14:50 -07003859 rtc::scoped_refptr<PeerConnectionInterface> pc(pcf_->CreatePeerConnection(
3860 config, constraints, nullptr, nullptr, &observer_));
nisse51542be2016-02-12 02:27:06 -08003861 EXPECT_TRUE(pc.get());
nisseeaabdf62017-05-05 02:23:02 -07003862 return pc->GetConfiguration().media_config;
nisse51542be2016-02-12 02:27:06 -08003863 }
3864
zhihuang9763d562016-08-05 11:14:50 -07003865 rtc::scoped_refptr<PeerConnectionFactoryForTest> pcf_;
nisse51542be2016-02-12 02:27:06 -08003866 MockPeerConnectionObserver observer_;
3867};
3868
3869// This test verifies the default behaviour with no constraints and a
3870// default RTCConfiguration.
3871TEST_F(PeerConnectionMediaConfigTest, TestDefaults) {
3872 PeerConnectionInterface::RTCConfiguration config;
3873 FakeConstraints constraints;
3874
3875 const cricket::MediaConfig& media_config =
3876 TestCreatePeerConnection(config, &constraints);
3877
3878 EXPECT_FALSE(media_config.enable_dscp);
nisse0db023a2016-03-01 04:29:59 -08003879 EXPECT_TRUE(media_config.video.enable_cpu_overuse_detection);
3880 EXPECT_FALSE(media_config.video.disable_prerenderer_smoothing);
3881 EXPECT_FALSE(media_config.video.suspend_below_min_bitrate);
nisse51542be2016-02-12 02:27:06 -08003882}
3883
3884// This test verifies the DSCP constraint is recognized and passed to
nisse528b7932017-05-08 03:21:43 -07003885// the PeerConnection.
nisse51542be2016-02-12 02:27:06 -08003886TEST_F(PeerConnectionMediaConfigTest, TestDscpConstraintTrue) {
3887 PeerConnectionInterface::RTCConfiguration config;
3888 FakeConstraints constraints;
3889
3890 constraints.AddOptional(webrtc::MediaConstraintsInterface::kEnableDscp, true);
3891 const cricket::MediaConfig& media_config =
3892 TestCreatePeerConnection(config, &constraints);
3893
3894 EXPECT_TRUE(media_config.enable_dscp);
3895}
3896
3897// This test verifies the cpu overuse detection constraint is
nisse528b7932017-05-08 03:21:43 -07003898// recognized and passed to the PeerConnection.
nisse51542be2016-02-12 02:27:06 -08003899TEST_F(PeerConnectionMediaConfigTest, TestCpuOveruseConstraintFalse) {
3900 PeerConnectionInterface::RTCConfiguration config;
3901 FakeConstraints constraints;
3902
3903 constraints.AddOptional(
3904 webrtc::MediaConstraintsInterface::kCpuOveruseDetection, false);
3905 const cricket::MediaConfig media_config =
3906 TestCreatePeerConnection(config, &constraints);
3907
nisse0db023a2016-03-01 04:29:59 -08003908 EXPECT_FALSE(media_config.video.enable_cpu_overuse_detection);
nisse51542be2016-02-12 02:27:06 -08003909}
3910
3911// This test verifies that the disable_prerenderer_smoothing flag is
nisse528b7932017-05-08 03:21:43 -07003912// propagated from RTCConfiguration to the PeerConnection.
nisse51542be2016-02-12 02:27:06 -08003913TEST_F(PeerConnectionMediaConfigTest, TestDisablePrerendererSmoothingTrue) {
3914 PeerConnectionInterface::RTCConfiguration config;
3915 FakeConstraints constraints;
3916
Niels Möller71bdda02016-03-31 12:59:59 +02003917 config.set_prerenderer_smoothing(false);
nisse51542be2016-02-12 02:27:06 -08003918 const cricket::MediaConfig& media_config =
3919 TestCreatePeerConnection(config, &constraints);
3920
nisse0db023a2016-03-01 04:29:59 -08003921 EXPECT_TRUE(media_config.video.disable_prerenderer_smoothing);
3922}
3923
3924// This test verifies the suspend below min bitrate constraint is
nisse528b7932017-05-08 03:21:43 -07003925// recognized and passed to the PeerConnection.
nisse0db023a2016-03-01 04:29:59 -08003926TEST_F(PeerConnectionMediaConfigTest,
3927 TestSuspendBelowMinBitrateConstraintTrue) {
3928 PeerConnectionInterface::RTCConfiguration config;
3929 FakeConstraints constraints;
3930
3931 constraints.AddOptional(
3932 webrtc::MediaConstraintsInterface::kEnableVideoSuspendBelowMinBitrate,
3933 true);
3934 const cricket::MediaConfig media_config =
3935 TestCreatePeerConnection(config, &constraints);
3936
3937 EXPECT_TRUE(media_config.video.suspend_below_min_bitrate);
nisse51542be2016-02-12 02:27:06 -08003938}
3939
deadbeef293e9262017-01-11 12:28:30 -08003940// Tests a few random fields being different.
3941TEST(RTCConfigurationTest, ComparisonOperators) {
3942 PeerConnectionInterface::RTCConfiguration a;
3943 PeerConnectionInterface::RTCConfiguration b;
3944 EXPECT_EQ(a, b);
3945
3946 PeerConnectionInterface::RTCConfiguration c;
3947 c.servers.push_back(PeerConnectionInterface::IceServer());
3948 EXPECT_NE(a, c);
3949
3950 PeerConnectionInterface::RTCConfiguration d;
3951 d.type = PeerConnectionInterface::kRelay;
3952 EXPECT_NE(a, d);
3953
3954 PeerConnectionInterface::RTCConfiguration e;
3955 e.audio_jitter_buffer_max_packets = 5;
3956 EXPECT_NE(a, e);
3957
3958 PeerConnectionInterface::RTCConfiguration f;
3959 f.ice_connection_receiving_timeout = 1337;
3960 EXPECT_NE(a, f);
3961
3962 PeerConnectionInterface::RTCConfiguration g;
3963 g.disable_ipv6 = true;
3964 EXPECT_NE(a, g);
3965
3966 PeerConnectionInterface::RTCConfiguration h(
3967 PeerConnectionInterface::RTCConfigurationType::kAggressive);
3968 EXPECT_NE(a, h);
3969}