blob: cc063b4f74662b5b738974078ef94554c05f4377 [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"
kwibergac9f8762016-09-30 22:29:43 -070046
47#ifdef WEBRTC_ANDROID
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020048#include "pc/test/androidtestinitializer.h"
kwibergac9f8762016-09-30 22:29:43 -070049#endif
henrike@webrtc.org28e20752013-07-10 00:45:36 +000050
51static const char kStreamLabel1[] = "local_stream_1";
52static const char kStreamLabel2[] = "local_stream_2";
53static const char kStreamLabel3[] = "local_stream_3";
54static const int kDefaultStunPort = 3478;
55static const char kStunAddressOnly[] = "stun:address";
56static const char kStunInvalidPort[] = "stun:address:-1";
57static const char kStunAddressPortAndMore1[] = "stun:address:port:more";
58static const char kStunAddressPortAndMore2[] = "stun:address:port more";
59static const char kTurnIceServerUri[] = "turn:user@turn.example.org";
60static const char kTurnUsername[] = "user";
61static const char kTurnPassword[] = "password";
62static const char kTurnHostname[] = "turn.example.org";
Peter Boström0c4e06b2015-10-07 12:23:21 +020063static const uint32_t kTimeout = 10000U;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000064
deadbeefab9b2d12015-10-14 11:33:11 -070065static const char kStreams[][8] = {"stream1", "stream2"};
66static const char kAudioTracks[][32] = {"audiotrack0", "audiotrack1"};
67static const char kVideoTracks[][32] = {"videotrack0", "videotrack1"};
68
deadbeef5e97fb52015-10-15 12:49:08 -070069static const char kRecvonly[] = "recvonly";
70static const char kSendrecv[] = "sendrecv";
71
deadbeefab9b2d12015-10-14 11:33:11 -070072// Reference SDP with a MediaStream with label "stream1" and audio track with
73// id "audio_1" and a video track with id "video_1;
74static const char kSdpStringWithStream1[] =
75 "v=0\r\n"
76 "o=- 0 0 IN IP4 127.0.0.1\r\n"
77 "s=-\r\n"
78 "t=0 0\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -080079 "m=audio 1 RTP/AVPF 103\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -070080 "a=ice-ufrag:e5785931\r\n"
81 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
82 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
83 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -070084 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -070085 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -080086 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -070087 "a=rtpmap:103 ISAC/16000\r\n"
88 "a=ssrc:1 cname:stream1\r\n"
89 "a=ssrc:1 mslabel:stream1\r\n"
90 "a=ssrc:1 label:audiotrack0\r\n"
91 "m=video 1 RTP/AVPF 120\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -080092 "a=ice-ufrag:e5785931\r\n"
93 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
94 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
95 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -070096 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -070097 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -080098 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -070099 "a=rtpmap:120 VP8/90000\r\n"
100 "a=ssrc:2 cname:stream1\r\n"
101 "a=ssrc:2 mslabel:stream1\r\n"
102 "a=ssrc:2 label:videotrack0\r\n";
103
zhihuang81c3a032016-11-17 12:06:24 -0800104// Reference SDP with a MediaStream with label "stream1" and audio track with
105// id "audio_1";
106static const char kSdpStringWithStream1AudioTrackOnly[] =
107 "v=0\r\n"
108 "o=- 0 0 IN IP4 127.0.0.1\r\n"
109 "s=-\r\n"
110 "t=0 0\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800111 "m=audio 1 RTP/AVPF 103\r\n"
zhihuang81c3a032016-11-17 12:06:24 -0800112 "a=ice-ufrag:e5785931\r\n"
113 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
114 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
115 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
zhihuang81c3a032016-11-17 12:06:24 -0800116 "a=mid:audio\r\n"
117 "a=sendrecv\r\n"
118 "a=rtpmap:103 ISAC/16000\r\n"
119 "a=ssrc:1 cname:stream1\r\n"
120 "a=ssrc:1 mslabel:stream1\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800121 "a=ssrc:1 label:audiotrack0\r\n"
122 "a=rtcp-mux\r\n";
zhihuang81c3a032016-11-17 12:06:24 -0800123
deadbeefab9b2d12015-10-14 11:33:11 -0700124// Reference SDP with two MediaStreams with label "stream1" and "stream2. Each
125// MediaStreams have one audio track and one video track.
126// This uses MSID.
127static const char kSdpStringWithStream1And2[] =
128 "v=0\r\n"
129 "o=- 0 0 IN IP4 127.0.0.1\r\n"
130 "s=-\r\n"
131 "t=0 0\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800132 "a=msid-semantic: WMS stream1 stream2\r\n"
133 "m=audio 1 RTP/AVPF 103\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700134 "a=ice-ufrag:e5785931\r\n"
135 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
136 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
137 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700138 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700139 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800140 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700141 "a=rtpmap:103 ISAC/16000\r\n"
142 "a=ssrc:1 cname:stream1\r\n"
143 "a=ssrc:1 msid:stream1 audiotrack0\r\n"
144 "a=ssrc:3 cname:stream2\r\n"
145 "a=ssrc:3 msid:stream2 audiotrack1\r\n"
146 "m=video 1 RTP/AVPF 120\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800147 "a=ice-ufrag:e5785931\r\n"
148 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
149 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
150 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700151 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700152 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800153 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700154 "a=rtpmap:120 VP8/0\r\n"
155 "a=ssrc:2 cname:stream1\r\n"
156 "a=ssrc:2 msid:stream1 videotrack0\r\n"
157 "a=ssrc:4 cname:stream2\r\n"
158 "a=ssrc:4 msid:stream2 videotrack1\r\n";
159
160// Reference SDP without MediaStreams. Msid is not supported.
161static const char kSdpStringWithoutStreams[] =
162 "v=0\r\n"
163 "o=- 0 0 IN IP4 127.0.0.1\r\n"
164 "s=-\r\n"
165 "t=0 0\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800166 "m=audio 1 RTP/AVPF 103\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700167 "a=ice-ufrag:e5785931\r\n"
168 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
169 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
170 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700171 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700172 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800173 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700174 "a=rtpmap:103 ISAC/16000\r\n"
175 "m=video 1 RTP/AVPF 120\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800176 "a=ice-ufrag:e5785931\r\n"
177 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
178 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
179 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700180 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700181 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800182 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700183 "a=rtpmap:120 VP8/90000\r\n";
184
185// Reference SDP without MediaStreams. Msid is supported.
186static const char kSdpStringWithMsidWithoutStreams[] =
187 "v=0\r\n"
188 "o=- 0 0 IN IP4 127.0.0.1\r\n"
189 "s=-\r\n"
190 "t=0 0\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800191 "a=msid-semantic: WMS\r\n"
192 "m=audio 1 RTP/AVPF 103\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700193 "a=ice-ufrag:e5785931\r\n"
194 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
195 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
196 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700197 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700198 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800199 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700200 "a=rtpmap:103 ISAC/16000\r\n"
201 "m=video 1 RTP/AVPF 120\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800202 "a=ice-ufrag:e5785931\r\n"
203 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
204 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
205 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700206 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700207 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800208 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700209 "a=rtpmap:120 VP8/90000\r\n";
210
211// Reference SDP without MediaStreams and audio only.
212static const char kSdpStringWithoutStreamsAudioOnly[] =
213 "v=0\r\n"
214 "o=- 0 0 IN IP4 127.0.0.1\r\n"
215 "s=-\r\n"
216 "t=0 0\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800217 "m=audio 1 RTP/AVPF 103\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700218 "a=ice-ufrag:e5785931\r\n"
219 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
220 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
221 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700222 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700223 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800224 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700225 "a=rtpmap:103 ISAC/16000\r\n";
226
227// Reference SENDONLY SDP without MediaStreams. Msid is not supported.
228static const char kSdpStringSendOnlyWithoutStreams[] =
229 "v=0\r\n"
230 "o=- 0 0 IN IP4 127.0.0.1\r\n"
231 "s=-\r\n"
232 "t=0 0\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800233 "m=audio 1 RTP/AVPF 103\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700234 "a=ice-ufrag:e5785931\r\n"
235 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
236 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
237 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700238 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700239 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700240 "a=sendonly\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800241 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700242 "a=rtpmap:103 ISAC/16000\r\n"
243 "m=video 1 RTP/AVPF 120\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800244 "a=ice-ufrag:e5785931\r\n"
245 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
246 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
247 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700248 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700249 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700250 "a=sendonly\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800251 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700252 "a=rtpmap:120 VP8/90000\r\n";
253
254static const char kSdpStringInit[] =
255 "v=0\r\n"
256 "o=- 0 0 IN IP4 127.0.0.1\r\n"
257 "s=-\r\n"
258 "t=0 0\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700259 "a=msid-semantic: WMS\r\n";
260
261static const char kSdpStringAudio[] =
262 "m=audio 1 RTP/AVPF 103\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800263 "a=ice-ufrag:e5785931\r\n"
264 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
265 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
266 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700267 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700268 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800269 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700270 "a=rtpmap:103 ISAC/16000\r\n";
271
272static const char kSdpStringVideo[] =
273 "m=video 1 RTP/AVPF 120\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800274 "a=ice-ufrag:e5785931\r\n"
275 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
276 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
277 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700278 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700279 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800280 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700281 "a=rtpmap:120 VP8/90000\r\n";
282
283static const char kSdpStringMs1Audio0[] =
284 "a=ssrc:1 cname:stream1\r\n"
285 "a=ssrc:1 msid:stream1 audiotrack0\r\n";
286
287static const char kSdpStringMs1Video0[] =
288 "a=ssrc:2 cname:stream1\r\n"
289 "a=ssrc:2 msid:stream1 videotrack0\r\n";
290
291static const char kSdpStringMs1Audio1[] =
292 "a=ssrc:3 cname:stream1\r\n"
293 "a=ssrc:3 msid:stream1 audiotrack1\r\n";
294
295static const char kSdpStringMs1Video1[] =
296 "a=ssrc:4 cname:stream1\r\n"
297 "a=ssrc:4 msid:stream1 videotrack1\r\n";
298
deadbeef8662f942017-01-20 21:20:51 -0800299static const char kDtlsSdesFallbackSdp[] =
300 "v=0\r\n"
301 "o=xxxxxx 7 2 IN IP4 0.0.0.0\r\n"
302 "s=-\r\n"
303 "c=IN IP4 0.0.0.0\r\n"
304 "t=0 0\r\n"
305 "a=group:BUNDLE audio\r\n"
306 "a=msid-semantic: WMS\r\n"
307 "m=audio 1 RTP/SAVPF 0\r\n"
308 "a=sendrecv\r\n"
309 "a=rtcp-mux\r\n"
310 "a=mid:audio\r\n"
311 "a=ssrc:1 cname:stream1\r\n"
312 "a=ssrc:1 mslabel:stream1\r\n"
313 "a=ssrc:1 label:audiotrack0\r\n"
314 "a=ice-ufrag:e5785931\r\n"
315 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
316 "a=rtpmap:0 pcmu/8000\r\n"
317 "a=fingerprint:sha-1 "
318 "4A:AD:B9:B1:3F:82:18:3B:54:02:12:DF:3E:5D:49:6B:19:E5:7C:AB\r\n"
319 "a=setup:actpass\r\n"
320 "a=crypto:1 AES_CM_128_HMAC_SHA1_32 "
321 "inline:NzB4d1BINUAvLEw6UzF3WSJ+PSdFcGdUJShpX1Zj|2^20|1:32 "
322 "dummy_session_params\r\n";
323
perkjd61bf802016-03-24 03:16:19 -0700324using ::testing::Exactly;
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700325using cricket::StreamParams;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000326using webrtc::AudioSourceInterface;
deadbeefab9b2d12015-10-14 11:33:11 -0700327using webrtc::AudioTrack;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000328using webrtc::AudioTrackInterface;
329using webrtc::DataBuffer;
330using webrtc::DataChannelInterface;
331using webrtc::FakeConstraints;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000332using webrtc::IceCandidateInterface;
deadbeefc80741f2015-10-22 13:14:45 -0700333using webrtc::MediaConstraintsInterface;
deadbeefab9b2d12015-10-14 11:33:11 -0700334using webrtc::MediaStream;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000335using webrtc::MediaStreamInterface;
336using webrtc::MediaStreamTrackInterface;
337using webrtc::MockCreateSessionDescriptionObserver;
338using webrtc::MockDataChannelObserver;
Steve Anton94286cb2017-09-26 16:20:19 -0700339using webrtc::MockPeerConnectionObserver;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000340using webrtc::MockSetSessionDescriptionObserver;
341using webrtc::MockStatsObserver;
perkjd61bf802016-03-24 03:16:19 -0700342using webrtc::NotifierInterface;
343using webrtc::ObserverInterface;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000344using webrtc::PeerConnectionInterface;
345using webrtc::PeerConnectionObserver;
deadbeef293e9262017-01-11 12:28:30 -0800346using webrtc::RTCError;
347using webrtc::RTCErrorType;
deadbeefab9b2d12015-10-14 11:33:11 -0700348using webrtc::RtpReceiverInterface;
349using webrtc::RtpSenderInterface;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000350using webrtc::SdpParseError;
351using webrtc::SessionDescriptionInterface;
deadbeefab9b2d12015-10-14 11:33:11 -0700352using webrtc::StreamCollection;
353using webrtc::StreamCollectionInterface;
perkja3ede6c2016-03-08 01:27:48 +0100354using webrtc::VideoTrackSourceInterface;
deadbeefab9b2d12015-10-14 11:33:11 -0700355using webrtc::VideoTrack;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000356using webrtc::VideoTrackInterface;
357
deadbeefab9b2d12015-10-14 11:33:11 -0700358typedef PeerConnectionInterface::RTCOfferAnswerOptions RTCOfferAnswerOptions;
359
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000360namespace {
361
362// Gets the first ssrc of given content type from the ContentInfo.
363bool GetFirstSsrc(const cricket::ContentInfo* content_info, int* ssrc) {
364 if (!content_info || !ssrc) {
365 return false;
366 }
367 const cricket::MediaContentDescription* media_desc =
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000368 static_cast<const cricket::MediaContentDescription*>(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000369 content_info->description);
370 if (!media_desc || media_desc->streams().empty()) {
371 return false;
372 }
373 *ssrc = media_desc->streams().begin()->first_ssrc();
374 return true;
375}
376
deadbeefd1a38b52016-12-10 13:15:33 -0800377// Get the ufrags out of an SDP blob. Useful for testing ICE restart
378// behavior.
379std::vector<std::string> GetUfrags(
380 const webrtc::SessionDescriptionInterface* desc) {
381 std::vector<std::string> ufrags;
382 for (const cricket::TransportInfo& info :
383 desc->description()->transport_infos()) {
384 ufrags.push_back(info.description.ice_ufrag);
385 }
386 return ufrags;
387}
388
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000389void SetSsrcToZero(std::string* sdp) {
390 const char kSdpSsrcAtribute[] = "a=ssrc:";
391 const char kSdpSsrcAtributeZero[] = "a=ssrc:0";
392 size_t ssrc_pos = 0;
393 while ((ssrc_pos = sdp->find(kSdpSsrcAtribute, ssrc_pos)) !=
394 std::string::npos) {
395 size_t end_ssrc = sdp->find(" ", ssrc_pos);
396 sdp->replace(ssrc_pos, end_ssrc - ssrc_pos, kSdpSsrcAtributeZero);
397 ssrc_pos = end_ssrc;
398 }
399}
400
deadbeefab9b2d12015-10-14 11:33:11 -0700401// Check if |streams| contains the specified track.
402bool ContainsTrack(const std::vector<cricket::StreamParams>& streams,
403 const std::string& stream_label,
404 const std::string& track_id) {
405 for (const cricket::StreamParams& params : streams) {
406 if (params.sync_label == stream_label && params.id == track_id) {
407 return true;
408 }
409 }
410 return false;
411}
412
413// Check if |senders| contains the specified sender, by id.
414bool ContainsSender(
415 const std::vector<rtc::scoped_refptr<RtpSenderInterface>>& senders,
416 const std::string& id) {
417 for (const auto& sender : senders) {
418 if (sender->id() == id) {
419 return true;
420 }
421 }
422 return false;
423}
424
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700425// Check if |senders| contains the specified sender, by id and stream id.
426bool ContainsSender(
427 const std::vector<rtc::scoped_refptr<RtpSenderInterface>>& senders,
428 const std::string& id,
429 const std::string& stream_id) {
430 for (const auto& sender : senders) {
deadbeefa601f5c2016-06-06 14:27:39 -0700431 if (sender->id() == id && sender->stream_ids()[0] == stream_id) {
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700432 return true;
433 }
434 }
435 return false;
436}
437
deadbeefab9b2d12015-10-14 11:33:11 -0700438// Create a collection of streams.
439// CreateStreamCollection(1) creates a collection that
440// correspond to kSdpStringWithStream1.
441// CreateStreamCollection(2) correspond to kSdpStringWithStream1And2.
442rtc::scoped_refptr<StreamCollection> CreateStreamCollection(
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700443 int number_of_streams,
444 int tracks_per_stream) {
deadbeefab9b2d12015-10-14 11:33:11 -0700445 rtc::scoped_refptr<StreamCollection> local_collection(
446 StreamCollection::Create());
447
448 for (int i = 0; i < number_of_streams; ++i) {
449 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream(
450 webrtc::MediaStream::Create(kStreams[i]));
451
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700452 for (int j = 0; j < tracks_per_stream; ++j) {
453 // Add a local audio track.
454 rtc::scoped_refptr<webrtc::AudioTrackInterface> audio_track(
455 webrtc::AudioTrack::Create(kAudioTracks[i * tracks_per_stream + j],
456 nullptr));
457 stream->AddTrack(audio_track);
deadbeefab9b2d12015-10-14 11:33:11 -0700458
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700459 // Add a local video track.
460 rtc::scoped_refptr<webrtc::VideoTrackInterface> video_track(
461 webrtc::VideoTrack::Create(kVideoTracks[i * tracks_per_stream + j],
perkj773be362017-07-31 23:22:01 -0700462 webrtc::FakeVideoTrackSource::Create(),
463 rtc::Thread::Current()));
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700464 stream->AddTrack(video_track);
465 }
deadbeefab9b2d12015-10-14 11:33:11 -0700466
467 local_collection->AddStream(stream);
468 }
469 return local_collection;
470}
471
472// Check equality of StreamCollections.
473bool CompareStreamCollections(StreamCollectionInterface* s1,
474 StreamCollectionInterface* s2) {
475 if (s1 == nullptr || s2 == nullptr || s1->count() != s2->count()) {
476 return false;
477 }
478
479 for (size_t i = 0; i != s1->count(); ++i) {
480 if (s1->at(i)->label() != s2->at(i)->label()) {
481 return false;
482 }
483 webrtc::AudioTrackVector audio_tracks1 = s1->at(i)->GetAudioTracks();
484 webrtc::AudioTrackVector audio_tracks2 = s2->at(i)->GetAudioTracks();
485 webrtc::VideoTrackVector video_tracks1 = s1->at(i)->GetVideoTracks();
486 webrtc::VideoTrackVector video_tracks2 = s2->at(i)->GetVideoTracks();
487
488 if (audio_tracks1.size() != audio_tracks2.size()) {
489 return false;
490 }
491 for (size_t j = 0; j != audio_tracks1.size(); ++j) {
492 if (audio_tracks1[j]->id() != audio_tracks2[j]->id()) {
493 return false;
494 }
495 }
496 if (video_tracks1.size() != video_tracks2.size()) {
497 return false;
498 }
499 for (size_t j = 0; j != video_tracks1.size(); ++j) {
500 if (video_tracks1[j]->id() != video_tracks2[j]->id()) {
501 return false;
502 }
503 }
504 }
505 return true;
506}
507
perkjd61bf802016-03-24 03:16:19 -0700508// Helper class to test Observer.
509class MockTrackObserver : public ObserverInterface {
510 public:
511 explicit MockTrackObserver(NotifierInterface* notifier)
512 : notifier_(notifier) {
513 notifier_->RegisterObserver(this);
514 }
515
516 ~MockTrackObserver() { Unregister(); }
517
518 void Unregister() {
519 if (notifier_) {
520 notifier_->UnregisterObserver(this);
521 notifier_ = nullptr;
522 }
523 }
524
525 MOCK_METHOD0(OnChanged, void());
526
527 private:
528 NotifierInterface* notifier_;
529};
530
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000531} // namespace
deadbeefab9b2d12015-10-14 11:33:11 -0700532
nisse528b7932017-05-08 03:21:43 -0700533// The PeerConnectionMediaConfig tests below verify that configuration and
534// constraints are propagated into the PeerConnection's MediaConfig. These
535// settings are intended for MediaChannel constructors, but that is not
536// exercised by these unittest.
zhihuang29ff8442016-07-27 11:07:25 -0700537class PeerConnectionFactoryForTest : public webrtc::PeerConnectionFactory {
538 public:
zhihuang38ede132017-06-15 12:52:32 -0700539 static rtc::scoped_refptr<PeerConnectionFactoryForTest>
540 CreatePeerConnectionFactoryForTest() {
541 auto audio_encoder_factory = webrtc::CreateBuiltinAudioEncoderFactory();
542 auto audio_decoder_factory = webrtc::CreateBuiltinAudioDecoderFactory();
543
544 auto media_engine = std::unique_ptr<cricket::MediaEngineInterface>(
545 cricket::WebRtcMediaEngineFactory::Create(
546 nullptr, audio_encoder_factory, audio_decoder_factory, nullptr,
peaha9cc40b2017-06-29 08:32:09 -0700547 nullptr, nullptr, webrtc::AudioProcessing::Create()));
zhihuang38ede132017-06-15 12:52:32 -0700548
549 std::unique_ptr<webrtc::CallFactoryInterface> call_factory =
550 webrtc::CreateCallFactory();
551
552 std::unique_ptr<webrtc::RtcEventLogFactoryInterface> event_log_factory =
553 webrtc::CreateRtcEventLogFactory();
554
555 return new rtc::RefCountedObject<PeerConnectionFactoryForTest>(
556 rtc::Thread::Current(), rtc::Thread::Current(), rtc::Thread::Current(),
Magnus Jedvert02e7a192017-09-23 17:21:32 +0200557 std::move(media_engine), std::move(call_factory),
zhihuang38ede132017-06-15 12:52:32 -0700558 std::move(event_log_factory));
559 }
560
561 PeerConnectionFactoryForTest(
562 rtc::Thread* network_thread,
563 rtc::Thread* worker_thread,
564 rtc::Thread* signaling_thread,
zhihuang38ede132017-06-15 12:52:32 -0700565 std::unique_ptr<cricket::MediaEngineInterface> media_engine,
566 std::unique_ptr<webrtc::CallFactoryInterface> call_factory,
567 std::unique_ptr<webrtc::RtcEventLogFactoryInterface> event_log_factory)
568 : webrtc::PeerConnectionFactory(network_thread,
569 worker_thread,
570 signaling_thread,
zhihuang38ede132017-06-15 12:52:32 -0700571 std::move(media_engine),
572 std::move(call_factory),
573 std::move(event_log_factory)) {}
kwiberg1e4e8cb2017-01-31 01:48:08 -0800574
zhihuang29ff8442016-07-27 11:07:25 -0700575 cricket::TransportController* CreateTransportController(
Honghai Zhangbfd398c2016-08-30 22:07:42 -0700576 cricket::PortAllocator* port_allocator,
577 bool redetermine_role_on_ice_restart) override {
zhihuang29ff8442016-07-27 11:07:25 -0700578 transport_controller = new cricket::TransportController(
Honghai Zhangbfd398c2016-08-30 22:07:42 -0700579 rtc::Thread::Current(), rtc::Thread::Current(), port_allocator,
deadbeef7914b8c2017-04-21 03:23:33 -0700580 redetermine_role_on_ice_restart, rtc::CryptoOptions());
zhihuang29ff8442016-07-27 11:07:25 -0700581 return transport_controller;
582 }
583
deadbeefd7850b22017-08-23 10:59:19 -0700584 rtc::scoped_refptr<FakeAudioCaptureModule> fake_audio_capture_module_;
zhihuang29ff8442016-07-27 11:07:25 -0700585 cricket::TransportController* transport_controller;
zhihuang29ff8442016-07-27 11:07:25 -0700586};
587
Steve Anton94286cb2017-09-26 16:20:19 -0700588// TODO(steveanton): Convert to use the new PeerConnectionUnitTestFixture and
589// PeerConnectionWrapper.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000590class PeerConnectionInterfaceTest : public testing::Test {
591 protected:
deadbeef9a6f4d42017-05-15 19:43:33 -0700592 PeerConnectionInterfaceTest()
deadbeef98e186c2017-05-16 18:00:06 -0700593 : vss_(new rtc::VirtualSocketServer()), main_(vss_.get()) {
phoglund37ebcf02016-01-08 05:04:57 -0800594#ifdef WEBRTC_ANDROID
595 webrtc::InitializeAndroidObjects();
596#endif
597 }
598
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000599 virtual void SetUp() {
deadbeefd7850b22017-08-23 10:59:19 -0700600 // Use fake audio capture module since we're only testing the interface
601 // level, and using a real one could make tests flaky when run in parallel.
602 fake_audio_capture_module_ = FakeAudioCaptureModule::Create();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000603 pc_factory_ = webrtc::CreatePeerConnectionFactory(
danilchape9021a32016-05-17 01:52:02 -0700604 rtc::Thread::Current(), rtc::Thread::Current(), rtc::Thread::Current(),
deadbeefd7850b22017-08-23 10:59:19 -0700605 fake_audio_capture_module_, nullptr, nullptr);
danilchape9021a32016-05-17 01:52:02 -0700606 ASSERT_TRUE(pc_factory_);
zhihuang29ff8442016-07-27 11:07:25 -0700607 pc_factory_for_test_ =
zhihuang38ede132017-06-15 12:52:32 -0700608 PeerConnectionFactoryForTest::CreatePeerConnectionFactoryForTest();
zhihuang29ff8442016-07-27 11:07:25 -0700609 pc_factory_for_test_->Initialize();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000610 }
611
612 void CreatePeerConnection() {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700613 CreatePeerConnection(PeerConnectionInterface::RTCConfiguration(), nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000614 }
615
deadbeef293e9262017-01-11 12:28:30 -0800616 // DTLS does not work in a loopback call, so is disabled for most of the
617 // tests in this file.
618 void CreatePeerConnectionWithoutDtls() {
619 FakeConstraints no_dtls_constraints;
620 no_dtls_constraints.AddMandatory(
621 webrtc::MediaConstraintsInterface::kEnableDtlsSrtp, false);
622
623 CreatePeerConnection(PeerConnectionInterface::RTCConfiguration(),
624 &no_dtls_constraints);
625 }
626
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000627 void CreatePeerConnection(webrtc::MediaConstraintsInterface* constraints) {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700628 CreatePeerConnection(PeerConnectionInterface::RTCConfiguration(),
629 constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000630 }
631
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700632 void CreatePeerConnectionWithIceTransportsType(
633 PeerConnectionInterface::IceTransportsType type) {
634 PeerConnectionInterface::RTCConfiguration config;
635 config.type = type;
636 return CreatePeerConnection(config, nullptr);
637 }
638
639 void CreatePeerConnectionWithIceServer(const std::string& uri,
640 const std::string& password) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800641 PeerConnectionInterface::RTCConfiguration config;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000642 PeerConnectionInterface::IceServer server;
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700643 server.uri = uri;
644 server.password = password;
645 config.servers.push_back(server);
646 CreatePeerConnection(config, nullptr);
647 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000648
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700649 void CreatePeerConnection(PeerConnectionInterface::RTCConfiguration config,
650 webrtc::MediaConstraintsInterface* constraints) {
kwibergd1fe2812016-04-27 06:47:29 -0700651 std::unique_ptr<cricket::FakePortAllocator> port_allocator(
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800652 new cricket::FakePortAllocator(rtc::Thread::Current(), nullptr));
653 port_allocator_ = port_allocator.get();
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000654
deadbeef1dcb1642017-03-29 21:08:16 -0700655 // Create certificate generator unless DTLS constraint is explicitly set to
656 // false.
Henrik Boströmd79599d2016-06-01 13:58:50 +0200657 std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator;
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000658 bool dtls;
659 if (FindConstraint(constraints,
660 webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
661 &dtls,
Henrik Boström5e56c592015-08-11 10:33:13 +0200662 nullptr) && dtls) {
deadbeef8662f942017-01-20 21:20:51 -0800663 fake_certificate_generator_ = new FakeRTCCertificateGenerator();
664 cert_generator.reset(fake_certificate_generator_);
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000665 }
Henrik Boströmd79599d2016-06-01 13:58:50 +0200666 pc_ = pc_factory_->CreatePeerConnection(
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800667 config, constraints, std::move(port_allocator),
Henrik Boströmd79599d2016-06-01 13:58:50 +0200668 std::move(cert_generator), &observer_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000669 ASSERT_TRUE(pc_.get() != NULL);
670 observer_.SetPeerConnectionInterface(pc_.get());
671 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
672 }
673
deadbeef0a6c4ca2015-10-06 11:38:28 -0700674 void CreatePeerConnectionExpectFail(const std::string& uri) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800675 PeerConnectionInterface::RTCConfiguration config;
deadbeef0a6c4ca2015-10-06 11:38:28 -0700676 PeerConnectionInterface::IceServer server;
deadbeef0a6c4ca2015-10-06 11:38:28 -0700677 server.uri = uri;
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800678 config.servers.push_back(server);
deadbeef0a6c4ca2015-10-06 11:38:28 -0700679
zhihuang9763d562016-08-05 11:14:50 -0700680 rtc::scoped_refptr<PeerConnectionInterface> pc;
hbosd7973cc2016-05-27 06:08:53 -0700681 pc = pc_factory_->CreatePeerConnection(config, nullptr, nullptr, nullptr,
682 &observer_);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800683 EXPECT_EQ(nullptr, pc);
deadbeef0a6c4ca2015-10-06 11:38:28 -0700684 }
685
Steve Anton038834f2017-07-14 15:59:59 -0700686 void CreatePeerConnectionExpectFail(
687 PeerConnectionInterface::RTCConfiguration config) {
688 PeerConnectionInterface::IceServer server;
689 server.uri = kTurnIceServerUri;
690 server.password = kTurnPassword;
691 config.servers.push_back(server);
692 rtc::scoped_refptr<PeerConnectionInterface> pc =
693 pc_factory_->CreatePeerConnection(config, nullptr, nullptr, &observer_);
694 EXPECT_EQ(nullptr, pc);
695 }
696
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000697 void CreatePeerConnectionWithDifferentConfigurations() {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700698 CreatePeerConnectionWithIceServer(kStunAddressOnly, "");
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800699 EXPECT_EQ(1u, port_allocator_->stun_servers().size());
700 EXPECT_EQ(0u, port_allocator_->turn_servers().size());
701 EXPECT_EQ("address", port_allocator_->stun_servers().begin()->hostname());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000702 EXPECT_EQ(kDefaultStunPort,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800703 port_allocator_->stun_servers().begin()->port());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000704
deadbeef0a6c4ca2015-10-06 11:38:28 -0700705 CreatePeerConnectionExpectFail(kStunInvalidPort);
706 CreatePeerConnectionExpectFail(kStunAddressPortAndMore1);
707 CreatePeerConnectionExpectFail(kStunAddressPortAndMore2);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000708
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700709 CreatePeerConnectionWithIceServer(kTurnIceServerUri, kTurnPassword);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800710 EXPECT_EQ(0u, port_allocator_->stun_servers().size());
711 EXPECT_EQ(1u, port_allocator_->turn_servers().size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000712 EXPECT_EQ(kTurnUsername,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800713 port_allocator_->turn_servers()[0].credentials.username);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000714 EXPECT_EQ(kTurnPassword,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800715 port_allocator_->turn_servers()[0].credentials.password);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000716 EXPECT_EQ(kTurnHostname,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800717 port_allocator_->turn_servers()[0].ports[0].address.hostname());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000718 }
719
720 void ReleasePeerConnection() {
721 pc_ = NULL;
722 observer_.SetPeerConnectionInterface(NULL);
723 }
724
deadbeefab9b2d12015-10-14 11:33:11 -0700725 void AddVideoStream(const std::string& label) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000726 // Create a local stream.
zhihuang9763d562016-08-05 11:14:50 -0700727 rtc::scoped_refptr<MediaStreamInterface> stream(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000728 pc_factory_->CreateLocalMediaStream(label));
zhihuang9763d562016-08-05 11:14:50 -0700729 rtc::scoped_refptr<VideoTrackSourceInterface> video_source(
deadbeef112b2e92017-02-10 20:13:37 -0800730 pc_factory_->CreateVideoSource(std::unique_ptr<cricket::VideoCapturer>(
731 new cricket::FakeVideoCapturer()),
732 NULL));
zhihuang9763d562016-08-05 11:14:50 -0700733 rtc::scoped_refptr<VideoTrackInterface> video_track(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000734 pc_factory_->CreateVideoTrack(label + "v0", video_source));
735 stream->AddTrack(video_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +0000736 EXPECT_TRUE(pc_->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000737 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
738 observer_.renegotiation_needed_ = false;
739 }
740
741 void AddVoiceStream(const std::string& label) {
742 // Create a local stream.
zhihuang9763d562016-08-05 11:14:50 -0700743 rtc::scoped_refptr<MediaStreamInterface> stream(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000744 pc_factory_->CreateLocalMediaStream(label));
zhihuang9763d562016-08-05 11:14:50 -0700745 rtc::scoped_refptr<AudioTrackInterface> audio_track(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000746 pc_factory_->CreateAudioTrack(label + "a0", NULL));
747 stream->AddTrack(audio_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +0000748 EXPECT_TRUE(pc_->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000749 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
750 observer_.renegotiation_needed_ = false;
751 }
752
753 void AddAudioVideoStream(const std::string& stream_label,
754 const std::string& audio_track_label,
755 const std::string& video_track_label) {
756 // Create a local stream.
zhihuang9763d562016-08-05 11:14:50 -0700757 rtc::scoped_refptr<MediaStreamInterface> stream(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000758 pc_factory_->CreateLocalMediaStream(stream_label));
zhihuang9763d562016-08-05 11:14:50 -0700759 rtc::scoped_refptr<AudioTrackInterface> audio_track(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000760 pc_factory_->CreateAudioTrack(
761 audio_track_label, static_cast<AudioSourceInterface*>(NULL)));
762 stream->AddTrack(audio_track.get());
zhihuang9763d562016-08-05 11:14:50 -0700763 rtc::scoped_refptr<VideoTrackInterface> video_track(
nisseaf510af2016-03-21 08:20:42 -0700764 pc_factory_->CreateVideoTrack(
deadbeef112b2e92017-02-10 20:13:37 -0800765 video_track_label, pc_factory_->CreateVideoSource(
766 std::unique_ptr<cricket::VideoCapturer>(
767 new cricket::FakeVideoCapturer()))));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000768 stream->AddTrack(video_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +0000769 EXPECT_TRUE(pc_->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000770 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
771 observer_.renegotiation_needed_ = false;
772 }
773
kwibergd1fe2812016-04-27 06:47:29 -0700774 bool DoCreateOfferAnswer(std::unique_ptr<SessionDescriptionInterface>* desc,
deadbeefc80741f2015-10-22 13:14:45 -0700775 bool offer,
776 MediaConstraintsInterface* constraints) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000777 rtc::scoped_refptr<MockCreateSessionDescriptionObserver>
778 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000779 MockCreateSessionDescriptionObserver>());
780 if (offer) {
deadbeefc80741f2015-10-22 13:14:45 -0700781 pc_->CreateOffer(observer, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000782 } else {
deadbeefc80741f2015-10-22 13:14:45 -0700783 pc_->CreateAnswer(observer, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000784 }
785 EXPECT_EQ_WAIT(true, observer->called(), kTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -0700786 *desc = observer->MoveDescription();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000787 return observer->result();
788 }
789
kwibergd1fe2812016-04-27 06:47:29 -0700790 bool DoCreateOffer(std::unique_ptr<SessionDescriptionInterface>* desc,
deadbeefc80741f2015-10-22 13:14:45 -0700791 MediaConstraintsInterface* constraints) {
792 return DoCreateOfferAnswer(desc, true, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000793 }
794
kwibergd1fe2812016-04-27 06:47:29 -0700795 bool DoCreateAnswer(std::unique_ptr<SessionDescriptionInterface>* desc,
deadbeefc80741f2015-10-22 13:14:45 -0700796 MediaConstraintsInterface* constraints) {
797 return DoCreateOfferAnswer(desc, false, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000798 }
799
Steve Antondb45ca82017-09-11 18:27:34 -0700800 bool DoSetSessionDescription(
801 std::unique_ptr<SessionDescriptionInterface> desc,
802 bool local) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000803 rtc::scoped_refptr<MockSetSessionDescriptionObserver>
804 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000805 MockSetSessionDescriptionObserver>());
806 if (local) {
Steve Antondb45ca82017-09-11 18:27:34 -0700807 pc_->SetLocalDescription(observer, desc.release());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000808 } else {
Steve Antondb45ca82017-09-11 18:27:34 -0700809 pc_->SetRemoteDescription(observer, desc.release());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000810 }
zhihuang29ff8442016-07-27 11:07:25 -0700811 if (pc_->signaling_state() != PeerConnectionInterface::kClosed) {
812 EXPECT_EQ_WAIT(true, observer->called(), kTimeout);
813 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000814 return observer->result();
815 }
816
Steve Antondb45ca82017-09-11 18:27:34 -0700817 bool DoSetLocalDescription(
818 std::unique_ptr<SessionDescriptionInterface> desc) {
819 return DoSetSessionDescription(std::move(desc), true);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000820 }
821
Steve Antondb45ca82017-09-11 18:27:34 -0700822 bool DoSetRemoteDescription(
823 std::unique_ptr<SessionDescriptionInterface> desc) {
824 return DoSetSessionDescription(std::move(desc), false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000825 }
826
827 // Calls PeerConnection::GetStats and check the return value.
828 // It does not verify the values in the StatReports since a RTCP packet might
829 // be required.
830 bool DoGetStats(MediaStreamTrackInterface* track) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000831 rtc::scoped_refptr<MockStatsObserver> observer(
832 new rtc::RefCountedObject<MockStatsObserver>());
jiayl@webrtc.orgdb41b4d2014-03-03 21:30:06 +0000833 if (!pc_->GetStats(
834 observer, track, PeerConnectionInterface::kStatsOutputLevelStandard))
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000835 return false;
836 EXPECT_TRUE_WAIT(observer->called(), kTimeout);
837 return observer->called();
838 }
839
840 void InitiateCall() {
deadbeef293e9262017-01-11 12:28:30 -0800841 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000842 // Create a local stream with audio&video tracks.
843 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
844 CreateOfferReceiveAnswer();
845 }
846
847 // Verify that RTP Header extensions has been negotiated for audio and video.
848 void VerifyRemoteRtpHeaderExtensions() {
849 const cricket::MediaContentDescription* desc =
850 cricket::GetFirstAudioContentDescription(
851 pc_->remote_description()->description());
852 ASSERT_TRUE(desc != NULL);
853 EXPECT_GT(desc->rtp_header_extensions().size(), 0u);
854
855 desc = cricket::GetFirstVideoContentDescription(
856 pc_->remote_description()->description());
857 ASSERT_TRUE(desc != NULL);
858 EXPECT_GT(desc->rtp_header_extensions().size(), 0u);
859 }
860
861 void CreateOfferAsRemoteDescription() {
kwibergd1fe2812016-04-27 06:47:29 -0700862 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -0700863 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000864 std::string sdp;
865 EXPECT_TRUE(offer->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -0700866 std::unique_ptr<SessionDescriptionInterface> remote_offer(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000867 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
Steve Antondb45ca82017-09-11 18:27:34 -0700868 sdp, nullptr));
869 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_offer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000870 EXPECT_EQ(PeerConnectionInterface::kHaveRemoteOffer, observer_.state_);
871 }
872
deadbeefab9b2d12015-10-14 11:33:11 -0700873 void CreateAndSetRemoteOffer(const std::string& sdp) {
Steve Antondb45ca82017-09-11 18:27:34 -0700874 std::unique_ptr<SessionDescriptionInterface> remote_offer(
deadbeefab9b2d12015-10-14 11:33:11 -0700875 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
Steve Antondb45ca82017-09-11 18:27:34 -0700876 sdp, nullptr));
877 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_offer)));
deadbeefab9b2d12015-10-14 11:33:11 -0700878 EXPECT_EQ(PeerConnectionInterface::kHaveRemoteOffer, observer_.state_);
879 }
880
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000881 void CreateAnswerAsLocalDescription() {
kwibergd1fe2812016-04-27 06:47:29 -0700882 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -0700883 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000884
885 // TODO(perkj): Currently SetLocalDescription fails if any parameters in an
886 // audio codec change, even if the parameter has nothing to do with
887 // receiving. Not all parameters are serialized to SDP.
888 // Since CreatePrAnswerAsLocalDescription serialize/deserialize
889 // the SessionDescription, it is necessary to do that here to in order to
890 // get ReceiveOfferCreatePrAnswerAndAnswer and RenegotiateAudioOnly to pass.
891 // https://code.google.com/p/webrtc/issues/detail?id=1356
892 std::string sdp;
893 EXPECT_TRUE(answer->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -0700894 std::unique_ptr<SessionDescriptionInterface> new_answer(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000895 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
Steve Antondb45ca82017-09-11 18:27:34 -0700896 sdp, nullptr));
897 EXPECT_TRUE(DoSetLocalDescription(std::move(new_answer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000898 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
899 }
900
901 void CreatePrAnswerAsLocalDescription() {
kwibergd1fe2812016-04-27 06:47:29 -0700902 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -0700903 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000904
905 std::string sdp;
906 EXPECT_TRUE(answer->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -0700907 std::unique_ptr<SessionDescriptionInterface> pr_answer(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000908 webrtc::CreateSessionDescription(SessionDescriptionInterface::kPrAnswer,
Steve Antondb45ca82017-09-11 18:27:34 -0700909 sdp, nullptr));
910 EXPECT_TRUE(DoSetLocalDescription(std::move(pr_answer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000911 EXPECT_EQ(PeerConnectionInterface::kHaveLocalPrAnswer, observer_.state_);
912 }
913
914 void CreateOfferReceiveAnswer() {
915 CreateOfferAsLocalDescription();
916 std::string sdp;
917 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
918 CreateAnswerAsRemoteDescription(sdp);
919 }
920
921 void CreateOfferAsLocalDescription() {
kwibergd1fe2812016-04-27 06:47:29 -0700922 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -0700923 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000924 // TODO(perkj): Currently SetLocalDescription fails if any parameters in an
925 // audio codec change, even if the parameter has nothing to do with
926 // receiving. Not all parameters are serialized to SDP.
927 // Since CreatePrAnswerAsLocalDescription serialize/deserialize
928 // the SessionDescription, it is necessary to do that here to in order to
929 // get ReceiveOfferCreatePrAnswerAndAnswer and RenegotiateAudioOnly to pass.
930 // https://code.google.com/p/webrtc/issues/detail?id=1356
931 std::string sdp;
932 EXPECT_TRUE(offer->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -0700933 std::unique_ptr<SessionDescriptionInterface> new_offer(
934 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
935 sdp, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000936
Steve Antondb45ca82017-09-11 18:27:34 -0700937 EXPECT_TRUE(DoSetLocalDescription(std::move(new_offer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000938 EXPECT_EQ(PeerConnectionInterface::kHaveLocalOffer, observer_.state_);
mallinath@webrtc.org68cbd012014-01-22 00:16:46 +0000939 // Wait for the ice_complete message, so that SDP will have candidates.
940 EXPECT_TRUE_WAIT(observer_.ice_complete_, kTimeout);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000941 }
942
deadbeefab9b2d12015-10-14 11:33:11 -0700943 void CreateAnswerAsRemoteDescription(const std::string& sdp) {
Steve Antondb45ca82017-09-11 18:27:34 -0700944 std::unique_ptr<SessionDescriptionInterface> answer(
945 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
946 sdp, nullptr));
947 ASSERT_TRUE(answer);
948 EXPECT_TRUE(DoSetRemoteDescription(std::move(answer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000949 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
950 }
951
deadbeefab9b2d12015-10-14 11:33:11 -0700952 void CreatePrAnswerAndAnswerAsRemoteDescription(const std::string& sdp) {
Steve Antondb45ca82017-09-11 18:27:34 -0700953 std::unique_ptr<SessionDescriptionInterface> pr_answer(
954 webrtc::CreateSessionDescription(SessionDescriptionInterface::kPrAnswer,
955 sdp, nullptr));
956 ASSERT_TRUE(pr_answer);
957 EXPECT_TRUE(DoSetRemoteDescription(std::move(pr_answer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000958 EXPECT_EQ(PeerConnectionInterface::kHaveRemotePrAnswer, observer_.state_);
Steve Antondb45ca82017-09-11 18:27:34 -0700959 std::unique_ptr<SessionDescriptionInterface> answer(
960 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
961 sdp, nullptr));
962 ASSERT_TRUE(answer);
963 EXPECT_TRUE(DoSetRemoteDescription(std::move(answer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000964 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
965 }
966
967 // Help function used for waiting until a the last signaled remote stream has
968 // the same label as |stream_label|. In a few of the tests in this file we
969 // answer with the same session description as we offer and thus we can
970 // check if OnAddStream have been called with the same stream as we offer to
971 // send.
972 void WaitAndVerifyOnAddStream(const std::string& stream_label) {
973 EXPECT_EQ_WAIT(stream_label, observer_.GetLastAddedStreamLabel(), kTimeout);
974 }
975
976 // Creates an offer and applies it as a local session description.
977 // Creates an answer with the same SDP an the offer but removes all lines
978 // that start with a:ssrc"
979 void CreateOfferReceiveAnswerWithoutSsrc() {
980 CreateOfferAsLocalDescription();
981 std::string sdp;
982 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
983 SetSsrcToZero(&sdp);
984 CreateAnswerAsRemoteDescription(sdp);
985 }
986
deadbeefab9b2d12015-10-14 11:33:11 -0700987 // This function creates a MediaStream with label kStreams[0] and
988 // |number_of_audio_tracks| and |number_of_video_tracks| tracks and the
989 // corresponding SessionDescriptionInterface. The SessionDescriptionInterface
kwiberg2bbff992016-03-16 11:03:04 -0700990 // is returned and the MediaStream is stored in
deadbeefab9b2d12015-10-14 11:33:11 -0700991 // |reference_collection_|
kwibergd1fe2812016-04-27 06:47:29 -0700992 std::unique_ptr<SessionDescriptionInterface>
kwiberg2bbff992016-03-16 11:03:04 -0700993 CreateSessionDescriptionAndReference(size_t number_of_audio_tracks,
994 size_t number_of_video_tracks) {
995 EXPECT_LE(number_of_audio_tracks, 2u);
996 EXPECT_LE(number_of_video_tracks, 2u);
deadbeefab9b2d12015-10-14 11:33:11 -0700997
998 reference_collection_ = StreamCollection::Create();
999 std::string sdp_ms1 = std::string(kSdpStringInit);
1000
1001 std::string mediastream_label = kStreams[0];
1002
1003 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream(
1004 webrtc::MediaStream::Create(mediastream_label));
1005 reference_collection_->AddStream(stream);
1006
1007 if (number_of_audio_tracks > 0) {
1008 sdp_ms1 += std::string(kSdpStringAudio);
1009 sdp_ms1 += std::string(kSdpStringMs1Audio0);
1010 AddAudioTrack(kAudioTracks[0], stream);
1011 }
1012 if (number_of_audio_tracks > 1) {
1013 sdp_ms1 += kSdpStringMs1Audio1;
1014 AddAudioTrack(kAudioTracks[1], stream);
1015 }
1016
1017 if (number_of_video_tracks > 0) {
1018 sdp_ms1 += std::string(kSdpStringVideo);
1019 sdp_ms1 += std::string(kSdpStringMs1Video0);
1020 AddVideoTrack(kVideoTracks[0], stream);
1021 }
1022 if (number_of_video_tracks > 1) {
1023 sdp_ms1 += kSdpStringMs1Video1;
1024 AddVideoTrack(kVideoTracks[1], stream);
1025 }
1026
kwibergd1fe2812016-04-27 06:47:29 -07001027 return std::unique_ptr<SessionDescriptionInterface>(
kwiberg2bbff992016-03-16 11:03:04 -07001028 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
1029 sdp_ms1, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07001030 }
1031
1032 void AddAudioTrack(const std::string& track_id,
1033 MediaStreamInterface* stream) {
1034 rtc::scoped_refptr<webrtc::AudioTrackInterface> audio_track(
1035 webrtc::AudioTrack::Create(track_id, nullptr));
1036 ASSERT_TRUE(stream->AddTrack(audio_track));
1037 }
1038
1039 void AddVideoTrack(const std::string& track_id,
1040 MediaStreamInterface* stream) {
1041 rtc::scoped_refptr<webrtc::VideoTrackInterface> video_track(
nisseaf510af2016-03-21 08:20:42 -07001042 webrtc::VideoTrack::Create(track_id,
perkj773be362017-07-31 23:22:01 -07001043 webrtc::FakeVideoTrackSource::Create(),
1044 rtc::Thread::Current()));
deadbeefab9b2d12015-10-14 11:33:11 -07001045 ASSERT_TRUE(stream->AddTrack(video_track));
1046 }
1047
kwibergfd8be342016-05-14 19:44:11 -07001048 std::unique_ptr<SessionDescriptionInterface> CreateOfferWithOneAudioStream() {
deadbeef293e9262017-01-11 12:28:30 -08001049 CreatePeerConnectionWithoutDtls();
zhihuang8f65cdf2016-05-06 18:40:30 -07001050 AddVoiceStream(kStreamLabel1);
kwibergfd8be342016-05-14 19:44:11 -07001051 std::unique_ptr<SessionDescriptionInterface> offer;
zhihuang8f65cdf2016-05-06 18:40:30 -07001052 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
1053 return offer;
1054 }
1055
kwibergfd8be342016-05-14 19:44:11 -07001056 std::unique_ptr<SessionDescriptionInterface>
zhihuang8f65cdf2016-05-06 18:40:30 -07001057 CreateAnswerWithOneAudioStream() {
Steve Antondb45ca82017-09-11 18:27:34 -07001058 EXPECT_TRUE(DoSetRemoteDescription(CreateOfferWithOneAudioStream()));
kwibergfd8be342016-05-14 19:44:11 -07001059 std::unique_ptr<SessionDescriptionInterface> answer;
zhihuang8f65cdf2016-05-06 18:40:30 -07001060 EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
1061 return answer;
1062 }
1063
1064 const std::string& GetFirstAudioStreamCname(
1065 const SessionDescriptionInterface* desc) {
1066 const cricket::ContentInfo* audio_content =
1067 cricket::GetFirstAudioContent(desc->description());
1068 const cricket::AudioContentDescription* audio_desc =
1069 static_cast<const cricket::AudioContentDescription*>(
1070 audio_content->description);
1071 return audio_desc->streams()[0].cname;
1072 }
1073
zhihuang1c378ed2017-08-17 14:10:50 -07001074 std::unique_ptr<SessionDescriptionInterface> CreateOfferWithOptions(
1075 const RTCOfferAnswerOptions& offer_answer_options) {
1076 RTC_DCHECK(pc_);
1077 rtc::scoped_refptr<MockCreateSessionDescriptionObserver> observer(
1078 new rtc::RefCountedObject<MockCreateSessionDescriptionObserver>());
1079 pc_->CreateOffer(observer, offer_answer_options);
1080 EXPECT_EQ_WAIT(true, observer->called(), kTimeout);
1081 return observer->MoveDescription();
1082 }
1083
1084 void CreateOfferWithOptionsAsRemoteDescription(
1085 std::unique_ptr<SessionDescriptionInterface>* desc,
1086 const RTCOfferAnswerOptions& offer_answer_options) {
1087 *desc = CreateOfferWithOptions(offer_answer_options);
1088 ASSERT_TRUE(desc != nullptr);
1089 std::string sdp;
1090 EXPECT_TRUE((*desc)->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07001091 std::unique_ptr<SessionDescriptionInterface> remote_offer(
zhihuang1c378ed2017-08-17 14:10:50 -07001092 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
Steve Antondb45ca82017-09-11 18:27:34 -07001093 sdp, nullptr));
1094 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_offer)));
zhihuang1c378ed2017-08-17 14:10:50 -07001095 EXPECT_EQ(PeerConnectionInterface::kHaveRemoteOffer, observer_.state_);
1096 }
1097
1098 void CreateOfferWithOptionsAsLocalDescription(
1099 std::unique_ptr<SessionDescriptionInterface>* desc,
1100 const RTCOfferAnswerOptions& offer_answer_options) {
1101 *desc = CreateOfferWithOptions(offer_answer_options);
1102 ASSERT_TRUE(desc != nullptr);
1103 std::string sdp;
1104 EXPECT_TRUE((*desc)->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07001105 std::unique_ptr<SessionDescriptionInterface> new_offer(
1106 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
1107 sdp, nullptr));
zhihuang1c378ed2017-08-17 14:10:50 -07001108
Steve Antondb45ca82017-09-11 18:27:34 -07001109 EXPECT_TRUE(DoSetLocalDescription(std::move(new_offer)));
zhihuang1c378ed2017-08-17 14:10:50 -07001110 EXPECT_EQ(PeerConnectionInterface::kHaveLocalOffer, observer_.state_);
1111 }
1112
1113 bool HasCNCodecs(const cricket::ContentInfo* content) {
1114 const cricket::ContentDescription* description = content->description;
1115 RTC_DCHECK(description);
1116 const cricket::AudioContentDescription* audio_content_desc =
1117 static_cast<const cricket::AudioContentDescription*>(description);
1118 RTC_DCHECK(audio_content_desc);
1119 for (size_t i = 0; i < audio_content_desc->codecs().size(); ++i) {
1120 if (audio_content_desc->codecs()[i].name == "CN")
1121 return true;
1122 }
1123 return false;
1124 }
1125
deadbeef9a6f4d42017-05-15 19:43:33 -07001126 std::unique_ptr<rtc::VirtualSocketServer> vss_;
1127 rtc::AutoSocketServerThread main_;
deadbeefd7850b22017-08-23 10:59:19 -07001128 rtc::scoped_refptr<FakeAudioCaptureModule> fake_audio_capture_module_;
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08001129 cricket::FakePortAllocator* port_allocator_ = nullptr;
deadbeef8662f942017-01-20 21:20:51 -08001130 FakeRTCCertificateGenerator* fake_certificate_generator_ = nullptr;
zhihuang9763d562016-08-05 11:14:50 -07001131 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface> pc_factory_;
1132 rtc::scoped_refptr<PeerConnectionFactoryForTest> pc_factory_for_test_;
1133 rtc::scoped_refptr<PeerConnectionInterface> pc_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001134 MockPeerConnectionObserver observer_;
deadbeefab9b2d12015-10-14 11:33:11 -07001135 rtc::scoped_refptr<StreamCollection> reference_collection_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001136};
1137
zhihuang29ff8442016-07-27 11:07:25 -07001138// Test that no callbacks on the PeerConnectionObserver are called after the
1139// PeerConnection is closed.
1140TEST_F(PeerConnectionInterfaceTest, CloseAndTestCallbackFunctions) {
zhihuang9763d562016-08-05 11:14:50 -07001141 rtc::scoped_refptr<PeerConnectionInterface> pc(
zhihuang29ff8442016-07-27 11:07:25 -07001142 pc_factory_for_test_->CreatePeerConnection(
1143 PeerConnectionInterface::RTCConfiguration(), nullptr, nullptr,
1144 nullptr, &observer_));
1145 observer_.SetPeerConnectionInterface(pc.get());
1146 pc->Close();
1147
1148 // No callbacks is expected to be called.
zhihuang81c3a032016-11-17 12:06:24 -08001149 observer_.callback_triggered_ = false;
zhihuang29ff8442016-07-27 11:07:25 -07001150 std::vector<cricket::Candidate> candidates;
1151 pc_factory_for_test_->transport_controller->SignalGatheringState(
1152 cricket::IceGatheringState{});
1153 pc_factory_for_test_->transport_controller->SignalCandidatesGathered(
1154 "", candidates);
1155 pc_factory_for_test_->transport_controller->SignalConnectionState(
1156 cricket::IceConnectionState{});
1157 pc_factory_for_test_->transport_controller->SignalCandidatesRemoved(
1158 candidates);
1159 pc_factory_for_test_->transport_controller->SignalReceiving(false);
zhihuang81c3a032016-11-17 12:06:24 -08001160 EXPECT_FALSE(observer_.callback_triggered_);
zhihuang29ff8442016-07-27 11:07:25 -07001161}
1162
zhihuang8f65cdf2016-05-06 18:40:30 -07001163// Generate different CNAMEs when PeerConnections are created.
1164// The CNAMEs are expected to be generated randomly. It is possible
1165// that the test fails, though the possibility is very low.
1166TEST_F(PeerConnectionInterfaceTest, CnameGenerationInOffer) {
kwibergfd8be342016-05-14 19:44:11 -07001167 std::unique_ptr<SessionDescriptionInterface> offer1 =
zhihuang8f65cdf2016-05-06 18:40:30 -07001168 CreateOfferWithOneAudioStream();
kwibergfd8be342016-05-14 19:44:11 -07001169 std::unique_ptr<SessionDescriptionInterface> offer2 =
zhihuang8f65cdf2016-05-06 18:40:30 -07001170 CreateOfferWithOneAudioStream();
1171 EXPECT_NE(GetFirstAudioStreamCname(offer1.get()),
1172 GetFirstAudioStreamCname(offer2.get()));
1173}
1174
1175TEST_F(PeerConnectionInterfaceTest, CnameGenerationInAnswer) {
kwibergfd8be342016-05-14 19:44:11 -07001176 std::unique_ptr<SessionDescriptionInterface> answer1 =
zhihuang8f65cdf2016-05-06 18:40:30 -07001177 CreateAnswerWithOneAudioStream();
kwibergfd8be342016-05-14 19:44:11 -07001178 std::unique_ptr<SessionDescriptionInterface> answer2 =
zhihuang8f65cdf2016-05-06 18:40:30 -07001179 CreateAnswerWithOneAudioStream();
1180 EXPECT_NE(GetFirstAudioStreamCname(answer1.get()),
1181 GetFirstAudioStreamCname(answer2.get()));
1182}
1183
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001184TEST_F(PeerConnectionInterfaceTest,
1185 CreatePeerConnectionWithDifferentConfigurations) {
1186 CreatePeerConnectionWithDifferentConfigurations();
1187}
1188
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001189TEST_F(PeerConnectionInterfaceTest,
1190 CreatePeerConnectionWithDifferentIceTransportsTypes) {
1191 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kNone);
1192 EXPECT_EQ(cricket::CF_NONE, port_allocator_->candidate_filter());
1193 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kRelay);
1194 EXPECT_EQ(cricket::CF_RELAY, port_allocator_->candidate_filter());
1195 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kNoHost);
1196 EXPECT_EQ(cricket::CF_ALL & ~cricket::CF_HOST,
1197 port_allocator_->candidate_filter());
1198 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kAll);
1199 EXPECT_EQ(cricket::CF_ALL, port_allocator_->candidate_filter());
1200}
1201
1202// Test that when a PeerConnection is created with a nonzero candidate pool
1203// size, the pooled PortAllocatorSession is created with all the attributes
1204// in the RTCConfiguration.
1205TEST_F(PeerConnectionInterfaceTest, CreatePeerConnectionWithPooledCandidates) {
1206 PeerConnectionInterface::RTCConfiguration config;
1207 PeerConnectionInterface::IceServer server;
1208 server.uri = kStunAddressOnly;
1209 config.servers.push_back(server);
1210 config.type = PeerConnectionInterface::kRelay;
1211 config.disable_ipv6 = true;
1212 config.tcp_candidate_policy =
1213 PeerConnectionInterface::kTcpCandidatePolicyDisabled;
honghaiz60347052016-05-31 18:29:12 -07001214 config.candidate_network_policy =
1215 PeerConnectionInterface::kCandidateNetworkPolicyLowCost;
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001216 config.ice_candidate_pool_size = 1;
1217 CreatePeerConnection(config, nullptr);
1218
1219 const cricket::FakePortAllocatorSession* session =
1220 static_cast<const cricket::FakePortAllocatorSession*>(
1221 port_allocator_->GetPooledSession());
1222 ASSERT_NE(nullptr, session);
1223 EXPECT_EQ(1UL, session->stun_servers().size());
1224 EXPECT_EQ(0U, session->flags() & cricket::PORTALLOCATOR_ENABLE_IPV6);
1225 EXPECT_LT(0U, session->flags() & cricket::PORTALLOCATOR_DISABLE_TCP);
honghaiz60347052016-05-31 18:29:12 -07001226 EXPECT_LT(0U,
1227 session->flags() & cricket::PORTALLOCATOR_DISABLE_COSTLY_NETWORKS);
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001228}
1229
deadbeefd21eab32017-07-26 16:50:11 -07001230// Test that network-related RTCConfiguration members are applied to the
1231// PortAllocator when CreatePeerConnection is called. Specifically:
1232// - disable_ipv6_on_wifi
1233// - max_ipv6_networks
1234// - tcp_candidate_policy
1235// - candidate_network_policy
1236// - prune_turn_ports
1237//
1238// Note that the candidate filter (RTCConfiguration::type) is already tested
1239// above.
1240TEST_F(PeerConnectionInterfaceTest,
1241 CreatePeerConnectionAppliesNetworkConfigToPortAllocator) {
1242 // Create fake port allocator.
1243 std::unique_ptr<cricket::FakePortAllocator> port_allocator(
1244 new cricket::FakePortAllocator(rtc::Thread::Current(), nullptr));
1245 cricket::FakePortAllocator* raw_port_allocator = port_allocator.get();
1246
1247 // Create RTCConfiguration with some network-related fields relevant to
1248 // PortAllocator populated.
1249 PeerConnectionInterface::RTCConfiguration config;
1250 config.disable_ipv6_on_wifi = true;
1251 config.max_ipv6_networks = 10;
1252 config.tcp_candidate_policy =
1253 PeerConnectionInterface::kTcpCandidatePolicyDisabled;
1254 config.candidate_network_policy =
1255 PeerConnectionInterface::kCandidateNetworkPolicyLowCost;
1256 config.prune_turn_ports = true;
1257
1258 // Create the PC factory and PC with the above config.
1259 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface> pc_factory(
1260 webrtc::CreatePeerConnectionFactory(
1261 rtc::Thread::Current(), rtc::Thread::Current(),
deadbeefd7850b22017-08-23 10:59:19 -07001262 rtc::Thread::Current(), fake_audio_capture_module_, nullptr,
1263 nullptr));
deadbeefd21eab32017-07-26 16:50:11 -07001264 rtc::scoped_refptr<PeerConnectionInterface> pc(
1265 pc_factory->CreatePeerConnection(
1266 config, nullptr, std::move(port_allocator), nullptr, &observer_));
1267
1268 // Now validate that the config fields set above were applied to the
1269 // PortAllocator, as flags or otherwise.
1270 EXPECT_FALSE(raw_port_allocator->flags() &
1271 cricket::PORTALLOCATOR_ENABLE_IPV6_ON_WIFI);
1272 EXPECT_EQ(10, raw_port_allocator->max_ipv6_networks());
1273 EXPECT_TRUE(raw_port_allocator->flags() & cricket::PORTALLOCATOR_DISABLE_TCP);
1274 EXPECT_TRUE(raw_port_allocator->flags() &
1275 cricket::PORTALLOCATOR_DISABLE_COSTLY_NETWORKS);
1276 EXPECT_TRUE(raw_port_allocator->prune_turn_ports());
1277}
1278
Taylor Brandstetterf8e65772016-06-27 17:20:15 -07001279// Test that the PeerConnection initializes the port allocator passed into it,
1280// and on the correct thread.
1281TEST_F(PeerConnectionInterfaceTest,
deadbeefd21eab32017-07-26 16:50:11 -07001282 CreatePeerConnectionInitializesPortAllocatorOnNetworkThread) {
tommie7251592017-07-14 14:44:46 -07001283 std::unique_ptr<rtc::Thread> network_thread(
1284 rtc::Thread::CreateWithSocketServer());
1285 network_thread->Start();
Taylor Brandstetterf8e65772016-06-27 17:20:15 -07001286 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface> pc_factory(
1287 webrtc::CreatePeerConnectionFactory(
tommie7251592017-07-14 14:44:46 -07001288 network_thread.get(), rtc::Thread::Current(), rtc::Thread::Current(),
deadbeefd7850b22017-08-23 10:59:19 -07001289 fake_audio_capture_module_, nullptr, nullptr));
Taylor Brandstetterf8e65772016-06-27 17:20:15 -07001290 std::unique_ptr<cricket::FakePortAllocator> port_allocator(
tommie7251592017-07-14 14:44:46 -07001291 new cricket::FakePortAllocator(network_thread.get(), nullptr));
Taylor Brandstetterf8e65772016-06-27 17:20:15 -07001292 cricket::FakePortAllocator* raw_port_allocator = port_allocator.get();
1293 PeerConnectionInterface::RTCConfiguration config;
1294 rtc::scoped_refptr<PeerConnectionInterface> pc(
1295 pc_factory->CreatePeerConnection(
1296 config, nullptr, std::move(port_allocator), nullptr, &observer_));
1297 // FakePortAllocator RTC_CHECKs that it's initialized on the right thread,
1298 // so all we have to do here is check that it's initialized.
1299 EXPECT_TRUE(raw_port_allocator->initialized());
1300}
1301
deadbeef46c73892016-11-16 19:42:04 -08001302// Check that GetConfiguration returns the configuration the PeerConnection was
1303// constructed with, before SetConfiguration is called.
1304TEST_F(PeerConnectionInterfaceTest, GetConfigurationAfterCreatePeerConnection) {
1305 PeerConnectionInterface::RTCConfiguration config;
1306 config.type = PeerConnectionInterface::kRelay;
1307 CreatePeerConnection(config, nullptr);
1308
1309 PeerConnectionInterface::RTCConfiguration returned_config =
1310 pc_->GetConfiguration();
1311 EXPECT_EQ(PeerConnectionInterface::kRelay, returned_config.type);
1312}
1313
1314// Check that GetConfiguration returns the last configuration passed into
1315// SetConfiguration.
1316TEST_F(PeerConnectionInterfaceTest, GetConfigurationAfterSetConfiguration) {
1317 CreatePeerConnection();
1318
1319 PeerConnectionInterface::RTCConfiguration config;
1320 config.type = PeerConnectionInterface::kRelay;
1321 EXPECT_TRUE(pc_->SetConfiguration(config));
1322
1323 PeerConnectionInterface::RTCConfiguration returned_config =
1324 pc_->GetConfiguration();
1325 EXPECT_EQ(PeerConnectionInterface::kRelay, returned_config.type);
1326}
1327
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001328TEST_F(PeerConnectionInterfaceTest, AddStreams) {
deadbeef293e9262017-01-11 12:28:30 -08001329 CreatePeerConnectionWithoutDtls();
deadbeefab9b2d12015-10-14 11:33:11 -07001330 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001331 AddVoiceStream(kStreamLabel2);
1332 ASSERT_EQ(2u, pc_->local_streams()->count());
1333
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001334 // Test we can add multiple local streams to one peerconnection.
zhihuang9763d562016-08-05 11:14:50 -07001335 rtc::scoped_refptr<MediaStreamInterface> stream(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001336 pc_factory_->CreateLocalMediaStream(kStreamLabel3));
zhihuang9763d562016-08-05 11:14:50 -07001337 rtc::scoped_refptr<AudioTrackInterface> audio_track(
1338 pc_factory_->CreateAudioTrack(kStreamLabel3,
1339 static_cast<AudioSourceInterface*>(NULL)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001340 stream->AddTrack(audio_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +00001341 EXPECT_TRUE(pc_->AddStream(stream));
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001342 EXPECT_EQ(3u, pc_->local_streams()->count());
1343
1344 // Remove the third stream.
1345 pc_->RemoveStream(pc_->local_streams()->at(2));
1346 EXPECT_EQ(2u, pc_->local_streams()->count());
1347
1348 // Remove the second stream.
1349 pc_->RemoveStream(pc_->local_streams()->at(1));
1350 EXPECT_EQ(1u, pc_->local_streams()->count());
1351
1352 // Remove the first stream.
1353 pc_->RemoveStream(pc_->local_streams()->at(0));
1354 EXPECT_EQ(0u, pc_->local_streams()->count());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001355}
1356
deadbeefab9b2d12015-10-14 11:33:11 -07001357// Test that the created offer includes streams we added.
1358TEST_F(PeerConnectionInterfaceTest, AddedStreamsPresentInOffer) {
deadbeef293e9262017-01-11 12:28:30 -08001359 CreatePeerConnectionWithoutDtls();
deadbeefab9b2d12015-10-14 11:33:11 -07001360 AddAudioVideoStream(kStreamLabel1, "audio_track", "video_track");
kwibergd1fe2812016-04-27 06:47:29 -07001361 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001362 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07001363
1364 const cricket::ContentInfo* audio_content =
1365 cricket::GetFirstAudioContent(offer->description());
1366 const cricket::AudioContentDescription* audio_desc =
1367 static_cast<const cricket::AudioContentDescription*>(
1368 audio_content->description);
1369 EXPECT_TRUE(
1370 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
1371
1372 const cricket::ContentInfo* video_content =
1373 cricket::GetFirstVideoContent(offer->description());
1374 const cricket::VideoContentDescription* video_desc =
1375 static_cast<const cricket::VideoContentDescription*>(
1376 video_content->description);
1377 EXPECT_TRUE(
1378 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1379
1380 // Add another stream and ensure the offer includes both the old and new
1381 // streams.
1382 AddAudioVideoStream(kStreamLabel2, "audio_track2", "video_track2");
kwiberg2bbff992016-03-16 11:03:04 -07001383 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07001384
1385 audio_content = cricket::GetFirstAudioContent(offer->description());
1386 audio_desc = static_cast<const cricket::AudioContentDescription*>(
1387 audio_content->description);
1388 EXPECT_TRUE(
1389 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
1390 EXPECT_TRUE(
1391 ContainsTrack(audio_desc->streams(), kStreamLabel2, "audio_track2"));
1392
1393 video_content = cricket::GetFirstVideoContent(offer->description());
1394 video_desc = static_cast<const cricket::VideoContentDescription*>(
1395 video_content->description);
1396 EXPECT_TRUE(
1397 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1398 EXPECT_TRUE(
1399 ContainsTrack(video_desc->streams(), kStreamLabel2, "video_track2"));
1400}
1401
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001402TEST_F(PeerConnectionInterfaceTest, RemoveStream) {
deadbeef293e9262017-01-11 12:28:30 -08001403 CreatePeerConnectionWithoutDtls();
deadbeefab9b2d12015-10-14 11:33:11 -07001404 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001405 ASSERT_EQ(1u, pc_->local_streams()->count());
1406 pc_->RemoveStream(pc_->local_streams()->at(0));
1407 EXPECT_EQ(0u, pc_->local_streams()->count());
1408}
1409
deadbeefe1f9d832016-01-14 15:35:42 -08001410// Test for AddTrack and RemoveTrack methods.
1411// Tests that the created offer includes tracks we added,
1412// and that the RtpSenders are created correctly.
1413// Also tests that RemoveTrack removes the tracks from subsequent offers.
1414TEST_F(PeerConnectionInterfaceTest, AddTrackRemoveTrack) {
deadbeef293e9262017-01-11 12:28:30 -08001415 CreatePeerConnectionWithoutDtls();
deadbeefe1f9d832016-01-14 15:35:42 -08001416 // Create a dummy stream, so tracks share a stream label.
zhihuang9763d562016-08-05 11:14:50 -07001417 rtc::scoped_refptr<MediaStreamInterface> stream(
deadbeefe1f9d832016-01-14 15:35:42 -08001418 pc_factory_->CreateLocalMediaStream(kStreamLabel1));
1419 std::vector<MediaStreamInterface*> stream_list;
1420 stream_list.push_back(stream.get());
zhihuang9763d562016-08-05 11:14:50 -07001421 rtc::scoped_refptr<AudioTrackInterface> audio_track(
deadbeefe1f9d832016-01-14 15:35:42 -08001422 pc_factory_->CreateAudioTrack("audio_track", nullptr));
zhihuang9763d562016-08-05 11:14:50 -07001423 rtc::scoped_refptr<VideoTrackInterface> video_track(
1424 pc_factory_->CreateVideoTrack(
deadbeef112b2e92017-02-10 20:13:37 -08001425 "video_track", pc_factory_->CreateVideoSource(
1426 std::unique_ptr<cricket::VideoCapturer>(
1427 new cricket::FakeVideoCapturer()))));
deadbeefe1f9d832016-01-14 15:35:42 -08001428 auto audio_sender = pc_->AddTrack(audio_track, stream_list);
1429 auto video_sender = pc_->AddTrack(video_track, stream_list);
deadbeefa601f5c2016-06-06 14:27:39 -07001430 EXPECT_EQ(1UL, audio_sender->stream_ids().size());
1431 EXPECT_EQ(kStreamLabel1, audio_sender->stream_ids()[0]);
deadbeefe1f9d832016-01-14 15:35:42 -08001432 EXPECT_EQ("audio_track", audio_sender->id());
1433 EXPECT_EQ(audio_track, audio_sender->track());
deadbeefa601f5c2016-06-06 14:27:39 -07001434 EXPECT_EQ(1UL, video_sender->stream_ids().size());
1435 EXPECT_EQ(kStreamLabel1, video_sender->stream_ids()[0]);
deadbeefe1f9d832016-01-14 15:35:42 -08001436 EXPECT_EQ("video_track", video_sender->id());
1437 EXPECT_EQ(video_track, video_sender->track());
1438
1439 // Now create an offer and check for the senders.
kwibergd1fe2812016-04-27 06:47:29 -07001440 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001441 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefe1f9d832016-01-14 15:35:42 -08001442
1443 const cricket::ContentInfo* audio_content =
1444 cricket::GetFirstAudioContent(offer->description());
1445 const cricket::AudioContentDescription* audio_desc =
1446 static_cast<const cricket::AudioContentDescription*>(
1447 audio_content->description);
1448 EXPECT_TRUE(
1449 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
1450
1451 const cricket::ContentInfo* video_content =
1452 cricket::GetFirstVideoContent(offer->description());
1453 const cricket::VideoContentDescription* video_desc =
1454 static_cast<const cricket::VideoContentDescription*>(
1455 video_content->description);
1456 EXPECT_TRUE(
1457 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1458
Steve Antondb45ca82017-09-11 18:27:34 -07001459 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefe1f9d832016-01-14 15:35:42 -08001460
1461 // Now try removing the tracks.
1462 EXPECT_TRUE(pc_->RemoveTrack(audio_sender));
1463 EXPECT_TRUE(pc_->RemoveTrack(video_sender));
1464
1465 // Create a new offer and ensure it doesn't contain the removed senders.
kwiberg2bbff992016-03-16 11:03:04 -07001466 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefe1f9d832016-01-14 15:35:42 -08001467
1468 audio_content = cricket::GetFirstAudioContent(offer->description());
1469 audio_desc = static_cast<const cricket::AudioContentDescription*>(
1470 audio_content->description);
1471 EXPECT_FALSE(
1472 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
1473
1474 video_content = cricket::GetFirstVideoContent(offer->description());
1475 video_desc = static_cast<const cricket::VideoContentDescription*>(
1476 video_content->description);
1477 EXPECT_FALSE(
1478 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1479
Steve Antondb45ca82017-09-11 18:27:34 -07001480 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefe1f9d832016-01-14 15:35:42 -08001481
1482 // Calling RemoveTrack on a sender no longer attached to a PeerConnection
1483 // should return false.
1484 EXPECT_FALSE(pc_->RemoveTrack(audio_sender));
1485 EXPECT_FALSE(pc_->RemoveTrack(video_sender));
1486}
1487
1488// Test creating senders without a stream specified,
1489// expecting a random stream ID to be generated.
1490TEST_F(PeerConnectionInterfaceTest, AddTrackWithoutStream) {
deadbeef293e9262017-01-11 12:28:30 -08001491 CreatePeerConnectionWithoutDtls();
deadbeefe1f9d832016-01-14 15:35:42 -08001492 // Create a dummy stream, so tracks share a stream label.
zhihuang9763d562016-08-05 11:14:50 -07001493 rtc::scoped_refptr<AudioTrackInterface> audio_track(
deadbeefe1f9d832016-01-14 15:35:42 -08001494 pc_factory_->CreateAudioTrack("audio_track", nullptr));
zhihuang9763d562016-08-05 11:14:50 -07001495 rtc::scoped_refptr<VideoTrackInterface> video_track(
1496 pc_factory_->CreateVideoTrack(
deadbeef112b2e92017-02-10 20:13:37 -08001497 "video_track", pc_factory_->CreateVideoSource(
1498 std::unique_ptr<cricket::VideoCapturer>(
1499 new cricket::FakeVideoCapturer()))));
deadbeefe1f9d832016-01-14 15:35:42 -08001500 auto audio_sender =
1501 pc_->AddTrack(audio_track, std::vector<MediaStreamInterface*>());
1502 auto video_sender =
1503 pc_->AddTrack(video_track, std::vector<MediaStreamInterface*>());
1504 EXPECT_EQ("audio_track", audio_sender->id());
1505 EXPECT_EQ(audio_track, audio_sender->track());
1506 EXPECT_EQ("video_track", video_sender->id());
1507 EXPECT_EQ(video_track, video_sender->track());
1508 // If the ID is truly a random GUID, it should be infinitely unlikely they
1509 // will be the same.
deadbeefa601f5c2016-06-06 14:27:39 -07001510 EXPECT_NE(video_sender->stream_ids(), audio_sender->stream_ids());
deadbeefe1f9d832016-01-14 15:35:42 -08001511}
1512
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001513TEST_F(PeerConnectionInterfaceTest, CreateOfferReceiveAnswer) {
1514 InitiateCall();
1515 WaitAndVerifyOnAddStream(kStreamLabel1);
1516 VerifyRemoteRtpHeaderExtensions();
1517}
1518
1519TEST_F(PeerConnectionInterfaceTest, CreateOfferReceivePrAnswerAndAnswer) {
deadbeef293e9262017-01-11 12:28:30 -08001520 CreatePeerConnectionWithoutDtls();
deadbeefab9b2d12015-10-14 11:33:11 -07001521 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001522 CreateOfferAsLocalDescription();
1523 std::string offer;
1524 EXPECT_TRUE(pc_->local_description()->ToString(&offer));
1525 CreatePrAnswerAndAnswerAsRemoteDescription(offer);
1526 WaitAndVerifyOnAddStream(kStreamLabel1);
1527}
1528
1529TEST_F(PeerConnectionInterfaceTest, ReceiveOfferCreateAnswer) {
deadbeef293e9262017-01-11 12:28:30 -08001530 CreatePeerConnectionWithoutDtls();
deadbeefab9b2d12015-10-14 11:33:11 -07001531 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001532
1533 CreateOfferAsRemoteDescription();
1534 CreateAnswerAsLocalDescription();
1535
1536 WaitAndVerifyOnAddStream(kStreamLabel1);
1537}
1538
1539TEST_F(PeerConnectionInterfaceTest, ReceiveOfferCreatePrAnswerAndAnswer) {
deadbeef293e9262017-01-11 12:28:30 -08001540 CreatePeerConnectionWithoutDtls();
deadbeefab9b2d12015-10-14 11:33:11 -07001541 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001542
1543 CreateOfferAsRemoteDescription();
1544 CreatePrAnswerAsLocalDescription();
1545 CreateAnswerAsLocalDescription();
1546
1547 WaitAndVerifyOnAddStream(kStreamLabel1);
1548}
1549
1550TEST_F(PeerConnectionInterfaceTest, Renegotiate) {
1551 InitiateCall();
1552 ASSERT_EQ(1u, pc_->remote_streams()->count());
1553 pc_->RemoveStream(pc_->local_streams()->at(0));
1554 CreateOfferReceiveAnswer();
1555 EXPECT_EQ(0u, pc_->remote_streams()->count());
deadbeefab9b2d12015-10-14 11:33:11 -07001556 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001557 CreateOfferReceiveAnswer();
1558}
1559
1560// Tests that after negotiating an audio only call, the respondent can perform a
1561// renegotiation that removes the audio stream.
1562TEST_F(PeerConnectionInterfaceTest, RenegotiateAudioOnly) {
deadbeef293e9262017-01-11 12:28:30 -08001563 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001564 AddVoiceStream(kStreamLabel1);
1565 CreateOfferAsRemoteDescription();
1566 CreateAnswerAsLocalDescription();
1567
1568 ASSERT_EQ(1u, pc_->remote_streams()->count());
1569 pc_->RemoveStream(pc_->local_streams()->at(0));
1570 CreateOfferReceiveAnswer();
1571 EXPECT_EQ(0u, pc_->remote_streams()->count());
1572}
1573
1574// Test that candidates are generated and that we can parse our own candidates.
1575TEST_F(PeerConnectionInterfaceTest, IceCandidates) {
deadbeef293e9262017-01-11 12:28:30 -08001576 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001577
1578 EXPECT_FALSE(pc_->AddIceCandidate(observer_.last_candidate_.get()));
1579 // SetRemoteDescription takes ownership of offer.
kwibergd1fe2812016-04-27 06:47:29 -07001580 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefab9b2d12015-10-14 11:33:11 -07001581 AddVideoStream(kStreamLabel1);
deadbeefc80741f2015-10-22 13:14:45 -07001582 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07001583 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001584
1585 // SetLocalDescription takes ownership of answer.
kwibergd1fe2812016-04-27 06:47:29 -07001586 std::unique_ptr<SessionDescriptionInterface> answer;
deadbeefc80741f2015-10-22 13:14:45 -07001587 EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07001588 EXPECT_TRUE(DoSetLocalDescription(std::move(answer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001589
1590 EXPECT_TRUE_WAIT(observer_.last_candidate_.get() != NULL, kTimeout);
1591 EXPECT_TRUE_WAIT(observer_.ice_complete_, kTimeout);
1592
1593 EXPECT_TRUE(pc_->AddIceCandidate(observer_.last_candidate_.get()));
1594}
1595
deadbeefab9b2d12015-10-14 11:33:11 -07001596// Test that CreateOffer and CreateAnswer will fail if the track labels are
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001597// not unique.
1598TEST_F(PeerConnectionInterfaceTest, CreateOfferAnswerWithInvalidStream) {
deadbeef293e9262017-01-11 12:28:30 -08001599 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001600 // Create a regular offer for the CreateAnswer test later.
kwibergd1fe2812016-04-27 06:47:29 -07001601 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefc80741f2015-10-22 13:14:45 -07001602 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
kwiberg2bbff992016-03-16 11:03:04 -07001603 EXPECT_TRUE(offer);
1604 offer.reset();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001605
1606 // Create a local stream with audio&video tracks having same label.
1607 AddAudioVideoStream(kStreamLabel1, "track_label", "track_label");
1608
1609 // Test CreateOffer
deadbeefc80741f2015-10-22 13:14:45 -07001610 EXPECT_FALSE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001611
1612 // Test CreateAnswer
kwibergd1fe2812016-04-27 06:47:29 -07001613 std::unique_ptr<SessionDescriptionInterface> answer;
deadbeefc80741f2015-10-22 13:14:45 -07001614 EXPECT_FALSE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001615}
1616
1617// Test that we will get different SSRCs for each tracks in the offer and answer
1618// we created.
1619TEST_F(PeerConnectionInterfaceTest, SsrcInOfferAnswer) {
deadbeef293e9262017-01-11 12:28:30 -08001620 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001621 // Create a local stream with audio&video tracks having different labels.
1622 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
1623
1624 // Test CreateOffer
kwibergd1fe2812016-04-27 06:47:29 -07001625 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001626 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001627 int audio_ssrc = 0;
1628 int video_ssrc = 0;
1629 EXPECT_TRUE(GetFirstSsrc(GetFirstAudioContent(offer->description()),
1630 &audio_ssrc));
1631 EXPECT_TRUE(GetFirstSsrc(GetFirstVideoContent(offer->description()),
1632 &video_ssrc));
1633 EXPECT_NE(audio_ssrc, video_ssrc);
1634
1635 // Test CreateAnswer
Steve Antondb45ca82017-09-11 18:27:34 -07001636 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
kwibergd1fe2812016-04-27 06:47:29 -07001637 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -07001638 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001639 audio_ssrc = 0;
1640 video_ssrc = 0;
1641 EXPECT_TRUE(GetFirstSsrc(GetFirstAudioContent(answer->description()),
1642 &audio_ssrc));
1643 EXPECT_TRUE(GetFirstSsrc(GetFirstVideoContent(answer->description()),
1644 &video_ssrc));
1645 EXPECT_NE(audio_ssrc, video_ssrc);
1646}
1647
deadbeefeb459812015-12-15 19:24:43 -08001648// Test that it's possible to call AddTrack on a MediaStream after adding
1649// the stream to a PeerConnection.
1650// TODO(deadbeef): Remove this test once this behavior is no longer supported.
1651TEST_F(PeerConnectionInterfaceTest, AddTrackAfterAddStream) {
deadbeef293e9262017-01-11 12:28:30 -08001652 CreatePeerConnectionWithoutDtls();
deadbeefeb459812015-12-15 19:24:43 -08001653 // Create audio stream and add to PeerConnection.
1654 AddVoiceStream(kStreamLabel1);
1655 MediaStreamInterface* stream = pc_->local_streams()->at(0);
1656
1657 // Add video track to the audio-only stream.
zhihuang9763d562016-08-05 11:14:50 -07001658 rtc::scoped_refptr<VideoTrackInterface> video_track(
1659 pc_factory_->CreateVideoTrack(
deadbeef112b2e92017-02-10 20:13:37 -08001660 "video_label", pc_factory_->CreateVideoSource(
1661 std::unique_ptr<cricket::VideoCapturer>(
1662 new cricket::FakeVideoCapturer()))));
deadbeefeb459812015-12-15 19:24:43 -08001663 stream->AddTrack(video_track.get());
1664
kwibergd1fe2812016-04-27 06:47:29 -07001665 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001666 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefeb459812015-12-15 19:24:43 -08001667
1668 const cricket::MediaContentDescription* video_desc =
1669 cricket::GetFirstVideoContentDescription(offer->description());
1670 EXPECT_TRUE(video_desc != nullptr);
1671}
1672
1673// Test that it's possible to call RemoveTrack on a MediaStream after adding
1674// the stream to a PeerConnection.
1675// TODO(deadbeef): Remove this test once this behavior is no longer supported.
1676TEST_F(PeerConnectionInterfaceTest, RemoveTrackAfterAddStream) {
deadbeef293e9262017-01-11 12:28:30 -08001677 CreatePeerConnectionWithoutDtls();
deadbeefeb459812015-12-15 19:24:43 -08001678 // Create audio/video stream and add to PeerConnection.
1679 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
1680 MediaStreamInterface* stream = pc_->local_streams()->at(0);
1681
1682 // Remove the video track.
1683 stream->RemoveTrack(stream->GetVideoTracks()[0]);
1684
kwibergd1fe2812016-04-27 06:47:29 -07001685 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001686 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefeb459812015-12-15 19:24:43 -08001687
1688 const cricket::MediaContentDescription* video_desc =
1689 cricket::GetFirstVideoContentDescription(offer->description());
1690 EXPECT_TRUE(video_desc == nullptr);
1691}
1692
deadbeef1dcb1642017-03-29 21:08:16 -07001693// Verify that CreateDtmfSender only succeeds if called with a valid local
1694// track. Other aspects of DtmfSenders are tested in
1695// peerconnection_integrationtest.cc.
1696TEST_F(PeerConnectionInterfaceTest, CreateDtmfSenderWithInvalidParams) {
1697 CreatePeerConnection();
1698 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
1699 EXPECT_EQ(nullptr, pc_->CreateDtmfSender(nullptr));
1700 rtc::scoped_refptr<webrtc::AudioTrackInterface> non_localtrack(
1701 pc_factory_->CreateAudioTrack("dummy_track", nullptr));
1702 EXPECT_EQ(nullptr, pc_->CreateDtmfSender(non_localtrack));
1703}
1704
deadbeefbd7d8f72015-12-18 16:58:44 -08001705// Test creating a sender with a stream ID, and ensure the ID is populated
1706// in the offer.
1707TEST_F(PeerConnectionInterfaceTest, CreateSenderWithStream) {
deadbeef293e9262017-01-11 12:28:30 -08001708 CreatePeerConnectionWithoutDtls();
deadbeefbd7d8f72015-12-18 16:58:44 -08001709 pc_->CreateSender("video", kStreamLabel1);
1710
kwibergd1fe2812016-04-27 06:47:29 -07001711 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001712 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefbd7d8f72015-12-18 16:58:44 -08001713
1714 const cricket::MediaContentDescription* video_desc =
1715 cricket::GetFirstVideoContentDescription(offer->description());
1716 ASSERT_TRUE(video_desc != nullptr);
1717 ASSERT_EQ(1u, video_desc->streams().size());
1718 EXPECT_EQ(kStreamLabel1, video_desc->streams()[0].sync_label);
1719}
1720
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001721// Test that we can specify a certain track that we want statistics about.
1722TEST_F(PeerConnectionInterfaceTest, GetStatsForSpecificTrack) {
1723 InitiateCall();
1724 ASSERT_LT(0u, pc_->remote_streams()->count());
1725 ASSERT_LT(0u, pc_->remote_streams()->at(0)->GetAudioTracks().size());
zhihuang9763d562016-08-05 11:14:50 -07001726 rtc::scoped_refptr<MediaStreamTrackInterface> remote_audio =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001727 pc_->remote_streams()->at(0)->GetAudioTracks()[0];
1728 EXPECT_TRUE(DoGetStats(remote_audio));
1729
1730 // Remove the stream. Since we are sending to our selves the local
1731 // and the remote stream is the same.
1732 pc_->RemoveStream(pc_->local_streams()->at(0));
1733 // Do a re-negotiation.
1734 CreateOfferReceiveAnswer();
1735
1736 ASSERT_EQ(0u, pc_->remote_streams()->count());
1737
1738 // Test that we still can get statistics for the old track. Even if it is not
1739 // sent any longer.
1740 EXPECT_TRUE(DoGetStats(remote_audio));
1741}
1742
1743// Test that we can get stats on a video track.
1744TEST_F(PeerConnectionInterfaceTest, GetStatsForVideoTrack) {
1745 InitiateCall();
1746 ASSERT_LT(0u, pc_->remote_streams()->count());
1747 ASSERT_LT(0u, pc_->remote_streams()->at(0)->GetVideoTracks().size());
zhihuang9763d562016-08-05 11:14:50 -07001748 rtc::scoped_refptr<MediaStreamTrackInterface> remote_video =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001749 pc_->remote_streams()->at(0)->GetVideoTracks()[0];
1750 EXPECT_TRUE(DoGetStats(remote_video));
1751}
1752
1753// Test that we don't get statistics for an invalid track.
zhihuange9e94c32016-11-04 11:38:15 -07001754TEST_F(PeerConnectionInterfaceTest, GetStatsForInvalidTrack) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001755 InitiateCall();
zhihuang9763d562016-08-05 11:14:50 -07001756 rtc::scoped_refptr<AudioTrackInterface> unknown_audio_track(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001757 pc_factory_->CreateAudioTrack("unknown track", NULL));
1758 EXPECT_FALSE(DoGetStats(unknown_audio_track));
1759}
1760
1761// This test setup two RTP data channels in loop back.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001762TEST_F(PeerConnectionInterfaceTest, TestDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001763 FakeConstraints constraints;
1764 constraints.SetAllowRtpDataChannels();
1765 CreatePeerConnection(&constraints);
zhihuang9763d562016-08-05 11:14:50 -07001766 rtc::scoped_refptr<DataChannelInterface> data1 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001767 pc_->CreateDataChannel("test1", NULL);
zhihuang9763d562016-08-05 11:14:50 -07001768 rtc::scoped_refptr<DataChannelInterface> data2 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001769 pc_->CreateDataChannel("test2", NULL);
1770 ASSERT_TRUE(data1 != NULL);
kwibergd1fe2812016-04-27 06:47:29 -07001771 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001772 new MockDataChannelObserver(data1));
kwibergd1fe2812016-04-27 06:47:29 -07001773 std::unique_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001774 new MockDataChannelObserver(data2));
1775
1776 EXPECT_EQ(DataChannelInterface::kConnecting, data1->state());
1777 EXPECT_EQ(DataChannelInterface::kConnecting, data2->state());
1778 std::string data_to_send1 = "testing testing";
1779 std::string data_to_send2 = "testing something else";
1780 EXPECT_FALSE(data1->Send(DataBuffer(data_to_send1)));
1781
1782 CreateOfferReceiveAnswer();
1783 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1784 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
1785
1786 EXPECT_EQ(DataChannelInterface::kOpen, data1->state());
1787 EXPECT_EQ(DataChannelInterface::kOpen, data2->state());
1788 EXPECT_TRUE(data1->Send(DataBuffer(data_to_send1)));
1789 EXPECT_TRUE(data2->Send(DataBuffer(data_to_send2)));
1790
1791 EXPECT_EQ_WAIT(data_to_send1, observer1->last_message(), kTimeout);
1792 EXPECT_EQ_WAIT(data_to_send2, observer2->last_message(), kTimeout);
1793
1794 data1->Close();
1795 EXPECT_EQ(DataChannelInterface::kClosing, data1->state());
1796 CreateOfferReceiveAnswer();
1797 EXPECT_FALSE(observer1->IsOpen());
1798 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
1799 EXPECT_TRUE(observer2->IsOpen());
1800
1801 data_to_send2 = "testing something else again";
1802 EXPECT_TRUE(data2->Send(DataBuffer(data_to_send2)));
1803
1804 EXPECT_EQ_WAIT(data_to_send2, observer2->last_message(), kTimeout);
1805}
1806
1807// This test verifies that sendnig binary data over RTP data channels should
1808// fail.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001809TEST_F(PeerConnectionInterfaceTest, TestSendBinaryOnRtpDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001810 FakeConstraints constraints;
1811 constraints.SetAllowRtpDataChannels();
1812 CreatePeerConnection(&constraints);
zhihuang9763d562016-08-05 11:14:50 -07001813 rtc::scoped_refptr<DataChannelInterface> data1 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001814 pc_->CreateDataChannel("test1", NULL);
zhihuang9763d562016-08-05 11:14:50 -07001815 rtc::scoped_refptr<DataChannelInterface> data2 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001816 pc_->CreateDataChannel("test2", NULL);
1817 ASSERT_TRUE(data1 != NULL);
kwibergd1fe2812016-04-27 06:47:29 -07001818 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001819 new MockDataChannelObserver(data1));
kwibergd1fe2812016-04-27 06:47:29 -07001820 std::unique_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001821 new MockDataChannelObserver(data2));
1822
1823 EXPECT_EQ(DataChannelInterface::kConnecting, data1->state());
1824 EXPECT_EQ(DataChannelInterface::kConnecting, data2->state());
1825
1826 CreateOfferReceiveAnswer();
1827 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1828 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
1829
1830 EXPECT_EQ(DataChannelInterface::kOpen, data1->state());
1831 EXPECT_EQ(DataChannelInterface::kOpen, data2->state());
1832
jbaucheec21bd2016-03-20 06:15:43 -07001833 rtc::CopyOnWriteBuffer buffer("test", 4);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001834 EXPECT_FALSE(data1->Send(DataBuffer(buffer, true)));
1835}
1836
1837// This test setup a RTP data channels in loop back and test that a channel is
1838// opened even if the remote end answer with a zero SSRC.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001839TEST_F(PeerConnectionInterfaceTest, TestSendOnlyDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001840 FakeConstraints constraints;
1841 constraints.SetAllowRtpDataChannels();
1842 CreatePeerConnection(&constraints);
zhihuang9763d562016-08-05 11:14:50 -07001843 rtc::scoped_refptr<DataChannelInterface> data1 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001844 pc_->CreateDataChannel("test1", NULL);
kwibergd1fe2812016-04-27 06:47:29 -07001845 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001846 new MockDataChannelObserver(data1));
1847
1848 CreateOfferReceiveAnswerWithoutSsrc();
1849
1850 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1851
1852 data1->Close();
1853 EXPECT_EQ(DataChannelInterface::kClosing, data1->state());
1854 CreateOfferReceiveAnswerWithoutSsrc();
1855 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
1856 EXPECT_FALSE(observer1->IsOpen());
1857}
1858
1859// This test that if a data channel is added in an answer a receive only channel
1860// channel is created.
1861TEST_F(PeerConnectionInterfaceTest, TestReceiveOnlyDataChannel) {
1862 FakeConstraints constraints;
1863 constraints.SetAllowRtpDataChannels();
1864 CreatePeerConnection(&constraints);
1865
1866 std::string offer_label = "offer_channel";
zhihuang9763d562016-08-05 11:14:50 -07001867 rtc::scoped_refptr<DataChannelInterface> offer_channel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001868 pc_->CreateDataChannel(offer_label, NULL);
1869
1870 CreateOfferAsLocalDescription();
1871
1872 // Replace the data channel label in the offer and apply it as an answer.
1873 std::string receive_label = "answer_channel";
1874 std::string sdp;
1875 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001876 rtc::replace_substrs(offer_label.c_str(), offer_label.length(),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001877 receive_label.c_str(), receive_label.length(),
1878 &sdp);
1879 CreateAnswerAsRemoteDescription(sdp);
1880
1881 // Verify that a new incoming data channel has been created and that
1882 // it is open but can't we written to.
1883 ASSERT_TRUE(observer_.last_datachannel_ != NULL);
1884 DataChannelInterface* received_channel = observer_.last_datachannel_;
1885 EXPECT_EQ(DataChannelInterface::kConnecting, received_channel->state());
1886 EXPECT_EQ(receive_label, received_channel->label());
1887 EXPECT_FALSE(received_channel->Send(DataBuffer("something")));
1888
1889 // Verify that the channel we initially offered has been rejected.
1890 EXPECT_EQ(DataChannelInterface::kClosed, offer_channel->state());
1891
1892 // Do another offer / answer exchange and verify that the data channel is
1893 // opened.
1894 CreateOfferReceiveAnswer();
1895 EXPECT_EQ_WAIT(DataChannelInterface::kOpen, received_channel->state(),
1896 kTimeout);
1897}
1898
1899// This test that no data channel is returned if a reliable channel is
1900// requested.
1901// TODO(perkj): Remove this test once reliable channels are implemented.
1902TEST_F(PeerConnectionInterfaceTest, CreateReliableRtpDataChannelShouldFail) {
1903 FakeConstraints constraints;
1904 constraints.SetAllowRtpDataChannels();
1905 CreatePeerConnection(&constraints);
1906
1907 std::string label = "test";
1908 webrtc::DataChannelInit config;
1909 config.reliable = true;
zhihuang9763d562016-08-05 11:14:50 -07001910 rtc::scoped_refptr<DataChannelInterface> channel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001911 pc_->CreateDataChannel(label, &config);
1912 EXPECT_TRUE(channel == NULL);
1913}
1914
deadbeefab9b2d12015-10-14 11:33:11 -07001915// Verifies that duplicated label is not allowed for RTP data channel.
1916TEST_F(PeerConnectionInterfaceTest, RtpDuplicatedLabelNotAllowed) {
1917 FakeConstraints constraints;
1918 constraints.SetAllowRtpDataChannels();
1919 CreatePeerConnection(&constraints);
1920
1921 std::string label = "test";
zhihuang9763d562016-08-05 11:14:50 -07001922 rtc::scoped_refptr<DataChannelInterface> channel =
deadbeefab9b2d12015-10-14 11:33:11 -07001923 pc_->CreateDataChannel(label, nullptr);
1924 EXPECT_NE(channel, nullptr);
1925
zhihuang9763d562016-08-05 11:14:50 -07001926 rtc::scoped_refptr<DataChannelInterface> dup_channel =
deadbeefab9b2d12015-10-14 11:33:11 -07001927 pc_->CreateDataChannel(label, nullptr);
1928 EXPECT_EQ(dup_channel, nullptr);
1929}
1930
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001931// This tests that a SCTP data channel is returned using different
1932// DataChannelInit configurations.
1933TEST_F(PeerConnectionInterfaceTest, CreateSctpDataChannel) {
1934 FakeConstraints constraints;
1935 constraints.SetAllowDtlsSctpDataChannels();
1936 CreatePeerConnection(&constraints);
1937
1938 webrtc::DataChannelInit config;
1939
zhihuang9763d562016-08-05 11:14:50 -07001940 rtc::scoped_refptr<DataChannelInterface> channel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001941 pc_->CreateDataChannel("1", &config);
1942 EXPECT_TRUE(channel != NULL);
1943 EXPECT_TRUE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001944 EXPECT_TRUE(observer_.renegotiation_needed_);
1945 observer_.renegotiation_needed_ = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001946
1947 config.ordered = false;
1948 channel = pc_->CreateDataChannel("2", &config);
1949 EXPECT_TRUE(channel != NULL);
1950 EXPECT_TRUE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001951 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001952
1953 config.ordered = true;
1954 config.maxRetransmits = 0;
1955 channel = pc_->CreateDataChannel("3", &config);
1956 EXPECT_TRUE(channel != NULL);
1957 EXPECT_FALSE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001958 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001959
1960 config.maxRetransmits = -1;
1961 config.maxRetransmitTime = 0;
1962 channel = pc_->CreateDataChannel("4", &config);
1963 EXPECT_TRUE(channel != NULL);
1964 EXPECT_FALSE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001965 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001966}
1967
1968// This tests that no data channel is returned if both maxRetransmits and
1969// maxRetransmitTime are set for SCTP data channels.
1970TEST_F(PeerConnectionInterfaceTest,
1971 CreateSctpDataChannelShouldFailForInvalidConfig) {
1972 FakeConstraints constraints;
1973 constraints.SetAllowDtlsSctpDataChannels();
1974 CreatePeerConnection(&constraints);
1975
1976 std::string label = "test";
1977 webrtc::DataChannelInit config;
1978 config.maxRetransmits = 0;
1979 config.maxRetransmitTime = 0;
1980
zhihuang9763d562016-08-05 11:14:50 -07001981 rtc::scoped_refptr<DataChannelInterface> channel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001982 pc_->CreateDataChannel(label, &config);
1983 EXPECT_TRUE(channel == NULL);
1984}
1985
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001986// The test verifies that creating a SCTP data channel with an id already in use
1987// or out of range should fail.
1988TEST_F(PeerConnectionInterfaceTest,
1989 CreateSctpDataChannelWithInvalidIdShouldFail) {
1990 FakeConstraints constraints;
1991 constraints.SetAllowDtlsSctpDataChannels();
1992 CreatePeerConnection(&constraints);
1993
1994 webrtc::DataChannelInit config;
zhihuang9763d562016-08-05 11:14:50 -07001995 rtc::scoped_refptr<DataChannelInterface> channel;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001996
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001997 config.id = 1;
1998 channel = pc_->CreateDataChannel("1", &config);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001999 EXPECT_TRUE(channel != NULL);
2000 EXPECT_EQ(1, channel->id());
2001
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002002 channel = pc_->CreateDataChannel("x", &config);
2003 EXPECT_TRUE(channel == NULL);
2004
2005 config.id = cricket::kMaxSctpSid;
2006 channel = pc_->CreateDataChannel("max", &config);
2007 EXPECT_TRUE(channel != NULL);
2008 EXPECT_EQ(config.id, channel->id());
2009
2010 config.id = cricket::kMaxSctpSid + 1;
2011 channel = pc_->CreateDataChannel("x", &config);
2012 EXPECT_TRUE(channel == NULL);
2013}
2014
deadbeefab9b2d12015-10-14 11:33:11 -07002015// Verifies that duplicated label is allowed for SCTP data channel.
2016TEST_F(PeerConnectionInterfaceTest, SctpDuplicatedLabelAllowed) {
2017 FakeConstraints constraints;
2018 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2019 true);
2020 CreatePeerConnection(&constraints);
2021
2022 std::string label = "test";
zhihuang9763d562016-08-05 11:14:50 -07002023 rtc::scoped_refptr<DataChannelInterface> channel =
deadbeefab9b2d12015-10-14 11:33:11 -07002024 pc_->CreateDataChannel(label, nullptr);
2025 EXPECT_NE(channel, nullptr);
2026
zhihuang9763d562016-08-05 11:14:50 -07002027 rtc::scoped_refptr<DataChannelInterface> dup_channel =
deadbeefab9b2d12015-10-14 11:33:11 -07002028 pc_->CreateDataChannel(label, nullptr);
2029 EXPECT_NE(dup_channel, nullptr);
2030}
2031
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00002032// This test verifies that OnRenegotiationNeeded is fired for every new RTP
2033// DataChannel.
2034TEST_F(PeerConnectionInterfaceTest, RenegotiationNeededForNewRtpDataChannel) {
2035 FakeConstraints constraints;
2036 constraints.SetAllowRtpDataChannels();
2037 CreatePeerConnection(&constraints);
2038
zhihuang9763d562016-08-05 11:14:50 -07002039 rtc::scoped_refptr<DataChannelInterface> dc1 =
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00002040 pc_->CreateDataChannel("test1", NULL);
2041 EXPECT_TRUE(observer_.renegotiation_needed_);
2042 observer_.renegotiation_needed_ = false;
2043
zhihuang9763d562016-08-05 11:14:50 -07002044 rtc::scoped_refptr<DataChannelInterface> dc2 =
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00002045 pc_->CreateDataChannel("test2", NULL);
2046 EXPECT_TRUE(observer_.renegotiation_needed_);
2047}
2048
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002049// This test that a data channel closes when a PeerConnection is deleted/closed.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002050TEST_F(PeerConnectionInterfaceTest, DataChannelCloseWhenPeerConnectionClose) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002051 FakeConstraints constraints;
2052 constraints.SetAllowRtpDataChannels();
2053 CreatePeerConnection(&constraints);
2054
zhihuang9763d562016-08-05 11:14:50 -07002055 rtc::scoped_refptr<DataChannelInterface> data1 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002056 pc_->CreateDataChannel("test1", NULL);
zhihuang9763d562016-08-05 11:14:50 -07002057 rtc::scoped_refptr<DataChannelInterface> data2 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002058 pc_->CreateDataChannel("test2", NULL);
2059 ASSERT_TRUE(data1 != NULL);
kwibergd1fe2812016-04-27 06:47:29 -07002060 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002061 new MockDataChannelObserver(data1));
kwibergd1fe2812016-04-27 06:47:29 -07002062 std::unique_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002063 new MockDataChannelObserver(data2));
2064
2065 CreateOfferReceiveAnswer();
2066 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
2067 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
2068
2069 ReleasePeerConnection();
2070 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
2071 EXPECT_EQ(DataChannelInterface::kClosed, data2->state());
2072}
2073
2074// This test that data channels can be rejected in an answer.
2075TEST_F(PeerConnectionInterfaceTest, TestRejectDataChannelInAnswer) {
2076 FakeConstraints constraints;
2077 constraints.SetAllowRtpDataChannels();
2078 CreatePeerConnection(&constraints);
2079
zhihuang9763d562016-08-05 11:14:50 -07002080 rtc::scoped_refptr<DataChannelInterface> offer_channel(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002081 pc_->CreateDataChannel("offer_channel", NULL));
2082
2083 CreateOfferAsLocalDescription();
2084
2085 // Create an answer where the m-line for data channels are rejected.
2086 std::string sdp;
2087 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07002088 std::unique_ptr<SessionDescriptionInterface> answer(
2089 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
2090 sdp, nullptr));
2091 ASSERT_TRUE(answer);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002092 cricket::ContentInfo* data_info =
2093 answer->description()->GetContentByName("data");
2094 data_info->rejected = true;
2095
Steve Antondb45ca82017-09-11 18:27:34 -07002096 DoSetRemoteDescription(std::move(answer));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002097 EXPECT_EQ(DataChannelInterface::kClosed, offer_channel->state());
2098}
2099
2100// Test that we can create a session description from an SDP string from
2101// FireFox, use it as a remote session description, generate an answer and use
2102// the answer as a local description.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002103TEST_F(PeerConnectionInterfaceTest, ReceiveFireFoxOffer) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002104 FakeConstraints constraints;
2105 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2106 true);
2107 CreatePeerConnection(&constraints);
2108 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
Steve Antondb45ca82017-09-11 18:27:34 -07002109 std::unique_ptr<SessionDescriptionInterface> desc(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002110 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
Steve Antondb45ca82017-09-11 18:27:34 -07002111 webrtc::kFireFoxSdpOffer, nullptr));
2112 EXPECT_TRUE(DoSetSessionDescription(std::move(desc), false));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002113 CreateAnswerAsLocalDescription();
2114 ASSERT_TRUE(pc_->local_description() != NULL);
2115 ASSERT_TRUE(pc_->remote_description() != NULL);
2116
2117 const cricket::ContentInfo* content =
2118 cricket::GetFirstAudioContent(pc_->local_description()->description());
2119 ASSERT_TRUE(content != NULL);
2120 EXPECT_FALSE(content->rejected);
2121
2122 content =
2123 cricket::GetFirstVideoContent(pc_->local_description()->description());
2124 ASSERT_TRUE(content != NULL);
2125 EXPECT_FALSE(content->rejected);
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00002126#ifdef HAVE_SCTP
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002127 content =
2128 cricket::GetFirstDataContent(pc_->local_description()->description());
2129 ASSERT_TRUE(content != NULL);
2130 EXPECT_TRUE(content->rejected);
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00002131#endif
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002132}
2133
zhihuangb19012e2017-09-19 13:47:59 -07002134// Test that fallback from DTLS to SDES is not supported.
2135// The fallback was previously supported but was removed to simplify the code
2136// and because it's non-standard.
2137TEST_F(PeerConnectionInterfaceTest, DtlsSdesFallbackNotSupported) {
deadbeef8662f942017-01-20 21:20:51 -08002138 FakeConstraints constraints;
2139 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2140 true);
2141 CreatePeerConnection(&constraints);
2142 // Wait for fake certificate to be generated. Previously, this is what caused
2143 // the "a=crypto" lines to be rejected.
2144 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
2145 ASSERT_NE(nullptr, fake_certificate_generator_);
2146 EXPECT_EQ_WAIT(1, fake_certificate_generator_->generated_certificates(),
2147 kTimeout);
Steve Antondb45ca82017-09-11 18:27:34 -07002148 std::unique_ptr<SessionDescriptionInterface> desc(
2149 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
2150 kDtlsSdesFallbackSdp, nullptr));
zhihuangb19012e2017-09-19 13:47:59 -07002151 EXPECT_FALSE(DoSetSessionDescription(std::move(desc), false));
deadbeef8662f942017-01-20 21:20:51 -08002152}
2153
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002154// Test that we can create an audio only offer and receive an answer with a
2155// limited set of audio codecs and receive an updated offer with more audio
2156// codecs, where the added codecs are not supported.
2157TEST_F(PeerConnectionInterfaceTest, ReceiveUpdatedAudioOfferWithBadCodecs) {
deadbeef293e9262017-01-11 12:28:30 -08002158 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002159 AddVoiceStream("audio_label");
2160 CreateOfferAsLocalDescription();
2161
Steve Antondb45ca82017-09-11 18:27:34 -07002162 std::unique_ptr<SessionDescriptionInterface> answer(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002163 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
Steve Antondb45ca82017-09-11 18:27:34 -07002164 webrtc::kAudioSdp, nullptr));
2165 EXPECT_TRUE(DoSetSessionDescription(std::move(answer), false));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002166
Steve Antondb45ca82017-09-11 18:27:34 -07002167 std::unique_ptr<SessionDescriptionInterface> updated_offer(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002168 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
jbauchfabe2c92015-07-16 13:43:14 -07002169 webrtc::kAudioSdpWithUnsupportedCodecs,
Steve Antondb45ca82017-09-11 18:27:34 -07002170 nullptr));
2171 EXPECT_TRUE(DoSetSessionDescription(std::move(updated_offer), false));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002172 CreateAnswerAsLocalDescription();
2173}
2174
deadbeefc80741f2015-10-22 13:14:45 -07002175// Test that if we're receiving (but not sending) a track, subsequent offers
2176// will have m-lines with a=recvonly.
2177TEST_F(PeerConnectionInterfaceTest, CreateSubsequentRecvOnlyOffer) {
2178 FakeConstraints constraints;
2179 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2180 true);
2181 CreatePeerConnection(&constraints);
2182 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2183 CreateAnswerAsLocalDescription();
2184
2185 // At this point we should be receiving stream 1, but not sending anything.
2186 // A new offer should be recvonly.
kwibergd1fe2812016-04-27 06:47:29 -07002187 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefc80741f2015-10-22 13:14:45 -07002188 DoCreateOffer(&offer, nullptr);
2189
2190 const cricket::ContentInfo* video_content =
2191 cricket::GetFirstVideoContent(offer->description());
2192 const cricket::VideoContentDescription* video_desc =
2193 static_cast<const cricket::VideoContentDescription*>(
2194 video_content->description);
2195 ASSERT_EQ(cricket::MD_RECVONLY, video_desc->direction());
2196
2197 const cricket::ContentInfo* audio_content =
2198 cricket::GetFirstAudioContent(offer->description());
2199 const cricket::AudioContentDescription* audio_desc =
2200 static_cast<const cricket::AudioContentDescription*>(
2201 audio_content->description);
2202 ASSERT_EQ(cricket::MD_RECVONLY, audio_desc->direction());
2203}
2204
2205// Test that if we're receiving (but not sending) a track, and the
2206// offerToReceiveVideo/offerToReceiveAudio constraints are explicitly set to
2207// false, the generated m-lines will be a=inactive.
2208TEST_F(PeerConnectionInterfaceTest, CreateSubsequentInactiveOffer) {
2209 FakeConstraints constraints;
2210 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2211 true);
2212 CreatePeerConnection(&constraints);
2213 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2214 CreateAnswerAsLocalDescription();
2215
2216 // At this point we should be receiving stream 1, but not sending anything.
2217 // A new offer would be recvonly, but we'll set the "no receive" constraints
2218 // to make it inactive.
kwibergd1fe2812016-04-27 06:47:29 -07002219 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefc80741f2015-10-22 13:14:45 -07002220 FakeConstraints offer_constraints;
2221 offer_constraints.AddMandatory(
2222 webrtc::MediaConstraintsInterface::kOfferToReceiveVideo, false);
2223 offer_constraints.AddMandatory(
2224 webrtc::MediaConstraintsInterface::kOfferToReceiveAudio, false);
2225 DoCreateOffer(&offer, &offer_constraints);
2226
2227 const cricket::ContentInfo* video_content =
2228 cricket::GetFirstVideoContent(offer->description());
2229 const cricket::VideoContentDescription* video_desc =
2230 static_cast<const cricket::VideoContentDescription*>(
2231 video_content->description);
2232 ASSERT_EQ(cricket::MD_INACTIVE, video_desc->direction());
2233
2234 const cricket::ContentInfo* audio_content =
2235 cricket::GetFirstAudioContent(offer->description());
2236 const cricket::AudioContentDescription* audio_desc =
2237 static_cast<const cricket::AudioContentDescription*>(
2238 audio_content->description);
2239 ASSERT_EQ(cricket::MD_INACTIVE, audio_desc->direction());
2240}
2241
deadbeef653b8e02015-11-11 12:55:10 -08002242// Test that we can use SetConfiguration to change the ICE servers of the
2243// PortAllocator.
2244TEST_F(PeerConnectionInterfaceTest, SetConfigurationChangesIceServers) {
2245 CreatePeerConnection();
2246
2247 PeerConnectionInterface::RTCConfiguration config;
2248 PeerConnectionInterface::IceServer server;
2249 server.uri = "stun:test_hostname";
2250 config.servers.push_back(server);
2251 EXPECT_TRUE(pc_->SetConfiguration(config));
2252
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002253 EXPECT_EQ(1u, port_allocator_->stun_servers().size());
2254 EXPECT_EQ("test_hostname",
2255 port_allocator_->stun_servers().begin()->hostname());
deadbeef653b8e02015-11-11 12:55:10 -08002256}
2257
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002258TEST_F(PeerConnectionInterfaceTest, SetConfigurationChangesCandidateFilter) {
2259 CreatePeerConnection();
2260 PeerConnectionInterface::RTCConfiguration config;
2261 config.type = PeerConnectionInterface::kRelay;
2262 EXPECT_TRUE(pc_->SetConfiguration(config));
2263 EXPECT_EQ(cricket::CF_RELAY, port_allocator_->candidate_filter());
2264}
2265
deadbeef293e9262017-01-11 12:28:30 -08002266TEST_F(PeerConnectionInterfaceTest, SetConfigurationChangesPruneTurnPortsFlag) {
2267 PeerConnectionInterface::RTCConfiguration config;
2268 config.prune_turn_ports = false;
2269 CreatePeerConnection(config, nullptr);
2270 EXPECT_FALSE(port_allocator_->prune_turn_ports());
2271
2272 config.prune_turn_ports = true;
2273 EXPECT_TRUE(pc_->SetConfiguration(config));
2274 EXPECT_TRUE(port_allocator_->prune_turn_ports());
2275}
2276
skvladd1f5fda2017-02-03 16:54:05 -08002277// Test that the ice check interval can be changed. This does not verify that
2278// the setting makes it all the way to P2PTransportChannel, as that would
2279// require a very complex set of mocks.
2280TEST_F(PeerConnectionInterfaceTest, SetConfigurationChangesIceCheckInterval) {
2281 PeerConnectionInterface::RTCConfiguration config;
2282 config.ice_check_min_interval = rtc::Optional<int>();
2283 CreatePeerConnection(config, nullptr);
2284 config.ice_check_min_interval = rtc::Optional<int>(100);
2285 EXPECT_TRUE(pc_->SetConfiguration(config));
2286 PeerConnectionInterface::RTCConfiguration new_config =
2287 pc_->GetConfiguration();
2288 EXPECT_EQ(new_config.ice_check_min_interval, rtc::Optional<int>(100));
2289}
2290
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002291// Test that when SetConfiguration changes both the pool size and other
2292// attributes, the pooled session is created with the updated attributes.
2293TEST_F(PeerConnectionInterfaceTest,
2294 SetConfigurationCreatesPooledSessionCorrectly) {
2295 CreatePeerConnection();
2296 PeerConnectionInterface::RTCConfiguration config;
2297 config.ice_candidate_pool_size = 1;
2298 PeerConnectionInterface::IceServer server;
2299 server.uri = kStunAddressOnly;
2300 config.servers.push_back(server);
2301 config.type = PeerConnectionInterface::kRelay;
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07002302 EXPECT_TRUE(pc_->SetConfiguration(config));
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002303
2304 const cricket::FakePortAllocatorSession* session =
2305 static_cast<const cricket::FakePortAllocatorSession*>(
2306 port_allocator_->GetPooledSession());
2307 ASSERT_NE(nullptr, session);
2308 EXPECT_EQ(1UL, session->stun_servers().size());
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002309}
2310
deadbeef293e9262017-01-11 12:28:30 -08002311// Test that after SetLocalDescription, changing the pool size is not allowed,
2312// and an invalid modification error is returned.
deadbeef6de92f92016-12-12 18:49:32 -08002313TEST_F(PeerConnectionInterfaceTest,
2314 CantChangePoolSizeAfterSetLocalDescription) {
2315 CreatePeerConnection();
2316 // Start by setting a size of 1.
2317 PeerConnectionInterface::RTCConfiguration config;
2318 config.ice_candidate_pool_size = 1;
2319 EXPECT_TRUE(pc_->SetConfiguration(config));
2320
2321 // Set remote offer; can still change pool size at this point.
2322 CreateOfferAsRemoteDescription();
2323 config.ice_candidate_pool_size = 2;
2324 EXPECT_TRUE(pc_->SetConfiguration(config));
2325
2326 // Set local answer; now it's too late.
2327 CreateAnswerAsLocalDescription();
2328 config.ice_candidate_pool_size = 3;
deadbeef293e9262017-01-11 12:28:30 -08002329 RTCError error;
2330 EXPECT_FALSE(pc_->SetConfiguration(config, &error));
2331 EXPECT_EQ(RTCErrorType::INVALID_MODIFICATION, error.type());
2332}
2333
deadbeef42a42632017-03-10 15:18:00 -08002334// Test that after setting an answer, extra pooled sessions are discarded. The
2335// ICE candidate pool is only intended to be used for the first offer/answer.
2336TEST_F(PeerConnectionInterfaceTest,
2337 ExtraPooledSessionsDiscardedAfterApplyingAnswer) {
2338 CreatePeerConnection();
2339
2340 // Set a larger-than-necessary size.
2341 PeerConnectionInterface::RTCConfiguration config;
2342 config.ice_candidate_pool_size = 4;
2343 EXPECT_TRUE(pc_->SetConfiguration(config));
2344
2345 // Do offer/answer.
2346 CreateOfferAsRemoteDescription();
2347 CreateAnswerAsLocalDescription();
2348
2349 // Expect no pooled sessions to be left.
2350 const cricket::PortAllocatorSession* session =
2351 port_allocator_->GetPooledSession();
2352 EXPECT_EQ(nullptr, session);
2353}
2354
2355// After Close is called, pooled candidates should be discarded so as to not
2356// waste network resources.
2357TEST_F(PeerConnectionInterfaceTest, PooledSessionsDiscardedAfterClose) {
2358 CreatePeerConnection();
2359
2360 PeerConnectionInterface::RTCConfiguration config;
2361 config.ice_candidate_pool_size = 3;
2362 EXPECT_TRUE(pc_->SetConfiguration(config));
2363 pc_->Close();
2364
2365 // Expect no pooled sessions to be left.
2366 const cricket::PortAllocatorSession* session =
2367 port_allocator_->GetPooledSession();
2368 EXPECT_EQ(nullptr, session);
2369}
2370
deadbeef293e9262017-01-11 12:28:30 -08002371// Test that SetConfiguration returns an invalid modification error if
2372// modifying a field in the configuration that isn't allowed to be modified.
2373TEST_F(PeerConnectionInterfaceTest,
2374 SetConfigurationReturnsInvalidModificationError) {
2375 PeerConnectionInterface::RTCConfiguration config;
2376 config.bundle_policy = PeerConnectionInterface::kBundlePolicyBalanced;
2377 config.rtcp_mux_policy = PeerConnectionInterface::kRtcpMuxPolicyNegotiate;
2378 config.continual_gathering_policy = PeerConnectionInterface::GATHER_ONCE;
2379 CreatePeerConnection(config, nullptr);
2380
2381 PeerConnectionInterface::RTCConfiguration modified_config = config;
2382 modified_config.bundle_policy =
2383 PeerConnectionInterface::kBundlePolicyMaxBundle;
2384 RTCError error;
2385 EXPECT_FALSE(pc_->SetConfiguration(modified_config, &error));
2386 EXPECT_EQ(RTCErrorType::INVALID_MODIFICATION, error.type());
2387
2388 modified_config = config;
2389 modified_config.rtcp_mux_policy =
2390 PeerConnectionInterface::kRtcpMuxPolicyRequire;
2391 error.set_type(RTCErrorType::NONE);
2392 EXPECT_FALSE(pc_->SetConfiguration(modified_config, &error));
2393 EXPECT_EQ(RTCErrorType::INVALID_MODIFICATION, error.type());
2394
2395 modified_config = config;
2396 modified_config.continual_gathering_policy =
2397 PeerConnectionInterface::GATHER_CONTINUALLY;
2398 error.set_type(RTCErrorType::NONE);
2399 EXPECT_FALSE(pc_->SetConfiguration(modified_config, &error));
2400 EXPECT_EQ(RTCErrorType::INVALID_MODIFICATION, error.type());
2401}
2402
2403// Test that SetConfiguration returns a range error if the candidate pool size
2404// is negative or larger than allowed by the spec.
2405TEST_F(PeerConnectionInterfaceTest,
2406 SetConfigurationReturnsRangeErrorForBadCandidatePoolSize) {
2407 PeerConnectionInterface::RTCConfiguration config;
2408 CreatePeerConnection(config, nullptr);
2409
2410 config.ice_candidate_pool_size = -1;
2411 RTCError error;
2412 EXPECT_FALSE(pc_->SetConfiguration(config, &error));
2413 EXPECT_EQ(RTCErrorType::INVALID_RANGE, error.type());
2414
2415 config.ice_candidate_pool_size = INT_MAX;
2416 error.set_type(RTCErrorType::NONE);
2417 EXPECT_FALSE(pc_->SetConfiguration(config, &error));
2418 EXPECT_EQ(RTCErrorType::INVALID_RANGE, error.type());
2419}
2420
2421// Test that SetConfiguration returns a syntax error if parsing an ICE server
2422// URL failed.
2423TEST_F(PeerConnectionInterfaceTest,
2424 SetConfigurationReturnsSyntaxErrorFromBadIceUrls) {
2425 PeerConnectionInterface::RTCConfiguration config;
2426 CreatePeerConnection(config, nullptr);
2427
2428 PeerConnectionInterface::IceServer bad_server;
2429 bad_server.uri = "stunn:www.example.com";
2430 config.servers.push_back(bad_server);
2431 RTCError error;
2432 EXPECT_FALSE(pc_->SetConfiguration(config, &error));
2433 EXPECT_EQ(RTCErrorType::SYNTAX_ERROR, error.type());
2434}
2435
2436// Test that SetConfiguration returns an invalid parameter error if a TURN
2437// IceServer is missing a username or password.
2438TEST_F(PeerConnectionInterfaceTest,
2439 SetConfigurationReturnsInvalidParameterIfCredentialsMissing) {
2440 PeerConnectionInterface::RTCConfiguration config;
2441 CreatePeerConnection(config, nullptr);
2442
2443 PeerConnectionInterface::IceServer bad_server;
2444 bad_server.uri = "turn:www.example.com";
2445 // Missing password.
2446 bad_server.username = "foo";
2447 config.servers.push_back(bad_server);
2448 RTCError error;
2449 EXPECT_FALSE(pc_->SetConfiguration(config, &error));
2450 EXPECT_EQ(RTCErrorType::INVALID_PARAMETER, error.type());
deadbeef6de92f92016-12-12 18:49:32 -08002451}
2452
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002453// Test that PeerConnection::Close changes the states to closed and all remote
2454// tracks change state to ended.
2455TEST_F(PeerConnectionInterfaceTest, CloseAndTestStreamsAndStates) {
2456 // Initialize a PeerConnection and negotiate local and remote session
2457 // description.
2458 InitiateCall();
2459 ASSERT_EQ(1u, pc_->local_streams()->count());
2460 ASSERT_EQ(1u, pc_->remote_streams()->count());
2461
2462 pc_->Close();
2463
2464 EXPECT_EQ(PeerConnectionInterface::kClosed, pc_->signaling_state());
2465 EXPECT_EQ(PeerConnectionInterface::kIceConnectionClosed,
2466 pc_->ice_connection_state());
2467 EXPECT_EQ(PeerConnectionInterface::kIceGatheringComplete,
2468 pc_->ice_gathering_state());
2469
2470 EXPECT_EQ(1u, pc_->local_streams()->count());
2471 EXPECT_EQ(1u, pc_->remote_streams()->count());
2472
zhihuang9763d562016-08-05 11:14:50 -07002473 rtc::scoped_refptr<MediaStreamInterface> remote_stream =
2474 pc_->remote_streams()->at(0);
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002475 // Track state may be updated asynchronously.
perkjd61bf802016-03-24 03:16:19 -07002476 EXPECT_EQ_WAIT(MediaStreamTrackInterface::kEnded,
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002477 remote_stream->GetAudioTracks()[0]->state(), kTimeout);
2478 EXPECT_EQ_WAIT(MediaStreamTrackInterface::kEnded,
2479 remote_stream->GetVideoTracks()[0]->state(), kTimeout);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002480}
2481
2482// Test that PeerConnection methods fails gracefully after
2483// PeerConnection::Close has been called.
2484TEST_F(PeerConnectionInterfaceTest, CloseAndTestMethods) {
deadbeef293e9262017-01-11 12:28:30 -08002485 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002486 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
2487 CreateOfferAsRemoteDescription();
2488 CreateAnswerAsLocalDescription();
2489
2490 ASSERT_EQ(1u, pc_->local_streams()->count());
zhihuang9763d562016-08-05 11:14:50 -07002491 rtc::scoped_refptr<MediaStreamInterface> local_stream =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002492 pc_->local_streams()->at(0);
2493
2494 pc_->Close();
2495
2496 pc_->RemoveStream(local_stream);
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +00002497 EXPECT_FALSE(pc_->AddStream(local_stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002498
2499 ASSERT_FALSE(local_stream->GetAudioTracks().empty());
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002500 rtc::scoped_refptr<webrtc::DtmfSenderInterface> dtmf_sender(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002501 pc_->CreateDtmfSender(local_stream->GetAudioTracks()[0]));
wu@webrtc.org66037362013-08-13 00:09:35 +00002502 EXPECT_TRUE(NULL == dtmf_sender); // local stream has been removed.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002503
2504 EXPECT_TRUE(pc_->CreateDataChannel("test", NULL) == NULL);
2505
2506 EXPECT_TRUE(pc_->local_description() != NULL);
2507 EXPECT_TRUE(pc_->remote_description() != NULL);
2508
kwibergd1fe2812016-04-27 06:47:29 -07002509 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07002510 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
kwibergd1fe2812016-04-27 06:47:29 -07002511 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -07002512 EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002513
2514 std::string sdp;
2515 ASSERT_TRUE(pc_->remote_description()->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07002516 std::unique_ptr<SessionDescriptionInterface> remote_offer(
2517 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer, sdp,
2518 nullptr));
2519 EXPECT_FALSE(DoSetRemoteDescription(std::move(remote_offer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002520
2521 ASSERT_TRUE(pc_->local_description()->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07002522 std::unique_ptr<SessionDescriptionInterface> local_offer(
2523 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer, sdp,
2524 nullptr));
2525 EXPECT_FALSE(DoSetLocalDescription(std::move(local_offer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002526}
2527
2528// Test that GetStats can still be called after PeerConnection::Close.
2529TEST_F(PeerConnectionInterfaceTest, CloseAndGetStats) {
2530 InitiateCall();
2531 pc_->Close();
2532 DoGetStats(NULL);
2533}
deadbeefab9b2d12015-10-14 11:33:11 -07002534
2535// NOTE: The series of tests below come from what used to be
2536// mediastreamsignaling_unittest.cc, and are mostly aimed at testing that
2537// setting a remote or local description has the expected effects.
2538
2539// This test verifies that the remote MediaStreams corresponding to a received
2540// SDP string is created. In this test the two separate MediaStreams are
2541// signaled.
2542TEST_F(PeerConnectionInterfaceTest, UpdateRemoteStreams) {
2543 FakeConstraints constraints;
2544 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2545 true);
2546 CreatePeerConnection(&constraints);
2547 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2548
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002549 rtc::scoped_refptr<StreamCollection> reference(CreateStreamCollection(1, 1));
deadbeefab9b2d12015-10-14 11:33:11 -07002550 EXPECT_TRUE(
2551 CompareStreamCollections(observer_.remote_streams(), reference.get()));
2552 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2553 EXPECT_TRUE(remote_stream->GetVideoTracks()[0]->GetSource() != nullptr);
2554
2555 // Create a session description based on another SDP with another
2556 // MediaStream.
2557 CreateAndSetRemoteOffer(kSdpStringWithStream1And2);
2558
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002559 rtc::scoped_refptr<StreamCollection> reference2(CreateStreamCollection(2, 1));
deadbeefab9b2d12015-10-14 11:33:11 -07002560 EXPECT_TRUE(
2561 CompareStreamCollections(observer_.remote_streams(), reference2.get()));
2562}
2563
2564// This test verifies that when remote tracks are added/removed from SDP, the
2565// created remote streams are updated appropriately.
2566TEST_F(PeerConnectionInterfaceTest,
2567 AddRemoveTrackFromExistingRemoteMediaStream) {
2568 FakeConstraints constraints;
2569 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2570 true);
2571 CreatePeerConnection(&constraints);
kwibergd1fe2812016-04-27 06:47:29 -07002572 std::unique_ptr<SessionDescriptionInterface> desc_ms1 =
kwiberg2bbff992016-03-16 11:03:04 -07002573 CreateSessionDescriptionAndReference(1, 1);
Steve Antondb45ca82017-09-11 18:27:34 -07002574 EXPECT_TRUE(DoSetRemoteDescription(std::move(desc_ms1)));
deadbeefab9b2d12015-10-14 11:33:11 -07002575 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
2576 reference_collection_));
2577
2578 // Add extra audio and video tracks to the same MediaStream.
kwibergd1fe2812016-04-27 06:47:29 -07002579 std::unique_ptr<SessionDescriptionInterface> desc_ms1_two_tracks =
kwiberg2bbff992016-03-16 11:03:04 -07002580 CreateSessionDescriptionAndReference(2, 2);
Steve Antondb45ca82017-09-11 18:27:34 -07002581 EXPECT_TRUE(DoSetRemoteDescription(std::move(desc_ms1_two_tracks)));
deadbeefab9b2d12015-10-14 11:33:11 -07002582 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
2583 reference_collection_));
zhihuang9763d562016-08-05 11:14:50 -07002584 rtc::scoped_refptr<AudioTrackInterface> audio_track2 =
perkjd61bf802016-03-24 03:16:19 -07002585 observer_.remote_streams()->at(0)->GetAudioTracks()[1];
2586 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, audio_track2->state());
zhihuang9763d562016-08-05 11:14:50 -07002587 rtc::scoped_refptr<VideoTrackInterface> video_track2 =
perkjd61bf802016-03-24 03:16:19 -07002588 observer_.remote_streams()->at(0)->GetVideoTracks()[1];
2589 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, video_track2->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002590
2591 // Remove the extra audio and video tracks.
kwibergd1fe2812016-04-27 06:47:29 -07002592 std::unique_ptr<SessionDescriptionInterface> desc_ms2 =
kwiberg2bbff992016-03-16 11:03:04 -07002593 CreateSessionDescriptionAndReference(1, 1);
perkjd61bf802016-03-24 03:16:19 -07002594 MockTrackObserver audio_track_observer(audio_track2);
2595 MockTrackObserver video_track_observer(video_track2);
2596
2597 EXPECT_CALL(audio_track_observer, OnChanged()).Times(Exactly(1));
2598 EXPECT_CALL(video_track_observer, OnChanged()).Times(Exactly(1));
Steve Antondb45ca82017-09-11 18:27:34 -07002599 EXPECT_TRUE(DoSetRemoteDescription(std::move(desc_ms2)));
deadbeefab9b2d12015-10-14 11:33:11 -07002600 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
2601 reference_collection_));
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002602 // Track state may be updated asynchronously.
perkjd61bf802016-03-24 03:16:19 -07002603 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002604 audio_track2->state(), kTimeout);
2605 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
2606 video_track2->state(), kTimeout);
deadbeefab9b2d12015-10-14 11:33:11 -07002607}
2608
2609// This tests that remote tracks are ended if a local session description is set
2610// that rejects the media content type.
2611TEST_F(PeerConnectionInterfaceTest, RejectMediaContent) {
2612 FakeConstraints constraints;
2613 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2614 true);
2615 CreatePeerConnection(&constraints);
2616 // First create and set a remote offer, then reject its video content in our
2617 // answer.
2618 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2619 ASSERT_EQ(1u, observer_.remote_streams()->count());
2620 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2621 ASSERT_EQ(1u, remote_stream->GetVideoTracks().size());
2622 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2623
2624 rtc::scoped_refptr<webrtc::VideoTrackInterface> remote_video =
2625 remote_stream->GetVideoTracks()[0];
2626 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, remote_video->state());
2627 rtc::scoped_refptr<webrtc::AudioTrackInterface> remote_audio =
2628 remote_stream->GetAudioTracks()[0];
2629 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, remote_audio->state());
2630
kwibergd1fe2812016-04-27 06:47:29 -07002631 std::unique_ptr<SessionDescriptionInterface> local_answer;
kwiberg2bbff992016-03-16 11:03:04 -07002632 EXPECT_TRUE(DoCreateAnswer(&local_answer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07002633 cricket::ContentInfo* video_info =
2634 local_answer->description()->GetContentByName("video");
2635 video_info->rejected = true;
Steve Antondb45ca82017-09-11 18:27:34 -07002636 EXPECT_TRUE(DoSetLocalDescription(std::move(local_answer)));
deadbeefab9b2d12015-10-14 11:33:11 -07002637 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kEnded, remote_video->state());
2638 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, remote_audio->state());
2639
2640 // Now create an offer where we reject both video and audio.
kwibergd1fe2812016-04-27 06:47:29 -07002641 std::unique_ptr<SessionDescriptionInterface> local_offer;
kwiberg2bbff992016-03-16 11:03:04 -07002642 EXPECT_TRUE(DoCreateOffer(&local_offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07002643 video_info = local_offer->description()->GetContentByName("video");
2644 ASSERT_TRUE(video_info != nullptr);
2645 video_info->rejected = true;
2646 cricket::ContentInfo* audio_info =
2647 local_offer->description()->GetContentByName("audio");
2648 ASSERT_TRUE(audio_info != nullptr);
2649 audio_info->rejected = true;
Steve Antondb45ca82017-09-11 18:27:34 -07002650 EXPECT_TRUE(DoSetLocalDescription(std::move(local_offer)));
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002651 // Track state may be updated asynchronously.
perkjd61bf802016-03-24 03:16:19 -07002652 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002653 remote_audio->state(), kTimeout);
2654 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
2655 remote_video->state(), kTimeout);
deadbeefab9b2d12015-10-14 11:33:11 -07002656}
2657
2658// This tests that we won't crash if the remote track has been removed outside
2659// of PeerConnection and then PeerConnection tries to reject the track.
2660TEST_F(PeerConnectionInterfaceTest, RemoveTrackThenRejectMediaContent) {
2661 FakeConstraints constraints;
2662 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2663 true);
2664 CreatePeerConnection(&constraints);
2665 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2666 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2667 remote_stream->RemoveTrack(remote_stream->GetVideoTracks()[0]);
2668 remote_stream->RemoveTrack(remote_stream->GetAudioTracks()[0]);
2669
kwibergd1fe2812016-04-27 06:47:29 -07002670 std::unique_ptr<SessionDescriptionInterface> local_answer(
deadbeefab9b2d12015-10-14 11:33:11 -07002671 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
2672 kSdpStringWithStream1, nullptr));
2673 cricket::ContentInfo* video_info =
2674 local_answer->description()->GetContentByName("video");
2675 video_info->rejected = true;
2676 cricket::ContentInfo* audio_info =
2677 local_answer->description()->GetContentByName("audio");
2678 audio_info->rejected = true;
Steve Antondb45ca82017-09-11 18:27:34 -07002679 EXPECT_TRUE(DoSetLocalDescription(std::move(local_answer)));
deadbeefab9b2d12015-10-14 11:33:11 -07002680
2681 // No crash is a pass.
2682}
2683
deadbeef5e97fb52015-10-15 12:49:08 -07002684// This tests that if a recvonly remote description is set, no remote streams
2685// will be created, even if the description contains SSRCs/MSIDs.
2686// See: https://code.google.com/p/webrtc/issues/detail?id=5054
2687TEST_F(PeerConnectionInterfaceTest, RecvonlyDescriptionDoesntCreateStream) {
2688 FakeConstraints constraints;
2689 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2690 true);
2691 CreatePeerConnection(&constraints);
2692
2693 std::string recvonly_offer = kSdpStringWithStream1;
2694 rtc::replace_substrs(kSendrecv, strlen(kSendrecv), kRecvonly,
2695 strlen(kRecvonly), &recvonly_offer);
2696 CreateAndSetRemoteOffer(recvonly_offer);
2697
2698 EXPECT_EQ(0u, observer_.remote_streams()->count());
2699}
2700
deadbeefab9b2d12015-10-14 11:33:11 -07002701// This tests that a default MediaStream is created if a remote session
2702// description doesn't contain any streams and no MSID support.
2703// It also tests that the default stream is updated if a video m-line is added
2704// in a subsequent session description.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002705TEST_F(PeerConnectionInterfaceTest, SdpWithoutMsidCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002706 FakeConstraints constraints;
2707 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2708 true);
2709 CreatePeerConnection(&constraints);
2710 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2711
2712 ASSERT_EQ(1u, observer_.remote_streams()->count());
2713 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2714
2715 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2716 EXPECT_EQ(0u, remote_stream->GetVideoTracks().size());
2717 EXPECT_EQ("default", remote_stream->label());
2718
2719 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2720 ASSERT_EQ(1u, observer_.remote_streams()->count());
2721 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2722 EXPECT_EQ("defaulta0", remote_stream->GetAudioTracks()[0]->id());
deadbeef884f5852016-01-15 09:20:04 -08002723 EXPECT_EQ(MediaStreamTrackInterface::kLive,
2724 remote_stream->GetAudioTracks()[0]->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002725 ASSERT_EQ(1u, remote_stream->GetVideoTracks().size());
2726 EXPECT_EQ("defaultv0", remote_stream->GetVideoTracks()[0]->id());
deadbeef884f5852016-01-15 09:20:04 -08002727 EXPECT_EQ(MediaStreamTrackInterface::kLive,
2728 remote_stream->GetVideoTracks()[0]->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002729}
2730
2731// This tests that a default MediaStream is created if a remote session
2732// description doesn't contain any streams and media direction is send only.
2733TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002734 SendOnlySdpWithoutMsidCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002735 FakeConstraints constraints;
2736 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2737 true);
2738 CreatePeerConnection(&constraints);
2739 CreateAndSetRemoteOffer(kSdpStringSendOnlyWithoutStreams);
2740
2741 ASSERT_EQ(1u, observer_.remote_streams()->count());
2742 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2743
2744 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2745 EXPECT_EQ(1u, remote_stream->GetVideoTracks().size());
2746 EXPECT_EQ("default", remote_stream->label());
2747}
2748
2749// This tests that it won't crash when PeerConnection tries to remove
2750// a remote track that as already been removed from the MediaStream.
2751TEST_F(PeerConnectionInterfaceTest, RemoveAlreadyGoneRemoteStream) {
2752 FakeConstraints constraints;
2753 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2754 true);
2755 CreatePeerConnection(&constraints);
2756 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2757 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2758 remote_stream->RemoveTrack(remote_stream->GetAudioTracks()[0]);
2759 remote_stream->RemoveTrack(remote_stream->GetVideoTracks()[0]);
2760
2761 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2762
2763 // No crash is a pass.
2764}
2765
2766// This tests that a default MediaStream is created if the remote session
2767// description doesn't contain any streams and don't contain an indication if
2768// MSID is supported.
2769TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002770 SdpWithoutMsidAndStreamsCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002771 FakeConstraints constraints;
2772 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2773 true);
2774 CreatePeerConnection(&constraints);
2775 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2776
2777 ASSERT_EQ(1u, observer_.remote_streams()->count());
2778 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2779 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2780 EXPECT_EQ(1u, remote_stream->GetVideoTracks().size());
2781}
2782
2783// This tests that a default MediaStream is not created if the remote session
2784// description doesn't contain any streams but does support MSID.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002785TEST_F(PeerConnectionInterfaceTest, SdpWithMsidDontCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002786 FakeConstraints constraints;
2787 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2788 true);
2789 CreatePeerConnection(&constraints);
2790 CreateAndSetRemoteOffer(kSdpStringWithMsidWithoutStreams);
2791 EXPECT_EQ(0u, observer_.remote_streams()->count());
2792}
2793
deadbeefbda7e0b2015-12-08 17:13:40 -08002794// This tests that when setting a new description, the old default tracks are
2795// not destroyed and recreated.
2796// See: https://bugs.chromium.org/p/webrtc/issues/detail?id=5250
Stefan Holmer102362b2016-03-18 09:39:07 +01002797TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002798 DefaultTracksNotDestroyedAndRecreated) {
deadbeefbda7e0b2015-12-08 17:13:40 -08002799 FakeConstraints constraints;
2800 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2801 true);
2802 CreatePeerConnection(&constraints);
2803 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2804
2805 ASSERT_EQ(1u, observer_.remote_streams()->count());
2806 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2807 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2808
2809 // Set the track to "disabled", then set a new description and ensure the
2810 // track is still disabled, which ensures it hasn't been recreated.
2811 remote_stream->GetAudioTracks()[0]->set_enabled(false);
2812 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2813 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2814 EXPECT_FALSE(remote_stream->GetAudioTracks()[0]->enabled());
2815}
2816
deadbeefab9b2d12015-10-14 11:33:11 -07002817// This tests that a default MediaStream is not created if a remote session
2818// description is updated to not have any MediaStreams.
2819TEST_F(PeerConnectionInterfaceTest, VerifyDefaultStreamIsNotCreated) {
2820 FakeConstraints constraints;
2821 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2822 true);
2823 CreatePeerConnection(&constraints);
2824 CreateAndSetRemoteOffer(kSdpStringWithStream1);
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002825 rtc::scoped_refptr<StreamCollection> reference(CreateStreamCollection(1, 1));
deadbeefab9b2d12015-10-14 11:33:11 -07002826 EXPECT_TRUE(
2827 CompareStreamCollections(observer_.remote_streams(), reference.get()));
2828
2829 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2830 EXPECT_EQ(0u, observer_.remote_streams()->count());
2831}
2832
2833// This tests that an RtpSender is created when the local description is set
2834// after adding a local stream.
2835// TODO(deadbeef): This test and the one below it need to be updated when
2836// an RtpSender's lifetime isn't determined by when a local description is set.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002837TEST_F(PeerConnectionInterfaceTest, LocalDescriptionChanged) {
deadbeefab9b2d12015-10-14 11:33:11 -07002838 FakeConstraints constraints;
2839 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2840 true);
2841 CreatePeerConnection(&constraints);
deadbeefab9b2d12015-10-14 11:33:11 -07002842
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002843 // Create an offer with 1 stream with 2 tracks of each type.
2844 rtc::scoped_refptr<StreamCollection> stream_collection =
2845 CreateStreamCollection(1, 2);
2846 pc_->AddStream(stream_collection->at(0));
2847 std::unique_ptr<SessionDescriptionInterface> offer;
2848 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07002849 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07002850
deadbeefab9b2d12015-10-14 11:33:11 -07002851 auto senders = pc_->GetSenders();
2852 EXPECT_EQ(4u, senders.size());
2853 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2854 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2855 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[1]));
2856 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[1]));
2857
2858 // Remove an audio and video track.
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002859 pc_->RemoveStream(stream_collection->at(0));
2860 stream_collection = CreateStreamCollection(1, 1);
2861 pc_->AddStream(stream_collection->at(0));
2862 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07002863 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002864
deadbeefab9b2d12015-10-14 11:33:11 -07002865 senders = pc_->GetSenders();
2866 EXPECT_EQ(2u, senders.size());
2867 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2868 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2869 EXPECT_FALSE(ContainsSender(senders, kAudioTracks[1]));
2870 EXPECT_FALSE(ContainsSender(senders, kVideoTracks[1]));
2871}
2872
2873// This tests that an RtpSender is created when the local description is set
2874// before adding a local stream.
2875TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002876 AddLocalStreamAfterLocalDescriptionChanged) {
deadbeefab9b2d12015-10-14 11:33:11 -07002877 FakeConstraints constraints;
2878 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2879 true);
2880 CreatePeerConnection(&constraints);
deadbeefab9b2d12015-10-14 11:33:11 -07002881
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002882 rtc::scoped_refptr<StreamCollection> stream_collection =
2883 CreateStreamCollection(1, 2);
2884 // Add a stream to create the offer, but remove it afterwards.
2885 pc_->AddStream(stream_collection->at(0));
2886 std::unique_ptr<SessionDescriptionInterface> offer;
2887 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2888 pc_->RemoveStream(stream_collection->at(0));
deadbeefab9b2d12015-10-14 11:33:11 -07002889
Steve Antondb45ca82017-09-11 18:27:34 -07002890 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07002891 auto senders = pc_->GetSenders();
2892 EXPECT_EQ(0u, senders.size());
2893
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002894 pc_->AddStream(stream_collection->at(0));
deadbeefab9b2d12015-10-14 11:33:11 -07002895 senders = pc_->GetSenders();
2896 EXPECT_EQ(4u, senders.size());
2897 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2898 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2899 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[1]));
2900 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[1]));
2901}
2902
2903// This tests that the expected behavior occurs if the SSRC on a local track is
2904// changed when SetLocalDescription is called.
2905TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002906 ChangeSsrcOnTrackInLocalSessionDescription) {
deadbeefab9b2d12015-10-14 11:33:11 -07002907 FakeConstraints constraints;
2908 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2909 true);
2910 CreatePeerConnection(&constraints);
deadbeefab9b2d12015-10-14 11:33:11 -07002911
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002912 rtc::scoped_refptr<StreamCollection> stream_collection =
2913 CreateStreamCollection(2, 1);
2914 pc_->AddStream(stream_collection->at(0));
2915 std::unique_ptr<SessionDescriptionInterface> offer;
2916 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2917 // Grab a copy of the offer before it gets passed into the PC.
Steve Antondb45ca82017-09-11 18:27:34 -07002918 std::unique_ptr<webrtc::JsepSessionDescription> modified_offer(
2919 new webrtc::JsepSessionDescription(SessionDescriptionInterface::kOffer));
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002920 modified_offer->Initialize(offer->description()->Copy(), offer->session_id(),
2921 offer->session_version());
Steve Antondb45ca82017-09-11 18:27:34 -07002922 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07002923
deadbeefab9b2d12015-10-14 11:33:11 -07002924 auto senders = pc_->GetSenders();
2925 EXPECT_EQ(2u, senders.size());
2926 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2927 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2928
2929 // Change the ssrc of the audio and video track.
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002930 cricket::MediaContentDescription* desc =
2931 cricket::GetFirstAudioContentDescription(modified_offer->description());
2932 ASSERT_TRUE(desc != NULL);
2933 for (StreamParams& stream : desc->mutable_streams()) {
2934 for (unsigned int& ssrc : stream.ssrcs) {
2935 ++ssrc;
2936 }
2937 }
deadbeefab9b2d12015-10-14 11:33:11 -07002938
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002939 desc =
2940 cricket::GetFirstVideoContentDescription(modified_offer->description());
2941 ASSERT_TRUE(desc != NULL);
2942 for (StreamParams& stream : desc->mutable_streams()) {
2943 for (unsigned int& ssrc : stream.ssrcs) {
2944 ++ssrc;
2945 }
2946 }
2947
Steve Antondb45ca82017-09-11 18:27:34 -07002948 EXPECT_TRUE(DoSetLocalDescription(std::move(modified_offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07002949 senders = pc_->GetSenders();
2950 EXPECT_EQ(2u, senders.size());
2951 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2952 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2953 // TODO(deadbeef): Once RtpSenders expose parameters, check that the SSRC
2954 // changed.
2955}
2956
2957// This tests that the expected behavior occurs if a new session description is
2958// set with the same tracks, but on a different MediaStream.
Stefan Holmer55d6e7c2016-03-17 16:26:40 +01002959TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002960 SignalSameTracksInSeparateMediaStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002961 FakeConstraints constraints;
2962 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2963 true);
2964 CreatePeerConnection(&constraints);
deadbeefab9b2d12015-10-14 11:33:11 -07002965
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002966 rtc::scoped_refptr<StreamCollection> stream_collection =
2967 CreateStreamCollection(2, 1);
2968 pc_->AddStream(stream_collection->at(0));
2969 std::unique_ptr<SessionDescriptionInterface> offer;
2970 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07002971 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07002972
deadbeefab9b2d12015-10-14 11:33:11 -07002973 auto senders = pc_->GetSenders();
2974 EXPECT_EQ(2u, senders.size());
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002975 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0], kStreams[0]));
2976 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0], kStreams[0]));
deadbeefab9b2d12015-10-14 11:33:11 -07002977
2978 // Add a new MediaStream but with the same tracks as in the first stream.
2979 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream_1(
2980 webrtc::MediaStream::Create(kStreams[1]));
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002981 stream_1->AddTrack(stream_collection->at(0)->GetVideoTracks()[0]);
2982 stream_1->AddTrack(stream_collection->at(0)->GetAudioTracks()[0]);
deadbeefab9b2d12015-10-14 11:33:11 -07002983 pc_->AddStream(stream_1);
2984
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002985 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07002986 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07002987
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002988 auto new_senders = pc_->GetSenders();
2989 // Should be the same senders as before, but with updated stream id.
2990 // Note that this behavior is subject to change in the future.
2991 // We may decide the PC should ignore existing tracks in AddStream.
2992 EXPECT_EQ(senders, new_senders);
2993 EXPECT_TRUE(ContainsSender(new_senders, kAudioTracks[0], kStreams[1]));
2994 EXPECT_TRUE(ContainsSender(new_senders, kVideoTracks[0], kStreams[1]));
deadbeefab9b2d12015-10-14 11:33:11 -07002995}
2996
zhihuang81c3a032016-11-17 12:06:24 -08002997// This tests that PeerConnectionObserver::OnAddTrack is correctly called.
2998TEST_F(PeerConnectionInterfaceTest, OnAddTrackCallback) {
2999 FakeConstraints constraints;
3000 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
3001 true);
3002 CreatePeerConnection(&constraints);
3003 CreateAndSetRemoteOffer(kSdpStringWithStream1AudioTrackOnly);
3004 EXPECT_EQ(observer_.num_added_tracks_, 1);
3005 EXPECT_EQ(observer_.last_added_track_label_, kAudioTracks[0]);
3006
3007 // Create and set the updated remote SDP.
3008 CreateAndSetRemoteOffer(kSdpStringWithStream1);
3009 EXPECT_EQ(observer_.num_added_tracks_, 2);
3010 EXPECT_EQ(observer_.last_added_track_label_, kVideoTracks[0]);
3011}
3012
deadbeefd1a38b52016-12-10 13:15:33 -08003013// Test that when SetConfiguration is called and the configuration is
3014// changing, the next offer causes an ICE restart.
3015TEST_F(PeerConnectionInterfaceTest, SetConfigurationCausingIceRetart) {
3016 PeerConnectionInterface::RTCConfiguration config;
3017 config.type = PeerConnectionInterface::kRelay;
3018 // Need to pass default constraints to prevent disabling of DTLS...
3019 FakeConstraints default_constraints;
3020 CreatePeerConnection(config, &default_constraints);
3021 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
3022
3023 // Do initial offer/answer so there's something to restart.
3024 CreateOfferAsLocalDescription();
3025 CreateAnswerAsRemoteDescription(kSdpStringWithStream1);
3026
3027 // Grab the ufrags.
3028 std::vector<std::string> initial_ufrags = GetUfrags(pc_->local_description());
3029
3030 // Change ICE policy, which should trigger an ICE restart on the next offer.
3031 config.type = PeerConnectionInterface::kAll;
3032 EXPECT_TRUE(pc_->SetConfiguration(config));
3033 CreateOfferAsLocalDescription();
3034
3035 // Grab the new ufrags.
3036 std::vector<std::string> subsequent_ufrags =
3037 GetUfrags(pc_->local_description());
3038
3039 // Sanity check.
3040 EXPECT_EQ(initial_ufrags.size(), subsequent_ufrags.size());
3041 // Check that each ufrag is different.
3042 for (int i = 0; i < static_cast<int>(initial_ufrags.size()); ++i) {
3043 EXPECT_NE(initial_ufrags[i], subsequent_ufrags[i]);
3044 }
3045}
3046
3047// Test that when SetConfiguration is called and the configuration *isn't*
3048// changing, the next offer does *not* cause an ICE restart.
3049TEST_F(PeerConnectionInterfaceTest, SetConfigurationNotCausingIceRetart) {
3050 PeerConnectionInterface::RTCConfiguration config;
3051 config.type = PeerConnectionInterface::kRelay;
3052 // Need to pass default constraints to prevent disabling of DTLS...
3053 FakeConstraints default_constraints;
3054 CreatePeerConnection(config, &default_constraints);
3055 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
3056
3057 // Do initial offer/answer so there's something to restart.
3058 CreateOfferAsLocalDescription();
3059 CreateAnswerAsRemoteDescription(kSdpStringWithStream1);
3060
3061 // Grab the ufrags.
3062 std::vector<std::string> initial_ufrags = GetUfrags(pc_->local_description());
3063
3064 // Call SetConfiguration with a config identical to what the PC was
3065 // constructed with.
3066 EXPECT_TRUE(pc_->SetConfiguration(config));
3067 CreateOfferAsLocalDescription();
3068
3069 // Grab the new ufrags.
3070 std::vector<std::string> subsequent_ufrags =
3071 GetUfrags(pc_->local_description());
3072
3073 EXPECT_EQ(initial_ufrags, subsequent_ufrags);
3074}
3075
3076// Test for a weird corner case scenario:
3077// 1. Audio/video session established.
3078// 2. SetConfiguration changes ICE config; ICE restart needed.
3079// 3. ICE restart initiated by remote peer, but only for one m= section.
3080// 4. Next createOffer should initiate an ICE restart, but only for the other
3081// m= section; it would be pointless to do an ICE restart for the m= section
3082// that was already restarted.
3083TEST_F(PeerConnectionInterfaceTest, SetConfigurationCausingPartialIceRestart) {
3084 PeerConnectionInterface::RTCConfiguration config;
3085 config.type = PeerConnectionInterface::kRelay;
3086 // Need to pass default constraints to prevent disabling of DTLS...
3087 FakeConstraints default_constraints;
3088 CreatePeerConnection(config, &default_constraints);
3089 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
3090
3091 // Do initial offer/answer so there's something to restart.
3092 CreateOfferAsLocalDescription();
3093 CreateAnswerAsRemoteDescription(kSdpStringWithStream1);
3094
3095 // Change ICE policy, which should set the "needs-ice-restart" flag.
3096 config.type = PeerConnectionInterface::kAll;
3097 EXPECT_TRUE(pc_->SetConfiguration(config));
3098
3099 // Do ICE restart for the first m= section, initiated by remote peer.
Steve Antondb45ca82017-09-11 18:27:34 -07003100 std::unique_ptr<webrtc::SessionDescriptionInterface> remote_offer(
3101 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
3102 kSdpStringWithStream1, nullptr));
3103 ASSERT_TRUE(remote_offer);
deadbeefd1a38b52016-12-10 13:15:33 -08003104 remote_offer->description()->transport_infos()[0].description.ice_ufrag =
3105 "modified";
Steve Antondb45ca82017-09-11 18:27:34 -07003106 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_offer)));
deadbeefd1a38b52016-12-10 13:15:33 -08003107 CreateAnswerAsLocalDescription();
3108
3109 // Grab the ufrags.
3110 std::vector<std::string> initial_ufrags = GetUfrags(pc_->local_description());
3111 ASSERT_EQ(2, initial_ufrags.size());
3112
3113 // Create offer and grab the new ufrags.
3114 CreateOfferAsLocalDescription();
3115 std::vector<std::string> subsequent_ufrags =
3116 GetUfrags(pc_->local_description());
3117 ASSERT_EQ(2, subsequent_ufrags.size());
3118
3119 // Ensure that only the ufrag for the second m= section changed.
3120 EXPECT_EQ(initial_ufrags[0], subsequent_ufrags[0]);
3121 EXPECT_NE(initial_ufrags[1], subsequent_ufrags[1]);
3122}
3123
deadbeeffe4a8a42016-12-20 17:56:17 -08003124// Tests that the methods to return current/pending descriptions work as
3125// expected at different points in the offer/answer exchange. This test does
3126// one offer/answer exchange as the offerer, then another as the answerer.
3127TEST_F(PeerConnectionInterfaceTest, CurrentAndPendingDescriptions) {
3128 // This disables DTLS so we can apply an answer to ourselves.
3129 CreatePeerConnection();
3130
3131 // Create initial local offer and get SDP (which will also be used as
3132 // answer/pranswer);
Steve Antondb45ca82017-09-11 18:27:34 -07003133 std::unique_ptr<SessionDescriptionInterface> local_offer;
3134 ASSERT_TRUE(DoCreateOffer(&local_offer, nullptr));
deadbeeffe4a8a42016-12-20 17:56:17 -08003135 std::string sdp;
Steve Antondb45ca82017-09-11 18:27:34 -07003136 EXPECT_TRUE(local_offer->ToString(&sdp));
deadbeeffe4a8a42016-12-20 17:56:17 -08003137
3138 // Set local offer.
Steve Antondb45ca82017-09-11 18:27:34 -07003139 SessionDescriptionInterface* local_offer_ptr = local_offer.get();
3140 EXPECT_TRUE(DoSetLocalDescription(std::move(local_offer)));
3141 EXPECT_EQ(local_offer_ptr, pc_->pending_local_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003142 EXPECT_EQ(nullptr, pc_->pending_remote_description());
3143 EXPECT_EQ(nullptr, pc_->current_local_description());
3144 EXPECT_EQ(nullptr, pc_->current_remote_description());
3145
3146 // Set remote pranswer.
Steve Antondb45ca82017-09-11 18:27:34 -07003147 std::unique_ptr<SessionDescriptionInterface> remote_pranswer(
deadbeeffe4a8a42016-12-20 17:56:17 -08003148 webrtc::CreateSessionDescription(SessionDescriptionInterface::kPrAnswer,
Steve Antondb45ca82017-09-11 18:27:34 -07003149 sdp, nullptr));
3150 SessionDescriptionInterface* remote_pranswer_ptr = remote_pranswer.get();
3151 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_pranswer)));
3152 EXPECT_EQ(local_offer_ptr, pc_->pending_local_description());
3153 EXPECT_EQ(remote_pranswer_ptr, pc_->pending_remote_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003154 EXPECT_EQ(nullptr, pc_->current_local_description());
3155 EXPECT_EQ(nullptr, pc_->current_remote_description());
3156
3157 // Set remote answer.
Steve Antondb45ca82017-09-11 18:27:34 -07003158 std::unique_ptr<SessionDescriptionInterface> remote_answer(
3159 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
3160 sdp, nullptr));
3161 SessionDescriptionInterface* remote_answer_ptr = remote_answer.get();
3162 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_answer)));
deadbeeffe4a8a42016-12-20 17:56:17 -08003163 EXPECT_EQ(nullptr, pc_->pending_local_description());
3164 EXPECT_EQ(nullptr, pc_->pending_remote_description());
Steve Antondb45ca82017-09-11 18:27:34 -07003165 EXPECT_EQ(local_offer_ptr, pc_->current_local_description());
3166 EXPECT_EQ(remote_answer_ptr, pc_->current_remote_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003167
3168 // Set remote offer.
Steve Antondb45ca82017-09-11 18:27:34 -07003169 std::unique_ptr<SessionDescriptionInterface> remote_offer(
3170 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer, sdp,
3171 nullptr));
3172 SessionDescriptionInterface* remote_offer_ptr = remote_offer.get();
3173 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_offer)));
3174 EXPECT_EQ(remote_offer_ptr, pc_->pending_remote_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003175 EXPECT_EQ(nullptr, pc_->pending_local_description());
Steve Antondb45ca82017-09-11 18:27:34 -07003176 EXPECT_EQ(local_offer_ptr, pc_->current_local_description());
3177 EXPECT_EQ(remote_answer_ptr, pc_->current_remote_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003178
3179 // Set local pranswer.
Steve Antondb45ca82017-09-11 18:27:34 -07003180 std::unique_ptr<SessionDescriptionInterface> local_pranswer(
deadbeeffe4a8a42016-12-20 17:56:17 -08003181 webrtc::CreateSessionDescription(SessionDescriptionInterface::kPrAnswer,
Steve Antondb45ca82017-09-11 18:27:34 -07003182 sdp, nullptr));
3183 SessionDescriptionInterface* local_pranswer_ptr = local_pranswer.get();
3184 EXPECT_TRUE(DoSetLocalDescription(std::move(local_pranswer)));
3185 EXPECT_EQ(remote_offer_ptr, pc_->pending_remote_description());
3186 EXPECT_EQ(local_pranswer_ptr, pc_->pending_local_description());
3187 EXPECT_EQ(local_offer_ptr, pc_->current_local_description());
3188 EXPECT_EQ(remote_answer_ptr, pc_->current_remote_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003189
3190 // Set local answer.
Steve Antondb45ca82017-09-11 18:27:34 -07003191 std::unique_ptr<SessionDescriptionInterface> local_answer(
3192 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
3193 sdp, nullptr));
3194 SessionDescriptionInterface* local_answer_ptr = local_answer.get();
3195 EXPECT_TRUE(DoSetLocalDescription(std::move(local_answer)));
deadbeeffe4a8a42016-12-20 17:56:17 -08003196 EXPECT_EQ(nullptr, pc_->pending_remote_description());
3197 EXPECT_EQ(nullptr, pc_->pending_local_description());
Steve Antondb45ca82017-09-11 18:27:34 -07003198 EXPECT_EQ(remote_offer_ptr, pc_->current_remote_description());
3199 EXPECT_EQ(local_answer_ptr, pc_->current_local_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003200}
3201
zhihuang77985012017-02-07 15:45:16 -08003202// Tests that it won't crash when calling StartRtcEventLog or StopRtcEventLog
3203// after the PeerConnection is closed.
3204TEST_F(PeerConnectionInterfaceTest,
3205 StartAndStopLoggingAfterPeerConnectionClosed) {
3206 CreatePeerConnection();
3207 // The RtcEventLog will be reset when the PeerConnection is closed.
3208 pc_->Close();
3209
3210 rtc::PlatformFile file = 0;
3211 int64_t max_size_bytes = 1024;
3212 EXPECT_FALSE(pc_->StartRtcEventLog(file, max_size_bytes));
3213 pc_->StopRtcEventLog();
3214}
3215
deadbeef30952b42017-04-21 02:41:29 -07003216// Test that generated offers/answers include "ice-option:trickle".
3217TEST_F(PeerConnectionInterfaceTest, OffersAndAnswersHaveTrickleIceOption) {
3218 CreatePeerConnection();
3219
3220 // First, create an offer with audio/video.
3221 FakeConstraints constraints;
3222 constraints.SetMandatoryReceiveAudio(true);
3223 constraints.SetMandatoryReceiveVideo(true);
3224 std::unique_ptr<SessionDescriptionInterface> offer;
3225 ASSERT_TRUE(DoCreateOffer(&offer, &constraints));
3226 cricket::SessionDescription* desc = offer->description();
3227 ASSERT_EQ(2u, desc->transport_infos().size());
3228 EXPECT_TRUE(desc->transport_infos()[0].description.HasOption("trickle"));
3229 EXPECT_TRUE(desc->transport_infos()[1].description.HasOption("trickle"));
3230
3231 // Apply the offer as a remote description, then create an answer.
Steve Antondb45ca82017-09-11 18:27:34 -07003232 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
deadbeef30952b42017-04-21 02:41:29 -07003233 std::unique_ptr<SessionDescriptionInterface> answer;
3234 ASSERT_TRUE(DoCreateAnswer(&answer, &constraints));
3235 desc = answer->description();
3236 ASSERT_EQ(2u, desc->transport_infos().size());
3237 EXPECT_TRUE(desc->transport_infos()[0].description.HasOption("trickle"));
3238 EXPECT_TRUE(desc->transport_infos()[1].description.HasOption("trickle"));
3239}
3240
deadbeef1dcb1642017-03-29 21:08:16 -07003241// Test that ICE renomination isn't offered if it's not enabled in the PC's
3242// RTCConfiguration.
3243TEST_F(PeerConnectionInterfaceTest, IceRenominationNotOffered) {
3244 PeerConnectionInterface::RTCConfiguration config;
3245 config.enable_ice_renomination = false;
3246 CreatePeerConnection(config, nullptr);
3247 AddVoiceStream("foo");
3248
3249 std::unique_ptr<SessionDescriptionInterface> offer;
3250 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
3251 cricket::SessionDescription* desc = offer->description();
3252 EXPECT_EQ(1u, desc->transport_infos().size());
3253 EXPECT_FALSE(
3254 desc->transport_infos()[0].description.GetIceParameters().renomination);
3255}
3256
3257// Test that the ICE renomination option is present in generated offers/answers
3258// if it's enabled in the PC's RTCConfiguration.
3259TEST_F(PeerConnectionInterfaceTest, IceRenominationOptionInOfferAndAnswer) {
3260 PeerConnectionInterface::RTCConfiguration config;
3261 config.enable_ice_renomination = true;
3262 CreatePeerConnection(config, nullptr);
3263 AddVoiceStream("foo");
3264
3265 std::unique_ptr<SessionDescriptionInterface> offer;
3266 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
3267 cricket::SessionDescription* desc = offer->description();
3268 EXPECT_EQ(1u, desc->transport_infos().size());
3269 EXPECT_TRUE(
3270 desc->transport_infos()[0].description.GetIceParameters().renomination);
3271
3272 // Set the offer as a remote description, then create an answer and ensure it
3273 // has the renomination flag too.
Steve Antondb45ca82017-09-11 18:27:34 -07003274 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
deadbeef1dcb1642017-03-29 21:08:16 -07003275 std::unique_ptr<SessionDescriptionInterface> answer;
3276 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
3277 desc = answer->description();
3278 EXPECT_EQ(1u, desc->transport_infos().size());
3279 EXPECT_TRUE(
3280 desc->transport_infos()[0].description.GetIceParameters().renomination);
3281}
3282
3283// Test that if CreateOffer is called with the deprecated "offer to receive
3284// audio/video" constraints, they're processed and result in an offer with
3285// audio/video sections just as if RTCOfferAnswerOptions had been used.
3286TEST_F(PeerConnectionInterfaceTest, CreateOfferWithOfferToReceiveConstraints) {
3287 CreatePeerConnection();
3288
3289 FakeConstraints constraints;
3290 constraints.SetMandatoryReceiveAudio(true);
3291 constraints.SetMandatoryReceiveVideo(true);
3292 std::unique_ptr<SessionDescriptionInterface> offer;
3293 ASSERT_TRUE(DoCreateOffer(&offer, &constraints));
3294
3295 cricket::SessionDescription* desc = offer->description();
3296 const cricket::ContentInfo* audio = cricket::GetFirstAudioContent(desc);
3297 const cricket::ContentInfo* video = cricket::GetFirstVideoContent(desc);
3298 ASSERT_NE(nullptr, audio);
3299 ASSERT_NE(nullptr, video);
3300 EXPECT_FALSE(audio->rejected);
3301 EXPECT_FALSE(video->rejected);
3302}
3303
3304// Test that if CreateAnswer is called with the deprecated "offer to receive
3305// audio/video" constraints, they're processed and can be used to reject an
3306// offered m= section just as can be done with RTCOfferAnswerOptions;
3307TEST_F(PeerConnectionInterfaceTest, CreateAnswerWithOfferToReceiveConstraints) {
3308 CreatePeerConnection();
3309
3310 // First, create an offer with audio/video and apply it as a remote
3311 // description.
3312 FakeConstraints constraints;
3313 constraints.SetMandatoryReceiveAudio(true);
3314 constraints.SetMandatoryReceiveVideo(true);
3315 std::unique_ptr<SessionDescriptionInterface> offer;
3316 ASSERT_TRUE(DoCreateOffer(&offer, &constraints));
Steve Antondb45ca82017-09-11 18:27:34 -07003317 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
deadbeef1dcb1642017-03-29 21:08:16 -07003318
3319 // Now create answer that rejects audio/video.
3320 constraints.SetMandatoryReceiveAudio(false);
3321 constraints.SetMandatoryReceiveVideo(false);
3322 std::unique_ptr<SessionDescriptionInterface> answer;
3323 ASSERT_TRUE(DoCreateAnswer(&answer, &constraints));
3324
3325 cricket::SessionDescription* desc = answer->description();
3326 const cricket::ContentInfo* audio = cricket::GetFirstAudioContent(desc);
3327 const cricket::ContentInfo* video = cricket::GetFirstVideoContent(desc);
3328 ASSERT_NE(nullptr, audio);
3329 ASSERT_NE(nullptr, video);
3330 EXPECT_TRUE(audio->rejected);
3331 EXPECT_TRUE(video->rejected);
3332}
3333
3334#ifdef HAVE_SCTP
3335#define MAYBE_DataChannelOnlyOfferWithMaxBundlePolicy \
3336 DataChannelOnlyOfferWithMaxBundlePolicy
3337#else
3338#define MAYBE_DataChannelOnlyOfferWithMaxBundlePolicy \
3339 DISABLED_DataChannelOnlyOfferWithMaxBundlePolicy
3340#endif
3341
3342// Test that negotiation can succeed with a data channel only, and with the max
3343// bundle policy. Previously there was a bug that prevented this.
3344TEST_F(PeerConnectionInterfaceTest,
3345 MAYBE_DataChannelOnlyOfferWithMaxBundlePolicy) {
3346 PeerConnectionInterface::RTCConfiguration config;
3347 config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
3348 CreatePeerConnection(config, nullptr);
3349
3350 // First, create an offer with only a data channel and apply it as a remote
3351 // description.
3352 pc_->CreateDataChannel("test", nullptr);
3353 std::unique_ptr<SessionDescriptionInterface> offer;
3354 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07003355 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
deadbeef1dcb1642017-03-29 21:08:16 -07003356
3357 // Create and set answer as well.
3358 std::unique_ptr<SessionDescriptionInterface> answer;
3359 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07003360 EXPECT_TRUE(DoSetLocalDescription(std::move(answer)));
deadbeef1dcb1642017-03-29 21:08:16 -07003361}
3362
zstein4b979802017-06-02 14:37:37 -07003363TEST_F(PeerConnectionInterfaceTest, SetBitrateWithoutMinSucceeds) {
3364 CreatePeerConnection();
3365 PeerConnectionInterface::BitrateParameters bitrate;
3366 bitrate.current_bitrate_bps = rtc::Optional<int>(100000);
3367 EXPECT_TRUE(pc_->SetBitrate(bitrate).ok());
3368}
3369
3370TEST_F(PeerConnectionInterfaceTest, SetBitrateNegativeMinFails) {
3371 CreatePeerConnection();
3372 PeerConnectionInterface::BitrateParameters bitrate;
3373 bitrate.min_bitrate_bps = rtc::Optional<int>(-1);
3374 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3375}
3376
3377TEST_F(PeerConnectionInterfaceTest, SetBitrateCurrentLessThanMinFails) {
3378 CreatePeerConnection();
3379 PeerConnectionInterface::BitrateParameters bitrate;
3380 bitrate.min_bitrate_bps = rtc::Optional<int>(5);
3381 bitrate.current_bitrate_bps = rtc::Optional<int>(3);
3382 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3383}
3384
3385TEST_F(PeerConnectionInterfaceTest, SetBitrateCurrentNegativeFails) {
3386 CreatePeerConnection();
3387 PeerConnectionInterface::BitrateParameters bitrate;
3388 bitrate.current_bitrate_bps = rtc::Optional<int>(-1);
3389 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3390}
3391
3392TEST_F(PeerConnectionInterfaceTest, SetBitrateMaxLessThanCurrentFails) {
3393 CreatePeerConnection();
3394 PeerConnectionInterface::BitrateParameters bitrate;
3395 bitrate.current_bitrate_bps = rtc::Optional<int>(10);
3396 bitrate.max_bitrate_bps = rtc::Optional<int>(8);
3397 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3398}
3399
3400TEST_F(PeerConnectionInterfaceTest, SetBitrateMaxLessThanMinFails) {
3401 CreatePeerConnection();
3402 PeerConnectionInterface::BitrateParameters bitrate;
3403 bitrate.min_bitrate_bps = rtc::Optional<int>(10);
3404 bitrate.max_bitrate_bps = rtc::Optional<int>(8);
3405 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3406}
3407
3408TEST_F(PeerConnectionInterfaceTest, SetBitrateMaxNegativeFails) {
3409 CreatePeerConnection();
3410 PeerConnectionInterface::BitrateParameters bitrate;
3411 bitrate.max_bitrate_bps = rtc::Optional<int>(-1);
3412 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3413}
3414
Steve Anton038834f2017-07-14 15:59:59 -07003415// ice_regather_interval_range requires WebRTC to be configured for continual
3416// gathering already.
3417TEST_F(PeerConnectionInterfaceTest,
3418 SetIceRegatherIntervalRangeWithoutContinualGatheringFails) {
3419 PeerConnectionInterface::RTCConfiguration config;
3420 config.ice_regather_interval_range.emplace(1000, 2000);
3421 config.continual_gathering_policy =
3422 PeerConnectionInterface::ContinualGatheringPolicy::GATHER_ONCE;
3423 CreatePeerConnectionExpectFail(config);
3424}
3425
3426// Ensures that there is no error when ice_regather_interval_range is set with
3427// continual gathering enabled.
3428TEST_F(PeerConnectionInterfaceTest,
3429 SetIceRegatherIntervalRangeWithContinualGathering) {
3430 PeerConnectionInterface::RTCConfiguration config;
3431 config.ice_regather_interval_range.emplace(1000, 2000);
3432 config.continual_gathering_policy =
3433 PeerConnectionInterface::ContinualGatheringPolicy::GATHER_CONTINUALLY;
3434 CreatePeerConnection(config, nullptr);
3435}
3436
zstein4b979802017-06-02 14:37:37 -07003437// The current bitrate from Call's BitrateConfigMask is currently clamped by
3438// Call's BitrateConfig, which comes from the SDP or a default value. This test
3439// checks that a call to SetBitrate with a current bitrate that will be clamped
3440// succeeds.
3441TEST_F(PeerConnectionInterfaceTest, SetBitrateCurrentLessThanImplicitMin) {
3442 CreatePeerConnection();
3443 PeerConnectionInterface::BitrateParameters bitrate;
3444 bitrate.current_bitrate_bps = rtc::Optional<int>(1);
3445 EXPECT_TRUE(pc_->SetBitrate(bitrate).ok());
3446}
3447
zhihuang1c378ed2017-08-17 14:10:50 -07003448// The following tests verify that the offer can be created correctly.
3449TEST_F(PeerConnectionInterfaceTest,
3450 CreateOfferFailsWithInvalidOfferToReceiveAudio) {
3451 RTCOfferAnswerOptions rtc_options;
3452
3453 // Setting offer_to_receive_audio to a value lower than kUndefined or greater
3454 // than kMaxOfferToReceiveMedia should be treated as invalid.
3455 rtc_options.offer_to_receive_audio = RTCOfferAnswerOptions::kUndefined - 1;
3456 CreatePeerConnection();
3457 EXPECT_FALSE(CreateOfferWithOptions(rtc_options));
3458
3459 rtc_options.offer_to_receive_audio =
3460 RTCOfferAnswerOptions::kMaxOfferToReceiveMedia + 1;
3461 EXPECT_FALSE(CreateOfferWithOptions(rtc_options));
3462}
3463
3464TEST_F(PeerConnectionInterfaceTest,
3465 CreateOfferFailsWithInvalidOfferToReceiveVideo) {
3466 RTCOfferAnswerOptions rtc_options;
3467
3468 // Setting offer_to_receive_video to a value lower than kUndefined or greater
3469 // than kMaxOfferToReceiveMedia should be treated as invalid.
3470 rtc_options.offer_to_receive_video = RTCOfferAnswerOptions::kUndefined - 1;
3471 CreatePeerConnection();
3472 EXPECT_FALSE(CreateOfferWithOptions(rtc_options));
3473
3474 rtc_options.offer_to_receive_video =
3475 RTCOfferAnswerOptions::kMaxOfferToReceiveMedia + 1;
3476 EXPECT_FALSE(CreateOfferWithOptions(rtc_options));
3477}
3478
3479// Test that the audio and video content will be added to an offer if both
3480// |offer_to_receive_audio| and |offer_to_receive_video| options are 1.
3481TEST_F(PeerConnectionInterfaceTest, CreateOfferWithAudioVideoOptions) {
3482 RTCOfferAnswerOptions rtc_options;
3483 rtc_options.offer_to_receive_audio = 1;
3484 rtc_options.offer_to_receive_video = 1;
3485
3486 std::unique_ptr<SessionDescriptionInterface> offer;
3487 CreatePeerConnection();
3488 offer = CreateOfferWithOptions(rtc_options);
3489 ASSERT_TRUE(offer);
3490 EXPECT_NE(nullptr, GetFirstAudioContent(offer->description()));
3491 EXPECT_NE(nullptr, GetFirstVideoContent(offer->description()));
3492}
3493
3494// Test that only audio content will be added to the offer if only
3495// |offer_to_receive_audio| options is 1.
3496TEST_F(PeerConnectionInterfaceTest, CreateOfferWithAudioOnlyOptions) {
3497 RTCOfferAnswerOptions rtc_options;
3498 rtc_options.offer_to_receive_audio = 1;
3499 rtc_options.offer_to_receive_video = 0;
3500
3501 std::unique_ptr<SessionDescriptionInterface> offer;
3502 CreatePeerConnection();
3503 offer = CreateOfferWithOptions(rtc_options);
3504 ASSERT_TRUE(offer);
3505 EXPECT_NE(nullptr, GetFirstAudioContent(offer->description()));
3506 EXPECT_EQ(nullptr, GetFirstVideoContent(offer->description()));
3507}
3508
3509// Test that only video content will be added if only |offer_to_receive_video|
3510// options is 1.
3511TEST_F(PeerConnectionInterfaceTest, CreateOfferWithVideoOnlyOptions) {
3512 RTCOfferAnswerOptions rtc_options;
3513 rtc_options.offer_to_receive_audio = 0;
3514 rtc_options.offer_to_receive_video = 1;
3515
3516 std::unique_ptr<SessionDescriptionInterface> offer;
3517 CreatePeerConnection();
3518 offer = CreateOfferWithOptions(rtc_options);
3519 ASSERT_TRUE(offer);
3520 EXPECT_EQ(nullptr, GetFirstAudioContent(offer->description()));
3521 EXPECT_NE(nullptr, GetFirstVideoContent(offer->description()));
3522}
3523
3524// Test that if |voice_activity_detection| is false, no CN codec is added to the
3525// offer.
3526TEST_F(PeerConnectionInterfaceTest, CreateOfferWithVADOptions) {
3527 RTCOfferAnswerOptions rtc_options;
3528 rtc_options.offer_to_receive_audio = 1;
3529 rtc_options.offer_to_receive_video = 0;
3530
3531 std::unique_ptr<SessionDescriptionInterface> offer;
3532 CreatePeerConnection();
3533 offer = CreateOfferWithOptions(rtc_options);
3534 ASSERT_TRUE(offer);
3535 const cricket::ContentInfo* audio_content =
3536 offer->description()->GetContentByName(cricket::CN_AUDIO);
3537 ASSERT_TRUE(audio_content);
3538 // |voice_activity_detection| is true by default.
3539 EXPECT_TRUE(HasCNCodecs(audio_content));
3540
3541 rtc_options.voice_activity_detection = false;
3542 CreatePeerConnection();
3543 offer = CreateOfferWithOptions(rtc_options);
3544 ASSERT_TRUE(offer);
3545 audio_content = offer->description()->GetContentByName(cricket::CN_AUDIO);
3546 ASSERT_TRUE(audio_content);
3547 EXPECT_FALSE(HasCNCodecs(audio_content));
3548}
3549
3550// Test that no media content will be added to the offer if using default
3551// RTCOfferAnswerOptions.
3552TEST_F(PeerConnectionInterfaceTest, CreateOfferWithDefaultOfferAnswerOptions) {
3553 RTCOfferAnswerOptions rtc_options;
3554
3555 std::unique_ptr<SessionDescriptionInterface> offer;
3556 CreatePeerConnection();
3557 offer = CreateOfferWithOptions(rtc_options);
3558 ASSERT_TRUE(offer);
3559 EXPECT_EQ(nullptr, GetFirstAudioContent(offer->description()));
3560 EXPECT_EQ(nullptr, GetFirstVideoContent(offer->description()));
3561}
3562
3563// Test that if |ice_restart| is true, the ufrag/pwd will change, otherwise
3564// ufrag/pwd will be the same in the new offer.
3565TEST_F(PeerConnectionInterfaceTest, CreateOfferWithIceRestart) {
3566 RTCOfferAnswerOptions rtc_options;
3567 rtc_options.ice_restart = false;
3568 rtc_options.offer_to_receive_audio = 1;
3569
3570 std::unique_ptr<SessionDescriptionInterface> offer;
3571 CreatePeerConnection();
3572 CreateOfferWithOptionsAsLocalDescription(&offer, rtc_options);
3573 auto ufrag1 = offer->description()
3574 ->GetTransportInfoByName(cricket::CN_AUDIO)
3575 ->description.ice_ufrag;
3576 auto pwd1 = offer->description()
3577 ->GetTransportInfoByName(cricket::CN_AUDIO)
3578 ->description.ice_pwd;
3579
3580 // |ice_restart| is false, the ufrag/pwd shouldn't change.
3581 CreateOfferWithOptionsAsLocalDescription(&offer, rtc_options);
3582 auto ufrag2 = offer->description()
3583 ->GetTransportInfoByName(cricket::CN_AUDIO)
3584 ->description.ice_ufrag;
3585 auto pwd2 = offer->description()
3586 ->GetTransportInfoByName(cricket::CN_AUDIO)
3587 ->description.ice_pwd;
3588
3589 // |ice_restart| is true, the ufrag/pwd should change.
3590 rtc_options.ice_restart = true;
3591 CreateOfferWithOptionsAsLocalDescription(&offer, rtc_options);
3592 auto ufrag3 = offer->description()
3593 ->GetTransportInfoByName(cricket::CN_AUDIO)
3594 ->description.ice_ufrag;
3595 auto pwd3 = offer->description()
3596 ->GetTransportInfoByName(cricket::CN_AUDIO)
3597 ->description.ice_pwd;
3598
3599 EXPECT_EQ(ufrag1, ufrag2);
3600 EXPECT_EQ(pwd1, pwd2);
3601 EXPECT_NE(ufrag2, ufrag3);
3602 EXPECT_NE(pwd2, pwd3);
3603}
3604
3605// Test that if |use_rtp_mux| is true, the bundling will be enabled in the
3606// offer; if it is false, there won't be any bundle group in the offer.
3607TEST_F(PeerConnectionInterfaceTest, CreateOfferWithRtpMux) {
3608 RTCOfferAnswerOptions rtc_options;
3609 rtc_options.offer_to_receive_audio = 1;
3610 rtc_options.offer_to_receive_video = 1;
3611
3612 std::unique_ptr<SessionDescriptionInterface> offer;
3613 CreatePeerConnection();
3614
3615 rtc_options.use_rtp_mux = true;
3616 offer = CreateOfferWithOptions(rtc_options);
3617 ASSERT_TRUE(offer);
3618 EXPECT_NE(nullptr, GetFirstAudioContent(offer->description()));
3619 EXPECT_NE(nullptr, GetFirstVideoContent(offer->description()));
3620 EXPECT_TRUE(offer->description()->HasGroup(cricket::GROUP_TYPE_BUNDLE));
3621
3622 rtc_options.use_rtp_mux = false;
3623 offer = CreateOfferWithOptions(rtc_options);
3624 ASSERT_TRUE(offer);
3625 EXPECT_NE(nullptr, GetFirstAudioContent(offer->description()));
3626 EXPECT_NE(nullptr, GetFirstVideoContent(offer->description()));
3627 EXPECT_FALSE(offer->description()->HasGroup(cricket::GROUP_TYPE_BUNDLE));
3628}
3629
3630// If SetMandatoryReceiveAudio(false) and SetMandatoryReceiveVideo(false) are
3631// called for the answer constraints, but an audio and a video section were
3632// offered, there will still be an audio and a video section in the answer.
3633TEST_F(PeerConnectionInterfaceTest,
3634 RejectAudioAndVideoInAnswerWithConstraints) {
3635 // Offer both audio and video.
3636 RTCOfferAnswerOptions rtc_offer_options;
3637 rtc_offer_options.offer_to_receive_audio = 1;
3638 rtc_offer_options.offer_to_receive_video = 1;
3639
3640 CreatePeerConnection();
3641 std::unique_ptr<SessionDescriptionInterface> offer;
3642 CreateOfferWithOptionsAsRemoteDescription(&offer, rtc_offer_options);
3643 EXPECT_NE(nullptr, GetFirstAudioContent(offer->description()));
3644 EXPECT_NE(nullptr, GetFirstVideoContent(offer->description()));
3645
3646 // Since an offer has been created with both audio and video,
3647 // Answers will contain the media types that exist in the offer regardless of
3648 // the value of |answer_options.has_audio| and |answer_options.has_video|.
3649 FakeConstraints answer_c;
3650 // Reject both audio and video.
3651 answer_c.SetMandatoryReceiveAudio(false);
3652 answer_c.SetMandatoryReceiveVideo(false);
3653
3654 std::unique_ptr<SessionDescriptionInterface> answer;
3655 ASSERT_TRUE(DoCreateAnswer(&answer, &answer_c));
3656 const cricket::ContentInfo* audio_content =
3657 GetFirstAudioContent(answer->description());
3658 const cricket::ContentInfo* video_content =
3659 GetFirstVideoContent(answer->description());
3660 ASSERT_NE(nullptr, audio_content);
3661 ASSERT_NE(nullptr, video_content);
3662 EXPECT_TRUE(audio_content->rejected);
3663 EXPECT_TRUE(video_content->rejected);
3664}
3665
zhihuang141aacb2017-08-29 13:23:53 -07003666// This test ensures OnRenegotiationNeeded is called when we add track with
3667// MediaStream -> AddTrack in the same way it is called when we add track with
3668// PeerConnection -> AddTrack.
3669// The test can be removed once addStream is rewritten in terms of addTrack
3670// https://bugs.chromium.org/p/webrtc/issues/detail?id=7815
3671TEST_F(PeerConnectionInterfaceTest, MediaStreamAddTrackRemoveTrackRenegotiate) {
3672 CreatePeerConnectionWithoutDtls();
3673 rtc::scoped_refptr<MediaStreamInterface> stream(
3674 pc_factory_->CreateLocalMediaStream(kStreamLabel1));
3675 pc_->AddStream(stream);
3676 rtc::scoped_refptr<AudioTrackInterface> audio_track(
3677 pc_factory_->CreateAudioTrack("audio_track", nullptr));
3678 rtc::scoped_refptr<VideoTrackInterface> video_track(
3679 pc_factory_->CreateVideoTrack(
3680 "video_track", pc_factory_->CreateVideoSource(
3681 std::unique_ptr<cricket::VideoCapturer>(
3682 new cricket::FakeVideoCapturer()))));
3683 stream->AddTrack(audio_track);
3684 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
3685 observer_.renegotiation_needed_ = false;
3686
3687 stream->AddTrack(video_track);
3688 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
3689 observer_.renegotiation_needed_ = false;
3690
3691 stream->RemoveTrack(audio_track);
3692 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
3693 observer_.renegotiation_needed_ = false;
3694
3695 stream->RemoveTrack(video_track);
3696 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
3697 observer_.renegotiation_needed_ = false;
3698}
3699
3700// Tests that creating answer would fail gracefully without being crashed if the
3701// remote description is unset.
3702TEST_F(PeerConnectionInterfaceTest, CreateAnswerWithoutRemoteDescription) {
3703 CreatePeerConnection();
3704 // Creating answer fails because the remote description is unset.
3705 std::unique_ptr<SessionDescriptionInterface> answer;
3706 EXPECT_FALSE(DoCreateAnswer(&answer, nullptr));
3707
3708 // Createing answer succeeds when the remote description is set.
3709 CreateOfferAsRemoteDescription();
3710 EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
3711}
3712
Zhi Huang2a5e4262017-09-14 01:15:03 -07003713// Test that an error is returned if a description is applied that doesn't
3714// respect the order of existing media sections.
3715TEST_F(PeerConnectionInterfaceTest,
3716 MediaSectionOrderEnforcedForSubsequentOffers) {
3717 CreatePeerConnection();
3718 FakeConstraints constraints;
3719 constraints.SetMandatoryReceiveAudio(true);
3720 constraints.SetMandatoryReceiveVideo(true);
3721 std::unique_ptr<SessionDescriptionInterface> offer;
3722 ASSERT_TRUE(DoCreateOffer(&offer, &constraints));
3723 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
3724
3725 std::unique_ptr<SessionDescriptionInterface> answer;
3726 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
3727 EXPECT_TRUE(DoSetLocalDescription(std::move(answer)));
3728
3729 // A remote offer with different m=line order should be rejected.
3730 ASSERT_TRUE(DoCreateOffer(&offer, &constraints));
3731 std::reverse(offer->description()->contents().begin(),
3732 offer->description()->contents().end());
3733 std::reverse(offer->description()->transport_infos().begin(),
3734 offer->description()->transport_infos().end());
3735 EXPECT_FALSE(DoSetRemoteDescription(std::move(offer)));
3736
3737 // A subsequent local offer with different m=line order should be rejected.
3738 ASSERT_TRUE(DoCreateOffer(&offer, &constraints));
3739 std::reverse(offer->description()->contents().begin(),
3740 offer->description()->contents().end());
3741 std::reverse(offer->description()->transport_infos().begin(),
3742 offer->description()->transport_infos().end());
3743 EXPECT_FALSE(DoSetLocalDescription(std::move(offer)));
3744}
3745
nisse51542be2016-02-12 02:27:06 -08003746class PeerConnectionMediaConfigTest : public testing::Test {
3747 protected:
3748 void SetUp() override {
zhihuang38ede132017-06-15 12:52:32 -07003749 pcf_ = PeerConnectionFactoryForTest::CreatePeerConnectionFactoryForTest();
nisse51542be2016-02-12 02:27:06 -08003750 pcf_->Initialize();
3751 }
nisseeaabdf62017-05-05 02:23:02 -07003752 const cricket::MediaConfig TestCreatePeerConnection(
nisse51542be2016-02-12 02:27:06 -08003753 const PeerConnectionInterface::RTCConfiguration& config,
zhihuang1c378ed2017-08-17 14:10:50 -07003754 const MediaConstraintsInterface* constraints) {
zhihuang9763d562016-08-05 11:14:50 -07003755 rtc::scoped_refptr<PeerConnectionInterface> pc(pcf_->CreatePeerConnection(
3756 config, constraints, nullptr, nullptr, &observer_));
nisse51542be2016-02-12 02:27:06 -08003757 EXPECT_TRUE(pc.get());
nisseeaabdf62017-05-05 02:23:02 -07003758 return pc->GetConfiguration().media_config;
nisse51542be2016-02-12 02:27:06 -08003759 }
3760
zhihuang9763d562016-08-05 11:14:50 -07003761 rtc::scoped_refptr<PeerConnectionFactoryForTest> pcf_;
nisse51542be2016-02-12 02:27:06 -08003762 MockPeerConnectionObserver observer_;
3763};
3764
3765// This test verifies the default behaviour with no constraints and a
3766// default RTCConfiguration.
3767TEST_F(PeerConnectionMediaConfigTest, TestDefaults) {
3768 PeerConnectionInterface::RTCConfiguration config;
3769 FakeConstraints constraints;
3770
3771 const cricket::MediaConfig& media_config =
3772 TestCreatePeerConnection(config, &constraints);
3773
3774 EXPECT_FALSE(media_config.enable_dscp);
nisse0db023a2016-03-01 04:29:59 -08003775 EXPECT_TRUE(media_config.video.enable_cpu_overuse_detection);
3776 EXPECT_FALSE(media_config.video.disable_prerenderer_smoothing);
3777 EXPECT_FALSE(media_config.video.suspend_below_min_bitrate);
nisse51542be2016-02-12 02:27:06 -08003778}
3779
3780// This test verifies the DSCP constraint is recognized and passed to
nisse528b7932017-05-08 03:21:43 -07003781// the PeerConnection.
nisse51542be2016-02-12 02:27:06 -08003782TEST_F(PeerConnectionMediaConfigTest, TestDscpConstraintTrue) {
3783 PeerConnectionInterface::RTCConfiguration config;
3784 FakeConstraints constraints;
3785
3786 constraints.AddOptional(webrtc::MediaConstraintsInterface::kEnableDscp, true);
3787 const cricket::MediaConfig& media_config =
3788 TestCreatePeerConnection(config, &constraints);
3789
3790 EXPECT_TRUE(media_config.enable_dscp);
3791}
3792
3793// This test verifies the cpu overuse detection constraint is
nisse528b7932017-05-08 03:21:43 -07003794// recognized and passed to the PeerConnection.
nisse51542be2016-02-12 02:27:06 -08003795TEST_F(PeerConnectionMediaConfigTest, TestCpuOveruseConstraintFalse) {
3796 PeerConnectionInterface::RTCConfiguration config;
3797 FakeConstraints constraints;
3798
3799 constraints.AddOptional(
3800 webrtc::MediaConstraintsInterface::kCpuOveruseDetection, false);
3801 const cricket::MediaConfig media_config =
3802 TestCreatePeerConnection(config, &constraints);
3803
nisse0db023a2016-03-01 04:29:59 -08003804 EXPECT_FALSE(media_config.video.enable_cpu_overuse_detection);
nisse51542be2016-02-12 02:27:06 -08003805}
3806
3807// This test verifies that the disable_prerenderer_smoothing flag is
nisse528b7932017-05-08 03:21:43 -07003808// propagated from RTCConfiguration to the PeerConnection.
nisse51542be2016-02-12 02:27:06 -08003809TEST_F(PeerConnectionMediaConfigTest, TestDisablePrerendererSmoothingTrue) {
3810 PeerConnectionInterface::RTCConfiguration config;
3811 FakeConstraints constraints;
3812
Niels Möller71bdda02016-03-31 12:59:59 +02003813 config.set_prerenderer_smoothing(false);
nisse51542be2016-02-12 02:27:06 -08003814 const cricket::MediaConfig& media_config =
3815 TestCreatePeerConnection(config, &constraints);
3816
nisse0db023a2016-03-01 04:29:59 -08003817 EXPECT_TRUE(media_config.video.disable_prerenderer_smoothing);
3818}
3819
3820// This test verifies the suspend below min bitrate constraint is
nisse528b7932017-05-08 03:21:43 -07003821// recognized and passed to the PeerConnection.
nisse0db023a2016-03-01 04:29:59 -08003822TEST_F(PeerConnectionMediaConfigTest,
3823 TestSuspendBelowMinBitrateConstraintTrue) {
3824 PeerConnectionInterface::RTCConfiguration config;
3825 FakeConstraints constraints;
3826
3827 constraints.AddOptional(
3828 webrtc::MediaConstraintsInterface::kEnableVideoSuspendBelowMinBitrate,
3829 true);
3830 const cricket::MediaConfig media_config =
3831 TestCreatePeerConnection(config, &constraints);
3832
3833 EXPECT_TRUE(media_config.video.suspend_below_min_bitrate);
nisse51542be2016-02-12 02:27:06 -08003834}
3835
deadbeef293e9262017-01-11 12:28:30 -08003836// Tests a few random fields being different.
3837TEST(RTCConfigurationTest, ComparisonOperators) {
3838 PeerConnectionInterface::RTCConfiguration a;
3839 PeerConnectionInterface::RTCConfiguration b;
3840 EXPECT_EQ(a, b);
3841
3842 PeerConnectionInterface::RTCConfiguration c;
3843 c.servers.push_back(PeerConnectionInterface::IceServer());
3844 EXPECT_NE(a, c);
3845
3846 PeerConnectionInterface::RTCConfiguration d;
3847 d.type = PeerConnectionInterface::kRelay;
3848 EXPECT_NE(a, d);
3849
3850 PeerConnectionInterface::RTCConfiguration e;
3851 e.audio_jitter_buffer_max_packets = 5;
3852 EXPECT_NE(a, e);
3853
3854 PeerConnectionInterface::RTCConfiguration f;
3855 f.ice_connection_receiving_timeout = 1337;
3856 EXPECT_NE(a, f);
3857
3858 PeerConnectionInterface::RTCConfiguration g;
3859 g.disable_ipv6 = true;
3860 EXPECT_NE(a, g);
3861
3862 PeerConnectionInterface::RTCConfiguration h(
3863 PeerConnectionInterface::RTCConfigurationType::kAggressive);
3864 EXPECT_NE(a, h);
3865}