blob: fb38d6541c9bd3882ee52625ef1d052b8ef9fde3 [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
Zhi Huang2a5e4262017-09-14 01:15:03 -070011#include <algorithm>
kwibergd1fe2812016-04-27 06:47:29 -070012#include <memory>
deadbeef3edec7c2016-12-10 11:44:26 -080013#include <sstream>
henrike@webrtc.org28e20752013-07-10 00:45:36 +000014#include <string>
kwiberg0eb15ed2015-12-17 03:04:15 -080015#include <utility>
henrike@webrtc.org28e20752013-07-10 00:45:36 +000016
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020017#include "api/audio_codecs/builtin_audio_decoder_factory.h"
18#include "api/audio_codecs/builtin_audio_encoder_factory.h"
19#include "api/jsepsessiondescription.h"
20#include "api/mediastreaminterface.h"
21#include "api/peerconnectioninterface.h"
22#include "api/rtpreceiverinterface.h"
23#include "api/rtpsenderinterface.h"
24#include "api/test/fakeconstraints.h"
25#include "media/base/fakevideocapturer.h"
26#include "media/engine/webrtcmediaengine.h"
27#include "media/sctp/sctptransportinternal.h"
28#include "modules/audio_processing/include/audio_processing.h"
29#include "p2p/base/fakeportallocator.h"
30#include "pc/audiotrack.h"
31#include "pc/mediasession.h"
32#include "pc/mediastream.h"
33#include "pc/peerconnection.h"
34#include "pc/streamcollection.h"
35#include "pc/test/fakeaudiocapturemodule.h"
36#include "pc/test/fakertccertificategenerator.h"
37#include "pc/test/fakevideotracksource.h"
38#include "pc/test/mockpeerconnectionobservers.h"
39#include "pc/test/testsdpstrings.h"
40#include "pc/videocapturertracksource.h"
41#include "pc/videotrack.h"
42#include "rtc_base/gunit.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020043#include "rtc_base/stringutils.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020044#include "rtc_base/virtualsocketserver.h"
45#include "test/gmock.h"
Elad Alon9e6565b2017-10-11 16:04:13 +020046#include "test/testsupport/fileutils.h"
kwibergac9f8762016-09-30 22:29:43 -070047
48#ifdef WEBRTC_ANDROID
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020049#include "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;
deadbeefc80741f2015-10-22 13:14:45 -0700334using webrtc::MediaConstraintsInterface;
deadbeefab9b2d12015-10-14 11:33:11 -0700335using webrtc::MediaStream;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000336using webrtc::MediaStreamInterface;
337using webrtc::MediaStreamTrackInterface;
338using webrtc::MockCreateSessionDescriptionObserver;
339using webrtc::MockDataChannelObserver;
Steve Anton94286cb2017-09-26 16:20:19 -0700340using webrtc::MockPeerConnectionObserver;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000341using 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 +0000532} // namespace
deadbeefab9b2d12015-10-14 11:33:11 -0700533
nisse528b7932017-05-08 03:21:43 -0700534// The PeerConnectionMediaConfig tests below verify that configuration and
535// constraints are propagated into the PeerConnection's MediaConfig. These
536// settings are intended for MediaChannel constructors, but that is not
537// exercised by these unittest.
zhihuang29ff8442016-07-27 11:07:25 -0700538class PeerConnectionFactoryForTest : public webrtc::PeerConnectionFactory {
539 public:
zhihuang38ede132017-06-15 12:52:32 -0700540 static rtc::scoped_refptr<PeerConnectionFactoryForTest>
541 CreatePeerConnectionFactoryForTest() {
542 auto audio_encoder_factory = webrtc::CreateBuiltinAudioEncoderFactory();
543 auto audio_decoder_factory = webrtc::CreateBuiltinAudioDecoderFactory();
544
545 auto media_engine = std::unique_ptr<cricket::MediaEngineInterface>(
546 cricket::WebRtcMediaEngineFactory::Create(
547 nullptr, audio_encoder_factory, audio_decoder_factory, nullptr,
peaha9cc40b2017-06-29 08:32:09 -0700548 nullptr, nullptr, webrtc::AudioProcessing::Create()));
zhihuang38ede132017-06-15 12:52:32 -0700549
550 std::unique_ptr<webrtc::CallFactoryInterface> call_factory =
551 webrtc::CreateCallFactory();
552
553 std::unique_ptr<webrtc::RtcEventLogFactoryInterface> event_log_factory =
554 webrtc::CreateRtcEventLogFactory();
555
556 return new rtc::RefCountedObject<PeerConnectionFactoryForTest>(
557 rtc::Thread::Current(), rtc::Thread::Current(), rtc::Thread::Current(),
Magnus Jedvert02e7a192017-09-23 17:21:32 +0200558 std::move(media_engine), std::move(call_factory),
zhihuang38ede132017-06-15 12:52:32 -0700559 std::move(event_log_factory));
560 }
561
562 PeerConnectionFactoryForTest(
563 rtc::Thread* network_thread,
564 rtc::Thread* worker_thread,
565 rtc::Thread* signaling_thread,
zhihuang38ede132017-06-15 12:52:32 -0700566 std::unique_ptr<cricket::MediaEngineInterface> media_engine,
567 std::unique_ptr<webrtc::CallFactoryInterface> call_factory,
568 std::unique_ptr<webrtc::RtcEventLogFactoryInterface> event_log_factory)
569 : webrtc::PeerConnectionFactory(network_thread,
570 worker_thread,
571 signaling_thread,
zhihuang38ede132017-06-15 12:52:32 -0700572 std::move(media_engine),
573 std::move(call_factory),
574 std::move(event_log_factory)) {}
kwiberg1e4e8cb2017-01-31 01:48:08 -0800575
zhihuang29ff8442016-07-27 11:07:25 -0700576 cricket::TransportController* CreateTransportController(
Honghai Zhangbfd398c2016-08-30 22:07:42 -0700577 cricket::PortAllocator* port_allocator,
578 bool redetermine_role_on_ice_restart) override {
zhihuang29ff8442016-07-27 11:07:25 -0700579 transport_controller = new cricket::TransportController(
Honghai Zhangbfd398c2016-08-30 22:07:42 -0700580 rtc::Thread::Current(), rtc::Thread::Current(), port_allocator,
deadbeef7914b8c2017-04-21 03:23:33 -0700581 redetermine_role_on_ice_restart, rtc::CryptoOptions());
zhihuang29ff8442016-07-27 11:07:25 -0700582 return transport_controller;
583 }
584
deadbeefd7850b22017-08-23 10:59:19 -0700585 rtc::scoped_refptr<FakeAudioCaptureModule> fake_audio_capture_module_;
zhihuang29ff8442016-07-27 11:07:25 -0700586 cricket::TransportController* transport_controller;
zhihuang29ff8442016-07-27 11:07:25 -0700587};
588
Steve Anton94286cb2017-09-26 16:20:19 -0700589// TODO(steveanton): Convert to use the new PeerConnectionUnitTestFixture and
590// PeerConnectionWrapper.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000591class PeerConnectionInterfaceTest : public testing::Test {
592 protected:
deadbeef9a6f4d42017-05-15 19:43:33 -0700593 PeerConnectionInterfaceTest()
deadbeef98e186c2017-05-16 18:00:06 -0700594 : vss_(new rtc::VirtualSocketServer()), main_(vss_.get()) {
phoglund37ebcf02016-01-08 05:04:57 -0800595#ifdef WEBRTC_ANDROID
596 webrtc::InitializeAndroidObjects();
597#endif
598 }
599
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000600 virtual void SetUp() {
deadbeefd7850b22017-08-23 10:59:19 -0700601 // Use fake audio capture module since we're only testing the interface
602 // level, and using a real one could make tests flaky when run in parallel.
603 fake_audio_capture_module_ = FakeAudioCaptureModule::Create();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000604 pc_factory_ = webrtc::CreatePeerConnectionFactory(
danilchape9021a32016-05-17 01:52:02 -0700605 rtc::Thread::Current(), rtc::Thread::Current(), rtc::Thread::Current(),
deadbeefd7850b22017-08-23 10:59:19 -0700606 fake_audio_capture_module_, nullptr, nullptr);
danilchape9021a32016-05-17 01:52:02 -0700607 ASSERT_TRUE(pc_factory_);
zhihuang29ff8442016-07-27 11:07:25 -0700608 pc_factory_for_test_ =
zhihuang38ede132017-06-15 12:52:32 -0700609 PeerConnectionFactoryForTest::CreatePeerConnectionFactoryForTest();
zhihuang29ff8442016-07-27 11:07:25 -0700610 pc_factory_for_test_->Initialize();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000611 }
612
613 void CreatePeerConnection() {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700614 CreatePeerConnection(PeerConnectionInterface::RTCConfiguration(), nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000615 }
616
deadbeef293e9262017-01-11 12:28:30 -0800617 // DTLS does not work in a loopback call, so is disabled for most of the
618 // tests in this file.
619 void CreatePeerConnectionWithoutDtls() {
620 FakeConstraints no_dtls_constraints;
621 no_dtls_constraints.AddMandatory(
622 webrtc::MediaConstraintsInterface::kEnableDtlsSrtp, false);
623
624 CreatePeerConnection(PeerConnectionInterface::RTCConfiguration(),
625 &no_dtls_constraints);
626 }
627
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000628 void CreatePeerConnection(webrtc::MediaConstraintsInterface* constraints) {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700629 CreatePeerConnection(PeerConnectionInterface::RTCConfiguration(),
630 constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000631 }
632
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700633 void CreatePeerConnectionWithIceTransportsType(
634 PeerConnectionInterface::IceTransportsType type) {
635 PeerConnectionInterface::RTCConfiguration config;
636 config.type = type;
637 return CreatePeerConnection(config, nullptr);
638 }
639
640 void CreatePeerConnectionWithIceServer(const std::string& uri,
641 const std::string& password) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800642 PeerConnectionInterface::RTCConfiguration config;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000643 PeerConnectionInterface::IceServer server;
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700644 server.uri = uri;
645 server.password = password;
646 config.servers.push_back(server);
647 CreatePeerConnection(config, nullptr);
648 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000649
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700650 void CreatePeerConnection(PeerConnectionInterface::RTCConfiguration config,
651 webrtc::MediaConstraintsInterface* constraints) {
kwibergd1fe2812016-04-27 06:47:29 -0700652 std::unique_ptr<cricket::FakePortAllocator> port_allocator(
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800653 new cricket::FakePortAllocator(rtc::Thread::Current(), nullptr));
654 port_allocator_ = port_allocator.get();
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000655
deadbeef1dcb1642017-03-29 21:08:16 -0700656 // Create certificate generator unless DTLS constraint is explicitly set to
657 // false.
Henrik Boströmd79599d2016-06-01 13:58:50 +0200658 std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator;
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000659 bool dtls;
660 if (FindConstraint(constraints,
661 webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
662 &dtls,
Henrik Boström5e56c592015-08-11 10:33:13 +0200663 nullptr) && dtls) {
deadbeef8662f942017-01-20 21:20:51 -0800664 fake_certificate_generator_ = new FakeRTCCertificateGenerator();
665 cert_generator.reset(fake_certificate_generator_);
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000666 }
Henrik Boströmd79599d2016-06-01 13:58:50 +0200667 pc_ = pc_factory_->CreatePeerConnection(
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800668 config, constraints, std::move(port_allocator),
Henrik Boströmd79599d2016-06-01 13:58:50 +0200669 std::move(cert_generator), &observer_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000670 ASSERT_TRUE(pc_.get() != NULL);
671 observer_.SetPeerConnectionInterface(pc_.get());
672 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
673 }
674
deadbeef0a6c4ca2015-10-06 11:38:28 -0700675 void CreatePeerConnectionExpectFail(const std::string& uri) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800676 PeerConnectionInterface::RTCConfiguration config;
deadbeef0a6c4ca2015-10-06 11:38:28 -0700677 PeerConnectionInterface::IceServer server;
deadbeef0a6c4ca2015-10-06 11:38:28 -0700678 server.uri = uri;
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800679 config.servers.push_back(server);
deadbeef0a6c4ca2015-10-06 11:38:28 -0700680
zhihuang9763d562016-08-05 11:14:50 -0700681 rtc::scoped_refptr<PeerConnectionInterface> pc;
hbosd7973cc2016-05-27 06:08:53 -0700682 pc = pc_factory_->CreatePeerConnection(config, nullptr, nullptr, nullptr,
683 &observer_);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800684 EXPECT_EQ(nullptr, pc);
deadbeef0a6c4ca2015-10-06 11:38:28 -0700685 }
686
Steve Anton038834f2017-07-14 15:59:59 -0700687 void CreatePeerConnectionExpectFail(
688 PeerConnectionInterface::RTCConfiguration config) {
689 PeerConnectionInterface::IceServer server;
690 server.uri = kTurnIceServerUri;
691 server.password = kTurnPassword;
692 config.servers.push_back(server);
693 rtc::scoped_refptr<PeerConnectionInterface> pc =
694 pc_factory_->CreatePeerConnection(config, nullptr, nullptr, &observer_);
695 EXPECT_EQ(nullptr, pc);
696 }
697
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000698 void CreatePeerConnectionWithDifferentConfigurations() {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700699 CreatePeerConnectionWithIceServer(kStunAddressOnly, "");
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800700 EXPECT_EQ(1u, port_allocator_->stun_servers().size());
701 EXPECT_EQ(0u, port_allocator_->turn_servers().size());
702 EXPECT_EQ("address", port_allocator_->stun_servers().begin()->hostname());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000703 EXPECT_EQ(kDefaultStunPort,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800704 port_allocator_->stun_servers().begin()->port());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000705
deadbeef0a6c4ca2015-10-06 11:38:28 -0700706 CreatePeerConnectionExpectFail(kStunInvalidPort);
707 CreatePeerConnectionExpectFail(kStunAddressPortAndMore1);
708 CreatePeerConnectionExpectFail(kStunAddressPortAndMore2);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000709
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700710 CreatePeerConnectionWithIceServer(kTurnIceServerUri, kTurnPassword);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800711 EXPECT_EQ(0u, port_allocator_->stun_servers().size());
712 EXPECT_EQ(1u, port_allocator_->turn_servers().size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000713 EXPECT_EQ(kTurnUsername,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800714 port_allocator_->turn_servers()[0].credentials.username);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000715 EXPECT_EQ(kTurnPassword,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800716 port_allocator_->turn_servers()[0].credentials.password);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000717 EXPECT_EQ(kTurnHostname,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800718 port_allocator_->turn_servers()[0].ports[0].address.hostname());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000719 }
720
721 void ReleasePeerConnection() {
722 pc_ = NULL;
723 observer_.SetPeerConnectionInterface(NULL);
724 }
725
deadbeefab9b2d12015-10-14 11:33:11 -0700726 void AddVideoStream(const std::string& label) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000727 // Create a local stream.
zhihuang9763d562016-08-05 11:14:50 -0700728 rtc::scoped_refptr<MediaStreamInterface> stream(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000729 pc_factory_->CreateLocalMediaStream(label));
zhihuang9763d562016-08-05 11:14:50 -0700730 rtc::scoped_refptr<VideoTrackSourceInterface> video_source(
deadbeef112b2e92017-02-10 20:13:37 -0800731 pc_factory_->CreateVideoSource(std::unique_ptr<cricket::VideoCapturer>(
732 new cricket::FakeVideoCapturer()),
733 NULL));
zhihuang9763d562016-08-05 11:14:50 -0700734 rtc::scoped_refptr<VideoTrackInterface> video_track(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000735 pc_factory_->CreateVideoTrack(label + "v0", video_source));
736 stream->AddTrack(video_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +0000737 EXPECT_TRUE(pc_->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000738 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
739 observer_.renegotiation_needed_ = false;
740 }
741
742 void AddVoiceStream(const std::string& label) {
743 // Create a local stream.
zhihuang9763d562016-08-05 11:14:50 -0700744 rtc::scoped_refptr<MediaStreamInterface> stream(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000745 pc_factory_->CreateLocalMediaStream(label));
zhihuang9763d562016-08-05 11:14:50 -0700746 rtc::scoped_refptr<AudioTrackInterface> audio_track(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000747 pc_factory_->CreateAudioTrack(label + "a0", NULL));
748 stream->AddTrack(audio_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +0000749 EXPECT_TRUE(pc_->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000750 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
751 observer_.renegotiation_needed_ = false;
752 }
753
754 void AddAudioVideoStream(const std::string& stream_label,
755 const std::string& audio_track_label,
756 const std::string& video_track_label) {
757 // Create a local stream.
zhihuang9763d562016-08-05 11:14:50 -0700758 rtc::scoped_refptr<MediaStreamInterface> stream(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000759 pc_factory_->CreateLocalMediaStream(stream_label));
zhihuang9763d562016-08-05 11:14:50 -0700760 rtc::scoped_refptr<AudioTrackInterface> audio_track(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000761 pc_factory_->CreateAudioTrack(
762 audio_track_label, static_cast<AudioSourceInterface*>(NULL)));
763 stream->AddTrack(audio_track.get());
zhihuang9763d562016-08-05 11:14:50 -0700764 rtc::scoped_refptr<VideoTrackInterface> video_track(
nisseaf510af2016-03-21 08:20:42 -0700765 pc_factory_->CreateVideoTrack(
deadbeef112b2e92017-02-10 20:13:37 -0800766 video_track_label, pc_factory_->CreateVideoSource(
767 std::unique_ptr<cricket::VideoCapturer>(
768 new cricket::FakeVideoCapturer()))));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000769 stream->AddTrack(video_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +0000770 EXPECT_TRUE(pc_->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000771 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
772 observer_.renegotiation_needed_ = false;
773 }
774
kwibergd1fe2812016-04-27 06:47:29 -0700775 bool DoCreateOfferAnswer(std::unique_ptr<SessionDescriptionInterface>* desc,
deadbeefc80741f2015-10-22 13:14:45 -0700776 bool offer,
777 MediaConstraintsInterface* constraints) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000778 rtc::scoped_refptr<MockCreateSessionDescriptionObserver>
779 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000780 MockCreateSessionDescriptionObserver>());
781 if (offer) {
deadbeefc80741f2015-10-22 13:14:45 -0700782 pc_->CreateOffer(observer, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000783 } else {
deadbeefc80741f2015-10-22 13:14:45 -0700784 pc_->CreateAnswer(observer, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000785 }
786 EXPECT_EQ_WAIT(true, observer->called(), kTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -0700787 *desc = observer->MoveDescription();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000788 return observer->result();
789 }
790
kwibergd1fe2812016-04-27 06:47:29 -0700791 bool DoCreateOffer(std::unique_ptr<SessionDescriptionInterface>* desc,
deadbeefc80741f2015-10-22 13:14:45 -0700792 MediaConstraintsInterface* constraints) {
793 return DoCreateOfferAnswer(desc, true, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000794 }
795
kwibergd1fe2812016-04-27 06:47:29 -0700796 bool DoCreateAnswer(std::unique_ptr<SessionDescriptionInterface>* desc,
deadbeefc80741f2015-10-22 13:14:45 -0700797 MediaConstraintsInterface* constraints) {
798 return DoCreateOfferAnswer(desc, false, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000799 }
800
Steve Antondb45ca82017-09-11 18:27:34 -0700801 bool DoSetSessionDescription(
802 std::unique_ptr<SessionDescriptionInterface> desc,
803 bool local) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000804 rtc::scoped_refptr<MockSetSessionDescriptionObserver>
805 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000806 MockSetSessionDescriptionObserver>());
807 if (local) {
Steve Antondb45ca82017-09-11 18:27:34 -0700808 pc_->SetLocalDescription(observer, desc.release());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000809 } else {
Steve Antondb45ca82017-09-11 18:27:34 -0700810 pc_->SetRemoteDescription(observer, desc.release());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000811 }
zhihuang29ff8442016-07-27 11:07:25 -0700812 if (pc_->signaling_state() != PeerConnectionInterface::kClosed) {
813 EXPECT_EQ_WAIT(true, observer->called(), kTimeout);
814 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000815 return observer->result();
816 }
817
Steve Antondb45ca82017-09-11 18:27:34 -0700818 bool DoSetLocalDescription(
819 std::unique_ptr<SessionDescriptionInterface> desc) {
820 return DoSetSessionDescription(std::move(desc), true);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000821 }
822
Steve Antondb45ca82017-09-11 18:27:34 -0700823 bool DoSetRemoteDescription(
824 std::unique_ptr<SessionDescriptionInterface> desc) {
825 return DoSetSessionDescription(std::move(desc), false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000826 }
827
828 // Calls PeerConnection::GetStats and check the return value.
829 // It does not verify the values in the StatReports since a RTCP packet might
830 // be required.
831 bool DoGetStats(MediaStreamTrackInterface* track) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000832 rtc::scoped_refptr<MockStatsObserver> observer(
833 new rtc::RefCountedObject<MockStatsObserver>());
jiayl@webrtc.orgdb41b4d2014-03-03 21:30:06 +0000834 if (!pc_->GetStats(
835 observer, track, PeerConnectionInterface::kStatsOutputLevelStandard))
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000836 return false;
837 EXPECT_TRUE_WAIT(observer->called(), kTimeout);
838 return observer->called();
839 }
840
841 void InitiateCall() {
deadbeef293e9262017-01-11 12:28:30 -0800842 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000843 // Create a local stream with audio&video tracks.
844 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
845 CreateOfferReceiveAnswer();
846 }
847
848 // Verify that RTP Header extensions has been negotiated for audio and video.
849 void VerifyRemoteRtpHeaderExtensions() {
850 const cricket::MediaContentDescription* desc =
851 cricket::GetFirstAudioContentDescription(
852 pc_->remote_description()->description());
853 ASSERT_TRUE(desc != NULL);
854 EXPECT_GT(desc->rtp_header_extensions().size(), 0u);
855
856 desc = cricket::GetFirstVideoContentDescription(
857 pc_->remote_description()->description());
858 ASSERT_TRUE(desc != NULL);
859 EXPECT_GT(desc->rtp_header_extensions().size(), 0u);
860 }
861
862 void CreateOfferAsRemoteDescription() {
kwibergd1fe2812016-04-27 06:47:29 -0700863 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -0700864 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000865 std::string sdp;
866 EXPECT_TRUE(offer->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -0700867 std::unique_ptr<SessionDescriptionInterface> remote_offer(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000868 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
Steve Antondb45ca82017-09-11 18:27:34 -0700869 sdp, nullptr));
870 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_offer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000871 EXPECT_EQ(PeerConnectionInterface::kHaveRemoteOffer, observer_.state_);
872 }
873
deadbeefab9b2d12015-10-14 11:33:11 -0700874 void CreateAndSetRemoteOffer(const std::string& sdp) {
Steve Antondb45ca82017-09-11 18:27:34 -0700875 std::unique_ptr<SessionDescriptionInterface> remote_offer(
deadbeefab9b2d12015-10-14 11:33:11 -0700876 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
Steve Antondb45ca82017-09-11 18:27:34 -0700877 sdp, nullptr));
878 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_offer)));
deadbeefab9b2d12015-10-14 11:33:11 -0700879 EXPECT_EQ(PeerConnectionInterface::kHaveRemoteOffer, observer_.state_);
880 }
881
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000882 void CreateAnswerAsLocalDescription() {
kwibergd1fe2812016-04-27 06:47:29 -0700883 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -0700884 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000885
886 // TODO(perkj): Currently SetLocalDescription fails if any parameters in an
887 // audio codec change, even if the parameter has nothing to do with
888 // receiving. Not all parameters are serialized to SDP.
889 // Since CreatePrAnswerAsLocalDescription serialize/deserialize
890 // the SessionDescription, it is necessary to do that here to in order to
891 // get ReceiveOfferCreatePrAnswerAndAnswer and RenegotiateAudioOnly to pass.
892 // https://code.google.com/p/webrtc/issues/detail?id=1356
893 std::string sdp;
894 EXPECT_TRUE(answer->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -0700895 std::unique_ptr<SessionDescriptionInterface> new_answer(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000896 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
Steve Antondb45ca82017-09-11 18:27:34 -0700897 sdp, nullptr));
898 EXPECT_TRUE(DoSetLocalDescription(std::move(new_answer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000899 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
900 }
901
902 void CreatePrAnswerAsLocalDescription() {
kwibergd1fe2812016-04-27 06:47:29 -0700903 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -0700904 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000905
906 std::string sdp;
907 EXPECT_TRUE(answer->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -0700908 std::unique_ptr<SessionDescriptionInterface> pr_answer(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000909 webrtc::CreateSessionDescription(SessionDescriptionInterface::kPrAnswer,
Steve Antondb45ca82017-09-11 18:27:34 -0700910 sdp, nullptr));
911 EXPECT_TRUE(DoSetLocalDescription(std::move(pr_answer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000912 EXPECT_EQ(PeerConnectionInterface::kHaveLocalPrAnswer, observer_.state_);
913 }
914
915 void CreateOfferReceiveAnswer() {
916 CreateOfferAsLocalDescription();
917 std::string sdp;
918 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
919 CreateAnswerAsRemoteDescription(sdp);
920 }
921
922 void CreateOfferAsLocalDescription() {
kwibergd1fe2812016-04-27 06:47:29 -0700923 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -0700924 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000925 // TODO(perkj): Currently SetLocalDescription fails if any parameters in an
926 // audio codec change, even if the parameter has nothing to do with
927 // receiving. Not all parameters are serialized to SDP.
928 // Since CreatePrAnswerAsLocalDescription serialize/deserialize
929 // the SessionDescription, it is necessary to do that here to in order to
930 // get ReceiveOfferCreatePrAnswerAndAnswer and RenegotiateAudioOnly to pass.
931 // https://code.google.com/p/webrtc/issues/detail?id=1356
932 std::string sdp;
933 EXPECT_TRUE(offer->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -0700934 std::unique_ptr<SessionDescriptionInterface> new_offer(
935 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
936 sdp, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000937
Steve Antondb45ca82017-09-11 18:27:34 -0700938 EXPECT_TRUE(DoSetLocalDescription(std::move(new_offer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000939 EXPECT_EQ(PeerConnectionInterface::kHaveLocalOffer, observer_.state_);
mallinath@webrtc.org68cbd012014-01-22 00:16:46 +0000940 // Wait for the ice_complete message, so that SDP will have candidates.
941 EXPECT_TRUE_WAIT(observer_.ice_complete_, kTimeout);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000942 }
943
deadbeefab9b2d12015-10-14 11:33:11 -0700944 void CreateAnswerAsRemoteDescription(const std::string& sdp) {
Steve Antondb45ca82017-09-11 18:27:34 -0700945 std::unique_ptr<SessionDescriptionInterface> answer(
946 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
947 sdp, nullptr));
948 ASSERT_TRUE(answer);
949 EXPECT_TRUE(DoSetRemoteDescription(std::move(answer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000950 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
951 }
952
deadbeefab9b2d12015-10-14 11:33:11 -0700953 void CreatePrAnswerAndAnswerAsRemoteDescription(const std::string& sdp) {
Steve Antondb45ca82017-09-11 18:27:34 -0700954 std::unique_ptr<SessionDescriptionInterface> pr_answer(
955 webrtc::CreateSessionDescription(SessionDescriptionInterface::kPrAnswer,
956 sdp, nullptr));
957 ASSERT_TRUE(pr_answer);
958 EXPECT_TRUE(DoSetRemoteDescription(std::move(pr_answer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000959 EXPECT_EQ(PeerConnectionInterface::kHaveRemotePrAnswer, observer_.state_);
Steve Antondb45ca82017-09-11 18:27:34 -0700960 std::unique_ptr<SessionDescriptionInterface> answer(
961 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
962 sdp, nullptr));
963 ASSERT_TRUE(answer);
964 EXPECT_TRUE(DoSetRemoteDescription(std::move(answer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000965 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
966 }
967
968 // Help function used for waiting until a the last signaled remote stream has
969 // the same label as |stream_label|. In a few of the tests in this file we
970 // answer with the same session description as we offer and thus we can
971 // check if OnAddStream have been called with the same stream as we offer to
972 // send.
973 void WaitAndVerifyOnAddStream(const std::string& stream_label) {
974 EXPECT_EQ_WAIT(stream_label, observer_.GetLastAddedStreamLabel(), kTimeout);
975 }
976
977 // Creates an offer and applies it as a local session description.
978 // Creates an answer with the same SDP an the offer but removes all lines
979 // that start with a:ssrc"
980 void CreateOfferReceiveAnswerWithoutSsrc() {
981 CreateOfferAsLocalDescription();
982 std::string sdp;
983 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
984 SetSsrcToZero(&sdp);
985 CreateAnswerAsRemoteDescription(sdp);
986 }
987
deadbeefab9b2d12015-10-14 11:33:11 -0700988 // This function creates a MediaStream with label kStreams[0] and
989 // |number_of_audio_tracks| and |number_of_video_tracks| tracks and the
990 // corresponding SessionDescriptionInterface. The SessionDescriptionInterface
kwiberg2bbff992016-03-16 11:03:04 -0700991 // is returned and the MediaStream is stored in
deadbeefab9b2d12015-10-14 11:33:11 -0700992 // |reference_collection_|
kwibergd1fe2812016-04-27 06:47:29 -0700993 std::unique_ptr<SessionDescriptionInterface>
kwiberg2bbff992016-03-16 11:03:04 -0700994 CreateSessionDescriptionAndReference(size_t number_of_audio_tracks,
995 size_t number_of_video_tracks) {
996 EXPECT_LE(number_of_audio_tracks, 2u);
997 EXPECT_LE(number_of_video_tracks, 2u);
deadbeefab9b2d12015-10-14 11:33:11 -0700998
999 reference_collection_ = StreamCollection::Create();
1000 std::string sdp_ms1 = std::string(kSdpStringInit);
1001
1002 std::string mediastream_label = kStreams[0];
1003
1004 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream(
1005 webrtc::MediaStream::Create(mediastream_label));
1006 reference_collection_->AddStream(stream);
1007
1008 if (number_of_audio_tracks > 0) {
1009 sdp_ms1 += std::string(kSdpStringAudio);
1010 sdp_ms1 += std::string(kSdpStringMs1Audio0);
1011 AddAudioTrack(kAudioTracks[0], stream);
1012 }
1013 if (number_of_audio_tracks > 1) {
1014 sdp_ms1 += kSdpStringMs1Audio1;
1015 AddAudioTrack(kAudioTracks[1], stream);
1016 }
1017
1018 if (number_of_video_tracks > 0) {
1019 sdp_ms1 += std::string(kSdpStringVideo);
1020 sdp_ms1 += std::string(kSdpStringMs1Video0);
1021 AddVideoTrack(kVideoTracks[0], stream);
1022 }
1023 if (number_of_video_tracks > 1) {
1024 sdp_ms1 += kSdpStringMs1Video1;
1025 AddVideoTrack(kVideoTracks[1], stream);
1026 }
1027
kwibergd1fe2812016-04-27 06:47:29 -07001028 return std::unique_ptr<SessionDescriptionInterface>(
kwiberg2bbff992016-03-16 11:03:04 -07001029 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
1030 sdp_ms1, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07001031 }
1032
1033 void AddAudioTrack(const std::string& track_id,
1034 MediaStreamInterface* stream) {
1035 rtc::scoped_refptr<webrtc::AudioTrackInterface> audio_track(
1036 webrtc::AudioTrack::Create(track_id, nullptr));
1037 ASSERT_TRUE(stream->AddTrack(audio_track));
1038 }
1039
1040 void AddVideoTrack(const std::string& track_id,
1041 MediaStreamInterface* stream) {
1042 rtc::scoped_refptr<webrtc::VideoTrackInterface> video_track(
nisseaf510af2016-03-21 08:20:42 -07001043 webrtc::VideoTrack::Create(track_id,
perkj773be362017-07-31 23:22:01 -07001044 webrtc::FakeVideoTrackSource::Create(),
1045 rtc::Thread::Current()));
deadbeefab9b2d12015-10-14 11:33:11 -07001046 ASSERT_TRUE(stream->AddTrack(video_track));
1047 }
1048
kwibergfd8be342016-05-14 19:44:11 -07001049 std::unique_ptr<SessionDescriptionInterface> CreateOfferWithOneAudioStream() {
deadbeef293e9262017-01-11 12:28:30 -08001050 CreatePeerConnectionWithoutDtls();
zhihuang8f65cdf2016-05-06 18:40:30 -07001051 AddVoiceStream(kStreamLabel1);
kwibergfd8be342016-05-14 19:44:11 -07001052 std::unique_ptr<SessionDescriptionInterface> offer;
zhihuang8f65cdf2016-05-06 18:40:30 -07001053 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
1054 return offer;
1055 }
1056
kwibergfd8be342016-05-14 19:44:11 -07001057 std::unique_ptr<SessionDescriptionInterface>
zhihuang8f65cdf2016-05-06 18:40:30 -07001058 CreateAnswerWithOneAudioStream() {
Steve Antondb45ca82017-09-11 18:27:34 -07001059 EXPECT_TRUE(DoSetRemoteDescription(CreateOfferWithOneAudioStream()));
kwibergfd8be342016-05-14 19:44:11 -07001060 std::unique_ptr<SessionDescriptionInterface> answer;
zhihuang8f65cdf2016-05-06 18:40:30 -07001061 EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
1062 return answer;
1063 }
1064
1065 const std::string& GetFirstAudioStreamCname(
1066 const SessionDescriptionInterface* desc) {
1067 const cricket::ContentInfo* audio_content =
1068 cricket::GetFirstAudioContent(desc->description());
1069 const cricket::AudioContentDescription* audio_desc =
1070 static_cast<const cricket::AudioContentDescription*>(
1071 audio_content->description);
1072 return audio_desc->streams()[0].cname;
1073 }
1074
zhihuang1c378ed2017-08-17 14:10:50 -07001075 std::unique_ptr<SessionDescriptionInterface> CreateOfferWithOptions(
1076 const RTCOfferAnswerOptions& offer_answer_options) {
1077 RTC_DCHECK(pc_);
1078 rtc::scoped_refptr<MockCreateSessionDescriptionObserver> observer(
1079 new rtc::RefCountedObject<MockCreateSessionDescriptionObserver>());
1080 pc_->CreateOffer(observer, offer_answer_options);
1081 EXPECT_EQ_WAIT(true, observer->called(), kTimeout);
1082 return observer->MoveDescription();
1083 }
1084
1085 void CreateOfferWithOptionsAsRemoteDescription(
1086 std::unique_ptr<SessionDescriptionInterface>* desc,
1087 const RTCOfferAnswerOptions& offer_answer_options) {
1088 *desc = CreateOfferWithOptions(offer_answer_options);
1089 ASSERT_TRUE(desc != nullptr);
1090 std::string sdp;
1091 EXPECT_TRUE((*desc)->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07001092 std::unique_ptr<SessionDescriptionInterface> remote_offer(
zhihuang1c378ed2017-08-17 14:10:50 -07001093 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
Steve Antondb45ca82017-09-11 18:27:34 -07001094 sdp, nullptr));
1095 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_offer)));
zhihuang1c378ed2017-08-17 14:10:50 -07001096 EXPECT_EQ(PeerConnectionInterface::kHaveRemoteOffer, observer_.state_);
1097 }
1098
1099 void CreateOfferWithOptionsAsLocalDescription(
1100 std::unique_ptr<SessionDescriptionInterface>* desc,
1101 const RTCOfferAnswerOptions& offer_answer_options) {
1102 *desc = CreateOfferWithOptions(offer_answer_options);
1103 ASSERT_TRUE(desc != nullptr);
1104 std::string sdp;
1105 EXPECT_TRUE((*desc)->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07001106 std::unique_ptr<SessionDescriptionInterface> new_offer(
1107 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
1108 sdp, nullptr));
zhihuang1c378ed2017-08-17 14:10:50 -07001109
Steve Antondb45ca82017-09-11 18:27:34 -07001110 EXPECT_TRUE(DoSetLocalDescription(std::move(new_offer)));
zhihuang1c378ed2017-08-17 14:10:50 -07001111 EXPECT_EQ(PeerConnectionInterface::kHaveLocalOffer, observer_.state_);
1112 }
1113
1114 bool HasCNCodecs(const cricket::ContentInfo* content) {
1115 const cricket::ContentDescription* description = content->description;
1116 RTC_DCHECK(description);
1117 const cricket::AudioContentDescription* audio_content_desc =
1118 static_cast<const cricket::AudioContentDescription*>(description);
1119 RTC_DCHECK(audio_content_desc);
1120 for (size_t i = 0; i < audio_content_desc->codecs().size(); ++i) {
1121 if (audio_content_desc->codecs()[i].name == "CN")
1122 return true;
1123 }
1124 return false;
1125 }
1126
deadbeef9a6f4d42017-05-15 19:43:33 -07001127 std::unique_ptr<rtc::VirtualSocketServer> vss_;
1128 rtc::AutoSocketServerThread main_;
deadbeefd7850b22017-08-23 10:59:19 -07001129 rtc::scoped_refptr<FakeAudioCaptureModule> fake_audio_capture_module_;
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08001130 cricket::FakePortAllocator* port_allocator_ = nullptr;
deadbeef8662f942017-01-20 21:20:51 -08001131 FakeRTCCertificateGenerator* fake_certificate_generator_ = nullptr;
zhihuang9763d562016-08-05 11:14:50 -07001132 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface> pc_factory_;
1133 rtc::scoped_refptr<PeerConnectionFactoryForTest> pc_factory_for_test_;
1134 rtc::scoped_refptr<PeerConnectionInterface> pc_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001135 MockPeerConnectionObserver observer_;
deadbeefab9b2d12015-10-14 11:33:11 -07001136 rtc::scoped_refptr<StreamCollection> reference_collection_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001137};
1138
zhihuang29ff8442016-07-27 11:07:25 -07001139// Test that no callbacks on the PeerConnectionObserver are called after the
1140// PeerConnection is closed.
1141TEST_F(PeerConnectionInterfaceTest, CloseAndTestCallbackFunctions) {
zhihuang9763d562016-08-05 11:14:50 -07001142 rtc::scoped_refptr<PeerConnectionInterface> pc(
zhihuang29ff8442016-07-27 11:07:25 -07001143 pc_factory_for_test_->CreatePeerConnection(
1144 PeerConnectionInterface::RTCConfiguration(), nullptr, nullptr,
1145 nullptr, &observer_));
1146 observer_.SetPeerConnectionInterface(pc.get());
1147 pc->Close();
1148
1149 // No callbacks is expected to be called.
zhihuang81c3a032016-11-17 12:06:24 -08001150 observer_.callback_triggered_ = false;
zhihuang29ff8442016-07-27 11:07:25 -07001151 std::vector<cricket::Candidate> candidates;
1152 pc_factory_for_test_->transport_controller->SignalGatheringState(
1153 cricket::IceGatheringState{});
1154 pc_factory_for_test_->transport_controller->SignalCandidatesGathered(
1155 "", candidates);
1156 pc_factory_for_test_->transport_controller->SignalConnectionState(
1157 cricket::IceConnectionState{});
1158 pc_factory_for_test_->transport_controller->SignalCandidatesRemoved(
1159 candidates);
1160 pc_factory_for_test_->transport_controller->SignalReceiving(false);
zhihuang81c3a032016-11-17 12:06:24 -08001161 EXPECT_FALSE(observer_.callback_triggered_);
zhihuang29ff8442016-07-27 11:07:25 -07001162}
1163
zhihuang8f65cdf2016-05-06 18:40:30 -07001164// Generate different CNAMEs when PeerConnections are created.
1165// The CNAMEs are expected to be generated randomly. It is possible
1166// that the test fails, though the possibility is very low.
1167TEST_F(PeerConnectionInterfaceTest, CnameGenerationInOffer) {
kwibergfd8be342016-05-14 19:44:11 -07001168 std::unique_ptr<SessionDescriptionInterface> offer1 =
zhihuang8f65cdf2016-05-06 18:40:30 -07001169 CreateOfferWithOneAudioStream();
kwibergfd8be342016-05-14 19:44:11 -07001170 std::unique_ptr<SessionDescriptionInterface> offer2 =
zhihuang8f65cdf2016-05-06 18:40:30 -07001171 CreateOfferWithOneAudioStream();
1172 EXPECT_NE(GetFirstAudioStreamCname(offer1.get()),
1173 GetFirstAudioStreamCname(offer2.get()));
1174}
1175
1176TEST_F(PeerConnectionInterfaceTest, CnameGenerationInAnswer) {
kwibergfd8be342016-05-14 19:44:11 -07001177 std::unique_ptr<SessionDescriptionInterface> answer1 =
zhihuang8f65cdf2016-05-06 18:40:30 -07001178 CreateAnswerWithOneAudioStream();
kwibergfd8be342016-05-14 19:44:11 -07001179 std::unique_ptr<SessionDescriptionInterface> answer2 =
zhihuang8f65cdf2016-05-06 18:40:30 -07001180 CreateAnswerWithOneAudioStream();
1181 EXPECT_NE(GetFirstAudioStreamCname(answer1.get()),
1182 GetFirstAudioStreamCname(answer2.get()));
1183}
1184
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001185TEST_F(PeerConnectionInterfaceTest,
1186 CreatePeerConnectionWithDifferentConfigurations) {
1187 CreatePeerConnectionWithDifferentConfigurations();
1188}
1189
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001190TEST_F(PeerConnectionInterfaceTest,
1191 CreatePeerConnectionWithDifferentIceTransportsTypes) {
1192 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kNone);
1193 EXPECT_EQ(cricket::CF_NONE, port_allocator_->candidate_filter());
1194 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kRelay);
1195 EXPECT_EQ(cricket::CF_RELAY, port_allocator_->candidate_filter());
1196 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kNoHost);
1197 EXPECT_EQ(cricket::CF_ALL & ~cricket::CF_HOST,
1198 port_allocator_->candidate_filter());
1199 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kAll);
1200 EXPECT_EQ(cricket::CF_ALL, port_allocator_->candidate_filter());
1201}
1202
1203// Test that when a PeerConnection is created with a nonzero candidate pool
1204// size, the pooled PortAllocatorSession is created with all the attributes
1205// in the RTCConfiguration.
1206TEST_F(PeerConnectionInterfaceTest, CreatePeerConnectionWithPooledCandidates) {
1207 PeerConnectionInterface::RTCConfiguration config;
1208 PeerConnectionInterface::IceServer server;
1209 server.uri = kStunAddressOnly;
1210 config.servers.push_back(server);
1211 config.type = PeerConnectionInterface::kRelay;
1212 config.disable_ipv6 = true;
1213 config.tcp_candidate_policy =
1214 PeerConnectionInterface::kTcpCandidatePolicyDisabled;
honghaiz60347052016-05-31 18:29:12 -07001215 config.candidate_network_policy =
1216 PeerConnectionInterface::kCandidateNetworkPolicyLowCost;
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001217 config.ice_candidate_pool_size = 1;
1218 CreatePeerConnection(config, nullptr);
1219
1220 const cricket::FakePortAllocatorSession* session =
1221 static_cast<const cricket::FakePortAllocatorSession*>(
1222 port_allocator_->GetPooledSession());
1223 ASSERT_NE(nullptr, session);
1224 EXPECT_EQ(1UL, session->stun_servers().size());
1225 EXPECT_EQ(0U, session->flags() & cricket::PORTALLOCATOR_ENABLE_IPV6);
1226 EXPECT_LT(0U, session->flags() & cricket::PORTALLOCATOR_DISABLE_TCP);
honghaiz60347052016-05-31 18:29:12 -07001227 EXPECT_LT(0U,
1228 session->flags() & cricket::PORTALLOCATOR_DISABLE_COSTLY_NETWORKS);
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001229}
1230
deadbeefd21eab32017-07-26 16:50:11 -07001231// Test that network-related RTCConfiguration members are applied to the
1232// PortAllocator when CreatePeerConnection is called. Specifically:
1233// - disable_ipv6_on_wifi
1234// - max_ipv6_networks
1235// - tcp_candidate_policy
1236// - candidate_network_policy
1237// - prune_turn_ports
1238//
1239// Note that the candidate filter (RTCConfiguration::type) is already tested
1240// above.
1241TEST_F(PeerConnectionInterfaceTest,
1242 CreatePeerConnectionAppliesNetworkConfigToPortAllocator) {
1243 // Create fake port allocator.
1244 std::unique_ptr<cricket::FakePortAllocator> port_allocator(
1245 new cricket::FakePortAllocator(rtc::Thread::Current(), nullptr));
1246 cricket::FakePortAllocator* raw_port_allocator = port_allocator.get();
1247
1248 // Create RTCConfiguration with some network-related fields relevant to
1249 // PortAllocator populated.
1250 PeerConnectionInterface::RTCConfiguration config;
1251 config.disable_ipv6_on_wifi = true;
1252 config.max_ipv6_networks = 10;
1253 config.tcp_candidate_policy =
1254 PeerConnectionInterface::kTcpCandidatePolicyDisabled;
1255 config.candidate_network_policy =
1256 PeerConnectionInterface::kCandidateNetworkPolicyLowCost;
1257 config.prune_turn_ports = true;
1258
1259 // Create the PC factory and PC with the above config.
1260 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface> pc_factory(
1261 webrtc::CreatePeerConnectionFactory(
1262 rtc::Thread::Current(), rtc::Thread::Current(),
deadbeefd7850b22017-08-23 10:59:19 -07001263 rtc::Thread::Current(), fake_audio_capture_module_, nullptr,
1264 nullptr));
deadbeefd21eab32017-07-26 16:50:11 -07001265 rtc::scoped_refptr<PeerConnectionInterface> pc(
1266 pc_factory->CreatePeerConnection(
1267 config, nullptr, std::move(port_allocator), nullptr, &observer_));
1268
1269 // Now validate that the config fields set above were applied to the
1270 // PortAllocator, as flags or otherwise.
1271 EXPECT_FALSE(raw_port_allocator->flags() &
1272 cricket::PORTALLOCATOR_ENABLE_IPV6_ON_WIFI);
1273 EXPECT_EQ(10, raw_port_allocator->max_ipv6_networks());
1274 EXPECT_TRUE(raw_port_allocator->flags() & cricket::PORTALLOCATOR_DISABLE_TCP);
1275 EXPECT_TRUE(raw_port_allocator->flags() &
1276 cricket::PORTALLOCATOR_DISABLE_COSTLY_NETWORKS);
1277 EXPECT_TRUE(raw_port_allocator->prune_turn_ports());
1278}
1279
Taylor Brandstetterf8e65772016-06-27 17:20:15 -07001280// Test that the PeerConnection initializes the port allocator passed into it,
1281// and on the correct thread.
1282TEST_F(PeerConnectionInterfaceTest,
deadbeefd21eab32017-07-26 16:50:11 -07001283 CreatePeerConnectionInitializesPortAllocatorOnNetworkThread) {
tommie7251592017-07-14 14:44:46 -07001284 std::unique_ptr<rtc::Thread> network_thread(
1285 rtc::Thread::CreateWithSocketServer());
1286 network_thread->Start();
Taylor Brandstetterf8e65772016-06-27 17:20:15 -07001287 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface> pc_factory(
1288 webrtc::CreatePeerConnectionFactory(
tommie7251592017-07-14 14:44:46 -07001289 network_thread.get(), rtc::Thread::Current(), rtc::Thread::Current(),
deadbeefd7850b22017-08-23 10:59:19 -07001290 fake_audio_capture_module_, nullptr, nullptr));
Taylor Brandstetterf8e65772016-06-27 17:20:15 -07001291 std::unique_ptr<cricket::FakePortAllocator> port_allocator(
tommie7251592017-07-14 14:44:46 -07001292 new cricket::FakePortAllocator(network_thread.get(), nullptr));
Taylor Brandstetterf8e65772016-06-27 17:20:15 -07001293 cricket::FakePortAllocator* raw_port_allocator = port_allocator.get();
1294 PeerConnectionInterface::RTCConfiguration config;
1295 rtc::scoped_refptr<PeerConnectionInterface> pc(
1296 pc_factory->CreatePeerConnection(
1297 config, nullptr, std::move(port_allocator), nullptr, &observer_));
1298 // FakePortAllocator RTC_CHECKs that it's initialized on the right thread,
1299 // so all we have to do here is check that it's initialized.
1300 EXPECT_TRUE(raw_port_allocator->initialized());
1301}
1302
deadbeef46c73892016-11-16 19:42:04 -08001303// Check that GetConfiguration returns the configuration the PeerConnection was
1304// constructed with, before SetConfiguration is called.
1305TEST_F(PeerConnectionInterfaceTest, GetConfigurationAfterCreatePeerConnection) {
1306 PeerConnectionInterface::RTCConfiguration config;
1307 config.type = PeerConnectionInterface::kRelay;
1308 CreatePeerConnection(config, nullptr);
1309
1310 PeerConnectionInterface::RTCConfiguration returned_config =
1311 pc_->GetConfiguration();
1312 EXPECT_EQ(PeerConnectionInterface::kRelay, returned_config.type);
1313}
1314
1315// Check that GetConfiguration returns the last configuration passed into
1316// SetConfiguration.
1317TEST_F(PeerConnectionInterfaceTest, GetConfigurationAfterSetConfiguration) {
1318 CreatePeerConnection();
1319
1320 PeerConnectionInterface::RTCConfiguration config;
1321 config.type = PeerConnectionInterface::kRelay;
1322 EXPECT_TRUE(pc_->SetConfiguration(config));
1323
1324 PeerConnectionInterface::RTCConfiguration returned_config =
1325 pc_->GetConfiguration();
1326 EXPECT_EQ(PeerConnectionInterface::kRelay, returned_config.type);
1327}
1328
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001329TEST_F(PeerConnectionInterfaceTest, AddStreams) {
deadbeef293e9262017-01-11 12:28:30 -08001330 CreatePeerConnectionWithoutDtls();
deadbeefab9b2d12015-10-14 11:33:11 -07001331 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001332 AddVoiceStream(kStreamLabel2);
1333 ASSERT_EQ(2u, pc_->local_streams()->count());
1334
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001335 // Test we can add multiple local streams to one peerconnection.
zhihuang9763d562016-08-05 11:14:50 -07001336 rtc::scoped_refptr<MediaStreamInterface> stream(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001337 pc_factory_->CreateLocalMediaStream(kStreamLabel3));
zhihuang9763d562016-08-05 11:14:50 -07001338 rtc::scoped_refptr<AudioTrackInterface> audio_track(
1339 pc_factory_->CreateAudioTrack(kStreamLabel3,
1340 static_cast<AudioSourceInterface*>(NULL)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001341 stream->AddTrack(audio_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +00001342 EXPECT_TRUE(pc_->AddStream(stream));
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001343 EXPECT_EQ(3u, pc_->local_streams()->count());
1344
1345 // Remove the third stream.
1346 pc_->RemoveStream(pc_->local_streams()->at(2));
1347 EXPECT_EQ(2u, pc_->local_streams()->count());
1348
1349 // Remove the second stream.
1350 pc_->RemoveStream(pc_->local_streams()->at(1));
1351 EXPECT_EQ(1u, pc_->local_streams()->count());
1352
1353 // Remove the first stream.
1354 pc_->RemoveStream(pc_->local_streams()->at(0));
1355 EXPECT_EQ(0u, pc_->local_streams()->count());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001356}
1357
deadbeefab9b2d12015-10-14 11:33:11 -07001358// Test that the created offer includes streams we added.
1359TEST_F(PeerConnectionInterfaceTest, AddedStreamsPresentInOffer) {
deadbeef293e9262017-01-11 12:28:30 -08001360 CreatePeerConnectionWithoutDtls();
deadbeefab9b2d12015-10-14 11:33:11 -07001361 AddAudioVideoStream(kStreamLabel1, "audio_track", "video_track");
kwibergd1fe2812016-04-27 06:47:29 -07001362 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001363 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07001364
1365 const cricket::ContentInfo* audio_content =
1366 cricket::GetFirstAudioContent(offer->description());
1367 const cricket::AudioContentDescription* audio_desc =
1368 static_cast<const cricket::AudioContentDescription*>(
1369 audio_content->description);
1370 EXPECT_TRUE(
1371 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
1372
1373 const cricket::ContentInfo* video_content =
1374 cricket::GetFirstVideoContent(offer->description());
1375 const cricket::VideoContentDescription* video_desc =
1376 static_cast<const cricket::VideoContentDescription*>(
1377 video_content->description);
1378 EXPECT_TRUE(
1379 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1380
1381 // Add another stream and ensure the offer includes both the old and new
1382 // streams.
1383 AddAudioVideoStream(kStreamLabel2, "audio_track2", "video_track2");
kwiberg2bbff992016-03-16 11:03:04 -07001384 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07001385
1386 audio_content = cricket::GetFirstAudioContent(offer->description());
1387 audio_desc = static_cast<const cricket::AudioContentDescription*>(
1388 audio_content->description);
1389 EXPECT_TRUE(
1390 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
1391 EXPECT_TRUE(
1392 ContainsTrack(audio_desc->streams(), kStreamLabel2, "audio_track2"));
1393
1394 video_content = cricket::GetFirstVideoContent(offer->description());
1395 video_desc = static_cast<const cricket::VideoContentDescription*>(
1396 video_content->description);
1397 EXPECT_TRUE(
1398 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1399 EXPECT_TRUE(
1400 ContainsTrack(video_desc->streams(), kStreamLabel2, "video_track2"));
1401}
1402
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001403TEST_F(PeerConnectionInterfaceTest, RemoveStream) {
deadbeef293e9262017-01-11 12:28:30 -08001404 CreatePeerConnectionWithoutDtls();
deadbeefab9b2d12015-10-14 11:33:11 -07001405 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001406 ASSERT_EQ(1u, pc_->local_streams()->count());
1407 pc_->RemoveStream(pc_->local_streams()->at(0));
1408 EXPECT_EQ(0u, pc_->local_streams()->count());
1409}
1410
deadbeefe1f9d832016-01-14 15:35:42 -08001411// Test for AddTrack and RemoveTrack methods.
1412// Tests that the created offer includes tracks we added,
1413// and that the RtpSenders are created correctly.
1414// Also tests that RemoveTrack removes the tracks from subsequent offers.
1415TEST_F(PeerConnectionInterfaceTest, AddTrackRemoveTrack) {
deadbeef293e9262017-01-11 12:28:30 -08001416 CreatePeerConnectionWithoutDtls();
deadbeefe1f9d832016-01-14 15:35:42 -08001417 // Create a dummy stream, so tracks share a stream label.
zhihuang9763d562016-08-05 11:14:50 -07001418 rtc::scoped_refptr<MediaStreamInterface> stream(
deadbeefe1f9d832016-01-14 15:35:42 -08001419 pc_factory_->CreateLocalMediaStream(kStreamLabel1));
1420 std::vector<MediaStreamInterface*> stream_list;
1421 stream_list.push_back(stream.get());
zhihuang9763d562016-08-05 11:14:50 -07001422 rtc::scoped_refptr<AudioTrackInterface> audio_track(
deadbeefe1f9d832016-01-14 15:35:42 -08001423 pc_factory_->CreateAudioTrack("audio_track", nullptr));
zhihuang9763d562016-08-05 11:14:50 -07001424 rtc::scoped_refptr<VideoTrackInterface> video_track(
1425 pc_factory_->CreateVideoTrack(
deadbeef112b2e92017-02-10 20:13:37 -08001426 "video_track", pc_factory_->CreateVideoSource(
1427 std::unique_ptr<cricket::VideoCapturer>(
1428 new cricket::FakeVideoCapturer()))));
deadbeefe1f9d832016-01-14 15:35:42 -08001429 auto audio_sender = pc_->AddTrack(audio_track, stream_list);
1430 auto video_sender = pc_->AddTrack(video_track, stream_list);
deadbeefa601f5c2016-06-06 14:27:39 -07001431 EXPECT_EQ(1UL, audio_sender->stream_ids().size());
1432 EXPECT_EQ(kStreamLabel1, audio_sender->stream_ids()[0]);
deadbeefe1f9d832016-01-14 15:35:42 -08001433 EXPECT_EQ("audio_track", audio_sender->id());
1434 EXPECT_EQ(audio_track, audio_sender->track());
deadbeefa601f5c2016-06-06 14:27:39 -07001435 EXPECT_EQ(1UL, video_sender->stream_ids().size());
1436 EXPECT_EQ(kStreamLabel1, video_sender->stream_ids()[0]);
deadbeefe1f9d832016-01-14 15:35:42 -08001437 EXPECT_EQ("video_track", video_sender->id());
1438 EXPECT_EQ(video_track, video_sender->track());
1439
1440 // Now create an offer and check for the senders.
kwibergd1fe2812016-04-27 06:47:29 -07001441 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001442 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefe1f9d832016-01-14 15:35:42 -08001443
1444 const cricket::ContentInfo* audio_content =
1445 cricket::GetFirstAudioContent(offer->description());
1446 const cricket::AudioContentDescription* audio_desc =
1447 static_cast<const cricket::AudioContentDescription*>(
1448 audio_content->description);
1449 EXPECT_TRUE(
1450 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
1451
1452 const cricket::ContentInfo* video_content =
1453 cricket::GetFirstVideoContent(offer->description());
1454 const cricket::VideoContentDescription* video_desc =
1455 static_cast<const cricket::VideoContentDescription*>(
1456 video_content->description);
1457 EXPECT_TRUE(
1458 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1459
Steve Antondb45ca82017-09-11 18:27:34 -07001460 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefe1f9d832016-01-14 15:35:42 -08001461
1462 // Now try removing the tracks.
1463 EXPECT_TRUE(pc_->RemoveTrack(audio_sender));
1464 EXPECT_TRUE(pc_->RemoveTrack(video_sender));
1465
1466 // Create a new offer and ensure it doesn't contain the removed senders.
kwiberg2bbff992016-03-16 11:03:04 -07001467 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefe1f9d832016-01-14 15:35:42 -08001468
1469 audio_content = cricket::GetFirstAudioContent(offer->description());
1470 audio_desc = static_cast<const cricket::AudioContentDescription*>(
1471 audio_content->description);
1472 EXPECT_FALSE(
1473 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
1474
1475 video_content = cricket::GetFirstVideoContent(offer->description());
1476 video_desc = static_cast<const cricket::VideoContentDescription*>(
1477 video_content->description);
1478 EXPECT_FALSE(
1479 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1480
Steve Antondb45ca82017-09-11 18:27:34 -07001481 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefe1f9d832016-01-14 15:35:42 -08001482
1483 // Calling RemoveTrack on a sender no longer attached to a PeerConnection
1484 // should return false.
1485 EXPECT_FALSE(pc_->RemoveTrack(audio_sender));
1486 EXPECT_FALSE(pc_->RemoveTrack(video_sender));
1487}
1488
1489// Test creating senders without a stream specified,
1490// expecting a random stream ID to be generated.
1491TEST_F(PeerConnectionInterfaceTest, AddTrackWithoutStream) {
deadbeef293e9262017-01-11 12:28:30 -08001492 CreatePeerConnectionWithoutDtls();
deadbeefe1f9d832016-01-14 15:35:42 -08001493 // Create a dummy stream, so tracks share a stream label.
zhihuang9763d562016-08-05 11:14:50 -07001494 rtc::scoped_refptr<AudioTrackInterface> audio_track(
deadbeefe1f9d832016-01-14 15:35:42 -08001495 pc_factory_->CreateAudioTrack("audio_track", nullptr));
zhihuang9763d562016-08-05 11:14:50 -07001496 rtc::scoped_refptr<VideoTrackInterface> video_track(
1497 pc_factory_->CreateVideoTrack(
deadbeef112b2e92017-02-10 20:13:37 -08001498 "video_track", pc_factory_->CreateVideoSource(
1499 std::unique_ptr<cricket::VideoCapturer>(
1500 new cricket::FakeVideoCapturer()))));
deadbeefe1f9d832016-01-14 15:35:42 -08001501 auto audio_sender =
1502 pc_->AddTrack(audio_track, std::vector<MediaStreamInterface*>());
1503 auto video_sender =
1504 pc_->AddTrack(video_track, std::vector<MediaStreamInterface*>());
1505 EXPECT_EQ("audio_track", audio_sender->id());
1506 EXPECT_EQ(audio_track, audio_sender->track());
1507 EXPECT_EQ("video_track", video_sender->id());
1508 EXPECT_EQ(video_track, video_sender->track());
1509 // If the ID is truly a random GUID, it should be infinitely unlikely they
1510 // will be the same.
deadbeefa601f5c2016-06-06 14:27:39 -07001511 EXPECT_NE(video_sender->stream_ids(), audio_sender->stream_ids());
deadbeefe1f9d832016-01-14 15:35:42 -08001512}
1513
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001514TEST_F(PeerConnectionInterfaceTest, CreateOfferReceiveAnswer) {
1515 InitiateCall();
1516 WaitAndVerifyOnAddStream(kStreamLabel1);
1517 VerifyRemoteRtpHeaderExtensions();
1518}
1519
1520TEST_F(PeerConnectionInterfaceTest, CreateOfferReceivePrAnswerAndAnswer) {
deadbeef293e9262017-01-11 12:28:30 -08001521 CreatePeerConnectionWithoutDtls();
deadbeefab9b2d12015-10-14 11:33:11 -07001522 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001523 CreateOfferAsLocalDescription();
1524 std::string offer;
1525 EXPECT_TRUE(pc_->local_description()->ToString(&offer));
1526 CreatePrAnswerAndAnswerAsRemoteDescription(offer);
1527 WaitAndVerifyOnAddStream(kStreamLabel1);
1528}
1529
1530TEST_F(PeerConnectionInterfaceTest, ReceiveOfferCreateAnswer) {
deadbeef293e9262017-01-11 12:28:30 -08001531 CreatePeerConnectionWithoutDtls();
deadbeefab9b2d12015-10-14 11:33:11 -07001532 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001533
1534 CreateOfferAsRemoteDescription();
1535 CreateAnswerAsLocalDescription();
1536
1537 WaitAndVerifyOnAddStream(kStreamLabel1);
1538}
1539
1540TEST_F(PeerConnectionInterfaceTest, ReceiveOfferCreatePrAnswerAndAnswer) {
deadbeef293e9262017-01-11 12:28:30 -08001541 CreatePeerConnectionWithoutDtls();
deadbeefab9b2d12015-10-14 11:33:11 -07001542 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001543
1544 CreateOfferAsRemoteDescription();
1545 CreatePrAnswerAsLocalDescription();
1546 CreateAnswerAsLocalDescription();
1547
1548 WaitAndVerifyOnAddStream(kStreamLabel1);
1549}
1550
1551TEST_F(PeerConnectionInterfaceTest, Renegotiate) {
1552 InitiateCall();
1553 ASSERT_EQ(1u, pc_->remote_streams()->count());
1554 pc_->RemoveStream(pc_->local_streams()->at(0));
1555 CreateOfferReceiveAnswer();
1556 EXPECT_EQ(0u, pc_->remote_streams()->count());
deadbeefab9b2d12015-10-14 11:33:11 -07001557 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001558 CreateOfferReceiveAnswer();
1559}
1560
1561// Tests that after negotiating an audio only call, the respondent can perform a
1562// renegotiation that removes the audio stream.
1563TEST_F(PeerConnectionInterfaceTest, RenegotiateAudioOnly) {
deadbeef293e9262017-01-11 12:28:30 -08001564 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001565 AddVoiceStream(kStreamLabel1);
1566 CreateOfferAsRemoteDescription();
1567 CreateAnswerAsLocalDescription();
1568
1569 ASSERT_EQ(1u, pc_->remote_streams()->count());
1570 pc_->RemoveStream(pc_->local_streams()->at(0));
1571 CreateOfferReceiveAnswer();
1572 EXPECT_EQ(0u, pc_->remote_streams()->count());
1573}
1574
1575// Test that candidates are generated and that we can parse our own candidates.
1576TEST_F(PeerConnectionInterfaceTest, IceCandidates) {
deadbeef293e9262017-01-11 12:28:30 -08001577 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001578
1579 EXPECT_FALSE(pc_->AddIceCandidate(observer_.last_candidate_.get()));
1580 // SetRemoteDescription takes ownership of offer.
kwibergd1fe2812016-04-27 06:47:29 -07001581 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefab9b2d12015-10-14 11:33:11 -07001582 AddVideoStream(kStreamLabel1);
deadbeefc80741f2015-10-22 13:14:45 -07001583 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07001584 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001585
1586 // SetLocalDescription takes ownership of answer.
kwibergd1fe2812016-04-27 06:47:29 -07001587 std::unique_ptr<SessionDescriptionInterface> answer;
deadbeefc80741f2015-10-22 13:14:45 -07001588 EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07001589 EXPECT_TRUE(DoSetLocalDescription(std::move(answer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001590
1591 EXPECT_TRUE_WAIT(observer_.last_candidate_.get() != NULL, kTimeout);
1592 EXPECT_TRUE_WAIT(observer_.ice_complete_, kTimeout);
1593
1594 EXPECT_TRUE(pc_->AddIceCandidate(observer_.last_candidate_.get()));
1595}
1596
deadbeefab9b2d12015-10-14 11:33:11 -07001597// Test that CreateOffer and CreateAnswer will fail if the track labels are
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001598// not unique.
1599TEST_F(PeerConnectionInterfaceTest, CreateOfferAnswerWithInvalidStream) {
deadbeef293e9262017-01-11 12:28:30 -08001600 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001601 // Create a regular offer for the CreateAnswer test later.
kwibergd1fe2812016-04-27 06:47:29 -07001602 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefc80741f2015-10-22 13:14:45 -07001603 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
kwiberg2bbff992016-03-16 11:03:04 -07001604 EXPECT_TRUE(offer);
1605 offer.reset();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001606
1607 // Create a local stream with audio&video tracks having same label.
1608 AddAudioVideoStream(kStreamLabel1, "track_label", "track_label");
1609
1610 // Test CreateOffer
deadbeefc80741f2015-10-22 13:14:45 -07001611 EXPECT_FALSE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001612
1613 // Test CreateAnswer
kwibergd1fe2812016-04-27 06:47:29 -07001614 std::unique_ptr<SessionDescriptionInterface> answer;
deadbeefc80741f2015-10-22 13:14:45 -07001615 EXPECT_FALSE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001616}
1617
1618// Test that we will get different SSRCs for each tracks in the offer and answer
1619// we created.
1620TEST_F(PeerConnectionInterfaceTest, SsrcInOfferAnswer) {
deadbeef293e9262017-01-11 12:28:30 -08001621 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001622 // Create a local stream with audio&video tracks having different labels.
1623 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
1624
1625 // Test CreateOffer
kwibergd1fe2812016-04-27 06:47:29 -07001626 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001627 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001628 int audio_ssrc = 0;
1629 int video_ssrc = 0;
1630 EXPECT_TRUE(GetFirstSsrc(GetFirstAudioContent(offer->description()),
1631 &audio_ssrc));
1632 EXPECT_TRUE(GetFirstSsrc(GetFirstVideoContent(offer->description()),
1633 &video_ssrc));
1634 EXPECT_NE(audio_ssrc, video_ssrc);
1635
1636 // Test CreateAnswer
Steve Antondb45ca82017-09-11 18:27:34 -07001637 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
kwibergd1fe2812016-04-27 06:47:29 -07001638 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -07001639 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001640 audio_ssrc = 0;
1641 video_ssrc = 0;
1642 EXPECT_TRUE(GetFirstSsrc(GetFirstAudioContent(answer->description()),
1643 &audio_ssrc));
1644 EXPECT_TRUE(GetFirstSsrc(GetFirstVideoContent(answer->description()),
1645 &video_ssrc));
1646 EXPECT_NE(audio_ssrc, video_ssrc);
1647}
1648
deadbeefeb459812015-12-15 19:24:43 -08001649// Test that it's possible to call AddTrack on a MediaStream after adding
1650// the stream to a PeerConnection.
1651// TODO(deadbeef): Remove this test once this behavior is no longer supported.
1652TEST_F(PeerConnectionInterfaceTest, AddTrackAfterAddStream) {
deadbeef293e9262017-01-11 12:28:30 -08001653 CreatePeerConnectionWithoutDtls();
deadbeefeb459812015-12-15 19:24:43 -08001654 // Create audio stream and add to PeerConnection.
1655 AddVoiceStream(kStreamLabel1);
1656 MediaStreamInterface* stream = pc_->local_streams()->at(0);
1657
1658 // Add video track to the audio-only stream.
zhihuang9763d562016-08-05 11:14:50 -07001659 rtc::scoped_refptr<VideoTrackInterface> video_track(
1660 pc_factory_->CreateVideoTrack(
deadbeef112b2e92017-02-10 20:13:37 -08001661 "video_label", pc_factory_->CreateVideoSource(
1662 std::unique_ptr<cricket::VideoCapturer>(
1663 new cricket::FakeVideoCapturer()))));
deadbeefeb459812015-12-15 19:24:43 -08001664 stream->AddTrack(video_track.get());
1665
kwibergd1fe2812016-04-27 06:47:29 -07001666 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001667 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefeb459812015-12-15 19:24:43 -08001668
1669 const cricket::MediaContentDescription* video_desc =
1670 cricket::GetFirstVideoContentDescription(offer->description());
1671 EXPECT_TRUE(video_desc != nullptr);
1672}
1673
1674// Test that it's possible to call RemoveTrack on a MediaStream after adding
1675// the stream to a PeerConnection.
1676// TODO(deadbeef): Remove this test once this behavior is no longer supported.
1677TEST_F(PeerConnectionInterfaceTest, RemoveTrackAfterAddStream) {
deadbeef293e9262017-01-11 12:28:30 -08001678 CreatePeerConnectionWithoutDtls();
deadbeefeb459812015-12-15 19:24:43 -08001679 // Create audio/video stream and add to PeerConnection.
1680 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
1681 MediaStreamInterface* stream = pc_->local_streams()->at(0);
1682
1683 // Remove the video track.
1684 stream->RemoveTrack(stream->GetVideoTracks()[0]);
1685
kwibergd1fe2812016-04-27 06:47:29 -07001686 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001687 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefeb459812015-12-15 19:24:43 -08001688
1689 const cricket::MediaContentDescription* video_desc =
1690 cricket::GetFirstVideoContentDescription(offer->description());
1691 EXPECT_TRUE(video_desc == nullptr);
1692}
1693
deadbeef1dcb1642017-03-29 21:08:16 -07001694// Verify that CreateDtmfSender only succeeds if called with a valid local
1695// track. Other aspects of DtmfSenders are tested in
1696// peerconnection_integrationtest.cc.
1697TEST_F(PeerConnectionInterfaceTest, CreateDtmfSenderWithInvalidParams) {
1698 CreatePeerConnection();
1699 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
1700 EXPECT_EQ(nullptr, pc_->CreateDtmfSender(nullptr));
1701 rtc::scoped_refptr<webrtc::AudioTrackInterface> non_localtrack(
1702 pc_factory_->CreateAudioTrack("dummy_track", nullptr));
1703 EXPECT_EQ(nullptr, pc_->CreateDtmfSender(non_localtrack));
1704}
1705
deadbeefbd7d8f72015-12-18 16:58:44 -08001706// Test creating a sender with a stream ID, and ensure the ID is populated
1707// in the offer.
1708TEST_F(PeerConnectionInterfaceTest, CreateSenderWithStream) {
deadbeef293e9262017-01-11 12:28:30 -08001709 CreatePeerConnectionWithoutDtls();
deadbeefbd7d8f72015-12-18 16:58:44 -08001710 pc_->CreateSender("video", kStreamLabel1);
1711
kwibergd1fe2812016-04-27 06:47:29 -07001712 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001713 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefbd7d8f72015-12-18 16:58:44 -08001714
1715 const cricket::MediaContentDescription* video_desc =
1716 cricket::GetFirstVideoContentDescription(offer->description());
1717 ASSERT_TRUE(video_desc != nullptr);
1718 ASSERT_EQ(1u, video_desc->streams().size());
1719 EXPECT_EQ(kStreamLabel1, video_desc->streams()[0].sync_label);
1720}
1721
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001722// Test that we can specify a certain track that we want statistics about.
1723TEST_F(PeerConnectionInterfaceTest, GetStatsForSpecificTrack) {
1724 InitiateCall();
1725 ASSERT_LT(0u, pc_->remote_streams()->count());
1726 ASSERT_LT(0u, pc_->remote_streams()->at(0)->GetAudioTracks().size());
zhihuang9763d562016-08-05 11:14:50 -07001727 rtc::scoped_refptr<MediaStreamTrackInterface> remote_audio =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001728 pc_->remote_streams()->at(0)->GetAudioTracks()[0];
1729 EXPECT_TRUE(DoGetStats(remote_audio));
1730
1731 // Remove the stream. Since we are sending to our selves the local
1732 // and the remote stream is the same.
1733 pc_->RemoveStream(pc_->local_streams()->at(0));
1734 // Do a re-negotiation.
1735 CreateOfferReceiveAnswer();
1736
1737 ASSERT_EQ(0u, pc_->remote_streams()->count());
1738
1739 // Test that we still can get statistics for the old track. Even if it is not
1740 // sent any longer.
1741 EXPECT_TRUE(DoGetStats(remote_audio));
1742}
1743
1744// Test that we can get stats on a video track.
1745TEST_F(PeerConnectionInterfaceTest, GetStatsForVideoTrack) {
1746 InitiateCall();
1747 ASSERT_LT(0u, pc_->remote_streams()->count());
1748 ASSERT_LT(0u, pc_->remote_streams()->at(0)->GetVideoTracks().size());
zhihuang9763d562016-08-05 11:14:50 -07001749 rtc::scoped_refptr<MediaStreamTrackInterface> remote_video =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001750 pc_->remote_streams()->at(0)->GetVideoTracks()[0];
1751 EXPECT_TRUE(DoGetStats(remote_video));
1752}
1753
1754// Test that we don't get statistics for an invalid track.
zhihuange9e94c32016-11-04 11:38:15 -07001755TEST_F(PeerConnectionInterfaceTest, GetStatsForInvalidTrack) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001756 InitiateCall();
zhihuang9763d562016-08-05 11:14:50 -07001757 rtc::scoped_refptr<AudioTrackInterface> unknown_audio_track(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001758 pc_factory_->CreateAudioTrack("unknown track", NULL));
1759 EXPECT_FALSE(DoGetStats(unknown_audio_track));
1760}
1761
1762// This test setup two RTP data channels in loop back.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001763TEST_F(PeerConnectionInterfaceTest, TestDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001764 FakeConstraints constraints;
1765 constraints.SetAllowRtpDataChannels();
1766 CreatePeerConnection(&constraints);
zhihuang9763d562016-08-05 11:14:50 -07001767 rtc::scoped_refptr<DataChannelInterface> data1 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001768 pc_->CreateDataChannel("test1", NULL);
zhihuang9763d562016-08-05 11:14:50 -07001769 rtc::scoped_refptr<DataChannelInterface> data2 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001770 pc_->CreateDataChannel("test2", NULL);
1771 ASSERT_TRUE(data1 != NULL);
kwibergd1fe2812016-04-27 06:47:29 -07001772 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001773 new MockDataChannelObserver(data1));
kwibergd1fe2812016-04-27 06:47:29 -07001774 std::unique_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001775 new MockDataChannelObserver(data2));
1776
1777 EXPECT_EQ(DataChannelInterface::kConnecting, data1->state());
1778 EXPECT_EQ(DataChannelInterface::kConnecting, data2->state());
1779 std::string data_to_send1 = "testing testing";
1780 std::string data_to_send2 = "testing something else";
1781 EXPECT_FALSE(data1->Send(DataBuffer(data_to_send1)));
1782
1783 CreateOfferReceiveAnswer();
1784 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1785 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
1786
1787 EXPECT_EQ(DataChannelInterface::kOpen, data1->state());
1788 EXPECT_EQ(DataChannelInterface::kOpen, data2->state());
1789 EXPECT_TRUE(data1->Send(DataBuffer(data_to_send1)));
1790 EXPECT_TRUE(data2->Send(DataBuffer(data_to_send2)));
1791
1792 EXPECT_EQ_WAIT(data_to_send1, observer1->last_message(), kTimeout);
1793 EXPECT_EQ_WAIT(data_to_send2, observer2->last_message(), kTimeout);
1794
1795 data1->Close();
1796 EXPECT_EQ(DataChannelInterface::kClosing, data1->state());
1797 CreateOfferReceiveAnswer();
1798 EXPECT_FALSE(observer1->IsOpen());
1799 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
1800 EXPECT_TRUE(observer2->IsOpen());
1801
1802 data_to_send2 = "testing something else again";
1803 EXPECT_TRUE(data2->Send(DataBuffer(data_to_send2)));
1804
1805 EXPECT_EQ_WAIT(data_to_send2, observer2->last_message(), kTimeout);
1806}
1807
1808// This test verifies that sendnig binary data over RTP data channels should
1809// fail.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001810TEST_F(PeerConnectionInterfaceTest, TestSendBinaryOnRtpDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001811 FakeConstraints constraints;
1812 constraints.SetAllowRtpDataChannels();
1813 CreatePeerConnection(&constraints);
zhihuang9763d562016-08-05 11:14:50 -07001814 rtc::scoped_refptr<DataChannelInterface> data1 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001815 pc_->CreateDataChannel("test1", NULL);
zhihuang9763d562016-08-05 11:14:50 -07001816 rtc::scoped_refptr<DataChannelInterface> data2 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001817 pc_->CreateDataChannel("test2", NULL);
1818 ASSERT_TRUE(data1 != NULL);
kwibergd1fe2812016-04-27 06:47:29 -07001819 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001820 new MockDataChannelObserver(data1));
kwibergd1fe2812016-04-27 06:47:29 -07001821 std::unique_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001822 new MockDataChannelObserver(data2));
1823
1824 EXPECT_EQ(DataChannelInterface::kConnecting, data1->state());
1825 EXPECT_EQ(DataChannelInterface::kConnecting, data2->state());
1826
1827 CreateOfferReceiveAnswer();
1828 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1829 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
1830
1831 EXPECT_EQ(DataChannelInterface::kOpen, data1->state());
1832 EXPECT_EQ(DataChannelInterface::kOpen, data2->state());
1833
jbaucheec21bd2016-03-20 06:15:43 -07001834 rtc::CopyOnWriteBuffer buffer("test", 4);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001835 EXPECT_FALSE(data1->Send(DataBuffer(buffer, true)));
1836}
1837
1838// This test setup a RTP data channels in loop back and test that a channel is
1839// opened even if the remote end answer with a zero SSRC.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001840TEST_F(PeerConnectionInterfaceTest, TestSendOnlyDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001841 FakeConstraints constraints;
1842 constraints.SetAllowRtpDataChannels();
1843 CreatePeerConnection(&constraints);
zhihuang9763d562016-08-05 11:14:50 -07001844 rtc::scoped_refptr<DataChannelInterface> data1 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001845 pc_->CreateDataChannel("test1", NULL);
kwibergd1fe2812016-04-27 06:47:29 -07001846 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001847 new MockDataChannelObserver(data1));
1848
1849 CreateOfferReceiveAnswerWithoutSsrc();
1850
1851 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1852
1853 data1->Close();
1854 EXPECT_EQ(DataChannelInterface::kClosing, data1->state());
1855 CreateOfferReceiveAnswerWithoutSsrc();
1856 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
1857 EXPECT_FALSE(observer1->IsOpen());
1858}
1859
1860// This test that if a data channel is added in an answer a receive only channel
1861// channel is created.
1862TEST_F(PeerConnectionInterfaceTest, TestReceiveOnlyDataChannel) {
1863 FakeConstraints constraints;
1864 constraints.SetAllowRtpDataChannels();
1865 CreatePeerConnection(&constraints);
1866
1867 std::string offer_label = "offer_channel";
zhihuang9763d562016-08-05 11:14:50 -07001868 rtc::scoped_refptr<DataChannelInterface> offer_channel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001869 pc_->CreateDataChannel(offer_label, NULL);
1870
1871 CreateOfferAsLocalDescription();
1872
1873 // Replace the data channel label in the offer and apply it as an answer.
1874 std::string receive_label = "answer_channel";
1875 std::string sdp;
1876 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001877 rtc::replace_substrs(offer_label.c_str(), offer_label.length(),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001878 receive_label.c_str(), receive_label.length(),
1879 &sdp);
1880 CreateAnswerAsRemoteDescription(sdp);
1881
1882 // Verify that a new incoming data channel has been created and that
1883 // it is open but can't we written to.
1884 ASSERT_TRUE(observer_.last_datachannel_ != NULL);
1885 DataChannelInterface* received_channel = observer_.last_datachannel_;
1886 EXPECT_EQ(DataChannelInterface::kConnecting, received_channel->state());
1887 EXPECT_EQ(receive_label, received_channel->label());
1888 EXPECT_FALSE(received_channel->Send(DataBuffer("something")));
1889
1890 // Verify that the channel we initially offered has been rejected.
1891 EXPECT_EQ(DataChannelInterface::kClosed, offer_channel->state());
1892
1893 // Do another offer / answer exchange and verify that the data channel is
1894 // opened.
1895 CreateOfferReceiveAnswer();
1896 EXPECT_EQ_WAIT(DataChannelInterface::kOpen, received_channel->state(),
1897 kTimeout);
1898}
1899
1900// This test that no data channel is returned if a reliable channel is
1901// requested.
1902// TODO(perkj): Remove this test once reliable channels are implemented.
1903TEST_F(PeerConnectionInterfaceTest, CreateReliableRtpDataChannelShouldFail) {
1904 FakeConstraints constraints;
1905 constraints.SetAllowRtpDataChannels();
1906 CreatePeerConnection(&constraints);
1907
1908 std::string label = "test";
1909 webrtc::DataChannelInit config;
1910 config.reliable = true;
zhihuang9763d562016-08-05 11:14:50 -07001911 rtc::scoped_refptr<DataChannelInterface> channel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001912 pc_->CreateDataChannel(label, &config);
1913 EXPECT_TRUE(channel == NULL);
1914}
1915
deadbeefab9b2d12015-10-14 11:33:11 -07001916// Verifies that duplicated label is not allowed for RTP data channel.
1917TEST_F(PeerConnectionInterfaceTest, RtpDuplicatedLabelNotAllowed) {
1918 FakeConstraints constraints;
1919 constraints.SetAllowRtpDataChannels();
1920 CreatePeerConnection(&constraints);
1921
1922 std::string label = "test";
zhihuang9763d562016-08-05 11:14:50 -07001923 rtc::scoped_refptr<DataChannelInterface> channel =
deadbeefab9b2d12015-10-14 11:33:11 -07001924 pc_->CreateDataChannel(label, nullptr);
1925 EXPECT_NE(channel, nullptr);
1926
zhihuang9763d562016-08-05 11:14:50 -07001927 rtc::scoped_refptr<DataChannelInterface> dup_channel =
deadbeefab9b2d12015-10-14 11:33:11 -07001928 pc_->CreateDataChannel(label, nullptr);
1929 EXPECT_EQ(dup_channel, nullptr);
1930}
1931
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001932// This tests that a SCTP data channel is returned using different
1933// DataChannelInit configurations.
1934TEST_F(PeerConnectionInterfaceTest, CreateSctpDataChannel) {
1935 FakeConstraints constraints;
1936 constraints.SetAllowDtlsSctpDataChannels();
1937 CreatePeerConnection(&constraints);
1938
1939 webrtc::DataChannelInit config;
1940
zhihuang9763d562016-08-05 11:14:50 -07001941 rtc::scoped_refptr<DataChannelInterface> channel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001942 pc_->CreateDataChannel("1", &config);
1943 EXPECT_TRUE(channel != NULL);
1944 EXPECT_TRUE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001945 EXPECT_TRUE(observer_.renegotiation_needed_);
1946 observer_.renegotiation_needed_ = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001947
1948 config.ordered = false;
1949 channel = pc_->CreateDataChannel("2", &config);
1950 EXPECT_TRUE(channel != NULL);
1951 EXPECT_TRUE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001952 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001953
1954 config.ordered = true;
1955 config.maxRetransmits = 0;
1956 channel = pc_->CreateDataChannel("3", &config);
1957 EXPECT_TRUE(channel != NULL);
1958 EXPECT_FALSE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001959 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001960
1961 config.maxRetransmits = -1;
1962 config.maxRetransmitTime = 0;
1963 channel = pc_->CreateDataChannel("4", &config);
1964 EXPECT_TRUE(channel != NULL);
1965 EXPECT_FALSE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001966 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001967}
1968
1969// This tests that no data channel is returned if both maxRetransmits and
1970// maxRetransmitTime are set for SCTP data channels.
1971TEST_F(PeerConnectionInterfaceTest,
1972 CreateSctpDataChannelShouldFailForInvalidConfig) {
1973 FakeConstraints constraints;
1974 constraints.SetAllowDtlsSctpDataChannels();
1975 CreatePeerConnection(&constraints);
1976
1977 std::string label = "test";
1978 webrtc::DataChannelInit config;
1979 config.maxRetransmits = 0;
1980 config.maxRetransmitTime = 0;
1981
zhihuang9763d562016-08-05 11:14:50 -07001982 rtc::scoped_refptr<DataChannelInterface> channel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001983 pc_->CreateDataChannel(label, &config);
1984 EXPECT_TRUE(channel == NULL);
1985}
1986
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001987// The test verifies that creating a SCTP data channel with an id already in use
1988// or out of range should fail.
1989TEST_F(PeerConnectionInterfaceTest,
1990 CreateSctpDataChannelWithInvalidIdShouldFail) {
1991 FakeConstraints constraints;
1992 constraints.SetAllowDtlsSctpDataChannels();
1993 CreatePeerConnection(&constraints);
1994
1995 webrtc::DataChannelInit config;
zhihuang9763d562016-08-05 11:14:50 -07001996 rtc::scoped_refptr<DataChannelInterface> channel;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001997
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001998 config.id = 1;
1999 channel = pc_->CreateDataChannel("1", &config);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002000 EXPECT_TRUE(channel != NULL);
2001 EXPECT_EQ(1, channel->id());
2002
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002003 channel = pc_->CreateDataChannel("x", &config);
2004 EXPECT_TRUE(channel == NULL);
2005
2006 config.id = cricket::kMaxSctpSid;
2007 channel = pc_->CreateDataChannel("max", &config);
2008 EXPECT_TRUE(channel != NULL);
2009 EXPECT_EQ(config.id, channel->id());
2010
2011 config.id = cricket::kMaxSctpSid + 1;
2012 channel = pc_->CreateDataChannel("x", &config);
2013 EXPECT_TRUE(channel == NULL);
2014}
2015
deadbeefab9b2d12015-10-14 11:33:11 -07002016// Verifies that duplicated label is allowed for SCTP data channel.
2017TEST_F(PeerConnectionInterfaceTest, SctpDuplicatedLabelAllowed) {
2018 FakeConstraints constraints;
2019 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2020 true);
2021 CreatePeerConnection(&constraints);
2022
2023 std::string label = "test";
zhihuang9763d562016-08-05 11:14:50 -07002024 rtc::scoped_refptr<DataChannelInterface> channel =
deadbeefab9b2d12015-10-14 11:33:11 -07002025 pc_->CreateDataChannel(label, nullptr);
2026 EXPECT_NE(channel, nullptr);
2027
zhihuang9763d562016-08-05 11:14:50 -07002028 rtc::scoped_refptr<DataChannelInterface> dup_channel =
deadbeefab9b2d12015-10-14 11:33:11 -07002029 pc_->CreateDataChannel(label, nullptr);
2030 EXPECT_NE(dup_channel, nullptr);
2031}
2032
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00002033// This test verifies that OnRenegotiationNeeded is fired for every new RTP
2034// DataChannel.
2035TEST_F(PeerConnectionInterfaceTest, RenegotiationNeededForNewRtpDataChannel) {
2036 FakeConstraints constraints;
2037 constraints.SetAllowRtpDataChannels();
2038 CreatePeerConnection(&constraints);
2039
zhihuang9763d562016-08-05 11:14:50 -07002040 rtc::scoped_refptr<DataChannelInterface> dc1 =
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00002041 pc_->CreateDataChannel("test1", NULL);
2042 EXPECT_TRUE(observer_.renegotiation_needed_);
2043 observer_.renegotiation_needed_ = false;
2044
zhihuang9763d562016-08-05 11:14:50 -07002045 rtc::scoped_refptr<DataChannelInterface> dc2 =
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00002046 pc_->CreateDataChannel("test2", NULL);
2047 EXPECT_TRUE(observer_.renegotiation_needed_);
2048}
2049
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002050// This test that a data channel closes when a PeerConnection is deleted/closed.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002051TEST_F(PeerConnectionInterfaceTest, DataChannelCloseWhenPeerConnectionClose) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002052 FakeConstraints constraints;
2053 constraints.SetAllowRtpDataChannels();
2054 CreatePeerConnection(&constraints);
2055
zhihuang9763d562016-08-05 11:14:50 -07002056 rtc::scoped_refptr<DataChannelInterface> data1 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002057 pc_->CreateDataChannel("test1", NULL);
zhihuang9763d562016-08-05 11:14:50 -07002058 rtc::scoped_refptr<DataChannelInterface> data2 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002059 pc_->CreateDataChannel("test2", NULL);
2060 ASSERT_TRUE(data1 != NULL);
kwibergd1fe2812016-04-27 06:47:29 -07002061 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002062 new MockDataChannelObserver(data1));
kwibergd1fe2812016-04-27 06:47:29 -07002063 std::unique_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002064 new MockDataChannelObserver(data2));
2065
2066 CreateOfferReceiveAnswer();
2067 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
2068 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
2069
2070 ReleasePeerConnection();
2071 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
2072 EXPECT_EQ(DataChannelInterface::kClosed, data2->state());
2073}
2074
2075// This test that data channels can be rejected in an answer.
2076TEST_F(PeerConnectionInterfaceTest, TestRejectDataChannelInAnswer) {
2077 FakeConstraints constraints;
2078 constraints.SetAllowRtpDataChannels();
2079 CreatePeerConnection(&constraints);
2080
zhihuang9763d562016-08-05 11:14:50 -07002081 rtc::scoped_refptr<DataChannelInterface> offer_channel(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002082 pc_->CreateDataChannel("offer_channel", NULL));
2083
2084 CreateOfferAsLocalDescription();
2085
2086 // Create an answer where the m-line for data channels are rejected.
2087 std::string sdp;
2088 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07002089 std::unique_ptr<SessionDescriptionInterface> answer(
2090 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
2091 sdp, nullptr));
2092 ASSERT_TRUE(answer);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002093 cricket::ContentInfo* data_info =
2094 answer->description()->GetContentByName("data");
2095 data_info->rejected = true;
2096
Steve Antondb45ca82017-09-11 18:27:34 -07002097 DoSetRemoteDescription(std::move(answer));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002098 EXPECT_EQ(DataChannelInterface::kClosed, offer_channel->state());
2099}
2100
2101// Test that we can create a session description from an SDP string from
2102// FireFox, use it as a remote session description, generate an answer and use
2103// the answer as a local description.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002104TEST_F(PeerConnectionInterfaceTest, ReceiveFireFoxOffer) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002105 FakeConstraints constraints;
2106 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2107 true);
2108 CreatePeerConnection(&constraints);
2109 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
Steve Antondb45ca82017-09-11 18:27:34 -07002110 std::unique_ptr<SessionDescriptionInterface> desc(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002111 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
Steve Antondb45ca82017-09-11 18:27:34 -07002112 webrtc::kFireFoxSdpOffer, nullptr));
2113 EXPECT_TRUE(DoSetSessionDescription(std::move(desc), false));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002114 CreateAnswerAsLocalDescription();
2115 ASSERT_TRUE(pc_->local_description() != NULL);
2116 ASSERT_TRUE(pc_->remote_description() != NULL);
2117
2118 const cricket::ContentInfo* content =
2119 cricket::GetFirstAudioContent(pc_->local_description()->description());
2120 ASSERT_TRUE(content != NULL);
2121 EXPECT_FALSE(content->rejected);
2122
2123 content =
2124 cricket::GetFirstVideoContent(pc_->local_description()->description());
2125 ASSERT_TRUE(content != NULL);
2126 EXPECT_FALSE(content->rejected);
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00002127#ifdef HAVE_SCTP
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002128 content =
2129 cricket::GetFirstDataContent(pc_->local_description()->description());
2130 ASSERT_TRUE(content != NULL);
2131 EXPECT_TRUE(content->rejected);
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00002132#endif
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002133}
2134
zhihuangb19012e2017-09-19 13:47:59 -07002135// Test that fallback from DTLS to SDES is not supported.
2136// The fallback was previously supported but was removed to simplify the code
2137// and because it's non-standard.
2138TEST_F(PeerConnectionInterfaceTest, DtlsSdesFallbackNotSupported) {
deadbeef8662f942017-01-20 21:20:51 -08002139 FakeConstraints constraints;
2140 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2141 true);
2142 CreatePeerConnection(&constraints);
2143 // Wait for fake certificate to be generated. Previously, this is what caused
2144 // the "a=crypto" lines to be rejected.
2145 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
2146 ASSERT_NE(nullptr, fake_certificate_generator_);
2147 EXPECT_EQ_WAIT(1, fake_certificate_generator_->generated_certificates(),
2148 kTimeout);
Steve Antondb45ca82017-09-11 18:27:34 -07002149 std::unique_ptr<SessionDescriptionInterface> desc(
2150 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
2151 kDtlsSdesFallbackSdp, nullptr));
zhihuangb19012e2017-09-19 13:47:59 -07002152 EXPECT_FALSE(DoSetSessionDescription(std::move(desc), false));
deadbeef8662f942017-01-20 21:20:51 -08002153}
2154
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002155// Test that we can create an audio only offer and receive an answer with a
2156// limited set of audio codecs and receive an updated offer with more audio
2157// codecs, where the added codecs are not supported.
2158TEST_F(PeerConnectionInterfaceTest, ReceiveUpdatedAudioOfferWithBadCodecs) {
deadbeef293e9262017-01-11 12:28:30 -08002159 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002160 AddVoiceStream("audio_label");
2161 CreateOfferAsLocalDescription();
2162
Steve Antondb45ca82017-09-11 18:27:34 -07002163 std::unique_ptr<SessionDescriptionInterface> answer(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002164 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
Steve Antondb45ca82017-09-11 18:27:34 -07002165 webrtc::kAudioSdp, nullptr));
2166 EXPECT_TRUE(DoSetSessionDescription(std::move(answer), false));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002167
Steve Antondb45ca82017-09-11 18:27:34 -07002168 std::unique_ptr<SessionDescriptionInterface> updated_offer(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002169 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
jbauchfabe2c92015-07-16 13:43:14 -07002170 webrtc::kAudioSdpWithUnsupportedCodecs,
Steve Antondb45ca82017-09-11 18:27:34 -07002171 nullptr));
2172 EXPECT_TRUE(DoSetSessionDescription(std::move(updated_offer), false));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002173 CreateAnswerAsLocalDescription();
2174}
2175
deadbeefc80741f2015-10-22 13:14:45 -07002176// Test that if we're receiving (but not sending) a track, subsequent offers
2177// will have m-lines with a=recvonly.
2178TEST_F(PeerConnectionInterfaceTest, CreateSubsequentRecvOnlyOffer) {
2179 FakeConstraints constraints;
2180 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2181 true);
2182 CreatePeerConnection(&constraints);
2183 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2184 CreateAnswerAsLocalDescription();
2185
2186 // At this point we should be receiving stream 1, but not sending anything.
2187 // A new offer should be recvonly.
kwibergd1fe2812016-04-27 06:47:29 -07002188 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefc80741f2015-10-22 13:14:45 -07002189 DoCreateOffer(&offer, nullptr);
2190
2191 const cricket::ContentInfo* video_content =
2192 cricket::GetFirstVideoContent(offer->description());
2193 const cricket::VideoContentDescription* video_desc =
2194 static_cast<const cricket::VideoContentDescription*>(
2195 video_content->description);
2196 ASSERT_EQ(cricket::MD_RECVONLY, video_desc->direction());
2197
2198 const cricket::ContentInfo* audio_content =
2199 cricket::GetFirstAudioContent(offer->description());
2200 const cricket::AudioContentDescription* audio_desc =
2201 static_cast<const cricket::AudioContentDescription*>(
2202 audio_content->description);
2203 ASSERT_EQ(cricket::MD_RECVONLY, audio_desc->direction());
2204}
2205
2206// Test that if we're receiving (but not sending) a track, and the
2207// offerToReceiveVideo/offerToReceiveAudio constraints are explicitly set to
2208// false, the generated m-lines will be a=inactive.
2209TEST_F(PeerConnectionInterfaceTest, CreateSubsequentInactiveOffer) {
2210 FakeConstraints constraints;
2211 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2212 true);
2213 CreatePeerConnection(&constraints);
2214 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2215 CreateAnswerAsLocalDescription();
2216
2217 // At this point we should be receiving stream 1, but not sending anything.
2218 // A new offer would be recvonly, but we'll set the "no receive" constraints
2219 // to make it inactive.
kwibergd1fe2812016-04-27 06:47:29 -07002220 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefc80741f2015-10-22 13:14:45 -07002221 FakeConstraints offer_constraints;
2222 offer_constraints.AddMandatory(
2223 webrtc::MediaConstraintsInterface::kOfferToReceiveVideo, false);
2224 offer_constraints.AddMandatory(
2225 webrtc::MediaConstraintsInterface::kOfferToReceiveAudio, false);
2226 DoCreateOffer(&offer, &offer_constraints);
2227
2228 const cricket::ContentInfo* video_content =
2229 cricket::GetFirstVideoContent(offer->description());
2230 const cricket::VideoContentDescription* video_desc =
2231 static_cast<const cricket::VideoContentDescription*>(
2232 video_content->description);
2233 ASSERT_EQ(cricket::MD_INACTIVE, video_desc->direction());
2234
2235 const cricket::ContentInfo* audio_content =
2236 cricket::GetFirstAudioContent(offer->description());
2237 const cricket::AudioContentDescription* audio_desc =
2238 static_cast<const cricket::AudioContentDescription*>(
2239 audio_content->description);
2240 ASSERT_EQ(cricket::MD_INACTIVE, audio_desc->direction());
2241}
2242
deadbeef653b8e02015-11-11 12:55:10 -08002243// Test that we can use SetConfiguration to change the ICE servers of the
2244// PortAllocator.
2245TEST_F(PeerConnectionInterfaceTest, SetConfigurationChangesIceServers) {
2246 CreatePeerConnection();
2247
2248 PeerConnectionInterface::RTCConfiguration config;
2249 PeerConnectionInterface::IceServer server;
2250 server.uri = "stun:test_hostname";
2251 config.servers.push_back(server);
2252 EXPECT_TRUE(pc_->SetConfiguration(config));
2253
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002254 EXPECT_EQ(1u, port_allocator_->stun_servers().size());
2255 EXPECT_EQ("test_hostname",
2256 port_allocator_->stun_servers().begin()->hostname());
deadbeef653b8e02015-11-11 12:55:10 -08002257}
2258
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002259TEST_F(PeerConnectionInterfaceTest, SetConfigurationChangesCandidateFilter) {
2260 CreatePeerConnection();
2261 PeerConnectionInterface::RTCConfiguration config;
2262 config.type = PeerConnectionInterface::kRelay;
2263 EXPECT_TRUE(pc_->SetConfiguration(config));
2264 EXPECT_EQ(cricket::CF_RELAY, port_allocator_->candidate_filter());
2265}
2266
deadbeef293e9262017-01-11 12:28:30 -08002267TEST_F(PeerConnectionInterfaceTest, SetConfigurationChangesPruneTurnPortsFlag) {
2268 PeerConnectionInterface::RTCConfiguration config;
2269 config.prune_turn_ports = false;
2270 CreatePeerConnection(config, nullptr);
2271 EXPECT_FALSE(port_allocator_->prune_turn_ports());
2272
2273 config.prune_turn_ports = true;
2274 EXPECT_TRUE(pc_->SetConfiguration(config));
2275 EXPECT_TRUE(port_allocator_->prune_turn_ports());
2276}
2277
skvladd1f5fda2017-02-03 16:54:05 -08002278// Test that the ice check interval can be changed. This does not verify that
2279// the setting makes it all the way to P2PTransportChannel, as that would
2280// require a very complex set of mocks.
2281TEST_F(PeerConnectionInterfaceTest, SetConfigurationChangesIceCheckInterval) {
2282 PeerConnectionInterface::RTCConfiguration config;
2283 config.ice_check_min_interval = rtc::Optional<int>();
2284 CreatePeerConnection(config, nullptr);
2285 config.ice_check_min_interval = rtc::Optional<int>(100);
2286 EXPECT_TRUE(pc_->SetConfiguration(config));
2287 PeerConnectionInterface::RTCConfiguration new_config =
2288 pc_->GetConfiguration();
2289 EXPECT_EQ(new_config.ice_check_min_interval, rtc::Optional<int>(100));
2290}
2291
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002292// Test that when SetConfiguration changes both the pool size and other
2293// attributes, the pooled session is created with the updated attributes.
2294TEST_F(PeerConnectionInterfaceTest,
2295 SetConfigurationCreatesPooledSessionCorrectly) {
2296 CreatePeerConnection();
2297 PeerConnectionInterface::RTCConfiguration config;
2298 config.ice_candidate_pool_size = 1;
2299 PeerConnectionInterface::IceServer server;
2300 server.uri = kStunAddressOnly;
2301 config.servers.push_back(server);
2302 config.type = PeerConnectionInterface::kRelay;
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07002303 EXPECT_TRUE(pc_->SetConfiguration(config));
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002304
2305 const cricket::FakePortAllocatorSession* session =
2306 static_cast<const cricket::FakePortAllocatorSession*>(
2307 port_allocator_->GetPooledSession());
2308 ASSERT_NE(nullptr, session);
2309 EXPECT_EQ(1UL, session->stun_servers().size());
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002310}
2311
deadbeef293e9262017-01-11 12:28:30 -08002312// Test that after SetLocalDescription, changing the pool size is not allowed,
2313// and an invalid modification error is returned.
deadbeef6de92f92016-12-12 18:49:32 -08002314TEST_F(PeerConnectionInterfaceTest,
2315 CantChangePoolSizeAfterSetLocalDescription) {
2316 CreatePeerConnection();
2317 // Start by setting a size of 1.
2318 PeerConnectionInterface::RTCConfiguration config;
2319 config.ice_candidate_pool_size = 1;
2320 EXPECT_TRUE(pc_->SetConfiguration(config));
2321
2322 // Set remote offer; can still change pool size at this point.
2323 CreateOfferAsRemoteDescription();
2324 config.ice_candidate_pool_size = 2;
2325 EXPECT_TRUE(pc_->SetConfiguration(config));
2326
2327 // Set local answer; now it's too late.
2328 CreateAnswerAsLocalDescription();
2329 config.ice_candidate_pool_size = 3;
deadbeef293e9262017-01-11 12:28:30 -08002330 RTCError error;
2331 EXPECT_FALSE(pc_->SetConfiguration(config, &error));
2332 EXPECT_EQ(RTCErrorType::INVALID_MODIFICATION, error.type());
2333}
2334
deadbeef42a42632017-03-10 15:18:00 -08002335// Test that after setting an answer, extra pooled sessions are discarded. The
2336// ICE candidate pool is only intended to be used for the first offer/answer.
2337TEST_F(PeerConnectionInterfaceTest,
2338 ExtraPooledSessionsDiscardedAfterApplyingAnswer) {
2339 CreatePeerConnection();
2340
2341 // Set a larger-than-necessary size.
2342 PeerConnectionInterface::RTCConfiguration config;
2343 config.ice_candidate_pool_size = 4;
2344 EXPECT_TRUE(pc_->SetConfiguration(config));
2345
2346 // Do offer/answer.
2347 CreateOfferAsRemoteDescription();
2348 CreateAnswerAsLocalDescription();
2349
2350 // Expect no pooled sessions to be left.
2351 const cricket::PortAllocatorSession* session =
2352 port_allocator_->GetPooledSession();
2353 EXPECT_EQ(nullptr, session);
2354}
2355
2356// After Close is called, pooled candidates should be discarded so as to not
2357// waste network resources.
2358TEST_F(PeerConnectionInterfaceTest, PooledSessionsDiscardedAfterClose) {
2359 CreatePeerConnection();
2360
2361 PeerConnectionInterface::RTCConfiguration config;
2362 config.ice_candidate_pool_size = 3;
2363 EXPECT_TRUE(pc_->SetConfiguration(config));
2364 pc_->Close();
2365
2366 // Expect no pooled sessions to be left.
2367 const cricket::PortAllocatorSession* session =
2368 port_allocator_->GetPooledSession();
2369 EXPECT_EQ(nullptr, session);
2370}
2371
deadbeef293e9262017-01-11 12:28:30 -08002372// Test that SetConfiguration returns an invalid modification error if
2373// modifying a field in the configuration that isn't allowed to be modified.
2374TEST_F(PeerConnectionInterfaceTest,
2375 SetConfigurationReturnsInvalidModificationError) {
2376 PeerConnectionInterface::RTCConfiguration config;
2377 config.bundle_policy = PeerConnectionInterface::kBundlePolicyBalanced;
2378 config.rtcp_mux_policy = PeerConnectionInterface::kRtcpMuxPolicyNegotiate;
2379 config.continual_gathering_policy = PeerConnectionInterface::GATHER_ONCE;
2380 CreatePeerConnection(config, nullptr);
2381
2382 PeerConnectionInterface::RTCConfiguration modified_config = config;
2383 modified_config.bundle_policy =
2384 PeerConnectionInterface::kBundlePolicyMaxBundle;
2385 RTCError error;
2386 EXPECT_FALSE(pc_->SetConfiguration(modified_config, &error));
2387 EXPECT_EQ(RTCErrorType::INVALID_MODIFICATION, error.type());
2388
2389 modified_config = config;
2390 modified_config.rtcp_mux_policy =
2391 PeerConnectionInterface::kRtcpMuxPolicyRequire;
2392 error.set_type(RTCErrorType::NONE);
2393 EXPECT_FALSE(pc_->SetConfiguration(modified_config, &error));
2394 EXPECT_EQ(RTCErrorType::INVALID_MODIFICATION, error.type());
2395
2396 modified_config = config;
2397 modified_config.continual_gathering_policy =
2398 PeerConnectionInterface::GATHER_CONTINUALLY;
2399 error.set_type(RTCErrorType::NONE);
2400 EXPECT_FALSE(pc_->SetConfiguration(modified_config, &error));
2401 EXPECT_EQ(RTCErrorType::INVALID_MODIFICATION, error.type());
2402}
2403
2404// Test that SetConfiguration returns a range error if the candidate pool size
2405// is negative or larger than allowed by the spec.
2406TEST_F(PeerConnectionInterfaceTest,
2407 SetConfigurationReturnsRangeErrorForBadCandidatePoolSize) {
2408 PeerConnectionInterface::RTCConfiguration config;
2409 CreatePeerConnection(config, nullptr);
2410
2411 config.ice_candidate_pool_size = -1;
2412 RTCError error;
2413 EXPECT_FALSE(pc_->SetConfiguration(config, &error));
2414 EXPECT_EQ(RTCErrorType::INVALID_RANGE, error.type());
2415
2416 config.ice_candidate_pool_size = INT_MAX;
2417 error.set_type(RTCErrorType::NONE);
2418 EXPECT_FALSE(pc_->SetConfiguration(config, &error));
2419 EXPECT_EQ(RTCErrorType::INVALID_RANGE, error.type());
2420}
2421
2422// Test that SetConfiguration returns a syntax error if parsing an ICE server
2423// URL failed.
2424TEST_F(PeerConnectionInterfaceTest,
2425 SetConfigurationReturnsSyntaxErrorFromBadIceUrls) {
2426 PeerConnectionInterface::RTCConfiguration config;
2427 CreatePeerConnection(config, nullptr);
2428
2429 PeerConnectionInterface::IceServer bad_server;
2430 bad_server.uri = "stunn:www.example.com";
2431 config.servers.push_back(bad_server);
2432 RTCError error;
2433 EXPECT_FALSE(pc_->SetConfiguration(config, &error));
2434 EXPECT_EQ(RTCErrorType::SYNTAX_ERROR, error.type());
2435}
2436
2437// Test that SetConfiguration returns an invalid parameter error if a TURN
2438// IceServer is missing a username or password.
2439TEST_F(PeerConnectionInterfaceTest,
2440 SetConfigurationReturnsInvalidParameterIfCredentialsMissing) {
2441 PeerConnectionInterface::RTCConfiguration config;
2442 CreatePeerConnection(config, nullptr);
2443
2444 PeerConnectionInterface::IceServer bad_server;
2445 bad_server.uri = "turn:www.example.com";
2446 // Missing password.
2447 bad_server.username = "foo";
2448 config.servers.push_back(bad_server);
2449 RTCError error;
2450 EXPECT_FALSE(pc_->SetConfiguration(config, &error));
2451 EXPECT_EQ(RTCErrorType::INVALID_PARAMETER, error.type());
deadbeef6de92f92016-12-12 18:49:32 -08002452}
2453
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002454// Test that PeerConnection::Close changes the states to closed and all remote
2455// tracks change state to ended.
2456TEST_F(PeerConnectionInterfaceTest, CloseAndTestStreamsAndStates) {
2457 // Initialize a PeerConnection and negotiate local and remote session
2458 // description.
2459 InitiateCall();
2460 ASSERT_EQ(1u, pc_->local_streams()->count());
2461 ASSERT_EQ(1u, pc_->remote_streams()->count());
2462
2463 pc_->Close();
2464
2465 EXPECT_EQ(PeerConnectionInterface::kClosed, pc_->signaling_state());
2466 EXPECT_EQ(PeerConnectionInterface::kIceConnectionClosed,
2467 pc_->ice_connection_state());
2468 EXPECT_EQ(PeerConnectionInterface::kIceGatheringComplete,
2469 pc_->ice_gathering_state());
2470
2471 EXPECT_EQ(1u, pc_->local_streams()->count());
2472 EXPECT_EQ(1u, pc_->remote_streams()->count());
2473
zhihuang9763d562016-08-05 11:14:50 -07002474 rtc::scoped_refptr<MediaStreamInterface> remote_stream =
2475 pc_->remote_streams()->at(0);
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002476 // Track state may be updated asynchronously.
perkjd61bf802016-03-24 03:16:19 -07002477 EXPECT_EQ_WAIT(MediaStreamTrackInterface::kEnded,
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002478 remote_stream->GetAudioTracks()[0]->state(), kTimeout);
2479 EXPECT_EQ_WAIT(MediaStreamTrackInterface::kEnded,
2480 remote_stream->GetVideoTracks()[0]->state(), kTimeout);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002481}
2482
2483// Test that PeerConnection methods fails gracefully after
2484// PeerConnection::Close has been called.
2485TEST_F(PeerConnectionInterfaceTest, CloseAndTestMethods) {
deadbeef293e9262017-01-11 12:28:30 -08002486 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002487 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
2488 CreateOfferAsRemoteDescription();
2489 CreateAnswerAsLocalDescription();
2490
2491 ASSERT_EQ(1u, pc_->local_streams()->count());
zhihuang9763d562016-08-05 11:14:50 -07002492 rtc::scoped_refptr<MediaStreamInterface> local_stream =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002493 pc_->local_streams()->at(0);
2494
2495 pc_->Close();
2496
2497 pc_->RemoveStream(local_stream);
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +00002498 EXPECT_FALSE(pc_->AddStream(local_stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002499
2500 ASSERT_FALSE(local_stream->GetAudioTracks().empty());
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002501 rtc::scoped_refptr<webrtc::DtmfSenderInterface> dtmf_sender(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002502 pc_->CreateDtmfSender(local_stream->GetAudioTracks()[0]));
wu@webrtc.org66037362013-08-13 00:09:35 +00002503 EXPECT_TRUE(NULL == dtmf_sender); // local stream has been removed.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002504
2505 EXPECT_TRUE(pc_->CreateDataChannel("test", NULL) == NULL);
2506
2507 EXPECT_TRUE(pc_->local_description() != NULL);
2508 EXPECT_TRUE(pc_->remote_description() != NULL);
2509
kwibergd1fe2812016-04-27 06:47:29 -07002510 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07002511 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
kwibergd1fe2812016-04-27 06:47:29 -07002512 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -07002513 EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002514
2515 std::string sdp;
2516 ASSERT_TRUE(pc_->remote_description()->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07002517 std::unique_ptr<SessionDescriptionInterface> remote_offer(
2518 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer, sdp,
2519 nullptr));
2520 EXPECT_FALSE(DoSetRemoteDescription(std::move(remote_offer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002521
2522 ASSERT_TRUE(pc_->local_description()->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07002523 std::unique_ptr<SessionDescriptionInterface> local_offer(
2524 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer, sdp,
2525 nullptr));
2526 EXPECT_FALSE(DoSetLocalDescription(std::move(local_offer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002527}
2528
2529// Test that GetStats can still be called after PeerConnection::Close.
2530TEST_F(PeerConnectionInterfaceTest, CloseAndGetStats) {
2531 InitiateCall();
2532 pc_->Close();
2533 DoGetStats(NULL);
2534}
deadbeefab9b2d12015-10-14 11:33:11 -07002535
2536// NOTE: The series of tests below come from what used to be
2537// mediastreamsignaling_unittest.cc, and are mostly aimed at testing that
2538// setting a remote or local description has the expected effects.
2539
2540// This test verifies that the remote MediaStreams corresponding to a received
2541// SDP string is created. In this test the two separate MediaStreams are
2542// signaled.
2543TEST_F(PeerConnectionInterfaceTest, UpdateRemoteStreams) {
2544 FakeConstraints constraints;
2545 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2546 true);
2547 CreatePeerConnection(&constraints);
2548 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2549
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002550 rtc::scoped_refptr<StreamCollection> reference(CreateStreamCollection(1, 1));
deadbeefab9b2d12015-10-14 11:33:11 -07002551 EXPECT_TRUE(
2552 CompareStreamCollections(observer_.remote_streams(), reference.get()));
2553 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2554 EXPECT_TRUE(remote_stream->GetVideoTracks()[0]->GetSource() != nullptr);
2555
2556 // Create a session description based on another SDP with another
2557 // MediaStream.
2558 CreateAndSetRemoteOffer(kSdpStringWithStream1And2);
2559
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002560 rtc::scoped_refptr<StreamCollection> reference2(CreateStreamCollection(2, 1));
deadbeefab9b2d12015-10-14 11:33:11 -07002561 EXPECT_TRUE(
2562 CompareStreamCollections(observer_.remote_streams(), reference2.get()));
2563}
2564
2565// This test verifies that when remote tracks are added/removed from SDP, the
2566// created remote streams are updated appropriately.
2567TEST_F(PeerConnectionInterfaceTest,
2568 AddRemoveTrackFromExistingRemoteMediaStream) {
2569 FakeConstraints constraints;
2570 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2571 true);
2572 CreatePeerConnection(&constraints);
kwibergd1fe2812016-04-27 06:47:29 -07002573 std::unique_ptr<SessionDescriptionInterface> desc_ms1 =
kwiberg2bbff992016-03-16 11:03:04 -07002574 CreateSessionDescriptionAndReference(1, 1);
Steve Antondb45ca82017-09-11 18:27:34 -07002575 EXPECT_TRUE(DoSetRemoteDescription(std::move(desc_ms1)));
deadbeefab9b2d12015-10-14 11:33:11 -07002576 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
2577 reference_collection_));
2578
2579 // Add extra audio and video tracks to the same MediaStream.
kwibergd1fe2812016-04-27 06:47:29 -07002580 std::unique_ptr<SessionDescriptionInterface> desc_ms1_two_tracks =
kwiberg2bbff992016-03-16 11:03:04 -07002581 CreateSessionDescriptionAndReference(2, 2);
Steve Antondb45ca82017-09-11 18:27:34 -07002582 EXPECT_TRUE(DoSetRemoteDescription(std::move(desc_ms1_two_tracks)));
deadbeefab9b2d12015-10-14 11:33:11 -07002583 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
2584 reference_collection_));
zhihuang9763d562016-08-05 11:14:50 -07002585 rtc::scoped_refptr<AudioTrackInterface> audio_track2 =
perkjd61bf802016-03-24 03:16:19 -07002586 observer_.remote_streams()->at(0)->GetAudioTracks()[1];
2587 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, audio_track2->state());
zhihuang9763d562016-08-05 11:14:50 -07002588 rtc::scoped_refptr<VideoTrackInterface> video_track2 =
perkjd61bf802016-03-24 03:16:19 -07002589 observer_.remote_streams()->at(0)->GetVideoTracks()[1];
2590 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, video_track2->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002591
2592 // Remove the extra audio and video tracks.
kwibergd1fe2812016-04-27 06:47:29 -07002593 std::unique_ptr<SessionDescriptionInterface> desc_ms2 =
kwiberg2bbff992016-03-16 11:03:04 -07002594 CreateSessionDescriptionAndReference(1, 1);
perkjd61bf802016-03-24 03:16:19 -07002595 MockTrackObserver audio_track_observer(audio_track2);
2596 MockTrackObserver video_track_observer(video_track2);
2597
2598 EXPECT_CALL(audio_track_observer, OnChanged()).Times(Exactly(1));
2599 EXPECT_CALL(video_track_observer, OnChanged()).Times(Exactly(1));
Steve Antondb45ca82017-09-11 18:27:34 -07002600 EXPECT_TRUE(DoSetRemoteDescription(std::move(desc_ms2)));
deadbeefab9b2d12015-10-14 11:33:11 -07002601 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
2602 reference_collection_));
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002603 // Track state may be updated asynchronously.
perkjd61bf802016-03-24 03:16:19 -07002604 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002605 audio_track2->state(), kTimeout);
2606 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
2607 video_track2->state(), kTimeout);
deadbeefab9b2d12015-10-14 11:33:11 -07002608}
2609
2610// This tests that remote tracks are ended if a local session description is set
2611// that rejects the media content type.
2612TEST_F(PeerConnectionInterfaceTest, RejectMediaContent) {
2613 FakeConstraints constraints;
2614 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2615 true);
2616 CreatePeerConnection(&constraints);
2617 // First create and set a remote offer, then reject its video content in our
2618 // answer.
2619 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2620 ASSERT_EQ(1u, observer_.remote_streams()->count());
2621 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2622 ASSERT_EQ(1u, remote_stream->GetVideoTracks().size());
2623 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2624
2625 rtc::scoped_refptr<webrtc::VideoTrackInterface> remote_video =
2626 remote_stream->GetVideoTracks()[0];
2627 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, remote_video->state());
2628 rtc::scoped_refptr<webrtc::AudioTrackInterface> remote_audio =
2629 remote_stream->GetAudioTracks()[0];
2630 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, remote_audio->state());
2631
kwibergd1fe2812016-04-27 06:47:29 -07002632 std::unique_ptr<SessionDescriptionInterface> local_answer;
kwiberg2bbff992016-03-16 11:03:04 -07002633 EXPECT_TRUE(DoCreateAnswer(&local_answer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07002634 cricket::ContentInfo* video_info =
2635 local_answer->description()->GetContentByName("video");
2636 video_info->rejected = true;
Steve Antondb45ca82017-09-11 18:27:34 -07002637 EXPECT_TRUE(DoSetLocalDescription(std::move(local_answer)));
deadbeefab9b2d12015-10-14 11:33:11 -07002638 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kEnded, remote_video->state());
2639 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, remote_audio->state());
2640
2641 // Now create an offer where we reject both video and audio.
kwibergd1fe2812016-04-27 06:47:29 -07002642 std::unique_ptr<SessionDescriptionInterface> local_offer;
kwiberg2bbff992016-03-16 11:03:04 -07002643 EXPECT_TRUE(DoCreateOffer(&local_offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07002644 video_info = local_offer->description()->GetContentByName("video");
2645 ASSERT_TRUE(video_info != nullptr);
2646 video_info->rejected = true;
2647 cricket::ContentInfo* audio_info =
2648 local_offer->description()->GetContentByName("audio");
2649 ASSERT_TRUE(audio_info != nullptr);
2650 audio_info->rejected = true;
Steve Antondb45ca82017-09-11 18:27:34 -07002651 EXPECT_TRUE(DoSetLocalDescription(std::move(local_offer)));
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002652 // Track state may be updated asynchronously.
perkjd61bf802016-03-24 03:16:19 -07002653 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002654 remote_audio->state(), kTimeout);
2655 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
2656 remote_video->state(), kTimeout);
deadbeefab9b2d12015-10-14 11:33:11 -07002657}
2658
2659// This tests that we won't crash if the remote track has been removed outside
2660// of PeerConnection and then PeerConnection tries to reject the track.
2661TEST_F(PeerConnectionInterfaceTest, RemoveTrackThenRejectMediaContent) {
2662 FakeConstraints constraints;
2663 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2664 true);
2665 CreatePeerConnection(&constraints);
2666 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2667 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2668 remote_stream->RemoveTrack(remote_stream->GetVideoTracks()[0]);
2669 remote_stream->RemoveTrack(remote_stream->GetAudioTracks()[0]);
2670
kwibergd1fe2812016-04-27 06:47:29 -07002671 std::unique_ptr<SessionDescriptionInterface> local_answer(
deadbeefab9b2d12015-10-14 11:33:11 -07002672 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
2673 kSdpStringWithStream1, nullptr));
2674 cricket::ContentInfo* video_info =
2675 local_answer->description()->GetContentByName("video");
2676 video_info->rejected = true;
2677 cricket::ContentInfo* audio_info =
2678 local_answer->description()->GetContentByName("audio");
2679 audio_info->rejected = true;
Steve Antondb45ca82017-09-11 18:27:34 -07002680 EXPECT_TRUE(DoSetLocalDescription(std::move(local_answer)));
deadbeefab9b2d12015-10-14 11:33:11 -07002681
2682 // No crash is a pass.
2683}
2684
deadbeef5e97fb52015-10-15 12:49:08 -07002685// This tests that if a recvonly remote description is set, no remote streams
2686// will be created, even if the description contains SSRCs/MSIDs.
2687// See: https://code.google.com/p/webrtc/issues/detail?id=5054
2688TEST_F(PeerConnectionInterfaceTest, RecvonlyDescriptionDoesntCreateStream) {
2689 FakeConstraints constraints;
2690 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2691 true);
2692 CreatePeerConnection(&constraints);
2693
2694 std::string recvonly_offer = kSdpStringWithStream1;
2695 rtc::replace_substrs(kSendrecv, strlen(kSendrecv), kRecvonly,
2696 strlen(kRecvonly), &recvonly_offer);
2697 CreateAndSetRemoteOffer(recvonly_offer);
2698
2699 EXPECT_EQ(0u, observer_.remote_streams()->count());
2700}
2701
deadbeefab9b2d12015-10-14 11:33:11 -07002702// This tests that a default MediaStream is created if a remote session
2703// description doesn't contain any streams and no MSID support.
2704// It also tests that the default stream is updated if a video m-line is added
2705// in a subsequent session description.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002706TEST_F(PeerConnectionInterfaceTest, SdpWithoutMsidCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002707 FakeConstraints constraints;
2708 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2709 true);
2710 CreatePeerConnection(&constraints);
2711 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2712
2713 ASSERT_EQ(1u, observer_.remote_streams()->count());
2714 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2715
2716 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2717 EXPECT_EQ(0u, remote_stream->GetVideoTracks().size());
2718 EXPECT_EQ("default", remote_stream->label());
2719
2720 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2721 ASSERT_EQ(1u, observer_.remote_streams()->count());
2722 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2723 EXPECT_EQ("defaulta0", remote_stream->GetAudioTracks()[0]->id());
deadbeef884f5852016-01-15 09:20:04 -08002724 EXPECT_EQ(MediaStreamTrackInterface::kLive,
2725 remote_stream->GetAudioTracks()[0]->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002726 ASSERT_EQ(1u, remote_stream->GetVideoTracks().size());
2727 EXPECT_EQ("defaultv0", remote_stream->GetVideoTracks()[0]->id());
deadbeef884f5852016-01-15 09:20:04 -08002728 EXPECT_EQ(MediaStreamTrackInterface::kLive,
2729 remote_stream->GetVideoTracks()[0]->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002730}
2731
2732// This tests that a default MediaStream is created if a remote session
2733// description doesn't contain any streams and media direction is send only.
2734TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002735 SendOnlySdpWithoutMsidCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002736 FakeConstraints constraints;
2737 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2738 true);
2739 CreatePeerConnection(&constraints);
2740 CreateAndSetRemoteOffer(kSdpStringSendOnlyWithoutStreams);
2741
2742 ASSERT_EQ(1u, observer_.remote_streams()->count());
2743 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2744
2745 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2746 EXPECT_EQ(1u, remote_stream->GetVideoTracks().size());
2747 EXPECT_EQ("default", remote_stream->label());
2748}
2749
2750// This tests that it won't crash when PeerConnection tries to remove
2751// a remote track that as already been removed from the MediaStream.
2752TEST_F(PeerConnectionInterfaceTest, RemoveAlreadyGoneRemoteStream) {
2753 FakeConstraints constraints;
2754 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2755 true);
2756 CreatePeerConnection(&constraints);
2757 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2758 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2759 remote_stream->RemoveTrack(remote_stream->GetAudioTracks()[0]);
2760 remote_stream->RemoveTrack(remote_stream->GetVideoTracks()[0]);
2761
2762 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2763
2764 // No crash is a pass.
2765}
2766
2767// This tests that a default MediaStream is created if the remote session
2768// description doesn't contain any streams and don't contain an indication if
2769// MSID is supported.
2770TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002771 SdpWithoutMsidAndStreamsCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002772 FakeConstraints constraints;
2773 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2774 true);
2775 CreatePeerConnection(&constraints);
2776 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2777
2778 ASSERT_EQ(1u, observer_.remote_streams()->count());
2779 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2780 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2781 EXPECT_EQ(1u, remote_stream->GetVideoTracks().size());
2782}
2783
2784// This tests that a default MediaStream is not created if the remote session
2785// description doesn't contain any streams but does support MSID.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002786TEST_F(PeerConnectionInterfaceTest, SdpWithMsidDontCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002787 FakeConstraints constraints;
2788 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2789 true);
2790 CreatePeerConnection(&constraints);
2791 CreateAndSetRemoteOffer(kSdpStringWithMsidWithoutStreams);
2792 EXPECT_EQ(0u, observer_.remote_streams()->count());
2793}
2794
deadbeefbda7e0b2015-12-08 17:13:40 -08002795// This tests that when setting a new description, the old default tracks are
2796// not destroyed and recreated.
2797// See: https://bugs.chromium.org/p/webrtc/issues/detail?id=5250
Stefan Holmer102362b2016-03-18 09:39:07 +01002798TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002799 DefaultTracksNotDestroyedAndRecreated) {
deadbeefbda7e0b2015-12-08 17:13:40 -08002800 FakeConstraints constraints;
2801 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2802 true);
2803 CreatePeerConnection(&constraints);
2804 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2805
2806 ASSERT_EQ(1u, observer_.remote_streams()->count());
2807 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2808 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2809
2810 // Set the track to "disabled", then set a new description and ensure the
2811 // track is still disabled, which ensures it hasn't been recreated.
2812 remote_stream->GetAudioTracks()[0]->set_enabled(false);
2813 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2814 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2815 EXPECT_FALSE(remote_stream->GetAudioTracks()[0]->enabled());
2816}
2817
deadbeefab9b2d12015-10-14 11:33:11 -07002818// This tests that a default MediaStream is not created if a remote session
2819// description is updated to not have any MediaStreams.
2820TEST_F(PeerConnectionInterfaceTest, VerifyDefaultStreamIsNotCreated) {
2821 FakeConstraints constraints;
2822 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2823 true);
2824 CreatePeerConnection(&constraints);
2825 CreateAndSetRemoteOffer(kSdpStringWithStream1);
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002826 rtc::scoped_refptr<StreamCollection> reference(CreateStreamCollection(1, 1));
deadbeefab9b2d12015-10-14 11:33:11 -07002827 EXPECT_TRUE(
2828 CompareStreamCollections(observer_.remote_streams(), reference.get()));
2829
2830 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2831 EXPECT_EQ(0u, observer_.remote_streams()->count());
2832}
2833
2834// This tests that an RtpSender is created when the local description is set
2835// after adding a local stream.
2836// TODO(deadbeef): This test and the one below it need to be updated when
2837// an RtpSender's lifetime isn't determined by when a local description is set.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002838TEST_F(PeerConnectionInterfaceTest, LocalDescriptionChanged) {
deadbeefab9b2d12015-10-14 11:33:11 -07002839 FakeConstraints constraints;
2840 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2841 true);
2842 CreatePeerConnection(&constraints);
deadbeefab9b2d12015-10-14 11:33:11 -07002843
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002844 // Create an offer with 1 stream with 2 tracks of each type.
2845 rtc::scoped_refptr<StreamCollection> stream_collection =
2846 CreateStreamCollection(1, 2);
2847 pc_->AddStream(stream_collection->at(0));
2848 std::unique_ptr<SessionDescriptionInterface> offer;
2849 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07002850 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07002851
deadbeefab9b2d12015-10-14 11:33:11 -07002852 auto senders = pc_->GetSenders();
2853 EXPECT_EQ(4u, senders.size());
2854 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2855 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2856 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[1]));
2857 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[1]));
2858
2859 // Remove an audio and video track.
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002860 pc_->RemoveStream(stream_collection->at(0));
2861 stream_collection = CreateStreamCollection(1, 1);
2862 pc_->AddStream(stream_collection->at(0));
2863 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07002864 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002865
deadbeefab9b2d12015-10-14 11:33:11 -07002866 senders = pc_->GetSenders();
2867 EXPECT_EQ(2u, senders.size());
2868 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2869 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2870 EXPECT_FALSE(ContainsSender(senders, kAudioTracks[1]));
2871 EXPECT_FALSE(ContainsSender(senders, kVideoTracks[1]));
2872}
2873
2874// This tests that an RtpSender is created when the local description is set
2875// before adding a local stream.
2876TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002877 AddLocalStreamAfterLocalDescriptionChanged) {
deadbeefab9b2d12015-10-14 11:33:11 -07002878 FakeConstraints constraints;
2879 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2880 true);
2881 CreatePeerConnection(&constraints);
deadbeefab9b2d12015-10-14 11:33:11 -07002882
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002883 rtc::scoped_refptr<StreamCollection> stream_collection =
2884 CreateStreamCollection(1, 2);
2885 // Add a stream to create the offer, but remove it afterwards.
2886 pc_->AddStream(stream_collection->at(0));
2887 std::unique_ptr<SessionDescriptionInterface> offer;
2888 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2889 pc_->RemoveStream(stream_collection->at(0));
deadbeefab9b2d12015-10-14 11:33:11 -07002890
Steve Antondb45ca82017-09-11 18:27:34 -07002891 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07002892 auto senders = pc_->GetSenders();
2893 EXPECT_EQ(0u, senders.size());
2894
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002895 pc_->AddStream(stream_collection->at(0));
deadbeefab9b2d12015-10-14 11:33:11 -07002896 senders = pc_->GetSenders();
2897 EXPECT_EQ(4u, senders.size());
2898 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2899 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2900 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[1]));
2901 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[1]));
2902}
2903
2904// This tests that the expected behavior occurs if the SSRC on a local track is
2905// changed when SetLocalDescription is called.
2906TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002907 ChangeSsrcOnTrackInLocalSessionDescription) {
deadbeefab9b2d12015-10-14 11:33:11 -07002908 FakeConstraints constraints;
2909 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2910 true);
2911 CreatePeerConnection(&constraints);
deadbeefab9b2d12015-10-14 11:33:11 -07002912
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002913 rtc::scoped_refptr<StreamCollection> stream_collection =
2914 CreateStreamCollection(2, 1);
2915 pc_->AddStream(stream_collection->at(0));
2916 std::unique_ptr<SessionDescriptionInterface> offer;
2917 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2918 // Grab a copy of the offer before it gets passed into the PC.
Steve Antondb45ca82017-09-11 18:27:34 -07002919 std::unique_ptr<webrtc::JsepSessionDescription> modified_offer(
2920 new webrtc::JsepSessionDescription(SessionDescriptionInterface::kOffer));
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002921 modified_offer->Initialize(offer->description()->Copy(), offer->session_id(),
2922 offer->session_version());
Steve Antondb45ca82017-09-11 18:27:34 -07002923 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07002924
deadbeefab9b2d12015-10-14 11:33:11 -07002925 auto senders = pc_->GetSenders();
2926 EXPECT_EQ(2u, senders.size());
2927 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2928 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2929
2930 // Change the ssrc of the audio and video track.
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002931 cricket::MediaContentDescription* desc =
2932 cricket::GetFirstAudioContentDescription(modified_offer->description());
2933 ASSERT_TRUE(desc != NULL);
2934 for (StreamParams& stream : desc->mutable_streams()) {
2935 for (unsigned int& ssrc : stream.ssrcs) {
2936 ++ssrc;
2937 }
2938 }
deadbeefab9b2d12015-10-14 11:33:11 -07002939
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002940 desc =
2941 cricket::GetFirstVideoContentDescription(modified_offer->description());
2942 ASSERT_TRUE(desc != NULL);
2943 for (StreamParams& stream : desc->mutable_streams()) {
2944 for (unsigned int& ssrc : stream.ssrcs) {
2945 ++ssrc;
2946 }
2947 }
2948
Steve Antondb45ca82017-09-11 18:27:34 -07002949 EXPECT_TRUE(DoSetLocalDescription(std::move(modified_offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07002950 senders = pc_->GetSenders();
2951 EXPECT_EQ(2u, senders.size());
2952 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2953 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2954 // TODO(deadbeef): Once RtpSenders expose parameters, check that the SSRC
2955 // changed.
2956}
2957
2958// This tests that the expected behavior occurs if a new session description is
2959// set with the same tracks, but on a different MediaStream.
Stefan Holmer55d6e7c2016-03-17 16:26:40 +01002960TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002961 SignalSameTracksInSeparateMediaStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002962 FakeConstraints constraints;
2963 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2964 true);
2965 CreatePeerConnection(&constraints);
deadbeefab9b2d12015-10-14 11:33:11 -07002966
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002967 rtc::scoped_refptr<StreamCollection> stream_collection =
2968 CreateStreamCollection(2, 1);
2969 pc_->AddStream(stream_collection->at(0));
2970 std::unique_ptr<SessionDescriptionInterface> offer;
2971 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07002972 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07002973
deadbeefab9b2d12015-10-14 11:33:11 -07002974 auto senders = pc_->GetSenders();
2975 EXPECT_EQ(2u, senders.size());
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002976 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0], kStreams[0]));
2977 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0], kStreams[0]));
deadbeefab9b2d12015-10-14 11:33:11 -07002978
2979 // Add a new MediaStream but with the same tracks as in the first stream.
2980 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream_1(
2981 webrtc::MediaStream::Create(kStreams[1]));
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002982 stream_1->AddTrack(stream_collection->at(0)->GetVideoTracks()[0]);
2983 stream_1->AddTrack(stream_collection->at(0)->GetAudioTracks()[0]);
deadbeefab9b2d12015-10-14 11:33:11 -07002984 pc_->AddStream(stream_1);
2985
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002986 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07002987 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07002988
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002989 auto new_senders = pc_->GetSenders();
2990 // Should be the same senders as before, but with updated stream id.
2991 // Note that this behavior is subject to change in the future.
2992 // We may decide the PC should ignore existing tracks in AddStream.
2993 EXPECT_EQ(senders, new_senders);
2994 EXPECT_TRUE(ContainsSender(new_senders, kAudioTracks[0], kStreams[1]));
2995 EXPECT_TRUE(ContainsSender(new_senders, kVideoTracks[0], kStreams[1]));
deadbeefab9b2d12015-10-14 11:33:11 -07002996}
2997
zhihuang81c3a032016-11-17 12:06:24 -08002998// This tests that PeerConnectionObserver::OnAddTrack is correctly called.
2999TEST_F(PeerConnectionInterfaceTest, OnAddTrackCallback) {
3000 FakeConstraints constraints;
3001 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
3002 true);
3003 CreatePeerConnection(&constraints);
3004 CreateAndSetRemoteOffer(kSdpStringWithStream1AudioTrackOnly);
3005 EXPECT_EQ(observer_.num_added_tracks_, 1);
3006 EXPECT_EQ(observer_.last_added_track_label_, kAudioTracks[0]);
3007
3008 // Create and set the updated remote SDP.
3009 CreateAndSetRemoteOffer(kSdpStringWithStream1);
3010 EXPECT_EQ(observer_.num_added_tracks_, 2);
3011 EXPECT_EQ(observer_.last_added_track_label_, kVideoTracks[0]);
3012}
3013
deadbeefd1a38b52016-12-10 13:15:33 -08003014// Test that when SetConfiguration is called and the configuration is
3015// changing, the next offer causes an ICE restart.
3016TEST_F(PeerConnectionInterfaceTest, SetConfigurationCausingIceRetart) {
3017 PeerConnectionInterface::RTCConfiguration config;
3018 config.type = PeerConnectionInterface::kRelay;
3019 // Need to pass default constraints to prevent disabling of DTLS...
3020 FakeConstraints default_constraints;
3021 CreatePeerConnection(config, &default_constraints);
3022 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
3023
3024 // Do initial offer/answer so there's something to restart.
3025 CreateOfferAsLocalDescription();
3026 CreateAnswerAsRemoteDescription(kSdpStringWithStream1);
3027
3028 // Grab the ufrags.
3029 std::vector<std::string> initial_ufrags = GetUfrags(pc_->local_description());
3030
3031 // Change ICE policy, which should trigger an ICE restart on the next offer.
3032 config.type = PeerConnectionInterface::kAll;
3033 EXPECT_TRUE(pc_->SetConfiguration(config));
3034 CreateOfferAsLocalDescription();
3035
3036 // Grab the new ufrags.
3037 std::vector<std::string> subsequent_ufrags =
3038 GetUfrags(pc_->local_description());
3039
3040 // Sanity check.
3041 EXPECT_EQ(initial_ufrags.size(), subsequent_ufrags.size());
3042 // Check that each ufrag is different.
3043 for (int i = 0; i < static_cast<int>(initial_ufrags.size()); ++i) {
3044 EXPECT_NE(initial_ufrags[i], subsequent_ufrags[i]);
3045 }
3046}
3047
3048// Test that when SetConfiguration is called and the configuration *isn't*
3049// changing, the next offer does *not* cause an ICE restart.
3050TEST_F(PeerConnectionInterfaceTest, SetConfigurationNotCausingIceRetart) {
3051 PeerConnectionInterface::RTCConfiguration config;
3052 config.type = PeerConnectionInterface::kRelay;
3053 // Need to pass default constraints to prevent disabling of DTLS...
3054 FakeConstraints default_constraints;
3055 CreatePeerConnection(config, &default_constraints);
3056 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
3057
3058 // Do initial offer/answer so there's something to restart.
3059 CreateOfferAsLocalDescription();
3060 CreateAnswerAsRemoteDescription(kSdpStringWithStream1);
3061
3062 // Grab the ufrags.
3063 std::vector<std::string> initial_ufrags = GetUfrags(pc_->local_description());
3064
3065 // Call SetConfiguration with a config identical to what the PC was
3066 // constructed with.
3067 EXPECT_TRUE(pc_->SetConfiguration(config));
3068 CreateOfferAsLocalDescription();
3069
3070 // Grab the new ufrags.
3071 std::vector<std::string> subsequent_ufrags =
3072 GetUfrags(pc_->local_description());
3073
3074 EXPECT_EQ(initial_ufrags, subsequent_ufrags);
3075}
3076
3077// Test for a weird corner case scenario:
3078// 1. Audio/video session established.
3079// 2. SetConfiguration changes ICE config; ICE restart needed.
3080// 3. ICE restart initiated by remote peer, but only for one m= section.
3081// 4. Next createOffer should initiate an ICE restart, but only for the other
3082// m= section; it would be pointless to do an ICE restart for the m= section
3083// that was already restarted.
3084TEST_F(PeerConnectionInterfaceTest, SetConfigurationCausingPartialIceRestart) {
3085 PeerConnectionInterface::RTCConfiguration config;
3086 config.type = PeerConnectionInterface::kRelay;
3087 // Need to pass default constraints to prevent disabling of DTLS...
3088 FakeConstraints default_constraints;
3089 CreatePeerConnection(config, &default_constraints);
3090 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
3091
3092 // Do initial offer/answer so there's something to restart.
3093 CreateOfferAsLocalDescription();
3094 CreateAnswerAsRemoteDescription(kSdpStringWithStream1);
3095
3096 // Change ICE policy, which should set the "needs-ice-restart" flag.
3097 config.type = PeerConnectionInterface::kAll;
3098 EXPECT_TRUE(pc_->SetConfiguration(config));
3099
3100 // Do ICE restart for the first m= section, initiated by remote peer.
Steve Antondb45ca82017-09-11 18:27:34 -07003101 std::unique_ptr<webrtc::SessionDescriptionInterface> remote_offer(
3102 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
3103 kSdpStringWithStream1, nullptr));
3104 ASSERT_TRUE(remote_offer);
deadbeefd1a38b52016-12-10 13:15:33 -08003105 remote_offer->description()->transport_infos()[0].description.ice_ufrag =
3106 "modified";
Steve Antondb45ca82017-09-11 18:27:34 -07003107 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_offer)));
deadbeefd1a38b52016-12-10 13:15:33 -08003108 CreateAnswerAsLocalDescription();
3109
3110 // Grab the ufrags.
3111 std::vector<std::string> initial_ufrags = GetUfrags(pc_->local_description());
3112 ASSERT_EQ(2, initial_ufrags.size());
3113
3114 // Create offer and grab the new ufrags.
3115 CreateOfferAsLocalDescription();
3116 std::vector<std::string> subsequent_ufrags =
3117 GetUfrags(pc_->local_description());
3118 ASSERT_EQ(2, subsequent_ufrags.size());
3119
3120 // Ensure that only the ufrag for the second m= section changed.
3121 EXPECT_EQ(initial_ufrags[0], subsequent_ufrags[0]);
3122 EXPECT_NE(initial_ufrags[1], subsequent_ufrags[1]);
3123}
3124
deadbeeffe4a8a42016-12-20 17:56:17 -08003125// Tests that the methods to return current/pending descriptions work as
3126// expected at different points in the offer/answer exchange. This test does
3127// one offer/answer exchange as the offerer, then another as the answerer.
3128TEST_F(PeerConnectionInterfaceTest, CurrentAndPendingDescriptions) {
3129 // This disables DTLS so we can apply an answer to ourselves.
3130 CreatePeerConnection();
3131
3132 // Create initial local offer and get SDP (which will also be used as
3133 // answer/pranswer);
Steve Antondb45ca82017-09-11 18:27:34 -07003134 std::unique_ptr<SessionDescriptionInterface> local_offer;
3135 ASSERT_TRUE(DoCreateOffer(&local_offer, nullptr));
deadbeeffe4a8a42016-12-20 17:56:17 -08003136 std::string sdp;
Steve Antondb45ca82017-09-11 18:27:34 -07003137 EXPECT_TRUE(local_offer->ToString(&sdp));
deadbeeffe4a8a42016-12-20 17:56:17 -08003138
3139 // Set local offer.
Steve Antondb45ca82017-09-11 18:27:34 -07003140 SessionDescriptionInterface* local_offer_ptr = local_offer.get();
3141 EXPECT_TRUE(DoSetLocalDescription(std::move(local_offer)));
3142 EXPECT_EQ(local_offer_ptr, pc_->pending_local_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003143 EXPECT_EQ(nullptr, pc_->pending_remote_description());
3144 EXPECT_EQ(nullptr, pc_->current_local_description());
3145 EXPECT_EQ(nullptr, pc_->current_remote_description());
3146
3147 // Set remote pranswer.
Steve Antondb45ca82017-09-11 18:27:34 -07003148 std::unique_ptr<SessionDescriptionInterface> remote_pranswer(
deadbeeffe4a8a42016-12-20 17:56:17 -08003149 webrtc::CreateSessionDescription(SessionDescriptionInterface::kPrAnswer,
Steve Antondb45ca82017-09-11 18:27:34 -07003150 sdp, nullptr));
3151 SessionDescriptionInterface* remote_pranswer_ptr = remote_pranswer.get();
3152 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_pranswer)));
3153 EXPECT_EQ(local_offer_ptr, pc_->pending_local_description());
3154 EXPECT_EQ(remote_pranswer_ptr, pc_->pending_remote_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003155 EXPECT_EQ(nullptr, pc_->current_local_description());
3156 EXPECT_EQ(nullptr, pc_->current_remote_description());
3157
3158 // Set remote answer.
Steve Antondb45ca82017-09-11 18:27:34 -07003159 std::unique_ptr<SessionDescriptionInterface> remote_answer(
3160 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
3161 sdp, nullptr));
3162 SessionDescriptionInterface* remote_answer_ptr = remote_answer.get();
3163 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_answer)));
deadbeeffe4a8a42016-12-20 17:56:17 -08003164 EXPECT_EQ(nullptr, pc_->pending_local_description());
3165 EXPECT_EQ(nullptr, pc_->pending_remote_description());
Steve Antondb45ca82017-09-11 18:27:34 -07003166 EXPECT_EQ(local_offer_ptr, pc_->current_local_description());
3167 EXPECT_EQ(remote_answer_ptr, pc_->current_remote_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003168
3169 // Set remote offer.
Steve Antondb45ca82017-09-11 18:27:34 -07003170 std::unique_ptr<SessionDescriptionInterface> remote_offer(
3171 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer, sdp,
3172 nullptr));
3173 SessionDescriptionInterface* remote_offer_ptr = remote_offer.get();
3174 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_offer)));
3175 EXPECT_EQ(remote_offer_ptr, pc_->pending_remote_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003176 EXPECT_EQ(nullptr, pc_->pending_local_description());
Steve Antondb45ca82017-09-11 18:27:34 -07003177 EXPECT_EQ(local_offer_ptr, pc_->current_local_description());
3178 EXPECT_EQ(remote_answer_ptr, pc_->current_remote_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003179
3180 // Set local pranswer.
Steve Antondb45ca82017-09-11 18:27:34 -07003181 std::unique_ptr<SessionDescriptionInterface> local_pranswer(
deadbeeffe4a8a42016-12-20 17:56:17 -08003182 webrtc::CreateSessionDescription(SessionDescriptionInterface::kPrAnswer,
Steve Antondb45ca82017-09-11 18:27:34 -07003183 sdp, nullptr));
3184 SessionDescriptionInterface* local_pranswer_ptr = local_pranswer.get();
3185 EXPECT_TRUE(DoSetLocalDescription(std::move(local_pranswer)));
3186 EXPECT_EQ(remote_offer_ptr, pc_->pending_remote_description());
3187 EXPECT_EQ(local_pranswer_ptr, pc_->pending_local_description());
3188 EXPECT_EQ(local_offer_ptr, pc_->current_local_description());
3189 EXPECT_EQ(remote_answer_ptr, pc_->current_remote_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003190
3191 // Set local answer.
Steve Antondb45ca82017-09-11 18:27:34 -07003192 std::unique_ptr<SessionDescriptionInterface> local_answer(
3193 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
3194 sdp, nullptr));
3195 SessionDescriptionInterface* local_answer_ptr = local_answer.get();
3196 EXPECT_TRUE(DoSetLocalDescription(std::move(local_answer)));
deadbeeffe4a8a42016-12-20 17:56:17 -08003197 EXPECT_EQ(nullptr, pc_->pending_remote_description());
3198 EXPECT_EQ(nullptr, pc_->pending_local_description());
Steve Antondb45ca82017-09-11 18:27:34 -07003199 EXPECT_EQ(remote_offer_ptr, pc_->current_remote_description());
3200 EXPECT_EQ(local_answer_ptr, pc_->current_local_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003201}
3202
zhihuang77985012017-02-07 15:45:16 -08003203// Tests that it won't crash when calling StartRtcEventLog or StopRtcEventLog
3204// after the PeerConnection is closed.
3205TEST_F(PeerConnectionInterfaceTest,
3206 StartAndStopLoggingAfterPeerConnectionClosed) {
3207 CreatePeerConnection();
3208 // The RtcEventLog will be reset when the PeerConnection is closed.
3209 pc_->Close();
3210
Elad Alon9e6565b2017-10-11 16:04:13 +02003211 auto test_info = ::testing::UnitTest::GetInstance()->current_test_info();
3212 std::string filename = webrtc::test::OutputPath() +
3213 test_info->test_case_name() + test_info->name();
3214 rtc::PlatformFile file = rtc::CreatePlatformFile(filename);
3215
3216 constexpr int64_t max_size_bytes = 1024;
3217
zhihuang77985012017-02-07 15:45:16 -08003218 EXPECT_FALSE(pc_->StartRtcEventLog(file, max_size_bytes));
3219 pc_->StopRtcEventLog();
Elad Alon9e6565b2017-10-11 16:04:13 +02003220
3221 // Cleanup.
3222 rtc::ClosePlatformFile(file);
3223 rtc::RemoveFile(filename);
zhihuang77985012017-02-07 15:45:16 -08003224}
3225
deadbeef30952b42017-04-21 02:41:29 -07003226// Test that generated offers/answers include "ice-option:trickle".
3227TEST_F(PeerConnectionInterfaceTest, OffersAndAnswersHaveTrickleIceOption) {
3228 CreatePeerConnection();
3229
3230 // First, create an offer with audio/video.
3231 FakeConstraints constraints;
3232 constraints.SetMandatoryReceiveAudio(true);
3233 constraints.SetMandatoryReceiveVideo(true);
3234 std::unique_ptr<SessionDescriptionInterface> offer;
3235 ASSERT_TRUE(DoCreateOffer(&offer, &constraints));
3236 cricket::SessionDescription* desc = offer->description();
3237 ASSERT_EQ(2u, desc->transport_infos().size());
3238 EXPECT_TRUE(desc->transport_infos()[0].description.HasOption("trickle"));
3239 EXPECT_TRUE(desc->transport_infos()[1].description.HasOption("trickle"));
3240
3241 // Apply the offer as a remote description, then create an answer.
Steve Antondb45ca82017-09-11 18:27:34 -07003242 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
deadbeef30952b42017-04-21 02:41:29 -07003243 std::unique_ptr<SessionDescriptionInterface> answer;
3244 ASSERT_TRUE(DoCreateAnswer(&answer, &constraints));
3245 desc = answer->description();
3246 ASSERT_EQ(2u, desc->transport_infos().size());
3247 EXPECT_TRUE(desc->transport_infos()[0].description.HasOption("trickle"));
3248 EXPECT_TRUE(desc->transport_infos()[1].description.HasOption("trickle"));
3249}
3250
deadbeef1dcb1642017-03-29 21:08:16 -07003251// Test that ICE renomination isn't offered if it's not enabled in the PC's
3252// RTCConfiguration.
3253TEST_F(PeerConnectionInterfaceTest, IceRenominationNotOffered) {
3254 PeerConnectionInterface::RTCConfiguration config;
3255 config.enable_ice_renomination = false;
3256 CreatePeerConnection(config, nullptr);
3257 AddVoiceStream("foo");
3258
3259 std::unique_ptr<SessionDescriptionInterface> offer;
3260 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
3261 cricket::SessionDescription* desc = offer->description();
3262 EXPECT_EQ(1u, desc->transport_infos().size());
3263 EXPECT_FALSE(
3264 desc->transport_infos()[0].description.GetIceParameters().renomination);
3265}
3266
3267// Test that the ICE renomination option is present in generated offers/answers
3268// if it's enabled in the PC's RTCConfiguration.
3269TEST_F(PeerConnectionInterfaceTest, IceRenominationOptionInOfferAndAnswer) {
3270 PeerConnectionInterface::RTCConfiguration config;
3271 config.enable_ice_renomination = true;
3272 CreatePeerConnection(config, nullptr);
3273 AddVoiceStream("foo");
3274
3275 std::unique_ptr<SessionDescriptionInterface> offer;
3276 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
3277 cricket::SessionDescription* desc = offer->description();
3278 EXPECT_EQ(1u, desc->transport_infos().size());
3279 EXPECT_TRUE(
3280 desc->transport_infos()[0].description.GetIceParameters().renomination);
3281
3282 // Set the offer as a remote description, then create an answer and ensure it
3283 // has the renomination flag too.
Steve Antondb45ca82017-09-11 18:27:34 -07003284 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
deadbeef1dcb1642017-03-29 21:08:16 -07003285 std::unique_ptr<SessionDescriptionInterface> answer;
3286 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
3287 desc = answer->description();
3288 EXPECT_EQ(1u, desc->transport_infos().size());
3289 EXPECT_TRUE(
3290 desc->transport_infos()[0].description.GetIceParameters().renomination);
3291}
3292
3293// Test that if CreateOffer is called with the deprecated "offer to receive
3294// audio/video" constraints, they're processed and result in an offer with
3295// audio/video sections just as if RTCOfferAnswerOptions had been used.
3296TEST_F(PeerConnectionInterfaceTest, CreateOfferWithOfferToReceiveConstraints) {
3297 CreatePeerConnection();
3298
3299 FakeConstraints constraints;
3300 constraints.SetMandatoryReceiveAudio(true);
3301 constraints.SetMandatoryReceiveVideo(true);
3302 std::unique_ptr<SessionDescriptionInterface> offer;
3303 ASSERT_TRUE(DoCreateOffer(&offer, &constraints));
3304
3305 cricket::SessionDescription* desc = offer->description();
3306 const cricket::ContentInfo* audio = cricket::GetFirstAudioContent(desc);
3307 const cricket::ContentInfo* video = cricket::GetFirstVideoContent(desc);
3308 ASSERT_NE(nullptr, audio);
3309 ASSERT_NE(nullptr, video);
3310 EXPECT_FALSE(audio->rejected);
3311 EXPECT_FALSE(video->rejected);
3312}
3313
3314// Test that if CreateAnswer is called with the deprecated "offer to receive
3315// audio/video" constraints, they're processed and can be used to reject an
3316// offered m= section just as can be done with RTCOfferAnswerOptions;
3317TEST_F(PeerConnectionInterfaceTest, CreateAnswerWithOfferToReceiveConstraints) {
3318 CreatePeerConnection();
3319
3320 // First, create an offer with audio/video and apply it as a remote
3321 // description.
3322 FakeConstraints constraints;
3323 constraints.SetMandatoryReceiveAudio(true);
3324 constraints.SetMandatoryReceiveVideo(true);
3325 std::unique_ptr<SessionDescriptionInterface> offer;
3326 ASSERT_TRUE(DoCreateOffer(&offer, &constraints));
Steve Antondb45ca82017-09-11 18:27:34 -07003327 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
deadbeef1dcb1642017-03-29 21:08:16 -07003328
3329 // Now create answer that rejects audio/video.
3330 constraints.SetMandatoryReceiveAudio(false);
3331 constraints.SetMandatoryReceiveVideo(false);
3332 std::unique_ptr<SessionDescriptionInterface> answer;
3333 ASSERT_TRUE(DoCreateAnswer(&answer, &constraints));
3334
3335 cricket::SessionDescription* desc = answer->description();
3336 const cricket::ContentInfo* audio = cricket::GetFirstAudioContent(desc);
3337 const cricket::ContentInfo* video = cricket::GetFirstVideoContent(desc);
3338 ASSERT_NE(nullptr, audio);
3339 ASSERT_NE(nullptr, video);
3340 EXPECT_TRUE(audio->rejected);
3341 EXPECT_TRUE(video->rejected);
3342}
3343
3344#ifdef HAVE_SCTP
3345#define MAYBE_DataChannelOnlyOfferWithMaxBundlePolicy \
3346 DataChannelOnlyOfferWithMaxBundlePolicy
3347#else
3348#define MAYBE_DataChannelOnlyOfferWithMaxBundlePolicy \
3349 DISABLED_DataChannelOnlyOfferWithMaxBundlePolicy
3350#endif
3351
3352// Test that negotiation can succeed with a data channel only, and with the max
3353// bundle policy. Previously there was a bug that prevented this.
3354TEST_F(PeerConnectionInterfaceTest,
3355 MAYBE_DataChannelOnlyOfferWithMaxBundlePolicy) {
3356 PeerConnectionInterface::RTCConfiguration config;
3357 config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
3358 CreatePeerConnection(config, nullptr);
3359
3360 // First, create an offer with only a data channel and apply it as a remote
3361 // description.
3362 pc_->CreateDataChannel("test", nullptr);
3363 std::unique_ptr<SessionDescriptionInterface> offer;
3364 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07003365 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
deadbeef1dcb1642017-03-29 21:08:16 -07003366
3367 // Create and set answer as well.
3368 std::unique_ptr<SessionDescriptionInterface> answer;
3369 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07003370 EXPECT_TRUE(DoSetLocalDescription(std::move(answer)));
deadbeef1dcb1642017-03-29 21:08:16 -07003371}
3372
zstein4b979802017-06-02 14:37:37 -07003373TEST_F(PeerConnectionInterfaceTest, SetBitrateWithoutMinSucceeds) {
3374 CreatePeerConnection();
3375 PeerConnectionInterface::BitrateParameters bitrate;
3376 bitrate.current_bitrate_bps = rtc::Optional<int>(100000);
3377 EXPECT_TRUE(pc_->SetBitrate(bitrate).ok());
3378}
3379
3380TEST_F(PeerConnectionInterfaceTest, SetBitrateNegativeMinFails) {
3381 CreatePeerConnection();
3382 PeerConnectionInterface::BitrateParameters bitrate;
3383 bitrate.min_bitrate_bps = rtc::Optional<int>(-1);
3384 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3385}
3386
3387TEST_F(PeerConnectionInterfaceTest, SetBitrateCurrentLessThanMinFails) {
3388 CreatePeerConnection();
3389 PeerConnectionInterface::BitrateParameters bitrate;
3390 bitrate.min_bitrate_bps = rtc::Optional<int>(5);
3391 bitrate.current_bitrate_bps = rtc::Optional<int>(3);
3392 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3393}
3394
3395TEST_F(PeerConnectionInterfaceTest, SetBitrateCurrentNegativeFails) {
3396 CreatePeerConnection();
3397 PeerConnectionInterface::BitrateParameters bitrate;
3398 bitrate.current_bitrate_bps = rtc::Optional<int>(-1);
3399 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3400}
3401
3402TEST_F(PeerConnectionInterfaceTest, SetBitrateMaxLessThanCurrentFails) {
3403 CreatePeerConnection();
3404 PeerConnectionInterface::BitrateParameters bitrate;
3405 bitrate.current_bitrate_bps = rtc::Optional<int>(10);
3406 bitrate.max_bitrate_bps = rtc::Optional<int>(8);
3407 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3408}
3409
3410TEST_F(PeerConnectionInterfaceTest, SetBitrateMaxLessThanMinFails) {
3411 CreatePeerConnection();
3412 PeerConnectionInterface::BitrateParameters bitrate;
3413 bitrate.min_bitrate_bps = rtc::Optional<int>(10);
3414 bitrate.max_bitrate_bps = rtc::Optional<int>(8);
3415 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3416}
3417
3418TEST_F(PeerConnectionInterfaceTest, SetBitrateMaxNegativeFails) {
3419 CreatePeerConnection();
3420 PeerConnectionInterface::BitrateParameters bitrate;
3421 bitrate.max_bitrate_bps = rtc::Optional<int>(-1);
3422 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3423}
3424
Steve Anton038834f2017-07-14 15:59:59 -07003425// ice_regather_interval_range requires WebRTC to be configured for continual
3426// gathering already.
3427TEST_F(PeerConnectionInterfaceTest,
3428 SetIceRegatherIntervalRangeWithoutContinualGatheringFails) {
3429 PeerConnectionInterface::RTCConfiguration config;
3430 config.ice_regather_interval_range.emplace(1000, 2000);
3431 config.continual_gathering_policy =
3432 PeerConnectionInterface::ContinualGatheringPolicy::GATHER_ONCE;
3433 CreatePeerConnectionExpectFail(config);
3434}
3435
3436// Ensures that there is no error when ice_regather_interval_range is set with
3437// continual gathering enabled.
3438TEST_F(PeerConnectionInterfaceTest,
3439 SetIceRegatherIntervalRangeWithContinualGathering) {
3440 PeerConnectionInterface::RTCConfiguration config;
3441 config.ice_regather_interval_range.emplace(1000, 2000);
3442 config.continual_gathering_policy =
3443 PeerConnectionInterface::ContinualGatheringPolicy::GATHER_CONTINUALLY;
3444 CreatePeerConnection(config, nullptr);
3445}
3446
zstein4b979802017-06-02 14:37:37 -07003447// The current bitrate from Call's BitrateConfigMask is currently clamped by
3448// Call's BitrateConfig, which comes from the SDP or a default value. This test
3449// checks that a call to SetBitrate with a current bitrate that will be clamped
3450// succeeds.
3451TEST_F(PeerConnectionInterfaceTest, SetBitrateCurrentLessThanImplicitMin) {
3452 CreatePeerConnection();
3453 PeerConnectionInterface::BitrateParameters bitrate;
3454 bitrate.current_bitrate_bps = rtc::Optional<int>(1);
3455 EXPECT_TRUE(pc_->SetBitrate(bitrate).ok());
3456}
3457
zhihuang1c378ed2017-08-17 14:10:50 -07003458// The following tests verify that the offer can be created correctly.
3459TEST_F(PeerConnectionInterfaceTest,
3460 CreateOfferFailsWithInvalidOfferToReceiveAudio) {
3461 RTCOfferAnswerOptions rtc_options;
3462
3463 // Setting offer_to_receive_audio to a value lower than kUndefined or greater
3464 // than kMaxOfferToReceiveMedia should be treated as invalid.
3465 rtc_options.offer_to_receive_audio = RTCOfferAnswerOptions::kUndefined - 1;
3466 CreatePeerConnection();
3467 EXPECT_FALSE(CreateOfferWithOptions(rtc_options));
3468
3469 rtc_options.offer_to_receive_audio =
3470 RTCOfferAnswerOptions::kMaxOfferToReceiveMedia + 1;
3471 EXPECT_FALSE(CreateOfferWithOptions(rtc_options));
3472}
3473
3474TEST_F(PeerConnectionInterfaceTest,
3475 CreateOfferFailsWithInvalidOfferToReceiveVideo) {
3476 RTCOfferAnswerOptions rtc_options;
3477
3478 // Setting offer_to_receive_video to a value lower than kUndefined or greater
3479 // than kMaxOfferToReceiveMedia should be treated as invalid.
3480 rtc_options.offer_to_receive_video = RTCOfferAnswerOptions::kUndefined - 1;
3481 CreatePeerConnection();
3482 EXPECT_FALSE(CreateOfferWithOptions(rtc_options));
3483
3484 rtc_options.offer_to_receive_video =
3485 RTCOfferAnswerOptions::kMaxOfferToReceiveMedia + 1;
3486 EXPECT_FALSE(CreateOfferWithOptions(rtc_options));
3487}
3488
3489// Test that the audio and video content will be added to an offer if both
3490// |offer_to_receive_audio| and |offer_to_receive_video| options are 1.
3491TEST_F(PeerConnectionInterfaceTest, CreateOfferWithAudioVideoOptions) {
3492 RTCOfferAnswerOptions rtc_options;
3493 rtc_options.offer_to_receive_audio = 1;
3494 rtc_options.offer_to_receive_video = 1;
3495
3496 std::unique_ptr<SessionDescriptionInterface> offer;
3497 CreatePeerConnection();
3498 offer = CreateOfferWithOptions(rtc_options);
3499 ASSERT_TRUE(offer);
3500 EXPECT_NE(nullptr, GetFirstAudioContent(offer->description()));
3501 EXPECT_NE(nullptr, GetFirstVideoContent(offer->description()));
3502}
3503
3504// Test that only audio content will be added to the offer if only
3505// |offer_to_receive_audio| options is 1.
3506TEST_F(PeerConnectionInterfaceTest, CreateOfferWithAudioOnlyOptions) {
3507 RTCOfferAnswerOptions rtc_options;
3508 rtc_options.offer_to_receive_audio = 1;
3509 rtc_options.offer_to_receive_video = 0;
3510
3511 std::unique_ptr<SessionDescriptionInterface> offer;
3512 CreatePeerConnection();
3513 offer = CreateOfferWithOptions(rtc_options);
3514 ASSERT_TRUE(offer);
3515 EXPECT_NE(nullptr, GetFirstAudioContent(offer->description()));
3516 EXPECT_EQ(nullptr, GetFirstVideoContent(offer->description()));
3517}
3518
3519// Test that only video content will be added if only |offer_to_receive_video|
3520// options is 1.
3521TEST_F(PeerConnectionInterfaceTest, CreateOfferWithVideoOnlyOptions) {
3522 RTCOfferAnswerOptions rtc_options;
3523 rtc_options.offer_to_receive_audio = 0;
3524 rtc_options.offer_to_receive_video = 1;
3525
3526 std::unique_ptr<SessionDescriptionInterface> offer;
3527 CreatePeerConnection();
3528 offer = CreateOfferWithOptions(rtc_options);
3529 ASSERT_TRUE(offer);
3530 EXPECT_EQ(nullptr, GetFirstAudioContent(offer->description()));
3531 EXPECT_NE(nullptr, GetFirstVideoContent(offer->description()));
3532}
3533
3534// Test that if |voice_activity_detection| is false, no CN codec is added to the
3535// offer.
3536TEST_F(PeerConnectionInterfaceTest, CreateOfferWithVADOptions) {
3537 RTCOfferAnswerOptions rtc_options;
3538 rtc_options.offer_to_receive_audio = 1;
3539 rtc_options.offer_to_receive_video = 0;
3540
3541 std::unique_ptr<SessionDescriptionInterface> offer;
3542 CreatePeerConnection();
3543 offer = CreateOfferWithOptions(rtc_options);
3544 ASSERT_TRUE(offer);
3545 const cricket::ContentInfo* audio_content =
3546 offer->description()->GetContentByName(cricket::CN_AUDIO);
3547 ASSERT_TRUE(audio_content);
3548 // |voice_activity_detection| is true by default.
3549 EXPECT_TRUE(HasCNCodecs(audio_content));
3550
3551 rtc_options.voice_activity_detection = false;
3552 CreatePeerConnection();
3553 offer = CreateOfferWithOptions(rtc_options);
3554 ASSERT_TRUE(offer);
3555 audio_content = offer->description()->GetContentByName(cricket::CN_AUDIO);
3556 ASSERT_TRUE(audio_content);
3557 EXPECT_FALSE(HasCNCodecs(audio_content));
3558}
3559
3560// Test that no media content will be added to the offer if using default
3561// RTCOfferAnswerOptions.
3562TEST_F(PeerConnectionInterfaceTest, CreateOfferWithDefaultOfferAnswerOptions) {
3563 RTCOfferAnswerOptions rtc_options;
3564
3565 std::unique_ptr<SessionDescriptionInterface> offer;
3566 CreatePeerConnection();
3567 offer = CreateOfferWithOptions(rtc_options);
3568 ASSERT_TRUE(offer);
3569 EXPECT_EQ(nullptr, GetFirstAudioContent(offer->description()));
3570 EXPECT_EQ(nullptr, GetFirstVideoContent(offer->description()));
3571}
3572
3573// Test that if |ice_restart| is true, the ufrag/pwd will change, otherwise
3574// ufrag/pwd will be the same in the new offer.
3575TEST_F(PeerConnectionInterfaceTest, CreateOfferWithIceRestart) {
3576 RTCOfferAnswerOptions rtc_options;
3577 rtc_options.ice_restart = false;
3578 rtc_options.offer_to_receive_audio = 1;
3579
3580 std::unique_ptr<SessionDescriptionInterface> offer;
3581 CreatePeerConnection();
3582 CreateOfferWithOptionsAsLocalDescription(&offer, rtc_options);
3583 auto ufrag1 = offer->description()
3584 ->GetTransportInfoByName(cricket::CN_AUDIO)
3585 ->description.ice_ufrag;
3586 auto pwd1 = offer->description()
3587 ->GetTransportInfoByName(cricket::CN_AUDIO)
3588 ->description.ice_pwd;
3589
3590 // |ice_restart| is false, the ufrag/pwd shouldn't change.
3591 CreateOfferWithOptionsAsLocalDescription(&offer, rtc_options);
3592 auto ufrag2 = offer->description()
3593 ->GetTransportInfoByName(cricket::CN_AUDIO)
3594 ->description.ice_ufrag;
3595 auto pwd2 = offer->description()
3596 ->GetTransportInfoByName(cricket::CN_AUDIO)
3597 ->description.ice_pwd;
3598
3599 // |ice_restart| is true, the ufrag/pwd should change.
3600 rtc_options.ice_restart = true;
3601 CreateOfferWithOptionsAsLocalDescription(&offer, rtc_options);
3602 auto ufrag3 = offer->description()
3603 ->GetTransportInfoByName(cricket::CN_AUDIO)
3604 ->description.ice_ufrag;
3605 auto pwd3 = offer->description()
3606 ->GetTransportInfoByName(cricket::CN_AUDIO)
3607 ->description.ice_pwd;
3608
3609 EXPECT_EQ(ufrag1, ufrag2);
3610 EXPECT_EQ(pwd1, pwd2);
3611 EXPECT_NE(ufrag2, ufrag3);
3612 EXPECT_NE(pwd2, pwd3);
3613}
3614
3615// Test that if |use_rtp_mux| is true, the bundling will be enabled in the
3616// offer; if it is false, there won't be any bundle group in the offer.
3617TEST_F(PeerConnectionInterfaceTest, CreateOfferWithRtpMux) {
3618 RTCOfferAnswerOptions rtc_options;
3619 rtc_options.offer_to_receive_audio = 1;
3620 rtc_options.offer_to_receive_video = 1;
3621
3622 std::unique_ptr<SessionDescriptionInterface> offer;
3623 CreatePeerConnection();
3624
3625 rtc_options.use_rtp_mux = true;
3626 offer = CreateOfferWithOptions(rtc_options);
3627 ASSERT_TRUE(offer);
3628 EXPECT_NE(nullptr, GetFirstAudioContent(offer->description()));
3629 EXPECT_NE(nullptr, GetFirstVideoContent(offer->description()));
3630 EXPECT_TRUE(offer->description()->HasGroup(cricket::GROUP_TYPE_BUNDLE));
3631
3632 rtc_options.use_rtp_mux = false;
3633 offer = CreateOfferWithOptions(rtc_options);
3634 ASSERT_TRUE(offer);
3635 EXPECT_NE(nullptr, GetFirstAudioContent(offer->description()));
3636 EXPECT_NE(nullptr, GetFirstVideoContent(offer->description()));
3637 EXPECT_FALSE(offer->description()->HasGroup(cricket::GROUP_TYPE_BUNDLE));
3638}
3639
3640// If SetMandatoryReceiveAudio(false) and SetMandatoryReceiveVideo(false) are
3641// called for the answer constraints, but an audio and a video section were
3642// offered, there will still be an audio and a video section in the answer.
3643TEST_F(PeerConnectionInterfaceTest,
3644 RejectAudioAndVideoInAnswerWithConstraints) {
3645 // Offer both audio and video.
3646 RTCOfferAnswerOptions rtc_offer_options;
3647 rtc_offer_options.offer_to_receive_audio = 1;
3648 rtc_offer_options.offer_to_receive_video = 1;
3649
3650 CreatePeerConnection();
3651 std::unique_ptr<SessionDescriptionInterface> offer;
3652 CreateOfferWithOptionsAsRemoteDescription(&offer, rtc_offer_options);
3653 EXPECT_NE(nullptr, GetFirstAudioContent(offer->description()));
3654 EXPECT_NE(nullptr, GetFirstVideoContent(offer->description()));
3655
3656 // Since an offer has been created with both audio and video,
3657 // Answers will contain the media types that exist in the offer regardless of
3658 // the value of |answer_options.has_audio| and |answer_options.has_video|.
3659 FakeConstraints answer_c;
3660 // Reject both audio and video.
3661 answer_c.SetMandatoryReceiveAudio(false);
3662 answer_c.SetMandatoryReceiveVideo(false);
3663
3664 std::unique_ptr<SessionDescriptionInterface> answer;
3665 ASSERT_TRUE(DoCreateAnswer(&answer, &answer_c));
3666 const cricket::ContentInfo* audio_content =
3667 GetFirstAudioContent(answer->description());
3668 const cricket::ContentInfo* video_content =
3669 GetFirstVideoContent(answer->description());
3670 ASSERT_NE(nullptr, audio_content);
3671 ASSERT_NE(nullptr, video_content);
3672 EXPECT_TRUE(audio_content->rejected);
3673 EXPECT_TRUE(video_content->rejected);
3674}
3675
zhihuang141aacb2017-08-29 13:23:53 -07003676// This test ensures OnRenegotiationNeeded is called when we add track with
3677// MediaStream -> AddTrack in the same way it is called when we add track with
3678// PeerConnection -> AddTrack.
3679// The test can be removed once addStream is rewritten in terms of addTrack
3680// https://bugs.chromium.org/p/webrtc/issues/detail?id=7815
3681TEST_F(PeerConnectionInterfaceTest, MediaStreamAddTrackRemoveTrackRenegotiate) {
3682 CreatePeerConnectionWithoutDtls();
3683 rtc::scoped_refptr<MediaStreamInterface> stream(
3684 pc_factory_->CreateLocalMediaStream(kStreamLabel1));
3685 pc_->AddStream(stream);
3686 rtc::scoped_refptr<AudioTrackInterface> audio_track(
3687 pc_factory_->CreateAudioTrack("audio_track", nullptr));
3688 rtc::scoped_refptr<VideoTrackInterface> video_track(
3689 pc_factory_->CreateVideoTrack(
3690 "video_track", pc_factory_->CreateVideoSource(
3691 std::unique_ptr<cricket::VideoCapturer>(
3692 new cricket::FakeVideoCapturer()))));
3693 stream->AddTrack(audio_track);
3694 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
3695 observer_.renegotiation_needed_ = false;
3696
3697 stream->AddTrack(video_track);
3698 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
3699 observer_.renegotiation_needed_ = false;
3700
3701 stream->RemoveTrack(audio_track);
3702 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
3703 observer_.renegotiation_needed_ = false;
3704
3705 stream->RemoveTrack(video_track);
3706 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
3707 observer_.renegotiation_needed_ = false;
3708}
3709
3710// Tests that creating answer would fail gracefully without being crashed if the
3711// remote description is unset.
3712TEST_F(PeerConnectionInterfaceTest, CreateAnswerWithoutRemoteDescription) {
3713 CreatePeerConnection();
3714 // Creating answer fails because the remote description is unset.
3715 std::unique_ptr<SessionDescriptionInterface> answer;
3716 EXPECT_FALSE(DoCreateAnswer(&answer, nullptr));
3717
3718 // Createing answer succeeds when the remote description is set.
3719 CreateOfferAsRemoteDescription();
3720 EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
3721}
3722
Zhi Huang2a5e4262017-09-14 01:15:03 -07003723// Test that an error is returned if a description is applied that doesn't
3724// respect the order of existing media sections.
3725TEST_F(PeerConnectionInterfaceTest,
3726 MediaSectionOrderEnforcedForSubsequentOffers) {
3727 CreatePeerConnection();
3728 FakeConstraints constraints;
3729 constraints.SetMandatoryReceiveAudio(true);
3730 constraints.SetMandatoryReceiveVideo(true);
3731 std::unique_ptr<SessionDescriptionInterface> offer;
3732 ASSERT_TRUE(DoCreateOffer(&offer, &constraints));
3733 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
3734
3735 std::unique_ptr<SessionDescriptionInterface> answer;
3736 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
3737 EXPECT_TRUE(DoSetLocalDescription(std::move(answer)));
3738
3739 // A remote offer with different m=line order should be rejected.
3740 ASSERT_TRUE(DoCreateOffer(&offer, &constraints));
3741 std::reverse(offer->description()->contents().begin(),
3742 offer->description()->contents().end());
3743 std::reverse(offer->description()->transport_infos().begin(),
3744 offer->description()->transport_infos().end());
3745 EXPECT_FALSE(DoSetRemoteDescription(std::move(offer)));
3746
3747 // A subsequent local offer with different m=line order should be rejected.
3748 ASSERT_TRUE(DoCreateOffer(&offer, &constraints));
3749 std::reverse(offer->description()->contents().begin(),
3750 offer->description()->contents().end());
3751 std::reverse(offer->description()->transport_infos().begin(),
3752 offer->description()->transport_infos().end());
3753 EXPECT_FALSE(DoSetLocalDescription(std::move(offer)));
3754}
3755
nisse51542be2016-02-12 02:27:06 -08003756class PeerConnectionMediaConfigTest : public testing::Test {
3757 protected:
3758 void SetUp() override {
zhihuang38ede132017-06-15 12:52:32 -07003759 pcf_ = PeerConnectionFactoryForTest::CreatePeerConnectionFactoryForTest();
nisse51542be2016-02-12 02:27:06 -08003760 pcf_->Initialize();
3761 }
nisseeaabdf62017-05-05 02:23:02 -07003762 const cricket::MediaConfig TestCreatePeerConnection(
nisse51542be2016-02-12 02:27:06 -08003763 const PeerConnectionInterface::RTCConfiguration& config,
zhihuang1c378ed2017-08-17 14:10:50 -07003764 const MediaConstraintsInterface* constraints) {
zhihuang9763d562016-08-05 11:14:50 -07003765 rtc::scoped_refptr<PeerConnectionInterface> pc(pcf_->CreatePeerConnection(
3766 config, constraints, nullptr, nullptr, &observer_));
nisse51542be2016-02-12 02:27:06 -08003767 EXPECT_TRUE(pc.get());
nisseeaabdf62017-05-05 02:23:02 -07003768 return pc->GetConfiguration().media_config;
nisse51542be2016-02-12 02:27:06 -08003769 }
3770
zhihuang9763d562016-08-05 11:14:50 -07003771 rtc::scoped_refptr<PeerConnectionFactoryForTest> pcf_;
nisse51542be2016-02-12 02:27:06 -08003772 MockPeerConnectionObserver observer_;
3773};
3774
3775// This test verifies the default behaviour with no constraints and a
3776// default RTCConfiguration.
3777TEST_F(PeerConnectionMediaConfigTest, TestDefaults) {
3778 PeerConnectionInterface::RTCConfiguration config;
3779 FakeConstraints constraints;
3780
3781 const cricket::MediaConfig& media_config =
3782 TestCreatePeerConnection(config, &constraints);
3783
3784 EXPECT_FALSE(media_config.enable_dscp);
nisse0db023a2016-03-01 04:29:59 -08003785 EXPECT_TRUE(media_config.video.enable_cpu_overuse_detection);
3786 EXPECT_FALSE(media_config.video.disable_prerenderer_smoothing);
3787 EXPECT_FALSE(media_config.video.suspend_below_min_bitrate);
nisse51542be2016-02-12 02:27:06 -08003788}
3789
3790// This test verifies the DSCP constraint is recognized and passed to
nisse528b7932017-05-08 03:21:43 -07003791// the PeerConnection.
nisse51542be2016-02-12 02:27:06 -08003792TEST_F(PeerConnectionMediaConfigTest, TestDscpConstraintTrue) {
3793 PeerConnectionInterface::RTCConfiguration config;
3794 FakeConstraints constraints;
3795
3796 constraints.AddOptional(webrtc::MediaConstraintsInterface::kEnableDscp, true);
3797 const cricket::MediaConfig& media_config =
3798 TestCreatePeerConnection(config, &constraints);
3799
3800 EXPECT_TRUE(media_config.enable_dscp);
3801}
3802
3803// This test verifies the cpu overuse detection constraint is
nisse528b7932017-05-08 03:21:43 -07003804// recognized and passed to the PeerConnection.
nisse51542be2016-02-12 02:27:06 -08003805TEST_F(PeerConnectionMediaConfigTest, TestCpuOveruseConstraintFalse) {
3806 PeerConnectionInterface::RTCConfiguration config;
3807 FakeConstraints constraints;
3808
3809 constraints.AddOptional(
3810 webrtc::MediaConstraintsInterface::kCpuOveruseDetection, false);
3811 const cricket::MediaConfig media_config =
3812 TestCreatePeerConnection(config, &constraints);
3813
nisse0db023a2016-03-01 04:29:59 -08003814 EXPECT_FALSE(media_config.video.enable_cpu_overuse_detection);
nisse51542be2016-02-12 02:27:06 -08003815}
3816
3817// This test verifies that the disable_prerenderer_smoothing flag is
nisse528b7932017-05-08 03:21:43 -07003818// propagated from RTCConfiguration to the PeerConnection.
nisse51542be2016-02-12 02:27:06 -08003819TEST_F(PeerConnectionMediaConfigTest, TestDisablePrerendererSmoothingTrue) {
3820 PeerConnectionInterface::RTCConfiguration config;
3821 FakeConstraints constraints;
3822
Niels Möller71bdda02016-03-31 12:59:59 +02003823 config.set_prerenderer_smoothing(false);
nisse51542be2016-02-12 02:27:06 -08003824 const cricket::MediaConfig& media_config =
3825 TestCreatePeerConnection(config, &constraints);
3826
nisse0db023a2016-03-01 04:29:59 -08003827 EXPECT_TRUE(media_config.video.disable_prerenderer_smoothing);
3828}
3829
3830// This test verifies the suspend below min bitrate constraint is
nisse528b7932017-05-08 03:21:43 -07003831// recognized and passed to the PeerConnection.
nisse0db023a2016-03-01 04:29:59 -08003832TEST_F(PeerConnectionMediaConfigTest,
3833 TestSuspendBelowMinBitrateConstraintTrue) {
3834 PeerConnectionInterface::RTCConfiguration config;
3835 FakeConstraints constraints;
3836
3837 constraints.AddOptional(
3838 webrtc::MediaConstraintsInterface::kEnableVideoSuspendBelowMinBitrate,
3839 true);
3840 const cricket::MediaConfig media_config =
3841 TestCreatePeerConnection(config, &constraints);
3842
3843 EXPECT_TRUE(media_config.video.suspend_below_min_bitrate);
nisse51542be2016-02-12 02:27:06 -08003844}
3845
deadbeef293e9262017-01-11 12:28:30 -08003846// Tests a few random fields being different.
3847TEST(RTCConfigurationTest, ComparisonOperators) {
3848 PeerConnectionInterface::RTCConfiguration a;
3849 PeerConnectionInterface::RTCConfiguration b;
3850 EXPECT_EQ(a, b);
3851
3852 PeerConnectionInterface::RTCConfiguration c;
3853 c.servers.push_back(PeerConnectionInterface::IceServer());
3854 EXPECT_NE(a, c);
3855
3856 PeerConnectionInterface::RTCConfiguration d;
3857 d.type = PeerConnectionInterface::kRelay;
3858 EXPECT_NE(a, d);
3859
3860 PeerConnectionInterface::RTCConfiguration e;
3861 e.audio_jitter_buffer_max_packets = 5;
3862 EXPECT_NE(a, e);
3863
3864 PeerConnectionInterface::RTCConfiguration f;
3865 f.ice_connection_receiving_timeout = 1337;
3866 EXPECT_NE(a, f);
3867
3868 PeerConnectionInterface::RTCConfiguration g;
3869 g.disable_ipv6 = true;
3870 EXPECT_NE(a, g);
3871
3872 PeerConnectionInterface::RTCConfiguration h(
3873 PeerConnectionInterface::RTCConfigurationType::kAggressive);
3874 EXPECT_NE(a, h);
3875}