blob: ce5180b2d0af0ee51915a18040c15c634531121e [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"
34#include "webrtc/pc/test/fakertccertificategenerator.h"
35#include "webrtc/pc/test/fakevideotracksource.h"
36#include "webrtc/pc/test/mockpeerconnectionobservers.h"
37#include "webrtc/pc/test/testsdpstrings.h"
38#include "webrtc/pc/videocapturertracksource.h"
39#include "webrtc/pc/videotrack.h"
Edward Lemurc20978e2017-07-06 19:44:34 +020040#include "webrtc/rtc_base/gunit.h"
41#include "webrtc/rtc_base/ssladapter.h"
42#include "webrtc/rtc_base/sslstreamadapter.h"
43#include "webrtc/rtc_base/stringutils.h"
44#include "webrtc/rtc_base/thread.h"
45#include "webrtc/rtc_base/virtualsocketserver.h"
kwibergac9f8762016-09-30 22:29:43 -070046#include "webrtc/test/gmock.h"
47
48#ifdef WEBRTC_ANDROID
ossu7bb87ee2017-01-23 04:56:25 -080049#include "webrtc/pc/test/androidtestinitializer.h"
kwibergac9f8762016-09-30 22:29:43 -070050#endif
henrike@webrtc.org28e20752013-07-10 00:45:36 +000051
52static const char kStreamLabel1[] = "local_stream_1";
53static const char kStreamLabel2[] = "local_stream_2";
54static const char kStreamLabel3[] = "local_stream_3";
55static const int kDefaultStunPort = 3478;
56static const char kStunAddressOnly[] = "stun:address";
57static const char kStunInvalidPort[] = "stun:address:-1";
58static const char kStunAddressPortAndMore1[] = "stun:address:port:more";
59static const char kStunAddressPortAndMore2[] = "stun:address:port more";
60static const char kTurnIceServerUri[] = "turn:user@turn.example.org";
61static const char kTurnUsername[] = "user";
62static const char kTurnPassword[] = "password";
63static const char kTurnHostname[] = "turn.example.org";
Peter Boström0c4e06b2015-10-07 12:23:21 +020064static const uint32_t kTimeout = 10000U;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000065
deadbeefab9b2d12015-10-14 11:33:11 -070066static const char kStreams[][8] = {"stream1", "stream2"};
67static const char kAudioTracks[][32] = {"audiotrack0", "audiotrack1"};
68static const char kVideoTracks[][32] = {"videotrack0", "videotrack1"};
69
deadbeef5e97fb52015-10-15 12:49:08 -070070static const char kRecvonly[] = "recvonly";
71static const char kSendrecv[] = "sendrecv";
72
deadbeefab9b2d12015-10-14 11:33:11 -070073// Reference SDP with a MediaStream with label "stream1" and audio track with
74// id "audio_1" and a video track with id "video_1;
75static const char kSdpStringWithStream1[] =
76 "v=0\r\n"
77 "o=- 0 0 IN IP4 127.0.0.1\r\n"
78 "s=-\r\n"
79 "t=0 0\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -080080 "m=audio 1 RTP/AVPF 103\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -070081 "a=ice-ufrag:e5785931\r\n"
82 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
83 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
84 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -070085 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -070086 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -080087 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -070088 "a=rtpmap:103 ISAC/16000\r\n"
89 "a=ssrc:1 cname:stream1\r\n"
90 "a=ssrc:1 mslabel:stream1\r\n"
91 "a=ssrc:1 label:audiotrack0\r\n"
92 "m=video 1 RTP/AVPF 120\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -080093 "a=ice-ufrag:e5785931\r\n"
94 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
95 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
96 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -070097 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -070098 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -080099 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700100 "a=rtpmap:120 VP8/90000\r\n"
101 "a=ssrc:2 cname:stream1\r\n"
102 "a=ssrc:2 mslabel:stream1\r\n"
103 "a=ssrc:2 label:videotrack0\r\n";
104
zhihuang81c3a032016-11-17 12:06:24 -0800105// Reference SDP with a MediaStream with label "stream1" and audio track with
106// id "audio_1";
107static const char kSdpStringWithStream1AudioTrackOnly[] =
108 "v=0\r\n"
109 "o=- 0 0 IN IP4 127.0.0.1\r\n"
110 "s=-\r\n"
111 "t=0 0\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800112 "m=audio 1 RTP/AVPF 103\r\n"
zhihuang81c3a032016-11-17 12:06:24 -0800113 "a=ice-ufrag:e5785931\r\n"
114 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
115 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
116 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
zhihuang81c3a032016-11-17 12:06:24 -0800117 "a=mid:audio\r\n"
118 "a=sendrecv\r\n"
119 "a=rtpmap:103 ISAC/16000\r\n"
120 "a=ssrc:1 cname:stream1\r\n"
121 "a=ssrc:1 mslabel:stream1\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800122 "a=ssrc:1 label:audiotrack0\r\n"
123 "a=rtcp-mux\r\n";
zhihuang81c3a032016-11-17 12:06:24 -0800124
deadbeefab9b2d12015-10-14 11:33:11 -0700125// Reference SDP with two MediaStreams with label "stream1" and "stream2. Each
126// MediaStreams have one audio track and one video track.
127// This uses MSID.
128static const char kSdpStringWithStream1And2[] =
129 "v=0\r\n"
130 "o=- 0 0 IN IP4 127.0.0.1\r\n"
131 "s=-\r\n"
132 "t=0 0\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800133 "a=msid-semantic: WMS stream1 stream2\r\n"
134 "m=audio 1 RTP/AVPF 103\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700135 "a=ice-ufrag:e5785931\r\n"
136 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
137 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
138 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700139 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700140 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800141 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700142 "a=rtpmap:103 ISAC/16000\r\n"
143 "a=ssrc:1 cname:stream1\r\n"
144 "a=ssrc:1 msid:stream1 audiotrack0\r\n"
145 "a=ssrc:3 cname:stream2\r\n"
146 "a=ssrc:3 msid:stream2 audiotrack1\r\n"
147 "m=video 1 RTP/AVPF 120\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800148 "a=ice-ufrag:e5785931\r\n"
149 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
150 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
151 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700152 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700153 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800154 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700155 "a=rtpmap:120 VP8/0\r\n"
156 "a=ssrc:2 cname:stream1\r\n"
157 "a=ssrc:2 msid:stream1 videotrack0\r\n"
158 "a=ssrc:4 cname:stream2\r\n"
159 "a=ssrc:4 msid:stream2 videotrack1\r\n";
160
161// Reference SDP without MediaStreams. Msid is not supported.
162static const char kSdpStringWithoutStreams[] =
163 "v=0\r\n"
164 "o=- 0 0 IN IP4 127.0.0.1\r\n"
165 "s=-\r\n"
166 "t=0 0\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800167 "m=audio 1 RTP/AVPF 103\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700168 "a=ice-ufrag:e5785931\r\n"
169 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
170 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
171 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700172 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700173 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800174 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700175 "a=rtpmap:103 ISAC/16000\r\n"
176 "m=video 1 RTP/AVPF 120\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800177 "a=ice-ufrag:e5785931\r\n"
178 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
179 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
180 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700181 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700182 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800183 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700184 "a=rtpmap:120 VP8/90000\r\n";
185
186// Reference SDP without MediaStreams. Msid is supported.
187static const char kSdpStringWithMsidWithoutStreams[] =
188 "v=0\r\n"
189 "o=- 0 0 IN IP4 127.0.0.1\r\n"
190 "s=-\r\n"
191 "t=0 0\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800192 "a=msid-semantic: WMS\r\n"
193 "m=audio 1 RTP/AVPF 103\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700194 "a=ice-ufrag:e5785931\r\n"
195 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
196 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
197 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700198 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700199 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800200 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700201 "a=rtpmap:103 ISAC/16000\r\n"
202 "m=video 1 RTP/AVPF 120\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800203 "a=ice-ufrag:e5785931\r\n"
204 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
205 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
206 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700207 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700208 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800209 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700210 "a=rtpmap:120 VP8/90000\r\n";
211
212// Reference SDP without MediaStreams and audio only.
213static const char kSdpStringWithoutStreamsAudioOnly[] =
214 "v=0\r\n"
215 "o=- 0 0 IN IP4 127.0.0.1\r\n"
216 "s=-\r\n"
217 "t=0 0\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800218 "m=audio 1 RTP/AVPF 103\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700219 "a=ice-ufrag:e5785931\r\n"
220 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
221 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
222 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700223 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700224 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800225 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700226 "a=rtpmap:103 ISAC/16000\r\n";
227
228// Reference SENDONLY SDP without MediaStreams. Msid is not supported.
229static const char kSdpStringSendOnlyWithoutStreams[] =
230 "v=0\r\n"
231 "o=- 0 0 IN IP4 127.0.0.1\r\n"
232 "s=-\r\n"
233 "t=0 0\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800234 "m=audio 1 RTP/AVPF 103\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700235 "a=ice-ufrag:e5785931\r\n"
236 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
237 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
238 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700239 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700240 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700241 "a=sendonly\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800242 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700243 "a=rtpmap:103 ISAC/16000\r\n"
244 "m=video 1 RTP/AVPF 120\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800245 "a=ice-ufrag:e5785931\r\n"
246 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
247 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
248 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700249 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700250 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700251 "a=sendonly\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800252 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700253 "a=rtpmap:120 VP8/90000\r\n";
254
255static const char kSdpStringInit[] =
256 "v=0\r\n"
257 "o=- 0 0 IN IP4 127.0.0.1\r\n"
258 "s=-\r\n"
259 "t=0 0\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700260 "a=msid-semantic: WMS\r\n";
261
262static const char kSdpStringAudio[] =
263 "m=audio 1 RTP/AVPF 103\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800264 "a=ice-ufrag:e5785931\r\n"
265 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
266 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
267 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700268 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700269 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800270 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700271 "a=rtpmap:103 ISAC/16000\r\n";
272
273static const char kSdpStringVideo[] =
274 "m=video 1 RTP/AVPF 120\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800275 "a=ice-ufrag:e5785931\r\n"
276 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
277 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
278 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700279 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700280 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800281 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700282 "a=rtpmap:120 VP8/90000\r\n";
283
284static const char kSdpStringMs1Audio0[] =
285 "a=ssrc:1 cname:stream1\r\n"
286 "a=ssrc:1 msid:stream1 audiotrack0\r\n";
287
288static const char kSdpStringMs1Video0[] =
289 "a=ssrc:2 cname:stream1\r\n"
290 "a=ssrc:2 msid:stream1 videotrack0\r\n";
291
292static const char kSdpStringMs1Audio1[] =
293 "a=ssrc:3 cname:stream1\r\n"
294 "a=ssrc:3 msid:stream1 audiotrack1\r\n";
295
296static const char kSdpStringMs1Video1[] =
297 "a=ssrc:4 cname:stream1\r\n"
298 "a=ssrc:4 msid:stream1 videotrack1\r\n";
299
deadbeef8662f942017-01-20 21:20:51 -0800300static const char kDtlsSdesFallbackSdp[] =
301 "v=0\r\n"
302 "o=xxxxxx 7 2 IN IP4 0.0.0.0\r\n"
303 "s=-\r\n"
304 "c=IN IP4 0.0.0.0\r\n"
305 "t=0 0\r\n"
306 "a=group:BUNDLE audio\r\n"
307 "a=msid-semantic: WMS\r\n"
308 "m=audio 1 RTP/SAVPF 0\r\n"
309 "a=sendrecv\r\n"
310 "a=rtcp-mux\r\n"
311 "a=mid:audio\r\n"
312 "a=ssrc:1 cname:stream1\r\n"
313 "a=ssrc:1 mslabel:stream1\r\n"
314 "a=ssrc:1 label:audiotrack0\r\n"
315 "a=ice-ufrag:e5785931\r\n"
316 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
317 "a=rtpmap:0 pcmu/8000\r\n"
318 "a=fingerprint:sha-1 "
319 "4A:AD:B9:B1:3F:82:18:3B:54:02:12:DF:3E:5D:49:6B:19:E5:7C:AB\r\n"
320 "a=setup:actpass\r\n"
321 "a=crypto:1 AES_CM_128_HMAC_SHA1_32 "
322 "inline:NzB4d1BINUAvLEw6UzF3WSJ+PSdFcGdUJShpX1Zj|2^20|1:32 "
323 "dummy_session_params\r\n";
324
perkjd61bf802016-03-24 03:16:19 -0700325using ::testing::Exactly;
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700326using cricket::StreamParams;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000327using webrtc::AudioSourceInterface;
deadbeefab9b2d12015-10-14 11:33:11 -0700328using webrtc::AudioTrack;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000329using webrtc::AudioTrackInterface;
330using webrtc::DataBuffer;
331using webrtc::DataChannelInterface;
332using webrtc::FakeConstraints;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000333using webrtc::IceCandidateInterface;
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700334using webrtc::JsepSessionDescription;
deadbeefc80741f2015-10-22 13:14:45 -0700335using webrtc::MediaConstraintsInterface;
deadbeefab9b2d12015-10-14 11:33:11 -0700336using webrtc::MediaStream;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000337using webrtc::MediaStreamInterface;
338using webrtc::MediaStreamTrackInterface;
339using webrtc::MockCreateSessionDescriptionObserver;
340using webrtc::MockDataChannelObserver;
341using webrtc::MockSetSessionDescriptionObserver;
342using webrtc::MockStatsObserver;
perkjd61bf802016-03-24 03:16:19 -0700343using webrtc::NotifierInterface;
344using webrtc::ObserverInterface;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000345using webrtc::PeerConnectionInterface;
346using webrtc::PeerConnectionObserver;
deadbeef293e9262017-01-11 12:28:30 -0800347using webrtc::RTCError;
348using webrtc::RTCErrorType;
deadbeefab9b2d12015-10-14 11:33:11 -0700349using webrtc::RtpReceiverInterface;
350using webrtc::RtpSenderInterface;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000351using webrtc::SdpParseError;
352using webrtc::SessionDescriptionInterface;
deadbeefab9b2d12015-10-14 11:33:11 -0700353using webrtc::StreamCollection;
354using webrtc::StreamCollectionInterface;
perkja3ede6c2016-03-08 01:27:48 +0100355using webrtc::VideoTrackSourceInterface;
deadbeefab9b2d12015-10-14 11:33:11 -0700356using webrtc::VideoTrack;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000357using webrtc::VideoTrackInterface;
358
deadbeefab9b2d12015-10-14 11:33:11 -0700359typedef PeerConnectionInterface::RTCOfferAnswerOptions RTCOfferAnswerOptions;
360
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000361namespace {
362
363// Gets the first ssrc of given content type from the ContentInfo.
364bool GetFirstSsrc(const cricket::ContentInfo* content_info, int* ssrc) {
365 if (!content_info || !ssrc) {
366 return false;
367 }
368 const cricket::MediaContentDescription* media_desc =
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000369 static_cast<const cricket::MediaContentDescription*>(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000370 content_info->description);
371 if (!media_desc || media_desc->streams().empty()) {
372 return false;
373 }
374 *ssrc = media_desc->streams().begin()->first_ssrc();
375 return true;
376}
377
deadbeefd1a38b52016-12-10 13:15:33 -0800378// Get the ufrags out of an SDP blob. Useful for testing ICE restart
379// behavior.
380std::vector<std::string> GetUfrags(
381 const webrtc::SessionDescriptionInterface* desc) {
382 std::vector<std::string> ufrags;
383 for (const cricket::TransportInfo& info :
384 desc->description()->transport_infos()) {
385 ufrags.push_back(info.description.ice_ufrag);
386 }
387 return ufrags;
388}
389
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000390void SetSsrcToZero(std::string* sdp) {
391 const char kSdpSsrcAtribute[] = "a=ssrc:";
392 const char kSdpSsrcAtributeZero[] = "a=ssrc:0";
393 size_t ssrc_pos = 0;
394 while ((ssrc_pos = sdp->find(kSdpSsrcAtribute, ssrc_pos)) !=
395 std::string::npos) {
396 size_t end_ssrc = sdp->find(" ", ssrc_pos);
397 sdp->replace(ssrc_pos, end_ssrc - ssrc_pos, kSdpSsrcAtributeZero);
398 ssrc_pos = end_ssrc;
399 }
400}
401
deadbeefab9b2d12015-10-14 11:33:11 -0700402// Check if |streams| contains the specified track.
403bool ContainsTrack(const std::vector<cricket::StreamParams>& streams,
404 const std::string& stream_label,
405 const std::string& track_id) {
406 for (const cricket::StreamParams& params : streams) {
407 if (params.sync_label == stream_label && params.id == track_id) {
408 return true;
409 }
410 }
411 return false;
412}
413
414// Check if |senders| contains the specified sender, by id.
415bool ContainsSender(
416 const std::vector<rtc::scoped_refptr<RtpSenderInterface>>& senders,
417 const std::string& id) {
418 for (const auto& sender : senders) {
419 if (sender->id() == id) {
420 return true;
421 }
422 }
423 return false;
424}
425
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700426// Check if |senders| contains the specified sender, by id and stream id.
427bool ContainsSender(
428 const std::vector<rtc::scoped_refptr<RtpSenderInterface>>& senders,
429 const std::string& id,
430 const std::string& stream_id) {
431 for (const auto& sender : senders) {
deadbeefa601f5c2016-06-06 14:27:39 -0700432 if (sender->id() == id && sender->stream_ids()[0] == stream_id) {
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700433 return true;
434 }
435 }
436 return false;
437}
438
deadbeefab9b2d12015-10-14 11:33:11 -0700439// Create a collection of streams.
440// CreateStreamCollection(1) creates a collection that
441// correspond to kSdpStringWithStream1.
442// CreateStreamCollection(2) correspond to kSdpStringWithStream1And2.
443rtc::scoped_refptr<StreamCollection> CreateStreamCollection(
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700444 int number_of_streams,
445 int tracks_per_stream) {
deadbeefab9b2d12015-10-14 11:33:11 -0700446 rtc::scoped_refptr<StreamCollection> local_collection(
447 StreamCollection::Create());
448
449 for (int i = 0; i < number_of_streams; ++i) {
450 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream(
451 webrtc::MediaStream::Create(kStreams[i]));
452
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700453 for (int j = 0; j < tracks_per_stream; ++j) {
454 // Add a local audio track.
455 rtc::scoped_refptr<webrtc::AudioTrackInterface> audio_track(
456 webrtc::AudioTrack::Create(kAudioTracks[i * tracks_per_stream + j],
457 nullptr));
458 stream->AddTrack(audio_track);
deadbeefab9b2d12015-10-14 11:33:11 -0700459
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700460 // Add a local video track.
461 rtc::scoped_refptr<webrtc::VideoTrackInterface> video_track(
462 webrtc::VideoTrack::Create(kVideoTracks[i * tracks_per_stream + j],
mbonadei539d1042017-07-10 02:40:49 -0700463 webrtc::FakeVideoTrackSource::Create()));
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700464 stream->AddTrack(video_track);
465 }
deadbeefab9b2d12015-10-14 11:33:11 -0700466
467 local_collection->AddStream(stream);
468 }
469 return local_collection;
470}
471
472// Check equality of StreamCollections.
473bool CompareStreamCollections(StreamCollectionInterface* s1,
474 StreamCollectionInterface* s2) {
475 if (s1 == nullptr || s2 == nullptr || s1->count() != s2->count()) {
476 return false;
477 }
478
479 for (size_t i = 0; i != s1->count(); ++i) {
480 if (s1->at(i)->label() != s2->at(i)->label()) {
481 return false;
482 }
483 webrtc::AudioTrackVector audio_tracks1 = s1->at(i)->GetAudioTracks();
484 webrtc::AudioTrackVector audio_tracks2 = s2->at(i)->GetAudioTracks();
485 webrtc::VideoTrackVector video_tracks1 = s1->at(i)->GetVideoTracks();
486 webrtc::VideoTrackVector video_tracks2 = s2->at(i)->GetVideoTracks();
487
488 if (audio_tracks1.size() != audio_tracks2.size()) {
489 return false;
490 }
491 for (size_t j = 0; j != audio_tracks1.size(); ++j) {
492 if (audio_tracks1[j]->id() != audio_tracks2[j]->id()) {
493 return false;
494 }
495 }
496 if (video_tracks1.size() != video_tracks2.size()) {
497 return false;
498 }
499 for (size_t j = 0; j != video_tracks1.size(); ++j) {
500 if (video_tracks1[j]->id() != video_tracks2[j]->id()) {
501 return false;
502 }
503 }
504 }
505 return true;
506}
507
perkjd61bf802016-03-24 03:16:19 -0700508// Helper class to test Observer.
509class MockTrackObserver : public ObserverInterface {
510 public:
511 explicit MockTrackObserver(NotifierInterface* notifier)
512 : notifier_(notifier) {
513 notifier_->RegisterObserver(this);
514 }
515
516 ~MockTrackObserver() { Unregister(); }
517
518 void Unregister() {
519 if (notifier_) {
520 notifier_->UnregisterObserver(this);
521 notifier_ = nullptr;
522 }
523 }
524
525 MOCK_METHOD0(OnChanged, void());
526
527 private:
528 NotifierInterface* notifier_;
529};
530
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000531class MockPeerConnectionObserver : public PeerConnectionObserver {
532 public:
deadbeefab9b2d12015-10-14 11:33:11 -0700533 MockPeerConnectionObserver() : remote_streams_(StreamCollection::Create()) {}
Henrik Kjellander3fe372d2016-05-12 08:10:52 +0200534 virtual ~MockPeerConnectionObserver() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000535 }
536 void SetPeerConnectionInterface(PeerConnectionInterface* pc) {
537 pc_ = pc;
538 if (pc) {
539 state_ = pc_->signaling_state();
540 }
541 }
nisseef8b61e2016-04-29 06:09:15 -0700542 void OnSignalingChange(
543 PeerConnectionInterface::SignalingState new_state) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000544 EXPECT_EQ(pc_->signaling_state(), new_state);
545 state_ = new_state;
546 }
deadbeefab9b2d12015-10-14 11:33:11 -0700547
548 MediaStreamInterface* RemoteStream(const std::string& label) {
549 return remote_streams_->find(label);
550 }
551 StreamCollectionInterface* remote_streams() const { return remote_streams_; }
Taylor Brandstetter98cde262016-05-31 13:02:21 -0700552 void OnAddStream(rtc::scoped_refptr<MediaStreamInterface> stream) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000553 last_added_stream_ = stream;
deadbeefab9b2d12015-10-14 11:33:11 -0700554 remote_streams_->AddStream(stream);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000555 }
Taylor Brandstetter98cde262016-05-31 13:02:21 -0700556 void OnRemoveStream(
557 rtc::scoped_refptr<MediaStreamInterface> stream) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000558 last_removed_stream_ = stream;
deadbeefab9b2d12015-10-14 11:33:11 -0700559 remote_streams_->RemoveStream(stream);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000560 }
perkjdfb769d2016-02-09 03:09:43 -0800561 void OnRenegotiationNeeded() override { renegotiation_needed_ = true; }
Taylor Brandstetter98cde262016-05-31 13:02:21 -0700562 void OnDataChannel(
563 rtc::scoped_refptr<DataChannelInterface> data_channel) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000564 last_datachannel_ = data_channel;
565 }
566
perkjdfb769d2016-02-09 03:09:43 -0800567 void OnIceConnectionChange(
568 PeerConnectionInterface::IceConnectionState new_state) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000569 EXPECT_EQ(pc_->ice_connection_state(), new_state);
zhihuang81c3a032016-11-17 12:06:24 -0800570 callback_triggered_ = true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000571 }
perkjdfb769d2016-02-09 03:09:43 -0800572 void OnIceGatheringChange(
573 PeerConnectionInterface::IceGatheringState new_state) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000574 EXPECT_EQ(pc_->ice_gathering_state(), new_state);
perkjdfb769d2016-02-09 03:09:43 -0800575 ice_complete_ = new_state == PeerConnectionInterface::kIceGatheringComplete;
zhihuang81c3a032016-11-17 12:06:24 -0800576 callback_triggered_ = true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000577 }
perkjdfb769d2016-02-09 03:09:43 -0800578 void OnIceCandidate(const webrtc::IceCandidateInterface* candidate) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000579 EXPECT_NE(PeerConnectionInterface::kIceGatheringNew,
580 pc_->ice_gathering_state());
581
582 std::string sdp;
583 EXPECT_TRUE(candidate->ToString(&sdp));
584 EXPECT_LT(0u, sdp.size());
585 last_candidate_.reset(webrtc::CreateIceCandidate(candidate->sdp_mid(),
586 candidate->sdp_mline_index(), sdp, NULL));
587 EXPECT_TRUE(last_candidate_.get() != NULL);
zhihuang81c3a032016-11-17 12:06:24 -0800588 callback_triggered_ = true;
zhihuang29ff8442016-07-27 11:07:25 -0700589 }
590
591 void OnIceCandidatesRemoved(
592 const std::vector<cricket::Candidate>& candidates) override {
zhihuang81c3a032016-11-17 12:06:24 -0800593 callback_triggered_ = true;
zhihuang29ff8442016-07-27 11:07:25 -0700594 }
595
596 void OnIceConnectionReceivingChange(bool receiving) override {
zhihuang81c3a032016-11-17 12:06:24 -0800597 callback_triggered_ = true;
598 }
599
zhihuangc63b8942016-12-02 15:41:10 -0800600 void OnAddTrack(
601 rtc::scoped_refptr<webrtc::RtpReceiverInterface> receiver,
602 const std::vector<rtc::scoped_refptr<webrtc::MediaStreamInterface>>&
603 streams) override {
zhihuang81c3a032016-11-17 12:06:24 -0800604 EXPECT_TRUE(receiver != nullptr);
605 num_added_tracks_++;
606 last_added_track_label_ = receiver->id();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000607 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000608
609 // Returns the label of the last added stream.
610 // Empty string if no stream have been added.
611 std::string GetLastAddedStreamLabel() {
612 if (last_added_stream_.get())
613 return last_added_stream_->label();
614 return "";
615 }
616 std::string GetLastRemovedStreamLabel() {
617 if (last_removed_stream_.get())
618 return last_removed_stream_->label();
619 return "";
620 }
621
zhihuang9763d562016-08-05 11:14:50 -0700622 rtc::scoped_refptr<PeerConnectionInterface> pc_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000623 PeerConnectionInterface::SignalingState state_;
kwibergd1fe2812016-04-27 06:47:29 -0700624 std::unique_ptr<IceCandidateInterface> last_candidate_;
zhihuang9763d562016-08-05 11:14:50 -0700625 rtc::scoped_refptr<DataChannelInterface> last_datachannel_;
deadbeefab9b2d12015-10-14 11:33:11 -0700626 rtc::scoped_refptr<StreamCollection> remote_streams_;
627 bool renegotiation_needed_ = false;
628 bool ice_complete_ = false;
zhihuang81c3a032016-11-17 12:06:24 -0800629 bool callback_triggered_ = false;
630 int num_added_tracks_ = 0;
631 std::string last_added_track_label_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000632
633 private:
zhihuang9763d562016-08-05 11:14:50 -0700634 rtc::scoped_refptr<MediaStreamInterface> last_added_stream_;
635 rtc::scoped_refptr<MediaStreamInterface> last_removed_stream_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000636};
637
638} // namespace
deadbeefab9b2d12015-10-14 11:33:11 -0700639
nisse528b7932017-05-08 03:21:43 -0700640// The PeerConnectionMediaConfig tests below verify that configuration and
641// constraints are propagated into the PeerConnection's MediaConfig. These
642// settings are intended for MediaChannel constructors, but that is not
643// exercised by these unittest.
zhihuang29ff8442016-07-27 11:07:25 -0700644class PeerConnectionFactoryForTest : public webrtc::PeerConnectionFactory {
645 public:
zhihuang38ede132017-06-15 12:52:32 -0700646 static rtc::scoped_refptr<PeerConnectionFactoryForTest>
647 CreatePeerConnectionFactoryForTest() {
648 auto audio_encoder_factory = webrtc::CreateBuiltinAudioEncoderFactory();
649 auto audio_decoder_factory = webrtc::CreateBuiltinAudioDecoderFactory();
650
651 auto media_engine = std::unique_ptr<cricket::MediaEngineInterface>(
652 cricket::WebRtcMediaEngineFactory::Create(
653 nullptr, audio_encoder_factory, audio_decoder_factory, nullptr,
peaha9cc40b2017-06-29 08:32:09 -0700654 nullptr, nullptr, webrtc::AudioProcessing::Create()));
zhihuang38ede132017-06-15 12:52:32 -0700655
656 std::unique_ptr<webrtc::CallFactoryInterface> call_factory =
657 webrtc::CreateCallFactory();
658
659 std::unique_ptr<webrtc::RtcEventLogFactoryInterface> event_log_factory =
660 webrtc::CreateRtcEventLogFactory();
661
662 return new rtc::RefCountedObject<PeerConnectionFactoryForTest>(
663 rtc::Thread::Current(), rtc::Thread::Current(), rtc::Thread::Current(),
664 nullptr, audio_encoder_factory, audio_decoder_factory, nullptr, nullptr,
665 nullptr, std::move(media_engine), std::move(call_factory),
666 std::move(event_log_factory));
667 }
668
669 PeerConnectionFactoryForTest(
670 rtc::Thread* network_thread,
671 rtc::Thread* worker_thread,
672 rtc::Thread* signaling_thread,
673 webrtc::AudioDeviceModule* default_adm,
674 rtc::scoped_refptr<webrtc::AudioEncoderFactory> audio_encoder_factory,
675 rtc::scoped_refptr<webrtc::AudioDecoderFactory> audio_decoder_factory,
676 cricket::WebRtcVideoEncoderFactory* video_encoder_factory,
677 cricket::WebRtcVideoDecoderFactory* video_decoder_factory,
678 rtc::scoped_refptr<webrtc::AudioMixer> audio_mixer,
679 std::unique_ptr<cricket::MediaEngineInterface> media_engine,
680 std::unique_ptr<webrtc::CallFactoryInterface> call_factory,
681 std::unique_ptr<webrtc::RtcEventLogFactoryInterface> event_log_factory)
682 : webrtc::PeerConnectionFactory(network_thread,
683 worker_thread,
684 signaling_thread,
685 default_adm,
686 audio_encoder_factory,
687 audio_decoder_factory,
688 video_encoder_factory,
689 video_decoder_factory,
690 audio_mixer,
691 std::move(media_engine),
692 std::move(call_factory),
693 std::move(event_log_factory)) {}
kwiberg1e4e8cb2017-01-31 01:48:08 -0800694
zhihuang29ff8442016-07-27 11:07:25 -0700695 cricket::TransportController* CreateTransportController(
Honghai Zhangbfd398c2016-08-30 22:07:42 -0700696 cricket::PortAllocator* port_allocator,
697 bool redetermine_role_on_ice_restart) override {
zhihuang29ff8442016-07-27 11:07:25 -0700698 transport_controller = new cricket::TransportController(
Honghai Zhangbfd398c2016-08-30 22:07:42 -0700699 rtc::Thread::Current(), rtc::Thread::Current(), port_allocator,
deadbeef7914b8c2017-04-21 03:23:33 -0700700 redetermine_role_on_ice_restart, rtc::CryptoOptions());
zhihuang29ff8442016-07-27 11:07:25 -0700701 return transport_controller;
702 }
703
704 cricket::TransportController* transport_controller;
zhihuang29ff8442016-07-27 11:07:25 -0700705};
706
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000707class PeerConnectionInterfaceTest : public testing::Test {
708 protected:
deadbeef9a6f4d42017-05-15 19:43:33 -0700709 PeerConnectionInterfaceTest()
deadbeef98e186c2017-05-16 18:00:06 -0700710 : vss_(new rtc::VirtualSocketServer()), main_(vss_.get()) {
phoglund37ebcf02016-01-08 05:04:57 -0800711#ifdef WEBRTC_ANDROID
712 webrtc::InitializeAndroidObjects();
713#endif
714 }
715
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000716 virtual void SetUp() {
717 pc_factory_ = webrtc::CreatePeerConnectionFactory(
danilchape9021a32016-05-17 01:52:02 -0700718 rtc::Thread::Current(), rtc::Thread::Current(), rtc::Thread::Current(),
719 nullptr, nullptr, nullptr);
720 ASSERT_TRUE(pc_factory_);
zhihuang29ff8442016-07-27 11:07:25 -0700721 pc_factory_for_test_ =
zhihuang38ede132017-06-15 12:52:32 -0700722 PeerConnectionFactoryForTest::CreatePeerConnectionFactoryForTest();
zhihuang29ff8442016-07-27 11:07:25 -0700723 pc_factory_for_test_->Initialize();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000724 }
725
726 void CreatePeerConnection() {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700727 CreatePeerConnection(PeerConnectionInterface::RTCConfiguration(), nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000728 }
729
deadbeef293e9262017-01-11 12:28:30 -0800730 // DTLS does not work in a loopback call, so is disabled for most of the
731 // tests in this file.
732 void CreatePeerConnectionWithoutDtls() {
733 FakeConstraints no_dtls_constraints;
734 no_dtls_constraints.AddMandatory(
735 webrtc::MediaConstraintsInterface::kEnableDtlsSrtp, false);
736
737 CreatePeerConnection(PeerConnectionInterface::RTCConfiguration(),
738 &no_dtls_constraints);
739 }
740
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000741 void CreatePeerConnection(webrtc::MediaConstraintsInterface* constraints) {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700742 CreatePeerConnection(PeerConnectionInterface::RTCConfiguration(),
743 constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000744 }
745
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700746 void CreatePeerConnectionWithIceTransportsType(
747 PeerConnectionInterface::IceTransportsType type) {
748 PeerConnectionInterface::RTCConfiguration config;
749 config.type = type;
750 return CreatePeerConnection(config, nullptr);
751 }
752
753 void CreatePeerConnectionWithIceServer(const std::string& uri,
754 const std::string& password) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800755 PeerConnectionInterface::RTCConfiguration config;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000756 PeerConnectionInterface::IceServer server;
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700757 server.uri = uri;
758 server.password = password;
759 config.servers.push_back(server);
760 CreatePeerConnection(config, nullptr);
761 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000762
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700763 void CreatePeerConnection(PeerConnectionInterface::RTCConfiguration config,
764 webrtc::MediaConstraintsInterface* constraints) {
kwibergd1fe2812016-04-27 06:47:29 -0700765 std::unique_ptr<cricket::FakePortAllocator> port_allocator(
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800766 new cricket::FakePortAllocator(rtc::Thread::Current(), nullptr));
767 port_allocator_ = port_allocator.get();
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000768
deadbeef1dcb1642017-03-29 21:08:16 -0700769 // Create certificate generator unless DTLS constraint is explicitly set to
770 // false.
Henrik Boströmd79599d2016-06-01 13:58:50 +0200771 std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator;
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000772 bool dtls;
773 if (FindConstraint(constraints,
774 webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
775 &dtls,
Henrik Boström5e56c592015-08-11 10:33:13 +0200776 nullptr) && dtls) {
deadbeef8662f942017-01-20 21:20:51 -0800777 fake_certificate_generator_ = new FakeRTCCertificateGenerator();
778 cert_generator.reset(fake_certificate_generator_);
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000779 }
Henrik Boströmd79599d2016-06-01 13:58:50 +0200780 pc_ = pc_factory_->CreatePeerConnection(
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800781 config, constraints, std::move(port_allocator),
Henrik Boströmd79599d2016-06-01 13:58:50 +0200782 std::move(cert_generator), &observer_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000783 ASSERT_TRUE(pc_.get() != NULL);
784 observer_.SetPeerConnectionInterface(pc_.get());
785 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
786 }
787
deadbeef0a6c4ca2015-10-06 11:38:28 -0700788 void CreatePeerConnectionExpectFail(const std::string& uri) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800789 PeerConnectionInterface::RTCConfiguration config;
deadbeef0a6c4ca2015-10-06 11:38:28 -0700790 PeerConnectionInterface::IceServer server;
deadbeef0a6c4ca2015-10-06 11:38:28 -0700791 server.uri = uri;
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800792 config.servers.push_back(server);
deadbeef0a6c4ca2015-10-06 11:38:28 -0700793
zhihuang9763d562016-08-05 11:14:50 -0700794 rtc::scoped_refptr<PeerConnectionInterface> pc;
hbosd7973cc2016-05-27 06:08:53 -0700795 pc = pc_factory_->CreatePeerConnection(config, nullptr, nullptr, nullptr,
796 &observer_);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800797 EXPECT_EQ(nullptr, pc);
deadbeef0a6c4ca2015-10-06 11:38:28 -0700798 }
799
Steve Anton038834f2017-07-14 15:59:59 -0700800 void CreatePeerConnectionExpectFail(
801 PeerConnectionInterface::RTCConfiguration config) {
802 PeerConnectionInterface::IceServer server;
803 server.uri = kTurnIceServerUri;
804 server.password = kTurnPassword;
805 config.servers.push_back(server);
806 rtc::scoped_refptr<PeerConnectionInterface> pc =
807 pc_factory_->CreatePeerConnection(config, nullptr, nullptr, &observer_);
808 EXPECT_EQ(nullptr, pc);
809 }
810
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000811 void CreatePeerConnectionWithDifferentConfigurations() {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700812 CreatePeerConnectionWithIceServer(kStunAddressOnly, "");
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800813 EXPECT_EQ(1u, port_allocator_->stun_servers().size());
814 EXPECT_EQ(0u, port_allocator_->turn_servers().size());
815 EXPECT_EQ("address", port_allocator_->stun_servers().begin()->hostname());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000816 EXPECT_EQ(kDefaultStunPort,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800817 port_allocator_->stun_servers().begin()->port());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000818
deadbeef0a6c4ca2015-10-06 11:38:28 -0700819 CreatePeerConnectionExpectFail(kStunInvalidPort);
820 CreatePeerConnectionExpectFail(kStunAddressPortAndMore1);
821 CreatePeerConnectionExpectFail(kStunAddressPortAndMore2);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000822
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700823 CreatePeerConnectionWithIceServer(kTurnIceServerUri, kTurnPassword);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800824 EXPECT_EQ(0u, port_allocator_->stun_servers().size());
825 EXPECT_EQ(1u, port_allocator_->turn_servers().size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000826 EXPECT_EQ(kTurnUsername,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800827 port_allocator_->turn_servers()[0].credentials.username);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000828 EXPECT_EQ(kTurnPassword,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800829 port_allocator_->turn_servers()[0].credentials.password);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000830 EXPECT_EQ(kTurnHostname,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800831 port_allocator_->turn_servers()[0].ports[0].address.hostname());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000832 }
833
834 void ReleasePeerConnection() {
835 pc_ = NULL;
836 observer_.SetPeerConnectionInterface(NULL);
837 }
838
deadbeefab9b2d12015-10-14 11:33:11 -0700839 void AddVideoStream(const std::string& label) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000840 // Create a local stream.
zhihuang9763d562016-08-05 11:14:50 -0700841 rtc::scoped_refptr<MediaStreamInterface> stream(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000842 pc_factory_->CreateLocalMediaStream(label));
zhihuang9763d562016-08-05 11:14:50 -0700843 rtc::scoped_refptr<VideoTrackSourceInterface> video_source(
deadbeef112b2e92017-02-10 20:13:37 -0800844 pc_factory_->CreateVideoSource(std::unique_ptr<cricket::VideoCapturer>(
845 new cricket::FakeVideoCapturer()),
846 NULL));
zhihuang9763d562016-08-05 11:14:50 -0700847 rtc::scoped_refptr<VideoTrackInterface> video_track(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000848 pc_factory_->CreateVideoTrack(label + "v0", video_source));
849 stream->AddTrack(video_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +0000850 EXPECT_TRUE(pc_->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000851 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
852 observer_.renegotiation_needed_ = false;
853 }
854
855 void AddVoiceStream(const std::string& label) {
856 // Create a local stream.
zhihuang9763d562016-08-05 11:14:50 -0700857 rtc::scoped_refptr<MediaStreamInterface> stream(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000858 pc_factory_->CreateLocalMediaStream(label));
zhihuang9763d562016-08-05 11:14:50 -0700859 rtc::scoped_refptr<AudioTrackInterface> audio_track(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000860 pc_factory_->CreateAudioTrack(label + "a0", NULL));
861 stream->AddTrack(audio_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +0000862 EXPECT_TRUE(pc_->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000863 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
864 observer_.renegotiation_needed_ = false;
865 }
866
867 void AddAudioVideoStream(const std::string& stream_label,
868 const std::string& audio_track_label,
869 const std::string& video_track_label) {
870 // Create a local stream.
zhihuang9763d562016-08-05 11:14:50 -0700871 rtc::scoped_refptr<MediaStreamInterface> stream(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000872 pc_factory_->CreateLocalMediaStream(stream_label));
zhihuang9763d562016-08-05 11:14:50 -0700873 rtc::scoped_refptr<AudioTrackInterface> audio_track(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000874 pc_factory_->CreateAudioTrack(
875 audio_track_label, static_cast<AudioSourceInterface*>(NULL)));
876 stream->AddTrack(audio_track.get());
zhihuang9763d562016-08-05 11:14:50 -0700877 rtc::scoped_refptr<VideoTrackInterface> video_track(
nisseaf510af2016-03-21 08:20:42 -0700878 pc_factory_->CreateVideoTrack(
deadbeef112b2e92017-02-10 20:13:37 -0800879 video_track_label, pc_factory_->CreateVideoSource(
880 std::unique_ptr<cricket::VideoCapturer>(
881 new cricket::FakeVideoCapturer()))));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000882 stream->AddTrack(video_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +0000883 EXPECT_TRUE(pc_->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000884 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
885 observer_.renegotiation_needed_ = false;
886 }
887
kwibergd1fe2812016-04-27 06:47:29 -0700888 bool DoCreateOfferAnswer(std::unique_ptr<SessionDescriptionInterface>* desc,
deadbeefc80741f2015-10-22 13:14:45 -0700889 bool offer,
890 MediaConstraintsInterface* constraints) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000891 rtc::scoped_refptr<MockCreateSessionDescriptionObserver>
892 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000893 MockCreateSessionDescriptionObserver>());
894 if (offer) {
deadbeefc80741f2015-10-22 13:14:45 -0700895 pc_->CreateOffer(observer, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000896 } else {
deadbeefc80741f2015-10-22 13:14:45 -0700897 pc_->CreateAnswer(observer, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000898 }
899 EXPECT_EQ_WAIT(true, observer->called(), kTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -0700900 *desc = observer->MoveDescription();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000901 return observer->result();
902 }
903
kwibergd1fe2812016-04-27 06:47:29 -0700904 bool DoCreateOffer(std::unique_ptr<SessionDescriptionInterface>* desc,
deadbeefc80741f2015-10-22 13:14:45 -0700905 MediaConstraintsInterface* constraints) {
906 return DoCreateOfferAnswer(desc, true, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000907 }
908
kwibergd1fe2812016-04-27 06:47:29 -0700909 bool DoCreateAnswer(std::unique_ptr<SessionDescriptionInterface>* desc,
deadbeefc80741f2015-10-22 13:14:45 -0700910 MediaConstraintsInterface* constraints) {
911 return DoCreateOfferAnswer(desc, false, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000912 }
913
914 bool DoSetSessionDescription(SessionDescriptionInterface* desc, bool local) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000915 rtc::scoped_refptr<MockSetSessionDescriptionObserver>
916 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000917 MockSetSessionDescriptionObserver>());
918 if (local) {
919 pc_->SetLocalDescription(observer, desc);
920 } else {
921 pc_->SetRemoteDescription(observer, desc);
922 }
zhihuang29ff8442016-07-27 11:07:25 -0700923 if (pc_->signaling_state() != PeerConnectionInterface::kClosed) {
924 EXPECT_EQ_WAIT(true, observer->called(), kTimeout);
925 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000926 return observer->result();
927 }
928
929 bool DoSetLocalDescription(SessionDescriptionInterface* desc) {
930 return DoSetSessionDescription(desc, true);
931 }
932
933 bool DoSetRemoteDescription(SessionDescriptionInterface* desc) {
934 return DoSetSessionDescription(desc, false);
935 }
936
937 // Calls PeerConnection::GetStats and check the return value.
938 // It does not verify the values in the StatReports since a RTCP packet might
939 // be required.
940 bool DoGetStats(MediaStreamTrackInterface* track) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000941 rtc::scoped_refptr<MockStatsObserver> observer(
942 new rtc::RefCountedObject<MockStatsObserver>());
jiayl@webrtc.orgdb41b4d2014-03-03 21:30:06 +0000943 if (!pc_->GetStats(
944 observer, track, PeerConnectionInterface::kStatsOutputLevelStandard))
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000945 return false;
946 EXPECT_TRUE_WAIT(observer->called(), kTimeout);
947 return observer->called();
948 }
949
950 void InitiateCall() {
deadbeef293e9262017-01-11 12:28:30 -0800951 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000952 // Create a local stream with audio&video tracks.
953 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
954 CreateOfferReceiveAnswer();
955 }
956
957 // Verify that RTP Header extensions has been negotiated for audio and video.
958 void VerifyRemoteRtpHeaderExtensions() {
959 const cricket::MediaContentDescription* desc =
960 cricket::GetFirstAudioContentDescription(
961 pc_->remote_description()->description());
962 ASSERT_TRUE(desc != NULL);
963 EXPECT_GT(desc->rtp_header_extensions().size(), 0u);
964
965 desc = cricket::GetFirstVideoContentDescription(
966 pc_->remote_description()->description());
967 ASSERT_TRUE(desc != NULL);
968 EXPECT_GT(desc->rtp_header_extensions().size(), 0u);
969 }
970
971 void CreateOfferAsRemoteDescription() {
kwibergd1fe2812016-04-27 06:47:29 -0700972 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -0700973 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000974 std::string sdp;
975 EXPECT_TRUE(offer->ToString(&sdp));
976 SessionDescriptionInterface* remote_offer =
977 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
978 sdp, NULL);
979 EXPECT_TRUE(DoSetRemoteDescription(remote_offer));
980 EXPECT_EQ(PeerConnectionInterface::kHaveRemoteOffer, observer_.state_);
981 }
982
deadbeefab9b2d12015-10-14 11:33:11 -0700983 void CreateAndSetRemoteOffer(const std::string& sdp) {
984 SessionDescriptionInterface* remote_offer =
985 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
986 sdp, nullptr);
987 EXPECT_TRUE(DoSetRemoteDescription(remote_offer));
988 EXPECT_EQ(PeerConnectionInterface::kHaveRemoteOffer, observer_.state_);
989 }
990
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000991 void CreateAnswerAsLocalDescription() {
kwibergd1fe2812016-04-27 06:47:29 -0700992 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -0700993 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000994
995 // TODO(perkj): Currently SetLocalDescription fails if any parameters in an
996 // audio codec change, even if the parameter has nothing to do with
997 // receiving. Not all parameters are serialized to SDP.
998 // Since CreatePrAnswerAsLocalDescription serialize/deserialize
999 // the SessionDescription, it is necessary to do that here to in order to
1000 // get ReceiveOfferCreatePrAnswerAndAnswer and RenegotiateAudioOnly to pass.
1001 // https://code.google.com/p/webrtc/issues/detail?id=1356
1002 std::string sdp;
1003 EXPECT_TRUE(answer->ToString(&sdp));
1004 SessionDescriptionInterface* new_answer =
1005 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
1006 sdp, NULL);
1007 EXPECT_TRUE(DoSetLocalDescription(new_answer));
1008 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
1009 }
1010
1011 void CreatePrAnswerAsLocalDescription() {
kwibergd1fe2812016-04-27 06:47:29 -07001012 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -07001013 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001014
1015 std::string sdp;
1016 EXPECT_TRUE(answer->ToString(&sdp));
1017 SessionDescriptionInterface* pr_answer =
1018 webrtc::CreateSessionDescription(SessionDescriptionInterface::kPrAnswer,
1019 sdp, NULL);
1020 EXPECT_TRUE(DoSetLocalDescription(pr_answer));
1021 EXPECT_EQ(PeerConnectionInterface::kHaveLocalPrAnswer, observer_.state_);
1022 }
1023
1024 void CreateOfferReceiveAnswer() {
1025 CreateOfferAsLocalDescription();
1026 std::string sdp;
1027 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
1028 CreateAnswerAsRemoteDescription(sdp);
1029 }
1030
1031 void CreateOfferAsLocalDescription() {
kwibergd1fe2812016-04-27 06:47:29 -07001032 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001033 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001034 // TODO(perkj): Currently SetLocalDescription fails if any parameters in an
1035 // audio codec change, even if the parameter has nothing to do with
1036 // receiving. Not all parameters are serialized to SDP.
1037 // Since CreatePrAnswerAsLocalDescription serialize/deserialize
1038 // the SessionDescription, it is necessary to do that here to in order to
1039 // get ReceiveOfferCreatePrAnswerAndAnswer and RenegotiateAudioOnly to pass.
1040 // https://code.google.com/p/webrtc/issues/detail?id=1356
1041 std::string sdp;
1042 EXPECT_TRUE(offer->ToString(&sdp));
1043 SessionDescriptionInterface* new_offer =
1044 webrtc::CreateSessionDescription(
1045 SessionDescriptionInterface::kOffer,
1046 sdp, NULL);
1047
1048 EXPECT_TRUE(DoSetLocalDescription(new_offer));
1049 EXPECT_EQ(PeerConnectionInterface::kHaveLocalOffer, observer_.state_);
mallinath@webrtc.org68cbd012014-01-22 00:16:46 +00001050 // Wait for the ice_complete message, so that SDP will have candidates.
1051 EXPECT_TRUE_WAIT(observer_.ice_complete_, kTimeout);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001052 }
1053
deadbeefab9b2d12015-10-14 11:33:11 -07001054 void CreateAnswerAsRemoteDescription(const std::string& sdp) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001055 webrtc::JsepSessionDescription* answer = new webrtc::JsepSessionDescription(
1056 SessionDescriptionInterface::kAnswer);
deadbeefab9b2d12015-10-14 11:33:11 -07001057 EXPECT_TRUE(answer->Initialize(sdp, NULL));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001058 EXPECT_TRUE(DoSetRemoteDescription(answer));
1059 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
1060 }
1061
deadbeefab9b2d12015-10-14 11:33:11 -07001062 void CreatePrAnswerAndAnswerAsRemoteDescription(const std::string& sdp) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001063 webrtc::JsepSessionDescription* pr_answer =
1064 new webrtc::JsepSessionDescription(
1065 SessionDescriptionInterface::kPrAnswer);
deadbeefab9b2d12015-10-14 11:33:11 -07001066 EXPECT_TRUE(pr_answer->Initialize(sdp, NULL));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001067 EXPECT_TRUE(DoSetRemoteDescription(pr_answer));
1068 EXPECT_EQ(PeerConnectionInterface::kHaveRemotePrAnswer, observer_.state_);
1069 webrtc::JsepSessionDescription* answer =
1070 new webrtc::JsepSessionDescription(
1071 SessionDescriptionInterface::kAnswer);
deadbeefab9b2d12015-10-14 11:33:11 -07001072 EXPECT_TRUE(answer->Initialize(sdp, NULL));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001073 EXPECT_TRUE(DoSetRemoteDescription(answer));
1074 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
1075 }
1076
1077 // Help function used for waiting until a the last signaled remote stream has
1078 // the same label as |stream_label|. In a few of the tests in this file we
1079 // answer with the same session description as we offer and thus we can
1080 // check if OnAddStream have been called with the same stream as we offer to
1081 // send.
1082 void WaitAndVerifyOnAddStream(const std::string& stream_label) {
1083 EXPECT_EQ_WAIT(stream_label, observer_.GetLastAddedStreamLabel(), kTimeout);
1084 }
1085
1086 // Creates an offer and applies it as a local session description.
1087 // Creates an answer with the same SDP an the offer but removes all lines
1088 // that start with a:ssrc"
1089 void CreateOfferReceiveAnswerWithoutSsrc() {
1090 CreateOfferAsLocalDescription();
1091 std::string sdp;
1092 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
1093 SetSsrcToZero(&sdp);
1094 CreateAnswerAsRemoteDescription(sdp);
1095 }
1096
deadbeefab9b2d12015-10-14 11:33:11 -07001097 // This function creates a MediaStream with label kStreams[0] and
1098 // |number_of_audio_tracks| and |number_of_video_tracks| tracks and the
1099 // corresponding SessionDescriptionInterface. The SessionDescriptionInterface
kwiberg2bbff992016-03-16 11:03:04 -07001100 // is returned and the MediaStream is stored in
deadbeefab9b2d12015-10-14 11:33:11 -07001101 // |reference_collection_|
kwibergd1fe2812016-04-27 06:47:29 -07001102 std::unique_ptr<SessionDescriptionInterface>
kwiberg2bbff992016-03-16 11:03:04 -07001103 CreateSessionDescriptionAndReference(size_t number_of_audio_tracks,
1104 size_t number_of_video_tracks) {
1105 EXPECT_LE(number_of_audio_tracks, 2u);
1106 EXPECT_LE(number_of_video_tracks, 2u);
deadbeefab9b2d12015-10-14 11:33:11 -07001107
1108 reference_collection_ = StreamCollection::Create();
1109 std::string sdp_ms1 = std::string(kSdpStringInit);
1110
1111 std::string mediastream_label = kStreams[0];
1112
1113 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream(
1114 webrtc::MediaStream::Create(mediastream_label));
1115 reference_collection_->AddStream(stream);
1116
1117 if (number_of_audio_tracks > 0) {
1118 sdp_ms1 += std::string(kSdpStringAudio);
1119 sdp_ms1 += std::string(kSdpStringMs1Audio0);
1120 AddAudioTrack(kAudioTracks[0], stream);
1121 }
1122 if (number_of_audio_tracks > 1) {
1123 sdp_ms1 += kSdpStringMs1Audio1;
1124 AddAudioTrack(kAudioTracks[1], stream);
1125 }
1126
1127 if (number_of_video_tracks > 0) {
1128 sdp_ms1 += std::string(kSdpStringVideo);
1129 sdp_ms1 += std::string(kSdpStringMs1Video0);
1130 AddVideoTrack(kVideoTracks[0], stream);
1131 }
1132 if (number_of_video_tracks > 1) {
1133 sdp_ms1 += kSdpStringMs1Video1;
1134 AddVideoTrack(kVideoTracks[1], stream);
1135 }
1136
kwibergd1fe2812016-04-27 06:47:29 -07001137 return std::unique_ptr<SessionDescriptionInterface>(
kwiberg2bbff992016-03-16 11:03:04 -07001138 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
1139 sdp_ms1, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07001140 }
1141
1142 void AddAudioTrack(const std::string& track_id,
1143 MediaStreamInterface* stream) {
1144 rtc::scoped_refptr<webrtc::AudioTrackInterface> audio_track(
1145 webrtc::AudioTrack::Create(track_id, nullptr));
1146 ASSERT_TRUE(stream->AddTrack(audio_track));
1147 }
1148
1149 void AddVideoTrack(const std::string& track_id,
1150 MediaStreamInterface* stream) {
1151 rtc::scoped_refptr<webrtc::VideoTrackInterface> video_track(
nisseaf510af2016-03-21 08:20:42 -07001152 webrtc::VideoTrack::Create(track_id,
mbonadei539d1042017-07-10 02:40:49 -07001153 webrtc::FakeVideoTrackSource::Create()));
deadbeefab9b2d12015-10-14 11:33:11 -07001154 ASSERT_TRUE(stream->AddTrack(video_track));
1155 }
1156
kwibergfd8be342016-05-14 19:44:11 -07001157 std::unique_ptr<SessionDescriptionInterface> CreateOfferWithOneAudioStream() {
deadbeef293e9262017-01-11 12:28:30 -08001158 CreatePeerConnectionWithoutDtls();
zhihuang8f65cdf2016-05-06 18:40:30 -07001159 AddVoiceStream(kStreamLabel1);
kwibergfd8be342016-05-14 19:44:11 -07001160 std::unique_ptr<SessionDescriptionInterface> offer;
zhihuang8f65cdf2016-05-06 18:40:30 -07001161 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
1162 return offer;
1163 }
1164
kwibergfd8be342016-05-14 19:44:11 -07001165 std::unique_ptr<SessionDescriptionInterface>
zhihuang8f65cdf2016-05-06 18:40:30 -07001166 CreateAnswerWithOneAudioStream() {
kwibergfd8be342016-05-14 19:44:11 -07001167 std::unique_ptr<SessionDescriptionInterface> offer =
zhihuang8f65cdf2016-05-06 18:40:30 -07001168 CreateOfferWithOneAudioStream();
1169 EXPECT_TRUE(DoSetRemoteDescription(offer.release()));
kwibergfd8be342016-05-14 19:44:11 -07001170 std::unique_ptr<SessionDescriptionInterface> answer;
zhihuang8f65cdf2016-05-06 18:40:30 -07001171 EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
1172 return answer;
1173 }
1174
1175 const std::string& GetFirstAudioStreamCname(
1176 const SessionDescriptionInterface* desc) {
1177 const cricket::ContentInfo* audio_content =
1178 cricket::GetFirstAudioContent(desc->description());
1179 const cricket::AudioContentDescription* audio_desc =
1180 static_cast<const cricket::AudioContentDescription*>(
1181 audio_content->description);
1182 return audio_desc->streams()[0].cname;
1183 }
1184
deadbeef9a6f4d42017-05-15 19:43:33 -07001185 std::unique_ptr<rtc::VirtualSocketServer> vss_;
1186 rtc::AutoSocketServerThread main_;
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08001187 cricket::FakePortAllocator* port_allocator_ = nullptr;
deadbeef8662f942017-01-20 21:20:51 -08001188 FakeRTCCertificateGenerator* fake_certificate_generator_ = nullptr;
zhihuang9763d562016-08-05 11:14:50 -07001189 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface> pc_factory_;
1190 rtc::scoped_refptr<PeerConnectionFactoryForTest> pc_factory_for_test_;
1191 rtc::scoped_refptr<PeerConnectionInterface> pc_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001192 MockPeerConnectionObserver observer_;
deadbeefab9b2d12015-10-14 11:33:11 -07001193 rtc::scoped_refptr<StreamCollection> reference_collection_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001194};
1195
zhihuang29ff8442016-07-27 11:07:25 -07001196// Test that no callbacks on the PeerConnectionObserver are called after the
1197// PeerConnection is closed.
1198TEST_F(PeerConnectionInterfaceTest, CloseAndTestCallbackFunctions) {
zhihuang9763d562016-08-05 11:14:50 -07001199 rtc::scoped_refptr<PeerConnectionInterface> pc(
zhihuang29ff8442016-07-27 11:07:25 -07001200 pc_factory_for_test_->CreatePeerConnection(
1201 PeerConnectionInterface::RTCConfiguration(), nullptr, nullptr,
1202 nullptr, &observer_));
1203 observer_.SetPeerConnectionInterface(pc.get());
1204 pc->Close();
1205
1206 // No callbacks is expected to be called.
zhihuang81c3a032016-11-17 12:06:24 -08001207 observer_.callback_triggered_ = false;
zhihuang29ff8442016-07-27 11:07:25 -07001208 std::vector<cricket::Candidate> candidates;
1209 pc_factory_for_test_->transport_controller->SignalGatheringState(
1210 cricket::IceGatheringState{});
1211 pc_factory_for_test_->transport_controller->SignalCandidatesGathered(
1212 "", candidates);
1213 pc_factory_for_test_->transport_controller->SignalConnectionState(
1214 cricket::IceConnectionState{});
1215 pc_factory_for_test_->transport_controller->SignalCandidatesRemoved(
1216 candidates);
1217 pc_factory_for_test_->transport_controller->SignalReceiving(false);
zhihuang81c3a032016-11-17 12:06:24 -08001218 EXPECT_FALSE(observer_.callback_triggered_);
zhihuang29ff8442016-07-27 11:07:25 -07001219}
1220
zhihuang8f65cdf2016-05-06 18:40:30 -07001221// Generate different CNAMEs when PeerConnections are created.
1222// The CNAMEs are expected to be generated randomly. It is possible
1223// that the test fails, though the possibility is very low.
1224TEST_F(PeerConnectionInterfaceTest, CnameGenerationInOffer) {
kwibergfd8be342016-05-14 19:44:11 -07001225 std::unique_ptr<SessionDescriptionInterface> offer1 =
zhihuang8f65cdf2016-05-06 18:40:30 -07001226 CreateOfferWithOneAudioStream();
kwibergfd8be342016-05-14 19:44:11 -07001227 std::unique_ptr<SessionDescriptionInterface> offer2 =
zhihuang8f65cdf2016-05-06 18:40:30 -07001228 CreateOfferWithOneAudioStream();
1229 EXPECT_NE(GetFirstAudioStreamCname(offer1.get()),
1230 GetFirstAudioStreamCname(offer2.get()));
1231}
1232
1233TEST_F(PeerConnectionInterfaceTest, CnameGenerationInAnswer) {
kwibergfd8be342016-05-14 19:44:11 -07001234 std::unique_ptr<SessionDescriptionInterface> answer1 =
zhihuang8f65cdf2016-05-06 18:40:30 -07001235 CreateAnswerWithOneAudioStream();
kwibergfd8be342016-05-14 19:44:11 -07001236 std::unique_ptr<SessionDescriptionInterface> answer2 =
zhihuang8f65cdf2016-05-06 18:40:30 -07001237 CreateAnswerWithOneAudioStream();
1238 EXPECT_NE(GetFirstAudioStreamCname(answer1.get()),
1239 GetFirstAudioStreamCname(answer2.get()));
1240}
1241
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001242TEST_F(PeerConnectionInterfaceTest,
1243 CreatePeerConnectionWithDifferentConfigurations) {
1244 CreatePeerConnectionWithDifferentConfigurations();
1245}
1246
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001247TEST_F(PeerConnectionInterfaceTest,
1248 CreatePeerConnectionWithDifferentIceTransportsTypes) {
1249 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kNone);
1250 EXPECT_EQ(cricket::CF_NONE, port_allocator_->candidate_filter());
1251 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kRelay);
1252 EXPECT_EQ(cricket::CF_RELAY, port_allocator_->candidate_filter());
1253 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kNoHost);
1254 EXPECT_EQ(cricket::CF_ALL & ~cricket::CF_HOST,
1255 port_allocator_->candidate_filter());
1256 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kAll);
1257 EXPECT_EQ(cricket::CF_ALL, port_allocator_->candidate_filter());
1258}
1259
1260// Test that when a PeerConnection is created with a nonzero candidate pool
1261// size, the pooled PortAllocatorSession is created with all the attributes
1262// in the RTCConfiguration.
1263TEST_F(PeerConnectionInterfaceTest, CreatePeerConnectionWithPooledCandidates) {
1264 PeerConnectionInterface::RTCConfiguration config;
1265 PeerConnectionInterface::IceServer server;
1266 server.uri = kStunAddressOnly;
1267 config.servers.push_back(server);
1268 config.type = PeerConnectionInterface::kRelay;
1269 config.disable_ipv6 = true;
1270 config.tcp_candidate_policy =
1271 PeerConnectionInterface::kTcpCandidatePolicyDisabled;
honghaiz60347052016-05-31 18:29:12 -07001272 config.candidate_network_policy =
1273 PeerConnectionInterface::kCandidateNetworkPolicyLowCost;
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001274 config.ice_candidate_pool_size = 1;
1275 CreatePeerConnection(config, nullptr);
1276
1277 const cricket::FakePortAllocatorSession* session =
1278 static_cast<const cricket::FakePortAllocatorSession*>(
1279 port_allocator_->GetPooledSession());
1280 ASSERT_NE(nullptr, session);
1281 EXPECT_EQ(1UL, session->stun_servers().size());
1282 EXPECT_EQ(0U, session->flags() & cricket::PORTALLOCATOR_ENABLE_IPV6);
1283 EXPECT_LT(0U, session->flags() & cricket::PORTALLOCATOR_DISABLE_TCP);
honghaiz60347052016-05-31 18:29:12 -07001284 EXPECT_LT(0U,
1285 session->flags() & cricket::PORTALLOCATOR_DISABLE_COSTLY_NETWORKS);
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001286}
1287
Taylor Brandstetterf8e65772016-06-27 17:20:15 -07001288// Test that the PeerConnection initializes the port allocator passed into it,
1289// and on the correct thread.
1290TEST_F(PeerConnectionInterfaceTest,
1291 CreatePeerConnectionInitializesPortAllocator) {
tommie7251592017-07-14 14:44:46 -07001292 std::unique_ptr<rtc::Thread> network_thread(
1293 rtc::Thread::CreateWithSocketServer());
1294 network_thread->Start();
Taylor Brandstetterf8e65772016-06-27 17:20:15 -07001295 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface> pc_factory(
1296 webrtc::CreatePeerConnectionFactory(
tommie7251592017-07-14 14:44:46 -07001297 network_thread.get(), rtc::Thread::Current(), rtc::Thread::Current(),
Taylor Brandstetterf8e65772016-06-27 17:20:15 -07001298 nullptr, nullptr, nullptr));
1299 std::unique_ptr<cricket::FakePortAllocator> port_allocator(
tommie7251592017-07-14 14:44:46 -07001300 new cricket::FakePortAllocator(network_thread.get(), nullptr));
Taylor Brandstetterf8e65772016-06-27 17:20:15 -07001301 cricket::FakePortAllocator* raw_port_allocator = port_allocator.get();
1302 PeerConnectionInterface::RTCConfiguration config;
1303 rtc::scoped_refptr<PeerConnectionInterface> pc(
1304 pc_factory->CreatePeerConnection(
1305 config, nullptr, std::move(port_allocator), nullptr, &observer_));
1306 // FakePortAllocator RTC_CHECKs that it's initialized on the right thread,
1307 // so all we have to do here is check that it's initialized.
1308 EXPECT_TRUE(raw_port_allocator->initialized());
1309}
1310
deadbeef46c73892016-11-16 19:42:04 -08001311// Check that GetConfiguration returns the configuration the PeerConnection was
1312// constructed with, before SetConfiguration is called.
1313TEST_F(PeerConnectionInterfaceTest, GetConfigurationAfterCreatePeerConnection) {
1314 PeerConnectionInterface::RTCConfiguration config;
1315 config.type = PeerConnectionInterface::kRelay;
1316 CreatePeerConnection(config, nullptr);
1317
1318 PeerConnectionInterface::RTCConfiguration returned_config =
1319 pc_->GetConfiguration();
1320 EXPECT_EQ(PeerConnectionInterface::kRelay, returned_config.type);
1321}
1322
1323// Check that GetConfiguration returns the last configuration passed into
1324// SetConfiguration.
1325TEST_F(PeerConnectionInterfaceTest, GetConfigurationAfterSetConfiguration) {
1326 CreatePeerConnection();
1327
1328 PeerConnectionInterface::RTCConfiguration config;
1329 config.type = PeerConnectionInterface::kRelay;
1330 EXPECT_TRUE(pc_->SetConfiguration(config));
1331
1332 PeerConnectionInterface::RTCConfiguration returned_config =
1333 pc_->GetConfiguration();
1334 EXPECT_EQ(PeerConnectionInterface::kRelay, returned_config.type);
1335}
1336
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001337TEST_F(PeerConnectionInterfaceTest, AddStreams) {
deadbeef293e9262017-01-11 12:28:30 -08001338 CreatePeerConnectionWithoutDtls();
deadbeefab9b2d12015-10-14 11:33:11 -07001339 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001340 AddVoiceStream(kStreamLabel2);
1341 ASSERT_EQ(2u, pc_->local_streams()->count());
1342
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001343 // Test we can add multiple local streams to one peerconnection.
zhihuang9763d562016-08-05 11:14:50 -07001344 rtc::scoped_refptr<MediaStreamInterface> stream(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001345 pc_factory_->CreateLocalMediaStream(kStreamLabel3));
zhihuang9763d562016-08-05 11:14:50 -07001346 rtc::scoped_refptr<AudioTrackInterface> audio_track(
1347 pc_factory_->CreateAudioTrack(kStreamLabel3,
1348 static_cast<AudioSourceInterface*>(NULL)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001349 stream->AddTrack(audio_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +00001350 EXPECT_TRUE(pc_->AddStream(stream));
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001351 EXPECT_EQ(3u, pc_->local_streams()->count());
1352
1353 // Remove the third stream.
1354 pc_->RemoveStream(pc_->local_streams()->at(2));
1355 EXPECT_EQ(2u, pc_->local_streams()->count());
1356
1357 // Remove the second stream.
1358 pc_->RemoveStream(pc_->local_streams()->at(1));
1359 EXPECT_EQ(1u, pc_->local_streams()->count());
1360
1361 // Remove the first stream.
1362 pc_->RemoveStream(pc_->local_streams()->at(0));
1363 EXPECT_EQ(0u, pc_->local_streams()->count());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001364}
1365
deadbeefab9b2d12015-10-14 11:33:11 -07001366// Test that the created offer includes streams we added.
1367TEST_F(PeerConnectionInterfaceTest, AddedStreamsPresentInOffer) {
deadbeef293e9262017-01-11 12:28:30 -08001368 CreatePeerConnectionWithoutDtls();
deadbeefab9b2d12015-10-14 11:33:11 -07001369 AddAudioVideoStream(kStreamLabel1, "audio_track", "video_track");
kwibergd1fe2812016-04-27 06:47:29 -07001370 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001371 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07001372
1373 const cricket::ContentInfo* audio_content =
1374 cricket::GetFirstAudioContent(offer->description());
1375 const cricket::AudioContentDescription* audio_desc =
1376 static_cast<const cricket::AudioContentDescription*>(
1377 audio_content->description);
1378 EXPECT_TRUE(
1379 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
1380
1381 const cricket::ContentInfo* video_content =
1382 cricket::GetFirstVideoContent(offer->description());
1383 const cricket::VideoContentDescription* video_desc =
1384 static_cast<const cricket::VideoContentDescription*>(
1385 video_content->description);
1386 EXPECT_TRUE(
1387 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1388
1389 // Add another stream and ensure the offer includes both the old and new
1390 // streams.
1391 AddAudioVideoStream(kStreamLabel2, "audio_track2", "video_track2");
kwiberg2bbff992016-03-16 11:03:04 -07001392 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07001393
1394 audio_content = cricket::GetFirstAudioContent(offer->description());
1395 audio_desc = static_cast<const cricket::AudioContentDescription*>(
1396 audio_content->description);
1397 EXPECT_TRUE(
1398 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
1399 EXPECT_TRUE(
1400 ContainsTrack(audio_desc->streams(), kStreamLabel2, "audio_track2"));
1401
1402 video_content = cricket::GetFirstVideoContent(offer->description());
1403 video_desc = static_cast<const cricket::VideoContentDescription*>(
1404 video_content->description);
1405 EXPECT_TRUE(
1406 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1407 EXPECT_TRUE(
1408 ContainsTrack(video_desc->streams(), kStreamLabel2, "video_track2"));
1409}
1410
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001411TEST_F(PeerConnectionInterfaceTest, RemoveStream) {
deadbeef293e9262017-01-11 12:28:30 -08001412 CreatePeerConnectionWithoutDtls();
deadbeefab9b2d12015-10-14 11:33:11 -07001413 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001414 ASSERT_EQ(1u, pc_->local_streams()->count());
1415 pc_->RemoveStream(pc_->local_streams()->at(0));
1416 EXPECT_EQ(0u, pc_->local_streams()->count());
1417}
1418
deadbeefe1f9d832016-01-14 15:35:42 -08001419// Test for AddTrack and RemoveTrack methods.
1420// Tests that the created offer includes tracks we added,
1421// and that the RtpSenders are created correctly.
1422// Also tests that RemoveTrack removes the tracks from subsequent offers.
1423TEST_F(PeerConnectionInterfaceTest, AddTrackRemoveTrack) {
deadbeef293e9262017-01-11 12:28:30 -08001424 CreatePeerConnectionWithoutDtls();
deadbeefe1f9d832016-01-14 15:35:42 -08001425 // Create a dummy stream, so tracks share a stream label.
zhihuang9763d562016-08-05 11:14:50 -07001426 rtc::scoped_refptr<MediaStreamInterface> stream(
deadbeefe1f9d832016-01-14 15:35:42 -08001427 pc_factory_->CreateLocalMediaStream(kStreamLabel1));
1428 std::vector<MediaStreamInterface*> stream_list;
1429 stream_list.push_back(stream.get());
zhihuang9763d562016-08-05 11:14:50 -07001430 rtc::scoped_refptr<AudioTrackInterface> audio_track(
deadbeefe1f9d832016-01-14 15:35:42 -08001431 pc_factory_->CreateAudioTrack("audio_track", nullptr));
zhihuang9763d562016-08-05 11:14:50 -07001432 rtc::scoped_refptr<VideoTrackInterface> video_track(
1433 pc_factory_->CreateVideoTrack(
deadbeef112b2e92017-02-10 20:13:37 -08001434 "video_track", pc_factory_->CreateVideoSource(
1435 std::unique_ptr<cricket::VideoCapturer>(
1436 new cricket::FakeVideoCapturer()))));
deadbeefe1f9d832016-01-14 15:35:42 -08001437 auto audio_sender = pc_->AddTrack(audio_track, stream_list);
1438 auto video_sender = pc_->AddTrack(video_track, stream_list);
deadbeefa601f5c2016-06-06 14:27:39 -07001439 EXPECT_EQ(1UL, audio_sender->stream_ids().size());
1440 EXPECT_EQ(kStreamLabel1, audio_sender->stream_ids()[0]);
deadbeefe1f9d832016-01-14 15:35:42 -08001441 EXPECT_EQ("audio_track", audio_sender->id());
1442 EXPECT_EQ(audio_track, audio_sender->track());
deadbeefa601f5c2016-06-06 14:27:39 -07001443 EXPECT_EQ(1UL, video_sender->stream_ids().size());
1444 EXPECT_EQ(kStreamLabel1, video_sender->stream_ids()[0]);
deadbeefe1f9d832016-01-14 15:35:42 -08001445 EXPECT_EQ("video_track", video_sender->id());
1446 EXPECT_EQ(video_track, video_sender->track());
1447
1448 // Now create an offer and check for the senders.
kwibergd1fe2812016-04-27 06:47:29 -07001449 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001450 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefe1f9d832016-01-14 15:35:42 -08001451
1452 const cricket::ContentInfo* audio_content =
1453 cricket::GetFirstAudioContent(offer->description());
1454 const cricket::AudioContentDescription* audio_desc =
1455 static_cast<const cricket::AudioContentDescription*>(
1456 audio_content->description);
1457 EXPECT_TRUE(
1458 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
1459
1460 const cricket::ContentInfo* video_content =
1461 cricket::GetFirstVideoContent(offer->description());
1462 const cricket::VideoContentDescription* video_desc =
1463 static_cast<const cricket::VideoContentDescription*>(
1464 video_content->description);
1465 EXPECT_TRUE(
1466 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1467
1468 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
1469
1470 // Now try removing the tracks.
1471 EXPECT_TRUE(pc_->RemoveTrack(audio_sender));
1472 EXPECT_TRUE(pc_->RemoveTrack(video_sender));
1473
1474 // Create a new offer and ensure it doesn't contain the removed senders.
kwiberg2bbff992016-03-16 11:03:04 -07001475 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefe1f9d832016-01-14 15:35:42 -08001476
1477 audio_content = cricket::GetFirstAudioContent(offer->description());
1478 audio_desc = static_cast<const cricket::AudioContentDescription*>(
1479 audio_content->description);
1480 EXPECT_FALSE(
1481 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
1482
1483 video_content = cricket::GetFirstVideoContent(offer->description());
1484 video_desc = static_cast<const cricket::VideoContentDescription*>(
1485 video_content->description);
1486 EXPECT_FALSE(
1487 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1488
1489 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
1490
1491 // Calling RemoveTrack on a sender no longer attached to a PeerConnection
1492 // should return false.
1493 EXPECT_FALSE(pc_->RemoveTrack(audio_sender));
1494 EXPECT_FALSE(pc_->RemoveTrack(video_sender));
1495}
1496
1497// Test creating senders without a stream specified,
1498// expecting a random stream ID to be generated.
1499TEST_F(PeerConnectionInterfaceTest, AddTrackWithoutStream) {
deadbeef293e9262017-01-11 12:28:30 -08001500 CreatePeerConnectionWithoutDtls();
deadbeefe1f9d832016-01-14 15:35:42 -08001501 // Create a dummy stream, so tracks share a stream label.
zhihuang9763d562016-08-05 11:14:50 -07001502 rtc::scoped_refptr<AudioTrackInterface> audio_track(
deadbeefe1f9d832016-01-14 15:35:42 -08001503 pc_factory_->CreateAudioTrack("audio_track", nullptr));
zhihuang9763d562016-08-05 11:14:50 -07001504 rtc::scoped_refptr<VideoTrackInterface> video_track(
1505 pc_factory_->CreateVideoTrack(
deadbeef112b2e92017-02-10 20:13:37 -08001506 "video_track", pc_factory_->CreateVideoSource(
1507 std::unique_ptr<cricket::VideoCapturer>(
1508 new cricket::FakeVideoCapturer()))));
deadbeefe1f9d832016-01-14 15:35:42 -08001509 auto audio_sender =
1510 pc_->AddTrack(audio_track, std::vector<MediaStreamInterface*>());
1511 auto video_sender =
1512 pc_->AddTrack(video_track, std::vector<MediaStreamInterface*>());
1513 EXPECT_EQ("audio_track", audio_sender->id());
1514 EXPECT_EQ(audio_track, audio_sender->track());
1515 EXPECT_EQ("video_track", video_sender->id());
1516 EXPECT_EQ(video_track, video_sender->track());
1517 // If the ID is truly a random GUID, it should be infinitely unlikely they
1518 // will be the same.
deadbeefa601f5c2016-06-06 14:27:39 -07001519 EXPECT_NE(video_sender->stream_ids(), audio_sender->stream_ids());
deadbeefe1f9d832016-01-14 15:35:42 -08001520}
1521
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001522TEST_F(PeerConnectionInterfaceTest, CreateOfferReceiveAnswer) {
1523 InitiateCall();
1524 WaitAndVerifyOnAddStream(kStreamLabel1);
1525 VerifyRemoteRtpHeaderExtensions();
1526}
1527
1528TEST_F(PeerConnectionInterfaceTest, CreateOfferReceivePrAnswerAndAnswer) {
deadbeef293e9262017-01-11 12:28:30 -08001529 CreatePeerConnectionWithoutDtls();
deadbeefab9b2d12015-10-14 11:33:11 -07001530 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001531 CreateOfferAsLocalDescription();
1532 std::string offer;
1533 EXPECT_TRUE(pc_->local_description()->ToString(&offer));
1534 CreatePrAnswerAndAnswerAsRemoteDescription(offer);
1535 WaitAndVerifyOnAddStream(kStreamLabel1);
1536}
1537
1538TEST_F(PeerConnectionInterfaceTest, ReceiveOfferCreateAnswer) {
deadbeef293e9262017-01-11 12:28:30 -08001539 CreatePeerConnectionWithoutDtls();
deadbeefab9b2d12015-10-14 11:33:11 -07001540 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001541
1542 CreateOfferAsRemoteDescription();
1543 CreateAnswerAsLocalDescription();
1544
1545 WaitAndVerifyOnAddStream(kStreamLabel1);
1546}
1547
1548TEST_F(PeerConnectionInterfaceTest, ReceiveOfferCreatePrAnswerAndAnswer) {
deadbeef293e9262017-01-11 12:28:30 -08001549 CreatePeerConnectionWithoutDtls();
deadbeefab9b2d12015-10-14 11:33:11 -07001550 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001551
1552 CreateOfferAsRemoteDescription();
1553 CreatePrAnswerAsLocalDescription();
1554 CreateAnswerAsLocalDescription();
1555
1556 WaitAndVerifyOnAddStream(kStreamLabel1);
1557}
1558
1559TEST_F(PeerConnectionInterfaceTest, Renegotiate) {
1560 InitiateCall();
1561 ASSERT_EQ(1u, pc_->remote_streams()->count());
1562 pc_->RemoveStream(pc_->local_streams()->at(0));
1563 CreateOfferReceiveAnswer();
1564 EXPECT_EQ(0u, pc_->remote_streams()->count());
deadbeefab9b2d12015-10-14 11:33:11 -07001565 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001566 CreateOfferReceiveAnswer();
1567}
1568
1569// Tests that after negotiating an audio only call, the respondent can perform a
1570// renegotiation that removes the audio stream.
1571TEST_F(PeerConnectionInterfaceTest, RenegotiateAudioOnly) {
deadbeef293e9262017-01-11 12:28:30 -08001572 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001573 AddVoiceStream(kStreamLabel1);
1574 CreateOfferAsRemoteDescription();
1575 CreateAnswerAsLocalDescription();
1576
1577 ASSERT_EQ(1u, pc_->remote_streams()->count());
1578 pc_->RemoveStream(pc_->local_streams()->at(0));
1579 CreateOfferReceiveAnswer();
1580 EXPECT_EQ(0u, pc_->remote_streams()->count());
1581}
1582
1583// Test that candidates are generated and that we can parse our own candidates.
1584TEST_F(PeerConnectionInterfaceTest, IceCandidates) {
deadbeef293e9262017-01-11 12:28:30 -08001585 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001586
1587 EXPECT_FALSE(pc_->AddIceCandidate(observer_.last_candidate_.get()));
1588 // SetRemoteDescription takes ownership of offer.
kwibergd1fe2812016-04-27 06:47:29 -07001589 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefab9b2d12015-10-14 11:33:11 -07001590 AddVideoStream(kStreamLabel1);
deadbeefc80741f2015-10-22 13:14:45 -07001591 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
kwiberg2bbff992016-03-16 11:03:04 -07001592 EXPECT_TRUE(DoSetRemoteDescription(offer.release()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001593
1594 // SetLocalDescription takes ownership of answer.
kwibergd1fe2812016-04-27 06:47:29 -07001595 std::unique_ptr<SessionDescriptionInterface> answer;
deadbeefc80741f2015-10-22 13:14:45 -07001596 EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
kwiberg2bbff992016-03-16 11:03:04 -07001597 EXPECT_TRUE(DoSetLocalDescription(answer.release()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001598
1599 EXPECT_TRUE_WAIT(observer_.last_candidate_.get() != NULL, kTimeout);
1600 EXPECT_TRUE_WAIT(observer_.ice_complete_, kTimeout);
1601
1602 EXPECT_TRUE(pc_->AddIceCandidate(observer_.last_candidate_.get()));
1603}
1604
deadbeefab9b2d12015-10-14 11:33:11 -07001605// Test that CreateOffer and CreateAnswer will fail if the track labels are
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001606// not unique.
1607TEST_F(PeerConnectionInterfaceTest, CreateOfferAnswerWithInvalidStream) {
deadbeef293e9262017-01-11 12:28:30 -08001608 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001609 // Create a regular offer for the CreateAnswer test later.
kwibergd1fe2812016-04-27 06:47:29 -07001610 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefc80741f2015-10-22 13:14:45 -07001611 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
kwiberg2bbff992016-03-16 11:03:04 -07001612 EXPECT_TRUE(offer);
1613 offer.reset();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001614
1615 // Create a local stream with audio&video tracks having same label.
1616 AddAudioVideoStream(kStreamLabel1, "track_label", "track_label");
1617
1618 // Test CreateOffer
deadbeefc80741f2015-10-22 13:14:45 -07001619 EXPECT_FALSE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001620
1621 // Test CreateAnswer
kwibergd1fe2812016-04-27 06:47:29 -07001622 std::unique_ptr<SessionDescriptionInterface> answer;
deadbeefc80741f2015-10-22 13:14:45 -07001623 EXPECT_FALSE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001624}
1625
1626// Test that we will get different SSRCs for each tracks in the offer and answer
1627// we created.
1628TEST_F(PeerConnectionInterfaceTest, SsrcInOfferAnswer) {
deadbeef293e9262017-01-11 12:28:30 -08001629 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001630 // Create a local stream with audio&video tracks having different labels.
1631 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
1632
1633 // Test CreateOffer
kwibergd1fe2812016-04-27 06:47:29 -07001634 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001635 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001636 int audio_ssrc = 0;
1637 int video_ssrc = 0;
1638 EXPECT_TRUE(GetFirstSsrc(GetFirstAudioContent(offer->description()),
1639 &audio_ssrc));
1640 EXPECT_TRUE(GetFirstSsrc(GetFirstVideoContent(offer->description()),
1641 &video_ssrc));
1642 EXPECT_NE(audio_ssrc, video_ssrc);
1643
1644 // Test CreateAnswer
1645 EXPECT_TRUE(DoSetRemoteDescription(offer.release()));
kwibergd1fe2812016-04-27 06:47:29 -07001646 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -07001647 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001648 audio_ssrc = 0;
1649 video_ssrc = 0;
1650 EXPECT_TRUE(GetFirstSsrc(GetFirstAudioContent(answer->description()),
1651 &audio_ssrc));
1652 EXPECT_TRUE(GetFirstSsrc(GetFirstVideoContent(answer->description()),
1653 &video_ssrc));
1654 EXPECT_NE(audio_ssrc, video_ssrc);
1655}
1656
deadbeefeb459812015-12-15 19:24:43 -08001657// Test that it's possible to call AddTrack on a MediaStream after adding
1658// the stream to a PeerConnection.
1659// TODO(deadbeef): Remove this test once this behavior is no longer supported.
1660TEST_F(PeerConnectionInterfaceTest, AddTrackAfterAddStream) {
deadbeef293e9262017-01-11 12:28:30 -08001661 CreatePeerConnectionWithoutDtls();
deadbeefeb459812015-12-15 19:24:43 -08001662 // Create audio stream and add to PeerConnection.
1663 AddVoiceStream(kStreamLabel1);
1664 MediaStreamInterface* stream = pc_->local_streams()->at(0);
1665
1666 // Add video track to the audio-only stream.
zhihuang9763d562016-08-05 11:14:50 -07001667 rtc::scoped_refptr<VideoTrackInterface> video_track(
1668 pc_factory_->CreateVideoTrack(
deadbeef112b2e92017-02-10 20:13:37 -08001669 "video_label", pc_factory_->CreateVideoSource(
1670 std::unique_ptr<cricket::VideoCapturer>(
1671 new cricket::FakeVideoCapturer()))));
deadbeefeb459812015-12-15 19:24:43 -08001672 stream->AddTrack(video_track.get());
1673
kwibergd1fe2812016-04-27 06:47:29 -07001674 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001675 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefeb459812015-12-15 19:24:43 -08001676
1677 const cricket::MediaContentDescription* video_desc =
1678 cricket::GetFirstVideoContentDescription(offer->description());
1679 EXPECT_TRUE(video_desc != nullptr);
1680}
1681
1682// Test that it's possible to call RemoveTrack on a MediaStream after adding
1683// the stream to a PeerConnection.
1684// TODO(deadbeef): Remove this test once this behavior is no longer supported.
1685TEST_F(PeerConnectionInterfaceTest, RemoveTrackAfterAddStream) {
deadbeef293e9262017-01-11 12:28:30 -08001686 CreatePeerConnectionWithoutDtls();
deadbeefeb459812015-12-15 19:24:43 -08001687 // Create audio/video stream and add to PeerConnection.
1688 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
1689 MediaStreamInterface* stream = pc_->local_streams()->at(0);
1690
1691 // Remove the video track.
1692 stream->RemoveTrack(stream->GetVideoTracks()[0]);
1693
kwibergd1fe2812016-04-27 06:47:29 -07001694 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001695 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefeb459812015-12-15 19:24:43 -08001696
1697 const cricket::MediaContentDescription* video_desc =
1698 cricket::GetFirstVideoContentDescription(offer->description());
1699 EXPECT_TRUE(video_desc == nullptr);
1700}
1701
deadbeef1dcb1642017-03-29 21:08:16 -07001702// Verify that CreateDtmfSender only succeeds if called with a valid local
1703// track. Other aspects of DtmfSenders are tested in
1704// peerconnection_integrationtest.cc.
1705TEST_F(PeerConnectionInterfaceTest, CreateDtmfSenderWithInvalidParams) {
1706 CreatePeerConnection();
1707 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
1708 EXPECT_EQ(nullptr, pc_->CreateDtmfSender(nullptr));
1709 rtc::scoped_refptr<webrtc::AudioTrackInterface> non_localtrack(
1710 pc_factory_->CreateAudioTrack("dummy_track", nullptr));
1711 EXPECT_EQ(nullptr, pc_->CreateDtmfSender(non_localtrack));
1712}
1713
deadbeefbd7d8f72015-12-18 16:58:44 -08001714// Test creating a sender with a stream ID, and ensure the ID is populated
1715// in the offer.
1716TEST_F(PeerConnectionInterfaceTest, CreateSenderWithStream) {
deadbeef293e9262017-01-11 12:28:30 -08001717 CreatePeerConnectionWithoutDtls();
deadbeefbd7d8f72015-12-18 16:58:44 -08001718 pc_->CreateSender("video", kStreamLabel1);
1719
kwibergd1fe2812016-04-27 06:47:29 -07001720 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001721 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefbd7d8f72015-12-18 16:58:44 -08001722
1723 const cricket::MediaContentDescription* video_desc =
1724 cricket::GetFirstVideoContentDescription(offer->description());
1725 ASSERT_TRUE(video_desc != nullptr);
1726 ASSERT_EQ(1u, video_desc->streams().size());
1727 EXPECT_EQ(kStreamLabel1, video_desc->streams()[0].sync_label);
1728}
1729
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001730// Test that we can specify a certain track that we want statistics about.
1731TEST_F(PeerConnectionInterfaceTest, GetStatsForSpecificTrack) {
1732 InitiateCall();
1733 ASSERT_LT(0u, pc_->remote_streams()->count());
1734 ASSERT_LT(0u, pc_->remote_streams()->at(0)->GetAudioTracks().size());
zhihuang9763d562016-08-05 11:14:50 -07001735 rtc::scoped_refptr<MediaStreamTrackInterface> remote_audio =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001736 pc_->remote_streams()->at(0)->GetAudioTracks()[0];
1737 EXPECT_TRUE(DoGetStats(remote_audio));
1738
1739 // Remove the stream. Since we are sending to our selves the local
1740 // and the remote stream is the same.
1741 pc_->RemoveStream(pc_->local_streams()->at(0));
1742 // Do a re-negotiation.
1743 CreateOfferReceiveAnswer();
1744
1745 ASSERT_EQ(0u, pc_->remote_streams()->count());
1746
1747 // Test that we still can get statistics for the old track. Even if it is not
1748 // sent any longer.
1749 EXPECT_TRUE(DoGetStats(remote_audio));
1750}
1751
1752// Test that we can get stats on a video track.
1753TEST_F(PeerConnectionInterfaceTest, GetStatsForVideoTrack) {
1754 InitiateCall();
1755 ASSERT_LT(0u, pc_->remote_streams()->count());
1756 ASSERT_LT(0u, pc_->remote_streams()->at(0)->GetVideoTracks().size());
zhihuang9763d562016-08-05 11:14:50 -07001757 rtc::scoped_refptr<MediaStreamTrackInterface> remote_video =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001758 pc_->remote_streams()->at(0)->GetVideoTracks()[0];
1759 EXPECT_TRUE(DoGetStats(remote_video));
1760}
1761
1762// Test that we don't get statistics for an invalid track.
zhihuange9e94c32016-11-04 11:38:15 -07001763TEST_F(PeerConnectionInterfaceTest, GetStatsForInvalidTrack) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001764 InitiateCall();
zhihuang9763d562016-08-05 11:14:50 -07001765 rtc::scoped_refptr<AudioTrackInterface> unknown_audio_track(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001766 pc_factory_->CreateAudioTrack("unknown track", NULL));
1767 EXPECT_FALSE(DoGetStats(unknown_audio_track));
1768}
1769
1770// This test setup two RTP data channels in loop back.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001771TEST_F(PeerConnectionInterfaceTest, TestDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001772 FakeConstraints constraints;
1773 constraints.SetAllowRtpDataChannels();
1774 CreatePeerConnection(&constraints);
zhihuang9763d562016-08-05 11:14:50 -07001775 rtc::scoped_refptr<DataChannelInterface> data1 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001776 pc_->CreateDataChannel("test1", NULL);
zhihuang9763d562016-08-05 11:14:50 -07001777 rtc::scoped_refptr<DataChannelInterface> data2 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001778 pc_->CreateDataChannel("test2", NULL);
1779 ASSERT_TRUE(data1 != NULL);
kwibergd1fe2812016-04-27 06:47:29 -07001780 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001781 new MockDataChannelObserver(data1));
kwibergd1fe2812016-04-27 06:47:29 -07001782 std::unique_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001783 new MockDataChannelObserver(data2));
1784
1785 EXPECT_EQ(DataChannelInterface::kConnecting, data1->state());
1786 EXPECT_EQ(DataChannelInterface::kConnecting, data2->state());
1787 std::string data_to_send1 = "testing testing";
1788 std::string data_to_send2 = "testing something else";
1789 EXPECT_FALSE(data1->Send(DataBuffer(data_to_send1)));
1790
1791 CreateOfferReceiveAnswer();
1792 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1793 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
1794
1795 EXPECT_EQ(DataChannelInterface::kOpen, data1->state());
1796 EXPECT_EQ(DataChannelInterface::kOpen, data2->state());
1797 EXPECT_TRUE(data1->Send(DataBuffer(data_to_send1)));
1798 EXPECT_TRUE(data2->Send(DataBuffer(data_to_send2)));
1799
1800 EXPECT_EQ_WAIT(data_to_send1, observer1->last_message(), kTimeout);
1801 EXPECT_EQ_WAIT(data_to_send2, observer2->last_message(), kTimeout);
1802
1803 data1->Close();
1804 EXPECT_EQ(DataChannelInterface::kClosing, data1->state());
1805 CreateOfferReceiveAnswer();
1806 EXPECT_FALSE(observer1->IsOpen());
1807 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
1808 EXPECT_TRUE(observer2->IsOpen());
1809
1810 data_to_send2 = "testing something else again";
1811 EXPECT_TRUE(data2->Send(DataBuffer(data_to_send2)));
1812
1813 EXPECT_EQ_WAIT(data_to_send2, observer2->last_message(), kTimeout);
1814}
1815
1816// This test verifies that sendnig binary data over RTP data channels should
1817// fail.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001818TEST_F(PeerConnectionInterfaceTest, TestSendBinaryOnRtpDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001819 FakeConstraints constraints;
1820 constraints.SetAllowRtpDataChannels();
1821 CreatePeerConnection(&constraints);
zhihuang9763d562016-08-05 11:14:50 -07001822 rtc::scoped_refptr<DataChannelInterface> data1 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001823 pc_->CreateDataChannel("test1", NULL);
zhihuang9763d562016-08-05 11:14:50 -07001824 rtc::scoped_refptr<DataChannelInterface> data2 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001825 pc_->CreateDataChannel("test2", NULL);
1826 ASSERT_TRUE(data1 != NULL);
kwibergd1fe2812016-04-27 06:47:29 -07001827 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001828 new MockDataChannelObserver(data1));
kwibergd1fe2812016-04-27 06:47:29 -07001829 std::unique_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001830 new MockDataChannelObserver(data2));
1831
1832 EXPECT_EQ(DataChannelInterface::kConnecting, data1->state());
1833 EXPECT_EQ(DataChannelInterface::kConnecting, data2->state());
1834
1835 CreateOfferReceiveAnswer();
1836 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1837 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
1838
1839 EXPECT_EQ(DataChannelInterface::kOpen, data1->state());
1840 EXPECT_EQ(DataChannelInterface::kOpen, data2->state());
1841
jbaucheec21bd2016-03-20 06:15:43 -07001842 rtc::CopyOnWriteBuffer buffer("test", 4);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001843 EXPECT_FALSE(data1->Send(DataBuffer(buffer, true)));
1844}
1845
1846// This test setup a RTP data channels in loop back and test that a channel is
1847// opened even if the remote end answer with a zero SSRC.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001848TEST_F(PeerConnectionInterfaceTest, TestSendOnlyDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001849 FakeConstraints constraints;
1850 constraints.SetAllowRtpDataChannels();
1851 CreatePeerConnection(&constraints);
zhihuang9763d562016-08-05 11:14:50 -07001852 rtc::scoped_refptr<DataChannelInterface> data1 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001853 pc_->CreateDataChannel("test1", NULL);
kwibergd1fe2812016-04-27 06:47:29 -07001854 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001855 new MockDataChannelObserver(data1));
1856
1857 CreateOfferReceiveAnswerWithoutSsrc();
1858
1859 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1860
1861 data1->Close();
1862 EXPECT_EQ(DataChannelInterface::kClosing, data1->state());
1863 CreateOfferReceiveAnswerWithoutSsrc();
1864 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
1865 EXPECT_FALSE(observer1->IsOpen());
1866}
1867
1868// This test that if a data channel is added in an answer a receive only channel
1869// channel is created.
1870TEST_F(PeerConnectionInterfaceTest, TestReceiveOnlyDataChannel) {
1871 FakeConstraints constraints;
1872 constraints.SetAllowRtpDataChannels();
1873 CreatePeerConnection(&constraints);
1874
1875 std::string offer_label = "offer_channel";
zhihuang9763d562016-08-05 11:14:50 -07001876 rtc::scoped_refptr<DataChannelInterface> offer_channel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001877 pc_->CreateDataChannel(offer_label, NULL);
1878
1879 CreateOfferAsLocalDescription();
1880
1881 // Replace the data channel label in the offer and apply it as an answer.
1882 std::string receive_label = "answer_channel";
1883 std::string sdp;
1884 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001885 rtc::replace_substrs(offer_label.c_str(), offer_label.length(),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001886 receive_label.c_str(), receive_label.length(),
1887 &sdp);
1888 CreateAnswerAsRemoteDescription(sdp);
1889
1890 // Verify that a new incoming data channel has been created and that
1891 // it is open but can't we written to.
1892 ASSERT_TRUE(observer_.last_datachannel_ != NULL);
1893 DataChannelInterface* received_channel = observer_.last_datachannel_;
1894 EXPECT_EQ(DataChannelInterface::kConnecting, received_channel->state());
1895 EXPECT_EQ(receive_label, received_channel->label());
1896 EXPECT_FALSE(received_channel->Send(DataBuffer("something")));
1897
1898 // Verify that the channel we initially offered has been rejected.
1899 EXPECT_EQ(DataChannelInterface::kClosed, offer_channel->state());
1900
1901 // Do another offer / answer exchange and verify that the data channel is
1902 // opened.
1903 CreateOfferReceiveAnswer();
1904 EXPECT_EQ_WAIT(DataChannelInterface::kOpen, received_channel->state(),
1905 kTimeout);
1906}
1907
1908// This test that no data channel is returned if a reliable channel is
1909// requested.
1910// TODO(perkj): Remove this test once reliable channels are implemented.
1911TEST_F(PeerConnectionInterfaceTest, CreateReliableRtpDataChannelShouldFail) {
1912 FakeConstraints constraints;
1913 constraints.SetAllowRtpDataChannels();
1914 CreatePeerConnection(&constraints);
1915
1916 std::string label = "test";
1917 webrtc::DataChannelInit config;
1918 config.reliable = true;
zhihuang9763d562016-08-05 11:14:50 -07001919 rtc::scoped_refptr<DataChannelInterface> channel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001920 pc_->CreateDataChannel(label, &config);
1921 EXPECT_TRUE(channel == NULL);
1922}
1923
deadbeefab9b2d12015-10-14 11:33:11 -07001924// Verifies that duplicated label is not allowed for RTP data channel.
1925TEST_F(PeerConnectionInterfaceTest, RtpDuplicatedLabelNotAllowed) {
1926 FakeConstraints constraints;
1927 constraints.SetAllowRtpDataChannels();
1928 CreatePeerConnection(&constraints);
1929
1930 std::string label = "test";
zhihuang9763d562016-08-05 11:14:50 -07001931 rtc::scoped_refptr<DataChannelInterface> channel =
deadbeefab9b2d12015-10-14 11:33:11 -07001932 pc_->CreateDataChannel(label, nullptr);
1933 EXPECT_NE(channel, nullptr);
1934
zhihuang9763d562016-08-05 11:14:50 -07001935 rtc::scoped_refptr<DataChannelInterface> dup_channel =
deadbeefab9b2d12015-10-14 11:33:11 -07001936 pc_->CreateDataChannel(label, nullptr);
1937 EXPECT_EQ(dup_channel, nullptr);
1938}
1939
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001940// This tests that a SCTP data channel is returned using different
1941// DataChannelInit configurations.
1942TEST_F(PeerConnectionInterfaceTest, CreateSctpDataChannel) {
1943 FakeConstraints constraints;
1944 constraints.SetAllowDtlsSctpDataChannels();
1945 CreatePeerConnection(&constraints);
1946
1947 webrtc::DataChannelInit config;
1948
zhihuang9763d562016-08-05 11:14:50 -07001949 rtc::scoped_refptr<DataChannelInterface> channel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001950 pc_->CreateDataChannel("1", &config);
1951 EXPECT_TRUE(channel != NULL);
1952 EXPECT_TRUE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001953 EXPECT_TRUE(observer_.renegotiation_needed_);
1954 observer_.renegotiation_needed_ = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001955
1956 config.ordered = false;
1957 channel = pc_->CreateDataChannel("2", &config);
1958 EXPECT_TRUE(channel != NULL);
1959 EXPECT_TRUE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001960 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001961
1962 config.ordered = true;
1963 config.maxRetransmits = 0;
1964 channel = pc_->CreateDataChannel("3", &config);
1965 EXPECT_TRUE(channel != NULL);
1966 EXPECT_FALSE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001967 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001968
1969 config.maxRetransmits = -1;
1970 config.maxRetransmitTime = 0;
1971 channel = pc_->CreateDataChannel("4", &config);
1972 EXPECT_TRUE(channel != NULL);
1973 EXPECT_FALSE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001974 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001975}
1976
1977// This tests that no data channel is returned if both maxRetransmits and
1978// maxRetransmitTime are set for SCTP data channels.
1979TEST_F(PeerConnectionInterfaceTest,
1980 CreateSctpDataChannelShouldFailForInvalidConfig) {
1981 FakeConstraints constraints;
1982 constraints.SetAllowDtlsSctpDataChannels();
1983 CreatePeerConnection(&constraints);
1984
1985 std::string label = "test";
1986 webrtc::DataChannelInit config;
1987 config.maxRetransmits = 0;
1988 config.maxRetransmitTime = 0;
1989
zhihuang9763d562016-08-05 11:14:50 -07001990 rtc::scoped_refptr<DataChannelInterface> channel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001991 pc_->CreateDataChannel(label, &config);
1992 EXPECT_TRUE(channel == NULL);
1993}
1994
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001995// The test verifies that creating a SCTP data channel with an id already in use
1996// or out of range should fail.
1997TEST_F(PeerConnectionInterfaceTest,
1998 CreateSctpDataChannelWithInvalidIdShouldFail) {
1999 FakeConstraints constraints;
2000 constraints.SetAllowDtlsSctpDataChannels();
2001 CreatePeerConnection(&constraints);
2002
2003 webrtc::DataChannelInit config;
zhihuang9763d562016-08-05 11:14:50 -07002004 rtc::scoped_refptr<DataChannelInterface> channel;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002005
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00002006 config.id = 1;
2007 channel = pc_->CreateDataChannel("1", &config);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002008 EXPECT_TRUE(channel != NULL);
2009 EXPECT_EQ(1, channel->id());
2010
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002011 channel = pc_->CreateDataChannel("x", &config);
2012 EXPECT_TRUE(channel == NULL);
2013
2014 config.id = cricket::kMaxSctpSid;
2015 channel = pc_->CreateDataChannel("max", &config);
2016 EXPECT_TRUE(channel != NULL);
2017 EXPECT_EQ(config.id, channel->id());
2018
2019 config.id = cricket::kMaxSctpSid + 1;
2020 channel = pc_->CreateDataChannel("x", &config);
2021 EXPECT_TRUE(channel == NULL);
2022}
2023
deadbeefab9b2d12015-10-14 11:33:11 -07002024// Verifies that duplicated label is allowed for SCTP data channel.
2025TEST_F(PeerConnectionInterfaceTest, SctpDuplicatedLabelAllowed) {
2026 FakeConstraints constraints;
2027 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2028 true);
2029 CreatePeerConnection(&constraints);
2030
2031 std::string label = "test";
zhihuang9763d562016-08-05 11:14:50 -07002032 rtc::scoped_refptr<DataChannelInterface> channel =
deadbeefab9b2d12015-10-14 11:33:11 -07002033 pc_->CreateDataChannel(label, nullptr);
2034 EXPECT_NE(channel, nullptr);
2035
zhihuang9763d562016-08-05 11:14:50 -07002036 rtc::scoped_refptr<DataChannelInterface> dup_channel =
deadbeefab9b2d12015-10-14 11:33:11 -07002037 pc_->CreateDataChannel(label, nullptr);
2038 EXPECT_NE(dup_channel, nullptr);
2039}
2040
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00002041// This test verifies that OnRenegotiationNeeded is fired for every new RTP
2042// DataChannel.
2043TEST_F(PeerConnectionInterfaceTest, RenegotiationNeededForNewRtpDataChannel) {
2044 FakeConstraints constraints;
2045 constraints.SetAllowRtpDataChannels();
2046 CreatePeerConnection(&constraints);
2047
zhihuang9763d562016-08-05 11:14:50 -07002048 rtc::scoped_refptr<DataChannelInterface> dc1 =
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00002049 pc_->CreateDataChannel("test1", NULL);
2050 EXPECT_TRUE(observer_.renegotiation_needed_);
2051 observer_.renegotiation_needed_ = false;
2052
zhihuang9763d562016-08-05 11:14:50 -07002053 rtc::scoped_refptr<DataChannelInterface> dc2 =
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00002054 pc_->CreateDataChannel("test2", NULL);
2055 EXPECT_TRUE(observer_.renegotiation_needed_);
2056}
2057
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002058// This test that a data channel closes when a PeerConnection is deleted/closed.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002059TEST_F(PeerConnectionInterfaceTest, DataChannelCloseWhenPeerConnectionClose) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002060 FakeConstraints constraints;
2061 constraints.SetAllowRtpDataChannels();
2062 CreatePeerConnection(&constraints);
2063
zhihuang9763d562016-08-05 11:14:50 -07002064 rtc::scoped_refptr<DataChannelInterface> data1 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002065 pc_->CreateDataChannel("test1", NULL);
zhihuang9763d562016-08-05 11:14:50 -07002066 rtc::scoped_refptr<DataChannelInterface> data2 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002067 pc_->CreateDataChannel("test2", NULL);
2068 ASSERT_TRUE(data1 != NULL);
kwibergd1fe2812016-04-27 06:47:29 -07002069 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002070 new MockDataChannelObserver(data1));
kwibergd1fe2812016-04-27 06:47:29 -07002071 std::unique_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002072 new MockDataChannelObserver(data2));
2073
2074 CreateOfferReceiveAnswer();
2075 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
2076 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
2077
2078 ReleasePeerConnection();
2079 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
2080 EXPECT_EQ(DataChannelInterface::kClosed, data2->state());
2081}
2082
2083// This test that data channels can be rejected in an answer.
2084TEST_F(PeerConnectionInterfaceTest, TestRejectDataChannelInAnswer) {
2085 FakeConstraints constraints;
2086 constraints.SetAllowRtpDataChannels();
2087 CreatePeerConnection(&constraints);
2088
zhihuang9763d562016-08-05 11:14:50 -07002089 rtc::scoped_refptr<DataChannelInterface> offer_channel(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002090 pc_->CreateDataChannel("offer_channel", NULL));
2091
2092 CreateOfferAsLocalDescription();
2093
2094 // Create an answer where the m-line for data channels are rejected.
2095 std::string sdp;
2096 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
2097 webrtc::JsepSessionDescription* answer = new webrtc::JsepSessionDescription(
2098 SessionDescriptionInterface::kAnswer);
2099 EXPECT_TRUE(answer->Initialize(sdp, NULL));
2100 cricket::ContentInfo* data_info =
2101 answer->description()->GetContentByName("data");
2102 data_info->rejected = true;
2103
2104 DoSetRemoteDescription(answer);
2105 EXPECT_EQ(DataChannelInterface::kClosed, offer_channel->state());
2106}
2107
2108// Test that we can create a session description from an SDP string from
2109// FireFox, use it as a remote session description, generate an answer and use
2110// the answer as a local description.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002111TEST_F(PeerConnectionInterfaceTest, ReceiveFireFoxOffer) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002112 FakeConstraints constraints;
2113 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2114 true);
2115 CreatePeerConnection(&constraints);
2116 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
2117 SessionDescriptionInterface* desc =
2118 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
jbauchfabe2c92015-07-16 13:43:14 -07002119 webrtc::kFireFoxSdpOffer, nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002120 EXPECT_TRUE(DoSetSessionDescription(desc, false));
2121 CreateAnswerAsLocalDescription();
2122 ASSERT_TRUE(pc_->local_description() != NULL);
2123 ASSERT_TRUE(pc_->remote_description() != NULL);
2124
2125 const cricket::ContentInfo* content =
2126 cricket::GetFirstAudioContent(pc_->local_description()->description());
2127 ASSERT_TRUE(content != NULL);
2128 EXPECT_FALSE(content->rejected);
2129
2130 content =
2131 cricket::GetFirstVideoContent(pc_->local_description()->description());
2132 ASSERT_TRUE(content != NULL);
2133 EXPECT_FALSE(content->rejected);
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00002134#ifdef HAVE_SCTP
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002135 content =
2136 cricket::GetFirstDataContent(pc_->local_description()->description());
2137 ASSERT_TRUE(content != NULL);
2138 EXPECT_TRUE(content->rejected);
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00002139#endif
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002140}
2141
deadbeef8662f942017-01-20 21:20:51 -08002142// Test that an offer can be received which offers DTLS with SDES fallback.
2143// Regression test for issue:
2144// https://bugs.chromium.org/p/webrtc/issues/detail?id=6972
2145TEST_F(PeerConnectionInterfaceTest, ReceiveDtlsSdesFallbackOffer) {
2146 FakeConstraints constraints;
2147 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2148 true);
2149 CreatePeerConnection(&constraints);
2150 // Wait for fake certificate to be generated. Previously, this is what caused
2151 // the "a=crypto" lines to be rejected.
2152 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
2153 ASSERT_NE(nullptr, fake_certificate_generator_);
2154 EXPECT_EQ_WAIT(1, fake_certificate_generator_->generated_certificates(),
2155 kTimeout);
2156 SessionDescriptionInterface* desc = webrtc::CreateSessionDescription(
2157 SessionDescriptionInterface::kOffer, kDtlsSdesFallbackSdp, nullptr);
2158 EXPECT_TRUE(DoSetSessionDescription(desc, false));
2159 CreateAnswerAsLocalDescription();
2160}
2161
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002162// Test that we can create an audio only offer and receive an answer with a
2163// limited set of audio codecs and receive an updated offer with more audio
2164// codecs, where the added codecs are not supported.
2165TEST_F(PeerConnectionInterfaceTest, ReceiveUpdatedAudioOfferWithBadCodecs) {
deadbeef293e9262017-01-11 12:28:30 -08002166 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002167 AddVoiceStream("audio_label");
2168 CreateOfferAsLocalDescription();
2169
2170 SessionDescriptionInterface* answer =
2171 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
jbauchfabe2c92015-07-16 13:43:14 -07002172 webrtc::kAudioSdp, nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002173 EXPECT_TRUE(DoSetSessionDescription(answer, false));
2174
2175 SessionDescriptionInterface* updated_offer =
2176 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
jbauchfabe2c92015-07-16 13:43:14 -07002177 webrtc::kAudioSdpWithUnsupportedCodecs,
2178 nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002179 EXPECT_TRUE(DoSetSessionDescription(updated_offer, false));
2180 CreateAnswerAsLocalDescription();
2181}
2182
deadbeefc80741f2015-10-22 13:14:45 -07002183// Test that if we're receiving (but not sending) a track, subsequent offers
2184// will have m-lines with a=recvonly.
2185TEST_F(PeerConnectionInterfaceTest, CreateSubsequentRecvOnlyOffer) {
2186 FakeConstraints constraints;
2187 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2188 true);
2189 CreatePeerConnection(&constraints);
2190 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2191 CreateAnswerAsLocalDescription();
2192
2193 // At this point we should be receiving stream 1, but not sending anything.
2194 // A new offer should be recvonly.
kwibergd1fe2812016-04-27 06:47:29 -07002195 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefc80741f2015-10-22 13:14:45 -07002196 DoCreateOffer(&offer, nullptr);
2197
2198 const cricket::ContentInfo* video_content =
2199 cricket::GetFirstVideoContent(offer->description());
2200 const cricket::VideoContentDescription* video_desc =
2201 static_cast<const cricket::VideoContentDescription*>(
2202 video_content->description);
2203 ASSERT_EQ(cricket::MD_RECVONLY, video_desc->direction());
2204
2205 const cricket::ContentInfo* audio_content =
2206 cricket::GetFirstAudioContent(offer->description());
2207 const cricket::AudioContentDescription* audio_desc =
2208 static_cast<const cricket::AudioContentDescription*>(
2209 audio_content->description);
2210 ASSERT_EQ(cricket::MD_RECVONLY, audio_desc->direction());
2211}
2212
2213// Test that if we're receiving (but not sending) a track, and the
2214// offerToReceiveVideo/offerToReceiveAudio constraints are explicitly set to
2215// false, the generated m-lines will be a=inactive.
2216TEST_F(PeerConnectionInterfaceTest, CreateSubsequentInactiveOffer) {
2217 FakeConstraints constraints;
2218 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2219 true);
2220 CreatePeerConnection(&constraints);
2221 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2222 CreateAnswerAsLocalDescription();
2223
2224 // At this point we should be receiving stream 1, but not sending anything.
2225 // A new offer would be recvonly, but we'll set the "no receive" constraints
2226 // to make it inactive.
kwibergd1fe2812016-04-27 06:47:29 -07002227 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefc80741f2015-10-22 13:14:45 -07002228 FakeConstraints offer_constraints;
2229 offer_constraints.AddMandatory(
2230 webrtc::MediaConstraintsInterface::kOfferToReceiveVideo, false);
2231 offer_constraints.AddMandatory(
2232 webrtc::MediaConstraintsInterface::kOfferToReceiveAudio, false);
2233 DoCreateOffer(&offer, &offer_constraints);
2234
2235 const cricket::ContentInfo* video_content =
2236 cricket::GetFirstVideoContent(offer->description());
2237 const cricket::VideoContentDescription* video_desc =
2238 static_cast<const cricket::VideoContentDescription*>(
2239 video_content->description);
2240 ASSERT_EQ(cricket::MD_INACTIVE, video_desc->direction());
2241
2242 const cricket::ContentInfo* audio_content =
2243 cricket::GetFirstAudioContent(offer->description());
2244 const cricket::AudioContentDescription* audio_desc =
2245 static_cast<const cricket::AudioContentDescription*>(
2246 audio_content->description);
2247 ASSERT_EQ(cricket::MD_INACTIVE, audio_desc->direction());
2248}
2249
deadbeef653b8e02015-11-11 12:55:10 -08002250// Test that we can use SetConfiguration to change the ICE servers of the
2251// PortAllocator.
2252TEST_F(PeerConnectionInterfaceTest, SetConfigurationChangesIceServers) {
2253 CreatePeerConnection();
2254
2255 PeerConnectionInterface::RTCConfiguration config;
2256 PeerConnectionInterface::IceServer server;
2257 server.uri = "stun:test_hostname";
2258 config.servers.push_back(server);
2259 EXPECT_TRUE(pc_->SetConfiguration(config));
2260
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002261 EXPECT_EQ(1u, port_allocator_->stun_servers().size());
2262 EXPECT_EQ("test_hostname",
2263 port_allocator_->stun_servers().begin()->hostname());
deadbeef653b8e02015-11-11 12:55:10 -08002264}
2265
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002266TEST_F(PeerConnectionInterfaceTest, SetConfigurationChangesCandidateFilter) {
2267 CreatePeerConnection();
2268 PeerConnectionInterface::RTCConfiguration config;
2269 config.type = PeerConnectionInterface::kRelay;
2270 EXPECT_TRUE(pc_->SetConfiguration(config));
2271 EXPECT_EQ(cricket::CF_RELAY, port_allocator_->candidate_filter());
2272}
2273
deadbeef293e9262017-01-11 12:28:30 -08002274TEST_F(PeerConnectionInterfaceTest, SetConfigurationChangesPruneTurnPortsFlag) {
2275 PeerConnectionInterface::RTCConfiguration config;
2276 config.prune_turn_ports = false;
2277 CreatePeerConnection(config, nullptr);
2278 EXPECT_FALSE(port_allocator_->prune_turn_ports());
2279
2280 config.prune_turn_ports = true;
2281 EXPECT_TRUE(pc_->SetConfiguration(config));
2282 EXPECT_TRUE(port_allocator_->prune_turn_ports());
2283}
2284
skvladd1f5fda2017-02-03 16:54:05 -08002285// Test that the ice check interval can be changed. This does not verify that
2286// the setting makes it all the way to P2PTransportChannel, as that would
2287// require a very complex set of mocks.
2288TEST_F(PeerConnectionInterfaceTest, SetConfigurationChangesIceCheckInterval) {
2289 PeerConnectionInterface::RTCConfiguration config;
2290 config.ice_check_min_interval = rtc::Optional<int>();
2291 CreatePeerConnection(config, nullptr);
2292 config.ice_check_min_interval = rtc::Optional<int>(100);
2293 EXPECT_TRUE(pc_->SetConfiguration(config));
2294 PeerConnectionInterface::RTCConfiguration new_config =
2295 pc_->GetConfiguration();
2296 EXPECT_EQ(new_config.ice_check_min_interval, rtc::Optional<int>(100));
2297}
2298
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002299// Test that when SetConfiguration changes both the pool size and other
2300// attributes, the pooled session is created with the updated attributes.
2301TEST_F(PeerConnectionInterfaceTest,
2302 SetConfigurationCreatesPooledSessionCorrectly) {
2303 CreatePeerConnection();
2304 PeerConnectionInterface::RTCConfiguration config;
2305 config.ice_candidate_pool_size = 1;
2306 PeerConnectionInterface::IceServer server;
2307 server.uri = kStunAddressOnly;
2308 config.servers.push_back(server);
2309 config.type = PeerConnectionInterface::kRelay;
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07002310 EXPECT_TRUE(pc_->SetConfiguration(config));
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002311
2312 const cricket::FakePortAllocatorSession* session =
2313 static_cast<const cricket::FakePortAllocatorSession*>(
2314 port_allocator_->GetPooledSession());
2315 ASSERT_NE(nullptr, session);
2316 EXPECT_EQ(1UL, session->stun_servers().size());
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002317}
2318
deadbeef293e9262017-01-11 12:28:30 -08002319// Test that after SetLocalDescription, changing the pool size is not allowed,
2320// and an invalid modification error is returned.
deadbeef6de92f92016-12-12 18:49:32 -08002321TEST_F(PeerConnectionInterfaceTest,
2322 CantChangePoolSizeAfterSetLocalDescription) {
2323 CreatePeerConnection();
2324 // Start by setting a size of 1.
2325 PeerConnectionInterface::RTCConfiguration config;
2326 config.ice_candidate_pool_size = 1;
2327 EXPECT_TRUE(pc_->SetConfiguration(config));
2328
2329 // Set remote offer; can still change pool size at this point.
2330 CreateOfferAsRemoteDescription();
2331 config.ice_candidate_pool_size = 2;
2332 EXPECT_TRUE(pc_->SetConfiguration(config));
2333
2334 // Set local answer; now it's too late.
2335 CreateAnswerAsLocalDescription();
2336 config.ice_candidate_pool_size = 3;
deadbeef293e9262017-01-11 12:28:30 -08002337 RTCError error;
2338 EXPECT_FALSE(pc_->SetConfiguration(config, &error));
2339 EXPECT_EQ(RTCErrorType::INVALID_MODIFICATION, error.type());
2340}
2341
deadbeef42a42632017-03-10 15:18:00 -08002342// Test that after setting an answer, extra pooled sessions are discarded. The
2343// ICE candidate pool is only intended to be used for the first offer/answer.
2344TEST_F(PeerConnectionInterfaceTest,
2345 ExtraPooledSessionsDiscardedAfterApplyingAnswer) {
2346 CreatePeerConnection();
2347
2348 // Set a larger-than-necessary size.
2349 PeerConnectionInterface::RTCConfiguration config;
2350 config.ice_candidate_pool_size = 4;
2351 EXPECT_TRUE(pc_->SetConfiguration(config));
2352
2353 // Do offer/answer.
2354 CreateOfferAsRemoteDescription();
2355 CreateAnswerAsLocalDescription();
2356
2357 // Expect no pooled sessions to be left.
2358 const cricket::PortAllocatorSession* session =
2359 port_allocator_->GetPooledSession();
2360 EXPECT_EQ(nullptr, session);
2361}
2362
2363// After Close is called, pooled candidates should be discarded so as to not
2364// waste network resources.
2365TEST_F(PeerConnectionInterfaceTest, PooledSessionsDiscardedAfterClose) {
2366 CreatePeerConnection();
2367
2368 PeerConnectionInterface::RTCConfiguration config;
2369 config.ice_candidate_pool_size = 3;
2370 EXPECT_TRUE(pc_->SetConfiguration(config));
2371 pc_->Close();
2372
2373 // Expect no pooled sessions to be left.
2374 const cricket::PortAllocatorSession* session =
2375 port_allocator_->GetPooledSession();
2376 EXPECT_EQ(nullptr, session);
2377}
2378
deadbeef293e9262017-01-11 12:28:30 -08002379// Test that SetConfiguration returns an invalid modification error if
2380// modifying a field in the configuration that isn't allowed to be modified.
2381TEST_F(PeerConnectionInterfaceTest,
2382 SetConfigurationReturnsInvalidModificationError) {
2383 PeerConnectionInterface::RTCConfiguration config;
2384 config.bundle_policy = PeerConnectionInterface::kBundlePolicyBalanced;
2385 config.rtcp_mux_policy = PeerConnectionInterface::kRtcpMuxPolicyNegotiate;
2386 config.continual_gathering_policy = PeerConnectionInterface::GATHER_ONCE;
2387 CreatePeerConnection(config, nullptr);
2388
2389 PeerConnectionInterface::RTCConfiguration modified_config = config;
2390 modified_config.bundle_policy =
2391 PeerConnectionInterface::kBundlePolicyMaxBundle;
2392 RTCError error;
2393 EXPECT_FALSE(pc_->SetConfiguration(modified_config, &error));
2394 EXPECT_EQ(RTCErrorType::INVALID_MODIFICATION, error.type());
2395
2396 modified_config = config;
2397 modified_config.rtcp_mux_policy =
2398 PeerConnectionInterface::kRtcpMuxPolicyRequire;
2399 error.set_type(RTCErrorType::NONE);
2400 EXPECT_FALSE(pc_->SetConfiguration(modified_config, &error));
2401 EXPECT_EQ(RTCErrorType::INVALID_MODIFICATION, error.type());
2402
2403 modified_config = config;
2404 modified_config.continual_gathering_policy =
2405 PeerConnectionInterface::GATHER_CONTINUALLY;
2406 error.set_type(RTCErrorType::NONE);
2407 EXPECT_FALSE(pc_->SetConfiguration(modified_config, &error));
2408 EXPECT_EQ(RTCErrorType::INVALID_MODIFICATION, error.type());
2409}
2410
2411// Test that SetConfiguration returns a range error if the candidate pool size
2412// is negative or larger than allowed by the spec.
2413TEST_F(PeerConnectionInterfaceTest,
2414 SetConfigurationReturnsRangeErrorForBadCandidatePoolSize) {
2415 PeerConnectionInterface::RTCConfiguration config;
2416 CreatePeerConnection(config, nullptr);
2417
2418 config.ice_candidate_pool_size = -1;
2419 RTCError error;
2420 EXPECT_FALSE(pc_->SetConfiguration(config, &error));
2421 EXPECT_EQ(RTCErrorType::INVALID_RANGE, error.type());
2422
2423 config.ice_candidate_pool_size = INT_MAX;
2424 error.set_type(RTCErrorType::NONE);
2425 EXPECT_FALSE(pc_->SetConfiguration(config, &error));
2426 EXPECT_EQ(RTCErrorType::INVALID_RANGE, error.type());
2427}
2428
2429// Test that SetConfiguration returns a syntax error if parsing an ICE server
2430// URL failed.
2431TEST_F(PeerConnectionInterfaceTest,
2432 SetConfigurationReturnsSyntaxErrorFromBadIceUrls) {
2433 PeerConnectionInterface::RTCConfiguration config;
2434 CreatePeerConnection(config, nullptr);
2435
2436 PeerConnectionInterface::IceServer bad_server;
2437 bad_server.uri = "stunn:www.example.com";
2438 config.servers.push_back(bad_server);
2439 RTCError error;
2440 EXPECT_FALSE(pc_->SetConfiguration(config, &error));
2441 EXPECT_EQ(RTCErrorType::SYNTAX_ERROR, error.type());
2442}
2443
2444// Test that SetConfiguration returns an invalid parameter error if a TURN
2445// IceServer is missing a username or password.
2446TEST_F(PeerConnectionInterfaceTest,
2447 SetConfigurationReturnsInvalidParameterIfCredentialsMissing) {
2448 PeerConnectionInterface::RTCConfiguration config;
2449 CreatePeerConnection(config, nullptr);
2450
2451 PeerConnectionInterface::IceServer bad_server;
2452 bad_server.uri = "turn:www.example.com";
2453 // Missing password.
2454 bad_server.username = "foo";
2455 config.servers.push_back(bad_server);
2456 RTCError error;
2457 EXPECT_FALSE(pc_->SetConfiguration(config, &error));
2458 EXPECT_EQ(RTCErrorType::INVALID_PARAMETER, error.type());
deadbeef6de92f92016-12-12 18:49:32 -08002459}
2460
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002461// Test that PeerConnection::Close changes the states to closed and all remote
2462// tracks change state to ended.
2463TEST_F(PeerConnectionInterfaceTest, CloseAndTestStreamsAndStates) {
2464 // Initialize a PeerConnection and negotiate local and remote session
2465 // description.
2466 InitiateCall();
2467 ASSERT_EQ(1u, pc_->local_streams()->count());
2468 ASSERT_EQ(1u, pc_->remote_streams()->count());
2469
2470 pc_->Close();
2471
2472 EXPECT_EQ(PeerConnectionInterface::kClosed, pc_->signaling_state());
2473 EXPECT_EQ(PeerConnectionInterface::kIceConnectionClosed,
2474 pc_->ice_connection_state());
2475 EXPECT_EQ(PeerConnectionInterface::kIceGatheringComplete,
2476 pc_->ice_gathering_state());
2477
2478 EXPECT_EQ(1u, pc_->local_streams()->count());
2479 EXPECT_EQ(1u, pc_->remote_streams()->count());
2480
zhihuang9763d562016-08-05 11:14:50 -07002481 rtc::scoped_refptr<MediaStreamInterface> remote_stream =
2482 pc_->remote_streams()->at(0);
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002483 // Track state may be updated asynchronously.
perkjd61bf802016-03-24 03:16:19 -07002484 EXPECT_EQ_WAIT(MediaStreamTrackInterface::kEnded,
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002485 remote_stream->GetAudioTracks()[0]->state(), kTimeout);
2486 EXPECT_EQ_WAIT(MediaStreamTrackInterface::kEnded,
2487 remote_stream->GetVideoTracks()[0]->state(), kTimeout);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002488}
2489
2490// Test that PeerConnection methods fails gracefully after
2491// PeerConnection::Close has been called.
2492TEST_F(PeerConnectionInterfaceTest, CloseAndTestMethods) {
deadbeef293e9262017-01-11 12:28:30 -08002493 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002494 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
2495 CreateOfferAsRemoteDescription();
2496 CreateAnswerAsLocalDescription();
2497
2498 ASSERT_EQ(1u, pc_->local_streams()->count());
zhihuang9763d562016-08-05 11:14:50 -07002499 rtc::scoped_refptr<MediaStreamInterface> local_stream =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002500 pc_->local_streams()->at(0);
2501
2502 pc_->Close();
2503
2504 pc_->RemoveStream(local_stream);
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +00002505 EXPECT_FALSE(pc_->AddStream(local_stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002506
2507 ASSERT_FALSE(local_stream->GetAudioTracks().empty());
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002508 rtc::scoped_refptr<webrtc::DtmfSenderInterface> dtmf_sender(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002509 pc_->CreateDtmfSender(local_stream->GetAudioTracks()[0]));
wu@webrtc.org66037362013-08-13 00:09:35 +00002510 EXPECT_TRUE(NULL == dtmf_sender); // local stream has been removed.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002511
2512 EXPECT_TRUE(pc_->CreateDataChannel("test", NULL) == NULL);
2513
2514 EXPECT_TRUE(pc_->local_description() != NULL);
2515 EXPECT_TRUE(pc_->remote_description() != NULL);
2516
kwibergd1fe2812016-04-27 06:47:29 -07002517 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07002518 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
kwibergd1fe2812016-04-27 06:47:29 -07002519 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -07002520 EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002521
2522 std::string sdp;
2523 ASSERT_TRUE(pc_->remote_description()->ToString(&sdp));
2524 SessionDescriptionInterface* remote_offer =
2525 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
2526 sdp, NULL);
2527 EXPECT_FALSE(DoSetRemoteDescription(remote_offer));
2528
2529 ASSERT_TRUE(pc_->local_description()->ToString(&sdp));
2530 SessionDescriptionInterface* local_offer =
2531 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
2532 sdp, NULL);
2533 EXPECT_FALSE(DoSetLocalDescription(local_offer));
2534}
2535
2536// Test that GetStats can still be called after PeerConnection::Close.
2537TEST_F(PeerConnectionInterfaceTest, CloseAndGetStats) {
2538 InitiateCall();
2539 pc_->Close();
2540 DoGetStats(NULL);
2541}
deadbeefab9b2d12015-10-14 11:33:11 -07002542
2543// NOTE: The series of tests below come from what used to be
2544// mediastreamsignaling_unittest.cc, and are mostly aimed at testing that
2545// setting a remote or local description has the expected effects.
2546
2547// This test verifies that the remote MediaStreams corresponding to a received
2548// SDP string is created. In this test the two separate MediaStreams are
2549// signaled.
2550TEST_F(PeerConnectionInterfaceTest, UpdateRemoteStreams) {
2551 FakeConstraints constraints;
2552 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2553 true);
2554 CreatePeerConnection(&constraints);
2555 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2556
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002557 rtc::scoped_refptr<StreamCollection> reference(CreateStreamCollection(1, 1));
deadbeefab9b2d12015-10-14 11:33:11 -07002558 EXPECT_TRUE(
2559 CompareStreamCollections(observer_.remote_streams(), reference.get()));
2560 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2561 EXPECT_TRUE(remote_stream->GetVideoTracks()[0]->GetSource() != nullptr);
2562
2563 // Create a session description based on another SDP with another
2564 // MediaStream.
2565 CreateAndSetRemoteOffer(kSdpStringWithStream1And2);
2566
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002567 rtc::scoped_refptr<StreamCollection> reference2(CreateStreamCollection(2, 1));
deadbeefab9b2d12015-10-14 11:33:11 -07002568 EXPECT_TRUE(
2569 CompareStreamCollections(observer_.remote_streams(), reference2.get()));
2570}
2571
2572// This test verifies that when remote tracks are added/removed from SDP, the
2573// created remote streams are updated appropriately.
2574TEST_F(PeerConnectionInterfaceTest,
2575 AddRemoveTrackFromExistingRemoteMediaStream) {
2576 FakeConstraints constraints;
2577 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2578 true);
2579 CreatePeerConnection(&constraints);
kwibergd1fe2812016-04-27 06:47:29 -07002580 std::unique_ptr<SessionDescriptionInterface> desc_ms1 =
kwiberg2bbff992016-03-16 11:03:04 -07002581 CreateSessionDescriptionAndReference(1, 1);
deadbeefab9b2d12015-10-14 11:33:11 -07002582 EXPECT_TRUE(DoSetRemoteDescription(desc_ms1.release()));
2583 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
2584 reference_collection_));
2585
2586 // Add extra audio and video tracks to the same MediaStream.
kwibergd1fe2812016-04-27 06:47:29 -07002587 std::unique_ptr<SessionDescriptionInterface> desc_ms1_two_tracks =
kwiberg2bbff992016-03-16 11:03:04 -07002588 CreateSessionDescriptionAndReference(2, 2);
deadbeefab9b2d12015-10-14 11:33:11 -07002589 EXPECT_TRUE(DoSetRemoteDescription(desc_ms1_two_tracks.release()));
2590 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
2591 reference_collection_));
zhihuang9763d562016-08-05 11:14:50 -07002592 rtc::scoped_refptr<AudioTrackInterface> audio_track2 =
perkjd61bf802016-03-24 03:16:19 -07002593 observer_.remote_streams()->at(0)->GetAudioTracks()[1];
2594 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, audio_track2->state());
zhihuang9763d562016-08-05 11:14:50 -07002595 rtc::scoped_refptr<VideoTrackInterface> video_track2 =
perkjd61bf802016-03-24 03:16:19 -07002596 observer_.remote_streams()->at(0)->GetVideoTracks()[1];
2597 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, video_track2->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002598
2599 // Remove the extra audio and video tracks.
kwibergd1fe2812016-04-27 06:47:29 -07002600 std::unique_ptr<SessionDescriptionInterface> desc_ms2 =
kwiberg2bbff992016-03-16 11:03:04 -07002601 CreateSessionDescriptionAndReference(1, 1);
perkjd61bf802016-03-24 03:16:19 -07002602 MockTrackObserver audio_track_observer(audio_track2);
2603 MockTrackObserver video_track_observer(video_track2);
2604
2605 EXPECT_CALL(audio_track_observer, OnChanged()).Times(Exactly(1));
2606 EXPECT_CALL(video_track_observer, OnChanged()).Times(Exactly(1));
deadbeefab9b2d12015-10-14 11:33:11 -07002607 EXPECT_TRUE(DoSetRemoteDescription(desc_ms2.release()));
2608 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
2609 reference_collection_));
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002610 // Track state may be updated asynchronously.
perkjd61bf802016-03-24 03:16:19 -07002611 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002612 audio_track2->state(), kTimeout);
2613 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
2614 video_track2->state(), kTimeout);
deadbeefab9b2d12015-10-14 11:33:11 -07002615}
2616
2617// This tests that remote tracks are ended if a local session description is set
2618// that rejects the media content type.
2619TEST_F(PeerConnectionInterfaceTest, RejectMediaContent) {
2620 FakeConstraints constraints;
2621 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2622 true);
2623 CreatePeerConnection(&constraints);
2624 // First create and set a remote offer, then reject its video content in our
2625 // answer.
2626 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2627 ASSERT_EQ(1u, observer_.remote_streams()->count());
2628 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2629 ASSERT_EQ(1u, remote_stream->GetVideoTracks().size());
2630 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2631
2632 rtc::scoped_refptr<webrtc::VideoTrackInterface> remote_video =
2633 remote_stream->GetVideoTracks()[0];
2634 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, remote_video->state());
2635 rtc::scoped_refptr<webrtc::AudioTrackInterface> remote_audio =
2636 remote_stream->GetAudioTracks()[0];
2637 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, remote_audio->state());
2638
kwibergd1fe2812016-04-27 06:47:29 -07002639 std::unique_ptr<SessionDescriptionInterface> local_answer;
kwiberg2bbff992016-03-16 11:03:04 -07002640 EXPECT_TRUE(DoCreateAnswer(&local_answer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07002641 cricket::ContentInfo* video_info =
2642 local_answer->description()->GetContentByName("video");
2643 video_info->rejected = true;
2644 EXPECT_TRUE(DoSetLocalDescription(local_answer.release()));
2645 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kEnded, remote_video->state());
2646 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, remote_audio->state());
2647
2648 // Now create an offer where we reject both video and audio.
kwibergd1fe2812016-04-27 06:47:29 -07002649 std::unique_ptr<SessionDescriptionInterface> local_offer;
kwiberg2bbff992016-03-16 11:03:04 -07002650 EXPECT_TRUE(DoCreateOffer(&local_offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07002651 video_info = local_offer->description()->GetContentByName("video");
2652 ASSERT_TRUE(video_info != nullptr);
2653 video_info->rejected = true;
2654 cricket::ContentInfo* audio_info =
2655 local_offer->description()->GetContentByName("audio");
2656 ASSERT_TRUE(audio_info != nullptr);
2657 audio_info->rejected = true;
2658 EXPECT_TRUE(DoSetLocalDescription(local_offer.release()));
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002659 // Track state may be updated asynchronously.
perkjd61bf802016-03-24 03:16:19 -07002660 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002661 remote_audio->state(), kTimeout);
2662 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
2663 remote_video->state(), kTimeout);
deadbeefab9b2d12015-10-14 11:33:11 -07002664}
2665
2666// This tests that we won't crash if the remote track has been removed outside
2667// of PeerConnection and then PeerConnection tries to reject the track.
2668TEST_F(PeerConnectionInterfaceTest, RemoveTrackThenRejectMediaContent) {
2669 FakeConstraints constraints;
2670 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2671 true);
2672 CreatePeerConnection(&constraints);
2673 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2674 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2675 remote_stream->RemoveTrack(remote_stream->GetVideoTracks()[0]);
2676 remote_stream->RemoveTrack(remote_stream->GetAudioTracks()[0]);
2677
kwibergd1fe2812016-04-27 06:47:29 -07002678 std::unique_ptr<SessionDescriptionInterface> local_answer(
deadbeefab9b2d12015-10-14 11:33:11 -07002679 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
2680 kSdpStringWithStream1, nullptr));
2681 cricket::ContentInfo* video_info =
2682 local_answer->description()->GetContentByName("video");
2683 video_info->rejected = true;
2684 cricket::ContentInfo* audio_info =
2685 local_answer->description()->GetContentByName("audio");
2686 audio_info->rejected = true;
2687 EXPECT_TRUE(DoSetLocalDescription(local_answer.release()));
2688
2689 // No crash is a pass.
2690}
2691
deadbeef5e97fb52015-10-15 12:49:08 -07002692// This tests that if a recvonly remote description is set, no remote streams
2693// will be created, even if the description contains SSRCs/MSIDs.
2694// See: https://code.google.com/p/webrtc/issues/detail?id=5054
2695TEST_F(PeerConnectionInterfaceTest, RecvonlyDescriptionDoesntCreateStream) {
2696 FakeConstraints constraints;
2697 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2698 true);
2699 CreatePeerConnection(&constraints);
2700
2701 std::string recvonly_offer = kSdpStringWithStream1;
2702 rtc::replace_substrs(kSendrecv, strlen(kSendrecv), kRecvonly,
2703 strlen(kRecvonly), &recvonly_offer);
2704 CreateAndSetRemoteOffer(recvonly_offer);
2705
2706 EXPECT_EQ(0u, observer_.remote_streams()->count());
2707}
2708
deadbeefab9b2d12015-10-14 11:33:11 -07002709// This tests that a default MediaStream is created if a remote session
2710// description doesn't contain any streams and no MSID support.
2711// It also tests that the default stream is updated if a video m-line is added
2712// in a subsequent session description.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002713TEST_F(PeerConnectionInterfaceTest, SdpWithoutMsidCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002714 FakeConstraints constraints;
2715 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2716 true);
2717 CreatePeerConnection(&constraints);
2718 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2719
2720 ASSERT_EQ(1u, observer_.remote_streams()->count());
2721 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2722
2723 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2724 EXPECT_EQ(0u, remote_stream->GetVideoTracks().size());
2725 EXPECT_EQ("default", remote_stream->label());
2726
2727 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2728 ASSERT_EQ(1u, observer_.remote_streams()->count());
2729 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2730 EXPECT_EQ("defaulta0", remote_stream->GetAudioTracks()[0]->id());
deadbeef884f5852016-01-15 09:20:04 -08002731 EXPECT_EQ(MediaStreamTrackInterface::kLive,
2732 remote_stream->GetAudioTracks()[0]->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002733 ASSERT_EQ(1u, remote_stream->GetVideoTracks().size());
2734 EXPECT_EQ("defaultv0", remote_stream->GetVideoTracks()[0]->id());
deadbeef884f5852016-01-15 09:20:04 -08002735 EXPECT_EQ(MediaStreamTrackInterface::kLive,
2736 remote_stream->GetVideoTracks()[0]->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002737}
2738
2739// This tests that a default MediaStream is created if a remote session
2740// description doesn't contain any streams and media direction is send only.
2741TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002742 SendOnlySdpWithoutMsidCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002743 FakeConstraints constraints;
2744 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2745 true);
2746 CreatePeerConnection(&constraints);
2747 CreateAndSetRemoteOffer(kSdpStringSendOnlyWithoutStreams);
2748
2749 ASSERT_EQ(1u, observer_.remote_streams()->count());
2750 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2751
2752 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2753 EXPECT_EQ(1u, remote_stream->GetVideoTracks().size());
2754 EXPECT_EQ("default", remote_stream->label());
2755}
2756
2757// This tests that it won't crash when PeerConnection tries to remove
2758// a remote track that as already been removed from the MediaStream.
2759TEST_F(PeerConnectionInterfaceTest, RemoveAlreadyGoneRemoteStream) {
2760 FakeConstraints constraints;
2761 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2762 true);
2763 CreatePeerConnection(&constraints);
2764 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2765 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2766 remote_stream->RemoveTrack(remote_stream->GetAudioTracks()[0]);
2767 remote_stream->RemoveTrack(remote_stream->GetVideoTracks()[0]);
2768
2769 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2770
2771 // No crash is a pass.
2772}
2773
2774// This tests that a default MediaStream is created if the remote session
2775// description doesn't contain any streams and don't contain an indication if
2776// MSID is supported.
2777TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002778 SdpWithoutMsidAndStreamsCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002779 FakeConstraints constraints;
2780 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2781 true);
2782 CreatePeerConnection(&constraints);
2783 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2784
2785 ASSERT_EQ(1u, observer_.remote_streams()->count());
2786 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2787 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2788 EXPECT_EQ(1u, remote_stream->GetVideoTracks().size());
2789}
2790
2791// This tests that a default MediaStream is not created if the remote session
2792// description doesn't contain any streams but does support MSID.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002793TEST_F(PeerConnectionInterfaceTest, SdpWithMsidDontCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002794 FakeConstraints constraints;
2795 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2796 true);
2797 CreatePeerConnection(&constraints);
2798 CreateAndSetRemoteOffer(kSdpStringWithMsidWithoutStreams);
2799 EXPECT_EQ(0u, observer_.remote_streams()->count());
2800}
2801
deadbeefbda7e0b2015-12-08 17:13:40 -08002802// This tests that when setting a new description, the old default tracks are
2803// not destroyed and recreated.
2804// See: https://bugs.chromium.org/p/webrtc/issues/detail?id=5250
Stefan Holmer102362b2016-03-18 09:39:07 +01002805TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002806 DefaultTracksNotDestroyedAndRecreated) {
deadbeefbda7e0b2015-12-08 17:13:40 -08002807 FakeConstraints constraints;
2808 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2809 true);
2810 CreatePeerConnection(&constraints);
2811 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2812
2813 ASSERT_EQ(1u, observer_.remote_streams()->count());
2814 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2815 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2816
2817 // Set the track to "disabled", then set a new description and ensure the
2818 // track is still disabled, which ensures it hasn't been recreated.
2819 remote_stream->GetAudioTracks()[0]->set_enabled(false);
2820 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2821 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2822 EXPECT_FALSE(remote_stream->GetAudioTracks()[0]->enabled());
2823}
2824
deadbeefab9b2d12015-10-14 11:33:11 -07002825// This tests that a default MediaStream is not created if a remote session
2826// description is updated to not have any MediaStreams.
2827TEST_F(PeerConnectionInterfaceTest, VerifyDefaultStreamIsNotCreated) {
2828 FakeConstraints constraints;
2829 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2830 true);
2831 CreatePeerConnection(&constraints);
2832 CreateAndSetRemoteOffer(kSdpStringWithStream1);
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002833 rtc::scoped_refptr<StreamCollection> reference(CreateStreamCollection(1, 1));
deadbeefab9b2d12015-10-14 11:33:11 -07002834 EXPECT_TRUE(
2835 CompareStreamCollections(observer_.remote_streams(), reference.get()));
2836
2837 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2838 EXPECT_EQ(0u, observer_.remote_streams()->count());
2839}
2840
2841// This tests that an RtpSender is created when the local description is set
2842// after adding a local stream.
2843// TODO(deadbeef): This test and the one below it need to be updated when
2844// an RtpSender's lifetime isn't determined by when a local description is set.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002845TEST_F(PeerConnectionInterfaceTest, LocalDescriptionChanged) {
deadbeefab9b2d12015-10-14 11:33:11 -07002846 FakeConstraints constraints;
2847 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2848 true);
2849 CreatePeerConnection(&constraints);
deadbeefab9b2d12015-10-14 11:33:11 -07002850
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002851 // Create an offer with 1 stream with 2 tracks of each type.
2852 rtc::scoped_refptr<StreamCollection> stream_collection =
2853 CreateStreamCollection(1, 2);
2854 pc_->AddStream(stream_collection->at(0));
2855 std::unique_ptr<SessionDescriptionInterface> offer;
2856 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2857 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
deadbeefab9b2d12015-10-14 11:33:11 -07002858
deadbeefab9b2d12015-10-14 11:33:11 -07002859 auto senders = pc_->GetSenders();
2860 EXPECT_EQ(4u, senders.size());
2861 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2862 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2863 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[1]));
2864 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[1]));
2865
2866 // Remove an audio and video track.
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002867 pc_->RemoveStream(stream_collection->at(0));
2868 stream_collection = CreateStreamCollection(1, 1);
2869 pc_->AddStream(stream_collection->at(0));
2870 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2871 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
2872
deadbeefab9b2d12015-10-14 11:33:11 -07002873 senders = pc_->GetSenders();
2874 EXPECT_EQ(2u, senders.size());
2875 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2876 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2877 EXPECT_FALSE(ContainsSender(senders, kAudioTracks[1]));
2878 EXPECT_FALSE(ContainsSender(senders, kVideoTracks[1]));
2879}
2880
2881// This tests that an RtpSender is created when the local description is set
2882// before adding a local stream.
2883TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002884 AddLocalStreamAfterLocalDescriptionChanged) {
deadbeefab9b2d12015-10-14 11:33:11 -07002885 FakeConstraints constraints;
2886 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2887 true);
2888 CreatePeerConnection(&constraints);
deadbeefab9b2d12015-10-14 11:33:11 -07002889
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002890 rtc::scoped_refptr<StreamCollection> stream_collection =
2891 CreateStreamCollection(1, 2);
2892 // Add a stream to create the offer, but remove it afterwards.
2893 pc_->AddStream(stream_collection->at(0));
2894 std::unique_ptr<SessionDescriptionInterface> offer;
2895 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2896 pc_->RemoveStream(stream_collection->at(0));
deadbeefab9b2d12015-10-14 11:33:11 -07002897
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002898 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
deadbeefab9b2d12015-10-14 11:33:11 -07002899 auto senders = pc_->GetSenders();
2900 EXPECT_EQ(0u, senders.size());
2901
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002902 pc_->AddStream(stream_collection->at(0));
deadbeefab9b2d12015-10-14 11:33:11 -07002903 senders = pc_->GetSenders();
2904 EXPECT_EQ(4u, senders.size());
2905 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2906 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2907 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[1]));
2908 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[1]));
2909}
2910
2911// This tests that the expected behavior occurs if the SSRC on a local track is
2912// changed when SetLocalDescription is called.
2913TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002914 ChangeSsrcOnTrackInLocalSessionDescription) {
deadbeefab9b2d12015-10-14 11:33:11 -07002915 FakeConstraints constraints;
2916 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2917 true);
2918 CreatePeerConnection(&constraints);
deadbeefab9b2d12015-10-14 11:33:11 -07002919
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002920 rtc::scoped_refptr<StreamCollection> stream_collection =
2921 CreateStreamCollection(2, 1);
2922 pc_->AddStream(stream_collection->at(0));
2923 std::unique_ptr<SessionDescriptionInterface> offer;
2924 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2925 // Grab a copy of the offer before it gets passed into the PC.
2926 std::unique_ptr<JsepSessionDescription> modified_offer(
2927 new JsepSessionDescription(JsepSessionDescription::kOffer));
2928 modified_offer->Initialize(offer->description()->Copy(), offer->session_id(),
2929 offer->session_version());
2930 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
deadbeefab9b2d12015-10-14 11:33:11 -07002931
deadbeefab9b2d12015-10-14 11:33:11 -07002932 auto senders = pc_->GetSenders();
2933 EXPECT_EQ(2u, senders.size());
2934 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2935 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2936
2937 // Change the ssrc of the audio and video track.
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002938 cricket::MediaContentDescription* desc =
2939 cricket::GetFirstAudioContentDescription(modified_offer->description());
2940 ASSERT_TRUE(desc != NULL);
2941 for (StreamParams& stream : desc->mutable_streams()) {
2942 for (unsigned int& ssrc : stream.ssrcs) {
2943 ++ssrc;
2944 }
2945 }
deadbeefab9b2d12015-10-14 11:33:11 -07002946
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002947 desc =
2948 cricket::GetFirstVideoContentDescription(modified_offer->description());
2949 ASSERT_TRUE(desc != NULL);
2950 for (StreamParams& stream : desc->mutable_streams()) {
2951 for (unsigned int& ssrc : stream.ssrcs) {
2952 ++ssrc;
2953 }
2954 }
2955
2956 EXPECT_TRUE(DoSetLocalDescription(modified_offer.release()));
deadbeefab9b2d12015-10-14 11:33:11 -07002957 senders = pc_->GetSenders();
2958 EXPECT_EQ(2u, senders.size());
2959 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2960 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2961 // TODO(deadbeef): Once RtpSenders expose parameters, check that the SSRC
2962 // changed.
2963}
2964
2965// This tests that the expected behavior occurs if a new session description is
2966// set with the same tracks, but on a different MediaStream.
Stefan Holmer55d6e7c2016-03-17 16:26:40 +01002967TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002968 SignalSameTracksInSeparateMediaStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002969 FakeConstraints constraints;
2970 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2971 true);
2972 CreatePeerConnection(&constraints);
deadbeefab9b2d12015-10-14 11:33:11 -07002973
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002974 rtc::scoped_refptr<StreamCollection> stream_collection =
2975 CreateStreamCollection(2, 1);
2976 pc_->AddStream(stream_collection->at(0));
2977 std::unique_ptr<SessionDescriptionInterface> offer;
2978 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2979 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
deadbeefab9b2d12015-10-14 11:33:11 -07002980
deadbeefab9b2d12015-10-14 11:33:11 -07002981 auto senders = pc_->GetSenders();
2982 EXPECT_EQ(2u, senders.size());
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002983 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0], kStreams[0]));
2984 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0], kStreams[0]));
deadbeefab9b2d12015-10-14 11:33:11 -07002985
2986 // Add a new MediaStream but with the same tracks as in the first stream.
2987 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream_1(
2988 webrtc::MediaStream::Create(kStreams[1]));
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002989 stream_1->AddTrack(stream_collection->at(0)->GetVideoTracks()[0]);
2990 stream_1->AddTrack(stream_collection->at(0)->GetAudioTracks()[0]);
deadbeefab9b2d12015-10-14 11:33:11 -07002991 pc_->AddStream(stream_1);
2992
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002993 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2994 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
deadbeefab9b2d12015-10-14 11:33:11 -07002995
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002996 auto new_senders = pc_->GetSenders();
2997 // Should be the same senders as before, but with updated stream id.
2998 // Note that this behavior is subject to change in the future.
2999 // We may decide the PC should ignore existing tracks in AddStream.
3000 EXPECT_EQ(senders, new_senders);
3001 EXPECT_TRUE(ContainsSender(new_senders, kAudioTracks[0], kStreams[1]));
3002 EXPECT_TRUE(ContainsSender(new_senders, kVideoTracks[0], kStreams[1]));
deadbeefab9b2d12015-10-14 11:33:11 -07003003}
3004
zhihuang81c3a032016-11-17 12:06:24 -08003005// This tests that PeerConnectionObserver::OnAddTrack is correctly called.
3006TEST_F(PeerConnectionInterfaceTest, OnAddTrackCallback) {
3007 FakeConstraints constraints;
3008 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
3009 true);
3010 CreatePeerConnection(&constraints);
3011 CreateAndSetRemoteOffer(kSdpStringWithStream1AudioTrackOnly);
3012 EXPECT_EQ(observer_.num_added_tracks_, 1);
3013 EXPECT_EQ(observer_.last_added_track_label_, kAudioTracks[0]);
3014
3015 // Create and set the updated remote SDP.
3016 CreateAndSetRemoteOffer(kSdpStringWithStream1);
3017 EXPECT_EQ(observer_.num_added_tracks_, 2);
3018 EXPECT_EQ(observer_.last_added_track_label_, kVideoTracks[0]);
3019}
3020
deadbeefd1a38b52016-12-10 13:15:33 -08003021// Test that when SetConfiguration is called and the configuration is
3022// changing, the next offer causes an ICE restart.
3023TEST_F(PeerConnectionInterfaceTest, SetConfigurationCausingIceRetart) {
3024 PeerConnectionInterface::RTCConfiguration config;
3025 config.type = PeerConnectionInterface::kRelay;
3026 // Need to pass default constraints to prevent disabling of DTLS...
3027 FakeConstraints default_constraints;
3028 CreatePeerConnection(config, &default_constraints);
3029 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
3030
3031 // Do initial offer/answer so there's something to restart.
3032 CreateOfferAsLocalDescription();
3033 CreateAnswerAsRemoteDescription(kSdpStringWithStream1);
3034
3035 // Grab the ufrags.
3036 std::vector<std::string> initial_ufrags = GetUfrags(pc_->local_description());
3037
3038 // Change ICE policy, which should trigger an ICE restart on the next offer.
3039 config.type = PeerConnectionInterface::kAll;
3040 EXPECT_TRUE(pc_->SetConfiguration(config));
3041 CreateOfferAsLocalDescription();
3042
3043 // Grab the new ufrags.
3044 std::vector<std::string> subsequent_ufrags =
3045 GetUfrags(pc_->local_description());
3046
3047 // Sanity check.
3048 EXPECT_EQ(initial_ufrags.size(), subsequent_ufrags.size());
3049 // Check that each ufrag is different.
3050 for (int i = 0; i < static_cast<int>(initial_ufrags.size()); ++i) {
3051 EXPECT_NE(initial_ufrags[i], subsequent_ufrags[i]);
3052 }
3053}
3054
3055// Test that when SetConfiguration is called and the configuration *isn't*
3056// changing, the next offer does *not* cause an ICE restart.
3057TEST_F(PeerConnectionInterfaceTest, SetConfigurationNotCausingIceRetart) {
3058 PeerConnectionInterface::RTCConfiguration config;
3059 config.type = PeerConnectionInterface::kRelay;
3060 // Need to pass default constraints to prevent disabling of DTLS...
3061 FakeConstraints default_constraints;
3062 CreatePeerConnection(config, &default_constraints);
3063 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
3064
3065 // Do initial offer/answer so there's something to restart.
3066 CreateOfferAsLocalDescription();
3067 CreateAnswerAsRemoteDescription(kSdpStringWithStream1);
3068
3069 // Grab the ufrags.
3070 std::vector<std::string> initial_ufrags = GetUfrags(pc_->local_description());
3071
3072 // Call SetConfiguration with a config identical to what the PC was
3073 // constructed with.
3074 EXPECT_TRUE(pc_->SetConfiguration(config));
3075 CreateOfferAsLocalDescription();
3076
3077 // Grab the new ufrags.
3078 std::vector<std::string> subsequent_ufrags =
3079 GetUfrags(pc_->local_description());
3080
3081 EXPECT_EQ(initial_ufrags, subsequent_ufrags);
3082}
3083
3084// Test for a weird corner case scenario:
3085// 1. Audio/video session established.
3086// 2. SetConfiguration changes ICE config; ICE restart needed.
3087// 3. ICE restart initiated by remote peer, but only for one m= section.
3088// 4. Next createOffer should initiate an ICE restart, but only for the other
3089// m= section; it would be pointless to do an ICE restart for the m= section
3090// that was already restarted.
3091TEST_F(PeerConnectionInterfaceTest, SetConfigurationCausingPartialIceRestart) {
3092 PeerConnectionInterface::RTCConfiguration config;
3093 config.type = PeerConnectionInterface::kRelay;
3094 // Need to pass default constraints to prevent disabling of DTLS...
3095 FakeConstraints default_constraints;
3096 CreatePeerConnection(config, &default_constraints);
3097 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
3098
3099 // Do initial offer/answer so there's something to restart.
3100 CreateOfferAsLocalDescription();
3101 CreateAnswerAsRemoteDescription(kSdpStringWithStream1);
3102
3103 // Change ICE policy, which should set the "needs-ice-restart" flag.
3104 config.type = PeerConnectionInterface::kAll;
3105 EXPECT_TRUE(pc_->SetConfiguration(config));
3106
3107 // Do ICE restart for the first m= section, initiated by remote peer.
3108 webrtc::JsepSessionDescription* remote_offer =
3109 new webrtc::JsepSessionDescription(SessionDescriptionInterface::kOffer);
3110 EXPECT_TRUE(remote_offer->Initialize(kSdpStringWithStream1, nullptr));
3111 remote_offer->description()->transport_infos()[0].description.ice_ufrag =
3112 "modified";
3113 EXPECT_TRUE(DoSetRemoteDescription(remote_offer));
3114 CreateAnswerAsLocalDescription();
3115
3116 // Grab the ufrags.
3117 std::vector<std::string> initial_ufrags = GetUfrags(pc_->local_description());
3118 ASSERT_EQ(2, initial_ufrags.size());
3119
3120 // Create offer and grab the new ufrags.
3121 CreateOfferAsLocalDescription();
3122 std::vector<std::string> subsequent_ufrags =
3123 GetUfrags(pc_->local_description());
3124 ASSERT_EQ(2, subsequent_ufrags.size());
3125
3126 // Ensure that only the ufrag for the second m= section changed.
3127 EXPECT_EQ(initial_ufrags[0], subsequent_ufrags[0]);
3128 EXPECT_NE(initial_ufrags[1], subsequent_ufrags[1]);
3129}
3130
deadbeeffe4a8a42016-12-20 17:56:17 -08003131// Tests that the methods to return current/pending descriptions work as
3132// expected at different points in the offer/answer exchange. This test does
3133// one offer/answer exchange as the offerer, then another as the answerer.
3134TEST_F(PeerConnectionInterfaceTest, CurrentAndPendingDescriptions) {
3135 // This disables DTLS so we can apply an answer to ourselves.
3136 CreatePeerConnection();
3137
3138 // Create initial local offer and get SDP (which will also be used as
3139 // answer/pranswer);
3140 std::unique_ptr<SessionDescriptionInterface> offer;
3141 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
3142 std::string sdp;
3143 EXPECT_TRUE(offer->ToString(&sdp));
3144
3145 // Set local offer.
3146 SessionDescriptionInterface* local_offer = offer.release();
3147 EXPECT_TRUE(DoSetLocalDescription(local_offer));
3148 EXPECT_EQ(local_offer, pc_->pending_local_description());
3149 EXPECT_EQ(nullptr, pc_->pending_remote_description());
3150 EXPECT_EQ(nullptr, pc_->current_local_description());
3151 EXPECT_EQ(nullptr, pc_->current_remote_description());
3152
3153 // Set remote pranswer.
3154 SessionDescriptionInterface* remote_pranswer =
3155 webrtc::CreateSessionDescription(SessionDescriptionInterface::kPrAnswer,
3156 sdp, nullptr);
3157 EXPECT_TRUE(DoSetRemoteDescription(remote_pranswer));
3158 EXPECT_EQ(local_offer, pc_->pending_local_description());
3159 EXPECT_EQ(remote_pranswer, pc_->pending_remote_description());
3160 EXPECT_EQ(nullptr, pc_->current_local_description());
3161 EXPECT_EQ(nullptr, pc_->current_remote_description());
3162
3163 // Set remote answer.
3164 SessionDescriptionInterface* remote_answer = webrtc::CreateSessionDescription(
3165 SessionDescriptionInterface::kAnswer, sdp, nullptr);
3166 EXPECT_TRUE(DoSetRemoteDescription(remote_answer));
3167 EXPECT_EQ(nullptr, pc_->pending_local_description());
3168 EXPECT_EQ(nullptr, pc_->pending_remote_description());
3169 EXPECT_EQ(local_offer, pc_->current_local_description());
3170 EXPECT_EQ(remote_answer, pc_->current_remote_description());
3171
3172 // Set remote offer.
3173 SessionDescriptionInterface* remote_offer = webrtc::CreateSessionDescription(
3174 SessionDescriptionInterface::kOffer, sdp, nullptr);
3175 EXPECT_TRUE(DoSetRemoteDescription(remote_offer));
3176 EXPECT_EQ(remote_offer, pc_->pending_remote_description());
3177 EXPECT_EQ(nullptr, pc_->pending_local_description());
3178 EXPECT_EQ(local_offer, pc_->current_local_description());
3179 EXPECT_EQ(remote_answer, pc_->current_remote_description());
3180
3181 // Set local pranswer.
3182 SessionDescriptionInterface* local_pranswer =
3183 webrtc::CreateSessionDescription(SessionDescriptionInterface::kPrAnswer,
3184 sdp, nullptr);
3185 EXPECT_TRUE(DoSetLocalDescription(local_pranswer));
3186 EXPECT_EQ(remote_offer, pc_->pending_remote_description());
3187 EXPECT_EQ(local_pranswer, pc_->pending_local_description());
3188 EXPECT_EQ(local_offer, pc_->current_local_description());
3189 EXPECT_EQ(remote_answer, pc_->current_remote_description());
3190
3191 // Set local answer.
3192 SessionDescriptionInterface* local_answer = webrtc::CreateSessionDescription(
3193 SessionDescriptionInterface::kAnswer, sdp, nullptr);
3194 EXPECT_TRUE(DoSetLocalDescription(local_answer));
3195 EXPECT_EQ(nullptr, pc_->pending_remote_description());
3196 EXPECT_EQ(nullptr, pc_->pending_local_description());
3197 EXPECT_EQ(remote_offer, pc_->current_remote_description());
3198 EXPECT_EQ(local_answer, pc_->current_local_description());
3199}
3200
zhihuang77985012017-02-07 15:45:16 -08003201// Tests that it won't crash when calling StartRtcEventLog or StopRtcEventLog
3202// after the PeerConnection is closed.
3203TEST_F(PeerConnectionInterfaceTest,
3204 StartAndStopLoggingAfterPeerConnectionClosed) {
3205 CreatePeerConnection();
3206 // The RtcEventLog will be reset when the PeerConnection is closed.
3207 pc_->Close();
3208
3209 rtc::PlatformFile file = 0;
3210 int64_t max_size_bytes = 1024;
3211 EXPECT_FALSE(pc_->StartRtcEventLog(file, max_size_bytes));
3212 pc_->StopRtcEventLog();
3213}
3214
deadbeef30952b42017-04-21 02:41:29 -07003215// Test that generated offers/answers include "ice-option:trickle".
3216TEST_F(PeerConnectionInterfaceTest, OffersAndAnswersHaveTrickleIceOption) {
3217 CreatePeerConnection();
3218
3219 // First, create an offer with audio/video.
3220 FakeConstraints constraints;
3221 constraints.SetMandatoryReceiveAudio(true);
3222 constraints.SetMandatoryReceiveVideo(true);
3223 std::unique_ptr<SessionDescriptionInterface> offer;
3224 ASSERT_TRUE(DoCreateOffer(&offer, &constraints));
3225 cricket::SessionDescription* desc = offer->description();
3226 ASSERT_EQ(2u, desc->transport_infos().size());
3227 EXPECT_TRUE(desc->transport_infos()[0].description.HasOption("trickle"));
3228 EXPECT_TRUE(desc->transport_infos()[1].description.HasOption("trickle"));
3229
3230 // Apply the offer as a remote description, then create an answer.
3231 EXPECT_TRUE(DoSetRemoteDescription(offer.release()));
3232 std::unique_ptr<SessionDescriptionInterface> answer;
3233 ASSERT_TRUE(DoCreateAnswer(&answer, &constraints));
3234 desc = answer->description();
3235 ASSERT_EQ(2u, desc->transport_infos().size());
3236 EXPECT_TRUE(desc->transport_infos()[0].description.HasOption("trickle"));
3237 EXPECT_TRUE(desc->transport_infos()[1].description.HasOption("trickle"));
3238}
3239
deadbeef1dcb1642017-03-29 21:08:16 -07003240// Test that ICE renomination isn't offered if it's not enabled in the PC's
3241// RTCConfiguration.
3242TEST_F(PeerConnectionInterfaceTest, IceRenominationNotOffered) {
3243 PeerConnectionInterface::RTCConfiguration config;
3244 config.enable_ice_renomination = false;
3245 CreatePeerConnection(config, nullptr);
3246 AddVoiceStream("foo");
3247
3248 std::unique_ptr<SessionDescriptionInterface> offer;
3249 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
3250 cricket::SessionDescription* desc = offer->description();
3251 EXPECT_EQ(1u, desc->transport_infos().size());
3252 EXPECT_FALSE(
3253 desc->transport_infos()[0].description.GetIceParameters().renomination);
3254}
3255
3256// Test that the ICE renomination option is present in generated offers/answers
3257// if it's enabled in the PC's RTCConfiguration.
3258TEST_F(PeerConnectionInterfaceTest, IceRenominationOptionInOfferAndAnswer) {
3259 PeerConnectionInterface::RTCConfiguration config;
3260 config.enable_ice_renomination = true;
3261 CreatePeerConnection(config, nullptr);
3262 AddVoiceStream("foo");
3263
3264 std::unique_ptr<SessionDescriptionInterface> offer;
3265 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
3266 cricket::SessionDescription* desc = offer->description();
3267 EXPECT_EQ(1u, desc->transport_infos().size());
3268 EXPECT_TRUE(
3269 desc->transport_infos()[0].description.GetIceParameters().renomination);
3270
3271 // Set the offer as a remote description, then create an answer and ensure it
3272 // has the renomination flag too.
3273 EXPECT_TRUE(DoSetRemoteDescription(offer.release()));
3274 std::unique_ptr<SessionDescriptionInterface> answer;
3275 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
3276 desc = answer->description();
3277 EXPECT_EQ(1u, desc->transport_infos().size());
3278 EXPECT_TRUE(
3279 desc->transport_infos()[0].description.GetIceParameters().renomination);
3280}
3281
3282// Test that if CreateOffer is called with the deprecated "offer to receive
3283// audio/video" constraints, they're processed and result in an offer with
3284// audio/video sections just as if RTCOfferAnswerOptions had been used.
3285TEST_F(PeerConnectionInterfaceTest, CreateOfferWithOfferToReceiveConstraints) {
3286 CreatePeerConnection();
3287
3288 FakeConstraints constraints;
3289 constraints.SetMandatoryReceiveAudio(true);
3290 constraints.SetMandatoryReceiveVideo(true);
3291 std::unique_ptr<SessionDescriptionInterface> offer;
3292 ASSERT_TRUE(DoCreateOffer(&offer, &constraints));
3293
3294 cricket::SessionDescription* desc = offer->description();
3295 const cricket::ContentInfo* audio = cricket::GetFirstAudioContent(desc);
3296 const cricket::ContentInfo* video = cricket::GetFirstVideoContent(desc);
3297 ASSERT_NE(nullptr, audio);
3298 ASSERT_NE(nullptr, video);
3299 EXPECT_FALSE(audio->rejected);
3300 EXPECT_FALSE(video->rejected);
3301}
3302
3303// Test that if CreateAnswer is called with the deprecated "offer to receive
3304// audio/video" constraints, they're processed and can be used to reject an
3305// offered m= section just as can be done with RTCOfferAnswerOptions;
3306TEST_F(PeerConnectionInterfaceTest, CreateAnswerWithOfferToReceiveConstraints) {
3307 CreatePeerConnection();
3308
3309 // First, create an offer with audio/video and apply it as a remote
3310 // description.
3311 FakeConstraints constraints;
3312 constraints.SetMandatoryReceiveAudio(true);
3313 constraints.SetMandatoryReceiveVideo(true);
3314 std::unique_ptr<SessionDescriptionInterface> offer;
3315 ASSERT_TRUE(DoCreateOffer(&offer, &constraints));
3316 EXPECT_TRUE(DoSetRemoteDescription(offer.release()));
3317
3318 // Now create answer that rejects audio/video.
3319 constraints.SetMandatoryReceiveAudio(false);
3320 constraints.SetMandatoryReceiveVideo(false);
3321 std::unique_ptr<SessionDescriptionInterface> answer;
3322 ASSERT_TRUE(DoCreateAnswer(&answer, &constraints));
3323
3324 cricket::SessionDescription* desc = answer->description();
3325 const cricket::ContentInfo* audio = cricket::GetFirstAudioContent(desc);
3326 const cricket::ContentInfo* video = cricket::GetFirstVideoContent(desc);
3327 ASSERT_NE(nullptr, audio);
3328 ASSERT_NE(nullptr, video);
3329 EXPECT_TRUE(audio->rejected);
3330 EXPECT_TRUE(video->rejected);
3331}
3332
3333#ifdef HAVE_SCTP
3334#define MAYBE_DataChannelOnlyOfferWithMaxBundlePolicy \
3335 DataChannelOnlyOfferWithMaxBundlePolicy
3336#else
3337#define MAYBE_DataChannelOnlyOfferWithMaxBundlePolicy \
3338 DISABLED_DataChannelOnlyOfferWithMaxBundlePolicy
3339#endif
3340
3341// Test that negotiation can succeed with a data channel only, and with the max
3342// bundle policy. Previously there was a bug that prevented this.
3343TEST_F(PeerConnectionInterfaceTest,
3344 MAYBE_DataChannelOnlyOfferWithMaxBundlePolicy) {
3345 PeerConnectionInterface::RTCConfiguration config;
3346 config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
3347 CreatePeerConnection(config, nullptr);
3348
3349 // First, create an offer with only a data channel and apply it as a remote
3350 // description.
3351 pc_->CreateDataChannel("test", nullptr);
3352 std::unique_ptr<SessionDescriptionInterface> offer;
3353 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
3354 EXPECT_TRUE(DoSetRemoteDescription(offer.release()));
3355
3356 // Create and set answer as well.
3357 std::unique_ptr<SessionDescriptionInterface> answer;
3358 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
3359 EXPECT_TRUE(DoSetLocalDescription(answer.release()));
3360}
3361
zstein4b979802017-06-02 14:37:37 -07003362TEST_F(PeerConnectionInterfaceTest, SetBitrateWithoutMinSucceeds) {
3363 CreatePeerConnection();
3364 PeerConnectionInterface::BitrateParameters bitrate;
3365 bitrate.current_bitrate_bps = rtc::Optional<int>(100000);
3366 EXPECT_TRUE(pc_->SetBitrate(bitrate).ok());
3367}
3368
3369TEST_F(PeerConnectionInterfaceTest, SetBitrateNegativeMinFails) {
3370 CreatePeerConnection();
3371 PeerConnectionInterface::BitrateParameters bitrate;
3372 bitrate.min_bitrate_bps = rtc::Optional<int>(-1);
3373 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3374}
3375
3376TEST_F(PeerConnectionInterfaceTest, SetBitrateCurrentLessThanMinFails) {
3377 CreatePeerConnection();
3378 PeerConnectionInterface::BitrateParameters bitrate;
3379 bitrate.min_bitrate_bps = rtc::Optional<int>(5);
3380 bitrate.current_bitrate_bps = rtc::Optional<int>(3);
3381 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3382}
3383
3384TEST_F(PeerConnectionInterfaceTest, SetBitrateCurrentNegativeFails) {
3385 CreatePeerConnection();
3386 PeerConnectionInterface::BitrateParameters bitrate;
3387 bitrate.current_bitrate_bps = rtc::Optional<int>(-1);
3388 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3389}
3390
3391TEST_F(PeerConnectionInterfaceTest, SetBitrateMaxLessThanCurrentFails) {
3392 CreatePeerConnection();
3393 PeerConnectionInterface::BitrateParameters bitrate;
3394 bitrate.current_bitrate_bps = rtc::Optional<int>(10);
3395 bitrate.max_bitrate_bps = rtc::Optional<int>(8);
3396 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3397}
3398
3399TEST_F(PeerConnectionInterfaceTest, SetBitrateMaxLessThanMinFails) {
3400 CreatePeerConnection();
3401 PeerConnectionInterface::BitrateParameters bitrate;
3402 bitrate.min_bitrate_bps = rtc::Optional<int>(10);
3403 bitrate.max_bitrate_bps = rtc::Optional<int>(8);
3404 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3405}
3406
3407TEST_F(PeerConnectionInterfaceTest, SetBitrateMaxNegativeFails) {
3408 CreatePeerConnection();
3409 PeerConnectionInterface::BitrateParameters bitrate;
3410 bitrate.max_bitrate_bps = rtc::Optional<int>(-1);
3411 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3412}
3413
Steve Anton038834f2017-07-14 15:59:59 -07003414// ice_regather_interval_range requires WebRTC to be configured for continual
3415// gathering already.
3416TEST_F(PeerConnectionInterfaceTest,
3417 SetIceRegatherIntervalRangeWithoutContinualGatheringFails) {
3418 PeerConnectionInterface::RTCConfiguration config;
3419 config.ice_regather_interval_range.emplace(1000, 2000);
3420 config.continual_gathering_policy =
3421 PeerConnectionInterface::ContinualGatheringPolicy::GATHER_ONCE;
3422 CreatePeerConnectionExpectFail(config);
3423}
3424
3425// Ensures that there is no error when ice_regather_interval_range is set with
3426// continual gathering enabled.
3427TEST_F(PeerConnectionInterfaceTest,
3428 SetIceRegatherIntervalRangeWithContinualGathering) {
3429 PeerConnectionInterface::RTCConfiguration config;
3430 config.ice_regather_interval_range.emplace(1000, 2000);
3431 config.continual_gathering_policy =
3432 PeerConnectionInterface::ContinualGatheringPolicy::GATHER_CONTINUALLY;
3433 CreatePeerConnection(config, nullptr);
3434}
3435
zstein4b979802017-06-02 14:37:37 -07003436// The current bitrate from Call's BitrateConfigMask is currently clamped by
3437// Call's BitrateConfig, which comes from the SDP or a default value. This test
3438// checks that a call to SetBitrate with a current bitrate that will be clamped
3439// succeeds.
3440TEST_F(PeerConnectionInterfaceTest, SetBitrateCurrentLessThanImplicitMin) {
3441 CreatePeerConnection();
3442 PeerConnectionInterface::BitrateParameters bitrate;
3443 bitrate.current_bitrate_bps = rtc::Optional<int>(1);
3444 EXPECT_TRUE(pc_->SetBitrate(bitrate).ok());
3445}
3446
nisse51542be2016-02-12 02:27:06 -08003447class PeerConnectionMediaConfigTest : public testing::Test {
3448 protected:
3449 void SetUp() override {
zhihuang38ede132017-06-15 12:52:32 -07003450 pcf_ = PeerConnectionFactoryForTest::CreatePeerConnectionFactoryForTest();
nisse51542be2016-02-12 02:27:06 -08003451 pcf_->Initialize();
3452 }
nisseeaabdf62017-05-05 02:23:02 -07003453 const cricket::MediaConfig TestCreatePeerConnection(
nisse51542be2016-02-12 02:27:06 -08003454 const PeerConnectionInterface::RTCConfiguration& config,
3455 const MediaConstraintsInterface *constraints) {
nisse51542be2016-02-12 02:27:06 -08003456
zhihuang9763d562016-08-05 11:14:50 -07003457 rtc::scoped_refptr<PeerConnectionInterface> pc(pcf_->CreatePeerConnection(
3458 config, constraints, nullptr, nullptr, &observer_));
nisse51542be2016-02-12 02:27:06 -08003459 EXPECT_TRUE(pc.get());
nisseeaabdf62017-05-05 02:23:02 -07003460 return pc->GetConfiguration().media_config;
nisse51542be2016-02-12 02:27:06 -08003461 }
3462
zhihuang9763d562016-08-05 11:14:50 -07003463 rtc::scoped_refptr<PeerConnectionFactoryForTest> pcf_;
nisse51542be2016-02-12 02:27:06 -08003464 MockPeerConnectionObserver observer_;
3465};
3466
3467// This test verifies the default behaviour with no constraints and a
3468// default RTCConfiguration.
3469TEST_F(PeerConnectionMediaConfigTest, TestDefaults) {
3470 PeerConnectionInterface::RTCConfiguration config;
3471 FakeConstraints constraints;
3472
3473 const cricket::MediaConfig& media_config =
3474 TestCreatePeerConnection(config, &constraints);
3475
3476 EXPECT_FALSE(media_config.enable_dscp);
nisse0db023a2016-03-01 04:29:59 -08003477 EXPECT_TRUE(media_config.video.enable_cpu_overuse_detection);
3478 EXPECT_FALSE(media_config.video.disable_prerenderer_smoothing);
3479 EXPECT_FALSE(media_config.video.suspend_below_min_bitrate);
nisse51542be2016-02-12 02:27:06 -08003480}
3481
3482// This test verifies the DSCP constraint is recognized and passed to
nisse528b7932017-05-08 03:21:43 -07003483// the PeerConnection.
nisse51542be2016-02-12 02:27:06 -08003484TEST_F(PeerConnectionMediaConfigTest, TestDscpConstraintTrue) {
3485 PeerConnectionInterface::RTCConfiguration config;
3486 FakeConstraints constraints;
3487
3488 constraints.AddOptional(webrtc::MediaConstraintsInterface::kEnableDscp, true);
3489 const cricket::MediaConfig& media_config =
3490 TestCreatePeerConnection(config, &constraints);
3491
3492 EXPECT_TRUE(media_config.enable_dscp);
3493}
3494
3495// This test verifies the cpu overuse detection constraint is
nisse528b7932017-05-08 03:21:43 -07003496// recognized and passed to the PeerConnection.
nisse51542be2016-02-12 02:27:06 -08003497TEST_F(PeerConnectionMediaConfigTest, TestCpuOveruseConstraintFalse) {
3498 PeerConnectionInterface::RTCConfiguration config;
3499 FakeConstraints constraints;
3500
3501 constraints.AddOptional(
3502 webrtc::MediaConstraintsInterface::kCpuOveruseDetection, false);
3503 const cricket::MediaConfig media_config =
3504 TestCreatePeerConnection(config, &constraints);
3505
nisse0db023a2016-03-01 04:29:59 -08003506 EXPECT_FALSE(media_config.video.enable_cpu_overuse_detection);
nisse51542be2016-02-12 02:27:06 -08003507}
3508
3509// This test verifies that the disable_prerenderer_smoothing flag is
nisse528b7932017-05-08 03:21:43 -07003510// propagated from RTCConfiguration to the PeerConnection.
nisse51542be2016-02-12 02:27:06 -08003511TEST_F(PeerConnectionMediaConfigTest, TestDisablePrerendererSmoothingTrue) {
3512 PeerConnectionInterface::RTCConfiguration config;
3513 FakeConstraints constraints;
3514
Niels Möller71bdda02016-03-31 12:59:59 +02003515 config.set_prerenderer_smoothing(false);
nisse51542be2016-02-12 02:27:06 -08003516 const cricket::MediaConfig& media_config =
3517 TestCreatePeerConnection(config, &constraints);
3518
nisse0db023a2016-03-01 04:29:59 -08003519 EXPECT_TRUE(media_config.video.disable_prerenderer_smoothing);
3520}
3521
3522// This test verifies the suspend below min bitrate constraint is
nisse528b7932017-05-08 03:21:43 -07003523// recognized and passed to the PeerConnection.
nisse0db023a2016-03-01 04:29:59 -08003524TEST_F(PeerConnectionMediaConfigTest,
3525 TestSuspendBelowMinBitrateConstraintTrue) {
3526 PeerConnectionInterface::RTCConfiguration config;
3527 FakeConstraints constraints;
3528
3529 constraints.AddOptional(
3530 webrtc::MediaConstraintsInterface::kEnableVideoSuspendBelowMinBitrate,
3531 true);
3532 const cricket::MediaConfig media_config =
3533 TestCreatePeerConnection(config, &constraints);
3534
3535 EXPECT_TRUE(media_config.video.suspend_below_min_bitrate);
nisse51542be2016-02-12 02:27:06 -08003536}
3537
deadbeefab9b2d12015-10-14 11:33:11 -07003538// The following tests verify that session options are created correctly.
deadbeefc80741f2015-10-22 13:14:45 -07003539// TODO(deadbeef): Convert these tests to be more end-to-end. Instead of
3540// "verify options are converted correctly", should be "pass options into
3541// CreateOffer and verify the correct offer is produced."
deadbeefab9b2d12015-10-14 11:33:11 -07003542
3543TEST(CreateSessionOptionsTest, GetOptionsForOfferWithInvalidAudioOption) {
3544 RTCOfferAnswerOptions rtc_options;
3545 rtc_options.offer_to_receive_audio = RTCOfferAnswerOptions::kUndefined - 1;
3546
3547 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08003548 EXPECT_FALSE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07003549
3550 rtc_options.offer_to_receive_audio =
3551 RTCOfferAnswerOptions::kMaxOfferToReceiveMedia + 1;
htaaac2dea2016-03-10 13:35:55 -08003552 EXPECT_FALSE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07003553}
3554
3555TEST(CreateSessionOptionsTest, GetOptionsForOfferWithInvalidVideoOption) {
3556 RTCOfferAnswerOptions rtc_options;
3557 rtc_options.offer_to_receive_video = RTCOfferAnswerOptions::kUndefined - 1;
3558
3559 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08003560 EXPECT_FALSE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07003561
3562 rtc_options.offer_to_receive_video =
3563 RTCOfferAnswerOptions::kMaxOfferToReceiveMedia + 1;
htaaac2dea2016-03-10 13:35:55 -08003564 EXPECT_FALSE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07003565}
3566
3567// Test that a MediaSessionOptions is created for an offer if
deadbeefc80741f2015-10-22 13:14:45 -07003568// OfferToReceiveAudio and OfferToReceiveVideo options are set.
deadbeefab9b2d12015-10-14 11:33:11 -07003569TEST(CreateSessionOptionsTest, GetMediaSessionOptionsForOfferWithAudioVideo) {
3570 RTCOfferAnswerOptions rtc_options;
3571 rtc_options.offer_to_receive_audio = 1;
3572 rtc_options.offer_to_receive_video = 1;
3573
3574 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08003575 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07003576 EXPECT_TRUE(options.has_audio());
3577 EXPECT_TRUE(options.has_video());
3578 EXPECT_TRUE(options.bundle_enabled);
3579}
3580
3581// Test that a correct MediaSessionOptions is created for an offer if
deadbeefc80741f2015-10-22 13:14:45 -07003582// OfferToReceiveAudio is set.
deadbeefab9b2d12015-10-14 11:33:11 -07003583TEST(CreateSessionOptionsTest, GetMediaSessionOptionsForOfferWithAudio) {
3584 RTCOfferAnswerOptions rtc_options;
3585 rtc_options.offer_to_receive_audio = 1;
3586
3587 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08003588 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07003589 EXPECT_TRUE(options.has_audio());
3590 EXPECT_FALSE(options.has_video());
3591 EXPECT_TRUE(options.bundle_enabled);
3592}
3593
3594// Test that a correct MediaSessionOptions is created for an offer if
deadbeefc80741f2015-10-22 13:14:45 -07003595// the default OfferOptions are used.
deadbeefab9b2d12015-10-14 11:33:11 -07003596TEST(CreateSessionOptionsTest, GetDefaultMediaSessionOptionsForOffer) {
3597 RTCOfferAnswerOptions rtc_options;
3598
3599 cricket::MediaSessionOptions options;
deadbeef0ed85b22016-02-23 17:24:52 -08003600 options.transport_options["audio"] = cricket::TransportOptions();
3601 options.transport_options["video"] = cricket::TransportOptions();
htaaac2dea2016-03-10 13:35:55 -08003602 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefc80741f2015-10-22 13:14:45 -07003603 EXPECT_TRUE(options.has_audio());
deadbeefab9b2d12015-10-14 11:33:11 -07003604 EXPECT_FALSE(options.has_video());
deadbeefc80741f2015-10-22 13:14:45 -07003605 EXPECT_TRUE(options.bundle_enabled);
deadbeefab9b2d12015-10-14 11:33:11 -07003606 EXPECT_TRUE(options.vad_enabled);
deadbeef0ed85b22016-02-23 17:24:52 -08003607 EXPECT_FALSE(options.transport_options["audio"].ice_restart);
3608 EXPECT_FALSE(options.transport_options["video"].ice_restart);
deadbeefab9b2d12015-10-14 11:33:11 -07003609}
3610
3611// Test that a correct MediaSessionOptions is created for an offer if
deadbeefc80741f2015-10-22 13:14:45 -07003612// OfferToReceiveVideo is set.
deadbeefab9b2d12015-10-14 11:33:11 -07003613TEST(CreateSessionOptionsTest, GetMediaSessionOptionsForOfferWithVideo) {
3614 RTCOfferAnswerOptions rtc_options;
3615 rtc_options.offer_to_receive_audio = 0;
3616 rtc_options.offer_to_receive_video = 1;
3617
3618 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08003619 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07003620 EXPECT_FALSE(options.has_audio());
3621 EXPECT_TRUE(options.has_video());
3622 EXPECT_TRUE(options.bundle_enabled);
3623}
3624
3625// Test that a correct MediaSessionOptions is created for an offer if
3626// UseRtpMux is set to false.
3627TEST(CreateSessionOptionsTest,
3628 GetMediaSessionOptionsForOfferWithBundleDisabled) {
3629 RTCOfferAnswerOptions rtc_options;
3630 rtc_options.offer_to_receive_audio = 1;
3631 rtc_options.offer_to_receive_video = 1;
3632 rtc_options.use_rtp_mux = false;
3633
3634 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08003635 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07003636 EXPECT_TRUE(options.has_audio());
3637 EXPECT_TRUE(options.has_video());
3638 EXPECT_FALSE(options.bundle_enabled);
3639}
3640
3641// Test that a correct MediaSessionOptions is created to restart ice if
3642// IceRestart is set. It also tests that subsequent MediaSessionOptions don't
Taylor Brandstetterf475d362016-01-08 15:35:57 -08003643// have |audio_transport_options.ice_restart| etc. set.
deadbeefab9b2d12015-10-14 11:33:11 -07003644TEST(CreateSessionOptionsTest, GetMediaSessionOptionsForOfferWithIceRestart) {
3645 RTCOfferAnswerOptions rtc_options;
3646 rtc_options.ice_restart = true;
3647
3648 cricket::MediaSessionOptions options;
deadbeef0ed85b22016-02-23 17:24:52 -08003649 options.transport_options["audio"] = cricket::TransportOptions();
3650 options.transport_options["video"] = cricket::TransportOptions();
htaaac2dea2016-03-10 13:35:55 -08003651 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeef0ed85b22016-02-23 17:24:52 -08003652 EXPECT_TRUE(options.transport_options["audio"].ice_restart);
3653 EXPECT_TRUE(options.transport_options["video"].ice_restart);
deadbeefab9b2d12015-10-14 11:33:11 -07003654
3655 rtc_options = RTCOfferAnswerOptions();
htaaac2dea2016-03-10 13:35:55 -08003656 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeef0ed85b22016-02-23 17:24:52 -08003657 EXPECT_FALSE(options.transport_options["audio"].ice_restart);
3658 EXPECT_FALSE(options.transport_options["video"].ice_restart);
deadbeefab9b2d12015-10-14 11:33:11 -07003659}
3660
3661// Test that the MediaConstraints in an answer don't affect if audio and video
3662// is offered in an offer but that if kOfferToReceiveAudio or
3663// kOfferToReceiveVideo constraints are true in an offer, the media type will be
3664// included in subsequent answers.
3665TEST(CreateSessionOptionsTest, MediaConstraintsInAnswer) {
3666 FakeConstraints answer_c;
3667 answer_c.SetMandatoryReceiveAudio(true);
3668 answer_c.SetMandatoryReceiveVideo(true);
3669
3670 cricket::MediaSessionOptions answer_options;
3671 EXPECT_TRUE(ParseConstraintsForAnswer(&answer_c, &answer_options));
3672 EXPECT_TRUE(answer_options.has_audio());
3673 EXPECT_TRUE(answer_options.has_video());
3674
deadbeefc80741f2015-10-22 13:14:45 -07003675 RTCOfferAnswerOptions rtc_offer_options;
deadbeefab9b2d12015-10-14 11:33:11 -07003676
3677 cricket::MediaSessionOptions offer_options;
htaaac2dea2016-03-10 13:35:55 -08003678 EXPECT_TRUE(
3679 ExtractMediaSessionOptions(rtc_offer_options, false, &offer_options));
deadbeefc80741f2015-10-22 13:14:45 -07003680 EXPECT_TRUE(offer_options.has_audio());
htaaac2dea2016-03-10 13:35:55 -08003681 EXPECT_TRUE(offer_options.has_video());
deadbeefab9b2d12015-10-14 11:33:11 -07003682
deadbeefc80741f2015-10-22 13:14:45 -07003683 RTCOfferAnswerOptions updated_rtc_offer_options;
3684 updated_rtc_offer_options.offer_to_receive_audio = 1;
3685 updated_rtc_offer_options.offer_to_receive_video = 1;
deadbeefab9b2d12015-10-14 11:33:11 -07003686
3687 cricket::MediaSessionOptions updated_offer_options;
htaaac2dea2016-03-10 13:35:55 -08003688 EXPECT_TRUE(ExtractMediaSessionOptions(updated_rtc_offer_options, false,
htaa2a49d92016-03-04 02:51:39 -08003689 &updated_offer_options));
deadbeefab9b2d12015-10-14 11:33:11 -07003690 EXPECT_TRUE(updated_offer_options.has_audio());
3691 EXPECT_TRUE(updated_offer_options.has_video());
3692
3693 // Since an offer has been created with both audio and video, subsequent
3694 // offers and answers should contain both audio and video.
3695 // Answers will only contain the media types that exist in the offer
3696 // regardless of the value of |updated_answer_options.has_audio| and
3697 // |updated_answer_options.has_video|.
3698 FakeConstraints updated_answer_c;
3699 answer_c.SetMandatoryReceiveAudio(false);
3700 answer_c.SetMandatoryReceiveVideo(false);
3701
3702 cricket::MediaSessionOptions updated_answer_options;
3703 EXPECT_TRUE(
3704 ParseConstraintsForAnswer(&updated_answer_c, &updated_answer_options));
3705 EXPECT_TRUE(updated_answer_options.has_audio());
3706 EXPECT_TRUE(updated_answer_options.has_video());
deadbeefab9b2d12015-10-14 11:33:11 -07003707}
deadbeef3edec7c2016-12-10 11:44:26 -08003708
deadbeef293e9262017-01-11 12:28:30 -08003709// Tests a few random fields being different.
3710TEST(RTCConfigurationTest, ComparisonOperators) {
3711 PeerConnectionInterface::RTCConfiguration a;
3712 PeerConnectionInterface::RTCConfiguration b;
3713 EXPECT_EQ(a, b);
3714
3715 PeerConnectionInterface::RTCConfiguration c;
3716 c.servers.push_back(PeerConnectionInterface::IceServer());
3717 EXPECT_NE(a, c);
3718
3719 PeerConnectionInterface::RTCConfiguration d;
3720 d.type = PeerConnectionInterface::kRelay;
3721 EXPECT_NE(a, d);
3722
3723 PeerConnectionInterface::RTCConfiguration e;
3724 e.audio_jitter_buffer_max_packets = 5;
3725 EXPECT_NE(a, e);
3726
3727 PeerConnectionInterface::RTCConfiguration f;
3728 f.ice_connection_receiving_timeout = 1337;
3729 EXPECT_NE(a, f);
3730
3731 PeerConnectionInterface::RTCConfiguration g;
3732 g.disable_ipv6 = true;
3733 EXPECT_NE(a, g);
3734
3735 PeerConnectionInterface::RTCConfiguration h(
3736 PeerConnectionInterface::RTCConfigurationType::kAggressive);
3737 EXPECT_NE(a, h);
3738}