blob: 738b736dd8fd14d4f2c80317ea1de3618f2008d6 [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
perkjd61bf802016-03-24 03:16:19 -070015#include "testing/gmock/include/gmock/gmock.h"
Henrik Kjellander15583c12016-02-10 10:53:12 +010016#include "webrtc/api/audiotrack.h"
17#include "webrtc/api/jsepsessiondescription.h"
18#include "webrtc/api/mediastream.h"
19#include "webrtc/api/mediastreaminterface.h"
20#include "webrtc/api/peerconnection.h"
21#include "webrtc/api/peerconnectioninterface.h"
22#include "webrtc/api/rtpreceiverinterface.h"
23#include "webrtc/api/rtpsenderinterface.h"
24#include "webrtc/api/streamcollection.h"
25#ifdef WEBRTC_ANDROID
26#include "webrtc/api/test/androidtestinitializer.h"
27#endif
28#include "webrtc/api/test/fakeconstraints.h"
29#include "webrtc/api/test/fakedtlsidentitystore.h"
nisseaf510af2016-03-21 08:20:42 -070030#include "webrtc/api/test/fakevideotracksource.h"
Henrik Kjellander15583c12016-02-10 10:53:12 +010031#include "webrtc/api/test/mockpeerconnectionobservers.h"
32#include "webrtc/api/test/testsdpstrings.h"
perkja3ede6c2016-03-08 01:27:48 +010033#include "webrtc/api/videocapturertracksource.h"
Henrik Kjellander15583c12016-02-10 10:53:12 +010034#include "webrtc/api/videotrack.h"
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000035#include "webrtc/base/gunit.h"
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000036#include "webrtc/base/ssladapter.h"
37#include "webrtc/base/sslstreamadapter.h"
38#include "webrtc/base/stringutils.h"
39#include "webrtc/base/thread.h"
kjellandera96e2d72016-02-04 23:52:28 -080040#include "webrtc/media/base/fakevideocapturer.h"
41#include "webrtc/media/sctp/sctpdataengine.h"
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -080042#include "webrtc/p2p/client/fakeportallocator.h"
kjellander@webrtc.org9b8df252016-02-12 06:47:59 +010043#include "webrtc/pc/mediasession.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000044
45static const char kStreamLabel1[] = "local_stream_1";
46static const char kStreamLabel2[] = "local_stream_2";
47static const char kStreamLabel3[] = "local_stream_3";
48static const int kDefaultStunPort = 3478;
49static const char kStunAddressOnly[] = "stun:address";
50static const char kStunInvalidPort[] = "stun:address:-1";
51static const char kStunAddressPortAndMore1[] = "stun:address:port:more";
52static const char kStunAddressPortAndMore2[] = "stun:address:port more";
53static const char kTurnIceServerUri[] = "turn:user@turn.example.org";
54static const char kTurnUsername[] = "user";
55static const char kTurnPassword[] = "password";
56static const char kTurnHostname[] = "turn.example.org";
Peter Boström0c4e06b2015-10-07 12:23:21 +020057static const uint32_t kTimeout = 10000U;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000058
deadbeefab9b2d12015-10-14 11:33:11 -070059static const char kStreams[][8] = {"stream1", "stream2"};
60static const char kAudioTracks[][32] = {"audiotrack0", "audiotrack1"};
61static const char kVideoTracks[][32] = {"videotrack0", "videotrack1"};
62
deadbeef5e97fb52015-10-15 12:49:08 -070063static const char kRecvonly[] = "recvonly";
64static const char kSendrecv[] = "sendrecv";
65
deadbeefab9b2d12015-10-14 11:33:11 -070066// Reference SDP with a MediaStream with label "stream1" and audio track with
67// id "audio_1" and a video track with id "video_1;
68static const char kSdpStringWithStream1[] =
69 "v=0\r\n"
70 "o=- 0 0 IN IP4 127.0.0.1\r\n"
71 "s=-\r\n"
72 "t=0 0\r\n"
73 "a=ice-ufrag:e5785931\r\n"
74 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
75 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
76 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
77 "m=audio 1 RTP/AVPF 103\r\n"
78 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -070079 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -070080 "a=rtpmap:103 ISAC/16000\r\n"
81 "a=ssrc:1 cname:stream1\r\n"
82 "a=ssrc:1 mslabel:stream1\r\n"
83 "a=ssrc:1 label:audiotrack0\r\n"
84 "m=video 1 RTP/AVPF 120\r\n"
85 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -070086 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -070087 "a=rtpmap:120 VP8/90000\r\n"
88 "a=ssrc:2 cname:stream1\r\n"
89 "a=ssrc:2 mslabel:stream1\r\n"
90 "a=ssrc:2 label:videotrack0\r\n";
91
92// Reference SDP with two MediaStreams with label "stream1" and "stream2. Each
93// MediaStreams have one audio track and one video track.
94// This uses MSID.
95static const char kSdpStringWithStream1And2[] =
96 "v=0\r\n"
97 "o=- 0 0 IN IP4 127.0.0.1\r\n"
98 "s=-\r\n"
99 "t=0 0\r\n"
100 "a=ice-ufrag:e5785931\r\n"
101 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
102 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
103 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
104 "a=msid-semantic: WMS stream1 stream2\r\n"
105 "m=audio 1 RTP/AVPF 103\r\n"
106 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700107 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700108 "a=rtpmap:103 ISAC/16000\r\n"
109 "a=ssrc:1 cname:stream1\r\n"
110 "a=ssrc:1 msid:stream1 audiotrack0\r\n"
111 "a=ssrc:3 cname:stream2\r\n"
112 "a=ssrc:3 msid:stream2 audiotrack1\r\n"
113 "m=video 1 RTP/AVPF 120\r\n"
114 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700115 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700116 "a=rtpmap:120 VP8/0\r\n"
117 "a=ssrc:2 cname:stream1\r\n"
118 "a=ssrc:2 msid:stream1 videotrack0\r\n"
119 "a=ssrc:4 cname:stream2\r\n"
120 "a=ssrc:4 msid:stream2 videotrack1\r\n";
121
122// Reference SDP without MediaStreams. Msid is not supported.
123static const char kSdpStringWithoutStreams[] =
124 "v=0\r\n"
125 "o=- 0 0 IN IP4 127.0.0.1\r\n"
126 "s=-\r\n"
127 "t=0 0\r\n"
128 "a=ice-ufrag:e5785931\r\n"
129 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
130 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
131 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
132 "m=audio 1 RTP/AVPF 103\r\n"
133 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700134 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700135 "a=rtpmap:103 ISAC/16000\r\n"
136 "m=video 1 RTP/AVPF 120\r\n"
137 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700138 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700139 "a=rtpmap:120 VP8/90000\r\n";
140
141// Reference SDP without MediaStreams. Msid is supported.
142static const char kSdpStringWithMsidWithoutStreams[] =
143 "v=0\r\n"
144 "o=- 0 0 IN IP4 127.0.0.1\r\n"
145 "s=-\r\n"
146 "t=0 0\r\n"
147 "a=ice-ufrag:e5785931\r\n"
148 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
149 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
150 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
151 "a=msid-semantic: WMS\r\n"
152 "m=audio 1 RTP/AVPF 103\r\n"
153 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700154 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700155 "a=rtpmap:103 ISAC/16000\r\n"
156 "m=video 1 RTP/AVPF 120\r\n"
157 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700158 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700159 "a=rtpmap:120 VP8/90000\r\n";
160
161// Reference SDP without MediaStreams and audio only.
162static const char kSdpStringWithoutStreamsAudioOnly[] =
163 "v=0\r\n"
164 "o=- 0 0 IN IP4 127.0.0.1\r\n"
165 "s=-\r\n"
166 "t=0 0\r\n"
167 "a=ice-ufrag:e5785931\r\n"
168 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
169 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
170 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
171 "m=audio 1 RTP/AVPF 103\r\n"
172 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700173 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700174 "a=rtpmap:103 ISAC/16000\r\n";
175
176// Reference SENDONLY SDP without MediaStreams. Msid is not supported.
177static const char kSdpStringSendOnlyWithoutStreams[] =
178 "v=0\r\n"
179 "o=- 0 0 IN IP4 127.0.0.1\r\n"
180 "s=-\r\n"
181 "t=0 0\r\n"
182 "a=ice-ufrag:e5785931\r\n"
183 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
184 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
185 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
186 "m=audio 1 RTP/AVPF 103\r\n"
187 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700188 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700189 "a=sendonly\r\n"
190 "a=rtpmap:103 ISAC/16000\r\n"
191 "m=video 1 RTP/AVPF 120\r\n"
192 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700193 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700194 "a=sendonly\r\n"
195 "a=rtpmap:120 VP8/90000\r\n";
196
197static const char kSdpStringInit[] =
198 "v=0\r\n"
199 "o=- 0 0 IN IP4 127.0.0.1\r\n"
200 "s=-\r\n"
201 "t=0 0\r\n"
202 "a=ice-ufrag:e5785931\r\n"
203 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
204 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
205 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
206 "a=msid-semantic: WMS\r\n";
207
208static const char kSdpStringAudio[] =
209 "m=audio 1 RTP/AVPF 103\r\n"
210 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700211 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700212 "a=rtpmap:103 ISAC/16000\r\n";
213
214static const char kSdpStringVideo[] =
215 "m=video 1 RTP/AVPF 120\r\n"
216 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700217 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700218 "a=rtpmap:120 VP8/90000\r\n";
219
220static const char kSdpStringMs1Audio0[] =
221 "a=ssrc:1 cname:stream1\r\n"
222 "a=ssrc:1 msid:stream1 audiotrack0\r\n";
223
224static const char kSdpStringMs1Video0[] =
225 "a=ssrc:2 cname:stream1\r\n"
226 "a=ssrc:2 msid:stream1 videotrack0\r\n";
227
228static const char kSdpStringMs1Audio1[] =
229 "a=ssrc:3 cname:stream1\r\n"
230 "a=ssrc:3 msid:stream1 audiotrack1\r\n";
231
232static const char kSdpStringMs1Video1[] =
233 "a=ssrc:4 cname:stream1\r\n"
234 "a=ssrc:4 msid:stream1 videotrack1\r\n";
235
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000236#define MAYBE_SKIP_TEST(feature) \
237 if (!(feature())) { \
238 LOG(LS_INFO) << "Feature disabled... skipping"; \
239 return; \
240 }
241
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000242using rtc::scoped_refptr;
perkjd61bf802016-03-24 03:16:19 -0700243using ::testing::Exactly;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000244using webrtc::AudioSourceInterface;
deadbeefab9b2d12015-10-14 11:33:11 -0700245using webrtc::AudioTrack;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000246using webrtc::AudioTrackInterface;
247using webrtc::DataBuffer;
248using webrtc::DataChannelInterface;
249using webrtc::FakeConstraints;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000250using webrtc::IceCandidateInterface;
deadbeefc80741f2015-10-22 13:14:45 -0700251using webrtc::MediaConstraintsInterface;
deadbeefab9b2d12015-10-14 11:33:11 -0700252using webrtc::MediaStream;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000253using webrtc::MediaStreamInterface;
254using webrtc::MediaStreamTrackInterface;
255using webrtc::MockCreateSessionDescriptionObserver;
256using webrtc::MockDataChannelObserver;
257using webrtc::MockSetSessionDescriptionObserver;
258using webrtc::MockStatsObserver;
perkjd61bf802016-03-24 03:16:19 -0700259using webrtc::NotifierInterface;
260using webrtc::ObserverInterface;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000261using webrtc::PeerConnectionInterface;
262using webrtc::PeerConnectionObserver;
deadbeefab9b2d12015-10-14 11:33:11 -0700263using webrtc::RtpReceiverInterface;
264using webrtc::RtpSenderInterface;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000265using webrtc::SdpParseError;
266using webrtc::SessionDescriptionInterface;
deadbeefab9b2d12015-10-14 11:33:11 -0700267using webrtc::StreamCollection;
268using webrtc::StreamCollectionInterface;
perkja3ede6c2016-03-08 01:27:48 +0100269using webrtc::VideoTrackSourceInterface;
deadbeefab9b2d12015-10-14 11:33:11 -0700270using webrtc::VideoTrack;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000271using webrtc::VideoTrackInterface;
272
deadbeefab9b2d12015-10-14 11:33:11 -0700273typedef PeerConnectionInterface::RTCOfferAnswerOptions RTCOfferAnswerOptions;
274
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000275namespace {
276
277// Gets the first ssrc of given content type from the ContentInfo.
278bool GetFirstSsrc(const cricket::ContentInfo* content_info, int* ssrc) {
279 if (!content_info || !ssrc) {
280 return false;
281 }
282 const cricket::MediaContentDescription* media_desc =
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000283 static_cast<const cricket::MediaContentDescription*>(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000284 content_info->description);
285 if (!media_desc || media_desc->streams().empty()) {
286 return false;
287 }
288 *ssrc = media_desc->streams().begin()->first_ssrc();
289 return true;
290}
291
292void SetSsrcToZero(std::string* sdp) {
293 const char kSdpSsrcAtribute[] = "a=ssrc:";
294 const char kSdpSsrcAtributeZero[] = "a=ssrc:0";
295 size_t ssrc_pos = 0;
296 while ((ssrc_pos = sdp->find(kSdpSsrcAtribute, ssrc_pos)) !=
297 std::string::npos) {
298 size_t end_ssrc = sdp->find(" ", ssrc_pos);
299 sdp->replace(ssrc_pos, end_ssrc - ssrc_pos, kSdpSsrcAtributeZero);
300 ssrc_pos = end_ssrc;
301 }
302}
303
deadbeefab9b2d12015-10-14 11:33:11 -0700304// Check if |streams| contains the specified track.
305bool ContainsTrack(const std::vector<cricket::StreamParams>& streams,
306 const std::string& stream_label,
307 const std::string& track_id) {
308 for (const cricket::StreamParams& params : streams) {
309 if (params.sync_label == stream_label && params.id == track_id) {
310 return true;
311 }
312 }
313 return false;
314}
315
316// Check if |senders| contains the specified sender, by id.
317bool ContainsSender(
318 const std::vector<rtc::scoped_refptr<RtpSenderInterface>>& senders,
319 const std::string& id) {
320 for (const auto& sender : senders) {
321 if (sender->id() == id) {
322 return true;
323 }
324 }
325 return false;
326}
327
328// Create a collection of streams.
329// CreateStreamCollection(1) creates a collection that
330// correspond to kSdpStringWithStream1.
331// CreateStreamCollection(2) correspond to kSdpStringWithStream1And2.
332rtc::scoped_refptr<StreamCollection> CreateStreamCollection(
333 int number_of_streams) {
334 rtc::scoped_refptr<StreamCollection> local_collection(
335 StreamCollection::Create());
336
337 for (int i = 0; i < number_of_streams; ++i) {
338 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream(
339 webrtc::MediaStream::Create(kStreams[i]));
340
341 // Add a local audio track.
342 rtc::scoped_refptr<webrtc::AudioTrackInterface> audio_track(
343 webrtc::AudioTrack::Create(kAudioTracks[i], nullptr));
344 stream->AddTrack(audio_track);
345
346 // Add a local video track.
347 rtc::scoped_refptr<webrtc::VideoTrackInterface> video_track(
nisseaf510af2016-03-21 08:20:42 -0700348 webrtc::VideoTrack::Create(kVideoTracks[i],
349 webrtc::FakeVideoTrackSource::Create()));
deadbeefab9b2d12015-10-14 11:33:11 -0700350 stream->AddTrack(video_track);
351
352 local_collection->AddStream(stream);
353 }
354 return local_collection;
355}
356
357// Check equality of StreamCollections.
358bool CompareStreamCollections(StreamCollectionInterface* s1,
359 StreamCollectionInterface* s2) {
360 if (s1 == nullptr || s2 == nullptr || s1->count() != s2->count()) {
361 return false;
362 }
363
364 for (size_t i = 0; i != s1->count(); ++i) {
365 if (s1->at(i)->label() != s2->at(i)->label()) {
366 return false;
367 }
368 webrtc::AudioTrackVector audio_tracks1 = s1->at(i)->GetAudioTracks();
369 webrtc::AudioTrackVector audio_tracks2 = s2->at(i)->GetAudioTracks();
370 webrtc::VideoTrackVector video_tracks1 = s1->at(i)->GetVideoTracks();
371 webrtc::VideoTrackVector video_tracks2 = s2->at(i)->GetVideoTracks();
372
373 if (audio_tracks1.size() != audio_tracks2.size()) {
374 return false;
375 }
376 for (size_t j = 0; j != audio_tracks1.size(); ++j) {
377 if (audio_tracks1[j]->id() != audio_tracks2[j]->id()) {
378 return false;
379 }
380 }
381 if (video_tracks1.size() != video_tracks2.size()) {
382 return false;
383 }
384 for (size_t j = 0; j != video_tracks1.size(); ++j) {
385 if (video_tracks1[j]->id() != video_tracks2[j]->id()) {
386 return false;
387 }
388 }
389 }
390 return true;
391}
392
perkjd61bf802016-03-24 03:16:19 -0700393// Helper class to test Observer.
394class MockTrackObserver : public ObserverInterface {
395 public:
396 explicit MockTrackObserver(NotifierInterface* notifier)
397 : notifier_(notifier) {
398 notifier_->RegisterObserver(this);
399 }
400
401 ~MockTrackObserver() { Unregister(); }
402
403 void Unregister() {
404 if (notifier_) {
405 notifier_->UnregisterObserver(this);
406 notifier_ = nullptr;
407 }
408 }
409
410 MOCK_METHOD0(OnChanged, void());
411
412 private:
413 NotifierInterface* notifier_;
414};
415
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000416class MockPeerConnectionObserver : public PeerConnectionObserver {
417 public:
deadbeefab9b2d12015-10-14 11:33:11 -0700418 MockPeerConnectionObserver() : remote_streams_(StreamCollection::Create()) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000419 ~MockPeerConnectionObserver() {
420 }
421 void SetPeerConnectionInterface(PeerConnectionInterface* pc) {
422 pc_ = pc;
423 if (pc) {
424 state_ = pc_->signaling_state();
425 }
426 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000427 virtual void OnSignalingChange(
428 PeerConnectionInterface::SignalingState new_state) {
429 EXPECT_EQ(pc_->signaling_state(), new_state);
430 state_ = new_state;
431 }
432 // TODO(bemasc): Remove this once callers transition to OnIceGatheringChange.
433 virtual void OnStateChange(StateType state_changed) {
434 if (pc_.get() == NULL)
435 return;
436 switch (state_changed) {
437 case kSignalingState:
438 // OnSignalingChange and OnStateChange(kSignalingState) should always
439 // be called approximately simultaneously. To ease testing, we require
440 // that they always be called in that order. This check verifies
441 // that OnSignalingChange has just been called.
442 EXPECT_EQ(pc_->signaling_state(), state_);
443 break;
444 case kIceState:
445 ADD_FAILURE();
446 break;
447 default:
448 ADD_FAILURE();
449 break;
450 }
451 }
deadbeefab9b2d12015-10-14 11:33:11 -0700452
453 MediaStreamInterface* RemoteStream(const std::string& label) {
454 return remote_streams_->find(label);
455 }
456 StreamCollectionInterface* remote_streams() const { return remote_streams_; }
perkjdfb769d2016-02-09 03:09:43 -0800457 void OnAddStream(MediaStreamInterface* stream) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000458 last_added_stream_ = stream;
deadbeefab9b2d12015-10-14 11:33:11 -0700459 remote_streams_->AddStream(stream);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000460 }
perkjdfb769d2016-02-09 03:09:43 -0800461 void OnRemoveStream(MediaStreamInterface* stream) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000462 last_removed_stream_ = stream;
deadbeefab9b2d12015-10-14 11:33:11 -0700463 remote_streams_->RemoveStream(stream);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000464 }
perkjdfb769d2016-02-09 03:09:43 -0800465 void OnRenegotiationNeeded() override { renegotiation_needed_ = true; }
466 void OnDataChannel(DataChannelInterface* data_channel) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000467 last_datachannel_ = data_channel;
468 }
469
perkjdfb769d2016-02-09 03:09:43 -0800470 void OnIceConnectionChange(
471 PeerConnectionInterface::IceConnectionState new_state) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000472 EXPECT_EQ(pc_->ice_connection_state(), new_state);
473 }
perkjdfb769d2016-02-09 03:09:43 -0800474 void OnIceGatheringChange(
475 PeerConnectionInterface::IceGatheringState new_state) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000476 EXPECT_EQ(pc_->ice_gathering_state(), new_state);
perkjdfb769d2016-02-09 03:09:43 -0800477 ice_complete_ = new_state == PeerConnectionInterface::kIceGatheringComplete;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000478 }
perkjdfb769d2016-02-09 03:09:43 -0800479 void OnIceCandidate(const webrtc::IceCandidateInterface* candidate) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000480 EXPECT_NE(PeerConnectionInterface::kIceGatheringNew,
481 pc_->ice_gathering_state());
482
483 std::string sdp;
484 EXPECT_TRUE(candidate->ToString(&sdp));
485 EXPECT_LT(0u, sdp.size());
486 last_candidate_.reset(webrtc::CreateIceCandidate(candidate->sdp_mid(),
487 candidate->sdp_mline_index(), sdp, NULL));
488 EXPECT_TRUE(last_candidate_.get() != NULL);
489 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000490
491 // Returns the label of the last added stream.
492 // Empty string if no stream have been added.
493 std::string GetLastAddedStreamLabel() {
494 if (last_added_stream_.get())
495 return last_added_stream_->label();
496 return "";
497 }
498 std::string GetLastRemovedStreamLabel() {
499 if (last_removed_stream_.get())
500 return last_removed_stream_->label();
501 return "";
502 }
503
504 scoped_refptr<PeerConnectionInterface> pc_;
505 PeerConnectionInterface::SignalingState state_;
kwibergd1fe2812016-04-27 06:47:29 -0700506 std::unique_ptr<IceCandidateInterface> last_candidate_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000507 scoped_refptr<DataChannelInterface> last_datachannel_;
deadbeefab9b2d12015-10-14 11:33:11 -0700508 rtc::scoped_refptr<StreamCollection> remote_streams_;
509 bool renegotiation_needed_ = false;
510 bool ice_complete_ = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000511
512 private:
513 scoped_refptr<MediaStreamInterface> last_added_stream_;
514 scoped_refptr<MediaStreamInterface> last_removed_stream_;
515};
516
517} // namespace
deadbeefab9b2d12015-10-14 11:33:11 -0700518
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000519class PeerConnectionInterfaceTest : public testing::Test {
520 protected:
phoglund37ebcf02016-01-08 05:04:57 -0800521 PeerConnectionInterfaceTest() {
522#ifdef WEBRTC_ANDROID
523 webrtc::InitializeAndroidObjects();
524#endif
525 }
526
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000527 virtual void SetUp() {
528 pc_factory_ = webrtc::CreatePeerConnectionFactory(
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000529 rtc::Thread::Current(), rtc::Thread::Current(), NULL, NULL,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000530 NULL);
531 ASSERT_TRUE(pc_factory_.get() != NULL);
532 }
533
534 void CreatePeerConnection() {
535 CreatePeerConnection("", "", NULL);
536 }
537
538 void CreatePeerConnection(webrtc::MediaConstraintsInterface* constraints) {
539 CreatePeerConnection("", "", constraints);
540 }
541
542 void CreatePeerConnection(const std::string& uri,
543 const std::string& password,
544 webrtc::MediaConstraintsInterface* constraints) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800545 PeerConnectionInterface::RTCConfiguration config;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000546 PeerConnectionInterface::IceServer server;
deadbeef0a6c4ca2015-10-06 11:38:28 -0700547 if (!uri.empty()) {
548 server.uri = uri;
549 server.password = password;
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800550 config.servers.push_back(server);
deadbeef0a6c4ca2015-10-06 11:38:28 -0700551 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000552
kwibergd1fe2812016-04-27 06:47:29 -0700553 std::unique_ptr<cricket::FakePortAllocator> port_allocator(
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800554 new cricket::FakePortAllocator(rtc::Thread::Current(), nullptr));
555 port_allocator_ = port_allocator.get();
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000556
buildbot@webrtc.org61c1b8e2014-04-09 06:06:38 +0000557 // DTLS does not work in a loopback call, so is disabled for most of the
558 // tests in this file. We only create a FakeIdentityService if the test
559 // explicitly sets the constraint.
jiayl@webrtc.org61e00b02015-03-04 22:17:38 +0000560 FakeConstraints default_constraints;
561 if (!constraints) {
562 constraints = &default_constraints;
563
564 default_constraints.AddMandatory(
565 webrtc::MediaConstraintsInterface::kEnableDtlsSrtp, false);
566 }
567
kwibergd1fe2812016-04-27 06:47:29 -0700568 std::unique_ptr<webrtc::DtlsIdentityStoreInterface> dtls_identity_store;
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000569 bool dtls;
570 if (FindConstraint(constraints,
571 webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
572 &dtls,
Henrik Boström5e56c592015-08-11 10:33:13 +0200573 nullptr) && dtls) {
574 dtls_identity_store.reset(new FakeDtlsIdentityStore());
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000575 }
kwiberg0eb15ed2015-12-17 03:04:15 -0800576 pc_ = pc_factory_->CreatePeerConnection(
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800577 config, constraints, std::move(port_allocator),
kwiberg0eb15ed2015-12-17 03:04:15 -0800578 std::move(dtls_identity_store), &observer_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000579 ASSERT_TRUE(pc_.get() != NULL);
580 observer_.SetPeerConnectionInterface(pc_.get());
581 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
582 }
583
deadbeef0a6c4ca2015-10-06 11:38:28 -0700584 void CreatePeerConnectionExpectFail(const std::string& uri) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800585 PeerConnectionInterface::RTCConfiguration config;
deadbeef0a6c4ca2015-10-06 11:38:28 -0700586 PeerConnectionInterface::IceServer server;
deadbeef0a6c4ca2015-10-06 11:38:28 -0700587 server.uri = uri;
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800588 config.servers.push_back(server);
deadbeef0a6c4ca2015-10-06 11:38:28 -0700589
deadbeef0a6c4ca2015-10-06 11:38:28 -0700590 scoped_refptr<PeerConnectionInterface> pc;
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800591 pc = pc_factory_->CreatePeerConnection(config, nullptr, nullptr, nullptr,
592 &observer_);
593 EXPECT_EQ(nullptr, pc);
deadbeef0a6c4ca2015-10-06 11:38:28 -0700594 }
595
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000596 void CreatePeerConnectionWithDifferentConfigurations() {
597 CreatePeerConnection(kStunAddressOnly, "", NULL);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800598 EXPECT_EQ(1u, port_allocator_->stun_servers().size());
599 EXPECT_EQ(0u, port_allocator_->turn_servers().size());
600 EXPECT_EQ("address", port_allocator_->stun_servers().begin()->hostname());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000601 EXPECT_EQ(kDefaultStunPort,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800602 port_allocator_->stun_servers().begin()->port());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000603
deadbeef0a6c4ca2015-10-06 11:38:28 -0700604 CreatePeerConnectionExpectFail(kStunInvalidPort);
605 CreatePeerConnectionExpectFail(kStunAddressPortAndMore1);
606 CreatePeerConnectionExpectFail(kStunAddressPortAndMore2);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000607
608 CreatePeerConnection(kTurnIceServerUri, kTurnPassword, NULL);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800609 EXPECT_EQ(0u, port_allocator_->stun_servers().size());
610 EXPECT_EQ(1u, port_allocator_->turn_servers().size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000611 EXPECT_EQ(kTurnUsername,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800612 port_allocator_->turn_servers()[0].credentials.username);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000613 EXPECT_EQ(kTurnPassword,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800614 port_allocator_->turn_servers()[0].credentials.password);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000615 EXPECT_EQ(kTurnHostname,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800616 port_allocator_->turn_servers()[0].ports[0].address.hostname());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000617 }
618
619 void ReleasePeerConnection() {
620 pc_ = NULL;
621 observer_.SetPeerConnectionInterface(NULL);
622 }
623
deadbeefab9b2d12015-10-14 11:33:11 -0700624 void AddVideoStream(const std::string& label) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000625 // Create a local stream.
626 scoped_refptr<MediaStreamInterface> stream(
627 pc_factory_->CreateLocalMediaStream(label));
perkja3ede6c2016-03-08 01:27:48 +0100628 scoped_refptr<VideoTrackSourceInterface> video_source(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000629 pc_factory_->CreateVideoSource(new cricket::FakeVideoCapturer(), NULL));
630 scoped_refptr<VideoTrackInterface> video_track(
631 pc_factory_->CreateVideoTrack(label + "v0", video_source));
632 stream->AddTrack(video_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +0000633 EXPECT_TRUE(pc_->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000634 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
635 observer_.renegotiation_needed_ = false;
636 }
637
638 void AddVoiceStream(const std::string& label) {
639 // Create a local stream.
640 scoped_refptr<MediaStreamInterface> stream(
641 pc_factory_->CreateLocalMediaStream(label));
642 scoped_refptr<AudioTrackInterface> audio_track(
643 pc_factory_->CreateAudioTrack(label + "a0", NULL));
644 stream->AddTrack(audio_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +0000645 EXPECT_TRUE(pc_->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000646 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
647 observer_.renegotiation_needed_ = false;
648 }
649
650 void AddAudioVideoStream(const std::string& stream_label,
651 const std::string& audio_track_label,
652 const std::string& video_track_label) {
653 // Create a local stream.
654 scoped_refptr<MediaStreamInterface> stream(
655 pc_factory_->CreateLocalMediaStream(stream_label));
656 scoped_refptr<AudioTrackInterface> audio_track(
657 pc_factory_->CreateAudioTrack(
658 audio_track_label, static_cast<AudioSourceInterface*>(NULL)));
659 stream->AddTrack(audio_track.get());
660 scoped_refptr<VideoTrackInterface> video_track(
nisseaf510af2016-03-21 08:20:42 -0700661 pc_factory_->CreateVideoTrack(
662 video_track_label,
663 pc_factory_->CreateVideoSource(new cricket::FakeVideoCapturer())));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000664 stream->AddTrack(video_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +0000665 EXPECT_TRUE(pc_->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000666 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
667 observer_.renegotiation_needed_ = false;
668 }
669
kwibergd1fe2812016-04-27 06:47:29 -0700670 bool DoCreateOfferAnswer(std::unique_ptr<SessionDescriptionInterface>* desc,
deadbeefc80741f2015-10-22 13:14:45 -0700671 bool offer,
672 MediaConstraintsInterface* constraints) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000673 rtc::scoped_refptr<MockCreateSessionDescriptionObserver>
674 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000675 MockCreateSessionDescriptionObserver>());
676 if (offer) {
deadbeefc80741f2015-10-22 13:14:45 -0700677 pc_->CreateOffer(observer, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000678 } else {
deadbeefc80741f2015-10-22 13:14:45 -0700679 pc_->CreateAnswer(observer, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000680 }
681 EXPECT_EQ_WAIT(true, observer->called(), kTimeout);
kwiberg2bbff992016-03-16 11:03:04 -0700682 desc->reset(observer->release_desc());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000683 return observer->result();
684 }
685
kwibergd1fe2812016-04-27 06:47:29 -0700686 bool DoCreateOffer(std::unique_ptr<SessionDescriptionInterface>* desc,
deadbeefc80741f2015-10-22 13:14:45 -0700687 MediaConstraintsInterface* constraints) {
688 return DoCreateOfferAnswer(desc, true, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000689 }
690
kwibergd1fe2812016-04-27 06:47:29 -0700691 bool DoCreateAnswer(std::unique_ptr<SessionDescriptionInterface>* desc,
deadbeefc80741f2015-10-22 13:14:45 -0700692 MediaConstraintsInterface* constraints) {
693 return DoCreateOfferAnswer(desc, false, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000694 }
695
696 bool DoSetSessionDescription(SessionDescriptionInterface* desc, bool local) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000697 rtc::scoped_refptr<MockSetSessionDescriptionObserver>
698 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000699 MockSetSessionDescriptionObserver>());
700 if (local) {
701 pc_->SetLocalDescription(observer, desc);
702 } else {
703 pc_->SetRemoteDescription(observer, desc);
704 }
705 EXPECT_EQ_WAIT(true, observer->called(), kTimeout);
706 return observer->result();
707 }
708
709 bool DoSetLocalDescription(SessionDescriptionInterface* desc) {
710 return DoSetSessionDescription(desc, true);
711 }
712
713 bool DoSetRemoteDescription(SessionDescriptionInterface* desc) {
714 return DoSetSessionDescription(desc, false);
715 }
716
717 // Calls PeerConnection::GetStats and check the return value.
718 // It does not verify the values in the StatReports since a RTCP packet might
719 // be required.
720 bool DoGetStats(MediaStreamTrackInterface* track) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000721 rtc::scoped_refptr<MockStatsObserver> observer(
722 new rtc::RefCountedObject<MockStatsObserver>());
jiayl@webrtc.orgdb41b4d2014-03-03 21:30:06 +0000723 if (!pc_->GetStats(
724 observer, track, PeerConnectionInterface::kStatsOutputLevelStandard))
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000725 return false;
726 EXPECT_TRUE_WAIT(observer->called(), kTimeout);
727 return observer->called();
728 }
729
730 void InitiateCall() {
731 CreatePeerConnection();
732 // Create a local stream with audio&video tracks.
733 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
734 CreateOfferReceiveAnswer();
735 }
736
737 // Verify that RTP Header extensions has been negotiated for audio and video.
738 void VerifyRemoteRtpHeaderExtensions() {
739 const cricket::MediaContentDescription* desc =
740 cricket::GetFirstAudioContentDescription(
741 pc_->remote_description()->description());
742 ASSERT_TRUE(desc != NULL);
743 EXPECT_GT(desc->rtp_header_extensions().size(), 0u);
744
745 desc = cricket::GetFirstVideoContentDescription(
746 pc_->remote_description()->description());
747 ASSERT_TRUE(desc != NULL);
748 EXPECT_GT(desc->rtp_header_extensions().size(), 0u);
749 }
750
751 void CreateOfferAsRemoteDescription() {
kwibergd1fe2812016-04-27 06:47:29 -0700752 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -0700753 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000754 std::string sdp;
755 EXPECT_TRUE(offer->ToString(&sdp));
756 SessionDescriptionInterface* remote_offer =
757 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
758 sdp, NULL);
759 EXPECT_TRUE(DoSetRemoteDescription(remote_offer));
760 EXPECT_EQ(PeerConnectionInterface::kHaveRemoteOffer, observer_.state_);
761 }
762
deadbeefab9b2d12015-10-14 11:33:11 -0700763 void CreateAndSetRemoteOffer(const std::string& sdp) {
764 SessionDescriptionInterface* remote_offer =
765 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
766 sdp, nullptr);
767 EXPECT_TRUE(DoSetRemoteDescription(remote_offer));
768 EXPECT_EQ(PeerConnectionInterface::kHaveRemoteOffer, observer_.state_);
769 }
770
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000771 void CreateAnswerAsLocalDescription() {
kwibergd1fe2812016-04-27 06:47:29 -0700772 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -0700773 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000774
775 // TODO(perkj): Currently SetLocalDescription fails if any parameters in an
776 // audio codec change, even if the parameter has nothing to do with
777 // receiving. Not all parameters are serialized to SDP.
778 // Since CreatePrAnswerAsLocalDescription serialize/deserialize
779 // the SessionDescription, it is necessary to do that here to in order to
780 // get ReceiveOfferCreatePrAnswerAndAnswer and RenegotiateAudioOnly to pass.
781 // https://code.google.com/p/webrtc/issues/detail?id=1356
782 std::string sdp;
783 EXPECT_TRUE(answer->ToString(&sdp));
784 SessionDescriptionInterface* new_answer =
785 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
786 sdp, NULL);
787 EXPECT_TRUE(DoSetLocalDescription(new_answer));
788 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
789 }
790
791 void CreatePrAnswerAsLocalDescription() {
kwibergd1fe2812016-04-27 06:47:29 -0700792 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -0700793 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000794
795 std::string sdp;
796 EXPECT_TRUE(answer->ToString(&sdp));
797 SessionDescriptionInterface* pr_answer =
798 webrtc::CreateSessionDescription(SessionDescriptionInterface::kPrAnswer,
799 sdp, NULL);
800 EXPECT_TRUE(DoSetLocalDescription(pr_answer));
801 EXPECT_EQ(PeerConnectionInterface::kHaveLocalPrAnswer, observer_.state_);
802 }
803
804 void CreateOfferReceiveAnswer() {
805 CreateOfferAsLocalDescription();
806 std::string sdp;
807 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
808 CreateAnswerAsRemoteDescription(sdp);
809 }
810
811 void CreateOfferAsLocalDescription() {
kwibergd1fe2812016-04-27 06:47:29 -0700812 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -0700813 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000814 // TODO(perkj): Currently SetLocalDescription fails if any parameters in an
815 // audio codec change, even if the parameter has nothing to do with
816 // receiving. Not all parameters are serialized to SDP.
817 // Since CreatePrAnswerAsLocalDescription serialize/deserialize
818 // the SessionDescription, it is necessary to do that here to in order to
819 // get ReceiveOfferCreatePrAnswerAndAnswer and RenegotiateAudioOnly to pass.
820 // https://code.google.com/p/webrtc/issues/detail?id=1356
821 std::string sdp;
822 EXPECT_TRUE(offer->ToString(&sdp));
823 SessionDescriptionInterface* new_offer =
824 webrtc::CreateSessionDescription(
825 SessionDescriptionInterface::kOffer,
826 sdp, NULL);
827
828 EXPECT_TRUE(DoSetLocalDescription(new_offer));
829 EXPECT_EQ(PeerConnectionInterface::kHaveLocalOffer, observer_.state_);
mallinath@webrtc.org68cbd012014-01-22 00:16:46 +0000830 // Wait for the ice_complete message, so that SDP will have candidates.
831 EXPECT_TRUE_WAIT(observer_.ice_complete_, kTimeout);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000832 }
833
deadbeefab9b2d12015-10-14 11:33:11 -0700834 void CreateAnswerAsRemoteDescription(const std::string& sdp) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000835 webrtc::JsepSessionDescription* answer = new webrtc::JsepSessionDescription(
836 SessionDescriptionInterface::kAnswer);
deadbeefab9b2d12015-10-14 11:33:11 -0700837 EXPECT_TRUE(answer->Initialize(sdp, NULL));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000838 EXPECT_TRUE(DoSetRemoteDescription(answer));
839 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
840 }
841
deadbeefab9b2d12015-10-14 11:33:11 -0700842 void CreatePrAnswerAndAnswerAsRemoteDescription(const std::string& sdp) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000843 webrtc::JsepSessionDescription* pr_answer =
844 new webrtc::JsepSessionDescription(
845 SessionDescriptionInterface::kPrAnswer);
deadbeefab9b2d12015-10-14 11:33:11 -0700846 EXPECT_TRUE(pr_answer->Initialize(sdp, NULL));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000847 EXPECT_TRUE(DoSetRemoteDescription(pr_answer));
848 EXPECT_EQ(PeerConnectionInterface::kHaveRemotePrAnswer, observer_.state_);
849 webrtc::JsepSessionDescription* answer =
850 new webrtc::JsepSessionDescription(
851 SessionDescriptionInterface::kAnswer);
deadbeefab9b2d12015-10-14 11:33:11 -0700852 EXPECT_TRUE(answer->Initialize(sdp, NULL));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000853 EXPECT_TRUE(DoSetRemoteDescription(answer));
854 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
855 }
856
857 // Help function used for waiting until a the last signaled remote stream has
858 // the same label as |stream_label|. In a few of the tests in this file we
859 // answer with the same session description as we offer and thus we can
860 // check if OnAddStream have been called with the same stream as we offer to
861 // send.
862 void WaitAndVerifyOnAddStream(const std::string& stream_label) {
863 EXPECT_EQ_WAIT(stream_label, observer_.GetLastAddedStreamLabel(), kTimeout);
864 }
865
866 // Creates an offer and applies it as a local session description.
867 // Creates an answer with the same SDP an the offer but removes all lines
868 // that start with a:ssrc"
869 void CreateOfferReceiveAnswerWithoutSsrc() {
870 CreateOfferAsLocalDescription();
871 std::string sdp;
872 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
873 SetSsrcToZero(&sdp);
874 CreateAnswerAsRemoteDescription(sdp);
875 }
876
deadbeefab9b2d12015-10-14 11:33:11 -0700877 // This function creates a MediaStream with label kStreams[0] and
878 // |number_of_audio_tracks| and |number_of_video_tracks| tracks and the
879 // corresponding SessionDescriptionInterface. The SessionDescriptionInterface
kwiberg2bbff992016-03-16 11:03:04 -0700880 // is returned and the MediaStream is stored in
deadbeefab9b2d12015-10-14 11:33:11 -0700881 // |reference_collection_|
kwibergd1fe2812016-04-27 06:47:29 -0700882 std::unique_ptr<SessionDescriptionInterface>
kwiberg2bbff992016-03-16 11:03:04 -0700883 CreateSessionDescriptionAndReference(size_t number_of_audio_tracks,
884 size_t number_of_video_tracks) {
885 EXPECT_LE(number_of_audio_tracks, 2u);
886 EXPECT_LE(number_of_video_tracks, 2u);
deadbeefab9b2d12015-10-14 11:33:11 -0700887
888 reference_collection_ = StreamCollection::Create();
889 std::string sdp_ms1 = std::string(kSdpStringInit);
890
891 std::string mediastream_label = kStreams[0];
892
893 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream(
894 webrtc::MediaStream::Create(mediastream_label));
895 reference_collection_->AddStream(stream);
896
897 if (number_of_audio_tracks > 0) {
898 sdp_ms1 += std::string(kSdpStringAudio);
899 sdp_ms1 += std::string(kSdpStringMs1Audio0);
900 AddAudioTrack(kAudioTracks[0], stream);
901 }
902 if (number_of_audio_tracks > 1) {
903 sdp_ms1 += kSdpStringMs1Audio1;
904 AddAudioTrack(kAudioTracks[1], stream);
905 }
906
907 if (number_of_video_tracks > 0) {
908 sdp_ms1 += std::string(kSdpStringVideo);
909 sdp_ms1 += std::string(kSdpStringMs1Video0);
910 AddVideoTrack(kVideoTracks[0], stream);
911 }
912 if (number_of_video_tracks > 1) {
913 sdp_ms1 += kSdpStringMs1Video1;
914 AddVideoTrack(kVideoTracks[1], stream);
915 }
916
kwibergd1fe2812016-04-27 06:47:29 -0700917 return std::unique_ptr<SessionDescriptionInterface>(
kwiberg2bbff992016-03-16 11:03:04 -0700918 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
919 sdp_ms1, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -0700920 }
921
922 void AddAudioTrack(const std::string& track_id,
923 MediaStreamInterface* stream) {
924 rtc::scoped_refptr<webrtc::AudioTrackInterface> audio_track(
925 webrtc::AudioTrack::Create(track_id, nullptr));
926 ASSERT_TRUE(stream->AddTrack(audio_track));
927 }
928
929 void AddVideoTrack(const std::string& track_id,
930 MediaStreamInterface* stream) {
931 rtc::scoped_refptr<webrtc::VideoTrackInterface> video_track(
nisseaf510af2016-03-21 08:20:42 -0700932 webrtc::VideoTrack::Create(track_id,
933 webrtc::FakeVideoTrackSource::Create()));
deadbeefab9b2d12015-10-14 11:33:11 -0700934 ASSERT_TRUE(stream->AddTrack(video_track));
935 }
936
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800937 cricket::FakePortAllocator* port_allocator_ = nullptr;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000938 scoped_refptr<webrtc::PeerConnectionFactoryInterface> pc_factory_;
939 scoped_refptr<PeerConnectionInterface> pc_;
940 MockPeerConnectionObserver observer_;
deadbeefab9b2d12015-10-14 11:33:11 -0700941 rtc::scoped_refptr<StreamCollection> reference_collection_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000942};
943
944TEST_F(PeerConnectionInterfaceTest,
945 CreatePeerConnectionWithDifferentConfigurations) {
946 CreatePeerConnectionWithDifferentConfigurations();
947}
948
949TEST_F(PeerConnectionInterfaceTest, AddStreams) {
950 CreatePeerConnection();
deadbeefab9b2d12015-10-14 11:33:11 -0700951 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000952 AddVoiceStream(kStreamLabel2);
953 ASSERT_EQ(2u, pc_->local_streams()->count());
954
wu@webrtc.org9dba5252013-08-05 20:36:57 +0000955 // Test we can add multiple local streams to one peerconnection.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000956 scoped_refptr<MediaStreamInterface> stream(
957 pc_factory_->CreateLocalMediaStream(kStreamLabel3));
958 scoped_refptr<AudioTrackInterface> audio_track(
959 pc_factory_->CreateAudioTrack(
960 kStreamLabel3, static_cast<AudioSourceInterface*>(NULL)));
961 stream->AddTrack(audio_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +0000962 EXPECT_TRUE(pc_->AddStream(stream));
wu@webrtc.org9dba5252013-08-05 20:36:57 +0000963 EXPECT_EQ(3u, pc_->local_streams()->count());
964
965 // Remove the third stream.
966 pc_->RemoveStream(pc_->local_streams()->at(2));
967 EXPECT_EQ(2u, pc_->local_streams()->count());
968
969 // Remove the second stream.
970 pc_->RemoveStream(pc_->local_streams()->at(1));
971 EXPECT_EQ(1u, pc_->local_streams()->count());
972
973 // Remove the first stream.
974 pc_->RemoveStream(pc_->local_streams()->at(0));
975 EXPECT_EQ(0u, pc_->local_streams()->count());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000976}
977
deadbeefab9b2d12015-10-14 11:33:11 -0700978// Test that the created offer includes streams we added.
979TEST_F(PeerConnectionInterfaceTest, AddedStreamsPresentInOffer) {
980 CreatePeerConnection();
981 AddAudioVideoStream(kStreamLabel1, "audio_track", "video_track");
kwibergd1fe2812016-04-27 06:47:29 -0700982 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -0700983 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -0700984
985 const cricket::ContentInfo* audio_content =
986 cricket::GetFirstAudioContent(offer->description());
987 const cricket::AudioContentDescription* audio_desc =
988 static_cast<const cricket::AudioContentDescription*>(
989 audio_content->description);
990 EXPECT_TRUE(
991 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
992
993 const cricket::ContentInfo* video_content =
994 cricket::GetFirstVideoContent(offer->description());
995 const cricket::VideoContentDescription* video_desc =
996 static_cast<const cricket::VideoContentDescription*>(
997 video_content->description);
998 EXPECT_TRUE(
999 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1000
1001 // Add another stream and ensure the offer includes both the old and new
1002 // streams.
1003 AddAudioVideoStream(kStreamLabel2, "audio_track2", "video_track2");
kwiberg2bbff992016-03-16 11:03:04 -07001004 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07001005
1006 audio_content = cricket::GetFirstAudioContent(offer->description());
1007 audio_desc = static_cast<const cricket::AudioContentDescription*>(
1008 audio_content->description);
1009 EXPECT_TRUE(
1010 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
1011 EXPECT_TRUE(
1012 ContainsTrack(audio_desc->streams(), kStreamLabel2, "audio_track2"));
1013
1014 video_content = cricket::GetFirstVideoContent(offer->description());
1015 video_desc = static_cast<const cricket::VideoContentDescription*>(
1016 video_content->description);
1017 EXPECT_TRUE(
1018 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1019 EXPECT_TRUE(
1020 ContainsTrack(video_desc->streams(), kStreamLabel2, "video_track2"));
1021}
1022
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001023TEST_F(PeerConnectionInterfaceTest, RemoveStream) {
1024 CreatePeerConnection();
deadbeefab9b2d12015-10-14 11:33:11 -07001025 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001026 ASSERT_EQ(1u, pc_->local_streams()->count());
1027 pc_->RemoveStream(pc_->local_streams()->at(0));
1028 EXPECT_EQ(0u, pc_->local_streams()->count());
1029}
1030
deadbeefe1f9d832016-01-14 15:35:42 -08001031// Test for AddTrack and RemoveTrack methods.
1032// Tests that the created offer includes tracks we added,
1033// and that the RtpSenders are created correctly.
1034// Also tests that RemoveTrack removes the tracks from subsequent offers.
1035TEST_F(PeerConnectionInterfaceTest, AddTrackRemoveTrack) {
1036 CreatePeerConnection();
1037 // Create a dummy stream, so tracks share a stream label.
1038 scoped_refptr<MediaStreamInterface> stream(
1039 pc_factory_->CreateLocalMediaStream(kStreamLabel1));
1040 std::vector<MediaStreamInterface*> stream_list;
1041 stream_list.push_back(stream.get());
1042 scoped_refptr<AudioTrackInterface> audio_track(
1043 pc_factory_->CreateAudioTrack("audio_track", nullptr));
nisseaf510af2016-03-21 08:20:42 -07001044 scoped_refptr<VideoTrackInterface> video_track(pc_factory_->CreateVideoTrack(
1045 "video_track",
1046 pc_factory_->CreateVideoSource(new cricket::FakeVideoCapturer())));
deadbeefe1f9d832016-01-14 15:35:42 -08001047 auto audio_sender = pc_->AddTrack(audio_track, stream_list);
1048 auto video_sender = pc_->AddTrack(video_track, stream_list);
1049 EXPECT_EQ(kStreamLabel1, audio_sender->stream_id());
1050 EXPECT_EQ("audio_track", audio_sender->id());
1051 EXPECT_EQ(audio_track, audio_sender->track());
1052 EXPECT_EQ(kStreamLabel1, video_sender->stream_id());
1053 EXPECT_EQ("video_track", video_sender->id());
1054 EXPECT_EQ(video_track, video_sender->track());
1055
1056 // Now create an offer and check for the senders.
kwibergd1fe2812016-04-27 06:47:29 -07001057 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001058 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefe1f9d832016-01-14 15:35:42 -08001059
1060 const cricket::ContentInfo* audio_content =
1061 cricket::GetFirstAudioContent(offer->description());
1062 const cricket::AudioContentDescription* audio_desc =
1063 static_cast<const cricket::AudioContentDescription*>(
1064 audio_content->description);
1065 EXPECT_TRUE(
1066 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
1067
1068 const cricket::ContentInfo* video_content =
1069 cricket::GetFirstVideoContent(offer->description());
1070 const cricket::VideoContentDescription* video_desc =
1071 static_cast<const cricket::VideoContentDescription*>(
1072 video_content->description);
1073 EXPECT_TRUE(
1074 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1075
1076 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
1077
1078 // Now try removing the tracks.
1079 EXPECT_TRUE(pc_->RemoveTrack(audio_sender));
1080 EXPECT_TRUE(pc_->RemoveTrack(video_sender));
1081
1082 // Create a new offer and ensure it doesn't contain the removed senders.
kwiberg2bbff992016-03-16 11:03:04 -07001083 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefe1f9d832016-01-14 15:35:42 -08001084
1085 audio_content = cricket::GetFirstAudioContent(offer->description());
1086 audio_desc = static_cast<const cricket::AudioContentDescription*>(
1087 audio_content->description);
1088 EXPECT_FALSE(
1089 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
1090
1091 video_content = cricket::GetFirstVideoContent(offer->description());
1092 video_desc = static_cast<const cricket::VideoContentDescription*>(
1093 video_content->description);
1094 EXPECT_FALSE(
1095 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1096
1097 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
1098
1099 // Calling RemoveTrack on a sender no longer attached to a PeerConnection
1100 // should return false.
1101 EXPECT_FALSE(pc_->RemoveTrack(audio_sender));
1102 EXPECT_FALSE(pc_->RemoveTrack(video_sender));
1103}
1104
1105// Test creating senders without a stream specified,
1106// expecting a random stream ID to be generated.
1107TEST_F(PeerConnectionInterfaceTest, AddTrackWithoutStream) {
1108 CreatePeerConnection();
1109 // Create a dummy stream, so tracks share a stream label.
1110 scoped_refptr<AudioTrackInterface> audio_track(
1111 pc_factory_->CreateAudioTrack("audio_track", nullptr));
nisseaf510af2016-03-21 08:20:42 -07001112 scoped_refptr<VideoTrackInterface> video_track(pc_factory_->CreateVideoTrack(
1113 "video_track",
1114 pc_factory_->CreateVideoSource(new cricket::FakeVideoCapturer())));
deadbeefe1f9d832016-01-14 15:35:42 -08001115 auto audio_sender =
1116 pc_->AddTrack(audio_track, std::vector<MediaStreamInterface*>());
1117 auto video_sender =
1118 pc_->AddTrack(video_track, std::vector<MediaStreamInterface*>());
1119 EXPECT_EQ("audio_track", audio_sender->id());
1120 EXPECT_EQ(audio_track, audio_sender->track());
1121 EXPECT_EQ("video_track", video_sender->id());
1122 EXPECT_EQ(video_track, video_sender->track());
1123 // If the ID is truly a random GUID, it should be infinitely unlikely they
1124 // will be the same.
1125 EXPECT_NE(video_sender->stream_id(), audio_sender->stream_id());
1126}
1127
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001128TEST_F(PeerConnectionInterfaceTest, CreateOfferReceiveAnswer) {
1129 InitiateCall();
1130 WaitAndVerifyOnAddStream(kStreamLabel1);
1131 VerifyRemoteRtpHeaderExtensions();
1132}
1133
1134TEST_F(PeerConnectionInterfaceTest, CreateOfferReceivePrAnswerAndAnswer) {
1135 CreatePeerConnection();
deadbeefab9b2d12015-10-14 11:33:11 -07001136 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001137 CreateOfferAsLocalDescription();
1138 std::string offer;
1139 EXPECT_TRUE(pc_->local_description()->ToString(&offer));
1140 CreatePrAnswerAndAnswerAsRemoteDescription(offer);
1141 WaitAndVerifyOnAddStream(kStreamLabel1);
1142}
1143
1144TEST_F(PeerConnectionInterfaceTest, ReceiveOfferCreateAnswer) {
1145 CreatePeerConnection();
deadbeefab9b2d12015-10-14 11:33:11 -07001146 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001147
1148 CreateOfferAsRemoteDescription();
1149 CreateAnswerAsLocalDescription();
1150
1151 WaitAndVerifyOnAddStream(kStreamLabel1);
1152}
1153
1154TEST_F(PeerConnectionInterfaceTest, ReceiveOfferCreatePrAnswerAndAnswer) {
1155 CreatePeerConnection();
deadbeefab9b2d12015-10-14 11:33:11 -07001156 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001157
1158 CreateOfferAsRemoteDescription();
1159 CreatePrAnswerAsLocalDescription();
1160 CreateAnswerAsLocalDescription();
1161
1162 WaitAndVerifyOnAddStream(kStreamLabel1);
1163}
1164
1165TEST_F(PeerConnectionInterfaceTest, Renegotiate) {
1166 InitiateCall();
1167 ASSERT_EQ(1u, pc_->remote_streams()->count());
1168 pc_->RemoveStream(pc_->local_streams()->at(0));
1169 CreateOfferReceiveAnswer();
1170 EXPECT_EQ(0u, pc_->remote_streams()->count());
deadbeefab9b2d12015-10-14 11:33:11 -07001171 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001172 CreateOfferReceiveAnswer();
1173}
1174
1175// Tests that after negotiating an audio only call, the respondent can perform a
1176// renegotiation that removes the audio stream.
1177TEST_F(PeerConnectionInterfaceTest, RenegotiateAudioOnly) {
1178 CreatePeerConnection();
1179 AddVoiceStream(kStreamLabel1);
1180 CreateOfferAsRemoteDescription();
1181 CreateAnswerAsLocalDescription();
1182
1183 ASSERT_EQ(1u, pc_->remote_streams()->count());
1184 pc_->RemoveStream(pc_->local_streams()->at(0));
1185 CreateOfferReceiveAnswer();
1186 EXPECT_EQ(0u, pc_->remote_streams()->count());
1187}
1188
1189// Test that candidates are generated and that we can parse our own candidates.
1190TEST_F(PeerConnectionInterfaceTest, IceCandidates) {
1191 CreatePeerConnection();
1192
1193 EXPECT_FALSE(pc_->AddIceCandidate(observer_.last_candidate_.get()));
1194 // SetRemoteDescription takes ownership of offer.
kwibergd1fe2812016-04-27 06:47:29 -07001195 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefab9b2d12015-10-14 11:33:11 -07001196 AddVideoStream(kStreamLabel1);
deadbeefc80741f2015-10-22 13:14:45 -07001197 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
kwiberg2bbff992016-03-16 11:03:04 -07001198 EXPECT_TRUE(DoSetRemoteDescription(offer.release()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001199
1200 // SetLocalDescription takes ownership of answer.
kwibergd1fe2812016-04-27 06:47:29 -07001201 std::unique_ptr<SessionDescriptionInterface> answer;
deadbeefc80741f2015-10-22 13:14:45 -07001202 EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
kwiberg2bbff992016-03-16 11:03:04 -07001203 EXPECT_TRUE(DoSetLocalDescription(answer.release()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001204
1205 EXPECT_TRUE_WAIT(observer_.last_candidate_.get() != NULL, kTimeout);
1206 EXPECT_TRUE_WAIT(observer_.ice_complete_, kTimeout);
1207
1208 EXPECT_TRUE(pc_->AddIceCandidate(observer_.last_candidate_.get()));
1209}
1210
deadbeefab9b2d12015-10-14 11:33:11 -07001211// Test that CreateOffer and CreateAnswer will fail if the track labels are
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001212// not unique.
1213TEST_F(PeerConnectionInterfaceTest, CreateOfferAnswerWithInvalidStream) {
1214 CreatePeerConnection();
1215 // Create a regular offer for the CreateAnswer test later.
kwibergd1fe2812016-04-27 06:47:29 -07001216 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefc80741f2015-10-22 13:14:45 -07001217 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
kwiberg2bbff992016-03-16 11:03:04 -07001218 EXPECT_TRUE(offer);
1219 offer.reset();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001220
1221 // Create a local stream with audio&video tracks having same label.
1222 AddAudioVideoStream(kStreamLabel1, "track_label", "track_label");
1223
1224 // Test CreateOffer
deadbeefc80741f2015-10-22 13:14:45 -07001225 EXPECT_FALSE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001226
1227 // Test CreateAnswer
kwibergd1fe2812016-04-27 06:47:29 -07001228 std::unique_ptr<SessionDescriptionInterface> answer;
deadbeefc80741f2015-10-22 13:14:45 -07001229 EXPECT_FALSE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001230}
1231
1232// Test that we will get different SSRCs for each tracks in the offer and answer
1233// we created.
1234TEST_F(PeerConnectionInterfaceTest, SsrcInOfferAnswer) {
1235 CreatePeerConnection();
1236 // Create a local stream with audio&video tracks having different labels.
1237 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
1238
1239 // Test CreateOffer
kwibergd1fe2812016-04-27 06:47:29 -07001240 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001241 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001242 int audio_ssrc = 0;
1243 int video_ssrc = 0;
1244 EXPECT_TRUE(GetFirstSsrc(GetFirstAudioContent(offer->description()),
1245 &audio_ssrc));
1246 EXPECT_TRUE(GetFirstSsrc(GetFirstVideoContent(offer->description()),
1247 &video_ssrc));
1248 EXPECT_NE(audio_ssrc, video_ssrc);
1249
1250 // Test CreateAnswer
1251 EXPECT_TRUE(DoSetRemoteDescription(offer.release()));
kwibergd1fe2812016-04-27 06:47:29 -07001252 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -07001253 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001254 audio_ssrc = 0;
1255 video_ssrc = 0;
1256 EXPECT_TRUE(GetFirstSsrc(GetFirstAudioContent(answer->description()),
1257 &audio_ssrc));
1258 EXPECT_TRUE(GetFirstSsrc(GetFirstVideoContent(answer->description()),
1259 &video_ssrc));
1260 EXPECT_NE(audio_ssrc, video_ssrc);
1261}
1262
deadbeefeb459812015-12-15 19:24:43 -08001263// Test that it's possible to call AddTrack on a MediaStream after adding
1264// the stream to a PeerConnection.
1265// TODO(deadbeef): Remove this test once this behavior is no longer supported.
1266TEST_F(PeerConnectionInterfaceTest, AddTrackAfterAddStream) {
1267 CreatePeerConnection();
1268 // Create audio stream and add to PeerConnection.
1269 AddVoiceStream(kStreamLabel1);
1270 MediaStreamInterface* stream = pc_->local_streams()->at(0);
1271
1272 // Add video track to the audio-only stream.
nisseaf510af2016-03-21 08:20:42 -07001273 scoped_refptr<VideoTrackInterface> video_track(pc_factory_->CreateVideoTrack(
1274 "video_label",
1275 pc_factory_->CreateVideoSource(new cricket::FakeVideoCapturer())));
deadbeefeb459812015-12-15 19:24:43 -08001276 stream->AddTrack(video_track.get());
1277
kwibergd1fe2812016-04-27 06:47:29 -07001278 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001279 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefeb459812015-12-15 19:24:43 -08001280
1281 const cricket::MediaContentDescription* video_desc =
1282 cricket::GetFirstVideoContentDescription(offer->description());
1283 EXPECT_TRUE(video_desc != nullptr);
1284}
1285
1286// Test that it's possible to call RemoveTrack on a MediaStream after adding
1287// the stream to a PeerConnection.
1288// TODO(deadbeef): Remove this test once this behavior is no longer supported.
1289TEST_F(PeerConnectionInterfaceTest, RemoveTrackAfterAddStream) {
1290 CreatePeerConnection();
1291 // Create audio/video stream and add to PeerConnection.
1292 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
1293 MediaStreamInterface* stream = pc_->local_streams()->at(0);
1294
1295 // Remove the video track.
1296 stream->RemoveTrack(stream->GetVideoTracks()[0]);
1297
kwibergd1fe2812016-04-27 06:47:29 -07001298 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001299 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefeb459812015-12-15 19:24:43 -08001300
1301 const cricket::MediaContentDescription* video_desc =
1302 cricket::GetFirstVideoContentDescription(offer->description());
1303 EXPECT_TRUE(video_desc == nullptr);
1304}
1305
deadbeefbd7d8f72015-12-18 16:58:44 -08001306// Test creating a sender with a stream ID, and ensure the ID is populated
1307// in the offer.
1308TEST_F(PeerConnectionInterfaceTest, CreateSenderWithStream) {
1309 CreatePeerConnection();
1310 pc_->CreateSender("video", kStreamLabel1);
1311
kwibergd1fe2812016-04-27 06:47:29 -07001312 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001313 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefbd7d8f72015-12-18 16:58:44 -08001314
1315 const cricket::MediaContentDescription* video_desc =
1316 cricket::GetFirstVideoContentDescription(offer->description());
1317 ASSERT_TRUE(video_desc != nullptr);
1318 ASSERT_EQ(1u, video_desc->streams().size());
1319 EXPECT_EQ(kStreamLabel1, video_desc->streams()[0].sync_label);
1320}
1321
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001322// Test that we can specify a certain track that we want statistics about.
1323TEST_F(PeerConnectionInterfaceTest, GetStatsForSpecificTrack) {
1324 InitiateCall();
1325 ASSERT_LT(0u, pc_->remote_streams()->count());
1326 ASSERT_LT(0u, pc_->remote_streams()->at(0)->GetAudioTracks().size());
1327 scoped_refptr<MediaStreamTrackInterface> remote_audio =
1328 pc_->remote_streams()->at(0)->GetAudioTracks()[0];
1329 EXPECT_TRUE(DoGetStats(remote_audio));
1330
1331 // Remove the stream. Since we are sending to our selves the local
1332 // and the remote stream is the same.
1333 pc_->RemoveStream(pc_->local_streams()->at(0));
1334 // Do a re-negotiation.
1335 CreateOfferReceiveAnswer();
1336
1337 ASSERT_EQ(0u, pc_->remote_streams()->count());
1338
1339 // Test that we still can get statistics for the old track. Even if it is not
1340 // sent any longer.
1341 EXPECT_TRUE(DoGetStats(remote_audio));
1342}
1343
1344// Test that we can get stats on a video track.
1345TEST_F(PeerConnectionInterfaceTest, GetStatsForVideoTrack) {
1346 InitiateCall();
1347 ASSERT_LT(0u, pc_->remote_streams()->count());
1348 ASSERT_LT(0u, pc_->remote_streams()->at(0)->GetVideoTracks().size());
1349 scoped_refptr<MediaStreamTrackInterface> remote_video =
1350 pc_->remote_streams()->at(0)->GetVideoTracks()[0];
1351 EXPECT_TRUE(DoGetStats(remote_video));
1352}
1353
1354// Test that we don't get statistics for an invalid track.
tommi@webrtc.org908f57e2014-07-21 11:44:39 +00001355// TODO(tommi): Fix this test. DoGetStats will return true
1356// for the unknown track (since GetStats is async), but no
1357// data is returned for the track.
1358TEST_F(PeerConnectionInterfaceTest, DISABLED_GetStatsForInvalidTrack) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001359 InitiateCall();
1360 scoped_refptr<AudioTrackInterface> unknown_audio_track(
1361 pc_factory_->CreateAudioTrack("unknown track", NULL));
1362 EXPECT_FALSE(DoGetStats(unknown_audio_track));
1363}
1364
1365// This test setup two RTP data channels in loop back.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001366TEST_F(PeerConnectionInterfaceTest, TestDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001367 FakeConstraints constraints;
1368 constraints.SetAllowRtpDataChannels();
1369 CreatePeerConnection(&constraints);
1370 scoped_refptr<DataChannelInterface> data1 =
1371 pc_->CreateDataChannel("test1", NULL);
1372 scoped_refptr<DataChannelInterface> data2 =
1373 pc_->CreateDataChannel("test2", NULL);
1374 ASSERT_TRUE(data1 != NULL);
kwibergd1fe2812016-04-27 06:47:29 -07001375 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001376 new MockDataChannelObserver(data1));
kwibergd1fe2812016-04-27 06:47:29 -07001377 std::unique_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001378 new MockDataChannelObserver(data2));
1379
1380 EXPECT_EQ(DataChannelInterface::kConnecting, data1->state());
1381 EXPECT_EQ(DataChannelInterface::kConnecting, data2->state());
1382 std::string data_to_send1 = "testing testing";
1383 std::string data_to_send2 = "testing something else";
1384 EXPECT_FALSE(data1->Send(DataBuffer(data_to_send1)));
1385
1386 CreateOfferReceiveAnswer();
1387 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1388 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
1389
1390 EXPECT_EQ(DataChannelInterface::kOpen, data1->state());
1391 EXPECT_EQ(DataChannelInterface::kOpen, data2->state());
1392 EXPECT_TRUE(data1->Send(DataBuffer(data_to_send1)));
1393 EXPECT_TRUE(data2->Send(DataBuffer(data_to_send2)));
1394
1395 EXPECT_EQ_WAIT(data_to_send1, observer1->last_message(), kTimeout);
1396 EXPECT_EQ_WAIT(data_to_send2, observer2->last_message(), kTimeout);
1397
1398 data1->Close();
1399 EXPECT_EQ(DataChannelInterface::kClosing, data1->state());
1400 CreateOfferReceiveAnswer();
1401 EXPECT_FALSE(observer1->IsOpen());
1402 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
1403 EXPECT_TRUE(observer2->IsOpen());
1404
1405 data_to_send2 = "testing something else again";
1406 EXPECT_TRUE(data2->Send(DataBuffer(data_to_send2)));
1407
1408 EXPECT_EQ_WAIT(data_to_send2, observer2->last_message(), kTimeout);
1409}
1410
1411// This test verifies that sendnig binary data over RTP data channels should
1412// fail.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001413TEST_F(PeerConnectionInterfaceTest, TestSendBinaryOnRtpDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001414 FakeConstraints constraints;
1415 constraints.SetAllowRtpDataChannels();
1416 CreatePeerConnection(&constraints);
1417 scoped_refptr<DataChannelInterface> data1 =
1418 pc_->CreateDataChannel("test1", NULL);
1419 scoped_refptr<DataChannelInterface> data2 =
1420 pc_->CreateDataChannel("test2", NULL);
1421 ASSERT_TRUE(data1 != NULL);
kwibergd1fe2812016-04-27 06:47:29 -07001422 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001423 new MockDataChannelObserver(data1));
kwibergd1fe2812016-04-27 06:47:29 -07001424 std::unique_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001425 new MockDataChannelObserver(data2));
1426
1427 EXPECT_EQ(DataChannelInterface::kConnecting, data1->state());
1428 EXPECT_EQ(DataChannelInterface::kConnecting, data2->state());
1429
1430 CreateOfferReceiveAnswer();
1431 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1432 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
1433
1434 EXPECT_EQ(DataChannelInterface::kOpen, data1->state());
1435 EXPECT_EQ(DataChannelInterface::kOpen, data2->state());
1436
jbaucheec21bd2016-03-20 06:15:43 -07001437 rtc::CopyOnWriteBuffer buffer("test", 4);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001438 EXPECT_FALSE(data1->Send(DataBuffer(buffer, true)));
1439}
1440
1441// This test setup a RTP data channels in loop back and test that a channel is
1442// opened even if the remote end answer with a zero SSRC.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001443TEST_F(PeerConnectionInterfaceTest, TestSendOnlyDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001444 FakeConstraints constraints;
1445 constraints.SetAllowRtpDataChannels();
1446 CreatePeerConnection(&constraints);
1447 scoped_refptr<DataChannelInterface> data1 =
1448 pc_->CreateDataChannel("test1", NULL);
kwibergd1fe2812016-04-27 06:47:29 -07001449 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001450 new MockDataChannelObserver(data1));
1451
1452 CreateOfferReceiveAnswerWithoutSsrc();
1453
1454 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1455
1456 data1->Close();
1457 EXPECT_EQ(DataChannelInterface::kClosing, data1->state());
1458 CreateOfferReceiveAnswerWithoutSsrc();
1459 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
1460 EXPECT_FALSE(observer1->IsOpen());
1461}
1462
1463// This test that if a data channel is added in an answer a receive only channel
1464// channel is created.
1465TEST_F(PeerConnectionInterfaceTest, TestReceiveOnlyDataChannel) {
1466 FakeConstraints constraints;
1467 constraints.SetAllowRtpDataChannels();
1468 CreatePeerConnection(&constraints);
1469
1470 std::string offer_label = "offer_channel";
1471 scoped_refptr<DataChannelInterface> offer_channel =
1472 pc_->CreateDataChannel(offer_label, NULL);
1473
1474 CreateOfferAsLocalDescription();
1475
1476 // Replace the data channel label in the offer and apply it as an answer.
1477 std::string receive_label = "answer_channel";
1478 std::string sdp;
1479 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001480 rtc::replace_substrs(offer_label.c_str(), offer_label.length(),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001481 receive_label.c_str(), receive_label.length(),
1482 &sdp);
1483 CreateAnswerAsRemoteDescription(sdp);
1484
1485 // Verify that a new incoming data channel has been created and that
1486 // it is open but can't we written to.
1487 ASSERT_TRUE(observer_.last_datachannel_ != NULL);
1488 DataChannelInterface* received_channel = observer_.last_datachannel_;
1489 EXPECT_EQ(DataChannelInterface::kConnecting, received_channel->state());
1490 EXPECT_EQ(receive_label, received_channel->label());
1491 EXPECT_FALSE(received_channel->Send(DataBuffer("something")));
1492
1493 // Verify that the channel we initially offered has been rejected.
1494 EXPECT_EQ(DataChannelInterface::kClosed, offer_channel->state());
1495
1496 // Do another offer / answer exchange and verify that the data channel is
1497 // opened.
1498 CreateOfferReceiveAnswer();
1499 EXPECT_EQ_WAIT(DataChannelInterface::kOpen, received_channel->state(),
1500 kTimeout);
1501}
1502
1503// This test that no data channel is returned if a reliable channel is
1504// requested.
1505// TODO(perkj): Remove this test once reliable channels are implemented.
1506TEST_F(PeerConnectionInterfaceTest, CreateReliableRtpDataChannelShouldFail) {
1507 FakeConstraints constraints;
1508 constraints.SetAllowRtpDataChannels();
1509 CreatePeerConnection(&constraints);
1510
1511 std::string label = "test";
1512 webrtc::DataChannelInit config;
1513 config.reliable = true;
1514 scoped_refptr<DataChannelInterface> channel =
1515 pc_->CreateDataChannel(label, &config);
1516 EXPECT_TRUE(channel == NULL);
1517}
1518
deadbeefab9b2d12015-10-14 11:33:11 -07001519// Verifies that duplicated label is not allowed for RTP data channel.
1520TEST_F(PeerConnectionInterfaceTest, RtpDuplicatedLabelNotAllowed) {
1521 FakeConstraints constraints;
1522 constraints.SetAllowRtpDataChannels();
1523 CreatePeerConnection(&constraints);
1524
1525 std::string label = "test";
1526 scoped_refptr<DataChannelInterface> channel =
1527 pc_->CreateDataChannel(label, nullptr);
1528 EXPECT_NE(channel, nullptr);
1529
1530 scoped_refptr<DataChannelInterface> dup_channel =
1531 pc_->CreateDataChannel(label, nullptr);
1532 EXPECT_EQ(dup_channel, nullptr);
1533}
1534
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001535// This tests that a SCTP data channel is returned using different
1536// DataChannelInit configurations.
1537TEST_F(PeerConnectionInterfaceTest, CreateSctpDataChannel) {
1538 FakeConstraints constraints;
1539 constraints.SetAllowDtlsSctpDataChannels();
1540 CreatePeerConnection(&constraints);
1541
1542 webrtc::DataChannelInit config;
1543
1544 scoped_refptr<DataChannelInterface> channel =
1545 pc_->CreateDataChannel("1", &config);
1546 EXPECT_TRUE(channel != NULL);
1547 EXPECT_TRUE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001548 EXPECT_TRUE(observer_.renegotiation_needed_);
1549 observer_.renegotiation_needed_ = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001550
1551 config.ordered = false;
1552 channel = pc_->CreateDataChannel("2", &config);
1553 EXPECT_TRUE(channel != NULL);
1554 EXPECT_TRUE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001555 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001556
1557 config.ordered = true;
1558 config.maxRetransmits = 0;
1559 channel = pc_->CreateDataChannel("3", &config);
1560 EXPECT_TRUE(channel != NULL);
1561 EXPECT_FALSE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001562 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001563
1564 config.maxRetransmits = -1;
1565 config.maxRetransmitTime = 0;
1566 channel = pc_->CreateDataChannel("4", &config);
1567 EXPECT_TRUE(channel != NULL);
1568 EXPECT_FALSE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001569 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001570}
1571
1572// This tests that no data channel is returned if both maxRetransmits and
1573// maxRetransmitTime are set for SCTP data channels.
1574TEST_F(PeerConnectionInterfaceTest,
1575 CreateSctpDataChannelShouldFailForInvalidConfig) {
1576 FakeConstraints constraints;
1577 constraints.SetAllowDtlsSctpDataChannels();
1578 CreatePeerConnection(&constraints);
1579
1580 std::string label = "test";
1581 webrtc::DataChannelInit config;
1582 config.maxRetransmits = 0;
1583 config.maxRetransmitTime = 0;
1584
1585 scoped_refptr<DataChannelInterface> channel =
1586 pc_->CreateDataChannel(label, &config);
1587 EXPECT_TRUE(channel == NULL);
1588}
1589
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001590// The test verifies that creating a SCTP data channel with an id already in use
1591// or out of range should fail.
1592TEST_F(PeerConnectionInterfaceTest,
1593 CreateSctpDataChannelWithInvalidIdShouldFail) {
1594 FakeConstraints constraints;
1595 constraints.SetAllowDtlsSctpDataChannels();
1596 CreatePeerConnection(&constraints);
1597
1598 webrtc::DataChannelInit config;
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001599 scoped_refptr<DataChannelInterface> channel;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001600
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001601 config.id = 1;
1602 channel = pc_->CreateDataChannel("1", &config);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001603 EXPECT_TRUE(channel != NULL);
1604 EXPECT_EQ(1, channel->id());
1605
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001606 channel = pc_->CreateDataChannel("x", &config);
1607 EXPECT_TRUE(channel == NULL);
1608
1609 config.id = cricket::kMaxSctpSid;
1610 channel = pc_->CreateDataChannel("max", &config);
1611 EXPECT_TRUE(channel != NULL);
1612 EXPECT_EQ(config.id, channel->id());
1613
1614 config.id = cricket::kMaxSctpSid + 1;
1615 channel = pc_->CreateDataChannel("x", &config);
1616 EXPECT_TRUE(channel == NULL);
1617}
1618
deadbeefab9b2d12015-10-14 11:33:11 -07001619// Verifies that duplicated label is allowed for SCTP data channel.
1620TEST_F(PeerConnectionInterfaceTest, SctpDuplicatedLabelAllowed) {
1621 FakeConstraints constraints;
1622 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
1623 true);
1624 CreatePeerConnection(&constraints);
1625
1626 std::string label = "test";
1627 scoped_refptr<DataChannelInterface> channel =
1628 pc_->CreateDataChannel(label, nullptr);
1629 EXPECT_NE(channel, nullptr);
1630
1631 scoped_refptr<DataChannelInterface> dup_channel =
1632 pc_->CreateDataChannel(label, nullptr);
1633 EXPECT_NE(dup_channel, nullptr);
1634}
1635
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001636// This test verifies that OnRenegotiationNeeded is fired for every new RTP
1637// DataChannel.
1638TEST_F(PeerConnectionInterfaceTest, RenegotiationNeededForNewRtpDataChannel) {
1639 FakeConstraints constraints;
1640 constraints.SetAllowRtpDataChannels();
1641 CreatePeerConnection(&constraints);
1642
1643 scoped_refptr<DataChannelInterface> dc1 =
1644 pc_->CreateDataChannel("test1", NULL);
1645 EXPECT_TRUE(observer_.renegotiation_needed_);
1646 observer_.renegotiation_needed_ = false;
1647
1648 scoped_refptr<DataChannelInterface> dc2 =
1649 pc_->CreateDataChannel("test2", NULL);
1650 EXPECT_TRUE(observer_.renegotiation_needed_);
1651}
1652
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001653// This test that a data channel closes when a PeerConnection is deleted/closed.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001654TEST_F(PeerConnectionInterfaceTest, DataChannelCloseWhenPeerConnectionClose) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001655 FakeConstraints constraints;
1656 constraints.SetAllowRtpDataChannels();
1657 CreatePeerConnection(&constraints);
1658
1659 scoped_refptr<DataChannelInterface> data1 =
1660 pc_->CreateDataChannel("test1", NULL);
1661 scoped_refptr<DataChannelInterface> data2 =
1662 pc_->CreateDataChannel("test2", NULL);
1663 ASSERT_TRUE(data1 != NULL);
kwibergd1fe2812016-04-27 06:47:29 -07001664 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001665 new MockDataChannelObserver(data1));
kwibergd1fe2812016-04-27 06:47:29 -07001666 std::unique_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001667 new MockDataChannelObserver(data2));
1668
1669 CreateOfferReceiveAnswer();
1670 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1671 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
1672
1673 ReleasePeerConnection();
1674 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
1675 EXPECT_EQ(DataChannelInterface::kClosed, data2->state());
1676}
1677
1678// This test that data channels can be rejected in an answer.
1679TEST_F(PeerConnectionInterfaceTest, TestRejectDataChannelInAnswer) {
1680 FakeConstraints constraints;
1681 constraints.SetAllowRtpDataChannels();
1682 CreatePeerConnection(&constraints);
1683
1684 scoped_refptr<DataChannelInterface> offer_channel(
1685 pc_->CreateDataChannel("offer_channel", NULL));
1686
1687 CreateOfferAsLocalDescription();
1688
1689 // Create an answer where the m-line for data channels are rejected.
1690 std::string sdp;
1691 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
1692 webrtc::JsepSessionDescription* answer = new webrtc::JsepSessionDescription(
1693 SessionDescriptionInterface::kAnswer);
1694 EXPECT_TRUE(answer->Initialize(sdp, NULL));
1695 cricket::ContentInfo* data_info =
1696 answer->description()->GetContentByName("data");
1697 data_info->rejected = true;
1698
1699 DoSetRemoteDescription(answer);
1700 EXPECT_EQ(DataChannelInterface::kClosed, offer_channel->state());
1701}
1702
1703// Test that we can create a session description from an SDP string from
1704// FireFox, use it as a remote session description, generate an answer and use
1705// the answer as a local description.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07001706TEST_F(PeerConnectionInterfaceTest, ReceiveFireFoxOffer) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001707 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001708 FakeConstraints constraints;
1709 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
1710 true);
1711 CreatePeerConnection(&constraints);
1712 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
1713 SessionDescriptionInterface* desc =
1714 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
jbauchfabe2c92015-07-16 13:43:14 -07001715 webrtc::kFireFoxSdpOffer, nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001716 EXPECT_TRUE(DoSetSessionDescription(desc, false));
1717 CreateAnswerAsLocalDescription();
1718 ASSERT_TRUE(pc_->local_description() != NULL);
1719 ASSERT_TRUE(pc_->remote_description() != NULL);
1720
1721 const cricket::ContentInfo* content =
1722 cricket::GetFirstAudioContent(pc_->local_description()->description());
1723 ASSERT_TRUE(content != NULL);
1724 EXPECT_FALSE(content->rejected);
1725
1726 content =
1727 cricket::GetFirstVideoContent(pc_->local_description()->description());
1728 ASSERT_TRUE(content != NULL);
1729 EXPECT_FALSE(content->rejected);
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00001730#ifdef HAVE_SCTP
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001731 content =
1732 cricket::GetFirstDataContent(pc_->local_description()->description());
1733 ASSERT_TRUE(content != NULL);
1734 EXPECT_TRUE(content->rejected);
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00001735#endif
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001736}
1737
1738// Test that we can create an audio only offer and receive an answer with a
1739// limited set of audio codecs and receive an updated offer with more audio
1740// codecs, where the added codecs are not supported.
1741TEST_F(PeerConnectionInterfaceTest, ReceiveUpdatedAudioOfferWithBadCodecs) {
1742 CreatePeerConnection();
1743 AddVoiceStream("audio_label");
1744 CreateOfferAsLocalDescription();
1745
1746 SessionDescriptionInterface* answer =
1747 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
jbauchfabe2c92015-07-16 13:43:14 -07001748 webrtc::kAudioSdp, nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001749 EXPECT_TRUE(DoSetSessionDescription(answer, false));
1750
1751 SessionDescriptionInterface* updated_offer =
1752 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
jbauchfabe2c92015-07-16 13:43:14 -07001753 webrtc::kAudioSdpWithUnsupportedCodecs,
1754 nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001755 EXPECT_TRUE(DoSetSessionDescription(updated_offer, false));
1756 CreateAnswerAsLocalDescription();
1757}
1758
deadbeefc80741f2015-10-22 13:14:45 -07001759// Test that if we're receiving (but not sending) a track, subsequent offers
1760// will have m-lines with a=recvonly.
1761TEST_F(PeerConnectionInterfaceTest, CreateSubsequentRecvOnlyOffer) {
1762 FakeConstraints constraints;
1763 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
1764 true);
1765 CreatePeerConnection(&constraints);
1766 CreateAndSetRemoteOffer(kSdpStringWithStream1);
1767 CreateAnswerAsLocalDescription();
1768
1769 // At this point we should be receiving stream 1, but not sending anything.
1770 // A new offer should be recvonly.
kwibergd1fe2812016-04-27 06:47:29 -07001771 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefc80741f2015-10-22 13:14:45 -07001772 DoCreateOffer(&offer, nullptr);
1773
1774 const cricket::ContentInfo* video_content =
1775 cricket::GetFirstVideoContent(offer->description());
1776 const cricket::VideoContentDescription* video_desc =
1777 static_cast<const cricket::VideoContentDescription*>(
1778 video_content->description);
1779 ASSERT_EQ(cricket::MD_RECVONLY, video_desc->direction());
1780
1781 const cricket::ContentInfo* audio_content =
1782 cricket::GetFirstAudioContent(offer->description());
1783 const cricket::AudioContentDescription* audio_desc =
1784 static_cast<const cricket::AudioContentDescription*>(
1785 audio_content->description);
1786 ASSERT_EQ(cricket::MD_RECVONLY, audio_desc->direction());
1787}
1788
1789// Test that if we're receiving (but not sending) a track, and the
1790// offerToReceiveVideo/offerToReceiveAudio constraints are explicitly set to
1791// false, the generated m-lines will be a=inactive.
1792TEST_F(PeerConnectionInterfaceTest, CreateSubsequentInactiveOffer) {
1793 FakeConstraints constraints;
1794 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
1795 true);
1796 CreatePeerConnection(&constraints);
1797 CreateAndSetRemoteOffer(kSdpStringWithStream1);
1798 CreateAnswerAsLocalDescription();
1799
1800 // At this point we should be receiving stream 1, but not sending anything.
1801 // A new offer would be recvonly, but we'll set the "no receive" constraints
1802 // to make it inactive.
kwibergd1fe2812016-04-27 06:47:29 -07001803 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefc80741f2015-10-22 13:14:45 -07001804 FakeConstraints offer_constraints;
1805 offer_constraints.AddMandatory(
1806 webrtc::MediaConstraintsInterface::kOfferToReceiveVideo, false);
1807 offer_constraints.AddMandatory(
1808 webrtc::MediaConstraintsInterface::kOfferToReceiveAudio, false);
1809 DoCreateOffer(&offer, &offer_constraints);
1810
1811 const cricket::ContentInfo* video_content =
1812 cricket::GetFirstVideoContent(offer->description());
1813 const cricket::VideoContentDescription* video_desc =
1814 static_cast<const cricket::VideoContentDescription*>(
1815 video_content->description);
1816 ASSERT_EQ(cricket::MD_INACTIVE, video_desc->direction());
1817
1818 const cricket::ContentInfo* audio_content =
1819 cricket::GetFirstAudioContent(offer->description());
1820 const cricket::AudioContentDescription* audio_desc =
1821 static_cast<const cricket::AudioContentDescription*>(
1822 audio_content->description);
1823 ASSERT_EQ(cricket::MD_INACTIVE, audio_desc->direction());
1824}
1825
deadbeef653b8e02015-11-11 12:55:10 -08001826// Test that we can use SetConfiguration to change the ICE servers of the
1827// PortAllocator.
1828TEST_F(PeerConnectionInterfaceTest, SetConfigurationChangesIceServers) {
1829 CreatePeerConnection();
1830
1831 PeerConnectionInterface::RTCConfiguration config;
1832 PeerConnectionInterface::IceServer server;
1833 server.uri = "stun:test_hostname";
1834 config.servers.push_back(server);
1835 EXPECT_TRUE(pc_->SetConfiguration(config));
1836
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08001837 EXPECT_EQ(1u, port_allocator_->stun_servers().size());
1838 EXPECT_EQ("test_hostname",
1839 port_allocator_->stun_servers().begin()->hostname());
deadbeef653b8e02015-11-11 12:55:10 -08001840}
1841
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001842// Test that PeerConnection::Close changes the states to closed and all remote
1843// tracks change state to ended.
1844TEST_F(PeerConnectionInterfaceTest, CloseAndTestStreamsAndStates) {
1845 // Initialize a PeerConnection and negotiate local and remote session
1846 // description.
1847 InitiateCall();
1848 ASSERT_EQ(1u, pc_->local_streams()->count());
1849 ASSERT_EQ(1u, pc_->remote_streams()->count());
1850
1851 pc_->Close();
1852
1853 EXPECT_EQ(PeerConnectionInterface::kClosed, pc_->signaling_state());
1854 EXPECT_EQ(PeerConnectionInterface::kIceConnectionClosed,
1855 pc_->ice_connection_state());
1856 EXPECT_EQ(PeerConnectionInterface::kIceGatheringComplete,
1857 pc_->ice_gathering_state());
1858
1859 EXPECT_EQ(1u, pc_->local_streams()->count());
1860 EXPECT_EQ(1u, pc_->remote_streams()->count());
1861
1862 scoped_refptr<MediaStreamInterface> remote_stream =
1863 pc_->remote_streams()->at(0);
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07001864 // Track state may be updated asynchronously.
perkjd61bf802016-03-24 03:16:19 -07001865 EXPECT_EQ_WAIT(MediaStreamTrackInterface::kEnded,
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07001866 remote_stream->GetAudioTracks()[0]->state(), kTimeout);
1867 EXPECT_EQ_WAIT(MediaStreamTrackInterface::kEnded,
1868 remote_stream->GetVideoTracks()[0]->state(), kTimeout);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001869}
1870
1871// Test that PeerConnection methods fails gracefully after
1872// PeerConnection::Close has been called.
1873TEST_F(PeerConnectionInterfaceTest, CloseAndTestMethods) {
1874 CreatePeerConnection();
1875 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
1876 CreateOfferAsRemoteDescription();
1877 CreateAnswerAsLocalDescription();
1878
1879 ASSERT_EQ(1u, pc_->local_streams()->count());
1880 scoped_refptr<MediaStreamInterface> local_stream =
1881 pc_->local_streams()->at(0);
1882
1883 pc_->Close();
1884
1885 pc_->RemoveStream(local_stream);
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +00001886 EXPECT_FALSE(pc_->AddStream(local_stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001887
1888 ASSERT_FALSE(local_stream->GetAudioTracks().empty());
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001889 rtc::scoped_refptr<webrtc::DtmfSenderInterface> dtmf_sender(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001890 pc_->CreateDtmfSender(local_stream->GetAudioTracks()[0]));
wu@webrtc.org66037362013-08-13 00:09:35 +00001891 EXPECT_TRUE(NULL == dtmf_sender); // local stream has been removed.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001892
1893 EXPECT_TRUE(pc_->CreateDataChannel("test", NULL) == NULL);
1894
1895 EXPECT_TRUE(pc_->local_description() != NULL);
1896 EXPECT_TRUE(pc_->remote_description() != NULL);
1897
kwibergd1fe2812016-04-27 06:47:29 -07001898 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001899 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
kwibergd1fe2812016-04-27 06:47:29 -07001900 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -07001901 EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001902
1903 std::string sdp;
1904 ASSERT_TRUE(pc_->remote_description()->ToString(&sdp));
1905 SessionDescriptionInterface* remote_offer =
1906 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
1907 sdp, NULL);
1908 EXPECT_FALSE(DoSetRemoteDescription(remote_offer));
1909
1910 ASSERT_TRUE(pc_->local_description()->ToString(&sdp));
1911 SessionDescriptionInterface* local_offer =
1912 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
1913 sdp, NULL);
1914 EXPECT_FALSE(DoSetLocalDescription(local_offer));
1915}
1916
1917// Test that GetStats can still be called after PeerConnection::Close.
1918TEST_F(PeerConnectionInterfaceTest, CloseAndGetStats) {
1919 InitiateCall();
1920 pc_->Close();
1921 DoGetStats(NULL);
1922}
deadbeefab9b2d12015-10-14 11:33:11 -07001923
1924// NOTE: The series of tests below come from what used to be
1925// mediastreamsignaling_unittest.cc, and are mostly aimed at testing that
1926// setting a remote or local description has the expected effects.
1927
1928// This test verifies that the remote MediaStreams corresponding to a received
1929// SDP string is created. In this test the two separate MediaStreams are
1930// signaled.
1931TEST_F(PeerConnectionInterfaceTest, UpdateRemoteStreams) {
1932 FakeConstraints constraints;
1933 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
1934 true);
1935 CreatePeerConnection(&constraints);
1936 CreateAndSetRemoteOffer(kSdpStringWithStream1);
1937
1938 rtc::scoped_refptr<StreamCollection> reference(CreateStreamCollection(1));
1939 EXPECT_TRUE(
1940 CompareStreamCollections(observer_.remote_streams(), reference.get()));
1941 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
1942 EXPECT_TRUE(remote_stream->GetVideoTracks()[0]->GetSource() != nullptr);
1943
1944 // Create a session description based on another SDP with another
1945 // MediaStream.
1946 CreateAndSetRemoteOffer(kSdpStringWithStream1And2);
1947
1948 rtc::scoped_refptr<StreamCollection> reference2(CreateStreamCollection(2));
1949 EXPECT_TRUE(
1950 CompareStreamCollections(observer_.remote_streams(), reference2.get()));
1951}
1952
1953// This test verifies that when remote tracks are added/removed from SDP, the
1954// created remote streams are updated appropriately.
1955TEST_F(PeerConnectionInterfaceTest,
1956 AddRemoveTrackFromExistingRemoteMediaStream) {
1957 FakeConstraints constraints;
1958 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
1959 true);
1960 CreatePeerConnection(&constraints);
kwibergd1fe2812016-04-27 06:47:29 -07001961 std::unique_ptr<SessionDescriptionInterface> desc_ms1 =
kwiberg2bbff992016-03-16 11:03:04 -07001962 CreateSessionDescriptionAndReference(1, 1);
deadbeefab9b2d12015-10-14 11:33:11 -07001963 EXPECT_TRUE(DoSetRemoteDescription(desc_ms1.release()));
1964 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
1965 reference_collection_));
1966
1967 // Add extra audio and video tracks to the same MediaStream.
kwibergd1fe2812016-04-27 06:47:29 -07001968 std::unique_ptr<SessionDescriptionInterface> desc_ms1_two_tracks =
kwiberg2bbff992016-03-16 11:03:04 -07001969 CreateSessionDescriptionAndReference(2, 2);
deadbeefab9b2d12015-10-14 11:33:11 -07001970 EXPECT_TRUE(DoSetRemoteDescription(desc_ms1_two_tracks.release()));
1971 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
1972 reference_collection_));
perkjd61bf802016-03-24 03:16:19 -07001973 scoped_refptr<AudioTrackInterface> audio_track2 =
1974 observer_.remote_streams()->at(0)->GetAudioTracks()[1];
1975 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, audio_track2->state());
1976 scoped_refptr<VideoTrackInterface> video_track2 =
1977 observer_.remote_streams()->at(0)->GetVideoTracks()[1];
1978 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, video_track2->state());
deadbeefab9b2d12015-10-14 11:33:11 -07001979
1980 // Remove the extra audio and video tracks.
kwibergd1fe2812016-04-27 06:47:29 -07001981 std::unique_ptr<SessionDescriptionInterface> desc_ms2 =
kwiberg2bbff992016-03-16 11:03:04 -07001982 CreateSessionDescriptionAndReference(1, 1);
perkjd61bf802016-03-24 03:16:19 -07001983 MockTrackObserver audio_track_observer(audio_track2);
1984 MockTrackObserver video_track_observer(video_track2);
1985
1986 EXPECT_CALL(audio_track_observer, OnChanged()).Times(Exactly(1));
1987 EXPECT_CALL(video_track_observer, OnChanged()).Times(Exactly(1));
deadbeefab9b2d12015-10-14 11:33:11 -07001988 EXPECT_TRUE(DoSetRemoteDescription(desc_ms2.release()));
1989 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
1990 reference_collection_));
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07001991 // Track state may be updated asynchronously.
perkjd61bf802016-03-24 03:16:19 -07001992 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07001993 audio_track2->state(), kTimeout);
1994 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
1995 video_track2->state(), kTimeout);
deadbeefab9b2d12015-10-14 11:33:11 -07001996}
1997
1998// This tests that remote tracks are ended if a local session description is set
1999// that rejects the media content type.
2000TEST_F(PeerConnectionInterfaceTest, RejectMediaContent) {
2001 FakeConstraints constraints;
2002 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2003 true);
2004 CreatePeerConnection(&constraints);
2005 // First create and set a remote offer, then reject its video content in our
2006 // answer.
2007 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2008 ASSERT_EQ(1u, observer_.remote_streams()->count());
2009 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2010 ASSERT_EQ(1u, remote_stream->GetVideoTracks().size());
2011 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2012
2013 rtc::scoped_refptr<webrtc::VideoTrackInterface> remote_video =
2014 remote_stream->GetVideoTracks()[0];
2015 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, remote_video->state());
2016 rtc::scoped_refptr<webrtc::AudioTrackInterface> remote_audio =
2017 remote_stream->GetAudioTracks()[0];
2018 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, remote_audio->state());
2019
kwibergd1fe2812016-04-27 06:47:29 -07002020 std::unique_ptr<SessionDescriptionInterface> local_answer;
kwiberg2bbff992016-03-16 11:03:04 -07002021 EXPECT_TRUE(DoCreateAnswer(&local_answer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07002022 cricket::ContentInfo* video_info =
2023 local_answer->description()->GetContentByName("video");
2024 video_info->rejected = true;
2025 EXPECT_TRUE(DoSetLocalDescription(local_answer.release()));
2026 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kEnded, remote_video->state());
2027 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, remote_audio->state());
2028
2029 // Now create an offer where we reject both video and audio.
kwibergd1fe2812016-04-27 06:47:29 -07002030 std::unique_ptr<SessionDescriptionInterface> local_offer;
kwiberg2bbff992016-03-16 11:03:04 -07002031 EXPECT_TRUE(DoCreateOffer(&local_offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07002032 video_info = local_offer->description()->GetContentByName("video");
2033 ASSERT_TRUE(video_info != nullptr);
2034 video_info->rejected = true;
2035 cricket::ContentInfo* audio_info =
2036 local_offer->description()->GetContentByName("audio");
2037 ASSERT_TRUE(audio_info != nullptr);
2038 audio_info->rejected = true;
2039 EXPECT_TRUE(DoSetLocalDescription(local_offer.release()));
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002040 // Track state may be updated asynchronously.
perkjd61bf802016-03-24 03:16:19 -07002041 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002042 remote_audio->state(), kTimeout);
2043 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
2044 remote_video->state(), kTimeout);
deadbeefab9b2d12015-10-14 11:33:11 -07002045}
2046
2047// This tests that we won't crash if the remote track has been removed outside
2048// of PeerConnection and then PeerConnection tries to reject the track.
2049TEST_F(PeerConnectionInterfaceTest, RemoveTrackThenRejectMediaContent) {
2050 FakeConstraints constraints;
2051 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2052 true);
2053 CreatePeerConnection(&constraints);
2054 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2055 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2056 remote_stream->RemoveTrack(remote_stream->GetVideoTracks()[0]);
2057 remote_stream->RemoveTrack(remote_stream->GetAudioTracks()[0]);
2058
kwibergd1fe2812016-04-27 06:47:29 -07002059 std::unique_ptr<SessionDescriptionInterface> local_answer(
deadbeefab9b2d12015-10-14 11:33:11 -07002060 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
2061 kSdpStringWithStream1, nullptr));
2062 cricket::ContentInfo* video_info =
2063 local_answer->description()->GetContentByName("video");
2064 video_info->rejected = true;
2065 cricket::ContentInfo* audio_info =
2066 local_answer->description()->GetContentByName("audio");
2067 audio_info->rejected = true;
2068 EXPECT_TRUE(DoSetLocalDescription(local_answer.release()));
2069
2070 // No crash is a pass.
2071}
2072
deadbeef5e97fb52015-10-15 12:49:08 -07002073// This tests that if a recvonly remote description is set, no remote streams
2074// will be created, even if the description contains SSRCs/MSIDs.
2075// See: https://code.google.com/p/webrtc/issues/detail?id=5054
2076TEST_F(PeerConnectionInterfaceTest, RecvonlyDescriptionDoesntCreateStream) {
2077 FakeConstraints constraints;
2078 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2079 true);
2080 CreatePeerConnection(&constraints);
2081
2082 std::string recvonly_offer = kSdpStringWithStream1;
2083 rtc::replace_substrs(kSendrecv, strlen(kSendrecv), kRecvonly,
2084 strlen(kRecvonly), &recvonly_offer);
2085 CreateAndSetRemoteOffer(recvonly_offer);
2086
2087 EXPECT_EQ(0u, observer_.remote_streams()->count());
2088}
2089
deadbeefab9b2d12015-10-14 11:33:11 -07002090// This tests that a default MediaStream is created if a remote session
2091// description doesn't contain any streams and no MSID support.
2092// It also tests that the default stream is updated if a video m-line is added
2093// in a subsequent session description.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002094TEST_F(PeerConnectionInterfaceTest, SdpWithoutMsidCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002095 FakeConstraints constraints;
2096 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2097 true);
2098 CreatePeerConnection(&constraints);
2099 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2100
2101 ASSERT_EQ(1u, observer_.remote_streams()->count());
2102 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2103
2104 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2105 EXPECT_EQ(0u, remote_stream->GetVideoTracks().size());
2106 EXPECT_EQ("default", remote_stream->label());
2107
2108 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2109 ASSERT_EQ(1u, observer_.remote_streams()->count());
2110 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2111 EXPECT_EQ("defaulta0", remote_stream->GetAudioTracks()[0]->id());
deadbeef884f5852016-01-15 09:20:04 -08002112 EXPECT_EQ(MediaStreamTrackInterface::kLive,
2113 remote_stream->GetAudioTracks()[0]->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002114 ASSERT_EQ(1u, remote_stream->GetVideoTracks().size());
2115 EXPECT_EQ("defaultv0", remote_stream->GetVideoTracks()[0]->id());
deadbeef884f5852016-01-15 09:20:04 -08002116 EXPECT_EQ(MediaStreamTrackInterface::kLive,
2117 remote_stream->GetVideoTracks()[0]->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002118}
2119
2120// This tests that a default MediaStream is created if a remote session
2121// description doesn't contain any streams and media direction is send only.
2122TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002123 SendOnlySdpWithoutMsidCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002124 FakeConstraints constraints;
2125 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2126 true);
2127 CreatePeerConnection(&constraints);
2128 CreateAndSetRemoteOffer(kSdpStringSendOnlyWithoutStreams);
2129
2130 ASSERT_EQ(1u, observer_.remote_streams()->count());
2131 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2132
2133 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2134 EXPECT_EQ(1u, remote_stream->GetVideoTracks().size());
2135 EXPECT_EQ("default", remote_stream->label());
2136}
2137
2138// This tests that it won't crash when PeerConnection tries to remove
2139// a remote track that as already been removed from the MediaStream.
2140TEST_F(PeerConnectionInterfaceTest, RemoveAlreadyGoneRemoteStream) {
2141 FakeConstraints constraints;
2142 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2143 true);
2144 CreatePeerConnection(&constraints);
2145 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2146 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2147 remote_stream->RemoveTrack(remote_stream->GetAudioTracks()[0]);
2148 remote_stream->RemoveTrack(remote_stream->GetVideoTracks()[0]);
2149
2150 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2151
2152 // No crash is a pass.
2153}
2154
2155// This tests that a default MediaStream is created if the remote session
2156// description doesn't contain any streams and don't contain an indication if
2157// MSID is supported.
2158TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002159 SdpWithoutMsidAndStreamsCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002160 FakeConstraints constraints;
2161 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2162 true);
2163 CreatePeerConnection(&constraints);
2164 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2165
2166 ASSERT_EQ(1u, observer_.remote_streams()->count());
2167 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2168 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2169 EXPECT_EQ(1u, remote_stream->GetVideoTracks().size());
2170}
2171
2172// This tests that a default MediaStream is not created if the remote session
2173// description doesn't contain any streams but does support MSID.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002174TEST_F(PeerConnectionInterfaceTest, SdpWithMsidDontCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002175 FakeConstraints constraints;
2176 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2177 true);
2178 CreatePeerConnection(&constraints);
2179 CreateAndSetRemoteOffer(kSdpStringWithMsidWithoutStreams);
2180 EXPECT_EQ(0u, observer_.remote_streams()->count());
2181}
2182
deadbeefbda7e0b2015-12-08 17:13:40 -08002183// This tests that when setting a new description, the old default tracks are
2184// not destroyed and recreated.
2185// See: https://bugs.chromium.org/p/webrtc/issues/detail?id=5250
Stefan Holmer102362b2016-03-18 09:39:07 +01002186TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002187 DefaultTracksNotDestroyedAndRecreated) {
deadbeefbda7e0b2015-12-08 17:13:40 -08002188 FakeConstraints constraints;
2189 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2190 true);
2191 CreatePeerConnection(&constraints);
2192 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2193
2194 ASSERT_EQ(1u, observer_.remote_streams()->count());
2195 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2196 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2197
2198 // Set the track to "disabled", then set a new description and ensure the
2199 // track is still disabled, which ensures it hasn't been recreated.
2200 remote_stream->GetAudioTracks()[0]->set_enabled(false);
2201 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2202 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2203 EXPECT_FALSE(remote_stream->GetAudioTracks()[0]->enabled());
2204}
2205
deadbeefab9b2d12015-10-14 11:33:11 -07002206// This tests that a default MediaStream is not created if a remote session
2207// description is updated to not have any MediaStreams.
2208TEST_F(PeerConnectionInterfaceTest, VerifyDefaultStreamIsNotCreated) {
2209 FakeConstraints constraints;
2210 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2211 true);
2212 CreatePeerConnection(&constraints);
2213 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2214 rtc::scoped_refptr<StreamCollection> reference(CreateStreamCollection(1));
2215 EXPECT_TRUE(
2216 CompareStreamCollections(observer_.remote_streams(), reference.get()));
2217
2218 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2219 EXPECT_EQ(0u, observer_.remote_streams()->count());
2220}
2221
2222// This tests that an RtpSender is created when the local description is set
2223// after adding a local stream.
2224// TODO(deadbeef): This test and the one below it need to be updated when
2225// an RtpSender's lifetime isn't determined by when a local description is set.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002226TEST_F(PeerConnectionInterfaceTest, LocalDescriptionChanged) {
deadbeefab9b2d12015-10-14 11:33:11 -07002227 FakeConstraints constraints;
2228 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2229 true);
2230 CreatePeerConnection(&constraints);
2231 // Create an offer just to ensure we have an identity before we manually
2232 // call SetLocalDescription.
kwibergd1fe2812016-04-27 06:47:29 -07002233 std::unique_ptr<SessionDescriptionInterface> throwaway;
kwiberg2bbff992016-03-16 11:03:04 -07002234 ASSERT_TRUE(DoCreateOffer(&throwaway, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07002235
kwibergd1fe2812016-04-27 06:47:29 -07002236 std::unique_ptr<SessionDescriptionInterface> desc_1 =
kwiberg2bbff992016-03-16 11:03:04 -07002237 CreateSessionDescriptionAndReference(2, 2);
deadbeefab9b2d12015-10-14 11:33:11 -07002238
2239 pc_->AddStream(reference_collection_->at(0));
2240 EXPECT_TRUE(DoSetLocalDescription(desc_1.release()));
2241 auto senders = pc_->GetSenders();
2242 EXPECT_EQ(4u, senders.size());
2243 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2244 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2245 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[1]));
2246 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[1]));
2247
2248 // Remove an audio and video track.
deadbeeffac06552015-11-25 11:26:01 -08002249 pc_->RemoveStream(reference_collection_->at(0));
kwibergd1fe2812016-04-27 06:47:29 -07002250 std::unique_ptr<SessionDescriptionInterface> desc_2 =
kwiberg2bbff992016-03-16 11:03:04 -07002251 CreateSessionDescriptionAndReference(1, 1);
deadbeeffac06552015-11-25 11:26:01 -08002252 pc_->AddStream(reference_collection_->at(0));
deadbeefab9b2d12015-10-14 11:33:11 -07002253 EXPECT_TRUE(DoSetLocalDescription(desc_2.release()));
2254 senders = pc_->GetSenders();
2255 EXPECT_EQ(2u, senders.size());
2256 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2257 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2258 EXPECT_FALSE(ContainsSender(senders, kAudioTracks[1]));
2259 EXPECT_FALSE(ContainsSender(senders, kVideoTracks[1]));
2260}
2261
2262// This tests that an RtpSender is created when the local description is set
2263// before adding a local stream.
2264TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002265 AddLocalStreamAfterLocalDescriptionChanged) {
deadbeefab9b2d12015-10-14 11:33:11 -07002266 FakeConstraints constraints;
2267 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2268 true);
2269 CreatePeerConnection(&constraints);
2270 // Create an offer just to ensure we have an identity before we manually
2271 // call SetLocalDescription.
kwibergd1fe2812016-04-27 06:47:29 -07002272 std::unique_ptr<SessionDescriptionInterface> throwaway;
kwiberg2bbff992016-03-16 11:03:04 -07002273 ASSERT_TRUE(DoCreateOffer(&throwaway, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07002274
kwibergd1fe2812016-04-27 06:47:29 -07002275 std::unique_ptr<SessionDescriptionInterface> desc_1 =
kwiberg2bbff992016-03-16 11:03:04 -07002276 CreateSessionDescriptionAndReference(2, 2);
deadbeefab9b2d12015-10-14 11:33:11 -07002277
2278 EXPECT_TRUE(DoSetLocalDescription(desc_1.release()));
2279 auto senders = pc_->GetSenders();
2280 EXPECT_EQ(0u, senders.size());
2281
2282 pc_->AddStream(reference_collection_->at(0));
2283 senders = pc_->GetSenders();
2284 EXPECT_EQ(4u, senders.size());
2285 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2286 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2287 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[1]));
2288 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[1]));
2289}
2290
2291// This tests that the expected behavior occurs if the SSRC on a local track is
2292// changed when SetLocalDescription is called.
2293TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002294 ChangeSsrcOnTrackInLocalSessionDescription) {
deadbeefab9b2d12015-10-14 11:33:11 -07002295 FakeConstraints constraints;
2296 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2297 true);
2298 CreatePeerConnection(&constraints);
2299 // Create an offer just to ensure we have an identity before we manually
2300 // call SetLocalDescription.
kwibergd1fe2812016-04-27 06:47:29 -07002301 std::unique_ptr<SessionDescriptionInterface> throwaway;
kwiberg2bbff992016-03-16 11:03:04 -07002302 ASSERT_TRUE(DoCreateOffer(&throwaway, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07002303
kwibergd1fe2812016-04-27 06:47:29 -07002304 std::unique_ptr<SessionDescriptionInterface> desc =
kwiberg2bbff992016-03-16 11:03:04 -07002305 CreateSessionDescriptionAndReference(1, 1);
deadbeefab9b2d12015-10-14 11:33:11 -07002306 std::string sdp;
2307 desc->ToString(&sdp);
2308
2309 pc_->AddStream(reference_collection_->at(0));
2310 EXPECT_TRUE(DoSetLocalDescription(desc.release()));
2311 auto senders = pc_->GetSenders();
2312 EXPECT_EQ(2u, senders.size());
2313 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2314 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2315
2316 // Change the ssrc of the audio and video track.
2317 std::string ssrc_org = "a=ssrc:1";
2318 std::string ssrc_to = "a=ssrc:97";
2319 rtc::replace_substrs(ssrc_org.c_str(), ssrc_org.length(), ssrc_to.c_str(),
2320 ssrc_to.length(), &sdp);
2321 ssrc_org = "a=ssrc:2";
2322 ssrc_to = "a=ssrc:98";
2323 rtc::replace_substrs(ssrc_org.c_str(), ssrc_org.length(), ssrc_to.c_str(),
2324 ssrc_to.length(), &sdp);
kwibergd1fe2812016-04-27 06:47:29 -07002325 std::unique_ptr<SessionDescriptionInterface> updated_desc(
deadbeefab9b2d12015-10-14 11:33:11 -07002326 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer, sdp,
2327 nullptr));
2328
2329 EXPECT_TRUE(DoSetLocalDescription(updated_desc.release()));
2330 senders = pc_->GetSenders();
2331 EXPECT_EQ(2u, senders.size());
2332 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2333 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2334 // TODO(deadbeef): Once RtpSenders expose parameters, check that the SSRC
2335 // changed.
2336}
2337
2338// This tests that the expected behavior occurs if a new session description is
2339// set with the same tracks, but on a different MediaStream.
Stefan Holmer55d6e7c2016-03-17 16:26:40 +01002340TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002341 SignalSameTracksInSeparateMediaStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002342 FakeConstraints constraints;
2343 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2344 true);
2345 CreatePeerConnection(&constraints);
2346 // Create an offer just to ensure we have an identity before we manually
2347 // call SetLocalDescription.
kwibergd1fe2812016-04-27 06:47:29 -07002348 std::unique_ptr<SessionDescriptionInterface> throwaway;
kwiberg2bbff992016-03-16 11:03:04 -07002349 ASSERT_TRUE(DoCreateOffer(&throwaway, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07002350
kwibergd1fe2812016-04-27 06:47:29 -07002351 std::unique_ptr<SessionDescriptionInterface> desc =
kwiberg2bbff992016-03-16 11:03:04 -07002352 CreateSessionDescriptionAndReference(1, 1);
deadbeefab9b2d12015-10-14 11:33:11 -07002353 std::string sdp;
2354 desc->ToString(&sdp);
2355
2356 pc_->AddStream(reference_collection_->at(0));
2357 EXPECT_TRUE(DoSetLocalDescription(desc.release()));
2358 auto senders = pc_->GetSenders();
2359 EXPECT_EQ(2u, senders.size());
2360 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2361 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2362
2363 // Add a new MediaStream but with the same tracks as in the first stream.
2364 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream_1(
2365 webrtc::MediaStream::Create(kStreams[1]));
2366 stream_1->AddTrack(reference_collection_->at(0)->GetVideoTracks()[0]);
2367 stream_1->AddTrack(reference_collection_->at(0)->GetAudioTracks()[0]);
2368 pc_->AddStream(stream_1);
2369
2370 // Replace msid in the original SDP.
2371 rtc::replace_substrs(kStreams[0], strlen(kStreams[0]), kStreams[1],
2372 strlen(kStreams[1]), &sdp);
2373
kwibergd1fe2812016-04-27 06:47:29 -07002374 std::unique_ptr<SessionDescriptionInterface> updated_desc(
deadbeefab9b2d12015-10-14 11:33:11 -07002375 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer, sdp,
2376 nullptr));
2377
2378 EXPECT_TRUE(DoSetLocalDescription(updated_desc.release()));
2379 senders = pc_->GetSenders();
2380 EXPECT_EQ(2u, senders.size());
2381 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2382 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2383}
2384
nisse51542be2016-02-12 02:27:06 -08002385// The PeerConnectionMediaConfig tests below verify that configuration
2386// and constraints are propagated into the MediaConfig passed to
2387// CreateMediaController. These settings are intended for MediaChannel
2388// constructors, but that is not exercised by these unittest.
2389class PeerConnectionFactoryForTest : public webrtc::PeerConnectionFactory {
2390 public:
2391 webrtc::MediaControllerInterface* CreateMediaController(
2392 const cricket::MediaConfig& config) const override {
2393 create_media_controller_called_ = true;
2394 create_media_controller_config_ = config;
2395
2396 webrtc::MediaControllerInterface* mc =
2397 PeerConnectionFactory::CreateMediaController(config);
2398 EXPECT_TRUE(mc != nullptr);
2399 return mc;
2400 }
2401
2402 // Mutable, so they can be modified in the above const-declared method.
2403 mutable bool create_media_controller_called_ = false;
2404 mutable cricket::MediaConfig create_media_controller_config_;
2405};
2406
2407class PeerConnectionMediaConfigTest : public testing::Test {
2408 protected:
2409 void SetUp() override {
nisseaf510af2016-03-21 08:20:42 -07002410 pcf_ = new rtc::RefCountedObject<PeerConnectionFactoryForTest>();
nisse51542be2016-02-12 02:27:06 -08002411 pcf_->Initialize();
2412 }
2413 const cricket::MediaConfig& TestCreatePeerConnection(
2414 const PeerConnectionInterface::RTCConfiguration& config,
2415 const MediaConstraintsInterface *constraints) {
2416 pcf_->create_media_controller_called_ = false;
2417
2418 scoped_refptr<PeerConnectionInterface> pc(
2419 pcf_->CreatePeerConnection(config, constraints, nullptr, nullptr,
2420 &observer_));
2421 EXPECT_TRUE(pc.get());
2422 EXPECT_TRUE(pcf_->create_media_controller_called_);
2423 return pcf_->create_media_controller_config_;
2424 }
2425
2426 scoped_refptr<PeerConnectionFactoryForTest> pcf_;
2427 MockPeerConnectionObserver observer_;
2428};
2429
2430// This test verifies the default behaviour with no constraints and a
2431// default RTCConfiguration.
2432TEST_F(PeerConnectionMediaConfigTest, TestDefaults) {
2433 PeerConnectionInterface::RTCConfiguration config;
2434 FakeConstraints constraints;
2435
2436 const cricket::MediaConfig& media_config =
2437 TestCreatePeerConnection(config, &constraints);
2438
2439 EXPECT_FALSE(media_config.enable_dscp);
nisse0db023a2016-03-01 04:29:59 -08002440 EXPECT_TRUE(media_config.video.enable_cpu_overuse_detection);
2441 EXPECT_FALSE(media_config.video.disable_prerenderer_smoothing);
2442 EXPECT_FALSE(media_config.video.suspend_below_min_bitrate);
nisse51542be2016-02-12 02:27:06 -08002443}
2444
2445// This test verifies the DSCP constraint is recognized and passed to
2446// the CreateMediaController call.
2447TEST_F(PeerConnectionMediaConfigTest, TestDscpConstraintTrue) {
2448 PeerConnectionInterface::RTCConfiguration config;
2449 FakeConstraints constraints;
2450
2451 constraints.AddOptional(webrtc::MediaConstraintsInterface::kEnableDscp, true);
2452 const cricket::MediaConfig& media_config =
2453 TestCreatePeerConnection(config, &constraints);
2454
2455 EXPECT_TRUE(media_config.enable_dscp);
2456}
2457
2458// This test verifies the cpu overuse detection constraint is
2459// recognized and passed to the CreateMediaController call.
2460TEST_F(PeerConnectionMediaConfigTest, TestCpuOveruseConstraintFalse) {
2461 PeerConnectionInterface::RTCConfiguration config;
2462 FakeConstraints constraints;
2463
2464 constraints.AddOptional(
2465 webrtc::MediaConstraintsInterface::kCpuOveruseDetection, false);
2466 const cricket::MediaConfig media_config =
2467 TestCreatePeerConnection(config, &constraints);
2468
nisse0db023a2016-03-01 04:29:59 -08002469 EXPECT_FALSE(media_config.video.enable_cpu_overuse_detection);
nisse51542be2016-02-12 02:27:06 -08002470}
2471
2472// This test verifies that the disable_prerenderer_smoothing flag is
2473// propagated from RTCConfiguration to the CreateMediaController call.
2474TEST_F(PeerConnectionMediaConfigTest, TestDisablePrerendererSmoothingTrue) {
2475 PeerConnectionInterface::RTCConfiguration config;
2476 FakeConstraints constraints;
2477
Niels Möller71bdda02016-03-31 12:59:59 +02002478 config.set_prerenderer_smoothing(false);
nisse51542be2016-02-12 02:27:06 -08002479 const cricket::MediaConfig& media_config =
2480 TestCreatePeerConnection(config, &constraints);
2481
nisse0db023a2016-03-01 04:29:59 -08002482 EXPECT_TRUE(media_config.video.disable_prerenderer_smoothing);
2483}
2484
2485// This test verifies the suspend below min bitrate constraint is
2486// recognized and passed to the CreateMediaController call.
2487TEST_F(PeerConnectionMediaConfigTest,
2488 TestSuspendBelowMinBitrateConstraintTrue) {
2489 PeerConnectionInterface::RTCConfiguration config;
2490 FakeConstraints constraints;
2491
2492 constraints.AddOptional(
2493 webrtc::MediaConstraintsInterface::kEnableVideoSuspendBelowMinBitrate,
2494 true);
2495 const cricket::MediaConfig media_config =
2496 TestCreatePeerConnection(config, &constraints);
2497
2498 EXPECT_TRUE(media_config.video.suspend_below_min_bitrate);
nisse51542be2016-02-12 02:27:06 -08002499}
2500
deadbeefab9b2d12015-10-14 11:33:11 -07002501// The following tests verify that session options are created correctly.
deadbeefc80741f2015-10-22 13:14:45 -07002502// TODO(deadbeef): Convert these tests to be more end-to-end. Instead of
2503// "verify options are converted correctly", should be "pass options into
2504// CreateOffer and verify the correct offer is produced."
deadbeefab9b2d12015-10-14 11:33:11 -07002505
2506TEST(CreateSessionOptionsTest, GetOptionsForOfferWithInvalidAudioOption) {
2507 RTCOfferAnswerOptions rtc_options;
2508 rtc_options.offer_to_receive_audio = RTCOfferAnswerOptions::kUndefined - 1;
2509
2510 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08002511 EXPECT_FALSE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002512
2513 rtc_options.offer_to_receive_audio =
2514 RTCOfferAnswerOptions::kMaxOfferToReceiveMedia + 1;
htaaac2dea2016-03-10 13:35:55 -08002515 EXPECT_FALSE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002516}
2517
2518TEST(CreateSessionOptionsTest, GetOptionsForOfferWithInvalidVideoOption) {
2519 RTCOfferAnswerOptions rtc_options;
2520 rtc_options.offer_to_receive_video = RTCOfferAnswerOptions::kUndefined - 1;
2521
2522 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08002523 EXPECT_FALSE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002524
2525 rtc_options.offer_to_receive_video =
2526 RTCOfferAnswerOptions::kMaxOfferToReceiveMedia + 1;
htaaac2dea2016-03-10 13:35:55 -08002527 EXPECT_FALSE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002528}
2529
2530// Test that a MediaSessionOptions is created for an offer if
deadbeefc80741f2015-10-22 13:14:45 -07002531// OfferToReceiveAudio and OfferToReceiveVideo options are set.
deadbeefab9b2d12015-10-14 11:33:11 -07002532TEST(CreateSessionOptionsTest, GetMediaSessionOptionsForOfferWithAudioVideo) {
2533 RTCOfferAnswerOptions rtc_options;
2534 rtc_options.offer_to_receive_audio = 1;
2535 rtc_options.offer_to_receive_video = 1;
2536
2537 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08002538 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002539 EXPECT_TRUE(options.has_audio());
2540 EXPECT_TRUE(options.has_video());
2541 EXPECT_TRUE(options.bundle_enabled);
2542}
2543
2544// Test that a correct MediaSessionOptions is created for an offer if
deadbeefc80741f2015-10-22 13:14:45 -07002545// OfferToReceiveAudio is set.
deadbeefab9b2d12015-10-14 11:33:11 -07002546TEST(CreateSessionOptionsTest, GetMediaSessionOptionsForOfferWithAudio) {
2547 RTCOfferAnswerOptions rtc_options;
2548 rtc_options.offer_to_receive_audio = 1;
2549
2550 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08002551 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002552 EXPECT_TRUE(options.has_audio());
2553 EXPECT_FALSE(options.has_video());
2554 EXPECT_TRUE(options.bundle_enabled);
2555}
2556
2557// Test that a correct MediaSessionOptions is created for an offer if
deadbeefc80741f2015-10-22 13:14:45 -07002558// the default OfferOptions are used.
deadbeefab9b2d12015-10-14 11:33:11 -07002559TEST(CreateSessionOptionsTest, GetDefaultMediaSessionOptionsForOffer) {
2560 RTCOfferAnswerOptions rtc_options;
2561
2562 cricket::MediaSessionOptions options;
deadbeef0ed85b22016-02-23 17:24:52 -08002563 options.transport_options["audio"] = cricket::TransportOptions();
2564 options.transport_options["video"] = cricket::TransportOptions();
htaaac2dea2016-03-10 13:35:55 -08002565 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefc80741f2015-10-22 13:14:45 -07002566 EXPECT_TRUE(options.has_audio());
deadbeefab9b2d12015-10-14 11:33:11 -07002567 EXPECT_FALSE(options.has_video());
deadbeefc80741f2015-10-22 13:14:45 -07002568 EXPECT_TRUE(options.bundle_enabled);
deadbeefab9b2d12015-10-14 11:33:11 -07002569 EXPECT_TRUE(options.vad_enabled);
deadbeef0ed85b22016-02-23 17:24:52 -08002570 EXPECT_FALSE(options.transport_options["audio"].ice_restart);
2571 EXPECT_FALSE(options.transport_options["video"].ice_restart);
deadbeefab9b2d12015-10-14 11:33:11 -07002572}
2573
2574// Test that a correct MediaSessionOptions is created for an offer if
deadbeefc80741f2015-10-22 13:14:45 -07002575// OfferToReceiveVideo is set.
deadbeefab9b2d12015-10-14 11:33:11 -07002576TEST(CreateSessionOptionsTest, GetMediaSessionOptionsForOfferWithVideo) {
2577 RTCOfferAnswerOptions rtc_options;
2578 rtc_options.offer_to_receive_audio = 0;
2579 rtc_options.offer_to_receive_video = 1;
2580
2581 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08002582 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002583 EXPECT_FALSE(options.has_audio());
2584 EXPECT_TRUE(options.has_video());
2585 EXPECT_TRUE(options.bundle_enabled);
2586}
2587
2588// Test that a correct MediaSessionOptions is created for an offer if
2589// UseRtpMux is set to false.
2590TEST(CreateSessionOptionsTest,
2591 GetMediaSessionOptionsForOfferWithBundleDisabled) {
2592 RTCOfferAnswerOptions rtc_options;
2593 rtc_options.offer_to_receive_audio = 1;
2594 rtc_options.offer_to_receive_video = 1;
2595 rtc_options.use_rtp_mux = false;
2596
2597 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08002598 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002599 EXPECT_TRUE(options.has_audio());
2600 EXPECT_TRUE(options.has_video());
2601 EXPECT_FALSE(options.bundle_enabled);
2602}
2603
2604// Test that a correct MediaSessionOptions is created to restart ice if
2605// IceRestart is set. It also tests that subsequent MediaSessionOptions don't
Taylor Brandstetterf475d362016-01-08 15:35:57 -08002606// have |audio_transport_options.ice_restart| etc. set.
deadbeefab9b2d12015-10-14 11:33:11 -07002607TEST(CreateSessionOptionsTest, GetMediaSessionOptionsForOfferWithIceRestart) {
2608 RTCOfferAnswerOptions rtc_options;
2609 rtc_options.ice_restart = true;
2610
2611 cricket::MediaSessionOptions options;
deadbeef0ed85b22016-02-23 17:24:52 -08002612 options.transport_options["audio"] = cricket::TransportOptions();
2613 options.transport_options["video"] = cricket::TransportOptions();
htaaac2dea2016-03-10 13:35:55 -08002614 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeef0ed85b22016-02-23 17:24:52 -08002615 EXPECT_TRUE(options.transport_options["audio"].ice_restart);
2616 EXPECT_TRUE(options.transport_options["video"].ice_restart);
deadbeefab9b2d12015-10-14 11:33:11 -07002617
2618 rtc_options = RTCOfferAnswerOptions();
htaaac2dea2016-03-10 13:35:55 -08002619 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeef0ed85b22016-02-23 17:24:52 -08002620 EXPECT_FALSE(options.transport_options["audio"].ice_restart);
2621 EXPECT_FALSE(options.transport_options["video"].ice_restart);
deadbeefab9b2d12015-10-14 11:33:11 -07002622}
2623
2624// Test that the MediaConstraints in an answer don't affect if audio and video
2625// is offered in an offer but that if kOfferToReceiveAudio or
2626// kOfferToReceiveVideo constraints are true in an offer, the media type will be
2627// included in subsequent answers.
2628TEST(CreateSessionOptionsTest, MediaConstraintsInAnswer) {
2629 FakeConstraints answer_c;
2630 answer_c.SetMandatoryReceiveAudio(true);
2631 answer_c.SetMandatoryReceiveVideo(true);
2632
2633 cricket::MediaSessionOptions answer_options;
2634 EXPECT_TRUE(ParseConstraintsForAnswer(&answer_c, &answer_options));
2635 EXPECT_TRUE(answer_options.has_audio());
2636 EXPECT_TRUE(answer_options.has_video());
2637
deadbeefc80741f2015-10-22 13:14:45 -07002638 RTCOfferAnswerOptions rtc_offer_options;
deadbeefab9b2d12015-10-14 11:33:11 -07002639
2640 cricket::MediaSessionOptions offer_options;
htaaac2dea2016-03-10 13:35:55 -08002641 EXPECT_TRUE(
2642 ExtractMediaSessionOptions(rtc_offer_options, false, &offer_options));
deadbeefc80741f2015-10-22 13:14:45 -07002643 EXPECT_TRUE(offer_options.has_audio());
htaaac2dea2016-03-10 13:35:55 -08002644 EXPECT_TRUE(offer_options.has_video());
deadbeefab9b2d12015-10-14 11:33:11 -07002645
deadbeefc80741f2015-10-22 13:14:45 -07002646 RTCOfferAnswerOptions updated_rtc_offer_options;
2647 updated_rtc_offer_options.offer_to_receive_audio = 1;
2648 updated_rtc_offer_options.offer_to_receive_video = 1;
deadbeefab9b2d12015-10-14 11:33:11 -07002649
2650 cricket::MediaSessionOptions updated_offer_options;
htaaac2dea2016-03-10 13:35:55 -08002651 EXPECT_TRUE(ExtractMediaSessionOptions(updated_rtc_offer_options, false,
htaa2a49d92016-03-04 02:51:39 -08002652 &updated_offer_options));
deadbeefab9b2d12015-10-14 11:33:11 -07002653 EXPECT_TRUE(updated_offer_options.has_audio());
2654 EXPECT_TRUE(updated_offer_options.has_video());
2655
2656 // Since an offer has been created with both audio and video, subsequent
2657 // offers and answers should contain both audio and video.
2658 // Answers will only contain the media types that exist in the offer
2659 // regardless of the value of |updated_answer_options.has_audio| and
2660 // |updated_answer_options.has_video|.
2661 FakeConstraints updated_answer_c;
2662 answer_c.SetMandatoryReceiveAudio(false);
2663 answer_c.SetMandatoryReceiveVideo(false);
2664
2665 cricket::MediaSessionOptions updated_answer_options;
2666 EXPECT_TRUE(
2667 ParseConstraintsForAnswer(&updated_answer_c, &updated_answer_options));
2668 EXPECT_TRUE(updated_answer_options.has_audio());
2669 EXPECT_TRUE(updated_answer_options.has_video());
deadbeefab9b2d12015-10-14 11:33:11 -07002670}