blob: d91336cd1af31b2f26939420109a9d7c83bf4158 [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"
deadbeefab9b2d12015-10-14 11:33:11 -070082 "a=rtpmap:103 ISAC/16000\r\n"
83 "a=ssrc:1 cname:stream1\r\n"
84 "a=ssrc:1 mslabel:stream1\r\n"
85 "a=ssrc:1 label:audiotrack0\r\n"
86 "m=video 1 RTP/AVPF 120\r\n"
87 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -070088 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -070089 "a=rtpmap:120 VP8/90000\r\n"
90 "a=ssrc:2 cname:stream1\r\n"
91 "a=ssrc:2 mslabel:stream1\r\n"
92 "a=ssrc:2 label:videotrack0\r\n";
93
94// Reference SDP with two MediaStreams with label "stream1" and "stream2. Each
95// MediaStreams have one audio track and one video track.
96// This uses MSID.
97static const char kSdpStringWithStream1And2[] =
98 "v=0\r\n"
99 "o=- 0 0 IN IP4 127.0.0.1\r\n"
100 "s=-\r\n"
101 "t=0 0\r\n"
102 "a=ice-ufrag:e5785931\r\n"
103 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
104 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
105 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
106 "a=msid-semantic: WMS stream1 stream2\r\n"
107 "m=audio 1 RTP/AVPF 103\r\n"
108 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700109 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700110 "a=rtpmap:103 ISAC/16000\r\n"
111 "a=ssrc:1 cname:stream1\r\n"
112 "a=ssrc:1 msid:stream1 audiotrack0\r\n"
113 "a=ssrc:3 cname:stream2\r\n"
114 "a=ssrc:3 msid:stream2 audiotrack1\r\n"
115 "m=video 1 RTP/AVPF 120\r\n"
116 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700117 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700118 "a=rtpmap:120 VP8/0\r\n"
119 "a=ssrc:2 cname:stream1\r\n"
120 "a=ssrc:2 msid:stream1 videotrack0\r\n"
121 "a=ssrc:4 cname:stream2\r\n"
122 "a=ssrc:4 msid:stream2 videotrack1\r\n";
123
124// Reference SDP without MediaStreams. Msid is not supported.
125static const char kSdpStringWithoutStreams[] =
126 "v=0\r\n"
127 "o=- 0 0 IN IP4 127.0.0.1\r\n"
128 "s=-\r\n"
129 "t=0 0\r\n"
130 "a=ice-ufrag:e5785931\r\n"
131 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
132 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
133 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
134 "m=audio 1 RTP/AVPF 103\r\n"
135 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700136 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700137 "a=rtpmap:103 ISAC/16000\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"
deadbeefab9b2d12015-10-14 11:33:11 -0700141 "a=rtpmap:120 VP8/90000\r\n";
142
143// Reference SDP without MediaStreams. Msid is supported.
144static const char kSdpStringWithMsidWithoutStreams[] =
145 "v=0\r\n"
146 "o=- 0 0 IN IP4 127.0.0.1\r\n"
147 "s=-\r\n"
148 "t=0 0\r\n"
149 "a=ice-ufrag:e5785931\r\n"
150 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
151 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
152 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
153 "a=msid-semantic: WMS\r\n"
154 "m=audio 1 RTP/AVPF 103\r\n"
155 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700156 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700157 "a=rtpmap:103 ISAC/16000\r\n"
158 "m=video 1 RTP/AVPF 120\r\n"
159 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700160 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700161 "a=rtpmap:120 VP8/90000\r\n";
162
163// Reference SDP without MediaStreams and audio only.
164static const char kSdpStringWithoutStreamsAudioOnly[] =
165 "v=0\r\n"
166 "o=- 0 0 IN IP4 127.0.0.1\r\n"
167 "s=-\r\n"
168 "t=0 0\r\n"
169 "a=ice-ufrag:e5785931\r\n"
170 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
171 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
172 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
173 "m=audio 1 RTP/AVPF 103\r\n"
174 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700175 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700176 "a=rtpmap:103 ISAC/16000\r\n";
177
178// Reference SENDONLY SDP without MediaStreams. Msid is not supported.
179static const char kSdpStringSendOnlyWithoutStreams[] =
180 "v=0\r\n"
181 "o=- 0 0 IN IP4 127.0.0.1\r\n"
182 "s=-\r\n"
183 "t=0 0\r\n"
184 "a=ice-ufrag:e5785931\r\n"
185 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
186 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
187 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
188 "m=audio 1 RTP/AVPF 103\r\n"
189 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700190 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700191 "a=sendonly\r\n"
192 "a=rtpmap:103 ISAC/16000\r\n"
193 "m=video 1 RTP/AVPF 120\r\n"
194 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700195 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700196 "a=sendonly\r\n"
197 "a=rtpmap:120 VP8/90000\r\n";
198
199static const char kSdpStringInit[] =
200 "v=0\r\n"
201 "o=- 0 0 IN IP4 127.0.0.1\r\n"
202 "s=-\r\n"
203 "t=0 0\r\n"
204 "a=ice-ufrag:e5785931\r\n"
205 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
206 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
207 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
208 "a=msid-semantic: WMS\r\n";
209
210static const char kSdpStringAudio[] =
211 "m=audio 1 RTP/AVPF 103\r\n"
212 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700213 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700214 "a=rtpmap:103 ISAC/16000\r\n";
215
216static const char kSdpStringVideo[] =
217 "m=video 1 RTP/AVPF 120\r\n"
218 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700219 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700220 "a=rtpmap:120 VP8/90000\r\n";
221
222static const char kSdpStringMs1Audio0[] =
223 "a=ssrc:1 cname:stream1\r\n"
224 "a=ssrc:1 msid:stream1 audiotrack0\r\n";
225
226static const char kSdpStringMs1Video0[] =
227 "a=ssrc:2 cname:stream1\r\n"
228 "a=ssrc:2 msid:stream1 videotrack0\r\n";
229
230static const char kSdpStringMs1Audio1[] =
231 "a=ssrc:3 cname:stream1\r\n"
232 "a=ssrc:3 msid:stream1 audiotrack1\r\n";
233
234static const char kSdpStringMs1Video1[] =
235 "a=ssrc:4 cname:stream1\r\n"
236 "a=ssrc:4 msid:stream1 videotrack1\r\n";
237
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000238#define MAYBE_SKIP_TEST(feature) \
239 if (!(feature())) { \
240 LOG(LS_INFO) << "Feature disabled... skipping"; \
241 return; \
242 }
243
perkjd61bf802016-03-24 03:16:19 -0700244using ::testing::Exactly;
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700245using cricket::StreamParams;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000246using webrtc::AudioSourceInterface;
deadbeefab9b2d12015-10-14 11:33:11 -0700247using webrtc::AudioTrack;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000248using webrtc::AudioTrackInterface;
249using webrtc::DataBuffer;
250using webrtc::DataChannelInterface;
251using webrtc::FakeConstraints;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000252using webrtc::IceCandidateInterface;
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700253using webrtc::JsepSessionDescription;
deadbeefc80741f2015-10-22 13:14:45 -0700254using webrtc::MediaConstraintsInterface;
deadbeefab9b2d12015-10-14 11:33:11 -0700255using webrtc::MediaStream;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000256using webrtc::MediaStreamInterface;
257using webrtc::MediaStreamTrackInterface;
258using webrtc::MockCreateSessionDescriptionObserver;
259using webrtc::MockDataChannelObserver;
260using webrtc::MockSetSessionDescriptionObserver;
261using webrtc::MockStatsObserver;
perkjd61bf802016-03-24 03:16:19 -0700262using webrtc::NotifierInterface;
263using webrtc::ObserverInterface;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000264using webrtc::PeerConnectionInterface;
265using webrtc::PeerConnectionObserver;
deadbeefab9b2d12015-10-14 11:33:11 -0700266using webrtc::RtpReceiverInterface;
267using webrtc::RtpSenderInterface;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000268using webrtc::SdpParseError;
269using webrtc::SessionDescriptionInterface;
deadbeefab9b2d12015-10-14 11:33:11 -0700270using webrtc::StreamCollection;
271using webrtc::StreamCollectionInterface;
perkja3ede6c2016-03-08 01:27:48 +0100272using webrtc::VideoTrackSourceInterface;
deadbeefab9b2d12015-10-14 11:33:11 -0700273using webrtc::VideoTrack;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000274using webrtc::VideoTrackInterface;
275
deadbeefab9b2d12015-10-14 11:33:11 -0700276typedef PeerConnectionInterface::RTCOfferAnswerOptions RTCOfferAnswerOptions;
277
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000278namespace {
279
280// Gets the first ssrc of given content type from the ContentInfo.
281bool GetFirstSsrc(const cricket::ContentInfo* content_info, int* ssrc) {
282 if (!content_info || !ssrc) {
283 return false;
284 }
285 const cricket::MediaContentDescription* media_desc =
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000286 static_cast<const cricket::MediaContentDescription*>(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000287 content_info->description);
288 if (!media_desc || media_desc->streams().empty()) {
289 return false;
290 }
291 *ssrc = media_desc->streams().begin()->first_ssrc();
292 return true;
293}
294
295void SetSsrcToZero(std::string* sdp) {
296 const char kSdpSsrcAtribute[] = "a=ssrc:";
297 const char kSdpSsrcAtributeZero[] = "a=ssrc:0";
298 size_t ssrc_pos = 0;
299 while ((ssrc_pos = sdp->find(kSdpSsrcAtribute, ssrc_pos)) !=
300 std::string::npos) {
301 size_t end_ssrc = sdp->find(" ", ssrc_pos);
302 sdp->replace(ssrc_pos, end_ssrc - ssrc_pos, kSdpSsrcAtributeZero);
303 ssrc_pos = end_ssrc;
304 }
305}
306
deadbeefab9b2d12015-10-14 11:33:11 -0700307// Check if |streams| contains the specified track.
308bool ContainsTrack(const std::vector<cricket::StreamParams>& streams,
309 const std::string& stream_label,
310 const std::string& track_id) {
311 for (const cricket::StreamParams& params : streams) {
312 if (params.sync_label == stream_label && params.id == track_id) {
313 return true;
314 }
315 }
316 return false;
317}
318
319// Check if |senders| contains the specified sender, by id.
320bool ContainsSender(
321 const std::vector<rtc::scoped_refptr<RtpSenderInterface>>& senders,
322 const std::string& id) {
323 for (const auto& sender : senders) {
324 if (sender->id() == id) {
325 return true;
326 }
327 }
328 return false;
329}
330
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700331// Check if |senders| contains the specified sender, by id and stream id.
332bool ContainsSender(
333 const std::vector<rtc::scoped_refptr<RtpSenderInterface>>& senders,
334 const std::string& id,
335 const std::string& stream_id) {
336 for (const auto& sender : senders) {
deadbeefa601f5c2016-06-06 14:27:39 -0700337 if (sender->id() == id && sender->stream_ids()[0] == stream_id) {
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700338 return true;
339 }
340 }
341 return false;
342}
343
deadbeefab9b2d12015-10-14 11:33:11 -0700344// Create a collection of streams.
345// CreateStreamCollection(1) creates a collection that
346// correspond to kSdpStringWithStream1.
347// CreateStreamCollection(2) correspond to kSdpStringWithStream1And2.
348rtc::scoped_refptr<StreamCollection> CreateStreamCollection(
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700349 int number_of_streams,
350 int tracks_per_stream) {
deadbeefab9b2d12015-10-14 11:33:11 -0700351 rtc::scoped_refptr<StreamCollection> local_collection(
352 StreamCollection::Create());
353
354 for (int i = 0; i < number_of_streams; ++i) {
355 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream(
356 webrtc::MediaStream::Create(kStreams[i]));
357
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700358 for (int j = 0; j < tracks_per_stream; ++j) {
359 // Add a local audio track.
360 rtc::scoped_refptr<webrtc::AudioTrackInterface> audio_track(
361 webrtc::AudioTrack::Create(kAudioTracks[i * tracks_per_stream + j],
362 nullptr));
363 stream->AddTrack(audio_track);
deadbeefab9b2d12015-10-14 11:33:11 -0700364
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700365 // Add a local video track.
366 rtc::scoped_refptr<webrtc::VideoTrackInterface> video_track(
367 webrtc::VideoTrack::Create(kVideoTracks[i * tracks_per_stream + j],
368 webrtc::FakeVideoTrackSource::Create()));
369 stream->AddTrack(video_track);
370 }
deadbeefab9b2d12015-10-14 11:33:11 -0700371
372 local_collection->AddStream(stream);
373 }
374 return local_collection;
375}
376
377// Check equality of StreamCollections.
378bool CompareStreamCollections(StreamCollectionInterface* s1,
379 StreamCollectionInterface* s2) {
380 if (s1 == nullptr || s2 == nullptr || s1->count() != s2->count()) {
381 return false;
382 }
383
384 for (size_t i = 0; i != s1->count(); ++i) {
385 if (s1->at(i)->label() != s2->at(i)->label()) {
386 return false;
387 }
388 webrtc::AudioTrackVector audio_tracks1 = s1->at(i)->GetAudioTracks();
389 webrtc::AudioTrackVector audio_tracks2 = s2->at(i)->GetAudioTracks();
390 webrtc::VideoTrackVector video_tracks1 = s1->at(i)->GetVideoTracks();
391 webrtc::VideoTrackVector video_tracks2 = s2->at(i)->GetVideoTracks();
392
393 if (audio_tracks1.size() != audio_tracks2.size()) {
394 return false;
395 }
396 for (size_t j = 0; j != audio_tracks1.size(); ++j) {
397 if (audio_tracks1[j]->id() != audio_tracks2[j]->id()) {
398 return false;
399 }
400 }
401 if (video_tracks1.size() != video_tracks2.size()) {
402 return false;
403 }
404 for (size_t j = 0; j != video_tracks1.size(); ++j) {
405 if (video_tracks1[j]->id() != video_tracks2[j]->id()) {
406 return false;
407 }
408 }
409 }
410 return true;
411}
412
perkjd61bf802016-03-24 03:16:19 -0700413// Helper class to test Observer.
414class MockTrackObserver : public ObserverInterface {
415 public:
416 explicit MockTrackObserver(NotifierInterface* notifier)
417 : notifier_(notifier) {
418 notifier_->RegisterObserver(this);
419 }
420
421 ~MockTrackObserver() { Unregister(); }
422
423 void Unregister() {
424 if (notifier_) {
425 notifier_->UnregisterObserver(this);
426 notifier_ = nullptr;
427 }
428 }
429
430 MOCK_METHOD0(OnChanged, void());
431
432 private:
433 NotifierInterface* notifier_;
434};
435
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000436class MockPeerConnectionObserver : public PeerConnectionObserver {
437 public:
deadbeefab9b2d12015-10-14 11:33:11 -0700438 MockPeerConnectionObserver() : remote_streams_(StreamCollection::Create()) {}
Henrik Kjellander3fe372d2016-05-12 08:10:52 +0200439 virtual ~MockPeerConnectionObserver() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000440 }
441 void SetPeerConnectionInterface(PeerConnectionInterface* pc) {
442 pc_ = pc;
443 if (pc) {
444 state_ = pc_->signaling_state();
445 }
446 }
nisseef8b61e2016-04-29 06:09:15 -0700447 void OnSignalingChange(
448 PeerConnectionInterface::SignalingState new_state) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000449 EXPECT_EQ(pc_->signaling_state(), new_state);
450 state_ = new_state;
451 }
452 // TODO(bemasc): Remove this once callers transition to OnIceGatheringChange.
453 virtual void OnStateChange(StateType state_changed) {
454 if (pc_.get() == NULL)
455 return;
456 switch (state_changed) {
457 case kSignalingState:
458 // OnSignalingChange and OnStateChange(kSignalingState) should always
459 // be called approximately simultaneously. To ease testing, we require
460 // that they always be called in that order. This check verifies
461 // that OnSignalingChange has just been called.
462 EXPECT_EQ(pc_->signaling_state(), state_);
463 break;
464 case kIceState:
465 ADD_FAILURE();
466 break;
467 default:
468 ADD_FAILURE();
469 break;
470 }
471 }
deadbeefab9b2d12015-10-14 11:33:11 -0700472
473 MediaStreamInterface* RemoteStream(const std::string& label) {
474 return remote_streams_->find(label);
475 }
476 StreamCollectionInterface* remote_streams() const { return remote_streams_; }
Taylor Brandstetter98cde262016-05-31 13:02:21 -0700477 void OnAddStream(rtc::scoped_refptr<MediaStreamInterface> stream) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000478 last_added_stream_ = stream;
deadbeefab9b2d12015-10-14 11:33:11 -0700479 remote_streams_->AddStream(stream);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000480 }
Taylor Brandstetter98cde262016-05-31 13:02:21 -0700481 void OnRemoveStream(
482 rtc::scoped_refptr<MediaStreamInterface> stream) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000483 last_removed_stream_ = stream;
deadbeefab9b2d12015-10-14 11:33:11 -0700484 remote_streams_->RemoveStream(stream);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000485 }
perkjdfb769d2016-02-09 03:09:43 -0800486 void OnRenegotiationNeeded() override { renegotiation_needed_ = true; }
Taylor Brandstetter98cde262016-05-31 13:02:21 -0700487 void OnDataChannel(
488 rtc::scoped_refptr<DataChannelInterface> data_channel) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000489 last_datachannel_ = data_channel;
490 }
491
perkjdfb769d2016-02-09 03:09:43 -0800492 void OnIceConnectionChange(
493 PeerConnectionInterface::IceConnectionState new_state) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000494 EXPECT_EQ(pc_->ice_connection_state(), new_state);
zhihuang29ff8442016-07-27 11:07:25 -0700495 callback_triggered = true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000496 }
perkjdfb769d2016-02-09 03:09:43 -0800497 void OnIceGatheringChange(
498 PeerConnectionInterface::IceGatheringState new_state) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000499 EXPECT_EQ(pc_->ice_gathering_state(), new_state);
perkjdfb769d2016-02-09 03:09:43 -0800500 ice_complete_ = new_state == PeerConnectionInterface::kIceGatheringComplete;
zhihuang29ff8442016-07-27 11:07:25 -0700501 callback_triggered = true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000502 }
perkjdfb769d2016-02-09 03:09:43 -0800503 void OnIceCandidate(const webrtc::IceCandidateInterface* candidate) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000504 EXPECT_NE(PeerConnectionInterface::kIceGatheringNew,
505 pc_->ice_gathering_state());
506
507 std::string sdp;
508 EXPECT_TRUE(candidate->ToString(&sdp));
509 EXPECT_LT(0u, sdp.size());
510 last_candidate_.reset(webrtc::CreateIceCandidate(candidate->sdp_mid(),
511 candidate->sdp_mline_index(), sdp, NULL));
512 EXPECT_TRUE(last_candidate_.get() != NULL);
zhihuang29ff8442016-07-27 11:07:25 -0700513 callback_triggered = true;
514 }
515
516 void OnIceCandidatesRemoved(
517 const std::vector<cricket::Candidate>& candidates) override {
518 callback_triggered = true;
519 }
520
521 void OnIceConnectionReceivingChange(bool receiving) override {
522 callback_triggered = true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000523 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000524
525 // Returns the label of the last added stream.
526 // Empty string if no stream have been added.
527 std::string GetLastAddedStreamLabel() {
528 if (last_added_stream_.get())
529 return last_added_stream_->label();
530 return "";
531 }
532 std::string GetLastRemovedStreamLabel() {
533 if (last_removed_stream_.get())
534 return last_removed_stream_->label();
535 return "";
536 }
537
zhihuang9763d562016-08-05 11:14:50 -0700538 rtc::scoped_refptr<PeerConnectionInterface> pc_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000539 PeerConnectionInterface::SignalingState state_;
kwibergd1fe2812016-04-27 06:47:29 -0700540 std::unique_ptr<IceCandidateInterface> last_candidate_;
zhihuang9763d562016-08-05 11:14:50 -0700541 rtc::scoped_refptr<DataChannelInterface> last_datachannel_;
deadbeefab9b2d12015-10-14 11:33:11 -0700542 rtc::scoped_refptr<StreamCollection> remote_streams_;
543 bool renegotiation_needed_ = false;
544 bool ice_complete_ = false;
zhihuang29ff8442016-07-27 11:07:25 -0700545 bool callback_triggered = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000546
547 private:
zhihuang9763d562016-08-05 11:14:50 -0700548 rtc::scoped_refptr<MediaStreamInterface> last_added_stream_;
549 rtc::scoped_refptr<MediaStreamInterface> last_removed_stream_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000550};
551
552} // namespace
deadbeefab9b2d12015-10-14 11:33:11 -0700553
zhihuang29ff8442016-07-27 11:07:25 -0700554// The PeerConnectionMediaConfig tests below verify that configuration
555// and constraints are propagated into the MediaConfig passed to
556// CreateMediaController. These settings are intended for MediaChannel
557// constructors, but that is not exercised by these unittest.
558class PeerConnectionFactoryForTest : public webrtc::PeerConnectionFactory {
559 public:
560 webrtc::MediaControllerInterface* CreateMediaController(
561 const cricket::MediaConfig& config) const override {
562 create_media_controller_called_ = true;
563 create_media_controller_config_ = config;
564
565 webrtc::MediaControllerInterface* mc =
566 PeerConnectionFactory::CreateMediaController(config);
567 EXPECT_TRUE(mc != nullptr);
568 return mc;
569 }
570
571 cricket::TransportController* CreateTransportController(
Honghai Zhangbfd398c2016-08-30 22:07:42 -0700572 cricket::PortAllocator* port_allocator,
573 bool redetermine_role_on_ice_restart) override {
zhihuang29ff8442016-07-27 11:07:25 -0700574 transport_controller = new cricket::TransportController(
Honghai Zhangbfd398c2016-08-30 22:07:42 -0700575 rtc::Thread::Current(), rtc::Thread::Current(), port_allocator,
576 redetermine_role_on_ice_restart);
zhihuang29ff8442016-07-27 11:07:25 -0700577 return transport_controller;
578 }
579
580 cricket::TransportController* transport_controller;
581 // Mutable, so they can be modified in the above const-declared method.
582 mutable bool create_media_controller_called_ = false;
583 mutable cricket::MediaConfig create_media_controller_config_;
584};
585
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000586class PeerConnectionInterfaceTest : public testing::Test {
587 protected:
phoglund37ebcf02016-01-08 05:04:57 -0800588 PeerConnectionInterfaceTest() {
589#ifdef WEBRTC_ANDROID
590 webrtc::InitializeAndroidObjects();
591#endif
592 }
593
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000594 virtual void SetUp() {
595 pc_factory_ = webrtc::CreatePeerConnectionFactory(
danilchape9021a32016-05-17 01:52:02 -0700596 rtc::Thread::Current(), rtc::Thread::Current(), rtc::Thread::Current(),
597 nullptr, nullptr, nullptr);
598 ASSERT_TRUE(pc_factory_);
zhihuang29ff8442016-07-27 11:07:25 -0700599 pc_factory_for_test_ =
600 new rtc::RefCountedObject<PeerConnectionFactoryForTest>();
601 pc_factory_for_test_->Initialize();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000602 }
603
604 void CreatePeerConnection() {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700605 CreatePeerConnection(PeerConnectionInterface::RTCConfiguration(), nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000606 }
607
608 void CreatePeerConnection(webrtc::MediaConstraintsInterface* constraints) {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700609 CreatePeerConnection(PeerConnectionInterface::RTCConfiguration(),
610 constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000611 }
612
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700613 void CreatePeerConnectionWithIceTransportsType(
614 PeerConnectionInterface::IceTransportsType type) {
615 PeerConnectionInterface::RTCConfiguration config;
616 config.type = type;
617 return CreatePeerConnection(config, nullptr);
618 }
619
620 void CreatePeerConnectionWithIceServer(const std::string& uri,
621 const std::string& password) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800622 PeerConnectionInterface::RTCConfiguration config;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000623 PeerConnectionInterface::IceServer server;
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700624 server.uri = uri;
625 server.password = password;
626 config.servers.push_back(server);
627 CreatePeerConnection(config, nullptr);
628 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000629
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700630 void CreatePeerConnection(PeerConnectionInterface::RTCConfiguration config,
631 webrtc::MediaConstraintsInterface* constraints) {
kwibergd1fe2812016-04-27 06:47:29 -0700632 std::unique_ptr<cricket::FakePortAllocator> port_allocator(
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800633 new cricket::FakePortAllocator(rtc::Thread::Current(), nullptr));
634 port_allocator_ = port_allocator.get();
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000635
buildbot@webrtc.org61c1b8e2014-04-09 06:06:38 +0000636 // DTLS does not work in a loopback call, so is disabled for most of the
637 // tests in this file. We only create a FakeIdentityService if the test
638 // explicitly sets the constraint.
jiayl@webrtc.org61e00b02015-03-04 22:17:38 +0000639 FakeConstraints default_constraints;
640 if (!constraints) {
641 constraints = &default_constraints;
642
643 default_constraints.AddMandatory(
644 webrtc::MediaConstraintsInterface::kEnableDtlsSrtp, false);
645 }
646
Henrik Boströmd79599d2016-06-01 13:58:50 +0200647 std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator;
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000648 bool dtls;
649 if (FindConstraint(constraints,
650 webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
651 &dtls,
Henrik Boström5e56c592015-08-11 10:33:13 +0200652 nullptr) && dtls) {
Henrik Boströmd79599d2016-06-01 13:58:50 +0200653 cert_generator.reset(new FakeRTCCertificateGenerator());
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000654 }
Henrik Boströmd79599d2016-06-01 13:58:50 +0200655 pc_ = pc_factory_->CreatePeerConnection(
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800656 config, constraints, std::move(port_allocator),
Henrik Boströmd79599d2016-06-01 13:58:50 +0200657 std::move(cert_generator), &observer_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000658 ASSERT_TRUE(pc_.get() != NULL);
659 observer_.SetPeerConnectionInterface(pc_.get());
660 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
661 }
662
deadbeef0a6c4ca2015-10-06 11:38:28 -0700663 void CreatePeerConnectionExpectFail(const std::string& uri) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800664 PeerConnectionInterface::RTCConfiguration config;
deadbeef0a6c4ca2015-10-06 11:38:28 -0700665 PeerConnectionInterface::IceServer server;
deadbeef0a6c4ca2015-10-06 11:38:28 -0700666 server.uri = uri;
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800667 config.servers.push_back(server);
deadbeef0a6c4ca2015-10-06 11:38:28 -0700668
zhihuang9763d562016-08-05 11:14:50 -0700669 rtc::scoped_refptr<PeerConnectionInterface> pc;
hbosd7973cc2016-05-27 06:08:53 -0700670 pc = pc_factory_->CreatePeerConnection(config, nullptr, nullptr, nullptr,
671 &observer_);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800672 EXPECT_EQ(nullptr, pc);
deadbeef0a6c4ca2015-10-06 11:38:28 -0700673 }
674
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000675 void CreatePeerConnectionWithDifferentConfigurations() {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700676 CreatePeerConnectionWithIceServer(kStunAddressOnly, "");
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800677 EXPECT_EQ(1u, port_allocator_->stun_servers().size());
678 EXPECT_EQ(0u, port_allocator_->turn_servers().size());
679 EXPECT_EQ("address", port_allocator_->stun_servers().begin()->hostname());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000680 EXPECT_EQ(kDefaultStunPort,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800681 port_allocator_->stun_servers().begin()->port());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000682
deadbeef0a6c4ca2015-10-06 11:38:28 -0700683 CreatePeerConnectionExpectFail(kStunInvalidPort);
684 CreatePeerConnectionExpectFail(kStunAddressPortAndMore1);
685 CreatePeerConnectionExpectFail(kStunAddressPortAndMore2);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000686
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700687 CreatePeerConnectionWithIceServer(kTurnIceServerUri, kTurnPassword);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800688 EXPECT_EQ(0u, port_allocator_->stun_servers().size());
689 EXPECT_EQ(1u, port_allocator_->turn_servers().size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000690 EXPECT_EQ(kTurnUsername,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800691 port_allocator_->turn_servers()[0].credentials.username);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000692 EXPECT_EQ(kTurnPassword,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800693 port_allocator_->turn_servers()[0].credentials.password);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000694 EXPECT_EQ(kTurnHostname,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800695 port_allocator_->turn_servers()[0].ports[0].address.hostname());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000696 }
697
698 void ReleasePeerConnection() {
699 pc_ = NULL;
700 observer_.SetPeerConnectionInterface(NULL);
701 }
702
deadbeefab9b2d12015-10-14 11:33:11 -0700703 void AddVideoStream(const std::string& label) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000704 // Create a local stream.
zhihuang9763d562016-08-05 11:14:50 -0700705 rtc::scoped_refptr<MediaStreamInterface> stream(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000706 pc_factory_->CreateLocalMediaStream(label));
zhihuang9763d562016-08-05 11:14:50 -0700707 rtc::scoped_refptr<VideoTrackSourceInterface> video_source(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000708 pc_factory_->CreateVideoSource(new cricket::FakeVideoCapturer(), NULL));
zhihuang9763d562016-08-05 11:14:50 -0700709 rtc::scoped_refptr<VideoTrackInterface> video_track(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000710 pc_factory_->CreateVideoTrack(label + "v0", video_source));
711 stream->AddTrack(video_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +0000712 EXPECT_TRUE(pc_->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000713 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
714 observer_.renegotiation_needed_ = false;
715 }
716
717 void AddVoiceStream(const std::string& label) {
718 // Create a local stream.
zhihuang9763d562016-08-05 11:14:50 -0700719 rtc::scoped_refptr<MediaStreamInterface> stream(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000720 pc_factory_->CreateLocalMediaStream(label));
zhihuang9763d562016-08-05 11:14:50 -0700721 rtc::scoped_refptr<AudioTrackInterface> audio_track(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000722 pc_factory_->CreateAudioTrack(label + "a0", NULL));
723 stream->AddTrack(audio_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +0000724 EXPECT_TRUE(pc_->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000725 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
726 observer_.renegotiation_needed_ = false;
727 }
728
729 void AddAudioVideoStream(const std::string& stream_label,
730 const std::string& audio_track_label,
731 const std::string& video_track_label) {
732 // Create a local stream.
zhihuang9763d562016-08-05 11:14:50 -0700733 rtc::scoped_refptr<MediaStreamInterface> stream(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000734 pc_factory_->CreateLocalMediaStream(stream_label));
zhihuang9763d562016-08-05 11:14:50 -0700735 rtc::scoped_refptr<AudioTrackInterface> audio_track(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000736 pc_factory_->CreateAudioTrack(
737 audio_track_label, static_cast<AudioSourceInterface*>(NULL)));
738 stream->AddTrack(audio_track.get());
zhihuang9763d562016-08-05 11:14:50 -0700739 rtc::scoped_refptr<VideoTrackInterface> video_track(
nisseaf510af2016-03-21 08:20:42 -0700740 pc_factory_->CreateVideoTrack(
741 video_track_label,
742 pc_factory_->CreateVideoSource(new cricket::FakeVideoCapturer())));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000743 stream->AddTrack(video_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +0000744 EXPECT_TRUE(pc_->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000745 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
746 observer_.renegotiation_needed_ = false;
747 }
748
kwibergd1fe2812016-04-27 06:47:29 -0700749 bool DoCreateOfferAnswer(std::unique_ptr<SessionDescriptionInterface>* desc,
deadbeefc80741f2015-10-22 13:14:45 -0700750 bool offer,
751 MediaConstraintsInterface* constraints) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000752 rtc::scoped_refptr<MockCreateSessionDescriptionObserver>
753 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000754 MockCreateSessionDescriptionObserver>());
755 if (offer) {
deadbeefc80741f2015-10-22 13:14:45 -0700756 pc_->CreateOffer(observer, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000757 } else {
deadbeefc80741f2015-10-22 13:14:45 -0700758 pc_->CreateAnswer(observer, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000759 }
760 EXPECT_EQ_WAIT(true, observer->called(), kTimeout);
kwiberg2bbff992016-03-16 11:03:04 -0700761 desc->reset(observer->release_desc());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000762 return observer->result();
763 }
764
kwibergd1fe2812016-04-27 06:47:29 -0700765 bool DoCreateOffer(std::unique_ptr<SessionDescriptionInterface>* desc,
deadbeefc80741f2015-10-22 13:14:45 -0700766 MediaConstraintsInterface* constraints) {
767 return DoCreateOfferAnswer(desc, true, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000768 }
769
kwibergd1fe2812016-04-27 06:47:29 -0700770 bool DoCreateAnswer(std::unique_ptr<SessionDescriptionInterface>* desc,
deadbeefc80741f2015-10-22 13:14:45 -0700771 MediaConstraintsInterface* constraints) {
772 return DoCreateOfferAnswer(desc, false, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000773 }
774
775 bool DoSetSessionDescription(SessionDescriptionInterface* desc, bool local) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000776 rtc::scoped_refptr<MockSetSessionDescriptionObserver>
777 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000778 MockSetSessionDescriptionObserver>());
779 if (local) {
780 pc_->SetLocalDescription(observer, desc);
781 } else {
782 pc_->SetRemoteDescription(observer, desc);
783 }
zhihuang29ff8442016-07-27 11:07:25 -0700784 if (pc_->signaling_state() != PeerConnectionInterface::kClosed) {
785 EXPECT_EQ_WAIT(true, observer->called(), kTimeout);
786 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000787 return observer->result();
788 }
789
790 bool DoSetLocalDescription(SessionDescriptionInterface* desc) {
791 return DoSetSessionDescription(desc, true);
792 }
793
794 bool DoSetRemoteDescription(SessionDescriptionInterface* desc) {
795 return DoSetSessionDescription(desc, false);
796 }
797
798 // Calls PeerConnection::GetStats and check the return value.
799 // It does not verify the values in the StatReports since a RTCP packet might
800 // be required.
801 bool DoGetStats(MediaStreamTrackInterface* track) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000802 rtc::scoped_refptr<MockStatsObserver> observer(
803 new rtc::RefCountedObject<MockStatsObserver>());
jiayl@webrtc.orgdb41b4d2014-03-03 21:30:06 +0000804 if (!pc_->GetStats(
805 observer, track, PeerConnectionInterface::kStatsOutputLevelStandard))
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000806 return false;
807 EXPECT_TRUE_WAIT(observer->called(), kTimeout);
808 return observer->called();
809 }
810
811 void InitiateCall() {
812 CreatePeerConnection();
813 // Create a local stream with audio&video tracks.
814 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
815 CreateOfferReceiveAnswer();
816 }
817
818 // Verify that RTP Header extensions has been negotiated for audio and video.
819 void VerifyRemoteRtpHeaderExtensions() {
820 const cricket::MediaContentDescription* desc =
821 cricket::GetFirstAudioContentDescription(
822 pc_->remote_description()->description());
823 ASSERT_TRUE(desc != NULL);
824 EXPECT_GT(desc->rtp_header_extensions().size(), 0u);
825
826 desc = cricket::GetFirstVideoContentDescription(
827 pc_->remote_description()->description());
828 ASSERT_TRUE(desc != NULL);
829 EXPECT_GT(desc->rtp_header_extensions().size(), 0u);
830 }
831
832 void CreateOfferAsRemoteDescription() {
kwibergd1fe2812016-04-27 06:47:29 -0700833 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -0700834 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000835 std::string sdp;
836 EXPECT_TRUE(offer->ToString(&sdp));
837 SessionDescriptionInterface* remote_offer =
838 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
839 sdp, NULL);
840 EXPECT_TRUE(DoSetRemoteDescription(remote_offer));
841 EXPECT_EQ(PeerConnectionInterface::kHaveRemoteOffer, observer_.state_);
842 }
843
deadbeefab9b2d12015-10-14 11:33:11 -0700844 void CreateAndSetRemoteOffer(const std::string& sdp) {
845 SessionDescriptionInterface* remote_offer =
846 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
847 sdp, nullptr);
848 EXPECT_TRUE(DoSetRemoteDescription(remote_offer));
849 EXPECT_EQ(PeerConnectionInterface::kHaveRemoteOffer, observer_.state_);
850 }
851
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000852 void CreateAnswerAsLocalDescription() {
kwibergd1fe2812016-04-27 06:47:29 -0700853 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -0700854 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000855
856 // TODO(perkj): Currently SetLocalDescription fails if any parameters in an
857 // audio codec change, even if the parameter has nothing to do with
858 // receiving. Not all parameters are serialized to SDP.
859 // Since CreatePrAnswerAsLocalDescription serialize/deserialize
860 // the SessionDescription, it is necessary to do that here to in order to
861 // get ReceiveOfferCreatePrAnswerAndAnswer and RenegotiateAudioOnly to pass.
862 // https://code.google.com/p/webrtc/issues/detail?id=1356
863 std::string sdp;
864 EXPECT_TRUE(answer->ToString(&sdp));
865 SessionDescriptionInterface* new_answer =
866 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
867 sdp, NULL);
868 EXPECT_TRUE(DoSetLocalDescription(new_answer));
869 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
870 }
871
872 void CreatePrAnswerAsLocalDescription() {
kwibergd1fe2812016-04-27 06:47:29 -0700873 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -0700874 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000875
876 std::string sdp;
877 EXPECT_TRUE(answer->ToString(&sdp));
878 SessionDescriptionInterface* pr_answer =
879 webrtc::CreateSessionDescription(SessionDescriptionInterface::kPrAnswer,
880 sdp, NULL);
881 EXPECT_TRUE(DoSetLocalDescription(pr_answer));
882 EXPECT_EQ(PeerConnectionInterface::kHaveLocalPrAnswer, observer_.state_);
883 }
884
885 void CreateOfferReceiveAnswer() {
886 CreateOfferAsLocalDescription();
887 std::string sdp;
888 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
889 CreateAnswerAsRemoteDescription(sdp);
890 }
891
892 void CreateOfferAsLocalDescription() {
kwibergd1fe2812016-04-27 06:47:29 -0700893 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -0700894 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000895 // TODO(perkj): Currently SetLocalDescription fails if any parameters in an
896 // audio codec change, even if the parameter has nothing to do with
897 // receiving. Not all parameters are serialized to SDP.
898 // Since CreatePrAnswerAsLocalDescription serialize/deserialize
899 // the SessionDescription, it is necessary to do that here to in order to
900 // get ReceiveOfferCreatePrAnswerAndAnswer and RenegotiateAudioOnly to pass.
901 // https://code.google.com/p/webrtc/issues/detail?id=1356
902 std::string sdp;
903 EXPECT_TRUE(offer->ToString(&sdp));
904 SessionDescriptionInterface* new_offer =
905 webrtc::CreateSessionDescription(
906 SessionDescriptionInterface::kOffer,
907 sdp, NULL);
908
909 EXPECT_TRUE(DoSetLocalDescription(new_offer));
910 EXPECT_EQ(PeerConnectionInterface::kHaveLocalOffer, observer_.state_);
mallinath@webrtc.org68cbd012014-01-22 00:16:46 +0000911 // Wait for the ice_complete message, so that SDP will have candidates.
912 EXPECT_TRUE_WAIT(observer_.ice_complete_, kTimeout);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000913 }
914
deadbeefab9b2d12015-10-14 11:33:11 -0700915 void CreateAnswerAsRemoteDescription(const std::string& sdp) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000916 webrtc::JsepSessionDescription* answer = new webrtc::JsepSessionDescription(
917 SessionDescriptionInterface::kAnswer);
deadbeefab9b2d12015-10-14 11:33:11 -0700918 EXPECT_TRUE(answer->Initialize(sdp, NULL));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000919 EXPECT_TRUE(DoSetRemoteDescription(answer));
920 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
921 }
922
deadbeefab9b2d12015-10-14 11:33:11 -0700923 void CreatePrAnswerAndAnswerAsRemoteDescription(const std::string& sdp) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000924 webrtc::JsepSessionDescription* pr_answer =
925 new webrtc::JsepSessionDescription(
926 SessionDescriptionInterface::kPrAnswer);
deadbeefab9b2d12015-10-14 11:33:11 -0700927 EXPECT_TRUE(pr_answer->Initialize(sdp, NULL));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000928 EXPECT_TRUE(DoSetRemoteDescription(pr_answer));
929 EXPECT_EQ(PeerConnectionInterface::kHaveRemotePrAnswer, observer_.state_);
930 webrtc::JsepSessionDescription* answer =
931 new webrtc::JsepSessionDescription(
932 SessionDescriptionInterface::kAnswer);
deadbeefab9b2d12015-10-14 11:33:11 -0700933 EXPECT_TRUE(answer->Initialize(sdp, NULL));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000934 EXPECT_TRUE(DoSetRemoteDescription(answer));
935 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
936 }
937
938 // Help function used for waiting until a the last signaled remote stream has
939 // the same label as |stream_label|. In a few of the tests in this file we
940 // answer with the same session description as we offer and thus we can
941 // check if OnAddStream have been called with the same stream as we offer to
942 // send.
943 void WaitAndVerifyOnAddStream(const std::string& stream_label) {
944 EXPECT_EQ_WAIT(stream_label, observer_.GetLastAddedStreamLabel(), kTimeout);
945 }
946
947 // Creates an offer and applies it as a local session description.
948 // Creates an answer with the same SDP an the offer but removes all lines
949 // that start with a:ssrc"
950 void CreateOfferReceiveAnswerWithoutSsrc() {
951 CreateOfferAsLocalDescription();
952 std::string sdp;
953 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
954 SetSsrcToZero(&sdp);
955 CreateAnswerAsRemoteDescription(sdp);
956 }
957
deadbeefab9b2d12015-10-14 11:33:11 -0700958 // This function creates a MediaStream with label kStreams[0] and
959 // |number_of_audio_tracks| and |number_of_video_tracks| tracks and the
960 // corresponding SessionDescriptionInterface. The SessionDescriptionInterface
kwiberg2bbff992016-03-16 11:03:04 -0700961 // is returned and the MediaStream is stored in
deadbeefab9b2d12015-10-14 11:33:11 -0700962 // |reference_collection_|
kwibergd1fe2812016-04-27 06:47:29 -0700963 std::unique_ptr<SessionDescriptionInterface>
kwiberg2bbff992016-03-16 11:03:04 -0700964 CreateSessionDescriptionAndReference(size_t number_of_audio_tracks,
965 size_t number_of_video_tracks) {
966 EXPECT_LE(number_of_audio_tracks, 2u);
967 EXPECT_LE(number_of_video_tracks, 2u);
deadbeefab9b2d12015-10-14 11:33:11 -0700968
969 reference_collection_ = StreamCollection::Create();
970 std::string sdp_ms1 = std::string(kSdpStringInit);
971
972 std::string mediastream_label = kStreams[0];
973
974 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream(
975 webrtc::MediaStream::Create(mediastream_label));
976 reference_collection_->AddStream(stream);
977
978 if (number_of_audio_tracks > 0) {
979 sdp_ms1 += std::string(kSdpStringAudio);
980 sdp_ms1 += std::string(kSdpStringMs1Audio0);
981 AddAudioTrack(kAudioTracks[0], stream);
982 }
983 if (number_of_audio_tracks > 1) {
984 sdp_ms1 += kSdpStringMs1Audio1;
985 AddAudioTrack(kAudioTracks[1], stream);
986 }
987
988 if (number_of_video_tracks > 0) {
989 sdp_ms1 += std::string(kSdpStringVideo);
990 sdp_ms1 += std::string(kSdpStringMs1Video0);
991 AddVideoTrack(kVideoTracks[0], stream);
992 }
993 if (number_of_video_tracks > 1) {
994 sdp_ms1 += kSdpStringMs1Video1;
995 AddVideoTrack(kVideoTracks[1], stream);
996 }
997
kwibergd1fe2812016-04-27 06:47:29 -0700998 return std::unique_ptr<SessionDescriptionInterface>(
kwiberg2bbff992016-03-16 11:03:04 -0700999 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
1000 sdp_ms1, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07001001 }
1002
1003 void AddAudioTrack(const std::string& track_id,
1004 MediaStreamInterface* stream) {
1005 rtc::scoped_refptr<webrtc::AudioTrackInterface> audio_track(
1006 webrtc::AudioTrack::Create(track_id, nullptr));
1007 ASSERT_TRUE(stream->AddTrack(audio_track));
1008 }
1009
1010 void AddVideoTrack(const std::string& track_id,
1011 MediaStreamInterface* stream) {
1012 rtc::scoped_refptr<webrtc::VideoTrackInterface> video_track(
nisseaf510af2016-03-21 08:20:42 -07001013 webrtc::VideoTrack::Create(track_id,
1014 webrtc::FakeVideoTrackSource::Create()));
deadbeefab9b2d12015-10-14 11:33:11 -07001015 ASSERT_TRUE(stream->AddTrack(video_track));
1016 }
1017
kwibergfd8be342016-05-14 19:44:11 -07001018 std::unique_ptr<SessionDescriptionInterface> CreateOfferWithOneAudioStream() {
zhihuang8f65cdf2016-05-06 18:40:30 -07001019 CreatePeerConnection();
1020 AddVoiceStream(kStreamLabel1);
kwibergfd8be342016-05-14 19:44:11 -07001021 std::unique_ptr<SessionDescriptionInterface> offer;
zhihuang8f65cdf2016-05-06 18:40:30 -07001022 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
1023 return offer;
1024 }
1025
kwibergfd8be342016-05-14 19:44:11 -07001026 std::unique_ptr<SessionDescriptionInterface>
zhihuang8f65cdf2016-05-06 18:40:30 -07001027 CreateAnswerWithOneAudioStream() {
kwibergfd8be342016-05-14 19:44:11 -07001028 std::unique_ptr<SessionDescriptionInterface> offer =
zhihuang8f65cdf2016-05-06 18:40:30 -07001029 CreateOfferWithOneAudioStream();
1030 EXPECT_TRUE(DoSetRemoteDescription(offer.release()));
kwibergfd8be342016-05-14 19:44:11 -07001031 std::unique_ptr<SessionDescriptionInterface> answer;
zhihuang8f65cdf2016-05-06 18:40:30 -07001032 EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
1033 return answer;
1034 }
1035
1036 const std::string& GetFirstAudioStreamCname(
1037 const SessionDescriptionInterface* desc) {
1038 const cricket::ContentInfo* audio_content =
1039 cricket::GetFirstAudioContent(desc->description());
1040 const cricket::AudioContentDescription* audio_desc =
1041 static_cast<const cricket::AudioContentDescription*>(
1042 audio_content->description);
1043 return audio_desc->streams()[0].cname;
1044 }
1045
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08001046 cricket::FakePortAllocator* port_allocator_ = nullptr;
zhihuang9763d562016-08-05 11:14:50 -07001047 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface> pc_factory_;
1048 rtc::scoped_refptr<PeerConnectionFactoryForTest> pc_factory_for_test_;
1049 rtc::scoped_refptr<PeerConnectionInterface> pc_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001050 MockPeerConnectionObserver observer_;
deadbeefab9b2d12015-10-14 11:33:11 -07001051 rtc::scoped_refptr<StreamCollection> reference_collection_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001052};
1053
zhihuang29ff8442016-07-27 11:07:25 -07001054// Test that no callbacks on the PeerConnectionObserver are called after the
1055// PeerConnection is closed.
1056TEST_F(PeerConnectionInterfaceTest, CloseAndTestCallbackFunctions) {
zhihuang9763d562016-08-05 11:14:50 -07001057 rtc::scoped_refptr<PeerConnectionInterface> pc(
zhihuang29ff8442016-07-27 11:07:25 -07001058 pc_factory_for_test_->CreatePeerConnection(
1059 PeerConnectionInterface::RTCConfiguration(), nullptr, nullptr,
1060 nullptr, &observer_));
1061 observer_.SetPeerConnectionInterface(pc.get());
1062 pc->Close();
1063
1064 // No callbacks is expected to be called.
1065 observer_.callback_triggered = false;
1066 std::vector<cricket::Candidate> candidates;
1067 pc_factory_for_test_->transport_controller->SignalGatheringState(
1068 cricket::IceGatheringState{});
1069 pc_factory_for_test_->transport_controller->SignalCandidatesGathered(
1070 "", candidates);
1071 pc_factory_for_test_->transport_controller->SignalConnectionState(
1072 cricket::IceConnectionState{});
1073 pc_factory_for_test_->transport_controller->SignalCandidatesRemoved(
1074 candidates);
1075 pc_factory_for_test_->transport_controller->SignalReceiving(false);
1076 EXPECT_FALSE(observer_.callback_triggered);
1077}
1078
zhihuang8f65cdf2016-05-06 18:40:30 -07001079// Generate different CNAMEs when PeerConnections are created.
1080// The CNAMEs are expected to be generated randomly. It is possible
1081// that the test fails, though the possibility is very low.
1082TEST_F(PeerConnectionInterfaceTest, CnameGenerationInOffer) {
kwibergfd8be342016-05-14 19:44:11 -07001083 std::unique_ptr<SessionDescriptionInterface> offer1 =
zhihuang8f65cdf2016-05-06 18:40:30 -07001084 CreateOfferWithOneAudioStream();
kwibergfd8be342016-05-14 19:44:11 -07001085 std::unique_ptr<SessionDescriptionInterface> offer2 =
zhihuang8f65cdf2016-05-06 18:40:30 -07001086 CreateOfferWithOneAudioStream();
1087 EXPECT_NE(GetFirstAudioStreamCname(offer1.get()),
1088 GetFirstAudioStreamCname(offer2.get()));
1089}
1090
1091TEST_F(PeerConnectionInterfaceTest, CnameGenerationInAnswer) {
kwibergfd8be342016-05-14 19:44:11 -07001092 std::unique_ptr<SessionDescriptionInterface> answer1 =
zhihuang8f65cdf2016-05-06 18:40:30 -07001093 CreateAnswerWithOneAudioStream();
kwibergfd8be342016-05-14 19:44:11 -07001094 std::unique_ptr<SessionDescriptionInterface> answer2 =
zhihuang8f65cdf2016-05-06 18:40:30 -07001095 CreateAnswerWithOneAudioStream();
1096 EXPECT_NE(GetFirstAudioStreamCname(answer1.get()),
1097 GetFirstAudioStreamCname(answer2.get()));
1098}
1099
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001100TEST_F(PeerConnectionInterfaceTest,
1101 CreatePeerConnectionWithDifferentConfigurations) {
1102 CreatePeerConnectionWithDifferentConfigurations();
1103}
1104
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001105TEST_F(PeerConnectionInterfaceTest,
1106 CreatePeerConnectionWithDifferentIceTransportsTypes) {
1107 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kNone);
1108 EXPECT_EQ(cricket::CF_NONE, port_allocator_->candidate_filter());
1109 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kRelay);
1110 EXPECT_EQ(cricket::CF_RELAY, port_allocator_->candidate_filter());
1111 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kNoHost);
1112 EXPECT_EQ(cricket::CF_ALL & ~cricket::CF_HOST,
1113 port_allocator_->candidate_filter());
1114 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kAll);
1115 EXPECT_EQ(cricket::CF_ALL, port_allocator_->candidate_filter());
1116}
1117
1118// Test that when a PeerConnection is created with a nonzero candidate pool
1119// size, the pooled PortAllocatorSession is created with all the attributes
1120// in the RTCConfiguration.
1121TEST_F(PeerConnectionInterfaceTest, CreatePeerConnectionWithPooledCandidates) {
1122 PeerConnectionInterface::RTCConfiguration config;
1123 PeerConnectionInterface::IceServer server;
1124 server.uri = kStunAddressOnly;
1125 config.servers.push_back(server);
1126 config.type = PeerConnectionInterface::kRelay;
1127 config.disable_ipv6 = true;
1128 config.tcp_candidate_policy =
1129 PeerConnectionInterface::kTcpCandidatePolicyDisabled;
honghaiz60347052016-05-31 18:29:12 -07001130 config.candidate_network_policy =
1131 PeerConnectionInterface::kCandidateNetworkPolicyLowCost;
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001132 config.ice_candidate_pool_size = 1;
1133 CreatePeerConnection(config, nullptr);
1134
1135 const cricket::FakePortAllocatorSession* session =
1136 static_cast<const cricket::FakePortAllocatorSession*>(
1137 port_allocator_->GetPooledSession());
1138 ASSERT_NE(nullptr, session);
1139 EXPECT_EQ(1UL, session->stun_servers().size());
1140 EXPECT_EQ(0U, session->flags() & cricket::PORTALLOCATOR_ENABLE_IPV6);
1141 EXPECT_LT(0U, session->flags() & cricket::PORTALLOCATOR_DISABLE_TCP);
honghaiz60347052016-05-31 18:29:12 -07001142 EXPECT_LT(0U,
1143 session->flags() & cricket::PORTALLOCATOR_DISABLE_COSTLY_NETWORKS);
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001144}
1145
Taylor Brandstetterf8e65772016-06-27 17:20:15 -07001146// Test that the PeerConnection initializes the port allocator passed into it,
1147// and on the correct thread.
1148TEST_F(PeerConnectionInterfaceTest,
1149 CreatePeerConnectionInitializesPortAllocator) {
1150 rtc::Thread network_thread;
1151 network_thread.Start();
1152 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface> pc_factory(
1153 webrtc::CreatePeerConnectionFactory(
1154 &network_thread, rtc::Thread::Current(), rtc::Thread::Current(),
1155 nullptr, nullptr, nullptr));
1156 std::unique_ptr<cricket::FakePortAllocator> port_allocator(
1157 new cricket::FakePortAllocator(&network_thread, nullptr));
1158 cricket::FakePortAllocator* raw_port_allocator = port_allocator.get();
1159 PeerConnectionInterface::RTCConfiguration config;
1160 rtc::scoped_refptr<PeerConnectionInterface> pc(
1161 pc_factory->CreatePeerConnection(
1162 config, nullptr, std::move(port_allocator), nullptr, &observer_));
1163 // FakePortAllocator RTC_CHECKs that it's initialized on the right thread,
1164 // so all we have to do here is check that it's initialized.
1165 EXPECT_TRUE(raw_port_allocator->initialized());
1166}
1167
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001168TEST_F(PeerConnectionInterfaceTest, AddStreams) {
1169 CreatePeerConnection();
deadbeefab9b2d12015-10-14 11:33:11 -07001170 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001171 AddVoiceStream(kStreamLabel2);
1172 ASSERT_EQ(2u, pc_->local_streams()->count());
1173
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001174 // Test we can add multiple local streams to one peerconnection.
zhihuang9763d562016-08-05 11:14:50 -07001175 rtc::scoped_refptr<MediaStreamInterface> stream(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001176 pc_factory_->CreateLocalMediaStream(kStreamLabel3));
zhihuang9763d562016-08-05 11:14:50 -07001177 rtc::scoped_refptr<AudioTrackInterface> audio_track(
1178 pc_factory_->CreateAudioTrack(kStreamLabel3,
1179 static_cast<AudioSourceInterface*>(NULL)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001180 stream->AddTrack(audio_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +00001181 EXPECT_TRUE(pc_->AddStream(stream));
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001182 EXPECT_EQ(3u, pc_->local_streams()->count());
1183
1184 // Remove the third stream.
1185 pc_->RemoveStream(pc_->local_streams()->at(2));
1186 EXPECT_EQ(2u, pc_->local_streams()->count());
1187
1188 // Remove the second stream.
1189 pc_->RemoveStream(pc_->local_streams()->at(1));
1190 EXPECT_EQ(1u, pc_->local_streams()->count());
1191
1192 // Remove the first stream.
1193 pc_->RemoveStream(pc_->local_streams()->at(0));
1194 EXPECT_EQ(0u, pc_->local_streams()->count());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001195}
1196
deadbeefab9b2d12015-10-14 11:33:11 -07001197// Test that the created offer includes streams we added.
1198TEST_F(PeerConnectionInterfaceTest, AddedStreamsPresentInOffer) {
1199 CreatePeerConnection();
1200 AddAudioVideoStream(kStreamLabel1, "audio_track", "video_track");
kwibergd1fe2812016-04-27 06:47:29 -07001201 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001202 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07001203
1204 const cricket::ContentInfo* audio_content =
1205 cricket::GetFirstAudioContent(offer->description());
1206 const cricket::AudioContentDescription* audio_desc =
1207 static_cast<const cricket::AudioContentDescription*>(
1208 audio_content->description);
1209 EXPECT_TRUE(
1210 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
1211
1212 const cricket::ContentInfo* video_content =
1213 cricket::GetFirstVideoContent(offer->description());
1214 const cricket::VideoContentDescription* video_desc =
1215 static_cast<const cricket::VideoContentDescription*>(
1216 video_content->description);
1217 EXPECT_TRUE(
1218 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1219
1220 // Add another stream and ensure the offer includes both the old and new
1221 // streams.
1222 AddAudioVideoStream(kStreamLabel2, "audio_track2", "video_track2");
kwiberg2bbff992016-03-16 11:03:04 -07001223 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07001224
1225 audio_content = cricket::GetFirstAudioContent(offer->description());
1226 audio_desc = static_cast<const cricket::AudioContentDescription*>(
1227 audio_content->description);
1228 EXPECT_TRUE(
1229 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
1230 EXPECT_TRUE(
1231 ContainsTrack(audio_desc->streams(), kStreamLabel2, "audio_track2"));
1232
1233 video_content = cricket::GetFirstVideoContent(offer->description());
1234 video_desc = static_cast<const cricket::VideoContentDescription*>(
1235 video_content->description);
1236 EXPECT_TRUE(
1237 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1238 EXPECT_TRUE(
1239 ContainsTrack(video_desc->streams(), kStreamLabel2, "video_track2"));
1240}
1241
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001242TEST_F(PeerConnectionInterfaceTest, RemoveStream) {
1243 CreatePeerConnection();
deadbeefab9b2d12015-10-14 11:33:11 -07001244 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001245 ASSERT_EQ(1u, pc_->local_streams()->count());
1246 pc_->RemoveStream(pc_->local_streams()->at(0));
1247 EXPECT_EQ(0u, pc_->local_streams()->count());
1248}
1249
deadbeefe1f9d832016-01-14 15:35:42 -08001250// Test for AddTrack and RemoveTrack methods.
1251// Tests that the created offer includes tracks we added,
1252// and that the RtpSenders are created correctly.
1253// Also tests that RemoveTrack removes the tracks from subsequent offers.
1254TEST_F(PeerConnectionInterfaceTest, AddTrackRemoveTrack) {
1255 CreatePeerConnection();
1256 // Create a dummy stream, so tracks share a stream label.
zhihuang9763d562016-08-05 11:14:50 -07001257 rtc::scoped_refptr<MediaStreamInterface> stream(
deadbeefe1f9d832016-01-14 15:35:42 -08001258 pc_factory_->CreateLocalMediaStream(kStreamLabel1));
1259 std::vector<MediaStreamInterface*> stream_list;
1260 stream_list.push_back(stream.get());
zhihuang9763d562016-08-05 11:14:50 -07001261 rtc::scoped_refptr<AudioTrackInterface> audio_track(
deadbeefe1f9d832016-01-14 15:35:42 -08001262 pc_factory_->CreateAudioTrack("audio_track", nullptr));
zhihuang9763d562016-08-05 11:14:50 -07001263 rtc::scoped_refptr<VideoTrackInterface> video_track(
1264 pc_factory_->CreateVideoTrack(
1265 "video_track",
1266 pc_factory_->CreateVideoSource(new cricket::FakeVideoCapturer())));
deadbeefe1f9d832016-01-14 15:35:42 -08001267 auto audio_sender = pc_->AddTrack(audio_track, stream_list);
1268 auto video_sender = pc_->AddTrack(video_track, stream_list);
deadbeefa601f5c2016-06-06 14:27:39 -07001269 EXPECT_EQ(1UL, audio_sender->stream_ids().size());
1270 EXPECT_EQ(kStreamLabel1, audio_sender->stream_ids()[0]);
deadbeefe1f9d832016-01-14 15:35:42 -08001271 EXPECT_EQ("audio_track", audio_sender->id());
1272 EXPECT_EQ(audio_track, audio_sender->track());
deadbeefa601f5c2016-06-06 14:27:39 -07001273 EXPECT_EQ(1UL, video_sender->stream_ids().size());
1274 EXPECT_EQ(kStreamLabel1, video_sender->stream_ids()[0]);
deadbeefe1f9d832016-01-14 15:35:42 -08001275 EXPECT_EQ("video_track", video_sender->id());
1276 EXPECT_EQ(video_track, video_sender->track());
1277
1278 // Now create an offer and check for the senders.
kwibergd1fe2812016-04-27 06:47:29 -07001279 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001280 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefe1f9d832016-01-14 15:35:42 -08001281
1282 const cricket::ContentInfo* audio_content =
1283 cricket::GetFirstAudioContent(offer->description());
1284 const cricket::AudioContentDescription* audio_desc =
1285 static_cast<const cricket::AudioContentDescription*>(
1286 audio_content->description);
1287 EXPECT_TRUE(
1288 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
1289
1290 const cricket::ContentInfo* video_content =
1291 cricket::GetFirstVideoContent(offer->description());
1292 const cricket::VideoContentDescription* video_desc =
1293 static_cast<const cricket::VideoContentDescription*>(
1294 video_content->description);
1295 EXPECT_TRUE(
1296 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1297
1298 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
1299
1300 // Now try removing the tracks.
1301 EXPECT_TRUE(pc_->RemoveTrack(audio_sender));
1302 EXPECT_TRUE(pc_->RemoveTrack(video_sender));
1303
1304 // Create a new offer and ensure it doesn't contain the removed senders.
kwiberg2bbff992016-03-16 11:03:04 -07001305 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefe1f9d832016-01-14 15:35:42 -08001306
1307 audio_content = cricket::GetFirstAudioContent(offer->description());
1308 audio_desc = static_cast<const cricket::AudioContentDescription*>(
1309 audio_content->description);
1310 EXPECT_FALSE(
1311 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
1312
1313 video_content = cricket::GetFirstVideoContent(offer->description());
1314 video_desc = static_cast<const cricket::VideoContentDescription*>(
1315 video_content->description);
1316 EXPECT_FALSE(
1317 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1318
1319 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
1320
1321 // Calling RemoveTrack on a sender no longer attached to a PeerConnection
1322 // should return false.
1323 EXPECT_FALSE(pc_->RemoveTrack(audio_sender));
1324 EXPECT_FALSE(pc_->RemoveTrack(video_sender));
1325}
1326
1327// Test creating senders without a stream specified,
1328// expecting a random stream ID to be generated.
1329TEST_F(PeerConnectionInterfaceTest, AddTrackWithoutStream) {
1330 CreatePeerConnection();
1331 // Create a dummy stream, so tracks share a stream label.
zhihuang9763d562016-08-05 11:14:50 -07001332 rtc::scoped_refptr<AudioTrackInterface> audio_track(
deadbeefe1f9d832016-01-14 15:35:42 -08001333 pc_factory_->CreateAudioTrack("audio_track", nullptr));
zhihuang9763d562016-08-05 11:14:50 -07001334 rtc::scoped_refptr<VideoTrackInterface> video_track(
1335 pc_factory_->CreateVideoTrack(
1336 "video_track",
1337 pc_factory_->CreateVideoSource(new cricket::FakeVideoCapturer())));
deadbeefe1f9d832016-01-14 15:35:42 -08001338 auto audio_sender =
1339 pc_->AddTrack(audio_track, std::vector<MediaStreamInterface*>());
1340 auto video_sender =
1341 pc_->AddTrack(video_track, std::vector<MediaStreamInterface*>());
1342 EXPECT_EQ("audio_track", audio_sender->id());
1343 EXPECT_EQ(audio_track, audio_sender->track());
1344 EXPECT_EQ("video_track", video_sender->id());
1345 EXPECT_EQ(video_track, video_sender->track());
1346 // If the ID is truly a random GUID, it should be infinitely unlikely they
1347 // will be the same.
deadbeefa601f5c2016-06-06 14:27:39 -07001348 EXPECT_NE(video_sender->stream_ids(), audio_sender->stream_ids());
deadbeefe1f9d832016-01-14 15:35:42 -08001349}
1350
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001351TEST_F(PeerConnectionInterfaceTest, CreateOfferReceiveAnswer) {
1352 InitiateCall();
1353 WaitAndVerifyOnAddStream(kStreamLabel1);
1354 VerifyRemoteRtpHeaderExtensions();
1355}
1356
1357TEST_F(PeerConnectionInterfaceTest, CreateOfferReceivePrAnswerAndAnswer) {
1358 CreatePeerConnection();
deadbeefab9b2d12015-10-14 11:33:11 -07001359 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001360 CreateOfferAsLocalDescription();
1361 std::string offer;
1362 EXPECT_TRUE(pc_->local_description()->ToString(&offer));
1363 CreatePrAnswerAndAnswerAsRemoteDescription(offer);
1364 WaitAndVerifyOnAddStream(kStreamLabel1);
1365}
1366
1367TEST_F(PeerConnectionInterfaceTest, ReceiveOfferCreateAnswer) {
1368 CreatePeerConnection();
deadbeefab9b2d12015-10-14 11:33:11 -07001369 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001370
1371 CreateOfferAsRemoteDescription();
1372 CreateAnswerAsLocalDescription();
1373
1374 WaitAndVerifyOnAddStream(kStreamLabel1);
1375}
1376
1377TEST_F(PeerConnectionInterfaceTest, ReceiveOfferCreatePrAnswerAndAnswer) {
1378 CreatePeerConnection();
deadbeefab9b2d12015-10-14 11:33:11 -07001379 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001380
1381 CreateOfferAsRemoteDescription();
1382 CreatePrAnswerAsLocalDescription();
1383 CreateAnswerAsLocalDescription();
1384
1385 WaitAndVerifyOnAddStream(kStreamLabel1);
1386}
1387
1388TEST_F(PeerConnectionInterfaceTest, Renegotiate) {
1389 InitiateCall();
1390 ASSERT_EQ(1u, pc_->remote_streams()->count());
1391 pc_->RemoveStream(pc_->local_streams()->at(0));
1392 CreateOfferReceiveAnswer();
1393 EXPECT_EQ(0u, pc_->remote_streams()->count());
deadbeefab9b2d12015-10-14 11:33:11 -07001394 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001395 CreateOfferReceiveAnswer();
1396}
1397
1398// Tests that after negotiating an audio only call, the respondent can perform a
1399// renegotiation that removes the audio stream.
1400TEST_F(PeerConnectionInterfaceTest, RenegotiateAudioOnly) {
1401 CreatePeerConnection();
1402 AddVoiceStream(kStreamLabel1);
1403 CreateOfferAsRemoteDescription();
1404 CreateAnswerAsLocalDescription();
1405
1406 ASSERT_EQ(1u, pc_->remote_streams()->count());
1407 pc_->RemoveStream(pc_->local_streams()->at(0));
1408 CreateOfferReceiveAnswer();
1409 EXPECT_EQ(0u, pc_->remote_streams()->count());
1410}
1411
1412// Test that candidates are generated and that we can parse our own candidates.
1413TEST_F(PeerConnectionInterfaceTest, IceCandidates) {
1414 CreatePeerConnection();
1415
1416 EXPECT_FALSE(pc_->AddIceCandidate(observer_.last_candidate_.get()));
1417 // SetRemoteDescription takes ownership of offer.
kwibergd1fe2812016-04-27 06:47:29 -07001418 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefab9b2d12015-10-14 11:33:11 -07001419 AddVideoStream(kStreamLabel1);
deadbeefc80741f2015-10-22 13:14:45 -07001420 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
kwiberg2bbff992016-03-16 11:03:04 -07001421 EXPECT_TRUE(DoSetRemoteDescription(offer.release()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001422
1423 // SetLocalDescription takes ownership of answer.
kwibergd1fe2812016-04-27 06:47:29 -07001424 std::unique_ptr<SessionDescriptionInterface> answer;
deadbeefc80741f2015-10-22 13:14:45 -07001425 EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
kwiberg2bbff992016-03-16 11:03:04 -07001426 EXPECT_TRUE(DoSetLocalDescription(answer.release()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001427
1428 EXPECT_TRUE_WAIT(observer_.last_candidate_.get() != NULL, kTimeout);
1429 EXPECT_TRUE_WAIT(observer_.ice_complete_, kTimeout);
1430
1431 EXPECT_TRUE(pc_->AddIceCandidate(observer_.last_candidate_.get()));
1432}
1433
deadbeefab9b2d12015-10-14 11:33:11 -07001434// Test that CreateOffer and CreateAnswer will fail if the track labels are
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001435// not unique.
1436TEST_F(PeerConnectionInterfaceTest, CreateOfferAnswerWithInvalidStream) {
1437 CreatePeerConnection();
1438 // Create a regular offer for the CreateAnswer test later.
kwibergd1fe2812016-04-27 06:47:29 -07001439 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefc80741f2015-10-22 13:14:45 -07001440 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
kwiberg2bbff992016-03-16 11:03:04 -07001441 EXPECT_TRUE(offer);
1442 offer.reset();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001443
1444 // Create a local stream with audio&video tracks having same label.
1445 AddAudioVideoStream(kStreamLabel1, "track_label", "track_label");
1446
1447 // Test CreateOffer
deadbeefc80741f2015-10-22 13:14:45 -07001448 EXPECT_FALSE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001449
1450 // Test CreateAnswer
kwibergd1fe2812016-04-27 06:47:29 -07001451 std::unique_ptr<SessionDescriptionInterface> answer;
deadbeefc80741f2015-10-22 13:14:45 -07001452 EXPECT_FALSE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001453}
1454
1455// Test that we will get different SSRCs for each tracks in the offer and answer
1456// we created.
1457TEST_F(PeerConnectionInterfaceTest, SsrcInOfferAnswer) {
1458 CreatePeerConnection();
1459 // Create a local stream with audio&video tracks having different labels.
1460 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
1461
1462 // Test CreateOffer
kwibergd1fe2812016-04-27 06:47:29 -07001463 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001464 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001465 int audio_ssrc = 0;
1466 int video_ssrc = 0;
1467 EXPECT_TRUE(GetFirstSsrc(GetFirstAudioContent(offer->description()),
1468 &audio_ssrc));
1469 EXPECT_TRUE(GetFirstSsrc(GetFirstVideoContent(offer->description()),
1470 &video_ssrc));
1471 EXPECT_NE(audio_ssrc, video_ssrc);
1472
1473 // Test CreateAnswer
1474 EXPECT_TRUE(DoSetRemoteDescription(offer.release()));
kwibergd1fe2812016-04-27 06:47:29 -07001475 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -07001476 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001477 audio_ssrc = 0;
1478 video_ssrc = 0;
1479 EXPECT_TRUE(GetFirstSsrc(GetFirstAudioContent(answer->description()),
1480 &audio_ssrc));
1481 EXPECT_TRUE(GetFirstSsrc(GetFirstVideoContent(answer->description()),
1482 &video_ssrc));
1483 EXPECT_NE(audio_ssrc, video_ssrc);
1484}
1485
deadbeefeb459812015-12-15 19:24:43 -08001486// Test that it's possible to call AddTrack on a MediaStream after adding
1487// the stream to a PeerConnection.
1488// TODO(deadbeef): Remove this test once this behavior is no longer supported.
1489TEST_F(PeerConnectionInterfaceTest, AddTrackAfterAddStream) {
1490 CreatePeerConnection();
1491 // Create audio stream and add to PeerConnection.
1492 AddVoiceStream(kStreamLabel1);
1493 MediaStreamInterface* stream = pc_->local_streams()->at(0);
1494
1495 // Add video track to the audio-only stream.
zhihuang9763d562016-08-05 11:14:50 -07001496 rtc::scoped_refptr<VideoTrackInterface> video_track(
1497 pc_factory_->CreateVideoTrack(
1498 "video_label",
1499 pc_factory_->CreateVideoSource(new cricket::FakeVideoCapturer())));
deadbeefeb459812015-12-15 19:24:43 -08001500 stream->AddTrack(video_track.get());
1501
kwibergd1fe2812016-04-27 06:47:29 -07001502 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001503 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefeb459812015-12-15 19:24:43 -08001504
1505 const cricket::MediaContentDescription* video_desc =
1506 cricket::GetFirstVideoContentDescription(offer->description());
1507 EXPECT_TRUE(video_desc != nullptr);
1508}
1509
1510// Test that it's possible to call RemoveTrack on a MediaStream after adding
1511// the stream to a PeerConnection.
1512// TODO(deadbeef): Remove this test once this behavior is no longer supported.
1513TEST_F(PeerConnectionInterfaceTest, RemoveTrackAfterAddStream) {
1514 CreatePeerConnection();
1515 // Create audio/video stream and add to PeerConnection.
1516 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
1517 MediaStreamInterface* stream = pc_->local_streams()->at(0);
1518
1519 // Remove the video track.
1520 stream->RemoveTrack(stream->GetVideoTracks()[0]);
1521
kwibergd1fe2812016-04-27 06:47:29 -07001522 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001523 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefeb459812015-12-15 19:24:43 -08001524
1525 const cricket::MediaContentDescription* video_desc =
1526 cricket::GetFirstVideoContentDescription(offer->description());
1527 EXPECT_TRUE(video_desc == nullptr);
1528}
1529
deadbeefbd7d8f72015-12-18 16:58:44 -08001530// Test creating a sender with a stream ID, and ensure the ID is populated
1531// in the offer.
1532TEST_F(PeerConnectionInterfaceTest, CreateSenderWithStream) {
1533 CreatePeerConnection();
1534 pc_->CreateSender("video", kStreamLabel1);
1535
kwibergd1fe2812016-04-27 06:47:29 -07001536 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001537 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefbd7d8f72015-12-18 16:58:44 -08001538
1539 const cricket::MediaContentDescription* video_desc =
1540 cricket::GetFirstVideoContentDescription(offer->description());
1541 ASSERT_TRUE(video_desc != nullptr);
1542 ASSERT_EQ(1u, video_desc->streams().size());
1543 EXPECT_EQ(kStreamLabel1, video_desc->streams()[0].sync_label);
1544}
1545
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001546// Test that we can specify a certain track that we want statistics about.
1547TEST_F(PeerConnectionInterfaceTest, GetStatsForSpecificTrack) {
1548 InitiateCall();
1549 ASSERT_LT(0u, pc_->remote_streams()->count());
1550 ASSERT_LT(0u, pc_->remote_streams()->at(0)->GetAudioTracks().size());
zhihuang9763d562016-08-05 11:14:50 -07001551 rtc::scoped_refptr<MediaStreamTrackInterface> remote_audio =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001552 pc_->remote_streams()->at(0)->GetAudioTracks()[0];
1553 EXPECT_TRUE(DoGetStats(remote_audio));
1554
1555 // Remove the stream. Since we are sending to our selves the local
1556 // and the remote stream is the same.
1557 pc_->RemoveStream(pc_->local_streams()->at(0));
1558 // Do a re-negotiation.
1559 CreateOfferReceiveAnswer();
1560
1561 ASSERT_EQ(0u, pc_->remote_streams()->count());
1562
1563 // Test that we still can get statistics for the old track. Even if it is not
1564 // sent any longer.
1565 EXPECT_TRUE(DoGetStats(remote_audio));
1566}
1567
1568// Test that we can get stats on a video track.
1569TEST_F(PeerConnectionInterfaceTest, GetStatsForVideoTrack) {
1570 InitiateCall();
1571 ASSERT_LT(0u, pc_->remote_streams()->count());
1572 ASSERT_LT(0u, pc_->remote_streams()->at(0)->GetVideoTracks().size());
zhihuang9763d562016-08-05 11:14:50 -07001573 rtc::scoped_refptr<MediaStreamTrackInterface> remote_video =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001574 pc_->remote_streams()->at(0)->GetVideoTracks()[0];
1575 EXPECT_TRUE(DoGetStats(remote_video));
1576}
1577
1578// Test that we don't get statistics for an invalid track.
tommi@webrtc.org908f57e2014-07-21 11:44:39 +00001579// TODO(tommi): Fix this test. DoGetStats will return true
1580// for the unknown track (since GetStats is async), but no
1581// data is returned for the track.
1582TEST_F(PeerConnectionInterfaceTest, DISABLED_GetStatsForInvalidTrack) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001583 InitiateCall();
zhihuang9763d562016-08-05 11:14:50 -07001584 rtc::scoped_refptr<AudioTrackInterface> unknown_audio_track(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001585 pc_factory_->CreateAudioTrack("unknown track", NULL));
1586 EXPECT_FALSE(DoGetStats(unknown_audio_track));
1587}
1588
1589// This test setup two RTP data channels in loop back.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001590TEST_F(PeerConnectionInterfaceTest, TestDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001591 FakeConstraints constraints;
1592 constraints.SetAllowRtpDataChannels();
1593 CreatePeerConnection(&constraints);
zhihuang9763d562016-08-05 11:14:50 -07001594 rtc::scoped_refptr<DataChannelInterface> data1 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001595 pc_->CreateDataChannel("test1", NULL);
zhihuang9763d562016-08-05 11:14:50 -07001596 rtc::scoped_refptr<DataChannelInterface> data2 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001597 pc_->CreateDataChannel("test2", NULL);
1598 ASSERT_TRUE(data1 != NULL);
kwibergd1fe2812016-04-27 06:47:29 -07001599 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001600 new MockDataChannelObserver(data1));
kwibergd1fe2812016-04-27 06:47:29 -07001601 std::unique_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001602 new MockDataChannelObserver(data2));
1603
1604 EXPECT_EQ(DataChannelInterface::kConnecting, data1->state());
1605 EXPECT_EQ(DataChannelInterface::kConnecting, data2->state());
1606 std::string data_to_send1 = "testing testing";
1607 std::string data_to_send2 = "testing something else";
1608 EXPECT_FALSE(data1->Send(DataBuffer(data_to_send1)));
1609
1610 CreateOfferReceiveAnswer();
1611 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1612 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
1613
1614 EXPECT_EQ(DataChannelInterface::kOpen, data1->state());
1615 EXPECT_EQ(DataChannelInterface::kOpen, data2->state());
1616 EXPECT_TRUE(data1->Send(DataBuffer(data_to_send1)));
1617 EXPECT_TRUE(data2->Send(DataBuffer(data_to_send2)));
1618
1619 EXPECT_EQ_WAIT(data_to_send1, observer1->last_message(), kTimeout);
1620 EXPECT_EQ_WAIT(data_to_send2, observer2->last_message(), kTimeout);
1621
1622 data1->Close();
1623 EXPECT_EQ(DataChannelInterface::kClosing, data1->state());
1624 CreateOfferReceiveAnswer();
1625 EXPECT_FALSE(observer1->IsOpen());
1626 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
1627 EXPECT_TRUE(observer2->IsOpen());
1628
1629 data_to_send2 = "testing something else again";
1630 EXPECT_TRUE(data2->Send(DataBuffer(data_to_send2)));
1631
1632 EXPECT_EQ_WAIT(data_to_send2, observer2->last_message(), kTimeout);
1633}
1634
1635// This test verifies that sendnig binary data over RTP data channels should
1636// fail.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001637TEST_F(PeerConnectionInterfaceTest, TestSendBinaryOnRtpDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001638 FakeConstraints constraints;
1639 constraints.SetAllowRtpDataChannels();
1640 CreatePeerConnection(&constraints);
zhihuang9763d562016-08-05 11:14:50 -07001641 rtc::scoped_refptr<DataChannelInterface> data1 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001642 pc_->CreateDataChannel("test1", NULL);
zhihuang9763d562016-08-05 11:14:50 -07001643 rtc::scoped_refptr<DataChannelInterface> data2 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001644 pc_->CreateDataChannel("test2", NULL);
1645 ASSERT_TRUE(data1 != NULL);
kwibergd1fe2812016-04-27 06:47:29 -07001646 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001647 new MockDataChannelObserver(data1));
kwibergd1fe2812016-04-27 06:47:29 -07001648 std::unique_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001649 new MockDataChannelObserver(data2));
1650
1651 EXPECT_EQ(DataChannelInterface::kConnecting, data1->state());
1652 EXPECT_EQ(DataChannelInterface::kConnecting, data2->state());
1653
1654 CreateOfferReceiveAnswer();
1655 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1656 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
1657
1658 EXPECT_EQ(DataChannelInterface::kOpen, data1->state());
1659 EXPECT_EQ(DataChannelInterface::kOpen, data2->state());
1660
jbaucheec21bd2016-03-20 06:15:43 -07001661 rtc::CopyOnWriteBuffer buffer("test", 4);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001662 EXPECT_FALSE(data1->Send(DataBuffer(buffer, true)));
1663}
1664
1665// This test setup a RTP data channels in loop back and test that a channel is
1666// opened even if the remote end answer with a zero SSRC.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001667TEST_F(PeerConnectionInterfaceTest, TestSendOnlyDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001668 FakeConstraints constraints;
1669 constraints.SetAllowRtpDataChannels();
1670 CreatePeerConnection(&constraints);
zhihuang9763d562016-08-05 11:14:50 -07001671 rtc::scoped_refptr<DataChannelInterface> data1 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001672 pc_->CreateDataChannel("test1", 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));
1675
1676 CreateOfferReceiveAnswerWithoutSsrc();
1677
1678 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1679
1680 data1->Close();
1681 EXPECT_EQ(DataChannelInterface::kClosing, data1->state());
1682 CreateOfferReceiveAnswerWithoutSsrc();
1683 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
1684 EXPECT_FALSE(observer1->IsOpen());
1685}
1686
1687// This test that if a data channel is added in an answer a receive only channel
1688// channel is created.
1689TEST_F(PeerConnectionInterfaceTest, TestReceiveOnlyDataChannel) {
1690 FakeConstraints constraints;
1691 constraints.SetAllowRtpDataChannels();
1692 CreatePeerConnection(&constraints);
1693
1694 std::string offer_label = "offer_channel";
zhihuang9763d562016-08-05 11:14:50 -07001695 rtc::scoped_refptr<DataChannelInterface> offer_channel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001696 pc_->CreateDataChannel(offer_label, NULL);
1697
1698 CreateOfferAsLocalDescription();
1699
1700 // Replace the data channel label in the offer and apply it as an answer.
1701 std::string receive_label = "answer_channel";
1702 std::string sdp;
1703 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001704 rtc::replace_substrs(offer_label.c_str(), offer_label.length(),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001705 receive_label.c_str(), receive_label.length(),
1706 &sdp);
1707 CreateAnswerAsRemoteDescription(sdp);
1708
1709 // Verify that a new incoming data channel has been created and that
1710 // it is open but can't we written to.
1711 ASSERT_TRUE(observer_.last_datachannel_ != NULL);
1712 DataChannelInterface* received_channel = observer_.last_datachannel_;
1713 EXPECT_EQ(DataChannelInterface::kConnecting, received_channel->state());
1714 EXPECT_EQ(receive_label, received_channel->label());
1715 EXPECT_FALSE(received_channel->Send(DataBuffer("something")));
1716
1717 // Verify that the channel we initially offered has been rejected.
1718 EXPECT_EQ(DataChannelInterface::kClosed, offer_channel->state());
1719
1720 // Do another offer / answer exchange and verify that the data channel is
1721 // opened.
1722 CreateOfferReceiveAnswer();
1723 EXPECT_EQ_WAIT(DataChannelInterface::kOpen, received_channel->state(),
1724 kTimeout);
1725}
1726
1727// This test that no data channel is returned if a reliable channel is
1728// requested.
1729// TODO(perkj): Remove this test once reliable channels are implemented.
1730TEST_F(PeerConnectionInterfaceTest, CreateReliableRtpDataChannelShouldFail) {
1731 FakeConstraints constraints;
1732 constraints.SetAllowRtpDataChannels();
1733 CreatePeerConnection(&constraints);
1734
1735 std::string label = "test";
1736 webrtc::DataChannelInit config;
1737 config.reliable = true;
zhihuang9763d562016-08-05 11:14:50 -07001738 rtc::scoped_refptr<DataChannelInterface> channel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001739 pc_->CreateDataChannel(label, &config);
1740 EXPECT_TRUE(channel == NULL);
1741}
1742
deadbeefab9b2d12015-10-14 11:33:11 -07001743// Verifies that duplicated label is not allowed for RTP data channel.
1744TEST_F(PeerConnectionInterfaceTest, RtpDuplicatedLabelNotAllowed) {
1745 FakeConstraints constraints;
1746 constraints.SetAllowRtpDataChannels();
1747 CreatePeerConnection(&constraints);
1748
1749 std::string label = "test";
zhihuang9763d562016-08-05 11:14:50 -07001750 rtc::scoped_refptr<DataChannelInterface> channel =
deadbeefab9b2d12015-10-14 11:33:11 -07001751 pc_->CreateDataChannel(label, nullptr);
1752 EXPECT_NE(channel, nullptr);
1753
zhihuang9763d562016-08-05 11:14:50 -07001754 rtc::scoped_refptr<DataChannelInterface> dup_channel =
deadbeefab9b2d12015-10-14 11:33:11 -07001755 pc_->CreateDataChannel(label, nullptr);
1756 EXPECT_EQ(dup_channel, nullptr);
1757}
1758
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001759// This tests that a SCTP data channel is returned using different
1760// DataChannelInit configurations.
1761TEST_F(PeerConnectionInterfaceTest, CreateSctpDataChannel) {
1762 FakeConstraints constraints;
1763 constraints.SetAllowDtlsSctpDataChannels();
1764 CreatePeerConnection(&constraints);
1765
1766 webrtc::DataChannelInit config;
1767
zhihuang9763d562016-08-05 11:14:50 -07001768 rtc::scoped_refptr<DataChannelInterface> channel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001769 pc_->CreateDataChannel("1", &config);
1770 EXPECT_TRUE(channel != NULL);
1771 EXPECT_TRUE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001772 EXPECT_TRUE(observer_.renegotiation_needed_);
1773 observer_.renegotiation_needed_ = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001774
1775 config.ordered = false;
1776 channel = pc_->CreateDataChannel("2", &config);
1777 EXPECT_TRUE(channel != NULL);
1778 EXPECT_TRUE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001779 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001780
1781 config.ordered = true;
1782 config.maxRetransmits = 0;
1783 channel = pc_->CreateDataChannel("3", &config);
1784 EXPECT_TRUE(channel != NULL);
1785 EXPECT_FALSE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001786 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001787
1788 config.maxRetransmits = -1;
1789 config.maxRetransmitTime = 0;
1790 channel = pc_->CreateDataChannel("4", &config);
1791 EXPECT_TRUE(channel != NULL);
1792 EXPECT_FALSE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001793 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001794}
1795
1796// This tests that no data channel is returned if both maxRetransmits and
1797// maxRetransmitTime are set for SCTP data channels.
1798TEST_F(PeerConnectionInterfaceTest,
1799 CreateSctpDataChannelShouldFailForInvalidConfig) {
1800 FakeConstraints constraints;
1801 constraints.SetAllowDtlsSctpDataChannels();
1802 CreatePeerConnection(&constraints);
1803
1804 std::string label = "test";
1805 webrtc::DataChannelInit config;
1806 config.maxRetransmits = 0;
1807 config.maxRetransmitTime = 0;
1808
zhihuang9763d562016-08-05 11:14:50 -07001809 rtc::scoped_refptr<DataChannelInterface> channel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001810 pc_->CreateDataChannel(label, &config);
1811 EXPECT_TRUE(channel == NULL);
1812}
1813
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001814// The test verifies that creating a SCTP data channel with an id already in use
1815// or out of range should fail.
1816TEST_F(PeerConnectionInterfaceTest,
1817 CreateSctpDataChannelWithInvalidIdShouldFail) {
1818 FakeConstraints constraints;
1819 constraints.SetAllowDtlsSctpDataChannels();
1820 CreatePeerConnection(&constraints);
1821
1822 webrtc::DataChannelInit config;
zhihuang9763d562016-08-05 11:14:50 -07001823 rtc::scoped_refptr<DataChannelInterface> channel;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001824
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001825 config.id = 1;
1826 channel = pc_->CreateDataChannel("1", &config);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001827 EXPECT_TRUE(channel != NULL);
1828 EXPECT_EQ(1, channel->id());
1829
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001830 channel = pc_->CreateDataChannel("x", &config);
1831 EXPECT_TRUE(channel == NULL);
1832
1833 config.id = cricket::kMaxSctpSid;
1834 channel = pc_->CreateDataChannel("max", &config);
1835 EXPECT_TRUE(channel != NULL);
1836 EXPECT_EQ(config.id, channel->id());
1837
1838 config.id = cricket::kMaxSctpSid + 1;
1839 channel = pc_->CreateDataChannel("x", &config);
1840 EXPECT_TRUE(channel == NULL);
1841}
1842
deadbeefab9b2d12015-10-14 11:33:11 -07001843// Verifies that duplicated label is allowed for SCTP data channel.
1844TEST_F(PeerConnectionInterfaceTest, SctpDuplicatedLabelAllowed) {
1845 FakeConstraints constraints;
1846 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
1847 true);
1848 CreatePeerConnection(&constraints);
1849
1850 std::string label = "test";
zhihuang9763d562016-08-05 11:14:50 -07001851 rtc::scoped_refptr<DataChannelInterface> channel =
deadbeefab9b2d12015-10-14 11:33:11 -07001852 pc_->CreateDataChannel(label, nullptr);
1853 EXPECT_NE(channel, nullptr);
1854
zhihuang9763d562016-08-05 11:14:50 -07001855 rtc::scoped_refptr<DataChannelInterface> dup_channel =
deadbeefab9b2d12015-10-14 11:33:11 -07001856 pc_->CreateDataChannel(label, nullptr);
1857 EXPECT_NE(dup_channel, nullptr);
1858}
1859
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001860// This test verifies that OnRenegotiationNeeded is fired for every new RTP
1861// DataChannel.
1862TEST_F(PeerConnectionInterfaceTest, RenegotiationNeededForNewRtpDataChannel) {
1863 FakeConstraints constraints;
1864 constraints.SetAllowRtpDataChannels();
1865 CreatePeerConnection(&constraints);
1866
zhihuang9763d562016-08-05 11:14:50 -07001867 rtc::scoped_refptr<DataChannelInterface> dc1 =
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001868 pc_->CreateDataChannel("test1", NULL);
1869 EXPECT_TRUE(observer_.renegotiation_needed_);
1870 observer_.renegotiation_needed_ = false;
1871
zhihuang9763d562016-08-05 11:14:50 -07001872 rtc::scoped_refptr<DataChannelInterface> dc2 =
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001873 pc_->CreateDataChannel("test2", NULL);
1874 EXPECT_TRUE(observer_.renegotiation_needed_);
1875}
1876
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001877// This test that a data channel closes when a PeerConnection is deleted/closed.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001878TEST_F(PeerConnectionInterfaceTest, DataChannelCloseWhenPeerConnectionClose) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001879 FakeConstraints constraints;
1880 constraints.SetAllowRtpDataChannels();
1881 CreatePeerConnection(&constraints);
1882
zhihuang9763d562016-08-05 11:14:50 -07001883 rtc::scoped_refptr<DataChannelInterface> data1 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001884 pc_->CreateDataChannel("test1", NULL);
zhihuang9763d562016-08-05 11:14:50 -07001885 rtc::scoped_refptr<DataChannelInterface> data2 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001886 pc_->CreateDataChannel("test2", NULL);
1887 ASSERT_TRUE(data1 != NULL);
kwibergd1fe2812016-04-27 06:47:29 -07001888 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001889 new MockDataChannelObserver(data1));
kwibergd1fe2812016-04-27 06:47:29 -07001890 std::unique_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001891 new MockDataChannelObserver(data2));
1892
1893 CreateOfferReceiveAnswer();
1894 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1895 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
1896
1897 ReleasePeerConnection();
1898 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
1899 EXPECT_EQ(DataChannelInterface::kClosed, data2->state());
1900}
1901
1902// This test that data channels can be rejected in an answer.
1903TEST_F(PeerConnectionInterfaceTest, TestRejectDataChannelInAnswer) {
1904 FakeConstraints constraints;
1905 constraints.SetAllowRtpDataChannels();
1906 CreatePeerConnection(&constraints);
1907
zhihuang9763d562016-08-05 11:14:50 -07001908 rtc::scoped_refptr<DataChannelInterface> offer_channel(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001909 pc_->CreateDataChannel("offer_channel", NULL));
1910
1911 CreateOfferAsLocalDescription();
1912
1913 // Create an answer where the m-line for data channels are rejected.
1914 std::string sdp;
1915 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
1916 webrtc::JsepSessionDescription* answer = new webrtc::JsepSessionDescription(
1917 SessionDescriptionInterface::kAnswer);
1918 EXPECT_TRUE(answer->Initialize(sdp, NULL));
1919 cricket::ContentInfo* data_info =
1920 answer->description()->GetContentByName("data");
1921 data_info->rejected = true;
1922
1923 DoSetRemoteDescription(answer);
1924 EXPECT_EQ(DataChannelInterface::kClosed, offer_channel->state());
1925}
1926
1927// Test that we can create a session description from an SDP string from
1928// FireFox, use it as a remote session description, generate an answer and use
1929// the answer as a local description.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07001930TEST_F(PeerConnectionInterfaceTest, ReceiveFireFoxOffer) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001931 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001932 FakeConstraints constraints;
1933 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
1934 true);
1935 CreatePeerConnection(&constraints);
1936 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
1937 SessionDescriptionInterface* desc =
1938 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
jbauchfabe2c92015-07-16 13:43:14 -07001939 webrtc::kFireFoxSdpOffer, nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001940 EXPECT_TRUE(DoSetSessionDescription(desc, false));
1941 CreateAnswerAsLocalDescription();
1942 ASSERT_TRUE(pc_->local_description() != NULL);
1943 ASSERT_TRUE(pc_->remote_description() != NULL);
1944
1945 const cricket::ContentInfo* content =
1946 cricket::GetFirstAudioContent(pc_->local_description()->description());
1947 ASSERT_TRUE(content != NULL);
1948 EXPECT_FALSE(content->rejected);
1949
1950 content =
1951 cricket::GetFirstVideoContent(pc_->local_description()->description());
1952 ASSERT_TRUE(content != NULL);
1953 EXPECT_FALSE(content->rejected);
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00001954#ifdef HAVE_SCTP
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001955 content =
1956 cricket::GetFirstDataContent(pc_->local_description()->description());
1957 ASSERT_TRUE(content != NULL);
1958 EXPECT_TRUE(content->rejected);
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00001959#endif
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001960}
1961
1962// Test that we can create an audio only offer and receive an answer with a
1963// limited set of audio codecs and receive an updated offer with more audio
1964// codecs, where the added codecs are not supported.
1965TEST_F(PeerConnectionInterfaceTest, ReceiveUpdatedAudioOfferWithBadCodecs) {
1966 CreatePeerConnection();
1967 AddVoiceStream("audio_label");
1968 CreateOfferAsLocalDescription();
1969
1970 SessionDescriptionInterface* answer =
1971 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
jbauchfabe2c92015-07-16 13:43:14 -07001972 webrtc::kAudioSdp, nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001973 EXPECT_TRUE(DoSetSessionDescription(answer, false));
1974
1975 SessionDescriptionInterface* updated_offer =
1976 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
jbauchfabe2c92015-07-16 13:43:14 -07001977 webrtc::kAudioSdpWithUnsupportedCodecs,
1978 nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001979 EXPECT_TRUE(DoSetSessionDescription(updated_offer, false));
1980 CreateAnswerAsLocalDescription();
1981}
1982
deadbeefc80741f2015-10-22 13:14:45 -07001983// Test that if we're receiving (but not sending) a track, subsequent offers
1984// will have m-lines with a=recvonly.
1985TEST_F(PeerConnectionInterfaceTest, CreateSubsequentRecvOnlyOffer) {
1986 FakeConstraints constraints;
1987 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
1988 true);
1989 CreatePeerConnection(&constraints);
1990 CreateAndSetRemoteOffer(kSdpStringWithStream1);
1991 CreateAnswerAsLocalDescription();
1992
1993 // At this point we should be receiving stream 1, but not sending anything.
1994 // A new offer should be recvonly.
kwibergd1fe2812016-04-27 06:47:29 -07001995 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefc80741f2015-10-22 13:14:45 -07001996 DoCreateOffer(&offer, nullptr);
1997
1998 const cricket::ContentInfo* video_content =
1999 cricket::GetFirstVideoContent(offer->description());
2000 const cricket::VideoContentDescription* video_desc =
2001 static_cast<const cricket::VideoContentDescription*>(
2002 video_content->description);
2003 ASSERT_EQ(cricket::MD_RECVONLY, video_desc->direction());
2004
2005 const cricket::ContentInfo* audio_content =
2006 cricket::GetFirstAudioContent(offer->description());
2007 const cricket::AudioContentDescription* audio_desc =
2008 static_cast<const cricket::AudioContentDescription*>(
2009 audio_content->description);
2010 ASSERT_EQ(cricket::MD_RECVONLY, audio_desc->direction());
2011}
2012
2013// Test that if we're receiving (but not sending) a track, and the
2014// offerToReceiveVideo/offerToReceiveAudio constraints are explicitly set to
2015// false, the generated m-lines will be a=inactive.
2016TEST_F(PeerConnectionInterfaceTest, CreateSubsequentInactiveOffer) {
2017 FakeConstraints constraints;
2018 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2019 true);
2020 CreatePeerConnection(&constraints);
2021 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2022 CreateAnswerAsLocalDescription();
2023
2024 // At this point we should be receiving stream 1, but not sending anything.
2025 // A new offer would be recvonly, but we'll set the "no receive" constraints
2026 // to make it inactive.
kwibergd1fe2812016-04-27 06:47:29 -07002027 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefc80741f2015-10-22 13:14:45 -07002028 FakeConstraints offer_constraints;
2029 offer_constraints.AddMandatory(
2030 webrtc::MediaConstraintsInterface::kOfferToReceiveVideo, false);
2031 offer_constraints.AddMandatory(
2032 webrtc::MediaConstraintsInterface::kOfferToReceiveAudio, false);
2033 DoCreateOffer(&offer, &offer_constraints);
2034
2035 const cricket::ContentInfo* video_content =
2036 cricket::GetFirstVideoContent(offer->description());
2037 const cricket::VideoContentDescription* video_desc =
2038 static_cast<const cricket::VideoContentDescription*>(
2039 video_content->description);
2040 ASSERT_EQ(cricket::MD_INACTIVE, video_desc->direction());
2041
2042 const cricket::ContentInfo* audio_content =
2043 cricket::GetFirstAudioContent(offer->description());
2044 const cricket::AudioContentDescription* audio_desc =
2045 static_cast<const cricket::AudioContentDescription*>(
2046 audio_content->description);
2047 ASSERT_EQ(cricket::MD_INACTIVE, audio_desc->direction());
2048}
2049
deadbeef653b8e02015-11-11 12:55:10 -08002050// Test that we can use SetConfiguration to change the ICE servers of the
2051// PortAllocator.
2052TEST_F(PeerConnectionInterfaceTest, SetConfigurationChangesIceServers) {
2053 CreatePeerConnection();
2054
2055 PeerConnectionInterface::RTCConfiguration config;
2056 PeerConnectionInterface::IceServer server;
2057 server.uri = "stun:test_hostname";
2058 config.servers.push_back(server);
2059 EXPECT_TRUE(pc_->SetConfiguration(config));
2060
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002061 EXPECT_EQ(1u, port_allocator_->stun_servers().size());
2062 EXPECT_EQ("test_hostname",
2063 port_allocator_->stun_servers().begin()->hostname());
deadbeef653b8e02015-11-11 12:55:10 -08002064}
2065
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002066TEST_F(PeerConnectionInterfaceTest, SetConfigurationChangesCandidateFilter) {
2067 CreatePeerConnection();
2068 PeerConnectionInterface::RTCConfiguration config;
2069 config.type = PeerConnectionInterface::kRelay;
2070 EXPECT_TRUE(pc_->SetConfiguration(config));
2071 EXPECT_EQ(cricket::CF_RELAY, port_allocator_->candidate_filter());
2072}
2073
2074// Test that when SetConfiguration changes both the pool size and other
2075// attributes, the pooled session is created with the updated attributes.
2076TEST_F(PeerConnectionInterfaceTest,
2077 SetConfigurationCreatesPooledSessionCorrectly) {
2078 CreatePeerConnection();
2079 PeerConnectionInterface::RTCConfiguration config;
2080 config.ice_candidate_pool_size = 1;
2081 PeerConnectionInterface::IceServer server;
2082 server.uri = kStunAddressOnly;
2083 config.servers.push_back(server);
2084 config.type = PeerConnectionInterface::kRelay;
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07002085 EXPECT_TRUE(pc_->SetConfiguration(config));
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002086
2087 const cricket::FakePortAllocatorSession* session =
2088 static_cast<const cricket::FakePortAllocatorSession*>(
2089 port_allocator_->GetPooledSession());
2090 ASSERT_NE(nullptr, session);
2091 EXPECT_EQ(1UL, session->stun_servers().size());
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002092}
2093
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002094// Test that PeerConnection::Close changes the states to closed and all remote
2095// tracks change state to ended.
2096TEST_F(PeerConnectionInterfaceTest, CloseAndTestStreamsAndStates) {
2097 // Initialize a PeerConnection and negotiate local and remote session
2098 // description.
2099 InitiateCall();
2100 ASSERT_EQ(1u, pc_->local_streams()->count());
2101 ASSERT_EQ(1u, pc_->remote_streams()->count());
2102
2103 pc_->Close();
2104
2105 EXPECT_EQ(PeerConnectionInterface::kClosed, pc_->signaling_state());
2106 EXPECT_EQ(PeerConnectionInterface::kIceConnectionClosed,
2107 pc_->ice_connection_state());
2108 EXPECT_EQ(PeerConnectionInterface::kIceGatheringComplete,
2109 pc_->ice_gathering_state());
2110
2111 EXPECT_EQ(1u, pc_->local_streams()->count());
2112 EXPECT_EQ(1u, pc_->remote_streams()->count());
2113
zhihuang9763d562016-08-05 11:14:50 -07002114 rtc::scoped_refptr<MediaStreamInterface> remote_stream =
2115 pc_->remote_streams()->at(0);
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002116 // Track state may be updated asynchronously.
perkjd61bf802016-03-24 03:16:19 -07002117 EXPECT_EQ_WAIT(MediaStreamTrackInterface::kEnded,
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002118 remote_stream->GetAudioTracks()[0]->state(), kTimeout);
2119 EXPECT_EQ_WAIT(MediaStreamTrackInterface::kEnded,
2120 remote_stream->GetVideoTracks()[0]->state(), kTimeout);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002121}
2122
2123// Test that PeerConnection methods fails gracefully after
2124// PeerConnection::Close has been called.
2125TEST_F(PeerConnectionInterfaceTest, CloseAndTestMethods) {
2126 CreatePeerConnection();
2127 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
2128 CreateOfferAsRemoteDescription();
2129 CreateAnswerAsLocalDescription();
2130
2131 ASSERT_EQ(1u, pc_->local_streams()->count());
zhihuang9763d562016-08-05 11:14:50 -07002132 rtc::scoped_refptr<MediaStreamInterface> local_stream =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002133 pc_->local_streams()->at(0);
2134
2135 pc_->Close();
2136
2137 pc_->RemoveStream(local_stream);
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +00002138 EXPECT_FALSE(pc_->AddStream(local_stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002139
2140 ASSERT_FALSE(local_stream->GetAudioTracks().empty());
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002141 rtc::scoped_refptr<webrtc::DtmfSenderInterface> dtmf_sender(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002142 pc_->CreateDtmfSender(local_stream->GetAudioTracks()[0]));
wu@webrtc.org66037362013-08-13 00:09:35 +00002143 EXPECT_TRUE(NULL == dtmf_sender); // local stream has been removed.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002144
2145 EXPECT_TRUE(pc_->CreateDataChannel("test", NULL) == NULL);
2146
2147 EXPECT_TRUE(pc_->local_description() != NULL);
2148 EXPECT_TRUE(pc_->remote_description() != NULL);
2149
kwibergd1fe2812016-04-27 06:47:29 -07002150 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07002151 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
kwibergd1fe2812016-04-27 06:47:29 -07002152 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -07002153 EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002154
2155 std::string sdp;
2156 ASSERT_TRUE(pc_->remote_description()->ToString(&sdp));
2157 SessionDescriptionInterface* remote_offer =
2158 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
2159 sdp, NULL);
2160 EXPECT_FALSE(DoSetRemoteDescription(remote_offer));
2161
2162 ASSERT_TRUE(pc_->local_description()->ToString(&sdp));
2163 SessionDescriptionInterface* local_offer =
2164 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
2165 sdp, NULL);
2166 EXPECT_FALSE(DoSetLocalDescription(local_offer));
2167}
2168
2169// Test that GetStats can still be called after PeerConnection::Close.
2170TEST_F(PeerConnectionInterfaceTest, CloseAndGetStats) {
2171 InitiateCall();
2172 pc_->Close();
2173 DoGetStats(NULL);
2174}
deadbeefab9b2d12015-10-14 11:33:11 -07002175
2176// NOTE: The series of tests below come from what used to be
2177// mediastreamsignaling_unittest.cc, and are mostly aimed at testing that
2178// setting a remote or local description has the expected effects.
2179
2180// This test verifies that the remote MediaStreams corresponding to a received
2181// SDP string is created. In this test the two separate MediaStreams are
2182// signaled.
2183TEST_F(PeerConnectionInterfaceTest, UpdateRemoteStreams) {
2184 FakeConstraints constraints;
2185 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2186 true);
2187 CreatePeerConnection(&constraints);
2188 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2189
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002190 rtc::scoped_refptr<StreamCollection> reference(CreateStreamCollection(1, 1));
deadbeefab9b2d12015-10-14 11:33:11 -07002191 EXPECT_TRUE(
2192 CompareStreamCollections(observer_.remote_streams(), reference.get()));
2193 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2194 EXPECT_TRUE(remote_stream->GetVideoTracks()[0]->GetSource() != nullptr);
2195
2196 // Create a session description based on another SDP with another
2197 // MediaStream.
2198 CreateAndSetRemoteOffer(kSdpStringWithStream1And2);
2199
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002200 rtc::scoped_refptr<StreamCollection> reference2(CreateStreamCollection(2, 1));
deadbeefab9b2d12015-10-14 11:33:11 -07002201 EXPECT_TRUE(
2202 CompareStreamCollections(observer_.remote_streams(), reference2.get()));
2203}
2204
2205// This test verifies that when remote tracks are added/removed from SDP, the
2206// created remote streams are updated appropriately.
2207TEST_F(PeerConnectionInterfaceTest,
2208 AddRemoveTrackFromExistingRemoteMediaStream) {
2209 FakeConstraints constraints;
2210 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2211 true);
2212 CreatePeerConnection(&constraints);
kwibergd1fe2812016-04-27 06:47:29 -07002213 std::unique_ptr<SessionDescriptionInterface> desc_ms1 =
kwiberg2bbff992016-03-16 11:03:04 -07002214 CreateSessionDescriptionAndReference(1, 1);
deadbeefab9b2d12015-10-14 11:33:11 -07002215 EXPECT_TRUE(DoSetRemoteDescription(desc_ms1.release()));
2216 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
2217 reference_collection_));
2218
2219 // Add extra audio and video tracks to the same MediaStream.
kwibergd1fe2812016-04-27 06:47:29 -07002220 std::unique_ptr<SessionDescriptionInterface> desc_ms1_two_tracks =
kwiberg2bbff992016-03-16 11:03:04 -07002221 CreateSessionDescriptionAndReference(2, 2);
deadbeefab9b2d12015-10-14 11:33:11 -07002222 EXPECT_TRUE(DoSetRemoteDescription(desc_ms1_two_tracks.release()));
2223 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
2224 reference_collection_));
zhihuang9763d562016-08-05 11:14:50 -07002225 rtc::scoped_refptr<AudioTrackInterface> audio_track2 =
perkjd61bf802016-03-24 03:16:19 -07002226 observer_.remote_streams()->at(0)->GetAudioTracks()[1];
2227 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, audio_track2->state());
zhihuang9763d562016-08-05 11:14:50 -07002228 rtc::scoped_refptr<VideoTrackInterface> video_track2 =
perkjd61bf802016-03-24 03:16:19 -07002229 observer_.remote_streams()->at(0)->GetVideoTracks()[1];
2230 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, video_track2->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002231
2232 // Remove the extra audio and video tracks.
kwibergd1fe2812016-04-27 06:47:29 -07002233 std::unique_ptr<SessionDescriptionInterface> desc_ms2 =
kwiberg2bbff992016-03-16 11:03:04 -07002234 CreateSessionDescriptionAndReference(1, 1);
perkjd61bf802016-03-24 03:16:19 -07002235 MockTrackObserver audio_track_observer(audio_track2);
2236 MockTrackObserver video_track_observer(video_track2);
2237
2238 EXPECT_CALL(audio_track_observer, OnChanged()).Times(Exactly(1));
2239 EXPECT_CALL(video_track_observer, OnChanged()).Times(Exactly(1));
deadbeefab9b2d12015-10-14 11:33:11 -07002240 EXPECT_TRUE(DoSetRemoteDescription(desc_ms2.release()));
2241 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
2242 reference_collection_));
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002243 // Track state may be updated asynchronously.
perkjd61bf802016-03-24 03:16:19 -07002244 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002245 audio_track2->state(), kTimeout);
2246 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
2247 video_track2->state(), kTimeout);
deadbeefab9b2d12015-10-14 11:33:11 -07002248}
2249
2250// This tests that remote tracks are ended if a local session description is set
2251// that rejects the media content type.
2252TEST_F(PeerConnectionInterfaceTest, RejectMediaContent) {
2253 FakeConstraints constraints;
2254 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2255 true);
2256 CreatePeerConnection(&constraints);
2257 // First create and set a remote offer, then reject its video content in our
2258 // answer.
2259 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2260 ASSERT_EQ(1u, observer_.remote_streams()->count());
2261 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2262 ASSERT_EQ(1u, remote_stream->GetVideoTracks().size());
2263 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2264
2265 rtc::scoped_refptr<webrtc::VideoTrackInterface> remote_video =
2266 remote_stream->GetVideoTracks()[0];
2267 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, remote_video->state());
2268 rtc::scoped_refptr<webrtc::AudioTrackInterface> remote_audio =
2269 remote_stream->GetAudioTracks()[0];
2270 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, remote_audio->state());
2271
kwibergd1fe2812016-04-27 06:47:29 -07002272 std::unique_ptr<SessionDescriptionInterface> local_answer;
kwiberg2bbff992016-03-16 11:03:04 -07002273 EXPECT_TRUE(DoCreateAnswer(&local_answer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07002274 cricket::ContentInfo* video_info =
2275 local_answer->description()->GetContentByName("video");
2276 video_info->rejected = true;
2277 EXPECT_TRUE(DoSetLocalDescription(local_answer.release()));
2278 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kEnded, remote_video->state());
2279 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, remote_audio->state());
2280
2281 // Now create an offer where we reject both video and audio.
kwibergd1fe2812016-04-27 06:47:29 -07002282 std::unique_ptr<SessionDescriptionInterface> local_offer;
kwiberg2bbff992016-03-16 11:03:04 -07002283 EXPECT_TRUE(DoCreateOffer(&local_offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07002284 video_info = local_offer->description()->GetContentByName("video");
2285 ASSERT_TRUE(video_info != nullptr);
2286 video_info->rejected = true;
2287 cricket::ContentInfo* audio_info =
2288 local_offer->description()->GetContentByName("audio");
2289 ASSERT_TRUE(audio_info != nullptr);
2290 audio_info->rejected = true;
2291 EXPECT_TRUE(DoSetLocalDescription(local_offer.release()));
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002292 // Track state may be updated asynchronously.
perkjd61bf802016-03-24 03:16:19 -07002293 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002294 remote_audio->state(), kTimeout);
2295 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
2296 remote_video->state(), kTimeout);
deadbeefab9b2d12015-10-14 11:33:11 -07002297}
2298
2299// This tests that we won't crash if the remote track has been removed outside
2300// of PeerConnection and then PeerConnection tries to reject the track.
2301TEST_F(PeerConnectionInterfaceTest, RemoveTrackThenRejectMediaContent) {
2302 FakeConstraints constraints;
2303 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2304 true);
2305 CreatePeerConnection(&constraints);
2306 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2307 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2308 remote_stream->RemoveTrack(remote_stream->GetVideoTracks()[0]);
2309 remote_stream->RemoveTrack(remote_stream->GetAudioTracks()[0]);
2310
kwibergd1fe2812016-04-27 06:47:29 -07002311 std::unique_ptr<SessionDescriptionInterface> local_answer(
deadbeefab9b2d12015-10-14 11:33:11 -07002312 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
2313 kSdpStringWithStream1, nullptr));
2314 cricket::ContentInfo* video_info =
2315 local_answer->description()->GetContentByName("video");
2316 video_info->rejected = true;
2317 cricket::ContentInfo* audio_info =
2318 local_answer->description()->GetContentByName("audio");
2319 audio_info->rejected = true;
2320 EXPECT_TRUE(DoSetLocalDescription(local_answer.release()));
2321
2322 // No crash is a pass.
2323}
2324
deadbeef5e97fb52015-10-15 12:49:08 -07002325// This tests that if a recvonly remote description is set, no remote streams
2326// will be created, even if the description contains SSRCs/MSIDs.
2327// See: https://code.google.com/p/webrtc/issues/detail?id=5054
2328TEST_F(PeerConnectionInterfaceTest, RecvonlyDescriptionDoesntCreateStream) {
2329 FakeConstraints constraints;
2330 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2331 true);
2332 CreatePeerConnection(&constraints);
2333
2334 std::string recvonly_offer = kSdpStringWithStream1;
2335 rtc::replace_substrs(kSendrecv, strlen(kSendrecv), kRecvonly,
2336 strlen(kRecvonly), &recvonly_offer);
2337 CreateAndSetRemoteOffer(recvonly_offer);
2338
2339 EXPECT_EQ(0u, observer_.remote_streams()->count());
2340}
2341
deadbeefab9b2d12015-10-14 11:33:11 -07002342// This tests that a default MediaStream is created if a remote session
2343// description doesn't contain any streams and no MSID support.
2344// It also tests that the default stream is updated if a video m-line is added
2345// in a subsequent session description.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002346TEST_F(PeerConnectionInterfaceTest, SdpWithoutMsidCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002347 FakeConstraints constraints;
2348 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2349 true);
2350 CreatePeerConnection(&constraints);
2351 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2352
2353 ASSERT_EQ(1u, observer_.remote_streams()->count());
2354 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2355
2356 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2357 EXPECT_EQ(0u, remote_stream->GetVideoTracks().size());
2358 EXPECT_EQ("default", remote_stream->label());
2359
2360 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2361 ASSERT_EQ(1u, observer_.remote_streams()->count());
2362 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2363 EXPECT_EQ("defaulta0", remote_stream->GetAudioTracks()[0]->id());
deadbeef884f5852016-01-15 09:20:04 -08002364 EXPECT_EQ(MediaStreamTrackInterface::kLive,
2365 remote_stream->GetAudioTracks()[0]->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002366 ASSERT_EQ(1u, remote_stream->GetVideoTracks().size());
2367 EXPECT_EQ("defaultv0", remote_stream->GetVideoTracks()[0]->id());
deadbeef884f5852016-01-15 09:20:04 -08002368 EXPECT_EQ(MediaStreamTrackInterface::kLive,
2369 remote_stream->GetVideoTracks()[0]->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002370}
2371
2372// This tests that a default MediaStream is created if a remote session
2373// description doesn't contain any streams and media direction is send only.
2374TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002375 SendOnlySdpWithoutMsidCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002376 FakeConstraints constraints;
2377 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2378 true);
2379 CreatePeerConnection(&constraints);
2380 CreateAndSetRemoteOffer(kSdpStringSendOnlyWithoutStreams);
2381
2382 ASSERT_EQ(1u, observer_.remote_streams()->count());
2383 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2384
2385 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2386 EXPECT_EQ(1u, remote_stream->GetVideoTracks().size());
2387 EXPECT_EQ("default", remote_stream->label());
2388}
2389
2390// This tests that it won't crash when PeerConnection tries to remove
2391// a remote track that as already been removed from the MediaStream.
2392TEST_F(PeerConnectionInterfaceTest, RemoveAlreadyGoneRemoteStream) {
2393 FakeConstraints constraints;
2394 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2395 true);
2396 CreatePeerConnection(&constraints);
2397 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2398 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2399 remote_stream->RemoveTrack(remote_stream->GetAudioTracks()[0]);
2400 remote_stream->RemoveTrack(remote_stream->GetVideoTracks()[0]);
2401
2402 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2403
2404 // No crash is a pass.
2405}
2406
2407// This tests that a default MediaStream is created if the remote session
2408// description doesn't contain any streams and don't contain an indication if
2409// MSID is supported.
2410TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002411 SdpWithoutMsidAndStreamsCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002412 FakeConstraints constraints;
2413 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2414 true);
2415 CreatePeerConnection(&constraints);
2416 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2417
2418 ASSERT_EQ(1u, observer_.remote_streams()->count());
2419 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2420 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2421 EXPECT_EQ(1u, remote_stream->GetVideoTracks().size());
2422}
2423
2424// This tests that a default MediaStream is not created if the remote session
2425// description doesn't contain any streams but does support MSID.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002426TEST_F(PeerConnectionInterfaceTest, SdpWithMsidDontCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002427 FakeConstraints constraints;
2428 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2429 true);
2430 CreatePeerConnection(&constraints);
2431 CreateAndSetRemoteOffer(kSdpStringWithMsidWithoutStreams);
2432 EXPECT_EQ(0u, observer_.remote_streams()->count());
2433}
2434
deadbeefbda7e0b2015-12-08 17:13:40 -08002435// This tests that when setting a new description, the old default tracks are
2436// not destroyed and recreated.
2437// See: https://bugs.chromium.org/p/webrtc/issues/detail?id=5250
Stefan Holmer102362b2016-03-18 09:39:07 +01002438TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002439 DefaultTracksNotDestroyedAndRecreated) {
deadbeefbda7e0b2015-12-08 17:13:40 -08002440 FakeConstraints constraints;
2441 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2442 true);
2443 CreatePeerConnection(&constraints);
2444 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2445
2446 ASSERT_EQ(1u, observer_.remote_streams()->count());
2447 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2448 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2449
2450 // Set the track to "disabled", then set a new description and ensure the
2451 // track is still disabled, which ensures it hasn't been recreated.
2452 remote_stream->GetAudioTracks()[0]->set_enabled(false);
2453 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2454 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2455 EXPECT_FALSE(remote_stream->GetAudioTracks()[0]->enabled());
2456}
2457
deadbeefab9b2d12015-10-14 11:33:11 -07002458// This tests that a default MediaStream is not created if a remote session
2459// description is updated to not have any MediaStreams.
2460TEST_F(PeerConnectionInterfaceTest, VerifyDefaultStreamIsNotCreated) {
2461 FakeConstraints constraints;
2462 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2463 true);
2464 CreatePeerConnection(&constraints);
2465 CreateAndSetRemoteOffer(kSdpStringWithStream1);
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002466 rtc::scoped_refptr<StreamCollection> reference(CreateStreamCollection(1, 1));
deadbeefab9b2d12015-10-14 11:33:11 -07002467 EXPECT_TRUE(
2468 CompareStreamCollections(observer_.remote_streams(), reference.get()));
2469
2470 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2471 EXPECT_EQ(0u, observer_.remote_streams()->count());
2472}
2473
2474// This tests that an RtpSender is created when the local description is set
2475// after adding a local stream.
2476// TODO(deadbeef): This test and the one below it need to be updated when
2477// an RtpSender's lifetime isn't determined by when a local description is set.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002478TEST_F(PeerConnectionInterfaceTest, LocalDescriptionChanged) {
deadbeefab9b2d12015-10-14 11:33:11 -07002479 FakeConstraints constraints;
2480 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2481 true);
2482 CreatePeerConnection(&constraints);
deadbeefab9b2d12015-10-14 11:33:11 -07002483
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002484 // Create an offer with 1 stream with 2 tracks of each type.
2485 rtc::scoped_refptr<StreamCollection> stream_collection =
2486 CreateStreamCollection(1, 2);
2487 pc_->AddStream(stream_collection->at(0));
2488 std::unique_ptr<SessionDescriptionInterface> offer;
2489 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2490 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
deadbeefab9b2d12015-10-14 11:33:11 -07002491
deadbeefab9b2d12015-10-14 11:33:11 -07002492 auto senders = pc_->GetSenders();
2493 EXPECT_EQ(4u, senders.size());
2494 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2495 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2496 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[1]));
2497 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[1]));
2498
2499 // Remove an audio and video track.
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002500 pc_->RemoveStream(stream_collection->at(0));
2501 stream_collection = CreateStreamCollection(1, 1);
2502 pc_->AddStream(stream_collection->at(0));
2503 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2504 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
2505
deadbeefab9b2d12015-10-14 11:33:11 -07002506 senders = pc_->GetSenders();
2507 EXPECT_EQ(2u, senders.size());
2508 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2509 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2510 EXPECT_FALSE(ContainsSender(senders, kAudioTracks[1]));
2511 EXPECT_FALSE(ContainsSender(senders, kVideoTracks[1]));
2512}
2513
2514// This tests that an RtpSender is created when the local description is set
2515// before adding a local stream.
2516TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002517 AddLocalStreamAfterLocalDescriptionChanged) {
deadbeefab9b2d12015-10-14 11:33:11 -07002518 FakeConstraints constraints;
2519 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2520 true);
2521 CreatePeerConnection(&constraints);
deadbeefab9b2d12015-10-14 11:33:11 -07002522
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002523 rtc::scoped_refptr<StreamCollection> stream_collection =
2524 CreateStreamCollection(1, 2);
2525 // Add a stream to create the offer, but remove it afterwards.
2526 pc_->AddStream(stream_collection->at(0));
2527 std::unique_ptr<SessionDescriptionInterface> offer;
2528 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2529 pc_->RemoveStream(stream_collection->at(0));
deadbeefab9b2d12015-10-14 11:33:11 -07002530
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002531 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
deadbeefab9b2d12015-10-14 11:33:11 -07002532 auto senders = pc_->GetSenders();
2533 EXPECT_EQ(0u, senders.size());
2534
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002535 pc_->AddStream(stream_collection->at(0));
deadbeefab9b2d12015-10-14 11:33:11 -07002536 senders = pc_->GetSenders();
2537 EXPECT_EQ(4u, senders.size());
2538 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2539 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2540 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[1]));
2541 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[1]));
2542}
2543
2544// This tests that the expected behavior occurs if the SSRC on a local track is
2545// changed when SetLocalDescription is called.
2546TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002547 ChangeSsrcOnTrackInLocalSessionDescription) {
deadbeefab9b2d12015-10-14 11:33:11 -07002548 FakeConstraints constraints;
2549 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2550 true);
2551 CreatePeerConnection(&constraints);
deadbeefab9b2d12015-10-14 11:33:11 -07002552
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002553 rtc::scoped_refptr<StreamCollection> stream_collection =
2554 CreateStreamCollection(2, 1);
2555 pc_->AddStream(stream_collection->at(0));
2556 std::unique_ptr<SessionDescriptionInterface> offer;
2557 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2558 // Grab a copy of the offer before it gets passed into the PC.
2559 std::unique_ptr<JsepSessionDescription> modified_offer(
2560 new JsepSessionDescription(JsepSessionDescription::kOffer));
2561 modified_offer->Initialize(offer->description()->Copy(), offer->session_id(),
2562 offer->session_version());
2563 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
deadbeefab9b2d12015-10-14 11:33:11 -07002564
deadbeefab9b2d12015-10-14 11:33:11 -07002565 auto senders = pc_->GetSenders();
2566 EXPECT_EQ(2u, senders.size());
2567 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2568 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2569
2570 // Change the ssrc of the audio and video track.
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002571 cricket::MediaContentDescription* desc =
2572 cricket::GetFirstAudioContentDescription(modified_offer->description());
2573 ASSERT_TRUE(desc != NULL);
2574 for (StreamParams& stream : desc->mutable_streams()) {
2575 for (unsigned int& ssrc : stream.ssrcs) {
2576 ++ssrc;
2577 }
2578 }
deadbeefab9b2d12015-10-14 11:33:11 -07002579
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002580 desc =
2581 cricket::GetFirstVideoContentDescription(modified_offer->description());
2582 ASSERT_TRUE(desc != NULL);
2583 for (StreamParams& stream : desc->mutable_streams()) {
2584 for (unsigned int& ssrc : stream.ssrcs) {
2585 ++ssrc;
2586 }
2587 }
2588
2589 EXPECT_TRUE(DoSetLocalDescription(modified_offer.release()));
deadbeefab9b2d12015-10-14 11:33:11 -07002590 senders = pc_->GetSenders();
2591 EXPECT_EQ(2u, senders.size());
2592 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2593 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2594 // TODO(deadbeef): Once RtpSenders expose parameters, check that the SSRC
2595 // changed.
2596}
2597
2598// This tests that the expected behavior occurs if a new session description is
2599// set with the same tracks, but on a different MediaStream.
Stefan Holmer55d6e7c2016-03-17 16:26:40 +01002600TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002601 SignalSameTracksInSeparateMediaStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002602 FakeConstraints constraints;
2603 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2604 true);
2605 CreatePeerConnection(&constraints);
deadbeefab9b2d12015-10-14 11:33:11 -07002606
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002607 rtc::scoped_refptr<StreamCollection> stream_collection =
2608 CreateStreamCollection(2, 1);
2609 pc_->AddStream(stream_collection->at(0));
2610 std::unique_ptr<SessionDescriptionInterface> offer;
2611 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2612 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
deadbeefab9b2d12015-10-14 11:33:11 -07002613
deadbeefab9b2d12015-10-14 11:33:11 -07002614 auto senders = pc_->GetSenders();
2615 EXPECT_EQ(2u, senders.size());
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002616 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0], kStreams[0]));
2617 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0], kStreams[0]));
deadbeefab9b2d12015-10-14 11:33:11 -07002618
2619 // Add a new MediaStream but with the same tracks as in the first stream.
2620 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream_1(
2621 webrtc::MediaStream::Create(kStreams[1]));
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002622 stream_1->AddTrack(stream_collection->at(0)->GetVideoTracks()[0]);
2623 stream_1->AddTrack(stream_collection->at(0)->GetAudioTracks()[0]);
deadbeefab9b2d12015-10-14 11:33:11 -07002624 pc_->AddStream(stream_1);
2625
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002626 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2627 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
deadbeefab9b2d12015-10-14 11:33:11 -07002628
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002629 auto new_senders = pc_->GetSenders();
2630 // Should be the same senders as before, but with updated stream id.
2631 // Note that this behavior is subject to change in the future.
2632 // We may decide the PC should ignore existing tracks in AddStream.
2633 EXPECT_EQ(senders, new_senders);
2634 EXPECT_TRUE(ContainsSender(new_senders, kAudioTracks[0], kStreams[1]));
2635 EXPECT_TRUE(ContainsSender(new_senders, kVideoTracks[0], kStreams[1]));
deadbeefab9b2d12015-10-14 11:33:11 -07002636}
2637
nisse51542be2016-02-12 02:27:06 -08002638class PeerConnectionMediaConfigTest : public testing::Test {
2639 protected:
2640 void SetUp() override {
nisseaf510af2016-03-21 08:20:42 -07002641 pcf_ = new rtc::RefCountedObject<PeerConnectionFactoryForTest>();
nisse51542be2016-02-12 02:27:06 -08002642 pcf_->Initialize();
2643 }
2644 const cricket::MediaConfig& TestCreatePeerConnection(
2645 const PeerConnectionInterface::RTCConfiguration& config,
2646 const MediaConstraintsInterface *constraints) {
2647 pcf_->create_media_controller_called_ = false;
2648
zhihuang9763d562016-08-05 11:14:50 -07002649 rtc::scoped_refptr<PeerConnectionInterface> pc(pcf_->CreatePeerConnection(
2650 config, constraints, nullptr, nullptr, &observer_));
nisse51542be2016-02-12 02:27:06 -08002651 EXPECT_TRUE(pc.get());
2652 EXPECT_TRUE(pcf_->create_media_controller_called_);
2653 return pcf_->create_media_controller_config_;
2654 }
2655
zhihuang9763d562016-08-05 11:14:50 -07002656 rtc::scoped_refptr<PeerConnectionFactoryForTest> pcf_;
nisse51542be2016-02-12 02:27:06 -08002657 MockPeerConnectionObserver observer_;
2658};
2659
2660// This test verifies the default behaviour with no constraints and a
2661// default RTCConfiguration.
2662TEST_F(PeerConnectionMediaConfigTest, TestDefaults) {
2663 PeerConnectionInterface::RTCConfiguration config;
2664 FakeConstraints constraints;
2665
2666 const cricket::MediaConfig& media_config =
2667 TestCreatePeerConnection(config, &constraints);
2668
2669 EXPECT_FALSE(media_config.enable_dscp);
nisse0db023a2016-03-01 04:29:59 -08002670 EXPECT_TRUE(media_config.video.enable_cpu_overuse_detection);
2671 EXPECT_FALSE(media_config.video.disable_prerenderer_smoothing);
2672 EXPECT_FALSE(media_config.video.suspend_below_min_bitrate);
nisse51542be2016-02-12 02:27:06 -08002673}
2674
2675// This test verifies the DSCP constraint is recognized and passed to
2676// the CreateMediaController call.
2677TEST_F(PeerConnectionMediaConfigTest, TestDscpConstraintTrue) {
2678 PeerConnectionInterface::RTCConfiguration config;
2679 FakeConstraints constraints;
2680
2681 constraints.AddOptional(webrtc::MediaConstraintsInterface::kEnableDscp, true);
2682 const cricket::MediaConfig& media_config =
2683 TestCreatePeerConnection(config, &constraints);
2684
2685 EXPECT_TRUE(media_config.enable_dscp);
2686}
2687
2688// This test verifies the cpu overuse detection constraint is
2689// recognized and passed to the CreateMediaController call.
2690TEST_F(PeerConnectionMediaConfigTest, TestCpuOveruseConstraintFalse) {
2691 PeerConnectionInterface::RTCConfiguration config;
2692 FakeConstraints constraints;
2693
2694 constraints.AddOptional(
2695 webrtc::MediaConstraintsInterface::kCpuOveruseDetection, false);
2696 const cricket::MediaConfig media_config =
2697 TestCreatePeerConnection(config, &constraints);
2698
nisse0db023a2016-03-01 04:29:59 -08002699 EXPECT_FALSE(media_config.video.enable_cpu_overuse_detection);
nisse51542be2016-02-12 02:27:06 -08002700}
2701
2702// This test verifies that the disable_prerenderer_smoothing flag is
2703// propagated from RTCConfiguration to the CreateMediaController call.
2704TEST_F(PeerConnectionMediaConfigTest, TestDisablePrerendererSmoothingTrue) {
2705 PeerConnectionInterface::RTCConfiguration config;
2706 FakeConstraints constraints;
2707
Niels Möller71bdda02016-03-31 12:59:59 +02002708 config.set_prerenderer_smoothing(false);
nisse51542be2016-02-12 02:27:06 -08002709 const cricket::MediaConfig& media_config =
2710 TestCreatePeerConnection(config, &constraints);
2711
nisse0db023a2016-03-01 04:29:59 -08002712 EXPECT_TRUE(media_config.video.disable_prerenderer_smoothing);
2713}
2714
2715// This test verifies the suspend below min bitrate constraint is
2716// recognized and passed to the CreateMediaController call.
2717TEST_F(PeerConnectionMediaConfigTest,
2718 TestSuspendBelowMinBitrateConstraintTrue) {
2719 PeerConnectionInterface::RTCConfiguration config;
2720 FakeConstraints constraints;
2721
2722 constraints.AddOptional(
2723 webrtc::MediaConstraintsInterface::kEnableVideoSuspendBelowMinBitrate,
2724 true);
2725 const cricket::MediaConfig media_config =
2726 TestCreatePeerConnection(config, &constraints);
2727
2728 EXPECT_TRUE(media_config.video.suspend_below_min_bitrate);
nisse51542be2016-02-12 02:27:06 -08002729}
2730
deadbeefab9b2d12015-10-14 11:33:11 -07002731// The following tests verify that session options are created correctly.
deadbeefc80741f2015-10-22 13:14:45 -07002732// TODO(deadbeef): Convert these tests to be more end-to-end. Instead of
2733// "verify options are converted correctly", should be "pass options into
2734// CreateOffer and verify the correct offer is produced."
deadbeefab9b2d12015-10-14 11:33:11 -07002735
2736TEST(CreateSessionOptionsTest, GetOptionsForOfferWithInvalidAudioOption) {
2737 RTCOfferAnswerOptions rtc_options;
2738 rtc_options.offer_to_receive_audio = RTCOfferAnswerOptions::kUndefined - 1;
2739
2740 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08002741 EXPECT_FALSE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002742
2743 rtc_options.offer_to_receive_audio =
2744 RTCOfferAnswerOptions::kMaxOfferToReceiveMedia + 1;
htaaac2dea2016-03-10 13:35:55 -08002745 EXPECT_FALSE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002746}
2747
2748TEST(CreateSessionOptionsTest, GetOptionsForOfferWithInvalidVideoOption) {
2749 RTCOfferAnswerOptions rtc_options;
2750 rtc_options.offer_to_receive_video = RTCOfferAnswerOptions::kUndefined - 1;
2751
2752 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08002753 EXPECT_FALSE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002754
2755 rtc_options.offer_to_receive_video =
2756 RTCOfferAnswerOptions::kMaxOfferToReceiveMedia + 1;
htaaac2dea2016-03-10 13:35:55 -08002757 EXPECT_FALSE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002758}
2759
2760// Test that a MediaSessionOptions is created for an offer if
deadbeefc80741f2015-10-22 13:14:45 -07002761// OfferToReceiveAudio and OfferToReceiveVideo options are set.
deadbeefab9b2d12015-10-14 11:33:11 -07002762TEST(CreateSessionOptionsTest, GetMediaSessionOptionsForOfferWithAudioVideo) {
2763 RTCOfferAnswerOptions rtc_options;
2764 rtc_options.offer_to_receive_audio = 1;
2765 rtc_options.offer_to_receive_video = 1;
2766
2767 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08002768 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002769 EXPECT_TRUE(options.has_audio());
2770 EXPECT_TRUE(options.has_video());
2771 EXPECT_TRUE(options.bundle_enabled);
2772}
2773
2774// Test that a correct MediaSessionOptions is created for an offer if
deadbeefc80741f2015-10-22 13:14:45 -07002775// OfferToReceiveAudio is set.
deadbeefab9b2d12015-10-14 11:33:11 -07002776TEST(CreateSessionOptionsTest, GetMediaSessionOptionsForOfferWithAudio) {
2777 RTCOfferAnswerOptions rtc_options;
2778 rtc_options.offer_to_receive_audio = 1;
2779
2780 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08002781 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002782 EXPECT_TRUE(options.has_audio());
2783 EXPECT_FALSE(options.has_video());
2784 EXPECT_TRUE(options.bundle_enabled);
2785}
2786
2787// Test that a correct MediaSessionOptions is created for an offer if
deadbeefc80741f2015-10-22 13:14:45 -07002788// the default OfferOptions are used.
deadbeefab9b2d12015-10-14 11:33:11 -07002789TEST(CreateSessionOptionsTest, GetDefaultMediaSessionOptionsForOffer) {
2790 RTCOfferAnswerOptions rtc_options;
2791
2792 cricket::MediaSessionOptions options;
deadbeef0ed85b22016-02-23 17:24:52 -08002793 options.transport_options["audio"] = cricket::TransportOptions();
2794 options.transport_options["video"] = cricket::TransportOptions();
htaaac2dea2016-03-10 13:35:55 -08002795 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefc80741f2015-10-22 13:14:45 -07002796 EXPECT_TRUE(options.has_audio());
deadbeefab9b2d12015-10-14 11:33:11 -07002797 EXPECT_FALSE(options.has_video());
deadbeefc80741f2015-10-22 13:14:45 -07002798 EXPECT_TRUE(options.bundle_enabled);
deadbeefab9b2d12015-10-14 11:33:11 -07002799 EXPECT_TRUE(options.vad_enabled);
deadbeef0ed85b22016-02-23 17:24:52 -08002800 EXPECT_FALSE(options.transport_options["audio"].ice_restart);
2801 EXPECT_FALSE(options.transport_options["video"].ice_restart);
deadbeefab9b2d12015-10-14 11:33:11 -07002802}
2803
2804// Test that a correct MediaSessionOptions is created for an offer if
deadbeefc80741f2015-10-22 13:14:45 -07002805// OfferToReceiveVideo is set.
deadbeefab9b2d12015-10-14 11:33:11 -07002806TEST(CreateSessionOptionsTest, GetMediaSessionOptionsForOfferWithVideo) {
2807 RTCOfferAnswerOptions rtc_options;
2808 rtc_options.offer_to_receive_audio = 0;
2809 rtc_options.offer_to_receive_video = 1;
2810
2811 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08002812 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002813 EXPECT_FALSE(options.has_audio());
2814 EXPECT_TRUE(options.has_video());
2815 EXPECT_TRUE(options.bundle_enabled);
2816}
2817
2818// Test that a correct MediaSessionOptions is created for an offer if
2819// UseRtpMux is set to false.
2820TEST(CreateSessionOptionsTest,
2821 GetMediaSessionOptionsForOfferWithBundleDisabled) {
2822 RTCOfferAnswerOptions rtc_options;
2823 rtc_options.offer_to_receive_audio = 1;
2824 rtc_options.offer_to_receive_video = 1;
2825 rtc_options.use_rtp_mux = false;
2826
2827 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08002828 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002829 EXPECT_TRUE(options.has_audio());
2830 EXPECT_TRUE(options.has_video());
2831 EXPECT_FALSE(options.bundle_enabled);
2832}
2833
2834// Test that a correct MediaSessionOptions is created to restart ice if
2835// IceRestart is set. It also tests that subsequent MediaSessionOptions don't
Taylor Brandstetterf475d362016-01-08 15:35:57 -08002836// have |audio_transport_options.ice_restart| etc. set.
deadbeefab9b2d12015-10-14 11:33:11 -07002837TEST(CreateSessionOptionsTest, GetMediaSessionOptionsForOfferWithIceRestart) {
2838 RTCOfferAnswerOptions rtc_options;
2839 rtc_options.ice_restart = true;
2840
2841 cricket::MediaSessionOptions options;
deadbeef0ed85b22016-02-23 17:24:52 -08002842 options.transport_options["audio"] = cricket::TransportOptions();
2843 options.transport_options["video"] = cricket::TransportOptions();
htaaac2dea2016-03-10 13:35:55 -08002844 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeef0ed85b22016-02-23 17:24:52 -08002845 EXPECT_TRUE(options.transport_options["audio"].ice_restart);
2846 EXPECT_TRUE(options.transport_options["video"].ice_restart);
deadbeefab9b2d12015-10-14 11:33:11 -07002847
2848 rtc_options = RTCOfferAnswerOptions();
htaaac2dea2016-03-10 13:35:55 -08002849 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeef0ed85b22016-02-23 17:24:52 -08002850 EXPECT_FALSE(options.transport_options["audio"].ice_restart);
2851 EXPECT_FALSE(options.transport_options["video"].ice_restart);
deadbeefab9b2d12015-10-14 11:33:11 -07002852}
2853
2854// Test that the MediaConstraints in an answer don't affect if audio and video
2855// is offered in an offer but that if kOfferToReceiveAudio or
2856// kOfferToReceiveVideo constraints are true in an offer, the media type will be
2857// included in subsequent answers.
2858TEST(CreateSessionOptionsTest, MediaConstraintsInAnswer) {
2859 FakeConstraints answer_c;
2860 answer_c.SetMandatoryReceiveAudio(true);
2861 answer_c.SetMandatoryReceiveVideo(true);
2862
2863 cricket::MediaSessionOptions answer_options;
2864 EXPECT_TRUE(ParseConstraintsForAnswer(&answer_c, &answer_options));
2865 EXPECT_TRUE(answer_options.has_audio());
2866 EXPECT_TRUE(answer_options.has_video());
2867
deadbeefc80741f2015-10-22 13:14:45 -07002868 RTCOfferAnswerOptions rtc_offer_options;
deadbeefab9b2d12015-10-14 11:33:11 -07002869
2870 cricket::MediaSessionOptions offer_options;
htaaac2dea2016-03-10 13:35:55 -08002871 EXPECT_TRUE(
2872 ExtractMediaSessionOptions(rtc_offer_options, false, &offer_options));
deadbeefc80741f2015-10-22 13:14:45 -07002873 EXPECT_TRUE(offer_options.has_audio());
htaaac2dea2016-03-10 13:35:55 -08002874 EXPECT_TRUE(offer_options.has_video());
deadbeefab9b2d12015-10-14 11:33:11 -07002875
deadbeefc80741f2015-10-22 13:14:45 -07002876 RTCOfferAnswerOptions updated_rtc_offer_options;
2877 updated_rtc_offer_options.offer_to_receive_audio = 1;
2878 updated_rtc_offer_options.offer_to_receive_video = 1;
deadbeefab9b2d12015-10-14 11:33:11 -07002879
2880 cricket::MediaSessionOptions updated_offer_options;
htaaac2dea2016-03-10 13:35:55 -08002881 EXPECT_TRUE(ExtractMediaSessionOptions(updated_rtc_offer_options, false,
htaa2a49d92016-03-04 02:51:39 -08002882 &updated_offer_options));
deadbeefab9b2d12015-10-14 11:33:11 -07002883 EXPECT_TRUE(updated_offer_options.has_audio());
2884 EXPECT_TRUE(updated_offer_options.has_video());
2885
2886 // Since an offer has been created with both audio and video, subsequent
2887 // offers and answers should contain both audio and video.
2888 // Answers will only contain the media types that exist in the offer
2889 // regardless of the value of |updated_answer_options.has_audio| and
2890 // |updated_answer_options.has_video|.
2891 FakeConstraints updated_answer_c;
2892 answer_c.SetMandatoryReceiveAudio(false);
2893 answer_c.SetMandatoryReceiveVideo(false);
2894
2895 cricket::MediaSessionOptions updated_answer_options;
2896 EXPECT_TRUE(
2897 ParseConstraintsForAnswer(&updated_answer_c, &updated_answer_options));
2898 EXPECT_TRUE(updated_answer_options.has_audio());
2899 EXPECT_TRUE(updated_answer_options.has_video());
deadbeefab9b2d12015-10-14 11:33:11 -07002900}