blob: 05d53ad1a153fdcacc66f240d706c88f69d51f64 [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"
Henrik Kjellander15583c12016-02-10 10:53:12 +010017#include "webrtc/api/jsepsessiondescription.h"
Henrik Kjellander15583c12016-02-10 10:53:12 +010018#include "webrtc/api/mediastreaminterface.h"
Henrik Kjellander15583c12016-02-10 10:53:12 +010019#include "webrtc/api/peerconnectioninterface.h"
20#include "webrtc/api/rtpreceiverinterface.h"
21#include "webrtc/api/rtpsenderinterface.h"
Henrik Kjellander15583c12016-02-10 10:53:12 +010022#include "webrtc/api/test/fakeconstraints.h"
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000023#include "webrtc/base/gunit.h"
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000024#include "webrtc/base/ssladapter.h"
25#include "webrtc/base/sslstreamadapter.h"
26#include "webrtc/base/stringutils.h"
27#include "webrtc/base/thread.h"
kjellandera96e2d72016-02-04 23:52:28 -080028#include "webrtc/media/base/fakevideocapturer.h"
deadbeef953c2ce2017-01-09 14:53:41 -080029#include "webrtc/media/sctp/sctptransportinternal.h"
Taylor Brandstettera1c30352016-05-13 08:15:11 -070030#include "webrtc/p2p/base/fakeportallocator.h"
ossu7bb87ee2017-01-23 04:56:25 -080031#include "webrtc/pc/audiotrack.h"
kjellander@webrtc.org9b8df252016-02-12 06:47:59 +010032#include "webrtc/pc/mediasession.h"
ossu7bb87ee2017-01-23 04:56:25 -080033#include "webrtc/pc/mediastream.h"
34#include "webrtc/pc/peerconnection.h"
35#include "webrtc/pc/streamcollection.h"
36#include "webrtc/pc/test/fakertccertificategenerator.h"
37#include "webrtc/pc/test/fakevideotracksource.h"
38#include "webrtc/pc/test/mockpeerconnectionobservers.h"
39#include "webrtc/pc/test/testsdpstrings.h"
40#include "webrtc/pc/videocapturertracksource.h"
41#include "webrtc/pc/videotrack.h"
kwibergac9f8762016-09-30 22:29:43 -070042#include "webrtc/test/gmock.h"
43
44#ifdef WEBRTC_ANDROID
ossu7bb87ee2017-01-23 04:56:25 -080045#include "webrtc/pc/test/androidtestinitializer.h"
kwibergac9f8762016-09-30 22:29:43 -070046#endif
henrike@webrtc.org28e20752013-07-10 00:45:36 +000047
48static const char kStreamLabel1[] = "local_stream_1";
49static const char kStreamLabel2[] = "local_stream_2";
50static const char kStreamLabel3[] = "local_stream_3";
51static const int kDefaultStunPort = 3478;
52static const char kStunAddressOnly[] = "stun:address";
53static const char kStunInvalidPort[] = "stun:address:-1";
54static const char kStunAddressPortAndMore1[] = "stun:address:port:more";
55static const char kStunAddressPortAndMore2[] = "stun:address:port more";
56static const char kTurnIceServerUri[] = "turn:user@turn.example.org";
57static const char kTurnUsername[] = "user";
58static const char kTurnPassword[] = "password";
59static const char kTurnHostname[] = "turn.example.org";
Peter Boström0c4e06b2015-10-07 12:23:21 +020060static const uint32_t kTimeout = 10000U;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000061
deadbeefab9b2d12015-10-14 11:33:11 -070062static const char kStreams[][8] = {"stream1", "stream2"};
63static const char kAudioTracks[][32] = {"audiotrack0", "audiotrack1"};
64static const char kVideoTracks[][32] = {"videotrack0", "videotrack1"};
65
deadbeef5e97fb52015-10-15 12:49:08 -070066static const char kRecvonly[] = "recvonly";
67static const char kSendrecv[] = "sendrecv";
68
deadbeefab9b2d12015-10-14 11:33:11 -070069// Reference SDP with a MediaStream with label "stream1" and audio track with
70// id "audio_1" and a video track with id "video_1;
71static const char kSdpStringWithStream1[] =
72 "v=0\r\n"
73 "o=- 0 0 IN IP4 127.0.0.1\r\n"
74 "s=-\r\n"
75 "t=0 0\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -080076 "m=audio 1 RTP/AVPF 103\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -070077 "a=ice-ufrag:e5785931\r\n"
78 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
79 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
80 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -070081 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -070082 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -080083 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -070084 "a=rtpmap:103 ISAC/16000\r\n"
85 "a=ssrc:1 cname:stream1\r\n"
86 "a=ssrc:1 mslabel:stream1\r\n"
87 "a=ssrc:1 label:audiotrack0\r\n"
88 "m=video 1 RTP/AVPF 120\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -080089 "a=ice-ufrag:e5785931\r\n"
90 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
91 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
92 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -070093 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -070094 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -080095 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -070096 "a=rtpmap:120 VP8/90000\r\n"
97 "a=ssrc:2 cname:stream1\r\n"
98 "a=ssrc:2 mslabel:stream1\r\n"
99 "a=ssrc:2 label:videotrack0\r\n";
100
zhihuang81c3a032016-11-17 12:06:24 -0800101// Reference SDP with a MediaStream with label "stream1" and audio track with
102// id "audio_1";
103static const char kSdpStringWithStream1AudioTrackOnly[] =
104 "v=0\r\n"
105 "o=- 0 0 IN IP4 127.0.0.1\r\n"
106 "s=-\r\n"
107 "t=0 0\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800108 "m=audio 1 RTP/AVPF 103\r\n"
zhihuang81c3a032016-11-17 12:06:24 -0800109 "a=ice-ufrag:e5785931\r\n"
110 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
111 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
112 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
zhihuang81c3a032016-11-17 12:06:24 -0800113 "a=mid:audio\r\n"
114 "a=sendrecv\r\n"
115 "a=rtpmap:103 ISAC/16000\r\n"
116 "a=ssrc:1 cname:stream1\r\n"
117 "a=ssrc:1 mslabel:stream1\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800118 "a=ssrc:1 label:audiotrack0\r\n"
119 "a=rtcp-mux\r\n";
zhihuang81c3a032016-11-17 12:06:24 -0800120
deadbeefab9b2d12015-10-14 11:33:11 -0700121// Reference SDP with two MediaStreams with label "stream1" and "stream2. Each
122// MediaStreams have one audio track and one video track.
123// This uses MSID.
124static const char kSdpStringWithStream1And2[] =
125 "v=0\r\n"
126 "o=- 0 0 IN IP4 127.0.0.1\r\n"
127 "s=-\r\n"
128 "t=0 0\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800129 "a=msid-semantic: WMS stream1 stream2\r\n"
130 "m=audio 1 RTP/AVPF 103\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700131 "a=ice-ufrag:e5785931\r\n"
132 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
133 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
134 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700135 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700136 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800137 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700138 "a=rtpmap:103 ISAC/16000\r\n"
139 "a=ssrc:1 cname:stream1\r\n"
140 "a=ssrc:1 msid:stream1 audiotrack0\r\n"
141 "a=ssrc:3 cname:stream2\r\n"
142 "a=ssrc:3 msid:stream2 audiotrack1\r\n"
143 "m=video 1 RTP/AVPF 120\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800144 "a=ice-ufrag:e5785931\r\n"
145 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
146 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
147 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700148 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700149 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800150 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700151 "a=rtpmap:120 VP8/0\r\n"
152 "a=ssrc:2 cname:stream1\r\n"
153 "a=ssrc:2 msid:stream1 videotrack0\r\n"
154 "a=ssrc:4 cname:stream2\r\n"
155 "a=ssrc:4 msid:stream2 videotrack1\r\n";
156
157// Reference SDP without MediaStreams. Msid is not supported.
158static const char kSdpStringWithoutStreams[] =
159 "v=0\r\n"
160 "o=- 0 0 IN IP4 127.0.0.1\r\n"
161 "s=-\r\n"
162 "t=0 0\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800163 "m=audio 1 RTP/AVPF 103\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700164 "a=ice-ufrag:e5785931\r\n"
165 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
166 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
167 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700168 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700169 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800170 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700171 "a=rtpmap:103 ISAC/16000\r\n"
172 "m=video 1 RTP/AVPF 120\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800173 "a=ice-ufrag:e5785931\r\n"
174 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
175 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
176 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700177 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700178 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800179 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700180 "a=rtpmap:120 VP8/90000\r\n";
181
182// Reference SDP without MediaStreams. Msid is supported.
183static const char kSdpStringWithMsidWithoutStreams[] =
184 "v=0\r\n"
185 "o=- 0 0 IN IP4 127.0.0.1\r\n"
186 "s=-\r\n"
187 "t=0 0\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800188 "a=msid-semantic: WMS\r\n"
189 "m=audio 1 RTP/AVPF 103\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700190 "a=ice-ufrag:e5785931\r\n"
191 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
192 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
193 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700194 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700195 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800196 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700197 "a=rtpmap:103 ISAC/16000\r\n"
198 "m=video 1 RTP/AVPF 120\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800199 "a=ice-ufrag:e5785931\r\n"
200 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
201 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
202 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700203 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700204 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800205 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700206 "a=rtpmap:120 VP8/90000\r\n";
207
208// Reference SDP without MediaStreams and audio only.
209static const char kSdpStringWithoutStreamsAudioOnly[] =
210 "v=0\r\n"
211 "o=- 0 0 IN IP4 127.0.0.1\r\n"
212 "s=-\r\n"
213 "t=0 0\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800214 "m=audio 1 RTP/AVPF 103\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700215 "a=ice-ufrag:e5785931\r\n"
216 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
217 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
218 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700219 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700220 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800221 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700222 "a=rtpmap:103 ISAC/16000\r\n";
223
224// Reference SENDONLY SDP without MediaStreams. Msid is not supported.
225static const char kSdpStringSendOnlyWithoutStreams[] =
226 "v=0\r\n"
227 "o=- 0 0 IN IP4 127.0.0.1\r\n"
228 "s=-\r\n"
229 "t=0 0\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800230 "m=audio 1 RTP/AVPF 103\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700231 "a=ice-ufrag:e5785931\r\n"
232 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
233 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
234 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700235 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700236 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700237 "a=sendonly\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800238 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700239 "a=rtpmap:103 ISAC/16000\r\n"
240 "m=video 1 RTP/AVPF 120\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800241 "a=ice-ufrag:e5785931\r\n"
242 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
243 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
244 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700245 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700246 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700247 "a=sendonly\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800248 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700249 "a=rtpmap:120 VP8/90000\r\n";
250
251static const char kSdpStringInit[] =
252 "v=0\r\n"
253 "o=- 0 0 IN IP4 127.0.0.1\r\n"
254 "s=-\r\n"
255 "t=0 0\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700256 "a=msid-semantic: WMS\r\n";
257
258static const char kSdpStringAudio[] =
259 "m=audio 1 RTP/AVPF 103\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800260 "a=ice-ufrag:e5785931\r\n"
261 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
262 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
263 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700264 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700265 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800266 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700267 "a=rtpmap:103 ISAC/16000\r\n";
268
269static const char kSdpStringVideo[] =
270 "m=video 1 RTP/AVPF 120\r\n"
deadbeefd1a38b52016-12-10 13:15:33 -0800271 "a=ice-ufrag:e5785931\r\n"
272 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
273 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
274 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700275 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700276 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800277 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700278 "a=rtpmap:120 VP8/90000\r\n";
279
280static const char kSdpStringMs1Audio0[] =
281 "a=ssrc:1 cname:stream1\r\n"
282 "a=ssrc:1 msid:stream1 audiotrack0\r\n";
283
284static const char kSdpStringMs1Video0[] =
285 "a=ssrc:2 cname:stream1\r\n"
286 "a=ssrc:2 msid:stream1 videotrack0\r\n";
287
288static const char kSdpStringMs1Audio1[] =
289 "a=ssrc:3 cname:stream1\r\n"
290 "a=ssrc:3 msid:stream1 audiotrack1\r\n";
291
292static const char kSdpStringMs1Video1[] =
293 "a=ssrc:4 cname:stream1\r\n"
294 "a=ssrc:4 msid:stream1 videotrack1\r\n";
295
deadbeef8662f942017-01-20 21:20:51 -0800296static const char kDtlsSdesFallbackSdp[] =
297 "v=0\r\n"
298 "o=xxxxxx 7 2 IN IP4 0.0.0.0\r\n"
299 "s=-\r\n"
300 "c=IN IP4 0.0.0.0\r\n"
301 "t=0 0\r\n"
302 "a=group:BUNDLE audio\r\n"
303 "a=msid-semantic: WMS\r\n"
304 "m=audio 1 RTP/SAVPF 0\r\n"
305 "a=sendrecv\r\n"
306 "a=rtcp-mux\r\n"
307 "a=mid:audio\r\n"
308 "a=ssrc:1 cname:stream1\r\n"
309 "a=ssrc:1 mslabel:stream1\r\n"
310 "a=ssrc:1 label:audiotrack0\r\n"
311 "a=ice-ufrag:e5785931\r\n"
312 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
313 "a=rtpmap:0 pcmu/8000\r\n"
314 "a=fingerprint:sha-1 "
315 "4A:AD:B9:B1:3F:82:18:3B:54:02:12:DF:3E:5D:49:6B:19:E5:7C:AB\r\n"
316 "a=setup:actpass\r\n"
317 "a=crypto:1 AES_CM_128_HMAC_SHA1_32 "
318 "inline:NzB4d1BINUAvLEw6UzF3WSJ+PSdFcGdUJShpX1Zj|2^20|1:32 "
319 "dummy_session_params\r\n";
320
perkjd61bf802016-03-24 03:16:19 -0700321using ::testing::Exactly;
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700322using cricket::StreamParams;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000323using webrtc::AudioSourceInterface;
deadbeefab9b2d12015-10-14 11:33:11 -0700324using webrtc::AudioTrack;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000325using webrtc::AudioTrackInterface;
326using webrtc::DataBuffer;
327using webrtc::DataChannelInterface;
328using webrtc::FakeConstraints;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000329using webrtc::IceCandidateInterface;
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700330using webrtc::JsepSessionDescription;
deadbeefc80741f2015-10-22 13:14:45 -0700331using webrtc::MediaConstraintsInterface;
deadbeefab9b2d12015-10-14 11:33:11 -0700332using webrtc::MediaStream;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000333using webrtc::MediaStreamInterface;
334using webrtc::MediaStreamTrackInterface;
335using webrtc::MockCreateSessionDescriptionObserver;
336using webrtc::MockDataChannelObserver;
337using webrtc::MockSetSessionDescriptionObserver;
338using webrtc::MockStatsObserver;
perkjd61bf802016-03-24 03:16:19 -0700339using webrtc::NotifierInterface;
340using webrtc::ObserverInterface;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000341using webrtc::PeerConnectionInterface;
342using webrtc::PeerConnectionObserver;
deadbeef293e9262017-01-11 12:28:30 -0800343using webrtc::RTCError;
344using webrtc::RTCErrorType;
deadbeefab9b2d12015-10-14 11:33:11 -0700345using webrtc::RtpReceiverInterface;
346using webrtc::RtpSenderInterface;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000347using webrtc::SdpParseError;
348using webrtc::SessionDescriptionInterface;
deadbeefab9b2d12015-10-14 11:33:11 -0700349using webrtc::StreamCollection;
350using webrtc::StreamCollectionInterface;
perkja3ede6c2016-03-08 01:27:48 +0100351using webrtc::VideoTrackSourceInterface;
deadbeefab9b2d12015-10-14 11:33:11 -0700352using webrtc::VideoTrack;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000353using webrtc::VideoTrackInterface;
354
deadbeefab9b2d12015-10-14 11:33:11 -0700355typedef PeerConnectionInterface::RTCOfferAnswerOptions RTCOfferAnswerOptions;
356
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000357namespace {
358
359// Gets the first ssrc of given content type from the ContentInfo.
360bool GetFirstSsrc(const cricket::ContentInfo* content_info, int* ssrc) {
361 if (!content_info || !ssrc) {
362 return false;
363 }
364 const cricket::MediaContentDescription* media_desc =
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000365 static_cast<const cricket::MediaContentDescription*>(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000366 content_info->description);
367 if (!media_desc || media_desc->streams().empty()) {
368 return false;
369 }
370 *ssrc = media_desc->streams().begin()->first_ssrc();
371 return true;
372}
373
deadbeefd1a38b52016-12-10 13:15:33 -0800374// Get the ufrags out of an SDP blob. Useful for testing ICE restart
375// behavior.
376std::vector<std::string> GetUfrags(
377 const webrtc::SessionDescriptionInterface* desc) {
378 std::vector<std::string> ufrags;
379 for (const cricket::TransportInfo& info :
380 desc->description()->transport_infos()) {
381 ufrags.push_back(info.description.ice_ufrag);
382 }
383 return ufrags;
384}
385
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000386void SetSsrcToZero(std::string* sdp) {
387 const char kSdpSsrcAtribute[] = "a=ssrc:";
388 const char kSdpSsrcAtributeZero[] = "a=ssrc:0";
389 size_t ssrc_pos = 0;
390 while ((ssrc_pos = sdp->find(kSdpSsrcAtribute, ssrc_pos)) !=
391 std::string::npos) {
392 size_t end_ssrc = sdp->find(" ", ssrc_pos);
393 sdp->replace(ssrc_pos, end_ssrc - ssrc_pos, kSdpSsrcAtributeZero);
394 ssrc_pos = end_ssrc;
395 }
396}
397
deadbeefab9b2d12015-10-14 11:33:11 -0700398// Check if |streams| contains the specified track.
399bool ContainsTrack(const std::vector<cricket::StreamParams>& streams,
400 const std::string& stream_label,
401 const std::string& track_id) {
402 for (const cricket::StreamParams& params : streams) {
403 if (params.sync_label == stream_label && params.id == track_id) {
404 return true;
405 }
406 }
407 return false;
408}
409
410// Check if |senders| contains the specified sender, by id.
411bool ContainsSender(
412 const std::vector<rtc::scoped_refptr<RtpSenderInterface>>& senders,
413 const std::string& id) {
414 for (const auto& sender : senders) {
415 if (sender->id() == id) {
416 return true;
417 }
418 }
419 return false;
420}
421
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700422// Check if |senders| contains the specified sender, by id and stream id.
423bool ContainsSender(
424 const std::vector<rtc::scoped_refptr<RtpSenderInterface>>& senders,
425 const std::string& id,
426 const std::string& stream_id) {
427 for (const auto& sender : senders) {
deadbeefa601f5c2016-06-06 14:27:39 -0700428 if (sender->id() == id && sender->stream_ids()[0] == stream_id) {
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700429 return true;
430 }
431 }
432 return false;
433}
434
deadbeefab9b2d12015-10-14 11:33:11 -0700435// Create a collection of streams.
436// CreateStreamCollection(1) creates a collection that
437// correspond to kSdpStringWithStream1.
438// CreateStreamCollection(2) correspond to kSdpStringWithStream1And2.
439rtc::scoped_refptr<StreamCollection> CreateStreamCollection(
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700440 int number_of_streams,
441 int tracks_per_stream) {
deadbeefab9b2d12015-10-14 11:33:11 -0700442 rtc::scoped_refptr<StreamCollection> local_collection(
443 StreamCollection::Create());
444
445 for (int i = 0; i < number_of_streams; ++i) {
446 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream(
447 webrtc::MediaStream::Create(kStreams[i]));
448
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700449 for (int j = 0; j < tracks_per_stream; ++j) {
450 // Add a local audio track.
451 rtc::scoped_refptr<webrtc::AudioTrackInterface> audio_track(
452 webrtc::AudioTrack::Create(kAudioTracks[i * tracks_per_stream + j],
453 nullptr));
454 stream->AddTrack(audio_track);
deadbeefab9b2d12015-10-14 11:33:11 -0700455
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700456 // Add a local video track.
457 rtc::scoped_refptr<webrtc::VideoTrackInterface> video_track(
458 webrtc::VideoTrack::Create(kVideoTracks[i * tracks_per_stream + j],
459 webrtc::FakeVideoTrackSource::Create()));
460 stream->AddTrack(video_track);
461 }
deadbeefab9b2d12015-10-14 11:33:11 -0700462
463 local_collection->AddStream(stream);
464 }
465 return local_collection;
466}
467
468// Check equality of StreamCollections.
469bool CompareStreamCollections(StreamCollectionInterface* s1,
470 StreamCollectionInterface* s2) {
471 if (s1 == nullptr || s2 == nullptr || s1->count() != s2->count()) {
472 return false;
473 }
474
475 for (size_t i = 0; i != s1->count(); ++i) {
476 if (s1->at(i)->label() != s2->at(i)->label()) {
477 return false;
478 }
479 webrtc::AudioTrackVector audio_tracks1 = s1->at(i)->GetAudioTracks();
480 webrtc::AudioTrackVector audio_tracks2 = s2->at(i)->GetAudioTracks();
481 webrtc::VideoTrackVector video_tracks1 = s1->at(i)->GetVideoTracks();
482 webrtc::VideoTrackVector video_tracks2 = s2->at(i)->GetVideoTracks();
483
484 if (audio_tracks1.size() != audio_tracks2.size()) {
485 return false;
486 }
487 for (size_t j = 0; j != audio_tracks1.size(); ++j) {
488 if (audio_tracks1[j]->id() != audio_tracks2[j]->id()) {
489 return false;
490 }
491 }
492 if (video_tracks1.size() != video_tracks2.size()) {
493 return false;
494 }
495 for (size_t j = 0; j != video_tracks1.size(); ++j) {
496 if (video_tracks1[j]->id() != video_tracks2[j]->id()) {
497 return false;
498 }
499 }
500 }
501 return true;
502}
503
perkjd61bf802016-03-24 03:16:19 -0700504// Helper class to test Observer.
505class MockTrackObserver : public ObserverInterface {
506 public:
507 explicit MockTrackObserver(NotifierInterface* notifier)
508 : notifier_(notifier) {
509 notifier_->RegisterObserver(this);
510 }
511
512 ~MockTrackObserver() { Unregister(); }
513
514 void Unregister() {
515 if (notifier_) {
516 notifier_->UnregisterObserver(this);
517 notifier_ = nullptr;
518 }
519 }
520
521 MOCK_METHOD0(OnChanged, void());
522
523 private:
524 NotifierInterface* notifier_;
525};
526
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000527class MockPeerConnectionObserver : public PeerConnectionObserver {
528 public:
deadbeefab9b2d12015-10-14 11:33:11 -0700529 MockPeerConnectionObserver() : remote_streams_(StreamCollection::Create()) {}
Henrik Kjellander3fe372d2016-05-12 08:10:52 +0200530 virtual ~MockPeerConnectionObserver() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000531 }
532 void SetPeerConnectionInterface(PeerConnectionInterface* pc) {
533 pc_ = pc;
534 if (pc) {
535 state_ = pc_->signaling_state();
536 }
537 }
nisseef8b61e2016-04-29 06:09:15 -0700538 void OnSignalingChange(
539 PeerConnectionInterface::SignalingState new_state) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000540 EXPECT_EQ(pc_->signaling_state(), new_state);
541 state_ = new_state;
542 }
deadbeefab9b2d12015-10-14 11:33:11 -0700543
544 MediaStreamInterface* RemoteStream(const std::string& label) {
545 return remote_streams_->find(label);
546 }
547 StreamCollectionInterface* remote_streams() const { return remote_streams_; }
Taylor Brandstetter98cde262016-05-31 13:02:21 -0700548 void OnAddStream(rtc::scoped_refptr<MediaStreamInterface> stream) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000549 last_added_stream_ = stream;
deadbeefab9b2d12015-10-14 11:33:11 -0700550 remote_streams_->AddStream(stream);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000551 }
Taylor Brandstetter98cde262016-05-31 13:02:21 -0700552 void OnRemoveStream(
553 rtc::scoped_refptr<MediaStreamInterface> stream) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000554 last_removed_stream_ = stream;
deadbeefab9b2d12015-10-14 11:33:11 -0700555 remote_streams_->RemoveStream(stream);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000556 }
perkjdfb769d2016-02-09 03:09:43 -0800557 void OnRenegotiationNeeded() override { renegotiation_needed_ = true; }
Taylor Brandstetter98cde262016-05-31 13:02:21 -0700558 void OnDataChannel(
559 rtc::scoped_refptr<DataChannelInterface> data_channel) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000560 last_datachannel_ = data_channel;
561 }
562
perkjdfb769d2016-02-09 03:09:43 -0800563 void OnIceConnectionChange(
564 PeerConnectionInterface::IceConnectionState new_state) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000565 EXPECT_EQ(pc_->ice_connection_state(), new_state);
zhihuang81c3a032016-11-17 12:06:24 -0800566 callback_triggered_ = true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000567 }
perkjdfb769d2016-02-09 03:09:43 -0800568 void OnIceGatheringChange(
569 PeerConnectionInterface::IceGatheringState new_state) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000570 EXPECT_EQ(pc_->ice_gathering_state(), new_state);
perkjdfb769d2016-02-09 03:09:43 -0800571 ice_complete_ = new_state == PeerConnectionInterface::kIceGatheringComplete;
zhihuang81c3a032016-11-17 12:06:24 -0800572 callback_triggered_ = true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000573 }
perkjdfb769d2016-02-09 03:09:43 -0800574 void OnIceCandidate(const webrtc::IceCandidateInterface* candidate) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000575 EXPECT_NE(PeerConnectionInterface::kIceGatheringNew,
576 pc_->ice_gathering_state());
577
578 std::string sdp;
579 EXPECT_TRUE(candidate->ToString(&sdp));
580 EXPECT_LT(0u, sdp.size());
581 last_candidate_.reset(webrtc::CreateIceCandidate(candidate->sdp_mid(),
582 candidate->sdp_mline_index(), sdp, NULL));
583 EXPECT_TRUE(last_candidate_.get() != NULL);
zhihuang81c3a032016-11-17 12:06:24 -0800584 callback_triggered_ = true;
zhihuang29ff8442016-07-27 11:07:25 -0700585 }
586
587 void OnIceCandidatesRemoved(
588 const std::vector<cricket::Candidate>& candidates) override {
zhihuang81c3a032016-11-17 12:06:24 -0800589 callback_triggered_ = true;
zhihuang29ff8442016-07-27 11:07:25 -0700590 }
591
592 void OnIceConnectionReceivingChange(bool receiving) override {
zhihuang81c3a032016-11-17 12:06:24 -0800593 callback_triggered_ = true;
594 }
595
zhihuangc63b8942016-12-02 15:41:10 -0800596 void OnAddTrack(
597 rtc::scoped_refptr<webrtc::RtpReceiverInterface> receiver,
598 const std::vector<rtc::scoped_refptr<webrtc::MediaStreamInterface>>&
599 streams) override {
zhihuang81c3a032016-11-17 12:06:24 -0800600 EXPECT_TRUE(receiver != nullptr);
601 num_added_tracks_++;
602 last_added_track_label_ = receiver->id();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000603 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000604
605 // Returns the label of the last added stream.
606 // Empty string if no stream have been added.
607 std::string GetLastAddedStreamLabel() {
608 if (last_added_stream_.get())
609 return last_added_stream_->label();
610 return "";
611 }
612 std::string GetLastRemovedStreamLabel() {
613 if (last_removed_stream_.get())
614 return last_removed_stream_->label();
615 return "";
616 }
617
zhihuang9763d562016-08-05 11:14:50 -0700618 rtc::scoped_refptr<PeerConnectionInterface> pc_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000619 PeerConnectionInterface::SignalingState state_;
kwibergd1fe2812016-04-27 06:47:29 -0700620 std::unique_ptr<IceCandidateInterface> last_candidate_;
zhihuang9763d562016-08-05 11:14:50 -0700621 rtc::scoped_refptr<DataChannelInterface> last_datachannel_;
deadbeefab9b2d12015-10-14 11:33:11 -0700622 rtc::scoped_refptr<StreamCollection> remote_streams_;
623 bool renegotiation_needed_ = false;
624 bool ice_complete_ = false;
zhihuang81c3a032016-11-17 12:06:24 -0800625 bool callback_triggered_ = false;
626 int num_added_tracks_ = 0;
627 std::string last_added_track_label_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000628
629 private:
zhihuang9763d562016-08-05 11:14:50 -0700630 rtc::scoped_refptr<MediaStreamInterface> last_added_stream_;
631 rtc::scoped_refptr<MediaStreamInterface> last_removed_stream_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000632};
633
634} // namespace
deadbeefab9b2d12015-10-14 11:33:11 -0700635
zhihuang29ff8442016-07-27 11:07:25 -0700636// The PeerConnectionMediaConfig tests below verify that configuration
637// and constraints are propagated into the MediaConfig passed to
638// CreateMediaController. These settings are intended for MediaChannel
639// constructors, but that is not exercised by these unittest.
640class PeerConnectionFactoryForTest : public webrtc::PeerConnectionFactory {
641 public:
kwiberg1e4e8cb2017-01-31 01:48:08 -0800642 PeerConnectionFactoryForTest()
643 : webrtc::PeerConnectionFactory(
644 webrtc::CreateBuiltinAudioEncoderFactory(),
645 webrtc::CreateBuiltinAudioDecoderFactory()) {}
646
zhihuang29ff8442016-07-27 11:07:25 -0700647 webrtc::MediaControllerInterface* CreateMediaController(
skvlad11a9cbf2016-10-07 11:53:05 -0700648 const cricket::MediaConfig& config,
649 webrtc::RtcEventLog* event_log) const override {
zhihuang29ff8442016-07-27 11:07:25 -0700650 create_media_controller_called_ = true;
651 create_media_controller_config_ = config;
652
653 webrtc::MediaControllerInterface* mc =
skvlad11a9cbf2016-10-07 11:53:05 -0700654 PeerConnectionFactory::CreateMediaController(config, event_log);
zhihuang29ff8442016-07-27 11:07:25 -0700655 EXPECT_TRUE(mc != nullptr);
656 return mc;
657 }
658
659 cricket::TransportController* CreateTransportController(
Honghai Zhangbfd398c2016-08-30 22:07:42 -0700660 cricket::PortAllocator* port_allocator,
661 bool redetermine_role_on_ice_restart) override {
zhihuang29ff8442016-07-27 11:07:25 -0700662 transport_controller = new cricket::TransportController(
Honghai Zhangbfd398c2016-08-30 22:07:42 -0700663 rtc::Thread::Current(), rtc::Thread::Current(), port_allocator,
664 redetermine_role_on_ice_restart);
zhihuang29ff8442016-07-27 11:07:25 -0700665 return transport_controller;
666 }
667
668 cricket::TransportController* transport_controller;
669 // Mutable, so they can be modified in the above const-declared method.
670 mutable bool create_media_controller_called_ = false;
671 mutable cricket::MediaConfig create_media_controller_config_;
672};
673
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000674class PeerConnectionInterfaceTest : public testing::Test {
675 protected:
phoglund37ebcf02016-01-08 05:04:57 -0800676 PeerConnectionInterfaceTest() {
677#ifdef WEBRTC_ANDROID
678 webrtc::InitializeAndroidObjects();
679#endif
680 }
681
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000682 virtual void SetUp() {
683 pc_factory_ = webrtc::CreatePeerConnectionFactory(
danilchape9021a32016-05-17 01:52:02 -0700684 rtc::Thread::Current(), rtc::Thread::Current(), rtc::Thread::Current(),
685 nullptr, nullptr, nullptr);
686 ASSERT_TRUE(pc_factory_);
zhihuang29ff8442016-07-27 11:07:25 -0700687 pc_factory_for_test_ =
688 new rtc::RefCountedObject<PeerConnectionFactoryForTest>();
689 pc_factory_for_test_->Initialize();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000690 }
691
692 void CreatePeerConnection() {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700693 CreatePeerConnection(PeerConnectionInterface::RTCConfiguration(), nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000694 }
695
deadbeef293e9262017-01-11 12:28:30 -0800696 // DTLS does not work in a loopback call, so is disabled for most of the
697 // tests in this file.
698 void CreatePeerConnectionWithoutDtls() {
699 FakeConstraints no_dtls_constraints;
700 no_dtls_constraints.AddMandatory(
701 webrtc::MediaConstraintsInterface::kEnableDtlsSrtp, false);
702
703 CreatePeerConnection(PeerConnectionInterface::RTCConfiguration(),
704 &no_dtls_constraints);
705 }
706
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000707 void CreatePeerConnection(webrtc::MediaConstraintsInterface* constraints) {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700708 CreatePeerConnection(PeerConnectionInterface::RTCConfiguration(),
709 constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000710 }
711
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700712 void CreatePeerConnectionWithIceTransportsType(
713 PeerConnectionInterface::IceTransportsType type) {
714 PeerConnectionInterface::RTCConfiguration config;
715 config.type = type;
716 return CreatePeerConnection(config, nullptr);
717 }
718
719 void CreatePeerConnectionWithIceServer(const std::string& uri,
720 const std::string& password) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800721 PeerConnectionInterface::RTCConfiguration config;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000722 PeerConnectionInterface::IceServer server;
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700723 server.uri = uri;
724 server.password = password;
725 config.servers.push_back(server);
726 CreatePeerConnection(config, nullptr);
727 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000728
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700729 void CreatePeerConnection(PeerConnectionInterface::RTCConfiguration config,
730 webrtc::MediaConstraintsInterface* constraints) {
kwibergd1fe2812016-04-27 06:47:29 -0700731 std::unique_ptr<cricket::FakePortAllocator> port_allocator(
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800732 new cricket::FakePortAllocator(rtc::Thread::Current(), nullptr));
733 port_allocator_ = port_allocator.get();
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000734
deadbeef1dcb1642017-03-29 21:08:16 -0700735 // Create certificate generator unless DTLS constraint is explicitly set to
736 // false.
Henrik Boströmd79599d2016-06-01 13:58:50 +0200737 std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator;
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000738 bool dtls;
739 if (FindConstraint(constraints,
740 webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
741 &dtls,
Henrik Boström5e56c592015-08-11 10:33:13 +0200742 nullptr) && dtls) {
deadbeef8662f942017-01-20 21:20:51 -0800743 fake_certificate_generator_ = new FakeRTCCertificateGenerator();
744 cert_generator.reset(fake_certificate_generator_);
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000745 }
Henrik Boströmd79599d2016-06-01 13:58:50 +0200746 pc_ = pc_factory_->CreatePeerConnection(
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800747 config, constraints, std::move(port_allocator),
Henrik Boströmd79599d2016-06-01 13:58:50 +0200748 std::move(cert_generator), &observer_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000749 ASSERT_TRUE(pc_.get() != NULL);
750 observer_.SetPeerConnectionInterface(pc_.get());
751 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
752 }
753
deadbeef0a6c4ca2015-10-06 11:38:28 -0700754 void CreatePeerConnectionExpectFail(const std::string& uri) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800755 PeerConnectionInterface::RTCConfiguration config;
deadbeef0a6c4ca2015-10-06 11:38:28 -0700756 PeerConnectionInterface::IceServer server;
deadbeef0a6c4ca2015-10-06 11:38:28 -0700757 server.uri = uri;
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800758 config.servers.push_back(server);
deadbeef0a6c4ca2015-10-06 11:38:28 -0700759
zhihuang9763d562016-08-05 11:14:50 -0700760 rtc::scoped_refptr<PeerConnectionInterface> pc;
hbosd7973cc2016-05-27 06:08:53 -0700761 pc = pc_factory_->CreatePeerConnection(config, nullptr, nullptr, nullptr,
762 &observer_);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800763 EXPECT_EQ(nullptr, pc);
deadbeef0a6c4ca2015-10-06 11:38:28 -0700764 }
765
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000766 void CreatePeerConnectionWithDifferentConfigurations() {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700767 CreatePeerConnectionWithIceServer(kStunAddressOnly, "");
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800768 EXPECT_EQ(1u, port_allocator_->stun_servers().size());
769 EXPECT_EQ(0u, port_allocator_->turn_servers().size());
770 EXPECT_EQ("address", port_allocator_->stun_servers().begin()->hostname());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000771 EXPECT_EQ(kDefaultStunPort,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800772 port_allocator_->stun_servers().begin()->port());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000773
deadbeef0a6c4ca2015-10-06 11:38:28 -0700774 CreatePeerConnectionExpectFail(kStunInvalidPort);
775 CreatePeerConnectionExpectFail(kStunAddressPortAndMore1);
776 CreatePeerConnectionExpectFail(kStunAddressPortAndMore2);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000777
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700778 CreatePeerConnectionWithIceServer(kTurnIceServerUri, kTurnPassword);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800779 EXPECT_EQ(0u, port_allocator_->stun_servers().size());
780 EXPECT_EQ(1u, port_allocator_->turn_servers().size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000781 EXPECT_EQ(kTurnUsername,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800782 port_allocator_->turn_servers()[0].credentials.username);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000783 EXPECT_EQ(kTurnPassword,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800784 port_allocator_->turn_servers()[0].credentials.password);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000785 EXPECT_EQ(kTurnHostname,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800786 port_allocator_->turn_servers()[0].ports[0].address.hostname());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000787 }
788
789 void ReleasePeerConnection() {
790 pc_ = NULL;
791 observer_.SetPeerConnectionInterface(NULL);
792 }
793
deadbeefab9b2d12015-10-14 11:33:11 -0700794 void AddVideoStream(const std::string& label) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000795 // Create a local stream.
zhihuang9763d562016-08-05 11:14:50 -0700796 rtc::scoped_refptr<MediaStreamInterface> stream(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000797 pc_factory_->CreateLocalMediaStream(label));
zhihuang9763d562016-08-05 11:14:50 -0700798 rtc::scoped_refptr<VideoTrackSourceInterface> video_source(
deadbeef112b2e92017-02-10 20:13:37 -0800799 pc_factory_->CreateVideoSource(std::unique_ptr<cricket::VideoCapturer>(
800 new cricket::FakeVideoCapturer()),
801 NULL));
zhihuang9763d562016-08-05 11:14:50 -0700802 rtc::scoped_refptr<VideoTrackInterface> video_track(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000803 pc_factory_->CreateVideoTrack(label + "v0", video_source));
804 stream->AddTrack(video_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +0000805 EXPECT_TRUE(pc_->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000806 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
807 observer_.renegotiation_needed_ = false;
808 }
809
810 void AddVoiceStream(const std::string& label) {
811 // Create a local stream.
zhihuang9763d562016-08-05 11:14:50 -0700812 rtc::scoped_refptr<MediaStreamInterface> stream(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000813 pc_factory_->CreateLocalMediaStream(label));
zhihuang9763d562016-08-05 11:14:50 -0700814 rtc::scoped_refptr<AudioTrackInterface> audio_track(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000815 pc_factory_->CreateAudioTrack(label + "a0", NULL));
816 stream->AddTrack(audio_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +0000817 EXPECT_TRUE(pc_->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000818 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
819 observer_.renegotiation_needed_ = false;
820 }
821
822 void AddAudioVideoStream(const std::string& stream_label,
823 const std::string& audio_track_label,
824 const std::string& video_track_label) {
825 // Create a local stream.
zhihuang9763d562016-08-05 11:14:50 -0700826 rtc::scoped_refptr<MediaStreamInterface> stream(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000827 pc_factory_->CreateLocalMediaStream(stream_label));
zhihuang9763d562016-08-05 11:14:50 -0700828 rtc::scoped_refptr<AudioTrackInterface> audio_track(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000829 pc_factory_->CreateAudioTrack(
830 audio_track_label, static_cast<AudioSourceInterface*>(NULL)));
831 stream->AddTrack(audio_track.get());
zhihuang9763d562016-08-05 11:14:50 -0700832 rtc::scoped_refptr<VideoTrackInterface> video_track(
nisseaf510af2016-03-21 08:20:42 -0700833 pc_factory_->CreateVideoTrack(
deadbeef112b2e92017-02-10 20:13:37 -0800834 video_track_label, pc_factory_->CreateVideoSource(
835 std::unique_ptr<cricket::VideoCapturer>(
836 new cricket::FakeVideoCapturer()))));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000837 stream->AddTrack(video_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +0000838 EXPECT_TRUE(pc_->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000839 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
840 observer_.renegotiation_needed_ = false;
841 }
842
kwibergd1fe2812016-04-27 06:47:29 -0700843 bool DoCreateOfferAnswer(std::unique_ptr<SessionDescriptionInterface>* desc,
deadbeefc80741f2015-10-22 13:14:45 -0700844 bool offer,
845 MediaConstraintsInterface* constraints) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000846 rtc::scoped_refptr<MockCreateSessionDescriptionObserver>
847 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000848 MockCreateSessionDescriptionObserver>());
849 if (offer) {
deadbeefc80741f2015-10-22 13:14:45 -0700850 pc_->CreateOffer(observer, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000851 } else {
deadbeefc80741f2015-10-22 13:14:45 -0700852 pc_->CreateAnswer(observer, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000853 }
854 EXPECT_EQ_WAIT(true, observer->called(), kTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -0700855 *desc = observer->MoveDescription();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000856 return observer->result();
857 }
858
kwibergd1fe2812016-04-27 06:47:29 -0700859 bool DoCreateOffer(std::unique_ptr<SessionDescriptionInterface>* desc,
deadbeefc80741f2015-10-22 13:14:45 -0700860 MediaConstraintsInterface* constraints) {
861 return DoCreateOfferAnswer(desc, true, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000862 }
863
kwibergd1fe2812016-04-27 06:47:29 -0700864 bool DoCreateAnswer(std::unique_ptr<SessionDescriptionInterface>* desc,
deadbeefc80741f2015-10-22 13:14:45 -0700865 MediaConstraintsInterface* constraints) {
866 return DoCreateOfferAnswer(desc, false, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000867 }
868
869 bool DoSetSessionDescription(SessionDescriptionInterface* desc, bool local) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000870 rtc::scoped_refptr<MockSetSessionDescriptionObserver>
871 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000872 MockSetSessionDescriptionObserver>());
873 if (local) {
874 pc_->SetLocalDescription(observer, desc);
875 } else {
876 pc_->SetRemoteDescription(observer, desc);
877 }
zhihuang29ff8442016-07-27 11:07:25 -0700878 if (pc_->signaling_state() != PeerConnectionInterface::kClosed) {
879 EXPECT_EQ_WAIT(true, observer->called(), kTimeout);
880 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000881 return observer->result();
882 }
883
884 bool DoSetLocalDescription(SessionDescriptionInterface* desc) {
885 return DoSetSessionDescription(desc, true);
886 }
887
888 bool DoSetRemoteDescription(SessionDescriptionInterface* desc) {
889 return DoSetSessionDescription(desc, false);
890 }
891
892 // Calls PeerConnection::GetStats and check the return value.
893 // It does not verify the values in the StatReports since a RTCP packet might
894 // be required.
895 bool DoGetStats(MediaStreamTrackInterface* track) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000896 rtc::scoped_refptr<MockStatsObserver> observer(
897 new rtc::RefCountedObject<MockStatsObserver>());
jiayl@webrtc.orgdb41b4d2014-03-03 21:30:06 +0000898 if (!pc_->GetStats(
899 observer, track, PeerConnectionInterface::kStatsOutputLevelStandard))
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000900 return false;
901 EXPECT_TRUE_WAIT(observer->called(), kTimeout);
902 return observer->called();
903 }
904
905 void InitiateCall() {
deadbeef293e9262017-01-11 12:28:30 -0800906 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000907 // Create a local stream with audio&video tracks.
908 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
909 CreateOfferReceiveAnswer();
910 }
911
912 // Verify that RTP Header extensions has been negotiated for audio and video.
913 void VerifyRemoteRtpHeaderExtensions() {
914 const cricket::MediaContentDescription* desc =
915 cricket::GetFirstAudioContentDescription(
916 pc_->remote_description()->description());
917 ASSERT_TRUE(desc != NULL);
918 EXPECT_GT(desc->rtp_header_extensions().size(), 0u);
919
920 desc = cricket::GetFirstVideoContentDescription(
921 pc_->remote_description()->description());
922 ASSERT_TRUE(desc != NULL);
923 EXPECT_GT(desc->rtp_header_extensions().size(), 0u);
924 }
925
926 void CreateOfferAsRemoteDescription() {
kwibergd1fe2812016-04-27 06:47:29 -0700927 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -0700928 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000929 std::string sdp;
930 EXPECT_TRUE(offer->ToString(&sdp));
931 SessionDescriptionInterface* remote_offer =
932 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
933 sdp, NULL);
934 EXPECT_TRUE(DoSetRemoteDescription(remote_offer));
935 EXPECT_EQ(PeerConnectionInterface::kHaveRemoteOffer, observer_.state_);
936 }
937
deadbeefab9b2d12015-10-14 11:33:11 -0700938 void CreateAndSetRemoteOffer(const std::string& sdp) {
939 SessionDescriptionInterface* remote_offer =
940 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
941 sdp, nullptr);
942 EXPECT_TRUE(DoSetRemoteDescription(remote_offer));
943 EXPECT_EQ(PeerConnectionInterface::kHaveRemoteOffer, observer_.state_);
944 }
945
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000946 void CreateAnswerAsLocalDescription() {
kwibergd1fe2812016-04-27 06:47:29 -0700947 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -0700948 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000949
950 // TODO(perkj): Currently SetLocalDescription fails if any parameters in an
951 // audio codec change, even if the parameter has nothing to do with
952 // receiving. Not all parameters are serialized to SDP.
953 // Since CreatePrAnswerAsLocalDescription serialize/deserialize
954 // the SessionDescription, it is necessary to do that here to in order to
955 // get ReceiveOfferCreatePrAnswerAndAnswer and RenegotiateAudioOnly to pass.
956 // https://code.google.com/p/webrtc/issues/detail?id=1356
957 std::string sdp;
958 EXPECT_TRUE(answer->ToString(&sdp));
959 SessionDescriptionInterface* new_answer =
960 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
961 sdp, NULL);
962 EXPECT_TRUE(DoSetLocalDescription(new_answer));
963 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
964 }
965
966 void CreatePrAnswerAsLocalDescription() {
kwibergd1fe2812016-04-27 06:47:29 -0700967 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -0700968 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000969
970 std::string sdp;
971 EXPECT_TRUE(answer->ToString(&sdp));
972 SessionDescriptionInterface* pr_answer =
973 webrtc::CreateSessionDescription(SessionDescriptionInterface::kPrAnswer,
974 sdp, NULL);
975 EXPECT_TRUE(DoSetLocalDescription(pr_answer));
976 EXPECT_EQ(PeerConnectionInterface::kHaveLocalPrAnswer, observer_.state_);
977 }
978
979 void CreateOfferReceiveAnswer() {
980 CreateOfferAsLocalDescription();
981 std::string sdp;
982 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
983 CreateAnswerAsRemoteDescription(sdp);
984 }
985
986 void CreateOfferAsLocalDescription() {
kwibergd1fe2812016-04-27 06:47:29 -0700987 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -0700988 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000989 // TODO(perkj): Currently SetLocalDescription fails if any parameters in an
990 // audio codec change, even if the parameter has nothing to do with
991 // receiving. Not all parameters are serialized to SDP.
992 // Since CreatePrAnswerAsLocalDescription serialize/deserialize
993 // the SessionDescription, it is necessary to do that here to in order to
994 // get ReceiveOfferCreatePrAnswerAndAnswer and RenegotiateAudioOnly to pass.
995 // https://code.google.com/p/webrtc/issues/detail?id=1356
996 std::string sdp;
997 EXPECT_TRUE(offer->ToString(&sdp));
998 SessionDescriptionInterface* new_offer =
999 webrtc::CreateSessionDescription(
1000 SessionDescriptionInterface::kOffer,
1001 sdp, NULL);
1002
1003 EXPECT_TRUE(DoSetLocalDescription(new_offer));
1004 EXPECT_EQ(PeerConnectionInterface::kHaveLocalOffer, observer_.state_);
mallinath@webrtc.org68cbd012014-01-22 00:16:46 +00001005 // Wait for the ice_complete message, so that SDP will have candidates.
1006 EXPECT_TRUE_WAIT(observer_.ice_complete_, kTimeout);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001007 }
1008
deadbeefab9b2d12015-10-14 11:33:11 -07001009 void CreateAnswerAsRemoteDescription(const std::string& sdp) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001010 webrtc::JsepSessionDescription* answer = new webrtc::JsepSessionDescription(
1011 SessionDescriptionInterface::kAnswer);
deadbeefab9b2d12015-10-14 11:33:11 -07001012 EXPECT_TRUE(answer->Initialize(sdp, NULL));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001013 EXPECT_TRUE(DoSetRemoteDescription(answer));
1014 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
1015 }
1016
deadbeefab9b2d12015-10-14 11:33:11 -07001017 void CreatePrAnswerAndAnswerAsRemoteDescription(const std::string& sdp) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001018 webrtc::JsepSessionDescription* pr_answer =
1019 new webrtc::JsepSessionDescription(
1020 SessionDescriptionInterface::kPrAnswer);
deadbeefab9b2d12015-10-14 11:33:11 -07001021 EXPECT_TRUE(pr_answer->Initialize(sdp, NULL));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001022 EXPECT_TRUE(DoSetRemoteDescription(pr_answer));
1023 EXPECT_EQ(PeerConnectionInterface::kHaveRemotePrAnswer, observer_.state_);
1024 webrtc::JsepSessionDescription* answer =
1025 new webrtc::JsepSessionDescription(
1026 SessionDescriptionInterface::kAnswer);
deadbeefab9b2d12015-10-14 11:33:11 -07001027 EXPECT_TRUE(answer->Initialize(sdp, NULL));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001028 EXPECT_TRUE(DoSetRemoteDescription(answer));
1029 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
1030 }
1031
1032 // Help function used for waiting until a the last signaled remote stream has
1033 // the same label as |stream_label|. In a few of the tests in this file we
1034 // answer with the same session description as we offer and thus we can
1035 // check if OnAddStream have been called with the same stream as we offer to
1036 // send.
1037 void WaitAndVerifyOnAddStream(const std::string& stream_label) {
1038 EXPECT_EQ_WAIT(stream_label, observer_.GetLastAddedStreamLabel(), kTimeout);
1039 }
1040
1041 // Creates an offer and applies it as a local session description.
1042 // Creates an answer with the same SDP an the offer but removes all lines
1043 // that start with a:ssrc"
1044 void CreateOfferReceiveAnswerWithoutSsrc() {
1045 CreateOfferAsLocalDescription();
1046 std::string sdp;
1047 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
1048 SetSsrcToZero(&sdp);
1049 CreateAnswerAsRemoteDescription(sdp);
1050 }
1051
deadbeefab9b2d12015-10-14 11:33:11 -07001052 // This function creates a MediaStream with label kStreams[0] and
1053 // |number_of_audio_tracks| and |number_of_video_tracks| tracks and the
1054 // corresponding SessionDescriptionInterface. The SessionDescriptionInterface
kwiberg2bbff992016-03-16 11:03:04 -07001055 // is returned and the MediaStream is stored in
deadbeefab9b2d12015-10-14 11:33:11 -07001056 // |reference_collection_|
kwibergd1fe2812016-04-27 06:47:29 -07001057 std::unique_ptr<SessionDescriptionInterface>
kwiberg2bbff992016-03-16 11:03:04 -07001058 CreateSessionDescriptionAndReference(size_t number_of_audio_tracks,
1059 size_t number_of_video_tracks) {
1060 EXPECT_LE(number_of_audio_tracks, 2u);
1061 EXPECT_LE(number_of_video_tracks, 2u);
deadbeefab9b2d12015-10-14 11:33:11 -07001062
1063 reference_collection_ = StreamCollection::Create();
1064 std::string sdp_ms1 = std::string(kSdpStringInit);
1065
1066 std::string mediastream_label = kStreams[0];
1067
1068 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream(
1069 webrtc::MediaStream::Create(mediastream_label));
1070 reference_collection_->AddStream(stream);
1071
1072 if (number_of_audio_tracks > 0) {
1073 sdp_ms1 += std::string(kSdpStringAudio);
1074 sdp_ms1 += std::string(kSdpStringMs1Audio0);
1075 AddAudioTrack(kAudioTracks[0], stream);
1076 }
1077 if (number_of_audio_tracks > 1) {
1078 sdp_ms1 += kSdpStringMs1Audio1;
1079 AddAudioTrack(kAudioTracks[1], stream);
1080 }
1081
1082 if (number_of_video_tracks > 0) {
1083 sdp_ms1 += std::string(kSdpStringVideo);
1084 sdp_ms1 += std::string(kSdpStringMs1Video0);
1085 AddVideoTrack(kVideoTracks[0], stream);
1086 }
1087 if (number_of_video_tracks > 1) {
1088 sdp_ms1 += kSdpStringMs1Video1;
1089 AddVideoTrack(kVideoTracks[1], stream);
1090 }
1091
kwibergd1fe2812016-04-27 06:47:29 -07001092 return std::unique_ptr<SessionDescriptionInterface>(
kwiberg2bbff992016-03-16 11:03:04 -07001093 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
1094 sdp_ms1, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07001095 }
1096
1097 void AddAudioTrack(const std::string& track_id,
1098 MediaStreamInterface* stream) {
1099 rtc::scoped_refptr<webrtc::AudioTrackInterface> audio_track(
1100 webrtc::AudioTrack::Create(track_id, nullptr));
1101 ASSERT_TRUE(stream->AddTrack(audio_track));
1102 }
1103
1104 void AddVideoTrack(const std::string& track_id,
1105 MediaStreamInterface* stream) {
1106 rtc::scoped_refptr<webrtc::VideoTrackInterface> video_track(
nisseaf510af2016-03-21 08:20:42 -07001107 webrtc::VideoTrack::Create(track_id,
1108 webrtc::FakeVideoTrackSource::Create()));
deadbeefab9b2d12015-10-14 11:33:11 -07001109 ASSERT_TRUE(stream->AddTrack(video_track));
1110 }
1111
kwibergfd8be342016-05-14 19:44:11 -07001112 std::unique_ptr<SessionDescriptionInterface> CreateOfferWithOneAudioStream() {
deadbeef293e9262017-01-11 12:28:30 -08001113 CreatePeerConnectionWithoutDtls();
zhihuang8f65cdf2016-05-06 18:40:30 -07001114 AddVoiceStream(kStreamLabel1);
kwibergfd8be342016-05-14 19:44:11 -07001115 std::unique_ptr<SessionDescriptionInterface> offer;
zhihuang8f65cdf2016-05-06 18:40:30 -07001116 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
1117 return offer;
1118 }
1119
kwibergfd8be342016-05-14 19:44:11 -07001120 std::unique_ptr<SessionDescriptionInterface>
zhihuang8f65cdf2016-05-06 18:40:30 -07001121 CreateAnswerWithOneAudioStream() {
kwibergfd8be342016-05-14 19:44:11 -07001122 std::unique_ptr<SessionDescriptionInterface> offer =
zhihuang8f65cdf2016-05-06 18:40:30 -07001123 CreateOfferWithOneAudioStream();
1124 EXPECT_TRUE(DoSetRemoteDescription(offer.release()));
kwibergfd8be342016-05-14 19:44:11 -07001125 std::unique_ptr<SessionDescriptionInterface> answer;
zhihuang8f65cdf2016-05-06 18:40:30 -07001126 EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
1127 return answer;
1128 }
1129
1130 const std::string& GetFirstAudioStreamCname(
1131 const SessionDescriptionInterface* desc) {
1132 const cricket::ContentInfo* audio_content =
1133 cricket::GetFirstAudioContent(desc->description());
1134 const cricket::AudioContentDescription* audio_desc =
1135 static_cast<const cricket::AudioContentDescription*>(
1136 audio_content->description);
1137 return audio_desc->streams()[0].cname;
1138 }
1139
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08001140 cricket::FakePortAllocator* port_allocator_ = nullptr;
deadbeef8662f942017-01-20 21:20:51 -08001141 FakeRTCCertificateGenerator* fake_certificate_generator_ = nullptr;
zhihuang9763d562016-08-05 11:14:50 -07001142 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface> pc_factory_;
1143 rtc::scoped_refptr<PeerConnectionFactoryForTest> pc_factory_for_test_;
1144 rtc::scoped_refptr<PeerConnectionInterface> pc_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001145 MockPeerConnectionObserver observer_;
deadbeefab9b2d12015-10-14 11:33:11 -07001146 rtc::scoped_refptr<StreamCollection> reference_collection_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001147};
1148
zhihuang29ff8442016-07-27 11:07:25 -07001149// Test that no callbacks on the PeerConnectionObserver are called after the
1150// PeerConnection is closed.
1151TEST_F(PeerConnectionInterfaceTest, CloseAndTestCallbackFunctions) {
zhihuang9763d562016-08-05 11:14:50 -07001152 rtc::scoped_refptr<PeerConnectionInterface> pc(
zhihuang29ff8442016-07-27 11:07:25 -07001153 pc_factory_for_test_->CreatePeerConnection(
1154 PeerConnectionInterface::RTCConfiguration(), nullptr, nullptr,
1155 nullptr, &observer_));
1156 observer_.SetPeerConnectionInterface(pc.get());
1157 pc->Close();
1158
1159 // No callbacks is expected to be called.
zhihuang81c3a032016-11-17 12:06:24 -08001160 observer_.callback_triggered_ = false;
zhihuang29ff8442016-07-27 11:07:25 -07001161 std::vector<cricket::Candidate> candidates;
1162 pc_factory_for_test_->transport_controller->SignalGatheringState(
1163 cricket::IceGatheringState{});
1164 pc_factory_for_test_->transport_controller->SignalCandidatesGathered(
1165 "", candidates);
1166 pc_factory_for_test_->transport_controller->SignalConnectionState(
1167 cricket::IceConnectionState{});
1168 pc_factory_for_test_->transport_controller->SignalCandidatesRemoved(
1169 candidates);
1170 pc_factory_for_test_->transport_controller->SignalReceiving(false);
zhihuang81c3a032016-11-17 12:06:24 -08001171 EXPECT_FALSE(observer_.callback_triggered_);
zhihuang29ff8442016-07-27 11:07:25 -07001172}
1173
zhihuang8f65cdf2016-05-06 18:40:30 -07001174// Generate different CNAMEs when PeerConnections are created.
1175// The CNAMEs are expected to be generated randomly. It is possible
1176// that the test fails, though the possibility is very low.
1177TEST_F(PeerConnectionInterfaceTest, CnameGenerationInOffer) {
kwibergfd8be342016-05-14 19:44:11 -07001178 std::unique_ptr<SessionDescriptionInterface> offer1 =
zhihuang8f65cdf2016-05-06 18:40:30 -07001179 CreateOfferWithOneAudioStream();
kwibergfd8be342016-05-14 19:44:11 -07001180 std::unique_ptr<SessionDescriptionInterface> offer2 =
zhihuang8f65cdf2016-05-06 18:40:30 -07001181 CreateOfferWithOneAudioStream();
1182 EXPECT_NE(GetFirstAudioStreamCname(offer1.get()),
1183 GetFirstAudioStreamCname(offer2.get()));
1184}
1185
1186TEST_F(PeerConnectionInterfaceTest, CnameGenerationInAnswer) {
kwibergfd8be342016-05-14 19:44:11 -07001187 std::unique_ptr<SessionDescriptionInterface> answer1 =
zhihuang8f65cdf2016-05-06 18:40:30 -07001188 CreateAnswerWithOneAudioStream();
kwibergfd8be342016-05-14 19:44:11 -07001189 std::unique_ptr<SessionDescriptionInterface> answer2 =
zhihuang8f65cdf2016-05-06 18:40:30 -07001190 CreateAnswerWithOneAudioStream();
1191 EXPECT_NE(GetFirstAudioStreamCname(answer1.get()),
1192 GetFirstAudioStreamCname(answer2.get()));
1193}
1194
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001195TEST_F(PeerConnectionInterfaceTest,
1196 CreatePeerConnectionWithDifferentConfigurations) {
1197 CreatePeerConnectionWithDifferentConfigurations();
1198}
1199
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001200TEST_F(PeerConnectionInterfaceTest,
1201 CreatePeerConnectionWithDifferentIceTransportsTypes) {
1202 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kNone);
1203 EXPECT_EQ(cricket::CF_NONE, port_allocator_->candidate_filter());
1204 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kRelay);
1205 EXPECT_EQ(cricket::CF_RELAY, port_allocator_->candidate_filter());
1206 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kNoHost);
1207 EXPECT_EQ(cricket::CF_ALL & ~cricket::CF_HOST,
1208 port_allocator_->candidate_filter());
1209 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kAll);
1210 EXPECT_EQ(cricket::CF_ALL, port_allocator_->candidate_filter());
1211}
1212
1213// Test that when a PeerConnection is created with a nonzero candidate pool
1214// size, the pooled PortAllocatorSession is created with all the attributes
1215// in the RTCConfiguration.
1216TEST_F(PeerConnectionInterfaceTest, CreatePeerConnectionWithPooledCandidates) {
1217 PeerConnectionInterface::RTCConfiguration config;
1218 PeerConnectionInterface::IceServer server;
1219 server.uri = kStunAddressOnly;
1220 config.servers.push_back(server);
1221 config.type = PeerConnectionInterface::kRelay;
1222 config.disable_ipv6 = true;
1223 config.tcp_candidate_policy =
1224 PeerConnectionInterface::kTcpCandidatePolicyDisabled;
honghaiz60347052016-05-31 18:29:12 -07001225 config.candidate_network_policy =
1226 PeerConnectionInterface::kCandidateNetworkPolicyLowCost;
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001227 config.ice_candidate_pool_size = 1;
1228 CreatePeerConnection(config, nullptr);
1229
1230 const cricket::FakePortAllocatorSession* session =
1231 static_cast<const cricket::FakePortAllocatorSession*>(
1232 port_allocator_->GetPooledSession());
1233 ASSERT_NE(nullptr, session);
1234 EXPECT_EQ(1UL, session->stun_servers().size());
1235 EXPECT_EQ(0U, session->flags() & cricket::PORTALLOCATOR_ENABLE_IPV6);
1236 EXPECT_LT(0U, session->flags() & cricket::PORTALLOCATOR_DISABLE_TCP);
honghaiz60347052016-05-31 18:29:12 -07001237 EXPECT_LT(0U,
1238 session->flags() & cricket::PORTALLOCATOR_DISABLE_COSTLY_NETWORKS);
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001239}
1240
Taylor Brandstetterf8e65772016-06-27 17:20:15 -07001241// Test that the PeerConnection initializes the port allocator passed into it,
1242// and on the correct thread.
1243TEST_F(PeerConnectionInterfaceTest,
1244 CreatePeerConnectionInitializesPortAllocator) {
1245 rtc::Thread network_thread;
1246 network_thread.Start();
1247 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface> pc_factory(
1248 webrtc::CreatePeerConnectionFactory(
1249 &network_thread, rtc::Thread::Current(), rtc::Thread::Current(),
1250 nullptr, nullptr, nullptr));
1251 std::unique_ptr<cricket::FakePortAllocator> port_allocator(
1252 new cricket::FakePortAllocator(&network_thread, nullptr));
1253 cricket::FakePortAllocator* raw_port_allocator = port_allocator.get();
1254 PeerConnectionInterface::RTCConfiguration config;
1255 rtc::scoped_refptr<PeerConnectionInterface> pc(
1256 pc_factory->CreatePeerConnection(
1257 config, nullptr, std::move(port_allocator), nullptr, &observer_));
1258 // FakePortAllocator RTC_CHECKs that it's initialized on the right thread,
1259 // so all we have to do here is check that it's initialized.
1260 EXPECT_TRUE(raw_port_allocator->initialized());
1261}
1262
deadbeef46c73892016-11-16 19:42:04 -08001263// Check that GetConfiguration returns the configuration the PeerConnection was
1264// constructed with, before SetConfiguration is called.
1265TEST_F(PeerConnectionInterfaceTest, GetConfigurationAfterCreatePeerConnection) {
1266 PeerConnectionInterface::RTCConfiguration config;
1267 config.type = PeerConnectionInterface::kRelay;
1268 CreatePeerConnection(config, nullptr);
1269
1270 PeerConnectionInterface::RTCConfiguration returned_config =
1271 pc_->GetConfiguration();
1272 EXPECT_EQ(PeerConnectionInterface::kRelay, returned_config.type);
1273}
1274
1275// Check that GetConfiguration returns the last configuration passed into
1276// SetConfiguration.
1277TEST_F(PeerConnectionInterfaceTest, GetConfigurationAfterSetConfiguration) {
1278 CreatePeerConnection();
1279
1280 PeerConnectionInterface::RTCConfiguration config;
1281 config.type = PeerConnectionInterface::kRelay;
1282 EXPECT_TRUE(pc_->SetConfiguration(config));
1283
1284 PeerConnectionInterface::RTCConfiguration returned_config =
1285 pc_->GetConfiguration();
1286 EXPECT_EQ(PeerConnectionInterface::kRelay, returned_config.type);
1287}
1288
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001289TEST_F(PeerConnectionInterfaceTest, AddStreams) {
deadbeef293e9262017-01-11 12:28:30 -08001290 CreatePeerConnectionWithoutDtls();
deadbeefab9b2d12015-10-14 11:33:11 -07001291 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001292 AddVoiceStream(kStreamLabel2);
1293 ASSERT_EQ(2u, pc_->local_streams()->count());
1294
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001295 // Test we can add multiple local streams to one peerconnection.
zhihuang9763d562016-08-05 11:14:50 -07001296 rtc::scoped_refptr<MediaStreamInterface> stream(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001297 pc_factory_->CreateLocalMediaStream(kStreamLabel3));
zhihuang9763d562016-08-05 11:14:50 -07001298 rtc::scoped_refptr<AudioTrackInterface> audio_track(
1299 pc_factory_->CreateAudioTrack(kStreamLabel3,
1300 static_cast<AudioSourceInterface*>(NULL)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001301 stream->AddTrack(audio_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +00001302 EXPECT_TRUE(pc_->AddStream(stream));
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001303 EXPECT_EQ(3u, pc_->local_streams()->count());
1304
1305 // Remove the third stream.
1306 pc_->RemoveStream(pc_->local_streams()->at(2));
1307 EXPECT_EQ(2u, pc_->local_streams()->count());
1308
1309 // Remove the second stream.
1310 pc_->RemoveStream(pc_->local_streams()->at(1));
1311 EXPECT_EQ(1u, pc_->local_streams()->count());
1312
1313 // Remove the first stream.
1314 pc_->RemoveStream(pc_->local_streams()->at(0));
1315 EXPECT_EQ(0u, pc_->local_streams()->count());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001316}
1317
deadbeefab9b2d12015-10-14 11:33:11 -07001318// Test that the created offer includes streams we added.
1319TEST_F(PeerConnectionInterfaceTest, AddedStreamsPresentInOffer) {
deadbeef293e9262017-01-11 12:28:30 -08001320 CreatePeerConnectionWithoutDtls();
deadbeefab9b2d12015-10-14 11:33:11 -07001321 AddAudioVideoStream(kStreamLabel1, "audio_track", "video_track");
kwibergd1fe2812016-04-27 06:47:29 -07001322 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001323 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07001324
1325 const cricket::ContentInfo* audio_content =
1326 cricket::GetFirstAudioContent(offer->description());
1327 const cricket::AudioContentDescription* audio_desc =
1328 static_cast<const cricket::AudioContentDescription*>(
1329 audio_content->description);
1330 EXPECT_TRUE(
1331 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
1332
1333 const cricket::ContentInfo* video_content =
1334 cricket::GetFirstVideoContent(offer->description());
1335 const cricket::VideoContentDescription* video_desc =
1336 static_cast<const cricket::VideoContentDescription*>(
1337 video_content->description);
1338 EXPECT_TRUE(
1339 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1340
1341 // Add another stream and ensure the offer includes both the old and new
1342 // streams.
1343 AddAudioVideoStream(kStreamLabel2, "audio_track2", "video_track2");
kwiberg2bbff992016-03-16 11:03:04 -07001344 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07001345
1346 audio_content = cricket::GetFirstAudioContent(offer->description());
1347 audio_desc = static_cast<const cricket::AudioContentDescription*>(
1348 audio_content->description);
1349 EXPECT_TRUE(
1350 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
1351 EXPECT_TRUE(
1352 ContainsTrack(audio_desc->streams(), kStreamLabel2, "audio_track2"));
1353
1354 video_content = cricket::GetFirstVideoContent(offer->description());
1355 video_desc = static_cast<const cricket::VideoContentDescription*>(
1356 video_content->description);
1357 EXPECT_TRUE(
1358 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1359 EXPECT_TRUE(
1360 ContainsTrack(video_desc->streams(), kStreamLabel2, "video_track2"));
1361}
1362
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001363TEST_F(PeerConnectionInterfaceTest, RemoveStream) {
deadbeef293e9262017-01-11 12:28:30 -08001364 CreatePeerConnectionWithoutDtls();
deadbeefab9b2d12015-10-14 11:33:11 -07001365 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001366 ASSERT_EQ(1u, pc_->local_streams()->count());
1367 pc_->RemoveStream(pc_->local_streams()->at(0));
1368 EXPECT_EQ(0u, pc_->local_streams()->count());
1369}
1370
deadbeefe1f9d832016-01-14 15:35:42 -08001371// Test for AddTrack and RemoveTrack methods.
1372// Tests that the created offer includes tracks we added,
1373// and that the RtpSenders are created correctly.
1374// Also tests that RemoveTrack removes the tracks from subsequent offers.
1375TEST_F(PeerConnectionInterfaceTest, AddTrackRemoveTrack) {
deadbeef293e9262017-01-11 12:28:30 -08001376 CreatePeerConnectionWithoutDtls();
deadbeefe1f9d832016-01-14 15:35:42 -08001377 // Create a dummy stream, so tracks share a stream label.
zhihuang9763d562016-08-05 11:14:50 -07001378 rtc::scoped_refptr<MediaStreamInterface> stream(
deadbeefe1f9d832016-01-14 15:35:42 -08001379 pc_factory_->CreateLocalMediaStream(kStreamLabel1));
1380 std::vector<MediaStreamInterface*> stream_list;
1381 stream_list.push_back(stream.get());
zhihuang9763d562016-08-05 11:14:50 -07001382 rtc::scoped_refptr<AudioTrackInterface> audio_track(
deadbeefe1f9d832016-01-14 15:35:42 -08001383 pc_factory_->CreateAudioTrack("audio_track", nullptr));
zhihuang9763d562016-08-05 11:14:50 -07001384 rtc::scoped_refptr<VideoTrackInterface> video_track(
1385 pc_factory_->CreateVideoTrack(
deadbeef112b2e92017-02-10 20:13:37 -08001386 "video_track", pc_factory_->CreateVideoSource(
1387 std::unique_ptr<cricket::VideoCapturer>(
1388 new cricket::FakeVideoCapturer()))));
deadbeefe1f9d832016-01-14 15:35:42 -08001389 auto audio_sender = pc_->AddTrack(audio_track, stream_list);
1390 auto video_sender = pc_->AddTrack(video_track, stream_list);
deadbeefa601f5c2016-06-06 14:27:39 -07001391 EXPECT_EQ(1UL, audio_sender->stream_ids().size());
1392 EXPECT_EQ(kStreamLabel1, audio_sender->stream_ids()[0]);
deadbeefe1f9d832016-01-14 15:35:42 -08001393 EXPECT_EQ("audio_track", audio_sender->id());
1394 EXPECT_EQ(audio_track, audio_sender->track());
deadbeefa601f5c2016-06-06 14:27:39 -07001395 EXPECT_EQ(1UL, video_sender->stream_ids().size());
1396 EXPECT_EQ(kStreamLabel1, video_sender->stream_ids()[0]);
deadbeefe1f9d832016-01-14 15:35:42 -08001397 EXPECT_EQ("video_track", video_sender->id());
1398 EXPECT_EQ(video_track, video_sender->track());
1399
1400 // Now create an offer and check for the senders.
kwibergd1fe2812016-04-27 06:47:29 -07001401 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001402 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefe1f9d832016-01-14 15:35:42 -08001403
1404 const cricket::ContentInfo* audio_content =
1405 cricket::GetFirstAudioContent(offer->description());
1406 const cricket::AudioContentDescription* audio_desc =
1407 static_cast<const cricket::AudioContentDescription*>(
1408 audio_content->description);
1409 EXPECT_TRUE(
1410 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
1411
1412 const cricket::ContentInfo* video_content =
1413 cricket::GetFirstVideoContent(offer->description());
1414 const cricket::VideoContentDescription* video_desc =
1415 static_cast<const cricket::VideoContentDescription*>(
1416 video_content->description);
1417 EXPECT_TRUE(
1418 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1419
1420 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
1421
1422 // Now try removing the tracks.
1423 EXPECT_TRUE(pc_->RemoveTrack(audio_sender));
1424 EXPECT_TRUE(pc_->RemoveTrack(video_sender));
1425
1426 // Create a new offer and ensure it doesn't contain the removed senders.
kwiberg2bbff992016-03-16 11:03:04 -07001427 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefe1f9d832016-01-14 15:35:42 -08001428
1429 audio_content = cricket::GetFirstAudioContent(offer->description());
1430 audio_desc = static_cast<const cricket::AudioContentDescription*>(
1431 audio_content->description);
1432 EXPECT_FALSE(
1433 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
1434
1435 video_content = cricket::GetFirstVideoContent(offer->description());
1436 video_desc = static_cast<const cricket::VideoContentDescription*>(
1437 video_content->description);
1438 EXPECT_FALSE(
1439 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1440
1441 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
1442
1443 // Calling RemoveTrack on a sender no longer attached to a PeerConnection
1444 // should return false.
1445 EXPECT_FALSE(pc_->RemoveTrack(audio_sender));
1446 EXPECT_FALSE(pc_->RemoveTrack(video_sender));
1447}
1448
1449// Test creating senders without a stream specified,
1450// expecting a random stream ID to be generated.
1451TEST_F(PeerConnectionInterfaceTest, AddTrackWithoutStream) {
deadbeef293e9262017-01-11 12:28:30 -08001452 CreatePeerConnectionWithoutDtls();
deadbeefe1f9d832016-01-14 15:35:42 -08001453 // Create a dummy stream, so tracks share a stream label.
zhihuang9763d562016-08-05 11:14:50 -07001454 rtc::scoped_refptr<AudioTrackInterface> audio_track(
deadbeefe1f9d832016-01-14 15:35:42 -08001455 pc_factory_->CreateAudioTrack("audio_track", nullptr));
zhihuang9763d562016-08-05 11:14:50 -07001456 rtc::scoped_refptr<VideoTrackInterface> video_track(
1457 pc_factory_->CreateVideoTrack(
deadbeef112b2e92017-02-10 20:13:37 -08001458 "video_track", pc_factory_->CreateVideoSource(
1459 std::unique_ptr<cricket::VideoCapturer>(
1460 new cricket::FakeVideoCapturer()))));
deadbeefe1f9d832016-01-14 15:35:42 -08001461 auto audio_sender =
1462 pc_->AddTrack(audio_track, std::vector<MediaStreamInterface*>());
1463 auto video_sender =
1464 pc_->AddTrack(video_track, std::vector<MediaStreamInterface*>());
1465 EXPECT_EQ("audio_track", audio_sender->id());
1466 EXPECT_EQ(audio_track, audio_sender->track());
1467 EXPECT_EQ("video_track", video_sender->id());
1468 EXPECT_EQ(video_track, video_sender->track());
1469 // If the ID is truly a random GUID, it should be infinitely unlikely they
1470 // will be the same.
deadbeefa601f5c2016-06-06 14:27:39 -07001471 EXPECT_NE(video_sender->stream_ids(), audio_sender->stream_ids());
deadbeefe1f9d832016-01-14 15:35:42 -08001472}
1473
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001474TEST_F(PeerConnectionInterfaceTest, CreateOfferReceiveAnswer) {
1475 InitiateCall();
1476 WaitAndVerifyOnAddStream(kStreamLabel1);
1477 VerifyRemoteRtpHeaderExtensions();
1478}
1479
1480TEST_F(PeerConnectionInterfaceTest, CreateOfferReceivePrAnswerAndAnswer) {
deadbeef293e9262017-01-11 12:28:30 -08001481 CreatePeerConnectionWithoutDtls();
deadbeefab9b2d12015-10-14 11:33:11 -07001482 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001483 CreateOfferAsLocalDescription();
1484 std::string offer;
1485 EXPECT_TRUE(pc_->local_description()->ToString(&offer));
1486 CreatePrAnswerAndAnswerAsRemoteDescription(offer);
1487 WaitAndVerifyOnAddStream(kStreamLabel1);
1488}
1489
1490TEST_F(PeerConnectionInterfaceTest, ReceiveOfferCreateAnswer) {
deadbeef293e9262017-01-11 12:28:30 -08001491 CreatePeerConnectionWithoutDtls();
deadbeefab9b2d12015-10-14 11:33:11 -07001492 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001493
1494 CreateOfferAsRemoteDescription();
1495 CreateAnswerAsLocalDescription();
1496
1497 WaitAndVerifyOnAddStream(kStreamLabel1);
1498}
1499
1500TEST_F(PeerConnectionInterfaceTest, ReceiveOfferCreatePrAnswerAndAnswer) {
deadbeef293e9262017-01-11 12:28:30 -08001501 CreatePeerConnectionWithoutDtls();
deadbeefab9b2d12015-10-14 11:33:11 -07001502 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001503
1504 CreateOfferAsRemoteDescription();
1505 CreatePrAnswerAsLocalDescription();
1506 CreateAnswerAsLocalDescription();
1507
1508 WaitAndVerifyOnAddStream(kStreamLabel1);
1509}
1510
1511TEST_F(PeerConnectionInterfaceTest, Renegotiate) {
1512 InitiateCall();
1513 ASSERT_EQ(1u, pc_->remote_streams()->count());
1514 pc_->RemoveStream(pc_->local_streams()->at(0));
1515 CreateOfferReceiveAnswer();
1516 EXPECT_EQ(0u, pc_->remote_streams()->count());
deadbeefab9b2d12015-10-14 11:33:11 -07001517 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001518 CreateOfferReceiveAnswer();
1519}
1520
1521// Tests that after negotiating an audio only call, the respondent can perform a
1522// renegotiation that removes the audio stream.
1523TEST_F(PeerConnectionInterfaceTest, RenegotiateAudioOnly) {
deadbeef293e9262017-01-11 12:28:30 -08001524 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001525 AddVoiceStream(kStreamLabel1);
1526 CreateOfferAsRemoteDescription();
1527 CreateAnswerAsLocalDescription();
1528
1529 ASSERT_EQ(1u, pc_->remote_streams()->count());
1530 pc_->RemoveStream(pc_->local_streams()->at(0));
1531 CreateOfferReceiveAnswer();
1532 EXPECT_EQ(0u, pc_->remote_streams()->count());
1533}
1534
1535// Test that candidates are generated and that we can parse our own candidates.
1536TEST_F(PeerConnectionInterfaceTest, IceCandidates) {
deadbeef293e9262017-01-11 12:28:30 -08001537 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001538
1539 EXPECT_FALSE(pc_->AddIceCandidate(observer_.last_candidate_.get()));
1540 // SetRemoteDescription takes ownership of offer.
kwibergd1fe2812016-04-27 06:47:29 -07001541 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefab9b2d12015-10-14 11:33:11 -07001542 AddVideoStream(kStreamLabel1);
deadbeefc80741f2015-10-22 13:14:45 -07001543 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
kwiberg2bbff992016-03-16 11:03:04 -07001544 EXPECT_TRUE(DoSetRemoteDescription(offer.release()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001545
1546 // SetLocalDescription takes ownership of answer.
kwibergd1fe2812016-04-27 06:47:29 -07001547 std::unique_ptr<SessionDescriptionInterface> answer;
deadbeefc80741f2015-10-22 13:14:45 -07001548 EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
kwiberg2bbff992016-03-16 11:03:04 -07001549 EXPECT_TRUE(DoSetLocalDescription(answer.release()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001550
1551 EXPECT_TRUE_WAIT(observer_.last_candidate_.get() != NULL, kTimeout);
1552 EXPECT_TRUE_WAIT(observer_.ice_complete_, kTimeout);
1553
1554 EXPECT_TRUE(pc_->AddIceCandidate(observer_.last_candidate_.get()));
1555}
1556
deadbeefab9b2d12015-10-14 11:33:11 -07001557// Test that CreateOffer and CreateAnswer will fail if the track labels are
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001558// not unique.
1559TEST_F(PeerConnectionInterfaceTest, CreateOfferAnswerWithInvalidStream) {
deadbeef293e9262017-01-11 12:28:30 -08001560 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001561 // Create a regular offer for the CreateAnswer test later.
kwibergd1fe2812016-04-27 06:47:29 -07001562 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefc80741f2015-10-22 13:14:45 -07001563 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
kwiberg2bbff992016-03-16 11:03:04 -07001564 EXPECT_TRUE(offer);
1565 offer.reset();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001566
1567 // Create a local stream with audio&video tracks having same label.
1568 AddAudioVideoStream(kStreamLabel1, "track_label", "track_label");
1569
1570 // Test CreateOffer
deadbeefc80741f2015-10-22 13:14:45 -07001571 EXPECT_FALSE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001572
1573 // Test CreateAnswer
kwibergd1fe2812016-04-27 06:47:29 -07001574 std::unique_ptr<SessionDescriptionInterface> answer;
deadbeefc80741f2015-10-22 13:14:45 -07001575 EXPECT_FALSE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001576}
1577
1578// Test that we will get different SSRCs for each tracks in the offer and answer
1579// we created.
1580TEST_F(PeerConnectionInterfaceTest, SsrcInOfferAnswer) {
deadbeef293e9262017-01-11 12:28:30 -08001581 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001582 // Create a local stream with audio&video tracks having different labels.
1583 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
1584
1585 // Test CreateOffer
kwibergd1fe2812016-04-27 06:47:29 -07001586 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001587 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001588 int audio_ssrc = 0;
1589 int video_ssrc = 0;
1590 EXPECT_TRUE(GetFirstSsrc(GetFirstAudioContent(offer->description()),
1591 &audio_ssrc));
1592 EXPECT_TRUE(GetFirstSsrc(GetFirstVideoContent(offer->description()),
1593 &video_ssrc));
1594 EXPECT_NE(audio_ssrc, video_ssrc);
1595
1596 // Test CreateAnswer
1597 EXPECT_TRUE(DoSetRemoteDescription(offer.release()));
kwibergd1fe2812016-04-27 06:47:29 -07001598 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -07001599 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001600 audio_ssrc = 0;
1601 video_ssrc = 0;
1602 EXPECT_TRUE(GetFirstSsrc(GetFirstAudioContent(answer->description()),
1603 &audio_ssrc));
1604 EXPECT_TRUE(GetFirstSsrc(GetFirstVideoContent(answer->description()),
1605 &video_ssrc));
1606 EXPECT_NE(audio_ssrc, video_ssrc);
1607}
1608
deadbeefeb459812015-12-15 19:24:43 -08001609// Test that it's possible to call AddTrack on a MediaStream after adding
1610// the stream to a PeerConnection.
1611// TODO(deadbeef): Remove this test once this behavior is no longer supported.
1612TEST_F(PeerConnectionInterfaceTest, AddTrackAfterAddStream) {
deadbeef293e9262017-01-11 12:28:30 -08001613 CreatePeerConnectionWithoutDtls();
deadbeefeb459812015-12-15 19:24:43 -08001614 // Create audio stream and add to PeerConnection.
1615 AddVoiceStream(kStreamLabel1);
1616 MediaStreamInterface* stream = pc_->local_streams()->at(0);
1617
1618 // Add video track to the audio-only stream.
zhihuang9763d562016-08-05 11:14:50 -07001619 rtc::scoped_refptr<VideoTrackInterface> video_track(
1620 pc_factory_->CreateVideoTrack(
deadbeef112b2e92017-02-10 20:13:37 -08001621 "video_label", pc_factory_->CreateVideoSource(
1622 std::unique_ptr<cricket::VideoCapturer>(
1623 new cricket::FakeVideoCapturer()))));
deadbeefeb459812015-12-15 19:24:43 -08001624 stream->AddTrack(video_track.get());
1625
kwibergd1fe2812016-04-27 06:47:29 -07001626 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001627 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefeb459812015-12-15 19:24:43 -08001628
1629 const cricket::MediaContentDescription* video_desc =
1630 cricket::GetFirstVideoContentDescription(offer->description());
1631 EXPECT_TRUE(video_desc != nullptr);
1632}
1633
1634// Test that it's possible to call RemoveTrack on a MediaStream after adding
1635// the stream to a PeerConnection.
1636// TODO(deadbeef): Remove this test once this behavior is no longer supported.
1637TEST_F(PeerConnectionInterfaceTest, RemoveTrackAfterAddStream) {
deadbeef293e9262017-01-11 12:28:30 -08001638 CreatePeerConnectionWithoutDtls();
deadbeefeb459812015-12-15 19:24:43 -08001639 // Create audio/video stream and add to PeerConnection.
1640 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
1641 MediaStreamInterface* stream = pc_->local_streams()->at(0);
1642
1643 // Remove the video track.
1644 stream->RemoveTrack(stream->GetVideoTracks()[0]);
1645
kwibergd1fe2812016-04-27 06:47:29 -07001646 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001647 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefeb459812015-12-15 19:24:43 -08001648
1649 const cricket::MediaContentDescription* video_desc =
1650 cricket::GetFirstVideoContentDescription(offer->description());
1651 EXPECT_TRUE(video_desc == nullptr);
1652}
1653
deadbeef1dcb1642017-03-29 21:08:16 -07001654// Verify that CreateDtmfSender only succeeds if called with a valid local
1655// track. Other aspects of DtmfSenders are tested in
1656// peerconnection_integrationtest.cc.
1657TEST_F(PeerConnectionInterfaceTest, CreateDtmfSenderWithInvalidParams) {
1658 CreatePeerConnection();
1659 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
1660 EXPECT_EQ(nullptr, pc_->CreateDtmfSender(nullptr));
1661 rtc::scoped_refptr<webrtc::AudioTrackInterface> non_localtrack(
1662 pc_factory_->CreateAudioTrack("dummy_track", nullptr));
1663 EXPECT_EQ(nullptr, pc_->CreateDtmfSender(non_localtrack));
1664}
1665
deadbeefbd7d8f72015-12-18 16:58:44 -08001666// Test creating a sender with a stream ID, and ensure the ID is populated
1667// in the offer.
1668TEST_F(PeerConnectionInterfaceTest, CreateSenderWithStream) {
deadbeef293e9262017-01-11 12:28:30 -08001669 CreatePeerConnectionWithoutDtls();
deadbeefbd7d8f72015-12-18 16:58:44 -08001670 pc_->CreateSender("video", kStreamLabel1);
1671
kwibergd1fe2812016-04-27 06:47:29 -07001672 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001673 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefbd7d8f72015-12-18 16:58:44 -08001674
1675 const cricket::MediaContentDescription* video_desc =
1676 cricket::GetFirstVideoContentDescription(offer->description());
1677 ASSERT_TRUE(video_desc != nullptr);
1678 ASSERT_EQ(1u, video_desc->streams().size());
1679 EXPECT_EQ(kStreamLabel1, video_desc->streams()[0].sync_label);
1680}
1681
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001682// Test that we can specify a certain track that we want statistics about.
1683TEST_F(PeerConnectionInterfaceTest, GetStatsForSpecificTrack) {
1684 InitiateCall();
1685 ASSERT_LT(0u, pc_->remote_streams()->count());
1686 ASSERT_LT(0u, pc_->remote_streams()->at(0)->GetAudioTracks().size());
zhihuang9763d562016-08-05 11:14:50 -07001687 rtc::scoped_refptr<MediaStreamTrackInterface> remote_audio =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001688 pc_->remote_streams()->at(0)->GetAudioTracks()[0];
1689 EXPECT_TRUE(DoGetStats(remote_audio));
1690
1691 // Remove the stream. Since we are sending to our selves the local
1692 // and the remote stream is the same.
1693 pc_->RemoveStream(pc_->local_streams()->at(0));
1694 // Do a re-negotiation.
1695 CreateOfferReceiveAnswer();
1696
1697 ASSERT_EQ(0u, pc_->remote_streams()->count());
1698
1699 // Test that we still can get statistics for the old track. Even if it is not
1700 // sent any longer.
1701 EXPECT_TRUE(DoGetStats(remote_audio));
1702}
1703
1704// Test that we can get stats on a video track.
1705TEST_F(PeerConnectionInterfaceTest, GetStatsForVideoTrack) {
1706 InitiateCall();
1707 ASSERT_LT(0u, pc_->remote_streams()->count());
1708 ASSERT_LT(0u, pc_->remote_streams()->at(0)->GetVideoTracks().size());
zhihuang9763d562016-08-05 11:14:50 -07001709 rtc::scoped_refptr<MediaStreamTrackInterface> remote_video =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001710 pc_->remote_streams()->at(0)->GetVideoTracks()[0];
1711 EXPECT_TRUE(DoGetStats(remote_video));
1712}
1713
1714// Test that we don't get statistics for an invalid track.
zhihuange9e94c32016-11-04 11:38:15 -07001715TEST_F(PeerConnectionInterfaceTest, GetStatsForInvalidTrack) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001716 InitiateCall();
zhihuang9763d562016-08-05 11:14:50 -07001717 rtc::scoped_refptr<AudioTrackInterface> unknown_audio_track(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001718 pc_factory_->CreateAudioTrack("unknown track", NULL));
1719 EXPECT_FALSE(DoGetStats(unknown_audio_track));
1720}
1721
1722// This test setup two RTP data channels in loop back.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001723TEST_F(PeerConnectionInterfaceTest, TestDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001724 FakeConstraints constraints;
1725 constraints.SetAllowRtpDataChannels();
1726 CreatePeerConnection(&constraints);
zhihuang9763d562016-08-05 11:14:50 -07001727 rtc::scoped_refptr<DataChannelInterface> data1 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001728 pc_->CreateDataChannel("test1", NULL);
zhihuang9763d562016-08-05 11:14:50 -07001729 rtc::scoped_refptr<DataChannelInterface> data2 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001730 pc_->CreateDataChannel("test2", NULL);
1731 ASSERT_TRUE(data1 != NULL);
kwibergd1fe2812016-04-27 06:47:29 -07001732 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001733 new MockDataChannelObserver(data1));
kwibergd1fe2812016-04-27 06:47:29 -07001734 std::unique_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001735 new MockDataChannelObserver(data2));
1736
1737 EXPECT_EQ(DataChannelInterface::kConnecting, data1->state());
1738 EXPECT_EQ(DataChannelInterface::kConnecting, data2->state());
1739 std::string data_to_send1 = "testing testing";
1740 std::string data_to_send2 = "testing something else";
1741 EXPECT_FALSE(data1->Send(DataBuffer(data_to_send1)));
1742
1743 CreateOfferReceiveAnswer();
1744 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1745 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
1746
1747 EXPECT_EQ(DataChannelInterface::kOpen, data1->state());
1748 EXPECT_EQ(DataChannelInterface::kOpen, data2->state());
1749 EXPECT_TRUE(data1->Send(DataBuffer(data_to_send1)));
1750 EXPECT_TRUE(data2->Send(DataBuffer(data_to_send2)));
1751
1752 EXPECT_EQ_WAIT(data_to_send1, observer1->last_message(), kTimeout);
1753 EXPECT_EQ_WAIT(data_to_send2, observer2->last_message(), kTimeout);
1754
1755 data1->Close();
1756 EXPECT_EQ(DataChannelInterface::kClosing, data1->state());
1757 CreateOfferReceiveAnswer();
1758 EXPECT_FALSE(observer1->IsOpen());
1759 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
1760 EXPECT_TRUE(observer2->IsOpen());
1761
1762 data_to_send2 = "testing something else again";
1763 EXPECT_TRUE(data2->Send(DataBuffer(data_to_send2)));
1764
1765 EXPECT_EQ_WAIT(data_to_send2, observer2->last_message(), kTimeout);
1766}
1767
1768// This test verifies that sendnig binary data over RTP data channels should
1769// fail.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001770TEST_F(PeerConnectionInterfaceTest, TestSendBinaryOnRtpDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001771 FakeConstraints constraints;
1772 constraints.SetAllowRtpDataChannels();
1773 CreatePeerConnection(&constraints);
zhihuang9763d562016-08-05 11:14:50 -07001774 rtc::scoped_refptr<DataChannelInterface> data1 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001775 pc_->CreateDataChannel("test1", NULL);
zhihuang9763d562016-08-05 11:14:50 -07001776 rtc::scoped_refptr<DataChannelInterface> data2 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001777 pc_->CreateDataChannel("test2", NULL);
1778 ASSERT_TRUE(data1 != NULL);
kwibergd1fe2812016-04-27 06:47:29 -07001779 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001780 new MockDataChannelObserver(data1));
kwibergd1fe2812016-04-27 06:47:29 -07001781 std::unique_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001782 new MockDataChannelObserver(data2));
1783
1784 EXPECT_EQ(DataChannelInterface::kConnecting, data1->state());
1785 EXPECT_EQ(DataChannelInterface::kConnecting, data2->state());
1786
1787 CreateOfferReceiveAnswer();
1788 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1789 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
1790
1791 EXPECT_EQ(DataChannelInterface::kOpen, data1->state());
1792 EXPECT_EQ(DataChannelInterface::kOpen, data2->state());
1793
jbaucheec21bd2016-03-20 06:15:43 -07001794 rtc::CopyOnWriteBuffer buffer("test", 4);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001795 EXPECT_FALSE(data1->Send(DataBuffer(buffer, true)));
1796}
1797
1798// This test setup a RTP data channels in loop back and test that a channel is
1799// opened even if the remote end answer with a zero SSRC.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001800TEST_F(PeerConnectionInterfaceTest, TestSendOnlyDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001801 FakeConstraints constraints;
1802 constraints.SetAllowRtpDataChannels();
1803 CreatePeerConnection(&constraints);
zhihuang9763d562016-08-05 11:14:50 -07001804 rtc::scoped_refptr<DataChannelInterface> data1 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001805 pc_->CreateDataChannel("test1", NULL);
kwibergd1fe2812016-04-27 06:47:29 -07001806 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001807 new MockDataChannelObserver(data1));
1808
1809 CreateOfferReceiveAnswerWithoutSsrc();
1810
1811 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1812
1813 data1->Close();
1814 EXPECT_EQ(DataChannelInterface::kClosing, data1->state());
1815 CreateOfferReceiveAnswerWithoutSsrc();
1816 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
1817 EXPECT_FALSE(observer1->IsOpen());
1818}
1819
1820// This test that if a data channel is added in an answer a receive only channel
1821// channel is created.
1822TEST_F(PeerConnectionInterfaceTest, TestReceiveOnlyDataChannel) {
1823 FakeConstraints constraints;
1824 constraints.SetAllowRtpDataChannels();
1825 CreatePeerConnection(&constraints);
1826
1827 std::string offer_label = "offer_channel";
zhihuang9763d562016-08-05 11:14:50 -07001828 rtc::scoped_refptr<DataChannelInterface> offer_channel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001829 pc_->CreateDataChannel(offer_label, NULL);
1830
1831 CreateOfferAsLocalDescription();
1832
1833 // Replace the data channel label in the offer and apply it as an answer.
1834 std::string receive_label = "answer_channel";
1835 std::string sdp;
1836 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001837 rtc::replace_substrs(offer_label.c_str(), offer_label.length(),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001838 receive_label.c_str(), receive_label.length(),
1839 &sdp);
1840 CreateAnswerAsRemoteDescription(sdp);
1841
1842 // Verify that a new incoming data channel has been created and that
1843 // it is open but can't we written to.
1844 ASSERT_TRUE(observer_.last_datachannel_ != NULL);
1845 DataChannelInterface* received_channel = observer_.last_datachannel_;
1846 EXPECT_EQ(DataChannelInterface::kConnecting, received_channel->state());
1847 EXPECT_EQ(receive_label, received_channel->label());
1848 EXPECT_FALSE(received_channel->Send(DataBuffer("something")));
1849
1850 // Verify that the channel we initially offered has been rejected.
1851 EXPECT_EQ(DataChannelInterface::kClosed, offer_channel->state());
1852
1853 // Do another offer / answer exchange and verify that the data channel is
1854 // opened.
1855 CreateOfferReceiveAnswer();
1856 EXPECT_EQ_WAIT(DataChannelInterface::kOpen, received_channel->state(),
1857 kTimeout);
1858}
1859
1860// This test that no data channel is returned if a reliable channel is
1861// requested.
1862// TODO(perkj): Remove this test once reliable channels are implemented.
1863TEST_F(PeerConnectionInterfaceTest, CreateReliableRtpDataChannelShouldFail) {
1864 FakeConstraints constraints;
1865 constraints.SetAllowRtpDataChannels();
1866 CreatePeerConnection(&constraints);
1867
1868 std::string label = "test";
1869 webrtc::DataChannelInit config;
1870 config.reliable = true;
zhihuang9763d562016-08-05 11:14:50 -07001871 rtc::scoped_refptr<DataChannelInterface> channel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001872 pc_->CreateDataChannel(label, &config);
1873 EXPECT_TRUE(channel == NULL);
1874}
1875
deadbeefab9b2d12015-10-14 11:33:11 -07001876// Verifies that duplicated label is not allowed for RTP data channel.
1877TEST_F(PeerConnectionInterfaceTest, RtpDuplicatedLabelNotAllowed) {
1878 FakeConstraints constraints;
1879 constraints.SetAllowRtpDataChannels();
1880 CreatePeerConnection(&constraints);
1881
1882 std::string label = "test";
zhihuang9763d562016-08-05 11:14:50 -07001883 rtc::scoped_refptr<DataChannelInterface> channel =
deadbeefab9b2d12015-10-14 11:33:11 -07001884 pc_->CreateDataChannel(label, nullptr);
1885 EXPECT_NE(channel, nullptr);
1886
zhihuang9763d562016-08-05 11:14:50 -07001887 rtc::scoped_refptr<DataChannelInterface> dup_channel =
deadbeefab9b2d12015-10-14 11:33:11 -07001888 pc_->CreateDataChannel(label, nullptr);
1889 EXPECT_EQ(dup_channel, nullptr);
1890}
1891
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001892// This tests that a SCTP data channel is returned using different
1893// DataChannelInit configurations.
1894TEST_F(PeerConnectionInterfaceTest, CreateSctpDataChannel) {
1895 FakeConstraints constraints;
1896 constraints.SetAllowDtlsSctpDataChannels();
1897 CreatePeerConnection(&constraints);
1898
1899 webrtc::DataChannelInit config;
1900
zhihuang9763d562016-08-05 11:14:50 -07001901 rtc::scoped_refptr<DataChannelInterface> channel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001902 pc_->CreateDataChannel("1", &config);
1903 EXPECT_TRUE(channel != NULL);
1904 EXPECT_TRUE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001905 EXPECT_TRUE(observer_.renegotiation_needed_);
1906 observer_.renegotiation_needed_ = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001907
1908 config.ordered = false;
1909 channel = pc_->CreateDataChannel("2", &config);
1910 EXPECT_TRUE(channel != NULL);
1911 EXPECT_TRUE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001912 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001913
1914 config.ordered = true;
1915 config.maxRetransmits = 0;
1916 channel = pc_->CreateDataChannel("3", &config);
1917 EXPECT_TRUE(channel != NULL);
1918 EXPECT_FALSE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001919 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001920
1921 config.maxRetransmits = -1;
1922 config.maxRetransmitTime = 0;
1923 channel = pc_->CreateDataChannel("4", &config);
1924 EXPECT_TRUE(channel != NULL);
1925 EXPECT_FALSE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001926 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001927}
1928
1929// This tests that no data channel is returned if both maxRetransmits and
1930// maxRetransmitTime are set for SCTP data channels.
1931TEST_F(PeerConnectionInterfaceTest,
1932 CreateSctpDataChannelShouldFailForInvalidConfig) {
1933 FakeConstraints constraints;
1934 constraints.SetAllowDtlsSctpDataChannels();
1935 CreatePeerConnection(&constraints);
1936
1937 std::string label = "test";
1938 webrtc::DataChannelInit config;
1939 config.maxRetransmits = 0;
1940 config.maxRetransmitTime = 0;
1941
zhihuang9763d562016-08-05 11:14:50 -07001942 rtc::scoped_refptr<DataChannelInterface> channel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001943 pc_->CreateDataChannel(label, &config);
1944 EXPECT_TRUE(channel == NULL);
1945}
1946
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001947// The test verifies that creating a SCTP data channel with an id already in use
1948// or out of range should fail.
1949TEST_F(PeerConnectionInterfaceTest,
1950 CreateSctpDataChannelWithInvalidIdShouldFail) {
1951 FakeConstraints constraints;
1952 constraints.SetAllowDtlsSctpDataChannels();
1953 CreatePeerConnection(&constraints);
1954
1955 webrtc::DataChannelInit config;
zhihuang9763d562016-08-05 11:14:50 -07001956 rtc::scoped_refptr<DataChannelInterface> channel;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001957
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001958 config.id = 1;
1959 channel = pc_->CreateDataChannel("1", &config);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001960 EXPECT_TRUE(channel != NULL);
1961 EXPECT_EQ(1, channel->id());
1962
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001963 channel = pc_->CreateDataChannel("x", &config);
1964 EXPECT_TRUE(channel == NULL);
1965
1966 config.id = cricket::kMaxSctpSid;
1967 channel = pc_->CreateDataChannel("max", &config);
1968 EXPECT_TRUE(channel != NULL);
1969 EXPECT_EQ(config.id, channel->id());
1970
1971 config.id = cricket::kMaxSctpSid + 1;
1972 channel = pc_->CreateDataChannel("x", &config);
1973 EXPECT_TRUE(channel == NULL);
1974}
1975
deadbeefab9b2d12015-10-14 11:33:11 -07001976// Verifies that duplicated label is allowed for SCTP data channel.
1977TEST_F(PeerConnectionInterfaceTest, SctpDuplicatedLabelAllowed) {
1978 FakeConstraints constraints;
1979 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
1980 true);
1981 CreatePeerConnection(&constraints);
1982
1983 std::string label = "test";
zhihuang9763d562016-08-05 11:14:50 -07001984 rtc::scoped_refptr<DataChannelInterface> channel =
deadbeefab9b2d12015-10-14 11:33:11 -07001985 pc_->CreateDataChannel(label, nullptr);
1986 EXPECT_NE(channel, nullptr);
1987
zhihuang9763d562016-08-05 11:14:50 -07001988 rtc::scoped_refptr<DataChannelInterface> dup_channel =
deadbeefab9b2d12015-10-14 11:33:11 -07001989 pc_->CreateDataChannel(label, nullptr);
1990 EXPECT_NE(dup_channel, nullptr);
1991}
1992
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001993// This test verifies that OnRenegotiationNeeded is fired for every new RTP
1994// DataChannel.
1995TEST_F(PeerConnectionInterfaceTest, RenegotiationNeededForNewRtpDataChannel) {
1996 FakeConstraints constraints;
1997 constraints.SetAllowRtpDataChannels();
1998 CreatePeerConnection(&constraints);
1999
zhihuang9763d562016-08-05 11:14:50 -07002000 rtc::scoped_refptr<DataChannelInterface> dc1 =
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00002001 pc_->CreateDataChannel("test1", NULL);
2002 EXPECT_TRUE(observer_.renegotiation_needed_);
2003 observer_.renegotiation_needed_ = false;
2004
zhihuang9763d562016-08-05 11:14:50 -07002005 rtc::scoped_refptr<DataChannelInterface> dc2 =
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00002006 pc_->CreateDataChannel("test2", NULL);
2007 EXPECT_TRUE(observer_.renegotiation_needed_);
2008}
2009
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002010// This test that a data channel closes when a PeerConnection is deleted/closed.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002011TEST_F(PeerConnectionInterfaceTest, DataChannelCloseWhenPeerConnectionClose) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002012 FakeConstraints constraints;
2013 constraints.SetAllowRtpDataChannels();
2014 CreatePeerConnection(&constraints);
2015
zhihuang9763d562016-08-05 11:14:50 -07002016 rtc::scoped_refptr<DataChannelInterface> data1 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002017 pc_->CreateDataChannel("test1", NULL);
zhihuang9763d562016-08-05 11:14:50 -07002018 rtc::scoped_refptr<DataChannelInterface> data2 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002019 pc_->CreateDataChannel("test2", NULL);
2020 ASSERT_TRUE(data1 != NULL);
kwibergd1fe2812016-04-27 06:47:29 -07002021 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002022 new MockDataChannelObserver(data1));
kwibergd1fe2812016-04-27 06:47:29 -07002023 std::unique_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002024 new MockDataChannelObserver(data2));
2025
2026 CreateOfferReceiveAnswer();
2027 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
2028 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
2029
2030 ReleasePeerConnection();
2031 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
2032 EXPECT_EQ(DataChannelInterface::kClosed, data2->state());
2033}
2034
2035// This test that data channels can be rejected in an answer.
2036TEST_F(PeerConnectionInterfaceTest, TestRejectDataChannelInAnswer) {
2037 FakeConstraints constraints;
2038 constraints.SetAllowRtpDataChannels();
2039 CreatePeerConnection(&constraints);
2040
zhihuang9763d562016-08-05 11:14:50 -07002041 rtc::scoped_refptr<DataChannelInterface> offer_channel(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002042 pc_->CreateDataChannel("offer_channel", NULL));
2043
2044 CreateOfferAsLocalDescription();
2045
2046 // Create an answer where the m-line for data channels are rejected.
2047 std::string sdp;
2048 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
2049 webrtc::JsepSessionDescription* answer = new webrtc::JsepSessionDescription(
2050 SessionDescriptionInterface::kAnswer);
2051 EXPECT_TRUE(answer->Initialize(sdp, NULL));
2052 cricket::ContentInfo* data_info =
2053 answer->description()->GetContentByName("data");
2054 data_info->rejected = true;
2055
2056 DoSetRemoteDescription(answer);
2057 EXPECT_EQ(DataChannelInterface::kClosed, offer_channel->state());
2058}
2059
2060// Test that we can create a session description from an SDP string from
2061// FireFox, use it as a remote session description, generate an answer and use
2062// the answer as a local description.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002063TEST_F(PeerConnectionInterfaceTest, ReceiveFireFoxOffer) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002064 FakeConstraints constraints;
2065 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2066 true);
2067 CreatePeerConnection(&constraints);
2068 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
2069 SessionDescriptionInterface* desc =
2070 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
jbauchfabe2c92015-07-16 13:43:14 -07002071 webrtc::kFireFoxSdpOffer, nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002072 EXPECT_TRUE(DoSetSessionDescription(desc, false));
2073 CreateAnswerAsLocalDescription();
2074 ASSERT_TRUE(pc_->local_description() != NULL);
2075 ASSERT_TRUE(pc_->remote_description() != NULL);
2076
2077 const cricket::ContentInfo* content =
2078 cricket::GetFirstAudioContent(pc_->local_description()->description());
2079 ASSERT_TRUE(content != NULL);
2080 EXPECT_FALSE(content->rejected);
2081
2082 content =
2083 cricket::GetFirstVideoContent(pc_->local_description()->description());
2084 ASSERT_TRUE(content != NULL);
2085 EXPECT_FALSE(content->rejected);
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00002086#ifdef HAVE_SCTP
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002087 content =
2088 cricket::GetFirstDataContent(pc_->local_description()->description());
2089 ASSERT_TRUE(content != NULL);
2090 EXPECT_TRUE(content->rejected);
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00002091#endif
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002092}
2093
deadbeef8662f942017-01-20 21:20:51 -08002094// Test that an offer can be received which offers DTLS with SDES fallback.
2095// Regression test for issue:
2096// https://bugs.chromium.org/p/webrtc/issues/detail?id=6972
2097TEST_F(PeerConnectionInterfaceTest, ReceiveDtlsSdesFallbackOffer) {
2098 FakeConstraints constraints;
2099 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2100 true);
2101 CreatePeerConnection(&constraints);
2102 // Wait for fake certificate to be generated. Previously, this is what caused
2103 // the "a=crypto" lines to be rejected.
2104 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
2105 ASSERT_NE(nullptr, fake_certificate_generator_);
2106 EXPECT_EQ_WAIT(1, fake_certificate_generator_->generated_certificates(),
2107 kTimeout);
2108 SessionDescriptionInterface* desc = webrtc::CreateSessionDescription(
2109 SessionDescriptionInterface::kOffer, kDtlsSdesFallbackSdp, nullptr);
2110 EXPECT_TRUE(DoSetSessionDescription(desc, false));
2111 CreateAnswerAsLocalDescription();
2112}
2113
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002114// Test that we can create an audio only offer and receive an answer with a
2115// limited set of audio codecs and receive an updated offer with more audio
2116// codecs, where the added codecs are not supported.
2117TEST_F(PeerConnectionInterfaceTest, ReceiveUpdatedAudioOfferWithBadCodecs) {
deadbeef293e9262017-01-11 12:28:30 -08002118 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002119 AddVoiceStream("audio_label");
2120 CreateOfferAsLocalDescription();
2121
2122 SessionDescriptionInterface* answer =
2123 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
jbauchfabe2c92015-07-16 13:43:14 -07002124 webrtc::kAudioSdp, nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002125 EXPECT_TRUE(DoSetSessionDescription(answer, false));
2126
2127 SessionDescriptionInterface* updated_offer =
2128 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
jbauchfabe2c92015-07-16 13:43:14 -07002129 webrtc::kAudioSdpWithUnsupportedCodecs,
2130 nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002131 EXPECT_TRUE(DoSetSessionDescription(updated_offer, false));
2132 CreateAnswerAsLocalDescription();
2133}
2134
deadbeefc80741f2015-10-22 13:14:45 -07002135// Test that if we're receiving (but not sending) a track, subsequent offers
2136// will have m-lines with a=recvonly.
2137TEST_F(PeerConnectionInterfaceTest, CreateSubsequentRecvOnlyOffer) {
2138 FakeConstraints constraints;
2139 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2140 true);
2141 CreatePeerConnection(&constraints);
2142 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2143 CreateAnswerAsLocalDescription();
2144
2145 // At this point we should be receiving stream 1, but not sending anything.
2146 // A new offer should be recvonly.
kwibergd1fe2812016-04-27 06:47:29 -07002147 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefc80741f2015-10-22 13:14:45 -07002148 DoCreateOffer(&offer, nullptr);
2149
2150 const cricket::ContentInfo* video_content =
2151 cricket::GetFirstVideoContent(offer->description());
2152 const cricket::VideoContentDescription* video_desc =
2153 static_cast<const cricket::VideoContentDescription*>(
2154 video_content->description);
2155 ASSERT_EQ(cricket::MD_RECVONLY, video_desc->direction());
2156
2157 const cricket::ContentInfo* audio_content =
2158 cricket::GetFirstAudioContent(offer->description());
2159 const cricket::AudioContentDescription* audio_desc =
2160 static_cast<const cricket::AudioContentDescription*>(
2161 audio_content->description);
2162 ASSERT_EQ(cricket::MD_RECVONLY, audio_desc->direction());
2163}
2164
2165// Test that if we're receiving (but not sending) a track, and the
2166// offerToReceiveVideo/offerToReceiveAudio constraints are explicitly set to
2167// false, the generated m-lines will be a=inactive.
2168TEST_F(PeerConnectionInterfaceTest, CreateSubsequentInactiveOffer) {
2169 FakeConstraints constraints;
2170 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2171 true);
2172 CreatePeerConnection(&constraints);
2173 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2174 CreateAnswerAsLocalDescription();
2175
2176 // At this point we should be receiving stream 1, but not sending anything.
2177 // A new offer would be recvonly, but we'll set the "no receive" constraints
2178 // to make it inactive.
kwibergd1fe2812016-04-27 06:47:29 -07002179 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefc80741f2015-10-22 13:14:45 -07002180 FakeConstraints offer_constraints;
2181 offer_constraints.AddMandatory(
2182 webrtc::MediaConstraintsInterface::kOfferToReceiveVideo, false);
2183 offer_constraints.AddMandatory(
2184 webrtc::MediaConstraintsInterface::kOfferToReceiveAudio, false);
2185 DoCreateOffer(&offer, &offer_constraints);
2186
2187 const cricket::ContentInfo* video_content =
2188 cricket::GetFirstVideoContent(offer->description());
2189 const cricket::VideoContentDescription* video_desc =
2190 static_cast<const cricket::VideoContentDescription*>(
2191 video_content->description);
2192 ASSERT_EQ(cricket::MD_INACTIVE, video_desc->direction());
2193
2194 const cricket::ContentInfo* audio_content =
2195 cricket::GetFirstAudioContent(offer->description());
2196 const cricket::AudioContentDescription* audio_desc =
2197 static_cast<const cricket::AudioContentDescription*>(
2198 audio_content->description);
2199 ASSERT_EQ(cricket::MD_INACTIVE, audio_desc->direction());
2200}
2201
deadbeef653b8e02015-11-11 12:55:10 -08002202// Test that we can use SetConfiguration to change the ICE servers of the
2203// PortAllocator.
2204TEST_F(PeerConnectionInterfaceTest, SetConfigurationChangesIceServers) {
2205 CreatePeerConnection();
2206
2207 PeerConnectionInterface::RTCConfiguration config;
2208 PeerConnectionInterface::IceServer server;
2209 server.uri = "stun:test_hostname";
2210 config.servers.push_back(server);
2211 EXPECT_TRUE(pc_->SetConfiguration(config));
2212
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002213 EXPECT_EQ(1u, port_allocator_->stun_servers().size());
2214 EXPECT_EQ("test_hostname",
2215 port_allocator_->stun_servers().begin()->hostname());
deadbeef653b8e02015-11-11 12:55:10 -08002216}
2217
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002218TEST_F(PeerConnectionInterfaceTest, SetConfigurationChangesCandidateFilter) {
2219 CreatePeerConnection();
2220 PeerConnectionInterface::RTCConfiguration config;
2221 config.type = PeerConnectionInterface::kRelay;
2222 EXPECT_TRUE(pc_->SetConfiguration(config));
2223 EXPECT_EQ(cricket::CF_RELAY, port_allocator_->candidate_filter());
2224}
2225
deadbeef293e9262017-01-11 12:28:30 -08002226TEST_F(PeerConnectionInterfaceTest, SetConfigurationChangesPruneTurnPortsFlag) {
2227 PeerConnectionInterface::RTCConfiguration config;
2228 config.prune_turn_ports = false;
2229 CreatePeerConnection(config, nullptr);
2230 EXPECT_FALSE(port_allocator_->prune_turn_ports());
2231
2232 config.prune_turn_ports = true;
2233 EXPECT_TRUE(pc_->SetConfiguration(config));
2234 EXPECT_TRUE(port_allocator_->prune_turn_ports());
2235}
2236
skvladd1f5fda2017-02-03 16:54:05 -08002237// Test that the ice check interval can be changed. This does not verify that
2238// the setting makes it all the way to P2PTransportChannel, as that would
2239// require a very complex set of mocks.
2240TEST_F(PeerConnectionInterfaceTest, SetConfigurationChangesIceCheckInterval) {
2241 PeerConnectionInterface::RTCConfiguration config;
2242 config.ice_check_min_interval = rtc::Optional<int>();
2243 CreatePeerConnection(config, nullptr);
2244 config.ice_check_min_interval = rtc::Optional<int>(100);
2245 EXPECT_TRUE(pc_->SetConfiguration(config));
2246 PeerConnectionInterface::RTCConfiguration new_config =
2247 pc_->GetConfiguration();
2248 EXPECT_EQ(new_config.ice_check_min_interval, rtc::Optional<int>(100));
2249}
2250
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002251// Test that when SetConfiguration changes both the pool size and other
2252// attributes, the pooled session is created with the updated attributes.
2253TEST_F(PeerConnectionInterfaceTest,
2254 SetConfigurationCreatesPooledSessionCorrectly) {
2255 CreatePeerConnection();
2256 PeerConnectionInterface::RTCConfiguration config;
2257 config.ice_candidate_pool_size = 1;
2258 PeerConnectionInterface::IceServer server;
2259 server.uri = kStunAddressOnly;
2260 config.servers.push_back(server);
2261 config.type = PeerConnectionInterface::kRelay;
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07002262 EXPECT_TRUE(pc_->SetConfiguration(config));
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002263
2264 const cricket::FakePortAllocatorSession* session =
2265 static_cast<const cricket::FakePortAllocatorSession*>(
2266 port_allocator_->GetPooledSession());
2267 ASSERT_NE(nullptr, session);
2268 EXPECT_EQ(1UL, session->stun_servers().size());
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002269}
2270
deadbeef293e9262017-01-11 12:28:30 -08002271// Test that after SetLocalDescription, changing the pool size is not allowed,
2272// and an invalid modification error is returned.
deadbeef6de92f92016-12-12 18:49:32 -08002273TEST_F(PeerConnectionInterfaceTest,
2274 CantChangePoolSizeAfterSetLocalDescription) {
2275 CreatePeerConnection();
2276 // Start by setting a size of 1.
2277 PeerConnectionInterface::RTCConfiguration config;
2278 config.ice_candidate_pool_size = 1;
2279 EXPECT_TRUE(pc_->SetConfiguration(config));
2280
2281 // Set remote offer; can still change pool size at this point.
2282 CreateOfferAsRemoteDescription();
2283 config.ice_candidate_pool_size = 2;
2284 EXPECT_TRUE(pc_->SetConfiguration(config));
2285
2286 // Set local answer; now it's too late.
2287 CreateAnswerAsLocalDescription();
2288 config.ice_candidate_pool_size = 3;
deadbeef293e9262017-01-11 12:28:30 -08002289 RTCError error;
2290 EXPECT_FALSE(pc_->SetConfiguration(config, &error));
2291 EXPECT_EQ(RTCErrorType::INVALID_MODIFICATION, error.type());
2292}
2293
deadbeef42a42632017-03-10 15:18:00 -08002294// Test that after setting an answer, extra pooled sessions are discarded. The
2295// ICE candidate pool is only intended to be used for the first offer/answer.
2296TEST_F(PeerConnectionInterfaceTest,
2297 ExtraPooledSessionsDiscardedAfterApplyingAnswer) {
2298 CreatePeerConnection();
2299
2300 // Set a larger-than-necessary size.
2301 PeerConnectionInterface::RTCConfiguration config;
2302 config.ice_candidate_pool_size = 4;
2303 EXPECT_TRUE(pc_->SetConfiguration(config));
2304
2305 // Do offer/answer.
2306 CreateOfferAsRemoteDescription();
2307 CreateAnswerAsLocalDescription();
2308
2309 // Expect no pooled sessions to be left.
2310 const cricket::PortAllocatorSession* session =
2311 port_allocator_->GetPooledSession();
2312 EXPECT_EQ(nullptr, session);
2313}
2314
2315// After Close is called, pooled candidates should be discarded so as to not
2316// waste network resources.
2317TEST_F(PeerConnectionInterfaceTest, PooledSessionsDiscardedAfterClose) {
2318 CreatePeerConnection();
2319
2320 PeerConnectionInterface::RTCConfiguration config;
2321 config.ice_candidate_pool_size = 3;
2322 EXPECT_TRUE(pc_->SetConfiguration(config));
2323 pc_->Close();
2324
2325 // Expect no pooled sessions to be left.
2326 const cricket::PortAllocatorSession* session =
2327 port_allocator_->GetPooledSession();
2328 EXPECT_EQ(nullptr, session);
2329}
2330
deadbeef293e9262017-01-11 12:28:30 -08002331// Test that SetConfiguration returns an invalid modification error if
2332// modifying a field in the configuration that isn't allowed to be modified.
2333TEST_F(PeerConnectionInterfaceTest,
2334 SetConfigurationReturnsInvalidModificationError) {
2335 PeerConnectionInterface::RTCConfiguration config;
2336 config.bundle_policy = PeerConnectionInterface::kBundlePolicyBalanced;
2337 config.rtcp_mux_policy = PeerConnectionInterface::kRtcpMuxPolicyNegotiate;
2338 config.continual_gathering_policy = PeerConnectionInterface::GATHER_ONCE;
2339 CreatePeerConnection(config, nullptr);
2340
2341 PeerConnectionInterface::RTCConfiguration modified_config = config;
2342 modified_config.bundle_policy =
2343 PeerConnectionInterface::kBundlePolicyMaxBundle;
2344 RTCError error;
2345 EXPECT_FALSE(pc_->SetConfiguration(modified_config, &error));
2346 EXPECT_EQ(RTCErrorType::INVALID_MODIFICATION, error.type());
2347
2348 modified_config = config;
2349 modified_config.rtcp_mux_policy =
2350 PeerConnectionInterface::kRtcpMuxPolicyRequire;
2351 error.set_type(RTCErrorType::NONE);
2352 EXPECT_FALSE(pc_->SetConfiguration(modified_config, &error));
2353 EXPECT_EQ(RTCErrorType::INVALID_MODIFICATION, error.type());
2354
2355 modified_config = config;
2356 modified_config.continual_gathering_policy =
2357 PeerConnectionInterface::GATHER_CONTINUALLY;
2358 error.set_type(RTCErrorType::NONE);
2359 EXPECT_FALSE(pc_->SetConfiguration(modified_config, &error));
2360 EXPECT_EQ(RTCErrorType::INVALID_MODIFICATION, error.type());
2361}
2362
2363// Test that SetConfiguration returns a range error if the candidate pool size
2364// is negative or larger than allowed by the spec.
2365TEST_F(PeerConnectionInterfaceTest,
2366 SetConfigurationReturnsRangeErrorForBadCandidatePoolSize) {
2367 PeerConnectionInterface::RTCConfiguration config;
2368 CreatePeerConnection(config, nullptr);
2369
2370 config.ice_candidate_pool_size = -1;
2371 RTCError error;
2372 EXPECT_FALSE(pc_->SetConfiguration(config, &error));
2373 EXPECT_EQ(RTCErrorType::INVALID_RANGE, error.type());
2374
2375 config.ice_candidate_pool_size = INT_MAX;
2376 error.set_type(RTCErrorType::NONE);
2377 EXPECT_FALSE(pc_->SetConfiguration(config, &error));
2378 EXPECT_EQ(RTCErrorType::INVALID_RANGE, error.type());
2379}
2380
2381// Test that SetConfiguration returns a syntax error if parsing an ICE server
2382// URL failed.
2383TEST_F(PeerConnectionInterfaceTest,
2384 SetConfigurationReturnsSyntaxErrorFromBadIceUrls) {
2385 PeerConnectionInterface::RTCConfiguration config;
2386 CreatePeerConnection(config, nullptr);
2387
2388 PeerConnectionInterface::IceServer bad_server;
2389 bad_server.uri = "stunn:www.example.com";
2390 config.servers.push_back(bad_server);
2391 RTCError error;
2392 EXPECT_FALSE(pc_->SetConfiguration(config, &error));
2393 EXPECT_EQ(RTCErrorType::SYNTAX_ERROR, error.type());
2394}
2395
2396// Test that SetConfiguration returns an invalid parameter error if a TURN
2397// IceServer is missing a username or password.
2398TEST_F(PeerConnectionInterfaceTest,
2399 SetConfigurationReturnsInvalidParameterIfCredentialsMissing) {
2400 PeerConnectionInterface::RTCConfiguration config;
2401 CreatePeerConnection(config, nullptr);
2402
2403 PeerConnectionInterface::IceServer bad_server;
2404 bad_server.uri = "turn:www.example.com";
2405 // Missing password.
2406 bad_server.username = "foo";
2407 config.servers.push_back(bad_server);
2408 RTCError error;
2409 EXPECT_FALSE(pc_->SetConfiguration(config, &error));
2410 EXPECT_EQ(RTCErrorType::INVALID_PARAMETER, error.type());
deadbeef6de92f92016-12-12 18:49:32 -08002411}
2412
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002413// Test that PeerConnection::Close changes the states to closed and all remote
2414// tracks change state to ended.
2415TEST_F(PeerConnectionInterfaceTest, CloseAndTestStreamsAndStates) {
2416 // Initialize a PeerConnection and negotiate local and remote session
2417 // description.
2418 InitiateCall();
2419 ASSERT_EQ(1u, pc_->local_streams()->count());
2420 ASSERT_EQ(1u, pc_->remote_streams()->count());
2421
2422 pc_->Close();
2423
2424 EXPECT_EQ(PeerConnectionInterface::kClosed, pc_->signaling_state());
2425 EXPECT_EQ(PeerConnectionInterface::kIceConnectionClosed,
2426 pc_->ice_connection_state());
2427 EXPECT_EQ(PeerConnectionInterface::kIceGatheringComplete,
2428 pc_->ice_gathering_state());
2429
2430 EXPECT_EQ(1u, pc_->local_streams()->count());
2431 EXPECT_EQ(1u, pc_->remote_streams()->count());
2432
zhihuang9763d562016-08-05 11:14:50 -07002433 rtc::scoped_refptr<MediaStreamInterface> remote_stream =
2434 pc_->remote_streams()->at(0);
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002435 // Track state may be updated asynchronously.
perkjd61bf802016-03-24 03:16:19 -07002436 EXPECT_EQ_WAIT(MediaStreamTrackInterface::kEnded,
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002437 remote_stream->GetAudioTracks()[0]->state(), kTimeout);
2438 EXPECT_EQ_WAIT(MediaStreamTrackInterface::kEnded,
2439 remote_stream->GetVideoTracks()[0]->state(), kTimeout);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002440}
2441
2442// Test that PeerConnection methods fails gracefully after
2443// PeerConnection::Close has been called.
2444TEST_F(PeerConnectionInterfaceTest, CloseAndTestMethods) {
deadbeef293e9262017-01-11 12:28:30 -08002445 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002446 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
2447 CreateOfferAsRemoteDescription();
2448 CreateAnswerAsLocalDescription();
2449
2450 ASSERT_EQ(1u, pc_->local_streams()->count());
zhihuang9763d562016-08-05 11:14:50 -07002451 rtc::scoped_refptr<MediaStreamInterface> local_stream =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002452 pc_->local_streams()->at(0);
2453
2454 pc_->Close();
2455
2456 pc_->RemoveStream(local_stream);
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +00002457 EXPECT_FALSE(pc_->AddStream(local_stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002458
2459 ASSERT_FALSE(local_stream->GetAudioTracks().empty());
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002460 rtc::scoped_refptr<webrtc::DtmfSenderInterface> dtmf_sender(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002461 pc_->CreateDtmfSender(local_stream->GetAudioTracks()[0]));
wu@webrtc.org66037362013-08-13 00:09:35 +00002462 EXPECT_TRUE(NULL == dtmf_sender); // local stream has been removed.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002463
2464 EXPECT_TRUE(pc_->CreateDataChannel("test", NULL) == NULL);
2465
2466 EXPECT_TRUE(pc_->local_description() != NULL);
2467 EXPECT_TRUE(pc_->remote_description() != NULL);
2468
kwibergd1fe2812016-04-27 06:47:29 -07002469 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07002470 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
kwibergd1fe2812016-04-27 06:47:29 -07002471 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -07002472 EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002473
2474 std::string sdp;
2475 ASSERT_TRUE(pc_->remote_description()->ToString(&sdp));
2476 SessionDescriptionInterface* remote_offer =
2477 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
2478 sdp, NULL);
2479 EXPECT_FALSE(DoSetRemoteDescription(remote_offer));
2480
2481 ASSERT_TRUE(pc_->local_description()->ToString(&sdp));
2482 SessionDescriptionInterface* local_offer =
2483 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
2484 sdp, NULL);
2485 EXPECT_FALSE(DoSetLocalDescription(local_offer));
2486}
2487
2488// Test that GetStats can still be called after PeerConnection::Close.
2489TEST_F(PeerConnectionInterfaceTest, CloseAndGetStats) {
2490 InitiateCall();
2491 pc_->Close();
2492 DoGetStats(NULL);
2493}
deadbeefab9b2d12015-10-14 11:33:11 -07002494
2495// NOTE: The series of tests below come from what used to be
2496// mediastreamsignaling_unittest.cc, and are mostly aimed at testing that
2497// setting a remote or local description has the expected effects.
2498
2499// This test verifies that the remote MediaStreams corresponding to a received
2500// SDP string is created. In this test the two separate MediaStreams are
2501// signaled.
2502TEST_F(PeerConnectionInterfaceTest, UpdateRemoteStreams) {
2503 FakeConstraints constraints;
2504 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2505 true);
2506 CreatePeerConnection(&constraints);
2507 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2508
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002509 rtc::scoped_refptr<StreamCollection> reference(CreateStreamCollection(1, 1));
deadbeefab9b2d12015-10-14 11:33:11 -07002510 EXPECT_TRUE(
2511 CompareStreamCollections(observer_.remote_streams(), reference.get()));
2512 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2513 EXPECT_TRUE(remote_stream->GetVideoTracks()[0]->GetSource() != nullptr);
2514
2515 // Create a session description based on another SDP with another
2516 // MediaStream.
2517 CreateAndSetRemoteOffer(kSdpStringWithStream1And2);
2518
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002519 rtc::scoped_refptr<StreamCollection> reference2(CreateStreamCollection(2, 1));
deadbeefab9b2d12015-10-14 11:33:11 -07002520 EXPECT_TRUE(
2521 CompareStreamCollections(observer_.remote_streams(), reference2.get()));
2522}
2523
2524// This test verifies that when remote tracks are added/removed from SDP, the
2525// created remote streams are updated appropriately.
2526TEST_F(PeerConnectionInterfaceTest,
2527 AddRemoveTrackFromExistingRemoteMediaStream) {
2528 FakeConstraints constraints;
2529 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2530 true);
2531 CreatePeerConnection(&constraints);
kwibergd1fe2812016-04-27 06:47:29 -07002532 std::unique_ptr<SessionDescriptionInterface> desc_ms1 =
kwiberg2bbff992016-03-16 11:03:04 -07002533 CreateSessionDescriptionAndReference(1, 1);
deadbeefab9b2d12015-10-14 11:33:11 -07002534 EXPECT_TRUE(DoSetRemoteDescription(desc_ms1.release()));
2535 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
2536 reference_collection_));
2537
2538 // Add extra audio and video tracks to the same MediaStream.
kwibergd1fe2812016-04-27 06:47:29 -07002539 std::unique_ptr<SessionDescriptionInterface> desc_ms1_two_tracks =
kwiberg2bbff992016-03-16 11:03:04 -07002540 CreateSessionDescriptionAndReference(2, 2);
deadbeefab9b2d12015-10-14 11:33:11 -07002541 EXPECT_TRUE(DoSetRemoteDescription(desc_ms1_two_tracks.release()));
2542 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
2543 reference_collection_));
zhihuang9763d562016-08-05 11:14:50 -07002544 rtc::scoped_refptr<AudioTrackInterface> audio_track2 =
perkjd61bf802016-03-24 03:16:19 -07002545 observer_.remote_streams()->at(0)->GetAudioTracks()[1];
2546 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, audio_track2->state());
zhihuang9763d562016-08-05 11:14:50 -07002547 rtc::scoped_refptr<VideoTrackInterface> video_track2 =
perkjd61bf802016-03-24 03:16:19 -07002548 observer_.remote_streams()->at(0)->GetVideoTracks()[1];
2549 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, video_track2->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002550
2551 // Remove the extra audio and video tracks.
kwibergd1fe2812016-04-27 06:47:29 -07002552 std::unique_ptr<SessionDescriptionInterface> desc_ms2 =
kwiberg2bbff992016-03-16 11:03:04 -07002553 CreateSessionDescriptionAndReference(1, 1);
perkjd61bf802016-03-24 03:16:19 -07002554 MockTrackObserver audio_track_observer(audio_track2);
2555 MockTrackObserver video_track_observer(video_track2);
2556
2557 EXPECT_CALL(audio_track_observer, OnChanged()).Times(Exactly(1));
2558 EXPECT_CALL(video_track_observer, OnChanged()).Times(Exactly(1));
deadbeefab9b2d12015-10-14 11:33:11 -07002559 EXPECT_TRUE(DoSetRemoteDescription(desc_ms2.release()));
2560 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
2561 reference_collection_));
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002562 // Track state may be updated asynchronously.
perkjd61bf802016-03-24 03:16:19 -07002563 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002564 audio_track2->state(), kTimeout);
2565 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
2566 video_track2->state(), kTimeout);
deadbeefab9b2d12015-10-14 11:33:11 -07002567}
2568
2569// This tests that remote tracks are ended if a local session description is set
2570// that rejects the media content type.
2571TEST_F(PeerConnectionInterfaceTest, RejectMediaContent) {
2572 FakeConstraints constraints;
2573 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2574 true);
2575 CreatePeerConnection(&constraints);
2576 // First create and set a remote offer, then reject its video content in our
2577 // answer.
2578 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2579 ASSERT_EQ(1u, observer_.remote_streams()->count());
2580 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2581 ASSERT_EQ(1u, remote_stream->GetVideoTracks().size());
2582 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2583
2584 rtc::scoped_refptr<webrtc::VideoTrackInterface> remote_video =
2585 remote_stream->GetVideoTracks()[0];
2586 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, remote_video->state());
2587 rtc::scoped_refptr<webrtc::AudioTrackInterface> remote_audio =
2588 remote_stream->GetAudioTracks()[0];
2589 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, remote_audio->state());
2590
kwibergd1fe2812016-04-27 06:47:29 -07002591 std::unique_ptr<SessionDescriptionInterface> local_answer;
kwiberg2bbff992016-03-16 11:03:04 -07002592 EXPECT_TRUE(DoCreateAnswer(&local_answer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07002593 cricket::ContentInfo* video_info =
2594 local_answer->description()->GetContentByName("video");
2595 video_info->rejected = true;
2596 EXPECT_TRUE(DoSetLocalDescription(local_answer.release()));
2597 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kEnded, remote_video->state());
2598 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, remote_audio->state());
2599
2600 // Now create an offer where we reject both video and audio.
kwibergd1fe2812016-04-27 06:47:29 -07002601 std::unique_ptr<SessionDescriptionInterface> local_offer;
kwiberg2bbff992016-03-16 11:03:04 -07002602 EXPECT_TRUE(DoCreateOffer(&local_offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07002603 video_info = local_offer->description()->GetContentByName("video");
2604 ASSERT_TRUE(video_info != nullptr);
2605 video_info->rejected = true;
2606 cricket::ContentInfo* audio_info =
2607 local_offer->description()->GetContentByName("audio");
2608 ASSERT_TRUE(audio_info != nullptr);
2609 audio_info->rejected = true;
2610 EXPECT_TRUE(DoSetLocalDescription(local_offer.release()));
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002611 // Track state may be updated asynchronously.
perkjd61bf802016-03-24 03:16:19 -07002612 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002613 remote_audio->state(), kTimeout);
2614 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
2615 remote_video->state(), kTimeout);
deadbeefab9b2d12015-10-14 11:33:11 -07002616}
2617
2618// This tests that we won't crash if the remote track has been removed outside
2619// of PeerConnection and then PeerConnection tries to reject the track.
2620TEST_F(PeerConnectionInterfaceTest, RemoveTrackThenRejectMediaContent) {
2621 FakeConstraints constraints;
2622 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2623 true);
2624 CreatePeerConnection(&constraints);
2625 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2626 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2627 remote_stream->RemoveTrack(remote_stream->GetVideoTracks()[0]);
2628 remote_stream->RemoveTrack(remote_stream->GetAudioTracks()[0]);
2629
kwibergd1fe2812016-04-27 06:47:29 -07002630 std::unique_ptr<SessionDescriptionInterface> local_answer(
deadbeefab9b2d12015-10-14 11:33:11 -07002631 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
2632 kSdpStringWithStream1, nullptr));
2633 cricket::ContentInfo* video_info =
2634 local_answer->description()->GetContentByName("video");
2635 video_info->rejected = true;
2636 cricket::ContentInfo* audio_info =
2637 local_answer->description()->GetContentByName("audio");
2638 audio_info->rejected = true;
2639 EXPECT_TRUE(DoSetLocalDescription(local_answer.release()));
2640
2641 // No crash is a pass.
2642}
2643
deadbeef5e97fb52015-10-15 12:49:08 -07002644// This tests that if a recvonly remote description is set, no remote streams
2645// will be created, even if the description contains SSRCs/MSIDs.
2646// See: https://code.google.com/p/webrtc/issues/detail?id=5054
2647TEST_F(PeerConnectionInterfaceTest, RecvonlyDescriptionDoesntCreateStream) {
2648 FakeConstraints constraints;
2649 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2650 true);
2651 CreatePeerConnection(&constraints);
2652
2653 std::string recvonly_offer = kSdpStringWithStream1;
2654 rtc::replace_substrs(kSendrecv, strlen(kSendrecv), kRecvonly,
2655 strlen(kRecvonly), &recvonly_offer);
2656 CreateAndSetRemoteOffer(recvonly_offer);
2657
2658 EXPECT_EQ(0u, observer_.remote_streams()->count());
2659}
2660
deadbeefab9b2d12015-10-14 11:33:11 -07002661// This tests that a default MediaStream is created if a remote session
2662// description doesn't contain any streams and no MSID support.
2663// It also tests that the default stream is updated if a video m-line is added
2664// in a subsequent session description.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002665TEST_F(PeerConnectionInterfaceTest, SdpWithoutMsidCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002666 FakeConstraints constraints;
2667 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2668 true);
2669 CreatePeerConnection(&constraints);
2670 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2671
2672 ASSERT_EQ(1u, observer_.remote_streams()->count());
2673 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2674
2675 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2676 EXPECT_EQ(0u, remote_stream->GetVideoTracks().size());
2677 EXPECT_EQ("default", remote_stream->label());
2678
2679 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2680 ASSERT_EQ(1u, observer_.remote_streams()->count());
2681 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2682 EXPECT_EQ("defaulta0", remote_stream->GetAudioTracks()[0]->id());
deadbeef884f5852016-01-15 09:20:04 -08002683 EXPECT_EQ(MediaStreamTrackInterface::kLive,
2684 remote_stream->GetAudioTracks()[0]->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002685 ASSERT_EQ(1u, remote_stream->GetVideoTracks().size());
2686 EXPECT_EQ("defaultv0", remote_stream->GetVideoTracks()[0]->id());
deadbeef884f5852016-01-15 09:20:04 -08002687 EXPECT_EQ(MediaStreamTrackInterface::kLive,
2688 remote_stream->GetVideoTracks()[0]->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002689}
2690
2691// This tests that a default MediaStream is created if a remote session
2692// description doesn't contain any streams and media direction is send only.
2693TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002694 SendOnlySdpWithoutMsidCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002695 FakeConstraints constraints;
2696 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2697 true);
2698 CreatePeerConnection(&constraints);
2699 CreateAndSetRemoteOffer(kSdpStringSendOnlyWithoutStreams);
2700
2701 ASSERT_EQ(1u, observer_.remote_streams()->count());
2702 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2703
2704 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2705 EXPECT_EQ(1u, remote_stream->GetVideoTracks().size());
2706 EXPECT_EQ("default", remote_stream->label());
2707}
2708
2709// This tests that it won't crash when PeerConnection tries to remove
2710// a remote track that as already been removed from the MediaStream.
2711TEST_F(PeerConnectionInterfaceTest, RemoveAlreadyGoneRemoteStream) {
2712 FakeConstraints constraints;
2713 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2714 true);
2715 CreatePeerConnection(&constraints);
2716 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2717 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2718 remote_stream->RemoveTrack(remote_stream->GetAudioTracks()[0]);
2719 remote_stream->RemoveTrack(remote_stream->GetVideoTracks()[0]);
2720
2721 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2722
2723 // No crash is a pass.
2724}
2725
2726// This tests that a default MediaStream is created if the remote session
2727// description doesn't contain any streams and don't contain an indication if
2728// MSID is supported.
2729TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002730 SdpWithoutMsidAndStreamsCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002731 FakeConstraints constraints;
2732 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2733 true);
2734 CreatePeerConnection(&constraints);
2735 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2736
2737 ASSERT_EQ(1u, observer_.remote_streams()->count());
2738 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2739 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2740 EXPECT_EQ(1u, remote_stream->GetVideoTracks().size());
2741}
2742
2743// This tests that a default MediaStream is not created if the remote session
2744// description doesn't contain any streams but does support MSID.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002745TEST_F(PeerConnectionInterfaceTest, SdpWithMsidDontCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002746 FakeConstraints constraints;
2747 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2748 true);
2749 CreatePeerConnection(&constraints);
2750 CreateAndSetRemoteOffer(kSdpStringWithMsidWithoutStreams);
2751 EXPECT_EQ(0u, observer_.remote_streams()->count());
2752}
2753
deadbeefbda7e0b2015-12-08 17:13:40 -08002754// This tests that when setting a new description, the old default tracks are
2755// not destroyed and recreated.
2756// See: https://bugs.chromium.org/p/webrtc/issues/detail?id=5250
Stefan Holmer102362b2016-03-18 09:39:07 +01002757TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002758 DefaultTracksNotDestroyedAndRecreated) {
deadbeefbda7e0b2015-12-08 17:13:40 -08002759 FakeConstraints constraints;
2760 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2761 true);
2762 CreatePeerConnection(&constraints);
2763 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2764
2765 ASSERT_EQ(1u, observer_.remote_streams()->count());
2766 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2767 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2768
2769 // Set the track to "disabled", then set a new description and ensure the
2770 // track is still disabled, which ensures it hasn't been recreated.
2771 remote_stream->GetAudioTracks()[0]->set_enabled(false);
2772 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2773 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2774 EXPECT_FALSE(remote_stream->GetAudioTracks()[0]->enabled());
2775}
2776
deadbeefab9b2d12015-10-14 11:33:11 -07002777// This tests that a default MediaStream is not created if a remote session
2778// description is updated to not have any MediaStreams.
2779TEST_F(PeerConnectionInterfaceTest, VerifyDefaultStreamIsNotCreated) {
2780 FakeConstraints constraints;
2781 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2782 true);
2783 CreatePeerConnection(&constraints);
2784 CreateAndSetRemoteOffer(kSdpStringWithStream1);
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002785 rtc::scoped_refptr<StreamCollection> reference(CreateStreamCollection(1, 1));
deadbeefab9b2d12015-10-14 11:33:11 -07002786 EXPECT_TRUE(
2787 CompareStreamCollections(observer_.remote_streams(), reference.get()));
2788
2789 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2790 EXPECT_EQ(0u, observer_.remote_streams()->count());
2791}
2792
2793// This tests that an RtpSender is created when the local description is set
2794// after adding a local stream.
2795// TODO(deadbeef): This test and the one below it need to be updated when
2796// an RtpSender's lifetime isn't determined by when a local description is set.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002797TEST_F(PeerConnectionInterfaceTest, LocalDescriptionChanged) {
deadbeefab9b2d12015-10-14 11:33:11 -07002798 FakeConstraints constraints;
2799 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2800 true);
2801 CreatePeerConnection(&constraints);
deadbeefab9b2d12015-10-14 11:33:11 -07002802
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002803 // Create an offer with 1 stream with 2 tracks of each type.
2804 rtc::scoped_refptr<StreamCollection> stream_collection =
2805 CreateStreamCollection(1, 2);
2806 pc_->AddStream(stream_collection->at(0));
2807 std::unique_ptr<SessionDescriptionInterface> offer;
2808 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2809 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
deadbeefab9b2d12015-10-14 11:33:11 -07002810
deadbeefab9b2d12015-10-14 11:33:11 -07002811 auto senders = pc_->GetSenders();
2812 EXPECT_EQ(4u, senders.size());
2813 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2814 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2815 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[1]));
2816 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[1]));
2817
2818 // Remove an audio and video track.
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002819 pc_->RemoveStream(stream_collection->at(0));
2820 stream_collection = CreateStreamCollection(1, 1);
2821 pc_->AddStream(stream_collection->at(0));
2822 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2823 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
2824
deadbeefab9b2d12015-10-14 11:33:11 -07002825 senders = pc_->GetSenders();
2826 EXPECT_EQ(2u, senders.size());
2827 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2828 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2829 EXPECT_FALSE(ContainsSender(senders, kAudioTracks[1]));
2830 EXPECT_FALSE(ContainsSender(senders, kVideoTracks[1]));
2831}
2832
2833// This tests that an RtpSender is created when the local description is set
2834// before adding a local stream.
2835TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002836 AddLocalStreamAfterLocalDescriptionChanged) {
deadbeefab9b2d12015-10-14 11:33:11 -07002837 FakeConstraints constraints;
2838 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2839 true);
2840 CreatePeerConnection(&constraints);
deadbeefab9b2d12015-10-14 11:33:11 -07002841
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002842 rtc::scoped_refptr<StreamCollection> stream_collection =
2843 CreateStreamCollection(1, 2);
2844 // Add a stream to create the offer, but remove it afterwards.
2845 pc_->AddStream(stream_collection->at(0));
2846 std::unique_ptr<SessionDescriptionInterface> offer;
2847 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2848 pc_->RemoveStream(stream_collection->at(0));
deadbeefab9b2d12015-10-14 11:33:11 -07002849
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002850 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
deadbeefab9b2d12015-10-14 11:33:11 -07002851 auto senders = pc_->GetSenders();
2852 EXPECT_EQ(0u, senders.size());
2853
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002854 pc_->AddStream(stream_collection->at(0));
deadbeefab9b2d12015-10-14 11:33:11 -07002855 senders = pc_->GetSenders();
2856 EXPECT_EQ(4u, senders.size());
2857 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2858 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2859 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[1]));
2860 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[1]));
2861}
2862
2863// This tests that the expected behavior occurs if the SSRC on a local track is
2864// changed when SetLocalDescription is called.
2865TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002866 ChangeSsrcOnTrackInLocalSessionDescription) {
deadbeefab9b2d12015-10-14 11:33:11 -07002867 FakeConstraints constraints;
2868 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2869 true);
2870 CreatePeerConnection(&constraints);
deadbeefab9b2d12015-10-14 11:33:11 -07002871
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002872 rtc::scoped_refptr<StreamCollection> stream_collection =
2873 CreateStreamCollection(2, 1);
2874 pc_->AddStream(stream_collection->at(0));
2875 std::unique_ptr<SessionDescriptionInterface> offer;
2876 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2877 // Grab a copy of the offer before it gets passed into the PC.
2878 std::unique_ptr<JsepSessionDescription> modified_offer(
2879 new JsepSessionDescription(JsepSessionDescription::kOffer));
2880 modified_offer->Initialize(offer->description()->Copy(), offer->session_id(),
2881 offer->session_version());
2882 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
deadbeefab9b2d12015-10-14 11:33:11 -07002883
deadbeefab9b2d12015-10-14 11:33:11 -07002884 auto senders = pc_->GetSenders();
2885 EXPECT_EQ(2u, senders.size());
2886 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2887 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2888
2889 // Change the ssrc of the audio and video track.
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002890 cricket::MediaContentDescription* desc =
2891 cricket::GetFirstAudioContentDescription(modified_offer->description());
2892 ASSERT_TRUE(desc != NULL);
2893 for (StreamParams& stream : desc->mutable_streams()) {
2894 for (unsigned int& ssrc : stream.ssrcs) {
2895 ++ssrc;
2896 }
2897 }
deadbeefab9b2d12015-10-14 11:33:11 -07002898
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002899 desc =
2900 cricket::GetFirstVideoContentDescription(modified_offer->description());
2901 ASSERT_TRUE(desc != NULL);
2902 for (StreamParams& stream : desc->mutable_streams()) {
2903 for (unsigned int& ssrc : stream.ssrcs) {
2904 ++ssrc;
2905 }
2906 }
2907
2908 EXPECT_TRUE(DoSetLocalDescription(modified_offer.release()));
deadbeefab9b2d12015-10-14 11:33:11 -07002909 senders = pc_->GetSenders();
2910 EXPECT_EQ(2u, senders.size());
2911 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2912 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2913 // TODO(deadbeef): Once RtpSenders expose parameters, check that the SSRC
2914 // changed.
2915}
2916
2917// This tests that the expected behavior occurs if a new session description is
2918// set with the same tracks, but on a different MediaStream.
Stefan Holmer55d6e7c2016-03-17 16:26:40 +01002919TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002920 SignalSameTracksInSeparateMediaStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002921 FakeConstraints constraints;
2922 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2923 true);
2924 CreatePeerConnection(&constraints);
deadbeefab9b2d12015-10-14 11:33:11 -07002925
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002926 rtc::scoped_refptr<StreamCollection> stream_collection =
2927 CreateStreamCollection(2, 1);
2928 pc_->AddStream(stream_collection->at(0));
2929 std::unique_ptr<SessionDescriptionInterface> offer;
2930 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2931 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
deadbeefab9b2d12015-10-14 11:33:11 -07002932
deadbeefab9b2d12015-10-14 11:33:11 -07002933 auto senders = pc_->GetSenders();
2934 EXPECT_EQ(2u, senders.size());
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002935 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0], kStreams[0]));
2936 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0], kStreams[0]));
deadbeefab9b2d12015-10-14 11:33:11 -07002937
2938 // Add a new MediaStream but with the same tracks as in the first stream.
2939 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream_1(
2940 webrtc::MediaStream::Create(kStreams[1]));
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002941 stream_1->AddTrack(stream_collection->at(0)->GetVideoTracks()[0]);
2942 stream_1->AddTrack(stream_collection->at(0)->GetAudioTracks()[0]);
deadbeefab9b2d12015-10-14 11:33:11 -07002943 pc_->AddStream(stream_1);
2944
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002945 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2946 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
deadbeefab9b2d12015-10-14 11:33:11 -07002947
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002948 auto new_senders = pc_->GetSenders();
2949 // Should be the same senders as before, but with updated stream id.
2950 // Note that this behavior is subject to change in the future.
2951 // We may decide the PC should ignore existing tracks in AddStream.
2952 EXPECT_EQ(senders, new_senders);
2953 EXPECT_TRUE(ContainsSender(new_senders, kAudioTracks[0], kStreams[1]));
2954 EXPECT_TRUE(ContainsSender(new_senders, kVideoTracks[0], kStreams[1]));
deadbeefab9b2d12015-10-14 11:33:11 -07002955}
2956
zhihuang81c3a032016-11-17 12:06:24 -08002957// This tests that PeerConnectionObserver::OnAddTrack is correctly called.
2958TEST_F(PeerConnectionInterfaceTest, OnAddTrackCallback) {
2959 FakeConstraints constraints;
2960 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2961 true);
2962 CreatePeerConnection(&constraints);
2963 CreateAndSetRemoteOffer(kSdpStringWithStream1AudioTrackOnly);
2964 EXPECT_EQ(observer_.num_added_tracks_, 1);
2965 EXPECT_EQ(observer_.last_added_track_label_, kAudioTracks[0]);
2966
2967 // Create and set the updated remote SDP.
2968 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2969 EXPECT_EQ(observer_.num_added_tracks_, 2);
2970 EXPECT_EQ(observer_.last_added_track_label_, kVideoTracks[0]);
2971}
2972
deadbeefd1a38b52016-12-10 13:15:33 -08002973// Test that when SetConfiguration is called and the configuration is
2974// changing, the next offer causes an ICE restart.
2975TEST_F(PeerConnectionInterfaceTest, SetConfigurationCausingIceRetart) {
2976 PeerConnectionInterface::RTCConfiguration config;
2977 config.type = PeerConnectionInterface::kRelay;
2978 // Need to pass default constraints to prevent disabling of DTLS...
2979 FakeConstraints default_constraints;
2980 CreatePeerConnection(config, &default_constraints);
2981 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
2982
2983 // Do initial offer/answer so there's something to restart.
2984 CreateOfferAsLocalDescription();
2985 CreateAnswerAsRemoteDescription(kSdpStringWithStream1);
2986
2987 // Grab the ufrags.
2988 std::vector<std::string> initial_ufrags = GetUfrags(pc_->local_description());
2989
2990 // Change ICE policy, which should trigger an ICE restart on the next offer.
2991 config.type = PeerConnectionInterface::kAll;
2992 EXPECT_TRUE(pc_->SetConfiguration(config));
2993 CreateOfferAsLocalDescription();
2994
2995 // Grab the new ufrags.
2996 std::vector<std::string> subsequent_ufrags =
2997 GetUfrags(pc_->local_description());
2998
2999 // Sanity check.
3000 EXPECT_EQ(initial_ufrags.size(), subsequent_ufrags.size());
3001 // Check that each ufrag is different.
3002 for (int i = 0; i < static_cast<int>(initial_ufrags.size()); ++i) {
3003 EXPECT_NE(initial_ufrags[i], subsequent_ufrags[i]);
3004 }
3005}
3006
3007// Test that when SetConfiguration is called and the configuration *isn't*
3008// changing, the next offer does *not* cause an ICE restart.
3009TEST_F(PeerConnectionInterfaceTest, SetConfigurationNotCausingIceRetart) {
3010 PeerConnectionInterface::RTCConfiguration config;
3011 config.type = PeerConnectionInterface::kRelay;
3012 // Need to pass default constraints to prevent disabling of DTLS...
3013 FakeConstraints default_constraints;
3014 CreatePeerConnection(config, &default_constraints);
3015 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
3016
3017 // Do initial offer/answer so there's something to restart.
3018 CreateOfferAsLocalDescription();
3019 CreateAnswerAsRemoteDescription(kSdpStringWithStream1);
3020
3021 // Grab the ufrags.
3022 std::vector<std::string> initial_ufrags = GetUfrags(pc_->local_description());
3023
3024 // Call SetConfiguration with a config identical to what the PC was
3025 // constructed with.
3026 EXPECT_TRUE(pc_->SetConfiguration(config));
3027 CreateOfferAsLocalDescription();
3028
3029 // Grab the new ufrags.
3030 std::vector<std::string> subsequent_ufrags =
3031 GetUfrags(pc_->local_description());
3032
3033 EXPECT_EQ(initial_ufrags, subsequent_ufrags);
3034}
3035
3036// Test for a weird corner case scenario:
3037// 1. Audio/video session established.
3038// 2. SetConfiguration changes ICE config; ICE restart needed.
3039// 3. ICE restart initiated by remote peer, but only for one m= section.
3040// 4. Next createOffer should initiate an ICE restart, but only for the other
3041// m= section; it would be pointless to do an ICE restart for the m= section
3042// that was already restarted.
3043TEST_F(PeerConnectionInterfaceTest, SetConfigurationCausingPartialIceRestart) {
3044 PeerConnectionInterface::RTCConfiguration config;
3045 config.type = PeerConnectionInterface::kRelay;
3046 // Need to pass default constraints to prevent disabling of DTLS...
3047 FakeConstraints default_constraints;
3048 CreatePeerConnection(config, &default_constraints);
3049 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
3050
3051 // Do initial offer/answer so there's something to restart.
3052 CreateOfferAsLocalDescription();
3053 CreateAnswerAsRemoteDescription(kSdpStringWithStream1);
3054
3055 // Change ICE policy, which should set the "needs-ice-restart" flag.
3056 config.type = PeerConnectionInterface::kAll;
3057 EXPECT_TRUE(pc_->SetConfiguration(config));
3058
3059 // Do ICE restart for the first m= section, initiated by remote peer.
3060 webrtc::JsepSessionDescription* remote_offer =
3061 new webrtc::JsepSessionDescription(SessionDescriptionInterface::kOffer);
3062 EXPECT_TRUE(remote_offer->Initialize(kSdpStringWithStream1, nullptr));
3063 remote_offer->description()->transport_infos()[0].description.ice_ufrag =
3064 "modified";
3065 EXPECT_TRUE(DoSetRemoteDescription(remote_offer));
3066 CreateAnswerAsLocalDescription();
3067
3068 // Grab the ufrags.
3069 std::vector<std::string> initial_ufrags = GetUfrags(pc_->local_description());
3070 ASSERT_EQ(2, initial_ufrags.size());
3071
3072 // Create offer and grab the new ufrags.
3073 CreateOfferAsLocalDescription();
3074 std::vector<std::string> subsequent_ufrags =
3075 GetUfrags(pc_->local_description());
3076 ASSERT_EQ(2, subsequent_ufrags.size());
3077
3078 // Ensure that only the ufrag for the second m= section changed.
3079 EXPECT_EQ(initial_ufrags[0], subsequent_ufrags[0]);
3080 EXPECT_NE(initial_ufrags[1], subsequent_ufrags[1]);
3081}
3082
deadbeeffe4a8a42016-12-20 17:56:17 -08003083// Tests that the methods to return current/pending descriptions work as
3084// expected at different points in the offer/answer exchange. This test does
3085// one offer/answer exchange as the offerer, then another as the answerer.
3086TEST_F(PeerConnectionInterfaceTest, CurrentAndPendingDescriptions) {
3087 // This disables DTLS so we can apply an answer to ourselves.
3088 CreatePeerConnection();
3089
3090 // Create initial local offer and get SDP (which will also be used as
3091 // answer/pranswer);
3092 std::unique_ptr<SessionDescriptionInterface> offer;
3093 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
3094 std::string sdp;
3095 EXPECT_TRUE(offer->ToString(&sdp));
3096
3097 // Set local offer.
3098 SessionDescriptionInterface* local_offer = offer.release();
3099 EXPECT_TRUE(DoSetLocalDescription(local_offer));
3100 EXPECT_EQ(local_offer, pc_->pending_local_description());
3101 EXPECT_EQ(nullptr, pc_->pending_remote_description());
3102 EXPECT_EQ(nullptr, pc_->current_local_description());
3103 EXPECT_EQ(nullptr, pc_->current_remote_description());
3104
3105 // Set remote pranswer.
3106 SessionDescriptionInterface* remote_pranswer =
3107 webrtc::CreateSessionDescription(SessionDescriptionInterface::kPrAnswer,
3108 sdp, nullptr);
3109 EXPECT_TRUE(DoSetRemoteDescription(remote_pranswer));
3110 EXPECT_EQ(local_offer, pc_->pending_local_description());
3111 EXPECT_EQ(remote_pranswer, pc_->pending_remote_description());
3112 EXPECT_EQ(nullptr, pc_->current_local_description());
3113 EXPECT_EQ(nullptr, pc_->current_remote_description());
3114
3115 // Set remote answer.
3116 SessionDescriptionInterface* remote_answer = webrtc::CreateSessionDescription(
3117 SessionDescriptionInterface::kAnswer, sdp, nullptr);
3118 EXPECT_TRUE(DoSetRemoteDescription(remote_answer));
3119 EXPECT_EQ(nullptr, pc_->pending_local_description());
3120 EXPECT_EQ(nullptr, pc_->pending_remote_description());
3121 EXPECT_EQ(local_offer, pc_->current_local_description());
3122 EXPECT_EQ(remote_answer, pc_->current_remote_description());
3123
3124 // Set remote offer.
3125 SessionDescriptionInterface* remote_offer = webrtc::CreateSessionDescription(
3126 SessionDescriptionInterface::kOffer, sdp, nullptr);
3127 EXPECT_TRUE(DoSetRemoteDescription(remote_offer));
3128 EXPECT_EQ(remote_offer, pc_->pending_remote_description());
3129 EXPECT_EQ(nullptr, pc_->pending_local_description());
3130 EXPECT_EQ(local_offer, pc_->current_local_description());
3131 EXPECT_EQ(remote_answer, pc_->current_remote_description());
3132
3133 // Set local pranswer.
3134 SessionDescriptionInterface* local_pranswer =
3135 webrtc::CreateSessionDescription(SessionDescriptionInterface::kPrAnswer,
3136 sdp, nullptr);
3137 EXPECT_TRUE(DoSetLocalDescription(local_pranswer));
3138 EXPECT_EQ(remote_offer, pc_->pending_remote_description());
3139 EXPECT_EQ(local_pranswer, pc_->pending_local_description());
3140 EXPECT_EQ(local_offer, pc_->current_local_description());
3141 EXPECT_EQ(remote_answer, pc_->current_remote_description());
3142
3143 // Set local answer.
3144 SessionDescriptionInterface* local_answer = webrtc::CreateSessionDescription(
3145 SessionDescriptionInterface::kAnswer, sdp, nullptr);
3146 EXPECT_TRUE(DoSetLocalDescription(local_answer));
3147 EXPECT_EQ(nullptr, pc_->pending_remote_description());
3148 EXPECT_EQ(nullptr, pc_->pending_local_description());
3149 EXPECT_EQ(remote_offer, pc_->current_remote_description());
3150 EXPECT_EQ(local_answer, pc_->current_local_description());
3151}
3152
zhihuang77985012017-02-07 15:45:16 -08003153// Tests that it won't crash when calling StartRtcEventLog or StopRtcEventLog
3154// after the PeerConnection is closed.
3155TEST_F(PeerConnectionInterfaceTest,
3156 StartAndStopLoggingAfterPeerConnectionClosed) {
3157 CreatePeerConnection();
3158 // The RtcEventLog will be reset when the PeerConnection is closed.
3159 pc_->Close();
3160
3161 rtc::PlatformFile file = 0;
3162 int64_t max_size_bytes = 1024;
3163 EXPECT_FALSE(pc_->StartRtcEventLog(file, max_size_bytes));
3164 pc_->StopRtcEventLog();
3165}
3166
deadbeef1dcb1642017-03-29 21:08:16 -07003167// Test that ICE renomination isn't offered if it's not enabled in the PC's
3168// RTCConfiguration.
3169TEST_F(PeerConnectionInterfaceTest, IceRenominationNotOffered) {
3170 PeerConnectionInterface::RTCConfiguration config;
3171 config.enable_ice_renomination = false;
3172 CreatePeerConnection(config, nullptr);
3173 AddVoiceStream("foo");
3174
3175 std::unique_ptr<SessionDescriptionInterface> offer;
3176 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
3177 cricket::SessionDescription* desc = offer->description();
3178 EXPECT_EQ(1u, desc->transport_infos().size());
3179 EXPECT_FALSE(
3180 desc->transport_infos()[0].description.GetIceParameters().renomination);
3181}
3182
3183// Test that the ICE renomination option is present in generated offers/answers
3184// if it's enabled in the PC's RTCConfiguration.
3185TEST_F(PeerConnectionInterfaceTest, IceRenominationOptionInOfferAndAnswer) {
3186 PeerConnectionInterface::RTCConfiguration config;
3187 config.enable_ice_renomination = true;
3188 CreatePeerConnection(config, nullptr);
3189 AddVoiceStream("foo");
3190
3191 std::unique_ptr<SessionDescriptionInterface> offer;
3192 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
3193 cricket::SessionDescription* desc = offer->description();
3194 EXPECT_EQ(1u, desc->transport_infos().size());
3195 EXPECT_TRUE(
3196 desc->transport_infos()[0].description.GetIceParameters().renomination);
3197
3198 // Set the offer as a remote description, then create an answer and ensure it
3199 // has the renomination flag too.
3200 EXPECT_TRUE(DoSetRemoteDescription(offer.release()));
3201 std::unique_ptr<SessionDescriptionInterface> answer;
3202 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
3203 desc = answer->description();
3204 EXPECT_EQ(1u, desc->transport_infos().size());
3205 EXPECT_TRUE(
3206 desc->transport_infos()[0].description.GetIceParameters().renomination);
3207}
3208
3209// Test that if CreateOffer is called with the deprecated "offer to receive
3210// audio/video" constraints, they're processed and result in an offer with
3211// audio/video sections just as if RTCOfferAnswerOptions had been used.
3212TEST_F(PeerConnectionInterfaceTest, CreateOfferWithOfferToReceiveConstraints) {
3213 CreatePeerConnection();
3214
3215 FakeConstraints constraints;
3216 constraints.SetMandatoryReceiveAudio(true);
3217 constraints.SetMandatoryReceiveVideo(true);
3218 std::unique_ptr<SessionDescriptionInterface> offer;
3219 ASSERT_TRUE(DoCreateOffer(&offer, &constraints));
3220
3221 cricket::SessionDescription* desc = offer->description();
3222 const cricket::ContentInfo* audio = cricket::GetFirstAudioContent(desc);
3223 const cricket::ContentInfo* video = cricket::GetFirstVideoContent(desc);
3224 ASSERT_NE(nullptr, audio);
3225 ASSERT_NE(nullptr, video);
3226 EXPECT_FALSE(audio->rejected);
3227 EXPECT_FALSE(video->rejected);
3228}
3229
3230// Test that if CreateAnswer is called with the deprecated "offer to receive
3231// audio/video" constraints, they're processed and can be used to reject an
3232// offered m= section just as can be done with RTCOfferAnswerOptions;
3233TEST_F(PeerConnectionInterfaceTest, CreateAnswerWithOfferToReceiveConstraints) {
3234 CreatePeerConnection();
3235
3236 // First, create an offer with audio/video and apply it as a remote
3237 // description.
3238 FakeConstraints constraints;
3239 constraints.SetMandatoryReceiveAudio(true);
3240 constraints.SetMandatoryReceiveVideo(true);
3241 std::unique_ptr<SessionDescriptionInterface> offer;
3242 ASSERT_TRUE(DoCreateOffer(&offer, &constraints));
3243 EXPECT_TRUE(DoSetRemoteDescription(offer.release()));
3244
3245 // Now create answer that rejects audio/video.
3246 constraints.SetMandatoryReceiveAudio(false);
3247 constraints.SetMandatoryReceiveVideo(false);
3248 std::unique_ptr<SessionDescriptionInterface> answer;
3249 ASSERT_TRUE(DoCreateAnswer(&answer, &constraints));
3250
3251 cricket::SessionDescription* desc = answer->description();
3252 const cricket::ContentInfo* audio = cricket::GetFirstAudioContent(desc);
3253 const cricket::ContentInfo* video = cricket::GetFirstVideoContent(desc);
3254 ASSERT_NE(nullptr, audio);
3255 ASSERT_NE(nullptr, video);
3256 EXPECT_TRUE(audio->rejected);
3257 EXPECT_TRUE(video->rejected);
3258}
3259
3260#ifdef HAVE_SCTP
3261#define MAYBE_DataChannelOnlyOfferWithMaxBundlePolicy \
3262 DataChannelOnlyOfferWithMaxBundlePolicy
3263#else
3264#define MAYBE_DataChannelOnlyOfferWithMaxBundlePolicy \
3265 DISABLED_DataChannelOnlyOfferWithMaxBundlePolicy
3266#endif
3267
3268// Test that negotiation can succeed with a data channel only, and with the max
3269// bundle policy. Previously there was a bug that prevented this.
3270TEST_F(PeerConnectionInterfaceTest,
3271 MAYBE_DataChannelOnlyOfferWithMaxBundlePolicy) {
3272 PeerConnectionInterface::RTCConfiguration config;
3273 config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
3274 CreatePeerConnection(config, nullptr);
3275
3276 // First, create an offer with only a data channel and apply it as a remote
3277 // description.
3278 pc_->CreateDataChannel("test", nullptr);
3279 std::unique_ptr<SessionDescriptionInterface> offer;
3280 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
3281 EXPECT_TRUE(DoSetRemoteDescription(offer.release()));
3282
3283 // Create and set answer as well.
3284 std::unique_ptr<SessionDescriptionInterface> answer;
3285 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
3286 EXPECT_TRUE(DoSetLocalDescription(answer.release()));
3287}
3288
nisse51542be2016-02-12 02:27:06 -08003289class PeerConnectionMediaConfigTest : public testing::Test {
3290 protected:
3291 void SetUp() override {
nisseaf510af2016-03-21 08:20:42 -07003292 pcf_ = new rtc::RefCountedObject<PeerConnectionFactoryForTest>();
nisse51542be2016-02-12 02:27:06 -08003293 pcf_->Initialize();
3294 }
3295 const cricket::MediaConfig& TestCreatePeerConnection(
3296 const PeerConnectionInterface::RTCConfiguration& config,
3297 const MediaConstraintsInterface *constraints) {
3298 pcf_->create_media_controller_called_ = false;
3299
zhihuang9763d562016-08-05 11:14:50 -07003300 rtc::scoped_refptr<PeerConnectionInterface> pc(pcf_->CreatePeerConnection(
3301 config, constraints, nullptr, nullptr, &observer_));
nisse51542be2016-02-12 02:27:06 -08003302 EXPECT_TRUE(pc.get());
3303 EXPECT_TRUE(pcf_->create_media_controller_called_);
3304 return pcf_->create_media_controller_config_;
3305 }
3306
zhihuang9763d562016-08-05 11:14:50 -07003307 rtc::scoped_refptr<PeerConnectionFactoryForTest> pcf_;
nisse51542be2016-02-12 02:27:06 -08003308 MockPeerConnectionObserver observer_;
3309};
3310
3311// This test verifies the default behaviour with no constraints and a
3312// default RTCConfiguration.
3313TEST_F(PeerConnectionMediaConfigTest, TestDefaults) {
3314 PeerConnectionInterface::RTCConfiguration config;
3315 FakeConstraints constraints;
3316
3317 const cricket::MediaConfig& media_config =
3318 TestCreatePeerConnection(config, &constraints);
3319
3320 EXPECT_FALSE(media_config.enable_dscp);
nisse0db023a2016-03-01 04:29:59 -08003321 EXPECT_TRUE(media_config.video.enable_cpu_overuse_detection);
3322 EXPECT_FALSE(media_config.video.disable_prerenderer_smoothing);
3323 EXPECT_FALSE(media_config.video.suspend_below_min_bitrate);
nisse51542be2016-02-12 02:27:06 -08003324}
3325
3326// This test verifies the DSCP constraint is recognized and passed to
3327// the CreateMediaController call.
3328TEST_F(PeerConnectionMediaConfigTest, TestDscpConstraintTrue) {
3329 PeerConnectionInterface::RTCConfiguration config;
3330 FakeConstraints constraints;
3331
3332 constraints.AddOptional(webrtc::MediaConstraintsInterface::kEnableDscp, true);
3333 const cricket::MediaConfig& media_config =
3334 TestCreatePeerConnection(config, &constraints);
3335
3336 EXPECT_TRUE(media_config.enable_dscp);
3337}
3338
3339// This test verifies the cpu overuse detection constraint is
3340// recognized and passed to the CreateMediaController call.
3341TEST_F(PeerConnectionMediaConfigTest, TestCpuOveruseConstraintFalse) {
3342 PeerConnectionInterface::RTCConfiguration config;
3343 FakeConstraints constraints;
3344
3345 constraints.AddOptional(
3346 webrtc::MediaConstraintsInterface::kCpuOveruseDetection, false);
3347 const cricket::MediaConfig media_config =
3348 TestCreatePeerConnection(config, &constraints);
3349
nisse0db023a2016-03-01 04:29:59 -08003350 EXPECT_FALSE(media_config.video.enable_cpu_overuse_detection);
nisse51542be2016-02-12 02:27:06 -08003351}
3352
3353// This test verifies that the disable_prerenderer_smoothing flag is
3354// propagated from RTCConfiguration to the CreateMediaController call.
3355TEST_F(PeerConnectionMediaConfigTest, TestDisablePrerendererSmoothingTrue) {
3356 PeerConnectionInterface::RTCConfiguration config;
3357 FakeConstraints constraints;
3358
Niels Möller71bdda02016-03-31 12:59:59 +02003359 config.set_prerenderer_smoothing(false);
nisse51542be2016-02-12 02:27:06 -08003360 const cricket::MediaConfig& media_config =
3361 TestCreatePeerConnection(config, &constraints);
3362
nisse0db023a2016-03-01 04:29:59 -08003363 EXPECT_TRUE(media_config.video.disable_prerenderer_smoothing);
3364}
3365
3366// This test verifies the suspend below min bitrate constraint is
3367// recognized and passed to the CreateMediaController call.
3368TEST_F(PeerConnectionMediaConfigTest,
3369 TestSuspendBelowMinBitrateConstraintTrue) {
3370 PeerConnectionInterface::RTCConfiguration config;
3371 FakeConstraints constraints;
3372
3373 constraints.AddOptional(
3374 webrtc::MediaConstraintsInterface::kEnableVideoSuspendBelowMinBitrate,
3375 true);
3376 const cricket::MediaConfig media_config =
3377 TestCreatePeerConnection(config, &constraints);
3378
3379 EXPECT_TRUE(media_config.video.suspend_below_min_bitrate);
nisse51542be2016-02-12 02:27:06 -08003380}
3381
deadbeefab9b2d12015-10-14 11:33:11 -07003382// The following tests verify that session options are created correctly.
deadbeefc80741f2015-10-22 13:14:45 -07003383// TODO(deadbeef): Convert these tests to be more end-to-end. Instead of
3384// "verify options are converted correctly", should be "pass options into
3385// CreateOffer and verify the correct offer is produced."
deadbeefab9b2d12015-10-14 11:33:11 -07003386
3387TEST(CreateSessionOptionsTest, GetOptionsForOfferWithInvalidAudioOption) {
3388 RTCOfferAnswerOptions rtc_options;
3389 rtc_options.offer_to_receive_audio = RTCOfferAnswerOptions::kUndefined - 1;
3390
3391 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08003392 EXPECT_FALSE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07003393
3394 rtc_options.offer_to_receive_audio =
3395 RTCOfferAnswerOptions::kMaxOfferToReceiveMedia + 1;
htaaac2dea2016-03-10 13:35:55 -08003396 EXPECT_FALSE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07003397}
3398
3399TEST(CreateSessionOptionsTest, GetOptionsForOfferWithInvalidVideoOption) {
3400 RTCOfferAnswerOptions rtc_options;
3401 rtc_options.offer_to_receive_video = RTCOfferAnswerOptions::kUndefined - 1;
3402
3403 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08003404 EXPECT_FALSE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07003405
3406 rtc_options.offer_to_receive_video =
3407 RTCOfferAnswerOptions::kMaxOfferToReceiveMedia + 1;
htaaac2dea2016-03-10 13:35:55 -08003408 EXPECT_FALSE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07003409}
3410
3411// Test that a MediaSessionOptions is created for an offer if
deadbeefc80741f2015-10-22 13:14:45 -07003412// OfferToReceiveAudio and OfferToReceiveVideo options are set.
deadbeefab9b2d12015-10-14 11:33:11 -07003413TEST(CreateSessionOptionsTest, GetMediaSessionOptionsForOfferWithAudioVideo) {
3414 RTCOfferAnswerOptions rtc_options;
3415 rtc_options.offer_to_receive_audio = 1;
3416 rtc_options.offer_to_receive_video = 1;
3417
3418 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08003419 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07003420 EXPECT_TRUE(options.has_audio());
3421 EXPECT_TRUE(options.has_video());
3422 EXPECT_TRUE(options.bundle_enabled);
3423}
3424
3425// Test that a correct MediaSessionOptions is created for an offer if
deadbeefc80741f2015-10-22 13:14:45 -07003426// OfferToReceiveAudio is set.
deadbeefab9b2d12015-10-14 11:33:11 -07003427TEST(CreateSessionOptionsTest, GetMediaSessionOptionsForOfferWithAudio) {
3428 RTCOfferAnswerOptions rtc_options;
3429 rtc_options.offer_to_receive_audio = 1;
3430
3431 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08003432 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07003433 EXPECT_TRUE(options.has_audio());
3434 EXPECT_FALSE(options.has_video());
3435 EXPECT_TRUE(options.bundle_enabled);
3436}
3437
3438// Test that a correct MediaSessionOptions is created for an offer if
deadbeefc80741f2015-10-22 13:14:45 -07003439// the default OfferOptions are used.
deadbeefab9b2d12015-10-14 11:33:11 -07003440TEST(CreateSessionOptionsTest, GetDefaultMediaSessionOptionsForOffer) {
3441 RTCOfferAnswerOptions rtc_options;
3442
3443 cricket::MediaSessionOptions options;
deadbeef0ed85b22016-02-23 17:24:52 -08003444 options.transport_options["audio"] = cricket::TransportOptions();
3445 options.transport_options["video"] = cricket::TransportOptions();
htaaac2dea2016-03-10 13:35:55 -08003446 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefc80741f2015-10-22 13:14:45 -07003447 EXPECT_TRUE(options.has_audio());
deadbeefab9b2d12015-10-14 11:33:11 -07003448 EXPECT_FALSE(options.has_video());
deadbeefc80741f2015-10-22 13:14:45 -07003449 EXPECT_TRUE(options.bundle_enabled);
deadbeefab9b2d12015-10-14 11:33:11 -07003450 EXPECT_TRUE(options.vad_enabled);
deadbeef0ed85b22016-02-23 17:24:52 -08003451 EXPECT_FALSE(options.transport_options["audio"].ice_restart);
3452 EXPECT_FALSE(options.transport_options["video"].ice_restart);
deadbeefab9b2d12015-10-14 11:33:11 -07003453}
3454
3455// Test that a correct MediaSessionOptions is created for an offer if
deadbeefc80741f2015-10-22 13:14:45 -07003456// OfferToReceiveVideo is set.
deadbeefab9b2d12015-10-14 11:33:11 -07003457TEST(CreateSessionOptionsTest, GetMediaSessionOptionsForOfferWithVideo) {
3458 RTCOfferAnswerOptions rtc_options;
3459 rtc_options.offer_to_receive_audio = 0;
3460 rtc_options.offer_to_receive_video = 1;
3461
3462 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08003463 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07003464 EXPECT_FALSE(options.has_audio());
3465 EXPECT_TRUE(options.has_video());
3466 EXPECT_TRUE(options.bundle_enabled);
3467}
3468
3469// Test that a correct MediaSessionOptions is created for an offer if
3470// UseRtpMux is set to false.
3471TEST(CreateSessionOptionsTest,
3472 GetMediaSessionOptionsForOfferWithBundleDisabled) {
3473 RTCOfferAnswerOptions rtc_options;
3474 rtc_options.offer_to_receive_audio = 1;
3475 rtc_options.offer_to_receive_video = 1;
3476 rtc_options.use_rtp_mux = false;
3477
3478 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08003479 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07003480 EXPECT_TRUE(options.has_audio());
3481 EXPECT_TRUE(options.has_video());
3482 EXPECT_FALSE(options.bundle_enabled);
3483}
3484
3485// Test that a correct MediaSessionOptions is created to restart ice if
3486// IceRestart is set. It also tests that subsequent MediaSessionOptions don't
Taylor Brandstetterf475d362016-01-08 15:35:57 -08003487// have |audio_transport_options.ice_restart| etc. set.
deadbeefab9b2d12015-10-14 11:33:11 -07003488TEST(CreateSessionOptionsTest, GetMediaSessionOptionsForOfferWithIceRestart) {
3489 RTCOfferAnswerOptions rtc_options;
3490 rtc_options.ice_restart = true;
3491
3492 cricket::MediaSessionOptions options;
deadbeef0ed85b22016-02-23 17:24:52 -08003493 options.transport_options["audio"] = cricket::TransportOptions();
3494 options.transport_options["video"] = cricket::TransportOptions();
htaaac2dea2016-03-10 13:35:55 -08003495 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeef0ed85b22016-02-23 17:24:52 -08003496 EXPECT_TRUE(options.transport_options["audio"].ice_restart);
3497 EXPECT_TRUE(options.transport_options["video"].ice_restart);
deadbeefab9b2d12015-10-14 11:33:11 -07003498
3499 rtc_options = RTCOfferAnswerOptions();
htaaac2dea2016-03-10 13:35:55 -08003500 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeef0ed85b22016-02-23 17:24:52 -08003501 EXPECT_FALSE(options.transport_options["audio"].ice_restart);
3502 EXPECT_FALSE(options.transport_options["video"].ice_restart);
deadbeefab9b2d12015-10-14 11:33:11 -07003503}
3504
3505// Test that the MediaConstraints in an answer don't affect if audio and video
3506// is offered in an offer but that if kOfferToReceiveAudio or
3507// kOfferToReceiveVideo constraints are true in an offer, the media type will be
3508// included in subsequent answers.
3509TEST(CreateSessionOptionsTest, MediaConstraintsInAnswer) {
3510 FakeConstraints answer_c;
3511 answer_c.SetMandatoryReceiveAudio(true);
3512 answer_c.SetMandatoryReceiveVideo(true);
3513
3514 cricket::MediaSessionOptions answer_options;
3515 EXPECT_TRUE(ParseConstraintsForAnswer(&answer_c, &answer_options));
3516 EXPECT_TRUE(answer_options.has_audio());
3517 EXPECT_TRUE(answer_options.has_video());
3518
deadbeefc80741f2015-10-22 13:14:45 -07003519 RTCOfferAnswerOptions rtc_offer_options;
deadbeefab9b2d12015-10-14 11:33:11 -07003520
3521 cricket::MediaSessionOptions offer_options;
htaaac2dea2016-03-10 13:35:55 -08003522 EXPECT_TRUE(
3523 ExtractMediaSessionOptions(rtc_offer_options, false, &offer_options));
deadbeefc80741f2015-10-22 13:14:45 -07003524 EXPECT_TRUE(offer_options.has_audio());
htaaac2dea2016-03-10 13:35:55 -08003525 EXPECT_TRUE(offer_options.has_video());
deadbeefab9b2d12015-10-14 11:33:11 -07003526
deadbeefc80741f2015-10-22 13:14:45 -07003527 RTCOfferAnswerOptions updated_rtc_offer_options;
3528 updated_rtc_offer_options.offer_to_receive_audio = 1;
3529 updated_rtc_offer_options.offer_to_receive_video = 1;
deadbeefab9b2d12015-10-14 11:33:11 -07003530
3531 cricket::MediaSessionOptions updated_offer_options;
htaaac2dea2016-03-10 13:35:55 -08003532 EXPECT_TRUE(ExtractMediaSessionOptions(updated_rtc_offer_options, false,
htaa2a49d92016-03-04 02:51:39 -08003533 &updated_offer_options));
deadbeefab9b2d12015-10-14 11:33:11 -07003534 EXPECT_TRUE(updated_offer_options.has_audio());
3535 EXPECT_TRUE(updated_offer_options.has_video());
3536
3537 // Since an offer has been created with both audio and video, subsequent
3538 // offers and answers should contain both audio and video.
3539 // Answers will only contain the media types that exist in the offer
3540 // regardless of the value of |updated_answer_options.has_audio| and
3541 // |updated_answer_options.has_video|.
3542 FakeConstraints updated_answer_c;
3543 answer_c.SetMandatoryReceiveAudio(false);
3544 answer_c.SetMandatoryReceiveVideo(false);
3545
3546 cricket::MediaSessionOptions updated_answer_options;
3547 EXPECT_TRUE(
3548 ParseConstraintsForAnswer(&updated_answer_c, &updated_answer_options));
3549 EXPECT_TRUE(updated_answer_options.has_audio());
3550 EXPECT_TRUE(updated_answer_options.has_video());
deadbeefab9b2d12015-10-14 11:33:11 -07003551}
deadbeef3edec7c2016-12-10 11:44:26 -08003552
deadbeef293e9262017-01-11 12:28:30 -08003553// Tests a few random fields being different.
3554TEST(RTCConfigurationTest, ComparisonOperators) {
3555 PeerConnectionInterface::RTCConfiguration a;
3556 PeerConnectionInterface::RTCConfiguration b;
3557 EXPECT_EQ(a, b);
3558
3559 PeerConnectionInterface::RTCConfiguration c;
3560 c.servers.push_back(PeerConnectionInterface::IceServer());
3561 EXPECT_NE(a, c);
3562
3563 PeerConnectionInterface::RTCConfiguration d;
3564 d.type = PeerConnectionInterface::kRelay;
3565 EXPECT_NE(a, d);
3566
3567 PeerConnectionInterface::RTCConfiguration e;
3568 e.audio_jitter_buffer_max_packets = 5;
3569 EXPECT_NE(a, e);
3570
3571 PeerConnectionInterface::RTCConfiguration f;
3572 f.ice_connection_receiving_timeout = 1337;
3573 EXPECT_NE(a, f);
3574
3575 PeerConnectionInterface::RTCConfiguration g;
3576 g.disable_ipv6 = true;
3577 EXPECT_NE(a, g);
3578
3579 PeerConnectionInterface::RTCConfiguration h(
3580 PeerConnectionInterface::RTCConfigurationType::kAggressive);
3581 EXPECT_NE(a, h);
3582}