blob: 7f73db843222dbd087a4e2622019fe63d39109ad [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:
kjellander71a1b612016-11-07 01:18:08 -0800529 // We need these using declarations because there are two versions of each of
530 // the below methods and we only override one of them.
531 // TODO(deadbeef): Remove once there's only one version of the methods.
532 using PeerConnectionObserver::OnAddStream;
533 using PeerConnectionObserver::OnRemoveStream;
534 using PeerConnectionObserver::OnDataChannel;
535
deadbeefab9b2d12015-10-14 11:33:11 -0700536 MockPeerConnectionObserver() : remote_streams_(StreamCollection::Create()) {}
Henrik Kjellander3fe372d2016-05-12 08:10:52 +0200537 virtual ~MockPeerConnectionObserver() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000538 }
539 void SetPeerConnectionInterface(PeerConnectionInterface* pc) {
540 pc_ = pc;
541 if (pc) {
542 state_ = pc_->signaling_state();
543 }
544 }
nisseef8b61e2016-04-29 06:09:15 -0700545 void OnSignalingChange(
546 PeerConnectionInterface::SignalingState new_state) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000547 EXPECT_EQ(pc_->signaling_state(), new_state);
548 state_ = new_state;
549 }
deadbeefab9b2d12015-10-14 11:33:11 -0700550
551 MediaStreamInterface* RemoteStream(const std::string& label) {
552 return remote_streams_->find(label);
553 }
554 StreamCollectionInterface* remote_streams() const { return remote_streams_; }
Taylor Brandstetter98cde262016-05-31 13:02:21 -0700555 void OnAddStream(rtc::scoped_refptr<MediaStreamInterface> stream) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000556 last_added_stream_ = stream;
deadbeefab9b2d12015-10-14 11:33:11 -0700557 remote_streams_->AddStream(stream);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000558 }
Taylor Brandstetter98cde262016-05-31 13:02:21 -0700559 void OnRemoveStream(
560 rtc::scoped_refptr<MediaStreamInterface> stream) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000561 last_removed_stream_ = stream;
deadbeefab9b2d12015-10-14 11:33:11 -0700562 remote_streams_->RemoveStream(stream);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000563 }
perkjdfb769d2016-02-09 03:09:43 -0800564 void OnRenegotiationNeeded() override { renegotiation_needed_ = true; }
Taylor Brandstetter98cde262016-05-31 13:02:21 -0700565 void OnDataChannel(
566 rtc::scoped_refptr<DataChannelInterface> data_channel) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000567 last_datachannel_ = data_channel;
568 }
569
perkjdfb769d2016-02-09 03:09:43 -0800570 void OnIceConnectionChange(
571 PeerConnectionInterface::IceConnectionState new_state) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000572 EXPECT_EQ(pc_->ice_connection_state(), new_state);
zhihuang81c3a032016-11-17 12:06:24 -0800573 callback_triggered_ = true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000574 }
perkjdfb769d2016-02-09 03:09:43 -0800575 void OnIceGatheringChange(
576 PeerConnectionInterface::IceGatheringState new_state) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000577 EXPECT_EQ(pc_->ice_gathering_state(), new_state);
perkjdfb769d2016-02-09 03:09:43 -0800578 ice_complete_ = new_state == PeerConnectionInterface::kIceGatheringComplete;
zhihuang81c3a032016-11-17 12:06:24 -0800579 callback_triggered_ = true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000580 }
perkjdfb769d2016-02-09 03:09:43 -0800581 void OnIceCandidate(const webrtc::IceCandidateInterface* candidate) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000582 EXPECT_NE(PeerConnectionInterface::kIceGatheringNew,
583 pc_->ice_gathering_state());
584
585 std::string sdp;
586 EXPECT_TRUE(candidate->ToString(&sdp));
587 EXPECT_LT(0u, sdp.size());
588 last_candidate_.reset(webrtc::CreateIceCandidate(candidate->sdp_mid(),
589 candidate->sdp_mline_index(), sdp, NULL));
590 EXPECT_TRUE(last_candidate_.get() != NULL);
zhihuang81c3a032016-11-17 12:06:24 -0800591 callback_triggered_ = true;
zhihuang29ff8442016-07-27 11:07:25 -0700592 }
593
594 void OnIceCandidatesRemoved(
595 const std::vector<cricket::Candidate>& candidates) override {
zhihuang81c3a032016-11-17 12:06:24 -0800596 callback_triggered_ = true;
zhihuang29ff8442016-07-27 11:07:25 -0700597 }
598
599 void OnIceConnectionReceivingChange(bool receiving) override {
zhihuang81c3a032016-11-17 12:06:24 -0800600 callback_triggered_ = true;
601 }
602
zhihuangc63b8942016-12-02 15:41:10 -0800603 void OnAddTrack(
604 rtc::scoped_refptr<webrtc::RtpReceiverInterface> receiver,
605 const std::vector<rtc::scoped_refptr<webrtc::MediaStreamInterface>>&
606 streams) override {
zhihuang81c3a032016-11-17 12:06:24 -0800607 EXPECT_TRUE(receiver != nullptr);
608 num_added_tracks_++;
609 last_added_track_label_ = receiver->id();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000610 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000611
612 // Returns the label of the last added stream.
613 // Empty string if no stream have been added.
614 std::string GetLastAddedStreamLabel() {
615 if (last_added_stream_.get())
616 return last_added_stream_->label();
617 return "";
618 }
619 std::string GetLastRemovedStreamLabel() {
620 if (last_removed_stream_.get())
621 return last_removed_stream_->label();
622 return "";
623 }
624
zhihuang9763d562016-08-05 11:14:50 -0700625 rtc::scoped_refptr<PeerConnectionInterface> pc_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000626 PeerConnectionInterface::SignalingState state_;
kwibergd1fe2812016-04-27 06:47:29 -0700627 std::unique_ptr<IceCandidateInterface> last_candidate_;
zhihuang9763d562016-08-05 11:14:50 -0700628 rtc::scoped_refptr<DataChannelInterface> last_datachannel_;
deadbeefab9b2d12015-10-14 11:33:11 -0700629 rtc::scoped_refptr<StreamCollection> remote_streams_;
630 bool renegotiation_needed_ = false;
631 bool ice_complete_ = false;
zhihuang81c3a032016-11-17 12:06:24 -0800632 bool callback_triggered_ = false;
633 int num_added_tracks_ = 0;
634 std::string last_added_track_label_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000635
636 private:
zhihuang9763d562016-08-05 11:14:50 -0700637 rtc::scoped_refptr<MediaStreamInterface> last_added_stream_;
638 rtc::scoped_refptr<MediaStreamInterface> last_removed_stream_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000639};
640
641} // namespace
deadbeefab9b2d12015-10-14 11:33:11 -0700642
zhihuang29ff8442016-07-27 11:07:25 -0700643// The PeerConnectionMediaConfig tests below verify that configuration
644// and constraints are propagated into the MediaConfig passed to
645// CreateMediaController. These settings are intended for MediaChannel
646// constructors, but that is not exercised by these unittest.
647class PeerConnectionFactoryForTest : public webrtc::PeerConnectionFactory {
648 public:
kwiberg1e4e8cb2017-01-31 01:48:08 -0800649 PeerConnectionFactoryForTest()
650 : webrtc::PeerConnectionFactory(
651 webrtc::CreateBuiltinAudioEncoderFactory(),
652 webrtc::CreateBuiltinAudioDecoderFactory()) {}
653
zhihuang29ff8442016-07-27 11:07:25 -0700654 webrtc::MediaControllerInterface* CreateMediaController(
skvlad11a9cbf2016-10-07 11:53:05 -0700655 const cricket::MediaConfig& config,
656 webrtc::RtcEventLog* event_log) const override {
zhihuang29ff8442016-07-27 11:07:25 -0700657 create_media_controller_called_ = true;
658 create_media_controller_config_ = config;
659
660 webrtc::MediaControllerInterface* mc =
skvlad11a9cbf2016-10-07 11:53:05 -0700661 PeerConnectionFactory::CreateMediaController(config, event_log);
zhihuang29ff8442016-07-27 11:07:25 -0700662 EXPECT_TRUE(mc != nullptr);
663 return mc;
664 }
665
666 cricket::TransportController* CreateTransportController(
Honghai Zhangbfd398c2016-08-30 22:07:42 -0700667 cricket::PortAllocator* port_allocator,
668 bool redetermine_role_on_ice_restart) override {
zhihuang29ff8442016-07-27 11:07:25 -0700669 transport_controller = new cricket::TransportController(
Honghai Zhangbfd398c2016-08-30 22:07:42 -0700670 rtc::Thread::Current(), rtc::Thread::Current(), port_allocator,
671 redetermine_role_on_ice_restart);
zhihuang29ff8442016-07-27 11:07:25 -0700672 return transport_controller;
673 }
674
675 cricket::TransportController* transport_controller;
676 // Mutable, so they can be modified in the above const-declared method.
677 mutable bool create_media_controller_called_ = false;
678 mutable cricket::MediaConfig create_media_controller_config_;
679};
680
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000681class PeerConnectionInterfaceTest : public testing::Test {
682 protected:
phoglund37ebcf02016-01-08 05:04:57 -0800683 PeerConnectionInterfaceTest() {
684#ifdef WEBRTC_ANDROID
685 webrtc::InitializeAndroidObjects();
686#endif
687 }
688
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000689 virtual void SetUp() {
690 pc_factory_ = webrtc::CreatePeerConnectionFactory(
danilchape9021a32016-05-17 01:52:02 -0700691 rtc::Thread::Current(), rtc::Thread::Current(), rtc::Thread::Current(),
692 nullptr, nullptr, nullptr);
693 ASSERT_TRUE(pc_factory_);
zhihuang29ff8442016-07-27 11:07:25 -0700694 pc_factory_for_test_ =
695 new rtc::RefCountedObject<PeerConnectionFactoryForTest>();
696 pc_factory_for_test_->Initialize();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000697 }
698
699 void CreatePeerConnection() {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700700 CreatePeerConnection(PeerConnectionInterface::RTCConfiguration(), nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000701 }
702
deadbeef293e9262017-01-11 12:28:30 -0800703 // DTLS does not work in a loopback call, so is disabled for most of the
704 // tests in this file.
705 void CreatePeerConnectionWithoutDtls() {
706 FakeConstraints no_dtls_constraints;
707 no_dtls_constraints.AddMandatory(
708 webrtc::MediaConstraintsInterface::kEnableDtlsSrtp, false);
709
710 CreatePeerConnection(PeerConnectionInterface::RTCConfiguration(),
711 &no_dtls_constraints);
712 }
713
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000714 void CreatePeerConnection(webrtc::MediaConstraintsInterface* constraints) {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700715 CreatePeerConnection(PeerConnectionInterface::RTCConfiguration(),
716 constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000717 }
718
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700719 void CreatePeerConnectionWithIceTransportsType(
720 PeerConnectionInterface::IceTransportsType type) {
721 PeerConnectionInterface::RTCConfiguration config;
722 config.type = type;
723 return CreatePeerConnection(config, nullptr);
724 }
725
726 void CreatePeerConnectionWithIceServer(const std::string& uri,
727 const std::string& password) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800728 PeerConnectionInterface::RTCConfiguration config;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000729 PeerConnectionInterface::IceServer server;
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700730 server.uri = uri;
731 server.password = password;
732 config.servers.push_back(server);
733 CreatePeerConnection(config, nullptr);
734 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000735
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700736 void CreatePeerConnection(PeerConnectionInterface::RTCConfiguration config,
737 webrtc::MediaConstraintsInterface* constraints) {
kwibergd1fe2812016-04-27 06:47:29 -0700738 std::unique_ptr<cricket::FakePortAllocator> port_allocator(
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800739 new cricket::FakePortAllocator(rtc::Thread::Current(), nullptr));
740 port_allocator_ = port_allocator.get();
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000741
Henrik Boströmd79599d2016-06-01 13:58:50 +0200742 std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator;
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000743 bool dtls;
744 if (FindConstraint(constraints,
745 webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
746 &dtls,
Henrik Boström5e56c592015-08-11 10:33:13 +0200747 nullptr) && dtls) {
deadbeef8662f942017-01-20 21:20:51 -0800748 fake_certificate_generator_ = new FakeRTCCertificateGenerator();
749 cert_generator.reset(fake_certificate_generator_);
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000750 }
Henrik Boströmd79599d2016-06-01 13:58:50 +0200751 pc_ = pc_factory_->CreatePeerConnection(
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800752 config, constraints, std::move(port_allocator),
Henrik Boströmd79599d2016-06-01 13:58:50 +0200753 std::move(cert_generator), &observer_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000754 ASSERT_TRUE(pc_.get() != NULL);
755 observer_.SetPeerConnectionInterface(pc_.get());
756 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
757 }
758
deadbeef0a6c4ca2015-10-06 11:38:28 -0700759 void CreatePeerConnectionExpectFail(const std::string& uri) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800760 PeerConnectionInterface::RTCConfiguration config;
deadbeef0a6c4ca2015-10-06 11:38:28 -0700761 PeerConnectionInterface::IceServer server;
deadbeef0a6c4ca2015-10-06 11:38:28 -0700762 server.uri = uri;
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800763 config.servers.push_back(server);
deadbeef0a6c4ca2015-10-06 11:38:28 -0700764
zhihuang9763d562016-08-05 11:14:50 -0700765 rtc::scoped_refptr<PeerConnectionInterface> pc;
hbosd7973cc2016-05-27 06:08:53 -0700766 pc = pc_factory_->CreatePeerConnection(config, nullptr, nullptr, nullptr,
767 &observer_);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800768 EXPECT_EQ(nullptr, pc);
deadbeef0a6c4ca2015-10-06 11:38:28 -0700769 }
770
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000771 void CreatePeerConnectionWithDifferentConfigurations() {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700772 CreatePeerConnectionWithIceServer(kStunAddressOnly, "");
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800773 EXPECT_EQ(1u, port_allocator_->stun_servers().size());
774 EXPECT_EQ(0u, port_allocator_->turn_servers().size());
775 EXPECT_EQ("address", port_allocator_->stun_servers().begin()->hostname());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000776 EXPECT_EQ(kDefaultStunPort,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800777 port_allocator_->stun_servers().begin()->port());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000778
deadbeef0a6c4ca2015-10-06 11:38:28 -0700779 CreatePeerConnectionExpectFail(kStunInvalidPort);
780 CreatePeerConnectionExpectFail(kStunAddressPortAndMore1);
781 CreatePeerConnectionExpectFail(kStunAddressPortAndMore2);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000782
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700783 CreatePeerConnectionWithIceServer(kTurnIceServerUri, kTurnPassword);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800784 EXPECT_EQ(0u, port_allocator_->stun_servers().size());
785 EXPECT_EQ(1u, port_allocator_->turn_servers().size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000786 EXPECT_EQ(kTurnUsername,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800787 port_allocator_->turn_servers()[0].credentials.username);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000788 EXPECT_EQ(kTurnPassword,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800789 port_allocator_->turn_servers()[0].credentials.password);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000790 EXPECT_EQ(kTurnHostname,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800791 port_allocator_->turn_servers()[0].ports[0].address.hostname());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000792 }
793
794 void ReleasePeerConnection() {
795 pc_ = NULL;
796 observer_.SetPeerConnectionInterface(NULL);
797 }
798
deadbeefab9b2d12015-10-14 11:33:11 -0700799 void AddVideoStream(const std::string& label) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000800 // Create a local stream.
zhihuang9763d562016-08-05 11:14:50 -0700801 rtc::scoped_refptr<MediaStreamInterface> stream(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000802 pc_factory_->CreateLocalMediaStream(label));
zhihuang9763d562016-08-05 11:14:50 -0700803 rtc::scoped_refptr<VideoTrackSourceInterface> video_source(
deadbeef112b2e92017-02-10 20:13:37 -0800804 pc_factory_->CreateVideoSource(std::unique_ptr<cricket::VideoCapturer>(
805 new cricket::FakeVideoCapturer()),
806 NULL));
zhihuang9763d562016-08-05 11:14:50 -0700807 rtc::scoped_refptr<VideoTrackInterface> video_track(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000808 pc_factory_->CreateVideoTrack(label + "v0", video_source));
809 stream->AddTrack(video_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +0000810 EXPECT_TRUE(pc_->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000811 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
812 observer_.renegotiation_needed_ = false;
813 }
814
815 void AddVoiceStream(const std::string& label) {
816 // Create a local stream.
zhihuang9763d562016-08-05 11:14:50 -0700817 rtc::scoped_refptr<MediaStreamInterface> stream(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000818 pc_factory_->CreateLocalMediaStream(label));
zhihuang9763d562016-08-05 11:14:50 -0700819 rtc::scoped_refptr<AudioTrackInterface> audio_track(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000820 pc_factory_->CreateAudioTrack(label + "a0", NULL));
821 stream->AddTrack(audio_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +0000822 EXPECT_TRUE(pc_->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000823 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
824 observer_.renegotiation_needed_ = false;
825 }
826
827 void AddAudioVideoStream(const std::string& stream_label,
828 const std::string& audio_track_label,
829 const std::string& video_track_label) {
830 // Create a local stream.
zhihuang9763d562016-08-05 11:14:50 -0700831 rtc::scoped_refptr<MediaStreamInterface> stream(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000832 pc_factory_->CreateLocalMediaStream(stream_label));
zhihuang9763d562016-08-05 11:14:50 -0700833 rtc::scoped_refptr<AudioTrackInterface> audio_track(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000834 pc_factory_->CreateAudioTrack(
835 audio_track_label, static_cast<AudioSourceInterface*>(NULL)));
836 stream->AddTrack(audio_track.get());
zhihuang9763d562016-08-05 11:14:50 -0700837 rtc::scoped_refptr<VideoTrackInterface> video_track(
nisseaf510af2016-03-21 08:20:42 -0700838 pc_factory_->CreateVideoTrack(
deadbeef112b2e92017-02-10 20:13:37 -0800839 video_track_label, pc_factory_->CreateVideoSource(
840 std::unique_ptr<cricket::VideoCapturer>(
841 new cricket::FakeVideoCapturer()))));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000842 stream->AddTrack(video_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +0000843 EXPECT_TRUE(pc_->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000844 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
845 observer_.renegotiation_needed_ = false;
846 }
847
kwibergd1fe2812016-04-27 06:47:29 -0700848 bool DoCreateOfferAnswer(std::unique_ptr<SessionDescriptionInterface>* desc,
deadbeefc80741f2015-10-22 13:14:45 -0700849 bool offer,
850 MediaConstraintsInterface* constraints) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000851 rtc::scoped_refptr<MockCreateSessionDescriptionObserver>
852 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000853 MockCreateSessionDescriptionObserver>());
854 if (offer) {
deadbeefc80741f2015-10-22 13:14:45 -0700855 pc_->CreateOffer(observer, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000856 } else {
deadbeefc80741f2015-10-22 13:14:45 -0700857 pc_->CreateAnswer(observer, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000858 }
859 EXPECT_EQ_WAIT(true, observer->called(), kTimeout);
kwiberg2bbff992016-03-16 11:03:04 -0700860 desc->reset(observer->release_desc());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000861 return observer->result();
862 }
863
kwibergd1fe2812016-04-27 06:47:29 -0700864 bool DoCreateOffer(std::unique_ptr<SessionDescriptionInterface>* desc,
deadbeefc80741f2015-10-22 13:14:45 -0700865 MediaConstraintsInterface* constraints) {
866 return DoCreateOfferAnswer(desc, true, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000867 }
868
kwibergd1fe2812016-04-27 06:47:29 -0700869 bool DoCreateAnswer(std::unique_ptr<SessionDescriptionInterface>* desc,
deadbeefc80741f2015-10-22 13:14:45 -0700870 MediaConstraintsInterface* constraints) {
871 return DoCreateOfferAnswer(desc, false, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000872 }
873
874 bool DoSetSessionDescription(SessionDescriptionInterface* desc, bool local) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000875 rtc::scoped_refptr<MockSetSessionDescriptionObserver>
876 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000877 MockSetSessionDescriptionObserver>());
878 if (local) {
879 pc_->SetLocalDescription(observer, desc);
880 } else {
881 pc_->SetRemoteDescription(observer, desc);
882 }
zhihuang29ff8442016-07-27 11:07:25 -0700883 if (pc_->signaling_state() != PeerConnectionInterface::kClosed) {
884 EXPECT_EQ_WAIT(true, observer->called(), kTimeout);
885 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000886 return observer->result();
887 }
888
889 bool DoSetLocalDescription(SessionDescriptionInterface* desc) {
890 return DoSetSessionDescription(desc, true);
891 }
892
893 bool DoSetRemoteDescription(SessionDescriptionInterface* desc) {
894 return DoSetSessionDescription(desc, false);
895 }
896
897 // Calls PeerConnection::GetStats and check the return value.
898 // It does not verify the values in the StatReports since a RTCP packet might
899 // be required.
900 bool DoGetStats(MediaStreamTrackInterface* track) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000901 rtc::scoped_refptr<MockStatsObserver> observer(
902 new rtc::RefCountedObject<MockStatsObserver>());
jiayl@webrtc.orgdb41b4d2014-03-03 21:30:06 +0000903 if (!pc_->GetStats(
904 observer, track, PeerConnectionInterface::kStatsOutputLevelStandard))
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000905 return false;
906 EXPECT_TRUE_WAIT(observer->called(), kTimeout);
907 return observer->called();
908 }
909
910 void InitiateCall() {
deadbeef293e9262017-01-11 12:28:30 -0800911 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000912 // Create a local stream with audio&video tracks.
913 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
914 CreateOfferReceiveAnswer();
915 }
916
917 // Verify that RTP Header extensions has been negotiated for audio and video.
918 void VerifyRemoteRtpHeaderExtensions() {
919 const cricket::MediaContentDescription* desc =
920 cricket::GetFirstAudioContentDescription(
921 pc_->remote_description()->description());
922 ASSERT_TRUE(desc != NULL);
923 EXPECT_GT(desc->rtp_header_extensions().size(), 0u);
924
925 desc = cricket::GetFirstVideoContentDescription(
926 pc_->remote_description()->description());
927 ASSERT_TRUE(desc != NULL);
928 EXPECT_GT(desc->rtp_header_extensions().size(), 0u);
929 }
930
931 void CreateOfferAsRemoteDescription() {
kwibergd1fe2812016-04-27 06:47:29 -0700932 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -0700933 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000934 std::string sdp;
935 EXPECT_TRUE(offer->ToString(&sdp));
936 SessionDescriptionInterface* remote_offer =
937 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
938 sdp, NULL);
939 EXPECT_TRUE(DoSetRemoteDescription(remote_offer));
940 EXPECT_EQ(PeerConnectionInterface::kHaveRemoteOffer, observer_.state_);
941 }
942
deadbeefab9b2d12015-10-14 11:33:11 -0700943 void CreateAndSetRemoteOffer(const std::string& sdp) {
944 SessionDescriptionInterface* remote_offer =
945 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
946 sdp, nullptr);
947 EXPECT_TRUE(DoSetRemoteDescription(remote_offer));
948 EXPECT_EQ(PeerConnectionInterface::kHaveRemoteOffer, observer_.state_);
949 }
950
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000951 void CreateAnswerAsLocalDescription() {
kwibergd1fe2812016-04-27 06:47:29 -0700952 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -0700953 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000954
955 // TODO(perkj): Currently SetLocalDescription fails if any parameters in an
956 // audio codec change, even if the parameter has nothing to do with
957 // receiving. Not all parameters are serialized to SDP.
958 // Since CreatePrAnswerAsLocalDescription serialize/deserialize
959 // the SessionDescription, it is necessary to do that here to in order to
960 // get ReceiveOfferCreatePrAnswerAndAnswer and RenegotiateAudioOnly to pass.
961 // https://code.google.com/p/webrtc/issues/detail?id=1356
962 std::string sdp;
963 EXPECT_TRUE(answer->ToString(&sdp));
964 SessionDescriptionInterface* new_answer =
965 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
966 sdp, NULL);
967 EXPECT_TRUE(DoSetLocalDescription(new_answer));
968 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
969 }
970
971 void CreatePrAnswerAsLocalDescription() {
kwibergd1fe2812016-04-27 06:47:29 -0700972 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -0700973 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000974
975 std::string sdp;
976 EXPECT_TRUE(answer->ToString(&sdp));
977 SessionDescriptionInterface* pr_answer =
978 webrtc::CreateSessionDescription(SessionDescriptionInterface::kPrAnswer,
979 sdp, NULL);
980 EXPECT_TRUE(DoSetLocalDescription(pr_answer));
981 EXPECT_EQ(PeerConnectionInterface::kHaveLocalPrAnswer, observer_.state_);
982 }
983
984 void CreateOfferReceiveAnswer() {
985 CreateOfferAsLocalDescription();
986 std::string sdp;
987 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
988 CreateAnswerAsRemoteDescription(sdp);
989 }
990
991 void CreateOfferAsLocalDescription() {
kwibergd1fe2812016-04-27 06:47:29 -0700992 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -0700993 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000994 // TODO(perkj): Currently SetLocalDescription fails if any parameters in an
995 // audio codec change, even if the parameter has nothing to do with
996 // receiving. Not all parameters are serialized to SDP.
997 // Since CreatePrAnswerAsLocalDescription serialize/deserialize
998 // the SessionDescription, it is necessary to do that here to in order to
999 // get ReceiveOfferCreatePrAnswerAndAnswer and RenegotiateAudioOnly to pass.
1000 // https://code.google.com/p/webrtc/issues/detail?id=1356
1001 std::string sdp;
1002 EXPECT_TRUE(offer->ToString(&sdp));
1003 SessionDescriptionInterface* new_offer =
1004 webrtc::CreateSessionDescription(
1005 SessionDescriptionInterface::kOffer,
1006 sdp, NULL);
1007
1008 EXPECT_TRUE(DoSetLocalDescription(new_offer));
1009 EXPECT_EQ(PeerConnectionInterface::kHaveLocalOffer, observer_.state_);
mallinath@webrtc.org68cbd012014-01-22 00:16:46 +00001010 // Wait for the ice_complete message, so that SDP will have candidates.
1011 EXPECT_TRUE_WAIT(observer_.ice_complete_, kTimeout);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001012 }
1013
deadbeefab9b2d12015-10-14 11:33:11 -07001014 void CreateAnswerAsRemoteDescription(const std::string& sdp) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001015 webrtc::JsepSessionDescription* answer = new webrtc::JsepSessionDescription(
1016 SessionDescriptionInterface::kAnswer);
deadbeefab9b2d12015-10-14 11:33:11 -07001017 EXPECT_TRUE(answer->Initialize(sdp, NULL));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001018 EXPECT_TRUE(DoSetRemoteDescription(answer));
1019 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
1020 }
1021
deadbeefab9b2d12015-10-14 11:33:11 -07001022 void CreatePrAnswerAndAnswerAsRemoteDescription(const std::string& sdp) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001023 webrtc::JsepSessionDescription* pr_answer =
1024 new webrtc::JsepSessionDescription(
1025 SessionDescriptionInterface::kPrAnswer);
deadbeefab9b2d12015-10-14 11:33:11 -07001026 EXPECT_TRUE(pr_answer->Initialize(sdp, NULL));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001027 EXPECT_TRUE(DoSetRemoteDescription(pr_answer));
1028 EXPECT_EQ(PeerConnectionInterface::kHaveRemotePrAnswer, observer_.state_);
1029 webrtc::JsepSessionDescription* answer =
1030 new webrtc::JsepSessionDescription(
1031 SessionDescriptionInterface::kAnswer);
deadbeefab9b2d12015-10-14 11:33:11 -07001032 EXPECT_TRUE(answer->Initialize(sdp, NULL));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001033 EXPECT_TRUE(DoSetRemoteDescription(answer));
1034 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
1035 }
1036
1037 // Help function used for waiting until a the last signaled remote stream has
1038 // the same label as |stream_label|. In a few of the tests in this file we
1039 // answer with the same session description as we offer and thus we can
1040 // check if OnAddStream have been called with the same stream as we offer to
1041 // send.
1042 void WaitAndVerifyOnAddStream(const std::string& stream_label) {
1043 EXPECT_EQ_WAIT(stream_label, observer_.GetLastAddedStreamLabel(), kTimeout);
1044 }
1045
1046 // Creates an offer and applies it as a local session description.
1047 // Creates an answer with the same SDP an the offer but removes all lines
1048 // that start with a:ssrc"
1049 void CreateOfferReceiveAnswerWithoutSsrc() {
1050 CreateOfferAsLocalDescription();
1051 std::string sdp;
1052 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
1053 SetSsrcToZero(&sdp);
1054 CreateAnswerAsRemoteDescription(sdp);
1055 }
1056
deadbeefab9b2d12015-10-14 11:33:11 -07001057 // This function creates a MediaStream with label kStreams[0] and
1058 // |number_of_audio_tracks| and |number_of_video_tracks| tracks and the
1059 // corresponding SessionDescriptionInterface. The SessionDescriptionInterface
kwiberg2bbff992016-03-16 11:03:04 -07001060 // is returned and the MediaStream is stored in
deadbeefab9b2d12015-10-14 11:33:11 -07001061 // |reference_collection_|
kwibergd1fe2812016-04-27 06:47:29 -07001062 std::unique_ptr<SessionDescriptionInterface>
kwiberg2bbff992016-03-16 11:03:04 -07001063 CreateSessionDescriptionAndReference(size_t number_of_audio_tracks,
1064 size_t number_of_video_tracks) {
1065 EXPECT_LE(number_of_audio_tracks, 2u);
1066 EXPECT_LE(number_of_video_tracks, 2u);
deadbeefab9b2d12015-10-14 11:33:11 -07001067
1068 reference_collection_ = StreamCollection::Create();
1069 std::string sdp_ms1 = std::string(kSdpStringInit);
1070
1071 std::string mediastream_label = kStreams[0];
1072
1073 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream(
1074 webrtc::MediaStream::Create(mediastream_label));
1075 reference_collection_->AddStream(stream);
1076
1077 if (number_of_audio_tracks > 0) {
1078 sdp_ms1 += std::string(kSdpStringAudio);
1079 sdp_ms1 += std::string(kSdpStringMs1Audio0);
1080 AddAudioTrack(kAudioTracks[0], stream);
1081 }
1082 if (number_of_audio_tracks > 1) {
1083 sdp_ms1 += kSdpStringMs1Audio1;
1084 AddAudioTrack(kAudioTracks[1], stream);
1085 }
1086
1087 if (number_of_video_tracks > 0) {
1088 sdp_ms1 += std::string(kSdpStringVideo);
1089 sdp_ms1 += std::string(kSdpStringMs1Video0);
1090 AddVideoTrack(kVideoTracks[0], stream);
1091 }
1092 if (number_of_video_tracks > 1) {
1093 sdp_ms1 += kSdpStringMs1Video1;
1094 AddVideoTrack(kVideoTracks[1], stream);
1095 }
1096
kwibergd1fe2812016-04-27 06:47:29 -07001097 return std::unique_ptr<SessionDescriptionInterface>(
kwiberg2bbff992016-03-16 11:03:04 -07001098 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
1099 sdp_ms1, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07001100 }
1101
1102 void AddAudioTrack(const std::string& track_id,
1103 MediaStreamInterface* stream) {
1104 rtc::scoped_refptr<webrtc::AudioTrackInterface> audio_track(
1105 webrtc::AudioTrack::Create(track_id, nullptr));
1106 ASSERT_TRUE(stream->AddTrack(audio_track));
1107 }
1108
1109 void AddVideoTrack(const std::string& track_id,
1110 MediaStreamInterface* stream) {
1111 rtc::scoped_refptr<webrtc::VideoTrackInterface> video_track(
nisseaf510af2016-03-21 08:20:42 -07001112 webrtc::VideoTrack::Create(track_id,
1113 webrtc::FakeVideoTrackSource::Create()));
deadbeefab9b2d12015-10-14 11:33:11 -07001114 ASSERT_TRUE(stream->AddTrack(video_track));
1115 }
1116
kwibergfd8be342016-05-14 19:44:11 -07001117 std::unique_ptr<SessionDescriptionInterface> CreateOfferWithOneAudioStream() {
deadbeef293e9262017-01-11 12:28:30 -08001118 CreatePeerConnectionWithoutDtls();
zhihuang8f65cdf2016-05-06 18:40:30 -07001119 AddVoiceStream(kStreamLabel1);
kwibergfd8be342016-05-14 19:44:11 -07001120 std::unique_ptr<SessionDescriptionInterface> offer;
zhihuang8f65cdf2016-05-06 18:40:30 -07001121 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
1122 return offer;
1123 }
1124
kwibergfd8be342016-05-14 19:44:11 -07001125 std::unique_ptr<SessionDescriptionInterface>
zhihuang8f65cdf2016-05-06 18:40:30 -07001126 CreateAnswerWithOneAudioStream() {
kwibergfd8be342016-05-14 19:44:11 -07001127 std::unique_ptr<SessionDescriptionInterface> offer =
zhihuang8f65cdf2016-05-06 18:40:30 -07001128 CreateOfferWithOneAudioStream();
1129 EXPECT_TRUE(DoSetRemoteDescription(offer.release()));
kwibergfd8be342016-05-14 19:44:11 -07001130 std::unique_ptr<SessionDescriptionInterface> answer;
zhihuang8f65cdf2016-05-06 18:40:30 -07001131 EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
1132 return answer;
1133 }
1134
1135 const std::string& GetFirstAudioStreamCname(
1136 const SessionDescriptionInterface* desc) {
1137 const cricket::ContentInfo* audio_content =
1138 cricket::GetFirstAudioContent(desc->description());
1139 const cricket::AudioContentDescription* audio_desc =
1140 static_cast<const cricket::AudioContentDescription*>(
1141 audio_content->description);
1142 return audio_desc->streams()[0].cname;
1143 }
1144
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08001145 cricket::FakePortAllocator* port_allocator_ = nullptr;
deadbeef8662f942017-01-20 21:20:51 -08001146 FakeRTCCertificateGenerator* fake_certificate_generator_ = nullptr;
zhihuang9763d562016-08-05 11:14:50 -07001147 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface> pc_factory_;
1148 rtc::scoped_refptr<PeerConnectionFactoryForTest> pc_factory_for_test_;
1149 rtc::scoped_refptr<PeerConnectionInterface> pc_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001150 MockPeerConnectionObserver observer_;
deadbeefab9b2d12015-10-14 11:33:11 -07001151 rtc::scoped_refptr<StreamCollection> reference_collection_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001152};
1153
zhihuang29ff8442016-07-27 11:07:25 -07001154// Test that no callbacks on the PeerConnectionObserver are called after the
1155// PeerConnection is closed.
1156TEST_F(PeerConnectionInterfaceTest, CloseAndTestCallbackFunctions) {
zhihuang9763d562016-08-05 11:14:50 -07001157 rtc::scoped_refptr<PeerConnectionInterface> pc(
zhihuang29ff8442016-07-27 11:07:25 -07001158 pc_factory_for_test_->CreatePeerConnection(
1159 PeerConnectionInterface::RTCConfiguration(), nullptr, nullptr,
1160 nullptr, &observer_));
1161 observer_.SetPeerConnectionInterface(pc.get());
1162 pc->Close();
1163
1164 // No callbacks is expected to be called.
zhihuang81c3a032016-11-17 12:06:24 -08001165 observer_.callback_triggered_ = false;
zhihuang29ff8442016-07-27 11:07:25 -07001166 std::vector<cricket::Candidate> candidates;
1167 pc_factory_for_test_->transport_controller->SignalGatheringState(
1168 cricket::IceGatheringState{});
1169 pc_factory_for_test_->transport_controller->SignalCandidatesGathered(
1170 "", candidates);
1171 pc_factory_for_test_->transport_controller->SignalConnectionState(
1172 cricket::IceConnectionState{});
1173 pc_factory_for_test_->transport_controller->SignalCandidatesRemoved(
1174 candidates);
1175 pc_factory_for_test_->transport_controller->SignalReceiving(false);
zhihuang81c3a032016-11-17 12:06:24 -08001176 EXPECT_FALSE(observer_.callback_triggered_);
zhihuang29ff8442016-07-27 11:07:25 -07001177}
1178
zhihuang8f65cdf2016-05-06 18:40:30 -07001179// Generate different CNAMEs when PeerConnections are created.
1180// The CNAMEs are expected to be generated randomly. It is possible
1181// that the test fails, though the possibility is very low.
1182TEST_F(PeerConnectionInterfaceTest, CnameGenerationInOffer) {
kwibergfd8be342016-05-14 19:44:11 -07001183 std::unique_ptr<SessionDescriptionInterface> offer1 =
zhihuang8f65cdf2016-05-06 18:40:30 -07001184 CreateOfferWithOneAudioStream();
kwibergfd8be342016-05-14 19:44:11 -07001185 std::unique_ptr<SessionDescriptionInterface> offer2 =
zhihuang8f65cdf2016-05-06 18:40:30 -07001186 CreateOfferWithOneAudioStream();
1187 EXPECT_NE(GetFirstAudioStreamCname(offer1.get()),
1188 GetFirstAudioStreamCname(offer2.get()));
1189}
1190
1191TEST_F(PeerConnectionInterfaceTest, CnameGenerationInAnswer) {
kwibergfd8be342016-05-14 19:44:11 -07001192 std::unique_ptr<SessionDescriptionInterface> answer1 =
zhihuang8f65cdf2016-05-06 18:40:30 -07001193 CreateAnswerWithOneAudioStream();
kwibergfd8be342016-05-14 19:44:11 -07001194 std::unique_ptr<SessionDescriptionInterface> answer2 =
zhihuang8f65cdf2016-05-06 18:40:30 -07001195 CreateAnswerWithOneAudioStream();
1196 EXPECT_NE(GetFirstAudioStreamCname(answer1.get()),
1197 GetFirstAudioStreamCname(answer2.get()));
1198}
1199
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001200TEST_F(PeerConnectionInterfaceTest,
1201 CreatePeerConnectionWithDifferentConfigurations) {
1202 CreatePeerConnectionWithDifferentConfigurations();
1203}
1204
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001205TEST_F(PeerConnectionInterfaceTest,
1206 CreatePeerConnectionWithDifferentIceTransportsTypes) {
1207 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kNone);
1208 EXPECT_EQ(cricket::CF_NONE, port_allocator_->candidate_filter());
1209 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kRelay);
1210 EXPECT_EQ(cricket::CF_RELAY, port_allocator_->candidate_filter());
1211 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kNoHost);
1212 EXPECT_EQ(cricket::CF_ALL & ~cricket::CF_HOST,
1213 port_allocator_->candidate_filter());
1214 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kAll);
1215 EXPECT_EQ(cricket::CF_ALL, port_allocator_->candidate_filter());
1216}
1217
1218// Test that when a PeerConnection is created with a nonzero candidate pool
1219// size, the pooled PortAllocatorSession is created with all the attributes
1220// in the RTCConfiguration.
1221TEST_F(PeerConnectionInterfaceTest, CreatePeerConnectionWithPooledCandidates) {
1222 PeerConnectionInterface::RTCConfiguration config;
1223 PeerConnectionInterface::IceServer server;
1224 server.uri = kStunAddressOnly;
1225 config.servers.push_back(server);
1226 config.type = PeerConnectionInterface::kRelay;
1227 config.disable_ipv6 = true;
1228 config.tcp_candidate_policy =
1229 PeerConnectionInterface::kTcpCandidatePolicyDisabled;
honghaiz60347052016-05-31 18:29:12 -07001230 config.candidate_network_policy =
1231 PeerConnectionInterface::kCandidateNetworkPolicyLowCost;
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001232 config.ice_candidate_pool_size = 1;
1233 CreatePeerConnection(config, nullptr);
1234
1235 const cricket::FakePortAllocatorSession* session =
1236 static_cast<const cricket::FakePortAllocatorSession*>(
1237 port_allocator_->GetPooledSession());
1238 ASSERT_NE(nullptr, session);
1239 EXPECT_EQ(1UL, session->stun_servers().size());
1240 EXPECT_EQ(0U, session->flags() & cricket::PORTALLOCATOR_ENABLE_IPV6);
1241 EXPECT_LT(0U, session->flags() & cricket::PORTALLOCATOR_DISABLE_TCP);
honghaiz60347052016-05-31 18:29:12 -07001242 EXPECT_LT(0U,
1243 session->flags() & cricket::PORTALLOCATOR_DISABLE_COSTLY_NETWORKS);
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001244}
1245
Taylor Brandstetterf8e65772016-06-27 17:20:15 -07001246// Test that the PeerConnection initializes the port allocator passed into it,
1247// and on the correct thread.
1248TEST_F(PeerConnectionInterfaceTest,
1249 CreatePeerConnectionInitializesPortAllocator) {
1250 rtc::Thread network_thread;
1251 network_thread.Start();
1252 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface> pc_factory(
1253 webrtc::CreatePeerConnectionFactory(
1254 &network_thread, rtc::Thread::Current(), rtc::Thread::Current(),
1255 nullptr, nullptr, nullptr));
1256 std::unique_ptr<cricket::FakePortAllocator> port_allocator(
1257 new cricket::FakePortAllocator(&network_thread, nullptr));
1258 cricket::FakePortAllocator* raw_port_allocator = port_allocator.get();
1259 PeerConnectionInterface::RTCConfiguration config;
1260 rtc::scoped_refptr<PeerConnectionInterface> pc(
1261 pc_factory->CreatePeerConnection(
1262 config, nullptr, std::move(port_allocator), nullptr, &observer_));
1263 // FakePortAllocator RTC_CHECKs that it's initialized on the right thread,
1264 // so all we have to do here is check that it's initialized.
1265 EXPECT_TRUE(raw_port_allocator->initialized());
1266}
1267
deadbeef46c73892016-11-16 19:42:04 -08001268// Check that GetConfiguration returns the configuration the PeerConnection was
1269// constructed with, before SetConfiguration is called.
1270TEST_F(PeerConnectionInterfaceTest, GetConfigurationAfterCreatePeerConnection) {
1271 PeerConnectionInterface::RTCConfiguration config;
1272 config.type = PeerConnectionInterface::kRelay;
1273 CreatePeerConnection(config, nullptr);
1274
1275 PeerConnectionInterface::RTCConfiguration returned_config =
1276 pc_->GetConfiguration();
1277 EXPECT_EQ(PeerConnectionInterface::kRelay, returned_config.type);
1278}
1279
1280// Check that GetConfiguration returns the last configuration passed into
1281// SetConfiguration.
1282TEST_F(PeerConnectionInterfaceTest, GetConfigurationAfterSetConfiguration) {
1283 CreatePeerConnection();
1284
1285 PeerConnectionInterface::RTCConfiguration config;
1286 config.type = PeerConnectionInterface::kRelay;
1287 EXPECT_TRUE(pc_->SetConfiguration(config));
1288
1289 PeerConnectionInterface::RTCConfiguration returned_config =
1290 pc_->GetConfiguration();
1291 EXPECT_EQ(PeerConnectionInterface::kRelay, returned_config.type);
1292}
1293
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001294TEST_F(PeerConnectionInterfaceTest, AddStreams) {
deadbeef293e9262017-01-11 12:28:30 -08001295 CreatePeerConnectionWithoutDtls();
deadbeefab9b2d12015-10-14 11:33:11 -07001296 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001297 AddVoiceStream(kStreamLabel2);
1298 ASSERT_EQ(2u, pc_->local_streams()->count());
1299
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001300 // Test we can add multiple local streams to one peerconnection.
zhihuang9763d562016-08-05 11:14:50 -07001301 rtc::scoped_refptr<MediaStreamInterface> stream(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001302 pc_factory_->CreateLocalMediaStream(kStreamLabel3));
zhihuang9763d562016-08-05 11:14:50 -07001303 rtc::scoped_refptr<AudioTrackInterface> audio_track(
1304 pc_factory_->CreateAudioTrack(kStreamLabel3,
1305 static_cast<AudioSourceInterface*>(NULL)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001306 stream->AddTrack(audio_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +00001307 EXPECT_TRUE(pc_->AddStream(stream));
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001308 EXPECT_EQ(3u, pc_->local_streams()->count());
1309
1310 // Remove the third stream.
1311 pc_->RemoveStream(pc_->local_streams()->at(2));
1312 EXPECT_EQ(2u, pc_->local_streams()->count());
1313
1314 // Remove the second stream.
1315 pc_->RemoveStream(pc_->local_streams()->at(1));
1316 EXPECT_EQ(1u, pc_->local_streams()->count());
1317
1318 // Remove the first stream.
1319 pc_->RemoveStream(pc_->local_streams()->at(0));
1320 EXPECT_EQ(0u, pc_->local_streams()->count());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001321}
1322
deadbeefab9b2d12015-10-14 11:33:11 -07001323// Test that the created offer includes streams we added.
1324TEST_F(PeerConnectionInterfaceTest, AddedStreamsPresentInOffer) {
deadbeef293e9262017-01-11 12:28:30 -08001325 CreatePeerConnectionWithoutDtls();
deadbeefab9b2d12015-10-14 11:33:11 -07001326 AddAudioVideoStream(kStreamLabel1, "audio_track", "video_track");
kwibergd1fe2812016-04-27 06:47:29 -07001327 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001328 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07001329
1330 const cricket::ContentInfo* audio_content =
1331 cricket::GetFirstAudioContent(offer->description());
1332 const cricket::AudioContentDescription* audio_desc =
1333 static_cast<const cricket::AudioContentDescription*>(
1334 audio_content->description);
1335 EXPECT_TRUE(
1336 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
1337
1338 const cricket::ContentInfo* video_content =
1339 cricket::GetFirstVideoContent(offer->description());
1340 const cricket::VideoContentDescription* video_desc =
1341 static_cast<const cricket::VideoContentDescription*>(
1342 video_content->description);
1343 EXPECT_TRUE(
1344 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1345
1346 // Add another stream and ensure the offer includes both the old and new
1347 // streams.
1348 AddAudioVideoStream(kStreamLabel2, "audio_track2", "video_track2");
kwiberg2bbff992016-03-16 11:03:04 -07001349 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07001350
1351 audio_content = cricket::GetFirstAudioContent(offer->description());
1352 audio_desc = static_cast<const cricket::AudioContentDescription*>(
1353 audio_content->description);
1354 EXPECT_TRUE(
1355 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
1356 EXPECT_TRUE(
1357 ContainsTrack(audio_desc->streams(), kStreamLabel2, "audio_track2"));
1358
1359 video_content = cricket::GetFirstVideoContent(offer->description());
1360 video_desc = static_cast<const cricket::VideoContentDescription*>(
1361 video_content->description);
1362 EXPECT_TRUE(
1363 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1364 EXPECT_TRUE(
1365 ContainsTrack(video_desc->streams(), kStreamLabel2, "video_track2"));
1366}
1367
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001368TEST_F(PeerConnectionInterfaceTest, RemoveStream) {
deadbeef293e9262017-01-11 12:28:30 -08001369 CreatePeerConnectionWithoutDtls();
deadbeefab9b2d12015-10-14 11:33:11 -07001370 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001371 ASSERT_EQ(1u, pc_->local_streams()->count());
1372 pc_->RemoveStream(pc_->local_streams()->at(0));
1373 EXPECT_EQ(0u, pc_->local_streams()->count());
1374}
1375
deadbeefe1f9d832016-01-14 15:35:42 -08001376// Test for AddTrack and RemoveTrack methods.
1377// Tests that the created offer includes tracks we added,
1378// and that the RtpSenders are created correctly.
1379// Also tests that RemoveTrack removes the tracks from subsequent offers.
1380TEST_F(PeerConnectionInterfaceTest, AddTrackRemoveTrack) {
deadbeef293e9262017-01-11 12:28:30 -08001381 CreatePeerConnectionWithoutDtls();
deadbeefe1f9d832016-01-14 15:35:42 -08001382 // Create a dummy stream, so tracks share a stream label.
zhihuang9763d562016-08-05 11:14:50 -07001383 rtc::scoped_refptr<MediaStreamInterface> stream(
deadbeefe1f9d832016-01-14 15:35:42 -08001384 pc_factory_->CreateLocalMediaStream(kStreamLabel1));
1385 std::vector<MediaStreamInterface*> stream_list;
1386 stream_list.push_back(stream.get());
zhihuang9763d562016-08-05 11:14:50 -07001387 rtc::scoped_refptr<AudioTrackInterface> audio_track(
deadbeefe1f9d832016-01-14 15:35:42 -08001388 pc_factory_->CreateAudioTrack("audio_track", nullptr));
zhihuang9763d562016-08-05 11:14:50 -07001389 rtc::scoped_refptr<VideoTrackInterface> video_track(
1390 pc_factory_->CreateVideoTrack(
deadbeef112b2e92017-02-10 20:13:37 -08001391 "video_track", pc_factory_->CreateVideoSource(
1392 std::unique_ptr<cricket::VideoCapturer>(
1393 new cricket::FakeVideoCapturer()))));
deadbeefe1f9d832016-01-14 15:35:42 -08001394 auto audio_sender = pc_->AddTrack(audio_track, stream_list);
1395 auto video_sender = pc_->AddTrack(video_track, stream_list);
deadbeefa601f5c2016-06-06 14:27:39 -07001396 EXPECT_EQ(1UL, audio_sender->stream_ids().size());
1397 EXPECT_EQ(kStreamLabel1, audio_sender->stream_ids()[0]);
deadbeefe1f9d832016-01-14 15:35:42 -08001398 EXPECT_EQ("audio_track", audio_sender->id());
1399 EXPECT_EQ(audio_track, audio_sender->track());
deadbeefa601f5c2016-06-06 14:27:39 -07001400 EXPECT_EQ(1UL, video_sender->stream_ids().size());
1401 EXPECT_EQ(kStreamLabel1, video_sender->stream_ids()[0]);
deadbeefe1f9d832016-01-14 15:35:42 -08001402 EXPECT_EQ("video_track", video_sender->id());
1403 EXPECT_EQ(video_track, video_sender->track());
1404
1405 // Now create an offer and check for the senders.
kwibergd1fe2812016-04-27 06:47:29 -07001406 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001407 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefe1f9d832016-01-14 15:35:42 -08001408
1409 const cricket::ContentInfo* audio_content =
1410 cricket::GetFirstAudioContent(offer->description());
1411 const cricket::AudioContentDescription* audio_desc =
1412 static_cast<const cricket::AudioContentDescription*>(
1413 audio_content->description);
1414 EXPECT_TRUE(
1415 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
1416
1417 const cricket::ContentInfo* video_content =
1418 cricket::GetFirstVideoContent(offer->description());
1419 const cricket::VideoContentDescription* video_desc =
1420 static_cast<const cricket::VideoContentDescription*>(
1421 video_content->description);
1422 EXPECT_TRUE(
1423 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1424
1425 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
1426
1427 // Now try removing the tracks.
1428 EXPECT_TRUE(pc_->RemoveTrack(audio_sender));
1429 EXPECT_TRUE(pc_->RemoveTrack(video_sender));
1430
1431 // Create a new offer and ensure it doesn't contain the removed senders.
kwiberg2bbff992016-03-16 11:03:04 -07001432 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefe1f9d832016-01-14 15:35:42 -08001433
1434 audio_content = cricket::GetFirstAudioContent(offer->description());
1435 audio_desc = static_cast<const cricket::AudioContentDescription*>(
1436 audio_content->description);
1437 EXPECT_FALSE(
1438 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
1439
1440 video_content = cricket::GetFirstVideoContent(offer->description());
1441 video_desc = static_cast<const cricket::VideoContentDescription*>(
1442 video_content->description);
1443 EXPECT_FALSE(
1444 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1445
1446 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
1447
1448 // Calling RemoveTrack on a sender no longer attached to a PeerConnection
1449 // should return false.
1450 EXPECT_FALSE(pc_->RemoveTrack(audio_sender));
1451 EXPECT_FALSE(pc_->RemoveTrack(video_sender));
1452}
1453
1454// Test creating senders without a stream specified,
1455// expecting a random stream ID to be generated.
1456TEST_F(PeerConnectionInterfaceTest, AddTrackWithoutStream) {
deadbeef293e9262017-01-11 12:28:30 -08001457 CreatePeerConnectionWithoutDtls();
deadbeefe1f9d832016-01-14 15:35:42 -08001458 // Create a dummy stream, so tracks share a stream label.
zhihuang9763d562016-08-05 11:14:50 -07001459 rtc::scoped_refptr<AudioTrackInterface> audio_track(
deadbeefe1f9d832016-01-14 15:35:42 -08001460 pc_factory_->CreateAudioTrack("audio_track", nullptr));
zhihuang9763d562016-08-05 11:14:50 -07001461 rtc::scoped_refptr<VideoTrackInterface> video_track(
1462 pc_factory_->CreateVideoTrack(
deadbeef112b2e92017-02-10 20:13:37 -08001463 "video_track", pc_factory_->CreateVideoSource(
1464 std::unique_ptr<cricket::VideoCapturer>(
1465 new cricket::FakeVideoCapturer()))));
deadbeefe1f9d832016-01-14 15:35:42 -08001466 auto audio_sender =
1467 pc_->AddTrack(audio_track, std::vector<MediaStreamInterface*>());
1468 auto video_sender =
1469 pc_->AddTrack(video_track, std::vector<MediaStreamInterface*>());
1470 EXPECT_EQ("audio_track", audio_sender->id());
1471 EXPECT_EQ(audio_track, audio_sender->track());
1472 EXPECT_EQ("video_track", video_sender->id());
1473 EXPECT_EQ(video_track, video_sender->track());
1474 // If the ID is truly a random GUID, it should be infinitely unlikely they
1475 // will be the same.
deadbeefa601f5c2016-06-06 14:27:39 -07001476 EXPECT_NE(video_sender->stream_ids(), audio_sender->stream_ids());
deadbeefe1f9d832016-01-14 15:35:42 -08001477}
1478
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001479TEST_F(PeerConnectionInterfaceTest, CreateOfferReceiveAnswer) {
1480 InitiateCall();
1481 WaitAndVerifyOnAddStream(kStreamLabel1);
1482 VerifyRemoteRtpHeaderExtensions();
1483}
1484
1485TEST_F(PeerConnectionInterfaceTest, CreateOfferReceivePrAnswerAndAnswer) {
deadbeef293e9262017-01-11 12:28:30 -08001486 CreatePeerConnectionWithoutDtls();
deadbeefab9b2d12015-10-14 11:33:11 -07001487 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001488 CreateOfferAsLocalDescription();
1489 std::string offer;
1490 EXPECT_TRUE(pc_->local_description()->ToString(&offer));
1491 CreatePrAnswerAndAnswerAsRemoteDescription(offer);
1492 WaitAndVerifyOnAddStream(kStreamLabel1);
1493}
1494
1495TEST_F(PeerConnectionInterfaceTest, ReceiveOfferCreateAnswer) {
deadbeef293e9262017-01-11 12:28:30 -08001496 CreatePeerConnectionWithoutDtls();
deadbeefab9b2d12015-10-14 11:33:11 -07001497 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001498
1499 CreateOfferAsRemoteDescription();
1500 CreateAnswerAsLocalDescription();
1501
1502 WaitAndVerifyOnAddStream(kStreamLabel1);
1503}
1504
1505TEST_F(PeerConnectionInterfaceTest, ReceiveOfferCreatePrAnswerAndAnswer) {
deadbeef293e9262017-01-11 12:28:30 -08001506 CreatePeerConnectionWithoutDtls();
deadbeefab9b2d12015-10-14 11:33:11 -07001507 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001508
1509 CreateOfferAsRemoteDescription();
1510 CreatePrAnswerAsLocalDescription();
1511 CreateAnswerAsLocalDescription();
1512
1513 WaitAndVerifyOnAddStream(kStreamLabel1);
1514}
1515
1516TEST_F(PeerConnectionInterfaceTest, Renegotiate) {
1517 InitiateCall();
1518 ASSERT_EQ(1u, pc_->remote_streams()->count());
1519 pc_->RemoveStream(pc_->local_streams()->at(0));
1520 CreateOfferReceiveAnswer();
1521 EXPECT_EQ(0u, pc_->remote_streams()->count());
deadbeefab9b2d12015-10-14 11:33:11 -07001522 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001523 CreateOfferReceiveAnswer();
1524}
1525
1526// Tests that after negotiating an audio only call, the respondent can perform a
1527// renegotiation that removes the audio stream.
1528TEST_F(PeerConnectionInterfaceTest, RenegotiateAudioOnly) {
deadbeef293e9262017-01-11 12:28:30 -08001529 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001530 AddVoiceStream(kStreamLabel1);
1531 CreateOfferAsRemoteDescription();
1532 CreateAnswerAsLocalDescription();
1533
1534 ASSERT_EQ(1u, pc_->remote_streams()->count());
1535 pc_->RemoveStream(pc_->local_streams()->at(0));
1536 CreateOfferReceiveAnswer();
1537 EXPECT_EQ(0u, pc_->remote_streams()->count());
1538}
1539
1540// Test that candidates are generated and that we can parse our own candidates.
1541TEST_F(PeerConnectionInterfaceTest, IceCandidates) {
deadbeef293e9262017-01-11 12:28:30 -08001542 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001543
1544 EXPECT_FALSE(pc_->AddIceCandidate(observer_.last_candidate_.get()));
1545 // SetRemoteDescription takes ownership of offer.
kwibergd1fe2812016-04-27 06:47:29 -07001546 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefab9b2d12015-10-14 11:33:11 -07001547 AddVideoStream(kStreamLabel1);
deadbeefc80741f2015-10-22 13:14:45 -07001548 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
kwiberg2bbff992016-03-16 11:03:04 -07001549 EXPECT_TRUE(DoSetRemoteDescription(offer.release()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001550
1551 // SetLocalDescription takes ownership of answer.
kwibergd1fe2812016-04-27 06:47:29 -07001552 std::unique_ptr<SessionDescriptionInterface> answer;
deadbeefc80741f2015-10-22 13:14:45 -07001553 EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
kwiberg2bbff992016-03-16 11:03:04 -07001554 EXPECT_TRUE(DoSetLocalDescription(answer.release()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001555
1556 EXPECT_TRUE_WAIT(observer_.last_candidate_.get() != NULL, kTimeout);
1557 EXPECT_TRUE_WAIT(observer_.ice_complete_, kTimeout);
1558
1559 EXPECT_TRUE(pc_->AddIceCandidate(observer_.last_candidate_.get()));
1560}
1561
deadbeefab9b2d12015-10-14 11:33:11 -07001562// Test that CreateOffer and CreateAnswer will fail if the track labels are
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001563// not unique.
1564TEST_F(PeerConnectionInterfaceTest, CreateOfferAnswerWithInvalidStream) {
deadbeef293e9262017-01-11 12:28:30 -08001565 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001566 // Create a regular offer for the CreateAnswer test later.
kwibergd1fe2812016-04-27 06:47:29 -07001567 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefc80741f2015-10-22 13:14:45 -07001568 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
kwiberg2bbff992016-03-16 11:03:04 -07001569 EXPECT_TRUE(offer);
1570 offer.reset();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001571
1572 // Create a local stream with audio&video tracks having same label.
1573 AddAudioVideoStream(kStreamLabel1, "track_label", "track_label");
1574
1575 // Test CreateOffer
deadbeefc80741f2015-10-22 13:14:45 -07001576 EXPECT_FALSE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001577
1578 // Test CreateAnswer
kwibergd1fe2812016-04-27 06:47:29 -07001579 std::unique_ptr<SessionDescriptionInterface> answer;
deadbeefc80741f2015-10-22 13:14:45 -07001580 EXPECT_FALSE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001581}
1582
1583// Test that we will get different SSRCs for each tracks in the offer and answer
1584// we created.
1585TEST_F(PeerConnectionInterfaceTest, SsrcInOfferAnswer) {
deadbeef293e9262017-01-11 12:28:30 -08001586 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001587 // Create a local stream with audio&video tracks having different labels.
1588 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
1589
1590 // Test CreateOffer
kwibergd1fe2812016-04-27 06:47:29 -07001591 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001592 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001593 int audio_ssrc = 0;
1594 int video_ssrc = 0;
1595 EXPECT_TRUE(GetFirstSsrc(GetFirstAudioContent(offer->description()),
1596 &audio_ssrc));
1597 EXPECT_TRUE(GetFirstSsrc(GetFirstVideoContent(offer->description()),
1598 &video_ssrc));
1599 EXPECT_NE(audio_ssrc, video_ssrc);
1600
1601 // Test CreateAnswer
1602 EXPECT_TRUE(DoSetRemoteDescription(offer.release()));
kwibergd1fe2812016-04-27 06:47:29 -07001603 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -07001604 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001605 audio_ssrc = 0;
1606 video_ssrc = 0;
1607 EXPECT_TRUE(GetFirstSsrc(GetFirstAudioContent(answer->description()),
1608 &audio_ssrc));
1609 EXPECT_TRUE(GetFirstSsrc(GetFirstVideoContent(answer->description()),
1610 &video_ssrc));
1611 EXPECT_NE(audio_ssrc, video_ssrc);
1612}
1613
deadbeefeb459812015-12-15 19:24:43 -08001614// Test that it's possible to call AddTrack on a MediaStream after adding
1615// the stream to a PeerConnection.
1616// TODO(deadbeef): Remove this test once this behavior is no longer supported.
1617TEST_F(PeerConnectionInterfaceTest, AddTrackAfterAddStream) {
deadbeef293e9262017-01-11 12:28:30 -08001618 CreatePeerConnectionWithoutDtls();
deadbeefeb459812015-12-15 19:24:43 -08001619 // Create audio stream and add to PeerConnection.
1620 AddVoiceStream(kStreamLabel1);
1621 MediaStreamInterface* stream = pc_->local_streams()->at(0);
1622
1623 // Add video track to the audio-only stream.
zhihuang9763d562016-08-05 11:14:50 -07001624 rtc::scoped_refptr<VideoTrackInterface> video_track(
1625 pc_factory_->CreateVideoTrack(
deadbeef112b2e92017-02-10 20:13:37 -08001626 "video_label", pc_factory_->CreateVideoSource(
1627 std::unique_ptr<cricket::VideoCapturer>(
1628 new cricket::FakeVideoCapturer()))));
deadbeefeb459812015-12-15 19:24:43 -08001629 stream->AddTrack(video_track.get());
1630
kwibergd1fe2812016-04-27 06:47:29 -07001631 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001632 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefeb459812015-12-15 19:24:43 -08001633
1634 const cricket::MediaContentDescription* video_desc =
1635 cricket::GetFirstVideoContentDescription(offer->description());
1636 EXPECT_TRUE(video_desc != nullptr);
1637}
1638
1639// Test that it's possible to call RemoveTrack on a MediaStream after adding
1640// the stream to a PeerConnection.
1641// TODO(deadbeef): Remove this test once this behavior is no longer supported.
1642TEST_F(PeerConnectionInterfaceTest, RemoveTrackAfterAddStream) {
deadbeef293e9262017-01-11 12:28:30 -08001643 CreatePeerConnectionWithoutDtls();
deadbeefeb459812015-12-15 19:24:43 -08001644 // Create audio/video stream and add to PeerConnection.
1645 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
1646 MediaStreamInterface* stream = pc_->local_streams()->at(0);
1647
1648 // Remove the video track.
1649 stream->RemoveTrack(stream->GetVideoTracks()[0]);
1650
kwibergd1fe2812016-04-27 06:47:29 -07001651 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001652 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefeb459812015-12-15 19:24:43 -08001653
1654 const cricket::MediaContentDescription* video_desc =
1655 cricket::GetFirstVideoContentDescription(offer->description());
1656 EXPECT_TRUE(video_desc == nullptr);
1657}
1658
deadbeefbd7d8f72015-12-18 16:58:44 -08001659// Test creating a sender with a stream ID, and ensure the ID is populated
1660// in the offer.
1661TEST_F(PeerConnectionInterfaceTest, CreateSenderWithStream) {
deadbeef293e9262017-01-11 12:28:30 -08001662 CreatePeerConnectionWithoutDtls();
deadbeefbd7d8f72015-12-18 16:58:44 -08001663 pc_->CreateSender("video", kStreamLabel1);
1664
kwibergd1fe2812016-04-27 06:47:29 -07001665 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001666 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefbd7d8f72015-12-18 16:58:44 -08001667
1668 const cricket::MediaContentDescription* video_desc =
1669 cricket::GetFirstVideoContentDescription(offer->description());
1670 ASSERT_TRUE(video_desc != nullptr);
1671 ASSERT_EQ(1u, video_desc->streams().size());
1672 EXPECT_EQ(kStreamLabel1, video_desc->streams()[0].sync_label);
1673}
1674
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001675// Test that we can specify a certain track that we want statistics about.
1676TEST_F(PeerConnectionInterfaceTest, GetStatsForSpecificTrack) {
1677 InitiateCall();
1678 ASSERT_LT(0u, pc_->remote_streams()->count());
1679 ASSERT_LT(0u, pc_->remote_streams()->at(0)->GetAudioTracks().size());
zhihuang9763d562016-08-05 11:14:50 -07001680 rtc::scoped_refptr<MediaStreamTrackInterface> remote_audio =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001681 pc_->remote_streams()->at(0)->GetAudioTracks()[0];
1682 EXPECT_TRUE(DoGetStats(remote_audio));
1683
1684 // Remove the stream. Since we are sending to our selves the local
1685 // and the remote stream is the same.
1686 pc_->RemoveStream(pc_->local_streams()->at(0));
1687 // Do a re-negotiation.
1688 CreateOfferReceiveAnswer();
1689
1690 ASSERT_EQ(0u, pc_->remote_streams()->count());
1691
1692 // Test that we still can get statistics for the old track. Even if it is not
1693 // sent any longer.
1694 EXPECT_TRUE(DoGetStats(remote_audio));
1695}
1696
1697// Test that we can get stats on a video track.
1698TEST_F(PeerConnectionInterfaceTest, GetStatsForVideoTrack) {
1699 InitiateCall();
1700 ASSERT_LT(0u, pc_->remote_streams()->count());
1701 ASSERT_LT(0u, pc_->remote_streams()->at(0)->GetVideoTracks().size());
zhihuang9763d562016-08-05 11:14:50 -07001702 rtc::scoped_refptr<MediaStreamTrackInterface> remote_video =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001703 pc_->remote_streams()->at(0)->GetVideoTracks()[0];
1704 EXPECT_TRUE(DoGetStats(remote_video));
1705}
1706
1707// Test that we don't get statistics for an invalid track.
zhihuange9e94c32016-11-04 11:38:15 -07001708TEST_F(PeerConnectionInterfaceTest, GetStatsForInvalidTrack) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001709 InitiateCall();
zhihuang9763d562016-08-05 11:14:50 -07001710 rtc::scoped_refptr<AudioTrackInterface> unknown_audio_track(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001711 pc_factory_->CreateAudioTrack("unknown track", NULL));
1712 EXPECT_FALSE(DoGetStats(unknown_audio_track));
1713}
1714
1715// This test setup two RTP data channels in loop back.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001716TEST_F(PeerConnectionInterfaceTest, TestDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001717 FakeConstraints constraints;
1718 constraints.SetAllowRtpDataChannels();
1719 CreatePeerConnection(&constraints);
zhihuang9763d562016-08-05 11:14:50 -07001720 rtc::scoped_refptr<DataChannelInterface> data1 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001721 pc_->CreateDataChannel("test1", NULL);
zhihuang9763d562016-08-05 11:14:50 -07001722 rtc::scoped_refptr<DataChannelInterface> data2 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001723 pc_->CreateDataChannel("test2", NULL);
1724 ASSERT_TRUE(data1 != NULL);
kwibergd1fe2812016-04-27 06:47:29 -07001725 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001726 new MockDataChannelObserver(data1));
kwibergd1fe2812016-04-27 06:47:29 -07001727 std::unique_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001728 new MockDataChannelObserver(data2));
1729
1730 EXPECT_EQ(DataChannelInterface::kConnecting, data1->state());
1731 EXPECT_EQ(DataChannelInterface::kConnecting, data2->state());
1732 std::string data_to_send1 = "testing testing";
1733 std::string data_to_send2 = "testing something else";
1734 EXPECT_FALSE(data1->Send(DataBuffer(data_to_send1)));
1735
1736 CreateOfferReceiveAnswer();
1737 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1738 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
1739
1740 EXPECT_EQ(DataChannelInterface::kOpen, data1->state());
1741 EXPECT_EQ(DataChannelInterface::kOpen, data2->state());
1742 EXPECT_TRUE(data1->Send(DataBuffer(data_to_send1)));
1743 EXPECT_TRUE(data2->Send(DataBuffer(data_to_send2)));
1744
1745 EXPECT_EQ_WAIT(data_to_send1, observer1->last_message(), kTimeout);
1746 EXPECT_EQ_WAIT(data_to_send2, observer2->last_message(), kTimeout);
1747
1748 data1->Close();
1749 EXPECT_EQ(DataChannelInterface::kClosing, data1->state());
1750 CreateOfferReceiveAnswer();
1751 EXPECT_FALSE(observer1->IsOpen());
1752 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
1753 EXPECT_TRUE(observer2->IsOpen());
1754
1755 data_to_send2 = "testing something else again";
1756 EXPECT_TRUE(data2->Send(DataBuffer(data_to_send2)));
1757
1758 EXPECT_EQ_WAIT(data_to_send2, observer2->last_message(), kTimeout);
1759}
1760
1761// This test verifies that sendnig binary data over RTP data channels should
1762// fail.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001763TEST_F(PeerConnectionInterfaceTest, TestSendBinaryOnRtpDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001764 FakeConstraints constraints;
1765 constraints.SetAllowRtpDataChannels();
1766 CreatePeerConnection(&constraints);
zhihuang9763d562016-08-05 11:14:50 -07001767 rtc::scoped_refptr<DataChannelInterface> data1 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001768 pc_->CreateDataChannel("test1", NULL);
zhihuang9763d562016-08-05 11:14:50 -07001769 rtc::scoped_refptr<DataChannelInterface> data2 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001770 pc_->CreateDataChannel("test2", NULL);
1771 ASSERT_TRUE(data1 != NULL);
kwibergd1fe2812016-04-27 06:47:29 -07001772 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001773 new MockDataChannelObserver(data1));
kwibergd1fe2812016-04-27 06:47:29 -07001774 std::unique_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001775 new MockDataChannelObserver(data2));
1776
1777 EXPECT_EQ(DataChannelInterface::kConnecting, data1->state());
1778 EXPECT_EQ(DataChannelInterface::kConnecting, data2->state());
1779
1780 CreateOfferReceiveAnswer();
1781 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1782 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
1783
1784 EXPECT_EQ(DataChannelInterface::kOpen, data1->state());
1785 EXPECT_EQ(DataChannelInterface::kOpen, data2->state());
1786
jbaucheec21bd2016-03-20 06:15:43 -07001787 rtc::CopyOnWriteBuffer buffer("test", 4);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001788 EXPECT_FALSE(data1->Send(DataBuffer(buffer, true)));
1789}
1790
1791// This test setup a RTP data channels in loop back and test that a channel is
1792// opened even if the remote end answer with a zero SSRC.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001793TEST_F(PeerConnectionInterfaceTest, TestSendOnlyDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001794 FakeConstraints constraints;
1795 constraints.SetAllowRtpDataChannels();
1796 CreatePeerConnection(&constraints);
zhihuang9763d562016-08-05 11:14:50 -07001797 rtc::scoped_refptr<DataChannelInterface> data1 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001798 pc_->CreateDataChannel("test1", NULL);
kwibergd1fe2812016-04-27 06:47:29 -07001799 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001800 new MockDataChannelObserver(data1));
1801
1802 CreateOfferReceiveAnswerWithoutSsrc();
1803
1804 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1805
1806 data1->Close();
1807 EXPECT_EQ(DataChannelInterface::kClosing, data1->state());
1808 CreateOfferReceiveAnswerWithoutSsrc();
1809 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
1810 EXPECT_FALSE(observer1->IsOpen());
1811}
1812
1813// This test that if a data channel is added in an answer a receive only channel
1814// channel is created.
1815TEST_F(PeerConnectionInterfaceTest, TestReceiveOnlyDataChannel) {
1816 FakeConstraints constraints;
1817 constraints.SetAllowRtpDataChannels();
1818 CreatePeerConnection(&constraints);
1819
1820 std::string offer_label = "offer_channel";
zhihuang9763d562016-08-05 11:14:50 -07001821 rtc::scoped_refptr<DataChannelInterface> offer_channel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001822 pc_->CreateDataChannel(offer_label, NULL);
1823
1824 CreateOfferAsLocalDescription();
1825
1826 // Replace the data channel label in the offer and apply it as an answer.
1827 std::string receive_label = "answer_channel";
1828 std::string sdp;
1829 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001830 rtc::replace_substrs(offer_label.c_str(), offer_label.length(),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001831 receive_label.c_str(), receive_label.length(),
1832 &sdp);
1833 CreateAnswerAsRemoteDescription(sdp);
1834
1835 // Verify that a new incoming data channel has been created and that
1836 // it is open but can't we written to.
1837 ASSERT_TRUE(observer_.last_datachannel_ != NULL);
1838 DataChannelInterface* received_channel = observer_.last_datachannel_;
1839 EXPECT_EQ(DataChannelInterface::kConnecting, received_channel->state());
1840 EXPECT_EQ(receive_label, received_channel->label());
1841 EXPECT_FALSE(received_channel->Send(DataBuffer("something")));
1842
1843 // Verify that the channel we initially offered has been rejected.
1844 EXPECT_EQ(DataChannelInterface::kClosed, offer_channel->state());
1845
1846 // Do another offer / answer exchange and verify that the data channel is
1847 // opened.
1848 CreateOfferReceiveAnswer();
1849 EXPECT_EQ_WAIT(DataChannelInterface::kOpen, received_channel->state(),
1850 kTimeout);
1851}
1852
1853// This test that no data channel is returned if a reliable channel is
1854// requested.
1855// TODO(perkj): Remove this test once reliable channels are implemented.
1856TEST_F(PeerConnectionInterfaceTest, CreateReliableRtpDataChannelShouldFail) {
1857 FakeConstraints constraints;
1858 constraints.SetAllowRtpDataChannels();
1859 CreatePeerConnection(&constraints);
1860
1861 std::string label = "test";
1862 webrtc::DataChannelInit config;
1863 config.reliable = true;
zhihuang9763d562016-08-05 11:14:50 -07001864 rtc::scoped_refptr<DataChannelInterface> channel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001865 pc_->CreateDataChannel(label, &config);
1866 EXPECT_TRUE(channel == NULL);
1867}
1868
deadbeefab9b2d12015-10-14 11:33:11 -07001869// Verifies that duplicated label is not allowed for RTP data channel.
1870TEST_F(PeerConnectionInterfaceTest, RtpDuplicatedLabelNotAllowed) {
1871 FakeConstraints constraints;
1872 constraints.SetAllowRtpDataChannels();
1873 CreatePeerConnection(&constraints);
1874
1875 std::string label = "test";
zhihuang9763d562016-08-05 11:14:50 -07001876 rtc::scoped_refptr<DataChannelInterface> channel =
deadbeefab9b2d12015-10-14 11:33:11 -07001877 pc_->CreateDataChannel(label, nullptr);
1878 EXPECT_NE(channel, nullptr);
1879
zhihuang9763d562016-08-05 11:14:50 -07001880 rtc::scoped_refptr<DataChannelInterface> dup_channel =
deadbeefab9b2d12015-10-14 11:33:11 -07001881 pc_->CreateDataChannel(label, nullptr);
1882 EXPECT_EQ(dup_channel, nullptr);
1883}
1884
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001885// This tests that a SCTP data channel is returned using different
1886// DataChannelInit configurations.
1887TEST_F(PeerConnectionInterfaceTest, CreateSctpDataChannel) {
1888 FakeConstraints constraints;
1889 constraints.SetAllowDtlsSctpDataChannels();
1890 CreatePeerConnection(&constraints);
1891
1892 webrtc::DataChannelInit config;
1893
zhihuang9763d562016-08-05 11:14:50 -07001894 rtc::scoped_refptr<DataChannelInterface> channel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001895 pc_->CreateDataChannel("1", &config);
1896 EXPECT_TRUE(channel != NULL);
1897 EXPECT_TRUE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001898 EXPECT_TRUE(observer_.renegotiation_needed_);
1899 observer_.renegotiation_needed_ = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001900
1901 config.ordered = false;
1902 channel = pc_->CreateDataChannel("2", &config);
1903 EXPECT_TRUE(channel != NULL);
1904 EXPECT_TRUE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001905 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001906
1907 config.ordered = true;
1908 config.maxRetransmits = 0;
1909 channel = pc_->CreateDataChannel("3", &config);
1910 EXPECT_TRUE(channel != NULL);
1911 EXPECT_FALSE(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.maxRetransmits = -1;
1915 config.maxRetransmitTime = 0;
1916 channel = pc_->CreateDataChannel("4", &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
1922// This tests that no data channel is returned if both maxRetransmits and
1923// maxRetransmitTime are set for SCTP data channels.
1924TEST_F(PeerConnectionInterfaceTest,
1925 CreateSctpDataChannelShouldFailForInvalidConfig) {
1926 FakeConstraints constraints;
1927 constraints.SetAllowDtlsSctpDataChannels();
1928 CreatePeerConnection(&constraints);
1929
1930 std::string label = "test";
1931 webrtc::DataChannelInit config;
1932 config.maxRetransmits = 0;
1933 config.maxRetransmitTime = 0;
1934
zhihuang9763d562016-08-05 11:14:50 -07001935 rtc::scoped_refptr<DataChannelInterface> channel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001936 pc_->CreateDataChannel(label, &config);
1937 EXPECT_TRUE(channel == NULL);
1938}
1939
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001940// The test verifies that creating a SCTP data channel with an id already in use
1941// or out of range should fail.
1942TEST_F(PeerConnectionInterfaceTest,
1943 CreateSctpDataChannelWithInvalidIdShouldFail) {
1944 FakeConstraints constraints;
1945 constraints.SetAllowDtlsSctpDataChannels();
1946 CreatePeerConnection(&constraints);
1947
1948 webrtc::DataChannelInit config;
zhihuang9763d562016-08-05 11:14:50 -07001949 rtc::scoped_refptr<DataChannelInterface> channel;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001950
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001951 config.id = 1;
1952 channel = pc_->CreateDataChannel("1", &config);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001953 EXPECT_TRUE(channel != NULL);
1954 EXPECT_EQ(1, channel->id());
1955
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001956 channel = pc_->CreateDataChannel("x", &config);
1957 EXPECT_TRUE(channel == NULL);
1958
1959 config.id = cricket::kMaxSctpSid;
1960 channel = pc_->CreateDataChannel("max", &config);
1961 EXPECT_TRUE(channel != NULL);
1962 EXPECT_EQ(config.id, channel->id());
1963
1964 config.id = cricket::kMaxSctpSid + 1;
1965 channel = pc_->CreateDataChannel("x", &config);
1966 EXPECT_TRUE(channel == NULL);
1967}
1968
deadbeefab9b2d12015-10-14 11:33:11 -07001969// Verifies that duplicated label is allowed for SCTP data channel.
1970TEST_F(PeerConnectionInterfaceTest, SctpDuplicatedLabelAllowed) {
1971 FakeConstraints constraints;
1972 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
1973 true);
1974 CreatePeerConnection(&constraints);
1975
1976 std::string label = "test";
zhihuang9763d562016-08-05 11:14:50 -07001977 rtc::scoped_refptr<DataChannelInterface> channel =
deadbeefab9b2d12015-10-14 11:33:11 -07001978 pc_->CreateDataChannel(label, nullptr);
1979 EXPECT_NE(channel, nullptr);
1980
zhihuang9763d562016-08-05 11:14:50 -07001981 rtc::scoped_refptr<DataChannelInterface> dup_channel =
deadbeefab9b2d12015-10-14 11:33:11 -07001982 pc_->CreateDataChannel(label, nullptr);
1983 EXPECT_NE(dup_channel, nullptr);
1984}
1985
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001986// This test verifies that OnRenegotiationNeeded is fired for every new RTP
1987// DataChannel.
1988TEST_F(PeerConnectionInterfaceTest, RenegotiationNeededForNewRtpDataChannel) {
1989 FakeConstraints constraints;
1990 constraints.SetAllowRtpDataChannels();
1991 CreatePeerConnection(&constraints);
1992
zhihuang9763d562016-08-05 11:14:50 -07001993 rtc::scoped_refptr<DataChannelInterface> dc1 =
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001994 pc_->CreateDataChannel("test1", NULL);
1995 EXPECT_TRUE(observer_.renegotiation_needed_);
1996 observer_.renegotiation_needed_ = false;
1997
zhihuang9763d562016-08-05 11:14:50 -07001998 rtc::scoped_refptr<DataChannelInterface> dc2 =
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001999 pc_->CreateDataChannel("test2", NULL);
2000 EXPECT_TRUE(observer_.renegotiation_needed_);
2001}
2002
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002003// This test that a data channel closes when a PeerConnection is deleted/closed.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002004TEST_F(PeerConnectionInterfaceTest, DataChannelCloseWhenPeerConnectionClose) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002005 FakeConstraints constraints;
2006 constraints.SetAllowRtpDataChannels();
2007 CreatePeerConnection(&constraints);
2008
zhihuang9763d562016-08-05 11:14:50 -07002009 rtc::scoped_refptr<DataChannelInterface> data1 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002010 pc_->CreateDataChannel("test1", NULL);
zhihuang9763d562016-08-05 11:14:50 -07002011 rtc::scoped_refptr<DataChannelInterface> data2 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002012 pc_->CreateDataChannel("test2", NULL);
2013 ASSERT_TRUE(data1 != NULL);
kwibergd1fe2812016-04-27 06:47:29 -07002014 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002015 new MockDataChannelObserver(data1));
kwibergd1fe2812016-04-27 06:47:29 -07002016 std::unique_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002017 new MockDataChannelObserver(data2));
2018
2019 CreateOfferReceiveAnswer();
2020 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
2021 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
2022
2023 ReleasePeerConnection();
2024 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
2025 EXPECT_EQ(DataChannelInterface::kClosed, data2->state());
2026}
2027
2028// This test that data channels can be rejected in an answer.
2029TEST_F(PeerConnectionInterfaceTest, TestRejectDataChannelInAnswer) {
2030 FakeConstraints constraints;
2031 constraints.SetAllowRtpDataChannels();
2032 CreatePeerConnection(&constraints);
2033
zhihuang9763d562016-08-05 11:14:50 -07002034 rtc::scoped_refptr<DataChannelInterface> offer_channel(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002035 pc_->CreateDataChannel("offer_channel", NULL));
2036
2037 CreateOfferAsLocalDescription();
2038
2039 // Create an answer where the m-line for data channels are rejected.
2040 std::string sdp;
2041 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
2042 webrtc::JsepSessionDescription* answer = new webrtc::JsepSessionDescription(
2043 SessionDescriptionInterface::kAnswer);
2044 EXPECT_TRUE(answer->Initialize(sdp, NULL));
2045 cricket::ContentInfo* data_info =
2046 answer->description()->GetContentByName("data");
2047 data_info->rejected = true;
2048
2049 DoSetRemoteDescription(answer);
2050 EXPECT_EQ(DataChannelInterface::kClosed, offer_channel->state());
2051}
2052
2053// Test that we can create a session description from an SDP string from
2054// FireFox, use it as a remote session description, generate an answer and use
2055// the answer as a local description.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002056TEST_F(PeerConnectionInterfaceTest, ReceiveFireFoxOffer) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002057 FakeConstraints constraints;
2058 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2059 true);
2060 CreatePeerConnection(&constraints);
2061 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
2062 SessionDescriptionInterface* desc =
2063 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
jbauchfabe2c92015-07-16 13:43:14 -07002064 webrtc::kFireFoxSdpOffer, nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002065 EXPECT_TRUE(DoSetSessionDescription(desc, false));
2066 CreateAnswerAsLocalDescription();
2067 ASSERT_TRUE(pc_->local_description() != NULL);
2068 ASSERT_TRUE(pc_->remote_description() != NULL);
2069
2070 const cricket::ContentInfo* content =
2071 cricket::GetFirstAudioContent(pc_->local_description()->description());
2072 ASSERT_TRUE(content != NULL);
2073 EXPECT_FALSE(content->rejected);
2074
2075 content =
2076 cricket::GetFirstVideoContent(pc_->local_description()->description());
2077 ASSERT_TRUE(content != NULL);
2078 EXPECT_FALSE(content->rejected);
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00002079#ifdef HAVE_SCTP
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002080 content =
2081 cricket::GetFirstDataContent(pc_->local_description()->description());
2082 ASSERT_TRUE(content != NULL);
2083 EXPECT_TRUE(content->rejected);
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00002084#endif
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002085}
2086
deadbeef8662f942017-01-20 21:20:51 -08002087// Test that an offer can be received which offers DTLS with SDES fallback.
2088// Regression test for issue:
2089// https://bugs.chromium.org/p/webrtc/issues/detail?id=6972
2090TEST_F(PeerConnectionInterfaceTest, ReceiveDtlsSdesFallbackOffer) {
2091 FakeConstraints constraints;
2092 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2093 true);
2094 CreatePeerConnection(&constraints);
2095 // Wait for fake certificate to be generated. Previously, this is what caused
2096 // the "a=crypto" lines to be rejected.
2097 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
2098 ASSERT_NE(nullptr, fake_certificate_generator_);
2099 EXPECT_EQ_WAIT(1, fake_certificate_generator_->generated_certificates(),
2100 kTimeout);
2101 SessionDescriptionInterface* desc = webrtc::CreateSessionDescription(
2102 SessionDescriptionInterface::kOffer, kDtlsSdesFallbackSdp, nullptr);
2103 EXPECT_TRUE(DoSetSessionDescription(desc, false));
2104 CreateAnswerAsLocalDescription();
2105}
2106
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002107// Test that we can create an audio only offer and receive an answer with a
2108// limited set of audio codecs and receive an updated offer with more audio
2109// codecs, where the added codecs are not supported.
2110TEST_F(PeerConnectionInterfaceTest, ReceiveUpdatedAudioOfferWithBadCodecs) {
deadbeef293e9262017-01-11 12:28:30 -08002111 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002112 AddVoiceStream("audio_label");
2113 CreateOfferAsLocalDescription();
2114
2115 SessionDescriptionInterface* answer =
2116 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
jbauchfabe2c92015-07-16 13:43:14 -07002117 webrtc::kAudioSdp, nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002118 EXPECT_TRUE(DoSetSessionDescription(answer, false));
2119
2120 SessionDescriptionInterface* updated_offer =
2121 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
jbauchfabe2c92015-07-16 13:43:14 -07002122 webrtc::kAudioSdpWithUnsupportedCodecs,
2123 nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002124 EXPECT_TRUE(DoSetSessionDescription(updated_offer, false));
2125 CreateAnswerAsLocalDescription();
2126}
2127
deadbeefc80741f2015-10-22 13:14:45 -07002128// Test that if we're receiving (but not sending) a track, subsequent offers
2129// will have m-lines with a=recvonly.
2130TEST_F(PeerConnectionInterfaceTest, CreateSubsequentRecvOnlyOffer) {
2131 FakeConstraints constraints;
2132 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2133 true);
2134 CreatePeerConnection(&constraints);
2135 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2136 CreateAnswerAsLocalDescription();
2137
2138 // At this point we should be receiving stream 1, but not sending anything.
2139 // A new offer should be recvonly.
kwibergd1fe2812016-04-27 06:47:29 -07002140 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefc80741f2015-10-22 13:14:45 -07002141 DoCreateOffer(&offer, nullptr);
2142
2143 const cricket::ContentInfo* video_content =
2144 cricket::GetFirstVideoContent(offer->description());
2145 const cricket::VideoContentDescription* video_desc =
2146 static_cast<const cricket::VideoContentDescription*>(
2147 video_content->description);
2148 ASSERT_EQ(cricket::MD_RECVONLY, video_desc->direction());
2149
2150 const cricket::ContentInfo* audio_content =
2151 cricket::GetFirstAudioContent(offer->description());
2152 const cricket::AudioContentDescription* audio_desc =
2153 static_cast<const cricket::AudioContentDescription*>(
2154 audio_content->description);
2155 ASSERT_EQ(cricket::MD_RECVONLY, audio_desc->direction());
2156}
2157
2158// Test that if we're receiving (but not sending) a track, and the
2159// offerToReceiveVideo/offerToReceiveAudio constraints are explicitly set to
2160// false, the generated m-lines will be a=inactive.
2161TEST_F(PeerConnectionInterfaceTest, CreateSubsequentInactiveOffer) {
2162 FakeConstraints constraints;
2163 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2164 true);
2165 CreatePeerConnection(&constraints);
2166 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2167 CreateAnswerAsLocalDescription();
2168
2169 // At this point we should be receiving stream 1, but not sending anything.
2170 // A new offer would be recvonly, but we'll set the "no receive" constraints
2171 // to make it inactive.
kwibergd1fe2812016-04-27 06:47:29 -07002172 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefc80741f2015-10-22 13:14:45 -07002173 FakeConstraints offer_constraints;
2174 offer_constraints.AddMandatory(
2175 webrtc::MediaConstraintsInterface::kOfferToReceiveVideo, false);
2176 offer_constraints.AddMandatory(
2177 webrtc::MediaConstraintsInterface::kOfferToReceiveAudio, false);
2178 DoCreateOffer(&offer, &offer_constraints);
2179
2180 const cricket::ContentInfo* video_content =
2181 cricket::GetFirstVideoContent(offer->description());
2182 const cricket::VideoContentDescription* video_desc =
2183 static_cast<const cricket::VideoContentDescription*>(
2184 video_content->description);
2185 ASSERT_EQ(cricket::MD_INACTIVE, video_desc->direction());
2186
2187 const cricket::ContentInfo* audio_content =
2188 cricket::GetFirstAudioContent(offer->description());
2189 const cricket::AudioContentDescription* audio_desc =
2190 static_cast<const cricket::AudioContentDescription*>(
2191 audio_content->description);
2192 ASSERT_EQ(cricket::MD_INACTIVE, audio_desc->direction());
2193}
2194
deadbeef653b8e02015-11-11 12:55:10 -08002195// Test that we can use SetConfiguration to change the ICE servers of the
2196// PortAllocator.
2197TEST_F(PeerConnectionInterfaceTest, SetConfigurationChangesIceServers) {
2198 CreatePeerConnection();
2199
2200 PeerConnectionInterface::RTCConfiguration config;
2201 PeerConnectionInterface::IceServer server;
2202 server.uri = "stun:test_hostname";
2203 config.servers.push_back(server);
2204 EXPECT_TRUE(pc_->SetConfiguration(config));
2205
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002206 EXPECT_EQ(1u, port_allocator_->stun_servers().size());
2207 EXPECT_EQ("test_hostname",
2208 port_allocator_->stun_servers().begin()->hostname());
deadbeef653b8e02015-11-11 12:55:10 -08002209}
2210
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002211TEST_F(PeerConnectionInterfaceTest, SetConfigurationChangesCandidateFilter) {
2212 CreatePeerConnection();
2213 PeerConnectionInterface::RTCConfiguration config;
2214 config.type = PeerConnectionInterface::kRelay;
2215 EXPECT_TRUE(pc_->SetConfiguration(config));
2216 EXPECT_EQ(cricket::CF_RELAY, port_allocator_->candidate_filter());
2217}
2218
deadbeef293e9262017-01-11 12:28:30 -08002219TEST_F(PeerConnectionInterfaceTest, SetConfigurationChangesPruneTurnPortsFlag) {
2220 PeerConnectionInterface::RTCConfiguration config;
2221 config.prune_turn_ports = false;
2222 CreatePeerConnection(config, nullptr);
2223 EXPECT_FALSE(port_allocator_->prune_turn_ports());
2224
2225 config.prune_turn_ports = true;
2226 EXPECT_TRUE(pc_->SetConfiguration(config));
2227 EXPECT_TRUE(port_allocator_->prune_turn_ports());
2228}
2229
skvladd1f5fda2017-02-03 16:54:05 -08002230// Test that the ice check interval can be changed. This does not verify that
2231// the setting makes it all the way to P2PTransportChannel, as that would
2232// require a very complex set of mocks.
2233TEST_F(PeerConnectionInterfaceTest, SetConfigurationChangesIceCheckInterval) {
2234 PeerConnectionInterface::RTCConfiguration config;
2235 config.ice_check_min_interval = rtc::Optional<int>();
2236 CreatePeerConnection(config, nullptr);
2237 config.ice_check_min_interval = rtc::Optional<int>(100);
2238 EXPECT_TRUE(pc_->SetConfiguration(config));
2239 PeerConnectionInterface::RTCConfiguration new_config =
2240 pc_->GetConfiguration();
2241 EXPECT_EQ(new_config.ice_check_min_interval, rtc::Optional<int>(100));
2242}
2243
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002244// Test that when SetConfiguration changes both the pool size and other
2245// attributes, the pooled session is created with the updated attributes.
2246TEST_F(PeerConnectionInterfaceTest,
2247 SetConfigurationCreatesPooledSessionCorrectly) {
2248 CreatePeerConnection();
2249 PeerConnectionInterface::RTCConfiguration config;
2250 config.ice_candidate_pool_size = 1;
2251 PeerConnectionInterface::IceServer server;
2252 server.uri = kStunAddressOnly;
2253 config.servers.push_back(server);
2254 config.type = PeerConnectionInterface::kRelay;
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07002255 EXPECT_TRUE(pc_->SetConfiguration(config));
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002256
2257 const cricket::FakePortAllocatorSession* session =
2258 static_cast<const cricket::FakePortAllocatorSession*>(
2259 port_allocator_->GetPooledSession());
2260 ASSERT_NE(nullptr, session);
2261 EXPECT_EQ(1UL, session->stun_servers().size());
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002262}
2263
deadbeef293e9262017-01-11 12:28:30 -08002264// Test that after SetLocalDescription, changing the pool size is not allowed,
2265// and an invalid modification error is returned.
deadbeef6de92f92016-12-12 18:49:32 -08002266TEST_F(PeerConnectionInterfaceTest,
2267 CantChangePoolSizeAfterSetLocalDescription) {
2268 CreatePeerConnection();
2269 // Start by setting a size of 1.
2270 PeerConnectionInterface::RTCConfiguration config;
2271 config.ice_candidate_pool_size = 1;
2272 EXPECT_TRUE(pc_->SetConfiguration(config));
2273
2274 // Set remote offer; can still change pool size at this point.
2275 CreateOfferAsRemoteDescription();
2276 config.ice_candidate_pool_size = 2;
2277 EXPECT_TRUE(pc_->SetConfiguration(config));
2278
2279 // Set local answer; now it's too late.
2280 CreateAnswerAsLocalDescription();
2281 config.ice_candidate_pool_size = 3;
deadbeef293e9262017-01-11 12:28:30 -08002282 RTCError error;
2283 EXPECT_FALSE(pc_->SetConfiguration(config, &error));
2284 EXPECT_EQ(RTCErrorType::INVALID_MODIFICATION, error.type());
2285}
2286
2287// Test that SetConfiguration returns an invalid modification error if
2288// modifying a field in the configuration that isn't allowed to be modified.
2289TEST_F(PeerConnectionInterfaceTest,
2290 SetConfigurationReturnsInvalidModificationError) {
2291 PeerConnectionInterface::RTCConfiguration config;
2292 config.bundle_policy = PeerConnectionInterface::kBundlePolicyBalanced;
2293 config.rtcp_mux_policy = PeerConnectionInterface::kRtcpMuxPolicyNegotiate;
2294 config.continual_gathering_policy = PeerConnectionInterface::GATHER_ONCE;
2295 CreatePeerConnection(config, nullptr);
2296
2297 PeerConnectionInterface::RTCConfiguration modified_config = config;
2298 modified_config.bundle_policy =
2299 PeerConnectionInterface::kBundlePolicyMaxBundle;
2300 RTCError error;
2301 EXPECT_FALSE(pc_->SetConfiguration(modified_config, &error));
2302 EXPECT_EQ(RTCErrorType::INVALID_MODIFICATION, error.type());
2303
2304 modified_config = config;
2305 modified_config.rtcp_mux_policy =
2306 PeerConnectionInterface::kRtcpMuxPolicyRequire;
2307 error.set_type(RTCErrorType::NONE);
2308 EXPECT_FALSE(pc_->SetConfiguration(modified_config, &error));
2309 EXPECT_EQ(RTCErrorType::INVALID_MODIFICATION, error.type());
2310
2311 modified_config = config;
2312 modified_config.continual_gathering_policy =
2313 PeerConnectionInterface::GATHER_CONTINUALLY;
2314 error.set_type(RTCErrorType::NONE);
2315 EXPECT_FALSE(pc_->SetConfiguration(modified_config, &error));
2316 EXPECT_EQ(RTCErrorType::INVALID_MODIFICATION, error.type());
2317}
2318
2319// Test that SetConfiguration returns a range error if the candidate pool size
2320// is negative or larger than allowed by the spec.
2321TEST_F(PeerConnectionInterfaceTest,
2322 SetConfigurationReturnsRangeErrorForBadCandidatePoolSize) {
2323 PeerConnectionInterface::RTCConfiguration config;
2324 CreatePeerConnection(config, nullptr);
2325
2326 config.ice_candidate_pool_size = -1;
2327 RTCError error;
2328 EXPECT_FALSE(pc_->SetConfiguration(config, &error));
2329 EXPECT_EQ(RTCErrorType::INVALID_RANGE, error.type());
2330
2331 config.ice_candidate_pool_size = INT_MAX;
2332 error.set_type(RTCErrorType::NONE);
2333 EXPECT_FALSE(pc_->SetConfiguration(config, &error));
2334 EXPECT_EQ(RTCErrorType::INVALID_RANGE, error.type());
2335}
2336
2337// Test that SetConfiguration returns a syntax error if parsing an ICE server
2338// URL failed.
2339TEST_F(PeerConnectionInterfaceTest,
2340 SetConfigurationReturnsSyntaxErrorFromBadIceUrls) {
2341 PeerConnectionInterface::RTCConfiguration config;
2342 CreatePeerConnection(config, nullptr);
2343
2344 PeerConnectionInterface::IceServer bad_server;
2345 bad_server.uri = "stunn:www.example.com";
2346 config.servers.push_back(bad_server);
2347 RTCError error;
2348 EXPECT_FALSE(pc_->SetConfiguration(config, &error));
2349 EXPECT_EQ(RTCErrorType::SYNTAX_ERROR, error.type());
2350}
2351
2352// Test that SetConfiguration returns an invalid parameter error if a TURN
2353// IceServer is missing a username or password.
2354TEST_F(PeerConnectionInterfaceTest,
2355 SetConfigurationReturnsInvalidParameterIfCredentialsMissing) {
2356 PeerConnectionInterface::RTCConfiguration config;
2357 CreatePeerConnection(config, nullptr);
2358
2359 PeerConnectionInterface::IceServer bad_server;
2360 bad_server.uri = "turn:www.example.com";
2361 // Missing password.
2362 bad_server.username = "foo";
2363 config.servers.push_back(bad_server);
2364 RTCError error;
2365 EXPECT_FALSE(pc_->SetConfiguration(config, &error));
2366 EXPECT_EQ(RTCErrorType::INVALID_PARAMETER, error.type());
deadbeef6de92f92016-12-12 18:49:32 -08002367}
2368
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002369// Test that PeerConnection::Close changes the states to closed and all remote
2370// tracks change state to ended.
2371TEST_F(PeerConnectionInterfaceTest, CloseAndTestStreamsAndStates) {
2372 // Initialize a PeerConnection and negotiate local and remote session
2373 // description.
2374 InitiateCall();
2375 ASSERT_EQ(1u, pc_->local_streams()->count());
2376 ASSERT_EQ(1u, pc_->remote_streams()->count());
2377
2378 pc_->Close();
2379
2380 EXPECT_EQ(PeerConnectionInterface::kClosed, pc_->signaling_state());
2381 EXPECT_EQ(PeerConnectionInterface::kIceConnectionClosed,
2382 pc_->ice_connection_state());
2383 EXPECT_EQ(PeerConnectionInterface::kIceGatheringComplete,
2384 pc_->ice_gathering_state());
2385
2386 EXPECT_EQ(1u, pc_->local_streams()->count());
2387 EXPECT_EQ(1u, pc_->remote_streams()->count());
2388
zhihuang9763d562016-08-05 11:14:50 -07002389 rtc::scoped_refptr<MediaStreamInterface> remote_stream =
2390 pc_->remote_streams()->at(0);
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002391 // Track state may be updated asynchronously.
perkjd61bf802016-03-24 03:16:19 -07002392 EXPECT_EQ_WAIT(MediaStreamTrackInterface::kEnded,
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002393 remote_stream->GetAudioTracks()[0]->state(), kTimeout);
2394 EXPECT_EQ_WAIT(MediaStreamTrackInterface::kEnded,
2395 remote_stream->GetVideoTracks()[0]->state(), kTimeout);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002396}
2397
2398// Test that PeerConnection methods fails gracefully after
2399// PeerConnection::Close has been called.
2400TEST_F(PeerConnectionInterfaceTest, CloseAndTestMethods) {
deadbeef293e9262017-01-11 12:28:30 -08002401 CreatePeerConnectionWithoutDtls();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002402 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
2403 CreateOfferAsRemoteDescription();
2404 CreateAnswerAsLocalDescription();
2405
2406 ASSERT_EQ(1u, pc_->local_streams()->count());
zhihuang9763d562016-08-05 11:14:50 -07002407 rtc::scoped_refptr<MediaStreamInterface> local_stream =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002408 pc_->local_streams()->at(0);
2409
2410 pc_->Close();
2411
2412 pc_->RemoveStream(local_stream);
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +00002413 EXPECT_FALSE(pc_->AddStream(local_stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002414
2415 ASSERT_FALSE(local_stream->GetAudioTracks().empty());
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002416 rtc::scoped_refptr<webrtc::DtmfSenderInterface> dtmf_sender(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002417 pc_->CreateDtmfSender(local_stream->GetAudioTracks()[0]));
wu@webrtc.org66037362013-08-13 00:09:35 +00002418 EXPECT_TRUE(NULL == dtmf_sender); // local stream has been removed.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002419
2420 EXPECT_TRUE(pc_->CreateDataChannel("test", NULL) == NULL);
2421
2422 EXPECT_TRUE(pc_->local_description() != NULL);
2423 EXPECT_TRUE(pc_->remote_description() != NULL);
2424
kwibergd1fe2812016-04-27 06:47:29 -07002425 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07002426 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
kwibergd1fe2812016-04-27 06:47:29 -07002427 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -07002428 EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002429
2430 std::string sdp;
2431 ASSERT_TRUE(pc_->remote_description()->ToString(&sdp));
2432 SessionDescriptionInterface* remote_offer =
2433 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
2434 sdp, NULL);
2435 EXPECT_FALSE(DoSetRemoteDescription(remote_offer));
2436
2437 ASSERT_TRUE(pc_->local_description()->ToString(&sdp));
2438 SessionDescriptionInterface* local_offer =
2439 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
2440 sdp, NULL);
2441 EXPECT_FALSE(DoSetLocalDescription(local_offer));
2442}
2443
2444// Test that GetStats can still be called after PeerConnection::Close.
2445TEST_F(PeerConnectionInterfaceTest, CloseAndGetStats) {
2446 InitiateCall();
2447 pc_->Close();
2448 DoGetStats(NULL);
2449}
deadbeefab9b2d12015-10-14 11:33:11 -07002450
2451// NOTE: The series of tests below come from what used to be
2452// mediastreamsignaling_unittest.cc, and are mostly aimed at testing that
2453// setting a remote or local description has the expected effects.
2454
2455// This test verifies that the remote MediaStreams corresponding to a received
2456// SDP string is created. In this test the two separate MediaStreams are
2457// signaled.
2458TEST_F(PeerConnectionInterfaceTest, UpdateRemoteStreams) {
2459 FakeConstraints constraints;
2460 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2461 true);
2462 CreatePeerConnection(&constraints);
2463 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2464
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002465 rtc::scoped_refptr<StreamCollection> reference(CreateStreamCollection(1, 1));
deadbeefab9b2d12015-10-14 11:33:11 -07002466 EXPECT_TRUE(
2467 CompareStreamCollections(observer_.remote_streams(), reference.get()));
2468 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2469 EXPECT_TRUE(remote_stream->GetVideoTracks()[0]->GetSource() != nullptr);
2470
2471 // Create a session description based on another SDP with another
2472 // MediaStream.
2473 CreateAndSetRemoteOffer(kSdpStringWithStream1And2);
2474
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002475 rtc::scoped_refptr<StreamCollection> reference2(CreateStreamCollection(2, 1));
deadbeefab9b2d12015-10-14 11:33:11 -07002476 EXPECT_TRUE(
2477 CompareStreamCollections(observer_.remote_streams(), reference2.get()));
2478}
2479
2480// This test verifies that when remote tracks are added/removed from SDP, the
2481// created remote streams are updated appropriately.
2482TEST_F(PeerConnectionInterfaceTest,
2483 AddRemoveTrackFromExistingRemoteMediaStream) {
2484 FakeConstraints constraints;
2485 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2486 true);
2487 CreatePeerConnection(&constraints);
kwibergd1fe2812016-04-27 06:47:29 -07002488 std::unique_ptr<SessionDescriptionInterface> desc_ms1 =
kwiberg2bbff992016-03-16 11:03:04 -07002489 CreateSessionDescriptionAndReference(1, 1);
deadbeefab9b2d12015-10-14 11:33:11 -07002490 EXPECT_TRUE(DoSetRemoteDescription(desc_ms1.release()));
2491 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
2492 reference_collection_));
2493
2494 // Add extra audio and video tracks to the same MediaStream.
kwibergd1fe2812016-04-27 06:47:29 -07002495 std::unique_ptr<SessionDescriptionInterface> desc_ms1_two_tracks =
kwiberg2bbff992016-03-16 11:03:04 -07002496 CreateSessionDescriptionAndReference(2, 2);
deadbeefab9b2d12015-10-14 11:33:11 -07002497 EXPECT_TRUE(DoSetRemoteDescription(desc_ms1_two_tracks.release()));
2498 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
2499 reference_collection_));
zhihuang9763d562016-08-05 11:14:50 -07002500 rtc::scoped_refptr<AudioTrackInterface> audio_track2 =
perkjd61bf802016-03-24 03:16:19 -07002501 observer_.remote_streams()->at(0)->GetAudioTracks()[1];
2502 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, audio_track2->state());
zhihuang9763d562016-08-05 11:14:50 -07002503 rtc::scoped_refptr<VideoTrackInterface> video_track2 =
perkjd61bf802016-03-24 03:16:19 -07002504 observer_.remote_streams()->at(0)->GetVideoTracks()[1];
2505 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, video_track2->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002506
2507 // Remove the extra audio and video tracks.
kwibergd1fe2812016-04-27 06:47:29 -07002508 std::unique_ptr<SessionDescriptionInterface> desc_ms2 =
kwiberg2bbff992016-03-16 11:03:04 -07002509 CreateSessionDescriptionAndReference(1, 1);
perkjd61bf802016-03-24 03:16:19 -07002510 MockTrackObserver audio_track_observer(audio_track2);
2511 MockTrackObserver video_track_observer(video_track2);
2512
2513 EXPECT_CALL(audio_track_observer, OnChanged()).Times(Exactly(1));
2514 EXPECT_CALL(video_track_observer, OnChanged()).Times(Exactly(1));
deadbeefab9b2d12015-10-14 11:33:11 -07002515 EXPECT_TRUE(DoSetRemoteDescription(desc_ms2.release()));
2516 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
2517 reference_collection_));
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002518 // Track state may be updated asynchronously.
perkjd61bf802016-03-24 03:16:19 -07002519 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002520 audio_track2->state(), kTimeout);
2521 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
2522 video_track2->state(), kTimeout);
deadbeefab9b2d12015-10-14 11:33:11 -07002523}
2524
2525// This tests that remote tracks are ended if a local session description is set
2526// that rejects the media content type.
2527TEST_F(PeerConnectionInterfaceTest, RejectMediaContent) {
2528 FakeConstraints constraints;
2529 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2530 true);
2531 CreatePeerConnection(&constraints);
2532 // First create and set a remote offer, then reject its video content in our
2533 // answer.
2534 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2535 ASSERT_EQ(1u, observer_.remote_streams()->count());
2536 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2537 ASSERT_EQ(1u, remote_stream->GetVideoTracks().size());
2538 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2539
2540 rtc::scoped_refptr<webrtc::VideoTrackInterface> remote_video =
2541 remote_stream->GetVideoTracks()[0];
2542 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, remote_video->state());
2543 rtc::scoped_refptr<webrtc::AudioTrackInterface> remote_audio =
2544 remote_stream->GetAudioTracks()[0];
2545 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, remote_audio->state());
2546
kwibergd1fe2812016-04-27 06:47:29 -07002547 std::unique_ptr<SessionDescriptionInterface> local_answer;
kwiberg2bbff992016-03-16 11:03:04 -07002548 EXPECT_TRUE(DoCreateAnswer(&local_answer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07002549 cricket::ContentInfo* video_info =
2550 local_answer->description()->GetContentByName("video");
2551 video_info->rejected = true;
2552 EXPECT_TRUE(DoSetLocalDescription(local_answer.release()));
2553 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kEnded, remote_video->state());
2554 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, remote_audio->state());
2555
2556 // Now create an offer where we reject both video and audio.
kwibergd1fe2812016-04-27 06:47:29 -07002557 std::unique_ptr<SessionDescriptionInterface> local_offer;
kwiberg2bbff992016-03-16 11:03:04 -07002558 EXPECT_TRUE(DoCreateOffer(&local_offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07002559 video_info = local_offer->description()->GetContentByName("video");
2560 ASSERT_TRUE(video_info != nullptr);
2561 video_info->rejected = true;
2562 cricket::ContentInfo* audio_info =
2563 local_offer->description()->GetContentByName("audio");
2564 ASSERT_TRUE(audio_info != nullptr);
2565 audio_info->rejected = true;
2566 EXPECT_TRUE(DoSetLocalDescription(local_offer.release()));
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002567 // Track state may be updated asynchronously.
perkjd61bf802016-03-24 03:16:19 -07002568 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002569 remote_audio->state(), kTimeout);
2570 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
2571 remote_video->state(), kTimeout);
deadbeefab9b2d12015-10-14 11:33:11 -07002572}
2573
2574// This tests that we won't crash if the remote track has been removed outside
2575// of PeerConnection and then PeerConnection tries to reject the track.
2576TEST_F(PeerConnectionInterfaceTest, RemoveTrackThenRejectMediaContent) {
2577 FakeConstraints constraints;
2578 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2579 true);
2580 CreatePeerConnection(&constraints);
2581 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2582 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2583 remote_stream->RemoveTrack(remote_stream->GetVideoTracks()[0]);
2584 remote_stream->RemoveTrack(remote_stream->GetAudioTracks()[0]);
2585
kwibergd1fe2812016-04-27 06:47:29 -07002586 std::unique_ptr<SessionDescriptionInterface> local_answer(
deadbeefab9b2d12015-10-14 11:33:11 -07002587 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
2588 kSdpStringWithStream1, nullptr));
2589 cricket::ContentInfo* video_info =
2590 local_answer->description()->GetContentByName("video");
2591 video_info->rejected = true;
2592 cricket::ContentInfo* audio_info =
2593 local_answer->description()->GetContentByName("audio");
2594 audio_info->rejected = true;
2595 EXPECT_TRUE(DoSetLocalDescription(local_answer.release()));
2596
2597 // No crash is a pass.
2598}
2599
deadbeef5e97fb52015-10-15 12:49:08 -07002600// This tests that if a recvonly remote description is set, no remote streams
2601// will be created, even if the description contains SSRCs/MSIDs.
2602// See: https://code.google.com/p/webrtc/issues/detail?id=5054
2603TEST_F(PeerConnectionInterfaceTest, RecvonlyDescriptionDoesntCreateStream) {
2604 FakeConstraints constraints;
2605 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2606 true);
2607 CreatePeerConnection(&constraints);
2608
2609 std::string recvonly_offer = kSdpStringWithStream1;
2610 rtc::replace_substrs(kSendrecv, strlen(kSendrecv), kRecvonly,
2611 strlen(kRecvonly), &recvonly_offer);
2612 CreateAndSetRemoteOffer(recvonly_offer);
2613
2614 EXPECT_EQ(0u, observer_.remote_streams()->count());
2615}
2616
deadbeefab9b2d12015-10-14 11:33:11 -07002617// This tests that a default MediaStream is created if a remote session
2618// description doesn't contain any streams and no MSID support.
2619// It also tests that the default stream is updated if a video m-line is added
2620// in a subsequent session description.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002621TEST_F(PeerConnectionInterfaceTest, SdpWithoutMsidCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002622 FakeConstraints constraints;
2623 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2624 true);
2625 CreatePeerConnection(&constraints);
2626 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2627
2628 ASSERT_EQ(1u, observer_.remote_streams()->count());
2629 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2630
2631 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2632 EXPECT_EQ(0u, remote_stream->GetVideoTracks().size());
2633 EXPECT_EQ("default", remote_stream->label());
2634
2635 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2636 ASSERT_EQ(1u, observer_.remote_streams()->count());
2637 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2638 EXPECT_EQ("defaulta0", remote_stream->GetAudioTracks()[0]->id());
deadbeef884f5852016-01-15 09:20:04 -08002639 EXPECT_EQ(MediaStreamTrackInterface::kLive,
2640 remote_stream->GetAudioTracks()[0]->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002641 ASSERT_EQ(1u, remote_stream->GetVideoTracks().size());
2642 EXPECT_EQ("defaultv0", remote_stream->GetVideoTracks()[0]->id());
deadbeef884f5852016-01-15 09:20:04 -08002643 EXPECT_EQ(MediaStreamTrackInterface::kLive,
2644 remote_stream->GetVideoTracks()[0]->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002645}
2646
2647// This tests that a default MediaStream is created if a remote session
2648// description doesn't contain any streams and media direction is send only.
2649TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002650 SendOnlySdpWithoutMsidCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002651 FakeConstraints constraints;
2652 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2653 true);
2654 CreatePeerConnection(&constraints);
2655 CreateAndSetRemoteOffer(kSdpStringSendOnlyWithoutStreams);
2656
2657 ASSERT_EQ(1u, observer_.remote_streams()->count());
2658 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2659
2660 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2661 EXPECT_EQ(1u, remote_stream->GetVideoTracks().size());
2662 EXPECT_EQ("default", remote_stream->label());
2663}
2664
2665// This tests that it won't crash when PeerConnection tries to remove
2666// a remote track that as already been removed from the MediaStream.
2667TEST_F(PeerConnectionInterfaceTest, RemoveAlreadyGoneRemoteStream) {
2668 FakeConstraints constraints;
2669 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2670 true);
2671 CreatePeerConnection(&constraints);
2672 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2673 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2674 remote_stream->RemoveTrack(remote_stream->GetAudioTracks()[0]);
2675 remote_stream->RemoveTrack(remote_stream->GetVideoTracks()[0]);
2676
2677 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2678
2679 // No crash is a pass.
2680}
2681
2682// This tests that a default MediaStream is created if the remote session
2683// description doesn't contain any streams and don't contain an indication if
2684// MSID is supported.
2685TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002686 SdpWithoutMsidAndStreamsCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002687 FakeConstraints constraints;
2688 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2689 true);
2690 CreatePeerConnection(&constraints);
2691 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2692
2693 ASSERT_EQ(1u, observer_.remote_streams()->count());
2694 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2695 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2696 EXPECT_EQ(1u, remote_stream->GetVideoTracks().size());
2697}
2698
2699// This tests that a default MediaStream is not created if the remote session
2700// description doesn't contain any streams but does support MSID.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002701TEST_F(PeerConnectionInterfaceTest, SdpWithMsidDontCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002702 FakeConstraints constraints;
2703 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2704 true);
2705 CreatePeerConnection(&constraints);
2706 CreateAndSetRemoteOffer(kSdpStringWithMsidWithoutStreams);
2707 EXPECT_EQ(0u, observer_.remote_streams()->count());
2708}
2709
deadbeefbda7e0b2015-12-08 17:13:40 -08002710// This tests that when setting a new description, the old default tracks are
2711// not destroyed and recreated.
2712// See: https://bugs.chromium.org/p/webrtc/issues/detail?id=5250
Stefan Holmer102362b2016-03-18 09:39:07 +01002713TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002714 DefaultTracksNotDestroyedAndRecreated) {
deadbeefbda7e0b2015-12-08 17:13:40 -08002715 FakeConstraints constraints;
2716 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2717 true);
2718 CreatePeerConnection(&constraints);
2719 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2720
2721 ASSERT_EQ(1u, observer_.remote_streams()->count());
2722 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2723 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2724
2725 // Set the track to "disabled", then set a new description and ensure the
2726 // track is still disabled, which ensures it hasn't been recreated.
2727 remote_stream->GetAudioTracks()[0]->set_enabled(false);
2728 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2729 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2730 EXPECT_FALSE(remote_stream->GetAudioTracks()[0]->enabled());
2731}
2732
deadbeefab9b2d12015-10-14 11:33:11 -07002733// This tests that a default MediaStream is not created if a remote session
2734// description is updated to not have any MediaStreams.
2735TEST_F(PeerConnectionInterfaceTest, VerifyDefaultStreamIsNotCreated) {
2736 FakeConstraints constraints;
2737 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2738 true);
2739 CreatePeerConnection(&constraints);
2740 CreateAndSetRemoteOffer(kSdpStringWithStream1);
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002741 rtc::scoped_refptr<StreamCollection> reference(CreateStreamCollection(1, 1));
deadbeefab9b2d12015-10-14 11:33:11 -07002742 EXPECT_TRUE(
2743 CompareStreamCollections(observer_.remote_streams(), reference.get()));
2744
2745 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2746 EXPECT_EQ(0u, observer_.remote_streams()->count());
2747}
2748
2749// This tests that an RtpSender is created when the local description is set
2750// after adding a local stream.
2751// TODO(deadbeef): This test and the one below it need to be updated when
2752// an RtpSender's lifetime isn't determined by when a local description is set.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002753TEST_F(PeerConnectionInterfaceTest, LocalDescriptionChanged) {
deadbeefab9b2d12015-10-14 11:33:11 -07002754 FakeConstraints constraints;
2755 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2756 true);
2757 CreatePeerConnection(&constraints);
deadbeefab9b2d12015-10-14 11:33:11 -07002758
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002759 // Create an offer with 1 stream with 2 tracks of each type.
2760 rtc::scoped_refptr<StreamCollection> stream_collection =
2761 CreateStreamCollection(1, 2);
2762 pc_->AddStream(stream_collection->at(0));
2763 std::unique_ptr<SessionDescriptionInterface> offer;
2764 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2765 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
deadbeefab9b2d12015-10-14 11:33:11 -07002766
deadbeefab9b2d12015-10-14 11:33:11 -07002767 auto senders = pc_->GetSenders();
2768 EXPECT_EQ(4u, senders.size());
2769 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2770 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2771 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[1]));
2772 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[1]));
2773
2774 // Remove an audio and video track.
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002775 pc_->RemoveStream(stream_collection->at(0));
2776 stream_collection = CreateStreamCollection(1, 1);
2777 pc_->AddStream(stream_collection->at(0));
2778 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2779 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
2780
deadbeefab9b2d12015-10-14 11:33:11 -07002781 senders = pc_->GetSenders();
2782 EXPECT_EQ(2u, senders.size());
2783 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2784 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2785 EXPECT_FALSE(ContainsSender(senders, kAudioTracks[1]));
2786 EXPECT_FALSE(ContainsSender(senders, kVideoTracks[1]));
2787}
2788
2789// This tests that an RtpSender is created when the local description is set
2790// before adding a local stream.
2791TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002792 AddLocalStreamAfterLocalDescriptionChanged) {
deadbeefab9b2d12015-10-14 11:33:11 -07002793 FakeConstraints constraints;
2794 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2795 true);
2796 CreatePeerConnection(&constraints);
deadbeefab9b2d12015-10-14 11:33:11 -07002797
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002798 rtc::scoped_refptr<StreamCollection> stream_collection =
2799 CreateStreamCollection(1, 2);
2800 // Add a stream to create the offer, but remove it afterwards.
2801 pc_->AddStream(stream_collection->at(0));
2802 std::unique_ptr<SessionDescriptionInterface> offer;
2803 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2804 pc_->RemoveStream(stream_collection->at(0));
deadbeefab9b2d12015-10-14 11:33:11 -07002805
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002806 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
deadbeefab9b2d12015-10-14 11:33:11 -07002807 auto senders = pc_->GetSenders();
2808 EXPECT_EQ(0u, senders.size());
2809
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002810 pc_->AddStream(stream_collection->at(0));
deadbeefab9b2d12015-10-14 11:33:11 -07002811 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
2819// This tests that the expected behavior occurs if the SSRC on a local track is
2820// changed when SetLocalDescription is called.
2821TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002822 ChangeSsrcOnTrackInLocalSessionDescription) {
deadbeefab9b2d12015-10-14 11:33:11 -07002823 FakeConstraints constraints;
2824 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2825 true);
2826 CreatePeerConnection(&constraints);
deadbeefab9b2d12015-10-14 11:33:11 -07002827
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002828 rtc::scoped_refptr<StreamCollection> stream_collection =
2829 CreateStreamCollection(2, 1);
2830 pc_->AddStream(stream_collection->at(0));
2831 std::unique_ptr<SessionDescriptionInterface> offer;
2832 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2833 // Grab a copy of the offer before it gets passed into the PC.
2834 std::unique_ptr<JsepSessionDescription> modified_offer(
2835 new JsepSessionDescription(JsepSessionDescription::kOffer));
2836 modified_offer->Initialize(offer->description()->Copy(), offer->session_id(),
2837 offer->session_version());
2838 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
deadbeefab9b2d12015-10-14 11:33:11 -07002839
deadbeefab9b2d12015-10-14 11:33:11 -07002840 auto senders = pc_->GetSenders();
2841 EXPECT_EQ(2u, senders.size());
2842 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2843 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2844
2845 // Change the ssrc of the audio and video track.
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002846 cricket::MediaContentDescription* desc =
2847 cricket::GetFirstAudioContentDescription(modified_offer->description());
2848 ASSERT_TRUE(desc != NULL);
2849 for (StreamParams& stream : desc->mutable_streams()) {
2850 for (unsigned int& ssrc : stream.ssrcs) {
2851 ++ssrc;
2852 }
2853 }
deadbeefab9b2d12015-10-14 11:33:11 -07002854
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002855 desc =
2856 cricket::GetFirstVideoContentDescription(modified_offer->description());
2857 ASSERT_TRUE(desc != NULL);
2858 for (StreamParams& stream : desc->mutable_streams()) {
2859 for (unsigned int& ssrc : stream.ssrcs) {
2860 ++ssrc;
2861 }
2862 }
2863
2864 EXPECT_TRUE(DoSetLocalDescription(modified_offer.release()));
deadbeefab9b2d12015-10-14 11:33:11 -07002865 senders = pc_->GetSenders();
2866 EXPECT_EQ(2u, senders.size());
2867 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2868 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2869 // TODO(deadbeef): Once RtpSenders expose parameters, check that the SSRC
2870 // changed.
2871}
2872
2873// This tests that the expected behavior occurs if a new session description is
2874// set with the same tracks, but on a different MediaStream.
Stefan Holmer55d6e7c2016-03-17 16:26:40 +01002875TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002876 SignalSameTracksInSeparateMediaStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002877 FakeConstraints constraints;
2878 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2879 true);
2880 CreatePeerConnection(&constraints);
deadbeefab9b2d12015-10-14 11:33:11 -07002881
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002882 rtc::scoped_refptr<StreamCollection> stream_collection =
2883 CreateStreamCollection(2, 1);
2884 pc_->AddStream(stream_collection->at(0));
2885 std::unique_ptr<SessionDescriptionInterface> offer;
2886 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2887 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
deadbeefab9b2d12015-10-14 11:33:11 -07002888
deadbeefab9b2d12015-10-14 11:33:11 -07002889 auto senders = pc_->GetSenders();
2890 EXPECT_EQ(2u, senders.size());
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002891 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0], kStreams[0]));
2892 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0], kStreams[0]));
deadbeefab9b2d12015-10-14 11:33:11 -07002893
2894 // Add a new MediaStream but with the same tracks as in the first stream.
2895 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream_1(
2896 webrtc::MediaStream::Create(kStreams[1]));
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002897 stream_1->AddTrack(stream_collection->at(0)->GetVideoTracks()[0]);
2898 stream_1->AddTrack(stream_collection->at(0)->GetAudioTracks()[0]);
deadbeefab9b2d12015-10-14 11:33:11 -07002899 pc_->AddStream(stream_1);
2900
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002901 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2902 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
deadbeefab9b2d12015-10-14 11:33:11 -07002903
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002904 auto new_senders = pc_->GetSenders();
2905 // Should be the same senders as before, but with updated stream id.
2906 // Note that this behavior is subject to change in the future.
2907 // We may decide the PC should ignore existing tracks in AddStream.
2908 EXPECT_EQ(senders, new_senders);
2909 EXPECT_TRUE(ContainsSender(new_senders, kAudioTracks[0], kStreams[1]));
2910 EXPECT_TRUE(ContainsSender(new_senders, kVideoTracks[0], kStreams[1]));
deadbeefab9b2d12015-10-14 11:33:11 -07002911}
2912
zhihuang81c3a032016-11-17 12:06:24 -08002913// This tests that PeerConnectionObserver::OnAddTrack is correctly called.
2914TEST_F(PeerConnectionInterfaceTest, OnAddTrackCallback) {
2915 FakeConstraints constraints;
2916 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2917 true);
2918 CreatePeerConnection(&constraints);
2919 CreateAndSetRemoteOffer(kSdpStringWithStream1AudioTrackOnly);
2920 EXPECT_EQ(observer_.num_added_tracks_, 1);
2921 EXPECT_EQ(observer_.last_added_track_label_, kAudioTracks[0]);
2922
2923 // Create and set the updated remote SDP.
2924 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2925 EXPECT_EQ(observer_.num_added_tracks_, 2);
2926 EXPECT_EQ(observer_.last_added_track_label_, kVideoTracks[0]);
2927}
2928
deadbeefd1a38b52016-12-10 13:15:33 -08002929// Test that when SetConfiguration is called and the configuration is
2930// changing, the next offer causes an ICE restart.
2931TEST_F(PeerConnectionInterfaceTest, SetConfigurationCausingIceRetart) {
2932 PeerConnectionInterface::RTCConfiguration config;
2933 config.type = PeerConnectionInterface::kRelay;
2934 // Need to pass default constraints to prevent disabling of DTLS...
2935 FakeConstraints default_constraints;
2936 CreatePeerConnection(config, &default_constraints);
2937 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
2938
2939 // Do initial offer/answer so there's something to restart.
2940 CreateOfferAsLocalDescription();
2941 CreateAnswerAsRemoteDescription(kSdpStringWithStream1);
2942
2943 // Grab the ufrags.
2944 std::vector<std::string> initial_ufrags = GetUfrags(pc_->local_description());
2945
2946 // Change ICE policy, which should trigger an ICE restart on the next offer.
2947 config.type = PeerConnectionInterface::kAll;
2948 EXPECT_TRUE(pc_->SetConfiguration(config));
2949 CreateOfferAsLocalDescription();
2950
2951 // Grab the new ufrags.
2952 std::vector<std::string> subsequent_ufrags =
2953 GetUfrags(pc_->local_description());
2954
2955 // Sanity check.
2956 EXPECT_EQ(initial_ufrags.size(), subsequent_ufrags.size());
2957 // Check that each ufrag is different.
2958 for (int i = 0; i < static_cast<int>(initial_ufrags.size()); ++i) {
2959 EXPECT_NE(initial_ufrags[i], subsequent_ufrags[i]);
2960 }
2961}
2962
2963// Test that when SetConfiguration is called and the configuration *isn't*
2964// changing, the next offer does *not* cause an ICE restart.
2965TEST_F(PeerConnectionInterfaceTest, SetConfigurationNotCausingIceRetart) {
2966 PeerConnectionInterface::RTCConfiguration config;
2967 config.type = PeerConnectionInterface::kRelay;
2968 // Need to pass default constraints to prevent disabling of DTLS...
2969 FakeConstraints default_constraints;
2970 CreatePeerConnection(config, &default_constraints);
2971 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
2972
2973 // Do initial offer/answer so there's something to restart.
2974 CreateOfferAsLocalDescription();
2975 CreateAnswerAsRemoteDescription(kSdpStringWithStream1);
2976
2977 // Grab the ufrags.
2978 std::vector<std::string> initial_ufrags = GetUfrags(pc_->local_description());
2979
2980 // Call SetConfiguration with a config identical to what the PC was
2981 // constructed with.
2982 EXPECT_TRUE(pc_->SetConfiguration(config));
2983 CreateOfferAsLocalDescription();
2984
2985 // Grab the new ufrags.
2986 std::vector<std::string> subsequent_ufrags =
2987 GetUfrags(pc_->local_description());
2988
2989 EXPECT_EQ(initial_ufrags, subsequent_ufrags);
2990}
2991
2992// Test for a weird corner case scenario:
2993// 1. Audio/video session established.
2994// 2. SetConfiguration changes ICE config; ICE restart needed.
2995// 3. ICE restart initiated by remote peer, but only for one m= section.
2996// 4. Next createOffer should initiate an ICE restart, but only for the other
2997// m= section; it would be pointless to do an ICE restart for the m= section
2998// that was already restarted.
2999TEST_F(PeerConnectionInterfaceTest, SetConfigurationCausingPartialIceRestart) {
3000 PeerConnectionInterface::RTCConfiguration config;
3001 config.type = PeerConnectionInterface::kRelay;
3002 // Need to pass default constraints to prevent disabling of DTLS...
3003 FakeConstraints default_constraints;
3004 CreatePeerConnection(config, &default_constraints);
3005 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
3006
3007 // Do initial offer/answer so there's something to restart.
3008 CreateOfferAsLocalDescription();
3009 CreateAnswerAsRemoteDescription(kSdpStringWithStream1);
3010
3011 // Change ICE policy, which should set the "needs-ice-restart" flag.
3012 config.type = PeerConnectionInterface::kAll;
3013 EXPECT_TRUE(pc_->SetConfiguration(config));
3014
3015 // Do ICE restart for the first m= section, initiated by remote peer.
3016 webrtc::JsepSessionDescription* remote_offer =
3017 new webrtc::JsepSessionDescription(SessionDescriptionInterface::kOffer);
3018 EXPECT_TRUE(remote_offer->Initialize(kSdpStringWithStream1, nullptr));
3019 remote_offer->description()->transport_infos()[0].description.ice_ufrag =
3020 "modified";
3021 EXPECT_TRUE(DoSetRemoteDescription(remote_offer));
3022 CreateAnswerAsLocalDescription();
3023
3024 // Grab the ufrags.
3025 std::vector<std::string> initial_ufrags = GetUfrags(pc_->local_description());
3026 ASSERT_EQ(2, initial_ufrags.size());
3027
3028 // Create offer and grab the new ufrags.
3029 CreateOfferAsLocalDescription();
3030 std::vector<std::string> subsequent_ufrags =
3031 GetUfrags(pc_->local_description());
3032 ASSERT_EQ(2, subsequent_ufrags.size());
3033
3034 // Ensure that only the ufrag for the second m= section changed.
3035 EXPECT_EQ(initial_ufrags[0], subsequent_ufrags[0]);
3036 EXPECT_NE(initial_ufrags[1], subsequent_ufrags[1]);
3037}
3038
deadbeeffe4a8a42016-12-20 17:56:17 -08003039// Tests that the methods to return current/pending descriptions work as
3040// expected at different points in the offer/answer exchange. This test does
3041// one offer/answer exchange as the offerer, then another as the answerer.
3042TEST_F(PeerConnectionInterfaceTest, CurrentAndPendingDescriptions) {
3043 // This disables DTLS so we can apply an answer to ourselves.
3044 CreatePeerConnection();
3045
3046 // Create initial local offer and get SDP (which will also be used as
3047 // answer/pranswer);
3048 std::unique_ptr<SessionDescriptionInterface> offer;
3049 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
3050 std::string sdp;
3051 EXPECT_TRUE(offer->ToString(&sdp));
3052
3053 // Set local offer.
3054 SessionDescriptionInterface* local_offer = offer.release();
3055 EXPECT_TRUE(DoSetLocalDescription(local_offer));
3056 EXPECT_EQ(local_offer, pc_->pending_local_description());
3057 EXPECT_EQ(nullptr, pc_->pending_remote_description());
3058 EXPECT_EQ(nullptr, pc_->current_local_description());
3059 EXPECT_EQ(nullptr, pc_->current_remote_description());
3060
3061 // Set remote pranswer.
3062 SessionDescriptionInterface* remote_pranswer =
3063 webrtc::CreateSessionDescription(SessionDescriptionInterface::kPrAnswer,
3064 sdp, nullptr);
3065 EXPECT_TRUE(DoSetRemoteDescription(remote_pranswer));
3066 EXPECT_EQ(local_offer, pc_->pending_local_description());
3067 EXPECT_EQ(remote_pranswer, pc_->pending_remote_description());
3068 EXPECT_EQ(nullptr, pc_->current_local_description());
3069 EXPECT_EQ(nullptr, pc_->current_remote_description());
3070
3071 // Set remote answer.
3072 SessionDescriptionInterface* remote_answer = webrtc::CreateSessionDescription(
3073 SessionDescriptionInterface::kAnswer, sdp, nullptr);
3074 EXPECT_TRUE(DoSetRemoteDescription(remote_answer));
3075 EXPECT_EQ(nullptr, pc_->pending_local_description());
3076 EXPECT_EQ(nullptr, pc_->pending_remote_description());
3077 EXPECT_EQ(local_offer, pc_->current_local_description());
3078 EXPECT_EQ(remote_answer, pc_->current_remote_description());
3079
3080 // Set remote offer.
3081 SessionDescriptionInterface* remote_offer = webrtc::CreateSessionDescription(
3082 SessionDescriptionInterface::kOffer, sdp, nullptr);
3083 EXPECT_TRUE(DoSetRemoteDescription(remote_offer));
3084 EXPECT_EQ(remote_offer, pc_->pending_remote_description());
3085 EXPECT_EQ(nullptr, pc_->pending_local_description());
3086 EXPECT_EQ(local_offer, pc_->current_local_description());
3087 EXPECT_EQ(remote_answer, pc_->current_remote_description());
3088
3089 // Set local pranswer.
3090 SessionDescriptionInterface* local_pranswer =
3091 webrtc::CreateSessionDescription(SessionDescriptionInterface::kPrAnswer,
3092 sdp, nullptr);
3093 EXPECT_TRUE(DoSetLocalDescription(local_pranswer));
3094 EXPECT_EQ(remote_offer, pc_->pending_remote_description());
3095 EXPECT_EQ(local_pranswer, pc_->pending_local_description());
3096 EXPECT_EQ(local_offer, pc_->current_local_description());
3097 EXPECT_EQ(remote_answer, pc_->current_remote_description());
3098
3099 // Set local answer.
3100 SessionDescriptionInterface* local_answer = webrtc::CreateSessionDescription(
3101 SessionDescriptionInterface::kAnswer, sdp, nullptr);
3102 EXPECT_TRUE(DoSetLocalDescription(local_answer));
3103 EXPECT_EQ(nullptr, pc_->pending_remote_description());
3104 EXPECT_EQ(nullptr, pc_->pending_local_description());
3105 EXPECT_EQ(remote_offer, pc_->current_remote_description());
3106 EXPECT_EQ(local_answer, pc_->current_local_description());
3107}
3108
zhihuang77985012017-02-07 15:45:16 -08003109// Tests that it won't crash when calling StartRtcEventLog or StopRtcEventLog
3110// after the PeerConnection is closed.
3111TEST_F(PeerConnectionInterfaceTest,
3112 StartAndStopLoggingAfterPeerConnectionClosed) {
3113 CreatePeerConnection();
3114 // The RtcEventLog will be reset when the PeerConnection is closed.
3115 pc_->Close();
3116
3117 rtc::PlatformFile file = 0;
3118 int64_t max_size_bytes = 1024;
3119 EXPECT_FALSE(pc_->StartRtcEventLog(file, max_size_bytes));
3120 pc_->StopRtcEventLog();
3121}
3122
nisse51542be2016-02-12 02:27:06 -08003123class PeerConnectionMediaConfigTest : public testing::Test {
3124 protected:
3125 void SetUp() override {
nisseaf510af2016-03-21 08:20:42 -07003126 pcf_ = new rtc::RefCountedObject<PeerConnectionFactoryForTest>();
nisse51542be2016-02-12 02:27:06 -08003127 pcf_->Initialize();
3128 }
3129 const cricket::MediaConfig& TestCreatePeerConnection(
3130 const PeerConnectionInterface::RTCConfiguration& config,
3131 const MediaConstraintsInterface *constraints) {
3132 pcf_->create_media_controller_called_ = false;
3133
zhihuang9763d562016-08-05 11:14:50 -07003134 rtc::scoped_refptr<PeerConnectionInterface> pc(pcf_->CreatePeerConnection(
3135 config, constraints, nullptr, nullptr, &observer_));
nisse51542be2016-02-12 02:27:06 -08003136 EXPECT_TRUE(pc.get());
3137 EXPECT_TRUE(pcf_->create_media_controller_called_);
3138 return pcf_->create_media_controller_config_;
3139 }
3140
zhihuang9763d562016-08-05 11:14:50 -07003141 rtc::scoped_refptr<PeerConnectionFactoryForTest> pcf_;
nisse51542be2016-02-12 02:27:06 -08003142 MockPeerConnectionObserver observer_;
3143};
3144
3145// This test verifies the default behaviour with no constraints and a
3146// default RTCConfiguration.
3147TEST_F(PeerConnectionMediaConfigTest, TestDefaults) {
3148 PeerConnectionInterface::RTCConfiguration config;
3149 FakeConstraints constraints;
3150
3151 const cricket::MediaConfig& media_config =
3152 TestCreatePeerConnection(config, &constraints);
3153
3154 EXPECT_FALSE(media_config.enable_dscp);
nisse0db023a2016-03-01 04:29:59 -08003155 EXPECT_TRUE(media_config.video.enable_cpu_overuse_detection);
3156 EXPECT_FALSE(media_config.video.disable_prerenderer_smoothing);
3157 EXPECT_FALSE(media_config.video.suspend_below_min_bitrate);
nisse51542be2016-02-12 02:27:06 -08003158}
3159
3160// This test verifies the DSCP constraint is recognized and passed to
3161// the CreateMediaController call.
3162TEST_F(PeerConnectionMediaConfigTest, TestDscpConstraintTrue) {
3163 PeerConnectionInterface::RTCConfiguration config;
3164 FakeConstraints constraints;
3165
3166 constraints.AddOptional(webrtc::MediaConstraintsInterface::kEnableDscp, true);
3167 const cricket::MediaConfig& media_config =
3168 TestCreatePeerConnection(config, &constraints);
3169
3170 EXPECT_TRUE(media_config.enable_dscp);
3171}
3172
3173// This test verifies the cpu overuse detection constraint is
3174// recognized and passed to the CreateMediaController call.
3175TEST_F(PeerConnectionMediaConfigTest, TestCpuOveruseConstraintFalse) {
3176 PeerConnectionInterface::RTCConfiguration config;
3177 FakeConstraints constraints;
3178
3179 constraints.AddOptional(
3180 webrtc::MediaConstraintsInterface::kCpuOveruseDetection, false);
3181 const cricket::MediaConfig media_config =
3182 TestCreatePeerConnection(config, &constraints);
3183
nisse0db023a2016-03-01 04:29:59 -08003184 EXPECT_FALSE(media_config.video.enable_cpu_overuse_detection);
nisse51542be2016-02-12 02:27:06 -08003185}
3186
3187// This test verifies that the disable_prerenderer_smoothing flag is
3188// propagated from RTCConfiguration to the CreateMediaController call.
3189TEST_F(PeerConnectionMediaConfigTest, TestDisablePrerendererSmoothingTrue) {
3190 PeerConnectionInterface::RTCConfiguration config;
3191 FakeConstraints constraints;
3192
Niels Möller71bdda02016-03-31 12:59:59 +02003193 config.set_prerenderer_smoothing(false);
nisse51542be2016-02-12 02:27:06 -08003194 const cricket::MediaConfig& media_config =
3195 TestCreatePeerConnection(config, &constraints);
3196
nisse0db023a2016-03-01 04:29:59 -08003197 EXPECT_TRUE(media_config.video.disable_prerenderer_smoothing);
3198}
3199
3200// This test verifies the suspend below min bitrate constraint is
3201// recognized and passed to the CreateMediaController call.
3202TEST_F(PeerConnectionMediaConfigTest,
3203 TestSuspendBelowMinBitrateConstraintTrue) {
3204 PeerConnectionInterface::RTCConfiguration config;
3205 FakeConstraints constraints;
3206
3207 constraints.AddOptional(
3208 webrtc::MediaConstraintsInterface::kEnableVideoSuspendBelowMinBitrate,
3209 true);
3210 const cricket::MediaConfig media_config =
3211 TestCreatePeerConnection(config, &constraints);
3212
3213 EXPECT_TRUE(media_config.video.suspend_below_min_bitrate);
nisse51542be2016-02-12 02:27:06 -08003214}
3215
deadbeefab9b2d12015-10-14 11:33:11 -07003216// The following tests verify that session options are created correctly.
deadbeefc80741f2015-10-22 13:14:45 -07003217// TODO(deadbeef): Convert these tests to be more end-to-end. Instead of
3218// "verify options are converted correctly", should be "pass options into
3219// CreateOffer and verify the correct offer is produced."
deadbeefab9b2d12015-10-14 11:33:11 -07003220
3221TEST(CreateSessionOptionsTest, GetOptionsForOfferWithInvalidAudioOption) {
3222 RTCOfferAnswerOptions rtc_options;
3223 rtc_options.offer_to_receive_audio = RTCOfferAnswerOptions::kUndefined - 1;
3224
3225 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08003226 EXPECT_FALSE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07003227
3228 rtc_options.offer_to_receive_audio =
3229 RTCOfferAnswerOptions::kMaxOfferToReceiveMedia + 1;
htaaac2dea2016-03-10 13:35:55 -08003230 EXPECT_FALSE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07003231}
3232
3233TEST(CreateSessionOptionsTest, GetOptionsForOfferWithInvalidVideoOption) {
3234 RTCOfferAnswerOptions rtc_options;
3235 rtc_options.offer_to_receive_video = RTCOfferAnswerOptions::kUndefined - 1;
3236
3237 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08003238 EXPECT_FALSE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07003239
3240 rtc_options.offer_to_receive_video =
3241 RTCOfferAnswerOptions::kMaxOfferToReceiveMedia + 1;
htaaac2dea2016-03-10 13:35:55 -08003242 EXPECT_FALSE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07003243}
3244
3245// Test that a MediaSessionOptions is created for an offer if
deadbeefc80741f2015-10-22 13:14:45 -07003246// OfferToReceiveAudio and OfferToReceiveVideo options are set.
deadbeefab9b2d12015-10-14 11:33:11 -07003247TEST(CreateSessionOptionsTest, GetMediaSessionOptionsForOfferWithAudioVideo) {
3248 RTCOfferAnswerOptions rtc_options;
3249 rtc_options.offer_to_receive_audio = 1;
3250 rtc_options.offer_to_receive_video = 1;
3251
3252 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08003253 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07003254 EXPECT_TRUE(options.has_audio());
3255 EXPECT_TRUE(options.has_video());
3256 EXPECT_TRUE(options.bundle_enabled);
3257}
3258
3259// Test that a correct MediaSessionOptions is created for an offer if
deadbeefc80741f2015-10-22 13:14:45 -07003260// OfferToReceiveAudio is set.
deadbeefab9b2d12015-10-14 11:33:11 -07003261TEST(CreateSessionOptionsTest, GetMediaSessionOptionsForOfferWithAudio) {
3262 RTCOfferAnswerOptions rtc_options;
3263 rtc_options.offer_to_receive_audio = 1;
3264
3265 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08003266 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07003267 EXPECT_TRUE(options.has_audio());
3268 EXPECT_FALSE(options.has_video());
3269 EXPECT_TRUE(options.bundle_enabled);
3270}
3271
3272// Test that a correct MediaSessionOptions is created for an offer if
deadbeefc80741f2015-10-22 13:14:45 -07003273// the default OfferOptions are used.
deadbeefab9b2d12015-10-14 11:33:11 -07003274TEST(CreateSessionOptionsTest, GetDefaultMediaSessionOptionsForOffer) {
3275 RTCOfferAnswerOptions rtc_options;
3276
3277 cricket::MediaSessionOptions options;
deadbeef0ed85b22016-02-23 17:24:52 -08003278 options.transport_options["audio"] = cricket::TransportOptions();
3279 options.transport_options["video"] = cricket::TransportOptions();
htaaac2dea2016-03-10 13:35:55 -08003280 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefc80741f2015-10-22 13:14:45 -07003281 EXPECT_TRUE(options.has_audio());
deadbeefab9b2d12015-10-14 11:33:11 -07003282 EXPECT_FALSE(options.has_video());
deadbeefc80741f2015-10-22 13:14:45 -07003283 EXPECT_TRUE(options.bundle_enabled);
deadbeefab9b2d12015-10-14 11:33:11 -07003284 EXPECT_TRUE(options.vad_enabled);
deadbeef0ed85b22016-02-23 17:24:52 -08003285 EXPECT_FALSE(options.transport_options["audio"].ice_restart);
3286 EXPECT_FALSE(options.transport_options["video"].ice_restart);
deadbeefab9b2d12015-10-14 11:33:11 -07003287}
3288
3289// Test that a correct MediaSessionOptions is created for an offer if
deadbeefc80741f2015-10-22 13:14:45 -07003290// OfferToReceiveVideo is set.
deadbeefab9b2d12015-10-14 11:33:11 -07003291TEST(CreateSessionOptionsTest, GetMediaSessionOptionsForOfferWithVideo) {
3292 RTCOfferAnswerOptions rtc_options;
3293 rtc_options.offer_to_receive_audio = 0;
3294 rtc_options.offer_to_receive_video = 1;
3295
3296 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08003297 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07003298 EXPECT_FALSE(options.has_audio());
3299 EXPECT_TRUE(options.has_video());
3300 EXPECT_TRUE(options.bundle_enabled);
3301}
3302
3303// Test that a correct MediaSessionOptions is created for an offer if
3304// UseRtpMux is set to false.
3305TEST(CreateSessionOptionsTest,
3306 GetMediaSessionOptionsForOfferWithBundleDisabled) {
3307 RTCOfferAnswerOptions rtc_options;
3308 rtc_options.offer_to_receive_audio = 1;
3309 rtc_options.offer_to_receive_video = 1;
3310 rtc_options.use_rtp_mux = false;
3311
3312 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08003313 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07003314 EXPECT_TRUE(options.has_audio());
3315 EXPECT_TRUE(options.has_video());
3316 EXPECT_FALSE(options.bundle_enabled);
3317}
3318
3319// Test that a correct MediaSessionOptions is created to restart ice if
3320// IceRestart is set. It also tests that subsequent MediaSessionOptions don't
Taylor Brandstetterf475d362016-01-08 15:35:57 -08003321// have |audio_transport_options.ice_restart| etc. set.
deadbeefab9b2d12015-10-14 11:33:11 -07003322TEST(CreateSessionOptionsTest, GetMediaSessionOptionsForOfferWithIceRestart) {
3323 RTCOfferAnswerOptions rtc_options;
3324 rtc_options.ice_restart = true;
3325
3326 cricket::MediaSessionOptions options;
deadbeef0ed85b22016-02-23 17:24:52 -08003327 options.transport_options["audio"] = cricket::TransportOptions();
3328 options.transport_options["video"] = cricket::TransportOptions();
htaaac2dea2016-03-10 13:35:55 -08003329 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeef0ed85b22016-02-23 17:24:52 -08003330 EXPECT_TRUE(options.transport_options["audio"].ice_restart);
3331 EXPECT_TRUE(options.transport_options["video"].ice_restart);
deadbeefab9b2d12015-10-14 11:33:11 -07003332
3333 rtc_options = RTCOfferAnswerOptions();
htaaac2dea2016-03-10 13:35:55 -08003334 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeef0ed85b22016-02-23 17:24:52 -08003335 EXPECT_FALSE(options.transport_options["audio"].ice_restart);
3336 EXPECT_FALSE(options.transport_options["video"].ice_restart);
deadbeefab9b2d12015-10-14 11:33:11 -07003337}
3338
3339// Test that the MediaConstraints in an answer don't affect if audio and video
3340// is offered in an offer but that if kOfferToReceiveAudio or
3341// kOfferToReceiveVideo constraints are true in an offer, the media type will be
3342// included in subsequent answers.
3343TEST(CreateSessionOptionsTest, MediaConstraintsInAnswer) {
3344 FakeConstraints answer_c;
3345 answer_c.SetMandatoryReceiveAudio(true);
3346 answer_c.SetMandatoryReceiveVideo(true);
3347
3348 cricket::MediaSessionOptions answer_options;
3349 EXPECT_TRUE(ParseConstraintsForAnswer(&answer_c, &answer_options));
3350 EXPECT_TRUE(answer_options.has_audio());
3351 EXPECT_TRUE(answer_options.has_video());
3352
deadbeefc80741f2015-10-22 13:14:45 -07003353 RTCOfferAnswerOptions rtc_offer_options;
deadbeefab9b2d12015-10-14 11:33:11 -07003354
3355 cricket::MediaSessionOptions offer_options;
htaaac2dea2016-03-10 13:35:55 -08003356 EXPECT_TRUE(
3357 ExtractMediaSessionOptions(rtc_offer_options, false, &offer_options));
deadbeefc80741f2015-10-22 13:14:45 -07003358 EXPECT_TRUE(offer_options.has_audio());
htaaac2dea2016-03-10 13:35:55 -08003359 EXPECT_TRUE(offer_options.has_video());
deadbeefab9b2d12015-10-14 11:33:11 -07003360
deadbeefc80741f2015-10-22 13:14:45 -07003361 RTCOfferAnswerOptions updated_rtc_offer_options;
3362 updated_rtc_offer_options.offer_to_receive_audio = 1;
3363 updated_rtc_offer_options.offer_to_receive_video = 1;
deadbeefab9b2d12015-10-14 11:33:11 -07003364
3365 cricket::MediaSessionOptions updated_offer_options;
htaaac2dea2016-03-10 13:35:55 -08003366 EXPECT_TRUE(ExtractMediaSessionOptions(updated_rtc_offer_options, false,
htaa2a49d92016-03-04 02:51:39 -08003367 &updated_offer_options));
deadbeefab9b2d12015-10-14 11:33:11 -07003368 EXPECT_TRUE(updated_offer_options.has_audio());
3369 EXPECT_TRUE(updated_offer_options.has_video());
3370
3371 // Since an offer has been created with both audio and video, subsequent
3372 // offers and answers should contain both audio and video.
3373 // Answers will only contain the media types that exist in the offer
3374 // regardless of the value of |updated_answer_options.has_audio| and
3375 // |updated_answer_options.has_video|.
3376 FakeConstraints updated_answer_c;
3377 answer_c.SetMandatoryReceiveAudio(false);
3378 answer_c.SetMandatoryReceiveVideo(false);
3379
3380 cricket::MediaSessionOptions updated_answer_options;
3381 EXPECT_TRUE(
3382 ParseConstraintsForAnswer(&updated_answer_c, &updated_answer_options));
3383 EXPECT_TRUE(updated_answer_options.has_audio());
3384 EXPECT_TRUE(updated_answer_options.has_video());
deadbeefab9b2d12015-10-14 11:33:11 -07003385}
deadbeef3edec7c2016-12-10 11:44:26 -08003386
deadbeef293e9262017-01-11 12:28:30 -08003387TEST(RTCErrorTypeTest, OstreamOperator) {
deadbeef3edec7c2016-12-10 11:44:26 -08003388 std::ostringstream oss;
deadbeef293e9262017-01-11 12:28:30 -08003389 oss << webrtc::RTCErrorType::NONE << ' '
3390 << webrtc::RTCErrorType::INVALID_PARAMETER << ' '
3391 << webrtc::RTCErrorType::INTERNAL_ERROR;
deadbeef3edec7c2016-12-10 11:44:26 -08003392 EXPECT_EQ("NONE INVALID_PARAMETER INTERNAL_ERROR", oss.str());
3393}
deadbeef293e9262017-01-11 12:28:30 -08003394
3395// Tests a few random fields being different.
3396TEST(RTCConfigurationTest, ComparisonOperators) {
3397 PeerConnectionInterface::RTCConfiguration a;
3398 PeerConnectionInterface::RTCConfiguration b;
3399 EXPECT_EQ(a, b);
3400
3401 PeerConnectionInterface::RTCConfiguration c;
3402 c.servers.push_back(PeerConnectionInterface::IceServer());
3403 EXPECT_NE(a, c);
3404
3405 PeerConnectionInterface::RTCConfiguration d;
3406 d.type = PeerConnectionInterface::kRelay;
3407 EXPECT_NE(a, d);
3408
3409 PeerConnectionInterface::RTCConfiguration e;
3410 e.audio_jitter_buffer_max_packets = 5;
3411 EXPECT_NE(a, e);
3412
3413 PeerConnectionInterface::RTCConfiguration f;
3414 f.ice_connection_receiving_timeout = 1337;
3415 EXPECT_NE(a, f);
3416
3417 PeerConnectionInterface::RTCConfiguration g;
3418 g.disable_ipv6 = true;
3419 EXPECT_NE(a, g);
3420
3421 PeerConnectionInterface::RTCConfiguration h(
3422 PeerConnectionInterface::RTCConfigurationType::kAggressive);
3423 EXPECT_NE(a, h);
3424}