blob: d0e42621cffdeab915e6e4e9fbef0821dcd5b749 [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
kwibergd1fe2812016-04-27 06:47:29 -070011#include <memory>
deadbeef3edec7c2016-12-10 11:44:26 -080012#include <sstream>
henrike@webrtc.org28e20752013-07-10 00:45:36 +000013#include <string>
kwiberg0eb15ed2015-12-17 03:04:15 -080014#include <utility>
henrike@webrtc.org28e20752013-07-10 00:45:36 +000015
kwiberg087bd342017-02-10 08:15:44 -080016#include "webrtc/api/audio_codecs/builtin_audio_decoder_factory.h"
ossueb1fde42017-05-02 06:46:30 -070017#include "webrtc/api/audio_codecs/builtin_audio_encoder_factory.h"
Henrik Kjellander15583c12016-02-10 10:53:12 +010018#include "webrtc/api/jsepsessiondescription.h"
Henrik Kjellander15583c12016-02-10 10:53:12 +010019#include "webrtc/api/mediastreaminterface.h"
Henrik Kjellander15583c12016-02-10 10:53:12 +010020#include "webrtc/api/peerconnectioninterface.h"
21#include "webrtc/api/rtpreceiverinterface.h"
22#include "webrtc/api/rtpsenderinterface.h"
Henrik Kjellander15583c12016-02-10 10:53:12 +010023#include "webrtc/api/test/fakeconstraints.h"
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000024#include "webrtc/base/gunit.h"
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000025#include "webrtc/base/ssladapter.h"
26#include "webrtc/base/sslstreamadapter.h"
27#include "webrtc/base/stringutils.h"
28#include "webrtc/base/thread.h"
deadbeef9a6f4d42017-05-15 19:43:33 -070029#include "webrtc/base/virtualsocketserver.h"
kjellandera96e2d72016-02-04 23:52:28 -080030#include "webrtc/media/base/fakevideocapturer.h"
deadbeef953c2ce2017-01-09 14:53:41 -080031#include "webrtc/media/sctp/sctptransportinternal.h"
Taylor Brandstettera1c30352016-05-13 08:15:11 -070032#include "webrtc/p2p/base/fakeportallocator.h"
ossu7bb87ee2017-01-23 04:56:25 -080033#include "webrtc/pc/audiotrack.h"
kjellander@webrtc.org9b8df252016-02-12 06:47:59 +010034#include "webrtc/pc/mediasession.h"
ossu7bb87ee2017-01-23 04:56:25 -080035#include "webrtc/pc/mediastream.h"
36#include "webrtc/pc/peerconnection.h"
37#include "webrtc/pc/streamcollection.h"
38#include "webrtc/pc/test/fakertccertificategenerator.h"
39#include "webrtc/pc/test/fakevideotracksource.h"
40#include "webrtc/pc/test/mockpeerconnectionobservers.h"
41#include "webrtc/pc/test/testsdpstrings.h"
42#include "webrtc/pc/videocapturertracksource.h"
43#include "webrtc/pc/videotrack.h"
kwibergac9f8762016-09-30 22:29:43 -070044#include "webrtc/test/gmock.h"
45
46#ifdef WEBRTC_ANDROID
ossu7bb87ee2017-01-23 04:56:25 -080047#include "webrtc/pc/test/androidtestinitializer.h"
kwibergac9f8762016-09-30 22:29:43 -070048#endif
henrike@webrtc.org28e20752013-07-10 00:45:36 +000049
50static const char kStreamLabel1[] = "local_stream_1";
51static const char kStreamLabel2[] = "local_stream_2";
52static const char kStreamLabel3[] = "local_stream_3";
53static const int kDefaultStunPort = 3478;
54static const char kStunAddressOnly[] = "stun:address";
55static const char kStunInvalidPort[] = "stun:address:-1";
56static const char kStunAddressPortAndMore1[] = "stun:address:port:more";
57static const char kStunAddressPortAndMore2[] = "stun:address:port more";
58static const char kTurnIceServerUri[] = "turn:user@turn.example.org";
59static const char kTurnUsername[] = "user";
60static const char kTurnPassword[] = "password";
61static const char kTurnHostname[] = "turn.example.org";
Peter Boström0c4e06b2015-10-07 12:23:21 +020062static const uint32_t kTimeout = 10000U;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000063
deadbeefab9b2d12015-10-14 11:33:11 -070064static const char kStreams[][8] = {"stream1", "stream2"};
65static const char kAudioTracks[][32] = {"audiotrack0", "audiotrack1"};
66static const char kVideoTracks[][32] = {"videotrack0", "videotrack1"};
67
deadbeef5e97fb52015-10-15 12:49:08 -070068static const char kRecvonly[] = "recvonly";
69static const char kSendrecv[] = "sendrecv";
70
deadbeefab9b2d12015-10-14 11:33:11 -070071// Reference SDP with a MediaStream with label "stream1" and audio track with
72// id "audio_1" and a video track with id "video_1;
73static const char kSdpStringWithStream1[] =
74 "v=0\r\n"
75 "o=- 0 0 IN IP4 127.0.0.1\r\n"
76 "s=-\r\n"
77 "t=0 0\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -080078 "m=audio 1 RTP/AVPF 103\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -070079 "a=ice-ufrag:e5785931\r\n"
80 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
81 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
82 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -070083 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -070084 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -080085 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -070086 "a=rtpmap:103 ISAC/16000\r\n"
87 "a=ssrc:1 cname:stream1\r\n"
88 "a=ssrc:1 mslabel:stream1\r\n"
89 "a=ssrc:1 label:audiotrack0\r\n"
90 "m=video 1 RTP/AVPF 120\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -080091 "a=ice-ufrag:e5785931\r\n"
92 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
93 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
94 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -070095 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -070096 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -080097 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -070098 "a=rtpmap:120 VP8/90000\r\n"
99 "a=ssrc:2 cname:stream1\r\n"
100 "a=ssrc:2 mslabel:stream1\r\n"
101 "a=ssrc:2 label:videotrack0\r\n";
102
zhihuang81c3a032016-11-17 12:06:24 -0800103// Reference SDP with a MediaStream with label "stream1" and audio track with
104// id "audio_1";
105static const char kSdpStringWithStream1AudioTrackOnly[] =
106 "v=0\r\n"
107 "o=- 0 0 IN IP4 127.0.0.1\r\n"
108 "s=-\r\n"
109 "t=0 0\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800110 "m=audio 1 RTP/AVPF 103\r\n"
zhihuang81c3a032016-11-17 12:06:24 -0800111 "a=ice-ufrag:e5785931\r\n"
112 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
113 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
114 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
zhihuang81c3a032016-11-17 12:06:24 -0800115 "a=mid:audio\r\n"
116 "a=sendrecv\r\n"
117 "a=rtpmap:103 ISAC/16000\r\n"
118 "a=ssrc:1 cname:stream1\r\n"
119 "a=ssrc:1 mslabel:stream1\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800120 "a=ssrc:1 label:audiotrack0\r\n"
121 "a=rtcp-mux\r\n";
zhihuang81c3a032016-11-17 12:06:24 -0800122
deadbeefab9b2d12015-10-14 11:33:11 -0700123// Reference SDP with two MediaStreams with label "stream1" and "stream2. Each
124// MediaStreams have one audio track and one video track.
125// This uses MSID.
126static const char kSdpStringWithStream1And2[] =
127 "v=0\r\n"
128 "o=- 0 0 IN IP4 127.0.0.1\r\n"
129 "s=-\r\n"
130 "t=0 0\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800131 "a=msid-semantic: WMS stream1 stream2\r\n"
132 "m=audio 1 RTP/AVPF 103\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700133 "a=ice-ufrag:e5785931\r\n"
134 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
135 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
136 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700137 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700138 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800139 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700140 "a=rtpmap:103 ISAC/16000\r\n"
141 "a=ssrc:1 cname:stream1\r\n"
142 "a=ssrc:1 msid:stream1 audiotrack0\r\n"
143 "a=ssrc:3 cname:stream2\r\n"
144 "a=ssrc:3 msid:stream2 audiotrack1\r\n"
145 "m=video 1 RTP/AVPF 120\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800146 "a=ice-ufrag:e5785931\r\n"
147 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
148 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
149 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700150 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700151 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800152 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700153 "a=rtpmap:120 VP8/0\r\n"
154 "a=ssrc:2 cname:stream1\r\n"
155 "a=ssrc:2 msid:stream1 videotrack0\r\n"
156 "a=ssrc:4 cname:stream2\r\n"
157 "a=ssrc:4 msid:stream2 videotrack1\r\n";
158
159// Reference SDP without MediaStreams. Msid is not supported.
160static const char kSdpStringWithoutStreams[] =
161 "v=0\r\n"
162 "o=- 0 0 IN IP4 127.0.0.1\r\n"
163 "s=-\r\n"
164 "t=0 0\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800165 "m=audio 1 RTP/AVPF 103\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700166 "a=ice-ufrag:e5785931\r\n"
167 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
168 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
169 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700170 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700171 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800172 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700173 "a=rtpmap:103 ISAC/16000\r\n"
174 "m=video 1 RTP/AVPF 120\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800175 "a=ice-ufrag:e5785931\r\n"
176 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
177 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
178 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700179 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700180 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800181 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700182 "a=rtpmap:120 VP8/90000\r\n";
183
184// Reference SDP without MediaStreams. Msid is supported.
185static const char kSdpStringWithMsidWithoutStreams[] =
186 "v=0\r\n"
187 "o=- 0 0 IN IP4 127.0.0.1\r\n"
188 "s=-\r\n"
189 "t=0 0\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800190 "a=msid-semantic: WMS\r\n"
191 "m=audio 1 RTP/AVPF 103\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700192 "a=ice-ufrag:e5785931\r\n"
193 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
194 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
195 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700196 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700197 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800198 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700199 "a=rtpmap:103 ISAC/16000\r\n"
200 "m=video 1 RTP/AVPF 120\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800201 "a=ice-ufrag:e5785931\r\n"
202 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
203 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
204 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700205 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700206 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800207 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700208 "a=rtpmap:120 VP8/90000\r\n";
209
210// Reference SDP without MediaStreams and audio only.
211static const char kSdpStringWithoutStreamsAudioOnly[] =
212 "v=0\r\n"
213 "o=- 0 0 IN IP4 127.0.0.1\r\n"
214 "s=-\r\n"
215 "t=0 0\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800216 "m=audio 1 RTP/AVPF 103\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700217 "a=ice-ufrag:e5785931\r\n"
218 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
219 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
220 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700221 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700222 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800223 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700224 "a=rtpmap:103 ISAC/16000\r\n";
225
226// Reference SENDONLY SDP without MediaStreams. Msid is not supported.
227static const char kSdpStringSendOnlyWithoutStreams[] =
228 "v=0\r\n"
229 "o=- 0 0 IN IP4 127.0.0.1\r\n"
230 "s=-\r\n"
231 "t=0 0\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800232 "m=audio 1 RTP/AVPF 103\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700233 "a=ice-ufrag:e5785931\r\n"
234 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
235 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
236 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700237 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700238 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700239 "a=sendonly\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800240 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700241 "a=rtpmap:103 ISAC/16000\r\n"
242 "m=video 1 RTP/AVPF 120\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800243 "a=ice-ufrag:e5785931\r\n"
244 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
245 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
246 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700247 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700248 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700249 "a=sendonly\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800250 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700251 "a=rtpmap:120 VP8/90000\r\n";
252
253static const char kSdpStringInit[] =
254 "v=0\r\n"
255 "o=- 0 0 IN IP4 127.0.0.1\r\n"
256 "s=-\r\n"
257 "t=0 0\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700258 "a=msid-semantic: WMS\r\n";
259
260static const char kSdpStringAudio[] =
261 "m=audio 1 RTP/AVPF 103\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800262 "a=ice-ufrag:e5785931\r\n"
263 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
264 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
265 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700266 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700267 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800268 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700269 "a=rtpmap:103 ISAC/16000\r\n";
270
271static const char kSdpStringVideo[] =
272 "m=video 1 RTP/AVPF 120\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800273 "a=ice-ufrag:e5785931\r\n"
274 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
275 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
276 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700277 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700278 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800279 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700280 "a=rtpmap:120 VP8/90000\r\n";
281
282static const char kSdpStringMs1Audio0[] =
283 "a=ssrc:1 cname:stream1\r\n"
284 "a=ssrc:1 msid:stream1 audiotrack0\r\n";
285
286static const char kSdpStringMs1Video0[] =
287 "a=ssrc:2 cname:stream1\r\n"
288 "a=ssrc:2 msid:stream1 videotrack0\r\n";
289
290static const char kSdpStringMs1Audio1[] =
291 "a=ssrc:3 cname:stream1\r\n"
292 "a=ssrc:3 msid:stream1 audiotrack1\r\n";
293
294static const char kSdpStringMs1Video1[] =
295 "a=ssrc:4 cname:stream1\r\n"
296 "a=ssrc:4 msid:stream1 videotrack1\r\n";
297
deadbeef8662f942017-01-20 21:20:51 -0800298static const char kDtlsSdesFallbackSdp[] =
299 "v=0\r\n"
300 "o=xxxxxx 7 2 IN IP4 0.0.0.0\r\n"
301 "s=-\r\n"
302 "c=IN IP4 0.0.0.0\r\n"
303 "t=0 0\r\n"
304 "a=group:BUNDLE audio\r\n"
305 "a=msid-semantic: WMS\r\n"
306 "m=audio 1 RTP/SAVPF 0\r\n"
307 "a=sendrecv\r\n"
308 "a=rtcp-mux\r\n"
309 "a=mid:audio\r\n"
310 "a=ssrc:1 cname:stream1\r\n"
311 "a=ssrc:1 mslabel:stream1\r\n"
312 "a=ssrc:1 label:audiotrack0\r\n"
313 "a=ice-ufrag:e5785931\r\n"
314 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
315 "a=rtpmap:0 pcmu/8000\r\n"
316 "a=fingerprint:sha-1 "
317 "4A:AD:B9:B1:3F:82:18:3B:54:02:12:DF:3E:5D:49:6B:19:E5:7C:AB\r\n"
318 "a=setup:actpass\r\n"
319 "a=crypto:1 AES_CM_128_HMAC_SHA1_32 "
320 "inline:NzB4d1BINUAvLEw6UzF3WSJ+PSdFcGdUJShpX1Zj|2^20|1:32 "
321 "dummy_session_params\r\n";
322
perkjd61bf802016-03-24 03:16:19 -0700323using ::testing::Exactly;
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700324using cricket::StreamParams;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000325using webrtc::AudioSourceInterface;
deadbeefab9b2d12015-10-14 11:33:11 -0700326using webrtc::AudioTrack;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000327using webrtc::AudioTrackInterface;
328using webrtc::DataBuffer;
329using webrtc::DataChannelInterface;
330using webrtc::FakeConstraints;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000331using webrtc::IceCandidateInterface;
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700332using webrtc::JsepSessionDescription;
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],
461 webrtc::FakeVideoTrackSource::Create()));
462 stream->AddTrack(video_track);
463 }
deadbeefab9b2d12015-10-14 11:33:11 -0700464
465 local_collection->AddStream(stream);
466 }
467 return local_collection;
468}
469
470// Check equality of StreamCollections.
471bool CompareStreamCollections(StreamCollectionInterface* s1,
472 StreamCollectionInterface* s2) {
473 if (s1 == nullptr || s2 == nullptr || s1->count() != s2->count()) {
474 return false;
475 }
476
477 for (size_t i = 0; i != s1->count(); ++i) {
478 if (s1->at(i)->label() != s2->at(i)->label()) {
479 return false;
480 }
481 webrtc::AudioTrackVector audio_tracks1 = s1->at(i)->GetAudioTracks();
482 webrtc::AudioTrackVector audio_tracks2 = s2->at(i)->GetAudioTracks();
483 webrtc::VideoTrackVector video_tracks1 = s1->at(i)->GetVideoTracks();
484 webrtc::VideoTrackVector video_tracks2 = s2->at(i)->GetVideoTracks();
485
486 if (audio_tracks1.size() != audio_tracks2.size()) {
487 return false;
488 }
489 for (size_t j = 0; j != audio_tracks1.size(); ++j) {
490 if (audio_tracks1[j]->id() != audio_tracks2[j]->id()) {
491 return false;
492 }
493 }
494 if (video_tracks1.size() != video_tracks2.size()) {
495 return false;
496 }
497 for (size_t j = 0; j != video_tracks1.size(); ++j) {
498 if (video_tracks1[j]->id() != video_tracks2[j]->id()) {
499 return false;
500 }
501 }
502 }
503 return true;
504}
505
perkjd61bf802016-03-24 03:16:19 -0700506// Helper class to test Observer.
507class MockTrackObserver : public ObserverInterface {
508 public:
509 explicit MockTrackObserver(NotifierInterface* notifier)
510 : notifier_(notifier) {
511 notifier_->RegisterObserver(this);
512 }
513
514 ~MockTrackObserver() { Unregister(); }
515
516 void Unregister() {
517 if (notifier_) {
518 notifier_->UnregisterObserver(this);
519 notifier_ = nullptr;
520 }
521 }
522
523 MOCK_METHOD0(OnChanged, void());
524
525 private:
526 NotifierInterface* notifier_;
527};
528
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000529class MockPeerConnectionObserver : public PeerConnectionObserver {
530 public:
deadbeefab9b2d12015-10-14 11:33:11 -0700531 MockPeerConnectionObserver() : remote_streams_(StreamCollection::Create()) {}
Henrik Kjellander3fe372d2016-05-12 08:10:52 +0200532 virtual ~MockPeerConnectionObserver() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000533 }
534 void SetPeerConnectionInterface(PeerConnectionInterface* pc) {
535 pc_ = pc;
536 if (pc) {
537 state_ = pc_->signaling_state();
538 }
539 }
nisseef8b61e2016-04-29 06:09:15 -0700540 void OnSignalingChange(
541 PeerConnectionInterface::SignalingState new_state) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000542 EXPECT_EQ(pc_->signaling_state(), new_state);
543 state_ = new_state;
544 }
deadbeefab9b2d12015-10-14 11:33:11 -0700545
546 MediaStreamInterface* RemoteStream(const std::string& label) {
547 return remote_streams_->find(label);
548 }
549 StreamCollectionInterface* remote_streams() const { return remote_streams_; }
Taylor Brandstetter98cde262016-05-31 13:02:21 -0700550 void OnAddStream(rtc::scoped_refptr<MediaStreamInterface> stream) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000551 last_added_stream_ = stream;
deadbeefab9b2d12015-10-14 11:33:11 -0700552 remote_streams_->AddStream(stream);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000553 }
Taylor Brandstetter98cde262016-05-31 13:02:21 -0700554 void OnRemoveStream(
555 rtc::scoped_refptr<MediaStreamInterface> stream) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000556 last_removed_stream_ = stream;
deadbeefab9b2d12015-10-14 11:33:11 -0700557 remote_streams_->RemoveStream(stream);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000558 }
perkjdfb769d2016-02-09 03:09:43 -0800559 void OnRenegotiationNeeded() override { renegotiation_needed_ = true; }
Taylor Brandstetter98cde262016-05-31 13:02:21 -0700560 void OnDataChannel(
561 rtc::scoped_refptr<DataChannelInterface> data_channel) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000562 last_datachannel_ = data_channel;
563 }
564
perkjdfb769d2016-02-09 03:09:43 -0800565 void OnIceConnectionChange(
566 PeerConnectionInterface::IceConnectionState new_state) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000567 EXPECT_EQ(pc_->ice_connection_state(), new_state);
zhihuang81c3a032016-11-17 12:06:24 -0800568 callback_triggered_ = true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000569 }
perkjdfb769d2016-02-09 03:09:43 -0800570 void OnIceGatheringChange(
571 PeerConnectionInterface::IceGatheringState new_state) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000572 EXPECT_EQ(pc_->ice_gathering_state(), new_state);
perkjdfb769d2016-02-09 03:09:43 -0800573 ice_complete_ = new_state == PeerConnectionInterface::kIceGatheringComplete;
zhihuang81c3a032016-11-17 12:06:24 -0800574 callback_triggered_ = true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000575 }
perkjdfb769d2016-02-09 03:09:43 -0800576 void OnIceCandidate(const webrtc::IceCandidateInterface* candidate) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000577 EXPECT_NE(PeerConnectionInterface::kIceGatheringNew,
578 pc_->ice_gathering_state());
579
580 std::string sdp;
581 EXPECT_TRUE(candidate->ToString(&sdp));
582 EXPECT_LT(0u, sdp.size());
583 last_candidate_.reset(webrtc::CreateIceCandidate(candidate->sdp_mid(),
584 candidate->sdp_mline_index(), sdp, NULL));
585 EXPECT_TRUE(last_candidate_.get() != NULL);
zhihuang81c3a032016-11-17 12:06:24 -0800586 callback_triggered_ = true;
zhihuang29ff8442016-07-27 11:07:25 -0700587 }
588
589 void OnIceCandidatesRemoved(
590 const std::vector<cricket::Candidate>& candidates) override {
zhihuang81c3a032016-11-17 12:06:24 -0800591 callback_triggered_ = true;
zhihuang29ff8442016-07-27 11:07:25 -0700592 }
593
594 void OnIceConnectionReceivingChange(bool receiving) override {
zhihuang81c3a032016-11-17 12:06:24 -0800595 callback_triggered_ = true;
596 }
597
zhihuangc63b8942016-12-02 15:41:10 -0800598 void OnAddTrack(
599 rtc::scoped_refptr<webrtc::RtpReceiverInterface> receiver,
600 const std::vector<rtc::scoped_refptr<webrtc::MediaStreamInterface>>&
601 streams) override {
zhihuang81c3a032016-11-17 12:06:24 -0800602 EXPECT_TRUE(receiver != nullptr);
603 num_added_tracks_++;
604 last_added_track_label_ = receiver->id();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000605 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000606
607 // Returns the label of the last added stream.
608 // Empty string if no stream have been added.
609 std::string GetLastAddedStreamLabel() {
610 if (last_added_stream_.get())
611 return last_added_stream_->label();
612 return "";
613 }
614 std::string GetLastRemovedStreamLabel() {
615 if (last_removed_stream_.get())
616 return last_removed_stream_->label();
617 return "";
618 }
619
zhihuang9763d562016-08-05 11:14:50 -0700620 rtc::scoped_refptr<PeerConnectionInterface> pc_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000621 PeerConnectionInterface::SignalingState state_;
kwibergd1fe2812016-04-27 06:47:29 -0700622 std::unique_ptr<IceCandidateInterface> last_candidate_;
zhihuang9763d562016-08-05 11:14:50 -0700623 rtc::scoped_refptr<DataChannelInterface> last_datachannel_;
deadbeefab9b2d12015-10-14 11:33:11 -0700624 rtc::scoped_refptr<StreamCollection> remote_streams_;
625 bool renegotiation_needed_ = false;
626 bool ice_complete_ = false;
zhihuang81c3a032016-11-17 12:06:24 -0800627 bool callback_triggered_ = false;
628 int num_added_tracks_ = 0;
629 std::string last_added_track_label_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000630
631 private:
zhihuang9763d562016-08-05 11:14:50 -0700632 rtc::scoped_refptr<MediaStreamInterface> last_added_stream_;
633 rtc::scoped_refptr<MediaStreamInterface> last_removed_stream_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000634};
635
636} // namespace
deadbeefab9b2d12015-10-14 11:33:11 -0700637
nisse528b7932017-05-08 03:21:43 -0700638// The PeerConnectionMediaConfig tests below verify that configuration and
639// constraints are propagated into the PeerConnection's MediaConfig. These
640// settings are intended for MediaChannel constructors, but that is not
641// exercised by these unittest.
zhihuang29ff8442016-07-27 11:07:25 -0700642class PeerConnectionFactoryForTest : public webrtc::PeerConnectionFactory {
643 public:
kwiberg1e4e8cb2017-01-31 01:48:08 -0800644 PeerConnectionFactoryForTest()
645 : webrtc::PeerConnectionFactory(
646 webrtc::CreateBuiltinAudioEncoderFactory(),
647 webrtc::CreateBuiltinAudioDecoderFactory()) {}
648
zhihuang29ff8442016-07-27 11:07:25 -0700649 cricket::TransportController* CreateTransportController(
Honghai Zhangbfd398c2016-08-30 22:07:42 -0700650 cricket::PortAllocator* port_allocator,
651 bool redetermine_role_on_ice_restart) override {
zhihuang29ff8442016-07-27 11:07:25 -0700652 transport_controller = new cricket::TransportController(
Honghai Zhangbfd398c2016-08-30 22:07:42 -0700653 rtc::Thread::Current(), rtc::Thread::Current(), port_allocator,
deadbeef7914b8c2017-04-21 03:23:33 -0700654 redetermine_role_on_ice_restart, rtc::CryptoOptions());
zhihuang29ff8442016-07-27 11:07:25 -0700655 return transport_controller;
656 }
657
658 cricket::TransportController* transport_controller;
zhihuang29ff8442016-07-27 11:07:25 -0700659};
660
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000661class PeerConnectionInterfaceTest : public testing::Test {
662 protected:
deadbeef9a6f4d42017-05-15 19:43:33 -0700663 PeerConnectionInterfaceTest()
deadbeef98e186c2017-05-16 18:00:06 -0700664 : vss_(new rtc::VirtualSocketServer()), main_(vss_.get()) {
phoglund37ebcf02016-01-08 05:04:57 -0800665#ifdef WEBRTC_ANDROID
666 webrtc::InitializeAndroidObjects();
667#endif
668 }
669
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000670 virtual void SetUp() {
671 pc_factory_ = webrtc::CreatePeerConnectionFactory(
danilchape9021a32016-05-17 01:52:02 -0700672 rtc::Thread::Current(), rtc::Thread::Current(), rtc::Thread::Current(),
673 nullptr, nullptr, nullptr);
674 ASSERT_TRUE(pc_factory_);
zhihuang29ff8442016-07-27 11:07:25 -0700675 pc_factory_for_test_ =
676 new rtc::RefCountedObject<PeerConnectionFactoryForTest>();
677 pc_factory_for_test_->Initialize();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000678 }
679
680 void CreatePeerConnection() {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700681 CreatePeerConnection(PeerConnectionInterface::RTCConfiguration(), nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000682 }
683
deadbeef293e9262017-01-11 12:28:30 -0800684 // DTLS does not work in a loopback call, so is disabled for most of the
685 // tests in this file.
686 void CreatePeerConnectionWithoutDtls() {
687 FakeConstraints no_dtls_constraints;
688 no_dtls_constraints.AddMandatory(
689 webrtc::MediaConstraintsInterface::kEnableDtlsSrtp, false);
690
691 CreatePeerConnection(PeerConnectionInterface::RTCConfiguration(),
692 &no_dtls_constraints);
693 }
694
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000695 void CreatePeerConnection(webrtc::MediaConstraintsInterface* constraints) {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700696 CreatePeerConnection(PeerConnectionInterface::RTCConfiguration(),
697 constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000698 }
699
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700700 void CreatePeerConnectionWithIceTransportsType(
701 PeerConnectionInterface::IceTransportsType type) {
702 PeerConnectionInterface::RTCConfiguration config;
703 config.type = type;
704 return CreatePeerConnection(config, nullptr);
705 }
706
707 void CreatePeerConnectionWithIceServer(const std::string& uri,
708 const std::string& password) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800709 PeerConnectionInterface::RTCConfiguration config;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000710 PeerConnectionInterface::IceServer server;
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700711 server.uri = uri;
712 server.password = password;
713 config.servers.push_back(server);
714 CreatePeerConnection(config, nullptr);
715 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000716
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700717 void CreatePeerConnection(PeerConnectionInterface::RTCConfiguration config,
718 webrtc::MediaConstraintsInterface* constraints) {
kwibergd1fe2812016-04-27 06:47:29 -0700719 std::unique_ptr<cricket::FakePortAllocator> port_allocator(
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800720 new cricket::FakePortAllocator(rtc::Thread::Current(), nullptr));
721 port_allocator_ = port_allocator.get();
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000722
deadbeef1dcb1642017-03-29 21:08:16 -0700723 // Create certificate generator unless DTLS constraint is explicitly set to
724 // false.
Henrik Boströmd79599d2016-06-01 13:58:50 +0200725 std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator;
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000726 bool dtls;
727 if (FindConstraint(constraints,
728 webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
729 &dtls,
Henrik Boström5e56c592015-08-11 10:33:13 +0200730 nullptr) && dtls) {
deadbeef8662f942017-01-20 21:20:51 -0800731 fake_certificate_generator_ = new FakeRTCCertificateGenerator();
732 cert_generator.reset(fake_certificate_generator_);
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000733 }
Henrik Boströmd79599d2016-06-01 13:58:50 +0200734 pc_ = pc_factory_->CreatePeerConnection(
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800735 config, constraints, std::move(port_allocator),
Henrik Boströmd79599d2016-06-01 13:58:50 +0200736 std::move(cert_generator), &observer_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000737 ASSERT_TRUE(pc_.get() != NULL);
738 observer_.SetPeerConnectionInterface(pc_.get());
739 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
740 }
741
deadbeef0a6c4ca2015-10-06 11:38:28 -0700742 void CreatePeerConnectionExpectFail(const std::string& uri) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800743 PeerConnectionInterface::RTCConfiguration config;
deadbeef0a6c4ca2015-10-06 11:38:28 -0700744 PeerConnectionInterface::IceServer server;
deadbeef0a6c4ca2015-10-06 11:38:28 -0700745 server.uri = uri;
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800746 config.servers.push_back(server);
deadbeef0a6c4ca2015-10-06 11:38:28 -0700747
zhihuang9763d562016-08-05 11:14:50 -0700748 rtc::scoped_refptr<PeerConnectionInterface> pc;
hbosd7973cc2016-05-27 06:08:53 -0700749 pc = pc_factory_->CreatePeerConnection(config, nullptr, nullptr, nullptr,
750 &observer_);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800751 EXPECT_EQ(nullptr, pc);
deadbeef0a6c4ca2015-10-06 11:38:28 -0700752 }
753
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000754 void CreatePeerConnectionWithDifferentConfigurations() {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700755 CreatePeerConnectionWithIceServer(kStunAddressOnly, "");
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800756 EXPECT_EQ(1u, port_allocator_->stun_servers().size());
757 EXPECT_EQ(0u, port_allocator_->turn_servers().size());
758 EXPECT_EQ("address", port_allocator_->stun_servers().begin()->hostname());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000759 EXPECT_EQ(kDefaultStunPort,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800760 port_allocator_->stun_servers().begin()->port());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000761
deadbeef0a6c4ca2015-10-06 11:38:28 -0700762 CreatePeerConnectionExpectFail(kStunInvalidPort);
763 CreatePeerConnectionExpectFail(kStunAddressPortAndMore1);
764 CreatePeerConnectionExpectFail(kStunAddressPortAndMore2);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000765
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700766 CreatePeerConnectionWithIceServer(kTurnIceServerUri, kTurnPassword);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800767 EXPECT_EQ(0u, port_allocator_->stun_servers().size());
768 EXPECT_EQ(1u, port_allocator_->turn_servers().size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000769 EXPECT_EQ(kTurnUsername,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800770 port_allocator_->turn_servers()[0].credentials.username);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000771 EXPECT_EQ(kTurnPassword,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800772 port_allocator_->turn_servers()[0].credentials.password);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000773 EXPECT_EQ(kTurnHostname,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800774 port_allocator_->turn_servers()[0].ports[0].address.hostname());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000775 }
776
777 void ReleasePeerConnection() {
778 pc_ = NULL;
779 observer_.SetPeerConnectionInterface(NULL);
780 }
781
deadbeefab9b2d12015-10-14 11:33:11 -0700782 void AddVideoStream(const std::string& label) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000783 // Create a local stream.
zhihuang9763d562016-08-05 11:14:50 -0700784 rtc::scoped_refptr<MediaStreamInterface> stream(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000785 pc_factory_->CreateLocalMediaStream(label));
zhihuang9763d562016-08-05 11:14:50 -0700786 rtc::scoped_refptr<VideoTrackSourceInterface> video_source(
deadbeef112b2e92017-02-10 20:13:37 -0800787 pc_factory_->CreateVideoSource(std::unique_ptr<cricket::VideoCapturer>(
788 new cricket::FakeVideoCapturer()),
789 NULL));
zhihuang9763d562016-08-05 11:14:50 -0700790 rtc::scoped_refptr<VideoTrackInterface> video_track(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000791 pc_factory_->CreateVideoTrack(label + "v0", video_source));
792 stream->AddTrack(video_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +0000793 EXPECT_TRUE(pc_->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000794 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
795 observer_.renegotiation_needed_ = false;
796 }
797
798 void AddVoiceStream(const std::string& label) {
799 // Create a local stream.
zhihuang9763d562016-08-05 11:14:50 -0700800 rtc::scoped_refptr<MediaStreamInterface> stream(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000801 pc_factory_->CreateLocalMediaStream(label));
zhihuang9763d562016-08-05 11:14:50 -0700802 rtc::scoped_refptr<AudioTrackInterface> audio_track(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000803 pc_factory_->CreateAudioTrack(label + "a0", NULL));
804 stream->AddTrack(audio_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +0000805 EXPECT_TRUE(pc_->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000806 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
807 observer_.renegotiation_needed_ = false;
808 }
809
810 void AddAudioVideoStream(const std::string& stream_label,
811 const std::string& audio_track_label,
812 const std::string& video_track_label) {
813 // Create a local stream.
zhihuang9763d562016-08-05 11:14:50 -0700814 rtc::scoped_refptr<MediaStreamInterface> stream(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000815 pc_factory_->CreateLocalMediaStream(stream_label));
zhihuang9763d562016-08-05 11:14:50 -0700816 rtc::scoped_refptr<AudioTrackInterface> audio_track(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000817 pc_factory_->CreateAudioTrack(
818 audio_track_label, static_cast<AudioSourceInterface*>(NULL)));
819 stream->AddTrack(audio_track.get());
zhihuang9763d562016-08-05 11:14:50 -0700820 rtc::scoped_refptr<VideoTrackInterface> video_track(
nisseaf510af2016-03-21 08:20:42 -0700821 pc_factory_->CreateVideoTrack(
deadbeef112b2e92017-02-10 20:13:37 -0800822 video_track_label, pc_factory_->CreateVideoSource(
823 std::unique_ptr<cricket::VideoCapturer>(
824 new cricket::FakeVideoCapturer()))));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000825 stream->AddTrack(video_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +0000826 EXPECT_TRUE(pc_->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000827 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
828 observer_.renegotiation_needed_ = false;
829 }
830
kwibergd1fe2812016-04-27 06:47:29 -0700831 bool DoCreateOfferAnswer(std::unique_ptr<SessionDescriptionInterface>* desc,
deadbeefc80741f2015-10-22 13:14:45 -0700832 bool offer,
833 MediaConstraintsInterface* constraints) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000834 rtc::scoped_refptr<MockCreateSessionDescriptionObserver>
835 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000836 MockCreateSessionDescriptionObserver>());
837 if (offer) {
deadbeefc80741f2015-10-22 13:14:45 -0700838 pc_->CreateOffer(observer, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000839 } else {
deadbeefc80741f2015-10-22 13:14:45 -0700840 pc_->CreateAnswer(observer, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000841 }
842 EXPECT_EQ_WAIT(true, observer->called(), kTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -0700843 *desc = observer->MoveDescription();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000844 return observer->result();
845 }
846
kwibergd1fe2812016-04-27 06:47:29 -0700847 bool DoCreateOffer(std::unique_ptr<SessionDescriptionInterface>* desc,
deadbeefc80741f2015-10-22 13:14:45 -0700848 MediaConstraintsInterface* constraints) {
849 return DoCreateOfferAnswer(desc, true, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000850 }
851
kwibergd1fe2812016-04-27 06:47:29 -0700852 bool DoCreateAnswer(std::unique_ptr<SessionDescriptionInterface>* desc,
deadbeefc80741f2015-10-22 13:14:45 -0700853 MediaConstraintsInterface* constraints) {
854 return DoCreateOfferAnswer(desc, false, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000855 }
856
857 bool DoSetSessionDescription(SessionDescriptionInterface* desc, bool local) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000858 rtc::scoped_refptr<MockSetSessionDescriptionObserver>
859 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000860 MockSetSessionDescriptionObserver>());
861 if (local) {
862 pc_->SetLocalDescription(observer, desc);
863 } else {
864 pc_->SetRemoteDescription(observer, desc);
865 }
zhihuang29ff8442016-07-27 11:07:25 -0700866 if (pc_->signaling_state() != PeerConnectionInterface::kClosed) {
867 EXPECT_EQ_WAIT(true, observer->called(), kTimeout);
868 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000869 return observer->result();
870 }
871
872 bool DoSetLocalDescription(SessionDescriptionInterface* desc) {
873 return DoSetSessionDescription(desc, true);
874 }
875
876 bool DoSetRemoteDescription(SessionDescriptionInterface* desc) {
877 return DoSetSessionDescription(desc, false);
878 }
879
880 // Calls PeerConnection::GetStats and check the return value.
881 // It does not verify the values in the StatReports since a RTCP packet might
882 // be required.
883 bool DoGetStats(MediaStreamTrackInterface* track) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000884 rtc::scoped_refptr<MockStatsObserver> observer(
885 new rtc::RefCountedObject<MockStatsObserver>());
jiayl@webrtc.orgdb41b4d2014-03-03 21:30:06 +0000886 if (!pc_->GetStats(
887 observer, track, PeerConnectionInterface::kStatsOutputLevelStandard))
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000888 return false;
889 EXPECT_TRUE_WAIT(observer->called(), kTimeout);
890 return observer->called();
891 }
892
893 void InitiateCall() {
deadbeef293e9262017-01-11 12:28:30 -0800894 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000895 // Create a local stream with audio&video tracks.
896 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
897 CreateOfferReceiveAnswer();
898 }
899
900 // Verify that RTP Header extensions has been negotiated for audio and video.
901 void VerifyRemoteRtpHeaderExtensions() {
902 const cricket::MediaContentDescription* desc =
903 cricket::GetFirstAudioContentDescription(
904 pc_->remote_description()->description());
905 ASSERT_TRUE(desc != NULL);
906 EXPECT_GT(desc->rtp_header_extensions().size(), 0u);
907
908 desc = cricket::GetFirstVideoContentDescription(
909 pc_->remote_description()->description());
910 ASSERT_TRUE(desc != NULL);
911 EXPECT_GT(desc->rtp_header_extensions().size(), 0u);
912 }
913
914 void CreateOfferAsRemoteDescription() {
kwibergd1fe2812016-04-27 06:47:29 -0700915 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -0700916 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000917 std::string sdp;
918 EXPECT_TRUE(offer->ToString(&sdp));
919 SessionDescriptionInterface* remote_offer =
920 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
921 sdp, NULL);
922 EXPECT_TRUE(DoSetRemoteDescription(remote_offer));
923 EXPECT_EQ(PeerConnectionInterface::kHaveRemoteOffer, observer_.state_);
924 }
925
deadbeefab9b2d12015-10-14 11:33:11 -0700926 void CreateAndSetRemoteOffer(const std::string& sdp) {
927 SessionDescriptionInterface* remote_offer =
928 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
929 sdp, nullptr);
930 EXPECT_TRUE(DoSetRemoteDescription(remote_offer));
931 EXPECT_EQ(PeerConnectionInterface::kHaveRemoteOffer, observer_.state_);
932 }
933
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000934 void CreateAnswerAsLocalDescription() {
kwibergd1fe2812016-04-27 06:47:29 -0700935 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -0700936 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000937
938 // TODO(perkj): Currently SetLocalDescription fails if any parameters in an
939 // audio codec change, even if the parameter has nothing to do with
940 // receiving. Not all parameters are serialized to SDP.
941 // Since CreatePrAnswerAsLocalDescription serialize/deserialize
942 // the SessionDescription, it is necessary to do that here to in order to
943 // get ReceiveOfferCreatePrAnswerAndAnswer and RenegotiateAudioOnly to pass.
944 // https://code.google.com/p/webrtc/issues/detail?id=1356
945 std::string sdp;
946 EXPECT_TRUE(answer->ToString(&sdp));
947 SessionDescriptionInterface* new_answer =
948 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
949 sdp, NULL);
950 EXPECT_TRUE(DoSetLocalDescription(new_answer));
951 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
952 }
953
954 void CreatePrAnswerAsLocalDescription() {
kwibergd1fe2812016-04-27 06:47:29 -0700955 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -0700956 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000957
958 std::string sdp;
959 EXPECT_TRUE(answer->ToString(&sdp));
960 SessionDescriptionInterface* pr_answer =
961 webrtc::CreateSessionDescription(SessionDescriptionInterface::kPrAnswer,
962 sdp, NULL);
963 EXPECT_TRUE(DoSetLocalDescription(pr_answer));
964 EXPECT_EQ(PeerConnectionInterface::kHaveLocalPrAnswer, observer_.state_);
965 }
966
967 void CreateOfferReceiveAnswer() {
968 CreateOfferAsLocalDescription();
969 std::string sdp;
970 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
971 CreateAnswerAsRemoteDescription(sdp);
972 }
973
974 void CreateOfferAsLocalDescription() {
kwibergd1fe2812016-04-27 06:47:29 -0700975 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -0700976 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000977 // TODO(perkj): Currently SetLocalDescription fails if any parameters in an
978 // audio codec change, even if the parameter has nothing to do with
979 // receiving. Not all parameters are serialized to SDP.
980 // Since CreatePrAnswerAsLocalDescription serialize/deserialize
981 // the SessionDescription, it is necessary to do that here to in order to
982 // get ReceiveOfferCreatePrAnswerAndAnswer and RenegotiateAudioOnly to pass.
983 // https://code.google.com/p/webrtc/issues/detail?id=1356
984 std::string sdp;
985 EXPECT_TRUE(offer->ToString(&sdp));
986 SessionDescriptionInterface* new_offer =
987 webrtc::CreateSessionDescription(
988 SessionDescriptionInterface::kOffer,
989 sdp, NULL);
990
991 EXPECT_TRUE(DoSetLocalDescription(new_offer));
992 EXPECT_EQ(PeerConnectionInterface::kHaveLocalOffer, observer_.state_);
mallinath@webrtc.org68cbd012014-01-22 00:16:46 +0000993 // Wait for the ice_complete message, so that SDP will have candidates.
994 EXPECT_TRUE_WAIT(observer_.ice_complete_, kTimeout);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000995 }
996
deadbeefab9b2d12015-10-14 11:33:11 -0700997 void CreateAnswerAsRemoteDescription(const std::string& sdp) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000998 webrtc::JsepSessionDescription* answer = new webrtc::JsepSessionDescription(
999 SessionDescriptionInterface::kAnswer);
deadbeefab9b2d12015-10-14 11:33:11 -07001000 EXPECT_TRUE(answer->Initialize(sdp, NULL));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001001 EXPECT_TRUE(DoSetRemoteDescription(answer));
1002 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
1003 }
1004
deadbeefab9b2d12015-10-14 11:33:11 -07001005 void CreatePrAnswerAndAnswerAsRemoteDescription(const std::string& sdp) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001006 webrtc::JsepSessionDescription* pr_answer =
1007 new webrtc::JsepSessionDescription(
1008 SessionDescriptionInterface::kPrAnswer);
deadbeefab9b2d12015-10-14 11:33:11 -07001009 EXPECT_TRUE(pr_answer->Initialize(sdp, NULL));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001010 EXPECT_TRUE(DoSetRemoteDescription(pr_answer));
1011 EXPECT_EQ(PeerConnectionInterface::kHaveRemotePrAnswer, observer_.state_);
1012 webrtc::JsepSessionDescription* answer =
1013 new webrtc::JsepSessionDescription(
1014 SessionDescriptionInterface::kAnswer);
deadbeefab9b2d12015-10-14 11:33:11 -07001015 EXPECT_TRUE(answer->Initialize(sdp, NULL));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001016 EXPECT_TRUE(DoSetRemoteDescription(answer));
1017 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
1018 }
1019
1020 // Help function used for waiting until a the last signaled remote stream has
1021 // the same label as |stream_label|. In a few of the tests in this file we
1022 // answer with the same session description as we offer and thus we can
1023 // check if OnAddStream have been called with the same stream as we offer to
1024 // send.
1025 void WaitAndVerifyOnAddStream(const std::string& stream_label) {
1026 EXPECT_EQ_WAIT(stream_label, observer_.GetLastAddedStreamLabel(), kTimeout);
1027 }
1028
1029 // Creates an offer and applies it as a local session description.
1030 // Creates an answer with the same SDP an the offer but removes all lines
1031 // that start with a:ssrc"
1032 void CreateOfferReceiveAnswerWithoutSsrc() {
1033 CreateOfferAsLocalDescription();
1034 std::string sdp;
1035 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
1036 SetSsrcToZero(&sdp);
1037 CreateAnswerAsRemoteDescription(sdp);
1038 }
1039
deadbeefab9b2d12015-10-14 11:33:11 -07001040 // This function creates a MediaStream with label kStreams[0] and
1041 // |number_of_audio_tracks| and |number_of_video_tracks| tracks and the
1042 // corresponding SessionDescriptionInterface. The SessionDescriptionInterface
kwiberg2bbff992016-03-16 11:03:04 -07001043 // is returned and the MediaStream is stored in
deadbeefab9b2d12015-10-14 11:33:11 -07001044 // |reference_collection_|
kwibergd1fe2812016-04-27 06:47:29 -07001045 std::unique_ptr<SessionDescriptionInterface>
kwiberg2bbff992016-03-16 11:03:04 -07001046 CreateSessionDescriptionAndReference(size_t number_of_audio_tracks,
1047 size_t number_of_video_tracks) {
1048 EXPECT_LE(number_of_audio_tracks, 2u);
1049 EXPECT_LE(number_of_video_tracks, 2u);
deadbeefab9b2d12015-10-14 11:33:11 -07001050
1051 reference_collection_ = StreamCollection::Create();
1052 std::string sdp_ms1 = std::string(kSdpStringInit);
1053
1054 std::string mediastream_label = kStreams[0];
1055
1056 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream(
1057 webrtc::MediaStream::Create(mediastream_label));
1058 reference_collection_->AddStream(stream);
1059
1060 if (number_of_audio_tracks > 0) {
1061 sdp_ms1 += std::string(kSdpStringAudio);
1062 sdp_ms1 += std::string(kSdpStringMs1Audio0);
1063 AddAudioTrack(kAudioTracks[0], stream);
1064 }
1065 if (number_of_audio_tracks > 1) {
1066 sdp_ms1 += kSdpStringMs1Audio1;
1067 AddAudioTrack(kAudioTracks[1], stream);
1068 }
1069
1070 if (number_of_video_tracks > 0) {
1071 sdp_ms1 += std::string(kSdpStringVideo);
1072 sdp_ms1 += std::string(kSdpStringMs1Video0);
1073 AddVideoTrack(kVideoTracks[0], stream);
1074 }
1075 if (number_of_video_tracks > 1) {
1076 sdp_ms1 += kSdpStringMs1Video1;
1077 AddVideoTrack(kVideoTracks[1], stream);
1078 }
1079
kwibergd1fe2812016-04-27 06:47:29 -07001080 return std::unique_ptr<SessionDescriptionInterface>(
kwiberg2bbff992016-03-16 11:03:04 -07001081 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
1082 sdp_ms1, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07001083 }
1084
1085 void AddAudioTrack(const std::string& track_id,
1086 MediaStreamInterface* stream) {
1087 rtc::scoped_refptr<webrtc::AudioTrackInterface> audio_track(
1088 webrtc::AudioTrack::Create(track_id, nullptr));
1089 ASSERT_TRUE(stream->AddTrack(audio_track));
1090 }
1091
1092 void AddVideoTrack(const std::string& track_id,
1093 MediaStreamInterface* stream) {
1094 rtc::scoped_refptr<webrtc::VideoTrackInterface> video_track(
nisseaf510af2016-03-21 08:20:42 -07001095 webrtc::VideoTrack::Create(track_id,
1096 webrtc::FakeVideoTrackSource::Create()));
deadbeefab9b2d12015-10-14 11:33:11 -07001097 ASSERT_TRUE(stream->AddTrack(video_track));
1098 }
1099
kwibergfd8be342016-05-14 19:44:11 -07001100 std::unique_ptr<SessionDescriptionInterface> CreateOfferWithOneAudioStream() {
deadbeef293e9262017-01-11 12:28:30 -08001101 CreatePeerConnectionWithoutDtls();
zhihuang8f65cdf2016-05-06 18:40:30 -07001102 AddVoiceStream(kStreamLabel1);
kwibergfd8be342016-05-14 19:44:11 -07001103 std::unique_ptr<SessionDescriptionInterface> offer;
zhihuang8f65cdf2016-05-06 18:40:30 -07001104 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
1105 return offer;
1106 }
1107
kwibergfd8be342016-05-14 19:44:11 -07001108 std::unique_ptr<SessionDescriptionInterface>
zhihuang8f65cdf2016-05-06 18:40:30 -07001109 CreateAnswerWithOneAudioStream() {
kwibergfd8be342016-05-14 19:44:11 -07001110 std::unique_ptr<SessionDescriptionInterface> offer =
zhihuang8f65cdf2016-05-06 18:40:30 -07001111 CreateOfferWithOneAudioStream();
1112 EXPECT_TRUE(DoSetRemoteDescription(offer.release()));
kwibergfd8be342016-05-14 19:44:11 -07001113 std::unique_ptr<SessionDescriptionInterface> answer;
zhihuang8f65cdf2016-05-06 18:40:30 -07001114 EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
1115 return answer;
1116 }
1117
1118 const std::string& GetFirstAudioStreamCname(
1119 const SessionDescriptionInterface* desc) {
1120 const cricket::ContentInfo* audio_content =
1121 cricket::GetFirstAudioContent(desc->description());
1122 const cricket::AudioContentDescription* audio_desc =
1123 static_cast<const cricket::AudioContentDescription*>(
1124 audio_content->description);
1125 return audio_desc->streams()[0].cname;
1126 }
1127
deadbeef9a6f4d42017-05-15 19:43:33 -07001128 std::unique_ptr<rtc::VirtualSocketServer> vss_;
1129 rtc::AutoSocketServerThread main_;
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08001130 cricket::FakePortAllocator* port_allocator_ = nullptr;
deadbeef8662f942017-01-20 21:20:51 -08001131 FakeRTCCertificateGenerator* fake_certificate_generator_ = nullptr;
zhihuang9763d562016-08-05 11:14:50 -07001132 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface> pc_factory_;
1133 rtc::scoped_refptr<PeerConnectionFactoryForTest> pc_factory_for_test_;
1134 rtc::scoped_refptr<PeerConnectionInterface> pc_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001135 MockPeerConnectionObserver observer_;
deadbeefab9b2d12015-10-14 11:33:11 -07001136 rtc::scoped_refptr<StreamCollection> reference_collection_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001137};
1138
zhihuang29ff8442016-07-27 11:07:25 -07001139// Test that no callbacks on the PeerConnectionObserver are called after the
1140// PeerConnection is closed.
1141TEST_F(PeerConnectionInterfaceTest, CloseAndTestCallbackFunctions) {
zhihuang9763d562016-08-05 11:14:50 -07001142 rtc::scoped_refptr<PeerConnectionInterface> pc(
zhihuang29ff8442016-07-27 11:07:25 -07001143 pc_factory_for_test_->CreatePeerConnection(
1144 PeerConnectionInterface::RTCConfiguration(), nullptr, nullptr,
1145 nullptr, &observer_));
1146 observer_.SetPeerConnectionInterface(pc.get());
1147 pc->Close();
1148
1149 // No callbacks is expected to be called.
zhihuang81c3a032016-11-17 12:06:24 -08001150 observer_.callback_triggered_ = false;
zhihuang29ff8442016-07-27 11:07:25 -07001151 std::vector<cricket::Candidate> candidates;
1152 pc_factory_for_test_->transport_controller->SignalGatheringState(
1153 cricket::IceGatheringState{});
1154 pc_factory_for_test_->transport_controller->SignalCandidatesGathered(
1155 "", candidates);
1156 pc_factory_for_test_->transport_controller->SignalConnectionState(
1157 cricket::IceConnectionState{});
1158 pc_factory_for_test_->transport_controller->SignalCandidatesRemoved(
1159 candidates);
1160 pc_factory_for_test_->transport_controller->SignalReceiving(false);
zhihuang81c3a032016-11-17 12:06:24 -08001161 EXPECT_FALSE(observer_.callback_triggered_);
zhihuang29ff8442016-07-27 11:07:25 -07001162}
1163
zhihuang8f65cdf2016-05-06 18:40:30 -07001164// Generate different CNAMEs when PeerConnections are created.
1165// The CNAMEs are expected to be generated randomly. It is possible
1166// that the test fails, though the possibility is very low.
1167TEST_F(PeerConnectionInterfaceTest, CnameGenerationInOffer) {
kwibergfd8be342016-05-14 19:44:11 -07001168 std::unique_ptr<SessionDescriptionInterface> offer1 =
zhihuang8f65cdf2016-05-06 18:40:30 -07001169 CreateOfferWithOneAudioStream();
kwibergfd8be342016-05-14 19:44:11 -07001170 std::unique_ptr<SessionDescriptionInterface> offer2 =
zhihuang8f65cdf2016-05-06 18:40:30 -07001171 CreateOfferWithOneAudioStream();
1172 EXPECT_NE(GetFirstAudioStreamCname(offer1.get()),
1173 GetFirstAudioStreamCname(offer2.get()));
1174}
1175
1176TEST_F(PeerConnectionInterfaceTest, CnameGenerationInAnswer) {
kwibergfd8be342016-05-14 19:44:11 -07001177 std::unique_ptr<SessionDescriptionInterface> answer1 =
zhihuang8f65cdf2016-05-06 18:40:30 -07001178 CreateAnswerWithOneAudioStream();
kwibergfd8be342016-05-14 19:44:11 -07001179 std::unique_ptr<SessionDescriptionInterface> answer2 =
zhihuang8f65cdf2016-05-06 18:40:30 -07001180 CreateAnswerWithOneAudioStream();
1181 EXPECT_NE(GetFirstAudioStreamCname(answer1.get()),
1182 GetFirstAudioStreamCname(answer2.get()));
1183}
1184
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001185TEST_F(PeerConnectionInterfaceTest,
1186 CreatePeerConnectionWithDifferentConfigurations) {
1187 CreatePeerConnectionWithDifferentConfigurations();
1188}
1189
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001190TEST_F(PeerConnectionInterfaceTest,
1191 CreatePeerConnectionWithDifferentIceTransportsTypes) {
1192 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kNone);
1193 EXPECT_EQ(cricket::CF_NONE, port_allocator_->candidate_filter());
1194 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kRelay);
1195 EXPECT_EQ(cricket::CF_RELAY, port_allocator_->candidate_filter());
1196 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kNoHost);
1197 EXPECT_EQ(cricket::CF_ALL & ~cricket::CF_HOST,
1198 port_allocator_->candidate_filter());
1199 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kAll);
1200 EXPECT_EQ(cricket::CF_ALL, port_allocator_->candidate_filter());
1201}
1202
1203// Test that when a PeerConnection is created with a nonzero candidate pool
1204// size, the pooled PortAllocatorSession is created with all the attributes
1205// in the RTCConfiguration.
1206TEST_F(PeerConnectionInterfaceTest, CreatePeerConnectionWithPooledCandidates) {
1207 PeerConnectionInterface::RTCConfiguration config;
1208 PeerConnectionInterface::IceServer server;
1209 server.uri = kStunAddressOnly;
1210 config.servers.push_back(server);
1211 config.type = PeerConnectionInterface::kRelay;
1212 config.disable_ipv6 = true;
1213 config.tcp_candidate_policy =
1214 PeerConnectionInterface::kTcpCandidatePolicyDisabled;
honghaiz60347052016-05-31 18:29:12 -07001215 config.candidate_network_policy =
1216 PeerConnectionInterface::kCandidateNetworkPolicyLowCost;
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001217 config.ice_candidate_pool_size = 1;
1218 CreatePeerConnection(config, nullptr);
1219
1220 const cricket::FakePortAllocatorSession* session =
1221 static_cast<const cricket::FakePortAllocatorSession*>(
1222 port_allocator_->GetPooledSession());
1223 ASSERT_NE(nullptr, session);
1224 EXPECT_EQ(1UL, session->stun_servers().size());
1225 EXPECT_EQ(0U, session->flags() & cricket::PORTALLOCATOR_ENABLE_IPV6);
1226 EXPECT_LT(0U, session->flags() & cricket::PORTALLOCATOR_DISABLE_TCP);
honghaiz60347052016-05-31 18:29:12 -07001227 EXPECT_LT(0U,
1228 session->flags() & cricket::PORTALLOCATOR_DISABLE_COSTLY_NETWORKS);
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001229}
1230
Taylor Brandstetterf8e65772016-06-27 17:20:15 -07001231// Test that the PeerConnection initializes the port allocator passed into it,
1232// and on the correct thread.
1233TEST_F(PeerConnectionInterfaceTest,
1234 CreatePeerConnectionInitializesPortAllocator) {
1235 rtc::Thread network_thread;
1236 network_thread.Start();
1237 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface> pc_factory(
1238 webrtc::CreatePeerConnectionFactory(
1239 &network_thread, rtc::Thread::Current(), rtc::Thread::Current(),
1240 nullptr, nullptr, nullptr));
1241 std::unique_ptr<cricket::FakePortAllocator> port_allocator(
1242 new cricket::FakePortAllocator(&network_thread, nullptr));
1243 cricket::FakePortAllocator* raw_port_allocator = port_allocator.get();
1244 PeerConnectionInterface::RTCConfiguration config;
1245 rtc::scoped_refptr<PeerConnectionInterface> pc(
1246 pc_factory->CreatePeerConnection(
1247 config, nullptr, std::move(port_allocator), nullptr, &observer_));
1248 // FakePortAllocator RTC_CHECKs that it's initialized on the right thread,
1249 // so all we have to do here is check that it's initialized.
1250 EXPECT_TRUE(raw_port_allocator->initialized());
1251}
1252
deadbeef46c73892016-11-16 19:42:04 -08001253// Check that GetConfiguration returns the configuration the PeerConnection was
1254// constructed with, before SetConfiguration is called.
1255TEST_F(PeerConnectionInterfaceTest, GetConfigurationAfterCreatePeerConnection) {
1256 PeerConnectionInterface::RTCConfiguration config;
1257 config.type = PeerConnectionInterface::kRelay;
1258 CreatePeerConnection(config, nullptr);
1259
1260 PeerConnectionInterface::RTCConfiguration returned_config =
1261 pc_->GetConfiguration();
1262 EXPECT_EQ(PeerConnectionInterface::kRelay, returned_config.type);
1263}
1264
1265// Check that GetConfiguration returns the last configuration passed into
1266// SetConfiguration.
1267TEST_F(PeerConnectionInterfaceTest, GetConfigurationAfterSetConfiguration) {
1268 CreatePeerConnection();
1269
1270 PeerConnectionInterface::RTCConfiguration config;
1271 config.type = PeerConnectionInterface::kRelay;
1272 EXPECT_TRUE(pc_->SetConfiguration(config));
1273
1274 PeerConnectionInterface::RTCConfiguration returned_config =
1275 pc_->GetConfiguration();
1276 EXPECT_EQ(PeerConnectionInterface::kRelay, returned_config.type);
1277}
1278
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001279TEST_F(PeerConnectionInterfaceTest, AddStreams) {
deadbeef293e9262017-01-11 12:28:30 -08001280 CreatePeerConnectionWithoutDtls();
deadbeefab9b2d12015-10-14 11:33:11 -07001281 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001282 AddVoiceStream(kStreamLabel2);
1283 ASSERT_EQ(2u, pc_->local_streams()->count());
1284
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001285 // Test we can add multiple local streams to one peerconnection.
zhihuang9763d562016-08-05 11:14:50 -07001286 rtc::scoped_refptr<MediaStreamInterface> stream(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001287 pc_factory_->CreateLocalMediaStream(kStreamLabel3));
zhihuang9763d562016-08-05 11:14:50 -07001288 rtc::scoped_refptr<AudioTrackInterface> audio_track(
1289 pc_factory_->CreateAudioTrack(kStreamLabel3,
1290 static_cast<AudioSourceInterface*>(NULL)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001291 stream->AddTrack(audio_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +00001292 EXPECT_TRUE(pc_->AddStream(stream));
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001293 EXPECT_EQ(3u, pc_->local_streams()->count());
1294
1295 // Remove the third stream.
1296 pc_->RemoveStream(pc_->local_streams()->at(2));
1297 EXPECT_EQ(2u, pc_->local_streams()->count());
1298
1299 // Remove the second stream.
1300 pc_->RemoveStream(pc_->local_streams()->at(1));
1301 EXPECT_EQ(1u, pc_->local_streams()->count());
1302
1303 // Remove the first stream.
1304 pc_->RemoveStream(pc_->local_streams()->at(0));
1305 EXPECT_EQ(0u, pc_->local_streams()->count());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001306}
1307
deadbeefab9b2d12015-10-14 11:33:11 -07001308// Test that the created offer includes streams we added.
1309TEST_F(PeerConnectionInterfaceTest, AddedStreamsPresentInOffer) {
deadbeef293e9262017-01-11 12:28:30 -08001310 CreatePeerConnectionWithoutDtls();
deadbeefab9b2d12015-10-14 11:33:11 -07001311 AddAudioVideoStream(kStreamLabel1, "audio_track", "video_track");
kwibergd1fe2812016-04-27 06:47:29 -07001312 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001313 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07001314
1315 const cricket::ContentInfo* audio_content =
1316 cricket::GetFirstAudioContent(offer->description());
1317 const cricket::AudioContentDescription* audio_desc =
1318 static_cast<const cricket::AudioContentDescription*>(
1319 audio_content->description);
1320 EXPECT_TRUE(
1321 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
1322
1323 const cricket::ContentInfo* video_content =
1324 cricket::GetFirstVideoContent(offer->description());
1325 const cricket::VideoContentDescription* video_desc =
1326 static_cast<const cricket::VideoContentDescription*>(
1327 video_content->description);
1328 EXPECT_TRUE(
1329 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1330
1331 // Add another stream and ensure the offer includes both the old and new
1332 // streams.
1333 AddAudioVideoStream(kStreamLabel2, "audio_track2", "video_track2");
kwiberg2bbff992016-03-16 11:03:04 -07001334 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07001335
1336 audio_content = cricket::GetFirstAudioContent(offer->description());
1337 audio_desc = static_cast<const cricket::AudioContentDescription*>(
1338 audio_content->description);
1339 EXPECT_TRUE(
1340 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
1341 EXPECT_TRUE(
1342 ContainsTrack(audio_desc->streams(), kStreamLabel2, "audio_track2"));
1343
1344 video_content = cricket::GetFirstVideoContent(offer->description());
1345 video_desc = static_cast<const cricket::VideoContentDescription*>(
1346 video_content->description);
1347 EXPECT_TRUE(
1348 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1349 EXPECT_TRUE(
1350 ContainsTrack(video_desc->streams(), kStreamLabel2, "video_track2"));
1351}
1352
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001353TEST_F(PeerConnectionInterfaceTest, RemoveStream) {
deadbeef293e9262017-01-11 12:28:30 -08001354 CreatePeerConnectionWithoutDtls();
deadbeefab9b2d12015-10-14 11:33:11 -07001355 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001356 ASSERT_EQ(1u, pc_->local_streams()->count());
1357 pc_->RemoveStream(pc_->local_streams()->at(0));
1358 EXPECT_EQ(0u, pc_->local_streams()->count());
1359}
1360
deadbeefe1f9d832016-01-14 15:35:42 -08001361// Test for AddTrack and RemoveTrack methods.
1362// Tests that the created offer includes tracks we added,
1363// and that the RtpSenders are created correctly.
1364// Also tests that RemoveTrack removes the tracks from subsequent offers.
1365TEST_F(PeerConnectionInterfaceTest, AddTrackRemoveTrack) {
deadbeef293e9262017-01-11 12:28:30 -08001366 CreatePeerConnectionWithoutDtls();
deadbeefe1f9d832016-01-14 15:35:42 -08001367 // Create a dummy stream, so tracks share a stream label.
zhihuang9763d562016-08-05 11:14:50 -07001368 rtc::scoped_refptr<MediaStreamInterface> stream(
deadbeefe1f9d832016-01-14 15:35:42 -08001369 pc_factory_->CreateLocalMediaStream(kStreamLabel1));
1370 std::vector<MediaStreamInterface*> stream_list;
1371 stream_list.push_back(stream.get());
zhihuang9763d562016-08-05 11:14:50 -07001372 rtc::scoped_refptr<AudioTrackInterface> audio_track(
deadbeefe1f9d832016-01-14 15:35:42 -08001373 pc_factory_->CreateAudioTrack("audio_track", nullptr));
zhihuang9763d562016-08-05 11:14:50 -07001374 rtc::scoped_refptr<VideoTrackInterface> video_track(
1375 pc_factory_->CreateVideoTrack(
deadbeef112b2e92017-02-10 20:13:37 -08001376 "video_track", pc_factory_->CreateVideoSource(
1377 std::unique_ptr<cricket::VideoCapturer>(
1378 new cricket::FakeVideoCapturer()))));
deadbeefe1f9d832016-01-14 15:35:42 -08001379 auto audio_sender = pc_->AddTrack(audio_track, stream_list);
1380 auto video_sender = pc_->AddTrack(video_track, stream_list);
deadbeefa601f5c2016-06-06 14:27:39 -07001381 EXPECT_EQ(1UL, audio_sender->stream_ids().size());
1382 EXPECT_EQ(kStreamLabel1, audio_sender->stream_ids()[0]);
deadbeefe1f9d832016-01-14 15:35:42 -08001383 EXPECT_EQ("audio_track", audio_sender->id());
1384 EXPECT_EQ(audio_track, audio_sender->track());
deadbeefa601f5c2016-06-06 14:27:39 -07001385 EXPECT_EQ(1UL, video_sender->stream_ids().size());
1386 EXPECT_EQ(kStreamLabel1, video_sender->stream_ids()[0]);
deadbeefe1f9d832016-01-14 15:35:42 -08001387 EXPECT_EQ("video_track", video_sender->id());
1388 EXPECT_EQ(video_track, video_sender->track());
1389
1390 // Now create an offer and check for the senders.
kwibergd1fe2812016-04-27 06:47:29 -07001391 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001392 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefe1f9d832016-01-14 15:35:42 -08001393
1394 const cricket::ContentInfo* audio_content =
1395 cricket::GetFirstAudioContent(offer->description());
1396 const cricket::AudioContentDescription* audio_desc =
1397 static_cast<const cricket::AudioContentDescription*>(
1398 audio_content->description);
1399 EXPECT_TRUE(
1400 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
1401
1402 const cricket::ContentInfo* video_content =
1403 cricket::GetFirstVideoContent(offer->description());
1404 const cricket::VideoContentDescription* video_desc =
1405 static_cast<const cricket::VideoContentDescription*>(
1406 video_content->description);
1407 EXPECT_TRUE(
1408 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1409
1410 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
1411
1412 // Now try removing the tracks.
1413 EXPECT_TRUE(pc_->RemoveTrack(audio_sender));
1414 EXPECT_TRUE(pc_->RemoveTrack(video_sender));
1415
1416 // Create a new offer and ensure it doesn't contain the removed senders.
kwiberg2bbff992016-03-16 11:03:04 -07001417 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefe1f9d832016-01-14 15:35:42 -08001418
1419 audio_content = cricket::GetFirstAudioContent(offer->description());
1420 audio_desc = static_cast<const cricket::AudioContentDescription*>(
1421 audio_content->description);
1422 EXPECT_FALSE(
1423 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
1424
1425 video_content = cricket::GetFirstVideoContent(offer->description());
1426 video_desc = static_cast<const cricket::VideoContentDescription*>(
1427 video_content->description);
1428 EXPECT_FALSE(
1429 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1430
1431 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
1432
1433 // Calling RemoveTrack on a sender no longer attached to a PeerConnection
1434 // should return false.
1435 EXPECT_FALSE(pc_->RemoveTrack(audio_sender));
1436 EXPECT_FALSE(pc_->RemoveTrack(video_sender));
1437}
1438
1439// Test creating senders without a stream specified,
1440// expecting a random stream ID to be generated.
1441TEST_F(PeerConnectionInterfaceTest, AddTrackWithoutStream) {
deadbeef293e9262017-01-11 12:28:30 -08001442 CreatePeerConnectionWithoutDtls();
deadbeefe1f9d832016-01-14 15:35:42 -08001443 // Create a dummy stream, so tracks share a stream label.
zhihuang9763d562016-08-05 11:14:50 -07001444 rtc::scoped_refptr<AudioTrackInterface> audio_track(
deadbeefe1f9d832016-01-14 15:35:42 -08001445 pc_factory_->CreateAudioTrack("audio_track", nullptr));
zhihuang9763d562016-08-05 11:14:50 -07001446 rtc::scoped_refptr<VideoTrackInterface> video_track(
1447 pc_factory_->CreateVideoTrack(
deadbeef112b2e92017-02-10 20:13:37 -08001448 "video_track", pc_factory_->CreateVideoSource(
1449 std::unique_ptr<cricket::VideoCapturer>(
1450 new cricket::FakeVideoCapturer()))));
deadbeefe1f9d832016-01-14 15:35:42 -08001451 auto audio_sender =
1452 pc_->AddTrack(audio_track, std::vector<MediaStreamInterface*>());
1453 auto video_sender =
1454 pc_->AddTrack(video_track, std::vector<MediaStreamInterface*>());
1455 EXPECT_EQ("audio_track", audio_sender->id());
1456 EXPECT_EQ(audio_track, audio_sender->track());
1457 EXPECT_EQ("video_track", video_sender->id());
1458 EXPECT_EQ(video_track, video_sender->track());
1459 // If the ID is truly a random GUID, it should be infinitely unlikely they
1460 // will be the same.
deadbeefa601f5c2016-06-06 14:27:39 -07001461 EXPECT_NE(video_sender->stream_ids(), audio_sender->stream_ids());
deadbeefe1f9d832016-01-14 15:35:42 -08001462}
1463
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001464TEST_F(PeerConnectionInterfaceTest, CreateOfferReceiveAnswer) {
1465 InitiateCall();
1466 WaitAndVerifyOnAddStream(kStreamLabel1);
1467 VerifyRemoteRtpHeaderExtensions();
1468}
1469
1470TEST_F(PeerConnectionInterfaceTest, CreateOfferReceivePrAnswerAndAnswer) {
deadbeef293e9262017-01-11 12:28:30 -08001471 CreatePeerConnectionWithoutDtls();
deadbeefab9b2d12015-10-14 11:33:11 -07001472 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001473 CreateOfferAsLocalDescription();
1474 std::string offer;
1475 EXPECT_TRUE(pc_->local_description()->ToString(&offer));
1476 CreatePrAnswerAndAnswerAsRemoteDescription(offer);
1477 WaitAndVerifyOnAddStream(kStreamLabel1);
1478}
1479
1480TEST_F(PeerConnectionInterfaceTest, ReceiveOfferCreateAnswer) {
deadbeef293e9262017-01-11 12:28:30 -08001481 CreatePeerConnectionWithoutDtls();
deadbeefab9b2d12015-10-14 11:33:11 -07001482 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001483
1484 CreateOfferAsRemoteDescription();
1485 CreateAnswerAsLocalDescription();
1486
1487 WaitAndVerifyOnAddStream(kStreamLabel1);
1488}
1489
1490TEST_F(PeerConnectionInterfaceTest, ReceiveOfferCreatePrAnswerAndAnswer) {
deadbeef293e9262017-01-11 12:28:30 -08001491 CreatePeerConnectionWithoutDtls();
deadbeefab9b2d12015-10-14 11:33:11 -07001492 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001493
1494 CreateOfferAsRemoteDescription();
1495 CreatePrAnswerAsLocalDescription();
1496 CreateAnswerAsLocalDescription();
1497
1498 WaitAndVerifyOnAddStream(kStreamLabel1);
1499}
1500
1501TEST_F(PeerConnectionInterfaceTest, Renegotiate) {
1502 InitiateCall();
1503 ASSERT_EQ(1u, pc_->remote_streams()->count());
1504 pc_->RemoveStream(pc_->local_streams()->at(0));
1505 CreateOfferReceiveAnswer();
1506 EXPECT_EQ(0u, pc_->remote_streams()->count());
deadbeefab9b2d12015-10-14 11:33:11 -07001507 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001508 CreateOfferReceiveAnswer();
1509}
1510
1511// Tests that after negotiating an audio only call, the respondent can perform a
1512// renegotiation that removes the audio stream.
1513TEST_F(PeerConnectionInterfaceTest, RenegotiateAudioOnly) {
deadbeef293e9262017-01-11 12:28:30 -08001514 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001515 AddVoiceStream(kStreamLabel1);
1516 CreateOfferAsRemoteDescription();
1517 CreateAnswerAsLocalDescription();
1518
1519 ASSERT_EQ(1u, pc_->remote_streams()->count());
1520 pc_->RemoveStream(pc_->local_streams()->at(0));
1521 CreateOfferReceiveAnswer();
1522 EXPECT_EQ(0u, pc_->remote_streams()->count());
1523}
1524
1525// Test that candidates are generated and that we can parse our own candidates.
1526TEST_F(PeerConnectionInterfaceTest, IceCandidates) {
deadbeef293e9262017-01-11 12:28:30 -08001527 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001528
1529 EXPECT_FALSE(pc_->AddIceCandidate(observer_.last_candidate_.get()));
1530 // SetRemoteDescription takes ownership of offer.
kwibergd1fe2812016-04-27 06:47:29 -07001531 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefab9b2d12015-10-14 11:33:11 -07001532 AddVideoStream(kStreamLabel1);
deadbeefc80741f2015-10-22 13:14:45 -07001533 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
kwiberg2bbff992016-03-16 11:03:04 -07001534 EXPECT_TRUE(DoSetRemoteDescription(offer.release()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001535
1536 // SetLocalDescription takes ownership of answer.
kwibergd1fe2812016-04-27 06:47:29 -07001537 std::unique_ptr<SessionDescriptionInterface> answer;
deadbeefc80741f2015-10-22 13:14:45 -07001538 EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
kwiberg2bbff992016-03-16 11:03:04 -07001539 EXPECT_TRUE(DoSetLocalDescription(answer.release()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001540
1541 EXPECT_TRUE_WAIT(observer_.last_candidate_.get() != NULL, kTimeout);
1542 EXPECT_TRUE_WAIT(observer_.ice_complete_, kTimeout);
1543
1544 EXPECT_TRUE(pc_->AddIceCandidate(observer_.last_candidate_.get()));
1545}
1546
deadbeefab9b2d12015-10-14 11:33:11 -07001547// Test that CreateOffer and CreateAnswer will fail if the track labels are
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001548// not unique.
1549TEST_F(PeerConnectionInterfaceTest, CreateOfferAnswerWithInvalidStream) {
deadbeef293e9262017-01-11 12:28:30 -08001550 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001551 // Create a regular offer for the CreateAnswer test later.
kwibergd1fe2812016-04-27 06:47:29 -07001552 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefc80741f2015-10-22 13:14:45 -07001553 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
kwiberg2bbff992016-03-16 11:03:04 -07001554 EXPECT_TRUE(offer);
1555 offer.reset();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001556
1557 // Create a local stream with audio&video tracks having same label.
1558 AddAudioVideoStream(kStreamLabel1, "track_label", "track_label");
1559
1560 // Test CreateOffer
deadbeefc80741f2015-10-22 13:14:45 -07001561 EXPECT_FALSE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001562
1563 // Test CreateAnswer
kwibergd1fe2812016-04-27 06:47:29 -07001564 std::unique_ptr<SessionDescriptionInterface> answer;
deadbeefc80741f2015-10-22 13:14:45 -07001565 EXPECT_FALSE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001566}
1567
1568// Test that we will get different SSRCs for each tracks in the offer and answer
1569// we created.
1570TEST_F(PeerConnectionInterfaceTest, SsrcInOfferAnswer) {
deadbeef293e9262017-01-11 12:28:30 -08001571 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001572 // Create a local stream with audio&video tracks having different labels.
1573 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
1574
1575 // Test CreateOffer
kwibergd1fe2812016-04-27 06:47:29 -07001576 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001577 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001578 int audio_ssrc = 0;
1579 int video_ssrc = 0;
1580 EXPECT_TRUE(GetFirstSsrc(GetFirstAudioContent(offer->description()),
1581 &audio_ssrc));
1582 EXPECT_TRUE(GetFirstSsrc(GetFirstVideoContent(offer->description()),
1583 &video_ssrc));
1584 EXPECT_NE(audio_ssrc, video_ssrc);
1585
1586 // Test CreateAnswer
1587 EXPECT_TRUE(DoSetRemoteDescription(offer.release()));
kwibergd1fe2812016-04-27 06:47:29 -07001588 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -07001589 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001590 audio_ssrc = 0;
1591 video_ssrc = 0;
1592 EXPECT_TRUE(GetFirstSsrc(GetFirstAudioContent(answer->description()),
1593 &audio_ssrc));
1594 EXPECT_TRUE(GetFirstSsrc(GetFirstVideoContent(answer->description()),
1595 &video_ssrc));
1596 EXPECT_NE(audio_ssrc, video_ssrc);
1597}
1598
deadbeefeb459812015-12-15 19:24:43 -08001599// Test that it's possible to call AddTrack on a MediaStream after adding
1600// the stream to a PeerConnection.
1601// TODO(deadbeef): Remove this test once this behavior is no longer supported.
1602TEST_F(PeerConnectionInterfaceTest, AddTrackAfterAddStream) {
deadbeef293e9262017-01-11 12:28:30 -08001603 CreatePeerConnectionWithoutDtls();
deadbeefeb459812015-12-15 19:24:43 -08001604 // Create audio stream and add to PeerConnection.
1605 AddVoiceStream(kStreamLabel1);
1606 MediaStreamInterface* stream = pc_->local_streams()->at(0);
1607
1608 // Add video track to the audio-only stream.
zhihuang9763d562016-08-05 11:14:50 -07001609 rtc::scoped_refptr<VideoTrackInterface> video_track(
1610 pc_factory_->CreateVideoTrack(
deadbeef112b2e92017-02-10 20:13:37 -08001611 "video_label", pc_factory_->CreateVideoSource(
1612 std::unique_ptr<cricket::VideoCapturer>(
1613 new cricket::FakeVideoCapturer()))));
deadbeefeb459812015-12-15 19:24:43 -08001614 stream->AddTrack(video_track.get());
1615
kwibergd1fe2812016-04-27 06:47:29 -07001616 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001617 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefeb459812015-12-15 19:24:43 -08001618
1619 const cricket::MediaContentDescription* video_desc =
1620 cricket::GetFirstVideoContentDescription(offer->description());
1621 EXPECT_TRUE(video_desc != nullptr);
1622}
1623
1624// Test that it's possible to call RemoveTrack on a MediaStream after adding
1625// the stream to a PeerConnection.
1626// TODO(deadbeef): Remove this test once this behavior is no longer supported.
1627TEST_F(PeerConnectionInterfaceTest, RemoveTrackAfterAddStream) {
deadbeef293e9262017-01-11 12:28:30 -08001628 CreatePeerConnectionWithoutDtls();
deadbeefeb459812015-12-15 19:24:43 -08001629 // Create audio/video stream and add to PeerConnection.
1630 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
1631 MediaStreamInterface* stream = pc_->local_streams()->at(0);
1632
1633 // Remove the video track.
1634 stream->RemoveTrack(stream->GetVideoTracks()[0]);
1635
kwibergd1fe2812016-04-27 06:47:29 -07001636 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001637 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefeb459812015-12-15 19:24:43 -08001638
1639 const cricket::MediaContentDescription* video_desc =
1640 cricket::GetFirstVideoContentDescription(offer->description());
1641 EXPECT_TRUE(video_desc == nullptr);
1642}
1643
deadbeef1dcb1642017-03-29 21:08:16 -07001644// Verify that CreateDtmfSender only succeeds if called with a valid local
1645// track. Other aspects of DtmfSenders are tested in
1646// peerconnection_integrationtest.cc.
1647TEST_F(PeerConnectionInterfaceTest, CreateDtmfSenderWithInvalidParams) {
1648 CreatePeerConnection();
1649 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
1650 EXPECT_EQ(nullptr, pc_->CreateDtmfSender(nullptr));
1651 rtc::scoped_refptr<webrtc::AudioTrackInterface> non_localtrack(
1652 pc_factory_->CreateAudioTrack("dummy_track", nullptr));
1653 EXPECT_EQ(nullptr, pc_->CreateDtmfSender(non_localtrack));
1654}
1655
deadbeefbd7d8f72015-12-18 16:58:44 -08001656// Test creating a sender with a stream ID, and ensure the ID is populated
1657// in the offer.
1658TEST_F(PeerConnectionInterfaceTest, CreateSenderWithStream) {
deadbeef293e9262017-01-11 12:28:30 -08001659 CreatePeerConnectionWithoutDtls();
deadbeefbd7d8f72015-12-18 16:58:44 -08001660 pc_->CreateSender("video", kStreamLabel1);
1661
kwibergd1fe2812016-04-27 06:47:29 -07001662 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001663 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefbd7d8f72015-12-18 16:58:44 -08001664
1665 const cricket::MediaContentDescription* video_desc =
1666 cricket::GetFirstVideoContentDescription(offer->description());
1667 ASSERT_TRUE(video_desc != nullptr);
1668 ASSERT_EQ(1u, video_desc->streams().size());
1669 EXPECT_EQ(kStreamLabel1, video_desc->streams()[0].sync_label);
1670}
1671
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001672// Test that we can specify a certain track that we want statistics about.
1673TEST_F(PeerConnectionInterfaceTest, GetStatsForSpecificTrack) {
1674 InitiateCall();
1675 ASSERT_LT(0u, pc_->remote_streams()->count());
1676 ASSERT_LT(0u, pc_->remote_streams()->at(0)->GetAudioTracks().size());
zhihuang9763d562016-08-05 11:14:50 -07001677 rtc::scoped_refptr<MediaStreamTrackInterface> remote_audio =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001678 pc_->remote_streams()->at(0)->GetAudioTracks()[0];
1679 EXPECT_TRUE(DoGetStats(remote_audio));
1680
1681 // Remove the stream. Since we are sending to our selves the local
1682 // and the remote stream is the same.
1683 pc_->RemoveStream(pc_->local_streams()->at(0));
1684 // Do a re-negotiation.
1685 CreateOfferReceiveAnswer();
1686
1687 ASSERT_EQ(0u, pc_->remote_streams()->count());
1688
1689 // Test that we still can get statistics for the old track. Even if it is not
1690 // sent any longer.
1691 EXPECT_TRUE(DoGetStats(remote_audio));
1692}
1693
1694// Test that we can get stats on a video track.
1695TEST_F(PeerConnectionInterfaceTest, GetStatsForVideoTrack) {
1696 InitiateCall();
1697 ASSERT_LT(0u, pc_->remote_streams()->count());
1698 ASSERT_LT(0u, pc_->remote_streams()->at(0)->GetVideoTracks().size());
zhihuang9763d562016-08-05 11:14:50 -07001699 rtc::scoped_refptr<MediaStreamTrackInterface> remote_video =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001700 pc_->remote_streams()->at(0)->GetVideoTracks()[0];
1701 EXPECT_TRUE(DoGetStats(remote_video));
1702}
1703
1704// Test that we don't get statistics for an invalid track.
zhihuange9e94c32016-11-04 11:38:15 -07001705TEST_F(PeerConnectionInterfaceTest, GetStatsForInvalidTrack) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001706 InitiateCall();
zhihuang9763d562016-08-05 11:14:50 -07001707 rtc::scoped_refptr<AudioTrackInterface> unknown_audio_track(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001708 pc_factory_->CreateAudioTrack("unknown track", NULL));
1709 EXPECT_FALSE(DoGetStats(unknown_audio_track));
1710}
1711
1712// This test setup two RTP data channels in loop back.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001713TEST_F(PeerConnectionInterfaceTest, TestDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001714 FakeConstraints constraints;
1715 constraints.SetAllowRtpDataChannels();
1716 CreatePeerConnection(&constraints);
zhihuang9763d562016-08-05 11:14:50 -07001717 rtc::scoped_refptr<DataChannelInterface> data1 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001718 pc_->CreateDataChannel("test1", NULL);
zhihuang9763d562016-08-05 11:14:50 -07001719 rtc::scoped_refptr<DataChannelInterface> data2 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001720 pc_->CreateDataChannel("test2", NULL);
1721 ASSERT_TRUE(data1 != NULL);
kwibergd1fe2812016-04-27 06:47:29 -07001722 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001723 new MockDataChannelObserver(data1));
kwibergd1fe2812016-04-27 06:47:29 -07001724 std::unique_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001725 new MockDataChannelObserver(data2));
1726
1727 EXPECT_EQ(DataChannelInterface::kConnecting, data1->state());
1728 EXPECT_EQ(DataChannelInterface::kConnecting, data2->state());
1729 std::string data_to_send1 = "testing testing";
1730 std::string data_to_send2 = "testing something else";
1731 EXPECT_FALSE(data1->Send(DataBuffer(data_to_send1)));
1732
1733 CreateOfferReceiveAnswer();
1734 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1735 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
1736
1737 EXPECT_EQ(DataChannelInterface::kOpen, data1->state());
1738 EXPECT_EQ(DataChannelInterface::kOpen, data2->state());
1739 EXPECT_TRUE(data1->Send(DataBuffer(data_to_send1)));
1740 EXPECT_TRUE(data2->Send(DataBuffer(data_to_send2)));
1741
1742 EXPECT_EQ_WAIT(data_to_send1, observer1->last_message(), kTimeout);
1743 EXPECT_EQ_WAIT(data_to_send2, observer2->last_message(), kTimeout);
1744
1745 data1->Close();
1746 EXPECT_EQ(DataChannelInterface::kClosing, data1->state());
1747 CreateOfferReceiveAnswer();
1748 EXPECT_FALSE(observer1->IsOpen());
1749 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
1750 EXPECT_TRUE(observer2->IsOpen());
1751
1752 data_to_send2 = "testing something else again";
1753 EXPECT_TRUE(data2->Send(DataBuffer(data_to_send2)));
1754
1755 EXPECT_EQ_WAIT(data_to_send2, observer2->last_message(), kTimeout);
1756}
1757
1758// This test verifies that sendnig binary data over RTP data channels should
1759// fail.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001760TEST_F(PeerConnectionInterfaceTest, TestSendBinaryOnRtpDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001761 FakeConstraints constraints;
1762 constraints.SetAllowRtpDataChannels();
1763 CreatePeerConnection(&constraints);
zhihuang9763d562016-08-05 11:14:50 -07001764 rtc::scoped_refptr<DataChannelInterface> data1 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001765 pc_->CreateDataChannel("test1", NULL);
zhihuang9763d562016-08-05 11:14:50 -07001766 rtc::scoped_refptr<DataChannelInterface> data2 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001767 pc_->CreateDataChannel("test2", NULL);
1768 ASSERT_TRUE(data1 != NULL);
kwibergd1fe2812016-04-27 06:47:29 -07001769 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001770 new MockDataChannelObserver(data1));
kwibergd1fe2812016-04-27 06:47:29 -07001771 std::unique_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001772 new MockDataChannelObserver(data2));
1773
1774 EXPECT_EQ(DataChannelInterface::kConnecting, data1->state());
1775 EXPECT_EQ(DataChannelInterface::kConnecting, data2->state());
1776
1777 CreateOfferReceiveAnswer();
1778 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1779 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
1780
1781 EXPECT_EQ(DataChannelInterface::kOpen, data1->state());
1782 EXPECT_EQ(DataChannelInterface::kOpen, data2->state());
1783
jbaucheec21bd2016-03-20 06:15:43 -07001784 rtc::CopyOnWriteBuffer buffer("test", 4);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001785 EXPECT_FALSE(data1->Send(DataBuffer(buffer, true)));
1786}
1787
1788// This test setup a RTP data channels in loop back and test that a channel is
1789// opened even if the remote end answer with a zero SSRC.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001790TEST_F(PeerConnectionInterfaceTest, TestSendOnlyDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001791 FakeConstraints constraints;
1792 constraints.SetAllowRtpDataChannels();
1793 CreatePeerConnection(&constraints);
zhihuang9763d562016-08-05 11:14:50 -07001794 rtc::scoped_refptr<DataChannelInterface> data1 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001795 pc_->CreateDataChannel("test1", NULL);
kwibergd1fe2812016-04-27 06:47:29 -07001796 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001797 new MockDataChannelObserver(data1));
1798
1799 CreateOfferReceiveAnswerWithoutSsrc();
1800
1801 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1802
1803 data1->Close();
1804 EXPECT_EQ(DataChannelInterface::kClosing, data1->state());
1805 CreateOfferReceiveAnswerWithoutSsrc();
1806 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
1807 EXPECT_FALSE(observer1->IsOpen());
1808}
1809
1810// This test that if a data channel is added in an answer a receive only channel
1811// channel is created.
1812TEST_F(PeerConnectionInterfaceTest, TestReceiveOnlyDataChannel) {
1813 FakeConstraints constraints;
1814 constraints.SetAllowRtpDataChannels();
1815 CreatePeerConnection(&constraints);
1816
1817 std::string offer_label = "offer_channel";
zhihuang9763d562016-08-05 11:14:50 -07001818 rtc::scoped_refptr<DataChannelInterface> offer_channel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001819 pc_->CreateDataChannel(offer_label, NULL);
1820
1821 CreateOfferAsLocalDescription();
1822
1823 // Replace the data channel label in the offer and apply it as an answer.
1824 std::string receive_label = "answer_channel";
1825 std::string sdp;
1826 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001827 rtc::replace_substrs(offer_label.c_str(), offer_label.length(),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001828 receive_label.c_str(), receive_label.length(),
1829 &sdp);
1830 CreateAnswerAsRemoteDescription(sdp);
1831
1832 // Verify that a new incoming data channel has been created and that
1833 // it is open but can't we written to.
1834 ASSERT_TRUE(observer_.last_datachannel_ != NULL);
1835 DataChannelInterface* received_channel = observer_.last_datachannel_;
1836 EXPECT_EQ(DataChannelInterface::kConnecting, received_channel->state());
1837 EXPECT_EQ(receive_label, received_channel->label());
1838 EXPECT_FALSE(received_channel->Send(DataBuffer("something")));
1839
1840 // Verify that the channel we initially offered has been rejected.
1841 EXPECT_EQ(DataChannelInterface::kClosed, offer_channel->state());
1842
1843 // Do another offer / answer exchange and verify that the data channel is
1844 // opened.
1845 CreateOfferReceiveAnswer();
1846 EXPECT_EQ_WAIT(DataChannelInterface::kOpen, received_channel->state(),
1847 kTimeout);
1848}
1849
1850// This test that no data channel is returned if a reliable channel is
1851// requested.
1852// TODO(perkj): Remove this test once reliable channels are implemented.
1853TEST_F(PeerConnectionInterfaceTest, CreateReliableRtpDataChannelShouldFail) {
1854 FakeConstraints constraints;
1855 constraints.SetAllowRtpDataChannels();
1856 CreatePeerConnection(&constraints);
1857
1858 std::string label = "test";
1859 webrtc::DataChannelInit config;
1860 config.reliable = true;
zhihuang9763d562016-08-05 11:14:50 -07001861 rtc::scoped_refptr<DataChannelInterface> channel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001862 pc_->CreateDataChannel(label, &config);
1863 EXPECT_TRUE(channel == NULL);
1864}
1865
deadbeefab9b2d12015-10-14 11:33:11 -07001866// Verifies that duplicated label is not allowed for RTP data channel.
1867TEST_F(PeerConnectionInterfaceTest, RtpDuplicatedLabelNotAllowed) {
1868 FakeConstraints constraints;
1869 constraints.SetAllowRtpDataChannels();
1870 CreatePeerConnection(&constraints);
1871
1872 std::string label = "test";
zhihuang9763d562016-08-05 11:14:50 -07001873 rtc::scoped_refptr<DataChannelInterface> channel =
deadbeefab9b2d12015-10-14 11:33:11 -07001874 pc_->CreateDataChannel(label, nullptr);
1875 EXPECT_NE(channel, nullptr);
1876
zhihuang9763d562016-08-05 11:14:50 -07001877 rtc::scoped_refptr<DataChannelInterface> dup_channel =
deadbeefab9b2d12015-10-14 11:33:11 -07001878 pc_->CreateDataChannel(label, nullptr);
1879 EXPECT_EQ(dup_channel, nullptr);
1880}
1881
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001882// This tests that a SCTP data channel is returned using different
1883// DataChannelInit configurations.
1884TEST_F(PeerConnectionInterfaceTest, CreateSctpDataChannel) {
1885 FakeConstraints constraints;
1886 constraints.SetAllowDtlsSctpDataChannels();
1887 CreatePeerConnection(&constraints);
1888
1889 webrtc::DataChannelInit config;
1890
zhihuang9763d562016-08-05 11:14:50 -07001891 rtc::scoped_refptr<DataChannelInterface> channel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001892 pc_->CreateDataChannel("1", &config);
1893 EXPECT_TRUE(channel != NULL);
1894 EXPECT_TRUE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001895 EXPECT_TRUE(observer_.renegotiation_needed_);
1896 observer_.renegotiation_needed_ = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001897
1898 config.ordered = false;
1899 channel = pc_->CreateDataChannel("2", &config);
1900 EXPECT_TRUE(channel != NULL);
1901 EXPECT_TRUE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001902 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001903
1904 config.ordered = true;
1905 config.maxRetransmits = 0;
1906 channel = pc_->CreateDataChannel("3", &config);
1907 EXPECT_TRUE(channel != NULL);
1908 EXPECT_FALSE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001909 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001910
1911 config.maxRetransmits = -1;
1912 config.maxRetransmitTime = 0;
1913 channel = pc_->CreateDataChannel("4", &config);
1914 EXPECT_TRUE(channel != NULL);
1915 EXPECT_FALSE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001916 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001917}
1918
1919// This tests that no data channel is returned if both maxRetransmits and
1920// maxRetransmitTime are set for SCTP data channels.
1921TEST_F(PeerConnectionInterfaceTest,
1922 CreateSctpDataChannelShouldFailForInvalidConfig) {
1923 FakeConstraints constraints;
1924 constraints.SetAllowDtlsSctpDataChannels();
1925 CreatePeerConnection(&constraints);
1926
1927 std::string label = "test";
1928 webrtc::DataChannelInit config;
1929 config.maxRetransmits = 0;
1930 config.maxRetransmitTime = 0;
1931
zhihuang9763d562016-08-05 11:14:50 -07001932 rtc::scoped_refptr<DataChannelInterface> channel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001933 pc_->CreateDataChannel(label, &config);
1934 EXPECT_TRUE(channel == NULL);
1935}
1936
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001937// The test verifies that creating a SCTP data channel with an id already in use
1938// or out of range should fail.
1939TEST_F(PeerConnectionInterfaceTest,
1940 CreateSctpDataChannelWithInvalidIdShouldFail) {
1941 FakeConstraints constraints;
1942 constraints.SetAllowDtlsSctpDataChannels();
1943 CreatePeerConnection(&constraints);
1944
1945 webrtc::DataChannelInit config;
zhihuang9763d562016-08-05 11:14:50 -07001946 rtc::scoped_refptr<DataChannelInterface> channel;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001947
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001948 config.id = 1;
1949 channel = pc_->CreateDataChannel("1", &config);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001950 EXPECT_TRUE(channel != NULL);
1951 EXPECT_EQ(1, channel->id());
1952
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001953 channel = pc_->CreateDataChannel("x", &config);
1954 EXPECT_TRUE(channel == NULL);
1955
1956 config.id = cricket::kMaxSctpSid;
1957 channel = pc_->CreateDataChannel("max", &config);
1958 EXPECT_TRUE(channel != NULL);
1959 EXPECT_EQ(config.id, channel->id());
1960
1961 config.id = cricket::kMaxSctpSid + 1;
1962 channel = pc_->CreateDataChannel("x", &config);
1963 EXPECT_TRUE(channel == NULL);
1964}
1965
deadbeefab9b2d12015-10-14 11:33:11 -07001966// Verifies that duplicated label is allowed for SCTP data channel.
1967TEST_F(PeerConnectionInterfaceTest, SctpDuplicatedLabelAllowed) {
1968 FakeConstraints constraints;
1969 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
1970 true);
1971 CreatePeerConnection(&constraints);
1972
1973 std::string label = "test";
zhihuang9763d562016-08-05 11:14:50 -07001974 rtc::scoped_refptr<DataChannelInterface> channel =
deadbeefab9b2d12015-10-14 11:33:11 -07001975 pc_->CreateDataChannel(label, nullptr);
1976 EXPECT_NE(channel, nullptr);
1977
zhihuang9763d562016-08-05 11:14:50 -07001978 rtc::scoped_refptr<DataChannelInterface> dup_channel =
deadbeefab9b2d12015-10-14 11:33:11 -07001979 pc_->CreateDataChannel(label, nullptr);
1980 EXPECT_NE(dup_channel, nullptr);
1981}
1982
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001983// This test verifies that OnRenegotiationNeeded is fired for every new RTP
1984// DataChannel.
1985TEST_F(PeerConnectionInterfaceTest, RenegotiationNeededForNewRtpDataChannel) {
1986 FakeConstraints constraints;
1987 constraints.SetAllowRtpDataChannels();
1988 CreatePeerConnection(&constraints);
1989
zhihuang9763d562016-08-05 11:14:50 -07001990 rtc::scoped_refptr<DataChannelInterface> dc1 =
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001991 pc_->CreateDataChannel("test1", NULL);
1992 EXPECT_TRUE(observer_.renegotiation_needed_);
1993 observer_.renegotiation_needed_ = false;
1994
zhihuang9763d562016-08-05 11:14:50 -07001995 rtc::scoped_refptr<DataChannelInterface> dc2 =
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001996 pc_->CreateDataChannel("test2", NULL);
1997 EXPECT_TRUE(observer_.renegotiation_needed_);
1998}
1999
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002000// This test that a data channel closes when a PeerConnection is deleted/closed.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002001TEST_F(PeerConnectionInterfaceTest, DataChannelCloseWhenPeerConnectionClose) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002002 FakeConstraints constraints;
2003 constraints.SetAllowRtpDataChannels();
2004 CreatePeerConnection(&constraints);
2005
zhihuang9763d562016-08-05 11:14:50 -07002006 rtc::scoped_refptr<DataChannelInterface> data1 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002007 pc_->CreateDataChannel("test1", NULL);
zhihuang9763d562016-08-05 11:14:50 -07002008 rtc::scoped_refptr<DataChannelInterface> data2 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002009 pc_->CreateDataChannel("test2", NULL);
2010 ASSERT_TRUE(data1 != NULL);
kwibergd1fe2812016-04-27 06:47:29 -07002011 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002012 new MockDataChannelObserver(data1));
kwibergd1fe2812016-04-27 06:47:29 -07002013 std::unique_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002014 new MockDataChannelObserver(data2));
2015
2016 CreateOfferReceiveAnswer();
2017 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
2018 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
2019
2020 ReleasePeerConnection();
2021 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
2022 EXPECT_EQ(DataChannelInterface::kClosed, data2->state());
2023}
2024
2025// This test that data channels can be rejected in an answer.
2026TEST_F(PeerConnectionInterfaceTest, TestRejectDataChannelInAnswer) {
2027 FakeConstraints constraints;
2028 constraints.SetAllowRtpDataChannels();
2029 CreatePeerConnection(&constraints);
2030
zhihuang9763d562016-08-05 11:14:50 -07002031 rtc::scoped_refptr<DataChannelInterface> offer_channel(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002032 pc_->CreateDataChannel("offer_channel", NULL));
2033
2034 CreateOfferAsLocalDescription();
2035
2036 // Create an answer where the m-line for data channels are rejected.
2037 std::string sdp;
2038 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
2039 webrtc::JsepSessionDescription* answer = new webrtc::JsepSessionDescription(
2040 SessionDescriptionInterface::kAnswer);
2041 EXPECT_TRUE(answer->Initialize(sdp, NULL));
2042 cricket::ContentInfo* data_info =
2043 answer->description()->GetContentByName("data");
2044 data_info->rejected = true;
2045
2046 DoSetRemoteDescription(answer);
2047 EXPECT_EQ(DataChannelInterface::kClosed, offer_channel->state());
2048}
2049
2050// Test that we can create a session description from an SDP string from
2051// FireFox, use it as a remote session description, generate an answer and use
2052// the answer as a local description.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002053TEST_F(PeerConnectionInterfaceTest, ReceiveFireFoxOffer) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002054 FakeConstraints constraints;
2055 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2056 true);
2057 CreatePeerConnection(&constraints);
2058 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
2059 SessionDescriptionInterface* desc =
2060 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
jbauchfabe2c92015-07-16 13:43:14 -07002061 webrtc::kFireFoxSdpOffer, nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002062 EXPECT_TRUE(DoSetSessionDescription(desc, false));
2063 CreateAnswerAsLocalDescription();
2064 ASSERT_TRUE(pc_->local_description() != NULL);
2065 ASSERT_TRUE(pc_->remote_description() != NULL);
2066
2067 const cricket::ContentInfo* content =
2068 cricket::GetFirstAudioContent(pc_->local_description()->description());
2069 ASSERT_TRUE(content != NULL);
2070 EXPECT_FALSE(content->rejected);
2071
2072 content =
2073 cricket::GetFirstVideoContent(pc_->local_description()->description());
2074 ASSERT_TRUE(content != NULL);
2075 EXPECT_FALSE(content->rejected);
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00002076#ifdef HAVE_SCTP
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002077 content =
2078 cricket::GetFirstDataContent(pc_->local_description()->description());
2079 ASSERT_TRUE(content != NULL);
2080 EXPECT_TRUE(content->rejected);
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00002081#endif
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002082}
2083
deadbeef8662f942017-01-20 21:20:51 -08002084// Test that an offer can be received which offers DTLS with SDES fallback.
2085// Regression test for issue:
2086// https://bugs.chromium.org/p/webrtc/issues/detail?id=6972
2087TEST_F(PeerConnectionInterfaceTest, ReceiveDtlsSdesFallbackOffer) {
2088 FakeConstraints constraints;
2089 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2090 true);
2091 CreatePeerConnection(&constraints);
2092 // Wait for fake certificate to be generated. Previously, this is what caused
2093 // the "a=crypto" lines to be rejected.
2094 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
2095 ASSERT_NE(nullptr, fake_certificate_generator_);
2096 EXPECT_EQ_WAIT(1, fake_certificate_generator_->generated_certificates(),
2097 kTimeout);
2098 SessionDescriptionInterface* desc = webrtc::CreateSessionDescription(
2099 SessionDescriptionInterface::kOffer, kDtlsSdesFallbackSdp, nullptr);
2100 EXPECT_TRUE(DoSetSessionDescription(desc, false));
2101 CreateAnswerAsLocalDescription();
2102}
2103
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002104// Test that we can create an audio only offer and receive an answer with a
2105// limited set of audio codecs and receive an updated offer with more audio
2106// codecs, where the added codecs are not supported.
2107TEST_F(PeerConnectionInterfaceTest, ReceiveUpdatedAudioOfferWithBadCodecs) {
deadbeef293e9262017-01-11 12:28:30 -08002108 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002109 AddVoiceStream("audio_label");
2110 CreateOfferAsLocalDescription();
2111
2112 SessionDescriptionInterface* answer =
2113 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
jbauchfabe2c92015-07-16 13:43:14 -07002114 webrtc::kAudioSdp, nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002115 EXPECT_TRUE(DoSetSessionDescription(answer, false));
2116
2117 SessionDescriptionInterface* updated_offer =
2118 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
jbauchfabe2c92015-07-16 13:43:14 -07002119 webrtc::kAudioSdpWithUnsupportedCodecs,
2120 nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002121 EXPECT_TRUE(DoSetSessionDescription(updated_offer, false));
2122 CreateAnswerAsLocalDescription();
2123}
2124
deadbeefc80741f2015-10-22 13:14:45 -07002125// Test that if we're receiving (but not sending) a track, subsequent offers
2126// will have m-lines with a=recvonly.
2127TEST_F(PeerConnectionInterfaceTest, CreateSubsequentRecvOnlyOffer) {
2128 FakeConstraints constraints;
2129 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2130 true);
2131 CreatePeerConnection(&constraints);
2132 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2133 CreateAnswerAsLocalDescription();
2134
2135 // At this point we should be receiving stream 1, but not sending anything.
2136 // A new offer should be recvonly.
kwibergd1fe2812016-04-27 06:47:29 -07002137 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefc80741f2015-10-22 13:14:45 -07002138 DoCreateOffer(&offer, nullptr);
2139
2140 const cricket::ContentInfo* video_content =
2141 cricket::GetFirstVideoContent(offer->description());
2142 const cricket::VideoContentDescription* video_desc =
2143 static_cast<const cricket::VideoContentDescription*>(
2144 video_content->description);
2145 ASSERT_EQ(cricket::MD_RECVONLY, video_desc->direction());
2146
2147 const cricket::ContentInfo* audio_content =
2148 cricket::GetFirstAudioContent(offer->description());
2149 const cricket::AudioContentDescription* audio_desc =
2150 static_cast<const cricket::AudioContentDescription*>(
2151 audio_content->description);
2152 ASSERT_EQ(cricket::MD_RECVONLY, audio_desc->direction());
2153}
2154
2155// Test that if we're receiving (but not sending) a track, and the
2156// offerToReceiveVideo/offerToReceiveAudio constraints are explicitly set to
2157// false, the generated m-lines will be a=inactive.
2158TEST_F(PeerConnectionInterfaceTest, CreateSubsequentInactiveOffer) {
2159 FakeConstraints constraints;
2160 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2161 true);
2162 CreatePeerConnection(&constraints);
2163 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2164 CreateAnswerAsLocalDescription();
2165
2166 // At this point we should be receiving stream 1, but not sending anything.
2167 // A new offer would be recvonly, but we'll set the "no receive" constraints
2168 // to make it inactive.
kwibergd1fe2812016-04-27 06:47:29 -07002169 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefc80741f2015-10-22 13:14:45 -07002170 FakeConstraints offer_constraints;
2171 offer_constraints.AddMandatory(
2172 webrtc::MediaConstraintsInterface::kOfferToReceiveVideo, false);
2173 offer_constraints.AddMandatory(
2174 webrtc::MediaConstraintsInterface::kOfferToReceiveAudio, false);
2175 DoCreateOffer(&offer, &offer_constraints);
2176
2177 const cricket::ContentInfo* video_content =
2178 cricket::GetFirstVideoContent(offer->description());
2179 const cricket::VideoContentDescription* video_desc =
2180 static_cast<const cricket::VideoContentDescription*>(
2181 video_content->description);
2182 ASSERT_EQ(cricket::MD_INACTIVE, video_desc->direction());
2183
2184 const cricket::ContentInfo* audio_content =
2185 cricket::GetFirstAudioContent(offer->description());
2186 const cricket::AudioContentDescription* audio_desc =
2187 static_cast<const cricket::AudioContentDescription*>(
2188 audio_content->description);
2189 ASSERT_EQ(cricket::MD_INACTIVE, audio_desc->direction());
2190}
2191
deadbeef653b8e02015-11-11 12:55:10 -08002192// Test that we can use SetConfiguration to change the ICE servers of the
2193// PortAllocator.
2194TEST_F(PeerConnectionInterfaceTest, SetConfigurationChangesIceServers) {
2195 CreatePeerConnection();
2196
2197 PeerConnectionInterface::RTCConfiguration config;
2198 PeerConnectionInterface::IceServer server;
2199 server.uri = "stun:test_hostname";
2200 config.servers.push_back(server);
2201 EXPECT_TRUE(pc_->SetConfiguration(config));
2202
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002203 EXPECT_EQ(1u, port_allocator_->stun_servers().size());
2204 EXPECT_EQ("test_hostname",
2205 port_allocator_->stun_servers().begin()->hostname());
deadbeef653b8e02015-11-11 12:55:10 -08002206}
2207
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002208TEST_F(PeerConnectionInterfaceTest, SetConfigurationChangesCandidateFilter) {
2209 CreatePeerConnection();
2210 PeerConnectionInterface::RTCConfiguration config;
2211 config.type = PeerConnectionInterface::kRelay;
2212 EXPECT_TRUE(pc_->SetConfiguration(config));
2213 EXPECT_EQ(cricket::CF_RELAY, port_allocator_->candidate_filter());
2214}
2215
deadbeef293e9262017-01-11 12:28:30 -08002216TEST_F(PeerConnectionInterfaceTest, SetConfigurationChangesPruneTurnPortsFlag) {
2217 PeerConnectionInterface::RTCConfiguration config;
2218 config.prune_turn_ports = false;
2219 CreatePeerConnection(config, nullptr);
2220 EXPECT_FALSE(port_allocator_->prune_turn_ports());
2221
2222 config.prune_turn_ports = true;
2223 EXPECT_TRUE(pc_->SetConfiguration(config));
2224 EXPECT_TRUE(port_allocator_->prune_turn_ports());
2225}
2226
skvladd1f5fda2017-02-03 16:54:05 -08002227// Test that the ice check interval can be changed. This does not verify that
2228// the setting makes it all the way to P2PTransportChannel, as that would
2229// require a very complex set of mocks.
2230TEST_F(PeerConnectionInterfaceTest, SetConfigurationChangesIceCheckInterval) {
2231 PeerConnectionInterface::RTCConfiguration config;
2232 config.ice_check_min_interval = rtc::Optional<int>();
2233 CreatePeerConnection(config, nullptr);
2234 config.ice_check_min_interval = rtc::Optional<int>(100);
2235 EXPECT_TRUE(pc_->SetConfiguration(config));
2236 PeerConnectionInterface::RTCConfiguration new_config =
2237 pc_->GetConfiguration();
2238 EXPECT_EQ(new_config.ice_check_min_interval, rtc::Optional<int>(100));
2239}
2240
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002241// Test that when SetConfiguration changes both the pool size and other
2242// attributes, the pooled session is created with the updated attributes.
2243TEST_F(PeerConnectionInterfaceTest,
2244 SetConfigurationCreatesPooledSessionCorrectly) {
2245 CreatePeerConnection();
2246 PeerConnectionInterface::RTCConfiguration config;
2247 config.ice_candidate_pool_size = 1;
2248 PeerConnectionInterface::IceServer server;
2249 server.uri = kStunAddressOnly;
2250 config.servers.push_back(server);
2251 config.type = PeerConnectionInterface::kRelay;
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07002252 EXPECT_TRUE(pc_->SetConfiguration(config));
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002253
2254 const cricket::FakePortAllocatorSession* session =
2255 static_cast<const cricket::FakePortAllocatorSession*>(
2256 port_allocator_->GetPooledSession());
2257 ASSERT_NE(nullptr, session);
2258 EXPECT_EQ(1UL, session->stun_servers().size());
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002259}
2260
deadbeef293e9262017-01-11 12:28:30 -08002261// Test that after SetLocalDescription, changing the pool size is not allowed,
2262// and an invalid modification error is returned.
deadbeef6de92f92016-12-12 18:49:32 -08002263TEST_F(PeerConnectionInterfaceTest,
2264 CantChangePoolSizeAfterSetLocalDescription) {
2265 CreatePeerConnection();
2266 // Start by setting a size of 1.
2267 PeerConnectionInterface::RTCConfiguration config;
2268 config.ice_candidate_pool_size = 1;
2269 EXPECT_TRUE(pc_->SetConfiguration(config));
2270
2271 // Set remote offer; can still change pool size at this point.
2272 CreateOfferAsRemoteDescription();
2273 config.ice_candidate_pool_size = 2;
2274 EXPECT_TRUE(pc_->SetConfiguration(config));
2275
2276 // Set local answer; now it's too late.
2277 CreateAnswerAsLocalDescription();
2278 config.ice_candidate_pool_size = 3;
deadbeef293e9262017-01-11 12:28:30 -08002279 RTCError error;
2280 EXPECT_FALSE(pc_->SetConfiguration(config, &error));
2281 EXPECT_EQ(RTCErrorType::INVALID_MODIFICATION, error.type());
2282}
2283
deadbeef42a42632017-03-10 15:18:00 -08002284// Test that after setting an answer, extra pooled sessions are discarded. The
2285// ICE candidate pool is only intended to be used for the first offer/answer.
2286TEST_F(PeerConnectionInterfaceTest,
2287 ExtraPooledSessionsDiscardedAfterApplyingAnswer) {
2288 CreatePeerConnection();
2289
2290 // Set a larger-than-necessary size.
2291 PeerConnectionInterface::RTCConfiguration config;
2292 config.ice_candidate_pool_size = 4;
2293 EXPECT_TRUE(pc_->SetConfiguration(config));
2294
2295 // Do offer/answer.
2296 CreateOfferAsRemoteDescription();
2297 CreateAnswerAsLocalDescription();
2298
2299 // Expect no pooled sessions to be left.
2300 const cricket::PortAllocatorSession* session =
2301 port_allocator_->GetPooledSession();
2302 EXPECT_EQ(nullptr, session);
2303}
2304
2305// After Close is called, pooled candidates should be discarded so as to not
2306// waste network resources.
2307TEST_F(PeerConnectionInterfaceTest, PooledSessionsDiscardedAfterClose) {
2308 CreatePeerConnection();
2309
2310 PeerConnectionInterface::RTCConfiguration config;
2311 config.ice_candidate_pool_size = 3;
2312 EXPECT_TRUE(pc_->SetConfiguration(config));
2313 pc_->Close();
2314
2315 // Expect no pooled sessions to be left.
2316 const cricket::PortAllocatorSession* session =
2317 port_allocator_->GetPooledSession();
2318 EXPECT_EQ(nullptr, session);
2319}
2320
deadbeef293e9262017-01-11 12:28:30 -08002321// Test that SetConfiguration returns an invalid modification error if
2322// modifying a field in the configuration that isn't allowed to be modified.
2323TEST_F(PeerConnectionInterfaceTest,
2324 SetConfigurationReturnsInvalidModificationError) {
2325 PeerConnectionInterface::RTCConfiguration config;
2326 config.bundle_policy = PeerConnectionInterface::kBundlePolicyBalanced;
2327 config.rtcp_mux_policy = PeerConnectionInterface::kRtcpMuxPolicyNegotiate;
2328 config.continual_gathering_policy = PeerConnectionInterface::GATHER_ONCE;
2329 CreatePeerConnection(config, nullptr);
2330
2331 PeerConnectionInterface::RTCConfiguration modified_config = config;
2332 modified_config.bundle_policy =
2333 PeerConnectionInterface::kBundlePolicyMaxBundle;
2334 RTCError error;
2335 EXPECT_FALSE(pc_->SetConfiguration(modified_config, &error));
2336 EXPECT_EQ(RTCErrorType::INVALID_MODIFICATION, error.type());
2337
2338 modified_config = config;
2339 modified_config.rtcp_mux_policy =
2340 PeerConnectionInterface::kRtcpMuxPolicyRequire;
2341 error.set_type(RTCErrorType::NONE);
2342 EXPECT_FALSE(pc_->SetConfiguration(modified_config, &error));
2343 EXPECT_EQ(RTCErrorType::INVALID_MODIFICATION, error.type());
2344
2345 modified_config = config;
2346 modified_config.continual_gathering_policy =
2347 PeerConnectionInterface::GATHER_CONTINUALLY;
2348 error.set_type(RTCErrorType::NONE);
2349 EXPECT_FALSE(pc_->SetConfiguration(modified_config, &error));
2350 EXPECT_EQ(RTCErrorType::INVALID_MODIFICATION, error.type());
2351}
2352
2353// Test that SetConfiguration returns a range error if the candidate pool size
2354// is negative or larger than allowed by the spec.
2355TEST_F(PeerConnectionInterfaceTest,
2356 SetConfigurationReturnsRangeErrorForBadCandidatePoolSize) {
2357 PeerConnectionInterface::RTCConfiguration config;
2358 CreatePeerConnection(config, nullptr);
2359
2360 config.ice_candidate_pool_size = -1;
2361 RTCError error;
2362 EXPECT_FALSE(pc_->SetConfiguration(config, &error));
2363 EXPECT_EQ(RTCErrorType::INVALID_RANGE, error.type());
2364
2365 config.ice_candidate_pool_size = INT_MAX;
2366 error.set_type(RTCErrorType::NONE);
2367 EXPECT_FALSE(pc_->SetConfiguration(config, &error));
2368 EXPECT_EQ(RTCErrorType::INVALID_RANGE, error.type());
2369}
2370
2371// Test that SetConfiguration returns a syntax error if parsing an ICE server
2372// URL failed.
2373TEST_F(PeerConnectionInterfaceTest,
2374 SetConfigurationReturnsSyntaxErrorFromBadIceUrls) {
2375 PeerConnectionInterface::RTCConfiguration config;
2376 CreatePeerConnection(config, nullptr);
2377
2378 PeerConnectionInterface::IceServer bad_server;
2379 bad_server.uri = "stunn:www.example.com";
2380 config.servers.push_back(bad_server);
2381 RTCError error;
2382 EXPECT_FALSE(pc_->SetConfiguration(config, &error));
2383 EXPECT_EQ(RTCErrorType::SYNTAX_ERROR, error.type());
2384}
2385
2386// Test that SetConfiguration returns an invalid parameter error if a TURN
2387// IceServer is missing a username or password.
2388TEST_F(PeerConnectionInterfaceTest,
2389 SetConfigurationReturnsInvalidParameterIfCredentialsMissing) {
2390 PeerConnectionInterface::RTCConfiguration config;
2391 CreatePeerConnection(config, nullptr);
2392
2393 PeerConnectionInterface::IceServer bad_server;
2394 bad_server.uri = "turn:www.example.com";
2395 // Missing password.
2396 bad_server.username = "foo";
2397 config.servers.push_back(bad_server);
2398 RTCError error;
2399 EXPECT_FALSE(pc_->SetConfiguration(config, &error));
2400 EXPECT_EQ(RTCErrorType::INVALID_PARAMETER, error.type());
deadbeef6de92f92016-12-12 18:49:32 -08002401}
2402
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002403// Test that PeerConnection::Close changes the states to closed and all remote
2404// tracks change state to ended.
2405TEST_F(PeerConnectionInterfaceTest, CloseAndTestStreamsAndStates) {
2406 // Initialize a PeerConnection and negotiate local and remote session
2407 // description.
2408 InitiateCall();
2409 ASSERT_EQ(1u, pc_->local_streams()->count());
2410 ASSERT_EQ(1u, pc_->remote_streams()->count());
2411
2412 pc_->Close();
2413
2414 EXPECT_EQ(PeerConnectionInterface::kClosed, pc_->signaling_state());
2415 EXPECT_EQ(PeerConnectionInterface::kIceConnectionClosed,
2416 pc_->ice_connection_state());
2417 EXPECT_EQ(PeerConnectionInterface::kIceGatheringComplete,
2418 pc_->ice_gathering_state());
2419
2420 EXPECT_EQ(1u, pc_->local_streams()->count());
2421 EXPECT_EQ(1u, pc_->remote_streams()->count());
2422
zhihuang9763d562016-08-05 11:14:50 -07002423 rtc::scoped_refptr<MediaStreamInterface> remote_stream =
2424 pc_->remote_streams()->at(0);
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002425 // Track state may be updated asynchronously.
perkjd61bf802016-03-24 03:16:19 -07002426 EXPECT_EQ_WAIT(MediaStreamTrackInterface::kEnded,
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002427 remote_stream->GetAudioTracks()[0]->state(), kTimeout);
2428 EXPECT_EQ_WAIT(MediaStreamTrackInterface::kEnded,
2429 remote_stream->GetVideoTracks()[0]->state(), kTimeout);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002430}
2431
2432// Test that PeerConnection methods fails gracefully after
2433// PeerConnection::Close has been called.
2434TEST_F(PeerConnectionInterfaceTest, CloseAndTestMethods) {
deadbeef293e9262017-01-11 12:28:30 -08002435 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002436 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
2437 CreateOfferAsRemoteDescription();
2438 CreateAnswerAsLocalDescription();
2439
2440 ASSERT_EQ(1u, pc_->local_streams()->count());
zhihuang9763d562016-08-05 11:14:50 -07002441 rtc::scoped_refptr<MediaStreamInterface> local_stream =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002442 pc_->local_streams()->at(0);
2443
2444 pc_->Close();
2445
2446 pc_->RemoveStream(local_stream);
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +00002447 EXPECT_FALSE(pc_->AddStream(local_stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002448
2449 ASSERT_FALSE(local_stream->GetAudioTracks().empty());
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002450 rtc::scoped_refptr<webrtc::DtmfSenderInterface> dtmf_sender(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002451 pc_->CreateDtmfSender(local_stream->GetAudioTracks()[0]));
wu@webrtc.org66037362013-08-13 00:09:35 +00002452 EXPECT_TRUE(NULL == dtmf_sender); // local stream has been removed.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002453
2454 EXPECT_TRUE(pc_->CreateDataChannel("test", NULL) == NULL);
2455
2456 EXPECT_TRUE(pc_->local_description() != NULL);
2457 EXPECT_TRUE(pc_->remote_description() != NULL);
2458
kwibergd1fe2812016-04-27 06:47:29 -07002459 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07002460 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
kwibergd1fe2812016-04-27 06:47:29 -07002461 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -07002462 EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002463
2464 std::string sdp;
2465 ASSERT_TRUE(pc_->remote_description()->ToString(&sdp));
2466 SessionDescriptionInterface* remote_offer =
2467 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
2468 sdp, NULL);
2469 EXPECT_FALSE(DoSetRemoteDescription(remote_offer));
2470
2471 ASSERT_TRUE(pc_->local_description()->ToString(&sdp));
2472 SessionDescriptionInterface* local_offer =
2473 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
2474 sdp, NULL);
2475 EXPECT_FALSE(DoSetLocalDescription(local_offer));
2476}
2477
2478// Test that GetStats can still be called after PeerConnection::Close.
2479TEST_F(PeerConnectionInterfaceTest, CloseAndGetStats) {
2480 InitiateCall();
2481 pc_->Close();
2482 DoGetStats(NULL);
2483}
deadbeefab9b2d12015-10-14 11:33:11 -07002484
2485// NOTE: The series of tests below come from what used to be
2486// mediastreamsignaling_unittest.cc, and are mostly aimed at testing that
2487// setting a remote or local description has the expected effects.
2488
2489// This test verifies that the remote MediaStreams corresponding to a received
2490// SDP string is created. In this test the two separate MediaStreams are
2491// signaled.
2492TEST_F(PeerConnectionInterfaceTest, UpdateRemoteStreams) {
2493 FakeConstraints constraints;
2494 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2495 true);
2496 CreatePeerConnection(&constraints);
2497 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2498
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002499 rtc::scoped_refptr<StreamCollection> reference(CreateStreamCollection(1, 1));
deadbeefab9b2d12015-10-14 11:33:11 -07002500 EXPECT_TRUE(
2501 CompareStreamCollections(observer_.remote_streams(), reference.get()));
2502 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2503 EXPECT_TRUE(remote_stream->GetVideoTracks()[0]->GetSource() != nullptr);
2504
2505 // Create a session description based on another SDP with another
2506 // MediaStream.
2507 CreateAndSetRemoteOffer(kSdpStringWithStream1And2);
2508
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002509 rtc::scoped_refptr<StreamCollection> reference2(CreateStreamCollection(2, 1));
deadbeefab9b2d12015-10-14 11:33:11 -07002510 EXPECT_TRUE(
2511 CompareStreamCollections(observer_.remote_streams(), reference2.get()));
2512}
2513
2514// This test verifies that when remote tracks are added/removed from SDP, the
2515// created remote streams are updated appropriately.
2516TEST_F(PeerConnectionInterfaceTest,
2517 AddRemoveTrackFromExistingRemoteMediaStream) {
2518 FakeConstraints constraints;
2519 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2520 true);
2521 CreatePeerConnection(&constraints);
kwibergd1fe2812016-04-27 06:47:29 -07002522 std::unique_ptr<SessionDescriptionInterface> desc_ms1 =
kwiberg2bbff992016-03-16 11:03:04 -07002523 CreateSessionDescriptionAndReference(1, 1);
deadbeefab9b2d12015-10-14 11:33:11 -07002524 EXPECT_TRUE(DoSetRemoteDescription(desc_ms1.release()));
2525 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
2526 reference_collection_));
2527
2528 // Add extra audio and video tracks to the same MediaStream.
kwibergd1fe2812016-04-27 06:47:29 -07002529 std::unique_ptr<SessionDescriptionInterface> desc_ms1_two_tracks =
kwiberg2bbff992016-03-16 11:03:04 -07002530 CreateSessionDescriptionAndReference(2, 2);
deadbeefab9b2d12015-10-14 11:33:11 -07002531 EXPECT_TRUE(DoSetRemoteDescription(desc_ms1_two_tracks.release()));
2532 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
2533 reference_collection_));
zhihuang9763d562016-08-05 11:14:50 -07002534 rtc::scoped_refptr<AudioTrackInterface> audio_track2 =
perkjd61bf802016-03-24 03:16:19 -07002535 observer_.remote_streams()->at(0)->GetAudioTracks()[1];
2536 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, audio_track2->state());
zhihuang9763d562016-08-05 11:14:50 -07002537 rtc::scoped_refptr<VideoTrackInterface> video_track2 =
perkjd61bf802016-03-24 03:16:19 -07002538 observer_.remote_streams()->at(0)->GetVideoTracks()[1];
2539 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, video_track2->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002540
2541 // Remove the extra audio and video tracks.
kwibergd1fe2812016-04-27 06:47:29 -07002542 std::unique_ptr<SessionDescriptionInterface> desc_ms2 =
kwiberg2bbff992016-03-16 11:03:04 -07002543 CreateSessionDescriptionAndReference(1, 1);
perkjd61bf802016-03-24 03:16:19 -07002544 MockTrackObserver audio_track_observer(audio_track2);
2545 MockTrackObserver video_track_observer(video_track2);
2546
2547 EXPECT_CALL(audio_track_observer, OnChanged()).Times(Exactly(1));
2548 EXPECT_CALL(video_track_observer, OnChanged()).Times(Exactly(1));
deadbeefab9b2d12015-10-14 11:33:11 -07002549 EXPECT_TRUE(DoSetRemoteDescription(desc_ms2.release()));
2550 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
2551 reference_collection_));
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002552 // Track state may be updated asynchronously.
perkjd61bf802016-03-24 03:16:19 -07002553 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002554 audio_track2->state(), kTimeout);
2555 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
2556 video_track2->state(), kTimeout);
deadbeefab9b2d12015-10-14 11:33:11 -07002557}
2558
2559// This tests that remote tracks are ended if a local session description is set
2560// that rejects the media content type.
2561TEST_F(PeerConnectionInterfaceTest, RejectMediaContent) {
2562 FakeConstraints constraints;
2563 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2564 true);
2565 CreatePeerConnection(&constraints);
2566 // First create and set a remote offer, then reject its video content in our
2567 // answer.
2568 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2569 ASSERT_EQ(1u, observer_.remote_streams()->count());
2570 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2571 ASSERT_EQ(1u, remote_stream->GetVideoTracks().size());
2572 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2573
2574 rtc::scoped_refptr<webrtc::VideoTrackInterface> remote_video =
2575 remote_stream->GetVideoTracks()[0];
2576 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, remote_video->state());
2577 rtc::scoped_refptr<webrtc::AudioTrackInterface> remote_audio =
2578 remote_stream->GetAudioTracks()[0];
2579 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, remote_audio->state());
2580
kwibergd1fe2812016-04-27 06:47:29 -07002581 std::unique_ptr<SessionDescriptionInterface> local_answer;
kwiberg2bbff992016-03-16 11:03:04 -07002582 EXPECT_TRUE(DoCreateAnswer(&local_answer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07002583 cricket::ContentInfo* video_info =
2584 local_answer->description()->GetContentByName("video");
2585 video_info->rejected = true;
2586 EXPECT_TRUE(DoSetLocalDescription(local_answer.release()));
2587 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kEnded, remote_video->state());
2588 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, remote_audio->state());
2589
2590 // Now create an offer where we reject both video and audio.
kwibergd1fe2812016-04-27 06:47:29 -07002591 std::unique_ptr<SessionDescriptionInterface> local_offer;
kwiberg2bbff992016-03-16 11:03:04 -07002592 EXPECT_TRUE(DoCreateOffer(&local_offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07002593 video_info = local_offer->description()->GetContentByName("video");
2594 ASSERT_TRUE(video_info != nullptr);
2595 video_info->rejected = true;
2596 cricket::ContentInfo* audio_info =
2597 local_offer->description()->GetContentByName("audio");
2598 ASSERT_TRUE(audio_info != nullptr);
2599 audio_info->rejected = true;
2600 EXPECT_TRUE(DoSetLocalDescription(local_offer.release()));
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002601 // Track state may be updated asynchronously.
perkjd61bf802016-03-24 03:16:19 -07002602 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002603 remote_audio->state(), kTimeout);
2604 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
2605 remote_video->state(), kTimeout);
deadbeefab9b2d12015-10-14 11:33:11 -07002606}
2607
2608// This tests that we won't crash if the remote track has been removed outside
2609// of PeerConnection and then PeerConnection tries to reject the track.
2610TEST_F(PeerConnectionInterfaceTest, RemoveTrackThenRejectMediaContent) {
2611 FakeConstraints constraints;
2612 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2613 true);
2614 CreatePeerConnection(&constraints);
2615 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2616 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2617 remote_stream->RemoveTrack(remote_stream->GetVideoTracks()[0]);
2618 remote_stream->RemoveTrack(remote_stream->GetAudioTracks()[0]);
2619
kwibergd1fe2812016-04-27 06:47:29 -07002620 std::unique_ptr<SessionDescriptionInterface> local_answer(
deadbeefab9b2d12015-10-14 11:33:11 -07002621 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
2622 kSdpStringWithStream1, nullptr));
2623 cricket::ContentInfo* video_info =
2624 local_answer->description()->GetContentByName("video");
2625 video_info->rejected = true;
2626 cricket::ContentInfo* audio_info =
2627 local_answer->description()->GetContentByName("audio");
2628 audio_info->rejected = true;
2629 EXPECT_TRUE(DoSetLocalDescription(local_answer.release()));
2630
2631 // No crash is a pass.
2632}
2633
deadbeef5e97fb52015-10-15 12:49:08 -07002634// This tests that if a recvonly remote description is set, no remote streams
2635// will be created, even if the description contains SSRCs/MSIDs.
2636// See: https://code.google.com/p/webrtc/issues/detail?id=5054
2637TEST_F(PeerConnectionInterfaceTest, RecvonlyDescriptionDoesntCreateStream) {
2638 FakeConstraints constraints;
2639 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2640 true);
2641 CreatePeerConnection(&constraints);
2642
2643 std::string recvonly_offer = kSdpStringWithStream1;
2644 rtc::replace_substrs(kSendrecv, strlen(kSendrecv), kRecvonly,
2645 strlen(kRecvonly), &recvonly_offer);
2646 CreateAndSetRemoteOffer(recvonly_offer);
2647
2648 EXPECT_EQ(0u, observer_.remote_streams()->count());
2649}
2650
deadbeefab9b2d12015-10-14 11:33:11 -07002651// This tests that a default MediaStream is created if a remote session
2652// description doesn't contain any streams and no MSID support.
2653// It also tests that the default stream is updated if a video m-line is added
2654// in a subsequent session description.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002655TEST_F(PeerConnectionInterfaceTest, SdpWithoutMsidCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002656 FakeConstraints constraints;
2657 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2658 true);
2659 CreatePeerConnection(&constraints);
2660 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2661
2662 ASSERT_EQ(1u, observer_.remote_streams()->count());
2663 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2664
2665 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2666 EXPECT_EQ(0u, remote_stream->GetVideoTracks().size());
2667 EXPECT_EQ("default", remote_stream->label());
2668
2669 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2670 ASSERT_EQ(1u, observer_.remote_streams()->count());
2671 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2672 EXPECT_EQ("defaulta0", remote_stream->GetAudioTracks()[0]->id());
deadbeef884f5852016-01-15 09:20:04 -08002673 EXPECT_EQ(MediaStreamTrackInterface::kLive,
2674 remote_stream->GetAudioTracks()[0]->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002675 ASSERT_EQ(1u, remote_stream->GetVideoTracks().size());
2676 EXPECT_EQ("defaultv0", remote_stream->GetVideoTracks()[0]->id());
deadbeef884f5852016-01-15 09:20:04 -08002677 EXPECT_EQ(MediaStreamTrackInterface::kLive,
2678 remote_stream->GetVideoTracks()[0]->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002679}
2680
2681// This tests that a default MediaStream is created if a remote session
2682// description doesn't contain any streams and media direction is send only.
2683TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002684 SendOnlySdpWithoutMsidCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002685 FakeConstraints constraints;
2686 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2687 true);
2688 CreatePeerConnection(&constraints);
2689 CreateAndSetRemoteOffer(kSdpStringSendOnlyWithoutStreams);
2690
2691 ASSERT_EQ(1u, observer_.remote_streams()->count());
2692 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2693
2694 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2695 EXPECT_EQ(1u, remote_stream->GetVideoTracks().size());
2696 EXPECT_EQ("default", remote_stream->label());
2697}
2698
2699// This tests that it won't crash when PeerConnection tries to remove
2700// a remote track that as already been removed from the MediaStream.
2701TEST_F(PeerConnectionInterfaceTest, RemoveAlreadyGoneRemoteStream) {
2702 FakeConstraints constraints;
2703 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2704 true);
2705 CreatePeerConnection(&constraints);
2706 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2707 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2708 remote_stream->RemoveTrack(remote_stream->GetAudioTracks()[0]);
2709 remote_stream->RemoveTrack(remote_stream->GetVideoTracks()[0]);
2710
2711 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2712
2713 // No crash is a pass.
2714}
2715
2716// This tests that a default MediaStream is created if the remote session
2717// description doesn't contain any streams and don't contain an indication if
2718// MSID is supported.
2719TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002720 SdpWithoutMsidAndStreamsCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002721 FakeConstraints constraints;
2722 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2723 true);
2724 CreatePeerConnection(&constraints);
2725 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2726
2727 ASSERT_EQ(1u, observer_.remote_streams()->count());
2728 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2729 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2730 EXPECT_EQ(1u, remote_stream->GetVideoTracks().size());
2731}
2732
2733// This tests that a default MediaStream is not created if the remote session
2734// description doesn't contain any streams but does support MSID.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002735TEST_F(PeerConnectionInterfaceTest, SdpWithMsidDontCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002736 FakeConstraints constraints;
2737 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2738 true);
2739 CreatePeerConnection(&constraints);
2740 CreateAndSetRemoteOffer(kSdpStringWithMsidWithoutStreams);
2741 EXPECT_EQ(0u, observer_.remote_streams()->count());
2742}
2743
deadbeefbda7e0b2015-12-08 17:13:40 -08002744// This tests that when setting a new description, the old default tracks are
2745// not destroyed and recreated.
2746// See: https://bugs.chromium.org/p/webrtc/issues/detail?id=5250
Stefan Holmer102362b2016-03-18 09:39:07 +01002747TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002748 DefaultTracksNotDestroyedAndRecreated) {
deadbeefbda7e0b2015-12-08 17:13:40 -08002749 FakeConstraints constraints;
2750 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2751 true);
2752 CreatePeerConnection(&constraints);
2753 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2754
2755 ASSERT_EQ(1u, observer_.remote_streams()->count());
2756 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2757 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2758
2759 // Set the track to "disabled", then set a new description and ensure the
2760 // track is still disabled, which ensures it hasn't been recreated.
2761 remote_stream->GetAudioTracks()[0]->set_enabled(false);
2762 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2763 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2764 EXPECT_FALSE(remote_stream->GetAudioTracks()[0]->enabled());
2765}
2766
deadbeefab9b2d12015-10-14 11:33:11 -07002767// This tests that a default MediaStream is not created if a remote session
2768// description is updated to not have any MediaStreams.
2769TEST_F(PeerConnectionInterfaceTest, VerifyDefaultStreamIsNotCreated) {
2770 FakeConstraints constraints;
2771 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2772 true);
2773 CreatePeerConnection(&constraints);
2774 CreateAndSetRemoteOffer(kSdpStringWithStream1);
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002775 rtc::scoped_refptr<StreamCollection> reference(CreateStreamCollection(1, 1));
deadbeefab9b2d12015-10-14 11:33:11 -07002776 EXPECT_TRUE(
2777 CompareStreamCollections(observer_.remote_streams(), reference.get()));
2778
2779 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2780 EXPECT_EQ(0u, observer_.remote_streams()->count());
2781}
2782
2783// This tests that an RtpSender is created when the local description is set
2784// after adding a local stream.
2785// TODO(deadbeef): This test and the one below it need to be updated when
2786// an RtpSender's lifetime isn't determined by when a local description is set.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002787TEST_F(PeerConnectionInterfaceTest, LocalDescriptionChanged) {
deadbeefab9b2d12015-10-14 11:33:11 -07002788 FakeConstraints constraints;
2789 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2790 true);
2791 CreatePeerConnection(&constraints);
deadbeefab9b2d12015-10-14 11:33:11 -07002792
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002793 // Create an offer with 1 stream with 2 tracks of each type.
2794 rtc::scoped_refptr<StreamCollection> stream_collection =
2795 CreateStreamCollection(1, 2);
2796 pc_->AddStream(stream_collection->at(0));
2797 std::unique_ptr<SessionDescriptionInterface> offer;
2798 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2799 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
deadbeefab9b2d12015-10-14 11:33:11 -07002800
deadbeefab9b2d12015-10-14 11:33:11 -07002801 auto senders = pc_->GetSenders();
2802 EXPECT_EQ(4u, senders.size());
2803 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2804 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2805 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[1]));
2806 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[1]));
2807
2808 // Remove an audio and video track.
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002809 pc_->RemoveStream(stream_collection->at(0));
2810 stream_collection = CreateStreamCollection(1, 1);
2811 pc_->AddStream(stream_collection->at(0));
2812 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2813 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
2814
deadbeefab9b2d12015-10-14 11:33:11 -07002815 senders = pc_->GetSenders();
2816 EXPECT_EQ(2u, senders.size());
2817 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2818 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2819 EXPECT_FALSE(ContainsSender(senders, kAudioTracks[1]));
2820 EXPECT_FALSE(ContainsSender(senders, kVideoTracks[1]));
2821}
2822
2823// This tests that an RtpSender is created when the local description is set
2824// before adding a local stream.
2825TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002826 AddLocalStreamAfterLocalDescriptionChanged) {
deadbeefab9b2d12015-10-14 11:33:11 -07002827 FakeConstraints constraints;
2828 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2829 true);
2830 CreatePeerConnection(&constraints);
deadbeefab9b2d12015-10-14 11:33:11 -07002831
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002832 rtc::scoped_refptr<StreamCollection> stream_collection =
2833 CreateStreamCollection(1, 2);
2834 // Add a stream to create the offer, but remove it afterwards.
2835 pc_->AddStream(stream_collection->at(0));
2836 std::unique_ptr<SessionDescriptionInterface> offer;
2837 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2838 pc_->RemoveStream(stream_collection->at(0));
deadbeefab9b2d12015-10-14 11:33:11 -07002839
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002840 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
deadbeefab9b2d12015-10-14 11:33:11 -07002841 auto senders = pc_->GetSenders();
2842 EXPECT_EQ(0u, senders.size());
2843
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002844 pc_->AddStream(stream_collection->at(0));
deadbeefab9b2d12015-10-14 11:33:11 -07002845 senders = pc_->GetSenders();
2846 EXPECT_EQ(4u, senders.size());
2847 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2848 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2849 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[1]));
2850 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[1]));
2851}
2852
2853// This tests that the expected behavior occurs if the SSRC on a local track is
2854// changed when SetLocalDescription is called.
2855TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002856 ChangeSsrcOnTrackInLocalSessionDescription) {
deadbeefab9b2d12015-10-14 11:33:11 -07002857 FakeConstraints constraints;
2858 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2859 true);
2860 CreatePeerConnection(&constraints);
deadbeefab9b2d12015-10-14 11:33:11 -07002861
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002862 rtc::scoped_refptr<StreamCollection> stream_collection =
2863 CreateStreamCollection(2, 1);
2864 pc_->AddStream(stream_collection->at(0));
2865 std::unique_ptr<SessionDescriptionInterface> offer;
2866 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2867 // Grab a copy of the offer before it gets passed into the PC.
2868 std::unique_ptr<JsepSessionDescription> modified_offer(
2869 new JsepSessionDescription(JsepSessionDescription::kOffer));
2870 modified_offer->Initialize(offer->description()->Copy(), offer->session_id(),
2871 offer->session_version());
2872 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
deadbeefab9b2d12015-10-14 11:33:11 -07002873
deadbeefab9b2d12015-10-14 11:33:11 -07002874 auto senders = pc_->GetSenders();
2875 EXPECT_EQ(2u, senders.size());
2876 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2877 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2878
2879 // Change the ssrc of the audio and video track.
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002880 cricket::MediaContentDescription* desc =
2881 cricket::GetFirstAudioContentDescription(modified_offer->description());
2882 ASSERT_TRUE(desc != NULL);
2883 for (StreamParams& stream : desc->mutable_streams()) {
2884 for (unsigned int& ssrc : stream.ssrcs) {
2885 ++ssrc;
2886 }
2887 }
deadbeefab9b2d12015-10-14 11:33:11 -07002888
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002889 desc =
2890 cricket::GetFirstVideoContentDescription(modified_offer->description());
2891 ASSERT_TRUE(desc != NULL);
2892 for (StreamParams& stream : desc->mutable_streams()) {
2893 for (unsigned int& ssrc : stream.ssrcs) {
2894 ++ssrc;
2895 }
2896 }
2897
2898 EXPECT_TRUE(DoSetLocalDescription(modified_offer.release()));
deadbeefab9b2d12015-10-14 11:33:11 -07002899 senders = pc_->GetSenders();
2900 EXPECT_EQ(2u, senders.size());
2901 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2902 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2903 // TODO(deadbeef): Once RtpSenders expose parameters, check that the SSRC
2904 // changed.
2905}
2906
2907// This tests that the expected behavior occurs if a new session description is
2908// set with the same tracks, but on a different MediaStream.
Stefan Holmer55d6e7c2016-03-17 16:26:40 +01002909TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002910 SignalSameTracksInSeparateMediaStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002911 FakeConstraints constraints;
2912 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2913 true);
2914 CreatePeerConnection(&constraints);
deadbeefab9b2d12015-10-14 11:33:11 -07002915
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002916 rtc::scoped_refptr<StreamCollection> stream_collection =
2917 CreateStreamCollection(2, 1);
2918 pc_->AddStream(stream_collection->at(0));
2919 std::unique_ptr<SessionDescriptionInterface> offer;
2920 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2921 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
deadbeefab9b2d12015-10-14 11:33:11 -07002922
deadbeefab9b2d12015-10-14 11:33:11 -07002923 auto senders = pc_->GetSenders();
2924 EXPECT_EQ(2u, senders.size());
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002925 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0], kStreams[0]));
2926 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0], kStreams[0]));
deadbeefab9b2d12015-10-14 11:33:11 -07002927
2928 // Add a new MediaStream but with the same tracks as in the first stream.
2929 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream_1(
2930 webrtc::MediaStream::Create(kStreams[1]));
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002931 stream_1->AddTrack(stream_collection->at(0)->GetVideoTracks()[0]);
2932 stream_1->AddTrack(stream_collection->at(0)->GetAudioTracks()[0]);
deadbeefab9b2d12015-10-14 11:33:11 -07002933 pc_->AddStream(stream_1);
2934
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002935 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2936 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
deadbeefab9b2d12015-10-14 11:33:11 -07002937
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002938 auto new_senders = pc_->GetSenders();
2939 // Should be the same senders as before, but with updated stream id.
2940 // Note that this behavior is subject to change in the future.
2941 // We may decide the PC should ignore existing tracks in AddStream.
2942 EXPECT_EQ(senders, new_senders);
2943 EXPECT_TRUE(ContainsSender(new_senders, kAudioTracks[0], kStreams[1]));
2944 EXPECT_TRUE(ContainsSender(new_senders, kVideoTracks[0], kStreams[1]));
deadbeefab9b2d12015-10-14 11:33:11 -07002945}
2946
zhihuang81c3a032016-11-17 12:06:24 -08002947// This tests that PeerConnectionObserver::OnAddTrack is correctly called.
2948TEST_F(PeerConnectionInterfaceTest, OnAddTrackCallback) {
2949 FakeConstraints constraints;
2950 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2951 true);
2952 CreatePeerConnection(&constraints);
2953 CreateAndSetRemoteOffer(kSdpStringWithStream1AudioTrackOnly);
2954 EXPECT_EQ(observer_.num_added_tracks_, 1);
2955 EXPECT_EQ(observer_.last_added_track_label_, kAudioTracks[0]);
2956
2957 // Create and set the updated remote SDP.
2958 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2959 EXPECT_EQ(observer_.num_added_tracks_, 2);
2960 EXPECT_EQ(observer_.last_added_track_label_, kVideoTracks[0]);
2961}
2962
deadbeefd1a38b52016-12-10 13:15:33 -08002963// Test that when SetConfiguration is called and the configuration is
2964// changing, the next offer causes an ICE restart.
2965TEST_F(PeerConnectionInterfaceTest, SetConfigurationCausingIceRetart) {
2966 PeerConnectionInterface::RTCConfiguration config;
2967 config.type = PeerConnectionInterface::kRelay;
2968 // Need to pass default constraints to prevent disabling of DTLS...
2969 FakeConstraints default_constraints;
2970 CreatePeerConnection(config, &default_constraints);
2971 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
2972
2973 // Do initial offer/answer so there's something to restart.
2974 CreateOfferAsLocalDescription();
2975 CreateAnswerAsRemoteDescription(kSdpStringWithStream1);
2976
2977 // Grab the ufrags.
2978 std::vector<std::string> initial_ufrags = GetUfrags(pc_->local_description());
2979
2980 // Change ICE policy, which should trigger an ICE restart on the next offer.
2981 config.type = PeerConnectionInterface::kAll;
2982 EXPECT_TRUE(pc_->SetConfiguration(config));
2983 CreateOfferAsLocalDescription();
2984
2985 // Grab the new ufrags.
2986 std::vector<std::string> subsequent_ufrags =
2987 GetUfrags(pc_->local_description());
2988
2989 // Sanity check.
2990 EXPECT_EQ(initial_ufrags.size(), subsequent_ufrags.size());
2991 // Check that each ufrag is different.
2992 for (int i = 0; i < static_cast<int>(initial_ufrags.size()); ++i) {
2993 EXPECT_NE(initial_ufrags[i], subsequent_ufrags[i]);
2994 }
2995}
2996
2997// Test that when SetConfiguration is called and the configuration *isn't*
2998// changing, the next offer does *not* cause an ICE restart.
2999TEST_F(PeerConnectionInterfaceTest, SetConfigurationNotCausingIceRetart) {
3000 PeerConnectionInterface::RTCConfiguration config;
3001 config.type = PeerConnectionInterface::kRelay;
3002 // Need to pass default constraints to prevent disabling of DTLS...
3003 FakeConstraints default_constraints;
3004 CreatePeerConnection(config, &default_constraints);
3005 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
3006
3007 // Do initial offer/answer so there's something to restart.
3008 CreateOfferAsLocalDescription();
3009 CreateAnswerAsRemoteDescription(kSdpStringWithStream1);
3010
3011 // Grab the ufrags.
3012 std::vector<std::string> initial_ufrags = GetUfrags(pc_->local_description());
3013
3014 // Call SetConfiguration with a config identical to what the PC was
3015 // constructed with.
3016 EXPECT_TRUE(pc_->SetConfiguration(config));
3017 CreateOfferAsLocalDescription();
3018
3019 // Grab the new ufrags.
3020 std::vector<std::string> subsequent_ufrags =
3021 GetUfrags(pc_->local_description());
3022
3023 EXPECT_EQ(initial_ufrags, subsequent_ufrags);
3024}
3025
3026// Test for a weird corner case scenario:
3027// 1. Audio/video session established.
3028// 2. SetConfiguration changes ICE config; ICE restart needed.
3029// 3. ICE restart initiated by remote peer, but only for one m= section.
3030// 4. Next createOffer should initiate an ICE restart, but only for the other
3031// m= section; it would be pointless to do an ICE restart for the m= section
3032// that was already restarted.
3033TEST_F(PeerConnectionInterfaceTest, SetConfigurationCausingPartialIceRestart) {
3034 PeerConnectionInterface::RTCConfiguration config;
3035 config.type = PeerConnectionInterface::kRelay;
3036 // Need to pass default constraints to prevent disabling of DTLS...
3037 FakeConstraints default_constraints;
3038 CreatePeerConnection(config, &default_constraints);
3039 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
3040
3041 // Do initial offer/answer so there's something to restart.
3042 CreateOfferAsLocalDescription();
3043 CreateAnswerAsRemoteDescription(kSdpStringWithStream1);
3044
3045 // Change ICE policy, which should set the "needs-ice-restart" flag.
3046 config.type = PeerConnectionInterface::kAll;
3047 EXPECT_TRUE(pc_->SetConfiguration(config));
3048
3049 // Do ICE restart for the first m= section, initiated by remote peer.
3050 webrtc::JsepSessionDescription* remote_offer =
3051 new webrtc::JsepSessionDescription(SessionDescriptionInterface::kOffer);
3052 EXPECT_TRUE(remote_offer->Initialize(kSdpStringWithStream1, nullptr));
3053 remote_offer->description()->transport_infos()[0].description.ice_ufrag =
3054 "modified";
3055 EXPECT_TRUE(DoSetRemoteDescription(remote_offer));
3056 CreateAnswerAsLocalDescription();
3057
3058 // Grab the ufrags.
3059 std::vector<std::string> initial_ufrags = GetUfrags(pc_->local_description());
3060 ASSERT_EQ(2, initial_ufrags.size());
3061
3062 // Create offer and grab the new ufrags.
3063 CreateOfferAsLocalDescription();
3064 std::vector<std::string> subsequent_ufrags =
3065 GetUfrags(pc_->local_description());
3066 ASSERT_EQ(2, subsequent_ufrags.size());
3067
3068 // Ensure that only the ufrag for the second m= section changed.
3069 EXPECT_EQ(initial_ufrags[0], subsequent_ufrags[0]);
3070 EXPECT_NE(initial_ufrags[1], subsequent_ufrags[1]);
3071}
3072
deadbeeffe4a8a42016-12-20 17:56:17 -08003073// Tests that the methods to return current/pending descriptions work as
3074// expected at different points in the offer/answer exchange. This test does
3075// one offer/answer exchange as the offerer, then another as the answerer.
3076TEST_F(PeerConnectionInterfaceTest, CurrentAndPendingDescriptions) {
3077 // This disables DTLS so we can apply an answer to ourselves.
3078 CreatePeerConnection();
3079
3080 // Create initial local offer and get SDP (which will also be used as
3081 // answer/pranswer);
3082 std::unique_ptr<SessionDescriptionInterface> offer;
3083 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
3084 std::string sdp;
3085 EXPECT_TRUE(offer->ToString(&sdp));
3086
3087 // Set local offer.
3088 SessionDescriptionInterface* local_offer = offer.release();
3089 EXPECT_TRUE(DoSetLocalDescription(local_offer));
3090 EXPECT_EQ(local_offer, pc_->pending_local_description());
3091 EXPECT_EQ(nullptr, pc_->pending_remote_description());
3092 EXPECT_EQ(nullptr, pc_->current_local_description());
3093 EXPECT_EQ(nullptr, pc_->current_remote_description());
3094
3095 // Set remote pranswer.
3096 SessionDescriptionInterface* remote_pranswer =
3097 webrtc::CreateSessionDescription(SessionDescriptionInterface::kPrAnswer,
3098 sdp, nullptr);
3099 EXPECT_TRUE(DoSetRemoteDescription(remote_pranswer));
3100 EXPECT_EQ(local_offer, pc_->pending_local_description());
3101 EXPECT_EQ(remote_pranswer, pc_->pending_remote_description());
3102 EXPECT_EQ(nullptr, pc_->current_local_description());
3103 EXPECT_EQ(nullptr, pc_->current_remote_description());
3104
3105 // Set remote answer.
3106 SessionDescriptionInterface* remote_answer = webrtc::CreateSessionDescription(
3107 SessionDescriptionInterface::kAnswer, sdp, nullptr);
3108 EXPECT_TRUE(DoSetRemoteDescription(remote_answer));
3109 EXPECT_EQ(nullptr, pc_->pending_local_description());
3110 EXPECT_EQ(nullptr, pc_->pending_remote_description());
3111 EXPECT_EQ(local_offer, pc_->current_local_description());
3112 EXPECT_EQ(remote_answer, pc_->current_remote_description());
3113
3114 // Set remote offer.
3115 SessionDescriptionInterface* remote_offer = webrtc::CreateSessionDescription(
3116 SessionDescriptionInterface::kOffer, sdp, nullptr);
3117 EXPECT_TRUE(DoSetRemoteDescription(remote_offer));
3118 EXPECT_EQ(remote_offer, pc_->pending_remote_description());
3119 EXPECT_EQ(nullptr, pc_->pending_local_description());
3120 EXPECT_EQ(local_offer, pc_->current_local_description());
3121 EXPECT_EQ(remote_answer, pc_->current_remote_description());
3122
3123 // Set local pranswer.
3124 SessionDescriptionInterface* local_pranswer =
3125 webrtc::CreateSessionDescription(SessionDescriptionInterface::kPrAnswer,
3126 sdp, nullptr);
3127 EXPECT_TRUE(DoSetLocalDescription(local_pranswer));
3128 EXPECT_EQ(remote_offer, pc_->pending_remote_description());
3129 EXPECT_EQ(local_pranswer, pc_->pending_local_description());
3130 EXPECT_EQ(local_offer, pc_->current_local_description());
3131 EXPECT_EQ(remote_answer, pc_->current_remote_description());
3132
3133 // Set local answer.
3134 SessionDescriptionInterface* local_answer = webrtc::CreateSessionDescription(
3135 SessionDescriptionInterface::kAnswer, sdp, nullptr);
3136 EXPECT_TRUE(DoSetLocalDescription(local_answer));
3137 EXPECT_EQ(nullptr, pc_->pending_remote_description());
3138 EXPECT_EQ(nullptr, pc_->pending_local_description());
3139 EXPECT_EQ(remote_offer, pc_->current_remote_description());
3140 EXPECT_EQ(local_answer, pc_->current_local_description());
3141}
3142
zhihuang77985012017-02-07 15:45:16 -08003143// Tests that it won't crash when calling StartRtcEventLog or StopRtcEventLog
3144// after the PeerConnection is closed.
3145TEST_F(PeerConnectionInterfaceTest,
3146 StartAndStopLoggingAfterPeerConnectionClosed) {
3147 CreatePeerConnection();
3148 // The RtcEventLog will be reset when the PeerConnection is closed.
3149 pc_->Close();
3150
3151 rtc::PlatformFile file = 0;
3152 int64_t max_size_bytes = 1024;
3153 EXPECT_FALSE(pc_->StartRtcEventLog(file, max_size_bytes));
3154 pc_->StopRtcEventLog();
3155}
3156
deadbeef30952b42017-04-21 02:41:29 -07003157// Test that generated offers/answers include "ice-option:trickle".
3158TEST_F(PeerConnectionInterfaceTest, OffersAndAnswersHaveTrickleIceOption) {
3159 CreatePeerConnection();
3160
3161 // First, create an offer with audio/video.
3162 FakeConstraints constraints;
3163 constraints.SetMandatoryReceiveAudio(true);
3164 constraints.SetMandatoryReceiveVideo(true);
3165 std::unique_ptr<SessionDescriptionInterface> offer;
3166 ASSERT_TRUE(DoCreateOffer(&offer, &constraints));
3167 cricket::SessionDescription* desc = offer->description();
3168 ASSERT_EQ(2u, desc->transport_infos().size());
3169 EXPECT_TRUE(desc->transport_infos()[0].description.HasOption("trickle"));
3170 EXPECT_TRUE(desc->transport_infos()[1].description.HasOption("trickle"));
3171
3172 // Apply the offer as a remote description, then create an answer.
3173 EXPECT_TRUE(DoSetRemoteDescription(offer.release()));
3174 std::unique_ptr<SessionDescriptionInterface> answer;
3175 ASSERT_TRUE(DoCreateAnswer(&answer, &constraints));
3176 desc = answer->description();
3177 ASSERT_EQ(2u, desc->transport_infos().size());
3178 EXPECT_TRUE(desc->transport_infos()[0].description.HasOption("trickle"));
3179 EXPECT_TRUE(desc->transport_infos()[1].description.HasOption("trickle"));
3180}
3181
deadbeef1dcb1642017-03-29 21:08:16 -07003182// Test that ICE renomination isn't offered if it's not enabled in the PC's
3183// RTCConfiguration.
3184TEST_F(PeerConnectionInterfaceTest, IceRenominationNotOffered) {
3185 PeerConnectionInterface::RTCConfiguration config;
3186 config.enable_ice_renomination = false;
3187 CreatePeerConnection(config, nullptr);
3188 AddVoiceStream("foo");
3189
3190 std::unique_ptr<SessionDescriptionInterface> offer;
3191 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
3192 cricket::SessionDescription* desc = offer->description();
3193 EXPECT_EQ(1u, desc->transport_infos().size());
3194 EXPECT_FALSE(
3195 desc->transport_infos()[0].description.GetIceParameters().renomination);
3196}
3197
3198// Test that the ICE renomination option is present in generated offers/answers
3199// if it's enabled in the PC's RTCConfiguration.
3200TEST_F(PeerConnectionInterfaceTest, IceRenominationOptionInOfferAndAnswer) {
3201 PeerConnectionInterface::RTCConfiguration config;
3202 config.enable_ice_renomination = true;
3203 CreatePeerConnection(config, nullptr);
3204 AddVoiceStream("foo");
3205
3206 std::unique_ptr<SessionDescriptionInterface> offer;
3207 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
3208 cricket::SessionDescription* desc = offer->description();
3209 EXPECT_EQ(1u, desc->transport_infos().size());
3210 EXPECT_TRUE(
3211 desc->transport_infos()[0].description.GetIceParameters().renomination);
3212
3213 // Set the offer as a remote description, then create an answer and ensure it
3214 // has the renomination flag too.
3215 EXPECT_TRUE(DoSetRemoteDescription(offer.release()));
3216 std::unique_ptr<SessionDescriptionInterface> answer;
3217 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
3218 desc = answer->description();
3219 EXPECT_EQ(1u, desc->transport_infos().size());
3220 EXPECT_TRUE(
3221 desc->transport_infos()[0].description.GetIceParameters().renomination);
3222}
3223
3224// Test that if CreateOffer is called with the deprecated "offer to receive
3225// audio/video" constraints, they're processed and result in an offer with
3226// audio/video sections just as if RTCOfferAnswerOptions had been used.
3227TEST_F(PeerConnectionInterfaceTest, CreateOfferWithOfferToReceiveConstraints) {
3228 CreatePeerConnection();
3229
3230 FakeConstraints constraints;
3231 constraints.SetMandatoryReceiveAudio(true);
3232 constraints.SetMandatoryReceiveVideo(true);
3233 std::unique_ptr<SessionDescriptionInterface> offer;
3234 ASSERT_TRUE(DoCreateOffer(&offer, &constraints));
3235
3236 cricket::SessionDescription* desc = offer->description();
3237 const cricket::ContentInfo* audio = cricket::GetFirstAudioContent(desc);
3238 const cricket::ContentInfo* video = cricket::GetFirstVideoContent(desc);
3239 ASSERT_NE(nullptr, audio);
3240 ASSERT_NE(nullptr, video);
3241 EXPECT_FALSE(audio->rejected);
3242 EXPECT_FALSE(video->rejected);
3243}
3244
3245// Test that if CreateAnswer is called with the deprecated "offer to receive
3246// audio/video" constraints, they're processed and can be used to reject an
3247// offered m= section just as can be done with RTCOfferAnswerOptions;
3248TEST_F(PeerConnectionInterfaceTest, CreateAnswerWithOfferToReceiveConstraints) {
3249 CreatePeerConnection();
3250
3251 // First, create an offer with audio/video and apply it as a remote
3252 // description.
3253 FakeConstraints constraints;
3254 constraints.SetMandatoryReceiveAudio(true);
3255 constraints.SetMandatoryReceiveVideo(true);
3256 std::unique_ptr<SessionDescriptionInterface> offer;
3257 ASSERT_TRUE(DoCreateOffer(&offer, &constraints));
3258 EXPECT_TRUE(DoSetRemoteDescription(offer.release()));
3259
3260 // Now create answer that rejects audio/video.
3261 constraints.SetMandatoryReceiveAudio(false);
3262 constraints.SetMandatoryReceiveVideo(false);
3263 std::unique_ptr<SessionDescriptionInterface> answer;
3264 ASSERT_TRUE(DoCreateAnswer(&answer, &constraints));
3265
3266 cricket::SessionDescription* desc = answer->description();
3267 const cricket::ContentInfo* audio = cricket::GetFirstAudioContent(desc);
3268 const cricket::ContentInfo* video = cricket::GetFirstVideoContent(desc);
3269 ASSERT_NE(nullptr, audio);
3270 ASSERT_NE(nullptr, video);
3271 EXPECT_TRUE(audio->rejected);
3272 EXPECT_TRUE(video->rejected);
3273}
3274
3275#ifdef HAVE_SCTP
3276#define MAYBE_DataChannelOnlyOfferWithMaxBundlePolicy \
3277 DataChannelOnlyOfferWithMaxBundlePolicy
3278#else
3279#define MAYBE_DataChannelOnlyOfferWithMaxBundlePolicy \
3280 DISABLED_DataChannelOnlyOfferWithMaxBundlePolicy
3281#endif
3282
3283// Test that negotiation can succeed with a data channel only, and with the max
3284// bundle policy. Previously there was a bug that prevented this.
3285TEST_F(PeerConnectionInterfaceTest,
3286 MAYBE_DataChannelOnlyOfferWithMaxBundlePolicy) {
3287 PeerConnectionInterface::RTCConfiguration config;
3288 config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
3289 CreatePeerConnection(config, nullptr);
3290
3291 // First, create an offer with only a data channel and apply it as a remote
3292 // description.
3293 pc_->CreateDataChannel("test", nullptr);
3294 std::unique_ptr<SessionDescriptionInterface> offer;
3295 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
3296 EXPECT_TRUE(DoSetRemoteDescription(offer.release()));
3297
3298 // Create and set answer as well.
3299 std::unique_ptr<SessionDescriptionInterface> answer;
3300 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
3301 EXPECT_TRUE(DoSetLocalDescription(answer.release()));
3302}
3303
nisse51542be2016-02-12 02:27:06 -08003304class PeerConnectionMediaConfigTest : public testing::Test {
3305 protected:
3306 void SetUp() override {
nisseaf510af2016-03-21 08:20:42 -07003307 pcf_ = new rtc::RefCountedObject<PeerConnectionFactoryForTest>();
nisse51542be2016-02-12 02:27:06 -08003308 pcf_->Initialize();
3309 }
nisseeaabdf62017-05-05 02:23:02 -07003310 const cricket::MediaConfig TestCreatePeerConnection(
nisse51542be2016-02-12 02:27:06 -08003311 const PeerConnectionInterface::RTCConfiguration& config,
3312 const MediaConstraintsInterface *constraints) {
nisse51542be2016-02-12 02:27:06 -08003313
zhihuang9763d562016-08-05 11:14:50 -07003314 rtc::scoped_refptr<PeerConnectionInterface> pc(pcf_->CreatePeerConnection(
3315 config, constraints, nullptr, nullptr, &observer_));
nisse51542be2016-02-12 02:27:06 -08003316 EXPECT_TRUE(pc.get());
nisseeaabdf62017-05-05 02:23:02 -07003317 return pc->GetConfiguration().media_config;
nisse51542be2016-02-12 02:27:06 -08003318 }
3319
zhihuang9763d562016-08-05 11:14:50 -07003320 rtc::scoped_refptr<PeerConnectionFactoryForTest> pcf_;
nisse51542be2016-02-12 02:27:06 -08003321 MockPeerConnectionObserver observer_;
3322};
3323
3324// This test verifies the default behaviour with no constraints and a
3325// default RTCConfiguration.
3326TEST_F(PeerConnectionMediaConfigTest, TestDefaults) {
3327 PeerConnectionInterface::RTCConfiguration config;
3328 FakeConstraints constraints;
3329
3330 const cricket::MediaConfig& media_config =
3331 TestCreatePeerConnection(config, &constraints);
3332
3333 EXPECT_FALSE(media_config.enable_dscp);
nisse0db023a2016-03-01 04:29:59 -08003334 EXPECT_TRUE(media_config.video.enable_cpu_overuse_detection);
3335 EXPECT_FALSE(media_config.video.disable_prerenderer_smoothing);
3336 EXPECT_FALSE(media_config.video.suspend_below_min_bitrate);
nisse51542be2016-02-12 02:27:06 -08003337}
3338
3339// This test verifies the DSCP constraint is recognized and passed to
nisse528b7932017-05-08 03:21:43 -07003340// the PeerConnection.
nisse51542be2016-02-12 02:27:06 -08003341TEST_F(PeerConnectionMediaConfigTest, TestDscpConstraintTrue) {
3342 PeerConnectionInterface::RTCConfiguration config;
3343 FakeConstraints constraints;
3344
3345 constraints.AddOptional(webrtc::MediaConstraintsInterface::kEnableDscp, true);
3346 const cricket::MediaConfig& media_config =
3347 TestCreatePeerConnection(config, &constraints);
3348
3349 EXPECT_TRUE(media_config.enable_dscp);
3350}
3351
3352// This test verifies the cpu overuse detection constraint is
nisse528b7932017-05-08 03:21:43 -07003353// recognized and passed to the PeerConnection.
nisse51542be2016-02-12 02:27:06 -08003354TEST_F(PeerConnectionMediaConfigTest, TestCpuOveruseConstraintFalse) {
3355 PeerConnectionInterface::RTCConfiguration config;
3356 FakeConstraints constraints;
3357
3358 constraints.AddOptional(
3359 webrtc::MediaConstraintsInterface::kCpuOveruseDetection, false);
3360 const cricket::MediaConfig media_config =
3361 TestCreatePeerConnection(config, &constraints);
3362
nisse0db023a2016-03-01 04:29:59 -08003363 EXPECT_FALSE(media_config.video.enable_cpu_overuse_detection);
nisse51542be2016-02-12 02:27:06 -08003364}
3365
3366// This test verifies that the disable_prerenderer_smoothing flag is
nisse528b7932017-05-08 03:21:43 -07003367// propagated from RTCConfiguration to the PeerConnection.
nisse51542be2016-02-12 02:27:06 -08003368TEST_F(PeerConnectionMediaConfigTest, TestDisablePrerendererSmoothingTrue) {
3369 PeerConnectionInterface::RTCConfiguration config;
3370 FakeConstraints constraints;
3371
Niels Möller71bdda02016-03-31 12:59:59 +02003372 config.set_prerenderer_smoothing(false);
nisse51542be2016-02-12 02:27:06 -08003373 const cricket::MediaConfig& media_config =
3374 TestCreatePeerConnection(config, &constraints);
3375
nisse0db023a2016-03-01 04:29:59 -08003376 EXPECT_TRUE(media_config.video.disable_prerenderer_smoothing);
3377}
3378
3379// This test verifies the suspend below min bitrate constraint is
nisse528b7932017-05-08 03:21:43 -07003380// recognized and passed to the PeerConnection.
nisse0db023a2016-03-01 04:29:59 -08003381TEST_F(PeerConnectionMediaConfigTest,
3382 TestSuspendBelowMinBitrateConstraintTrue) {
3383 PeerConnectionInterface::RTCConfiguration config;
3384 FakeConstraints constraints;
3385
3386 constraints.AddOptional(
3387 webrtc::MediaConstraintsInterface::kEnableVideoSuspendBelowMinBitrate,
3388 true);
3389 const cricket::MediaConfig media_config =
3390 TestCreatePeerConnection(config, &constraints);
3391
3392 EXPECT_TRUE(media_config.video.suspend_below_min_bitrate);
nisse51542be2016-02-12 02:27:06 -08003393}
3394
deadbeefab9b2d12015-10-14 11:33:11 -07003395// The following tests verify that session options are created correctly.
deadbeefc80741f2015-10-22 13:14:45 -07003396// TODO(deadbeef): Convert these tests to be more end-to-end. Instead of
3397// "verify options are converted correctly", should be "pass options into
3398// CreateOffer and verify the correct offer is produced."
deadbeefab9b2d12015-10-14 11:33:11 -07003399
3400TEST(CreateSessionOptionsTest, GetOptionsForOfferWithInvalidAudioOption) {
3401 RTCOfferAnswerOptions rtc_options;
3402 rtc_options.offer_to_receive_audio = RTCOfferAnswerOptions::kUndefined - 1;
3403
3404 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08003405 EXPECT_FALSE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07003406
3407 rtc_options.offer_to_receive_audio =
3408 RTCOfferAnswerOptions::kMaxOfferToReceiveMedia + 1;
htaaac2dea2016-03-10 13:35:55 -08003409 EXPECT_FALSE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07003410}
3411
3412TEST(CreateSessionOptionsTest, GetOptionsForOfferWithInvalidVideoOption) {
3413 RTCOfferAnswerOptions rtc_options;
3414 rtc_options.offer_to_receive_video = RTCOfferAnswerOptions::kUndefined - 1;
3415
3416 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08003417 EXPECT_FALSE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07003418
3419 rtc_options.offer_to_receive_video =
3420 RTCOfferAnswerOptions::kMaxOfferToReceiveMedia + 1;
htaaac2dea2016-03-10 13:35:55 -08003421 EXPECT_FALSE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07003422}
3423
3424// Test that a MediaSessionOptions is created for an offer if
deadbeefc80741f2015-10-22 13:14:45 -07003425// OfferToReceiveAudio and OfferToReceiveVideo options are set.
deadbeefab9b2d12015-10-14 11:33:11 -07003426TEST(CreateSessionOptionsTest, GetMediaSessionOptionsForOfferWithAudioVideo) {
3427 RTCOfferAnswerOptions rtc_options;
3428 rtc_options.offer_to_receive_audio = 1;
3429 rtc_options.offer_to_receive_video = 1;
3430
3431 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08003432 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07003433 EXPECT_TRUE(options.has_audio());
3434 EXPECT_TRUE(options.has_video());
3435 EXPECT_TRUE(options.bundle_enabled);
3436}
3437
3438// Test that a correct MediaSessionOptions is created for an offer if
deadbeefc80741f2015-10-22 13:14:45 -07003439// OfferToReceiveAudio is set.
deadbeefab9b2d12015-10-14 11:33:11 -07003440TEST(CreateSessionOptionsTest, GetMediaSessionOptionsForOfferWithAudio) {
3441 RTCOfferAnswerOptions rtc_options;
3442 rtc_options.offer_to_receive_audio = 1;
3443
3444 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08003445 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07003446 EXPECT_TRUE(options.has_audio());
3447 EXPECT_FALSE(options.has_video());
3448 EXPECT_TRUE(options.bundle_enabled);
3449}
3450
3451// Test that a correct MediaSessionOptions is created for an offer if
deadbeefc80741f2015-10-22 13:14:45 -07003452// the default OfferOptions are used.
deadbeefab9b2d12015-10-14 11:33:11 -07003453TEST(CreateSessionOptionsTest, GetDefaultMediaSessionOptionsForOffer) {
3454 RTCOfferAnswerOptions rtc_options;
3455
3456 cricket::MediaSessionOptions options;
deadbeef0ed85b22016-02-23 17:24:52 -08003457 options.transport_options["audio"] = cricket::TransportOptions();
3458 options.transport_options["video"] = cricket::TransportOptions();
htaaac2dea2016-03-10 13:35:55 -08003459 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefc80741f2015-10-22 13:14:45 -07003460 EXPECT_TRUE(options.has_audio());
deadbeefab9b2d12015-10-14 11:33:11 -07003461 EXPECT_FALSE(options.has_video());
deadbeefc80741f2015-10-22 13:14:45 -07003462 EXPECT_TRUE(options.bundle_enabled);
deadbeefab9b2d12015-10-14 11:33:11 -07003463 EXPECT_TRUE(options.vad_enabled);
deadbeef0ed85b22016-02-23 17:24:52 -08003464 EXPECT_FALSE(options.transport_options["audio"].ice_restart);
3465 EXPECT_FALSE(options.transport_options["video"].ice_restart);
deadbeefab9b2d12015-10-14 11:33:11 -07003466}
3467
3468// Test that a correct MediaSessionOptions is created for an offer if
deadbeefc80741f2015-10-22 13:14:45 -07003469// OfferToReceiveVideo is set.
deadbeefab9b2d12015-10-14 11:33:11 -07003470TEST(CreateSessionOptionsTest, GetMediaSessionOptionsForOfferWithVideo) {
3471 RTCOfferAnswerOptions rtc_options;
3472 rtc_options.offer_to_receive_audio = 0;
3473 rtc_options.offer_to_receive_video = 1;
3474
3475 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08003476 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07003477 EXPECT_FALSE(options.has_audio());
3478 EXPECT_TRUE(options.has_video());
3479 EXPECT_TRUE(options.bundle_enabled);
3480}
3481
3482// Test that a correct MediaSessionOptions is created for an offer if
3483// UseRtpMux is set to false.
3484TEST(CreateSessionOptionsTest,
3485 GetMediaSessionOptionsForOfferWithBundleDisabled) {
3486 RTCOfferAnswerOptions rtc_options;
3487 rtc_options.offer_to_receive_audio = 1;
3488 rtc_options.offer_to_receive_video = 1;
3489 rtc_options.use_rtp_mux = false;
3490
3491 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08003492 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07003493 EXPECT_TRUE(options.has_audio());
3494 EXPECT_TRUE(options.has_video());
3495 EXPECT_FALSE(options.bundle_enabled);
3496}
3497
3498// Test that a correct MediaSessionOptions is created to restart ice if
3499// IceRestart is set. It also tests that subsequent MediaSessionOptions don't
Taylor Brandstetterf475d362016-01-08 15:35:57 -08003500// have |audio_transport_options.ice_restart| etc. set.
deadbeefab9b2d12015-10-14 11:33:11 -07003501TEST(CreateSessionOptionsTest, GetMediaSessionOptionsForOfferWithIceRestart) {
3502 RTCOfferAnswerOptions rtc_options;
3503 rtc_options.ice_restart = true;
3504
3505 cricket::MediaSessionOptions options;
deadbeef0ed85b22016-02-23 17:24:52 -08003506 options.transport_options["audio"] = cricket::TransportOptions();
3507 options.transport_options["video"] = cricket::TransportOptions();
htaaac2dea2016-03-10 13:35:55 -08003508 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeef0ed85b22016-02-23 17:24:52 -08003509 EXPECT_TRUE(options.transport_options["audio"].ice_restart);
3510 EXPECT_TRUE(options.transport_options["video"].ice_restart);
deadbeefab9b2d12015-10-14 11:33:11 -07003511
3512 rtc_options = RTCOfferAnswerOptions();
htaaac2dea2016-03-10 13:35:55 -08003513 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeef0ed85b22016-02-23 17:24:52 -08003514 EXPECT_FALSE(options.transport_options["audio"].ice_restart);
3515 EXPECT_FALSE(options.transport_options["video"].ice_restart);
deadbeefab9b2d12015-10-14 11:33:11 -07003516}
3517
3518// Test that the MediaConstraints in an answer don't affect if audio and video
3519// is offered in an offer but that if kOfferToReceiveAudio or
3520// kOfferToReceiveVideo constraints are true in an offer, the media type will be
3521// included in subsequent answers.
3522TEST(CreateSessionOptionsTest, MediaConstraintsInAnswer) {
3523 FakeConstraints answer_c;
3524 answer_c.SetMandatoryReceiveAudio(true);
3525 answer_c.SetMandatoryReceiveVideo(true);
3526
3527 cricket::MediaSessionOptions answer_options;
3528 EXPECT_TRUE(ParseConstraintsForAnswer(&answer_c, &answer_options));
3529 EXPECT_TRUE(answer_options.has_audio());
3530 EXPECT_TRUE(answer_options.has_video());
3531
deadbeefc80741f2015-10-22 13:14:45 -07003532 RTCOfferAnswerOptions rtc_offer_options;
deadbeefab9b2d12015-10-14 11:33:11 -07003533
3534 cricket::MediaSessionOptions offer_options;
htaaac2dea2016-03-10 13:35:55 -08003535 EXPECT_TRUE(
3536 ExtractMediaSessionOptions(rtc_offer_options, false, &offer_options));
deadbeefc80741f2015-10-22 13:14:45 -07003537 EXPECT_TRUE(offer_options.has_audio());
htaaac2dea2016-03-10 13:35:55 -08003538 EXPECT_TRUE(offer_options.has_video());
deadbeefab9b2d12015-10-14 11:33:11 -07003539
deadbeefc80741f2015-10-22 13:14:45 -07003540 RTCOfferAnswerOptions updated_rtc_offer_options;
3541 updated_rtc_offer_options.offer_to_receive_audio = 1;
3542 updated_rtc_offer_options.offer_to_receive_video = 1;
deadbeefab9b2d12015-10-14 11:33:11 -07003543
3544 cricket::MediaSessionOptions updated_offer_options;
htaaac2dea2016-03-10 13:35:55 -08003545 EXPECT_TRUE(ExtractMediaSessionOptions(updated_rtc_offer_options, false,
htaa2a49d92016-03-04 02:51:39 -08003546 &updated_offer_options));
deadbeefab9b2d12015-10-14 11:33:11 -07003547 EXPECT_TRUE(updated_offer_options.has_audio());
3548 EXPECT_TRUE(updated_offer_options.has_video());
3549
3550 // Since an offer has been created with both audio and video, subsequent
3551 // offers and answers should contain both audio and video.
3552 // Answers will only contain the media types that exist in the offer
3553 // regardless of the value of |updated_answer_options.has_audio| and
3554 // |updated_answer_options.has_video|.
3555 FakeConstraints updated_answer_c;
3556 answer_c.SetMandatoryReceiveAudio(false);
3557 answer_c.SetMandatoryReceiveVideo(false);
3558
3559 cricket::MediaSessionOptions updated_answer_options;
3560 EXPECT_TRUE(
3561 ParseConstraintsForAnswer(&updated_answer_c, &updated_answer_options));
3562 EXPECT_TRUE(updated_answer_options.has_audio());
3563 EXPECT_TRUE(updated_answer_options.has_video());
deadbeefab9b2d12015-10-14 11:33:11 -07003564}
deadbeef3edec7c2016-12-10 11:44:26 -08003565
deadbeef293e9262017-01-11 12:28:30 -08003566// Tests a few random fields being different.
3567TEST(RTCConfigurationTest, ComparisonOperators) {
3568 PeerConnectionInterface::RTCConfiguration a;
3569 PeerConnectionInterface::RTCConfiguration b;
3570 EXPECT_EQ(a, b);
3571
3572 PeerConnectionInterface::RTCConfiguration c;
3573 c.servers.push_back(PeerConnectionInterface::IceServer());
3574 EXPECT_NE(a, c);
3575
3576 PeerConnectionInterface::RTCConfiguration d;
3577 d.type = PeerConnectionInterface::kRelay;
3578 EXPECT_NE(a, d);
3579
3580 PeerConnectionInterface::RTCConfiguration e;
3581 e.audio_jitter_buffer_max_packets = 5;
3582 EXPECT_NE(a, e);
3583
3584 PeerConnectionInterface::RTCConfiguration f;
3585 f.ice_connection_receiving_timeout = 1337;
3586 EXPECT_NE(a, f);
3587
3588 PeerConnectionInterface::RTCConfiguration g;
3589 g.disable_ipv6 = true;
3590 EXPECT_NE(a, g);
3591
3592 PeerConnectionInterface::RTCConfiguration h(
3593 PeerConnectionInterface::RTCConfigurationType::kAggressive);
3594 EXPECT_NE(a, h);
3595}