blob: de62104acf25bdb16bc9f8947dda4e7c65b61fef [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
kjellanderb24317b2016-02-10 07:54:43 -08002 * Copyright 2012 The WebRTC project authors. All Rights Reserved.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003 *
kjellanderb24317b2016-02-10 07:54:43 -08004 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00009 */
10
kwibergd1fe2812016-04-27 06:47:29 -070011#include <memory>
deadbeef3edec7c2016-12-10 11:44:26 -080012#include <sstream>
henrike@webrtc.org28e20752013-07-10 00:45:36 +000013#include <string>
kwiberg0eb15ed2015-12-17 03:04:15 -080014#include <utility>
henrike@webrtc.org28e20752013-07-10 00:45:36 +000015
kwiberg087bd342017-02-10 08:15:44 -080016#include "webrtc/api/audio_codecs/builtin_audio_decoder_factory.h"
ossueb1fde42017-05-02 06:46:30 -070017#include "webrtc/api/audio_codecs/builtin_audio_encoder_factory.h"
Henrik Kjellander15583c12016-02-10 10:53:12 +010018#include "webrtc/api/jsepsessiondescription.h"
Henrik Kjellander15583c12016-02-10 10:53:12 +010019#include "webrtc/api/mediastreaminterface.h"
Henrik Kjellander15583c12016-02-10 10:53:12 +010020#include "webrtc/api/peerconnectioninterface.h"
21#include "webrtc/api/rtpreceiverinterface.h"
22#include "webrtc/api/rtpsenderinterface.h"
Henrik Kjellander15583c12016-02-10 10:53:12 +010023#include "webrtc/api/test/fakeconstraints.h"
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000024#include "webrtc/base/gunit.h"
deadbeef9a6f4d42017-05-15 19:43:33 -070025#include "webrtc/base/physicalsocketserver.h"
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000026#include "webrtc/base/ssladapter.h"
27#include "webrtc/base/sslstreamadapter.h"
28#include "webrtc/base/stringutils.h"
29#include "webrtc/base/thread.h"
deadbeef9a6f4d42017-05-15 19:43:33 -070030#include "webrtc/base/virtualsocketserver.h"
kjellandera96e2d72016-02-04 23:52:28 -080031#include "webrtc/media/base/fakevideocapturer.h"
deadbeef953c2ce2017-01-09 14:53:41 -080032#include "webrtc/media/sctp/sctptransportinternal.h"
Taylor Brandstettera1c30352016-05-13 08:15:11 -070033#include "webrtc/p2p/base/fakeportallocator.h"
ossu7bb87ee2017-01-23 04:56:25 -080034#include "webrtc/pc/audiotrack.h"
kjellander@webrtc.org9b8df252016-02-12 06:47:59 +010035#include "webrtc/pc/mediasession.h"
ossu7bb87ee2017-01-23 04:56:25 -080036#include "webrtc/pc/mediastream.h"
37#include "webrtc/pc/peerconnection.h"
38#include "webrtc/pc/streamcollection.h"
39#include "webrtc/pc/test/fakertccertificategenerator.h"
40#include "webrtc/pc/test/fakevideotracksource.h"
41#include "webrtc/pc/test/mockpeerconnectionobservers.h"
42#include "webrtc/pc/test/testsdpstrings.h"
43#include "webrtc/pc/videocapturertracksource.h"
44#include "webrtc/pc/videotrack.h"
kwibergac9f8762016-09-30 22:29:43 -070045#include "webrtc/test/gmock.h"
46
47#ifdef WEBRTC_ANDROID
ossu7bb87ee2017-01-23 04:56:25 -080048#include "webrtc/pc/test/androidtestinitializer.h"
kwibergac9f8762016-09-30 22:29:43 -070049#endif
henrike@webrtc.org28e20752013-07-10 00:45:36 +000050
51static const char kStreamLabel1[] = "local_stream_1";
52static const char kStreamLabel2[] = "local_stream_2";
53static const char kStreamLabel3[] = "local_stream_3";
54static const int kDefaultStunPort = 3478;
55static const char kStunAddressOnly[] = "stun:address";
56static const char kStunInvalidPort[] = "stun:address:-1";
57static const char kStunAddressPortAndMore1[] = "stun:address:port:more";
58static const char kStunAddressPortAndMore2[] = "stun:address:port more";
59static const char kTurnIceServerUri[] = "turn:user@turn.example.org";
60static const char kTurnUsername[] = "user";
61static const char kTurnPassword[] = "password";
62static const char kTurnHostname[] = "turn.example.org";
Peter Boström0c4e06b2015-10-07 12:23:21 +020063static const uint32_t kTimeout = 10000U;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000064
deadbeefab9b2d12015-10-14 11:33:11 -070065static const char kStreams[][8] = {"stream1", "stream2"};
66static const char kAudioTracks[][32] = {"audiotrack0", "audiotrack1"};
67static const char kVideoTracks[][32] = {"videotrack0", "videotrack1"};
68
deadbeef5e97fb52015-10-15 12:49:08 -070069static const char kRecvonly[] = "recvonly";
70static const char kSendrecv[] = "sendrecv";
71
deadbeefab9b2d12015-10-14 11:33:11 -070072// Reference SDP with a MediaStream with label "stream1" and audio track with
73// id "audio_1" and a video track with id "video_1;
74static const char kSdpStringWithStream1[] =
75 "v=0\r\n"
76 "o=- 0 0 IN IP4 127.0.0.1\r\n"
77 "s=-\r\n"
78 "t=0 0\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -080079 "m=audio 1 RTP/AVPF 103\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -070080 "a=ice-ufrag:e5785931\r\n"
81 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
82 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
83 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -070084 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -070085 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -080086 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -070087 "a=rtpmap:103 ISAC/16000\r\n"
88 "a=ssrc:1 cname:stream1\r\n"
89 "a=ssrc:1 mslabel:stream1\r\n"
90 "a=ssrc:1 label:audiotrack0\r\n"
91 "m=video 1 RTP/AVPF 120\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -080092 "a=ice-ufrag:e5785931\r\n"
93 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
94 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
95 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -070096 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -070097 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -080098 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -070099 "a=rtpmap:120 VP8/90000\r\n"
100 "a=ssrc:2 cname:stream1\r\n"
101 "a=ssrc:2 mslabel:stream1\r\n"
102 "a=ssrc:2 label:videotrack0\r\n";
103
zhihuang81c3a032016-11-17 12:06:24 -0800104// Reference SDP with a MediaStream with label "stream1" and audio track with
105// id "audio_1";
106static const char kSdpStringWithStream1AudioTrackOnly[] =
107 "v=0\r\n"
108 "o=- 0 0 IN IP4 127.0.0.1\r\n"
109 "s=-\r\n"
110 "t=0 0\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800111 "m=audio 1 RTP/AVPF 103\r\n"
zhihuang81c3a032016-11-17 12:06:24 -0800112 "a=ice-ufrag:e5785931\r\n"
113 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
114 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
115 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
zhihuang81c3a032016-11-17 12:06:24 -0800116 "a=mid:audio\r\n"
117 "a=sendrecv\r\n"
118 "a=rtpmap:103 ISAC/16000\r\n"
119 "a=ssrc:1 cname:stream1\r\n"
120 "a=ssrc:1 mslabel:stream1\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800121 "a=ssrc:1 label:audiotrack0\r\n"
122 "a=rtcp-mux\r\n";
zhihuang81c3a032016-11-17 12:06:24 -0800123
deadbeefab9b2d12015-10-14 11:33:11 -0700124// Reference SDP with two MediaStreams with label "stream1" and "stream2. Each
125// MediaStreams have one audio track and one video track.
126// This uses MSID.
127static const char kSdpStringWithStream1And2[] =
128 "v=0\r\n"
129 "o=- 0 0 IN IP4 127.0.0.1\r\n"
130 "s=-\r\n"
131 "t=0 0\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800132 "a=msid-semantic: WMS stream1 stream2\r\n"
133 "m=audio 1 RTP/AVPF 103\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700134 "a=ice-ufrag:e5785931\r\n"
135 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
136 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
137 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700138 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700139 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800140 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700141 "a=rtpmap:103 ISAC/16000\r\n"
142 "a=ssrc:1 cname:stream1\r\n"
143 "a=ssrc:1 msid:stream1 audiotrack0\r\n"
144 "a=ssrc:3 cname:stream2\r\n"
145 "a=ssrc:3 msid:stream2 audiotrack1\r\n"
146 "m=video 1 RTP/AVPF 120\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800147 "a=ice-ufrag:e5785931\r\n"
148 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
149 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
150 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700151 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700152 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800153 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700154 "a=rtpmap:120 VP8/0\r\n"
155 "a=ssrc:2 cname:stream1\r\n"
156 "a=ssrc:2 msid:stream1 videotrack0\r\n"
157 "a=ssrc:4 cname:stream2\r\n"
158 "a=ssrc:4 msid:stream2 videotrack1\r\n";
159
160// Reference SDP without MediaStreams. Msid is not supported.
161static const char kSdpStringWithoutStreams[] =
162 "v=0\r\n"
163 "o=- 0 0 IN IP4 127.0.0.1\r\n"
164 "s=-\r\n"
165 "t=0 0\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800166 "m=audio 1 RTP/AVPF 103\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700167 "a=ice-ufrag:e5785931\r\n"
168 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
169 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
170 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700171 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700172 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800173 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700174 "a=rtpmap:103 ISAC/16000\r\n"
175 "m=video 1 RTP/AVPF 120\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800176 "a=ice-ufrag:e5785931\r\n"
177 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
178 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
179 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700180 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700181 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800182 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700183 "a=rtpmap:120 VP8/90000\r\n";
184
185// Reference SDP without MediaStreams. Msid is supported.
186static const char kSdpStringWithMsidWithoutStreams[] =
187 "v=0\r\n"
188 "o=- 0 0 IN IP4 127.0.0.1\r\n"
189 "s=-\r\n"
190 "t=0 0\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800191 "a=msid-semantic: WMS\r\n"
192 "m=audio 1 RTP/AVPF 103\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700193 "a=ice-ufrag:e5785931\r\n"
194 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
195 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
196 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700197 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700198 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800199 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700200 "a=rtpmap:103 ISAC/16000\r\n"
201 "m=video 1 RTP/AVPF 120\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800202 "a=ice-ufrag:e5785931\r\n"
203 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
204 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
205 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700206 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700207 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800208 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700209 "a=rtpmap:120 VP8/90000\r\n";
210
211// Reference SDP without MediaStreams and audio only.
212static const char kSdpStringWithoutStreamsAudioOnly[] =
213 "v=0\r\n"
214 "o=- 0 0 IN IP4 127.0.0.1\r\n"
215 "s=-\r\n"
216 "t=0 0\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800217 "m=audio 1 RTP/AVPF 103\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700218 "a=ice-ufrag:e5785931\r\n"
219 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
220 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
221 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700222 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700223 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800224 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700225 "a=rtpmap:103 ISAC/16000\r\n";
226
227// Reference SENDONLY SDP without MediaStreams. Msid is not supported.
228static const char kSdpStringSendOnlyWithoutStreams[] =
229 "v=0\r\n"
230 "o=- 0 0 IN IP4 127.0.0.1\r\n"
231 "s=-\r\n"
232 "t=0 0\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800233 "m=audio 1 RTP/AVPF 103\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700234 "a=ice-ufrag:e5785931\r\n"
235 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
236 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
237 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700238 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700239 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700240 "a=sendonly\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800241 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700242 "a=rtpmap:103 ISAC/16000\r\n"
243 "m=video 1 RTP/AVPF 120\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800244 "a=ice-ufrag:e5785931\r\n"
245 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
246 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
247 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700248 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700249 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700250 "a=sendonly\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800251 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700252 "a=rtpmap:120 VP8/90000\r\n";
253
254static const char kSdpStringInit[] =
255 "v=0\r\n"
256 "o=- 0 0 IN IP4 127.0.0.1\r\n"
257 "s=-\r\n"
258 "t=0 0\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700259 "a=msid-semantic: WMS\r\n";
260
261static const char kSdpStringAudio[] =
262 "m=audio 1 RTP/AVPF 103\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800263 "a=ice-ufrag:e5785931\r\n"
264 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
265 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
266 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700267 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700268 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800269 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700270 "a=rtpmap:103 ISAC/16000\r\n";
271
272static const char kSdpStringVideo[] =
273 "m=video 1 RTP/AVPF 120\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800274 "a=ice-ufrag:e5785931\r\n"
275 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
276 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
277 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700278 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700279 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800280 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700281 "a=rtpmap:120 VP8/90000\r\n";
282
283static const char kSdpStringMs1Audio0[] =
284 "a=ssrc:1 cname:stream1\r\n"
285 "a=ssrc:1 msid:stream1 audiotrack0\r\n";
286
287static const char kSdpStringMs1Video0[] =
288 "a=ssrc:2 cname:stream1\r\n"
289 "a=ssrc:2 msid:stream1 videotrack0\r\n";
290
291static const char kSdpStringMs1Audio1[] =
292 "a=ssrc:3 cname:stream1\r\n"
293 "a=ssrc:3 msid:stream1 audiotrack1\r\n";
294
295static const char kSdpStringMs1Video1[] =
296 "a=ssrc:4 cname:stream1\r\n"
297 "a=ssrc:4 msid:stream1 videotrack1\r\n";
298
deadbeef8662f942017-01-20 21:20:51 -0800299static const char kDtlsSdesFallbackSdp[] =
300 "v=0\r\n"
301 "o=xxxxxx 7 2 IN IP4 0.0.0.0\r\n"
302 "s=-\r\n"
303 "c=IN IP4 0.0.0.0\r\n"
304 "t=0 0\r\n"
305 "a=group:BUNDLE audio\r\n"
306 "a=msid-semantic: WMS\r\n"
307 "m=audio 1 RTP/SAVPF 0\r\n"
308 "a=sendrecv\r\n"
309 "a=rtcp-mux\r\n"
310 "a=mid:audio\r\n"
311 "a=ssrc:1 cname:stream1\r\n"
312 "a=ssrc:1 mslabel:stream1\r\n"
313 "a=ssrc:1 label:audiotrack0\r\n"
314 "a=ice-ufrag:e5785931\r\n"
315 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
316 "a=rtpmap:0 pcmu/8000\r\n"
317 "a=fingerprint:sha-1 "
318 "4A:AD:B9:B1:3F:82:18:3B:54:02:12:DF:3E:5D:49:6B:19:E5:7C:AB\r\n"
319 "a=setup:actpass\r\n"
320 "a=crypto:1 AES_CM_128_HMAC_SHA1_32 "
321 "inline:NzB4d1BINUAvLEw6UzF3WSJ+PSdFcGdUJShpX1Zj|2^20|1:32 "
322 "dummy_session_params\r\n";
323
perkjd61bf802016-03-24 03:16:19 -0700324using ::testing::Exactly;
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700325using cricket::StreamParams;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000326using webrtc::AudioSourceInterface;
deadbeefab9b2d12015-10-14 11:33:11 -0700327using webrtc::AudioTrack;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000328using webrtc::AudioTrackInterface;
329using webrtc::DataBuffer;
330using webrtc::DataChannelInterface;
331using webrtc::FakeConstraints;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000332using webrtc::IceCandidateInterface;
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700333using webrtc::JsepSessionDescription;
deadbeefc80741f2015-10-22 13:14:45 -0700334using webrtc::MediaConstraintsInterface;
deadbeefab9b2d12015-10-14 11:33:11 -0700335using webrtc::MediaStream;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000336using webrtc::MediaStreamInterface;
337using webrtc::MediaStreamTrackInterface;
338using webrtc::MockCreateSessionDescriptionObserver;
339using webrtc::MockDataChannelObserver;
340using webrtc::MockSetSessionDescriptionObserver;
341using webrtc::MockStatsObserver;
perkjd61bf802016-03-24 03:16:19 -0700342using webrtc::NotifierInterface;
343using webrtc::ObserverInterface;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000344using webrtc::PeerConnectionInterface;
345using webrtc::PeerConnectionObserver;
deadbeef293e9262017-01-11 12:28:30 -0800346using webrtc::RTCError;
347using webrtc::RTCErrorType;
deadbeefab9b2d12015-10-14 11:33:11 -0700348using webrtc::RtpReceiverInterface;
349using webrtc::RtpSenderInterface;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000350using webrtc::SdpParseError;
351using webrtc::SessionDescriptionInterface;
deadbeefab9b2d12015-10-14 11:33:11 -0700352using webrtc::StreamCollection;
353using webrtc::StreamCollectionInterface;
perkja3ede6c2016-03-08 01:27:48 +0100354using webrtc::VideoTrackSourceInterface;
deadbeefab9b2d12015-10-14 11:33:11 -0700355using webrtc::VideoTrack;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000356using webrtc::VideoTrackInterface;
357
deadbeefab9b2d12015-10-14 11:33:11 -0700358typedef PeerConnectionInterface::RTCOfferAnswerOptions RTCOfferAnswerOptions;
359
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000360namespace {
361
362// Gets the first ssrc of given content type from the ContentInfo.
363bool GetFirstSsrc(const cricket::ContentInfo* content_info, int* ssrc) {
364 if (!content_info || !ssrc) {
365 return false;
366 }
367 const cricket::MediaContentDescription* media_desc =
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000368 static_cast<const cricket::MediaContentDescription*>(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000369 content_info->description);
370 if (!media_desc || media_desc->streams().empty()) {
371 return false;
372 }
373 *ssrc = media_desc->streams().begin()->first_ssrc();
374 return true;
375}
376
deadbeefd1a38b52016-12-10 13:15:33 -0800377// Get the ufrags out of an SDP blob. Useful for testing ICE restart
378// behavior.
379std::vector<std::string> GetUfrags(
380 const webrtc::SessionDescriptionInterface* desc) {
381 std::vector<std::string> ufrags;
382 for (const cricket::TransportInfo& info :
383 desc->description()->transport_infos()) {
384 ufrags.push_back(info.description.ice_ufrag);
385 }
386 return ufrags;
387}
388
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000389void SetSsrcToZero(std::string* sdp) {
390 const char kSdpSsrcAtribute[] = "a=ssrc:";
391 const char kSdpSsrcAtributeZero[] = "a=ssrc:0";
392 size_t ssrc_pos = 0;
393 while ((ssrc_pos = sdp->find(kSdpSsrcAtribute, ssrc_pos)) !=
394 std::string::npos) {
395 size_t end_ssrc = sdp->find(" ", ssrc_pos);
396 sdp->replace(ssrc_pos, end_ssrc - ssrc_pos, kSdpSsrcAtributeZero);
397 ssrc_pos = end_ssrc;
398 }
399}
400
deadbeefab9b2d12015-10-14 11:33:11 -0700401// Check if |streams| contains the specified track.
402bool ContainsTrack(const std::vector<cricket::StreamParams>& streams,
403 const std::string& stream_label,
404 const std::string& track_id) {
405 for (const cricket::StreamParams& params : streams) {
406 if (params.sync_label == stream_label && params.id == track_id) {
407 return true;
408 }
409 }
410 return false;
411}
412
413// Check if |senders| contains the specified sender, by id.
414bool ContainsSender(
415 const std::vector<rtc::scoped_refptr<RtpSenderInterface>>& senders,
416 const std::string& id) {
417 for (const auto& sender : senders) {
418 if (sender->id() == id) {
419 return true;
420 }
421 }
422 return false;
423}
424
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700425// Check if |senders| contains the specified sender, by id and stream id.
426bool ContainsSender(
427 const std::vector<rtc::scoped_refptr<RtpSenderInterface>>& senders,
428 const std::string& id,
429 const std::string& stream_id) {
430 for (const auto& sender : senders) {
deadbeefa601f5c2016-06-06 14:27:39 -0700431 if (sender->id() == id && sender->stream_ids()[0] == stream_id) {
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700432 return true;
433 }
434 }
435 return false;
436}
437
deadbeefab9b2d12015-10-14 11:33:11 -0700438// Create a collection of streams.
439// CreateStreamCollection(1) creates a collection that
440// correspond to kSdpStringWithStream1.
441// CreateStreamCollection(2) correspond to kSdpStringWithStream1And2.
442rtc::scoped_refptr<StreamCollection> CreateStreamCollection(
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700443 int number_of_streams,
444 int tracks_per_stream) {
deadbeefab9b2d12015-10-14 11:33:11 -0700445 rtc::scoped_refptr<StreamCollection> local_collection(
446 StreamCollection::Create());
447
448 for (int i = 0; i < number_of_streams; ++i) {
449 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream(
450 webrtc::MediaStream::Create(kStreams[i]));
451
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700452 for (int j = 0; j < tracks_per_stream; ++j) {
453 // Add a local audio track.
454 rtc::scoped_refptr<webrtc::AudioTrackInterface> audio_track(
455 webrtc::AudioTrack::Create(kAudioTracks[i * tracks_per_stream + j],
456 nullptr));
457 stream->AddTrack(audio_track);
deadbeefab9b2d12015-10-14 11:33:11 -0700458
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700459 // Add a local video track.
460 rtc::scoped_refptr<webrtc::VideoTrackInterface> video_track(
461 webrtc::VideoTrack::Create(kVideoTracks[i * tracks_per_stream + j],
462 webrtc::FakeVideoTrackSource::Create()));
463 stream->AddTrack(video_track);
464 }
deadbeefab9b2d12015-10-14 11:33:11 -0700465
466 local_collection->AddStream(stream);
467 }
468 return local_collection;
469}
470
471// Check equality of StreamCollections.
472bool CompareStreamCollections(StreamCollectionInterface* s1,
473 StreamCollectionInterface* s2) {
474 if (s1 == nullptr || s2 == nullptr || s1->count() != s2->count()) {
475 return false;
476 }
477
478 for (size_t i = 0; i != s1->count(); ++i) {
479 if (s1->at(i)->label() != s2->at(i)->label()) {
480 return false;
481 }
482 webrtc::AudioTrackVector audio_tracks1 = s1->at(i)->GetAudioTracks();
483 webrtc::AudioTrackVector audio_tracks2 = s2->at(i)->GetAudioTracks();
484 webrtc::VideoTrackVector video_tracks1 = s1->at(i)->GetVideoTracks();
485 webrtc::VideoTrackVector video_tracks2 = s2->at(i)->GetVideoTracks();
486
487 if (audio_tracks1.size() != audio_tracks2.size()) {
488 return false;
489 }
490 for (size_t j = 0; j != audio_tracks1.size(); ++j) {
491 if (audio_tracks1[j]->id() != audio_tracks2[j]->id()) {
492 return false;
493 }
494 }
495 if (video_tracks1.size() != video_tracks2.size()) {
496 return false;
497 }
498 for (size_t j = 0; j != video_tracks1.size(); ++j) {
499 if (video_tracks1[j]->id() != video_tracks2[j]->id()) {
500 return false;
501 }
502 }
503 }
504 return true;
505}
506
perkjd61bf802016-03-24 03:16:19 -0700507// Helper class to test Observer.
508class MockTrackObserver : public ObserverInterface {
509 public:
510 explicit MockTrackObserver(NotifierInterface* notifier)
511 : notifier_(notifier) {
512 notifier_->RegisterObserver(this);
513 }
514
515 ~MockTrackObserver() { Unregister(); }
516
517 void Unregister() {
518 if (notifier_) {
519 notifier_->UnregisterObserver(this);
520 notifier_ = nullptr;
521 }
522 }
523
524 MOCK_METHOD0(OnChanged, void());
525
526 private:
527 NotifierInterface* notifier_;
528};
529
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000530class MockPeerConnectionObserver : public PeerConnectionObserver {
531 public:
deadbeefab9b2d12015-10-14 11:33:11 -0700532 MockPeerConnectionObserver() : remote_streams_(StreamCollection::Create()) {}
Henrik Kjellander3fe372d2016-05-12 08:10:52 +0200533 virtual ~MockPeerConnectionObserver() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000534 }
535 void SetPeerConnectionInterface(PeerConnectionInterface* pc) {
536 pc_ = pc;
537 if (pc) {
538 state_ = pc_->signaling_state();
539 }
540 }
nisseef8b61e2016-04-29 06:09:15 -0700541 void OnSignalingChange(
542 PeerConnectionInterface::SignalingState new_state) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000543 EXPECT_EQ(pc_->signaling_state(), new_state);
544 state_ = new_state;
545 }
deadbeefab9b2d12015-10-14 11:33:11 -0700546
547 MediaStreamInterface* RemoteStream(const std::string& label) {
548 return remote_streams_->find(label);
549 }
550 StreamCollectionInterface* remote_streams() const { return remote_streams_; }
Taylor Brandstetter98cde262016-05-31 13:02:21 -0700551 void OnAddStream(rtc::scoped_refptr<MediaStreamInterface> stream) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000552 last_added_stream_ = stream;
deadbeefab9b2d12015-10-14 11:33:11 -0700553 remote_streams_->AddStream(stream);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000554 }
Taylor Brandstetter98cde262016-05-31 13:02:21 -0700555 void OnRemoveStream(
556 rtc::scoped_refptr<MediaStreamInterface> stream) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000557 last_removed_stream_ = stream;
deadbeefab9b2d12015-10-14 11:33:11 -0700558 remote_streams_->RemoveStream(stream);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000559 }
perkjdfb769d2016-02-09 03:09:43 -0800560 void OnRenegotiationNeeded() override { renegotiation_needed_ = true; }
Taylor Brandstetter98cde262016-05-31 13:02:21 -0700561 void OnDataChannel(
562 rtc::scoped_refptr<DataChannelInterface> data_channel) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000563 last_datachannel_ = data_channel;
564 }
565
perkjdfb769d2016-02-09 03:09:43 -0800566 void OnIceConnectionChange(
567 PeerConnectionInterface::IceConnectionState new_state) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000568 EXPECT_EQ(pc_->ice_connection_state(), new_state);
zhihuang81c3a032016-11-17 12:06:24 -0800569 callback_triggered_ = true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000570 }
perkjdfb769d2016-02-09 03:09:43 -0800571 void OnIceGatheringChange(
572 PeerConnectionInterface::IceGatheringState new_state) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000573 EXPECT_EQ(pc_->ice_gathering_state(), new_state);
perkjdfb769d2016-02-09 03:09:43 -0800574 ice_complete_ = new_state == PeerConnectionInterface::kIceGatheringComplete;
zhihuang81c3a032016-11-17 12:06:24 -0800575 callback_triggered_ = true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000576 }
perkjdfb769d2016-02-09 03:09:43 -0800577 void OnIceCandidate(const webrtc::IceCandidateInterface* candidate) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000578 EXPECT_NE(PeerConnectionInterface::kIceGatheringNew,
579 pc_->ice_gathering_state());
580
581 std::string sdp;
582 EXPECT_TRUE(candidate->ToString(&sdp));
583 EXPECT_LT(0u, sdp.size());
584 last_candidate_.reset(webrtc::CreateIceCandidate(candidate->sdp_mid(),
585 candidate->sdp_mline_index(), sdp, NULL));
586 EXPECT_TRUE(last_candidate_.get() != NULL);
zhihuang81c3a032016-11-17 12:06:24 -0800587 callback_triggered_ = true;
zhihuang29ff8442016-07-27 11:07:25 -0700588 }
589
590 void OnIceCandidatesRemoved(
591 const std::vector<cricket::Candidate>& candidates) override {
zhihuang81c3a032016-11-17 12:06:24 -0800592 callback_triggered_ = true;
zhihuang29ff8442016-07-27 11:07:25 -0700593 }
594
595 void OnIceConnectionReceivingChange(bool receiving) override {
zhihuang81c3a032016-11-17 12:06:24 -0800596 callback_triggered_ = true;
597 }
598
zhihuangc63b8942016-12-02 15:41:10 -0800599 void OnAddTrack(
600 rtc::scoped_refptr<webrtc::RtpReceiverInterface> receiver,
601 const std::vector<rtc::scoped_refptr<webrtc::MediaStreamInterface>>&
602 streams) override {
zhihuang81c3a032016-11-17 12:06:24 -0800603 EXPECT_TRUE(receiver != nullptr);
604 num_added_tracks_++;
605 last_added_track_label_ = receiver->id();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000606 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000607
608 // Returns the label of the last added stream.
609 // Empty string if no stream have been added.
610 std::string GetLastAddedStreamLabel() {
611 if (last_added_stream_.get())
612 return last_added_stream_->label();
613 return "";
614 }
615 std::string GetLastRemovedStreamLabel() {
616 if (last_removed_stream_.get())
617 return last_removed_stream_->label();
618 return "";
619 }
620
zhihuang9763d562016-08-05 11:14:50 -0700621 rtc::scoped_refptr<PeerConnectionInterface> pc_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000622 PeerConnectionInterface::SignalingState state_;
kwibergd1fe2812016-04-27 06:47:29 -0700623 std::unique_ptr<IceCandidateInterface> last_candidate_;
zhihuang9763d562016-08-05 11:14:50 -0700624 rtc::scoped_refptr<DataChannelInterface> last_datachannel_;
deadbeefab9b2d12015-10-14 11:33:11 -0700625 rtc::scoped_refptr<StreamCollection> remote_streams_;
626 bool renegotiation_needed_ = false;
627 bool ice_complete_ = false;
zhihuang81c3a032016-11-17 12:06:24 -0800628 bool callback_triggered_ = false;
629 int num_added_tracks_ = 0;
630 std::string last_added_track_label_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000631
632 private:
zhihuang9763d562016-08-05 11:14:50 -0700633 rtc::scoped_refptr<MediaStreamInterface> last_added_stream_;
634 rtc::scoped_refptr<MediaStreamInterface> last_removed_stream_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000635};
636
637} // namespace
deadbeefab9b2d12015-10-14 11:33:11 -0700638
nisse528b7932017-05-08 03:21:43 -0700639// The PeerConnectionMediaConfig tests below verify that configuration and
640// constraints are propagated into the PeerConnection's MediaConfig. These
641// settings are intended for MediaChannel constructors, but that is not
642// exercised by these unittest.
zhihuang29ff8442016-07-27 11:07:25 -0700643class PeerConnectionFactoryForTest : public webrtc::PeerConnectionFactory {
644 public:
kwiberg1e4e8cb2017-01-31 01:48:08 -0800645 PeerConnectionFactoryForTest()
646 : webrtc::PeerConnectionFactory(
647 webrtc::CreateBuiltinAudioEncoderFactory(),
648 webrtc::CreateBuiltinAudioDecoderFactory()) {}
649
zhihuang29ff8442016-07-27 11:07:25 -0700650 cricket::TransportController* CreateTransportController(
Honghai Zhangbfd398c2016-08-30 22:07:42 -0700651 cricket::PortAllocator* port_allocator,
652 bool redetermine_role_on_ice_restart) override {
zhihuang29ff8442016-07-27 11:07:25 -0700653 transport_controller = new cricket::TransportController(
Honghai Zhangbfd398c2016-08-30 22:07:42 -0700654 rtc::Thread::Current(), rtc::Thread::Current(), port_allocator,
deadbeef7914b8c2017-04-21 03:23:33 -0700655 redetermine_role_on_ice_restart, rtc::CryptoOptions());
zhihuang29ff8442016-07-27 11:07:25 -0700656 return transport_controller;
657 }
658
659 cricket::TransportController* transport_controller;
zhihuang29ff8442016-07-27 11:07:25 -0700660};
661
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000662class PeerConnectionInterfaceTest : public testing::Test {
663 protected:
deadbeef9a6f4d42017-05-15 19:43:33 -0700664 PeerConnectionInterfaceTest()
665 : pss_(new rtc::PhysicalSocketServer),
666 vss_(new rtc::VirtualSocketServer(pss_.get())),
667 main_(vss_.get()) {
phoglund37ebcf02016-01-08 05:04:57 -0800668#ifdef WEBRTC_ANDROID
669 webrtc::InitializeAndroidObjects();
670#endif
671 }
672
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000673 virtual void SetUp() {
674 pc_factory_ = webrtc::CreatePeerConnectionFactory(
danilchape9021a32016-05-17 01:52:02 -0700675 rtc::Thread::Current(), rtc::Thread::Current(), rtc::Thread::Current(),
676 nullptr, nullptr, nullptr);
677 ASSERT_TRUE(pc_factory_);
zhihuang29ff8442016-07-27 11:07:25 -0700678 pc_factory_for_test_ =
679 new rtc::RefCountedObject<PeerConnectionFactoryForTest>();
680 pc_factory_for_test_->Initialize();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000681 }
682
683 void CreatePeerConnection() {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700684 CreatePeerConnection(PeerConnectionInterface::RTCConfiguration(), nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000685 }
686
deadbeef293e9262017-01-11 12:28:30 -0800687 // DTLS does not work in a loopback call, so is disabled for most of the
688 // tests in this file.
689 void CreatePeerConnectionWithoutDtls() {
690 FakeConstraints no_dtls_constraints;
691 no_dtls_constraints.AddMandatory(
692 webrtc::MediaConstraintsInterface::kEnableDtlsSrtp, false);
693
694 CreatePeerConnection(PeerConnectionInterface::RTCConfiguration(),
695 &no_dtls_constraints);
696 }
697
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000698 void CreatePeerConnection(webrtc::MediaConstraintsInterface* constraints) {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700699 CreatePeerConnection(PeerConnectionInterface::RTCConfiguration(),
700 constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000701 }
702
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700703 void CreatePeerConnectionWithIceTransportsType(
704 PeerConnectionInterface::IceTransportsType type) {
705 PeerConnectionInterface::RTCConfiguration config;
706 config.type = type;
707 return CreatePeerConnection(config, nullptr);
708 }
709
710 void CreatePeerConnectionWithIceServer(const std::string& uri,
711 const std::string& password) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800712 PeerConnectionInterface::RTCConfiguration config;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000713 PeerConnectionInterface::IceServer server;
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700714 server.uri = uri;
715 server.password = password;
716 config.servers.push_back(server);
717 CreatePeerConnection(config, nullptr);
718 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000719
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700720 void CreatePeerConnection(PeerConnectionInterface::RTCConfiguration config,
721 webrtc::MediaConstraintsInterface* constraints) {
kwibergd1fe2812016-04-27 06:47:29 -0700722 std::unique_ptr<cricket::FakePortAllocator> port_allocator(
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800723 new cricket::FakePortAllocator(rtc::Thread::Current(), nullptr));
724 port_allocator_ = port_allocator.get();
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000725
deadbeef1dcb1642017-03-29 21:08:16 -0700726 // Create certificate generator unless DTLS constraint is explicitly set to
727 // false.
Henrik Boströmd79599d2016-06-01 13:58:50 +0200728 std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator;
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000729 bool dtls;
730 if (FindConstraint(constraints,
731 webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
732 &dtls,
Henrik Boström5e56c592015-08-11 10:33:13 +0200733 nullptr) && dtls) {
deadbeef8662f942017-01-20 21:20:51 -0800734 fake_certificate_generator_ = new FakeRTCCertificateGenerator();
735 cert_generator.reset(fake_certificate_generator_);
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000736 }
Henrik Boströmd79599d2016-06-01 13:58:50 +0200737 pc_ = pc_factory_->CreatePeerConnection(
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800738 config, constraints, std::move(port_allocator),
Henrik Boströmd79599d2016-06-01 13:58:50 +0200739 std::move(cert_generator), &observer_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000740 ASSERT_TRUE(pc_.get() != NULL);
741 observer_.SetPeerConnectionInterface(pc_.get());
742 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
743 }
744
deadbeef0a6c4ca2015-10-06 11:38:28 -0700745 void CreatePeerConnectionExpectFail(const std::string& uri) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800746 PeerConnectionInterface::RTCConfiguration config;
deadbeef0a6c4ca2015-10-06 11:38:28 -0700747 PeerConnectionInterface::IceServer server;
deadbeef0a6c4ca2015-10-06 11:38:28 -0700748 server.uri = uri;
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800749 config.servers.push_back(server);
deadbeef0a6c4ca2015-10-06 11:38:28 -0700750
zhihuang9763d562016-08-05 11:14:50 -0700751 rtc::scoped_refptr<PeerConnectionInterface> pc;
hbosd7973cc2016-05-27 06:08:53 -0700752 pc = pc_factory_->CreatePeerConnection(config, nullptr, nullptr, nullptr,
753 &observer_);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800754 EXPECT_EQ(nullptr, pc);
deadbeef0a6c4ca2015-10-06 11:38:28 -0700755 }
756
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000757 void CreatePeerConnectionWithDifferentConfigurations() {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700758 CreatePeerConnectionWithIceServer(kStunAddressOnly, "");
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800759 EXPECT_EQ(1u, port_allocator_->stun_servers().size());
760 EXPECT_EQ(0u, port_allocator_->turn_servers().size());
761 EXPECT_EQ("address", port_allocator_->stun_servers().begin()->hostname());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000762 EXPECT_EQ(kDefaultStunPort,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800763 port_allocator_->stun_servers().begin()->port());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000764
deadbeef0a6c4ca2015-10-06 11:38:28 -0700765 CreatePeerConnectionExpectFail(kStunInvalidPort);
766 CreatePeerConnectionExpectFail(kStunAddressPortAndMore1);
767 CreatePeerConnectionExpectFail(kStunAddressPortAndMore2);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000768
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700769 CreatePeerConnectionWithIceServer(kTurnIceServerUri, kTurnPassword);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800770 EXPECT_EQ(0u, port_allocator_->stun_servers().size());
771 EXPECT_EQ(1u, port_allocator_->turn_servers().size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000772 EXPECT_EQ(kTurnUsername,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800773 port_allocator_->turn_servers()[0].credentials.username);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000774 EXPECT_EQ(kTurnPassword,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800775 port_allocator_->turn_servers()[0].credentials.password);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000776 EXPECT_EQ(kTurnHostname,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800777 port_allocator_->turn_servers()[0].ports[0].address.hostname());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000778 }
779
780 void ReleasePeerConnection() {
781 pc_ = NULL;
782 observer_.SetPeerConnectionInterface(NULL);
783 }
784
deadbeefab9b2d12015-10-14 11:33:11 -0700785 void AddVideoStream(const std::string& label) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000786 // Create a local stream.
zhihuang9763d562016-08-05 11:14:50 -0700787 rtc::scoped_refptr<MediaStreamInterface> stream(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000788 pc_factory_->CreateLocalMediaStream(label));
zhihuang9763d562016-08-05 11:14:50 -0700789 rtc::scoped_refptr<VideoTrackSourceInterface> video_source(
deadbeef112b2e92017-02-10 20:13:37 -0800790 pc_factory_->CreateVideoSource(std::unique_ptr<cricket::VideoCapturer>(
791 new cricket::FakeVideoCapturer()),
792 NULL));
zhihuang9763d562016-08-05 11:14:50 -0700793 rtc::scoped_refptr<VideoTrackInterface> video_track(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000794 pc_factory_->CreateVideoTrack(label + "v0", video_source));
795 stream->AddTrack(video_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +0000796 EXPECT_TRUE(pc_->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000797 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
798 observer_.renegotiation_needed_ = false;
799 }
800
801 void AddVoiceStream(const std::string& label) {
802 // Create a local stream.
zhihuang9763d562016-08-05 11:14:50 -0700803 rtc::scoped_refptr<MediaStreamInterface> stream(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000804 pc_factory_->CreateLocalMediaStream(label));
zhihuang9763d562016-08-05 11:14:50 -0700805 rtc::scoped_refptr<AudioTrackInterface> audio_track(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000806 pc_factory_->CreateAudioTrack(label + "a0", NULL));
807 stream->AddTrack(audio_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +0000808 EXPECT_TRUE(pc_->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000809 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
810 observer_.renegotiation_needed_ = false;
811 }
812
813 void AddAudioVideoStream(const std::string& stream_label,
814 const std::string& audio_track_label,
815 const std::string& video_track_label) {
816 // Create a local stream.
zhihuang9763d562016-08-05 11:14:50 -0700817 rtc::scoped_refptr<MediaStreamInterface> stream(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000818 pc_factory_->CreateLocalMediaStream(stream_label));
zhihuang9763d562016-08-05 11:14:50 -0700819 rtc::scoped_refptr<AudioTrackInterface> audio_track(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000820 pc_factory_->CreateAudioTrack(
821 audio_track_label, static_cast<AudioSourceInterface*>(NULL)));
822 stream->AddTrack(audio_track.get());
zhihuang9763d562016-08-05 11:14:50 -0700823 rtc::scoped_refptr<VideoTrackInterface> video_track(
nisseaf510af2016-03-21 08:20:42 -0700824 pc_factory_->CreateVideoTrack(
deadbeef112b2e92017-02-10 20:13:37 -0800825 video_track_label, pc_factory_->CreateVideoSource(
826 std::unique_ptr<cricket::VideoCapturer>(
827 new cricket::FakeVideoCapturer()))));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000828 stream->AddTrack(video_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +0000829 EXPECT_TRUE(pc_->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000830 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
831 observer_.renegotiation_needed_ = false;
832 }
833
kwibergd1fe2812016-04-27 06:47:29 -0700834 bool DoCreateOfferAnswer(std::unique_ptr<SessionDescriptionInterface>* desc,
deadbeefc80741f2015-10-22 13:14:45 -0700835 bool offer,
836 MediaConstraintsInterface* constraints) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000837 rtc::scoped_refptr<MockCreateSessionDescriptionObserver>
838 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000839 MockCreateSessionDescriptionObserver>());
840 if (offer) {
deadbeefc80741f2015-10-22 13:14:45 -0700841 pc_->CreateOffer(observer, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000842 } else {
deadbeefc80741f2015-10-22 13:14:45 -0700843 pc_->CreateAnswer(observer, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000844 }
845 EXPECT_EQ_WAIT(true, observer->called(), kTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -0700846 *desc = observer->MoveDescription();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000847 return observer->result();
848 }
849
kwibergd1fe2812016-04-27 06:47:29 -0700850 bool DoCreateOffer(std::unique_ptr<SessionDescriptionInterface>* desc,
deadbeefc80741f2015-10-22 13:14:45 -0700851 MediaConstraintsInterface* constraints) {
852 return DoCreateOfferAnswer(desc, true, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000853 }
854
kwibergd1fe2812016-04-27 06:47:29 -0700855 bool DoCreateAnswer(std::unique_ptr<SessionDescriptionInterface>* desc,
deadbeefc80741f2015-10-22 13:14:45 -0700856 MediaConstraintsInterface* constraints) {
857 return DoCreateOfferAnswer(desc, false, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000858 }
859
860 bool DoSetSessionDescription(SessionDescriptionInterface* desc, bool local) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000861 rtc::scoped_refptr<MockSetSessionDescriptionObserver>
862 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000863 MockSetSessionDescriptionObserver>());
864 if (local) {
865 pc_->SetLocalDescription(observer, desc);
866 } else {
867 pc_->SetRemoteDescription(observer, desc);
868 }
zhihuang29ff8442016-07-27 11:07:25 -0700869 if (pc_->signaling_state() != PeerConnectionInterface::kClosed) {
870 EXPECT_EQ_WAIT(true, observer->called(), kTimeout);
871 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000872 return observer->result();
873 }
874
875 bool DoSetLocalDescription(SessionDescriptionInterface* desc) {
876 return DoSetSessionDescription(desc, true);
877 }
878
879 bool DoSetRemoteDescription(SessionDescriptionInterface* desc) {
880 return DoSetSessionDescription(desc, false);
881 }
882
883 // Calls PeerConnection::GetStats and check the return value.
884 // It does not verify the values in the StatReports since a RTCP packet might
885 // be required.
886 bool DoGetStats(MediaStreamTrackInterface* track) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000887 rtc::scoped_refptr<MockStatsObserver> observer(
888 new rtc::RefCountedObject<MockStatsObserver>());
jiayl@webrtc.orgdb41b4d2014-03-03 21:30:06 +0000889 if (!pc_->GetStats(
890 observer, track, PeerConnectionInterface::kStatsOutputLevelStandard))
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000891 return false;
892 EXPECT_TRUE_WAIT(observer->called(), kTimeout);
893 return observer->called();
894 }
895
896 void InitiateCall() {
deadbeef293e9262017-01-11 12:28:30 -0800897 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000898 // Create a local stream with audio&video tracks.
899 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
900 CreateOfferReceiveAnswer();
901 }
902
903 // Verify that RTP Header extensions has been negotiated for audio and video.
904 void VerifyRemoteRtpHeaderExtensions() {
905 const cricket::MediaContentDescription* desc =
906 cricket::GetFirstAudioContentDescription(
907 pc_->remote_description()->description());
908 ASSERT_TRUE(desc != NULL);
909 EXPECT_GT(desc->rtp_header_extensions().size(), 0u);
910
911 desc = cricket::GetFirstVideoContentDescription(
912 pc_->remote_description()->description());
913 ASSERT_TRUE(desc != NULL);
914 EXPECT_GT(desc->rtp_header_extensions().size(), 0u);
915 }
916
917 void CreateOfferAsRemoteDescription() {
kwibergd1fe2812016-04-27 06:47:29 -0700918 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -0700919 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000920 std::string sdp;
921 EXPECT_TRUE(offer->ToString(&sdp));
922 SessionDescriptionInterface* remote_offer =
923 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
924 sdp, NULL);
925 EXPECT_TRUE(DoSetRemoteDescription(remote_offer));
926 EXPECT_EQ(PeerConnectionInterface::kHaveRemoteOffer, observer_.state_);
927 }
928
deadbeefab9b2d12015-10-14 11:33:11 -0700929 void CreateAndSetRemoteOffer(const std::string& sdp) {
930 SessionDescriptionInterface* remote_offer =
931 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
932 sdp, nullptr);
933 EXPECT_TRUE(DoSetRemoteDescription(remote_offer));
934 EXPECT_EQ(PeerConnectionInterface::kHaveRemoteOffer, observer_.state_);
935 }
936
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000937 void CreateAnswerAsLocalDescription() {
kwibergd1fe2812016-04-27 06:47:29 -0700938 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -0700939 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000940
941 // TODO(perkj): Currently SetLocalDescription fails if any parameters in an
942 // audio codec change, even if the parameter has nothing to do with
943 // receiving. Not all parameters are serialized to SDP.
944 // Since CreatePrAnswerAsLocalDescription serialize/deserialize
945 // the SessionDescription, it is necessary to do that here to in order to
946 // get ReceiveOfferCreatePrAnswerAndAnswer and RenegotiateAudioOnly to pass.
947 // https://code.google.com/p/webrtc/issues/detail?id=1356
948 std::string sdp;
949 EXPECT_TRUE(answer->ToString(&sdp));
950 SessionDescriptionInterface* new_answer =
951 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
952 sdp, NULL);
953 EXPECT_TRUE(DoSetLocalDescription(new_answer));
954 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
955 }
956
957 void CreatePrAnswerAsLocalDescription() {
kwibergd1fe2812016-04-27 06:47:29 -0700958 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -0700959 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000960
961 std::string sdp;
962 EXPECT_TRUE(answer->ToString(&sdp));
963 SessionDescriptionInterface* pr_answer =
964 webrtc::CreateSessionDescription(SessionDescriptionInterface::kPrAnswer,
965 sdp, NULL);
966 EXPECT_TRUE(DoSetLocalDescription(pr_answer));
967 EXPECT_EQ(PeerConnectionInterface::kHaveLocalPrAnswer, observer_.state_);
968 }
969
970 void CreateOfferReceiveAnswer() {
971 CreateOfferAsLocalDescription();
972 std::string sdp;
973 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
974 CreateAnswerAsRemoteDescription(sdp);
975 }
976
977 void CreateOfferAsLocalDescription() {
kwibergd1fe2812016-04-27 06:47:29 -0700978 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -0700979 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000980 // TODO(perkj): Currently SetLocalDescription fails if any parameters in an
981 // audio codec change, even if the parameter has nothing to do with
982 // receiving. Not all parameters are serialized to SDP.
983 // Since CreatePrAnswerAsLocalDescription serialize/deserialize
984 // the SessionDescription, it is necessary to do that here to in order to
985 // get ReceiveOfferCreatePrAnswerAndAnswer and RenegotiateAudioOnly to pass.
986 // https://code.google.com/p/webrtc/issues/detail?id=1356
987 std::string sdp;
988 EXPECT_TRUE(offer->ToString(&sdp));
989 SessionDescriptionInterface* new_offer =
990 webrtc::CreateSessionDescription(
991 SessionDescriptionInterface::kOffer,
992 sdp, NULL);
993
994 EXPECT_TRUE(DoSetLocalDescription(new_offer));
995 EXPECT_EQ(PeerConnectionInterface::kHaveLocalOffer, observer_.state_);
mallinath@webrtc.org68cbd012014-01-22 00:16:46 +0000996 // Wait for the ice_complete message, so that SDP will have candidates.
997 EXPECT_TRUE_WAIT(observer_.ice_complete_, kTimeout);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000998 }
999
deadbeefab9b2d12015-10-14 11:33:11 -07001000 void CreateAnswerAsRemoteDescription(const std::string& sdp) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001001 webrtc::JsepSessionDescription* answer = new webrtc::JsepSessionDescription(
1002 SessionDescriptionInterface::kAnswer);
deadbeefab9b2d12015-10-14 11:33:11 -07001003 EXPECT_TRUE(answer->Initialize(sdp, NULL));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001004 EXPECT_TRUE(DoSetRemoteDescription(answer));
1005 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
1006 }
1007
deadbeefab9b2d12015-10-14 11:33:11 -07001008 void CreatePrAnswerAndAnswerAsRemoteDescription(const std::string& sdp) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001009 webrtc::JsepSessionDescription* pr_answer =
1010 new webrtc::JsepSessionDescription(
1011 SessionDescriptionInterface::kPrAnswer);
deadbeefab9b2d12015-10-14 11:33:11 -07001012 EXPECT_TRUE(pr_answer->Initialize(sdp, NULL));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001013 EXPECT_TRUE(DoSetRemoteDescription(pr_answer));
1014 EXPECT_EQ(PeerConnectionInterface::kHaveRemotePrAnswer, observer_.state_);
1015 webrtc::JsepSessionDescription* answer =
1016 new webrtc::JsepSessionDescription(
1017 SessionDescriptionInterface::kAnswer);
deadbeefab9b2d12015-10-14 11:33:11 -07001018 EXPECT_TRUE(answer->Initialize(sdp, NULL));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001019 EXPECT_TRUE(DoSetRemoteDescription(answer));
1020 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
1021 }
1022
1023 // Help function used for waiting until a the last signaled remote stream has
1024 // the same label as |stream_label|. In a few of the tests in this file we
1025 // answer with the same session description as we offer and thus we can
1026 // check if OnAddStream have been called with the same stream as we offer to
1027 // send.
1028 void WaitAndVerifyOnAddStream(const std::string& stream_label) {
1029 EXPECT_EQ_WAIT(stream_label, observer_.GetLastAddedStreamLabel(), kTimeout);
1030 }
1031
1032 // Creates an offer and applies it as a local session description.
1033 // Creates an answer with the same SDP an the offer but removes all lines
1034 // that start with a:ssrc"
1035 void CreateOfferReceiveAnswerWithoutSsrc() {
1036 CreateOfferAsLocalDescription();
1037 std::string sdp;
1038 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
1039 SetSsrcToZero(&sdp);
1040 CreateAnswerAsRemoteDescription(sdp);
1041 }
1042
deadbeefab9b2d12015-10-14 11:33:11 -07001043 // This function creates a MediaStream with label kStreams[0] and
1044 // |number_of_audio_tracks| and |number_of_video_tracks| tracks and the
1045 // corresponding SessionDescriptionInterface. The SessionDescriptionInterface
kwiberg2bbff992016-03-16 11:03:04 -07001046 // is returned and the MediaStream is stored in
deadbeefab9b2d12015-10-14 11:33:11 -07001047 // |reference_collection_|
kwibergd1fe2812016-04-27 06:47:29 -07001048 std::unique_ptr<SessionDescriptionInterface>
kwiberg2bbff992016-03-16 11:03:04 -07001049 CreateSessionDescriptionAndReference(size_t number_of_audio_tracks,
1050 size_t number_of_video_tracks) {
1051 EXPECT_LE(number_of_audio_tracks, 2u);
1052 EXPECT_LE(number_of_video_tracks, 2u);
deadbeefab9b2d12015-10-14 11:33:11 -07001053
1054 reference_collection_ = StreamCollection::Create();
1055 std::string sdp_ms1 = std::string(kSdpStringInit);
1056
1057 std::string mediastream_label = kStreams[0];
1058
1059 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream(
1060 webrtc::MediaStream::Create(mediastream_label));
1061 reference_collection_->AddStream(stream);
1062
1063 if (number_of_audio_tracks > 0) {
1064 sdp_ms1 += std::string(kSdpStringAudio);
1065 sdp_ms1 += std::string(kSdpStringMs1Audio0);
1066 AddAudioTrack(kAudioTracks[0], stream);
1067 }
1068 if (number_of_audio_tracks > 1) {
1069 sdp_ms1 += kSdpStringMs1Audio1;
1070 AddAudioTrack(kAudioTracks[1], stream);
1071 }
1072
1073 if (number_of_video_tracks > 0) {
1074 sdp_ms1 += std::string(kSdpStringVideo);
1075 sdp_ms1 += std::string(kSdpStringMs1Video0);
1076 AddVideoTrack(kVideoTracks[0], stream);
1077 }
1078 if (number_of_video_tracks > 1) {
1079 sdp_ms1 += kSdpStringMs1Video1;
1080 AddVideoTrack(kVideoTracks[1], stream);
1081 }
1082
kwibergd1fe2812016-04-27 06:47:29 -07001083 return std::unique_ptr<SessionDescriptionInterface>(
kwiberg2bbff992016-03-16 11:03:04 -07001084 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
1085 sdp_ms1, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07001086 }
1087
1088 void AddAudioTrack(const std::string& track_id,
1089 MediaStreamInterface* stream) {
1090 rtc::scoped_refptr<webrtc::AudioTrackInterface> audio_track(
1091 webrtc::AudioTrack::Create(track_id, nullptr));
1092 ASSERT_TRUE(stream->AddTrack(audio_track));
1093 }
1094
1095 void AddVideoTrack(const std::string& track_id,
1096 MediaStreamInterface* stream) {
1097 rtc::scoped_refptr<webrtc::VideoTrackInterface> video_track(
nisseaf510af2016-03-21 08:20:42 -07001098 webrtc::VideoTrack::Create(track_id,
1099 webrtc::FakeVideoTrackSource::Create()));
deadbeefab9b2d12015-10-14 11:33:11 -07001100 ASSERT_TRUE(stream->AddTrack(video_track));
1101 }
1102
kwibergfd8be342016-05-14 19:44:11 -07001103 std::unique_ptr<SessionDescriptionInterface> CreateOfferWithOneAudioStream() {
deadbeef293e9262017-01-11 12:28:30 -08001104 CreatePeerConnectionWithoutDtls();
zhihuang8f65cdf2016-05-06 18:40:30 -07001105 AddVoiceStream(kStreamLabel1);
kwibergfd8be342016-05-14 19:44:11 -07001106 std::unique_ptr<SessionDescriptionInterface> offer;
zhihuang8f65cdf2016-05-06 18:40:30 -07001107 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
1108 return offer;
1109 }
1110
kwibergfd8be342016-05-14 19:44:11 -07001111 std::unique_ptr<SessionDescriptionInterface>
zhihuang8f65cdf2016-05-06 18:40:30 -07001112 CreateAnswerWithOneAudioStream() {
kwibergfd8be342016-05-14 19:44:11 -07001113 std::unique_ptr<SessionDescriptionInterface> offer =
zhihuang8f65cdf2016-05-06 18:40:30 -07001114 CreateOfferWithOneAudioStream();
1115 EXPECT_TRUE(DoSetRemoteDescription(offer.release()));
kwibergfd8be342016-05-14 19:44:11 -07001116 std::unique_ptr<SessionDescriptionInterface> answer;
zhihuang8f65cdf2016-05-06 18:40:30 -07001117 EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
1118 return answer;
1119 }
1120
1121 const std::string& GetFirstAudioStreamCname(
1122 const SessionDescriptionInterface* desc) {
1123 const cricket::ContentInfo* audio_content =
1124 cricket::GetFirstAudioContent(desc->description());
1125 const cricket::AudioContentDescription* audio_desc =
1126 static_cast<const cricket::AudioContentDescription*>(
1127 audio_content->description);
1128 return audio_desc->streams()[0].cname;
1129 }
1130
deadbeef9a6f4d42017-05-15 19:43:33 -07001131 std::unique_ptr<rtc::PhysicalSocketServer> pss_;
1132 std::unique_ptr<rtc::VirtualSocketServer> vss_;
1133 rtc::AutoSocketServerThread main_;
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08001134 cricket::FakePortAllocator* port_allocator_ = nullptr;
deadbeef8662f942017-01-20 21:20:51 -08001135 FakeRTCCertificateGenerator* fake_certificate_generator_ = nullptr;
zhihuang9763d562016-08-05 11:14:50 -07001136 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface> pc_factory_;
1137 rtc::scoped_refptr<PeerConnectionFactoryForTest> pc_factory_for_test_;
1138 rtc::scoped_refptr<PeerConnectionInterface> pc_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001139 MockPeerConnectionObserver observer_;
deadbeefab9b2d12015-10-14 11:33:11 -07001140 rtc::scoped_refptr<StreamCollection> reference_collection_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001141};
1142
zhihuang29ff8442016-07-27 11:07:25 -07001143// Test that no callbacks on the PeerConnectionObserver are called after the
1144// PeerConnection is closed.
1145TEST_F(PeerConnectionInterfaceTest, CloseAndTestCallbackFunctions) {
zhihuang9763d562016-08-05 11:14:50 -07001146 rtc::scoped_refptr<PeerConnectionInterface> pc(
zhihuang29ff8442016-07-27 11:07:25 -07001147 pc_factory_for_test_->CreatePeerConnection(
1148 PeerConnectionInterface::RTCConfiguration(), nullptr, nullptr,
1149 nullptr, &observer_));
1150 observer_.SetPeerConnectionInterface(pc.get());
1151 pc->Close();
1152
1153 // No callbacks is expected to be called.
zhihuang81c3a032016-11-17 12:06:24 -08001154 observer_.callback_triggered_ = false;
zhihuang29ff8442016-07-27 11:07:25 -07001155 std::vector<cricket::Candidate> candidates;
1156 pc_factory_for_test_->transport_controller->SignalGatheringState(
1157 cricket::IceGatheringState{});
1158 pc_factory_for_test_->transport_controller->SignalCandidatesGathered(
1159 "", candidates);
1160 pc_factory_for_test_->transport_controller->SignalConnectionState(
1161 cricket::IceConnectionState{});
1162 pc_factory_for_test_->transport_controller->SignalCandidatesRemoved(
1163 candidates);
1164 pc_factory_for_test_->transport_controller->SignalReceiving(false);
zhihuang81c3a032016-11-17 12:06:24 -08001165 EXPECT_FALSE(observer_.callback_triggered_);
zhihuang29ff8442016-07-27 11:07:25 -07001166}
1167
zhihuang8f65cdf2016-05-06 18:40:30 -07001168// Generate different CNAMEs when PeerConnections are created.
1169// The CNAMEs are expected to be generated randomly. It is possible
1170// that the test fails, though the possibility is very low.
1171TEST_F(PeerConnectionInterfaceTest, CnameGenerationInOffer) {
kwibergfd8be342016-05-14 19:44:11 -07001172 std::unique_ptr<SessionDescriptionInterface> offer1 =
zhihuang8f65cdf2016-05-06 18:40:30 -07001173 CreateOfferWithOneAudioStream();
kwibergfd8be342016-05-14 19:44:11 -07001174 std::unique_ptr<SessionDescriptionInterface> offer2 =
zhihuang8f65cdf2016-05-06 18:40:30 -07001175 CreateOfferWithOneAudioStream();
1176 EXPECT_NE(GetFirstAudioStreamCname(offer1.get()),
1177 GetFirstAudioStreamCname(offer2.get()));
1178}
1179
1180TEST_F(PeerConnectionInterfaceTest, CnameGenerationInAnswer) {
kwibergfd8be342016-05-14 19:44:11 -07001181 std::unique_ptr<SessionDescriptionInterface> answer1 =
zhihuang8f65cdf2016-05-06 18:40:30 -07001182 CreateAnswerWithOneAudioStream();
kwibergfd8be342016-05-14 19:44:11 -07001183 std::unique_ptr<SessionDescriptionInterface> answer2 =
zhihuang8f65cdf2016-05-06 18:40:30 -07001184 CreateAnswerWithOneAudioStream();
1185 EXPECT_NE(GetFirstAudioStreamCname(answer1.get()),
1186 GetFirstAudioStreamCname(answer2.get()));
1187}
1188
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001189TEST_F(PeerConnectionInterfaceTest,
1190 CreatePeerConnectionWithDifferentConfigurations) {
1191 CreatePeerConnectionWithDifferentConfigurations();
1192}
1193
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001194TEST_F(PeerConnectionInterfaceTest,
1195 CreatePeerConnectionWithDifferentIceTransportsTypes) {
1196 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kNone);
1197 EXPECT_EQ(cricket::CF_NONE, port_allocator_->candidate_filter());
1198 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kRelay);
1199 EXPECT_EQ(cricket::CF_RELAY, port_allocator_->candidate_filter());
1200 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kNoHost);
1201 EXPECT_EQ(cricket::CF_ALL & ~cricket::CF_HOST,
1202 port_allocator_->candidate_filter());
1203 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kAll);
1204 EXPECT_EQ(cricket::CF_ALL, port_allocator_->candidate_filter());
1205}
1206
1207// Test that when a PeerConnection is created with a nonzero candidate pool
1208// size, the pooled PortAllocatorSession is created with all the attributes
1209// in the RTCConfiguration.
1210TEST_F(PeerConnectionInterfaceTest, CreatePeerConnectionWithPooledCandidates) {
1211 PeerConnectionInterface::RTCConfiguration config;
1212 PeerConnectionInterface::IceServer server;
1213 server.uri = kStunAddressOnly;
1214 config.servers.push_back(server);
1215 config.type = PeerConnectionInterface::kRelay;
1216 config.disable_ipv6 = true;
1217 config.tcp_candidate_policy =
1218 PeerConnectionInterface::kTcpCandidatePolicyDisabled;
honghaiz60347052016-05-31 18:29:12 -07001219 config.candidate_network_policy =
1220 PeerConnectionInterface::kCandidateNetworkPolicyLowCost;
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001221 config.ice_candidate_pool_size = 1;
1222 CreatePeerConnection(config, nullptr);
1223
1224 const cricket::FakePortAllocatorSession* session =
1225 static_cast<const cricket::FakePortAllocatorSession*>(
1226 port_allocator_->GetPooledSession());
1227 ASSERT_NE(nullptr, session);
1228 EXPECT_EQ(1UL, session->stun_servers().size());
1229 EXPECT_EQ(0U, session->flags() & cricket::PORTALLOCATOR_ENABLE_IPV6);
1230 EXPECT_LT(0U, session->flags() & cricket::PORTALLOCATOR_DISABLE_TCP);
honghaiz60347052016-05-31 18:29:12 -07001231 EXPECT_LT(0U,
1232 session->flags() & cricket::PORTALLOCATOR_DISABLE_COSTLY_NETWORKS);
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001233}
1234
Taylor Brandstetterf8e65772016-06-27 17:20:15 -07001235// Test that the PeerConnection initializes the port allocator passed into it,
1236// and on the correct thread.
1237TEST_F(PeerConnectionInterfaceTest,
1238 CreatePeerConnectionInitializesPortAllocator) {
1239 rtc::Thread network_thread;
1240 network_thread.Start();
1241 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface> pc_factory(
1242 webrtc::CreatePeerConnectionFactory(
1243 &network_thread, rtc::Thread::Current(), rtc::Thread::Current(),
1244 nullptr, nullptr, nullptr));
1245 std::unique_ptr<cricket::FakePortAllocator> port_allocator(
1246 new cricket::FakePortAllocator(&network_thread, nullptr));
1247 cricket::FakePortAllocator* raw_port_allocator = port_allocator.get();
1248 PeerConnectionInterface::RTCConfiguration config;
1249 rtc::scoped_refptr<PeerConnectionInterface> pc(
1250 pc_factory->CreatePeerConnection(
1251 config, nullptr, std::move(port_allocator), nullptr, &observer_));
1252 // FakePortAllocator RTC_CHECKs that it's initialized on the right thread,
1253 // so all we have to do here is check that it's initialized.
1254 EXPECT_TRUE(raw_port_allocator->initialized());
1255}
1256
deadbeef46c73892016-11-16 19:42:04 -08001257// Check that GetConfiguration returns the configuration the PeerConnection was
1258// constructed with, before SetConfiguration is called.
1259TEST_F(PeerConnectionInterfaceTest, GetConfigurationAfterCreatePeerConnection) {
1260 PeerConnectionInterface::RTCConfiguration config;
1261 config.type = PeerConnectionInterface::kRelay;
1262 CreatePeerConnection(config, nullptr);
1263
1264 PeerConnectionInterface::RTCConfiguration returned_config =
1265 pc_->GetConfiguration();
1266 EXPECT_EQ(PeerConnectionInterface::kRelay, returned_config.type);
1267}
1268
1269// Check that GetConfiguration returns the last configuration passed into
1270// SetConfiguration.
1271TEST_F(PeerConnectionInterfaceTest, GetConfigurationAfterSetConfiguration) {
1272 CreatePeerConnection();
1273
1274 PeerConnectionInterface::RTCConfiguration config;
1275 config.type = PeerConnectionInterface::kRelay;
1276 EXPECT_TRUE(pc_->SetConfiguration(config));
1277
1278 PeerConnectionInterface::RTCConfiguration returned_config =
1279 pc_->GetConfiguration();
1280 EXPECT_EQ(PeerConnectionInterface::kRelay, returned_config.type);
1281}
1282
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001283TEST_F(PeerConnectionInterfaceTest, AddStreams) {
deadbeef293e9262017-01-11 12:28:30 -08001284 CreatePeerConnectionWithoutDtls();
deadbeefab9b2d12015-10-14 11:33:11 -07001285 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001286 AddVoiceStream(kStreamLabel2);
1287 ASSERT_EQ(2u, pc_->local_streams()->count());
1288
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001289 // Test we can add multiple local streams to one peerconnection.
zhihuang9763d562016-08-05 11:14:50 -07001290 rtc::scoped_refptr<MediaStreamInterface> stream(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001291 pc_factory_->CreateLocalMediaStream(kStreamLabel3));
zhihuang9763d562016-08-05 11:14:50 -07001292 rtc::scoped_refptr<AudioTrackInterface> audio_track(
1293 pc_factory_->CreateAudioTrack(kStreamLabel3,
1294 static_cast<AudioSourceInterface*>(NULL)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001295 stream->AddTrack(audio_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +00001296 EXPECT_TRUE(pc_->AddStream(stream));
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001297 EXPECT_EQ(3u, pc_->local_streams()->count());
1298
1299 // Remove the third stream.
1300 pc_->RemoveStream(pc_->local_streams()->at(2));
1301 EXPECT_EQ(2u, pc_->local_streams()->count());
1302
1303 // Remove the second stream.
1304 pc_->RemoveStream(pc_->local_streams()->at(1));
1305 EXPECT_EQ(1u, pc_->local_streams()->count());
1306
1307 // Remove the first stream.
1308 pc_->RemoveStream(pc_->local_streams()->at(0));
1309 EXPECT_EQ(0u, pc_->local_streams()->count());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001310}
1311
deadbeefab9b2d12015-10-14 11:33:11 -07001312// Test that the created offer includes streams we added.
1313TEST_F(PeerConnectionInterfaceTest, AddedStreamsPresentInOffer) {
deadbeef293e9262017-01-11 12:28:30 -08001314 CreatePeerConnectionWithoutDtls();
deadbeefab9b2d12015-10-14 11:33:11 -07001315 AddAudioVideoStream(kStreamLabel1, "audio_track", "video_track");
kwibergd1fe2812016-04-27 06:47:29 -07001316 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001317 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07001318
1319 const cricket::ContentInfo* audio_content =
1320 cricket::GetFirstAudioContent(offer->description());
1321 const cricket::AudioContentDescription* audio_desc =
1322 static_cast<const cricket::AudioContentDescription*>(
1323 audio_content->description);
1324 EXPECT_TRUE(
1325 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
1326
1327 const cricket::ContentInfo* video_content =
1328 cricket::GetFirstVideoContent(offer->description());
1329 const cricket::VideoContentDescription* video_desc =
1330 static_cast<const cricket::VideoContentDescription*>(
1331 video_content->description);
1332 EXPECT_TRUE(
1333 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1334
1335 // Add another stream and ensure the offer includes both the old and new
1336 // streams.
1337 AddAudioVideoStream(kStreamLabel2, "audio_track2", "video_track2");
kwiberg2bbff992016-03-16 11:03:04 -07001338 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07001339
1340 audio_content = cricket::GetFirstAudioContent(offer->description());
1341 audio_desc = static_cast<const cricket::AudioContentDescription*>(
1342 audio_content->description);
1343 EXPECT_TRUE(
1344 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
1345 EXPECT_TRUE(
1346 ContainsTrack(audio_desc->streams(), kStreamLabel2, "audio_track2"));
1347
1348 video_content = cricket::GetFirstVideoContent(offer->description());
1349 video_desc = static_cast<const cricket::VideoContentDescription*>(
1350 video_content->description);
1351 EXPECT_TRUE(
1352 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1353 EXPECT_TRUE(
1354 ContainsTrack(video_desc->streams(), kStreamLabel2, "video_track2"));
1355}
1356
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001357TEST_F(PeerConnectionInterfaceTest, RemoveStream) {
deadbeef293e9262017-01-11 12:28:30 -08001358 CreatePeerConnectionWithoutDtls();
deadbeefab9b2d12015-10-14 11:33:11 -07001359 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001360 ASSERT_EQ(1u, pc_->local_streams()->count());
1361 pc_->RemoveStream(pc_->local_streams()->at(0));
1362 EXPECT_EQ(0u, pc_->local_streams()->count());
1363}
1364
deadbeefe1f9d832016-01-14 15:35:42 -08001365// Test for AddTrack and RemoveTrack methods.
1366// Tests that the created offer includes tracks we added,
1367// and that the RtpSenders are created correctly.
1368// Also tests that RemoveTrack removes the tracks from subsequent offers.
1369TEST_F(PeerConnectionInterfaceTest, AddTrackRemoveTrack) {
deadbeef293e9262017-01-11 12:28:30 -08001370 CreatePeerConnectionWithoutDtls();
deadbeefe1f9d832016-01-14 15:35:42 -08001371 // Create a dummy stream, so tracks share a stream label.
zhihuang9763d562016-08-05 11:14:50 -07001372 rtc::scoped_refptr<MediaStreamInterface> stream(
deadbeefe1f9d832016-01-14 15:35:42 -08001373 pc_factory_->CreateLocalMediaStream(kStreamLabel1));
1374 std::vector<MediaStreamInterface*> stream_list;
1375 stream_list.push_back(stream.get());
zhihuang9763d562016-08-05 11:14:50 -07001376 rtc::scoped_refptr<AudioTrackInterface> audio_track(
deadbeefe1f9d832016-01-14 15:35:42 -08001377 pc_factory_->CreateAudioTrack("audio_track", nullptr));
zhihuang9763d562016-08-05 11:14:50 -07001378 rtc::scoped_refptr<VideoTrackInterface> video_track(
1379 pc_factory_->CreateVideoTrack(
deadbeef112b2e92017-02-10 20:13:37 -08001380 "video_track", pc_factory_->CreateVideoSource(
1381 std::unique_ptr<cricket::VideoCapturer>(
1382 new cricket::FakeVideoCapturer()))));
deadbeefe1f9d832016-01-14 15:35:42 -08001383 auto audio_sender = pc_->AddTrack(audio_track, stream_list);
1384 auto video_sender = pc_->AddTrack(video_track, stream_list);
deadbeefa601f5c2016-06-06 14:27:39 -07001385 EXPECT_EQ(1UL, audio_sender->stream_ids().size());
1386 EXPECT_EQ(kStreamLabel1, audio_sender->stream_ids()[0]);
deadbeefe1f9d832016-01-14 15:35:42 -08001387 EXPECT_EQ("audio_track", audio_sender->id());
1388 EXPECT_EQ(audio_track, audio_sender->track());
deadbeefa601f5c2016-06-06 14:27:39 -07001389 EXPECT_EQ(1UL, video_sender->stream_ids().size());
1390 EXPECT_EQ(kStreamLabel1, video_sender->stream_ids()[0]);
deadbeefe1f9d832016-01-14 15:35:42 -08001391 EXPECT_EQ("video_track", video_sender->id());
1392 EXPECT_EQ(video_track, video_sender->track());
1393
1394 // Now create an offer and check for the senders.
kwibergd1fe2812016-04-27 06:47:29 -07001395 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001396 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefe1f9d832016-01-14 15:35:42 -08001397
1398 const cricket::ContentInfo* audio_content =
1399 cricket::GetFirstAudioContent(offer->description());
1400 const cricket::AudioContentDescription* audio_desc =
1401 static_cast<const cricket::AudioContentDescription*>(
1402 audio_content->description);
1403 EXPECT_TRUE(
1404 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
1405
1406 const cricket::ContentInfo* video_content =
1407 cricket::GetFirstVideoContent(offer->description());
1408 const cricket::VideoContentDescription* video_desc =
1409 static_cast<const cricket::VideoContentDescription*>(
1410 video_content->description);
1411 EXPECT_TRUE(
1412 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1413
1414 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
1415
1416 // Now try removing the tracks.
1417 EXPECT_TRUE(pc_->RemoveTrack(audio_sender));
1418 EXPECT_TRUE(pc_->RemoveTrack(video_sender));
1419
1420 // Create a new offer and ensure it doesn't contain the removed senders.
kwiberg2bbff992016-03-16 11:03:04 -07001421 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefe1f9d832016-01-14 15:35:42 -08001422
1423 audio_content = cricket::GetFirstAudioContent(offer->description());
1424 audio_desc = static_cast<const cricket::AudioContentDescription*>(
1425 audio_content->description);
1426 EXPECT_FALSE(
1427 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
1428
1429 video_content = cricket::GetFirstVideoContent(offer->description());
1430 video_desc = static_cast<const cricket::VideoContentDescription*>(
1431 video_content->description);
1432 EXPECT_FALSE(
1433 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1434
1435 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
1436
1437 // Calling RemoveTrack on a sender no longer attached to a PeerConnection
1438 // should return false.
1439 EXPECT_FALSE(pc_->RemoveTrack(audio_sender));
1440 EXPECT_FALSE(pc_->RemoveTrack(video_sender));
1441}
1442
1443// Test creating senders without a stream specified,
1444// expecting a random stream ID to be generated.
1445TEST_F(PeerConnectionInterfaceTest, AddTrackWithoutStream) {
deadbeef293e9262017-01-11 12:28:30 -08001446 CreatePeerConnectionWithoutDtls();
deadbeefe1f9d832016-01-14 15:35:42 -08001447 // Create a dummy stream, so tracks share a stream label.
zhihuang9763d562016-08-05 11:14:50 -07001448 rtc::scoped_refptr<AudioTrackInterface> audio_track(
deadbeefe1f9d832016-01-14 15:35:42 -08001449 pc_factory_->CreateAudioTrack("audio_track", nullptr));
zhihuang9763d562016-08-05 11:14:50 -07001450 rtc::scoped_refptr<VideoTrackInterface> video_track(
1451 pc_factory_->CreateVideoTrack(
deadbeef112b2e92017-02-10 20:13:37 -08001452 "video_track", pc_factory_->CreateVideoSource(
1453 std::unique_ptr<cricket::VideoCapturer>(
1454 new cricket::FakeVideoCapturer()))));
deadbeefe1f9d832016-01-14 15:35:42 -08001455 auto audio_sender =
1456 pc_->AddTrack(audio_track, std::vector<MediaStreamInterface*>());
1457 auto video_sender =
1458 pc_->AddTrack(video_track, std::vector<MediaStreamInterface*>());
1459 EXPECT_EQ("audio_track", audio_sender->id());
1460 EXPECT_EQ(audio_track, audio_sender->track());
1461 EXPECT_EQ("video_track", video_sender->id());
1462 EXPECT_EQ(video_track, video_sender->track());
1463 // If the ID is truly a random GUID, it should be infinitely unlikely they
1464 // will be the same.
deadbeefa601f5c2016-06-06 14:27:39 -07001465 EXPECT_NE(video_sender->stream_ids(), audio_sender->stream_ids());
deadbeefe1f9d832016-01-14 15:35:42 -08001466}
1467
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001468TEST_F(PeerConnectionInterfaceTest, CreateOfferReceiveAnswer) {
1469 InitiateCall();
1470 WaitAndVerifyOnAddStream(kStreamLabel1);
1471 VerifyRemoteRtpHeaderExtensions();
1472}
1473
1474TEST_F(PeerConnectionInterfaceTest, CreateOfferReceivePrAnswerAndAnswer) {
deadbeef293e9262017-01-11 12:28:30 -08001475 CreatePeerConnectionWithoutDtls();
deadbeefab9b2d12015-10-14 11:33:11 -07001476 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001477 CreateOfferAsLocalDescription();
1478 std::string offer;
1479 EXPECT_TRUE(pc_->local_description()->ToString(&offer));
1480 CreatePrAnswerAndAnswerAsRemoteDescription(offer);
1481 WaitAndVerifyOnAddStream(kStreamLabel1);
1482}
1483
1484TEST_F(PeerConnectionInterfaceTest, ReceiveOfferCreateAnswer) {
deadbeef293e9262017-01-11 12:28:30 -08001485 CreatePeerConnectionWithoutDtls();
deadbeefab9b2d12015-10-14 11:33:11 -07001486 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001487
1488 CreateOfferAsRemoteDescription();
1489 CreateAnswerAsLocalDescription();
1490
1491 WaitAndVerifyOnAddStream(kStreamLabel1);
1492}
1493
1494TEST_F(PeerConnectionInterfaceTest, ReceiveOfferCreatePrAnswerAndAnswer) {
deadbeef293e9262017-01-11 12:28:30 -08001495 CreatePeerConnectionWithoutDtls();
deadbeefab9b2d12015-10-14 11:33:11 -07001496 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001497
1498 CreateOfferAsRemoteDescription();
1499 CreatePrAnswerAsLocalDescription();
1500 CreateAnswerAsLocalDescription();
1501
1502 WaitAndVerifyOnAddStream(kStreamLabel1);
1503}
1504
1505TEST_F(PeerConnectionInterfaceTest, Renegotiate) {
1506 InitiateCall();
1507 ASSERT_EQ(1u, pc_->remote_streams()->count());
1508 pc_->RemoveStream(pc_->local_streams()->at(0));
1509 CreateOfferReceiveAnswer();
1510 EXPECT_EQ(0u, pc_->remote_streams()->count());
deadbeefab9b2d12015-10-14 11:33:11 -07001511 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001512 CreateOfferReceiveAnswer();
1513}
1514
1515// Tests that after negotiating an audio only call, the respondent can perform a
1516// renegotiation that removes the audio stream.
1517TEST_F(PeerConnectionInterfaceTest, RenegotiateAudioOnly) {
deadbeef293e9262017-01-11 12:28:30 -08001518 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001519 AddVoiceStream(kStreamLabel1);
1520 CreateOfferAsRemoteDescription();
1521 CreateAnswerAsLocalDescription();
1522
1523 ASSERT_EQ(1u, pc_->remote_streams()->count());
1524 pc_->RemoveStream(pc_->local_streams()->at(0));
1525 CreateOfferReceiveAnswer();
1526 EXPECT_EQ(0u, pc_->remote_streams()->count());
1527}
1528
1529// Test that candidates are generated and that we can parse our own candidates.
1530TEST_F(PeerConnectionInterfaceTest, IceCandidates) {
deadbeef293e9262017-01-11 12:28:30 -08001531 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001532
1533 EXPECT_FALSE(pc_->AddIceCandidate(observer_.last_candidate_.get()));
1534 // SetRemoteDescription takes ownership of offer.
kwibergd1fe2812016-04-27 06:47:29 -07001535 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefab9b2d12015-10-14 11:33:11 -07001536 AddVideoStream(kStreamLabel1);
deadbeefc80741f2015-10-22 13:14:45 -07001537 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
kwiberg2bbff992016-03-16 11:03:04 -07001538 EXPECT_TRUE(DoSetRemoteDescription(offer.release()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001539
1540 // SetLocalDescription takes ownership of answer.
kwibergd1fe2812016-04-27 06:47:29 -07001541 std::unique_ptr<SessionDescriptionInterface> answer;
deadbeefc80741f2015-10-22 13:14:45 -07001542 EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
kwiberg2bbff992016-03-16 11:03:04 -07001543 EXPECT_TRUE(DoSetLocalDescription(answer.release()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001544
1545 EXPECT_TRUE_WAIT(observer_.last_candidate_.get() != NULL, kTimeout);
1546 EXPECT_TRUE_WAIT(observer_.ice_complete_, kTimeout);
1547
1548 EXPECT_TRUE(pc_->AddIceCandidate(observer_.last_candidate_.get()));
1549}
1550
deadbeefab9b2d12015-10-14 11:33:11 -07001551// Test that CreateOffer and CreateAnswer will fail if the track labels are
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001552// not unique.
1553TEST_F(PeerConnectionInterfaceTest, CreateOfferAnswerWithInvalidStream) {
deadbeef293e9262017-01-11 12:28:30 -08001554 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001555 // Create a regular offer for the CreateAnswer test later.
kwibergd1fe2812016-04-27 06:47:29 -07001556 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefc80741f2015-10-22 13:14:45 -07001557 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
kwiberg2bbff992016-03-16 11:03:04 -07001558 EXPECT_TRUE(offer);
1559 offer.reset();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001560
1561 // Create a local stream with audio&video tracks having same label.
1562 AddAudioVideoStream(kStreamLabel1, "track_label", "track_label");
1563
1564 // Test CreateOffer
deadbeefc80741f2015-10-22 13:14:45 -07001565 EXPECT_FALSE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001566
1567 // Test CreateAnswer
kwibergd1fe2812016-04-27 06:47:29 -07001568 std::unique_ptr<SessionDescriptionInterface> answer;
deadbeefc80741f2015-10-22 13:14:45 -07001569 EXPECT_FALSE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001570}
1571
1572// Test that we will get different SSRCs for each tracks in the offer and answer
1573// we created.
1574TEST_F(PeerConnectionInterfaceTest, SsrcInOfferAnswer) {
deadbeef293e9262017-01-11 12:28:30 -08001575 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001576 // Create a local stream with audio&video tracks having different labels.
1577 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
1578
1579 // Test CreateOffer
kwibergd1fe2812016-04-27 06:47:29 -07001580 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001581 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001582 int audio_ssrc = 0;
1583 int video_ssrc = 0;
1584 EXPECT_TRUE(GetFirstSsrc(GetFirstAudioContent(offer->description()),
1585 &audio_ssrc));
1586 EXPECT_TRUE(GetFirstSsrc(GetFirstVideoContent(offer->description()),
1587 &video_ssrc));
1588 EXPECT_NE(audio_ssrc, video_ssrc);
1589
1590 // Test CreateAnswer
1591 EXPECT_TRUE(DoSetRemoteDescription(offer.release()));
kwibergd1fe2812016-04-27 06:47:29 -07001592 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -07001593 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001594 audio_ssrc = 0;
1595 video_ssrc = 0;
1596 EXPECT_TRUE(GetFirstSsrc(GetFirstAudioContent(answer->description()),
1597 &audio_ssrc));
1598 EXPECT_TRUE(GetFirstSsrc(GetFirstVideoContent(answer->description()),
1599 &video_ssrc));
1600 EXPECT_NE(audio_ssrc, video_ssrc);
1601}
1602
deadbeefeb459812015-12-15 19:24:43 -08001603// Test that it's possible to call AddTrack on a MediaStream after adding
1604// the stream to a PeerConnection.
1605// TODO(deadbeef): Remove this test once this behavior is no longer supported.
1606TEST_F(PeerConnectionInterfaceTest, AddTrackAfterAddStream) {
deadbeef293e9262017-01-11 12:28:30 -08001607 CreatePeerConnectionWithoutDtls();
deadbeefeb459812015-12-15 19:24:43 -08001608 // Create audio stream and add to PeerConnection.
1609 AddVoiceStream(kStreamLabel1);
1610 MediaStreamInterface* stream = pc_->local_streams()->at(0);
1611
1612 // Add video track to the audio-only stream.
zhihuang9763d562016-08-05 11:14:50 -07001613 rtc::scoped_refptr<VideoTrackInterface> video_track(
1614 pc_factory_->CreateVideoTrack(
deadbeef112b2e92017-02-10 20:13:37 -08001615 "video_label", pc_factory_->CreateVideoSource(
1616 std::unique_ptr<cricket::VideoCapturer>(
1617 new cricket::FakeVideoCapturer()))));
deadbeefeb459812015-12-15 19:24:43 -08001618 stream->AddTrack(video_track.get());
1619
kwibergd1fe2812016-04-27 06:47:29 -07001620 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001621 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefeb459812015-12-15 19:24:43 -08001622
1623 const cricket::MediaContentDescription* video_desc =
1624 cricket::GetFirstVideoContentDescription(offer->description());
1625 EXPECT_TRUE(video_desc != nullptr);
1626}
1627
1628// Test that it's possible to call RemoveTrack on a MediaStream after adding
1629// the stream to a PeerConnection.
1630// TODO(deadbeef): Remove this test once this behavior is no longer supported.
1631TEST_F(PeerConnectionInterfaceTest, RemoveTrackAfterAddStream) {
deadbeef293e9262017-01-11 12:28:30 -08001632 CreatePeerConnectionWithoutDtls();
deadbeefeb459812015-12-15 19:24:43 -08001633 // Create audio/video stream and add to PeerConnection.
1634 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
1635 MediaStreamInterface* stream = pc_->local_streams()->at(0);
1636
1637 // Remove the video track.
1638 stream->RemoveTrack(stream->GetVideoTracks()[0]);
1639
kwibergd1fe2812016-04-27 06:47:29 -07001640 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001641 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefeb459812015-12-15 19:24:43 -08001642
1643 const cricket::MediaContentDescription* video_desc =
1644 cricket::GetFirstVideoContentDescription(offer->description());
1645 EXPECT_TRUE(video_desc == nullptr);
1646}
1647
deadbeef1dcb1642017-03-29 21:08:16 -07001648// Verify that CreateDtmfSender only succeeds if called with a valid local
1649// track. Other aspects of DtmfSenders are tested in
1650// peerconnection_integrationtest.cc.
1651TEST_F(PeerConnectionInterfaceTest, CreateDtmfSenderWithInvalidParams) {
1652 CreatePeerConnection();
1653 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
1654 EXPECT_EQ(nullptr, pc_->CreateDtmfSender(nullptr));
1655 rtc::scoped_refptr<webrtc::AudioTrackInterface> non_localtrack(
1656 pc_factory_->CreateAudioTrack("dummy_track", nullptr));
1657 EXPECT_EQ(nullptr, pc_->CreateDtmfSender(non_localtrack));
1658}
1659
deadbeefbd7d8f72015-12-18 16:58:44 -08001660// Test creating a sender with a stream ID, and ensure the ID is populated
1661// in the offer.
1662TEST_F(PeerConnectionInterfaceTest, CreateSenderWithStream) {
deadbeef293e9262017-01-11 12:28:30 -08001663 CreatePeerConnectionWithoutDtls();
deadbeefbd7d8f72015-12-18 16:58:44 -08001664 pc_->CreateSender("video", kStreamLabel1);
1665
kwibergd1fe2812016-04-27 06:47:29 -07001666 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001667 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefbd7d8f72015-12-18 16:58:44 -08001668
1669 const cricket::MediaContentDescription* video_desc =
1670 cricket::GetFirstVideoContentDescription(offer->description());
1671 ASSERT_TRUE(video_desc != nullptr);
1672 ASSERT_EQ(1u, video_desc->streams().size());
1673 EXPECT_EQ(kStreamLabel1, video_desc->streams()[0].sync_label);
1674}
1675
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001676// Test that we can specify a certain track that we want statistics about.
1677TEST_F(PeerConnectionInterfaceTest, GetStatsForSpecificTrack) {
1678 InitiateCall();
1679 ASSERT_LT(0u, pc_->remote_streams()->count());
1680 ASSERT_LT(0u, pc_->remote_streams()->at(0)->GetAudioTracks().size());
zhihuang9763d562016-08-05 11:14:50 -07001681 rtc::scoped_refptr<MediaStreamTrackInterface> remote_audio =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001682 pc_->remote_streams()->at(0)->GetAudioTracks()[0];
1683 EXPECT_TRUE(DoGetStats(remote_audio));
1684
1685 // Remove the stream. Since we are sending to our selves the local
1686 // and the remote stream is the same.
1687 pc_->RemoveStream(pc_->local_streams()->at(0));
1688 // Do a re-negotiation.
1689 CreateOfferReceiveAnswer();
1690
1691 ASSERT_EQ(0u, pc_->remote_streams()->count());
1692
1693 // Test that we still can get statistics for the old track. Even if it is not
1694 // sent any longer.
1695 EXPECT_TRUE(DoGetStats(remote_audio));
1696}
1697
1698// Test that we can get stats on a video track.
1699TEST_F(PeerConnectionInterfaceTest, GetStatsForVideoTrack) {
1700 InitiateCall();
1701 ASSERT_LT(0u, pc_->remote_streams()->count());
1702 ASSERT_LT(0u, pc_->remote_streams()->at(0)->GetVideoTracks().size());
zhihuang9763d562016-08-05 11:14:50 -07001703 rtc::scoped_refptr<MediaStreamTrackInterface> remote_video =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001704 pc_->remote_streams()->at(0)->GetVideoTracks()[0];
1705 EXPECT_TRUE(DoGetStats(remote_video));
1706}
1707
1708// Test that we don't get statistics for an invalid track.
zhihuange9e94c32016-11-04 11:38:15 -07001709TEST_F(PeerConnectionInterfaceTest, GetStatsForInvalidTrack) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001710 InitiateCall();
zhihuang9763d562016-08-05 11:14:50 -07001711 rtc::scoped_refptr<AudioTrackInterface> unknown_audio_track(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001712 pc_factory_->CreateAudioTrack("unknown track", NULL));
1713 EXPECT_FALSE(DoGetStats(unknown_audio_track));
1714}
1715
1716// This test setup two RTP data channels in loop back.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001717TEST_F(PeerConnectionInterfaceTest, TestDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001718 FakeConstraints constraints;
1719 constraints.SetAllowRtpDataChannels();
1720 CreatePeerConnection(&constraints);
zhihuang9763d562016-08-05 11:14:50 -07001721 rtc::scoped_refptr<DataChannelInterface> data1 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001722 pc_->CreateDataChannel("test1", NULL);
zhihuang9763d562016-08-05 11:14:50 -07001723 rtc::scoped_refptr<DataChannelInterface> data2 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001724 pc_->CreateDataChannel("test2", NULL);
1725 ASSERT_TRUE(data1 != NULL);
kwibergd1fe2812016-04-27 06:47:29 -07001726 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001727 new MockDataChannelObserver(data1));
kwibergd1fe2812016-04-27 06:47:29 -07001728 std::unique_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001729 new MockDataChannelObserver(data2));
1730
1731 EXPECT_EQ(DataChannelInterface::kConnecting, data1->state());
1732 EXPECT_EQ(DataChannelInterface::kConnecting, data2->state());
1733 std::string data_to_send1 = "testing testing";
1734 std::string data_to_send2 = "testing something else";
1735 EXPECT_FALSE(data1->Send(DataBuffer(data_to_send1)));
1736
1737 CreateOfferReceiveAnswer();
1738 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1739 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
1740
1741 EXPECT_EQ(DataChannelInterface::kOpen, data1->state());
1742 EXPECT_EQ(DataChannelInterface::kOpen, data2->state());
1743 EXPECT_TRUE(data1->Send(DataBuffer(data_to_send1)));
1744 EXPECT_TRUE(data2->Send(DataBuffer(data_to_send2)));
1745
1746 EXPECT_EQ_WAIT(data_to_send1, observer1->last_message(), kTimeout);
1747 EXPECT_EQ_WAIT(data_to_send2, observer2->last_message(), kTimeout);
1748
1749 data1->Close();
1750 EXPECT_EQ(DataChannelInterface::kClosing, data1->state());
1751 CreateOfferReceiveAnswer();
1752 EXPECT_FALSE(observer1->IsOpen());
1753 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
1754 EXPECT_TRUE(observer2->IsOpen());
1755
1756 data_to_send2 = "testing something else again";
1757 EXPECT_TRUE(data2->Send(DataBuffer(data_to_send2)));
1758
1759 EXPECT_EQ_WAIT(data_to_send2, observer2->last_message(), kTimeout);
1760}
1761
1762// This test verifies that sendnig binary data over RTP data channels should
1763// fail.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001764TEST_F(PeerConnectionInterfaceTest, TestSendBinaryOnRtpDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001765 FakeConstraints constraints;
1766 constraints.SetAllowRtpDataChannels();
1767 CreatePeerConnection(&constraints);
zhihuang9763d562016-08-05 11:14:50 -07001768 rtc::scoped_refptr<DataChannelInterface> data1 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001769 pc_->CreateDataChannel("test1", NULL);
zhihuang9763d562016-08-05 11:14:50 -07001770 rtc::scoped_refptr<DataChannelInterface> data2 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001771 pc_->CreateDataChannel("test2", NULL);
1772 ASSERT_TRUE(data1 != NULL);
kwibergd1fe2812016-04-27 06:47:29 -07001773 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001774 new MockDataChannelObserver(data1));
kwibergd1fe2812016-04-27 06:47:29 -07001775 std::unique_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001776 new MockDataChannelObserver(data2));
1777
1778 EXPECT_EQ(DataChannelInterface::kConnecting, data1->state());
1779 EXPECT_EQ(DataChannelInterface::kConnecting, data2->state());
1780
1781 CreateOfferReceiveAnswer();
1782 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1783 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
1784
1785 EXPECT_EQ(DataChannelInterface::kOpen, data1->state());
1786 EXPECT_EQ(DataChannelInterface::kOpen, data2->state());
1787
jbaucheec21bd2016-03-20 06:15:43 -07001788 rtc::CopyOnWriteBuffer buffer("test", 4);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001789 EXPECT_FALSE(data1->Send(DataBuffer(buffer, true)));
1790}
1791
1792// This test setup a RTP data channels in loop back and test that a channel is
1793// opened even if the remote end answer with a zero SSRC.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001794TEST_F(PeerConnectionInterfaceTest, TestSendOnlyDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001795 FakeConstraints constraints;
1796 constraints.SetAllowRtpDataChannels();
1797 CreatePeerConnection(&constraints);
zhihuang9763d562016-08-05 11:14:50 -07001798 rtc::scoped_refptr<DataChannelInterface> data1 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001799 pc_->CreateDataChannel("test1", NULL);
kwibergd1fe2812016-04-27 06:47:29 -07001800 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001801 new MockDataChannelObserver(data1));
1802
1803 CreateOfferReceiveAnswerWithoutSsrc();
1804
1805 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1806
1807 data1->Close();
1808 EXPECT_EQ(DataChannelInterface::kClosing, data1->state());
1809 CreateOfferReceiveAnswerWithoutSsrc();
1810 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
1811 EXPECT_FALSE(observer1->IsOpen());
1812}
1813
1814// This test that if a data channel is added in an answer a receive only channel
1815// channel is created.
1816TEST_F(PeerConnectionInterfaceTest, TestReceiveOnlyDataChannel) {
1817 FakeConstraints constraints;
1818 constraints.SetAllowRtpDataChannels();
1819 CreatePeerConnection(&constraints);
1820
1821 std::string offer_label = "offer_channel";
zhihuang9763d562016-08-05 11:14:50 -07001822 rtc::scoped_refptr<DataChannelInterface> offer_channel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001823 pc_->CreateDataChannel(offer_label, NULL);
1824
1825 CreateOfferAsLocalDescription();
1826
1827 // Replace the data channel label in the offer and apply it as an answer.
1828 std::string receive_label = "answer_channel";
1829 std::string sdp;
1830 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001831 rtc::replace_substrs(offer_label.c_str(), offer_label.length(),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001832 receive_label.c_str(), receive_label.length(),
1833 &sdp);
1834 CreateAnswerAsRemoteDescription(sdp);
1835
1836 // Verify that a new incoming data channel has been created and that
1837 // it is open but can't we written to.
1838 ASSERT_TRUE(observer_.last_datachannel_ != NULL);
1839 DataChannelInterface* received_channel = observer_.last_datachannel_;
1840 EXPECT_EQ(DataChannelInterface::kConnecting, received_channel->state());
1841 EXPECT_EQ(receive_label, received_channel->label());
1842 EXPECT_FALSE(received_channel->Send(DataBuffer("something")));
1843
1844 // Verify that the channel we initially offered has been rejected.
1845 EXPECT_EQ(DataChannelInterface::kClosed, offer_channel->state());
1846
1847 // Do another offer / answer exchange and verify that the data channel is
1848 // opened.
1849 CreateOfferReceiveAnswer();
1850 EXPECT_EQ_WAIT(DataChannelInterface::kOpen, received_channel->state(),
1851 kTimeout);
1852}
1853
1854// This test that no data channel is returned if a reliable channel is
1855// requested.
1856// TODO(perkj): Remove this test once reliable channels are implemented.
1857TEST_F(PeerConnectionInterfaceTest, CreateReliableRtpDataChannelShouldFail) {
1858 FakeConstraints constraints;
1859 constraints.SetAllowRtpDataChannels();
1860 CreatePeerConnection(&constraints);
1861
1862 std::string label = "test";
1863 webrtc::DataChannelInit config;
1864 config.reliable = true;
zhihuang9763d562016-08-05 11:14:50 -07001865 rtc::scoped_refptr<DataChannelInterface> channel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001866 pc_->CreateDataChannel(label, &config);
1867 EXPECT_TRUE(channel == NULL);
1868}
1869
deadbeefab9b2d12015-10-14 11:33:11 -07001870// Verifies that duplicated label is not allowed for RTP data channel.
1871TEST_F(PeerConnectionInterfaceTest, RtpDuplicatedLabelNotAllowed) {
1872 FakeConstraints constraints;
1873 constraints.SetAllowRtpDataChannels();
1874 CreatePeerConnection(&constraints);
1875
1876 std::string label = "test";
zhihuang9763d562016-08-05 11:14:50 -07001877 rtc::scoped_refptr<DataChannelInterface> channel =
deadbeefab9b2d12015-10-14 11:33:11 -07001878 pc_->CreateDataChannel(label, nullptr);
1879 EXPECT_NE(channel, nullptr);
1880
zhihuang9763d562016-08-05 11:14:50 -07001881 rtc::scoped_refptr<DataChannelInterface> dup_channel =
deadbeefab9b2d12015-10-14 11:33:11 -07001882 pc_->CreateDataChannel(label, nullptr);
1883 EXPECT_EQ(dup_channel, nullptr);
1884}
1885
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001886// This tests that a SCTP data channel is returned using different
1887// DataChannelInit configurations.
1888TEST_F(PeerConnectionInterfaceTest, CreateSctpDataChannel) {
1889 FakeConstraints constraints;
1890 constraints.SetAllowDtlsSctpDataChannels();
1891 CreatePeerConnection(&constraints);
1892
1893 webrtc::DataChannelInit config;
1894
zhihuang9763d562016-08-05 11:14:50 -07001895 rtc::scoped_refptr<DataChannelInterface> channel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001896 pc_->CreateDataChannel("1", &config);
1897 EXPECT_TRUE(channel != NULL);
1898 EXPECT_TRUE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001899 EXPECT_TRUE(observer_.renegotiation_needed_);
1900 observer_.renegotiation_needed_ = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001901
1902 config.ordered = false;
1903 channel = pc_->CreateDataChannel("2", &config);
1904 EXPECT_TRUE(channel != NULL);
1905 EXPECT_TRUE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001906 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001907
1908 config.ordered = true;
1909 config.maxRetransmits = 0;
1910 channel = pc_->CreateDataChannel("3", &config);
1911 EXPECT_TRUE(channel != NULL);
1912 EXPECT_FALSE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001913 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001914
1915 config.maxRetransmits = -1;
1916 config.maxRetransmitTime = 0;
1917 channel = pc_->CreateDataChannel("4", &config);
1918 EXPECT_TRUE(channel != NULL);
1919 EXPECT_FALSE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001920 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001921}
1922
1923// This tests that no data channel is returned if both maxRetransmits and
1924// maxRetransmitTime are set for SCTP data channels.
1925TEST_F(PeerConnectionInterfaceTest,
1926 CreateSctpDataChannelShouldFailForInvalidConfig) {
1927 FakeConstraints constraints;
1928 constraints.SetAllowDtlsSctpDataChannels();
1929 CreatePeerConnection(&constraints);
1930
1931 std::string label = "test";
1932 webrtc::DataChannelInit config;
1933 config.maxRetransmits = 0;
1934 config.maxRetransmitTime = 0;
1935
zhihuang9763d562016-08-05 11:14:50 -07001936 rtc::scoped_refptr<DataChannelInterface> channel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001937 pc_->CreateDataChannel(label, &config);
1938 EXPECT_TRUE(channel == NULL);
1939}
1940
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001941// The test verifies that creating a SCTP data channel with an id already in use
1942// or out of range should fail.
1943TEST_F(PeerConnectionInterfaceTest,
1944 CreateSctpDataChannelWithInvalidIdShouldFail) {
1945 FakeConstraints constraints;
1946 constraints.SetAllowDtlsSctpDataChannels();
1947 CreatePeerConnection(&constraints);
1948
1949 webrtc::DataChannelInit config;
zhihuang9763d562016-08-05 11:14:50 -07001950 rtc::scoped_refptr<DataChannelInterface> channel;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001951
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001952 config.id = 1;
1953 channel = pc_->CreateDataChannel("1", &config);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001954 EXPECT_TRUE(channel != NULL);
1955 EXPECT_EQ(1, channel->id());
1956
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001957 channel = pc_->CreateDataChannel("x", &config);
1958 EXPECT_TRUE(channel == NULL);
1959
1960 config.id = cricket::kMaxSctpSid;
1961 channel = pc_->CreateDataChannel("max", &config);
1962 EXPECT_TRUE(channel != NULL);
1963 EXPECT_EQ(config.id, channel->id());
1964
1965 config.id = cricket::kMaxSctpSid + 1;
1966 channel = pc_->CreateDataChannel("x", &config);
1967 EXPECT_TRUE(channel == NULL);
1968}
1969
deadbeefab9b2d12015-10-14 11:33:11 -07001970// Verifies that duplicated label is allowed for SCTP data channel.
1971TEST_F(PeerConnectionInterfaceTest, SctpDuplicatedLabelAllowed) {
1972 FakeConstraints constraints;
1973 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
1974 true);
1975 CreatePeerConnection(&constraints);
1976
1977 std::string label = "test";
zhihuang9763d562016-08-05 11:14:50 -07001978 rtc::scoped_refptr<DataChannelInterface> channel =
deadbeefab9b2d12015-10-14 11:33:11 -07001979 pc_->CreateDataChannel(label, nullptr);
1980 EXPECT_NE(channel, nullptr);
1981
zhihuang9763d562016-08-05 11:14:50 -07001982 rtc::scoped_refptr<DataChannelInterface> dup_channel =
deadbeefab9b2d12015-10-14 11:33:11 -07001983 pc_->CreateDataChannel(label, nullptr);
1984 EXPECT_NE(dup_channel, nullptr);
1985}
1986
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001987// This test verifies that OnRenegotiationNeeded is fired for every new RTP
1988// DataChannel.
1989TEST_F(PeerConnectionInterfaceTest, RenegotiationNeededForNewRtpDataChannel) {
1990 FakeConstraints constraints;
1991 constraints.SetAllowRtpDataChannels();
1992 CreatePeerConnection(&constraints);
1993
zhihuang9763d562016-08-05 11:14:50 -07001994 rtc::scoped_refptr<DataChannelInterface> dc1 =
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001995 pc_->CreateDataChannel("test1", NULL);
1996 EXPECT_TRUE(observer_.renegotiation_needed_);
1997 observer_.renegotiation_needed_ = false;
1998
zhihuang9763d562016-08-05 11:14:50 -07001999 rtc::scoped_refptr<DataChannelInterface> dc2 =
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00002000 pc_->CreateDataChannel("test2", NULL);
2001 EXPECT_TRUE(observer_.renegotiation_needed_);
2002}
2003
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002004// This test that a data channel closes when a PeerConnection is deleted/closed.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002005TEST_F(PeerConnectionInterfaceTest, DataChannelCloseWhenPeerConnectionClose) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002006 FakeConstraints constraints;
2007 constraints.SetAllowRtpDataChannels();
2008 CreatePeerConnection(&constraints);
2009
zhihuang9763d562016-08-05 11:14:50 -07002010 rtc::scoped_refptr<DataChannelInterface> data1 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002011 pc_->CreateDataChannel("test1", NULL);
zhihuang9763d562016-08-05 11:14:50 -07002012 rtc::scoped_refptr<DataChannelInterface> data2 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002013 pc_->CreateDataChannel("test2", NULL);
2014 ASSERT_TRUE(data1 != NULL);
kwibergd1fe2812016-04-27 06:47:29 -07002015 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002016 new MockDataChannelObserver(data1));
kwibergd1fe2812016-04-27 06:47:29 -07002017 std::unique_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002018 new MockDataChannelObserver(data2));
2019
2020 CreateOfferReceiveAnswer();
2021 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
2022 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
2023
2024 ReleasePeerConnection();
2025 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
2026 EXPECT_EQ(DataChannelInterface::kClosed, data2->state());
2027}
2028
2029// This test that data channels can be rejected in an answer.
2030TEST_F(PeerConnectionInterfaceTest, TestRejectDataChannelInAnswer) {
2031 FakeConstraints constraints;
2032 constraints.SetAllowRtpDataChannels();
2033 CreatePeerConnection(&constraints);
2034
zhihuang9763d562016-08-05 11:14:50 -07002035 rtc::scoped_refptr<DataChannelInterface> offer_channel(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002036 pc_->CreateDataChannel("offer_channel", NULL));
2037
2038 CreateOfferAsLocalDescription();
2039
2040 // Create an answer where the m-line for data channels are rejected.
2041 std::string sdp;
2042 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
2043 webrtc::JsepSessionDescription* answer = new webrtc::JsepSessionDescription(
2044 SessionDescriptionInterface::kAnswer);
2045 EXPECT_TRUE(answer->Initialize(sdp, NULL));
2046 cricket::ContentInfo* data_info =
2047 answer->description()->GetContentByName("data");
2048 data_info->rejected = true;
2049
2050 DoSetRemoteDescription(answer);
2051 EXPECT_EQ(DataChannelInterface::kClosed, offer_channel->state());
2052}
2053
2054// Test that we can create a session description from an SDP string from
2055// FireFox, use it as a remote session description, generate an answer and use
2056// the answer as a local description.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002057TEST_F(PeerConnectionInterfaceTest, ReceiveFireFoxOffer) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002058 FakeConstraints constraints;
2059 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2060 true);
2061 CreatePeerConnection(&constraints);
2062 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
2063 SessionDescriptionInterface* desc =
2064 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
jbauchfabe2c92015-07-16 13:43:14 -07002065 webrtc::kFireFoxSdpOffer, nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002066 EXPECT_TRUE(DoSetSessionDescription(desc, false));
2067 CreateAnswerAsLocalDescription();
2068 ASSERT_TRUE(pc_->local_description() != NULL);
2069 ASSERT_TRUE(pc_->remote_description() != NULL);
2070
2071 const cricket::ContentInfo* content =
2072 cricket::GetFirstAudioContent(pc_->local_description()->description());
2073 ASSERT_TRUE(content != NULL);
2074 EXPECT_FALSE(content->rejected);
2075
2076 content =
2077 cricket::GetFirstVideoContent(pc_->local_description()->description());
2078 ASSERT_TRUE(content != NULL);
2079 EXPECT_FALSE(content->rejected);
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00002080#ifdef HAVE_SCTP
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002081 content =
2082 cricket::GetFirstDataContent(pc_->local_description()->description());
2083 ASSERT_TRUE(content != NULL);
2084 EXPECT_TRUE(content->rejected);
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00002085#endif
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002086}
2087
deadbeef8662f942017-01-20 21:20:51 -08002088// Test that an offer can be received which offers DTLS with SDES fallback.
2089// Regression test for issue:
2090// https://bugs.chromium.org/p/webrtc/issues/detail?id=6972
2091TEST_F(PeerConnectionInterfaceTest, ReceiveDtlsSdesFallbackOffer) {
2092 FakeConstraints constraints;
2093 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2094 true);
2095 CreatePeerConnection(&constraints);
2096 // Wait for fake certificate to be generated. Previously, this is what caused
2097 // the "a=crypto" lines to be rejected.
2098 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
2099 ASSERT_NE(nullptr, fake_certificate_generator_);
2100 EXPECT_EQ_WAIT(1, fake_certificate_generator_->generated_certificates(),
2101 kTimeout);
2102 SessionDescriptionInterface* desc = webrtc::CreateSessionDescription(
2103 SessionDescriptionInterface::kOffer, kDtlsSdesFallbackSdp, nullptr);
2104 EXPECT_TRUE(DoSetSessionDescription(desc, false));
2105 CreateAnswerAsLocalDescription();
2106}
2107
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002108// Test that we can create an audio only offer and receive an answer with a
2109// limited set of audio codecs and receive an updated offer with more audio
2110// codecs, where the added codecs are not supported.
2111TEST_F(PeerConnectionInterfaceTest, ReceiveUpdatedAudioOfferWithBadCodecs) {
deadbeef293e9262017-01-11 12:28:30 -08002112 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002113 AddVoiceStream("audio_label");
2114 CreateOfferAsLocalDescription();
2115
2116 SessionDescriptionInterface* answer =
2117 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
jbauchfabe2c92015-07-16 13:43:14 -07002118 webrtc::kAudioSdp, nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002119 EXPECT_TRUE(DoSetSessionDescription(answer, false));
2120
2121 SessionDescriptionInterface* updated_offer =
2122 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
jbauchfabe2c92015-07-16 13:43:14 -07002123 webrtc::kAudioSdpWithUnsupportedCodecs,
2124 nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002125 EXPECT_TRUE(DoSetSessionDescription(updated_offer, false));
2126 CreateAnswerAsLocalDescription();
2127}
2128
deadbeefc80741f2015-10-22 13:14:45 -07002129// Test that if we're receiving (but not sending) a track, subsequent offers
2130// will have m-lines with a=recvonly.
2131TEST_F(PeerConnectionInterfaceTest, CreateSubsequentRecvOnlyOffer) {
2132 FakeConstraints constraints;
2133 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2134 true);
2135 CreatePeerConnection(&constraints);
2136 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2137 CreateAnswerAsLocalDescription();
2138
2139 // At this point we should be receiving stream 1, but not sending anything.
2140 // A new offer should be recvonly.
kwibergd1fe2812016-04-27 06:47:29 -07002141 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefc80741f2015-10-22 13:14:45 -07002142 DoCreateOffer(&offer, nullptr);
2143
2144 const cricket::ContentInfo* video_content =
2145 cricket::GetFirstVideoContent(offer->description());
2146 const cricket::VideoContentDescription* video_desc =
2147 static_cast<const cricket::VideoContentDescription*>(
2148 video_content->description);
2149 ASSERT_EQ(cricket::MD_RECVONLY, video_desc->direction());
2150
2151 const cricket::ContentInfo* audio_content =
2152 cricket::GetFirstAudioContent(offer->description());
2153 const cricket::AudioContentDescription* audio_desc =
2154 static_cast<const cricket::AudioContentDescription*>(
2155 audio_content->description);
2156 ASSERT_EQ(cricket::MD_RECVONLY, audio_desc->direction());
2157}
2158
2159// Test that if we're receiving (but not sending) a track, and the
2160// offerToReceiveVideo/offerToReceiveAudio constraints are explicitly set to
2161// false, the generated m-lines will be a=inactive.
2162TEST_F(PeerConnectionInterfaceTest, CreateSubsequentInactiveOffer) {
2163 FakeConstraints constraints;
2164 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2165 true);
2166 CreatePeerConnection(&constraints);
2167 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2168 CreateAnswerAsLocalDescription();
2169
2170 // At this point we should be receiving stream 1, but not sending anything.
2171 // A new offer would be recvonly, but we'll set the "no receive" constraints
2172 // to make it inactive.
kwibergd1fe2812016-04-27 06:47:29 -07002173 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefc80741f2015-10-22 13:14:45 -07002174 FakeConstraints offer_constraints;
2175 offer_constraints.AddMandatory(
2176 webrtc::MediaConstraintsInterface::kOfferToReceiveVideo, false);
2177 offer_constraints.AddMandatory(
2178 webrtc::MediaConstraintsInterface::kOfferToReceiveAudio, false);
2179 DoCreateOffer(&offer, &offer_constraints);
2180
2181 const cricket::ContentInfo* video_content =
2182 cricket::GetFirstVideoContent(offer->description());
2183 const cricket::VideoContentDescription* video_desc =
2184 static_cast<const cricket::VideoContentDescription*>(
2185 video_content->description);
2186 ASSERT_EQ(cricket::MD_INACTIVE, video_desc->direction());
2187
2188 const cricket::ContentInfo* audio_content =
2189 cricket::GetFirstAudioContent(offer->description());
2190 const cricket::AudioContentDescription* audio_desc =
2191 static_cast<const cricket::AudioContentDescription*>(
2192 audio_content->description);
2193 ASSERT_EQ(cricket::MD_INACTIVE, audio_desc->direction());
2194}
2195
deadbeef653b8e02015-11-11 12:55:10 -08002196// Test that we can use SetConfiguration to change the ICE servers of the
2197// PortAllocator.
2198TEST_F(PeerConnectionInterfaceTest, SetConfigurationChangesIceServers) {
2199 CreatePeerConnection();
2200
2201 PeerConnectionInterface::RTCConfiguration config;
2202 PeerConnectionInterface::IceServer server;
2203 server.uri = "stun:test_hostname";
2204 config.servers.push_back(server);
2205 EXPECT_TRUE(pc_->SetConfiguration(config));
2206
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002207 EXPECT_EQ(1u, port_allocator_->stun_servers().size());
2208 EXPECT_EQ("test_hostname",
2209 port_allocator_->stun_servers().begin()->hostname());
deadbeef653b8e02015-11-11 12:55:10 -08002210}
2211
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002212TEST_F(PeerConnectionInterfaceTest, SetConfigurationChangesCandidateFilter) {
2213 CreatePeerConnection();
2214 PeerConnectionInterface::RTCConfiguration config;
2215 config.type = PeerConnectionInterface::kRelay;
2216 EXPECT_TRUE(pc_->SetConfiguration(config));
2217 EXPECT_EQ(cricket::CF_RELAY, port_allocator_->candidate_filter());
2218}
2219
deadbeef293e9262017-01-11 12:28:30 -08002220TEST_F(PeerConnectionInterfaceTest, SetConfigurationChangesPruneTurnPortsFlag) {
2221 PeerConnectionInterface::RTCConfiguration config;
2222 config.prune_turn_ports = false;
2223 CreatePeerConnection(config, nullptr);
2224 EXPECT_FALSE(port_allocator_->prune_turn_ports());
2225
2226 config.prune_turn_ports = true;
2227 EXPECT_TRUE(pc_->SetConfiguration(config));
2228 EXPECT_TRUE(port_allocator_->prune_turn_ports());
2229}
2230
skvladd1f5fda2017-02-03 16:54:05 -08002231// Test that the ice check interval can be changed. This does not verify that
2232// the setting makes it all the way to P2PTransportChannel, as that would
2233// require a very complex set of mocks.
2234TEST_F(PeerConnectionInterfaceTest, SetConfigurationChangesIceCheckInterval) {
2235 PeerConnectionInterface::RTCConfiguration config;
2236 config.ice_check_min_interval = rtc::Optional<int>();
2237 CreatePeerConnection(config, nullptr);
2238 config.ice_check_min_interval = rtc::Optional<int>(100);
2239 EXPECT_TRUE(pc_->SetConfiguration(config));
2240 PeerConnectionInterface::RTCConfiguration new_config =
2241 pc_->GetConfiguration();
2242 EXPECT_EQ(new_config.ice_check_min_interval, rtc::Optional<int>(100));
2243}
2244
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002245// Test that when SetConfiguration changes both the pool size and other
2246// attributes, the pooled session is created with the updated attributes.
2247TEST_F(PeerConnectionInterfaceTest,
2248 SetConfigurationCreatesPooledSessionCorrectly) {
2249 CreatePeerConnection();
2250 PeerConnectionInterface::RTCConfiguration config;
2251 config.ice_candidate_pool_size = 1;
2252 PeerConnectionInterface::IceServer server;
2253 server.uri = kStunAddressOnly;
2254 config.servers.push_back(server);
2255 config.type = PeerConnectionInterface::kRelay;
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07002256 EXPECT_TRUE(pc_->SetConfiguration(config));
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002257
2258 const cricket::FakePortAllocatorSession* session =
2259 static_cast<const cricket::FakePortAllocatorSession*>(
2260 port_allocator_->GetPooledSession());
2261 ASSERT_NE(nullptr, session);
2262 EXPECT_EQ(1UL, session->stun_servers().size());
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002263}
2264
deadbeef293e9262017-01-11 12:28:30 -08002265// Test that after SetLocalDescription, changing the pool size is not allowed,
2266// and an invalid modification error is returned.
deadbeef6de92f92016-12-12 18:49:32 -08002267TEST_F(PeerConnectionInterfaceTest,
2268 CantChangePoolSizeAfterSetLocalDescription) {
2269 CreatePeerConnection();
2270 // Start by setting a size of 1.
2271 PeerConnectionInterface::RTCConfiguration config;
2272 config.ice_candidate_pool_size = 1;
2273 EXPECT_TRUE(pc_->SetConfiguration(config));
2274
2275 // Set remote offer; can still change pool size at this point.
2276 CreateOfferAsRemoteDescription();
2277 config.ice_candidate_pool_size = 2;
2278 EXPECT_TRUE(pc_->SetConfiguration(config));
2279
2280 // Set local answer; now it's too late.
2281 CreateAnswerAsLocalDescription();
2282 config.ice_candidate_pool_size = 3;
deadbeef293e9262017-01-11 12:28:30 -08002283 RTCError error;
2284 EXPECT_FALSE(pc_->SetConfiguration(config, &error));
2285 EXPECT_EQ(RTCErrorType::INVALID_MODIFICATION, error.type());
2286}
2287
deadbeef42a42632017-03-10 15:18:00 -08002288// Test that after setting an answer, extra pooled sessions are discarded. The
2289// ICE candidate pool is only intended to be used for the first offer/answer.
2290TEST_F(PeerConnectionInterfaceTest,
2291 ExtraPooledSessionsDiscardedAfterApplyingAnswer) {
2292 CreatePeerConnection();
2293
2294 // Set a larger-than-necessary size.
2295 PeerConnectionInterface::RTCConfiguration config;
2296 config.ice_candidate_pool_size = 4;
2297 EXPECT_TRUE(pc_->SetConfiguration(config));
2298
2299 // Do offer/answer.
2300 CreateOfferAsRemoteDescription();
2301 CreateAnswerAsLocalDescription();
2302
2303 // Expect no pooled sessions to be left.
2304 const cricket::PortAllocatorSession* session =
2305 port_allocator_->GetPooledSession();
2306 EXPECT_EQ(nullptr, session);
2307}
2308
2309// After Close is called, pooled candidates should be discarded so as to not
2310// waste network resources.
2311TEST_F(PeerConnectionInterfaceTest, PooledSessionsDiscardedAfterClose) {
2312 CreatePeerConnection();
2313
2314 PeerConnectionInterface::RTCConfiguration config;
2315 config.ice_candidate_pool_size = 3;
2316 EXPECT_TRUE(pc_->SetConfiguration(config));
2317 pc_->Close();
2318
2319 // Expect no pooled sessions to be left.
2320 const cricket::PortAllocatorSession* session =
2321 port_allocator_->GetPooledSession();
2322 EXPECT_EQ(nullptr, session);
2323}
2324
deadbeef293e9262017-01-11 12:28:30 -08002325// Test that SetConfiguration returns an invalid modification error if
2326// modifying a field in the configuration that isn't allowed to be modified.
2327TEST_F(PeerConnectionInterfaceTest,
2328 SetConfigurationReturnsInvalidModificationError) {
2329 PeerConnectionInterface::RTCConfiguration config;
2330 config.bundle_policy = PeerConnectionInterface::kBundlePolicyBalanced;
2331 config.rtcp_mux_policy = PeerConnectionInterface::kRtcpMuxPolicyNegotiate;
2332 config.continual_gathering_policy = PeerConnectionInterface::GATHER_ONCE;
2333 CreatePeerConnection(config, nullptr);
2334
2335 PeerConnectionInterface::RTCConfiguration modified_config = config;
2336 modified_config.bundle_policy =
2337 PeerConnectionInterface::kBundlePolicyMaxBundle;
2338 RTCError error;
2339 EXPECT_FALSE(pc_->SetConfiguration(modified_config, &error));
2340 EXPECT_EQ(RTCErrorType::INVALID_MODIFICATION, error.type());
2341
2342 modified_config = config;
2343 modified_config.rtcp_mux_policy =
2344 PeerConnectionInterface::kRtcpMuxPolicyRequire;
2345 error.set_type(RTCErrorType::NONE);
2346 EXPECT_FALSE(pc_->SetConfiguration(modified_config, &error));
2347 EXPECT_EQ(RTCErrorType::INVALID_MODIFICATION, error.type());
2348
2349 modified_config = config;
2350 modified_config.continual_gathering_policy =
2351 PeerConnectionInterface::GATHER_CONTINUALLY;
2352 error.set_type(RTCErrorType::NONE);
2353 EXPECT_FALSE(pc_->SetConfiguration(modified_config, &error));
2354 EXPECT_EQ(RTCErrorType::INVALID_MODIFICATION, error.type());
2355}
2356
2357// Test that SetConfiguration returns a range error if the candidate pool size
2358// is negative or larger than allowed by the spec.
2359TEST_F(PeerConnectionInterfaceTest,
2360 SetConfigurationReturnsRangeErrorForBadCandidatePoolSize) {
2361 PeerConnectionInterface::RTCConfiguration config;
2362 CreatePeerConnection(config, nullptr);
2363
2364 config.ice_candidate_pool_size = -1;
2365 RTCError error;
2366 EXPECT_FALSE(pc_->SetConfiguration(config, &error));
2367 EXPECT_EQ(RTCErrorType::INVALID_RANGE, error.type());
2368
2369 config.ice_candidate_pool_size = INT_MAX;
2370 error.set_type(RTCErrorType::NONE);
2371 EXPECT_FALSE(pc_->SetConfiguration(config, &error));
2372 EXPECT_EQ(RTCErrorType::INVALID_RANGE, error.type());
2373}
2374
2375// Test that SetConfiguration returns a syntax error if parsing an ICE server
2376// URL failed.
2377TEST_F(PeerConnectionInterfaceTest,
2378 SetConfigurationReturnsSyntaxErrorFromBadIceUrls) {
2379 PeerConnectionInterface::RTCConfiguration config;
2380 CreatePeerConnection(config, nullptr);
2381
2382 PeerConnectionInterface::IceServer bad_server;
2383 bad_server.uri = "stunn:www.example.com";
2384 config.servers.push_back(bad_server);
2385 RTCError error;
2386 EXPECT_FALSE(pc_->SetConfiguration(config, &error));
2387 EXPECT_EQ(RTCErrorType::SYNTAX_ERROR, error.type());
2388}
2389
2390// Test that SetConfiguration returns an invalid parameter error if a TURN
2391// IceServer is missing a username or password.
2392TEST_F(PeerConnectionInterfaceTest,
2393 SetConfigurationReturnsInvalidParameterIfCredentialsMissing) {
2394 PeerConnectionInterface::RTCConfiguration config;
2395 CreatePeerConnection(config, nullptr);
2396
2397 PeerConnectionInterface::IceServer bad_server;
2398 bad_server.uri = "turn:www.example.com";
2399 // Missing password.
2400 bad_server.username = "foo";
2401 config.servers.push_back(bad_server);
2402 RTCError error;
2403 EXPECT_FALSE(pc_->SetConfiguration(config, &error));
2404 EXPECT_EQ(RTCErrorType::INVALID_PARAMETER, error.type());
deadbeef6de92f92016-12-12 18:49:32 -08002405}
2406
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002407// Test that PeerConnection::Close changes the states to closed and all remote
2408// tracks change state to ended.
2409TEST_F(PeerConnectionInterfaceTest, CloseAndTestStreamsAndStates) {
2410 // Initialize a PeerConnection and negotiate local and remote session
2411 // description.
2412 InitiateCall();
2413 ASSERT_EQ(1u, pc_->local_streams()->count());
2414 ASSERT_EQ(1u, pc_->remote_streams()->count());
2415
2416 pc_->Close();
2417
2418 EXPECT_EQ(PeerConnectionInterface::kClosed, pc_->signaling_state());
2419 EXPECT_EQ(PeerConnectionInterface::kIceConnectionClosed,
2420 pc_->ice_connection_state());
2421 EXPECT_EQ(PeerConnectionInterface::kIceGatheringComplete,
2422 pc_->ice_gathering_state());
2423
2424 EXPECT_EQ(1u, pc_->local_streams()->count());
2425 EXPECT_EQ(1u, pc_->remote_streams()->count());
2426
zhihuang9763d562016-08-05 11:14:50 -07002427 rtc::scoped_refptr<MediaStreamInterface> remote_stream =
2428 pc_->remote_streams()->at(0);
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002429 // Track state may be updated asynchronously.
perkjd61bf802016-03-24 03:16:19 -07002430 EXPECT_EQ_WAIT(MediaStreamTrackInterface::kEnded,
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002431 remote_stream->GetAudioTracks()[0]->state(), kTimeout);
2432 EXPECT_EQ_WAIT(MediaStreamTrackInterface::kEnded,
2433 remote_stream->GetVideoTracks()[0]->state(), kTimeout);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002434}
2435
2436// Test that PeerConnection methods fails gracefully after
2437// PeerConnection::Close has been called.
2438TEST_F(PeerConnectionInterfaceTest, CloseAndTestMethods) {
deadbeef293e9262017-01-11 12:28:30 -08002439 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002440 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
2441 CreateOfferAsRemoteDescription();
2442 CreateAnswerAsLocalDescription();
2443
2444 ASSERT_EQ(1u, pc_->local_streams()->count());
zhihuang9763d562016-08-05 11:14:50 -07002445 rtc::scoped_refptr<MediaStreamInterface> local_stream =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002446 pc_->local_streams()->at(0);
2447
2448 pc_->Close();
2449
2450 pc_->RemoveStream(local_stream);
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +00002451 EXPECT_FALSE(pc_->AddStream(local_stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002452
2453 ASSERT_FALSE(local_stream->GetAudioTracks().empty());
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002454 rtc::scoped_refptr<webrtc::DtmfSenderInterface> dtmf_sender(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002455 pc_->CreateDtmfSender(local_stream->GetAudioTracks()[0]));
wu@webrtc.org66037362013-08-13 00:09:35 +00002456 EXPECT_TRUE(NULL == dtmf_sender); // local stream has been removed.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002457
2458 EXPECT_TRUE(pc_->CreateDataChannel("test", NULL) == NULL);
2459
2460 EXPECT_TRUE(pc_->local_description() != NULL);
2461 EXPECT_TRUE(pc_->remote_description() != NULL);
2462
kwibergd1fe2812016-04-27 06:47:29 -07002463 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07002464 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
kwibergd1fe2812016-04-27 06:47:29 -07002465 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -07002466 EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002467
2468 std::string sdp;
2469 ASSERT_TRUE(pc_->remote_description()->ToString(&sdp));
2470 SessionDescriptionInterface* remote_offer =
2471 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
2472 sdp, NULL);
2473 EXPECT_FALSE(DoSetRemoteDescription(remote_offer));
2474
2475 ASSERT_TRUE(pc_->local_description()->ToString(&sdp));
2476 SessionDescriptionInterface* local_offer =
2477 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
2478 sdp, NULL);
2479 EXPECT_FALSE(DoSetLocalDescription(local_offer));
2480}
2481
2482// Test that GetStats can still be called after PeerConnection::Close.
2483TEST_F(PeerConnectionInterfaceTest, CloseAndGetStats) {
2484 InitiateCall();
2485 pc_->Close();
2486 DoGetStats(NULL);
2487}
deadbeefab9b2d12015-10-14 11:33:11 -07002488
2489// NOTE: The series of tests below come from what used to be
2490// mediastreamsignaling_unittest.cc, and are mostly aimed at testing that
2491// setting a remote or local description has the expected effects.
2492
2493// This test verifies that the remote MediaStreams corresponding to a received
2494// SDP string is created. In this test the two separate MediaStreams are
2495// signaled.
2496TEST_F(PeerConnectionInterfaceTest, UpdateRemoteStreams) {
2497 FakeConstraints constraints;
2498 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2499 true);
2500 CreatePeerConnection(&constraints);
2501 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2502
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002503 rtc::scoped_refptr<StreamCollection> reference(CreateStreamCollection(1, 1));
deadbeefab9b2d12015-10-14 11:33:11 -07002504 EXPECT_TRUE(
2505 CompareStreamCollections(observer_.remote_streams(), reference.get()));
2506 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2507 EXPECT_TRUE(remote_stream->GetVideoTracks()[0]->GetSource() != nullptr);
2508
2509 // Create a session description based on another SDP with another
2510 // MediaStream.
2511 CreateAndSetRemoteOffer(kSdpStringWithStream1And2);
2512
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002513 rtc::scoped_refptr<StreamCollection> reference2(CreateStreamCollection(2, 1));
deadbeefab9b2d12015-10-14 11:33:11 -07002514 EXPECT_TRUE(
2515 CompareStreamCollections(observer_.remote_streams(), reference2.get()));
2516}
2517
2518// This test verifies that when remote tracks are added/removed from SDP, the
2519// created remote streams are updated appropriately.
2520TEST_F(PeerConnectionInterfaceTest,
2521 AddRemoveTrackFromExistingRemoteMediaStream) {
2522 FakeConstraints constraints;
2523 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2524 true);
2525 CreatePeerConnection(&constraints);
kwibergd1fe2812016-04-27 06:47:29 -07002526 std::unique_ptr<SessionDescriptionInterface> desc_ms1 =
kwiberg2bbff992016-03-16 11:03:04 -07002527 CreateSessionDescriptionAndReference(1, 1);
deadbeefab9b2d12015-10-14 11:33:11 -07002528 EXPECT_TRUE(DoSetRemoteDescription(desc_ms1.release()));
2529 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
2530 reference_collection_));
2531
2532 // Add extra audio and video tracks to the same MediaStream.
kwibergd1fe2812016-04-27 06:47:29 -07002533 std::unique_ptr<SessionDescriptionInterface> desc_ms1_two_tracks =
kwiberg2bbff992016-03-16 11:03:04 -07002534 CreateSessionDescriptionAndReference(2, 2);
deadbeefab9b2d12015-10-14 11:33:11 -07002535 EXPECT_TRUE(DoSetRemoteDescription(desc_ms1_two_tracks.release()));
2536 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
2537 reference_collection_));
zhihuang9763d562016-08-05 11:14:50 -07002538 rtc::scoped_refptr<AudioTrackInterface> audio_track2 =
perkjd61bf802016-03-24 03:16:19 -07002539 observer_.remote_streams()->at(0)->GetAudioTracks()[1];
2540 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, audio_track2->state());
zhihuang9763d562016-08-05 11:14:50 -07002541 rtc::scoped_refptr<VideoTrackInterface> video_track2 =
perkjd61bf802016-03-24 03:16:19 -07002542 observer_.remote_streams()->at(0)->GetVideoTracks()[1];
2543 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, video_track2->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002544
2545 // Remove the extra audio and video tracks.
kwibergd1fe2812016-04-27 06:47:29 -07002546 std::unique_ptr<SessionDescriptionInterface> desc_ms2 =
kwiberg2bbff992016-03-16 11:03:04 -07002547 CreateSessionDescriptionAndReference(1, 1);
perkjd61bf802016-03-24 03:16:19 -07002548 MockTrackObserver audio_track_observer(audio_track2);
2549 MockTrackObserver video_track_observer(video_track2);
2550
2551 EXPECT_CALL(audio_track_observer, OnChanged()).Times(Exactly(1));
2552 EXPECT_CALL(video_track_observer, OnChanged()).Times(Exactly(1));
deadbeefab9b2d12015-10-14 11:33:11 -07002553 EXPECT_TRUE(DoSetRemoteDescription(desc_ms2.release()));
2554 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
2555 reference_collection_));
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002556 // Track state may be updated asynchronously.
perkjd61bf802016-03-24 03:16:19 -07002557 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002558 audio_track2->state(), kTimeout);
2559 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
2560 video_track2->state(), kTimeout);
deadbeefab9b2d12015-10-14 11:33:11 -07002561}
2562
2563// This tests that remote tracks are ended if a local session description is set
2564// that rejects the media content type.
2565TEST_F(PeerConnectionInterfaceTest, RejectMediaContent) {
2566 FakeConstraints constraints;
2567 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2568 true);
2569 CreatePeerConnection(&constraints);
2570 // First create and set a remote offer, then reject its video content in our
2571 // answer.
2572 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2573 ASSERT_EQ(1u, observer_.remote_streams()->count());
2574 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2575 ASSERT_EQ(1u, remote_stream->GetVideoTracks().size());
2576 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2577
2578 rtc::scoped_refptr<webrtc::VideoTrackInterface> remote_video =
2579 remote_stream->GetVideoTracks()[0];
2580 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, remote_video->state());
2581 rtc::scoped_refptr<webrtc::AudioTrackInterface> remote_audio =
2582 remote_stream->GetAudioTracks()[0];
2583 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, remote_audio->state());
2584
kwibergd1fe2812016-04-27 06:47:29 -07002585 std::unique_ptr<SessionDescriptionInterface> local_answer;
kwiberg2bbff992016-03-16 11:03:04 -07002586 EXPECT_TRUE(DoCreateAnswer(&local_answer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07002587 cricket::ContentInfo* video_info =
2588 local_answer->description()->GetContentByName("video");
2589 video_info->rejected = true;
2590 EXPECT_TRUE(DoSetLocalDescription(local_answer.release()));
2591 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kEnded, remote_video->state());
2592 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, remote_audio->state());
2593
2594 // Now create an offer where we reject both video and audio.
kwibergd1fe2812016-04-27 06:47:29 -07002595 std::unique_ptr<SessionDescriptionInterface> local_offer;
kwiberg2bbff992016-03-16 11:03:04 -07002596 EXPECT_TRUE(DoCreateOffer(&local_offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07002597 video_info = local_offer->description()->GetContentByName("video");
2598 ASSERT_TRUE(video_info != nullptr);
2599 video_info->rejected = true;
2600 cricket::ContentInfo* audio_info =
2601 local_offer->description()->GetContentByName("audio");
2602 ASSERT_TRUE(audio_info != nullptr);
2603 audio_info->rejected = true;
2604 EXPECT_TRUE(DoSetLocalDescription(local_offer.release()));
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002605 // Track state may be updated asynchronously.
perkjd61bf802016-03-24 03:16:19 -07002606 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002607 remote_audio->state(), kTimeout);
2608 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
2609 remote_video->state(), kTimeout);
deadbeefab9b2d12015-10-14 11:33:11 -07002610}
2611
2612// This tests that we won't crash if the remote track has been removed outside
2613// of PeerConnection and then PeerConnection tries to reject the track.
2614TEST_F(PeerConnectionInterfaceTest, RemoveTrackThenRejectMediaContent) {
2615 FakeConstraints constraints;
2616 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2617 true);
2618 CreatePeerConnection(&constraints);
2619 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2620 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2621 remote_stream->RemoveTrack(remote_stream->GetVideoTracks()[0]);
2622 remote_stream->RemoveTrack(remote_stream->GetAudioTracks()[0]);
2623
kwibergd1fe2812016-04-27 06:47:29 -07002624 std::unique_ptr<SessionDescriptionInterface> local_answer(
deadbeefab9b2d12015-10-14 11:33:11 -07002625 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
2626 kSdpStringWithStream1, nullptr));
2627 cricket::ContentInfo* video_info =
2628 local_answer->description()->GetContentByName("video");
2629 video_info->rejected = true;
2630 cricket::ContentInfo* audio_info =
2631 local_answer->description()->GetContentByName("audio");
2632 audio_info->rejected = true;
2633 EXPECT_TRUE(DoSetLocalDescription(local_answer.release()));
2634
2635 // No crash is a pass.
2636}
2637
deadbeef5e97fb52015-10-15 12:49:08 -07002638// This tests that if a recvonly remote description is set, no remote streams
2639// will be created, even if the description contains SSRCs/MSIDs.
2640// See: https://code.google.com/p/webrtc/issues/detail?id=5054
2641TEST_F(PeerConnectionInterfaceTest, RecvonlyDescriptionDoesntCreateStream) {
2642 FakeConstraints constraints;
2643 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2644 true);
2645 CreatePeerConnection(&constraints);
2646
2647 std::string recvonly_offer = kSdpStringWithStream1;
2648 rtc::replace_substrs(kSendrecv, strlen(kSendrecv), kRecvonly,
2649 strlen(kRecvonly), &recvonly_offer);
2650 CreateAndSetRemoteOffer(recvonly_offer);
2651
2652 EXPECT_EQ(0u, observer_.remote_streams()->count());
2653}
2654
deadbeefab9b2d12015-10-14 11:33:11 -07002655// This tests that a default MediaStream is created if a remote session
2656// description doesn't contain any streams and no MSID support.
2657// It also tests that the default stream is updated if a video m-line is added
2658// in a subsequent session description.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002659TEST_F(PeerConnectionInterfaceTest, SdpWithoutMsidCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002660 FakeConstraints constraints;
2661 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2662 true);
2663 CreatePeerConnection(&constraints);
2664 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2665
2666 ASSERT_EQ(1u, observer_.remote_streams()->count());
2667 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2668
2669 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2670 EXPECT_EQ(0u, remote_stream->GetVideoTracks().size());
2671 EXPECT_EQ("default", remote_stream->label());
2672
2673 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2674 ASSERT_EQ(1u, observer_.remote_streams()->count());
2675 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2676 EXPECT_EQ("defaulta0", remote_stream->GetAudioTracks()[0]->id());
deadbeef884f5852016-01-15 09:20:04 -08002677 EXPECT_EQ(MediaStreamTrackInterface::kLive,
2678 remote_stream->GetAudioTracks()[0]->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002679 ASSERT_EQ(1u, remote_stream->GetVideoTracks().size());
2680 EXPECT_EQ("defaultv0", remote_stream->GetVideoTracks()[0]->id());
deadbeef884f5852016-01-15 09:20:04 -08002681 EXPECT_EQ(MediaStreamTrackInterface::kLive,
2682 remote_stream->GetVideoTracks()[0]->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002683}
2684
2685// This tests that a default MediaStream is created if a remote session
2686// description doesn't contain any streams and media direction is send only.
2687TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002688 SendOnlySdpWithoutMsidCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002689 FakeConstraints constraints;
2690 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2691 true);
2692 CreatePeerConnection(&constraints);
2693 CreateAndSetRemoteOffer(kSdpStringSendOnlyWithoutStreams);
2694
2695 ASSERT_EQ(1u, observer_.remote_streams()->count());
2696 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2697
2698 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2699 EXPECT_EQ(1u, remote_stream->GetVideoTracks().size());
2700 EXPECT_EQ("default", remote_stream->label());
2701}
2702
2703// This tests that it won't crash when PeerConnection tries to remove
2704// a remote track that as already been removed from the MediaStream.
2705TEST_F(PeerConnectionInterfaceTest, RemoveAlreadyGoneRemoteStream) {
2706 FakeConstraints constraints;
2707 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2708 true);
2709 CreatePeerConnection(&constraints);
2710 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2711 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2712 remote_stream->RemoveTrack(remote_stream->GetAudioTracks()[0]);
2713 remote_stream->RemoveTrack(remote_stream->GetVideoTracks()[0]);
2714
2715 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2716
2717 // No crash is a pass.
2718}
2719
2720// This tests that a default MediaStream is created if the remote session
2721// description doesn't contain any streams and don't contain an indication if
2722// MSID is supported.
2723TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002724 SdpWithoutMsidAndStreamsCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002725 FakeConstraints constraints;
2726 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2727 true);
2728 CreatePeerConnection(&constraints);
2729 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2730
2731 ASSERT_EQ(1u, observer_.remote_streams()->count());
2732 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2733 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2734 EXPECT_EQ(1u, remote_stream->GetVideoTracks().size());
2735}
2736
2737// This tests that a default MediaStream is not created if the remote session
2738// description doesn't contain any streams but does support MSID.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002739TEST_F(PeerConnectionInterfaceTest, SdpWithMsidDontCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002740 FakeConstraints constraints;
2741 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2742 true);
2743 CreatePeerConnection(&constraints);
2744 CreateAndSetRemoteOffer(kSdpStringWithMsidWithoutStreams);
2745 EXPECT_EQ(0u, observer_.remote_streams()->count());
2746}
2747
deadbeefbda7e0b2015-12-08 17:13:40 -08002748// This tests that when setting a new description, the old default tracks are
2749// not destroyed and recreated.
2750// See: https://bugs.chromium.org/p/webrtc/issues/detail?id=5250
Stefan Holmer102362b2016-03-18 09:39:07 +01002751TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002752 DefaultTracksNotDestroyedAndRecreated) {
deadbeefbda7e0b2015-12-08 17:13:40 -08002753 FakeConstraints constraints;
2754 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2755 true);
2756 CreatePeerConnection(&constraints);
2757 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2758
2759 ASSERT_EQ(1u, observer_.remote_streams()->count());
2760 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2761 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2762
2763 // Set the track to "disabled", then set a new description and ensure the
2764 // track is still disabled, which ensures it hasn't been recreated.
2765 remote_stream->GetAudioTracks()[0]->set_enabled(false);
2766 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2767 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2768 EXPECT_FALSE(remote_stream->GetAudioTracks()[0]->enabled());
2769}
2770
deadbeefab9b2d12015-10-14 11:33:11 -07002771// This tests that a default MediaStream is not created if a remote session
2772// description is updated to not have any MediaStreams.
2773TEST_F(PeerConnectionInterfaceTest, VerifyDefaultStreamIsNotCreated) {
2774 FakeConstraints constraints;
2775 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2776 true);
2777 CreatePeerConnection(&constraints);
2778 CreateAndSetRemoteOffer(kSdpStringWithStream1);
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002779 rtc::scoped_refptr<StreamCollection> reference(CreateStreamCollection(1, 1));
deadbeefab9b2d12015-10-14 11:33:11 -07002780 EXPECT_TRUE(
2781 CompareStreamCollections(observer_.remote_streams(), reference.get()));
2782
2783 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2784 EXPECT_EQ(0u, observer_.remote_streams()->count());
2785}
2786
2787// This tests that an RtpSender is created when the local description is set
2788// after adding a local stream.
2789// TODO(deadbeef): This test and the one below it need to be updated when
2790// an RtpSender's lifetime isn't determined by when a local description is set.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002791TEST_F(PeerConnectionInterfaceTest, LocalDescriptionChanged) {
deadbeefab9b2d12015-10-14 11:33:11 -07002792 FakeConstraints constraints;
2793 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2794 true);
2795 CreatePeerConnection(&constraints);
deadbeefab9b2d12015-10-14 11:33:11 -07002796
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002797 // Create an offer with 1 stream with 2 tracks of each type.
2798 rtc::scoped_refptr<StreamCollection> stream_collection =
2799 CreateStreamCollection(1, 2);
2800 pc_->AddStream(stream_collection->at(0));
2801 std::unique_ptr<SessionDescriptionInterface> offer;
2802 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2803 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
deadbeefab9b2d12015-10-14 11:33:11 -07002804
deadbeefab9b2d12015-10-14 11:33:11 -07002805 auto senders = pc_->GetSenders();
2806 EXPECT_EQ(4u, senders.size());
2807 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2808 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2809 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[1]));
2810 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[1]));
2811
2812 // Remove an audio and video track.
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002813 pc_->RemoveStream(stream_collection->at(0));
2814 stream_collection = CreateStreamCollection(1, 1);
2815 pc_->AddStream(stream_collection->at(0));
2816 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2817 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
2818
deadbeefab9b2d12015-10-14 11:33:11 -07002819 senders = pc_->GetSenders();
2820 EXPECT_EQ(2u, senders.size());
2821 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2822 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2823 EXPECT_FALSE(ContainsSender(senders, kAudioTracks[1]));
2824 EXPECT_FALSE(ContainsSender(senders, kVideoTracks[1]));
2825}
2826
2827// This tests that an RtpSender is created when the local description is set
2828// before adding a local stream.
2829TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002830 AddLocalStreamAfterLocalDescriptionChanged) {
deadbeefab9b2d12015-10-14 11:33:11 -07002831 FakeConstraints constraints;
2832 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2833 true);
2834 CreatePeerConnection(&constraints);
deadbeefab9b2d12015-10-14 11:33:11 -07002835
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002836 rtc::scoped_refptr<StreamCollection> stream_collection =
2837 CreateStreamCollection(1, 2);
2838 // Add a stream to create the offer, but remove it afterwards.
2839 pc_->AddStream(stream_collection->at(0));
2840 std::unique_ptr<SessionDescriptionInterface> offer;
2841 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2842 pc_->RemoveStream(stream_collection->at(0));
deadbeefab9b2d12015-10-14 11:33:11 -07002843
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002844 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
deadbeefab9b2d12015-10-14 11:33:11 -07002845 auto senders = pc_->GetSenders();
2846 EXPECT_EQ(0u, senders.size());
2847
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002848 pc_->AddStream(stream_collection->at(0));
deadbeefab9b2d12015-10-14 11:33:11 -07002849 senders = pc_->GetSenders();
2850 EXPECT_EQ(4u, senders.size());
2851 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2852 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2853 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[1]));
2854 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[1]));
2855}
2856
2857// This tests that the expected behavior occurs if the SSRC on a local track is
2858// changed when SetLocalDescription is called.
2859TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002860 ChangeSsrcOnTrackInLocalSessionDescription) {
deadbeefab9b2d12015-10-14 11:33:11 -07002861 FakeConstraints constraints;
2862 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2863 true);
2864 CreatePeerConnection(&constraints);
deadbeefab9b2d12015-10-14 11:33:11 -07002865
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002866 rtc::scoped_refptr<StreamCollection> stream_collection =
2867 CreateStreamCollection(2, 1);
2868 pc_->AddStream(stream_collection->at(0));
2869 std::unique_ptr<SessionDescriptionInterface> offer;
2870 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2871 // Grab a copy of the offer before it gets passed into the PC.
2872 std::unique_ptr<JsepSessionDescription> modified_offer(
2873 new JsepSessionDescription(JsepSessionDescription::kOffer));
2874 modified_offer->Initialize(offer->description()->Copy(), offer->session_id(),
2875 offer->session_version());
2876 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
deadbeefab9b2d12015-10-14 11:33:11 -07002877
deadbeefab9b2d12015-10-14 11:33:11 -07002878 auto senders = pc_->GetSenders();
2879 EXPECT_EQ(2u, senders.size());
2880 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2881 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2882
2883 // Change the ssrc of the audio and video track.
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002884 cricket::MediaContentDescription* desc =
2885 cricket::GetFirstAudioContentDescription(modified_offer->description());
2886 ASSERT_TRUE(desc != NULL);
2887 for (StreamParams& stream : desc->mutable_streams()) {
2888 for (unsigned int& ssrc : stream.ssrcs) {
2889 ++ssrc;
2890 }
2891 }
deadbeefab9b2d12015-10-14 11:33:11 -07002892
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002893 desc =
2894 cricket::GetFirstVideoContentDescription(modified_offer->description());
2895 ASSERT_TRUE(desc != NULL);
2896 for (StreamParams& stream : desc->mutable_streams()) {
2897 for (unsigned int& ssrc : stream.ssrcs) {
2898 ++ssrc;
2899 }
2900 }
2901
2902 EXPECT_TRUE(DoSetLocalDescription(modified_offer.release()));
deadbeefab9b2d12015-10-14 11:33:11 -07002903 senders = pc_->GetSenders();
2904 EXPECT_EQ(2u, senders.size());
2905 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2906 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2907 // TODO(deadbeef): Once RtpSenders expose parameters, check that the SSRC
2908 // changed.
2909}
2910
2911// This tests that the expected behavior occurs if a new session description is
2912// set with the same tracks, but on a different MediaStream.
Stefan Holmer55d6e7c2016-03-17 16:26:40 +01002913TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002914 SignalSameTracksInSeparateMediaStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002915 FakeConstraints constraints;
2916 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2917 true);
2918 CreatePeerConnection(&constraints);
deadbeefab9b2d12015-10-14 11:33:11 -07002919
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002920 rtc::scoped_refptr<StreamCollection> stream_collection =
2921 CreateStreamCollection(2, 1);
2922 pc_->AddStream(stream_collection->at(0));
2923 std::unique_ptr<SessionDescriptionInterface> offer;
2924 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2925 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
deadbeefab9b2d12015-10-14 11:33:11 -07002926
deadbeefab9b2d12015-10-14 11:33:11 -07002927 auto senders = pc_->GetSenders();
2928 EXPECT_EQ(2u, senders.size());
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002929 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0], kStreams[0]));
2930 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0], kStreams[0]));
deadbeefab9b2d12015-10-14 11:33:11 -07002931
2932 // Add a new MediaStream but with the same tracks as in the first stream.
2933 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream_1(
2934 webrtc::MediaStream::Create(kStreams[1]));
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002935 stream_1->AddTrack(stream_collection->at(0)->GetVideoTracks()[0]);
2936 stream_1->AddTrack(stream_collection->at(0)->GetAudioTracks()[0]);
deadbeefab9b2d12015-10-14 11:33:11 -07002937 pc_->AddStream(stream_1);
2938
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002939 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2940 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
deadbeefab9b2d12015-10-14 11:33:11 -07002941
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002942 auto new_senders = pc_->GetSenders();
2943 // Should be the same senders as before, but with updated stream id.
2944 // Note that this behavior is subject to change in the future.
2945 // We may decide the PC should ignore existing tracks in AddStream.
2946 EXPECT_EQ(senders, new_senders);
2947 EXPECT_TRUE(ContainsSender(new_senders, kAudioTracks[0], kStreams[1]));
2948 EXPECT_TRUE(ContainsSender(new_senders, kVideoTracks[0], kStreams[1]));
deadbeefab9b2d12015-10-14 11:33:11 -07002949}
2950
zhihuang81c3a032016-11-17 12:06:24 -08002951// This tests that PeerConnectionObserver::OnAddTrack is correctly called.
2952TEST_F(PeerConnectionInterfaceTest, OnAddTrackCallback) {
2953 FakeConstraints constraints;
2954 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2955 true);
2956 CreatePeerConnection(&constraints);
2957 CreateAndSetRemoteOffer(kSdpStringWithStream1AudioTrackOnly);
2958 EXPECT_EQ(observer_.num_added_tracks_, 1);
2959 EXPECT_EQ(observer_.last_added_track_label_, kAudioTracks[0]);
2960
2961 // Create and set the updated remote SDP.
2962 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2963 EXPECT_EQ(observer_.num_added_tracks_, 2);
2964 EXPECT_EQ(observer_.last_added_track_label_, kVideoTracks[0]);
2965}
2966
deadbeefd1a38b52016-12-10 13:15:33 -08002967// Test that when SetConfiguration is called and the configuration is
2968// changing, the next offer causes an ICE restart.
2969TEST_F(PeerConnectionInterfaceTest, SetConfigurationCausingIceRetart) {
2970 PeerConnectionInterface::RTCConfiguration config;
2971 config.type = PeerConnectionInterface::kRelay;
2972 // Need to pass default constraints to prevent disabling of DTLS...
2973 FakeConstraints default_constraints;
2974 CreatePeerConnection(config, &default_constraints);
2975 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
2976
2977 // Do initial offer/answer so there's something to restart.
2978 CreateOfferAsLocalDescription();
2979 CreateAnswerAsRemoteDescription(kSdpStringWithStream1);
2980
2981 // Grab the ufrags.
2982 std::vector<std::string> initial_ufrags = GetUfrags(pc_->local_description());
2983
2984 // Change ICE policy, which should trigger an ICE restart on the next offer.
2985 config.type = PeerConnectionInterface::kAll;
2986 EXPECT_TRUE(pc_->SetConfiguration(config));
2987 CreateOfferAsLocalDescription();
2988
2989 // Grab the new ufrags.
2990 std::vector<std::string> subsequent_ufrags =
2991 GetUfrags(pc_->local_description());
2992
2993 // Sanity check.
2994 EXPECT_EQ(initial_ufrags.size(), subsequent_ufrags.size());
2995 // Check that each ufrag is different.
2996 for (int i = 0; i < static_cast<int>(initial_ufrags.size()); ++i) {
2997 EXPECT_NE(initial_ufrags[i], subsequent_ufrags[i]);
2998 }
2999}
3000
3001// Test that when SetConfiguration is called and the configuration *isn't*
3002// changing, the next offer does *not* cause an ICE restart.
3003TEST_F(PeerConnectionInterfaceTest, SetConfigurationNotCausingIceRetart) {
3004 PeerConnectionInterface::RTCConfiguration config;
3005 config.type = PeerConnectionInterface::kRelay;
3006 // Need to pass default constraints to prevent disabling of DTLS...
3007 FakeConstraints default_constraints;
3008 CreatePeerConnection(config, &default_constraints);
3009 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
3010
3011 // Do initial offer/answer so there's something to restart.
3012 CreateOfferAsLocalDescription();
3013 CreateAnswerAsRemoteDescription(kSdpStringWithStream1);
3014
3015 // Grab the ufrags.
3016 std::vector<std::string> initial_ufrags = GetUfrags(pc_->local_description());
3017
3018 // Call SetConfiguration with a config identical to what the PC was
3019 // constructed with.
3020 EXPECT_TRUE(pc_->SetConfiguration(config));
3021 CreateOfferAsLocalDescription();
3022
3023 // Grab the new ufrags.
3024 std::vector<std::string> subsequent_ufrags =
3025 GetUfrags(pc_->local_description());
3026
3027 EXPECT_EQ(initial_ufrags, subsequent_ufrags);
3028}
3029
3030// Test for a weird corner case scenario:
3031// 1. Audio/video session established.
3032// 2. SetConfiguration changes ICE config; ICE restart needed.
3033// 3. ICE restart initiated by remote peer, but only for one m= section.
3034// 4. Next createOffer should initiate an ICE restart, but only for the other
3035// m= section; it would be pointless to do an ICE restart for the m= section
3036// that was already restarted.
3037TEST_F(PeerConnectionInterfaceTest, SetConfigurationCausingPartialIceRestart) {
3038 PeerConnectionInterface::RTCConfiguration config;
3039 config.type = PeerConnectionInterface::kRelay;
3040 // Need to pass default constraints to prevent disabling of DTLS...
3041 FakeConstraints default_constraints;
3042 CreatePeerConnection(config, &default_constraints);
3043 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
3044
3045 // Do initial offer/answer so there's something to restart.
3046 CreateOfferAsLocalDescription();
3047 CreateAnswerAsRemoteDescription(kSdpStringWithStream1);
3048
3049 // Change ICE policy, which should set the "needs-ice-restart" flag.
3050 config.type = PeerConnectionInterface::kAll;
3051 EXPECT_TRUE(pc_->SetConfiguration(config));
3052
3053 // Do ICE restart for the first m= section, initiated by remote peer.
3054 webrtc::JsepSessionDescription* remote_offer =
3055 new webrtc::JsepSessionDescription(SessionDescriptionInterface::kOffer);
3056 EXPECT_TRUE(remote_offer->Initialize(kSdpStringWithStream1, nullptr));
3057 remote_offer->description()->transport_infos()[0].description.ice_ufrag =
3058 "modified";
3059 EXPECT_TRUE(DoSetRemoteDescription(remote_offer));
3060 CreateAnswerAsLocalDescription();
3061
3062 // Grab the ufrags.
3063 std::vector<std::string> initial_ufrags = GetUfrags(pc_->local_description());
3064 ASSERT_EQ(2, initial_ufrags.size());
3065
3066 // Create offer and grab the new ufrags.
3067 CreateOfferAsLocalDescription();
3068 std::vector<std::string> subsequent_ufrags =
3069 GetUfrags(pc_->local_description());
3070 ASSERT_EQ(2, subsequent_ufrags.size());
3071
3072 // Ensure that only the ufrag for the second m= section changed.
3073 EXPECT_EQ(initial_ufrags[0], subsequent_ufrags[0]);
3074 EXPECT_NE(initial_ufrags[1], subsequent_ufrags[1]);
3075}
3076
deadbeeffe4a8a42016-12-20 17:56:17 -08003077// Tests that the methods to return current/pending descriptions work as
3078// expected at different points in the offer/answer exchange. This test does
3079// one offer/answer exchange as the offerer, then another as the answerer.
3080TEST_F(PeerConnectionInterfaceTest, CurrentAndPendingDescriptions) {
3081 // This disables DTLS so we can apply an answer to ourselves.
3082 CreatePeerConnection();
3083
3084 // Create initial local offer and get SDP (which will also be used as
3085 // answer/pranswer);
3086 std::unique_ptr<SessionDescriptionInterface> offer;
3087 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
3088 std::string sdp;
3089 EXPECT_TRUE(offer->ToString(&sdp));
3090
3091 // Set local offer.
3092 SessionDescriptionInterface* local_offer = offer.release();
3093 EXPECT_TRUE(DoSetLocalDescription(local_offer));
3094 EXPECT_EQ(local_offer, pc_->pending_local_description());
3095 EXPECT_EQ(nullptr, pc_->pending_remote_description());
3096 EXPECT_EQ(nullptr, pc_->current_local_description());
3097 EXPECT_EQ(nullptr, pc_->current_remote_description());
3098
3099 // Set remote pranswer.
3100 SessionDescriptionInterface* remote_pranswer =
3101 webrtc::CreateSessionDescription(SessionDescriptionInterface::kPrAnswer,
3102 sdp, nullptr);
3103 EXPECT_TRUE(DoSetRemoteDescription(remote_pranswer));
3104 EXPECT_EQ(local_offer, pc_->pending_local_description());
3105 EXPECT_EQ(remote_pranswer, pc_->pending_remote_description());
3106 EXPECT_EQ(nullptr, pc_->current_local_description());
3107 EXPECT_EQ(nullptr, pc_->current_remote_description());
3108
3109 // Set remote answer.
3110 SessionDescriptionInterface* remote_answer = webrtc::CreateSessionDescription(
3111 SessionDescriptionInterface::kAnswer, sdp, nullptr);
3112 EXPECT_TRUE(DoSetRemoteDescription(remote_answer));
3113 EXPECT_EQ(nullptr, pc_->pending_local_description());
3114 EXPECT_EQ(nullptr, pc_->pending_remote_description());
3115 EXPECT_EQ(local_offer, pc_->current_local_description());
3116 EXPECT_EQ(remote_answer, pc_->current_remote_description());
3117
3118 // Set remote offer.
3119 SessionDescriptionInterface* remote_offer = webrtc::CreateSessionDescription(
3120 SessionDescriptionInterface::kOffer, sdp, nullptr);
3121 EXPECT_TRUE(DoSetRemoteDescription(remote_offer));
3122 EXPECT_EQ(remote_offer, pc_->pending_remote_description());
3123 EXPECT_EQ(nullptr, pc_->pending_local_description());
3124 EXPECT_EQ(local_offer, pc_->current_local_description());
3125 EXPECT_EQ(remote_answer, pc_->current_remote_description());
3126
3127 // Set local pranswer.
3128 SessionDescriptionInterface* local_pranswer =
3129 webrtc::CreateSessionDescription(SessionDescriptionInterface::kPrAnswer,
3130 sdp, nullptr);
3131 EXPECT_TRUE(DoSetLocalDescription(local_pranswer));
3132 EXPECT_EQ(remote_offer, pc_->pending_remote_description());
3133 EXPECT_EQ(local_pranswer, pc_->pending_local_description());
3134 EXPECT_EQ(local_offer, pc_->current_local_description());
3135 EXPECT_EQ(remote_answer, pc_->current_remote_description());
3136
3137 // Set local answer.
3138 SessionDescriptionInterface* local_answer = webrtc::CreateSessionDescription(
3139 SessionDescriptionInterface::kAnswer, sdp, nullptr);
3140 EXPECT_TRUE(DoSetLocalDescription(local_answer));
3141 EXPECT_EQ(nullptr, pc_->pending_remote_description());
3142 EXPECT_EQ(nullptr, pc_->pending_local_description());
3143 EXPECT_EQ(remote_offer, pc_->current_remote_description());
3144 EXPECT_EQ(local_answer, pc_->current_local_description());
3145}
3146
zhihuang77985012017-02-07 15:45:16 -08003147// Tests that it won't crash when calling StartRtcEventLog or StopRtcEventLog
3148// after the PeerConnection is closed.
3149TEST_F(PeerConnectionInterfaceTest,
3150 StartAndStopLoggingAfterPeerConnectionClosed) {
3151 CreatePeerConnection();
3152 // The RtcEventLog will be reset when the PeerConnection is closed.
3153 pc_->Close();
3154
3155 rtc::PlatformFile file = 0;
3156 int64_t max_size_bytes = 1024;
3157 EXPECT_FALSE(pc_->StartRtcEventLog(file, max_size_bytes));
3158 pc_->StopRtcEventLog();
3159}
3160
deadbeef30952b42017-04-21 02:41:29 -07003161// Test that generated offers/answers include "ice-option:trickle".
3162TEST_F(PeerConnectionInterfaceTest, OffersAndAnswersHaveTrickleIceOption) {
3163 CreatePeerConnection();
3164
3165 // First, create an offer with audio/video.
3166 FakeConstraints constraints;
3167 constraints.SetMandatoryReceiveAudio(true);
3168 constraints.SetMandatoryReceiveVideo(true);
3169 std::unique_ptr<SessionDescriptionInterface> offer;
3170 ASSERT_TRUE(DoCreateOffer(&offer, &constraints));
3171 cricket::SessionDescription* desc = offer->description();
3172 ASSERT_EQ(2u, desc->transport_infos().size());
3173 EXPECT_TRUE(desc->transport_infos()[0].description.HasOption("trickle"));
3174 EXPECT_TRUE(desc->transport_infos()[1].description.HasOption("trickle"));
3175
3176 // Apply the offer as a remote description, then create an answer.
3177 EXPECT_TRUE(DoSetRemoteDescription(offer.release()));
3178 std::unique_ptr<SessionDescriptionInterface> answer;
3179 ASSERT_TRUE(DoCreateAnswer(&answer, &constraints));
3180 desc = answer->description();
3181 ASSERT_EQ(2u, desc->transport_infos().size());
3182 EXPECT_TRUE(desc->transport_infos()[0].description.HasOption("trickle"));
3183 EXPECT_TRUE(desc->transport_infos()[1].description.HasOption("trickle"));
3184}
3185
deadbeef1dcb1642017-03-29 21:08:16 -07003186// Test that ICE renomination isn't offered if it's not enabled in the PC's
3187// RTCConfiguration.
3188TEST_F(PeerConnectionInterfaceTest, IceRenominationNotOffered) {
3189 PeerConnectionInterface::RTCConfiguration config;
3190 config.enable_ice_renomination = false;
3191 CreatePeerConnection(config, nullptr);
3192 AddVoiceStream("foo");
3193
3194 std::unique_ptr<SessionDescriptionInterface> offer;
3195 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
3196 cricket::SessionDescription* desc = offer->description();
3197 EXPECT_EQ(1u, desc->transport_infos().size());
3198 EXPECT_FALSE(
3199 desc->transport_infos()[0].description.GetIceParameters().renomination);
3200}
3201
3202// Test that the ICE renomination option is present in generated offers/answers
3203// if it's enabled in the PC's RTCConfiguration.
3204TEST_F(PeerConnectionInterfaceTest, IceRenominationOptionInOfferAndAnswer) {
3205 PeerConnectionInterface::RTCConfiguration config;
3206 config.enable_ice_renomination = true;
3207 CreatePeerConnection(config, nullptr);
3208 AddVoiceStream("foo");
3209
3210 std::unique_ptr<SessionDescriptionInterface> offer;
3211 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
3212 cricket::SessionDescription* desc = offer->description();
3213 EXPECT_EQ(1u, desc->transport_infos().size());
3214 EXPECT_TRUE(
3215 desc->transport_infos()[0].description.GetIceParameters().renomination);
3216
3217 // Set the offer as a remote description, then create an answer and ensure it
3218 // has the renomination flag too.
3219 EXPECT_TRUE(DoSetRemoteDescription(offer.release()));
3220 std::unique_ptr<SessionDescriptionInterface> answer;
3221 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
3222 desc = answer->description();
3223 EXPECT_EQ(1u, desc->transport_infos().size());
3224 EXPECT_TRUE(
3225 desc->transport_infos()[0].description.GetIceParameters().renomination);
3226}
3227
3228// Test that if CreateOffer is called with the deprecated "offer to receive
3229// audio/video" constraints, they're processed and result in an offer with
3230// audio/video sections just as if RTCOfferAnswerOptions had been used.
3231TEST_F(PeerConnectionInterfaceTest, CreateOfferWithOfferToReceiveConstraints) {
3232 CreatePeerConnection();
3233
3234 FakeConstraints constraints;
3235 constraints.SetMandatoryReceiveAudio(true);
3236 constraints.SetMandatoryReceiveVideo(true);
3237 std::unique_ptr<SessionDescriptionInterface> offer;
3238 ASSERT_TRUE(DoCreateOffer(&offer, &constraints));
3239
3240 cricket::SessionDescription* desc = offer->description();
3241 const cricket::ContentInfo* audio = cricket::GetFirstAudioContent(desc);
3242 const cricket::ContentInfo* video = cricket::GetFirstVideoContent(desc);
3243 ASSERT_NE(nullptr, audio);
3244 ASSERT_NE(nullptr, video);
3245 EXPECT_FALSE(audio->rejected);
3246 EXPECT_FALSE(video->rejected);
3247}
3248
3249// Test that if CreateAnswer is called with the deprecated "offer to receive
3250// audio/video" constraints, they're processed and can be used to reject an
3251// offered m= section just as can be done with RTCOfferAnswerOptions;
3252TEST_F(PeerConnectionInterfaceTest, CreateAnswerWithOfferToReceiveConstraints) {
3253 CreatePeerConnection();
3254
3255 // First, create an offer with audio/video and apply it as a remote
3256 // description.
3257 FakeConstraints constraints;
3258 constraints.SetMandatoryReceiveAudio(true);
3259 constraints.SetMandatoryReceiveVideo(true);
3260 std::unique_ptr<SessionDescriptionInterface> offer;
3261 ASSERT_TRUE(DoCreateOffer(&offer, &constraints));
3262 EXPECT_TRUE(DoSetRemoteDescription(offer.release()));
3263
3264 // Now create answer that rejects audio/video.
3265 constraints.SetMandatoryReceiveAudio(false);
3266 constraints.SetMandatoryReceiveVideo(false);
3267 std::unique_ptr<SessionDescriptionInterface> answer;
3268 ASSERT_TRUE(DoCreateAnswer(&answer, &constraints));
3269
3270 cricket::SessionDescription* desc = answer->description();
3271 const cricket::ContentInfo* audio = cricket::GetFirstAudioContent(desc);
3272 const cricket::ContentInfo* video = cricket::GetFirstVideoContent(desc);
3273 ASSERT_NE(nullptr, audio);
3274 ASSERT_NE(nullptr, video);
3275 EXPECT_TRUE(audio->rejected);
3276 EXPECT_TRUE(video->rejected);
3277}
3278
3279#ifdef HAVE_SCTP
3280#define MAYBE_DataChannelOnlyOfferWithMaxBundlePolicy \
3281 DataChannelOnlyOfferWithMaxBundlePolicy
3282#else
3283#define MAYBE_DataChannelOnlyOfferWithMaxBundlePolicy \
3284 DISABLED_DataChannelOnlyOfferWithMaxBundlePolicy
3285#endif
3286
3287// Test that negotiation can succeed with a data channel only, and with the max
3288// bundle policy. Previously there was a bug that prevented this.
3289TEST_F(PeerConnectionInterfaceTest,
3290 MAYBE_DataChannelOnlyOfferWithMaxBundlePolicy) {
3291 PeerConnectionInterface::RTCConfiguration config;
3292 config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
3293 CreatePeerConnection(config, nullptr);
3294
3295 // First, create an offer with only a data channel and apply it as a remote
3296 // description.
3297 pc_->CreateDataChannel("test", nullptr);
3298 std::unique_ptr<SessionDescriptionInterface> offer;
3299 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
3300 EXPECT_TRUE(DoSetRemoteDescription(offer.release()));
3301
3302 // Create and set answer as well.
3303 std::unique_ptr<SessionDescriptionInterface> answer;
3304 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
3305 EXPECT_TRUE(DoSetLocalDescription(answer.release()));
3306}
3307
nisse51542be2016-02-12 02:27:06 -08003308class PeerConnectionMediaConfigTest : public testing::Test {
3309 protected:
3310 void SetUp() override {
nisseaf510af2016-03-21 08:20:42 -07003311 pcf_ = new rtc::RefCountedObject<PeerConnectionFactoryForTest>();
nisse51542be2016-02-12 02:27:06 -08003312 pcf_->Initialize();
3313 }
nisseeaabdf62017-05-05 02:23:02 -07003314 const cricket::MediaConfig TestCreatePeerConnection(
nisse51542be2016-02-12 02:27:06 -08003315 const PeerConnectionInterface::RTCConfiguration& config,
3316 const MediaConstraintsInterface *constraints) {
nisse51542be2016-02-12 02:27:06 -08003317
zhihuang9763d562016-08-05 11:14:50 -07003318 rtc::scoped_refptr<PeerConnectionInterface> pc(pcf_->CreatePeerConnection(
3319 config, constraints, nullptr, nullptr, &observer_));
nisse51542be2016-02-12 02:27:06 -08003320 EXPECT_TRUE(pc.get());
nisseeaabdf62017-05-05 02:23:02 -07003321 return pc->GetConfiguration().media_config;
nisse51542be2016-02-12 02:27:06 -08003322 }
3323
zhihuang9763d562016-08-05 11:14:50 -07003324 rtc::scoped_refptr<PeerConnectionFactoryForTest> pcf_;
nisse51542be2016-02-12 02:27:06 -08003325 MockPeerConnectionObserver observer_;
3326};
3327
3328// This test verifies the default behaviour with no constraints and a
3329// default RTCConfiguration.
3330TEST_F(PeerConnectionMediaConfigTest, TestDefaults) {
3331 PeerConnectionInterface::RTCConfiguration config;
3332 FakeConstraints constraints;
3333
3334 const cricket::MediaConfig& media_config =
3335 TestCreatePeerConnection(config, &constraints);
3336
3337 EXPECT_FALSE(media_config.enable_dscp);
nisse0db023a2016-03-01 04:29:59 -08003338 EXPECT_TRUE(media_config.video.enable_cpu_overuse_detection);
3339 EXPECT_FALSE(media_config.video.disable_prerenderer_smoothing);
3340 EXPECT_FALSE(media_config.video.suspend_below_min_bitrate);
nisse51542be2016-02-12 02:27:06 -08003341}
3342
3343// This test verifies the DSCP constraint is recognized and passed to
nisse528b7932017-05-08 03:21:43 -07003344// the PeerConnection.
nisse51542be2016-02-12 02:27:06 -08003345TEST_F(PeerConnectionMediaConfigTest, TestDscpConstraintTrue) {
3346 PeerConnectionInterface::RTCConfiguration config;
3347 FakeConstraints constraints;
3348
3349 constraints.AddOptional(webrtc::MediaConstraintsInterface::kEnableDscp, true);
3350 const cricket::MediaConfig& media_config =
3351 TestCreatePeerConnection(config, &constraints);
3352
3353 EXPECT_TRUE(media_config.enable_dscp);
3354}
3355
3356// This test verifies the cpu overuse detection constraint is
nisse528b7932017-05-08 03:21:43 -07003357// recognized and passed to the PeerConnection.
nisse51542be2016-02-12 02:27:06 -08003358TEST_F(PeerConnectionMediaConfigTest, TestCpuOveruseConstraintFalse) {
3359 PeerConnectionInterface::RTCConfiguration config;
3360 FakeConstraints constraints;
3361
3362 constraints.AddOptional(
3363 webrtc::MediaConstraintsInterface::kCpuOveruseDetection, false);
3364 const cricket::MediaConfig media_config =
3365 TestCreatePeerConnection(config, &constraints);
3366
nisse0db023a2016-03-01 04:29:59 -08003367 EXPECT_FALSE(media_config.video.enable_cpu_overuse_detection);
nisse51542be2016-02-12 02:27:06 -08003368}
3369
3370// This test verifies that the disable_prerenderer_smoothing flag is
nisse528b7932017-05-08 03:21:43 -07003371// propagated from RTCConfiguration to the PeerConnection.
nisse51542be2016-02-12 02:27:06 -08003372TEST_F(PeerConnectionMediaConfigTest, TestDisablePrerendererSmoothingTrue) {
3373 PeerConnectionInterface::RTCConfiguration config;
3374 FakeConstraints constraints;
3375
Niels Möller71bdda02016-03-31 12:59:59 +02003376 config.set_prerenderer_smoothing(false);
nisse51542be2016-02-12 02:27:06 -08003377 const cricket::MediaConfig& media_config =
3378 TestCreatePeerConnection(config, &constraints);
3379
nisse0db023a2016-03-01 04:29:59 -08003380 EXPECT_TRUE(media_config.video.disable_prerenderer_smoothing);
3381}
3382
3383// This test verifies the suspend below min bitrate constraint is
nisse528b7932017-05-08 03:21:43 -07003384// recognized and passed to the PeerConnection.
nisse0db023a2016-03-01 04:29:59 -08003385TEST_F(PeerConnectionMediaConfigTest,
3386 TestSuspendBelowMinBitrateConstraintTrue) {
3387 PeerConnectionInterface::RTCConfiguration config;
3388 FakeConstraints constraints;
3389
3390 constraints.AddOptional(
3391 webrtc::MediaConstraintsInterface::kEnableVideoSuspendBelowMinBitrate,
3392 true);
3393 const cricket::MediaConfig media_config =
3394 TestCreatePeerConnection(config, &constraints);
3395
3396 EXPECT_TRUE(media_config.video.suspend_below_min_bitrate);
nisse51542be2016-02-12 02:27:06 -08003397}
3398
deadbeefab9b2d12015-10-14 11:33:11 -07003399// The following tests verify that session options are created correctly.
deadbeefc80741f2015-10-22 13:14:45 -07003400// TODO(deadbeef): Convert these tests to be more end-to-end. Instead of
3401// "verify options are converted correctly", should be "pass options into
3402// CreateOffer and verify the correct offer is produced."
deadbeefab9b2d12015-10-14 11:33:11 -07003403
3404TEST(CreateSessionOptionsTest, GetOptionsForOfferWithInvalidAudioOption) {
3405 RTCOfferAnswerOptions rtc_options;
3406 rtc_options.offer_to_receive_audio = RTCOfferAnswerOptions::kUndefined - 1;
3407
3408 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08003409 EXPECT_FALSE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07003410
3411 rtc_options.offer_to_receive_audio =
3412 RTCOfferAnswerOptions::kMaxOfferToReceiveMedia + 1;
htaaac2dea2016-03-10 13:35:55 -08003413 EXPECT_FALSE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07003414}
3415
3416TEST(CreateSessionOptionsTest, GetOptionsForOfferWithInvalidVideoOption) {
3417 RTCOfferAnswerOptions rtc_options;
3418 rtc_options.offer_to_receive_video = RTCOfferAnswerOptions::kUndefined - 1;
3419
3420 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08003421 EXPECT_FALSE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07003422
3423 rtc_options.offer_to_receive_video =
3424 RTCOfferAnswerOptions::kMaxOfferToReceiveMedia + 1;
htaaac2dea2016-03-10 13:35:55 -08003425 EXPECT_FALSE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07003426}
3427
3428// Test that a MediaSessionOptions is created for an offer if
deadbeefc80741f2015-10-22 13:14:45 -07003429// OfferToReceiveAudio and OfferToReceiveVideo options are set.
deadbeefab9b2d12015-10-14 11:33:11 -07003430TEST(CreateSessionOptionsTest, GetMediaSessionOptionsForOfferWithAudioVideo) {
3431 RTCOfferAnswerOptions rtc_options;
3432 rtc_options.offer_to_receive_audio = 1;
3433 rtc_options.offer_to_receive_video = 1;
3434
3435 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08003436 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07003437 EXPECT_TRUE(options.has_audio());
3438 EXPECT_TRUE(options.has_video());
3439 EXPECT_TRUE(options.bundle_enabled);
3440}
3441
3442// Test that a correct MediaSessionOptions is created for an offer if
deadbeefc80741f2015-10-22 13:14:45 -07003443// OfferToReceiveAudio is set.
deadbeefab9b2d12015-10-14 11:33:11 -07003444TEST(CreateSessionOptionsTest, GetMediaSessionOptionsForOfferWithAudio) {
3445 RTCOfferAnswerOptions rtc_options;
3446 rtc_options.offer_to_receive_audio = 1;
3447
3448 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08003449 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07003450 EXPECT_TRUE(options.has_audio());
3451 EXPECT_FALSE(options.has_video());
3452 EXPECT_TRUE(options.bundle_enabled);
3453}
3454
3455// Test that a correct MediaSessionOptions is created for an offer if
deadbeefc80741f2015-10-22 13:14:45 -07003456// the default OfferOptions are used.
deadbeefab9b2d12015-10-14 11:33:11 -07003457TEST(CreateSessionOptionsTest, GetDefaultMediaSessionOptionsForOffer) {
3458 RTCOfferAnswerOptions rtc_options;
3459
3460 cricket::MediaSessionOptions options;
deadbeef0ed85b22016-02-23 17:24:52 -08003461 options.transport_options["audio"] = cricket::TransportOptions();
3462 options.transport_options["video"] = cricket::TransportOptions();
htaaac2dea2016-03-10 13:35:55 -08003463 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefc80741f2015-10-22 13:14:45 -07003464 EXPECT_TRUE(options.has_audio());
deadbeefab9b2d12015-10-14 11:33:11 -07003465 EXPECT_FALSE(options.has_video());
deadbeefc80741f2015-10-22 13:14:45 -07003466 EXPECT_TRUE(options.bundle_enabled);
deadbeefab9b2d12015-10-14 11:33:11 -07003467 EXPECT_TRUE(options.vad_enabled);
deadbeef0ed85b22016-02-23 17:24:52 -08003468 EXPECT_FALSE(options.transport_options["audio"].ice_restart);
3469 EXPECT_FALSE(options.transport_options["video"].ice_restart);
deadbeefab9b2d12015-10-14 11:33:11 -07003470}
3471
3472// Test that a correct MediaSessionOptions is created for an offer if
deadbeefc80741f2015-10-22 13:14:45 -07003473// OfferToReceiveVideo is set.
deadbeefab9b2d12015-10-14 11:33:11 -07003474TEST(CreateSessionOptionsTest, GetMediaSessionOptionsForOfferWithVideo) {
3475 RTCOfferAnswerOptions rtc_options;
3476 rtc_options.offer_to_receive_audio = 0;
3477 rtc_options.offer_to_receive_video = 1;
3478
3479 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08003480 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07003481 EXPECT_FALSE(options.has_audio());
3482 EXPECT_TRUE(options.has_video());
3483 EXPECT_TRUE(options.bundle_enabled);
3484}
3485
3486// Test that a correct MediaSessionOptions is created for an offer if
3487// UseRtpMux is set to false.
3488TEST(CreateSessionOptionsTest,
3489 GetMediaSessionOptionsForOfferWithBundleDisabled) {
3490 RTCOfferAnswerOptions rtc_options;
3491 rtc_options.offer_to_receive_audio = 1;
3492 rtc_options.offer_to_receive_video = 1;
3493 rtc_options.use_rtp_mux = false;
3494
3495 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08003496 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07003497 EXPECT_TRUE(options.has_audio());
3498 EXPECT_TRUE(options.has_video());
3499 EXPECT_FALSE(options.bundle_enabled);
3500}
3501
3502// Test that a correct MediaSessionOptions is created to restart ice if
3503// IceRestart is set. It also tests that subsequent MediaSessionOptions don't
Taylor Brandstetterf475d362016-01-08 15:35:57 -08003504// have |audio_transport_options.ice_restart| etc. set.
deadbeefab9b2d12015-10-14 11:33:11 -07003505TEST(CreateSessionOptionsTest, GetMediaSessionOptionsForOfferWithIceRestart) {
3506 RTCOfferAnswerOptions rtc_options;
3507 rtc_options.ice_restart = true;
3508
3509 cricket::MediaSessionOptions options;
deadbeef0ed85b22016-02-23 17:24:52 -08003510 options.transport_options["audio"] = cricket::TransportOptions();
3511 options.transport_options["video"] = cricket::TransportOptions();
htaaac2dea2016-03-10 13:35:55 -08003512 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeef0ed85b22016-02-23 17:24:52 -08003513 EXPECT_TRUE(options.transport_options["audio"].ice_restart);
3514 EXPECT_TRUE(options.transport_options["video"].ice_restart);
deadbeefab9b2d12015-10-14 11:33:11 -07003515
3516 rtc_options = RTCOfferAnswerOptions();
htaaac2dea2016-03-10 13:35:55 -08003517 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeef0ed85b22016-02-23 17:24:52 -08003518 EXPECT_FALSE(options.transport_options["audio"].ice_restart);
3519 EXPECT_FALSE(options.transport_options["video"].ice_restart);
deadbeefab9b2d12015-10-14 11:33:11 -07003520}
3521
3522// Test that the MediaConstraints in an answer don't affect if audio and video
3523// is offered in an offer but that if kOfferToReceiveAudio or
3524// kOfferToReceiveVideo constraints are true in an offer, the media type will be
3525// included in subsequent answers.
3526TEST(CreateSessionOptionsTest, MediaConstraintsInAnswer) {
3527 FakeConstraints answer_c;
3528 answer_c.SetMandatoryReceiveAudio(true);
3529 answer_c.SetMandatoryReceiveVideo(true);
3530
3531 cricket::MediaSessionOptions answer_options;
3532 EXPECT_TRUE(ParseConstraintsForAnswer(&answer_c, &answer_options));
3533 EXPECT_TRUE(answer_options.has_audio());
3534 EXPECT_TRUE(answer_options.has_video());
3535
deadbeefc80741f2015-10-22 13:14:45 -07003536 RTCOfferAnswerOptions rtc_offer_options;
deadbeefab9b2d12015-10-14 11:33:11 -07003537
3538 cricket::MediaSessionOptions offer_options;
htaaac2dea2016-03-10 13:35:55 -08003539 EXPECT_TRUE(
3540 ExtractMediaSessionOptions(rtc_offer_options, false, &offer_options));
deadbeefc80741f2015-10-22 13:14:45 -07003541 EXPECT_TRUE(offer_options.has_audio());
htaaac2dea2016-03-10 13:35:55 -08003542 EXPECT_TRUE(offer_options.has_video());
deadbeefab9b2d12015-10-14 11:33:11 -07003543
deadbeefc80741f2015-10-22 13:14:45 -07003544 RTCOfferAnswerOptions updated_rtc_offer_options;
3545 updated_rtc_offer_options.offer_to_receive_audio = 1;
3546 updated_rtc_offer_options.offer_to_receive_video = 1;
deadbeefab9b2d12015-10-14 11:33:11 -07003547
3548 cricket::MediaSessionOptions updated_offer_options;
htaaac2dea2016-03-10 13:35:55 -08003549 EXPECT_TRUE(ExtractMediaSessionOptions(updated_rtc_offer_options, false,
htaa2a49d92016-03-04 02:51:39 -08003550 &updated_offer_options));
deadbeefab9b2d12015-10-14 11:33:11 -07003551 EXPECT_TRUE(updated_offer_options.has_audio());
3552 EXPECT_TRUE(updated_offer_options.has_video());
3553
3554 // Since an offer has been created with both audio and video, subsequent
3555 // offers and answers should contain both audio and video.
3556 // Answers will only contain the media types that exist in the offer
3557 // regardless of the value of |updated_answer_options.has_audio| and
3558 // |updated_answer_options.has_video|.
3559 FakeConstraints updated_answer_c;
3560 answer_c.SetMandatoryReceiveAudio(false);
3561 answer_c.SetMandatoryReceiveVideo(false);
3562
3563 cricket::MediaSessionOptions updated_answer_options;
3564 EXPECT_TRUE(
3565 ParseConstraintsForAnswer(&updated_answer_c, &updated_answer_options));
3566 EXPECT_TRUE(updated_answer_options.has_audio());
3567 EXPECT_TRUE(updated_answer_options.has_video());
deadbeefab9b2d12015-10-14 11:33:11 -07003568}
deadbeef3edec7c2016-12-10 11:44:26 -08003569
deadbeef293e9262017-01-11 12:28:30 -08003570// Tests a few random fields being different.
3571TEST(RTCConfigurationTest, ComparisonOperators) {
3572 PeerConnectionInterface::RTCConfiguration a;
3573 PeerConnectionInterface::RTCConfiguration b;
3574 EXPECT_EQ(a, b);
3575
3576 PeerConnectionInterface::RTCConfiguration c;
3577 c.servers.push_back(PeerConnectionInterface::IceServer());
3578 EXPECT_NE(a, c);
3579
3580 PeerConnectionInterface::RTCConfiguration d;
3581 d.type = PeerConnectionInterface::kRelay;
3582 EXPECT_NE(a, d);
3583
3584 PeerConnectionInterface::RTCConfiguration e;
3585 e.audio_jitter_buffer_max_packets = 5;
3586 EXPECT_NE(a, e);
3587
3588 PeerConnectionInterface::RTCConfiguration f;
3589 f.ice_connection_receiving_timeout = 1337;
3590 EXPECT_NE(a, f);
3591
3592 PeerConnectionInterface::RTCConfiguration g;
3593 g.disable_ipv6 = true;
3594 EXPECT_NE(a, g);
3595
3596 PeerConnectionInterface::RTCConfiguration h(
3597 PeerConnectionInterface::RTCConfigurationType::kAggressive);
3598 EXPECT_NE(a, h);
3599}