blob: 892615ad5bc40cbddbc350ccae705af951188093 [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],
perkj773be362017-07-31 23:22:01 -0700463 webrtc::FakeVideoTrackSource::Create(),
464 rtc::Thread::Current()));
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700465 stream->AddTrack(video_track);
466 }
deadbeefab9b2d12015-10-14 11:33:11 -0700467
468 local_collection->AddStream(stream);
469 }
470 return local_collection;
471}
472
473// Check equality of StreamCollections.
474bool CompareStreamCollections(StreamCollectionInterface* s1,
475 StreamCollectionInterface* s2) {
476 if (s1 == nullptr || s2 == nullptr || s1->count() != s2->count()) {
477 return false;
478 }
479
480 for (size_t i = 0; i != s1->count(); ++i) {
481 if (s1->at(i)->label() != s2->at(i)->label()) {
482 return false;
483 }
484 webrtc::AudioTrackVector audio_tracks1 = s1->at(i)->GetAudioTracks();
485 webrtc::AudioTrackVector audio_tracks2 = s2->at(i)->GetAudioTracks();
486 webrtc::VideoTrackVector video_tracks1 = s1->at(i)->GetVideoTracks();
487 webrtc::VideoTrackVector video_tracks2 = s2->at(i)->GetVideoTracks();
488
489 if (audio_tracks1.size() != audio_tracks2.size()) {
490 return false;
491 }
492 for (size_t j = 0; j != audio_tracks1.size(); ++j) {
493 if (audio_tracks1[j]->id() != audio_tracks2[j]->id()) {
494 return false;
495 }
496 }
497 if (video_tracks1.size() != video_tracks2.size()) {
498 return false;
499 }
500 for (size_t j = 0; j != video_tracks1.size(); ++j) {
501 if (video_tracks1[j]->id() != video_tracks2[j]->id()) {
502 return false;
503 }
504 }
505 }
506 return true;
507}
508
perkjd61bf802016-03-24 03:16:19 -0700509// Helper class to test Observer.
510class MockTrackObserver : public ObserverInterface {
511 public:
512 explicit MockTrackObserver(NotifierInterface* notifier)
513 : notifier_(notifier) {
514 notifier_->RegisterObserver(this);
515 }
516
517 ~MockTrackObserver() { Unregister(); }
518
519 void Unregister() {
520 if (notifier_) {
521 notifier_->UnregisterObserver(this);
522 notifier_ = nullptr;
523 }
524 }
525
526 MOCK_METHOD0(OnChanged, void());
527
528 private:
529 NotifierInterface* notifier_;
530};
531
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000532class MockPeerConnectionObserver : public PeerConnectionObserver {
533 public:
deadbeefab9b2d12015-10-14 11:33:11 -0700534 MockPeerConnectionObserver() : remote_streams_(StreamCollection::Create()) {}
Henrik Kjellander3fe372d2016-05-12 08:10:52 +0200535 virtual ~MockPeerConnectionObserver() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000536 }
537 void SetPeerConnectionInterface(PeerConnectionInterface* pc) {
538 pc_ = pc;
539 if (pc) {
540 state_ = pc_->signaling_state();
541 }
542 }
nisseef8b61e2016-04-29 06:09:15 -0700543 void OnSignalingChange(
544 PeerConnectionInterface::SignalingState new_state) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000545 EXPECT_EQ(pc_->signaling_state(), new_state);
546 state_ = new_state;
547 }
deadbeefab9b2d12015-10-14 11:33:11 -0700548
549 MediaStreamInterface* RemoteStream(const std::string& label) {
550 return remote_streams_->find(label);
551 }
552 StreamCollectionInterface* remote_streams() const { return remote_streams_; }
Taylor Brandstetter98cde262016-05-31 13:02:21 -0700553 void OnAddStream(rtc::scoped_refptr<MediaStreamInterface> stream) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000554 last_added_stream_ = stream;
deadbeefab9b2d12015-10-14 11:33:11 -0700555 remote_streams_->AddStream(stream);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000556 }
Taylor Brandstetter98cde262016-05-31 13:02:21 -0700557 void OnRemoveStream(
558 rtc::scoped_refptr<MediaStreamInterface> stream) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000559 last_removed_stream_ = stream;
deadbeefab9b2d12015-10-14 11:33:11 -0700560 remote_streams_->RemoveStream(stream);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000561 }
perkjdfb769d2016-02-09 03:09:43 -0800562 void OnRenegotiationNeeded() override { renegotiation_needed_ = true; }
Taylor Brandstetter98cde262016-05-31 13:02:21 -0700563 void OnDataChannel(
564 rtc::scoped_refptr<DataChannelInterface> data_channel) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000565 last_datachannel_ = data_channel;
566 }
567
perkjdfb769d2016-02-09 03:09:43 -0800568 void OnIceConnectionChange(
569 PeerConnectionInterface::IceConnectionState new_state) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000570 EXPECT_EQ(pc_->ice_connection_state(), new_state);
zhihuang81c3a032016-11-17 12:06:24 -0800571 callback_triggered_ = true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000572 }
perkjdfb769d2016-02-09 03:09:43 -0800573 void OnIceGatheringChange(
574 PeerConnectionInterface::IceGatheringState new_state) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000575 EXPECT_EQ(pc_->ice_gathering_state(), new_state);
perkjdfb769d2016-02-09 03:09:43 -0800576 ice_complete_ = new_state == PeerConnectionInterface::kIceGatheringComplete;
zhihuang81c3a032016-11-17 12:06:24 -0800577 callback_triggered_ = true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000578 }
perkjdfb769d2016-02-09 03:09:43 -0800579 void OnIceCandidate(const webrtc::IceCandidateInterface* candidate) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000580 EXPECT_NE(PeerConnectionInterface::kIceGatheringNew,
581 pc_->ice_gathering_state());
582
583 std::string sdp;
584 EXPECT_TRUE(candidate->ToString(&sdp));
585 EXPECT_LT(0u, sdp.size());
586 last_candidate_.reset(webrtc::CreateIceCandidate(candidate->sdp_mid(),
587 candidate->sdp_mline_index(), sdp, NULL));
588 EXPECT_TRUE(last_candidate_.get() != NULL);
zhihuang81c3a032016-11-17 12:06:24 -0800589 callback_triggered_ = true;
zhihuang29ff8442016-07-27 11:07:25 -0700590 }
591
592 void OnIceCandidatesRemoved(
593 const std::vector<cricket::Candidate>& candidates) override {
zhihuang81c3a032016-11-17 12:06:24 -0800594 callback_triggered_ = true;
zhihuang29ff8442016-07-27 11:07:25 -0700595 }
596
597 void OnIceConnectionReceivingChange(bool receiving) override {
zhihuang81c3a032016-11-17 12:06:24 -0800598 callback_triggered_ = true;
599 }
600
zhihuangc63b8942016-12-02 15:41:10 -0800601 void OnAddTrack(
602 rtc::scoped_refptr<webrtc::RtpReceiverInterface> receiver,
603 const std::vector<rtc::scoped_refptr<webrtc::MediaStreamInterface>>&
604 streams) override {
zhihuang81c3a032016-11-17 12:06:24 -0800605 EXPECT_TRUE(receiver != nullptr);
606 num_added_tracks_++;
607 last_added_track_label_ = receiver->id();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000608 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000609
610 // Returns the label of the last added stream.
611 // Empty string if no stream have been added.
612 std::string GetLastAddedStreamLabel() {
613 if (last_added_stream_.get())
614 return last_added_stream_->label();
615 return "";
616 }
617 std::string GetLastRemovedStreamLabel() {
618 if (last_removed_stream_.get())
619 return last_removed_stream_->label();
620 return "";
621 }
622
zhihuang9763d562016-08-05 11:14:50 -0700623 rtc::scoped_refptr<PeerConnectionInterface> pc_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000624 PeerConnectionInterface::SignalingState state_;
kwibergd1fe2812016-04-27 06:47:29 -0700625 std::unique_ptr<IceCandidateInterface> last_candidate_;
zhihuang9763d562016-08-05 11:14:50 -0700626 rtc::scoped_refptr<DataChannelInterface> last_datachannel_;
deadbeefab9b2d12015-10-14 11:33:11 -0700627 rtc::scoped_refptr<StreamCollection> remote_streams_;
628 bool renegotiation_needed_ = false;
629 bool ice_complete_ = false;
zhihuang81c3a032016-11-17 12:06:24 -0800630 bool callback_triggered_ = false;
631 int num_added_tracks_ = 0;
632 std::string last_added_track_label_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000633
634 private:
zhihuang9763d562016-08-05 11:14:50 -0700635 rtc::scoped_refptr<MediaStreamInterface> last_added_stream_;
636 rtc::scoped_refptr<MediaStreamInterface> last_removed_stream_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000637};
638
639} // namespace
deadbeefab9b2d12015-10-14 11:33:11 -0700640
nisse528b7932017-05-08 03:21:43 -0700641// The PeerConnectionMediaConfig tests below verify that configuration and
642// constraints are propagated into the PeerConnection's MediaConfig. These
643// settings are intended for MediaChannel constructors, but that is not
644// exercised by these unittest.
zhihuang29ff8442016-07-27 11:07:25 -0700645class PeerConnectionFactoryForTest : public webrtc::PeerConnectionFactory {
646 public:
zhihuang38ede132017-06-15 12:52:32 -0700647 static rtc::scoped_refptr<PeerConnectionFactoryForTest>
648 CreatePeerConnectionFactoryForTest() {
649 auto audio_encoder_factory = webrtc::CreateBuiltinAudioEncoderFactory();
650 auto audio_decoder_factory = webrtc::CreateBuiltinAudioDecoderFactory();
651
652 auto media_engine = std::unique_ptr<cricket::MediaEngineInterface>(
653 cricket::WebRtcMediaEngineFactory::Create(
654 nullptr, audio_encoder_factory, audio_decoder_factory, nullptr,
peaha9cc40b2017-06-29 08:32:09 -0700655 nullptr, nullptr, webrtc::AudioProcessing::Create()));
zhihuang38ede132017-06-15 12:52:32 -0700656
657 std::unique_ptr<webrtc::CallFactoryInterface> call_factory =
658 webrtc::CreateCallFactory();
659
660 std::unique_ptr<webrtc::RtcEventLogFactoryInterface> event_log_factory =
661 webrtc::CreateRtcEventLogFactory();
662
663 return new rtc::RefCountedObject<PeerConnectionFactoryForTest>(
664 rtc::Thread::Current(), rtc::Thread::Current(), rtc::Thread::Current(),
665 nullptr, audio_encoder_factory, audio_decoder_factory, nullptr, nullptr,
666 nullptr, std::move(media_engine), std::move(call_factory),
667 std::move(event_log_factory));
668 }
669
670 PeerConnectionFactoryForTest(
671 rtc::Thread* network_thread,
672 rtc::Thread* worker_thread,
673 rtc::Thread* signaling_thread,
674 webrtc::AudioDeviceModule* default_adm,
675 rtc::scoped_refptr<webrtc::AudioEncoderFactory> audio_encoder_factory,
676 rtc::scoped_refptr<webrtc::AudioDecoderFactory> audio_decoder_factory,
677 cricket::WebRtcVideoEncoderFactory* video_encoder_factory,
678 cricket::WebRtcVideoDecoderFactory* video_decoder_factory,
679 rtc::scoped_refptr<webrtc::AudioMixer> audio_mixer,
680 std::unique_ptr<cricket::MediaEngineInterface> media_engine,
681 std::unique_ptr<webrtc::CallFactoryInterface> call_factory,
682 std::unique_ptr<webrtc::RtcEventLogFactoryInterface> event_log_factory)
683 : webrtc::PeerConnectionFactory(network_thread,
684 worker_thread,
685 signaling_thread,
686 default_adm,
687 audio_encoder_factory,
688 audio_decoder_factory,
689 video_encoder_factory,
690 video_decoder_factory,
691 audio_mixer,
692 std::move(media_engine),
693 std::move(call_factory),
694 std::move(event_log_factory)) {}
kwiberg1e4e8cb2017-01-31 01:48:08 -0800695
zhihuang29ff8442016-07-27 11:07:25 -0700696 cricket::TransportController* CreateTransportController(
Honghai Zhangbfd398c2016-08-30 22:07:42 -0700697 cricket::PortAllocator* port_allocator,
698 bool redetermine_role_on_ice_restart) override {
zhihuang29ff8442016-07-27 11:07:25 -0700699 transport_controller = new cricket::TransportController(
Honghai Zhangbfd398c2016-08-30 22:07:42 -0700700 rtc::Thread::Current(), rtc::Thread::Current(), port_allocator,
deadbeef7914b8c2017-04-21 03:23:33 -0700701 redetermine_role_on_ice_restart, rtc::CryptoOptions());
zhihuang29ff8442016-07-27 11:07:25 -0700702 return transport_controller;
703 }
704
705 cricket::TransportController* transport_controller;
zhihuang29ff8442016-07-27 11:07:25 -0700706};
707
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000708class PeerConnectionInterfaceTest : public testing::Test {
709 protected:
deadbeef9a6f4d42017-05-15 19:43:33 -0700710 PeerConnectionInterfaceTest()
deadbeef98e186c2017-05-16 18:00:06 -0700711 : vss_(new rtc::VirtualSocketServer()), main_(vss_.get()) {
phoglund37ebcf02016-01-08 05:04:57 -0800712#ifdef WEBRTC_ANDROID
713 webrtc::InitializeAndroidObjects();
714#endif
715 }
716
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000717 virtual void SetUp() {
718 pc_factory_ = webrtc::CreatePeerConnectionFactory(
danilchape9021a32016-05-17 01:52:02 -0700719 rtc::Thread::Current(), rtc::Thread::Current(), rtc::Thread::Current(),
720 nullptr, nullptr, nullptr);
721 ASSERT_TRUE(pc_factory_);
zhihuang29ff8442016-07-27 11:07:25 -0700722 pc_factory_for_test_ =
zhihuang38ede132017-06-15 12:52:32 -0700723 PeerConnectionFactoryForTest::CreatePeerConnectionFactoryForTest();
zhihuang29ff8442016-07-27 11:07:25 -0700724 pc_factory_for_test_->Initialize();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000725 }
726
727 void CreatePeerConnection() {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700728 CreatePeerConnection(PeerConnectionInterface::RTCConfiguration(), nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000729 }
730
deadbeef293e9262017-01-11 12:28:30 -0800731 // DTLS does not work in a loopback call, so is disabled for most of the
732 // tests in this file.
733 void CreatePeerConnectionWithoutDtls() {
734 FakeConstraints no_dtls_constraints;
735 no_dtls_constraints.AddMandatory(
736 webrtc::MediaConstraintsInterface::kEnableDtlsSrtp, false);
737
738 CreatePeerConnection(PeerConnectionInterface::RTCConfiguration(),
739 &no_dtls_constraints);
740 }
741
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000742 void CreatePeerConnection(webrtc::MediaConstraintsInterface* constraints) {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700743 CreatePeerConnection(PeerConnectionInterface::RTCConfiguration(),
744 constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000745 }
746
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700747 void CreatePeerConnectionWithIceTransportsType(
748 PeerConnectionInterface::IceTransportsType type) {
749 PeerConnectionInterface::RTCConfiguration config;
750 config.type = type;
751 return CreatePeerConnection(config, nullptr);
752 }
753
754 void CreatePeerConnectionWithIceServer(const std::string& uri,
755 const std::string& password) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800756 PeerConnectionInterface::RTCConfiguration config;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000757 PeerConnectionInterface::IceServer server;
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700758 server.uri = uri;
759 server.password = password;
760 config.servers.push_back(server);
761 CreatePeerConnection(config, nullptr);
762 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000763
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700764 void CreatePeerConnection(PeerConnectionInterface::RTCConfiguration config,
765 webrtc::MediaConstraintsInterface* constraints) {
kwibergd1fe2812016-04-27 06:47:29 -0700766 std::unique_ptr<cricket::FakePortAllocator> port_allocator(
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800767 new cricket::FakePortAllocator(rtc::Thread::Current(), nullptr));
768 port_allocator_ = port_allocator.get();
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000769
deadbeef1dcb1642017-03-29 21:08:16 -0700770 // Create certificate generator unless DTLS constraint is explicitly set to
771 // false.
Henrik Boströmd79599d2016-06-01 13:58:50 +0200772 std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator;
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000773 bool dtls;
774 if (FindConstraint(constraints,
775 webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
776 &dtls,
Henrik Boström5e56c592015-08-11 10:33:13 +0200777 nullptr) && dtls) {
deadbeef8662f942017-01-20 21:20:51 -0800778 fake_certificate_generator_ = new FakeRTCCertificateGenerator();
779 cert_generator.reset(fake_certificate_generator_);
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000780 }
Henrik Boströmd79599d2016-06-01 13:58:50 +0200781 pc_ = pc_factory_->CreatePeerConnection(
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800782 config, constraints, std::move(port_allocator),
Henrik Boströmd79599d2016-06-01 13:58:50 +0200783 std::move(cert_generator), &observer_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000784 ASSERT_TRUE(pc_.get() != NULL);
785 observer_.SetPeerConnectionInterface(pc_.get());
786 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
787 }
788
deadbeef0a6c4ca2015-10-06 11:38:28 -0700789 void CreatePeerConnectionExpectFail(const std::string& uri) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800790 PeerConnectionInterface::RTCConfiguration config;
deadbeef0a6c4ca2015-10-06 11:38:28 -0700791 PeerConnectionInterface::IceServer server;
deadbeef0a6c4ca2015-10-06 11:38:28 -0700792 server.uri = uri;
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800793 config.servers.push_back(server);
deadbeef0a6c4ca2015-10-06 11:38:28 -0700794
zhihuang9763d562016-08-05 11:14:50 -0700795 rtc::scoped_refptr<PeerConnectionInterface> pc;
hbosd7973cc2016-05-27 06:08:53 -0700796 pc = pc_factory_->CreatePeerConnection(config, nullptr, nullptr, nullptr,
797 &observer_);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800798 EXPECT_EQ(nullptr, pc);
deadbeef0a6c4ca2015-10-06 11:38:28 -0700799 }
800
Steve Anton038834f2017-07-14 15:59:59 -0700801 void CreatePeerConnectionExpectFail(
802 PeerConnectionInterface::RTCConfiguration config) {
803 PeerConnectionInterface::IceServer server;
804 server.uri = kTurnIceServerUri;
805 server.password = kTurnPassword;
806 config.servers.push_back(server);
807 rtc::scoped_refptr<PeerConnectionInterface> pc =
808 pc_factory_->CreatePeerConnection(config, nullptr, nullptr, &observer_);
809 EXPECT_EQ(nullptr, pc);
810 }
811
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000812 void CreatePeerConnectionWithDifferentConfigurations() {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700813 CreatePeerConnectionWithIceServer(kStunAddressOnly, "");
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800814 EXPECT_EQ(1u, port_allocator_->stun_servers().size());
815 EXPECT_EQ(0u, port_allocator_->turn_servers().size());
816 EXPECT_EQ("address", port_allocator_->stun_servers().begin()->hostname());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000817 EXPECT_EQ(kDefaultStunPort,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800818 port_allocator_->stun_servers().begin()->port());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000819
deadbeef0a6c4ca2015-10-06 11:38:28 -0700820 CreatePeerConnectionExpectFail(kStunInvalidPort);
821 CreatePeerConnectionExpectFail(kStunAddressPortAndMore1);
822 CreatePeerConnectionExpectFail(kStunAddressPortAndMore2);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000823
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700824 CreatePeerConnectionWithIceServer(kTurnIceServerUri, kTurnPassword);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800825 EXPECT_EQ(0u, port_allocator_->stun_servers().size());
826 EXPECT_EQ(1u, port_allocator_->turn_servers().size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000827 EXPECT_EQ(kTurnUsername,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800828 port_allocator_->turn_servers()[0].credentials.username);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000829 EXPECT_EQ(kTurnPassword,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800830 port_allocator_->turn_servers()[0].credentials.password);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000831 EXPECT_EQ(kTurnHostname,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800832 port_allocator_->turn_servers()[0].ports[0].address.hostname());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000833 }
834
835 void ReleasePeerConnection() {
836 pc_ = NULL;
837 observer_.SetPeerConnectionInterface(NULL);
838 }
839
deadbeefab9b2d12015-10-14 11:33:11 -0700840 void AddVideoStream(const std::string& label) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000841 // Create a local stream.
zhihuang9763d562016-08-05 11:14:50 -0700842 rtc::scoped_refptr<MediaStreamInterface> stream(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000843 pc_factory_->CreateLocalMediaStream(label));
zhihuang9763d562016-08-05 11:14:50 -0700844 rtc::scoped_refptr<VideoTrackSourceInterface> video_source(
deadbeef112b2e92017-02-10 20:13:37 -0800845 pc_factory_->CreateVideoSource(std::unique_ptr<cricket::VideoCapturer>(
846 new cricket::FakeVideoCapturer()),
847 NULL));
zhihuang9763d562016-08-05 11:14:50 -0700848 rtc::scoped_refptr<VideoTrackInterface> video_track(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000849 pc_factory_->CreateVideoTrack(label + "v0", video_source));
850 stream->AddTrack(video_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 AddVoiceStream(const std::string& label) {
857 // Create a local stream.
zhihuang9763d562016-08-05 11:14:50 -0700858 rtc::scoped_refptr<MediaStreamInterface> stream(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000859 pc_factory_->CreateLocalMediaStream(label));
zhihuang9763d562016-08-05 11:14:50 -0700860 rtc::scoped_refptr<AudioTrackInterface> audio_track(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000861 pc_factory_->CreateAudioTrack(label + "a0", NULL));
862 stream->AddTrack(audio_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +0000863 EXPECT_TRUE(pc_->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000864 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
865 observer_.renegotiation_needed_ = false;
866 }
867
868 void AddAudioVideoStream(const std::string& stream_label,
869 const std::string& audio_track_label,
870 const std::string& video_track_label) {
871 // Create a local stream.
zhihuang9763d562016-08-05 11:14:50 -0700872 rtc::scoped_refptr<MediaStreamInterface> stream(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000873 pc_factory_->CreateLocalMediaStream(stream_label));
zhihuang9763d562016-08-05 11:14:50 -0700874 rtc::scoped_refptr<AudioTrackInterface> audio_track(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000875 pc_factory_->CreateAudioTrack(
876 audio_track_label, static_cast<AudioSourceInterface*>(NULL)));
877 stream->AddTrack(audio_track.get());
zhihuang9763d562016-08-05 11:14:50 -0700878 rtc::scoped_refptr<VideoTrackInterface> video_track(
nisseaf510af2016-03-21 08:20:42 -0700879 pc_factory_->CreateVideoTrack(
deadbeef112b2e92017-02-10 20:13:37 -0800880 video_track_label, pc_factory_->CreateVideoSource(
881 std::unique_ptr<cricket::VideoCapturer>(
882 new cricket::FakeVideoCapturer()))));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000883 stream->AddTrack(video_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +0000884 EXPECT_TRUE(pc_->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000885 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
886 observer_.renegotiation_needed_ = false;
887 }
888
kwibergd1fe2812016-04-27 06:47:29 -0700889 bool DoCreateOfferAnswer(std::unique_ptr<SessionDescriptionInterface>* desc,
deadbeefc80741f2015-10-22 13:14:45 -0700890 bool offer,
891 MediaConstraintsInterface* constraints) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000892 rtc::scoped_refptr<MockCreateSessionDescriptionObserver>
893 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000894 MockCreateSessionDescriptionObserver>());
895 if (offer) {
deadbeefc80741f2015-10-22 13:14:45 -0700896 pc_->CreateOffer(observer, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000897 } else {
deadbeefc80741f2015-10-22 13:14:45 -0700898 pc_->CreateAnswer(observer, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000899 }
900 EXPECT_EQ_WAIT(true, observer->called(), kTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -0700901 *desc = observer->MoveDescription();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000902 return observer->result();
903 }
904
kwibergd1fe2812016-04-27 06:47:29 -0700905 bool DoCreateOffer(std::unique_ptr<SessionDescriptionInterface>* desc,
deadbeefc80741f2015-10-22 13:14:45 -0700906 MediaConstraintsInterface* constraints) {
907 return DoCreateOfferAnswer(desc, true, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000908 }
909
kwibergd1fe2812016-04-27 06:47:29 -0700910 bool DoCreateAnswer(std::unique_ptr<SessionDescriptionInterface>* desc,
deadbeefc80741f2015-10-22 13:14:45 -0700911 MediaConstraintsInterface* constraints) {
912 return DoCreateOfferAnswer(desc, false, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000913 }
914
915 bool DoSetSessionDescription(SessionDescriptionInterface* desc, bool local) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000916 rtc::scoped_refptr<MockSetSessionDescriptionObserver>
917 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000918 MockSetSessionDescriptionObserver>());
919 if (local) {
920 pc_->SetLocalDescription(observer, desc);
921 } else {
922 pc_->SetRemoteDescription(observer, desc);
923 }
zhihuang29ff8442016-07-27 11:07:25 -0700924 if (pc_->signaling_state() != PeerConnectionInterface::kClosed) {
925 EXPECT_EQ_WAIT(true, observer->called(), kTimeout);
926 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000927 return observer->result();
928 }
929
930 bool DoSetLocalDescription(SessionDescriptionInterface* desc) {
931 return DoSetSessionDescription(desc, true);
932 }
933
934 bool DoSetRemoteDescription(SessionDescriptionInterface* desc) {
935 return DoSetSessionDescription(desc, false);
936 }
937
938 // Calls PeerConnection::GetStats and check the return value.
939 // It does not verify the values in the StatReports since a RTCP packet might
940 // be required.
941 bool DoGetStats(MediaStreamTrackInterface* track) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000942 rtc::scoped_refptr<MockStatsObserver> observer(
943 new rtc::RefCountedObject<MockStatsObserver>());
jiayl@webrtc.orgdb41b4d2014-03-03 21:30:06 +0000944 if (!pc_->GetStats(
945 observer, track, PeerConnectionInterface::kStatsOutputLevelStandard))
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000946 return false;
947 EXPECT_TRUE_WAIT(observer->called(), kTimeout);
948 return observer->called();
949 }
950
951 void InitiateCall() {
deadbeef293e9262017-01-11 12:28:30 -0800952 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000953 // Create a local stream with audio&video tracks.
954 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
955 CreateOfferReceiveAnswer();
956 }
957
958 // Verify that RTP Header extensions has been negotiated for audio and video.
959 void VerifyRemoteRtpHeaderExtensions() {
960 const cricket::MediaContentDescription* desc =
961 cricket::GetFirstAudioContentDescription(
962 pc_->remote_description()->description());
963 ASSERT_TRUE(desc != NULL);
964 EXPECT_GT(desc->rtp_header_extensions().size(), 0u);
965
966 desc = cricket::GetFirstVideoContentDescription(
967 pc_->remote_description()->description());
968 ASSERT_TRUE(desc != NULL);
969 EXPECT_GT(desc->rtp_header_extensions().size(), 0u);
970 }
971
972 void CreateOfferAsRemoteDescription() {
kwibergd1fe2812016-04-27 06:47:29 -0700973 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -0700974 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000975 std::string sdp;
976 EXPECT_TRUE(offer->ToString(&sdp));
977 SessionDescriptionInterface* remote_offer =
978 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
979 sdp, NULL);
980 EXPECT_TRUE(DoSetRemoteDescription(remote_offer));
981 EXPECT_EQ(PeerConnectionInterface::kHaveRemoteOffer, observer_.state_);
982 }
983
deadbeefab9b2d12015-10-14 11:33:11 -0700984 void CreateAndSetRemoteOffer(const std::string& sdp) {
985 SessionDescriptionInterface* remote_offer =
986 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
987 sdp, nullptr);
988 EXPECT_TRUE(DoSetRemoteDescription(remote_offer));
989 EXPECT_EQ(PeerConnectionInterface::kHaveRemoteOffer, observer_.state_);
990 }
991
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000992 void CreateAnswerAsLocalDescription() {
kwibergd1fe2812016-04-27 06:47:29 -0700993 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -0700994 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000995
996 // TODO(perkj): Currently SetLocalDescription fails if any parameters in an
997 // audio codec change, even if the parameter has nothing to do with
998 // receiving. Not all parameters are serialized to SDP.
999 // Since CreatePrAnswerAsLocalDescription serialize/deserialize
1000 // the SessionDescription, it is necessary to do that here to in order to
1001 // get ReceiveOfferCreatePrAnswerAndAnswer and RenegotiateAudioOnly to pass.
1002 // https://code.google.com/p/webrtc/issues/detail?id=1356
1003 std::string sdp;
1004 EXPECT_TRUE(answer->ToString(&sdp));
1005 SessionDescriptionInterface* new_answer =
1006 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
1007 sdp, NULL);
1008 EXPECT_TRUE(DoSetLocalDescription(new_answer));
1009 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
1010 }
1011
1012 void CreatePrAnswerAsLocalDescription() {
kwibergd1fe2812016-04-27 06:47:29 -07001013 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -07001014 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001015
1016 std::string sdp;
1017 EXPECT_TRUE(answer->ToString(&sdp));
1018 SessionDescriptionInterface* pr_answer =
1019 webrtc::CreateSessionDescription(SessionDescriptionInterface::kPrAnswer,
1020 sdp, NULL);
1021 EXPECT_TRUE(DoSetLocalDescription(pr_answer));
1022 EXPECT_EQ(PeerConnectionInterface::kHaveLocalPrAnswer, observer_.state_);
1023 }
1024
1025 void CreateOfferReceiveAnswer() {
1026 CreateOfferAsLocalDescription();
1027 std::string sdp;
1028 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
1029 CreateAnswerAsRemoteDescription(sdp);
1030 }
1031
1032 void CreateOfferAsLocalDescription() {
kwibergd1fe2812016-04-27 06:47:29 -07001033 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001034 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001035 // TODO(perkj): Currently SetLocalDescription fails if any parameters in an
1036 // audio codec change, even if the parameter has nothing to do with
1037 // receiving. Not all parameters are serialized to SDP.
1038 // Since CreatePrAnswerAsLocalDescription serialize/deserialize
1039 // the SessionDescription, it is necessary to do that here to in order to
1040 // get ReceiveOfferCreatePrAnswerAndAnswer and RenegotiateAudioOnly to pass.
1041 // https://code.google.com/p/webrtc/issues/detail?id=1356
1042 std::string sdp;
1043 EXPECT_TRUE(offer->ToString(&sdp));
1044 SessionDescriptionInterface* new_offer =
1045 webrtc::CreateSessionDescription(
1046 SessionDescriptionInterface::kOffer,
1047 sdp, NULL);
1048
1049 EXPECT_TRUE(DoSetLocalDescription(new_offer));
1050 EXPECT_EQ(PeerConnectionInterface::kHaveLocalOffer, observer_.state_);
mallinath@webrtc.org68cbd012014-01-22 00:16:46 +00001051 // Wait for the ice_complete message, so that SDP will have candidates.
1052 EXPECT_TRUE_WAIT(observer_.ice_complete_, kTimeout);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001053 }
1054
deadbeefab9b2d12015-10-14 11:33:11 -07001055 void CreateAnswerAsRemoteDescription(const std::string& sdp) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001056 webrtc::JsepSessionDescription* answer = new webrtc::JsepSessionDescription(
1057 SessionDescriptionInterface::kAnswer);
deadbeefab9b2d12015-10-14 11:33:11 -07001058 EXPECT_TRUE(answer->Initialize(sdp, NULL));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001059 EXPECT_TRUE(DoSetRemoteDescription(answer));
1060 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
1061 }
1062
deadbeefab9b2d12015-10-14 11:33:11 -07001063 void CreatePrAnswerAndAnswerAsRemoteDescription(const std::string& sdp) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001064 webrtc::JsepSessionDescription* pr_answer =
1065 new webrtc::JsepSessionDescription(
1066 SessionDescriptionInterface::kPrAnswer);
deadbeefab9b2d12015-10-14 11:33:11 -07001067 EXPECT_TRUE(pr_answer->Initialize(sdp, NULL));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001068 EXPECT_TRUE(DoSetRemoteDescription(pr_answer));
1069 EXPECT_EQ(PeerConnectionInterface::kHaveRemotePrAnswer, observer_.state_);
1070 webrtc::JsepSessionDescription* answer =
1071 new webrtc::JsepSessionDescription(
1072 SessionDescriptionInterface::kAnswer);
deadbeefab9b2d12015-10-14 11:33:11 -07001073 EXPECT_TRUE(answer->Initialize(sdp, NULL));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001074 EXPECT_TRUE(DoSetRemoteDescription(answer));
1075 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
1076 }
1077
1078 // Help function used for waiting until a the last signaled remote stream has
1079 // the same label as |stream_label|. In a few of the tests in this file we
1080 // answer with the same session description as we offer and thus we can
1081 // check if OnAddStream have been called with the same stream as we offer to
1082 // send.
1083 void WaitAndVerifyOnAddStream(const std::string& stream_label) {
1084 EXPECT_EQ_WAIT(stream_label, observer_.GetLastAddedStreamLabel(), kTimeout);
1085 }
1086
1087 // Creates an offer and applies it as a local session description.
1088 // Creates an answer with the same SDP an the offer but removes all lines
1089 // that start with a:ssrc"
1090 void CreateOfferReceiveAnswerWithoutSsrc() {
1091 CreateOfferAsLocalDescription();
1092 std::string sdp;
1093 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
1094 SetSsrcToZero(&sdp);
1095 CreateAnswerAsRemoteDescription(sdp);
1096 }
1097
deadbeefab9b2d12015-10-14 11:33:11 -07001098 // This function creates a MediaStream with label kStreams[0] and
1099 // |number_of_audio_tracks| and |number_of_video_tracks| tracks and the
1100 // corresponding SessionDescriptionInterface. The SessionDescriptionInterface
kwiberg2bbff992016-03-16 11:03:04 -07001101 // is returned and the MediaStream is stored in
deadbeefab9b2d12015-10-14 11:33:11 -07001102 // |reference_collection_|
kwibergd1fe2812016-04-27 06:47:29 -07001103 std::unique_ptr<SessionDescriptionInterface>
kwiberg2bbff992016-03-16 11:03:04 -07001104 CreateSessionDescriptionAndReference(size_t number_of_audio_tracks,
1105 size_t number_of_video_tracks) {
1106 EXPECT_LE(number_of_audio_tracks, 2u);
1107 EXPECT_LE(number_of_video_tracks, 2u);
deadbeefab9b2d12015-10-14 11:33:11 -07001108
1109 reference_collection_ = StreamCollection::Create();
1110 std::string sdp_ms1 = std::string(kSdpStringInit);
1111
1112 std::string mediastream_label = kStreams[0];
1113
1114 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream(
1115 webrtc::MediaStream::Create(mediastream_label));
1116 reference_collection_->AddStream(stream);
1117
1118 if (number_of_audio_tracks > 0) {
1119 sdp_ms1 += std::string(kSdpStringAudio);
1120 sdp_ms1 += std::string(kSdpStringMs1Audio0);
1121 AddAudioTrack(kAudioTracks[0], stream);
1122 }
1123 if (number_of_audio_tracks > 1) {
1124 sdp_ms1 += kSdpStringMs1Audio1;
1125 AddAudioTrack(kAudioTracks[1], stream);
1126 }
1127
1128 if (number_of_video_tracks > 0) {
1129 sdp_ms1 += std::string(kSdpStringVideo);
1130 sdp_ms1 += std::string(kSdpStringMs1Video0);
1131 AddVideoTrack(kVideoTracks[0], stream);
1132 }
1133 if (number_of_video_tracks > 1) {
1134 sdp_ms1 += kSdpStringMs1Video1;
1135 AddVideoTrack(kVideoTracks[1], stream);
1136 }
1137
kwibergd1fe2812016-04-27 06:47:29 -07001138 return std::unique_ptr<SessionDescriptionInterface>(
kwiberg2bbff992016-03-16 11:03:04 -07001139 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
1140 sdp_ms1, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07001141 }
1142
1143 void AddAudioTrack(const std::string& track_id,
1144 MediaStreamInterface* stream) {
1145 rtc::scoped_refptr<webrtc::AudioTrackInterface> audio_track(
1146 webrtc::AudioTrack::Create(track_id, nullptr));
1147 ASSERT_TRUE(stream->AddTrack(audio_track));
1148 }
1149
1150 void AddVideoTrack(const std::string& track_id,
1151 MediaStreamInterface* stream) {
1152 rtc::scoped_refptr<webrtc::VideoTrackInterface> video_track(
nisseaf510af2016-03-21 08:20:42 -07001153 webrtc::VideoTrack::Create(track_id,
perkj773be362017-07-31 23:22:01 -07001154 webrtc::FakeVideoTrackSource::Create(),
1155 rtc::Thread::Current()));
deadbeefab9b2d12015-10-14 11:33:11 -07001156 ASSERT_TRUE(stream->AddTrack(video_track));
1157 }
1158
kwibergfd8be342016-05-14 19:44:11 -07001159 std::unique_ptr<SessionDescriptionInterface> CreateOfferWithOneAudioStream() {
deadbeef293e9262017-01-11 12:28:30 -08001160 CreatePeerConnectionWithoutDtls();
zhihuang8f65cdf2016-05-06 18:40:30 -07001161 AddVoiceStream(kStreamLabel1);
kwibergfd8be342016-05-14 19:44:11 -07001162 std::unique_ptr<SessionDescriptionInterface> offer;
zhihuang8f65cdf2016-05-06 18:40:30 -07001163 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
1164 return offer;
1165 }
1166
kwibergfd8be342016-05-14 19:44:11 -07001167 std::unique_ptr<SessionDescriptionInterface>
zhihuang8f65cdf2016-05-06 18:40:30 -07001168 CreateAnswerWithOneAudioStream() {
kwibergfd8be342016-05-14 19:44:11 -07001169 std::unique_ptr<SessionDescriptionInterface> offer =
zhihuang8f65cdf2016-05-06 18:40:30 -07001170 CreateOfferWithOneAudioStream();
1171 EXPECT_TRUE(DoSetRemoteDescription(offer.release()));
kwibergfd8be342016-05-14 19:44:11 -07001172 std::unique_ptr<SessionDescriptionInterface> answer;
zhihuang8f65cdf2016-05-06 18:40:30 -07001173 EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
1174 return answer;
1175 }
1176
1177 const std::string& GetFirstAudioStreamCname(
1178 const SessionDescriptionInterface* desc) {
1179 const cricket::ContentInfo* audio_content =
1180 cricket::GetFirstAudioContent(desc->description());
1181 const cricket::AudioContentDescription* audio_desc =
1182 static_cast<const cricket::AudioContentDescription*>(
1183 audio_content->description);
1184 return audio_desc->streams()[0].cname;
1185 }
1186
zhihuanga77e6bb2017-08-14 18:17:48 -07001187 std::unique_ptr<SessionDescriptionInterface> CreateOfferWithOptions(
1188 const RTCOfferAnswerOptions& offer_answer_options) {
1189 RTC_DCHECK(pc_);
1190 rtc::scoped_refptr<MockCreateSessionDescriptionObserver> observer(
1191 new rtc::RefCountedObject<MockCreateSessionDescriptionObserver>());
1192 pc_->CreateOffer(observer, offer_answer_options);
1193 EXPECT_EQ_WAIT(true, observer->called(), kTimeout);
1194 return observer->MoveDescription();
1195 }
1196
1197 void CreateOfferWithOptionsAsRemoteDescription(
1198 std::unique_ptr<SessionDescriptionInterface>* desc,
1199 const RTCOfferAnswerOptions& offer_answer_options) {
1200 *desc = CreateOfferWithOptions(offer_answer_options);
1201 ASSERT_TRUE(desc != nullptr);
1202 std::string sdp;
1203 EXPECT_TRUE((*desc)->ToString(&sdp));
1204 SessionDescriptionInterface* remote_offer =
1205 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
1206 sdp, NULL);
1207 EXPECT_TRUE(DoSetRemoteDescription(remote_offer));
1208 EXPECT_EQ(PeerConnectionInterface::kHaveRemoteOffer, observer_.state_);
1209 }
1210
1211 void CreateOfferWithOptionsAsLocalDescription(
1212 std::unique_ptr<SessionDescriptionInterface>* desc,
1213 const RTCOfferAnswerOptions& offer_answer_options) {
1214 *desc = CreateOfferWithOptions(offer_answer_options);
1215 ASSERT_TRUE(desc != nullptr);
1216 std::string sdp;
1217 EXPECT_TRUE((*desc)->ToString(&sdp));
1218 SessionDescriptionInterface* new_offer = webrtc::CreateSessionDescription(
1219 SessionDescriptionInterface::kOffer, sdp, NULL);
1220
1221 EXPECT_TRUE(DoSetLocalDescription(new_offer));
1222 EXPECT_EQ(PeerConnectionInterface::kHaveLocalOffer, observer_.state_);
1223 }
1224
1225 bool HasCNCodecs(const cricket::ContentInfo* content) {
1226 const cricket::ContentDescription* description = content->description;
1227 RTC_DCHECK(description);
1228 const cricket::AudioContentDescription* audio_content_desc =
1229 static_cast<const cricket::AudioContentDescription*>(description);
1230 RTC_DCHECK(audio_content_desc);
1231 for (size_t i = 0; i < audio_content_desc->codecs().size(); ++i) {
1232 if (audio_content_desc->codecs()[i].name == "CN")
1233 return true;
1234 }
1235 return false;
1236 }
1237
deadbeef9a6f4d42017-05-15 19:43:33 -07001238 std::unique_ptr<rtc::VirtualSocketServer> vss_;
1239 rtc::AutoSocketServerThread main_;
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08001240 cricket::FakePortAllocator* port_allocator_ = nullptr;
deadbeef8662f942017-01-20 21:20:51 -08001241 FakeRTCCertificateGenerator* fake_certificate_generator_ = nullptr;
zhihuang9763d562016-08-05 11:14:50 -07001242 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface> pc_factory_;
1243 rtc::scoped_refptr<PeerConnectionFactoryForTest> pc_factory_for_test_;
1244 rtc::scoped_refptr<PeerConnectionInterface> pc_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001245 MockPeerConnectionObserver observer_;
deadbeefab9b2d12015-10-14 11:33:11 -07001246 rtc::scoped_refptr<StreamCollection> reference_collection_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001247};
1248
zhihuang29ff8442016-07-27 11:07:25 -07001249// Test that no callbacks on the PeerConnectionObserver are called after the
1250// PeerConnection is closed.
1251TEST_F(PeerConnectionInterfaceTest, CloseAndTestCallbackFunctions) {
zhihuang9763d562016-08-05 11:14:50 -07001252 rtc::scoped_refptr<PeerConnectionInterface> pc(
zhihuang29ff8442016-07-27 11:07:25 -07001253 pc_factory_for_test_->CreatePeerConnection(
1254 PeerConnectionInterface::RTCConfiguration(), nullptr, nullptr,
1255 nullptr, &observer_));
1256 observer_.SetPeerConnectionInterface(pc.get());
1257 pc->Close();
1258
1259 // No callbacks is expected to be called.
zhihuang81c3a032016-11-17 12:06:24 -08001260 observer_.callback_triggered_ = false;
zhihuang29ff8442016-07-27 11:07:25 -07001261 std::vector<cricket::Candidate> candidates;
1262 pc_factory_for_test_->transport_controller->SignalGatheringState(
1263 cricket::IceGatheringState{});
1264 pc_factory_for_test_->transport_controller->SignalCandidatesGathered(
1265 "", candidates);
1266 pc_factory_for_test_->transport_controller->SignalConnectionState(
1267 cricket::IceConnectionState{});
1268 pc_factory_for_test_->transport_controller->SignalCandidatesRemoved(
1269 candidates);
1270 pc_factory_for_test_->transport_controller->SignalReceiving(false);
zhihuang81c3a032016-11-17 12:06:24 -08001271 EXPECT_FALSE(observer_.callback_triggered_);
zhihuang29ff8442016-07-27 11:07:25 -07001272}
1273
zhihuang8f65cdf2016-05-06 18:40:30 -07001274// Generate different CNAMEs when PeerConnections are created.
1275// The CNAMEs are expected to be generated randomly. It is possible
1276// that the test fails, though the possibility is very low.
1277TEST_F(PeerConnectionInterfaceTest, CnameGenerationInOffer) {
kwibergfd8be342016-05-14 19:44:11 -07001278 std::unique_ptr<SessionDescriptionInterface> offer1 =
zhihuang8f65cdf2016-05-06 18:40:30 -07001279 CreateOfferWithOneAudioStream();
kwibergfd8be342016-05-14 19:44:11 -07001280 std::unique_ptr<SessionDescriptionInterface> offer2 =
zhihuang8f65cdf2016-05-06 18:40:30 -07001281 CreateOfferWithOneAudioStream();
1282 EXPECT_NE(GetFirstAudioStreamCname(offer1.get()),
1283 GetFirstAudioStreamCname(offer2.get()));
1284}
1285
1286TEST_F(PeerConnectionInterfaceTest, CnameGenerationInAnswer) {
kwibergfd8be342016-05-14 19:44:11 -07001287 std::unique_ptr<SessionDescriptionInterface> answer1 =
zhihuang8f65cdf2016-05-06 18:40:30 -07001288 CreateAnswerWithOneAudioStream();
kwibergfd8be342016-05-14 19:44:11 -07001289 std::unique_ptr<SessionDescriptionInterface> answer2 =
zhihuang8f65cdf2016-05-06 18:40:30 -07001290 CreateAnswerWithOneAudioStream();
1291 EXPECT_NE(GetFirstAudioStreamCname(answer1.get()),
1292 GetFirstAudioStreamCname(answer2.get()));
1293}
1294
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001295TEST_F(PeerConnectionInterfaceTest,
1296 CreatePeerConnectionWithDifferentConfigurations) {
1297 CreatePeerConnectionWithDifferentConfigurations();
1298}
1299
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001300TEST_F(PeerConnectionInterfaceTest,
1301 CreatePeerConnectionWithDifferentIceTransportsTypes) {
1302 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kNone);
1303 EXPECT_EQ(cricket::CF_NONE, port_allocator_->candidate_filter());
1304 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kRelay);
1305 EXPECT_EQ(cricket::CF_RELAY, port_allocator_->candidate_filter());
1306 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kNoHost);
1307 EXPECT_EQ(cricket::CF_ALL & ~cricket::CF_HOST,
1308 port_allocator_->candidate_filter());
1309 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kAll);
1310 EXPECT_EQ(cricket::CF_ALL, port_allocator_->candidate_filter());
1311}
1312
1313// Test that when a PeerConnection is created with a nonzero candidate pool
1314// size, the pooled PortAllocatorSession is created with all the attributes
1315// in the RTCConfiguration.
1316TEST_F(PeerConnectionInterfaceTest, CreatePeerConnectionWithPooledCandidates) {
1317 PeerConnectionInterface::RTCConfiguration config;
1318 PeerConnectionInterface::IceServer server;
1319 server.uri = kStunAddressOnly;
1320 config.servers.push_back(server);
1321 config.type = PeerConnectionInterface::kRelay;
1322 config.disable_ipv6 = true;
1323 config.tcp_candidate_policy =
1324 PeerConnectionInterface::kTcpCandidatePolicyDisabled;
honghaiz60347052016-05-31 18:29:12 -07001325 config.candidate_network_policy =
1326 PeerConnectionInterface::kCandidateNetworkPolicyLowCost;
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001327 config.ice_candidate_pool_size = 1;
1328 CreatePeerConnection(config, nullptr);
1329
1330 const cricket::FakePortAllocatorSession* session =
1331 static_cast<const cricket::FakePortAllocatorSession*>(
1332 port_allocator_->GetPooledSession());
1333 ASSERT_NE(nullptr, session);
1334 EXPECT_EQ(1UL, session->stun_servers().size());
1335 EXPECT_EQ(0U, session->flags() & cricket::PORTALLOCATOR_ENABLE_IPV6);
1336 EXPECT_LT(0U, session->flags() & cricket::PORTALLOCATOR_DISABLE_TCP);
honghaiz60347052016-05-31 18:29:12 -07001337 EXPECT_LT(0U,
1338 session->flags() & cricket::PORTALLOCATOR_DISABLE_COSTLY_NETWORKS);
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001339}
1340
deadbeefd21eab32017-07-26 16:50:11 -07001341// Test that network-related RTCConfiguration members are applied to the
1342// PortAllocator when CreatePeerConnection is called. Specifically:
1343// - disable_ipv6_on_wifi
1344// - max_ipv6_networks
1345// - tcp_candidate_policy
1346// - candidate_network_policy
1347// - prune_turn_ports
1348//
1349// Note that the candidate filter (RTCConfiguration::type) is already tested
1350// above.
1351TEST_F(PeerConnectionInterfaceTest,
1352 CreatePeerConnectionAppliesNetworkConfigToPortAllocator) {
1353 // Create fake port allocator.
1354 std::unique_ptr<cricket::FakePortAllocator> port_allocator(
1355 new cricket::FakePortAllocator(rtc::Thread::Current(), nullptr));
1356 cricket::FakePortAllocator* raw_port_allocator = port_allocator.get();
1357
1358 // Create RTCConfiguration with some network-related fields relevant to
1359 // PortAllocator populated.
1360 PeerConnectionInterface::RTCConfiguration config;
1361 config.disable_ipv6_on_wifi = true;
1362 config.max_ipv6_networks = 10;
1363 config.tcp_candidate_policy =
1364 PeerConnectionInterface::kTcpCandidatePolicyDisabled;
1365 config.candidate_network_policy =
1366 PeerConnectionInterface::kCandidateNetworkPolicyLowCost;
1367 config.prune_turn_ports = true;
1368
1369 // Create the PC factory and PC with the above config.
1370 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface> pc_factory(
1371 webrtc::CreatePeerConnectionFactory(
1372 rtc::Thread::Current(), rtc::Thread::Current(),
1373 rtc::Thread::Current(), nullptr, nullptr, nullptr));
1374 rtc::scoped_refptr<PeerConnectionInterface> pc(
1375 pc_factory->CreatePeerConnection(
1376 config, nullptr, std::move(port_allocator), nullptr, &observer_));
1377
1378 // Now validate that the config fields set above were applied to the
1379 // PortAllocator, as flags or otherwise.
1380 EXPECT_FALSE(raw_port_allocator->flags() &
1381 cricket::PORTALLOCATOR_ENABLE_IPV6_ON_WIFI);
1382 EXPECT_EQ(10, raw_port_allocator->max_ipv6_networks());
1383 EXPECT_TRUE(raw_port_allocator->flags() & cricket::PORTALLOCATOR_DISABLE_TCP);
1384 EXPECT_TRUE(raw_port_allocator->flags() &
1385 cricket::PORTALLOCATOR_DISABLE_COSTLY_NETWORKS);
1386 EXPECT_TRUE(raw_port_allocator->prune_turn_ports());
1387}
1388
Taylor Brandstetterf8e65772016-06-27 17:20:15 -07001389// Test that the PeerConnection initializes the port allocator passed into it,
1390// and on the correct thread.
1391TEST_F(PeerConnectionInterfaceTest,
deadbeefd21eab32017-07-26 16:50:11 -07001392 CreatePeerConnectionInitializesPortAllocatorOnNetworkThread) {
tommie7251592017-07-14 14:44:46 -07001393 std::unique_ptr<rtc::Thread> network_thread(
1394 rtc::Thread::CreateWithSocketServer());
1395 network_thread->Start();
Taylor Brandstetterf8e65772016-06-27 17:20:15 -07001396 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface> pc_factory(
1397 webrtc::CreatePeerConnectionFactory(
tommie7251592017-07-14 14:44:46 -07001398 network_thread.get(), rtc::Thread::Current(), rtc::Thread::Current(),
Taylor Brandstetterf8e65772016-06-27 17:20:15 -07001399 nullptr, nullptr, nullptr));
1400 std::unique_ptr<cricket::FakePortAllocator> port_allocator(
tommie7251592017-07-14 14:44:46 -07001401 new cricket::FakePortAllocator(network_thread.get(), nullptr));
Taylor Brandstetterf8e65772016-06-27 17:20:15 -07001402 cricket::FakePortAllocator* raw_port_allocator = port_allocator.get();
1403 PeerConnectionInterface::RTCConfiguration config;
1404 rtc::scoped_refptr<PeerConnectionInterface> pc(
1405 pc_factory->CreatePeerConnection(
1406 config, nullptr, std::move(port_allocator), nullptr, &observer_));
1407 // FakePortAllocator RTC_CHECKs that it's initialized on the right thread,
1408 // so all we have to do here is check that it's initialized.
1409 EXPECT_TRUE(raw_port_allocator->initialized());
1410}
1411
deadbeef46c73892016-11-16 19:42:04 -08001412// Check that GetConfiguration returns the configuration the PeerConnection was
1413// constructed with, before SetConfiguration is called.
1414TEST_F(PeerConnectionInterfaceTest, GetConfigurationAfterCreatePeerConnection) {
1415 PeerConnectionInterface::RTCConfiguration config;
1416 config.type = PeerConnectionInterface::kRelay;
1417 CreatePeerConnection(config, nullptr);
1418
1419 PeerConnectionInterface::RTCConfiguration returned_config =
1420 pc_->GetConfiguration();
1421 EXPECT_EQ(PeerConnectionInterface::kRelay, returned_config.type);
1422}
1423
1424// Check that GetConfiguration returns the last configuration passed into
1425// SetConfiguration.
1426TEST_F(PeerConnectionInterfaceTest, GetConfigurationAfterSetConfiguration) {
1427 CreatePeerConnection();
1428
1429 PeerConnectionInterface::RTCConfiguration config;
1430 config.type = PeerConnectionInterface::kRelay;
1431 EXPECT_TRUE(pc_->SetConfiguration(config));
1432
1433 PeerConnectionInterface::RTCConfiguration returned_config =
1434 pc_->GetConfiguration();
1435 EXPECT_EQ(PeerConnectionInterface::kRelay, returned_config.type);
1436}
1437
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001438TEST_F(PeerConnectionInterfaceTest, AddStreams) {
deadbeef293e9262017-01-11 12:28:30 -08001439 CreatePeerConnectionWithoutDtls();
deadbeefab9b2d12015-10-14 11:33:11 -07001440 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001441 AddVoiceStream(kStreamLabel2);
1442 ASSERT_EQ(2u, pc_->local_streams()->count());
1443
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001444 // Test we can add multiple local streams to one peerconnection.
zhihuang9763d562016-08-05 11:14:50 -07001445 rtc::scoped_refptr<MediaStreamInterface> stream(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001446 pc_factory_->CreateLocalMediaStream(kStreamLabel3));
zhihuang9763d562016-08-05 11:14:50 -07001447 rtc::scoped_refptr<AudioTrackInterface> audio_track(
1448 pc_factory_->CreateAudioTrack(kStreamLabel3,
1449 static_cast<AudioSourceInterface*>(NULL)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001450 stream->AddTrack(audio_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +00001451 EXPECT_TRUE(pc_->AddStream(stream));
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001452 EXPECT_EQ(3u, pc_->local_streams()->count());
1453
1454 // Remove the third stream.
1455 pc_->RemoveStream(pc_->local_streams()->at(2));
1456 EXPECT_EQ(2u, pc_->local_streams()->count());
1457
1458 // Remove the second stream.
1459 pc_->RemoveStream(pc_->local_streams()->at(1));
1460 EXPECT_EQ(1u, pc_->local_streams()->count());
1461
1462 // Remove the first stream.
1463 pc_->RemoveStream(pc_->local_streams()->at(0));
1464 EXPECT_EQ(0u, pc_->local_streams()->count());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001465}
1466
deadbeefab9b2d12015-10-14 11:33:11 -07001467// Test that the created offer includes streams we added.
1468TEST_F(PeerConnectionInterfaceTest, AddedStreamsPresentInOffer) {
deadbeef293e9262017-01-11 12:28:30 -08001469 CreatePeerConnectionWithoutDtls();
deadbeefab9b2d12015-10-14 11:33:11 -07001470 AddAudioVideoStream(kStreamLabel1, "audio_track", "video_track");
kwibergd1fe2812016-04-27 06:47:29 -07001471 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001472 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07001473
1474 const cricket::ContentInfo* audio_content =
1475 cricket::GetFirstAudioContent(offer->description());
1476 const cricket::AudioContentDescription* audio_desc =
1477 static_cast<const cricket::AudioContentDescription*>(
1478 audio_content->description);
1479 EXPECT_TRUE(
1480 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
1481
1482 const cricket::ContentInfo* video_content =
1483 cricket::GetFirstVideoContent(offer->description());
1484 const cricket::VideoContentDescription* video_desc =
1485 static_cast<const cricket::VideoContentDescription*>(
1486 video_content->description);
1487 EXPECT_TRUE(
1488 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1489
1490 // Add another stream and ensure the offer includes both the old and new
1491 // streams.
1492 AddAudioVideoStream(kStreamLabel2, "audio_track2", "video_track2");
kwiberg2bbff992016-03-16 11:03:04 -07001493 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07001494
1495 audio_content = cricket::GetFirstAudioContent(offer->description());
1496 audio_desc = static_cast<const cricket::AudioContentDescription*>(
1497 audio_content->description);
1498 EXPECT_TRUE(
1499 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
1500 EXPECT_TRUE(
1501 ContainsTrack(audio_desc->streams(), kStreamLabel2, "audio_track2"));
1502
1503 video_content = cricket::GetFirstVideoContent(offer->description());
1504 video_desc = static_cast<const cricket::VideoContentDescription*>(
1505 video_content->description);
1506 EXPECT_TRUE(
1507 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1508 EXPECT_TRUE(
1509 ContainsTrack(video_desc->streams(), kStreamLabel2, "video_track2"));
1510}
1511
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001512TEST_F(PeerConnectionInterfaceTest, RemoveStream) {
deadbeef293e9262017-01-11 12:28:30 -08001513 CreatePeerConnectionWithoutDtls();
deadbeefab9b2d12015-10-14 11:33:11 -07001514 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001515 ASSERT_EQ(1u, pc_->local_streams()->count());
1516 pc_->RemoveStream(pc_->local_streams()->at(0));
1517 EXPECT_EQ(0u, pc_->local_streams()->count());
1518}
1519
deadbeefe1f9d832016-01-14 15:35:42 -08001520// Test for AddTrack and RemoveTrack methods.
1521// Tests that the created offer includes tracks we added,
1522// and that the RtpSenders are created correctly.
1523// Also tests that RemoveTrack removes the tracks from subsequent offers.
1524TEST_F(PeerConnectionInterfaceTest, AddTrackRemoveTrack) {
deadbeef293e9262017-01-11 12:28:30 -08001525 CreatePeerConnectionWithoutDtls();
deadbeefe1f9d832016-01-14 15:35:42 -08001526 // Create a dummy stream, so tracks share a stream label.
zhihuang9763d562016-08-05 11:14:50 -07001527 rtc::scoped_refptr<MediaStreamInterface> stream(
deadbeefe1f9d832016-01-14 15:35:42 -08001528 pc_factory_->CreateLocalMediaStream(kStreamLabel1));
1529 std::vector<MediaStreamInterface*> stream_list;
1530 stream_list.push_back(stream.get());
zhihuang9763d562016-08-05 11:14:50 -07001531 rtc::scoped_refptr<AudioTrackInterface> audio_track(
deadbeefe1f9d832016-01-14 15:35:42 -08001532 pc_factory_->CreateAudioTrack("audio_track", nullptr));
zhihuang9763d562016-08-05 11:14:50 -07001533 rtc::scoped_refptr<VideoTrackInterface> video_track(
1534 pc_factory_->CreateVideoTrack(
deadbeef112b2e92017-02-10 20:13:37 -08001535 "video_track", pc_factory_->CreateVideoSource(
1536 std::unique_ptr<cricket::VideoCapturer>(
1537 new cricket::FakeVideoCapturer()))));
deadbeefe1f9d832016-01-14 15:35:42 -08001538 auto audio_sender = pc_->AddTrack(audio_track, stream_list);
1539 auto video_sender = pc_->AddTrack(video_track, stream_list);
deadbeefa601f5c2016-06-06 14:27:39 -07001540 EXPECT_EQ(1UL, audio_sender->stream_ids().size());
1541 EXPECT_EQ(kStreamLabel1, audio_sender->stream_ids()[0]);
deadbeefe1f9d832016-01-14 15:35:42 -08001542 EXPECT_EQ("audio_track", audio_sender->id());
1543 EXPECT_EQ(audio_track, audio_sender->track());
deadbeefa601f5c2016-06-06 14:27:39 -07001544 EXPECT_EQ(1UL, video_sender->stream_ids().size());
1545 EXPECT_EQ(kStreamLabel1, video_sender->stream_ids()[0]);
deadbeefe1f9d832016-01-14 15:35:42 -08001546 EXPECT_EQ("video_track", video_sender->id());
1547 EXPECT_EQ(video_track, video_sender->track());
1548
1549 // Now create an offer and check for the senders.
kwibergd1fe2812016-04-27 06:47:29 -07001550 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001551 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefe1f9d832016-01-14 15:35:42 -08001552
1553 const cricket::ContentInfo* audio_content =
1554 cricket::GetFirstAudioContent(offer->description());
1555 const cricket::AudioContentDescription* audio_desc =
1556 static_cast<const cricket::AudioContentDescription*>(
1557 audio_content->description);
1558 EXPECT_TRUE(
1559 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
1560
1561 const cricket::ContentInfo* video_content =
1562 cricket::GetFirstVideoContent(offer->description());
1563 const cricket::VideoContentDescription* video_desc =
1564 static_cast<const cricket::VideoContentDescription*>(
1565 video_content->description);
1566 EXPECT_TRUE(
1567 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1568
1569 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
1570
1571 // Now try removing the tracks.
1572 EXPECT_TRUE(pc_->RemoveTrack(audio_sender));
1573 EXPECT_TRUE(pc_->RemoveTrack(video_sender));
1574
1575 // Create a new offer and ensure it doesn't contain the removed senders.
kwiberg2bbff992016-03-16 11:03:04 -07001576 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefe1f9d832016-01-14 15:35:42 -08001577
1578 audio_content = cricket::GetFirstAudioContent(offer->description());
1579 audio_desc = static_cast<const cricket::AudioContentDescription*>(
1580 audio_content->description);
1581 EXPECT_FALSE(
1582 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
1583
1584 video_content = cricket::GetFirstVideoContent(offer->description());
1585 video_desc = static_cast<const cricket::VideoContentDescription*>(
1586 video_content->description);
1587 EXPECT_FALSE(
1588 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1589
1590 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
1591
1592 // Calling RemoveTrack on a sender no longer attached to a PeerConnection
1593 // should return false.
1594 EXPECT_FALSE(pc_->RemoveTrack(audio_sender));
1595 EXPECT_FALSE(pc_->RemoveTrack(video_sender));
1596}
1597
1598// Test creating senders without a stream specified,
1599// expecting a random stream ID to be generated.
1600TEST_F(PeerConnectionInterfaceTest, AddTrackWithoutStream) {
deadbeef293e9262017-01-11 12:28:30 -08001601 CreatePeerConnectionWithoutDtls();
deadbeefe1f9d832016-01-14 15:35:42 -08001602 // Create a dummy stream, so tracks share a stream label.
zhihuang9763d562016-08-05 11:14:50 -07001603 rtc::scoped_refptr<AudioTrackInterface> audio_track(
deadbeefe1f9d832016-01-14 15:35:42 -08001604 pc_factory_->CreateAudioTrack("audio_track", nullptr));
zhihuang9763d562016-08-05 11:14:50 -07001605 rtc::scoped_refptr<VideoTrackInterface> video_track(
1606 pc_factory_->CreateVideoTrack(
deadbeef112b2e92017-02-10 20:13:37 -08001607 "video_track", pc_factory_->CreateVideoSource(
1608 std::unique_ptr<cricket::VideoCapturer>(
1609 new cricket::FakeVideoCapturer()))));
deadbeefe1f9d832016-01-14 15:35:42 -08001610 auto audio_sender =
1611 pc_->AddTrack(audio_track, std::vector<MediaStreamInterface*>());
1612 auto video_sender =
1613 pc_->AddTrack(video_track, std::vector<MediaStreamInterface*>());
1614 EXPECT_EQ("audio_track", audio_sender->id());
1615 EXPECT_EQ(audio_track, audio_sender->track());
1616 EXPECT_EQ("video_track", video_sender->id());
1617 EXPECT_EQ(video_track, video_sender->track());
1618 // If the ID is truly a random GUID, it should be infinitely unlikely they
1619 // will be the same.
deadbeefa601f5c2016-06-06 14:27:39 -07001620 EXPECT_NE(video_sender->stream_ids(), audio_sender->stream_ids());
deadbeefe1f9d832016-01-14 15:35:42 -08001621}
1622
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001623TEST_F(PeerConnectionInterfaceTest, CreateOfferReceiveAnswer) {
1624 InitiateCall();
1625 WaitAndVerifyOnAddStream(kStreamLabel1);
1626 VerifyRemoteRtpHeaderExtensions();
1627}
1628
1629TEST_F(PeerConnectionInterfaceTest, CreateOfferReceivePrAnswerAndAnswer) {
deadbeef293e9262017-01-11 12:28:30 -08001630 CreatePeerConnectionWithoutDtls();
deadbeefab9b2d12015-10-14 11:33:11 -07001631 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001632 CreateOfferAsLocalDescription();
1633 std::string offer;
1634 EXPECT_TRUE(pc_->local_description()->ToString(&offer));
1635 CreatePrAnswerAndAnswerAsRemoteDescription(offer);
1636 WaitAndVerifyOnAddStream(kStreamLabel1);
1637}
1638
1639TEST_F(PeerConnectionInterfaceTest, ReceiveOfferCreateAnswer) {
deadbeef293e9262017-01-11 12:28:30 -08001640 CreatePeerConnectionWithoutDtls();
deadbeefab9b2d12015-10-14 11:33:11 -07001641 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001642
1643 CreateOfferAsRemoteDescription();
1644 CreateAnswerAsLocalDescription();
1645
1646 WaitAndVerifyOnAddStream(kStreamLabel1);
1647}
1648
1649TEST_F(PeerConnectionInterfaceTest, ReceiveOfferCreatePrAnswerAndAnswer) {
deadbeef293e9262017-01-11 12:28:30 -08001650 CreatePeerConnectionWithoutDtls();
deadbeefab9b2d12015-10-14 11:33:11 -07001651 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001652
1653 CreateOfferAsRemoteDescription();
1654 CreatePrAnswerAsLocalDescription();
1655 CreateAnswerAsLocalDescription();
1656
1657 WaitAndVerifyOnAddStream(kStreamLabel1);
1658}
1659
1660TEST_F(PeerConnectionInterfaceTest, Renegotiate) {
1661 InitiateCall();
1662 ASSERT_EQ(1u, pc_->remote_streams()->count());
1663 pc_->RemoveStream(pc_->local_streams()->at(0));
1664 CreateOfferReceiveAnswer();
1665 EXPECT_EQ(0u, pc_->remote_streams()->count());
deadbeefab9b2d12015-10-14 11:33:11 -07001666 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001667 CreateOfferReceiveAnswer();
1668}
1669
1670// Tests that after negotiating an audio only call, the respondent can perform a
1671// renegotiation that removes the audio stream.
1672TEST_F(PeerConnectionInterfaceTest, RenegotiateAudioOnly) {
deadbeef293e9262017-01-11 12:28:30 -08001673 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001674 AddVoiceStream(kStreamLabel1);
1675 CreateOfferAsRemoteDescription();
1676 CreateAnswerAsLocalDescription();
1677
1678 ASSERT_EQ(1u, pc_->remote_streams()->count());
1679 pc_->RemoveStream(pc_->local_streams()->at(0));
1680 CreateOfferReceiveAnswer();
1681 EXPECT_EQ(0u, pc_->remote_streams()->count());
1682}
1683
1684// Test that candidates are generated and that we can parse our own candidates.
1685TEST_F(PeerConnectionInterfaceTest, IceCandidates) {
deadbeef293e9262017-01-11 12:28:30 -08001686 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001687
1688 EXPECT_FALSE(pc_->AddIceCandidate(observer_.last_candidate_.get()));
1689 // SetRemoteDescription takes ownership of offer.
kwibergd1fe2812016-04-27 06:47:29 -07001690 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefab9b2d12015-10-14 11:33:11 -07001691 AddVideoStream(kStreamLabel1);
deadbeefc80741f2015-10-22 13:14:45 -07001692 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
kwiberg2bbff992016-03-16 11:03:04 -07001693 EXPECT_TRUE(DoSetRemoteDescription(offer.release()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001694
1695 // SetLocalDescription takes ownership of answer.
kwibergd1fe2812016-04-27 06:47:29 -07001696 std::unique_ptr<SessionDescriptionInterface> answer;
deadbeefc80741f2015-10-22 13:14:45 -07001697 EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
kwiberg2bbff992016-03-16 11:03:04 -07001698 EXPECT_TRUE(DoSetLocalDescription(answer.release()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001699
1700 EXPECT_TRUE_WAIT(observer_.last_candidate_.get() != NULL, kTimeout);
1701 EXPECT_TRUE_WAIT(observer_.ice_complete_, kTimeout);
1702
1703 EXPECT_TRUE(pc_->AddIceCandidate(observer_.last_candidate_.get()));
1704}
1705
deadbeefab9b2d12015-10-14 11:33:11 -07001706// Test that CreateOffer and CreateAnswer will fail if the track labels are
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001707// not unique.
1708TEST_F(PeerConnectionInterfaceTest, CreateOfferAnswerWithInvalidStream) {
deadbeef293e9262017-01-11 12:28:30 -08001709 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001710 // Create a regular offer for the CreateAnswer test later.
kwibergd1fe2812016-04-27 06:47:29 -07001711 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefc80741f2015-10-22 13:14:45 -07001712 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
kwiberg2bbff992016-03-16 11:03:04 -07001713 EXPECT_TRUE(offer);
1714 offer.reset();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001715
1716 // Create a local stream with audio&video tracks having same label.
1717 AddAudioVideoStream(kStreamLabel1, "track_label", "track_label");
1718
1719 // Test CreateOffer
deadbeefc80741f2015-10-22 13:14:45 -07001720 EXPECT_FALSE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001721
1722 // Test CreateAnswer
kwibergd1fe2812016-04-27 06:47:29 -07001723 std::unique_ptr<SessionDescriptionInterface> answer;
deadbeefc80741f2015-10-22 13:14:45 -07001724 EXPECT_FALSE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001725}
1726
1727// Test that we will get different SSRCs for each tracks in the offer and answer
1728// we created.
1729TEST_F(PeerConnectionInterfaceTest, SsrcInOfferAnswer) {
deadbeef293e9262017-01-11 12:28:30 -08001730 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001731 // Create a local stream with audio&video tracks having different labels.
1732 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
1733
1734 // Test CreateOffer
kwibergd1fe2812016-04-27 06:47:29 -07001735 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001736 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001737 int audio_ssrc = 0;
1738 int video_ssrc = 0;
1739 EXPECT_TRUE(GetFirstSsrc(GetFirstAudioContent(offer->description()),
1740 &audio_ssrc));
1741 EXPECT_TRUE(GetFirstSsrc(GetFirstVideoContent(offer->description()),
1742 &video_ssrc));
1743 EXPECT_NE(audio_ssrc, video_ssrc);
1744
1745 // Test CreateAnswer
1746 EXPECT_TRUE(DoSetRemoteDescription(offer.release()));
kwibergd1fe2812016-04-27 06:47:29 -07001747 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -07001748 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001749 audio_ssrc = 0;
1750 video_ssrc = 0;
1751 EXPECT_TRUE(GetFirstSsrc(GetFirstAudioContent(answer->description()),
1752 &audio_ssrc));
1753 EXPECT_TRUE(GetFirstSsrc(GetFirstVideoContent(answer->description()),
1754 &video_ssrc));
1755 EXPECT_NE(audio_ssrc, video_ssrc);
1756}
1757
deadbeefeb459812015-12-15 19:24:43 -08001758// Test that it's possible to call AddTrack on a MediaStream after adding
1759// the stream to a PeerConnection.
1760// TODO(deadbeef): Remove this test once this behavior is no longer supported.
1761TEST_F(PeerConnectionInterfaceTest, AddTrackAfterAddStream) {
deadbeef293e9262017-01-11 12:28:30 -08001762 CreatePeerConnectionWithoutDtls();
deadbeefeb459812015-12-15 19:24:43 -08001763 // Create audio stream and add to PeerConnection.
1764 AddVoiceStream(kStreamLabel1);
1765 MediaStreamInterface* stream = pc_->local_streams()->at(0);
1766
1767 // Add video track to the audio-only stream.
zhihuang9763d562016-08-05 11:14:50 -07001768 rtc::scoped_refptr<VideoTrackInterface> video_track(
1769 pc_factory_->CreateVideoTrack(
deadbeef112b2e92017-02-10 20:13:37 -08001770 "video_label", pc_factory_->CreateVideoSource(
1771 std::unique_ptr<cricket::VideoCapturer>(
1772 new cricket::FakeVideoCapturer()))));
deadbeefeb459812015-12-15 19:24:43 -08001773 stream->AddTrack(video_track.get());
1774
kwibergd1fe2812016-04-27 06:47:29 -07001775 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001776 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefeb459812015-12-15 19:24:43 -08001777
1778 const cricket::MediaContentDescription* video_desc =
1779 cricket::GetFirstVideoContentDescription(offer->description());
1780 EXPECT_TRUE(video_desc != nullptr);
1781}
1782
1783// Test that it's possible to call RemoveTrack on a MediaStream after adding
1784// the stream to a PeerConnection.
1785// TODO(deadbeef): Remove this test once this behavior is no longer supported.
1786TEST_F(PeerConnectionInterfaceTest, RemoveTrackAfterAddStream) {
deadbeef293e9262017-01-11 12:28:30 -08001787 CreatePeerConnectionWithoutDtls();
deadbeefeb459812015-12-15 19:24:43 -08001788 // Create audio/video stream and add to PeerConnection.
1789 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
1790 MediaStreamInterface* stream = pc_->local_streams()->at(0);
1791
1792 // Remove the video track.
1793 stream->RemoveTrack(stream->GetVideoTracks()[0]);
1794
kwibergd1fe2812016-04-27 06:47:29 -07001795 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001796 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefeb459812015-12-15 19:24:43 -08001797
1798 const cricket::MediaContentDescription* video_desc =
1799 cricket::GetFirstVideoContentDescription(offer->description());
1800 EXPECT_TRUE(video_desc == nullptr);
1801}
1802
deadbeef1dcb1642017-03-29 21:08:16 -07001803// Verify that CreateDtmfSender only succeeds if called with a valid local
1804// track. Other aspects of DtmfSenders are tested in
1805// peerconnection_integrationtest.cc.
1806TEST_F(PeerConnectionInterfaceTest, CreateDtmfSenderWithInvalidParams) {
1807 CreatePeerConnection();
1808 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
1809 EXPECT_EQ(nullptr, pc_->CreateDtmfSender(nullptr));
1810 rtc::scoped_refptr<webrtc::AudioTrackInterface> non_localtrack(
1811 pc_factory_->CreateAudioTrack("dummy_track", nullptr));
1812 EXPECT_EQ(nullptr, pc_->CreateDtmfSender(non_localtrack));
1813}
1814
deadbeefbd7d8f72015-12-18 16:58:44 -08001815// Test creating a sender with a stream ID, and ensure the ID is populated
1816// in the offer.
1817TEST_F(PeerConnectionInterfaceTest, CreateSenderWithStream) {
deadbeef293e9262017-01-11 12:28:30 -08001818 CreatePeerConnectionWithoutDtls();
deadbeefbd7d8f72015-12-18 16:58:44 -08001819 pc_->CreateSender("video", kStreamLabel1);
1820
kwibergd1fe2812016-04-27 06:47:29 -07001821 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001822 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefbd7d8f72015-12-18 16:58:44 -08001823
1824 const cricket::MediaContentDescription* video_desc =
1825 cricket::GetFirstVideoContentDescription(offer->description());
1826 ASSERT_TRUE(video_desc != nullptr);
1827 ASSERT_EQ(1u, video_desc->streams().size());
1828 EXPECT_EQ(kStreamLabel1, video_desc->streams()[0].sync_label);
1829}
1830
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001831// Test that we can specify a certain track that we want statistics about.
1832TEST_F(PeerConnectionInterfaceTest, GetStatsForSpecificTrack) {
1833 InitiateCall();
1834 ASSERT_LT(0u, pc_->remote_streams()->count());
1835 ASSERT_LT(0u, pc_->remote_streams()->at(0)->GetAudioTracks().size());
zhihuang9763d562016-08-05 11:14:50 -07001836 rtc::scoped_refptr<MediaStreamTrackInterface> remote_audio =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001837 pc_->remote_streams()->at(0)->GetAudioTracks()[0];
1838 EXPECT_TRUE(DoGetStats(remote_audio));
1839
1840 // Remove the stream. Since we are sending to our selves the local
1841 // and the remote stream is the same.
1842 pc_->RemoveStream(pc_->local_streams()->at(0));
1843 // Do a re-negotiation.
1844 CreateOfferReceiveAnswer();
1845
1846 ASSERT_EQ(0u, pc_->remote_streams()->count());
1847
1848 // Test that we still can get statistics for the old track. Even if it is not
1849 // sent any longer.
1850 EXPECT_TRUE(DoGetStats(remote_audio));
1851}
1852
1853// Test that we can get stats on a video track.
1854TEST_F(PeerConnectionInterfaceTest, GetStatsForVideoTrack) {
1855 InitiateCall();
1856 ASSERT_LT(0u, pc_->remote_streams()->count());
1857 ASSERT_LT(0u, pc_->remote_streams()->at(0)->GetVideoTracks().size());
zhihuang9763d562016-08-05 11:14:50 -07001858 rtc::scoped_refptr<MediaStreamTrackInterface> remote_video =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001859 pc_->remote_streams()->at(0)->GetVideoTracks()[0];
1860 EXPECT_TRUE(DoGetStats(remote_video));
1861}
1862
1863// Test that we don't get statistics for an invalid track.
zhihuange9e94c32016-11-04 11:38:15 -07001864TEST_F(PeerConnectionInterfaceTest, GetStatsForInvalidTrack) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001865 InitiateCall();
zhihuang9763d562016-08-05 11:14:50 -07001866 rtc::scoped_refptr<AudioTrackInterface> unknown_audio_track(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001867 pc_factory_->CreateAudioTrack("unknown track", NULL));
1868 EXPECT_FALSE(DoGetStats(unknown_audio_track));
1869}
1870
1871// This test setup two RTP data channels in loop back.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001872TEST_F(PeerConnectionInterfaceTest, TestDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001873 FakeConstraints constraints;
1874 constraints.SetAllowRtpDataChannels();
1875 CreatePeerConnection(&constraints);
zhihuang9763d562016-08-05 11:14:50 -07001876 rtc::scoped_refptr<DataChannelInterface> data1 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001877 pc_->CreateDataChannel("test1", NULL);
zhihuang9763d562016-08-05 11:14:50 -07001878 rtc::scoped_refptr<DataChannelInterface> data2 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001879 pc_->CreateDataChannel("test2", NULL);
1880 ASSERT_TRUE(data1 != NULL);
kwibergd1fe2812016-04-27 06:47:29 -07001881 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001882 new MockDataChannelObserver(data1));
kwibergd1fe2812016-04-27 06:47:29 -07001883 std::unique_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001884 new MockDataChannelObserver(data2));
1885
1886 EXPECT_EQ(DataChannelInterface::kConnecting, data1->state());
1887 EXPECT_EQ(DataChannelInterface::kConnecting, data2->state());
1888 std::string data_to_send1 = "testing testing";
1889 std::string data_to_send2 = "testing something else";
1890 EXPECT_FALSE(data1->Send(DataBuffer(data_to_send1)));
1891
1892 CreateOfferReceiveAnswer();
1893 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1894 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
1895
1896 EXPECT_EQ(DataChannelInterface::kOpen, data1->state());
1897 EXPECT_EQ(DataChannelInterface::kOpen, data2->state());
1898 EXPECT_TRUE(data1->Send(DataBuffer(data_to_send1)));
1899 EXPECT_TRUE(data2->Send(DataBuffer(data_to_send2)));
1900
1901 EXPECT_EQ_WAIT(data_to_send1, observer1->last_message(), kTimeout);
1902 EXPECT_EQ_WAIT(data_to_send2, observer2->last_message(), kTimeout);
1903
1904 data1->Close();
1905 EXPECT_EQ(DataChannelInterface::kClosing, data1->state());
1906 CreateOfferReceiveAnswer();
1907 EXPECT_FALSE(observer1->IsOpen());
1908 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
1909 EXPECT_TRUE(observer2->IsOpen());
1910
1911 data_to_send2 = "testing something else again";
1912 EXPECT_TRUE(data2->Send(DataBuffer(data_to_send2)));
1913
1914 EXPECT_EQ_WAIT(data_to_send2, observer2->last_message(), kTimeout);
1915}
1916
1917// This test verifies that sendnig binary data over RTP data channels should
1918// fail.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001919TEST_F(PeerConnectionInterfaceTest, TestSendBinaryOnRtpDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001920 FakeConstraints constraints;
1921 constraints.SetAllowRtpDataChannels();
1922 CreatePeerConnection(&constraints);
zhihuang9763d562016-08-05 11:14:50 -07001923 rtc::scoped_refptr<DataChannelInterface> data1 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001924 pc_->CreateDataChannel("test1", NULL);
zhihuang9763d562016-08-05 11:14:50 -07001925 rtc::scoped_refptr<DataChannelInterface> data2 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001926 pc_->CreateDataChannel("test2", NULL);
1927 ASSERT_TRUE(data1 != NULL);
kwibergd1fe2812016-04-27 06:47:29 -07001928 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001929 new MockDataChannelObserver(data1));
kwibergd1fe2812016-04-27 06:47:29 -07001930 std::unique_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001931 new MockDataChannelObserver(data2));
1932
1933 EXPECT_EQ(DataChannelInterface::kConnecting, data1->state());
1934 EXPECT_EQ(DataChannelInterface::kConnecting, data2->state());
1935
1936 CreateOfferReceiveAnswer();
1937 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1938 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
1939
1940 EXPECT_EQ(DataChannelInterface::kOpen, data1->state());
1941 EXPECT_EQ(DataChannelInterface::kOpen, data2->state());
1942
jbaucheec21bd2016-03-20 06:15:43 -07001943 rtc::CopyOnWriteBuffer buffer("test", 4);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001944 EXPECT_FALSE(data1->Send(DataBuffer(buffer, true)));
1945}
1946
1947// This test setup a RTP data channels in loop back and test that a channel is
1948// opened even if the remote end answer with a zero SSRC.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001949TEST_F(PeerConnectionInterfaceTest, TestSendOnlyDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001950 FakeConstraints constraints;
1951 constraints.SetAllowRtpDataChannels();
1952 CreatePeerConnection(&constraints);
zhihuang9763d562016-08-05 11:14:50 -07001953 rtc::scoped_refptr<DataChannelInterface> data1 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001954 pc_->CreateDataChannel("test1", NULL);
kwibergd1fe2812016-04-27 06:47:29 -07001955 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001956 new MockDataChannelObserver(data1));
1957
1958 CreateOfferReceiveAnswerWithoutSsrc();
1959
1960 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1961
1962 data1->Close();
1963 EXPECT_EQ(DataChannelInterface::kClosing, data1->state());
1964 CreateOfferReceiveAnswerWithoutSsrc();
1965 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
1966 EXPECT_FALSE(observer1->IsOpen());
1967}
1968
1969// This test that if a data channel is added in an answer a receive only channel
1970// channel is created.
1971TEST_F(PeerConnectionInterfaceTest, TestReceiveOnlyDataChannel) {
1972 FakeConstraints constraints;
1973 constraints.SetAllowRtpDataChannels();
1974 CreatePeerConnection(&constraints);
1975
1976 std::string offer_label = "offer_channel";
zhihuang9763d562016-08-05 11:14:50 -07001977 rtc::scoped_refptr<DataChannelInterface> offer_channel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001978 pc_->CreateDataChannel(offer_label, NULL);
1979
1980 CreateOfferAsLocalDescription();
1981
1982 // Replace the data channel label in the offer and apply it as an answer.
1983 std::string receive_label = "answer_channel";
1984 std::string sdp;
1985 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001986 rtc::replace_substrs(offer_label.c_str(), offer_label.length(),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001987 receive_label.c_str(), receive_label.length(),
1988 &sdp);
1989 CreateAnswerAsRemoteDescription(sdp);
1990
1991 // Verify that a new incoming data channel has been created and that
1992 // it is open but can't we written to.
1993 ASSERT_TRUE(observer_.last_datachannel_ != NULL);
1994 DataChannelInterface* received_channel = observer_.last_datachannel_;
1995 EXPECT_EQ(DataChannelInterface::kConnecting, received_channel->state());
1996 EXPECT_EQ(receive_label, received_channel->label());
1997 EXPECT_FALSE(received_channel->Send(DataBuffer("something")));
1998
1999 // Verify that the channel we initially offered has been rejected.
2000 EXPECT_EQ(DataChannelInterface::kClosed, offer_channel->state());
2001
2002 // Do another offer / answer exchange and verify that the data channel is
2003 // opened.
2004 CreateOfferReceiveAnswer();
2005 EXPECT_EQ_WAIT(DataChannelInterface::kOpen, received_channel->state(),
2006 kTimeout);
2007}
2008
2009// This test that no data channel is returned if a reliable channel is
2010// requested.
2011// TODO(perkj): Remove this test once reliable channels are implemented.
2012TEST_F(PeerConnectionInterfaceTest, CreateReliableRtpDataChannelShouldFail) {
2013 FakeConstraints constraints;
2014 constraints.SetAllowRtpDataChannels();
2015 CreatePeerConnection(&constraints);
2016
2017 std::string label = "test";
2018 webrtc::DataChannelInit config;
2019 config.reliable = true;
zhihuang9763d562016-08-05 11:14:50 -07002020 rtc::scoped_refptr<DataChannelInterface> channel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002021 pc_->CreateDataChannel(label, &config);
2022 EXPECT_TRUE(channel == NULL);
2023}
2024
deadbeefab9b2d12015-10-14 11:33:11 -07002025// Verifies that duplicated label is not allowed for RTP data channel.
2026TEST_F(PeerConnectionInterfaceTest, RtpDuplicatedLabelNotAllowed) {
2027 FakeConstraints constraints;
2028 constraints.SetAllowRtpDataChannels();
2029 CreatePeerConnection(&constraints);
2030
2031 std::string label = "test";
zhihuang9763d562016-08-05 11:14:50 -07002032 rtc::scoped_refptr<DataChannelInterface> channel =
deadbeefab9b2d12015-10-14 11:33:11 -07002033 pc_->CreateDataChannel(label, nullptr);
2034 EXPECT_NE(channel, nullptr);
2035
zhihuang9763d562016-08-05 11:14:50 -07002036 rtc::scoped_refptr<DataChannelInterface> dup_channel =
deadbeefab9b2d12015-10-14 11:33:11 -07002037 pc_->CreateDataChannel(label, nullptr);
2038 EXPECT_EQ(dup_channel, nullptr);
2039}
2040
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002041// This tests that a SCTP data channel is returned using different
2042// DataChannelInit configurations.
2043TEST_F(PeerConnectionInterfaceTest, CreateSctpDataChannel) {
2044 FakeConstraints constraints;
2045 constraints.SetAllowDtlsSctpDataChannels();
2046 CreatePeerConnection(&constraints);
2047
2048 webrtc::DataChannelInit config;
2049
zhihuang9763d562016-08-05 11:14:50 -07002050 rtc::scoped_refptr<DataChannelInterface> channel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002051 pc_->CreateDataChannel("1", &config);
2052 EXPECT_TRUE(channel != NULL);
2053 EXPECT_TRUE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00002054 EXPECT_TRUE(observer_.renegotiation_needed_);
2055 observer_.renegotiation_needed_ = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002056
2057 config.ordered = false;
2058 channel = pc_->CreateDataChannel("2", &config);
2059 EXPECT_TRUE(channel != NULL);
2060 EXPECT_TRUE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00002061 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002062
2063 config.ordered = true;
2064 config.maxRetransmits = 0;
2065 channel = pc_->CreateDataChannel("3", &config);
2066 EXPECT_TRUE(channel != NULL);
2067 EXPECT_FALSE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00002068 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002069
2070 config.maxRetransmits = -1;
2071 config.maxRetransmitTime = 0;
2072 channel = pc_->CreateDataChannel("4", &config);
2073 EXPECT_TRUE(channel != NULL);
2074 EXPECT_FALSE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00002075 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002076}
2077
2078// This tests that no data channel is returned if both maxRetransmits and
2079// maxRetransmitTime are set for SCTP data channels.
2080TEST_F(PeerConnectionInterfaceTest,
2081 CreateSctpDataChannelShouldFailForInvalidConfig) {
2082 FakeConstraints constraints;
2083 constraints.SetAllowDtlsSctpDataChannels();
2084 CreatePeerConnection(&constraints);
2085
2086 std::string label = "test";
2087 webrtc::DataChannelInit config;
2088 config.maxRetransmits = 0;
2089 config.maxRetransmitTime = 0;
2090
zhihuang9763d562016-08-05 11:14:50 -07002091 rtc::scoped_refptr<DataChannelInterface> channel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002092 pc_->CreateDataChannel(label, &config);
2093 EXPECT_TRUE(channel == NULL);
2094}
2095
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002096// The test verifies that creating a SCTP data channel with an id already in use
2097// or out of range should fail.
2098TEST_F(PeerConnectionInterfaceTest,
2099 CreateSctpDataChannelWithInvalidIdShouldFail) {
2100 FakeConstraints constraints;
2101 constraints.SetAllowDtlsSctpDataChannels();
2102 CreatePeerConnection(&constraints);
2103
2104 webrtc::DataChannelInit config;
zhihuang9763d562016-08-05 11:14:50 -07002105 rtc::scoped_refptr<DataChannelInterface> channel;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002106
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00002107 config.id = 1;
2108 channel = pc_->CreateDataChannel("1", &config);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002109 EXPECT_TRUE(channel != NULL);
2110 EXPECT_EQ(1, channel->id());
2111
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002112 channel = pc_->CreateDataChannel("x", &config);
2113 EXPECT_TRUE(channel == NULL);
2114
2115 config.id = cricket::kMaxSctpSid;
2116 channel = pc_->CreateDataChannel("max", &config);
2117 EXPECT_TRUE(channel != NULL);
2118 EXPECT_EQ(config.id, channel->id());
2119
2120 config.id = cricket::kMaxSctpSid + 1;
2121 channel = pc_->CreateDataChannel("x", &config);
2122 EXPECT_TRUE(channel == NULL);
2123}
2124
deadbeefab9b2d12015-10-14 11:33:11 -07002125// Verifies that duplicated label is allowed for SCTP data channel.
2126TEST_F(PeerConnectionInterfaceTest, SctpDuplicatedLabelAllowed) {
2127 FakeConstraints constraints;
2128 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2129 true);
2130 CreatePeerConnection(&constraints);
2131
2132 std::string label = "test";
zhihuang9763d562016-08-05 11:14:50 -07002133 rtc::scoped_refptr<DataChannelInterface> channel =
deadbeefab9b2d12015-10-14 11:33:11 -07002134 pc_->CreateDataChannel(label, nullptr);
2135 EXPECT_NE(channel, nullptr);
2136
zhihuang9763d562016-08-05 11:14:50 -07002137 rtc::scoped_refptr<DataChannelInterface> dup_channel =
deadbeefab9b2d12015-10-14 11:33:11 -07002138 pc_->CreateDataChannel(label, nullptr);
2139 EXPECT_NE(dup_channel, nullptr);
2140}
2141
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00002142// This test verifies that OnRenegotiationNeeded is fired for every new RTP
2143// DataChannel.
2144TEST_F(PeerConnectionInterfaceTest, RenegotiationNeededForNewRtpDataChannel) {
2145 FakeConstraints constraints;
2146 constraints.SetAllowRtpDataChannels();
2147 CreatePeerConnection(&constraints);
2148
zhihuang9763d562016-08-05 11:14:50 -07002149 rtc::scoped_refptr<DataChannelInterface> dc1 =
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00002150 pc_->CreateDataChannel("test1", NULL);
2151 EXPECT_TRUE(observer_.renegotiation_needed_);
2152 observer_.renegotiation_needed_ = false;
2153
zhihuang9763d562016-08-05 11:14:50 -07002154 rtc::scoped_refptr<DataChannelInterface> dc2 =
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00002155 pc_->CreateDataChannel("test2", NULL);
2156 EXPECT_TRUE(observer_.renegotiation_needed_);
2157}
2158
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002159// This test that a data channel closes when a PeerConnection is deleted/closed.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002160TEST_F(PeerConnectionInterfaceTest, DataChannelCloseWhenPeerConnectionClose) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002161 FakeConstraints constraints;
2162 constraints.SetAllowRtpDataChannels();
2163 CreatePeerConnection(&constraints);
2164
zhihuang9763d562016-08-05 11:14:50 -07002165 rtc::scoped_refptr<DataChannelInterface> data1 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002166 pc_->CreateDataChannel("test1", NULL);
zhihuang9763d562016-08-05 11:14:50 -07002167 rtc::scoped_refptr<DataChannelInterface> data2 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002168 pc_->CreateDataChannel("test2", NULL);
2169 ASSERT_TRUE(data1 != NULL);
kwibergd1fe2812016-04-27 06:47:29 -07002170 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002171 new MockDataChannelObserver(data1));
kwibergd1fe2812016-04-27 06:47:29 -07002172 std::unique_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002173 new MockDataChannelObserver(data2));
2174
2175 CreateOfferReceiveAnswer();
2176 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
2177 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
2178
2179 ReleasePeerConnection();
2180 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
2181 EXPECT_EQ(DataChannelInterface::kClosed, data2->state());
2182}
2183
2184// This test that data channels can be rejected in an answer.
2185TEST_F(PeerConnectionInterfaceTest, TestRejectDataChannelInAnswer) {
2186 FakeConstraints constraints;
2187 constraints.SetAllowRtpDataChannels();
2188 CreatePeerConnection(&constraints);
2189
zhihuang9763d562016-08-05 11:14:50 -07002190 rtc::scoped_refptr<DataChannelInterface> offer_channel(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002191 pc_->CreateDataChannel("offer_channel", NULL));
2192
2193 CreateOfferAsLocalDescription();
2194
2195 // Create an answer where the m-line for data channels are rejected.
2196 std::string sdp;
2197 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
2198 webrtc::JsepSessionDescription* answer = new webrtc::JsepSessionDescription(
2199 SessionDescriptionInterface::kAnswer);
2200 EXPECT_TRUE(answer->Initialize(sdp, NULL));
2201 cricket::ContentInfo* data_info =
2202 answer->description()->GetContentByName("data");
2203 data_info->rejected = true;
2204
2205 DoSetRemoteDescription(answer);
2206 EXPECT_EQ(DataChannelInterface::kClosed, offer_channel->state());
2207}
2208
2209// Test that we can create a session description from an SDP string from
2210// FireFox, use it as a remote session description, generate an answer and use
2211// the answer as a local description.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002212TEST_F(PeerConnectionInterfaceTest, ReceiveFireFoxOffer) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002213 FakeConstraints constraints;
2214 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2215 true);
2216 CreatePeerConnection(&constraints);
2217 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
2218 SessionDescriptionInterface* desc =
2219 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
jbauchfabe2c92015-07-16 13:43:14 -07002220 webrtc::kFireFoxSdpOffer, nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002221 EXPECT_TRUE(DoSetSessionDescription(desc, false));
2222 CreateAnswerAsLocalDescription();
2223 ASSERT_TRUE(pc_->local_description() != NULL);
2224 ASSERT_TRUE(pc_->remote_description() != NULL);
2225
2226 const cricket::ContentInfo* content =
2227 cricket::GetFirstAudioContent(pc_->local_description()->description());
2228 ASSERT_TRUE(content != NULL);
2229 EXPECT_FALSE(content->rejected);
2230
2231 content =
2232 cricket::GetFirstVideoContent(pc_->local_description()->description());
2233 ASSERT_TRUE(content != NULL);
2234 EXPECT_FALSE(content->rejected);
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00002235#ifdef HAVE_SCTP
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002236 content =
2237 cricket::GetFirstDataContent(pc_->local_description()->description());
2238 ASSERT_TRUE(content != NULL);
2239 EXPECT_TRUE(content->rejected);
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00002240#endif
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002241}
2242
deadbeef8662f942017-01-20 21:20:51 -08002243// Test that an offer can be received which offers DTLS with SDES fallback.
2244// Regression test for issue:
2245// https://bugs.chromium.org/p/webrtc/issues/detail?id=6972
2246TEST_F(PeerConnectionInterfaceTest, ReceiveDtlsSdesFallbackOffer) {
2247 FakeConstraints constraints;
2248 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2249 true);
2250 CreatePeerConnection(&constraints);
2251 // Wait for fake certificate to be generated. Previously, this is what caused
2252 // the "a=crypto" lines to be rejected.
2253 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
2254 ASSERT_NE(nullptr, fake_certificate_generator_);
2255 EXPECT_EQ_WAIT(1, fake_certificate_generator_->generated_certificates(),
2256 kTimeout);
2257 SessionDescriptionInterface* desc = webrtc::CreateSessionDescription(
2258 SessionDescriptionInterface::kOffer, kDtlsSdesFallbackSdp, nullptr);
2259 EXPECT_TRUE(DoSetSessionDescription(desc, false));
2260 CreateAnswerAsLocalDescription();
2261}
2262
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002263// Test that we can create an audio only offer and receive an answer with a
2264// limited set of audio codecs and receive an updated offer with more audio
2265// codecs, where the added codecs are not supported.
2266TEST_F(PeerConnectionInterfaceTest, ReceiveUpdatedAudioOfferWithBadCodecs) {
deadbeef293e9262017-01-11 12:28:30 -08002267 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002268 AddVoiceStream("audio_label");
2269 CreateOfferAsLocalDescription();
2270
2271 SessionDescriptionInterface* answer =
2272 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
jbauchfabe2c92015-07-16 13:43:14 -07002273 webrtc::kAudioSdp, nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002274 EXPECT_TRUE(DoSetSessionDescription(answer, false));
2275
2276 SessionDescriptionInterface* updated_offer =
2277 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
jbauchfabe2c92015-07-16 13:43:14 -07002278 webrtc::kAudioSdpWithUnsupportedCodecs,
2279 nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002280 EXPECT_TRUE(DoSetSessionDescription(updated_offer, false));
2281 CreateAnswerAsLocalDescription();
2282}
2283
deadbeefc80741f2015-10-22 13:14:45 -07002284// Test that if we're receiving (but not sending) a track, subsequent offers
2285// will have m-lines with a=recvonly.
2286TEST_F(PeerConnectionInterfaceTest, CreateSubsequentRecvOnlyOffer) {
2287 FakeConstraints constraints;
2288 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2289 true);
2290 CreatePeerConnection(&constraints);
2291 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2292 CreateAnswerAsLocalDescription();
2293
2294 // At this point we should be receiving stream 1, but not sending anything.
2295 // A new offer should be recvonly.
kwibergd1fe2812016-04-27 06:47:29 -07002296 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefc80741f2015-10-22 13:14:45 -07002297 DoCreateOffer(&offer, nullptr);
2298
2299 const cricket::ContentInfo* video_content =
2300 cricket::GetFirstVideoContent(offer->description());
2301 const cricket::VideoContentDescription* video_desc =
2302 static_cast<const cricket::VideoContentDescription*>(
2303 video_content->description);
2304 ASSERT_EQ(cricket::MD_RECVONLY, video_desc->direction());
2305
2306 const cricket::ContentInfo* audio_content =
2307 cricket::GetFirstAudioContent(offer->description());
2308 const cricket::AudioContentDescription* audio_desc =
2309 static_cast<const cricket::AudioContentDescription*>(
2310 audio_content->description);
2311 ASSERT_EQ(cricket::MD_RECVONLY, audio_desc->direction());
2312}
2313
2314// Test that if we're receiving (but not sending) a track, and the
2315// offerToReceiveVideo/offerToReceiveAudio constraints are explicitly set to
2316// false, the generated m-lines will be a=inactive.
2317TEST_F(PeerConnectionInterfaceTest, CreateSubsequentInactiveOffer) {
2318 FakeConstraints constraints;
2319 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2320 true);
2321 CreatePeerConnection(&constraints);
2322 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2323 CreateAnswerAsLocalDescription();
2324
2325 // At this point we should be receiving stream 1, but not sending anything.
2326 // A new offer would be recvonly, but we'll set the "no receive" constraints
2327 // to make it inactive.
kwibergd1fe2812016-04-27 06:47:29 -07002328 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefc80741f2015-10-22 13:14:45 -07002329 FakeConstraints offer_constraints;
2330 offer_constraints.AddMandatory(
2331 webrtc::MediaConstraintsInterface::kOfferToReceiveVideo, false);
2332 offer_constraints.AddMandatory(
2333 webrtc::MediaConstraintsInterface::kOfferToReceiveAudio, false);
2334 DoCreateOffer(&offer, &offer_constraints);
2335
2336 const cricket::ContentInfo* video_content =
2337 cricket::GetFirstVideoContent(offer->description());
2338 const cricket::VideoContentDescription* video_desc =
2339 static_cast<const cricket::VideoContentDescription*>(
2340 video_content->description);
2341 ASSERT_EQ(cricket::MD_INACTIVE, video_desc->direction());
2342
2343 const cricket::ContentInfo* audio_content =
2344 cricket::GetFirstAudioContent(offer->description());
2345 const cricket::AudioContentDescription* audio_desc =
2346 static_cast<const cricket::AudioContentDescription*>(
2347 audio_content->description);
2348 ASSERT_EQ(cricket::MD_INACTIVE, audio_desc->direction());
2349}
2350
deadbeef653b8e02015-11-11 12:55:10 -08002351// Test that we can use SetConfiguration to change the ICE servers of the
2352// PortAllocator.
2353TEST_F(PeerConnectionInterfaceTest, SetConfigurationChangesIceServers) {
2354 CreatePeerConnection();
2355
2356 PeerConnectionInterface::RTCConfiguration config;
2357 PeerConnectionInterface::IceServer server;
2358 server.uri = "stun:test_hostname";
2359 config.servers.push_back(server);
2360 EXPECT_TRUE(pc_->SetConfiguration(config));
2361
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002362 EXPECT_EQ(1u, port_allocator_->stun_servers().size());
2363 EXPECT_EQ("test_hostname",
2364 port_allocator_->stun_servers().begin()->hostname());
deadbeef653b8e02015-11-11 12:55:10 -08002365}
2366
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002367TEST_F(PeerConnectionInterfaceTest, SetConfigurationChangesCandidateFilter) {
2368 CreatePeerConnection();
2369 PeerConnectionInterface::RTCConfiguration config;
2370 config.type = PeerConnectionInterface::kRelay;
2371 EXPECT_TRUE(pc_->SetConfiguration(config));
2372 EXPECT_EQ(cricket::CF_RELAY, port_allocator_->candidate_filter());
2373}
2374
deadbeef293e9262017-01-11 12:28:30 -08002375TEST_F(PeerConnectionInterfaceTest, SetConfigurationChangesPruneTurnPortsFlag) {
2376 PeerConnectionInterface::RTCConfiguration config;
2377 config.prune_turn_ports = false;
2378 CreatePeerConnection(config, nullptr);
2379 EXPECT_FALSE(port_allocator_->prune_turn_ports());
2380
2381 config.prune_turn_ports = true;
2382 EXPECT_TRUE(pc_->SetConfiguration(config));
2383 EXPECT_TRUE(port_allocator_->prune_turn_ports());
2384}
2385
skvladd1f5fda2017-02-03 16:54:05 -08002386// Test that the ice check interval can be changed. This does not verify that
2387// the setting makes it all the way to P2PTransportChannel, as that would
2388// require a very complex set of mocks.
2389TEST_F(PeerConnectionInterfaceTest, SetConfigurationChangesIceCheckInterval) {
2390 PeerConnectionInterface::RTCConfiguration config;
2391 config.ice_check_min_interval = rtc::Optional<int>();
2392 CreatePeerConnection(config, nullptr);
2393 config.ice_check_min_interval = rtc::Optional<int>(100);
2394 EXPECT_TRUE(pc_->SetConfiguration(config));
2395 PeerConnectionInterface::RTCConfiguration new_config =
2396 pc_->GetConfiguration();
2397 EXPECT_EQ(new_config.ice_check_min_interval, rtc::Optional<int>(100));
2398}
2399
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002400// Test that when SetConfiguration changes both the pool size and other
2401// attributes, the pooled session is created with the updated attributes.
2402TEST_F(PeerConnectionInterfaceTest,
2403 SetConfigurationCreatesPooledSessionCorrectly) {
2404 CreatePeerConnection();
2405 PeerConnectionInterface::RTCConfiguration config;
2406 config.ice_candidate_pool_size = 1;
2407 PeerConnectionInterface::IceServer server;
2408 server.uri = kStunAddressOnly;
2409 config.servers.push_back(server);
2410 config.type = PeerConnectionInterface::kRelay;
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07002411 EXPECT_TRUE(pc_->SetConfiguration(config));
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002412
2413 const cricket::FakePortAllocatorSession* session =
2414 static_cast<const cricket::FakePortAllocatorSession*>(
2415 port_allocator_->GetPooledSession());
2416 ASSERT_NE(nullptr, session);
2417 EXPECT_EQ(1UL, session->stun_servers().size());
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002418}
2419
deadbeef293e9262017-01-11 12:28:30 -08002420// Test that after SetLocalDescription, changing the pool size is not allowed,
2421// and an invalid modification error is returned.
deadbeef6de92f92016-12-12 18:49:32 -08002422TEST_F(PeerConnectionInterfaceTest,
2423 CantChangePoolSizeAfterSetLocalDescription) {
2424 CreatePeerConnection();
2425 // Start by setting a size of 1.
2426 PeerConnectionInterface::RTCConfiguration config;
2427 config.ice_candidate_pool_size = 1;
2428 EXPECT_TRUE(pc_->SetConfiguration(config));
2429
2430 // Set remote offer; can still change pool size at this point.
2431 CreateOfferAsRemoteDescription();
2432 config.ice_candidate_pool_size = 2;
2433 EXPECT_TRUE(pc_->SetConfiguration(config));
2434
2435 // Set local answer; now it's too late.
2436 CreateAnswerAsLocalDescription();
2437 config.ice_candidate_pool_size = 3;
deadbeef293e9262017-01-11 12:28:30 -08002438 RTCError error;
2439 EXPECT_FALSE(pc_->SetConfiguration(config, &error));
2440 EXPECT_EQ(RTCErrorType::INVALID_MODIFICATION, error.type());
2441}
2442
deadbeef42a42632017-03-10 15:18:00 -08002443// Test that after setting an answer, extra pooled sessions are discarded. The
2444// ICE candidate pool is only intended to be used for the first offer/answer.
2445TEST_F(PeerConnectionInterfaceTest,
2446 ExtraPooledSessionsDiscardedAfterApplyingAnswer) {
2447 CreatePeerConnection();
2448
2449 // Set a larger-than-necessary size.
2450 PeerConnectionInterface::RTCConfiguration config;
2451 config.ice_candidate_pool_size = 4;
2452 EXPECT_TRUE(pc_->SetConfiguration(config));
2453
2454 // Do offer/answer.
2455 CreateOfferAsRemoteDescription();
2456 CreateAnswerAsLocalDescription();
2457
2458 // Expect no pooled sessions to be left.
2459 const cricket::PortAllocatorSession* session =
2460 port_allocator_->GetPooledSession();
2461 EXPECT_EQ(nullptr, session);
2462}
2463
2464// After Close is called, pooled candidates should be discarded so as to not
2465// waste network resources.
2466TEST_F(PeerConnectionInterfaceTest, PooledSessionsDiscardedAfterClose) {
2467 CreatePeerConnection();
2468
2469 PeerConnectionInterface::RTCConfiguration config;
2470 config.ice_candidate_pool_size = 3;
2471 EXPECT_TRUE(pc_->SetConfiguration(config));
2472 pc_->Close();
2473
2474 // Expect no pooled sessions to be left.
2475 const cricket::PortAllocatorSession* session =
2476 port_allocator_->GetPooledSession();
2477 EXPECT_EQ(nullptr, session);
2478}
2479
deadbeef293e9262017-01-11 12:28:30 -08002480// Test that SetConfiguration returns an invalid modification error if
2481// modifying a field in the configuration that isn't allowed to be modified.
2482TEST_F(PeerConnectionInterfaceTest,
2483 SetConfigurationReturnsInvalidModificationError) {
2484 PeerConnectionInterface::RTCConfiguration config;
2485 config.bundle_policy = PeerConnectionInterface::kBundlePolicyBalanced;
2486 config.rtcp_mux_policy = PeerConnectionInterface::kRtcpMuxPolicyNegotiate;
2487 config.continual_gathering_policy = PeerConnectionInterface::GATHER_ONCE;
2488 CreatePeerConnection(config, nullptr);
2489
2490 PeerConnectionInterface::RTCConfiguration modified_config = config;
2491 modified_config.bundle_policy =
2492 PeerConnectionInterface::kBundlePolicyMaxBundle;
2493 RTCError error;
2494 EXPECT_FALSE(pc_->SetConfiguration(modified_config, &error));
2495 EXPECT_EQ(RTCErrorType::INVALID_MODIFICATION, error.type());
2496
2497 modified_config = config;
2498 modified_config.rtcp_mux_policy =
2499 PeerConnectionInterface::kRtcpMuxPolicyRequire;
2500 error.set_type(RTCErrorType::NONE);
2501 EXPECT_FALSE(pc_->SetConfiguration(modified_config, &error));
2502 EXPECT_EQ(RTCErrorType::INVALID_MODIFICATION, error.type());
2503
2504 modified_config = config;
2505 modified_config.continual_gathering_policy =
2506 PeerConnectionInterface::GATHER_CONTINUALLY;
2507 error.set_type(RTCErrorType::NONE);
2508 EXPECT_FALSE(pc_->SetConfiguration(modified_config, &error));
2509 EXPECT_EQ(RTCErrorType::INVALID_MODIFICATION, error.type());
2510}
2511
2512// Test that SetConfiguration returns a range error if the candidate pool size
2513// is negative or larger than allowed by the spec.
2514TEST_F(PeerConnectionInterfaceTest,
2515 SetConfigurationReturnsRangeErrorForBadCandidatePoolSize) {
2516 PeerConnectionInterface::RTCConfiguration config;
2517 CreatePeerConnection(config, nullptr);
2518
2519 config.ice_candidate_pool_size = -1;
2520 RTCError error;
2521 EXPECT_FALSE(pc_->SetConfiguration(config, &error));
2522 EXPECT_EQ(RTCErrorType::INVALID_RANGE, error.type());
2523
2524 config.ice_candidate_pool_size = INT_MAX;
2525 error.set_type(RTCErrorType::NONE);
2526 EXPECT_FALSE(pc_->SetConfiguration(config, &error));
2527 EXPECT_EQ(RTCErrorType::INVALID_RANGE, error.type());
2528}
2529
2530// Test that SetConfiguration returns a syntax error if parsing an ICE server
2531// URL failed.
2532TEST_F(PeerConnectionInterfaceTest,
2533 SetConfigurationReturnsSyntaxErrorFromBadIceUrls) {
2534 PeerConnectionInterface::RTCConfiguration config;
2535 CreatePeerConnection(config, nullptr);
2536
2537 PeerConnectionInterface::IceServer bad_server;
2538 bad_server.uri = "stunn:www.example.com";
2539 config.servers.push_back(bad_server);
2540 RTCError error;
2541 EXPECT_FALSE(pc_->SetConfiguration(config, &error));
2542 EXPECT_EQ(RTCErrorType::SYNTAX_ERROR, error.type());
2543}
2544
2545// Test that SetConfiguration returns an invalid parameter error if a TURN
2546// IceServer is missing a username or password.
2547TEST_F(PeerConnectionInterfaceTest,
2548 SetConfigurationReturnsInvalidParameterIfCredentialsMissing) {
2549 PeerConnectionInterface::RTCConfiguration config;
2550 CreatePeerConnection(config, nullptr);
2551
2552 PeerConnectionInterface::IceServer bad_server;
2553 bad_server.uri = "turn:www.example.com";
2554 // Missing password.
2555 bad_server.username = "foo";
2556 config.servers.push_back(bad_server);
2557 RTCError error;
2558 EXPECT_FALSE(pc_->SetConfiguration(config, &error));
2559 EXPECT_EQ(RTCErrorType::INVALID_PARAMETER, error.type());
deadbeef6de92f92016-12-12 18:49:32 -08002560}
2561
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002562// Test that PeerConnection::Close changes the states to closed and all remote
2563// tracks change state to ended.
2564TEST_F(PeerConnectionInterfaceTest, CloseAndTestStreamsAndStates) {
2565 // Initialize a PeerConnection and negotiate local and remote session
2566 // description.
2567 InitiateCall();
2568 ASSERT_EQ(1u, pc_->local_streams()->count());
2569 ASSERT_EQ(1u, pc_->remote_streams()->count());
2570
2571 pc_->Close();
2572
2573 EXPECT_EQ(PeerConnectionInterface::kClosed, pc_->signaling_state());
2574 EXPECT_EQ(PeerConnectionInterface::kIceConnectionClosed,
2575 pc_->ice_connection_state());
2576 EXPECT_EQ(PeerConnectionInterface::kIceGatheringComplete,
2577 pc_->ice_gathering_state());
2578
2579 EXPECT_EQ(1u, pc_->local_streams()->count());
2580 EXPECT_EQ(1u, pc_->remote_streams()->count());
2581
zhihuang9763d562016-08-05 11:14:50 -07002582 rtc::scoped_refptr<MediaStreamInterface> remote_stream =
2583 pc_->remote_streams()->at(0);
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002584 // Track state may be updated asynchronously.
perkjd61bf802016-03-24 03:16:19 -07002585 EXPECT_EQ_WAIT(MediaStreamTrackInterface::kEnded,
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002586 remote_stream->GetAudioTracks()[0]->state(), kTimeout);
2587 EXPECT_EQ_WAIT(MediaStreamTrackInterface::kEnded,
2588 remote_stream->GetVideoTracks()[0]->state(), kTimeout);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002589}
2590
2591// Test that PeerConnection methods fails gracefully after
2592// PeerConnection::Close has been called.
2593TEST_F(PeerConnectionInterfaceTest, CloseAndTestMethods) {
deadbeef293e9262017-01-11 12:28:30 -08002594 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002595 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
2596 CreateOfferAsRemoteDescription();
2597 CreateAnswerAsLocalDescription();
2598
2599 ASSERT_EQ(1u, pc_->local_streams()->count());
zhihuang9763d562016-08-05 11:14:50 -07002600 rtc::scoped_refptr<MediaStreamInterface> local_stream =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002601 pc_->local_streams()->at(0);
2602
2603 pc_->Close();
2604
2605 pc_->RemoveStream(local_stream);
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +00002606 EXPECT_FALSE(pc_->AddStream(local_stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002607
2608 ASSERT_FALSE(local_stream->GetAudioTracks().empty());
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002609 rtc::scoped_refptr<webrtc::DtmfSenderInterface> dtmf_sender(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002610 pc_->CreateDtmfSender(local_stream->GetAudioTracks()[0]));
wu@webrtc.org66037362013-08-13 00:09:35 +00002611 EXPECT_TRUE(NULL == dtmf_sender); // local stream has been removed.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002612
2613 EXPECT_TRUE(pc_->CreateDataChannel("test", NULL) == NULL);
2614
2615 EXPECT_TRUE(pc_->local_description() != NULL);
2616 EXPECT_TRUE(pc_->remote_description() != NULL);
2617
kwibergd1fe2812016-04-27 06:47:29 -07002618 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07002619 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
kwibergd1fe2812016-04-27 06:47:29 -07002620 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -07002621 EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002622
2623 std::string sdp;
2624 ASSERT_TRUE(pc_->remote_description()->ToString(&sdp));
2625 SessionDescriptionInterface* remote_offer =
2626 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
2627 sdp, NULL);
2628 EXPECT_FALSE(DoSetRemoteDescription(remote_offer));
2629
2630 ASSERT_TRUE(pc_->local_description()->ToString(&sdp));
2631 SessionDescriptionInterface* local_offer =
2632 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
2633 sdp, NULL);
2634 EXPECT_FALSE(DoSetLocalDescription(local_offer));
2635}
2636
2637// Test that GetStats can still be called after PeerConnection::Close.
2638TEST_F(PeerConnectionInterfaceTest, CloseAndGetStats) {
2639 InitiateCall();
2640 pc_->Close();
2641 DoGetStats(NULL);
2642}
deadbeefab9b2d12015-10-14 11:33:11 -07002643
2644// NOTE: The series of tests below come from what used to be
2645// mediastreamsignaling_unittest.cc, and are mostly aimed at testing that
2646// setting a remote or local description has the expected effects.
2647
2648// This test verifies that the remote MediaStreams corresponding to a received
2649// SDP string is created. In this test the two separate MediaStreams are
2650// signaled.
2651TEST_F(PeerConnectionInterfaceTest, UpdateRemoteStreams) {
2652 FakeConstraints constraints;
2653 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2654 true);
2655 CreatePeerConnection(&constraints);
2656 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2657
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002658 rtc::scoped_refptr<StreamCollection> reference(CreateStreamCollection(1, 1));
deadbeefab9b2d12015-10-14 11:33:11 -07002659 EXPECT_TRUE(
2660 CompareStreamCollections(observer_.remote_streams(), reference.get()));
2661 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2662 EXPECT_TRUE(remote_stream->GetVideoTracks()[0]->GetSource() != nullptr);
2663
2664 // Create a session description based on another SDP with another
2665 // MediaStream.
2666 CreateAndSetRemoteOffer(kSdpStringWithStream1And2);
2667
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002668 rtc::scoped_refptr<StreamCollection> reference2(CreateStreamCollection(2, 1));
deadbeefab9b2d12015-10-14 11:33:11 -07002669 EXPECT_TRUE(
2670 CompareStreamCollections(observer_.remote_streams(), reference2.get()));
2671}
2672
2673// This test verifies that when remote tracks are added/removed from SDP, the
2674// created remote streams are updated appropriately.
2675TEST_F(PeerConnectionInterfaceTest,
2676 AddRemoveTrackFromExistingRemoteMediaStream) {
2677 FakeConstraints constraints;
2678 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2679 true);
2680 CreatePeerConnection(&constraints);
kwibergd1fe2812016-04-27 06:47:29 -07002681 std::unique_ptr<SessionDescriptionInterface> desc_ms1 =
kwiberg2bbff992016-03-16 11:03:04 -07002682 CreateSessionDescriptionAndReference(1, 1);
deadbeefab9b2d12015-10-14 11:33:11 -07002683 EXPECT_TRUE(DoSetRemoteDescription(desc_ms1.release()));
2684 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
2685 reference_collection_));
2686
2687 // Add extra audio and video tracks to the same MediaStream.
kwibergd1fe2812016-04-27 06:47:29 -07002688 std::unique_ptr<SessionDescriptionInterface> desc_ms1_two_tracks =
kwiberg2bbff992016-03-16 11:03:04 -07002689 CreateSessionDescriptionAndReference(2, 2);
deadbeefab9b2d12015-10-14 11:33:11 -07002690 EXPECT_TRUE(DoSetRemoteDescription(desc_ms1_two_tracks.release()));
2691 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
2692 reference_collection_));
zhihuang9763d562016-08-05 11:14:50 -07002693 rtc::scoped_refptr<AudioTrackInterface> audio_track2 =
perkjd61bf802016-03-24 03:16:19 -07002694 observer_.remote_streams()->at(0)->GetAudioTracks()[1];
2695 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, audio_track2->state());
zhihuang9763d562016-08-05 11:14:50 -07002696 rtc::scoped_refptr<VideoTrackInterface> video_track2 =
perkjd61bf802016-03-24 03:16:19 -07002697 observer_.remote_streams()->at(0)->GetVideoTracks()[1];
2698 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, video_track2->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002699
2700 // Remove the extra audio and video tracks.
kwibergd1fe2812016-04-27 06:47:29 -07002701 std::unique_ptr<SessionDescriptionInterface> desc_ms2 =
kwiberg2bbff992016-03-16 11:03:04 -07002702 CreateSessionDescriptionAndReference(1, 1);
perkjd61bf802016-03-24 03:16:19 -07002703 MockTrackObserver audio_track_observer(audio_track2);
2704 MockTrackObserver video_track_observer(video_track2);
2705
2706 EXPECT_CALL(audio_track_observer, OnChanged()).Times(Exactly(1));
2707 EXPECT_CALL(video_track_observer, OnChanged()).Times(Exactly(1));
deadbeefab9b2d12015-10-14 11:33:11 -07002708 EXPECT_TRUE(DoSetRemoteDescription(desc_ms2.release()));
2709 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
2710 reference_collection_));
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002711 // Track state may be updated asynchronously.
perkjd61bf802016-03-24 03:16:19 -07002712 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002713 audio_track2->state(), kTimeout);
2714 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
2715 video_track2->state(), kTimeout);
deadbeefab9b2d12015-10-14 11:33:11 -07002716}
2717
2718// This tests that remote tracks are ended if a local session description is set
2719// that rejects the media content type.
2720TEST_F(PeerConnectionInterfaceTest, RejectMediaContent) {
2721 FakeConstraints constraints;
2722 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2723 true);
2724 CreatePeerConnection(&constraints);
2725 // First create and set a remote offer, then reject its video content in our
2726 // answer.
2727 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2728 ASSERT_EQ(1u, observer_.remote_streams()->count());
2729 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2730 ASSERT_EQ(1u, remote_stream->GetVideoTracks().size());
2731 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2732
2733 rtc::scoped_refptr<webrtc::VideoTrackInterface> remote_video =
2734 remote_stream->GetVideoTracks()[0];
2735 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, remote_video->state());
2736 rtc::scoped_refptr<webrtc::AudioTrackInterface> remote_audio =
2737 remote_stream->GetAudioTracks()[0];
2738 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, remote_audio->state());
2739
kwibergd1fe2812016-04-27 06:47:29 -07002740 std::unique_ptr<SessionDescriptionInterface> local_answer;
kwiberg2bbff992016-03-16 11:03:04 -07002741 EXPECT_TRUE(DoCreateAnswer(&local_answer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07002742 cricket::ContentInfo* video_info =
2743 local_answer->description()->GetContentByName("video");
2744 video_info->rejected = true;
2745 EXPECT_TRUE(DoSetLocalDescription(local_answer.release()));
2746 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kEnded, remote_video->state());
2747 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, remote_audio->state());
2748
2749 // Now create an offer where we reject both video and audio.
kwibergd1fe2812016-04-27 06:47:29 -07002750 std::unique_ptr<SessionDescriptionInterface> local_offer;
kwiberg2bbff992016-03-16 11:03:04 -07002751 EXPECT_TRUE(DoCreateOffer(&local_offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07002752 video_info = local_offer->description()->GetContentByName("video");
2753 ASSERT_TRUE(video_info != nullptr);
2754 video_info->rejected = true;
2755 cricket::ContentInfo* audio_info =
2756 local_offer->description()->GetContentByName("audio");
2757 ASSERT_TRUE(audio_info != nullptr);
2758 audio_info->rejected = true;
2759 EXPECT_TRUE(DoSetLocalDescription(local_offer.release()));
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002760 // Track state may be updated asynchronously.
perkjd61bf802016-03-24 03:16:19 -07002761 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002762 remote_audio->state(), kTimeout);
2763 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
2764 remote_video->state(), kTimeout);
deadbeefab9b2d12015-10-14 11:33:11 -07002765}
2766
2767// This tests that we won't crash if the remote track has been removed outside
2768// of PeerConnection and then PeerConnection tries to reject the track.
2769TEST_F(PeerConnectionInterfaceTest, RemoveTrackThenRejectMediaContent) {
2770 FakeConstraints constraints;
2771 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2772 true);
2773 CreatePeerConnection(&constraints);
2774 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2775 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2776 remote_stream->RemoveTrack(remote_stream->GetVideoTracks()[0]);
2777 remote_stream->RemoveTrack(remote_stream->GetAudioTracks()[0]);
2778
kwibergd1fe2812016-04-27 06:47:29 -07002779 std::unique_ptr<SessionDescriptionInterface> local_answer(
deadbeefab9b2d12015-10-14 11:33:11 -07002780 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
2781 kSdpStringWithStream1, nullptr));
2782 cricket::ContentInfo* video_info =
2783 local_answer->description()->GetContentByName("video");
2784 video_info->rejected = true;
2785 cricket::ContentInfo* audio_info =
2786 local_answer->description()->GetContentByName("audio");
2787 audio_info->rejected = true;
2788 EXPECT_TRUE(DoSetLocalDescription(local_answer.release()));
2789
2790 // No crash is a pass.
2791}
2792
deadbeef5e97fb52015-10-15 12:49:08 -07002793// This tests that if a recvonly remote description is set, no remote streams
2794// will be created, even if the description contains SSRCs/MSIDs.
2795// See: https://code.google.com/p/webrtc/issues/detail?id=5054
2796TEST_F(PeerConnectionInterfaceTest, RecvonlyDescriptionDoesntCreateStream) {
2797 FakeConstraints constraints;
2798 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2799 true);
2800 CreatePeerConnection(&constraints);
2801
2802 std::string recvonly_offer = kSdpStringWithStream1;
2803 rtc::replace_substrs(kSendrecv, strlen(kSendrecv), kRecvonly,
2804 strlen(kRecvonly), &recvonly_offer);
2805 CreateAndSetRemoteOffer(recvonly_offer);
2806
2807 EXPECT_EQ(0u, observer_.remote_streams()->count());
2808}
2809
deadbeefab9b2d12015-10-14 11:33:11 -07002810// This tests that a default MediaStream is created if a remote session
2811// description doesn't contain any streams and no MSID support.
2812// It also tests that the default stream is updated if a video m-line is added
2813// in a subsequent session description.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002814TEST_F(PeerConnectionInterfaceTest, SdpWithoutMsidCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002815 FakeConstraints constraints;
2816 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2817 true);
2818 CreatePeerConnection(&constraints);
2819 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2820
2821 ASSERT_EQ(1u, observer_.remote_streams()->count());
2822 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2823
2824 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2825 EXPECT_EQ(0u, remote_stream->GetVideoTracks().size());
2826 EXPECT_EQ("default", remote_stream->label());
2827
2828 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2829 ASSERT_EQ(1u, observer_.remote_streams()->count());
2830 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2831 EXPECT_EQ("defaulta0", remote_stream->GetAudioTracks()[0]->id());
deadbeef884f5852016-01-15 09:20:04 -08002832 EXPECT_EQ(MediaStreamTrackInterface::kLive,
2833 remote_stream->GetAudioTracks()[0]->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002834 ASSERT_EQ(1u, remote_stream->GetVideoTracks().size());
2835 EXPECT_EQ("defaultv0", remote_stream->GetVideoTracks()[0]->id());
deadbeef884f5852016-01-15 09:20:04 -08002836 EXPECT_EQ(MediaStreamTrackInterface::kLive,
2837 remote_stream->GetVideoTracks()[0]->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002838}
2839
2840// This tests that a default MediaStream is created if a remote session
2841// description doesn't contain any streams and media direction is send only.
2842TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002843 SendOnlySdpWithoutMsidCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002844 FakeConstraints constraints;
2845 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2846 true);
2847 CreatePeerConnection(&constraints);
2848 CreateAndSetRemoteOffer(kSdpStringSendOnlyWithoutStreams);
2849
2850 ASSERT_EQ(1u, observer_.remote_streams()->count());
2851 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2852
2853 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2854 EXPECT_EQ(1u, remote_stream->GetVideoTracks().size());
2855 EXPECT_EQ("default", remote_stream->label());
2856}
2857
2858// This tests that it won't crash when PeerConnection tries to remove
2859// a remote track that as already been removed from the MediaStream.
2860TEST_F(PeerConnectionInterfaceTest, RemoveAlreadyGoneRemoteStream) {
2861 FakeConstraints constraints;
2862 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2863 true);
2864 CreatePeerConnection(&constraints);
2865 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2866 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2867 remote_stream->RemoveTrack(remote_stream->GetAudioTracks()[0]);
2868 remote_stream->RemoveTrack(remote_stream->GetVideoTracks()[0]);
2869
2870 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2871
2872 // No crash is a pass.
2873}
2874
2875// This tests that a default MediaStream is created if the remote session
2876// description doesn't contain any streams and don't contain an indication if
2877// MSID is supported.
2878TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002879 SdpWithoutMsidAndStreamsCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002880 FakeConstraints constraints;
2881 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2882 true);
2883 CreatePeerConnection(&constraints);
2884 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2885
2886 ASSERT_EQ(1u, observer_.remote_streams()->count());
2887 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2888 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2889 EXPECT_EQ(1u, remote_stream->GetVideoTracks().size());
2890}
2891
2892// This tests that a default MediaStream is not created if the remote session
2893// description doesn't contain any streams but does support MSID.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002894TEST_F(PeerConnectionInterfaceTest, SdpWithMsidDontCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002895 FakeConstraints constraints;
2896 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2897 true);
2898 CreatePeerConnection(&constraints);
2899 CreateAndSetRemoteOffer(kSdpStringWithMsidWithoutStreams);
2900 EXPECT_EQ(0u, observer_.remote_streams()->count());
2901}
2902
deadbeefbda7e0b2015-12-08 17:13:40 -08002903// This tests that when setting a new description, the old default tracks are
2904// not destroyed and recreated.
2905// See: https://bugs.chromium.org/p/webrtc/issues/detail?id=5250
Stefan Holmer102362b2016-03-18 09:39:07 +01002906TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002907 DefaultTracksNotDestroyedAndRecreated) {
deadbeefbda7e0b2015-12-08 17:13:40 -08002908 FakeConstraints constraints;
2909 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2910 true);
2911 CreatePeerConnection(&constraints);
2912 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2913
2914 ASSERT_EQ(1u, observer_.remote_streams()->count());
2915 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2916 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2917
2918 // Set the track to "disabled", then set a new description and ensure the
2919 // track is still disabled, which ensures it hasn't been recreated.
2920 remote_stream->GetAudioTracks()[0]->set_enabled(false);
2921 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2922 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2923 EXPECT_FALSE(remote_stream->GetAudioTracks()[0]->enabled());
2924}
2925
deadbeefab9b2d12015-10-14 11:33:11 -07002926// This tests that a default MediaStream is not created if a remote session
2927// description is updated to not have any MediaStreams.
2928TEST_F(PeerConnectionInterfaceTest, VerifyDefaultStreamIsNotCreated) {
2929 FakeConstraints constraints;
2930 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2931 true);
2932 CreatePeerConnection(&constraints);
2933 CreateAndSetRemoteOffer(kSdpStringWithStream1);
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002934 rtc::scoped_refptr<StreamCollection> reference(CreateStreamCollection(1, 1));
deadbeefab9b2d12015-10-14 11:33:11 -07002935 EXPECT_TRUE(
2936 CompareStreamCollections(observer_.remote_streams(), reference.get()));
2937
2938 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2939 EXPECT_EQ(0u, observer_.remote_streams()->count());
2940}
2941
2942// This tests that an RtpSender is created when the local description is set
2943// after adding a local stream.
2944// TODO(deadbeef): This test and the one below it need to be updated when
2945// an RtpSender's lifetime isn't determined by when a local description is set.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002946TEST_F(PeerConnectionInterfaceTest, LocalDescriptionChanged) {
deadbeefab9b2d12015-10-14 11:33:11 -07002947 FakeConstraints constraints;
2948 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2949 true);
2950 CreatePeerConnection(&constraints);
deadbeefab9b2d12015-10-14 11:33:11 -07002951
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002952 // Create an offer with 1 stream with 2 tracks of each type.
2953 rtc::scoped_refptr<StreamCollection> stream_collection =
2954 CreateStreamCollection(1, 2);
2955 pc_->AddStream(stream_collection->at(0));
2956 std::unique_ptr<SessionDescriptionInterface> offer;
2957 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2958 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
deadbeefab9b2d12015-10-14 11:33:11 -07002959
deadbeefab9b2d12015-10-14 11:33:11 -07002960 auto senders = pc_->GetSenders();
2961 EXPECT_EQ(4u, senders.size());
2962 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2963 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2964 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[1]));
2965 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[1]));
2966
2967 // Remove an audio and video track.
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002968 pc_->RemoveStream(stream_collection->at(0));
2969 stream_collection = CreateStreamCollection(1, 1);
2970 pc_->AddStream(stream_collection->at(0));
2971 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2972 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
2973
deadbeefab9b2d12015-10-14 11:33:11 -07002974 senders = pc_->GetSenders();
2975 EXPECT_EQ(2u, senders.size());
2976 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2977 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2978 EXPECT_FALSE(ContainsSender(senders, kAudioTracks[1]));
2979 EXPECT_FALSE(ContainsSender(senders, kVideoTracks[1]));
2980}
2981
2982// This tests that an RtpSender is created when the local description is set
2983// before adding a local stream.
2984TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002985 AddLocalStreamAfterLocalDescriptionChanged) {
deadbeefab9b2d12015-10-14 11:33:11 -07002986 FakeConstraints constraints;
2987 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2988 true);
2989 CreatePeerConnection(&constraints);
deadbeefab9b2d12015-10-14 11:33:11 -07002990
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002991 rtc::scoped_refptr<StreamCollection> stream_collection =
2992 CreateStreamCollection(1, 2);
2993 // Add a stream to create the offer, but remove it afterwards.
2994 pc_->AddStream(stream_collection->at(0));
2995 std::unique_ptr<SessionDescriptionInterface> offer;
2996 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2997 pc_->RemoveStream(stream_collection->at(0));
deadbeefab9b2d12015-10-14 11:33:11 -07002998
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002999 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
deadbeefab9b2d12015-10-14 11:33:11 -07003000 auto senders = pc_->GetSenders();
3001 EXPECT_EQ(0u, senders.size());
3002
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003003 pc_->AddStream(stream_collection->at(0));
deadbeefab9b2d12015-10-14 11:33:11 -07003004 senders = pc_->GetSenders();
3005 EXPECT_EQ(4u, senders.size());
3006 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
3007 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
3008 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[1]));
3009 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[1]));
3010}
3011
3012// This tests that the expected behavior occurs if the SSRC on a local track is
3013// changed when SetLocalDescription is called.
3014TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07003015 ChangeSsrcOnTrackInLocalSessionDescription) {
deadbeefab9b2d12015-10-14 11:33:11 -07003016 FakeConstraints constraints;
3017 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
3018 true);
3019 CreatePeerConnection(&constraints);
deadbeefab9b2d12015-10-14 11:33:11 -07003020
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003021 rtc::scoped_refptr<StreamCollection> stream_collection =
3022 CreateStreamCollection(2, 1);
3023 pc_->AddStream(stream_collection->at(0));
3024 std::unique_ptr<SessionDescriptionInterface> offer;
3025 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
3026 // Grab a copy of the offer before it gets passed into the PC.
3027 std::unique_ptr<JsepSessionDescription> modified_offer(
3028 new JsepSessionDescription(JsepSessionDescription::kOffer));
3029 modified_offer->Initialize(offer->description()->Copy(), offer->session_id(),
3030 offer->session_version());
3031 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
deadbeefab9b2d12015-10-14 11:33:11 -07003032
deadbeefab9b2d12015-10-14 11:33:11 -07003033 auto senders = pc_->GetSenders();
3034 EXPECT_EQ(2u, senders.size());
3035 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
3036 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
3037
3038 // Change the ssrc of the audio and video track.
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003039 cricket::MediaContentDescription* desc =
3040 cricket::GetFirstAudioContentDescription(modified_offer->description());
3041 ASSERT_TRUE(desc != NULL);
3042 for (StreamParams& stream : desc->mutable_streams()) {
3043 for (unsigned int& ssrc : stream.ssrcs) {
3044 ++ssrc;
3045 }
3046 }
deadbeefab9b2d12015-10-14 11:33:11 -07003047
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003048 desc =
3049 cricket::GetFirstVideoContentDescription(modified_offer->description());
3050 ASSERT_TRUE(desc != NULL);
3051 for (StreamParams& stream : desc->mutable_streams()) {
3052 for (unsigned int& ssrc : stream.ssrcs) {
3053 ++ssrc;
3054 }
3055 }
3056
3057 EXPECT_TRUE(DoSetLocalDescription(modified_offer.release()));
deadbeefab9b2d12015-10-14 11:33:11 -07003058 senders = pc_->GetSenders();
3059 EXPECT_EQ(2u, senders.size());
3060 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
3061 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
3062 // TODO(deadbeef): Once RtpSenders expose parameters, check that the SSRC
3063 // changed.
3064}
3065
3066// This tests that the expected behavior occurs if a new session description is
3067// set with the same tracks, but on a different MediaStream.
Stefan Holmer55d6e7c2016-03-17 16:26:40 +01003068TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07003069 SignalSameTracksInSeparateMediaStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07003070 FakeConstraints constraints;
3071 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
3072 true);
3073 CreatePeerConnection(&constraints);
deadbeefab9b2d12015-10-14 11:33:11 -07003074
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003075 rtc::scoped_refptr<StreamCollection> stream_collection =
3076 CreateStreamCollection(2, 1);
3077 pc_->AddStream(stream_collection->at(0));
3078 std::unique_ptr<SessionDescriptionInterface> offer;
3079 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
3080 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
deadbeefab9b2d12015-10-14 11:33:11 -07003081
deadbeefab9b2d12015-10-14 11:33:11 -07003082 auto senders = pc_->GetSenders();
3083 EXPECT_EQ(2u, senders.size());
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003084 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0], kStreams[0]));
3085 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0], kStreams[0]));
deadbeefab9b2d12015-10-14 11:33:11 -07003086
3087 // Add a new MediaStream but with the same tracks as in the first stream.
3088 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream_1(
3089 webrtc::MediaStream::Create(kStreams[1]));
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003090 stream_1->AddTrack(stream_collection->at(0)->GetVideoTracks()[0]);
3091 stream_1->AddTrack(stream_collection->at(0)->GetAudioTracks()[0]);
deadbeefab9b2d12015-10-14 11:33:11 -07003092 pc_->AddStream(stream_1);
3093
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003094 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
3095 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
deadbeefab9b2d12015-10-14 11:33:11 -07003096
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003097 auto new_senders = pc_->GetSenders();
3098 // Should be the same senders as before, but with updated stream id.
3099 // Note that this behavior is subject to change in the future.
3100 // We may decide the PC should ignore existing tracks in AddStream.
3101 EXPECT_EQ(senders, new_senders);
3102 EXPECT_TRUE(ContainsSender(new_senders, kAudioTracks[0], kStreams[1]));
3103 EXPECT_TRUE(ContainsSender(new_senders, kVideoTracks[0], kStreams[1]));
deadbeefab9b2d12015-10-14 11:33:11 -07003104}
3105
zhihuang81c3a032016-11-17 12:06:24 -08003106// This tests that PeerConnectionObserver::OnAddTrack is correctly called.
3107TEST_F(PeerConnectionInterfaceTest, OnAddTrackCallback) {
3108 FakeConstraints constraints;
3109 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
3110 true);
3111 CreatePeerConnection(&constraints);
3112 CreateAndSetRemoteOffer(kSdpStringWithStream1AudioTrackOnly);
3113 EXPECT_EQ(observer_.num_added_tracks_, 1);
3114 EXPECT_EQ(observer_.last_added_track_label_, kAudioTracks[0]);
3115
3116 // Create and set the updated remote SDP.
3117 CreateAndSetRemoteOffer(kSdpStringWithStream1);
3118 EXPECT_EQ(observer_.num_added_tracks_, 2);
3119 EXPECT_EQ(observer_.last_added_track_label_, kVideoTracks[0]);
3120}
3121
deadbeefd1a38b52016-12-10 13:15:33 -08003122// Test that when SetConfiguration is called and the configuration is
3123// changing, the next offer causes an ICE restart.
3124TEST_F(PeerConnectionInterfaceTest, SetConfigurationCausingIceRetart) {
3125 PeerConnectionInterface::RTCConfiguration config;
3126 config.type = PeerConnectionInterface::kRelay;
3127 // Need to pass default constraints to prevent disabling of DTLS...
3128 FakeConstraints default_constraints;
3129 CreatePeerConnection(config, &default_constraints);
3130 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
3131
3132 // Do initial offer/answer so there's something to restart.
3133 CreateOfferAsLocalDescription();
3134 CreateAnswerAsRemoteDescription(kSdpStringWithStream1);
3135
3136 // Grab the ufrags.
3137 std::vector<std::string> initial_ufrags = GetUfrags(pc_->local_description());
3138
3139 // Change ICE policy, which should trigger an ICE restart on the next offer.
3140 config.type = PeerConnectionInterface::kAll;
3141 EXPECT_TRUE(pc_->SetConfiguration(config));
3142 CreateOfferAsLocalDescription();
3143
3144 // Grab the new ufrags.
3145 std::vector<std::string> subsequent_ufrags =
3146 GetUfrags(pc_->local_description());
3147
3148 // Sanity check.
3149 EXPECT_EQ(initial_ufrags.size(), subsequent_ufrags.size());
3150 // Check that each ufrag is different.
3151 for (int i = 0; i < static_cast<int>(initial_ufrags.size()); ++i) {
3152 EXPECT_NE(initial_ufrags[i], subsequent_ufrags[i]);
3153 }
3154}
3155
3156// Test that when SetConfiguration is called and the configuration *isn't*
3157// changing, the next offer does *not* cause an ICE restart.
3158TEST_F(PeerConnectionInterfaceTest, SetConfigurationNotCausingIceRetart) {
3159 PeerConnectionInterface::RTCConfiguration config;
3160 config.type = PeerConnectionInterface::kRelay;
3161 // Need to pass default constraints to prevent disabling of DTLS...
3162 FakeConstraints default_constraints;
3163 CreatePeerConnection(config, &default_constraints);
3164 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
3165
3166 // Do initial offer/answer so there's something to restart.
3167 CreateOfferAsLocalDescription();
3168 CreateAnswerAsRemoteDescription(kSdpStringWithStream1);
3169
3170 // Grab the ufrags.
3171 std::vector<std::string> initial_ufrags = GetUfrags(pc_->local_description());
3172
3173 // Call SetConfiguration with a config identical to what the PC was
3174 // constructed with.
3175 EXPECT_TRUE(pc_->SetConfiguration(config));
3176 CreateOfferAsLocalDescription();
3177
3178 // Grab the new ufrags.
3179 std::vector<std::string> subsequent_ufrags =
3180 GetUfrags(pc_->local_description());
3181
3182 EXPECT_EQ(initial_ufrags, subsequent_ufrags);
3183}
3184
3185// Test for a weird corner case scenario:
3186// 1. Audio/video session established.
3187// 2. SetConfiguration changes ICE config; ICE restart needed.
3188// 3. ICE restart initiated by remote peer, but only for one m= section.
3189// 4. Next createOffer should initiate an ICE restart, but only for the other
3190// m= section; it would be pointless to do an ICE restart for the m= section
3191// that was already restarted.
3192TEST_F(PeerConnectionInterfaceTest, SetConfigurationCausingPartialIceRestart) {
3193 PeerConnectionInterface::RTCConfiguration config;
3194 config.type = PeerConnectionInterface::kRelay;
3195 // Need to pass default constraints to prevent disabling of DTLS...
3196 FakeConstraints default_constraints;
3197 CreatePeerConnection(config, &default_constraints);
3198 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
3199
3200 // Do initial offer/answer so there's something to restart.
3201 CreateOfferAsLocalDescription();
3202 CreateAnswerAsRemoteDescription(kSdpStringWithStream1);
3203
3204 // Change ICE policy, which should set the "needs-ice-restart" flag.
3205 config.type = PeerConnectionInterface::kAll;
3206 EXPECT_TRUE(pc_->SetConfiguration(config));
3207
3208 // Do ICE restart for the first m= section, initiated by remote peer.
3209 webrtc::JsepSessionDescription* remote_offer =
3210 new webrtc::JsepSessionDescription(SessionDescriptionInterface::kOffer);
3211 EXPECT_TRUE(remote_offer->Initialize(kSdpStringWithStream1, nullptr));
3212 remote_offer->description()->transport_infos()[0].description.ice_ufrag =
3213 "modified";
3214 EXPECT_TRUE(DoSetRemoteDescription(remote_offer));
3215 CreateAnswerAsLocalDescription();
3216
3217 // Grab the ufrags.
3218 std::vector<std::string> initial_ufrags = GetUfrags(pc_->local_description());
3219 ASSERT_EQ(2, initial_ufrags.size());
3220
3221 // Create offer and grab the new ufrags.
3222 CreateOfferAsLocalDescription();
3223 std::vector<std::string> subsequent_ufrags =
3224 GetUfrags(pc_->local_description());
3225 ASSERT_EQ(2, subsequent_ufrags.size());
3226
3227 // Ensure that only the ufrag for the second m= section changed.
3228 EXPECT_EQ(initial_ufrags[0], subsequent_ufrags[0]);
3229 EXPECT_NE(initial_ufrags[1], subsequent_ufrags[1]);
3230}
3231
deadbeeffe4a8a42016-12-20 17:56:17 -08003232// Tests that the methods to return current/pending descriptions work as
3233// expected at different points in the offer/answer exchange. This test does
3234// one offer/answer exchange as the offerer, then another as the answerer.
3235TEST_F(PeerConnectionInterfaceTest, CurrentAndPendingDescriptions) {
3236 // This disables DTLS so we can apply an answer to ourselves.
3237 CreatePeerConnection();
3238
3239 // Create initial local offer and get SDP (which will also be used as
3240 // answer/pranswer);
3241 std::unique_ptr<SessionDescriptionInterface> offer;
3242 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
3243 std::string sdp;
3244 EXPECT_TRUE(offer->ToString(&sdp));
3245
3246 // Set local offer.
3247 SessionDescriptionInterface* local_offer = offer.release();
3248 EXPECT_TRUE(DoSetLocalDescription(local_offer));
3249 EXPECT_EQ(local_offer, pc_->pending_local_description());
3250 EXPECT_EQ(nullptr, pc_->pending_remote_description());
3251 EXPECT_EQ(nullptr, pc_->current_local_description());
3252 EXPECT_EQ(nullptr, pc_->current_remote_description());
3253
3254 // Set remote pranswer.
3255 SessionDescriptionInterface* remote_pranswer =
3256 webrtc::CreateSessionDescription(SessionDescriptionInterface::kPrAnswer,
3257 sdp, nullptr);
3258 EXPECT_TRUE(DoSetRemoteDescription(remote_pranswer));
3259 EXPECT_EQ(local_offer, pc_->pending_local_description());
3260 EXPECT_EQ(remote_pranswer, pc_->pending_remote_description());
3261 EXPECT_EQ(nullptr, pc_->current_local_description());
3262 EXPECT_EQ(nullptr, pc_->current_remote_description());
3263
3264 // Set remote answer.
3265 SessionDescriptionInterface* remote_answer = webrtc::CreateSessionDescription(
3266 SessionDescriptionInterface::kAnswer, sdp, nullptr);
3267 EXPECT_TRUE(DoSetRemoteDescription(remote_answer));
3268 EXPECT_EQ(nullptr, pc_->pending_local_description());
3269 EXPECT_EQ(nullptr, pc_->pending_remote_description());
3270 EXPECT_EQ(local_offer, pc_->current_local_description());
3271 EXPECT_EQ(remote_answer, pc_->current_remote_description());
3272
3273 // Set remote offer.
3274 SessionDescriptionInterface* remote_offer = webrtc::CreateSessionDescription(
3275 SessionDescriptionInterface::kOffer, sdp, nullptr);
3276 EXPECT_TRUE(DoSetRemoteDescription(remote_offer));
3277 EXPECT_EQ(remote_offer, pc_->pending_remote_description());
3278 EXPECT_EQ(nullptr, pc_->pending_local_description());
3279 EXPECT_EQ(local_offer, pc_->current_local_description());
3280 EXPECT_EQ(remote_answer, pc_->current_remote_description());
3281
3282 // Set local pranswer.
3283 SessionDescriptionInterface* local_pranswer =
3284 webrtc::CreateSessionDescription(SessionDescriptionInterface::kPrAnswer,
3285 sdp, nullptr);
3286 EXPECT_TRUE(DoSetLocalDescription(local_pranswer));
3287 EXPECT_EQ(remote_offer, pc_->pending_remote_description());
3288 EXPECT_EQ(local_pranswer, pc_->pending_local_description());
3289 EXPECT_EQ(local_offer, pc_->current_local_description());
3290 EXPECT_EQ(remote_answer, pc_->current_remote_description());
3291
3292 // Set local answer.
3293 SessionDescriptionInterface* local_answer = webrtc::CreateSessionDescription(
3294 SessionDescriptionInterface::kAnswer, sdp, nullptr);
3295 EXPECT_TRUE(DoSetLocalDescription(local_answer));
3296 EXPECT_EQ(nullptr, pc_->pending_remote_description());
3297 EXPECT_EQ(nullptr, pc_->pending_local_description());
3298 EXPECT_EQ(remote_offer, pc_->current_remote_description());
3299 EXPECT_EQ(local_answer, pc_->current_local_description());
3300}
3301
zhihuang77985012017-02-07 15:45:16 -08003302// Tests that it won't crash when calling StartRtcEventLog or StopRtcEventLog
3303// after the PeerConnection is closed.
3304TEST_F(PeerConnectionInterfaceTest,
3305 StartAndStopLoggingAfterPeerConnectionClosed) {
3306 CreatePeerConnection();
3307 // The RtcEventLog will be reset when the PeerConnection is closed.
3308 pc_->Close();
3309
3310 rtc::PlatformFile file = 0;
3311 int64_t max_size_bytes = 1024;
3312 EXPECT_FALSE(pc_->StartRtcEventLog(file, max_size_bytes));
3313 pc_->StopRtcEventLog();
3314}
3315
deadbeef30952b42017-04-21 02:41:29 -07003316// Test that generated offers/answers include "ice-option:trickle".
3317TEST_F(PeerConnectionInterfaceTest, OffersAndAnswersHaveTrickleIceOption) {
3318 CreatePeerConnection();
3319
3320 // First, create an offer with audio/video.
3321 FakeConstraints constraints;
3322 constraints.SetMandatoryReceiveAudio(true);
3323 constraints.SetMandatoryReceiveVideo(true);
3324 std::unique_ptr<SessionDescriptionInterface> offer;
3325 ASSERT_TRUE(DoCreateOffer(&offer, &constraints));
3326 cricket::SessionDescription* desc = offer->description();
3327 ASSERT_EQ(2u, desc->transport_infos().size());
3328 EXPECT_TRUE(desc->transport_infos()[0].description.HasOption("trickle"));
3329 EXPECT_TRUE(desc->transport_infos()[1].description.HasOption("trickle"));
3330
3331 // Apply the offer as a remote description, then create an answer.
3332 EXPECT_TRUE(DoSetRemoteDescription(offer.release()));
3333 std::unique_ptr<SessionDescriptionInterface> answer;
3334 ASSERT_TRUE(DoCreateAnswer(&answer, &constraints));
3335 desc = answer->description();
3336 ASSERT_EQ(2u, desc->transport_infos().size());
3337 EXPECT_TRUE(desc->transport_infos()[0].description.HasOption("trickle"));
3338 EXPECT_TRUE(desc->transport_infos()[1].description.HasOption("trickle"));
3339}
3340
deadbeef1dcb1642017-03-29 21:08:16 -07003341// Test that ICE renomination isn't offered if it's not enabled in the PC's
3342// RTCConfiguration.
3343TEST_F(PeerConnectionInterfaceTest, IceRenominationNotOffered) {
3344 PeerConnectionInterface::RTCConfiguration config;
3345 config.enable_ice_renomination = false;
3346 CreatePeerConnection(config, nullptr);
3347 AddVoiceStream("foo");
3348
3349 std::unique_ptr<SessionDescriptionInterface> offer;
3350 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
3351 cricket::SessionDescription* desc = offer->description();
3352 EXPECT_EQ(1u, desc->transport_infos().size());
3353 EXPECT_FALSE(
3354 desc->transport_infos()[0].description.GetIceParameters().renomination);
3355}
3356
3357// Test that the ICE renomination option is present in generated offers/answers
3358// if it's enabled in the PC's RTCConfiguration.
3359TEST_F(PeerConnectionInterfaceTest, IceRenominationOptionInOfferAndAnswer) {
3360 PeerConnectionInterface::RTCConfiguration config;
3361 config.enable_ice_renomination = true;
3362 CreatePeerConnection(config, nullptr);
3363 AddVoiceStream("foo");
3364
3365 std::unique_ptr<SessionDescriptionInterface> offer;
3366 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
3367 cricket::SessionDescription* desc = offer->description();
3368 EXPECT_EQ(1u, desc->transport_infos().size());
3369 EXPECT_TRUE(
3370 desc->transport_infos()[0].description.GetIceParameters().renomination);
3371
3372 // Set the offer as a remote description, then create an answer and ensure it
3373 // has the renomination flag too.
3374 EXPECT_TRUE(DoSetRemoteDescription(offer.release()));
3375 std::unique_ptr<SessionDescriptionInterface> answer;
3376 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
3377 desc = answer->description();
3378 EXPECT_EQ(1u, desc->transport_infos().size());
3379 EXPECT_TRUE(
3380 desc->transport_infos()[0].description.GetIceParameters().renomination);
3381}
3382
3383// Test that if CreateOffer is called with the deprecated "offer to receive
3384// audio/video" constraints, they're processed and result in an offer with
3385// audio/video sections just as if RTCOfferAnswerOptions had been used.
3386TEST_F(PeerConnectionInterfaceTest, CreateOfferWithOfferToReceiveConstraints) {
3387 CreatePeerConnection();
3388
3389 FakeConstraints constraints;
3390 constraints.SetMandatoryReceiveAudio(true);
3391 constraints.SetMandatoryReceiveVideo(true);
3392 std::unique_ptr<SessionDescriptionInterface> offer;
3393 ASSERT_TRUE(DoCreateOffer(&offer, &constraints));
3394
3395 cricket::SessionDescription* desc = offer->description();
3396 const cricket::ContentInfo* audio = cricket::GetFirstAudioContent(desc);
3397 const cricket::ContentInfo* video = cricket::GetFirstVideoContent(desc);
3398 ASSERT_NE(nullptr, audio);
3399 ASSERT_NE(nullptr, video);
3400 EXPECT_FALSE(audio->rejected);
3401 EXPECT_FALSE(video->rejected);
3402}
3403
3404// Test that if CreateAnswer is called with the deprecated "offer to receive
3405// audio/video" constraints, they're processed and can be used to reject an
3406// offered m= section just as can be done with RTCOfferAnswerOptions;
3407TEST_F(PeerConnectionInterfaceTest, CreateAnswerWithOfferToReceiveConstraints) {
3408 CreatePeerConnection();
3409
3410 // First, create an offer with audio/video and apply it as a remote
3411 // description.
3412 FakeConstraints constraints;
3413 constraints.SetMandatoryReceiveAudio(true);
3414 constraints.SetMandatoryReceiveVideo(true);
3415 std::unique_ptr<SessionDescriptionInterface> offer;
3416 ASSERT_TRUE(DoCreateOffer(&offer, &constraints));
3417 EXPECT_TRUE(DoSetRemoteDescription(offer.release()));
3418
3419 // Now create answer that rejects audio/video.
3420 constraints.SetMandatoryReceiveAudio(false);
3421 constraints.SetMandatoryReceiveVideo(false);
3422 std::unique_ptr<SessionDescriptionInterface> answer;
3423 ASSERT_TRUE(DoCreateAnswer(&answer, &constraints));
3424
3425 cricket::SessionDescription* desc = answer->description();
3426 const cricket::ContentInfo* audio = cricket::GetFirstAudioContent(desc);
3427 const cricket::ContentInfo* video = cricket::GetFirstVideoContent(desc);
3428 ASSERT_NE(nullptr, audio);
3429 ASSERT_NE(nullptr, video);
3430 EXPECT_TRUE(audio->rejected);
3431 EXPECT_TRUE(video->rejected);
3432}
3433
3434#ifdef HAVE_SCTP
3435#define MAYBE_DataChannelOnlyOfferWithMaxBundlePolicy \
3436 DataChannelOnlyOfferWithMaxBundlePolicy
3437#else
3438#define MAYBE_DataChannelOnlyOfferWithMaxBundlePolicy \
3439 DISABLED_DataChannelOnlyOfferWithMaxBundlePolicy
3440#endif
3441
3442// Test that negotiation can succeed with a data channel only, and with the max
3443// bundle policy. Previously there was a bug that prevented this.
3444TEST_F(PeerConnectionInterfaceTest,
3445 MAYBE_DataChannelOnlyOfferWithMaxBundlePolicy) {
3446 PeerConnectionInterface::RTCConfiguration config;
3447 config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
3448 CreatePeerConnection(config, nullptr);
3449
3450 // First, create an offer with only a data channel and apply it as a remote
3451 // description.
3452 pc_->CreateDataChannel("test", nullptr);
3453 std::unique_ptr<SessionDescriptionInterface> offer;
3454 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
3455 EXPECT_TRUE(DoSetRemoteDescription(offer.release()));
3456
3457 // Create and set answer as well.
3458 std::unique_ptr<SessionDescriptionInterface> answer;
3459 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
3460 EXPECT_TRUE(DoSetLocalDescription(answer.release()));
3461}
3462
zstein4b979802017-06-02 14:37:37 -07003463TEST_F(PeerConnectionInterfaceTest, SetBitrateWithoutMinSucceeds) {
3464 CreatePeerConnection();
3465 PeerConnectionInterface::BitrateParameters bitrate;
3466 bitrate.current_bitrate_bps = rtc::Optional<int>(100000);
3467 EXPECT_TRUE(pc_->SetBitrate(bitrate).ok());
3468}
3469
3470TEST_F(PeerConnectionInterfaceTest, SetBitrateNegativeMinFails) {
3471 CreatePeerConnection();
3472 PeerConnectionInterface::BitrateParameters bitrate;
3473 bitrate.min_bitrate_bps = rtc::Optional<int>(-1);
3474 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3475}
3476
3477TEST_F(PeerConnectionInterfaceTest, SetBitrateCurrentLessThanMinFails) {
3478 CreatePeerConnection();
3479 PeerConnectionInterface::BitrateParameters bitrate;
3480 bitrate.min_bitrate_bps = rtc::Optional<int>(5);
3481 bitrate.current_bitrate_bps = rtc::Optional<int>(3);
3482 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3483}
3484
3485TEST_F(PeerConnectionInterfaceTest, SetBitrateCurrentNegativeFails) {
3486 CreatePeerConnection();
3487 PeerConnectionInterface::BitrateParameters bitrate;
3488 bitrate.current_bitrate_bps = rtc::Optional<int>(-1);
3489 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3490}
3491
3492TEST_F(PeerConnectionInterfaceTest, SetBitrateMaxLessThanCurrentFails) {
3493 CreatePeerConnection();
3494 PeerConnectionInterface::BitrateParameters bitrate;
3495 bitrate.current_bitrate_bps = rtc::Optional<int>(10);
3496 bitrate.max_bitrate_bps = rtc::Optional<int>(8);
3497 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3498}
3499
3500TEST_F(PeerConnectionInterfaceTest, SetBitrateMaxLessThanMinFails) {
3501 CreatePeerConnection();
3502 PeerConnectionInterface::BitrateParameters bitrate;
3503 bitrate.min_bitrate_bps = rtc::Optional<int>(10);
3504 bitrate.max_bitrate_bps = rtc::Optional<int>(8);
3505 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3506}
3507
3508TEST_F(PeerConnectionInterfaceTest, SetBitrateMaxNegativeFails) {
3509 CreatePeerConnection();
3510 PeerConnectionInterface::BitrateParameters bitrate;
3511 bitrate.max_bitrate_bps = rtc::Optional<int>(-1);
3512 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3513}
3514
Steve Anton038834f2017-07-14 15:59:59 -07003515// ice_regather_interval_range requires WebRTC to be configured for continual
3516// gathering already.
3517TEST_F(PeerConnectionInterfaceTest,
3518 SetIceRegatherIntervalRangeWithoutContinualGatheringFails) {
3519 PeerConnectionInterface::RTCConfiguration config;
3520 config.ice_regather_interval_range.emplace(1000, 2000);
3521 config.continual_gathering_policy =
3522 PeerConnectionInterface::ContinualGatheringPolicy::GATHER_ONCE;
3523 CreatePeerConnectionExpectFail(config);
3524}
3525
3526// Ensures that there is no error when ice_regather_interval_range is set with
3527// continual gathering enabled.
3528TEST_F(PeerConnectionInterfaceTest,
3529 SetIceRegatherIntervalRangeWithContinualGathering) {
3530 PeerConnectionInterface::RTCConfiguration config;
3531 config.ice_regather_interval_range.emplace(1000, 2000);
3532 config.continual_gathering_policy =
3533 PeerConnectionInterface::ContinualGatheringPolicy::GATHER_CONTINUALLY;
3534 CreatePeerConnection(config, nullptr);
3535}
3536
zstein4b979802017-06-02 14:37:37 -07003537// The current bitrate from Call's BitrateConfigMask is currently clamped by
3538// Call's BitrateConfig, which comes from the SDP or a default value. This test
3539// checks that a call to SetBitrate with a current bitrate that will be clamped
3540// succeeds.
3541TEST_F(PeerConnectionInterfaceTest, SetBitrateCurrentLessThanImplicitMin) {
3542 CreatePeerConnection();
3543 PeerConnectionInterface::BitrateParameters bitrate;
3544 bitrate.current_bitrate_bps = rtc::Optional<int>(1);
3545 EXPECT_TRUE(pc_->SetBitrate(bitrate).ok());
3546}
3547
zhihuanga77e6bb2017-08-14 18:17:48 -07003548// The following tests verify that the offer can be created correctly.
3549TEST_F(PeerConnectionInterfaceTest,
3550 CreateOfferFailsWithInvalidOfferToReceiveAudio) {
3551 RTCOfferAnswerOptions rtc_options;
3552
3553 // Setting offer_to_receive_audio to a value lower than kUndefined or greater
3554 // than kMaxOfferToReceiveMedia should be treated as invalid.
3555 rtc_options.offer_to_receive_audio = RTCOfferAnswerOptions::kUndefined - 1;
3556 CreatePeerConnection();
3557 EXPECT_FALSE(CreateOfferWithOptions(rtc_options));
3558
3559 rtc_options.offer_to_receive_audio =
3560 RTCOfferAnswerOptions::kMaxOfferToReceiveMedia + 1;
3561 EXPECT_FALSE(CreateOfferWithOptions(rtc_options));
3562}
3563
3564TEST_F(PeerConnectionInterfaceTest,
3565 CreateOfferFailsWithInvalidOfferToReceiveVideo) {
3566 RTCOfferAnswerOptions rtc_options;
3567
3568 // Setting offer_to_receive_video to a value lower than kUndefined or greater
3569 // than kMaxOfferToReceiveMedia should be treated as invalid.
3570 rtc_options.offer_to_receive_video = RTCOfferAnswerOptions::kUndefined - 1;
3571 CreatePeerConnection();
3572 EXPECT_FALSE(CreateOfferWithOptions(rtc_options));
3573
3574 rtc_options.offer_to_receive_video =
3575 RTCOfferAnswerOptions::kMaxOfferToReceiveMedia + 1;
3576 EXPECT_FALSE(CreateOfferWithOptions(rtc_options));
3577}
3578
3579// Test that the audio and video content will be added to an offer if both
3580// |offer_to_receive_audio| and |offer_to_receive_video| options are 1.
3581TEST_F(PeerConnectionInterfaceTest, CreateOfferWithAudioVideoOptions) {
3582 RTCOfferAnswerOptions rtc_options;
3583 rtc_options.offer_to_receive_audio = 1;
3584 rtc_options.offer_to_receive_video = 1;
3585
3586 std::unique_ptr<SessionDescriptionInterface> offer;
3587 CreatePeerConnection();
3588 offer = CreateOfferWithOptions(rtc_options);
3589 ASSERT_TRUE(offer);
3590 EXPECT_NE(nullptr, GetFirstAudioContent(offer->description()));
3591 EXPECT_NE(nullptr, GetFirstVideoContent(offer->description()));
3592}
3593
3594// Test that only audio content will be added to the offer if only
3595// |offer_to_receive_audio| options is 1.
3596TEST_F(PeerConnectionInterfaceTest, CreateOfferWithAudioOnlyOptions) {
3597 RTCOfferAnswerOptions rtc_options;
3598 rtc_options.offer_to_receive_audio = 1;
3599 rtc_options.offer_to_receive_video = 0;
3600
3601 std::unique_ptr<SessionDescriptionInterface> offer;
3602 CreatePeerConnection();
3603 offer = CreateOfferWithOptions(rtc_options);
3604 ASSERT_TRUE(offer);
3605 EXPECT_NE(nullptr, GetFirstAudioContent(offer->description()));
3606 EXPECT_EQ(nullptr, GetFirstVideoContent(offer->description()));
3607}
3608
3609// Test that only video content will be added if only |offer_to_receive_video|
3610// options is 1.
3611TEST_F(PeerConnectionInterfaceTest, CreateOfferWithVideoOnlyOptions) {
3612 RTCOfferAnswerOptions rtc_options;
3613 rtc_options.offer_to_receive_audio = 0;
3614 rtc_options.offer_to_receive_video = 1;
3615
3616 std::unique_ptr<SessionDescriptionInterface> offer;
3617 CreatePeerConnection();
3618 offer = CreateOfferWithOptions(rtc_options);
3619 ASSERT_TRUE(offer);
3620 EXPECT_EQ(nullptr, GetFirstAudioContent(offer->description()));
3621 EXPECT_NE(nullptr, GetFirstVideoContent(offer->description()));
3622}
3623
3624// Test that if |voice_activity_detection| is false, no CN codec is added to the
3625// offer.
3626TEST_F(PeerConnectionInterfaceTest, CreateOfferWithVADOptions) {
3627 RTCOfferAnswerOptions rtc_options;
3628 rtc_options.offer_to_receive_audio = 1;
3629 rtc_options.offer_to_receive_video = 0;
3630
3631 std::unique_ptr<SessionDescriptionInterface> offer;
3632 CreatePeerConnection();
3633 offer = CreateOfferWithOptions(rtc_options);
3634 ASSERT_TRUE(offer);
3635 const cricket::ContentInfo* audio_content =
3636 offer->description()->GetContentByName(cricket::CN_AUDIO);
3637 ASSERT_TRUE(audio_content);
3638 // |voice_activity_detection| is true by default.
3639 EXPECT_TRUE(HasCNCodecs(audio_content));
3640
3641 rtc_options.voice_activity_detection = false;
3642 CreatePeerConnection();
3643 offer = CreateOfferWithOptions(rtc_options);
3644 ASSERT_TRUE(offer);
3645 audio_content = offer->description()->GetContentByName(cricket::CN_AUDIO);
3646 ASSERT_TRUE(audio_content);
3647 EXPECT_FALSE(HasCNCodecs(audio_content));
3648}
3649
3650// Test that no media content will be added to the offer if using default
3651// RTCOfferAnswerOptions.
3652TEST_F(PeerConnectionInterfaceTest, CreateOfferWithDefaultOfferAnswerOptions) {
3653 RTCOfferAnswerOptions rtc_options;
3654
3655 std::unique_ptr<SessionDescriptionInterface> offer;
3656 CreatePeerConnection();
3657 offer = CreateOfferWithOptions(rtc_options);
3658 ASSERT_TRUE(offer);
3659 EXPECT_EQ(nullptr, GetFirstAudioContent(offer->description()));
3660 EXPECT_EQ(nullptr, GetFirstVideoContent(offer->description()));
3661}
3662
3663// Test that if |ice_restart| is true, the ufrag/pwd will change, otherwise
3664// ufrag/pwd will be the same in the new offer.
3665TEST_F(PeerConnectionInterfaceTest, CreateOfferWithIceRestart) {
3666 RTCOfferAnswerOptions rtc_options;
3667 rtc_options.ice_restart = false;
3668 rtc_options.offer_to_receive_audio = 1;
3669
3670 std::unique_ptr<SessionDescriptionInterface> offer;
3671 CreatePeerConnection();
3672 CreateOfferWithOptionsAsLocalDescription(&offer, rtc_options);
3673 auto ufrag1 = offer->description()
3674 ->GetTransportInfoByName(cricket::CN_AUDIO)
3675 ->description.ice_ufrag;
3676 auto pwd1 = offer->description()
3677 ->GetTransportInfoByName(cricket::CN_AUDIO)
3678 ->description.ice_pwd;
3679
3680 // |ice_restart| is false, the ufrag/pwd shouldn't change.
3681 CreateOfferWithOptionsAsLocalDescription(&offer, rtc_options);
3682 auto ufrag2 = offer->description()
3683 ->GetTransportInfoByName(cricket::CN_AUDIO)
3684 ->description.ice_ufrag;
3685 auto pwd2 = offer->description()
3686 ->GetTransportInfoByName(cricket::CN_AUDIO)
3687 ->description.ice_pwd;
3688
3689 // |ice_restart| is true, the ufrag/pwd should change.
3690 rtc_options.ice_restart = true;
3691 CreateOfferWithOptionsAsLocalDescription(&offer, rtc_options);
3692 auto ufrag3 = offer->description()
3693 ->GetTransportInfoByName(cricket::CN_AUDIO)
3694 ->description.ice_ufrag;
3695 auto pwd3 = offer->description()
3696 ->GetTransportInfoByName(cricket::CN_AUDIO)
3697 ->description.ice_pwd;
3698
3699 EXPECT_EQ(ufrag1, ufrag2);
3700 EXPECT_EQ(pwd1, pwd2);
3701 EXPECT_NE(ufrag2, ufrag3);
3702 EXPECT_NE(pwd2, pwd3);
3703}
3704
3705// Test that if |use_rtp_mux| is true, the bundling will be enabled in the
3706// offer; if it is false, there won't be any bundle group in the offer.
3707TEST_F(PeerConnectionInterfaceTest, CreateOfferWithRtpMux) {
3708 RTCOfferAnswerOptions rtc_options;
3709 rtc_options.offer_to_receive_audio = 1;
3710 rtc_options.offer_to_receive_video = 1;
3711
3712 std::unique_ptr<SessionDescriptionInterface> offer;
3713 CreatePeerConnection();
3714
3715 rtc_options.use_rtp_mux = true;
3716 offer = CreateOfferWithOptions(rtc_options);
3717 ASSERT_TRUE(offer);
3718 EXPECT_NE(nullptr, GetFirstAudioContent(offer->description()));
3719 EXPECT_NE(nullptr, GetFirstVideoContent(offer->description()));
3720 EXPECT_TRUE(offer->description()->HasGroup(cricket::GROUP_TYPE_BUNDLE));
3721
3722 rtc_options.use_rtp_mux = false;
3723 offer = CreateOfferWithOptions(rtc_options);
3724 ASSERT_TRUE(offer);
3725 EXPECT_NE(nullptr, GetFirstAudioContent(offer->description()));
3726 EXPECT_NE(nullptr, GetFirstVideoContent(offer->description()));
3727 EXPECT_FALSE(offer->description()->HasGroup(cricket::GROUP_TYPE_BUNDLE));
3728}
3729
3730// If SetMandatoryReceiveAudio(false) and SetMandatoryReceiveVideo(false) are
3731// called for the answer constraints, but an audio and a video section were
3732// offered, there will still be an audio and a video section in the answer.
3733TEST_F(PeerConnectionInterfaceTest,
3734 RejectAudioAndVideoInAnswerWithConstraints) {
3735 // Offer both audio and video.
3736 RTCOfferAnswerOptions rtc_offer_options;
3737 rtc_offer_options.offer_to_receive_audio = 1;
3738 rtc_offer_options.offer_to_receive_video = 1;
3739
3740 CreatePeerConnection();
3741 std::unique_ptr<SessionDescriptionInterface> offer;
3742 CreateOfferWithOptionsAsRemoteDescription(&offer, rtc_offer_options);
3743 EXPECT_NE(nullptr, GetFirstAudioContent(offer->description()));
3744 EXPECT_NE(nullptr, GetFirstVideoContent(offer->description()));
3745
3746 // Since an offer has been created with both audio and video,
3747 // Answers will contain the media types that exist in the offer regardless of
3748 // the value of |answer_options.has_audio| and |answer_options.has_video|.
3749 FakeConstraints answer_c;
3750 // Reject both audio and video.
3751 answer_c.SetMandatoryReceiveAudio(false);
3752 answer_c.SetMandatoryReceiveVideo(false);
3753
3754 std::unique_ptr<SessionDescriptionInterface> answer;
3755 ASSERT_TRUE(DoCreateAnswer(&answer, &answer_c));
3756 const cricket::ContentInfo* audio_content =
3757 GetFirstAudioContent(answer->description());
3758 const cricket::ContentInfo* video_content =
3759 GetFirstVideoContent(answer->description());
3760 ASSERT_NE(nullptr, audio_content);
3761 ASSERT_NE(nullptr, video_content);
3762 EXPECT_TRUE(audio_content->rejected);
3763 EXPECT_TRUE(video_content->rejected);
3764}
3765
nisse51542be2016-02-12 02:27:06 -08003766class PeerConnectionMediaConfigTest : public testing::Test {
3767 protected:
3768 void SetUp() override {
zhihuang38ede132017-06-15 12:52:32 -07003769 pcf_ = PeerConnectionFactoryForTest::CreatePeerConnectionFactoryForTest();
nisse51542be2016-02-12 02:27:06 -08003770 pcf_->Initialize();
3771 }
nisseeaabdf62017-05-05 02:23:02 -07003772 const cricket::MediaConfig TestCreatePeerConnection(
nisse51542be2016-02-12 02:27:06 -08003773 const PeerConnectionInterface::RTCConfiguration& config,
zhihuanga77e6bb2017-08-14 18:17:48 -07003774 const MediaConstraintsInterface* constraints) {
zhihuang9763d562016-08-05 11:14:50 -07003775 rtc::scoped_refptr<PeerConnectionInterface> pc(pcf_->CreatePeerConnection(
3776 config, constraints, nullptr, nullptr, &observer_));
nisse51542be2016-02-12 02:27:06 -08003777 EXPECT_TRUE(pc.get());
nisseeaabdf62017-05-05 02:23:02 -07003778 return pc->GetConfiguration().media_config;
nisse51542be2016-02-12 02:27:06 -08003779 }
3780
zhihuang9763d562016-08-05 11:14:50 -07003781 rtc::scoped_refptr<PeerConnectionFactoryForTest> pcf_;
nisse51542be2016-02-12 02:27:06 -08003782 MockPeerConnectionObserver observer_;
3783};
3784
3785// This test verifies the default behaviour with no constraints and a
3786// default RTCConfiguration.
3787TEST_F(PeerConnectionMediaConfigTest, TestDefaults) {
3788 PeerConnectionInterface::RTCConfiguration config;
3789 FakeConstraints constraints;
3790
3791 const cricket::MediaConfig& media_config =
3792 TestCreatePeerConnection(config, &constraints);
3793
3794 EXPECT_FALSE(media_config.enable_dscp);
nisse0db023a2016-03-01 04:29:59 -08003795 EXPECT_TRUE(media_config.video.enable_cpu_overuse_detection);
3796 EXPECT_FALSE(media_config.video.disable_prerenderer_smoothing);
3797 EXPECT_FALSE(media_config.video.suspend_below_min_bitrate);
nisse51542be2016-02-12 02:27:06 -08003798}
3799
3800// This test verifies the DSCP constraint is recognized and passed to
nisse528b7932017-05-08 03:21:43 -07003801// the PeerConnection.
nisse51542be2016-02-12 02:27:06 -08003802TEST_F(PeerConnectionMediaConfigTest, TestDscpConstraintTrue) {
3803 PeerConnectionInterface::RTCConfiguration config;
3804 FakeConstraints constraints;
3805
3806 constraints.AddOptional(webrtc::MediaConstraintsInterface::kEnableDscp, true);
3807 const cricket::MediaConfig& media_config =
3808 TestCreatePeerConnection(config, &constraints);
3809
3810 EXPECT_TRUE(media_config.enable_dscp);
3811}
3812
3813// This test verifies the cpu overuse detection constraint is
nisse528b7932017-05-08 03:21:43 -07003814// recognized and passed to the PeerConnection.
nisse51542be2016-02-12 02:27:06 -08003815TEST_F(PeerConnectionMediaConfigTest, TestCpuOveruseConstraintFalse) {
3816 PeerConnectionInterface::RTCConfiguration config;
3817 FakeConstraints constraints;
3818
3819 constraints.AddOptional(
3820 webrtc::MediaConstraintsInterface::kCpuOveruseDetection, false);
3821 const cricket::MediaConfig media_config =
3822 TestCreatePeerConnection(config, &constraints);
3823
nisse0db023a2016-03-01 04:29:59 -08003824 EXPECT_FALSE(media_config.video.enable_cpu_overuse_detection);
nisse51542be2016-02-12 02:27:06 -08003825}
3826
3827// This test verifies that the disable_prerenderer_smoothing flag is
nisse528b7932017-05-08 03:21:43 -07003828// propagated from RTCConfiguration to the PeerConnection.
nisse51542be2016-02-12 02:27:06 -08003829TEST_F(PeerConnectionMediaConfigTest, TestDisablePrerendererSmoothingTrue) {
3830 PeerConnectionInterface::RTCConfiguration config;
3831 FakeConstraints constraints;
3832
Niels Möller71bdda02016-03-31 12:59:59 +02003833 config.set_prerenderer_smoothing(false);
nisse51542be2016-02-12 02:27:06 -08003834 const cricket::MediaConfig& media_config =
3835 TestCreatePeerConnection(config, &constraints);
3836
nisse0db023a2016-03-01 04:29:59 -08003837 EXPECT_TRUE(media_config.video.disable_prerenderer_smoothing);
3838}
3839
3840// This test verifies the suspend below min bitrate constraint is
nisse528b7932017-05-08 03:21:43 -07003841// recognized and passed to the PeerConnection.
nisse0db023a2016-03-01 04:29:59 -08003842TEST_F(PeerConnectionMediaConfigTest,
3843 TestSuspendBelowMinBitrateConstraintTrue) {
3844 PeerConnectionInterface::RTCConfiguration config;
3845 FakeConstraints constraints;
3846
3847 constraints.AddOptional(
3848 webrtc::MediaConstraintsInterface::kEnableVideoSuspendBelowMinBitrate,
3849 true);
3850 const cricket::MediaConfig media_config =
3851 TestCreatePeerConnection(config, &constraints);
3852
3853 EXPECT_TRUE(media_config.video.suspend_below_min_bitrate);
nisse51542be2016-02-12 02:27:06 -08003854}
3855
deadbeef293e9262017-01-11 12:28:30 -08003856// Tests a few random fields being different.
3857TEST(RTCConfigurationTest, ComparisonOperators) {
3858 PeerConnectionInterface::RTCConfiguration a;
3859 PeerConnectionInterface::RTCConfiguration b;
3860 EXPECT_EQ(a, b);
3861
3862 PeerConnectionInterface::RTCConfiguration c;
3863 c.servers.push_back(PeerConnectionInterface::IceServer());
3864 EXPECT_NE(a, c);
3865
3866 PeerConnectionInterface::RTCConfiguration d;
3867 d.type = PeerConnectionInterface::kRelay;
3868 EXPECT_NE(a, d);
3869
3870 PeerConnectionInterface::RTCConfiguration e;
3871 e.audio_jitter_buffer_max_packets = 5;
3872 EXPECT_NE(a, e);
3873
3874 PeerConnectionInterface::RTCConfiguration f;
3875 f.ice_connection_receiving_timeout = 1337;
3876 EXPECT_NE(a, f);
3877
3878 PeerConnectionInterface::RTCConfiguration g;
3879 g.disable_ipv6 = true;
3880 EXPECT_NE(a, g);
3881
3882 PeerConnectionInterface::RTCConfiguration h(
3883 PeerConnectionInterface::RTCConfigurationType::kAggressive);
3884 EXPECT_NE(a, h);
3885}
korniltsev.anatolyec390b52017-07-24 17:00:25 -07003886
3887// This test ensures OnRenegotiationNeeded is called when we add track with
3888// MediaStream -> AddTrack in the same way it is called when we add track with
3889// PeerConnection -> AddTrack.
3890// The test can be removed once addStream is rewritten in terms of addTrack
3891// https://bugs.chromium.org/p/webrtc/issues/detail?id=7815
3892TEST_F(PeerConnectionInterfaceTest, MediaStreamAddTrackRemoveTrackRenegotiate) {
3893 CreatePeerConnectionWithoutDtls();
3894 rtc::scoped_refptr<MediaStreamInterface> stream(
3895 pc_factory_->CreateLocalMediaStream(kStreamLabel1));
3896 pc_->AddStream(stream);
3897 rtc::scoped_refptr<AudioTrackInterface> audio_track(
3898 pc_factory_->CreateAudioTrack("audio_track", nullptr));
3899 rtc::scoped_refptr<VideoTrackInterface> video_track(
3900 pc_factory_->CreateVideoTrack(
3901 "video_track", pc_factory_->CreateVideoSource(
3902 std::unique_ptr<cricket::VideoCapturer>(
3903 new cricket::FakeVideoCapturer()))));
3904 stream->AddTrack(audio_track);
3905 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
3906 observer_.renegotiation_needed_ = false;
3907
3908 stream->AddTrack(video_track);
3909 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
3910 observer_.renegotiation_needed_ = false;
3911
3912 stream->RemoveTrack(audio_track);
3913 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
3914 observer_.renegotiation_needed_ = false;
3915
3916 stream->RemoveTrack(video_track);
3917 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
3918 observer_.renegotiation_needed_ = false;
3919}