blob: c23a79d597b4fe8bdaec83edbd867df52ef3f3f5 [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;
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700335using webrtc::JsepSessionDescription;
deadbeefc80741f2015-10-22 13:14:45 -0700336using webrtc::MediaConstraintsInterface;
deadbeefab9b2d12015-10-14 11:33:11 -0700337using webrtc::MediaStream;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000338using webrtc::MediaStreamInterface;
339using webrtc::MediaStreamTrackInterface;
340using webrtc::MockCreateSessionDescriptionObserver;
341using webrtc::MockDataChannelObserver;
342using webrtc::MockSetSessionDescriptionObserver;
343using webrtc::MockStatsObserver;
perkjd61bf802016-03-24 03:16:19 -0700344using webrtc::NotifierInterface;
345using webrtc::ObserverInterface;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000346using webrtc::PeerConnectionInterface;
347using webrtc::PeerConnectionObserver;
deadbeef293e9262017-01-11 12:28:30 -0800348using webrtc::RTCError;
349using webrtc::RTCErrorType;
deadbeefab9b2d12015-10-14 11:33:11 -0700350using webrtc::RtpReceiverInterface;
351using webrtc::RtpSenderInterface;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000352using webrtc::SdpParseError;
353using webrtc::SessionDescriptionInterface;
deadbeefab9b2d12015-10-14 11:33:11 -0700354using webrtc::StreamCollection;
355using webrtc::StreamCollectionInterface;
perkja3ede6c2016-03-08 01:27:48 +0100356using webrtc::VideoTrackSourceInterface;
deadbeefab9b2d12015-10-14 11:33:11 -0700357using webrtc::VideoTrack;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000358using webrtc::VideoTrackInterface;
359
deadbeefab9b2d12015-10-14 11:33:11 -0700360typedef PeerConnectionInterface::RTCOfferAnswerOptions RTCOfferAnswerOptions;
361
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000362namespace {
363
364// Gets the first ssrc of given content type from the ContentInfo.
365bool GetFirstSsrc(const cricket::ContentInfo* content_info, int* ssrc) {
366 if (!content_info || !ssrc) {
367 return false;
368 }
369 const cricket::MediaContentDescription* media_desc =
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000370 static_cast<const cricket::MediaContentDescription*>(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000371 content_info->description);
372 if (!media_desc || media_desc->streams().empty()) {
373 return false;
374 }
375 *ssrc = media_desc->streams().begin()->first_ssrc();
376 return true;
377}
378
deadbeefd1a38b52016-12-10 13:15:33 -0800379// Get the ufrags out of an SDP blob. Useful for testing ICE restart
380// behavior.
381std::vector<std::string> GetUfrags(
382 const webrtc::SessionDescriptionInterface* desc) {
383 std::vector<std::string> ufrags;
384 for (const cricket::TransportInfo& info :
385 desc->description()->transport_infos()) {
386 ufrags.push_back(info.description.ice_ufrag);
387 }
388 return ufrags;
389}
390
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000391void SetSsrcToZero(std::string* sdp) {
392 const char kSdpSsrcAtribute[] = "a=ssrc:";
393 const char kSdpSsrcAtributeZero[] = "a=ssrc:0";
394 size_t ssrc_pos = 0;
395 while ((ssrc_pos = sdp->find(kSdpSsrcAtribute, ssrc_pos)) !=
396 std::string::npos) {
397 size_t end_ssrc = sdp->find(" ", ssrc_pos);
398 sdp->replace(ssrc_pos, end_ssrc - ssrc_pos, kSdpSsrcAtributeZero);
399 ssrc_pos = end_ssrc;
400 }
401}
402
deadbeefab9b2d12015-10-14 11:33:11 -0700403// Check if |streams| contains the specified track.
404bool ContainsTrack(const std::vector<cricket::StreamParams>& streams,
405 const std::string& stream_label,
406 const std::string& track_id) {
407 for (const cricket::StreamParams& params : streams) {
408 if (params.sync_label == stream_label && params.id == track_id) {
409 return true;
410 }
411 }
412 return false;
413}
414
415// Check if |senders| contains the specified sender, by id.
416bool ContainsSender(
417 const std::vector<rtc::scoped_refptr<RtpSenderInterface>>& senders,
418 const std::string& id) {
419 for (const auto& sender : senders) {
420 if (sender->id() == id) {
421 return true;
422 }
423 }
424 return false;
425}
426
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700427// Check if |senders| contains the specified sender, by id and stream id.
428bool ContainsSender(
429 const std::vector<rtc::scoped_refptr<RtpSenderInterface>>& senders,
430 const std::string& id,
431 const std::string& stream_id) {
432 for (const auto& sender : senders) {
deadbeefa601f5c2016-06-06 14:27:39 -0700433 if (sender->id() == id && sender->stream_ids()[0] == stream_id) {
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700434 return true;
435 }
436 }
437 return false;
438}
439
deadbeefab9b2d12015-10-14 11:33:11 -0700440// Create a collection of streams.
441// CreateStreamCollection(1) creates a collection that
442// correspond to kSdpStringWithStream1.
443// CreateStreamCollection(2) correspond to kSdpStringWithStream1And2.
444rtc::scoped_refptr<StreamCollection> CreateStreamCollection(
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700445 int number_of_streams,
446 int tracks_per_stream) {
deadbeefab9b2d12015-10-14 11:33:11 -0700447 rtc::scoped_refptr<StreamCollection> local_collection(
448 StreamCollection::Create());
449
450 for (int i = 0; i < number_of_streams; ++i) {
451 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream(
452 webrtc::MediaStream::Create(kStreams[i]));
453
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700454 for (int j = 0; j < tracks_per_stream; ++j) {
455 // Add a local audio track.
456 rtc::scoped_refptr<webrtc::AudioTrackInterface> audio_track(
457 webrtc::AudioTrack::Create(kAudioTracks[i * tracks_per_stream + j],
458 nullptr));
459 stream->AddTrack(audio_track);
deadbeefab9b2d12015-10-14 11:33:11 -0700460
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700461 // Add a local video track.
462 rtc::scoped_refptr<webrtc::VideoTrackInterface> video_track(
463 webrtc::VideoTrack::Create(kVideoTracks[i * tracks_per_stream + j],
perkj773be362017-07-31 23:22:01 -0700464 webrtc::FakeVideoTrackSource::Create(),
465 rtc::Thread::Current()));
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700466 stream->AddTrack(video_track);
467 }
deadbeefab9b2d12015-10-14 11:33:11 -0700468
469 local_collection->AddStream(stream);
470 }
471 return local_collection;
472}
473
474// Check equality of StreamCollections.
475bool CompareStreamCollections(StreamCollectionInterface* s1,
476 StreamCollectionInterface* s2) {
477 if (s1 == nullptr || s2 == nullptr || s1->count() != s2->count()) {
478 return false;
479 }
480
481 for (size_t i = 0; i != s1->count(); ++i) {
482 if (s1->at(i)->label() != s2->at(i)->label()) {
483 return false;
484 }
485 webrtc::AudioTrackVector audio_tracks1 = s1->at(i)->GetAudioTracks();
486 webrtc::AudioTrackVector audio_tracks2 = s2->at(i)->GetAudioTracks();
487 webrtc::VideoTrackVector video_tracks1 = s1->at(i)->GetVideoTracks();
488 webrtc::VideoTrackVector video_tracks2 = s2->at(i)->GetVideoTracks();
489
490 if (audio_tracks1.size() != audio_tracks2.size()) {
491 return false;
492 }
493 for (size_t j = 0; j != audio_tracks1.size(); ++j) {
494 if (audio_tracks1[j]->id() != audio_tracks2[j]->id()) {
495 return false;
496 }
497 }
498 if (video_tracks1.size() != video_tracks2.size()) {
499 return false;
500 }
501 for (size_t j = 0; j != video_tracks1.size(); ++j) {
502 if (video_tracks1[j]->id() != video_tracks2[j]->id()) {
503 return false;
504 }
505 }
506 }
507 return true;
508}
509
perkjd61bf802016-03-24 03:16:19 -0700510// Helper class to test Observer.
511class MockTrackObserver : public ObserverInterface {
512 public:
513 explicit MockTrackObserver(NotifierInterface* notifier)
514 : notifier_(notifier) {
515 notifier_->RegisterObserver(this);
516 }
517
518 ~MockTrackObserver() { Unregister(); }
519
520 void Unregister() {
521 if (notifier_) {
522 notifier_->UnregisterObserver(this);
523 notifier_ = nullptr;
524 }
525 }
526
527 MOCK_METHOD0(OnChanged, void());
528
529 private:
530 NotifierInterface* notifier_;
531};
532
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000533class MockPeerConnectionObserver : public PeerConnectionObserver {
534 public:
deadbeefab9b2d12015-10-14 11:33:11 -0700535 MockPeerConnectionObserver() : remote_streams_(StreamCollection::Create()) {}
Henrik Kjellander3fe372d2016-05-12 08:10:52 +0200536 virtual ~MockPeerConnectionObserver() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000537 }
538 void SetPeerConnectionInterface(PeerConnectionInterface* pc) {
539 pc_ = pc;
540 if (pc) {
541 state_ = pc_->signaling_state();
542 }
543 }
nisseef8b61e2016-04-29 06:09:15 -0700544 void OnSignalingChange(
545 PeerConnectionInterface::SignalingState new_state) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000546 EXPECT_EQ(pc_->signaling_state(), new_state);
547 state_ = new_state;
548 }
deadbeefab9b2d12015-10-14 11:33:11 -0700549
550 MediaStreamInterface* RemoteStream(const std::string& label) {
551 return remote_streams_->find(label);
552 }
553 StreamCollectionInterface* remote_streams() const { return remote_streams_; }
Taylor Brandstetter98cde262016-05-31 13:02:21 -0700554 void OnAddStream(rtc::scoped_refptr<MediaStreamInterface> stream) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000555 last_added_stream_ = stream;
deadbeefab9b2d12015-10-14 11:33:11 -0700556 remote_streams_->AddStream(stream);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000557 }
Taylor Brandstetter98cde262016-05-31 13:02:21 -0700558 void OnRemoveStream(
559 rtc::scoped_refptr<MediaStreamInterface> stream) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000560 last_removed_stream_ = stream;
deadbeefab9b2d12015-10-14 11:33:11 -0700561 remote_streams_->RemoveStream(stream);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000562 }
perkjdfb769d2016-02-09 03:09:43 -0800563 void OnRenegotiationNeeded() override { renegotiation_needed_ = true; }
Taylor Brandstetter98cde262016-05-31 13:02:21 -0700564 void OnDataChannel(
565 rtc::scoped_refptr<DataChannelInterface> data_channel) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000566 last_datachannel_ = data_channel;
567 }
568
perkjdfb769d2016-02-09 03:09:43 -0800569 void OnIceConnectionChange(
570 PeerConnectionInterface::IceConnectionState new_state) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000571 EXPECT_EQ(pc_->ice_connection_state(), new_state);
zhihuang81c3a032016-11-17 12:06:24 -0800572 callback_triggered_ = true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000573 }
perkjdfb769d2016-02-09 03:09:43 -0800574 void OnIceGatheringChange(
575 PeerConnectionInterface::IceGatheringState new_state) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000576 EXPECT_EQ(pc_->ice_gathering_state(), new_state);
perkjdfb769d2016-02-09 03:09:43 -0800577 ice_complete_ = new_state == PeerConnectionInterface::kIceGatheringComplete;
zhihuang81c3a032016-11-17 12:06:24 -0800578 callback_triggered_ = true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000579 }
perkjdfb769d2016-02-09 03:09:43 -0800580 void OnIceCandidate(const webrtc::IceCandidateInterface* candidate) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000581 EXPECT_NE(PeerConnectionInterface::kIceGatheringNew,
582 pc_->ice_gathering_state());
583
584 std::string sdp;
585 EXPECT_TRUE(candidate->ToString(&sdp));
586 EXPECT_LT(0u, sdp.size());
587 last_candidate_.reset(webrtc::CreateIceCandidate(candidate->sdp_mid(),
588 candidate->sdp_mline_index(), sdp, NULL));
589 EXPECT_TRUE(last_candidate_.get() != NULL);
zhihuang81c3a032016-11-17 12:06:24 -0800590 callback_triggered_ = true;
zhihuang29ff8442016-07-27 11:07:25 -0700591 }
592
593 void OnIceCandidatesRemoved(
594 const std::vector<cricket::Candidate>& candidates) override {
zhihuang81c3a032016-11-17 12:06:24 -0800595 callback_triggered_ = true;
zhihuang29ff8442016-07-27 11:07:25 -0700596 }
597
598 void OnIceConnectionReceivingChange(bool receiving) override {
zhihuang81c3a032016-11-17 12:06:24 -0800599 callback_triggered_ = true;
600 }
601
zhihuangc63b8942016-12-02 15:41:10 -0800602 void OnAddTrack(
603 rtc::scoped_refptr<webrtc::RtpReceiverInterface> receiver,
604 const std::vector<rtc::scoped_refptr<webrtc::MediaStreamInterface>>&
605 streams) override {
zhihuang81c3a032016-11-17 12:06:24 -0800606 EXPECT_TRUE(receiver != nullptr);
607 num_added_tracks_++;
608 last_added_track_label_ = receiver->id();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000609 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000610
611 // Returns the label of the last added stream.
612 // Empty string if no stream have been added.
613 std::string GetLastAddedStreamLabel() {
614 if (last_added_stream_.get())
615 return last_added_stream_->label();
616 return "";
617 }
618 std::string GetLastRemovedStreamLabel() {
619 if (last_removed_stream_.get())
620 return last_removed_stream_->label();
621 return "";
622 }
623
zhihuang9763d562016-08-05 11:14:50 -0700624 rtc::scoped_refptr<PeerConnectionInterface> pc_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000625 PeerConnectionInterface::SignalingState state_;
kwibergd1fe2812016-04-27 06:47:29 -0700626 std::unique_ptr<IceCandidateInterface> last_candidate_;
zhihuang9763d562016-08-05 11:14:50 -0700627 rtc::scoped_refptr<DataChannelInterface> last_datachannel_;
deadbeefab9b2d12015-10-14 11:33:11 -0700628 rtc::scoped_refptr<StreamCollection> remote_streams_;
629 bool renegotiation_needed_ = false;
630 bool ice_complete_ = false;
zhihuang81c3a032016-11-17 12:06:24 -0800631 bool callback_triggered_ = false;
632 int num_added_tracks_ = 0;
633 std::string last_added_track_label_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000634
635 private:
zhihuang9763d562016-08-05 11:14:50 -0700636 rtc::scoped_refptr<MediaStreamInterface> last_added_stream_;
637 rtc::scoped_refptr<MediaStreamInterface> last_removed_stream_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000638};
639
640} // namespace
deadbeefab9b2d12015-10-14 11:33:11 -0700641
nisse528b7932017-05-08 03:21:43 -0700642// The PeerConnectionMediaConfig tests below verify that configuration and
643// constraints are propagated into the PeerConnection's MediaConfig. These
644// settings are intended for MediaChannel constructors, but that is not
645// exercised by these unittest.
zhihuang29ff8442016-07-27 11:07:25 -0700646class PeerConnectionFactoryForTest : public webrtc::PeerConnectionFactory {
647 public:
zhihuang38ede132017-06-15 12:52:32 -0700648 static rtc::scoped_refptr<PeerConnectionFactoryForTest>
649 CreatePeerConnectionFactoryForTest() {
650 auto audio_encoder_factory = webrtc::CreateBuiltinAudioEncoderFactory();
651 auto audio_decoder_factory = webrtc::CreateBuiltinAudioDecoderFactory();
652
653 auto media_engine = std::unique_ptr<cricket::MediaEngineInterface>(
654 cricket::WebRtcMediaEngineFactory::Create(
655 nullptr, audio_encoder_factory, audio_decoder_factory, nullptr,
peaha9cc40b2017-06-29 08:32:09 -0700656 nullptr, nullptr, webrtc::AudioProcessing::Create()));
zhihuang38ede132017-06-15 12:52:32 -0700657
658 std::unique_ptr<webrtc::CallFactoryInterface> call_factory =
659 webrtc::CreateCallFactory();
660
661 std::unique_ptr<webrtc::RtcEventLogFactoryInterface> event_log_factory =
662 webrtc::CreateRtcEventLogFactory();
663
664 return new rtc::RefCountedObject<PeerConnectionFactoryForTest>(
665 rtc::Thread::Current(), rtc::Thread::Current(), rtc::Thread::Current(),
deadbeefd7850b22017-08-23 10:59:19 -0700666 FakeAudioCaptureModule::Create(), audio_encoder_factory,
667 audio_decoder_factory, nullptr, nullptr, nullptr,
668 std::move(media_engine), std::move(call_factory),
zhihuang38ede132017-06-15 12:52:32 -0700669 std::move(event_log_factory));
670 }
671
672 PeerConnectionFactoryForTest(
673 rtc::Thread* network_thread,
674 rtc::Thread* worker_thread,
675 rtc::Thread* signaling_thread,
deadbeefd7850b22017-08-23 10:59:19 -0700676 rtc::scoped_refptr<FakeAudioCaptureModule> fake_adm,
zhihuang38ede132017-06-15 12:52:32 -0700677 rtc::scoped_refptr<webrtc::AudioEncoderFactory> audio_encoder_factory,
678 rtc::scoped_refptr<webrtc::AudioDecoderFactory> audio_decoder_factory,
679 cricket::WebRtcVideoEncoderFactory* video_encoder_factory,
680 cricket::WebRtcVideoDecoderFactory* video_decoder_factory,
681 rtc::scoped_refptr<webrtc::AudioMixer> audio_mixer,
682 std::unique_ptr<cricket::MediaEngineInterface> media_engine,
683 std::unique_ptr<webrtc::CallFactoryInterface> call_factory,
684 std::unique_ptr<webrtc::RtcEventLogFactoryInterface> event_log_factory)
685 : webrtc::PeerConnectionFactory(network_thread,
686 worker_thread,
687 signaling_thread,
deadbeefd7850b22017-08-23 10:59:19 -0700688 fake_adm,
zhihuang38ede132017-06-15 12:52:32 -0700689 audio_encoder_factory,
690 audio_decoder_factory,
691 video_encoder_factory,
692 video_decoder_factory,
693 audio_mixer,
694 std::move(media_engine),
695 std::move(call_factory),
696 std::move(event_log_factory)) {}
kwiberg1e4e8cb2017-01-31 01:48:08 -0800697
zhihuang29ff8442016-07-27 11:07:25 -0700698 cricket::TransportController* CreateTransportController(
Honghai Zhangbfd398c2016-08-30 22:07:42 -0700699 cricket::PortAllocator* port_allocator,
700 bool redetermine_role_on_ice_restart) override {
zhihuang29ff8442016-07-27 11:07:25 -0700701 transport_controller = new cricket::TransportController(
Honghai Zhangbfd398c2016-08-30 22:07:42 -0700702 rtc::Thread::Current(), rtc::Thread::Current(), port_allocator,
deadbeef7914b8c2017-04-21 03:23:33 -0700703 redetermine_role_on_ice_restart, rtc::CryptoOptions());
zhihuang29ff8442016-07-27 11:07:25 -0700704 return transport_controller;
705 }
706
deadbeefd7850b22017-08-23 10:59:19 -0700707 rtc::scoped_refptr<FakeAudioCaptureModule> fake_audio_capture_module_;
zhihuang29ff8442016-07-27 11:07:25 -0700708 cricket::TransportController* transport_controller;
zhihuang29ff8442016-07-27 11:07:25 -0700709};
710
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000711class PeerConnectionInterfaceTest : public testing::Test {
712 protected:
deadbeef9a6f4d42017-05-15 19:43:33 -0700713 PeerConnectionInterfaceTest()
deadbeef98e186c2017-05-16 18:00:06 -0700714 : vss_(new rtc::VirtualSocketServer()), main_(vss_.get()) {
phoglund37ebcf02016-01-08 05:04:57 -0800715#ifdef WEBRTC_ANDROID
716 webrtc::InitializeAndroidObjects();
717#endif
718 }
719
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000720 virtual void SetUp() {
deadbeefd7850b22017-08-23 10:59:19 -0700721 // Use fake audio capture module since we're only testing the interface
722 // level, and using a real one could make tests flaky when run in parallel.
723 fake_audio_capture_module_ = FakeAudioCaptureModule::Create();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000724 pc_factory_ = webrtc::CreatePeerConnectionFactory(
danilchape9021a32016-05-17 01:52:02 -0700725 rtc::Thread::Current(), rtc::Thread::Current(), rtc::Thread::Current(),
deadbeefd7850b22017-08-23 10:59:19 -0700726 fake_audio_capture_module_, nullptr, nullptr);
danilchape9021a32016-05-17 01:52:02 -0700727 ASSERT_TRUE(pc_factory_);
zhihuang29ff8442016-07-27 11:07:25 -0700728 pc_factory_for_test_ =
zhihuang38ede132017-06-15 12:52:32 -0700729 PeerConnectionFactoryForTest::CreatePeerConnectionFactoryForTest();
zhihuang29ff8442016-07-27 11:07:25 -0700730 pc_factory_for_test_->Initialize();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000731 }
732
733 void CreatePeerConnection() {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700734 CreatePeerConnection(PeerConnectionInterface::RTCConfiguration(), nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000735 }
736
deadbeef293e9262017-01-11 12:28:30 -0800737 // DTLS does not work in a loopback call, so is disabled for most of the
738 // tests in this file.
739 void CreatePeerConnectionWithoutDtls() {
740 FakeConstraints no_dtls_constraints;
741 no_dtls_constraints.AddMandatory(
742 webrtc::MediaConstraintsInterface::kEnableDtlsSrtp, false);
743
744 CreatePeerConnection(PeerConnectionInterface::RTCConfiguration(),
745 &no_dtls_constraints);
746 }
747
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000748 void CreatePeerConnection(webrtc::MediaConstraintsInterface* constraints) {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700749 CreatePeerConnection(PeerConnectionInterface::RTCConfiguration(),
750 constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000751 }
752
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700753 void CreatePeerConnectionWithIceTransportsType(
754 PeerConnectionInterface::IceTransportsType type) {
755 PeerConnectionInterface::RTCConfiguration config;
756 config.type = type;
757 return CreatePeerConnection(config, nullptr);
758 }
759
760 void CreatePeerConnectionWithIceServer(const std::string& uri,
761 const std::string& password) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800762 PeerConnectionInterface::RTCConfiguration config;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000763 PeerConnectionInterface::IceServer server;
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700764 server.uri = uri;
765 server.password = password;
766 config.servers.push_back(server);
767 CreatePeerConnection(config, nullptr);
768 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000769
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700770 void CreatePeerConnection(PeerConnectionInterface::RTCConfiguration config,
771 webrtc::MediaConstraintsInterface* constraints) {
kwibergd1fe2812016-04-27 06:47:29 -0700772 std::unique_ptr<cricket::FakePortAllocator> port_allocator(
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800773 new cricket::FakePortAllocator(rtc::Thread::Current(), nullptr));
774 port_allocator_ = port_allocator.get();
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000775
deadbeef1dcb1642017-03-29 21:08:16 -0700776 // Create certificate generator unless DTLS constraint is explicitly set to
777 // false.
Henrik Boströmd79599d2016-06-01 13:58:50 +0200778 std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator;
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000779 bool dtls;
780 if (FindConstraint(constraints,
781 webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
782 &dtls,
Henrik Boström5e56c592015-08-11 10:33:13 +0200783 nullptr) && dtls) {
deadbeef8662f942017-01-20 21:20:51 -0800784 fake_certificate_generator_ = new FakeRTCCertificateGenerator();
785 cert_generator.reset(fake_certificate_generator_);
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000786 }
Henrik Boströmd79599d2016-06-01 13:58:50 +0200787 pc_ = pc_factory_->CreatePeerConnection(
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800788 config, constraints, std::move(port_allocator),
Henrik Boströmd79599d2016-06-01 13:58:50 +0200789 std::move(cert_generator), &observer_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000790 ASSERT_TRUE(pc_.get() != NULL);
791 observer_.SetPeerConnectionInterface(pc_.get());
792 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
793 }
794
deadbeef0a6c4ca2015-10-06 11:38:28 -0700795 void CreatePeerConnectionExpectFail(const std::string& uri) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800796 PeerConnectionInterface::RTCConfiguration config;
deadbeef0a6c4ca2015-10-06 11:38:28 -0700797 PeerConnectionInterface::IceServer server;
deadbeef0a6c4ca2015-10-06 11:38:28 -0700798 server.uri = uri;
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800799 config.servers.push_back(server);
deadbeef0a6c4ca2015-10-06 11:38:28 -0700800
zhihuang9763d562016-08-05 11:14:50 -0700801 rtc::scoped_refptr<PeerConnectionInterface> pc;
hbosd7973cc2016-05-27 06:08:53 -0700802 pc = pc_factory_->CreatePeerConnection(config, nullptr, nullptr, nullptr,
803 &observer_);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800804 EXPECT_EQ(nullptr, pc);
deadbeef0a6c4ca2015-10-06 11:38:28 -0700805 }
806
Steve Anton038834f2017-07-14 15:59:59 -0700807 void CreatePeerConnectionExpectFail(
808 PeerConnectionInterface::RTCConfiguration config) {
809 PeerConnectionInterface::IceServer server;
810 server.uri = kTurnIceServerUri;
811 server.password = kTurnPassword;
812 config.servers.push_back(server);
813 rtc::scoped_refptr<PeerConnectionInterface> pc =
814 pc_factory_->CreatePeerConnection(config, nullptr, nullptr, &observer_);
815 EXPECT_EQ(nullptr, pc);
816 }
817
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000818 void CreatePeerConnectionWithDifferentConfigurations() {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700819 CreatePeerConnectionWithIceServer(kStunAddressOnly, "");
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800820 EXPECT_EQ(1u, port_allocator_->stun_servers().size());
821 EXPECT_EQ(0u, port_allocator_->turn_servers().size());
822 EXPECT_EQ("address", port_allocator_->stun_servers().begin()->hostname());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000823 EXPECT_EQ(kDefaultStunPort,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800824 port_allocator_->stun_servers().begin()->port());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000825
deadbeef0a6c4ca2015-10-06 11:38:28 -0700826 CreatePeerConnectionExpectFail(kStunInvalidPort);
827 CreatePeerConnectionExpectFail(kStunAddressPortAndMore1);
828 CreatePeerConnectionExpectFail(kStunAddressPortAndMore2);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000829
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700830 CreatePeerConnectionWithIceServer(kTurnIceServerUri, kTurnPassword);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800831 EXPECT_EQ(0u, port_allocator_->stun_servers().size());
832 EXPECT_EQ(1u, port_allocator_->turn_servers().size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000833 EXPECT_EQ(kTurnUsername,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800834 port_allocator_->turn_servers()[0].credentials.username);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000835 EXPECT_EQ(kTurnPassword,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800836 port_allocator_->turn_servers()[0].credentials.password);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000837 EXPECT_EQ(kTurnHostname,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800838 port_allocator_->turn_servers()[0].ports[0].address.hostname());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000839 }
840
841 void ReleasePeerConnection() {
842 pc_ = NULL;
843 observer_.SetPeerConnectionInterface(NULL);
844 }
845
deadbeefab9b2d12015-10-14 11:33:11 -0700846 void AddVideoStream(const std::string& label) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000847 // Create a local stream.
zhihuang9763d562016-08-05 11:14:50 -0700848 rtc::scoped_refptr<MediaStreamInterface> stream(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000849 pc_factory_->CreateLocalMediaStream(label));
zhihuang9763d562016-08-05 11:14:50 -0700850 rtc::scoped_refptr<VideoTrackSourceInterface> video_source(
deadbeef112b2e92017-02-10 20:13:37 -0800851 pc_factory_->CreateVideoSource(std::unique_ptr<cricket::VideoCapturer>(
852 new cricket::FakeVideoCapturer()),
853 NULL));
zhihuang9763d562016-08-05 11:14:50 -0700854 rtc::scoped_refptr<VideoTrackInterface> video_track(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000855 pc_factory_->CreateVideoTrack(label + "v0", video_source));
856 stream->AddTrack(video_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +0000857 EXPECT_TRUE(pc_->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000858 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
859 observer_.renegotiation_needed_ = false;
860 }
861
862 void AddVoiceStream(const std::string& label) {
863 // Create a local stream.
zhihuang9763d562016-08-05 11:14:50 -0700864 rtc::scoped_refptr<MediaStreamInterface> stream(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000865 pc_factory_->CreateLocalMediaStream(label));
zhihuang9763d562016-08-05 11:14:50 -0700866 rtc::scoped_refptr<AudioTrackInterface> audio_track(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000867 pc_factory_->CreateAudioTrack(label + "a0", NULL));
868 stream->AddTrack(audio_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +0000869 EXPECT_TRUE(pc_->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000870 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
871 observer_.renegotiation_needed_ = false;
872 }
873
874 void AddAudioVideoStream(const std::string& stream_label,
875 const std::string& audio_track_label,
876 const std::string& video_track_label) {
877 // Create a local stream.
zhihuang9763d562016-08-05 11:14:50 -0700878 rtc::scoped_refptr<MediaStreamInterface> stream(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000879 pc_factory_->CreateLocalMediaStream(stream_label));
zhihuang9763d562016-08-05 11:14:50 -0700880 rtc::scoped_refptr<AudioTrackInterface> audio_track(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000881 pc_factory_->CreateAudioTrack(
882 audio_track_label, static_cast<AudioSourceInterface*>(NULL)));
883 stream->AddTrack(audio_track.get());
zhihuang9763d562016-08-05 11:14:50 -0700884 rtc::scoped_refptr<VideoTrackInterface> video_track(
nisseaf510af2016-03-21 08:20:42 -0700885 pc_factory_->CreateVideoTrack(
deadbeef112b2e92017-02-10 20:13:37 -0800886 video_track_label, pc_factory_->CreateVideoSource(
887 std::unique_ptr<cricket::VideoCapturer>(
888 new cricket::FakeVideoCapturer()))));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000889 stream->AddTrack(video_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +0000890 EXPECT_TRUE(pc_->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000891 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
892 observer_.renegotiation_needed_ = false;
893 }
894
kwibergd1fe2812016-04-27 06:47:29 -0700895 bool DoCreateOfferAnswer(std::unique_ptr<SessionDescriptionInterface>* desc,
deadbeefc80741f2015-10-22 13:14:45 -0700896 bool offer,
897 MediaConstraintsInterface* constraints) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000898 rtc::scoped_refptr<MockCreateSessionDescriptionObserver>
899 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000900 MockCreateSessionDescriptionObserver>());
901 if (offer) {
deadbeefc80741f2015-10-22 13:14:45 -0700902 pc_->CreateOffer(observer, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000903 } else {
deadbeefc80741f2015-10-22 13:14:45 -0700904 pc_->CreateAnswer(observer, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000905 }
906 EXPECT_EQ_WAIT(true, observer->called(), kTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -0700907 *desc = observer->MoveDescription();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000908 return observer->result();
909 }
910
kwibergd1fe2812016-04-27 06:47:29 -0700911 bool DoCreateOffer(std::unique_ptr<SessionDescriptionInterface>* desc,
deadbeefc80741f2015-10-22 13:14:45 -0700912 MediaConstraintsInterface* constraints) {
913 return DoCreateOfferAnswer(desc, true, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000914 }
915
kwibergd1fe2812016-04-27 06:47:29 -0700916 bool DoCreateAnswer(std::unique_ptr<SessionDescriptionInterface>* desc,
deadbeefc80741f2015-10-22 13:14:45 -0700917 MediaConstraintsInterface* constraints) {
918 return DoCreateOfferAnswer(desc, false, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000919 }
920
921 bool DoSetSessionDescription(SessionDescriptionInterface* desc, bool local) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000922 rtc::scoped_refptr<MockSetSessionDescriptionObserver>
923 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000924 MockSetSessionDescriptionObserver>());
925 if (local) {
926 pc_->SetLocalDescription(observer, desc);
927 } else {
928 pc_->SetRemoteDescription(observer, desc);
929 }
zhihuang29ff8442016-07-27 11:07:25 -0700930 if (pc_->signaling_state() != PeerConnectionInterface::kClosed) {
931 EXPECT_EQ_WAIT(true, observer->called(), kTimeout);
932 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000933 return observer->result();
934 }
935
936 bool DoSetLocalDescription(SessionDescriptionInterface* desc) {
937 return DoSetSessionDescription(desc, true);
938 }
939
940 bool DoSetRemoteDescription(SessionDescriptionInterface* desc) {
941 return DoSetSessionDescription(desc, false);
942 }
943
944 // Calls PeerConnection::GetStats and check the return value.
945 // It does not verify the values in the StatReports since a RTCP packet might
946 // be required.
947 bool DoGetStats(MediaStreamTrackInterface* track) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000948 rtc::scoped_refptr<MockStatsObserver> observer(
949 new rtc::RefCountedObject<MockStatsObserver>());
jiayl@webrtc.orgdb41b4d2014-03-03 21:30:06 +0000950 if (!pc_->GetStats(
951 observer, track, PeerConnectionInterface::kStatsOutputLevelStandard))
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000952 return false;
953 EXPECT_TRUE_WAIT(observer->called(), kTimeout);
954 return observer->called();
955 }
956
957 void InitiateCall() {
deadbeef293e9262017-01-11 12:28:30 -0800958 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000959 // Create a local stream with audio&video tracks.
960 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
961 CreateOfferReceiveAnswer();
962 }
963
964 // Verify that RTP Header extensions has been negotiated for audio and video.
965 void VerifyRemoteRtpHeaderExtensions() {
966 const cricket::MediaContentDescription* desc =
967 cricket::GetFirstAudioContentDescription(
968 pc_->remote_description()->description());
969 ASSERT_TRUE(desc != NULL);
970 EXPECT_GT(desc->rtp_header_extensions().size(), 0u);
971
972 desc = cricket::GetFirstVideoContentDescription(
973 pc_->remote_description()->description());
974 ASSERT_TRUE(desc != NULL);
975 EXPECT_GT(desc->rtp_header_extensions().size(), 0u);
976 }
977
978 void CreateOfferAsRemoteDescription() {
kwibergd1fe2812016-04-27 06:47:29 -0700979 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -0700980 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000981 std::string sdp;
982 EXPECT_TRUE(offer->ToString(&sdp));
983 SessionDescriptionInterface* remote_offer =
984 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
985 sdp, NULL);
986 EXPECT_TRUE(DoSetRemoteDescription(remote_offer));
987 EXPECT_EQ(PeerConnectionInterface::kHaveRemoteOffer, observer_.state_);
988 }
989
deadbeefab9b2d12015-10-14 11:33:11 -0700990 void CreateAndSetRemoteOffer(const std::string& sdp) {
991 SessionDescriptionInterface* remote_offer =
992 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
993 sdp, nullptr);
994 EXPECT_TRUE(DoSetRemoteDescription(remote_offer));
995 EXPECT_EQ(PeerConnectionInterface::kHaveRemoteOffer, observer_.state_);
996 }
997
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000998 void CreateAnswerAsLocalDescription() {
kwibergd1fe2812016-04-27 06:47:29 -0700999 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -07001000 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001001
1002 // TODO(perkj): Currently SetLocalDescription fails if any parameters in an
1003 // audio codec change, even if the parameter has nothing to do with
1004 // receiving. Not all parameters are serialized to SDP.
1005 // Since CreatePrAnswerAsLocalDescription serialize/deserialize
1006 // the SessionDescription, it is necessary to do that here to in order to
1007 // get ReceiveOfferCreatePrAnswerAndAnswer and RenegotiateAudioOnly to pass.
1008 // https://code.google.com/p/webrtc/issues/detail?id=1356
1009 std::string sdp;
1010 EXPECT_TRUE(answer->ToString(&sdp));
1011 SessionDescriptionInterface* new_answer =
1012 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
1013 sdp, NULL);
1014 EXPECT_TRUE(DoSetLocalDescription(new_answer));
1015 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
1016 }
1017
1018 void CreatePrAnswerAsLocalDescription() {
kwibergd1fe2812016-04-27 06:47:29 -07001019 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -07001020 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001021
1022 std::string sdp;
1023 EXPECT_TRUE(answer->ToString(&sdp));
1024 SessionDescriptionInterface* pr_answer =
1025 webrtc::CreateSessionDescription(SessionDescriptionInterface::kPrAnswer,
1026 sdp, NULL);
1027 EXPECT_TRUE(DoSetLocalDescription(pr_answer));
1028 EXPECT_EQ(PeerConnectionInterface::kHaveLocalPrAnswer, observer_.state_);
1029 }
1030
1031 void CreateOfferReceiveAnswer() {
1032 CreateOfferAsLocalDescription();
1033 std::string sdp;
1034 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
1035 CreateAnswerAsRemoteDescription(sdp);
1036 }
1037
1038 void CreateOfferAsLocalDescription() {
kwibergd1fe2812016-04-27 06:47:29 -07001039 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001040 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001041 // TODO(perkj): Currently SetLocalDescription fails if any parameters in an
1042 // audio codec change, even if the parameter has nothing to do with
1043 // receiving. Not all parameters are serialized to SDP.
1044 // Since CreatePrAnswerAsLocalDescription serialize/deserialize
1045 // the SessionDescription, it is necessary to do that here to in order to
1046 // get ReceiveOfferCreatePrAnswerAndAnswer and RenegotiateAudioOnly to pass.
1047 // https://code.google.com/p/webrtc/issues/detail?id=1356
1048 std::string sdp;
1049 EXPECT_TRUE(offer->ToString(&sdp));
1050 SessionDescriptionInterface* new_offer =
1051 webrtc::CreateSessionDescription(
1052 SessionDescriptionInterface::kOffer,
1053 sdp, NULL);
1054
1055 EXPECT_TRUE(DoSetLocalDescription(new_offer));
1056 EXPECT_EQ(PeerConnectionInterface::kHaveLocalOffer, observer_.state_);
mallinath@webrtc.org68cbd012014-01-22 00:16:46 +00001057 // Wait for the ice_complete message, so that SDP will have candidates.
1058 EXPECT_TRUE_WAIT(observer_.ice_complete_, kTimeout);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001059 }
1060
deadbeefab9b2d12015-10-14 11:33:11 -07001061 void CreateAnswerAsRemoteDescription(const std::string& sdp) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001062 webrtc::JsepSessionDescription* answer = new webrtc::JsepSessionDescription(
1063 SessionDescriptionInterface::kAnswer);
deadbeefab9b2d12015-10-14 11:33:11 -07001064 EXPECT_TRUE(answer->Initialize(sdp, NULL));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001065 EXPECT_TRUE(DoSetRemoteDescription(answer));
1066 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
1067 }
1068
deadbeefab9b2d12015-10-14 11:33:11 -07001069 void CreatePrAnswerAndAnswerAsRemoteDescription(const std::string& sdp) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001070 webrtc::JsepSessionDescription* pr_answer =
1071 new webrtc::JsepSessionDescription(
1072 SessionDescriptionInterface::kPrAnswer);
deadbeefab9b2d12015-10-14 11:33:11 -07001073 EXPECT_TRUE(pr_answer->Initialize(sdp, NULL));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001074 EXPECT_TRUE(DoSetRemoteDescription(pr_answer));
1075 EXPECT_EQ(PeerConnectionInterface::kHaveRemotePrAnswer, observer_.state_);
1076 webrtc::JsepSessionDescription* answer =
1077 new webrtc::JsepSessionDescription(
1078 SessionDescriptionInterface::kAnswer);
deadbeefab9b2d12015-10-14 11:33:11 -07001079 EXPECT_TRUE(answer->Initialize(sdp, NULL));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001080 EXPECT_TRUE(DoSetRemoteDescription(answer));
1081 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
1082 }
1083
1084 // Help function used for waiting until a the last signaled remote stream has
1085 // the same label as |stream_label|. In a few of the tests in this file we
1086 // answer with the same session description as we offer and thus we can
1087 // check if OnAddStream have been called with the same stream as we offer to
1088 // send.
1089 void WaitAndVerifyOnAddStream(const std::string& stream_label) {
1090 EXPECT_EQ_WAIT(stream_label, observer_.GetLastAddedStreamLabel(), kTimeout);
1091 }
1092
1093 // Creates an offer and applies it as a local session description.
1094 // Creates an answer with the same SDP an the offer but removes all lines
1095 // that start with a:ssrc"
1096 void CreateOfferReceiveAnswerWithoutSsrc() {
1097 CreateOfferAsLocalDescription();
1098 std::string sdp;
1099 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
1100 SetSsrcToZero(&sdp);
1101 CreateAnswerAsRemoteDescription(sdp);
1102 }
1103
deadbeefab9b2d12015-10-14 11:33:11 -07001104 // This function creates a MediaStream with label kStreams[0] and
1105 // |number_of_audio_tracks| and |number_of_video_tracks| tracks and the
1106 // corresponding SessionDescriptionInterface. The SessionDescriptionInterface
kwiberg2bbff992016-03-16 11:03:04 -07001107 // is returned and the MediaStream is stored in
deadbeefab9b2d12015-10-14 11:33:11 -07001108 // |reference_collection_|
kwibergd1fe2812016-04-27 06:47:29 -07001109 std::unique_ptr<SessionDescriptionInterface>
kwiberg2bbff992016-03-16 11:03:04 -07001110 CreateSessionDescriptionAndReference(size_t number_of_audio_tracks,
1111 size_t number_of_video_tracks) {
1112 EXPECT_LE(number_of_audio_tracks, 2u);
1113 EXPECT_LE(number_of_video_tracks, 2u);
deadbeefab9b2d12015-10-14 11:33:11 -07001114
1115 reference_collection_ = StreamCollection::Create();
1116 std::string sdp_ms1 = std::string(kSdpStringInit);
1117
1118 std::string mediastream_label = kStreams[0];
1119
1120 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream(
1121 webrtc::MediaStream::Create(mediastream_label));
1122 reference_collection_->AddStream(stream);
1123
1124 if (number_of_audio_tracks > 0) {
1125 sdp_ms1 += std::string(kSdpStringAudio);
1126 sdp_ms1 += std::string(kSdpStringMs1Audio0);
1127 AddAudioTrack(kAudioTracks[0], stream);
1128 }
1129 if (number_of_audio_tracks > 1) {
1130 sdp_ms1 += kSdpStringMs1Audio1;
1131 AddAudioTrack(kAudioTracks[1], stream);
1132 }
1133
1134 if (number_of_video_tracks > 0) {
1135 sdp_ms1 += std::string(kSdpStringVideo);
1136 sdp_ms1 += std::string(kSdpStringMs1Video0);
1137 AddVideoTrack(kVideoTracks[0], stream);
1138 }
1139 if (number_of_video_tracks > 1) {
1140 sdp_ms1 += kSdpStringMs1Video1;
1141 AddVideoTrack(kVideoTracks[1], stream);
1142 }
1143
kwibergd1fe2812016-04-27 06:47:29 -07001144 return std::unique_ptr<SessionDescriptionInterface>(
kwiberg2bbff992016-03-16 11:03:04 -07001145 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
1146 sdp_ms1, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07001147 }
1148
1149 void AddAudioTrack(const std::string& track_id,
1150 MediaStreamInterface* stream) {
1151 rtc::scoped_refptr<webrtc::AudioTrackInterface> audio_track(
1152 webrtc::AudioTrack::Create(track_id, nullptr));
1153 ASSERT_TRUE(stream->AddTrack(audio_track));
1154 }
1155
1156 void AddVideoTrack(const std::string& track_id,
1157 MediaStreamInterface* stream) {
1158 rtc::scoped_refptr<webrtc::VideoTrackInterface> video_track(
nisseaf510af2016-03-21 08:20:42 -07001159 webrtc::VideoTrack::Create(track_id,
perkj773be362017-07-31 23:22:01 -07001160 webrtc::FakeVideoTrackSource::Create(),
1161 rtc::Thread::Current()));
deadbeefab9b2d12015-10-14 11:33:11 -07001162 ASSERT_TRUE(stream->AddTrack(video_track));
1163 }
1164
kwibergfd8be342016-05-14 19:44:11 -07001165 std::unique_ptr<SessionDescriptionInterface> CreateOfferWithOneAudioStream() {
deadbeef293e9262017-01-11 12:28:30 -08001166 CreatePeerConnectionWithoutDtls();
zhihuang8f65cdf2016-05-06 18:40:30 -07001167 AddVoiceStream(kStreamLabel1);
kwibergfd8be342016-05-14 19:44:11 -07001168 std::unique_ptr<SessionDescriptionInterface> offer;
zhihuang8f65cdf2016-05-06 18:40:30 -07001169 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
1170 return offer;
1171 }
1172
kwibergfd8be342016-05-14 19:44:11 -07001173 std::unique_ptr<SessionDescriptionInterface>
zhihuang8f65cdf2016-05-06 18:40:30 -07001174 CreateAnswerWithOneAudioStream() {
kwibergfd8be342016-05-14 19:44:11 -07001175 std::unique_ptr<SessionDescriptionInterface> offer =
zhihuang8f65cdf2016-05-06 18:40:30 -07001176 CreateOfferWithOneAudioStream();
1177 EXPECT_TRUE(DoSetRemoteDescription(offer.release()));
kwibergfd8be342016-05-14 19:44:11 -07001178 std::unique_ptr<SessionDescriptionInterface> answer;
zhihuang8f65cdf2016-05-06 18:40:30 -07001179 EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
1180 return answer;
1181 }
1182
1183 const std::string& GetFirstAudioStreamCname(
1184 const SessionDescriptionInterface* desc) {
1185 const cricket::ContentInfo* audio_content =
1186 cricket::GetFirstAudioContent(desc->description());
1187 const cricket::AudioContentDescription* audio_desc =
1188 static_cast<const cricket::AudioContentDescription*>(
1189 audio_content->description);
1190 return audio_desc->streams()[0].cname;
1191 }
1192
zhihuang1c378ed2017-08-17 14:10:50 -07001193 std::unique_ptr<SessionDescriptionInterface> CreateOfferWithOptions(
1194 const RTCOfferAnswerOptions& offer_answer_options) {
1195 RTC_DCHECK(pc_);
1196 rtc::scoped_refptr<MockCreateSessionDescriptionObserver> observer(
1197 new rtc::RefCountedObject<MockCreateSessionDescriptionObserver>());
1198 pc_->CreateOffer(observer, offer_answer_options);
1199 EXPECT_EQ_WAIT(true, observer->called(), kTimeout);
1200 return observer->MoveDescription();
1201 }
1202
1203 void CreateOfferWithOptionsAsRemoteDescription(
1204 std::unique_ptr<SessionDescriptionInterface>* desc,
1205 const RTCOfferAnswerOptions& offer_answer_options) {
1206 *desc = CreateOfferWithOptions(offer_answer_options);
1207 ASSERT_TRUE(desc != nullptr);
1208 std::string sdp;
1209 EXPECT_TRUE((*desc)->ToString(&sdp));
1210 SessionDescriptionInterface* remote_offer =
1211 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
1212 sdp, NULL);
1213 EXPECT_TRUE(DoSetRemoteDescription(remote_offer));
1214 EXPECT_EQ(PeerConnectionInterface::kHaveRemoteOffer, observer_.state_);
1215 }
1216
1217 void CreateOfferWithOptionsAsLocalDescription(
1218 std::unique_ptr<SessionDescriptionInterface>* desc,
1219 const RTCOfferAnswerOptions& offer_answer_options) {
1220 *desc = CreateOfferWithOptions(offer_answer_options);
1221 ASSERT_TRUE(desc != nullptr);
1222 std::string sdp;
1223 EXPECT_TRUE((*desc)->ToString(&sdp));
1224 SessionDescriptionInterface* new_offer = webrtc::CreateSessionDescription(
1225 SessionDescriptionInterface::kOffer, sdp, NULL);
1226
1227 EXPECT_TRUE(DoSetLocalDescription(new_offer));
1228 EXPECT_EQ(PeerConnectionInterface::kHaveLocalOffer, observer_.state_);
1229 }
1230
1231 bool HasCNCodecs(const cricket::ContentInfo* content) {
1232 const cricket::ContentDescription* description = content->description;
1233 RTC_DCHECK(description);
1234 const cricket::AudioContentDescription* audio_content_desc =
1235 static_cast<const cricket::AudioContentDescription*>(description);
1236 RTC_DCHECK(audio_content_desc);
1237 for (size_t i = 0; i < audio_content_desc->codecs().size(); ++i) {
1238 if (audio_content_desc->codecs()[i].name == "CN")
1239 return true;
1240 }
1241 return false;
1242 }
1243
deadbeef9a6f4d42017-05-15 19:43:33 -07001244 std::unique_ptr<rtc::VirtualSocketServer> vss_;
1245 rtc::AutoSocketServerThread main_;
deadbeefd7850b22017-08-23 10:59:19 -07001246 rtc::scoped_refptr<FakeAudioCaptureModule> fake_audio_capture_module_;
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08001247 cricket::FakePortAllocator* port_allocator_ = nullptr;
deadbeef8662f942017-01-20 21:20:51 -08001248 FakeRTCCertificateGenerator* fake_certificate_generator_ = nullptr;
zhihuang9763d562016-08-05 11:14:50 -07001249 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface> pc_factory_;
1250 rtc::scoped_refptr<PeerConnectionFactoryForTest> pc_factory_for_test_;
1251 rtc::scoped_refptr<PeerConnectionInterface> pc_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001252 MockPeerConnectionObserver observer_;
deadbeefab9b2d12015-10-14 11:33:11 -07001253 rtc::scoped_refptr<StreamCollection> reference_collection_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001254};
1255
zhihuang29ff8442016-07-27 11:07:25 -07001256// Test that no callbacks on the PeerConnectionObserver are called after the
1257// PeerConnection is closed.
1258TEST_F(PeerConnectionInterfaceTest, CloseAndTestCallbackFunctions) {
zhihuang9763d562016-08-05 11:14:50 -07001259 rtc::scoped_refptr<PeerConnectionInterface> pc(
zhihuang29ff8442016-07-27 11:07:25 -07001260 pc_factory_for_test_->CreatePeerConnection(
1261 PeerConnectionInterface::RTCConfiguration(), nullptr, nullptr,
1262 nullptr, &observer_));
1263 observer_.SetPeerConnectionInterface(pc.get());
1264 pc->Close();
1265
1266 // No callbacks is expected to be called.
zhihuang81c3a032016-11-17 12:06:24 -08001267 observer_.callback_triggered_ = false;
zhihuang29ff8442016-07-27 11:07:25 -07001268 std::vector<cricket::Candidate> candidates;
1269 pc_factory_for_test_->transport_controller->SignalGatheringState(
1270 cricket::IceGatheringState{});
1271 pc_factory_for_test_->transport_controller->SignalCandidatesGathered(
1272 "", candidates);
1273 pc_factory_for_test_->transport_controller->SignalConnectionState(
1274 cricket::IceConnectionState{});
1275 pc_factory_for_test_->transport_controller->SignalCandidatesRemoved(
1276 candidates);
1277 pc_factory_for_test_->transport_controller->SignalReceiving(false);
zhihuang81c3a032016-11-17 12:06:24 -08001278 EXPECT_FALSE(observer_.callback_triggered_);
zhihuang29ff8442016-07-27 11:07:25 -07001279}
1280
zhihuang8f65cdf2016-05-06 18:40:30 -07001281// Generate different CNAMEs when PeerConnections are created.
1282// The CNAMEs are expected to be generated randomly. It is possible
1283// that the test fails, though the possibility is very low.
1284TEST_F(PeerConnectionInterfaceTest, CnameGenerationInOffer) {
kwibergfd8be342016-05-14 19:44:11 -07001285 std::unique_ptr<SessionDescriptionInterface> offer1 =
zhihuang8f65cdf2016-05-06 18:40:30 -07001286 CreateOfferWithOneAudioStream();
kwibergfd8be342016-05-14 19:44:11 -07001287 std::unique_ptr<SessionDescriptionInterface> offer2 =
zhihuang8f65cdf2016-05-06 18:40:30 -07001288 CreateOfferWithOneAudioStream();
1289 EXPECT_NE(GetFirstAudioStreamCname(offer1.get()),
1290 GetFirstAudioStreamCname(offer2.get()));
1291}
1292
1293TEST_F(PeerConnectionInterfaceTest, CnameGenerationInAnswer) {
kwibergfd8be342016-05-14 19:44:11 -07001294 std::unique_ptr<SessionDescriptionInterface> answer1 =
zhihuang8f65cdf2016-05-06 18:40:30 -07001295 CreateAnswerWithOneAudioStream();
kwibergfd8be342016-05-14 19:44:11 -07001296 std::unique_ptr<SessionDescriptionInterface> answer2 =
zhihuang8f65cdf2016-05-06 18:40:30 -07001297 CreateAnswerWithOneAudioStream();
1298 EXPECT_NE(GetFirstAudioStreamCname(answer1.get()),
1299 GetFirstAudioStreamCname(answer2.get()));
1300}
1301
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001302TEST_F(PeerConnectionInterfaceTest,
1303 CreatePeerConnectionWithDifferentConfigurations) {
1304 CreatePeerConnectionWithDifferentConfigurations();
1305}
1306
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001307TEST_F(PeerConnectionInterfaceTest,
1308 CreatePeerConnectionWithDifferentIceTransportsTypes) {
1309 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kNone);
1310 EXPECT_EQ(cricket::CF_NONE, port_allocator_->candidate_filter());
1311 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kRelay);
1312 EXPECT_EQ(cricket::CF_RELAY, port_allocator_->candidate_filter());
1313 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kNoHost);
1314 EXPECT_EQ(cricket::CF_ALL & ~cricket::CF_HOST,
1315 port_allocator_->candidate_filter());
1316 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kAll);
1317 EXPECT_EQ(cricket::CF_ALL, port_allocator_->candidate_filter());
1318}
1319
1320// Test that when a PeerConnection is created with a nonzero candidate pool
1321// size, the pooled PortAllocatorSession is created with all the attributes
1322// in the RTCConfiguration.
1323TEST_F(PeerConnectionInterfaceTest, CreatePeerConnectionWithPooledCandidates) {
1324 PeerConnectionInterface::RTCConfiguration config;
1325 PeerConnectionInterface::IceServer server;
1326 server.uri = kStunAddressOnly;
1327 config.servers.push_back(server);
1328 config.type = PeerConnectionInterface::kRelay;
1329 config.disable_ipv6 = true;
1330 config.tcp_candidate_policy =
1331 PeerConnectionInterface::kTcpCandidatePolicyDisabled;
honghaiz60347052016-05-31 18:29:12 -07001332 config.candidate_network_policy =
1333 PeerConnectionInterface::kCandidateNetworkPolicyLowCost;
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001334 config.ice_candidate_pool_size = 1;
1335 CreatePeerConnection(config, nullptr);
1336
1337 const cricket::FakePortAllocatorSession* session =
1338 static_cast<const cricket::FakePortAllocatorSession*>(
1339 port_allocator_->GetPooledSession());
1340 ASSERT_NE(nullptr, session);
1341 EXPECT_EQ(1UL, session->stun_servers().size());
1342 EXPECT_EQ(0U, session->flags() & cricket::PORTALLOCATOR_ENABLE_IPV6);
1343 EXPECT_LT(0U, session->flags() & cricket::PORTALLOCATOR_DISABLE_TCP);
honghaiz60347052016-05-31 18:29:12 -07001344 EXPECT_LT(0U,
1345 session->flags() & cricket::PORTALLOCATOR_DISABLE_COSTLY_NETWORKS);
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001346}
1347
deadbeefd21eab32017-07-26 16:50:11 -07001348// Test that network-related RTCConfiguration members are applied to the
1349// PortAllocator when CreatePeerConnection is called. Specifically:
1350// - disable_ipv6_on_wifi
1351// - max_ipv6_networks
1352// - tcp_candidate_policy
1353// - candidate_network_policy
1354// - prune_turn_ports
1355//
1356// Note that the candidate filter (RTCConfiguration::type) is already tested
1357// above.
1358TEST_F(PeerConnectionInterfaceTest,
1359 CreatePeerConnectionAppliesNetworkConfigToPortAllocator) {
1360 // Create fake port allocator.
1361 std::unique_ptr<cricket::FakePortAllocator> port_allocator(
1362 new cricket::FakePortAllocator(rtc::Thread::Current(), nullptr));
1363 cricket::FakePortAllocator* raw_port_allocator = port_allocator.get();
1364
1365 // Create RTCConfiguration with some network-related fields relevant to
1366 // PortAllocator populated.
1367 PeerConnectionInterface::RTCConfiguration config;
1368 config.disable_ipv6_on_wifi = true;
1369 config.max_ipv6_networks = 10;
1370 config.tcp_candidate_policy =
1371 PeerConnectionInterface::kTcpCandidatePolicyDisabled;
1372 config.candidate_network_policy =
1373 PeerConnectionInterface::kCandidateNetworkPolicyLowCost;
1374 config.prune_turn_ports = true;
1375
1376 // Create the PC factory and PC with the above config.
1377 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface> pc_factory(
1378 webrtc::CreatePeerConnectionFactory(
1379 rtc::Thread::Current(), rtc::Thread::Current(),
deadbeefd7850b22017-08-23 10:59:19 -07001380 rtc::Thread::Current(), fake_audio_capture_module_, nullptr,
1381 nullptr));
deadbeefd21eab32017-07-26 16:50:11 -07001382 rtc::scoped_refptr<PeerConnectionInterface> pc(
1383 pc_factory->CreatePeerConnection(
1384 config, nullptr, std::move(port_allocator), nullptr, &observer_));
1385
1386 // Now validate that the config fields set above were applied to the
1387 // PortAllocator, as flags or otherwise.
1388 EXPECT_FALSE(raw_port_allocator->flags() &
1389 cricket::PORTALLOCATOR_ENABLE_IPV6_ON_WIFI);
1390 EXPECT_EQ(10, raw_port_allocator->max_ipv6_networks());
1391 EXPECT_TRUE(raw_port_allocator->flags() & cricket::PORTALLOCATOR_DISABLE_TCP);
1392 EXPECT_TRUE(raw_port_allocator->flags() &
1393 cricket::PORTALLOCATOR_DISABLE_COSTLY_NETWORKS);
1394 EXPECT_TRUE(raw_port_allocator->prune_turn_ports());
1395}
1396
Taylor Brandstetterf8e65772016-06-27 17:20:15 -07001397// Test that the PeerConnection initializes the port allocator passed into it,
1398// and on the correct thread.
1399TEST_F(PeerConnectionInterfaceTest,
deadbeefd21eab32017-07-26 16:50:11 -07001400 CreatePeerConnectionInitializesPortAllocatorOnNetworkThread) {
tommie7251592017-07-14 14:44:46 -07001401 std::unique_ptr<rtc::Thread> network_thread(
1402 rtc::Thread::CreateWithSocketServer());
1403 network_thread->Start();
Taylor Brandstetterf8e65772016-06-27 17:20:15 -07001404 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface> pc_factory(
1405 webrtc::CreatePeerConnectionFactory(
tommie7251592017-07-14 14:44:46 -07001406 network_thread.get(), rtc::Thread::Current(), rtc::Thread::Current(),
deadbeefd7850b22017-08-23 10:59:19 -07001407 fake_audio_capture_module_, nullptr, nullptr));
Taylor Brandstetterf8e65772016-06-27 17:20:15 -07001408 std::unique_ptr<cricket::FakePortAllocator> port_allocator(
tommie7251592017-07-14 14:44:46 -07001409 new cricket::FakePortAllocator(network_thread.get(), nullptr));
Taylor Brandstetterf8e65772016-06-27 17:20:15 -07001410 cricket::FakePortAllocator* raw_port_allocator = port_allocator.get();
1411 PeerConnectionInterface::RTCConfiguration config;
1412 rtc::scoped_refptr<PeerConnectionInterface> pc(
1413 pc_factory->CreatePeerConnection(
1414 config, nullptr, std::move(port_allocator), nullptr, &observer_));
1415 // FakePortAllocator RTC_CHECKs that it's initialized on the right thread,
1416 // so all we have to do here is check that it's initialized.
1417 EXPECT_TRUE(raw_port_allocator->initialized());
1418}
1419
deadbeef46c73892016-11-16 19:42:04 -08001420// Check that GetConfiguration returns the configuration the PeerConnection was
1421// constructed with, before SetConfiguration is called.
1422TEST_F(PeerConnectionInterfaceTest, GetConfigurationAfterCreatePeerConnection) {
1423 PeerConnectionInterface::RTCConfiguration config;
1424 config.type = PeerConnectionInterface::kRelay;
1425 CreatePeerConnection(config, nullptr);
1426
1427 PeerConnectionInterface::RTCConfiguration returned_config =
1428 pc_->GetConfiguration();
1429 EXPECT_EQ(PeerConnectionInterface::kRelay, returned_config.type);
1430}
1431
1432// Check that GetConfiguration returns the last configuration passed into
1433// SetConfiguration.
1434TEST_F(PeerConnectionInterfaceTest, GetConfigurationAfterSetConfiguration) {
1435 CreatePeerConnection();
1436
1437 PeerConnectionInterface::RTCConfiguration config;
1438 config.type = PeerConnectionInterface::kRelay;
1439 EXPECT_TRUE(pc_->SetConfiguration(config));
1440
1441 PeerConnectionInterface::RTCConfiguration returned_config =
1442 pc_->GetConfiguration();
1443 EXPECT_EQ(PeerConnectionInterface::kRelay, returned_config.type);
1444}
1445
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001446TEST_F(PeerConnectionInterfaceTest, AddStreams) {
deadbeef293e9262017-01-11 12:28:30 -08001447 CreatePeerConnectionWithoutDtls();
deadbeefab9b2d12015-10-14 11:33:11 -07001448 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001449 AddVoiceStream(kStreamLabel2);
1450 ASSERT_EQ(2u, pc_->local_streams()->count());
1451
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001452 // Test we can add multiple local streams to one peerconnection.
zhihuang9763d562016-08-05 11:14:50 -07001453 rtc::scoped_refptr<MediaStreamInterface> stream(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001454 pc_factory_->CreateLocalMediaStream(kStreamLabel3));
zhihuang9763d562016-08-05 11:14:50 -07001455 rtc::scoped_refptr<AudioTrackInterface> audio_track(
1456 pc_factory_->CreateAudioTrack(kStreamLabel3,
1457 static_cast<AudioSourceInterface*>(NULL)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001458 stream->AddTrack(audio_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +00001459 EXPECT_TRUE(pc_->AddStream(stream));
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001460 EXPECT_EQ(3u, pc_->local_streams()->count());
1461
1462 // Remove the third stream.
1463 pc_->RemoveStream(pc_->local_streams()->at(2));
1464 EXPECT_EQ(2u, pc_->local_streams()->count());
1465
1466 // Remove the second stream.
1467 pc_->RemoveStream(pc_->local_streams()->at(1));
1468 EXPECT_EQ(1u, pc_->local_streams()->count());
1469
1470 // Remove the first stream.
1471 pc_->RemoveStream(pc_->local_streams()->at(0));
1472 EXPECT_EQ(0u, pc_->local_streams()->count());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001473}
1474
deadbeefab9b2d12015-10-14 11:33:11 -07001475// Test that the created offer includes streams we added.
1476TEST_F(PeerConnectionInterfaceTest, AddedStreamsPresentInOffer) {
deadbeef293e9262017-01-11 12:28:30 -08001477 CreatePeerConnectionWithoutDtls();
deadbeefab9b2d12015-10-14 11:33:11 -07001478 AddAudioVideoStream(kStreamLabel1, "audio_track", "video_track");
kwibergd1fe2812016-04-27 06:47:29 -07001479 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001480 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07001481
1482 const cricket::ContentInfo* audio_content =
1483 cricket::GetFirstAudioContent(offer->description());
1484 const cricket::AudioContentDescription* audio_desc =
1485 static_cast<const cricket::AudioContentDescription*>(
1486 audio_content->description);
1487 EXPECT_TRUE(
1488 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
1489
1490 const cricket::ContentInfo* video_content =
1491 cricket::GetFirstVideoContent(offer->description());
1492 const cricket::VideoContentDescription* video_desc =
1493 static_cast<const cricket::VideoContentDescription*>(
1494 video_content->description);
1495 EXPECT_TRUE(
1496 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1497
1498 // Add another stream and ensure the offer includes both the old and new
1499 // streams.
1500 AddAudioVideoStream(kStreamLabel2, "audio_track2", "video_track2");
kwiberg2bbff992016-03-16 11:03:04 -07001501 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07001502
1503 audio_content = cricket::GetFirstAudioContent(offer->description());
1504 audio_desc = static_cast<const cricket::AudioContentDescription*>(
1505 audio_content->description);
1506 EXPECT_TRUE(
1507 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
1508 EXPECT_TRUE(
1509 ContainsTrack(audio_desc->streams(), kStreamLabel2, "audio_track2"));
1510
1511 video_content = cricket::GetFirstVideoContent(offer->description());
1512 video_desc = static_cast<const cricket::VideoContentDescription*>(
1513 video_content->description);
1514 EXPECT_TRUE(
1515 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1516 EXPECT_TRUE(
1517 ContainsTrack(video_desc->streams(), kStreamLabel2, "video_track2"));
1518}
1519
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001520TEST_F(PeerConnectionInterfaceTest, RemoveStream) {
deadbeef293e9262017-01-11 12:28:30 -08001521 CreatePeerConnectionWithoutDtls();
deadbeefab9b2d12015-10-14 11:33:11 -07001522 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001523 ASSERT_EQ(1u, pc_->local_streams()->count());
1524 pc_->RemoveStream(pc_->local_streams()->at(0));
1525 EXPECT_EQ(0u, pc_->local_streams()->count());
1526}
1527
deadbeefe1f9d832016-01-14 15:35:42 -08001528// Test for AddTrack and RemoveTrack methods.
1529// Tests that the created offer includes tracks we added,
1530// and that the RtpSenders are created correctly.
1531// Also tests that RemoveTrack removes the tracks from subsequent offers.
1532TEST_F(PeerConnectionInterfaceTest, AddTrackRemoveTrack) {
deadbeef293e9262017-01-11 12:28:30 -08001533 CreatePeerConnectionWithoutDtls();
deadbeefe1f9d832016-01-14 15:35:42 -08001534 // Create a dummy stream, so tracks share a stream label.
zhihuang9763d562016-08-05 11:14:50 -07001535 rtc::scoped_refptr<MediaStreamInterface> stream(
deadbeefe1f9d832016-01-14 15:35:42 -08001536 pc_factory_->CreateLocalMediaStream(kStreamLabel1));
1537 std::vector<MediaStreamInterface*> stream_list;
1538 stream_list.push_back(stream.get());
zhihuang9763d562016-08-05 11:14:50 -07001539 rtc::scoped_refptr<AudioTrackInterface> audio_track(
deadbeefe1f9d832016-01-14 15:35:42 -08001540 pc_factory_->CreateAudioTrack("audio_track", nullptr));
zhihuang9763d562016-08-05 11:14:50 -07001541 rtc::scoped_refptr<VideoTrackInterface> video_track(
1542 pc_factory_->CreateVideoTrack(
deadbeef112b2e92017-02-10 20:13:37 -08001543 "video_track", pc_factory_->CreateVideoSource(
1544 std::unique_ptr<cricket::VideoCapturer>(
1545 new cricket::FakeVideoCapturer()))));
deadbeefe1f9d832016-01-14 15:35:42 -08001546 auto audio_sender = pc_->AddTrack(audio_track, stream_list);
1547 auto video_sender = pc_->AddTrack(video_track, stream_list);
deadbeefa601f5c2016-06-06 14:27:39 -07001548 EXPECT_EQ(1UL, audio_sender->stream_ids().size());
1549 EXPECT_EQ(kStreamLabel1, audio_sender->stream_ids()[0]);
deadbeefe1f9d832016-01-14 15:35:42 -08001550 EXPECT_EQ("audio_track", audio_sender->id());
1551 EXPECT_EQ(audio_track, audio_sender->track());
deadbeefa601f5c2016-06-06 14:27:39 -07001552 EXPECT_EQ(1UL, video_sender->stream_ids().size());
1553 EXPECT_EQ(kStreamLabel1, video_sender->stream_ids()[0]);
deadbeefe1f9d832016-01-14 15:35:42 -08001554 EXPECT_EQ("video_track", video_sender->id());
1555 EXPECT_EQ(video_track, video_sender->track());
1556
1557 // Now create an offer and check for the senders.
kwibergd1fe2812016-04-27 06:47:29 -07001558 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001559 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefe1f9d832016-01-14 15:35:42 -08001560
1561 const cricket::ContentInfo* audio_content =
1562 cricket::GetFirstAudioContent(offer->description());
1563 const cricket::AudioContentDescription* audio_desc =
1564 static_cast<const cricket::AudioContentDescription*>(
1565 audio_content->description);
1566 EXPECT_TRUE(
1567 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
1568
1569 const cricket::ContentInfo* video_content =
1570 cricket::GetFirstVideoContent(offer->description());
1571 const cricket::VideoContentDescription* video_desc =
1572 static_cast<const cricket::VideoContentDescription*>(
1573 video_content->description);
1574 EXPECT_TRUE(
1575 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1576
1577 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
1578
1579 // Now try removing the tracks.
1580 EXPECT_TRUE(pc_->RemoveTrack(audio_sender));
1581 EXPECT_TRUE(pc_->RemoveTrack(video_sender));
1582
1583 // Create a new offer and ensure it doesn't contain the removed senders.
kwiberg2bbff992016-03-16 11:03:04 -07001584 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefe1f9d832016-01-14 15:35:42 -08001585
1586 audio_content = cricket::GetFirstAudioContent(offer->description());
1587 audio_desc = static_cast<const cricket::AudioContentDescription*>(
1588 audio_content->description);
1589 EXPECT_FALSE(
1590 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
1591
1592 video_content = cricket::GetFirstVideoContent(offer->description());
1593 video_desc = static_cast<const cricket::VideoContentDescription*>(
1594 video_content->description);
1595 EXPECT_FALSE(
1596 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1597
1598 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
1599
1600 // Calling RemoveTrack on a sender no longer attached to a PeerConnection
1601 // should return false.
1602 EXPECT_FALSE(pc_->RemoveTrack(audio_sender));
1603 EXPECT_FALSE(pc_->RemoveTrack(video_sender));
1604}
1605
1606// Test creating senders without a stream specified,
1607// expecting a random stream ID to be generated.
1608TEST_F(PeerConnectionInterfaceTest, AddTrackWithoutStream) {
deadbeef293e9262017-01-11 12:28:30 -08001609 CreatePeerConnectionWithoutDtls();
deadbeefe1f9d832016-01-14 15:35:42 -08001610 // Create a dummy stream, so tracks share a stream label.
zhihuang9763d562016-08-05 11:14:50 -07001611 rtc::scoped_refptr<AudioTrackInterface> audio_track(
deadbeefe1f9d832016-01-14 15:35:42 -08001612 pc_factory_->CreateAudioTrack("audio_track", nullptr));
zhihuang9763d562016-08-05 11:14:50 -07001613 rtc::scoped_refptr<VideoTrackInterface> video_track(
1614 pc_factory_->CreateVideoTrack(
deadbeef112b2e92017-02-10 20:13:37 -08001615 "video_track", pc_factory_->CreateVideoSource(
1616 std::unique_ptr<cricket::VideoCapturer>(
1617 new cricket::FakeVideoCapturer()))));
deadbeefe1f9d832016-01-14 15:35:42 -08001618 auto audio_sender =
1619 pc_->AddTrack(audio_track, std::vector<MediaStreamInterface*>());
1620 auto video_sender =
1621 pc_->AddTrack(video_track, std::vector<MediaStreamInterface*>());
1622 EXPECT_EQ("audio_track", audio_sender->id());
1623 EXPECT_EQ(audio_track, audio_sender->track());
1624 EXPECT_EQ("video_track", video_sender->id());
1625 EXPECT_EQ(video_track, video_sender->track());
1626 // If the ID is truly a random GUID, it should be infinitely unlikely they
1627 // will be the same.
deadbeefa601f5c2016-06-06 14:27:39 -07001628 EXPECT_NE(video_sender->stream_ids(), audio_sender->stream_ids());
deadbeefe1f9d832016-01-14 15:35:42 -08001629}
1630
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001631TEST_F(PeerConnectionInterfaceTest, CreateOfferReceiveAnswer) {
1632 InitiateCall();
1633 WaitAndVerifyOnAddStream(kStreamLabel1);
1634 VerifyRemoteRtpHeaderExtensions();
1635}
1636
1637TEST_F(PeerConnectionInterfaceTest, CreateOfferReceivePrAnswerAndAnswer) {
deadbeef293e9262017-01-11 12:28:30 -08001638 CreatePeerConnectionWithoutDtls();
deadbeefab9b2d12015-10-14 11:33:11 -07001639 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001640 CreateOfferAsLocalDescription();
1641 std::string offer;
1642 EXPECT_TRUE(pc_->local_description()->ToString(&offer));
1643 CreatePrAnswerAndAnswerAsRemoteDescription(offer);
1644 WaitAndVerifyOnAddStream(kStreamLabel1);
1645}
1646
1647TEST_F(PeerConnectionInterfaceTest, ReceiveOfferCreateAnswer) {
deadbeef293e9262017-01-11 12:28:30 -08001648 CreatePeerConnectionWithoutDtls();
deadbeefab9b2d12015-10-14 11:33:11 -07001649 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001650
1651 CreateOfferAsRemoteDescription();
1652 CreateAnswerAsLocalDescription();
1653
1654 WaitAndVerifyOnAddStream(kStreamLabel1);
1655}
1656
1657TEST_F(PeerConnectionInterfaceTest, ReceiveOfferCreatePrAnswerAndAnswer) {
deadbeef293e9262017-01-11 12:28:30 -08001658 CreatePeerConnectionWithoutDtls();
deadbeefab9b2d12015-10-14 11:33:11 -07001659 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001660
1661 CreateOfferAsRemoteDescription();
1662 CreatePrAnswerAsLocalDescription();
1663 CreateAnswerAsLocalDescription();
1664
1665 WaitAndVerifyOnAddStream(kStreamLabel1);
1666}
1667
1668TEST_F(PeerConnectionInterfaceTest, Renegotiate) {
1669 InitiateCall();
1670 ASSERT_EQ(1u, pc_->remote_streams()->count());
1671 pc_->RemoveStream(pc_->local_streams()->at(0));
1672 CreateOfferReceiveAnswer();
1673 EXPECT_EQ(0u, pc_->remote_streams()->count());
deadbeefab9b2d12015-10-14 11:33:11 -07001674 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001675 CreateOfferReceiveAnswer();
1676}
1677
1678// Tests that after negotiating an audio only call, the respondent can perform a
1679// renegotiation that removes the audio stream.
1680TEST_F(PeerConnectionInterfaceTest, RenegotiateAudioOnly) {
deadbeef293e9262017-01-11 12:28:30 -08001681 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001682 AddVoiceStream(kStreamLabel1);
1683 CreateOfferAsRemoteDescription();
1684 CreateAnswerAsLocalDescription();
1685
1686 ASSERT_EQ(1u, pc_->remote_streams()->count());
1687 pc_->RemoveStream(pc_->local_streams()->at(0));
1688 CreateOfferReceiveAnswer();
1689 EXPECT_EQ(0u, pc_->remote_streams()->count());
1690}
1691
1692// Test that candidates are generated and that we can parse our own candidates.
1693TEST_F(PeerConnectionInterfaceTest, IceCandidates) {
deadbeef293e9262017-01-11 12:28:30 -08001694 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001695
1696 EXPECT_FALSE(pc_->AddIceCandidate(observer_.last_candidate_.get()));
1697 // SetRemoteDescription takes ownership of offer.
kwibergd1fe2812016-04-27 06:47:29 -07001698 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefab9b2d12015-10-14 11:33:11 -07001699 AddVideoStream(kStreamLabel1);
deadbeefc80741f2015-10-22 13:14:45 -07001700 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
kwiberg2bbff992016-03-16 11:03:04 -07001701 EXPECT_TRUE(DoSetRemoteDescription(offer.release()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001702
1703 // SetLocalDescription takes ownership of answer.
kwibergd1fe2812016-04-27 06:47:29 -07001704 std::unique_ptr<SessionDescriptionInterface> answer;
deadbeefc80741f2015-10-22 13:14:45 -07001705 EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
kwiberg2bbff992016-03-16 11:03:04 -07001706 EXPECT_TRUE(DoSetLocalDescription(answer.release()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001707
1708 EXPECT_TRUE_WAIT(observer_.last_candidate_.get() != NULL, kTimeout);
1709 EXPECT_TRUE_WAIT(observer_.ice_complete_, kTimeout);
1710
1711 EXPECT_TRUE(pc_->AddIceCandidate(observer_.last_candidate_.get()));
1712}
1713
deadbeefab9b2d12015-10-14 11:33:11 -07001714// Test that CreateOffer and CreateAnswer will fail if the track labels are
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001715// not unique.
1716TEST_F(PeerConnectionInterfaceTest, CreateOfferAnswerWithInvalidStream) {
deadbeef293e9262017-01-11 12:28:30 -08001717 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001718 // Create a regular offer for the CreateAnswer test later.
kwibergd1fe2812016-04-27 06:47:29 -07001719 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefc80741f2015-10-22 13:14:45 -07001720 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
kwiberg2bbff992016-03-16 11:03:04 -07001721 EXPECT_TRUE(offer);
1722 offer.reset();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001723
1724 // Create a local stream with audio&video tracks having same label.
1725 AddAudioVideoStream(kStreamLabel1, "track_label", "track_label");
1726
1727 // Test CreateOffer
deadbeefc80741f2015-10-22 13:14:45 -07001728 EXPECT_FALSE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001729
1730 // Test CreateAnswer
kwibergd1fe2812016-04-27 06:47:29 -07001731 std::unique_ptr<SessionDescriptionInterface> answer;
deadbeefc80741f2015-10-22 13:14:45 -07001732 EXPECT_FALSE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001733}
1734
1735// Test that we will get different SSRCs for each tracks in the offer and answer
1736// we created.
1737TEST_F(PeerConnectionInterfaceTest, SsrcInOfferAnswer) {
deadbeef293e9262017-01-11 12:28:30 -08001738 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001739 // Create a local stream with audio&video tracks having different labels.
1740 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
1741
1742 // Test CreateOffer
kwibergd1fe2812016-04-27 06:47:29 -07001743 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001744 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001745 int audio_ssrc = 0;
1746 int video_ssrc = 0;
1747 EXPECT_TRUE(GetFirstSsrc(GetFirstAudioContent(offer->description()),
1748 &audio_ssrc));
1749 EXPECT_TRUE(GetFirstSsrc(GetFirstVideoContent(offer->description()),
1750 &video_ssrc));
1751 EXPECT_NE(audio_ssrc, video_ssrc);
1752
1753 // Test CreateAnswer
1754 EXPECT_TRUE(DoSetRemoteDescription(offer.release()));
kwibergd1fe2812016-04-27 06:47:29 -07001755 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -07001756 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001757 audio_ssrc = 0;
1758 video_ssrc = 0;
1759 EXPECT_TRUE(GetFirstSsrc(GetFirstAudioContent(answer->description()),
1760 &audio_ssrc));
1761 EXPECT_TRUE(GetFirstSsrc(GetFirstVideoContent(answer->description()),
1762 &video_ssrc));
1763 EXPECT_NE(audio_ssrc, video_ssrc);
1764}
1765
deadbeefeb459812015-12-15 19:24:43 -08001766// Test that it's possible to call AddTrack on a MediaStream after adding
1767// the stream to a PeerConnection.
1768// TODO(deadbeef): Remove this test once this behavior is no longer supported.
1769TEST_F(PeerConnectionInterfaceTest, AddTrackAfterAddStream) {
deadbeef293e9262017-01-11 12:28:30 -08001770 CreatePeerConnectionWithoutDtls();
deadbeefeb459812015-12-15 19:24:43 -08001771 // Create audio stream and add to PeerConnection.
1772 AddVoiceStream(kStreamLabel1);
1773 MediaStreamInterface* stream = pc_->local_streams()->at(0);
1774
1775 // Add video track to the audio-only stream.
zhihuang9763d562016-08-05 11:14:50 -07001776 rtc::scoped_refptr<VideoTrackInterface> video_track(
1777 pc_factory_->CreateVideoTrack(
deadbeef112b2e92017-02-10 20:13:37 -08001778 "video_label", pc_factory_->CreateVideoSource(
1779 std::unique_ptr<cricket::VideoCapturer>(
1780 new cricket::FakeVideoCapturer()))));
deadbeefeb459812015-12-15 19:24:43 -08001781 stream->AddTrack(video_track.get());
1782
kwibergd1fe2812016-04-27 06:47:29 -07001783 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001784 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefeb459812015-12-15 19:24:43 -08001785
1786 const cricket::MediaContentDescription* video_desc =
1787 cricket::GetFirstVideoContentDescription(offer->description());
1788 EXPECT_TRUE(video_desc != nullptr);
1789}
1790
1791// Test that it's possible to call RemoveTrack on a MediaStream after adding
1792// the stream to a PeerConnection.
1793// TODO(deadbeef): Remove this test once this behavior is no longer supported.
1794TEST_F(PeerConnectionInterfaceTest, RemoveTrackAfterAddStream) {
deadbeef293e9262017-01-11 12:28:30 -08001795 CreatePeerConnectionWithoutDtls();
deadbeefeb459812015-12-15 19:24:43 -08001796 // Create audio/video stream and add to PeerConnection.
1797 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
1798 MediaStreamInterface* stream = pc_->local_streams()->at(0);
1799
1800 // Remove the video track.
1801 stream->RemoveTrack(stream->GetVideoTracks()[0]);
1802
kwibergd1fe2812016-04-27 06:47:29 -07001803 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001804 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefeb459812015-12-15 19:24:43 -08001805
1806 const cricket::MediaContentDescription* video_desc =
1807 cricket::GetFirstVideoContentDescription(offer->description());
1808 EXPECT_TRUE(video_desc == nullptr);
1809}
1810
deadbeef1dcb1642017-03-29 21:08:16 -07001811// Verify that CreateDtmfSender only succeeds if called with a valid local
1812// track. Other aspects of DtmfSenders are tested in
1813// peerconnection_integrationtest.cc.
1814TEST_F(PeerConnectionInterfaceTest, CreateDtmfSenderWithInvalidParams) {
1815 CreatePeerConnection();
1816 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
1817 EXPECT_EQ(nullptr, pc_->CreateDtmfSender(nullptr));
1818 rtc::scoped_refptr<webrtc::AudioTrackInterface> non_localtrack(
1819 pc_factory_->CreateAudioTrack("dummy_track", nullptr));
1820 EXPECT_EQ(nullptr, pc_->CreateDtmfSender(non_localtrack));
1821}
1822
deadbeefbd7d8f72015-12-18 16:58:44 -08001823// Test creating a sender with a stream ID, and ensure the ID is populated
1824// in the offer.
1825TEST_F(PeerConnectionInterfaceTest, CreateSenderWithStream) {
deadbeef293e9262017-01-11 12:28:30 -08001826 CreatePeerConnectionWithoutDtls();
deadbeefbd7d8f72015-12-18 16:58:44 -08001827 pc_->CreateSender("video", kStreamLabel1);
1828
kwibergd1fe2812016-04-27 06:47:29 -07001829 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001830 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefbd7d8f72015-12-18 16:58:44 -08001831
1832 const cricket::MediaContentDescription* video_desc =
1833 cricket::GetFirstVideoContentDescription(offer->description());
1834 ASSERT_TRUE(video_desc != nullptr);
1835 ASSERT_EQ(1u, video_desc->streams().size());
1836 EXPECT_EQ(kStreamLabel1, video_desc->streams()[0].sync_label);
1837}
1838
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001839// Test that we can specify a certain track that we want statistics about.
1840TEST_F(PeerConnectionInterfaceTest, GetStatsForSpecificTrack) {
1841 InitiateCall();
1842 ASSERT_LT(0u, pc_->remote_streams()->count());
1843 ASSERT_LT(0u, pc_->remote_streams()->at(0)->GetAudioTracks().size());
zhihuang9763d562016-08-05 11:14:50 -07001844 rtc::scoped_refptr<MediaStreamTrackInterface> remote_audio =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001845 pc_->remote_streams()->at(0)->GetAudioTracks()[0];
1846 EXPECT_TRUE(DoGetStats(remote_audio));
1847
1848 // Remove the stream. Since we are sending to our selves the local
1849 // and the remote stream is the same.
1850 pc_->RemoveStream(pc_->local_streams()->at(0));
1851 // Do a re-negotiation.
1852 CreateOfferReceiveAnswer();
1853
1854 ASSERT_EQ(0u, pc_->remote_streams()->count());
1855
1856 // Test that we still can get statistics for the old track. Even if it is not
1857 // sent any longer.
1858 EXPECT_TRUE(DoGetStats(remote_audio));
1859}
1860
1861// Test that we can get stats on a video track.
1862TEST_F(PeerConnectionInterfaceTest, GetStatsForVideoTrack) {
1863 InitiateCall();
1864 ASSERT_LT(0u, pc_->remote_streams()->count());
1865 ASSERT_LT(0u, pc_->remote_streams()->at(0)->GetVideoTracks().size());
zhihuang9763d562016-08-05 11:14:50 -07001866 rtc::scoped_refptr<MediaStreamTrackInterface> remote_video =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001867 pc_->remote_streams()->at(0)->GetVideoTracks()[0];
1868 EXPECT_TRUE(DoGetStats(remote_video));
1869}
1870
1871// Test that we don't get statistics for an invalid track.
zhihuange9e94c32016-11-04 11:38:15 -07001872TEST_F(PeerConnectionInterfaceTest, GetStatsForInvalidTrack) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001873 InitiateCall();
zhihuang9763d562016-08-05 11:14:50 -07001874 rtc::scoped_refptr<AudioTrackInterface> unknown_audio_track(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001875 pc_factory_->CreateAudioTrack("unknown track", NULL));
1876 EXPECT_FALSE(DoGetStats(unknown_audio_track));
1877}
1878
1879// This test setup two RTP data channels in loop back.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001880TEST_F(PeerConnectionInterfaceTest, TestDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001881 FakeConstraints constraints;
1882 constraints.SetAllowRtpDataChannels();
1883 CreatePeerConnection(&constraints);
zhihuang9763d562016-08-05 11:14:50 -07001884 rtc::scoped_refptr<DataChannelInterface> data1 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001885 pc_->CreateDataChannel("test1", NULL);
zhihuang9763d562016-08-05 11:14:50 -07001886 rtc::scoped_refptr<DataChannelInterface> data2 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001887 pc_->CreateDataChannel("test2", NULL);
1888 ASSERT_TRUE(data1 != NULL);
kwibergd1fe2812016-04-27 06:47:29 -07001889 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001890 new MockDataChannelObserver(data1));
kwibergd1fe2812016-04-27 06:47:29 -07001891 std::unique_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001892 new MockDataChannelObserver(data2));
1893
1894 EXPECT_EQ(DataChannelInterface::kConnecting, data1->state());
1895 EXPECT_EQ(DataChannelInterface::kConnecting, data2->state());
1896 std::string data_to_send1 = "testing testing";
1897 std::string data_to_send2 = "testing something else";
1898 EXPECT_FALSE(data1->Send(DataBuffer(data_to_send1)));
1899
1900 CreateOfferReceiveAnswer();
1901 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1902 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
1903
1904 EXPECT_EQ(DataChannelInterface::kOpen, data1->state());
1905 EXPECT_EQ(DataChannelInterface::kOpen, data2->state());
1906 EXPECT_TRUE(data1->Send(DataBuffer(data_to_send1)));
1907 EXPECT_TRUE(data2->Send(DataBuffer(data_to_send2)));
1908
1909 EXPECT_EQ_WAIT(data_to_send1, observer1->last_message(), kTimeout);
1910 EXPECT_EQ_WAIT(data_to_send2, observer2->last_message(), kTimeout);
1911
1912 data1->Close();
1913 EXPECT_EQ(DataChannelInterface::kClosing, data1->state());
1914 CreateOfferReceiveAnswer();
1915 EXPECT_FALSE(observer1->IsOpen());
1916 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
1917 EXPECT_TRUE(observer2->IsOpen());
1918
1919 data_to_send2 = "testing something else again";
1920 EXPECT_TRUE(data2->Send(DataBuffer(data_to_send2)));
1921
1922 EXPECT_EQ_WAIT(data_to_send2, observer2->last_message(), kTimeout);
1923}
1924
1925// This test verifies that sendnig binary data over RTP data channels should
1926// fail.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001927TEST_F(PeerConnectionInterfaceTest, TestSendBinaryOnRtpDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001928 FakeConstraints constraints;
1929 constraints.SetAllowRtpDataChannels();
1930 CreatePeerConnection(&constraints);
zhihuang9763d562016-08-05 11:14:50 -07001931 rtc::scoped_refptr<DataChannelInterface> data1 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001932 pc_->CreateDataChannel("test1", NULL);
zhihuang9763d562016-08-05 11:14:50 -07001933 rtc::scoped_refptr<DataChannelInterface> data2 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001934 pc_->CreateDataChannel("test2", NULL);
1935 ASSERT_TRUE(data1 != NULL);
kwibergd1fe2812016-04-27 06:47:29 -07001936 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001937 new MockDataChannelObserver(data1));
kwibergd1fe2812016-04-27 06:47:29 -07001938 std::unique_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001939 new MockDataChannelObserver(data2));
1940
1941 EXPECT_EQ(DataChannelInterface::kConnecting, data1->state());
1942 EXPECT_EQ(DataChannelInterface::kConnecting, data2->state());
1943
1944 CreateOfferReceiveAnswer();
1945 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1946 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
1947
1948 EXPECT_EQ(DataChannelInterface::kOpen, data1->state());
1949 EXPECT_EQ(DataChannelInterface::kOpen, data2->state());
1950
jbaucheec21bd2016-03-20 06:15:43 -07001951 rtc::CopyOnWriteBuffer buffer("test", 4);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001952 EXPECT_FALSE(data1->Send(DataBuffer(buffer, true)));
1953}
1954
1955// This test setup a RTP data channels in loop back and test that a channel is
1956// opened even if the remote end answer with a zero SSRC.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001957TEST_F(PeerConnectionInterfaceTest, TestSendOnlyDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001958 FakeConstraints constraints;
1959 constraints.SetAllowRtpDataChannels();
1960 CreatePeerConnection(&constraints);
zhihuang9763d562016-08-05 11:14:50 -07001961 rtc::scoped_refptr<DataChannelInterface> data1 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001962 pc_->CreateDataChannel("test1", NULL);
kwibergd1fe2812016-04-27 06:47:29 -07001963 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001964 new MockDataChannelObserver(data1));
1965
1966 CreateOfferReceiveAnswerWithoutSsrc();
1967
1968 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1969
1970 data1->Close();
1971 EXPECT_EQ(DataChannelInterface::kClosing, data1->state());
1972 CreateOfferReceiveAnswerWithoutSsrc();
1973 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
1974 EXPECT_FALSE(observer1->IsOpen());
1975}
1976
1977// This test that if a data channel is added in an answer a receive only channel
1978// channel is created.
1979TEST_F(PeerConnectionInterfaceTest, TestReceiveOnlyDataChannel) {
1980 FakeConstraints constraints;
1981 constraints.SetAllowRtpDataChannels();
1982 CreatePeerConnection(&constraints);
1983
1984 std::string offer_label = "offer_channel";
zhihuang9763d562016-08-05 11:14:50 -07001985 rtc::scoped_refptr<DataChannelInterface> offer_channel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001986 pc_->CreateDataChannel(offer_label, NULL);
1987
1988 CreateOfferAsLocalDescription();
1989
1990 // Replace the data channel label in the offer and apply it as an answer.
1991 std::string receive_label = "answer_channel";
1992 std::string sdp;
1993 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001994 rtc::replace_substrs(offer_label.c_str(), offer_label.length(),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001995 receive_label.c_str(), receive_label.length(),
1996 &sdp);
1997 CreateAnswerAsRemoteDescription(sdp);
1998
1999 // Verify that a new incoming data channel has been created and that
2000 // it is open but can't we written to.
2001 ASSERT_TRUE(observer_.last_datachannel_ != NULL);
2002 DataChannelInterface* received_channel = observer_.last_datachannel_;
2003 EXPECT_EQ(DataChannelInterface::kConnecting, received_channel->state());
2004 EXPECT_EQ(receive_label, received_channel->label());
2005 EXPECT_FALSE(received_channel->Send(DataBuffer("something")));
2006
2007 // Verify that the channel we initially offered has been rejected.
2008 EXPECT_EQ(DataChannelInterface::kClosed, offer_channel->state());
2009
2010 // Do another offer / answer exchange and verify that the data channel is
2011 // opened.
2012 CreateOfferReceiveAnswer();
2013 EXPECT_EQ_WAIT(DataChannelInterface::kOpen, received_channel->state(),
2014 kTimeout);
2015}
2016
2017// This test that no data channel is returned if a reliable channel is
2018// requested.
2019// TODO(perkj): Remove this test once reliable channels are implemented.
2020TEST_F(PeerConnectionInterfaceTest, CreateReliableRtpDataChannelShouldFail) {
2021 FakeConstraints constraints;
2022 constraints.SetAllowRtpDataChannels();
2023 CreatePeerConnection(&constraints);
2024
2025 std::string label = "test";
2026 webrtc::DataChannelInit config;
2027 config.reliable = true;
zhihuang9763d562016-08-05 11:14:50 -07002028 rtc::scoped_refptr<DataChannelInterface> channel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002029 pc_->CreateDataChannel(label, &config);
2030 EXPECT_TRUE(channel == NULL);
2031}
2032
deadbeefab9b2d12015-10-14 11:33:11 -07002033// Verifies that duplicated label is not allowed for RTP data channel.
2034TEST_F(PeerConnectionInterfaceTest, RtpDuplicatedLabelNotAllowed) {
2035 FakeConstraints constraints;
2036 constraints.SetAllowRtpDataChannels();
2037 CreatePeerConnection(&constraints);
2038
2039 std::string label = "test";
zhihuang9763d562016-08-05 11:14:50 -07002040 rtc::scoped_refptr<DataChannelInterface> channel =
deadbeefab9b2d12015-10-14 11:33:11 -07002041 pc_->CreateDataChannel(label, nullptr);
2042 EXPECT_NE(channel, nullptr);
2043
zhihuang9763d562016-08-05 11:14:50 -07002044 rtc::scoped_refptr<DataChannelInterface> dup_channel =
deadbeefab9b2d12015-10-14 11:33:11 -07002045 pc_->CreateDataChannel(label, nullptr);
2046 EXPECT_EQ(dup_channel, nullptr);
2047}
2048
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002049// This tests that a SCTP data channel is returned using different
2050// DataChannelInit configurations.
2051TEST_F(PeerConnectionInterfaceTest, CreateSctpDataChannel) {
2052 FakeConstraints constraints;
2053 constraints.SetAllowDtlsSctpDataChannels();
2054 CreatePeerConnection(&constraints);
2055
2056 webrtc::DataChannelInit config;
2057
zhihuang9763d562016-08-05 11:14:50 -07002058 rtc::scoped_refptr<DataChannelInterface> channel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002059 pc_->CreateDataChannel("1", &config);
2060 EXPECT_TRUE(channel != NULL);
2061 EXPECT_TRUE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00002062 EXPECT_TRUE(observer_.renegotiation_needed_);
2063 observer_.renegotiation_needed_ = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002064
2065 config.ordered = false;
2066 channel = pc_->CreateDataChannel("2", &config);
2067 EXPECT_TRUE(channel != NULL);
2068 EXPECT_TRUE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00002069 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002070
2071 config.ordered = true;
2072 config.maxRetransmits = 0;
2073 channel = pc_->CreateDataChannel("3", &config);
2074 EXPECT_TRUE(channel != NULL);
2075 EXPECT_FALSE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00002076 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002077
2078 config.maxRetransmits = -1;
2079 config.maxRetransmitTime = 0;
2080 channel = pc_->CreateDataChannel("4", &config);
2081 EXPECT_TRUE(channel != NULL);
2082 EXPECT_FALSE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00002083 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002084}
2085
2086// This tests that no data channel is returned if both maxRetransmits and
2087// maxRetransmitTime are set for SCTP data channels.
2088TEST_F(PeerConnectionInterfaceTest,
2089 CreateSctpDataChannelShouldFailForInvalidConfig) {
2090 FakeConstraints constraints;
2091 constraints.SetAllowDtlsSctpDataChannels();
2092 CreatePeerConnection(&constraints);
2093
2094 std::string label = "test";
2095 webrtc::DataChannelInit config;
2096 config.maxRetransmits = 0;
2097 config.maxRetransmitTime = 0;
2098
zhihuang9763d562016-08-05 11:14:50 -07002099 rtc::scoped_refptr<DataChannelInterface> channel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002100 pc_->CreateDataChannel(label, &config);
2101 EXPECT_TRUE(channel == NULL);
2102}
2103
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002104// The test verifies that creating a SCTP data channel with an id already in use
2105// or out of range should fail.
2106TEST_F(PeerConnectionInterfaceTest,
2107 CreateSctpDataChannelWithInvalidIdShouldFail) {
2108 FakeConstraints constraints;
2109 constraints.SetAllowDtlsSctpDataChannels();
2110 CreatePeerConnection(&constraints);
2111
2112 webrtc::DataChannelInit config;
zhihuang9763d562016-08-05 11:14:50 -07002113 rtc::scoped_refptr<DataChannelInterface> channel;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002114
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00002115 config.id = 1;
2116 channel = pc_->CreateDataChannel("1", &config);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002117 EXPECT_TRUE(channel != NULL);
2118 EXPECT_EQ(1, channel->id());
2119
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002120 channel = pc_->CreateDataChannel("x", &config);
2121 EXPECT_TRUE(channel == NULL);
2122
2123 config.id = cricket::kMaxSctpSid;
2124 channel = pc_->CreateDataChannel("max", &config);
2125 EXPECT_TRUE(channel != NULL);
2126 EXPECT_EQ(config.id, channel->id());
2127
2128 config.id = cricket::kMaxSctpSid + 1;
2129 channel = pc_->CreateDataChannel("x", &config);
2130 EXPECT_TRUE(channel == NULL);
2131}
2132
deadbeefab9b2d12015-10-14 11:33:11 -07002133// Verifies that duplicated label is allowed for SCTP data channel.
2134TEST_F(PeerConnectionInterfaceTest, SctpDuplicatedLabelAllowed) {
2135 FakeConstraints constraints;
2136 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2137 true);
2138 CreatePeerConnection(&constraints);
2139
2140 std::string label = "test";
zhihuang9763d562016-08-05 11:14:50 -07002141 rtc::scoped_refptr<DataChannelInterface> channel =
deadbeefab9b2d12015-10-14 11:33:11 -07002142 pc_->CreateDataChannel(label, nullptr);
2143 EXPECT_NE(channel, nullptr);
2144
zhihuang9763d562016-08-05 11:14:50 -07002145 rtc::scoped_refptr<DataChannelInterface> dup_channel =
deadbeefab9b2d12015-10-14 11:33:11 -07002146 pc_->CreateDataChannel(label, nullptr);
2147 EXPECT_NE(dup_channel, nullptr);
2148}
2149
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00002150// This test verifies that OnRenegotiationNeeded is fired for every new RTP
2151// DataChannel.
2152TEST_F(PeerConnectionInterfaceTest, RenegotiationNeededForNewRtpDataChannel) {
2153 FakeConstraints constraints;
2154 constraints.SetAllowRtpDataChannels();
2155 CreatePeerConnection(&constraints);
2156
zhihuang9763d562016-08-05 11:14:50 -07002157 rtc::scoped_refptr<DataChannelInterface> dc1 =
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00002158 pc_->CreateDataChannel("test1", NULL);
2159 EXPECT_TRUE(observer_.renegotiation_needed_);
2160 observer_.renegotiation_needed_ = false;
2161
zhihuang9763d562016-08-05 11:14:50 -07002162 rtc::scoped_refptr<DataChannelInterface> dc2 =
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00002163 pc_->CreateDataChannel("test2", NULL);
2164 EXPECT_TRUE(observer_.renegotiation_needed_);
2165}
2166
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002167// This test that a data channel closes when a PeerConnection is deleted/closed.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002168TEST_F(PeerConnectionInterfaceTest, DataChannelCloseWhenPeerConnectionClose) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002169 FakeConstraints constraints;
2170 constraints.SetAllowRtpDataChannels();
2171 CreatePeerConnection(&constraints);
2172
zhihuang9763d562016-08-05 11:14:50 -07002173 rtc::scoped_refptr<DataChannelInterface> data1 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002174 pc_->CreateDataChannel("test1", NULL);
zhihuang9763d562016-08-05 11:14:50 -07002175 rtc::scoped_refptr<DataChannelInterface> data2 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002176 pc_->CreateDataChannel("test2", NULL);
2177 ASSERT_TRUE(data1 != NULL);
kwibergd1fe2812016-04-27 06:47:29 -07002178 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002179 new MockDataChannelObserver(data1));
kwibergd1fe2812016-04-27 06:47:29 -07002180 std::unique_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002181 new MockDataChannelObserver(data2));
2182
2183 CreateOfferReceiveAnswer();
2184 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
2185 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
2186
2187 ReleasePeerConnection();
2188 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
2189 EXPECT_EQ(DataChannelInterface::kClosed, data2->state());
2190}
2191
2192// This test that data channels can be rejected in an answer.
2193TEST_F(PeerConnectionInterfaceTest, TestRejectDataChannelInAnswer) {
2194 FakeConstraints constraints;
2195 constraints.SetAllowRtpDataChannels();
2196 CreatePeerConnection(&constraints);
2197
zhihuang9763d562016-08-05 11:14:50 -07002198 rtc::scoped_refptr<DataChannelInterface> offer_channel(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002199 pc_->CreateDataChannel("offer_channel", NULL));
2200
2201 CreateOfferAsLocalDescription();
2202
2203 // Create an answer where the m-line for data channels are rejected.
2204 std::string sdp;
2205 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
2206 webrtc::JsepSessionDescription* answer = new webrtc::JsepSessionDescription(
2207 SessionDescriptionInterface::kAnswer);
2208 EXPECT_TRUE(answer->Initialize(sdp, NULL));
2209 cricket::ContentInfo* data_info =
2210 answer->description()->GetContentByName("data");
2211 data_info->rejected = true;
2212
2213 DoSetRemoteDescription(answer);
2214 EXPECT_EQ(DataChannelInterface::kClosed, offer_channel->state());
2215}
2216
2217// Test that we can create a session description from an SDP string from
2218// FireFox, use it as a remote session description, generate an answer and use
2219// the answer as a local description.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002220TEST_F(PeerConnectionInterfaceTest, ReceiveFireFoxOffer) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002221 FakeConstraints constraints;
2222 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2223 true);
2224 CreatePeerConnection(&constraints);
2225 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
2226 SessionDescriptionInterface* desc =
2227 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
jbauchfabe2c92015-07-16 13:43:14 -07002228 webrtc::kFireFoxSdpOffer, nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002229 EXPECT_TRUE(DoSetSessionDescription(desc, false));
2230 CreateAnswerAsLocalDescription();
2231 ASSERT_TRUE(pc_->local_description() != NULL);
2232 ASSERT_TRUE(pc_->remote_description() != NULL);
2233
2234 const cricket::ContentInfo* content =
2235 cricket::GetFirstAudioContent(pc_->local_description()->description());
2236 ASSERT_TRUE(content != NULL);
2237 EXPECT_FALSE(content->rejected);
2238
2239 content =
2240 cricket::GetFirstVideoContent(pc_->local_description()->description());
2241 ASSERT_TRUE(content != NULL);
2242 EXPECT_FALSE(content->rejected);
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00002243#ifdef HAVE_SCTP
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002244 content =
2245 cricket::GetFirstDataContent(pc_->local_description()->description());
2246 ASSERT_TRUE(content != NULL);
2247 EXPECT_TRUE(content->rejected);
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00002248#endif
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002249}
2250
deadbeef8662f942017-01-20 21:20:51 -08002251// Test that an offer can be received which offers DTLS with SDES fallback.
2252// Regression test for issue:
2253// https://bugs.chromium.org/p/webrtc/issues/detail?id=6972
2254TEST_F(PeerConnectionInterfaceTest, ReceiveDtlsSdesFallbackOffer) {
2255 FakeConstraints constraints;
2256 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2257 true);
2258 CreatePeerConnection(&constraints);
2259 // Wait for fake certificate to be generated. Previously, this is what caused
2260 // the "a=crypto" lines to be rejected.
2261 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
2262 ASSERT_NE(nullptr, fake_certificate_generator_);
2263 EXPECT_EQ_WAIT(1, fake_certificate_generator_->generated_certificates(),
2264 kTimeout);
2265 SessionDescriptionInterface* desc = webrtc::CreateSessionDescription(
2266 SessionDescriptionInterface::kOffer, kDtlsSdesFallbackSdp, nullptr);
2267 EXPECT_TRUE(DoSetSessionDescription(desc, false));
2268 CreateAnswerAsLocalDescription();
2269}
2270
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002271// Test that we can create an audio only offer and receive an answer with a
2272// limited set of audio codecs and receive an updated offer with more audio
2273// codecs, where the added codecs are not supported.
2274TEST_F(PeerConnectionInterfaceTest, ReceiveUpdatedAudioOfferWithBadCodecs) {
deadbeef293e9262017-01-11 12:28:30 -08002275 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002276 AddVoiceStream("audio_label");
2277 CreateOfferAsLocalDescription();
2278
2279 SessionDescriptionInterface* answer =
2280 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
jbauchfabe2c92015-07-16 13:43:14 -07002281 webrtc::kAudioSdp, nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002282 EXPECT_TRUE(DoSetSessionDescription(answer, false));
2283
2284 SessionDescriptionInterface* updated_offer =
2285 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
jbauchfabe2c92015-07-16 13:43:14 -07002286 webrtc::kAudioSdpWithUnsupportedCodecs,
2287 nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002288 EXPECT_TRUE(DoSetSessionDescription(updated_offer, false));
2289 CreateAnswerAsLocalDescription();
2290}
2291
deadbeefc80741f2015-10-22 13:14:45 -07002292// Test that if we're receiving (but not sending) a track, subsequent offers
2293// will have m-lines with a=recvonly.
2294TEST_F(PeerConnectionInterfaceTest, CreateSubsequentRecvOnlyOffer) {
2295 FakeConstraints constraints;
2296 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2297 true);
2298 CreatePeerConnection(&constraints);
2299 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2300 CreateAnswerAsLocalDescription();
2301
2302 // At this point we should be receiving stream 1, but not sending anything.
2303 // A new offer should be recvonly.
kwibergd1fe2812016-04-27 06:47:29 -07002304 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefc80741f2015-10-22 13:14:45 -07002305 DoCreateOffer(&offer, nullptr);
2306
2307 const cricket::ContentInfo* video_content =
2308 cricket::GetFirstVideoContent(offer->description());
2309 const cricket::VideoContentDescription* video_desc =
2310 static_cast<const cricket::VideoContentDescription*>(
2311 video_content->description);
2312 ASSERT_EQ(cricket::MD_RECVONLY, video_desc->direction());
2313
2314 const cricket::ContentInfo* audio_content =
2315 cricket::GetFirstAudioContent(offer->description());
2316 const cricket::AudioContentDescription* audio_desc =
2317 static_cast<const cricket::AudioContentDescription*>(
2318 audio_content->description);
2319 ASSERT_EQ(cricket::MD_RECVONLY, audio_desc->direction());
2320}
2321
2322// Test that if we're receiving (but not sending) a track, and the
2323// offerToReceiveVideo/offerToReceiveAudio constraints are explicitly set to
2324// false, the generated m-lines will be a=inactive.
2325TEST_F(PeerConnectionInterfaceTest, CreateSubsequentInactiveOffer) {
2326 FakeConstraints constraints;
2327 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2328 true);
2329 CreatePeerConnection(&constraints);
2330 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2331 CreateAnswerAsLocalDescription();
2332
2333 // At this point we should be receiving stream 1, but not sending anything.
2334 // A new offer would be recvonly, but we'll set the "no receive" constraints
2335 // to make it inactive.
kwibergd1fe2812016-04-27 06:47:29 -07002336 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefc80741f2015-10-22 13:14:45 -07002337 FakeConstraints offer_constraints;
2338 offer_constraints.AddMandatory(
2339 webrtc::MediaConstraintsInterface::kOfferToReceiveVideo, false);
2340 offer_constraints.AddMandatory(
2341 webrtc::MediaConstraintsInterface::kOfferToReceiveAudio, false);
2342 DoCreateOffer(&offer, &offer_constraints);
2343
2344 const cricket::ContentInfo* video_content =
2345 cricket::GetFirstVideoContent(offer->description());
2346 const cricket::VideoContentDescription* video_desc =
2347 static_cast<const cricket::VideoContentDescription*>(
2348 video_content->description);
2349 ASSERT_EQ(cricket::MD_INACTIVE, video_desc->direction());
2350
2351 const cricket::ContentInfo* audio_content =
2352 cricket::GetFirstAudioContent(offer->description());
2353 const cricket::AudioContentDescription* audio_desc =
2354 static_cast<const cricket::AudioContentDescription*>(
2355 audio_content->description);
2356 ASSERT_EQ(cricket::MD_INACTIVE, audio_desc->direction());
2357}
2358
deadbeef653b8e02015-11-11 12:55:10 -08002359// Test that we can use SetConfiguration to change the ICE servers of the
2360// PortAllocator.
2361TEST_F(PeerConnectionInterfaceTest, SetConfigurationChangesIceServers) {
2362 CreatePeerConnection();
2363
2364 PeerConnectionInterface::RTCConfiguration config;
2365 PeerConnectionInterface::IceServer server;
2366 server.uri = "stun:test_hostname";
2367 config.servers.push_back(server);
2368 EXPECT_TRUE(pc_->SetConfiguration(config));
2369
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002370 EXPECT_EQ(1u, port_allocator_->stun_servers().size());
2371 EXPECT_EQ("test_hostname",
2372 port_allocator_->stun_servers().begin()->hostname());
deadbeef653b8e02015-11-11 12:55:10 -08002373}
2374
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002375TEST_F(PeerConnectionInterfaceTest, SetConfigurationChangesCandidateFilter) {
2376 CreatePeerConnection();
2377 PeerConnectionInterface::RTCConfiguration config;
2378 config.type = PeerConnectionInterface::kRelay;
2379 EXPECT_TRUE(pc_->SetConfiguration(config));
2380 EXPECT_EQ(cricket::CF_RELAY, port_allocator_->candidate_filter());
2381}
2382
deadbeef293e9262017-01-11 12:28:30 -08002383TEST_F(PeerConnectionInterfaceTest, SetConfigurationChangesPruneTurnPortsFlag) {
2384 PeerConnectionInterface::RTCConfiguration config;
2385 config.prune_turn_ports = false;
2386 CreatePeerConnection(config, nullptr);
2387 EXPECT_FALSE(port_allocator_->prune_turn_ports());
2388
2389 config.prune_turn_ports = true;
2390 EXPECT_TRUE(pc_->SetConfiguration(config));
2391 EXPECT_TRUE(port_allocator_->prune_turn_ports());
2392}
2393
skvladd1f5fda2017-02-03 16:54:05 -08002394// Test that the ice check interval can be changed. This does not verify that
2395// the setting makes it all the way to P2PTransportChannel, as that would
2396// require a very complex set of mocks.
2397TEST_F(PeerConnectionInterfaceTest, SetConfigurationChangesIceCheckInterval) {
2398 PeerConnectionInterface::RTCConfiguration config;
2399 config.ice_check_min_interval = rtc::Optional<int>();
2400 CreatePeerConnection(config, nullptr);
2401 config.ice_check_min_interval = rtc::Optional<int>(100);
2402 EXPECT_TRUE(pc_->SetConfiguration(config));
2403 PeerConnectionInterface::RTCConfiguration new_config =
2404 pc_->GetConfiguration();
2405 EXPECT_EQ(new_config.ice_check_min_interval, rtc::Optional<int>(100));
2406}
2407
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002408// Test that when SetConfiguration changes both the pool size and other
2409// attributes, the pooled session is created with the updated attributes.
2410TEST_F(PeerConnectionInterfaceTest,
2411 SetConfigurationCreatesPooledSessionCorrectly) {
2412 CreatePeerConnection();
2413 PeerConnectionInterface::RTCConfiguration config;
2414 config.ice_candidate_pool_size = 1;
2415 PeerConnectionInterface::IceServer server;
2416 server.uri = kStunAddressOnly;
2417 config.servers.push_back(server);
2418 config.type = PeerConnectionInterface::kRelay;
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07002419 EXPECT_TRUE(pc_->SetConfiguration(config));
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002420
2421 const cricket::FakePortAllocatorSession* session =
2422 static_cast<const cricket::FakePortAllocatorSession*>(
2423 port_allocator_->GetPooledSession());
2424 ASSERT_NE(nullptr, session);
2425 EXPECT_EQ(1UL, session->stun_servers().size());
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002426}
2427
deadbeef293e9262017-01-11 12:28:30 -08002428// Test that after SetLocalDescription, changing the pool size is not allowed,
2429// and an invalid modification error is returned.
deadbeef6de92f92016-12-12 18:49:32 -08002430TEST_F(PeerConnectionInterfaceTest,
2431 CantChangePoolSizeAfterSetLocalDescription) {
2432 CreatePeerConnection();
2433 // Start by setting a size of 1.
2434 PeerConnectionInterface::RTCConfiguration config;
2435 config.ice_candidate_pool_size = 1;
2436 EXPECT_TRUE(pc_->SetConfiguration(config));
2437
2438 // Set remote offer; can still change pool size at this point.
2439 CreateOfferAsRemoteDescription();
2440 config.ice_candidate_pool_size = 2;
2441 EXPECT_TRUE(pc_->SetConfiguration(config));
2442
2443 // Set local answer; now it's too late.
2444 CreateAnswerAsLocalDescription();
2445 config.ice_candidate_pool_size = 3;
deadbeef293e9262017-01-11 12:28:30 -08002446 RTCError error;
2447 EXPECT_FALSE(pc_->SetConfiguration(config, &error));
2448 EXPECT_EQ(RTCErrorType::INVALID_MODIFICATION, error.type());
2449}
2450
deadbeef42a42632017-03-10 15:18:00 -08002451// Test that after setting an answer, extra pooled sessions are discarded. The
2452// ICE candidate pool is only intended to be used for the first offer/answer.
2453TEST_F(PeerConnectionInterfaceTest,
2454 ExtraPooledSessionsDiscardedAfterApplyingAnswer) {
2455 CreatePeerConnection();
2456
2457 // Set a larger-than-necessary size.
2458 PeerConnectionInterface::RTCConfiguration config;
2459 config.ice_candidate_pool_size = 4;
2460 EXPECT_TRUE(pc_->SetConfiguration(config));
2461
2462 // Do offer/answer.
2463 CreateOfferAsRemoteDescription();
2464 CreateAnswerAsLocalDescription();
2465
2466 // Expect no pooled sessions to be left.
2467 const cricket::PortAllocatorSession* session =
2468 port_allocator_->GetPooledSession();
2469 EXPECT_EQ(nullptr, session);
2470}
2471
2472// After Close is called, pooled candidates should be discarded so as to not
2473// waste network resources.
2474TEST_F(PeerConnectionInterfaceTest, PooledSessionsDiscardedAfterClose) {
2475 CreatePeerConnection();
2476
2477 PeerConnectionInterface::RTCConfiguration config;
2478 config.ice_candidate_pool_size = 3;
2479 EXPECT_TRUE(pc_->SetConfiguration(config));
2480 pc_->Close();
2481
2482 // Expect no pooled sessions to be left.
2483 const cricket::PortAllocatorSession* session =
2484 port_allocator_->GetPooledSession();
2485 EXPECT_EQ(nullptr, session);
2486}
2487
deadbeef293e9262017-01-11 12:28:30 -08002488// Test that SetConfiguration returns an invalid modification error if
2489// modifying a field in the configuration that isn't allowed to be modified.
2490TEST_F(PeerConnectionInterfaceTest,
2491 SetConfigurationReturnsInvalidModificationError) {
2492 PeerConnectionInterface::RTCConfiguration config;
2493 config.bundle_policy = PeerConnectionInterface::kBundlePolicyBalanced;
2494 config.rtcp_mux_policy = PeerConnectionInterface::kRtcpMuxPolicyNegotiate;
2495 config.continual_gathering_policy = PeerConnectionInterface::GATHER_ONCE;
2496 CreatePeerConnection(config, nullptr);
2497
2498 PeerConnectionInterface::RTCConfiguration modified_config = config;
2499 modified_config.bundle_policy =
2500 PeerConnectionInterface::kBundlePolicyMaxBundle;
2501 RTCError error;
2502 EXPECT_FALSE(pc_->SetConfiguration(modified_config, &error));
2503 EXPECT_EQ(RTCErrorType::INVALID_MODIFICATION, error.type());
2504
2505 modified_config = config;
2506 modified_config.rtcp_mux_policy =
2507 PeerConnectionInterface::kRtcpMuxPolicyRequire;
2508 error.set_type(RTCErrorType::NONE);
2509 EXPECT_FALSE(pc_->SetConfiguration(modified_config, &error));
2510 EXPECT_EQ(RTCErrorType::INVALID_MODIFICATION, error.type());
2511
2512 modified_config = config;
2513 modified_config.continual_gathering_policy =
2514 PeerConnectionInterface::GATHER_CONTINUALLY;
2515 error.set_type(RTCErrorType::NONE);
2516 EXPECT_FALSE(pc_->SetConfiguration(modified_config, &error));
2517 EXPECT_EQ(RTCErrorType::INVALID_MODIFICATION, error.type());
2518}
2519
2520// Test that SetConfiguration returns a range error if the candidate pool size
2521// is negative or larger than allowed by the spec.
2522TEST_F(PeerConnectionInterfaceTest,
2523 SetConfigurationReturnsRangeErrorForBadCandidatePoolSize) {
2524 PeerConnectionInterface::RTCConfiguration config;
2525 CreatePeerConnection(config, nullptr);
2526
2527 config.ice_candidate_pool_size = -1;
2528 RTCError error;
2529 EXPECT_FALSE(pc_->SetConfiguration(config, &error));
2530 EXPECT_EQ(RTCErrorType::INVALID_RANGE, error.type());
2531
2532 config.ice_candidate_pool_size = INT_MAX;
2533 error.set_type(RTCErrorType::NONE);
2534 EXPECT_FALSE(pc_->SetConfiguration(config, &error));
2535 EXPECT_EQ(RTCErrorType::INVALID_RANGE, error.type());
2536}
2537
2538// Test that SetConfiguration returns a syntax error if parsing an ICE server
2539// URL failed.
2540TEST_F(PeerConnectionInterfaceTest,
2541 SetConfigurationReturnsSyntaxErrorFromBadIceUrls) {
2542 PeerConnectionInterface::RTCConfiguration config;
2543 CreatePeerConnection(config, nullptr);
2544
2545 PeerConnectionInterface::IceServer bad_server;
2546 bad_server.uri = "stunn:www.example.com";
2547 config.servers.push_back(bad_server);
2548 RTCError error;
2549 EXPECT_FALSE(pc_->SetConfiguration(config, &error));
2550 EXPECT_EQ(RTCErrorType::SYNTAX_ERROR, error.type());
2551}
2552
2553// Test that SetConfiguration returns an invalid parameter error if a TURN
2554// IceServer is missing a username or password.
2555TEST_F(PeerConnectionInterfaceTest,
2556 SetConfigurationReturnsInvalidParameterIfCredentialsMissing) {
2557 PeerConnectionInterface::RTCConfiguration config;
2558 CreatePeerConnection(config, nullptr);
2559
2560 PeerConnectionInterface::IceServer bad_server;
2561 bad_server.uri = "turn:www.example.com";
2562 // Missing password.
2563 bad_server.username = "foo";
2564 config.servers.push_back(bad_server);
2565 RTCError error;
2566 EXPECT_FALSE(pc_->SetConfiguration(config, &error));
2567 EXPECT_EQ(RTCErrorType::INVALID_PARAMETER, error.type());
deadbeef6de92f92016-12-12 18:49:32 -08002568}
2569
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002570// Test that PeerConnection::Close changes the states to closed and all remote
2571// tracks change state to ended.
2572TEST_F(PeerConnectionInterfaceTest, CloseAndTestStreamsAndStates) {
2573 // Initialize a PeerConnection and negotiate local and remote session
2574 // description.
2575 InitiateCall();
2576 ASSERT_EQ(1u, pc_->local_streams()->count());
2577 ASSERT_EQ(1u, pc_->remote_streams()->count());
2578
2579 pc_->Close();
2580
2581 EXPECT_EQ(PeerConnectionInterface::kClosed, pc_->signaling_state());
2582 EXPECT_EQ(PeerConnectionInterface::kIceConnectionClosed,
2583 pc_->ice_connection_state());
2584 EXPECT_EQ(PeerConnectionInterface::kIceGatheringComplete,
2585 pc_->ice_gathering_state());
2586
2587 EXPECT_EQ(1u, pc_->local_streams()->count());
2588 EXPECT_EQ(1u, pc_->remote_streams()->count());
2589
zhihuang9763d562016-08-05 11:14:50 -07002590 rtc::scoped_refptr<MediaStreamInterface> remote_stream =
2591 pc_->remote_streams()->at(0);
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002592 // Track state may be updated asynchronously.
perkjd61bf802016-03-24 03:16:19 -07002593 EXPECT_EQ_WAIT(MediaStreamTrackInterface::kEnded,
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002594 remote_stream->GetAudioTracks()[0]->state(), kTimeout);
2595 EXPECT_EQ_WAIT(MediaStreamTrackInterface::kEnded,
2596 remote_stream->GetVideoTracks()[0]->state(), kTimeout);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002597}
2598
2599// Test that PeerConnection methods fails gracefully after
2600// PeerConnection::Close has been called.
2601TEST_F(PeerConnectionInterfaceTest, CloseAndTestMethods) {
deadbeef293e9262017-01-11 12:28:30 -08002602 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002603 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
2604 CreateOfferAsRemoteDescription();
2605 CreateAnswerAsLocalDescription();
2606
2607 ASSERT_EQ(1u, pc_->local_streams()->count());
zhihuang9763d562016-08-05 11:14:50 -07002608 rtc::scoped_refptr<MediaStreamInterface> local_stream =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002609 pc_->local_streams()->at(0);
2610
2611 pc_->Close();
2612
2613 pc_->RemoveStream(local_stream);
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +00002614 EXPECT_FALSE(pc_->AddStream(local_stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002615
2616 ASSERT_FALSE(local_stream->GetAudioTracks().empty());
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002617 rtc::scoped_refptr<webrtc::DtmfSenderInterface> dtmf_sender(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002618 pc_->CreateDtmfSender(local_stream->GetAudioTracks()[0]));
wu@webrtc.org66037362013-08-13 00:09:35 +00002619 EXPECT_TRUE(NULL == dtmf_sender); // local stream has been removed.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002620
2621 EXPECT_TRUE(pc_->CreateDataChannel("test", NULL) == NULL);
2622
2623 EXPECT_TRUE(pc_->local_description() != NULL);
2624 EXPECT_TRUE(pc_->remote_description() != NULL);
2625
kwibergd1fe2812016-04-27 06:47:29 -07002626 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07002627 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
kwibergd1fe2812016-04-27 06:47:29 -07002628 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -07002629 EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002630
2631 std::string sdp;
2632 ASSERT_TRUE(pc_->remote_description()->ToString(&sdp));
2633 SessionDescriptionInterface* remote_offer =
2634 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
2635 sdp, NULL);
2636 EXPECT_FALSE(DoSetRemoteDescription(remote_offer));
2637
2638 ASSERT_TRUE(pc_->local_description()->ToString(&sdp));
2639 SessionDescriptionInterface* local_offer =
2640 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
2641 sdp, NULL);
2642 EXPECT_FALSE(DoSetLocalDescription(local_offer));
2643}
2644
2645// Test that GetStats can still be called after PeerConnection::Close.
2646TEST_F(PeerConnectionInterfaceTest, CloseAndGetStats) {
2647 InitiateCall();
2648 pc_->Close();
2649 DoGetStats(NULL);
2650}
deadbeefab9b2d12015-10-14 11:33:11 -07002651
2652// NOTE: The series of tests below come from what used to be
2653// mediastreamsignaling_unittest.cc, and are mostly aimed at testing that
2654// setting a remote or local description has the expected effects.
2655
2656// This test verifies that the remote MediaStreams corresponding to a received
2657// SDP string is created. In this test the two separate MediaStreams are
2658// signaled.
2659TEST_F(PeerConnectionInterfaceTest, UpdateRemoteStreams) {
2660 FakeConstraints constraints;
2661 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2662 true);
2663 CreatePeerConnection(&constraints);
2664 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2665
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002666 rtc::scoped_refptr<StreamCollection> reference(CreateStreamCollection(1, 1));
deadbeefab9b2d12015-10-14 11:33:11 -07002667 EXPECT_TRUE(
2668 CompareStreamCollections(observer_.remote_streams(), reference.get()));
2669 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2670 EXPECT_TRUE(remote_stream->GetVideoTracks()[0]->GetSource() != nullptr);
2671
2672 // Create a session description based on another SDP with another
2673 // MediaStream.
2674 CreateAndSetRemoteOffer(kSdpStringWithStream1And2);
2675
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002676 rtc::scoped_refptr<StreamCollection> reference2(CreateStreamCollection(2, 1));
deadbeefab9b2d12015-10-14 11:33:11 -07002677 EXPECT_TRUE(
2678 CompareStreamCollections(observer_.remote_streams(), reference2.get()));
2679}
2680
2681// This test verifies that when remote tracks are added/removed from SDP, the
2682// created remote streams are updated appropriately.
2683TEST_F(PeerConnectionInterfaceTest,
2684 AddRemoveTrackFromExistingRemoteMediaStream) {
2685 FakeConstraints constraints;
2686 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2687 true);
2688 CreatePeerConnection(&constraints);
kwibergd1fe2812016-04-27 06:47:29 -07002689 std::unique_ptr<SessionDescriptionInterface> desc_ms1 =
kwiberg2bbff992016-03-16 11:03:04 -07002690 CreateSessionDescriptionAndReference(1, 1);
deadbeefab9b2d12015-10-14 11:33:11 -07002691 EXPECT_TRUE(DoSetRemoteDescription(desc_ms1.release()));
2692 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
2693 reference_collection_));
2694
2695 // Add extra audio and video tracks to the same MediaStream.
kwibergd1fe2812016-04-27 06:47:29 -07002696 std::unique_ptr<SessionDescriptionInterface> desc_ms1_two_tracks =
kwiberg2bbff992016-03-16 11:03:04 -07002697 CreateSessionDescriptionAndReference(2, 2);
deadbeefab9b2d12015-10-14 11:33:11 -07002698 EXPECT_TRUE(DoSetRemoteDescription(desc_ms1_two_tracks.release()));
2699 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
2700 reference_collection_));
zhihuang9763d562016-08-05 11:14:50 -07002701 rtc::scoped_refptr<AudioTrackInterface> audio_track2 =
perkjd61bf802016-03-24 03:16:19 -07002702 observer_.remote_streams()->at(0)->GetAudioTracks()[1];
2703 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, audio_track2->state());
zhihuang9763d562016-08-05 11:14:50 -07002704 rtc::scoped_refptr<VideoTrackInterface> video_track2 =
perkjd61bf802016-03-24 03:16:19 -07002705 observer_.remote_streams()->at(0)->GetVideoTracks()[1];
2706 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, video_track2->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002707
2708 // Remove the extra audio and video tracks.
kwibergd1fe2812016-04-27 06:47:29 -07002709 std::unique_ptr<SessionDescriptionInterface> desc_ms2 =
kwiberg2bbff992016-03-16 11:03:04 -07002710 CreateSessionDescriptionAndReference(1, 1);
perkjd61bf802016-03-24 03:16:19 -07002711 MockTrackObserver audio_track_observer(audio_track2);
2712 MockTrackObserver video_track_observer(video_track2);
2713
2714 EXPECT_CALL(audio_track_observer, OnChanged()).Times(Exactly(1));
2715 EXPECT_CALL(video_track_observer, OnChanged()).Times(Exactly(1));
deadbeefab9b2d12015-10-14 11:33:11 -07002716 EXPECT_TRUE(DoSetRemoteDescription(desc_ms2.release()));
2717 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
2718 reference_collection_));
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002719 // Track state may be updated asynchronously.
perkjd61bf802016-03-24 03:16:19 -07002720 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002721 audio_track2->state(), kTimeout);
2722 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
2723 video_track2->state(), kTimeout);
deadbeefab9b2d12015-10-14 11:33:11 -07002724}
2725
2726// This tests that remote tracks are ended if a local session description is set
2727// that rejects the media content type.
2728TEST_F(PeerConnectionInterfaceTest, RejectMediaContent) {
2729 FakeConstraints constraints;
2730 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2731 true);
2732 CreatePeerConnection(&constraints);
2733 // First create and set a remote offer, then reject its video content in our
2734 // answer.
2735 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2736 ASSERT_EQ(1u, observer_.remote_streams()->count());
2737 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2738 ASSERT_EQ(1u, remote_stream->GetVideoTracks().size());
2739 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2740
2741 rtc::scoped_refptr<webrtc::VideoTrackInterface> remote_video =
2742 remote_stream->GetVideoTracks()[0];
2743 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, remote_video->state());
2744 rtc::scoped_refptr<webrtc::AudioTrackInterface> remote_audio =
2745 remote_stream->GetAudioTracks()[0];
2746 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, remote_audio->state());
2747
kwibergd1fe2812016-04-27 06:47:29 -07002748 std::unique_ptr<SessionDescriptionInterface> local_answer;
kwiberg2bbff992016-03-16 11:03:04 -07002749 EXPECT_TRUE(DoCreateAnswer(&local_answer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07002750 cricket::ContentInfo* video_info =
2751 local_answer->description()->GetContentByName("video");
2752 video_info->rejected = true;
2753 EXPECT_TRUE(DoSetLocalDescription(local_answer.release()));
2754 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kEnded, remote_video->state());
2755 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, remote_audio->state());
2756
2757 // Now create an offer where we reject both video and audio.
kwibergd1fe2812016-04-27 06:47:29 -07002758 std::unique_ptr<SessionDescriptionInterface> local_offer;
kwiberg2bbff992016-03-16 11:03:04 -07002759 EXPECT_TRUE(DoCreateOffer(&local_offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07002760 video_info = local_offer->description()->GetContentByName("video");
2761 ASSERT_TRUE(video_info != nullptr);
2762 video_info->rejected = true;
2763 cricket::ContentInfo* audio_info =
2764 local_offer->description()->GetContentByName("audio");
2765 ASSERT_TRUE(audio_info != nullptr);
2766 audio_info->rejected = true;
2767 EXPECT_TRUE(DoSetLocalDescription(local_offer.release()));
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002768 // Track state may be updated asynchronously.
perkjd61bf802016-03-24 03:16:19 -07002769 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002770 remote_audio->state(), kTimeout);
2771 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
2772 remote_video->state(), kTimeout);
deadbeefab9b2d12015-10-14 11:33:11 -07002773}
2774
2775// This tests that we won't crash if the remote track has been removed outside
2776// of PeerConnection and then PeerConnection tries to reject the track.
2777TEST_F(PeerConnectionInterfaceTest, RemoveTrackThenRejectMediaContent) {
2778 FakeConstraints constraints;
2779 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2780 true);
2781 CreatePeerConnection(&constraints);
2782 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2783 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2784 remote_stream->RemoveTrack(remote_stream->GetVideoTracks()[0]);
2785 remote_stream->RemoveTrack(remote_stream->GetAudioTracks()[0]);
2786
kwibergd1fe2812016-04-27 06:47:29 -07002787 std::unique_ptr<SessionDescriptionInterface> local_answer(
deadbeefab9b2d12015-10-14 11:33:11 -07002788 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
2789 kSdpStringWithStream1, nullptr));
2790 cricket::ContentInfo* video_info =
2791 local_answer->description()->GetContentByName("video");
2792 video_info->rejected = true;
2793 cricket::ContentInfo* audio_info =
2794 local_answer->description()->GetContentByName("audio");
2795 audio_info->rejected = true;
2796 EXPECT_TRUE(DoSetLocalDescription(local_answer.release()));
2797
2798 // No crash is a pass.
2799}
2800
deadbeef5e97fb52015-10-15 12:49:08 -07002801// This tests that if a recvonly remote description is set, no remote streams
2802// will be created, even if the description contains SSRCs/MSIDs.
2803// See: https://code.google.com/p/webrtc/issues/detail?id=5054
2804TEST_F(PeerConnectionInterfaceTest, RecvonlyDescriptionDoesntCreateStream) {
2805 FakeConstraints constraints;
2806 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2807 true);
2808 CreatePeerConnection(&constraints);
2809
2810 std::string recvonly_offer = kSdpStringWithStream1;
2811 rtc::replace_substrs(kSendrecv, strlen(kSendrecv), kRecvonly,
2812 strlen(kRecvonly), &recvonly_offer);
2813 CreateAndSetRemoteOffer(recvonly_offer);
2814
2815 EXPECT_EQ(0u, observer_.remote_streams()->count());
2816}
2817
deadbeefab9b2d12015-10-14 11:33:11 -07002818// This tests that a default MediaStream is created if a remote session
2819// description doesn't contain any streams and no MSID support.
2820// It also tests that the default stream is updated if a video m-line is added
2821// in a subsequent session description.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002822TEST_F(PeerConnectionInterfaceTest, SdpWithoutMsidCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002823 FakeConstraints constraints;
2824 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2825 true);
2826 CreatePeerConnection(&constraints);
2827 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2828
2829 ASSERT_EQ(1u, observer_.remote_streams()->count());
2830 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2831
2832 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2833 EXPECT_EQ(0u, remote_stream->GetVideoTracks().size());
2834 EXPECT_EQ("default", remote_stream->label());
2835
2836 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2837 ASSERT_EQ(1u, observer_.remote_streams()->count());
2838 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2839 EXPECT_EQ("defaulta0", remote_stream->GetAudioTracks()[0]->id());
deadbeef884f5852016-01-15 09:20:04 -08002840 EXPECT_EQ(MediaStreamTrackInterface::kLive,
2841 remote_stream->GetAudioTracks()[0]->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002842 ASSERT_EQ(1u, remote_stream->GetVideoTracks().size());
2843 EXPECT_EQ("defaultv0", remote_stream->GetVideoTracks()[0]->id());
deadbeef884f5852016-01-15 09:20:04 -08002844 EXPECT_EQ(MediaStreamTrackInterface::kLive,
2845 remote_stream->GetVideoTracks()[0]->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002846}
2847
2848// This tests that a default MediaStream is created if a remote session
2849// description doesn't contain any streams and media direction is send only.
2850TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002851 SendOnlySdpWithoutMsidCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002852 FakeConstraints constraints;
2853 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2854 true);
2855 CreatePeerConnection(&constraints);
2856 CreateAndSetRemoteOffer(kSdpStringSendOnlyWithoutStreams);
2857
2858 ASSERT_EQ(1u, observer_.remote_streams()->count());
2859 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2860
2861 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2862 EXPECT_EQ(1u, remote_stream->GetVideoTracks().size());
2863 EXPECT_EQ("default", remote_stream->label());
2864}
2865
2866// This tests that it won't crash when PeerConnection tries to remove
2867// a remote track that as already been removed from the MediaStream.
2868TEST_F(PeerConnectionInterfaceTest, RemoveAlreadyGoneRemoteStream) {
2869 FakeConstraints constraints;
2870 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2871 true);
2872 CreatePeerConnection(&constraints);
2873 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2874 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2875 remote_stream->RemoveTrack(remote_stream->GetAudioTracks()[0]);
2876 remote_stream->RemoveTrack(remote_stream->GetVideoTracks()[0]);
2877
2878 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2879
2880 // No crash is a pass.
2881}
2882
2883// This tests that a default MediaStream is created if the remote session
2884// description doesn't contain any streams and don't contain an indication if
2885// MSID is supported.
2886TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002887 SdpWithoutMsidAndStreamsCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002888 FakeConstraints constraints;
2889 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2890 true);
2891 CreatePeerConnection(&constraints);
2892 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2893
2894 ASSERT_EQ(1u, observer_.remote_streams()->count());
2895 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2896 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2897 EXPECT_EQ(1u, remote_stream->GetVideoTracks().size());
2898}
2899
2900// This tests that a default MediaStream is not created if the remote session
2901// description doesn't contain any streams but does support MSID.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002902TEST_F(PeerConnectionInterfaceTest, SdpWithMsidDontCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002903 FakeConstraints constraints;
2904 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2905 true);
2906 CreatePeerConnection(&constraints);
2907 CreateAndSetRemoteOffer(kSdpStringWithMsidWithoutStreams);
2908 EXPECT_EQ(0u, observer_.remote_streams()->count());
2909}
2910
deadbeefbda7e0b2015-12-08 17:13:40 -08002911// This tests that when setting a new description, the old default tracks are
2912// not destroyed and recreated.
2913// See: https://bugs.chromium.org/p/webrtc/issues/detail?id=5250
Stefan Holmer102362b2016-03-18 09:39:07 +01002914TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002915 DefaultTracksNotDestroyedAndRecreated) {
deadbeefbda7e0b2015-12-08 17:13:40 -08002916 FakeConstraints constraints;
2917 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2918 true);
2919 CreatePeerConnection(&constraints);
2920 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2921
2922 ASSERT_EQ(1u, observer_.remote_streams()->count());
2923 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2924 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2925
2926 // Set the track to "disabled", then set a new description and ensure the
2927 // track is still disabled, which ensures it hasn't been recreated.
2928 remote_stream->GetAudioTracks()[0]->set_enabled(false);
2929 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2930 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2931 EXPECT_FALSE(remote_stream->GetAudioTracks()[0]->enabled());
2932}
2933
deadbeefab9b2d12015-10-14 11:33:11 -07002934// This tests that a default MediaStream is not created if a remote session
2935// description is updated to not have any MediaStreams.
2936TEST_F(PeerConnectionInterfaceTest, VerifyDefaultStreamIsNotCreated) {
2937 FakeConstraints constraints;
2938 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2939 true);
2940 CreatePeerConnection(&constraints);
2941 CreateAndSetRemoteOffer(kSdpStringWithStream1);
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002942 rtc::scoped_refptr<StreamCollection> reference(CreateStreamCollection(1, 1));
deadbeefab9b2d12015-10-14 11:33:11 -07002943 EXPECT_TRUE(
2944 CompareStreamCollections(observer_.remote_streams(), reference.get()));
2945
2946 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2947 EXPECT_EQ(0u, observer_.remote_streams()->count());
2948}
2949
2950// This tests that an RtpSender is created when the local description is set
2951// after adding a local stream.
2952// TODO(deadbeef): This test and the one below it need to be updated when
2953// an RtpSender's lifetime isn't determined by when a local description is set.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002954TEST_F(PeerConnectionInterfaceTest, LocalDescriptionChanged) {
deadbeefab9b2d12015-10-14 11:33:11 -07002955 FakeConstraints constraints;
2956 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2957 true);
2958 CreatePeerConnection(&constraints);
deadbeefab9b2d12015-10-14 11:33:11 -07002959
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002960 // Create an offer with 1 stream with 2 tracks of each type.
2961 rtc::scoped_refptr<StreamCollection> stream_collection =
2962 CreateStreamCollection(1, 2);
2963 pc_->AddStream(stream_collection->at(0));
2964 std::unique_ptr<SessionDescriptionInterface> offer;
2965 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2966 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
deadbeefab9b2d12015-10-14 11:33:11 -07002967
deadbeefab9b2d12015-10-14 11:33:11 -07002968 auto senders = pc_->GetSenders();
2969 EXPECT_EQ(4u, senders.size());
2970 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2971 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2972 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[1]));
2973 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[1]));
2974
2975 // Remove an audio and video track.
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002976 pc_->RemoveStream(stream_collection->at(0));
2977 stream_collection = CreateStreamCollection(1, 1);
2978 pc_->AddStream(stream_collection->at(0));
2979 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2980 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
2981
deadbeefab9b2d12015-10-14 11:33:11 -07002982 senders = pc_->GetSenders();
2983 EXPECT_EQ(2u, senders.size());
2984 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2985 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2986 EXPECT_FALSE(ContainsSender(senders, kAudioTracks[1]));
2987 EXPECT_FALSE(ContainsSender(senders, kVideoTracks[1]));
2988}
2989
2990// This tests that an RtpSender is created when the local description is set
2991// before adding a local stream.
2992TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002993 AddLocalStreamAfterLocalDescriptionChanged) {
deadbeefab9b2d12015-10-14 11:33:11 -07002994 FakeConstraints constraints;
2995 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2996 true);
2997 CreatePeerConnection(&constraints);
deadbeefab9b2d12015-10-14 11:33:11 -07002998
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002999 rtc::scoped_refptr<StreamCollection> stream_collection =
3000 CreateStreamCollection(1, 2);
3001 // Add a stream to create the offer, but remove it afterwards.
3002 pc_->AddStream(stream_collection->at(0));
3003 std::unique_ptr<SessionDescriptionInterface> offer;
3004 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
3005 pc_->RemoveStream(stream_collection->at(0));
deadbeefab9b2d12015-10-14 11:33:11 -07003006
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003007 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
deadbeefab9b2d12015-10-14 11:33:11 -07003008 auto senders = pc_->GetSenders();
3009 EXPECT_EQ(0u, senders.size());
3010
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003011 pc_->AddStream(stream_collection->at(0));
deadbeefab9b2d12015-10-14 11:33:11 -07003012 senders = pc_->GetSenders();
3013 EXPECT_EQ(4u, senders.size());
3014 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
3015 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
3016 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[1]));
3017 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[1]));
3018}
3019
3020// This tests that the expected behavior occurs if the SSRC on a local track is
3021// changed when SetLocalDescription is called.
3022TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07003023 ChangeSsrcOnTrackInLocalSessionDescription) {
deadbeefab9b2d12015-10-14 11:33:11 -07003024 FakeConstraints constraints;
3025 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
3026 true);
3027 CreatePeerConnection(&constraints);
deadbeefab9b2d12015-10-14 11:33:11 -07003028
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003029 rtc::scoped_refptr<StreamCollection> stream_collection =
3030 CreateStreamCollection(2, 1);
3031 pc_->AddStream(stream_collection->at(0));
3032 std::unique_ptr<SessionDescriptionInterface> offer;
3033 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
3034 // Grab a copy of the offer before it gets passed into the PC.
3035 std::unique_ptr<JsepSessionDescription> modified_offer(
3036 new JsepSessionDescription(JsepSessionDescription::kOffer));
3037 modified_offer->Initialize(offer->description()->Copy(), offer->session_id(),
3038 offer->session_version());
3039 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
deadbeefab9b2d12015-10-14 11:33:11 -07003040
deadbeefab9b2d12015-10-14 11:33:11 -07003041 auto senders = pc_->GetSenders();
3042 EXPECT_EQ(2u, senders.size());
3043 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
3044 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
3045
3046 // Change the ssrc of the audio and video track.
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003047 cricket::MediaContentDescription* desc =
3048 cricket::GetFirstAudioContentDescription(modified_offer->description());
3049 ASSERT_TRUE(desc != NULL);
3050 for (StreamParams& stream : desc->mutable_streams()) {
3051 for (unsigned int& ssrc : stream.ssrcs) {
3052 ++ssrc;
3053 }
3054 }
deadbeefab9b2d12015-10-14 11:33:11 -07003055
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003056 desc =
3057 cricket::GetFirstVideoContentDescription(modified_offer->description());
3058 ASSERT_TRUE(desc != NULL);
3059 for (StreamParams& stream : desc->mutable_streams()) {
3060 for (unsigned int& ssrc : stream.ssrcs) {
3061 ++ssrc;
3062 }
3063 }
3064
3065 EXPECT_TRUE(DoSetLocalDescription(modified_offer.release()));
deadbeefab9b2d12015-10-14 11:33:11 -07003066 senders = pc_->GetSenders();
3067 EXPECT_EQ(2u, senders.size());
3068 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
3069 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
3070 // TODO(deadbeef): Once RtpSenders expose parameters, check that the SSRC
3071 // changed.
3072}
3073
3074// This tests that the expected behavior occurs if a new session description is
3075// set with the same tracks, but on a different MediaStream.
Stefan Holmer55d6e7c2016-03-17 16:26:40 +01003076TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07003077 SignalSameTracksInSeparateMediaStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07003078 FakeConstraints constraints;
3079 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
3080 true);
3081 CreatePeerConnection(&constraints);
deadbeefab9b2d12015-10-14 11:33:11 -07003082
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003083 rtc::scoped_refptr<StreamCollection> stream_collection =
3084 CreateStreamCollection(2, 1);
3085 pc_->AddStream(stream_collection->at(0));
3086 std::unique_ptr<SessionDescriptionInterface> offer;
3087 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
3088 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
deadbeefab9b2d12015-10-14 11:33:11 -07003089
deadbeefab9b2d12015-10-14 11:33:11 -07003090 auto senders = pc_->GetSenders();
3091 EXPECT_EQ(2u, senders.size());
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003092 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0], kStreams[0]));
3093 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0], kStreams[0]));
deadbeefab9b2d12015-10-14 11:33:11 -07003094
3095 // Add a new MediaStream but with the same tracks as in the first stream.
3096 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream_1(
3097 webrtc::MediaStream::Create(kStreams[1]));
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003098 stream_1->AddTrack(stream_collection->at(0)->GetVideoTracks()[0]);
3099 stream_1->AddTrack(stream_collection->at(0)->GetAudioTracks()[0]);
deadbeefab9b2d12015-10-14 11:33:11 -07003100 pc_->AddStream(stream_1);
3101
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003102 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
3103 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
deadbeefab9b2d12015-10-14 11:33:11 -07003104
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003105 auto new_senders = pc_->GetSenders();
3106 // Should be the same senders as before, but with updated stream id.
3107 // Note that this behavior is subject to change in the future.
3108 // We may decide the PC should ignore existing tracks in AddStream.
3109 EXPECT_EQ(senders, new_senders);
3110 EXPECT_TRUE(ContainsSender(new_senders, kAudioTracks[0], kStreams[1]));
3111 EXPECT_TRUE(ContainsSender(new_senders, kVideoTracks[0], kStreams[1]));
deadbeefab9b2d12015-10-14 11:33:11 -07003112}
3113
zhihuang81c3a032016-11-17 12:06:24 -08003114// This tests that PeerConnectionObserver::OnAddTrack is correctly called.
3115TEST_F(PeerConnectionInterfaceTest, OnAddTrackCallback) {
3116 FakeConstraints constraints;
3117 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
3118 true);
3119 CreatePeerConnection(&constraints);
3120 CreateAndSetRemoteOffer(kSdpStringWithStream1AudioTrackOnly);
3121 EXPECT_EQ(observer_.num_added_tracks_, 1);
3122 EXPECT_EQ(observer_.last_added_track_label_, kAudioTracks[0]);
3123
3124 // Create and set the updated remote SDP.
3125 CreateAndSetRemoteOffer(kSdpStringWithStream1);
3126 EXPECT_EQ(observer_.num_added_tracks_, 2);
3127 EXPECT_EQ(observer_.last_added_track_label_, kVideoTracks[0]);
3128}
3129
deadbeefd1a38b52016-12-10 13:15:33 -08003130// Test that when SetConfiguration is called and the configuration is
3131// changing, the next offer causes an ICE restart.
3132TEST_F(PeerConnectionInterfaceTest, SetConfigurationCausingIceRetart) {
3133 PeerConnectionInterface::RTCConfiguration config;
3134 config.type = PeerConnectionInterface::kRelay;
3135 // Need to pass default constraints to prevent disabling of DTLS...
3136 FakeConstraints default_constraints;
3137 CreatePeerConnection(config, &default_constraints);
3138 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
3139
3140 // Do initial offer/answer so there's something to restart.
3141 CreateOfferAsLocalDescription();
3142 CreateAnswerAsRemoteDescription(kSdpStringWithStream1);
3143
3144 // Grab the ufrags.
3145 std::vector<std::string> initial_ufrags = GetUfrags(pc_->local_description());
3146
3147 // Change ICE policy, which should trigger an ICE restart on the next offer.
3148 config.type = PeerConnectionInterface::kAll;
3149 EXPECT_TRUE(pc_->SetConfiguration(config));
3150 CreateOfferAsLocalDescription();
3151
3152 // Grab the new ufrags.
3153 std::vector<std::string> subsequent_ufrags =
3154 GetUfrags(pc_->local_description());
3155
3156 // Sanity check.
3157 EXPECT_EQ(initial_ufrags.size(), subsequent_ufrags.size());
3158 // Check that each ufrag is different.
3159 for (int i = 0; i < static_cast<int>(initial_ufrags.size()); ++i) {
3160 EXPECT_NE(initial_ufrags[i], subsequent_ufrags[i]);
3161 }
3162}
3163
3164// Test that when SetConfiguration is called and the configuration *isn't*
3165// changing, the next offer does *not* cause an ICE restart.
3166TEST_F(PeerConnectionInterfaceTest, SetConfigurationNotCausingIceRetart) {
3167 PeerConnectionInterface::RTCConfiguration config;
3168 config.type = PeerConnectionInterface::kRelay;
3169 // Need to pass default constraints to prevent disabling of DTLS...
3170 FakeConstraints default_constraints;
3171 CreatePeerConnection(config, &default_constraints);
3172 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
3173
3174 // Do initial offer/answer so there's something to restart.
3175 CreateOfferAsLocalDescription();
3176 CreateAnswerAsRemoteDescription(kSdpStringWithStream1);
3177
3178 // Grab the ufrags.
3179 std::vector<std::string> initial_ufrags = GetUfrags(pc_->local_description());
3180
3181 // Call SetConfiguration with a config identical to what the PC was
3182 // constructed with.
3183 EXPECT_TRUE(pc_->SetConfiguration(config));
3184 CreateOfferAsLocalDescription();
3185
3186 // Grab the new ufrags.
3187 std::vector<std::string> subsequent_ufrags =
3188 GetUfrags(pc_->local_description());
3189
3190 EXPECT_EQ(initial_ufrags, subsequent_ufrags);
3191}
3192
3193// Test for a weird corner case scenario:
3194// 1. Audio/video session established.
3195// 2. SetConfiguration changes ICE config; ICE restart needed.
3196// 3. ICE restart initiated by remote peer, but only for one m= section.
3197// 4. Next createOffer should initiate an ICE restart, but only for the other
3198// m= section; it would be pointless to do an ICE restart for the m= section
3199// that was already restarted.
3200TEST_F(PeerConnectionInterfaceTest, SetConfigurationCausingPartialIceRestart) {
3201 PeerConnectionInterface::RTCConfiguration config;
3202 config.type = PeerConnectionInterface::kRelay;
3203 // Need to pass default constraints to prevent disabling of DTLS...
3204 FakeConstraints default_constraints;
3205 CreatePeerConnection(config, &default_constraints);
3206 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
3207
3208 // Do initial offer/answer so there's something to restart.
3209 CreateOfferAsLocalDescription();
3210 CreateAnswerAsRemoteDescription(kSdpStringWithStream1);
3211
3212 // Change ICE policy, which should set the "needs-ice-restart" flag.
3213 config.type = PeerConnectionInterface::kAll;
3214 EXPECT_TRUE(pc_->SetConfiguration(config));
3215
3216 // Do ICE restart for the first m= section, initiated by remote peer.
3217 webrtc::JsepSessionDescription* remote_offer =
3218 new webrtc::JsepSessionDescription(SessionDescriptionInterface::kOffer);
3219 EXPECT_TRUE(remote_offer->Initialize(kSdpStringWithStream1, nullptr));
3220 remote_offer->description()->transport_infos()[0].description.ice_ufrag =
3221 "modified";
3222 EXPECT_TRUE(DoSetRemoteDescription(remote_offer));
3223 CreateAnswerAsLocalDescription();
3224
3225 // Grab the ufrags.
3226 std::vector<std::string> initial_ufrags = GetUfrags(pc_->local_description());
3227 ASSERT_EQ(2, initial_ufrags.size());
3228
3229 // Create offer and grab the new ufrags.
3230 CreateOfferAsLocalDescription();
3231 std::vector<std::string> subsequent_ufrags =
3232 GetUfrags(pc_->local_description());
3233 ASSERT_EQ(2, subsequent_ufrags.size());
3234
3235 // Ensure that only the ufrag for the second m= section changed.
3236 EXPECT_EQ(initial_ufrags[0], subsequent_ufrags[0]);
3237 EXPECT_NE(initial_ufrags[1], subsequent_ufrags[1]);
3238}
3239
deadbeeffe4a8a42016-12-20 17:56:17 -08003240// Tests that the methods to return current/pending descriptions work as
3241// expected at different points in the offer/answer exchange. This test does
3242// one offer/answer exchange as the offerer, then another as the answerer.
3243TEST_F(PeerConnectionInterfaceTest, CurrentAndPendingDescriptions) {
3244 // This disables DTLS so we can apply an answer to ourselves.
3245 CreatePeerConnection();
3246
3247 // Create initial local offer and get SDP (which will also be used as
3248 // answer/pranswer);
3249 std::unique_ptr<SessionDescriptionInterface> offer;
3250 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
3251 std::string sdp;
3252 EXPECT_TRUE(offer->ToString(&sdp));
3253
3254 // Set local offer.
3255 SessionDescriptionInterface* local_offer = offer.release();
3256 EXPECT_TRUE(DoSetLocalDescription(local_offer));
3257 EXPECT_EQ(local_offer, pc_->pending_local_description());
3258 EXPECT_EQ(nullptr, pc_->pending_remote_description());
3259 EXPECT_EQ(nullptr, pc_->current_local_description());
3260 EXPECT_EQ(nullptr, pc_->current_remote_description());
3261
3262 // Set remote pranswer.
3263 SessionDescriptionInterface* remote_pranswer =
3264 webrtc::CreateSessionDescription(SessionDescriptionInterface::kPrAnswer,
3265 sdp, nullptr);
3266 EXPECT_TRUE(DoSetRemoteDescription(remote_pranswer));
3267 EXPECT_EQ(local_offer, pc_->pending_local_description());
3268 EXPECT_EQ(remote_pranswer, pc_->pending_remote_description());
3269 EXPECT_EQ(nullptr, pc_->current_local_description());
3270 EXPECT_EQ(nullptr, pc_->current_remote_description());
3271
3272 // Set remote answer.
3273 SessionDescriptionInterface* remote_answer = webrtc::CreateSessionDescription(
3274 SessionDescriptionInterface::kAnswer, sdp, nullptr);
3275 EXPECT_TRUE(DoSetRemoteDescription(remote_answer));
3276 EXPECT_EQ(nullptr, pc_->pending_local_description());
3277 EXPECT_EQ(nullptr, pc_->pending_remote_description());
3278 EXPECT_EQ(local_offer, pc_->current_local_description());
3279 EXPECT_EQ(remote_answer, pc_->current_remote_description());
3280
3281 // Set remote offer.
3282 SessionDescriptionInterface* remote_offer = webrtc::CreateSessionDescription(
3283 SessionDescriptionInterface::kOffer, sdp, nullptr);
3284 EXPECT_TRUE(DoSetRemoteDescription(remote_offer));
3285 EXPECT_EQ(remote_offer, pc_->pending_remote_description());
3286 EXPECT_EQ(nullptr, pc_->pending_local_description());
3287 EXPECT_EQ(local_offer, pc_->current_local_description());
3288 EXPECT_EQ(remote_answer, pc_->current_remote_description());
3289
3290 // Set local pranswer.
3291 SessionDescriptionInterface* local_pranswer =
3292 webrtc::CreateSessionDescription(SessionDescriptionInterface::kPrAnswer,
3293 sdp, nullptr);
3294 EXPECT_TRUE(DoSetLocalDescription(local_pranswer));
3295 EXPECT_EQ(remote_offer, pc_->pending_remote_description());
3296 EXPECT_EQ(local_pranswer, pc_->pending_local_description());
3297 EXPECT_EQ(local_offer, pc_->current_local_description());
3298 EXPECT_EQ(remote_answer, pc_->current_remote_description());
3299
3300 // Set local answer.
3301 SessionDescriptionInterface* local_answer = webrtc::CreateSessionDescription(
3302 SessionDescriptionInterface::kAnswer, sdp, nullptr);
3303 EXPECT_TRUE(DoSetLocalDescription(local_answer));
3304 EXPECT_EQ(nullptr, pc_->pending_remote_description());
3305 EXPECT_EQ(nullptr, pc_->pending_local_description());
3306 EXPECT_EQ(remote_offer, pc_->current_remote_description());
3307 EXPECT_EQ(local_answer, pc_->current_local_description());
3308}
3309
zhihuang77985012017-02-07 15:45:16 -08003310// Tests that it won't crash when calling StartRtcEventLog or StopRtcEventLog
3311// after the PeerConnection is closed.
3312TEST_F(PeerConnectionInterfaceTest,
3313 StartAndStopLoggingAfterPeerConnectionClosed) {
3314 CreatePeerConnection();
3315 // The RtcEventLog will be reset when the PeerConnection is closed.
3316 pc_->Close();
3317
3318 rtc::PlatformFile file = 0;
3319 int64_t max_size_bytes = 1024;
3320 EXPECT_FALSE(pc_->StartRtcEventLog(file, max_size_bytes));
3321 pc_->StopRtcEventLog();
3322}
3323
deadbeef30952b42017-04-21 02:41:29 -07003324// Test that generated offers/answers include "ice-option:trickle".
3325TEST_F(PeerConnectionInterfaceTest, OffersAndAnswersHaveTrickleIceOption) {
3326 CreatePeerConnection();
3327
3328 // First, create an offer with audio/video.
3329 FakeConstraints constraints;
3330 constraints.SetMandatoryReceiveAudio(true);
3331 constraints.SetMandatoryReceiveVideo(true);
3332 std::unique_ptr<SessionDescriptionInterface> offer;
3333 ASSERT_TRUE(DoCreateOffer(&offer, &constraints));
3334 cricket::SessionDescription* desc = offer->description();
3335 ASSERT_EQ(2u, desc->transport_infos().size());
3336 EXPECT_TRUE(desc->transport_infos()[0].description.HasOption("trickle"));
3337 EXPECT_TRUE(desc->transport_infos()[1].description.HasOption("trickle"));
3338
3339 // Apply the offer as a remote description, then create an answer.
3340 EXPECT_TRUE(DoSetRemoteDescription(offer.release()));
3341 std::unique_ptr<SessionDescriptionInterface> answer;
3342 ASSERT_TRUE(DoCreateAnswer(&answer, &constraints));
3343 desc = answer->description();
3344 ASSERT_EQ(2u, desc->transport_infos().size());
3345 EXPECT_TRUE(desc->transport_infos()[0].description.HasOption("trickle"));
3346 EXPECT_TRUE(desc->transport_infos()[1].description.HasOption("trickle"));
3347}
3348
deadbeef1dcb1642017-03-29 21:08:16 -07003349// Test that ICE renomination isn't offered if it's not enabled in the PC's
3350// RTCConfiguration.
3351TEST_F(PeerConnectionInterfaceTest, IceRenominationNotOffered) {
3352 PeerConnectionInterface::RTCConfiguration config;
3353 config.enable_ice_renomination = false;
3354 CreatePeerConnection(config, nullptr);
3355 AddVoiceStream("foo");
3356
3357 std::unique_ptr<SessionDescriptionInterface> offer;
3358 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
3359 cricket::SessionDescription* desc = offer->description();
3360 EXPECT_EQ(1u, desc->transport_infos().size());
3361 EXPECT_FALSE(
3362 desc->transport_infos()[0].description.GetIceParameters().renomination);
3363}
3364
3365// Test that the ICE renomination option is present in generated offers/answers
3366// if it's enabled in the PC's RTCConfiguration.
3367TEST_F(PeerConnectionInterfaceTest, IceRenominationOptionInOfferAndAnswer) {
3368 PeerConnectionInterface::RTCConfiguration config;
3369 config.enable_ice_renomination = true;
3370 CreatePeerConnection(config, nullptr);
3371 AddVoiceStream("foo");
3372
3373 std::unique_ptr<SessionDescriptionInterface> offer;
3374 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
3375 cricket::SessionDescription* desc = offer->description();
3376 EXPECT_EQ(1u, desc->transport_infos().size());
3377 EXPECT_TRUE(
3378 desc->transport_infos()[0].description.GetIceParameters().renomination);
3379
3380 // Set the offer as a remote description, then create an answer and ensure it
3381 // has the renomination flag too.
3382 EXPECT_TRUE(DoSetRemoteDescription(offer.release()));
3383 std::unique_ptr<SessionDescriptionInterface> answer;
3384 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
3385 desc = answer->description();
3386 EXPECT_EQ(1u, desc->transport_infos().size());
3387 EXPECT_TRUE(
3388 desc->transport_infos()[0].description.GetIceParameters().renomination);
3389}
3390
3391// Test that if CreateOffer is called with the deprecated "offer to receive
3392// audio/video" constraints, they're processed and result in an offer with
3393// audio/video sections just as if RTCOfferAnswerOptions had been used.
3394TEST_F(PeerConnectionInterfaceTest, CreateOfferWithOfferToReceiveConstraints) {
3395 CreatePeerConnection();
3396
3397 FakeConstraints constraints;
3398 constraints.SetMandatoryReceiveAudio(true);
3399 constraints.SetMandatoryReceiveVideo(true);
3400 std::unique_ptr<SessionDescriptionInterface> offer;
3401 ASSERT_TRUE(DoCreateOffer(&offer, &constraints));
3402
3403 cricket::SessionDescription* desc = offer->description();
3404 const cricket::ContentInfo* audio = cricket::GetFirstAudioContent(desc);
3405 const cricket::ContentInfo* video = cricket::GetFirstVideoContent(desc);
3406 ASSERT_NE(nullptr, audio);
3407 ASSERT_NE(nullptr, video);
3408 EXPECT_FALSE(audio->rejected);
3409 EXPECT_FALSE(video->rejected);
3410}
3411
3412// Test that if CreateAnswer is called with the deprecated "offer to receive
3413// audio/video" constraints, they're processed and can be used to reject an
3414// offered m= section just as can be done with RTCOfferAnswerOptions;
3415TEST_F(PeerConnectionInterfaceTest, CreateAnswerWithOfferToReceiveConstraints) {
3416 CreatePeerConnection();
3417
3418 // First, create an offer with audio/video and apply it as a remote
3419 // description.
3420 FakeConstraints constraints;
3421 constraints.SetMandatoryReceiveAudio(true);
3422 constraints.SetMandatoryReceiveVideo(true);
3423 std::unique_ptr<SessionDescriptionInterface> offer;
3424 ASSERT_TRUE(DoCreateOffer(&offer, &constraints));
3425 EXPECT_TRUE(DoSetRemoteDescription(offer.release()));
3426
3427 // Now create answer that rejects audio/video.
3428 constraints.SetMandatoryReceiveAudio(false);
3429 constraints.SetMandatoryReceiveVideo(false);
3430 std::unique_ptr<SessionDescriptionInterface> answer;
3431 ASSERT_TRUE(DoCreateAnswer(&answer, &constraints));
3432
3433 cricket::SessionDescription* desc = answer->description();
3434 const cricket::ContentInfo* audio = cricket::GetFirstAudioContent(desc);
3435 const cricket::ContentInfo* video = cricket::GetFirstVideoContent(desc);
3436 ASSERT_NE(nullptr, audio);
3437 ASSERT_NE(nullptr, video);
3438 EXPECT_TRUE(audio->rejected);
3439 EXPECT_TRUE(video->rejected);
3440}
3441
3442#ifdef HAVE_SCTP
3443#define MAYBE_DataChannelOnlyOfferWithMaxBundlePolicy \
3444 DataChannelOnlyOfferWithMaxBundlePolicy
3445#else
3446#define MAYBE_DataChannelOnlyOfferWithMaxBundlePolicy \
3447 DISABLED_DataChannelOnlyOfferWithMaxBundlePolicy
3448#endif
3449
3450// Test that negotiation can succeed with a data channel only, and with the max
3451// bundle policy. Previously there was a bug that prevented this.
3452TEST_F(PeerConnectionInterfaceTest,
3453 MAYBE_DataChannelOnlyOfferWithMaxBundlePolicy) {
3454 PeerConnectionInterface::RTCConfiguration config;
3455 config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
3456 CreatePeerConnection(config, nullptr);
3457
3458 // First, create an offer with only a data channel and apply it as a remote
3459 // description.
3460 pc_->CreateDataChannel("test", nullptr);
3461 std::unique_ptr<SessionDescriptionInterface> offer;
3462 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
3463 EXPECT_TRUE(DoSetRemoteDescription(offer.release()));
3464
3465 // Create and set answer as well.
3466 std::unique_ptr<SessionDescriptionInterface> answer;
3467 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
3468 EXPECT_TRUE(DoSetLocalDescription(answer.release()));
3469}
3470
zstein4b979802017-06-02 14:37:37 -07003471TEST_F(PeerConnectionInterfaceTest, SetBitrateWithoutMinSucceeds) {
3472 CreatePeerConnection();
3473 PeerConnectionInterface::BitrateParameters bitrate;
3474 bitrate.current_bitrate_bps = rtc::Optional<int>(100000);
3475 EXPECT_TRUE(pc_->SetBitrate(bitrate).ok());
3476}
3477
3478TEST_F(PeerConnectionInterfaceTest, SetBitrateNegativeMinFails) {
3479 CreatePeerConnection();
3480 PeerConnectionInterface::BitrateParameters bitrate;
3481 bitrate.min_bitrate_bps = rtc::Optional<int>(-1);
3482 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3483}
3484
3485TEST_F(PeerConnectionInterfaceTest, SetBitrateCurrentLessThanMinFails) {
3486 CreatePeerConnection();
3487 PeerConnectionInterface::BitrateParameters bitrate;
3488 bitrate.min_bitrate_bps = rtc::Optional<int>(5);
3489 bitrate.current_bitrate_bps = rtc::Optional<int>(3);
3490 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3491}
3492
3493TEST_F(PeerConnectionInterfaceTest, SetBitrateCurrentNegativeFails) {
3494 CreatePeerConnection();
3495 PeerConnectionInterface::BitrateParameters bitrate;
3496 bitrate.current_bitrate_bps = rtc::Optional<int>(-1);
3497 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3498}
3499
3500TEST_F(PeerConnectionInterfaceTest, SetBitrateMaxLessThanCurrentFails) {
3501 CreatePeerConnection();
3502 PeerConnectionInterface::BitrateParameters bitrate;
3503 bitrate.current_bitrate_bps = rtc::Optional<int>(10);
3504 bitrate.max_bitrate_bps = rtc::Optional<int>(8);
3505 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3506}
3507
3508TEST_F(PeerConnectionInterfaceTest, SetBitrateMaxLessThanMinFails) {
3509 CreatePeerConnection();
3510 PeerConnectionInterface::BitrateParameters bitrate;
3511 bitrate.min_bitrate_bps = rtc::Optional<int>(10);
3512 bitrate.max_bitrate_bps = rtc::Optional<int>(8);
3513 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3514}
3515
3516TEST_F(PeerConnectionInterfaceTest, SetBitrateMaxNegativeFails) {
3517 CreatePeerConnection();
3518 PeerConnectionInterface::BitrateParameters bitrate;
3519 bitrate.max_bitrate_bps = rtc::Optional<int>(-1);
3520 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3521}
3522
Steve Anton038834f2017-07-14 15:59:59 -07003523// ice_regather_interval_range requires WebRTC to be configured for continual
3524// gathering already.
3525TEST_F(PeerConnectionInterfaceTest,
3526 SetIceRegatherIntervalRangeWithoutContinualGatheringFails) {
3527 PeerConnectionInterface::RTCConfiguration config;
3528 config.ice_regather_interval_range.emplace(1000, 2000);
3529 config.continual_gathering_policy =
3530 PeerConnectionInterface::ContinualGatheringPolicy::GATHER_ONCE;
3531 CreatePeerConnectionExpectFail(config);
3532}
3533
3534// Ensures that there is no error when ice_regather_interval_range is set with
3535// continual gathering enabled.
3536TEST_F(PeerConnectionInterfaceTest,
3537 SetIceRegatherIntervalRangeWithContinualGathering) {
3538 PeerConnectionInterface::RTCConfiguration config;
3539 config.ice_regather_interval_range.emplace(1000, 2000);
3540 config.continual_gathering_policy =
3541 PeerConnectionInterface::ContinualGatheringPolicy::GATHER_CONTINUALLY;
3542 CreatePeerConnection(config, nullptr);
3543}
3544
zstein4b979802017-06-02 14:37:37 -07003545// The current bitrate from Call's BitrateConfigMask is currently clamped by
3546// Call's BitrateConfig, which comes from the SDP or a default value. This test
3547// checks that a call to SetBitrate with a current bitrate that will be clamped
3548// succeeds.
3549TEST_F(PeerConnectionInterfaceTest, SetBitrateCurrentLessThanImplicitMin) {
3550 CreatePeerConnection();
3551 PeerConnectionInterface::BitrateParameters bitrate;
3552 bitrate.current_bitrate_bps = rtc::Optional<int>(1);
3553 EXPECT_TRUE(pc_->SetBitrate(bitrate).ok());
3554}
3555
zhihuang1c378ed2017-08-17 14:10:50 -07003556// The following tests verify that the offer can be created correctly.
3557TEST_F(PeerConnectionInterfaceTest,
3558 CreateOfferFailsWithInvalidOfferToReceiveAudio) {
3559 RTCOfferAnswerOptions rtc_options;
3560
3561 // Setting offer_to_receive_audio to a value lower than kUndefined or greater
3562 // than kMaxOfferToReceiveMedia should be treated as invalid.
3563 rtc_options.offer_to_receive_audio = RTCOfferAnswerOptions::kUndefined - 1;
3564 CreatePeerConnection();
3565 EXPECT_FALSE(CreateOfferWithOptions(rtc_options));
3566
3567 rtc_options.offer_to_receive_audio =
3568 RTCOfferAnswerOptions::kMaxOfferToReceiveMedia + 1;
3569 EXPECT_FALSE(CreateOfferWithOptions(rtc_options));
3570}
3571
3572TEST_F(PeerConnectionInterfaceTest,
3573 CreateOfferFailsWithInvalidOfferToReceiveVideo) {
3574 RTCOfferAnswerOptions rtc_options;
3575
3576 // Setting offer_to_receive_video to a value lower than kUndefined or greater
3577 // than kMaxOfferToReceiveMedia should be treated as invalid.
3578 rtc_options.offer_to_receive_video = RTCOfferAnswerOptions::kUndefined - 1;
3579 CreatePeerConnection();
3580 EXPECT_FALSE(CreateOfferWithOptions(rtc_options));
3581
3582 rtc_options.offer_to_receive_video =
3583 RTCOfferAnswerOptions::kMaxOfferToReceiveMedia + 1;
3584 EXPECT_FALSE(CreateOfferWithOptions(rtc_options));
3585}
3586
3587// Test that the audio and video content will be added to an offer if both
3588// |offer_to_receive_audio| and |offer_to_receive_video| options are 1.
3589TEST_F(PeerConnectionInterfaceTest, CreateOfferWithAudioVideoOptions) {
3590 RTCOfferAnswerOptions rtc_options;
3591 rtc_options.offer_to_receive_audio = 1;
3592 rtc_options.offer_to_receive_video = 1;
3593
3594 std::unique_ptr<SessionDescriptionInterface> offer;
3595 CreatePeerConnection();
3596 offer = CreateOfferWithOptions(rtc_options);
3597 ASSERT_TRUE(offer);
3598 EXPECT_NE(nullptr, GetFirstAudioContent(offer->description()));
3599 EXPECT_NE(nullptr, GetFirstVideoContent(offer->description()));
3600}
3601
3602// Test that only audio content will be added to the offer if only
3603// |offer_to_receive_audio| options is 1.
3604TEST_F(PeerConnectionInterfaceTest, CreateOfferWithAudioOnlyOptions) {
3605 RTCOfferAnswerOptions rtc_options;
3606 rtc_options.offer_to_receive_audio = 1;
3607 rtc_options.offer_to_receive_video = 0;
3608
3609 std::unique_ptr<SessionDescriptionInterface> offer;
3610 CreatePeerConnection();
3611 offer = CreateOfferWithOptions(rtc_options);
3612 ASSERT_TRUE(offer);
3613 EXPECT_NE(nullptr, GetFirstAudioContent(offer->description()));
3614 EXPECT_EQ(nullptr, GetFirstVideoContent(offer->description()));
3615}
3616
3617// Test that only video content will be added if only |offer_to_receive_video|
3618// options is 1.
3619TEST_F(PeerConnectionInterfaceTest, CreateOfferWithVideoOnlyOptions) {
3620 RTCOfferAnswerOptions rtc_options;
3621 rtc_options.offer_to_receive_audio = 0;
3622 rtc_options.offer_to_receive_video = 1;
3623
3624 std::unique_ptr<SessionDescriptionInterface> offer;
3625 CreatePeerConnection();
3626 offer = CreateOfferWithOptions(rtc_options);
3627 ASSERT_TRUE(offer);
3628 EXPECT_EQ(nullptr, GetFirstAudioContent(offer->description()));
3629 EXPECT_NE(nullptr, GetFirstVideoContent(offer->description()));
3630}
3631
3632// Test that if |voice_activity_detection| is false, no CN codec is added to the
3633// offer.
3634TEST_F(PeerConnectionInterfaceTest, CreateOfferWithVADOptions) {
3635 RTCOfferAnswerOptions rtc_options;
3636 rtc_options.offer_to_receive_audio = 1;
3637 rtc_options.offer_to_receive_video = 0;
3638
3639 std::unique_ptr<SessionDescriptionInterface> offer;
3640 CreatePeerConnection();
3641 offer = CreateOfferWithOptions(rtc_options);
3642 ASSERT_TRUE(offer);
3643 const cricket::ContentInfo* audio_content =
3644 offer->description()->GetContentByName(cricket::CN_AUDIO);
3645 ASSERT_TRUE(audio_content);
3646 // |voice_activity_detection| is true by default.
3647 EXPECT_TRUE(HasCNCodecs(audio_content));
3648
3649 rtc_options.voice_activity_detection = false;
3650 CreatePeerConnection();
3651 offer = CreateOfferWithOptions(rtc_options);
3652 ASSERT_TRUE(offer);
3653 audio_content = offer->description()->GetContentByName(cricket::CN_AUDIO);
3654 ASSERT_TRUE(audio_content);
3655 EXPECT_FALSE(HasCNCodecs(audio_content));
3656}
3657
3658// Test that no media content will be added to the offer if using default
3659// RTCOfferAnswerOptions.
3660TEST_F(PeerConnectionInterfaceTest, CreateOfferWithDefaultOfferAnswerOptions) {
3661 RTCOfferAnswerOptions rtc_options;
3662
3663 std::unique_ptr<SessionDescriptionInterface> offer;
3664 CreatePeerConnection();
3665 offer = CreateOfferWithOptions(rtc_options);
3666 ASSERT_TRUE(offer);
3667 EXPECT_EQ(nullptr, GetFirstAudioContent(offer->description()));
3668 EXPECT_EQ(nullptr, GetFirstVideoContent(offer->description()));
3669}
3670
3671// Test that if |ice_restart| is true, the ufrag/pwd will change, otherwise
3672// ufrag/pwd will be the same in the new offer.
3673TEST_F(PeerConnectionInterfaceTest, CreateOfferWithIceRestart) {
3674 RTCOfferAnswerOptions rtc_options;
3675 rtc_options.ice_restart = false;
3676 rtc_options.offer_to_receive_audio = 1;
3677
3678 std::unique_ptr<SessionDescriptionInterface> offer;
3679 CreatePeerConnection();
3680 CreateOfferWithOptionsAsLocalDescription(&offer, rtc_options);
3681 auto ufrag1 = offer->description()
3682 ->GetTransportInfoByName(cricket::CN_AUDIO)
3683 ->description.ice_ufrag;
3684 auto pwd1 = offer->description()
3685 ->GetTransportInfoByName(cricket::CN_AUDIO)
3686 ->description.ice_pwd;
3687
3688 // |ice_restart| is false, the ufrag/pwd shouldn't change.
3689 CreateOfferWithOptionsAsLocalDescription(&offer, rtc_options);
3690 auto ufrag2 = offer->description()
3691 ->GetTransportInfoByName(cricket::CN_AUDIO)
3692 ->description.ice_ufrag;
3693 auto pwd2 = offer->description()
3694 ->GetTransportInfoByName(cricket::CN_AUDIO)
3695 ->description.ice_pwd;
3696
3697 // |ice_restart| is true, the ufrag/pwd should change.
3698 rtc_options.ice_restart = true;
3699 CreateOfferWithOptionsAsLocalDescription(&offer, rtc_options);
3700 auto ufrag3 = offer->description()
3701 ->GetTransportInfoByName(cricket::CN_AUDIO)
3702 ->description.ice_ufrag;
3703 auto pwd3 = offer->description()
3704 ->GetTransportInfoByName(cricket::CN_AUDIO)
3705 ->description.ice_pwd;
3706
3707 EXPECT_EQ(ufrag1, ufrag2);
3708 EXPECT_EQ(pwd1, pwd2);
3709 EXPECT_NE(ufrag2, ufrag3);
3710 EXPECT_NE(pwd2, pwd3);
3711}
3712
3713// Test that if |use_rtp_mux| is true, the bundling will be enabled in the
3714// offer; if it is false, there won't be any bundle group in the offer.
3715TEST_F(PeerConnectionInterfaceTest, CreateOfferWithRtpMux) {
3716 RTCOfferAnswerOptions rtc_options;
3717 rtc_options.offer_to_receive_audio = 1;
3718 rtc_options.offer_to_receive_video = 1;
3719
3720 std::unique_ptr<SessionDescriptionInterface> offer;
3721 CreatePeerConnection();
3722
3723 rtc_options.use_rtp_mux = true;
3724 offer = CreateOfferWithOptions(rtc_options);
3725 ASSERT_TRUE(offer);
3726 EXPECT_NE(nullptr, GetFirstAudioContent(offer->description()));
3727 EXPECT_NE(nullptr, GetFirstVideoContent(offer->description()));
3728 EXPECT_TRUE(offer->description()->HasGroup(cricket::GROUP_TYPE_BUNDLE));
3729
3730 rtc_options.use_rtp_mux = false;
3731 offer = CreateOfferWithOptions(rtc_options);
3732 ASSERT_TRUE(offer);
3733 EXPECT_NE(nullptr, GetFirstAudioContent(offer->description()));
3734 EXPECT_NE(nullptr, GetFirstVideoContent(offer->description()));
3735 EXPECT_FALSE(offer->description()->HasGroup(cricket::GROUP_TYPE_BUNDLE));
3736}
3737
3738// If SetMandatoryReceiveAudio(false) and SetMandatoryReceiveVideo(false) are
3739// called for the answer constraints, but an audio and a video section were
3740// offered, there will still be an audio and a video section in the answer.
3741TEST_F(PeerConnectionInterfaceTest,
3742 RejectAudioAndVideoInAnswerWithConstraints) {
3743 // Offer both audio and video.
3744 RTCOfferAnswerOptions rtc_offer_options;
3745 rtc_offer_options.offer_to_receive_audio = 1;
3746 rtc_offer_options.offer_to_receive_video = 1;
3747
3748 CreatePeerConnection();
3749 std::unique_ptr<SessionDescriptionInterface> offer;
3750 CreateOfferWithOptionsAsRemoteDescription(&offer, rtc_offer_options);
3751 EXPECT_NE(nullptr, GetFirstAudioContent(offer->description()));
3752 EXPECT_NE(nullptr, GetFirstVideoContent(offer->description()));
3753
3754 // Since an offer has been created with both audio and video,
3755 // Answers will contain the media types that exist in the offer regardless of
3756 // the value of |answer_options.has_audio| and |answer_options.has_video|.
3757 FakeConstraints answer_c;
3758 // Reject both audio and video.
3759 answer_c.SetMandatoryReceiveAudio(false);
3760 answer_c.SetMandatoryReceiveVideo(false);
3761
3762 std::unique_ptr<SessionDescriptionInterface> answer;
3763 ASSERT_TRUE(DoCreateAnswer(&answer, &answer_c));
3764 const cricket::ContentInfo* audio_content =
3765 GetFirstAudioContent(answer->description());
3766 const cricket::ContentInfo* video_content =
3767 GetFirstVideoContent(answer->description());
3768 ASSERT_NE(nullptr, audio_content);
3769 ASSERT_NE(nullptr, video_content);
3770 EXPECT_TRUE(audio_content->rejected);
3771 EXPECT_TRUE(video_content->rejected);
3772}
3773
zhihuang141aacb2017-08-29 13:23:53 -07003774// This test ensures OnRenegotiationNeeded is called when we add track with
3775// MediaStream -> AddTrack in the same way it is called when we add track with
3776// PeerConnection -> AddTrack.
3777// The test can be removed once addStream is rewritten in terms of addTrack
3778// https://bugs.chromium.org/p/webrtc/issues/detail?id=7815
3779TEST_F(PeerConnectionInterfaceTest, MediaStreamAddTrackRemoveTrackRenegotiate) {
3780 CreatePeerConnectionWithoutDtls();
3781 rtc::scoped_refptr<MediaStreamInterface> stream(
3782 pc_factory_->CreateLocalMediaStream(kStreamLabel1));
3783 pc_->AddStream(stream);
3784 rtc::scoped_refptr<AudioTrackInterface> audio_track(
3785 pc_factory_->CreateAudioTrack("audio_track", nullptr));
3786 rtc::scoped_refptr<VideoTrackInterface> video_track(
3787 pc_factory_->CreateVideoTrack(
3788 "video_track", pc_factory_->CreateVideoSource(
3789 std::unique_ptr<cricket::VideoCapturer>(
3790 new cricket::FakeVideoCapturer()))));
3791 stream->AddTrack(audio_track);
3792 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
3793 observer_.renegotiation_needed_ = false;
3794
3795 stream->AddTrack(video_track);
3796 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
3797 observer_.renegotiation_needed_ = false;
3798
3799 stream->RemoveTrack(audio_track);
3800 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
3801 observer_.renegotiation_needed_ = false;
3802
3803 stream->RemoveTrack(video_track);
3804 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
3805 observer_.renegotiation_needed_ = false;
3806}
3807
3808// Tests that creating answer would fail gracefully without being crashed if the
3809// remote description is unset.
3810TEST_F(PeerConnectionInterfaceTest, CreateAnswerWithoutRemoteDescription) {
3811 CreatePeerConnection();
3812 // Creating answer fails because the remote description is unset.
3813 std::unique_ptr<SessionDescriptionInterface> answer;
3814 EXPECT_FALSE(DoCreateAnswer(&answer, nullptr));
3815
3816 // Createing answer succeeds when the remote description is set.
3817 CreateOfferAsRemoteDescription();
3818 EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
3819}
3820
nisse51542be2016-02-12 02:27:06 -08003821class PeerConnectionMediaConfigTest : public testing::Test {
3822 protected:
3823 void SetUp() override {
zhihuang38ede132017-06-15 12:52:32 -07003824 pcf_ = PeerConnectionFactoryForTest::CreatePeerConnectionFactoryForTest();
nisse51542be2016-02-12 02:27:06 -08003825 pcf_->Initialize();
3826 }
nisseeaabdf62017-05-05 02:23:02 -07003827 const cricket::MediaConfig TestCreatePeerConnection(
nisse51542be2016-02-12 02:27:06 -08003828 const PeerConnectionInterface::RTCConfiguration& config,
zhihuang1c378ed2017-08-17 14:10:50 -07003829 const MediaConstraintsInterface* constraints) {
zhihuang9763d562016-08-05 11:14:50 -07003830 rtc::scoped_refptr<PeerConnectionInterface> pc(pcf_->CreatePeerConnection(
3831 config, constraints, nullptr, nullptr, &observer_));
nisse51542be2016-02-12 02:27:06 -08003832 EXPECT_TRUE(pc.get());
nisseeaabdf62017-05-05 02:23:02 -07003833 return pc->GetConfiguration().media_config;
nisse51542be2016-02-12 02:27:06 -08003834 }
3835
zhihuang9763d562016-08-05 11:14:50 -07003836 rtc::scoped_refptr<PeerConnectionFactoryForTest> pcf_;
nisse51542be2016-02-12 02:27:06 -08003837 MockPeerConnectionObserver observer_;
3838};
3839
3840// This test verifies the default behaviour with no constraints and a
3841// default RTCConfiguration.
3842TEST_F(PeerConnectionMediaConfigTest, TestDefaults) {
3843 PeerConnectionInterface::RTCConfiguration config;
3844 FakeConstraints constraints;
3845
3846 const cricket::MediaConfig& media_config =
3847 TestCreatePeerConnection(config, &constraints);
3848
3849 EXPECT_FALSE(media_config.enable_dscp);
nisse0db023a2016-03-01 04:29:59 -08003850 EXPECT_TRUE(media_config.video.enable_cpu_overuse_detection);
3851 EXPECT_FALSE(media_config.video.disable_prerenderer_smoothing);
3852 EXPECT_FALSE(media_config.video.suspend_below_min_bitrate);
nisse51542be2016-02-12 02:27:06 -08003853}
3854
3855// This test verifies the DSCP constraint is recognized and passed to
nisse528b7932017-05-08 03:21:43 -07003856// the PeerConnection.
nisse51542be2016-02-12 02:27:06 -08003857TEST_F(PeerConnectionMediaConfigTest, TestDscpConstraintTrue) {
3858 PeerConnectionInterface::RTCConfiguration config;
3859 FakeConstraints constraints;
3860
3861 constraints.AddOptional(webrtc::MediaConstraintsInterface::kEnableDscp, true);
3862 const cricket::MediaConfig& media_config =
3863 TestCreatePeerConnection(config, &constraints);
3864
3865 EXPECT_TRUE(media_config.enable_dscp);
3866}
3867
3868// This test verifies the cpu overuse detection constraint is
nisse528b7932017-05-08 03:21:43 -07003869// recognized and passed to the PeerConnection.
nisse51542be2016-02-12 02:27:06 -08003870TEST_F(PeerConnectionMediaConfigTest, TestCpuOveruseConstraintFalse) {
3871 PeerConnectionInterface::RTCConfiguration config;
3872 FakeConstraints constraints;
3873
3874 constraints.AddOptional(
3875 webrtc::MediaConstraintsInterface::kCpuOveruseDetection, false);
3876 const cricket::MediaConfig media_config =
3877 TestCreatePeerConnection(config, &constraints);
3878
nisse0db023a2016-03-01 04:29:59 -08003879 EXPECT_FALSE(media_config.video.enable_cpu_overuse_detection);
nisse51542be2016-02-12 02:27:06 -08003880}
3881
3882// This test verifies that the disable_prerenderer_smoothing flag is
nisse528b7932017-05-08 03:21:43 -07003883// propagated from RTCConfiguration to the PeerConnection.
nisse51542be2016-02-12 02:27:06 -08003884TEST_F(PeerConnectionMediaConfigTest, TestDisablePrerendererSmoothingTrue) {
3885 PeerConnectionInterface::RTCConfiguration config;
3886 FakeConstraints constraints;
3887
Niels Möller71bdda02016-03-31 12:59:59 +02003888 config.set_prerenderer_smoothing(false);
nisse51542be2016-02-12 02:27:06 -08003889 const cricket::MediaConfig& media_config =
3890 TestCreatePeerConnection(config, &constraints);
3891
nisse0db023a2016-03-01 04:29:59 -08003892 EXPECT_TRUE(media_config.video.disable_prerenderer_smoothing);
3893}
3894
3895// This test verifies the suspend below min bitrate constraint is
nisse528b7932017-05-08 03:21:43 -07003896// recognized and passed to the PeerConnection.
nisse0db023a2016-03-01 04:29:59 -08003897TEST_F(PeerConnectionMediaConfigTest,
3898 TestSuspendBelowMinBitrateConstraintTrue) {
3899 PeerConnectionInterface::RTCConfiguration config;
3900 FakeConstraints constraints;
3901
3902 constraints.AddOptional(
3903 webrtc::MediaConstraintsInterface::kEnableVideoSuspendBelowMinBitrate,
3904 true);
3905 const cricket::MediaConfig media_config =
3906 TestCreatePeerConnection(config, &constraints);
3907
3908 EXPECT_TRUE(media_config.video.suspend_below_min_bitrate);
nisse51542be2016-02-12 02:27:06 -08003909}
3910
deadbeef293e9262017-01-11 12:28:30 -08003911// Tests a few random fields being different.
3912TEST(RTCConfigurationTest, ComparisonOperators) {
3913 PeerConnectionInterface::RTCConfiguration a;
3914 PeerConnectionInterface::RTCConfiguration b;
3915 EXPECT_EQ(a, b);
3916
3917 PeerConnectionInterface::RTCConfiguration c;
3918 c.servers.push_back(PeerConnectionInterface::IceServer());
3919 EXPECT_NE(a, c);
3920
3921 PeerConnectionInterface::RTCConfiguration d;
3922 d.type = PeerConnectionInterface::kRelay;
3923 EXPECT_NE(a, d);
3924
3925 PeerConnectionInterface::RTCConfiguration e;
3926 e.audio_jitter_buffer_max_packets = 5;
3927 EXPECT_NE(a, e);
3928
3929 PeerConnectionInterface::RTCConfiguration f;
3930 f.ice_connection_receiving_timeout = 1337;
3931 EXPECT_NE(a, f);
3932
3933 PeerConnectionInterface::RTCConfiguration g;
3934 g.disable_ipv6 = true;
3935 EXPECT_NE(a, g);
3936
3937 PeerConnectionInterface::RTCConfiguration h(
3938 PeerConnectionInterface::RTCConfigurationType::kAggressive);
3939 EXPECT_NE(a, h);
3940}