blob: 91c79ebb76eaeec7ddde66ee0cbd19b1b7732b59 [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"
Henrik Kjellander15583c12016-02-10 10:53:12 +010017#include "webrtc/api/jsepsessiondescription.h"
Henrik Kjellander15583c12016-02-10 10:53:12 +010018#include "webrtc/api/mediastreaminterface.h"
Henrik Kjellander15583c12016-02-10 10:53:12 +010019#include "webrtc/api/peerconnectioninterface.h"
20#include "webrtc/api/rtpreceiverinterface.h"
21#include "webrtc/api/rtpsenderinterface.h"
Henrik Kjellander15583c12016-02-10 10:53:12 +010022#include "webrtc/api/test/fakeconstraints.h"
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000023#include "webrtc/base/gunit.h"
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000024#include "webrtc/base/ssladapter.h"
25#include "webrtc/base/sslstreamadapter.h"
26#include "webrtc/base/stringutils.h"
27#include "webrtc/base/thread.h"
kjellandera96e2d72016-02-04 23:52:28 -080028#include "webrtc/media/base/fakevideocapturer.h"
deadbeef953c2ce2017-01-09 14:53:41 -080029#include "webrtc/media/sctp/sctptransportinternal.h"
ossua1a040a2017-04-06 10:03:21 -070030#include "webrtc/modules/audio_coding/codecs/builtin_audio_encoder_factory.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 webrtc::MediaControllerInterface* CreateMediaController(
skvlad11a9cbf2016-10-07 11:53:05 -0700649 const cricket::MediaConfig& config,
650 webrtc::RtcEventLog* event_log) const override {
zhihuang29ff8442016-07-27 11:07:25 -0700651 create_media_controller_called_ = true;
652 create_media_controller_config_ = config;
653
654 webrtc::MediaControllerInterface* mc =
skvlad11a9cbf2016-10-07 11:53:05 -0700655 PeerConnectionFactory::CreateMediaController(config, event_log);
zhihuang29ff8442016-07-27 11:07:25 -0700656 EXPECT_TRUE(mc != nullptr);
657 return mc;
658 }
659
660 cricket::TransportController* CreateTransportController(
Honghai Zhangbfd398c2016-08-30 22:07:42 -0700661 cricket::PortAllocator* port_allocator,
662 bool redetermine_role_on_ice_restart) override {
zhihuang29ff8442016-07-27 11:07:25 -0700663 transport_controller = new cricket::TransportController(
Honghai Zhangbfd398c2016-08-30 22:07:42 -0700664 rtc::Thread::Current(), rtc::Thread::Current(), port_allocator,
665 redetermine_role_on_ice_restart);
zhihuang29ff8442016-07-27 11:07:25 -0700666 return transport_controller;
667 }
668
669 cricket::TransportController* transport_controller;
670 // Mutable, so they can be modified in the above const-declared method.
671 mutable bool create_media_controller_called_ = false;
672 mutable cricket::MediaConfig create_media_controller_config_;
673};
674
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000675class PeerConnectionInterfaceTest : public testing::Test {
676 protected:
phoglund37ebcf02016-01-08 05:04:57 -0800677 PeerConnectionInterfaceTest() {
678#ifdef WEBRTC_ANDROID
679 webrtc::InitializeAndroidObjects();
680#endif
681 }
682
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000683 virtual void SetUp() {
684 pc_factory_ = webrtc::CreatePeerConnectionFactory(
danilchape9021a32016-05-17 01:52:02 -0700685 rtc::Thread::Current(), rtc::Thread::Current(), rtc::Thread::Current(),
686 nullptr, nullptr, nullptr);
687 ASSERT_TRUE(pc_factory_);
zhihuang29ff8442016-07-27 11:07:25 -0700688 pc_factory_for_test_ =
689 new rtc::RefCountedObject<PeerConnectionFactoryForTest>();
690 pc_factory_for_test_->Initialize();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000691 }
692
693 void CreatePeerConnection() {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700694 CreatePeerConnection(PeerConnectionInterface::RTCConfiguration(), nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000695 }
696
deadbeef293e9262017-01-11 12:28:30 -0800697 // DTLS does not work in a loopback call, so is disabled for most of the
698 // tests in this file.
699 void CreatePeerConnectionWithoutDtls() {
700 FakeConstraints no_dtls_constraints;
701 no_dtls_constraints.AddMandatory(
702 webrtc::MediaConstraintsInterface::kEnableDtlsSrtp, false);
703
704 CreatePeerConnection(PeerConnectionInterface::RTCConfiguration(),
705 &no_dtls_constraints);
706 }
707
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000708 void CreatePeerConnection(webrtc::MediaConstraintsInterface* constraints) {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700709 CreatePeerConnection(PeerConnectionInterface::RTCConfiguration(),
710 constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000711 }
712
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700713 void CreatePeerConnectionWithIceTransportsType(
714 PeerConnectionInterface::IceTransportsType type) {
715 PeerConnectionInterface::RTCConfiguration config;
716 config.type = type;
717 return CreatePeerConnection(config, nullptr);
718 }
719
720 void CreatePeerConnectionWithIceServer(const std::string& uri,
721 const std::string& password) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800722 PeerConnectionInterface::RTCConfiguration config;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000723 PeerConnectionInterface::IceServer server;
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700724 server.uri = uri;
725 server.password = password;
726 config.servers.push_back(server);
727 CreatePeerConnection(config, nullptr);
728 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000729
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700730 void CreatePeerConnection(PeerConnectionInterface::RTCConfiguration config,
731 webrtc::MediaConstraintsInterface* constraints) {
kwibergd1fe2812016-04-27 06:47:29 -0700732 std::unique_ptr<cricket::FakePortAllocator> port_allocator(
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800733 new cricket::FakePortAllocator(rtc::Thread::Current(), nullptr));
734 port_allocator_ = port_allocator.get();
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000735
deadbeef1dcb1642017-03-29 21:08:16 -0700736 // Create certificate generator unless DTLS constraint is explicitly set to
737 // false.
Henrik Boströmd79599d2016-06-01 13:58:50 +0200738 std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator;
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000739 bool dtls;
740 if (FindConstraint(constraints,
741 webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
742 &dtls,
Henrik Boström5e56c592015-08-11 10:33:13 +0200743 nullptr) && dtls) {
deadbeef8662f942017-01-20 21:20:51 -0800744 fake_certificate_generator_ = new FakeRTCCertificateGenerator();
745 cert_generator.reset(fake_certificate_generator_);
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000746 }
Henrik Boströmd79599d2016-06-01 13:58:50 +0200747 pc_ = pc_factory_->CreatePeerConnection(
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800748 config, constraints, std::move(port_allocator),
Henrik Boströmd79599d2016-06-01 13:58:50 +0200749 std::move(cert_generator), &observer_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000750 ASSERT_TRUE(pc_.get() != NULL);
751 observer_.SetPeerConnectionInterface(pc_.get());
752 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
753 }
754
deadbeef0a6c4ca2015-10-06 11:38:28 -0700755 void CreatePeerConnectionExpectFail(const std::string& uri) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800756 PeerConnectionInterface::RTCConfiguration config;
deadbeef0a6c4ca2015-10-06 11:38:28 -0700757 PeerConnectionInterface::IceServer server;
deadbeef0a6c4ca2015-10-06 11:38:28 -0700758 server.uri = uri;
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800759 config.servers.push_back(server);
deadbeef0a6c4ca2015-10-06 11:38:28 -0700760
zhihuang9763d562016-08-05 11:14:50 -0700761 rtc::scoped_refptr<PeerConnectionInterface> pc;
hbosd7973cc2016-05-27 06:08:53 -0700762 pc = pc_factory_->CreatePeerConnection(config, nullptr, nullptr, nullptr,
763 &observer_);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800764 EXPECT_EQ(nullptr, pc);
deadbeef0a6c4ca2015-10-06 11:38:28 -0700765 }
766
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000767 void CreatePeerConnectionWithDifferentConfigurations() {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700768 CreatePeerConnectionWithIceServer(kStunAddressOnly, "");
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800769 EXPECT_EQ(1u, port_allocator_->stun_servers().size());
770 EXPECT_EQ(0u, port_allocator_->turn_servers().size());
771 EXPECT_EQ("address", port_allocator_->stun_servers().begin()->hostname());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000772 EXPECT_EQ(kDefaultStunPort,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800773 port_allocator_->stun_servers().begin()->port());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000774
deadbeef0a6c4ca2015-10-06 11:38:28 -0700775 CreatePeerConnectionExpectFail(kStunInvalidPort);
776 CreatePeerConnectionExpectFail(kStunAddressPortAndMore1);
777 CreatePeerConnectionExpectFail(kStunAddressPortAndMore2);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000778
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700779 CreatePeerConnectionWithIceServer(kTurnIceServerUri, kTurnPassword);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800780 EXPECT_EQ(0u, port_allocator_->stun_servers().size());
781 EXPECT_EQ(1u, port_allocator_->turn_servers().size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000782 EXPECT_EQ(kTurnUsername,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800783 port_allocator_->turn_servers()[0].credentials.username);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000784 EXPECT_EQ(kTurnPassword,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800785 port_allocator_->turn_servers()[0].credentials.password);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000786 EXPECT_EQ(kTurnHostname,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800787 port_allocator_->turn_servers()[0].ports[0].address.hostname());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000788 }
789
790 void ReleasePeerConnection() {
791 pc_ = NULL;
792 observer_.SetPeerConnectionInterface(NULL);
793 }
794
deadbeefab9b2d12015-10-14 11:33:11 -0700795 void AddVideoStream(const std::string& label) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000796 // Create a local stream.
zhihuang9763d562016-08-05 11:14:50 -0700797 rtc::scoped_refptr<MediaStreamInterface> stream(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000798 pc_factory_->CreateLocalMediaStream(label));
zhihuang9763d562016-08-05 11:14:50 -0700799 rtc::scoped_refptr<VideoTrackSourceInterface> video_source(
deadbeef112b2e92017-02-10 20:13:37 -0800800 pc_factory_->CreateVideoSource(std::unique_ptr<cricket::VideoCapturer>(
801 new cricket::FakeVideoCapturer()),
802 NULL));
zhihuang9763d562016-08-05 11:14:50 -0700803 rtc::scoped_refptr<VideoTrackInterface> video_track(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000804 pc_factory_->CreateVideoTrack(label + "v0", video_source));
805 stream->AddTrack(video_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +0000806 EXPECT_TRUE(pc_->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000807 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
808 observer_.renegotiation_needed_ = false;
809 }
810
811 void AddVoiceStream(const std::string& label) {
812 // Create a local stream.
zhihuang9763d562016-08-05 11:14:50 -0700813 rtc::scoped_refptr<MediaStreamInterface> stream(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000814 pc_factory_->CreateLocalMediaStream(label));
zhihuang9763d562016-08-05 11:14:50 -0700815 rtc::scoped_refptr<AudioTrackInterface> audio_track(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000816 pc_factory_->CreateAudioTrack(label + "a0", NULL));
817 stream->AddTrack(audio_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +0000818 EXPECT_TRUE(pc_->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000819 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
820 observer_.renegotiation_needed_ = false;
821 }
822
823 void AddAudioVideoStream(const std::string& stream_label,
824 const std::string& audio_track_label,
825 const std::string& video_track_label) {
826 // Create a local stream.
zhihuang9763d562016-08-05 11:14:50 -0700827 rtc::scoped_refptr<MediaStreamInterface> stream(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000828 pc_factory_->CreateLocalMediaStream(stream_label));
zhihuang9763d562016-08-05 11:14:50 -0700829 rtc::scoped_refptr<AudioTrackInterface> audio_track(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000830 pc_factory_->CreateAudioTrack(
831 audio_track_label, static_cast<AudioSourceInterface*>(NULL)));
832 stream->AddTrack(audio_track.get());
zhihuang9763d562016-08-05 11:14:50 -0700833 rtc::scoped_refptr<VideoTrackInterface> video_track(
nisseaf510af2016-03-21 08:20:42 -0700834 pc_factory_->CreateVideoTrack(
deadbeef112b2e92017-02-10 20:13:37 -0800835 video_track_label, pc_factory_->CreateVideoSource(
836 std::unique_ptr<cricket::VideoCapturer>(
837 new cricket::FakeVideoCapturer()))));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000838 stream->AddTrack(video_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +0000839 EXPECT_TRUE(pc_->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000840 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
841 observer_.renegotiation_needed_ = false;
842 }
843
kwibergd1fe2812016-04-27 06:47:29 -0700844 bool DoCreateOfferAnswer(std::unique_ptr<SessionDescriptionInterface>* desc,
deadbeefc80741f2015-10-22 13:14:45 -0700845 bool offer,
846 MediaConstraintsInterface* constraints) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000847 rtc::scoped_refptr<MockCreateSessionDescriptionObserver>
848 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000849 MockCreateSessionDescriptionObserver>());
850 if (offer) {
deadbeefc80741f2015-10-22 13:14:45 -0700851 pc_->CreateOffer(observer, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000852 } else {
deadbeefc80741f2015-10-22 13:14:45 -0700853 pc_->CreateAnswer(observer, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000854 }
855 EXPECT_EQ_WAIT(true, observer->called(), kTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -0700856 *desc = observer->MoveDescription();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000857 return observer->result();
858 }
859
kwibergd1fe2812016-04-27 06:47:29 -0700860 bool DoCreateOffer(std::unique_ptr<SessionDescriptionInterface>* desc,
deadbeefc80741f2015-10-22 13:14:45 -0700861 MediaConstraintsInterface* constraints) {
862 return DoCreateOfferAnswer(desc, true, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000863 }
864
kwibergd1fe2812016-04-27 06:47:29 -0700865 bool DoCreateAnswer(std::unique_ptr<SessionDescriptionInterface>* desc,
deadbeefc80741f2015-10-22 13:14:45 -0700866 MediaConstraintsInterface* constraints) {
867 return DoCreateOfferAnswer(desc, false, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000868 }
869
870 bool DoSetSessionDescription(SessionDescriptionInterface* desc, bool local) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000871 rtc::scoped_refptr<MockSetSessionDescriptionObserver>
872 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000873 MockSetSessionDescriptionObserver>());
874 if (local) {
875 pc_->SetLocalDescription(observer, desc);
876 } else {
877 pc_->SetRemoteDescription(observer, desc);
878 }
zhihuang29ff8442016-07-27 11:07:25 -0700879 if (pc_->signaling_state() != PeerConnectionInterface::kClosed) {
880 EXPECT_EQ_WAIT(true, observer->called(), kTimeout);
881 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000882 return observer->result();
883 }
884
885 bool DoSetLocalDescription(SessionDescriptionInterface* desc) {
886 return DoSetSessionDescription(desc, true);
887 }
888
889 bool DoSetRemoteDescription(SessionDescriptionInterface* desc) {
890 return DoSetSessionDescription(desc, false);
891 }
892
893 // Calls PeerConnection::GetStats and check the return value.
894 // It does not verify the values in the StatReports since a RTCP packet might
895 // be required.
896 bool DoGetStats(MediaStreamTrackInterface* track) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000897 rtc::scoped_refptr<MockStatsObserver> observer(
898 new rtc::RefCountedObject<MockStatsObserver>());
jiayl@webrtc.orgdb41b4d2014-03-03 21:30:06 +0000899 if (!pc_->GetStats(
900 observer, track, PeerConnectionInterface::kStatsOutputLevelStandard))
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000901 return false;
902 EXPECT_TRUE_WAIT(observer->called(), kTimeout);
903 return observer->called();
904 }
905
906 void InitiateCall() {
deadbeef293e9262017-01-11 12:28:30 -0800907 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000908 // Create a local stream with audio&video tracks.
909 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
910 CreateOfferReceiveAnswer();
911 }
912
913 // Verify that RTP Header extensions has been negotiated for audio and video.
914 void VerifyRemoteRtpHeaderExtensions() {
915 const cricket::MediaContentDescription* desc =
916 cricket::GetFirstAudioContentDescription(
917 pc_->remote_description()->description());
918 ASSERT_TRUE(desc != NULL);
919 EXPECT_GT(desc->rtp_header_extensions().size(), 0u);
920
921 desc = cricket::GetFirstVideoContentDescription(
922 pc_->remote_description()->description());
923 ASSERT_TRUE(desc != NULL);
924 EXPECT_GT(desc->rtp_header_extensions().size(), 0u);
925 }
926
927 void CreateOfferAsRemoteDescription() {
kwibergd1fe2812016-04-27 06:47:29 -0700928 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -0700929 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000930 std::string sdp;
931 EXPECT_TRUE(offer->ToString(&sdp));
932 SessionDescriptionInterface* remote_offer =
933 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
934 sdp, NULL);
935 EXPECT_TRUE(DoSetRemoteDescription(remote_offer));
936 EXPECT_EQ(PeerConnectionInterface::kHaveRemoteOffer, observer_.state_);
937 }
938
deadbeefab9b2d12015-10-14 11:33:11 -0700939 void CreateAndSetRemoteOffer(const std::string& sdp) {
940 SessionDescriptionInterface* remote_offer =
941 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
942 sdp, nullptr);
943 EXPECT_TRUE(DoSetRemoteDescription(remote_offer));
944 EXPECT_EQ(PeerConnectionInterface::kHaveRemoteOffer, observer_.state_);
945 }
946
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000947 void CreateAnswerAsLocalDescription() {
kwibergd1fe2812016-04-27 06:47:29 -0700948 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -0700949 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000950
951 // TODO(perkj): Currently SetLocalDescription fails if any parameters in an
952 // audio codec change, even if the parameter has nothing to do with
953 // receiving. Not all parameters are serialized to SDP.
954 // Since CreatePrAnswerAsLocalDescription serialize/deserialize
955 // the SessionDescription, it is necessary to do that here to in order to
956 // get ReceiveOfferCreatePrAnswerAndAnswer and RenegotiateAudioOnly to pass.
957 // https://code.google.com/p/webrtc/issues/detail?id=1356
958 std::string sdp;
959 EXPECT_TRUE(answer->ToString(&sdp));
960 SessionDescriptionInterface* new_answer =
961 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
962 sdp, NULL);
963 EXPECT_TRUE(DoSetLocalDescription(new_answer));
964 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
965 }
966
967 void CreatePrAnswerAsLocalDescription() {
kwibergd1fe2812016-04-27 06:47:29 -0700968 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -0700969 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000970
971 std::string sdp;
972 EXPECT_TRUE(answer->ToString(&sdp));
973 SessionDescriptionInterface* pr_answer =
974 webrtc::CreateSessionDescription(SessionDescriptionInterface::kPrAnswer,
975 sdp, NULL);
976 EXPECT_TRUE(DoSetLocalDescription(pr_answer));
977 EXPECT_EQ(PeerConnectionInterface::kHaveLocalPrAnswer, observer_.state_);
978 }
979
980 void CreateOfferReceiveAnswer() {
981 CreateOfferAsLocalDescription();
982 std::string sdp;
983 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
984 CreateAnswerAsRemoteDescription(sdp);
985 }
986
987 void CreateOfferAsLocalDescription() {
kwibergd1fe2812016-04-27 06:47:29 -0700988 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -0700989 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000990 // TODO(perkj): Currently SetLocalDescription fails if any parameters in an
991 // audio codec change, even if the parameter has nothing to do with
992 // receiving. Not all parameters are serialized to SDP.
993 // Since CreatePrAnswerAsLocalDescription serialize/deserialize
994 // the SessionDescription, it is necessary to do that here to in order to
995 // get ReceiveOfferCreatePrAnswerAndAnswer and RenegotiateAudioOnly to pass.
996 // https://code.google.com/p/webrtc/issues/detail?id=1356
997 std::string sdp;
998 EXPECT_TRUE(offer->ToString(&sdp));
999 SessionDescriptionInterface* new_offer =
1000 webrtc::CreateSessionDescription(
1001 SessionDescriptionInterface::kOffer,
1002 sdp, NULL);
1003
1004 EXPECT_TRUE(DoSetLocalDescription(new_offer));
1005 EXPECT_EQ(PeerConnectionInterface::kHaveLocalOffer, observer_.state_);
mallinath@webrtc.org68cbd012014-01-22 00:16:46 +00001006 // Wait for the ice_complete message, so that SDP will have candidates.
1007 EXPECT_TRUE_WAIT(observer_.ice_complete_, kTimeout);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001008 }
1009
deadbeefab9b2d12015-10-14 11:33:11 -07001010 void CreateAnswerAsRemoteDescription(const std::string& sdp) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001011 webrtc::JsepSessionDescription* answer = 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
deadbeefab9b2d12015-10-14 11:33:11 -07001018 void CreatePrAnswerAndAnswerAsRemoteDescription(const std::string& sdp) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001019 webrtc::JsepSessionDescription* pr_answer =
1020 new webrtc::JsepSessionDescription(
1021 SessionDescriptionInterface::kPrAnswer);
deadbeefab9b2d12015-10-14 11:33:11 -07001022 EXPECT_TRUE(pr_answer->Initialize(sdp, NULL));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001023 EXPECT_TRUE(DoSetRemoteDescription(pr_answer));
1024 EXPECT_EQ(PeerConnectionInterface::kHaveRemotePrAnswer, observer_.state_);
1025 webrtc::JsepSessionDescription* answer =
1026 new webrtc::JsepSessionDescription(
1027 SessionDescriptionInterface::kAnswer);
deadbeefab9b2d12015-10-14 11:33:11 -07001028 EXPECT_TRUE(answer->Initialize(sdp, NULL));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001029 EXPECT_TRUE(DoSetRemoteDescription(answer));
1030 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
1031 }
1032
1033 // Help function used for waiting until a the last signaled remote stream has
1034 // the same label as |stream_label|. In a few of the tests in this file we
1035 // answer with the same session description as we offer and thus we can
1036 // check if OnAddStream have been called with the same stream as we offer to
1037 // send.
1038 void WaitAndVerifyOnAddStream(const std::string& stream_label) {
1039 EXPECT_EQ_WAIT(stream_label, observer_.GetLastAddedStreamLabel(), kTimeout);
1040 }
1041
1042 // Creates an offer and applies it as a local session description.
1043 // Creates an answer with the same SDP an the offer but removes all lines
1044 // that start with a:ssrc"
1045 void CreateOfferReceiveAnswerWithoutSsrc() {
1046 CreateOfferAsLocalDescription();
1047 std::string sdp;
1048 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
1049 SetSsrcToZero(&sdp);
1050 CreateAnswerAsRemoteDescription(sdp);
1051 }
1052
deadbeefab9b2d12015-10-14 11:33:11 -07001053 // This function creates a MediaStream with label kStreams[0] and
1054 // |number_of_audio_tracks| and |number_of_video_tracks| tracks and the
1055 // corresponding SessionDescriptionInterface. The SessionDescriptionInterface
kwiberg2bbff992016-03-16 11:03:04 -07001056 // is returned and the MediaStream is stored in
deadbeefab9b2d12015-10-14 11:33:11 -07001057 // |reference_collection_|
kwibergd1fe2812016-04-27 06:47:29 -07001058 std::unique_ptr<SessionDescriptionInterface>
kwiberg2bbff992016-03-16 11:03:04 -07001059 CreateSessionDescriptionAndReference(size_t number_of_audio_tracks,
1060 size_t number_of_video_tracks) {
1061 EXPECT_LE(number_of_audio_tracks, 2u);
1062 EXPECT_LE(number_of_video_tracks, 2u);
deadbeefab9b2d12015-10-14 11:33:11 -07001063
1064 reference_collection_ = StreamCollection::Create();
1065 std::string sdp_ms1 = std::string(kSdpStringInit);
1066
1067 std::string mediastream_label = kStreams[0];
1068
1069 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream(
1070 webrtc::MediaStream::Create(mediastream_label));
1071 reference_collection_->AddStream(stream);
1072
1073 if (number_of_audio_tracks > 0) {
1074 sdp_ms1 += std::string(kSdpStringAudio);
1075 sdp_ms1 += std::string(kSdpStringMs1Audio0);
1076 AddAudioTrack(kAudioTracks[0], stream);
1077 }
1078 if (number_of_audio_tracks > 1) {
1079 sdp_ms1 += kSdpStringMs1Audio1;
1080 AddAudioTrack(kAudioTracks[1], stream);
1081 }
1082
1083 if (number_of_video_tracks > 0) {
1084 sdp_ms1 += std::string(kSdpStringVideo);
1085 sdp_ms1 += std::string(kSdpStringMs1Video0);
1086 AddVideoTrack(kVideoTracks[0], stream);
1087 }
1088 if (number_of_video_tracks > 1) {
1089 sdp_ms1 += kSdpStringMs1Video1;
1090 AddVideoTrack(kVideoTracks[1], stream);
1091 }
1092
kwibergd1fe2812016-04-27 06:47:29 -07001093 return std::unique_ptr<SessionDescriptionInterface>(
kwiberg2bbff992016-03-16 11:03:04 -07001094 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
1095 sdp_ms1, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07001096 }
1097
1098 void AddAudioTrack(const std::string& track_id,
1099 MediaStreamInterface* stream) {
1100 rtc::scoped_refptr<webrtc::AudioTrackInterface> audio_track(
1101 webrtc::AudioTrack::Create(track_id, nullptr));
1102 ASSERT_TRUE(stream->AddTrack(audio_track));
1103 }
1104
1105 void AddVideoTrack(const std::string& track_id,
1106 MediaStreamInterface* stream) {
1107 rtc::scoped_refptr<webrtc::VideoTrackInterface> video_track(
nisseaf510af2016-03-21 08:20:42 -07001108 webrtc::VideoTrack::Create(track_id,
1109 webrtc::FakeVideoTrackSource::Create()));
deadbeefab9b2d12015-10-14 11:33:11 -07001110 ASSERT_TRUE(stream->AddTrack(video_track));
1111 }
1112
kwibergfd8be342016-05-14 19:44:11 -07001113 std::unique_ptr<SessionDescriptionInterface> CreateOfferWithOneAudioStream() {
deadbeef293e9262017-01-11 12:28:30 -08001114 CreatePeerConnectionWithoutDtls();
zhihuang8f65cdf2016-05-06 18:40:30 -07001115 AddVoiceStream(kStreamLabel1);
kwibergfd8be342016-05-14 19:44:11 -07001116 std::unique_ptr<SessionDescriptionInterface> offer;
zhihuang8f65cdf2016-05-06 18:40:30 -07001117 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
1118 return offer;
1119 }
1120
kwibergfd8be342016-05-14 19:44:11 -07001121 std::unique_ptr<SessionDescriptionInterface>
zhihuang8f65cdf2016-05-06 18:40:30 -07001122 CreateAnswerWithOneAudioStream() {
kwibergfd8be342016-05-14 19:44:11 -07001123 std::unique_ptr<SessionDescriptionInterface> offer =
zhihuang8f65cdf2016-05-06 18:40:30 -07001124 CreateOfferWithOneAudioStream();
1125 EXPECT_TRUE(DoSetRemoteDescription(offer.release()));
kwibergfd8be342016-05-14 19:44:11 -07001126 std::unique_ptr<SessionDescriptionInterface> answer;
zhihuang8f65cdf2016-05-06 18:40:30 -07001127 EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
1128 return answer;
1129 }
1130
1131 const std::string& GetFirstAudioStreamCname(
1132 const SessionDescriptionInterface* desc) {
1133 const cricket::ContentInfo* audio_content =
1134 cricket::GetFirstAudioContent(desc->description());
1135 const cricket::AudioContentDescription* audio_desc =
1136 static_cast<const cricket::AudioContentDescription*>(
1137 audio_content->description);
1138 return audio_desc->streams()[0].cname;
1139 }
1140
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08001141 cricket::FakePortAllocator* port_allocator_ = nullptr;
deadbeef8662f942017-01-20 21:20:51 -08001142 FakeRTCCertificateGenerator* fake_certificate_generator_ = nullptr;
zhihuang9763d562016-08-05 11:14:50 -07001143 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface> pc_factory_;
1144 rtc::scoped_refptr<PeerConnectionFactoryForTest> pc_factory_for_test_;
1145 rtc::scoped_refptr<PeerConnectionInterface> pc_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001146 MockPeerConnectionObserver observer_;
deadbeefab9b2d12015-10-14 11:33:11 -07001147 rtc::scoped_refptr<StreamCollection> reference_collection_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001148};
1149
zhihuang29ff8442016-07-27 11:07:25 -07001150// Test that no callbacks on the PeerConnectionObserver are called after the
1151// PeerConnection is closed.
1152TEST_F(PeerConnectionInterfaceTest, CloseAndTestCallbackFunctions) {
zhihuang9763d562016-08-05 11:14:50 -07001153 rtc::scoped_refptr<PeerConnectionInterface> pc(
zhihuang29ff8442016-07-27 11:07:25 -07001154 pc_factory_for_test_->CreatePeerConnection(
1155 PeerConnectionInterface::RTCConfiguration(), nullptr, nullptr,
1156 nullptr, &observer_));
1157 observer_.SetPeerConnectionInterface(pc.get());
1158 pc->Close();
1159
1160 // No callbacks is expected to be called.
zhihuang81c3a032016-11-17 12:06:24 -08001161 observer_.callback_triggered_ = false;
zhihuang29ff8442016-07-27 11:07:25 -07001162 std::vector<cricket::Candidate> candidates;
1163 pc_factory_for_test_->transport_controller->SignalGatheringState(
1164 cricket::IceGatheringState{});
1165 pc_factory_for_test_->transport_controller->SignalCandidatesGathered(
1166 "", candidates);
1167 pc_factory_for_test_->transport_controller->SignalConnectionState(
1168 cricket::IceConnectionState{});
1169 pc_factory_for_test_->transport_controller->SignalCandidatesRemoved(
1170 candidates);
1171 pc_factory_for_test_->transport_controller->SignalReceiving(false);
zhihuang81c3a032016-11-17 12:06:24 -08001172 EXPECT_FALSE(observer_.callback_triggered_);
zhihuang29ff8442016-07-27 11:07:25 -07001173}
1174
zhihuang8f65cdf2016-05-06 18:40:30 -07001175// Generate different CNAMEs when PeerConnections are created.
1176// The CNAMEs are expected to be generated randomly. It is possible
1177// that the test fails, though the possibility is very low.
1178TEST_F(PeerConnectionInterfaceTest, CnameGenerationInOffer) {
kwibergfd8be342016-05-14 19:44:11 -07001179 std::unique_ptr<SessionDescriptionInterface> offer1 =
zhihuang8f65cdf2016-05-06 18:40:30 -07001180 CreateOfferWithOneAudioStream();
kwibergfd8be342016-05-14 19:44:11 -07001181 std::unique_ptr<SessionDescriptionInterface> offer2 =
zhihuang8f65cdf2016-05-06 18:40:30 -07001182 CreateOfferWithOneAudioStream();
1183 EXPECT_NE(GetFirstAudioStreamCname(offer1.get()),
1184 GetFirstAudioStreamCname(offer2.get()));
1185}
1186
1187TEST_F(PeerConnectionInterfaceTest, CnameGenerationInAnswer) {
kwibergfd8be342016-05-14 19:44:11 -07001188 std::unique_ptr<SessionDescriptionInterface> answer1 =
zhihuang8f65cdf2016-05-06 18:40:30 -07001189 CreateAnswerWithOneAudioStream();
kwibergfd8be342016-05-14 19:44:11 -07001190 std::unique_ptr<SessionDescriptionInterface> answer2 =
zhihuang8f65cdf2016-05-06 18:40:30 -07001191 CreateAnswerWithOneAudioStream();
1192 EXPECT_NE(GetFirstAudioStreamCname(answer1.get()),
1193 GetFirstAudioStreamCname(answer2.get()));
1194}
1195
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001196TEST_F(PeerConnectionInterfaceTest,
1197 CreatePeerConnectionWithDifferentConfigurations) {
1198 CreatePeerConnectionWithDifferentConfigurations();
1199}
1200
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001201TEST_F(PeerConnectionInterfaceTest,
1202 CreatePeerConnectionWithDifferentIceTransportsTypes) {
1203 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kNone);
1204 EXPECT_EQ(cricket::CF_NONE, port_allocator_->candidate_filter());
1205 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kRelay);
1206 EXPECT_EQ(cricket::CF_RELAY, port_allocator_->candidate_filter());
1207 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kNoHost);
1208 EXPECT_EQ(cricket::CF_ALL & ~cricket::CF_HOST,
1209 port_allocator_->candidate_filter());
1210 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kAll);
1211 EXPECT_EQ(cricket::CF_ALL, port_allocator_->candidate_filter());
1212}
1213
1214// Test that when a PeerConnection is created with a nonzero candidate pool
1215// size, the pooled PortAllocatorSession is created with all the attributes
1216// in the RTCConfiguration.
1217TEST_F(PeerConnectionInterfaceTest, CreatePeerConnectionWithPooledCandidates) {
1218 PeerConnectionInterface::RTCConfiguration config;
1219 PeerConnectionInterface::IceServer server;
1220 server.uri = kStunAddressOnly;
1221 config.servers.push_back(server);
1222 config.type = PeerConnectionInterface::kRelay;
1223 config.disable_ipv6 = true;
1224 config.tcp_candidate_policy =
1225 PeerConnectionInterface::kTcpCandidatePolicyDisabled;
honghaiz60347052016-05-31 18:29:12 -07001226 config.candidate_network_policy =
1227 PeerConnectionInterface::kCandidateNetworkPolicyLowCost;
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001228 config.ice_candidate_pool_size = 1;
1229 CreatePeerConnection(config, nullptr);
1230
1231 const cricket::FakePortAllocatorSession* session =
1232 static_cast<const cricket::FakePortAllocatorSession*>(
1233 port_allocator_->GetPooledSession());
1234 ASSERT_NE(nullptr, session);
1235 EXPECT_EQ(1UL, session->stun_servers().size());
1236 EXPECT_EQ(0U, session->flags() & cricket::PORTALLOCATOR_ENABLE_IPV6);
1237 EXPECT_LT(0U, session->flags() & cricket::PORTALLOCATOR_DISABLE_TCP);
honghaiz60347052016-05-31 18:29:12 -07001238 EXPECT_LT(0U,
1239 session->flags() & cricket::PORTALLOCATOR_DISABLE_COSTLY_NETWORKS);
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001240}
1241
Taylor Brandstetterf8e65772016-06-27 17:20:15 -07001242// Test that the PeerConnection initializes the port allocator passed into it,
1243// and on the correct thread.
1244TEST_F(PeerConnectionInterfaceTest,
1245 CreatePeerConnectionInitializesPortAllocator) {
1246 rtc::Thread network_thread;
1247 network_thread.Start();
1248 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface> pc_factory(
1249 webrtc::CreatePeerConnectionFactory(
1250 &network_thread, rtc::Thread::Current(), rtc::Thread::Current(),
1251 nullptr, nullptr, nullptr));
1252 std::unique_ptr<cricket::FakePortAllocator> port_allocator(
1253 new cricket::FakePortAllocator(&network_thread, nullptr));
1254 cricket::FakePortAllocator* raw_port_allocator = port_allocator.get();
1255 PeerConnectionInterface::RTCConfiguration config;
1256 rtc::scoped_refptr<PeerConnectionInterface> pc(
1257 pc_factory->CreatePeerConnection(
1258 config, nullptr, std::move(port_allocator), nullptr, &observer_));
1259 // FakePortAllocator RTC_CHECKs that it's initialized on the right thread,
1260 // so all we have to do here is check that it's initialized.
1261 EXPECT_TRUE(raw_port_allocator->initialized());
1262}
1263
deadbeef46c73892016-11-16 19:42:04 -08001264// Check that GetConfiguration returns the configuration the PeerConnection was
1265// constructed with, before SetConfiguration is called.
1266TEST_F(PeerConnectionInterfaceTest, GetConfigurationAfterCreatePeerConnection) {
1267 PeerConnectionInterface::RTCConfiguration config;
1268 config.type = PeerConnectionInterface::kRelay;
1269 CreatePeerConnection(config, nullptr);
1270
1271 PeerConnectionInterface::RTCConfiguration returned_config =
1272 pc_->GetConfiguration();
1273 EXPECT_EQ(PeerConnectionInterface::kRelay, returned_config.type);
1274}
1275
1276// Check that GetConfiguration returns the last configuration passed into
1277// SetConfiguration.
1278TEST_F(PeerConnectionInterfaceTest, GetConfigurationAfterSetConfiguration) {
1279 CreatePeerConnection();
1280
1281 PeerConnectionInterface::RTCConfiguration config;
1282 config.type = PeerConnectionInterface::kRelay;
1283 EXPECT_TRUE(pc_->SetConfiguration(config));
1284
1285 PeerConnectionInterface::RTCConfiguration returned_config =
1286 pc_->GetConfiguration();
1287 EXPECT_EQ(PeerConnectionInterface::kRelay, returned_config.type);
1288}
1289
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001290TEST_F(PeerConnectionInterfaceTest, AddStreams) {
deadbeef293e9262017-01-11 12:28:30 -08001291 CreatePeerConnectionWithoutDtls();
deadbeefab9b2d12015-10-14 11:33:11 -07001292 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001293 AddVoiceStream(kStreamLabel2);
1294 ASSERT_EQ(2u, pc_->local_streams()->count());
1295
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001296 // Test we can add multiple local streams to one peerconnection.
zhihuang9763d562016-08-05 11:14:50 -07001297 rtc::scoped_refptr<MediaStreamInterface> stream(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001298 pc_factory_->CreateLocalMediaStream(kStreamLabel3));
zhihuang9763d562016-08-05 11:14:50 -07001299 rtc::scoped_refptr<AudioTrackInterface> audio_track(
1300 pc_factory_->CreateAudioTrack(kStreamLabel3,
1301 static_cast<AudioSourceInterface*>(NULL)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001302 stream->AddTrack(audio_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +00001303 EXPECT_TRUE(pc_->AddStream(stream));
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001304 EXPECT_EQ(3u, pc_->local_streams()->count());
1305
1306 // Remove the third stream.
1307 pc_->RemoveStream(pc_->local_streams()->at(2));
1308 EXPECT_EQ(2u, pc_->local_streams()->count());
1309
1310 // Remove the second stream.
1311 pc_->RemoveStream(pc_->local_streams()->at(1));
1312 EXPECT_EQ(1u, pc_->local_streams()->count());
1313
1314 // Remove the first stream.
1315 pc_->RemoveStream(pc_->local_streams()->at(0));
1316 EXPECT_EQ(0u, pc_->local_streams()->count());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001317}
1318
deadbeefab9b2d12015-10-14 11:33:11 -07001319// Test that the created offer includes streams we added.
1320TEST_F(PeerConnectionInterfaceTest, AddedStreamsPresentInOffer) {
deadbeef293e9262017-01-11 12:28:30 -08001321 CreatePeerConnectionWithoutDtls();
deadbeefab9b2d12015-10-14 11:33:11 -07001322 AddAudioVideoStream(kStreamLabel1, "audio_track", "video_track");
kwibergd1fe2812016-04-27 06:47:29 -07001323 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001324 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07001325
1326 const cricket::ContentInfo* audio_content =
1327 cricket::GetFirstAudioContent(offer->description());
1328 const cricket::AudioContentDescription* audio_desc =
1329 static_cast<const cricket::AudioContentDescription*>(
1330 audio_content->description);
1331 EXPECT_TRUE(
1332 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
1333
1334 const cricket::ContentInfo* video_content =
1335 cricket::GetFirstVideoContent(offer->description());
1336 const cricket::VideoContentDescription* video_desc =
1337 static_cast<const cricket::VideoContentDescription*>(
1338 video_content->description);
1339 EXPECT_TRUE(
1340 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1341
1342 // Add another stream and ensure the offer includes both the old and new
1343 // streams.
1344 AddAudioVideoStream(kStreamLabel2, "audio_track2", "video_track2");
kwiberg2bbff992016-03-16 11:03:04 -07001345 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07001346
1347 audio_content = cricket::GetFirstAudioContent(offer->description());
1348 audio_desc = static_cast<const cricket::AudioContentDescription*>(
1349 audio_content->description);
1350 EXPECT_TRUE(
1351 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
1352 EXPECT_TRUE(
1353 ContainsTrack(audio_desc->streams(), kStreamLabel2, "audio_track2"));
1354
1355 video_content = cricket::GetFirstVideoContent(offer->description());
1356 video_desc = static_cast<const cricket::VideoContentDescription*>(
1357 video_content->description);
1358 EXPECT_TRUE(
1359 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1360 EXPECT_TRUE(
1361 ContainsTrack(video_desc->streams(), kStreamLabel2, "video_track2"));
1362}
1363
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001364TEST_F(PeerConnectionInterfaceTest, RemoveStream) {
deadbeef293e9262017-01-11 12:28:30 -08001365 CreatePeerConnectionWithoutDtls();
deadbeefab9b2d12015-10-14 11:33:11 -07001366 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001367 ASSERT_EQ(1u, pc_->local_streams()->count());
1368 pc_->RemoveStream(pc_->local_streams()->at(0));
1369 EXPECT_EQ(0u, pc_->local_streams()->count());
1370}
1371
deadbeefe1f9d832016-01-14 15:35:42 -08001372// Test for AddTrack and RemoveTrack methods.
1373// Tests that the created offer includes tracks we added,
1374// and that the RtpSenders are created correctly.
1375// Also tests that RemoveTrack removes the tracks from subsequent offers.
1376TEST_F(PeerConnectionInterfaceTest, AddTrackRemoveTrack) {
deadbeef293e9262017-01-11 12:28:30 -08001377 CreatePeerConnectionWithoutDtls();
deadbeefe1f9d832016-01-14 15:35:42 -08001378 // Create a dummy stream, so tracks share a stream label.
zhihuang9763d562016-08-05 11:14:50 -07001379 rtc::scoped_refptr<MediaStreamInterface> stream(
deadbeefe1f9d832016-01-14 15:35:42 -08001380 pc_factory_->CreateLocalMediaStream(kStreamLabel1));
1381 std::vector<MediaStreamInterface*> stream_list;
1382 stream_list.push_back(stream.get());
zhihuang9763d562016-08-05 11:14:50 -07001383 rtc::scoped_refptr<AudioTrackInterface> audio_track(
deadbeefe1f9d832016-01-14 15:35:42 -08001384 pc_factory_->CreateAudioTrack("audio_track", nullptr));
zhihuang9763d562016-08-05 11:14:50 -07001385 rtc::scoped_refptr<VideoTrackInterface> video_track(
1386 pc_factory_->CreateVideoTrack(
deadbeef112b2e92017-02-10 20:13:37 -08001387 "video_track", pc_factory_->CreateVideoSource(
1388 std::unique_ptr<cricket::VideoCapturer>(
1389 new cricket::FakeVideoCapturer()))));
deadbeefe1f9d832016-01-14 15:35:42 -08001390 auto audio_sender = pc_->AddTrack(audio_track, stream_list);
1391 auto video_sender = pc_->AddTrack(video_track, stream_list);
deadbeefa601f5c2016-06-06 14:27:39 -07001392 EXPECT_EQ(1UL, audio_sender->stream_ids().size());
1393 EXPECT_EQ(kStreamLabel1, audio_sender->stream_ids()[0]);
deadbeefe1f9d832016-01-14 15:35:42 -08001394 EXPECT_EQ("audio_track", audio_sender->id());
1395 EXPECT_EQ(audio_track, audio_sender->track());
deadbeefa601f5c2016-06-06 14:27:39 -07001396 EXPECT_EQ(1UL, video_sender->stream_ids().size());
1397 EXPECT_EQ(kStreamLabel1, video_sender->stream_ids()[0]);
deadbeefe1f9d832016-01-14 15:35:42 -08001398 EXPECT_EQ("video_track", video_sender->id());
1399 EXPECT_EQ(video_track, video_sender->track());
1400
1401 // Now create an offer and check for the senders.
kwibergd1fe2812016-04-27 06:47:29 -07001402 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001403 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefe1f9d832016-01-14 15:35:42 -08001404
1405 const cricket::ContentInfo* audio_content =
1406 cricket::GetFirstAudioContent(offer->description());
1407 const cricket::AudioContentDescription* audio_desc =
1408 static_cast<const cricket::AudioContentDescription*>(
1409 audio_content->description);
1410 EXPECT_TRUE(
1411 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
1412
1413 const cricket::ContentInfo* video_content =
1414 cricket::GetFirstVideoContent(offer->description());
1415 const cricket::VideoContentDescription* video_desc =
1416 static_cast<const cricket::VideoContentDescription*>(
1417 video_content->description);
1418 EXPECT_TRUE(
1419 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1420
1421 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
1422
1423 // Now try removing the tracks.
1424 EXPECT_TRUE(pc_->RemoveTrack(audio_sender));
1425 EXPECT_TRUE(pc_->RemoveTrack(video_sender));
1426
1427 // Create a new offer and ensure it doesn't contain the removed senders.
kwiberg2bbff992016-03-16 11:03:04 -07001428 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefe1f9d832016-01-14 15:35:42 -08001429
1430 audio_content = cricket::GetFirstAudioContent(offer->description());
1431 audio_desc = static_cast<const cricket::AudioContentDescription*>(
1432 audio_content->description);
1433 EXPECT_FALSE(
1434 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
1435
1436 video_content = cricket::GetFirstVideoContent(offer->description());
1437 video_desc = static_cast<const cricket::VideoContentDescription*>(
1438 video_content->description);
1439 EXPECT_FALSE(
1440 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1441
1442 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
1443
1444 // Calling RemoveTrack on a sender no longer attached to a PeerConnection
1445 // should return false.
1446 EXPECT_FALSE(pc_->RemoveTrack(audio_sender));
1447 EXPECT_FALSE(pc_->RemoveTrack(video_sender));
1448}
1449
1450// Test creating senders without a stream specified,
1451// expecting a random stream ID to be generated.
1452TEST_F(PeerConnectionInterfaceTest, AddTrackWithoutStream) {
deadbeef293e9262017-01-11 12:28:30 -08001453 CreatePeerConnectionWithoutDtls();
deadbeefe1f9d832016-01-14 15:35:42 -08001454 // Create a dummy stream, so tracks share a stream label.
zhihuang9763d562016-08-05 11:14:50 -07001455 rtc::scoped_refptr<AudioTrackInterface> audio_track(
deadbeefe1f9d832016-01-14 15:35:42 -08001456 pc_factory_->CreateAudioTrack("audio_track", nullptr));
zhihuang9763d562016-08-05 11:14:50 -07001457 rtc::scoped_refptr<VideoTrackInterface> video_track(
1458 pc_factory_->CreateVideoTrack(
deadbeef112b2e92017-02-10 20:13:37 -08001459 "video_track", pc_factory_->CreateVideoSource(
1460 std::unique_ptr<cricket::VideoCapturer>(
1461 new cricket::FakeVideoCapturer()))));
deadbeefe1f9d832016-01-14 15:35:42 -08001462 auto audio_sender =
1463 pc_->AddTrack(audio_track, std::vector<MediaStreamInterface*>());
1464 auto video_sender =
1465 pc_->AddTrack(video_track, std::vector<MediaStreamInterface*>());
1466 EXPECT_EQ("audio_track", audio_sender->id());
1467 EXPECT_EQ(audio_track, audio_sender->track());
1468 EXPECT_EQ("video_track", video_sender->id());
1469 EXPECT_EQ(video_track, video_sender->track());
1470 // If the ID is truly a random GUID, it should be infinitely unlikely they
1471 // will be the same.
deadbeefa601f5c2016-06-06 14:27:39 -07001472 EXPECT_NE(video_sender->stream_ids(), audio_sender->stream_ids());
deadbeefe1f9d832016-01-14 15:35:42 -08001473}
1474
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001475TEST_F(PeerConnectionInterfaceTest, CreateOfferReceiveAnswer) {
1476 InitiateCall();
1477 WaitAndVerifyOnAddStream(kStreamLabel1);
1478 VerifyRemoteRtpHeaderExtensions();
1479}
1480
1481TEST_F(PeerConnectionInterfaceTest, CreateOfferReceivePrAnswerAndAnswer) {
deadbeef293e9262017-01-11 12:28:30 -08001482 CreatePeerConnectionWithoutDtls();
deadbeefab9b2d12015-10-14 11:33:11 -07001483 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001484 CreateOfferAsLocalDescription();
1485 std::string offer;
1486 EXPECT_TRUE(pc_->local_description()->ToString(&offer));
1487 CreatePrAnswerAndAnswerAsRemoteDescription(offer);
1488 WaitAndVerifyOnAddStream(kStreamLabel1);
1489}
1490
1491TEST_F(PeerConnectionInterfaceTest, ReceiveOfferCreateAnswer) {
deadbeef293e9262017-01-11 12:28:30 -08001492 CreatePeerConnectionWithoutDtls();
deadbeefab9b2d12015-10-14 11:33:11 -07001493 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001494
1495 CreateOfferAsRemoteDescription();
1496 CreateAnswerAsLocalDescription();
1497
1498 WaitAndVerifyOnAddStream(kStreamLabel1);
1499}
1500
1501TEST_F(PeerConnectionInterfaceTest, ReceiveOfferCreatePrAnswerAndAnswer) {
deadbeef293e9262017-01-11 12:28:30 -08001502 CreatePeerConnectionWithoutDtls();
deadbeefab9b2d12015-10-14 11:33:11 -07001503 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001504
1505 CreateOfferAsRemoteDescription();
1506 CreatePrAnswerAsLocalDescription();
1507 CreateAnswerAsLocalDescription();
1508
1509 WaitAndVerifyOnAddStream(kStreamLabel1);
1510}
1511
1512TEST_F(PeerConnectionInterfaceTest, Renegotiate) {
1513 InitiateCall();
1514 ASSERT_EQ(1u, pc_->remote_streams()->count());
1515 pc_->RemoveStream(pc_->local_streams()->at(0));
1516 CreateOfferReceiveAnswer();
1517 EXPECT_EQ(0u, pc_->remote_streams()->count());
deadbeefab9b2d12015-10-14 11:33:11 -07001518 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001519 CreateOfferReceiveAnswer();
1520}
1521
1522// Tests that after negotiating an audio only call, the respondent can perform a
1523// renegotiation that removes the audio stream.
1524TEST_F(PeerConnectionInterfaceTest, RenegotiateAudioOnly) {
deadbeef293e9262017-01-11 12:28:30 -08001525 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001526 AddVoiceStream(kStreamLabel1);
1527 CreateOfferAsRemoteDescription();
1528 CreateAnswerAsLocalDescription();
1529
1530 ASSERT_EQ(1u, pc_->remote_streams()->count());
1531 pc_->RemoveStream(pc_->local_streams()->at(0));
1532 CreateOfferReceiveAnswer();
1533 EXPECT_EQ(0u, pc_->remote_streams()->count());
1534}
1535
1536// Test that candidates are generated and that we can parse our own candidates.
1537TEST_F(PeerConnectionInterfaceTest, IceCandidates) {
deadbeef293e9262017-01-11 12:28:30 -08001538 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001539
1540 EXPECT_FALSE(pc_->AddIceCandidate(observer_.last_candidate_.get()));
1541 // SetRemoteDescription takes ownership of offer.
kwibergd1fe2812016-04-27 06:47:29 -07001542 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefab9b2d12015-10-14 11:33:11 -07001543 AddVideoStream(kStreamLabel1);
deadbeefc80741f2015-10-22 13:14:45 -07001544 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
kwiberg2bbff992016-03-16 11:03:04 -07001545 EXPECT_TRUE(DoSetRemoteDescription(offer.release()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001546
1547 // SetLocalDescription takes ownership of answer.
kwibergd1fe2812016-04-27 06:47:29 -07001548 std::unique_ptr<SessionDescriptionInterface> answer;
deadbeefc80741f2015-10-22 13:14:45 -07001549 EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
kwiberg2bbff992016-03-16 11:03:04 -07001550 EXPECT_TRUE(DoSetLocalDescription(answer.release()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001551
1552 EXPECT_TRUE_WAIT(observer_.last_candidate_.get() != NULL, kTimeout);
1553 EXPECT_TRUE_WAIT(observer_.ice_complete_, kTimeout);
1554
1555 EXPECT_TRUE(pc_->AddIceCandidate(observer_.last_candidate_.get()));
1556}
1557
deadbeefab9b2d12015-10-14 11:33:11 -07001558// Test that CreateOffer and CreateAnswer will fail if the track labels are
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001559// not unique.
1560TEST_F(PeerConnectionInterfaceTest, CreateOfferAnswerWithInvalidStream) {
deadbeef293e9262017-01-11 12:28:30 -08001561 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001562 // Create a regular offer for the CreateAnswer test later.
kwibergd1fe2812016-04-27 06:47:29 -07001563 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefc80741f2015-10-22 13:14:45 -07001564 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
kwiberg2bbff992016-03-16 11:03:04 -07001565 EXPECT_TRUE(offer);
1566 offer.reset();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001567
1568 // Create a local stream with audio&video tracks having same label.
1569 AddAudioVideoStream(kStreamLabel1, "track_label", "track_label");
1570
1571 // Test CreateOffer
deadbeefc80741f2015-10-22 13:14:45 -07001572 EXPECT_FALSE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001573
1574 // Test CreateAnswer
kwibergd1fe2812016-04-27 06:47:29 -07001575 std::unique_ptr<SessionDescriptionInterface> answer;
deadbeefc80741f2015-10-22 13:14:45 -07001576 EXPECT_FALSE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001577}
1578
1579// Test that we will get different SSRCs for each tracks in the offer and answer
1580// we created.
1581TEST_F(PeerConnectionInterfaceTest, SsrcInOfferAnswer) {
deadbeef293e9262017-01-11 12:28:30 -08001582 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001583 // Create a local stream with audio&video tracks having different labels.
1584 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
1585
1586 // Test CreateOffer
kwibergd1fe2812016-04-27 06:47:29 -07001587 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001588 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001589 int audio_ssrc = 0;
1590 int video_ssrc = 0;
1591 EXPECT_TRUE(GetFirstSsrc(GetFirstAudioContent(offer->description()),
1592 &audio_ssrc));
1593 EXPECT_TRUE(GetFirstSsrc(GetFirstVideoContent(offer->description()),
1594 &video_ssrc));
1595 EXPECT_NE(audio_ssrc, video_ssrc);
1596
1597 // Test CreateAnswer
1598 EXPECT_TRUE(DoSetRemoteDescription(offer.release()));
kwibergd1fe2812016-04-27 06:47:29 -07001599 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -07001600 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001601 audio_ssrc = 0;
1602 video_ssrc = 0;
1603 EXPECT_TRUE(GetFirstSsrc(GetFirstAudioContent(answer->description()),
1604 &audio_ssrc));
1605 EXPECT_TRUE(GetFirstSsrc(GetFirstVideoContent(answer->description()),
1606 &video_ssrc));
1607 EXPECT_NE(audio_ssrc, video_ssrc);
1608}
1609
deadbeefeb459812015-12-15 19:24:43 -08001610// Test that it's possible to call AddTrack on a MediaStream after adding
1611// the stream to a PeerConnection.
1612// TODO(deadbeef): Remove this test once this behavior is no longer supported.
1613TEST_F(PeerConnectionInterfaceTest, AddTrackAfterAddStream) {
deadbeef293e9262017-01-11 12:28:30 -08001614 CreatePeerConnectionWithoutDtls();
deadbeefeb459812015-12-15 19:24:43 -08001615 // Create audio stream and add to PeerConnection.
1616 AddVoiceStream(kStreamLabel1);
1617 MediaStreamInterface* stream = pc_->local_streams()->at(0);
1618
1619 // Add video track to the audio-only stream.
zhihuang9763d562016-08-05 11:14:50 -07001620 rtc::scoped_refptr<VideoTrackInterface> video_track(
1621 pc_factory_->CreateVideoTrack(
deadbeef112b2e92017-02-10 20:13:37 -08001622 "video_label", pc_factory_->CreateVideoSource(
1623 std::unique_ptr<cricket::VideoCapturer>(
1624 new cricket::FakeVideoCapturer()))));
deadbeefeb459812015-12-15 19:24:43 -08001625 stream->AddTrack(video_track.get());
1626
kwibergd1fe2812016-04-27 06:47:29 -07001627 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001628 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefeb459812015-12-15 19:24:43 -08001629
1630 const cricket::MediaContentDescription* video_desc =
1631 cricket::GetFirstVideoContentDescription(offer->description());
1632 EXPECT_TRUE(video_desc != nullptr);
1633}
1634
1635// Test that it's possible to call RemoveTrack on a MediaStream after adding
1636// the stream to a PeerConnection.
1637// TODO(deadbeef): Remove this test once this behavior is no longer supported.
1638TEST_F(PeerConnectionInterfaceTest, RemoveTrackAfterAddStream) {
deadbeef293e9262017-01-11 12:28:30 -08001639 CreatePeerConnectionWithoutDtls();
deadbeefeb459812015-12-15 19:24:43 -08001640 // Create audio/video stream and add to PeerConnection.
1641 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
1642 MediaStreamInterface* stream = pc_->local_streams()->at(0);
1643
1644 // Remove the video track.
1645 stream->RemoveTrack(stream->GetVideoTracks()[0]);
1646
kwibergd1fe2812016-04-27 06:47:29 -07001647 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001648 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefeb459812015-12-15 19:24:43 -08001649
1650 const cricket::MediaContentDescription* video_desc =
1651 cricket::GetFirstVideoContentDescription(offer->description());
1652 EXPECT_TRUE(video_desc == nullptr);
1653}
1654
deadbeef1dcb1642017-03-29 21:08:16 -07001655// Verify that CreateDtmfSender only succeeds if called with a valid local
1656// track. Other aspects of DtmfSenders are tested in
1657// peerconnection_integrationtest.cc.
1658TEST_F(PeerConnectionInterfaceTest, CreateDtmfSenderWithInvalidParams) {
1659 CreatePeerConnection();
1660 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
1661 EXPECT_EQ(nullptr, pc_->CreateDtmfSender(nullptr));
1662 rtc::scoped_refptr<webrtc::AudioTrackInterface> non_localtrack(
1663 pc_factory_->CreateAudioTrack("dummy_track", nullptr));
1664 EXPECT_EQ(nullptr, pc_->CreateDtmfSender(non_localtrack));
1665}
1666
deadbeefbd7d8f72015-12-18 16:58:44 -08001667// Test creating a sender with a stream ID, and ensure the ID is populated
1668// in the offer.
1669TEST_F(PeerConnectionInterfaceTest, CreateSenderWithStream) {
deadbeef293e9262017-01-11 12:28:30 -08001670 CreatePeerConnectionWithoutDtls();
deadbeefbd7d8f72015-12-18 16:58:44 -08001671 pc_->CreateSender("video", kStreamLabel1);
1672
kwibergd1fe2812016-04-27 06:47:29 -07001673 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001674 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefbd7d8f72015-12-18 16:58:44 -08001675
1676 const cricket::MediaContentDescription* video_desc =
1677 cricket::GetFirstVideoContentDescription(offer->description());
1678 ASSERT_TRUE(video_desc != nullptr);
1679 ASSERT_EQ(1u, video_desc->streams().size());
1680 EXPECT_EQ(kStreamLabel1, video_desc->streams()[0].sync_label);
1681}
1682
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001683// Test that we can specify a certain track that we want statistics about.
1684TEST_F(PeerConnectionInterfaceTest, GetStatsForSpecificTrack) {
1685 InitiateCall();
1686 ASSERT_LT(0u, pc_->remote_streams()->count());
1687 ASSERT_LT(0u, pc_->remote_streams()->at(0)->GetAudioTracks().size());
zhihuang9763d562016-08-05 11:14:50 -07001688 rtc::scoped_refptr<MediaStreamTrackInterface> remote_audio =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001689 pc_->remote_streams()->at(0)->GetAudioTracks()[0];
1690 EXPECT_TRUE(DoGetStats(remote_audio));
1691
1692 // Remove the stream. Since we are sending to our selves the local
1693 // and the remote stream is the same.
1694 pc_->RemoveStream(pc_->local_streams()->at(0));
1695 // Do a re-negotiation.
1696 CreateOfferReceiveAnswer();
1697
1698 ASSERT_EQ(0u, pc_->remote_streams()->count());
1699
1700 // Test that we still can get statistics for the old track. Even if it is not
1701 // sent any longer.
1702 EXPECT_TRUE(DoGetStats(remote_audio));
1703}
1704
1705// Test that we can get stats on a video track.
1706TEST_F(PeerConnectionInterfaceTest, GetStatsForVideoTrack) {
1707 InitiateCall();
1708 ASSERT_LT(0u, pc_->remote_streams()->count());
1709 ASSERT_LT(0u, pc_->remote_streams()->at(0)->GetVideoTracks().size());
zhihuang9763d562016-08-05 11:14:50 -07001710 rtc::scoped_refptr<MediaStreamTrackInterface> remote_video =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001711 pc_->remote_streams()->at(0)->GetVideoTracks()[0];
1712 EXPECT_TRUE(DoGetStats(remote_video));
1713}
1714
1715// Test that we don't get statistics for an invalid track.
zhihuange9e94c32016-11-04 11:38:15 -07001716TEST_F(PeerConnectionInterfaceTest, GetStatsForInvalidTrack) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001717 InitiateCall();
zhihuang9763d562016-08-05 11:14:50 -07001718 rtc::scoped_refptr<AudioTrackInterface> unknown_audio_track(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001719 pc_factory_->CreateAudioTrack("unknown track", NULL));
1720 EXPECT_FALSE(DoGetStats(unknown_audio_track));
1721}
1722
1723// This test setup two RTP data channels in loop back.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001724TEST_F(PeerConnectionInterfaceTest, TestDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001725 FakeConstraints constraints;
1726 constraints.SetAllowRtpDataChannels();
1727 CreatePeerConnection(&constraints);
zhihuang9763d562016-08-05 11:14:50 -07001728 rtc::scoped_refptr<DataChannelInterface> data1 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001729 pc_->CreateDataChannel("test1", NULL);
zhihuang9763d562016-08-05 11:14:50 -07001730 rtc::scoped_refptr<DataChannelInterface> data2 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001731 pc_->CreateDataChannel("test2", NULL);
1732 ASSERT_TRUE(data1 != NULL);
kwibergd1fe2812016-04-27 06:47:29 -07001733 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001734 new MockDataChannelObserver(data1));
kwibergd1fe2812016-04-27 06:47:29 -07001735 std::unique_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001736 new MockDataChannelObserver(data2));
1737
1738 EXPECT_EQ(DataChannelInterface::kConnecting, data1->state());
1739 EXPECT_EQ(DataChannelInterface::kConnecting, data2->state());
1740 std::string data_to_send1 = "testing testing";
1741 std::string data_to_send2 = "testing something else";
1742 EXPECT_FALSE(data1->Send(DataBuffer(data_to_send1)));
1743
1744 CreateOfferReceiveAnswer();
1745 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1746 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
1747
1748 EXPECT_EQ(DataChannelInterface::kOpen, data1->state());
1749 EXPECT_EQ(DataChannelInterface::kOpen, data2->state());
1750 EXPECT_TRUE(data1->Send(DataBuffer(data_to_send1)));
1751 EXPECT_TRUE(data2->Send(DataBuffer(data_to_send2)));
1752
1753 EXPECT_EQ_WAIT(data_to_send1, observer1->last_message(), kTimeout);
1754 EXPECT_EQ_WAIT(data_to_send2, observer2->last_message(), kTimeout);
1755
1756 data1->Close();
1757 EXPECT_EQ(DataChannelInterface::kClosing, data1->state());
1758 CreateOfferReceiveAnswer();
1759 EXPECT_FALSE(observer1->IsOpen());
1760 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
1761 EXPECT_TRUE(observer2->IsOpen());
1762
1763 data_to_send2 = "testing something else again";
1764 EXPECT_TRUE(data2->Send(DataBuffer(data_to_send2)));
1765
1766 EXPECT_EQ_WAIT(data_to_send2, observer2->last_message(), kTimeout);
1767}
1768
1769// This test verifies that sendnig binary data over RTP data channels should
1770// fail.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001771TEST_F(PeerConnectionInterfaceTest, TestSendBinaryOnRtpDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001772 FakeConstraints constraints;
1773 constraints.SetAllowRtpDataChannels();
1774 CreatePeerConnection(&constraints);
zhihuang9763d562016-08-05 11:14:50 -07001775 rtc::scoped_refptr<DataChannelInterface> data1 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001776 pc_->CreateDataChannel("test1", NULL);
zhihuang9763d562016-08-05 11:14:50 -07001777 rtc::scoped_refptr<DataChannelInterface> data2 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001778 pc_->CreateDataChannel("test2", NULL);
1779 ASSERT_TRUE(data1 != NULL);
kwibergd1fe2812016-04-27 06:47:29 -07001780 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001781 new MockDataChannelObserver(data1));
kwibergd1fe2812016-04-27 06:47:29 -07001782 std::unique_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001783 new MockDataChannelObserver(data2));
1784
1785 EXPECT_EQ(DataChannelInterface::kConnecting, data1->state());
1786 EXPECT_EQ(DataChannelInterface::kConnecting, data2->state());
1787
1788 CreateOfferReceiveAnswer();
1789 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1790 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
1791
1792 EXPECT_EQ(DataChannelInterface::kOpen, data1->state());
1793 EXPECT_EQ(DataChannelInterface::kOpen, data2->state());
1794
jbaucheec21bd2016-03-20 06:15:43 -07001795 rtc::CopyOnWriteBuffer buffer("test", 4);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001796 EXPECT_FALSE(data1->Send(DataBuffer(buffer, true)));
1797}
1798
1799// This test setup a RTP data channels in loop back and test that a channel is
1800// opened even if the remote end answer with a zero SSRC.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001801TEST_F(PeerConnectionInterfaceTest, TestSendOnlyDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001802 FakeConstraints constraints;
1803 constraints.SetAllowRtpDataChannels();
1804 CreatePeerConnection(&constraints);
zhihuang9763d562016-08-05 11:14:50 -07001805 rtc::scoped_refptr<DataChannelInterface> data1 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001806 pc_->CreateDataChannel("test1", NULL);
kwibergd1fe2812016-04-27 06:47:29 -07001807 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001808 new MockDataChannelObserver(data1));
1809
1810 CreateOfferReceiveAnswerWithoutSsrc();
1811
1812 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1813
1814 data1->Close();
1815 EXPECT_EQ(DataChannelInterface::kClosing, data1->state());
1816 CreateOfferReceiveAnswerWithoutSsrc();
1817 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
1818 EXPECT_FALSE(observer1->IsOpen());
1819}
1820
1821// This test that if a data channel is added in an answer a receive only channel
1822// channel is created.
1823TEST_F(PeerConnectionInterfaceTest, TestReceiveOnlyDataChannel) {
1824 FakeConstraints constraints;
1825 constraints.SetAllowRtpDataChannels();
1826 CreatePeerConnection(&constraints);
1827
1828 std::string offer_label = "offer_channel";
zhihuang9763d562016-08-05 11:14:50 -07001829 rtc::scoped_refptr<DataChannelInterface> offer_channel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001830 pc_->CreateDataChannel(offer_label, NULL);
1831
1832 CreateOfferAsLocalDescription();
1833
1834 // Replace the data channel label in the offer and apply it as an answer.
1835 std::string receive_label = "answer_channel";
1836 std::string sdp;
1837 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001838 rtc::replace_substrs(offer_label.c_str(), offer_label.length(),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001839 receive_label.c_str(), receive_label.length(),
1840 &sdp);
1841 CreateAnswerAsRemoteDescription(sdp);
1842
1843 // Verify that a new incoming data channel has been created and that
1844 // it is open but can't we written to.
1845 ASSERT_TRUE(observer_.last_datachannel_ != NULL);
1846 DataChannelInterface* received_channel = observer_.last_datachannel_;
1847 EXPECT_EQ(DataChannelInterface::kConnecting, received_channel->state());
1848 EXPECT_EQ(receive_label, received_channel->label());
1849 EXPECT_FALSE(received_channel->Send(DataBuffer("something")));
1850
1851 // Verify that the channel we initially offered has been rejected.
1852 EXPECT_EQ(DataChannelInterface::kClosed, offer_channel->state());
1853
1854 // Do another offer / answer exchange and verify that the data channel is
1855 // opened.
1856 CreateOfferReceiveAnswer();
1857 EXPECT_EQ_WAIT(DataChannelInterface::kOpen, received_channel->state(),
1858 kTimeout);
1859}
1860
1861// This test that no data channel is returned if a reliable channel is
1862// requested.
1863// TODO(perkj): Remove this test once reliable channels are implemented.
1864TEST_F(PeerConnectionInterfaceTest, CreateReliableRtpDataChannelShouldFail) {
1865 FakeConstraints constraints;
1866 constraints.SetAllowRtpDataChannels();
1867 CreatePeerConnection(&constraints);
1868
1869 std::string label = "test";
1870 webrtc::DataChannelInit config;
1871 config.reliable = true;
zhihuang9763d562016-08-05 11:14:50 -07001872 rtc::scoped_refptr<DataChannelInterface> channel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001873 pc_->CreateDataChannel(label, &config);
1874 EXPECT_TRUE(channel == NULL);
1875}
1876
deadbeefab9b2d12015-10-14 11:33:11 -07001877// Verifies that duplicated label is not allowed for RTP data channel.
1878TEST_F(PeerConnectionInterfaceTest, RtpDuplicatedLabelNotAllowed) {
1879 FakeConstraints constraints;
1880 constraints.SetAllowRtpDataChannels();
1881 CreatePeerConnection(&constraints);
1882
1883 std::string label = "test";
zhihuang9763d562016-08-05 11:14:50 -07001884 rtc::scoped_refptr<DataChannelInterface> channel =
deadbeefab9b2d12015-10-14 11:33:11 -07001885 pc_->CreateDataChannel(label, nullptr);
1886 EXPECT_NE(channel, nullptr);
1887
zhihuang9763d562016-08-05 11:14:50 -07001888 rtc::scoped_refptr<DataChannelInterface> dup_channel =
deadbeefab9b2d12015-10-14 11:33:11 -07001889 pc_->CreateDataChannel(label, nullptr);
1890 EXPECT_EQ(dup_channel, nullptr);
1891}
1892
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001893// This tests that a SCTP data channel is returned using different
1894// DataChannelInit configurations.
1895TEST_F(PeerConnectionInterfaceTest, CreateSctpDataChannel) {
1896 FakeConstraints constraints;
1897 constraints.SetAllowDtlsSctpDataChannels();
1898 CreatePeerConnection(&constraints);
1899
1900 webrtc::DataChannelInit config;
1901
zhihuang9763d562016-08-05 11:14:50 -07001902 rtc::scoped_refptr<DataChannelInterface> channel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001903 pc_->CreateDataChannel("1", &config);
1904 EXPECT_TRUE(channel != NULL);
1905 EXPECT_TRUE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001906 EXPECT_TRUE(observer_.renegotiation_needed_);
1907 observer_.renegotiation_needed_ = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001908
1909 config.ordered = false;
1910 channel = pc_->CreateDataChannel("2", &config);
1911 EXPECT_TRUE(channel != NULL);
1912 EXPECT_TRUE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001913 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001914
1915 config.ordered = true;
1916 config.maxRetransmits = 0;
1917 channel = pc_->CreateDataChannel("3", &config);
1918 EXPECT_TRUE(channel != NULL);
1919 EXPECT_FALSE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001920 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001921
1922 config.maxRetransmits = -1;
1923 config.maxRetransmitTime = 0;
1924 channel = pc_->CreateDataChannel("4", &config);
1925 EXPECT_TRUE(channel != NULL);
1926 EXPECT_FALSE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001927 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001928}
1929
1930// This tests that no data channel is returned if both maxRetransmits and
1931// maxRetransmitTime are set for SCTP data channels.
1932TEST_F(PeerConnectionInterfaceTest,
1933 CreateSctpDataChannelShouldFailForInvalidConfig) {
1934 FakeConstraints constraints;
1935 constraints.SetAllowDtlsSctpDataChannels();
1936 CreatePeerConnection(&constraints);
1937
1938 std::string label = "test";
1939 webrtc::DataChannelInit config;
1940 config.maxRetransmits = 0;
1941 config.maxRetransmitTime = 0;
1942
zhihuang9763d562016-08-05 11:14:50 -07001943 rtc::scoped_refptr<DataChannelInterface> channel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001944 pc_->CreateDataChannel(label, &config);
1945 EXPECT_TRUE(channel == NULL);
1946}
1947
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001948// The test verifies that creating a SCTP data channel with an id already in use
1949// or out of range should fail.
1950TEST_F(PeerConnectionInterfaceTest,
1951 CreateSctpDataChannelWithInvalidIdShouldFail) {
1952 FakeConstraints constraints;
1953 constraints.SetAllowDtlsSctpDataChannels();
1954 CreatePeerConnection(&constraints);
1955
1956 webrtc::DataChannelInit config;
zhihuang9763d562016-08-05 11:14:50 -07001957 rtc::scoped_refptr<DataChannelInterface> channel;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001958
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001959 config.id = 1;
1960 channel = pc_->CreateDataChannel("1", &config);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001961 EXPECT_TRUE(channel != NULL);
1962 EXPECT_EQ(1, channel->id());
1963
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001964 channel = pc_->CreateDataChannel("x", &config);
1965 EXPECT_TRUE(channel == NULL);
1966
1967 config.id = cricket::kMaxSctpSid;
1968 channel = pc_->CreateDataChannel("max", &config);
1969 EXPECT_TRUE(channel != NULL);
1970 EXPECT_EQ(config.id, channel->id());
1971
1972 config.id = cricket::kMaxSctpSid + 1;
1973 channel = pc_->CreateDataChannel("x", &config);
1974 EXPECT_TRUE(channel == NULL);
1975}
1976
deadbeefab9b2d12015-10-14 11:33:11 -07001977// Verifies that duplicated label is allowed for SCTP data channel.
1978TEST_F(PeerConnectionInterfaceTest, SctpDuplicatedLabelAllowed) {
1979 FakeConstraints constraints;
1980 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
1981 true);
1982 CreatePeerConnection(&constraints);
1983
1984 std::string label = "test";
zhihuang9763d562016-08-05 11:14:50 -07001985 rtc::scoped_refptr<DataChannelInterface> channel =
deadbeefab9b2d12015-10-14 11:33:11 -07001986 pc_->CreateDataChannel(label, nullptr);
1987 EXPECT_NE(channel, nullptr);
1988
zhihuang9763d562016-08-05 11:14:50 -07001989 rtc::scoped_refptr<DataChannelInterface> dup_channel =
deadbeefab9b2d12015-10-14 11:33:11 -07001990 pc_->CreateDataChannel(label, nullptr);
1991 EXPECT_NE(dup_channel, nullptr);
1992}
1993
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001994// This test verifies that OnRenegotiationNeeded is fired for every new RTP
1995// DataChannel.
1996TEST_F(PeerConnectionInterfaceTest, RenegotiationNeededForNewRtpDataChannel) {
1997 FakeConstraints constraints;
1998 constraints.SetAllowRtpDataChannels();
1999 CreatePeerConnection(&constraints);
2000
zhihuang9763d562016-08-05 11:14:50 -07002001 rtc::scoped_refptr<DataChannelInterface> dc1 =
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00002002 pc_->CreateDataChannel("test1", NULL);
2003 EXPECT_TRUE(observer_.renegotiation_needed_);
2004 observer_.renegotiation_needed_ = false;
2005
zhihuang9763d562016-08-05 11:14:50 -07002006 rtc::scoped_refptr<DataChannelInterface> dc2 =
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00002007 pc_->CreateDataChannel("test2", NULL);
2008 EXPECT_TRUE(observer_.renegotiation_needed_);
2009}
2010
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002011// This test that a data channel closes when a PeerConnection is deleted/closed.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002012TEST_F(PeerConnectionInterfaceTest, DataChannelCloseWhenPeerConnectionClose) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002013 FakeConstraints constraints;
2014 constraints.SetAllowRtpDataChannels();
2015 CreatePeerConnection(&constraints);
2016
zhihuang9763d562016-08-05 11:14:50 -07002017 rtc::scoped_refptr<DataChannelInterface> data1 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002018 pc_->CreateDataChannel("test1", NULL);
zhihuang9763d562016-08-05 11:14:50 -07002019 rtc::scoped_refptr<DataChannelInterface> data2 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002020 pc_->CreateDataChannel("test2", NULL);
2021 ASSERT_TRUE(data1 != NULL);
kwibergd1fe2812016-04-27 06:47:29 -07002022 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002023 new MockDataChannelObserver(data1));
kwibergd1fe2812016-04-27 06:47:29 -07002024 std::unique_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002025 new MockDataChannelObserver(data2));
2026
2027 CreateOfferReceiveAnswer();
2028 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
2029 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
2030
2031 ReleasePeerConnection();
2032 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
2033 EXPECT_EQ(DataChannelInterface::kClosed, data2->state());
2034}
2035
2036// This test that data channels can be rejected in an answer.
2037TEST_F(PeerConnectionInterfaceTest, TestRejectDataChannelInAnswer) {
2038 FakeConstraints constraints;
2039 constraints.SetAllowRtpDataChannels();
2040 CreatePeerConnection(&constraints);
2041
zhihuang9763d562016-08-05 11:14:50 -07002042 rtc::scoped_refptr<DataChannelInterface> offer_channel(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002043 pc_->CreateDataChannel("offer_channel", NULL));
2044
2045 CreateOfferAsLocalDescription();
2046
2047 // Create an answer where the m-line for data channels are rejected.
2048 std::string sdp;
2049 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
2050 webrtc::JsepSessionDescription* answer = new webrtc::JsepSessionDescription(
2051 SessionDescriptionInterface::kAnswer);
2052 EXPECT_TRUE(answer->Initialize(sdp, NULL));
2053 cricket::ContentInfo* data_info =
2054 answer->description()->GetContentByName("data");
2055 data_info->rejected = true;
2056
2057 DoSetRemoteDescription(answer);
2058 EXPECT_EQ(DataChannelInterface::kClosed, offer_channel->state());
2059}
2060
2061// Test that we can create a session description from an SDP string from
2062// FireFox, use it as a remote session description, generate an answer and use
2063// the answer as a local description.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002064TEST_F(PeerConnectionInterfaceTest, ReceiveFireFoxOffer) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002065 FakeConstraints constraints;
2066 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2067 true);
2068 CreatePeerConnection(&constraints);
2069 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
2070 SessionDescriptionInterface* desc =
2071 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
jbauchfabe2c92015-07-16 13:43:14 -07002072 webrtc::kFireFoxSdpOffer, nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002073 EXPECT_TRUE(DoSetSessionDescription(desc, false));
2074 CreateAnswerAsLocalDescription();
2075 ASSERT_TRUE(pc_->local_description() != NULL);
2076 ASSERT_TRUE(pc_->remote_description() != NULL);
2077
2078 const cricket::ContentInfo* content =
2079 cricket::GetFirstAudioContent(pc_->local_description()->description());
2080 ASSERT_TRUE(content != NULL);
2081 EXPECT_FALSE(content->rejected);
2082
2083 content =
2084 cricket::GetFirstVideoContent(pc_->local_description()->description());
2085 ASSERT_TRUE(content != NULL);
2086 EXPECT_FALSE(content->rejected);
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00002087#ifdef HAVE_SCTP
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002088 content =
2089 cricket::GetFirstDataContent(pc_->local_description()->description());
2090 ASSERT_TRUE(content != NULL);
2091 EXPECT_TRUE(content->rejected);
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00002092#endif
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002093}
2094
deadbeef8662f942017-01-20 21:20:51 -08002095// Test that an offer can be received which offers DTLS with SDES fallback.
2096// Regression test for issue:
2097// https://bugs.chromium.org/p/webrtc/issues/detail?id=6972
2098TEST_F(PeerConnectionInterfaceTest, ReceiveDtlsSdesFallbackOffer) {
2099 FakeConstraints constraints;
2100 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2101 true);
2102 CreatePeerConnection(&constraints);
2103 // Wait for fake certificate to be generated. Previously, this is what caused
2104 // the "a=crypto" lines to be rejected.
2105 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
2106 ASSERT_NE(nullptr, fake_certificate_generator_);
2107 EXPECT_EQ_WAIT(1, fake_certificate_generator_->generated_certificates(),
2108 kTimeout);
2109 SessionDescriptionInterface* desc = webrtc::CreateSessionDescription(
2110 SessionDescriptionInterface::kOffer, kDtlsSdesFallbackSdp, nullptr);
2111 EXPECT_TRUE(DoSetSessionDescription(desc, false));
2112 CreateAnswerAsLocalDescription();
2113}
2114
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002115// Test that we can create an audio only offer and receive an answer with a
2116// limited set of audio codecs and receive an updated offer with more audio
2117// codecs, where the added codecs are not supported.
2118TEST_F(PeerConnectionInterfaceTest, ReceiveUpdatedAudioOfferWithBadCodecs) {
deadbeef293e9262017-01-11 12:28:30 -08002119 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002120 AddVoiceStream("audio_label");
2121 CreateOfferAsLocalDescription();
2122
2123 SessionDescriptionInterface* answer =
2124 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
jbauchfabe2c92015-07-16 13:43:14 -07002125 webrtc::kAudioSdp, nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002126 EXPECT_TRUE(DoSetSessionDescription(answer, false));
2127
2128 SessionDescriptionInterface* updated_offer =
2129 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
jbauchfabe2c92015-07-16 13:43:14 -07002130 webrtc::kAudioSdpWithUnsupportedCodecs,
2131 nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002132 EXPECT_TRUE(DoSetSessionDescription(updated_offer, false));
2133 CreateAnswerAsLocalDescription();
2134}
2135
deadbeefc80741f2015-10-22 13:14:45 -07002136// Test that if we're receiving (but not sending) a track, subsequent offers
2137// will have m-lines with a=recvonly.
2138TEST_F(PeerConnectionInterfaceTest, CreateSubsequentRecvOnlyOffer) {
2139 FakeConstraints constraints;
2140 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2141 true);
2142 CreatePeerConnection(&constraints);
2143 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2144 CreateAnswerAsLocalDescription();
2145
2146 // At this point we should be receiving stream 1, but not sending anything.
2147 // A new offer should be recvonly.
kwibergd1fe2812016-04-27 06:47:29 -07002148 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefc80741f2015-10-22 13:14:45 -07002149 DoCreateOffer(&offer, nullptr);
2150
2151 const cricket::ContentInfo* video_content =
2152 cricket::GetFirstVideoContent(offer->description());
2153 const cricket::VideoContentDescription* video_desc =
2154 static_cast<const cricket::VideoContentDescription*>(
2155 video_content->description);
2156 ASSERT_EQ(cricket::MD_RECVONLY, video_desc->direction());
2157
2158 const cricket::ContentInfo* audio_content =
2159 cricket::GetFirstAudioContent(offer->description());
2160 const cricket::AudioContentDescription* audio_desc =
2161 static_cast<const cricket::AudioContentDescription*>(
2162 audio_content->description);
2163 ASSERT_EQ(cricket::MD_RECVONLY, audio_desc->direction());
2164}
2165
2166// Test that if we're receiving (but not sending) a track, and the
2167// offerToReceiveVideo/offerToReceiveAudio constraints are explicitly set to
2168// false, the generated m-lines will be a=inactive.
2169TEST_F(PeerConnectionInterfaceTest, CreateSubsequentInactiveOffer) {
2170 FakeConstraints constraints;
2171 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2172 true);
2173 CreatePeerConnection(&constraints);
2174 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2175 CreateAnswerAsLocalDescription();
2176
2177 // At this point we should be receiving stream 1, but not sending anything.
2178 // A new offer would be recvonly, but we'll set the "no receive" constraints
2179 // to make it inactive.
kwibergd1fe2812016-04-27 06:47:29 -07002180 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefc80741f2015-10-22 13:14:45 -07002181 FakeConstraints offer_constraints;
2182 offer_constraints.AddMandatory(
2183 webrtc::MediaConstraintsInterface::kOfferToReceiveVideo, false);
2184 offer_constraints.AddMandatory(
2185 webrtc::MediaConstraintsInterface::kOfferToReceiveAudio, false);
2186 DoCreateOffer(&offer, &offer_constraints);
2187
2188 const cricket::ContentInfo* video_content =
2189 cricket::GetFirstVideoContent(offer->description());
2190 const cricket::VideoContentDescription* video_desc =
2191 static_cast<const cricket::VideoContentDescription*>(
2192 video_content->description);
2193 ASSERT_EQ(cricket::MD_INACTIVE, video_desc->direction());
2194
2195 const cricket::ContentInfo* audio_content =
2196 cricket::GetFirstAudioContent(offer->description());
2197 const cricket::AudioContentDescription* audio_desc =
2198 static_cast<const cricket::AudioContentDescription*>(
2199 audio_content->description);
2200 ASSERT_EQ(cricket::MD_INACTIVE, audio_desc->direction());
2201}
2202
deadbeef653b8e02015-11-11 12:55:10 -08002203// Test that we can use SetConfiguration to change the ICE servers of the
2204// PortAllocator.
2205TEST_F(PeerConnectionInterfaceTest, SetConfigurationChangesIceServers) {
2206 CreatePeerConnection();
2207
2208 PeerConnectionInterface::RTCConfiguration config;
2209 PeerConnectionInterface::IceServer server;
2210 server.uri = "stun:test_hostname";
2211 config.servers.push_back(server);
2212 EXPECT_TRUE(pc_->SetConfiguration(config));
2213
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002214 EXPECT_EQ(1u, port_allocator_->stun_servers().size());
2215 EXPECT_EQ("test_hostname",
2216 port_allocator_->stun_servers().begin()->hostname());
deadbeef653b8e02015-11-11 12:55:10 -08002217}
2218
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002219TEST_F(PeerConnectionInterfaceTest, SetConfigurationChangesCandidateFilter) {
2220 CreatePeerConnection();
2221 PeerConnectionInterface::RTCConfiguration config;
2222 config.type = PeerConnectionInterface::kRelay;
2223 EXPECT_TRUE(pc_->SetConfiguration(config));
2224 EXPECT_EQ(cricket::CF_RELAY, port_allocator_->candidate_filter());
2225}
2226
deadbeef293e9262017-01-11 12:28:30 -08002227TEST_F(PeerConnectionInterfaceTest, SetConfigurationChangesPruneTurnPortsFlag) {
2228 PeerConnectionInterface::RTCConfiguration config;
2229 config.prune_turn_ports = false;
2230 CreatePeerConnection(config, nullptr);
2231 EXPECT_FALSE(port_allocator_->prune_turn_ports());
2232
2233 config.prune_turn_ports = true;
2234 EXPECT_TRUE(pc_->SetConfiguration(config));
2235 EXPECT_TRUE(port_allocator_->prune_turn_ports());
2236}
2237
skvladd1f5fda2017-02-03 16:54:05 -08002238// Test that the ice check interval can be changed. This does not verify that
2239// the setting makes it all the way to P2PTransportChannel, as that would
2240// require a very complex set of mocks.
2241TEST_F(PeerConnectionInterfaceTest, SetConfigurationChangesIceCheckInterval) {
2242 PeerConnectionInterface::RTCConfiguration config;
2243 config.ice_check_min_interval = rtc::Optional<int>();
2244 CreatePeerConnection(config, nullptr);
2245 config.ice_check_min_interval = rtc::Optional<int>(100);
2246 EXPECT_TRUE(pc_->SetConfiguration(config));
2247 PeerConnectionInterface::RTCConfiguration new_config =
2248 pc_->GetConfiguration();
2249 EXPECT_EQ(new_config.ice_check_min_interval, rtc::Optional<int>(100));
2250}
2251
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002252// Test that when SetConfiguration changes both the pool size and other
2253// attributes, the pooled session is created with the updated attributes.
2254TEST_F(PeerConnectionInterfaceTest,
2255 SetConfigurationCreatesPooledSessionCorrectly) {
2256 CreatePeerConnection();
2257 PeerConnectionInterface::RTCConfiguration config;
2258 config.ice_candidate_pool_size = 1;
2259 PeerConnectionInterface::IceServer server;
2260 server.uri = kStunAddressOnly;
2261 config.servers.push_back(server);
2262 config.type = PeerConnectionInterface::kRelay;
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07002263 EXPECT_TRUE(pc_->SetConfiguration(config));
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002264
2265 const cricket::FakePortAllocatorSession* session =
2266 static_cast<const cricket::FakePortAllocatorSession*>(
2267 port_allocator_->GetPooledSession());
2268 ASSERT_NE(nullptr, session);
2269 EXPECT_EQ(1UL, session->stun_servers().size());
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002270}
2271
deadbeef293e9262017-01-11 12:28:30 -08002272// Test that after SetLocalDescription, changing the pool size is not allowed,
2273// and an invalid modification error is returned.
deadbeef6de92f92016-12-12 18:49:32 -08002274TEST_F(PeerConnectionInterfaceTest,
2275 CantChangePoolSizeAfterSetLocalDescription) {
2276 CreatePeerConnection();
2277 // Start by setting a size of 1.
2278 PeerConnectionInterface::RTCConfiguration config;
2279 config.ice_candidate_pool_size = 1;
2280 EXPECT_TRUE(pc_->SetConfiguration(config));
2281
2282 // Set remote offer; can still change pool size at this point.
2283 CreateOfferAsRemoteDescription();
2284 config.ice_candidate_pool_size = 2;
2285 EXPECT_TRUE(pc_->SetConfiguration(config));
2286
2287 // Set local answer; now it's too late.
2288 CreateAnswerAsLocalDescription();
2289 config.ice_candidate_pool_size = 3;
deadbeef293e9262017-01-11 12:28:30 -08002290 RTCError error;
2291 EXPECT_FALSE(pc_->SetConfiguration(config, &error));
2292 EXPECT_EQ(RTCErrorType::INVALID_MODIFICATION, error.type());
2293}
2294
deadbeef42a42632017-03-10 15:18:00 -08002295// Test that after setting an answer, extra pooled sessions are discarded. The
2296// ICE candidate pool is only intended to be used for the first offer/answer.
2297TEST_F(PeerConnectionInterfaceTest,
2298 ExtraPooledSessionsDiscardedAfterApplyingAnswer) {
2299 CreatePeerConnection();
2300
2301 // Set a larger-than-necessary size.
2302 PeerConnectionInterface::RTCConfiguration config;
2303 config.ice_candidate_pool_size = 4;
2304 EXPECT_TRUE(pc_->SetConfiguration(config));
2305
2306 // Do offer/answer.
2307 CreateOfferAsRemoteDescription();
2308 CreateAnswerAsLocalDescription();
2309
2310 // Expect no pooled sessions to be left.
2311 const cricket::PortAllocatorSession* session =
2312 port_allocator_->GetPooledSession();
2313 EXPECT_EQ(nullptr, session);
2314}
2315
2316// After Close is called, pooled candidates should be discarded so as to not
2317// waste network resources.
2318TEST_F(PeerConnectionInterfaceTest, PooledSessionsDiscardedAfterClose) {
2319 CreatePeerConnection();
2320
2321 PeerConnectionInterface::RTCConfiguration config;
2322 config.ice_candidate_pool_size = 3;
2323 EXPECT_TRUE(pc_->SetConfiguration(config));
2324 pc_->Close();
2325
2326 // Expect no pooled sessions to be left.
2327 const cricket::PortAllocatorSession* session =
2328 port_allocator_->GetPooledSession();
2329 EXPECT_EQ(nullptr, session);
2330}
2331
deadbeef293e9262017-01-11 12:28:30 -08002332// Test that SetConfiguration returns an invalid modification error if
2333// modifying a field in the configuration that isn't allowed to be modified.
2334TEST_F(PeerConnectionInterfaceTest,
2335 SetConfigurationReturnsInvalidModificationError) {
2336 PeerConnectionInterface::RTCConfiguration config;
2337 config.bundle_policy = PeerConnectionInterface::kBundlePolicyBalanced;
2338 config.rtcp_mux_policy = PeerConnectionInterface::kRtcpMuxPolicyNegotiate;
2339 config.continual_gathering_policy = PeerConnectionInterface::GATHER_ONCE;
2340 CreatePeerConnection(config, nullptr);
2341
2342 PeerConnectionInterface::RTCConfiguration modified_config = config;
2343 modified_config.bundle_policy =
2344 PeerConnectionInterface::kBundlePolicyMaxBundle;
2345 RTCError error;
2346 EXPECT_FALSE(pc_->SetConfiguration(modified_config, &error));
2347 EXPECT_EQ(RTCErrorType::INVALID_MODIFICATION, error.type());
2348
2349 modified_config = config;
2350 modified_config.rtcp_mux_policy =
2351 PeerConnectionInterface::kRtcpMuxPolicyRequire;
2352 error.set_type(RTCErrorType::NONE);
2353 EXPECT_FALSE(pc_->SetConfiguration(modified_config, &error));
2354 EXPECT_EQ(RTCErrorType::INVALID_MODIFICATION, error.type());
2355
2356 modified_config = config;
2357 modified_config.continual_gathering_policy =
2358 PeerConnectionInterface::GATHER_CONTINUALLY;
2359 error.set_type(RTCErrorType::NONE);
2360 EXPECT_FALSE(pc_->SetConfiguration(modified_config, &error));
2361 EXPECT_EQ(RTCErrorType::INVALID_MODIFICATION, error.type());
2362}
2363
2364// Test that SetConfiguration returns a range error if the candidate pool size
2365// is negative or larger than allowed by the spec.
2366TEST_F(PeerConnectionInterfaceTest,
2367 SetConfigurationReturnsRangeErrorForBadCandidatePoolSize) {
2368 PeerConnectionInterface::RTCConfiguration config;
2369 CreatePeerConnection(config, nullptr);
2370
2371 config.ice_candidate_pool_size = -1;
2372 RTCError error;
2373 EXPECT_FALSE(pc_->SetConfiguration(config, &error));
2374 EXPECT_EQ(RTCErrorType::INVALID_RANGE, error.type());
2375
2376 config.ice_candidate_pool_size = INT_MAX;
2377 error.set_type(RTCErrorType::NONE);
2378 EXPECT_FALSE(pc_->SetConfiguration(config, &error));
2379 EXPECT_EQ(RTCErrorType::INVALID_RANGE, error.type());
2380}
2381
2382// Test that SetConfiguration returns a syntax error if parsing an ICE server
2383// URL failed.
2384TEST_F(PeerConnectionInterfaceTest,
2385 SetConfigurationReturnsSyntaxErrorFromBadIceUrls) {
2386 PeerConnectionInterface::RTCConfiguration config;
2387 CreatePeerConnection(config, nullptr);
2388
2389 PeerConnectionInterface::IceServer bad_server;
2390 bad_server.uri = "stunn:www.example.com";
2391 config.servers.push_back(bad_server);
2392 RTCError error;
2393 EXPECT_FALSE(pc_->SetConfiguration(config, &error));
2394 EXPECT_EQ(RTCErrorType::SYNTAX_ERROR, error.type());
2395}
2396
2397// Test that SetConfiguration returns an invalid parameter error if a TURN
2398// IceServer is missing a username or password.
2399TEST_F(PeerConnectionInterfaceTest,
2400 SetConfigurationReturnsInvalidParameterIfCredentialsMissing) {
2401 PeerConnectionInterface::RTCConfiguration config;
2402 CreatePeerConnection(config, nullptr);
2403
2404 PeerConnectionInterface::IceServer bad_server;
2405 bad_server.uri = "turn:www.example.com";
2406 // Missing password.
2407 bad_server.username = "foo";
2408 config.servers.push_back(bad_server);
2409 RTCError error;
2410 EXPECT_FALSE(pc_->SetConfiguration(config, &error));
2411 EXPECT_EQ(RTCErrorType::INVALID_PARAMETER, error.type());
deadbeef6de92f92016-12-12 18:49:32 -08002412}
2413
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002414// Test that PeerConnection::Close changes the states to closed and all remote
2415// tracks change state to ended.
2416TEST_F(PeerConnectionInterfaceTest, CloseAndTestStreamsAndStates) {
2417 // Initialize a PeerConnection and negotiate local and remote session
2418 // description.
2419 InitiateCall();
2420 ASSERT_EQ(1u, pc_->local_streams()->count());
2421 ASSERT_EQ(1u, pc_->remote_streams()->count());
2422
2423 pc_->Close();
2424
2425 EXPECT_EQ(PeerConnectionInterface::kClosed, pc_->signaling_state());
2426 EXPECT_EQ(PeerConnectionInterface::kIceConnectionClosed,
2427 pc_->ice_connection_state());
2428 EXPECT_EQ(PeerConnectionInterface::kIceGatheringComplete,
2429 pc_->ice_gathering_state());
2430
2431 EXPECT_EQ(1u, pc_->local_streams()->count());
2432 EXPECT_EQ(1u, pc_->remote_streams()->count());
2433
zhihuang9763d562016-08-05 11:14:50 -07002434 rtc::scoped_refptr<MediaStreamInterface> remote_stream =
2435 pc_->remote_streams()->at(0);
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002436 // Track state may be updated asynchronously.
perkjd61bf802016-03-24 03:16:19 -07002437 EXPECT_EQ_WAIT(MediaStreamTrackInterface::kEnded,
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002438 remote_stream->GetAudioTracks()[0]->state(), kTimeout);
2439 EXPECT_EQ_WAIT(MediaStreamTrackInterface::kEnded,
2440 remote_stream->GetVideoTracks()[0]->state(), kTimeout);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002441}
2442
2443// Test that PeerConnection methods fails gracefully after
2444// PeerConnection::Close has been called.
2445TEST_F(PeerConnectionInterfaceTest, CloseAndTestMethods) {
deadbeef293e9262017-01-11 12:28:30 -08002446 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002447 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
2448 CreateOfferAsRemoteDescription();
2449 CreateAnswerAsLocalDescription();
2450
2451 ASSERT_EQ(1u, pc_->local_streams()->count());
zhihuang9763d562016-08-05 11:14:50 -07002452 rtc::scoped_refptr<MediaStreamInterface> local_stream =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002453 pc_->local_streams()->at(0);
2454
2455 pc_->Close();
2456
2457 pc_->RemoveStream(local_stream);
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +00002458 EXPECT_FALSE(pc_->AddStream(local_stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002459
2460 ASSERT_FALSE(local_stream->GetAudioTracks().empty());
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002461 rtc::scoped_refptr<webrtc::DtmfSenderInterface> dtmf_sender(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002462 pc_->CreateDtmfSender(local_stream->GetAudioTracks()[0]));
wu@webrtc.org66037362013-08-13 00:09:35 +00002463 EXPECT_TRUE(NULL == dtmf_sender); // local stream has been removed.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002464
2465 EXPECT_TRUE(pc_->CreateDataChannel("test", NULL) == NULL);
2466
2467 EXPECT_TRUE(pc_->local_description() != NULL);
2468 EXPECT_TRUE(pc_->remote_description() != NULL);
2469
kwibergd1fe2812016-04-27 06:47:29 -07002470 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07002471 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
kwibergd1fe2812016-04-27 06:47:29 -07002472 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -07002473 EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002474
2475 std::string sdp;
2476 ASSERT_TRUE(pc_->remote_description()->ToString(&sdp));
2477 SessionDescriptionInterface* remote_offer =
2478 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
2479 sdp, NULL);
2480 EXPECT_FALSE(DoSetRemoteDescription(remote_offer));
2481
2482 ASSERT_TRUE(pc_->local_description()->ToString(&sdp));
2483 SessionDescriptionInterface* local_offer =
2484 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
2485 sdp, NULL);
2486 EXPECT_FALSE(DoSetLocalDescription(local_offer));
2487}
2488
2489// Test that GetStats can still be called after PeerConnection::Close.
2490TEST_F(PeerConnectionInterfaceTest, CloseAndGetStats) {
2491 InitiateCall();
2492 pc_->Close();
2493 DoGetStats(NULL);
2494}
deadbeefab9b2d12015-10-14 11:33:11 -07002495
2496// NOTE: The series of tests below come from what used to be
2497// mediastreamsignaling_unittest.cc, and are mostly aimed at testing that
2498// setting a remote or local description has the expected effects.
2499
2500// This test verifies that the remote MediaStreams corresponding to a received
2501// SDP string is created. In this test the two separate MediaStreams are
2502// signaled.
2503TEST_F(PeerConnectionInterfaceTest, UpdateRemoteStreams) {
2504 FakeConstraints constraints;
2505 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2506 true);
2507 CreatePeerConnection(&constraints);
2508 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2509
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002510 rtc::scoped_refptr<StreamCollection> reference(CreateStreamCollection(1, 1));
deadbeefab9b2d12015-10-14 11:33:11 -07002511 EXPECT_TRUE(
2512 CompareStreamCollections(observer_.remote_streams(), reference.get()));
2513 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2514 EXPECT_TRUE(remote_stream->GetVideoTracks()[0]->GetSource() != nullptr);
2515
2516 // Create a session description based on another SDP with another
2517 // MediaStream.
2518 CreateAndSetRemoteOffer(kSdpStringWithStream1And2);
2519
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002520 rtc::scoped_refptr<StreamCollection> reference2(CreateStreamCollection(2, 1));
deadbeefab9b2d12015-10-14 11:33:11 -07002521 EXPECT_TRUE(
2522 CompareStreamCollections(observer_.remote_streams(), reference2.get()));
2523}
2524
2525// This test verifies that when remote tracks are added/removed from SDP, the
2526// created remote streams are updated appropriately.
2527TEST_F(PeerConnectionInterfaceTest,
2528 AddRemoveTrackFromExistingRemoteMediaStream) {
2529 FakeConstraints constraints;
2530 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2531 true);
2532 CreatePeerConnection(&constraints);
kwibergd1fe2812016-04-27 06:47:29 -07002533 std::unique_ptr<SessionDescriptionInterface> desc_ms1 =
kwiberg2bbff992016-03-16 11:03:04 -07002534 CreateSessionDescriptionAndReference(1, 1);
deadbeefab9b2d12015-10-14 11:33:11 -07002535 EXPECT_TRUE(DoSetRemoteDescription(desc_ms1.release()));
2536 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
2537 reference_collection_));
2538
2539 // Add extra audio and video tracks to the same MediaStream.
kwibergd1fe2812016-04-27 06:47:29 -07002540 std::unique_ptr<SessionDescriptionInterface> desc_ms1_two_tracks =
kwiberg2bbff992016-03-16 11:03:04 -07002541 CreateSessionDescriptionAndReference(2, 2);
deadbeefab9b2d12015-10-14 11:33:11 -07002542 EXPECT_TRUE(DoSetRemoteDescription(desc_ms1_two_tracks.release()));
2543 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
2544 reference_collection_));
zhihuang9763d562016-08-05 11:14:50 -07002545 rtc::scoped_refptr<AudioTrackInterface> audio_track2 =
perkjd61bf802016-03-24 03:16:19 -07002546 observer_.remote_streams()->at(0)->GetAudioTracks()[1];
2547 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, audio_track2->state());
zhihuang9763d562016-08-05 11:14:50 -07002548 rtc::scoped_refptr<VideoTrackInterface> video_track2 =
perkjd61bf802016-03-24 03:16:19 -07002549 observer_.remote_streams()->at(0)->GetVideoTracks()[1];
2550 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, video_track2->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002551
2552 // Remove the extra audio and video tracks.
kwibergd1fe2812016-04-27 06:47:29 -07002553 std::unique_ptr<SessionDescriptionInterface> desc_ms2 =
kwiberg2bbff992016-03-16 11:03:04 -07002554 CreateSessionDescriptionAndReference(1, 1);
perkjd61bf802016-03-24 03:16:19 -07002555 MockTrackObserver audio_track_observer(audio_track2);
2556 MockTrackObserver video_track_observer(video_track2);
2557
2558 EXPECT_CALL(audio_track_observer, OnChanged()).Times(Exactly(1));
2559 EXPECT_CALL(video_track_observer, OnChanged()).Times(Exactly(1));
deadbeefab9b2d12015-10-14 11:33:11 -07002560 EXPECT_TRUE(DoSetRemoteDescription(desc_ms2.release()));
2561 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
2562 reference_collection_));
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002563 // Track state may be updated asynchronously.
perkjd61bf802016-03-24 03:16:19 -07002564 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002565 audio_track2->state(), kTimeout);
2566 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
2567 video_track2->state(), kTimeout);
deadbeefab9b2d12015-10-14 11:33:11 -07002568}
2569
2570// This tests that remote tracks are ended if a local session description is set
2571// that rejects the media content type.
2572TEST_F(PeerConnectionInterfaceTest, RejectMediaContent) {
2573 FakeConstraints constraints;
2574 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2575 true);
2576 CreatePeerConnection(&constraints);
2577 // First create and set a remote offer, then reject its video content in our
2578 // answer.
2579 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2580 ASSERT_EQ(1u, observer_.remote_streams()->count());
2581 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2582 ASSERT_EQ(1u, remote_stream->GetVideoTracks().size());
2583 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2584
2585 rtc::scoped_refptr<webrtc::VideoTrackInterface> remote_video =
2586 remote_stream->GetVideoTracks()[0];
2587 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, remote_video->state());
2588 rtc::scoped_refptr<webrtc::AudioTrackInterface> remote_audio =
2589 remote_stream->GetAudioTracks()[0];
2590 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, remote_audio->state());
2591
kwibergd1fe2812016-04-27 06:47:29 -07002592 std::unique_ptr<SessionDescriptionInterface> local_answer;
kwiberg2bbff992016-03-16 11:03:04 -07002593 EXPECT_TRUE(DoCreateAnswer(&local_answer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07002594 cricket::ContentInfo* video_info =
2595 local_answer->description()->GetContentByName("video");
2596 video_info->rejected = true;
2597 EXPECT_TRUE(DoSetLocalDescription(local_answer.release()));
2598 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kEnded, remote_video->state());
2599 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, remote_audio->state());
2600
2601 // Now create an offer where we reject both video and audio.
kwibergd1fe2812016-04-27 06:47:29 -07002602 std::unique_ptr<SessionDescriptionInterface> local_offer;
kwiberg2bbff992016-03-16 11:03:04 -07002603 EXPECT_TRUE(DoCreateOffer(&local_offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07002604 video_info = local_offer->description()->GetContentByName("video");
2605 ASSERT_TRUE(video_info != nullptr);
2606 video_info->rejected = true;
2607 cricket::ContentInfo* audio_info =
2608 local_offer->description()->GetContentByName("audio");
2609 ASSERT_TRUE(audio_info != nullptr);
2610 audio_info->rejected = true;
2611 EXPECT_TRUE(DoSetLocalDescription(local_offer.release()));
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002612 // Track state may be updated asynchronously.
perkjd61bf802016-03-24 03:16:19 -07002613 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002614 remote_audio->state(), kTimeout);
2615 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
2616 remote_video->state(), kTimeout);
deadbeefab9b2d12015-10-14 11:33:11 -07002617}
2618
2619// This tests that we won't crash if the remote track has been removed outside
2620// of PeerConnection and then PeerConnection tries to reject the track.
2621TEST_F(PeerConnectionInterfaceTest, RemoveTrackThenRejectMediaContent) {
2622 FakeConstraints constraints;
2623 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2624 true);
2625 CreatePeerConnection(&constraints);
2626 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2627 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2628 remote_stream->RemoveTrack(remote_stream->GetVideoTracks()[0]);
2629 remote_stream->RemoveTrack(remote_stream->GetAudioTracks()[0]);
2630
kwibergd1fe2812016-04-27 06:47:29 -07002631 std::unique_ptr<SessionDescriptionInterface> local_answer(
deadbeefab9b2d12015-10-14 11:33:11 -07002632 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
2633 kSdpStringWithStream1, nullptr));
2634 cricket::ContentInfo* video_info =
2635 local_answer->description()->GetContentByName("video");
2636 video_info->rejected = true;
2637 cricket::ContentInfo* audio_info =
2638 local_answer->description()->GetContentByName("audio");
2639 audio_info->rejected = true;
2640 EXPECT_TRUE(DoSetLocalDescription(local_answer.release()));
2641
2642 // No crash is a pass.
2643}
2644
deadbeef5e97fb52015-10-15 12:49:08 -07002645// This tests that if a recvonly remote description is set, no remote streams
2646// will be created, even if the description contains SSRCs/MSIDs.
2647// See: https://code.google.com/p/webrtc/issues/detail?id=5054
2648TEST_F(PeerConnectionInterfaceTest, RecvonlyDescriptionDoesntCreateStream) {
2649 FakeConstraints constraints;
2650 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2651 true);
2652 CreatePeerConnection(&constraints);
2653
2654 std::string recvonly_offer = kSdpStringWithStream1;
2655 rtc::replace_substrs(kSendrecv, strlen(kSendrecv), kRecvonly,
2656 strlen(kRecvonly), &recvonly_offer);
2657 CreateAndSetRemoteOffer(recvonly_offer);
2658
2659 EXPECT_EQ(0u, observer_.remote_streams()->count());
2660}
2661
deadbeefab9b2d12015-10-14 11:33:11 -07002662// This tests that a default MediaStream is created if a remote session
2663// description doesn't contain any streams and no MSID support.
2664// It also tests that the default stream is updated if a video m-line is added
2665// in a subsequent session description.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002666TEST_F(PeerConnectionInterfaceTest, SdpWithoutMsidCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002667 FakeConstraints constraints;
2668 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2669 true);
2670 CreatePeerConnection(&constraints);
2671 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2672
2673 ASSERT_EQ(1u, observer_.remote_streams()->count());
2674 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2675
2676 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2677 EXPECT_EQ(0u, remote_stream->GetVideoTracks().size());
2678 EXPECT_EQ("default", remote_stream->label());
2679
2680 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2681 ASSERT_EQ(1u, observer_.remote_streams()->count());
2682 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2683 EXPECT_EQ("defaulta0", remote_stream->GetAudioTracks()[0]->id());
deadbeef884f5852016-01-15 09:20:04 -08002684 EXPECT_EQ(MediaStreamTrackInterface::kLive,
2685 remote_stream->GetAudioTracks()[0]->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002686 ASSERT_EQ(1u, remote_stream->GetVideoTracks().size());
2687 EXPECT_EQ("defaultv0", remote_stream->GetVideoTracks()[0]->id());
deadbeef884f5852016-01-15 09:20:04 -08002688 EXPECT_EQ(MediaStreamTrackInterface::kLive,
2689 remote_stream->GetVideoTracks()[0]->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002690}
2691
2692// This tests that a default MediaStream is created if a remote session
2693// description doesn't contain any streams and media direction is send only.
2694TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002695 SendOnlySdpWithoutMsidCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002696 FakeConstraints constraints;
2697 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2698 true);
2699 CreatePeerConnection(&constraints);
2700 CreateAndSetRemoteOffer(kSdpStringSendOnlyWithoutStreams);
2701
2702 ASSERT_EQ(1u, observer_.remote_streams()->count());
2703 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2704
2705 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2706 EXPECT_EQ(1u, remote_stream->GetVideoTracks().size());
2707 EXPECT_EQ("default", remote_stream->label());
2708}
2709
2710// This tests that it won't crash when PeerConnection tries to remove
2711// a remote track that as already been removed from the MediaStream.
2712TEST_F(PeerConnectionInterfaceTest, RemoveAlreadyGoneRemoteStream) {
2713 FakeConstraints constraints;
2714 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2715 true);
2716 CreatePeerConnection(&constraints);
2717 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2718 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2719 remote_stream->RemoveTrack(remote_stream->GetAudioTracks()[0]);
2720 remote_stream->RemoveTrack(remote_stream->GetVideoTracks()[0]);
2721
2722 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2723
2724 // No crash is a pass.
2725}
2726
2727// This tests that a default MediaStream is created if the remote session
2728// description doesn't contain any streams and don't contain an indication if
2729// MSID is supported.
2730TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002731 SdpWithoutMsidAndStreamsCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002732 FakeConstraints constraints;
2733 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2734 true);
2735 CreatePeerConnection(&constraints);
2736 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2737
2738 ASSERT_EQ(1u, observer_.remote_streams()->count());
2739 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2740 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2741 EXPECT_EQ(1u, remote_stream->GetVideoTracks().size());
2742}
2743
2744// This tests that a default MediaStream is not created if the remote session
2745// description doesn't contain any streams but does support MSID.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002746TEST_F(PeerConnectionInterfaceTest, SdpWithMsidDontCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002747 FakeConstraints constraints;
2748 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2749 true);
2750 CreatePeerConnection(&constraints);
2751 CreateAndSetRemoteOffer(kSdpStringWithMsidWithoutStreams);
2752 EXPECT_EQ(0u, observer_.remote_streams()->count());
2753}
2754
deadbeefbda7e0b2015-12-08 17:13:40 -08002755// This tests that when setting a new description, the old default tracks are
2756// not destroyed and recreated.
2757// See: https://bugs.chromium.org/p/webrtc/issues/detail?id=5250
Stefan Holmer102362b2016-03-18 09:39:07 +01002758TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002759 DefaultTracksNotDestroyedAndRecreated) {
deadbeefbda7e0b2015-12-08 17:13:40 -08002760 FakeConstraints constraints;
2761 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2762 true);
2763 CreatePeerConnection(&constraints);
2764 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2765
2766 ASSERT_EQ(1u, observer_.remote_streams()->count());
2767 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2768 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2769
2770 // Set the track to "disabled", then set a new description and ensure the
2771 // track is still disabled, which ensures it hasn't been recreated.
2772 remote_stream->GetAudioTracks()[0]->set_enabled(false);
2773 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2774 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2775 EXPECT_FALSE(remote_stream->GetAudioTracks()[0]->enabled());
2776}
2777
deadbeefab9b2d12015-10-14 11:33:11 -07002778// This tests that a default MediaStream is not created if a remote session
2779// description is updated to not have any MediaStreams.
2780TEST_F(PeerConnectionInterfaceTest, VerifyDefaultStreamIsNotCreated) {
2781 FakeConstraints constraints;
2782 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2783 true);
2784 CreatePeerConnection(&constraints);
2785 CreateAndSetRemoteOffer(kSdpStringWithStream1);
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002786 rtc::scoped_refptr<StreamCollection> reference(CreateStreamCollection(1, 1));
deadbeefab9b2d12015-10-14 11:33:11 -07002787 EXPECT_TRUE(
2788 CompareStreamCollections(observer_.remote_streams(), reference.get()));
2789
2790 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2791 EXPECT_EQ(0u, observer_.remote_streams()->count());
2792}
2793
2794// This tests that an RtpSender is created when the local description is set
2795// after adding a local stream.
2796// TODO(deadbeef): This test and the one below it need to be updated when
2797// an RtpSender's lifetime isn't determined by when a local description is set.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002798TEST_F(PeerConnectionInterfaceTest, LocalDescriptionChanged) {
deadbeefab9b2d12015-10-14 11:33:11 -07002799 FakeConstraints constraints;
2800 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2801 true);
2802 CreatePeerConnection(&constraints);
deadbeefab9b2d12015-10-14 11:33:11 -07002803
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002804 // Create an offer with 1 stream with 2 tracks of each type.
2805 rtc::scoped_refptr<StreamCollection> stream_collection =
2806 CreateStreamCollection(1, 2);
2807 pc_->AddStream(stream_collection->at(0));
2808 std::unique_ptr<SessionDescriptionInterface> offer;
2809 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2810 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
deadbeefab9b2d12015-10-14 11:33:11 -07002811
deadbeefab9b2d12015-10-14 11:33:11 -07002812 auto senders = pc_->GetSenders();
2813 EXPECT_EQ(4u, senders.size());
2814 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2815 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2816 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[1]));
2817 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[1]));
2818
2819 // Remove an audio and video track.
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002820 pc_->RemoveStream(stream_collection->at(0));
2821 stream_collection = CreateStreamCollection(1, 1);
2822 pc_->AddStream(stream_collection->at(0));
2823 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2824 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
2825
deadbeefab9b2d12015-10-14 11:33:11 -07002826 senders = pc_->GetSenders();
2827 EXPECT_EQ(2u, senders.size());
2828 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2829 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2830 EXPECT_FALSE(ContainsSender(senders, kAudioTracks[1]));
2831 EXPECT_FALSE(ContainsSender(senders, kVideoTracks[1]));
2832}
2833
2834// This tests that an RtpSender is created when the local description is set
2835// before adding a local stream.
2836TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002837 AddLocalStreamAfterLocalDescriptionChanged) {
deadbeefab9b2d12015-10-14 11:33:11 -07002838 FakeConstraints constraints;
2839 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2840 true);
2841 CreatePeerConnection(&constraints);
deadbeefab9b2d12015-10-14 11:33:11 -07002842
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002843 rtc::scoped_refptr<StreamCollection> stream_collection =
2844 CreateStreamCollection(1, 2);
2845 // Add a stream to create the offer, but remove it afterwards.
2846 pc_->AddStream(stream_collection->at(0));
2847 std::unique_ptr<SessionDescriptionInterface> offer;
2848 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2849 pc_->RemoveStream(stream_collection->at(0));
deadbeefab9b2d12015-10-14 11:33:11 -07002850
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002851 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
deadbeefab9b2d12015-10-14 11:33:11 -07002852 auto senders = pc_->GetSenders();
2853 EXPECT_EQ(0u, senders.size());
2854
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002855 pc_->AddStream(stream_collection->at(0));
deadbeefab9b2d12015-10-14 11:33:11 -07002856 senders = pc_->GetSenders();
2857 EXPECT_EQ(4u, senders.size());
2858 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2859 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2860 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[1]));
2861 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[1]));
2862}
2863
2864// This tests that the expected behavior occurs if the SSRC on a local track is
2865// changed when SetLocalDescription is called.
2866TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002867 ChangeSsrcOnTrackInLocalSessionDescription) {
deadbeefab9b2d12015-10-14 11:33:11 -07002868 FakeConstraints constraints;
2869 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2870 true);
2871 CreatePeerConnection(&constraints);
deadbeefab9b2d12015-10-14 11:33:11 -07002872
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002873 rtc::scoped_refptr<StreamCollection> stream_collection =
2874 CreateStreamCollection(2, 1);
2875 pc_->AddStream(stream_collection->at(0));
2876 std::unique_ptr<SessionDescriptionInterface> offer;
2877 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2878 // Grab a copy of the offer before it gets passed into the PC.
2879 std::unique_ptr<JsepSessionDescription> modified_offer(
2880 new JsepSessionDescription(JsepSessionDescription::kOffer));
2881 modified_offer->Initialize(offer->description()->Copy(), offer->session_id(),
2882 offer->session_version());
2883 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
deadbeefab9b2d12015-10-14 11:33:11 -07002884
deadbeefab9b2d12015-10-14 11:33:11 -07002885 auto senders = pc_->GetSenders();
2886 EXPECT_EQ(2u, senders.size());
2887 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2888 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2889
2890 // Change the ssrc of the audio and video track.
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002891 cricket::MediaContentDescription* desc =
2892 cricket::GetFirstAudioContentDescription(modified_offer->description());
2893 ASSERT_TRUE(desc != NULL);
2894 for (StreamParams& stream : desc->mutable_streams()) {
2895 for (unsigned int& ssrc : stream.ssrcs) {
2896 ++ssrc;
2897 }
2898 }
deadbeefab9b2d12015-10-14 11:33:11 -07002899
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002900 desc =
2901 cricket::GetFirstVideoContentDescription(modified_offer->description());
2902 ASSERT_TRUE(desc != NULL);
2903 for (StreamParams& stream : desc->mutable_streams()) {
2904 for (unsigned int& ssrc : stream.ssrcs) {
2905 ++ssrc;
2906 }
2907 }
2908
2909 EXPECT_TRUE(DoSetLocalDescription(modified_offer.release()));
deadbeefab9b2d12015-10-14 11:33:11 -07002910 senders = pc_->GetSenders();
2911 EXPECT_EQ(2u, senders.size());
2912 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2913 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2914 // TODO(deadbeef): Once RtpSenders expose parameters, check that the SSRC
2915 // changed.
2916}
2917
2918// This tests that the expected behavior occurs if a new session description is
2919// set with the same tracks, but on a different MediaStream.
Stefan Holmer55d6e7c2016-03-17 16:26:40 +01002920TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002921 SignalSameTracksInSeparateMediaStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002922 FakeConstraints constraints;
2923 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2924 true);
2925 CreatePeerConnection(&constraints);
deadbeefab9b2d12015-10-14 11:33:11 -07002926
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002927 rtc::scoped_refptr<StreamCollection> stream_collection =
2928 CreateStreamCollection(2, 1);
2929 pc_->AddStream(stream_collection->at(0));
2930 std::unique_ptr<SessionDescriptionInterface> offer;
2931 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2932 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
deadbeefab9b2d12015-10-14 11:33:11 -07002933
deadbeefab9b2d12015-10-14 11:33:11 -07002934 auto senders = pc_->GetSenders();
2935 EXPECT_EQ(2u, senders.size());
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002936 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0], kStreams[0]));
2937 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0], kStreams[0]));
deadbeefab9b2d12015-10-14 11:33:11 -07002938
2939 // Add a new MediaStream but with the same tracks as in the first stream.
2940 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream_1(
2941 webrtc::MediaStream::Create(kStreams[1]));
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002942 stream_1->AddTrack(stream_collection->at(0)->GetVideoTracks()[0]);
2943 stream_1->AddTrack(stream_collection->at(0)->GetAudioTracks()[0]);
deadbeefab9b2d12015-10-14 11:33:11 -07002944 pc_->AddStream(stream_1);
2945
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002946 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2947 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
deadbeefab9b2d12015-10-14 11:33:11 -07002948
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002949 auto new_senders = pc_->GetSenders();
2950 // Should be the same senders as before, but with updated stream id.
2951 // Note that this behavior is subject to change in the future.
2952 // We may decide the PC should ignore existing tracks in AddStream.
2953 EXPECT_EQ(senders, new_senders);
2954 EXPECT_TRUE(ContainsSender(new_senders, kAudioTracks[0], kStreams[1]));
2955 EXPECT_TRUE(ContainsSender(new_senders, kVideoTracks[0], kStreams[1]));
deadbeefab9b2d12015-10-14 11:33:11 -07002956}
2957
zhihuang81c3a032016-11-17 12:06:24 -08002958// This tests that PeerConnectionObserver::OnAddTrack is correctly called.
2959TEST_F(PeerConnectionInterfaceTest, OnAddTrackCallback) {
2960 FakeConstraints constraints;
2961 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2962 true);
2963 CreatePeerConnection(&constraints);
2964 CreateAndSetRemoteOffer(kSdpStringWithStream1AudioTrackOnly);
2965 EXPECT_EQ(observer_.num_added_tracks_, 1);
2966 EXPECT_EQ(observer_.last_added_track_label_, kAudioTracks[0]);
2967
2968 // Create and set the updated remote SDP.
2969 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2970 EXPECT_EQ(observer_.num_added_tracks_, 2);
2971 EXPECT_EQ(observer_.last_added_track_label_, kVideoTracks[0]);
2972}
2973
deadbeefd1a38b52016-12-10 13:15:33 -08002974// Test that when SetConfiguration is called and the configuration is
2975// changing, the next offer causes an ICE restart.
2976TEST_F(PeerConnectionInterfaceTest, SetConfigurationCausingIceRetart) {
2977 PeerConnectionInterface::RTCConfiguration config;
2978 config.type = PeerConnectionInterface::kRelay;
2979 // Need to pass default constraints to prevent disabling of DTLS...
2980 FakeConstraints default_constraints;
2981 CreatePeerConnection(config, &default_constraints);
2982 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
2983
2984 // Do initial offer/answer so there's something to restart.
2985 CreateOfferAsLocalDescription();
2986 CreateAnswerAsRemoteDescription(kSdpStringWithStream1);
2987
2988 // Grab the ufrags.
2989 std::vector<std::string> initial_ufrags = GetUfrags(pc_->local_description());
2990
2991 // Change ICE policy, which should trigger an ICE restart on the next offer.
2992 config.type = PeerConnectionInterface::kAll;
2993 EXPECT_TRUE(pc_->SetConfiguration(config));
2994 CreateOfferAsLocalDescription();
2995
2996 // Grab the new ufrags.
2997 std::vector<std::string> subsequent_ufrags =
2998 GetUfrags(pc_->local_description());
2999
3000 // Sanity check.
3001 EXPECT_EQ(initial_ufrags.size(), subsequent_ufrags.size());
3002 // Check that each ufrag is different.
3003 for (int i = 0; i < static_cast<int>(initial_ufrags.size()); ++i) {
3004 EXPECT_NE(initial_ufrags[i], subsequent_ufrags[i]);
3005 }
3006}
3007
3008// Test that when SetConfiguration is called and the configuration *isn't*
3009// changing, the next offer does *not* cause an ICE restart.
3010TEST_F(PeerConnectionInterfaceTest, SetConfigurationNotCausingIceRetart) {
3011 PeerConnectionInterface::RTCConfiguration config;
3012 config.type = PeerConnectionInterface::kRelay;
3013 // Need to pass default constraints to prevent disabling of DTLS...
3014 FakeConstraints default_constraints;
3015 CreatePeerConnection(config, &default_constraints);
3016 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
3017
3018 // Do initial offer/answer so there's something to restart.
3019 CreateOfferAsLocalDescription();
3020 CreateAnswerAsRemoteDescription(kSdpStringWithStream1);
3021
3022 // Grab the ufrags.
3023 std::vector<std::string> initial_ufrags = GetUfrags(pc_->local_description());
3024
3025 // Call SetConfiguration with a config identical to what the PC was
3026 // constructed with.
3027 EXPECT_TRUE(pc_->SetConfiguration(config));
3028 CreateOfferAsLocalDescription();
3029
3030 // Grab the new ufrags.
3031 std::vector<std::string> subsequent_ufrags =
3032 GetUfrags(pc_->local_description());
3033
3034 EXPECT_EQ(initial_ufrags, subsequent_ufrags);
3035}
3036
3037// Test for a weird corner case scenario:
3038// 1. Audio/video session established.
3039// 2. SetConfiguration changes ICE config; ICE restart needed.
3040// 3. ICE restart initiated by remote peer, but only for one m= section.
3041// 4. Next createOffer should initiate an ICE restart, but only for the other
3042// m= section; it would be pointless to do an ICE restart for the m= section
3043// that was already restarted.
3044TEST_F(PeerConnectionInterfaceTest, SetConfigurationCausingPartialIceRestart) {
3045 PeerConnectionInterface::RTCConfiguration config;
3046 config.type = PeerConnectionInterface::kRelay;
3047 // Need to pass default constraints to prevent disabling of DTLS...
3048 FakeConstraints default_constraints;
3049 CreatePeerConnection(config, &default_constraints);
3050 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
3051
3052 // Do initial offer/answer so there's something to restart.
3053 CreateOfferAsLocalDescription();
3054 CreateAnswerAsRemoteDescription(kSdpStringWithStream1);
3055
3056 // Change ICE policy, which should set the "needs-ice-restart" flag.
3057 config.type = PeerConnectionInterface::kAll;
3058 EXPECT_TRUE(pc_->SetConfiguration(config));
3059
3060 // Do ICE restart for the first m= section, initiated by remote peer.
3061 webrtc::JsepSessionDescription* remote_offer =
3062 new webrtc::JsepSessionDescription(SessionDescriptionInterface::kOffer);
3063 EXPECT_TRUE(remote_offer->Initialize(kSdpStringWithStream1, nullptr));
3064 remote_offer->description()->transport_infos()[0].description.ice_ufrag =
3065 "modified";
3066 EXPECT_TRUE(DoSetRemoteDescription(remote_offer));
3067 CreateAnswerAsLocalDescription();
3068
3069 // Grab the ufrags.
3070 std::vector<std::string> initial_ufrags = GetUfrags(pc_->local_description());
3071 ASSERT_EQ(2, initial_ufrags.size());
3072
3073 // Create offer and grab the new ufrags.
3074 CreateOfferAsLocalDescription();
3075 std::vector<std::string> subsequent_ufrags =
3076 GetUfrags(pc_->local_description());
3077 ASSERT_EQ(2, subsequent_ufrags.size());
3078
3079 // Ensure that only the ufrag for the second m= section changed.
3080 EXPECT_EQ(initial_ufrags[0], subsequent_ufrags[0]);
3081 EXPECT_NE(initial_ufrags[1], subsequent_ufrags[1]);
3082}
3083
deadbeeffe4a8a42016-12-20 17:56:17 -08003084// Tests that the methods to return current/pending descriptions work as
3085// expected at different points in the offer/answer exchange. This test does
3086// one offer/answer exchange as the offerer, then another as the answerer.
3087TEST_F(PeerConnectionInterfaceTest, CurrentAndPendingDescriptions) {
3088 // This disables DTLS so we can apply an answer to ourselves.
3089 CreatePeerConnection();
3090
3091 // Create initial local offer and get SDP (which will also be used as
3092 // answer/pranswer);
3093 std::unique_ptr<SessionDescriptionInterface> offer;
3094 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
3095 std::string sdp;
3096 EXPECT_TRUE(offer->ToString(&sdp));
3097
3098 // Set local offer.
3099 SessionDescriptionInterface* local_offer = offer.release();
3100 EXPECT_TRUE(DoSetLocalDescription(local_offer));
3101 EXPECT_EQ(local_offer, pc_->pending_local_description());
3102 EXPECT_EQ(nullptr, pc_->pending_remote_description());
3103 EXPECT_EQ(nullptr, pc_->current_local_description());
3104 EXPECT_EQ(nullptr, pc_->current_remote_description());
3105
3106 // Set remote pranswer.
3107 SessionDescriptionInterface* remote_pranswer =
3108 webrtc::CreateSessionDescription(SessionDescriptionInterface::kPrAnswer,
3109 sdp, nullptr);
3110 EXPECT_TRUE(DoSetRemoteDescription(remote_pranswer));
3111 EXPECT_EQ(local_offer, pc_->pending_local_description());
3112 EXPECT_EQ(remote_pranswer, pc_->pending_remote_description());
3113 EXPECT_EQ(nullptr, pc_->current_local_description());
3114 EXPECT_EQ(nullptr, pc_->current_remote_description());
3115
3116 // Set remote answer.
3117 SessionDescriptionInterface* remote_answer = webrtc::CreateSessionDescription(
3118 SessionDescriptionInterface::kAnswer, sdp, nullptr);
3119 EXPECT_TRUE(DoSetRemoteDescription(remote_answer));
3120 EXPECT_EQ(nullptr, pc_->pending_local_description());
3121 EXPECT_EQ(nullptr, pc_->pending_remote_description());
3122 EXPECT_EQ(local_offer, pc_->current_local_description());
3123 EXPECT_EQ(remote_answer, pc_->current_remote_description());
3124
3125 // Set remote offer.
3126 SessionDescriptionInterface* remote_offer = webrtc::CreateSessionDescription(
3127 SessionDescriptionInterface::kOffer, sdp, nullptr);
3128 EXPECT_TRUE(DoSetRemoteDescription(remote_offer));
3129 EXPECT_EQ(remote_offer, pc_->pending_remote_description());
3130 EXPECT_EQ(nullptr, pc_->pending_local_description());
3131 EXPECT_EQ(local_offer, pc_->current_local_description());
3132 EXPECT_EQ(remote_answer, pc_->current_remote_description());
3133
3134 // Set local pranswer.
3135 SessionDescriptionInterface* local_pranswer =
3136 webrtc::CreateSessionDescription(SessionDescriptionInterface::kPrAnswer,
3137 sdp, nullptr);
3138 EXPECT_TRUE(DoSetLocalDescription(local_pranswer));
3139 EXPECT_EQ(remote_offer, pc_->pending_remote_description());
3140 EXPECT_EQ(local_pranswer, pc_->pending_local_description());
3141 EXPECT_EQ(local_offer, pc_->current_local_description());
3142 EXPECT_EQ(remote_answer, pc_->current_remote_description());
3143
3144 // Set local answer.
3145 SessionDescriptionInterface* local_answer = webrtc::CreateSessionDescription(
3146 SessionDescriptionInterface::kAnswer, sdp, nullptr);
3147 EXPECT_TRUE(DoSetLocalDescription(local_answer));
3148 EXPECT_EQ(nullptr, pc_->pending_remote_description());
3149 EXPECT_EQ(nullptr, pc_->pending_local_description());
3150 EXPECT_EQ(remote_offer, pc_->current_remote_description());
3151 EXPECT_EQ(local_answer, pc_->current_local_description());
3152}
3153
zhihuang77985012017-02-07 15:45:16 -08003154// Tests that it won't crash when calling StartRtcEventLog or StopRtcEventLog
3155// after the PeerConnection is closed.
3156TEST_F(PeerConnectionInterfaceTest,
3157 StartAndStopLoggingAfterPeerConnectionClosed) {
3158 CreatePeerConnection();
3159 // The RtcEventLog will be reset when the PeerConnection is closed.
3160 pc_->Close();
3161
3162 rtc::PlatformFile file = 0;
3163 int64_t max_size_bytes = 1024;
3164 EXPECT_FALSE(pc_->StartRtcEventLog(file, max_size_bytes));
3165 pc_->StopRtcEventLog();
3166}
3167
deadbeef30952b42017-04-21 02:41:29 -07003168// Test that generated offers/answers include "ice-option:trickle".
3169TEST_F(PeerConnectionInterfaceTest, OffersAndAnswersHaveTrickleIceOption) {
3170 CreatePeerConnection();
3171
3172 // First, create an offer with audio/video.
3173 FakeConstraints constraints;
3174 constraints.SetMandatoryReceiveAudio(true);
3175 constraints.SetMandatoryReceiveVideo(true);
3176 std::unique_ptr<SessionDescriptionInterface> offer;
3177 ASSERT_TRUE(DoCreateOffer(&offer, &constraints));
3178 cricket::SessionDescription* desc = offer->description();
3179 ASSERT_EQ(2u, desc->transport_infos().size());
3180 EXPECT_TRUE(desc->transport_infos()[0].description.HasOption("trickle"));
3181 EXPECT_TRUE(desc->transport_infos()[1].description.HasOption("trickle"));
3182
3183 // Apply the offer as a remote description, then create an answer.
3184 EXPECT_TRUE(DoSetRemoteDescription(offer.release()));
3185 std::unique_ptr<SessionDescriptionInterface> answer;
3186 ASSERT_TRUE(DoCreateAnswer(&answer, &constraints));
3187 desc = answer->description();
3188 ASSERT_EQ(2u, desc->transport_infos().size());
3189 EXPECT_TRUE(desc->transport_infos()[0].description.HasOption("trickle"));
3190 EXPECT_TRUE(desc->transport_infos()[1].description.HasOption("trickle"));
3191}
3192
deadbeef1dcb1642017-03-29 21:08:16 -07003193// Test that ICE renomination isn't offered if it's not enabled in the PC's
3194// RTCConfiguration.
3195TEST_F(PeerConnectionInterfaceTest, IceRenominationNotOffered) {
3196 PeerConnectionInterface::RTCConfiguration config;
3197 config.enable_ice_renomination = false;
3198 CreatePeerConnection(config, nullptr);
3199 AddVoiceStream("foo");
3200
3201 std::unique_ptr<SessionDescriptionInterface> offer;
3202 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
3203 cricket::SessionDescription* desc = offer->description();
3204 EXPECT_EQ(1u, desc->transport_infos().size());
3205 EXPECT_FALSE(
3206 desc->transport_infos()[0].description.GetIceParameters().renomination);
3207}
3208
3209// Test that the ICE renomination option is present in generated offers/answers
3210// if it's enabled in the PC's RTCConfiguration.
3211TEST_F(PeerConnectionInterfaceTest, IceRenominationOptionInOfferAndAnswer) {
3212 PeerConnectionInterface::RTCConfiguration config;
3213 config.enable_ice_renomination = true;
3214 CreatePeerConnection(config, nullptr);
3215 AddVoiceStream("foo");
3216
3217 std::unique_ptr<SessionDescriptionInterface> offer;
3218 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
3219 cricket::SessionDescription* desc = offer->description();
3220 EXPECT_EQ(1u, desc->transport_infos().size());
3221 EXPECT_TRUE(
3222 desc->transport_infos()[0].description.GetIceParameters().renomination);
3223
3224 // Set the offer as a remote description, then create an answer and ensure it
3225 // has the renomination flag too.
3226 EXPECT_TRUE(DoSetRemoteDescription(offer.release()));
3227 std::unique_ptr<SessionDescriptionInterface> answer;
3228 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
3229 desc = answer->description();
3230 EXPECT_EQ(1u, desc->transport_infos().size());
3231 EXPECT_TRUE(
3232 desc->transport_infos()[0].description.GetIceParameters().renomination);
3233}
3234
3235// Test that if CreateOffer is called with the deprecated "offer to receive
3236// audio/video" constraints, they're processed and result in an offer with
3237// audio/video sections just as if RTCOfferAnswerOptions had been used.
3238TEST_F(PeerConnectionInterfaceTest, CreateOfferWithOfferToReceiveConstraints) {
3239 CreatePeerConnection();
3240
3241 FakeConstraints constraints;
3242 constraints.SetMandatoryReceiveAudio(true);
3243 constraints.SetMandatoryReceiveVideo(true);
3244 std::unique_ptr<SessionDescriptionInterface> offer;
3245 ASSERT_TRUE(DoCreateOffer(&offer, &constraints));
3246
3247 cricket::SessionDescription* desc = offer->description();
3248 const cricket::ContentInfo* audio = cricket::GetFirstAudioContent(desc);
3249 const cricket::ContentInfo* video = cricket::GetFirstVideoContent(desc);
3250 ASSERT_NE(nullptr, audio);
3251 ASSERT_NE(nullptr, video);
3252 EXPECT_FALSE(audio->rejected);
3253 EXPECT_FALSE(video->rejected);
3254}
3255
3256// Test that if CreateAnswer is called with the deprecated "offer to receive
3257// audio/video" constraints, they're processed and can be used to reject an
3258// offered m= section just as can be done with RTCOfferAnswerOptions;
3259TEST_F(PeerConnectionInterfaceTest, CreateAnswerWithOfferToReceiveConstraints) {
3260 CreatePeerConnection();
3261
3262 // First, create an offer with audio/video and apply it as a remote
3263 // description.
3264 FakeConstraints constraints;
3265 constraints.SetMandatoryReceiveAudio(true);
3266 constraints.SetMandatoryReceiveVideo(true);
3267 std::unique_ptr<SessionDescriptionInterface> offer;
3268 ASSERT_TRUE(DoCreateOffer(&offer, &constraints));
3269 EXPECT_TRUE(DoSetRemoteDescription(offer.release()));
3270
3271 // Now create answer that rejects audio/video.
3272 constraints.SetMandatoryReceiveAudio(false);
3273 constraints.SetMandatoryReceiveVideo(false);
3274 std::unique_ptr<SessionDescriptionInterface> answer;
3275 ASSERT_TRUE(DoCreateAnswer(&answer, &constraints));
3276
3277 cricket::SessionDescription* desc = answer->description();
3278 const cricket::ContentInfo* audio = cricket::GetFirstAudioContent(desc);
3279 const cricket::ContentInfo* video = cricket::GetFirstVideoContent(desc);
3280 ASSERT_NE(nullptr, audio);
3281 ASSERT_NE(nullptr, video);
3282 EXPECT_TRUE(audio->rejected);
3283 EXPECT_TRUE(video->rejected);
3284}
3285
3286#ifdef HAVE_SCTP
3287#define MAYBE_DataChannelOnlyOfferWithMaxBundlePolicy \
3288 DataChannelOnlyOfferWithMaxBundlePolicy
3289#else
3290#define MAYBE_DataChannelOnlyOfferWithMaxBundlePolicy \
3291 DISABLED_DataChannelOnlyOfferWithMaxBundlePolicy
3292#endif
3293
3294// Test that negotiation can succeed with a data channel only, and with the max
3295// bundle policy. Previously there was a bug that prevented this.
3296TEST_F(PeerConnectionInterfaceTest,
3297 MAYBE_DataChannelOnlyOfferWithMaxBundlePolicy) {
3298 PeerConnectionInterface::RTCConfiguration config;
3299 config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
3300 CreatePeerConnection(config, nullptr);
3301
3302 // First, create an offer with only a data channel and apply it as a remote
3303 // description.
3304 pc_->CreateDataChannel("test", nullptr);
3305 std::unique_ptr<SessionDescriptionInterface> offer;
3306 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
3307 EXPECT_TRUE(DoSetRemoteDescription(offer.release()));
3308
3309 // Create and set answer as well.
3310 std::unique_ptr<SessionDescriptionInterface> answer;
3311 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
3312 EXPECT_TRUE(DoSetLocalDescription(answer.release()));
3313}
3314
nisse51542be2016-02-12 02:27:06 -08003315class PeerConnectionMediaConfigTest : public testing::Test {
3316 protected:
3317 void SetUp() override {
nisseaf510af2016-03-21 08:20:42 -07003318 pcf_ = new rtc::RefCountedObject<PeerConnectionFactoryForTest>();
nisse51542be2016-02-12 02:27:06 -08003319 pcf_->Initialize();
3320 }
3321 const cricket::MediaConfig& TestCreatePeerConnection(
3322 const PeerConnectionInterface::RTCConfiguration& config,
3323 const MediaConstraintsInterface *constraints) {
3324 pcf_->create_media_controller_called_ = false;
3325
zhihuang9763d562016-08-05 11:14:50 -07003326 rtc::scoped_refptr<PeerConnectionInterface> pc(pcf_->CreatePeerConnection(
3327 config, constraints, nullptr, nullptr, &observer_));
nisse51542be2016-02-12 02:27:06 -08003328 EXPECT_TRUE(pc.get());
3329 EXPECT_TRUE(pcf_->create_media_controller_called_);
3330 return pcf_->create_media_controller_config_;
3331 }
3332
zhihuang9763d562016-08-05 11:14:50 -07003333 rtc::scoped_refptr<PeerConnectionFactoryForTest> pcf_;
nisse51542be2016-02-12 02:27:06 -08003334 MockPeerConnectionObserver observer_;
3335};
3336
3337// This test verifies the default behaviour with no constraints and a
3338// default RTCConfiguration.
3339TEST_F(PeerConnectionMediaConfigTest, TestDefaults) {
3340 PeerConnectionInterface::RTCConfiguration config;
3341 FakeConstraints constraints;
3342
3343 const cricket::MediaConfig& media_config =
3344 TestCreatePeerConnection(config, &constraints);
3345
3346 EXPECT_FALSE(media_config.enable_dscp);
nisse0db023a2016-03-01 04:29:59 -08003347 EXPECT_TRUE(media_config.video.enable_cpu_overuse_detection);
3348 EXPECT_FALSE(media_config.video.disable_prerenderer_smoothing);
3349 EXPECT_FALSE(media_config.video.suspend_below_min_bitrate);
nisse51542be2016-02-12 02:27:06 -08003350}
3351
3352// This test verifies the DSCP constraint is recognized and passed to
3353// the CreateMediaController call.
3354TEST_F(PeerConnectionMediaConfigTest, TestDscpConstraintTrue) {
3355 PeerConnectionInterface::RTCConfiguration config;
3356 FakeConstraints constraints;
3357
3358 constraints.AddOptional(webrtc::MediaConstraintsInterface::kEnableDscp, true);
3359 const cricket::MediaConfig& media_config =
3360 TestCreatePeerConnection(config, &constraints);
3361
3362 EXPECT_TRUE(media_config.enable_dscp);
3363}
3364
3365// This test verifies the cpu overuse detection constraint is
3366// recognized and passed to the CreateMediaController call.
3367TEST_F(PeerConnectionMediaConfigTest, TestCpuOveruseConstraintFalse) {
3368 PeerConnectionInterface::RTCConfiguration config;
3369 FakeConstraints constraints;
3370
3371 constraints.AddOptional(
3372 webrtc::MediaConstraintsInterface::kCpuOveruseDetection, false);
3373 const cricket::MediaConfig media_config =
3374 TestCreatePeerConnection(config, &constraints);
3375
nisse0db023a2016-03-01 04:29:59 -08003376 EXPECT_FALSE(media_config.video.enable_cpu_overuse_detection);
nisse51542be2016-02-12 02:27:06 -08003377}
3378
3379// This test verifies that the disable_prerenderer_smoothing flag is
3380// propagated from RTCConfiguration to the CreateMediaController call.
3381TEST_F(PeerConnectionMediaConfigTest, TestDisablePrerendererSmoothingTrue) {
3382 PeerConnectionInterface::RTCConfiguration config;
3383 FakeConstraints constraints;
3384
Niels Möller71bdda02016-03-31 12:59:59 +02003385 config.set_prerenderer_smoothing(false);
nisse51542be2016-02-12 02:27:06 -08003386 const cricket::MediaConfig& media_config =
3387 TestCreatePeerConnection(config, &constraints);
3388
nisse0db023a2016-03-01 04:29:59 -08003389 EXPECT_TRUE(media_config.video.disable_prerenderer_smoothing);
3390}
3391
3392// This test verifies the suspend below min bitrate constraint is
3393// recognized and passed to the CreateMediaController call.
3394TEST_F(PeerConnectionMediaConfigTest,
3395 TestSuspendBelowMinBitrateConstraintTrue) {
3396 PeerConnectionInterface::RTCConfiguration config;
3397 FakeConstraints constraints;
3398
3399 constraints.AddOptional(
3400 webrtc::MediaConstraintsInterface::kEnableVideoSuspendBelowMinBitrate,
3401 true);
3402 const cricket::MediaConfig media_config =
3403 TestCreatePeerConnection(config, &constraints);
3404
3405 EXPECT_TRUE(media_config.video.suspend_below_min_bitrate);
nisse51542be2016-02-12 02:27:06 -08003406}
3407
deadbeefab9b2d12015-10-14 11:33:11 -07003408// The following tests verify that session options are created correctly.
deadbeefc80741f2015-10-22 13:14:45 -07003409// TODO(deadbeef): Convert these tests to be more end-to-end. Instead of
3410// "verify options are converted correctly", should be "pass options into
3411// CreateOffer and verify the correct offer is produced."
deadbeefab9b2d12015-10-14 11:33:11 -07003412
3413TEST(CreateSessionOptionsTest, GetOptionsForOfferWithInvalidAudioOption) {
3414 RTCOfferAnswerOptions rtc_options;
3415 rtc_options.offer_to_receive_audio = RTCOfferAnswerOptions::kUndefined - 1;
3416
3417 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08003418 EXPECT_FALSE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07003419
3420 rtc_options.offer_to_receive_audio =
3421 RTCOfferAnswerOptions::kMaxOfferToReceiveMedia + 1;
htaaac2dea2016-03-10 13:35:55 -08003422 EXPECT_FALSE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07003423}
3424
3425TEST(CreateSessionOptionsTest, GetOptionsForOfferWithInvalidVideoOption) {
3426 RTCOfferAnswerOptions rtc_options;
3427 rtc_options.offer_to_receive_video = RTCOfferAnswerOptions::kUndefined - 1;
3428
3429 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08003430 EXPECT_FALSE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07003431
3432 rtc_options.offer_to_receive_video =
3433 RTCOfferAnswerOptions::kMaxOfferToReceiveMedia + 1;
htaaac2dea2016-03-10 13:35:55 -08003434 EXPECT_FALSE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07003435}
3436
3437// Test that a MediaSessionOptions is created for an offer if
deadbeefc80741f2015-10-22 13:14:45 -07003438// OfferToReceiveAudio and OfferToReceiveVideo options are set.
deadbeefab9b2d12015-10-14 11:33:11 -07003439TEST(CreateSessionOptionsTest, GetMediaSessionOptionsForOfferWithAudioVideo) {
3440 RTCOfferAnswerOptions rtc_options;
3441 rtc_options.offer_to_receive_audio = 1;
3442 rtc_options.offer_to_receive_video = 1;
3443
3444 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08003445 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07003446 EXPECT_TRUE(options.has_audio());
3447 EXPECT_TRUE(options.has_video());
3448 EXPECT_TRUE(options.bundle_enabled);
3449}
3450
3451// Test that a correct MediaSessionOptions is created for an offer if
deadbeefc80741f2015-10-22 13:14:45 -07003452// OfferToReceiveAudio is set.
deadbeefab9b2d12015-10-14 11:33:11 -07003453TEST(CreateSessionOptionsTest, GetMediaSessionOptionsForOfferWithAudio) {
3454 RTCOfferAnswerOptions rtc_options;
3455 rtc_options.offer_to_receive_audio = 1;
3456
3457 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08003458 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07003459 EXPECT_TRUE(options.has_audio());
3460 EXPECT_FALSE(options.has_video());
3461 EXPECT_TRUE(options.bundle_enabled);
3462}
3463
3464// Test that a correct MediaSessionOptions is created for an offer if
deadbeefc80741f2015-10-22 13:14:45 -07003465// the default OfferOptions are used.
deadbeefab9b2d12015-10-14 11:33:11 -07003466TEST(CreateSessionOptionsTest, GetDefaultMediaSessionOptionsForOffer) {
3467 RTCOfferAnswerOptions rtc_options;
3468
3469 cricket::MediaSessionOptions options;
deadbeef0ed85b22016-02-23 17:24:52 -08003470 options.transport_options["audio"] = cricket::TransportOptions();
3471 options.transport_options["video"] = cricket::TransportOptions();
htaaac2dea2016-03-10 13:35:55 -08003472 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefc80741f2015-10-22 13:14:45 -07003473 EXPECT_TRUE(options.has_audio());
deadbeefab9b2d12015-10-14 11:33:11 -07003474 EXPECT_FALSE(options.has_video());
deadbeefc80741f2015-10-22 13:14:45 -07003475 EXPECT_TRUE(options.bundle_enabled);
deadbeefab9b2d12015-10-14 11:33:11 -07003476 EXPECT_TRUE(options.vad_enabled);
deadbeef0ed85b22016-02-23 17:24:52 -08003477 EXPECT_FALSE(options.transport_options["audio"].ice_restart);
3478 EXPECT_FALSE(options.transport_options["video"].ice_restart);
deadbeefab9b2d12015-10-14 11:33:11 -07003479}
3480
3481// Test that a correct MediaSessionOptions is created for an offer if
deadbeefc80741f2015-10-22 13:14:45 -07003482// OfferToReceiveVideo is set.
deadbeefab9b2d12015-10-14 11:33:11 -07003483TEST(CreateSessionOptionsTest, GetMediaSessionOptionsForOfferWithVideo) {
3484 RTCOfferAnswerOptions rtc_options;
3485 rtc_options.offer_to_receive_audio = 0;
3486 rtc_options.offer_to_receive_video = 1;
3487
3488 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08003489 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07003490 EXPECT_FALSE(options.has_audio());
3491 EXPECT_TRUE(options.has_video());
3492 EXPECT_TRUE(options.bundle_enabled);
3493}
3494
3495// Test that a correct MediaSessionOptions is created for an offer if
3496// UseRtpMux is set to false.
3497TEST(CreateSessionOptionsTest,
3498 GetMediaSessionOptionsForOfferWithBundleDisabled) {
3499 RTCOfferAnswerOptions rtc_options;
3500 rtc_options.offer_to_receive_audio = 1;
3501 rtc_options.offer_to_receive_video = 1;
3502 rtc_options.use_rtp_mux = false;
3503
3504 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08003505 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07003506 EXPECT_TRUE(options.has_audio());
3507 EXPECT_TRUE(options.has_video());
3508 EXPECT_FALSE(options.bundle_enabled);
3509}
3510
3511// Test that a correct MediaSessionOptions is created to restart ice if
3512// IceRestart is set. It also tests that subsequent MediaSessionOptions don't
Taylor Brandstetterf475d362016-01-08 15:35:57 -08003513// have |audio_transport_options.ice_restart| etc. set.
deadbeefab9b2d12015-10-14 11:33:11 -07003514TEST(CreateSessionOptionsTest, GetMediaSessionOptionsForOfferWithIceRestart) {
3515 RTCOfferAnswerOptions rtc_options;
3516 rtc_options.ice_restart = true;
3517
3518 cricket::MediaSessionOptions options;
deadbeef0ed85b22016-02-23 17:24:52 -08003519 options.transport_options["audio"] = cricket::TransportOptions();
3520 options.transport_options["video"] = cricket::TransportOptions();
htaaac2dea2016-03-10 13:35:55 -08003521 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeef0ed85b22016-02-23 17:24:52 -08003522 EXPECT_TRUE(options.transport_options["audio"].ice_restart);
3523 EXPECT_TRUE(options.transport_options["video"].ice_restart);
deadbeefab9b2d12015-10-14 11:33:11 -07003524
3525 rtc_options = RTCOfferAnswerOptions();
htaaac2dea2016-03-10 13:35:55 -08003526 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeef0ed85b22016-02-23 17:24:52 -08003527 EXPECT_FALSE(options.transport_options["audio"].ice_restart);
3528 EXPECT_FALSE(options.transport_options["video"].ice_restart);
deadbeefab9b2d12015-10-14 11:33:11 -07003529}
3530
3531// Test that the MediaConstraints in an answer don't affect if audio and video
3532// is offered in an offer but that if kOfferToReceiveAudio or
3533// kOfferToReceiveVideo constraints are true in an offer, the media type will be
3534// included in subsequent answers.
3535TEST(CreateSessionOptionsTest, MediaConstraintsInAnswer) {
3536 FakeConstraints answer_c;
3537 answer_c.SetMandatoryReceiveAudio(true);
3538 answer_c.SetMandatoryReceiveVideo(true);
3539
3540 cricket::MediaSessionOptions answer_options;
3541 EXPECT_TRUE(ParseConstraintsForAnswer(&answer_c, &answer_options));
3542 EXPECT_TRUE(answer_options.has_audio());
3543 EXPECT_TRUE(answer_options.has_video());
3544
deadbeefc80741f2015-10-22 13:14:45 -07003545 RTCOfferAnswerOptions rtc_offer_options;
deadbeefab9b2d12015-10-14 11:33:11 -07003546
3547 cricket::MediaSessionOptions offer_options;
htaaac2dea2016-03-10 13:35:55 -08003548 EXPECT_TRUE(
3549 ExtractMediaSessionOptions(rtc_offer_options, false, &offer_options));
deadbeefc80741f2015-10-22 13:14:45 -07003550 EXPECT_TRUE(offer_options.has_audio());
htaaac2dea2016-03-10 13:35:55 -08003551 EXPECT_TRUE(offer_options.has_video());
deadbeefab9b2d12015-10-14 11:33:11 -07003552
deadbeefc80741f2015-10-22 13:14:45 -07003553 RTCOfferAnswerOptions updated_rtc_offer_options;
3554 updated_rtc_offer_options.offer_to_receive_audio = 1;
3555 updated_rtc_offer_options.offer_to_receive_video = 1;
deadbeefab9b2d12015-10-14 11:33:11 -07003556
3557 cricket::MediaSessionOptions updated_offer_options;
htaaac2dea2016-03-10 13:35:55 -08003558 EXPECT_TRUE(ExtractMediaSessionOptions(updated_rtc_offer_options, false,
htaa2a49d92016-03-04 02:51:39 -08003559 &updated_offer_options));
deadbeefab9b2d12015-10-14 11:33:11 -07003560 EXPECT_TRUE(updated_offer_options.has_audio());
3561 EXPECT_TRUE(updated_offer_options.has_video());
3562
3563 // Since an offer has been created with both audio and video, subsequent
3564 // offers and answers should contain both audio and video.
3565 // Answers will only contain the media types that exist in the offer
3566 // regardless of the value of |updated_answer_options.has_audio| and
3567 // |updated_answer_options.has_video|.
3568 FakeConstraints updated_answer_c;
3569 answer_c.SetMandatoryReceiveAudio(false);
3570 answer_c.SetMandatoryReceiveVideo(false);
3571
3572 cricket::MediaSessionOptions updated_answer_options;
3573 EXPECT_TRUE(
3574 ParseConstraintsForAnswer(&updated_answer_c, &updated_answer_options));
3575 EXPECT_TRUE(updated_answer_options.has_audio());
3576 EXPECT_TRUE(updated_answer_options.has_video());
deadbeefab9b2d12015-10-14 11:33:11 -07003577}
deadbeef3edec7c2016-12-10 11:44:26 -08003578
deadbeef293e9262017-01-11 12:28:30 -08003579// Tests a few random fields being different.
3580TEST(RTCConfigurationTest, ComparisonOperators) {
3581 PeerConnectionInterface::RTCConfiguration a;
3582 PeerConnectionInterface::RTCConfiguration b;
3583 EXPECT_EQ(a, b);
3584
3585 PeerConnectionInterface::RTCConfiguration c;
3586 c.servers.push_back(PeerConnectionInterface::IceServer());
3587 EXPECT_NE(a, c);
3588
3589 PeerConnectionInterface::RTCConfiguration d;
3590 d.type = PeerConnectionInterface::kRelay;
3591 EXPECT_NE(a, d);
3592
3593 PeerConnectionInterface::RTCConfiguration e;
3594 e.audio_jitter_buffer_max_packets = 5;
3595 EXPECT_NE(a, e);
3596
3597 PeerConnectionInterface::RTCConfiguration f;
3598 f.ice_connection_receiving_timeout = 1337;
3599 EXPECT_NE(a, f);
3600
3601 PeerConnectionInterface::RTCConfiguration g;
3602 g.disable_ipv6 = true;
3603 EXPECT_NE(a, g);
3604
3605 PeerConnectionInterface::RTCConfiguration h(
3606 PeerConnectionInterface::RTCConfigurationType::kAggressive);
3607 EXPECT_NE(a, h);
3608}