blob: ae2c873a3f74a66b3ae197d7856a6a12171fc6a8 [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"
kjellandera96e2d72016-02-04 23:52:28 -080029#include "webrtc/media/base/fakevideocapturer.h"
deadbeef953c2ce2017-01-09 14:53:41 -080030#include "webrtc/media/sctp/sctptransportinternal.h"
Taylor Brandstettera1c30352016-05-13 08:15:11 -070031#include "webrtc/p2p/base/fakeportallocator.h"
ossu7bb87ee2017-01-23 04:56:25 -080032#include "webrtc/pc/audiotrack.h"
kjellander@webrtc.org9b8df252016-02-12 06:47:59 +010033#include "webrtc/pc/mediasession.h"
ossu7bb87ee2017-01-23 04:56:25 -080034#include "webrtc/pc/mediastream.h"
35#include "webrtc/pc/peerconnection.h"
36#include "webrtc/pc/streamcollection.h"
37#include "webrtc/pc/test/fakertccertificategenerator.h"
38#include "webrtc/pc/test/fakevideotracksource.h"
39#include "webrtc/pc/test/mockpeerconnectionobservers.h"
40#include "webrtc/pc/test/testsdpstrings.h"
41#include "webrtc/pc/videocapturertracksource.h"
42#include "webrtc/pc/videotrack.h"
kwibergac9f8762016-09-30 22:29:43 -070043#include "webrtc/test/gmock.h"
44
45#ifdef WEBRTC_ANDROID
ossu7bb87ee2017-01-23 04:56:25 -080046#include "webrtc/pc/test/androidtestinitializer.h"
kwibergac9f8762016-09-30 22:29:43 -070047#endif
henrike@webrtc.org28e20752013-07-10 00:45:36 +000048
49static const char kStreamLabel1[] = "local_stream_1";
50static const char kStreamLabel2[] = "local_stream_2";
51static const char kStreamLabel3[] = "local_stream_3";
52static const int kDefaultStunPort = 3478;
53static const char kStunAddressOnly[] = "stun:address";
54static const char kStunInvalidPort[] = "stun:address:-1";
55static const char kStunAddressPortAndMore1[] = "stun:address:port:more";
56static const char kStunAddressPortAndMore2[] = "stun:address:port more";
57static const char kTurnIceServerUri[] = "turn:user@turn.example.org";
58static const char kTurnUsername[] = "user";
59static const char kTurnPassword[] = "password";
60static const char kTurnHostname[] = "turn.example.org";
Peter Boström0c4e06b2015-10-07 12:23:21 +020061static const uint32_t kTimeout = 10000U;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000062
deadbeefab9b2d12015-10-14 11:33:11 -070063static const char kStreams[][8] = {"stream1", "stream2"};
64static const char kAudioTracks[][32] = {"audiotrack0", "audiotrack1"};
65static const char kVideoTracks[][32] = {"videotrack0", "videotrack1"};
66
deadbeef5e97fb52015-10-15 12:49:08 -070067static const char kRecvonly[] = "recvonly";
68static const char kSendrecv[] = "sendrecv";
69
deadbeefab9b2d12015-10-14 11:33:11 -070070// Reference SDP with a MediaStream with label "stream1" and audio track with
71// id "audio_1" and a video track with id "video_1;
72static const char kSdpStringWithStream1[] =
73 "v=0\r\n"
74 "o=- 0 0 IN IP4 127.0.0.1\r\n"
75 "s=-\r\n"
76 "t=0 0\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -080077 "m=audio 1 RTP/AVPF 103\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -070078 "a=ice-ufrag:e5785931\r\n"
79 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
80 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
81 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -070082 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -070083 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -080084 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -070085 "a=rtpmap:103 ISAC/16000\r\n"
86 "a=ssrc:1 cname:stream1\r\n"
87 "a=ssrc:1 mslabel:stream1\r\n"
88 "a=ssrc:1 label:audiotrack0\r\n"
89 "m=video 1 RTP/AVPF 120\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -080090 "a=ice-ufrag:e5785931\r\n"
91 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
92 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
93 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -070094 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -070095 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -080096 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -070097 "a=rtpmap:120 VP8/90000\r\n"
98 "a=ssrc:2 cname:stream1\r\n"
99 "a=ssrc:2 mslabel:stream1\r\n"
100 "a=ssrc:2 label:videotrack0\r\n";
101
zhihuang81c3a032016-11-17 12:06:24 -0800102// Reference SDP with a MediaStream with label "stream1" and audio track with
103// id "audio_1";
104static const char kSdpStringWithStream1AudioTrackOnly[] =
105 "v=0\r\n"
106 "o=- 0 0 IN IP4 127.0.0.1\r\n"
107 "s=-\r\n"
108 "t=0 0\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800109 "m=audio 1 RTP/AVPF 103\r\n"
zhihuang81c3a032016-11-17 12:06:24 -0800110 "a=ice-ufrag:e5785931\r\n"
111 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
112 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
113 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
zhihuang81c3a032016-11-17 12:06:24 -0800114 "a=mid:audio\r\n"
115 "a=sendrecv\r\n"
116 "a=rtpmap:103 ISAC/16000\r\n"
117 "a=ssrc:1 cname:stream1\r\n"
118 "a=ssrc:1 mslabel:stream1\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800119 "a=ssrc:1 label:audiotrack0\r\n"
120 "a=rtcp-mux\r\n";
zhihuang81c3a032016-11-17 12:06:24 -0800121
deadbeefab9b2d12015-10-14 11:33:11 -0700122// Reference SDP with two MediaStreams with label "stream1" and "stream2. Each
123// MediaStreams have one audio track and one video track.
124// This uses MSID.
125static const char kSdpStringWithStream1And2[] =
126 "v=0\r\n"
127 "o=- 0 0 IN IP4 127.0.0.1\r\n"
128 "s=-\r\n"
129 "t=0 0\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800130 "a=msid-semantic: WMS stream1 stream2\r\n"
131 "m=audio 1 RTP/AVPF 103\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700132 "a=ice-ufrag:e5785931\r\n"
133 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
134 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
135 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700136 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700137 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800138 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700139 "a=rtpmap:103 ISAC/16000\r\n"
140 "a=ssrc:1 cname:stream1\r\n"
141 "a=ssrc:1 msid:stream1 audiotrack0\r\n"
142 "a=ssrc:3 cname:stream2\r\n"
143 "a=ssrc:3 msid:stream2 audiotrack1\r\n"
144 "m=video 1 RTP/AVPF 120\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800145 "a=ice-ufrag:e5785931\r\n"
146 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
147 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
148 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700149 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700150 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800151 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700152 "a=rtpmap:120 VP8/0\r\n"
153 "a=ssrc:2 cname:stream1\r\n"
154 "a=ssrc:2 msid:stream1 videotrack0\r\n"
155 "a=ssrc:4 cname:stream2\r\n"
156 "a=ssrc:4 msid:stream2 videotrack1\r\n";
157
158// Reference SDP without MediaStreams. Msid is not supported.
159static const char kSdpStringWithoutStreams[] =
160 "v=0\r\n"
161 "o=- 0 0 IN IP4 127.0.0.1\r\n"
162 "s=-\r\n"
163 "t=0 0\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800164 "m=audio 1 RTP/AVPF 103\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700165 "a=ice-ufrag:e5785931\r\n"
166 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
167 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
168 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700169 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700170 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800171 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700172 "a=rtpmap:103 ISAC/16000\r\n"
173 "m=video 1 RTP/AVPF 120\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800174 "a=ice-ufrag:e5785931\r\n"
175 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
176 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
177 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700178 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700179 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800180 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700181 "a=rtpmap:120 VP8/90000\r\n";
182
183// Reference SDP without MediaStreams. Msid is supported.
184static const char kSdpStringWithMsidWithoutStreams[] =
185 "v=0\r\n"
186 "o=- 0 0 IN IP4 127.0.0.1\r\n"
187 "s=-\r\n"
188 "t=0 0\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800189 "a=msid-semantic: WMS\r\n"
190 "m=audio 1 RTP/AVPF 103\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700191 "a=ice-ufrag:e5785931\r\n"
192 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
193 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
194 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700195 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700196 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800197 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700198 "a=rtpmap:103 ISAC/16000\r\n"
199 "m=video 1 RTP/AVPF 120\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800200 "a=ice-ufrag:e5785931\r\n"
201 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
202 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
203 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700204 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700205 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800206 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700207 "a=rtpmap:120 VP8/90000\r\n";
208
209// Reference SDP without MediaStreams and audio only.
210static const char kSdpStringWithoutStreamsAudioOnly[] =
211 "v=0\r\n"
212 "o=- 0 0 IN IP4 127.0.0.1\r\n"
213 "s=-\r\n"
214 "t=0 0\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800215 "m=audio 1 RTP/AVPF 103\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700216 "a=ice-ufrag:e5785931\r\n"
217 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
218 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
219 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700220 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700221 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800222 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700223 "a=rtpmap:103 ISAC/16000\r\n";
224
225// Reference SENDONLY SDP without MediaStreams. Msid is not supported.
226static const char kSdpStringSendOnlyWithoutStreams[] =
227 "v=0\r\n"
228 "o=- 0 0 IN IP4 127.0.0.1\r\n"
229 "s=-\r\n"
230 "t=0 0\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800231 "m=audio 1 RTP/AVPF 103\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700232 "a=ice-ufrag:e5785931\r\n"
233 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
234 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
235 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700236 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700237 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700238 "a=sendonly\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800239 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700240 "a=rtpmap:103 ISAC/16000\r\n"
241 "m=video 1 RTP/AVPF 120\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800242 "a=ice-ufrag:e5785931\r\n"
243 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
244 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
245 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700246 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700247 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700248 "a=sendonly\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800249 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700250 "a=rtpmap:120 VP8/90000\r\n";
251
252static const char kSdpStringInit[] =
253 "v=0\r\n"
254 "o=- 0 0 IN IP4 127.0.0.1\r\n"
255 "s=-\r\n"
256 "t=0 0\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700257 "a=msid-semantic: WMS\r\n";
258
259static const char kSdpStringAudio[] =
260 "m=audio 1 RTP/AVPF 103\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800261 "a=ice-ufrag:e5785931\r\n"
262 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
263 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
264 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700265 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700266 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800267 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700268 "a=rtpmap:103 ISAC/16000\r\n";
269
270static const char kSdpStringVideo[] =
271 "m=video 1 RTP/AVPF 120\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800272 "a=ice-ufrag:e5785931\r\n"
273 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
274 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
275 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700276 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700277 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800278 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700279 "a=rtpmap:120 VP8/90000\r\n";
280
281static const char kSdpStringMs1Audio0[] =
282 "a=ssrc:1 cname:stream1\r\n"
283 "a=ssrc:1 msid:stream1 audiotrack0\r\n";
284
285static const char kSdpStringMs1Video0[] =
286 "a=ssrc:2 cname:stream1\r\n"
287 "a=ssrc:2 msid:stream1 videotrack0\r\n";
288
289static const char kSdpStringMs1Audio1[] =
290 "a=ssrc:3 cname:stream1\r\n"
291 "a=ssrc:3 msid:stream1 audiotrack1\r\n";
292
293static const char kSdpStringMs1Video1[] =
294 "a=ssrc:4 cname:stream1\r\n"
295 "a=ssrc:4 msid:stream1 videotrack1\r\n";
296
deadbeef8662f942017-01-20 21:20:51 -0800297static const char kDtlsSdesFallbackSdp[] =
298 "v=0\r\n"
299 "o=xxxxxx 7 2 IN IP4 0.0.0.0\r\n"
300 "s=-\r\n"
301 "c=IN IP4 0.0.0.0\r\n"
302 "t=0 0\r\n"
303 "a=group:BUNDLE audio\r\n"
304 "a=msid-semantic: WMS\r\n"
305 "m=audio 1 RTP/SAVPF 0\r\n"
306 "a=sendrecv\r\n"
307 "a=rtcp-mux\r\n"
308 "a=mid:audio\r\n"
309 "a=ssrc:1 cname:stream1\r\n"
310 "a=ssrc:1 mslabel:stream1\r\n"
311 "a=ssrc:1 label:audiotrack0\r\n"
312 "a=ice-ufrag:e5785931\r\n"
313 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
314 "a=rtpmap:0 pcmu/8000\r\n"
315 "a=fingerprint:sha-1 "
316 "4A:AD:B9:B1:3F:82:18:3B:54:02:12:DF:3E:5D:49:6B:19:E5:7C:AB\r\n"
317 "a=setup:actpass\r\n"
318 "a=crypto:1 AES_CM_128_HMAC_SHA1_32 "
319 "inline:NzB4d1BINUAvLEw6UzF3WSJ+PSdFcGdUJShpX1Zj|2^20|1:32 "
320 "dummy_session_params\r\n";
321
perkjd61bf802016-03-24 03:16:19 -0700322using ::testing::Exactly;
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700323using cricket::StreamParams;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000324using webrtc::AudioSourceInterface;
deadbeefab9b2d12015-10-14 11:33:11 -0700325using webrtc::AudioTrack;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000326using webrtc::AudioTrackInterface;
327using webrtc::DataBuffer;
328using webrtc::DataChannelInterface;
329using webrtc::FakeConstraints;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000330using webrtc::IceCandidateInterface;
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700331using webrtc::JsepSessionDescription;
deadbeefc80741f2015-10-22 13:14:45 -0700332using webrtc::MediaConstraintsInterface;
deadbeefab9b2d12015-10-14 11:33:11 -0700333using webrtc::MediaStream;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000334using webrtc::MediaStreamInterface;
335using webrtc::MediaStreamTrackInterface;
336using webrtc::MockCreateSessionDescriptionObserver;
337using webrtc::MockDataChannelObserver;
338using webrtc::MockSetSessionDescriptionObserver;
339using webrtc::MockStatsObserver;
perkjd61bf802016-03-24 03:16:19 -0700340using webrtc::NotifierInterface;
341using webrtc::ObserverInterface;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000342using webrtc::PeerConnectionInterface;
343using webrtc::PeerConnectionObserver;
deadbeef293e9262017-01-11 12:28:30 -0800344using webrtc::RTCError;
345using webrtc::RTCErrorType;
deadbeefab9b2d12015-10-14 11:33:11 -0700346using webrtc::RtpReceiverInterface;
347using webrtc::RtpSenderInterface;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000348using webrtc::SdpParseError;
349using webrtc::SessionDescriptionInterface;
deadbeefab9b2d12015-10-14 11:33:11 -0700350using webrtc::StreamCollection;
351using webrtc::StreamCollectionInterface;
perkja3ede6c2016-03-08 01:27:48 +0100352using webrtc::VideoTrackSourceInterface;
deadbeefab9b2d12015-10-14 11:33:11 -0700353using webrtc::VideoTrack;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000354using webrtc::VideoTrackInterface;
355
deadbeefab9b2d12015-10-14 11:33:11 -0700356typedef PeerConnectionInterface::RTCOfferAnswerOptions RTCOfferAnswerOptions;
357
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000358namespace {
359
360// Gets the first ssrc of given content type from the ContentInfo.
361bool GetFirstSsrc(const cricket::ContentInfo* content_info, int* ssrc) {
362 if (!content_info || !ssrc) {
363 return false;
364 }
365 const cricket::MediaContentDescription* media_desc =
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000366 static_cast<const cricket::MediaContentDescription*>(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000367 content_info->description);
368 if (!media_desc || media_desc->streams().empty()) {
369 return false;
370 }
371 *ssrc = media_desc->streams().begin()->first_ssrc();
372 return true;
373}
374
deadbeefd1a38b52016-12-10 13:15:33 -0800375// Get the ufrags out of an SDP blob. Useful for testing ICE restart
376// behavior.
377std::vector<std::string> GetUfrags(
378 const webrtc::SessionDescriptionInterface* desc) {
379 std::vector<std::string> ufrags;
380 for (const cricket::TransportInfo& info :
381 desc->description()->transport_infos()) {
382 ufrags.push_back(info.description.ice_ufrag);
383 }
384 return ufrags;
385}
386
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000387void SetSsrcToZero(std::string* sdp) {
388 const char kSdpSsrcAtribute[] = "a=ssrc:";
389 const char kSdpSsrcAtributeZero[] = "a=ssrc:0";
390 size_t ssrc_pos = 0;
391 while ((ssrc_pos = sdp->find(kSdpSsrcAtribute, ssrc_pos)) !=
392 std::string::npos) {
393 size_t end_ssrc = sdp->find(" ", ssrc_pos);
394 sdp->replace(ssrc_pos, end_ssrc - ssrc_pos, kSdpSsrcAtributeZero);
395 ssrc_pos = end_ssrc;
396 }
397}
398
deadbeefab9b2d12015-10-14 11:33:11 -0700399// Check if |streams| contains the specified track.
400bool ContainsTrack(const std::vector<cricket::StreamParams>& streams,
401 const std::string& stream_label,
402 const std::string& track_id) {
403 for (const cricket::StreamParams& params : streams) {
404 if (params.sync_label == stream_label && params.id == track_id) {
405 return true;
406 }
407 }
408 return false;
409}
410
411// Check if |senders| contains the specified sender, by id.
412bool ContainsSender(
413 const std::vector<rtc::scoped_refptr<RtpSenderInterface>>& senders,
414 const std::string& id) {
415 for (const auto& sender : senders) {
416 if (sender->id() == id) {
417 return true;
418 }
419 }
420 return false;
421}
422
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700423// Check if |senders| contains the specified sender, by id and stream id.
424bool ContainsSender(
425 const std::vector<rtc::scoped_refptr<RtpSenderInterface>>& senders,
426 const std::string& id,
427 const std::string& stream_id) {
428 for (const auto& sender : senders) {
deadbeefa601f5c2016-06-06 14:27:39 -0700429 if (sender->id() == id && sender->stream_ids()[0] == stream_id) {
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700430 return true;
431 }
432 }
433 return false;
434}
435
deadbeefab9b2d12015-10-14 11:33:11 -0700436// Create a collection of streams.
437// CreateStreamCollection(1) creates a collection that
438// correspond to kSdpStringWithStream1.
439// CreateStreamCollection(2) correspond to kSdpStringWithStream1And2.
440rtc::scoped_refptr<StreamCollection> CreateStreamCollection(
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700441 int number_of_streams,
442 int tracks_per_stream) {
deadbeefab9b2d12015-10-14 11:33:11 -0700443 rtc::scoped_refptr<StreamCollection> local_collection(
444 StreamCollection::Create());
445
446 for (int i = 0; i < number_of_streams; ++i) {
447 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream(
448 webrtc::MediaStream::Create(kStreams[i]));
449
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700450 for (int j = 0; j < tracks_per_stream; ++j) {
451 // Add a local audio track.
452 rtc::scoped_refptr<webrtc::AudioTrackInterface> audio_track(
453 webrtc::AudioTrack::Create(kAudioTracks[i * tracks_per_stream + j],
454 nullptr));
455 stream->AddTrack(audio_track);
deadbeefab9b2d12015-10-14 11:33:11 -0700456
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700457 // Add a local video track.
458 rtc::scoped_refptr<webrtc::VideoTrackInterface> video_track(
459 webrtc::VideoTrack::Create(kVideoTracks[i * tracks_per_stream + j],
460 webrtc::FakeVideoTrackSource::Create()));
461 stream->AddTrack(video_track);
462 }
deadbeefab9b2d12015-10-14 11:33:11 -0700463
464 local_collection->AddStream(stream);
465 }
466 return local_collection;
467}
468
469// Check equality of StreamCollections.
470bool CompareStreamCollections(StreamCollectionInterface* s1,
471 StreamCollectionInterface* s2) {
472 if (s1 == nullptr || s2 == nullptr || s1->count() != s2->count()) {
473 return false;
474 }
475
476 for (size_t i = 0; i != s1->count(); ++i) {
477 if (s1->at(i)->label() != s2->at(i)->label()) {
478 return false;
479 }
480 webrtc::AudioTrackVector audio_tracks1 = s1->at(i)->GetAudioTracks();
481 webrtc::AudioTrackVector audio_tracks2 = s2->at(i)->GetAudioTracks();
482 webrtc::VideoTrackVector video_tracks1 = s1->at(i)->GetVideoTracks();
483 webrtc::VideoTrackVector video_tracks2 = s2->at(i)->GetVideoTracks();
484
485 if (audio_tracks1.size() != audio_tracks2.size()) {
486 return false;
487 }
488 for (size_t j = 0; j != audio_tracks1.size(); ++j) {
489 if (audio_tracks1[j]->id() != audio_tracks2[j]->id()) {
490 return false;
491 }
492 }
493 if (video_tracks1.size() != video_tracks2.size()) {
494 return false;
495 }
496 for (size_t j = 0; j != video_tracks1.size(); ++j) {
497 if (video_tracks1[j]->id() != video_tracks2[j]->id()) {
498 return false;
499 }
500 }
501 }
502 return true;
503}
504
perkjd61bf802016-03-24 03:16:19 -0700505// Helper class to test Observer.
506class MockTrackObserver : public ObserverInterface {
507 public:
508 explicit MockTrackObserver(NotifierInterface* notifier)
509 : notifier_(notifier) {
510 notifier_->RegisterObserver(this);
511 }
512
513 ~MockTrackObserver() { Unregister(); }
514
515 void Unregister() {
516 if (notifier_) {
517 notifier_->UnregisterObserver(this);
518 notifier_ = nullptr;
519 }
520 }
521
522 MOCK_METHOD0(OnChanged, void());
523
524 private:
525 NotifierInterface* notifier_;
526};
527
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000528class MockPeerConnectionObserver : public PeerConnectionObserver {
529 public:
deadbeefab9b2d12015-10-14 11:33:11 -0700530 MockPeerConnectionObserver() : remote_streams_(StreamCollection::Create()) {}
Henrik Kjellander3fe372d2016-05-12 08:10:52 +0200531 virtual ~MockPeerConnectionObserver() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000532 }
533 void SetPeerConnectionInterface(PeerConnectionInterface* pc) {
534 pc_ = pc;
535 if (pc) {
536 state_ = pc_->signaling_state();
537 }
538 }
nisseef8b61e2016-04-29 06:09:15 -0700539 void OnSignalingChange(
540 PeerConnectionInterface::SignalingState new_state) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000541 EXPECT_EQ(pc_->signaling_state(), new_state);
542 state_ = new_state;
543 }
deadbeefab9b2d12015-10-14 11:33:11 -0700544
545 MediaStreamInterface* RemoteStream(const std::string& label) {
546 return remote_streams_->find(label);
547 }
548 StreamCollectionInterface* remote_streams() const { return remote_streams_; }
Taylor Brandstetter98cde262016-05-31 13:02:21 -0700549 void OnAddStream(rtc::scoped_refptr<MediaStreamInterface> stream) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000550 last_added_stream_ = stream;
deadbeefab9b2d12015-10-14 11:33:11 -0700551 remote_streams_->AddStream(stream);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000552 }
Taylor Brandstetter98cde262016-05-31 13:02:21 -0700553 void OnRemoveStream(
554 rtc::scoped_refptr<MediaStreamInterface> stream) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000555 last_removed_stream_ = stream;
deadbeefab9b2d12015-10-14 11:33:11 -0700556 remote_streams_->RemoveStream(stream);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000557 }
perkjdfb769d2016-02-09 03:09:43 -0800558 void OnRenegotiationNeeded() override { renegotiation_needed_ = true; }
Taylor Brandstetter98cde262016-05-31 13:02:21 -0700559 void OnDataChannel(
560 rtc::scoped_refptr<DataChannelInterface> data_channel) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000561 last_datachannel_ = data_channel;
562 }
563
perkjdfb769d2016-02-09 03:09:43 -0800564 void OnIceConnectionChange(
565 PeerConnectionInterface::IceConnectionState new_state) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000566 EXPECT_EQ(pc_->ice_connection_state(), new_state);
zhihuang81c3a032016-11-17 12:06:24 -0800567 callback_triggered_ = true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000568 }
perkjdfb769d2016-02-09 03:09:43 -0800569 void OnIceGatheringChange(
570 PeerConnectionInterface::IceGatheringState new_state) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000571 EXPECT_EQ(pc_->ice_gathering_state(), new_state);
perkjdfb769d2016-02-09 03:09:43 -0800572 ice_complete_ = new_state == PeerConnectionInterface::kIceGatheringComplete;
zhihuang81c3a032016-11-17 12:06:24 -0800573 callback_triggered_ = true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000574 }
perkjdfb769d2016-02-09 03:09:43 -0800575 void OnIceCandidate(const webrtc::IceCandidateInterface* candidate) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000576 EXPECT_NE(PeerConnectionInterface::kIceGatheringNew,
577 pc_->ice_gathering_state());
578
579 std::string sdp;
580 EXPECT_TRUE(candidate->ToString(&sdp));
581 EXPECT_LT(0u, sdp.size());
582 last_candidate_.reset(webrtc::CreateIceCandidate(candidate->sdp_mid(),
583 candidate->sdp_mline_index(), sdp, NULL));
584 EXPECT_TRUE(last_candidate_.get() != NULL);
zhihuang81c3a032016-11-17 12:06:24 -0800585 callback_triggered_ = true;
zhihuang29ff8442016-07-27 11:07:25 -0700586 }
587
588 void OnIceCandidatesRemoved(
589 const std::vector<cricket::Candidate>& candidates) override {
zhihuang81c3a032016-11-17 12:06:24 -0800590 callback_triggered_ = true;
zhihuang29ff8442016-07-27 11:07:25 -0700591 }
592
593 void OnIceConnectionReceivingChange(bool receiving) override {
zhihuang81c3a032016-11-17 12:06:24 -0800594 callback_triggered_ = true;
595 }
596
zhihuangc63b8942016-12-02 15:41:10 -0800597 void OnAddTrack(
598 rtc::scoped_refptr<webrtc::RtpReceiverInterface> receiver,
599 const std::vector<rtc::scoped_refptr<webrtc::MediaStreamInterface>>&
600 streams) override {
zhihuang81c3a032016-11-17 12:06:24 -0800601 EXPECT_TRUE(receiver != nullptr);
602 num_added_tracks_++;
603 last_added_track_label_ = receiver->id();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000604 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000605
606 // Returns the label of the last added stream.
607 // Empty string if no stream have been added.
608 std::string GetLastAddedStreamLabel() {
609 if (last_added_stream_.get())
610 return last_added_stream_->label();
611 return "";
612 }
613 std::string GetLastRemovedStreamLabel() {
614 if (last_removed_stream_.get())
615 return last_removed_stream_->label();
616 return "";
617 }
618
zhihuang9763d562016-08-05 11:14:50 -0700619 rtc::scoped_refptr<PeerConnectionInterface> pc_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000620 PeerConnectionInterface::SignalingState state_;
kwibergd1fe2812016-04-27 06:47:29 -0700621 std::unique_ptr<IceCandidateInterface> last_candidate_;
zhihuang9763d562016-08-05 11:14:50 -0700622 rtc::scoped_refptr<DataChannelInterface> last_datachannel_;
deadbeefab9b2d12015-10-14 11:33:11 -0700623 rtc::scoped_refptr<StreamCollection> remote_streams_;
624 bool renegotiation_needed_ = false;
625 bool ice_complete_ = false;
zhihuang81c3a032016-11-17 12:06:24 -0800626 bool callback_triggered_ = false;
627 int num_added_tracks_ = 0;
628 std::string last_added_track_label_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000629
630 private:
zhihuang9763d562016-08-05 11:14:50 -0700631 rtc::scoped_refptr<MediaStreamInterface> last_added_stream_;
632 rtc::scoped_refptr<MediaStreamInterface> last_removed_stream_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000633};
634
635} // namespace
deadbeefab9b2d12015-10-14 11:33:11 -0700636
zhihuang29ff8442016-07-27 11:07:25 -0700637// The PeerConnectionMediaConfig tests below verify that configuration
638// and constraints are propagated into the MediaConfig passed to
639// CreateMediaController. These settings are intended for MediaChannel
640// constructors, but that is not exercised by these unittest.
641class PeerConnectionFactoryForTest : public webrtc::PeerConnectionFactory {
642 public:
kwiberg1e4e8cb2017-01-31 01:48:08 -0800643 PeerConnectionFactoryForTest()
644 : webrtc::PeerConnectionFactory(
645 webrtc::CreateBuiltinAudioEncoderFactory(),
646 webrtc::CreateBuiltinAudioDecoderFactory()) {}
647
zhihuang29ff8442016-07-27 11:07:25 -0700648 cricket::TransportController* CreateTransportController(
Honghai Zhangbfd398c2016-08-30 22:07:42 -0700649 cricket::PortAllocator* port_allocator,
650 bool redetermine_role_on_ice_restart) override {
zhihuang29ff8442016-07-27 11:07:25 -0700651 transport_controller = new cricket::TransportController(
Honghai Zhangbfd398c2016-08-30 22:07:42 -0700652 rtc::Thread::Current(), rtc::Thread::Current(), port_allocator,
deadbeef7914b8c2017-04-21 03:23:33 -0700653 redetermine_role_on_ice_restart, rtc::CryptoOptions());
zhihuang29ff8442016-07-27 11:07:25 -0700654 return transport_controller;
655 }
656
657 cricket::TransportController* transport_controller;
zhihuang29ff8442016-07-27 11:07:25 -0700658};
659
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000660class PeerConnectionInterfaceTest : public testing::Test {
661 protected:
phoglund37ebcf02016-01-08 05:04:57 -0800662 PeerConnectionInterfaceTest() {
663#ifdef WEBRTC_ANDROID
664 webrtc::InitializeAndroidObjects();
665#endif
666 }
667
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000668 virtual void SetUp() {
669 pc_factory_ = webrtc::CreatePeerConnectionFactory(
danilchape9021a32016-05-17 01:52:02 -0700670 rtc::Thread::Current(), rtc::Thread::Current(), rtc::Thread::Current(),
671 nullptr, nullptr, nullptr);
672 ASSERT_TRUE(pc_factory_);
zhihuang29ff8442016-07-27 11:07:25 -0700673 pc_factory_for_test_ =
674 new rtc::RefCountedObject<PeerConnectionFactoryForTest>();
675 pc_factory_for_test_->Initialize();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000676 }
677
678 void CreatePeerConnection() {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700679 CreatePeerConnection(PeerConnectionInterface::RTCConfiguration(), nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000680 }
681
deadbeef293e9262017-01-11 12:28:30 -0800682 // DTLS does not work in a loopback call, so is disabled for most of the
683 // tests in this file.
684 void CreatePeerConnectionWithoutDtls() {
685 FakeConstraints no_dtls_constraints;
686 no_dtls_constraints.AddMandatory(
687 webrtc::MediaConstraintsInterface::kEnableDtlsSrtp, false);
688
689 CreatePeerConnection(PeerConnectionInterface::RTCConfiguration(),
690 &no_dtls_constraints);
691 }
692
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000693 void CreatePeerConnection(webrtc::MediaConstraintsInterface* constraints) {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700694 CreatePeerConnection(PeerConnectionInterface::RTCConfiguration(),
695 constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000696 }
697
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700698 void CreatePeerConnectionWithIceTransportsType(
699 PeerConnectionInterface::IceTransportsType type) {
700 PeerConnectionInterface::RTCConfiguration config;
701 config.type = type;
702 return CreatePeerConnection(config, nullptr);
703 }
704
705 void CreatePeerConnectionWithIceServer(const std::string& uri,
706 const std::string& password) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800707 PeerConnectionInterface::RTCConfiguration config;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000708 PeerConnectionInterface::IceServer server;
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700709 server.uri = uri;
710 server.password = password;
711 config.servers.push_back(server);
712 CreatePeerConnection(config, nullptr);
713 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000714
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700715 void CreatePeerConnection(PeerConnectionInterface::RTCConfiguration config,
716 webrtc::MediaConstraintsInterface* constraints) {
kwibergd1fe2812016-04-27 06:47:29 -0700717 std::unique_ptr<cricket::FakePortAllocator> port_allocator(
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800718 new cricket::FakePortAllocator(rtc::Thread::Current(), nullptr));
719 port_allocator_ = port_allocator.get();
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000720
deadbeef1dcb1642017-03-29 21:08:16 -0700721 // Create certificate generator unless DTLS constraint is explicitly set to
722 // false.
Henrik Boströmd79599d2016-06-01 13:58:50 +0200723 std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator;
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000724 bool dtls;
725 if (FindConstraint(constraints,
726 webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
727 &dtls,
Henrik Boström5e56c592015-08-11 10:33:13 +0200728 nullptr) && dtls) {
deadbeef8662f942017-01-20 21:20:51 -0800729 fake_certificate_generator_ = new FakeRTCCertificateGenerator();
730 cert_generator.reset(fake_certificate_generator_);
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000731 }
Henrik Boströmd79599d2016-06-01 13:58:50 +0200732 pc_ = pc_factory_->CreatePeerConnection(
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800733 config, constraints, std::move(port_allocator),
Henrik Boströmd79599d2016-06-01 13:58:50 +0200734 std::move(cert_generator), &observer_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000735 ASSERT_TRUE(pc_.get() != NULL);
736 observer_.SetPeerConnectionInterface(pc_.get());
737 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
738 }
739
deadbeef0a6c4ca2015-10-06 11:38:28 -0700740 void CreatePeerConnectionExpectFail(const std::string& uri) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800741 PeerConnectionInterface::RTCConfiguration config;
deadbeef0a6c4ca2015-10-06 11:38:28 -0700742 PeerConnectionInterface::IceServer server;
deadbeef0a6c4ca2015-10-06 11:38:28 -0700743 server.uri = uri;
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800744 config.servers.push_back(server);
deadbeef0a6c4ca2015-10-06 11:38:28 -0700745
zhihuang9763d562016-08-05 11:14:50 -0700746 rtc::scoped_refptr<PeerConnectionInterface> pc;
hbosd7973cc2016-05-27 06:08:53 -0700747 pc = pc_factory_->CreatePeerConnection(config, nullptr, nullptr, nullptr,
748 &observer_);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800749 EXPECT_EQ(nullptr, pc);
deadbeef0a6c4ca2015-10-06 11:38:28 -0700750 }
751
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000752 void CreatePeerConnectionWithDifferentConfigurations() {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700753 CreatePeerConnectionWithIceServer(kStunAddressOnly, "");
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800754 EXPECT_EQ(1u, port_allocator_->stun_servers().size());
755 EXPECT_EQ(0u, port_allocator_->turn_servers().size());
756 EXPECT_EQ("address", port_allocator_->stun_servers().begin()->hostname());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000757 EXPECT_EQ(kDefaultStunPort,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800758 port_allocator_->stun_servers().begin()->port());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000759
deadbeef0a6c4ca2015-10-06 11:38:28 -0700760 CreatePeerConnectionExpectFail(kStunInvalidPort);
761 CreatePeerConnectionExpectFail(kStunAddressPortAndMore1);
762 CreatePeerConnectionExpectFail(kStunAddressPortAndMore2);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000763
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700764 CreatePeerConnectionWithIceServer(kTurnIceServerUri, kTurnPassword);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800765 EXPECT_EQ(0u, port_allocator_->stun_servers().size());
766 EXPECT_EQ(1u, port_allocator_->turn_servers().size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000767 EXPECT_EQ(kTurnUsername,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800768 port_allocator_->turn_servers()[0].credentials.username);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000769 EXPECT_EQ(kTurnPassword,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800770 port_allocator_->turn_servers()[0].credentials.password);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000771 EXPECT_EQ(kTurnHostname,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800772 port_allocator_->turn_servers()[0].ports[0].address.hostname());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000773 }
774
775 void ReleasePeerConnection() {
776 pc_ = NULL;
777 observer_.SetPeerConnectionInterface(NULL);
778 }
779
deadbeefab9b2d12015-10-14 11:33:11 -0700780 void AddVideoStream(const std::string& label) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000781 // Create a local stream.
zhihuang9763d562016-08-05 11:14:50 -0700782 rtc::scoped_refptr<MediaStreamInterface> stream(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000783 pc_factory_->CreateLocalMediaStream(label));
zhihuang9763d562016-08-05 11:14:50 -0700784 rtc::scoped_refptr<VideoTrackSourceInterface> video_source(
deadbeef112b2e92017-02-10 20:13:37 -0800785 pc_factory_->CreateVideoSource(std::unique_ptr<cricket::VideoCapturer>(
786 new cricket::FakeVideoCapturer()),
787 NULL));
zhihuang9763d562016-08-05 11:14:50 -0700788 rtc::scoped_refptr<VideoTrackInterface> video_track(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000789 pc_factory_->CreateVideoTrack(label + "v0", video_source));
790 stream->AddTrack(video_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +0000791 EXPECT_TRUE(pc_->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000792 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
793 observer_.renegotiation_needed_ = false;
794 }
795
796 void AddVoiceStream(const std::string& label) {
797 // Create a local stream.
zhihuang9763d562016-08-05 11:14:50 -0700798 rtc::scoped_refptr<MediaStreamInterface> stream(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000799 pc_factory_->CreateLocalMediaStream(label));
zhihuang9763d562016-08-05 11:14:50 -0700800 rtc::scoped_refptr<AudioTrackInterface> audio_track(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000801 pc_factory_->CreateAudioTrack(label + "a0", NULL));
802 stream->AddTrack(audio_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +0000803 EXPECT_TRUE(pc_->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000804 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
805 observer_.renegotiation_needed_ = false;
806 }
807
808 void AddAudioVideoStream(const std::string& stream_label,
809 const std::string& audio_track_label,
810 const std::string& video_track_label) {
811 // Create a local stream.
zhihuang9763d562016-08-05 11:14:50 -0700812 rtc::scoped_refptr<MediaStreamInterface> stream(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000813 pc_factory_->CreateLocalMediaStream(stream_label));
zhihuang9763d562016-08-05 11:14:50 -0700814 rtc::scoped_refptr<AudioTrackInterface> audio_track(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000815 pc_factory_->CreateAudioTrack(
816 audio_track_label, static_cast<AudioSourceInterface*>(NULL)));
817 stream->AddTrack(audio_track.get());
zhihuang9763d562016-08-05 11:14:50 -0700818 rtc::scoped_refptr<VideoTrackInterface> video_track(
nisseaf510af2016-03-21 08:20:42 -0700819 pc_factory_->CreateVideoTrack(
deadbeef112b2e92017-02-10 20:13:37 -0800820 video_track_label, pc_factory_->CreateVideoSource(
821 std::unique_ptr<cricket::VideoCapturer>(
822 new cricket::FakeVideoCapturer()))));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000823 stream->AddTrack(video_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +0000824 EXPECT_TRUE(pc_->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000825 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
826 observer_.renegotiation_needed_ = false;
827 }
828
kwibergd1fe2812016-04-27 06:47:29 -0700829 bool DoCreateOfferAnswer(std::unique_ptr<SessionDescriptionInterface>* desc,
deadbeefc80741f2015-10-22 13:14:45 -0700830 bool offer,
831 MediaConstraintsInterface* constraints) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000832 rtc::scoped_refptr<MockCreateSessionDescriptionObserver>
833 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000834 MockCreateSessionDescriptionObserver>());
835 if (offer) {
deadbeefc80741f2015-10-22 13:14:45 -0700836 pc_->CreateOffer(observer, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000837 } else {
deadbeefc80741f2015-10-22 13:14:45 -0700838 pc_->CreateAnswer(observer, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000839 }
840 EXPECT_EQ_WAIT(true, observer->called(), kTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -0700841 *desc = observer->MoveDescription();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000842 return observer->result();
843 }
844
kwibergd1fe2812016-04-27 06:47:29 -0700845 bool DoCreateOffer(std::unique_ptr<SessionDescriptionInterface>* desc,
deadbeefc80741f2015-10-22 13:14:45 -0700846 MediaConstraintsInterface* constraints) {
847 return DoCreateOfferAnswer(desc, true, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000848 }
849
kwibergd1fe2812016-04-27 06:47:29 -0700850 bool DoCreateAnswer(std::unique_ptr<SessionDescriptionInterface>* desc,
deadbeefc80741f2015-10-22 13:14:45 -0700851 MediaConstraintsInterface* constraints) {
852 return DoCreateOfferAnswer(desc, false, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000853 }
854
855 bool DoSetSessionDescription(SessionDescriptionInterface* desc, bool local) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000856 rtc::scoped_refptr<MockSetSessionDescriptionObserver>
857 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000858 MockSetSessionDescriptionObserver>());
859 if (local) {
860 pc_->SetLocalDescription(observer, desc);
861 } else {
862 pc_->SetRemoteDescription(observer, desc);
863 }
zhihuang29ff8442016-07-27 11:07:25 -0700864 if (pc_->signaling_state() != PeerConnectionInterface::kClosed) {
865 EXPECT_EQ_WAIT(true, observer->called(), kTimeout);
866 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000867 return observer->result();
868 }
869
870 bool DoSetLocalDescription(SessionDescriptionInterface* desc) {
871 return DoSetSessionDescription(desc, true);
872 }
873
874 bool DoSetRemoteDescription(SessionDescriptionInterface* desc) {
875 return DoSetSessionDescription(desc, false);
876 }
877
878 // Calls PeerConnection::GetStats and check the return value.
879 // It does not verify the values in the StatReports since a RTCP packet might
880 // be required.
881 bool DoGetStats(MediaStreamTrackInterface* track) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000882 rtc::scoped_refptr<MockStatsObserver> observer(
883 new rtc::RefCountedObject<MockStatsObserver>());
jiayl@webrtc.orgdb41b4d2014-03-03 21:30:06 +0000884 if (!pc_->GetStats(
885 observer, track, PeerConnectionInterface::kStatsOutputLevelStandard))
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000886 return false;
887 EXPECT_TRUE_WAIT(observer->called(), kTimeout);
888 return observer->called();
889 }
890
891 void InitiateCall() {
deadbeef293e9262017-01-11 12:28:30 -0800892 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000893 // Create a local stream with audio&video tracks.
894 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
895 CreateOfferReceiveAnswer();
896 }
897
898 // Verify that RTP Header extensions has been negotiated for audio and video.
899 void VerifyRemoteRtpHeaderExtensions() {
900 const cricket::MediaContentDescription* desc =
901 cricket::GetFirstAudioContentDescription(
902 pc_->remote_description()->description());
903 ASSERT_TRUE(desc != NULL);
904 EXPECT_GT(desc->rtp_header_extensions().size(), 0u);
905
906 desc = cricket::GetFirstVideoContentDescription(
907 pc_->remote_description()->description());
908 ASSERT_TRUE(desc != NULL);
909 EXPECT_GT(desc->rtp_header_extensions().size(), 0u);
910 }
911
912 void CreateOfferAsRemoteDescription() {
kwibergd1fe2812016-04-27 06:47:29 -0700913 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -0700914 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000915 std::string sdp;
916 EXPECT_TRUE(offer->ToString(&sdp));
917 SessionDescriptionInterface* remote_offer =
918 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
919 sdp, NULL);
920 EXPECT_TRUE(DoSetRemoteDescription(remote_offer));
921 EXPECT_EQ(PeerConnectionInterface::kHaveRemoteOffer, observer_.state_);
922 }
923
deadbeefab9b2d12015-10-14 11:33:11 -0700924 void CreateAndSetRemoteOffer(const std::string& sdp) {
925 SessionDescriptionInterface* remote_offer =
926 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
927 sdp, nullptr);
928 EXPECT_TRUE(DoSetRemoteDescription(remote_offer));
929 EXPECT_EQ(PeerConnectionInterface::kHaveRemoteOffer, observer_.state_);
930 }
931
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000932 void CreateAnswerAsLocalDescription() {
kwibergd1fe2812016-04-27 06:47:29 -0700933 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -0700934 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000935
936 // TODO(perkj): Currently SetLocalDescription fails if any parameters in an
937 // audio codec change, even if the parameter has nothing to do with
938 // receiving. Not all parameters are serialized to SDP.
939 // Since CreatePrAnswerAsLocalDescription serialize/deserialize
940 // the SessionDescription, it is necessary to do that here to in order to
941 // get ReceiveOfferCreatePrAnswerAndAnswer and RenegotiateAudioOnly to pass.
942 // https://code.google.com/p/webrtc/issues/detail?id=1356
943 std::string sdp;
944 EXPECT_TRUE(answer->ToString(&sdp));
945 SessionDescriptionInterface* new_answer =
946 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
947 sdp, NULL);
948 EXPECT_TRUE(DoSetLocalDescription(new_answer));
949 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
950 }
951
952 void CreatePrAnswerAsLocalDescription() {
kwibergd1fe2812016-04-27 06:47:29 -0700953 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -0700954 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000955
956 std::string sdp;
957 EXPECT_TRUE(answer->ToString(&sdp));
958 SessionDescriptionInterface* pr_answer =
959 webrtc::CreateSessionDescription(SessionDescriptionInterface::kPrAnswer,
960 sdp, NULL);
961 EXPECT_TRUE(DoSetLocalDescription(pr_answer));
962 EXPECT_EQ(PeerConnectionInterface::kHaveLocalPrAnswer, observer_.state_);
963 }
964
965 void CreateOfferReceiveAnswer() {
966 CreateOfferAsLocalDescription();
967 std::string sdp;
968 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
969 CreateAnswerAsRemoteDescription(sdp);
970 }
971
972 void CreateOfferAsLocalDescription() {
kwibergd1fe2812016-04-27 06:47:29 -0700973 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -0700974 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000975 // TODO(perkj): Currently SetLocalDescription fails if any parameters in an
976 // audio codec change, even if the parameter has nothing to do with
977 // receiving. Not all parameters are serialized to SDP.
978 // Since CreatePrAnswerAsLocalDescription serialize/deserialize
979 // the SessionDescription, it is necessary to do that here to in order to
980 // get ReceiveOfferCreatePrAnswerAndAnswer and RenegotiateAudioOnly to pass.
981 // https://code.google.com/p/webrtc/issues/detail?id=1356
982 std::string sdp;
983 EXPECT_TRUE(offer->ToString(&sdp));
984 SessionDescriptionInterface* new_offer =
985 webrtc::CreateSessionDescription(
986 SessionDescriptionInterface::kOffer,
987 sdp, NULL);
988
989 EXPECT_TRUE(DoSetLocalDescription(new_offer));
990 EXPECT_EQ(PeerConnectionInterface::kHaveLocalOffer, observer_.state_);
mallinath@webrtc.org68cbd012014-01-22 00:16:46 +0000991 // Wait for the ice_complete message, so that SDP will have candidates.
992 EXPECT_TRUE_WAIT(observer_.ice_complete_, kTimeout);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000993 }
994
deadbeefab9b2d12015-10-14 11:33:11 -0700995 void CreateAnswerAsRemoteDescription(const std::string& sdp) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000996 webrtc::JsepSessionDescription* answer = new webrtc::JsepSessionDescription(
997 SessionDescriptionInterface::kAnswer);
deadbeefab9b2d12015-10-14 11:33:11 -0700998 EXPECT_TRUE(answer->Initialize(sdp, NULL));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000999 EXPECT_TRUE(DoSetRemoteDescription(answer));
1000 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
1001 }
1002
deadbeefab9b2d12015-10-14 11:33:11 -07001003 void CreatePrAnswerAndAnswerAsRemoteDescription(const std::string& sdp) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001004 webrtc::JsepSessionDescription* pr_answer =
1005 new webrtc::JsepSessionDescription(
1006 SessionDescriptionInterface::kPrAnswer);
deadbeefab9b2d12015-10-14 11:33:11 -07001007 EXPECT_TRUE(pr_answer->Initialize(sdp, NULL));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001008 EXPECT_TRUE(DoSetRemoteDescription(pr_answer));
1009 EXPECT_EQ(PeerConnectionInterface::kHaveRemotePrAnswer, observer_.state_);
1010 webrtc::JsepSessionDescription* answer =
1011 new webrtc::JsepSessionDescription(
1012 SessionDescriptionInterface::kAnswer);
deadbeefab9b2d12015-10-14 11:33:11 -07001013 EXPECT_TRUE(answer->Initialize(sdp, NULL));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001014 EXPECT_TRUE(DoSetRemoteDescription(answer));
1015 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
1016 }
1017
1018 // Help function used for waiting until a the last signaled remote stream has
1019 // the same label as |stream_label|. In a few of the tests in this file we
1020 // answer with the same session description as we offer and thus we can
1021 // check if OnAddStream have been called with the same stream as we offer to
1022 // send.
1023 void WaitAndVerifyOnAddStream(const std::string& stream_label) {
1024 EXPECT_EQ_WAIT(stream_label, observer_.GetLastAddedStreamLabel(), kTimeout);
1025 }
1026
1027 // Creates an offer and applies it as a local session description.
1028 // Creates an answer with the same SDP an the offer but removes all lines
1029 // that start with a:ssrc"
1030 void CreateOfferReceiveAnswerWithoutSsrc() {
1031 CreateOfferAsLocalDescription();
1032 std::string sdp;
1033 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
1034 SetSsrcToZero(&sdp);
1035 CreateAnswerAsRemoteDescription(sdp);
1036 }
1037
deadbeefab9b2d12015-10-14 11:33:11 -07001038 // This function creates a MediaStream with label kStreams[0] and
1039 // |number_of_audio_tracks| and |number_of_video_tracks| tracks and the
1040 // corresponding SessionDescriptionInterface. The SessionDescriptionInterface
kwiberg2bbff992016-03-16 11:03:04 -07001041 // is returned and the MediaStream is stored in
deadbeefab9b2d12015-10-14 11:33:11 -07001042 // |reference_collection_|
kwibergd1fe2812016-04-27 06:47:29 -07001043 std::unique_ptr<SessionDescriptionInterface>
kwiberg2bbff992016-03-16 11:03:04 -07001044 CreateSessionDescriptionAndReference(size_t number_of_audio_tracks,
1045 size_t number_of_video_tracks) {
1046 EXPECT_LE(number_of_audio_tracks, 2u);
1047 EXPECT_LE(number_of_video_tracks, 2u);
deadbeefab9b2d12015-10-14 11:33:11 -07001048
1049 reference_collection_ = StreamCollection::Create();
1050 std::string sdp_ms1 = std::string(kSdpStringInit);
1051
1052 std::string mediastream_label = kStreams[0];
1053
1054 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream(
1055 webrtc::MediaStream::Create(mediastream_label));
1056 reference_collection_->AddStream(stream);
1057
1058 if (number_of_audio_tracks > 0) {
1059 sdp_ms1 += std::string(kSdpStringAudio);
1060 sdp_ms1 += std::string(kSdpStringMs1Audio0);
1061 AddAudioTrack(kAudioTracks[0], stream);
1062 }
1063 if (number_of_audio_tracks > 1) {
1064 sdp_ms1 += kSdpStringMs1Audio1;
1065 AddAudioTrack(kAudioTracks[1], stream);
1066 }
1067
1068 if (number_of_video_tracks > 0) {
1069 sdp_ms1 += std::string(kSdpStringVideo);
1070 sdp_ms1 += std::string(kSdpStringMs1Video0);
1071 AddVideoTrack(kVideoTracks[0], stream);
1072 }
1073 if (number_of_video_tracks > 1) {
1074 sdp_ms1 += kSdpStringMs1Video1;
1075 AddVideoTrack(kVideoTracks[1], stream);
1076 }
1077
kwibergd1fe2812016-04-27 06:47:29 -07001078 return std::unique_ptr<SessionDescriptionInterface>(
kwiberg2bbff992016-03-16 11:03:04 -07001079 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
1080 sdp_ms1, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07001081 }
1082
1083 void AddAudioTrack(const std::string& track_id,
1084 MediaStreamInterface* stream) {
1085 rtc::scoped_refptr<webrtc::AudioTrackInterface> audio_track(
1086 webrtc::AudioTrack::Create(track_id, nullptr));
1087 ASSERT_TRUE(stream->AddTrack(audio_track));
1088 }
1089
1090 void AddVideoTrack(const std::string& track_id,
1091 MediaStreamInterface* stream) {
1092 rtc::scoped_refptr<webrtc::VideoTrackInterface> video_track(
nisseaf510af2016-03-21 08:20:42 -07001093 webrtc::VideoTrack::Create(track_id,
1094 webrtc::FakeVideoTrackSource::Create()));
deadbeefab9b2d12015-10-14 11:33:11 -07001095 ASSERT_TRUE(stream->AddTrack(video_track));
1096 }
1097
kwibergfd8be342016-05-14 19:44:11 -07001098 std::unique_ptr<SessionDescriptionInterface> CreateOfferWithOneAudioStream() {
deadbeef293e9262017-01-11 12:28:30 -08001099 CreatePeerConnectionWithoutDtls();
zhihuang8f65cdf2016-05-06 18:40:30 -07001100 AddVoiceStream(kStreamLabel1);
kwibergfd8be342016-05-14 19:44:11 -07001101 std::unique_ptr<SessionDescriptionInterface> offer;
zhihuang8f65cdf2016-05-06 18:40:30 -07001102 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
1103 return offer;
1104 }
1105
kwibergfd8be342016-05-14 19:44:11 -07001106 std::unique_ptr<SessionDescriptionInterface>
zhihuang8f65cdf2016-05-06 18:40:30 -07001107 CreateAnswerWithOneAudioStream() {
kwibergfd8be342016-05-14 19:44:11 -07001108 std::unique_ptr<SessionDescriptionInterface> offer =
zhihuang8f65cdf2016-05-06 18:40:30 -07001109 CreateOfferWithOneAudioStream();
1110 EXPECT_TRUE(DoSetRemoteDescription(offer.release()));
kwibergfd8be342016-05-14 19:44:11 -07001111 std::unique_ptr<SessionDescriptionInterface> answer;
zhihuang8f65cdf2016-05-06 18:40:30 -07001112 EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
1113 return answer;
1114 }
1115
1116 const std::string& GetFirstAudioStreamCname(
1117 const SessionDescriptionInterface* desc) {
1118 const cricket::ContentInfo* audio_content =
1119 cricket::GetFirstAudioContent(desc->description());
1120 const cricket::AudioContentDescription* audio_desc =
1121 static_cast<const cricket::AudioContentDescription*>(
1122 audio_content->description);
1123 return audio_desc->streams()[0].cname;
1124 }
1125
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08001126 cricket::FakePortAllocator* port_allocator_ = nullptr;
deadbeef8662f942017-01-20 21:20:51 -08001127 FakeRTCCertificateGenerator* fake_certificate_generator_ = nullptr;
zhihuang9763d562016-08-05 11:14:50 -07001128 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface> pc_factory_;
1129 rtc::scoped_refptr<PeerConnectionFactoryForTest> pc_factory_for_test_;
1130 rtc::scoped_refptr<PeerConnectionInterface> pc_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001131 MockPeerConnectionObserver observer_;
deadbeefab9b2d12015-10-14 11:33:11 -07001132 rtc::scoped_refptr<StreamCollection> reference_collection_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001133};
1134
zhihuang29ff8442016-07-27 11:07:25 -07001135// Test that no callbacks on the PeerConnectionObserver are called after the
1136// PeerConnection is closed.
1137TEST_F(PeerConnectionInterfaceTest, CloseAndTestCallbackFunctions) {
zhihuang9763d562016-08-05 11:14:50 -07001138 rtc::scoped_refptr<PeerConnectionInterface> pc(
zhihuang29ff8442016-07-27 11:07:25 -07001139 pc_factory_for_test_->CreatePeerConnection(
1140 PeerConnectionInterface::RTCConfiguration(), nullptr, nullptr,
1141 nullptr, &observer_));
1142 observer_.SetPeerConnectionInterface(pc.get());
1143 pc->Close();
1144
1145 // No callbacks is expected to be called.
zhihuang81c3a032016-11-17 12:06:24 -08001146 observer_.callback_triggered_ = false;
zhihuang29ff8442016-07-27 11:07:25 -07001147 std::vector<cricket::Candidate> candidates;
1148 pc_factory_for_test_->transport_controller->SignalGatheringState(
1149 cricket::IceGatheringState{});
1150 pc_factory_for_test_->transport_controller->SignalCandidatesGathered(
1151 "", candidates);
1152 pc_factory_for_test_->transport_controller->SignalConnectionState(
1153 cricket::IceConnectionState{});
1154 pc_factory_for_test_->transport_controller->SignalCandidatesRemoved(
1155 candidates);
1156 pc_factory_for_test_->transport_controller->SignalReceiving(false);
zhihuang81c3a032016-11-17 12:06:24 -08001157 EXPECT_FALSE(observer_.callback_triggered_);
zhihuang29ff8442016-07-27 11:07:25 -07001158}
1159
zhihuang8f65cdf2016-05-06 18:40:30 -07001160// Generate different CNAMEs when PeerConnections are created.
1161// The CNAMEs are expected to be generated randomly. It is possible
1162// that the test fails, though the possibility is very low.
1163TEST_F(PeerConnectionInterfaceTest, CnameGenerationInOffer) {
kwibergfd8be342016-05-14 19:44:11 -07001164 std::unique_ptr<SessionDescriptionInterface> offer1 =
zhihuang8f65cdf2016-05-06 18:40:30 -07001165 CreateOfferWithOneAudioStream();
kwibergfd8be342016-05-14 19:44:11 -07001166 std::unique_ptr<SessionDescriptionInterface> offer2 =
zhihuang8f65cdf2016-05-06 18:40:30 -07001167 CreateOfferWithOneAudioStream();
1168 EXPECT_NE(GetFirstAudioStreamCname(offer1.get()),
1169 GetFirstAudioStreamCname(offer2.get()));
1170}
1171
1172TEST_F(PeerConnectionInterfaceTest, CnameGenerationInAnswer) {
kwibergfd8be342016-05-14 19:44:11 -07001173 std::unique_ptr<SessionDescriptionInterface> answer1 =
zhihuang8f65cdf2016-05-06 18:40:30 -07001174 CreateAnswerWithOneAudioStream();
kwibergfd8be342016-05-14 19:44:11 -07001175 std::unique_ptr<SessionDescriptionInterface> answer2 =
zhihuang8f65cdf2016-05-06 18:40:30 -07001176 CreateAnswerWithOneAudioStream();
1177 EXPECT_NE(GetFirstAudioStreamCname(answer1.get()),
1178 GetFirstAudioStreamCname(answer2.get()));
1179}
1180
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001181TEST_F(PeerConnectionInterfaceTest,
1182 CreatePeerConnectionWithDifferentConfigurations) {
1183 CreatePeerConnectionWithDifferentConfigurations();
1184}
1185
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001186TEST_F(PeerConnectionInterfaceTest,
1187 CreatePeerConnectionWithDifferentIceTransportsTypes) {
1188 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kNone);
1189 EXPECT_EQ(cricket::CF_NONE, port_allocator_->candidate_filter());
1190 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kRelay);
1191 EXPECT_EQ(cricket::CF_RELAY, port_allocator_->candidate_filter());
1192 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kNoHost);
1193 EXPECT_EQ(cricket::CF_ALL & ~cricket::CF_HOST,
1194 port_allocator_->candidate_filter());
1195 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kAll);
1196 EXPECT_EQ(cricket::CF_ALL, port_allocator_->candidate_filter());
1197}
1198
1199// Test that when a PeerConnection is created with a nonzero candidate pool
1200// size, the pooled PortAllocatorSession is created with all the attributes
1201// in the RTCConfiguration.
1202TEST_F(PeerConnectionInterfaceTest, CreatePeerConnectionWithPooledCandidates) {
1203 PeerConnectionInterface::RTCConfiguration config;
1204 PeerConnectionInterface::IceServer server;
1205 server.uri = kStunAddressOnly;
1206 config.servers.push_back(server);
1207 config.type = PeerConnectionInterface::kRelay;
1208 config.disable_ipv6 = true;
1209 config.tcp_candidate_policy =
1210 PeerConnectionInterface::kTcpCandidatePolicyDisabled;
honghaiz60347052016-05-31 18:29:12 -07001211 config.candidate_network_policy =
1212 PeerConnectionInterface::kCandidateNetworkPolicyLowCost;
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001213 config.ice_candidate_pool_size = 1;
1214 CreatePeerConnection(config, nullptr);
1215
1216 const cricket::FakePortAllocatorSession* session =
1217 static_cast<const cricket::FakePortAllocatorSession*>(
1218 port_allocator_->GetPooledSession());
1219 ASSERT_NE(nullptr, session);
1220 EXPECT_EQ(1UL, session->stun_servers().size());
1221 EXPECT_EQ(0U, session->flags() & cricket::PORTALLOCATOR_ENABLE_IPV6);
1222 EXPECT_LT(0U, session->flags() & cricket::PORTALLOCATOR_DISABLE_TCP);
honghaiz60347052016-05-31 18:29:12 -07001223 EXPECT_LT(0U,
1224 session->flags() & cricket::PORTALLOCATOR_DISABLE_COSTLY_NETWORKS);
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001225}
1226
Taylor Brandstetterf8e65772016-06-27 17:20:15 -07001227// Test that the PeerConnection initializes the port allocator passed into it,
1228// and on the correct thread.
1229TEST_F(PeerConnectionInterfaceTest,
1230 CreatePeerConnectionInitializesPortAllocator) {
1231 rtc::Thread network_thread;
1232 network_thread.Start();
1233 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface> pc_factory(
1234 webrtc::CreatePeerConnectionFactory(
1235 &network_thread, rtc::Thread::Current(), rtc::Thread::Current(),
1236 nullptr, nullptr, nullptr));
1237 std::unique_ptr<cricket::FakePortAllocator> port_allocator(
1238 new cricket::FakePortAllocator(&network_thread, nullptr));
1239 cricket::FakePortAllocator* raw_port_allocator = port_allocator.get();
1240 PeerConnectionInterface::RTCConfiguration config;
1241 rtc::scoped_refptr<PeerConnectionInterface> pc(
1242 pc_factory->CreatePeerConnection(
1243 config, nullptr, std::move(port_allocator), nullptr, &observer_));
1244 // FakePortAllocator RTC_CHECKs that it's initialized on the right thread,
1245 // so all we have to do here is check that it's initialized.
1246 EXPECT_TRUE(raw_port_allocator->initialized());
1247}
1248
deadbeef46c73892016-11-16 19:42:04 -08001249// Check that GetConfiguration returns the configuration the PeerConnection was
1250// constructed with, before SetConfiguration is called.
1251TEST_F(PeerConnectionInterfaceTest, GetConfigurationAfterCreatePeerConnection) {
1252 PeerConnectionInterface::RTCConfiguration config;
1253 config.type = PeerConnectionInterface::kRelay;
1254 CreatePeerConnection(config, nullptr);
1255
1256 PeerConnectionInterface::RTCConfiguration returned_config =
1257 pc_->GetConfiguration();
1258 EXPECT_EQ(PeerConnectionInterface::kRelay, returned_config.type);
1259}
1260
1261// Check that GetConfiguration returns the last configuration passed into
1262// SetConfiguration.
1263TEST_F(PeerConnectionInterfaceTest, GetConfigurationAfterSetConfiguration) {
1264 CreatePeerConnection();
1265
1266 PeerConnectionInterface::RTCConfiguration config;
1267 config.type = PeerConnectionInterface::kRelay;
1268 EXPECT_TRUE(pc_->SetConfiguration(config));
1269
1270 PeerConnectionInterface::RTCConfiguration returned_config =
1271 pc_->GetConfiguration();
1272 EXPECT_EQ(PeerConnectionInterface::kRelay, returned_config.type);
1273}
1274
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001275TEST_F(PeerConnectionInterfaceTest, AddStreams) {
deadbeef293e9262017-01-11 12:28:30 -08001276 CreatePeerConnectionWithoutDtls();
deadbeefab9b2d12015-10-14 11:33:11 -07001277 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001278 AddVoiceStream(kStreamLabel2);
1279 ASSERT_EQ(2u, pc_->local_streams()->count());
1280
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001281 // Test we can add multiple local streams to one peerconnection.
zhihuang9763d562016-08-05 11:14:50 -07001282 rtc::scoped_refptr<MediaStreamInterface> stream(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001283 pc_factory_->CreateLocalMediaStream(kStreamLabel3));
zhihuang9763d562016-08-05 11:14:50 -07001284 rtc::scoped_refptr<AudioTrackInterface> audio_track(
1285 pc_factory_->CreateAudioTrack(kStreamLabel3,
1286 static_cast<AudioSourceInterface*>(NULL)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001287 stream->AddTrack(audio_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +00001288 EXPECT_TRUE(pc_->AddStream(stream));
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001289 EXPECT_EQ(3u, pc_->local_streams()->count());
1290
1291 // Remove the third stream.
1292 pc_->RemoveStream(pc_->local_streams()->at(2));
1293 EXPECT_EQ(2u, pc_->local_streams()->count());
1294
1295 // Remove the second stream.
1296 pc_->RemoveStream(pc_->local_streams()->at(1));
1297 EXPECT_EQ(1u, pc_->local_streams()->count());
1298
1299 // Remove the first stream.
1300 pc_->RemoveStream(pc_->local_streams()->at(0));
1301 EXPECT_EQ(0u, pc_->local_streams()->count());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001302}
1303
deadbeefab9b2d12015-10-14 11:33:11 -07001304// Test that the created offer includes streams we added.
1305TEST_F(PeerConnectionInterfaceTest, AddedStreamsPresentInOffer) {
deadbeef293e9262017-01-11 12:28:30 -08001306 CreatePeerConnectionWithoutDtls();
deadbeefab9b2d12015-10-14 11:33:11 -07001307 AddAudioVideoStream(kStreamLabel1, "audio_track", "video_track");
kwibergd1fe2812016-04-27 06:47:29 -07001308 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001309 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07001310
1311 const cricket::ContentInfo* audio_content =
1312 cricket::GetFirstAudioContent(offer->description());
1313 const cricket::AudioContentDescription* audio_desc =
1314 static_cast<const cricket::AudioContentDescription*>(
1315 audio_content->description);
1316 EXPECT_TRUE(
1317 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
1318
1319 const cricket::ContentInfo* video_content =
1320 cricket::GetFirstVideoContent(offer->description());
1321 const cricket::VideoContentDescription* video_desc =
1322 static_cast<const cricket::VideoContentDescription*>(
1323 video_content->description);
1324 EXPECT_TRUE(
1325 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1326
1327 // Add another stream and ensure the offer includes both the old and new
1328 // streams.
1329 AddAudioVideoStream(kStreamLabel2, "audio_track2", "video_track2");
kwiberg2bbff992016-03-16 11:03:04 -07001330 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07001331
1332 audio_content = cricket::GetFirstAudioContent(offer->description());
1333 audio_desc = static_cast<const cricket::AudioContentDescription*>(
1334 audio_content->description);
1335 EXPECT_TRUE(
1336 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
1337 EXPECT_TRUE(
1338 ContainsTrack(audio_desc->streams(), kStreamLabel2, "audio_track2"));
1339
1340 video_content = cricket::GetFirstVideoContent(offer->description());
1341 video_desc = static_cast<const cricket::VideoContentDescription*>(
1342 video_content->description);
1343 EXPECT_TRUE(
1344 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1345 EXPECT_TRUE(
1346 ContainsTrack(video_desc->streams(), kStreamLabel2, "video_track2"));
1347}
1348
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001349TEST_F(PeerConnectionInterfaceTest, RemoveStream) {
deadbeef293e9262017-01-11 12:28:30 -08001350 CreatePeerConnectionWithoutDtls();
deadbeefab9b2d12015-10-14 11:33:11 -07001351 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001352 ASSERT_EQ(1u, pc_->local_streams()->count());
1353 pc_->RemoveStream(pc_->local_streams()->at(0));
1354 EXPECT_EQ(0u, pc_->local_streams()->count());
1355}
1356
deadbeefe1f9d832016-01-14 15:35:42 -08001357// Test for AddTrack and RemoveTrack methods.
1358// Tests that the created offer includes tracks we added,
1359// and that the RtpSenders are created correctly.
1360// Also tests that RemoveTrack removes the tracks from subsequent offers.
1361TEST_F(PeerConnectionInterfaceTest, AddTrackRemoveTrack) {
deadbeef293e9262017-01-11 12:28:30 -08001362 CreatePeerConnectionWithoutDtls();
deadbeefe1f9d832016-01-14 15:35:42 -08001363 // Create a dummy stream, so tracks share a stream label.
zhihuang9763d562016-08-05 11:14:50 -07001364 rtc::scoped_refptr<MediaStreamInterface> stream(
deadbeefe1f9d832016-01-14 15:35:42 -08001365 pc_factory_->CreateLocalMediaStream(kStreamLabel1));
1366 std::vector<MediaStreamInterface*> stream_list;
1367 stream_list.push_back(stream.get());
zhihuang9763d562016-08-05 11:14:50 -07001368 rtc::scoped_refptr<AudioTrackInterface> audio_track(
deadbeefe1f9d832016-01-14 15:35:42 -08001369 pc_factory_->CreateAudioTrack("audio_track", nullptr));
zhihuang9763d562016-08-05 11:14:50 -07001370 rtc::scoped_refptr<VideoTrackInterface> video_track(
1371 pc_factory_->CreateVideoTrack(
deadbeef112b2e92017-02-10 20:13:37 -08001372 "video_track", pc_factory_->CreateVideoSource(
1373 std::unique_ptr<cricket::VideoCapturer>(
1374 new cricket::FakeVideoCapturer()))));
deadbeefe1f9d832016-01-14 15:35:42 -08001375 auto audio_sender = pc_->AddTrack(audio_track, stream_list);
1376 auto video_sender = pc_->AddTrack(video_track, stream_list);
deadbeefa601f5c2016-06-06 14:27:39 -07001377 EXPECT_EQ(1UL, audio_sender->stream_ids().size());
1378 EXPECT_EQ(kStreamLabel1, audio_sender->stream_ids()[0]);
deadbeefe1f9d832016-01-14 15:35:42 -08001379 EXPECT_EQ("audio_track", audio_sender->id());
1380 EXPECT_EQ(audio_track, audio_sender->track());
deadbeefa601f5c2016-06-06 14:27:39 -07001381 EXPECT_EQ(1UL, video_sender->stream_ids().size());
1382 EXPECT_EQ(kStreamLabel1, video_sender->stream_ids()[0]);
deadbeefe1f9d832016-01-14 15:35:42 -08001383 EXPECT_EQ("video_track", video_sender->id());
1384 EXPECT_EQ(video_track, video_sender->track());
1385
1386 // Now create an offer and check for the senders.
kwibergd1fe2812016-04-27 06:47:29 -07001387 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001388 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefe1f9d832016-01-14 15:35:42 -08001389
1390 const cricket::ContentInfo* audio_content =
1391 cricket::GetFirstAudioContent(offer->description());
1392 const cricket::AudioContentDescription* audio_desc =
1393 static_cast<const cricket::AudioContentDescription*>(
1394 audio_content->description);
1395 EXPECT_TRUE(
1396 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
1397
1398 const cricket::ContentInfo* video_content =
1399 cricket::GetFirstVideoContent(offer->description());
1400 const cricket::VideoContentDescription* video_desc =
1401 static_cast<const cricket::VideoContentDescription*>(
1402 video_content->description);
1403 EXPECT_TRUE(
1404 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1405
1406 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
1407
1408 // Now try removing the tracks.
1409 EXPECT_TRUE(pc_->RemoveTrack(audio_sender));
1410 EXPECT_TRUE(pc_->RemoveTrack(video_sender));
1411
1412 // Create a new offer and ensure it doesn't contain the removed senders.
kwiberg2bbff992016-03-16 11:03:04 -07001413 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefe1f9d832016-01-14 15:35:42 -08001414
1415 audio_content = cricket::GetFirstAudioContent(offer->description());
1416 audio_desc = static_cast<const cricket::AudioContentDescription*>(
1417 audio_content->description);
1418 EXPECT_FALSE(
1419 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
1420
1421 video_content = cricket::GetFirstVideoContent(offer->description());
1422 video_desc = static_cast<const cricket::VideoContentDescription*>(
1423 video_content->description);
1424 EXPECT_FALSE(
1425 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1426
1427 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
1428
1429 // Calling RemoveTrack on a sender no longer attached to a PeerConnection
1430 // should return false.
1431 EXPECT_FALSE(pc_->RemoveTrack(audio_sender));
1432 EXPECT_FALSE(pc_->RemoveTrack(video_sender));
1433}
1434
1435// Test creating senders without a stream specified,
1436// expecting a random stream ID to be generated.
1437TEST_F(PeerConnectionInterfaceTest, AddTrackWithoutStream) {
deadbeef293e9262017-01-11 12:28:30 -08001438 CreatePeerConnectionWithoutDtls();
deadbeefe1f9d832016-01-14 15:35:42 -08001439 // Create a dummy stream, so tracks share a stream label.
zhihuang9763d562016-08-05 11:14:50 -07001440 rtc::scoped_refptr<AudioTrackInterface> audio_track(
deadbeefe1f9d832016-01-14 15:35:42 -08001441 pc_factory_->CreateAudioTrack("audio_track", nullptr));
zhihuang9763d562016-08-05 11:14:50 -07001442 rtc::scoped_refptr<VideoTrackInterface> video_track(
1443 pc_factory_->CreateVideoTrack(
deadbeef112b2e92017-02-10 20:13:37 -08001444 "video_track", pc_factory_->CreateVideoSource(
1445 std::unique_ptr<cricket::VideoCapturer>(
1446 new cricket::FakeVideoCapturer()))));
deadbeefe1f9d832016-01-14 15:35:42 -08001447 auto audio_sender =
1448 pc_->AddTrack(audio_track, std::vector<MediaStreamInterface*>());
1449 auto video_sender =
1450 pc_->AddTrack(video_track, std::vector<MediaStreamInterface*>());
1451 EXPECT_EQ("audio_track", audio_sender->id());
1452 EXPECT_EQ(audio_track, audio_sender->track());
1453 EXPECT_EQ("video_track", video_sender->id());
1454 EXPECT_EQ(video_track, video_sender->track());
1455 // If the ID is truly a random GUID, it should be infinitely unlikely they
1456 // will be the same.
deadbeefa601f5c2016-06-06 14:27:39 -07001457 EXPECT_NE(video_sender->stream_ids(), audio_sender->stream_ids());
deadbeefe1f9d832016-01-14 15:35:42 -08001458}
1459
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001460TEST_F(PeerConnectionInterfaceTest, CreateOfferReceiveAnswer) {
1461 InitiateCall();
1462 WaitAndVerifyOnAddStream(kStreamLabel1);
1463 VerifyRemoteRtpHeaderExtensions();
1464}
1465
1466TEST_F(PeerConnectionInterfaceTest, CreateOfferReceivePrAnswerAndAnswer) {
deadbeef293e9262017-01-11 12:28:30 -08001467 CreatePeerConnectionWithoutDtls();
deadbeefab9b2d12015-10-14 11:33:11 -07001468 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001469 CreateOfferAsLocalDescription();
1470 std::string offer;
1471 EXPECT_TRUE(pc_->local_description()->ToString(&offer));
1472 CreatePrAnswerAndAnswerAsRemoteDescription(offer);
1473 WaitAndVerifyOnAddStream(kStreamLabel1);
1474}
1475
1476TEST_F(PeerConnectionInterfaceTest, ReceiveOfferCreateAnswer) {
deadbeef293e9262017-01-11 12:28:30 -08001477 CreatePeerConnectionWithoutDtls();
deadbeefab9b2d12015-10-14 11:33:11 -07001478 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001479
1480 CreateOfferAsRemoteDescription();
1481 CreateAnswerAsLocalDescription();
1482
1483 WaitAndVerifyOnAddStream(kStreamLabel1);
1484}
1485
1486TEST_F(PeerConnectionInterfaceTest, ReceiveOfferCreatePrAnswerAndAnswer) {
deadbeef293e9262017-01-11 12:28:30 -08001487 CreatePeerConnectionWithoutDtls();
deadbeefab9b2d12015-10-14 11:33:11 -07001488 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001489
1490 CreateOfferAsRemoteDescription();
1491 CreatePrAnswerAsLocalDescription();
1492 CreateAnswerAsLocalDescription();
1493
1494 WaitAndVerifyOnAddStream(kStreamLabel1);
1495}
1496
1497TEST_F(PeerConnectionInterfaceTest, Renegotiate) {
1498 InitiateCall();
1499 ASSERT_EQ(1u, pc_->remote_streams()->count());
1500 pc_->RemoveStream(pc_->local_streams()->at(0));
1501 CreateOfferReceiveAnswer();
1502 EXPECT_EQ(0u, pc_->remote_streams()->count());
deadbeefab9b2d12015-10-14 11:33:11 -07001503 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001504 CreateOfferReceiveAnswer();
1505}
1506
1507// Tests that after negotiating an audio only call, the respondent can perform a
1508// renegotiation that removes the audio stream.
1509TEST_F(PeerConnectionInterfaceTest, RenegotiateAudioOnly) {
deadbeef293e9262017-01-11 12:28:30 -08001510 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001511 AddVoiceStream(kStreamLabel1);
1512 CreateOfferAsRemoteDescription();
1513 CreateAnswerAsLocalDescription();
1514
1515 ASSERT_EQ(1u, pc_->remote_streams()->count());
1516 pc_->RemoveStream(pc_->local_streams()->at(0));
1517 CreateOfferReceiveAnswer();
1518 EXPECT_EQ(0u, pc_->remote_streams()->count());
1519}
1520
1521// Test that candidates are generated and that we can parse our own candidates.
1522TEST_F(PeerConnectionInterfaceTest, IceCandidates) {
deadbeef293e9262017-01-11 12:28:30 -08001523 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001524
1525 EXPECT_FALSE(pc_->AddIceCandidate(observer_.last_candidate_.get()));
1526 // SetRemoteDescription takes ownership of offer.
kwibergd1fe2812016-04-27 06:47:29 -07001527 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefab9b2d12015-10-14 11:33:11 -07001528 AddVideoStream(kStreamLabel1);
deadbeefc80741f2015-10-22 13:14:45 -07001529 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
kwiberg2bbff992016-03-16 11:03:04 -07001530 EXPECT_TRUE(DoSetRemoteDescription(offer.release()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001531
1532 // SetLocalDescription takes ownership of answer.
kwibergd1fe2812016-04-27 06:47:29 -07001533 std::unique_ptr<SessionDescriptionInterface> answer;
deadbeefc80741f2015-10-22 13:14:45 -07001534 EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
kwiberg2bbff992016-03-16 11:03:04 -07001535 EXPECT_TRUE(DoSetLocalDescription(answer.release()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001536
1537 EXPECT_TRUE_WAIT(observer_.last_candidate_.get() != NULL, kTimeout);
1538 EXPECT_TRUE_WAIT(observer_.ice_complete_, kTimeout);
1539
1540 EXPECT_TRUE(pc_->AddIceCandidate(observer_.last_candidate_.get()));
1541}
1542
deadbeefab9b2d12015-10-14 11:33:11 -07001543// Test that CreateOffer and CreateAnswer will fail if the track labels are
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001544// not unique.
1545TEST_F(PeerConnectionInterfaceTest, CreateOfferAnswerWithInvalidStream) {
deadbeef293e9262017-01-11 12:28:30 -08001546 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001547 // Create a regular offer for the CreateAnswer test later.
kwibergd1fe2812016-04-27 06:47:29 -07001548 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefc80741f2015-10-22 13:14:45 -07001549 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
kwiberg2bbff992016-03-16 11:03:04 -07001550 EXPECT_TRUE(offer);
1551 offer.reset();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001552
1553 // Create a local stream with audio&video tracks having same label.
1554 AddAudioVideoStream(kStreamLabel1, "track_label", "track_label");
1555
1556 // Test CreateOffer
deadbeefc80741f2015-10-22 13:14:45 -07001557 EXPECT_FALSE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001558
1559 // Test CreateAnswer
kwibergd1fe2812016-04-27 06:47:29 -07001560 std::unique_ptr<SessionDescriptionInterface> answer;
deadbeefc80741f2015-10-22 13:14:45 -07001561 EXPECT_FALSE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001562}
1563
1564// Test that we will get different SSRCs for each tracks in the offer and answer
1565// we created.
1566TEST_F(PeerConnectionInterfaceTest, SsrcInOfferAnswer) {
deadbeef293e9262017-01-11 12:28:30 -08001567 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001568 // Create a local stream with audio&video tracks having different labels.
1569 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
1570
1571 // Test CreateOffer
kwibergd1fe2812016-04-27 06:47:29 -07001572 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001573 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001574 int audio_ssrc = 0;
1575 int video_ssrc = 0;
1576 EXPECT_TRUE(GetFirstSsrc(GetFirstAudioContent(offer->description()),
1577 &audio_ssrc));
1578 EXPECT_TRUE(GetFirstSsrc(GetFirstVideoContent(offer->description()),
1579 &video_ssrc));
1580 EXPECT_NE(audio_ssrc, video_ssrc);
1581
1582 // Test CreateAnswer
1583 EXPECT_TRUE(DoSetRemoteDescription(offer.release()));
kwibergd1fe2812016-04-27 06:47:29 -07001584 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -07001585 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001586 audio_ssrc = 0;
1587 video_ssrc = 0;
1588 EXPECT_TRUE(GetFirstSsrc(GetFirstAudioContent(answer->description()),
1589 &audio_ssrc));
1590 EXPECT_TRUE(GetFirstSsrc(GetFirstVideoContent(answer->description()),
1591 &video_ssrc));
1592 EXPECT_NE(audio_ssrc, video_ssrc);
1593}
1594
deadbeefeb459812015-12-15 19:24:43 -08001595// Test that it's possible to call AddTrack on a MediaStream after adding
1596// the stream to a PeerConnection.
1597// TODO(deadbeef): Remove this test once this behavior is no longer supported.
1598TEST_F(PeerConnectionInterfaceTest, AddTrackAfterAddStream) {
deadbeef293e9262017-01-11 12:28:30 -08001599 CreatePeerConnectionWithoutDtls();
deadbeefeb459812015-12-15 19:24:43 -08001600 // Create audio stream and add to PeerConnection.
1601 AddVoiceStream(kStreamLabel1);
1602 MediaStreamInterface* stream = pc_->local_streams()->at(0);
1603
1604 // Add video track to the audio-only stream.
zhihuang9763d562016-08-05 11:14:50 -07001605 rtc::scoped_refptr<VideoTrackInterface> video_track(
1606 pc_factory_->CreateVideoTrack(
deadbeef112b2e92017-02-10 20:13:37 -08001607 "video_label", pc_factory_->CreateVideoSource(
1608 std::unique_ptr<cricket::VideoCapturer>(
1609 new cricket::FakeVideoCapturer()))));
deadbeefeb459812015-12-15 19:24:43 -08001610 stream->AddTrack(video_track.get());
1611
kwibergd1fe2812016-04-27 06:47:29 -07001612 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001613 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefeb459812015-12-15 19:24:43 -08001614
1615 const cricket::MediaContentDescription* video_desc =
1616 cricket::GetFirstVideoContentDescription(offer->description());
1617 EXPECT_TRUE(video_desc != nullptr);
1618}
1619
1620// Test that it's possible to call RemoveTrack on a MediaStream after adding
1621// the stream to a PeerConnection.
1622// TODO(deadbeef): Remove this test once this behavior is no longer supported.
1623TEST_F(PeerConnectionInterfaceTest, RemoveTrackAfterAddStream) {
deadbeef293e9262017-01-11 12:28:30 -08001624 CreatePeerConnectionWithoutDtls();
deadbeefeb459812015-12-15 19:24:43 -08001625 // Create audio/video stream and add to PeerConnection.
1626 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
1627 MediaStreamInterface* stream = pc_->local_streams()->at(0);
1628
1629 // Remove the video track.
1630 stream->RemoveTrack(stream->GetVideoTracks()[0]);
1631
kwibergd1fe2812016-04-27 06:47:29 -07001632 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001633 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefeb459812015-12-15 19:24:43 -08001634
1635 const cricket::MediaContentDescription* video_desc =
1636 cricket::GetFirstVideoContentDescription(offer->description());
1637 EXPECT_TRUE(video_desc == nullptr);
1638}
1639
deadbeef1dcb1642017-03-29 21:08:16 -07001640// Verify that CreateDtmfSender only succeeds if called with a valid local
1641// track. Other aspects of DtmfSenders are tested in
1642// peerconnection_integrationtest.cc.
1643TEST_F(PeerConnectionInterfaceTest, CreateDtmfSenderWithInvalidParams) {
1644 CreatePeerConnection();
1645 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
1646 EXPECT_EQ(nullptr, pc_->CreateDtmfSender(nullptr));
1647 rtc::scoped_refptr<webrtc::AudioTrackInterface> non_localtrack(
1648 pc_factory_->CreateAudioTrack("dummy_track", nullptr));
1649 EXPECT_EQ(nullptr, pc_->CreateDtmfSender(non_localtrack));
1650}
1651
deadbeefbd7d8f72015-12-18 16:58:44 -08001652// Test creating a sender with a stream ID, and ensure the ID is populated
1653// in the offer.
1654TEST_F(PeerConnectionInterfaceTest, CreateSenderWithStream) {
deadbeef293e9262017-01-11 12:28:30 -08001655 CreatePeerConnectionWithoutDtls();
deadbeefbd7d8f72015-12-18 16:58:44 -08001656 pc_->CreateSender("video", kStreamLabel1);
1657
kwibergd1fe2812016-04-27 06:47:29 -07001658 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001659 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefbd7d8f72015-12-18 16:58:44 -08001660
1661 const cricket::MediaContentDescription* video_desc =
1662 cricket::GetFirstVideoContentDescription(offer->description());
1663 ASSERT_TRUE(video_desc != nullptr);
1664 ASSERT_EQ(1u, video_desc->streams().size());
1665 EXPECT_EQ(kStreamLabel1, video_desc->streams()[0].sync_label);
1666}
1667
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001668// Test that we can specify a certain track that we want statistics about.
1669TEST_F(PeerConnectionInterfaceTest, GetStatsForSpecificTrack) {
1670 InitiateCall();
1671 ASSERT_LT(0u, pc_->remote_streams()->count());
1672 ASSERT_LT(0u, pc_->remote_streams()->at(0)->GetAudioTracks().size());
zhihuang9763d562016-08-05 11:14:50 -07001673 rtc::scoped_refptr<MediaStreamTrackInterface> remote_audio =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001674 pc_->remote_streams()->at(0)->GetAudioTracks()[0];
1675 EXPECT_TRUE(DoGetStats(remote_audio));
1676
1677 // Remove the stream. Since we are sending to our selves the local
1678 // and the remote stream is the same.
1679 pc_->RemoveStream(pc_->local_streams()->at(0));
1680 // Do a re-negotiation.
1681 CreateOfferReceiveAnswer();
1682
1683 ASSERT_EQ(0u, pc_->remote_streams()->count());
1684
1685 // Test that we still can get statistics for the old track. Even if it is not
1686 // sent any longer.
1687 EXPECT_TRUE(DoGetStats(remote_audio));
1688}
1689
1690// Test that we can get stats on a video track.
1691TEST_F(PeerConnectionInterfaceTest, GetStatsForVideoTrack) {
1692 InitiateCall();
1693 ASSERT_LT(0u, pc_->remote_streams()->count());
1694 ASSERT_LT(0u, pc_->remote_streams()->at(0)->GetVideoTracks().size());
zhihuang9763d562016-08-05 11:14:50 -07001695 rtc::scoped_refptr<MediaStreamTrackInterface> remote_video =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001696 pc_->remote_streams()->at(0)->GetVideoTracks()[0];
1697 EXPECT_TRUE(DoGetStats(remote_video));
1698}
1699
1700// Test that we don't get statistics for an invalid track.
zhihuange9e94c32016-11-04 11:38:15 -07001701TEST_F(PeerConnectionInterfaceTest, GetStatsForInvalidTrack) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001702 InitiateCall();
zhihuang9763d562016-08-05 11:14:50 -07001703 rtc::scoped_refptr<AudioTrackInterface> unknown_audio_track(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001704 pc_factory_->CreateAudioTrack("unknown track", NULL));
1705 EXPECT_FALSE(DoGetStats(unknown_audio_track));
1706}
1707
1708// This test setup two RTP data channels in loop back.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001709TEST_F(PeerConnectionInterfaceTest, TestDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001710 FakeConstraints constraints;
1711 constraints.SetAllowRtpDataChannels();
1712 CreatePeerConnection(&constraints);
zhihuang9763d562016-08-05 11:14:50 -07001713 rtc::scoped_refptr<DataChannelInterface> data1 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001714 pc_->CreateDataChannel("test1", NULL);
zhihuang9763d562016-08-05 11:14:50 -07001715 rtc::scoped_refptr<DataChannelInterface> data2 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001716 pc_->CreateDataChannel("test2", NULL);
1717 ASSERT_TRUE(data1 != NULL);
kwibergd1fe2812016-04-27 06:47:29 -07001718 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001719 new MockDataChannelObserver(data1));
kwibergd1fe2812016-04-27 06:47:29 -07001720 std::unique_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001721 new MockDataChannelObserver(data2));
1722
1723 EXPECT_EQ(DataChannelInterface::kConnecting, data1->state());
1724 EXPECT_EQ(DataChannelInterface::kConnecting, data2->state());
1725 std::string data_to_send1 = "testing testing";
1726 std::string data_to_send2 = "testing something else";
1727 EXPECT_FALSE(data1->Send(DataBuffer(data_to_send1)));
1728
1729 CreateOfferReceiveAnswer();
1730 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1731 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
1732
1733 EXPECT_EQ(DataChannelInterface::kOpen, data1->state());
1734 EXPECT_EQ(DataChannelInterface::kOpen, data2->state());
1735 EXPECT_TRUE(data1->Send(DataBuffer(data_to_send1)));
1736 EXPECT_TRUE(data2->Send(DataBuffer(data_to_send2)));
1737
1738 EXPECT_EQ_WAIT(data_to_send1, observer1->last_message(), kTimeout);
1739 EXPECT_EQ_WAIT(data_to_send2, observer2->last_message(), kTimeout);
1740
1741 data1->Close();
1742 EXPECT_EQ(DataChannelInterface::kClosing, data1->state());
1743 CreateOfferReceiveAnswer();
1744 EXPECT_FALSE(observer1->IsOpen());
1745 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
1746 EXPECT_TRUE(observer2->IsOpen());
1747
1748 data_to_send2 = "testing something else again";
1749 EXPECT_TRUE(data2->Send(DataBuffer(data_to_send2)));
1750
1751 EXPECT_EQ_WAIT(data_to_send2, observer2->last_message(), kTimeout);
1752}
1753
1754// This test verifies that sendnig binary data over RTP data channels should
1755// fail.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001756TEST_F(PeerConnectionInterfaceTest, TestSendBinaryOnRtpDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001757 FakeConstraints constraints;
1758 constraints.SetAllowRtpDataChannels();
1759 CreatePeerConnection(&constraints);
zhihuang9763d562016-08-05 11:14:50 -07001760 rtc::scoped_refptr<DataChannelInterface> data1 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001761 pc_->CreateDataChannel("test1", NULL);
zhihuang9763d562016-08-05 11:14:50 -07001762 rtc::scoped_refptr<DataChannelInterface> data2 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001763 pc_->CreateDataChannel("test2", NULL);
1764 ASSERT_TRUE(data1 != NULL);
kwibergd1fe2812016-04-27 06:47:29 -07001765 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001766 new MockDataChannelObserver(data1));
kwibergd1fe2812016-04-27 06:47:29 -07001767 std::unique_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001768 new MockDataChannelObserver(data2));
1769
1770 EXPECT_EQ(DataChannelInterface::kConnecting, data1->state());
1771 EXPECT_EQ(DataChannelInterface::kConnecting, data2->state());
1772
1773 CreateOfferReceiveAnswer();
1774 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1775 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
1776
1777 EXPECT_EQ(DataChannelInterface::kOpen, data1->state());
1778 EXPECT_EQ(DataChannelInterface::kOpen, data2->state());
1779
jbaucheec21bd2016-03-20 06:15:43 -07001780 rtc::CopyOnWriteBuffer buffer("test", 4);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001781 EXPECT_FALSE(data1->Send(DataBuffer(buffer, true)));
1782}
1783
1784// This test setup a RTP data channels in loop back and test that a channel is
1785// opened even if the remote end answer with a zero SSRC.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001786TEST_F(PeerConnectionInterfaceTest, TestSendOnlyDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001787 FakeConstraints constraints;
1788 constraints.SetAllowRtpDataChannels();
1789 CreatePeerConnection(&constraints);
zhihuang9763d562016-08-05 11:14:50 -07001790 rtc::scoped_refptr<DataChannelInterface> data1 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001791 pc_->CreateDataChannel("test1", NULL);
kwibergd1fe2812016-04-27 06:47:29 -07001792 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001793 new MockDataChannelObserver(data1));
1794
1795 CreateOfferReceiveAnswerWithoutSsrc();
1796
1797 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1798
1799 data1->Close();
1800 EXPECT_EQ(DataChannelInterface::kClosing, data1->state());
1801 CreateOfferReceiveAnswerWithoutSsrc();
1802 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
1803 EXPECT_FALSE(observer1->IsOpen());
1804}
1805
1806// This test that if a data channel is added in an answer a receive only channel
1807// channel is created.
1808TEST_F(PeerConnectionInterfaceTest, TestReceiveOnlyDataChannel) {
1809 FakeConstraints constraints;
1810 constraints.SetAllowRtpDataChannels();
1811 CreatePeerConnection(&constraints);
1812
1813 std::string offer_label = "offer_channel";
zhihuang9763d562016-08-05 11:14:50 -07001814 rtc::scoped_refptr<DataChannelInterface> offer_channel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001815 pc_->CreateDataChannel(offer_label, NULL);
1816
1817 CreateOfferAsLocalDescription();
1818
1819 // Replace the data channel label in the offer and apply it as an answer.
1820 std::string receive_label = "answer_channel";
1821 std::string sdp;
1822 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001823 rtc::replace_substrs(offer_label.c_str(), offer_label.length(),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001824 receive_label.c_str(), receive_label.length(),
1825 &sdp);
1826 CreateAnswerAsRemoteDescription(sdp);
1827
1828 // Verify that a new incoming data channel has been created and that
1829 // it is open but can't we written to.
1830 ASSERT_TRUE(observer_.last_datachannel_ != NULL);
1831 DataChannelInterface* received_channel = observer_.last_datachannel_;
1832 EXPECT_EQ(DataChannelInterface::kConnecting, received_channel->state());
1833 EXPECT_EQ(receive_label, received_channel->label());
1834 EXPECT_FALSE(received_channel->Send(DataBuffer("something")));
1835
1836 // Verify that the channel we initially offered has been rejected.
1837 EXPECT_EQ(DataChannelInterface::kClosed, offer_channel->state());
1838
1839 // Do another offer / answer exchange and verify that the data channel is
1840 // opened.
1841 CreateOfferReceiveAnswer();
1842 EXPECT_EQ_WAIT(DataChannelInterface::kOpen, received_channel->state(),
1843 kTimeout);
1844}
1845
1846// This test that no data channel is returned if a reliable channel is
1847// requested.
1848// TODO(perkj): Remove this test once reliable channels are implemented.
1849TEST_F(PeerConnectionInterfaceTest, CreateReliableRtpDataChannelShouldFail) {
1850 FakeConstraints constraints;
1851 constraints.SetAllowRtpDataChannels();
1852 CreatePeerConnection(&constraints);
1853
1854 std::string label = "test";
1855 webrtc::DataChannelInit config;
1856 config.reliable = true;
zhihuang9763d562016-08-05 11:14:50 -07001857 rtc::scoped_refptr<DataChannelInterface> channel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001858 pc_->CreateDataChannel(label, &config);
1859 EXPECT_TRUE(channel == NULL);
1860}
1861
deadbeefab9b2d12015-10-14 11:33:11 -07001862// Verifies that duplicated label is not allowed for RTP data channel.
1863TEST_F(PeerConnectionInterfaceTest, RtpDuplicatedLabelNotAllowed) {
1864 FakeConstraints constraints;
1865 constraints.SetAllowRtpDataChannels();
1866 CreatePeerConnection(&constraints);
1867
1868 std::string label = "test";
zhihuang9763d562016-08-05 11:14:50 -07001869 rtc::scoped_refptr<DataChannelInterface> channel =
deadbeefab9b2d12015-10-14 11:33:11 -07001870 pc_->CreateDataChannel(label, nullptr);
1871 EXPECT_NE(channel, nullptr);
1872
zhihuang9763d562016-08-05 11:14:50 -07001873 rtc::scoped_refptr<DataChannelInterface> dup_channel =
deadbeefab9b2d12015-10-14 11:33:11 -07001874 pc_->CreateDataChannel(label, nullptr);
1875 EXPECT_EQ(dup_channel, nullptr);
1876}
1877
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001878// This tests that a SCTP data channel is returned using different
1879// DataChannelInit configurations.
1880TEST_F(PeerConnectionInterfaceTest, CreateSctpDataChannel) {
1881 FakeConstraints constraints;
1882 constraints.SetAllowDtlsSctpDataChannels();
1883 CreatePeerConnection(&constraints);
1884
1885 webrtc::DataChannelInit config;
1886
zhihuang9763d562016-08-05 11:14:50 -07001887 rtc::scoped_refptr<DataChannelInterface> channel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001888 pc_->CreateDataChannel("1", &config);
1889 EXPECT_TRUE(channel != NULL);
1890 EXPECT_TRUE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001891 EXPECT_TRUE(observer_.renegotiation_needed_);
1892 observer_.renegotiation_needed_ = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001893
1894 config.ordered = false;
1895 channel = pc_->CreateDataChannel("2", &config);
1896 EXPECT_TRUE(channel != NULL);
1897 EXPECT_TRUE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001898 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001899
1900 config.ordered = true;
1901 config.maxRetransmits = 0;
1902 channel = pc_->CreateDataChannel("3", &config);
1903 EXPECT_TRUE(channel != NULL);
1904 EXPECT_FALSE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001905 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001906
1907 config.maxRetransmits = -1;
1908 config.maxRetransmitTime = 0;
1909 channel = pc_->CreateDataChannel("4", &config);
1910 EXPECT_TRUE(channel != NULL);
1911 EXPECT_FALSE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001912 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001913}
1914
1915// This tests that no data channel is returned if both maxRetransmits and
1916// maxRetransmitTime are set for SCTP data channels.
1917TEST_F(PeerConnectionInterfaceTest,
1918 CreateSctpDataChannelShouldFailForInvalidConfig) {
1919 FakeConstraints constraints;
1920 constraints.SetAllowDtlsSctpDataChannels();
1921 CreatePeerConnection(&constraints);
1922
1923 std::string label = "test";
1924 webrtc::DataChannelInit config;
1925 config.maxRetransmits = 0;
1926 config.maxRetransmitTime = 0;
1927
zhihuang9763d562016-08-05 11:14:50 -07001928 rtc::scoped_refptr<DataChannelInterface> channel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001929 pc_->CreateDataChannel(label, &config);
1930 EXPECT_TRUE(channel == NULL);
1931}
1932
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001933// The test verifies that creating a SCTP data channel with an id already in use
1934// or out of range should fail.
1935TEST_F(PeerConnectionInterfaceTest,
1936 CreateSctpDataChannelWithInvalidIdShouldFail) {
1937 FakeConstraints constraints;
1938 constraints.SetAllowDtlsSctpDataChannels();
1939 CreatePeerConnection(&constraints);
1940
1941 webrtc::DataChannelInit config;
zhihuang9763d562016-08-05 11:14:50 -07001942 rtc::scoped_refptr<DataChannelInterface> channel;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001943
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001944 config.id = 1;
1945 channel = pc_->CreateDataChannel("1", &config);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001946 EXPECT_TRUE(channel != NULL);
1947 EXPECT_EQ(1, channel->id());
1948
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001949 channel = pc_->CreateDataChannel("x", &config);
1950 EXPECT_TRUE(channel == NULL);
1951
1952 config.id = cricket::kMaxSctpSid;
1953 channel = pc_->CreateDataChannel("max", &config);
1954 EXPECT_TRUE(channel != NULL);
1955 EXPECT_EQ(config.id, channel->id());
1956
1957 config.id = cricket::kMaxSctpSid + 1;
1958 channel = pc_->CreateDataChannel("x", &config);
1959 EXPECT_TRUE(channel == NULL);
1960}
1961
deadbeefab9b2d12015-10-14 11:33:11 -07001962// Verifies that duplicated label is allowed for SCTP data channel.
1963TEST_F(PeerConnectionInterfaceTest, SctpDuplicatedLabelAllowed) {
1964 FakeConstraints constraints;
1965 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
1966 true);
1967 CreatePeerConnection(&constraints);
1968
1969 std::string label = "test";
zhihuang9763d562016-08-05 11:14:50 -07001970 rtc::scoped_refptr<DataChannelInterface> channel =
deadbeefab9b2d12015-10-14 11:33:11 -07001971 pc_->CreateDataChannel(label, nullptr);
1972 EXPECT_NE(channel, nullptr);
1973
zhihuang9763d562016-08-05 11:14:50 -07001974 rtc::scoped_refptr<DataChannelInterface> dup_channel =
deadbeefab9b2d12015-10-14 11:33:11 -07001975 pc_->CreateDataChannel(label, nullptr);
1976 EXPECT_NE(dup_channel, nullptr);
1977}
1978
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001979// This test verifies that OnRenegotiationNeeded is fired for every new RTP
1980// DataChannel.
1981TEST_F(PeerConnectionInterfaceTest, RenegotiationNeededForNewRtpDataChannel) {
1982 FakeConstraints constraints;
1983 constraints.SetAllowRtpDataChannels();
1984 CreatePeerConnection(&constraints);
1985
zhihuang9763d562016-08-05 11:14:50 -07001986 rtc::scoped_refptr<DataChannelInterface> dc1 =
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001987 pc_->CreateDataChannel("test1", NULL);
1988 EXPECT_TRUE(observer_.renegotiation_needed_);
1989 observer_.renegotiation_needed_ = false;
1990
zhihuang9763d562016-08-05 11:14:50 -07001991 rtc::scoped_refptr<DataChannelInterface> dc2 =
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001992 pc_->CreateDataChannel("test2", NULL);
1993 EXPECT_TRUE(observer_.renegotiation_needed_);
1994}
1995
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001996// This test that a data channel closes when a PeerConnection is deleted/closed.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001997TEST_F(PeerConnectionInterfaceTest, DataChannelCloseWhenPeerConnectionClose) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001998 FakeConstraints constraints;
1999 constraints.SetAllowRtpDataChannels();
2000 CreatePeerConnection(&constraints);
2001
zhihuang9763d562016-08-05 11:14:50 -07002002 rtc::scoped_refptr<DataChannelInterface> data1 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002003 pc_->CreateDataChannel("test1", NULL);
zhihuang9763d562016-08-05 11:14:50 -07002004 rtc::scoped_refptr<DataChannelInterface> data2 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002005 pc_->CreateDataChannel("test2", NULL);
2006 ASSERT_TRUE(data1 != NULL);
kwibergd1fe2812016-04-27 06:47:29 -07002007 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002008 new MockDataChannelObserver(data1));
kwibergd1fe2812016-04-27 06:47:29 -07002009 std::unique_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002010 new MockDataChannelObserver(data2));
2011
2012 CreateOfferReceiveAnswer();
2013 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
2014 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
2015
2016 ReleasePeerConnection();
2017 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
2018 EXPECT_EQ(DataChannelInterface::kClosed, data2->state());
2019}
2020
2021// This test that data channels can be rejected in an answer.
2022TEST_F(PeerConnectionInterfaceTest, TestRejectDataChannelInAnswer) {
2023 FakeConstraints constraints;
2024 constraints.SetAllowRtpDataChannels();
2025 CreatePeerConnection(&constraints);
2026
zhihuang9763d562016-08-05 11:14:50 -07002027 rtc::scoped_refptr<DataChannelInterface> offer_channel(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002028 pc_->CreateDataChannel("offer_channel", NULL));
2029
2030 CreateOfferAsLocalDescription();
2031
2032 // Create an answer where the m-line for data channels are rejected.
2033 std::string sdp;
2034 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
2035 webrtc::JsepSessionDescription* answer = new webrtc::JsepSessionDescription(
2036 SessionDescriptionInterface::kAnswer);
2037 EXPECT_TRUE(answer->Initialize(sdp, NULL));
2038 cricket::ContentInfo* data_info =
2039 answer->description()->GetContentByName("data");
2040 data_info->rejected = true;
2041
2042 DoSetRemoteDescription(answer);
2043 EXPECT_EQ(DataChannelInterface::kClosed, offer_channel->state());
2044}
2045
2046// Test that we can create a session description from an SDP string from
2047// FireFox, use it as a remote session description, generate an answer and use
2048// the answer as a local description.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002049TEST_F(PeerConnectionInterfaceTest, ReceiveFireFoxOffer) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002050 FakeConstraints constraints;
2051 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2052 true);
2053 CreatePeerConnection(&constraints);
2054 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
2055 SessionDescriptionInterface* desc =
2056 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
jbauchfabe2c92015-07-16 13:43:14 -07002057 webrtc::kFireFoxSdpOffer, nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002058 EXPECT_TRUE(DoSetSessionDescription(desc, false));
2059 CreateAnswerAsLocalDescription();
2060 ASSERT_TRUE(pc_->local_description() != NULL);
2061 ASSERT_TRUE(pc_->remote_description() != NULL);
2062
2063 const cricket::ContentInfo* content =
2064 cricket::GetFirstAudioContent(pc_->local_description()->description());
2065 ASSERT_TRUE(content != NULL);
2066 EXPECT_FALSE(content->rejected);
2067
2068 content =
2069 cricket::GetFirstVideoContent(pc_->local_description()->description());
2070 ASSERT_TRUE(content != NULL);
2071 EXPECT_FALSE(content->rejected);
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00002072#ifdef HAVE_SCTP
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002073 content =
2074 cricket::GetFirstDataContent(pc_->local_description()->description());
2075 ASSERT_TRUE(content != NULL);
2076 EXPECT_TRUE(content->rejected);
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00002077#endif
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002078}
2079
deadbeef8662f942017-01-20 21:20:51 -08002080// Test that an offer can be received which offers DTLS with SDES fallback.
2081// Regression test for issue:
2082// https://bugs.chromium.org/p/webrtc/issues/detail?id=6972
2083TEST_F(PeerConnectionInterfaceTest, ReceiveDtlsSdesFallbackOffer) {
2084 FakeConstraints constraints;
2085 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2086 true);
2087 CreatePeerConnection(&constraints);
2088 // Wait for fake certificate to be generated. Previously, this is what caused
2089 // the "a=crypto" lines to be rejected.
2090 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
2091 ASSERT_NE(nullptr, fake_certificate_generator_);
2092 EXPECT_EQ_WAIT(1, fake_certificate_generator_->generated_certificates(),
2093 kTimeout);
2094 SessionDescriptionInterface* desc = webrtc::CreateSessionDescription(
2095 SessionDescriptionInterface::kOffer, kDtlsSdesFallbackSdp, nullptr);
2096 EXPECT_TRUE(DoSetSessionDescription(desc, false));
2097 CreateAnswerAsLocalDescription();
2098}
2099
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002100// Test that we can create an audio only offer and receive an answer with a
2101// limited set of audio codecs and receive an updated offer with more audio
2102// codecs, where the added codecs are not supported.
2103TEST_F(PeerConnectionInterfaceTest, ReceiveUpdatedAudioOfferWithBadCodecs) {
deadbeef293e9262017-01-11 12:28:30 -08002104 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002105 AddVoiceStream("audio_label");
2106 CreateOfferAsLocalDescription();
2107
2108 SessionDescriptionInterface* answer =
2109 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
jbauchfabe2c92015-07-16 13:43:14 -07002110 webrtc::kAudioSdp, nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002111 EXPECT_TRUE(DoSetSessionDescription(answer, false));
2112
2113 SessionDescriptionInterface* updated_offer =
2114 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
jbauchfabe2c92015-07-16 13:43:14 -07002115 webrtc::kAudioSdpWithUnsupportedCodecs,
2116 nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002117 EXPECT_TRUE(DoSetSessionDescription(updated_offer, false));
2118 CreateAnswerAsLocalDescription();
2119}
2120
deadbeefc80741f2015-10-22 13:14:45 -07002121// Test that if we're receiving (but not sending) a track, subsequent offers
2122// will have m-lines with a=recvonly.
2123TEST_F(PeerConnectionInterfaceTest, CreateSubsequentRecvOnlyOffer) {
2124 FakeConstraints constraints;
2125 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2126 true);
2127 CreatePeerConnection(&constraints);
2128 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2129 CreateAnswerAsLocalDescription();
2130
2131 // At this point we should be receiving stream 1, but not sending anything.
2132 // A new offer should be recvonly.
kwibergd1fe2812016-04-27 06:47:29 -07002133 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefc80741f2015-10-22 13:14:45 -07002134 DoCreateOffer(&offer, nullptr);
2135
2136 const cricket::ContentInfo* video_content =
2137 cricket::GetFirstVideoContent(offer->description());
2138 const cricket::VideoContentDescription* video_desc =
2139 static_cast<const cricket::VideoContentDescription*>(
2140 video_content->description);
2141 ASSERT_EQ(cricket::MD_RECVONLY, video_desc->direction());
2142
2143 const cricket::ContentInfo* audio_content =
2144 cricket::GetFirstAudioContent(offer->description());
2145 const cricket::AudioContentDescription* audio_desc =
2146 static_cast<const cricket::AudioContentDescription*>(
2147 audio_content->description);
2148 ASSERT_EQ(cricket::MD_RECVONLY, audio_desc->direction());
2149}
2150
2151// Test that if we're receiving (but not sending) a track, and the
2152// offerToReceiveVideo/offerToReceiveAudio constraints are explicitly set to
2153// false, the generated m-lines will be a=inactive.
2154TEST_F(PeerConnectionInterfaceTest, CreateSubsequentInactiveOffer) {
2155 FakeConstraints constraints;
2156 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2157 true);
2158 CreatePeerConnection(&constraints);
2159 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2160 CreateAnswerAsLocalDescription();
2161
2162 // At this point we should be receiving stream 1, but not sending anything.
2163 // A new offer would be recvonly, but we'll set the "no receive" constraints
2164 // to make it inactive.
kwibergd1fe2812016-04-27 06:47:29 -07002165 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefc80741f2015-10-22 13:14:45 -07002166 FakeConstraints offer_constraints;
2167 offer_constraints.AddMandatory(
2168 webrtc::MediaConstraintsInterface::kOfferToReceiveVideo, false);
2169 offer_constraints.AddMandatory(
2170 webrtc::MediaConstraintsInterface::kOfferToReceiveAudio, false);
2171 DoCreateOffer(&offer, &offer_constraints);
2172
2173 const cricket::ContentInfo* video_content =
2174 cricket::GetFirstVideoContent(offer->description());
2175 const cricket::VideoContentDescription* video_desc =
2176 static_cast<const cricket::VideoContentDescription*>(
2177 video_content->description);
2178 ASSERT_EQ(cricket::MD_INACTIVE, video_desc->direction());
2179
2180 const cricket::ContentInfo* audio_content =
2181 cricket::GetFirstAudioContent(offer->description());
2182 const cricket::AudioContentDescription* audio_desc =
2183 static_cast<const cricket::AudioContentDescription*>(
2184 audio_content->description);
2185 ASSERT_EQ(cricket::MD_INACTIVE, audio_desc->direction());
2186}
2187
deadbeef653b8e02015-11-11 12:55:10 -08002188// Test that we can use SetConfiguration to change the ICE servers of the
2189// PortAllocator.
2190TEST_F(PeerConnectionInterfaceTest, SetConfigurationChangesIceServers) {
2191 CreatePeerConnection();
2192
2193 PeerConnectionInterface::RTCConfiguration config;
2194 PeerConnectionInterface::IceServer server;
2195 server.uri = "stun:test_hostname";
2196 config.servers.push_back(server);
2197 EXPECT_TRUE(pc_->SetConfiguration(config));
2198
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002199 EXPECT_EQ(1u, port_allocator_->stun_servers().size());
2200 EXPECT_EQ("test_hostname",
2201 port_allocator_->stun_servers().begin()->hostname());
deadbeef653b8e02015-11-11 12:55:10 -08002202}
2203
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002204TEST_F(PeerConnectionInterfaceTest, SetConfigurationChangesCandidateFilter) {
2205 CreatePeerConnection();
2206 PeerConnectionInterface::RTCConfiguration config;
2207 config.type = PeerConnectionInterface::kRelay;
2208 EXPECT_TRUE(pc_->SetConfiguration(config));
2209 EXPECT_EQ(cricket::CF_RELAY, port_allocator_->candidate_filter());
2210}
2211
deadbeef293e9262017-01-11 12:28:30 -08002212TEST_F(PeerConnectionInterfaceTest, SetConfigurationChangesPruneTurnPortsFlag) {
2213 PeerConnectionInterface::RTCConfiguration config;
2214 config.prune_turn_ports = false;
2215 CreatePeerConnection(config, nullptr);
2216 EXPECT_FALSE(port_allocator_->prune_turn_ports());
2217
2218 config.prune_turn_ports = true;
2219 EXPECT_TRUE(pc_->SetConfiguration(config));
2220 EXPECT_TRUE(port_allocator_->prune_turn_ports());
2221}
2222
skvladd1f5fda2017-02-03 16:54:05 -08002223// Test that the ice check interval can be changed. This does not verify that
2224// the setting makes it all the way to P2PTransportChannel, as that would
2225// require a very complex set of mocks.
2226TEST_F(PeerConnectionInterfaceTest, SetConfigurationChangesIceCheckInterval) {
2227 PeerConnectionInterface::RTCConfiguration config;
2228 config.ice_check_min_interval = rtc::Optional<int>();
2229 CreatePeerConnection(config, nullptr);
2230 config.ice_check_min_interval = rtc::Optional<int>(100);
2231 EXPECT_TRUE(pc_->SetConfiguration(config));
2232 PeerConnectionInterface::RTCConfiguration new_config =
2233 pc_->GetConfiguration();
2234 EXPECT_EQ(new_config.ice_check_min_interval, rtc::Optional<int>(100));
2235}
2236
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002237// Test that when SetConfiguration changes both the pool size and other
2238// attributes, the pooled session is created with the updated attributes.
2239TEST_F(PeerConnectionInterfaceTest,
2240 SetConfigurationCreatesPooledSessionCorrectly) {
2241 CreatePeerConnection();
2242 PeerConnectionInterface::RTCConfiguration config;
2243 config.ice_candidate_pool_size = 1;
2244 PeerConnectionInterface::IceServer server;
2245 server.uri = kStunAddressOnly;
2246 config.servers.push_back(server);
2247 config.type = PeerConnectionInterface::kRelay;
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07002248 EXPECT_TRUE(pc_->SetConfiguration(config));
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002249
2250 const cricket::FakePortAllocatorSession* session =
2251 static_cast<const cricket::FakePortAllocatorSession*>(
2252 port_allocator_->GetPooledSession());
2253 ASSERT_NE(nullptr, session);
2254 EXPECT_EQ(1UL, session->stun_servers().size());
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002255}
2256
deadbeef293e9262017-01-11 12:28:30 -08002257// Test that after SetLocalDescription, changing the pool size is not allowed,
2258// and an invalid modification error is returned.
deadbeef6de92f92016-12-12 18:49:32 -08002259TEST_F(PeerConnectionInterfaceTest,
2260 CantChangePoolSizeAfterSetLocalDescription) {
2261 CreatePeerConnection();
2262 // Start by setting a size of 1.
2263 PeerConnectionInterface::RTCConfiguration config;
2264 config.ice_candidate_pool_size = 1;
2265 EXPECT_TRUE(pc_->SetConfiguration(config));
2266
2267 // Set remote offer; can still change pool size at this point.
2268 CreateOfferAsRemoteDescription();
2269 config.ice_candidate_pool_size = 2;
2270 EXPECT_TRUE(pc_->SetConfiguration(config));
2271
2272 // Set local answer; now it's too late.
2273 CreateAnswerAsLocalDescription();
2274 config.ice_candidate_pool_size = 3;
deadbeef293e9262017-01-11 12:28:30 -08002275 RTCError error;
2276 EXPECT_FALSE(pc_->SetConfiguration(config, &error));
2277 EXPECT_EQ(RTCErrorType::INVALID_MODIFICATION, error.type());
2278}
2279
deadbeef42a42632017-03-10 15:18:00 -08002280// Test that after setting an answer, extra pooled sessions are discarded. The
2281// ICE candidate pool is only intended to be used for the first offer/answer.
2282TEST_F(PeerConnectionInterfaceTest,
2283 ExtraPooledSessionsDiscardedAfterApplyingAnswer) {
2284 CreatePeerConnection();
2285
2286 // Set a larger-than-necessary size.
2287 PeerConnectionInterface::RTCConfiguration config;
2288 config.ice_candidate_pool_size = 4;
2289 EXPECT_TRUE(pc_->SetConfiguration(config));
2290
2291 // Do offer/answer.
2292 CreateOfferAsRemoteDescription();
2293 CreateAnswerAsLocalDescription();
2294
2295 // Expect no pooled sessions to be left.
2296 const cricket::PortAllocatorSession* session =
2297 port_allocator_->GetPooledSession();
2298 EXPECT_EQ(nullptr, session);
2299}
2300
2301// After Close is called, pooled candidates should be discarded so as to not
2302// waste network resources.
2303TEST_F(PeerConnectionInterfaceTest, PooledSessionsDiscardedAfterClose) {
2304 CreatePeerConnection();
2305
2306 PeerConnectionInterface::RTCConfiguration config;
2307 config.ice_candidate_pool_size = 3;
2308 EXPECT_TRUE(pc_->SetConfiguration(config));
2309 pc_->Close();
2310
2311 // Expect no pooled sessions to be left.
2312 const cricket::PortAllocatorSession* session =
2313 port_allocator_->GetPooledSession();
2314 EXPECT_EQ(nullptr, session);
2315}
2316
deadbeef293e9262017-01-11 12:28:30 -08002317// Test that SetConfiguration returns an invalid modification error if
2318// modifying a field in the configuration that isn't allowed to be modified.
2319TEST_F(PeerConnectionInterfaceTest,
2320 SetConfigurationReturnsInvalidModificationError) {
2321 PeerConnectionInterface::RTCConfiguration config;
2322 config.bundle_policy = PeerConnectionInterface::kBundlePolicyBalanced;
2323 config.rtcp_mux_policy = PeerConnectionInterface::kRtcpMuxPolicyNegotiate;
2324 config.continual_gathering_policy = PeerConnectionInterface::GATHER_ONCE;
2325 CreatePeerConnection(config, nullptr);
2326
2327 PeerConnectionInterface::RTCConfiguration modified_config = config;
2328 modified_config.bundle_policy =
2329 PeerConnectionInterface::kBundlePolicyMaxBundle;
2330 RTCError error;
2331 EXPECT_FALSE(pc_->SetConfiguration(modified_config, &error));
2332 EXPECT_EQ(RTCErrorType::INVALID_MODIFICATION, error.type());
2333
2334 modified_config = config;
2335 modified_config.rtcp_mux_policy =
2336 PeerConnectionInterface::kRtcpMuxPolicyRequire;
2337 error.set_type(RTCErrorType::NONE);
2338 EXPECT_FALSE(pc_->SetConfiguration(modified_config, &error));
2339 EXPECT_EQ(RTCErrorType::INVALID_MODIFICATION, error.type());
2340
2341 modified_config = config;
2342 modified_config.continual_gathering_policy =
2343 PeerConnectionInterface::GATHER_CONTINUALLY;
2344 error.set_type(RTCErrorType::NONE);
2345 EXPECT_FALSE(pc_->SetConfiguration(modified_config, &error));
2346 EXPECT_EQ(RTCErrorType::INVALID_MODIFICATION, error.type());
2347}
2348
2349// Test that SetConfiguration returns a range error if the candidate pool size
2350// is negative or larger than allowed by the spec.
2351TEST_F(PeerConnectionInterfaceTest,
2352 SetConfigurationReturnsRangeErrorForBadCandidatePoolSize) {
2353 PeerConnectionInterface::RTCConfiguration config;
2354 CreatePeerConnection(config, nullptr);
2355
2356 config.ice_candidate_pool_size = -1;
2357 RTCError error;
2358 EXPECT_FALSE(pc_->SetConfiguration(config, &error));
2359 EXPECT_EQ(RTCErrorType::INVALID_RANGE, error.type());
2360
2361 config.ice_candidate_pool_size = INT_MAX;
2362 error.set_type(RTCErrorType::NONE);
2363 EXPECT_FALSE(pc_->SetConfiguration(config, &error));
2364 EXPECT_EQ(RTCErrorType::INVALID_RANGE, error.type());
2365}
2366
2367// Test that SetConfiguration returns a syntax error if parsing an ICE server
2368// URL failed.
2369TEST_F(PeerConnectionInterfaceTest,
2370 SetConfigurationReturnsSyntaxErrorFromBadIceUrls) {
2371 PeerConnectionInterface::RTCConfiguration config;
2372 CreatePeerConnection(config, nullptr);
2373
2374 PeerConnectionInterface::IceServer bad_server;
2375 bad_server.uri = "stunn:www.example.com";
2376 config.servers.push_back(bad_server);
2377 RTCError error;
2378 EXPECT_FALSE(pc_->SetConfiguration(config, &error));
2379 EXPECT_EQ(RTCErrorType::SYNTAX_ERROR, error.type());
2380}
2381
2382// Test that SetConfiguration returns an invalid parameter error if a TURN
2383// IceServer is missing a username or password.
2384TEST_F(PeerConnectionInterfaceTest,
2385 SetConfigurationReturnsInvalidParameterIfCredentialsMissing) {
2386 PeerConnectionInterface::RTCConfiguration config;
2387 CreatePeerConnection(config, nullptr);
2388
2389 PeerConnectionInterface::IceServer bad_server;
2390 bad_server.uri = "turn:www.example.com";
2391 // Missing password.
2392 bad_server.username = "foo";
2393 config.servers.push_back(bad_server);
2394 RTCError error;
2395 EXPECT_FALSE(pc_->SetConfiguration(config, &error));
2396 EXPECT_EQ(RTCErrorType::INVALID_PARAMETER, error.type());
deadbeef6de92f92016-12-12 18:49:32 -08002397}
2398
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002399// Test that PeerConnection::Close changes the states to closed and all remote
2400// tracks change state to ended.
2401TEST_F(PeerConnectionInterfaceTest, CloseAndTestStreamsAndStates) {
2402 // Initialize a PeerConnection and negotiate local and remote session
2403 // description.
2404 InitiateCall();
2405 ASSERT_EQ(1u, pc_->local_streams()->count());
2406 ASSERT_EQ(1u, pc_->remote_streams()->count());
2407
2408 pc_->Close();
2409
2410 EXPECT_EQ(PeerConnectionInterface::kClosed, pc_->signaling_state());
2411 EXPECT_EQ(PeerConnectionInterface::kIceConnectionClosed,
2412 pc_->ice_connection_state());
2413 EXPECT_EQ(PeerConnectionInterface::kIceGatheringComplete,
2414 pc_->ice_gathering_state());
2415
2416 EXPECT_EQ(1u, pc_->local_streams()->count());
2417 EXPECT_EQ(1u, pc_->remote_streams()->count());
2418
zhihuang9763d562016-08-05 11:14:50 -07002419 rtc::scoped_refptr<MediaStreamInterface> remote_stream =
2420 pc_->remote_streams()->at(0);
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002421 // Track state may be updated asynchronously.
perkjd61bf802016-03-24 03:16:19 -07002422 EXPECT_EQ_WAIT(MediaStreamTrackInterface::kEnded,
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002423 remote_stream->GetAudioTracks()[0]->state(), kTimeout);
2424 EXPECT_EQ_WAIT(MediaStreamTrackInterface::kEnded,
2425 remote_stream->GetVideoTracks()[0]->state(), kTimeout);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002426}
2427
2428// Test that PeerConnection methods fails gracefully after
2429// PeerConnection::Close has been called.
2430TEST_F(PeerConnectionInterfaceTest, CloseAndTestMethods) {
deadbeef293e9262017-01-11 12:28:30 -08002431 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002432 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
2433 CreateOfferAsRemoteDescription();
2434 CreateAnswerAsLocalDescription();
2435
2436 ASSERT_EQ(1u, pc_->local_streams()->count());
zhihuang9763d562016-08-05 11:14:50 -07002437 rtc::scoped_refptr<MediaStreamInterface> local_stream =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002438 pc_->local_streams()->at(0);
2439
2440 pc_->Close();
2441
2442 pc_->RemoveStream(local_stream);
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +00002443 EXPECT_FALSE(pc_->AddStream(local_stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002444
2445 ASSERT_FALSE(local_stream->GetAudioTracks().empty());
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002446 rtc::scoped_refptr<webrtc::DtmfSenderInterface> dtmf_sender(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002447 pc_->CreateDtmfSender(local_stream->GetAudioTracks()[0]));
wu@webrtc.org66037362013-08-13 00:09:35 +00002448 EXPECT_TRUE(NULL == dtmf_sender); // local stream has been removed.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002449
2450 EXPECT_TRUE(pc_->CreateDataChannel("test", NULL) == NULL);
2451
2452 EXPECT_TRUE(pc_->local_description() != NULL);
2453 EXPECT_TRUE(pc_->remote_description() != NULL);
2454
kwibergd1fe2812016-04-27 06:47:29 -07002455 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07002456 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
kwibergd1fe2812016-04-27 06:47:29 -07002457 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -07002458 EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002459
2460 std::string sdp;
2461 ASSERT_TRUE(pc_->remote_description()->ToString(&sdp));
2462 SessionDescriptionInterface* remote_offer =
2463 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
2464 sdp, NULL);
2465 EXPECT_FALSE(DoSetRemoteDescription(remote_offer));
2466
2467 ASSERT_TRUE(pc_->local_description()->ToString(&sdp));
2468 SessionDescriptionInterface* local_offer =
2469 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
2470 sdp, NULL);
2471 EXPECT_FALSE(DoSetLocalDescription(local_offer));
2472}
2473
2474// Test that GetStats can still be called after PeerConnection::Close.
2475TEST_F(PeerConnectionInterfaceTest, CloseAndGetStats) {
2476 InitiateCall();
2477 pc_->Close();
2478 DoGetStats(NULL);
2479}
deadbeefab9b2d12015-10-14 11:33:11 -07002480
2481// NOTE: The series of tests below come from what used to be
2482// mediastreamsignaling_unittest.cc, and are mostly aimed at testing that
2483// setting a remote or local description has the expected effects.
2484
2485// This test verifies that the remote MediaStreams corresponding to a received
2486// SDP string is created. In this test the two separate MediaStreams are
2487// signaled.
2488TEST_F(PeerConnectionInterfaceTest, UpdateRemoteStreams) {
2489 FakeConstraints constraints;
2490 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2491 true);
2492 CreatePeerConnection(&constraints);
2493 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2494
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002495 rtc::scoped_refptr<StreamCollection> reference(CreateStreamCollection(1, 1));
deadbeefab9b2d12015-10-14 11:33:11 -07002496 EXPECT_TRUE(
2497 CompareStreamCollections(observer_.remote_streams(), reference.get()));
2498 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2499 EXPECT_TRUE(remote_stream->GetVideoTracks()[0]->GetSource() != nullptr);
2500
2501 // Create a session description based on another SDP with another
2502 // MediaStream.
2503 CreateAndSetRemoteOffer(kSdpStringWithStream1And2);
2504
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002505 rtc::scoped_refptr<StreamCollection> reference2(CreateStreamCollection(2, 1));
deadbeefab9b2d12015-10-14 11:33:11 -07002506 EXPECT_TRUE(
2507 CompareStreamCollections(observer_.remote_streams(), reference2.get()));
2508}
2509
2510// This test verifies that when remote tracks are added/removed from SDP, the
2511// created remote streams are updated appropriately.
2512TEST_F(PeerConnectionInterfaceTest,
2513 AddRemoveTrackFromExistingRemoteMediaStream) {
2514 FakeConstraints constraints;
2515 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2516 true);
2517 CreatePeerConnection(&constraints);
kwibergd1fe2812016-04-27 06:47:29 -07002518 std::unique_ptr<SessionDescriptionInterface> desc_ms1 =
kwiberg2bbff992016-03-16 11:03:04 -07002519 CreateSessionDescriptionAndReference(1, 1);
deadbeefab9b2d12015-10-14 11:33:11 -07002520 EXPECT_TRUE(DoSetRemoteDescription(desc_ms1.release()));
2521 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
2522 reference_collection_));
2523
2524 // Add extra audio and video tracks to the same MediaStream.
kwibergd1fe2812016-04-27 06:47:29 -07002525 std::unique_ptr<SessionDescriptionInterface> desc_ms1_two_tracks =
kwiberg2bbff992016-03-16 11:03:04 -07002526 CreateSessionDescriptionAndReference(2, 2);
deadbeefab9b2d12015-10-14 11:33:11 -07002527 EXPECT_TRUE(DoSetRemoteDescription(desc_ms1_two_tracks.release()));
2528 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
2529 reference_collection_));
zhihuang9763d562016-08-05 11:14:50 -07002530 rtc::scoped_refptr<AudioTrackInterface> audio_track2 =
perkjd61bf802016-03-24 03:16:19 -07002531 observer_.remote_streams()->at(0)->GetAudioTracks()[1];
2532 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, audio_track2->state());
zhihuang9763d562016-08-05 11:14:50 -07002533 rtc::scoped_refptr<VideoTrackInterface> video_track2 =
perkjd61bf802016-03-24 03:16:19 -07002534 observer_.remote_streams()->at(0)->GetVideoTracks()[1];
2535 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, video_track2->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002536
2537 // Remove the extra audio and video tracks.
kwibergd1fe2812016-04-27 06:47:29 -07002538 std::unique_ptr<SessionDescriptionInterface> desc_ms2 =
kwiberg2bbff992016-03-16 11:03:04 -07002539 CreateSessionDescriptionAndReference(1, 1);
perkjd61bf802016-03-24 03:16:19 -07002540 MockTrackObserver audio_track_observer(audio_track2);
2541 MockTrackObserver video_track_observer(video_track2);
2542
2543 EXPECT_CALL(audio_track_observer, OnChanged()).Times(Exactly(1));
2544 EXPECT_CALL(video_track_observer, OnChanged()).Times(Exactly(1));
deadbeefab9b2d12015-10-14 11:33:11 -07002545 EXPECT_TRUE(DoSetRemoteDescription(desc_ms2.release()));
2546 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
2547 reference_collection_));
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002548 // Track state may be updated asynchronously.
perkjd61bf802016-03-24 03:16:19 -07002549 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002550 audio_track2->state(), kTimeout);
2551 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
2552 video_track2->state(), kTimeout);
deadbeefab9b2d12015-10-14 11:33:11 -07002553}
2554
2555// This tests that remote tracks are ended if a local session description is set
2556// that rejects the media content type.
2557TEST_F(PeerConnectionInterfaceTest, RejectMediaContent) {
2558 FakeConstraints constraints;
2559 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2560 true);
2561 CreatePeerConnection(&constraints);
2562 // First create and set a remote offer, then reject its video content in our
2563 // answer.
2564 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2565 ASSERT_EQ(1u, observer_.remote_streams()->count());
2566 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2567 ASSERT_EQ(1u, remote_stream->GetVideoTracks().size());
2568 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2569
2570 rtc::scoped_refptr<webrtc::VideoTrackInterface> remote_video =
2571 remote_stream->GetVideoTracks()[0];
2572 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, remote_video->state());
2573 rtc::scoped_refptr<webrtc::AudioTrackInterface> remote_audio =
2574 remote_stream->GetAudioTracks()[0];
2575 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, remote_audio->state());
2576
kwibergd1fe2812016-04-27 06:47:29 -07002577 std::unique_ptr<SessionDescriptionInterface> local_answer;
kwiberg2bbff992016-03-16 11:03:04 -07002578 EXPECT_TRUE(DoCreateAnswer(&local_answer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07002579 cricket::ContentInfo* video_info =
2580 local_answer->description()->GetContentByName("video");
2581 video_info->rejected = true;
2582 EXPECT_TRUE(DoSetLocalDescription(local_answer.release()));
2583 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kEnded, remote_video->state());
2584 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, remote_audio->state());
2585
2586 // Now create an offer where we reject both video and audio.
kwibergd1fe2812016-04-27 06:47:29 -07002587 std::unique_ptr<SessionDescriptionInterface> local_offer;
kwiberg2bbff992016-03-16 11:03:04 -07002588 EXPECT_TRUE(DoCreateOffer(&local_offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07002589 video_info = local_offer->description()->GetContentByName("video");
2590 ASSERT_TRUE(video_info != nullptr);
2591 video_info->rejected = true;
2592 cricket::ContentInfo* audio_info =
2593 local_offer->description()->GetContentByName("audio");
2594 ASSERT_TRUE(audio_info != nullptr);
2595 audio_info->rejected = true;
2596 EXPECT_TRUE(DoSetLocalDescription(local_offer.release()));
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002597 // Track state may be updated asynchronously.
perkjd61bf802016-03-24 03:16:19 -07002598 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002599 remote_audio->state(), kTimeout);
2600 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
2601 remote_video->state(), kTimeout);
deadbeefab9b2d12015-10-14 11:33:11 -07002602}
2603
2604// This tests that we won't crash if the remote track has been removed outside
2605// of PeerConnection and then PeerConnection tries to reject the track.
2606TEST_F(PeerConnectionInterfaceTest, RemoveTrackThenRejectMediaContent) {
2607 FakeConstraints constraints;
2608 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2609 true);
2610 CreatePeerConnection(&constraints);
2611 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2612 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2613 remote_stream->RemoveTrack(remote_stream->GetVideoTracks()[0]);
2614 remote_stream->RemoveTrack(remote_stream->GetAudioTracks()[0]);
2615
kwibergd1fe2812016-04-27 06:47:29 -07002616 std::unique_ptr<SessionDescriptionInterface> local_answer(
deadbeefab9b2d12015-10-14 11:33:11 -07002617 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
2618 kSdpStringWithStream1, nullptr));
2619 cricket::ContentInfo* video_info =
2620 local_answer->description()->GetContentByName("video");
2621 video_info->rejected = true;
2622 cricket::ContentInfo* audio_info =
2623 local_answer->description()->GetContentByName("audio");
2624 audio_info->rejected = true;
2625 EXPECT_TRUE(DoSetLocalDescription(local_answer.release()));
2626
2627 // No crash is a pass.
2628}
2629
deadbeef5e97fb52015-10-15 12:49:08 -07002630// This tests that if a recvonly remote description is set, no remote streams
2631// will be created, even if the description contains SSRCs/MSIDs.
2632// See: https://code.google.com/p/webrtc/issues/detail?id=5054
2633TEST_F(PeerConnectionInterfaceTest, RecvonlyDescriptionDoesntCreateStream) {
2634 FakeConstraints constraints;
2635 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2636 true);
2637 CreatePeerConnection(&constraints);
2638
2639 std::string recvonly_offer = kSdpStringWithStream1;
2640 rtc::replace_substrs(kSendrecv, strlen(kSendrecv), kRecvonly,
2641 strlen(kRecvonly), &recvonly_offer);
2642 CreateAndSetRemoteOffer(recvonly_offer);
2643
2644 EXPECT_EQ(0u, observer_.remote_streams()->count());
2645}
2646
deadbeefab9b2d12015-10-14 11:33:11 -07002647// This tests that a default MediaStream is created if a remote session
2648// description doesn't contain any streams and no MSID support.
2649// It also tests that the default stream is updated if a video m-line is added
2650// in a subsequent session description.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002651TEST_F(PeerConnectionInterfaceTest, SdpWithoutMsidCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002652 FakeConstraints constraints;
2653 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2654 true);
2655 CreatePeerConnection(&constraints);
2656 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2657
2658 ASSERT_EQ(1u, observer_.remote_streams()->count());
2659 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2660
2661 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2662 EXPECT_EQ(0u, remote_stream->GetVideoTracks().size());
2663 EXPECT_EQ("default", remote_stream->label());
2664
2665 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2666 ASSERT_EQ(1u, observer_.remote_streams()->count());
2667 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2668 EXPECT_EQ("defaulta0", remote_stream->GetAudioTracks()[0]->id());
deadbeef884f5852016-01-15 09:20:04 -08002669 EXPECT_EQ(MediaStreamTrackInterface::kLive,
2670 remote_stream->GetAudioTracks()[0]->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002671 ASSERT_EQ(1u, remote_stream->GetVideoTracks().size());
2672 EXPECT_EQ("defaultv0", remote_stream->GetVideoTracks()[0]->id());
deadbeef884f5852016-01-15 09:20:04 -08002673 EXPECT_EQ(MediaStreamTrackInterface::kLive,
2674 remote_stream->GetVideoTracks()[0]->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002675}
2676
2677// This tests that a default MediaStream is created if a remote session
2678// description doesn't contain any streams and media direction is send only.
2679TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002680 SendOnlySdpWithoutMsidCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002681 FakeConstraints constraints;
2682 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2683 true);
2684 CreatePeerConnection(&constraints);
2685 CreateAndSetRemoteOffer(kSdpStringSendOnlyWithoutStreams);
2686
2687 ASSERT_EQ(1u, observer_.remote_streams()->count());
2688 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2689
2690 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2691 EXPECT_EQ(1u, remote_stream->GetVideoTracks().size());
2692 EXPECT_EQ("default", remote_stream->label());
2693}
2694
2695// This tests that it won't crash when PeerConnection tries to remove
2696// a remote track that as already been removed from the MediaStream.
2697TEST_F(PeerConnectionInterfaceTest, RemoveAlreadyGoneRemoteStream) {
2698 FakeConstraints constraints;
2699 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2700 true);
2701 CreatePeerConnection(&constraints);
2702 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2703 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2704 remote_stream->RemoveTrack(remote_stream->GetAudioTracks()[0]);
2705 remote_stream->RemoveTrack(remote_stream->GetVideoTracks()[0]);
2706
2707 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2708
2709 // No crash is a pass.
2710}
2711
2712// This tests that a default MediaStream is created if the remote session
2713// description doesn't contain any streams and don't contain an indication if
2714// MSID is supported.
2715TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002716 SdpWithoutMsidAndStreamsCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002717 FakeConstraints constraints;
2718 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2719 true);
2720 CreatePeerConnection(&constraints);
2721 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2722
2723 ASSERT_EQ(1u, observer_.remote_streams()->count());
2724 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2725 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2726 EXPECT_EQ(1u, remote_stream->GetVideoTracks().size());
2727}
2728
2729// This tests that a default MediaStream is not created if the remote session
2730// description doesn't contain any streams but does support MSID.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002731TEST_F(PeerConnectionInterfaceTest, SdpWithMsidDontCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002732 FakeConstraints constraints;
2733 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2734 true);
2735 CreatePeerConnection(&constraints);
2736 CreateAndSetRemoteOffer(kSdpStringWithMsidWithoutStreams);
2737 EXPECT_EQ(0u, observer_.remote_streams()->count());
2738}
2739
deadbeefbda7e0b2015-12-08 17:13:40 -08002740// This tests that when setting a new description, the old default tracks are
2741// not destroyed and recreated.
2742// See: https://bugs.chromium.org/p/webrtc/issues/detail?id=5250
Stefan Holmer102362b2016-03-18 09:39:07 +01002743TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002744 DefaultTracksNotDestroyedAndRecreated) {
deadbeefbda7e0b2015-12-08 17:13:40 -08002745 FakeConstraints constraints;
2746 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2747 true);
2748 CreatePeerConnection(&constraints);
2749 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2750
2751 ASSERT_EQ(1u, observer_.remote_streams()->count());
2752 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2753 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2754
2755 // Set the track to "disabled", then set a new description and ensure the
2756 // track is still disabled, which ensures it hasn't been recreated.
2757 remote_stream->GetAudioTracks()[0]->set_enabled(false);
2758 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2759 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2760 EXPECT_FALSE(remote_stream->GetAudioTracks()[0]->enabled());
2761}
2762
deadbeefab9b2d12015-10-14 11:33:11 -07002763// This tests that a default MediaStream is not created if a remote session
2764// description is updated to not have any MediaStreams.
2765TEST_F(PeerConnectionInterfaceTest, VerifyDefaultStreamIsNotCreated) {
2766 FakeConstraints constraints;
2767 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2768 true);
2769 CreatePeerConnection(&constraints);
2770 CreateAndSetRemoteOffer(kSdpStringWithStream1);
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002771 rtc::scoped_refptr<StreamCollection> reference(CreateStreamCollection(1, 1));
deadbeefab9b2d12015-10-14 11:33:11 -07002772 EXPECT_TRUE(
2773 CompareStreamCollections(observer_.remote_streams(), reference.get()));
2774
2775 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2776 EXPECT_EQ(0u, observer_.remote_streams()->count());
2777}
2778
2779// This tests that an RtpSender is created when the local description is set
2780// after adding a local stream.
2781// TODO(deadbeef): This test and the one below it need to be updated when
2782// an RtpSender's lifetime isn't determined by when a local description is set.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002783TEST_F(PeerConnectionInterfaceTest, LocalDescriptionChanged) {
deadbeefab9b2d12015-10-14 11:33:11 -07002784 FakeConstraints constraints;
2785 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2786 true);
2787 CreatePeerConnection(&constraints);
deadbeefab9b2d12015-10-14 11:33:11 -07002788
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002789 // Create an offer with 1 stream with 2 tracks of each type.
2790 rtc::scoped_refptr<StreamCollection> stream_collection =
2791 CreateStreamCollection(1, 2);
2792 pc_->AddStream(stream_collection->at(0));
2793 std::unique_ptr<SessionDescriptionInterface> offer;
2794 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2795 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
deadbeefab9b2d12015-10-14 11:33:11 -07002796
deadbeefab9b2d12015-10-14 11:33:11 -07002797 auto senders = pc_->GetSenders();
2798 EXPECT_EQ(4u, senders.size());
2799 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2800 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2801 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[1]));
2802 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[1]));
2803
2804 // Remove an audio and video track.
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002805 pc_->RemoveStream(stream_collection->at(0));
2806 stream_collection = CreateStreamCollection(1, 1);
2807 pc_->AddStream(stream_collection->at(0));
2808 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2809 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
2810
deadbeefab9b2d12015-10-14 11:33:11 -07002811 senders = pc_->GetSenders();
2812 EXPECT_EQ(2u, senders.size());
2813 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2814 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2815 EXPECT_FALSE(ContainsSender(senders, kAudioTracks[1]));
2816 EXPECT_FALSE(ContainsSender(senders, kVideoTracks[1]));
2817}
2818
2819// This tests that an RtpSender is created when the local description is set
2820// before adding a local stream.
2821TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002822 AddLocalStreamAfterLocalDescriptionChanged) {
deadbeefab9b2d12015-10-14 11:33:11 -07002823 FakeConstraints constraints;
2824 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2825 true);
2826 CreatePeerConnection(&constraints);
deadbeefab9b2d12015-10-14 11:33:11 -07002827
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002828 rtc::scoped_refptr<StreamCollection> stream_collection =
2829 CreateStreamCollection(1, 2);
2830 // Add a stream to create the offer, but remove it afterwards.
2831 pc_->AddStream(stream_collection->at(0));
2832 std::unique_ptr<SessionDescriptionInterface> offer;
2833 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2834 pc_->RemoveStream(stream_collection->at(0));
deadbeefab9b2d12015-10-14 11:33:11 -07002835
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002836 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
deadbeefab9b2d12015-10-14 11:33:11 -07002837 auto senders = pc_->GetSenders();
2838 EXPECT_EQ(0u, senders.size());
2839
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002840 pc_->AddStream(stream_collection->at(0));
deadbeefab9b2d12015-10-14 11:33:11 -07002841 senders = pc_->GetSenders();
2842 EXPECT_EQ(4u, senders.size());
2843 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2844 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2845 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[1]));
2846 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[1]));
2847}
2848
2849// This tests that the expected behavior occurs if the SSRC on a local track is
2850// changed when SetLocalDescription is called.
2851TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002852 ChangeSsrcOnTrackInLocalSessionDescription) {
deadbeefab9b2d12015-10-14 11:33:11 -07002853 FakeConstraints constraints;
2854 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2855 true);
2856 CreatePeerConnection(&constraints);
deadbeefab9b2d12015-10-14 11:33:11 -07002857
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002858 rtc::scoped_refptr<StreamCollection> stream_collection =
2859 CreateStreamCollection(2, 1);
2860 pc_->AddStream(stream_collection->at(0));
2861 std::unique_ptr<SessionDescriptionInterface> offer;
2862 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2863 // Grab a copy of the offer before it gets passed into the PC.
2864 std::unique_ptr<JsepSessionDescription> modified_offer(
2865 new JsepSessionDescription(JsepSessionDescription::kOffer));
2866 modified_offer->Initialize(offer->description()->Copy(), offer->session_id(),
2867 offer->session_version());
2868 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
deadbeefab9b2d12015-10-14 11:33:11 -07002869
deadbeefab9b2d12015-10-14 11:33:11 -07002870 auto senders = pc_->GetSenders();
2871 EXPECT_EQ(2u, senders.size());
2872 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2873 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2874
2875 // Change the ssrc of the audio and video track.
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002876 cricket::MediaContentDescription* desc =
2877 cricket::GetFirstAudioContentDescription(modified_offer->description());
2878 ASSERT_TRUE(desc != NULL);
2879 for (StreamParams& stream : desc->mutable_streams()) {
2880 for (unsigned int& ssrc : stream.ssrcs) {
2881 ++ssrc;
2882 }
2883 }
deadbeefab9b2d12015-10-14 11:33:11 -07002884
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002885 desc =
2886 cricket::GetFirstVideoContentDescription(modified_offer->description());
2887 ASSERT_TRUE(desc != NULL);
2888 for (StreamParams& stream : desc->mutable_streams()) {
2889 for (unsigned int& ssrc : stream.ssrcs) {
2890 ++ssrc;
2891 }
2892 }
2893
2894 EXPECT_TRUE(DoSetLocalDescription(modified_offer.release()));
deadbeefab9b2d12015-10-14 11:33:11 -07002895 senders = pc_->GetSenders();
2896 EXPECT_EQ(2u, senders.size());
2897 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2898 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2899 // TODO(deadbeef): Once RtpSenders expose parameters, check that the SSRC
2900 // changed.
2901}
2902
2903// This tests that the expected behavior occurs if a new session description is
2904// set with the same tracks, but on a different MediaStream.
Stefan Holmer55d6e7c2016-03-17 16:26:40 +01002905TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002906 SignalSameTracksInSeparateMediaStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002907 FakeConstraints constraints;
2908 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2909 true);
2910 CreatePeerConnection(&constraints);
deadbeefab9b2d12015-10-14 11:33:11 -07002911
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002912 rtc::scoped_refptr<StreamCollection> stream_collection =
2913 CreateStreamCollection(2, 1);
2914 pc_->AddStream(stream_collection->at(0));
2915 std::unique_ptr<SessionDescriptionInterface> offer;
2916 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2917 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
deadbeefab9b2d12015-10-14 11:33:11 -07002918
deadbeefab9b2d12015-10-14 11:33:11 -07002919 auto senders = pc_->GetSenders();
2920 EXPECT_EQ(2u, senders.size());
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002921 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0], kStreams[0]));
2922 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0], kStreams[0]));
deadbeefab9b2d12015-10-14 11:33:11 -07002923
2924 // Add a new MediaStream but with the same tracks as in the first stream.
2925 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream_1(
2926 webrtc::MediaStream::Create(kStreams[1]));
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002927 stream_1->AddTrack(stream_collection->at(0)->GetVideoTracks()[0]);
2928 stream_1->AddTrack(stream_collection->at(0)->GetAudioTracks()[0]);
deadbeefab9b2d12015-10-14 11:33:11 -07002929 pc_->AddStream(stream_1);
2930
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002931 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2932 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
deadbeefab9b2d12015-10-14 11:33:11 -07002933
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002934 auto new_senders = pc_->GetSenders();
2935 // Should be the same senders as before, but with updated stream id.
2936 // Note that this behavior is subject to change in the future.
2937 // We may decide the PC should ignore existing tracks in AddStream.
2938 EXPECT_EQ(senders, new_senders);
2939 EXPECT_TRUE(ContainsSender(new_senders, kAudioTracks[0], kStreams[1]));
2940 EXPECT_TRUE(ContainsSender(new_senders, kVideoTracks[0], kStreams[1]));
deadbeefab9b2d12015-10-14 11:33:11 -07002941}
2942
zhihuang81c3a032016-11-17 12:06:24 -08002943// This tests that PeerConnectionObserver::OnAddTrack is correctly called.
2944TEST_F(PeerConnectionInterfaceTest, OnAddTrackCallback) {
2945 FakeConstraints constraints;
2946 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2947 true);
2948 CreatePeerConnection(&constraints);
2949 CreateAndSetRemoteOffer(kSdpStringWithStream1AudioTrackOnly);
2950 EXPECT_EQ(observer_.num_added_tracks_, 1);
2951 EXPECT_EQ(observer_.last_added_track_label_, kAudioTracks[0]);
2952
2953 // Create and set the updated remote SDP.
2954 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2955 EXPECT_EQ(observer_.num_added_tracks_, 2);
2956 EXPECT_EQ(observer_.last_added_track_label_, kVideoTracks[0]);
2957}
2958
deadbeefd1a38b52016-12-10 13:15:33 -08002959// Test that when SetConfiguration is called and the configuration is
2960// changing, the next offer causes an ICE restart.
2961TEST_F(PeerConnectionInterfaceTest, SetConfigurationCausingIceRetart) {
2962 PeerConnectionInterface::RTCConfiguration config;
2963 config.type = PeerConnectionInterface::kRelay;
2964 // Need to pass default constraints to prevent disabling of DTLS...
2965 FakeConstraints default_constraints;
2966 CreatePeerConnection(config, &default_constraints);
2967 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
2968
2969 // Do initial offer/answer so there's something to restart.
2970 CreateOfferAsLocalDescription();
2971 CreateAnswerAsRemoteDescription(kSdpStringWithStream1);
2972
2973 // Grab the ufrags.
2974 std::vector<std::string> initial_ufrags = GetUfrags(pc_->local_description());
2975
2976 // Change ICE policy, which should trigger an ICE restart on the next offer.
2977 config.type = PeerConnectionInterface::kAll;
2978 EXPECT_TRUE(pc_->SetConfiguration(config));
2979 CreateOfferAsLocalDescription();
2980
2981 // Grab the new ufrags.
2982 std::vector<std::string> subsequent_ufrags =
2983 GetUfrags(pc_->local_description());
2984
2985 // Sanity check.
2986 EXPECT_EQ(initial_ufrags.size(), subsequent_ufrags.size());
2987 // Check that each ufrag is different.
2988 for (int i = 0; i < static_cast<int>(initial_ufrags.size()); ++i) {
2989 EXPECT_NE(initial_ufrags[i], subsequent_ufrags[i]);
2990 }
2991}
2992
2993// Test that when SetConfiguration is called and the configuration *isn't*
2994// changing, the next offer does *not* cause an ICE restart.
2995TEST_F(PeerConnectionInterfaceTest, SetConfigurationNotCausingIceRetart) {
2996 PeerConnectionInterface::RTCConfiguration config;
2997 config.type = PeerConnectionInterface::kRelay;
2998 // Need to pass default constraints to prevent disabling of DTLS...
2999 FakeConstraints default_constraints;
3000 CreatePeerConnection(config, &default_constraints);
3001 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
3002
3003 // Do initial offer/answer so there's something to restart.
3004 CreateOfferAsLocalDescription();
3005 CreateAnswerAsRemoteDescription(kSdpStringWithStream1);
3006
3007 // Grab the ufrags.
3008 std::vector<std::string> initial_ufrags = GetUfrags(pc_->local_description());
3009
3010 // Call SetConfiguration with a config identical to what the PC was
3011 // constructed with.
3012 EXPECT_TRUE(pc_->SetConfiguration(config));
3013 CreateOfferAsLocalDescription();
3014
3015 // Grab the new ufrags.
3016 std::vector<std::string> subsequent_ufrags =
3017 GetUfrags(pc_->local_description());
3018
3019 EXPECT_EQ(initial_ufrags, subsequent_ufrags);
3020}
3021
3022// Test for a weird corner case scenario:
3023// 1. Audio/video session established.
3024// 2. SetConfiguration changes ICE config; ICE restart needed.
3025// 3. ICE restart initiated by remote peer, but only for one m= section.
3026// 4. Next createOffer should initiate an ICE restart, but only for the other
3027// m= section; it would be pointless to do an ICE restart for the m= section
3028// that was already restarted.
3029TEST_F(PeerConnectionInterfaceTest, SetConfigurationCausingPartialIceRestart) {
3030 PeerConnectionInterface::RTCConfiguration config;
3031 config.type = PeerConnectionInterface::kRelay;
3032 // Need to pass default constraints to prevent disabling of DTLS...
3033 FakeConstraints default_constraints;
3034 CreatePeerConnection(config, &default_constraints);
3035 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
3036
3037 // Do initial offer/answer so there's something to restart.
3038 CreateOfferAsLocalDescription();
3039 CreateAnswerAsRemoteDescription(kSdpStringWithStream1);
3040
3041 // Change ICE policy, which should set the "needs-ice-restart" flag.
3042 config.type = PeerConnectionInterface::kAll;
3043 EXPECT_TRUE(pc_->SetConfiguration(config));
3044
3045 // Do ICE restart for the first m= section, initiated by remote peer.
3046 webrtc::JsepSessionDescription* remote_offer =
3047 new webrtc::JsepSessionDescription(SessionDescriptionInterface::kOffer);
3048 EXPECT_TRUE(remote_offer->Initialize(kSdpStringWithStream1, nullptr));
3049 remote_offer->description()->transport_infos()[0].description.ice_ufrag =
3050 "modified";
3051 EXPECT_TRUE(DoSetRemoteDescription(remote_offer));
3052 CreateAnswerAsLocalDescription();
3053
3054 // Grab the ufrags.
3055 std::vector<std::string> initial_ufrags = GetUfrags(pc_->local_description());
3056 ASSERT_EQ(2, initial_ufrags.size());
3057
3058 // Create offer and grab the new ufrags.
3059 CreateOfferAsLocalDescription();
3060 std::vector<std::string> subsequent_ufrags =
3061 GetUfrags(pc_->local_description());
3062 ASSERT_EQ(2, subsequent_ufrags.size());
3063
3064 // Ensure that only the ufrag for the second m= section changed.
3065 EXPECT_EQ(initial_ufrags[0], subsequent_ufrags[0]);
3066 EXPECT_NE(initial_ufrags[1], subsequent_ufrags[1]);
3067}
3068
deadbeeffe4a8a42016-12-20 17:56:17 -08003069// Tests that the methods to return current/pending descriptions work as
3070// expected at different points in the offer/answer exchange. This test does
3071// one offer/answer exchange as the offerer, then another as the answerer.
3072TEST_F(PeerConnectionInterfaceTest, CurrentAndPendingDescriptions) {
3073 // This disables DTLS so we can apply an answer to ourselves.
3074 CreatePeerConnection();
3075
3076 // Create initial local offer and get SDP (which will also be used as
3077 // answer/pranswer);
3078 std::unique_ptr<SessionDescriptionInterface> offer;
3079 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
3080 std::string sdp;
3081 EXPECT_TRUE(offer->ToString(&sdp));
3082
3083 // Set local offer.
3084 SessionDescriptionInterface* local_offer = offer.release();
3085 EXPECT_TRUE(DoSetLocalDescription(local_offer));
3086 EXPECT_EQ(local_offer, pc_->pending_local_description());
3087 EXPECT_EQ(nullptr, pc_->pending_remote_description());
3088 EXPECT_EQ(nullptr, pc_->current_local_description());
3089 EXPECT_EQ(nullptr, pc_->current_remote_description());
3090
3091 // Set remote pranswer.
3092 SessionDescriptionInterface* remote_pranswer =
3093 webrtc::CreateSessionDescription(SessionDescriptionInterface::kPrAnswer,
3094 sdp, nullptr);
3095 EXPECT_TRUE(DoSetRemoteDescription(remote_pranswer));
3096 EXPECT_EQ(local_offer, pc_->pending_local_description());
3097 EXPECT_EQ(remote_pranswer, pc_->pending_remote_description());
3098 EXPECT_EQ(nullptr, pc_->current_local_description());
3099 EXPECT_EQ(nullptr, pc_->current_remote_description());
3100
3101 // Set remote answer.
3102 SessionDescriptionInterface* remote_answer = webrtc::CreateSessionDescription(
3103 SessionDescriptionInterface::kAnswer, sdp, nullptr);
3104 EXPECT_TRUE(DoSetRemoteDescription(remote_answer));
3105 EXPECT_EQ(nullptr, pc_->pending_local_description());
3106 EXPECT_EQ(nullptr, pc_->pending_remote_description());
3107 EXPECT_EQ(local_offer, pc_->current_local_description());
3108 EXPECT_EQ(remote_answer, pc_->current_remote_description());
3109
3110 // Set remote offer.
3111 SessionDescriptionInterface* remote_offer = webrtc::CreateSessionDescription(
3112 SessionDescriptionInterface::kOffer, sdp, nullptr);
3113 EXPECT_TRUE(DoSetRemoteDescription(remote_offer));
3114 EXPECT_EQ(remote_offer, pc_->pending_remote_description());
3115 EXPECT_EQ(nullptr, pc_->pending_local_description());
3116 EXPECT_EQ(local_offer, pc_->current_local_description());
3117 EXPECT_EQ(remote_answer, pc_->current_remote_description());
3118
3119 // Set local pranswer.
3120 SessionDescriptionInterface* local_pranswer =
3121 webrtc::CreateSessionDescription(SessionDescriptionInterface::kPrAnswer,
3122 sdp, nullptr);
3123 EXPECT_TRUE(DoSetLocalDescription(local_pranswer));
3124 EXPECT_EQ(remote_offer, pc_->pending_remote_description());
3125 EXPECT_EQ(local_pranswer, pc_->pending_local_description());
3126 EXPECT_EQ(local_offer, pc_->current_local_description());
3127 EXPECT_EQ(remote_answer, pc_->current_remote_description());
3128
3129 // Set local answer.
3130 SessionDescriptionInterface* local_answer = webrtc::CreateSessionDescription(
3131 SessionDescriptionInterface::kAnswer, sdp, nullptr);
3132 EXPECT_TRUE(DoSetLocalDescription(local_answer));
3133 EXPECT_EQ(nullptr, pc_->pending_remote_description());
3134 EXPECT_EQ(nullptr, pc_->pending_local_description());
3135 EXPECT_EQ(remote_offer, pc_->current_remote_description());
3136 EXPECT_EQ(local_answer, pc_->current_local_description());
3137}
3138
zhihuang77985012017-02-07 15:45:16 -08003139// Tests that it won't crash when calling StartRtcEventLog or StopRtcEventLog
3140// after the PeerConnection is closed.
3141TEST_F(PeerConnectionInterfaceTest,
3142 StartAndStopLoggingAfterPeerConnectionClosed) {
3143 CreatePeerConnection();
3144 // The RtcEventLog will be reset when the PeerConnection is closed.
3145 pc_->Close();
3146
3147 rtc::PlatformFile file = 0;
3148 int64_t max_size_bytes = 1024;
3149 EXPECT_FALSE(pc_->StartRtcEventLog(file, max_size_bytes));
3150 pc_->StopRtcEventLog();
3151}
3152
deadbeef30952b42017-04-21 02:41:29 -07003153// Test that generated offers/answers include "ice-option:trickle".
3154TEST_F(PeerConnectionInterfaceTest, OffersAndAnswersHaveTrickleIceOption) {
3155 CreatePeerConnection();
3156
3157 // First, create an offer with audio/video.
3158 FakeConstraints constraints;
3159 constraints.SetMandatoryReceiveAudio(true);
3160 constraints.SetMandatoryReceiveVideo(true);
3161 std::unique_ptr<SessionDescriptionInterface> offer;
3162 ASSERT_TRUE(DoCreateOffer(&offer, &constraints));
3163 cricket::SessionDescription* desc = offer->description();
3164 ASSERT_EQ(2u, desc->transport_infos().size());
3165 EXPECT_TRUE(desc->transport_infos()[0].description.HasOption("trickle"));
3166 EXPECT_TRUE(desc->transport_infos()[1].description.HasOption("trickle"));
3167
3168 // Apply the offer as a remote description, then create an answer.
3169 EXPECT_TRUE(DoSetRemoteDescription(offer.release()));
3170 std::unique_ptr<SessionDescriptionInterface> answer;
3171 ASSERT_TRUE(DoCreateAnswer(&answer, &constraints));
3172 desc = answer->description();
3173 ASSERT_EQ(2u, desc->transport_infos().size());
3174 EXPECT_TRUE(desc->transport_infos()[0].description.HasOption("trickle"));
3175 EXPECT_TRUE(desc->transport_infos()[1].description.HasOption("trickle"));
3176}
3177
deadbeef1dcb1642017-03-29 21:08:16 -07003178// Test that ICE renomination isn't offered if it's not enabled in the PC's
3179// RTCConfiguration.
3180TEST_F(PeerConnectionInterfaceTest, IceRenominationNotOffered) {
3181 PeerConnectionInterface::RTCConfiguration config;
3182 config.enable_ice_renomination = false;
3183 CreatePeerConnection(config, nullptr);
3184 AddVoiceStream("foo");
3185
3186 std::unique_ptr<SessionDescriptionInterface> offer;
3187 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
3188 cricket::SessionDescription* desc = offer->description();
3189 EXPECT_EQ(1u, desc->transport_infos().size());
3190 EXPECT_FALSE(
3191 desc->transport_infos()[0].description.GetIceParameters().renomination);
3192}
3193
3194// Test that the ICE renomination option is present in generated offers/answers
3195// if it's enabled in the PC's RTCConfiguration.
3196TEST_F(PeerConnectionInterfaceTest, IceRenominationOptionInOfferAndAnswer) {
3197 PeerConnectionInterface::RTCConfiguration config;
3198 config.enable_ice_renomination = true;
3199 CreatePeerConnection(config, nullptr);
3200 AddVoiceStream("foo");
3201
3202 std::unique_ptr<SessionDescriptionInterface> offer;
3203 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
3204 cricket::SessionDescription* desc = offer->description();
3205 EXPECT_EQ(1u, desc->transport_infos().size());
3206 EXPECT_TRUE(
3207 desc->transport_infos()[0].description.GetIceParameters().renomination);
3208
3209 // Set the offer as a remote description, then create an answer and ensure it
3210 // has the renomination flag too.
3211 EXPECT_TRUE(DoSetRemoteDescription(offer.release()));
3212 std::unique_ptr<SessionDescriptionInterface> answer;
3213 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
3214 desc = answer->description();
3215 EXPECT_EQ(1u, desc->transport_infos().size());
3216 EXPECT_TRUE(
3217 desc->transport_infos()[0].description.GetIceParameters().renomination);
3218}
3219
3220// Test that if CreateOffer is called with the deprecated "offer to receive
3221// audio/video" constraints, they're processed and result in an offer with
3222// audio/video sections just as if RTCOfferAnswerOptions had been used.
3223TEST_F(PeerConnectionInterfaceTest, CreateOfferWithOfferToReceiveConstraints) {
3224 CreatePeerConnection();
3225
3226 FakeConstraints constraints;
3227 constraints.SetMandatoryReceiveAudio(true);
3228 constraints.SetMandatoryReceiveVideo(true);
3229 std::unique_ptr<SessionDescriptionInterface> offer;
3230 ASSERT_TRUE(DoCreateOffer(&offer, &constraints));
3231
3232 cricket::SessionDescription* desc = offer->description();
3233 const cricket::ContentInfo* audio = cricket::GetFirstAudioContent(desc);
3234 const cricket::ContentInfo* video = cricket::GetFirstVideoContent(desc);
3235 ASSERT_NE(nullptr, audio);
3236 ASSERT_NE(nullptr, video);
3237 EXPECT_FALSE(audio->rejected);
3238 EXPECT_FALSE(video->rejected);
3239}
3240
3241// Test that if CreateAnswer is called with the deprecated "offer to receive
3242// audio/video" constraints, they're processed and can be used to reject an
3243// offered m= section just as can be done with RTCOfferAnswerOptions;
3244TEST_F(PeerConnectionInterfaceTest, CreateAnswerWithOfferToReceiveConstraints) {
3245 CreatePeerConnection();
3246
3247 // First, create an offer with audio/video and apply it as a remote
3248 // description.
3249 FakeConstraints constraints;
3250 constraints.SetMandatoryReceiveAudio(true);
3251 constraints.SetMandatoryReceiveVideo(true);
3252 std::unique_ptr<SessionDescriptionInterface> offer;
3253 ASSERT_TRUE(DoCreateOffer(&offer, &constraints));
3254 EXPECT_TRUE(DoSetRemoteDescription(offer.release()));
3255
3256 // Now create answer that rejects audio/video.
3257 constraints.SetMandatoryReceiveAudio(false);
3258 constraints.SetMandatoryReceiveVideo(false);
3259 std::unique_ptr<SessionDescriptionInterface> answer;
3260 ASSERT_TRUE(DoCreateAnswer(&answer, &constraints));
3261
3262 cricket::SessionDescription* desc = answer->description();
3263 const cricket::ContentInfo* audio = cricket::GetFirstAudioContent(desc);
3264 const cricket::ContentInfo* video = cricket::GetFirstVideoContent(desc);
3265 ASSERT_NE(nullptr, audio);
3266 ASSERT_NE(nullptr, video);
3267 EXPECT_TRUE(audio->rejected);
3268 EXPECT_TRUE(video->rejected);
3269}
3270
3271#ifdef HAVE_SCTP
3272#define MAYBE_DataChannelOnlyOfferWithMaxBundlePolicy \
3273 DataChannelOnlyOfferWithMaxBundlePolicy
3274#else
3275#define MAYBE_DataChannelOnlyOfferWithMaxBundlePolicy \
3276 DISABLED_DataChannelOnlyOfferWithMaxBundlePolicy
3277#endif
3278
3279// Test that negotiation can succeed with a data channel only, and with the max
3280// bundle policy. Previously there was a bug that prevented this.
3281TEST_F(PeerConnectionInterfaceTest,
3282 MAYBE_DataChannelOnlyOfferWithMaxBundlePolicy) {
3283 PeerConnectionInterface::RTCConfiguration config;
3284 config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
3285 CreatePeerConnection(config, nullptr);
3286
3287 // First, create an offer with only a data channel and apply it as a remote
3288 // description.
3289 pc_->CreateDataChannel("test", nullptr);
3290 std::unique_ptr<SessionDescriptionInterface> offer;
3291 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
3292 EXPECT_TRUE(DoSetRemoteDescription(offer.release()));
3293
3294 // Create and set answer as well.
3295 std::unique_ptr<SessionDescriptionInterface> answer;
3296 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
3297 EXPECT_TRUE(DoSetLocalDescription(answer.release()));
3298}
3299
nisse51542be2016-02-12 02:27:06 -08003300class PeerConnectionMediaConfigTest : public testing::Test {
3301 protected:
3302 void SetUp() override {
nisseaf510af2016-03-21 08:20:42 -07003303 pcf_ = new rtc::RefCountedObject<PeerConnectionFactoryForTest>();
nisse51542be2016-02-12 02:27:06 -08003304 pcf_->Initialize();
3305 }
nisseeaabdf62017-05-05 02:23:02 -07003306 const cricket::MediaConfig TestCreatePeerConnection(
nisse51542be2016-02-12 02:27:06 -08003307 const PeerConnectionInterface::RTCConfiguration& config,
3308 const MediaConstraintsInterface *constraints) {
nisse51542be2016-02-12 02:27:06 -08003309
zhihuang9763d562016-08-05 11:14:50 -07003310 rtc::scoped_refptr<PeerConnectionInterface> pc(pcf_->CreatePeerConnection(
3311 config, constraints, nullptr, nullptr, &observer_));
nisse51542be2016-02-12 02:27:06 -08003312 EXPECT_TRUE(pc.get());
nisseeaabdf62017-05-05 02:23:02 -07003313 return pc->GetConfiguration().media_config;
nisse51542be2016-02-12 02:27:06 -08003314 }
3315
zhihuang9763d562016-08-05 11:14:50 -07003316 rtc::scoped_refptr<PeerConnectionFactoryForTest> pcf_;
nisse51542be2016-02-12 02:27:06 -08003317 MockPeerConnectionObserver observer_;
3318};
3319
3320// This test verifies the default behaviour with no constraints and a
3321// default RTCConfiguration.
3322TEST_F(PeerConnectionMediaConfigTest, TestDefaults) {
3323 PeerConnectionInterface::RTCConfiguration config;
3324 FakeConstraints constraints;
3325
3326 const cricket::MediaConfig& media_config =
3327 TestCreatePeerConnection(config, &constraints);
3328
3329 EXPECT_FALSE(media_config.enable_dscp);
nisse0db023a2016-03-01 04:29:59 -08003330 EXPECT_TRUE(media_config.video.enable_cpu_overuse_detection);
3331 EXPECT_FALSE(media_config.video.disable_prerenderer_smoothing);
3332 EXPECT_FALSE(media_config.video.suspend_below_min_bitrate);
nisse51542be2016-02-12 02:27:06 -08003333}
3334
3335// This test verifies the DSCP constraint is recognized and passed to
3336// the CreateMediaController call.
3337TEST_F(PeerConnectionMediaConfigTest, TestDscpConstraintTrue) {
3338 PeerConnectionInterface::RTCConfiguration config;
3339 FakeConstraints constraints;
3340
3341 constraints.AddOptional(webrtc::MediaConstraintsInterface::kEnableDscp, true);
3342 const cricket::MediaConfig& media_config =
3343 TestCreatePeerConnection(config, &constraints);
3344
3345 EXPECT_TRUE(media_config.enable_dscp);
3346}
3347
3348// This test verifies the cpu overuse detection constraint is
3349// recognized and passed to the CreateMediaController call.
3350TEST_F(PeerConnectionMediaConfigTest, TestCpuOveruseConstraintFalse) {
3351 PeerConnectionInterface::RTCConfiguration config;
3352 FakeConstraints constraints;
3353
3354 constraints.AddOptional(
3355 webrtc::MediaConstraintsInterface::kCpuOveruseDetection, false);
3356 const cricket::MediaConfig media_config =
3357 TestCreatePeerConnection(config, &constraints);
3358
nisse0db023a2016-03-01 04:29:59 -08003359 EXPECT_FALSE(media_config.video.enable_cpu_overuse_detection);
nisse51542be2016-02-12 02:27:06 -08003360}
3361
3362// This test verifies that the disable_prerenderer_smoothing flag is
3363// propagated from RTCConfiguration to the CreateMediaController call.
3364TEST_F(PeerConnectionMediaConfigTest, TestDisablePrerendererSmoothingTrue) {
3365 PeerConnectionInterface::RTCConfiguration config;
3366 FakeConstraints constraints;
3367
Niels Möller71bdda02016-03-31 12:59:59 +02003368 config.set_prerenderer_smoothing(false);
nisse51542be2016-02-12 02:27:06 -08003369 const cricket::MediaConfig& media_config =
3370 TestCreatePeerConnection(config, &constraints);
3371
nisse0db023a2016-03-01 04:29:59 -08003372 EXPECT_TRUE(media_config.video.disable_prerenderer_smoothing);
3373}
3374
3375// This test verifies the suspend below min bitrate constraint is
3376// recognized and passed to the CreateMediaController call.
3377TEST_F(PeerConnectionMediaConfigTest,
3378 TestSuspendBelowMinBitrateConstraintTrue) {
3379 PeerConnectionInterface::RTCConfiguration config;
3380 FakeConstraints constraints;
3381
3382 constraints.AddOptional(
3383 webrtc::MediaConstraintsInterface::kEnableVideoSuspendBelowMinBitrate,
3384 true);
3385 const cricket::MediaConfig media_config =
3386 TestCreatePeerConnection(config, &constraints);
3387
3388 EXPECT_TRUE(media_config.video.suspend_below_min_bitrate);
nisse51542be2016-02-12 02:27:06 -08003389}
3390
deadbeefab9b2d12015-10-14 11:33:11 -07003391// The following tests verify that session options are created correctly.
deadbeefc80741f2015-10-22 13:14:45 -07003392// TODO(deadbeef): Convert these tests to be more end-to-end. Instead of
3393// "verify options are converted correctly", should be "pass options into
3394// CreateOffer and verify the correct offer is produced."
deadbeefab9b2d12015-10-14 11:33:11 -07003395
3396TEST(CreateSessionOptionsTest, GetOptionsForOfferWithInvalidAudioOption) {
3397 RTCOfferAnswerOptions rtc_options;
3398 rtc_options.offer_to_receive_audio = RTCOfferAnswerOptions::kUndefined - 1;
3399
3400 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08003401 EXPECT_FALSE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07003402
3403 rtc_options.offer_to_receive_audio =
3404 RTCOfferAnswerOptions::kMaxOfferToReceiveMedia + 1;
htaaac2dea2016-03-10 13:35:55 -08003405 EXPECT_FALSE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07003406}
3407
3408TEST(CreateSessionOptionsTest, GetOptionsForOfferWithInvalidVideoOption) {
3409 RTCOfferAnswerOptions rtc_options;
3410 rtc_options.offer_to_receive_video = RTCOfferAnswerOptions::kUndefined - 1;
3411
3412 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08003413 EXPECT_FALSE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07003414
3415 rtc_options.offer_to_receive_video =
3416 RTCOfferAnswerOptions::kMaxOfferToReceiveMedia + 1;
htaaac2dea2016-03-10 13:35:55 -08003417 EXPECT_FALSE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07003418}
3419
3420// Test that a MediaSessionOptions is created for an offer if
deadbeefc80741f2015-10-22 13:14:45 -07003421// OfferToReceiveAudio and OfferToReceiveVideo options are set.
deadbeefab9b2d12015-10-14 11:33:11 -07003422TEST(CreateSessionOptionsTest, GetMediaSessionOptionsForOfferWithAudioVideo) {
3423 RTCOfferAnswerOptions rtc_options;
3424 rtc_options.offer_to_receive_audio = 1;
3425 rtc_options.offer_to_receive_video = 1;
3426
3427 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08003428 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07003429 EXPECT_TRUE(options.has_audio());
3430 EXPECT_TRUE(options.has_video());
3431 EXPECT_TRUE(options.bundle_enabled);
3432}
3433
3434// Test that a correct MediaSessionOptions is created for an offer if
deadbeefc80741f2015-10-22 13:14:45 -07003435// OfferToReceiveAudio is set.
deadbeefab9b2d12015-10-14 11:33:11 -07003436TEST(CreateSessionOptionsTest, GetMediaSessionOptionsForOfferWithAudio) {
3437 RTCOfferAnswerOptions rtc_options;
3438 rtc_options.offer_to_receive_audio = 1;
3439
3440 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08003441 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07003442 EXPECT_TRUE(options.has_audio());
3443 EXPECT_FALSE(options.has_video());
3444 EXPECT_TRUE(options.bundle_enabled);
3445}
3446
3447// Test that a correct MediaSessionOptions is created for an offer if
deadbeefc80741f2015-10-22 13:14:45 -07003448// the default OfferOptions are used.
deadbeefab9b2d12015-10-14 11:33:11 -07003449TEST(CreateSessionOptionsTest, GetDefaultMediaSessionOptionsForOffer) {
3450 RTCOfferAnswerOptions rtc_options;
3451
3452 cricket::MediaSessionOptions options;
deadbeef0ed85b22016-02-23 17:24:52 -08003453 options.transport_options["audio"] = cricket::TransportOptions();
3454 options.transport_options["video"] = cricket::TransportOptions();
htaaac2dea2016-03-10 13:35:55 -08003455 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefc80741f2015-10-22 13:14:45 -07003456 EXPECT_TRUE(options.has_audio());
deadbeefab9b2d12015-10-14 11:33:11 -07003457 EXPECT_FALSE(options.has_video());
deadbeefc80741f2015-10-22 13:14:45 -07003458 EXPECT_TRUE(options.bundle_enabled);
deadbeefab9b2d12015-10-14 11:33:11 -07003459 EXPECT_TRUE(options.vad_enabled);
deadbeef0ed85b22016-02-23 17:24:52 -08003460 EXPECT_FALSE(options.transport_options["audio"].ice_restart);
3461 EXPECT_FALSE(options.transport_options["video"].ice_restart);
deadbeefab9b2d12015-10-14 11:33:11 -07003462}
3463
3464// Test that a correct MediaSessionOptions is created for an offer if
deadbeefc80741f2015-10-22 13:14:45 -07003465// OfferToReceiveVideo is set.
deadbeefab9b2d12015-10-14 11:33:11 -07003466TEST(CreateSessionOptionsTest, GetMediaSessionOptionsForOfferWithVideo) {
3467 RTCOfferAnswerOptions rtc_options;
3468 rtc_options.offer_to_receive_audio = 0;
3469 rtc_options.offer_to_receive_video = 1;
3470
3471 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08003472 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07003473 EXPECT_FALSE(options.has_audio());
3474 EXPECT_TRUE(options.has_video());
3475 EXPECT_TRUE(options.bundle_enabled);
3476}
3477
3478// Test that a correct MediaSessionOptions is created for an offer if
3479// UseRtpMux is set to false.
3480TEST(CreateSessionOptionsTest,
3481 GetMediaSessionOptionsForOfferWithBundleDisabled) {
3482 RTCOfferAnswerOptions rtc_options;
3483 rtc_options.offer_to_receive_audio = 1;
3484 rtc_options.offer_to_receive_video = 1;
3485 rtc_options.use_rtp_mux = false;
3486
3487 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08003488 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07003489 EXPECT_TRUE(options.has_audio());
3490 EXPECT_TRUE(options.has_video());
3491 EXPECT_FALSE(options.bundle_enabled);
3492}
3493
3494// Test that a correct MediaSessionOptions is created to restart ice if
3495// IceRestart is set. It also tests that subsequent MediaSessionOptions don't
Taylor Brandstetterf475d362016-01-08 15:35:57 -08003496// have |audio_transport_options.ice_restart| etc. set.
deadbeefab9b2d12015-10-14 11:33:11 -07003497TEST(CreateSessionOptionsTest, GetMediaSessionOptionsForOfferWithIceRestart) {
3498 RTCOfferAnswerOptions rtc_options;
3499 rtc_options.ice_restart = true;
3500
3501 cricket::MediaSessionOptions options;
deadbeef0ed85b22016-02-23 17:24:52 -08003502 options.transport_options["audio"] = cricket::TransportOptions();
3503 options.transport_options["video"] = cricket::TransportOptions();
htaaac2dea2016-03-10 13:35:55 -08003504 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeef0ed85b22016-02-23 17:24:52 -08003505 EXPECT_TRUE(options.transport_options["audio"].ice_restart);
3506 EXPECT_TRUE(options.transport_options["video"].ice_restart);
deadbeefab9b2d12015-10-14 11:33:11 -07003507
3508 rtc_options = RTCOfferAnswerOptions();
htaaac2dea2016-03-10 13:35:55 -08003509 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeef0ed85b22016-02-23 17:24:52 -08003510 EXPECT_FALSE(options.transport_options["audio"].ice_restart);
3511 EXPECT_FALSE(options.transport_options["video"].ice_restart);
deadbeefab9b2d12015-10-14 11:33:11 -07003512}
3513
3514// Test that the MediaConstraints in an answer don't affect if audio and video
3515// is offered in an offer but that if kOfferToReceiveAudio or
3516// kOfferToReceiveVideo constraints are true in an offer, the media type will be
3517// included in subsequent answers.
3518TEST(CreateSessionOptionsTest, MediaConstraintsInAnswer) {
3519 FakeConstraints answer_c;
3520 answer_c.SetMandatoryReceiveAudio(true);
3521 answer_c.SetMandatoryReceiveVideo(true);
3522
3523 cricket::MediaSessionOptions answer_options;
3524 EXPECT_TRUE(ParseConstraintsForAnswer(&answer_c, &answer_options));
3525 EXPECT_TRUE(answer_options.has_audio());
3526 EXPECT_TRUE(answer_options.has_video());
3527
deadbeefc80741f2015-10-22 13:14:45 -07003528 RTCOfferAnswerOptions rtc_offer_options;
deadbeefab9b2d12015-10-14 11:33:11 -07003529
3530 cricket::MediaSessionOptions offer_options;
htaaac2dea2016-03-10 13:35:55 -08003531 EXPECT_TRUE(
3532 ExtractMediaSessionOptions(rtc_offer_options, false, &offer_options));
deadbeefc80741f2015-10-22 13:14:45 -07003533 EXPECT_TRUE(offer_options.has_audio());
htaaac2dea2016-03-10 13:35:55 -08003534 EXPECT_TRUE(offer_options.has_video());
deadbeefab9b2d12015-10-14 11:33:11 -07003535
deadbeefc80741f2015-10-22 13:14:45 -07003536 RTCOfferAnswerOptions updated_rtc_offer_options;
3537 updated_rtc_offer_options.offer_to_receive_audio = 1;
3538 updated_rtc_offer_options.offer_to_receive_video = 1;
deadbeefab9b2d12015-10-14 11:33:11 -07003539
3540 cricket::MediaSessionOptions updated_offer_options;
htaaac2dea2016-03-10 13:35:55 -08003541 EXPECT_TRUE(ExtractMediaSessionOptions(updated_rtc_offer_options, false,
htaa2a49d92016-03-04 02:51:39 -08003542 &updated_offer_options));
deadbeefab9b2d12015-10-14 11:33:11 -07003543 EXPECT_TRUE(updated_offer_options.has_audio());
3544 EXPECT_TRUE(updated_offer_options.has_video());
3545
3546 // Since an offer has been created with both audio and video, subsequent
3547 // offers and answers should contain both audio and video.
3548 // Answers will only contain the media types that exist in the offer
3549 // regardless of the value of |updated_answer_options.has_audio| and
3550 // |updated_answer_options.has_video|.
3551 FakeConstraints updated_answer_c;
3552 answer_c.SetMandatoryReceiveAudio(false);
3553 answer_c.SetMandatoryReceiveVideo(false);
3554
3555 cricket::MediaSessionOptions updated_answer_options;
3556 EXPECT_TRUE(
3557 ParseConstraintsForAnswer(&updated_answer_c, &updated_answer_options));
3558 EXPECT_TRUE(updated_answer_options.has_audio());
3559 EXPECT_TRUE(updated_answer_options.has_video());
deadbeefab9b2d12015-10-14 11:33:11 -07003560}
deadbeef3edec7c2016-12-10 11:44:26 -08003561
deadbeef293e9262017-01-11 12:28:30 -08003562// Tests a few random fields being different.
3563TEST(RTCConfigurationTest, ComparisonOperators) {
3564 PeerConnectionInterface::RTCConfiguration a;
3565 PeerConnectionInterface::RTCConfiguration b;
3566 EXPECT_EQ(a, b);
3567
3568 PeerConnectionInterface::RTCConfiguration c;
3569 c.servers.push_back(PeerConnectionInterface::IceServer());
3570 EXPECT_NE(a, c);
3571
3572 PeerConnectionInterface::RTCConfiguration d;
3573 d.type = PeerConnectionInterface::kRelay;
3574 EXPECT_NE(a, d);
3575
3576 PeerConnectionInterface::RTCConfiguration e;
3577 e.audio_jitter_buffer_max_packets = 5;
3578 EXPECT_NE(a, e);
3579
3580 PeerConnectionInterface::RTCConfiguration f;
3581 f.ice_connection_receiving_timeout = 1337;
3582 EXPECT_NE(a, f);
3583
3584 PeerConnectionInterface::RTCConfiguration g;
3585 g.disable_ipv6 = true;
3586 EXPECT_NE(a, g);
3587
3588 PeerConnectionInterface::RTCConfiguration h(
3589 PeerConnectionInterface::RTCConfigurationType::kAggressive);
3590 EXPECT_NE(a, h);
3591}