blob: 59ed2e4b28a235f84e81fe2b829b8b6cfefa6d60 [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
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000800 void CreatePeerConnectionWithDifferentConfigurations() {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700801 CreatePeerConnectionWithIceServer(kStunAddressOnly, "");
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800802 EXPECT_EQ(1u, port_allocator_->stun_servers().size());
803 EXPECT_EQ(0u, port_allocator_->turn_servers().size());
804 EXPECT_EQ("address", port_allocator_->stun_servers().begin()->hostname());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000805 EXPECT_EQ(kDefaultStunPort,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800806 port_allocator_->stun_servers().begin()->port());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000807
deadbeef0a6c4ca2015-10-06 11:38:28 -0700808 CreatePeerConnectionExpectFail(kStunInvalidPort);
809 CreatePeerConnectionExpectFail(kStunAddressPortAndMore1);
810 CreatePeerConnectionExpectFail(kStunAddressPortAndMore2);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000811
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700812 CreatePeerConnectionWithIceServer(kTurnIceServerUri, kTurnPassword);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800813 EXPECT_EQ(0u, port_allocator_->stun_servers().size());
814 EXPECT_EQ(1u, port_allocator_->turn_servers().size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000815 EXPECT_EQ(kTurnUsername,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800816 port_allocator_->turn_servers()[0].credentials.username);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000817 EXPECT_EQ(kTurnPassword,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800818 port_allocator_->turn_servers()[0].credentials.password);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000819 EXPECT_EQ(kTurnHostname,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800820 port_allocator_->turn_servers()[0].ports[0].address.hostname());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000821 }
822
823 void ReleasePeerConnection() {
824 pc_ = NULL;
825 observer_.SetPeerConnectionInterface(NULL);
826 }
827
deadbeefab9b2d12015-10-14 11:33:11 -0700828 void AddVideoStream(const std::string& label) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000829 // Create a local stream.
zhihuang9763d562016-08-05 11:14:50 -0700830 rtc::scoped_refptr<MediaStreamInterface> stream(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000831 pc_factory_->CreateLocalMediaStream(label));
zhihuang9763d562016-08-05 11:14:50 -0700832 rtc::scoped_refptr<VideoTrackSourceInterface> video_source(
deadbeef112b2e92017-02-10 20:13:37 -0800833 pc_factory_->CreateVideoSource(std::unique_ptr<cricket::VideoCapturer>(
834 new cricket::FakeVideoCapturer()),
835 NULL));
zhihuang9763d562016-08-05 11:14:50 -0700836 rtc::scoped_refptr<VideoTrackInterface> video_track(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000837 pc_factory_->CreateVideoTrack(label + "v0", video_source));
838 stream->AddTrack(video_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +0000839 EXPECT_TRUE(pc_->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000840 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
841 observer_.renegotiation_needed_ = false;
842 }
843
844 void AddVoiceStream(const std::string& label) {
845 // Create a local stream.
zhihuang9763d562016-08-05 11:14:50 -0700846 rtc::scoped_refptr<MediaStreamInterface> stream(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000847 pc_factory_->CreateLocalMediaStream(label));
zhihuang9763d562016-08-05 11:14:50 -0700848 rtc::scoped_refptr<AudioTrackInterface> audio_track(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000849 pc_factory_->CreateAudioTrack(label + "a0", NULL));
850 stream->AddTrack(audio_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +0000851 EXPECT_TRUE(pc_->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000852 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
853 observer_.renegotiation_needed_ = false;
854 }
855
856 void AddAudioVideoStream(const std::string& stream_label,
857 const std::string& audio_track_label,
858 const std::string& video_track_label) {
859 // Create a local stream.
zhihuang9763d562016-08-05 11:14:50 -0700860 rtc::scoped_refptr<MediaStreamInterface> stream(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000861 pc_factory_->CreateLocalMediaStream(stream_label));
zhihuang9763d562016-08-05 11:14:50 -0700862 rtc::scoped_refptr<AudioTrackInterface> audio_track(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000863 pc_factory_->CreateAudioTrack(
864 audio_track_label, static_cast<AudioSourceInterface*>(NULL)));
865 stream->AddTrack(audio_track.get());
zhihuang9763d562016-08-05 11:14:50 -0700866 rtc::scoped_refptr<VideoTrackInterface> video_track(
nisseaf510af2016-03-21 08:20:42 -0700867 pc_factory_->CreateVideoTrack(
deadbeef112b2e92017-02-10 20:13:37 -0800868 video_track_label, pc_factory_->CreateVideoSource(
869 std::unique_ptr<cricket::VideoCapturer>(
870 new cricket::FakeVideoCapturer()))));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000871 stream->AddTrack(video_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +0000872 EXPECT_TRUE(pc_->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000873 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
874 observer_.renegotiation_needed_ = false;
875 }
876
kwibergd1fe2812016-04-27 06:47:29 -0700877 bool DoCreateOfferAnswer(std::unique_ptr<SessionDescriptionInterface>* desc,
deadbeefc80741f2015-10-22 13:14:45 -0700878 bool offer,
879 MediaConstraintsInterface* constraints) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000880 rtc::scoped_refptr<MockCreateSessionDescriptionObserver>
881 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000882 MockCreateSessionDescriptionObserver>());
883 if (offer) {
deadbeefc80741f2015-10-22 13:14:45 -0700884 pc_->CreateOffer(observer, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000885 } else {
deadbeefc80741f2015-10-22 13:14:45 -0700886 pc_->CreateAnswer(observer, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000887 }
888 EXPECT_EQ_WAIT(true, observer->called(), kTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -0700889 *desc = observer->MoveDescription();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000890 return observer->result();
891 }
892
kwibergd1fe2812016-04-27 06:47:29 -0700893 bool DoCreateOffer(std::unique_ptr<SessionDescriptionInterface>* desc,
deadbeefc80741f2015-10-22 13:14:45 -0700894 MediaConstraintsInterface* constraints) {
895 return DoCreateOfferAnswer(desc, true, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000896 }
897
kwibergd1fe2812016-04-27 06:47:29 -0700898 bool DoCreateAnswer(std::unique_ptr<SessionDescriptionInterface>* desc,
deadbeefc80741f2015-10-22 13:14:45 -0700899 MediaConstraintsInterface* constraints) {
900 return DoCreateOfferAnswer(desc, false, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000901 }
902
903 bool DoSetSessionDescription(SessionDescriptionInterface* desc, bool local) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000904 rtc::scoped_refptr<MockSetSessionDescriptionObserver>
905 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000906 MockSetSessionDescriptionObserver>());
907 if (local) {
908 pc_->SetLocalDescription(observer, desc);
909 } else {
910 pc_->SetRemoteDescription(observer, desc);
911 }
zhihuang29ff8442016-07-27 11:07:25 -0700912 if (pc_->signaling_state() != PeerConnectionInterface::kClosed) {
913 EXPECT_EQ_WAIT(true, observer->called(), kTimeout);
914 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000915 return observer->result();
916 }
917
918 bool DoSetLocalDescription(SessionDescriptionInterface* desc) {
919 return DoSetSessionDescription(desc, true);
920 }
921
922 bool DoSetRemoteDescription(SessionDescriptionInterface* desc) {
923 return DoSetSessionDescription(desc, false);
924 }
925
926 // Calls PeerConnection::GetStats and check the return value.
927 // It does not verify the values in the StatReports since a RTCP packet might
928 // be required.
929 bool DoGetStats(MediaStreamTrackInterface* track) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000930 rtc::scoped_refptr<MockStatsObserver> observer(
931 new rtc::RefCountedObject<MockStatsObserver>());
jiayl@webrtc.orgdb41b4d2014-03-03 21:30:06 +0000932 if (!pc_->GetStats(
933 observer, track, PeerConnectionInterface::kStatsOutputLevelStandard))
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000934 return false;
935 EXPECT_TRUE_WAIT(observer->called(), kTimeout);
936 return observer->called();
937 }
938
939 void InitiateCall() {
deadbeef293e9262017-01-11 12:28:30 -0800940 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000941 // Create a local stream with audio&video tracks.
942 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
943 CreateOfferReceiveAnswer();
944 }
945
946 // Verify that RTP Header extensions has been negotiated for audio and video.
947 void VerifyRemoteRtpHeaderExtensions() {
948 const cricket::MediaContentDescription* desc =
949 cricket::GetFirstAudioContentDescription(
950 pc_->remote_description()->description());
951 ASSERT_TRUE(desc != NULL);
952 EXPECT_GT(desc->rtp_header_extensions().size(), 0u);
953
954 desc = cricket::GetFirstVideoContentDescription(
955 pc_->remote_description()->description());
956 ASSERT_TRUE(desc != NULL);
957 EXPECT_GT(desc->rtp_header_extensions().size(), 0u);
958 }
959
960 void CreateOfferAsRemoteDescription() {
kwibergd1fe2812016-04-27 06:47:29 -0700961 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -0700962 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000963 std::string sdp;
964 EXPECT_TRUE(offer->ToString(&sdp));
965 SessionDescriptionInterface* remote_offer =
966 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
967 sdp, NULL);
968 EXPECT_TRUE(DoSetRemoteDescription(remote_offer));
969 EXPECT_EQ(PeerConnectionInterface::kHaveRemoteOffer, observer_.state_);
970 }
971
deadbeefab9b2d12015-10-14 11:33:11 -0700972 void CreateAndSetRemoteOffer(const std::string& sdp) {
973 SessionDescriptionInterface* remote_offer =
974 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
975 sdp, nullptr);
976 EXPECT_TRUE(DoSetRemoteDescription(remote_offer));
977 EXPECT_EQ(PeerConnectionInterface::kHaveRemoteOffer, observer_.state_);
978 }
979
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000980 void CreateAnswerAsLocalDescription() {
kwibergd1fe2812016-04-27 06:47:29 -0700981 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -0700982 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000983
984 // TODO(perkj): Currently SetLocalDescription fails if any parameters in an
985 // audio codec change, even if the parameter has nothing to do with
986 // receiving. Not all parameters are serialized to SDP.
987 // Since CreatePrAnswerAsLocalDescription serialize/deserialize
988 // the SessionDescription, it is necessary to do that here to in order to
989 // get ReceiveOfferCreatePrAnswerAndAnswer and RenegotiateAudioOnly to pass.
990 // https://code.google.com/p/webrtc/issues/detail?id=1356
991 std::string sdp;
992 EXPECT_TRUE(answer->ToString(&sdp));
993 SessionDescriptionInterface* new_answer =
994 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
995 sdp, NULL);
996 EXPECT_TRUE(DoSetLocalDescription(new_answer));
997 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
998 }
999
1000 void CreatePrAnswerAsLocalDescription() {
kwibergd1fe2812016-04-27 06:47:29 -07001001 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -07001002 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001003
1004 std::string sdp;
1005 EXPECT_TRUE(answer->ToString(&sdp));
1006 SessionDescriptionInterface* pr_answer =
1007 webrtc::CreateSessionDescription(SessionDescriptionInterface::kPrAnswer,
1008 sdp, NULL);
1009 EXPECT_TRUE(DoSetLocalDescription(pr_answer));
1010 EXPECT_EQ(PeerConnectionInterface::kHaveLocalPrAnswer, observer_.state_);
1011 }
1012
1013 void CreateOfferReceiveAnswer() {
1014 CreateOfferAsLocalDescription();
1015 std::string sdp;
1016 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
1017 CreateAnswerAsRemoteDescription(sdp);
1018 }
1019
1020 void CreateOfferAsLocalDescription() {
kwibergd1fe2812016-04-27 06:47:29 -07001021 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001022 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001023 // TODO(perkj): Currently SetLocalDescription fails if any parameters in an
1024 // audio codec change, even if the parameter has nothing to do with
1025 // receiving. Not all parameters are serialized to SDP.
1026 // Since CreatePrAnswerAsLocalDescription serialize/deserialize
1027 // the SessionDescription, it is necessary to do that here to in order to
1028 // get ReceiveOfferCreatePrAnswerAndAnswer and RenegotiateAudioOnly to pass.
1029 // https://code.google.com/p/webrtc/issues/detail?id=1356
1030 std::string sdp;
1031 EXPECT_TRUE(offer->ToString(&sdp));
1032 SessionDescriptionInterface* new_offer =
1033 webrtc::CreateSessionDescription(
1034 SessionDescriptionInterface::kOffer,
1035 sdp, NULL);
1036
1037 EXPECT_TRUE(DoSetLocalDescription(new_offer));
1038 EXPECT_EQ(PeerConnectionInterface::kHaveLocalOffer, observer_.state_);
mallinath@webrtc.org68cbd012014-01-22 00:16:46 +00001039 // Wait for the ice_complete message, so that SDP will have candidates.
1040 EXPECT_TRUE_WAIT(observer_.ice_complete_, kTimeout);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001041 }
1042
deadbeefab9b2d12015-10-14 11:33:11 -07001043 void CreateAnswerAsRemoteDescription(const std::string& sdp) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001044 webrtc::JsepSessionDescription* answer = new webrtc::JsepSessionDescription(
1045 SessionDescriptionInterface::kAnswer);
deadbeefab9b2d12015-10-14 11:33:11 -07001046 EXPECT_TRUE(answer->Initialize(sdp, NULL));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001047 EXPECT_TRUE(DoSetRemoteDescription(answer));
1048 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
1049 }
1050
deadbeefab9b2d12015-10-14 11:33:11 -07001051 void CreatePrAnswerAndAnswerAsRemoteDescription(const std::string& sdp) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001052 webrtc::JsepSessionDescription* pr_answer =
1053 new webrtc::JsepSessionDescription(
1054 SessionDescriptionInterface::kPrAnswer);
deadbeefab9b2d12015-10-14 11:33:11 -07001055 EXPECT_TRUE(pr_answer->Initialize(sdp, NULL));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001056 EXPECT_TRUE(DoSetRemoteDescription(pr_answer));
1057 EXPECT_EQ(PeerConnectionInterface::kHaveRemotePrAnswer, observer_.state_);
1058 webrtc::JsepSessionDescription* answer =
1059 new webrtc::JsepSessionDescription(
1060 SessionDescriptionInterface::kAnswer);
deadbeefab9b2d12015-10-14 11:33:11 -07001061 EXPECT_TRUE(answer->Initialize(sdp, NULL));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001062 EXPECT_TRUE(DoSetRemoteDescription(answer));
1063 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
1064 }
1065
1066 // Help function used for waiting until a the last signaled remote stream has
1067 // the same label as |stream_label|. In a few of the tests in this file we
1068 // answer with the same session description as we offer and thus we can
1069 // check if OnAddStream have been called with the same stream as we offer to
1070 // send.
1071 void WaitAndVerifyOnAddStream(const std::string& stream_label) {
1072 EXPECT_EQ_WAIT(stream_label, observer_.GetLastAddedStreamLabel(), kTimeout);
1073 }
1074
1075 // Creates an offer and applies it as a local session description.
1076 // Creates an answer with the same SDP an the offer but removes all lines
1077 // that start with a:ssrc"
1078 void CreateOfferReceiveAnswerWithoutSsrc() {
1079 CreateOfferAsLocalDescription();
1080 std::string sdp;
1081 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
1082 SetSsrcToZero(&sdp);
1083 CreateAnswerAsRemoteDescription(sdp);
1084 }
1085
deadbeefab9b2d12015-10-14 11:33:11 -07001086 // This function creates a MediaStream with label kStreams[0] and
1087 // |number_of_audio_tracks| and |number_of_video_tracks| tracks and the
1088 // corresponding SessionDescriptionInterface. The SessionDescriptionInterface
kwiberg2bbff992016-03-16 11:03:04 -07001089 // is returned and the MediaStream is stored in
deadbeefab9b2d12015-10-14 11:33:11 -07001090 // |reference_collection_|
kwibergd1fe2812016-04-27 06:47:29 -07001091 std::unique_ptr<SessionDescriptionInterface>
kwiberg2bbff992016-03-16 11:03:04 -07001092 CreateSessionDescriptionAndReference(size_t number_of_audio_tracks,
1093 size_t number_of_video_tracks) {
1094 EXPECT_LE(number_of_audio_tracks, 2u);
1095 EXPECT_LE(number_of_video_tracks, 2u);
deadbeefab9b2d12015-10-14 11:33:11 -07001096
1097 reference_collection_ = StreamCollection::Create();
1098 std::string sdp_ms1 = std::string(kSdpStringInit);
1099
1100 std::string mediastream_label = kStreams[0];
1101
1102 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream(
1103 webrtc::MediaStream::Create(mediastream_label));
1104 reference_collection_->AddStream(stream);
1105
1106 if (number_of_audio_tracks > 0) {
1107 sdp_ms1 += std::string(kSdpStringAudio);
1108 sdp_ms1 += std::string(kSdpStringMs1Audio0);
1109 AddAudioTrack(kAudioTracks[0], stream);
1110 }
1111 if (number_of_audio_tracks > 1) {
1112 sdp_ms1 += kSdpStringMs1Audio1;
1113 AddAudioTrack(kAudioTracks[1], stream);
1114 }
1115
1116 if (number_of_video_tracks > 0) {
1117 sdp_ms1 += std::string(kSdpStringVideo);
1118 sdp_ms1 += std::string(kSdpStringMs1Video0);
1119 AddVideoTrack(kVideoTracks[0], stream);
1120 }
1121 if (number_of_video_tracks > 1) {
1122 sdp_ms1 += kSdpStringMs1Video1;
1123 AddVideoTrack(kVideoTracks[1], stream);
1124 }
1125
kwibergd1fe2812016-04-27 06:47:29 -07001126 return std::unique_ptr<SessionDescriptionInterface>(
kwiberg2bbff992016-03-16 11:03:04 -07001127 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
1128 sdp_ms1, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07001129 }
1130
1131 void AddAudioTrack(const std::string& track_id,
1132 MediaStreamInterface* stream) {
1133 rtc::scoped_refptr<webrtc::AudioTrackInterface> audio_track(
1134 webrtc::AudioTrack::Create(track_id, nullptr));
1135 ASSERT_TRUE(stream->AddTrack(audio_track));
1136 }
1137
1138 void AddVideoTrack(const std::string& track_id,
1139 MediaStreamInterface* stream) {
1140 rtc::scoped_refptr<webrtc::VideoTrackInterface> video_track(
nisseaf510af2016-03-21 08:20:42 -07001141 webrtc::VideoTrack::Create(track_id,
mbonadei539d1042017-07-10 02:40:49 -07001142 webrtc::FakeVideoTrackSource::Create()));
deadbeefab9b2d12015-10-14 11:33:11 -07001143 ASSERT_TRUE(stream->AddTrack(video_track));
1144 }
1145
kwibergfd8be342016-05-14 19:44:11 -07001146 std::unique_ptr<SessionDescriptionInterface> CreateOfferWithOneAudioStream() {
deadbeef293e9262017-01-11 12:28:30 -08001147 CreatePeerConnectionWithoutDtls();
zhihuang8f65cdf2016-05-06 18:40:30 -07001148 AddVoiceStream(kStreamLabel1);
kwibergfd8be342016-05-14 19:44:11 -07001149 std::unique_ptr<SessionDescriptionInterface> offer;
zhihuang8f65cdf2016-05-06 18:40:30 -07001150 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
1151 return offer;
1152 }
1153
kwibergfd8be342016-05-14 19:44:11 -07001154 std::unique_ptr<SessionDescriptionInterface>
zhihuang8f65cdf2016-05-06 18:40:30 -07001155 CreateAnswerWithOneAudioStream() {
kwibergfd8be342016-05-14 19:44:11 -07001156 std::unique_ptr<SessionDescriptionInterface> offer =
zhihuang8f65cdf2016-05-06 18:40:30 -07001157 CreateOfferWithOneAudioStream();
1158 EXPECT_TRUE(DoSetRemoteDescription(offer.release()));
kwibergfd8be342016-05-14 19:44:11 -07001159 std::unique_ptr<SessionDescriptionInterface> answer;
zhihuang8f65cdf2016-05-06 18:40:30 -07001160 EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
1161 return answer;
1162 }
1163
1164 const std::string& GetFirstAudioStreamCname(
1165 const SessionDescriptionInterface* desc) {
1166 const cricket::ContentInfo* audio_content =
1167 cricket::GetFirstAudioContent(desc->description());
1168 const cricket::AudioContentDescription* audio_desc =
1169 static_cast<const cricket::AudioContentDescription*>(
1170 audio_content->description);
1171 return audio_desc->streams()[0].cname;
1172 }
1173
deadbeef9a6f4d42017-05-15 19:43:33 -07001174 std::unique_ptr<rtc::VirtualSocketServer> vss_;
1175 rtc::AutoSocketServerThread main_;
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08001176 cricket::FakePortAllocator* port_allocator_ = nullptr;
deadbeef8662f942017-01-20 21:20:51 -08001177 FakeRTCCertificateGenerator* fake_certificate_generator_ = nullptr;
zhihuang9763d562016-08-05 11:14:50 -07001178 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface> pc_factory_;
1179 rtc::scoped_refptr<PeerConnectionFactoryForTest> pc_factory_for_test_;
1180 rtc::scoped_refptr<PeerConnectionInterface> pc_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001181 MockPeerConnectionObserver observer_;
deadbeefab9b2d12015-10-14 11:33:11 -07001182 rtc::scoped_refptr<StreamCollection> reference_collection_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001183};
1184
zhihuang29ff8442016-07-27 11:07:25 -07001185// Test that no callbacks on the PeerConnectionObserver are called after the
1186// PeerConnection is closed.
1187TEST_F(PeerConnectionInterfaceTest, CloseAndTestCallbackFunctions) {
zhihuang9763d562016-08-05 11:14:50 -07001188 rtc::scoped_refptr<PeerConnectionInterface> pc(
zhihuang29ff8442016-07-27 11:07:25 -07001189 pc_factory_for_test_->CreatePeerConnection(
1190 PeerConnectionInterface::RTCConfiguration(), nullptr, nullptr,
1191 nullptr, &observer_));
1192 observer_.SetPeerConnectionInterface(pc.get());
1193 pc->Close();
1194
1195 // No callbacks is expected to be called.
zhihuang81c3a032016-11-17 12:06:24 -08001196 observer_.callback_triggered_ = false;
zhihuang29ff8442016-07-27 11:07:25 -07001197 std::vector<cricket::Candidate> candidates;
1198 pc_factory_for_test_->transport_controller->SignalGatheringState(
1199 cricket::IceGatheringState{});
1200 pc_factory_for_test_->transport_controller->SignalCandidatesGathered(
1201 "", candidates);
1202 pc_factory_for_test_->transport_controller->SignalConnectionState(
1203 cricket::IceConnectionState{});
1204 pc_factory_for_test_->transport_controller->SignalCandidatesRemoved(
1205 candidates);
1206 pc_factory_for_test_->transport_controller->SignalReceiving(false);
zhihuang81c3a032016-11-17 12:06:24 -08001207 EXPECT_FALSE(observer_.callback_triggered_);
zhihuang29ff8442016-07-27 11:07:25 -07001208}
1209
zhihuang8f65cdf2016-05-06 18:40:30 -07001210// Generate different CNAMEs when PeerConnections are created.
1211// The CNAMEs are expected to be generated randomly. It is possible
1212// that the test fails, though the possibility is very low.
1213TEST_F(PeerConnectionInterfaceTest, CnameGenerationInOffer) {
kwibergfd8be342016-05-14 19:44:11 -07001214 std::unique_ptr<SessionDescriptionInterface> offer1 =
zhihuang8f65cdf2016-05-06 18:40:30 -07001215 CreateOfferWithOneAudioStream();
kwibergfd8be342016-05-14 19:44:11 -07001216 std::unique_ptr<SessionDescriptionInterface> offer2 =
zhihuang8f65cdf2016-05-06 18:40:30 -07001217 CreateOfferWithOneAudioStream();
1218 EXPECT_NE(GetFirstAudioStreamCname(offer1.get()),
1219 GetFirstAudioStreamCname(offer2.get()));
1220}
1221
1222TEST_F(PeerConnectionInterfaceTest, CnameGenerationInAnswer) {
kwibergfd8be342016-05-14 19:44:11 -07001223 std::unique_ptr<SessionDescriptionInterface> answer1 =
zhihuang8f65cdf2016-05-06 18:40:30 -07001224 CreateAnswerWithOneAudioStream();
kwibergfd8be342016-05-14 19:44:11 -07001225 std::unique_ptr<SessionDescriptionInterface> answer2 =
zhihuang8f65cdf2016-05-06 18:40:30 -07001226 CreateAnswerWithOneAudioStream();
1227 EXPECT_NE(GetFirstAudioStreamCname(answer1.get()),
1228 GetFirstAudioStreamCname(answer2.get()));
1229}
1230
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001231TEST_F(PeerConnectionInterfaceTest,
1232 CreatePeerConnectionWithDifferentConfigurations) {
1233 CreatePeerConnectionWithDifferentConfigurations();
1234}
1235
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001236TEST_F(PeerConnectionInterfaceTest,
1237 CreatePeerConnectionWithDifferentIceTransportsTypes) {
1238 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kNone);
1239 EXPECT_EQ(cricket::CF_NONE, port_allocator_->candidate_filter());
1240 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kRelay);
1241 EXPECT_EQ(cricket::CF_RELAY, port_allocator_->candidate_filter());
1242 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kNoHost);
1243 EXPECT_EQ(cricket::CF_ALL & ~cricket::CF_HOST,
1244 port_allocator_->candidate_filter());
1245 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kAll);
1246 EXPECT_EQ(cricket::CF_ALL, port_allocator_->candidate_filter());
1247}
1248
1249// Test that when a PeerConnection is created with a nonzero candidate pool
1250// size, the pooled PortAllocatorSession is created with all the attributes
1251// in the RTCConfiguration.
1252TEST_F(PeerConnectionInterfaceTest, CreatePeerConnectionWithPooledCandidates) {
1253 PeerConnectionInterface::RTCConfiguration config;
1254 PeerConnectionInterface::IceServer server;
1255 server.uri = kStunAddressOnly;
1256 config.servers.push_back(server);
1257 config.type = PeerConnectionInterface::kRelay;
1258 config.disable_ipv6 = true;
1259 config.tcp_candidate_policy =
1260 PeerConnectionInterface::kTcpCandidatePolicyDisabled;
honghaiz60347052016-05-31 18:29:12 -07001261 config.candidate_network_policy =
1262 PeerConnectionInterface::kCandidateNetworkPolicyLowCost;
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001263 config.ice_candidate_pool_size = 1;
1264 CreatePeerConnection(config, nullptr);
1265
1266 const cricket::FakePortAllocatorSession* session =
1267 static_cast<const cricket::FakePortAllocatorSession*>(
1268 port_allocator_->GetPooledSession());
1269 ASSERT_NE(nullptr, session);
1270 EXPECT_EQ(1UL, session->stun_servers().size());
1271 EXPECT_EQ(0U, session->flags() & cricket::PORTALLOCATOR_ENABLE_IPV6);
1272 EXPECT_LT(0U, session->flags() & cricket::PORTALLOCATOR_DISABLE_TCP);
honghaiz60347052016-05-31 18:29:12 -07001273 EXPECT_LT(0U,
1274 session->flags() & cricket::PORTALLOCATOR_DISABLE_COSTLY_NETWORKS);
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001275}
1276
Taylor Brandstetterf8e65772016-06-27 17:20:15 -07001277// Test that the PeerConnection initializes the port allocator passed into it,
1278// and on the correct thread.
1279TEST_F(PeerConnectionInterfaceTest,
1280 CreatePeerConnectionInitializesPortAllocator) {
tommia8a35152017-07-13 05:47:25 -07001281 std::unique_ptr<rtc::Thread> network_thread(
1282 rtc::Thread::CreateWithSocketServer());
1283 network_thread->Start();
Taylor Brandstetterf8e65772016-06-27 17:20:15 -07001284 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface> pc_factory(
1285 webrtc::CreatePeerConnectionFactory(
tommia8a35152017-07-13 05:47:25 -07001286 network_thread.get(), rtc::Thread::Current(), rtc::Thread::Current(),
Taylor Brandstetterf8e65772016-06-27 17:20:15 -07001287 nullptr, nullptr, nullptr));
1288 std::unique_ptr<cricket::FakePortAllocator> port_allocator(
tommia8a35152017-07-13 05:47:25 -07001289 new cricket::FakePortAllocator(network_thread.get(), nullptr));
Taylor Brandstetterf8e65772016-06-27 17:20:15 -07001290 cricket::FakePortAllocator* raw_port_allocator = port_allocator.get();
1291 PeerConnectionInterface::RTCConfiguration config;
1292 rtc::scoped_refptr<PeerConnectionInterface> pc(
1293 pc_factory->CreatePeerConnection(
1294 config, nullptr, std::move(port_allocator), nullptr, &observer_));
1295 // FakePortAllocator RTC_CHECKs that it's initialized on the right thread,
1296 // so all we have to do here is check that it's initialized.
1297 EXPECT_TRUE(raw_port_allocator->initialized());
1298}
1299
deadbeef46c73892016-11-16 19:42:04 -08001300// Check that GetConfiguration returns the configuration the PeerConnection was
1301// constructed with, before SetConfiguration is called.
1302TEST_F(PeerConnectionInterfaceTest, GetConfigurationAfterCreatePeerConnection) {
1303 PeerConnectionInterface::RTCConfiguration config;
1304 config.type = PeerConnectionInterface::kRelay;
1305 CreatePeerConnection(config, nullptr);
1306
1307 PeerConnectionInterface::RTCConfiguration returned_config =
1308 pc_->GetConfiguration();
1309 EXPECT_EQ(PeerConnectionInterface::kRelay, returned_config.type);
1310}
1311
1312// Check that GetConfiguration returns the last configuration passed into
1313// SetConfiguration.
1314TEST_F(PeerConnectionInterfaceTest, GetConfigurationAfterSetConfiguration) {
1315 CreatePeerConnection();
1316
1317 PeerConnectionInterface::RTCConfiguration config;
1318 config.type = PeerConnectionInterface::kRelay;
1319 EXPECT_TRUE(pc_->SetConfiguration(config));
1320
1321 PeerConnectionInterface::RTCConfiguration returned_config =
1322 pc_->GetConfiguration();
1323 EXPECT_EQ(PeerConnectionInterface::kRelay, returned_config.type);
1324}
1325
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001326TEST_F(PeerConnectionInterfaceTest, AddStreams) {
deadbeef293e9262017-01-11 12:28:30 -08001327 CreatePeerConnectionWithoutDtls();
deadbeefab9b2d12015-10-14 11:33:11 -07001328 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001329 AddVoiceStream(kStreamLabel2);
1330 ASSERT_EQ(2u, pc_->local_streams()->count());
1331
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001332 // Test we can add multiple local streams to one peerconnection.
zhihuang9763d562016-08-05 11:14:50 -07001333 rtc::scoped_refptr<MediaStreamInterface> stream(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001334 pc_factory_->CreateLocalMediaStream(kStreamLabel3));
zhihuang9763d562016-08-05 11:14:50 -07001335 rtc::scoped_refptr<AudioTrackInterface> audio_track(
1336 pc_factory_->CreateAudioTrack(kStreamLabel3,
1337 static_cast<AudioSourceInterface*>(NULL)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001338 stream->AddTrack(audio_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +00001339 EXPECT_TRUE(pc_->AddStream(stream));
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001340 EXPECT_EQ(3u, pc_->local_streams()->count());
1341
1342 // Remove the third stream.
1343 pc_->RemoveStream(pc_->local_streams()->at(2));
1344 EXPECT_EQ(2u, pc_->local_streams()->count());
1345
1346 // Remove the second stream.
1347 pc_->RemoveStream(pc_->local_streams()->at(1));
1348 EXPECT_EQ(1u, pc_->local_streams()->count());
1349
1350 // Remove the first stream.
1351 pc_->RemoveStream(pc_->local_streams()->at(0));
1352 EXPECT_EQ(0u, pc_->local_streams()->count());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001353}
1354
deadbeefab9b2d12015-10-14 11:33:11 -07001355// Test that the created offer includes streams we added.
1356TEST_F(PeerConnectionInterfaceTest, AddedStreamsPresentInOffer) {
deadbeef293e9262017-01-11 12:28:30 -08001357 CreatePeerConnectionWithoutDtls();
deadbeefab9b2d12015-10-14 11:33:11 -07001358 AddAudioVideoStream(kStreamLabel1, "audio_track", "video_track");
kwibergd1fe2812016-04-27 06:47:29 -07001359 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001360 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07001361
1362 const cricket::ContentInfo* audio_content =
1363 cricket::GetFirstAudioContent(offer->description());
1364 const cricket::AudioContentDescription* audio_desc =
1365 static_cast<const cricket::AudioContentDescription*>(
1366 audio_content->description);
1367 EXPECT_TRUE(
1368 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
1369
1370 const cricket::ContentInfo* video_content =
1371 cricket::GetFirstVideoContent(offer->description());
1372 const cricket::VideoContentDescription* video_desc =
1373 static_cast<const cricket::VideoContentDescription*>(
1374 video_content->description);
1375 EXPECT_TRUE(
1376 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1377
1378 // Add another stream and ensure the offer includes both the old and new
1379 // streams.
1380 AddAudioVideoStream(kStreamLabel2, "audio_track2", "video_track2");
kwiberg2bbff992016-03-16 11:03:04 -07001381 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07001382
1383 audio_content = cricket::GetFirstAudioContent(offer->description());
1384 audio_desc = static_cast<const cricket::AudioContentDescription*>(
1385 audio_content->description);
1386 EXPECT_TRUE(
1387 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
1388 EXPECT_TRUE(
1389 ContainsTrack(audio_desc->streams(), kStreamLabel2, "audio_track2"));
1390
1391 video_content = cricket::GetFirstVideoContent(offer->description());
1392 video_desc = static_cast<const cricket::VideoContentDescription*>(
1393 video_content->description);
1394 EXPECT_TRUE(
1395 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1396 EXPECT_TRUE(
1397 ContainsTrack(video_desc->streams(), kStreamLabel2, "video_track2"));
1398}
1399
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001400TEST_F(PeerConnectionInterfaceTest, RemoveStream) {
deadbeef293e9262017-01-11 12:28:30 -08001401 CreatePeerConnectionWithoutDtls();
deadbeefab9b2d12015-10-14 11:33:11 -07001402 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001403 ASSERT_EQ(1u, pc_->local_streams()->count());
1404 pc_->RemoveStream(pc_->local_streams()->at(0));
1405 EXPECT_EQ(0u, pc_->local_streams()->count());
1406}
1407
deadbeefe1f9d832016-01-14 15:35:42 -08001408// Test for AddTrack and RemoveTrack methods.
1409// Tests that the created offer includes tracks we added,
1410// and that the RtpSenders are created correctly.
1411// Also tests that RemoveTrack removes the tracks from subsequent offers.
1412TEST_F(PeerConnectionInterfaceTest, AddTrackRemoveTrack) {
deadbeef293e9262017-01-11 12:28:30 -08001413 CreatePeerConnectionWithoutDtls();
deadbeefe1f9d832016-01-14 15:35:42 -08001414 // Create a dummy stream, so tracks share a stream label.
zhihuang9763d562016-08-05 11:14:50 -07001415 rtc::scoped_refptr<MediaStreamInterface> stream(
deadbeefe1f9d832016-01-14 15:35:42 -08001416 pc_factory_->CreateLocalMediaStream(kStreamLabel1));
1417 std::vector<MediaStreamInterface*> stream_list;
1418 stream_list.push_back(stream.get());
zhihuang9763d562016-08-05 11:14:50 -07001419 rtc::scoped_refptr<AudioTrackInterface> audio_track(
deadbeefe1f9d832016-01-14 15:35:42 -08001420 pc_factory_->CreateAudioTrack("audio_track", nullptr));
zhihuang9763d562016-08-05 11:14:50 -07001421 rtc::scoped_refptr<VideoTrackInterface> video_track(
1422 pc_factory_->CreateVideoTrack(
deadbeef112b2e92017-02-10 20:13:37 -08001423 "video_track", pc_factory_->CreateVideoSource(
1424 std::unique_ptr<cricket::VideoCapturer>(
1425 new cricket::FakeVideoCapturer()))));
deadbeefe1f9d832016-01-14 15:35:42 -08001426 auto audio_sender = pc_->AddTrack(audio_track, stream_list);
1427 auto video_sender = pc_->AddTrack(video_track, stream_list);
deadbeefa601f5c2016-06-06 14:27:39 -07001428 EXPECT_EQ(1UL, audio_sender->stream_ids().size());
1429 EXPECT_EQ(kStreamLabel1, audio_sender->stream_ids()[0]);
deadbeefe1f9d832016-01-14 15:35:42 -08001430 EXPECT_EQ("audio_track", audio_sender->id());
1431 EXPECT_EQ(audio_track, audio_sender->track());
deadbeefa601f5c2016-06-06 14:27:39 -07001432 EXPECT_EQ(1UL, video_sender->stream_ids().size());
1433 EXPECT_EQ(kStreamLabel1, video_sender->stream_ids()[0]);
deadbeefe1f9d832016-01-14 15:35:42 -08001434 EXPECT_EQ("video_track", video_sender->id());
1435 EXPECT_EQ(video_track, video_sender->track());
1436
1437 // Now create an offer and check for the senders.
kwibergd1fe2812016-04-27 06:47:29 -07001438 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001439 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefe1f9d832016-01-14 15:35:42 -08001440
1441 const cricket::ContentInfo* audio_content =
1442 cricket::GetFirstAudioContent(offer->description());
1443 const cricket::AudioContentDescription* audio_desc =
1444 static_cast<const cricket::AudioContentDescription*>(
1445 audio_content->description);
1446 EXPECT_TRUE(
1447 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
1448
1449 const cricket::ContentInfo* video_content =
1450 cricket::GetFirstVideoContent(offer->description());
1451 const cricket::VideoContentDescription* video_desc =
1452 static_cast<const cricket::VideoContentDescription*>(
1453 video_content->description);
1454 EXPECT_TRUE(
1455 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1456
1457 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
1458
1459 // Now try removing the tracks.
1460 EXPECT_TRUE(pc_->RemoveTrack(audio_sender));
1461 EXPECT_TRUE(pc_->RemoveTrack(video_sender));
1462
1463 // Create a new offer and ensure it doesn't contain the removed senders.
kwiberg2bbff992016-03-16 11:03:04 -07001464 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefe1f9d832016-01-14 15:35:42 -08001465
1466 audio_content = cricket::GetFirstAudioContent(offer->description());
1467 audio_desc = static_cast<const cricket::AudioContentDescription*>(
1468 audio_content->description);
1469 EXPECT_FALSE(
1470 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
1471
1472 video_content = cricket::GetFirstVideoContent(offer->description());
1473 video_desc = static_cast<const cricket::VideoContentDescription*>(
1474 video_content->description);
1475 EXPECT_FALSE(
1476 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1477
1478 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
1479
1480 // Calling RemoveTrack on a sender no longer attached to a PeerConnection
1481 // should return false.
1482 EXPECT_FALSE(pc_->RemoveTrack(audio_sender));
1483 EXPECT_FALSE(pc_->RemoveTrack(video_sender));
1484}
1485
1486// Test creating senders without a stream specified,
1487// expecting a random stream ID to be generated.
1488TEST_F(PeerConnectionInterfaceTest, AddTrackWithoutStream) {
deadbeef293e9262017-01-11 12:28:30 -08001489 CreatePeerConnectionWithoutDtls();
deadbeefe1f9d832016-01-14 15:35:42 -08001490 // Create a dummy stream, so tracks share a stream label.
zhihuang9763d562016-08-05 11:14:50 -07001491 rtc::scoped_refptr<AudioTrackInterface> audio_track(
deadbeefe1f9d832016-01-14 15:35:42 -08001492 pc_factory_->CreateAudioTrack("audio_track", nullptr));
zhihuang9763d562016-08-05 11:14:50 -07001493 rtc::scoped_refptr<VideoTrackInterface> video_track(
1494 pc_factory_->CreateVideoTrack(
deadbeef112b2e92017-02-10 20:13:37 -08001495 "video_track", pc_factory_->CreateVideoSource(
1496 std::unique_ptr<cricket::VideoCapturer>(
1497 new cricket::FakeVideoCapturer()))));
deadbeefe1f9d832016-01-14 15:35:42 -08001498 auto audio_sender =
1499 pc_->AddTrack(audio_track, std::vector<MediaStreamInterface*>());
1500 auto video_sender =
1501 pc_->AddTrack(video_track, std::vector<MediaStreamInterface*>());
1502 EXPECT_EQ("audio_track", audio_sender->id());
1503 EXPECT_EQ(audio_track, audio_sender->track());
1504 EXPECT_EQ("video_track", video_sender->id());
1505 EXPECT_EQ(video_track, video_sender->track());
1506 // If the ID is truly a random GUID, it should be infinitely unlikely they
1507 // will be the same.
deadbeefa601f5c2016-06-06 14:27:39 -07001508 EXPECT_NE(video_sender->stream_ids(), audio_sender->stream_ids());
deadbeefe1f9d832016-01-14 15:35:42 -08001509}
1510
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001511TEST_F(PeerConnectionInterfaceTest, CreateOfferReceiveAnswer) {
1512 InitiateCall();
1513 WaitAndVerifyOnAddStream(kStreamLabel1);
1514 VerifyRemoteRtpHeaderExtensions();
1515}
1516
1517TEST_F(PeerConnectionInterfaceTest, CreateOfferReceivePrAnswerAndAnswer) {
deadbeef293e9262017-01-11 12:28:30 -08001518 CreatePeerConnectionWithoutDtls();
deadbeefab9b2d12015-10-14 11:33:11 -07001519 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001520 CreateOfferAsLocalDescription();
1521 std::string offer;
1522 EXPECT_TRUE(pc_->local_description()->ToString(&offer));
1523 CreatePrAnswerAndAnswerAsRemoteDescription(offer);
1524 WaitAndVerifyOnAddStream(kStreamLabel1);
1525}
1526
1527TEST_F(PeerConnectionInterfaceTest, ReceiveOfferCreateAnswer) {
deadbeef293e9262017-01-11 12:28:30 -08001528 CreatePeerConnectionWithoutDtls();
deadbeefab9b2d12015-10-14 11:33:11 -07001529 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001530
1531 CreateOfferAsRemoteDescription();
1532 CreateAnswerAsLocalDescription();
1533
1534 WaitAndVerifyOnAddStream(kStreamLabel1);
1535}
1536
1537TEST_F(PeerConnectionInterfaceTest, ReceiveOfferCreatePrAnswerAndAnswer) {
deadbeef293e9262017-01-11 12:28:30 -08001538 CreatePeerConnectionWithoutDtls();
deadbeefab9b2d12015-10-14 11:33:11 -07001539 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001540
1541 CreateOfferAsRemoteDescription();
1542 CreatePrAnswerAsLocalDescription();
1543 CreateAnswerAsLocalDescription();
1544
1545 WaitAndVerifyOnAddStream(kStreamLabel1);
1546}
1547
1548TEST_F(PeerConnectionInterfaceTest, Renegotiate) {
1549 InitiateCall();
1550 ASSERT_EQ(1u, pc_->remote_streams()->count());
1551 pc_->RemoveStream(pc_->local_streams()->at(0));
1552 CreateOfferReceiveAnswer();
1553 EXPECT_EQ(0u, pc_->remote_streams()->count());
deadbeefab9b2d12015-10-14 11:33:11 -07001554 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001555 CreateOfferReceiveAnswer();
1556}
1557
1558// Tests that after negotiating an audio only call, the respondent can perform a
1559// renegotiation that removes the audio stream.
1560TEST_F(PeerConnectionInterfaceTest, RenegotiateAudioOnly) {
deadbeef293e9262017-01-11 12:28:30 -08001561 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001562 AddVoiceStream(kStreamLabel1);
1563 CreateOfferAsRemoteDescription();
1564 CreateAnswerAsLocalDescription();
1565
1566 ASSERT_EQ(1u, pc_->remote_streams()->count());
1567 pc_->RemoveStream(pc_->local_streams()->at(0));
1568 CreateOfferReceiveAnswer();
1569 EXPECT_EQ(0u, pc_->remote_streams()->count());
1570}
1571
1572// Test that candidates are generated and that we can parse our own candidates.
1573TEST_F(PeerConnectionInterfaceTest, IceCandidates) {
deadbeef293e9262017-01-11 12:28:30 -08001574 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001575
1576 EXPECT_FALSE(pc_->AddIceCandidate(observer_.last_candidate_.get()));
1577 // SetRemoteDescription takes ownership of offer.
kwibergd1fe2812016-04-27 06:47:29 -07001578 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefab9b2d12015-10-14 11:33:11 -07001579 AddVideoStream(kStreamLabel1);
deadbeefc80741f2015-10-22 13:14:45 -07001580 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
kwiberg2bbff992016-03-16 11:03:04 -07001581 EXPECT_TRUE(DoSetRemoteDescription(offer.release()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001582
1583 // SetLocalDescription takes ownership of answer.
kwibergd1fe2812016-04-27 06:47:29 -07001584 std::unique_ptr<SessionDescriptionInterface> answer;
deadbeefc80741f2015-10-22 13:14:45 -07001585 EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
kwiberg2bbff992016-03-16 11:03:04 -07001586 EXPECT_TRUE(DoSetLocalDescription(answer.release()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001587
1588 EXPECT_TRUE_WAIT(observer_.last_candidate_.get() != NULL, kTimeout);
1589 EXPECT_TRUE_WAIT(observer_.ice_complete_, kTimeout);
1590
1591 EXPECT_TRUE(pc_->AddIceCandidate(observer_.last_candidate_.get()));
1592}
1593
deadbeefab9b2d12015-10-14 11:33:11 -07001594// Test that CreateOffer and CreateAnswer will fail if the track labels are
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001595// not unique.
1596TEST_F(PeerConnectionInterfaceTest, CreateOfferAnswerWithInvalidStream) {
deadbeef293e9262017-01-11 12:28:30 -08001597 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001598 // Create a regular offer for the CreateAnswer test later.
kwibergd1fe2812016-04-27 06:47:29 -07001599 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefc80741f2015-10-22 13:14:45 -07001600 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
kwiberg2bbff992016-03-16 11:03:04 -07001601 EXPECT_TRUE(offer);
1602 offer.reset();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001603
1604 // Create a local stream with audio&video tracks having same label.
1605 AddAudioVideoStream(kStreamLabel1, "track_label", "track_label");
1606
1607 // Test CreateOffer
deadbeefc80741f2015-10-22 13:14:45 -07001608 EXPECT_FALSE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001609
1610 // Test CreateAnswer
kwibergd1fe2812016-04-27 06:47:29 -07001611 std::unique_ptr<SessionDescriptionInterface> answer;
deadbeefc80741f2015-10-22 13:14:45 -07001612 EXPECT_FALSE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001613}
1614
1615// Test that we will get different SSRCs for each tracks in the offer and answer
1616// we created.
1617TEST_F(PeerConnectionInterfaceTest, SsrcInOfferAnswer) {
deadbeef293e9262017-01-11 12:28:30 -08001618 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001619 // Create a local stream with audio&video tracks having different labels.
1620 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
1621
1622 // Test CreateOffer
kwibergd1fe2812016-04-27 06:47:29 -07001623 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001624 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001625 int audio_ssrc = 0;
1626 int video_ssrc = 0;
1627 EXPECT_TRUE(GetFirstSsrc(GetFirstAudioContent(offer->description()),
1628 &audio_ssrc));
1629 EXPECT_TRUE(GetFirstSsrc(GetFirstVideoContent(offer->description()),
1630 &video_ssrc));
1631 EXPECT_NE(audio_ssrc, video_ssrc);
1632
1633 // Test CreateAnswer
1634 EXPECT_TRUE(DoSetRemoteDescription(offer.release()));
kwibergd1fe2812016-04-27 06:47:29 -07001635 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -07001636 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001637 audio_ssrc = 0;
1638 video_ssrc = 0;
1639 EXPECT_TRUE(GetFirstSsrc(GetFirstAudioContent(answer->description()),
1640 &audio_ssrc));
1641 EXPECT_TRUE(GetFirstSsrc(GetFirstVideoContent(answer->description()),
1642 &video_ssrc));
1643 EXPECT_NE(audio_ssrc, video_ssrc);
1644}
1645
deadbeefeb459812015-12-15 19:24:43 -08001646// Test that it's possible to call AddTrack on a MediaStream after adding
1647// the stream to a PeerConnection.
1648// TODO(deadbeef): Remove this test once this behavior is no longer supported.
1649TEST_F(PeerConnectionInterfaceTest, AddTrackAfterAddStream) {
deadbeef293e9262017-01-11 12:28:30 -08001650 CreatePeerConnectionWithoutDtls();
deadbeefeb459812015-12-15 19:24:43 -08001651 // Create audio stream and add to PeerConnection.
1652 AddVoiceStream(kStreamLabel1);
1653 MediaStreamInterface* stream = pc_->local_streams()->at(0);
1654
1655 // Add video track to the audio-only stream.
zhihuang9763d562016-08-05 11:14:50 -07001656 rtc::scoped_refptr<VideoTrackInterface> video_track(
1657 pc_factory_->CreateVideoTrack(
deadbeef112b2e92017-02-10 20:13:37 -08001658 "video_label", pc_factory_->CreateVideoSource(
1659 std::unique_ptr<cricket::VideoCapturer>(
1660 new cricket::FakeVideoCapturer()))));
deadbeefeb459812015-12-15 19:24:43 -08001661 stream->AddTrack(video_track.get());
1662
kwibergd1fe2812016-04-27 06:47:29 -07001663 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001664 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefeb459812015-12-15 19:24:43 -08001665
1666 const cricket::MediaContentDescription* video_desc =
1667 cricket::GetFirstVideoContentDescription(offer->description());
1668 EXPECT_TRUE(video_desc != nullptr);
1669}
1670
1671// Test that it's possible to call RemoveTrack on a MediaStream after adding
1672// the stream to a PeerConnection.
1673// TODO(deadbeef): Remove this test once this behavior is no longer supported.
1674TEST_F(PeerConnectionInterfaceTest, RemoveTrackAfterAddStream) {
deadbeef293e9262017-01-11 12:28:30 -08001675 CreatePeerConnectionWithoutDtls();
deadbeefeb459812015-12-15 19:24:43 -08001676 // Create audio/video stream and add to PeerConnection.
1677 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
1678 MediaStreamInterface* stream = pc_->local_streams()->at(0);
1679
1680 // Remove the video track.
1681 stream->RemoveTrack(stream->GetVideoTracks()[0]);
1682
kwibergd1fe2812016-04-27 06:47:29 -07001683 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001684 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefeb459812015-12-15 19:24:43 -08001685
1686 const cricket::MediaContentDescription* video_desc =
1687 cricket::GetFirstVideoContentDescription(offer->description());
1688 EXPECT_TRUE(video_desc == nullptr);
1689}
1690
deadbeef1dcb1642017-03-29 21:08:16 -07001691// Verify that CreateDtmfSender only succeeds if called with a valid local
1692// track. Other aspects of DtmfSenders are tested in
1693// peerconnection_integrationtest.cc.
1694TEST_F(PeerConnectionInterfaceTest, CreateDtmfSenderWithInvalidParams) {
1695 CreatePeerConnection();
1696 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
1697 EXPECT_EQ(nullptr, pc_->CreateDtmfSender(nullptr));
1698 rtc::scoped_refptr<webrtc::AudioTrackInterface> non_localtrack(
1699 pc_factory_->CreateAudioTrack("dummy_track", nullptr));
1700 EXPECT_EQ(nullptr, pc_->CreateDtmfSender(non_localtrack));
1701}
1702
deadbeefbd7d8f72015-12-18 16:58:44 -08001703// Test creating a sender with a stream ID, and ensure the ID is populated
1704// in the offer.
1705TEST_F(PeerConnectionInterfaceTest, CreateSenderWithStream) {
deadbeef293e9262017-01-11 12:28:30 -08001706 CreatePeerConnectionWithoutDtls();
deadbeefbd7d8f72015-12-18 16:58:44 -08001707 pc_->CreateSender("video", kStreamLabel1);
1708
kwibergd1fe2812016-04-27 06:47:29 -07001709 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001710 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefbd7d8f72015-12-18 16:58:44 -08001711
1712 const cricket::MediaContentDescription* video_desc =
1713 cricket::GetFirstVideoContentDescription(offer->description());
1714 ASSERT_TRUE(video_desc != nullptr);
1715 ASSERT_EQ(1u, video_desc->streams().size());
1716 EXPECT_EQ(kStreamLabel1, video_desc->streams()[0].sync_label);
1717}
1718
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001719// Test that we can specify a certain track that we want statistics about.
1720TEST_F(PeerConnectionInterfaceTest, GetStatsForSpecificTrack) {
1721 InitiateCall();
1722 ASSERT_LT(0u, pc_->remote_streams()->count());
1723 ASSERT_LT(0u, pc_->remote_streams()->at(0)->GetAudioTracks().size());
zhihuang9763d562016-08-05 11:14:50 -07001724 rtc::scoped_refptr<MediaStreamTrackInterface> remote_audio =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001725 pc_->remote_streams()->at(0)->GetAudioTracks()[0];
1726 EXPECT_TRUE(DoGetStats(remote_audio));
1727
1728 // Remove the stream. Since we are sending to our selves the local
1729 // and the remote stream is the same.
1730 pc_->RemoveStream(pc_->local_streams()->at(0));
1731 // Do a re-negotiation.
1732 CreateOfferReceiveAnswer();
1733
1734 ASSERT_EQ(0u, pc_->remote_streams()->count());
1735
1736 // Test that we still can get statistics for the old track. Even if it is not
1737 // sent any longer.
1738 EXPECT_TRUE(DoGetStats(remote_audio));
1739}
1740
1741// Test that we can get stats on a video track.
1742TEST_F(PeerConnectionInterfaceTest, GetStatsForVideoTrack) {
1743 InitiateCall();
1744 ASSERT_LT(0u, pc_->remote_streams()->count());
1745 ASSERT_LT(0u, pc_->remote_streams()->at(0)->GetVideoTracks().size());
zhihuang9763d562016-08-05 11:14:50 -07001746 rtc::scoped_refptr<MediaStreamTrackInterface> remote_video =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001747 pc_->remote_streams()->at(0)->GetVideoTracks()[0];
1748 EXPECT_TRUE(DoGetStats(remote_video));
1749}
1750
1751// Test that we don't get statistics for an invalid track.
zhihuange9e94c32016-11-04 11:38:15 -07001752TEST_F(PeerConnectionInterfaceTest, GetStatsForInvalidTrack) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001753 InitiateCall();
zhihuang9763d562016-08-05 11:14:50 -07001754 rtc::scoped_refptr<AudioTrackInterface> unknown_audio_track(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001755 pc_factory_->CreateAudioTrack("unknown track", NULL));
1756 EXPECT_FALSE(DoGetStats(unknown_audio_track));
1757}
1758
1759// This test setup two RTP data channels in loop back.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001760TEST_F(PeerConnectionInterfaceTest, TestDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001761 FakeConstraints constraints;
1762 constraints.SetAllowRtpDataChannels();
1763 CreatePeerConnection(&constraints);
zhihuang9763d562016-08-05 11:14:50 -07001764 rtc::scoped_refptr<DataChannelInterface> data1 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001765 pc_->CreateDataChannel("test1", NULL);
zhihuang9763d562016-08-05 11:14:50 -07001766 rtc::scoped_refptr<DataChannelInterface> data2 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001767 pc_->CreateDataChannel("test2", NULL);
1768 ASSERT_TRUE(data1 != NULL);
kwibergd1fe2812016-04-27 06:47:29 -07001769 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001770 new MockDataChannelObserver(data1));
kwibergd1fe2812016-04-27 06:47:29 -07001771 std::unique_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001772 new MockDataChannelObserver(data2));
1773
1774 EXPECT_EQ(DataChannelInterface::kConnecting, data1->state());
1775 EXPECT_EQ(DataChannelInterface::kConnecting, data2->state());
1776 std::string data_to_send1 = "testing testing";
1777 std::string data_to_send2 = "testing something else";
1778 EXPECT_FALSE(data1->Send(DataBuffer(data_to_send1)));
1779
1780 CreateOfferReceiveAnswer();
1781 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1782 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
1783
1784 EXPECT_EQ(DataChannelInterface::kOpen, data1->state());
1785 EXPECT_EQ(DataChannelInterface::kOpen, data2->state());
1786 EXPECT_TRUE(data1->Send(DataBuffer(data_to_send1)));
1787 EXPECT_TRUE(data2->Send(DataBuffer(data_to_send2)));
1788
1789 EXPECT_EQ_WAIT(data_to_send1, observer1->last_message(), kTimeout);
1790 EXPECT_EQ_WAIT(data_to_send2, observer2->last_message(), kTimeout);
1791
1792 data1->Close();
1793 EXPECT_EQ(DataChannelInterface::kClosing, data1->state());
1794 CreateOfferReceiveAnswer();
1795 EXPECT_FALSE(observer1->IsOpen());
1796 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
1797 EXPECT_TRUE(observer2->IsOpen());
1798
1799 data_to_send2 = "testing something else again";
1800 EXPECT_TRUE(data2->Send(DataBuffer(data_to_send2)));
1801
1802 EXPECT_EQ_WAIT(data_to_send2, observer2->last_message(), kTimeout);
1803}
1804
1805// This test verifies that sendnig binary data over RTP data channels should
1806// fail.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001807TEST_F(PeerConnectionInterfaceTest, TestSendBinaryOnRtpDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001808 FakeConstraints constraints;
1809 constraints.SetAllowRtpDataChannels();
1810 CreatePeerConnection(&constraints);
zhihuang9763d562016-08-05 11:14:50 -07001811 rtc::scoped_refptr<DataChannelInterface> data1 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001812 pc_->CreateDataChannel("test1", NULL);
zhihuang9763d562016-08-05 11:14:50 -07001813 rtc::scoped_refptr<DataChannelInterface> data2 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001814 pc_->CreateDataChannel("test2", NULL);
1815 ASSERT_TRUE(data1 != NULL);
kwibergd1fe2812016-04-27 06:47:29 -07001816 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001817 new MockDataChannelObserver(data1));
kwibergd1fe2812016-04-27 06:47:29 -07001818 std::unique_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001819 new MockDataChannelObserver(data2));
1820
1821 EXPECT_EQ(DataChannelInterface::kConnecting, data1->state());
1822 EXPECT_EQ(DataChannelInterface::kConnecting, data2->state());
1823
1824 CreateOfferReceiveAnswer();
1825 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1826 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
1827
1828 EXPECT_EQ(DataChannelInterface::kOpen, data1->state());
1829 EXPECT_EQ(DataChannelInterface::kOpen, data2->state());
1830
jbaucheec21bd2016-03-20 06:15:43 -07001831 rtc::CopyOnWriteBuffer buffer("test", 4);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001832 EXPECT_FALSE(data1->Send(DataBuffer(buffer, true)));
1833}
1834
1835// This test setup a RTP data channels in loop back and test that a channel is
1836// opened even if the remote end answer with a zero SSRC.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001837TEST_F(PeerConnectionInterfaceTest, TestSendOnlyDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001838 FakeConstraints constraints;
1839 constraints.SetAllowRtpDataChannels();
1840 CreatePeerConnection(&constraints);
zhihuang9763d562016-08-05 11:14:50 -07001841 rtc::scoped_refptr<DataChannelInterface> data1 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001842 pc_->CreateDataChannel("test1", NULL);
kwibergd1fe2812016-04-27 06:47:29 -07001843 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001844 new MockDataChannelObserver(data1));
1845
1846 CreateOfferReceiveAnswerWithoutSsrc();
1847
1848 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1849
1850 data1->Close();
1851 EXPECT_EQ(DataChannelInterface::kClosing, data1->state());
1852 CreateOfferReceiveAnswerWithoutSsrc();
1853 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
1854 EXPECT_FALSE(observer1->IsOpen());
1855}
1856
1857// This test that if a data channel is added in an answer a receive only channel
1858// channel is created.
1859TEST_F(PeerConnectionInterfaceTest, TestReceiveOnlyDataChannel) {
1860 FakeConstraints constraints;
1861 constraints.SetAllowRtpDataChannels();
1862 CreatePeerConnection(&constraints);
1863
1864 std::string offer_label = "offer_channel";
zhihuang9763d562016-08-05 11:14:50 -07001865 rtc::scoped_refptr<DataChannelInterface> offer_channel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001866 pc_->CreateDataChannel(offer_label, NULL);
1867
1868 CreateOfferAsLocalDescription();
1869
1870 // Replace the data channel label in the offer and apply it as an answer.
1871 std::string receive_label = "answer_channel";
1872 std::string sdp;
1873 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001874 rtc::replace_substrs(offer_label.c_str(), offer_label.length(),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001875 receive_label.c_str(), receive_label.length(),
1876 &sdp);
1877 CreateAnswerAsRemoteDescription(sdp);
1878
1879 // Verify that a new incoming data channel has been created and that
1880 // it is open but can't we written to.
1881 ASSERT_TRUE(observer_.last_datachannel_ != NULL);
1882 DataChannelInterface* received_channel = observer_.last_datachannel_;
1883 EXPECT_EQ(DataChannelInterface::kConnecting, received_channel->state());
1884 EXPECT_EQ(receive_label, received_channel->label());
1885 EXPECT_FALSE(received_channel->Send(DataBuffer("something")));
1886
1887 // Verify that the channel we initially offered has been rejected.
1888 EXPECT_EQ(DataChannelInterface::kClosed, offer_channel->state());
1889
1890 // Do another offer / answer exchange and verify that the data channel is
1891 // opened.
1892 CreateOfferReceiveAnswer();
1893 EXPECT_EQ_WAIT(DataChannelInterface::kOpen, received_channel->state(),
1894 kTimeout);
1895}
1896
1897// This test that no data channel is returned if a reliable channel is
1898// requested.
1899// TODO(perkj): Remove this test once reliable channels are implemented.
1900TEST_F(PeerConnectionInterfaceTest, CreateReliableRtpDataChannelShouldFail) {
1901 FakeConstraints constraints;
1902 constraints.SetAllowRtpDataChannels();
1903 CreatePeerConnection(&constraints);
1904
1905 std::string label = "test";
1906 webrtc::DataChannelInit config;
1907 config.reliable = true;
zhihuang9763d562016-08-05 11:14:50 -07001908 rtc::scoped_refptr<DataChannelInterface> channel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001909 pc_->CreateDataChannel(label, &config);
1910 EXPECT_TRUE(channel == NULL);
1911}
1912
deadbeefab9b2d12015-10-14 11:33:11 -07001913// Verifies that duplicated label is not allowed for RTP data channel.
1914TEST_F(PeerConnectionInterfaceTest, RtpDuplicatedLabelNotAllowed) {
1915 FakeConstraints constraints;
1916 constraints.SetAllowRtpDataChannels();
1917 CreatePeerConnection(&constraints);
1918
1919 std::string label = "test";
zhihuang9763d562016-08-05 11:14:50 -07001920 rtc::scoped_refptr<DataChannelInterface> channel =
deadbeefab9b2d12015-10-14 11:33:11 -07001921 pc_->CreateDataChannel(label, nullptr);
1922 EXPECT_NE(channel, nullptr);
1923
zhihuang9763d562016-08-05 11:14:50 -07001924 rtc::scoped_refptr<DataChannelInterface> dup_channel =
deadbeefab9b2d12015-10-14 11:33:11 -07001925 pc_->CreateDataChannel(label, nullptr);
1926 EXPECT_EQ(dup_channel, nullptr);
1927}
1928
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001929// This tests that a SCTP data channel is returned using different
1930// DataChannelInit configurations.
1931TEST_F(PeerConnectionInterfaceTest, CreateSctpDataChannel) {
1932 FakeConstraints constraints;
1933 constraints.SetAllowDtlsSctpDataChannels();
1934 CreatePeerConnection(&constraints);
1935
1936 webrtc::DataChannelInit config;
1937
zhihuang9763d562016-08-05 11:14:50 -07001938 rtc::scoped_refptr<DataChannelInterface> channel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001939 pc_->CreateDataChannel("1", &config);
1940 EXPECT_TRUE(channel != NULL);
1941 EXPECT_TRUE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001942 EXPECT_TRUE(observer_.renegotiation_needed_);
1943 observer_.renegotiation_needed_ = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001944
1945 config.ordered = false;
1946 channel = pc_->CreateDataChannel("2", &config);
1947 EXPECT_TRUE(channel != NULL);
1948 EXPECT_TRUE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001949 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001950
1951 config.ordered = true;
1952 config.maxRetransmits = 0;
1953 channel = pc_->CreateDataChannel("3", &config);
1954 EXPECT_TRUE(channel != NULL);
1955 EXPECT_FALSE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001956 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001957
1958 config.maxRetransmits = -1;
1959 config.maxRetransmitTime = 0;
1960 channel = pc_->CreateDataChannel("4", &config);
1961 EXPECT_TRUE(channel != NULL);
1962 EXPECT_FALSE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001963 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001964}
1965
1966// This tests that no data channel is returned if both maxRetransmits and
1967// maxRetransmitTime are set for SCTP data channels.
1968TEST_F(PeerConnectionInterfaceTest,
1969 CreateSctpDataChannelShouldFailForInvalidConfig) {
1970 FakeConstraints constraints;
1971 constraints.SetAllowDtlsSctpDataChannels();
1972 CreatePeerConnection(&constraints);
1973
1974 std::string label = "test";
1975 webrtc::DataChannelInit config;
1976 config.maxRetransmits = 0;
1977 config.maxRetransmitTime = 0;
1978
zhihuang9763d562016-08-05 11:14:50 -07001979 rtc::scoped_refptr<DataChannelInterface> channel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001980 pc_->CreateDataChannel(label, &config);
1981 EXPECT_TRUE(channel == NULL);
1982}
1983
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001984// The test verifies that creating a SCTP data channel with an id already in use
1985// or out of range should fail.
1986TEST_F(PeerConnectionInterfaceTest,
1987 CreateSctpDataChannelWithInvalidIdShouldFail) {
1988 FakeConstraints constraints;
1989 constraints.SetAllowDtlsSctpDataChannels();
1990 CreatePeerConnection(&constraints);
1991
1992 webrtc::DataChannelInit config;
zhihuang9763d562016-08-05 11:14:50 -07001993 rtc::scoped_refptr<DataChannelInterface> channel;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001994
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001995 config.id = 1;
1996 channel = pc_->CreateDataChannel("1", &config);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001997 EXPECT_TRUE(channel != NULL);
1998 EXPECT_EQ(1, channel->id());
1999
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002000 channel = pc_->CreateDataChannel("x", &config);
2001 EXPECT_TRUE(channel == NULL);
2002
2003 config.id = cricket::kMaxSctpSid;
2004 channel = pc_->CreateDataChannel("max", &config);
2005 EXPECT_TRUE(channel != NULL);
2006 EXPECT_EQ(config.id, channel->id());
2007
2008 config.id = cricket::kMaxSctpSid + 1;
2009 channel = pc_->CreateDataChannel("x", &config);
2010 EXPECT_TRUE(channel == NULL);
2011}
2012
deadbeefab9b2d12015-10-14 11:33:11 -07002013// Verifies that duplicated label is allowed for SCTP data channel.
2014TEST_F(PeerConnectionInterfaceTest, SctpDuplicatedLabelAllowed) {
2015 FakeConstraints constraints;
2016 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2017 true);
2018 CreatePeerConnection(&constraints);
2019
2020 std::string label = "test";
zhihuang9763d562016-08-05 11:14:50 -07002021 rtc::scoped_refptr<DataChannelInterface> channel =
deadbeefab9b2d12015-10-14 11:33:11 -07002022 pc_->CreateDataChannel(label, nullptr);
2023 EXPECT_NE(channel, nullptr);
2024
zhihuang9763d562016-08-05 11:14:50 -07002025 rtc::scoped_refptr<DataChannelInterface> dup_channel =
deadbeefab9b2d12015-10-14 11:33:11 -07002026 pc_->CreateDataChannel(label, nullptr);
2027 EXPECT_NE(dup_channel, nullptr);
2028}
2029
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00002030// This test verifies that OnRenegotiationNeeded is fired for every new RTP
2031// DataChannel.
2032TEST_F(PeerConnectionInterfaceTest, RenegotiationNeededForNewRtpDataChannel) {
2033 FakeConstraints constraints;
2034 constraints.SetAllowRtpDataChannels();
2035 CreatePeerConnection(&constraints);
2036
zhihuang9763d562016-08-05 11:14:50 -07002037 rtc::scoped_refptr<DataChannelInterface> dc1 =
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00002038 pc_->CreateDataChannel("test1", NULL);
2039 EXPECT_TRUE(observer_.renegotiation_needed_);
2040 observer_.renegotiation_needed_ = false;
2041
zhihuang9763d562016-08-05 11:14:50 -07002042 rtc::scoped_refptr<DataChannelInterface> dc2 =
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00002043 pc_->CreateDataChannel("test2", NULL);
2044 EXPECT_TRUE(observer_.renegotiation_needed_);
2045}
2046
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002047// This test that a data channel closes when a PeerConnection is deleted/closed.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002048TEST_F(PeerConnectionInterfaceTest, DataChannelCloseWhenPeerConnectionClose) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002049 FakeConstraints constraints;
2050 constraints.SetAllowRtpDataChannels();
2051 CreatePeerConnection(&constraints);
2052
zhihuang9763d562016-08-05 11:14:50 -07002053 rtc::scoped_refptr<DataChannelInterface> data1 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002054 pc_->CreateDataChannel("test1", NULL);
zhihuang9763d562016-08-05 11:14:50 -07002055 rtc::scoped_refptr<DataChannelInterface> data2 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002056 pc_->CreateDataChannel("test2", NULL);
2057 ASSERT_TRUE(data1 != NULL);
kwibergd1fe2812016-04-27 06:47:29 -07002058 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002059 new MockDataChannelObserver(data1));
kwibergd1fe2812016-04-27 06:47:29 -07002060 std::unique_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002061 new MockDataChannelObserver(data2));
2062
2063 CreateOfferReceiveAnswer();
2064 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
2065 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
2066
2067 ReleasePeerConnection();
2068 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
2069 EXPECT_EQ(DataChannelInterface::kClosed, data2->state());
2070}
2071
2072// This test that data channels can be rejected in an answer.
2073TEST_F(PeerConnectionInterfaceTest, TestRejectDataChannelInAnswer) {
2074 FakeConstraints constraints;
2075 constraints.SetAllowRtpDataChannels();
2076 CreatePeerConnection(&constraints);
2077
zhihuang9763d562016-08-05 11:14:50 -07002078 rtc::scoped_refptr<DataChannelInterface> offer_channel(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002079 pc_->CreateDataChannel("offer_channel", NULL));
2080
2081 CreateOfferAsLocalDescription();
2082
2083 // Create an answer where the m-line for data channels are rejected.
2084 std::string sdp;
2085 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
2086 webrtc::JsepSessionDescription* answer = new webrtc::JsepSessionDescription(
2087 SessionDescriptionInterface::kAnswer);
2088 EXPECT_TRUE(answer->Initialize(sdp, NULL));
2089 cricket::ContentInfo* data_info =
2090 answer->description()->GetContentByName("data");
2091 data_info->rejected = true;
2092
2093 DoSetRemoteDescription(answer);
2094 EXPECT_EQ(DataChannelInterface::kClosed, offer_channel->state());
2095}
2096
2097// Test that we can create a session description from an SDP string from
2098// FireFox, use it as a remote session description, generate an answer and use
2099// the answer as a local description.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002100TEST_F(PeerConnectionInterfaceTest, ReceiveFireFoxOffer) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002101 FakeConstraints constraints;
2102 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2103 true);
2104 CreatePeerConnection(&constraints);
2105 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
2106 SessionDescriptionInterface* desc =
2107 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
jbauchfabe2c92015-07-16 13:43:14 -07002108 webrtc::kFireFoxSdpOffer, nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002109 EXPECT_TRUE(DoSetSessionDescription(desc, false));
2110 CreateAnswerAsLocalDescription();
2111 ASSERT_TRUE(pc_->local_description() != NULL);
2112 ASSERT_TRUE(pc_->remote_description() != NULL);
2113
2114 const cricket::ContentInfo* content =
2115 cricket::GetFirstAudioContent(pc_->local_description()->description());
2116 ASSERT_TRUE(content != NULL);
2117 EXPECT_FALSE(content->rejected);
2118
2119 content =
2120 cricket::GetFirstVideoContent(pc_->local_description()->description());
2121 ASSERT_TRUE(content != NULL);
2122 EXPECT_FALSE(content->rejected);
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00002123#ifdef HAVE_SCTP
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002124 content =
2125 cricket::GetFirstDataContent(pc_->local_description()->description());
2126 ASSERT_TRUE(content != NULL);
2127 EXPECT_TRUE(content->rejected);
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00002128#endif
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002129}
2130
deadbeef8662f942017-01-20 21:20:51 -08002131// Test that an offer can be received which offers DTLS with SDES fallback.
2132// Regression test for issue:
2133// https://bugs.chromium.org/p/webrtc/issues/detail?id=6972
2134TEST_F(PeerConnectionInterfaceTest, ReceiveDtlsSdesFallbackOffer) {
2135 FakeConstraints constraints;
2136 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2137 true);
2138 CreatePeerConnection(&constraints);
2139 // Wait for fake certificate to be generated. Previously, this is what caused
2140 // the "a=crypto" lines to be rejected.
2141 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
2142 ASSERT_NE(nullptr, fake_certificate_generator_);
2143 EXPECT_EQ_WAIT(1, fake_certificate_generator_->generated_certificates(),
2144 kTimeout);
2145 SessionDescriptionInterface* desc = webrtc::CreateSessionDescription(
2146 SessionDescriptionInterface::kOffer, kDtlsSdesFallbackSdp, nullptr);
2147 EXPECT_TRUE(DoSetSessionDescription(desc, false));
2148 CreateAnswerAsLocalDescription();
2149}
2150
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002151// Test that we can create an audio only offer and receive an answer with a
2152// limited set of audio codecs and receive an updated offer with more audio
2153// codecs, where the added codecs are not supported.
2154TEST_F(PeerConnectionInterfaceTest, ReceiveUpdatedAudioOfferWithBadCodecs) {
deadbeef293e9262017-01-11 12:28:30 -08002155 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002156 AddVoiceStream("audio_label");
2157 CreateOfferAsLocalDescription();
2158
2159 SessionDescriptionInterface* answer =
2160 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
jbauchfabe2c92015-07-16 13:43:14 -07002161 webrtc::kAudioSdp, nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002162 EXPECT_TRUE(DoSetSessionDescription(answer, false));
2163
2164 SessionDescriptionInterface* updated_offer =
2165 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
jbauchfabe2c92015-07-16 13:43:14 -07002166 webrtc::kAudioSdpWithUnsupportedCodecs,
2167 nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002168 EXPECT_TRUE(DoSetSessionDescription(updated_offer, false));
2169 CreateAnswerAsLocalDescription();
2170}
2171
deadbeefc80741f2015-10-22 13:14:45 -07002172// Test that if we're receiving (but not sending) a track, subsequent offers
2173// will have m-lines with a=recvonly.
2174TEST_F(PeerConnectionInterfaceTest, CreateSubsequentRecvOnlyOffer) {
2175 FakeConstraints constraints;
2176 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2177 true);
2178 CreatePeerConnection(&constraints);
2179 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2180 CreateAnswerAsLocalDescription();
2181
2182 // At this point we should be receiving stream 1, but not sending anything.
2183 // A new offer should be recvonly.
kwibergd1fe2812016-04-27 06:47:29 -07002184 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefc80741f2015-10-22 13:14:45 -07002185 DoCreateOffer(&offer, nullptr);
2186
2187 const cricket::ContentInfo* video_content =
2188 cricket::GetFirstVideoContent(offer->description());
2189 const cricket::VideoContentDescription* video_desc =
2190 static_cast<const cricket::VideoContentDescription*>(
2191 video_content->description);
2192 ASSERT_EQ(cricket::MD_RECVONLY, video_desc->direction());
2193
2194 const cricket::ContentInfo* audio_content =
2195 cricket::GetFirstAudioContent(offer->description());
2196 const cricket::AudioContentDescription* audio_desc =
2197 static_cast<const cricket::AudioContentDescription*>(
2198 audio_content->description);
2199 ASSERT_EQ(cricket::MD_RECVONLY, audio_desc->direction());
2200}
2201
2202// Test that if we're receiving (but not sending) a track, and the
2203// offerToReceiveVideo/offerToReceiveAudio constraints are explicitly set to
2204// false, the generated m-lines will be a=inactive.
2205TEST_F(PeerConnectionInterfaceTest, CreateSubsequentInactiveOffer) {
2206 FakeConstraints constraints;
2207 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2208 true);
2209 CreatePeerConnection(&constraints);
2210 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2211 CreateAnswerAsLocalDescription();
2212
2213 // At this point we should be receiving stream 1, but not sending anything.
2214 // A new offer would be recvonly, but we'll set the "no receive" constraints
2215 // to make it inactive.
kwibergd1fe2812016-04-27 06:47:29 -07002216 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefc80741f2015-10-22 13:14:45 -07002217 FakeConstraints offer_constraints;
2218 offer_constraints.AddMandatory(
2219 webrtc::MediaConstraintsInterface::kOfferToReceiveVideo, false);
2220 offer_constraints.AddMandatory(
2221 webrtc::MediaConstraintsInterface::kOfferToReceiveAudio, false);
2222 DoCreateOffer(&offer, &offer_constraints);
2223
2224 const cricket::ContentInfo* video_content =
2225 cricket::GetFirstVideoContent(offer->description());
2226 const cricket::VideoContentDescription* video_desc =
2227 static_cast<const cricket::VideoContentDescription*>(
2228 video_content->description);
2229 ASSERT_EQ(cricket::MD_INACTIVE, video_desc->direction());
2230
2231 const cricket::ContentInfo* audio_content =
2232 cricket::GetFirstAudioContent(offer->description());
2233 const cricket::AudioContentDescription* audio_desc =
2234 static_cast<const cricket::AudioContentDescription*>(
2235 audio_content->description);
2236 ASSERT_EQ(cricket::MD_INACTIVE, audio_desc->direction());
2237}
2238
deadbeef653b8e02015-11-11 12:55:10 -08002239// Test that we can use SetConfiguration to change the ICE servers of the
2240// PortAllocator.
2241TEST_F(PeerConnectionInterfaceTest, SetConfigurationChangesIceServers) {
2242 CreatePeerConnection();
2243
2244 PeerConnectionInterface::RTCConfiguration config;
2245 PeerConnectionInterface::IceServer server;
2246 server.uri = "stun:test_hostname";
2247 config.servers.push_back(server);
2248 EXPECT_TRUE(pc_->SetConfiguration(config));
2249
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002250 EXPECT_EQ(1u, port_allocator_->stun_servers().size());
2251 EXPECT_EQ("test_hostname",
2252 port_allocator_->stun_servers().begin()->hostname());
deadbeef653b8e02015-11-11 12:55:10 -08002253}
2254
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002255TEST_F(PeerConnectionInterfaceTest, SetConfigurationChangesCandidateFilter) {
2256 CreatePeerConnection();
2257 PeerConnectionInterface::RTCConfiguration config;
2258 config.type = PeerConnectionInterface::kRelay;
2259 EXPECT_TRUE(pc_->SetConfiguration(config));
2260 EXPECT_EQ(cricket::CF_RELAY, port_allocator_->candidate_filter());
2261}
2262
deadbeef293e9262017-01-11 12:28:30 -08002263TEST_F(PeerConnectionInterfaceTest, SetConfigurationChangesPruneTurnPortsFlag) {
2264 PeerConnectionInterface::RTCConfiguration config;
2265 config.prune_turn_ports = false;
2266 CreatePeerConnection(config, nullptr);
2267 EXPECT_FALSE(port_allocator_->prune_turn_ports());
2268
2269 config.prune_turn_ports = true;
2270 EXPECT_TRUE(pc_->SetConfiguration(config));
2271 EXPECT_TRUE(port_allocator_->prune_turn_ports());
2272}
2273
skvladd1f5fda2017-02-03 16:54:05 -08002274// Test that the ice check interval can be changed. This does not verify that
2275// the setting makes it all the way to P2PTransportChannel, as that would
2276// require a very complex set of mocks.
2277TEST_F(PeerConnectionInterfaceTest, SetConfigurationChangesIceCheckInterval) {
2278 PeerConnectionInterface::RTCConfiguration config;
2279 config.ice_check_min_interval = rtc::Optional<int>();
2280 CreatePeerConnection(config, nullptr);
2281 config.ice_check_min_interval = rtc::Optional<int>(100);
2282 EXPECT_TRUE(pc_->SetConfiguration(config));
2283 PeerConnectionInterface::RTCConfiguration new_config =
2284 pc_->GetConfiguration();
2285 EXPECT_EQ(new_config.ice_check_min_interval, rtc::Optional<int>(100));
2286}
2287
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002288// Test that when SetConfiguration changes both the pool size and other
2289// attributes, the pooled session is created with the updated attributes.
2290TEST_F(PeerConnectionInterfaceTest,
2291 SetConfigurationCreatesPooledSessionCorrectly) {
2292 CreatePeerConnection();
2293 PeerConnectionInterface::RTCConfiguration config;
2294 config.ice_candidate_pool_size = 1;
2295 PeerConnectionInterface::IceServer server;
2296 server.uri = kStunAddressOnly;
2297 config.servers.push_back(server);
2298 config.type = PeerConnectionInterface::kRelay;
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07002299 EXPECT_TRUE(pc_->SetConfiguration(config));
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002300
2301 const cricket::FakePortAllocatorSession* session =
2302 static_cast<const cricket::FakePortAllocatorSession*>(
2303 port_allocator_->GetPooledSession());
2304 ASSERT_NE(nullptr, session);
2305 EXPECT_EQ(1UL, session->stun_servers().size());
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002306}
2307
deadbeef293e9262017-01-11 12:28:30 -08002308// Test that after SetLocalDescription, changing the pool size is not allowed,
2309// and an invalid modification error is returned.
deadbeef6de92f92016-12-12 18:49:32 -08002310TEST_F(PeerConnectionInterfaceTest,
2311 CantChangePoolSizeAfterSetLocalDescription) {
2312 CreatePeerConnection();
2313 // Start by setting a size of 1.
2314 PeerConnectionInterface::RTCConfiguration config;
2315 config.ice_candidate_pool_size = 1;
2316 EXPECT_TRUE(pc_->SetConfiguration(config));
2317
2318 // Set remote offer; can still change pool size at this point.
2319 CreateOfferAsRemoteDescription();
2320 config.ice_candidate_pool_size = 2;
2321 EXPECT_TRUE(pc_->SetConfiguration(config));
2322
2323 // Set local answer; now it's too late.
2324 CreateAnswerAsLocalDescription();
2325 config.ice_candidate_pool_size = 3;
deadbeef293e9262017-01-11 12:28:30 -08002326 RTCError error;
2327 EXPECT_FALSE(pc_->SetConfiguration(config, &error));
2328 EXPECT_EQ(RTCErrorType::INVALID_MODIFICATION, error.type());
2329}
2330
deadbeef42a42632017-03-10 15:18:00 -08002331// Test that after setting an answer, extra pooled sessions are discarded. The
2332// ICE candidate pool is only intended to be used for the first offer/answer.
2333TEST_F(PeerConnectionInterfaceTest,
2334 ExtraPooledSessionsDiscardedAfterApplyingAnswer) {
2335 CreatePeerConnection();
2336
2337 // Set a larger-than-necessary size.
2338 PeerConnectionInterface::RTCConfiguration config;
2339 config.ice_candidate_pool_size = 4;
2340 EXPECT_TRUE(pc_->SetConfiguration(config));
2341
2342 // Do offer/answer.
2343 CreateOfferAsRemoteDescription();
2344 CreateAnswerAsLocalDescription();
2345
2346 // Expect no pooled sessions to be left.
2347 const cricket::PortAllocatorSession* session =
2348 port_allocator_->GetPooledSession();
2349 EXPECT_EQ(nullptr, session);
2350}
2351
2352// After Close is called, pooled candidates should be discarded so as to not
2353// waste network resources.
2354TEST_F(PeerConnectionInterfaceTest, PooledSessionsDiscardedAfterClose) {
2355 CreatePeerConnection();
2356
2357 PeerConnectionInterface::RTCConfiguration config;
2358 config.ice_candidate_pool_size = 3;
2359 EXPECT_TRUE(pc_->SetConfiguration(config));
2360 pc_->Close();
2361
2362 // Expect no pooled sessions to be left.
2363 const cricket::PortAllocatorSession* session =
2364 port_allocator_->GetPooledSession();
2365 EXPECT_EQ(nullptr, session);
2366}
2367
deadbeef293e9262017-01-11 12:28:30 -08002368// Test that SetConfiguration returns an invalid modification error if
2369// modifying a field in the configuration that isn't allowed to be modified.
2370TEST_F(PeerConnectionInterfaceTest,
2371 SetConfigurationReturnsInvalidModificationError) {
2372 PeerConnectionInterface::RTCConfiguration config;
2373 config.bundle_policy = PeerConnectionInterface::kBundlePolicyBalanced;
2374 config.rtcp_mux_policy = PeerConnectionInterface::kRtcpMuxPolicyNegotiate;
2375 config.continual_gathering_policy = PeerConnectionInterface::GATHER_ONCE;
2376 CreatePeerConnection(config, nullptr);
2377
2378 PeerConnectionInterface::RTCConfiguration modified_config = config;
2379 modified_config.bundle_policy =
2380 PeerConnectionInterface::kBundlePolicyMaxBundle;
2381 RTCError error;
2382 EXPECT_FALSE(pc_->SetConfiguration(modified_config, &error));
2383 EXPECT_EQ(RTCErrorType::INVALID_MODIFICATION, error.type());
2384
2385 modified_config = config;
2386 modified_config.rtcp_mux_policy =
2387 PeerConnectionInterface::kRtcpMuxPolicyRequire;
2388 error.set_type(RTCErrorType::NONE);
2389 EXPECT_FALSE(pc_->SetConfiguration(modified_config, &error));
2390 EXPECT_EQ(RTCErrorType::INVALID_MODIFICATION, error.type());
2391
2392 modified_config = config;
2393 modified_config.continual_gathering_policy =
2394 PeerConnectionInterface::GATHER_CONTINUALLY;
2395 error.set_type(RTCErrorType::NONE);
2396 EXPECT_FALSE(pc_->SetConfiguration(modified_config, &error));
2397 EXPECT_EQ(RTCErrorType::INVALID_MODIFICATION, error.type());
2398}
2399
2400// Test that SetConfiguration returns a range error if the candidate pool size
2401// is negative or larger than allowed by the spec.
2402TEST_F(PeerConnectionInterfaceTest,
2403 SetConfigurationReturnsRangeErrorForBadCandidatePoolSize) {
2404 PeerConnectionInterface::RTCConfiguration config;
2405 CreatePeerConnection(config, nullptr);
2406
2407 config.ice_candidate_pool_size = -1;
2408 RTCError error;
2409 EXPECT_FALSE(pc_->SetConfiguration(config, &error));
2410 EXPECT_EQ(RTCErrorType::INVALID_RANGE, error.type());
2411
2412 config.ice_candidate_pool_size = INT_MAX;
2413 error.set_type(RTCErrorType::NONE);
2414 EXPECT_FALSE(pc_->SetConfiguration(config, &error));
2415 EXPECT_EQ(RTCErrorType::INVALID_RANGE, error.type());
2416}
2417
2418// Test that SetConfiguration returns a syntax error if parsing an ICE server
2419// URL failed.
2420TEST_F(PeerConnectionInterfaceTest,
2421 SetConfigurationReturnsSyntaxErrorFromBadIceUrls) {
2422 PeerConnectionInterface::RTCConfiguration config;
2423 CreatePeerConnection(config, nullptr);
2424
2425 PeerConnectionInterface::IceServer bad_server;
2426 bad_server.uri = "stunn:www.example.com";
2427 config.servers.push_back(bad_server);
2428 RTCError error;
2429 EXPECT_FALSE(pc_->SetConfiguration(config, &error));
2430 EXPECT_EQ(RTCErrorType::SYNTAX_ERROR, error.type());
2431}
2432
2433// Test that SetConfiguration returns an invalid parameter error if a TURN
2434// IceServer is missing a username or password.
2435TEST_F(PeerConnectionInterfaceTest,
2436 SetConfigurationReturnsInvalidParameterIfCredentialsMissing) {
2437 PeerConnectionInterface::RTCConfiguration config;
2438 CreatePeerConnection(config, nullptr);
2439
2440 PeerConnectionInterface::IceServer bad_server;
2441 bad_server.uri = "turn:www.example.com";
2442 // Missing password.
2443 bad_server.username = "foo";
2444 config.servers.push_back(bad_server);
2445 RTCError error;
2446 EXPECT_FALSE(pc_->SetConfiguration(config, &error));
2447 EXPECT_EQ(RTCErrorType::INVALID_PARAMETER, error.type());
deadbeef6de92f92016-12-12 18:49:32 -08002448}
2449
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002450// Test that PeerConnection::Close changes the states to closed and all remote
2451// tracks change state to ended.
2452TEST_F(PeerConnectionInterfaceTest, CloseAndTestStreamsAndStates) {
2453 // Initialize a PeerConnection and negotiate local and remote session
2454 // description.
2455 InitiateCall();
2456 ASSERT_EQ(1u, pc_->local_streams()->count());
2457 ASSERT_EQ(1u, pc_->remote_streams()->count());
2458
2459 pc_->Close();
2460
2461 EXPECT_EQ(PeerConnectionInterface::kClosed, pc_->signaling_state());
2462 EXPECT_EQ(PeerConnectionInterface::kIceConnectionClosed,
2463 pc_->ice_connection_state());
2464 EXPECT_EQ(PeerConnectionInterface::kIceGatheringComplete,
2465 pc_->ice_gathering_state());
2466
2467 EXPECT_EQ(1u, pc_->local_streams()->count());
2468 EXPECT_EQ(1u, pc_->remote_streams()->count());
2469
zhihuang9763d562016-08-05 11:14:50 -07002470 rtc::scoped_refptr<MediaStreamInterface> remote_stream =
2471 pc_->remote_streams()->at(0);
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002472 // Track state may be updated asynchronously.
perkjd61bf802016-03-24 03:16:19 -07002473 EXPECT_EQ_WAIT(MediaStreamTrackInterface::kEnded,
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002474 remote_stream->GetAudioTracks()[0]->state(), kTimeout);
2475 EXPECT_EQ_WAIT(MediaStreamTrackInterface::kEnded,
2476 remote_stream->GetVideoTracks()[0]->state(), kTimeout);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002477}
2478
2479// Test that PeerConnection methods fails gracefully after
2480// PeerConnection::Close has been called.
2481TEST_F(PeerConnectionInterfaceTest, CloseAndTestMethods) {
deadbeef293e9262017-01-11 12:28:30 -08002482 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002483 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
2484 CreateOfferAsRemoteDescription();
2485 CreateAnswerAsLocalDescription();
2486
2487 ASSERT_EQ(1u, pc_->local_streams()->count());
zhihuang9763d562016-08-05 11:14:50 -07002488 rtc::scoped_refptr<MediaStreamInterface> local_stream =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002489 pc_->local_streams()->at(0);
2490
2491 pc_->Close();
2492
2493 pc_->RemoveStream(local_stream);
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +00002494 EXPECT_FALSE(pc_->AddStream(local_stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002495
2496 ASSERT_FALSE(local_stream->GetAudioTracks().empty());
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002497 rtc::scoped_refptr<webrtc::DtmfSenderInterface> dtmf_sender(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002498 pc_->CreateDtmfSender(local_stream->GetAudioTracks()[0]));
wu@webrtc.org66037362013-08-13 00:09:35 +00002499 EXPECT_TRUE(NULL == dtmf_sender); // local stream has been removed.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002500
2501 EXPECT_TRUE(pc_->CreateDataChannel("test", NULL) == NULL);
2502
2503 EXPECT_TRUE(pc_->local_description() != NULL);
2504 EXPECT_TRUE(pc_->remote_description() != NULL);
2505
kwibergd1fe2812016-04-27 06:47:29 -07002506 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07002507 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
kwibergd1fe2812016-04-27 06:47:29 -07002508 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -07002509 EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002510
2511 std::string sdp;
2512 ASSERT_TRUE(pc_->remote_description()->ToString(&sdp));
2513 SessionDescriptionInterface* remote_offer =
2514 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
2515 sdp, NULL);
2516 EXPECT_FALSE(DoSetRemoteDescription(remote_offer));
2517
2518 ASSERT_TRUE(pc_->local_description()->ToString(&sdp));
2519 SessionDescriptionInterface* local_offer =
2520 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
2521 sdp, NULL);
2522 EXPECT_FALSE(DoSetLocalDescription(local_offer));
2523}
2524
2525// Test that GetStats can still be called after PeerConnection::Close.
2526TEST_F(PeerConnectionInterfaceTest, CloseAndGetStats) {
2527 InitiateCall();
2528 pc_->Close();
2529 DoGetStats(NULL);
2530}
deadbeefab9b2d12015-10-14 11:33:11 -07002531
2532// NOTE: The series of tests below come from what used to be
2533// mediastreamsignaling_unittest.cc, and are mostly aimed at testing that
2534// setting a remote or local description has the expected effects.
2535
2536// This test verifies that the remote MediaStreams corresponding to a received
2537// SDP string is created. In this test the two separate MediaStreams are
2538// signaled.
2539TEST_F(PeerConnectionInterfaceTest, UpdateRemoteStreams) {
2540 FakeConstraints constraints;
2541 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2542 true);
2543 CreatePeerConnection(&constraints);
2544 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2545
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002546 rtc::scoped_refptr<StreamCollection> reference(CreateStreamCollection(1, 1));
deadbeefab9b2d12015-10-14 11:33:11 -07002547 EXPECT_TRUE(
2548 CompareStreamCollections(observer_.remote_streams(), reference.get()));
2549 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2550 EXPECT_TRUE(remote_stream->GetVideoTracks()[0]->GetSource() != nullptr);
2551
2552 // Create a session description based on another SDP with another
2553 // MediaStream.
2554 CreateAndSetRemoteOffer(kSdpStringWithStream1And2);
2555
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002556 rtc::scoped_refptr<StreamCollection> reference2(CreateStreamCollection(2, 1));
deadbeefab9b2d12015-10-14 11:33:11 -07002557 EXPECT_TRUE(
2558 CompareStreamCollections(observer_.remote_streams(), reference2.get()));
2559}
2560
2561// This test verifies that when remote tracks are added/removed from SDP, the
2562// created remote streams are updated appropriately.
2563TEST_F(PeerConnectionInterfaceTest,
2564 AddRemoveTrackFromExistingRemoteMediaStream) {
2565 FakeConstraints constraints;
2566 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2567 true);
2568 CreatePeerConnection(&constraints);
kwibergd1fe2812016-04-27 06:47:29 -07002569 std::unique_ptr<SessionDescriptionInterface> desc_ms1 =
kwiberg2bbff992016-03-16 11:03:04 -07002570 CreateSessionDescriptionAndReference(1, 1);
deadbeefab9b2d12015-10-14 11:33:11 -07002571 EXPECT_TRUE(DoSetRemoteDescription(desc_ms1.release()));
2572 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
2573 reference_collection_));
2574
2575 // Add extra audio and video tracks to the same MediaStream.
kwibergd1fe2812016-04-27 06:47:29 -07002576 std::unique_ptr<SessionDescriptionInterface> desc_ms1_two_tracks =
kwiberg2bbff992016-03-16 11:03:04 -07002577 CreateSessionDescriptionAndReference(2, 2);
deadbeefab9b2d12015-10-14 11:33:11 -07002578 EXPECT_TRUE(DoSetRemoteDescription(desc_ms1_two_tracks.release()));
2579 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
2580 reference_collection_));
zhihuang9763d562016-08-05 11:14:50 -07002581 rtc::scoped_refptr<AudioTrackInterface> audio_track2 =
perkjd61bf802016-03-24 03:16:19 -07002582 observer_.remote_streams()->at(0)->GetAudioTracks()[1];
2583 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, audio_track2->state());
zhihuang9763d562016-08-05 11:14:50 -07002584 rtc::scoped_refptr<VideoTrackInterface> video_track2 =
perkjd61bf802016-03-24 03:16:19 -07002585 observer_.remote_streams()->at(0)->GetVideoTracks()[1];
2586 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, video_track2->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002587
2588 // Remove the extra audio and video tracks.
kwibergd1fe2812016-04-27 06:47:29 -07002589 std::unique_ptr<SessionDescriptionInterface> desc_ms2 =
kwiberg2bbff992016-03-16 11:03:04 -07002590 CreateSessionDescriptionAndReference(1, 1);
perkjd61bf802016-03-24 03:16:19 -07002591 MockTrackObserver audio_track_observer(audio_track2);
2592 MockTrackObserver video_track_observer(video_track2);
2593
2594 EXPECT_CALL(audio_track_observer, OnChanged()).Times(Exactly(1));
2595 EXPECT_CALL(video_track_observer, OnChanged()).Times(Exactly(1));
deadbeefab9b2d12015-10-14 11:33:11 -07002596 EXPECT_TRUE(DoSetRemoteDescription(desc_ms2.release()));
2597 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
2598 reference_collection_));
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002599 // Track state may be updated asynchronously.
perkjd61bf802016-03-24 03:16:19 -07002600 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002601 audio_track2->state(), kTimeout);
2602 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
2603 video_track2->state(), kTimeout);
deadbeefab9b2d12015-10-14 11:33:11 -07002604}
2605
2606// This tests that remote tracks are ended if a local session description is set
2607// that rejects the media content type.
2608TEST_F(PeerConnectionInterfaceTest, RejectMediaContent) {
2609 FakeConstraints constraints;
2610 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2611 true);
2612 CreatePeerConnection(&constraints);
2613 // First create and set a remote offer, then reject its video content in our
2614 // answer.
2615 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2616 ASSERT_EQ(1u, observer_.remote_streams()->count());
2617 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2618 ASSERT_EQ(1u, remote_stream->GetVideoTracks().size());
2619 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2620
2621 rtc::scoped_refptr<webrtc::VideoTrackInterface> remote_video =
2622 remote_stream->GetVideoTracks()[0];
2623 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, remote_video->state());
2624 rtc::scoped_refptr<webrtc::AudioTrackInterface> remote_audio =
2625 remote_stream->GetAudioTracks()[0];
2626 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, remote_audio->state());
2627
kwibergd1fe2812016-04-27 06:47:29 -07002628 std::unique_ptr<SessionDescriptionInterface> local_answer;
kwiberg2bbff992016-03-16 11:03:04 -07002629 EXPECT_TRUE(DoCreateAnswer(&local_answer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07002630 cricket::ContentInfo* video_info =
2631 local_answer->description()->GetContentByName("video");
2632 video_info->rejected = true;
2633 EXPECT_TRUE(DoSetLocalDescription(local_answer.release()));
2634 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kEnded, remote_video->state());
2635 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, remote_audio->state());
2636
2637 // Now create an offer where we reject both video and audio.
kwibergd1fe2812016-04-27 06:47:29 -07002638 std::unique_ptr<SessionDescriptionInterface> local_offer;
kwiberg2bbff992016-03-16 11:03:04 -07002639 EXPECT_TRUE(DoCreateOffer(&local_offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07002640 video_info = local_offer->description()->GetContentByName("video");
2641 ASSERT_TRUE(video_info != nullptr);
2642 video_info->rejected = true;
2643 cricket::ContentInfo* audio_info =
2644 local_offer->description()->GetContentByName("audio");
2645 ASSERT_TRUE(audio_info != nullptr);
2646 audio_info->rejected = true;
2647 EXPECT_TRUE(DoSetLocalDescription(local_offer.release()));
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002648 // Track state may be updated asynchronously.
perkjd61bf802016-03-24 03:16:19 -07002649 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002650 remote_audio->state(), kTimeout);
2651 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
2652 remote_video->state(), kTimeout);
deadbeefab9b2d12015-10-14 11:33:11 -07002653}
2654
2655// This tests that we won't crash if the remote track has been removed outside
2656// of PeerConnection and then PeerConnection tries to reject the track.
2657TEST_F(PeerConnectionInterfaceTest, RemoveTrackThenRejectMediaContent) {
2658 FakeConstraints constraints;
2659 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2660 true);
2661 CreatePeerConnection(&constraints);
2662 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2663 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2664 remote_stream->RemoveTrack(remote_stream->GetVideoTracks()[0]);
2665 remote_stream->RemoveTrack(remote_stream->GetAudioTracks()[0]);
2666
kwibergd1fe2812016-04-27 06:47:29 -07002667 std::unique_ptr<SessionDescriptionInterface> local_answer(
deadbeefab9b2d12015-10-14 11:33:11 -07002668 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
2669 kSdpStringWithStream1, nullptr));
2670 cricket::ContentInfo* video_info =
2671 local_answer->description()->GetContentByName("video");
2672 video_info->rejected = true;
2673 cricket::ContentInfo* audio_info =
2674 local_answer->description()->GetContentByName("audio");
2675 audio_info->rejected = true;
2676 EXPECT_TRUE(DoSetLocalDescription(local_answer.release()));
2677
2678 // No crash is a pass.
2679}
2680
deadbeef5e97fb52015-10-15 12:49:08 -07002681// This tests that if a recvonly remote description is set, no remote streams
2682// will be created, even if the description contains SSRCs/MSIDs.
2683// See: https://code.google.com/p/webrtc/issues/detail?id=5054
2684TEST_F(PeerConnectionInterfaceTest, RecvonlyDescriptionDoesntCreateStream) {
2685 FakeConstraints constraints;
2686 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2687 true);
2688 CreatePeerConnection(&constraints);
2689
2690 std::string recvonly_offer = kSdpStringWithStream1;
2691 rtc::replace_substrs(kSendrecv, strlen(kSendrecv), kRecvonly,
2692 strlen(kRecvonly), &recvonly_offer);
2693 CreateAndSetRemoteOffer(recvonly_offer);
2694
2695 EXPECT_EQ(0u, observer_.remote_streams()->count());
2696}
2697
deadbeefab9b2d12015-10-14 11:33:11 -07002698// This tests that a default MediaStream is created if a remote session
2699// description doesn't contain any streams and no MSID support.
2700// It also tests that the default stream is updated if a video m-line is added
2701// in a subsequent session description.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002702TEST_F(PeerConnectionInterfaceTest, SdpWithoutMsidCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002703 FakeConstraints constraints;
2704 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2705 true);
2706 CreatePeerConnection(&constraints);
2707 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2708
2709 ASSERT_EQ(1u, observer_.remote_streams()->count());
2710 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2711
2712 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2713 EXPECT_EQ(0u, remote_stream->GetVideoTracks().size());
2714 EXPECT_EQ("default", remote_stream->label());
2715
2716 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2717 ASSERT_EQ(1u, observer_.remote_streams()->count());
2718 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2719 EXPECT_EQ("defaulta0", remote_stream->GetAudioTracks()[0]->id());
deadbeef884f5852016-01-15 09:20:04 -08002720 EXPECT_EQ(MediaStreamTrackInterface::kLive,
2721 remote_stream->GetAudioTracks()[0]->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002722 ASSERT_EQ(1u, remote_stream->GetVideoTracks().size());
2723 EXPECT_EQ("defaultv0", remote_stream->GetVideoTracks()[0]->id());
deadbeef884f5852016-01-15 09:20:04 -08002724 EXPECT_EQ(MediaStreamTrackInterface::kLive,
2725 remote_stream->GetVideoTracks()[0]->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002726}
2727
2728// This tests that a default MediaStream is created if a remote session
2729// description doesn't contain any streams and media direction is send only.
2730TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002731 SendOnlySdpWithoutMsidCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002732 FakeConstraints constraints;
2733 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2734 true);
2735 CreatePeerConnection(&constraints);
2736 CreateAndSetRemoteOffer(kSdpStringSendOnlyWithoutStreams);
2737
2738 ASSERT_EQ(1u, observer_.remote_streams()->count());
2739 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2740
2741 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2742 EXPECT_EQ(1u, remote_stream->GetVideoTracks().size());
2743 EXPECT_EQ("default", remote_stream->label());
2744}
2745
2746// This tests that it won't crash when PeerConnection tries to remove
2747// a remote track that as already been removed from the MediaStream.
2748TEST_F(PeerConnectionInterfaceTest, RemoveAlreadyGoneRemoteStream) {
2749 FakeConstraints constraints;
2750 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2751 true);
2752 CreatePeerConnection(&constraints);
2753 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2754 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2755 remote_stream->RemoveTrack(remote_stream->GetAudioTracks()[0]);
2756 remote_stream->RemoveTrack(remote_stream->GetVideoTracks()[0]);
2757
2758 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2759
2760 // No crash is a pass.
2761}
2762
2763// This tests that a default MediaStream is created if the remote session
2764// description doesn't contain any streams and don't contain an indication if
2765// MSID is supported.
2766TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002767 SdpWithoutMsidAndStreamsCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002768 FakeConstraints constraints;
2769 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2770 true);
2771 CreatePeerConnection(&constraints);
2772 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2773
2774 ASSERT_EQ(1u, observer_.remote_streams()->count());
2775 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2776 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2777 EXPECT_EQ(1u, remote_stream->GetVideoTracks().size());
2778}
2779
2780// This tests that a default MediaStream is not created if the remote session
2781// description doesn't contain any streams but does support MSID.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002782TEST_F(PeerConnectionInterfaceTest, SdpWithMsidDontCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002783 FakeConstraints constraints;
2784 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2785 true);
2786 CreatePeerConnection(&constraints);
2787 CreateAndSetRemoteOffer(kSdpStringWithMsidWithoutStreams);
2788 EXPECT_EQ(0u, observer_.remote_streams()->count());
2789}
2790
deadbeefbda7e0b2015-12-08 17:13:40 -08002791// This tests that when setting a new description, the old default tracks are
2792// not destroyed and recreated.
2793// See: https://bugs.chromium.org/p/webrtc/issues/detail?id=5250
Stefan Holmer102362b2016-03-18 09:39:07 +01002794TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002795 DefaultTracksNotDestroyedAndRecreated) {
deadbeefbda7e0b2015-12-08 17:13:40 -08002796 FakeConstraints constraints;
2797 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2798 true);
2799 CreatePeerConnection(&constraints);
2800 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2801
2802 ASSERT_EQ(1u, observer_.remote_streams()->count());
2803 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2804 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2805
2806 // Set the track to "disabled", then set a new description and ensure the
2807 // track is still disabled, which ensures it hasn't been recreated.
2808 remote_stream->GetAudioTracks()[0]->set_enabled(false);
2809 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2810 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2811 EXPECT_FALSE(remote_stream->GetAudioTracks()[0]->enabled());
2812}
2813
deadbeefab9b2d12015-10-14 11:33:11 -07002814// This tests that a default MediaStream is not created if a remote session
2815// description is updated to not have any MediaStreams.
2816TEST_F(PeerConnectionInterfaceTest, VerifyDefaultStreamIsNotCreated) {
2817 FakeConstraints constraints;
2818 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2819 true);
2820 CreatePeerConnection(&constraints);
2821 CreateAndSetRemoteOffer(kSdpStringWithStream1);
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002822 rtc::scoped_refptr<StreamCollection> reference(CreateStreamCollection(1, 1));
deadbeefab9b2d12015-10-14 11:33:11 -07002823 EXPECT_TRUE(
2824 CompareStreamCollections(observer_.remote_streams(), reference.get()));
2825
2826 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2827 EXPECT_EQ(0u, observer_.remote_streams()->count());
2828}
2829
2830// This tests that an RtpSender is created when the local description is set
2831// after adding a local stream.
2832// TODO(deadbeef): This test and the one below it need to be updated when
2833// an RtpSender's lifetime isn't determined by when a local description is set.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002834TEST_F(PeerConnectionInterfaceTest, LocalDescriptionChanged) {
deadbeefab9b2d12015-10-14 11:33:11 -07002835 FakeConstraints constraints;
2836 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2837 true);
2838 CreatePeerConnection(&constraints);
deadbeefab9b2d12015-10-14 11:33:11 -07002839
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002840 // Create an offer with 1 stream with 2 tracks of each type.
2841 rtc::scoped_refptr<StreamCollection> stream_collection =
2842 CreateStreamCollection(1, 2);
2843 pc_->AddStream(stream_collection->at(0));
2844 std::unique_ptr<SessionDescriptionInterface> offer;
2845 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2846 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
deadbeefab9b2d12015-10-14 11:33:11 -07002847
deadbeefab9b2d12015-10-14 11:33:11 -07002848 auto senders = pc_->GetSenders();
2849 EXPECT_EQ(4u, senders.size());
2850 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2851 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2852 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[1]));
2853 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[1]));
2854
2855 // Remove an audio and video track.
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002856 pc_->RemoveStream(stream_collection->at(0));
2857 stream_collection = CreateStreamCollection(1, 1);
2858 pc_->AddStream(stream_collection->at(0));
2859 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2860 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
2861
deadbeefab9b2d12015-10-14 11:33:11 -07002862 senders = pc_->GetSenders();
2863 EXPECT_EQ(2u, senders.size());
2864 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2865 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2866 EXPECT_FALSE(ContainsSender(senders, kAudioTracks[1]));
2867 EXPECT_FALSE(ContainsSender(senders, kVideoTracks[1]));
2868}
2869
2870// This tests that an RtpSender is created when the local description is set
2871// before adding a local stream.
2872TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002873 AddLocalStreamAfterLocalDescriptionChanged) {
deadbeefab9b2d12015-10-14 11:33:11 -07002874 FakeConstraints constraints;
2875 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2876 true);
2877 CreatePeerConnection(&constraints);
deadbeefab9b2d12015-10-14 11:33:11 -07002878
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002879 rtc::scoped_refptr<StreamCollection> stream_collection =
2880 CreateStreamCollection(1, 2);
2881 // Add a stream to create the offer, but remove it afterwards.
2882 pc_->AddStream(stream_collection->at(0));
2883 std::unique_ptr<SessionDescriptionInterface> offer;
2884 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2885 pc_->RemoveStream(stream_collection->at(0));
deadbeefab9b2d12015-10-14 11:33:11 -07002886
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002887 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
deadbeefab9b2d12015-10-14 11:33:11 -07002888 auto senders = pc_->GetSenders();
2889 EXPECT_EQ(0u, senders.size());
2890
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002891 pc_->AddStream(stream_collection->at(0));
deadbeefab9b2d12015-10-14 11:33:11 -07002892 senders = pc_->GetSenders();
2893 EXPECT_EQ(4u, senders.size());
2894 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2895 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2896 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[1]));
2897 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[1]));
2898}
2899
2900// This tests that the expected behavior occurs if the SSRC on a local track is
2901// changed when SetLocalDescription is called.
2902TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002903 ChangeSsrcOnTrackInLocalSessionDescription) {
deadbeefab9b2d12015-10-14 11:33:11 -07002904 FakeConstraints constraints;
2905 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2906 true);
2907 CreatePeerConnection(&constraints);
deadbeefab9b2d12015-10-14 11:33:11 -07002908
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002909 rtc::scoped_refptr<StreamCollection> stream_collection =
2910 CreateStreamCollection(2, 1);
2911 pc_->AddStream(stream_collection->at(0));
2912 std::unique_ptr<SessionDescriptionInterface> offer;
2913 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2914 // Grab a copy of the offer before it gets passed into the PC.
2915 std::unique_ptr<JsepSessionDescription> modified_offer(
2916 new JsepSessionDescription(JsepSessionDescription::kOffer));
2917 modified_offer->Initialize(offer->description()->Copy(), offer->session_id(),
2918 offer->session_version());
2919 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
deadbeefab9b2d12015-10-14 11:33:11 -07002920
deadbeefab9b2d12015-10-14 11:33:11 -07002921 auto senders = pc_->GetSenders();
2922 EXPECT_EQ(2u, senders.size());
2923 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2924 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2925
2926 // Change the ssrc of the audio and video track.
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002927 cricket::MediaContentDescription* desc =
2928 cricket::GetFirstAudioContentDescription(modified_offer->description());
2929 ASSERT_TRUE(desc != NULL);
2930 for (StreamParams& stream : desc->mutable_streams()) {
2931 for (unsigned int& ssrc : stream.ssrcs) {
2932 ++ssrc;
2933 }
2934 }
deadbeefab9b2d12015-10-14 11:33:11 -07002935
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002936 desc =
2937 cricket::GetFirstVideoContentDescription(modified_offer->description());
2938 ASSERT_TRUE(desc != NULL);
2939 for (StreamParams& stream : desc->mutable_streams()) {
2940 for (unsigned int& ssrc : stream.ssrcs) {
2941 ++ssrc;
2942 }
2943 }
2944
2945 EXPECT_TRUE(DoSetLocalDescription(modified_offer.release()));
deadbeefab9b2d12015-10-14 11:33:11 -07002946 senders = pc_->GetSenders();
2947 EXPECT_EQ(2u, senders.size());
2948 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2949 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2950 // TODO(deadbeef): Once RtpSenders expose parameters, check that the SSRC
2951 // changed.
2952}
2953
2954// This tests that the expected behavior occurs if a new session description is
2955// set with the same tracks, but on a different MediaStream.
Stefan Holmer55d6e7c2016-03-17 16:26:40 +01002956TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002957 SignalSameTracksInSeparateMediaStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002958 FakeConstraints constraints;
2959 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2960 true);
2961 CreatePeerConnection(&constraints);
deadbeefab9b2d12015-10-14 11:33:11 -07002962
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002963 rtc::scoped_refptr<StreamCollection> stream_collection =
2964 CreateStreamCollection(2, 1);
2965 pc_->AddStream(stream_collection->at(0));
2966 std::unique_ptr<SessionDescriptionInterface> offer;
2967 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2968 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
deadbeefab9b2d12015-10-14 11:33:11 -07002969
deadbeefab9b2d12015-10-14 11:33:11 -07002970 auto senders = pc_->GetSenders();
2971 EXPECT_EQ(2u, senders.size());
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002972 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0], kStreams[0]));
2973 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0], kStreams[0]));
deadbeefab9b2d12015-10-14 11:33:11 -07002974
2975 // Add a new MediaStream but with the same tracks as in the first stream.
2976 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream_1(
2977 webrtc::MediaStream::Create(kStreams[1]));
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002978 stream_1->AddTrack(stream_collection->at(0)->GetVideoTracks()[0]);
2979 stream_1->AddTrack(stream_collection->at(0)->GetAudioTracks()[0]);
deadbeefab9b2d12015-10-14 11:33:11 -07002980 pc_->AddStream(stream_1);
2981
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002982 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2983 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
deadbeefab9b2d12015-10-14 11:33:11 -07002984
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002985 auto new_senders = pc_->GetSenders();
2986 // Should be the same senders as before, but with updated stream id.
2987 // Note that this behavior is subject to change in the future.
2988 // We may decide the PC should ignore existing tracks in AddStream.
2989 EXPECT_EQ(senders, new_senders);
2990 EXPECT_TRUE(ContainsSender(new_senders, kAudioTracks[0], kStreams[1]));
2991 EXPECT_TRUE(ContainsSender(new_senders, kVideoTracks[0], kStreams[1]));
deadbeefab9b2d12015-10-14 11:33:11 -07002992}
2993
zhihuang81c3a032016-11-17 12:06:24 -08002994// This tests that PeerConnectionObserver::OnAddTrack is correctly called.
2995TEST_F(PeerConnectionInterfaceTest, OnAddTrackCallback) {
2996 FakeConstraints constraints;
2997 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2998 true);
2999 CreatePeerConnection(&constraints);
3000 CreateAndSetRemoteOffer(kSdpStringWithStream1AudioTrackOnly);
3001 EXPECT_EQ(observer_.num_added_tracks_, 1);
3002 EXPECT_EQ(observer_.last_added_track_label_, kAudioTracks[0]);
3003
3004 // Create and set the updated remote SDP.
3005 CreateAndSetRemoteOffer(kSdpStringWithStream1);
3006 EXPECT_EQ(observer_.num_added_tracks_, 2);
3007 EXPECT_EQ(observer_.last_added_track_label_, kVideoTracks[0]);
3008}
3009
deadbeefd1a38b52016-12-10 13:15:33 -08003010// Test that when SetConfiguration is called and the configuration is
3011// changing, the next offer causes an ICE restart.
3012TEST_F(PeerConnectionInterfaceTest, SetConfigurationCausingIceRetart) {
3013 PeerConnectionInterface::RTCConfiguration config;
3014 config.type = PeerConnectionInterface::kRelay;
3015 // Need to pass default constraints to prevent disabling of DTLS...
3016 FakeConstraints default_constraints;
3017 CreatePeerConnection(config, &default_constraints);
3018 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
3019
3020 // Do initial offer/answer so there's something to restart.
3021 CreateOfferAsLocalDescription();
3022 CreateAnswerAsRemoteDescription(kSdpStringWithStream1);
3023
3024 // Grab the ufrags.
3025 std::vector<std::string> initial_ufrags = GetUfrags(pc_->local_description());
3026
3027 // Change ICE policy, which should trigger an ICE restart on the next offer.
3028 config.type = PeerConnectionInterface::kAll;
3029 EXPECT_TRUE(pc_->SetConfiguration(config));
3030 CreateOfferAsLocalDescription();
3031
3032 // Grab the new ufrags.
3033 std::vector<std::string> subsequent_ufrags =
3034 GetUfrags(pc_->local_description());
3035
3036 // Sanity check.
3037 EXPECT_EQ(initial_ufrags.size(), subsequent_ufrags.size());
3038 // Check that each ufrag is different.
3039 for (int i = 0; i < static_cast<int>(initial_ufrags.size()); ++i) {
3040 EXPECT_NE(initial_ufrags[i], subsequent_ufrags[i]);
3041 }
3042}
3043
3044// Test that when SetConfiguration is called and the configuration *isn't*
3045// changing, the next offer does *not* cause an ICE restart.
3046TEST_F(PeerConnectionInterfaceTest, SetConfigurationNotCausingIceRetart) {
3047 PeerConnectionInterface::RTCConfiguration config;
3048 config.type = PeerConnectionInterface::kRelay;
3049 // Need to pass default constraints to prevent disabling of DTLS...
3050 FakeConstraints default_constraints;
3051 CreatePeerConnection(config, &default_constraints);
3052 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
3053
3054 // Do initial offer/answer so there's something to restart.
3055 CreateOfferAsLocalDescription();
3056 CreateAnswerAsRemoteDescription(kSdpStringWithStream1);
3057
3058 // Grab the ufrags.
3059 std::vector<std::string> initial_ufrags = GetUfrags(pc_->local_description());
3060
3061 // Call SetConfiguration with a config identical to what the PC was
3062 // constructed with.
3063 EXPECT_TRUE(pc_->SetConfiguration(config));
3064 CreateOfferAsLocalDescription();
3065
3066 // Grab the new ufrags.
3067 std::vector<std::string> subsequent_ufrags =
3068 GetUfrags(pc_->local_description());
3069
3070 EXPECT_EQ(initial_ufrags, subsequent_ufrags);
3071}
3072
3073// Test for a weird corner case scenario:
3074// 1. Audio/video session established.
3075// 2. SetConfiguration changes ICE config; ICE restart needed.
3076// 3. ICE restart initiated by remote peer, but only for one m= section.
3077// 4. Next createOffer should initiate an ICE restart, but only for the other
3078// m= section; it would be pointless to do an ICE restart for the m= section
3079// that was already restarted.
3080TEST_F(PeerConnectionInterfaceTest, SetConfigurationCausingPartialIceRestart) {
3081 PeerConnectionInterface::RTCConfiguration config;
3082 config.type = PeerConnectionInterface::kRelay;
3083 // Need to pass default constraints to prevent disabling of DTLS...
3084 FakeConstraints default_constraints;
3085 CreatePeerConnection(config, &default_constraints);
3086 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
3087
3088 // Do initial offer/answer so there's something to restart.
3089 CreateOfferAsLocalDescription();
3090 CreateAnswerAsRemoteDescription(kSdpStringWithStream1);
3091
3092 // Change ICE policy, which should set the "needs-ice-restart" flag.
3093 config.type = PeerConnectionInterface::kAll;
3094 EXPECT_TRUE(pc_->SetConfiguration(config));
3095
3096 // Do ICE restart for the first m= section, initiated by remote peer.
3097 webrtc::JsepSessionDescription* remote_offer =
3098 new webrtc::JsepSessionDescription(SessionDescriptionInterface::kOffer);
3099 EXPECT_TRUE(remote_offer->Initialize(kSdpStringWithStream1, nullptr));
3100 remote_offer->description()->transport_infos()[0].description.ice_ufrag =
3101 "modified";
3102 EXPECT_TRUE(DoSetRemoteDescription(remote_offer));
3103 CreateAnswerAsLocalDescription();
3104
3105 // Grab the ufrags.
3106 std::vector<std::string> initial_ufrags = GetUfrags(pc_->local_description());
3107 ASSERT_EQ(2, initial_ufrags.size());
3108
3109 // Create offer and grab the new ufrags.
3110 CreateOfferAsLocalDescription();
3111 std::vector<std::string> subsequent_ufrags =
3112 GetUfrags(pc_->local_description());
3113 ASSERT_EQ(2, subsequent_ufrags.size());
3114
3115 // Ensure that only the ufrag for the second m= section changed.
3116 EXPECT_EQ(initial_ufrags[0], subsequent_ufrags[0]);
3117 EXPECT_NE(initial_ufrags[1], subsequent_ufrags[1]);
3118}
3119
deadbeeffe4a8a42016-12-20 17:56:17 -08003120// Tests that the methods to return current/pending descriptions work as
3121// expected at different points in the offer/answer exchange. This test does
3122// one offer/answer exchange as the offerer, then another as the answerer.
3123TEST_F(PeerConnectionInterfaceTest, CurrentAndPendingDescriptions) {
3124 // This disables DTLS so we can apply an answer to ourselves.
3125 CreatePeerConnection();
3126
3127 // Create initial local offer and get SDP (which will also be used as
3128 // answer/pranswer);
3129 std::unique_ptr<SessionDescriptionInterface> offer;
3130 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
3131 std::string sdp;
3132 EXPECT_TRUE(offer->ToString(&sdp));
3133
3134 // Set local offer.
3135 SessionDescriptionInterface* local_offer = offer.release();
3136 EXPECT_TRUE(DoSetLocalDescription(local_offer));
3137 EXPECT_EQ(local_offer, pc_->pending_local_description());
3138 EXPECT_EQ(nullptr, pc_->pending_remote_description());
3139 EXPECT_EQ(nullptr, pc_->current_local_description());
3140 EXPECT_EQ(nullptr, pc_->current_remote_description());
3141
3142 // Set remote pranswer.
3143 SessionDescriptionInterface* remote_pranswer =
3144 webrtc::CreateSessionDescription(SessionDescriptionInterface::kPrAnswer,
3145 sdp, nullptr);
3146 EXPECT_TRUE(DoSetRemoteDescription(remote_pranswer));
3147 EXPECT_EQ(local_offer, pc_->pending_local_description());
3148 EXPECT_EQ(remote_pranswer, pc_->pending_remote_description());
3149 EXPECT_EQ(nullptr, pc_->current_local_description());
3150 EXPECT_EQ(nullptr, pc_->current_remote_description());
3151
3152 // Set remote answer.
3153 SessionDescriptionInterface* remote_answer = webrtc::CreateSessionDescription(
3154 SessionDescriptionInterface::kAnswer, sdp, nullptr);
3155 EXPECT_TRUE(DoSetRemoteDescription(remote_answer));
3156 EXPECT_EQ(nullptr, pc_->pending_local_description());
3157 EXPECT_EQ(nullptr, pc_->pending_remote_description());
3158 EXPECT_EQ(local_offer, pc_->current_local_description());
3159 EXPECT_EQ(remote_answer, pc_->current_remote_description());
3160
3161 // Set remote offer.
3162 SessionDescriptionInterface* remote_offer = webrtc::CreateSessionDescription(
3163 SessionDescriptionInterface::kOffer, sdp, nullptr);
3164 EXPECT_TRUE(DoSetRemoteDescription(remote_offer));
3165 EXPECT_EQ(remote_offer, pc_->pending_remote_description());
3166 EXPECT_EQ(nullptr, pc_->pending_local_description());
3167 EXPECT_EQ(local_offer, pc_->current_local_description());
3168 EXPECT_EQ(remote_answer, pc_->current_remote_description());
3169
3170 // Set local pranswer.
3171 SessionDescriptionInterface* local_pranswer =
3172 webrtc::CreateSessionDescription(SessionDescriptionInterface::kPrAnswer,
3173 sdp, nullptr);
3174 EXPECT_TRUE(DoSetLocalDescription(local_pranswer));
3175 EXPECT_EQ(remote_offer, pc_->pending_remote_description());
3176 EXPECT_EQ(local_pranswer, pc_->pending_local_description());
3177 EXPECT_EQ(local_offer, pc_->current_local_description());
3178 EXPECT_EQ(remote_answer, pc_->current_remote_description());
3179
3180 // Set local answer.
3181 SessionDescriptionInterface* local_answer = webrtc::CreateSessionDescription(
3182 SessionDescriptionInterface::kAnswer, sdp, nullptr);
3183 EXPECT_TRUE(DoSetLocalDescription(local_answer));
3184 EXPECT_EQ(nullptr, pc_->pending_remote_description());
3185 EXPECT_EQ(nullptr, pc_->pending_local_description());
3186 EXPECT_EQ(remote_offer, pc_->current_remote_description());
3187 EXPECT_EQ(local_answer, pc_->current_local_description());
3188}
3189
zhihuang77985012017-02-07 15:45:16 -08003190// Tests that it won't crash when calling StartRtcEventLog or StopRtcEventLog
3191// after the PeerConnection is closed.
3192TEST_F(PeerConnectionInterfaceTest,
3193 StartAndStopLoggingAfterPeerConnectionClosed) {
3194 CreatePeerConnection();
3195 // The RtcEventLog will be reset when the PeerConnection is closed.
3196 pc_->Close();
3197
3198 rtc::PlatformFile file = 0;
3199 int64_t max_size_bytes = 1024;
3200 EXPECT_FALSE(pc_->StartRtcEventLog(file, max_size_bytes));
3201 pc_->StopRtcEventLog();
3202}
3203
deadbeef30952b42017-04-21 02:41:29 -07003204// Test that generated offers/answers include "ice-option:trickle".
3205TEST_F(PeerConnectionInterfaceTest, OffersAndAnswersHaveTrickleIceOption) {
3206 CreatePeerConnection();
3207
3208 // First, create an offer with audio/video.
3209 FakeConstraints constraints;
3210 constraints.SetMandatoryReceiveAudio(true);
3211 constraints.SetMandatoryReceiveVideo(true);
3212 std::unique_ptr<SessionDescriptionInterface> offer;
3213 ASSERT_TRUE(DoCreateOffer(&offer, &constraints));
3214 cricket::SessionDescription* desc = offer->description();
3215 ASSERT_EQ(2u, desc->transport_infos().size());
3216 EXPECT_TRUE(desc->transport_infos()[0].description.HasOption("trickle"));
3217 EXPECT_TRUE(desc->transport_infos()[1].description.HasOption("trickle"));
3218
3219 // Apply the offer as a remote description, then create an answer.
3220 EXPECT_TRUE(DoSetRemoteDescription(offer.release()));
3221 std::unique_ptr<SessionDescriptionInterface> answer;
3222 ASSERT_TRUE(DoCreateAnswer(&answer, &constraints));
3223 desc = answer->description();
3224 ASSERT_EQ(2u, desc->transport_infos().size());
3225 EXPECT_TRUE(desc->transport_infos()[0].description.HasOption("trickle"));
3226 EXPECT_TRUE(desc->transport_infos()[1].description.HasOption("trickle"));
3227}
3228
deadbeef1dcb1642017-03-29 21:08:16 -07003229// Test that ICE renomination isn't offered if it's not enabled in the PC's
3230// RTCConfiguration.
3231TEST_F(PeerConnectionInterfaceTest, IceRenominationNotOffered) {
3232 PeerConnectionInterface::RTCConfiguration config;
3233 config.enable_ice_renomination = false;
3234 CreatePeerConnection(config, nullptr);
3235 AddVoiceStream("foo");
3236
3237 std::unique_ptr<SessionDescriptionInterface> offer;
3238 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
3239 cricket::SessionDescription* desc = offer->description();
3240 EXPECT_EQ(1u, desc->transport_infos().size());
3241 EXPECT_FALSE(
3242 desc->transport_infos()[0].description.GetIceParameters().renomination);
3243}
3244
3245// Test that the ICE renomination option is present in generated offers/answers
3246// if it's enabled in the PC's RTCConfiguration.
3247TEST_F(PeerConnectionInterfaceTest, IceRenominationOptionInOfferAndAnswer) {
3248 PeerConnectionInterface::RTCConfiguration config;
3249 config.enable_ice_renomination = true;
3250 CreatePeerConnection(config, nullptr);
3251 AddVoiceStream("foo");
3252
3253 std::unique_ptr<SessionDescriptionInterface> offer;
3254 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
3255 cricket::SessionDescription* desc = offer->description();
3256 EXPECT_EQ(1u, desc->transport_infos().size());
3257 EXPECT_TRUE(
3258 desc->transport_infos()[0].description.GetIceParameters().renomination);
3259
3260 // Set the offer as a remote description, then create an answer and ensure it
3261 // has the renomination flag too.
3262 EXPECT_TRUE(DoSetRemoteDescription(offer.release()));
3263 std::unique_ptr<SessionDescriptionInterface> answer;
3264 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
3265 desc = answer->description();
3266 EXPECT_EQ(1u, desc->transport_infos().size());
3267 EXPECT_TRUE(
3268 desc->transport_infos()[0].description.GetIceParameters().renomination);
3269}
3270
3271// Test that if CreateOffer is called with the deprecated "offer to receive
3272// audio/video" constraints, they're processed and result in an offer with
3273// audio/video sections just as if RTCOfferAnswerOptions had been used.
3274TEST_F(PeerConnectionInterfaceTest, CreateOfferWithOfferToReceiveConstraints) {
3275 CreatePeerConnection();
3276
3277 FakeConstraints constraints;
3278 constraints.SetMandatoryReceiveAudio(true);
3279 constraints.SetMandatoryReceiveVideo(true);
3280 std::unique_ptr<SessionDescriptionInterface> offer;
3281 ASSERT_TRUE(DoCreateOffer(&offer, &constraints));
3282
3283 cricket::SessionDescription* desc = offer->description();
3284 const cricket::ContentInfo* audio = cricket::GetFirstAudioContent(desc);
3285 const cricket::ContentInfo* video = cricket::GetFirstVideoContent(desc);
3286 ASSERT_NE(nullptr, audio);
3287 ASSERT_NE(nullptr, video);
3288 EXPECT_FALSE(audio->rejected);
3289 EXPECT_FALSE(video->rejected);
3290}
3291
3292// Test that if CreateAnswer is called with the deprecated "offer to receive
3293// audio/video" constraints, they're processed and can be used to reject an
3294// offered m= section just as can be done with RTCOfferAnswerOptions;
3295TEST_F(PeerConnectionInterfaceTest, CreateAnswerWithOfferToReceiveConstraints) {
3296 CreatePeerConnection();
3297
3298 // First, create an offer with audio/video and apply it as a remote
3299 // description.
3300 FakeConstraints constraints;
3301 constraints.SetMandatoryReceiveAudio(true);
3302 constraints.SetMandatoryReceiveVideo(true);
3303 std::unique_ptr<SessionDescriptionInterface> offer;
3304 ASSERT_TRUE(DoCreateOffer(&offer, &constraints));
3305 EXPECT_TRUE(DoSetRemoteDescription(offer.release()));
3306
3307 // Now create answer that rejects audio/video.
3308 constraints.SetMandatoryReceiveAudio(false);
3309 constraints.SetMandatoryReceiveVideo(false);
3310 std::unique_ptr<SessionDescriptionInterface> answer;
3311 ASSERT_TRUE(DoCreateAnswer(&answer, &constraints));
3312
3313 cricket::SessionDescription* desc = answer->description();
3314 const cricket::ContentInfo* audio = cricket::GetFirstAudioContent(desc);
3315 const cricket::ContentInfo* video = cricket::GetFirstVideoContent(desc);
3316 ASSERT_NE(nullptr, audio);
3317 ASSERT_NE(nullptr, video);
3318 EXPECT_TRUE(audio->rejected);
3319 EXPECT_TRUE(video->rejected);
3320}
3321
3322#ifdef HAVE_SCTP
3323#define MAYBE_DataChannelOnlyOfferWithMaxBundlePolicy \
3324 DataChannelOnlyOfferWithMaxBundlePolicy
3325#else
3326#define MAYBE_DataChannelOnlyOfferWithMaxBundlePolicy \
3327 DISABLED_DataChannelOnlyOfferWithMaxBundlePolicy
3328#endif
3329
3330// Test that negotiation can succeed with a data channel only, and with the max
3331// bundle policy. Previously there was a bug that prevented this.
3332TEST_F(PeerConnectionInterfaceTest,
3333 MAYBE_DataChannelOnlyOfferWithMaxBundlePolicy) {
3334 PeerConnectionInterface::RTCConfiguration config;
3335 config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
3336 CreatePeerConnection(config, nullptr);
3337
3338 // First, create an offer with only a data channel and apply it as a remote
3339 // description.
3340 pc_->CreateDataChannel("test", nullptr);
3341 std::unique_ptr<SessionDescriptionInterface> offer;
3342 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
3343 EXPECT_TRUE(DoSetRemoteDescription(offer.release()));
3344
3345 // Create and set answer as well.
3346 std::unique_ptr<SessionDescriptionInterface> answer;
3347 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
3348 EXPECT_TRUE(DoSetLocalDescription(answer.release()));
3349}
3350
zstein4b979802017-06-02 14:37:37 -07003351TEST_F(PeerConnectionInterfaceTest, SetBitrateWithoutMinSucceeds) {
3352 CreatePeerConnection();
3353 PeerConnectionInterface::BitrateParameters bitrate;
3354 bitrate.current_bitrate_bps = rtc::Optional<int>(100000);
3355 EXPECT_TRUE(pc_->SetBitrate(bitrate).ok());
3356}
3357
3358TEST_F(PeerConnectionInterfaceTest, SetBitrateNegativeMinFails) {
3359 CreatePeerConnection();
3360 PeerConnectionInterface::BitrateParameters bitrate;
3361 bitrate.min_bitrate_bps = rtc::Optional<int>(-1);
3362 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3363}
3364
3365TEST_F(PeerConnectionInterfaceTest, SetBitrateCurrentLessThanMinFails) {
3366 CreatePeerConnection();
3367 PeerConnectionInterface::BitrateParameters bitrate;
3368 bitrate.min_bitrate_bps = rtc::Optional<int>(5);
3369 bitrate.current_bitrate_bps = rtc::Optional<int>(3);
3370 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3371}
3372
3373TEST_F(PeerConnectionInterfaceTest, SetBitrateCurrentNegativeFails) {
3374 CreatePeerConnection();
3375 PeerConnectionInterface::BitrateParameters bitrate;
3376 bitrate.current_bitrate_bps = rtc::Optional<int>(-1);
3377 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3378}
3379
3380TEST_F(PeerConnectionInterfaceTest, SetBitrateMaxLessThanCurrentFails) {
3381 CreatePeerConnection();
3382 PeerConnectionInterface::BitrateParameters bitrate;
3383 bitrate.current_bitrate_bps = rtc::Optional<int>(10);
3384 bitrate.max_bitrate_bps = rtc::Optional<int>(8);
3385 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3386}
3387
3388TEST_F(PeerConnectionInterfaceTest, SetBitrateMaxLessThanMinFails) {
3389 CreatePeerConnection();
3390 PeerConnectionInterface::BitrateParameters bitrate;
3391 bitrate.min_bitrate_bps = rtc::Optional<int>(10);
3392 bitrate.max_bitrate_bps = rtc::Optional<int>(8);
3393 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3394}
3395
3396TEST_F(PeerConnectionInterfaceTest, SetBitrateMaxNegativeFails) {
3397 CreatePeerConnection();
3398 PeerConnectionInterface::BitrateParameters bitrate;
3399 bitrate.max_bitrate_bps = rtc::Optional<int>(-1);
3400 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3401}
3402
3403// The current bitrate from Call's BitrateConfigMask is currently clamped by
3404// Call's BitrateConfig, which comes from the SDP or a default value. This test
3405// checks that a call to SetBitrate with a current bitrate that will be clamped
3406// succeeds.
3407TEST_F(PeerConnectionInterfaceTest, SetBitrateCurrentLessThanImplicitMin) {
3408 CreatePeerConnection();
3409 PeerConnectionInterface::BitrateParameters bitrate;
3410 bitrate.current_bitrate_bps = rtc::Optional<int>(1);
3411 EXPECT_TRUE(pc_->SetBitrate(bitrate).ok());
3412}
3413
nisse51542be2016-02-12 02:27:06 -08003414class PeerConnectionMediaConfigTest : public testing::Test {
3415 protected:
3416 void SetUp() override {
zhihuang38ede132017-06-15 12:52:32 -07003417 pcf_ = PeerConnectionFactoryForTest::CreatePeerConnectionFactoryForTest();
nisse51542be2016-02-12 02:27:06 -08003418 pcf_->Initialize();
3419 }
nisseeaabdf62017-05-05 02:23:02 -07003420 const cricket::MediaConfig TestCreatePeerConnection(
nisse51542be2016-02-12 02:27:06 -08003421 const PeerConnectionInterface::RTCConfiguration& config,
3422 const MediaConstraintsInterface *constraints) {
nisse51542be2016-02-12 02:27:06 -08003423
zhihuang9763d562016-08-05 11:14:50 -07003424 rtc::scoped_refptr<PeerConnectionInterface> pc(pcf_->CreatePeerConnection(
3425 config, constraints, nullptr, nullptr, &observer_));
nisse51542be2016-02-12 02:27:06 -08003426 EXPECT_TRUE(pc.get());
nisseeaabdf62017-05-05 02:23:02 -07003427 return pc->GetConfiguration().media_config;
nisse51542be2016-02-12 02:27:06 -08003428 }
3429
zhihuang9763d562016-08-05 11:14:50 -07003430 rtc::scoped_refptr<PeerConnectionFactoryForTest> pcf_;
nisse51542be2016-02-12 02:27:06 -08003431 MockPeerConnectionObserver observer_;
3432};
3433
3434// This test verifies the default behaviour with no constraints and a
3435// default RTCConfiguration.
3436TEST_F(PeerConnectionMediaConfigTest, TestDefaults) {
3437 PeerConnectionInterface::RTCConfiguration config;
3438 FakeConstraints constraints;
3439
3440 const cricket::MediaConfig& media_config =
3441 TestCreatePeerConnection(config, &constraints);
3442
3443 EXPECT_FALSE(media_config.enable_dscp);
nisse0db023a2016-03-01 04:29:59 -08003444 EXPECT_TRUE(media_config.video.enable_cpu_overuse_detection);
3445 EXPECT_FALSE(media_config.video.disable_prerenderer_smoothing);
3446 EXPECT_FALSE(media_config.video.suspend_below_min_bitrate);
nisse51542be2016-02-12 02:27:06 -08003447}
3448
3449// This test verifies the DSCP constraint is recognized and passed to
nisse528b7932017-05-08 03:21:43 -07003450// the PeerConnection.
nisse51542be2016-02-12 02:27:06 -08003451TEST_F(PeerConnectionMediaConfigTest, TestDscpConstraintTrue) {
3452 PeerConnectionInterface::RTCConfiguration config;
3453 FakeConstraints constraints;
3454
3455 constraints.AddOptional(webrtc::MediaConstraintsInterface::kEnableDscp, true);
3456 const cricket::MediaConfig& media_config =
3457 TestCreatePeerConnection(config, &constraints);
3458
3459 EXPECT_TRUE(media_config.enable_dscp);
3460}
3461
3462// This test verifies the cpu overuse detection constraint is
nisse528b7932017-05-08 03:21:43 -07003463// recognized and passed to the PeerConnection.
nisse51542be2016-02-12 02:27:06 -08003464TEST_F(PeerConnectionMediaConfigTest, TestCpuOveruseConstraintFalse) {
3465 PeerConnectionInterface::RTCConfiguration config;
3466 FakeConstraints constraints;
3467
3468 constraints.AddOptional(
3469 webrtc::MediaConstraintsInterface::kCpuOveruseDetection, false);
3470 const cricket::MediaConfig media_config =
3471 TestCreatePeerConnection(config, &constraints);
3472
nisse0db023a2016-03-01 04:29:59 -08003473 EXPECT_FALSE(media_config.video.enable_cpu_overuse_detection);
nisse51542be2016-02-12 02:27:06 -08003474}
3475
3476// This test verifies that the disable_prerenderer_smoothing flag is
nisse528b7932017-05-08 03:21:43 -07003477// propagated from RTCConfiguration to the PeerConnection.
nisse51542be2016-02-12 02:27:06 -08003478TEST_F(PeerConnectionMediaConfigTest, TestDisablePrerendererSmoothingTrue) {
3479 PeerConnectionInterface::RTCConfiguration config;
3480 FakeConstraints constraints;
3481
Niels Möller71bdda02016-03-31 12:59:59 +02003482 config.set_prerenderer_smoothing(false);
nisse51542be2016-02-12 02:27:06 -08003483 const cricket::MediaConfig& media_config =
3484 TestCreatePeerConnection(config, &constraints);
3485
nisse0db023a2016-03-01 04:29:59 -08003486 EXPECT_TRUE(media_config.video.disable_prerenderer_smoothing);
3487}
3488
3489// This test verifies the suspend below min bitrate constraint is
nisse528b7932017-05-08 03:21:43 -07003490// recognized and passed to the PeerConnection.
nisse0db023a2016-03-01 04:29:59 -08003491TEST_F(PeerConnectionMediaConfigTest,
3492 TestSuspendBelowMinBitrateConstraintTrue) {
3493 PeerConnectionInterface::RTCConfiguration config;
3494 FakeConstraints constraints;
3495
3496 constraints.AddOptional(
3497 webrtc::MediaConstraintsInterface::kEnableVideoSuspendBelowMinBitrate,
3498 true);
3499 const cricket::MediaConfig media_config =
3500 TestCreatePeerConnection(config, &constraints);
3501
3502 EXPECT_TRUE(media_config.video.suspend_below_min_bitrate);
nisse51542be2016-02-12 02:27:06 -08003503}
3504
deadbeefab9b2d12015-10-14 11:33:11 -07003505// The following tests verify that session options are created correctly.
deadbeefc80741f2015-10-22 13:14:45 -07003506// TODO(deadbeef): Convert these tests to be more end-to-end. Instead of
3507// "verify options are converted correctly", should be "pass options into
3508// CreateOffer and verify the correct offer is produced."
deadbeefab9b2d12015-10-14 11:33:11 -07003509
3510TEST(CreateSessionOptionsTest, GetOptionsForOfferWithInvalidAudioOption) {
3511 RTCOfferAnswerOptions rtc_options;
3512 rtc_options.offer_to_receive_audio = RTCOfferAnswerOptions::kUndefined - 1;
3513
3514 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08003515 EXPECT_FALSE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07003516
3517 rtc_options.offer_to_receive_audio =
3518 RTCOfferAnswerOptions::kMaxOfferToReceiveMedia + 1;
htaaac2dea2016-03-10 13:35:55 -08003519 EXPECT_FALSE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07003520}
3521
3522TEST(CreateSessionOptionsTest, GetOptionsForOfferWithInvalidVideoOption) {
3523 RTCOfferAnswerOptions rtc_options;
3524 rtc_options.offer_to_receive_video = RTCOfferAnswerOptions::kUndefined - 1;
3525
3526 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08003527 EXPECT_FALSE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07003528
3529 rtc_options.offer_to_receive_video =
3530 RTCOfferAnswerOptions::kMaxOfferToReceiveMedia + 1;
htaaac2dea2016-03-10 13:35:55 -08003531 EXPECT_FALSE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07003532}
3533
3534// Test that a MediaSessionOptions is created for an offer if
deadbeefc80741f2015-10-22 13:14:45 -07003535// OfferToReceiveAudio and OfferToReceiveVideo options are set.
deadbeefab9b2d12015-10-14 11:33:11 -07003536TEST(CreateSessionOptionsTest, GetMediaSessionOptionsForOfferWithAudioVideo) {
3537 RTCOfferAnswerOptions rtc_options;
3538 rtc_options.offer_to_receive_audio = 1;
3539 rtc_options.offer_to_receive_video = 1;
3540
3541 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08003542 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07003543 EXPECT_TRUE(options.has_audio());
3544 EXPECT_TRUE(options.has_video());
3545 EXPECT_TRUE(options.bundle_enabled);
3546}
3547
3548// Test that a correct MediaSessionOptions is created for an offer if
deadbeefc80741f2015-10-22 13:14:45 -07003549// OfferToReceiveAudio is set.
deadbeefab9b2d12015-10-14 11:33:11 -07003550TEST(CreateSessionOptionsTest, GetMediaSessionOptionsForOfferWithAudio) {
3551 RTCOfferAnswerOptions rtc_options;
3552 rtc_options.offer_to_receive_audio = 1;
3553
3554 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08003555 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07003556 EXPECT_TRUE(options.has_audio());
3557 EXPECT_FALSE(options.has_video());
3558 EXPECT_TRUE(options.bundle_enabled);
3559}
3560
3561// Test that a correct MediaSessionOptions is created for an offer if
deadbeefc80741f2015-10-22 13:14:45 -07003562// the default OfferOptions are used.
deadbeefab9b2d12015-10-14 11:33:11 -07003563TEST(CreateSessionOptionsTest, GetDefaultMediaSessionOptionsForOffer) {
3564 RTCOfferAnswerOptions rtc_options;
3565
3566 cricket::MediaSessionOptions options;
deadbeef0ed85b22016-02-23 17:24:52 -08003567 options.transport_options["audio"] = cricket::TransportOptions();
3568 options.transport_options["video"] = cricket::TransportOptions();
htaaac2dea2016-03-10 13:35:55 -08003569 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefc80741f2015-10-22 13:14:45 -07003570 EXPECT_TRUE(options.has_audio());
deadbeefab9b2d12015-10-14 11:33:11 -07003571 EXPECT_FALSE(options.has_video());
deadbeefc80741f2015-10-22 13:14:45 -07003572 EXPECT_TRUE(options.bundle_enabled);
deadbeefab9b2d12015-10-14 11:33:11 -07003573 EXPECT_TRUE(options.vad_enabled);
deadbeef0ed85b22016-02-23 17:24:52 -08003574 EXPECT_FALSE(options.transport_options["audio"].ice_restart);
3575 EXPECT_FALSE(options.transport_options["video"].ice_restart);
deadbeefab9b2d12015-10-14 11:33:11 -07003576}
3577
3578// Test that a correct MediaSessionOptions is created for an offer if
deadbeefc80741f2015-10-22 13:14:45 -07003579// OfferToReceiveVideo is set.
deadbeefab9b2d12015-10-14 11:33:11 -07003580TEST(CreateSessionOptionsTest, GetMediaSessionOptionsForOfferWithVideo) {
3581 RTCOfferAnswerOptions rtc_options;
3582 rtc_options.offer_to_receive_audio = 0;
3583 rtc_options.offer_to_receive_video = 1;
3584
3585 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08003586 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07003587 EXPECT_FALSE(options.has_audio());
3588 EXPECT_TRUE(options.has_video());
3589 EXPECT_TRUE(options.bundle_enabled);
3590}
3591
3592// Test that a correct MediaSessionOptions is created for an offer if
3593// UseRtpMux is set to false.
3594TEST(CreateSessionOptionsTest,
3595 GetMediaSessionOptionsForOfferWithBundleDisabled) {
3596 RTCOfferAnswerOptions rtc_options;
3597 rtc_options.offer_to_receive_audio = 1;
3598 rtc_options.offer_to_receive_video = 1;
3599 rtc_options.use_rtp_mux = false;
3600
3601 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08003602 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07003603 EXPECT_TRUE(options.has_audio());
3604 EXPECT_TRUE(options.has_video());
3605 EXPECT_FALSE(options.bundle_enabled);
3606}
3607
3608// Test that a correct MediaSessionOptions is created to restart ice if
3609// IceRestart is set. It also tests that subsequent MediaSessionOptions don't
Taylor Brandstetterf475d362016-01-08 15:35:57 -08003610// have |audio_transport_options.ice_restart| etc. set.
deadbeefab9b2d12015-10-14 11:33:11 -07003611TEST(CreateSessionOptionsTest, GetMediaSessionOptionsForOfferWithIceRestart) {
3612 RTCOfferAnswerOptions rtc_options;
3613 rtc_options.ice_restart = true;
3614
3615 cricket::MediaSessionOptions options;
deadbeef0ed85b22016-02-23 17:24:52 -08003616 options.transport_options["audio"] = cricket::TransportOptions();
3617 options.transport_options["video"] = cricket::TransportOptions();
htaaac2dea2016-03-10 13:35:55 -08003618 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeef0ed85b22016-02-23 17:24:52 -08003619 EXPECT_TRUE(options.transport_options["audio"].ice_restart);
3620 EXPECT_TRUE(options.transport_options["video"].ice_restart);
deadbeefab9b2d12015-10-14 11:33:11 -07003621
3622 rtc_options = RTCOfferAnswerOptions();
htaaac2dea2016-03-10 13:35:55 -08003623 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeef0ed85b22016-02-23 17:24:52 -08003624 EXPECT_FALSE(options.transport_options["audio"].ice_restart);
3625 EXPECT_FALSE(options.transport_options["video"].ice_restart);
deadbeefab9b2d12015-10-14 11:33:11 -07003626}
3627
3628// Test that the MediaConstraints in an answer don't affect if audio and video
3629// is offered in an offer but that if kOfferToReceiveAudio or
3630// kOfferToReceiveVideo constraints are true in an offer, the media type will be
3631// included in subsequent answers.
3632TEST(CreateSessionOptionsTest, MediaConstraintsInAnswer) {
3633 FakeConstraints answer_c;
3634 answer_c.SetMandatoryReceiveAudio(true);
3635 answer_c.SetMandatoryReceiveVideo(true);
3636
3637 cricket::MediaSessionOptions answer_options;
3638 EXPECT_TRUE(ParseConstraintsForAnswer(&answer_c, &answer_options));
3639 EXPECT_TRUE(answer_options.has_audio());
3640 EXPECT_TRUE(answer_options.has_video());
3641
deadbeefc80741f2015-10-22 13:14:45 -07003642 RTCOfferAnswerOptions rtc_offer_options;
deadbeefab9b2d12015-10-14 11:33:11 -07003643
3644 cricket::MediaSessionOptions offer_options;
htaaac2dea2016-03-10 13:35:55 -08003645 EXPECT_TRUE(
3646 ExtractMediaSessionOptions(rtc_offer_options, false, &offer_options));
deadbeefc80741f2015-10-22 13:14:45 -07003647 EXPECT_TRUE(offer_options.has_audio());
htaaac2dea2016-03-10 13:35:55 -08003648 EXPECT_TRUE(offer_options.has_video());
deadbeefab9b2d12015-10-14 11:33:11 -07003649
deadbeefc80741f2015-10-22 13:14:45 -07003650 RTCOfferAnswerOptions updated_rtc_offer_options;
3651 updated_rtc_offer_options.offer_to_receive_audio = 1;
3652 updated_rtc_offer_options.offer_to_receive_video = 1;
deadbeefab9b2d12015-10-14 11:33:11 -07003653
3654 cricket::MediaSessionOptions updated_offer_options;
htaaac2dea2016-03-10 13:35:55 -08003655 EXPECT_TRUE(ExtractMediaSessionOptions(updated_rtc_offer_options, false,
htaa2a49d92016-03-04 02:51:39 -08003656 &updated_offer_options));
deadbeefab9b2d12015-10-14 11:33:11 -07003657 EXPECT_TRUE(updated_offer_options.has_audio());
3658 EXPECT_TRUE(updated_offer_options.has_video());
3659
3660 // Since an offer has been created with both audio and video, subsequent
3661 // offers and answers should contain both audio and video.
3662 // Answers will only contain the media types that exist in the offer
3663 // regardless of the value of |updated_answer_options.has_audio| and
3664 // |updated_answer_options.has_video|.
3665 FakeConstraints updated_answer_c;
3666 answer_c.SetMandatoryReceiveAudio(false);
3667 answer_c.SetMandatoryReceiveVideo(false);
3668
3669 cricket::MediaSessionOptions updated_answer_options;
3670 EXPECT_TRUE(
3671 ParseConstraintsForAnswer(&updated_answer_c, &updated_answer_options));
3672 EXPECT_TRUE(updated_answer_options.has_audio());
3673 EXPECT_TRUE(updated_answer_options.has_video());
deadbeefab9b2d12015-10-14 11:33:11 -07003674}
deadbeef3edec7c2016-12-10 11:44:26 -08003675
deadbeef293e9262017-01-11 12:28:30 -08003676// Tests a few random fields being different.
3677TEST(RTCConfigurationTest, ComparisonOperators) {
3678 PeerConnectionInterface::RTCConfiguration a;
3679 PeerConnectionInterface::RTCConfiguration b;
3680 EXPECT_EQ(a, b);
3681
3682 PeerConnectionInterface::RTCConfiguration c;
3683 c.servers.push_back(PeerConnectionInterface::IceServer());
3684 EXPECT_NE(a, c);
3685
3686 PeerConnectionInterface::RTCConfiguration d;
3687 d.type = PeerConnectionInterface::kRelay;
3688 EXPECT_NE(a, d);
3689
3690 PeerConnectionInterface::RTCConfiguration e;
3691 e.audio_jitter_buffer_max_packets = 5;
3692 EXPECT_NE(a, e);
3693
3694 PeerConnectionInterface::RTCConfiguration f;
3695 f.ice_connection_receiving_timeout = 1337;
3696 EXPECT_NE(a, f);
3697
3698 PeerConnectionInterface::RTCConfiguration g;
3699 g.disable_ipv6 = true;
3700 EXPECT_NE(a, g);
3701
3702 PeerConnectionInterface::RTCConfiguration h(
3703 PeerConnectionInterface::RTCConfigurationType::kAggressive);
3704 EXPECT_NE(a, h);
3705}