blob: 0726c75d4d1e0229da2fd63d312064747a769c7c [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>
henrike@webrtc.org28e20752013-07-10 00:45:36 +000012#include <string>
kwiberg0eb15ed2015-12-17 03:04:15 -080013#include <utility>
henrike@webrtc.org28e20752013-07-10 00:45:36 +000014
Henrik Kjellander15583c12016-02-10 10:53:12 +010015#include "webrtc/api/audiotrack.h"
16#include "webrtc/api/jsepsessiondescription.h"
17#include "webrtc/api/mediastream.h"
18#include "webrtc/api/mediastreaminterface.h"
19#include "webrtc/api/peerconnection.h"
20#include "webrtc/api/peerconnectioninterface.h"
21#include "webrtc/api/rtpreceiverinterface.h"
22#include "webrtc/api/rtpsenderinterface.h"
23#include "webrtc/api/streamcollection.h"
Henrik Kjellander15583c12016-02-10 10:53:12 +010024#include "webrtc/api/test/fakeconstraints.h"
Henrik Boströmd79599d2016-06-01 13:58:50 +020025#include "webrtc/api/test/fakertccertificategenerator.h"
nisseaf510af2016-03-21 08:20:42 -070026#include "webrtc/api/test/fakevideotracksource.h"
Henrik Kjellander15583c12016-02-10 10:53:12 +010027#include "webrtc/api/test/mockpeerconnectionobservers.h"
28#include "webrtc/api/test/testsdpstrings.h"
perkja3ede6c2016-03-08 01:27:48 +010029#include "webrtc/api/videocapturertracksource.h"
Henrik Kjellander15583c12016-02-10 10:53:12 +010030#include "webrtc/api/videotrack.h"
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000031#include "webrtc/base/gunit.h"
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000032#include "webrtc/base/ssladapter.h"
33#include "webrtc/base/sslstreamadapter.h"
34#include "webrtc/base/stringutils.h"
35#include "webrtc/base/thread.h"
kjellandera96e2d72016-02-04 23:52:28 -080036#include "webrtc/media/base/fakevideocapturer.h"
37#include "webrtc/media/sctp/sctpdataengine.h"
Taylor Brandstettera1c30352016-05-13 08:15:11 -070038#include "webrtc/p2p/base/fakeportallocator.h"
zhihuang29ff8442016-07-27 11:07:25 -070039#include "webrtc/p2p/base/faketransportcontroller.h"
kjellander@webrtc.org9b8df252016-02-12 06:47:59 +010040#include "webrtc/pc/mediasession.h"
kwibergac9f8762016-09-30 22:29:43 -070041#include "webrtc/test/gmock.h"
42
43#ifdef WEBRTC_ANDROID
44#include "webrtc/api/test/androidtestinitializer.h"
45#endif
henrike@webrtc.org28e20752013-07-10 00:45:36 +000046
47static const char kStreamLabel1[] = "local_stream_1";
48static const char kStreamLabel2[] = "local_stream_2";
49static const char kStreamLabel3[] = "local_stream_3";
50static const int kDefaultStunPort = 3478;
51static const char kStunAddressOnly[] = "stun:address";
52static const char kStunInvalidPort[] = "stun:address:-1";
53static const char kStunAddressPortAndMore1[] = "stun:address:port:more";
54static const char kStunAddressPortAndMore2[] = "stun:address:port more";
55static const char kTurnIceServerUri[] = "turn:user@turn.example.org";
56static const char kTurnUsername[] = "user";
57static const char kTurnPassword[] = "password";
58static const char kTurnHostname[] = "turn.example.org";
Peter Boström0c4e06b2015-10-07 12:23:21 +020059static const uint32_t kTimeout = 10000U;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000060
deadbeefab9b2d12015-10-14 11:33:11 -070061static const char kStreams[][8] = {"stream1", "stream2"};
62static const char kAudioTracks[][32] = {"audiotrack0", "audiotrack1"};
63static const char kVideoTracks[][32] = {"videotrack0", "videotrack1"};
64
deadbeef5e97fb52015-10-15 12:49:08 -070065static const char kRecvonly[] = "recvonly";
66static const char kSendrecv[] = "sendrecv";
67
deadbeefab9b2d12015-10-14 11:33:11 -070068// Reference SDP with a MediaStream with label "stream1" and audio track with
69// id "audio_1" and a video track with id "video_1;
70static const char kSdpStringWithStream1[] =
71 "v=0\r\n"
72 "o=- 0 0 IN IP4 127.0.0.1\r\n"
73 "s=-\r\n"
74 "t=0 0\r\n"
75 "a=ice-ufrag:e5785931\r\n"
76 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
77 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
78 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
79 "m=audio 1 RTP/AVPF 103\r\n"
80 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -070081 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -080082 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -070083 "a=rtpmap:103 ISAC/16000\r\n"
84 "a=ssrc:1 cname:stream1\r\n"
85 "a=ssrc:1 mslabel:stream1\r\n"
86 "a=ssrc:1 label:audiotrack0\r\n"
87 "m=video 1 RTP/AVPF 120\r\n"
88 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -070089 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -080090 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -070091 "a=rtpmap:120 VP8/90000\r\n"
92 "a=ssrc:2 cname:stream1\r\n"
93 "a=ssrc:2 mslabel:stream1\r\n"
94 "a=ssrc:2 label:videotrack0\r\n";
95
zhihuang81c3a032016-11-17 12:06:24 -080096// Reference SDP with a MediaStream with label "stream1" and audio track with
97// id "audio_1";
98static const char kSdpStringWithStream1AudioTrackOnly[] =
99 "v=0\r\n"
100 "o=- 0 0 IN IP4 127.0.0.1\r\n"
101 "s=-\r\n"
102 "t=0 0\r\n"
103 "a=ice-ufrag:e5785931\r\n"
104 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
105 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
106 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
107 "m=audio 1 RTP/AVPF 103\r\n"
108 "a=mid:audio\r\n"
109 "a=sendrecv\r\n"
110 "a=rtpmap:103 ISAC/16000\r\n"
111 "a=ssrc:1 cname:stream1\r\n"
112 "a=ssrc:1 mslabel:stream1\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800113 "a=ssrc:1 label:audiotrack0\r\n"
114 "a=rtcp-mux\r\n";
zhihuang81c3a032016-11-17 12:06:24 -0800115
deadbeefab9b2d12015-10-14 11:33:11 -0700116// Reference SDP with two MediaStreams with label "stream1" and "stream2. Each
117// MediaStreams have one audio track and one video track.
118// This uses MSID.
119static const char kSdpStringWithStream1And2[] =
120 "v=0\r\n"
121 "o=- 0 0 IN IP4 127.0.0.1\r\n"
122 "s=-\r\n"
123 "t=0 0\r\n"
124 "a=ice-ufrag:e5785931\r\n"
125 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
126 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
127 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
128 "a=msid-semantic: WMS stream1 stream2\r\n"
129 "m=audio 1 RTP/AVPF 103\r\n"
130 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700131 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800132 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700133 "a=rtpmap:103 ISAC/16000\r\n"
134 "a=ssrc:1 cname:stream1\r\n"
135 "a=ssrc:1 msid:stream1 audiotrack0\r\n"
136 "a=ssrc:3 cname:stream2\r\n"
137 "a=ssrc:3 msid:stream2 audiotrack1\r\n"
138 "m=video 1 RTP/AVPF 120\r\n"
139 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700140 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800141 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700142 "a=rtpmap:120 VP8/0\r\n"
143 "a=ssrc:2 cname:stream1\r\n"
144 "a=ssrc:2 msid:stream1 videotrack0\r\n"
145 "a=ssrc:4 cname:stream2\r\n"
146 "a=ssrc:4 msid:stream2 videotrack1\r\n";
147
148// Reference SDP without MediaStreams. Msid is not supported.
149static const char kSdpStringWithoutStreams[] =
150 "v=0\r\n"
151 "o=- 0 0 IN IP4 127.0.0.1\r\n"
152 "s=-\r\n"
153 "t=0 0\r\n"
154 "a=ice-ufrag:e5785931\r\n"
155 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
156 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
157 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
158 "m=audio 1 RTP/AVPF 103\r\n"
159 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700160 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800161 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700162 "a=rtpmap:103 ISAC/16000\r\n"
163 "m=video 1 RTP/AVPF 120\r\n"
164 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700165 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800166 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700167 "a=rtpmap:120 VP8/90000\r\n";
168
169// Reference SDP without MediaStreams. Msid is supported.
170static const char kSdpStringWithMsidWithoutStreams[] =
171 "v=0\r\n"
172 "o=- 0 0 IN IP4 127.0.0.1\r\n"
173 "s=-\r\n"
174 "t=0 0\r\n"
175 "a=ice-ufrag:e5785931\r\n"
176 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
177 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
178 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
179 "a=msid-semantic: WMS\r\n"
180 "m=audio 1 RTP/AVPF 103\r\n"
181 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700182 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800183 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700184 "a=rtpmap:103 ISAC/16000\r\n"
185 "m=video 1 RTP/AVPF 120\r\n"
186 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700187 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800188 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700189 "a=rtpmap:120 VP8/90000\r\n";
190
191// Reference SDP without MediaStreams and audio only.
192static const char kSdpStringWithoutStreamsAudioOnly[] =
193 "v=0\r\n"
194 "o=- 0 0 IN IP4 127.0.0.1\r\n"
195 "s=-\r\n"
196 "t=0 0\r\n"
197 "a=ice-ufrag:e5785931\r\n"
198 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
199 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
200 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
201 "m=audio 1 RTP/AVPF 103\r\n"
202 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700203 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800204 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700205 "a=rtpmap:103 ISAC/16000\r\n";
206
207// Reference SENDONLY SDP without MediaStreams. Msid is not supported.
208static const char kSdpStringSendOnlyWithoutStreams[] =
209 "v=0\r\n"
210 "o=- 0 0 IN IP4 127.0.0.1\r\n"
211 "s=-\r\n"
212 "t=0 0\r\n"
213 "a=ice-ufrag:e5785931\r\n"
214 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
215 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
216 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
217 "m=audio 1 RTP/AVPF 103\r\n"
218 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700219 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700220 "a=sendonly\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 "m=video 1 RTP/AVPF 120\r\n"
224 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700225 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700226 "a=sendonly\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800227 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700228 "a=rtpmap:120 VP8/90000\r\n";
229
230static const char kSdpStringInit[] =
231 "v=0\r\n"
232 "o=- 0 0 IN IP4 127.0.0.1\r\n"
233 "s=-\r\n"
234 "t=0 0\r\n"
235 "a=ice-ufrag:e5785931\r\n"
236 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
237 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
238 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
239 "a=msid-semantic: WMS\r\n";
240
241static const char kSdpStringAudio[] =
242 "m=audio 1 RTP/AVPF 103\r\n"
243 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700244 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800245 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700246 "a=rtpmap:103 ISAC/16000\r\n";
247
248static const char kSdpStringVideo[] =
249 "m=video 1 RTP/AVPF 120\r\n"
250 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700251 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800252 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700253 "a=rtpmap:120 VP8/90000\r\n";
254
255static const char kSdpStringMs1Audio0[] =
256 "a=ssrc:1 cname:stream1\r\n"
257 "a=ssrc:1 msid:stream1 audiotrack0\r\n";
258
259static const char kSdpStringMs1Video0[] =
260 "a=ssrc:2 cname:stream1\r\n"
261 "a=ssrc:2 msid:stream1 videotrack0\r\n";
262
263static const char kSdpStringMs1Audio1[] =
264 "a=ssrc:3 cname:stream1\r\n"
265 "a=ssrc:3 msid:stream1 audiotrack1\r\n";
266
267static const char kSdpStringMs1Video1[] =
268 "a=ssrc:4 cname:stream1\r\n"
269 "a=ssrc:4 msid:stream1 videotrack1\r\n";
270
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000271#define MAYBE_SKIP_TEST(feature) \
272 if (!(feature())) { \
273 LOG(LS_INFO) << "Feature disabled... skipping"; \
274 return; \
275 }
276
perkjd61bf802016-03-24 03:16:19 -0700277using ::testing::Exactly;
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700278using cricket::StreamParams;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000279using webrtc::AudioSourceInterface;
deadbeefab9b2d12015-10-14 11:33:11 -0700280using webrtc::AudioTrack;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000281using webrtc::AudioTrackInterface;
282using webrtc::DataBuffer;
283using webrtc::DataChannelInterface;
284using webrtc::FakeConstraints;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000285using webrtc::IceCandidateInterface;
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700286using webrtc::JsepSessionDescription;
deadbeefc80741f2015-10-22 13:14:45 -0700287using webrtc::MediaConstraintsInterface;
deadbeefab9b2d12015-10-14 11:33:11 -0700288using webrtc::MediaStream;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000289using webrtc::MediaStreamInterface;
290using webrtc::MediaStreamTrackInterface;
291using webrtc::MockCreateSessionDescriptionObserver;
292using webrtc::MockDataChannelObserver;
293using webrtc::MockSetSessionDescriptionObserver;
294using webrtc::MockStatsObserver;
perkjd61bf802016-03-24 03:16:19 -0700295using webrtc::NotifierInterface;
296using webrtc::ObserverInterface;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000297using webrtc::PeerConnectionInterface;
298using webrtc::PeerConnectionObserver;
deadbeefab9b2d12015-10-14 11:33:11 -0700299using webrtc::RtpReceiverInterface;
300using webrtc::RtpSenderInterface;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000301using webrtc::SdpParseError;
302using webrtc::SessionDescriptionInterface;
deadbeefab9b2d12015-10-14 11:33:11 -0700303using webrtc::StreamCollection;
304using webrtc::StreamCollectionInterface;
perkja3ede6c2016-03-08 01:27:48 +0100305using webrtc::VideoTrackSourceInterface;
deadbeefab9b2d12015-10-14 11:33:11 -0700306using webrtc::VideoTrack;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000307using webrtc::VideoTrackInterface;
308
deadbeefab9b2d12015-10-14 11:33:11 -0700309typedef PeerConnectionInterface::RTCOfferAnswerOptions RTCOfferAnswerOptions;
310
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000311namespace {
312
313// Gets the first ssrc of given content type from the ContentInfo.
314bool GetFirstSsrc(const cricket::ContentInfo* content_info, int* ssrc) {
315 if (!content_info || !ssrc) {
316 return false;
317 }
318 const cricket::MediaContentDescription* media_desc =
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000319 static_cast<const cricket::MediaContentDescription*>(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000320 content_info->description);
321 if (!media_desc || media_desc->streams().empty()) {
322 return false;
323 }
324 *ssrc = media_desc->streams().begin()->first_ssrc();
325 return true;
326}
327
328void SetSsrcToZero(std::string* sdp) {
329 const char kSdpSsrcAtribute[] = "a=ssrc:";
330 const char kSdpSsrcAtributeZero[] = "a=ssrc:0";
331 size_t ssrc_pos = 0;
332 while ((ssrc_pos = sdp->find(kSdpSsrcAtribute, ssrc_pos)) !=
333 std::string::npos) {
334 size_t end_ssrc = sdp->find(" ", ssrc_pos);
335 sdp->replace(ssrc_pos, end_ssrc - ssrc_pos, kSdpSsrcAtributeZero);
336 ssrc_pos = end_ssrc;
337 }
338}
339
deadbeefab9b2d12015-10-14 11:33:11 -0700340// Check if |streams| contains the specified track.
341bool ContainsTrack(const std::vector<cricket::StreamParams>& streams,
342 const std::string& stream_label,
343 const std::string& track_id) {
344 for (const cricket::StreamParams& params : streams) {
345 if (params.sync_label == stream_label && params.id == track_id) {
346 return true;
347 }
348 }
349 return false;
350}
351
352// Check if |senders| contains the specified sender, by id.
353bool ContainsSender(
354 const std::vector<rtc::scoped_refptr<RtpSenderInterface>>& senders,
355 const std::string& id) {
356 for (const auto& sender : senders) {
357 if (sender->id() == id) {
358 return true;
359 }
360 }
361 return false;
362}
363
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700364// Check if |senders| contains the specified sender, by id and stream id.
365bool ContainsSender(
366 const std::vector<rtc::scoped_refptr<RtpSenderInterface>>& senders,
367 const std::string& id,
368 const std::string& stream_id) {
369 for (const auto& sender : senders) {
deadbeefa601f5c2016-06-06 14:27:39 -0700370 if (sender->id() == id && sender->stream_ids()[0] == stream_id) {
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700371 return true;
372 }
373 }
374 return false;
375}
376
deadbeefab9b2d12015-10-14 11:33:11 -0700377// Create a collection of streams.
378// CreateStreamCollection(1) creates a collection that
379// correspond to kSdpStringWithStream1.
380// CreateStreamCollection(2) correspond to kSdpStringWithStream1And2.
381rtc::scoped_refptr<StreamCollection> CreateStreamCollection(
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700382 int number_of_streams,
383 int tracks_per_stream) {
deadbeefab9b2d12015-10-14 11:33:11 -0700384 rtc::scoped_refptr<StreamCollection> local_collection(
385 StreamCollection::Create());
386
387 for (int i = 0; i < number_of_streams; ++i) {
388 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream(
389 webrtc::MediaStream::Create(kStreams[i]));
390
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700391 for (int j = 0; j < tracks_per_stream; ++j) {
392 // Add a local audio track.
393 rtc::scoped_refptr<webrtc::AudioTrackInterface> audio_track(
394 webrtc::AudioTrack::Create(kAudioTracks[i * tracks_per_stream + j],
395 nullptr));
396 stream->AddTrack(audio_track);
deadbeefab9b2d12015-10-14 11:33:11 -0700397
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700398 // Add a local video track.
399 rtc::scoped_refptr<webrtc::VideoTrackInterface> video_track(
400 webrtc::VideoTrack::Create(kVideoTracks[i * tracks_per_stream + j],
401 webrtc::FakeVideoTrackSource::Create()));
402 stream->AddTrack(video_track);
403 }
deadbeefab9b2d12015-10-14 11:33:11 -0700404
405 local_collection->AddStream(stream);
406 }
407 return local_collection;
408}
409
410// Check equality of StreamCollections.
411bool CompareStreamCollections(StreamCollectionInterface* s1,
412 StreamCollectionInterface* s2) {
413 if (s1 == nullptr || s2 == nullptr || s1->count() != s2->count()) {
414 return false;
415 }
416
417 for (size_t i = 0; i != s1->count(); ++i) {
418 if (s1->at(i)->label() != s2->at(i)->label()) {
419 return false;
420 }
421 webrtc::AudioTrackVector audio_tracks1 = s1->at(i)->GetAudioTracks();
422 webrtc::AudioTrackVector audio_tracks2 = s2->at(i)->GetAudioTracks();
423 webrtc::VideoTrackVector video_tracks1 = s1->at(i)->GetVideoTracks();
424 webrtc::VideoTrackVector video_tracks2 = s2->at(i)->GetVideoTracks();
425
426 if (audio_tracks1.size() != audio_tracks2.size()) {
427 return false;
428 }
429 for (size_t j = 0; j != audio_tracks1.size(); ++j) {
430 if (audio_tracks1[j]->id() != audio_tracks2[j]->id()) {
431 return false;
432 }
433 }
434 if (video_tracks1.size() != video_tracks2.size()) {
435 return false;
436 }
437 for (size_t j = 0; j != video_tracks1.size(); ++j) {
438 if (video_tracks1[j]->id() != video_tracks2[j]->id()) {
439 return false;
440 }
441 }
442 }
443 return true;
444}
445
perkjd61bf802016-03-24 03:16:19 -0700446// Helper class to test Observer.
447class MockTrackObserver : public ObserverInterface {
448 public:
449 explicit MockTrackObserver(NotifierInterface* notifier)
450 : notifier_(notifier) {
451 notifier_->RegisterObserver(this);
452 }
453
454 ~MockTrackObserver() { Unregister(); }
455
456 void Unregister() {
457 if (notifier_) {
458 notifier_->UnregisterObserver(this);
459 notifier_ = nullptr;
460 }
461 }
462
463 MOCK_METHOD0(OnChanged, void());
464
465 private:
466 NotifierInterface* notifier_;
467};
468
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000469class MockPeerConnectionObserver : public PeerConnectionObserver {
470 public:
kjellander71a1b612016-11-07 01:18:08 -0800471 // We need these using declarations because there are two versions of each of
472 // the below methods and we only override one of them.
473 // TODO(deadbeef): Remove once there's only one version of the methods.
474 using PeerConnectionObserver::OnAddStream;
475 using PeerConnectionObserver::OnRemoveStream;
476 using PeerConnectionObserver::OnDataChannel;
477
deadbeefab9b2d12015-10-14 11:33:11 -0700478 MockPeerConnectionObserver() : remote_streams_(StreamCollection::Create()) {}
Henrik Kjellander3fe372d2016-05-12 08:10:52 +0200479 virtual ~MockPeerConnectionObserver() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000480 }
481 void SetPeerConnectionInterface(PeerConnectionInterface* pc) {
482 pc_ = pc;
483 if (pc) {
484 state_ = pc_->signaling_state();
485 }
486 }
nisseef8b61e2016-04-29 06:09:15 -0700487 void OnSignalingChange(
488 PeerConnectionInterface::SignalingState new_state) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000489 EXPECT_EQ(pc_->signaling_state(), new_state);
490 state_ = new_state;
491 }
492 // TODO(bemasc): Remove this once callers transition to OnIceGatheringChange.
493 virtual void OnStateChange(StateType state_changed) {
494 if (pc_.get() == NULL)
495 return;
496 switch (state_changed) {
497 case kSignalingState:
498 // OnSignalingChange and OnStateChange(kSignalingState) should always
499 // be called approximately simultaneously. To ease testing, we require
500 // that they always be called in that order. This check verifies
501 // that OnSignalingChange has just been called.
502 EXPECT_EQ(pc_->signaling_state(), state_);
503 break;
504 case kIceState:
505 ADD_FAILURE();
506 break;
507 default:
508 ADD_FAILURE();
509 break;
510 }
511 }
deadbeefab9b2d12015-10-14 11:33:11 -0700512
513 MediaStreamInterface* RemoteStream(const std::string& label) {
514 return remote_streams_->find(label);
515 }
516 StreamCollectionInterface* remote_streams() const { return remote_streams_; }
Taylor Brandstetter98cde262016-05-31 13:02:21 -0700517 void OnAddStream(rtc::scoped_refptr<MediaStreamInterface> stream) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000518 last_added_stream_ = stream;
deadbeefab9b2d12015-10-14 11:33:11 -0700519 remote_streams_->AddStream(stream);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000520 }
Taylor Brandstetter98cde262016-05-31 13:02:21 -0700521 void OnRemoveStream(
522 rtc::scoped_refptr<MediaStreamInterface> stream) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000523 last_removed_stream_ = stream;
deadbeefab9b2d12015-10-14 11:33:11 -0700524 remote_streams_->RemoveStream(stream);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000525 }
perkjdfb769d2016-02-09 03:09:43 -0800526 void OnRenegotiationNeeded() override { renegotiation_needed_ = true; }
Taylor Brandstetter98cde262016-05-31 13:02:21 -0700527 void OnDataChannel(
528 rtc::scoped_refptr<DataChannelInterface> data_channel) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000529 last_datachannel_ = data_channel;
530 }
531
perkjdfb769d2016-02-09 03:09:43 -0800532 void OnIceConnectionChange(
533 PeerConnectionInterface::IceConnectionState new_state) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000534 EXPECT_EQ(pc_->ice_connection_state(), new_state);
zhihuang81c3a032016-11-17 12:06:24 -0800535 callback_triggered_ = true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000536 }
perkjdfb769d2016-02-09 03:09:43 -0800537 void OnIceGatheringChange(
538 PeerConnectionInterface::IceGatheringState new_state) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000539 EXPECT_EQ(pc_->ice_gathering_state(), new_state);
perkjdfb769d2016-02-09 03:09:43 -0800540 ice_complete_ = new_state == PeerConnectionInterface::kIceGatheringComplete;
zhihuang81c3a032016-11-17 12:06:24 -0800541 callback_triggered_ = true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000542 }
perkjdfb769d2016-02-09 03:09:43 -0800543 void OnIceCandidate(const webrtc::IceCandidateInterface* candidate) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000544 EXPECT_NE(PeerConnectionInterface::kIceGatheringNew,
545 pc_->ice_gathering_state());
546
547 std::string sdp;
548 EXPECT_TRUE(candidate->ToString(&sdp));
549 EXPECT_LT(0u, sdp.size());
550 last_candidate_.reset(webrtc::CreateIceCandidate(candidate->sdp_mid(),
551 candidate->sdp_mline_index(), sdp, NULL));
552 EXPECT_TRUE(last_candidate_.get() != NULL);
zhihuang81c3a032016-11-17 12:06:24 -0800553 callback_triggered_ = true;
zhihuang29ff8442016-07-27 11:07:25 -0700554 }
555
556 void OnIceCandidatesRemoved(
557 const std::vector<cricket::Candidate>& candidates) override {
zhihuang81c3a032016-11-17 12:06:24 -0800558 callback_triggered_ = true;
zhihuang29ff8442016-07-27 11:07:25 -0700559 }
560
561 void OnIceConnectionReceivingChange(bool receiving) override {
zhihuang81c3a032016-11-17 12:06:24 -0800562 callback_triggered_ = true;
563 }
564
565 void OnAddTrack(rtc::scoped_refptr<webrtc::RtpReceiverInterface> receiver,
566 std::vector<rtc::scoped_refptr<webrtc::MediaStreamInterface>>
567 streams) override {
568 EXPECT_TRUE(receiver != nullptr);
569 num_added_tracks_++;
570 last_added_track_label_ = receiver->id();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000571 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000572
573 // Returns the label of the last added stream.
574 // Empty string if no stream have been added.
575 std::string GetLastAddedStreamLabel() {
576 if (last_added_stream_.get())
577 return last_added_stream_->label();
578 return "";
579 }
580 std::string GetLastRemovedStreamLabel() {
581 if (last_removed_stream_.get())
582 return last_removed_stream_->label();
583 return "";
584 }
585
zhihuang9763d562016-08-05 11:14:50 -0700586 rtc::scoped_refptr<PeerConnectionInterface> pc_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000587 PeerConnectionInterface::SignalingState state_;
kwibergd1fe2812016-04-27 06:47:29 -0700588 std::unique_ptr<IceCandidateInterface> last_candidate_;
zhihuang9763d562016-08-05 11:14:50 -0700589 rtc::scoped_refptr<DataChannelInterface> last_datachannel_;
deadbeefab9b2d12015-10-14 11:33:11 -0700590 rtc::scoped_refptr<StreamCollection> remote_streams_;
591 bool renegotiation_needed_ = false;
592 bool ice_complete_ = false;
zhihuang81c3a032016-11-17 12:06:24 -0800593 bool callback_triggered_ = false;
594 int num_added_tracks_ = 0;
595 std::string last_added_track_label_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000596
597 private:
zhihuang9763d562016-08-05 11:14:50 -0700598 rtc::scoped_refptr<MediaStreamInterface> last_added_stream_;
599 rtc::scoped_refptr<MediaStreamInterface> last_removed_stream_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000600};
601
602} // namespace
deadbeefab9b2d12015-10-14 11:33:11 -0700603
zhihuang29ff8442016-07-27 11:07:25 -0700604// The PeerConnectionMediaConfig tests below verify that configuration
605// and constraints are propagated into the MediaConfig passed to
606// CreateMediaController. These settings are intended for MediaChannel
607// constructors, but that is not exercised by these unittest.
608class PeerConnectionFactoryForTest : public webrtc::PeerConnectionFactory {
609 public:
610 webrtc::MediaControllerInterface* CreateMediaController(
skvlad11a9cbf2016-10-07 11:53:05 -0700611 const cricket::MediaConfig& config,
612 webrtc::RtcEventLog* event_log) const override {
zhihuang29ff8442016-07-27 11:07:25 -0700613 create_media_controller_called_ = true;
614 create_media_controller_config_ = config;
615
616 webrtc::MediaControllerInterface* mc =
skvlad11a9cbf2016-10-07 11:53:05 -0700617 PeerConnectionFactory::CreateMediaController(config, event_log);
zhihuang29ff8442016-07-27 11:07:25 -0700618 EXPECT_TRUE(mc != nullptr);
619 return mc;
620 }
621
622 cricket::TransportController* CreateTransportController(
Honghai Zhangbfd398c2016-08-30 22:07:42 -0700623 cricket::PortAllocator* port_allocator,
624 bool redetermine_role_on_ice_restart) override {
zhihuang29ff8442016-07-27 11:07:25 -0700625 transport_controller = new cricket::TransportController(
Honghai Zhangbfd398c2016-08-30 22:07:42 -0700626 rtc::Thread::Current(), rtc::Thread::Current(), port_allocator,
627 redetermine_role_on_ice_restart);
zhihuang29ff8442016-07-27 11:07:25 -0700628 return transport_controller;
629 }
630
631 cricket::TransportController* transport_controller;
632 // Mutable, so they can be modified in the above const-declared method.
633 mutable bool create_media_controller_called_ = false;
634 mutable cricket::MediaConfig create_media_controller_config_;
635};
636
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000637class PeerConnectionInterfaceTest : public testing::Test {
638 protected:
phoglund37ebcf02016-01-08 05:04:57 -0800639 PeerConnectionInterfaceTest() {
640#ifdef WEBRTC_ANDROID
641 webrtc::InitializeAndroidObjects();
642#endif
643 }
644
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000645 virtual void SetUp() {
646 pc_factory_ = webrtc::CreatePeerConnectionFactory(
danilchape9021a32016-05-17 01:52:02 -0700647 rtc::Thread::Current(), rtc::Thread::Current(), rtc::Thread::Current(),
648 nullptr, nullptr, nullptr);
649 ASSERT_TRUE(pc_factory_);
zhihuang29ff8442016-07-27 11:07:25 -0700650 pc_factory_for_test_ =
651 new rtc::RefCountedObject<PeerConnectionFactoryForTest>();
652 pc_factory_for_test_->Initialize();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000653 }
654
655 void CreatePeerConnection() {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700656 CreatePeerConnection(PeerConnectionInterface::RTCConfiguration(), nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000657 }
658
659 void CreatePeerConnection(webrtc::MediaConstraintsInterface* constraints) {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700660 CreatePeerConnection(PeerConnectionInterface::RTCConfiguration(),
661 constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000662 }
663
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700664 void CreatePeerConnectionWithIceTransportsType(
665 PeerConnectionInterface::IceTransportsType type) {
666 PeerConnectionInterface::RTCConfiguration config;
667 config.type = type;
668 return CreatePeerConnection(config, nullptr);
669 }
670
671 void CreatePeerConnectionWithIceServer(const std::string& uri,
672 const std::string& password) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800673 PeerConnectionInterface::RTCConfiguration config;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000674 PeerConnectionInterface::IceServer server;
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700675 server.uri = uri;
676 server.password = password;
677 config.servers.push_back(server);
678 CreatePeerConnection(config, nullptr);
679 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000680
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700681 void CreatePeerConnection(PeerConnectionInterface::RTCConfiguration config,
682 webrtc::MediaConstraintsInterface* constraints) {
kwibergd1fe2812016-04-27 06:47:29 -0700683 std::unique_ptr<cricket::FakePortAllocator> port_allocator(
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800684 new cricket::FakePortAllocator(rtc::Thread::Current(), nullptr));
685 port_allocator_ = port_allocator.get();
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000686
buildbot@webrtc.org61c1b8e2014-04-09 06:06:38 +0000687 // DTLS does not work in a loopback call, so is disabled for most of the
688 // tests in this file. We only create a FakeIdentityService if the test
689 // explicitly sets the constraint.
jiayl@webrtc.org61e00b02015-03-04 22:17:38 +0000690 FakeConstraints default_constraints;
691 if (!constraints) {
692 constraints = &default_constraints;
693
694 default_constraints.AddMandatory(
695 webrtc::MediaConstraintsInterface::kEnableDtlsSrtp, false);
696 }
697
Henrik Boströmd79599d2016-06-01 13:58:50 +0200698 std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator;
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000699 bool dtls;
700 if (FindConstraint(constraints,
701 webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
702 &dtls,
Henrik Boström5e56c592015-08-11 10:33:13 +0200703 nullptr) && dtls) {
Henrik Boströmd79599d2016-06-01 13:58:50 +0200704 cert_generator.reset(new FakeRTCCertificateGenerator());
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000705 }
Henrik Boströmd79599d2016-06-01 13:58:50 +0200706 pc_ = pc_factory_->CreatePeerConnection(
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800707 config, constraints, std::move(port_allocator),
Henrik Boströmd79599d2016-06-01 13:58:50 +0200708 std::move(cert_generator), &observer_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000709 ASSERT_TRUE(pc_.get() != NULL);
710 observer_.SetPeerConnectionInterface(pc_.get());
711 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
712 }
713
deadbeef0a6c4ca2015-10-06 11:38:28 -0700714 void CreatePeerConnectionExpectFail(const std::string& uri) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800715 PeerConnectionInterface::RTCConfiguration config;
deadbeef0a6c4ca2015-10-06 11:38:28 -0700716 PeerConnectionInterface::IceServer server;
deadbeef0a6c4ca2015-10-06 11:38:28 -0700717 server.uri = uri;
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800718 config.servers.push_back(server);
deadbeef0a6c4ca2015-10-06 11:38:28 -0700719
zhihuang9763d562016-08-05 11:14:50 -0700720 rtc::scoped_refptr<PeerConnectionInterface> pc;
hbosd7973cc2016-05-27 06:08:53 -0700721 pc = pc_factory_->CreatePeerConnection(config, nullptr, nullptr, nullptr,
722 &observer_);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800723 EXPECT_EQ(nullptr, pc);
deadbeef0a6c4ca2015-10-06 11:38:28 -0700724 }
725
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000726 void CreatePeerConnectionWithDifferentConfigurations() {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700727 CreatePeerConnectionWithIceServer(kStunAddressOnly, "");
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800728 EXPECT_EQ(1u, port_allocator_->stun_servers().size());
729 EXPECT_EQ(0u, port_allocator_->turn_servers().size());
730 EXPECT_EQ("address", port_allocator_->stun_servers().begin()->hostname());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000731 EXPECT_EQ(kDefaultStunPort,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800732 port_allocator_->stun_servers().begin()->port());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000733
deadbeef0a6c4ca2015-10-06 11:38:28 -0700734 CreatePeerConnectionExpectFail(kStunInvalidPort);
735 CreatePeerConnectionExpectFail(kStunAddressPortAndMore1);
736 CreatePeerConnectionExpectFail(kStunAddressPortAndMore2);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000737
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700738 CreatePeerConnectionWithIceServer(kTurnIceServerUri, kTurnPassword);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800739 EXPECT_EQ(0u, port_allocator_->stun_servers().size());
740 EXPECT_EQ(1u, port_allocator_->turn_servers().size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000741 EXPECT_EQ(kTurnUsername,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800742 port_allocator_->turn_servers()[0].credentials.username);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000743 EXPECT_EQ(kTurnPassword,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800744 port_allocator_->turn_servers()[0].credentials.password);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000745 EXPECT_EQ(kTurnHostname,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800746 port_allocator_->turn_servers()[0].ports[0].address.hostname());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000747 }
748
749 void ReleasePeerConnection() {
750 pc_ = NULL;
751 observer_.SetPeerConnectionInterface(NULL);
752 }
753
deadbeefab9b2d12015-10-14 11:33:11 -0700754 void AddVideoStream(const std::string& label) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000755 // Create a local stream.
zhihuang9763d562016-08-05 11:14:50 -0700756 rtc::scoped_refptr<MediaStreamInterface> stream(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000757 pc_factory_->CreateLocalMediaStream(label));
zhihuang9763d562016-08-05 11:14:50 -0700758 rtc::scoped_refptr<VideoTrackSourceInterface> video_source(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000759 pc_factory_->CreateVideoSource(new cricket::FakeVideoCapturer(), NULL));
zhihuang9763d562016-08-05 11:14:50 -0700760 rtc::scoped_refptr<VideoTrackInterface> video_track(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000761 pc_factory_->CreateVideoTrack(label + "v0", video_source));
762 stream->AddTrack(video_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +0000763 EXPECT_TRUE(pc_->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000764 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
765 observer_.renegotiation_needed_ = false;
766 }
767
768 void AddVoiceStream(const std::string& label) {
769 // Create a local stream.
zhihuang9763d562016-08-05 11:14:50 -0700770 rtc::scoped_refptr<MediaStreamInterface> stream(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000771 pc_factory_->CreateLocalMediaStream(label));
zhihuang9763d562016-08-05 11:14:50 -0700772 rtc::scoped_refptr<AudioTrackInterface> audio_track(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000773 pc_factory_->CreateAudioTrack(label + "a0", NULL));
774 stream->AddTrack(audio_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +0000775 EXPECT_TRUE(pc_->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000776 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
777 observer_.renegotiation_needed_ = false;
778 }
779
780 void AddAudioVideoStream(const std::string& stream_label,
781 const std::string& audio_track_label,
782 const std::string& video_track_label) {
783 // Create a local stream.
zhihuang9763d562016-08-05 11:14:50 -0700784 rtc::scoped_refptr<MediaStreamInterface> stream(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000785 pc_factory_->CreateLocalMediaStream(stream_label));
zhihuang9763d562016-08-05 11:14:50 -0700786 rtc::scoped_refptr<AudioTrackInterface> audio_track(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000787 pc_factory_->CreateAudioTrack(
788 audio_track_label, static_cast<AudioSourceInterface*>(NULL)));
789 stream->AddTrack(audio_track.get());
zhihuang9763d562016-08-05 11:14:50 -0700790 rtc::scoped_refptr<VideoTrackInterface> video_track(
nisseaf510af2016-03-21 08:20:42 -0700791 pc_factory_->CreateVideoTrack(
792 video_track_label,
793 pc_factory_->CreateVideoSource(new cricket::FakeVideoCapturer())));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000794 stream->AddTrack(video_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +0000795 EXPECT_TRUE(pc_->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000796 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
797 observer_.renegotiation_needed_ = false;
798 }
799
kwibergd1fe2812016-04-27 06:47:29 -0700800 bool DoCreateOfferAnswer(std::unique_ptr<SessionDescriptionInterface>* desc,
deadbeefc80741f2015-10-22 13:14:45 -0700801 bool offer,
802 MediaConstraintsInterface* constraints) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000803 rtc::scoped_refptr<MockCreateSessionDescriptionObserver>
804 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000805 MockCreateSessionDescriptionObserver>());
806 if (offer) {
deadbeefc80741f2015-10-22 13:14:45 -0700807 pc_->CreateOffer(observer, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000808 } else {
deadbeefc80741f2015-10-22 13:14:45 -0700809 pc_->CreateAnswer(observer, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000810 }
811 EXPECT_EQ_WAIT(true, observer->called(), kTimeout);
kwiberg2bbff992016-03-16 11:03:04 -0700812 desc->reset(observer->release_desc());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000813 return observer->result();
814 }
815
kwibergd1fe2812016-04-27 06:47:29 -0700816 bool DoCreateOffer(std::unique_ptr<SessionDescriptionInterface>* desc,
deadbeefc80741f2015-10-22 13:14:45 -0700817 MediaConstraintsInterface* constraints) {
818 return DoCreateOfferAnswer(desc, true, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000819 }
820
kwibergd1fe2812016-04-27 06:47:29 -0700821 bool DoCreateAnswer(std::unique_ptr<SessionDescriptionInterface>* desc,
deadbeefc80741f2015-10-22 13:14:45 -0700822 MediaConstraintsInterface* constraints) {
823 return DoCreateOfferAnswer(desc, false, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000824 }
825
826 bool DoSetSessionDescription(SessionDescriptionInterface* desc, bool local) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000827 rtc::scoped_refptr<MockSetSessionDescriptionObserver>
828 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000829 MockSetSessionDescriptionObserver>());
830 if (local) {
831 pc_->SetLocalDescription(observer, desc);
832 } else {
833 pc_->SetRemoteDescription(observer, desc);
834 }
zhihuang29ff8442016-07-27 11:07:25 -0700835 if (pc_->signaling_state() != PeerConnectionInterface::kClosed) {
836 EXPECT_EQ_WAIT(true, observer->called(), kTimeout);
837 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000838 return observer->result();
839 }
840
841 bool DoSetLocalDescription(SessionDescriptionInterface* desc) {
842 return DoSetSessionDescription(desc, true);
843 }
844
845 bool DoSetRemoteDescription(SessionDescriptionInterface* desc) {
846 return DoSetSessionDescription(desc, false);
847 }
848
849 // Calls PeerConnection::GetStats and check the return value.
850 // It does not verify the values in the StatReports since a RTCP packet might
851 // be required.
852 bool DoGetStats(MediaStreamTrackInterface* track) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000853 rtc::scoped_refptr<MockStatsObserver> observer(
854 new rtc::RefCountedObject<MockStatsObserver>());
jiayl@webrtc.orgdb41b4d2014-03-03 21:30:06 +0000855 if (!pc_->GetStats(
856 observer, track, PeerConnectionInterface::kStatsOutputLevelStandard))
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000857 return false;
858 EXPECT_TRUE_WAIT(observer->called(), kTimeout);
859 return observer->called();
860 }
861
862 void InitiateCall() {
863 CreatePeerConnection();
864 // Create a local stream with audio&video tracks.
865 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
866 CreateOfferReceiveAnswer();
867 }
868
869 // Verify that RTP Header extensions has been negotiated for audio and video.
870 void VerifyRemoteRtpHeaderExtensions() {
871 const cricket::MediaContentDescription* desc =
872 cricket::GetFirstAudioContentDescription(
873 pc_->remote_description()->description());
874 ASSERT_TRUE(desc != NULL);
875 EXPECT_GT(desc->rtp_header_extensions().size(), 0u);
876
877 desc = cricket::GetFirstVideoContentDescription(
878 pc_->remote_description()->description());
879 ASSERT_TRUE(desc != NULL);
880 EXPECT_GT(desc->rtp_header_extensions().size(), 0u);
881 }
882
883 void CreateOfferAsRemoteDescription() {
kwibergd1fe2812016-04-27 06:47:29 -0700884 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -0700885 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000886 std::string sdp;
887 EXPECT_TRUE(offer->ToString(&sdp));
888 SessionDescriptionInterface* remote_offer =
889 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
890 sdp, NULL);
891 EXPECT_TRUE(DoSetRemoteDescription(remote_offer));
892 EXPECT_EQ(PeerConnectionInterface::kHaveRemoteOffer, observer_.state_);
893 }
894
deadbeefab9b2d12015-10-14 11:33:11 -0700895 void CreateAndSetRemoteOffer(const std::string& sdp) {
896 SessionDescriptionInterface* remote_offer =
897 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
898 sdp, nullptr);
899 EXPECT_TRUE(DoSetRemoteDescription(remote_offer));
900 EXPECT_EQ(PeerConnectionInterface::kHaveRemoteOffer, observer_.state_);
901 }
902
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000903 void CreateAnswerAsLocalDescription() {
kwibergd1fe2812016-04-27 06:47:29 -0700904 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -0700905 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000906
907 // TODO(perkj): Currently SetLocalDescription fails if any parameters in an
908 // audio codec change, even if the parameter has nothing to do with
909 // receiving. Not all parameters are serialized to SDP.
910 // Since CreatePrAnswerAsLocalDescription serialize/deserialize
911 // the SessionDescription, it is necessary to do that here to in order to
912 // get ReceiveOfferCreatePrAnswerAndAnswer and RenegotiateAudioOnly to pass.
913 // https://code.google.com/p/webrtc/issues/detail?id=1356
914 std::string sdp;
915 EXPECT_TRUE(answer->ToString(&sdp));
916 SessionDescriptionInterface* new_answer =
917 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
918 sdp, NULL);
919 EXPECT_TRUE(DoSetLocalDescription(new_answer));
920 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
921 }
922
923 void CreatePrAnswerAsLocalDescription() {
kwibergd1fe2812016-04-27 06:47:29 -0700924 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -0700925 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000926
927 std::string sdp;
928 EXPECT_TRUE(answer->ToString(&sdp));
929 SessionDescriptionInterface* pr_answer =
930 webrtc::CreateSessionDescription(SessionDescriptionInterface::kPrAnswer,
931 sdp, NULL);
932 EXPECT_TRUE(DoSetLocalDescription(pr_answer));
933 EXPECT_EQ(PeerConnectionInterface::kHaveLocalPrAnswer, observer_.state_);
934 }
935
936 void CreateOfferReceiveAnswer() {
937 CreateOfferAsLocalDescription();
938 std::string sdp;
939 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
940 CreateAnswerAsRemoteDescription(sdp);
941 }
942
943 void CreateOfferAsLocalDescription() {
kwibergd1fe2812016-04-27 06:47:29 -0700944 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -0700945 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000946 // TODO(perkj): Currently SetLocalDescription fails if any parameters in an
947 // audio codec change, even if the parameter has nothing to do with
948 // receiving. Not all parameters are serialized to SDP.
949 // Since CreatePrAnswerAsLocalDescription serialize/deserialize
950 // the SessionDescription, it is necessary to do that here to in order to
951 // get ReceiveOfferCreatePrAnswerAndAnswer and RenegotiateAudioOnly to pass.
952 // https://code.google.com/p/webrtc/issues/detail?id=1356
953 std::string sdp;
954 EXPECT_TRUE(offer->ToString(&sdp));
955 SessionDescriptionInterface* new_offer =
956 webrtc::CreateSessionDescription(
957 SessionDescriptionInterface::kOffer,
958 sdp, NULL);
959
960 EXPECT_TRUE(DoSetLocalDescription(new_offer));
961 EXPECT_EQ(PeerConnectionInterface::kHaveLocalOffer, observer_.state_);
mallinath@webrtc.org68cbd012014-01-22 00:16:46 +0000962 // Wait for the ice_complete message, so that SDP will have candidates.
963 EXPECT_TRUE_WAIT(observer_.ice_complete_, kTimeout);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000964 }
965
deadbeefab9b2d12015-10-14 11:33:11 -0700966 void CreateAnswerAsRemoteDescription(const std::string& sdp) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000967 webrtc::JsepSessionDescription* answer = new webrtc::JsepSessionDescription(
968 SessionDescriptionInterface::kAnswer);
deadbeefab9b2d12015-10-14 11:33:11 -0700969 EXPECT_TRUE(answer->Initialize(sdp, NULL));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000970 EXPECT_TRUE(DoSetRemoteDescription(answer));
971 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
972 }
973
deadbeefab9b2d12015-10-14 11:33:11 -0700974 void CreatePrAnswerAndAnswerAsRemoteDescription(const std::string& sdp) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000975 webrtc::JsepSessionDescription* pr_answer =
976 new webrtc::JsepSessionDescription(
977 SessionDescriptionInterface::kPrAnswer);
deadbeefab9b2d12015-10-14 11:33:11 -0700978 EXPECT_TRUE(pr_answer->Initialize(sdp, NULL));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000979 EXPECT_TRUE(DoSetRemoteDescription(pr_answer));
980 EXPECT_EQ(PeerConnectionInterface::kHaveRemotePrAnswer, observer_.state_);
981 webrtc::JsepSessionDescription* answer =
982 new webrtc::JsepSessionDescription(
983 SessionDescriptionInterface::kAnswer);
deadbeefab9b2d12015-10-14 11:33:11 -0700984 EXPECT_TRUE(answer->Initialize(sdp, NULL));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000985 EXPECT_TRUE(DoSetRemoteDescription(answer));
986 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
987 }
988
989 // Help function used for waiting until a the last signaled remote stream has
990 // the same label as |stream_label|. In a few of the tests in this file we
991 // answer with the same session description as we offer and thus we can
992 // check if OnAddStream have been called with the same stream as we offer to
993 // send.
994 void WaitAndVerifyOnAddStream(const std::string& stream_label) {
995 EXPECT_EQ_WAIT(stream_label, observer_.GetLastAddedStreamLabel(), kTimeout);
996 }
997
998 // Creates an offer and applies it as a local session description.
999 // Creates an answer with the same SDP an the offer but removes all lines
1000 // that start with a:ssrc"
1001 void CreateOfferReceiveAnswerWithoutSsrc() {
1002 CreateOfferAsLocalDescription();
1003 std::string sdp;
1004 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
1005 SetSsrcToZero(&sdp);
1006 CreateAnswerAsRemoteDescription(sdp);
1007 }
1008
deadbeefab9b2d12015-10-14 11:33:11 -07001009 // This function creates a MediaStream with label kStreams[0] and
1010 // |number_of_audio_tracks| and |number_of_video_tracks| tracks and the
1011 // corresponding SessionDescriptionInterface. The SessionDescriptionInterface
kwiberg2bbff992016-03-16 11:03:04 -07001012 // is returned and the MediaStream is stored in
deadbeefab9b2d12015-10-14 11:33:11 -07001013 // |reference_collection_|
kwibergd1fe2812016-04-27 06:47:29 -07001014 std::unique_ptr<SessionDescriptionInterface>
kwiberg2bbff992016-03-16 11:03:04 -07001015 CreateSessionDescriptionAndReference(size_t number_of_audio_tracks,
1016 size_t number_of_video_tracks) {
1017 EXPECT_LE(number_of_audio_tracks, 2u);
1018 EXPECT_LE(number_of_video_tracks, 2u);
deadbeefab9b2d12015-10-14 11:33:11 -07001019
1020 reference_collection_ = StreamCollection::Create();
1021 std::string sdp_ms1 = std::string(kSdpStringInit);
1022
1023 std::string mediastream_label = kStreams[0];
1024
1025 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream(
1026 webrtc::MediaStream::Create(mediastream_label));
1027 reference_collection_->AddStream(stream);
1028
1029 if (number_of_audio_tracks > 0) {
1030 sdp_ms1 += std::string(kSdpStringAudio);
1031 sdp_ms1 += std::string(kSdpStringMs1Audio0);
1032 AddAudioTrack(kAudioTracks[0], stream);
1033 }
1034 if (number_of_audio_tracks > 1) {
1035 sdp_ms1 += kSdpStringMs1Audio1;
1036 AddAudioTrack(kAudioTracks[1], stream);
1037 }
1038
1039 if (number_of_video_tracks > 0) {
1040 sdp_ms1 += std::string(kSdpStringVideo);
1041 sdp_ms1 += std::string(kSdpStringMs1Video0);
1042 AddVideoTrack(kVideoTracks[0], stream);
1043 }
1044 if (number_of_video_tracks > 1) {
1045 sdp_ms1 += kSdpStringMs1Video1;
1046 AddVideoTrack(kVideoTracks[1], stream);
1047 }
1048
kwibergd1fe2812016-04-27 06:47:29 -07001049 return std::unique_ptr<SessionDescriptionInterface>(
kwiberg2bbff992016-03-16 11:03:04 -07001050 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
1051 sdp_ms1, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07001052 }
1053
1054 void AddAudioTrack(const std::string& track_id,
1055 MediaStreamInterface* stream) {
1056 rtc::scoped_refptr<webrtc::AudioTrackInterface> audio_track(
1057 webrtc::AudioTrack::Create(track_id, nullptr));
1058 ASSERT_TRUE(stream->AddTrack(audio_track));
1059 }
1060
1061 void AddVideoTrack(const std::string& track_id,
1062 MediaStreamInterface* stream) {
1063 rtc::scoped_refptr<webrtc::VideoTrackInterface> video_track(
nisseaf510af2016-03-21 08:20:42 -07001064 webrtc::VideoTrack::Create(track_id,
1065 webrtc::FakeVideoTrackSource::Create()));
deadbeefab9b2d12015-10-14 11:33:11 -07001066 ASSERT_TRUE(stream->AddTrack(video_track));
1067 }
1068
kwibergfd8be342016-05-14 19:44:11 -07001069 std::unique_ptr<SessionDescriptionInterface> CreateOfferWithOneAudioStream() {
zhihuang8f65cdf2016-05-06 18:40:30 -07001070 CreatePeerConnection();
1071 AddVoiceStream(kStreamLabel1);
kwibergfd8be342016-05-14 19:44:11 -07001072 std::unique_ptr<SessionDescriptionInterface> offer;
zhihuang8f65cdf2016-05-06 18:40:30 -07001073 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
1074 return offer;
1075 }
1076
kwibergfd8be342016-05-14 19:44:11 -07001077 std::unique_ptr<SessionDescriptionInterface>
zhihuang8f65cdf2016-05-06 18:40:30 -07001078 CreateAnswerWithOneAudioStream() {
kwibergfd8be342016-05-14 19:44:11 -07001079 std::unique_ptr<SessionDescriptionInterface> offer =
zhihuang8f65cdf2016-05-06 18:40:30 -07001080 CreateOfferWithOneAudioStream();
1081 EXPECT_TRUE(DoSetRemoteDescription(offer.release()));
kwibergfd8be342016-05-14 19:44:11 -07001082 std::unique_ptr<SessionDescriptionInterface> answer;
zhihuang8f65cdf2016-05-06 18:40:30 -07001083 EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
1084 return answer;
1085 }
1086
1087 const std::string& GetFirstAudioStreamCname(
1088 const SessionDescriptionInterface* desc) {
1089 const cricket::ContentInfo* audio_content =
1090 cricket::GetFirstAudioContent(desc->description());
1091 const cricket::AudioContentDescription* audio_desc =
1092 static_cast<const cricket::AudioContentDescription*>(
1093 audio_content->description);
1094 return audio_desc->streams()[0].cname;
1095 }
1096
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08001097 cricket::FakePortAllocator* port_allocator_ = nullptr;
zhihuang9763d562016-08-05 11:14:50 -07001098 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface> pc_factory_;
1099 rtc::scoped_refptr<PeerConnectionFactoryForTest> pc_factory_for_test_;
1100 rtc::scoped_refptr<PeerConnectionInterface> pc_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001101 MockPeerConnectionObserver observer_;
deadbeefab9b2d12015-10-14 11:33:11 -07001102 rtc::scoped_refptr<StreamCollection> reference_collection_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001103};
1104
zhihuang29ff8442016-07-27 11:07:25 -07001105// Test that no callbacks on the PeerConnectionObserver are called after the
1106// PeerConnection is closed.
1107TEST_F(PeerConnectionInterfaceTest, CloseAndTestCallbackFunctions) {
zhihuang9763d562016-08-05 11:14:50 -07001108 rtc::scoped_refptr<PeerConnectionInterface> pc(
zhihuang29ff8442016-07-27 11:07:25 -07001109 pc_factory_for_test_->CreatePeerConnection(
1110 PeerConnectionInterface::RTCConfiguration(), nullptr, nullptr,
1111 nullptr, &observer_));
1112 observer_.SetPeerConnectionInterface(pc.get());
1113 pc->Close();
1114
1115 // No callbacks is expected to be called.
zhihuang81c3a032016-11-17 12:06:24 -08001116 observer_.callback_triggered_ = false;
zhihuang29ff8442016-07-27 11:07:25 -07001117 std::vector<cricket::Candidate> candidates;
1118 pc_factory_for_test_->transport_controller->SignalGatheringState(
1119 cricket::IceGatheringState{});
1120 pc_factory_for_test_->transport_controller->SignalCandidatesGathered(
1121 "", candidates);
1122 pc_factory_for_test_->transport_controller->SignalConnectionState(
1123 cricket::IceConnectionState{});
1124 pc_factory_for_test_->transport_controller->SignalCandidatesRemoved(
1125 candidates);
1126 pc_factory_for_test_->transport_controller->SignalReceiving(false);
zhihuang81c3a032016-11-17 12:06:24 -08001127 EXPECT_FALSE(observer_.callback_triggered_);
zhihuang29ff8442016-07-27 11:07:25 -07001128}
1129
zhihuang8f65cdf2016-05-06 18:40:30 -07001130// Generate different CNAMEs when PeerConnections are created.
1131// The CNAMEs are expected to be generated randomly. It is possible
1132// that the test fails, though the possibility is very low.
1133TEST_F(PeerConnectionInterfaceTest, CnameGenerationInOffer) {
kwibergfd8be342016-05-14 19:44:11 -07001134 std::unique_ptr<SessionDescriptionInterface> offer1 =
zhihuang8f65cdf2016-05-06 18:40:30 -07001135 CreateOfferWithOneAudioStream();
kwibergfd8be342016-05-14 19:44:11 -07001136 std::unique_ptr<SessionDescriptionInterface> offer2 =
zhihuang8f65cdf2016-05-06 18:40:30 -07001137 CreateOfferWithOneAudioStream();
1138 EXPECT_NE(GetFirstAudioStreamCname(offer1.get()),
1139 GetFirstAudioStreamCname(offer2.get()));
1140}
1141
1142TEST_F(PeerConnectionInterfaceTest, CnameGenerationInAnswer) {
kwibergfd8be342016-05-14 19:44:11 -07001143 std::unique_ptr<SessionDescriptionInterface> answer1 =
zhihuang8f65cdf2016-05-06 18:40:30 -07001144 CreateAnswerWithOneAudioStream();
kwibergfd8be342016-05-14 19:44:11 -07001145 std::unique_ptr<SessionDescriptionInterface> answer2 =
zhihuang8f65cdf2016-05-06 18:40:30 -07001146 CreateAnswerWithOneAudioStream();
1147 EXPECT_NE(GetFirstAudioStreamCname(answer1.get()),
1148 GetFirstAudioStreamCname(answer2.get()));
1149}
1150
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001151TEST_F(PeerConnectionInterfaceTest,
1152 CreatePeerConnectionWithDifferentConfigurations) {
1153 CreatePeerConnectionWithDifferentConfigurations();
1154}
1155
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001156TEST_F(PeerConnectionInterfaceTest,
1157 CreatePeerConnectionWithDifferentIceTransportsTypes) {
1158 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kNone);
1159 EXPECT_EQ(cricket::CF_NONE, port_allocator_->candidate_filter());
1160 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kRelay);
1161 EXPECT_EQ(cricket::CF_RELAY, port_allocator_->candidate_filter());
1162 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kNoHost);
1163 EXPECT_EQ(cricket::CF_ALL & ~cricket::CF_HOST,
1164 port_allocator_->candidate_filter());
1165 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kAll);
1166 EXPECT_EQ(cricket::CF_ALL, port_allocator_->candidate_filter());
1167}
1168
1169// Test that when a PeerConnection is created with a nonzero candidate pool
1170// size, the pooled PortAllocatorSession is created with all the attributes
1171// in the RTCConfiguration.
1172TEST_F(PeerConnectionInterfaceTest, CreatePeerConnectionWithPooledCandidates) {
1173 PeerConnectionInterface::RTCConfiguration config;
1174 PeerConnectionInterface::IceServer server;
1175 server.uri = kStunAddressOnly;
1176 config.servers.push_back(server);
1177 config.type = PeerConnectionInterface::kRelay;
1178 config.disable_ipv6 = true;
1179 config.tcp_candidate_policy =
1180 PeerConnectionInterface::kTcpCandidatePolicyDisabled;
honghaiz60347052016-05-31 18:29:12 -07001181 config.candidate_network_policy =
1182 PeerConnectionInterface::kCandidateNetworkPolicyLowCost;
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001183 config.ice_candidate_pool_size = 1;
1184 CreatePeerConnection(config, nullptr);
1185
1186 const cricket::FakePortAllocatorSession* session =
1187 static_cast<const cricket::FakePortAllocatorSession*>(
1188 port_allocator_->GetPooledSession());
1189 ASSERT_NE(nullptr, session);
1190 EXPECT_EQ(1UL, session->stun_servers().size());
1191 EXPECT_EQ(0U, session->flags() & cricket::PORTALLOCATOR_ENABLE_IPV6);
1192 EXPECT_LT(0U, session->flags() & cricket::PORTALLOCATOR_DISABLE_TCP);
honghaiz60347052016-05-31 18:29:12 -07001193 EXPECT_LT(0U,
1194 session->flags() & cricket::PORTALLOCATOR_DISABLE_COSTLY_NETWORKS);
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001195}
1196
Taylor Brandstetterf8e65772016-06-27 17:20:15 -07001197// Test that the PeerConnection initializes the port allocator passed into it,
1198// and on the correct thread.
1199TEST_F(PeerConnectionInterfaceTest,
1200 CreatePeerConnectionInitializesPortAllocator) {
1201 rtc::Thread network_thread;
1202 network_thread.Start();
1203 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface> pc_factory(
1204 webrtc::CreatePeerConnectionFactory(
1205 &network_thread, rtc::Thread::Current(), rtc::Thread::Current(),
1206 nullptr, nullptr, nullptr));
1207 std::unique_ptr<cricket::FakePortAllocator> port_allocator(
1208 new cricket::FakePortAllocator(&network_thread, nullptr));
1209 cricket::FakePortAllocator* raw_port_allocator = port_allocator.get();
1210 PeerConnectionInterface::RTCConfiguration config;
1211 rtc::scoped_refptr<PeerConnectionInterface> pc(
1212 pc_factory->CreatePeerConnection(
1213 config, nullptr, std::move(port_allocator), nullptr, &observer_));
1214 // FakePortAllocator RTC_CHECKs that it's initialized on the right thread,
1215 // so all we have to do here is check that it's initialized.
1216 EXPECT_TRUE(raw_port_allocator->initialized());
1217}
1218
deadbeef46c73892016-11-16 19:42:04 -08001219// Check that GetConfiguration returns the configuration the PeerConnection was
1220// constructed with, before SetConfiguration is called.
1221TEST_F(PeerConnectionInterfaceTest, GetConfigurationAfterCreatePeerConnection) {
1222 PeerConnectionInterface::RTCConfiguration config;
1223 config.type = PeerConnectionInterface::kRelay;
1224 CreatePeerConnection(config, nullptr);
1225
1226 PeerConnectionInterface::RTCConfiguration returned_config =
1227 pc_->GetConfiguration();
1228 EXPECT_EQ(PeerConnectionInterface::kRelay, returned_config.type);
1229}
1230
1231// Check that GetConfiguration returns the last configuration passed into
1232// SetConfiguration.
1233TEST_F(PeerConnectionInterfaceTest, GetConfigurationAfterSetConfiguration) {
1234 CreatePeerConnection();
1235
1236 PeerConnectionInterface::RTCConfiguration config;
1237 config.type = PeerConnectionInterface::kRelay;
1238 EXPECT_TRUE(pc_->SetConfiguration(config));
1239
1240 PeerConnectionInterface::RTCConfiguration returned_config =
1241 pc_->GetConfiguration();
1242 EXPECT_EQ(PeerConnectionInterface::kRelay, returned_config.type);
1243}
1244
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001245TEST_F(PeerConnectionInterfaceTest, AddStreams) {
1246 CreatePeerConnection();
deadbeefab9b2d12015-10-14 11:33:11 -07001247 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001248 AddVoiceStream(kStreamLabel2);
1249 ASSERT_EQ(2u, pc_->local_streams()->count());
1250
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001251 // Test we can add multiple local streams to one peerconnection.
zhihuang9763d562016-08-05 11:14:50 -07001252 rtc::scoped_refptr<MediaStreamInterface> stream(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001253 pc_factory_->CreateLocalMediaStream(kStreamLabel3));
zhihuang9763d562016-08-05 11:14:50 -07001254 rtc::scoped_refptr<AudioTrackInterface> audio_track(
1255 pc_factory_->CreateAudioTrack(kStreamLabel3,
1256 static_cast<AudioSourceInterface*>(NULL)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001257 stream->AddTrack(audio_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +00001258 EXPECT_TRUE(pc_->AddStream(stream));
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001259 EXPECT_EQ(3u, pc_->local_streams()->count());
1260
1261 // Remove the third stream.
1262 pc_->RemoveStream(pc_->local_streams()->at(2));
1263 EXPECT_EQ(2u, pc_->local_streams()->count());
1264
1265 // Remove the second stream.
1266 pc_->RemoveStream(pc_->local_streams()->at(1));
1267 EXPECT_EQ(1u, pc_->local_streams()->count());
1268
1269 // Remove the first stream.
1270 pc_->RemoveStream(pc_->local_streams()->at(0));
1271 EXPECT_EQ(0u, pc_->local_streams()->count());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001272}
1273
deadbeefab9b2d12015-10-14 11:33:11 -07001274// Test that the created offer includes streams we added.
1275TEST_F(PeerConnectionInterfaceTest, AddedStreamsPresentInOffer) {
1276 CreatePeerConnection();
1277 AddAudioVideoStream(kStreamLabel1, "audio_track", "video_track");
kwibergd1fe2812016-04-27 06:47:29 -07001278 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001279 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07001280
1281 const cricket::ContentInfo* audio_content =
1282 cricket::GetFirstAudioContent(offer->description());
1283 const cricket::AudioContentDescription* audio_desc =
1284 static_cast<const cricket::AudioContentDescription*>(
1285 audio_content->description);
1286 EXPECT_TRUE(
1287 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
1288
1289 const cricket::ContentInfo* video_content =
1290 cricket::GetFirstVideoContent(offer->description());
1291 const cricket::VideoContentDescription* video_desc =
1292 static_cast<const cricket::VideoContentDescription*>(
1293 video_content->description);
1294 EXPECT_TRUE(
1295 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1296
1297 // Add another stream and ensure the offer includes both the old and new
1298 // streams.
1299 AddAudioVideoStream(kStreamLabel2, "audio_track2", "video_track2");
kwiberg2bbff992016-03-16 11:03:04 -07001300 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07001301
1302 audio_content = cricket::GetFirstAudioContent(offer->description());
1303 audio_desc = static_cast<const cricket::AudioContentDescription*>(
1304 audio_content->description);
1305 EXPECT_TRUE(
1306 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
1307 EXPECT_TRUE(
1308 ContainsTrack(audio_desc->streams(), kStreamLabel2, "audio_track2"));
1309
1310 video_content = cricket::GetFirstVideoContent(offer->description());
1311 video_desc = static_cast<const cricket::VideoContentDescription*>(
1312 video_content->description);
1313 EXPECT_TRUE(
1314 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1315 EXPECT_TRUE(
1316 ContainsTrack(video_desc->streams(), kStreamLabel2, "video_track2"));
1317}
1318
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001319TEST_F(PeerConnectionInterfaceTest, RemoveStream) {
1320 CreatePeerConnection();
deadbeefab9b2d12015-10-14 11:33:11 -07001321 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001322 ASSERT_EQ(1u, pc_->local_streams()->count());
1323 pc_->RemoveStream(pc_->local_streams()->at(0));
1324 EXPECT_EQ(0u, pc_->local_streams()->count());
1325}
1326
deadbeefe1f9d832016-01-14 15:35:42 -08001327// Test for AddTrack and RemoveTrack methods.
1328// Tests that the created offer includes tracks we added,
1329// and that the RtpSenders are created correctly.
1330// Also tests that RemoveTrack removes the tracks from subsequent offers.
1331TEST_F(PeerConnectionInterfaceTest, AddTrackRemoveTrack) {
1332 CreatePeerConnection();
1333 // Create a dummy stream, so tracks share a stream label.
zhihuang9763d562016-08-05 11:14:50 -07001334 rtc::scoped_refptr<MediaStreamInterface> stream(
deadbeefe1f9d832016-01-14 15:35:42 -08001335 pc_factory_->CreateLocalMediaStream(kStreamLabel1));
1336 std::vector<MediaStreamInterface*> stream_list;
1337 stream_list.push_back(stream.get());
zhihuang9763d562016-08-05 11:14:50 -07001338 rtc::scoped_refptr<AudioTrackInterface> audio_track(
deadbeefe1f9d832016-01-14 15:35:42 -08001339 pc_factory_->CreateAudioTrack("audio_track", nullptr));
zhihuang9763d562016-08-05 11:14:50 -07001340 rtc::scoped_refptr<VideoTrackInterface> video_track(
1341 pc_factory_->CreateVideoTrack(
1342 "video_track",
1343 pc_factory_->CreateVideoSource(new cricket::FakeVideoCapturer())));
deadbeefe1f9d832016-01-14 15:35:42 -08001344 auto audio_sender = pc_->AddTrack(audio_track, stream_list);
1345 auto video_sender = pc_->AddTrack(video_track, stream_list);
deadbeefa601f5c2016-06-06 14:27:39 -07001346 EXPECT_EQ(1UL, audio_sender->stream_ids().size());
1347 EXPECT_EQ(kStreamLabel1, audio_sender->stream_ids()[0]);
deadbeefe1f9d832016-01-14 15:35:42 -08001348 EXPECT_EQ("audio_track", audio_sender->id());
1349 EXPECT_EQ(audio_track, audio_sender->track());
deadbeefa601f5c2016-06-06 14:27:39 -07001350 EXPECT_EQ(1UL, video_sender->stream_ids().size());
1351 EXPECT_EQ(kStreamLabel1, video_sender->stream_ids()[0]);
deadbeefe1f9d832016-01-14 15:35:42 -08001352 EXPECT_EQ("video_track", video_sender->id());
1353 EXPECT_EQ(video_track, video_sender->track());
1354
1355 // Now create an offer and check for the senders.
kwibergd1fe2812016-04-27 06:47:29 -07001356 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001357 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefe1f9d832016-01-14 15:35:42 -08001358
1359 const cricket::ContentInfo* audio_content =
1360 cricket::GetFirstAudioContent(offer->description());
1361 const cricket::AudioContentDescription* audio_desc =
1362 static_cast<const cricket::AudioContentDescription*>(
1363 audio_content->description);
1364 EXPECT_TRUE(
1365 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
1366
1367 const cricket::ContentInfo* video_content =
1368 cricket::GetFirstVideoContent(offer->description());
1369 const cricket::VideoContentDescription* video_desc =
1370 static_cast<const cricket::VideoContentDescription*>(
1371 video_content->description);
1372 EXPECT_TRUE(
1373 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1374
1375 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
1376
1377 // Now try removing the tracks.
1378 EXPECT_TRUE(pc_->RemoveTrack(audio_sender));
1379 EXPECT_TRUE(pc_->RemoveTrack(video_sender));
1380
1381 // Create a new offer and ensure it doesn't contain the removed senders.
kwiberg2bbff992016-03-16 11:03:04 -07001382 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefe1f9d832016-01-14 15:35:42 -08001383
1384 audio_content = cricket::GetFirstAudioContent(offer->description());
1385 audio_desc = static_cast<const cricket::AudioContentDescription*>(
1386 audio_content->description);
1387 EXPECT_FALSE(
1388 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
1389
1390 video_content = cricket::GetFirstVideoContent(offer->description());
1391 video_desc = static_cast<const cricket::VideoContentDescription*>(
1392 video_content->description);
1393 EXPECT_FALSE(
1394 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1395
1396 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
1397
1398 // Calling RemoveTrack on a sender no longer attached to a PeerConnection
1399 // should return false.
1400 EXPECT_FALSE(pc_->RemoveTrack(audio_sender));
1401 EXPECT_FALSE(pc_->RemoveTrack(video_sender));
1402}
1403
1404// Test creating senders without a stream specified,
1405// expecting a random stream ID to be generated.
1406TEST_F(PeerConnectionInterfaceTest, AddTrackWithoutStream) {
1407 CreatePeerConnection();
1408 // Create a dummy stream, so tracks share a stream label.
zhihuang9763d562016-08-05 11:14:50 -07001409 rtc::scoped_refptr<AudioTrackInterface> audio_track(
deadbeefe1f9d832016-01-14 15:35:42 -08001410 pc_factory_->CreateAudioTrack("audio_track", nullptr));
zhihuang9763d562016-08-05 11:14:50 -07001411 rtc::scoped_refptr<VideoTrackInterface> video_track(
1412 pc_factory_->CreateVideoTrack(
1413 "video_track",
1414 pc_factory_->CreateVideoSource(new cricket::FakeVideoCapturer())));
deadbeefe1f9d832016-01-14 15:35:42 -08001415 auto audio_sender =
1416 pc_->AddTrack(audio_track, std::vector<MediaStreamInterface*>());
1417 auto video_sender =
1418 pc_->AddTrack(video_track, std::vector<MediaStreamInterface*>());
1419 EXPECT_EQ("audio_track", audio_sender->id());
1420 EXPECT_EQ(audio_track, audio_sender->track());
1421 EXPECT_EQ("video_track", video_sender->id());
1422 EXPECT_EQ(video_track, video_sender->track());
1423 // If the ID is truly a random GUID, it should be infinitely unlikely they
1424 // will be the same.
deadbeefa601f5c2016-06-06 14:27:39 -07001425 EXPECT_NE(video_sender->stream_ids(), audio_sender->stream_ids());
deadbeefe1f9d832016-01-14 15:35:42 -08001426}
1427
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001428TEST_F(PeerConnectionInterfaceTest, CreateOfferReceiveAnswer) {
1429 InitiateCall();
1430 WaitAndVerifyOnAddStream(kStreamLabel1);
1431 VerifyRemoteRtpHeaderExtensions();
1432}
1433
1434TEST_F(PeerConnectionInterfaceTest, CreateOfferReceivePrAnswerAndAnswer) {
1435 CreatePeerConnection();
deadbeefab9b2d12015-10-14 11:33:11 -07001436 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001437 CreateOfferAsLocalDescription();
1438 std::string offer;
1439 EXPECT_TRUE(pc_->local_description()->ToString(&offer));
1440 CreatePrAnswerAndAnswerAsRemoteDescription(offer);
1441 WaitAndVerifyOnAddStream(kStreamLabel1);
1442}
1443
1444TEST_F(PeerConnectionInterfaceTest, ReceiveOfferCreateAnswer) {
1445 CreatePeerConnection();
deadbeefab9b2d12015-10-14 11:33:11 -07001446 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001447
1448 CreateOfferAsRemoteDescription();
1449 CreateAnswerAsLocalDescription();
1450
1451 WaitAndVerifyOnAddStream(kStreamLabel1);
1452}
1453
1454TEST_F(PeerConnectionInterfaceTest, ReceiveOfferCreatePrAnswerAndAnswer) {
1455 CreatePeerConnection();
deadbeefab9b2d12015-10-14 11:33:11 -07001456 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001457
1458 CreateOfferAsRemoteDescription();
1459 CreatePrAnswerAsLocalDescription();
1460 CreateAnswerAsLocalDescription();
1461
1462 WaitAndVerifyOnAddStream(kStreamLabel1);
1463}
1464
1465TEST_F(PeerConnectionInterfaceTest, Renegotiate) {
1466 InitiateCall();
1467 ASSERT_EQ(1u, pc_->remote_streams()->count());
1468 pc_->RemoveStream(pc_->local_streams()->at(0));
1469 CreateOfferReceiveAnswer();
1470 EXPECT_EQ(0u, pc_->remote_streams()->count());
deadbeefab9b2d12015-10-14 11:33:11 -07001471 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001472 CreateOfferReceiveAnswer();
1473}
1474
1475// Tests that after negotiating an audio only call, the respondent can perform a
1476// renegotiation that removes the audio stream.
1477TEST_F(PeerConnectionInterfaceTest, RenegotiateAudioOnly) {
1478 CreatePeerConnection();
1479 AddVoiceStream(kStreamLabel1);
1480 CreateOfferAsRemoteDescription();
1481 CreateAnswerAsLocalDescription();
1482
1483 ASSERT_EQ(1u, pc_->remote_streams()->count());
1484 pc_->RemoveStream(pc_->local_streams()->at(0));
1485 CreateOfferReceiveAnswer();
1486 EXPECT_EQ(0u, pc_->remote_streams()->count());
1487}
1488
1489// Test that candidates are generated and that we can parse our own candidates.
1490TEST_F(PeerConnectionInterfaceTest, IceCandidates) {
1491 CreatePeerConnection();
1492
1493 EXPECT_FALSE(pc_->AddIceCandidate(observer_.last_candidate_.get()));
1494 // SetRemoteDescription takes ownership of offer.
kwibergd1fe2812016-04-27 06:47:29 -07001495 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefab9b2d12015-10-14 11:33:11 -07001496 AddVideoStream(kStreamLabel1);
deadbeefc80741f2015-10-22 13:14:45 -07001497 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
kwiberg2bbff992016-03-16 11:03:04 -07001498 EXPECT_TRUE(DoSetRemoteDescription(offer.release()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001499
1500 // SetLocalDescription takes ownership of answer.
kwibergd1fe2812016-04-27 06:47:29 -07001501 std::unique_ptr<SessionDescriptionInterface> answer;
deadbeefc80741f2015-10-22 13:14:45 -07001502 EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
kwiberg2bbff992016-03-16 11:03:04 -07001503 EXPECT_TRUE(DoSetLocalDescription(answer.release()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001504
1505 EXPECT_TRUE_WAIT(observer_.last_candidate_.get() != NULL, kTimeout);
1506 EXPECT_TRUE_WAIT(observer_.ice_complete_, kTimeout);
1507
1508 EXPECT_TRUE(pc_->AddIceCandidate(observer_.last_candidate_.get()));
1509}
1510
deadbeefab9b2d12015-10-14 11:33:11 -07001511// Test that CreateOffer and CreateAnswer will fail if the track labels are
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001512// not unique.
1513TEST_F(PeerConnectionInterfaceTest, CreateOfferAnswerWithInvalidStream) {
1514 CreatePeerConnection();
1515 // Create a regular offer for the CreateAnswer test later.
kwibergd1fe2812016-04-27 06:47:29 -07001516 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefc80741f2015-10-22 13:14:45 -07001517 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
kwiberg2bbff992016-03-16 11:03:04 -07001518 EXPECT_TRUE(offer);
1519 offer.reset();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001520
1521 // Create a local stream with audio&video tracks having same label.
1522 AddAudioVideoStream(kStreamLabel1, "track_label", "track_label");
1523
1524 // Test CreateOffer
deadbeefc80741f2015-10-22 13:14:45 -07001525 EXPECT_FALSE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001526
1527 // Test CreateAnswer
kwibergd1fe2812016-04-27 06:47:29 -07001528 std::unique_ptr<SessionDescriptionInterface> answer;
deadbeefc80741f2015-10-22 13:14:45 -07001529 EXPECT_FALSE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001530}
1531
1532// Test that we will get different SSRCs for each tracks in the offer and answer
1533// we created.
1534TEST_F(PeerConnectionInterfaceTest, SsrcInOfferAnswer) {
1535 CreatePeerConnection();
1536 // Create a local stream with audio&video tracks having different labels.
1537 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
1538
1539 // Test CreateOffer
kwibergd1fe2812016-04-27 06:47:29 -07001540 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001541 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001542 int audio_ssrc = 0;
1543 int video_ssrc = 0;
1544 EXPECT_TRUE(GetFirstSsrc(GetFirstAudioContent(offer->description()),
1545 &audio_ssrc));
1546 EXPECT_TRUE(GetFirstSsrc(GetFirstVideoContent(offer->description()),
1547 &video_ssrc));
1548 EXPECT_NE(audio_ssrc, video_ssrc);
1549
1550 // Test CreateAnswer
1551 EXPECT_TRUE(DoSetRemoteDescription(offer.release()));
kwibergd1fe2812016-04-27 06:47:29 -07001552 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -07001553 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001554 audio_ssrc = 0;
1555 video_ssrc = 0;
1556 EXPECT_TRUE(GetFirstSsrc(GetFirstAudioContent(answer->description()),
1557 &audio_ssrc));
1558 EXPECT_TRUE(GetFirstSsrc(GetFirstVideoContent(answer->description()),
1559 &video_ssrc));
1560 EXPECT_NE(audio_ssrc, video_ssrc);
1561}
1562
deadbeefeb459812015-12-15 19:24:43 -08001563// Test that it's possible to call AddTrack on a MediaStream after adding
1564// the stream to a PeerConnection.
1565// TODO(deadbeef): Remove this test once this behavior is no longer supported.
1566TEST_F(PeerConnectionInterfaceTest, AddTrackAfterAddStream) {
1567 CreatePeerConnection();
1568 // Create audio stream and add to PeerConnection.
1569 AddVoiceStream(kStreamLabel1);
1570 MediaStreamInterface* stream = pc_->local_streams()->at(0);
1571
1572 // Add video track to the audio-only stream.
zhihuang9763d562016-08-05 11:14:50 -07001573 rtc::scoped_refptr<VideoTrackInterface> video_track(
1574 pc_factory_->CreateVideoTrack(
1575 "video_label",
1576 pc_factory_->CreateVideoSource(new cricket::FakeVideoCapturer())));
deadbeefeb459812015-12-15 19:24:43 -08001577 stream->AddTrack(video_track.get());
1578
kwibergd1fe2812016-04-27 06:47:29 -07001579 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001580 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefeb459812015-12-15 19:24:43 -08001581
1582 const cricket::MediaContentDescription* video_desc =
1583 cricket::GetFirstVideoContentDescription(offer->description());
1584 EXPECT_TRUE(video_desc != nullptr);
1585}
1586
1587// Test that it's possible to call RemoveTrack on a MediaStream after adding
1588// the stream to a PeerConnection.
1589// TODO(deadbeef): Remove this test once this behavior is no longer supported.
1590TEST_F(PeerConnectionInterfaceTest, RemoveTrackAfterAddStream) {
1591 CreatePeerConnection();
1592 // Create audio/video stream and add to PeerConnection.
1593 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
1594 MediaStreamInterface* stream = pc_->local_streams()->at(0);
1595
1596 // Remove the video track.
1597 stream->RemoveTrack(stream->GetVideoTracks()[0]);
1598
kwibergd1fe2812016-04-27 06:47:29 -07001599 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001600 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefeb459812015-12-15 19:24:43 -08001601
1602 const cricket::MediaContentDescription* video_desc =
1603 cricket::GetFirstVideoContentDescription(offer->description());
1604 EXPECT_TRUE(video_desc == nullptr);
1605}
1606
deadbeefbd7d8f72015-12-18 16:58:44 -08001607// Test creating a sender with a stream ID, and ensure the ID is populated
1608// in the offer.
1609TEST_F(PeerConnectionInterfaceTest, CreateSenderWithStream) {
1610 CreatePeerConnection();
1611 pc_->CreateSender("video", kStreamLabel1);
1612
kwibergd1fe2812016-04-27 06:47:29 -07001613 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001614 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefbd7d8f72015-12-18 16:58:44 -08001615
1616 const cricket::MediaContentDescription* video_desc =
1617 cricket::GetFirstVideoContentDescription(offer->description());
1618 ASSERT_TRUE(video_desc != nullptr);
1619 ASSERT_EQ(1u, video_desc->streams().size());
1620 EXPECT_EQ(kStreamLabel1, video_desc->streams()[0].sync_label);
1621}
1622
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001623// Test that we can specify a certain track that we want statistics about.
1624TEST_F(PeerConnectionInterfaceTest, GetStatsForSpecificTrack) {
1625 InitiateCall();
1626 ASSERT_LT(0u, pc_->remote_streams()->count());
1627 ASSERT_LT(0u, pc_->remote_streams()->at(0)->GetAudioTracks().size());
zhihuang9763d562016-08-05 11:14:50 -07001628 rtc::scoped_refptr<MediaStreamTrackInterface> remote_audio =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001629 pc_->remote_streams()->at(0)->GetAudioTracks()[0];
1630 EXPECT_TRUE(DoGetStats(remote_audio));
1631
1632 // Remove the stream. Since we are sending to our selves the local
1633 // and the remote stream is the same.
1634 pc_->RemoveStream(pc_->local_streams()->at(0));
1635 // Do a re-negotiation.
1636 CreateOfferReceiveAnswer();
1637
1638 ASSERT_EQ(0u, pc_->remote_streams()->count());
1639
1640 // Test that we still can get statistics for the old track. Even if it is not
1641 // sent any longer.
1642 EXPECT_TRUE(DoGetStats(remote_audio));
1643}
1644
1645// Test that we can get stats on a video track.
1646TEST_F(PeerConnectionInterfaceTest, GetStatsForVideoTrack) {
1647 InitiateCall();
1648 ASSERT_LT(0u, pc_->remote_streams()->count());
1649 ASSERT_LT(0u, pc_->remote_streams()->at(0)->GetVideoTracks().size());
zhihuang9763d562016-08-05 11:14:50 -07001650 rtc::scoped_refptr<MediaStreamTrackInterface> remote_video =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001651 pc_->remote_streams()->at(0)->GetVideoTracks()[0];
1652 EXPECT_TRUE(DoGetStats(remote_video));
1653}
1654
1655// Test that we don't get statistics for an invalid track.
zhihuange9e94c32016-11-04 11:38:15 -07001656TEST_F(PeerConnectionInterfaceTest, GetStatsForInvalidTrack) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001657 InitiateCall();
zhihuang9763d562016-08-05 11:14:50 -07001658 rtc::scoped_refptr<AudioTrackInterface> unknown_audio_track(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001659 pc_factory_->CreateAudioTrack("unknown track", NULL));
1660 EXPECT_FALSE(DoGetStats(unknown_audio_track));
1661}
1662
1663// This test setup two RTP data channels in loop back.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001664TEST_F(PeerConnectionInterfaceTest, TestDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001665 FakeConstraints constraints;
1666 constraints.SetAllowRtpDataChannels();
1667 CreatePeerConnection(&constraints);
zhihuang9763d562016-08-05 11:14:50 -07001668 rtc::scoped_refptr<DataChannelInterface> data1 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001669 pc_->CreateDataChannel("test1", NULL);
zhihuang9763d562016-08-05 11:14:50 -07001670 rtc::scoped_refptr<DataChannelInterface> data2 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001671 pc_->CreateDataChannel("test2", NULL);
1672 ASSERT_TRUE(data1 != NULL);
kwibergd1fe2812016-04-27 06:47:29 -07001673 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001674 new MockDataChannelObserver(data1));
kwibergd1fe2812016-04-27 06:47:29 -07001675 std::unique_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001676 new MockDataChannelObserver(data2));
1677
1678 EXPECT_EQ(DataChannelInterface::kConnecting, data1->state());
1679 EXPECT_EQ(DataChannelInterface::kConnecting, data2->state());
1680 std::string data_to_send1 = "testing testing";
1681 std::string data_to_send2 = "testing something else";
1682 EXPECT_FALSE(data1->Send(DataBuffer(data_to_send1)));
1683
1684 CreateOfferReceiveAnswer();
1685 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1686 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
1687
1688 EXPECT_EQ(DataChannelInterface::kOpen, data1->state());
1689 EXPECT_EQ(DataChannelInterface::kOpen, data2->state());
1690 EXPECT_TRUE(data1->Send(DataBuffer(data_to_send1)));
1691 EXPECT_TRUE(data2->Send(DataBuffer(data_to_send2)));
1692
1693 EXPECT_EQ_WAIT(data_to_send1, observer1->last_message(), kTimeout);
1694 EXPECT_EQ_WAIT(data_to_send2, observer2->last_message(), kTimeout);
1695
1696 data1->Close();
1697 EXPECT_EQ(DataChannelInterface::kClosing, data1->state());
1698 CreateOfferReceiveAnswer();
1699 EXPECT_FALSE(observer1->IsOpen());
1700 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
1701 EXPECT_TRUE(observer2->IsOpen());
1702
1703 data_to_send2 = "testing something else again";
1704 EXPECT_TRUE(data2->Send(DataBuffer(data_to_send2)));
1705
1706 EXPECT_EQ_WAIT(data_to_send2, observer2->last_message(), kTimeout);
1707}
1708
1709// This test verifies that sendnig binary data over RTP data channels should
1710// fail.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001711TEST_F(PeerConnectionInterfaceTest, TestSendBinaryOnRtpDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001712 FakeConstraints constraints;
1713 constraints.SetAllowRtpDataChannels();
1714 CreatePeerConnection(&constraints);
zhihuang9763d562016-08-05 11:14:50 -07001715 rtc::scoped_refptr<DataChannelInterface> data1 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001716 pc_->CreateDataChannel("test1", NULL);
zhihuang9763d562016-08-05 11:14:50 -07001717 rtc::scoped_refptr<DataChannelInterface> data2 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001718 pc_->CreateDataChannel("test2", NULL);
1719 ASSERT_TRUE(data1 != NULL);
kwibergd1fe2812016-04-27 06:47:29 -07001720 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001721 new MockDataChannelObserver(data1));
kwibergd1fe2812016-04-27 06:47:29 -07001722 std::unique_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001723 new MockDataChannelObserver(data2));
1724
1725 EXPECT_EQ(DataChannelInterface::kConnecting, data1->state());
1726 EXPECT_EQ(DataChannelInterface::kConnecting, data2->state());
1727
1728 CreateOfferReceiveAnswer();
1729 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1730 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
1731
1732 EXPECT_EQ(DataChannelInterface::kOpen, data1->state());
1733 EXPECT_EQ(DataChannelInterface::kOpen, data2->state());
1734
jbaucheec21bd2016-03-20 06:15:43 -07001735 rtc::CopyOnWriteBuffer buffer("test", 4);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001736 EXPECT_FALSE(data1->Send(DataBuffer(buffer, true)));
1737}
1738
1739// This test setup a RTP data channels in loop back and test that a channel is
1740// opened even if the remote end answer with a zero SSRC.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001741TEST_F(PeerConnectionInterfaceTest, TestSendOnlyDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001742 FakeConstraints constraints;
1743 constraints.SetAllowRtpDataChannels();
1744 CreatePeerConnection(&constraints);
zhihuang9763d562016-08-05 11:14:50 -07001745 rtc::scoped_refptr<DataChannelInterface> data1 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001746 pc_->CreateDataChannel("test1", NULL);
kwibergd1fe2812016-04-27 06:47:29 -07001747 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001748 new MockDataChannelObserver(data1));
1749
1750 CreateOfferReceiveAnswerWithoutSsrc();
1751
1752 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1753
1754 data1->Close();
1755 EXPECT_EQ(DataChannelInterface::kClosing, data1->state());
1756 CreateOfferReceiveAnswerWithoutSsrc();
1757 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
1758 EXPECT_FALSE(observer1->IsOpen());
1759}
1760
1761// This test that if a data channel is added in an answer a receive only channel
1762// channel is created.
1763TEST_F(PeerConnectionInterfaceTest, TestReceiveOnlyDataChannel) {
1764 FakeConstraints constraints;
1765 constraints.SetAllowRtpDataChannels();
1766 CreatePeerConnection(&constraints);
1767
1768 std::string offer_label = "offer_channel";
zhihuang9763d562016-08-05 11:14:50 -07001769 rtc::scoped_refptr<DataChannelInterface> offer_channel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001770 pc_->CreateDataChannel(offer_label, NULL);
1771
1772 CreateOfferAsLocalDescription();
1773
1774 // Replace the data channel label in the offer and apply it as an answer.
1775 std::string receive_label = "answer_channel";
1776 std::string sdp;
1777 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001778 rtc::replace_substrs(offer_label.c_str(), offer_label.length(),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001779 receive_label.c_str(), receive_label.length(),
1780 &sdp);
1781 CreateAnswerAsRemoteDescription(sdp);
1782
1783 // Verify that a new incoming data channel has been created and that
1784 // it is open but can't we written to.
1785 ASSERT_TRUE(observer_.last_datachannel_ != NULL);
1786 DataChannelInterface* received_channel = observer_.last_datachannel_;
1787 EXPECT_EQ(DataChannelInterface::kConnecting, received_channel->state());
1788 EXPECT_EQ(receive_label, received_channel->label());
1789 EXPECT_FALSE(received_channel->Send(DataBuffer("something")));
1790
1791 // Verify that the channel we initially offered has been rejected.
1792 EXPECT_EQ(DataChannelInterface::kClosed, offer_channel->state());
1793
1794 // Do another offer / answer exchange and verify that the data channel is
1795 // opened.
1796 CreateOfferReceiveAnswer();
1797 EXPECT_EQ_WAIT(DataChannelInterface::kOpen, received_channel->state(),
1798 kTimeout);
1799}
1800
1801// This test that no data channel is returned if a reliable channel is
1802// requested.
1803// TODO(perkj): Remove this test once reliable channels are implemented.
1804TEST_F(PeerConnectionInterfaceTest, CreateReliableRtpDataChannelShouldFail) {
1805 FakeConstraints constraints;
1806 constraints.SetAllowRtpDataChannels();
1807 CreatePeerConnection(&constraints);
1808
1809 std::string label = "test";
1810 webrtc::DataChannelInit config;
1811 config.reliable = true;
zhihuang9763d562016-08-05 11:14:50 -07001812 rtc::scoped_refptr<DataChannelInterface> channel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001813 pc_->CreateDataChannel(label, &config);
1814 EXPECT_TRUE(channel == NULL);
1815}
1816
deadbeefab9b2d12015-10-14 11:33:11 -07001817// Verifies that duplicated label is not allowed for RTP data channel.
1818TEST_F(PeerConnectionInterfaceTest, RtpDuplicatedLabelNotAllowed) {
1819 FakeConstraints constraints;
1820 constraints.SetAllowRtpDataChannels();
1821 CreatePeerConnection(&constraints);
1822
1823 std::string label = "test";
zhihuang9763d562016-08-05 11:14:50 -07001824 rtc::scoped_refptr<DataChannelInterface> channel =
deadbeefab9b2d12015-10-14 11:33:11 -07001825 pc_->CreateDataChannel(label, nullptr);
1826 EXPECT_NE(channel, nullptr);
1827
zhihuang9763d562016-08-05 11:14:50 -07001828 rtc::scoped_refptr<DataChannelInterface> dup_channel =
deadbeefab9b2d12015-10-14 11:33:11 -07001829 pc_->CreateDataChannel(label, nullptr);
1830 EXPECT_EQ(dup_channel, nullptr);
1831}
1832
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001833// This tests that a SCTP data channel is returned using different
1834// DataChannelInit configurations.
1835TEST_F(PeerConnectionInterfaceTest, CreateSctpDataChannel) {
1836 FakeConstraints constraints;
1837 constraints.SetAllowDtlsSctpDataChannels();
1838 CreatePeerConnection(&constraints);
1839
1840 webrtc::DataChannelInit config;
1841
zhihuang9763d562016-08-05 11:14:50 -07001842 rtc::scoped_refptr<DataChannelInterface> channel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001843 pc_->CreateDataChannel("1", &config);
1844 EXPECT_TRUE(channel != NULL);
1845 EXPECT_TRUE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001846 EXPECT_TRUE(observer_.renegotiation_needed_);
1847 observer_.renegotiation_needed_ = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001848
1849 config.ordered = false;
1850 channel = pc_->CreateDataChannel("2", &config);
1851 EXPECT_TRUE(channel != NULL);
1852 EXPECT_TRUE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001853 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001854
1855 config.ordered = true;
1856 config.maxRetransmits = 0;
1857 channel = pc_->CreateDataChannel("3", &config);
1858 EXPECT_TRUE(channel != NULL);
1859 EXPECT_FALSE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001860 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001861
1862 config.maxRetransmits = -1;
1863 config.maxRetransmitTime = 0;
1864 channel = pc_->CreateDataChannel("4", &config);
1865 EXPECT_TRUE(channel != NULL);
1866 EXPECT_FALSE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001867 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001868}
1869
1870// This tests that no data channel is returned if both maxRetransmits and
1871// maxRetransmitTime are set for SCTP data channels.
1872TEST_F(PeerConnectionInterfaceTest,
1873 CreateSctpDataChannelShouldFailForInvalidConfig) {
1874 FakeConstraints constraints;
1875 constraints.SetAllowDtlsSctpDataChannels();
1876 CreatePeerConnection(&constraints);
1877
1878 std::string label = "test";
1879 webrtc::DataChannelInit config;
1880 config.maxRetransmits = 0;
1881 config.maxRetransmitTime = 0;
1882
zhihuang9763d562016-08-05 11:14:50 -07001883 rtc::scoped_refptr<DataChannelInterface> channel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001884 pc_->CreateDataChannel(label, &config);
1885 EXPECT_TRUE(channel == NULL);
1886}
1887
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001888// The test verifies that creating a SCTP data channel with an id already in use
1889// or out of range should fail.
1890TEST_F(PeerConnectionInterfaceTest,
1891 CreateSctpDataChannelWithInvalidIdShouldFail) {
1892 FakeConstraints constraints;
1893 constraints.SetAllowDtlsSctpDataChannels();
1894 CreatePeerConnection(&constraints);
1895
1896 webrtc::DataChannelInit config;
zhihuang9763d562016-08-05 11:14:50 -07001897 rtc::scoped_refptr<DataChannelInterface> channel;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001898
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001899 config.id = 1;
1900 channel = pc_->CreateDataChannel("1", &config);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001901 EXPECT_TRUE(channel != NULL);
1902 EXPECT_EQ(1, channel->id());
1903
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001904 channel = pc_->CreateDataChannel("x", &config);
1905 EXPECT_TRUE(channel == NULL);
1906
1907 config.id = cricket::kMaxSctpSid;
1908 channel = pc_->CreateDataChannel("max", &config);
1909 EXPECT_TRUE(channel != NULL);
1910 EXPECT_EQ(config.id, channel->id());
1911
1912 config.id = cricket::kMaxSctpSid + 1;
1913 channel = pc_->CreateDataChannel("x", &config);
1914 EXPECT_TRUE(channel == NULL);
1915}
1916
deadbeefab9b2d12015-10-14 11:33:11 -07001917// Verifies that duplicated label is allowed for SCTP data channel.
1918TEST_F(PeerConnectionInterfaceTest, SctpDuplicatedLabelAllowed) {
1919 FakeConstraints constraints;
1920 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
1921 true);
1922 CreatePeerConnection(&constraints);
1923
1924 std::string label = "test";
zhihuang9763d562016-08-05 11:14:50 -07001925 rtc::scoped_refptr<DataChannelInterface> channel =
deadbeefab9b2d12015-10-14 11:33:11 -07001926 pc_->CreateDataChannel(label, nullptr);
1927 EXPECT_NE(channel, nullptr);
1928
zhihuang9763d562016-08-05 11:14:50 -07001929 rtc::scoped_refptr<DataChannelInterface> dup_channel =
deadbeefab9b2d12015-10-14 11:33:11 -07001930 pc_->CreateDataChannel(label, nullptr);
1931 EXPECT_NE(dup_channel, nullptr);
1932}
1933
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001934// This test verifies that OnRenegotiationNeeded is fired for every new RTP
1935// DataChannel.
1936TEST_F(PeerConnectionInterfaceTest, RenegotiationNeededForNewRtpDataChannel) {
1937 FakeConstraints constraints;
1938 constraints.SetAllowRtpDataChannels();
1939 CreatePeerConnection(&constraints);
1940
zhihuang9763d562016-08-05 11:14:50 -07001941 rtc::scoped_refptr<DataChannelInterface> dc1 =
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001942 pc_->CreateDataChannel("test1", NULL);
1943 EXPECT_TRUE(observer_.renegotiation_needed_);
1944 observer_.renegotiation_needed_ = false;
1945
zhihuang9763d562016-08-05 11:14:50 -07001946 rtc::scoped_refptr<DataChannelInterface> dc2 =
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001947 pc_->CreateDataChannel("test2", NULL);
1948 EXPECT_TRUE(observer_.renegotiation_needed_);
1949}
1950
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001951// This test that a data channel closes when a PeerConnection is deleted/closed.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001952TEST_F(PeerConnectionInterfaceTest, DataChannelCloseWhenPeerConnectionClose) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001953 FakeConstraints constraints;
1954 constraints.SetAllowRtpDataChannels();
1955 CreatePeerConnection(&constraints);
1956
zhihuang9763d562016-08-05 11:14:50 -07001957 rtc::scoped_refptr<DataChannelInterface> data1 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001958 pc_->CreateDataChannel("test1", NULL);
zhihuang9763d562016-08-05 11:14:50 -07001959 rtc::scoped_refptr<DataChannelInterface> data2 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001960 pc_->CreateDataChannel("test2", NULL);
1961 ASSERT_TRUE(data1 != NULL);
kwibergd1fe2812016-04-27 06:47:29 -07001962 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001963 new MockDataChannelObserver(data1));
kwibergd1fe2812016-04-27 06:47:29 -07001964 std::unique_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001965 new MockDataChannelObserver(data2));
1966
1967 CreateOfferReceiveAnswer();
1968 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1969 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
1970
1971 ReleasePeerConnection();
1972 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
1973 EXPECT_EQ(DataChannelInterface::kClosed, data2->state());
1974}
1975
1976// This test that data channels can be rejected in an answer.
1977TEST_F(PeerConnectionInterfaceTest, TestRejectDataChannelInAnswer) {
1978 FakeConstraints constraints;
1979 constraints.SetAllowRtpDataChannels();
1980 CreatePeerConnection(&constraints);
1981
zhihuang9763d562016-08-05 11:14:50 -07001982 rtc::scoped_refptr<DataChannelInterface> offer_channel(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001983 pc_->CreateDataChannel("offer_channel", NULL));
1984
1985 CreateOfferAsLocalDescription();
1986
1987 // Create an answer where the m-line for data channels are rejected.
1988 std::string sdp;
1989 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
1990 webrtc::JsepSessionDescription* answer = new webrtc::JsepSessionDescription(
1991 SessionDescriptionInterface::kAnswer);
1992 EXPECT_TRUE(answer->Initialize(sdp, NULL));
1993 cricket::ContentInfo* data_info =
1994 answer->description()->GetContentByName("data");
1995 data_info->rejected = true;
1996
1997 DoSetRemoteDescription(answer);
1998 EXPECT_EQ(DataChannelInterface::kClosed, offer_channel->state());
1999}
2000
2001// Test that we can create a session description from an SDP string from
2002// FireFox, use it as a remote session description, generate an answer and use
2003// the answer as a local description.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002004TEST_F(PeerConnectionInterfaceTest, ReceiveFireFoxOffer) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002005 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002006 FakeConstraints constraints;
2007 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2008 true);
2009 CreatePeerConnection(&constraints);
2010 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
2011 SessionDescriptionInterface* desc =
2012 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
jbauchfabe2c92015-07-16 13:43:14 -07002013 webrtc::kFireFoxSdpOffer, nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002014 EXPECT_TRUE(DoSetSessionDescription(desc, false));
2015 CreateAnswerAsLocalDescription();
2016 ASSERT_TRUE(pc_->local_description() != NULL);
2017 ASSERT_TRUE(pc_->remote_description() != NULL);
2018
2019 const cricket::ContentInfo* content =
2020 cricket::GetFirstAudioContent(pc_->local_description()->description());
2021 ASSERT_TRUE(content != NULL);
2022 EXPECT_FALSE(content->rejected);
2023
2024 content =
2025 cricket::GetFirstVideoContent(pc_->local_description()->description());
2026 ASSERT_TRUE(content != NULL);
2027 EXPECT_FALSE(content->rejected);
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00002028#ifdef HAVE_SCTP
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002029 content =
2030 cricket::GetFirstDataContent(pc_->local_description()->description());
2031 ASSERT_TRUE(content != NULL);
2032 EXPECT_TRUE(content->rejected);
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00002033#endif
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002034}
2035
2036// Test that we can create an audio only offer and receive an answer with a
2037// limited set of audio codecs and receive an updated offer with more audio
2038// codecs, where the added codecs are not supported.
2039TEST_F(PeerConnectionInterfaceTest, ReceiveUpdatedAudioOfferWithBadCodecs) {
2040 CreatePeerConnection();
2041 AddVoiceStream("audio_label");
2042 CreateOfferAsLocalDescription();
2043
2044 SessionDescriptionInterface* answer =
2045 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
jbauchfabe2c92015-07-16 13:43:14 -07002046 webrtc::kAudioSdp, nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002047 EXPECT_TRUE(DoSetSessionDescription(answer, false));
2048
2049 SessionDescriptionInterface* updated_offer =
2050 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
jbauchfabe2c92015-07-16 13:43:14 -07002051 webrtc::kAudioSdpWithUnsupportedCodecs,
2052 nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002053 EXPECT_TRUE(DoSetSessionDescription(updated_offer, false));
2054 CreateAnswerAsLocalDescription();
2055}
2056
deadbeefc80741f2015-10-22 13:14:45 -07002057// Test that if we're receiving (but not sending) a track, subsequent offers
2058// will have m-lines with a=recvonly.
2059TEST_F(PeerConnectionInterfaceTest, CreateSubsequentRecvOnlyOffer) {
2060 FakeConstraints constraints;
2061 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2062 true);
2063 CreatePeerConnection(&constraints);
2064 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2065 CreateAnswerAsLocalDescription();
2066
2067 // At this point we should be receiving stream 1, but not sending anything.
2068 // A new offer should be recvonly.
kwibergd1fe2812016-04-27 06:47:29 -07002069 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefc80741f2015-10-22 13:14:45 -07002070 DoCreateOffer(&offer, nullptr);
2071
2072 const cricket::ContentInfo* video_content =
2073 cricket::GetFirstVideoContent(offer->description());
2074 const cricket::VideoContentDescription* video_desc =
2075 static_cast<const cricket::VideoContentDescription*>(
2076 video_content->description);
2077 ASSERT_EQ(cricket::MD_RECVONLY, video_desc->direction());
2078
2079 const cricket::ContentInfo* audio_content =
2080 cricket::GetFirstAudioContent(offer->description());
2081 const cricket::AudioContentDescription* audio_desc =
2082 static_cast<const cricket::AudioContentDescription*>(
2083 audio_content->description);
2084 ASSERT_EQ(cricket::MD_RECVONLY, audio_desc->direction());
2085}
2086
2087// Test that if we're receiving (but not sending) a track, and the
2088// offerToReceiveVideo/offerToReceiveAudio constraints are explicitly set to
2089// false, the generated m-lines will be a=inactive.
2090TEST_F(PeerConnectionInterfaceTest, CreateSubsequentInactiveOffer) {
2091 FakeConstraints constraints;
2092 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2093 true);
2094 CreatePeerConnection(&constraints);
2095 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2096 CreateAnswerAsLocalDescription();
2097
2098 // At this point we should be receiving stream 1, but not sending anything.
2099 // A new offer would be recvonly, but we'll set the "no receive" constraints
2100 // to make it inactive.
kwibergd1fe2812016-04-27 06:47:29 -07002101 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefc80741f2015-10-22 13:14:45 -07002102 FakeConstraints offer_constraints;
2103 offer_constraints.AddMandatory(
2104 webrtc::MediaConstraintsInterface::kOfferToReceiveVideo, false);
2105 offer_constraints.AddMandatory(
2106 webrtc::MediaConstraintsInterface::kOfferToReceiveAudio, false);
2107 DoCreateOffer(&offer, &offer_constraints);
2108
2109 const cricket::ContentInfo* video_content =
2110 cricket::GetFirstVideoContent(offer->description());
2111 const cricket::VideoContentDescription* video_desc =
2112 static_cast<const cricket::VideoContentDescription*>(
2113 video_content->description);
2114 ASSERT_EQ(cricket::MD_INACTIVE, video_desc->direction());
2115
2116 const cricket::ContentInfo* audio_content =
2117 cricket::GetFirstAudioContent(offer->description());
2118 const cricket::AudioContentDescription* audio_desc =
2119 static_cast<const cricket::AudioContentDescription*>(
2120 audio_content->description);
2121 ASSERT_EQ(cricket::MD_INACTIVE, audio_desc->direction());
2122}
2123
deadbeef653b8e02015-11-11 12:55:10 -08002124// Test that we can use SetConfiguration to change the ICE servers of the
2125// PortAllocator.
2126TEST_F(PeerConnectionInterfaceTest, SetConfigurationChangesIceServers) {
2127 CreatePeerConnection();
2128
2129 PeerConnectionInterface::RTCConfiguration config;
2130 PeerConnectionInterface::IceServer server;
2131 server.uri = "stun:test_hostname";
2132 config.servers.push_back(server);
2133 EXPECT_TRUE(pc_->SetConfiguration(config));
2134
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002135 EXPECT_EQ(1u, port_allocator_->stun_servers().size());
2136 EXPECT_EQ("test_hostname",
2137 port_allocator_->stun_servers().begin()->hostname());
deadbeef653b8e02015-11-11 12:55:10 -08002138}
2139
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002140TEST_F(PeerConnectionInterfaceTest, SetConfigurationChangesCandidateFilter) {
2141 CreatePeerConnection();
2142 PeerConnectionInterface::RTCConfiguration config;
2143 config.type = PeerConnectionInterface::kRelay;
2144 EXPECT_TRUE(pc_->SetConfiguration(config));
2145 EXPECT_EQ(cricket::CF_RELAY, port_allocator_->candidate_filter());
2146}
2147
2148// Test that when SetConfiguration changes both the pool size and other
2149// attributes, the pooled session is created with the updated attributes.
2150TEST_F(PeerConnectionInterfaceTest,
2151 SetConfigurationCreatesPooledSessionCorrectly) {
2152 CreatePeerConnection();
2153 PeerConnectionInterface::RTCConfiguration config;
2154 config.ice_candidate_pool_size = 1;
2155 PeerConnectionInterface::IceServer server;
2156 server.uri = kStunAddressOnly;
2157 config.servers.push_back(server);
2158 config.type = PeerConnectionInterface::kRelay;
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07002159 EXPECT_TRUE(pc_->SetConfiguration(config));
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002160
2161 const cricket::FakePortAllocatorSession* session =
2162 static_cast<const cricket::FakePortAllocatorSession*>(
2163 port_allocator_->GetPooledSession());
2164 ASSERT_NE(nullptr, session);
2165 EXPECT_EQ(1UL, session->stun_servers().size());
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002166}
2167
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002168// Test that PeerConnection::Close changes the states to closed and all remote
2169// tracks change state to ended.
2170TEST_F(PeerConnectionInterfaceTest, CloseAndTestStreamsAndStates) {
2171 // Initialize a PeerConnection and negotiate local and remote session
2172 // description.
2173 InitiateCall();
2174 ASSERT_EQ(1u, pc_->local_streams()->count());
2175 ASSERT_EQ(1u, pc_->remote_streams()->count());
2176
2177 pc_->Close();
2178
2179 EXPECT_EQ(PeerConnectionInterface::kClosed, pc_->signaling_state());
2180 EXPECT_EQ(PeerConnectionInterface::kIceConnectionClosed,
2181 pc_->ice_connection_state());
2182 EXPECT_EQ(PeerConnectionInterface::kIceGatheringComplete,
2183 pc_->ice_gathering_state());
2184
2185 EXPECT_EQ(1u, pc_->local_streams()->count());
2186 EXPECT_EQ(1u, pc_->remote_streams()->count());
2187
zhihuang9763d562016-08-05 11:14:50 -07002188 rtc::scoped_refptr<MediaStreamInterface> remote_stream =
2189 pc_->remote_streams()->at(0);
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002190 // Track state may be updated asynchronously.
perkjd61bf802016-03-24 03:16:19 -07002191 EXPECT_EQ_WAIT(MediaStreamTrackInterface::kEnded,
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002192 remote_stream->GetAudioTracks()[0]->state(), kTimeout);
2193 EXPECT_EQ_WAIT(MediaStreamTrackInterface::kEnded,
2194 remote_stream->GetVideoTracks()[0]->state(), kTimeout);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002195}
2196
2197// Test that PeerConnection methods fails gracefully after
2198// PeerConnection::Close has been called.
2199TEST_F(PeerConnectionInterfaceTest, CloseAndTestMethods) {
2200 CreatePeerConnection();
2201 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
2202 CreateOfferAsRemoteDescription();
2203 CreateAnswerAsLocalDescription();
2204
2205 ASSERT_EQ(1u, pc_->local_streams()->count());
zhihuang9763d562016-08-05 11:14:50 -07002206 rtc::scoped_refptr<MediaStreamInterface> local_stream =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002207 pc_->local_streams()->at(0);
2208
2209 pc_->Close();
2210
2211 pc_->RemoveStream(local_stream);
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +00002212 EXPECT_FALSE(pc_->AddStream(local_stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002213
2214 ASSERT_FALSE(local_stream->GetAudioTracks().empty());
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002215 rtc::scoped_refptr<webrtc::DtmfSenderInterface> dtmf_sender(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002216 pc_->CreateDtmfSender(local_stream->GetAudioTracks()[0]));
wu@webrtc.org66037362013-08-13 00:09:35 +00002217 EXPECT_TRUE(NULL == dtmf_sender); // local stream has been removed.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002218
2219 EXPECT_TRUE(pc_->CreateDataChannel("test", NULL) == NULL);
2220
2221 EXPECT_TRUE(pc_->local_description() != NULL);
2222 EXPECT_TRUE(pc_->remote_description() != NULL);
2223
kwibergd1fe2812016-04-27 06:47:29 -07002224 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07002225 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
kwibergd1fe2812016-04-27 06:47:29 -07002226 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -07002227 EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002228
2229 std::string sdp;
2230 ASSERT_TRUE(pc_->remote_description()->ToString(&sdp));
2231 SessionDescriptionInterface* remote_offer =
2232 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
2233 sdp, NULL);
2234 EXPECT_FALSE(DoSetRemoteDescription(remote_offer));
2235
2236 ASSERT_TRUE(pc_->local_description()->ToString(&sdp));
2237 SessionDescriptionInterface* local_offer =
2238 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
2239 sdp, NULL);
2240 EXPECT_FALSE(DoSetLocalDescription(local_offer));
2241}
2242
2243// Test that GetStats can still be called after PeerConnection::Close.
2244TEST_F(PeerConnectionInterfaceTest, CloseAndGetStats) {
2245 InitiateCall();
2246 pc_->Close();
2247 DoGetStats(NULL);
2248}
deadbeefab9b2d12015-10-14 11:33:11 -07002249
2250// NOTE: The series of tests below come from what used to be
2251// mediastreamsignaling_unittest.cc, and are mostly aimed at testing that
2252// setting a remote or local description has the expected effects.
2253
2254// This test verifies that the remote MediaStreams corresponding to a received
2255// SDP string is created. In this test the two separate MediaStreams are
2256// signaled.
2257TEST_F(PeerConnectionInterfaceTest, UpdateRemoteStreams) {
2258 FakeConstraints constraints;
2259 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2260 true);
2261 CreatePeerConnection(&constraints);
2262 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2263
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002264 rtc::scoped_refptr<StreamCollection> reference(CreateStreamCollection(1, 1));
deadbeefab9b2d12015-10-14 11:33:11 -07002265 EXPECT_TRUE(
2266 CompareStreamCollections(observer_.remote_streams(), reference.get()));
2267 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2268 EXPECT_TRUE(remote_stream->GetVideoTracks()[0]->GetSource() != nullptr);
2269
2270 // Create a session description based on another SDP with another
2271 // MediaStream.
2272 CreateAndSetRemoteOffer(kSdpStringWithStream1And2);
2273
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002274 rtc::scoped_refptr<StreamCollection> reference2(CreateStreamCollection(2, 1));
deadbeefab9b2d12015-10-14 11:33:11 -07002275 EXPECT_TRUE(
2276 CompareStreamCollections(observer_.remote_streams(), reference2.get()));
2277}
2278
2279// This test verifies that when remote tracks are added/removed from SDP, the
2280// created remote streams are updated appropriately.
2281TEST_F(PeerConnectionInterfaceTest,
2282 AddRemoveTrackFromExistingRemoteMediaStream) {
2283 FakeConstraints constraints;
2284 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2285 true);
2286 CreatePeerConnection(&constraints);
kwibergd1fe2812016-04-27 06:47:29 -07002287 std::unique_ptr<SessionDescriptionInterface> desc_ms1 =
kwiberg2bbff992016-03-16 11:03:04 -07002288 CreateSessionDescriptionAndReference(1, 1);
deadbeefab9b2d12015-10-14 11:33:11 -07002289 EXPECT_TRUE(DoSetRemoteDescription(desc_ms1.release()));
2290 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
2291 reference_collection_));
2292
2293 // Add extra audio and video tracks to the same MediaStream.
kwibergd1fe2812016-04-27 06:47:29 -07002294 std::unique_ptr<SessionDescriptionInterface> desc_ms1_two_tracks =
kwiberg2bbff992016-03-16 11:03:04 -07002295 CreateSessionDescriptionAndReference(2, 2);
deadbeefab9b2d12015-10-14 11:33:11 -07002296 EXPECT_TRUE(DoSetRemoteDescription(desc_ms1_two_tracks.release()));
2297 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
2298 reference_collection_));
zhihuang9763d562016-08-05 11:14:50 -07002299 rtc::scoped_refptr<AudioTrackInterface> audio_track2 =
perkjd61bf802016-03-24 03:16:19 -07002300 observer_.remote_streams()->at(0)->GetAudioTracks()[1];
2301 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, audio_track2->state());
zhihuang9763d562016-08-05 11:14:50 -07002302 rtc::scoped_refptr<VideoTrackInterface> video_track2 =
perkjd61bf802016-03-24 03:16:19 -07002303 observer_.remote_streams()->at(0)->GetVideoTracks()[1];
2304 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, video_track2->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002305
2306 // Remove the extra audio and video tracks.
kwibergd1fe2812016-04-27 06:47:29 -07002307 std::unique_ptr<SessionDescriptionInterface> desc_ms2 =
kwiberg2bbff992016-03-16 11:03:04 -07002308 CreateSessionDescriptionAndReference(1, 1);
perkjd61bf802016-03-24 03:16:19 -07002309 MockTrackObserver audio_track_observer(audio_track2);
2310 MockTrackObserver video_track_observer(video_track2);
2311
2312 EXPECT_CALL(audio_track_observer, OnChanged()).Times(Exactly(1));
2313 EXPECT_CALL(video_track_observer, OnChanged()).Times(Exactly(1));
deadbeefab9b2d12015-10-14 11:33:11 -07002314 EXPECT_TRUE(DoSetRemoteDescription(desc_ms2.release()));
2315 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
2316 reference_collection_));
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002317 // Track state may be updated asynchronously.
perkjd61bf802016-03-24 03:16:19 -07002318 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002319 audio_track2->state(), kTimeout);
2320 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
2321 video_track2->state(), kTimeout);
deadbeefab9b2d12015-10-14 11:33:11 -07002322}
2323
2324// This tests that remote tracks are ended if a local session description is set
2325// that rejects the media content type.
2326TEST_F(PeerConnectionInterfaceTest, RejectMediaContent) {
2327 FakeConstraints constraints;
2328 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2329 true);
2330 CreatePeerConnection(&constraints);
2331 // First create and set a remote offer, then reject its video content in our
2332 // answer.
2333 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2334 ASSERT_EQ(1u, observer_.remote_streams()->count());
2335 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2336 ASSERT_EQ(1u, remote_stream->GetVideoTracks().size());
2337 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2338
2339 rtc::scoped_refptr<webrtc::VideoTrackInterface> remote_video =
2340 remote_stream->GetVideoTracks()[0];
2341 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, remote_video->state());
2342 rtc::scoped_refptr<webrtc::AudioTrackInterface> remote_audio =
2343 remote_stream->GetAudioTracks()[0];
2344 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, remote_audio->state());
2345
kwibergd1fe2812016-04-27 06:47:29 -07002346 std::unique_ptr<SessionDescriptionInterface> local_answer;
kwiberg2bbff992016-03-16 11:03:04 -07002347 EXPECT_TRUE(DoCreateAnswer(&local_answer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07002348 cricket::ContentInfo* video_info =
2349 local_answer->description()->GetContentByName("video");
2350 video_info->rejected = true;
2351 EXPECT_TRUE(DoSetLocalDescription(local_answer.release()));
2352 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kEnded, remote_video->state());
2353 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, remote_audio->state());
2354
2355 // Now create an offer where we reject both video and audio.
kwibergd1fe2812016-04-27 06:47:29 -07002356 std::unique_ptr<SessionDescriptionInterface> local_offer;
kwiberg2bbff992016-03-16 11:03:04 -07002357 EXPECT_TRUE(DoCreateOffer(&local_offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07002358 video_info = local_offer->description()->GetContentByName("video");
2359 ASSERT_TRUE(video_info != nullptr);
2360 video_info->rejected = true;
2361 cricket::ContentInfo* audio_info =
2362 local_offer->description()->GetContentByName("audio");
2363 ASSERT_TRUE(audio_info != nullptr);
2364 audio_info->rejected = true;
2365 EXPECT_TRUE(DoSetLocalDescription(local_offer.release()));
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002366 // Track state may be updated asynchronously.
perkjd61bf802016-03-24 03:16:19 -07002367 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002368 remote_audio->state(), kTimeout);
2369 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
2370 remote_video->state(), kTimeout);
deadbeefab9b2d12015-10-14 11:33:11 -07002371}
2372
2373// This tests that we won't crash if the remote track has been removed outside
2374// of PeerConnection and then PeerConnection tries to reject the track.
2375TEST_F(PeerConnectionInterfaceTest, RemoveTrackThenRejectMediaContent) {
2376 FakeConstraints constraints;
2377 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2378 true);
2379 CreatePeerConnection(&constraints);
2380 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2381 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2382 remote_stream->RemoveTrack(remote_stream->GetVideoTracks()[0]);
2383 remote_stream->RemoveTrack(remote_stream->GetAudioTracks()[0]);
2384
kwibergd1fe2812016-04-27 06:47:29 -07002385 std::unique_ptr<SessionDescriptionInterface> local_answer(
deadbeefab9b2d12015-10-14 11:33:11 -07002386 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
2387 kSdpStringWithStream1, nullptr));
2388 cricket::ContentInfo* video_info =
2389 local_answer->description()->GetContentByName("video");
2390 video_info->rejected = true;
2391 cricket::ContentInfo* audio_info =
2392 local_answer->description()->GetContentByName("audio");
2393 audio_info->rejected = true;
2394 EXPECT_TRUE(DoSetLocalDescription(local_answer.release()));
2395
2396 // No crash is a pass.
2397}
2398
deadbeef5e97fb52015-10-15 12:49:08 -07002399// This tests that if a recvonly remote description is set, no remote streams
2400// will be created, even if the description contains SSRCs/MSIDs.
2401// See: https://code.google.com/p/webrtc/issues/detail?id=5054
2402TEST_F(PeerConnectionInterfaceTest, RecvonlyDescriptionDoesntCreateStream) {
2403 FakeConstraints constraints;
2404 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2405 true);
2406 CreatePeerConnection(&constraints);
2407
2408 std::string recvonly_offer = kSdpStringWithStream1;
2409 rtc::replace_substrs(kSendrecv, strlen(kSendrecv), kRecvonly,
2410 strlen(kRecvonly), &recvonly_offer);
2411 CreateAndSetRemoteOffer(recvonly_offer);
2412
2413 EXPECT_EQ(0u, observer_.remote_streams()->count());
2414}
2415
deadbeefab9b2d12015-10-14 11:33:11 -07002416// This tests that a default MediaStream is created if a remote session
2417// description doesn't contain any streams and no MSID support.
2418// It also tests that the default stream is updated if a video m-line is added
2419// in a subsequent session description.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002420TEST_F(PeerConnectionInterfaceTest, SdpWithoutMsidCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002421 FakeConstraints constraints;
2422 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2423 true);
2424 CreatePeerConnection(&constraints);
2425 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2426
2427 ASSERT_EQ(1u, observer_.remote_streams()->count());
2428 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2429
2430 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2431 EXPECT_EQ(0u, remote_stream->GetVideoTracks().size());
2432 EXPECT_EQ("default", remote_stream->label());
2433
2434 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2435 ASSERT_EQ(1u, observer_.remote_streams()->count());
2436 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2437 EXPECT_EQ("defaulta0", remote_stream->GetAudioTracks()[0]->id());
deadbeef884f5852016-01-15 09:20:04 -08002438 EXPECT_EQ(MediaStreamTrackInterface::kLive,
2439 remote_stream->GetAudioTracks()[0]->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002440 ASSERT_EQ(1u, remote_stream->GetVideoTracks().size());
2441 EXPECT_EQ("defaultv0", remote_stream->GetVideoTracks()[0]->id());
deadbeef884f5852016-01-15 09:20:04 -08002442 EXPECT_EQ(MediaStreamTrackInterface::kLive,
2443 remote_stream->GetVideoTracks()[0]->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002444}
2445
2446// This tests that a default MediaStream is created if a remote session
2447// description doesn't contain any streams and media direction is send only.
2448TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002449 SendOnlySdpWithoutMsidCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002450 FakeConstraints constraints;
2451 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2452 true);
2453 CreatePeerConnection(&constraints);
2454 CreateAndSetRemoteOffer(kSdpStringSendOnlyWithoutStreams);
2455
2456 ASSERT_EQ(1u, observer_.remote_streams()->count());
2457 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2458
2459 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2460 EXPECT_EQ(1u, remote_stream->GetVideoTracks().size());
2461 EXPECT_EQ("default", remote_stream->label());
2462}
2463
2464// This tests that it won't crash when PeerConnection tries to remove
2465// a remote track that as already been removed from the MediaStream.
2466TEST_F(PeerConnectionInterfaceTest, RemoveAlreadyGoneRemoteStream) {
2467 FakeConstraints constraints;
2468 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2469 true);
2470 CreatePeerConnection(&constraints);
2471 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2472 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2473 remote_stream->RemoveTrack(remote_stream->GetAudioTracks()[0]);
2474 remote_stream->RemoveTrack(remote_stream->GetVideoTracks()[0]);
2475
2476 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2477
2478 // No crash is a pass.
2479}
2480
2481// This tests that a default MediaStream is created if the remote session
2482// description doesn't contain any streams and don't contain an indication if
2483// MSID is supported.
2484TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002485 SdpWithoutMsidAndStreamsCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002486 FakeConstraints constraints;
2487 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2488 true);
2489 CreatePeerConnection(&constraints);
2490 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2491
2492 ASSERT_EQ(1u, observer_.remote_streams()->count());
2493 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2494 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2495 EXPECT_EQ(1u, remote_stream->GetVideoTracks().size());
2496}
2497
2498// This tests that a default MediaStream is not created if the remote session
2499// description doesn't contain any streams but does support MSID.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002500TEST_F(PeerConnectionInterfaceTest, SdpWithMsidDontCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002501 FakeConstraints constraints;
2502 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2503 true);
2504 CreatePeerConnection(&constraints);
2505 CreateAndSetRemoteOffer(kSdpStringWithMsidWithoutStreams);
2506 EXPECT_EQ(0u, observer_.remote_streams()->count());
2507}
2508
deadbeefbda7e0b2015-12-08 17:13:40 -08002509// This tests that when setting a new description, the old default tracks are
2510// not destroyed and recreated.
2511// See: https://bugs.chromium.org/p/webrtc/issues/detail?id=5250
Stefan Holmer102362b2016-03-18 09:39:07 +01002512TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002513 DefaultTracksNotDestroyedAndRecreated) {
deadbeefbda7e0b2015-12-08 17:13:40 -08002514 FakeConstraints constraints;
2515 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2516 true);
2517 CreatePeerConnection(&constraints);
2518 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2519
2520 ASSERT_EQ(1u, observer_.remote_streams()->count());
2521 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2522 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2523
2524 // Set the track to "disabled", then set a new description and ensure the
2525 // track is still disabled, which ensures it hasn't been recreated.
2526 remote_stream->GetAudioTracks()[0]->set_enabled(false);
2527 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2528 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2529 EXPECT_FALSE(remote_stream->GetAudioTracks()[0]->enabled());
2530}
2531
deadbeefab9b2d12015-10-14 11:33:11 -07002532// This tests that a default MediaStream is not created if a remote session
2533// description is updated to not have any MediaStreams.
2534TEST_F(PeerConnectionInterfaceTest, VerifyDefaultStreamIsNotCreated) {
2535 FakeConstraints constraints;
2536 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2537 true);
2538 CreatePeerConnection(&constraints);
2539 CreateAndSetRemoteOffer(kSdpStringWithStream1);
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002540 rtc::scoped_refptr<StreamCollection> reference(CreateStreamCollection(1, 1));
deadbeefab9b2d12015-10-14 11:33:11 -07002541 EXPECT_TRUE(
2542 CompareStreamCollections(observer_.remote_streams(), reference.get()));
2543
2544 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2545 EXPECT_EQ(0u, observer_.remote_streams()->count());
2546}
2547
2548// This tests that an RtpSender is created when the local description is set
2549// after adding a local stream.
2550// TODO(deadbeef): This test and the one below it need to be updated when
2551// an RtpSender's lifetime isn't determined by when a local description is set.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002552TEST_F(PeerConnectionInterfaceTest, LocalDescriptionChanged) {
deadbeefab9b2d12015-10-14 11:33:11 -07002553 FakeConstraints constraints;
2554 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2555 true);
2556 CreatePeerConnection(&constraints);
deadbeefab9b2d12015-10-14 11:33:11 -07002557
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002558 // Create an offer with 1 stream with 2 tracks of each type.
2559 rtc::scoped_refptr<StreamCollection> stream_collection =
2560 CreateStreamCollection(1, 2);
2561 pc_->AddStream(stream_collection->at(0));
2562 std::unique_ptr<SessionDescriptionInterface> offer;
2563 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2564 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
deadbeefab9b2d12015-10-14 11:33:11 -07002565
deadbeefab9b2d12015-10-14 11:33:11 -07002566 auto senders = pc_->GetSenders();
2567 EXPECT_EQ(4u, senders.size());
2568 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2569 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2570 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[1]));
2571 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[1]));
2572
2573 // Remove an audio and video track.
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002574 pc_->RemoveStream(stream_collection->at(0));
2575 stream_collection = CreateStreamCollection(1, 1);
2576 pc_->AddStream(stream_collection->at(0));
2577 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2578 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
2579
deadbeefab9b2d12015-10-14 11:33:11 -07002580 senders = pc_->GetSenders();
2581 EXPECT_EQ(2u, senders.size());
2582 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2583 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2584 EXPECT_FALSE(ContainsSender(senders, kAudioTracks[1]));
2585 EXPECT_FALSE(ContainsSender(senders, kVideoTracks[1]));
2586}
2587
2588// This tests that an RtpSender is created when the local description is set
2589// before adding a local stream.
2590TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002591 AddLocalStreamAfterLocalDescriptionChanged) {
deadbeefab9b2d12015-10-14 11:33:11 -07002592 FakeConstraints constraints;
2593 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2594 true);
2595 CreatePeerConnection(&constraints);
deadbeefab9b2d12015-10-14 11:33:11 -07002596
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002597 rtc::scoped_refptr<StreamCollection> stream_collection =
2598 CreateStreamCollection(1, 2);
2599 // Add a stream to create the offer, but remove it afterwards.
2600 pc_->AddStream(stream_collection->at(0));
2601 std::unique_ptr<SessionDescriptionInterface> offer;
2602 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2603 pc_->RemoveStream(stream_collection->at(0));
deadbeefab9b2d12015-10-14 11:33:11 -07002604
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002605 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
deadbeefab9b2d12015-10-14 11:33:11 -07002606 auto senders = pc_->GetSenders();
2607 EXPECT_EQ(0u, senders.size());
2608
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002609 pc_->AddStream(stream_collection->at(0));
deadbeefab9b2d12015-10-14 11:33:11 -07002610 senders = pc_->GetSenders();
2611 EXPECT_EQ(4u, senders.size());
2612 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2613 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2614 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[1]));
2615 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[1]));
2616}
2617
2618// This tests that the expected behavior occurs if the SSRC on a local track is
2619// changed when SetLocalDescription is called.
2620TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002621 ChangeSsrcOnTrackInLocalSessionDescription) {
deadbeefab9b2d12015-10-14 11:33:11 -07002622 FakeConstraints constraints;
2623 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2624 true);
2625 CreatePeerConnection(&constraints);
deadbeefab9b2d12015-10-14 11:33:11 -07002626
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002627 rtc::scoped_refptr<StreamCollection> stream_collection =
2628 CreateStreamCollection(2, 1);
2629 pc_->AddStream(stream_collection->at(0));
2630 std::unique_ptr<SessionDescriptionInterface> offer;
2631 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2632 // Grab a copy of the offer before it gets passed into the PC.
2633 std::unique_ptr<JsepSessionDescription> modified_offer(
2634 new JsepSessionDescription(JsepSessionDescription::kOffer));
2635 modified_offer->Initialize(offer->description()->Copy(), offer->session_id(),
2636 offer->session_version());
2637 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
deadbeefab9b2d12015-10-14 11:33:11 -07002638
deadbeefab9b2d12015-10-14 11:33:11 -07002639 auto senders = pc_->GetSenders();
2640 EXPECT_EQ(2u, senders.size());
2641 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2642 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2643
2644 // Change the ssrc of the audio and video track.
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002645 cricket::MediaContentDescription* desc =
2646 cricket::GetFirstAudioContentDescription(modified_offer->description());
2647 ASSERT_TRUE(desc != NULL);
2648 for (StreamParams& stream : desc->mutable_streams()) {
2649 for (unsigned int& ssrc : stream.ssrcs) {
2650 ++ssrc;
2651 }
2652 }
deadbeefab9b2d12015-10-14 11:33:11 -07002653
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002654 desc =
2655 cricket::GetFirstVideoContentDescription(modified_offer->description());
2656 ASSERT_TRUE(desc != NULL);
2657 for (StreamParams& stream : desc->mutable_streams()) {
2658 for (unsigned int& ssrc : stream.ssrcs) {
2659 ++ssrc;
2660 }
2661 }
2662
2663 EXPECT_TRUE(DoSetLocalDescription(modified_offer.release()));
deadbeefab9b2d12015-10-14 11:33:11 -07002664 senders = pc_->GetSenders();
2665 EXPECT_EQ(2u, senders.size());
2666 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2667 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2668 // TODO(deadbeef): Once RtpSenders expose parameters, check that the SSRC
2669 // changed.
2670}
2671
2672// This tests that the expected behavior occurs if a new session description is
2673// set with the same tracks, but on a different MediaStream.
Stefan Holmer55d6e7c2016-03-17 16:26:40 +01002674TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002675 SignalSameTracksInSeparateMediaStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002676 FakeConstraints constraints;
2677 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2678 true);
2679 CreatePeerConnection(&constraints);
deadbeefab9b2d12015-10-14 11:33:11 -07002680
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002681 rtc::scoped_refptr<StreamCollection> stream_collection =
2682 CreateStreamCollection(2, 1);
2683 pc_->AddStream(stream_collection->at(0));
2684 std::unique_ptr<SessionDescriptionInterface> offer;
2685 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2686 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
deadbeefab9b2d12015-10-14 11:33:11 -07002687
deadbeefab9b2d12015-10-14 11:33:11 -07002688 auto senders = pc_->GetSenders();
2689 EXPECT_EQ(2u, senders.size());
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002690 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0], kStreams[0]));
2691 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0], kStreams[0]));
deadbeefab9b2d12015-10-14 11:33:11 -07002692
2693 // Add a new MediaStream but with the same tracks as in the first stream.
2694 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream_1(
2695 webrtc::MediaStream::Create(kStreams[1]));
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002696 stream_1->AddTrack(stream_collection->at(0)->GetVideoTracks()[0]);
2697 stream_1->AddTrack(stream_collection->at(0)->GetAudioTracks()[0]);
deadbeefab9b2d12015-10-14 11:33:11 -07002698 pc_->AddStream(stream_1);
2699
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002700 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2701 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
deadbeefab9b2d12015-10-14 11:33:11 -07002702
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002703 auto new_senders = pc_->GetSenders();
2704 // Should be the same senders as before, but with updated stream id.
2705 // Note that this behavior is subject to change in the future.
2706 // We may decide the PC should ignore existing tracks in AddStream.
2707 EXPECT_EQ(senders, new_senders);
2708 EXPECT_TRUE(ContainsSender(new_senders, kAudioTracks[0], kStreams[1]));
2709 EXPECT_TRUE(ContainsSender(new_senders, kVideoTracks[0], kStreams[1]));
deadbeefab9b2d12015-10-14 11:33:11 -07002710}
2711
zhihuang81c3a032016-11-17 12:06:24 -08002712// This tests that PeerConnectionObserver::OnAddTrack is correctly called.
2713TEST_F(PeerConnectionInterfaceTest, OnAddTrackCallback) {
2714 FakeConstraints constraints;
2715 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2716 true);
2717 CreatePeerConnection(&constraints);
2718 CreateAndSetRemoteOffer(kSdpStringWithStream1AudioTrackOnly);
2719 EXPECT_EQ(observer_.num_added_tracks_, 1);
2720 EXPECT_EQ(observer_.last_added_track_label_, kAudioTracks[0]);
2721
2722 // Create and set the updated remote SDP.
2723 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2724 EXPECT_EQ(observer_.num_added_tracks_, 2);
2725 EXPECT_EQ(observer_.last_added_track_label_, kVideoTracks[0]);
2726}
2727
nisse51542be2016-02-12 02:27:06 -08002728class PeerConnectionMediaConfigTest : public testing::Test {
2729 protected:
2730 void SetUp() override {
nisseaf510af2016-03-21 08:20:42 -07002731 pcf_ = new rtc::RefCountedObject<PeerConnectionFactoryForTest>();
nisse51542be2016-02-12 02:27:06 -08002732 pcf_->Initialize();
2733 }
2734 const cricket::MediaConfig& TestCreatePeerConnection(
2735 const PeerConnectionInterface::RTCConfiguration& config,
2736 const MediaConstraintsInterface *constraints) {
2737 pcf_->create_media_controller_called_ = false;
2738
zhihuang9763d562016-08-05 11:14:50 -07002739 rtc::scoped_refptr<PeerConnectionInterface> pc(pcf_->CreatePeerConnection(
2740 config, constraints, nullptr, nullptr, &observer_));
nisse51542be2016-02-12 02:27:06 -08002741 EXPECT_TRUE(pc.get());
2742 EXPECT_TRUE(pcf_->create_media_controller_called_);
2743 return pcf_->create_media_controller_config_;
2744 }
2745
zhihuang9763d562016-08-05 11:14:50 -07002746 rtc::scoped_refptr<PeerConnectionFactoryForTest> pcf_;
nisse51542be2016-02-12 02:27:06 -08002747 MockPeerConnectionObserver observer_;
2748};
2749
2750// This test verifies the default behaviour with no constraints and a
2751// default RTCConfiguration.
2752TEST_F(PeerConnectionMediaConfigTest, TestDefaults) {
2753 PeerConnectionInterface::RTCConfiguration config;
2754 FakeConstraints constraints;
2755
2756 const cricket::MediaConfig& media_config =
2757 TestCreatePeerConnection(config, &constraints);
2758
2759 EXPECT_FALSE(media_config.enable_dscp);
nisse0db023a2016-03-01 04:29:59 -08002760 EXPECT_TRUE(media_config.video.enable_cpu_overuse_detection);
2761 EXPECT_FALSE(media_config.video.disable_prerenderer_smoothing);
2762 EXPECT_FALSE(media_config.video.suspend_below_min_bitrate);
nisse51542be2016-02-12 02:27:06 -08002763}
2764
2765// This test verifies the DSCP constraint is recognized and passed to
2766// the CreateMediaController call.
2767TEST_F(PeerConnectionMediaConfigTest, TestDscpConstraintTrue) {
2768 PeerConnectionInterface::RTCConfiguration config;
2769 FakeConstraints constraints;
2770
2771 constraints.AddOptional(webrtc::MediaConstraintsInterface::kEnableDscp, true);
2772 const cricket::MediaConfig& media_config =
2773 TestCreatePeerConnection(config, &constraints);
2774
2775 EXPECT_TRUE(media_config.enable_dscp);
2776}
2777
2778// This test verifies the cpu overuse detection constraint is
2779// recognized and passed to the CreateMediaController call.
2780TEST_F(PeerConnectionMediaConfigTest, TestCpuOveruseConstraintFalse) {
2781 PeerConnectionInterface::RTCConfiguration config;
2782 FakeConstraints constraints;
2783
2784 constraints.AddOptional(
2785 webrtc::MediaConstraintsInterface::kCpuOveruseDetection, false);
2786 const cricket::MediaConfig media_config =
2787 TestCreatePeerConnection(config, &constraints);
2788
nisse0db023a2016-03-01 04:29:59 -08002789 EXPECT_FALSE(media_config.video.enable_cpu_overuse_detection);
nisse51542be2016-02-12 02:27:06 -08002790}
2791
2792// This test verifies that the disable_prerenderer_smoothing flag is
2793// propagated from RTCConfiguration to the CreateMediaController call.
2794TEST_F(PeerConnectionMediaConfigTest, TestDisablePrerendererSmoothingTrue) {
2795 PeerConnectionInterface::RTCConfiguration config;
2796 FakeConstraints constraints;
2797
Niels Möller71bdda02016-03-31 12:59:59 +02002798 config.set_prerenderer_smoothing(false);
nisse51542be2016-02-12 02:27:06 -08002799 const cricket::MediaConfig& media_config =
2800 TestCreatePeerConnection(config, &constraints);
2801
nisse0db023a2016-03-01 04:29:59 -08002802 EXPECT_TRUE(media_config.video.disable_prerenderer_smoothing);
2803}
2804
2805// This test verifies the suspend below min bitrate constraint is
2806// recognized and passed to the CreateMediaController call.
2807TEST_F(PeerConnectionMediaConfigTest,
2808 TestSuspendBelowMinBitrateConstraintTrue) {
2809 PeerConnectionInterface::RTCConfiguration config;
2810 FakeConstraints constraints;
2811
2812 constraints.AddOptional(
2813 webrtc::MediaConstraintsInterface::kEnableVideoSuspendBelowMinBitrate,
2814 true);
2815 const cricket::MediaConfig media_config =
2816 TestCreatePeerConnection(config, &constraints);
2817
2818 EXPECT_TRUE(media_config.video.suspend_below_min_bitrate);
nisse51542be2016-02-12 02:27:06 -08002819}
2820
deadbeefab9b2d12015-10-14 11:33:11 -07002821// The following tests verify that session options are created correctly.
deadbeefc80741f2015-10-22 13:14:45 -07002822// TODO(deadbeef): Convert these tests to be more end-to-end. Instead of
2823// "verify options are converted correctly", should be "pass options into
2824// CreateOffer and verify the correct offer is produced."
deadbeefab9b2d12015-10-14 11:33:11 -07002825
2826TEST(CreateSessionOptionsTest, GetOptionsForOfferWithInvalidAudioOption) {
2827 RTCOfferAnswerOptions rtc_options;
2828 rtc_options.offer_to_receive_audio = RTCOfferAnswerOptions::kUndefined - 1;
2829
2830 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08002831 EXPECT_FALSE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002832
2833 rtc_options.offer_to_receive_audio =
2834 RTCOfferAnswerOptions::kMaxOfferToReceiveMedia + 1;
htaaac2dea2016-03-10 13:35:55 -08002835 EXPECT_FALSE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002836}
2837
2838TEST(CreateSessionOptionsTest, GetOptionsForOfferWithInvalidVideoOption) {
2839 RTCOfferAnswerOptions rtc_options;
2840 rtc_options.offer_to_receive_video = RTCOfferAnswerOptions::kUndefined - 1;
2841
2842 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08002843 EXPECT_FALSE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002844
2845 rtc_options.offer_to_receive_video =
2846 RTCOfferAnswerOptions::kMaxOfferToReceiveMedia + 1;
htaaac2dea2016-03-10 13:35:55 -08002847 EXPECT_FALSE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002848}
2849
2850// Test that a MediaSessionOptions is created for an offer if
deadbeefc80741f2015-10-22 13:14:45 -07002851// OfferToReceiveAudio and OfferToReceiveVideo options are set.
deadbeefab9b2d12015-10-14 11:33:11 -07002852TEST(CreateSessionOptionsTest, GetMediaSessionOptionsForOfferWithAudioVideo) {
2853 RTCOfferAnswerOptions rtc_options;
2854 rtc_options.offer_to_receive_audio = 1;
2855 rtc_options.offer_to_receive_video = 1;
2856
2857 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08002858 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002859 EXPECT_TRUE(options.has_audio());
2860 EXPECT_TRUE(options.has_video());
2861 EXPECT_TRUE(options.bundle_enabled);
2862}
2863
2864// Test that a correct MediaSessionOptions is created for an offer if
deadbeefc80741f2015-10-22 13:14:45 -07002865// OfferToReceiveAudio is set.
deadbeefab9b2d12015-10-14 11:33:11 -07002866TEST(CreateSessionOptionsTest, GetMediaSessionOptionsForOfferWithAudio) {
2867 RTCOfferAnswerOptions rtc_options;
2868 rtc_options.offer_to_receive_audio = 1;
2869
2870 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08002871 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002872 EXPECT_TRUE(options.has_audio());
2873 EXPECT_FALSE(options.has_video());
2874 EXPECT_TRUE(options.bundle_enabled);
2875}
2876
2877// Test that a correct MediaSessionOptions is created for an offer if
deadbeefc80741f2015-10-22 13:14:45 -07002878// the default OfferOptions are used.
deadbeefab9b2d12015-10-14 11:33:11 -07002879TEST(CreateSessionOptionsTest, GetDefaultMediaSessionOptionsForOffer) {
2880 RTCOfferAnswerOptions rtc_options;
2881
2882 cricket::MediaSessionOptions options;
deadbeef0ed85b22016-02-23 17:24:52 -08002883 options.transport_options["audio"] = cricket::TransportOptions();
2884 options.transport_options["video"] = cricket::TransportOptions();
htaaac2dea2016-03-10 13:35:55 -08002885 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefc80741f2015-10-22 13:14:45 -07002886 EXPECT_TRUE(options.has_audio());
deadbeefab9b2d12015-10-14 11:33:11 -07002887 EXPECT_FALSE(options.has_video());
deadbeefc80741f2015-10-22 13:14:45 -07002888 EXPECT_TRUE(options.bundle_enabled);
deadbeefab9b2d12015-10-14 11:33:11 -07002889 EXPECT_TRUE(options.vad_enabled);
deadbeef0ed85b22016-02-23 17:24:52 -08002890 EXPECT_FALSE(options.transport_options["audio"].ice_restart);
2891 EXPECT_FALSE(options.transport_options["video"].ice_restart);
deadbeefab9b2d12015-10-14 11:33:11 -07002892}
2893
2894// Test that a correct MediaSessionOptions is created for an offer if
deadbeefc80741f2015-10-22 13:14:45 -07002895// OfferToReceiveVideo is set.
deadbeefab9b2d12015-10-14 11:33:11 -07002896TEST(CreateSessionOptionsTest, GetMediaSessionOptionsForOfferWithVideo) {
2897 RTCOfferAnswerOptions rtc_options;
2898 rtc_options.offer_to_receive_audio = 0;
2899 rtc_options.offer_to_receive_video = 1;
2900
2901 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08002902 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002903 EXPECT_FALSE(options.has_audio());
2904 EXPECT_TRUE(options.has_video());
2905 EXPECT_TRUE(options.bundle_enabled);
2906}
2907
2908// Test that a correct MediaSessionOptions is created for an offer if
2909// UseRtpMux is set to false.
2910TEST(CreateSessionOptionsTest,
2911 GetMediaSessionOptionsForOfferWithBundleDisabled) {
2912 RTCOfferAnswerOptions rtc_options;
2913 rtc_options.offer_to_receive_audio = 1;
2914 rtc_options.offer_to_receive_video = 1;
2915 rtc_options.use_rtp_mux = false;
2916
2917 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08002918 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002919 EXPECT_TRUE(options.has_audio());
2920 EXPECT_TRUE(options.has_video());
2921 EXPECT_FALSE(options.bundle_enabled);
2922}
2923
2924// Test that a correct MediaSessionOptions is created to restart ice if
2925// IceRestart is set. It also tests that subsequent MediaSessionOptions don't
Taylor Brandstetterf475d362016-01-08 15:35:57 -08002926// have |audio_transport_options.ice_restart| etc. set.
deadbeefab9b2d12015-10-14 11:33:11 -07002927TEST(CreateSessionOptionsTest, GetMediaSessionOptionsForOfferWithIceRestart) {
2928 RTCOfferAnswerOptions rtc_options;
2929 rtc_options.ice_restart = true;
2930
2931 cricket::MediaSessionOptions options;
deadbeef0ed85b22016-02-23 17:24:52 -08002932 options.transport_options["audio"] = cricket::TransportOptions();
2933 options.transport_options["video"] = cricket::TransportOptions();
htaaac2dea2016-03-10 13:35:55 -08002934 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeef0ed85b22016-02-23 17:24:52 -08002935 EXPECT_TRUE(options.transport_options["audio"].ice_restart);
2936 EXPECT_TRUE(options.transport_options["video"].ice_restart);
deadbeefab9b2d12015-10-14 11:33:11 -07002937
2938 rtc_options = RTCOfferAnswerOptions();
htaaac2dea2016-03-10 13:35:55 -08002939 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeef0ed85b22016-02-23 17:24:52 -08002940 EXPECT_FALSE(options.transport_options["audio"].ice_restart);
2941 EXPECT_FALSE(options.transport_options["video"].ice_restart);
deadbeefab9b2d12015-10-14 11:33:11 -07002942}
2943
2944// Test that the MediaConstraints in an answer don't affect if audio and video
2945// is offered in an offer but that if kOfferToReceiveAudio or
2946// kOfferToReceiveVideo constraints are true in an offer, the media type will be
2947// included in subsequent answers.
2948TEST(CreateSessionOptionsTest, MediaConstraintsInAnswer) {
2949 FakeConstraints answer_c;
2950 answer_c.SetMandatoryReceiveAudio(true);
2951 answer_c.SetMandatoryReceiveVideo(true);
2952
2953 cricket::MediaSessionOptions answer_options;
2954 EXPECT_TRUE(ParseConstraintsForAnswer(&answer_c, &answer_options));
2955 EXPECT_TRUE(answer_options.has_audio());
2956 EXPECT_TRUE(answer_options.has_video());
2957
deadbeefc80741f2015-10-22 13:14:45 -07002958 RTCOfferAnswerOptions rtc_offer_options;
deadbeefab9b2d12015-10-14 11:33:11 -07002959
2960 cricket::MediaSessionOptions offer_options;
htaaac2dea2016-03-10 13:35:55 -08002961 EXPECT_TRUE(
2962 ExtractMediaSessionOptions(rtc_offer_options, false, &offer_options));
deadbeefc80741f2015-10-22 13:14:45 -07002963 EXPECT_TRUE(offer_options.has_audio());
htaaac2dea2016-03-10 13:35:55 -08002964 EXPECT_TRUE(offer_options.has_video());
deadbeefab9b2d12015-10-14 11:33:11 -07002965
deadbeefc80741f2015-10-22 13:14:45 -07002966 RTCOfferAnswerOptions updated_rtc_offer_options;
2967 updated_rtc_offer_options.offer_to_receive_audio = 1;
2968 updated_rtc_offer_options.offer_to_receive_video = 1;
deadbeefab9b2d12015-10-14 11:33:11 -07002969
2970 cricket::MediaSessionOptions updated_offer_options;
htaaac2dea2016-03-10 13:35:55 -08002971 EXPECT_TRUE(ExtractMediaSessionOptions(updated_rtc_offer_options, false,
htaa2a49d92016-03-04 02:51:39 -08002972 &updated_offer_options));
deadbeefab9b2d12015-10-14 11:33:11 -07002973 EXPECT_TRUE(updated_offer_options.has_audio());
2974 EXPECT_TRUE(updated_offer_options.has_video());
2975
2976 // Since an offer has been created with both audio and video, subsequent
2977 // offers and answers should contain both audio and video.
2978 // Answers will only contain the media types that exist in the offer
2979 // regardless of the value of |updated_answer_options.has_audio| and
2980 // |updated_answer_options.has_video|.
2981 FakeConstraints updated_answer_c;
2982 answer_c.SetMandatoryReceiveAudio(false);
2983 answer_c.SetMandatoryReceiveVideo(false);
2984
2985 cricket::MediaSessionOptions updated_answer_options;
2986 EXPECT_TRUE(
2987 ParseConstraintsForAnswer(&updated_answer_c, &updated_answer_options));
2988 EXPECT_TRUE(updated_answer_options.has_audio());
2989 EXPECT_TRUE(updated_answer_options.has_video());
deadbeefab9b2d12015-10-14 11:33:11 -07002990}