blob: 565704e935cc677cf6aebeb5640626fae468ee0d [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"
kjellandera96e2d72016-02-04 23:52:28 -080024#include "webrtc/media/base/fakevideocapturer.h"
zhihuang38ede132017-06-15 12:52:32 -070025#include "webrtc/media/engine/webrtcmediaengine.h"
deadbeef953c2ce2017-01-09 14:53:41 -080026#include "webrtc/media/sctp/sctptransportinternal.h"
peaha9cc40b2017-06-29 08:32:09 -070027#include "webrtc/modules/audio_processing/include/audio_processing.h"
Taylor Brandstettera1c30352016-05-13 08:15:11 -070028#include "webrtc/p2p/base/fakeportallocator.h"
ossu7bb87ee2017-01-23 04:56:25 -080029#include "webrtc/pc/audiotrack.h"
kjellander@webrtc.org9b8df252016-02-12 06:47:59 +010030#include "webrtc/pc/mediasession.h"
ossu7bb87ee2017-01-23 04:56:25 -080031#include "webrtc/pc/mediastream.h"
32#include "webrtc/pc/peerconnection.h"
33#include "webrtc/pc/streamcollection.h"
deadbeefd7850b22017-08-23 10:59:19 -070034#include "webrtc/pc/test/fakeaudiocapturemodule.h"
ossu7bb87ee2017-01-23 04:56:25 -080035#include "webrtc/pc/test/fakertccertificategenerator.h"
36#include "webrtc/pc/test/fakevideotracksource.h"
37#include "webrtc/pc/test/mockpeerconnectionobservers.h"
38#include "webrtc/pc/test/testsdpstrings.h"
39#include "webrtc/pc/videocapturertracksource.h"
40#include "webrtc/pc/videotrack.h"
Edward Lemurc20978e2017-07-06 19:44:34 +020041#include "webrtc/rtc_base/gunit.h"
42#include "webrtc/rtc_base/ssladapter.h"
43#include "webrtc/rtc_base/sslstreamadapter.h"
44#include "webrtc/rtc_base/stringutils.h"
45#include "webrtc/rtc_base/thread.h"
46#include "webrtc/rtc_base/virtualsocketserver.h"
kwibergac9f8762016-09-30 22:29:43 -070047#include "webrtc/test/gmock.h"
48
49#ifdef WEBRTC_ANDROID
ossu7bb87ee2017-01-23 04:56:25 -080050#include "webrtc/pc/test/androidtestinitializer.h"
kwibergac9f8762016-09-30 22:29:43 -070051#endif
henrike@webrtc.org28e20752013-07-10 00:45:36 +000052
53static const char kStreamLabel1[] = "local_stream_1";
54static const char kStreamLabel2[] = "local_stream_2";
55static const char kStreamLabel3[] = "local_stream_3";
56static const int kDefaultStunPort = 3478;
57static const char kStunAddressOnly[] = "stun:address";
58static const char kStunInvalidPort[] = "stun:address:-1";
59static const char kStunAddressPortAndMore1[] = "stun:address:port:more";
60static const char kStunAddressPortAndMore2[] = "stun:address:port more";
61static const char kTurnIceServerUri[] = "turn:user@turn.example.org";
62static const char kTurnUsername[] = "user";
63static const char kTurnPassword[] = "password";
64static const char kTurnHostname[] = "turn.example.org";
Peter Boström0c4e06b2015-10-07 12:23:21 +020065static const uint32_t kTimeout = 10000U;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000066
deadbeefab9b2d12015-10-14 11:33:11 -070067static const char kStreams[][8] = {"stream1", "stream2"};
68static const char kAudioTracks[][32] = {"audiotrack0", "audiotrack1"};
69static const char kVideoTracks[][32] = {"videotrack0", "videotrack1"};
70
deadbeef5e97fb52015-10-15 12:49:08 -070071static const char kRecvonly[] = "recvonly";
72static const char kSendrecv[] = "sendrecv";
73
deadbeefab9b2d12015-10-14 11:33:11 -070074// Reference SDP with a MediaStream with label "stream1" and audio track with
75// id "audio_1" and a video track with id "video_1;
76static const char kSdpStringWithStream1[] =
77 "v=0\r\n"
78 "o=- 0 0 IN IP4 127.0.0.1\r\n"
79 "s=-\r\n"
80 "t=0 0\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -080081 "m=audio 1 RTP/AVPF 103\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -070082 "a=ice-ufrag:e5785931\r\n"
83 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
84 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
85 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -070086 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -070087 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -080088 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -070089 "a=rtpmap:103 ISAC/16000\r\n"
90 "a=ssrc:1 cname:stream1\r\n"
91 "a=ssrc:1 mslabel:stream1\r\n"
92 "a=ssrc:1 label:audiotrack0\r\n"
93 "m=video 1 RTP/AVPF 120\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -080094 "a=ice-ufrag:e5785931\r\n"
95 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
96 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
97 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -070098 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -070099 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800100 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700101 "a=rtpmap:120 VP8/90000\r\n"
102 "a=ssrc:2 cname:stream1\r\n"
103 "a=ssrc:2 mslabel:stream1\r\n"
104 "a=ssrc:2 label:videotrack0\r\n";
105
zhihuang81c3a032016-11-17 12:06:24 -0800106// Reference SDP with a MediaStream with label "stream1" and audio track with
107// id "audio_1";
108static const char kSdpStringWithStream1AudioTrackOnly[] =
109 "v=0\r\n"
110 "o=- 0 0 IN IP4 127.0.0.1\r\n"
111 "s=-\r\n"
112 "t=0 0\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800113 "m=audio 1 RTP/AVPF 103\r\n"
zhihuang81c3a032016-11-17 12:06:24 -0800114 "a=ice-ufrag:e5785931\r\n"
115 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
116 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
117 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
zhihuang81c3a032016-11-17 12:06:24 -0800118 "a=mid:audio\r\n"
119 "a=sendrecv\r\n"
120 "a=rtpmap:103 ISAC/16000\r\n"
121 "a=ssrc:1 cname:stream1\r\n"
122 "a=ssrc:1 mslabel:stream1\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800123 "a=ssrc:1 label:audiotrack0\r\n"
124 "a=rtcp-mux\r\n";
zhihuang81c3a032016-11-17 12:06:24 -0800125
deadbeefab9b2d12015-10-14 11:33:11 -0700126// Reference SDP with two MediaStreams with label "stream1" and "stream2. Each
127// MediaStreams have one audio track and one video track.
128// This uses MSID.
129static const char kSdpStringWithStream1And2[] =
130 "v=0\r\n"
131 "o=- 0 0 IN IP4 127.0.0.1\r\n"
132 "s=-\r\n"
133 "t=0 0\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800134 "a=msid-semantic: WMS stream1 stream2\r\n"
135 "m=audio 1 RTP/AVPF 103\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700136 "a=ice-ufrag:e5785931\r\n"
137 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
138 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
139 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700140 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700141 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800142 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700143 "a=rtpmap:103 ISAC/16000\r\n"
144 "a=ssrc:1 cname:stream1\r\n"
145 "a=ssrc:1 msid:stream1 audiotrack0\r\n"
146 "a=ssrc:3 cname:stream2\r\n"
147 "a=ssrc:3 msid:stream2 audiotrack1\r\n"
148 "m=video 1 RTP/AVPF 120\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800149 "a=ice-ufrag:e5785931\r\n"
150 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
151 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
152 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700153 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700154 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800155 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700156 "a=rtpmap:120 VP8/0\r\n"
157 "a=ssrc:2 cname:stream1\r\n"
158 "a=ssrc:2 msid:stream1 videotrack0\r\n"
159 "a=ssrc:4 cname:stream2\r\n"
160 "a=ssrc:4 msid:stream2 videotrack1\r\n";
161
162// Reference SDP without MediaStreams. Msid is not supported.
163static const char kSdpStringWithoutStreams[] =
164 "v=0\r\n"
165 "o=- 0 0 IN IP4 127.0.0.1\r\n"
166 "s=-\r\n"
167 "t=0 0\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800168 "m=audio 1 RTP/AVPF 103\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700169 "a=ice-ufrag:e5785931\r\n"
170 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
171 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
172 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700173 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700174 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800175 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700176 "a=rtpmap:103 ISAC/16000\r\n"
177 "m=video 1 RTP/AVPF 120\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800178 "a=ice-ufrag:e5785931\r\n"
179 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
180 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
181 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700182 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700183 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800184 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700185 "a=rtpmap:120 VP8/90000\r\n";
186
187// Reference SDP without MediaStreams. Msid is supported.
188static const char kSdpStringWithMsidWithoutStreams[] =
189 "v=0\r\n"
190 "o=- 0 0 IN IP4 127.0.0.1\r\n"
191 "s=-\r\n"
192 "t=0 0\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800193 "a=msid-semantic: WMS\r\n"
194 "m=audio 1 RTP/AVPF 103\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700195 "a=ice-ufrag:e5785931\r\n"
196 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
197 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
198 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700199 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700200 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800201 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700202 "a=rtpmap:103 ISAC/16000\r\n"
203 "m=video 1 RTP/AVPF 120\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800204 "a=ice-ufrag:e5785931\r\n"
205 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
206 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
207 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700208 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700209 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800210 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700211 "a=rtpmap:120 VP8/90000\r\n";
212
213// Reference SDP without MediaStreams and audio only.
214static const char kSdpStringWithoutStreamsAudioOnly[] =
215 "v=0\r\n"
216 "o=- 0 0 IN IP4 127.0.0.1\r\n"
217 "s=-\r\n"
218 "t=0 0\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800219 "m=audio 1 RTP/AVPF 103\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700220 "a=ice-ufrag:e5785931\r\n"
221 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
222 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
223 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700224 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700225 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800226 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700227 "a=rtpmap:103 ISAC/16000\r\n";
228
229// Reference SENDONLY SDP without MediaStreams. Msid is not supported.
230static const char kSdpStringSendOnlyWithoutStreams[] =
231 "v=0\r\n"
232 "o=- 0 0 IN IP4 127.0.0.1\r\n"
233 "s=-\r\n"
234 "t=0 0\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800235 "m=audio 1 RTP/AVPF 103\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700236 "a=ice-ufrag:e5785931\r\n"
237 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
238 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
239 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700240 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700241 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700242 "a=sendonly\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800243 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700244 "a=rtpmap:103 ISAC/16000\r\n"
245 "m=video 1 RTP/AVPF 120\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800246 "a=ice-ufrag:e5785931\r\n"
247 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
248 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
249 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700250 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700251 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700252 "a=sendonly\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800253 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700254 "a=rtpmap:120 VP8/90000\r\n";
255
256static const char kSdpStringInit[] =
257 "v=0\r\n"
258 "o=- 0 0 IN IP4 127.0.0.1\r\n"
259 "s=-\r\n"
260 "t=0 0\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700261 "a=msid-semantic: WMS\r\n";
262
263static const char kSdpStringAudio[] =
264 "m=audio 1 RTP/AVPF 103\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800265 "a=ice-ufrag:e5785931\r\n"
266 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
267 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
268 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700269 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700270 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800271 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700272 "a=rtpmap:103 ISAC/16000\r\n";
273
274static const char kSdpStringVideo[] =
275 "m=video 1 RTP/AVPF 120\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800276 "a=ice-ufrag:e5785931\r\n"
277 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
278 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
279 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700280 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700281 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800282 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700283 "a=rtpmap:120 VP8/90000\r\n";
284
285static const char kSdpStringMs1Audio0[] =
286 "a=ssrc:1 cname:stream1\r\n"
287 "a=ssrc:1 msid:stream1 audiotrack0\r\n";
288
289static const char kSdpStringMs1Video0[] =
290 "a=ssrc:2 cname:stream1\r\n"
291 "a=ssrc:2 msid:stream1 videotrack0\r\n";
292
293static const char kSdpStringMs1Audio1[] =
294 "a=ssrc:3 cname:stream1\r\n"
295 "a=ssrc:3 msid:stream1 audiotrack1\r\n";
296
297static const char kSdpStringMs1Video1[] =
298 "a=ssrc:4 cname:stream1\r\n"
299 "a=ssrc:4 msid:stream1 videotrack1\r\n";
300
deadbeef8662f942017-01-20 21:20:51 -0800301static const char kDtlsSdesFallbackSdp[] =
302 "v=0\r\n"
303 "o=xxxxxx 7 2 IN IP4 0.0.0.0\r\n"
304 "s=-\r\n"
305 "c=IN IP4 0.0.0.0\r\n"
306 "t=0 0\r\n"
307 "a=group:BUNDLE audio\r\n"
308 "a=msid-semantic: WMS\r\n"
309 "m=audio 1 RTP/SAVPF 0\r\n"
310 "a=sendrecv\r\n"
311 "a=rtcp-mux\r\n"
312 "a=mid:audio\r\n"
313 "a=ssrc:1 cname:stream1\r\n"
314 "a=ssrc:1 mslabel:stream1\r\n"
315 "a=ssrc:1 label:audiotrack0\r\n"
316 "a=ice-ufrag:e5785931\r\n"
317 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
318 "a=rtpmap:0 pcmu/8000\r\n"
319 "a=fingerprint:sha-1 "
320 "4A:AD:B9:B1:3F:82:18:3B:54:02:12:DF:3E:5D:49:6B:19:E5:7C:AB\r\n"
321 "a=setup:actpass\r\n"
322 "a=crypto:1 AES_CM_128_HMAC_SHA1_32 "
323 "inline:NzB4d1BINUAvLEw6UzF3WSJ+PSdFcGdUJShpX1Zj|2^20|1:32 "
324 "dummy_session_params\r\n";
325
perkjd61bf802016-03-24 03:16:19 -0700326using ::testing::Exactly;
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700327using cricket::StreamParams;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000328using webrtc::AudioSourceInterface;
deadbeefab9b2d12015-10-14 11:33:11 -0700329using webrtc::AudioTrack;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000330using webrtc::AudioTrackInterface;
331using webrtc::DataBuffer;
332using webrtc::DataChannelInterface;
333using webrtc::FakeConstraints;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000334using webrtc::IceCandidateInterface;
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],
perkj773be362017-07-31 23:22:01 -0700463 webrtc::FakeVideoTrackSource::Create(),
464 rtc::Thread::Current()));
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700465 stream->AddTrack(video_track);
466 }
deadbeefab9b2d12015-10-14 11:33:11 -0700467
468 local_collection->AddStream(stream);
469 }
470 return local_collection;
471}
472
473// Check equality of StreamCollections.
474bool CompareStreamCollections(StreamCollectionInterface* s1,
475 StreamCollectionInterface* s2) {
476 if (s1 == nullptr || s2 == nullptr || s1->count() != s2->count()) {
477 return false;
478 }
479
480 for (size_t i = 0; i != s1->count(); ++i) {
481 if (s1->at(i)->label() != s2->at(i)->label()) {
482 return false;
483 }
484 webrtc::AudioTrackVector audio_tracks1 = s1->at(i)->GetAudioTracks();
485 webrtc::AudioTrackVector audio_tracks2 = s2->at(i)->GetAudioTracks();
486 webrtc::VideoTrackVector video_tracks1 = s1->at(i)->GetVideoTracks();
487 webrtc::VideoTrackVector video_tracks2 = s2->at(i)->GetVideoTracks();
488
489 if (audio_tracks1.size() != audio_tracks2.size()) {
490 return false;
491 }
492 for (size_t j = 0; j != audio_tracks1.size(); ++j) {
493 if (audio_tracks1[j]->id() != audio_tracks2[j]->id()) {
494 return false;
495 }
496 }
497 if (video_tracks1.size() != video_tracks2.size()) {
498 return false;
499 }
500 for (size_t j = 0; j != video_tracks1.size(); ++j) {
501 if (video_tracks1[j]->id() != video_tracks2[j]->id()) {
502 return false;
503 }
504 }
505 }
506 return true;
507}
508
perkjd61bf802016-03-24 03:16:19 -0700509// Helper class to test Observer.
510class MockTrackObserver : public ObserverInterface {
511 public:
512 explicit MockTrackObserver(NotifierInterface* notifier)
513 : notifier_(notifier) {
514 notifier_->RegisterObserver(this);
515 }
516
517 ~MockTrackObserver() { Unregister(); }
518
519 void Unregister() {
520 if (notifier_) {
521 notifier_->UnregisterObserver(this);
522 notifier_ = nullptr;
523 }
524 }
525
526 MOCK_METHOD0(OnChanged, void());
527
528 private:
529 NotifierInterface* notifier_;
530};
531
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000532class MockPeerConnectionObserver : public PeerConnectionObserver {
533 public:
deadbeefab9b2d12015-10-14 11:33:11 -0700534 MockPeerConnectionObserver() : remote_streams_(StreamCollection::Create()) {}
Henrik Kjellander3fe372d2016-05-12 08:10:52 +0200535 virtual ~MockPeerConnectionObserver() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000536 }
537 void SetPeerConnectionInterface(PeerConnectionInterface* pc) {
538 pc_ = pc;
539 if (pc) {
540 state_ = pc_->signaling_state();
541 }
542 }
nisseef8b61e2016-04-29 06:09:15 -0700543 void OnSignalingChange(
544 PeerConnectionInterface::SignalingState new_state) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000545 EXPECT_EQ(pc_->signaling_state(), new_state);
546 state_ = new_state;
547 }
deadbeefab9b2d12015-10-14 11:33:11 -0700548
549 MediaStreamInterface* RemoteStream(const std::string& label) {
550 return remote_streams_->find(label);
551 }
552 StreamCollectionInterface* remote_streams() const { return remote_streams_; }
Taylor Brandstetter98cde262016-05-31 13:02:21 -0700553 void OnAddStream(rtc::scoped_refptr<MediaStreamInterface> stream) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000554 last_added_stream_ = stream;
deadbeefab9b2d12015-10-14 11:33:11 -0700555 remote_streams_->AddStream(stream);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000556 }
Taylor Brandstetter98cde262016-05-31 13:02:21 -0700557 void OnRemoveStream(
558 rtc::scoped_refptr<MediaStreamInterface> stream) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000559 last_removed_stream_ = stream;
deadbeefab9b2d12015-10-14 11:33:11 -0700560 remote_streams_->RemoveStream(stream);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000561 }
perkjdfb769d2016-02-09 03:09:43 -0800562 void OnRenegotiationNeeded() override { renegotiation_needed_ = true; }
Taylor Brandstetter98cde262016-05-31 13:02:21 -0700563 void OnDataChannel(
564 rtc::scoped_refptr<DataChannelInterface> data_channel) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000565 last_datachannel_ = data_channel;
566 }
567
perkjdfb769d2016-02-09 03:09:43 -0800568 void OnIceConnectionChange(
569 PeerConnectionInterface::IceConnectionState new_state) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000570 EXPECT_EQ(pc_->ice_connection_state(), new_state);
zhihuang81c3a032016-11-17 12:06:24 -0800571 callback_triggered_ = true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000572 }
perkjdfb769d2016-02-09 03:09:43 -0800573 void OnIceGatheringChange(
574 PeerConnectionInterface::IceGatheringState new_state) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000575 EXPECT_EQ(pc_->ice_gathering_state(), new_state);
perkjdfb769d2016-02-09 03:09:43 -0800576 ice_complete_ = new_state == PeerConnectionInterface::kIceGatheringComplete;
zhihuang81c3a032016-11-17 12:06:24 -0800577 callback_triggered_ = true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000578 }
perkjdfb769d2016-02-09 03:09:43 -0800579 void OnIceCandidate(const webrtc::IceCandidateInterface* candidate) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000580 EXPECT_NE(PeerConnectionInterface::kIceGatheringNew,
581 pc_->ice_gathering_state());
582
583 std::string sdp;
584 EXPECT_TRUE(candidate->ToString(&sdp));
585 EXPECT_LT(0u, sdp.size());
586 last_candidate_.reset(webrtc::CreateIceCandidate(candidate->sdp_mid(),
587 candidate->sdp_mline_index(), sdp, NULL));
588 EXPECT_TRUE(last_candidate_.get() != NULL);
zhihuang81c3a032016-11-17 12:06:24 -0800589 callback_triggered_ = true;
zhihuang29ff8442016-07-27 11:07:25 -0700590 }
591
592 void OnIceCandidatesRemoved(
593 const std::vector<cricket::Candidate>& candidates) override {
zhihuang81c3a032016-11-17 12:06:24 -0800594 callback_triggered_ = true;
zhihuang29ff8442016-07-27 11:07:25 -0700595 }
596
597 void OnIceConnectionReceivingChange(bool receiving) override {
zhihuang81c3a032016-11-17 12:06:24 -0800598 callback_triggered_ = true;
599 }
600
zhihuangc63b8942016-12-02 15:41:10 -0800601 void OnAddTrack(
602 rtc::scoped_refptr<webrtc::RtpReceiverInterface> receiver,
603 const std::vector<rtc::scoped_refptr<webrtc::MediaStreamInterface>>&
604 streams) override {
zhihuang81c3a032016-11-17 12:06:24 -0800605 EXPECT_TRUE(receiver != nullptr);
606 num_added_tracks_++;
607 last_added_track_label_ = receiver->id();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000608 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000609
610 // Returns the label of the last added stream.
611 // Empty string if no stream have been added.
612 std::string GetLastAddedStreamLabel() {
613 if (last_added_stream_.get())
614 return last_added_stream_->label();
615 return "";
616 }
617 std::string GetLastRemovedStreamLabel() {
618 if (last_removed_stream_.get())
619 return last_removed_stream_->label();
620 return "";
621 }
622
zhihuang9763d562016-08-05 11:14:50 -0700623 rtc::scoped_refptr<PeerConnectionInterface> pc_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000624 PeerConnectionInterface::SignalingState state_;
kwibergd1fe2812016-04-27 06:47:29 -0700625 std::unique_ptr<IceCandidateInterface> last_candidate_;
zhihuang9763d562016-08-05 11:14:50 -0700626 rtc::scoped_refptr<DataChannelInterface> last_datachannel_;
deadbeefab9b2d12015-10-14 11:33:11 -0700627 rtc::scoped_refptr<StreamCollection> remote_streams_;
628 bool renegotiation_needed_ = false;
629 bool ice_complete_ = false;
zhihuang81c3a032016-11-17 12:06:24 -0800630 bool callback_triggered_ = false;
631 int num_added_tracks_ = 0;
632 std::string last_added_track_label_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000633
634 private:
zhihuang9763d562016-08-05 11:14:50 -0700635 rtc::scoped_refptr<MediaStreamInterface> last_added_stream_;
636 rtc::scoped_refptr<MediaStreamInterface> last_removed_stream_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000637};
638
639} // namespace
deadbeefab9b2d12015-10-14 11:33:11 -0700640
nisse528b7932017-05-08 03:21:43 -0700641// The PeerConnectionMediaConfig tests below verify that configuration and
642// constraints are propagated into the PeerConnection's MediaConfig. These
643// settings are intended for MediaChannel constructors, but that is not
644// exercised by these unittest.
zhihuang29ff8442016-07-27 11:07:25 -0700645class PeerConnectionFactoryForTest : public webrtc::PeerConnectionFactory {
646 public:
zhihuang38ede132017-06-15 12:52:32 -0700647 static rtc::scoped_refptr<PeerConnectionFactoryForTest>
648 CreatePeerConnectionFactoryForTest() {
649 auto audio_encoder_factory = webrtc::CreateBuiltinAudioEncoderFactory();
650 auto audio_decoder_factory = webrtc::CreateBuiltinAudioDecoderFactory();
651
652 auto media_engine = std::unique_ptr<cricket::MediaEngineInterface>(
653 cricket::WebRtcMediaEngineFactory::Create(
654 nullptr, audio_encoder_factory, audio_decoder_factory, nullptr,
peaha9cc40b2017-06-29 08:32:09 -0700655 nullptr, nullptr, webrtc::AudioProcessing::Create()));
zhihuang38ede132017-06-15 12:52:32 -0700656
657 std::unique_ptr<webrtc::CallFactoryInterface> call_factory =
658 webrtc::CreateCallFactory();
659
660 std::unique_ptr<webrtc::RtcEventLogFactoryInterface> event_log_factory =
661 webrtc::CreateRtcEventLogFactory();
662
663 return new rtc::RefCountedObject<PeerConnectionFactoryForTest>(
664 rtc::Thread::Current(), rtc::Thread::Current(), rtc::Thread::Current(),
deadbeefd7850b22017-08-23 10:59:19 -0700665 FakeAudioCaptureModule::Create(), audio_encoder_factory,
666 audio_decoder_factory, nullptr, nullptr, nullptr,
667 std::move(media_engine), std::move(call_factory),
zhihuang38ede132017-06-15 12:52:32 -0700668 std::move(event_log_factory));
669 }
670
671 PeerConnectionFactoryForTest(
672 rtc::Thread* network_thread,
673 rtc::Thread* worker_thread,
674 rtc::Thread* signaling_thread,
deadbeefd7850b22017-08-23 10:59:19 -0700675 rtc::scoped_refptr<FakeAudioCaptureModule> fake_adm,
zhihuang38ede132017-06-15 12:52:32 -0700676 rtc::scoped_refptr<webrtc::AudioEncoderFactory> audio_encoder_factory,
677 rtc::scoped_refptr<webrtc::AudioDecoderFactory> audio_decoder_factory,
678 cricket::WebRtcVideoEncoderFactory* video_encoder_factory,
679 cricket::WebRtcVideoDecoderFactory* video_decoder_factory,
680 rtc::scoped_refptr<webrtc::AudioMixer> audio_mixer,
681 std::unique_ptr<cricket::MediaEngineInterface> media_engine,
682 std::unique_ptr<webrtc::CallFactoryInterface> call_factory,
683 std::unique_ptr<webrtc::RtcEventLogFactoryInterface> event_log_factory)
684 : webrtc::PeerConnectionFactory(network_thread,
685 worker_thread,
686 signaling_thread,
deadbeefd7850b22017-08-23 10:59:19 -0700687 fake_adm,
zhihuang38ede132017-06-15 12:52:32 -0700688 audio_encoder_factory,
689 audio_decoder_factory,
690 video_encoder_factory,
691 video_decoder_factory,
692 audio_mixer,
693 std::move(media_engine),
694 std::move(call_factory),
695 std::move(event_log_factory)) {}
kwiberg1e4e8cb2017-01-31 01:48:08 -0800696
zhihuang29ff8442016-07-27 11:07:25 -0700697 cricket::TransportController* CreateTransportController(
Honghai Zhangbfd398c2016-08-30 22:07:42 -0700698 cricket::PortAllocator* port_allocator,
699 bool redetermine_role_on_ice_restart) override {
zhihuang29ff8442016-07-27 11:07:25 -0700700 transport_controller = new cricket::TransportController(
Honghai Zhangbfd398c2016-08-30 22:07:42 -0700701 rtc::Thread::Current(), rtc::Thread::Current(), port_allocator,
deadbeef7914b8c2017-04-21 03:23:33 -0700702 redetermine_role_on_ice_restart, rtc::CryptoOptions());
zhihuang29ff8442016-07-27 11:07:25 -0700703 return transport_controller;
704 }
705
deadbeefd7850b22017-08-23 10:59:19 -0700706 rtc::scoped_refptr<FakeAudioCaptureModule> fake_audio_capture_module_;
zhihuang29ff8442016-07-27 11:07:25 -0700707 cricket::TransportController* transport_controller;
zhihuang29ff8442016-07-27 11:07:25 -0700708};
709
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000710class PeerConnectionInterfaceTest : public testing::Test {
711 protected:
deadbeef9a6f4d42017-05-15 19:43:33 -0700712 PeerConnectionInterfaceTest()
deadbeef98e186c2017-05-16 18:00:06 -0700713 : vss_(new rtc::VirtualSocketServer()), main_(vss_.get()) {
phoglund37ebcf02016-01-08 05:04:57 -0800714#ifdef WEBRTC_ANDROID
715 webrtc::InitializeAndroidObjects();
716#endif
717 }
718
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000719 virtual void SetUp() {
deadbeefd7850b22017-08-23 10:59:19 -0700720 // Use fake audio capture module since we're only testing the interface
721 // level, and using a real one could make tests flaky when run in parallel.
722 fake_audio_capture_module_ = FakeAudioCaptureModule::Create();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000723 pc_factory_ = webrtc::CreatePeerConnectionFactory(
danilchape9021a32016-05-17 01:52:02 -0700724 rtc::Thread::Current(), rtc::Thread::Current(), rtc::Thread::Current(),
deadbeefd7850b22017-08-23 10:59:19 -0700725 fake_audio_capture_module_, nullptr, nullptr);
danilchape9021a32016-05-17 01:52:02 -0700726 ASSERT_TRUE(pc_factory_);
zhihuang29ff8442016-07-27 11:07:25 -0700727 pc_factory_for_test_ =
zhihuang38ede132017-06-15 12:52:32 -0700728 PeerConnectionFactoryForTest::CreatePeerConnectionFactoryForTest();
zhihuang29ff8442016-07-27 11:07:25 -0700729 pc_factory_for_test_->Initialize();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000730 }
731
732 void CreatePeerConnection() {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700733 CreatePeerConnection(PeerConnectionInterface::RTCConfiguration(), nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000734 }
735
deadbeef293e9262017-01-11 12:28:30 -0800736 // DTLS does not work in a loopback call, so is disabled for most of the
737 // tests in this file.
738 void CreatePeerConnectionWithoutDtls() {
739 FakeConstraints no_dtls_constraints;
740 no_dtls_constraints.AddMandatory(
741 webrtc::MediaConstraintsInterface::kEnableDtlsSrtp, false);
742
743 CreatePeerConnection(PeerConnectionInterface::RTCConfiguration(),
744 &no_dtls_constraints);
745 }
746
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000747 void CreatePeerConnection(webrtc::MediaConstraintsInterface* constraints) {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700748 CreatePeerConnection(PeerConnectionInterface::RTCConfiguration(),
749 constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000750 }
751
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700752 void CreatePeerConnectionWithIceTransportsType(
753 PeerConnectionInterface::IceTransportsType type) {
754 PeerConnectionInterface::RTCConfiguration config;
755 config.type = type;
756 return CreatePeerConnection(config, nullptr);
757 }
758
759 void CreatePeerConnectionWithIceServer(const std::string& uri,
760 const std::string& password) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800761 PeerConnectionInterface::RTCConfiguration config;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000762 PeerConnectionInterface::IceServer server;
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700763 server.uri = uri;
764 server.password = password;
765 config.servers.push_back(server);
766 CreatePeerConnection(config, nullptr);
767 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000768
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700769 void CreatePeerConnection(PeerConnectionInterface::RTCConfiguration config,
770 webrtc::MediaConstraintsInterface* constraints) {
kwibergd1fe2812016-04-27 06:47:29 -0700771 std::unique_ptr<cricket::FakePortAllocator> port_allocator(
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800772 new cricket::FakePortAllocator(rtc::Thread::Current(), nullptr));
773 port_allocator_ = port_allocator.get();
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000774
deadbeef1dcb1642017-03-29 21:08:16 -0700775 // Create certificate generator unless DTLS constraint is explicitly set to
776 // false.
Henrik Boströmd79599d2016-06-01 13:58:50 +0200777 std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator;
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000778 bool dtls;
779 if (FindConstraint(constraints,
780 webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
781 &dtls,
Henrik Boström5e56c592015-08-11 10:33:13 +0200782 nullptr) && dtls) {
deadbeef8662f942017-01-20 21:20:51 -0800783 fake_certificate_generator_ = new FakeRTCCertificateGenerator();
784 cert_generator.reset(fake_certificate_generator_);
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000785 }
Henrik Boströmd79599d2016-06-01 13:58:50 +0200786 pc_ = pc_factory_->CreatePeerConnection(
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800787 config, constraints, std::move(port_allocator),
Henrik Boströmd79599d2016-06-01 13:58:50 +0200788 std::move(cert_generator), &observer_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000789 ASSERT_TRUE(pc_.get() != NULL);
790 observer_.SetPeerConnectionInterface(pc_.get());
791 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
792 }
793
deadbeef0a6c4ca2015-10-06 11:38:28 -0700794 void CreatePeerConnectionExpectFail(const std::string& uri) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800795 PeerConnectionInterface::RTCConfiguration config;
deadbeef0a6c4ca2015-10-06 11:38:28 -0700796 PeerConnectionInterface::IceServer server;
deadbeef0a6c4ca2015-10-06 11:38:28 -0700797 server.uri = uri;
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800798 config.servers.push_back(server);
deadbeef0a6c4ca2015-10-06 11:38:28 -0700799
zhihuang9763d562016-08-05 11:14:50 -0700800 rtc::scoped_refptr<PeerConnectionInterface> pc;
hbosd7973cc2016-05-27 06:08:53 -0700801 pc = pc_factory_->CreatePeerConnection(config, nullptr, nullptr, nullptr,
802 &observer_);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800803 EXPECT_EQ(nullptr, pc);
deadbeef0a6c4ca2015-10-06 11:38:28 -0700804 }
805
Steve Anton038834f2017-07-14 15:59:59 -0700806 void CreatePeerConnectionExpectFail(
807 PeerConnectionInterface::RTCConfiguration config) {
808 PeerConnectionInterface::IceServer server;
809 server.uri = kTurnIceServerUri;
810 server.password = kTurnPassword;
811 config.servers.push_back(server);
812 rtc::scoped_refptr<PeerConnectionInterface> pc =
813 pc_factory_->CreatePeerConnection(config, nullptr, nullptr, &observer_);
814 EXPECT_EQ(nullptr, pc);
815 }
816
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000817 void CreatePeerConnectionWithDifferentConfigurations() {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700818 CreatePeerConnectionWithIceServer(kStunAddressOnly, "");
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800819 EXPECT_EQ(1u, port_allocator_->stun_servers().size());
820 EXPECT_EQ(0u, port_allocator_->turn_servers().size());
821 EXPECT_EQ("address", port_allocator_->stun_servers().begin()->hostname());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000822 EXPECT_EQ(kDefaultStunPort,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800823 port_allocator_->stun_servers().begin()->port());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000824
deadbeef0a6c4ca2015-10-06 11:38:28 -0700825 CreatePeerConnectionExpectFail(kStunInvalidPort);
826 CreatePeerConnectionExpectFail(kStunAddressPortAndMore1);
827 CreatePeerConnectionExpectFail(kStunAddressPortAndMore2);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000828
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700829 CreatePeerConnectionWithIceServer(kTurnIceServerUri, kTurnPassword);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800830 EXPECT_EQ(0u, port_allocator_->stun_servers().size());
831 EXPECT_EQ(1u, port_allocator_->turn_servers().size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000832 EXPECT_EQ(kTurnUsername,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800833 port_allocator_->turn_servers()[0].credentials.username);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000834 EXPECT_EQ(kTurnPassword,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800835 port_allocator_->turn_servers()[0].credentials.password);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000836 EXPECT_EQ(kTurnHostname,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800837 port_allocator_->turn_servers()[0].ports[0].address.hostname());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000838 }
839
840 void ReleasePeerConnection() {
841 pc_ = NULL;
842 observer_.SetPeerConnectionInterface(NULL);
843 }
844
deadbeefab9b2d12015-10-14 11:33:11 -0700845 void AddVideoStream(const std::string& label) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000846 // Create a local stream.
zhihuang9763d562016-08-05 11:14:50 -0700847 rtc::scoped_refptr<MediaStreamInterface> stream(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000848 pc_factory_->CreateLocalMediaStream(label));
zhihuang9763d562016-08-05 11:14:50 -0700849 rtc::scoped_refptr<VideoTrackSourceInterface> video_source(
deadbeef112b2e92017-02-10 20:13:37 -0800850 pc_factory_->CreateVideoSource(std::unique_ptr<cricket::VideoCapturer>(
851 new cricket::FakeVideoCapturer()),
852 NULL));
zhihuang9763d562016-08-05 11:14:50 -0700853 rtc::scoped_refptr<VideoTrackInterface> video_track(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000854 pc_factory_->CreateVideoTrack(label + "v0", video_source));
855 stream->AddTrack(video_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +0000856 EXPECT_TRUE(pc_->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000857 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
858 observer_.renegotiation_needed_ = false;
859 }
860
861 void AddVoiceStream(const std::string& label) {
862 // Create a local stream.
zhihuang9763d562016-08-05 11:14:50 -0700863 rtc::scoped_refptr<MediaStreamInterface> stream(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000864 pc_factory_->CreateLocalMediaStream(label));
zhihuang9763d562016-08-05 11:14:50 -0700865 rtc::scoped_refptr<AudioTrackInterface> audio_track(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000866 pc_factory_->CreateAudioTrack(label + "a0", NULL));
867 stream->AddTrack(audio_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +0000868 EXPECT_TRUE(pc_->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000869 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
870 observer_.renegotiation_needed_ = false;
871 }
872
873 void AddAudioVideoStream(const std::string& stream_label,
874 const std::string& audio_track_label,
875 const std::string& video_track_label) {
876 // Create a local stream.
zhihuang9763d562016-08-05 11:14:50 -0700877 rtc::scoped_refptr<MediaStreamInterface> stream(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000878 pc_factory_->CreateLocalMediaStream(stream_label));
zhihuang9763d562016-08-05 11:14:50 -0700879 rtc::scoped_refptr<AudioTrackInterface> audio_track(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000880 pc_factory_->CreateAudioTrack(
881 audio_track_label, static_cast<AudioSourceInterface*>(NULL)));
882 stream->AddTrack(audio_track.get());
zhihuang9763d562016-08-05 11:14:50 -0700883 rtc::scoped_refptr<VideoTrackInterface> video_track(
nisseaf510af2016-03-21 08:20:42 -0700884 pc_factory_->CreateVideoTrack(
deadbeef112b2e92017-02-10 20:13:37 -0800885 video_track_label, pc_factory_->CreateVideoSource(
886 std::unique_ptr<cricket::VideoCapturer>(
887 new cricket::FakeVideoCapturer()))));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000888 stream->AddTrack(video_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +0000889 EXPECT_TRUE(pc_->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000890 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
891 observer_.renegotiation_needed_ = false;
892 }
893
kwibergd1fe2812016-04-27 06:47:29 -0700894 bool DoCreateOfferAnswer(std::unique_ptr<SessionDescriptionInterface>* desc,
deadbeefc80741f2015-10-22 13:14:45 -0700895 bool offer,
896 MediaConstraintsInterface* constraints) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000897 rtc::scoped_refptr<MockCreateSessionDescriptionObserver>
898 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000899 MockCreateSessionDescriptionObserver>());
900 if (offer) {
deadbeefc80741f2015-10-22 13:14:45 -0700901 pc_->CreateOffer(observer, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000902 } else {
deadbeefc80741f2015-10-22 13:14:45 -0700903 pc_->CreateAnswer(observer, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000904 }
905 EXPECT_EQ_WAIT(true, observer->called(), kTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -0700906 *desc = observer->MoveDescription();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000907 return observer->result();
908 }
909
kwibergd1fe2812016-04-27 06:47:29 -0700910 bool DoCreateOffer(std::unique_ptr<SessionDescriptionInterface>* desc,
deadbeefc80741f2015-10-22 13:14:45 -0700911 MediaConstraintsInterface* constraints) {
912 return DoCreateOfferAnswer(desc, true, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000913 }
914
kwibergd1fe2812016-04-27 06:47:29 -0700915 bool DoCreateAnswer(std::unique_ptr<SessionDescriptionInterface>* desc,
deadbeefc80741f2015-10-22 13:14:45 -0700916 MediaConstraintsInterface* constraints) {
917 return DoCreateOfferAnswer(desc, false, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000918 }
919
Steve Antondb45ca82017-09-11 18:27:34 -0700920 bool DoSetSessionDescription(
921 std::unique_ptr<SessionDescriptionInterface> desc,
922 bool local) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000923 rtc::scoped_refptr<MockSetSessionDescriptionObserver>
924 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000925 MockSetSessionDescriptionObserver>());
926 if (local) {
Steve Antondb45ca82017-09-11 18:27:34 -0700927 pc_->SetLocalDescription(observer, desc.release());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000928 } else {
Steve Antondb45ca82017-09-11 18:27:34 -0700929 pc_->SetRemoteDescription(observer, desc.release());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000930 }
zhihuang29ff8442016-07-27 11:07:25 -0700931 if (pc_->signaling_state() != PeerConnectionInterface::kClosed) {
932 EXPECT_EQ_WAIT(true, observer->called(), kTimeout);
933 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000934 return observer->result();
935 }
936
Steve Antondb45ca82017-09-11 18:27:34 -0700937 bool DoSetLocalDescription(
938 std::unique_ptr<SessionDescriptionInterface> desc) {
939 return DoSetSessionDescription(std::move(desc), true);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000940 }
941
Steve Antondb45ca82017-09-11 18:27:34 -0700942 bool DoSetRemoteDescription(
943 std::unique_ptr<SessionDescriptionInterface> desc) {
944 return DoSetSessionDescription(std::move(desc), false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000945 }
946
947 // Calls PeerConnection::GetStats and check the return value.
948 // It does not verify the values in the StatReports since a RTCP packet might
949 // be required.
950 bool DoGetStats(MediaStreamTrackInterface* track) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000951 rtc::scoped_refptr<MockStatsObserver> observer(
952 new rtc::RefCountedObject<MockStatsObserver>());
jiayl@webrtc.orgdb41b4d2014-03-03 21:30:06 +0000953 if (!pc_->GetStats(
954 observer, track, PeerConnectionInterface::kStatsOutputLevelStandard))
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000955 return false;
956 EXPECT_TRUE_WAIT(observer->called(), kTimeout);
957 return observer->called();
958 }
959
960 void InitiateCall() {
deadbeef293e9262017-01-11 12:28:30 -0800961 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000962 // Create a local stream with audio&video tracks.
963 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
964 CreateOfferReceiveAnswer();
965 }
966
967 // Verify that RTP Header extensions has been negotiated for audio and video.
968 void VerifyRemoteRtpHeaderExtensions() {
969 const cricket::MediaContentDescription* desc =
970 cricket::GetFirstAudioContentDescription(
971 pc_->remote_description()->description());
972 ASSERT_TRUE(desc != NULL);
973 EXPECT_GT(desc->rtp_header_extensions().size(), 0u);
974
975 desc = cricket::GetFirstVideoContentDescription(
976 pc_->remote_description()->description());
977 ASSERT_TRUE(desc != NULL);
978 EXPECT_GT(desc->rtp_header_extensions().size(), 0u);
979 }
980
981 void CreateOfferAsRemoteDescription() {
kwibergd1fe2812016-04-27 06:47:29 -0700982 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -0700983 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000984 std::string sdp;
985 EXPECT_TRUE(offer->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -0700986 std::unique_ptr<SessionDescriptionInterface> remote_offer(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000987 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
Steve Antondb45ca82017-09-11 18:27:34 -0700988 sdp, nullptr));
989 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_offer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000990 EXPECT_EQ(PeerConnectionInterface::kHaveRemoteOffer, observer_.state_);
991 }
992
deadbeefab9b2d12015-10-14 11:33:11 -0700993 void CreateAndSetRemoteOffer(const std::string& sdp) {
Steve Antondb45ca82017-09-11 18:27:34 -0700994 std::unique_ptr<SessionDescriptionInterface> remote_offer(
deadbeefab9b2d12015-10-14 11:33:11 -0700995 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
Steve Antondb45ca82017-09-11 18:27:34 -0700996 sdp, nullptr));
997 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_offer)));
deadbeefab9b2d12015-10-14 11:33:11 -0700998 EXPECT_EQ(PeerConnectionInterface::kHaveRemoteOffer, observer_.state_);
999 }
1000
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001001 void CreateAnswerAsLocalDescription() {
kwibergd1fe2812016-04-27 06:47:29 -07001002 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -07001003 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001004
1005 // TODO(perkj): Currently SetLocalDescription fails if any parameters in an
1006 // audio codec change, even if the parameter has nothing to do with
1007 // receiving. Not all parameters are serialized to SDP.
1008 // Since CreatePrAnswerAsLocalDescription serialize/deserialize
1009 // the SessionDescription, it is necessary to do that here to in order to
1010 // get ReceiveOfferCreatePrAnswerAndAnswer and RenegotiateAudioOnly to pass.
1011 // https://code.google.com/p/webrtc/issues/detail?id=1356
1012 std::string sdp;
1013 EXPECT_TRUE(answer->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07001014 std::unique_ptr<SessionDescriptionInterface> new_answer(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001015 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
Steve Antondb45ca82017-09-11 18:27:34 -07001016 sdp, nullptr));
1017 EXPECT_TRUE(DoSetLocalDescription(std::move(new_answer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001018 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
1019 }
1020
1021 void CreatePrAnswerAsLocalDescription() {
kwibergd1fe2812016-04-27 06:47:29 -07001022 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -07001023 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001024
1025 std::string sdp;
1026 EXPECT_TRUE(answer->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07001027 std::unique_ptr<SessionDescriptionInterface> pr_answer(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001028 webrtc::CreateSessionDescription(SessionDescriptionInterface::kPrAnswer,
Steve Antondb45ca82017-09-11 18:27:34 -07001029 sdp, nullptr));
1030 EXPECT_TRUE(DoSetLocalDescription(std::move(pr_answer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001031 EXPECT_EQ(PeerConnectionInterface::kHaveLocalPrAnswer, observer_.state_);
1032 }
1033
1034 void CreateOfferReceiveAnswer() {
1035 CreateOfferAsLocalDescription();
1036 std::string sdp;
1037 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
1038 CreateAnswerAsRemoteDescription(sdp);
1039 }
1040
1041 void CreateOfferAsLocalDescription() {
kwibergd1fe2812016-04-27 06:47:29 -07001042 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001043 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001044 // TODO(perkj): Currently SetLocalDescription fails if any parameters in an
1045 // audio codec change, even if the parameter has nothing to do with
1046 // receiving. Not all parameters are serialized to SDP.
1047 // Since CreatePrAnswerAsLocalDescription serialize/deserialize
1048 // the SessionDescription, it is necessary to do that here to in order to
1049 // get ReceiveOfferCreatePrAnswerAndAnswer and RenegotiateAudioOnly to pass.
1050 // https://code.google.com/p/webrtc/issues/detail?id=1356
1051 std::string sdp;
1052 EXPECT_TRUE(offer->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07001053 std::unique_ptr<SessionDescriptionInterface> new_offer(
1054 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
1055 sdp, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001056
Steve Antondb45ca82017-09-11 18:27:34 -07001057 EXPECT_TRUE(DoSetLocalDescription(std::move(new_offer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001058 EXPECT_EQ(PeerConnectionInterface::kHaveLocalOffer, observer_.state_);
mallinath@webrtc.org68cbd012014-01-22 00:16:46 +00001059 // Wait for the ice_complete message, so that SDP will have candidates.
1060 EXPECT_TRUE_WAIT(observer_.ice_complete_, kTimeout);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001061 }
1062
deadbeefab9b2d12015-10-14 11:33:11 -07001063 void CreateAnswerAsRemoteDescription(const std::string& sdp) {
Steve Antondb45ca82017-09-11 18:27:34 -07001064 std::unique_ptr<SessionDescriptionInterface> answer(
1065 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
1066 sdp, nullptr));
1067 ASSERT_TRUE(answer);
1068 EXPECT_TRUE(DoSetRemoteDescription(std::move(answer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001069 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
1070 }
1071
deadbeefab9b2d12015-10-14 11:33:11 -07001072 void CreatePrAnswerAndAnswerAsRemoteDescription(const std::string& sdp) {
Steve Antondb45ca82017-09-11 18:27:34 -07001073 std::unique_ptr<SessionDescriptionInterface> pr_answer(
1074 webrtc::CreateSessionDescription(SessionDescriptionInterface::kPrAnswer,
1075 sdp, nullptr));
1076 ASSERT_TRUE(pr_answer);
1077 EXPECT_TRUE(DoSetRemoteDescription(std::move(pr_answer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001078 EXPECT_EQ(PeerConnectionInterface::kHaveRemotePrAnswer, observer_.state_);
Steve Antondb45ca82017-09-11 18:27:34 -07001079 std::unique_ptr<SessionDescriptionInterface> answer(
1080 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
1081 sdp, nullptr));
1082 ASSERT_TRUE(answer);
1083 EXPECT_TRUE(DoSetRemoteDescription(std::move(answer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001084 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
1085 }
1086
1087 // Help function used for waiting until a the last signaled remote stream has
1088 // the same label as |stream_label|. In a few of the tests in this file we
1089 // answer with the same session description as we offer and thus we can
1090 // check if OnAddStream have been called with the same stream as we offer to
1091 // send.
1092 void WaitAndVerifyOnAddStream(const std::string& stream_label) {
1093 EXPECT_EQ_WAIT(stream_label, observer_.GetLastAddedStreamLabel(), kTimeout);
1094 }
1095
1096 // Creates an offer and applies it as a local session description.
1097 // Creates an answer with the same SDP an the offer but removes all lines
1098 // that start with a:ssrc"
1099 void CreateOfferReceiveAnswerWithoutSsrc() {
1100 CreateOfferAsLocalDescription();
1101 std::string sdp;
1102 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
1103 SetSsrcToZero(&sdp);
1104 CreateAnswerAsRemoteDescription(sdp);
1105 }
1106
deadbeefab9b2d12015-10-14 11:33:11 -07001107 // This function creates a MediaStream with label kStreams[0] and
1108 // |number_of_audio_tracks| and |number_of_video_tracks| tracks and the
1109 // corresponding SessionDescriptionInterface. The SessionDescriptionInterface
kwiberg2bbff992016-03-16 11:03:04 -07001110 // is returned and the MediaStream is stored in
deadbeefab9b2d12015-10-14 11:33:11 -07001111 // |reference_collection_|
kwibergd1fe2812016-04-27 06:47:29 -07001112 std::unique_ptr<SessionDescriptionInterface>
kwiberg2bbff992016-03-16 11:03:04 -07001113 CreateSessionDescriptionAndReference(size_t number_of_audio_tracks,
1114 size_t number_of_video_tracks) {
1115 EXPECT_LE(number_of_audio_tracks, 2u);
1116 EXPECT_LE(number_of_video_tracks, 2u);
deadbeefab9b2d12015-10-14 11:33:11 -07001117
1118 reference_collection_ = StreamCollection::Create();
1119 std::string sdp_ms1 = std::string(kSdpStringInit);
1120
1121 std::string mediastream_label = kStreams[0];
1122
1123 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream(
1124 webrtc::MediaStream::Create(mediastream_label));
1125 reference_collection_->AddStream(stream);
1126
1127 if (number_of_audio_tracks > 0) {
1128 sdp_ms1 += std::string(kSdpStringAudio);
1129 sdp_ms1 += std::string(kSdpStringMs1Audio0);
1130 AddAudioTrack(kAudioTracks[0], stream);
1131 }
1132 if (number_of_audio_tracks > 1) {
1133 sdp_ms1 += kSdpStringMs1Audio1;
1134 AddAudioTrack(kAudioTracks[1], stream);
1135 }
1136
1137 if (number_of_video_tracks > 0) {
1138 sdp_ms1 += std::string(kSdpStringVideo);
1139 sdp_ms1 += std::string(kSdpStringMs1Video0);
1140 AddVideoTrack(kVideoTracks[0], stream);
1141 }
1142 if (number_of_video_tracks > 1) {
1143 sdp_ms1 += kSdpStringMs1Video1;
1144 AddVideoTrack(kVideoTracks[1], stream);
1145 }
1146
kwibergd1fe2812016-04-27 06:47:29 -07001147 return std::unique_ptr<SessionDescriptionInterface>(
kwiberg2bbff992016-03-16 11:03:04 -07001148 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
1149 sdp_ms1, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07001150 }
1151
1152 void AddAudioTrack(const std::string& track_id,
1153 MediaStreamInterface* stream) {
1154 rtc::scoped_refptr<webrtc::AudioTrackInterface> audio_track(
1155 webrtc::AudioTrack::Create(track_id, nullptr));
1156 ASSERT_TRUE(stream->AddTrack(audio_track));
1157 }
1158
1159 void AddVideoTrack(const std::string& track_id,
1160 MediaStreamInterface* stream) {
1161 rtc::scoped_refptr<webrtc::VideoTrackInterface> video_track(
nisseaf510af2016-03-21 08:20:42 -07001162 webrtc::VideoTrack::Create(track_id,
perkj773be362017-07-31 23:22:01 -07001163 webrtc::FakeVideoTrackSource::Create(),
1164 rtc::Thread::Current()));
deadbeefab9b2d12015-10-14 11:33:11 -07001165 ASSERT_TRUE(stream->AddTrack(video_track));
1166 }
1167
kwibergfd8be342016-05-14 19:44:11 -07001168 std::unique_ptr<SessionDescriptionInterface> CreateOfferWithOneAudioStream() {
deadbeef293e9262017-01-11 12:28:30 -08001169 CreatePeerConnectionWithoutDtls();
zhihuang8f65cdf2016-05-06 18:40:30 -07001170 AddVoiceStream(kStreamLabel1);
kwibergfd8be342016-05-14 19:44:11 -07001171 std::unique_ptr<SessionDescriptionInterface> offer;
zhihuang8f65cdf2016-05-06 18:40:30 -07001172 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
1173 return offer;
1174 }
1175
kwibergfd8be342016-05-14 19:44:11 -07001176 std::unique_ptr<SessionDescriptionInterface>
zhihuang8f65cdf2016-05-06 18:40:30 -07001177 CreateAnswerWithOneAudioStream() {
Steve Antondb45ca82017-09-11 18:27:34 -07001178 EXPECT_TRUE(DoSetRemoteDescription(CreateOfferWithOneAudioStream()));
kwibergfd8be342016-05-14 19:44:11 -07001179 std::unique_ptr<SessionDescriptionInterface> answer;
zhihuang8f65cdf2016-05-06 18:40:30 -07001180 EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
1181 return answer;
1182 }
1183
1184 const std::string& GetFirstAudioStreamCname(
1185 const SessionDescriptionInterface* desc) {
1186 const cricket::ContentInfo* audio_content =
1187 cricket::GetFirstAudioContent(desc->description());
1188 const cricket::AudioContentDescription* audio_desc =
1189 static_cast<const cricket::AudioContentDescription*>(
1190 audio_content->description);
1191 return audio_desc->streams()[0].cname;
1192 }
1193
zhihuang1c378ed2017-08-17 14:10:50 -07001194 std::unique_ptr<SessionDescriptionInterface> CreateOfferWithOptions(
1195 const RTCOfferAnswerOptions& offer_answer_options) {
1196 RTC_DCHECK(pc_);
1197 rtc::scoped_refptr<MockCreateSessionDescriptionObserver> observer(
1198 new rtc::RefCountedObject<MockCreateSessionDescriptionObserver>());
1199 pc_->CreateOffer(observer, offer_answer_options);
1200 EXPECT_EQ_WAIT(true, observer->called(), kTimeout);
1201 return observer->MoveDescription();
1202 }
1203
1204 void CreateOfferWithOptionsAsRemoteDescription(
1205 std::unique_ptr<SessionDescriptionInterface>* desc,
1206 const RTCOfferAnswerOptions& offer_answer_options) {
1207 *desc = CreateOfferWithOptions(offer_answer_options);
1208 ASSERT_TRUE(desc != nullptr);
1209 std::string sdp;
1210 EXPECT_TRUE((*desc)->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07001211 std::unique_ptr<SessionDescriptionInterface> remote_offer(
zhihuang1c378ed2017-08-17 14:10:50 -07001212 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
Steve Antondb45ca82017-09-11 18:27:34 -07001213 sdp, nullptr));
1214 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_offer)));
zhihuang1c378ed2017-08-17 14:10:50 -07001215 EXPECT_EQ(PeerConnectionInterface::kHaveRemoteOffer, observer_.state_);
1216 }
1217
1218 void CreateOfferWithOptionsAsLocalDescription(
1219 std::unique_ptr<SessionDescriptionInterface>* desc,
1220 const RTCOfferAnswerOptions& offer_answer_options) {
1221 *desc = CreateOfferWithOptions(offer_answer_options);
1222 ASSERT_TRUE(desc != nullptr);
1223 std::string sdp;
1224 EXPECT_TRUE((*desc)->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07001225 std::unique_ptr<SessionDescriptionInterface> new_offer(
1226 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
1227 sdp, nullptr));
zhihuang1c378ed2017-08-17 14:10:50 -07001228
Steve Antondb45ca82017-09-11 18:27:34 -07001229 EXPECT_TRUE(DoSetLocalDescription(std::move(new_offer)));
zhihuang1c378ed2017-08-17 14:10:50 -07001230 EXPECT_EQ(PeerConnectionInterface::kHaveLocalOffer, observer_.state_);
1231 }
1232
1233 bool HasCNCodecs(const cricket::ContentInfo* content) {
1234 const cricket::ContentDescription* description = content->description;
1235 RTC_DCHECK(description);
1236 const cricket::AudioContentDescription* audio_content_desc =
1237 static_cast<const cricket::AudioContentDescription*>(description);
1238 RTC_DCHECK(audio_content_desc);
1239 for (size_t i = 0; i < audio_content_desc->codecs().size(); ++i) {
1240 if (audio_content_desc->codecs()[i].name == "CN")
1241 return true;
1242 }
1243 return false;
1244 }
1245
deadbeef9a6f4d42017-05-15 19:43:33 -07001246 std::unique_ptr<rtc::VirtualSocketServer> vss_;
1247 rtc::AutoSocketServerThread main_;
deadbeefd7850b22017-08-23 10:59:19 -07001248 rtc::scoped_refptr<FakeAudioCaptureModule> fake_audio_capture_module_;
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08001249 cricket::FakePortAllocator* port_allocator_ = nullptr;
deadbeef8662f942017-01-20 21:20:51 -08001250 FakeRTCCertificateGenerator* fake_certificate_generator_ = nullptr;
zhihuang9763d562016-08-05 11:14:50 -07001251 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface> pc_factory_;
1252 rtc::scoped_refptr<PeerConnectionFactoryForTest> pc_factory_for_test_;
1253 rtc::scoped_refptr<PeerConnectionInterface> pc_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001254 MockPeerConnectionObserver observer_;
deadbeefab9b2d12015-10-14 11:33:11 -07001255 rtc::scoped_refptr<StreamCollection> reference_collection_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001256};
1257
zhihuang29ff8442016-07-27 11:07:25 -07001258// Test that no callbacks on the PeerConnectionObserver are called after the
1259// PeerConnection is closed.
1260TEST_F(PeerConnectionInterfaceTest, CloseAndTestCallbackFunctions) {
zhihuang9763d562016-08-05 11:14:50 -07001261 rtc::scoped_refptr<PeerConnectionInterface> pc(
zhihuang29ff8442016-07-27 11:07:25 -07001262 pc_factory_for_test_->CreatePeerConnection(
1263 PeerConnectionInterface::RTCConfiguration(), nullptr, nullptr,
1264 nullptr, &observer_));
1265 observer_.SetPeerConnectionInterface(pc.get());
1266 pc->Close();
1267
1268 // No callbacks is expected to be called.
zhihuang81c3a032016-11-17 12:06:24 -08001269 observer_.callback_triggered_ = false;
zhihuang29ff8442016-07-27 11:07:25 -07001270 std::vector<cricket::Candidate> candidates;
1271 pc_factory_for_test_->transport_controller->SignalGatheringState(
1272 cricket::IceGatheringState{});
1273 pc_factory_for_test_->transport_controller->SignalCandidatesGathered(
1274 "", candidates);
1275 pc_factory_for_test_->transport_controller->SignalConnectionState(
1276 cricket::IceConnectionState{});
1277 pc_factory_for_test_->transport_controller->SignalCandidatesRemoved(
1278 candidates);
1279 pc_factory_for_test_->transport_controller->SignalReceiving(false);
zhihuang81c3a032016-11-17 12:06:24 -08001280 EXPECT_FALSE(observer_.callback_triggered_);
zhihuang29ff8442016-07-27 11:07:25 -07001281}
1282
zhihuang8f65cdf2016-05-06 18:40:30 -07001283// Generate different CNAMEs when PeerConnections are created.
1284// The CNAMEs are expected to be generated randomly. It is possible
1285// that the test fails, though the possibility is very low.
1286TEST_F(PeerConnectionInterfaceTest, CnameGenerationInOffer) {
kwibergfd8be342016-05-14 19:44:11 -07001287 std::unique_ptr<SessionDescriptionInterface> offer1 =
zhihuang8f65cdf2016-05-06 18:40:30 -07001288 CreateOfferWithOneAudioStream();
kwibergfd8be342016-05-14 19:44:11 -07001289 std::unique_ptr<SessionDescriptionInterface> offer2 =
zhihuang8f65cdf2016-05-06 18:40:30 -07001290 CreateOfferWithOneAudioStream();
1291 EXPECT_NE(GetFirstAudioStreamCname(offer1.get()),
1292 GetFirstAudioStreamCname(offer2.get()));
1293}
1294
1295TEST_F(PeerConnectionInterfaceTest, CnameGenerationInAnswer) {
kwibergfd8be342016-05-14 19:44:11 -07001296 std::unique_ptr<SessionDescriptionInterface> answer1 =
zhihuang8f65cdf2016-05-06 18:40:30 -07001297 CreateAnswerWithOneAudioStream();
kwibergfd8be342016-05-14 19:44:11 -07001298 std::unique_ptr<SessionDescriptionInterface> answer2 =
zhihuang8f65cdf2016-05-06 18:40:30 -07001299 CreateAnswerWithOneAudioStream();
1300 EXPECT_NE(GetFirstAudioStreamCname(answer1.get()),
1301 GetFirstAudioStreamCname(answer2.get()));
1302}
1303
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001304TEST_F(PeerConnectionInterfaceTest,
1305 CreatePeerConnectionWithDifferentConfigurations) {
1306 CreatePeerConnectionWithDifferentConfigurations();
1307}
1308
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001309TEST_F(PeerConnectionInterfaceTest,
1310 CreatePeerConnectionWithDifferentIceTransportsTypes) {
1311 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kNone);
1312 EXPECT_EQ(cricket::CF_NONE, port_allocator_->candidate_filter());
1313 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kRelay);
1314 EXPECT_EQ(cricket::CF_RELAY, port_allocator_->candidate_filter());
1315 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kNoHost);
1316 EXPECT_EQ(cricket::CF_ALL & ~cricket::CF_HOST,
1317 port_allocator_->candidate_filter());
1318 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kAll);
1319 EXPECT_EQ(cricket::CF_ALL, port_allocator_->candidate_filter());
1320}
1321
1322// Test that when a PeerConnection is created with a nonzero candidate pool
1323// size, the pooled PortAllocatorSession is created with all the attributes
1324// in the RTCConfiguration.
1325TEST_F(PeerConnectionInterfaceTest, CreatePeerConnectionWithPooledCandidates) {
1326 PeerConnectionInterface::RTCConfiguration config;
1327 PeerConnectionInterface::IceServer server;
1328 server.uri = kStunAddressOnly;
1329 config.servers.push_back(server);
1330 config.type = PeerConnectionInterface::kRelay;
1331 config.disable_ipv6 = true;
1332 config.tcp_candidate_policy =
1333 PeerConnectionInterface::kTcpCandidatePolicyDisabled;
honghaiz60347052016-05-31 18:29:12 -07001334 config.candidate_network_policy =
1335 PeerConnectionInterface::kCandidateNetworkPolicyLowCost;
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001336 config.ice_candidate_pool_size = 1;
1337 CreatePeerConnection(config, nullptr);
1338
1339 const cricket::FakePortAllocatorSession* session =
1340 static_cast<const cricket::FakePortAllocatorSession*>(
1341 port_allocator_->GetPooledSession());
1342 ASSERT_NE(nullptr, session);
1343 EXPECT_EQ(1UL, session->stun_servers().size());
1344 EXPECT_EQ(0U, session->flags() & cricket::PORTALLOCATOR_ENABLE_IPV6);
1345 EXPECT_LT(0U, session->flags() & cricket::PORTALLOCATOR_DISABLE_TCP);
honghaiz60347052016-05-31 18:29:12 -07001346 EXPECT_LT(0U,
1347 session->flags() & cricket::PORTALLOCATOR_DISABLE_COSTLY_NETWORKS);
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001348}
1349
deadbeefd21eab32017-07-26 16:50:11 -07001350// Test that network-related RTCConfiguration members are applied to the
1351// PortAllocator when CreatePeerConnection is called. Specifically:
1352// - disable_ipv6_on_wifi
1353// - max_ipv6_networks
1354// - tcp_candidate_policy
1355// - candidate_network_policy
1356// - prune_turn_ports
1357//
1358// Note that the candidate filter (RTCConfiguration::type) is already tested
1359// above.
1360TEST_F(PeerConnectionInterfaceTest,
1361 CreatePeerConnectionAppliesNetworkConfigToPortAllocator) {
1362 // Create fake port allocator.
1363 std::unique_ptr<cricket::FakePortAllocator> port_allocator(
1364 new cricket::FakePortAllocator(rtc::Thread::Current(), nullptr));
1365 cricket::FakePortAllocator* raw_port_allocator = port_allocator.get();
1366
1367 // Create RTCConfiguration with some network-related fields relevant to
1368 // PortAllocator populated.
1369 PeerConnectionInterface::RTCConfiguration config;
1370 config.disable_ipv6_on_wifi = true;
1371 config.max_ipv6_networks = 10;
1372 config.tcp_candidate_policy =
1373 PeerConnectionInterface::kTcpCandidatePolicyDisabled;
1374 config.candidate_network_policy =
1375 PeerConnectionInterface::kCandidateNetworkPolicyLowCost;
1376 config.prune_turn_ports = true;
1377
1378 // Create the PC factory and PC with the above config.
1379 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface> pc_factory(
1380 webrtc::CreatePeerConnectionFactory(
1381 rtc::Thread::Current(), rtc::Thread::Current(),
deadbeefd7850b22017-08-23 10:59:19 -07001382 rtc::Thread::Current(), fake_audio_capture_module_, nullptr,
1383 nullptr));
deadbeefd21eab32017-07-26 16:50:11 -07001384 rtc::scoped_refptr<PeerConnectionInterface> pc(
1385 pc_factory->CreatePeerConnection(
1386 config, nullptr, std::move(port_allocator), nullptr, &observer_));
1387
1388 // Now validate that the config fields set above were applied to the
1389 // PortAllocator, as flags or otherwise.
1390 EXPECT_FALSE(raw_port_allocator->flags() &
1391 cricket::PORTALLOCATOR_ENABLE_IPV6_ON_WIFI);
1392 EXPECT_EQ(10, raw_port_allocator->max_ipv6_networks());
1393 EXPECT_TRUE(raw_port_allocator->flags() & cricket::PORTALLOCATOR_DISABLE_TCP);
1394 EXPECT_TRUE(raw_port_allocator->flags() &
1395 cricket::PORTALLOCATOR_DISABLE_COSTLY_NETWORKS);
1396 EXPECT_TRUE(raw_port_allocator->prune_turn_ports());
1397}
1398
Taylor Brandstetterf8e65772016-06-27 17:20:15 -07001399// Test that the PeerConnection initializes the port allocator passed into it,
1400// and on the correct thread.
1401TEST_F(PeerConnectionInterfaceTest,
deadbeefd21eab32017-07-26 16:50:11 -07001402 CreatePeerConnectionInitializesPortAllocatorOnNetworkThread) {
tommie7251592017-07-14 14:44:46 -07001403 std::unique_ptr<rtc::Thread> network_thread(
1404 rtc::Thread::CreateWithSocketServer());
1405 network_thread->Start();
Taylor Brandstetterf8e65772016-06-27 17:20:15 -07001406 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface> pc_factory(
1407 webrtc::CreatePeerConnectionFactory(
tommie7251592017-07-14 14:44:46 -07001408 network_thread.get(), rtc::Thread::Current(), rtc::Thread::Current(),
deadbeefd7850b22017-08-23 10:59:19 -07001409 fake_audio_capture_module_, nullptr, nullptr));
Taylor Brandstetterf8e65772016-06-27 17:20:15 -07001410 std::unique_ptr<cricket::FakePortAllocator> port_allocator(
tommie7251592017-07-14 14:44:46 -07001411 new cricket::FakePortAllocator(network_thread.get(), nullptr));
Taylor Brandstetterf8e65772016-06-27 17:20:15 -07001412 cricket::FakePortAllocator* raw_port_allocator = port_allocator.get();
1413 PeerConnectionInterface::RTCConfiguration config;
1414 rtc::scoped_refptr<PeerConnectionInterface> pc(
1415 pc_factory->CreatePeerConnection(
1416 config, nullptr, std::move(port_allocator), nullptr, &observer_));
1417 // FakePortAllocator RTC_CHECKs that it's initialized on the right thread,
1418 // so all we have to do here is check that it's initialized.
1419 EXPECT_TRUE(raw_port_allocator->initialized());
1420}
1421
deadbeef46c73892016-11-16 19:42:04 -08001422// Check that GetConfiguration returns the configuration the PeerConnection was
1423// constructed with, before SetConfiguration is called.
1424TEST_F(PeerConnectionInterfaceTest, GetConfigurationAfterCreatePeerConnection) {
1425 PeerConnectionInterface::RTCConfiguration config;
1426 config.type = PeerConnectionInterface::kRelay;
1427 CreatePeerConnection(config, nullptr);
1428
1429 PeerConnectionInterface::RTCConfiguration returned_config =
1430 pc_->GetConfiguration();
1431 EXPECT_EQ(PeerConnectionInterface::kRelay, returned_config.type);
1432}
1433
1434// Check that GetConfiguration returns the last configuration passed into
1435// SetConfiguration.
1436TEST_F(PeerConnectionInterfaceTest, GetConfigurationAfterSetConfiguration) {
1437 CreatePeerConnection();
1438
1439 PeerConnectionInterface::RTCConfiguration config;
1440 config.type = PeerConnectionInterface::kRelay;
1441 EXPECT_TRUE(pc_->SetConfiguration(config));
1442
1443 PeerConnectionInterface::RTCConfiguration returned_config =
1444 pc_->GetConfiguration();
1445 EXPECT_EQ(PeerConnectionInterface::kRelay, returned_config.type);
1446}
1447
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001448TEST_F(PeerConnectionInterfaceTest, AddStreams) {
deadbeef293e9262017-01-11 12:28:30 -08001449 CreatePeerConnectionWithoutDtls();
deadbeefab9b2d12015-10-14 11:33:11 -07001450 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001451 AddVoiceStream(kStreamLabel2);
1452 ASSERT_EQ(2u, pc_->local_streams()->count());
1453
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001454 // Test we can add multiple local streams to one peerconnection.
zhihuang9763d562016-08-05 11:14:50 -07001455 rtc::scoped_refptr<MediaStreamInterface> stream(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001456 pc_factory_->CreateLocalMediaStream(kStreamLabel3));
zhihuang9763d562016-08-05 11:14:50 -07001457 rtc::scoped_refptr<AudioTrackInterface> audio_track(
1458 pc_factory_->CreateAudioTrack(kStreamLabel3,
1459 static_cast<AudioSourceInterface*>(NULL)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001460 stream->AddTrack(audio_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +00001461 EXPECT_TRUE(pc_->AddStream(stream));
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001462 EXPECT_EQ(3u, pc_->local_streams()->count());
1463
1464 // Remove the third stream.
1465 pc_->RemoveStream(pc_->local_streams()->at(2));
1466 EXPECT_EQ(2u, pc_->local_streams()->count());
1467
1468 // Remove the second stream.
1469 pc_->RemoveStream(pc_->local_streams()->at(1));
1470 EXPECT_EQ(1u, pc_->local_streams()->count());
1471
1472 // Remove the first stream.
1473 pc_->RemoveStream(pc_->local_streams()->at(0));
1474 EXPECT_EQ(0u, pc_->local_streams()->count());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001475}
1476
deadbeefab9b2d12015-10-14 11:33:11 -07001477// Test that the created offer includes streams we added.
1478TEST_F(PeerConnectionInterfaceTest, AddedStreamsPresentInOffer) {
deadbeef293e9262017-01-11 12:28:30 -08001479 CreatePeerConnectionWithoutDtls();
deadbeefab9b2d12015-10-14 11:33:11 -07001480 AddAudioVideoStream(kStreamLabel1, "audio_track", "video_track");
kwibergd1fe2812016-04-27 06:47:29 -07001481 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001482 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07001483
1484 const cricket::ContentInfo* audio_content =
1485 cricket::GetFirstAudioContent(offer->description());
1486 const cricket::AudioContentDescription* audio_desc =
1487 static_cast<const cricket::AudioContentDescription*>(
1488 audio_content->description);
1489 EXPECT_TRUE(
1490 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
1491
1492 const cricket::ContentInfo* video_content =
1493 cricket::GetFirstVideoContent(offer->description());
1494 const cricket::VideoContentDescription* video_desc =
1495 static_cast<const cricket::VideoContentDescription*>(
1496 video_content->description);
1497 EXPECT_TRUE(
1498 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1499
1500 // Add another stream and ensure the offer includes both the old and new
1501 // streams.
1502 AddAudioVideoStream(kStreamLabel2, "audio_track2", "video_track2");
kwiberg2bbff992016-03-16 11:03:04 -07001503 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07001504
1505 audio_content = cricket::GetFirstAudioContent(offer->description());
1506 audio_desc = static_cast<const cricket::AudioContentDescription*>(
1507 audio_content->description);
1508 EXPECT_TRUE(
1509 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
1510 EXPECT_TRUE(
1511 ContainsTrack(audio_desc->streams(), kStreamLabel2, "audio_track2"));
1512
1513 video_content = cricket::GetFirstVideoContent(offer->description());
1514 video_desc = static_cast<const cricket::VideoContentDescription*>(
1515 video_content->description);
1516 EXPECT_TRUE(
1517 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1518 EXPECT_TRUE(
1519 ContainsTrack(video_desc->streams(), kStreamLabel2, "video_track2"));
1520}
1521
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001522TEST_F(PeerConnectionInterfaceTest, RemoveStream) {
deadbeef293e9262017-01-11 12:28:30 -08001523 CreatePeerConnectionWithoutDtls();
deadbeefab9b2d12015-10-14 11:33:11 -07001524 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001525 ASSERT_EQ(1u, pc_->local_streams()->count());
1526 pc_->RemoveStream(pc_->local_streams()->at(0));
1527 EXPECT_EQ(0u, pc_->local_streams()->count());
1528}
1529
deadbeefe1f9d832016-01-14 15:35:42 -08001530// Test for AddTrack and RemoveTrack methods.
1531// Tests that the created offer includes tracks we added,
1532// and that the RtpSenders are created correctly.
1533// Also tests that RemoveTrack removes the tracks from subsequent offers.
1534TEST_F(PeerConnectionInterfaceTest, AddTrackRemoveTrack) {
deadbeef293e9262017-01-11 12:28:30 -08001535 CreatePeerConnectionWithoutDtls();
deadbeefe1f9d832016-01-14 15:35:42 -08001536 // Create a dummy stream, so tracks share a stream label.
zhihuang9763d562016-08-05 11:14:50 -07001537 rtc::scoped_refptr<MediaStreamInterface> stream(
deadbeefe1f9d832016-01-14 15:35:42 -08001538 pc_factory_->CreateLocalMediaStream(kStreamLabel1));
1539 std::vector<MediaStreamInterface*> stream_list;
1540 stream_list.push_back(stream.get());
zhihuang9763d562016-08-05 11:14:50 -07001541 rtc::scoped_refptr<AudioTrackInterface> audio_track(
deadbeefe1f9d832016-01-14 15:35:42 -08001542 pc_factory_->CreateAudioTrack("audio_track", nullptr));
zhihuang9763d562016-08-05 11:14:50 -07001543 rtc::scoped_refptr<VideoTrackInterface> video_track(
1544 pc_factory_->CreateVideoTrack(
deadbeef112b2e92017-02-10 20:13:37 -08001545 "video_track", pc_factory_->CreateVideoSource(
1546 std::unique_ptr<cricket::VideoCapturer>(
1547 new cricket::FakeVideoCapturer()))));
deadbeefe1f9d832016-01-14 15:35:42 -08001548 auto audio_sender = pc_->AddTrack(audio_track, stream_list);
1549 auto video_sender = pc_->AddTrack(video_track, stream_list);
deadbeefa601f5c2016-06-06 14:27:39 -07001550 EXPECT_EQ(1UL, audio_sender->stream_ids().size());
1551 EXPECT_EQ(kStreamLabel1, audio_sender->stream_ids()[0]);
deadbeefe1f9d832016-01-14 15:35:42 -08001552 EXPECT_EQ("audio_track", audio_sender->id());
1553 EXPECT_EQ(audio_track, audio_sender->track());
deadbeefa601f5c2016-06-06 14:27:39 -07001554 EXPECT_EQ(1UL, video_sender->stream_ids().size());
1555 EXPECT_EQ(kStreamLabel1, video_sender->stream_ids()[0]);
deadbeefe1f9d832016-01-14 15:35:42 -08001556 EXPECT_EQ("video_track", video_sender->id());
1557 EXPECT_EQ(video_track, video_sender->track());
1558
1559 // Now create an offer and check for the senders.
kwibergd1fe2812016-04-27 06:47:29 -07001560 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001561 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefe1f9d832016-01-14 15:35:42 -08001562
1563 const cricket::ContentInfo* audio_content =
1564 cricket::GetFirstAudioContent(offer->description());
1565 const cricket::AudioContentDescription* audio_desc =
1566 static_cast<const cricket::AudioContentDescription*>(
1567 audio_content->description);
1568 EXPECT_TRUE(
1569 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
1570
1571 const cricket::ContentInfo* video_content =
1572 cricket::GetFirstVideoContent(offer->description());
1573 const cricket::VideoContentDescription* video_desc =
1574 static_cast<const cricket::VideoContentDescription*>(
1575 video_content->description);
1576 EXPECT_TRUE(
1577 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1578
Steve Antondb45ca82017-09-11 18:27:34 -07001579 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefe1f9d832016-01-14 15:35:42 -08001580
1581 // Now try removing the tracks.
1582 EXPECT_TRUE(pc_->RemoveTrack(audio_sender));
1583 EXPECT_TRUE(pc_->RemoveTrack(video_sender));
1584
1585 // Create a new offer and ensure it doesn't contain the removed senders.
kwiberg2bbff992016-03-16 11:03:04 -07001586 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefe1f9d832016-01-14 15:35:42 -08001587
1588 audio_content = cricket::GetFirstAudioContent(offer->description());
1589 audio_desc = static_cast<const cricket::AudioContentDescription*>(
1590 audio_content->description);
1591 EXPECT_FALSE(
1592 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
1593
1594 video_content = cricket::GetFirstVideoContent(offer->description());
1595 video_desc = static_cast<const cricket::VideoContentDescription*>(
1596 video_content->description);
1597 EXPECT_FALSE(
1598 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1599
Steve Antondb45ca82017-09-11 18:27:34 -07001600 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefe1f9d832016-01-14 15:35:42 -08001601
1602 // Calling RemoveTrack on a sender no longer attached to a PeerConnection
1603 // should return false.
1604 EXPECT_FALSE(pc_->RemoveTrack(audio_sender));
1605 EXPECT_FALSE(pc_->RemoveTrack(video_sender));
1606}
1607
1608// Test creating senders without a stream specified,
1609// expecting a random stream ID to be generated.
1610TEST_F(PeerConnectionInterfaceTest, AddTrackWithoutStream) {
deadbeef293e9262017-01-11 12:28:30 -08001611 CreatePeerConnectionWithoutDtls();
deadbeefe1f9d832016-01-14 15:35:42 -08001612 // Create a dummy stream, so tracks share a stream label.
zhihuang9763d562016-08-05 11:14:50 -07001613 rtc::scoped_refptr<AudioTrackInterface> audio_track(
deadbeefe1f9d832016-01-14 15:35:42 -08001614 pc_factory_->CreateAudioTrack("audio_track", nullptr));
zhihuang9763d562016-08-05 11:14:50 -07001615 rtc::scoped_refptr<VideoTrackInterface> video_track(
1616 pc_factory_->CreateVideoTrack(
deadbeef112b2e92017-02-10 20:13:37 -08001617 "video_track", pc_factory_->CreateVideoSource(
1618 std::unique_ptr<cricket::VideoCapturer>(
1619 new cricket::FakeVideoCapturer()))));
deadbeefe1f9d832016-01-14 15:35:42 -08001620 auto audio_sender =
1621 pc_->AddTrack(audio_track, std::vector<MediaStreamInterface*>());
1622 auto video_sender =
1623 pc_->AddTrack(video_track, std::vector<MediaStreamInterface*>());
1624 EXPECT_EQ("audio_track", audio_sender->id());
1625 EXPECT_EQ(audio_track, audio_sender->track());
1626 EXPECT_EQ("video_track", video_sender->id());
1627 EXPECT_EQ(video_track, video_sender->track());
1628 // If the ID is truly a random GUID, it should be infinitely unlikely they
1629 // will be the same.
deadbeefa601f5c2016-06-06 14:27:39 -07001630 EXPECT_NE(video_sender->stream_ids(), audio_sender->stream_ids());
deadbeefe1f9d832016-01-14 15:35:42 -08001631}
1632
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001633TEST_F(PeerConnectionInterfaceTest, CreateOfferReceiveAnswer) {
1634 InitiateCall();
1635 WaitAndVerifyOnAddStream(kStreamLabel1);
1636 VerifyRemoteRtpHeaderExtensions();
1637}
1638
1639TEST_F(PeerConnectionInterfaceTest, CreateOfferReceivePrAnswerAndAnswer) {
deadbeef293e9262017-01-11 12:28:30 -08001640 CreatePeerConnectionWithoutDtls();
deadbeefab9b2d12015-10-14 11:33:11 -07001641 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001642 CreateOfferAsLocalDescription();
1643 std::string offer;
1644 EXPECT_TRUE(pc_->local_description()->ToString(&offer));
1645 CreatePrAnswerAndAnswerAsRemoteDescription(offer);
1646 WaitAndVerifyOnAddStream(kStreamLabel1);
1647}
1648
1649TEST_F(PeerConnectionInterfaceTest, ReceiveOfferCreateAnswer) {
deadbeef293e9262017-01-11 12:28:30 -08001650 CreatePeerConnectionWithoutDtls();
deadbeefab9b2d12015-10-14 11:33:11 -07001651 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001652
1653 CreateOfferAsRemoteDescription();
1654 CreateAnswerAsLocalDescription();
1655
1656 WaitAndVerifyOnAddStream(kStreamLabel1);
1657}
1658
1659TEST_F(PeerConnectionInterfaceTest, ReceiveOfferCreatePrAnswerAndAnswer) {
deadbeef293e9262017-01-11 12:28:30 -08001660 CreatePeerConnectionWithoutDtls();
deadbeefab9b2d12015-10-14 11:33:11 -07001661 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001662
1663 CreateOfferAsRemoteDescription();
1664 CreatePrAnswerAsLocalDescription();
1665 CreateAnswerAsLocalDescription();
1666
1667 WaitAndVerifyOnAddStream(kStreamLabel1);
1668}
1669
1670TEST_F(PeerConnectionInterfaceTest, Renegotiate) {
1671 InitiateCall();
1672 ASSERT_EQ(1u, pc_->remote_streams()->count());
1673 pc_->RemoveStream(pc_->local_streams()->at(0));
1674 CreateOfferReceiveAnswer();
1675 EXPECT_EQ(0u, pc_->remote_streams()->count());
deadbeefab9b2d12015-10-14 11:33:11 -07001676 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001677 CreateOfferReceiveAnswer();
1678}
1679
1680// Tests that after negotiating an audio only call, the respondent can perform a
1681// renegotiation that removes the audio stream.
1682TEST_F(PeerConnectionInterfaceTest, RenegotiateAudioOnly) {
deadbeef293e9262017-01-11 12:28:30 -08001683 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001684 AddVoiceStream(kStreamLabel1);
1685 CreateOfferAsRemoteDescription();
1686 CreateAnswerAsLocalDescription();
1687
1688 ASSERT_EQ(1u, pc_->remote_streams()->count());
1689 pc_->RemoveStream(pc_->local_streams()->at(0));
1690 CreateOfferReceiveAnswer();
1691 EXPECT_EQ(0u, pc_->remote_streams()->count());
1692}
1693
1694// Test that candidates are generated and that we can parse our own candidates.
1695TEST_F(PeerConnectionInterfaceTest, IceCandidates) {
deadbeef293e9262017-01-11 12:28:30 -08001696 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001697
1698 EXPECT_FALSE(pc_->AddIceCandidate(observer_.last_candidate_.get()));
1699 // SetRemoteDescription takes ownership of offer.
kwibergd1fe2812016-04-27 06:47:29 -07001700 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefab9b2d12015-10-14 11:33:11 -07001701 AddVideoStream(kStreamLabel1);
deadbeefc80741f2015-10-22 13:14:45 -07001702 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07001703 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001704
1705 // SetLocalDescription takes ownership of answer.
kwibergd1fe2812016-04-27 06:47:29 -07001706 std::unique_ptr<SessionDescriptionInterface> answer;
deadbeefc80741f2015-10-22 13:14:45 -07001707 EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07001708 EXPECT_TRUE(DoSetLocalDescription(std::move(answer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001709
1710 EXPECT_TRUE_WAIT(observer_.last_candidate_.get() != NULL, kTimeout);
1711 EXPECT_TRUE_WAIT(observer_.ice_complete_, kTimeout);
1712
1713 EXPECT_TRUE(pc_->AddIceCandidate(observer_.last_candidate_.get()));
1714}
1715
deadbeefab9b2d12015-10-14 11:33:11 -07001716// Test that CreateOffer and CreateAnswer will fail if the track labels are
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001717// not unique.
1718TEST_F(PeerConnectionInterfaceTest, CreateOfferAnswerWithInvalidStream) {
deadbeef293e9262017-01-11 12:28:30 -08001719 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001720 // Create a regular offer for the CreateAnswer test later.
kwibergd1fe2812016-04-27 06:47:29 -07001721 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefc80741f2015-10-22 13:14:45 -07001722 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
kwiberg2bbff992016-03-16 11:03:04 -07001723 EXPECT_TRUE(offer);
1724 offer.reset();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001725
1726 // Create a local stream with audio&video tracks having same label.
1727 AddAudioVideoStream(kStreamLabel1, "track_label", "track_label");
1728
1729 // Test CreateOffer
deadbeefc80741f2015-10-22 13:14:45 -07001730 EXPECT_FALSE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001731
1732 // Test CreateAnswer
kwibergd1fe2812016-04-27 06:47:29 -07001733 std::unique_ptr<SessionDescriptionInterface> answer;
deadbeefc80741f2015-10-22 13:14:45 -07001734 EXPECT_FALSE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001735}
1736
1737// Test that we will get different SSRCs for each tracks in the offer and answer
1738// we created.
1739TEST_F(PeerConnectionInterfaceTest, SsrcInOfferAnswer) {
deadbeef293e9262017-01-11 12:28:30 -08001740 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001741 // Create a local stream with audio&video tracks having different labels.
1742 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
1743
1744 // Test CreateOffer
kwibergd1fe2812016-04-27 06:47:29 -07001745 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001746 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001747 int audio_ssrc = 0;
1748 int video_ssrc = 0;
1749 EXPECT_TRUE(GetFirstSsrc(GetFirstAudioContent(offer->description()),
1750 &audio_ssrc));
1751 EXPECT_TRUE(GetFirstSsrc(GetFirstVideoContent(offer->description()),
1752 &video_ssrc));
1753 EXPECT_NE(audio_ssrc, video_ssrc);
1754
1755 // Test CreateAnswer
Steve Antondb45ca82017-09-11 18:27:34 -07001756 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
kwibergd1fe2812016-04-27 06:47:29 -07001757 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -07001758 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001759 audio_ssrc = 0;
1760 video_ssrc = 0;
1761 EXPECT_TRUE(GetFirstSsrc(GetFirstAudioContent(answer->description()),
1762 &audio_ssrc));
1763 EXPECT_TRUE(GetFirstSsrc(GetFirstVideoContent(answer->description()),
1764 &video_ssrc));
1765 EXPECT_NE(audio_ssrc, video_ssrc);
1766}
1767
deadbeefeb459812015-12-15 19:24:43 -08001768// Test that it's possible to call AddTrack on a MediaStream after adding
1769// the stream to a PeerConnection.
1770// TODO(deadbeef): Remove this test once this behavior is no longer supported.
1771TEST_F(PeerConnectionInterfaceTest, AddTrackAfterAddStream) {
deadbeef293e9262017-01-11 12:28:30 -08001772 CreatePeerConnectionWithoutDtls();
deadbeefeb459812015-12-15 19:24:43 -08001773 // Create audio stream and add to PeerConnection.
1774 AddVoiceStream(kStreamLabel1);
1775 MediaStreamInterface* stream = pc_->local_streams()->at(0);
1776
1777 // Add video track to the audio-only stream.
zhihuang9763d562016-08-05 11:14:50 -07001778 rtc::scoped_refptr<VideoTrackInterface> video_track(
1779 pc_factory_->CreateVideoTrack(
deadbeef112b2e92017-02-10 20:13:37 -08001780 "video_label", pc_factory_->CreateVideoSource(
1781 std::unique_ptr<cricket::VideoCapturer>(
1782 new cricket::FakeVideoCapturer()))));
deadbeefeb459812015-12-15 19:24:43 -08001783 stream->AddTrack(video_track.get());
1784
kwibergd1fe2812016-04-27 06:47:29 -07001785 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001786 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefeb459812015-12-15 19:24:43 -08001787
1788 const cricket::MediaContentDescription* video_desc =
1789 cricket::GetFirstVideoContentDescription(offer->description());
1790 EXPECT_TRUE(video_desc != nullptr);
1791}
1792
1793// Test that it's possible to call RemoveTrack on a MediaStream after adding
1794// the stream to a PeerConnection.
1795// TODO(deadbeef): Remove this test once this behavior is no longer supported.
1796TEST_F(PeerConnectionInterfaceTest, RemoveTrackAfterAddStream) {
deadbeef293e9262017-01-11 12:28:30 -08001797 CreatePeerConnectionWithoutDtls();
deadbeefeb459812015-12-15 19:24:43 -08001798 // Create audio/video stream and add to PeerConnection.
1799 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
1800 MediaStreamInterface* stream = pc_->local_streams()->at(0);
1801
1802 // Remove the video track.
1803 stream->RemoveTrack(stream->GetVideoTracks()[0]);
1804
kwibergd1fe2812016-04-27 06:47:29 -07001805 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001806 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefeb459812015-12-15 19:24:43 -08001807
1808 const cricket::MediaContentDescription* video_desc =
1809 cricket::GetFirstVideoContentDescription(offer->description());
1810 EXPECT_TRUE(video_desc == nullptr);
1811}
1812
deadbeef1dcb1642017-03-29 21:08:16 -07001813// Verify that CreateDtmfSender only succeeds if called with a valid local
1814// track. Other aspects of DtmfSenders are tested in
1815// peerconnection_integrationtest.cc.
1816TEST_F(PeerConnectionInterfaceTest, CreateDtmfSenderWithInvalidParams) {
1817 CreatePeerConnection();
1818 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
1819 EXPECT_EQ(nullptr, pc_->CreateDtmfSender(nullptr));
1820 rtc::scoped_refptr<webrtc::AudioTrackInterface> non_localtrack(
1821 pc_factory_->CreateAudioTrack("dummy_track", nullptr));
1822 EXPECT_EQ(nullptr, pc_->CreateDtmfSender(non_localtrack));
1823}
1824
deadbeefbd7d8f72015-12-18 16:58:44 -08001825// Test creating a sender with a stream ID, and ensure the ID is populated
1826// in the offer.
1827TEST_F(PeerConnectionInterfaceTest, CreateSenderWithStream) {
deadbeef293e9262017-01-11 12:28:30 -08001828 CreatePeerConnectionWithoutDtls();
deadbeefbd7d8f72015-12-18 16:58:44 -08001829 pc_->CreateSender("video", kStreamLabel1);
1830
kwibergd1fe2812016-04-27 06:47:29 -07001831 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001832 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefbd7d8f72015-12-18 16:58:44 -08001833
1834 const cricket::MediaContentDescription* video_desc =
1835 cricket::GetFirstVideoContentDescription(offer->description());
1836 ASSERT_TRUE(video_desc != nullptr);
1837 ASSERT_EQ(1u, video_desc->streams().size());
1838 EXPECT_EQ(kStreamLabel1, video_desc->streams()[0].sync_label);
1839}
1840
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001841// Test that we can specify a certain track that we want statistics about.
1842TEST_F(PeerConnectionInterfaceTest, GetStatsForSpecificTrack) {
1843 InitiateCall();
1844 ASSERT_LT(0u, pc_->remote_streams()->count());
1845 ASSERT_LT(0u, pc_->remote_streams()->at(0)->GetAudioTracks().size());
zhihuang9763d562016-08-05 11:14:50 -07001846 rtc::scoped_refptr<MediaStreamTrackInterface> remote_audio =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001847 pc_->remote_streams()->at(0)->GetAudioTracks()[0];
1848 EXPECT_TRUE(DoGetStats(remote_audio));
1849
1850 // Remove the stream. Since we are sending to our selves the local
1851 // and the remote stream is the same.
1852 pc_->RemoveStream(pc_->local_streams()->at(0));
1853 // Do a re-negotiation.
1854 CreateOfferReceiveAnswer();
1855
1856 ASSERT_EQ(0u, pc_->remote_streams()->count());
1857
1858 // Test that we still can get statistics for the old track. Even if it is not
1859 // sent any longer.
1860 EXPECT_TRUE(DoGetStats(remote_audio));
1861}
1862
1863// Test that we can get stats on a video track.
1864TEST_F(PeerConnectionInterfaceTest, GetStatsForVideoTrack) {
1865 InitiateCall();
1866 ASSERT_LT(0u, pc_->remote_streams()->count());
1867 ASSERT_LT(0u, pc_->remote_streams()->at(0)->GetVideoTracks().size());
zhihuang9763d562016-08-05 11:14:50 -07001868 rtc::scoped_refptr<MediaStreamTrackInterface> remote_video =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001869 pc_->remote_streams()->at(0)->GetVideoTracks()[0];
1870 EXPECT_TRUE(DoGetStats(remote_video));
1871}
1872
1873// Test that we don't get statistics for an invalid track.
zhihuange9e94c32016-11-04 11:38:15 -07001874TEST_F(PeerConnectionInterfaceTest, GetStatsForInvalidTrack) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001875 InitiateCall();
zhihuang9763d562016-08-05 11:14:50 -07001876 rtc::scoped_refptr<AudioTrackInterface> unknown_audio_track(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001877 pc_factory_->CreateAudioTrack("unknown track", NULL));
1878 EXPECT_FALSE(DoGetStats(unknown_audio_track));
1879}
1880
1881// This test setup two RTP data channels in loop back.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001882TEST_F(PeerConnectionInterfaceTest, TestDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001883 FakeConstraints constraints;
1884 constraints.SetAllowRtpDataChannels();
1885 CreatePeerConnection(&constraints);
zhihuang9763d562016-08-05 11:14:50 -07001886 rtc::scoped_refptr<DataChannelInterface> data1 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001887 pc_->CreateDataChannel("test1", NULL);
zhihuang9763d562016-08-05 11:14:50 -07001888 rtc::scoped_refptr<DataChannelInterface> data2 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001889 pc_->CreateDataChannel("test2", NULL);
1890 ASSERT_TRUE(data1 != NULL);
kwibergd1fe2812016-04-27 06:47:29 -07001891 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001892 new MockDataChannelObserver(data1));
kwibergd1fe2812016-04-27 06:47:29 -07001893 std::unique_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001894 new MockDataChannelObserver(data2));
1895
1896 EXPECT_EQ(DataChannelInterface::kConnecting, data1->state());
1897 EXPECT_EQ(DataChannelInterface::kConnecting, data2->state());
1898 std::string data_to_send1 = "testing testing";
1899 std::string data_to_send2 = "testing something else";
1900 EXPECT_FALSE(data1->Send(DataBuffer(data_to_send1)));
1901
1902 CreateOfferReceiveAnswer();
1903 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1904 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
1905
1906 EXPECT_EQ(DataChannelInterface::kOpen, data1->state());
1907 EXPECT_EQ(DataChannelInterface::kOpen, data2->state());
1908 EXPECT_TRUE(data1->Send(DataBuffer(data_to_send1)));
1909 EXPECT_TRUE(data2->Send(DataBuffer(data_to_send2)));
1910
1911 EXPECT_EQ_WAIT(data_to_send1, observer1->last_message(), kTimeout);
1912 EXPECT_EQ_WAIT(data_to_send2, observer2->last_message(), kTimeout);
1913
1914 data1->Close();
1915 EXPECT_EQ(DataChannelInterface::kClosing, data1->state());
1916 CreateOfferReceiveAnswer();
1917 EXPECT_FALSE(observer1->IsOpen());
1918 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
1919 EXPECT_TRUE(observer2->IsOpen());
1920
1921 data_to_send2 = "testing something else again";
1922 EXPECT_TRUE(data2->Send(DataBuffer(data_to_send2)));
1923
1924 EXPECT_EQ_WAIT(data_to_send2, observer2->last_message(), kTimeout);
1925}
1926
1927// This test verifies that sendnig binary data over RTP data channels should
1928// fail.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001929TEST_F(PeerConnectionInterfaceTest, TestSendBinaryOnRtpDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001930 FakeConstraints constraints;
1931 constraints.SetAllowRtpDataChannels();
1932 CreatePeerConnection(&constraints);
zhihuang9763d562016-08-05 11:14:50 -07001933 rtc::scoped_refptr<DataChannelInterface> data1 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001934 pc_->CreateDataChannel("test1", NULL);
zhihuang9763d562016-08-05 11:14:50 -07001935 rtc::scoped_refptr<DataChannelInterface> data2 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001936 pc_->CreateDataChannel("test2", NULL);
1937 ASSERT_TRUE(data1 != NULL);
kwibergd1fe2812016-04-27 06:47:29 -07001938 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001939 new MockDataChannelObserver(data1));
kwibergd1fe2812016-04-27 06:47:29 -07001940 std::unique_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001941 new MockDataChannelObserver(data2));
1942
1943 EXPECT_EQ(DataChannelInterface::kConnecting, data1->state());
1944 EXPECT_EQ(DataChannelInterface::kConnecting, data2->state());
1945
1946 CreateOfferReceiveAnswer();
1947 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1948 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
1949
1950 EXPECT_EQ(DataChannelInterface::kOpen, data1->state());
1951 EXPECT_EQ(DataChannelInterface::kOpen, data2->state());
1952
jbaucheec21bd2016-03-20 06:15:43 -07001953 rtc::CopyOnWriteBuffer buffer("test", 4);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001954 EXPECT_FALSE(data1->Send(DataBuffer(buffer, true)));
1955}
1956
1957// This test setup a RTP data channels in loop back and test that a channel is
1958// opened even if the remote end answer with a zero SSRC.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001959TEST_F(PeerConnectionInterfaceTest, TestSendOnlyDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001960 FakeConstraints constraints;
1961 constraints.SetAllowRtpDataChannels();
1962 CreatePeerConnection(&constraints);
zhihuang9763d562016-08-05 11:14:50 -07001963 rtc::scoped_refptr<DataChannelInterface> data1 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001964 pc_->CreateDataChannel("test1", NULL);
kwibergd1fe2812016-04-27 06:47:29 -07001965 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001966 new MockDataChannelObserver(data1));
1967
1968 CreateOfferReceiveAnswerWithoutSsrc();
1969
1970 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1971
1972 data1->Close();
1973 EXPECT_EQ(DataChannelInterface::kClosing, data1->state());
1974 CreateOfferReceiveAnswerWithoutSsrc();
1975 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
1976 EXPECT_FALSE(observer1->IsOpen());
1977}
1978
1979// This test that if a data channel is added in an answer a receive only channel
1980// channel is created.
1981TEST_F(PeerConnectionInterfaceTest, TestReceiveOnlyDataChannel) {
1982 FakeConstraints constraints;
1983 constraints.SetAllowRtpDataChannels();
1984 CreatePeerConnection(&constraints);
1985
1986 std::string offer_label = "offer_channel";
zhihuang9763d562016-08-05 11:14:50 -07001987 rtc::scoped_refptr<DataChannelInterface> offer_channel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001988 pc_->CreateDataChannel(offer_label, NULL);
1989
1990 CreateOfferAsLocalDescription();
1991
1992 // Replace the data channel label in the offer and apply it as an answer.
1993 std::string receive_label = "answer_channel";
1994 std::string sdp;
1995 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001996 rtc::replace_substrs(offer_label.c_str(), offer_label.length(),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001997 receive_label.c_str(), receive_label.length(),
1998 &sdp);
1999 CreateAnswerAsRemoteDescription(sdp);
2000
2001 // Verify that a new incoming data channel has been created and that
2002 // it is open but can't we written to.
2003 ASSERT_TRUE(observer_.last_datachannel_ != NULL);
2004 DataChannelInterface* received_channel = observer_.last_datachannel_;
2005 EXPECT_EQ(DataChannelInterface::kConnecting, received_channel->state());
2006 EXPECT_EQ(receive_label, received_channel->label());
2007 EXPECT_FALSE(received_channel->Send(DataBuffer("something")));
2008
2009 // Verify that the channel we initially offered has been rejected.
2010 EXPECT_EQ(DataChannelInterface::kClosed, offer_channel->state());
2011
2012 // Do another offer / answer exchange and verify that the data channel is
2013 // opened.
2014 CreateOfferReceiveAnswer();
2015 EXPECT_EQ_WAIT(DataChannelInterface::kOpen, received_channel->state(),
2016 kTimeout);
2017}
2018
2019// This test that no data channel is returned if a reliable channel is
2020// requested.
2021// TODO(perkj): Remove this test once reliable channels are implemented.
2022TEST_F(PeerConnectionInterfaceTest, CreateReliableRtpDataChannelShouldFail) {
2023 FakeConstraints constraints;
2024 constraints.SetAllowRtpDataChannels();
2025 CreatePeerConnection(&constraints);
2026
2027 std::string label = "test";
2028 webrtc::DataChannelInit config;
2029 config.reliable = true;
zhihuang9763d562016-08-05 11:14:50 -07002030 rtc::scoped_refptr<DataChannelInterface> channel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002031 pc_->CreateDataChannel(label, &config);
2032 EXPECT_TRUE(channel == NULL);
2033}
2034
deadbeefab9b2d12015-10-14 11:33:11 -07002035// Verifies that duplicated label is not allowed for RTP data channel.
2036TEST_F(PeerConnectionInterfaceTest, RtpDuplicatedLabelNotAllowed) {
2037 FakeConstraints constraints;
2038 constraints.SetAllowRtpDataChannels();
2039 CreatePeerConnection(&constraints);
2040
2041 std::string label = "test";
zhihuang9763d562016-08-05 11:14:50 -07002042 rtc::scoped_refptr<DataChannelInterface> channel =
deadbeefab9b2d12015-10-14 11:33:11 -07002043 pc_->CreateDataChannel(label, nullptr);
2044 EXPECT_NE(channel, nullptr);
2045
zhihuang9763d562016-08-05 11:14:50 -07002046 rtc::scoped_refptr<DataChannelInterface> dup_channel =
deadbeefab9b2d12015-10-14 11:33:11 -07002047 pc_->CreateDataChannel(label, nullptr);
2048 EXPECT_EQ(dup_channel, nullptr);
2049}
2050
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002051// This tests that a SCTP data channel is returned using different
2052// DataChannelInit configurations.
2053TEST_F(PeerConnectionInterfaceTest, CreateSctpDataChannel) {
2054 FakeConstraints constraints;
2055 constraints.SetAllowDtlsSctpDataChannels();
2056 CreatePeerConnection(&constraints);
2057
2058 webrtc::DataChannelInit config;
2059
zhihuang9763d562016-08-05 11:14:50 -07002060 rtc::scoped_refptr<DataChannelInterface> channel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002061 pc_->CreateDataChannel("1", &config);
2062 EXPECT_TRUE(channel != NULL);
2063 EXPECT_TRUE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00002064 EXPECT_TRUE(observer_.renegotiation_needed_);
2065 observer_.renegotiation_needed_ = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002066
2067 config.ordered = false;
2068 channel = pc_->CreateDataChannel("2", &config);
2069 EXPECT_TRUE(channel != NULL);
2070 EXPECT_TRUE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00002071 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002072
2073 config.ordered = true;
2074 config.maxRetransmits = 0;
2075 channel = pc_->CreateDataChannel("3", &config);
2076 EXPECT_TRUE(channel != NULL);
2077 EXPECT_FALSE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00002078 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002079
2080 config.maxRetransmits = -1;
2081 config.maxRetransmitTime = 0;
2082 channel = pc_->CreateDataChannel("4", &config);
2083 EXPECT_TRUE(channel != NULL);
2084 EXPECT_FALSE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00002085 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002086}
2087
2088// This tests that no data channel is returned if both maxRetransmits and
2089// maxRetransmitTime are set for SCTP data channels.
2090TEST_F(PeerConnectionInterfaceTest,
2091 CreateSctpDataChannelShouldFailForInvalidConfig) {
2092 FakeConstraints constraints;
2093 constraints.SetAllowDtlsSctpDataChannels();
2094 CreatePeerConnection(&constraints);
2095
2096 std::string label = "test";
2097 webrtc::DataChannelInit config;
2098 config.maxRetransmits = 0;
2099 config.maxRetransmitTime = 0;
2100
zhihuang9763d562016-08-05 11:14:50 -07002101 rtc::scoped_refptr<DataChannelInterface> channel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002102 pc_->CreateDataChannel(label, &config);
2103 EXPECT_TRUE(channel == NULL);
2104}
2105
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002106// The test verifies that creating a SCTP data channel with an id already in use
2107// or out of range should fail.
2108TEST_F(PeerConnectionInterfaceTest,
2109 CreateSctpDataChannelWithInvalidIdShouldFail) {
2110 FakeConstraints constraints;
2111 constraints.SetAllowDtlsSctpDataChannels();
2112 CreatePeerConnection(&constraints);
2113
2114 webrtc::DataChannelInit config;
zhihuang9763d562016-08-05 11:14:50 -07002115 rtc::scoped_refptr<DataChannelInterface> channel;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002116
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00002117 config.id = 1;
2118 channel = pc_->CreateDataChannel("1", &config);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002119 EXPECT_TRUE(channel != NULL);
2120 EXPECT_EQ(1, channel->id());
2121
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002122 channel = pc_->CreateDataChannel("x", &config);
2123 EXPECT_TRUE(channel == NULL);
2124
2125 config.id = cricket::kMaxSctpSid;
2126 channel = pc_->CreateDataChannel("max", &config);
2127 EXPECT_TRUE(channel != NULL);
2128 EXPECT_EQ(config.id, channel->id());
2129
2130 config.id = cricket::kMaxSctpSid + 1;
2131 channel = pc_->CreateDataChannel("x", &config);
2132 EXPECT_TRUE(channel == NULL);
2133}
2134
deadbeefab9b2d12015-10-14 11:33:11 -07002135// Verifies that duplicated label is allowed for SCTP data channel.
2136TEST_F(PeerConnectionInterfaceTest, SctpDuplicatedLabelAllowed) {
2137 FakeConstraints constraints;
2138 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2139 true);
2140 CreatePeerConnection(&constraints);
2141
2142 std::string label = "test";
zhihuang9763d562016-08-05 11:14:50 -07002143 rtc::scoped_refptr<DataChannelInterface> channel =
deadbeefab9b2d12015-10-14 11:33:11 -07002144 pc_->CreateDataChannel(label, nullptr);
2145 EXPECT_NE(channel, nullptr);
2146
zhihuang9763d562016-08-05 11:14:50 -07002147 rtc::scoped_refptr<DataChannelInterface> dup_channel =
deadbeefab9b2d12015-10-14 11:33:11 -07002148 pc_->CreateDataChannel(label, nullptr);
2149 EXPECT_NE(dup_channel, nullptr);
2150}
2151
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00002152// This test verifies that OnRenegotiationNeeded is fired for every new RTP
2153// DataChannel.
2154TEST_F(PeerConnectionInterfaceTest, RenegotiationNeededForNewRtpDataChannel) {
2155 FakeConstraints constraints;
2156 constraints.SetAllowRtpDataChannels();
2157 CreatePeerConnection(&constraints);
2158
zhihuang9763d562016-08-05 11:14:50 -07002159 rtc::scoped_refptr<DataChannelInterface> dc1 =
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00002160 pc_->CreateDataChannel("test1", NULL);
2161 EXPECT_TRUE(observer_.renegotiation_needed_);
2162 observer_.renegotiation_needed_ = false;
2163
zhihuang9763d562016-08-05 11:14:50 -07002164 rtc::scoped_refptr<DataChannelInterface> dc2 =
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00002165 pc_->CreateDataChannel("test2", NULL);
2166 EXPECT_TRUE(observer_.renegotiation_needed_);
2167}
2168
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002169// This test that a data channel closes when a PeerConnection is deleted/closed.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002170TEST_F(PeerConnectionInterfaceTest, DataChannelCloseWhenPeerConnectionClose) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002171 FakeConstraints constraints;
2172 constraints.SetAllowRtpDataChannels();
2173 CreatePeerConnection(&constraints);
2174
zhihuang9763d562016-08-05 11:14:50 -07002175 rtc::scoped_refptr<DataChannelInterface> data1 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002176 pc_->CreateDataChannel("test1", NULL);
zhihuang9763d562016-08-05 11:14:50 -07002177 rtc::scoped_refptr<DataChannelInterface> data2 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002178 pc_->CreateDataChannel("test2", NULL);
2179 ASSERT_TRUE(data1 != NULL);
kwibergd1fe2812016-04-27 06:47:29 -07002180 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002181 new MockDataChannelObserver(data1));
kwibergd1fe2812016-04-27 06:47:29 -07002182 std::unique_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002183 new MockDataChannelObserver(data2));
2184
2185 CreateOfferReceiveAnswer();
2186 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
2187 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
2188
2189 ReleasePeerConnection();
2190 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
2191 EXPECT_EQ(DataChannelInterface::kClosed, data2->state());
2192}
2193
2194// This test that data channels can be rejected in an answer.
2195TEST_F(PeerConnectionInterfaceTest, TestRejectDataChannelInAnswer) {
2196 FakeConstraints constraints;
2197 constraints.SetAllowRtpDataChannels();
2198 CreatePeerConnection(&constraints);
2199
zhihuang9763d562016-08-05 11:14:50 -07002200 rtc::scoped_refptr<DataChannelInterface> offer_channel(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002201 pc_->CreateDataChannel("offer_channel", NULL));
2202
2203 CreateOfferAsLocalDescription();
2204
2205 // Create an answer where the m-line for data channels are rejected.
2206 std::string sdp;
2207 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07002208 std::unique_ptr<SessionDescriptionInterface> answer(
2209 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
2210 sdp, nullptr));
2211 ASSERT_TRUE(answer);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002212 cricket::ContentInfo* data_info =
2213 answer->description()->GetContentByName("data");
2214 data_info->rejected = true;
2215
Steve Antondb45ca82017-09-11 18:27:34 -07002216 DoSetRemoteDescription(std::move(answer));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002217 EXPECT_EQ(DataChannelInterface::kClosed, offer_channel->state());
2218}
2219
2220// Test that we can create a session description from an SDP string from
2221// FireFox, use it as a remote session description, generate an answer and use
2222// the answer as a local description.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002223TEST_F(PeerConnectionInterfaceTest, ReceiveFireFoxOffer) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002224 FakeConstraints constraints;
2225 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2226 true);
2227 CreatePeerConnection(&constraints);
2228 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
Steve Antondb45ca82017-09-11 18:27:34 -07002229 std::unique_ptr<SessionDescriptionInterface> desc(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002230 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
Steve Antondb45ca82017-09-11 18:27:34 -07002231 webrtc::kFireFoxSdpOffer, nullptr));
2232 EXPECT_TRUE(DoSetSessionDescription(std::move(desc), false));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002233 CreateAnswerAsLocalDescription();
2234 ASSERT_TRUE(pc_->local_description() != NULL);
2235 ASSERT_TRUE(pc_->remote_description() != NULL);
2236
2237 const cricket::ContentInfo* content =
2238 cricket::GetFirstAudioContent(pc_->local_description()->description());
2239 ASSERT_TRUE(content != NULL);
2240 EXPECT_FALSE(content->rejected);
2241
2242 content =
2243 cricket::GetFirstVideoContent(pc_->local_description()->description());
2244 ASSERT_TRUE(content != NULL);
2245 EXPECT_FALSE(content->rejected);
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00002246#ifdef HAVE_SCTP
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002247 content =
2248 cricket::GetFirstDataContent(pc_->local_description()->description());
2249 ASSERT_TRUE(content != NULL);
2250 EXPECT_TRUE(content->rejected);
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00002251#endif
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002252}
2253
deadbeef8662f942017-01-20 21:20:51 -08002254// Test that an offer can be received which offers DTLS with SDES fallback.
2255// Regression test for issue:
2256// https://bugs.chromium.org/p/webrtc/issues/detail?id=6972
2257TEST_F(PeerConnectionInterfaceTest, ReceiveDtlsSdesFallbackOffer) {
2258 FakeConstraints constraints;
2259 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2260 true);
2261 CreatePeerConnection(&constraints);
2262 // Wait for fake certificate to be generated. Previously, this is what caused
2263 // the "a=crypto" lines to be rejected.
2264 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
2265 ASSERT_NE(nullptr, fake_certificate_generator_);
2266 EXPECT_EQ_WAIT(1, fake_certificate_generator_->generated_certificates(),
2267 kTimeout);
Steve Antondb45ca82017-09-11 18:27:34 -07002268 std::unique_ptr<SessionDescriptionInterface> desc(
2269 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
2270 kDtlsSdesFallbackSdp, nullptr));
2271 EXPECT_TRUE(DoSetSessionDescription(std::move(desc), false));
deadbeef8662f942017-01-20 21:20:51 -08002272 CreateAnswerAsLocalDescription();
2273}
2274
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002275// Test that we can create an audio only offer and receive an answer with a
2276// limited set of audio codecs and receive an updated offer with more audio
2277// codecs, where the added codecs are not supported.
2278TEST_F(PeerConnectionInterfaceTest, ReceiveUpdatedAudioOfferWithBadCodecs) {
deadbeef293e9262017-01-11 12:28:30 -08002279 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002280 AddVoiceStream("audio_label");
2281 CreateOfferAsLocalDescription();
2282
Steve Antondb45ca82017-09-11 18:27:34 -07002283 std::unique_ptr<SessionDescriptionInterface> answer(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002284 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
Steve Antondb45ca82017-09-11 18:27:34 -07002285 webrtc::kAudioSdp, nullptr));
2286 EXPECT_TRUE(DoSetSessionDescription(std::move(answer), false));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002287
Steve Antondb45ca82017-09-11 18:27:34 -07002288 std::unique_ptr<SessionDescriptionInterface> updated_offer(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002289 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
jbauchfabe2c92015-07-16 13:43:14 -07002290 webrtc::kAudioSdpWithUnsupportedCodecs,
Steve Antondb45ca82017-09-11 18:27:34 -07002291 nullptr));
2292 EXPECT_TRUE(DoSetSessionDescription(std::move(updated_offer), false));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002293 CreateAnswerAsLocalDescription();
2294}
2295
deadbeefc80741f2015-10-22 13:14:45 -07002296// Test that if we're receiving (but not sending) a track, subsequent offers
2297// will have m-lines with a=recvonly.
2298TEST_F(PeerConnectionInterfaceTest, CreateSubsequentRecvOnlyOffer) {
2299 FakeConstraints constraints;
2300 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2301 true);
2302 CreatePeerConnection(&constraints);
2303 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2304 CreateAnswerAsLocalDescription();
2305
2306 // At this point we should be receiving stream 1, but not sending anything.
2307 // A new offer should be recvonly.
kwibergd1fe2812016-04-27 06:47:29 -07002308 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefc80741f2015-10-22 13:14:45 -07002309 DoCreateOffer(&offer, nullptr);
2310
2311 const cricket::ContentInfo* video_content =
2312 cricket::GetFirstVideoContent(offer->description());
2313 const cricket::VideoContentDescription* video_desc =
2314 static_cast<const cricket::VideoContentDescription*>(
2315 video_content->description);
2316 ASSERT_EQ(cricket::MD_RECVONLY, video_desc->direction());
2317
2318 const cricket::ContentInfo* audio_content =
2319 cricket::GetFirstAudioContent(offer->description());
2320 const cricket::AudioContentDescription* audio_desc =
2321 static_cast<const cricket::AudioContentDescription*>(
2322 audio_content->description);
2323 ASSERT_EQ(cricket::MD_RECVONLY, audio_desc->direction());
2324}
2325
2326// Test that if we're receiving (but not sending) a track, and the
2327// offerToReceiveVideo/offerToReceiveAudio constraints are explicitly set to
2328// false, the generated m-lines will be a=inactive.
2329TEST_F(PeerConnectionInterfaceTest, CreateSubsequentInactiveOffer) {
2330 FakeConstraints constraints;
2331 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2332 true);
2333 CreatePeerConnection(&constraints);
2334 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2335 CreateAnswerAsLocalDescription();
2336
2337 // At this point we should be receiving stream 1, but not sending anything.
2338 // A new offer would be recvonly, but we'll set the "no receive" constraints
2339 // to make it inactive.
kwibergd1fe2812016-04-27 06:47:29 -07002340 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefc80741f2015-10-22 13:14:45 -07002341 FakeConstraints offer_constraints;
2342 offer_constraints.AddMandatory(
2343 webrtc::MediaConstraintsInterface::kOfferToReceiveVideo, false);
2344 offer_constraints.AddMandatory(
2345 webrtc::MediaConstraintsInterface::kOfferToReceiveAudio, false);
2346 DoCreateOffer(&offer, &offer_constraints);
2347
2348 const cricket::ContentInfo* video_content =
2349 cricket::GetFirstVideoContent(offer->description());
2350 const cricket::VideoContentDescription* video_desc =
2351 static_cast<const cricket::VideoContentDescription*>(
2352 video_content->description);
2353 ASSERT_EQ(cricket::MD_INACTIVE, video_desc->direction());
2354
2355 const cricket::ContentInfo* audio_content =
2356 cricket::GetFirstAudioContent(offer->description());
2357 const cricket::AudioContentDescription* audio_desc =
2358 static_cast<const cricket::AudioContentDescription*>(
2359 audio_content->description);
2360 ASSERT_EQ(cricket::MD_INACTIVE, audio_desc->direction());
2361}
2362
deadbeef653b8e02015-11-11 12:55:10 -08002363// Test that we can use SetConfiguration to change the ICE servers of the
2364// PortAllocator.
2365TEST_F(PeerConnectionInterfaceTest, SetConfigurationChangesIceServers) {
2366 CreatePeerConnection();
2367
2368 PeerConnectionInterface::RTCConfiguration config;
2369 PeerConnectionInterface::IceServer server;
2370 server.uri = "stun:test_hostname";
2371 config.servers.push_back(server);
2372 EXPECT_TRUE(pc_->SetConfiguration(config));
2373
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002374 EXPECT_EQ(1u, port_allocator_->stun_servers().size());
2375 EXPECT_EQ("test_hostname",
2376 port_allocator_->stun_servers().begin()->hostname());
deadbeef653b8e02015-11-11 12:55:10 -08002377}
2378
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002379TEST_F(PeerConnectionInterfaceTest, SetConfigurationChangesCandidateFilter) {
2380 CreatePeerConnection();
2381 PeerConnectionInterface::RTCConfiguration config;
2382 config.type = PeerConnectionInterface::kRelay;
2383 EXPECT_TRUE(pc_->SetConfiguration(config));
2384 EXPECT_EQ(cricket::CF_RELAY, port_allocator_->candidate_filter());
2385}
2386
deadbeef293e9262017-01-11 12:28:30 -08002387TEST_F(PeerConnectionInterfaceTest, SetConfigurationChangesPruneTurnPortsFlag) {
2388 PeerConnectionInterface::RTCConfiguration config;
2389 config.prune_turn_ports = false;
2390 CreatePeerConnection(config, nullptr);
2391 EXPECT_FALSE(port_allocator_->prune_turn_ports());
2392
2393 config.prune_turn_ports = true;
2394 EXPECT_TRUE(pc_->SetConfiguration(config));
2395 EXPECT_TRUE(port_allocator_->prune_turn_ports());
2396}
2397
skvladd1f5fda2017-02-03 16:54:05 -08002398// Test that the ice check interval can be changed. This does not verify that
2399// the setting makes it all the way to P2PTransportChannel, as that would
2400// require a very complex set of mocks.
2401TEST_F(PeerConnectionInterfaceTest, SetConfigurationChangesIceCheckInterval) {
2402 PeerConnectionInterface::RTCConfiguration config;
2403 config.ice_check_min_interval = rtc::Optional<int>();
2404 CreatePeerConnection(config, nullptr);
2405 config.ice_check_min_interval = rtc::Optional<int>(100);
2406 EXPECT_TRUE(pc_->SetConfiguration(config));
2407 PeerConnectionInterface::RTCConfiguration new_config =
2408 pc_->GetConfiguration();
2409 EXPECT_EQ(new_config.ice_check_min_interval, rtc::Optional<int>(100));
2410}
2411
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002412// Test that when SetConfiguration changes both the pool size and other
2413// attributes, the pooled session is created with the updated attributes.
2414TEST_F(PeerConnectionInterfaceTest,
2415 SetConfigurationCreatesPooledSessionCorrectly) {
2416 CreatePeerConnection();
2417 PeerConnectionInterface::RTCConfiguration config;
2418 config.ice_candidate_pool_size = 1;
2419 PeerConnectionInterface::IceServer server;
2420 server.uri = kStunAddressOnly;
2421 config.servers.push_back(server);
2422 config.type = PeerConnectionInterface::kRelay;
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07002423 EXPECT_TRUE(pc_->SetConfiguration(config));
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002424
2425 const cricket::FakePortAllocatorSession* session =
2426 static_cast<const cricket::FakePortAllocatorSession*>(
2427 port_allocator_->GetPooledSession());
2428 ASSERT_NE(nullptr, session);
2429 EXPECT_EQ(1UL, session->stun_servers().size());
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002430}
2431
deadbeef293e9262017-01-11 12:28:30 -08002432// Test that after SetLocalDescription, changing the pool size is not allowed,
2433// and an invalid modification error is returned.
deadbeef6de92f92016-12-12 18:49:32 -08002434TEST_F(PeerConnectionInterfaceTest,
2435 CantChangePoolSizeAfterSetLocalDescription) {
2436 CreatePeerConnection();
2437 // Start by setting a size of 1.
2438 PeerConnectionInterface::RTCConfiguration config;
2439 config.ice_candidate_pool_size = 1;
2440 EXPECT_TRUE(pc_->SetConfiguration(config));
2441
2442 // Set remote offer; can still change pool size at this point.
2443 CreateOfferAsRemoteDescription();
2444 config.ice_candidate_pool_size = 2;
2445 EXPECT_TRUE(pc_->SetConfiguration(config));
2446
2447 // Set local answer; now it's too late.
2448 CreateAnswerAsLocalDescription();
2449 config.ice_candidate_pool_size = 3;
deadbeef293e9262017-01-11 12:28:30 -08002450 RTCError error;
2451 EXPECT_FALSE(pc_->SetConfiguration(config, &error));
2452 EXPECT_EQ(RTCErrorType::INVALID_MODIFICATION, error.type());
2453}
2454
deadbeef42a42632017-03-10 15:18:00 -08002455// Test that after setting an answer, extra pooled sessions are discarded. The
2456// ICE candidate pool is only intended to be used for the first offer/answer.
2457TEST_F(PeerConnectionInterfaceTest,
2458 ExtraPooledSessionsDiscardedAfterApplyingAnswer) {
2459 CreatePeerConnection();
2460
2461 // Set a larger-than-necessary size.
2462 PeerConnectionInterface::RTCConfiguration config;
2463 config.ice_candidate_pool_size = 4;
2464 EXPECT_TRUE(pc_->SetConfiguration(config));
2465
2466 // Do offer/answer.
2467 CreateOfferAsRemoteDescription();
2468 CreateAnswerAsLocalDescription();
2469
2470 // Expect no pooled sessions to be left.
2471 const cricket::PortAllocatorSession* session =
2472 port_allocator_->GetPooledSession();
2473 EXPECT_EQ(nullptr, session);
2474}
2475
2476// After Close is called, pooled candidates should be discarded so as to not
2477// waste network resources.
2478TEST_F(PeerConnectionInterfaceTest, PooledSessionsDiscardedAfterClose) {
2479 CreatePeerConnection();
2480
2481 PeerConnectionInterface::RTCConfiguration config;
2482 config.ice_candidate_pool_size = 3;
2483 EXPECT_TRUE(pc_->SetConfiguration(config));
2484 pc_->Close();
2485
2486 // Expect no pooled sessions to be left.
2487 const cricket::PortAllocatorSession* session =
2488 port_allocator_->GetPooledSession();
2489 EXPECT_EQ(nullptr, session);
2490}
2491
deadbeef293e9262017-01-11 12:28:30 -08002492// Test that SetConfiguration returns an invalid modification error if
2493// modifying a field in the configuration that isn't allowed to be modified.
2494TEST_F(PeerConnectionInterfaceTest,
2495 SetConfigurationReturnsInvalidModificationError) {
2496 PeerConnectionInterface::RTCConfiguration config;
2497 config.bundle_policy = PeerConnectionInterface::kBundlePolicyBalanced;
2498 config.rtcp_mux_policy = PeerConnectionInterface::kRtcpMuxPolicyNegotiate;
2499 config.continual_gathering_policy = PeerConnectionInterface::GATHER_ONCE;
2500 CreatePeerConnection(config, nullptr);
2501
2502 PeerConnectionInterface::RTCConfiguration modified_config = config;
2503 modified_config.bundle_policy =
2504 PeerConnectionInterface::kBundlePolicyMaxBundle;
2505 RTCError error;
2506 EXPECT_FALSE(pc_->SetConfiguration(modified_config, &error));
2507 EXPECT_EQ(RTCErrorType::INVALID_MODIFICATION, error.type());
2508
2509 modified_config = config;
2510 modified_config.rtcp_mux_policy =
2511 PeerConnectionInterface::kRtcpMuxPolicyRequire;
2512 error.set_type(RTCErrorType::NONE);
2513 EXPECT_FALSE(pc_->SetConfiguration(modified_config, &error));
2514 EXPECT_EQ(RTCErrorType::INVALID_MODIFICATION, error.type());
2515
2516 modified_config = config;
2517 modified_config.continual_gathering_policy =
2518 PeerConnectionInterface::GATHER_CONTINUALLY;
2519 error.set_type(RTCErrorType::NONE);
2520 EXPECT_FALSE(pc_->SetConfiguration(modified_config, &error));
2521 EXPECT_EQ(RTCErrorType::INVALID_MODIFICATION, error.type());
2522}
2523
2524// Test that SetConfiguration returns a range error if the candidate pool size
2525// is negative or larger than allowed by the spec.
2526TEST_F(PeerConnectionInterfaceTest,
2527 SetConfigurationReturnsRangeErrorForBadCandidatePoolSize) {
2528 PeerConnectionInterface::RTCConfiguration config;
2529 CreatePeerConnection(config, nullptr);
2530
2531 config.ice_candidate_pool_size = -1;
2532 RTCError error;
2533 EXPECT_FALSE(pc_->SetConfiguration(config, &error));
2534 EXPECT_EQ(RTCErrorType::INVALID_RANGE, error.type());
2535
2536 config.ice_candidate_pool_size = INT_MAX;
2537 error.set_type(RTCErrorType::NONE);
2538 EXPECT_FALSE(pc_->SetConfiguration(config, &error));
2539 EXPECT_EQ(RTCErrorType::INVALID_RANGE, error.type());
2540}
2541
2542// Test that SetConfiguration returns a syntax error if parsing an ICE server
2543// URL failed.
2544TEST_F(PeerConnectionInterfaceTest,
2545 SetConfigurationReturnsSyntaxErrorFromBadIceUrls) {
2546 PeerConnectionInterface::RTCConfiguration config;
2547 CreatePeerConnection(config, nullptr);
2548
2549 PeerConnectionInterface::IceServer bad_server;
2550 bad_server.uri = "stunn:www.example.com";
2551 config.servers.push_back(bad_server);
2552 RTCError error;
2553 EXPECT_FALSE(pc_->SetConfiguration(config, &error));
2554 EXPECT_EQ(RTCErrorType::SYNTAX_ERROR, error.type());
2555}
2556
2557// Test that SetConfiguration returns an invalid parameter error if a TURN
2558// IceServer is missing a username or password.
2559TEST_F(PeerConnectionInterfaceTest,
2560 SetConfigurationReturnsInvalidParameterIfCredentialsMissing) {
2561 PeerConnectionInterface::RTCConfiguration config;
2562 CreatePeerConnection(config, nullptr);
2563
2564 PeerConnectionInterface::IceServer bad_server;
2565 bad_server.uri = "turn:www.example.com";
2566 // Missing password.
2567 bad_server.username = "foo";
2568 config.servers.push_back(bad_server);
2569 RTCError error;
2570 EXPECT_FALSE(pc_->SetConfiguration(config, &error));
2571 EXPECT_EQ(RTCErrorType::INVALID_PARAMETER, error.type());
deadbeef6de92f92016-12-12 18:49:32 -08002572}
2573
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002574// Test that PeerConnection::Close changes the states to closed and all remote
2575// tracks change state to ended.
2576TEST_F(PeerConnectionInterfaceTest, CloseAndTestStreamsAndStates) {
2577 // Initialize a PeerConnection and negotiate local and remote session
2578 // description.
2579 InitiateCall();
2580 ASSERT_EQ(1u, pc_->local_streams()->count());
2581 ASSERT_EQ(1u, pc_->remote_streams()->count());
2582
2583 pc_->Close();
2584
2585 EXPECT_EQ(PeerConnectionInterface::kClosed, pc_->signaling_state());
2586 EXPECT_EQ(PeerConnectionInterface::kIceConnectionClosed,
2587 pc_->ice_connection_state());
2588 EXPECT_EQ(PeerConnectionInterface::kIceGatheringComplete,
2589 pc_->ice_gathering_state());
2590
2591 EXPECT_EQ(1u, pc_->local_streams()->count());
2592 EXPECT_EQ(1u, pc_->remote_streams()->count());
2593
zhihuang9763d562016-08-05 11:14:50 -07002594 rtc::scoped_refptr<MediaStreamInterface> remote_stream =
2595 pc_->remote_streams()->at(0);
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002596 // Track state may be updated asynchronously.
perkjd61bf802016-03-24 03:16:19 -07002597 EXPECT_EQ_WAIT(MediaStreamTrackInterface::kEnded,
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002598 remote_stream->GetAudioTracks()[0]->state(), kTimeout);
2599 EXPECT_EQ_WAIT(MediaStreamTrackInterface::kEnded,
2600 remote_stream->GetVideoTracks()[0]->state(), kTimeout);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002601}
2602
2603// Test that PeerConnection methods fails gracefully after
2604// PeerConnection::Close has been called.
2605TEST_F(PeerConnectionInterfaceTest, CloseAndTestMethods) {
deadbeef293e9262017-01-11 12:28:30 -08002606 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002607 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
2608 CreateOfferAsRemoteDescription();
2609 CreateAnswerAsLocalDescription();
2610
2611 ASSERT_EQ(1u, pc_->local_streams()->count());
zhihuang9763d562016-08-05 11:14:50 -07002612 rtc::scoped_refptr<MediaStreamInterface> local_stream =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002613 pc_->local_streams()->at(0);
2614
2615 pc_->Close();
2616
2617 pc_->RemoveStream(local_stream);
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +00002618 EXPECT_FALSE(pc_->AddStream(local_stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002619
2620 ASSERT_FALSE(local_stream->GetAudioTracks().empty());
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002621 rtc::scoped_refptr<webrtc::DtmfSenderInterface> dtmf_sender(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002622 pc_->CreateDtmfSender(local_stream->GetAudioTracks()[0]));
wu@webrtc.org66037362013-08-13 00:09:35 +00002623 EXPECT_TRUE(NULL == dtmf_sender); // local stream has been removed.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002624
2625 EXPECT_TRUE(pc_->CreateDataChannel("test", NULL) == NULL);
2626
2627 EXPECT_TRUE(pc_->local_description() != NULL);
2628 EXPECT_TRUE(pc_->remote_description() != NULL);
2629
kwibergd1fe2812016-04-27 06:47:29 -07002630 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07002631 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
kwibergd1fe2812016-04-27 06:47:29 -07002632 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -07002633 EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002634
2635 std::string sdp;
2636 ASSERT_TRUE(pc_->remote_description()->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07002637 std::unique_ptr<SessionDescriptionInterface> remote_offer(
2638 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer, sdp,
2639 nullptr));
2640 EXPECT_FALSE(DoSetRemoteDescription(std::move(remote_offer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002641
2642 ASSERT_TRUE(pc_->local_description()->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07002643 std::unique_ptr<SessionDescriptionInterface> local_offer(
2644 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer, sdp,
2645 nullptr));
2646 EXPECT_FALSE(DoSetLocalDescription(std::move(local_offer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002647}
2648
2649// Test that GetStats can still be called after PeerConnection::Close.
2650TEST_F(PeerConnectionInterfaceTest, CloseAndGetStats) {
2651 InitiateCall();
2652 pc_->Close();
2653 DoGetStats(NULL);
2654}
deadbeefab9b2d12015-10-14 11:33:11 -07002655
2656// NOTE: The series of tests below come from what used to be
2657// mediastreamsignaling_unittest.cc, and are mostly aimed at testing that
2658// setting a remote or local description has the expected effects.
2659
2660// This test verifies that the remote MediaStreams corresponding to a received
2661// SDP string is created. In this test the two separate MediaStreams are
2662// signaled.
2663TEST_F(PeerConnectionInterfaceTest, UpdateRemoteStreams) {
2664 FakeConstraints constraints;
2665 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2666 true);
2667 CreatePeerConnection(&constraints);
2668 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2669
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002670 rtc::scoped_refptr<StreamCollection> reference(CreateStreamCollection(1, 1));
deadbeefab9b2d12015-10-14 11:33:11 -07002671 EXPECT_TRUE(
2672 CompareStreamCollections(observer_.remote_streams(), reference.get()));
2673 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2674 EXPECT_TRUE(remote_stream->GetVideoTracks()[0]->GetSource() != nullptr);
2675
2676 // Create a session description based on another SDP with another
2677 // MediaStream.
2678 CreateAndSetRemoteOffer(kSdpStringWithStream1And2);
2679
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002680 rtc::scoped_refptr<StreamCollection> reference2(CreateStreamCollection(2, 1));
deadbeefab9b2d12015-10-14 11:33:11 -07002681 EXPECT_TRUE(
2682 CompareStreamCollections(observer_.remote_streams(), reference2.get()));
2683}
2684
2685// This test verifies that when remote tracks are added/removed from SDP, the
2686// created remote streams are updated appropriately.
2687TEST_F(PeerConnectionInterfaceTest,
2688 AddRemoveTrackFromExistingRemoteMediaStream) {
2689 FakeConstraints constraints;
2690 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2691 true);
2692 CreatePeerConnection(&constraints);
kwibergd1fe2812016-04-27 06:47:29 -07002693 std::unique_ptr<SessionDescriptionInterface> desc_ms1 =
kwiberg2bbff992016-03-16 11:03:04 -07002694 CreateSessionDescriptionAndReference(1, 1);
Steve Antondb45ca82017-09-11 18:27:34 -07002695 EXPECT_TRUE(DoSetRemoteDescription(std::move(desc_ms1)));
deadbeefab9b2d12015-10-14 11:33:11 -07002696 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
2697 reference_collection_));
2698
2699 // Add extra audio and video tracks to the same MediaStream.
kwibergd1fe2812016-04-27 06:47:29 -07002700 std::unique_ptr<SessionDescriptionInterface> desc_ms1_two_tracks =
kwiberg2bbff992016-03-16 11:03:04 -07002701 CreateSessionDescriptionAndReference(2, 2);
Steve Antondb45ca82017-09-11 18:27:34 -07002702 EXPECT_TRUE(DoSetRemoteDescription(std::move(desc_ms1_two_tracks)));
deadbeefab9b2d12015-10-14 11:33:11 -07002703 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
2704 reference_collection_));
zhihuang9763d562016-08-05 11:14:50 -07002705 rtc::scoped_refptr<AudioTrackInterface> audio_track2 =
perkjd61bf802016-03-24 03:16:19 -07002706 observer_.remote_streams()->at(0)->GetAudioTracks()[1];
2707 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, audio_track2->state());
zhihuang9763d562016-08-05 11:14:50 -07002708 rtc::scoped_refptr<VideoTrackInterface> video_track2 =
perkjd61bf802016-03-24 03:16:19 -07002709 observer_.remote_streams()->at(0)->GetVideoTracks()[1];
2710 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, video_track2->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002711
2712 // Remove the extra audio and video tracks.
kwibergd1fe2812016-04-27 06:47:29 -07002713 std::unique_ptr<SessionDescriptionInterface> desc_ms2 =
kwiberg2bbff992016-03-16 11:03:04 -07002714 CreateSessionDescriptionAndReference(1, 1);
perkjd61bf802016-03-24 03:16:19 -07002715 MockTrackObserver audio_track_observer(audio_track2);
2716 MockTrackObserver video_track_observer(video_track2);
2717
2718 EXPECT_CALL(audio_track_observer, OnChanged()).Times(Exactly(1));
2719 EXPECT_CALL(video_track_observer, OnChanged()).Times(Exactly(1));
Steve Antondb45ca82017-09-11 18:27:34 -07002720 EXPECT_TRUE(DoSetRemoteDescription(std::move(desc_ms2)));
deadbeefab9b2d12015-10-14 11:33:11 -07002721 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
2722 reference_collection_));
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002723 // Track state may be updated asynchronously.
perkjd61bf802016-03-24 03:16:19 -07002724 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002725 audio_track2->state(), kTimeout);
2726 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
2727 video_track2->state(), kTimeout);
deadbeefab9b2d12015-10-14 11:33:11 -07002728}
2729
2730// This tests that remote tracks are ended if a local session description is set
2731// that rejects the media content type.
2732TEST_F(PeerConnectionInterfaceTest, RejectMediaContent) {
2733 FakeConstraints constraints;
2734 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2735 true);
2736 CreatePeerConnection(&constraints);
2737 // First create and set a remote offer, then reject its video content in our
2738 // answer.
2739 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2740 ASSERT_EQ(1u, observer_.remote_streams()->count());
2741 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2742 ASSERT_EQ(1u, remote_stream->GetVideoTracks().size());
2743 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2744
2745 rtc::scoped_refptr<webrtc::VideoTrackInterface> remote_video =
2746 remote_stream->GetVideoTracks()[0];
2747 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, remote_video->state());
2748 rtc::scoped_refptr<webrtc::AudioTrackInterface> remote_audio =
2749 remote_stream->GetAudioTracks()[0];
2750 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, remote_audio->state());
2751
kwibergd1fe2812016-04-27 06:47:29 -07002752 std::unique_ptr<SessionDescriptionInterface> local_answer;
kwiberg2bbff992016-03-16 11:03:04 -07002753 EXPECT_TRUE(DoCreateAnswer(&local_answer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07002754 cricket::ContentInfo* video_info =
2755 local_answer->description()->GetContentByName("video");
2756 video_info->rejected = true;
Steve Antondb45ca82017-09-11 18:27:34 -07002757 EXPECT_TRUE(DoSetLocalDescription(std::move(local_answer)));
deadbeefab9b2d12015-10-14 11:33:11 -07002758 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kEnded, remote_video->state());
2759 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, remote_audio->state());
2760
2761 // Now create an offer where we reject both video and audio.
kwibergd1fe2812016-04-27 06:47:29 -07002762 std::unique_ptr<SessionDescriptionInterface> local_offer;
kwiberg2bbff992016-03-16 11:03:04 -07002763 EXPECT_TRUE(DoCreateOffer(&local_offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07002764 video_info = local_offer->description()->GetContentByName("video");
2765 ASSERT_TRUE(video_info != nullptr);
2766 video_info->rejected = true;
2767 cricket::ContentInfo* audio_info =
2768 local_offer->description()->GetContentByName("audio");
2769 ASSERT_TRUE(audio_info != nullptr);
2770 audio_info->rejected = true;
Steve Antondb45ca82017-09-11 18:27:34 -07002771 EXPECT_TRUE(DoSetLocalDescription(std::move(local_offer)));
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002772 // Track state may be updated asynchronously.
perkjd61bf802016-03-24 03:16:19 -07002773 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002774 remote_audio->state(), kTimeout);
2775 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
2776 remote_video->state(), kTimeout);
deadbeefab9b2d12015-10-14 11:33:11 -07002777}
2778
2779// This tests that we won't crash if the remote track has been removed outside
2780// of PeerConnection and then PeerConnection tries to reject the track.
2781TEST_F(PeerConnectionInterfaceTest, RemoveTrackThenRejectMediaContent) {
2782 FakeConstraints constraints;
2783 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2784 true);
2785 CreatePeerConnection(&constraints);
2786 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2787 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2788 remote_stream->RemoveTrack(remote_stream->GetVideoTracks()[0]);
2789 remote_stream->RemoveTrack(remote_stream->GetAudioTracks()[0]);
2790
kwibergd1fe2812016-04-27 06:47:29 -07002791 std::unique_ptr<SessionDescriptionInterface> local_answer(
deadbeefab9b2d12015-10-14 11:33:11 -07002792 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
2793 kSdpStringWithStream1, nullptr));
2794 cricket::ContentInfo* video_info =
2795 local_answer->description()->GetContentByName("video");
2796 video_info->rejected = true;
2797 cricket::ContentInfo* audio_info =
2798 local_answer->description()->GetContentByName("audio");
2799 audio_info->rejected = true;
Steve Antondb45ca82017-09-11 18:27:34 -07002800 EXPECT_TRUE(DoSetLocalDescription(std::move(local_answer)));
deadbeefab9b2d12015-10-14 11:33:11 -07002801
2802 // No crash is a pass.
2803}
2804
deadbeef5e97fb52015-10-15 12:49:08 -07002805// This tests that if a recvonly remote description is set, no remote streams
2806// will be created, even if the description contains SSRCs/MSIDs.
2807// See: https://code.google.com/p/webrtc/issues/detail?id=5054
2808TEST_F(PeerConnectionInterfaceTest, RecvonlyDescriptionDoesntCreateStream) {
2809 FakeConstraints constraints;
2810 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2811 true);
2812 CreatePeerConnection(&constraints);
2813
2814 std::string recvonly_offer = kSdpStringWithStream1;
2815 rtc::replace_substrs(kSendrecv, strlen(kSendrecv), kRecvonly,
2816 strlen(kRecvonly), &recvonly_offer);
2817 CreateAndSetRemoteOffer(recvonly_offer);
2818
2819 EXPECT_EQ(0u, observer_.remote_streams()->count());
2820}
2821
deadbeefab9b2d12015-10-14 11:33:11 -07002822// This tests that a default MediaStream is created if a remote session
2823// description doesn't contain any streams and no MSID support.
2824// It also tests that the default stream is updated if a video m-line is added
2825// in a subsequent session description.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002826TEST_F(PeerConnectionInterfaceTest, SdpWithoutMsidCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002827 FakeConstraints constraints;
2828 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2829 true);
2830 CreatePeerConnection(&constraints);
2831 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2832
2833 ASSERT_EQ(1u, observer_.remote_streams()->count());
2834 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2835
2836 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2837 EXPECT_EQ(0u, remote_stream->GetVideoTracks().size());
2838 EXPECT_EQ("default", remote_stream->label());
2839
2840 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2841 ASSERT_EQ(1u, observer_.remote_streams()->count());
2842 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2843 EXPECT_EQ("defaulta0", remote_stream->GetAudioTracks()[0]->id());
deadbeef884f5852016-01-15 09:20:04 -08002844 EXPECT_EQ(MediaStreamTrackInterface::kLive,
2845 remote_stream->GetAudioTracks()[0]->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002846 ASSERT_EQ(1u, remote_stream->GetVideoTracks().size());
2847 EXPECT_EQ("defaultv0", remote_stream->GetVideoTracks()[0]->id());
deadbeef884f5852016-01-15 09:20:04 -08002848 EXPECT_EQ(MediaStreamTrackInterface::kLive,
2849 remote_stream->GetVideoTracks()[0]->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002850}
2851
2852// This tests that a default MediaStream is created if a remote session
2853// description doesn't contain any streams and media direction is send only.
2854TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002855 SendOnlySdpWithoutMsidCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002856 FakeConstraints constraints;
2857 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2858 true);
2859 CreatePeerConnection(&constraints);
2860 CreateAndSetRemoteOffer(kSdpStringSendOnlyWithoutStreams);
2861
2862 ASSERT_EQ(1u, observer_.remote_streams()->count());
2863 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2864
2865 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2866 EXPECT_EQ(1u, remote_stream->GetVideoTracks().size());
2867 EXPECT_EQ("default", remote_stream->label());
2868}
2869
2870// This tests that it won't crash when PeerConnection tries to remove
2871// a remote track that as already been removed from the MediaStream.
2872TEST_F(PeerConnectionInterfaceTest, RemoveAlreadyGoneRemoteStream) {
2873 FakeConstraints constraints;
2874 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2875 true);
2876 CreatePeerConnection(&constraints);
2877 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2878 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2879 remote_stream->RemoveTrack(remote_stream->GetAudioTracks()[0]);
2880 remote_stream->RemoveTrack(remote_stream->GetVideoTracks()[0]);
2881
2882 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2883
2884 // No crash is a pass.
2885}
2886
2887// This tests that a default MediaStream is created if the remote session
2888// description doesn't contain any streams and don't contain an indication if
2889// MSID is supported.
2890TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002891 SdpWithoutMsidAndStreamsCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002892 FakeConstraints constraints;
2893 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2894 true);
2895 CreatePeerConnection(&constraints);
2896 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2897
2898 ASSERT_EQ(1u, observer_.remote_streams()->count());
2899 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2900 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2901 EXPECT_EQ(1u, remote_stream->GetVideoTracks().size());
2902}
2903
2904// This tests that a default MediaStream is not created if the remote session
2905// description doesn't contain any streams but does support MSID.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002906TEST_F(PeerConnectionInterfaceTest, SdpWithMsidDontCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002907 FakeConstraints constraints;
2908 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2909 true);
2910 CreatePeerConnection(&constraints);
2911 CreateAndSetRemoteOffer(kSdpStringWithMsidWithoutStreams);
2912 EXPECT_EQ(0u, observer_.remote_streams()->count());
2913}
2914
deadbeefbda7e0b2015-12-08 17:13:40 -08002915// This tests that when setting a new description, the old default tracks are
2916// not destroyed and recreated.
2917// See: https://bugs.chromium.org/p/webrtc/issues/detail?id=5250
Stefan Holmer102362b2016-03-18 09:39:07 +01002918TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002919 DefaultTracksNotDestroyedAndRecreated) {
deadbeefbda7e0b2015-12-08 17:13:40 -08002920 FakeConstraints constraints;
2921 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2922 true);
2923 CreatePeerConnection(&constraints);
2924 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2925
2926 ASSERT_EQ(1u, observer_.remote_streams()->count());
2927 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2928 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2929
2930 // Set the track to "disabled", then set a new description and ensure the
2931 // track is still disabled, which ensures it hasn't been recreated.
2932 remote_stream->GetAudioTracks()[0]->set_enabled(false);
2933 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2934 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2935 EXPECT_FALSE(remote_stream->GetAudioTracks()[0]->enabled());
2936}
2937
deadbeefab9b2d12015-10-14 11:33:11 -07002938// This tests that a default MediaStream is not created if a remote session
2939// description is updated to not have any MediaStreams.
2940TEST_F(PeerConnectionInterfaceTest, VerifyDefaultStreamIsNotCreated) {
2941 FakeConstraints constraints;
2942 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2943 true);
2944 CreatePeerConnection(&constraints);
2945 CreateAndSetRemoteOffer(kSdpStringWithStream1);
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002946 rtc::scoped_refptr<StreamCollection> reference(CreateStreamCollection(1, 1));
deadbeefab9b2d12015-10-14 11:33:11 -07002947 EXPECT_TRUE(
2948 CompareStreamCollections(observer_.remote_streams(), reference.get()));
2949
2950 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2951 EXPECT_EQ(0u, observer_.remote_streams()->count());
2952}
2953
2954// This tests that an RtpSender is created when the local description is set
2955// after adding a local stream.
2956// TODO(deadbeef): This test and the one below it need to be updated when
2957// an RtpSender's lifetime isn't determined by when a local description is set.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002958TEST_F(PeerConnectionInterfaceTest, LocalDescriptionChanged) {
deadbeefab9b2d12015-10-14 11:33:11 -07002959 FakeConstraints constraints;
2960 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2961 true);
2962 CreatePeerConnection(&constraints);
deadbeefab9b2d12015-10-14 11:33:11 -07002963
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002964 // Create an offer with 1 stream with 2 tracks of each type.
2965 rtc::scoped_refptr<StreamCollection> stream_collection =
2966 CreateStreamCollection(1, 2);
2967 pc_->AddStream(stream_collection->at(0));
2968 std::unique_ptr<SessionDescriptionInterface> offer;
2969 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07002970 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07002971
deadbeefab9b2d12015-10-14 11:33:11 -07002972 auto senders = pc_->GetSenders();
2973 EXPECT_EQ(4u, senders.size());
2974 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2975 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2976 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[1]));
2977 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[1]));
2978
2979 // Remove an audio and video track.
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002980 pc_->RemoveStream(stream_collection->at(0));
2981 stream_collection = CreateStreamCollection(1, 1);
2982 pc_->AddStream(stream_collection->at(0));
2983 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07002984 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002985
deadbeefab9b2d12015-10-14 11:33:11 -07002986 senders = pc_->GetSenders();
2987 EXPECT_EQ(2u, senders.size());
2988 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2989 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2990 EXPECT_FALSE(ContainsSender(senders, kAudioTracks[1]));
2991 EXPECT_FALSE(ContainsSender(senders, kVideoTracks[1]));
2992}
2993
2994// This tests that an RtpSender is created when the local description is set
2995// before adding a local stream.
2996TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002997 AddLocalStreamAfterLocalDescriptionChanged) {
deadbeefab9b2d12015-10-14 11:33:11 -07002998 FakeConstraints constraints;
2999 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
3000 true);
3001 CreatePeerConnection(&constraints);
deadbeefab9b2d12015-10-14 11:33:11 -07003002
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003003 rtc::scoped_refptr<StreamCollection> stream_collection =
3004 CreateStreamCollection(1, 2);
3005 // Add a stream to create the offer, but remove it afterwards.
3006 pc_->AddStream(stream_collection->at(0));
3007 std::unique_ptr<SessionDescriptionInterface> offer;
3008 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
3009 pc_->RemoveStream(stream_collection->at(0));
deadbeefab9b2d12015-10-14 11:33:11 -07003010
Steve Antondb45ca82017-09-11 18:27:34 -07003011 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07003012 auto senders = pc_->GetSenders();
3013 EXPECT_EQ(0u, senders.size());
3014
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003015 pc_->AddStream(stream_collection->at(0));
deadbeefab9b2d12015-10-14 11:33:11 -07003016 senders = pc_->GetSenders();
3017 EXPECT_EQ(4u, senders.size());
3018 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
3019 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
3020 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[1]));
3021 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[1]));
3022}
3023
3024// This tests that the expected behavior occurs if the SSRC on a local track is
3025// changed when SetLocalDescription is called.
3026TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07003027 ChangeSsrcOnTrackInLocalSessionDescription) {
deadbeefab9b2d12015-10-14 11:33:11 -07003028 FakeConstraints constraints;
3029 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
3030 true);
3031 CreatePeerConnection(&constraints);
deadbeefab9b2d12015-10-14 11:33:11 -07003032
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003033 rtc::scoped_refptr<StreamCollection> stream_collection =
3034 CreateStreamCollection(2, 1);
3035 pc_->AddStream(stream_collection->at(0));
3036 std::unique_ptr<SessionDescriptionInterface> offer;
3037 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
3038 // Grab a copy of the offer before it gets passed into the PC.
Steve Antondb45ca82017-09-11 18:27:34 -07003039 std::unique_ptr<webrtc::JsepSessionDescription> modified_offer(
3040 new webrtc::JsepSessionDescription(SessionDescriptionInterface::kOffer));
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003041 modified_offer->Initialize(offer->description()->Copy(), offer->session_id(),
3042 offer->session_version());
Steve Antondb45ca82017-09-11 18:27:34 -07003043 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07003044
deadbeefab9b2d12015-10-14 11:33:11 -07003045 auto senders = pc_->GetSenders();
3046 EXPECT_EQ(2u, senders.size());
3047 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
3048 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
3049
3050 // Change the ssrc of the audio and video track.
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003051 cricket::MediaContentDescription* desc =
3052 cricket::GetFirstAudioContentDescription(modified_offer->description());
3053 ASSERT_TRUE(desc != NULL);
3054 for (StreamParams& stream : desc->mutable_streams()) {
3055 for (unsigned int& ssrc : stream.ssrcs) {
3056 ++ssrc;
3057 }
3058 }
deadbeefab9b2d12015-10-14 11:33:11 -07003059
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003060 desc =
3061 cricket::GetFirstVideoContentDescription(modified_offer->description());
3062 ASSERT_TRUE(desc != NULL);
3063 for (StreamParams& stream : desc->mutable_streams()) {
3064 for (unsigned int& ssrc : stream.ssrcs) {
3065 ++ssrc;
3066 }
3067 }
3068
Steve Antondb45ca82017-09-11 18:27:34 -07003069 EXPECT_TRUE(DoSetLocalDescription(std::move(modified_offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07003070 senders = pc_->GetSenders();
3071 EXPECT_EQ(2u, senders.size());
3072 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
3073 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
3074 // TODO(deadbeef): Once RtpSenders expose parameters, check that the SSRC
3075 // changed.
3076}
3077
3078// This tests that the expected behavior occurs if a new session description is
3079// set with the same tracks, but on a different MediaStream.
Stefan Holmer55d6e7c2016-03-17 16:26:40 +01003080TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07003081 SignalSameTracksInSeparateMediaStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07003082 FakeConstraints constraints;
3083 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
3084 true);
3085 CreatePeerConnection(&constraints);
deadbeefab9b2d12015-10-14 11:33:11 -07003086
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003087 rtc::scoped_refptr<StreamCollection> stream_collection =
3088 CreateStreamCollection(2, 1);
3089 pc_->AddStream(stream_collection->at(0));
3090 std::unique_ptr<SessionDescriptionInterface> offer;
3091 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07003092 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07003093
deadbeefab9b2d12015-10-14 11:33:11 -07003094 auto senders = pc_->GetSenders();
3095 EXPECT_EQ(2u, senders.size());
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003096 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0], kStreams[0]));
3097 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0], kStreams[0]));
deadbeefab9b2d12015-10-14 11:33:11 -07003098
3099 // Add a new MediaStream but with the same tracks as in the first stream.
3100 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream_1(
3101 webrtc::MediaStream::Create(kStreams[1]));
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003102 stream_1->AddTrack(stream_collection->at(0)->GetVideoTracks()[0]);
3103 stream_1->AddTrack(stream_collection->at(0)->GetAudioTracks()[0]);
deadbeefab9b2d12015-10-14 11:33:11 -07003104 pc_->AddStream(stream_1);
3105
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003106 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07003107 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07003108
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003109 auto new_senders = pc_->GetSenders();
3110 // Should be the same senders as before, but with updated stream id.
3111 // Note that this behavior is subject to change in the future.
3112 // We may decide the PC should ignore existing tracks in AddStream.
3113 EXPECT_EQ(senders, new_senders);
3114 EXPECT_TRUE(ContainsSender(new_senders, kAudioTracks[0], kStreams[1]));
3115 EXPECT_TRUE(ContainsSender(new_senders, kVideoTracks[0], kStreams[1]));
deadbeefab9b2d12015-10-14 11:33:11 -07003116}
3117
zhihuang81c3a032016-11-17 12:06:24 -08003118// This tests that PeerConnectionObserver::OnAddTrack is correctly called.
3119TEST_F(PeerConnectionInterfaceTest, OnAddTrackCallback) {
3120 FakeConstraints constraints;
3121 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
3122 true);
3123 CreatePeerConnection(&constraints);
3124 CreateAndSetRemoteOffer(kSdpStringWithStream1AudioTrackOnly);
3125 EXPECT_EQ(observer_.num_added_tracks_, 1);
3126 EXPECT_EQ(observer_.last_added_track_label_, kAudioTracks[0]);
3127
3128 // Create and set the updated remote SDP.
3129 CreateAndSetRemoteOffer(kSdpStringWithStream1);
3130 EXPECT_EQ(observer_.num_added_tracks_, 2);
3131 EXPECT_EQ(observer_.last_added_track_label_, kVideoTracks[0]);
3132}
3133
deadbeefd1a38b52016-12-10 13:15:33 -08003134// Test that when SetConfiguration is called and the configuration is
3135// changing, the next offer causes an ICE restart.
3136TEST_F(PeerConnectionInterfaceTest, SetConfigurationCausingIceRetart) {
3137 PeerConnectionInterface::RTCConfiguration config;
3138 config.type = PeerConnectionInterface::kRelay;
3139 // Need to pass default constraints to prevent disabling of DTLS...
3140 FakeConstraints default_constraints;
3141 CreatePeerConnection(config, &default_constraints);
3142 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
3143
3144 // Do initial offer/answer so there's something to restart.
3145 CreateOfferAsLocalDescription();
3146 CreateAnswerAsRemoteDescription(kSdpStringWithStream1);
3147
3148 // Grab the ufrags.
3149 std::vector<std::string> initial_ufrags = GetUfrags(pc_->local_description());
3150
3151 // Change ICE policy, which should trigger an ICE restart on the next offer.
3152 config.type = PeerConnectionInterface::kAll;
3153 EXPECT_TRUE(pc_->SetConfiguration(config));
3154 CreateOfferAsLocalDescription();
3155
3156 // Grab the new ufrags.
3157 std::vector<std::string> subsequent_ufrags =
3158 GetUfrags(pc_->local_description());
3159
3160 // Sanity check.
3161 EXPECT_EQ(initial_ufrags.size(), subsequent_ufrags.size());
3162 // Check that each ufrag is different.
3163 for (int i = 0; i < static_cast<int>(initial_ufrags.size()); ++i) {
3164 EXPECT_NE(initial_ufrags[i], subsequent_ufrags[i]);
3165 }
3166}
3167
3168// Test that when SetConfiguration is called and the configuration *isn't*
3169// changing, the next offer does *not* cause an ICE restart.
3170TEST_F(PeerConnectionInterfaceTest, SetConfigurationNotCausingIceRetart) {
3171 PeerConnectionInterface::RTCConfiguration config;
3172 config.type = PeerConnectionInterface::kRelay;
3173 // Need to pass default constraints to prevent disabling of DTLS...
3174 FakeConstraints default_constraints;
3175 CreatePeerConnection(config, &default_constraints);
3176 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
3177
3178 // Do initial offer/answer so there's something to restart.
3179 CreateOfferAsLocalDescription();
3180 CreateAnswerAsRemoteDescription(kSdpStringWithStream1);
3181
3182 // Grab the ufrags.
3183 std::vector<std::string> initial_ufrags = GetUfrags(pc_->local_description());
3184
3185 // Call SetConfiguration with a config identical to what the PC was
3186 // constructed with.
3187 EXPECT_TRUE(pc_->SetConfiguration(config));
3188 CreateOfferAsLocalDescription();
3189
3190 // Grab the new ufrags.
3191 std::vector<std::string> subsequent_ufrags =
3192 GetUfrags(pc_->local_description());
3193
3194 EXPECT_EQ(initial_ufrags, subsequent_ufrags);
3195}
3196
3197// Test for a weird corner case scenario:
3198// 1. Audio/video session established.
3199// 2. SetConfiguration changes ICE config; ICE restart needed.
3200// 3. ICE restart initiated by remote peer, but only for one m= section.
3201// 4. Next createOffer should initiate an ICE restart, but only for the other
3202// m= section; it would be pointless to do an ICE restart for the m= section
3203// that was already restarted.
3204TEST_F(PeerConnectionInterfaceTest, SetConfigurationCausingPartialIceRestart) {
3205 PeerConnectionInterface::RTCConfiguration config;
3206 config.type = PeerConnectionInterface::kRelay;
3207 // Need to pass default constraints to prevent disabling of DTLS...
3208 FakeConstraints default_constraints;
3209 CreatePeerConnection(config, &default_constraints);
3210 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
3211
3212 // Do initial offer/answer so there's something to restart.
3213 CreateOfferAsLocalDescription();
3214 CreateAnswerAsRemoteDescription(kSdpStringWithStream1);
3215
3216 // Change ICE policy, which should set the "needs-ice-restart" flag.
3217 config.type = PeerConnectionInterface::kAll;
3218 EXPECT_TRUE(pc_->SetConfiguration(config));
3219
3220 // Do ICE restart for the first m= section, initiated by remote peer.
Steve Antondb45ca82017-09-11 18:27:34 -07003221 std::unique_ptr<webrtc::SessionDescriptionInterface> remote_offer(
3222 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
3223 kSdpStringWithStream1, nullptr));
3224 ASSERT_TRUE(remote_offer);
deadbeefd1a38b52016-12-10 13:15:33 -08003225 remote_offer->description()->transport_infos()[0].description.ice_ufrag =
3226 "modified";
Steve Antondb45ca82017-09-11 18:27:34 -07003227 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_offer)));
deadbeefd1a38b52016-12-10 13:15:33 -08003228 CreateAnswerAsLocalDescription();
3229
3230 // Grab the ufrags.
3231 std::vector<std::string> initial_ufrags = GetUfrags(pc_->local_description());
3232 ASSERT_EQ(2, initial_ufrags.size());
3233
3234 // Create offer and grab the new ufrags.
3235 CreateOfferAsLocalDescription();
3236 std::vector<std::string> subsequent_ufrags =
3237 GetUfrags(pc_->local_description());
3238 ASSERT_EQ(2, subsequent_ufrags.size());
3239
3240 // Ensure that only the ufrag for the second m= section changed.
3241 EXPECT_EQ(initial_ufrags[0], subsequent_ufrags[0]);
3242 EXPECT_NE(initial_ufrags[1], subsequent_ufrags[1]);
3243}
3244
deadbeeffe4a8a42016-12-20 17:56:17 -08003245// Tests that the methods to return current/pending descriptions work as
3246// expected at different points in the offer/answer exchange. This test does
3247// one offer/answer exchange as the offerer, then another as the answerer.
3248TEST_F(PeerConnectionInterfaceTest, CurrentAndPendingDescriptions) {
3249 // This disables DTLS so we can apply an answer to ourselves.
3250 CreatePeerConnection();
3251
3252 // Create initial local offer and get SDP (which will also be used as
3253 // answer/pranswer);
Steve Antondb45ca82017-09-11 18:27:34 -07003254 std::unique_ptr<SessionDescriptionInterface> local_offer;
3255 ASSERT_TRUE(DoCreateOffer(&local_offer, nullptr));
deadbeeffe4a8a42016-12-20 17:56:17 -08003256 std::string sdp;
Steve Antondb45ca82017-09-11 18:27:34 -07003257 EXPECT_TRUE(local_offer->ToString(&sdp));
deadbeeffe4a8a42016-12-20 17:56:17 -08003258
3259 // Set local offer.
Steve Antondb45ca82017-09-11 18:27:34 -07003260 SessionDescriptionInterface* local_offer_ptr = local_offer.get();
3261 EXPECT_TRUE(DoSetLocalDescription(std::move(local_offer)));
3262 EXPECT_EQ(local_offer_ptr, pc_->pending_local_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003263 EXPECT_EQ(nullptr, pc_->pending_remote_description());
3264 EXPECT_EQ(nullptr, pc_->current_local_description());
3265 EXPECT_EQ(nullptr, pc_->current_remote_description());
3266
3267 // Set remote pranswer.
Steve Antondb45ca82017-09-11 18:27:34 -07003268 std::unique_ptr<SessionDescriptionInterface> remote_pranswer(
deadbeeffe4a8a42016-12-20 17:56:17 -08003269 webrtc::CreateSessionDescription(SessionDescriptionInterface::kPrAnswer,
Steve Antondb45ca82017-09-11 18:27:34 -07003270 sdp, nullptr));
3271 SessionDescriptionInterface* remote_pranswer_ptr = remote_pranswer.get();
3272 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_pranswer)));
3273 EXPECT_EQ(local_offer_ptr, pc_->pending_local_description());
3274 EXPECT_EQ(remote_pranswer_ptr, pc_->pending_remote_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003275 EXPECT_EQ(nullptr, pc_->current_local_description());
3276 EXPECT_EQ(nullptr, pc_->current_remote_description());
3277
3278 // Set remote answer.
Steve Antondb45ca82017-09-11 18:27:34 -07003279 std::unique_ptr<SessionDescriptionInterface> remote_answer(
3280 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
3281 sdp, nullptr));
3282 SessionDescriptionInterface* remote_answer_ptr = remote_answer.get();
3283 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_answer)));
deadbeeffe4a8a42016-12-20 17:56:17 -08003284 EXPECT_EQ(nullptr, pc_->pending_local_description());
3285 EXPECT_EQ(nullptr, pc_->pending_remote_description());
Steve Antondb45ca82017-09-11 18:27:34 -07003286 EXPECT_EQ(local_offer_ptr, pc_->current_local_description());
3287 EXPECT_EQ(remote_answer_ptr, pc_->current_remote_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003288
3289 // Set remote offer.
Steve Antondb45ca82017-09-11 18:27:34 -07003290 std::unique_ptr<SessionDescriptionInterface> remote_offer(
3291 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer, sdp,
3292 nullptr));
3293 SessionDescriptionInterface* remote_offer_ptr = remote_offer.get();
3294 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_offer)));
3295 EXPECT_EQ(remote_offer_ptr, pc_->pending_remote_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003296 EXPECT_EQ(nullptr, pc_->pending_local_description());
Steve Antondb45ca82017-09-11 18:27:34 -07003297 EXPECT_EQ(local_offer_ptr, pc_->current_local_description());
3298 EXPECT_EQ(remote_answer_ptr, pc_->current_remote_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003299
3300 // Set local pranswer.
Steve Antondb45ca82017-09-11 18:27:34 -07003301 std::unique_ptr<SessionDescriptionInterface> local_pranswer(
deadbeeffe4a8a42016-12-20 17:56:17 -08003302 webrtc::CreateSessionDescription(SessionDescriptionInterface::kPrAnswer,
Steve Antondb45ca82017-09-11 18:27:34 -07003303 sdp, nullptr));
3304 SessionDescriptionInterface* local_pranswer_ptr = local_pranswer.get();
3305 EXPECT_TRUE(DoSetLocalDescription(std::move(local_pranswer)));
3306 EXPECT_EQ(remote_offer_ptr, pc_->pending_remote_description());
3307 EXPECT_EQ(local_pranswer_ptr, pc_->pending_local_description());
3308 EXPECT_EQ(local_offer_ptr, pc_->current_local_description());
3309 EXPECT_EQ(remote_answer_ptr, pc_->current_remote_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003310
3311 // Set local answer.
Steve Antondb45ca82017-09-11 18:27:34 -07003312 std::unique_ptr<SessionDescriptionInterface> local_answer(
3313 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
3314 sdp, nullptr));
3315 SessionDescriptionInterface* local_answer_ptr = local_answer.get();
3316 EXPECT_TRUE(DoSetLocalDescription(std::move(local_answer)));
deadbeeffe4a8a42016-12-20 17:56:17 -08003317 EXPECT_EQ(nullptr, pc_->pending_remote_description());
3318 EXPECT_EQ(nullptr, pc_->pending_local_description());
Steve Antondb45ca82017-09-11 18:27:34 -07003319 EXPECT_EQ(remote_offer_ptr, pc_->current_remote_description());
3320 EXPECT_EQ(local_answer_ptr, pc_->current_local_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003321}
3322
zhihuang77985012017-02-07 15:45:16 -08003323// Tests that it won't crash when calling StartRtcEventLog or StopRtcEventLog
3324// after the PeerConnection is closed.
3325TEST_F(PeerConnectionInterfaceTest,
3326 StartAndStopLoggingAfterPeerConnectionClosed) {
3327 CreatePeerConnection();
3328 // The RtcEventLog will be reset when the PeerConnection is closed.
3329 pc_->Close();
3330
3331 rtc::PlatformFile file = 0;
3332 int64_t max_size_bytes = 1024;
3333 EXPECT_FALSE(pc_->StartRtcEventLog(file, max_size_bytes));
3334 pc_->StopRtcEventLog();
3335}
3336
deadbeef30952b42017-04-21 02:41:29 -07003337// Test that generated offers/answers include "ice-option:trickle".
3338TEST_F(PeerConnectionInterfaceTest, OffersAndAnswersHaveTrickleIceOption) {
3339 CreatePeerConnection();
3340
3341 // First, create an offer with audio/video.
3342 FakeConstraints constraints;
3343 constraints.SetMandatoryReceiveAudio(true);
3344 constraints.SetMandatoryReceiveVideo(true);
3345 std::unique_ptr<SessionDescriptionInterface> offer;
3346 ASSERT_TRUE(DoCreateOffer(&offer, &constraints));
3347 cricket::SessionDescription* desc = offer->description();
3348 ASSERT_EQ(2u, desc->transport_infos().size());
3349 EXPECT_TRUE(desc->transport_infos()[0].description.HasOption("trickle"));
3350 EXPECT_TRUE(desc->transport_infos()[1].description.HasOption("trickle"));
3351
3352 // Apply the offer as a remote description, then create an answer.
Steve Antondb45ca82017-09-11 18:27:34 -07003353 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
deadbeef30952b42017-04-21 02:41:29 -07003354 std::unique_ptr<SessionDescriptionInterface> answer;
3355 ASSERT_TRUE(DoCreateAnswer(&answer, &constraints));
3356 desc = answer->description();
3357 ASSERT_EQ(2u, desc->transport_infos().size());
3358 EXPECT_TRUE(desc->transport_infos()[0].description.HasOption("trickle"));
3359 EXPECT_TRUE(desc->transport_infos()[1].description.HasOption("trickle"));
3360}
3361
deadbeef1dcb1642017-03-29 21:08:16 -07003362// Test that ICE renomination isn't offered if it's not enabled in the PC's
3363// RTCConfiguration.
3364TEST_F(PeerConnectionInterfaceTest, IceRenominationNotOffered) {
3365 PeerConnectionInterface::RTCConfiguration config;
3366 config.enable_ice_renomination = false;
3367 CreatePeerConnection(config, nullptr);
3368 AddVoiceStream("foo");
3369
3370 std::unique_ptr<SessionDescriptionInterface> offer;
3371 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
3372 cricket::SessionDescription* desc = offer->description();
3373 EXPECT_EQ(1u, desc->transport_infos().size());
3374 EXPECT_FALSE(
3375 desc->transport_infos()[0].description.GetIceParameters().renomination);
3376}
3377
3378// Test that the ICE renomination option is present in generated offers/answers
3379// if it's enabled in the PC's RTCConfiguration.
3380TEST_F(PeerConnectionInterfaceTest, IceRenominationOptionInOfferAndAnswer) {
3381 PeerConnectionInterface::RTCConfiguration config;
3382 config.enable_ice_renomination = true;
3383 CreatePeerConnection(config, nullptr);
3384 AddVoiceStream("foo");
3385
3386 std::unique_ptr<SessionDescriptionInterface> offer;
3387 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
3388 cricket::SessionDescription* desc = offer->description();
3389 EXPECT_EQ(1u, desc->transport_infos().size());
3390 EXPECT_TRUE(
3391 desc->transport_infos()[0].description.GetIceParameters().renomination);
3392
3393 // Set the offer as a remote description, then create an answer and ensure it
3394 // has the renomination flag too.
Steve Antondb45ca82017-09-11 18:27:34 -07003395 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
deadbeef1dcb1642017-03-29 21:08:16 -07003396 std::unique_ptr<SessionDescriptionInterface> answer;
3397 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
3398 desc = answer->description();
3399 EXPECT_EQ(1u, desc->transport_infos().size());
3400 EXPECT_TRUE(
3401 desc->transport_infos()[0].description.GetIceParameters().renomination);
3402}
3403
3404// Test that if CreateOffer is called with the deprecated "offer to receive
3405// audio/video" constraints, they're processed and result in an offer with
3406// audio/video sections just as if RTCOfferAnswerOptions had been used.
3407TEST_F(PeerConnectionInterfaceTest, CreateOfferWithOfferToReceiveConstraints) {
3408 CreatePeerConnection();
3409
3410 FakeConstraints constraints;
3411 constraints.SetMandatoryReceiveAudio(true);
3412 constraints.SetMandatoryReceiveVideo(true);
3413 std::unique_ptr<SessionDescriptionInterface> offer;
3414 ASSERT_TRUE(DoCreateOffer(&offer, &constraints));
3415
3416 cricket::SessionDescription* desc = offer->description();
3417 const cricket::ContentInfo* audio = cricket::GetFirstAudioContent(desc);
3418 const cricket::ContentInfo* video = cricket::GetFirstVideoContent(desc);
3419 ASSERT_NE(nullptr, audio);
3420 ASSERT_NE(nullptr, video);
3421 EXPECT_FALSE(audio->rejected);
3422 EXPECT_FALSE(video->rejected);
3423}
3424
3425// Test that if CreateAnswer is called with the deprecated "offer to receive
3426// audio/video" constraints, they're processed and can be used to reject an
3427// offered m= section just as can be done with RTCOfferAnswerOptions;
3428TEST_F(PeerConnectionInterfaceTest, CreateAnswerWithOfferToReceiveConstraints) {
3429 CreatePeerConnection();
3430
3431 // First, create an offer with audio/video and apply it as a remote
3432 // description.
3433 FakeConstraints constraints;
3434 constraints.SetMandatoryReceiveAudio(true);
3435 constraints.SetMandatoryReceiveVideo(true);
3436 std::unique_ptr<SessionDescriptionInterface> offer;
3437 ASSERT_TRUE(DoCreateOffer(&offer, &constraints));
Steve Antondb45ca82017-09-11 18:27:34 -07003438 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
deadbeef1dcb1642017-03-29 21:08:16 -07003439
3440 // Now create answer that rejects audio/video.
3441 constraints.SetMandatoryReceiveAudio(false);
3442 constraints.SetMandatoryReceiveVideo(false);
3443 std::unique_ptr<SessionDescriptionInterface> answer;
3444 ASSERT_TRUE(DoCreateAnswer(&answer, &constraints));
3445
3446 cricket::SessionDescription* desc = answer->description();
3447 const cricket::ContentInfo* audio = cricket::GetFirstAudioContent(desc);
3448 const cricket::ContentInfo* video = cricket::GetFirstVideoContent(desc);
3449 ASSERT_NE(nullptr, audio);
3450 ASSERT_NE(nullptr, video);
3451 EXPECT_TRUE(audio->rejected);
3452 EXPECT_TRUE(video->rejected);
3453}
3454
3455#ifdef HAVE_SCTP
3456#define MAYBE_DataChannelOnlyOfferWithMaxBundlePolicy \
3457 DataChannelOnlyOfferWithMaxBundlePolicy
3458#else
3459#define MAYBE_DataChannelOnlyOfferWithMaxBundlePolicy \
3460 DISABLED_DataChannelOnlyOfferWithMaxBundlePolicy
3461#endif
3462
3463// Test that negotiation can succeed with a data channel only, and with the max
3464// bundle policy. Previously there was a bug that prevented this.
3465TEST_F(PeerConnectionInterfaceTest,
3466 MAYBE_DataChannelOnlyOfferWithMaxBundlePolicy) {
3467 PeerConnectionInterface::RTCConfiguration config;
3468 config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
3469 CreatePeerConnection(config, nullptr);
3470
3471 // First, create an offer with only a data channel and apply it as a remote
3472 // description.
3473 pc_->CreateDataChannel("test", nullptr);
3474 std::unique_ptr<SessionDescriptionInterface> offer;
3475 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07003476 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
deadbeef1dcb1642017-03-29 21:08:16 -07003477
3478 // Create and set answer as well.
3479 std::unique_ptr<SessionDescriptionInterface> answer;
3480 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07003481 EXPECT_TRUE(DoSetLocalDescription(std::move(answer)));
deadbeef1dcb1642017-03-29 21:08:16 -07003482}
3483
zstein4b979802017-06-02 14:37:37 -07003484TEST_F(PeerConnectionInterfaceTest, SetBitrateWithoutMinSucceeds) {
3485 CreatePeerConnection();
3486 PeerConnectionInterface::BitrateParameters bitrate;
3487 bitrate.current_bitrate_bps = rtc::Optional<int>(100000);
3488 EXPECT_TRUE(pc_->SetBitrate(bitrate).ok());
3489}
3490
3491TEST_F(PeerConnectionInterfaceTest, SetBitrateNegativeMinFails) {
3492 CreatePeerConnection();
3493 PeerConnectionInterface::BitrateParameters bitrate;
3494 bitrate.min_bitrate_bps = rtc::Optional<int>(-1);
3495 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3496}
3497
3498TEST_F(PeerConnectionInterfaceTest, SetBitrateCurrentLessThanMinFails) {
3499 CreatePeerConnection();
3500 PeerConnectionInterface::BitrateParameters bitrate;
3501 bitrate.min_bitrate_bps = rtc::Optional<int>(5);
3502 bitrate.current_bitrate_bps = rtc::Optional<int>(3);
3503 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3504}
3505
3506TEST_F(PeerConnectionInterfaceTest, SetBitrateCurrentNegativeFails) {
3507 CreatePeerConnection();
3508 PeerConnectionInterface::BitrateParameters bitrate;
3509 bitrate.current_bitrate_bps = rtc::Optional<int>(-1);
3510 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3511}
3512
3513TEST_F(PeerConnectionInterfaceTest, SetBitrateMaxLessThanCurrentFails) {
3514 CreatePeerConnection();
3515 PeerConnectionInterface::BitrateParameters bitrate;
3516 bitrate.current_bitrate_bps = rtc::Optional<int>(10);
3517 bitrate.max_bitrate_bps = rtc::Optional<int>(8);
3518 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3519}
3520
3521TEST_F(PeerConnectionInterfaceTest, SetBitrateMaxLessThanMinFails) {
3522 CreatePeerConnection();
3523 PeerConnectionInterface::BitrateParameters bitrate;
3524 bitrate.min_bitrate_bps = rtc::Optional<int>(10);
3525 bitrate.max_bitrate_bps = rtc::Optional<int>(8);
3526 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3527}
3528
3529TEST_F(PeerConnectionInterfaceTest, SetBitrateMaxNegativeFails) {
3530 CreatePeerConnection();
3531 PeerConnectionInterface::BitrateParameters bitrate;
3532 bitrate.max_bitrate_bps = rtc::Optional<int>(-1);
3533 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3534}
3535
Steve Anton038834f2017-07-14 15:59:59 -07003536// ice_regather_interval_range requires WebRTC to be configured for continual
3537// gathering already.
3538TEST_F(PeerConnectionInterfaceTest,
3539 SetIceRegatherIntervalRangeWithoutContinualGatheringFails) {
3540 PeerConnectionInterface::RTCConfiguration config;
3541 config.ice_regather_interval_range.emplace(1000, 2000);
3542 config.continual_gathering_policy =
3543 PeerConnectionInterface::ContinualGatheringPolicy::GATHER_ONCE;
3544 CreatePeerConnectionExpectFail(config);
3545}
3546
3547// Ensures that there is no error when ice_regather_interval_range is set with
3548// continual gathering enabled.
3549TEST_F(PeerConnectionInterfaceTest,
3550 SetIceRegatherIntervalRangeWithContinualGathering) {
3551 PeerConnectionInterface::RTCConfiguration config;
3552 config.ice_regather_interval_range.emplace(1000, 2000);
3553 config.continual_gathering_policy =
3554 PeerConnectionInterface::ContinualGatheringPolicy::GATHER_CONTINUALLY;
3555 CreatePeerConnection(config, nullptr);
3556}
3557
zstein4b979802017-06-02 14:37:37 -07003558// The current bitrate from Call's BitrateConfigMask is currently clamped by
3559// Call's BitrateConfig, which comes from the SDP or a default value. This test
3560// checks that a call to SetBitrate with a current bitrate that will be clamped
3561// succeeds.
3562TEST_F(PeerConnectionInterfaceTest, SetBitrateCurrentLessThanImplicitMin) {
3563 CreatePeerConnection();
3564 PeerConnectionInterface::BitrateParameters bitrate;
3565 bitrate.current_bitrate_bps = rtc::Optional<int>(1);
3566 EXPECT_TRUE(pc_->SetBitrate(bitrate).ok());
3567}
3568
zhihuang1c378ed2017-08-17 14:10:50 -07003569// The following tests verify that the offer can be created correctly.
3570TEST_F(PeerConnectionInterfaceTest,
3571 CreateOfferFailsWithInvalidOfferToReceiveAudio) {
3572 RTCOfferAnswerOptions rtc_options;
3573
3574 // Setting offer_to_receive_audio to a value lower than kUndefined or greater
3575 // than kMaxOfferToReceiveMedia should be treated as invalid.
3576 rtc_options.offer_to_receive_audio = RTCOfferAnswerOptions::kUndefined - 1;
3577 CreatePeerConnection();
3578 EXPECT_FALSE(CreateOfferWithOptions(rtc_options));
3579
3580 rtc_options.offer_to_receive_audio =
3581 RTCOfferAnswerOptions::kMaxOfferToReceiveMedia + 1;
3582 EXPECT_FALSE(CreateOfferWithOptions(rtc_options));
3583}
3584
3585TEST_F(PeerConnectionInterfaceTest,
3586 CreateOfferFailsWithInvalidOfferToReceiveVideo) {
3587 RTCOfferAnswerOptions rtc_options;
3588
3589 // Setting offer_to_receive_video to a value lower than kUndefined or greater
3590 // than kMaxOfferToReceiveMedia should be treated as invalid.
3591 rtc_options.offer_to_receive_video = RTCOfferAnswerOptions::kUndefined - 1;
3592 CreatePeerConnection();
3593 EXPECT_FALSE(CreateOfferWithOptions(rtc_options));
3594
3595 rtc_options.offer_to_receive_video =
3596 RTCOfferAnswerOptions::kMaxOfferToReceiveMedia + 1;
3597 EXPECT_FALSE(CreateOfferWithOptions(rtc_options));
3598}
3599
3600// Test that the audio and video content will be added to an offer if both
3601// |offer_to_receive_audio| and |offer_to_receive_video| options are 1.
3602TEST_F(PeerConnectionInterfaceTest, CreateOfferWithAudioVideoOptions) {
3603 RTCOfferAnswerOptions rtc_options;
3604 rtc_options.offer_to_receive_audio = 1;
3605 rtc_options.offer_to_receive_video = 1;
3606
3607 std::unique_ptr<SessionDescriptionInterface> offer;
3608 CreatePeerConnection();
3609 offer = CreateOfferWithOptions(rtc_options);
3610 ASSERT_TRUE(offer);
3611 EXPECT_NE(nullptr, GetFirstAudioContent(offer->description()));
3612 EXPECT_NE(nullptr, GetFirstVideoContent(offer->description()));
3613}
3614
3615// Test that only audio content will be added to the offer if only
3616// |offer_to_receive_audio| options is 1.
3617TEST_F(PeerConnectionInterfaceTest, CreateOfferWithAudioOnlyOptions) {
3618 RTCOfferAnswerOptions rtc_options;
3619 rtc_options.offer_to_receive_audio = 1;
3620 rtc_options.offer_to_receive_video = 0;
3621
3622 std::unique_ptr<SessionDescriptionInterface> offer;
3623 CreatePeerConnection();
3624 offer = CreateOfferWithOptions(rtc_options);
3625 ASSERT_TRUE(offer);
3626 EXPECT_NE(nullptr, GetFirstAudioContent(offer->description()));
3627 EXPECT_EQ(nullptr, GetFirstVideoContent(offer->description()));
3628}
3629
3630// Test that only video content will be added if only |offer_to_receive_video|
3631// options is 1.
3632TEST_F(PeerConnectionInterfaceTest, CreateOfferWithVideoOnlyOptions) {
3633 RTCOfferAnswerOptions rtc_options;
3634 rtc_options.offer_to_receive_audio = 0;
3635 rtc_options.offer_to_receive_video = 1;
3636
3637 std::unique_ptr<SessionDescriptionInterface> offer;
3638 CreatePeerConnection();
3639 offer = CreateOfferWithOptions(rtc_options);
3640 ASSERT_TRUE(offer);
3641 EXPECT_EQ(nullptr, GetFirstAudioContent(offer->description()));
3642 EXPECT_NE(nullptr, GetFirstVideoContent(offer->description()));
3643}
3644
3645// Test that if |voice_activity_detection| is false, no CN codec is added to the
3646// offer.
3647TEST_F(PeerConnectionInterfaceTest, CreateOfferWithVADOptions) {
3648 RTCOfferAnswerOptions rtc_options;
3649 rtc_options.offer_to_receive_audio = 1;
3650 rtc_options.offer_to_receive_video = 0;
3651
3652 std::unique_ptr<SessionDescriptionInterface> offer;
3653 CreatePeerConnection();
3654 offer = CreateOfferWithOptions(rtc_options);
3655 ASSERT_TRUE(offer);
3656 const cricket::ContentInfo* audio_content =
3657 offer->description()->GetContentByName(cricket::CN_AUDIO);
3658 ASSERT_TRUE(audio_content);
3659 // |voice_activity_detection| is true by default.
3660 EXPECT_TRUE(HasCNCodecs(audio_content));
3661
3662 rtc_options.voice_activity_detection = false;
3663 CreatePeerConnection();
3664 offer = CreateOfferWithOptions(rtc_options);
3665 ASSERT_TRUE(offer);
3666 audio_content = offer->description()->GetContentByName(cricket::CN_AUDIO);
3667 ASSERT_TRUE(audio_content);
3668 EXPECT_FALSE(HasCNCodecs(audio_content));
3669}
3670
3671// Test that no media content will be added to the offer if using default
3672// RTCOfferAnswerOptions.
3673TEST_F(PeerConnectionInterfaceTest, CreateOfferWithDefaultOfferAnswerOptions) {
3674 RTCOfferAnswerOptions rtc_options;
3675
3676 std::unique_ptr<SessionDescriptionInterface> offer;
3677 CreatePeerConnection();
3678 offer = CreateOfferWithOptions(rtc_options);
3679 ASSERT_TRUE(offer);
3680 EXPECT_EQ(nullptr, GetFirstAudioContent(offer->description()));
3681 EXPECT_EQ(nullptr, GetFirstVideoContent(offer->description()));
3682}
3683
3684// Test that if |ice_restart| is true, the ufrag/pwd will change, otherwise
3685// ufrag/pwd will be the same in the new offer.
3686TEST_F(PeerConnectionInterfaceTest, CreateOfferWithIceRestart) {
3687 RTCOfferAnswerOptions rtc_options;
3688 rtc_options.ice_restart = false;
3689 rtc_options.offer_to_receive_audio = 1;
3690
3691 std::unique_ptr<SessionDescriptionInterface> offer;
3692 CreatePeerConnection();
3693 CreateOfferWithOptionsAsLocalDescription(&offer, rtc_options);
3694 auto ufrag1 = offer->description()
3695 ->GetTransportInfoByName(cricket::CN_AUDIO)
3696 ->description.ice_ufrag;
3697 auto pwd1 = offer->description()
3698 ->GetTransportInfoByName(cricket::CN_AUDIO)
3699 ->description.ice_pwd;
3700
3701 // |ice_restart| is false, the ufrag/pwd shouldn't change.
3702 CreateOfferWithOptionsAsLocalDescription(&offer, rtc_options);
3703 auto ufrag2 = offer->description()
3704 ->GetTransportInfoByName(cricket::CN_AUDIO)
3705 ->description.ice_ufrag;
3706 auto pwd2 = offer->description()
3707 ->GetTransportInfoByName(cricket::CN_AUDIO)
3708 ->description.ice_pwd;
3709
3710 // |ice_restart| is true, the ufrag/pwd should change.
3711 rtc_options.ice_restart = true;
3712 CreateOfferWithOptionsAsLocalDescription(&offer, rtc_options);
3713 auto ufrag3 = offer->description()
3714 ->GetTransportInfoByName(cricket::CN_AUDIO)
3715 ->description.ice_ufrag;
3716 auto pwd3 = offer->description()
3717 ->GetTransportInfoByName(cricket::CN_AUDIO)
3718 ->description.ice_pwd;
3719
3720 EXPECT_EQ(ufrag1, ufrag2);
3721 EXPECT_EQ(pwd1, pwd2);
3722 EXPECT_NE(ufrag2, ufrag3);
3723 EXPECT_NE(pwd2, pwd3);
3724}
3725
3726// Test that if |use_rtp_mux| is true, the bundling will be enabled in the
3727// offer; if it is false, there won't be any bundle group in the offer.
3728TEST_F(PeerConnectionInterfaceTest, CreateOfferWithRtpMux) {
3729 RTCOfferAnswerOptions rtc_options;
3730 rtc_options.offer_to_receive_audio = 1;
3731 rtc_options.offer_to_receive_video = 1;
3732
3733 std::unique_ptr<SessionDescriptionInterface> offer;
3734 CreatePeerConnection();
3735
3736 rtc_options.use_rtp_mux = true;
3737 offer = CreateOfferWithOptions(rtc_options);
3738 ASSERT_TRUE(offer);
3739 EXPECT_NE(nullptr, GetFirstAudioContent(offer->description()));
3740 EXPECT_NE(nullptr, GetFirstVideoContent(offer->description()));
3741 EXPECT_TRUE(offer->description()->HasGroup(cricket::GROUP_TYPE_BUNDLE));
3742
3743 rtc_options.use_rtp_mux = false;
3744 offer = CreateOfferWithOptions(rtc_options);
3745 ASSERT_TRUE(offer);
3746 EXPECT_NE(nullptr, GetFirstAudioContent(offer->description()));
3747 EXPECT_NE(nullptr, GetFirstVideoContent(offer->description()));
3748 EXPECT_FALSE(offer->description()->HasGroup(cricket::GROUP_TYPE_BUNDLE));
3749}
3750
3751// If SetMandatoryReceiveAudio(false) and SetMandatoryReceiveVideo(false) are
3752// called for the answer constraints, but an audio and a video section were
3753// offered, there will still be an audio and a video section in the answer.
3754TEST_F(PeerConnectionInterfaceTest,
3755 RejectAudioAndVideoInAnswerWithConstraints) {
3756 // Offer both audio and video.
3757 RTCOfferAnswerOptions rtc_offer_options;
3758 rtc_offer_options.offer_to_receive_audio = 1;
3759 rtc_offer_options.offer_to_receive_video = 1;
3760
3761 CreatePeerConnection();
3762 std::unique_ptr<SessionDescriptionInterface> offer;
3763 CreateOfferWithOptionsAsRemoteDescription(&offer, rtc_offer_options);
3764 EXPECT_NE(nullptr, GetFirstAudioContent(offer->description()));
3765 EXPECT_NE(nullptr, GetFirstVideoContent(offer->description()));
3766
3767 // Since an offer has been created with both audio and video,
3768 // Answers will contain the media types that exist in the offer regardless of
3769 // the value of |answer_options.has_audio| and |answer_options.has_video|.
3770 FakeConstraints answer_c;
3771 // Reject both audio and video.
3772 answer_c.SetMandatoryReceiveAudio(false);
3773 answer_c.SetMandatoryReceiveVideo(false);
3774
3775 std::unique_ptr<SessionDescriptionInterface> answer;
3776 ASSERT_TRUE(DoCreateAnswer(&answer, &answer_c));
3777 const cricket::ContentInfo* audio_content =
3778 GetFirstAudioContent(answer->description());
3779 const cricket::ContentInfo* video_content =
3780 GetFirstVideoContent(answer->description());
3781 ASSERT_NE(nullptr, audio_content);
3782 ASSERT_NE(nullptr, video_content);
3783 EXPECT_TRUE(audio_content->rejected);
3784 EXPECT_TRUE(video_content->rejected);
3785}
3786
zhihuang141aacb2017-08-29 13:23:53 -07003787// This test ensures OnRenegotiationNeeded is called when we add track with
3788// MediaStream -> AddTrack in the same way it is called when we add track with
3789// PeerConnection -> AddTrack.
3790// The test can be removed once addStream is rewritten in terms of addTrack
3791// https://bugs.chromium.org/p/webrtc/issues/detail?id=7815
3792TEST_F(PeerConnectionInterfaceTest, MediaStreamAddTrackRemoveTrackRenegotiate) {
3793 CreatePeerConnectionWithoutDtls();
3794 rtc::scoped_refptr<MediaStreamInterface> stream(
3795 pc_factory_->CreateLocalMediaStream(kStreamLabel1));
3796 pc_->AddStream(stream);
3797 rtc::scoped_refptr<AudioTrackInterface> audio_track(
3798 pc_factory_->CreateAudioTrack("audio_track", nullptr));
3799 rtc::scoped_refptr<VideoTrackInterface> video_track(
3800 pc_factory_->CreateVideoTrack(
3801 "video_track", pc_factory_->CreateVideoSource(
3802 std::unique_ptr<cricket::VideoCapturer>(
3803 new cricket::FakeVideoCapturer()))));
3804 stream->AddTrack(audio_track);
3805 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
3806 observer_.renegotiation_needed_ = false;
3807
3808 stream->AddTrack(video_track);
3809 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
3810 observer_.renegotiation_needed_ = false;
3811
3812 stream->RemoveTrack(audio_track);
3813 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
3814 observer_.renegotiation_needed_ = false;
3815
3816 stream->RemoveTrack(video_track);
3817 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
3818 observer_.renegotiation_needed_ = false;
3819}
3820
3821// Tests that creating answer would fail gracefully without being crashed if the
3822// remote description is unset.
3823TEST_F(PeerConnectionInterfaceTest, CreateAnswerWithoutRemoteDescription) {
3824 CreatePeerConnection();
3825 // Creating answer fails because the remote description is unset.
3826 std::unique_ptr<SessionDescriptionInterface> answer;
3827 EXPECT_FALSE(DoCreateAnswer(&answer, nullptr));
3828
3829 // Createing answer succeeds when the remote description is set.
3830 CreateOfferAsRemoteDescription();
3831 EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
3832}
3833
nisse51542be2016-02-12 02:27:06 -08003834class PeerConnectionMediaConfigTest : public testing::Test {
3835 protected:
3836 void SetUp() override {
zhihuang38ede132017-06-15 12:52:32 -07003837 pcf_ = PeerConnectionFactoryForTest::CreatePeerConnectionFactoryForTest();
nisse51542be2016-02-12 02:27:06 -08003838 pcf_->Initialize();
3839 }
nisseeaabdf62017-05-05 02:23:02 -07003840 const cricket::MediaConfig TestCreatePeerConnection(
nisse51542be2016-02-12 02:27:06 -08003841 const PeerConnectionInterface::RTCConfiguration& config,
zhihuang1c378ed2017-08-17 14:10:50 -07003842 const MediaConstraintsInterface* constraints) {
zhihuang9763d562016-08-05 11:14:50 -07003843 rtc::scoped_refptr<PeerConnectionInterface> pc(pcf_->CreatePeerConnection(
3844 config, constraints, nullptr, nullptr, &observer_));
nisse51542be2016-02-12 02:27:06 -08003845 EXPECT_TRUE(pc.get());
nisseeaabdf62017-05-05 02:23:02 -07003846 return pc->GetConfiguration().media_config;
nisse51542be2016-02-12 02:27:06 -08003847 }
3848
zhihuang9763d562016-08-05 11:14:50 -07003849 rtc::scoped_refptr<PeerConnectionFactoryForTest> pcf_;
nisse51542be2016-02-12 02:27:06 -08003850 MockPeerConnectionObserver observer_;
3851};
3852
3853// This test verifies the default behaviour with no constraints and a
3854// default RTCConfiguration.
3855TEST_F(PeerConnectionMediaConfigTest, TestDefaults) {
3856 PeerConnectionInterface::RTCConfiguration config;
3857 FakeConstraints constraints;
3858
3859 const cricket::MediaConfig& media_config =
3860 TestCreatePeerConnection(config, &constraints);
3861
3862 EXPECT_FALSE(media_config.enable_dscp);
nisse0db023a2016-03-01 04:29:59 -08003863 EXPECT_TRUE(media_config.video.enable_cpu_overuse_detection);
3864 EXPECT_FALSE(media_config.video.disable_prerenderer_smoothing);
3865 EXPECT_FALSE(media_config.video.suspend_below_min_bitrate);
nisse51542be2016-02-12 02:27:06 -08003866}
3867
3868// This test verifies the DSCP constraint is recognized and passed to
nisse528b7932017-05-08 03:21:43 -07003869// the PeerConnection.
nisse51542be2016-02-12 02:27:06 -08003870TEST_F(PeerConnectionMediaConfigTest, TestDscpConstraintTrue) {
3871 PeerConnectionInterface::RTCConfiguration config;
3872 FakeConstraints constraints;
3873
3874 constraints.AddOptional(webrtc::MediaConstraintsInterface::kEnableDscp, true);
3875 const cricket::MediaConfig& media_config =
3876 TestCreatePeerConnection(config, &constraints);
3877
3878 EXPECT_TRUE(media_config.enable_dscp);
3879}
3880
3881// This test verifies the cpu overuse detection constraint is
nisse528b7932017-05-08 03:21:43 -07003882// recognized and passed to the PeerConnection.
nisse51542be2016-02-12 02:27:06 -08003883TEST_F(PeerConnectionMediaConfigTest, TestCpuOveruseConstraintFalse) {
3884 PeerConnectionInterface::RTCConfiguration config;
3885 FakeConstraints constraints;
3886
3887 constraints.AddOptional(
3888 webrtc::MediaConstraintsInterface::kCpuOveruseDetection, false);
3889 const cricket::MediaConfig media_config =
3890 TestCreatePeerConnection(config, &constraints);
3891
nisse0db023a2016-03-01 04:29:59 -08003892 EXPECT_FALSE(media_config.video.enable_cpu_overuse_detection);
nisse51542be2016-02-12 02:27:06 -08003893}
3894
3895// This test verifies that the disable_prerenderer_smoothing flag is
nisse528b7932017-05-08 03:21:43 -07003896// propagated from RTCConfiguration to the PeerConnection.
nisse51542be2016-02-12 02:27:06 -08003897TEST_F(PeerConnectionMediaConfigTest, TestDisablePrerendererSmoothingTrue) {
3898 PeerConnectionInterface::RTCConfiguration config;
3899 FakeConstraints constraints;
3900
Niels Möller71bdda02016-03-31 12:59:59 +02003901 config.set_prerenderer_smoothing(false);
nisse51542be2016-02-12 02:27:06 -08003902 const cricket::MediaConfig& media_config =
3903 TestCreatePeerConnection(config, &constraints);
3904
nisse0db023a2016-03-01 04:29:59 -08003905 EXPECT_TRUE(media_config.video.disable_prerenderer_smoothing);
3906}
3907
3908// This test verifies the suspend below min bitrate constraint is
nisse528b7932017-05-08 03:21:43 -07003909// recognized and passed to the PeerConnection.
nisse0db023a2016-03-01 04:29:59 -08003910TEST_F(PeerConnectionMediaConfigTest,
3911 TestSuspendBelowMinBitrateConstraintTrue) {
3912 PeerConnectionInterface::RTCConfiguration config;
3913 FakeConstraints constraints;
3914
3915 constraints.AddOptional(
3916 webrtc::MediaConstraintsInterface::kEnableVideoSuspendBelowMinBitrate,
3917 true);
3918 const cricket::MediaConfig media_config =
3919 TestCreatePeerConnection(config, &constraints);
3920
3921 EXPECT_TRUE(media_config.video.suspend_below_min_bitrate);
nisse51542be2016-02-12 02:27:06 -08003922}
3923
deadbeef293e9262017-01-11 12:28:30 -08003924// Tests a few random fields being different.
3925TEST(RTCConfigurationTest, ComparisonOperators) {
3926 PeerConnectionInterface::RTCConfiguration a;
3927 PeerConnectionInterface::RTCConfiguration b;
3928 EXPECT_EQ(a, b);
3929
3930 PeerConnectionInterface::RTCConfiguration c;
3931 c.servers.push_back(PeerConnectionInterface::IceServer());
3932 EXPECT_NE(a, c);
3933
3934 PeerConnectionInterface::RTCConfiguration d;
3935 d.type = PeerConnectionInterface::kRelay;
3936 EXPECT_NE(a, d);
3937
3938 PeerConnectionInterface::RTCConfiguration e;
3939 e.audio_jitter_buffer_max_packets = 5;
3940 EXPECT_NE(a, e);
3941
3942 PeerConnectionInterface::RTCConfiguration f;
3943 f.ice_connection_receiving_timeout = 1337;
3944 EXPECT_NE(a, f);
3945
3946 PeerConnectionInterface::RTCConfiguration g;
3947 g.disable_ipv6 = true;
3948 EXPECT_NE(a, g);
3949
3950 PeerConnectionInterface::RTCConfiguration h(
3951 PeerConnectionInterface::RTCConfigurationType::kAggressive);
3952 EXPECT_NE(a, h);
3953}