blob: 8413b79dd070d1773051d14b1a1aa6e83b8e2f1a [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"
Elad Alon99c3fe52017-10-13 16:29:40 +020025#include "logging/rtc_event_log/output/rtc_event_log_output_file.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020026#include "media/base/fakevideocapturer.h"
27#include "media/engine/webrtcmediaengine.h"
28#include "media/sctp/sctptransportinternal.h"
29#include "modules/audio_processing/include/audio_processing.h"
30#include "p2p/base/fakeportallocator.h"
31#include "pc/audiotrack.h"
32#include "pc/mediasession.h"
33#include "pc/mediastream.h"
34#include "pc/peerconnection.h"
Steve Anton57858b32018-02-15 15:19:50 -080035#include "pc/rtpsender.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020036#include "pc/streamcollection.h"
37#include "pc/test/fakeaudiocapturemodule.h"
38#include "pc/test/fakertccertificategenerator.h"
39#include "pc/test/fakevideotracksource.h"
40#include "pc/test/mockpeerconnectionobservers.h"
41#include "pc/test/testsdpstrings.h"
42#include "pc/videocapturertracksource.h"
43#include "pc/videotrack.h"
44#include "rtc_base/gunit.h"
Elad Alon99c3fe52017-10-13 16:29:40 +020045#include "rtc_base/ptr_util.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020046#include "rtc_base/stringutils.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020047#include "rtc_base/virtualsocketserver.h"
48#include "test/gmock.h"
Elad Alon9e6565b2017-10-11 16:04:13 +020049#include "test/testsupport/fileutils.h"
kwibergac9f8762016-09-30 22:29:43 -070050
51#ifdef WEBRTC_ANDROID
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020052#include "pc/test/androidtestinitializer.h"
kwibergac9f8762016-09-30 22:29:43 -070053#endif
henrike@webrtc.org28e20752013-07-10 00:45:36 +000054
55static const char kStreamLabel1[] = "local_stream_1";
56static const char kStreamLabel2[] = "local_stream_2";
57static const char kStreamLabel3[] = "local_stream_3";
58static const int kDefaultStunPort = 3478;
59static const char kStunAddressOnly[] = "stun:address";
60static const char kStunInvalidPort[] = "stun:address:-1";
61static const char kStunAddressPortAndMore1[] = "stun:address:port:more";
62static const char kStunAddressPortAndMore2[] = "stun:address:port more";
63static const char kTurnIceServerUri[] = "turn:user@turn.example.org";
64static const char kTurnUsername[] = "user";
65static const char kTurnPassword[] = "password";
66static const char kTurnHostname[] = "turn.example.org";
Peter Boström0c4e06b2015-10-07 12:23:21 +020067static const uint32_t kTimeout = 10000U;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000068
deadbeefab9b2d12015-10-14 11:33:11 -070069static const char kStreams[][8] = {"stream1", "stream2"};
70static const char kAudioTracks[][32] = {"audiotrack0", "audiotrack1"};
71static const char kVideoTracks[][32] = {"videotrack0", "videotrack1"};
72
deadbeef5e97fb52015-10-15 12:49:08 -070073static const char kRecvonly[] = "recvonly";
74static const char kSendrecv[] = "sendrecv";
75
deadbeefab9b2d12015-10-14 11:33:11 -070076// Reference SDP with a MediaStream with label "stream1" and audio track with
77// id "audio_1" and a video track with id "video_1;
78static const char kSdpStringWithStream1[] =
79 "v=0\r\n"
80 "o=- 0 0 IN IP4 127.0.0.1\r\n"
81 "s=-\r\n"
82 "t=0 0\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -080083 "m=audio 1 RTP/AVPF 103\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -070084 "a=ice-ufrag:e5785931\r\n"
85 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
86 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
87 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -070088 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -070089 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -080090 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -070091 "a=rtpmap:103 ISAC/16000\r\n"
92 "a=ssrc:1 cname:stream1\r\n"
93 "a=ssrc:1 mslabel:stream1\r\n"
94 "a=ssrc:1 label:audiotrack0\r\n"
95 "m=video 1 RTP/AVPF 120\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -080096 "a=ice-ufrag:e5785931\r\n"
97 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
98 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
99 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700100 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700101 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800102 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700103 "a=rtpmap:120 VP8/90000\r\n"
104 "a=ssrc:2 cname:stream1\r\n"
105 "a=ssrc:2 mslabel:stream1\r\n"
106 "a=ssrc:2 label:videotrack0\r\n";
107
zhihuang81c3a032016-11-17 12:06:24 -0800108// Reference SDP with a MediaStream with label "stream1" and audio track with
109// id "audio_1";
110static const char kSdpStringWithStream1AudioTrackOnly[] =
111 "v=0\r\n"
112 "o=- 0 0 IN IP4 127.0.0.1\r\n"
113 "s=-\r\n"
114 "t=0 0\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800115 "m=audio 1 RTP/AVPF 103\r\n"
zhihuang81c3a032016-11-17 12:06:24 -0800116 "a=ice-ufrag:e5785931\r\n"
117 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
118 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
119 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
zhihuang81c3a032016-11-17 12:06:24 -0800120 "a=mid:audio\r\n"
121 "a=sendrecv\r\n"
122 "a=rtpmap:103 ISAC/16000\r\n"
123 "a=ssrc:1 cname:stream1\r\n"
124 "a=ssrc:1 mslabel:stream1\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800125 "a=ssrc:1 label:audiotrack0\r\n"
126 "a=rtcp-mux\r\n";
zhihuang81c3a032016-11-17 12:06:24 -0800127
deadbeefab9b2d12015-10-14 11:33:11 -0700128// Reference SDP with two MediaStreams with label "stream1" and "stream2. Each
129// MediaStreams have one audio track and one video track.
130// This uses MSID.
131static const char kSdpStringWithStream1And2[] =
132 "v=0\r\n"
133 "o=- 0 0 IN IP4 127.0.0.1\r\n"
134 "s=-\r\n"
135 "t=0 0\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800136 "a=msid-semantic: WMS stream1 stream2\r\n"
137 "m=audio 1 RTP/AVPF 103\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700138 "a=ice-ufrag:e5785931\r\n"
139 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
140 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
141 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700142 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700143 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800144 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700145 "a=rtpmap:103 ISAC/16000\r\n"
146 "a=ssrc:1 cname:stream1\r\n"
147 "a=ssrc:1 msid:stream1 audiotrack0\r\n"
148 "a=ssrc:3 cname:stream2\r\n"
149 "a=ssrc:3 msid:stream2 audiotrack1\r\n"
150 "m=video 1 RTP/AVPF 120\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800151 "a=ice-ufrag:e5785931\r\n"
152 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
153 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
154 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700155 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700156 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800157 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700158 "a=rtpmap:120 VP8/0\r\n"
159 "a=ssrc:2 cname:stream1\r\n"
160 "a=ssrc:2 msid:stream1 videotrack0\r\n"
161 "a=ssrc:4 cname:stream2\r\n"
162 "a=ssrc:4 msid:stream2 videotrack1\r\n";
163
164// Reference SDP without MediaStreams. Msid is not supported.
165static const char kSdpStringWithoutStreams[] =
166 "v=0\r\n"
167 "o=- 0 0 IN IP4 127.0.0.1\r\n"
168 "s=-\r\n"
169 "t=0 0\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800170 "m=audio 1 RTP/AVPF 103\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700171 "a=ice-ufrag:e5785931\r\n"
172 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
173 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
174 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700175 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700176 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800177 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700178 "a=rtpmap:103 ISAC/16000\r\n"
179 "m=video 1 RTP/AVPF 120\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800180 "a=ice-ufrag:e5785931\r\n"
181 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
182 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
183 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700184 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700185 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800186 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700187 "a=rtpmap:120 VP8/90000\r\n";
188
189// Reference SDP without MediaStreams. Msid is supported.
190static const char kSdpStringWithMsidWithoutStreams[] =
191 "v=0\r\n"
192 "o=- 0 0 IN IP4 127.0.0.1\r\n"
193 "s=-\r\n"
194 "t=0 0\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800195 "a=msid-semantic: WMS\r\n"
196 "m=audio 1 RTP/AVPF 103\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700197 "a=ice-ufrag:e5785931\r\n"
198 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
199 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
200 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700201 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700202 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800203 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700204 "a=rtpmap:103 ISAC/16000\r\n"
205 "m=video 1 RTP/AVPF 120\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800206 "a=ice-ufrag:e5785931\r\n"
207 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
208 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
209 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700210 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700211 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800212 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700213 "a=rtpmap:120 VP8/90000\r\n";
214
215// Reference SDP without MediaStreams and audio only.
216static const char kSdpStringWithoutStreamsAudioOnly[] =
217 "v=0\r\n"
218 "o=- 0 0 IN IP4 127.0.0.1\r\n"
219 "s=-\r\n"
220 "t=0 0\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800221 "m=audio 1 RTP/AVPF 103\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700222 "a=ice-ufrag:e5785931\r\n"
223 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
224 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
225 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700226 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700227 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800228 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700229 "a=rtpmap:103 ISAC/16000\r\n";
230
231// Reference SENDONLY SDP without MediaStreams. Msid is not supported.
232static const char kSdpStringSendOnlyWithoutStreams[] =
233 "v=0\r\n"
234 "o=- 0 0 IN IP4 127.0.0.1\r\n"
235 "s=-\r\n"
236 "t=0 0\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800237 "m=audio 1 RTP/AVPF 103\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700238 "a=ice-ufrag:e5785931\r\n"
239 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
240 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
241 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700242 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700243 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700244 "a=sendonly\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800245 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700246 "a=rtpmap:103 ISAC/16000\r\n"
247 "m=video 1 RTP/AVPF 120\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800248 "a=ice-ufrag:e5785931\r\n"
249 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
250 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
251 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700252 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700253 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700254 "a=sendonly\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800255 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700256 "a=rtpmap:120 VP8/90000\r\n";
257
258static const char kSdpStringInit[] =
259 "v=0\r\n"
260 "o=- 0 0 IN IP4 127.0.0.1\r\n"
261 "s=-\r\n"
262 "t=0 0\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700263 "a=msid-semantic: WMS\r\n";
264
265static const char kSdpStringAudio[] =
266 "m=audio 1 RTP/AVPF 103\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800267 "a=ice-ufrag:e5785931\r\n"
268 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
269 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
270 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700271 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700272 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800273 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700274 "a=rtpmap:103 ISAC/16000\r\n";
275
276static const char kSdpStringVideo[] =
277 "m=video 1 RTP/AVPF 120\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800278 "a=ice-ufrag:e5785931\r\n"
279 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
280 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
281 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700282 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700283 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800284 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700285 "a=rtpmap:120 VP8/90000\r\n";
286
287static const char kSdpStringMs1Audio0[] =
288 "a=ssrc:1 cname:stream1\r\n"
289 "a=ssrc:1 msid:stream1 audiotrack0\r\n";
290
291static const char kSdpStringMs1Video0[] =
292 "a=ssrc:2 cname:stream1\r\n"
293 "a=ssrc:2 msid:stream1 videotrack0\r\n";
294
295static const char kSdpStringMs1Audio1[] =
296 "a=ssrc:3 cname:stream1\r\n"
297 "a=ssrc:3 msid:stream1 audiotrack1\r\n";
298
299static const char kSdpStringMs1Video1[] =
300 "a=ssrc:4 cname:stream1\r\n"
301 "a=ssrc:4 msid:stream1 videotrack1\r\n";
302
deadbeef8662f942017-01-20 21:20:51 -0800303static const char kDtlsSdesFallbackSdp[] =
304 "v=0\r\n"
305 "o=xxxxxx 7 2 IN IP4 0.0.0.0\r\n"
306 "s=-\r\n"
307 "c=IN IP4 0.0.0.0\r\n"
308 "t=0 0\r\n"
309 "a=group:BUNDLE audio\r\n"
310 "a=msid-semantic: WMS\r\n"
311 "m=audio 1 RTP/SAVPF 0\r\n"
312 "a=sendrecv\r\n"
313 "a=rtcp-mux\r\n"
314 "a=mid:audio\r\n"
315 "a=ssrc:1 cname:stream1\r\n"
316 "a=ssrc:1 mslabel:stream1\r\n"
317 "a=ssrc:1 label:audiotrack0\r\n"
318 "a=ice-ufrag:e5785931\r\n"
319 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
320 "a=rtpmap:0 pcmu/8000\r\n"
321 "a=fingerprint:sha-1 "
322 "4A:AD:B9:B1:3F:82:18:3B:54:02:12:DF:3E:5D:49:6B:19:E5:7C:AB\r\n"
323 "a=setup:actpass\r\n"
Tommi8e545ee2018-02-08 16:25:20 +0000324 "a=crypto:1 AES_CM_128_HMAC_SHA1_32 "
deadbeef8662f942017-01-20 21:20:51 -0800325 "inline:NzB4d1BINUAvLEw6UzF3WSJ+PSdFcGdUJShpX1Zj|2^20|1:32 "
326 "dummy_session_params\r\n";
327
perkjd61bf802016-03-24 03:16:19 -0700328using ::testing::Exactly;
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700329using cricket::StreamParams;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000330using webrtc::AudioSourceInterface;
deadbeefab9b2d12015-10-14 11:33:11 -0700331using webrtc::AudioTrack;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000332using webrtc::AudioTrackInterface;
333using webrtc::DataBuffer;
334using webrtc::DataChannelInterface;
335using webrtc::FakeConstraints;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000336using webrtc::IceCandidateInterface;
deadbeefc80741f2015-10-22 13:14:45 -0700337using webrtc::MediaConstraintsInterface;
deadbeefab9b2d12015-10-14 11:33:11 -0700338using webrtc::MediaStream;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000339using webrtc::MediaStreamInterface;
340using webrtc::MediaStreamTrackInterface;
341using webrtc::MockCreateSessionDescriptionObserver;
342using webrtc::MockDataChannelObserver;
Steve Anton94286cb2017-09-26 16:20:19 -0700343using webrtc::MockPeerConnectionObserver;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000344using webrtc::MockSetSessionDescriptionObserver;
345using webrtc::MockStatsObserver;
perkjd61bf802016-03-24 03:16:19 -0700346using webrtc::NotifierInterface;
347using webrtc::ObserverInterface;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000348using webrtc::PeerConnectionInterface;
349using webrtc::PeerConnectionObserver;
deadbeef293e9262017-01-11 12:28:30 -0800350using webrtc::RTCError;
351using webrtc::RTCErrorType;
deadbeefab9b2d12015-10-14 11:33:11 -0700352using webrtc::RtpReceiverInterface;
353using webrtc::RtpSenderInterface;
Steve Anton57858b32018-02-15 15:19:50 -0800354using webrtc::RtpSenderProxyWithInternal;
355using webrtc::RtpSenderInternal;
Steve Anton4e70a722017-11-28 14:57:10 -0800356using webrtc::RtpTransceiverDirection;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000357using webrtc::SdpParseError;
Steve Antona3a92c22017-12-07 10:27:41 -0800358using webrtc::SdpType;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000359using webrtc::SessionDescriptionInterface;
deadbeefab9b2d12015-10-14 11:33:11 -0700360using webrtc::StreamCollection;
361using webrtc::StreamCollectionInterface;
perkja3ede6c2016-03-08 01:27:48 +0100362using webrtc::VideoTrackSourceInterface;
deadbeefab9b2d12015-10-14 11:33:11 -0700363using webrtc::VideoTrack;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000364using webrtc::VideoTrackInterface;
365
deadbeefab9b2d12015-10-14 11:33:11 -0700366typedef PeerConnectionInterface::RTCOfferAnswerOptions RTCOfferAnswerOptions;
367
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000368namespace {
369
370// Gets the first ssrc of given content type from the ContentInfo.
371bool GetFirstSsrc(const cricket::ContentInfo* content_info, int* ssrc) {
372 if (!content_info || !ssrc) {
373 return false;
374 }
375 const cricket::MediaContentDescription* media_desc =
Steve Antonb1c1de12017-12-21 15:14:30 -0800376 content_info->media_description();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000377 if (!media_desc || media_desc->streams().empty()) {
378 return false;
379 }
380 *ssrc = media_desc->streams().begin()->first_ssrc();
381 return true;
382}
383
deadbeefd1a38b52016-12-10 13:15:33 -0800384// Get the ufrags out of an SDP blob. Useful for testing ICE restart
385// behavior.
386std::vector<std::string> GetUfrags(
387 const webrtc::SessionDescriptionInterface* desc) {
388 std::vector<std::string> ufrags;
389 for (const cricket::TransportInfo& info :
390 desc->description()->transport_infos()) {
391 ufrags.push_back(info.description.ice_ufrag);
392 }
393 return ufrags;
394}
395
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000396void SetSsrcToZero(std::string* sdp) {
397 const char kSdpSsrcAtribute[] = "a=ssrc:";
398 const char kSdpSsrcAtributeZero[] = "a=ssrc:0";
399 size_t ssrc_pos = 0;
400 while ((ssrc_pos = sdp->find(kSdpSsrcAtribute, ssrc_pos)) !=
401 std::string::npos) {
402 size_t end_ssrc = sdp->find(" ", ssrc_pos);
403 sdp->replace(ssrc_pos, end_ssrc - ssrc_pos, kSdpSsrcAtributeZero);
404 ssrc_pos = end_ssrc;
405 }
406}
407
deadbeefab9b2d12015-10-14 11:33:11 -0700408// Check if |streams| contains the specified track.
409bool ContainsTrack(const std::vector<cricket::StreamParams>& streams,
410 const std::string& stream_label,
411 const std::string& track_id) {
412 for (const cricket::StreamParams& params : streams) {
413 if (params.sync_label == stream_label && params.id == track_id) {
414 return true;
415 }
416 }
417 return false;
418}
419
420// Check if |senders| contains the specified sender, by id.
421bool ContainsSender(
422 const std::vector<rtc::scoped_refptr<RtpSenderInterface>>& senders,
423 const std::string& id) {
424 for (const auto& sender : senders) {
425 if (sender->id() == id) {
426 return true;
427 }
428 }
429 return false;
430}
431
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700432// Check if |senders| contains the specified sender, by id and stream id.
433bool ContainsSender(
434 const std::vector<rtc::scoped_refptr<RtpSenderInterface>>& senders,
435 const std::string& id,
436 const std::string& stream_id) {
437 for (const auto& sender : senders) {
deadbeefa601f5c2016-06-06 14:27:39 -0700438 if (sender->id() == id && sender->stream_ids()[0] == stream_id) {
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700439 return true;
440 }
441 }
442 return false;
443}
444
deadbeefab9b2d12015-10-14 11:33:11 -0700445// Create a collection of streams.
446// CreateStreamCollection(1) creates a collection that
447// correspond to kSdpStringWithStream1.
448// CreateStreamCollection(2) correspond to kSdpStringWithStream1And2.
449rtc::scoped_refptr<StreamCollection> CreateStreamCollection(
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700450 int number_of_streams,
451 int tracks_per_stream) {
deadbeefab9b2d12015-10-14 11:33:11 -0700452 rtc::scoped_refptr<StreamCollection> local_collection(
453 StreamCollection::Create());
454
455 for (int i = 0; i < number_of_streams; ++i) {
456 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream(
457 webrtc::MediaStream::Create(kStreams[i]));
458
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700459 for (int j = 0; j < tracks_per_stream; ++j) {
460 // Add a local audio track.
461 rtc::scoped_refptr<webrtc::AudioTrackInterface> audio_track(
462 webrtc::AudioTrack::Create(kAudioTracks[i * tracks_per_stream + j],
463 nullptr));
464 stream->AddTrack(audio_track);
deadbeefab9b2d12015-10-14 11:33:11 -0700465
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700466 // Add a local video track.
467 rtc::scoped_refptr<webrtc::VideoTrackInterface> video_track(
468 webrtc::VideoTrack::Create(kVideoTracks[i * tracks_per_stream + j],
perkj773be362017-07-31 23:22:01 -0700469 webrtc::FakeVideoTrackSource::Create(),
470 rtc::Thread::Current()));
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700471 stream->AddTrack(video_track);
472 }
deadbeefab9b2d12015-10-14 11:33:11 -0700473
474 local_collection->AddStream(stream);
475 }
476 return local_collection;
477}
478
479// Check equality of StreamCollections.
480bool CompareStreamCollections(StreamCollectionInterface* s1,
481 StreamCollectionInterface* s2) {
482 if (s1 == nullptr || s2 == nullptr || s1->count() != s2->count()) {
483 return false;
484 }
485
486 for (size_t i = 0; i != s1->count(); ++i) {
487 if (s1->at(i)->label() != s2->at(i)->label()) {
488 return false;
489 }
490 webrtc::AudioTrackVector audio_tracks1 = s1->at(i)->GetAudioTracks();
491 webrtc::AudioTrackVector audio_tracks2 = s2->at(i)->GetAudioTracks();
492 webrtc::VideoTrackVector video_tracks1 = s1->at(i)->GetVideoTracks();
493 webrtc::VideoTrackVector video_tracks2 = s2->at(i)->GetVideoTracks();
494
495 if (audio_tracks1.size() != audio_tracks2.size()) {
496 return false;
497 }
498 for (size_t j = 0; j != audio_tracks1.size(); ++j) {
499 if (audio_tracks1[j]->id() != audio_tracks2[j]->id()) {
500 return false;
501 }
502 }
503 if (video_tracks1.size() != video_tracks2.size()) {
504 return false;
505 }
506 for (size_t j = 0; j != video_tracks1.size(); ++j) {
507 if (video_tracks1[j]->id() != video_tracks2[j]->id()) {
508 return false;
509 }
510 }
511 }
512 return true;
513}
514
perkjd61bf802016-03-24 03:16:19 -0700515// Helper class to test Observer.
516class MockTrackObserver : public ObserverInterface {
517 public:
518 explicit MockTrackObserver(NotifierInterface* notifier)
519 : notifier_(notifier) {
520 notifier_->RegisterObserver(this);
521 }
522
523 ~MockTrackObserver() { Unregister(); }
524
525 void Unregister() {
526 if (notifier_) {
527 notifier_->UnregisterObserver(this);
528 notifier_ = nullptr;
529 }
530 }
531
532 MOCK_METHOD0(OnChanged, void());
533
534 private:
535 NotifierInterface* notifier_;
536};
537
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000538} // namespace
deadbeefab9b2d12015-10-14 11:33:11 -0700539
nisse528b7932017-05-08 03:21:43 -0700540// The PeerConnectionMediaConfig tests below verify that configuration and
541// constraints are propagated into the PeerConnection's MediaConfig. These
542// settings are intended for MediaChannel constructors, but that is not
543// exercised by these unittest.
zhihuang29ff8442016-07-27 11:07:25 -0700544class PeerConnectionFactoryForTest : public webrtc::PeerConnectionFactory {
545 public:
zhihuang38ede132017-06-15 12:52:32 -0700546 static rtc::scoped_refptr<PeerConnectionFactoryForTest>
547 CreatePeerConnectionFactoryForTest() {
548 auto audio_encoder_factory = webrtc::CreateBuiltinAudioEncoderFactory();
549 auto audio_decoder_factory = webrtc::CreateBuiltinAudioDecoderFactory();
550
henrika919dc2e2017-10-12 14:24:55 +0200551 // Use fake audio device module since we're only testing the interface
552 // level, and using a real one could make tests flaky when run in parallel.
zhihuang38ede132017-06-15 12:52:32 -0700553 auto media_engine = std::unique_ptr<cricket::MediaEngineInterface>(
554 cricket::WebRtcMediaEngineFactory::Create(
henrika919dc2e2017-10-12 14:24:55 +0200555 FakeAudioCaptureModule::Create(), audio_encoder_factory,
556 audio_decoder_factory, nullptr, nullptr, nullptr,
Ivo Creusen62337e52018-01-09 14:17:33 +0100557 webrtc::AudioProcessingBuilder().Create()));
zhihuang38ede132017-06-15 12:52:32 -0700558
559 std::unique_ptr<webrtc::CallFactoryInterface> call_factory =
560 webrtc::CreateCallFactory();
561
562 std::unique_ptr<webrtc::RtcEventLogFactoryInterface> event_log_factory =
563 webrtc::CreateRtcEventLogFactory();
564
565 return new rtc::RefCountedObject<PeerConnectionFactoryForTest>(
566 rtc::Thread::Current(), rtc::Thread::Current(), rtc::Thread::Current(),
Magnus Jedvert02e7a192017-09-23 17:21:32 +0200567 std::move(media_engine), std::move(call_factory),
zhihuang38ede132017-06-15 12:52:32 -0700568 std::move(event_log_factory));
569 }
570
571 PeerConnectionFactoryForTest(
572 rtc::Thread* network_thread,
573 rtc::Thread* worker_thread,
574 rtc::Thread* signaling_thread,
zhihuang38ede132017-06-15 12:52:32 -0700575 std::unique_ptr<cricket::MediaEngineInterface> media_engine,
576 std::unique_ptr<webrtc::CallFactoryInterface> call_factory,
577 std::unique_ptr<webrtc::RtcEventLogFactoryInterface> event_log_factory)
578 : webrtc::PeerConnectionFactory(network_thread,
579 worker_thread,
580 signaling_thread,
zhihuang38ede132017-06-15 12:52:32 -0700581 std::move(media_engine),
582 std::move(call_factory),
583 std::move(event_log_factory)) {}
kwiberg1e4e8cb2017-01-31 01:48:08 -0800584
zhihuang29ff8442016-07-27 11:07:25 -0700585 cricket::TransportController* CreateTransportController(
Honghai Zhangbfd398c2016-08-30 22:07:42 -0700586 cricket::PortAllocator* port_allocator,
Qingsi Wang93a84392018-01-30 17:13:09 -0800587 bool redetermine_role_on_ice_restart,
588 webrtc::RtcEventLog* event_log = nullptr) override {
zhihuang29ff8442016-07-27 11:07:25 -0700589 transport_controller = new cricket::TransportController(
Honghai Zhangbfd398c2016-08-30 22:07:42 -0700590 rtc::Thread::Current(), rtc::Thread::Current(), port_allocator,
Qingsi Wang93a84392018-01-30 17:13:09 -0800591 redetermine_role_on_ice_restart, rtc::CryptoOptions(), event_log);
zhihuang29ff8442016-07-27 11:07:25 -0700592 return transport_controller;
593 }
594
deadbeefd7850b22017-08-23 10:59:19 -0700595 rtc::scoped_refptr<FakeAudioCaptureModule> fake_audio_capture_module_;
zhihuang29ff8442016-07-27 11:07:25 -0700596 cricket::TransportController* transport_controller;
zhihuang29ff8442016-07-27 11:07:25 -0700597};
598
Steve Anton94286cb2017-09-26 16:20:19 -0700599// TODO(steveanton): Convert to use the new PeerConnectionUnitTestFixture and
600// PeerConnectionWrapper.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000601class PeerConnectionInterfaceTest : public testing::Test {
602 protected:
deadbeef9a6f4d42017-05-15 19:43:33 -0700603 PeerConnectionInterfaceTest()
deadbeef98e186c2017-05-16 18:00:06 -0700604 : vss_(new rtc::VirtualSocketServer()), main_(vss_.get()) {
phoglund37ebcf02016-01-08 05:04:57 -0800605#ifdef WEBRTC_ANDROID
606 webrtc::InitializeAndroidObjects();
607#endif
608 }
609
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000610 virtual void SetUp() {
deadbeefd7850b22017-08-23 10:59:19 -0700611 // Use fake audio capture module since we're only testing the interface
612 // level, and using a real one could make tests flaky when run in parallel.
613 fake_audio_capture_module_ = FakeAudioCaptureModule::Create();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000614 pc_factory_ = webrtc::CreatePeerConnectionFactory(
danilchape9021a32016-05-17 01:52:02 -0700615 rtc::Thread::Current(), rtc::Thread::Current(), rtc::Thread::Current(),
Karl Wiberg1b0eae32017-10-17 14:48:54 +0200616 fake_audio_capture_module_, webrtc::CreateBuiltinAudioEncoderFactory(),
617 webrtc::CreateBuiltinAudioDecoderFactory(), nullptr, nullptr);
danilchape9021a32016-05-17 01:52:02 -0700618 ASSERT_TRUE(pc_factory_);
zhihuang29ff8442016-07-27 11:07:25 -0700619 pc_factory_for_test_ =
zhihuang38ede132017-06-15 12:52:32 -0700620 PeerConnectionFactoryForTest::CreatePeerConnectionFactoryForTest();
zhihuang29ff8442016-07-27 11:07:25 -0700621 pc_factory_for_test_->Initialize();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000622 }
623
624 void CreatePeerConnection() {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700625 CreatePeerConnection(PeerConnectionInterface::RTCConfiguration(), nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000626 }
627
deadbeef293e9262017-01-11 12:28:30 -0800628 // DTLS does not work in a loopback call, so is disabled for most of the
629 // tests in this file.
630 void CreatePeerConnectionWithoutDtls() {
631 FakeConstraints no_dtls_constraints;
632 no_dtls_constraints.AddMandatory(
633 webrtc::MediaConstraintsInterface::kEnableDtlsSrtp, false);
634
635 CreatePeerConnection(PeerConnectionInterface::RTCConfiguration(),
636 &no_dtls_constraints);
637 }
638
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000639 void CreatePeerConnection(webrtc::MediaConstraintsInterface* constraints) {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700640 CreatePeerConnection(PeerConnectionInterface::RTCConfiguration(),
641 constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000642 }
643
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700644 void CreatePeerConnectionWithIceTransportsType(
645 PeerConnectionInterface::IceTransportsType type) {
646 PeerConnectionInterface::RTCConfiguration config;
647 config.type = type;
648 return CreatePeerConnection(config, nullptr);
649 }
650
651 void CreatePeerConnectionWithIceServer(const std::string& uri,
652 const std::string& password) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800653 PeerConnectionInterface::RTCConfiguration config;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000654 PeerConnectionInterface::IceServer server;
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700655 server.uri = uri;
656 server.password = password;
657 config.servers.push_back(server);
658 CreatePeerConnection(config, nullptr);
659 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000660
Mirko Bonadeic61ce0d2017-11-21 17:04:20 +0100661 void CreatePeerConnection(
662 const PeerConnectionInterface::RTCConfiguration& config,
663 webrtc::MediaConstraintsInterface* constraints) {
kwibergd1fe2812016-04-27 06:47:29 -0700664 std::unique_ptr<cricket::FakePortAllocator> port_allocator(
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800665 new cricket::FakePortAllocator(rtc::Thread::Current(), nullptr));
666 port_allocator_ = port_allocator.get();
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000667
deadbeef1dcb1642017-03-29 21:08:16 -0700668 // Create certificate generator unless DTLS constraint is explicitly set to
669 // false.
Henrik Boströmd79599d2016-06-01 13:58:50 +0200670 std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator;
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000671 bool dtls;
672 if (FindConstraint(constraints,
673 webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
674 &dtls,
Henrik Boström5e56c592015-08-11 10:33:13 +0200675 nullptr) && dtls) {
deadbeef8662f942017-01-20 21:20:51 -0800676 fake_certificate_generator_ = new FakeRTCCertificateGenerator();
677 cert_generator.reset(fake_certificate_generator_);
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000678 }
Henrik Boströmd79599d2016-06-01 13:58:50 +0200679 pc_ = pc_factory_->CreatePeerConnection(
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800680 config, constraints, std::move(port_allocator),
Henrik Boströmd79599d2016-06-01 13:58:50 +0200681 std::move(cert_generator), &observer_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000682 ASSERT_TRUE(pc_.get() != NULL);
683 observer_.SetPeerConnectionInterface(pc_.get());
684 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
685 }
686
deadbeef0a6c4ca2015-10-06 11:38:28 -0700687 void CreatePeerConnectionExpectFail(const std::string& uri) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800688 PeerConnectionInterface::RTCConfiguration config;
deadbeef0a6c4ca2015-10-06 11:38:28 -0700689 PeerConnectionInterface::IceServer server;
deadbeef0a6c4ca2015-10-06 11:38:28 -0700690 server.uri = uri;
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800691 config.servers.push_back(server);
deadbeef0a6c4ca2015-10-06 11:38:28 -0700692
zhihuang9763d562016-08-05 11:14:50 -0700693 rtc::scoped_refptr<PeerConnectionInterface> pc;
hbosd7973cc2016-05-27 06:08:53 -0700694 pc = pc_factory_->CreatePeerConnection(config, nullptr, nullptr, nullptr,
695 &observer_);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800696 EXPECT_EQ(nullptr, pc);
deadbeef0a6c4ca2015-10-06 11:38:28 -0700697 }
698
Steve Anton038834f2017-07-14 15:59:59 -0700699 void CreatePeerConnectionExpectFail(
700 PeerConnectionInterface::RTCConfiguration config) {
701 PeerConnectionInterface::IceServer server;
702 server.uri = kTurnIceServerUri;
703 server.password = kTurnPassword;
704 config.servers.push_back(server);
705 rtc::scoped_refptr<PeerConnectionInterface> pc =
706 pc_factory_->CreatePeerConnection(config, nullptr, nullptr, &observer_);
707 EXPECT_EQ(nullptr, pc);
708 }
709
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000710 void CreatePeerConnectionWithDifferentConfigurations() {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700711 CreatePeerConnectionWithIceServer(kStunAddressOnly, "");
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800712 EXPECT_EQ(1u, port_allocator_->stun_servers().size());
713 EXPECT_EQ(0u, port_allocator_->turn_servers().size());
714 EXPECT_EQ("address", port_allocator_->stun_servers().begin()->hostname());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000715 EXPECT_EQ(kDefaultStunPort,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800716 port_allocator_->stun_servers().begin()->port());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000717
deadbeef0a6c4ca2015-10-06 11:38:28 -0700718 CreatePeerConnectionExpectFail(kStunInvalidPort);
719 CreatePeerConnectionExpectFail(kStunAddressPortAndMore1);
720 CreatePeerConnectionExpectFail(kStunAddressPortAndMore2);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000721
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700722 CreatePeerConnectionWithIceServer(kTurnIceServerUri, kTurnPassword);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800723 EXPECT_EQ(0u, port_allocator_->stun_servers().size());
724 EXPECT_EQ(1u, port_allocator_->turn_servers().size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000725 EXPECT_EQ(kTurnUsername,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800726 port_allocator_->turn_servers()[0].credentials.username);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000727 EXPECT_EQ(kTurnPassword,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800728 port_allocator_->turn_servers()[0].credentials.password);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000729 EXPECT_EQ(kTurnHostname,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800730 port_allocator_->turn_servers()[0].ports[0].address.hostname());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000731 }
732
733 void ReleasePeerConnection() {
734 pc_ = NULL;
735 observer_.SetPeerConnectionInterface(NULL);
736 }
737
deadbeefab9b2d12015-10-14 11:33:11 -0700738 void AddVideoStream(const std::string& label) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000739 // Create a local stream.
zhihuang9763d562016-08-05 11:14:50 -0700740 rtc::scoped_refptr<MediaStreamInterface> stream(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000741 pc_factory_->CreateLocalMediaStream(label));
zhihuang9763d562016-08-05 11:14:50 -0700742 rtc::scoped_refptr<VideoTrackSourceInterface> video_source(
deadbeef112b2e92017-02-10 20:13:37 -0800743 pc_factory_->CreateVideoSource(std::unique_ptr<cricket::VideoCapturer>(
744 new cricket::FakeVideoCapturer()),
745 NULL));
zhihuang9763d562016-08-05 11:14:50 -0700746 rtc::scoped_refptr<VideoTrackInterface> video_track(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000747 pc_factory_->CreateVideoTrack(label + "v0", video_source));
748 stream->AddTrack(video_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +0000749 EXPECT_TRUE(pc_->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000750 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
751 observer_.renegotiation_needed_ = false;
752 }
753
754 void AddVoiceStream(const std::string& label) {
755 // Create a local stream.
zhihuang9763d562016-08-05 11:14:50 -0700756 rtc::scoped_refptr<MediaStreamInterface> stream(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000757 pc_factory_->CreateLocalMediaStream(label));
zhihuang9763d562016-08-05 11:14:50 -0700758 rtc::scoped_refptr<AudioTrackInterface> audio_track(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000759 pc_factory_->CreateAudioTrack(label + "a0", NULL));
760 stream->AddTrack(audio_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +0000761 EXPECT_TRUE(pc_->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000762 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
763 observer_.renegotiation_needed_ = false;
764 }
765
766 void AddAudioVideoStream(const std::string& stream_label,
767 const std::string& audio_track_label,
768 const std::string& video_track_label) {
769 // Create a local stream.
zhihuang9763d562016-08-05 11:14:50 -0700770 rtc::scoped_refptr<MediaStreamInterface> stream(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000771 pc_factory_->CreateLocalMediaStream(stream_label));
zhihuang9763d562016-08-05 11:14:50 -0700772 rtc::scoped_refptr<AudioTrackInterface> audio_track(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000773 pc_factory_->CreateAudioTrack(
774 audio_track_label, static_cast<AudioSourceInterface*>(NULL)));
775 stream->AddTrack(audio_track.get());
zhihuang9763d562016-08-05 11:14:50 -0700776 rtc::scoped_refptr<VideoTrackInterface> video_track(
nisseaf510af2016-03-21 08:20:42 -0700777 pc_factory_->CreateVideoTrack(
deadbeef112b2e92017-02-10 20:13:37 -0800778 video_track_label, pc_factory_->CreateVideoSource(
779 std::unique_ptr<cricket::VideoCapturer>(
780 new cricket::FakeVideoCapturer()))));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000781 stream->AddTrack(video_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +0000782 EXPECT_TRUE(pc_->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000783 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
784 observer_.renegotiation_needed_ = false;
785 }
786
kwibergd1fe2812016-04-27 06:47:29 -0700787 bool DoCreateOfferAnswer(std::unique_ptr<SessionDescriptionInterface>* desc,
deadbeefc80741f2015-10-22 13:14:45 -0700788 bool offer,
789 MediaConstraintsInterface* constraints) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000790 rtc::scoped_refptr<MockCreateSessionDescriptionObserver>
791 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000792 MockCreateSessionDescriptionObserver>());
793 if (offer) {
deadbeefc80741f2015-10-22 13:14:45 -0700794 pc_->CreateOffer(observer, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000795 } else {
deadbeefc80741f2015-10-22 13:14:45 -0700796 pc_->CreateAnswer(observer, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000797 }
798 EXPECT_EQ_WAIT(true, observer->called(), kTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -0700799 *desc = observer->MoveDescription();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000800 return observer->result();
801 }
802
kwibergd1fe2812016-04-27 06:47:29 -0700803 bool DoCreateOffer(std::unique_ptr<SessionDescriptionInterface>* desc,
deadbeefc80741f2015-10-22 13:14:45 -0700804 MediaConstraintsInterface* constraints) {
805 return DoCreateOfferAnswer(desc, true, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000806 }
807
kwibergd1fe2812016-04-27 06:47:29 -0700808 bool DoCreateAnswer(std::unique_ptr<SessionDescriptionInterface>* desc,
deadbeefc80741f2015-10-22 13:14:45 -0700809 MediaConstraintsInterface* constraints) {
810 return DoCreateOfferAnswer(desc, false, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000811 }
812
Steve Antondb45ca82017-09-11 18:27:34 -0700813 bool DoSetSessionDescription(
814 std::unique_ptr<SessionDescriptionInterface> desc,
815 bool local) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000816 rtc::scoped_refptr<MockSetSessionDescriptionObserver>
817 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000818 MockSetSessionDescriptionObserver>());
819 if (local) {
Steve Antondb45ca82017-09-11 18:27:34 -0700820 pc_->SetLocalDescription(observer, desc.release());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000821 } else {
Steve Antondb45ca82017-09-11 18:27:34 -0700822 pc_->SetRemoteDescription(observer, desc.release());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000823 }
zhihuang29ff8442016-07-27 11:07:25 -0700824 if (pc_->signaling_state() != PeerConnectionInterface::kClosed) {
825 EXPECT_EQ_WAIT(true, observer->called(), kTimeout);
826 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000827 return observer->result();
828 }
829
Steve Antondb45ca82017-09-11 18:27:34 -0700830 bool DoSetLocalDescription(
831 std::unique_ptr<SessionDescriptionInterface> desc) {
832 return DoSetSessionDescription(std::move(desc), true);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000833 }
834
Steve Antondb45ca82017-09-11 18:27:34 -0700835 bool DoSetRemoteDescription(
836 std::unique_ptr<SessionDescriptionInterface> desc) {
837 return DoSetSessionDescription(std::move(desc), false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000838 }
839
840 // Calls PeerConnection::GetStats and check the return value.
841 // It does not verify the values in the StatReports since a RTCP packet might
842 // be required.
843 bool DoGetStats(MediaStreamTrackInterface* track) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000844 rtc::scoped_refptr<MockStatsObserver> observer(
845 new rtc::RefCountedObject<MockStatsObserver>());
jiayl@webrtc.orgdb41b4d2014-03-03 21:30:06 +0000846 if (!pc_->GetStats(
847 observer, track, PeerConnectionInterface::kStatsOutputLevelStandard))
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000848 return false;
849 EXPECT_TRUE_WAIT(observer->called(), kTimeout);
850 return observer->called();
851 }
852
Harald Alvestrand89061872018-01-02 14:08:34 +0100853 // Call the standards-compliant GetStats function.
854 bool DoGetRTCStats() {
855 rtc::scoped_refptr<webrtc::MockRTCStatsCollectorCallback> callback(
856 new rtc::RefCountedObject<webrtc::MockRTCStatsCollectorCallback>());
857 pc_->GetStats(callback);
858 EXPECT_TRUE_WAIT(callback->called(), kTimeout);
859 return callback->called();
860 }
861
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000862 void InitiateCall() {
deadbeef293e9262017-01-11 12:28:30 -0800863 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000864 // Create a local stream with audio&video tracks.
865 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
866 CreateOfferReceiveAnswer();
867 }
868
869 // Verify that RTP Header extensions has been negotiated for audio and video.
870 void VerifyRemoteRtpHeaderExtensions() {
871 const cricket::MediaContentDescription* desc =
872 cricket::GetFirstAudioContentDescription(
873 pc_->remote_description()->description());
874 ASSERT_TRUE(desc != NULL);
875 EXPECT_GT(desc->rtp_header_extensions().size(), 0u);
876
877 desc = cricket::GetFirstVideoContentDescription(
878 pc_->remote_description()->description());
879 ASSERT_TRUE(desc != NULL);
880 EXPECT_GT(desc->rtp_header_extensions().size(), 0u);
881 }
882
883 void CreateOfferAsRemoteDescription() {
kwibergd1fe2812016-04-27 06:47:29 -0700884 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -0700885 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000886 std::string sdp;
887 EXPECT_TRUE(offer->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -0700888 std::unique_ptr<SessionDescriptionInterface> remote_offer(
Steve Antona3a92c22017-12-07 10:27:41 -0800889 webrtc::CreateSessionDescription(SdpType::kOffer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -0700890 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_offer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000891 EXPECT_EQ(PeerConnectionInterface::kHaveRemoteOffer, observer_.state_);
892 }
893
deadbeefab9b2d12015-10-14 11:33:11 -0700894 void CreateAndSetRemoteOffer(const std::string& sdp) {
Steve Antondb45ca82017-09-11 18:27:34 -0700895 std::unique_ptr<SessionDescriptionInterface> remote_offer(
Steve Antona3a92c22017-12-07 10:27:41 -0800896 webrtc::CreateSessionDescription(SdpType::kOffer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -0700897 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_offer)));
deadbeefab9b2d12015-10-14 11:33:11 -0700898 EXPECT_EQ(PeerConnectionInterface::kHaveRemoteOffer, observer_.state_);
899 }
900
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000901 void CreateAnswerAsLocalDescription() {
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 // TODO(perkj): Currently SetLocalDescription fails if any parameters in an
906 // audio codec change, even if the parameter has nothing to do with
907 // receiving. Not all parameters are serialized to SDP.
908 // Since CreatePrAnswerAsLocalDescription serialize/deserialize
909 // the SessionDescription, it is necessary to do that here to in order to
910 // get ReceiveOfferCreatePrAnswerAndAnswer and RenegotiateAudioOnly to pass.
911 // https://code.google.com/p/webrtc/issues/detail?id=1356
912 std::string sdp;
913 EXPECT_TRUE(answer->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -0700914 std::unique_ptr<SessionDescriptionInterface> new_answer(
Steve Antona3a92c22017-12-07 10:27:41 -0800915 webrtc::CreateSessionDescription(SdpType::kAnswer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -0700916 EXPECT_TRUE(DoSetLocalDescription(std::move(new_answer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000917 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
918 }
919
920 void CreatePrAnswerAsLocalDescription() {
kwibergd1fe2812016-04-27 06:47:29 -0700921 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -0700922 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000923
924 std::string sdp;
925 EXPECT_TRUE(answer->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -0700926 std::unique_ptr<SessionDescriptionInterface> pr_answer(
Steve Antona3a92c22017-12-07 10:27:41 -0800927 webrtc::CreateSessionDescription(SdpType::kPrAnswer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -0700928 EXPECT_TRUE(DoSetLocalDescription(std::move(pr_answer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000929 EXPECT_EQ(PeerConnectionInterface::kHaveLocalPrAnswer, observer_.state_);
930 }
931
932 void CreateOfferReceiveAnswer() {
933 CreateOfferAsLocalDescription();
934 std::string sdp;
935 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
936 CreateAnswerAsRemoteDescription(sdp);
937 }
938
939 void CreateOfferAsLocalDescription() {
kwibergd1fe2812016-04-27 06:47:29 -0700940 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -0700941 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000942 // TODO(perkj): Currently SetLocalDescription fails if any parameters in an
943 // audio codec change, even if the parameter has nothing to do with
944 // receiving. Not all parameters are serialized to SDP.
945 // Since CreatePrAnswerAsLocalDescription serialize/deserialize
946 // the SessionDescription, it is necessary to do that here to in order to
947 // get ReceiveOfferCreatePrAnswerAndAnswer and RenegotiateAudioOnly to pass.
948 // https://code.google.com/p/webrtc/issues/detail?id=1356
949 std::string sdp;
950 EXPECT_TRUE(offer->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -0700951 std::unique_ptr<SessionDescriptionInterface> new_offer(
Steve Antona3a92c22017-12-07 10:27:41 -0800952 webrtc::CreateSessionDescription(SdpType::kOffer, sdp));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000953
Steve Antondb45ca82017-09-11 18:27:34 -0700954 EXPECT_TRUE(DoSetLocalDescription(std::move(new_offer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000955 EXPECT_EQ(PeerConnectionInterface::kHaveLocalOffer, observer_.state_);
mallinath@webrtc.org68cbd012014-01-22 00:16:46 +0000956 // Wait for the ice_complete message, so that SDP will have candidates.
Steve Anton6f25b092017-10-23 09:39:20 -0700957 EXPECT_TRUE_WAIT(observer_.ice_gathering_complete_, kTimeout);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000958 }
959
deadbeefab9b2d12015-10-14 11:33:11 -0700960 void CreateAnswerAsRemoteDescription(const std::string& sdp) {
Steve Antondb45ca82017-09-11 18:27:34 -0700961 std::unique_ptr<SessionDescriptionInterface> answer(
Steve Antona3a92c22017-12-07 10:27:41 -0800962 webrtc::CreateSessionDescription(SdpType::kAnswer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -0700963 ASSERT_TRUE(answer);
964 EXPECT_TRUE(DoSetRemoteDescription(std::move(answer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000965 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
966 }
967
deadbeefab9b2d12015-10-14 11:33:11 -0700968 void CreatePrAnswerAndAnswerAsRemoteDescription(const std::string& sdp) {
Steve Antondb45ca82017-09-11 18:27:34 -0700969 std::unique_ptr<SessionDescriptionInterface> pr_answer(
Steve Antona3a92c22017-12-07 10:27:41 -0800970 webrtc::CreateSessionDescription(SdpType::kPrAnswer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -0700971 ASSERT_TRUE(pr_answer);
972 EXPECT_TRUE(DoSetRemoteDescription(std::move(pr_answer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000973 EXPECT_EQ(PeerConnectionInterface::kHaveRemotePrAnswer, observer_.state_);
Steve Antondb45ca82017-09-11 18:27:34 -0700974 std::unique_ptr<SessionDescriptionInterface> answer(
Steve Antona3a92c22017-12-07 10:27:41 -0800975 webrtc::CreateSessionDescription(SdpType::kAnswer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -0700976 ASSERT_TRUE(answer);
977 EXPECT_TRUE(DoSetRemoteDescription(std::move(answer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000978 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
979 }
980
981 // Help function used for waiting until a the last signaled remote stream has
982 // the same label as |stream_label|. In a few of the tests in this file we
983 // answer with the same session description as we offer and thus we can
984 // check if OnAddStream have been called with the same stream as we offer to
985 // send.
986 void WaitAndVerifyOnAddStream(const std::string& stream_label) {
987 EXPECT_EQ_WAIT(stream_label, observer_.GetLastAddedStreamLabel(), kTimeout);
988 }
989
990 // Creates an offer and applies it as a local session description.
991 // Creates an answer with the same SDP an the offer but removes all lines
992 // that start with a:ssrc"
993 void CreateOfferReceiveAnswerWithoutSsrc() {
994 CreateOfferAsLocalDescription();
995 std::string sdp;
996 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
997 SetSsrcToZero(&sdp);
998 CreateAnswerAsRemoteDescription(sdp);
999 }
1000
deadbeefab9b2d12015-10-14 11:33:11 -07001001 // This function creates a MediaStream with label kStreams[0] and
1002 // |number_of_audio_tracks| and |number_of_video_tracks| tracks and the
1003 // corresponding SessionDescriptionInterface. The SessionDescriptionInterface
kwiberg2bbff992016-03-16 11:03:04 -07001004 // is returned and the MediaStream is stored in
deadbeefab9b2d12015-10-14 11:33:11 -07001005 // |reference_collection_|
kwibergd1fe2812016-04-27 06:47:29 -07001006 std::unique_ptr<SessionDescriptionInterface>
kwiberg2bbff992016-03-16 11:03:04 -07001007 CreateSessionDescriptionAndReference(size_t number_of_audio_tracks,
1008 size_t number_of_video_tracks) {
1009 EXPECT_LE(number_of_audio_tracks, 2u);
1010 EXPECT_LE(number_of_video_tracks, 2u);
deadbeefab9b2d12015-10-14 11:33:11 -07001011
1012 reference_collection_ = StreamCollection::Create();
1013 std::string sdp_ms1 = std::string(kSdpStringInit);
1014
1015 std::string mediastream_label = kStreams[0];
1016
1017 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream(
1018 webrtc::MediaStream::Create(mediastream_label));
1019 reference_collection_->AddStream(stream);
1020
1021 if (number_of_audio_tracks > 0) {
1022 sdp_ms1 += std::string(kSdpStringAudio);
1023 sdp_ms1 += std::string(kSdpStringMs1Audio0);
1024 AddAudioTrack(kAudioTracks[0], stream);
1025 }
1026 if (number_of_audio_tracks > 1) {
1027 sdp_ms1 += kSdpStringMs1Audio1;
1028 AddAudioTrack(kAudioTracks[1], stream);
1029 }
1030
1031 if (number_of_video_tracks > 0) {
1032 sdp_ms1 += std::string(kSdpStringVideo);
1033 sdp_ms1 += std::string(kSdpStringMs1Video0);
1034 AddVideoTrack(kVideoTracks[0], stream);
1035 }
1036 if (number_of_video_tracks > 1) {
1037 sdp_ms1 += kSdpStringMs1Video1;
1038 AddVideoTrack(kVideoTracks[1], stream);
1039 }
1040
kwibergd1fe2812016-04-27 06:47:29 -07001041 return std::unique_ptr<SessionDescriptionInterface>(
Steve Antona3a92c22017-12-07 10:27:41 -08001042 webrtc::CreateSessionDescription(SdpType::kOffer, sdp_ms1));
deadbeefab9b2d12015-10-14 11:33:11 -07001043 }
1044
1045 void AddAudioTrack(const std::string& track_id,
1046 MediaStreamInterface* stream) {
1047 rtc::scoped_refptr<webrtc::AudioTrackInterface> audio_track(
1048 webrtc::AudioTrack::Create(track_id, nullptr));
1049 ASSERT_TRUE(stream->AddTrack(audio_track));
1050 }
1051
1052 void AddVideoTrack(const std::string& track_id,
1053 MediaStreamInterface* stream) {
1054 rtc::scoped_refptr<webrtc::VideoTrackInterface> video_track(
nisseaf510af2016-03-21 08:20:42 -07001055 webrtc::VideoTrack::Create(track_id,
perkj773be362017-07-31 23:22:01 -07001056 webrtc::FakeVideoTrackSource::Create(),
1057 rtc::Thread::Current()));
deadbeefab9b2d12015-10-14 11:33:11 -07001058 ASSERT_TRUE(stream->AddTrack(video_track));
1059 }
1060
kwibergfd8be342016-05-14 19:44:11 -07001061 std::unique_ptr<SessionDescriptionInterface> CreateOfferWithOneAudioStream() {
deadbeef293e9262017-01-11 12:28:30 -08001062 CreatePeerConnectionWithoutDtls();
zhihuang8f65cdf2016-05-06 18:40:30 -07001063 AddVoiceStream(kStreamLabel1);
kwibergfd8be342016-05-14 19:44:11 -07001064 std::unique_ptr<SessionDescriptionInterface> offer;
zhihuang8f65cdf2016-05-06 18:40:30 -07001065 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
1066 return offer;
1067 }
1068
kwibergfd8be342016-05-14 19:44:11 -07001069 std::unique_ptr<SessionDescriptionInterface>
zhihuang8f65cdf2016-05-06 18:40:30 -07001070 CreateAnswerWithOneAudioStream() {
Steve Antondb45ca82017-09-11 18:27:34 -07001071 EXPECT_TRUE(DoSetRemoteDescription(CreateOfferWithOneAudioStream()));
kwibergfd8be342016-05-14 19:44:11 -07001072 std::unique_ptr<SessionDescriptionInterface> answer;
zhihuang8f65cdf2016-05-06 18:40:30 -07001073 EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
1074 return answer;
1075 }
1076
1077 const std::string& GetFirstAudioStreamCname(
1078 const SessionDescriptionInterface* desc) {
zhihuang8f65cdf2016-05-06 18:40:30 -07001079 const cricket::AudioContentDescription* audio_desc =
Steve Antonb1c1de12017-12-21 15:14:30 -08001080 cricket::GetFirstAudioContentDescription(desc->description());
zhihuang8f65cdf2016-05-06 18:40:30 -07001081 return audio_desc->streams()[0].cname;
1082 }
1083
zhihuang1c378ed2017-08-17 14:10:50 -07001084 std::unique_ptr<SessionDescriptionInterface> CreateOfferWithOptions(
1085 const RTCOfferAnswerOptions& offer_answer_options) {
1086 RTC_DCHECK(pc_);
1087 rtc::scoped_refptr<MockCreateSessionDescriptionObserver> observer(
1088 new rtc::RefCountedObject<MockCreateSessionDescriptionObserver>());
1089 pc_->CreateOffer(observer, offer_answer_options);
1090 EXPECT_EQ_WAIT(true, observer->called(), kTimeout);
1091 return observer->MoveDescription();
1092 }
1093
1094 void CreateOfferWithOptionsAsRemoteDescription(
1095 std::unique_ptr<SessionDescriptionInterface>* desc,
1096 const RTCOfferAnswerOptions& offer_answer_options) {
1097 *desc = CreateOfferWithOptions(offer_answer_options);
1098 ASSERT_TRUE(desc != nullptr);
1099 std::string sdp;
1100 EXPECT_TRUE((*desc)->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07001101 std::unique_ptr<SessionDescriptionInterface> remote_offer(
Steve Antona3a92c22017-12-07 10:27:41 -08001102 webrtc::CreateSessionDescription(SdpType::kOffer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07001103 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_offer)));
zhihuang1c378ed2017-08-17 14:10:50 -07001104 EXPECT_EQ(PeerConnectionInterface::kHaveRemoteOffer, observer_.state_);
1105 }
1106
1107 void CreateOfferWithOptionsAsLocalDescription(
1108 std::unique_ptr<SessionDescriptionInterface>* desc,
1109 const RTCOfferAnswerOptions& offer_answer_options) {
1110 *desc = CreateOfferWithOptions(offer_answer_options);
1111 ASSERT_TRUE(desc != nullptr);
1112 std::string sdp;
1113 EXPECT_TRUE((*desc)->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07001114 std::unique_ptr<SessionDescriptionInterface> new_offer(
Steve Antona3a92c22017-12-07 10:27:41 -08001115 webrtc::CreateSessionDescription(SdpType::kOffer, sdp));
zhihuang1c378ed2017-08-17 14:10:50 -07001116
Steve Antondb45ca82017-09-11 18:27:34 -07001117 EXPECT_TRUE(DoSetLocalDescription(std::move(new_offer)));
zhihuang1c378ed2017-08-17 14:10:50 -07001118 EXPECT_EQ(PeerConnectionInterface::kHaveLocalOffer, observer_.state_);
1119 }
1120
1121 bool HasCNCodecs(const cricket::ContentInfo* content) {
Steve Antonb1c1de12017-12-21 15:14:30 -08001122 RTC_DCHECK(content);
1123 RTC_DCHECK(content->media_description());
1124 for (const cricket::AudioCodec& codec :
1125 content->media_description()->as_audio()->codecs()) {
1126 if (codec.name == "CN") {
zhihuang1c378ed2017-08-17 14:10:50 -07001127 return true;
Steve Antonb1c1de12017-12-21 15:14:30 -08001128 }
zhihuang1c378ed2017-08-17 14:10:50 -07001129 }
1130 return false;
1131 }
1132
deadbeef9a6f4d42017-05-15 19:43:33 -07001133 std::unique_ptr<rtc::VirtualSocketServer> vss_;
1134 rtc::AutoSocketServerThread main_;
deadbeefd7850b22017-08-23 10:59:19 -07001135 rtc::scoped_refptr<FakeAudioCaptureModule> fake_audio_capture_module_;
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08001136 cricket::FakePortAllocator* port_allocator_ = nullptr;
deadbeef8662f942017-01-20 21:20:51 -08001137 FakeRTCCertificateGenerator* fake_certificate_generator_ = nullptr;
zhihuang9763d562016-08-05 11:14:50 -07001138 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface> pc_factory_;
1139 rtc::scoped_refptr<PeerConnectionFactoryForTest> pc_factory_for_test_;
1140 rtc::scoped_refptr<PeerConnectionInterface> pc_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001141 MockPeerConnectionObserver observer_;
deadbeefab9b2d12015-10-14 11:33:11 -07001142 rtc::scoped_refptr<StreamCollection> reference_collection_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001143};
1144
zhihuang8f65cdf2016-05-06 18:40:30 -07001145// Generate different CNAMEs when PeerConnections are created.
1146// The CNAMEs are expected to be generated randomly. It is possible
1147// that the test fails, though the possibility is very low.
1148TEST_F(PeerConnectionInterfaceTest, CnameGenerationInOffer) {
kwibergfd8be342016-05-14 19:44:11 -07001149 std::unique_ptr<SessionDescriptionInterface> offer1 =
zhihuang8f65cdf2016-05-06 18:40:30 -07001150 CreateOfferWithOneAudioStream();
kwibergfd8be342016-05-14 19:44:11 -07001151 std::unique_ptr<SessionDescriptionInterface> offer2 =
zhihuang8f65cdf2016-05-06 18:40:30 -07001152 CreateOfferWithOneAudioStream();
1153 EXPECT_NE(GetFirstAudioStreamCname(offer1.get()),
1154 GetFirstAudioStreamCname(offer2.get()));
1155}
1156
1157TEST_F(PeerConnectionInterfaceTest, CnameGenerationInAnswer) {
kwibergfd8be342016-05-14 19:44:11 -07001158 std::unique_ptr<SessionDescriptionInterface> answer1 =
zhihuang8f65cdf2016-05-06 18:40:30 -07001159 CreateAnswerWithOneAudioStream();
kwibergfd8be342016-05-14 19:44:11 -07001160 std::unique_ptr<SessionDescriptionInterface> answer2 =
zhihuang8f65cdf2016-05-06 18:40:30 -07001161 CreateAnswerWithOneAudioStream();
1162 EXPECT_NE(GetFirstAudioStreamCname(answer1.get()),
1163 GetFirstAudioStreamCname(answer2.get()));
1164}
1165
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001166TEST_F(PeerConnectionInterfaceTest,
1167 CreatePeerConnectionWithDifferentConfigurations) {
1168 CreatePeerConnectionWithDifferentConfigurations();
1169}
1170
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001171TEST_F(PeerConnectionInterfaceTest,
1172 CreatePeerConnectionWithDifferentIceTransportsTypes) {
1173 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kNone);
1174 EXPECT_EQ(cricket::CF_NONE, port_allocator_->candidate_filter());
1175 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kRelay);
1176 EXPECT_EQ(cricket::CF_RELAY, port_allocator_->candidate_filter());
1177 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kNoHost);
1178 EXPECT_EQ(cricket::CF_ALL & ~cricket::CF_HOST,
1179 port_allocator_->candidate_filter());
1180 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kAll);
1181 EXPECT_EQ(cricket::CF_ALL, port_allocator_->candidate_filter());
1182}
1183
1184// Test that when a PeerConnection is created with a nonzero candidate pool
1185// size, the pooled PortAllocatorSession is created with all the attributes
1186// in the RTCConfiguration.
1187TEST_F(PeerConnectionInterfaceTest, CreatePeerConnectionWithPooledCandidates) {
1188 PeerConnectionInterface::RTCConfiguration config;
1189 PeerConnectionInterface::IceServer server;
1190 server.uri = kStunAddressOnly;
1191 config.servers.push_back(server);
1192 config.type = PeerConnectionInterface::kRelay;
1193 config.disable_ipv6 = true;
1194 config.tcp_candidate_policy =
1195 PeerConnectionInterface::kTcpCandidatePolicyDisabled;
honghaiz60347052016-05-31 18:29:12 -07001196 config.candidate_network_policy =
1197 PeerConnectionInterface::kCandidateNetworkPolicyLowCost;
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001198 config.ice_candidate_pool_size = 1;
1199 CreatePeerConnection(config, nullptr);
1200
1201 const cricket::FakePortAllocatorSession* session =
1202 static_cast<const cricket::FakePortAllocatorSession*>(
1203 port_allocator_->GetPooledSession());
1204 ASSERT_NE(nullptr, session);
1205 EXPECT_EQ(1UL, session->stun_servers().size());
1206 EXPECT_EQ(0U, session->flags() & cricket::PORTALLOCATOR_ENABLE_IPV6);
1207 EXPECT_LT(0U, session->flags() & cricket::PORTALLOCATOR_DISABLE_TCP);
honghaiz60347052016-05-31 18:29:12 -07001208 EXPECT_LT(0U,
1209 session->flags() & cricket::PORTALLOCATOR_DISABLE_COSTLY_NETWORKS);
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001210}
1211
deadbeefd21eab32017-07-26 16:50:11 -07001212// Test that network-related RTCConfiguration members are applied to the
1213// PortAllocator when CreatePeerConnection is called. Specifically:
1214// - disable_ipv6_on_wifi
1215// - max_ipv6_networks
1216// - tcp_candidate_policy
1217// - candidate_network_policy
1218// - prune_turn_ports
1219//
1220// Note that the candidate filter (RTCConfiguration::type) is already tested
1221// above.
1222TEST_F(PeerConnectionInterfaceTest,
1223 CreatePeerConnectionAppliesNetworkConfigToPortAllocator) {
1224 // Create fake port allocator.
1225 std::unique_ptr<cricket::FakePortAllocator> port_allocator(
1226 new cricket::FakePortAllocator(rtc::Thread::Current(), nullptr));
1227 cricket::FakePortAllocator* raw_port_allocator = port_allocator.get();
1228
1229 // Create RTCConfiguration with some network-related fields relevant to
1230 // PortAllocator populated.
1231 PeerConnectionInterface::RTCConfiguration config;
1232 config.disable_ipv6_on_wifi = true;
1233 config.max_ipv6_networks = 10;
1234 config.tcp_candidate_policy =
1235 PeerConnectionInterface::kTcpCandidatePolicyDisabled;
1236 config.candidate_network_policy =
1237 PeerConnectionInterface::kCandidateNetworkPolicyLowCost;
1238 config.prune_turn_ports = true;
1239
1240 // Create the PC factory and PC with the above config.
1241 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface> pc_factory(
1242 webrtc::CreatePeerConnectionFactory(
1243 rtc::Thread::Current(), rtc::Thread::Current(),
Karl Wiberg1b0eae32017-10-17 14:48:54 +02001244 rtc::Thread::Current(), fake_audio_capture_module_,
1245 webrtc::CreateBuiltinAudioEncoderFactory(),
1246 webrtc::CreateBuiltinAudioDecoderFactory(), nullptr, nullptr));
deadbeefd21eab32017-07-26 16:50:11 -07001247 rtc::scoped_refptr<PeerConnectionInterface> pc(
1248 pc_factory->CreatePeerConnection(
1249 config, nullptr, std::move(port_allocator), nullptr, &observer_));
1250
1251 // Now validate that the config fields set above were applied to the
1252 // PortAllocator, as flags or otherwise.
1253 EXPECT_FALSE(raw_port_allocator->flags() &
1254 cricket::PORTALLOCATOR_ENABLE_IPV6_ON_WIFI);
1255 EXPECT_EQ(10, raw_port_allocator->max_ipv6_networks());
1256 EXPECT_TRUE(raw_port_allocator->flags() & cricket::PORTALLOCATOR_DISABLE_TCP);
1257 EXPECT_TRUE(raw_port_allocator->flags() &
1258 cricket::PORTALLOCATOR_DISABLE_COSTLY_NETWORKS);
1259 EXPECT_TRUE(raw_port_allocator->prune_turn_ports());
1260}
1261
Taylor Brandstetterf8e65772016-06-27 17:20:15 -07001262// Test that the PeerConnection initializes the port allocator passed into it,
1263// and on the correct thread.
1264TEST_F(PeerConnectionInterfaceTest,
deadbeefd21eab32017-07-26 16:50:11 -07001265 CreatePeerConnectionInitializesPortAllocatorOnNetworkThread) {
tommie7251592017-07-14 14:44:46 -07001266 std::unique_ptr<rtc::Thread> network_thread(
1267 rtc::Thread::CreateWithSocketServer());
1268 network_thread->Start();
Taylor Brandstetterf8e65772016-06-27 17:20:15 -07001269 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface> pc_factory(
1270 webrtc::CreatePeerConnectionFactory(
tommie7251592017-07-14 14:44:46 -07001271 network_thread.get(), rtc::Thread::Current(), rtc::Thread::Current(),
Karl Wiberg1b0eae32017-10-17 14:48:54 +02001272 fake_audio_capture_module_,
1273 webrtc::CreateBuiltinAudioEncoderFactory(),
1274 webrtc::CreateBuiltinAudioDecoderFactory(), nullptr, nullptr));
Taylor Brandstetterf8e65772016-06-27 17:20:15 -07001275 std::unique_ptr<cricket::FakePortAllocator> port_allocator(
tommie7251592017-07-14 14:44:46 -07001276 new cricket::FakePortAllocator(network_thread.get(), nullptr));
Taylor Brandstetterf8e65772016-06-27 17:20:15 -07001277 cricket::FakePortAllocator* raw_port_allocator = port_allocator.get();
1278 PeerConnectionInterface::RTCConfiguration config;
1279 rtc::scoped_refptr<PeerConnectionInterface> pc(
1280 pc_factory->CreatePeerConnection(
1281 config, nullptr, std::move(port_allocator), nullptr, &observer_));
1282 // FakePortAllocator RTC_CHECKs that it's initialized on the right thread,
1283 // so all we have to do here is check that it's initialized.
1284 EXPECT_TRUE(raw_port_allocator->initialized());
1285}
1286
deadbeef46c73892016-11-16 19:42:04 -08001287// Check that GetConfiguration returns the configuration the PeerConnection was
1288// constructed with, before SetConfiguration is called.
1289TEST_F(PeerConnectionInterfaceTest, GetConfigurationAfterCreatePeerConnection) {
1290 PeerConnectionInterface::RTCConfiguration config;
1291 config.type = PeerConnectionInterface::kRelay;
1292 CreatePeerConnection(config, nullptr);
1293
1294 PeerConnectionInterface::RTCConfiguration returned_config =
1295 pc_->GetConfiguration();
1296 EXPECT_EQ(PeerConnectionInterface::kRelay, returned_config.type);
1297}
1298
1299// Check that GetConfiguration returns the last configuration passed into
1300// SetConfiguration.
1301TEST_F(PeerConnectionInterfaceTest, GetConfigurationAfterSetConfiguration) {
1302 CreatePeerConnection();
1303
1304 PeerConnectionInterface::RTCConfiguration config;
1305 config.type = PeerConnectionInterface::kRelay;
1306 EXPECT_TRUE(pc_->SetConfiguration(config));
1307
1308 PeerConnectionInterface::RTCConfiguration returned_config =
1309 pc_->GetConfiguration();
1310 EXPECT_EQ(PeerConnectionInterface::kRelay, returned_config.type);
1311}
1312
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001313TEST_F(PeerConnectionInterfaceTest, AddStreams) {
deadbeef293e9262017-01-11 12:28:30 -08001314 CreatePeerConnectionWithoutDtls();
deadbeefab9b2d12015-10-14 11:33:11 -07001315 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001316 AddVoiceStream(kStreamLabel2);
1317 ASSERT_EQ(2u, pc_->local_streams()->count());
1318
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001319 // Test we can add multiple local streams to one peerconnection.
zhihuang9763d562016-08-05 11:14:50 -07001320 rtc::scoped_refptr<MediaStreamInterface> stream(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001321 pc_factory_->CreateLocalMediaStream(kStreamLabel3));
zhihuang9763d562016-08-05 11:14:50 -07001322 rtc::scoped_refptr<AudioTrackInterface> audio_track(
1323 pc_factory_->CreateAudioTrack(kStreamLabel3,
1324 static_cast<AudioSourceInterface*>(NULL)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001325 stream->AddTrack(audio_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +00001326 EXPECT_TRUE(pc_->AddStream(stream));
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001327 EXPECT_EQ(3u, pc_->local_streams()->count());
1328
1329 // Remove the third stream.
1330 pc_->RemoveStream(pc_->local_streams()->at(2));
1331 EXPECT_EQ(2u, pc_->local_streams()->count());
1332
1333 // Remove the second stream.
1334 pc_->RemoveStream(pc_->local_streams()->at(1));
1335 EXPECT_EQ(1u, pc_->local_streams()->count());
1336
1337 // Remove the first stream.
1338 pc_->RemoveStream(pc_->local_streams()->at(0));
1339 EXPECT_EQ(0u, pc_->local_streams()->count());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001340}
1341
deadbeefab9b2d12015-10-14 11:33:11 -07001342// Test that the created offer includes streams we added.
1343TEST_F(PeerConnectionInterfaceTest, AddedStreamsPresentInOffer) {
deadbeef293e9262017-01-11 12:28:30 -08001344 CreatePeerConnectionWithoutDtls();
deadbeefab9b2d12015-10-14 11:33:11 -07001345 AddAudioVideoStream(kStreamLabel1, "audio_track", "video_track");
kwibergd1fe2812016-04-27 06:47:29 -07001346 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001347 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07001348
deadbeefab9b2d12015-10-14 11:33:11 -07001349 const cricket::AudioContentDescription* audio_desc =
Steve Antonb1c1de12017-12-21 15:14:30 -08001350 cricket::GetFirstAudioContentDescription(offer->description());
deadbeefab9b2d12015-10-14 11:33:11 -07001351 EXPECT_TRUE(
1352 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
1353
deadbeefab9b2d12015-10-14 11:33:11 -07001354 const cricket::VideoContentDescription* video_desc =
Steve Antonb1c1de12017-12-21 15:14:30 -08001355 cricket::GetFirstVideoContentDescription(offer->description());
deadbeefab9b2d12015-10-14 11:33:11 -07001356 EXPECT_TRUE(
1357 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1358
1359 // Add another stream and ensure the offer includes both the old and new
1360 // streams.
1361 AddAudioVideoStream(kStreamLabel2, "audio_track2", "video_track2");
kwiberg2bbff992016-03-16 11:03:04 -07001362 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07001363
Steve Antonb1c1de12017-12-21 15:14:30 -08001364 audio_desc = cricket::GetFirstAudioContentDescription(offer->description());
deadbeefab9b2d12015-10-14 11:33:11 -07001365 EXPECT_TRUE(
1366 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
1367 EXPECT_TRUE(
1368 ContainsTrack(audio_desc->streams(), kStreamLabel2, "audio_track2"));
1369
Steve Antonb1c1de12017-12-21 15:14:30 -08001370 video_desc = cricket::GetFirstVideoContentDescription(offer->description());
deadbeefab9b2d12015-10-14 11:33:11 -07001371 EXPECT_TRUE(
1372 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1373 EXPECT_TRUE(
1374 ContainsTrack(video_desc->streams(), kStreamLabel2, "video_track2"));
1375}
1376
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001377TEST_F(PeerConnectionInterfaceTest, RemoveStream) {
deadbeef293e9262017-01-11 12:28:30 -08001378 CreatePeerConnectionWithoutDtls();
deadbeefab9b2d12015-10-14 11:33:11 -07001379 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001380 ASSERT_EQ(1u, pc_->local_streams()->count());
1381 pc_->RemoveStream(pc_->local_streams()->at(0));
1382 EXPECT_EQ(0u, pc_->local_streams()->count());
1383}
1384
deadbeefe1f9d832016-01-14 15:35:42 -08001385// Test for AddTrack and RemoveTrack methods.
1386// Tests that the created offer includes tracks we added,
1387// and that the RtpSenders are created correctly.
1388// Also tests that RemoveTrack removes the tracks from subsequent offers.
1389TEST_F(PeerConnectionInterfaceTest, AddTrackRemoveTrack) {
deadbeef293e9262017-01-11 12:28:30 -08001390 CreatePeerConnectionWithoutDtls();
zhihuang9763d562016-08-05 11:14:50 -07001391 rtc::scoped_refptr<AudioTrackInterface> audio_track(
deadbeefe1f9d832016-01-14 15:35:42 -08001392 pc_factory_->CreateAudioTrack("audio_track", nullptr));
zhihuang9763d562016-08-05 11:14:50 -07001393 rtc::scoped_refptr<VideoTrackInterface> video_track(
1394 pc_factory_->CreateVideoTrack(
deadbeef112b2e92017-02-10 20:13:37 -08001395 "video_track", pc_factory_->CreateVideoSource(
1396 std::unique_ptr<cricket::VideoCapturer>(
1397 new cricket::FakeVideoCapturer()))));
Steve Anton2d6c76a2018-01-05 17:10:52 -08001398 auto audio_sender = pc_->AddTrack(audio_track, {kStreamLabel1}).MoveValue();
1399 auto video_sender = pc_->AddTrack(video_track, {kStreamLabel1}).MoveValue();
deadbeefa601f5c2016-06-06 14:27:39 -07001400 EXPECT_EQ(1UL, audio_sender->stream_ids().size());
1401 EXPECT_EQ(kStreamLabel1, audio_sender->stream_ids()[0]);
deadbeefe1f9d832016-01-14 15:35:42 -08001402 EXPECT_EQ("audio_track", audio_sender->id());
1403 EXPECT_EQ(audio_track, audio_sender->track());
deadbeefa601f5c2016-06-06 14:27:39 -07001404 EXPECT_EQ(1UL, video_sender->stream_ids().size());
1405 EXPECT_EQ(kStreamLabel1, video_sender->stream_ids()[0]);
deadbeefe1f9d832016-01-14 15:35:42 -08001406 EXPECT_EQ("video_track", video_sender->id());
1407 EXPECT_EQ(video_track, video_sender->track());
1408
1409 // Now create an offer and check for the senders.
kwibergd1fe2812016-04-27 06:47:29 -07001410 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001411 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefe1f9d832016-01-14 15:35:42 -08001412
1413 const cricket::ContentInfo* audio_content =
1414 cricket::GetFirstAudioContent(offer->description());
Steve Antonb1c1de12017-12-21 15:14:30 -08001415 EXPECT_TRUE(ContainsTrack(audio_content->media_description()->streams(),
1416 kStreamLabel1, "audio_track"));
deadbeefe1f9d832016-01-14 15:35:42 -08001417
1418 const cricket::ContentInfo* video_content =
1419 cricket::GetFirstVideoContent(offer->description());
Steve Antonb1c1de12017-12-21 15:14:30 -08001420 EXPECT_TRUE(ContainsTrack(video_content->media_description()->streams(),
1421 kStreamLabel1, "video_track"));
deadbeefe1f9d832016-01-14 15:35:42 -08001422
Steve Antondb45ca82017-09-11 18:27:34 -07001423 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefe1f9d832016-01-14 15:35:42 -08001424
1425 // Now try removing the tracks.
1426 EXPECT_TRUE(pc_->RemoveTrack(audio_sender));
1427 EXPECT_TRUE(pc_->RemoveTrack(video_sender));
1428
1429 // Create a new offer and ensure it doesn't contain the removed senders.
kwiberg2bbff992016-03-16 11:03:04 -07001430 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefe1f9d832016-01-14 15:35:42 -08001431
1432 audio_content = cricket::GetFirstAudioContent(offer->description());
Steve Antonb1c1de12017-12-21 15:14:30 -08001433 EXPECT_FALSE(ContainsTrack(audio_content->media_description()->streams(),
1434 kStreamLabel1, "audio_track"));
deadbeefe1f9d832016-01-14 15:35:42 -08001435
1436 video_content = cricket::GetFirstVideoContent(offer->description());
Steve Antonb1c1de12017-12-21 15:14:30 -08001437 EXPECT_FALSE(ContainsTrack(video_content->media_description()->streams(),
1438 kStreamLabel1, "video_track"));
deadbeefe1f9d832016-01-14 15:35:42 -08001439
Steve Antondb45ca82017-09-11 18:27:34 -07001440 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefe1f9d832016-01-14 15:35:42 -08001441
1442 // Calling RemoveTrack on a sender no longer attached to a PeerConnection
1443 // should return false.
1444 EXPECT_FALSE(pc_->RemoveTrack(audio_sender));
1445 EXPECT_FALSE(pc_->RemoveTrack(video_sender));
1446}
1447
1448// Test creating senders without a stream specified,
1449// expecting a random stream ID to be generated.
1450TEST_F(PeerConnectionInterfaceTest, AddTrackWithoutStream) {
deadbeef293e9262017-01-11 12:28:30 -08001451 CreatePeerConnectionWithoutDtls();
zhihuang9763d562016-08-05 11:14:50 -07001452 rtc::scoped_refptr<AudioTrackInterface> audio_track(
deadbeefe1f9d832016-01-14 15:35:42 -08001453 pc_factory_->CreateAudioTrack("audio_track", nullptr));
zhihuang9763d562016-08-05 11:14:50 -07001454 rtc::scoped_refptr<VideoTrackInterface> video_track(
1455 pc_factory_->CreateVideoTrack(
deadbeef112b2e92017-02-10 20:13:37 -08001456 "video_track", pc_factory_->CreateVideoSource(
1457 std::unique_ptr<cricket::VideoCapturer>(
1458 new cricket::FakeVideoCapturer()))));
deadbeefe1f9d832016-01-14 15:35:42 -08001459 auto audio_sender =
Steve Anton2d6c76a2018-01-05 17:10:52 -08001460 pc_->AddTrack(audio_track, std::vector<std::string>()).MoveValue();
deadbeefe1f9d832016-01-14 15:35:42 -08001461 auto video_sender =
Steve Anton2d6c76a2018-01-05 17:10:52 -08001462 pc_->AddTrack(video_track, std::vector<std::string>()).MoveValue();
deadbeefe1f9d832016-01-14 15:35:42 -08001463 EXPECT_EQ("audio_track", audio_sender->id());
1464 EXPECT_EQ(audio_track, audio_sender->track());
1465 EXPECT_EQ("video_track", video_sender->id());
1466 EXPECT_EQ(video_track, video_sender->track());
1467 // If the ID is truly a random GUID, it should be infinitely unlikely they
1468 // will be the same.
deadbeefa601f5c2016-06-06 14:27:39 -07001469 EXPECT_NE(video_sender->stream_ids(), audio_sender->stream_ids());
deadbeefe1f9d832016-01-14 15:35:42 -08001470}
1471
Harald Alvestrand89061872018-01-02 14:08:34 +01001472// Test that we can call GetStats() after AddTrack but before connecting
1473// the PeerConnection to a peer.
1474TEST_F(PeerConnectionInterfaceTest, AddTrackBeforeConnecting) {
1475 CreatePeerConnectionWithoutDtls();
1476 rtc::scoped_refptr<AudioTrackInterface> audio_track(
1477 pc_factory_->CreateAudioTrack("audio_track", nullptr));
1478 rtc::scoped_refptr<VideoTrackInterface> video_track(
1479 pc_factory_->CreateVideoTrack(
1480 "video_track", pc_factory_->CreateVideoSource(
1481 std::unique_ptr<cricket::VideoCapturer>(
1482 new cricket::FakeVideoCapturer()))));
Steve Anton2d6c76a2018-01-05 17:10:52 -08001483 auto audio_sender = pc_->AddTrack(audio_track, std::vector<std::string>());
1484 auto video_sender = pc_->AddTrack(video_track, std::vector<std::string>());
Harald Alvestrand89061872018-01-02 14:08:34 +01001485 EXPECT_TRUE(DoGetStats(nullptr));
1486}
1487
Harald Alvestrandc72af932018-01-11 17:18:19 +01001488TEST_F(PeerConnectionInterfaceTest, AttachmentIdIsSetOnAddTrack) {
1489 CreatePeerConnectionWithoutDtls();
1490 rtc::scoped_refptr<AudioTrackInterface> audio_track(
1491 pc_factory_->CreateAudioTrack("audio_track", nullptr));
1492 rtc::scoped_refptr<VideoTrackInterface> video_track(
1493 pc_factory_->CreateVideoTrack(
1494 "video_track", pc_factory_->CreateVideoSource(
1495 std::unique_ptr<cricket::VideoCapturer>(
1496 new cricket::FakeVideoCapturer()))));
1497 auto audio_sender = pc_->AddTrack(audio_track, std::vector<std::string>());
Steve Anton57858b32018-02-15 15:19:50 -08001498 ASSERT_TRUE(audio_sender.ok());
1499 auto* audio_sender_proxy =
1500 static_cast<RtpSenderProxyWithInternal<RtpSenderInternal>*>(
1501 audio_sender.value().get());
1502 EXPECT_NE(0, audio_sender_proxy->internal()->AttachmentId());
1503
Harald Alvestrandc72af932018-01-11 17:18:19 +01001504 auto video_sender = pc_->AddTrack(video_track, std::vector<std::string>());
Steve Anton57858b32018-02-15 15:19:50 -08001505 ASSERT_TRUE(video_sender.ok());
1506 auto* video_sender_proxy =
1507 static_cast<RtpSenderProxyWithInternal<RtpSenderInternal>*>(
1508 video_sender.value().get());
1509 EXPECT_NE(0, video_sender_proxy->internal()->AttachmentId());
Harald Alvestrandc72af932018-01-11 17:18:19 +01001510}
1511
1512TEST_F(PeerConnectionInterfaceTest, AttachmentIdIsSetOnAddStream) {
1513 CreatePeerConnectionWithoutDtls();
1514 AddVideoStream(kStreamLabel1);
1515 auto senders = pc_->GetSenders();
Steve Anton57858b32018-02-15 15:19:50 -08001516 ASSERT_EQ(1u, senders.size());
1517 auto* sender_proxy =
1518 static_cast<RtpSenderProxyWithInternal<RtpSenderInternal>*>(
1519 senders[0].get());
1520 EXPECT_NE(0, sender_proxy->internal()->AttachmentId());
Harald Alvestrandc72af932018-01-11 17:18:19 +01001521}
1522
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001523TEST_F(PeerConnectionInterfaceTest, CreateOfferReceiveAnswer) {
1524 InitiateCall();
1525 WaitAndVerifyOnAddStream(kStreamLabel1);
1526 VerifyRemoteRtpHeaderExtensions();
1527}
1528
1529TEST_F(PeerConnectionInterfaceTest, CreateOfferReceivePrAnswerAndAnswer) {
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 CreateOfferAsLocalDescription();
1533 std::string offer;
1534 EXPECT_TRUE(pc_->local_description()->ToString(&offer));
1535 CreatePrAnswerAndAnswerAsRemoteDescription(offer);
1536 WaitAndVerifyOnAddStream(kStreamLabel1);
1537}
1538
1539TEST_F(PeerConnectionInterfaceTest, ReceiveOfferCreateAnswer) {
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 CreateAnswerAsLocalDescription();
1545
1546 WaitAndVerifyOnAddStream(kStreamLabel1);
1547}
1548
1549TEST_F(PeerConnectionInterfaceTest, ReceiveOfferCreatePrAnswerAndAnswer) {
deadbeef293e9262017-01-11 12:28:30 -08001550 CreatePeerConnectionWithoutDtls();
deadbeefab9b2d12015-10-14 11:33:11 -07001551 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001552
1553 CreateOfferAsRemoteDescription();
1554 CreatePrAnswerAsLocalDescription();
1555 CreateAnswerAsLocalDescription();
1556
1557 WaitAndVerifyOnAddStream(kStreamLabel1);
1558}
1559
1560TEST_F(PeerConnectionInterfaceTest, Renegotiate) {
1561 InitiateCall();
1562 ASSERT_EQ(1u, pc_->remote_streams()->count());
1563 pc_->RemoveStream(pc_->local_streams()->at(0));
1564 CreateOfferReceiveAnswer();
1565 EXPECT_EQ(0u, pc_->remote_streams()->count());
deadbeefab9b2d12015-10-14 11:33:11 -07001566 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001567 CreateOfferReceiveAnswer();
1568}
1569
1570// Tests that after negotiating an audio only call, the respondent can perform a
1571// renegotiation that removes the audio stream.
1572TEST_F(PeerConnectionInterfaceTest, RenegotiateAudioOnly) {
deadbeef293e9262017-01-11 12:28:30 -08001573 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001574 AddVoiceStream(kStreamLabel1);
1575 CreateOfferAsRemoteDescription();
1576 CreateAnswerAsLocalDescription();
1577
1578 ASSERT_EQ(1u, pc_->remote_streams()->count());
1579 pc_->RemoveStream(pc_->local_streams()->at(0));
1580 CreateOfferReceiveAnswer();
1581 EXPECT_EQ(0u, pc_->remote_streams()->count());
1582}
1583
1584// Test that candidates are generated and that we can parse our own candidates.
1585TEST_F(PeerConnectionInterfaceTest, IceCandidates) {
deadbeef293e9262017-01-11 12:28:30 -08001586 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001587
Steve Antonf1c6db12017-10-13 11:13:35 -07001588 EXPECT_FALSE(pc_->AddIceCandidate(observer_.last_candidate()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001589 // SetRemoteDescription takes ownership of offer.
kwibergd1fe2812016-04-27 06:47:29 -07001590 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefab9b2d12015-10-14 11:33:11 -07001591 AddVideoStream(kStreamLabel1);
deadbeefc80741f2015-10-22 13:14:45 -07001592 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07001593 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001594
1595 // SetLocalDescription takes ownership of answer.
kwibergd1fe2812016-04-27 06:47:29 -07001596 std::unique_ptr<SessionDescriptionInterface> answer;
deadbeefc80741f2015-10-22 13:14:45 -07001597 EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07001598 EXPECT_TRUE(DoSetLocalDescription(std::move(answer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001599
Steve Antonf1c6db12017-10-13 11:13:35 -07001600 EXPECT_TRUE_WAIT(observer_.last_candidate() != nullptr, kTimeout);
Steve Anton6f25b092017-10-23 09:39:20 -07001601 EXPECT_TRUE_WAIT(observer_.ice_gathering_complete_, kTimeout);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001602
Steve Antonf1c6db12017-10-13 11:13:35 -07001603 EXPECT_TRUE(pc_->AddIceCandidate(observer_.last_candidate()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001604}
1605
deadbeefab9b2d12015-10-14 11:33:11 -07001606// Test that CreateOffer and CreateAnswer will fail if the track labels are
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001607// not unique.
1608TEST_F(PeerConnectionInterfaceTest, CreateOfferAnswerWithInvalidStream) {
deadbeef293e9262017-01-11 12:28:30 -08001609 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001610 // Create a regular offer for the CreateAnswer test later.
kwibergd1fe2812016-04-27 06:47:29 -07001611 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefc80741f2015-10-22 13:14:45 -07001612 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
kwiberg2bbff992016-03-16 11:03:04 -07001613 EXPECT_TRUE(offer);
1614 offer.reset();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001615
1616 // Create a local stream with audio&video tracks having same label.
1617 AddAudioVideoStream(kStreamLabel1, "track_label", "track_label");
1618
1619 // Test CreateOffer
deadbeefc80741f2015-10-22 13:14:45 -07001620 EXPECT_FALSE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001621
1622 // Test CreateAnswer
kwibergd1fe2812016-04-27 06:47:29 -07001623 std::unique_ptr<SessionDescriptionInterface> answer;
deadbeefc80741f2015-10-22 13:14:45 -07001624 EXPECT_FALSE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001625}
1626
1627// Test that we will get different SSRCs for each tracks in the offer and answer
1628// we created.
1629TEST_F(PeerConnectionInterfaceTest, SsrcInOfferAnswer) {
deadbeef293e9262017-01-11 12:28:30 -08001630 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001631 // Create a local stream with audio&video tracks having different labels.
1632 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
1633
1634 // Test CreateOffer
kwibergd1fe2812016-04-27 06:47:29 -07001635 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001636 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001637 int audio_ssrc = 0;
1638 int video_ssrc = 0;
1639 EXPECT_TRUE(GetFirstSsrc(GetFirstAudioContent(offer->description()),
1640 &audio_ssrc));
1641 EXPECT_TRUE(GetFirstSsrc(GetFirstVideoContent(offer->description()),
1642 &video_ssrc));
1643 EXPECT_NE(audio_ssrc, video_ssrc);
1644
1645 // Test CreateAnswer
Steve Antondb45ca82017-09-11 18:27:34 -07001646 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
kwibergd1fe2812016-04-27 06:47:29 -07001647 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -07001648 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001649 audio_ssrc = 0;
1650 video_ssrc = 0;
1651 EXPECT_TRUE(GetFirstSsrc(GetFirstAudioContent(answer->description()),
1652 &audio_ssrc));
1653 EXPECT_TRUE(GetFirstSsrc(GetFirstVideoContent(answer->description()),
1654 &video_ssrc));
1655 EXPECT_NE(audio_ssrc, video_ssrc);
1656}
1657
deadbeefeb459812015-12-15 19:24:43 -08001658// Test that it's possible to call AddTrack on a MediaStream after adding
1659// the stream to a PeerConnection.
1660// TODO(deadbeef): Remove this test once this behavior is no longer supported.
1661TEST_F(PeerConnectionInterfaceTest, AddTrackAfterAddStream) {
deadbeef293e9262017-01-11 12:28:30 -08001662 CreatePeerConnectionWithoutDtls();
deadbeefeb459812015-12-15 19:24:43 -08001663 // Create audio stream and add to PeerConnection.
1664 AddVoiceStream(kStreamLabel1);
1665 MediaStreamInterface* stream = pc_->local_streams()->at(0);
1666
1667 // Add video track to the audio-only stream.
zhihuang9763d562016-08-05 11:14:50 -07001668 rtc::scoped_refptr<VideoTrackInterface> video_track(
1669 pc_factory_->CreateVideoTrack(
deadbeef112b2e92017-02-10 20:13:37 -08001670 "video_label", pc_factory_->CreateVideoSource(
1671 std::unique_ptr<cricket::VideoCapturer>(
1672 new cricket::FakeVideoCapturer()))));
deadbeefeb459812015-12-15 19:24:43 -08001673 stream->AddTrack(video_track.get());
1674
kwibergd1fe2812016-04-27 06:47:29 -07001675 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001676 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefeb459812015-12-15 19:24:43 -08001677
1678 const cricket::MediaContentDescription* video_desc =
1679 cricket::GetFirstVideoContentDescription(offer->description());
1680 EXPECT_TRUE(video_desc != nullptr);
1681}
1682
1683// Test that it's possible to call RemoveTrack on a MediaStream after adding
1684// the stream to a PeerConnection.
1685// TODO(deadbeef): Remove this test once this behavior is no longer supported.
1686TEST_F(PeerConnectionInterfaceTest, RemoveTrackAfterAddStream) {
deadbeef293e9262017-01-11 12:28:30 -08001687 CreatePeerConnectionWithoutDtls();
deadbeefeb459812015-12-15 19:24:43 -08001688 // Create audio/video stream and add to PeerConnection.
1689 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
1690 MediaStreamInterface* stream = pc_->local_streams()->at(0);
1691
1692 // Remove the video track.
1693 stream->RemoveTrack(stream->GetVideoTracks()[0]);
1694
kwibergd1fe2812016-04-27 06:47:29 -07001695 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001696 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefeb459812015-12-15 19:24:43 -08001697
1698 const cricket::MediaContentDescription* video_desc =
1699 cricket::GetFirstVideoContentDescription(offer->description());
1700 EXPECT_TRUE(video_desc == nullptr);
1701}
1702
deadbeef1dcb1642017-03-29 21:08:16 -07001703// Verify that CreateDtmfSender only succeeds if called with a valid local
1704// track. Other aspects of DtmfSenders are tested in
1705// peerconnection_integrationtest.cc.
1706TEST_F(PeerConnectionInterfaceTest, CreateDtmfSenderWithInvalidParams) {
1707 CreatePeerConnection();
1708 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
1709 EXPECT_EQ(nullptr, pc_->CreateDtmfSender(nullptr));
1710 rtc::scoped_refptr<webrtc::AudioTrackInterface> non_localtrack(
1711 pc_factory_->CreateAudioTrack("dummy_track", nullptr));
1712 EXPECT_EQ(nullptr, pc_->CreateDtmfSender(non_localtrack));
1713}
1714
deadbeefbd7d8f72015-12-18 16:58:44 -08001715// Test creating a sender with a stream ID, and ensure the ID is populated
1716// in the offer.
1717TEST_F(PeerConnectionInterfaceTest, CreateSenderWithStream) {
deadbeef293e9262017-01-11 12:28:30 -08001718 CreatePeerConnectionWithoutDtls();
deadbeefbd7d8f72015-12-18 16:58:44 -08001719 pc_->CreateSender("video", kStreamLabel1);
1720
kwibergd1fe2812016-04-27 06:47:29 -07001721 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001722 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefbd7d8f72015-12-18 16:58:44 -08001723
1724 const cricket::MediaContentDescription* video_desc =
1725 cricket::GetFirstVideoContentDescription(offer->description());
1726 ASSERT_TRUE(video_desc != nullptr);
1727 ASSERT_EQ(1u, video_desc->streams().size());
1728 EXPECT_EQ(kStreamLabel1, video_desc->streams()[0].sync_label);
1729}
1730
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001731// Test that we can specify a certain track that we want statistics about.
1732TEST_F(PeerConnectionInterfaceTest, GetStatsForSpecificTrack) {
1733 InitiateCall();
1734 ASSERT_LT(0u, pc_->remote_streams()->count());
1735 ASSERT_LT(0u, pc_->remote_streams()->at(0)->GetAudioTracks().size());
zhihuang9763d562016-08-05 11:14:50 -07001736 rtc::scoped_refptr<MediaStreamTrackInterface> remote_audio =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001737 pc_->remote_streams()->at(0)->GetAudioTracks()[0];
1738 EXPECT_TRUE(DoGetStats(remote_audio));
1739
1740 // Remove the stream. Since we are sending to our selves the local
1741 // and the remote stream is the same.
1742 pc_->RemoveStream(pc_->local_streams()->at(0));
1743 // Do a re-negotiation.
1744 CreateOfferReceiveAnswer();
1745
1746 ASSERT_EQ(0u, pc_->remote_streams()->count());
1747
1748 // Test that we still can get statistics for the old track. Even if it is not
1749 // sent any longer.
1750 EXPECT_TRUE(DoGetStats(remote_audio));
1751}
1752
1753// Test that we can get stats on a video track.
1754TEST_F(PeerConnectionInterfaceTest, GetStatsForVideoTrack) {
1755 InitiateCall();
1756 ASSERT_LT(0u, pc_->remote_streams()->count());
1757 ASSERT_LT(0u, pc_->remote_streams()->at(0)->GetVideoTracks().size());
zhihuang9763d562016-08-05 11:14:50 -07001758 rtc::scoped_refptr<MediaStreamTrackInterface> remote_video =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001759 pc_->remote_streams()->at(0)->GetVideoTracks()[0];
1760 EXPECT_TRUE(DoGetStats(remote_video));
1761}
1762
1763// Test that we don't get statistics for an invalid track.
zhihuange9e94c32016-11-04 11:38:15 -07001764TEST_F(PeerConnectionInterfaceTest, GetStatsForInvalidTrack) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001765 InitiateCall();
zhihuang9763d562016-08-05 11:14:50 -07001766 rtc::scoped_refptr<AudioTrackInterface> unknown_audio_track(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001767 pc_factory_->CreateAudioTrack("unknown track", NULL));
1768 EXPECT_FALSE(DoGetStats(unknown_audio_track));
1769}
1770
Harald Alvestrand89061872018-01-02 14:08:34 +01001771TEST_F(PeerConnectionInterfaceTest, GetRTCStatsBeforeAndAfterCalling) {
1772 CreatePeerConnectionWithoutDtls();
1773 EXPECT_TRUE(DoGetRTCStats());
1774 // Clearing stats cache is needed now, but should be temporary.
1775 // https://bugs.chromium.org/p/webrtc/issues/detail?id=8693
1776 pc_->ClearStatsCache();
1777 AddAudioVideoStream(kStreamLabel1, "audio_track", "video_track");
1778 EXPECT_TRUE(DoGetRTCStats());
1779 pc_->ClearStatsCache();
1780 CreateOfferReceiveAnswer();
1781 EXPECT_TRUE(DoGetRTCStats());
1782}
1783
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001784// This test setup two RTP data channels in loop back.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001785TEST_F(PeerConnectionInterfaceTest, TestDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001786 FakeConstraints constraints;
1787 constraints.SetAllowRtpDataChannels();
1788 CreatePeerConnection(&constraints);
zhihuang9763d562016-08-05 11:14:50 -07001789 rtc::scoped_refptr<DataChannelInterface> data1 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001790 pc_->CreateDataChannel("test1", NULL);
zhihuang9763d562016-08-05 11:14:50 -07001791 rtc::scoped_refptr<DataChannelInterface> data2 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001792 pc_->CreateDataChannel("test2", NULL);
1793 ASSERT_TRUE(data1 != NULL);
kwibergd1fe2812016-04-27 06:47:29 -07001794 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001795 new MockDataChannelObserver(data1));
kwibergd1fe2812016-04-27 06:47:29 -07001796 std::unique_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001797 new MockDataChannelObserver(data2));
1798
1799 EXPECT_EQ(DataChannelInterface::kConnecting, data1->state());
1800 EXPECT_EQ(DataChannelInterface::kConnecting, data2->state());
1801 std::string data_to_send1 = "testing testing";
1802 std::string data_to_send2 = "testing something else";
1803 EXPECT_FALSE(data1->Send(DataBuffer(data_to_send1)));
1804
1805 CreateOfferReceiveAnswer();
1806 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1807 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
1808
1809 EXPECT_EQ(DataChannelInterface::kOpen, data1->state());
1810 EXPECT_EQ(DataChannelInterface::kOpen, data2->state());
1811 EXPECT_TRUE(data1->Send(DataBuffer(data_to_send1)));
1812 EXPECT_TRUE(data2->Send(DataBuffer(data_to_send2)));
1813
1814 EXPECT_EQ_WAIT(data_to_send1, observer1->last_message(), kTimeout);
1815 EXPECT_EQ_WAIT(data_to_send2, observer2->last_message(), kTimeout);
1816
1817 data1->Close();
1818 EXPECT_EQ(DataChannelInterface::kClosing, data1->state());
1819 CreateOfferReceiveAnswer();
1820 EXPECT_FALSE(observer1->IsOpen());
1821 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
1822 EXPECT_TRUE(observer2->IsOpen());
1823
1824 data_to_send2 = "testing something else again";
1825 EXPECT_TRUE(data2->Send(DataBuffer(data_to_send2)));
1826
1827 EXPECT_EQ_WAIT(data_to_send2, observer2->last_message(), kTimeout);
1828}
1829
1830// This test verifies that sendnig binary data over RTP data channels should
1831// fail.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001832TEST_F(PeerConnectionInterfaceTest, TestSendBinaryOnRtpDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001833 FakeConstraints constraints;
1834 constraints.SetAllowRtpDataChannels();
1835 CreatePeerConnection(&constraints);
zhihuang9763d562016-08-05 11:14:50 -07001836 rtc::scoped_refptr<DataChannelInterface> data1 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001837 pc_->CreateDataChannel("test1", NULL);
zhihuang9763d562016-08-05 11:14:50 -07001838 rtc::scoped_refptr<DataChannelInterface> data2 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001839 pc_->CreateDataChannel("test2", NULL);
1840 ASSERT_TRUE(data1 != NULL);
kwibergd1fe2812016-04-27 06:47:29 -07001841 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001842 new MockDataChannelObserver(data1));
kwibergd1fe2812016-04-27 06:47:29 -07001843 std::unique_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001844 new MockDataChannelObserver(data2));
1845
1846 EXPECT_EQ(DataChannelInterface::kConnecting, data1->state());
1847 EXPECT_EQ(DataChannelInterface::kConnecting, data2->state());
1848
1849 CreateOfferReceiveAnswer();
1850 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1851 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
1852
1853 EXPECT_EQ(DataChannelInterface::kOpen, data1->state());
1854 EXPECT_EQ(DataChannelInterface::kOpen, data2->state());
1855
jbaucheec21bd2016-03-20 06:15:43 -07001856 rtc::CopyOnWriteBuffer buffer("test", 4);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001857 EXPECT_FALSE(data1->Send(DataBuffer(buffer, true)));
1858}
1859
1860// This test setup a RTP data channels in loop back and test that a channel is
1861// opened even if the remote end answer with a zero SSRC.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001862TEST_F(PeerConnectionInterfaceTest, TestSendOnlyDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001863 FakeConstraints constraints;
1864 constraints.SetAllowRtpDataChannels();
1865 CreatePeerConnection(&constraints);
zhihuang9763d562016-08-05 11:14:50 -07001866 rtc::scoped_refptr<DataChannelInterface> data1 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001867 pc_->CreateDataChannel("test1", NULL);
kwibergd1fe2812016-04-27 06:47:29 -07001868 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001869 new MockDataChannelObserver(data1));
1870
1871 CreateOfferReceiveAnswerWithoutSsrc();
1872
1873 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1874
1875 data1->Close();
1876 EXPECT_EQ(DataChannelInterface::kClosing, data1->state());
1877 CreateOfferReceiveAnswerWithoutSsrc();
1878 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
1879 EXPECT_FALSE(observer1->IsOpen());
1880}
1881
1882// This test that if a data channel is added in an answer a receive only channel
1883// channel is created.
1884TEST_F(PeerConnectionInterfaceTest, TestReceiveOnlyDataChannel) {
1885 FakeConstraints constraints;
1886 constraints.SetAllowRtpDataChannels();
1887 CreatePeerConnection(&constraints);
1888
1889 std::string offer_label = "offer_channel";
zhihuang9763d562016-08-05 11:14:50 -07001890 rtc::scoped_refptr<DataChannelInterface> offer_channel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001891 pc_->CreateDataChannel(offer_label, NULL);
1892
1893 CreateOfferAsLocalDescription();
1894
1895 // Replace the data channel label in the offer and apply it as an answer.
1896 std::string receive_label = "answer_channel";
1897 std::string sdp;
1898 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001899 rtc::replace_substrs(offer_label.c_str(), offer_label.length(),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001900 receive_label.c_str(), receive_label.length(),
1901 &sdp);
1902 CreateAnswerAsRemoteDescription(sdp);
1903
1904 // Verify that a new incoming data channel has been created and that
1905 // it is open but can't we written to.
1906 ASSERT_TRUE(observer_.last_datachannel_ != NULL);
1907 DataChannelInterface* received_channel = observer_.last_datachannel_;
1908 EXPECT_EQ(DataChannelInterface::kConnecting, received_channel->state());
1909 EXPECT_EQ(receive_label, received_channel->label());
1910 EXPECT_FALSE(received_channel->Send(DataBuffer("something")));
1911
1912 // Verify that the channel we initially offered has been rejected.
1913 EXPECT_EQ(DataChannelInterface::kClosed, offer_channel->state());
1914
1915 // Do another offer / answer exchange and verify that the data channel is
1916 // opened.
1917 CreateOfferReceiveAnswer();
1918 EXPECT_EQ_WAIT(DataChannelInterface::kOpen, received_channel->state(),
1919 kTimeout);
1920}
1921
1922// This test that no data channel is returned if a reliable channel is
1923// requested.
1924// TODO(perkj): Remove this test once reliable channels are implemented.
1925TEST_F(PeerConnectionInterfaceTest, CreateReliableRtpDataChannelShouldFail) {
1926 FakeConstraints constraints;
1927 constraints.SetAllowRtpDataChannels();
1928 CreatePeerConnection(&constraints);
1929
1930 std::string label = "test";
1931 webrtc::DataChannelInit config;
1932 config.reliable = true;
zhihuang9763d562016-08-05 11:14:50 -07001933 rtc::scoped_refptr<DataChannelInterface> channel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001934 pc_->CreateDataChannel(label, &config);
1935 EXPECT_TRUE(channel == NULL);
1936}
1937
deadbeefab9b2d12015-10-14 11:33:11 -07001938// Verifies that duplicated label is not allowed for RTP data channel.
1939TEST_F(PeerConnectionInterfaceTest, RtpDuplicatedLabelNotAllowed) {
1940 FakeConstraints constraints;
1941 constraints.SetAllowRtpDataChannels();
1942 CreatePeerConnection(&constraints);
1943
1944 std::string label = "test";
zhihuang9763d562016-08-05 11:14:50 -07001945 rtc::scoped_refptr<DataChannelInterface> channel =
deadbeefab9b2d12015-10-14 11:33:11 -07001946 pc_->CreateDataChannel(label, nullptr);
1947 EXPECT_NE(channel, nullptr);
1948
zhihuang9763d562016-08-05 11:14:50 -07001949 rtc::scoped_refptr<DataChannelInterface> dup_channel =
deadbeefab9b2d12015-10-14 11:33:11 -07001950 pc_->CreateDataChannel(label, nullptr);
1951 EXPECT_EQ(dup_channel, nullptr);
1952}
1953
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001954// This tests that a SCTP data channel is returned using different
1955// DataChannelInit configurations.
1956TEST_F(PeerConnectionInterfaceTest, CreateSctpDataChannel) {
1957 FakeConstraints constraints;
1958 constraints.SetAllowDtlsSctpDataChannels();
1959 CreatePeerConnection(&constraints);
1960
1961 webrtc::DataChannelInit config;
1962
zhihuang9763d562016-08-05 11:14:50 -07001963 rtc::scoped_refptr<DataChannelInterface> channel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001964 pc_->CreateDataChannel("1", &config);
1965 EXPECT_TRUE(channel != NULL);
1966 EXPECT_TRUE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001967 EXPECT_TRUE(observer_.renegotiation_needed_);
1968 observer_.renegotiation_needed_ = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001969
1970 config.ordered = false;
1971 channel = pc_->CreateDataChannel("2", &config);
1972 EXPECT_TRUE(channel != NULL);
1973 EXPECT_TRUE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001974 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001975
1976 config.ordered = true;
1977 config.maxRetransmits = 0;
1978 channel = pc_->CreateDataChannel("3", &config);
1979 EXPECT_TRUE(channel != NULL);
1980 EXPECT_FALSE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001981 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001982
1983 config.maxRetransmits = -1;
1984 config.maxRetransmitTime = 0;
1985 channel = pc_->CreateDataChannel("4", &config);
1986 EXPECT_TRUE(channel != NULL);
1987 EXPECT_FALSE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001988 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001989}
1990
1991// This tests that no data channel is returned if both maxRetransmits and
1992// maxRetransmitTime are set for SCTP data channels.
1993TEST_F(PeerConnectionInterfaceTest,
1994 CreateSctpDataChannelShouldFailForInvalidConfig) {
1995 FakeConstraints constraints;
1996 constraints.SetAllowDtlsSctpDataChannels();
1997 CreatePeerConnection(&constraints);
1998
1999 std::string label = "test";
2000 webrtc::DataChannelInit config;
2001 config.maxRetransmits = 0;
2002 config.maxRetransmitTime = 0;
2003
zhihuang9763d562016-08-05 11:14:50 -07002004 rtc::scoped_refptr<DataChannelInterface> channel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002005 pc_->CreateDataChannel(label, &config);
2006 EXPECT_TRUE(channel == NULL);
2007}
2008
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002009// The test verifies that creating a SCTP data channel with an id already in use
2010// or out of range should fail.
2011TEST_F(PeerConnectionInterfaceTest,
2012 CreateSctpDataChannelWithInvalidIdShouldFail) {
2013 FakeConstraints constraints;
2014 constraints.SetAllowDtlsSctpDataChannels();
2015 CreatePeerConnection(&constraints);
2016
2017 webrtc::DataChannelInit config;
zhihuang9763d562016-08-05 11:14:50 -07002018 rtc::scoped_refptr<DataChannelInterface> channel;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002019
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00002020 config.id = 1;
2021 channel = pc_->CreateDataChannel("1", &config);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002022 EXPECT_TRUE(channel != NULL);
2023 EXPECT_EQ(1, channel->id());
2024
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002025 channel = pc_->CreateDataChannel("x", &config);
2026 EXPECT_TRUE(channel == NULL);
2027
2028 config.id = cricket::kMaxSctpSid;
2029 channel = pc_->CreateDataChannel("max", &config);
2030 EXPECT_TRUE(channel != NULL);
2031 EXPECT_EQ(config.id, channel->id());
2032
2033 config.id = cricket::kMaxSctpSid + 1;
2034 channel = pc_->CreateDataChannel("x", &config);
2035 EXPECT_TRUE(channel == NULL);
2036}
2037
deadbeefab9b2d12015-10-14 11:33:11 -07002038// Verifies that duplicated label is allowed for SCTP data channel.
2039TEST_F(PeerConnectionInterfaceTest, SctpDuplicatedLabelAllowed) {
2040 FakeConstraints constraints;
2041 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2042 true);
2043 CreatePeerConnection(&constraints);
2044
2045 std::string label = "test";
zhihuang9763d562016-08-05 11:14:50 -07002046 rtc::scoped_refptr<DataChannelInterface> channel =
deadbeefab9b2d12015-10-14 11:33:11 -07002047 pc_->CreateDataChannel(label, nullptr);
2048 EXPECT_NE(channel, nullptr);
2049
zhihuang9763d562016-08-05 11:14:50 -07002050 rtc::scoped_refptr<DataChannelInterface> dup_channel =
deadbeefab9b2d12015-10-14 11:33:11 -07002051 pc_->CreateDataChannel(label, nullptr);
2052 EXPECT_NE(dup_channel, nullptr);
2053}
2054
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00002055// This test verifies that OnRenegotiationNeeded is fired for every new RTP
2056// DataChannel.
2057TEST_F(PeerConnectionInterfaceTest, RenegotiationNeededForNewRtpDataChannel) {
2058 FakeConstraints constraints;
2059 constraints.SetAllowRtpDataChannels();
2060 CreatePeerConnection(&constraints);
2061
zhihuang9763d562016-08-05 11:14:50 -07002062 rtc::scoped_refptr<DataChannelInterface> dc1 =
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00002063 pc_->CreateDataChannel("test1", NULL);
2064 EXPECT_TRUE(observer_.renegotiation_needed_);
2065 observer_.renegotiation_needed_ = false;
2066
zhihuang9763d562016-08-05 11:14:50 -07002067 rtc::scoped_refptr<DataChannelInterface> dc2 =
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00002068 pc_->CreateDataChannel("test2", NULL);
2069 EXPECT_TRUE(observer_.renegotiation_needed_);
2070}
2071
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002072// This test that a data channel closes when a PeerConnection is deleted/closed.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002073TEST_F(PeerConnectionInterfaceTest, DataChannelCloseWhenPeerConnectionClose) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002074 FakeConstraints constraints;
2075 constraints.SetAllowRtpDataChannels();
2076 CreatePeerConnection(&constraints);
2077
zhihuang9763d562016-08-05 11:14:50 -07002078 rtc::scoped_refptr<DataChannelInterface> data1 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002079 pc_->CreateDataChannel("test1", NULL);
zhihuang9763d562016-08-05 11:14:50 -07002080 rtc::scoped_refptr<DataChannelInterface> data2 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002081 pc_->CreateDataChannel("test2", NULL);
2082 ASSERT_TRUE(data1 != NULL);
kwibergd1fe2812016-04-27 06:47:29 -07002083 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002084 new MockDataChannelObserver(data1));
kwibergd1fe2812016-04-27 06:47:29 -07002085 std::unique_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002086 new MockDataChannelObserver(data2));
2087
2088 CreateOfferReceiveAnswer();
2089 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
2090 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
2091
2092 ReleasePeerConnection();
2093 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
2094 EXPECT_EQ(DataChannelInterface::kClosed, data2->state());
2095}
2096
2097// This test that data channels can be rejected in an answer.
2098TEST_F(PeerConnectionInterfaceTest, TestRejectDataChannelInAnswer) {
2099 FakeConstraints constraints;
2100 constraints.SetAllowRtpDataChannels();
2101 CreatePeerConnection(&constraints);
2102
zhihuang9763d562016-08-05 11:14:50 -07002103 rtc::scoped_refptr<DataChannelInterface> offer_channel(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002104 pc_->CreateDataChannel("offer_channel", NULL));
2105
2106 CreateOfferAsLocalDescription();
2107
2108 // Create an answer where the m-line for data channels are rejected.
2109 std::string sdp;
2110 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07002111 std::unique_ptr<SessionDescriptionInterface> answer(
Steve Antona3a92c22017-12-07 10:27:41 -08002112 webrtc::CreateSessionDescription(SdpType::kAnswer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07002113 ASSERT_TRUE(answer);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002114 cricket::ContentInfo* data_info =
2115 answer->description()->GetContentByName("data");
2116 data_info->rejected = true;
2117
Steve Antondb45ca82017-09-11 18:27:34 -07002118 DoSetRemoteDescription(std::move(answer));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002119 EXPECT_EQ(DataChannelInterface::kClosed, offer_channel->state());
2120}
2121
2122// Test that we can create a session description from an SDP string from
2123// FireFox, use it as a remote session description, generate an answer and use
2124// the answer as a local description.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002125TEST_F(PeerConnectionInterfaceTest, ReceiveFireFoxOffer) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002126 FakeConstraints constraints;
2127 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2128 true);
2129 CreatePeerConnection(&constraints);
2130 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
Steve Antondb45ca82017-09-11 18:27:34 -07002131 std::unique_ptr<SessionDescriptionInterface> desc(
Steve Antona3a92c22017-12-07 10:27:41 -08002132 webrtc::CreateSessionDescription(SdpType::kOffer,
Steve Antondb45ca82017-09-11 18:27:34 -07002133 webrtc::kFireFoxSdpOffer, nullptr));
2134 EXPECT_TRUE(DoSetSessionDescription(std::move(desc), false));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002135 CreateAnswerAsLocalDescription();
2136 ASSERT_TRUE(pc_->local_description() != NULL);
2137 ASSERT_TRUE(pc_->remote_description() != NULL);
2138
2139 const cricket::ContentInfo* content =
2140 cricket::GetFirstAudioContent(pc_->local_description()->description());
2141 ASSERT_TRUE(content != NULL);
2142 EXPECT_FALSE(content->rejected);
2143
2144 content =
2145 cricket::GetFirstVideoContent(pc_->local_description()->description());
2146 ASSERT_TRUE(content != NULL);
2147 EXPECT_FALSE(content->rejected);
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00002148#ifdef HAVE_SCTP
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002149 content =
2150 cricket::GetFirstDataContent(pc_->local_description()->description());
2151 ASSERT_TRUE(content != NULL);
Taylor Brandstetter74cefe12017-12-14 15:38:57 -08002152 // Expected to fail since it's using an incompatible format.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002153 EXPECT_TRUE(content->rejected);
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00002154#endif
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002155}
2156
zhihuangb19012e2017-09-19 13:47:59 -07002157// Test that fallback from DTLS to SDES is not supported.
2158// The fallback was previously supported but was removed to simplify the code
2159// and because it's non-standard.
2160TEST_F(PeerConnectionInterfaceTest, DtlsSdesFallbackNotSupported) {
deadbeef8662f942017-01-20 21:20:51 -08002161 FakeConstraints constraints;
2162 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2163 true);
2164 CreatePeerConnection(&constraints);
2165 // Wait for fake certificate to be generated. Previously, this is what caused
2166 // the "a=crypto" lines to be rejected.
2167 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
2168 ASSERT_NE(nullptr, fake_certificate_generator_);
2169 EXPECT_EQ_WAIT(1, fake_certificate_generator_->generated_certificates(),
2170 kTimeout);
Steve Antondb45ca82017-09-11 18:27:34 -07002171 std::unique_ptr<SessionDescriptionInterface> desc(
Steve Antona3a92c22017-12-07 10:27:41 -08002172 webrtc::CreateSessionDescription(SdpType::kOffer, kDtlsSdesFallbackSdp,
2173 nullptr));
zhihuangb19012e2017-09-19 13:47:59 -07002174 EXPECT_FALSE(DoSetSessionDescription(std::move(desc), false));
deadbeef8662f942017-01-20 21:20:51 -08002175}
2176
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002177// Test that we can create an audio only offer and receive an answer with a
2178// limited set of audio codecs and receive an updated offer with more audio
2179// codecs, where the added codecs are not supported.
2180TEST_F(PeerConnectionInterfaceTest, ReceiveUpdatedAudioOfferWithBadCodecs) {
deadbeef293e9262017-01-11 12:28:30 -08002181 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002182 AddVoiceStream("audio_label");
2183 CreateOfferAsLocalDescription();
2184
Steve Antondb45ca82017-09-11 18:27:34 -07002185 std::unique_ptr<SessionDescriptionInterface> answer(
Steve Antona3a92c22017-12-07 10:27:41 -08002186 webrtc::CreateSessionDescription(SdpType::kAnswer, webrtc::kAudioSdp,
2187 nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07002188 EXPECT_TRUE(DoSetSessionDescription(std::move(answer), false));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002189
Steve Antondb45ca82017-09-11 18:27:34 -07002190 std::unique_ptr<SessionDescriptionInterface> updated_offer(
Steve Antona3a92c22017-12-07 10:27:41 -08002191 webrtc::CreateSessionDescription(
2192 SdpType::kOffer, webrtc::kAudioSdpWithUnsupportedCodecs, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07002193 EXPECT_TRUE(DoSetSessionDescription(std::move(updated_offer), false));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002194 CreateAnswerAsLocalDescription();
2195}
2196
deadbeefc80741f2015-10-22 13:14:45 -07002197// Test that if we're receiving (but not sending) a track, subsequent offers
2198// will have m-lines with a=recvonly.
2199TEST_F(PeerConnectionInterfaceTest, CreateSubsequentRecvOnlyOffer) {
2200 FakeConstraints constraints;
2201 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2202 true);
2203 CreatePeerConnection(&constraints);
2204 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2205 CreateAnswerAsLocalDescription();
2206
2207 // At this point we should be receiving stream 1, but not sending anything.
2208 // A new offer should be recvonly.
kwibergd1fe2812016-04-27 06:47:29 -07002209 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefc80741f2015-10-22 13:14:45 -07002210 DoCreateOffer(&offer, nullptr);
2211
2212 const cricket::ContentInfo* video_content =
2213 cricket::GetFirstVideoContent(offer->description());
Steve Antonb1c1de12017-12-21 15:14:30 -08002214 ASSERT_EQ(RtpTransceiverDirection::kRecvOnly,
2215 video_content->media_description()->direction());
deadbeefc80741f2015-10-22 13:14:45 -07002216
2217 const cricket::ContentInfo* audio_content =
2218 cricket::GetFirstAudioContent(offer->description());
Steve Antonb1c1de12017-12-21 15:14:30 -08002219 ASSERT_EQ(RtpTransceiverDirection::kRecvOnly,
2220 audio_content->media_description()->direction());
deadbeefc80741f2015-10-22 13:14:45 -07002221}
2222
2223// Test that if we're receiving (but not sending) a track, and the
2224// offerToReceiveVideo/offerToReceiveAudio constraints are explicitly set to
2225// false, the generated m-lines will be a=inactive.
2226TEST_F(PeerConnectionInterfaceTest, CreateSubsequentInactiveOffer) {
2227 FakeConstraints constraints;
2228 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2229 true);
2230 CreatePeerConnection(&constraints);
2231 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2232 CreateAnswerAsLocalDescription();
2233
2234 // At this point we should be receiving stream 1, but not sending anything.
2235 // A new offer would be recvonly, but we'll set the "no receive" constraints
2236 // to make it inactive.
kwibergd1fe2812016-04-27 06:47:29 -07002237 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefc80741f2015-10-22 13:14:45 -07002238 FakeConstraints offer_constraints;
2239 offer_constraints.AddMandatory(
2240 webrtc::MediaConstraintsInterface::kOfferToReceiveVideo, false);
2241 offer_constraints.AddMandatory(
2242 webrtc::MediaConstraintsInterface::kOfferToReceiveAudio, false);
2243 DoCreateOffer(&offer, &offer_constraints);
2244
2245 const cricket::ContentInfo* video_content =
2246 cricket::GetFirstVideoContent(offer->description());
Steve Antonb1c1de12017-12-21 15:14:30 -08002247 ASSERT_EQ(RtpTransceiverDirection::kInactive,
2248 video_content->media_description()->direction());
deadbeefc80741f2015-10-22 13:14:45 -07002249
2250 const cricket::ContentInfo* audio_content =
2251 cricket::GetFirstAudioContent(offer->description());
Steve Antonb1c1de12017-12-21 15:14:30 -08002252 ASSERT_EQ(RtpTransceiverDirection::kInactive,
2253 audio_content->media_description()->direction());
deadbeefc80741f2015-10-22 13:14:45 -07002254}
2255
deadbeef653b8e02015-11-11 12:55:10 -08002256// Test that we can use SetConfiguration to change the ICE servers of the
2257// PortAllocator.
2258TEST_F(PeerConnectionInterfaceTest, SetConfigurationChangesIceServers) {
2259 CreatePeerConnection();
2260
2261 PeerConnectionInterface::RTCConfiguration config;
2262 PeerConnectionInterface::IceServer server;
2263 server.uri = "stun:test_hostname";
2264 config.servers.push_back(server);
2265 EXPECT_TRUE(pc_->SetConfiguration(config));
2266
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002267 EXPECT_EQ(1u, port_allocator_->stun_servers().size());
2268 EXPECT_EQ("test_hostname",
2269 port_allocator_->stun_servers().begin()->hostname());
deadbeef653b8e02015-11-11 12:55:10 -08002270}
2271
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002272TEST_F(PeerConnectionInterfaceTest, SetConfigurationChangesCandidateFilter) {
2273 CreatePeerConnection();
2274 PeerConnectionInterface::RTCConfiguration config;
2275 config.type = PeerConnectionInterface::kRelay;
2276 EXPECT_TRUE(pc_->SetConfiguration(config));
2277 EXPECT_EQ(cricket::CF_RELAY, port_allocator_->candidate_filter());
2278}
2279
deadbeef293e9262017-01-11 12:28:30 -08002280TEST_F(PeerConnectionInterfaceTest, SetConfigurationChangesPruneTurnPortsFlag) {
2281 PeerConnectionInterface::RTCConfiguration config;
2282 config.prune_turn_ports = false;
2283 CreatePeerConnection(config, nullptr);
2284 EXPECT_FALSE(port_allocator_->prune_turn_ports());
2285
2286 config.prune_turn_ports = true;
2287 EXPECT_TRUE(pc_->SetConfiguration(config));
2288 EXPECT_TRUE(port_allocator_->prune_turn_ports());
2289}
2290
skvladd1f5fda2017-02-03 16:54:05 -08002291// Test that the ice check interval can be changed. This does not verify that
2292// the setting makes it all the way to P2PTransportChannel, as that would
2293// require a very complex set of mocks.
2294TEST_F(PeerConnectionInterfaceTest, SetConfigurationChangesIceCheckInterval) {
2295 PeerConnectionInterface::RTCConfiguration config;
Oskar Sundbomb95fd2c2017-11-16 10:54:38 +01002296 config.ice_check_min_interval = rtc::nullopt;
skvladd1f5fda2017-02-03 16:54:05 -08002297 CreatePeerConnection(config, nullptr);
Oskar Sundbomb95fd2c2017-11-16 10:54:38 +01002298 config.ice_check_min_interval = 100;
skvladd1f5fda2017-02-03 16:54:05 -08002299 EXPECT_TRUE(pc_->SetConfiguration(config));
2300 PeerConnectionInterface::RTCConfiguration new_config =
2301 pc_->GetConfiguration();
Oskar Sundbomb95fd2c2017-11-16 10:54:38 +01002302 EXPECT_EQ(new_config.ice_check_min_interval, 100);
skvladd1f5fda2017-02-03 16:54:05 -08002303}
2304
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002305// Test that when SetConfiguration changes both the pool size and other
2306// attributes, the pooled session is created with the updated attributes.
2307TEST_F(PeerConnectionInterfaceTest,
2308 SetConfigurationCreatesPooledSessionCorrectly) {
2309 CreatePeerConnection();
2310 PeerConnectionInterface::RTCConfiguration config;
2311 config.ice_candidate_pool_size = 1;
2312 PeerConnectionInterface::IceServer server;
2313 server.uri = kStunAddressOnly;
2314 config.servers.push_back(server);
2315 config.type = PeerConnectionInterface::kRelay;
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07002316 EXPECT_TRUE(pc_->SetConfiguration(config));
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002317
2318 const cricket::FakePortAllocatorSession* session =
2319 static_cast<const cricket::FakePortAllocatorSession*>(
2320 port_allocator_->GetPooledSession());
2321 ASSERT_NE(nullptr, session);
2322 EXPECT_EQ(1UL, session->stun_servers().size());
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002323}
2324
deadbeef293e9262017-01-11 12:28:30 -08002325// Test that after SetLocalDescription, changing the pool size is not allowed,
2326// and an invalid modification error is returned.
deadbeef6de92f92016-12-12 18:49:32 -08002327TEST_F(PeerConnectionInterfaceTest,
2328 CantChangePoolSizeAfterSetLocalDescription) {
2329 CreatePeerConnection();
2330 // Start by setting a size of 1.
2331 PeerConnectionInterface::RTCConfiguration config;
2332 config.ice_candidate_pool_size = 1;
2333 EXPECT_TRUE(pc_->SetConfiguration(config));
2334
2335 // Set remote offer; can still change pool size at this point.
2336 CreateOfferAsRemoteDescription();
2337 config.ice_candidate_pool_size = 2;
2338 EXPECT_TRUE(pc_->SetConfiguration(config));
2339
2340 // Set local answer; now it's too late.
2341 CreateAnswerAsLocalDescription();
2342 config.ice_candidate_pool_size = 3;
deadbeef293e9262017-01-11 12:28:30 -08002343 RTCError error;
2344 EXPECT_FALSE(pc_->SetConfiguration(config, &error));
2345 EXPECT_EQ(RTCErrorType::INVALID_MODIFICATION, error.type());
2346}
2347
deadbeef42a42632017-03-10 15:18:00 -08002348// Test that after setting an answer, extra pooled sessions are discarded. The
2349// ICE candidate pool is only intended to be used for the first offer/answer.
2350TEST_F(PeerConnectionInterfaceTest,
2351 ExtraPooledSessionsDiscardedAfterApplyingAnswer) {
2352 CreatePeerConnection();
2353
2354 // Set a larger-than-necessary size.
2355 PeerConnectionInterface::RTCConfiguration config;
2356 config.ice_candidate_pool_size = 4;
2357 EXPECT_TRUE(pc_->SetConfiguration(config));
2358
2359 // Do offer/answer.
2360 CreateOfferAsRemoteDescription();
2361 CreateAnswerAsLocalDescription();
2362
2363 // Expect no pooled sessions to be left.
2364 const cricket::PortAllocatorSession* session =
2365 port_allocator_->GetPooledSession();
2366 EXPECT_EQ(nullptr, session);
2367}
2368
2369// After Close is called, pooled candidates should be discarded so as to not
2370// waste network resources.
2371TEST_F(PeerConnectionInterfaceTest, PooledSessionsDiscardedAfterClose) {
2372 CreatePeerConnection();
2373
2374 PeerConnectionInterface::RTCConfiguration config;
2375 config.ice_candidate_pool_size = 3;
2376 EXPECT_TRUE(pc_->SetConfiguration(config));
2377 pc_->Close();
2378
2379 // Expect no pooled sessions to be left.
2380 const cricket::PortAllocatorSession* session =
2381 port_allocator_->GetPooledSession();
2382 EXPECT_EQ(nullptr, session);
2383}
2384
deadbeef293e9262017-01-11 12:28:30 -08002385// Test that SetConfiguration returns an invalid modification error if
2386// modifying a field in the configuration that isn't allowed to be modified.
2387TEST_F(PeerConnectionInterfaceTest,
2388 SetConfigurationReturnsInvalidModificationError) {
2389 PeerConnectionInterface::RTCConfiguration config;
2390 config.bundle_policy = PeerConnectionInterface::kBundlePolicyBalanced;
2391 config.rtcp_mux_policy = PeerConnectionInterface::kRtcpMuxPolicyNegotiate;
2392 config.continual_gathering_policy = PeerConnectionInterface::GATHER_ONCE;
2393 CreatePeerConnection(config, nullptr);
2394
2395 PeerConnectionInterface::RTCConfiguration modified_config = config;
2396 modified_config.bundle_policy =
2397 PeerConnectionInterface::kBundlePolicyMaxBundle;
2398 RTCError error;
2399 EXPECT_FALSE(pc_->SetConfiguration(modified_config, &error));
2400 EXPECT_EQ(RTCErrorType::INVALID_MODIFICATION, error.type());
2401
2402 modified_config = config;
2403 modified_config.rtcp_mux_policy =
2404 PeerConnectionInterface::kRtcpMuxPolicyRequire;
2405 error.set_type(RTCErrorType::NONE);
2406 EXPECT_FALSE(pc_->SetConfiguration(modified_config, &error));
2407 EXPECT_EQ(RTCErrorType::INVALID_MODIFICATION, error.type());
2408
2409 modified_config = config;
2410 modified_config.continual_gathering_policy =
2411 PeerConnectionInterface::GATHER_CONTINUALLY;
2412 error.set_type(RTCErrorType::NONE);
2413 EXPECT_FALSE(pc_->SetConfiguration(modified_config, &error));
2414 EXPECT_EQ(RTCErrorType::INVALID_MODIFICATION, error.type());
2415}
2416
2417// Test that SetConfiguration returns a range error if the candidate pool size
2418// is negative or larger than allowed by the spec.
2419TEST_F(PeerConnectionInterfaceTest,
2420 SetConfigurationReturnsRangeErrorForBadCandidatePoolSize) {
2421 PeerConnectionInterface::RTCConfiguration config;
2422 CreatePeerConnection(config, nullptr);
2423
2424 config.ice_candidate_pool_size = -1;
2425 RTCError error;
2426 EXPECT_FALSE(pc_->SetConfiguration(config, &error));
2427 EXPECT_EQ(RTCErrorType::INVALID_RANGE, error.type());
2428
2429 config.ice_candidate_pool_size = INT_MAX;
2430 error.set_type(RTCErrorType::NONE);
2431 EXPECT_FALSE(pc_->SetConfiguration(config, &error));
2432 EXPECT_EQ(RTCErrorType::INVALID_RANGE, error.type());
2433}
2434
2435// Test that SetConfiguration returns a syntax error if parsing an ICE server
2436// URL failed.
2437TEST_F(PeerConnectionInterfaceTest,
2438 SetConfigurationReturnsSyntaxErrorFromBadIceUrls) {
2439 PeerConnectionInterface::RTCConfiguration config;
2440 CreatePeerConnection(config, nullptr);
2441
2442 PeerConnectionInterface::IceServer bad_server;
2443 bad_server.uri = "stunn:www.example.com";
2444 config.servers.push_back(bad_server);
2445 RTCError error;
2446 EXPECT_FALSE(pc_->SetConfiguration(config, &error));
2447 EXPECT_EQ(RTCErrorType::SYNTAX_ERROR, error.type());
2448}
2449
2450// Test that SetConfiguration returns an invalid parameter error if a TURN
2451// IceServer is missing a username or password.
2452TEST_F(PeerConnectionInterfaceTest,
2453 SetConfigurationReturnsInvalidParameterIfCredentialsMissing) {
2454 PeerConnectionInterface::RTCConfiguration config;
2455 CreatePeerConnection(config, nullptr);
2456
2457 PeerConnectionInterface::IceServer bad_server;
2458 bad_server.uri = "turn:www.example.com";
2459 // Missing password.
2460 bad_server.username = "foo";
2461 config.servers.push_back(bad_server);
2462 RTCError error;
2463 EXPECT_FALSE(pc_->SetConfiguration(config, &error));
2464 EXPECT_EQ(RTCErrorType::INVALID_PARAMETER, error.type());
deadbeef6de92f92016-12-12 18:49:32 -08002465}
2466
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002467// Test that PeerConnection::Close changes the states to closed and all remote
2468// tracks change state to ended.
2469TEST_F(PeerConnectionInterfaceTest, CloseAndTestStreamsAndStates) {
2470 // Initialize a PeerConnection and negotiate local and remote session
2471 // description.
2472 InitiateCall();
2473 ASSERT_EQ(1u, pc_->local_streams()->count());
2474 ASSERT_EQ(1u, pc_->remote_streams()->count());
2475
2476 pc_->Close();
2477
2478 EXPECT_EQ(PeerConnectionInterface::kClosed, pc_->signaling_state());
2479 EXPECT_EQ(PeerConnectionInterface::kIceConnectionClosed,
2480 pc_->ice_connection_state());
2481 EXPECT_EQ(PeerConnectionInterface::kIceGatheringComplete,
2482 pc_->ice_gathering_state());
2483
2484 EXPECT_EQ(1u, pc_->local_streams()->count());
2485 EXPECT_EQ(1u, pc_->remote_streams()->count());
2486
zhihuang9763d562016-08-05 11:14:50 -07002487 rtc::scoped_refptr<MediaStreamInterface> remote_stream =
2488 pc_->remote_streams()->at(0);
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002489 // Track state may be updated asynchronously.
perkjd61bf802016-03-24 03:16:19 -07002490 EXPECT_EQ_WAIT(MediaStreamTrackInterface::kEnded,
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002491 remote_stream->GetAudioTracks()[0]->state(), kTimeout);
2492 EXPECT_EQ_WAIT(MediaStreamTrackInterface::kEnded,
2493 remote_stream->GetVideoTracks()[0]->state(), kTimeout);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002494}
2495
2496// Test that PeerConnection methods fails gracefully after
2497// PeerConnection::Close has been called.
2498TEST_F(PeerConnectionInterfaceTest, CloseAndTestMethods) {
deadbeef293e9262017-01-11 12:28:30 -08002499 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002500 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
2501 CreateOfferAsRemoteDescription();
2502 CreateAnswerAsLocalDescription();
2503
2504 ASSERT_EQ(1u, pc_->local_streams()->count());
zhihuang9763d562016-08-05 11:14:50 -07002505 rtc::scoped_refptr<MediaStreamInterface> local_stream =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002506 pc_->local_streams()->at(0);
2507
2508 pc_->Close();
2509
2510 pc_->RemoveStream(local_stream);
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +00002511 EXPECT_FALSE(pc_->AddStream(local_stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002512
2513 ASSERT_FALSE(local_stream->GetAudioTracks().empty());
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002514 rtc::scoped_refptr<webrtc::DtmfSenderInterface> dtmf_sender(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002515 pc_->CreateDtmfSender(local_stream->GetAudioTracks()[0]));
wu@webrtc.org66037362013-08-13 00:09:35 +00002516 EXPECT_TRUE(NULL == dtmf_sender); // local stream has been removed.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002517
2518 EXPECT_TRUE(pc_->CreateDataChannel("test", NULL) == NULL);
2519
2520 EXPECT_TRUE(pc_->local_description() != NULL);
2521 EXPECT_TRUE(pc_->remote_description() != NULL);
2522
kwibergd1fe2812016-04-27 06:47:29 -07002523 std::unique_ptr<SessionDescriptionInterface> offer;
Steve Anton8d3444d2017-10-20 15:30:51 -07002524 EXPECT_FALSE(DoCreateOffer(&offer, nullptr));
kwibergd1fe2812016-04-27 06:47:29 -07002525 std::unique_ptr<SessionDescriptionInterface> answer;
Steve Anton8d3444d2017-10-20 15:30:51 -07002526 EXPECT_FALSE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002527
2528 std::string sdp;
2529 ASSERT_TRUE(pc_->remote_description()->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07002530 std::unique_ptr<SessionDescriptionInterface> remote_offer(
Steve Antona3a92c22017-12-07 10:27:41 -08002531 webrtc::CreateSessionDescription(SdpType::kOffer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07002532 EXPECT_FALSE(DoSetRemoteDescription(std::move(remote_offer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002533
2534 ASSERT_TRUE(pc_->local_description()->ToString(&sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07002535 std::unique_ptr<SessionDescriptionInterface> local_offer(
Steve Antona3a92c22017-12-07 10:27:41 -08002536 webrtc::CreateSessionDescription(SdpType::kOffer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07002537 EXPECT_FALSE(DoSetLocalDescription(std::move(local_offer)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002538}
2539
2540// Test that GetStats can still be called after PeerConnection::Close.
2541TEST_F(PeerConnectionInterfaceTest, CloseAndGetStats) {
2542 InitiateCall();
2543 pc_->Close();
2544 DoGetStats(NULL);
2545}
deadbeefab9b2d12015-10-14 11:33:11 -07002546
2547// NOTE: The series of tests below come from what used to be
2548// mediastreamsignaling_unittest.cc, and are mostly aimed at testing that
2549// setting a remote or local description has the expected effects.
2550
2551// This test verifies that the remote MediaStreams corresponding to a received
2552// SDP string is created. In this test the two separate MediaStreams are
2553// signaled.
2554TEST_F(PeerConnectionInterfaceTest, UpdateRemoteStreams) {
2555 FakeConstraints constraints;
2556 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2557 true);
2558 CreatePeerConnection(&constraints);
2559 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2560
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002561 rtc::scoped_refptr<StreamCollection> reference(CreateStreamCollection(1, 1));
deadbeefab9b2d12015-10-14 11:33:11 -07002562 EXPECT_TRUE(
2563 CompareStreamCollections(observer_.remote_streams(), reference.get()));
2564 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2565 EXPECT_TRUE(remote_stream->GetVideoTracks()[0]->GetSource() != nullptr);
2566
2567 // Create a session description based on another SDP with another
2568 // MediaStream.
2569 CreateAndSetRemoteOffer(kSdpStringWithStream1And2);
2570
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002571 rtc::scoped_refptr<StreamCollection> reference2(CreateStreamCollection(2, 1));
deadbeefab9b2d12015-10-14 11:33:11 -07002572 EXPECT_TRUE(
2573 CompareStreamCollections(observer_.remote_streams(), reference2.get()));
2574}
2575
2576// This test verifies that when remote tracks are added/removed from SDP, the
2577// created remote streams are updated appropriately.
2578TEST_F(PeerConnectionInterfaceTest,
2579 AddRemoveTrackFromExistingRemoteMediaStream) {
2580 FakeConstraints constraints;
2581 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2582 true);
2583 CreatePeerConnection(&constraints);
kwibergd1fe2812016-04-27 06:47:29 -07002584 std::unique_ptr<SessionDescriptionInterface> desc_ms1 =
kwiberg2bbff992016-03-16 11:03:04 -07002585 CreateSessionDescriptionAndReference(1, 1);
Steve Antondb45ca82017-09-11 18:27:34 -07002586 EXPECT_TRUE(DoSetRemoteDescription(std::move(desc_ms1)));
deadbeefab9b2d12015-10-14 11:33:11 -07002587 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
2588 reference_collection_));
2589
2590 // Add extra audio and video tracks to the same MediaStream.
kwibergd1fe2812016-04-27 06:47:29 -07002591 std::unique_ptr<SessionDescriptionInterface> desc_ms1_two_tracks =
kwiberg2bbff992016-03-16 11:03:04 -07002592 CreateSessionDescriptionAndReference(2, 2);
Steve Antondb45ca82017-09-11 18:27:34 -07002593 EXPECT_TRUE(DoSetRemoteDescription(std::move(desc_ms1_two_tracks)));
deadbeefab9b2d12015-10-14 11:33:11 -07002594 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
2595 reference_collection_));
zhihuang9763d562016-08-05 11:14:50 -07002596 rtc::scoped_refptr<AudioTrackInterface> audio_track2 =
perkjd61bf802016-03-24 03:16:19 -07002597 observer_.remote_streams()->at(0)->GetAudioTracks()[1];
2598 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, audio_track2->state());
zhihuang9763d562016-08-05 11:14:50 -07002599 rtc::scoped_refptr<VideoTrackInterface> video_track2 =
perkjd61bf802016-03-24 03:16:19 -07002600 observer_.remote_streams()->at(0)->GetVideoTracks()[1];
2601 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, video_track2->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002602
2603 // Remove the extra audio and video tracks.
kwibergd1fe2812016-04-27 06:47:29 -07002604 std::unique_ptr<SessionDescriptionInterface> desc_ms2 =
kwiberg2bbff992016-03-16 11:03:04 -07002605 CreateSessionDescriptionAndReference(1, 1);
perkjd61bf802016-03-24 03:16:19 -07002606 MockTrackObserver audio_track_observer(audio_track2);
2607 MockTrackObserver video_track_observer(video_track2);
2608
2609 EXPECT_CALL(audio_track_observer, OnChanged()).Times(Exactly(1));
2610 EXPECT_CALL(video_track_observer, OnChanged()).Times(Exactly(1));
Steve Antondb45ca82017-09-11 18:27:34 -07002611 EXPECT_TRUE(DoSetRemoteDescription(std::move(desc_ms2)));
deadbeefab9b2d12015-10-14 11:33:11 -07002612 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
2613 reference_collection_));
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002614 // Track state may be updated asynchronously.
perkjd61bf802016-03-24 03:16:19 -07002615 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002616 audio_track2->state(), kTimeout);
2617 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
2618 video_track2->state(), kTimeout);
deadbeefab9b2d12015-10-14 11:33:11 -07002619}
2620
2621// This tests that remote tracks are ended if a local session description is set
2622// that rejects the media content type.
2623TEST_F(PeerConnectionInterfaceTest, RejectMediaContent) {
2624 FakeConstraints constraints;
2625 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2626 true);
2627 CreatePeerConnection(&constraints);
2628 // First create and set a remote offer, then reject its video content in our
2629 // answer.
2630 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2631 ASSERT_EQ(1u, observer_.remote_streams()->count());
2632 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2633 ASSERT_EQ(1u, remote_stream->GetVideoTracks().size());
2634 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2635
2636 rtc::scoped_refptr<webrtc::VideoTrackInterface> remote_video =
2637 remote_stream->GetVideoTracks()[0];
2638 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, remote_video->state());
2639 rtc::scoped_refptr<webrtc::AudioTrackInterface> remote_audio =
2640 remote_stream->GetAudioTracks()[0];
2641 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, remote_audio->state());
2642
kwibergd1fe2812016-04-27 06:47:29 -07002643 std::unique_ptr<SessionDescriptionInterface> local_answer;
kwiberg2bbff992016-03-16 11:03:04 -07002644 EXPECT_TRUE(DoCreateAnswer(&local_answer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07002645 cricket::ContentInfo* video_info =
2646 local_answer->description()->GetContentByName("video");
2647 video_info->rejected = true;
Steve Antondb45ca82017-09-11 18:27:34 -07002648 EXPECT_TRUE(DoSetLocalDescription(std::move(local_answer)));
deadbeefab9b2d12015-10-14 11:33:11 -07002649 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kEnded, remote_video->state());
2650 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, remote_audio->state());
2651
2652 // Now create an offer where we reject both video and audio.
kwibergd1fe2812016-04-27 06:47:29 -07002653 std::unique_ptr<SessionDescriptionInterface> local_offer;
kwiberg2bbff992016-03-16 11:03:04 -07002654 EXPECT_TRUE(DoCreateOffer(&local_offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07002655 video_info = local_offer->description()->GetContentByName("video");
2656 ASSERT_TRUE(video_info != nullptr);
2657 video_info->rejected = true;
2658 cricket::ContentInfo* audio_info =
2659 local_offer->description()->GetContentByName("audio");
2660 ASSERT_TRUE(audio_info != nullptr);
2661 audio_info->rejected = true;
Steve Antondb45ca82017-09-11 18:27:34 -07002662 EXPECT_TRUE(DoSetLocalDescription(std::move(local_offer)));
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002663 // Track state may be updated asynchronously.
perkjd61bf802016-03-24 03:16:19 -07002664 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002665 remote_audio->state(), kTimeout);
2666 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
2667 remote_video->state(), kTimeout);
deadbeefab9b2d12015-10-14 11:33:11 -07002668}
2669
2670// This tests that we won't crash if the remote track has been removed outside
2671// of PeerConnection and then PeerConnection tries to reject the track.
2672TEST_F(PeerConnectionInterfaceTest, RemoveTrackThenRejectMediaContent) {
2673 FakeConstraints constraints;
2674 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2675 true);
2676 CreatePeerConnection(&constraints);
2677 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2678 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2679 remote_stream->RemoveTrack(remote_stream->GetVideoTracks()[0]);
2680 remote_stream->RemoveTrack(remote_stream->GetAudioTracks()[0]);
2681
kwibergd1fe2812016-04-27 06:47:29 -07002682 std::unique_ptr<SessionDescriptionInterface> local_answer(
Steve Antona3a92c22017-12-07 10:27:41 -08002683 webrtc::CreateSessionDescription(SdpType::kAnswer, kSdpStringWithStream1,
2684 nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07002685 cricket::ContentInfo* video_info =
2686 local_answer->description()->GetContentByName("video");
2687 video_info->rejected = true;
2688 cricket::ContentInfo* audio_info =
2689 local_answer->description()->GetContentByName("audio");
2690 audio_info->rejected = true;
Steve Antondb45ca82017-09-11 18:27:34 -07002691 EXPECT_TRUE(DoSetLocalDescription(std::move(local_answer)));
deadbeefab9b2d12015-10-14 11:33:11 -07002692
2693 // No crash is a pass.
2694}
2695
deadbeef5e97fb52015-10-15 12:49:08 -07002696// This tests that if a recvonly remote description is set, no remote streams
2697// will be created, even if the description contains SSRCs/MSIDs.
2698// See: https://code.google.com/p/webrtc/issues/detail?id=5054
2699TEST_F(PeerConnectionInterfaceTest, RecvonlyDescriptionDoesntCreateStream) {
2700 FakeConstraints constraints;
2701 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2702 true);
2703 CreatePeerConnection(&constraints);
2704
2705 std::string recvonly_offer = kSdpStringWithStream1;
2706 rtc::replace_substrs(kSendrecv, strlen(kSendrecv), kRecvonly,
2707 strlen(kRecvonly), &recvonly_offer);
2708 CreateAndSetRemoteOffer(recvonly_offer);
2709
2710 EXPECT_EQ(0u, observer_.remote_streams()->count());
2711}
2712
deadbeefab9b2d12015-10-14 11:33:11 -07002713// This tests that a default MediaStream is created if a remote session
2714// description doesn't contain any streams and no MSID support.
2715// It also tests that the default stream is updated if a video m-line is added
2716// in a subsequent session description.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002717TEST_F(PeerConnectionInterfaceTest, SdpWithoutMsidCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002718 FakeConstraints constraints;
2719 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2720 true);
2721 CreatePeerConnection(&constraints);
2722 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2723
2724 ASSERT_EQ(1u, observer_.remote_streams()->count());
2725 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2726
2727 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2728 EXPECT_EQ(0u, remote_stream->GetVideoTracks().size());
2729 EXPECT_EQ("default", remote_stream->label());
2730
2731 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2732 ASSERT_EQ(1u, observer_.remote_streams()->count());
2733 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2734 EXPECT_EQ("defaulta0", remote_stream->GetAudioTracks()[0]->id());
deadbeef884f5852016-01-15 09:20:04 -08002735 EXPECT_EQ(MediaStreamTrackInterface::kLive,
2736 remote_stream->GetAudioTracks()[0]->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002737 ASSERT_EQ(1u, remote_stream->GetVideoTracks().size());
2738 EXPECT_EQ("defaultv0", remote_stream->GetVideoTracks()[0]->id());
deadbeef884f5852016-01-15 09:20:04 -08002739 EXPECT_EQ(MediaStreamTrackInterface::kLive,
2740 remote_stream->GetVideoTracks()[0]->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002741}
2742
2743// This tests that a default MediaStream is created if a remote session
2744// description doesn't contain any streams and media direction is send only.
2745TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002746 SendOnlySdpWithoutMsidCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002747 FakeConstraints constraints;
2748 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2749 true);
2750 CreatePeerConnection(&constraints);
2751 CreateAndSetRemoteOffer(kSdpStringSendOnlyWithoutStreams);
2752
2753 ASSERT_EQ(1u, observer_.remote_streams()->count());
2754 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2755
2756 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2757 EXPECT_EQ(1u, remote_stream->GetVideoTracks().size());
2758 EXPECT_EQ("default", remote_stream->label());
2759}
2760
2761// This tests that it won't crash when PeerConnection tries to remove
2762// a remote track that as already been removed from the MediaStream.
2763TEST_F(PeerConnectionInterfaceTest, RemoveAlreadyGoneRemoteStream) {
2764 FakeConstraints constraints;
2765 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2766 true);
2767 CreatePeerConnection(&constraints);
2768 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2769 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2770 remote_stream->RemoveTrack(remote_stream->GetAudioTracks()[0]);
2771 remote_stream->RemoveTrack(remote_stream->GetVideoTracks()[0]);
2772
2773 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2774
2775 // No crash is a pass.
2776}
2777
2778// This tests that a default MediaStream is created if the remote session
2779// description doesn't contain any streams and don't contain an indication if
2780// MSID is supported.
2781TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002782 SdpWithoutMsidAndStreamsCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002783 FakeConstraints constraints;
2784 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2785 true);
2786 CreatePeerConnection(&constraints);
2787 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2788
2789 ASSERT_EQ(1u, observer_.remote_streams()->count());
2790 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2791 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2792 EXPECT_EQ(1u, remote_stream->GetVideoTracks().size());
2793}
2794
2795// This tests that a default MediaStream is not created if the remote session
2796// description doesn't contain any streams but does support MSID.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002797TEST_F(PeerConnectionInterfaceTest, SdpWithMsidDontCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002798 FakeConstraints constraints;
2799 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2800 true);
2801 CreatePeerConnection(&constraints);
2802 CreateAndSetRemoteOffer(kSdpStringWithMsidWithoutStreams);
2803 EXPECT_EQ(0u, observer_.remote_streams()->count());
2804}
2805
deadbeefbda7e0b2015-12-08 17:13:40 -08002806// This tests that when setting a new description, the old default tracks are
2807// not destroyed and recreated.
2808// See: https://bugs.chromium.org/p/webrtc/issues/detail?id=5250
Stefan Holmer102362b2016-03-18 09:39:07 +01002809TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002810 DefaultTracksNotDestroyedAndRecreated) {
deadbeefbda7e0b2015-12-08 17:13:40 -08002811 FakeConstraints constraints;
2812 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2813 true);
2814 CreatePeerConnection(&constraints);
2815 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2816
2817 ASSERT_EQ(1u, observer_.remote_streams()->count());
2818 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2819 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2820
2821 // Set the track to "disabled", then set a new description and ensure the
2822 // track is still disabled, which ensures it hasn't been recreated.
2823 remote_stream->GetAudioTracks()[0]->set_enabled(false);
2824 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2825 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2826 EXPECT_FALSE(remote_stream->GetAudioTracks()[0]->enabled());
2827}
2828
deadbeefab9b2d12015-10-14 11:33:11 -07002829// This tests that a default MediaStream is not created if a remote session
2830// description is updated to not have any MediaStreams.
2831TEST_F(PeerConnectionInterfaceTest, VerifyDefaultStreamIsNotCreated) {
2832 FakeConstraints constraints;
2833 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2834 true);
2835 CreatePeerConnection(&constraints);
2836 CreateAndSetRemoteOffer(kSdpStringWithStream1);
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002837 rtc::scoped_refptr<StreamCollection> reference(CreateStreamCollection(1, 1));
deadbeefab9b2d12015-10-14 11:33:11 -07002838 EXPECT_TRUE(
2839 CompareStreamCollections(observer_.remote_streams(), reference.get()));
2840
2841 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2842 EXPECT_EQ(0u, observer_.remote_streams()->count());
2843}
2844
2845// This tests that an RtpSender is created when the local description is set
2846// after adding a local stream.
2847// TODO(deadbeef): This test and the one below it need to be updated when
2848// an RtpSender's lifetime isn't determined by when a local description is set.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002849TEST_F(PeerConnectionInterfaceTest, LocalDescriptionChanged) {
deadbeefab9b2d12015-10-14 11:33:11 -07002850 FakeConstraints constraints;
2851 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2852 true);
2853 CreatePeerConnection(&constraints);
deadbeefab9b2d12015-10-14 11:33:11 -07002854
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002855 // Create an offer with 1 stream with 2 tracks of each type.
2856 rtc::scoped_refptr<StreamCollection> stream_collection =
2857 CreateStreamCollection(1, 2);
2858 pc_->AddStream(stream_collection->at(0));
2859 std::unique_ptr<SessionDescriptionInterface> offer;
2860 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07002861 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07002862
deadbeefab9b2d12015-10-14 11:33:11 -07002863 auto senders = pc_->GetSenders();
2864 EXPECT_EQ(4u, senders.size());
2865 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2866 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2867 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[1]));
2868 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[1]));
2869
2870 // Remove an audio and video track.
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002871 pc_->RemoveStream(stream_collection->at(0));
2872 stream_collection = CreateStreamCollection(1, 1);
2873 pc_->AddStream(stream_collection->at(0));
2874 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07002875 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002876
deadbeefab9b2d12015-10-14 11:33:11 -07002877 senders = pc_->GetSenders();
2878 EXPECT_EQ(2u, senders.size());
2879 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2880 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2881 EXPECT_FALSE(ContainsSender(senders, kAudioTracks[1]));
2882 EXPECT_FALSE(ContainsSender(senders, kVideoTracks[1]));
2883}
2884
2885// This tests that an RtpSender is created when the local description is set
2886// before adding a local stream.
2887TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002888 AddLocalStreamAfterLocalDescriptionChanged) {
deadbeefab9b2d12015-10-14 11:33:11 -07002889 FakeConstraints constraints;
2890 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2891 true);
2892 CreatePeerConnection(&constraints);
deadbeefab9b2d12015-10-14 11:33:11 -07002893
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002894 rtc::scoped_refptr<StreamCollection> stream_collection =
2895 CreateStreamCollection(1, 2);
2896 // Add a stream to create the offer, but remove it afterwards.
2897 pc_->AddStream(stream_collection->at(0));
2898 std::unique_ptr<SessionDescriptionInterface> offer;
2899 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2900 pc_->RemoveStream(stream_collection->at(0));
deadbeefab9b2d12015-10-14 11:33:11 -07002901
Steve Antondb45ca82017-09-11 18:27:34 -07002902 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07002903 auto senders = pc_->GetSenders();
2904 EXPECT_EQ(0u, senders.size());
2905
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002906 pc_->AddStream(stream_collection->at(0));
deadbeefab9b2d12015-10-14 11:33:11 -07002907 senders = pc_->GetSenders();
2908 EXPECT_EQ(4u, senders.size());
2909 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2910 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2911 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[1]));
2912 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[1]));
2913}
2914
2915// This tests that the expected behavior occurs if the SSRC on a local track is
2916// changed when SetLocalDescription is called.
2917TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002918 ChangeSsrcOnTrackInLocalSessionDescription) {
deadbeefab9b2d12015-10-14 11:33:11 -07002919 FakeConstraints constraints;
2920 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2921 true);
2922 CreatePeerConnection(&constraints);
deadbeefab9b2d12015-10-14 11:33:11 -07002923
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002924 rtc::scoped_refptr<StreamCollection> stream_collection =
2925 CreateStreamCollection(2, 1);
2926 pc_->AddStream(stream_collection->at(0));
2927 std::unique_ptr<SessionDescriptionInterface> offer;
2928 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2929 // Grab a copy of the offer before it gets passed into the PC.
Steve Antona3a92c22017-12-07 10:27:41 -08002930 auto modified_offer =
2931 rtc::MakeUnique<webrtc::JsepSessionDescription>(webrtc::SdpType::kOffer);
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002932 modified_offer->Initialize(offer->description()->Copy(), offer->session_id(),
2933 offer->session_version());
Steve Antondb45ca82017-09-11 18:27:34 -07002934 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07002935
deadbeefab9b2d12015-10-14 11:33:11 -07002936 auto senders = pc_->GetSenders();
2937 EXPECT_EQ(2u, senders.size());
2938 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2939 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2940
2941 // Change the ssrc of the audio and video track.
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002942 cricket::MediaContentDescription* desc =
2943 cricket::GetFirstAudioContentDescription(modified_offer->description());
2944 ASSERT_TRUE(desc != NULL);
2945 for (StreamParams& stream : desc->mutable_streams()) {
2946 for (unsigned int& ssrc : stream.ssrcs) {
2947 ++ssrc;
2948 }
2949 }
deadbeefab9b2d12015-10-14 11:33:11 -07002950
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002951 desc =
2952 cricket::GetFirstVideoContentDescription(modified_offer->description());
2953 ASSERT_TRUE(desc != NULL);
2954 for (StreamParams& stream : desc->mutable_streams()) {
2955 for (unsigned int& ssrc : stream.ssrcs) {
2956 ++ssrc;
2957 }
2958 }
2959
Steve Antondb45ca82017-09-11 18:27:34 -07002960 EXPECT_TRUE(DoSetLocalDescription(std::move(modified_offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07002961 senders = pc_->GetSenders();
2962 EXPECT_EQ(2u, senders.size());
2963 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2964 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2965 // TODO(deadbeef): Once RtpSenders expose parameters, check that the SSRC
2966 // changed.
2967}
2968
2969// This tests that the expected behavior occurs if a new session description is
2970// set with the same tracks, but on a different MediaStream.
Stefan Holmer55d6e7c2016-03-17 16:26:40 +01002971TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002972 SignalSameTracksInSeparateMediaStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002973 FakeConstraints constraints;
2974 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2975 true);
2976 CreatePeerConnection(&constraints);
deadbeefab9b2d12015-10-14 11:33:11 -07002977
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002978 rtc::scoped_refptr<StreamCollection> stream_collection =
2979 CreateStreamCollection(2, 1);
2980 pc_->AddStream(stream_collection->at(0));
2981 std::unique_ptr<SessionDescriptionInterface> offer;
2982 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07002983 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07002984
deadbeefab9b2d12015-10-14 11:33:11 -07002985 auto senders = pc_->GetSenders();
2986 EXPECT_EQ(2u, senders.size());
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002987 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0], kStreams[0]));
2988 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0], kStreams[0]));
deadbeefab9b2d12015-10-14 11:33:11 -07002989
2990 // Add a new MediaStream but with the same tracks as in the first stream.
2991 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream_1(
2992 webrtc::MediaStream::Create(kStreams[1]));
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002993 stream_1->AddTrack(stream_collection->at(0)->GetVideoTracks()[0]);
2994 stream_1->AddTrack(stream_collection->at(0)->GetAudioTracks()[0]);
deadbeefab9b2d12015-10-14 11:33:11 -07002995 pc_->AddStream(stream_1);
2996
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002997 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07002998 EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
deadbeefab9b2d12015-10-14 11:33:11 -07002999
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07003000 auto new_senders = pc_->GetSenders();
3001 // Should be the same senders as before, but with updated stream id.
3002 // Note that this behavior is subject to change in the future.
3003 // We may decide the PC should ignore existing tracks in AddStream.
3004 EXPECT_EQ(senders, new_senders);
3005 EXPECT_TRUE(ContainsSender(new_senders, kAudioTracks[0], kStreams[1]));
3006 EXPECT_TRUE(ContainsSender(new_senders, kVideoTracks[0], kStreams[1]));
deadbeefab9b2d12015-10-14 11:33:11 -07003007}
3008
zhihuang81c3a032016-11-17 12:06:24 -08003009// This tests that PeerConnectionObserver::OnAddTrack is correctly called.
3010TEST_F(PeerConnectionInterfaceTest, OnAddTrackCallback) {
3011 FakeConstraints constraints;
3012 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
3013 true);
3014 CreatePeerConnection(&constraints);
3015 CreateAndSetRemoteOffer(kSdpStringWithStream1AudioTrackOnly);
3016 EXPECT_EQ(observer_.num_added_tracks_, 1);
3017 EXPECT_EQ(observer_.last_added_track_label_, kAudioTracks[0]);
3018
3019 // Create and set the updated remote SDP.
3020 CreateAndSetRemoteOffer(kSdpStringWithStream1);
3021 EXPECT_EQ(observer_.num_added_tracks_, 2);
3022 EXPECT_EQ(observer_.last_added_track_label_, kVideoTracks[0]);
3023}
3024
deadbeefd1a38b52016-12-10 13:15:33 -08003025// Test that when SetConfiguration is called and the configuration is
3026// changing, the next offer causes an ICE restart.
3027TEST_F(PeerConnectionInterfaceTest, SetConfigurationCausingIceRetart) {
3028 PeerConnectionInterface::RTCConfiguration config;
3029 config.type = PeerConnectionInterface::kRelay;
3030 // Need to pass default constraints to prevent disabling of DTLS...
3031 FakeConstraints default_constraints;
3032 CreatePeerConnection(config, &default_constraints);
3033 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
3034
3035 // Do initial offer/answer so there's something to restart.
3036 CreateOfferAsLocalDescription();
3037 CreateAnswerAsRemoteDescription(kSdpStringWithStream1);
3038
3039 // Grab the ufrags.
3040 std::vector<std::string> initial_ufrags = GetUfrags(pc_->local_description());
3041
3042 // Change ICE policy, which should trigger an ICE restart on the next offer.
3043 config.type = PeerConnectionInterface::kAll;
3044 EXPECT_TRUE(pc_->SetConfiguration(config));
3045 CreateOfferAsLocalDescription();
3046
3047 // Grab the new ufrags.
3048 std::vector<std::string> subsequent_ufrags =
3049 GetUfrags(pc_->local_description());
3050
3051 // Sanity check.
3052 EXPECT_EQ(initial_ufrags.size(), subsequent_ufrags.size());
3053 // Check that each ufrag is different.
3054 for (int i = 0; i < static_cast<int>(initial_ufrags.size()); ++i) {
3055 EXPECT_NE(initial_ufrags[i], subsequent_ufrags[i]);
3056 }
3057}
3058
3059// Test that when SetConfiguration is called and the configuration *isn't*
3060// changing, the next offer does *not* cause an ICE restart.
3061TEST_F(PeerConnectionInterfaceTest, SetConfigurationNotCausingIceRetart) {
3062 PeerConnectionInterface::RTCConfiguration config;
3063 config.type = PeerConnectionInterface::kRelay;
3064 // Need to pass default constraints to prevent disabling of DTLS...
3065 FakeConstraints default_constraints;
3066 CreatePeerConnection(config, &default_constraints);
3067 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
3068
3069 // Do initial offer/answer so there's something to restart.
3070 CreateOfferAsLocalDescription();
3071 CreateAnswerAsRemoteDescription(kSdpStringWithStream1);
3072
3073 // Grab the ufrags.
3074 std::vector<std::string> initial_ufrags = GetUfrags(pc_->local_description());
3075
3076 // Call SetConfiguration with a config identical to what the PC was
3077 // constructed with.
3078 EXPECT_TRUE(pc_->SetConfiguration(config));
3079 CreateOfferAsLocalDescription();
3080
3081 // Grab the new ufrags.
3082 std::vector<std::string> subsequent_ufrags =
3083 GetUfrags(pc_->local_description());
3084
3085 EXPECT_EQ(initial_ufrags, subsequent_ufrags);
3086}
3087
3088// Test for a weird corner case scenario:
3089// 1. Audio/video session established.
3090// 2. SetConfiguration changes ICE config; ICE restart needed.
3091// 3. ICE restart initiated by remote peer, but only for one m= section.
3092// 4. Next createOffer should initiate an ICE restart, but only for the other
3093// m= section; it would be pointless to do an ICE restart for the m= section
3094// that was already restarted.
3095TEST_F(PeerConnectionInterfaceTest, SetConfigurationCausingPartialIceRestart) {
3096 PeerConnectionInterface::RTCConfiguration config;
3097 config.type = PeerConnectionInterface::kRelay;
3098 // Need to pass default constraints to prevent disabling of DTLS...
3099 FakeConstraints default_constraints;
3100 CreatePeerConnection(config, &default_constraints);
3101 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
3102
3103 // Do initial offer/answer so there's something to restart.
3104 CreateOfferAsLocalDescription();
3105 CreateAnswerAsRemoteDescription(kSdpStringWithStream1);
3106
3107 // Change ICE policy, which should set the "needs-ice-restart" flag.
3108 config.type = PeerConnectionInterface::kAll;
3109 EXPECT_TRUE(pc_->SetConfiguration(config));
3110
3111 // Do ICE restart for the first m= section, initiated by remote peer.
Steve Antondb45ca82017-09-11 18:27:34 -07003112 std::unique_ptr<webrtc::SessionDescriptionInterface> remote_offer(
Steve Antona3a92c22017-12-07 10:27:41 -08003113 webrtc::CreateSessionDescription(SdpType::kOffer, kSdpStringWithStream1,
3114 nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07003115 ASSERT_TRUE(remote_offer);
deadbeefd1a38b52016-12-10 13:15:33 -08003116 remote_offer->description()->transport_infos()[0].description.ice_ufrag =
3117 "modified";
Steve Antondb45ca82017-09-11 18:27:34 -07003118 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_offer)));
deadbeefd1a38b52016-12-10 13:15:33 -08003119 CreateAnswerAsLocalDescription();
3120
3121 // Grab the ufrags.
3122 std::vector<std::string> initial_ufrags = GetUfrags(pc_->local_description());
3123 ASSERT_EQ(2, initial_ufrags.size());
3124
3125 // Create offer and grab the new ufrags.
3126 CreateOfferAsLocalDescription();
3127 std::vector<std::string> subsequent_ufrags =
3128 GetUfrags(pc_->local_description());
3129 ASSERT_EQ(2, subsequent_ufrags.size());
3130
3131 // Ensure that only the ufrag for the second m= section changed.
3132 EXPECT_EQ(initial_ufrags[0], subsequent_ufrags[0]);
3133 EXPECT_NE(initial_ufrags[1], subsequent_ufrags[1]);
3134}
3135
deadbeeffe4a8a42016-12-20 17:56:17 -08003136// Tests that the methods to return current/pending descriptions work as
3137// expected at different points in the offer/answer exchange. This test does
3138// one offer/answer exchange as the offerer, then another as the answerer.
3139TEST_F(PeerConnectionInterfaceTest, CurrentAndPendingDescriptions) {
3140 // This disables DTLS so we can apply an answer to ourselves.
3141 CreatePeerConnection();
3142
3143 // Create initial local offer and get SDP (which will also be used as
3144 // answer/pranswer);
Steve Antondb45ca82017-09-11 18:27:34 -07003145 std::unique_ptr<SessionDescriptionInterface> local_offer;
3146 ASSERT_TRUE(DoCreateOffer(&local_offer, nullptr));
deadbeeffe4a8a42016-12-20 17:56:17 -08003147 std::string sdp;
Steve Antondb45ca82017-09-11 18:27:34 -07003148 EXPECT_TRUE(local_offer->ToString(&sdp));
deadbeeffe4a8a42016-12-20 17:56:17 -08003149
3150 // Set local offer.
Steve Antondb45ca82017-09-11 18:27:34 -07003151 SessionDescriptionInterface* local_offer_ptr = local_offer.get();
3152 EXPECT_TRUE(DoSetLocalDescription(std::move(local_offer)));
3153 EXPECT_EQ(local_offer_ptr, pc_->pending_local_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003154 EXPECT_EQ(nullptr, pc_->pending_remote_description());
3155 EXPECT_EQ(nullptr, pc_->current_local_description());
3156 EXPECT_EQ(nullptr, pc_->current_remote_description());
3157
3158 // Set remote pranswer.
Steve Antondb45ca82017-09-11 18:27:34 -07003159 std::unique_ptr<SessionDescriptionInterface> remote_pranswer(
Steve Antona3a92c22017-12-07 10:27:41 -08003160 webrtc::CreateSessionDescription(SdpType::kPrAnswer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07003161 SessionDescriptionInterface* remote_pranswer_ptr = remote_pranswer.get();
3162 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_pranswer)));
3163 EXPECT_EQ(local_offer_ptr, pc_->pending_local_description());
3164 EXPECT_EQ(remote_pranswer_ptr, pc_->pending_remote_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003165 EXPECT_EQ(nullptr, pc_->current_local_description());
3166 EXPECT_EQ(nullptr, pc_->current_remote_description());
3167
3168 // Set remote answer.
Steve Antondb45ca82017-09-11 18:27:34 -07003169 std::unique_ptr<SessionDescriptionInterface> remote_answer(
Steve Antona3a92c22017-12-07 10:27:41 -08003170 webrtc::CreateSessionDescription(SdpType::kAnswer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07003171 SessionDescriptionInterface* remote_answer_ptr = remote_answer.get();
3172 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_answer)));
deadbeeffe4a8a42016-12-20 17:56:17 -08003173 EXPECT_EQ(nullptr, pc_->pending_local_description());
3174 EXPECT_EQ(nullptr, pc_->pending_remote_description());
Steve Antondb45ca82017-09-11 18:27:34 -07003175 EXPECT_EQ(local_offer_ptr, pc_->current_local_description());
3176 EXPECT_EQ(remote_answer_ptr, pc_->current_remote_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003177
3178 // Set remote offer.
Steve Antondb45ca82017-09-11 18:27:34 -07003179 std::unique_ptr<SessionDescriptionInterface> remote_offer(
Steve Antona3a92c22017-12-07 10:27:41 -08003180 webrtc::CreateSessionDescription(SdpType::kOffer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07003181 SessionDescriptionInterface* remote_offer_ptr = remote_offer.get();
3182 EXPECT_TRUE(DoSetRemoteDescription(std::move(remote_offer)));
3183 EXPECT_EQ(remote_offer_ptr, pc_->pending_remote_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003184 EXPECT_EQ(nullptr, pc_->pending_local_description());
Steve Antondb45ca82017-09-11 18:27:34 -07003185 EXPECT_EQ(local_offer_ptr, pc_->current_local_description());
3186 EXPECT_EQ(remote_answer_ptr, pc_->current_remote_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003187
3188 // Set local pranswer.
Steve Antondb45ca82017-09-11 18:27:34 -07003189 std::unique_ptr<SessionDescriptionInterface> local_pranswer(
Steve Antona3a92c22017-12-07 10:27:41 -08003190 webrtc::CreateSessionDescription(SdpType::kPrAnswer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07003191 SessionDescriptionInterface* local_pranswer_ptr = local_pranswer.get();
3192 EXPECT_TRUE(DoSetLocalDescription(std::move(local_pranswer)));
3193 EXPECT_EQ(remote_offer_ptr, pc_->pending_remote_description());
3194 EXPECT_EQ(local_pranswer_ptr, pc_->pending_local_description());
3195 EXPECT_EQ(local_offer_ptr, pc_->current_local_description());
3196 EXPECT_EQ(remote_answer_ptr, pc_->current_remote_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003197
3198 // Set local answer.
Steve Antondb45ca82017-09-11 18:27:34 -07003199 std::unique_ptr<SessionDescriptionInterface> local_answer(
Steve Antona3a92c22017-12-07 10:27:41 -08003200 webrtc::CreateSessionDescription(SdpType::kAnswer, sdp));
Steve Antondb45ca82017-09-11 18:27:34 -07003201 SessionDescriptionInterface* local_answer_ptr = local_answer.get();
3202 EXPECT_TRUE(DoSetLocalDescription(std::move(local_answer)));
deadbeeffe4a8a42016-12-20 17:56:17 -08003203 EXPECT_EQ(nullptr, pc_->pending_remote_description());
3204 EXPECT_EQ(nullptr, pc_->pending_local_description());
Steve Antondb45ca82017-09-11 18:27:34 -07003205 EXPECT_EQ(remote_offer_ptr, pc_->current_remote_description());
3206 EXPECT_EQ(local_answer_ptr, pc_->current_local_description());
deadbeeffe4a8a42016-12-20 17:56:17 -08003207}
3208
zhihuang77985012017-02-07 15:45:16 -08003209// Tests that it won't crash when calling StartRtcEventLog or StopRtcEventLog
3210// after the PeerConnection is closed.
Elad Alon99c3fe52017-10-13 16:29:40 +02003211// This version tests the StartRtcEventLog version that receives a file.
zhihuang77985012017-02-07 15:45:16 -08003212TEST_F(PeerConnectionInterfaceTest,
Elad Alon99c3fe52017-10-13 16:29:40 +02003213 StartAndStopLoggingToFileAfterPeerConnectionClosed) {
zhihuang77985012017-02-07 15:45:16 -08003214 CreatePeerConnection();
3215 // The RtcEventLog will be reset when the PeerConnection is closed.
3216 pc_->Close();
3217
Elad Alon9e6565b2017-10-11 16:04:13 +02003218 auto test_info = ::testing::UnitTest::GetInstance()->current_test_info();
3219 std::string filename = webrtc::test::OutputPath() +
3220 test_info->test_case_name() + test_info->name();
3221 rtc::PlatformFile file = rtc::CreatePlatformFile(filename);
3222
3223 constexpr int64_t max_size_bytes = 1024;
3224
zhihuang77985012017-02-07 15:45:16 -08003225 EXPECT_FALSE(pc_->StartRtcEventLog(file, max_size_bytes));
3226 pc_->StopRtcEventLog();
Elad Alon9e6565b2017-10-11 16:04:13 +02003227
3228 // Cleanup.
3229 rtc::ClosePlatformFile(file);
3230 rtc::RemoveFile(filename);
zhihuang77985012017-02-07 15:45:16 -08003231}
3232
Elad Alon99c3fe52017-10-13 16:29:40 +02003233// Tests that it won't crash when calling StartRtcEventLog or StopRtcEventLog
3234// after the PeerConnection is closed.
3235// This version tests the StartRtcEventLog version that receives an object
3236// of type |RtcEventLogOutput|.
3237TEST_F(PeerConnectionInterfaceTest,
3238 StartAndStopLoggingToOutputAfterPeerConnectionClosed) {
3239 CreatePeerConnection();
3240 // The RtcEventLog will be reset when the PeerConnection is closed.
3241 pc_->Close();
3242
3243 rtc::PlatformFile file = 0;
3244 int64_t max_size_bytes = 1024;
3245 EXPECT_FALSE(pc_->StartRtcEventLog(
Bjorn Tereliusde939432017-11-20 17:38:14 +01003246 rtc::MakeUnique<webrtc::RtcEventLogOutputFile>(file, max_size_bytes),
3247 webrtc::RtcEventLog::kImmediateOutput));
Elad Alon99c3fe52017-10-13 16:29:40 +02003248 pc_->StopRtcEventLog();
3249}
3250
deadbeef30952b42017-04-21 02:41:29 -07003251// Test that generated offers/answers include "ice-option:trickle".
3252TEST_F(PeerConnectionInterfaceTest, OffersAndAnswersHaveTrickleIceOption) {
3253 CreatePeerConnection();
3254
3255 // First, create an offer with audio/video.
3256 FakeConstraints constraints;
3257 constraints.SetMandatoryReceiveAudio(true);
3258 constraints.SetMandatoryReceiveVideo(true);
3259 std::unique_ptr<SessionDescriptionInterface> offer;
3260 ASSERT_TRUE(DoCreateOffer(&offer, &constraints));
3261 cricket::SessionDescription* desc = offer->description();
3262 ASSERT_EQ(2u, desc->transport_infos().size());
3263 EXPECT_TRUE(desc->transport_infos()[0].description.HasOption("trickle"));
3264 EXPECT_TRUE(desc->transport_infos()[1].description.HasOption("trickle"));
3265
3266 // Apply the offer as a remote description, then create an answer.
Steve Antondb45ca82017-09-11 18:27:34 -07003267 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
deadbeef30952b42017-04-21 02:41:29 -07003268 std::unique_ptr<SessionDescriptionInterface> answer;
3269 ASSERT_TRUE(DoCreateAnswer(&answer, &constraints));
3270 desc = answer->description();
3271 ASSERT_EQ(2u, desc->transport_infos().size());
3272 EXPECT_TRUE(desc->transport_infos()[0].description.HasOption("trickle"));
3273 EXPECT_TRUE(desc->transport_infos()[1].description.HasOption("trickle"));
3274}
3275
deadbeef1dcb1642017-03-29 21:08:16 -07003276// Test that ICE renomination isn't offered if it's not enabled in the PC's
3277// RTCConfiguration.
3278TEST_F(PeerConnectionInterfaceTest, IceRenominationNotOffered) {
3279 PeerConnectionInterface::RTCConfiguration config;
3280 config.enable_ice_renomination = false;
3281 CreatePeerConnection(config, nullptr);
3282 AddVoiceStream("foo");
3283
3284 std::unique_ptr<SessionDescriptionInterface> offer;
3285 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
3286 cricket::SessionDescription* desc = offer->description();
3287 EXPECT_EQ(1u, desc->transport_infos().size());
3288 EXPECT_FALSE(
3289 desc->transport_infos()[0].description.GetIceParameters().renomination);
3290}
3291
3292// Test that the ICE renomination option is present in generated offers/answers
3293// if it's enabled in the PC's RTCConfiguration.
3294TEST_F(PeerConnectionInterfaceTest, IceRenominationOptionInOfferAndAnswer) {
3295 PeerConnectionInterface::RTCConfiguration config;
3296 config.enable_ice_renomination = true;
3297 CreatePeerConnection(config, nullptr);
3298 AddVoiceStream("foo");
3299
3300 std::unique_ptr<SessionDescriptionInterface> offer;
3301 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
3302 cricket::SessionDescription* desc = offer->description();
3303 EXPECT_EQ(1u, desc->transport_infos().size());
3304 EXPECT_TRUE(
3305 desc->transport_infos()[0].description.GetIceParameters().renomination);
3306
3307 // Set the offer as a remote description, then create an answer and ensure it
3308 // has the renomination flag too.
Steve Antondb45ca82017-09-11 18:27:34 -07003309 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
deadbeef1dcb1642017-03-29 21:08:16 -07003310 std::unique_ptr<SessionDescriptionInterface> answer;
3311 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
3312 desc = answer->description();
3313 EXPECT_EQ(1u, desc->transport_infos().size());
3314 EXPECT_TRUE(
3315 desc->transport_infos()[0].description.GetIceParameters().renomination);
3316}
3317
3318// Test that if CreateOffer is called with the deprecated "offer to receive
3319// audio/video" constraints, they're processed and result in an offer with
3320// audio/video sections just as if RTCOfferAnswerOptions had been used.
3321TEST_F(PeerConnectionInterfaceTest, CreateOfferWithOfferToReceiveConstraints) {
3322 CreatePeerConnection();
3323
3324 FakeConstraints constraints;
3325 constraints.SetMandatoryReceiveAudio(true);
3326 constraints.SetMandatoryReceiveVideo(true);
3327 std::unique_ptr<SessionDescriptionInterface> offer;
3328 ASSERT_TRUE(DoCreateOffer(&offer, &constraints));
3329
3330 cricket::SessionDescription* desc = offer->description();
3331 const cricket::ContentInfo* audio = cricket::GetFirstAudioContent(desc);
3332 const cricket::ContentInfo* video = cricket::GetFirstVideoContent(desc);
3333 ASSERT_NE(nullptr, audio);
3334 ASSERT_NE(nullptr, video);
3335 EXPECT_FALSE(audio->rejected);
3336 EXPECT_FALSE(video->rejected);
3337}
3338
3339// Test that if CreateAnswer is called with the deprecated "offer to receive
3340// audio/video" constraints, they're processed and can be used to reject an
3341// offered m= section just as can be done with RTCOfferAnswerOptions;
3342TEST_F(PeerConnectionInterfaceTest, CreateAnswerWithOfferToReceiveConstraints) {
3343 CreatePeerConnection();
3344
3345 // First, create an offer with audio/video and apply it as a remote
3346 // description.
3347 FakeConstraints constraints;
3348 constraints.SetMandatoryReceiveAudio(true);
3349 constraints.SetMandatoryReceiveVideo(true);
3350 std::unique_ptr<SessionDescriptionInterface> offer;
3351 ASSERT_TRUE(DoCreateOffer(&offer, &constraints));
Steve Antondb45ca82017-09-11 18:27:34 -07003352 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
deadbeef1dcb1642017-03-29 21:08:16 -07003353
3354 // Now create answer that rejects audio/video.
3355 constraints.SetMandatoryReceiveAudio(false);
3356 constraints.SetMandatoryReceiveVideo(false);
3357 std::unique_ptr<SessionDescriptionInterface> answer;
3358 ASSERT_TRUE(DoCreateAnswer(&answer, &constraints));
3359
3360 cricket::SessionDescription* desc = answer->description();
3361 const cricket::ContentInfo* audio = cricket::GetFirstAudioContent(desc);
3362 const cricket::ContentInfo* video = cricket::GetFirstVideoContent(desc);
3363 ASSERT_NE(nullptr, audio);
3364 ASSERT_NE(nullptr, video);
3365 EXPECT_TRUE(audio->rejected);
3366 EXPECT_TRUE(video->rejected);
3367}
3368
3369#ifdef HAVE_SCTP
3370#define MAYBE_DataChannelOnlyOfferWithMaxBundlePolicy \
3371 DataChannelOnlyOfferWithMaxBundlePolicy
3372#else
3373#define MAYBE_DataChannelOnlyOfferWithMaxBundlePolicy \
3374 DISABLED_DataChannelOnlyOfferWithMaxBundlePolicy
3375#endif
3376
3377// Test that negotiation can succeed with a data channel only, and with the max
3378// bundle policy. Previously there was a bug that prevented this.
3379TEST_F(PeerConnectionInterfaceTest,
3380 MAYBE_DataChannelOnlyOfferWithMaxBundlePolicy) {
3381 PeerConnectionInterface::RTCConfiguration config;
3382 config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
3383 CreatePeerConnection(config, nullptr);
3384
3385 // First, create an offer with only a data channel and apply it as a remote
3386 // description.
3387 pc_->CreateDataChannel("test", nullptr);
3388 std::unique_ptr<SessionDescriptionInterface> offer;
3389 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07003390 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
deadbeef1dcb1642017-03-29 21:08:16 -07003391
3392 // Create and set answer as well.
3393 std::unique_ptr<SessionDescriptionInterface> answer;
3394 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
Steve Antondb45ca82017-09-11 18:27:34 -07003395 EXPECT_TRUE(DoSetLocalDescription(std::move(answer)));
deadbeef1dcb1642017-03-29 21:08:16 -07003396}
3397
zstein4b979802017-06-02 14:37:37 -07003398TEST_F(PeerConnectionInterfaceTest, SetBitrateWithoutMinSucceeds) {
3399 CreatePeerConnection();
3400 PeerConnectionInterface::BitrateParameters bitrate;
Oskar Sundbomb95fd2c2017-11-16 10:54:38 +01003401 bitrate.current_bitrate_bps = 100000;
zstein4b979802017-06-02 14:37:37 -07003402 EXPECT_TRUE(pc_->SetBitrate(bitrate).ok());
3403}
3404
3405TEST_F(PeerConnectionInterfaceTest, SetBitrateNegativeMinFails) {
3406 CreatePeerConnection();
3407 PeerConnectionInterface::BitrateParameters bitrate;
Oskar Sundbomb95fd2c2017-11-16 10:54:38 +01003408 bitrate.min_bitrate_bps = -1;
zstein4b979802017-06-02 14:37:37 -07003409 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3410}
3411
3412TEST_F(PeerConnectionInterfaceTest, SetBitrateCurrentLessThanMinFails) {
3413 CreatePeerConnection();
3414 PeerConnectionInterface::BitrateParameters bitrate;
Oskar Sundbomb95fd2c2017-11-16 10:54:38 +01003415 bitrate.min_bitrate_bps = 5;
3416 bitrate.current_bitrate_bps = 3;
zstein4b979802017-06-02 14:37:37 -07003417 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3418}
3419
3420TEST_F(PeerConnectionInterfaceTest, SetBitrateCurrentNegativeFails) {
3421 CreatePeerConnection();
3422 PeerConnectionInterface::BitrateParameters bitrate;
Oskar Sundbomb95fd2c2017-11-16 10:54:38 +01003423 bitrate.current_bitrate_bps = -1;
zstein4b979802017-06-02 14:37:37 -07003424 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3425}
3426
3427TEST_F(PeerConnectionInterfaceTest, SetBitrateMaxLessThanCurrentFails) {
3428 CreatePeerConnection();
3429 PeerConnectionInterface::BitrateParameters bitrate;
Oskar Sundbomb95fd2c2017-11-16 10:54:38 +01003430 bitrate.current_bitrate_bps = 10;
3431 bitrate.max_bitrate_bps = 8;
zstein4b979802017-06-02 14:37:37 -07003432 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3433}
3434
3435TEST_F(PeerConnectionInterfaceTest, SetBitrateMaxLessThanMinFails) {
3436 CreatePeerConnection();
3437 PeerConnectionInterface::BitrateParameters bitrate;
Oskar Sundbomb95fd2c2017-11-16 10:54:38 +01003438 bitrate.min_bitrate_bps = 10;
3439 bitrate.max_bitrate_bps = 8;
zstein4b979802017-06-02 14:37:37 -07003440 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3441}
3442
3443TEST_F(PeerConnectionInterfaceTest, SetBitrateMaxNegativeFails) {
3444 CreatePeerConnection();
3445 PeerConnectionInterface::BitrateParameters bitrate;
Oskar Sundbomb95fd2c2017-11-16 10:54:38 +01003446 bitrate.max_bitrate_bps = -1;
zstein4b979802017-06-02 14:37:37 -07003447 EXPECT_FALSE(pc_->SetBitrate(bitrate).ok());
3448}
3449
Steve Anton038834f2017-07-14 15:59:59 -07003450// ice_regather_interval_range requires WebRTC to be configured for continual
3451// gathering already.
3452TEST_F(PeerConnectionInterfaceTest,
3453 SetIceRegatherIntervalRangeWithoutContinualGatheringFails) {
3454 PeerConnectionInterface::RTCConfiguration config;
3455 config.ice_regather_interval_range.emplace(1000, 2000);
3456 config.continual_gathering_policy =
3457 PeerConnectionInterface::ContinualGatheringPolicy::GATHER_ONCE;
3458 CreatePeerConnectionExpectFail(config);
3459}
3460
3461// Ensures that there is no error when ice_regather_interval_range is set with
3462// continual gathering enabled.
3463TEST_F(PeerConnectionInterfaceTest,
3464 SetIceRegatherIntervalRangeWithContinualGathering) {
3465 PeerConnectionInterface::RTCConfiguration config;
3466 config.ice_regather_interval_range.emplace(1000, 2000);
3467 config.continual_gathering_policy =
3468 PeerConnectionInterface::ContinualGatheringPolicy::GATHER_CONTINUALLY;
3469 CreatePeerConnection(config, nullptr);
3470}
3471
zstein4b979802017-06-02 14:37:37 -07003472// The current bitrate from Call's BitrateConfigMask is currently clamped by
3473// Call's BitrateConfig, which comes from the SDP or a default value. This test
3474// checks that a call to SetBitrate with a current bitrate that will be clamped
3475// succeeds.
3476TEST_F(PeerConnectionInterfaceTest, SetBitrateCurrentLessThanImplicitMin) {
3477 CreatePeerConnection();
3478 PeerConnectionInterface::BitrateParameters bitrate;
Oskar Sundbomb95fd2c2017-11-16 10:54:38 +01003479 bitrate.current_bitrate_bps = 1;
zstein4b979802017-06-02 14:37:37 -07003480 EXPECT_TRUE(pc_->SetBitrate(bitrate).ok());
3481}
3482
zhihuang1c378ed2017-08-17 14:10:50 -07003483// The following tests verify that the offer can be created correctly.
3484TEST_F(PeerConnectionInterfaceTest,
3485 CreateOfferFailsWithInvalidOfferToReceiveAudio) {
3486 RTCOfferAnswerOptions rtc_options;
3487
3488 // Setting offer_to_receive_audio to a value lower than kUndefined or greater
3489 // than kMaxOfferToReceiveMedia should be treated as invalid.
3490 rtc_options.offer_to_receive_audio = RTCOfferAnswerOptions::kUndefined - 1;
3491 CreatePeerConnection();
3492 EXPECT_FALSE(CreateOfferWithOptions(rtc_options));
3493
3494 rtc_options.offer_to_receive_audio =
3495 RTCOfferAnswerOptions::kMaxOfferToReceiveMedia + 1;
3496 EXPECT_FALSE(CreateOfferWithOptions(rtc_options));
3497}
3498
3499TEST_F(PeerConnectionInterfaceTest,
3500 CreateOfferFailsWithInvalidOfferToReceiveVideo) {
3501 RTCOfferAnswerOptions rtc_options;
3502
3503 // Setting offer_to_receive_video to a value lower than kUndefined or greater
3504 // than kMaxOfferToReceiveMedia should be treated as invalid.
3505 rtc_options.offer_to_receive_video = RTCOfferAnswerOptions::kUndefined - 1;
3506 CreatePeerConnection();
3507 EXPECT_FALSE(CreateOfferWithOptions(rtc_options));
3508
3509 rtc_options.offer_to_receive_video =
3510 RTCOfferAnswerOptions::kMaxOfferToReceiveMedia + 1;
3511 EXPECT_FALSE(CreateOfferWithOptions(rtc_options));
3512}
3513
3514// Test that the audio and video content will be added to an offer if both
3515// |offer_to_receive_audio| and |offer_to_receive_video| options are 1.
3516TEST_F(PeerConnectionInterfaceTest, CreateOfferWithAudioVideoOptions) {
3517 RTCOfferAnswerOptions rtc_options;
3518 rtc_options.offer_to_receive_audio = 1;
3519 rtc_options.offer_to_receive_video = 1;
3520
3521 std::unique_ptr<SessionDescriptionInterface> offer;
3522 CreatePeerConnection();
3523 offer = CreateOfferWithOptions(rtc_options);
3524 ASSERT_TRUE(offer);
3525 EXPECT_NE(nullptr, GetFirstAudioContent(offer->description()));
3526 EXPECT_NE(nullptr, GetFirstVideoContent(offer->description()));
3527}
3528
3529// Test that only audio content will be added to the offer if only
3530// |offer_to_receive_audio| options is 1.
3531TEST_F(PeerConnectionInterfaceTest, CreateOfferWithAudioOnlyOptions) {
3532 RTCOfferAnswerOptions rtc_options;
3533 rtc_options.offer_to_receive_audio = 1;
3534 rtc_options.offer_to_receive_video = 0;
3535
3536 std::unique_ptr<SessionDescriptionInterface> offer;
3537 CreatePeerConnection();
3538 offer = CreateOfferWithOptions(rtc_options);
3539 ASSERT_TRUE(offer);
3540 EXPECT_NE(nullptr, GetFirstAudioContent(offer->description()));
3541 EXPECT_EQ(nullptr, GetFirstVideoContent(offer->description()));
3542}
3543
3544// Test that only video content will be added if only |offer_to_receive_video|
3545// options is 1.
3546TEST_F(PeerConnectionInterfaceTest, CreateOfferWithVideoOnlyOptions) {
3547 RTCOfferAnswerOptions rtc_options;
3548 rtc_options.offer_to_receive_audio = 0;
3549 rtc_options.offer_to_receive_video = 1;
3550
3551 std::unique_ptr<SessionDescriptionInterface> offer;
3552 CreatePeerConnection();
3553 offer = CreateOfferWithOptions(rtc_options);
3554 ASSERT_TRUE(offer);
3555 EXPECT_EQ(nullptr, GetFirstAudioContent(offer->description()));
3556 EXPECT_NE(nullptr, GetFirstVideoContent(offer->description()));
3557}
3558
zhihuang1c378ed2017-08-17 14:10:50 -07003559// Test that no media content will be added to the offer if using default
3560// RTCOfferAnswerOptions.
3561TEST_F(PeerConnectionInterfaceTest, CreateOfferWithDefaultOfferAnswerOptions) {
3562 RTCOfferAnswerOptions rtc_options;
3563
3564 std::unique_ptr<SessionDescriptionInterface> offer;
3565 CreatePeerConnection();
3566 offer = CreateOfferWithOptions(rtc_options);
3567 ASSERT_TRUE(offer);
3568 EXPECT_EQ(nullptr, GetFirstAudioContent(offer->description()));
3569 EXPECT_EQ(nullptr, GetFirstVideoContent(offer->description()));
3570}
3571
3572// Test that if |ice_restart| is true, the ufrag/pwd will change, otherwise
3573// ufrag/pwd will be the same in the new offer.
3574TEST_F(PeerConnectionInterfaceTest, CreateOfferWithIceRestart) {
3575 RTCOfferAnswerOptions rtc_options;
3576 rtc_options.ice_restart = false;
3577 rtc_options.offer_to_receive_audio = 1;
3578
3579 std::unique_ptr<SessionDescriptionInterface> offer;
3580 CreatePeerConnection();
3581 CreateOfferWithOptionsAsLocalDescription(&offer, rtc_options);
3582 auto ufrag1 = offer->description()
3583 ->GetTransportInfoByName(cricket::CN_AUDIO)
3584 ->description.ice_ufrag;
3585 auto pwd1 = offer->description()
3586 ->GetTransportInfoByName(cricket::CN_AUDIO)
3587 ->description.ice_pwd;
3588
3589 // |ice_restart| is false, the ufrag/pwd shouldn't change.
3590 CreateOfferWithOptionsAsLocalDescription(&offer, rtc_options);
3591 auto ufrag2 = offer->description()
3592 ->GetTransportInfoByName(cricket::CN_AUDIO)
3593 ->description.ice_ufrag;
3594 auto pwd2 = offer->description()
3595 ->GetTransportInfoByName(cricket::CN_AUDIO)
3596 ->description.ice_pwd;
3597
3598 // |ice_restart| is true, the ufrag/pwd should change.
3599 rtc_options.ice_restart = true;
3600 CreateOfferWithOptionsAsLocalDescription(&offer, rtc_options);
3601 auto ufrag3 = offer->description()
3602 ->GetTransportInfoByName(cricket::CN_AUDIO)
3603 ->description.ice_ufrag;
3604 auto pwd3 = offer->description()
3605 ->GetTransportInfoByName(cricket::CN_AUDIO)
3606 ->description.ice_pwd;
3607
3608 EXPECT_EQ(ufrag1, ufrag2);
3609 EXPECT_EQ(pwd1, pwd2);
3610 EXPECT_NE(ufrag2, ufrag3);
3611 EXPECT_NE(pwd2, pwd3);
3612}
3613
3614// Test that if |use_rtp_mux| is true, the bundling will be enabled in the
3615// offer; if it is false, there won't be any bundle group in the offer.
3616TEST_F(PeerConnectionInterfaceTest, CreateOfferWithRtpMux) {
3617 RTCOfferAnswerOptions rtc_options;
3618 rtc_options.offer_to_receive_audio = 1;
3619 rtc_options.offer_to_receive_video = 1;
3620
3621 std::unique_ptr<SessionDescriptionInterface> offer;
3622 CreatePeerConnection();
3623
3624 rtc_options.use_rtp_mux = true;
3625 offer = CreateOfferWithOptions(rtc_options);
3626 ASSERT_TRUE(offer);
3627 EXPECT_NE(nullptr, GetFirstAudioContent(offer->description()));
3628 EXPECT_NE(nullptr, GetFirstVideoContent(offer->description()));
3629 EXPECT_TRUE(offer->description()->HasGroup(cricket::GROUP_TYPE_BUNDLE));
3630
3631 rtc_options.use_rtp_mux = false;
3632 offer = CreateOfferWithOptions(rtc_options);
3633 ASSERT_TRUE(offer);
3634 EXPECT_NE(nullptr, GetFirstAudioContent(offer->description()));
3635 EXPECT_NE(nullptr, GetFirstVideoContent(offer->description()));
3636 EXPECT_FALSE(offer->description()->HasGroup(cricket::GROUP_TYPE_BUNDLE));
3637}
3638
zhihuang141aacb2017-08-29 13:23:53 -07003639// This test ensures OnRenegotiationNeeded is called when we add track with
3640// MediaStream -> AddTrack in the same way it is called when we add track with
3641// PeerConnection -> AddTrack.
3642// The test can be removed once addStream is rewritten in terms of addTrack
3643// https://bugs.chromium.org/p/webrtc/issues/detail?id=7815
3644TEST_F(PeerConnectionInterfaceTest, MediaStreamAddTrackRemoveTrackRenegotiate) {
3645 CreatePeerConnectionWithoutDtls();
3646 rtc::scoped_refptr<MediaStreamInterface> stream(
3647 pc_factory_->CreateLocalMediaStream(kStreamLabel1));
3648 pc_->AddStream(stream);
3649 rtc::scoped_refptr<AudioTrackInterface> audio_track(
3650 pc_factory_->CreateAudioTrack("audio_track", nullptr));
3651 rtc::scoped_refptr<VideoTrackInterface> video_track(
3652 pc_factory_->CreateVideoTrack(
3653 "video_track", pc_factory_->CreateVideoSource(
3654 std::unique_ptr<cricket::VideoCapturer>(
3655 new cricket::FakeVideoCapturer()))));
3656 stream->AddTrack(audio_track);
3657 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
3658 observer_.renegotiation_needed_ = false;
3659
3660 stream->AddTrack(video_track);
3661 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
3662 observer_.renegotiation_needed_ = false;
3663
3664 stream->RemoveTrack(audio_track);
3665 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
3666 observer_.renegotiation_needed_ = false;
3667
3668 stream->RemoveTrack(video_track);
3669 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
3670 observer_.renegotiation_needed_ = false;
3671}
3672
Zhi Huangb2d355e2017-10-26 17:26:37 -07003673// Tests that an error is returned if a description is applied that has fewer
3674// media sections than the existing description.
3675TEST_F(PeerConnectionInterfaceTest,
3676 MediaSectionCountEnforcedForSubsequentOffer) {
3677 CreatePeerConnection();
3678 FakeConstraints constraints;
3679 constraints.SetMandatoryReceiveAudio(true);
3680 constraints.SetMandatoryReceiveVideo(true);
3681 std::unique_ptr<SessionDescriptionInterface> offer;
3682 ASSERT_TRUE(DoCreateOffer(&offer, &constraints));
3683 EXPECT_TRUE(DoSetRemoteDescription(std::move(offer)));
3684
3685 // A remote offer with fewer media sections should be rejected.
3686 ASSERT_TRUE(DoCreateOffer(&offer, &constraints));
3687 offer->description()->contents().pop_back();
3688 offer->description()->contents().pop_back();
3689 ASSERT_TRUE(offer->description()->contents().empty());
3690 EXPECT_FALSE(DoSetRemoteDescription(std::move(offer)));
3691
3692 std::unique_ptr<SessionDescriptionInterface> answer;
3693 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
3694 EXPECT_TRUE(DoSetLocalDescription(std::move(answer)));
3695
3696 // A subsequent local offer with fewer media sections should be rejected.
3697 ASSERT_TRUE(DoCreateOffer(&offer, &constraints));
3698 offer->description()->contents().pop_back();
3699 offer->description()->contents().pop_back();
3700 ASSERT_TRUE(offer->description()->contents().empty());
3701 EXPECT_FALSE(DoSetLocalDescription(std::move(offer)));
3702}
3703
nisse51542be2016-02-12 02:27:06 -08003704class PeerConnectionMediaConfigTest : public testing::Test {
3705 protected:
3706 void SetUp() override {
zhihuang38ede132017-06-15 12:52:32 -07003707 pcf_ = PeerConnectionFactoryForTest::CreatePeerConnectionFactoryForTest();
nisse51542be2016-02-12 02:27:06 -08003708 pcf_->Initialize();
3709 }
nisseeaabdf62017-05-05 02:23:02 -07003710 const cricket::MediaConfig TestCreatePeerConnection(
nisse51542be2016-02-12 02:27:06 -08003711 const PeerConnectionInterface::RTCConfiguration& config,
zhihuang1c378ed2017-08-17 14:10:50 -07003712 const MediaConstraintsInterface* constraints) {
zhihuang9763d562016-08-05 11:14:50 -07003713 rtc::scoped_refptr<PeerConnectionInterface> pc(pcf_->CreatePeerConnection(
3714 config, constraints, nullptr, nullptr, &observer_));
nisse51542be2016-02-12 02:27:06 -08003715 EXPECT_TRUE(pc.get());
nisseeaabdf62017-05-05 02:23:02 -07003716 return pc->GetConfiguration().media_config;
nisse51542be2016-02-12 02:27:06 -08003717 }
3718
zhihuang9763d562016-08-05 11:14:50 -07003719 rtc::scoped_refptr<PeerConnectionFactoryForTest> pcf_;
nisse51542be2016-02-12 02:27:06 -08003720 MockPeerConnectionObserver observer_;
3721};
3722
3723// This test verifies the default behaviour with no constraints and a
3724// default RTCConfiguration.
3725TEST_F(PeerConnectionMediaConfigTest, TestDefaults) {
3726 PeerConnectionInterface::RTCConfiguration config;
3727 FakeConstraints constraints;
3728
3729 const cricket::MediaConfig& media_config =
3730 TestCreatePeerConnection(config, &constraints);
3731
3732 EXPECT_FALSE(media_config.enable_dscp);
Niels Möller1d7ecd22018-01-18 15:25:12 +01003733 EXPECT_TRUE(media_config.video.enable_cpu_adaptation);
3734 EXPECT_TRUE(media_config.video.enable_prerenderer_smoothing);
nisse0db023a2016-03-01 04:29:59 -08003735 EXPECT_FALSE(media_config.video.suspend_below_min_bitrate);
Niels Möller6539f692018-01-18 08:58:50 +01003736 EXPECT_FALSE(media_config.video.experiment_cpu_load_estimator);
nisse51542be2016-02-12 02:27:06 -08003737}
3738
3739// This test verifies the DSCP constraint is recognized and passed to
nisse528b7932017-05-08 03:21:43 -07003740// the PeerConnection.
nisse51542be2016-02-12 02:27:06 -08003741TEST_F(PeerConnectionMediaConfigTest, TestDscpConstraintTrue) {
3742 PeerConnectionInterface::RTCConfiguration config;
3743 FakeConstraints constraints;
3744
3745 constraints.AddOptional(webrtc::MediaConstraintsInterface::kEnableDscp, true);
3746 const cricket::MediaConfig& media_config =
3747 TestCreatePeerConnection(config, &constraints);
3748
3749 EXPECT_TRUE(media_config.enable_dscp);
3750}
3751
3752// This test verifies the cpu overuse detection constraint is
nisse528b7932017-05-08 03:21:43 -07003753// recognized and passed to the PeerConnection.
nisse51542be2016-02-12 02:27:06 -08003754TEST_F(PeerConnectionMediaConfigTest, TestCpuOveruseConstraintFalse) {
3755 PeerConnectionInterface::RTCConfiguration config;
3756 FakeConstraints constraints;
3757
3758 constraints.AddOptional(
3759 webrtc::MediaConstraintsInterface::kCpuOveruseDetection, false);
3760 const cricket::MediaConfig media_config =
3761 TestCreatePeerConnection(config, &constraints);
3762
Niels Möller1d7ecd22018-01-18 15:25:12 +01003763 EXPECT_FALSE(media_config.video.enable_cpu_adaptation);
nisse51542be2016-02-12 02:27:06 -08003764}
3765
Niels Möller1d7ecd22018-01-18 15:25:12 +01003766// This test verifies that the enable_prerenderer_smoothing flag is
nisse528b7932017-05-08 03:21:43 -07003767// propagated from RTCConfiguration to the PeerConnection.
nisse51542be2016-02-12 02:27:06 -08003768TEST_F(PeerConnectionMediaConfigTest, TestDisablePrerendererSmoothingTrue) {
3769 PeerConnectionInterface::RTCConfiguration config;
3770 FakeConstraints constraints;
3771
Niels Möller71bdda02016-03-31 12:59:59 +02003772 config.set_prerenderer_smoothing(false);
nisse51542be2016-02-12 02:27:06 -08003773 const cricket::MediaConfig& media_config =
3774 TestCreatePeerConnection(config, &constraints);
3775
Niels Möller1d7ecd22018-01-18 15:25:12 +01003776 EXPECT_FALSE(media_config.video.enable_prerenderer_smoothing);
nisse0db023a2016-03-01 04:29:59 -08003777}
3778
Niels Möller6539f692018-01-18 08:58:50 +01003779// This test verifies that the experiment_cpu_load_estimator flag is
3780// propagated from RTCConfiguration to the PeerConnection.
3781TEST_F(PeerConnectionMediaConfigTest, TestEnableExperimentCpuLoadEstimator) {
3782 PeerConnectionInterface::RTCConfiguration config;
3783 FakeConstraints constraints;
3784
3785 config.set_experiment_cpu_load_estimator(true);
3786 const cricket::MediaConfig& media_config =
3787 TestCreatePeerConnection(config, &constraints);
3788
3789 EXPECT_TRUE(media_config.video.experiment_cpu_load_estimator);
3790}
3791
nisse0db023a2016-03-01 04:29:59 -08003792// This test verifies the suspend below min bitrate constraint is
nisse528b7932017-05-08 03:21:43 -07003793// recognized and passed to the PeerConnection.
nisse0db023a2016-03-01 04:29:59 -08003794TEST_F(PeerConnectionMediaConfigTest,
3795 TestSuspendBelowMinBitrateConstraintTrue) {
3796 PeerConnectionInterface::RTCConfiguration config;
3797 FakeConstraints constraints;
3798
3799 constraints.AddOptional(
3800 webrtc::MediaConstraintsInterface::kEnableVideoSuspendBelowMinBitrate,
3801 true);
3802 const cricket::MediaConfig media_config =
3803 TestCreatePeerConnection(config, &constraints);
3804
3805 EXPECT_TRUE(media_config.video.suspend_below_min_bitrate);
nisse51542be2016-02-12 02:27:06 -08003806}
3807
deadbeef293e9262017-01-11 12:28:30 -08003808// Tests a few random fields being different.
3809TEST(RTCConfigurationTest, ComparisonOperators) {
3810 PeerConnectionInterface::RTCConfiguration a;
3811 PeerConnectionInterface::RTCConfiguration b;
3812 EXPECT_EQ(a, b);
3813
3814 PeerConnectionInterface::RTCConfiguration c;
3815 c.servers.push_back(PeerConnectionInterface::IceServer());
3816 EXPECT_NE(a, c);
3817
3818 PeerConnectionInterface::RTCConfiguration d;
3819 d.type = PeerConnectionInterface::kRelay;
3820 EXPECT_NE(a, d);
3821
3822 PeerConnectionInterface::RTCConfiguration e;
3823 e.audio_jitter_buffer_max_packets = 5;
3824 EXPECT_NE(a, e);
3825
3826 PeerConnectionInterface::RTCConfiguration f;
3827 f.ice_connection_receiving_timeout = 1337;
3828 EXPECT_NE(a, f);
3829
3830 PeerConnectionInterface::RTCConfiguration g;
3831 g.disable_ipv6 = true;
3832 EXPECT_NE(a, g);
3833
3834 PeerConnectionInterface::RTCConfiguration h(
3835 PeerConnectionInterface::RTCConfigurationType::kAggressive);
3836 EXPECT_NE(a, h);
3837}