blob: c36460f73ba3591191f176931bd4fd3ba690d51f [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
kjellanderb24317b2016-02-10 07:54:43 -08002 * Copyright 2012 The WebRTC project authors. All Rights Reserved.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003 *
kjellanderb24317b2016-02-10 07:54:43 -08004 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00009 */
10
kwibergd1fe2812016-04-27 06:47:29 -070011#include <memory>
deadbeef3edec7c2016-12-10 11:44:26 -080012#include <sstream>
henrike@webrtc.org28e20752013-07-10 00:45:36 +000013#include <string>
kwiberg0eb15ed2015-12-17 03:04:15 -080014#include <utility>
henrike@webrtc.org28e20752013-07-10 00:45:36 +000015
kwiberg087bd342017-02-10 08:15:44 -080016#include "webrtc/api/audio_codecs/builtin_audio_decoder_factory.h"
ossueb1fde42017-05-02 06:46:30 -070017#include "webrtc/api/audio_codecs/builtin_audio_encoder_factory.h"
Henrik Kjellander15583c12016-02-10 10:53:12 +010018#include "webrtc/api/jsepsessiondescription.h"
Henrik Kjellander15583c12016-02-10 10:53:12 +010019#include "webrtc/api/mediastreaminterface.h"
Henrik Kjellander15583c12016-02-10 10:53:12 +010020#include "webrtc/api/peerconnectioninterface.h"
21#include "webrtc/api/rtpreceiverinterface.h"
22#include "webrtc/api/rtpsenderinterface.h"
Henrik Kjellander15583c12016-02-10 10:53:12 +010023#include "webrtc/api/test/fakeconstraints.h"
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000024#include "webrtc/base/gunit.h"
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000025#include "webrtc/base/ssladapter.h"
26#include "webrtc/base/sslstreamadapter.h"
27#include "webrtc/base/stringutils.h"
28#include "webrtc/base/thread.h"
deadbeef9a6f4d42017-05-15 19:43:33 -070029#include "webrtc/base/virtualsocketserver.h"
kjellandera96e2d72016-02-04 23:52:28 -080030#include "webrtc/media/base/fakevideocapturer.h"
zhihuang38ede132017-06-15 12:52:32 -070031#include "webrtc/media/engine/webrtcmediaengine.h"
deadbeef953c2ce2017-01-09 14:53:41 -080032#include "webrtc/media/sctp/sctptransportinternal.h"
peaha9cc40b2017-06-29 08:32:09 -070033#include "webrtc/modules/audio_processing/include/audio_processing.h"
Taylor Brandstettera1c30352016-05-13 08:15:11 -070034#include "webrtc/p2p/base/fakeportallocator.h"
ossu7bb87ee2017-01-23 04:56:25 -080035#include "webrtc/pc/audiotrack.h"
kjellander@webrtc.org9b8df252016-02-12 06:47:59 +010036#include "webrtc/pc/mediasession.h"
ossu7bb87ee2017-01-23 04:56:25 -080037#include "webrtc/pc/mediastream.h"
38#include "webrtc/pc/peerconnection.h"
39#include "webrtc/pc/streamcollection.h"
40#include "webrtc/pc/test/fakertccertificategenerator.h"
41#include "webrtc/pc/test/fakevideotracksource.h"
42#include "webrtc/pc/test/mockpeerconnectionobservers.h"
43#include "webrtc/pc/test/testsdpstrings.h"
44#include "webrtc/pc/videocapturertracksource.h"
45#include "webrtc/pc/videotrack.h"
kwibergac9f8762016-09-30 22:29:43 -070046#include "webrtc/test/gmock.h"
47
48#ifdef WEBRTC_ANDROID
ossu7bb87ee2017-01-23 04:56:25 -080049#include "webrtc/pc/test/androidtestinitializer.h"
kwibergac9f8762016-09-30 22:29:43 -070050#endif
henrike@webrtc.org28e20752013-07-10 00:45:36 +000051
52static const char kStreamLabel1[] = "local_stream_1";
53static const char kStreamLabel2[] = "local_stream_2";
54static const char kStreamLabel3[] = "local_stream_3";
55static const int kDefaultStunPort = 3478;
56static const char kStunAddressOnly[] = "stun:address";
57static const char kStunInvalidPort[] = "stun:address:-1";
58static const char kStunAddressPortAndMore1[] = "stun:address:port:more";
59static const char kStunAddressPortAndMore2[] = "stun:address:port more";
60static const char kTurnIceServerUri[] = "turn:user@turn.example.org";
61static const char kTurnUsername[] = "user";
62static const char kTurnPassword[] = "password";
63static const char kTurnHostname[] = "turn.example.org";
Peter Boström0c4e06b2015-10-07 12:23:21 +020064static const uint32_t kTimeout = 10000U;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000065
deadbeefab9b2d12015-10-14 11:33:11 -070066static const char kStreams[][8] = {"stream1", "stream2"};
67static const char kAudioTracks[][32] = {"audiotrack0", "audiotrack1"};
68static const char kVideoTracks[][32] = {"videotrack0", "videotrack1"};
69
deadbeef5e97fb52015-10-15 12:49:08 -070070static const char kRecvonly[] = "recvonly";
71static const char kSendrecv[] = "sendrecv";
72
deadbeefab9b2d12015-10-14 11:33:11 -070073// Reference SDP with a MediaStream with label "stream1" and audio track with
74// id "audio_1" and a video track with id "video_1;
75static const char kSdpStringWithStream1[] =
76 "v=0\r\n"
77 "o=- 0 0 IN IP4 127.0.0.1\r\n"
78 "s=-\r\n"
79 "t=0 0\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -080080 "m=audio 1 RTP/AVPF 103\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -070081 "a=ice-ufrag:e5785931\r\n"
82 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
83 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
84 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -070085 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -070086 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -080087 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -070088 "a=rtpmap:103 ISAC/16000\r\n"
89 "a=ssrc:1 cname:stream1\r\n"
90 "a=ssrc:1 mslabel:stream1\r\n"
91 "a=ssrc:1 label:audiotrack0\r\n"
92 "m=video 1 RTP/AVPF 120\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -080093 "a=ice-ufrag:e5785931\r\n"
94 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
95 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
96 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -070097 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -070098 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -080099 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700100 "a=rtpmap:120 VP8/90000\r\n"
101 "a=ssrc:2 cname:stream1\r\n"
102 "a=ssrc:2 mslabel:stream1\r\n"
103 "a=ssrc:2 label:videotrack0\r\n";
104
zhihuang81c3a032016-11-17 12:06:24 -0800105// Reference SDP with a MediaStream with label "stream1" and audio track with
106// id "audio_1";
107static const char kSdpStringWithStream1AudioTrackOnly[] =
108 "v=0\r\n"
109 "o=- 0 0 IN IP4 127.0.0.1\r\n"
110 "s=-\r\n"
111 "t=0 0\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800112 "m=audio 1 RTP/AVPF 103\r\n"
zhihuang81c3a032016-11-17 12:06:24 -0800113 "a=ice-ufrag:e5785931\r\n"
114 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
115 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
116 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
zhihuang81c3a032016-11-17 12:06:24 -0800117 "a=mid:audio\r\n"
118 "a=sendrecv\r\n"
119 "a=rtpmap:103 ISAC/16000\r\n"
120 "a=ssrc:1 cname:stream1\r\n"
121 "a=ssrc:1 mslabel:stream1\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800122 "a=ssrc:1 label:audiotrack0\r\n"
123 "a=rtcp-mux\r\n";
zhihuang81c3a032016-11-17 12:06:24 -0800124
deadbeefab9b2d12015-10-14 11:33:11 -0700125// Reference SDP with two MediaStreams with label "stream1" and "stream2. Each
126// MediaStreams have one audio track and one video track.
127// This uses MSID.
128static const char kSdpStringWithStream1And2[] =
129 "v=0\r\n"
130 "o=- 0 0 IN IP4 127.0.0.1\r\n"
131 "s=-\r\n"
132 "t=0 0\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800133 "a=msid-semantic: WMS stream1 stream2\r\n"
134 "m=audio 1 RTP/AVPF 103\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700135 "a=ice-ufrag:e5785931\r\n"
136 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
137 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
138 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700139 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700140 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800141 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700142 "a=rtpmap:103 ISAC/16000\r\n"
143 "a=ssrc:1 cname:stream1\r\n"
144 "a=ssrc:1 msid:stream1 audiotrack0\r\n"
145 "a=ssrc:3 cname:stream2\r\n"
146 "a=ssrc:3 msid:stream2 audiotrack1\r\n"
147 "m=video 1 RTP/AVPF 120\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800148 "a=ice-ufrag:e5785931\r\n"
149 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
150 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
151 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700152 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700153 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800154 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700155 "a=rtpmap:120 VP8/0\r\n"
156 "a=ssrc:2 cname:stream1\r\n"
157 "a=ssrc:2 msid:stream1 videotrack0\r\n"
158 "a=ssrc:4 cname:stream2\r\n"
159 "a=ssrc:4 msid:stream2 videotrack1\r\n";
160
161// Reference SDP without MediaStreams. Msid is not supported.
162static const char kSdpStringWithoutStreams[] =
163 "v=0\r\n"
164 "o=- 0 0 IN IP4 127.0.0.1\r\n"
165 "s=-\r\n"
166 "t=0 0\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800167 "m=audio 1 RTP/AVPF 103\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700168 "a=ice-ufrag:e5785931\r\n"
169 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
170 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
171 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700172 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700173 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800174 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700175 "a=rtpmap:103 ISAC/16000\r\n"
176 "m=video 1 RTP/AVPF 120\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800177 "a=ice-ufrag:e5785931\r\n"
178 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
179 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
180 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700181 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700182 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800183 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700184 "a=rtpmap:120 VP8/90000\r\n";
185
186// Reference SDP without MediaStreams. Msid is supported.
187static const char kSdpStringWithMsidWithoutStreams[] =
188 "v=0\r\n"
189 "o=- 0 0 IN IP4 127.0.0.1\r\n"
190 "s=-\r\n"
191 "t=0 0\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800192 "a=msid-semantic: WMS\r\n"
193 "m=audio 1 RTP/AVPF 103\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700194 "a=ice-ufrag:e5785931\r\n"
195 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
196 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
197 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700198 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700199 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800200 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700201 "a=rtpmap:103 ISAC/16000\r\n"
202 "m=video 1 RTP/AVPF 120\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800203 "a=ice-ufrag:e5785931\r\n"
204 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
205 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
206 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700207 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700208 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800209 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700210 "a=rtpmap:120 VP8/90000\r\n";
211
212// Reference SDP without MediaStreams and audio only.
213static const char kSdpStringWithoutStreamsAudioOnly[] =
214 "v=0\r\n"
215 "o=- 0 0 IN IP4 127.0.0.1\r\n"
216 "s=-\r\n"
217 "t=0 0\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800218 "m=audio 1 RTP/AVPF 103\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700219 "a=ice-ufrag:e5785931\r\n"
220 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
221 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
222 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700223 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700224 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800225 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700226 "a=rtpmap:103 ISAC/16000\r\n";
227
228// Reference SENDONLY SDP without MediaStreams. Msid is not supported.
229static const char kSdpStringSendOnlyWithoutStreams[] =
230 "v=0\r\n"
231 "o=- 0 0 IN IP4 127.0.0.1\r\n"
232 "s=-\r\n"
233 "t=0 0\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800234 "m=audio 1 RTP/AVPF 103\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700235 "a=ice-ufrag:e5785931\r\n"
236 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
237 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
238 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700239 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700240 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700241 "a=sendonly\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800242 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700243 "a=rtpmap:103 ISAC/16000\r\n"
244 "m=video 1 RTP/AVPF 120\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800245 "a=ice-ufrag:e5785931\r\n"
246 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
247 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
248 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700249 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700250 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700251 "a=sendonly\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800252 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700253 "a=rtpmap:120 VP8/90000\r\n";
254
255static const char kSdpStringInit[] =
256 "v=0\r\n"
257 "o=- 0 0 IN IP4 127.0.0.1\r\n"
258 "s=-\r\n"
259 "t=0 0\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700260 "a=msid-semantic: WMS\r\n";
261
262static const char kSdpStringAudio[] =
263 "m=audio 1 RTP/AVPF 103\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800264 "a=ice-ufrag:e5785931\r\n"
265 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
266 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
267 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700268 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700269 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800270 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700271 "a=rtpmap:103 ISAC/16000\r\n";
272
273static const char kSdpStringVideo[] =
274 "m=video 1 RTP/AVPF 120\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800275 "a=ice-ufrag:e5785931\r\n"
276 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
277 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
278 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700279 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700280 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800281 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700282 "a=rtpmap:120 VP8/90000\r\n";
283
284static const char kSdpStringMs1Audio0[] =
285 "a=ssrc:1 cname:stream1\r\n"
286 "a=ssrc:1 msid:stream1 audiotrack0\r\n";
287
288static const char kSdpStringMs1Video0[] =
289 "a=ssrc:2 cname:stream1\r\n"
290 "a=ssrc:2 msid:stream1 videotrack0\r\n";
291
292static const char kSdpStringMs1Audio1[] =
293 "a=ssrc:3 cname:stream1\r\n"
294 "a=ssrc:3 msid:stream1 audiotrack1\r\n";
295
296static const char kSdpStringMs1Video1[] =
297 "a=ssrc:4 cname:stream1\r\n"
298 "a=ssrc:4 msid:stream1 videotrack1\r\n";
299
deadbeef8662f942017-01-20 21:20:51 -0800300static const char kDtlsSdesFallbackSdp[] =
301 "v=0\r\n"
302 "o=xxxxxx 7 2 IN IP4 0.0.0.0\r\n"
303 "s=-\r\n"
304 "c=IN IP4 0.0.0.0\r\n"
305 "t=0 0\r\n"
306 "a=group:BUNDLE audio\r\n"
307 "a=msid-semantic: WMS\r\n"
308 "m=audio 1 RTP/SAVPF 0\r\n"
309 "a=sendrecv\r\n"
310 "a=rtcp-mux\r\n"
311 "a=mid:audio\r\n"
312 "a=ssrc:1 cname:stream1\r\n"
313 "a=ssrc:1 mslabel:stream1\r\n"
314 "a=ssrc:1 label:audiotrack0\r\n"
315 "a=ice-ufrag:e5785931\r\n"
316 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
317 "a=rtpmap:0 pcmu/8000\r\n"
318 "a=fingerprint:sha-1 "
319 "4A:AD:B9:B1:3F:82:18:3B:54:02:12:DF:3E:5D:49:6B:19:E5:7C:AB\r\n"
320 "a=setup:actpass\r\n"
321 "a=crypto:1 AES_CM_128_HMAC_SHA1_32 "
322 "inline:NzB4d1BINUAvLEw6UzF3WSJ+PSdFcGdUJShpX1Zj|2^20|1:32 "
323 "dummy_session_params\r\n";
324
perkjd61bf802016-03-24 03:16:19 -0700325using ::testing::Exactly;
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700326using cricket::StreamParams;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000327using webrtc::AudioSourceInterface;
deadbeefab9b2d12015-10-14 11:33:11 -0700328using webrtc::AudioTrack;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000329using webrtc::AudioTrackInterface;
330using webrtc::DataBuffer;
331using webrtc::DataChannelInterface;
332using webrtc::FakeConstraints;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000333using webrtc::IceCandidateInterface;
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700334using webrtc::JsepSessionDescription;
deadbeefc80741f2015-10-22 13:14:45 -0700335using webrtc::MediaConstraintsInterface;
deadbeefab9b2d12015-10-14 11:33:11 -0700336using webrtc::MediaStream;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000337using webrtc::MediaStreamInterface;
338using webrtc::MediaStreamTrackInterface;
339using webrtc::MockCreateSessionDescriptionObserver;
340using webrtc::MockDataChannelObserver;
341using webrtc::MockSetSessionDescriptionObserver;
342using webrtc::MockStatsObserver;
perkjd61bf802016-03-24 03:16:19 -0700343using webrtc::NotifierInterface;
344using webrtc::ObserverInterface;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000345using webrtc::PeerConnectionInterface;
346using webrtc::PeerConnectionObserver;
deadbeef293e9262017-01-11 12:28:30 -0800347using webrtc::RTCError;
348using webrtc::RTCErrorType;
deadbeefab9b2d12015-10-14 11:33:11 -0700349using webrtc::RtpReceiverInterface;
350using webrtc::RtpSenderInterface;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000351using webrtc::SdpParseError;
352using webrtc::SessionDescriptionInterface;
deadbeefab9b2d12015-10-14 11:33:11 -0700353using webrtc::StreamCollection;
354using webrtc::StreamCollectionInterface;
perkja3ede6c2016-03-08 01:27:48 +0100355using webrtc::VideoTrackSourceInterface;
deadbeefab9b2d12015-10-14 11:33:11 -0700356using webrtc::VideoTrack;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000357using webrtc::VideoTrackInterface;
358
deadbeefab9b2d12015-10-14 11:33:11 -0700359typedef PeerConnectionInterface::RTCOfferAnswerOptions RTCOfferAnswerOptions;
360
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000361namespace {
362
363// Gets the first ssrc of given content type from the ContentInfo.
364bool GetFirstSsrc(const cricket::ContentInfo* content_info, int* ssrc) {
365 if (!content_info || !ssrc) {
366 return false;
367 }
368 const cricket::MediaContentDescription* media_desc =
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000369 static_cast<const cricket::MediaContentDescription*>(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000370 content_info->description);
371 if (!media_desc || media_desc->streams().empty()) {
372 return false;
373 }
374 *ssrc = media_desc->streams().begin()->first_ssrc();
375 return true;
376}
377
deadbeefd1a38b52016-12-10 13:15:33 -0800378// Get the ufrags out of an SDP blob. Useful for testing ICE restart
379// behavior.
380std::vector<std::string> GetUfrags(
381 const webrtc::SessionDescriptionInterface* desc) {
382 std::vector<std::string> ufrags;
383 for (const cricket::TransportInfo& info :
384 desc->description()->transport_infos()) {
385 ufrags.push_back(info.description.ice_ufrag);
386 }
387 return ufrags;
388}
389
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000390void SetSsrcToZero(std::string* sdp) {
391 const char kSdpSsrcAtribute[] = "a=ssrc:";
392 const char kSdpSsrcAtributeZero[] = "a=ssrc:0";
393 size_t ssrc_pos = 0;
394 while ((ssrc_pos = sdp->find(kSdpSsrcAtribute, ssrc_pos)) !=
395 std::string::npos) {
396 size_t end_ssrc = sdp->find(" ", ssrc_pos);
397 sdp->replace(ssrc_pos, end_ssrc - ssrc_pos, kSdpSsrcAtributeZero);
398 ssrc_pos = end_ssrc;
399 }
400}
401
deadbeefab9b2d12015-10-14 11:33:11 -0700402// Check if |streams| contains the specified track.
403bool ContainsTrack(const std::vector<cricket::StreamParams>& streams,
404 const std::string& stream_label,
405 const std::string& track_id) {
406 for (const cricket::StreamParams& params : streams) {
407 if (params.sync_label == stream_label && params.id == track_id) {
408 return true;
409 }
410 }
411 return false;
412}
413
414// Check if |senders| contains the specified sender, by id.
415bool ContainsSender(
416 const std::vector<rtc::scoped_refptr<RtpSenderInterface>>& senders,
417 const std::string& id) {
418 for (const auto& sender : senders) {
419 if (sender->id() == id) {
420 return true;
421 }
422 }
423 return false;
424}
425
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700426// Check if |senders| contains the specified sender, by id and stream id.
427bool ContainsSender(
428 const std::vector<rtc::scoped_refptr<RtpSenderInterface>>& senders,
429 const std::string& id,
430 const std::string& stream_id) {
431 for (const auto& sender : senders) {
deadbeefa601f5c2016-06-06 14:27:39 -0700432 if (sender->id() == id && sender->stream_ids()[0] == stream_id) {
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700433 return true;
434 }
435 }
436 return false;
437}
438
deadbeefab9b2d12015-10-14 11:33:11 -0700439// Create a collection of streams.
440// CreateStreamCollection(1) creates a collection that
441// correspond to kSdpStringWithStream1.
442// CreateStreamCollection(2) correspond to kSdpStringWithStream1And2.
443rtc::scoped_refptr<StreamCollection> CreateStreamCollection(
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700444 int number_of_streams,
445 int tracks_per_stream) {
deadbeefab9b2d12015-10-14 11:33:11 -0700446 rtc::scoped_refptr<StreamCollection> local_collection(
447 StreamCollection::Create());
448
449 for (int i = 0; i < number_of_streams; ++i) {
450 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream(
451 webrtc::MediaStream::Create(kStreams[i]));
452
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700453 for (int j = 0; j < tracks_per_stream; ++j) {
454 // Add a local audio track.
455 rtc::scoped_refptr<webrtc::AudioTrackInterface> audio_track(
456 webrtc::AudioTrack::Create(kAudioTracks[i * tracks_per_stream + j],
457 nullptr));
458 stream->AddTrack(audio_track);
deadbeefab9b2d12015-10-14 11:33:11 -0700459
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700460 // Add a local video track.
461 rtc::scoped_refptr<webrtc::VideoTrackInterface> video_track(
462 webrtc::VideoTrack::Create(kVideoTracks[i * tracks_per_stream + j],
463 webrtc::FakeVideoTrackSource::Create()));
464 stream->AddTrack(video_track);
465 }
deadbeefab9b2d12015-10-14 11:33:11 -0700466
467 local_collection->AddStream(stream);
468 }
469 return local_collection;
470}
471
472// Check equality of StreamCollections.
473bool CompareStreamCollections(StreamCollectionInterface* s1,
474 StreamCollectionInterface* s2) {
475 if (s1 == nullptr || s2 == nullptr || s1->count() != s2->count()) {
476 return false;
477 }
478
479 for (size_t i = 0; i != s1->count(); ++i) {
480 if (s1->at(i)->label() != s2->at(i)->label()) {
481 return false;
482 }
483 webrtc::AudioTrackVector audio_tracks1 = s1->at(i)->GetAudioTracks();
484 webrtc::AudioTrackVector audio_tracks2 = s2->at(i)->GetAudioTracks();
485 webrtc::VideoTrackVector video_tracks1 = s1->at(i)->GetVideoTracks();
486 webrtc::VideoTrackVector video_tracks2 = s2->at(i)->GetVideoTracks();
487
488 if (audio_tracks1.size() != audio_tracks2.size()) {
489 return false;
490 }
491 for (size_t j = 0; j != audio_tracks1.size(); ++j) {
492 if (audio_tracks1[j]->id() != audio_tracks2[j]->id()) {
493 return false;
494 }
495 }
496 if (video_tracks1.size() != video_tracks2.size()) {
497 return false;
498 }
499 for (size_t j = 0; j != video_tracks1.size(); ++j) {
500 if (video_tracks1[j]->id() != video_tracks2[j]->id()) {
501 return false;
502 }
503 }
504 }
505 return true;
506}
507
perkjd61bf802016-03-24 03:16:19 -0700508// Helper class to test Observer.
509class MockTrackObserver : public ObserverInterface {
510 public:
511 explicit MockTrackObserver(NotifierInterface* notifier)
512 : notifier_(notifier) {
513 notifier_->RegisterObserver(this);
514 }
515
516 ~MockTrackObserver() { Unregister(); }
517
518 void Unregister() {
519 if (notifier_) {
520 notifier_->UnregisterObserver(this);
521 notifier_ = nullptr;
522 }
523 }
524
525 MOCK_METHOD0(OnChanged, void());
526
527 private:
528 NotifierInterface* notifier_;
529};
530
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000531class MockPeerConnectionObserver : public PeerConnectionObserver {
532 public:
deadbeefab9b2d12015-10-14 11:33:11 -0700533 MockPeerConnectionObserver() : remote_streams_(StreamCollection::Create()) {}
Henrik Kjellander3fe372d2016-05-12 08:10:52 +0200534 virtual ~MockPeerConnectionObserver() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000535 }
536 void SetPeerConnectionInterface(PeerConnectionInterface* pc) {
537 pc_ = pc;
538 if (pc) {
539 state_ = pc_->signaling_state();
540 }
541 }
nisseef8b61e2016-04-29 06:09:15 -0700542 void OnSignalingChange(
543 PeerConnectionInterface::SignalingState new_state) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000544 EXPECT_EQ(pc_->signaling_state(), new_state);
545 state_ = new_state;
546 }
deadbeefab9b2d12015-10-14 11:33:11 -0700547
548 MediaStreamInterface* RemoteStream(const std::string& label) {
549 return remote_streams_->find(label);
550 }
551 StreamCollectionInterface* remote_streams() const { return remote_streams_; }
Taylor Brandstetter98cde262016-05-31 13:02:21 -0700552 void OnAddStream(rtc::scoped_refptr<MediaStreamInterface> stream) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000553 last_added_stream_ = stream;
deadbeefab9b2d12015-10-14 11:33:11 -0700554 remote_streams_->AddStream(stream);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000555 }
Taylor Brandstetter98cde262016-05-31 13:02:21 -0700556 void OnRemoveStream(
557 rtc::scoped_refptr<MediaStreamInterface> stream) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000558 last_removed_stream_ = stream;
deadbeefab9b2d12015-10-14 11:33:11 -0700559 remote_streams_->RemoveStream(stream);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000560 }
perkjdfb769d2016-02-09 03:09:43 -0800561 void OnRenegotiationNeeded() override { renegotiation_needed_ = true; }
Taylor Brandstetter98cde262016-05-31 13:02:21 -0700562 void OnDataChannel(
563 rtc::scoped_refptr<DataChannelInterface> data_channel) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000564 last_datachannel_ = data_channel;
565 }
566
perkjdfb769d2016-02-09 03:09:43 -0800567 void OnIceConnectionChange(
568 PeerConnectionInterface::IceConnectionState new_state) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000569 EXPECT_EQ(pc_->ice_connection_state(), new_state);
zhihuang81c3a032016-11-17 12:06:24 -0800570 callback_triggered_ = true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000571 }
perkjdfb769d2016-02-09 03:09:43 -0800572 void OnIceGatheringChange(
573 PeerConnectionInterface::IceGatheringState new_state) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000574 EXPECT_EQ(pc_->ice_gathering_state(), new_state);
perkjdfb769d2016-02-09 03:09:43 -0800575 ice_complete_ = new_state == PeerConnectionInterface::kIceGatheringComplete;
zhihuang81c3a032016-11-17 12:06:24 -0800576 callback_triggered_ = true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000577 }
perkjdfb769d2016-02-09 03:09:43 -0800578 void OnIceCandidate(const webrtc::IceCandidateInterface* candidate) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000579 EXPECT_NE(PeerConnectionInterface::kIceGatheringNew,
580 pc_->ice_gathering_state());
581
582 std::string sdp;
583 EXPECT_TRUE(candidate->ToString(&sdp));
584 EXPECT_LT(0u, sdp.size());
585 last_candidate_.reset(webrtc::CreateIceCandidate(candidate->sdp_mid(),
586 candidate->sdp_mline_index(), sdp, NULL));
587 EXPECT_TRUE(last_candidate_.get() != NULL);
zhihuang81c3a032016-11-17 12:06:24 -0800588 callback_triggered_ = true;
zhihuang29ff8442016-07-27 11:07:25 -0700589 }
590
591 void OnIceCandidatesRemoved(
592 const std::vector<cricket::Candidate>& candidates) override {
zhihuang81c3a032016-11-17 12:06:24 -0800593 callback_triggered_ = true;
zhihuang29ff8442016-07-27 11:07:25 -0700594 }
595
596 void OnIceConnectionReceivingChange(bool receiving) override {
zhihuang81c3a032016-11-17 12:06:24 -0800597 callback_triggered_ = true;
598 }
599
zhihuangc63b8942016-12-02 15:41:10 -0800600 void OnAddTrack(
601 rtc::scoped_refptr<webrtc::RtpReceiverInterface> receiver,
602 const std::vector<rtc::scoped_refptr<webrtc::MediaStreamInterface>>&
603 streams) override {
zhihuang81c3a032016-11-17 12:06:24 -0800604 EXPECT_TRUE(receiver != nullptr);
605 num_added_tracks_++;
606 last_added_track_label_ = receiver->id();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000607 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000608
609 // Returns the label of the last added stream.
610 // Empty string if no stream have been added.
611 std::string GetLastAddedStreamLabel() {
612 if (last_added_stream_.get())
613 return last_added_stream_->label();
614 return "";
615 }
616 std::string GetLastRemovedStreamLabel() {
617 if (last_removed_stream_.get())
618 return last_removed_stream_->label();
619 return "";
620 }
621
zhihuang9763d562016-08-05 11:14:50 -0700622 rtc::scoped_refptr<PeerConnectionInterface> pc_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000623 PeerConnectionInterface::SignalingState state_;
kwibergd1fe2812016-04-27 06:47:29 -0700624 std::unique_ptr<IceCandidateInterface> last_candidate_;
zhihuang9763d562016-08-05 11:14:50 -0700625 rtc::scoped_refptr<DataChannelInterface> last_datachannel_;
deadbeefab9b2d12015-10-14 11:33:11 -0700626 rtc::scoped_refptr<StreamCollection> remote_streams_;
627 bool renegotiation_needed_ = false;
628 bool ice_complete_ = false;
zhihuang81c3a032016-11-17 12:06:24 -0800629 bool callback_triggered_ = false;
630 int num_added_tracks_ = 0;
631 std::string last_added_track_label_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000632
633 private:
zhihuang9763d562016-08-05 11:14:50 -0700634 rtc::scoped_refptr<MediaStreamInterface> last_added_stream_;
635 rtc::scoped_refptr<MediaStreamInterface> last_removed_stream_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000636};
637
638} // namespace
deadbeefab9b2d12015-10-14 11:33:11 -0700639
nisse528b7932017-05-08 03:21:43 -0700640// The PeerConnectionMediaConfig tests below verify that configuration and
641// constraints are propagated into the PeerConnection's MediaConfig. These
642// settings are intended for MediaChannel constructors, but that is not
643// exercised by these unittest.
zhihuang29ff8442016-07-27 11:07:25 -0700644class PeerConnectionFactoryForTest : public webrtc::PeerConnectionFactory {
645 public:
zhihuang38ede132017-06-15 12:52:32 -0700646 static rtc::scoped_refptr<PeerConnectionFactoryForTest>
647 CreatePeerConnectionFactoryForTest() {
648 auto audio_encoder_factory = webrtc::CreateBuiltinAudioEncoderFactory();
649 auto audio_decoder_factory = webrtc::CreateBuiltinAudioDecoderFactory();
650
651 auto media_engine = std::unique_ptr<cricket::MediaEngineInterface>(
652 cricket::WebRtcMediaEngineFactory::Create(
653 nullptr, audio_encoder_factory, audio_decoder_factory, nullptr,
peaha9cc40b2017-06-29 08:32:09 -0700654 nullptr, nullptr, webrtc::AudioProcessing::Create()));
zhihuang38ede132017-06-15 12:52:32 -0700655
656 std::unique_ptr<webrtc::CallFactoryInterface> call_factory =
657 webrtc::CreateCallFactory();
658
659 std::unique_ptr<webrtc::RtcEventLogFactoryInterface> event_log_factory =
660 webrtc::CreateRtcEventLogFactory();
661
662 return new rtc::RefCountedObject<PeerConnectionFactoryForTest>(
663 rtc::Thread::Current(), rtc::Thread::Current(), rtc::Thread::Current(),
664 nullptr, audio_encoder_factory, audio_decoder_factory, nullptr, nullptr,
665 nullptr, std::move(media_engine), std::move(call_factory),
666 std::move(event_log_factory));
667 }
668
669 PeerConnectionFactoryForTest(
670 rtc::Thread* network_thread,
671 rtc::Thread* worker_thread,
672 rtc::Thread* signaling_thread,
673 webrtc::AudioDeviceModule* default_adm,
674 rtc::scoped_refptr<webrtc::AudioEncoderFactory> audio_encoder_factory,
675 rtc::scoped_refptr<webrtc::AudioDecoderFactory> audio_decoder_factory,
676 cricket::WebRtcVideoEncoderFactory* video_encoder_factory,
677 cricket::WebRtcVideoDecoderFactory* video_decoder_factory,
678 rtc::scoped_refptr<webrtc::AudioMixer> audio_mixer,
679 std::unique_ptr<cricket::MediaEngineInterface> media_engine,
680 std::unique_ptr<webrtc::CallFactoryInterface> call_factory,
681 std::unique_ptr<webrtc::RtcEventLogFactoryInterface> event_log_factory)
682 : webrtc::PeerConnectionFactory(network_thread,
683 worker_thread,
684 signaling_thread,
685 default_adm,
686 audio_encoder_factory,
687 audio_decoder_factory,
688 video_encoder_factory,
689 video_decoder_factory,
690 audio_mixer,
691 std::move(media_engine),
692 std::move(call_factory),
693 std::move(event_log_factory)) {}
kwiberg1e4e8cb2017-01-31 01:48:08 -0800694
zhihuang29ff8442016-07-27 11:07:25 -0700695 cricket::TransportController* CreateTransportController(
Honghai Zhangbfd398c2016-08-30 22:07:42 -0700696 cricket::PortAllocator* port_allocator,
697 bool redetermine_role_on_ice_restart) override {
zhihuang29ff8442016-07-27 11:07:25 -0700698 transport_controller = new cricket::TransportController(
Honghai Zhangbfd398c2016-08-30 22:07:42 -0700699 rtc::Thread::Current(), rtc::Thread::Current(), port_allocator,
deadbeef7914b8c2017-04-21 03:23:33 -0700700 redetermine_role_on_ice_restart, rtc::CryptoOptions());
zhihuang29ff8442016-07-27 11:07:25 -0700701 return transport_controller;
702 }
703
704 cricket::TransportController* transport_controller;
zhihuang29ff8442016-07-27 11:07:25 -0700705};
706
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000707class PeerConnectionInterfaceTest : public testing::Test {
708 protected:
deadbeef9a6f4d42017-05-15 19:43:33 -0700709 PeerConnectionInterfaceTest()
deadbeef98e186c2017-05-16 18:00:06 -0700710 : vss_(new rtc::VirtualSocketServer()), main_(vss_.get()) {
phoglund37ebcf02016-01-08 05:04:57 -0800711#ifdef WEBRTC_ANDROID
712 webrtc::InitializeAndroidObjects();
713#endif
714 }
715
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000716 virtual void SetUp() {
717 pc_factory_ = webrtc::CreatePeerConnectionFactory(
danilchape9021a32016-05-17 01:52:02 -0700718 rtc::Thread::Current(), rtc::Thread::Current(), rtc::Thread::Current(),
719 nullptr, nullptr, nullptr);
720 ASSERT_TRUE(pc_factory_);
zhihuang29ff8442016-07-27 11:07:25 -0700721 pc_factory_for_test_ =
zhihuang38ede132017-06-15 12:52:32 -0700722 PeerConnectionFactoryForTest::CreatePeerConnectionFactoryForTest();
zhihuang29ff8442016-07-27 11:07:25 -0700723 pc_factory_for_test_->Initialize();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000724 }
725
726 void CreatePeerConnection() {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700727 CreatePeerConnection(PeerConnectionInterface::RTCConfiguration(), nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000728 }
729
deadbeef293e9262017-01-11 12:28:30 -0800730 // DTLS does not work in a loopback call, so is disabled for most of the
731 // tests in this file.
732 void CreatePeerConnectionWithoutDtls() {
733 FakeConstraints no_dtls_constraints;
734 no_dtls_constraints.AddMandatory(
735 webrtc::MediaConstraintsInterface::kEnableDtlsSrtp, false);
736
737 CreatePeerConnection(PeerConnectionInterface::RTCConfiguration(),
738 &no_dtls_constraints);
739 }
740
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000741 void CreatePeerConnection(webrtc::MediaConstraintsInterface* constraints) {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700742 CreatePeerConnection(PeerConnectionInterface::RTCConfiguration(),
743 constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000744 }
745
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700746 void CreatePeerConnectionWithIceTransportsType(
747 PeerConnectionInterface::IceTransportsType type) {
748 PeerConnectionInterface::RTCConfiguration config;
749 config.type = type;
750 return CreatePeerConnection(config, nullptr);
751 }
752
753 void CreatePeerConnectionWithIceServer(const std::string& uri,
754 const std::string& password) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800755 PeerConnectionInterface::RTCConfiguration config;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000756 PeerConnectionInterface::IceServer server;
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700757 server.uri = uri;
758 server.password = password;
759 config.servers.push_back(server);
760 CreatePeerConnection(config, nullptr);
761 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000762
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700763 void CreatePeerConnection(PeerConnectionInterface::RTCConfiguration config,
764 webrtc::MediaConstraintsInterface* constraints) {
kwibergd1fe2812016-04-27 06:47:29 -0700765 std::unique_ptr<cricket::FakePortAllocator> port_allocator(
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800766 new cricket::FakePortAllocator(rtc::Thread::Current(), nullptr));
767 port_allocator_ = port_allocator.get();
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000768
deadbeef1dcb1642017-03-29 21:08:16 -0700769 // Create certificate generator unless DTLS constraint is explicitly set to
770 // false.
Henrik Boströmd79599d2016-06-01 13:58:50 +0200771 std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator;
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000772 bool dtls;
773 if (FindConstraint(constraints,
774 webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
775 &dtls,
Henrik Boström5e56c592015-08-11 10:33:13 +0200776 nullptr) && dtls) {
deadbeef8662f942017-01-20 21:20:51 -0800777 fake_certificate_generator_ = new FakeRTCCertificateGenerator();
778 cert_generator.reset(fake_certificate_generator_);
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000779 }
Henrik Boströmd79599d2016-06-01 13:58:50 +0200780 pc_ = pc_factory_->CreatePeerConnection(
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800781 config, constraints, std::move(port_allocator),
Henrik Boströmd79599d2016-06-01 13:58:50 +0200782 std::move(cert_generator), &observer_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000783 ASSERT_TRUE(pc_.get() != NULL);
784 observer_.SetPeerConnectionInterface(pc_.get());
785 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
786 }
787
deadbeef0a6c4ca2015-10-06 11:38:28 -0700788 void CreatePeerConnectionExpectFail(const std::string& uri) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800789 PeerConnectionInterface::RTCConfiguration config;
deadbeef0a6c4ca2015-10-06 11:38:28 -0700790 PeerConnectionInterface::IceServer server;
deadbeef0a6c4ca2015-10-06 11:38:28 -0700791 server.uri = uri;
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800792 config.servers.push_back(server);
deadbeef0a6c4ca2015-10-06 11:38:28 -0700793
zhihuang9763d562016-08-05 11:14:50 -0700794 rtc::scoped_refptr<PeerConnectionInterface> pc;
hbosd7973cc2016-05-27 06:08:53 -0700795 pc = pc_factory_->CreatePeerConnection(config, nullptr, nullptr, nullptr,
796 &observer_);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800797 EXPECT_EQ(nullptr, pc);
deadbeef0a6c4ca2015-10-06 11:38:28 -0700798 }
799
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000800 void CreatePeerConnectionWithDifferentConfigurations() {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700801 CreatePeerConnectionWithIceServer(kStunAddressOnly, "");
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800802 EXPECT_EQ(1u, port_allocator_->stun_servers().size());
803 EXPECT_EQ(0u, port_allocator_->turn_servers().size());
804 EXPECT_EQ("address", port_allocator_->stun_servers().begin()->hostname());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000805 EXPECT_EQ(kDefaultStunPort,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800806 port_allocator_->stun_servers().begin()->port());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000807
deadbeef0a6c4ca2015-10-06 11:38:28 -0700808 CreatePeerConnectionExpectFail(kStunInvalidPort);
809 CreatePeerConnectionExpectFail(kStunAddressPortAndMore1);
810 CreatePeerConnectionExpectFail(kStunAddressPortAndMore2);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000811
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700812 CreatePeerConnectionWithIceServer(kTurnIceServerUri, kTurnPassword);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800813 EXPECT_EQ(0u, port_allocator_->stun_servers().size());
814 EXPECT_EQ(1u, port_allocator_->turn_servers().size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000815 EXPECT_EQ(kTurnUsername,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800816 port_allocator_->turn_servers()[0].credentials.username);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000817 EXPECT_EQ(kTurnPassword,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800818 port_allocator_->turn_servers()[0].credentials.password);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000819 EXPECT_EQ(kTurnHostname,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800820 port_allocator_->turn_servers()[0].ports[0].address.hostname());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000821 }
822
823 void ReleasePeerConnection() {
824 pc_ = NULL;
825 observer_.SetPeerConnectionInterface(NULL);
826 }
827
deadbeefab9b2d12015-10-14 11:33:11 -0700828 void AddVideoStream(const std::string& label) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000829 // Create a local stream.
zhihuang9763d562016-08-05 11:14:50 -0700830 rtc::scoped_refptr<MediaStreamInterface> stream(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000831 pc_factory_->CreateLocalMediaStream(label));
zhihuang9763d562016-08-05 11:14:50 -0700832 rtc::scoped_refptr<VideoTrackSourceInterface> video_source(
deadbeef112b2e92017-02-10 20:13:37 -0800833 pc_factory_->CreateVideoSource(std::unique_ptr<cricket::VideoCapturer>(
834 new cricket::FakeVideoCapturer()),
835 NULL));
zhihuang9763d562016-08-05 11:14:50 -0700836 rtc::scoped_refptr<VideoTrackInterface> video_track(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000837 pc_factory_->CreateVideoTrack(label + "v0", video_source));
838 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
844 void AddVoiceStream(const std::string& label) {
845 // Create a local stream.
zhihuang9763d562016-08-05 11:14:50 -0700846 rtc::scoped_refptr<MediaStreamInterface> stream(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000847 pc_factory_->CreateLocalMediaStream(label));
zhihuang9763d562016-08-05 11:14:50 -0700848 rtc::scoped_refptr<AudioTrackInterface> audio_track(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000849 pc_factory_->CreateAudioTrack(label + "a0", NULL));
850 stream->AddTrack(audio_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +0000851 EXPECT_TRUE(pc_->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000852 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
853 observer_.renegotiation_needed_ = false;
854 }
855
856 void AddAudioVideoStream(const std::string& stream_label,
857 const std::string& audio_track_label,
858 const std::string& video_track_label) {
859 // Create a local stream.
zhihuang9763d562016-08-05 11:14:50 -0700860 rtc::scoped_refptr<MediaStreamInterface> stream(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000861 pc_factory_->CreateLocalMediaStream(stream_label));
zhihuang9763d562016-08-05 11:14:50 -0700862 rtc::scoped_refptr<AudioTrackInterface> audio_track(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000863 pc_factory_->CreateAudioTrack(
864 audio_track_label, static_cast<AudioSourceInterface*>(NULL)));
865 stream->AddTrack(audio_track.get());
zhihuang9763d562016-08-05 11:14:50 -0700866 rtc::scoped_refptr<VideoTrackInterface> video_track(
nisseaf510af2016-03-21 08:20:42 -0700867 pc_factory_->CreateVideoTrack(
deadbeef112b2e92017-02-10 20:13:37 -0800868 video_track_label, pc_factory_->CreateVideoSource(
869 std::unique_ptr<cricket::VideoCapturer>(
870 new cricket::FakeVideoCapturer()))));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000871 stream->AddTrack(video_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +0000872 EXPECT_TRUE(pc_->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000873 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
874 observer_.renegotiation_needed_ = false;
875 }
876
kwibergd1fe2812016-04-27 06:47:29 -0700877 bool DoCreateOfferAnswer(std::unique_ptr<SessionDescriptionInterface>* desc,
deadbeefc80741f2015-10-22 13:14:45 -0700878 bool offer,
879 MediaConstraintsInterface* constraints) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000880 rtc::scoped_refptr<MockCreateSessionDescriptionObserver>
881 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000882 MockCreateSessionDescriptionObserver>());
883 if (offer) {
deadbeefc80741f2015-10-22 13:14:45 -0700884 pc_->CreateOffer(observer, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000885 } else {
deadbeefc80741f2015-10-22 13:14:45 -0700886 pc_->CreateAnswer(observer, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000887 }
888 EXPECT_EQ_WAIT(true, observer->called(), kTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -0700889 *desc = observer->MoveDescription();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000890 return observer->result();
891 }
892
kwibergd1fe2812016-04-27 06:47:29 -0700893 bool DoCreateOffer(std::unique_ptr<SessionDescriptionInterface>* desc,
deadbeefc80741f2015-10-22 13:14:45 -0700894 MediaConstraintsInterface* constraints) {
895 return DoCreateOfferAnswer(desc, true, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000896 }
897
kwibergd1fe2812016-04-27 06:47:29 -0700898 bool DoCreateAnswer(std::unique_ptr<SessionDescriptionInterface>* desc,
deadbeefc80741f2015-10-22 13:14:45 -0700899 MediaConstraintsInterface* constraints) {
900 return DoCreateOfferAnswer(desc, false, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000901 }
902
903 bool DoSetSessionDescription(SessionDescriptionInterface* desc, bool local) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000904 rtc::scoped_refptr<MockSetSessionDescriptionObserver>
905 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000906 MockSetSessionDescriptionObserver>());
907 if (local) {
908 pc_->SetLocalDescription(observer, desc);
909 } else {
910 pc_->SetRemoteDescription(observer, desc);
911 }
zhihuang29ff8442016-07-27 11:07:25 -0700912 if (pc_->signaling_state() != PeerConnectionInterface::kClosed) {
913 EXPECT_EQ_WAIT(true, observer->called(), kTimeout);
914 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000915 return observer->result();
916 }
917
918 bool DoSetLocalDescription(SessionDescriptionInterface* desc) {
919 return DoSetSessionDescription(desc, true);
920 }
921
922 bool DoSetRemoteDescription(SessionDescriptionInterface* desc) {
923 return DoSetSessionDescription(desc, false);
924 }
925
926 // Calls PeerConnection::GetStats and check the return value.
927 // It does not verify the values in the StatReports since a RTCP packet might
928 // be required.
929 bool DoGetStats(MediaStreamTrackInterface* track) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000930 rtc::scoped_refptr<MockStatsObserver> observer(
931 new rtc::RefCountedObject<MockStatsObserver>());
jiayl@webrtc.orgdb41b4d2014-03-03 21:30:06 +0000932 if (!pc_->GetStats(
933 observer, track, PeerConnectionInterface::kStatsOutputLevelStandard))
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000934 return false;
935 EXPECT_TRUE_WAIT(observer->called(), kTimeout);
936 return observer->called();
937 }
938
939 void InitiateCall() {
deadbeef293e9262017-01-11 12:28:30 -0800940 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000941 // Create a local stream with audio&video tracks.
942 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
943 CreateOfferReceiveAnswer();
944 }
945
946 // Verify that RTP Header extensions has been negotiated for audio and video.
947 void VerifyRemoteRtpHeaderExtensions() {
948 const cricket::MediaContentDescription* desc =
949 cricket::GetFirstAudioContentDescription(
950 pc_->remote_description()->description());
951 ASSERT_TRUE(desc != NULL);
952 EXPECT_GT(desc->rtp_header_extensions().size(), 0u);
953
954 desc = cricket::GetFirstVideoContentDescription(
955 pc_->remote_description()->description());
956 ASSERT_TRUE(desc != NULL);
957 EXPECT_GT(desc->rtp_header_extensions().size(), 0u);
958 }
959
960 void CreateOfferAsRemoteDescription() {
kwibergd1fe2812016-04-27 06:47:29 -0700961 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -0700962 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000963 std::string sdp;
964 EXPECT_TRUE(offer->ToString(&sdp));
965 SessionDescriptionInterface* remote_offer =
966 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
967 sdp, NULL);
968 EXPECT_TRUE(DoSetRemoteDescription(remote_offer));
969 EXPECT_EQ(PeerConnectionInterface::kHaveRemoteOffer, observer_.state_);
970 }
971
deadbeefab9b2d12015-10-14 11:33:11 -0700972 void CreateAndSetRemoteOffer(const std::string& sdp) {
973 SessionDescriptionInterface* remote_offer =
974 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
975 sdp, nullptr);
976 EXPECT_TRUE(DoSetRemoteDescription(remote_offer));
977 EXPECT_EQ(PeerConnectionInterface::kHaveRemoteOffer, observer_.state_);
978 }
979
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000980 void CreateAnswerAsLocalDescription() {
kwibergd1fe2812016-04-27 06:47:29 -0700981 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -0700982 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000983
984 // TODO(perkj): Currently SetLocalDescription fails if any parameters in an
985 // audio codec change, even if the parameter has nothing to do with
986 // receiving. Not all parameters are serialized to SDP.
987 // Since CreatePrAnswerAsLocalDescription serialize/deserialize
988 // the SessionDescription, it is necessary to do that here to in order to
989 // get ReceiveOfferCreatePrAnswerAndAnswer and RenegotiateAudioOnly to pass.
990 // https://code.google.com/p/webrtc/issues/detail?id=1356
991 std::string sdp;
992 EXPECT_TRUE(answer->ToString(&sdp));
993 SessionDescriptionInterface* new_answer =
994 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
995 sdp, NULL);
996 EXPECT_TRUE(DoSetLocalDescription(new_answer));
997 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
998 }
999
1000 void CreatePrAnswerAsLocalDescription() {
kwibergd1fe2812016-04-27 06:47:29 -07001001 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -07001002 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001003
1004 std::string sdp;
1005 EXPECT_TRUE(answer->ToString(&sdp));
1006 SessionDescriptionInterface* pr_answer =
1007 webrtc::CreateSessionDescription(SessionDescriptionInterface::kPrAnswer,
1008 sdp, NULL);
1009 EXPECT_TRUE(DoSetLocalDescription(pr_answer));
1010 EXPECT_EQ(PeerConnectionInterface::kHaveLocalPrAnswer, observer_.state_);
1011 }
1012
1013 void CreateOfferReceiveAnswer() {
1014 CreateOfferAsLocalDescription();
1015 std::string sdp;
1016 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
1017 CreateAnswerAsRemoteDescription(sdp);
1018 }
1019
1020 void CreateOfferAsLocalDescription() {
kwibergd1fe2812016-04-27 06:47:29 -07001021 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001022 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001023 // TODO(perkj): Currently SetLocalDescription fails if any parameters in an
1024 // audio codec change, even if the parameter has nothing to do with
1025 // receiving. Not all parameters are serialized to SDP.
1026 // Since CreatePrAnswerAsLocalDescription serialize/deserialize
1027 // the SessionDescription, it is necessary to do that here to in order to
1028 // get ReceiveOfferCreatePrAnswerAndAnswer and RenegotiateAudioOnly to pass.
1029 // https://code.google.com/p/webrtc/issues/detail?id=1356
1030 std::string sdp;
1031 EXPECT_TRUE(offer->ToString(&sdp));
1032 SessionDescriptionInterface* new_offer =
1033 webrtc::CreateSessionDescription(
1034 SessionDescriptionInterface::kOffer,
1035 sdp, NULL);
1036
1037 EXPECT_TRUE(DoSetLocalDescription(new_offer));
1038 EXPECT_EQ(PeerConnectionInterface::kHaveLocalOffer, observer_.state_);
mallinath@webrtc.org68cbd012014-01-22 00:16:46 +00001039 // Wait for the ice_complete message, so that SDP will have candidates.
1040 EXPECT_TRUE_WAIT(observer_.ice_complete_, kTimeout);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001041 }
1042
deadbeefab9b2d12015-10-14 11:33:11 -07001043 void CreateAnswerAsRemoteDescription(const std::string& sdp) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001044 webrtc::JsepSessionDescription* answer = new webrtc::JsepSessionDescription(
1045 SessionDescriptionInterface::kAnswer);
deadbeefab9b2d12015-10-14 11:33:11 -07001046 EXPECT_TRUE(answer->Initialize(sdp, NULL));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001047 EXPECT_TRUE(DoSetRemoteDescription(answer));
1048 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
1049 }
1050
deadbeefab9b2d12015-10-14 11:33:11 -07001051 void CreatePrAnswerAndAnswerAsRemoteDescription(const std::string& sdp) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001052 webrtc::JsepSessionDescription* pr_answer =
1053 new webrtc::JsepSessionDescription(
1054 SessionDescriptionInterface::kPrAnswer);
deadbeefab9b2d12015-10-14 11:33:11 -07001055 EXPECT_TRUE(pr_answer->Initialize(sdp, NULL));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001056 EXPECT_TRUE(DoSetRemoteDescription(pr_answer));
1057 EXPECT_EQ(PeerConnectionInterface::kHaveRemotePrAnswer, observer_.state_);
1058 webrtc::JsepSessionDescription* answer =
1059 new webrtc::JsepSessionDescription(
1060 SessionDescriptionInterface::kAnswer);
deadbeefab9b2d12015-10-14 11:33:11 -07001061 EXPECT_TRUE(answer->Initialize(sdp, NULL));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001062 EXPECT_TRUE(DoSetRemoteDescription(answer));
1063 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
1064 }
1065
1066 // Help function used for waiting until a the last signaled remote stream has
1067 // the same label as |stream_label|. In a few of the tests in this file we
1068 // answer with the same session description as we offer and thus we can
1069 // check if OnAddStream have been called with the same stream as we offer to
1070 // send.
1071 void WaitAndVerifyOnAddStream(const std::string& stream_label) {
1072 EXPECT_EQ_WAIT(stream_label, observer_.GetLastAddedStreamLabel(), kTimeout);
1073 }
1074
1075 // Creates an offer and applies it as a local session description.
1076 // Creates an answer with the same SDP an the offer but removes all lines
1077 // that start with a:ssrc"
1078 void CreateOfferReceiveAnswerWithoutSsrc() {
1079 CreateOfferAsLocalDescription();
1080 std::string sdp;
1081 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
1082 SetSsrcToZero(&sdp);
1083 CreateAnswerAsRemoteDescription(sdp);
1084 }
1085
deadbeefab9b2d12015-10-14 11:33:11 -07001086 // This function creates a MediaStream with label kStreams[0] and
1087 // |number_of_audio_tracks| and |number_of_video_tracks| tracks and the
1088 // corresponding SessionDescriptionInterface. The SessionDescriptionInterface
kwiberg2bbff992016-03-16 11:03:04 -07001089 // is returned and the MediaStream is stored in
deadbeefab9b2d12015-10-14 11:33:11 -07001090 // |reference_collection_|
kwibergd1fe2812016-04-27 06:47:29 -07001091 std::unique_ptr<SessionDescriptionInterface>
kwiberg2bbff992016-03-16 11:03:04 -07001092 CreateSessionDescriptionAndReference(size_t number_of_audio_tracks,
1093 size_t number_of_video_tracks) {
1094 EXPECT_LE(number_of_audio_tracks, 2u);
1095 EXPECT_LE(number_of_video_tracks, 2u);
deadbeefab9b2d12015-10-14 11:33:11 -07001096
1097 reference_collection_ = StreamCollection::Create();
1098 std::string sdp_ms1 = std::string(kSdpStringInit);
1099
1100 std::string mediastream_label = kStreams[0];
1101
1102 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream(
1103 webrtc::MediaStream::Create(mediastream_label));
1104 reference_collection_->AddStream(stream);
1105
1106 if (number_of_audio_tracks > 0) {
1107 sdp_ms1 += std::string(kSdpStringAudio);
1108 sdp_ms1 += std::string(kSdpStringMs1Audio0);
1109 AddAudioTrack(kAudioTracks[0], stream);
1110 }
1111 if (number_of_audio_tracks > 1) {
1112 sdp_ms1 += kSdpStringMs1Audio1;
1113 AddAudioTrack(kAudioTracks[1], stream);
1114 }
1115
1116 if (number_of_video_tracks > 0) {
1117 sdp_ms1 += std::string(kSdpStringVideo);
1118 sdp_ms1 += std::string(kSdpStringMs1Video0);
1119 AddVideoTrack(kVideoTracks[0], stream);
1120 }
1121 if (number_of_video_tracks > 1) {
1122 sdp_ms1 += kSdpStringMs1Video1;
1123 AddVideoTrack(kVideoTracks[1], stream);
1124 }
1125
kwibergd1fe2812016-04-27 06:47:29 -07001126 return std::unique_ptr<SessionDescriptionInterface>(
kwiberg2bbff992016-03-16 11:03:04 -07001127 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
1128 sdp_ms1, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07001129 }
1130
1131 void AddAudioTrack(const std::string& track_id,
1132 MediaStreamInterface* stream) {
1133 rtc::scoped_refptr<webrtc::AudioTrackInterface> audio_track(
1134 webrtc::AudioTrack::Create(track_id, nullptr));
1135 ASSERT_TRUE(stream->AddTrack(audio_track));
1136 }
1137
1138 void AddVideoTrack(const std::string& track_id,
1139 MediaStreamInterface* stream) {
1140 rtc::scoped_refptr<webrtc::VideoTrackInterface> video_track(
nisseaf510af2016-03-21 08:20:42 -07001141 webrtc::VideoTrack::Create(track_id,
1142 webrtc::FakeVideoTrackSource::Create()));
deadbeefab9b2d12015-10-14 11:33:11 -07001143 ASSERT_TRUE(stream->AddTrack(video_track));
1144 }
1145
kwibergfd8be342016-05-14 19:44:11 -07001146 std::unique_ptr<SessionDescriptionInterface> CreateOfferWithOneAudioStream() {
deadbeef293e9262017-01-11 12:28:30 -08001147 CreatePeerConnectionWithoutDtls();
zhihuang8f65cdf2016-05-06 18:40:30 -07001148 AddVoiceStream(kStreamLabel1);
kwibergfd8be342016-05-14 19:44:11 -07001149 std::unique_ptr<SessionDescriptionInterface> offer;
zhihuang8f65cdf2016-05-06 18:40:30 -07001150 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
1151 return offer;
1152 }
1153
kwibergfd8be342016-05-14 19:44:11 -07001154 std::unique_ptr<SessionDescriptionInterface>
zhihuang8f65cdf2016-05-06 18:40:30 -07001155 CreateAnswerWithOneAudioStream() {
kwibergfd8be342016-05-14 19:44:11 -07001156 std::unique_ptr<SessionDescriptionInterface> offer =
zhihuang8f65cdf2016-05-06 18:40:30 -07001157 CreateOfferWithOneAudioStream();
1158 EXPECT_TRUE(DoSetRemoteDescription(offer.release()));
kwibergfd8be342016-05-14 19:44:11 -07001159 std::unique_ptr<SessionDescriptionInterface> answer;
zhihuang8f65cdf2016-05-06 18:40:30 -07001160 EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
1161 return answer;
1162 }
1163
1164 const std::string& GetFirstAudioStreamCname(
1165 const SessionDescriptionInterface* desc) {
1166 const cricket::ContentInfo* audio_content =
1167 cricket::GetFirstAudioContent(desc->description());
1168 const cricket::AudioContentDescription* audio_desc =
1169 static_cast<const cricket::AudioContentDescription*>(
1170 audio_content->description);
1171 return audio_desc->streams()[0].cname;
1172 }
1173
deadbeef9a6f4d42017-05-15 19:43:33 -07001174 std::unique_ptr<rtc::VirtualSocketServer> vss_;
1175 rtc::AutoSocketServerThread main_;
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08001176 cricket::FakePortAllocator* port_allocator_ = nullptr;
deadbeef8662f942017-01-20 21:20:51 -08001177 FakeRTCCertificateGenerator* fake_certificate_generator_ = nullptr;
zhihuang9763d562016-08-05 11:14:50 -07001178 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface> pc_factory_;
1179 rtc::scoped_refptr<PeerConnectionFactoryForTest> pc_factory_for_test_;
1180 rtc::scoped_refptr<PeerConnectionInterface> pc_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001181 MockPeerConnectionObserver observer_;
deadbeefab9b2d12015-10-14 11:33:11 -07001182 rtc::scoped_refptr<StreamCollection> reference_collection_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001183};
1184
zhihuang29ff8442016-07-27 11:07:25 -07001185// Test that no callbacks on the PeerConnectionObserver are called after the
1186// PeerConnection is closed.
1187TEST_F(PeerConnectionInterfaceTest, CloseAndTestCallbackFunctions) {
zhihuang9763d562016-08-05 11:14:50 -07001188 rtc::scoped_refptr<PeerConnectionInterface> pc(
zhihuang29ff8442016-07-27 11:07:25 -07001189 pc_factory_for_test_->CreatePeerConnection(
1190 PeerConnectionInterface::RTCConfiguration(), nullptr, nullptr,
1191 nullptr, &observer_));
1192 observer_.SetPeerConnectionInterface(pc.get());
1193 pc->Close();
1194
1195 // No callbacks is expected to be called.
zhihuang81c3a032016-11-17 12:06:24 -08001196 observer_.callback_triggered_ = false;
zhihuang29ff8442016-07-27 11:07:25 -07001197 std::vector<cricket::Candidate> candidates;
1198 pc_factory_for_test_->transport_controller->SignalGatheringState(
1199 cricket::IceGatheringState{});
1200 pc_factory_for_test_->transport_controller->SignalCandidatesGathered(
1201 "", candidates);
1202 pc_factory_for_test_->transport_controller->SignalConnectionState(
1203 cricket::IceConnectionState{});
1204 pc_factory_for_test_->transport_controller->SignalCandidatesRemoved(
1205 candidates);
1206 pc_factory_for_test_->transport_controller->SignalReceiving(false);
zhihuang81c3a032016-11-17 12:06:24 -08001207 EXPECT_FALSE(observer_.callback_triggered_);
zhihuang29ff8442016-07-27 11:07:25 -07001208}
1209
zhihuang8f65cdf2016-05-06 18:40:30 -07001210// Generate different CNAMEs when PeerConnections are created.
1211// The CNAMEs are expected to be generated randomly. It is possible
1212// that the test fails, though the possibility is very low.
1213TEST_F(PeerConnectionInterfaceTest, CnameGenerationInOffer) {
kwibergfd8be342016-05-14 19:44:11 -07001214 std::unique_ptr<SessionDescriptionInterface> offer1 =
zhihuang8f65cdf2016-05-06 18:40:30 -07001215 CreateOfferWithOneAudioStream();
kwibergfd8be342016-05-14 19:44:11 -07001216 std::unique_ptr<SessionDescriptionInterface> offer2 =
zhihuang8f65cdf2016-05-06 18:40:30 -07001217 CreateOfferWithOneAudioStream();
1218 EXPECT_NE(GetFirstAudioStreamCname(offer1.get()),
1219 GetFirstAudioStreamCname(offer2.get()));
1220}
1221
1222TEST_F(PeerConnectionInterfaceTest, CnameGenerationInAnswer) {
kwibergfd8be342016-05-14 19:44:11 -07001223 std::unique_ptr<SessionDescriptionInterface> answer1 =
zhihuang8f65cdf2016-05-06 18:40:30 -07001224 CreateAnswerWithOneAudioStream();
kwibergfd8be342016-05-14 19:44:11 -07001225 std::unique_ptr<SessionDescriptionInterface> answer2 =
zhihuang8f65cdf2016-05-06 18:40:30 -07001226 CreateAnswerWithOneAudioStream();
1227 EXPECT_NE(GetFirstAudioStreamCname(answer1.get()),
1228 GetFirstAudioStreamCname(answer2.get()));
1229}
1230
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001231TEST_F(PeerConnectionInterfaceTest,
1232 CreatePeerConnectionWithDifferentConfigurations) {
1233 CreatePeerConnectionWithDifferentConfigurations();
1234}
1235
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001236TEST_F(PeerConnectionInterfaceTest,
1237 CreatePeerConnectionWithDifferentIceTransportsTypes) {
1238 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kNone);
1239 EXPECT_EQ(cricket::CF_NONE, port_allocator_->candidate_filter());
1240 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kRelay);
1241 EXPECT_EQ(cricket::CF_RELAY, port_allocator_->candidate_filter());
1242 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kNoHost);
1243 EXPECT_EQ(cricket::CF_ALL & ~cricket::CF_HOST,
1244 port_allocator_->candidate_filter());
1245 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kAll);
1246 EXPECT_EQ(cricket::CF_ALL, port_allocator_->candidate_filter());
1247}
1248
1249// Test that when a PeerConnection is created with a nonzero candidate pool
1250// size, the pooled PortAllocatorSession is created with all the attributes
1251// in the RTCConfiguration.
1252TEST_F(PeerConnectionInterfaceTest, CreatePeerConnectionWithPooledCandidates) {
1253 PeerConnectionInterface::RTCConfiguration config;
1254 PeerConnectionInterface::IceServer server;
1255 server.uri = kStunAddressOnly;
1256 config.servers.push_back(server);
1257 config.type = PeerConnectionInterface::kRelay;
1258 config.disable_ipv6 = true;
1259 config.tcp_candidate_policy =
1260 PeerConnectionInterface::kTcpCandidatePolicyDisabled;
honghaiz60347052016-05-31 18:29:12 -07001261 config.candidate_network_policy =
1262 PeerConnectionInterface::kCandidateNetworkPolicyLowCost;
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001263 config.ice_candidate_pool_size = 1;
1264 CreatePeerConnection(config, nullptr);
1265
1266 const cricket::FakePortAllocatorSession* session =
1267 static_cast<const cricket::FakePortAllocatorSession*>(
1268 port_allocator_->GetPooledSession());
1269 ASSERT_NE(nullptr, session);
1270 EXPECT_EQ(1UL, session->stun_servers().size());
1271 EXPECT_EQ(0U, session->flags() & cricket::PORTALLOCATOR_ENABLE_IPV6);
1272 EXPECT_LT(0U, session->flags() & cricket::PORTALLOCATOR_DISABLE_TCP);
honghaiz60347052016-05-31 18:29:12 -07001273 EXPECT_LT(0U,
1274 session->flags() & cricket::PORTALLOCATOR_DISABLE_COSTLY_NETWORKS);
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001275}
1276
Taylor Brandstetterf8e65772016-06-27 17:20:15 -07001277// Test that the PeerConnection initializes the port allocator passed into it,
1278// and on the correct thread.
1279TEST_F(PeerConnectionInterfaceTest,
1280 CreatePeerConnectionInitializesPortAllocator) {
1281 rtc::Thread network_thread;
1282 network_thread.Start();
1283 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface> pc_factory(
1284 webrtc::CreatePeerConnectionFactory(
1285 &network_thread, rtc::Thread::Current(), rtc::Thread::Current(),
1286 nullptr, nullptr, nullptr));
1287 std::unique_ptr<cricket::FakePortAllocator> port_allocator(
1288 new cricket::FakePortAllocator(&network_thread, nullptr));
1289 cricket::FakePortAllocator* raw_port_allocator = port_allocator.get();
1290 PeerConnectionInterface::RTCConfiguration config;
1291 rtc::scoped_refptr<PeerConnectionInterface> pc(
1292 pc_factory->CreatePeerConnection(
1293 config, nullptr, std::move(port_allocator), nullptr, &observer_));
1294 // FakePortAllocator RTC_CHECKs that it's initialized on the right thread,
1295 // so all we have to do here is check that it's initialized.
1296 EXPECT_TRUE(raw_port_allocator->initialized());
1297}
1298
deadbeef46c73892016-11-16 19:42:04 -08001299// Check that GetConfiguration returns the configuration the PeerConnection was
1300// constructed with, before SetConfiguration is called.
1301TEST_F(PeerConnectionInterfaceTest, GetConfigurationAfterCreatePeerConnection) {
1302 PeerConnectionInterface::RTCConfiguration config;
1303 config.type = PeerConnectionInterface::kRelay;
1304 CreatePeerConnection(config, nullptr);
1305
1306 PeerConnectionInterface::RTCConfiguration returned_config =
1307 pc_->GetConfiguration();
1308 EXPECT_EQ(PeerConnectionInterface::kRelay, returned_config.type);
1309}
1310
1311// Check that GetConfiguration returns the last configuration passed into
1312// SetConfiguration.
1313TEST_F(PeerConnectionInterfaceTest, GetConfigurationAfterSetConfiguration) {
1314 CreatePeerConnection();
1315
1316 PeerConnectionInterface::RTCConfiguration config;
1317 config.type = PeerConnectionInterface::kRelay;
1318 EXPECT_TRUE(pc_->SetConfiguration(config));
1319
1320 PeerConnectionInterface::RTCConfiguration returned_config =
1321 pc_->GetConfiguration();
1322 EXPECT_EQ(PeerConnectionInterface::kRelay, returned_config.type);
1323}
1324
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001325TEST_F(PeerConnectionInterfaceTest, AddStreams) {
deadbeef293e9262017-01-11 12:28:30 -08001326 CreatePeerConnectionWithoutDtls();
deadbeefab9b2d12015-10-14 11:33:11 -07001327 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001328 AddVoiceStream(kStreamLabel2);
1329 ASSERT_EQ(2u, pc_->local_streams()->count());
1330
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001331 // Test we can add multiple local streams to one peerconnection.
zhihuang9763d562016-08-05 11:14:50 -07001332 rtc::scoped_refptr<MediaStreamInterface> stream(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001333 pc_factory_->CreateLocalMediaStream(kStreamLabel3));
zhihuang9763d562016-08-05 11:14:50 -07001334 rtc::scoped_refptr<AudioTrackInterface> audio_track(
1335 pc_factory_->CreateAudioTrack(kStreamLabel3,
1336 static_cast<AudioSourceInterface*>(NULL)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001337 stream->AddTrack(audio_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +00001338 EXPECT_TRUE(pc_->AddStream(stream));
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001339 EXPECT_EQ(3u, pc_->local_streams()->count());
1340
1341 // Remove the third stream.
1342 pc_->RemoveStream(pc_->local_streams()->at(2));
1343 EXPECT_EQ(2u, pc_->local_streams()->count());
1344
1345 // Remove the second stream.
1346 pc_->RemoveStream(pc_->local_streams()->at(1));
1347 EXPECT_EQ(1u, pc_->local_streams()->count());
1348
1349 // Remove the first stream.
1350 pc_->RemoveStream(pc_->local_streams()->at(0));
1351 EXPECT_EQ(0u, pc_->local_streams()->count());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001352}
1353
deadbeefab9b2d12015-10-14 11:33:11 -07001354// Test that the created offer includes streams we added.
1355TEST_F(PeerConnectionInterfaceTest, AddedStreamsPresentInOffer) {
deadbeef293e9262017-01-11 12:28:30 -08001356 CreatePeerConnectionWithoutDtls();
deadbeefab9b2d12015-10-14 11:33:11 -07001357 AddAudioVideoStream(kStreamLabel1, "audio_track", "video_track");
kwibergd1fe2812016-04-27 06:47:29 -07001358 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001359 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07001360
1361 const cricket::ContentInfo* audio_content =
1362 cricket::GetFirstAudioContent(offer->description());
1363 const cricket::AudioContentDescription* audio_desc =
1364 static_cast<const cricket::AudioContentDescription*>(
1365 audio_content->description);
1366 EXPECT_TRUE(
1367 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
1368
1369 const cricket::ContentInfo* video_content =
1370 cricket::GetFirstVideoContent(offer->description());
1371 const cricket::VideoContentDescription* video_desc =
1372 static_cast<const cricket::VideoContentDescription*>(
1373 video_content->description);
1374 EXPECT_TRUE(
1375 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1376
1377 // Add another stream and ensure the offer includes both the old and new
1378 // streams.
1379 AddAudioVideoStream(kStreamLabel2, "audio_track2", "video_track2");
kwiberg2bbff992016-03-16 11:03:04 -07001380 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07001381
1382 audio_content = cricket::GetFirstAudioContent(offer->description());
1383 audio_desc = static_cast<const cricket::AudioContentDescription*>(
1384 audio_content->description);
1385 EXPECT_TRUE(
1386 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
1387 EXPECT_TRUE(
1388 ContainsTrack(audio_desc->streams(), kStreamLabel2, "audio_track2"));
1389
1390 video_content = cricket::GetFirstVideoContent(offer->description());
1391 video_desc = static_cast<const cricket::VideoContentDescription*>(
1392 video_content->description);
1393 EXPECT_TRUE(
1394 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1395 EXPECT_TRUE(
1396 ContainsTrack(video_desc->streams(), kStreamLabel2, "video_track2"));
1397}
1398
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001399TEST_F(PeerConnectionInterfaceTest, RemoveStream) {
deadbeef293e9262017-01-11 12:28:30 -08001400 CreatePeerConnectionWithoutDtls();
deadbeefab9b2d12015-10-14 11:33:11 -07001401 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001402 ASSERT_EQ(1u, pc_->local_streams()->count());
1403 pc_->RemoveStream(pc_->local_streams()->at(0));
1404 EXPECT_EQ(0u, pc_->local_streams()->count());
1405}
1406
deadbeefe1f9d832016-01-14 15:35:42 -08001407// Test for AddTrack and RemoveTrack methods.
1408// Tests that the created offer includes tracks we added,
1409// and that the RtpSenders are created correctly.
1410// Also tests that RemoveTrack removes the tracks from subsequent offers.
1411TEST_F(PeerConnectionInterfaceTest, AddTrackRemoveTrack) {
deadbeef293e9262017-01-11 12:28:30 -08001412 CreatePeerConnectionWithoutDtls();
deadbeefe1f9d832016-01-14 15:35:42 -08001413 // Create a dummy stream, so tracks share a stream label.
zhihuang9763d562016-08-05 11:14:50 -07001414 rtc::scoped_refptr<MediaStreamInterface> stream(
deadbeefe1f9d832016-01-14 15:35:42 -08001415 pc_factory_->CreateLocalMediaStream(kStreamLabel1));
1416 std::vector<MediaStreamInterface*> stream_list;
1417 stream_list.push_back(stream.get());
zhihuang9763d562016-08-05 11:14:50 -07001418 rtc::scoped_refptr<AudioTrackInterface> audio_track(
deadbeefe1f9d832016-01-14 15:35:42 -08001419 pc_factory_->CreateAudioTrack("audio_track", nullptr));
zhihuang9763d562016-08-05 11:14:50 -07001420 rtc::scoped_refptr<VideoTrackInterface> video_track(
1421 pc_factory_->CreateVideoTrack(
deadbeef112b2e92017-02-10 20:13:37 -08001422 "video_track", pc_factory_->CreateVideoSource(
1423 std::unique_ptr<cricket::VideoCapturer>(
1424 new cricket::FakeVideoCapturer()))));
deadbeefe1f9d832016-01-14 15:35:42 -08001425 auto audio_sender = pc_->AddTrack(audio_track, stream_list);
1426 auto video_sender = pc_->AddTrack(video_track, stream_list);
deadbeefa601f5c2016-06-06 14:27:39 -07001427 EXPECT_EQ(1UL, audio_sender->stream_ids().size());
1428 EXPECT_EQ(kStreamLabel1, audio_sender->stream_ids()[0]);
deadbeefe1f9d832016-01-14 15:35:42 -08001429 EXPECT_EQ("audio_track", audio_sender->id());
1430 EXPECT_EQ(audio_track, audio_sender->track());
deadbeefa601f5c2016-06-06 14:27:39 -07001431 EXPECT_EQ(1UL, video_sender->stream_ids().size());
1432 EXPECT_EQ(kStreamLabel1, video_sender->stream_ids()[0]);
deadbeefe1f9d832016-01-14 15:35:42 -08001433 EXPECT_EQ("video_track", video_sender->id());
1434 EXPECT_EQ(video_track, video_sender->track());
1435
1436 // Now create an offer and check for the senders.
kwibergd1fe2812016-04-27 06:47:29 -07001437 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001438 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefe1f9d832016-01-14 15:35:42 -08001439
1440 const cricket::ContentInfo* audio_content =
1441 cricket::GetFirstAudioContent(offer->description());
1442 const cricket::AudioContentDescription* audio_desc =
1443 static_cast<const cricket::AudioContentDescription*>(
1444 audio_content->description);
1445 EXPECT_TRUE(
1446 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
1447
1448 const cricket::ContentInfo* video_content =
1449 cricket::GetFirstVideoContent(offer->description());
1450 const cricket::VideoContentDescription* video_desc =
1451 static_cast<const cricket::VideoContentDescription*>(
1452 video_content->description);
1453 EXPECT_TRUE(
1454 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1455
1456 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
1457
1458 // Now try removing the tracks.
1459 EXPECT_TRUE(pc_->RemoveTrack(audio_sender));
1460 EXPECT_TRUE(pc_->RemoveTrack(video_sender));
1461
1462 // Create a new offer and ensure it doesn't contain the removed senders.
kwiberg2bbff992016-03-16 11:03:04 -07001463 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefe1f9d832016-01-14 15:35:42 -08001464
1465 audio_content = cricket::GetFirstAudioContent(offer->description());
1466 audio_desc = static_cast<const cricket::AudioContentDescription*>(
1467 audio_content->description);
1468 EXPECT_FALSE(
1469 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
1470
1471 video_content = cricket::GetFirstVideoContent(offer->description());
1472 video_desc = static_cast<const cricket::VideoContentDescription*>(
1473 video_content->description);
1474 EXPECT_FALSE(
1475 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1476
1477 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
1478
1479 // Calling RemoveTrack on a sender no longer attached to a PeerConnection
1480 // should return false.
1481 EXPECT_FALSE(pc_->RemoveTrack(audio_sender));
1482 EXPECT_FALSE(pc_->RemoveTrack(video_sender));
1483}
1484
1485// Test creating senders without a stream specified,
1486// expecting a random stream ID to be generated.
1487TEST_F(PeerConnectionInterfaceTest, AddTrackWithoutStream) {
deadbeef293e9262017-01-11 12:28:30 -08001488 CreatePeerConnectionWithoutDtls();
deadbeefe1f9d832016-01-14 15:35:42 -08001489 // Create a dummy stream, so tracks share a stream label.
zhihuang9763d562016-08-05 11:14:50 -07001490 rtc::scoped_refptr<AudioTrackInterface> audio_track(
deadbeefe1f9d832016-01-14 15:35:42 -08001491 pc_factory_->CreateAudioTrack("audio_track", nullptr));
zhihuang9763d562016-08-05 11:14:50 -07001492 rtc::scoped_refptr<VideoTrackInterface> video_track(
1493 pc_factory_->CreateVideoTrack(
deadbeef112b2e92017-02-10 20:13:37 -08001494 "video_track", pc_factory_->CreateVideoSource(
1495 std::unique_ptr<cricket::VideoCapturer>(
1496 new cricket::FakeVideoCapturer()))));
deadbeefe1f9d832016-01-14 15:35:42 -08001497 auto audio_sender =
1498 pc_->AddTrack(audio_track, std::vector<MediaStreamInterface*>());
1499 auto video_sender =
1500 pc_->AddTrack(video_track, std::vector<MediaStreamInterface*>());
1501 EXPECT_EQ("audio_track", audio_sender->id());
1502 EXPECT_EQ(audio_track, audio_sender->track());
1503 EXPECT_EQ("video_track", video_sender->id());
1504 EXPECT_EQ(video_track, video_sender->track());
1505 // If the ID is truly a random GUID, it should be infinitely unlikely they
1506 // will be the same.
deadbeefa601f5c2016-06-06 14:27:39 -07001507 EXPECT_NE(video_sender->stream_ids(), audio_sender->stream_ids());
deadbeefe1f9d832016-01-14 15:35:42 -08001508}
1509
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001510TEST_F(PeerConnectionInterfaceTest, CreateOfferReceiveAnswer) {
1511 InitiateCall();
1512 WaitAndVerifyOnAddStream(kStreamLabel1);
1513 VerifyRemoteRtpHeaderExtensions();
1514}
1515
1516TEST_F(PeerConnectionInterfaceTest, CreateOfferReceivePrAnswerAndAnswer) {
deadbeef293e9262017-01-11 12:28:30 -08001517 CreatePeerConnectionWithoutDtls();
deadbeefab9b2d12015-10-14 11:33:11 -07001518 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001519 CreateOfferAsLocalDescription();
1520 std::string offer;
1521 EXPECT_TRUE(pc_->local_description()->ToString(&offer));
1522 CreatePrAnswerAndAnswerAsRemoteDescription(offer);
1523 WaitAndVerifyOnAddStream(kStreamLabel1);
1524}
1525
1526TEST_F(PeerConnectionInterfaceTest, ReceiveOfferCreateAnswer) {
deadbeef293e9262017-01-11 12:28:30 -08001527 CreatePeerConnectionWithoutDtls();
deadbeefab9b2d12015-10-14 11:33:11 -07001528 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001529
1530 CreateOfferAsRemoteDescription();
1531 CreateAnswerAsLocalDescription();
1532
1533 WaitAndVerifyOnAddStream(kStreamLabel1);
1534}
1535
1536TEST_F(PeerConnectionInterfaceTest, ReceiveOfferCreatePrAnswerAndAnswer) {
deadbeef293e9262017-01-11 12:28:30 -08001537 CreatePeerConnectionWithoutDtls();
deadbeefab9b2d12015-10-14 11:33:11 -07001538 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001539
1540 CreateOfferAsRemoteDescription();
1541 CreatePrAnswerAsLocalDescription();
1542 CreateAnswerAsLocalDescription();
1543
1544 WaitAndVerifyOnAddStream(kStreamLabel1);
1545}
1546
1547TEST_F(PeerConnectionInterfaceTest, Renegotiate) {
1548 InitiateCall();
1549 ASSERT_EQ(1u, pc_->remote_streams()->count());
1550 pc_->RemoveStream(pc_->local_streams()->at(0));
1551 CreateOfferReceiveAnswer();
1552 EXPECT_EQ(0u, pc_->remote_streams()->count());
deadbeefab9b2d12015-10-14 11:33:11 -07001553 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001554 CreateOfferReceiveAnswer();
1555}
1556
1557// Tests that after negotiating an audio only call, the respondent can perform a
1558// renegotiation that removes the audio stream.
1559TEST_F(PeerConnectionInterfaceTest, RenegotiateAudioOnly) {
deadbeef293e9262017-01-11 12:28:30 -08001560 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001561 AddVoiceStream(kStreamLabel1);
1562 CreateOfferAsRemoteDescription();
1563 CreateAnswerAsLocalDescription();
1564
1565 ASSERT_EQ(1u, pc_->remote_streams()->count());
1566 pc_->RemoveStream(pc_->local_streams()->at(0));
1567 CreateOfferReceiveAnswer();
1568 EXPECT_EQ(0u, pc_->remote_streams()->count());
1569}
1570
1571// Test that candidates are generated and that we can parse our own candidates.
1572TEST_F(PeerConnectionInterfaceTest, IceCandidates) {
deadbeef293e9262017-01-11 12:28:30 -08001573 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001574
1575 EXPECT_FALSE(pc_->AddIceCandidate(observer_.last_candidate_.get()));
1576 // SetRemoteDescription takes ownership of offer.
kwibergd1fe2812016-04-27 06:47:29 -07001577 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefab9b2d12015-10-14 11:33:11 -07001578 AddVideoStream(kStreamLabel1);
deadbeefc80741f2015-10-22 13:14:45 -07001579 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
kwiberg2bbff992016-03-16 11:03:04 -07001580 EXPECT_TRUE(DoSetRemoteDescription(offer.release()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001581
1582 // SetLocalDescription takes ownership of answer.
kwibergd1fe2812016-04-27 06:47:29 -07001583 std::unique_ptr<SessionDescriptionInterface> answer;
deadbeefc80741f2015-10-22 13:14:45 -07001584 EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
kwiberg2bbff992016-03-16 11:03:04 -07001585 EXPECT_TRUE(DoSetLocalDescription(answer.release()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001586
1587 EXPECT_TRUE_WAIT(observer_.last_candidate_.get() != NULL, kTimeout);
1588 EXPECT_TRUE_WAIT(observer_.ice_complete_, kTimeout);
1589
1590 EXPECT_TRUE(pc_->AddIceCandidate(observer_.last_candidate_.get()));
1591}
1592
deadbeefab9b2d12015-10-14 11:33:11 -07001593// Test that CreateOffer and CreateAnswer will fail if the track labels are
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001594// not unique.
1595TEST_F(PeerConnectionInterfaceTest, CreateOfferAnswerWithInvalidStream) {
deadbeef293e9262017-01-11 12:28:30 -08001596 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001597 // Create a regular offer for the CreateAnswer test later.
kwibergd1fe2812016-04-27 06:47:29 -07001598 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefc80741f2015-10-22 13:14:45 -07001599 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
kwiberg2bbff992016-03-16 11:03:04 -07001600 EXPECT_TRUE(offer);
1601 offer.reset();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001602
1603 // Create a local stream with audio&video tracks having same label.
1604 AddAudioVideoStream(kStreamLabel1, "track_label", "track_label");
1605
1606 // Test CreateOffer
deadbeefc80741f2015-10-22 13:14:45 -07001607 EXPECT_FALSE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001608
1609 // Test CreateAnswer
kwibergd1fe2812016-04-27 06:47:29 -07001610 std::unique_ptr<SessionDescriptionInterface> answer;
deadbeefc80741f2015-10-22 13:14:45 -07001611 EXPECT_FALSE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001612}
1613
1614// Test that we will get different SSRCs for each tracks in the offer and answer
1615// we created.
1616TEST_F(PeerConnectionInterfaceTest, SsrcInOfferAnswer) {
deadbeef293e9262017-01-11 12:28:30 -08001617 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001618 // Create a local stream with audio&video tracks having different labels.
1619 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
1620
1621 // Test CreateOffer
kwibergd1fe2812016-04-27 06:47:29 -07001622 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001623 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001624 int audio_ssrc = 0;
1625 int video_ssrc = 0;
1626 EXPECT_TRUE(GetFirstSsrc(GetFirstAudioContent(offer->description()),
1627 &audio_ssrc));
1628 EXPECT_TRUE(GetFirstSsrc(GetFirstVideoContent(offer->description()),
1629 &video_ssrc));
1630 EXPECT_NE(audio_ssrc, video_ssrc);
1631
1632 // Test CreateAnswer
1633 EXPECT_TRUE(DoSetRemoteDescription(offer.release()));
kwibergd1fe2812016-04-27 06:47:29 -07001634 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -07001635 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001636 audio_ssrc = 0;
1637 video_ssrc = 0;
1638 EXPECT_TRUE(GetFirstSsrc(GetFirstAudioContent(answer->description()),
1639 &audio_ssrc));
1640 EXPECT_TRUE(GetFirstSsrc(GetFirstVideoContent(answer->description()),
1641 &video_ssrc));
1642 EXPECT_NE(audio_ssrc, video_ssrc);
1643}
1644
deadbeefeb459812015-12-15 19:24:43 -08001645// Test that it's possible to call AddTrack on a MediaStream after adding
1646// the stream to a PeerConnection.
1647// TODO(deadbeef): Remove this test once this behavior is no longer supported.
1648TEST_F(PeerConnectionInterfaceTest, AddTrackAfterAddStream) {
deadbeef293e9262017-01-11 12:28:30 -08001649 CreatePeerConnectionWithoutDtls();
deadbeefeb459812015-12-15 19:24:43 -08001650 // Create audio stream and add to PeerConnection.
1651 AddVoiceStream(kStreamLabel1);
1652 MediaStreamInterface* stream = pc_->local_streams()->at(0);
1653
1654 // Add video track to the audio-only stream.
zhihuang9763d562016-08-05 11:14:50 -07001655 rtc::scoped_refptr<VideoTrackInterface> video_track(
1656 pc_factory_->CreateVideoTrack(
deadbeef112b2e92017-02-10 20:13:37 -08001657 "video_label", pc_factory_->CreateVideoSource(
1658 std::unique_ptr<cricket::VideoCapturer>(
1659 new cricket::FakeVideoCapturer()))));
deadbeefeb459812015-12-15 19:24:43 -08001660 stream->AddTrack(video_track.get());
1661
kwibergd1fe2812016-04-27 06:47:29 -07001662 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001663 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefeb459812015-12-15 19:24:43 -08001664
1665 const cricket::MediaContentDescription* video_desc =
1666 cricket::GetFirstVideoContentDescription(offer->description());
1667 EXPECT_TRUE(video_desc != nullptr);
1668}
1669
1670// Test that it's possible to call RemoveTrack on a MediaStream after adding
1671// the stream to a PeerConnection.
1672// TODO(deadbeef): Remove this test once this behavior is no longer supported.
1673TEST_F(PeerConnectionInterfaceTest, RemoveTrackAfterAddStream) {
deadbeef293e9262017-01-11 12:28:30 -08001674 CreatePeerConnectionWithoutDtls();
deadbeefeb459812015-12-15 19:24:43 -08001675 // Create audio/video stream and add to PeerConnection.
1676 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
1677 MediaStreamInterface* stream = pc_->local_streams()->at(0);
1678
1679 // Remove the video track.
1680 stream->RemoveTrack(stream->GetVideoTracks()[0]);
1681
kwibergd1fe2812016-04-27 06:47:29 -07001682 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001683 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefeb459812015-12-15 19:24:43 -08001684
1685 const cricket::MediaContentDescription* video_desc =
1686 cricket::GetFirstVideoContentDescription(offer->description());
1687 EXPECT_TRUE(video_desc == nullptr);
1688}
1689
deadbeef1dcb1642017-03-29 21:08:16 -07001690// Verify that CreateDtmfSender only succeeds if called with a valid local
1691// track. Other aspects of DtmfSenders are tested in
1692// peerconnection_integrationtest.cc.
1693TEST_F(PeerConnectionInterfaceTest, CreateDtmfSenderWithInvalidParams) {
1694 CreatePeerConnection();
1695 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
1696 EXPECT_EQ(nullptr, pc_->CreateDtmfSender(nullptr));
1697 rtc::scoped_refptr<webrtc::AudioTrackInterface> non_localtrack(
1698 pc_factory_->CreateAudioTrack("dummy_track", nullptr));
1699 EXPECT_EQ(nullptr, pc_->CreateDtmfSender(non_localtrack));
1700}
1701
deadbeefbd7d8f72015-12-18 16:58:44 -08001702// Test creating a sender with a stream ID, and ensure the ID is populated
1703// in the offer.
1704TEST_F(PeerConnectionInterfaceTest, CreateSenderWithStream) {
deadbeef293e9262017-01-11 12:28:30 -08001705 CreatePeerConnectionWithoutDtls();
deadbeefbd7d8f72015-12-18 16:58:44 -08001706 pc_->CreateSender("video", kStreamLabel1);
1707
kwibergd1fe2812016-04-27 06:47:29 -07001708 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001709 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefbd7d8f72015-12-18 16:58:44 -08001710
1711 const cricket::MediaContentDescription* video_desc =
1712 cricket::GetFirstVideoContentDescription(offer->description());
1713 ASSERT_TRUE(video_desc != nullptr);
1714 ASSERT_EQ(1u, video_desc->streams().size());
1715 EXPECT_EQ(kStreamLabel1, video_desc->streams()[0].sync_label);
1716}
1717
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001718// Test that we can specify a certain track that we want statistics about.
1719TEST_F(PeerConnectionInterfaceTest, GetStatsForSpecificTrack) {
1720 InitiateCall();
1721 ASSERT_LT(0u, pc_->remote_streams()->count());
1722 ASSERT_LT(0u, pc_->remote_streams()->at(0)->GetAudioTracks().size());
zhihuang9763d562016-08-05 11:14:50 -07001723 rtc::scoped_refptr<MediaStreamTrackInterface> remote_audio =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001724 pc_->remote_streams()->at(0)->GetAudioTracks()[0];
1725 EXPECT_TRUE(DoGetStats(remote_audio));
1726
1727 // Remove the stream. Since we are sending to our selves the local
1728 // and the remote stream is the same.
1729 pc_->RemoveStream(pc_->local_streams()->at(0));
1730 // Do a re-negotiation.
1731 CreateOfferReceiveAnswer();
1732
1733 ASSERT_EQ(0u, pc_->remote_streams()->count());
1734
1735 // Test that we still can get statistics for the old track. Even if it is not
1736 // sent any longer.
1737 EXPECT_TRUE(DoGetStats(remote_audio));
1738}
1739
1740// Test that we can get stats on a video track.
1741TEST_F(PeerConnectionInterfaceTest, GetStatsForVideoTrack) {
1742 InitiateCall();
1743 ASSERT_LT(0u, pc_->remote_streams()->count());
1744 ASSERT_LT(0u, pc_->remote_streams()->at(0)->GetVideoTracks().size());
zhihuang9763d562016-08-05 11:14:50 -07001745 rtc::scoped_refptr<MediaStreamTrackInterface> remote_video =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001746 pc_->remote_streams()->at(0)->GetVideoTracks()[0];
1747 EXPECT_TRUE(DoGetStats(remote_video));
1748}
1749
1750// Test that we don't get statistics for an invalid track.
zhihuange9e94c32016-11-04 11:38:15 -07001751TEST_F(PeerConnectionInterfaceTest, GetStatsForInvalidTrack) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001752 InitiateCall();
zhihuang9763d562016-08-05 11:14:50 -07001753 rtc::scoped_refptr<AudioTrackInterface> unknown_audio_track(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001754 pc_factory_->CreateAudioTrack("unknown track", NULL));
1755 EXPECT_FALSE(DoGetStats(unknown_audio_track));
1756}
1757
1758// This test setup two RTP data channels in loop back.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001759TEST_F(PeerConnectionInterfaceTest, TestDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001760 FakeConstraints constraints;
1761 constraints.SetAllowRtpDataChannels();
1762 CreatePeerConnection(&constraints);
zhihuang9763d562016-08-05 11:14:50 -07001763 rtc::scoped_refptr<DataChannelInterface> data1 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001764 pc_->CreateDataChannel("test1", NULL);
zhihuang9763d562016-08-05 11:14:50 -07001765 rtc::scoped_refptr<DataChannelInterface> data2 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001766 pc_->CreateDataChannel("test2", NULL);
1767 ASSERT_TRUE(data1 != NULL);
kwibergd1fe2812016-04-27 06:47:29 -07001768 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001769 new MockDataChannelObserver(data1));
kwibergd1fe2812016-04-27 06:47:29 -07001770 std::unique_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001771 new MockDataChannelObserver(data2));
1772
1773 EXPECT_EQ(DataChannelInterface::kConnecting, data1->state());
1774 EXPECT_EQ(DataChannelInterface::kConnecting, data2->state());
1775 std::string data_to_send1 = "testing testing";
1776 std::string data_to_send2 = "testing something else";
1777 EXPECT_FALSE(data1->Send(DataBuffer(data_to_send1)));
1778
1779 CreateOfferReceiveAnswer();
1780 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1781 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
1782
1783 EXPECT_EQ(DataChannelInterface::kOpen, data1->state());
1784 EXPECT_EQ(DataChannelInterface::kOpen, data2->state());
1785 EXPECT_TRUE(data1->Send(DataBuffer(data_to_send1)));
1786 EXPECT_TRUE(data2->Send(DataBuffer(data_to_send2)));
1787
1788 EXPECT_EQ_WAIT(data_to_send1, observer1->last_message(), kTimeout);
1789 EXPECT_EQ_WAIT(data_to_send2, observer2->last_message(), kTimeout);
1790
1791 data1->Close();
1792 EXPECT_EQ(DataChannelInterface::kClosing, data1->state());
1793 CreateOfferReceiveAnswer();
1794 EXPECT_FALSE(observer1->IsOpen());
1795 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
1796 EXPECT_TRUE(observer2->IsOpen());
1797
1798 data_to_send2 = "testing something else again";
1799 EXPECT_TRUE(data2->Send(DataBuffer(data_to_send2)));
1800
1801 EXPECT_EQ_WAIT(data_to_send2, observer2->last_message(), kTimeout);
1802}
1803
1804// This test verifies that sendnig binary data over RTP data channels should
1805// fail.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001806TEST_F(PeerConnectionInterfaceTest, TestSendBinaryOnRtpDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001807 FakeConstraints constraints;
1808 constraints.SetAllowRtpDataChannels();
1809 CreatePeerConnection(&constraints);
zhihuang9763d562016-08-05 11:14:50 -07001810 rtc::scoped_refptr<DataChannelInterface> data1 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001811 pc_->CreateDataChannel("test1", NULL);
zhihuang9763d562016-08-05 11:14:50 -07001812 rtc::scoped_refptr<DataChannelInterface> data2 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001813 pc_->CreateDataChannel("test2", NULL);
1814 ASSERT_TRUE(data1 != NULL);
kwibergd1fe2812016-04-27 06:47:29 -07001815 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001816 new MockDataChannelObserver(data1));
kwibergd1fe2812016-04-27 06:47:29 -07001817 std::unique_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001818 new MockDataChannelObserver(data2));
1819
1820 EXPECT_EQ(DataChannelInterface::kConnecting, data1->state());
1821 EXPECT_EQ(DataChannelInterface::kConnecting, data2->state());
1822
1823 CreateOfferReceiveAnswer();
1824 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1825 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
1826
1827 EXPECT_EQ(DataChannelInterface::kOpen, data1->state());
1828 EXPECT_EQ(DataChannelInterface::kOpen, data2->state());
1829
jbaucheec21bd2016-03-20 06:15:43 -07001830 rtc::CopyOnWriteBuffer buffer("test", 4);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001831 EXPECT_FALSE(data1->Send(DataBuffer(buffer, true)));
1832}
1833
1834// This test setup a RTP data channels in loop back and test that a channel is
1835// opened even if the remote end answer with a zero SSRC.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001836TEST_F(PeerConnectionInterfaceTest, TestSendOnlyDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001837 FakeConstraints constraints;
1838 constraints.SetAllowRtpDataChannels();
1839 CreatePeerConnection(&constraints);
zhihuang9763d562016-08-05 11:14:50 -07001840 rtc::scoped_refptr<DataChannelInterface> data1 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001841 pc_->CreateDataChannel("test1", NULL);
kwibergd1fe2812016-04-27 06:47:29 -07001842 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001843 new MockDataChannelObserver(data1));
1844
1845 CreateOfferReceiveAnswerWithoutSsrc();
1846
1847 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1848
1849 data1->Close();
1850 EXPECT_EQ(DataChannelInterface::kClosing, data1->state());
1851 CreateOfferReceiveAnswerWithoutSsrc();
1852 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
1853 EXPECT_FALSE(observer1->IsOpen());
1854}
1855
1856// This test that if a data channel is added in an answer a receive only channel
1857// channel is created.
1858TEST_F(PeerConnectionInterfaceTest, TestReceiveOnlyDataChannel) {
1859 FakeConstraints constraints;
1860 constraints.SetAllowRtpDataChannels();
1861 CreatePeerConnection(&constraints);
1862
1863 std::string offer_label = "offer_channel";
zhihuang9763d562016-08-05 11:14:50 -07001864 rtc::scoped_refptr<DataChannelInterface> offer_channel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001865 pc_->CreateDataChannel(offer_label, NULL);
1866
1867 CreateOfferAsLocalDescription();
1868
1869 // Replace the data channel label in the offer and apply it as an answer.
1870 std::string receive_label = "answer_channel";
1871 std::string sdp;
1872 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001873 rtc::replace_substrs(offer_label.c_str(), offer_label.length(),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001874 receive_label.c_str(), receive_label.length(),
1875 &sdp);
1876 CreateAnswerAsRemoteDescription(sdp);
1877
1878 // Verify that a new incoming data channel has been created and that
1879 // it is open but can't we written to.
1880 ASSERT_TRUE(observer_.last_datachannel_ != NULL);
1881 DataChannelInterface* received_channel = observer_.last_datachannel_;
1882 EXPECT_EQ(DataChannelInterface::kConnecting, received_channel->state());
1883 EXPECT_EQ(receive_label, received_channel->label());
1884 EXPECT_FALSE(received_channel->Send(DataBuffer("something")));
1885
1886 // Verify that the channel we initially offered has been rejected.
1887 EXPECT_EQ(DataChannelInterface::kClosed, offer_channel->state());
1888
1889 // Do another offer / answer exchange and verify that the data channel is
1890 // opened.
1891 CreateOfferReceiveAnswer();
1892 EXPECT_EQ_WAIT(DataChannelInterface::kOpen, received_channel->state(),
1893 kTimeout);
1894}
1895
1896// This test that no data channel is returned if a reliable channel is
1897// requested.
1898// TODO(perkj): Remove this test once reliable channels are implemented.
1899TEST_F(PeerConnectionInterfaceTest, CreateReliableRtpDataChannelShouldFail) {
1900 FakeConstraints constraints;
1901 constraints.SetAllowRtpDataChannels();
1902 CreatePeerConnection(&constraints);
1903
1904 std::string label = "test";
1905 webrtc::DataChannelInit config;
1906 config.reliable = true;
zhihuang9763d562016-08-05 11:14:50 -07001907 rtc::scoped_refptr<DataChannelInterface> channel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001908 pc_->CreateDataChannel(label, &config);
1909 EXPECT_TRUE(channel == NULL);
1910}
1911
deadbeefab9b2d12015-10-14 11:33:11 -07001912// Verifies that duplicated label is not allowed for RTP data channel.
1913TEST_F(PeerConnectionInterfaceTest, RtpDuplicatedLabelNotAllowed) {
1914 FakeConstraints constraints;
1915 constraints.SetAllowRtpDataChannels();
1916 CreatePeerConnection(&constraints);
1917
1918 std::string label = "test";
zhihuang9763d562016-08-05 11:14:50 -07001919 rtc::scoped_refptr<DataChannelInterface> channel =
deadbeefab9b2d12015-10-14 11:33:11 -07001920 pc_->CreateDataChannel(label, nullptr);
1921 EXPECT_NE(channel, nullptr);
1922
zhihuang9763d562016-08-05 11:14:50 -07001923 rtc::scoped_refptr<DataChannelInterface> dup_channel =
deadbeefab9b2d12015-10-14 11:33:11 -07001924 pc_->CreateDataChannel(label, nullptr);
1925 EXPECT_EQ(dup_channel, nullptr);
1926}
1927
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001928// This tests that a SCTP data channel is returned using different
1929// DataChannelInit configurations.
1930TEST_F(PeerConnectionInterfaceTest, CreateSctpDataChannel) {
1931 FakeConstraints constraints;
1932 constraints.SetAllowDtlsSctpDataChannels();
1933 CreatePeerConnection(&constraints);
1934
1935 webrtc::DataChannelInit config;
1936
zhihuang9763d562016-08-05 11:14:50 -07001937 rtc::scoped_refptr<DataChannelInterface> channel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001938 pc_->CreateDataChannel("1", &config);
1939 EXPECT_TRUE(channel != NULL);
1940 EXPECT_TRUE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001941 EXPECT_TRUE(observer_.renegotiation_needed_);
1942 observer_.renegotiation_needed_ = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001943
1944 config.ordered = false;
1945 channel = pc_->CreateDataChannel("2", &config);
1946 EXPECT_TRUE(channel != NULL);
1947 EXPECT_TRUE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001948 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001949
1950 config.ordered = true;
1951 config.maxRetransmits = 0;
1952 channel = pc_->CreateDataChannel("3", &config);
1953 EXPECT_TRUE(channel != NULL);
1954 EXPECT_FALSE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001955 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001956
1957 config.maxRetransmits = -1;
1958 config.maxRetransmitTime = 0;
1959 channel = pc_->CreateDataChannel("4", &config);
1960 EXPECT_TRUE(channel != NULL);
1961 EXPECT_FALSE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001962 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001963}
1964
1965// This tests that no data channel is returned if both maxRetransmits and
1966// maxRetransmitTime are set for SCTP data channels.
1967TEST_F(PeerConnectionInterfaceTest,
1968 CreateSctpDataChannelShouldFailForInvalidConfig) {
1969 FakeConstraints constraints;
1970 constraints.SetAllowDtlsSctpDataChannels();
1971 CreatePeerConnection(&constraints);
1972
1973 std::string label = "test";
1974 webrtc::DataChannelInit config;
1975 config.maxRetransmits = 0;
1976 config.maxRetransmitTime = 0;
1977
zhihuang9763d562016-08-05 11:14:50 -07001978 rtc::scoped_refptr<DataChannelInterface> channel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001979 pc_->CreateDataChannel(label, &config);
1980 EXPECT_TRUE(channel == NULL);
1981}
1982
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001983// The test verifies that creating a SCTP data channel with an id already in use
1984// or out of range should fail.
1985TEST_F(PeerConnectionInterfaceTest,
1986 CreateSctpDataChannelWithInvalidIdShouldFail) {
1987 FakeConstraints constraints;
1988 constraints.SetAllowDtlsSctpDataChannels();
1989 CreatePeerConnection(&constraints);
1990
1991 webrtc::DataChannelInit config;
zhihuang9763d562016-08-05 11:14:50 -07001992 rtc::scoped_refptr<DataChannelInterface> channel;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001993
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001994 config.id = 1;
1995 channel = pc_->CreateDataChannel("1", &config);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001996 EXPECT_TRUE(channel != NULL);
1997 EXPECT_EQ(1, channel->id());
1998
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001999 channel = pc_->CreateDataChannel("x", &config);
2000 EXPECT_TRUE(channel == NULL);
2001
2002 config.id = cricket::kMaxSctpSid;
2003 channel = pc_->CreateDataChannel("max", &config);
2004 EXPECT_TRUE(channel != NULL);
2005 EXPECT_EQ(config.id, channel->id());
2006
2007 config.id = cricket::kMaxSctpSid + 1;
2008 channel = pc_->CreateDataChannel("x", &config);
2009 EXPECT_TRUE(channel == NULL);
2010}
2011
deadbeefab9b2d12015-10-14 11:33:11 -07002012// Verifies that duplicated label is allowed for SCTP data channel.
2013TEST_F(PeerConnectionInterfaceTest, SctpDuplicatedLabelAllowed) {
2014 FakeConstraints constraints;
2015 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2016 true);
2017 CreatePeerConnection(&constraints);
2018
2019 std::string label = "test";
zhihuang9763d562016-08-05 11:14:50 -07002020 rtc::scoped_refptr<DataChannelInterface> channel =
deadbeefab9b2d12015-10-14 11:33:11 -07002021 pc_->CreateDataChannel(label, nullptr);
2022 EXPECT_NE(channel, nullptr);
2023
zhihuang9763d562016-08-05 11:14:50 -07002024 rtc::scoped_refptr<DataChannelInterface> dup_channel =
deadbeefab9b2d12015-10-14 11:33:11 -07002025 pc_->CreateDataChannel(label, nullptr);
2026 EXPECT_NE(dup_channel, nullptr);
2027}
2028
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00002029// This test verifies that OnRenegotiationNeeded is fired for every new RTP
2030// DataChannel.
2031TEST_F(PeerConnectionInterfaceTest, RenegotiationNeededForNewRtpDataChannel) {
2032 FakeConstraints constraints;
2033 constraints.SetAllowRtpDataChannels();
2034 CreatePeerConnection(&constraints);
2035
zhihuang9763d562016-08-05 11:14:50 -07002036 rtc::scoped_refptr<DataChannelInterface> dc1 =
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00002037 pc_->CreateDataChannel("test1", NULL);
2038 EXPECT_TRUE(observer_.renegotiation_needed_);
2039 observer_.renegotiation_needed_ = false;
2040
zhihuang9763d562016-08-05 11:14:50 -07002041 rtc::scoped_refptr<DataChannelInterface> dc2 =
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00002042 pc_->CreateDataChannel("test2", NULL);
2043 EXPECT_TRUE(observer_.renegotiation_needed_);
2044}
2045
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002046// This test that a data channel closes when a PeerConnection is deleted/closed.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002047TEST_F(PeerConnectionInterfaceTest, DataChannelCloseWhenPeerConnectionClose) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002048 FakeConstraints constraints;
2049 constraints.SetAllowRtpDataChannels();
2050 CreatePeerConnection(&constraints);
2051
zhihuang9763d562016-08-05 11:14:50 -07002052 rtc::scoped_refptr<DataChannelInterface> data1 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002053 pc_->CreateDataChannel("test1", NULL);
zhihuang9763d562016-08-05 11:14:50 -07002054 rtc::scoped_refptr<DataChannelInterface> data2 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002055 pc_->CreateDataChannel("test2", NULL);
2056 ASSERT_TRUE(data1 != NULL);
kwibergd1fe2812016-04-27 06:47:29 -07002057 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002058 new MockDataChannelObserver(data1));
kwibergd1fe2812016-04-27 06:47:29 -07002059 std::unique_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002060 new MockDataChannelObserver(data2));
2061
2062 CreateOfferReceiveAnswer();
2063 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
2064 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
2065
2066 ReleasePeerConnection();
2067 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
2068 EXPECT_EQ(DataChannelInterface::kClosed, data2->state());
2069}
2070
2071// This test that data channels can be rejected in an answer.
2072TEST_F(PeerConnectionInterfaceTest, TestRejectDataChannelInAnswer) {
2073 FakeConstraints constraints;
2074 constraints.SetAllowRtpDataChannels();
2075 CreatePeerConnection(&constraints);
2076
zhihuang9763d562016-08-05 11:14:50 -07002077 rtc::scoped_refptr<DataChannelInterface> offer_channel(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002078 pc_->CreateDataChannel("offer_channel", NULL));
2079
2080 CreateOfferAsLocalDescription();
2081
2082 // Create an answer where the m-line for data channels are rejected.
2083 std::string sdp;
2084 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
2085 webrtc::JsepSessionDescription* answer = new webrtc::JsepSessionDescription(
2086 SessionDescriptionInterface::kAnswer);
2087 EXPECT_TRUE(answer->Initialize(sdp, NULL));
2088 cricket::ContentInfo* data_info =
2089 answer->description()->GetContentByName("data");
2090 data_info->rejected = true;
2091
2092 DoSetRemoteDescription(answer);
2093 EXPECT_EQ(DataChannelInterface::kClosed, offer_channel->state());
2094}
2095
2096// Test that we can create a session description from an SDP string from
2097// FireFox, use it as a remote session description, generate an answer and use
2098// the answer as a local description.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002099TEST_F(PeerConnectionInterfaceTest, ReceiveFireFoxOffer) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002100 FakeConstraints constraints;
2101 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2102 true);
2103 CreatePeerConnection(&constraints);
2104 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
2105 SessionDescriptionInterface* desc =
2106 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
jbauchfabe2c92015-07-16 13:43:14 -07002107 webrtc::kFireFoxSdpOffer, nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002108 EXPECT_TRUE(DoSetSessionDescription(desc, false));
2109 CreateAnswerAsLocalDescription();
2110 ASSERT_TRUE(pc_->local_description() != NULL);
2111 ASSERT_TRUE(pc_->remote_description() != NULL);
2112
2113 const cricket::ContentInfo* content =
2114 cricket::GetFirstAudioContent(pc_->local_description()->description());
2115 ASSERT_TRUE(content != NULL);
2116 EXPECT_FALSE(content->rejected);
2117
2118 content =
2119 cricket::GetFirstVideoContent(pc_->local_description()->description());
2120 ASSERT_TRUE(content != NULL);
2121 EXPECT_FALSE(content->rejected);
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00002122#ifdef HAVE_SCTP
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002123 content =
2124 cricket::GetFirstDataContent(pc_->local_description()->description());
2125 ASSERT_TRUE(content != NULL);
2126 EXPECT_TRUE(content->rejected);
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00002127#endif
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002128}
2129
deadbeef8662f942017-01-20 21:20:51 -08002130// Test that an offer can be received which offers DTLS with SDES fallback.
2131// Regression test for issue:
2132// https://bugs.chromium.org/p/webrtc/issues/detail?id=6972
2133TEST_F(PeerConnectionInterfaceTest, ReceiveDtlsSdesFallbackOffer) {
2134 FakeConstraints constraints;
2135 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2136 true);
2137 CreatePeerConnection(&constraints);
2138 // Wait for fake certificate to be generated. Previously, this is what caused
2139 // the "a=crypto" lines to be rejected.
2140 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
2141 ASSERT_NE(nullptr, fake_certificate_generator_);
2142 EXPECT_EQ_WAIT(1, fake_certificate_generator_->generated_certificates(),
2143 kTimeout);
2144 SessionDescriptionInterface* desc = webrtc::CreateSessionDescription(
2145 SessionDescriptionInterface::kOffer, kDtlsSdesFallbackSdp, nullptr);
2146 EXPECT_TRUE(DoSetSessionDescription(desc, false));
2147 CreateAnswerAsLocalDescription();
2148}
2149
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002150// Test that we can create an audio only offer and receive an answer with a
2151// limited set of audio codecs and receive an updated offer with more audio
2152// codecs, where the added codecs are not supported.
2153TEST_F(PeerConnectionInterfaceTest, ReceiveUpdatedAudioOfferWithBadCodecs) {
deadbeef293e9262017-01-11 12:28:30 -08002154 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002155 AddVoiceStream("audio_label");
2156 CreateOfferAsLocalDescription();
2157
2158 SessionDescriptionInterface* answer =
2159 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
jbauchfabe2c92015-07-16 13:43:14 -07002160 webrtc::kAudioSdp, nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002161 EXPECT_TRUE(DoSetSessionDescription(answer, false));
2162
2163 SessionDescriptionInterface* updated_offer =
2164 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
jbauchfabe2c92015-07-16 13:43:14 -07002165 webrtc::kAudioSdpWithUnsupportedCodecs,
2166 nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002167 EXPECT_TRUE(DoSetSessionDescription(updated_offer, false));
2168 CreateAnswerAsLocalDescription();
2169}
2170
deadbeefc80741f2015-10-22 13:14:45 -07002171// Test that if we're receiving (but not sending) a track, subsequent offers
2172// will have m-lines with a=recvonly.
2173TEST_F(PeerConnectionInterfaceTest, CreateSubsequentRecvOnlyOffer) {
2174 FakeConstraints constraints;
2175 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2176 true);
2177 CreatePeerConnection(&constraints);
2178 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2179 CreateAnswerAsLocalDescription();
2180
2181 // At this point we should be receiving stream 1, but not sending anything.
2182 // A new offer should be recvonly.
kwibergd1fe2812016-04-27 06:47:29 -07002183 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefc80741f2015-10-22 13:14:45 -07002184 DoCreateOffer(&offer, nullptr);
2185
2186 const cricket::ContentInfo* video_content =
2187 cricket::GetFirstVideoContent(offer->description());
2188 const cricket::VideoContentDescription* video_desc =
2189 static_cast<const cricket::VideoContentDescription*>(
2190 video_content->description);
2191 ASSERT_EQ(cricket::MD_RECVONLY, video_desc->direction());
2192
2193 const cricket::ContentInfo* audio_content =
2194 cricket::GetFirstAudioContent(offer->description());
2195 const cricket::AudioContentDescription* audio_desc =
2196 static_cast<const cricket::AudioContentDescription*>(
2197 audio_content->description);
2198 ASSERT_EQ(cricket::MD_RECVONLY, audio_desc->direction());
2199}
2200
2201// Test that if we're receiving (but not sending) a track, and the
2202// offerToReceiveVideo/offerToReceiveAudio constraints are explicitly set to
2203// false, the generated m-lines will be a=inactive.
2204TEST_F(PeerConnectionInterfaceTest, CreateSubsequentInactiveOffer) {
2205 FakeConstraints constraints;
2206 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2207 true);
2208 CreatePeerConnection(&constraints);
2209 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2210 CreateAnswerAsLocalDescription();
2211
2212 // At this point we should be receiving stream 1, but not sending anything.
2213 // A new offer would be recvonly, but we'll set the "no receive" constraints
2214 // to make it inactive.
kwibergd1fe2812016-04-27 06:47:29 -07002215 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefc80741f2015-10-22 13:14:45 -07002216 FakeConstraints offer_constraints;
2217 offer_constraints.AddMandatory(
2218 webrtc::MediaConstraintsInterface::kOfferToReceiveVideo, false);
2219 offer_constraints.AddMandatory(
2220 webrtc::MediaConstraintsInterface::kOfferToReceiveAudio, false);
2221 DoCreateOffer(&offer, &offer_constraints);
2222
2223 const cricket::ContentInfo* video_content =
2224 cricket::GetFirstVideoContent(offer->description());
2225 const cricket::VideoContentDescription* video_desc =
2226 static_cast<const cricket::VideoContentDescription*>(
2227 video_content->description);
2228 ASSERT_EQ(cricket::MD_INACTIVE, video_desc->direction());
2229
2230 const cricket::ContentInfo* audio_content =
2231 cricket::GetFirstAudioContent(offer->description());
2232 const cricket::AudioContentDescription* audio_desc =
2233 static_cast<const cricket::AudioContentDescription*>(
2234 audio_content->description);
2235 ASSERT_EQ(cricket::MD_INACTIVE, audio_desc->direction());
2236}
2237
deadbeef653b8e02015-11-11 12:55:10 -08002238// Test that we can use SetConfiguration to change the ICE servers of the
2239// PortAllocator.
2240TEST_F(PeerConnectionInterfaceTest, SetConfigurationChangesIceServers) {
2241 CreatePeerConnection();
2242
2243 PeerConnectionInterface::RTCConfiguration config;
2244 PeerConnectionInterface::IceServer server;
2245 server.uri = "stun:test_hostname";
2246 config.servers.push_back(server);
2247 EXPECT_TRUE(pc_->SetConfiguration(config));
2248
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002249 EXPECT_EQ(1u, port_allocator_->stun_servers().size());
2250 EXPECT_EQ("test_hostname",
2251 port_allocator_->stun_servers().begin()->hostname());
deadbeef653b8e02015-11-11 12:55:10 -08002252}
2253
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002254TEST_F(PeerConnectionInterfaceTest, SetConfigurationChangesCandidateFilter) {
2255 CreatePeerConnection();
2256 PeerConnectionInterface::RTCConfiguration config;
2257 config.type = PeerConnectionInterface::kRelay;
2258 EXPECT_TRUE(pc_->SetConfiguration(config));
2259 EXPECT_EQ(cricket::CF_RELAY, port_allocator_->candidate_filter());
2260}
2261
deadbeef293e9262017-01-11 12:28:30 -08002262TEST_F(PeerConnectionInterfaceTest, SetConfigurationChangesPruneTurnPortsFlag) {
2263 PeerConnectionInterface::RTCConfiguration config;
2264 config.prune_turn_ports = false;
2265 CreatePeerConnection(config, nullptr);
2266 EXPECT_FALSE(port_allocator_->prune_turn_ports());
2267
2268 config.prune_turn_ports = true;
2269 EXPECT_TRUE(pc_->SetConfiguration(config));
2270 EXPECT_TRUE(port_allocator_->prune_turn_ports());
2271}
2272
skvladd1f5fda2017-02-03 16:54:05 -08002273// Test that the ice check interval can be changed. This does not verify that
2274// the setting makes it all the way to P2PTransportChannel, as that would
2275// require a very complex set of mocks.
2276TEST_F(PeerConnectionInterfaceTest, SetConfigurationChangesIceCheckInterval) {
2277 PeerConnectionInterface::RTCConfiguration config;
2278 config.ice_check_min_interval = rtc::Optional<int>();
2279 CreatePeerConnection(config, nullptr);
2280 config.ice_check_min_interval = rtc::Optional<int>(100);
2281 EXPECT_TRUE(pc_->SetConfiguration(config));
2282 PeerConnectionInterface::RTCConfiguration new_config =
2283 pc_->GetConfiguration();
2284 EXPECT_EQ(new_config.ice_check_min_interval, rtc::Optional<int>(100));
2285}
2286
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002287// Test that when SetConfiguration changes both the pool size and other
2288// attributes, the pooled session is created with the updated attributes.
2289TEST_F(PeerConnectionInterfaceTest,
2290 SetConfigurationCreatesPooledSessionCorrectly) {
2291 CreatePeerConnection();
2292 PeerConnectionInterface::RTCConfiguration config;
2293 config.ice_candidate_pool_size = 1;
2294 PeerConnectionInterface::IceServer server;
2295 server.uri = kStunAddressOnly;
2296 config.servers.push_back(server);
2297 config.type = PeerConnectionInterface::kRelay;
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07002298 EXPECT_TRUE(pc_->SetConfiguration(config));
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002299
2300 const cricket::FakePortAllocatorSession* session =
2301 static_cast<const cricket::FakePortAllocatorSession*>(
2302 port_allocator_->GetPooledSession());
2303 ASSERT_NE(nullptr, session);
2304 EXPECT_EQ(1UL, session->stun_servers().size());
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002305}
2306
deadbeef293e9262017-01-11 12:28:30 -08002307// Test that after SetLocalDescription, changing the pool size is not allowed,
2308// and an invalid modification error is returned.
deadbeef6de92f92016-12-12 18:49:32 -08002309TEST_F(PeerConnectionInterfaceTest,
2310 CantChangePoolSizeAfterSetLocalDescription) {
2311 CreatePeerConnection();
2312 // Start by setting a size of 1.
2313 PeerConnectionInterface::RTCConfiguration config;
2314 config.ice_candidate_pool_size = 1;
2315 EXPECT_TRUE(pc_->SetConfiguration(config));
2316
2317 // Set remote offer; can still change pool size at this point.
2318 CreateOfferAsRemoteDescription();
2319 config.ice_candidate_pool_size = 2;
2320 EXPECT_TRUE(pc_->SetConfiguration(config));
2321
2322 // Set local answer; now it's too late.
2323 CreateAnswerAsLocalDescription();
2324 config.ice_candidate_pool_size = 3;
deadbeef293e9262017-01-11 12:28:30 -08002325 RTCError error;
2326 EXPECT_FALSE(pc_->SetConfiguration(config, &error));
2327 EXPECT_EQ(RTCErrorType::INVALID_MODIFICATION, error.type());
2328}
2329
deadbeef42a42632017-03-10 15:18:00 -08002330// Test that after setting an answer, extra pooled sessions are discarded. The
2331// ICE candidate pool is only intended to be used for the first offer/answer.
2332TEST_F(PeerConnectionInterfaceTest,
2333 ExtraPooledSessionsDiscardedAfterApplyingAnswer) {
2334 CreatePeerConnection();
2335
2336 // Set a larger-than-necessary size.
2337 PeerConnectionInterface::RTCConfiguration config;
2338 config.ice_candidate_pool_size = 4;
2339 EXPECT_TRUE(pc_->SetConfiguration(config));
2340
2341 // Do offer/answer.
2342 CreateOfferAsRemoteDescription();
2343 CreateAnswerAsLocalDescription();
2344
2345 // Expect no pooled sessions to be left.
2346 const cricket::PortAllocatorSession* session =
2347 port_allocator_->GetPooledSession();
2348 EXPECT_EQ(nullptr, session);
2349}
2350
2351// After Close is called, pooled candidates should be discarded so as to not
2352// waste network resources.
2353TEST_F(PeerConnectionInterfaceTest, PooledSessionsDiscardedAfterClose) {
2354 CreatePeerConnection();
2355
2356 PeerConnectionInterface::RTCConfiguration config;
2357 config.ice_candidate_pool_size = 3;
2358 EXPECT_TRUE(pc_->SetConfiguration(config));
2359 pc_->Close();
2360
2361 // Expect no pooled sessions to be left.
2362 const cricket::PortAllocatorSession* session =
2363 port_allocator_->GetPooledSession();
2364 EXPECT_EQ(nullptr, session);
2365}
2366
deadbeef293e9262017-01-11 12:28:30 -08002367// Test that SetConfiguration returns an invalid modification error if
2368// modifying a field in the configuration that isn't allowed to be modified.
2369TEST_F(PeerConnectionInterfaceTest,
2370 SetConfigurationReturnsInvalidModificationError) {
2371 PeerConnectionInterface::RTCConfiguration config;
2372 config.bundle_policy = PeerConnectionInterface::kBundlePolicyBalanced;
2373 config.rtcp_mux_policy = PeerConnectionInterface::kRtcpMuxPolicyNegotiate;
2374 config.continual_gathering_policy = PeerConnectionInterface::GATHER_ONCE;
2375 CreatePeerConnection(config, nullptr);
2376
2377 PeerConnectionInterface::RTCConfiguration modified_config = config;
2378 modified_config.bundle_policy =
2379 PeerConnectionInterface::kBundlePolicyMaxBundle;
2380 RTCError error;
2381 EXPECT_FALSE(pc_->SetConfiguration(modified_config, &error));
2382 EXPECT_EQ(RTCErrorType::INVALID_MODIFICATION, error.type());
2383
2384 modified_config = config;
2385 modified_config.rtcp_mux_policy =
2386 PeerConnectionInterface::kRtcpMuxPolicyRequire;
2387 error.set_type(RTCErrorType::NONE);
2388 EXPECT_FALSE(pc_->SetConfiguration(modified_config, &error));
2389 EXPECT_EQ(RTCErrorType::INVALID_MODIFICATION, error.type());
2390
2391 modified_config = config;
2392 modified_config.continual_gathering_policy =
2393 PeerConnectionInterface::GATHER_CONTINUALLY;
2394 error.set_type(RTCErrorType::NONE);
2395 EXPECT_FALSE(pc_->SetConfiguration(modified_config, &error));
2396 EXPECT_EQ(RTCErrorType::INVALID_MODIFICATION, error.type());
2397}
2398
2399// Test that SetConfiguration returns a range error if the candidate pool size
2400// is negative or larger than allowed by the spec.
2401TEST_F(PeerConnectionInterfaceTest,
2402 SetConfigurationReturnsRangeErrorForBadCandidatePoolSize) {
2403 PeerConnectionInterface::RTCConfiguration config;
2404 CreatePeerConnection(config, nullptr);
2405
2406 config.ice_candidate_pool_size = -1;
2407 RTCError error;
2408 EXPECT_FALSE(pc_->SetConfiguration(config, &error));
2409 EXPECT_EQ(RTCErrorType::INVALID_RANGE, error.type());
2410
2411 config.ice_candidate_pool_size = INT_MAX;
2412 error.set_type(RTCErrorType::NONE);
2413 EXPECT_FALSE(pc_->SetConfiguration(config, &error));
2414 EXPECT_EQ(RTCErrorType::INVALID_RANGE, error.type());
2415}
2416
2417// Test that SetConfiguration returns a syntax error if parsing an ICE server
2418// URL failed.
2419TEST_F(PeerConnectionInterfaceTest,
2420 SetConfigurationReturnsSyntaxErrorFromBadIceUrls) {
2421 PeerConnectionInterface::RTCConfiguration config;
2422 CreatePeerConnection(config, nullptr);
2423
2424 PeerConnectionInterface::IceServer bad_server;
2425 bad_server.uri = "stunn:www.example.com";
2426 config.servers.push_back(bad_server);
2427 RTCError error;
2428 EXPECT_FALSE(pc_->SetConfiguration(config, &error));
2429 EXPECT_EQ(RTCErrorType::SYNTAX_ERROR, error.type());
2430}
2431
2432// Test that SetConfiguration returns an invalid parameter error if a TURN
2433// IceServer is missing a username or password.
2434TEST_F(PeerConnectionInterfaceTest,
2435 SetConfigurationReturnsInvalidParameterIfCredentialsMissing) {
2436 PeerConnectionInterface::RTCConfiguration config;
2437 CreatePeerConnection(config, nullptr);
2438
2439 PeerConnectionInterface::IceServer bad_server;
2440 bad_server.uri = "turn:www.example.com";
2441 // Missing password.
2442 bad_server.username = "foo";
2443 config.servers.push_back(bad_server);
2444 RTCError error;
2445 EXPECT_FALSE(pc_->SetConfiguration(config, &error));
2446 EXPECT_EQ(RTCErrorType::INVALID_PARAMETER, error.type());
deadbeef6de92f92016-12-12 18:49:32 -08002447}
2448
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002449// Test that PeerConnection::Close changes the states to closed and all remote
2450// tracks change state to ended.
2451TEST_F(PeerConnectionInterfaceTest, CloseAndTestStreamsAndStates) {
2452 // Initialize a PeerConnection and negotiate local and remote session
2453 // description.
2454 InitiateCall();
2455 ASSERT_EQ(1u, pc_->local_streams()->count());
2456 ASSERT_EQ(1u, pc_->remote_streams()->count());
2457
2458 pc_->Close();
2459
2460 EXPECT_EQ(PeerConnectionInterface::kClosed, pc_->signaling_state());
2461 EXPECT_EQ(PeerConnectionInterface::kIceConnectionClosed,
2462 pc_->ice_connection_state());
2463 EXPECT_EQ(PeerConnectionInterface::kIceGatheringComplete,
2464 pc_->ice_gathering_state());
2465
2466 EXPECT_EQ(1u, pc_->local_streams()->count());
2467 EXPECT_EQ(1u, pc_->remote_streams()->count());
2468
zhihuang9763d562016-08-05 11:14:50 -07002469 rtc::scoped_refptr<MediaStreamInterface> remote_stream =
2470 pc_->remote_streams()->at(0);
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002471 // Track state may be updated asynchronously.
perkjd61bf802016-03-24 03:16:19 -07002472 EXPECT_EQ_WAIT(MediaStreamTrackInterface::kEnded,
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002473 remote_stream->GetAudioTracks()[0]->state(), kTimeout);
2474 EXPECT_EQ_WAIT(MediaStreamTrackInterface::kEnded,
2475 remote_stream->GetVideoTracks()[0]->state(), kTimeout);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002476}
2477
2478// Test that PeerConnection methods fails gracefully after
2479// PeerConnection::Close has been called.
2480TEST_F(PeerConnectionInterfaceTest, CloseAndTestMethods) {
deadbeef293e9262017-01-11 12:28:30 -08002481 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002482 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
2483 CreateOfferAsRemoteDescription();
2484 CreateAnswerAsLocalDescription();
2485
2486 ASSERT_EQ(1u, pc_->local_streams()->count());
zhihuang9763d562016-08-05 11:14:50 -07002487 rtc::scoped_refptr<MediaStreamInterface> local_stream =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002488 pc_->local_streams()->at(0);
2489
2490 pc_->Close();
2491
2492 pc_->RemoveStream(local_stream);
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +00002493 EXPECT_FALSE(pc_->AddStream(local_stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002494
2495 ASSERT_FALSE(local_stream->GetAudioTracks().empty());
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002496 rtc::scoped_refptr<webrtc::DtmfSenderInterface> dtmf_sender(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002497 pc_->CreateDtmfSender(local_stream->GetAudioTracks()[0]));
wu@webrtc.org66037362013-08-13 00:09:35 +00002498 EXPECT_TRUE(NULL == dtmf_sender); // local stream has been removed.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002499
2500 EXPECT_TRUE(pc_->CreateDataChannel("test", NULL) == NULL);
2501
2502 EXPECT_TRUE(pc_->local_description() != NULL);
2503 EXPECT_TRUE(pc_->remote_description() != NULL);
2504
kwibergd1fe2812016-04-27 06:47:29 -07002505 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07002506 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
kwibergd1fe2812016-04-27 06:47:29 -07002507 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -07002508 EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002509
2510 std::string sdp;
2511 ASSERT_TRUE(pc_->remote_description()->ToString(&sdp));
2512 SessionDescriptionInterface* remote_offer =
2513 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
2514 sdp, NULL);
2515 EXPECT_FALSE(DoSetRemoteDescription(remote_offer));
2516
2517 ASSERT_TRUE(pc_->local_description()->ToString(&sdp));
2518 SessionDescriptionInterface* local_offer =
2519 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
2520 sdp, NULL);
2521 EXPECT_FALSE(DoSetLocalDescription(local_offer));
2522}
2523
2524// Test that GetStats can still be called after PeerConnection::Close.
2525TEST_F(PeerConnectionInterfaceTest, CloseAndGetStats) {
2526 InitiateCall();
2527 pc_->Close();
2528 DoGetStats(NULL);
2529}
deadbeefab9b2d12015-10-14 11:33:11 -07002530
2531// NOTE: The series of tests below come from what used to be
2532// mediastreamsignaling_unittest.cc, and are mostly aimed at testing that
2533// setting a remote or local description has the expected effects.
2534
2535// This test verifies that the remote MediaStreams corresponding to a received
2536// SDP string is created. In this test the two separate MediaStreams are
2537// signaled.
2538TEST_F(PeerConnectionInterfaceTest, UpdateRemoteStreams) {
2539 FakeConstraints constraints;
2540 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2541 true);
2542 CreatePeerConnection(&constraints);
2543 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2544
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002545 rtc::scoped_refptr<StreamCollection> reference(CreateStreamCollection(1, 1));
deadbeefab9b2d12015-10-14 11:33:11 -07002546 EXPECT_TRUE(
2547 CompareStreamCollections(observer_.remote_streams(), reference.get()));
2548 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2549 EXPECT_TRUE(remote_stream->GetVideoTracks()[0]->GetSource() != nullptr);
2550
2551 // Create a session description based on another SDP with another
2552 // MediaStream.
2553 CreateAndSetRemoteOffer(kSdpStringWithStream1And2);
2554
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002555 rtc::scoped_refptr<StreamCollection> reference2(CreateStreamCollection(2, 1));
deadbeefab9b2d12015-10-14 11:33:11 -07002556 EXPECT_TRUE(
2557 CompareStreamCollections(observer_.remote_streams(), reference2.get()));
2558}
2559
2560// This test verifies that when remote tracks are added/removed from SDP, the
2561// created remote streams are updated appropriately.
2562TEST_F(PeerConnectionInterfaceTest,
2563 AddRemoveTrackFromExistingRemoteMediaStream) {
2564 FakeConstraints constraints;
2565 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2566 true);
2567 CreatePeerConnection(&constraints);
kwibergd1fe2812016-04-27 06:47:29 -07002568 std::unique_ptr<SessionDescriptionInterface> desc_ms1 =
kwiberg2bbff992016-03-16 11:03:04 -07002569 CreateSessionDescriptionAndReference(1, 1);
deadbeefab9b2d12015-10-14 11:33:11 -07002570 EXPECT_TRUE(DoSetRemoteDescription(desc_ms1.release()));
2571 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
2572 reference_collection_));
2573
2574 // Add extra audio and video tracks to the same MediaStream.
kwibergd1fe2812016-04-27 06:47:29 -07002575 std::unique_ptr<SessionDescriptionInterface> desc_ms1_two_tracks =
kwiberg2bbff992016-03-16 11:03:04 -07002576 CreateSessionDescriptionAndReference(2, 2);
deadbeefab9b2d12015-10-14 11:33:11 -07002577 EXPECT_TRUE(DoSetRemoteDescription(desc_ms1_two_tracks.release()));
2578 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
2579 reference_collection_));
zhihuang9763d562016-08-05 11:14:50 -07002580 rtc::scoped_refptr<AudioTrackInterface> audio_track2 =
perkjd61bf802016-03-24 03:16:19 -07002581 observer_.remote_streams()->at(0)->GetAudioTracks()[1];
2582 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, audio_track2->state());
zhihuang9763d562016-08-05 11:14:50 -07002583 rtc::scoped_refptr<VideoTrackInterface> video_track2 =
perkjd61bf802016-03-24 03:16:19 -07002584 observer_.remote_streams()->at(0)->GetVideoTracks()[1];
2585 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, video_track2->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002586
2587 // Remove the extra audio and video tracks.
kwibergd1fe2812016-04-27 06:47:29 -07002588 std::unique_ptr<SessionDescriptionInterface> desc_ms2 =
kwiberg2bbff992016-03-16 11:03:04 -07002589 CreateSessionDescriptionAndReference(1, 1);
perkjd61bf802016-03-24 03:16:19 -07002590 MockTrackObserver audio_track_observer(audio_track2);
2591 MockTrackObserver video_track_observer(video_track2);
2592
2593 EXPECT_CALL(audio_track_observer, OnChanged()).Times(Exactly(1));
2594 EXPECT_CALL(video_track_observer, OnChanged()).Times(Exactly(1));
deadbeefab9b2d12015-10-14 11:33:11 -07002595 EXPECT_TRUE(DoSetRemoteDescription(desc_ms2.release()));
2596 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
2597 reference_collection_));
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002598 // Track state may be updated asynchronously.
perkjd61bf802016-03-24 03:16:19 -07002599 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002600 audio_track2->state(), kTimeout);
2601 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
2602 video_track2->state(), kTimeout);
deadbeefab9b2d12015-10-14 11:33:11 -07002603}
2604
2605// This tests that remote tracks are ended if a local session description is set
2606// that rejects the media content type.
2607TEST_F(PeerConnectionInterfaceTest, RejectMediaContent) {
2608 FakeConstraints constraints;
2609 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2610 true);
2611 CreatePeerConnection(&constraints);
2612 // First create and set a remote offer, then reject its video content in our
2613 // answer.
2614 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2615 ASSERT_EQ(1u, observer_.remote_streams()->count());
2616 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2617 ASSERT_EQ(1u, remote_stream->GetVideoTracks().size());
2618 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2619
2620 rtc::scoped_refptr<webrtc::VideoTrackInterface> remote_video =
2621 remote_stream->GetVideoTracks()[0];
2622 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, remote_video->state());
2623 rtc::scoped_refptr<webrtc::AudioTrackInterface> remote_audio =
2624 remote_stream->GetAudioTracks()[0];
2625 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, remote_audio->state());
2626
kwibergd1fe2812016-04-27 06:47:29 -07002627 std::unique_ptr<SessionDescriptionInterface> local_answer;
kwiberg2bbff992016-03-16 11:03:04 -07002628 EXPECT_TRUE(DoCreateAnswer(&local_answer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07002629 cricket::ContentInfo* video_info =
2630 local_answer->description()->GetContentByName("video");
2631 video_info->rejected = true;
2632 EXPECT_TRUE(DoSetLocalDescription(local_answer.release()));
2633 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kEnded, remote_video->state());
2634 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, remote_audio->state());
2635
2636 // Now create an offer where we reject both video and audio.
kwibergd1fe2812016-04-27 06:47:29 -07002637 std::unique_ptr<SessionDescriptionInterface> local_offer;
kwiberg2bbff992016-03-16 11:03:04 -07002638 EXPECT_TRUE(DoCreateOffer(&local_offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07002639 video_info = local_offer->description()->GetContentByName("video");
2640 ASSERT_TRUE(video_info != nullptr);
2641 video_info->rejected = true;
2642 cricket::ContentInfo* audio_info =
2643 local_offer->description()->GetContentByName("audio");
2644 ASSERT_TRUE(audio_info != nullptr);
2645 audio_info->rejected = true;
2646 EXPECT_TRUE(DoSetLocalDescription(local_offer.release()));
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002647 // Track state may be updated asynchronously.
perkjd61bf802016-03-24 03:16:19 -07002648 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002649 remote_audio->state(), kTimeout);
2650 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
2651 remote_video->state(), kTimeout);
deadbeefab9b2d12015-10-14 11:33:11 -07002652}
2653
2654// This tests that we won't crash if the remote track has been removed outside
2655// of PeerConnection and then PeerConnection tries to reject the track.
2656TEST_F(PeerConnectionInterfaceTest, RemoveTrackThenRejectMediaContent) {
2657 FakeConstraints constraints;
2658 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2659 true);
2660 CreatePeerConnection(&constraints);
2661 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2662 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2663 remote_stream->RemoveTrack(remote_stream->GetVideoTracks()[0]);
2664 remote_stream->RemoveTrack(remote_stream->GetAudioTracks()[0]);
2665
kwibergd1fe2812016-04-27 06:47:29 -07002666 std::unique_ptr<SessionDescriptionInterface> local_answer(
deadbeefab9b2d12015-10-14 11:33:11 -07002667 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
2668 kSdpStringWithStream1, nullptr));
2669 cricket::ContentInfo* video_info =
2670 local_answer->description()->GetContentByName("video");
2671 video_info->rejected = true;
2672 cricket::ContentInfo* audio_info =
2673 local_answer->description()->GetContentByName("audio");
2674 audio_info->rejected = true;
2675 EXPECT_TRUE(DoSetLocalDescription(local_answer.release()));
2676
2677 // No crash is a pass.
2678}
2679
deadbeef5e97fb52015-10-15 12:49:08 -07002680// This tests that if a recvonly remote description is set, no remote streams
2681// will be created, even if the description contains SSRCs/MSIDs.
2682// See: https://code.google.com/p/webrtc/issues/detail?id=5054
2683TEST_F(PeerConnectionInterfaceTest, RecvonlyDescriptionDoesntCreateStream) {
2684 FakeConstraints constraints;
2685 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2686 true);
2687 CreatePeerConnection(&constraints);
2688
2689 std::string recvonly_offer = kSdpStringWithStream1;
2690 rtc::replace_substrs(kSendrecv, strlen(kSendrecv), kRecvonly,
2691 strlen(kRecvonly), &recvonly_offer);
2692 CreateAndSetRemoteOffer(recvonly_offer);
2693
2694 EXPECT_EQ(0u, observer_.remote_streams()->count());
2695}
2696
deadbeefab9b2d12015-10-14 11:33:11 -07002697// This tests that a default MediaStream is created if a remote session
2698// description doesn't contain any streams and no MSID support.
2699// It also tests that the default stream is updated if a video m-line is added
2700// in a subsequent session description.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002701TEST_F(PeerConnectionInterfaceTest, SdpWithoutMsidCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002702 FakeConstraints constraints;
2703 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2704 true);
2705 CreatePeerConnection(&constraints);
2706 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2707
2708 ASSERT_EQ(1u, observer_.remote_streams()->count());
2709 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2710
2711 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2712 EXPECT_EQ(0u, remote_stream->GetVideoTracks().size());
2713 EXPECT_EQ("default", remote_stream->label());
2714
2715 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2716 ASSERT_EQ(1u, observer_.remote_streams()->count());
2717 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2718 EXPECT_EQ("defaulta0", remote_stream->GetAudioTracks()[0]->id());
deadbeef884f5852016-01-15 09:20:04 -08002719 EXPECT_EQ(MediaStreamTrackInterface::kLive,
2720 remote_stream->GetAudioTracks()[0]->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002721 ASSERT_EQ(1u, remote_stream->GetVideoTracks().size());
2722 EXPECT_EQ("defaultv0", remote_stream->GetVideoTracks()[0]->id());
deadbeef884f5852016-01-15 09:20:04 -08002723 EXPECT_EQ(MediaStreamTrackInterface::kLive,
2724 remote_stream->GetVideoTracks()[0]->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002725}
2726
2727// This tests that a default MediaStream is created if a remote session
2728// description doesn't contain any streams and media direction is send only.
2729TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002730 SendOnlySdpWithoutMsidCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002731 FakeConstraints constraints;
2732 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2733 true);
2734 CreatePeerConnection(&constraints);
2735 CreateAndSetRemoteOffer(kSdpStringSendOnlyWithoutStreams);
2736
2737 ASSERT_EQ(1u, observer_.remote_streams()->count());
2738 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2739
2740 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2741 EXPECT_EQ(1u, remote_stream->GetVideoTracks().size());
2742 EXPECT_EQ("default", remote_stream->label());
2743}
2744
2745// This tests that it won't crash when PeerConnection tries to remove
2746// a remote track that as already been removed from the MediaStream.
2747TEST_F(PeerConnectionInterfaceTest, RemoveAlreadyGoneRemoteStream) {
2748 FakeConstraints constraints;
2749 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2750 true);
2751 CreatePeerConnection(&constraints);
2752 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2753 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2754 remote_stream->RemoveTrack(remote_stream->GetAudioTracks()[0]);
2755 remote_stream->RemoveTrack(remote_stream->GetVideoTracks()[0]);
2756
2757 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2758
2759 // No crash is a pass.
2760}
2761
2762// This tests that a default MediaStream is created if the remote session
2763// description doesn't contain any streams and don't contain an indication if
2764// MSID is supported.
2765TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002766 SdpWithoutMsidAndStreamsCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002767 FakeConstraints constraints;
2768 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2769 true);
2770 CreatePeerConnection(&constraints);
2771 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2772
2773 ASSERT_EQ(1u, observer_.remote_streams()->count());
2774 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2775 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2776 EXPECT_EQ(1u, remote_stream->GetVideoTracks().size());
2777}
2778
2779// This tests that a default MediaStream is not created if the remote session
2780// description doesn't contain any streams but does support MSID.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002781TEST_F(PeerConnectionInterfaceTest, SdpWithMsidDontCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002782 FakeConstraints constraints;
2783 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2784 true);
2785 CreatePeerConnection(&constraints);
2786 CreateAndSetRemoteOffer(kSdpStringWithMsidWithoutStreams);
2787 EXPECT_EQ(0u, observer_.remote_streams()->count());
2788}
2789
deadbeefbda7e0b2015-12-08 17:13:40 -08002790// This tests that when setting a new description, the old default tracks are
2791// not destroyed and recreated.
2792// See: https://bugs.chromium.org/p/webrtc/issues/detail?id=5250
Stefan Holmer102362b2016-03-18 09:39:07 +01002793TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002794 DefaultTracksNotDestroyedAndRecreated) {
deadbeefbda7e0b2015-12-08 17:13:40 -08002795 FakeConstraints constraints;
2796 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2797 true);
2798 CreatePeerConnection(&constraints);
2799 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2800
2801 ASSERT_EQ(1u, observer_.remote_streams()->count());
2802 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2803 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2804
2805 // Set the track to "disabled", then set a new description and ensure the
2806 // track is still disabled, which ensures it hasn't been recreated.
2807 remote_stream->GetAudioTracks()[0]->set_enabled(false);
2808 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2809 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2810 EXPECT_FALSE(remote_stream->GetAudioTracks()[0]->enabled());
2811}
2812
deadbeefab9b2d12015-10-14 11:33:11 -07002813// This tests that a default MediaStream is not created if a remote session
2814// description is updated to not have any MediaStreams.
2815TEST_F(PeerConnectionInterfaceTest, VerifyDefaultStreamIsNotCreated) {
2816 FakeConstraints constraints;
2817 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2818 true);
2819 CreatePeerConnection(&constraints);
2820 CreateAndSetRemoteOffer(kSdpStringWithStream1);
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002821 rtc::scoped_refptr<StreamCollection> reference(CreateStreamCollection(1, 1));
deadbeefab9b2d12015-10-14 11:33:11 -07002822 EXPECT_TRUE(
2823 CompareStreamCollections(observer_.remote_streams(), reference.get()));
2824
2825 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2826 EXPECT_EQ(0u, observer_.remote_streams()->count());
2827}
2828
2829// This tests that an RtpSender is created when the local description is set
2830// after adding a local stream.
2831// TODO(deadbeef): This test and the one below it need to be updated when
2832// an RtpSender's lifetime isn't determined by when a local description is set.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002833TEST_F(PeerConnectionInterfaceTest, LocalDescriptionChanged) {
deadbeefab9b2d12015-10-14 11:33:11 -07002834 FakeConstraints constraints;
2835 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2836 true);
2837 CreatePeerConnection(&constraints);
deadbeefab9b2d12015-10-14 11:33:11 -07002838
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002839 // Create an offer with 1 stream with 2 tracks of each type.
2840 rtc::scoped_refptr<StreamCollection> stream_collection =
2841 CreateStreamCollection(1, 2);
2842 pc_->AddStream(stream_collection->at(0));
2843 std::unique_ptr<SessionDescriptionInterface> offer;
2844 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2845 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
deadbeefab9b2d12015-10-14 11:33:11 -07002846
deadbeefab9b2d12015-10-14 11:33:11 -07002847 auto senders = pc_->GetSenders();
2848 EXPECT_EQ(4u, senders.size());
2849 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2850 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2851 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[1]));
2852 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[1]));
2853
2854 // Remove an audio and video track.
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002855 pc_->RemoveStream(stream_collection->at(0));
2856 stream_collection = CreateStreamCollection(1, 1);
2857 pc_->AddStream(stream_collection->at(0));
2858 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2859 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
2860
deadbeefab9b2d12015-10-14 11:33:11 -07002861 senders = pc_->GetSenders();
2862 EXPECT_EQ(2u, senders.size());
2863 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2864 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2865 EXPECT_FALSE(ContainsSender(senders, kAudioTracks[1]));
2866 EXPECT_FALSE(ContainsSender(senders, kVideoTracks[1]));
2867}
2868
2869// This tests that an RtpSender is created when the local description is set
2870// before adding a local stream.
2871TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002872 AddLocalStreamAfterLocalDescriptionChanged) {
deadbeefab9b2d12015-10-14 11:33:11 -07002873 FakeConstraints constraints;
2874 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2875 true);
2876 CreatePeerConnection(&constraints);
deadbeefab9b2d12015-10-14 11:33:11 -07002877
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002878 rtc::scoped_refptr<StreamCollection> stream_collection =
2879 CreateStreamCollection(1, 2);
2880 // Add a stream to create the offer, but remove it afterwards.
2881 pc_->AddStream(stream_collection->at(0));
2882 std::unique_ptr<SessionDescriptionInterface> offer;
2883 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2884 pc_->RemoveStream(stream_collection->at(0));
deadbeefab9b2d12015-10-14 11:33:11 -07002885
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002886 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
deadbeefab9b2d12015-10-14 11:33:11 -07002887 auto senders = pc_->GetSenders();
2888 EXPECT_EQ(0u, senders.size());
2889
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002890 pc_->AddStream(stream_collection->at(0));
deadbeefab9b2d12015-10-14 11:33:11 -07002891 senders = pc_->GetSenders();
2892 EXPECT_EQ(4u, senders.size());
2893 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2894 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2895 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[1]));
2896 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[1]));
2897}
2898
2899// This tests that the expected behavior occurs if the SSRC on a local track is
2900// changed when SetLocalDescription is called.
2901TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002902 ChangeSsrcOnTrackInLocalSessionDescription) {
deadbeefab9b2d12015-10-14 11:33:11 -07002903 FakeConstraints constraints;
2904 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2905 true);
2906 CreatePeerConnection(&constraints);
deadbeefab9b2d12015-10-14 11:33:11 -07002907
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002908 rtc::scoped_refptr<StreamCollection> stream_collection =
2909 CreateStreamCollection(2, 1);
2910 pc_->AddStream(stream_collection->at(0));
2911 std::unique_ptr<SessionDescriptionInterface> offer;
2912 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2913 // Grab a copy of the offer before it gets passed into the PC.
2914 std::unique_ptr<JsepSessionDescription> modified_offer(
2915 new JsepSessionDescription(JsepSessionDescription::kOffer));
2916 modified_offer->Initialize(offer->description()->Copy(), offer->session_id(),
2917 offer->session_version());
2918 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
deadbeefab9b2d12015-10-14 11:33:11 -07002919
deadbeefab9b2d12015-10-14 11:33:11 -07002920 auto senders = pc_->GetSenders();
2921 EXPECT_EQ(2u, senders.size());
2922 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2923 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2924
2925 // Change the ssrc of the audio and video track.
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002926 cricket::MediaContentDescription* desc =
2927 cricket::GetFirstAudioContentDescription(modified_offer->description());
2928 ASSERT_TRUE(desc != NULL);
2929 for (StreamParams& stream : desc->mutable_streams()) {
2930 for (unsigned int& ssrc : stream.ssrcs) {
2931 ++ssrc;
2932 }
2933 }
deadbeefab9b2d12015-10-14 11:33:11 -07002934
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002935 desc =
2936 cricket::GetFirstVideoContentDescription(modified_offer->description());
2937 ASSERT_TRUE(desc != NULL);
2938 for (StreamParams& stream : desc->mutable_streams()) {
2939 for (unsigned int& ssrc : stream.ssrcs) {
2940 ++ssrc;
2941 }
2942 }
2943
2944 EXPECT_TRUE(DoSetLocalDescription(modified_offer.release()));
deadbeefab9b2d12015-10-14 11:33:11 -07002945 senders = pc_->GetSenders();
2946 EXPECT_EQ(2u, senders.size());
2947 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2948 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2949 // TODO(deadbeef): Once RtpSenders expose parameters, check that the SSRC
2950 // changed.
2951}
2952
2953// This tests that the expected behavior occurs if a new session description is
2954// set with the same tracks, but on a different MediaStream.
Stefan Holmer55d6e7c2016-03-17 16:26:40 +01002955TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002956 SignalSameTracksInSeparateMediaStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002957 FakeConstraints constraints;
2958 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2959 true);
2960 CreatePeerConnection(&constraints);
deadbeefab9b2d12015-10-14 11:33:11 -07002961
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002962 rtc::scoped_refptr<StreamCollection> stream_collection =
2963 CreateStreamCollection(2, 1);
2964 pc_->AddStream(stream_collection->at(0));
2965 std::unique_ptr<SessionDescriptionInterface> offer;
2966 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2967 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
deadbeefab9b2d12015-10-14 11:33:11 -07002968
deadbeefab9b2d12015-10-14 11:33:11 -07002969 auto senders = pc_->GetSenders();
2970 EXPECT_EQ(2u, senders.size());
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002971 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0], kStreams[0]));
2972 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0], kStreams[0]));
deadbeefab9b2d12015-10-14 11:33:11 -07002973
2974 // Add a new MediaStream but with the same tracks as in the first stream.
2975 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream_1(
2976 webrtc::MediaStream::Create(kStreams[1]));
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002977 stream_1->AddTrack(stream_collection->at(0)->GetVideoTracks()[0]);
2978 stream_1->AddTrack(stream_collection->at(0)->GetAudioTracks()[0]);
deadbeefab9b2d12015-10-14 11:33:11 -07002979 pc_->AddStream(stream_1);
2980
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002981 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2982 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
deadbeefab9b2d12015-10-14 11:33:11 -07002983
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002984 auto new_senders = pc_->GetSenders();
2985 // Should be the same senders as before, but with updated stream id.
2986 // Note that this behavior is subject to change in the future.
2987 // We may decide the PC should ignore existing tracks in AddStream.
2988 EXPECT_EQ(senders, new_senders);
2989 EXPECT_TRUE(ContainsSender(new_senders, kAudioTracks[0], kStreams[1]));
2990 EXPECT_TRUE(ContainsSender(new_senders, kVideoTracks[0], kStreams[1]));
deadbeefab9b2d12015-10-14 11:33:11 -07002991}
2992
zhihuang81c3a032016-11-17 12:06:24 -08002993// This tests that PeerConnectionObserver::OnAddTrack is correctly called.
2994TEST_F(PeerConnectionInterfaceTest, OnAddTrackCallback) {
2995 FakeConstraints constraints;
2996 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2997 true);
2998 CreatePeerConnection(&constraints);
2999 CreateAndSetRemoteOffer(kSdpStringWithStream1AudioTrackOnly);
3000 EXPECT_EQ(observer_.num_added_tracks_, 1);
3001 EXPECT_EQ(observer_.last_added_track_label_, kAudioTracks[0]);
3002
3003 // Create and set the updated remote SDP.
3004 CreateAndSetRemoteOffer(kSdpStringWithStream1);
3005 EXPECT_EQ(observer_.num_added_tracks_, 2);
3006 EXPECT_EQ(observer_.last_added_track_label_, kVideoTracks[0]);
3007}
3008
deadbeefd1a38b52016-12-10 13:15:33 -08003009// Test that when SetConfiguration is called and the configuration is
3010// changing, the next offer causes an ICE restart.
3011TEST_F(PeerConnectionInterfaceTest, SetConfigurationCausingIceRetart) {
3012 PeerConnectionInterface::RTCConfiguration config;
3013 config.type = PeerConnectionInterface::kRelay;
3014 // Need to pass default constraints to prevent disabling of DTLS...
3015 FakeConstraints default_constraints;
3016 CreatePeerConnection(config, &default_constraints);
3017 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
3018
3019 // Do initial offer/answer so there's something to restart.
3020 CreateOfferAsLocalDescription();
3021 CreateAnswerAsRemoteDescription(kSdpStringWithStream1);
3022
3023 // Grab the ufrags.
3024 std::vector<std::string> initial_ufrags = GetUfrags(pc_->local_description());
3025
3026 // Change ICE policy, which should trigger an ICE restart on the next offer.
3027 config.type = PeerConnectionInterface::kAll;
3028 EXPECT_TRUE(pc_->SetConfiguration(config));
3029 CreateOfferAsLocalDescription();
3030
3031 // Grab the new ufrags.
3032 std::vector<std::string> subsequent_ufrags =
3033 GetUfrags(pc_->local_description());
3034
3035 // Sanity check.
3036 EXPECT_EQ(initial_ufrags.size(), subsequent_ufrags.size());
3037 // Check that each ufrag is different.
3038 for (int i = 0; i < static_cast<int>(initial_ufrags.size()); ++i) {
3039 EXPECT_NE(initial_ufrags[i], subsequent_ufrags[i]);
3040 }
3041}
3042
3043// Test that when SetConfiguration is called and the configuration *isn't*
3044// changing, the next offer does *not* cause an ICE restart.
3045TEST_F(PeerConnectionInterfaceTest, SetConfigurationNotCausingIceRetart) {
3046 PeerConnectionInterface::RTCConfiguration config;
3047 config.type = PeerConnectionInterface::kRelay;
3048 // Need to pass default constraints to prevent disabling of DTLS...
3049 FakeConstraints default_constraints;
3050 CreatePeerConnection(config, &default_constraints);
3051 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
3052
3053 // Do initial offer/answer so there's something to restart.
3054 CreateOfferAsLocalDescription();
3055 CreateAnswerAsRemoteDescription(kSdpStringWithStream1);
3056
3057 // Grab the ufrags.
3058 std::vector<std::string> initial_ufrags = GetUfrags(pc_->local_description());
3059
3060 // Call SetConfiguration with a config identical to what the PC was
3061 // constructed with.
3062 EXPECT_TRUE(pc_->SetConfiguration(config));
3063 CreateOfferAsLocalDescription();
3064
3065 // Grab the new ufrags.
3066 std::vector<std::string> subsequent_ufrags =
3067 GetUfrags(pc_->local_description());
3068
3069 EXPECT_EQ(initial_ufrags, subsequent_ufrags);
3070}
3071
3072// Test for a weird corner case scenario:
3073// 1. Audio/video session established.
3074// 2. SetConfiguration changes ICE config; ICE restart needed.
3075// 3. ICE restart initiated by remote peer, but only for one m= section.
3076// 4. Next createOffer should initiate an ICE restart, but only for the other
3077// m= section; it would be pointless to do an ICE restart for the m= section
3078// that was already restarted.
3079TEST_F(PeerConnectionInterfaceTest, SetConfigurationCausingPartialIceRestart) {
3080 PeerConnectionInterface::RTCConfiguration config;
3081 config.type = PeerConnectionInterface::kRelay;
3082 // Need to pass default constraints to prevent disabling of DTLS...
3083 FakeConstraints default_constraints;
3084 CreatePeerConnection(config, &default_constraints);
3085 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
3086
3087 // Do initial offer/answer so there's something to restart.
3088 CreateOfferAsLocalDescription();
3089 CreateAnswerAsRemoteDescription(kSdpStringWithStream1);
3090
3091 // Change ICE policy, which should set the "needs-ice-restart" flag.
3092 config.type = PeerConnectionInterface::kAll;
3093 EXPECT_TRUE(pc_->SetConfiguration(config));
3094
3095 // Do ICE restart for the first m= section, initiated by remote peer.
3096 webrtc::JsepSessionDescription* remote_offer =
3097 new webrtc::JsepSessionDescription(SessionDescriptionInterface::kOffer);
3098 EXPECT_TRUE(remote_offer->Initialize(kSdpStringWithStream1, nullptr));
3099 remote_offer->description()->transport_infos()[0].description.ice_ufrag =
3100 "modified";
3101 EXPECT_TRUE(DoSetRemoteDescription(remote_offer));
3102 CreateAnswerAsLocalDescription();
3103
3104 // Grab the ufrags.
3105 std::vector<std::string> initial_ufrags = GetUfrags(pc_->local_description());
3106 ASSERT_EQ(2, initial_ufrags.size());
3107
3108 // Create offer and grab the new ufrags.
3109 CreateOfferAsLocalDescription();
3110 std::vector<std::string> subsequent_ufrags =
3111 GetUfrags(pc_->local_description());
3112 ASSERT_EQ(2, subsequent_ufrags.size());
3113
3114 // Ensure that only the ufrag for the second m= section changed.
3115 EXPECT_EQ(initial_ufrags[0], subsequent_ufrags[0]);
3116 EXPECT_NE(initial_ufrags[1], subsequent_ufrags[1]);
3117}
3118
deadbeeffe4a8a42016-12-20 17:56:17 -08003119// Tests that the methods to return current/pending descriptions work as
3120// expected at different points in the offer/answer exchange. This test does
3121// one offer/answer exchange as the offerer, then another as the answerer.
3122TEST_F(PeerConnectionInterfaceTest, CurrentAndPendingDescriptions) {
3123 // This disables DTLS so we can apply an answer to ourselves.
3124 CreatePeerConnection();
3125
3126 // Create initial local offer and get SDP (which will also be used as
3127 // answer/pranswer);
3128 std::unique_ptr<SessionDescriptionInterface> offer;
3129 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
3130 std::string sdp;
3131 EXPECT_TRUE(offer->ToString(&sdp));
3132
3133 // Set local offer.
3134 SessionDescriptionInterface* local_offer = offer.release();
3135 EXPECT_TRUE(DoSetLocalDescription(local_offer));
3136 EXPECT_EQ(local_offer, pc_->pending_local_description());
3137 EXPECT_EQ(nullptr, pc_->pending_remote_description());
3138 EXPECT_EQ(nullptr, pc_->current_local_description());
3139 EXPECT_EQ(nullptr, pc_->current_remote_description());
3140
3141 // Set remote pranswer.
3142 SessionDescriptionInterface* remote_pranswer =
3143 webrtc::CreateSessionDescription(SessionDescriptionInterface::kPrAnswer,
3144 sdp, nullptr);
3145 EXPECT_TRUE(DoSetRemoteDescription(remote_pranswer));
3146 EXPECT_EQ(local_offer, pc_->pending_local_description());
3147 EXPECT_EQ(remote_pranswer, pc_->pending_remote_description());
3148 EXPECT_EQ(nullptr, pc_->current_local_description());
3149 EXPECT_EQ(nullptr, pc_->current_remote_description());
3150
3151 // Set remote answer.
3152 SessionDescriptionInterface* remote_answer = webrtc::CreateSessionDescription(
3153 SessionDescriptionInterface::kAnswer, sdp, nullptr);
3154 EXPECT_TRUE(DoSetRemoteDescription(remote_answer));
3155 EXPECT_EQ(nullptr, pc_->pending_local_description());
3156 EXPECT_EQ(nullptr, pc_->pending_remote_description());
3157 EXPECT_EQ(local_offer, pc_->current_local_description());
3158 EXPECT_EQ(remote_answer, pc_->current_remote_description());
3159
3160 // Set remote offer.
3161 SessionDescriptionInterface* remote_offer = webrtc::CreateSessionDescription(
3162 SessionDescriptionInterface::kOffer, sdp, nullptr);
3163 EXPECT_TRUE(DoSetRemoteDescription(remote_offer));
3164 EXPECT_EQ(remote_offer, pc_->pending_remote_description());
3165 EXPECT_EQ(nullptr, pc_->pending_local_description());
3166 EXPECT_EQ(local_offer, pc_->current_local_description());
3167 EXPECT_EQ(remote_answer, pc_->current_remote_description());
3168
3169 // Set local pranswer.
3170 SessionDescriptionInterface* local_pranswer =
3171 webrtc::CreateSessionDescription(SessionDescriptionInterface::kPrAnswer,
3172 sdp, nullptr);
3173 EXPECT_TRUE(DoSetLocalDescription(local_pranswer));
3174 EXPECT_EQ(remote_offer, pc_->pending_remote_description());
3175 EXPECT_EQ(local_pranswer, pc_->pending_local_description());
3176 EXPECT_EQ(local_offer, pc_->current_local_description());
3177 EXPECT_EQ(remote_answer, pc_->current_remote_description());
3178
3179 // Set local answer.
3180 SessionDescriptionInterface* local_answer = webrtc::CreateSessionDescription(
3181 SessionDescriptionInterface::kAnswer, sdp, nullptr);
3182 EXPECT_TRUE(DoSetLocalDescription(local_answer));
3183 EXPECT_EQ(nullptr, pc_->pending_remote_description());
3184 EXPECT_EQ(nullptr, pc_->pending_local_description());
3185 EXPECT_EQ(remote_offer, pc_->current_remote_description());
3186 EXPECT_EQ(local_answer, pc_->current_local_description());
3187}
3188
zhihuang77985012017-02-07 15:45:16 -08003189// Tests that it won't crash when calling StartRtcEventLog or StopRtcEventLog
3190// after the PeerConnection is closed.
3191TEST_F(PeerConnectionInterfaceTest,
3192 StartAndStopLoggingAfterPeerConnectionClosed) {
3193 CreatePeerConnection();
3194 // The RtcEventLog will be reset when the PeerConnection is closed.
3195 pc_->Close();
3196
3197 rtc::PlatformFile file = 0;
3198 int64_t max_size_bytes = 1024;
3199 EXPECT_FALSE(pc_->StartRtcEventLog(file, max_size_bytes));
3200 pc_->StopRtcEventLog();
3201}
3202
deadbeef30952b42017-04-21 02:41:29 -07003203// Test that generated offers/answers include "ice-option:trickle".
3204TEST_F(PeerConnectionInterfaceTest, OffersAndAnswersHaveTrickleIceOption) {
3205 CreatePeerConnection();
3206
3207 // First, create an offer with audio/video.
3208 FakeConstraints constraints;
3209 constraints.SetMandatoryReceiveAudio(true);
3210 constraints.SetMandatoryReceiveVideo(true);
3211 std::unique_ptr<SessionDescriptionInterface> offer;
3212 ASSERT_TRUE(DoCreateOffer(&offer, &constraints));
3213 cricket::SessionDescription* desc = offer->description();
3214 ASSERT_EQ(2u, desc->transport_infos().size());
3215 EXPECT_TRUE(desc->transport_infos()[0].description.HasOption("trickle"));
3216 EXPECT_TRUE(desc->transport_infos()[1].description.HasOption("trickle"));
3217
3218 // Apply the offer as a remote description, then create an answer.
3219 EXPECT_TRUE(DoSetRemoteDescription(offer.release()));
3220 std::unique_ptr<SessionDescriptionInterface> answer;
3221 ASSERT_TRUE(DoCreateAnswer(&answer, &constraints));
3222 desc = answer->description();
3223 ASSERT_EQ(2u, desc->transport_infos().size());
3224 EXPECT_TRUE(desc->transport_infos()[0].description.HasOption("trickle"));
3225 EXPECT_TRUE(desc->transport_infos()[1].description.HasOption("trickle"));
3226}
3227
deadbeef1dcb1642017-03-29 21:08:16 -07003228// Test that ICE renomination isn't offered if it's not enabled in the PC's
3229// RTCConfiguration.
3230TEST_F(PeerConnectionInterfaceTest, IceRenominationNotOffered) {
3231 PeerConnectionInterface::RTCConfiguration config;
3232 config.enable_ice_renomination = false;
3233 CreatePeerConnection(config, nullptr);
3234 AddVoiceStream("foo");
3235
3236 std::unique_ptr<SessionDescriptionInterface> offer;
3237 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
3238 cricket::SessionDescription* desc = offer->description();
3239 EXPECT_EQ(1u, desc->transport_infos().size());
3240 EXPECT_FALSE(
3241 desc->transport_infos()[0].description.GetIceParameters().renomination);
3242}
3243
3244// Test that the ICE renomination option is present in generated offers/answers
3245// if it's enabled in the PC's RTCConfiguration.
3246TEST_F(PeerConnectionInterfaceTest, IceRenominationOptionInOfferAndAnswer) {
3247 PeerConnectionInterface::RTCConfiguration config;
3248 config.enable_ice_renomination = true;
3249 CreatePeerConnection(config, nullptr);
3250 AddVoiceStream("foo");
3251
3252 std::unique_ptr<SessionDescriptionInterface> offer;
3253 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
3254 cricket::SessionDescription* desc = offer->description();
3255 EXPECT_EQ(1u, desc->transport_infos().size());
3256 EXPECT_TRUE(
3257 desc->transport_infos()[0].description.GetIceParameters().renomination);
3258
3259 // Set the offer as a remote description, then create an answer and ensure it
3260 // has the renomination flag too.
3261 EXPECT_TRUE(DoSetRemoteDescription(offer.release()));
3262 std::unique_ptr<SessionDescriptionInterface> answer;
3263 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
3264 desc = answer->description();
3265 EXPECT_EQ(1u, desc->transport_infos().size());
3266 EXPECT_TRUE(
3267 desc->transport_infos()[0].description.GetIceParameters().renomination);
3268}
3269
3270// Test that if CreateOffer is called with the deprecated "offer to receive
3271// audio/video" constraints, they're processed and result in an offer with
3272// audio/video sections just as if RTCOfferAnswerOptions had been used.
3273TEST_F(PeerConnectionInterfaceTest, CreateOfferWithOfferToReceiveConstraints) {
3274 CreatePeerConnection();
3275
3276 FakeConstraints constraints;
3277 constraints.SetMandatoryReceiveAudio(true);
3278 constraints.SetMandatoryReceiveVideo(true);
3279 std::unique_ptr<SessionDescriptionInterface> offer;
3280 ASSERT_TRUE(DoCreateOffer(&offer, &constraints));
3281
3282 cricket::SessionDescription* desc = offer->description();
3283 const cricket::ContentInfo* audio = cricket::GetFirstAudioContent(desc);
3284 const cricket::ContentInfo* video = cricket::GetFirstVideoContent(desc);
3285 ASSERT_NE(nullptr, audio);
3286 ASSERT_NE(nullptr, video);
3287 EXPECT_FALSE(audio->rejected);
3288 EXPECT_FALSE(video->rejected);
3289}
3290
3291// Test that if CreateAnswer is called with the deprecated "offer to receive
3292// audio/video" constraints, they're processed and can be used to reject an
3293// offered m= section just as can be done with RTCOfferAnswerOptions;
3294TEST_F(PeerConnectionInterfaceTest, CreateAnswerWithOfferToReceiveConstraints) {
3295 CreatePeerConnection();
3296
3297 // First, create an offer with audio/video and apply it as a remote
3298 // description.
3299 FakeConstraints constraints;
3300 constraints.SetMandatoryReceiveAudio(true);
3301 constraints.SetMandatoryReceiveVideo(true);
3302 std::unique_ptr<SessionDescriptionInterface> offer;
3303 ASSERT_TRUE(DoCreateOffer(&offer, &constraints));
3304 EXPECT_TRUE(DoSetRemoteDescription(offer.release()));
3305
3306 // Now create answer that rejects audio/video.
3307 constraints.SetMandatoryReceiveAudio(false);
3308 constraints.SetMandatoryReceiveVideo(false);
3309 std::unique_ptr<SessionDescriptionInterface> answer;
3310 ASSERT_TRUE(DoCreateAnswer(&answer, &constraints));
3311
3312 cricket::SessionDescription* desc = answer->description();
3313 const cricket::ContentInfo* audio = cricket::GetFirstAudioContent(desc);
3314 const cricket::ContentInfo* video = cricket::GetFirstVideoContent(desc);
3315 ASSERT_NE(nullptr, audio);
3316 ASSERT_NE(nullptr, video);
3317 EXPECT_TRUE(audio->rejected);
3318 EXPECT_TRUE(video->rejected);
3319}
3320
3321#ifdef HAVE_SCTP
3322#define MAYBE_DataChannelOnlyOfferWithMaxBundlePolicy \
3323 DataChannelOnlyOfferWithMaxBundlePolicy
3324#else
3325#define MAYBE_DataChannelOnlyOfferWithMaxBundlePolicy \
3326 DISABLED_DataChannelOnlyOfferWithMaxBundlePolicy
3327#endif
3328
3329// Test that negotiation can succeed with a data channel only, and with the max
3330// bundle policy. Previously there was a bug that prevented this.
3331TEST_F(PeerConnectionInterfaceTest,
3332 MAYBE_DataChannelOnlyOfferWithMaxBundlePolicy) {
3333 PeerConnectionInterface::RTCConfiguration config;
3334 config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
3335 CreatePeerConnection(config, nullptr);
3336
3337 // First, create an offer with only a data channel and apply it as a remote
3338 // description.
3339 pc_->CreateDataChannel("test", nullptr);
3340 std::unique_ptr<SessionDescriptionInterface> offer;
3341 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
3342 EXPECT_TRUE(DoSetRemoteDescription(offer.release()));
3343
3344 // Create and set answer as well.
3345 std::unique_ptr<SessionDescriptionInterface> answer;
3346 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
3347 EXPECT_TRUE(DoSetLocalDescription(answer.release()));
3348}
3349
zstein4b979802017-06-02 14:37:37 -07003350TEST_F(PeerConnectionInterfaceTest, SetBitrateWithoutMinSucceeds) {
3351 CreatePeerConnection();
3352 PeerConnectionInterface::BitrateParameters bitrate;
3353 bitrate.current_bitrate_bps = rtc::Optional<int>(100000);
3354 EXPECT_TRUE(pc_->SetBitrate(bitrate).ok());
3355}
3356
3357TEST_F(PeerConnectionInterfaceTest, SetBitrateNegativeMinFails) {
3358 CreatePeerConnection();
3359 PeerConnectionInterface::BitrateParameters bitrate;
3360 bitrate.min_bitrate_bps = rtc::Optional<int>(-1);
3361 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3362}
3363
3364TEST_F(PeerConnectionInterfaceTest, SetBitrateCurrentLessThanMinFails) {
3365 CreatePeerConnection();
3366 PeerConnectionInterface::BitrateParameters bitrate;
3367 bitrate.min_bitrate_bps = rtc::Optional<int>(5);
3368 bitrate.current_bitrate_bps = rtc::Optional<int>(3);
3369 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3370}
3371
3372TEST_F(PeerConnectionInterfaceTest, SetBitrateCurrentNegativeFails) {
3373 CreatePeerConnection();
3374 PeerConnectionInterface::BitrateParameters bitrate;
3375 bitrate.current_bitrate_bps = rtc::Optional<int>(-1);
3376 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3377}
3378
3379TEST_F(PeerConnectionInterfaceTest, SetBitrateMaxLessThanCurrentFails) {
3380 CreatePeerConnection();
3381 PeerConnectionInterface::BitrateParameters bitrate;
3382 bitrate.current_bitrate_bps = rtc::Optional<int>(10);
3383 bitrate.max_bitrate_bps = rtc::Optional<int>(8);
3384 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3385}
3386
3387TEST_F(PeerConnectionInterfaceTest, SetBitrateMaxLessThanMinFails) {
3388 CreatePeerConnection();
3389 PeerConnectionInterface::BitrateParameters bitrate;
3390 bitrate.min_bitrate_bps = rtc::Optional<int>(10);
3391 bitrate.max_bitrate_bps = rtc::Optional<int>(8);
3392 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3393}
3394
3395TEST_F(PeerConnectionInterfaceTest, SetBitrateMaxNegativeFails) {
3396 CreatePeerConnection();
3397 PeerConnectionInterface::BitrateParameters bitrate;
3398 bitrate.max_bitrate_bps = rtc::Optional<int>(-1);
3399 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3400}
3401
3402// The current bitrate from Call's BitrateConfigMask is currently clamped by
3403// Call's BitrateConfig, which comes from the SDP or a default value. This test
3404// checks that a call to SetBitrate with a current bitrate that will be clamped
3405// succeeds.
3406TEST_F(PeerConnectionInterfaceTest, SetBitrateCurrentLessThanImplicitMin) {
3407 CreatePeerConnection();
3408 PeerConnectionInterface::BitrateParameters bitrate;
3409 bitrate.current_bitrate_bps = rtc::Optional<int>(1);
3410 EXPECT_TRUE(pc_->SetBitrate(bitrate).ok());
3411}
3412
nisse51542be2016-02-12 02:27:06 -08003413class PeerConnectionMediaConfigTest : public testing::Test {
3414 protected:
3415 void SetUp() override {
zhihuang38ede132017-06-15 12:52:32 -07003416 pcf_ = PeerConnectionFactoryForTest::CreatePeerConnectionFactoryForTest();
nisse51542be2016-02-12 02:27:06 -08003417 pcf_->Initialize();
3418 }
nisseeaabdf62017-05-05 02:23:02 -07003419 const cricket::MediaConfig TestCreatePeerConnection(
nisse51542be2016-02-12 02:27:06 -08003420 const PeerConnectionInterface::RTCConfiguration& config,
3421 const MediaConstraintsInterface *constraints) {
nisse51542be2016-02-12 02:27:06 -08003422
zhihuang9763d562016-08-05 11:14:50 -07003423 rtc::scoped_refptr<PeerConnectionInterface> pc(pcf_->CreatePeerConnection(
3424 config, constraints, nullptr, nullptr, &observer_));
nisse51542be2016-02-12 02:27:06 -08003425 EXPECT_TRUE(pc.get());
nisseeaabdf62017-05-05 02:23:02 -07003426 return pc->GetConfiguration().media_config;
nisse51542be2016-02-12 02:27:06 -08003427 }
3428
zhihuang9763d562016-08-05 11:14:50 -07003429 rtc::scoped_refptr<PeerConnectionFactoryForTest> pcf_;
nisse51542be2016-02-12 02:27:06 -08003430 MockPeerConnectionObserver observer_;
3431};
3432
3433// This test verifies the default behaviour with no constraints and a
3434// default RTCConfiguration.
3435TEST_F(PeerConnectionMediaConfigTest, TestDefaults) {
3436 PeerConnectionInterface::RTCConfiguration config;
3437 FakeConstraints constraints;
3438
3439 const cricket::MediaConfig& media_config =
3440 TestCreatePeerConnection(config, &constraints);
3441
3442 EXPECT_FALSE(media_config.enable_dscp);
nisse0db023a2016-03-01 04:29:59 -08003443 EXPECT_TRUE(media_config.video.enable_cpu_overuse_detection);
3444 EXPECT_FALSE(media_config.video.disable_prerenderer_smoothing);
3445 EXPECT_FALSE(media_config.video.suspend_below_min_bitrate);
nisse51542be2016-02-12 02:27:06 -08003446}
3447
3448// This test verifies the DSCP constraint is recognized and passed to
nisse528b7932017-05-08 03:21:43 -07003449// the PeerConnection.
nisse51542be2016-02-12 02:27:06 -08003450TEST_F(PeerConnectionMediaConfigTest, TestDscpConstraintTrue) {
3451 PeerConnectionInterface::RTCConfiguration config;
3452 FakeConstraints constraints;
3453
3454 constraints.AddOptional(webrtc::MediaConstraintsInterface::kEnableDscp, true);
3455 const cricket::MediaConfig& media_config =
3456 TestCreatePeerConnection(config, &constraints);
3457
3458 EXPECT_TRUE(media_config.enable_dscp);
3459}
3460
3461// This test verifies the cpu overuse detection constraint is
nisse528b7932017-05-08 03:21:43 -07003462// recognized and passed to the PeerConnection.
nisse51542be2016-02-12 02:27:06 -08003463TEST_F(PeerConnectionMediaConfigTest, TestCpuOveruseConstraintFalse) {
3464 PeerConnectionInterface::RTCConfiguration config;
3465 FakeConstraints constraints;
3466
3467 constraints.AddOptional(
3468 webrtc::MediaConstraintsInterface::kCpuOveruseDetection, false);
3469 const cricket::MediaConfig media_config =
3470 TestCreatePeerConnection(config, &constraints);
3471
nisse0db023a2016-03-01 04:29:59 -08003472 EXPECT_FALSE(media_config.video.enable_cpu_overuse_detection);
nisse51542be2016-02-12 02:27:06 -08003473}
3474
3475// This test verifies that the disable_prerenderer_smoothing flag is
nisse528b7932017-05-08 03:21:43 -07003476// propagated from RTCConfiguration to the PeerConnection.
nisse51542be2016-02-12 02:27:06 -08003477TEST_F(PeerConnectionMediaConfigTest, TestDisablePrerendererSmoothingTrue) {
3478 PeerConnectionInterface::RTCConfiguration config;
3479 FakeConstraints constraints;
3480
Niels Möller71bdda02016-03-31 12:59:59 +02003481 config.set_prerenderer_smoothing(false);
nisse51542be2016-02-12 02:27:06 -08003482 const cricket::MediaConfig& media_config =
3483 TestCreatePeerConnection(config, &constraints);
3484
nisse0db023a2016-03-01 04:29:59 -08003485 EXPECT_TRUE(media_config.video.disable_prerenderer_smoothing);
3486}
3487
3488// This test verifies the suspend below min bitrate constraint is
nisse528b7932017-05-08 03:21:43 -07003489// recognized and passed to the PeerConnection.
nisse0db023a2016-03-01 04:29:59 -08003490TEST_F(PeerConnectionMediaConfigTest,
3491 TestSuspendBelowMinBitrateConstraintTrue) {
3492 PeerConnectionInterface::RTCConfiguration config;
3493 FakeConstraints constraints;
3494
3495 constraints.AddOptional(
3496 webrtc::MediaConstraintsInterface::kEnableVideoSuspendBelowMinBitrate,
3497 true);
3498 const cricket::MediaConfig media_config =
3499 TestCreatePeerConnection(config, &constraints);
3500
3501 EXPECT_TRUE(media_config.video.suspend_below_min_bitrate);
nisse51542be2016-02-12 02:27:06 -08003502}
3503
deadbeefab9b2d12015-10-14 11:33:11 -07003504// The following tests verify that session options are created correctly.
deadbeefc80741f2015-10-22 13:14:45 -07003505// TODO(deadbeef): Convert these tests to be more end-to-end. Instead of
3506// "verify options are converted correctly", should be "pass options into
3507// CreateOffer and verify the correct offer is produced."
deadbeefab9b2d12015-10-14 11:33:11 -07003508
3509TEST(CreateSessionOptionsTest, GetOptionsForOfferWithInvalidAudioOption) {
3510 RTCOfferAnswerOptions rtc_options;
3511 rtc_options.offer_to_receive_audio = RTCOfferAnswerOptions::kUndefined - 1;
3512
3513 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08003514 EXPECT_FALSE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07003515
3516 rtc_options.offer_to_receive_audio =
3517 RTCOfferAnswerOptions::kMaxOfferToReceiveMedia + 1;
htaaac2dea2016-03-10 13:35:55 -08003518 EXPECT_FALSE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07003519}
3520
3521TEST(CreateSessionOptionsTest, GetOptionsForOfferWithInvalidVideoOption) {
3522 RTCOfferAnswerOptions rtc_options;
3523 rtc_options.offer_to_receive_video = RTCOfferAnswerOptions::kUndefined - 1;
3524
3525 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08003526 EXPECT_FALSE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07003527
3528 rtc_options.offer_to_receive_video =
3529 RTCOfferAnswerOptions::kMaxOfferToReceiveMedia + 1;
htaaac2dea2016-03-10 13:35:55 -08003530 EXPECT_FALSE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07003531}
3532
3533// Test that a MediaSessionOptions is created for an offer if
deadbeefc80741f2015-10-22 13:14:45 -07003534// OfferToReceiveAudio and OfferToReceiveVideo options are set.
deadbeefab9b2d12015-10-14 11:33:11 -07003535TEST(CreateSessionOptionsTest, GetMediaSessionOptionsForOfferWithAudioVideo) {
3536 RTCOfferAnswerOptions rtc_options;
3537 rtc_options.offer_to_receive_audio = 1;
3538 rtc_options.offer_to_receive_video = 1;
3539
3540 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08003541 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07003542 EXPECT_TRUE(options.has_audio());
3543 EXPECT_TRUE(options.has_video());
3544 EXPECT_TRUE(options.bundle_enabled);
3545}
3546
3547// Test that a correct MediaSessionOptions is created for an offer if
deadbeefc80741f2015-10-22 13:14:45 -07003548// OfferToReceiveAudio is set.
deadbeefab9b2d12015-10-14 11:33:11 -07003549TEST(CreateSessionOptionsTest, GetMediaSessionOptionsForOfferWithAudio) {
3550 RTCOfferAnswerOptions rtc_options;
3551 rtc_options.offer_to_receive_audio = 1;
3552
3553 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08003554 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07003555 EXPECT_TRUE(options.has_audio());
3556 EXPECT_FALSE(options.has_video());
3557 EXPECT_TRUE(options.bundle_enabled);
3558}
3559
3560// Test that a correct MediaSessionOptions is created for an offer if
deadbeefc80741f2015-10-22 13:14:45 -07003561// the default OfferOptions are used.
deadbeefab9b2d12015-10-14 11:33:11 -07003562TEST(CreateSessionOptionsTest, GetDefaultMediaSessionOptionsForOffer) {
3563 RTCOfferAnswerOptions rtc_options;
3564
3565 cricket::MediaSessionOptions options;
deadbeef0ed85b22016-02-23 17:24:52 -08003566 options.transport_options["audio"] = cricket::TransportOptions();
3567 options.transport_options["video"] = cricket::TransportOptions();
htaaac2dea2016-03-10 13:35:55 -08003568 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefc80741f2015-10-22 13:14:45 -07003569 EXPECT_TRUE(options.has_audio());
deadbeefab9b2d12015-10-14 11:33:11 -07003570 EXPECT_FALSE(options.has_video());
deadbeefc80741f2015-10-22 13:14:45 -07003571 EXPECT_TRUE(options.bundle_enabled);
deadbeefab9b2d12015-10-14 11:33:11 -07003572 EXPECT_TRUE(options.vad_enabled);
deadbeef0ed85b22016-02-23 17:24:52 -08003573 EXPECT_FALSE(options.transport_options["audio"].ice_restart);
3574 EXPECT_FALSE(options.transport_options["video"].ice_restart);
deadbeefab9b2d12015-10-14 11:33:11 -07003575}
3576
3577// Test that a correct MediaSessionOptions is created for an offer if
deadbeefc80741f2015-10-22 13:14:45 -07003578// OfferToReceiveVideo is set.
deadbeefab9b2d12015-10-14 11:33:11 -07003579TEST(CreateSessionOptionsTest, GetMediaSessionOptionsForOfferWithVideo) {
3580 RTCOfferAnswerOptions rtc_options;
3581 rtc_options.offer_to_receive_audio = 0;
3582 rtc_options.offer_to_receive_video = 1;
3583
3584 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08003585 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07003586 EXPECT_FALSE(options.has_audio());
3587 EXPECT_TRUE(options.has_video());
3588 EXPECT_TRUE(options.bundle_enabled);
3589}
3590
3591// Test that a correct MediaSessionOptions is created for an offer if
3592// UseRtpMux is set to false.
3593TEST(CreateSessionOptionsTest,
3594 GetMediaSessionOptionsForOfferWithBundleDisabled) {
3595 RTCOfferAnswerOptions rtc_options;
3596 rtc_options.offer_to_receive_audio = 1;
3597 rtc_options.offer_to_receive_video = 1;
3598 rtc_options.use_rtp_mux = false;
3599
3600 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08003601 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07003602 EXPECT_TRUE(options.has_audio());
3603 EXPECT_TRUE(options.has_video());
3604 EXPECT_FALSE(options.bundle_enabled);
3605}
3606
3607// Test that a correct MediaSessionOptions is created to restart ice if
3608// IceRestart is set. It also tests that subsequent MediaSessionOptions don't
Taylor Brandstetterf475d362016-01-08 15:35:57 -08003609// have |audio_transport_options.ice_restart| etc. set.
deadbeefab9b2d12015-10-14 11:33:11 -07003610TEST(CreateSessionOptionsTest, GetMediaSessionOptionsForOfferWithIceRestart) {
3611 RTCOfferAnswerOptions rtc_options;
3612 rtc_options.ice_restart = true;
3613
3614 cricket::MediaSessionOptions options;
deadbeef0ed85b22016-02-23 17:24:52 -08003615 options.transport_options["audio"] = cricket::TransportOptions();
3616 options.transport_options["video"] = cricket::TransportOptions();
htaaac2dea2016-03-10 13:35:55 -08003617 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeef0ed85b22016-02-23 17:24:52 -08003618 EXPECT_TRUE(options.transport_options["audio"].ice_restart);
3619 EXPECT_TRUE(options.transport_options["video"].ice_restart);
deadbeefab9b2d12015-10-14 11:33:11 -07003620
3621 rtc_options = RTCOfferAnswerOptions();
htaaac2dea2016-03-10 13:35:55 -08003622 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeef0ed85b22016-02-23 17:24:52 -08003623 EXPECT_FALSE(options.transport_options["audio"].ice_restart);
3624 EXPECT_FALSE(options.transport_options["video"].ice_restart);
deadbeefab9b2d12015-10-14 11:33:11 -07003625}
3626
3627// Test that the MediaConstraints in an answer don't affect if audio and video
3628// is offered in an offer but that if kOfferToReceiveAudio or
3629// kOfferToReceiveVideo constraints are true in an offer, the media type will be
3630// included in subsequent answers.
3631TEST(CreateSessionOptionsTest, MediaConstraintsInAnswer) {
3632 FakeConstraints answer_c;
3633 answer_c.SetMandatoryReceiveAudio(true);
3634 answer_c.SetMandatoryReceiveVideo(true);
3635
3636 cricket::MediaSessionOptions answer_options;
3637 EXPECT_TRUE(ParseConstraintsForAnswer(&answer_c, &answer_options));
3638 EXPECT_TRUE(answer_options.has_audio());
3639 EXPECT_TRUE(answer_options.has_video());
3640
deadbeefc80741f2015-10-22 13:14:45 -07003641 RTCOfferAnswerOptions rtc_offer_options;
deadbeefab9b2d12015-10-14 11:33:11 -07003642
3643 cricket::MediaSessionOptions offer_options;
htaaac2dea2016-03-10 13:35:55 -08003644 EXPECT_TRUE(
3645 ExtractMediaSessionOptions(rtc_offer_options, false, &offer_options));
deadbeefc80741f2015-10-22 13:14:45 -07003646 EXPECT_TRUE(offer_options.has_audio());
htaaac2dea2016-03-10 13:35:55 -08003647 EXPECT_TRUE(offer_options.has_video());
deadbeefab9b2d12015-10-14 11:33:11 -07003648
deadbeefc80741f2015-10-22 13:14:45 -07003649 RTCOfferAnswerOptions updated_rtc_offer_options;
3650 updated_rtc_offer_options.offer_to_receive_audio = 1;
3651 updated_rtc_offer_options.offer_to_receive_video = 1;
deadbeefab9b2d12015-10-14 11:33:11 -07003652
3653 cricket::MediaSessionOptions updated_offer_options;
htaaac2dea2016-03-10 13:35:55 -08003654 EXPECT_TRUE(ExtractMediaSessionOptions(updated_rtc_offer_options, false,
htaa2a49d92016-03-04 02:51:39 -08003655 &updated_offer_options));
deadbeefab9b2d12015-10-14 11:33:11 -07003656 EXPECT_TRUE(updated_offer_options.has_audio());
3657 EXPECT_TRUE(updated_offer_options.has_video());
3658
3659 // Since an offer has been created with both audio and video, subsequent
3660 // offers and answers should contain both audio and video.
3661 // Answers will only contain the media types that exist in the offer
3662 // regardless of the value of |updated_answer_options.has_audio| and
3663 // |updated_answer_options.has_video|.
3664 FakeConstraints updated_answer_c;
3665 answer_c.SetMandatoryReceiveAudio(false);
3666 answer_c.SetMandatoryReceiveVideo(false);
3667
3668 cricket::MediaSessionOptions updated_answer_options;
3669 EXPECT_TRUE(
3670 ParseConstraintsForAnswer(&updated_answer_c, &updated_answer_options));
3671 EXPECT_TRUE(updated_answer_options.has_audio());
3672 EXPECT_TRUE(updated_answer_options.has_video());
deadbeefab9b2d12015-10-14 11:33:11 -07003673}
deadbeef3edec7c2016-12-10 11:44:26 -08003674
deadbeef293e9262017-01-11 12:28:30 -08003675// Tests a few random fields being different.
3676TEST(RTCConfigurationTest, ComparisonOperators) {
3677 PeerConnectionInterface::RTCConfiguration a;
3678 PeerConnectionInterface::RTCConfiguration b;
3679 EXPECT_EQ(a, b);
3680
3681 PeerConnectionInterface::RTCConfiguration c;
3682 c.servers.push_back(PeerConnectionInterface::IceServer());
3683 EXPECT_NE(a, c);
3684
3685 PeerConnectionInterface::RTCConfiguration d;
3686 d.type = PeerConnectionInterface::kRelay;
3687 EXPECT_NE(a, d);
3688
3689 PeerConnectionInterface::RTCConfiguration e;
3690 e.audio_jitter_buffer_max_packets = 5;
3691 EXPECT_NE(a, e);
3692
3693 PeerConnectionInterface::RTCConfiguration f;
3694 f.ice_connection_receiving_timeout = 1337;
3695 EXPECT_NE(a, f);
3696
3697 PeerConnectionInterface::RTCConfiguration g;
3698 g.disable_ipv6 = true;
3699 EXPECT_NE(a, g);
3700
3701 PeerConnectionInterface::RTCConfiguration h(
3702 PeerConnectionInterface::RTCConfigurationType::kAggressive);
3703 EXPECT_NE(a, h);
3704}