blob: 466c40236e8706b1416ffcd6269cc7e7c80d5832 [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 }
nisseef8b61e2016-04-29 06:09:15 -0700427 void OnSignalingChange(
428 PeerConnectionInterface::SignalingState new_state) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000429 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
kwiberg65fc62e2016-05-11 04:29:29 -0700937 std::unique_ptr<SessionDescriptionInterface> CreateOfferWithOneAudioStream() {
zhihuang8f65cdf2016-05-06 18:40:30 -0700938 CreatePeerConnection();
939 AddVoiceStream(kStreamLabel1);
kwiberg65fc62e2016-05-11 04:29:29 -0700940 std::unique_ptr<SessionDescriptionInterface> offer;
zhihuang8f65cdf2016-05-06 18:40:30 -0700941 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
942 return offer;
943 }
944
kwiberg65fc62e2016-05-11 04:29:29 -0700945 std::unique_ptr<SessionDescriptionInterface>
zhihuang8f65cdf2016-05-06 18:40:30 -0700946 CreateAnswerWithOneAudioStream() {
kwiberg65fc62e2016-05-11 04:29:29 -0700947 std::unique_ptr<SessionDescriptionInterface> offer =
zhihuang8f65cdf2016-05-06 18:40:30 -0700948 CreateOfferWithOneAudioStream();
949 EXPECT_TRUE(DoSetRemoteDescription(offer.release()));
kwiberg65fc62e2016-05-11 04:29:29 -0700950 std::unique_ptr<SessionDescriptionInterface> answer;
zhihuang8f65cdf2016-05-06 18:40:30 -0700951 EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
952 return answer;
953 }
954
955 const std::string& GetFirstAudioStreamCname(
956 const SessionDescriptionInterface* desc) {
957 const cricket::ContentInfo* audio_content =
958 cricket::GetFirstAudioContent(desc->description());
959 const cricket::AudioContentDescription* audio_desc =
960 static_cast<const cricket::AudioContentDescription*>(
961 audio_content->description);
962 return audio_desc->streams()[0].cname;
963 }
964
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800965 cricket::FakePortAllocator* port_allocator_ = nullptr;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000966 scoped_refptr<webrtc::PeerConnectionFactoryInterface> pc_factory_;
967 scoped_refptr<PeerConnectionInterface> pc_;
968 MockPeerConnectionObserver observer_;
deadbeefab9b2d12015-10-14 11:33:11 -0700969 rtc::scoped_refptr<StreamCollection> reference_collection_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000970};
971
zhihuang8f65cdf2016-05-06 18:40:30 -0700972// Generate different CNAMEs when PeerConnections are created.
973// The CNAMEs are expected to be generated randomly. It is possible
974// that the test fails, though the possibility is very low.
975TEST_F(PeerConnectionInterfaceTest, CnameGenerationInOffer) {
kwiberg65fc62e2016-05-11 04:29:29 -0700976 std::unique_ptr<SessionDescriptionInterface> offer1 =
zhihuang8f65cdf2016-05-06 18:40:30 -0700977 CreateOfferWithOneAudioStream();
kwiberg65fc62e2016-05-11 04:29:29 -0700978 std::unique_ptr<SessionDescriptionInterface> offer2 =
zhihuang8f65cdf2016-05-06 18:40:30 -0700979 CreateOfferWithOneAudioStream();
980 EXPECT_NE(GetFirstAudioStreamCname(offer1.get()),
981 GetFirstAudioStreamCname(offer2.get()));
982}
983
984TEST_F(PeerConnectionInterfaceTest, CnameGenerationInAnswer) {
kwiberg65fc62e2016-05-11 04:29:29 -0700985 std::unique_ptr<SessionDescriptionInterface> answer1 =
zhihuang8f65cdf2016-05-06 18:40:30 -0700986 CreateAnswerWithOneAudioStream();
kwiberg65fc62e2016-05-11 04:29:29 -0700987 std::unique_ptr<SessionDescriptionInterface> answer2 =
zhihuang8f65cdf2016-05-06 18:40:30 -0700988 CreateAnswerWithOneAudioStream();
989 EXPECT_NE(GetFirstAudioStreamCname(answer1.get()),
990 GetFirstAudioStreamCname(answer2.get()));
991}
992
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000993TEST_F(PeerConnectionInterfaceTest,
994 CreatePeerConnectionWithDifferentConfigurations) {
995 CreatePeerConnectionWithDifferentConfigurations();
996}
997
998TEST_F(PeerConnectionInterfaceTest, AddStreams) {
999 CreatePeerConnection();
deadbeefab9b2d12015-10-14 11:33:11 -07001000 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001001 AddVoiceStream(kStreamLabel2);
1002 ASSERT_EQ(2u, pc_->local_streams()->count());
1003
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001004 // Test we can add multiple local streams to one peerconnection.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001005 scoped_refptr<MediaStreamInterface> stream(
1006 pc_factory_->CreateLocalMediaStream(kStreamLabel3));
1007 scoped_refptr<AudioTrackInterface> audio_track(
1008 pc_factory_->CreateAudioTrack(
1009 kStreamLabel3, static_cast<AudioSourceInterface*>(NULL)));
1010 stream->AddTrack(audio_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +00001011 EXPECT_TRUE(pc_->AddStream(stream));
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001012 EXPECT_EQ(3u, pc_->local_streams()->count());
1013
1014 // Remove the third stream.
1015 pc_->RemoveStream(pc_->local_streams()->at(2));
1016 EXPECT_EQ(2u, pc_->local_streams()->count());
1017
1018 // Remove the second stream.
1019 pc_->RemoveStream(pc_->local_streams()->at(1));
1020 EXPECT_EQ(1u, pc_->local_streams()->count());
1021
1022 // Remove the first stream.
1023 pc_->RemoveStream(pc_->local_streams()->at(0));
1024 EXPECT_EQ(0u, pc_->local_streams()->count());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001025}
1026
deadbeefab9b2d12015-10-14 11:33:11 -07001027// Test that the created offer includes streams we added.
1028TEST_F(PeerConnectionInterfaceTest, AddedStreamsPresentInOffer) {
1029 CreatePeerConnection();
1030 AddAudioVideoStream(kStreamLabel1, "audio_track", "video_track");
kwibergd1fe2812016-04-27 06:47:29 -07001031 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001032 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07001033
1034 const cricket::ContentInfo* audio_content =
1035 cricket::GetFirstAudioContent(offer->description());
1036 const cricket::AudioContentDescription* audio_desc =
1037 static_cast<const cricket::AudioContentDescription*>(
1038 audio_content->description);
1039 EXPECT_TRUE(
1040 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
1041
1042 const cricket::ContentInfo* video_content =
1043 cricket::GetFirstVideoContent(offer->description());
1044 const cricket::VideoContentDescription* video_desc =
1045 static_cast<const cricket::VideoContentDescription*>(
1046 video_content->description);
1047 EXPECT_TRUE(
1048 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1049
1050 // Add another stream and ensure the offer includes both the old and new
1051 // streams.
1052 AddAudioVideoStream(kStreamLabel2, "audio_track2", "video_track2");
kwiberg2bbff992016-03-16 11:03:04 -07001053 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07001054
1055 audio_content = cricket::GetFirstAudioContent(offer->description());
1056 audio_desc = static_cast<const cricket::AudioContentDescription*>(
1057 audio_content->description);
1058 EXPECT_TRUE(
1059 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
1060 EXPECT_TRUE(
1061 ContainsTrack(audio_desc->streams(), kStreamLabel2, "audio_track2"));
1062
1063 video_content = cricket::GetFirstVideoContent(offer->description());
1064 video_desc = static_cast<const cricket::VideoContentDescription*>(
1065 video_content->description);
1066 EXPECT_TRUE(
1067 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1068 EXPECT_TRUE(
1069 ContainsTrack(video_desc->streams(), kStreamLabel2, "video_track2"));
1070}
1071
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001072TEST_F(PeerConnectionInterfaceTest, RemoveStream) {
1073 CreatePeerConnection();
deadbeefab9b2d12015-10-14 11:33:11 -07001074 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001075 ASSERT_EQ(1u, pc_->local_streams()->count());
1076 pc_->RemoveStream(pc_->local_streams()->at(0));
1077 EXPECT_EQ(0u, pc_->local_streams()->count());
1078}
1079
deadbeefe1f9d832016-01-14 15:35:42 -08001080// Test for AddTrack and RemoveTrack methods.
1081// Tests that the created offer includes tracks we added,
1082// and that the RtpSenders are created correctly.
1083// Also tests that RemoveTrack removes the tracks from subsequent offers.
1084TEST_F(PeerConnectionInterfaceTest, AddTrackRemoveTrack) {
1085 CreatePeerConnection();
1086 // Create a dummy stream, so tracks share a stream label.
1087 scoped_refptr<MediaStreamInterface> stream(
1088 pc_factory_->CreateLocalMediaStream(kStreamLabel1));
1089 std::vector<MediaStreamInterface*> stream_list;
1090 stream_list.push_back(stream.get());
1091 scoped_refptr<AudioTrackInterface> audio_track(
1092 pc_factory_->CreateAudioTrack("audio_track", nullptr));
nisseaf510af2016-03-21 08:20:42 -07001093 scoped_refptr<VideoTrackInterface> video_track(pc_factory_->CreateVideoTrack(
1094 "video_track",
1095 pc_factory_->CreateVideoSource(new cricket::FakeVideoCapturer())));
deadbeefe1f9d832016-01-14 15:35:42 -08001096 auto audio_sender = pc_->AddTrack(audio_track, stream_list);
1097 auto video_sender = pc_->AddTrack(video_track, stream_list);
1098 EXPECT_EQ(kStreamLabel1, audio_sender->stream_id());
1099 EXPECT_EQ("audio_track", audio_sender->id());
1100 EXPECT_EQ(audio_track, audio_sender->track());
1101 EXPECT_EQ(kStreamLabel1, video_sender->stream_id());
1102 EXPECT_EQ("video_track", video_sender->id());
1103 EXPECT_EQ(video_track, video_sender->track());
1104
1105 // Now create an offer and check for the senders.
kwibergd1fe2812016-04-27 06:47:29 -07001106 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001107 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefe1f9d832016-01-14 15:35:42 -08001108
1109 const cricket::ContentInfo* audio_content =
1110 cricket::GetFirstAudioContent(offer->description());
1111 const cricket::AudioContentDescription* audio_desc =
1112 static_cast<const cricket::AudioContentDescription*>(
1113 audio_content->description);
1114 EXPECT_TRUE(
1115 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
1116
1117 const cricket::ContentInfo* video_content =
1118 cricket::GetFirstVideoContent(offer->description());
1119 const cricket::VideoContentDescription* video_desc =
1120 static_cast<const cricket::VideoContentDescription*>(
1121 video_content->description);
1122 EXPECT_TRUE(
1123 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1124
1125 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
1126
1127 // Now try removing the tracks.
1128 EXPECT_TRUE(pc_->RemoveTrack(audio_sender));
1129 EXPECT_TRUE(pc_->RemoveTrack(video_sender));
1130
1131 // Create a new offer and ensure it doesn't contain the removed senders.
kwiberg2bbff992016-03-16 11:03:04 -07001132 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefe1f9d832016-01-14 15:35:42 -08001133
1134 audio_content = cricket::GetFirstAudioContent(offer->description());
1135 audio_desc = static_cast<const cricket::AudioContentDescription*>(
1136 audio_content->description);
1137 EXPECT_FALSE(
1138 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
1139
1140 video_content = cricket::GetFirstVideoContent(offer->description());
1141 video_desc = static_cast<const cricket::VideoContentDescription*>(
1142 video_content->description);
1143 EXPECT_FALSE(
1144 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1145
1146 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
1147
1148 // Calling RemoveTrack on a sender no longer attached to a PeerConnection
1149 // should return false.
1150 EXPECT_FALSE(pc_->RemoveTrack(audio_sender));
1151 EXPECT_FALSE(pc_->RemoveTrack(video_sender));
1152}
1153
1154// Test creating senders without a stream specified,
1155// expecting a random stream ID to be generated.
1156TEST_F(PeerConnectionInterfaceTest, AddTrackWithoutStream) {
1157 CreatePeerConnection();
1158 // Create a dummy stream, so tracks share a stream label.
1159 scoped_refptr<AudioTrackInterface> audio_track(
1160 pc_factory_->CreateAudioTrack("audio_track", nullptr));
nisseaf510af2016-03-21 08:20:42 -07001161 scoped_refptr<VideoTrackInterface> video_track(pc_factory_->CreateVideoTrack(
1162 "video_track",
1163 pc_factory_->CreateVideoSource(new cricket::FakeVideoCapturer())));
deadbeefe1f9d832016-01-14 15:35:42 -08001164 auto audio_sender =
1165 pc_->AddTrack(audio_track, std::vector<MediaStreamInterface*>());
1166 auto video_sender =
1167 pc_->AddTrack(video_track, std::vector<MediaStreamInterface*>());
1168 EXPECT_EQ("audio_track", audio_sender->id());
1169 EXPECT_EQ(audio_track, audio_sender->track());
1170 EXPECT_EQ("video_track", video_sender->id());
1171 EXPECT_EQ(video_track, video_sender->track());
1172 // If the ID is truly a random GUID, it should be infinitely unlikely they
1173 // will be the same.
1174 EXPECT_NE(video_sender->stream_id(), audio_sender->stream_id());
1175}
1176
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001177TEST_F(PeerConnectionInterfaceTest, CreateOfferReceiveAnswer) {
1178 InitiateCall();
1179 WaitAndVerifyOnAddStream(kStreamLabel1);
1180 VerifyRemoteRtpHeaderExtensions();
1181}
1182
1183TEST_F(PeerConnectionInterfaceTest, CreateOfferReceivePrAnswerAndAnswer) {
1184 CreatePeerConnection();
deadbeefab9b2d12015-10-14 11:33:11 -07001185 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001186 CreateOfferAsLocalDescription();
1187 std::string offer;
1188 EXPECT_TRUE(pc_->local_description()->ToString(&offer));
1189 CreatePrAnswerAndAnswerAsRemoteDescription(offer);
1190 WaitAndVerifyOnAddStream(kStreamLabel1);
1191}
1192
1193TEST_F(PeerConnectionInterfaceTest, ReceiveOfferCreateAnswer) {
1194 CreatePeerConnection();
deadbeefab9b2d12015-10-14 11:33:11 -07001195 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001196
1197 CreateOfferAsRemoteDescription();
1198 CreateAnswerAsLocalDescription();
1199
1200 WaitAndVerifyOnAddStream(kStreamLabel1);
1201}
1202
1203TEST_F(PeerConnectionInterfaceTest, ReceiveOfferCreatePrAnswerAndAnswer) {
1204 CreatePeerConnection();
deadbeefab9b2d12015-10-14 11:33:11 -07001205 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001206
1207 CreateOfferAsRemoteDescription();
1208 CreatePrAnswerAsLocalDescription();
1209 CreateAnswerAsLocalDescription();
1210
1211 WaitAndVerifyOnAddStream(kStreamLabel1);
1212}
1213
1214TEST_F(PeerConnectionInterfaceTest, Renegotiate) {
1215 InitiateCall();
1216 ASSERT_EQ(1u, pc_->remote_streams()->count());
1217 pc_->RemoveStream(pc_->local_streams()->at(0));
1218 CreateOfferReceiveAnswer();
1219 EXPECT_EQ(0u, pc_->remote_streams()->count());
deadbeefab9b2d12015-10-14 11:33:11 -07001220 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001221 CreateOfferReceiveAnswer();
1222}
1223
1224// Tests that after negotiating an audio only call, the respondent can perform a
1225// renegotiation that removes the audio stream.
1226TEST_F(PeerConnectionInterfaceTest, RenegotiateAudioOnly) {
1227 CreatePeerConnection();
1228 AddVoiceStream(kStreamLabel1);
1229 CreateOfferAsRemoteDescription();
1230 CreateAnswerAsLocalDescription();
1231
1232 ASSERT_EQ(1u, pc_->remote_streams()->count());
1233 pc_->RemoveStream(pc_->local_streams()->at(0));
1234 CreateOfferReceiveAnswer();
1235 EXPECT_EQ(0u, pc_->remote_streams()->count());
1236}
1237
1238// Test that candidates are generated and that we can parse our own candidates.
1239TEST_F(PeerConnectionInterfaceTest, IceCandidates) {
1240 CreatePeerConnection();
1241
1242 EXPECT_FALSE(pc_->AddIceCandidate(observer_.last_candidate_.get()));
1243 // SetRemoteDescription takes ownership of offer.
kwibergd1fe2812016-04-27 06:47:29 -07001244 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefab9b2d12015-10-14 11:33:11 -07001245 AddVideoStream(kStreamLabel1);
deadbeefc80741f2015-10-22 13:14:45 -07001246 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
kwiberg2bbff992016-03-16 11:03:04 -07001247 EXPECT_TRUE(DoSetRemoteDescription(offer.release()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001248
1249 // SetLocalDescription takes ownership of answer.
kwibergd1fe2812016-04-27 06:47:29 -07001250 std::unique_ptr<SessionDescriptionInterface> answer;
deadbeefc80741f2015-10-22 13:14:45 -07001251 EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
kwiberg2bbff992016-03-16 11:03:04 -07001252 EXPECT_TRUE(DoSetLocalDescription(answer.release()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001253
1254 EXPECT_TRUE_WAIT(observer_.last_candidate_.get() != NULL, kTimeout);
1255 EXPECT_TRUE_WAIT(observer_.ice_complete_, kTimeout);
1256
1257 EXPECT_TRUE(pc_->AddIceCandidate(observer_.last_candidate_.get()));
1258}
1259
deadbeefab9b2d12015-10-14 11:33:11 -07001260// Test that CreateOffer and CreateAnswer will fail if the track labels are
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001261// not unique.
1262TEST_F(PeerConnectionInterfaceTest, CreateOfferAnswerWithInvalidStream) {
1263 CreatePeerConnection();
1264 // Create a regular offer for the CreateAnswer test later.
kwibergd1fe2812016-04-27 06:47:29 -07001265 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefc80741f2015-10-22 13:14:45 -07001266 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
kwiberg2bbff992016-03-16 11:03:04 -07001267 EXPECT_TRUE(offer);
1268 offer.reset();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001269
1270 // Create a local stream with audio&video tracks having same label.
1271 AddAudioVideoStream(kStreamLabel1, "track_label", "track_label");
1272
1273 // Test CreateOffer
deadbeefc80741f2015-10-22 13:14:45 -07001274 EXPECT_FALSE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001275
1276 // Test CreateAnswer
kwibergd1fe2812016-04-27 06:47:29 -07001277 std::unique_ptr<SessionDescriptionInterface> answer;
deadbeefc80741f2015-10-22 13:14:45 -07001278 EXPECT_FALSE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001279}
1280
1281// Test that we will get different SSRCs for each tracks in the offer and answer
1282// we created.
1283TEST_F(PeerConnectionInterfaceTest, SsrcInOfferAnswer) {
1284 CreatePeerConnection();
1285 // Create a local stream with audio&video tracks having different labels.
1286 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
1287
1288 // Test CreateOffer
kwibergd1fe2812016-04-27 06:47:29 -07001289 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001290 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001291 int audio_ssrc = 0;
1292 int video_ssrc = 0;
1293 EXPECT_TRUE(GetFirstSsrc(GetFirstAudioContent(offer->description()),
1294 &audio_ssrc));
1295 EXPECT_TRUE(GetFirstSsrc(GetFirstVideoContent(offer->description()),
1296 &video_ssrc));
1297 EXPECT_NE(audio_ssrc, video_ssrc);
1298
1299 // Test CreateAnswer
1300 EXPECT_TRUE(DoSetRemoteDescription(offer.release()));
kwibergd1fe2812016-04-27 06:47:29 -07001301 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -07001302 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001303 audio_ssrc = 0;
1304 video_ssrc = 0;
1305 EXPECT_TRUE(GetFirstSsrc(GetFirstAudioContent(answer->description()),
1306 &audio_ssrc));
1307 EXPECT_TRUE(GetFirstSsrc(GetFirstVideoContent(answer->description()),
1308 &video_ssrc));
1309 EXPECT_NE(audio_ssrc, video_ssrc);
1310}
1311
deadbeefeb459812015-12-15 19:24:43 -08001312// Test that it's possible to call AddTrack on a MediaStream after adding
1313// the stream to a PeerConnection.
1314// TODO(deadbeef): Remove this test once this behavior is no longer supported.
1315TEST_F(PeerConnectionInterfaceTest, AddTrackAfterAddStream) {
1316 CreatePeerConnection();
1317 // Create audio stream and add to PeerConnection.
1318 AddVoiceStream(kStreamLabel1);
1319 MediaStreamInterface* stream = pc_->local_streams()->at(0);
1320
1321 // Add video track to the audio-only stream.
nisseaf510af2016-03-21 08:20:42 -07001322 scoped_refptr<VideoTrackInterface> video_track(pc_factory_->CreateVideoTrack(
1323 "video_label",
1324 pc_factory_->CreateVideoSource(new cricket::FakeVideoCapturer())));
deadbeefeb459812015-12-15 19:24:43 -08001325 stream->AddTrack(video_track.get());
1326
kwibergd1fe2812016-04-27 06:47:29 -07001327 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001328 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefeb459812015-12-15 19:24:43 -08001329
1330 const cricket::MediaContentDescription* video_desc =
1331 cricket::GetFirstVideoContentDescription(offer->description());
1332 EXPECT_TRUE(video_desc != nullptr);
1333}
1334
1335// Test that it's possible to call RemoveTrack on a MediaStream after adding
1336// the stream to a PeerConnection.
1337// TODO(deadbeef): Remove this test once this behavior is no longer supported.
1338TEST_F(PeerConnectionInterfaceTest, RemoveTrackAfterAddStream) {
1339 CreatePeerConnection();
1340 // Create audio/video stream and add to PeerConnection.
1341 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
1342 MediaStreamInterface* stream = pc_->local_streams()->at(0);
1343
1344 // Remove the video track.
1345 stream->RemoveTrack(stream->GetVideoTracks()[0]);
1346
kwibergd1fe2812016-04-27 06:47:29 -07001347 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001348 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefeb459812015-12-15 19:24:43 -08001349
1350 const cricket::MediaContentDescription* video_desc =
1351 cricket::GetFirstVideoContentDescription(offer->description());
1352 EXPECT_TRUE(video_desc == nullptr);
1353}
1354
deadbeefbd7d8f72015-12-18 16:58:44 -08001355// Test creating a sender with a stream ID, and ensure the ID is populated
1356// in the offer.
1357TEST_F(PeerConnectionInterfaceTest, CreateSenderWithStream) {
1358 CreatePeerConnection();
1359 pc_->CreateSender("video", kStreamLabel1);
1360
kwibergd1fe2812016-04-27 06:47:29 -07001361 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001362 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefbd7d8f72015-12-18 16:58:44 -08001363
1364 const cricket::MediaContentDescription* video_desc =
1365 cricket::GetFirstVideoContentDescription(offer->description());
1366 ASSERT_TRUE(video_desc != nullptr);
1367 ASSERT_EQ(1u, video_desc->streams().size());
1368 EXPECT_EQ(kStreamLabel1, video_desc->streams()[0].sync_label);
1369}
1370
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001371// Test that we can specify a certain track that we want statistics about.
1372TEST_F(PeerConnectionInterfaceTest, GetStatsForSpecificTrack) {
1373 InitiateCall();
1374 ASSERT_LT(0u, pc_->remote_streams()->count());
1375 ASSERT_LT(0u, pc_->remote_streams()->at(0)->GetAudioTracks().size());
1376 scoped_refptr<MediaStreamTrackInterface> remote_audio =
1377 pc_->remote_streams()->at(0)->GetAudioTracks()[0];
1378 EXPECT_TRUE(DoGetStats(remote_audio));
1379
1380 // Remove the stream. Since we are sending to our selves the local
1381 // and the remote stream is the same.
1382 pc_->RemoveStream(pc_->local_streams()->at(0));
1383 // Do a re-negotiation.
1384 CreateOfferReceiveAnswer();
1385
1386 ASSERT_EQ(0u, pc_->remote_streams()->count());
1387
1388 // Test that we still can get statistics for the old track. Even if it is not
1389 // sent any longer.
1390 EXPECT_TRUE(DoGetStats(remote_audio));
1391}
1392
1393// Test that we can get stats on a video track.
1394TEST_F(PeerConnectionInterfaceTest, GetStatsForVideoTrack) {
1395 InitiateCall();
1396 ASSERT_LT(0u, pc_->remote_streams()->count());
1397 ASSERT_LT(0u, pc_->remote_streams()->at(0)->GetVideoTracks().size());
1398 scoped_refptr<MediaStreamTrackInterface> remote_video =
1399 pc_->remote_streams()->at(0)->GetVideoTracks()[0];
1400 EXPECT_TRUE(DoGetStats(remote_video));
1401}
1402
1403// Test that we don't get statistics for an invalid track.
tommi@webrtc.org908f57e2014-07-21 11:44:39 +00001404// TODO(tommi): Fix this test. DoGetStats will return true
1405// for the unknown track (since GetStats is async), but no
1406// data is returned for the track.
1407TEST_F(PeerConnectionInterfaceTest, DISABLED_GetStatsForInvalidTrack) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001408 InitiateCall();
1409 scoped_refptr<AudioTrackInterface> unknown_audio_track(
1410 pc_factory_->CreateAudioTrack("unknown track", NULL));
1411 EXPECT_FALSE(DoGetStats(unknown_audio_track));
1412}
1413
1414// This test setup two RTP data channels in loop back.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001415TEST_F(PeerConnectionInterfaceTest, TestDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001416 FakeConstraints constraints;
1417 constraints.SetAllowRtpDataChannels();
1418 CreatePeerConnection(&constraints);
1419 scoped_refptr<DataChannelInterface> data1 =
1420 pc_->CreateDataChannel("test1", NULL);
1421 scoped_refptr<DataChannelInterface> data2 =
1422 pc_->CreateDataChannel("test2", NULL);
1423 ASSERT_TRUE(data1 != NULL);
kwibergd1fe2812016-04-27 06:47:29 -07001424 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001425 new MockDataChannelObserver(data1));
kwibergd1fe2812016-04-27 06:47:29 -07001426 std::unique_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001427 new MockDataChannelObserver(data2));
1428
1429 EXPECT_EQ(DataChannelInterface::kConnecting, data1->state());
1430 EXPECT_EQ(DataChannelInterface::kConnecting, data2->state());
1431 std::string data_to_send1 = "testing testing";
1432 std::string data_to_send2 = "testing something else";
1433 EXPECT_FALSE(data1->Send(DataBuffer(data_to_send1)));
1434
1435 CreateOfferReceiveAnswer();
1436 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1437 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
1438
1439 EXPECT_EQ(DataChannelInterface::kOpen, data1->state());
1440 EXPECT_EQ(DataChannelInterface::kOpen, data2->state());
1441 EXPECT_TRUE(data1->Send(DataBuffer(data_to_send1)));
1442 EXPECT_TRUE(data2->Send(DataBuffer(data_to_send2)));
1443
1444 EXPECT_EQ_WAIT(data_to_send1, observer1->last_message(), kTimeout);
1445 EXPECT_EQ_WAIT(data_to_send2, observer2->last_message(), kTimeout);
1446
1447 data1->Close();
1448 EXPECT_EQ(DataChannelInterface::kClosing, data1->state());
1449 CreateOfferReceiveAnswer();
1450 EXPECT_FALSE(observer1->IsOpen());
1451 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
1452 EXPECT_TRUE(observer2->IsOpen());
1453
1454 data_to_send2 = "testing something else again";
1455 EXPECT_TRUE(data2->Send(DataBuffer(data_to_send2)));
1456
1457 EXPECT_EQ_WAIT(data_to_send2, observer2->last_message(), kTimeout);
1458}
1459
1460// This test verifies that sendnig binary data over RTP data channels should
1461// fail.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001462TEST_F(PeerConnectionInterfaceTest, TestSendBinaryOnRtpDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001463 FakeConstraints constraints;
1464 constraints.SetAllowRtpDataChannels();
1465 CreatePeerConnection(&constraints);
1466 scoped_refptr<DataChannelInterface> data1 =
1467 pc_->CreateDataChannel("test1", NULL);
1468 scoped_refptr<DataChannelInterface> data2 =
1469 pc_->CreateDataChannel("test2", NULL);
1470 ASSERT_TRUE(data1 != NULL);
kwibergd1fe2812016-04-27 06:47:29 -07001471 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001472 new MockDataChannelObserver(data1));
kwibergd1fe2812016-04-27 06:47:29 -07001473 std::unique_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001474 new MockDataChannelObserver(data2));
1475
1476 EXPECT_EQ(DataChannelInterface::kConnecting, data1->state());
1477 EXPECT_EQ(DataChannelInterface::kConnecting, data2->state());
1478
1479 CreateOfferReceiveAnswer();
1480 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1481 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
1482
1483 EXPECT_EQ(DataChannelInterface::kOpen, data1->state());
1484 EXPECT_EQ(DataChannelInterface::kOpen, data2->state());
1485
jbaucheec21bd2016-03-20 06:15:43 -07001486 rtc::CopyOnWriteBuffer buffer("test", 4);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001487 EXPECT_FALSE(data1->Send(DataBuffer(buffer, true)));
1488}
1489
1490// This test setup a RTP data channels in loop back and test that a channel is
1491// opened even if the remote end answer with a zero SSRC.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001492TEST_F(PeerConnectionInterfaceTest, TestSendOnlyDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001493 FakeConstraints constraints;
1494 constraints.SetAllowRtpDataChannels();
1495 CreatePeerConnection(&constraints);
1496 scoped_refptr<DataChannelInterface> data1 =
1497 pc_->CreateDataChannel("test1", NULL);
kwibergd1fe2812016-04-27 06:47:29 -07001498 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001499 new MockDataChannelObserver(data1));
1500
1501 CreateOfferReceiveAnswerWithoutSsrc();
1502
1503 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1504
1505 data1->Close();
1506 EXPECT_EQ(DataChannelInterface::kClosing, data1->state());
1507 CreateOfferReceiveAnswerWithoutSsrc();
1508 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
1509 EXPECT_FALSE(observer1->IsOpen());
1510}
1511
1512// This test that if a data channel is added in an answer a receive only channel
1513// channel is created.
1514TEST_F(PeerConnectionInterfaceTest, TestReceiveOnlyDataChannel) {
1515 FakeConstraints constraints;
1516 constraints.SetAllowRtpDataChannels();
1517 CreatePeerConnection(&constraints);
1518
1519 std::string offer_label = "offer_channel";
1520 scoped_refptr<DataChannelInterface> offer_channel =
1521 pc_->CreateDataChannel(offer_label, NULL);
1522
1523 CreateOfferAsLocalDescription();
1524
1525 // Replace the data channel label in the offer and apply it as an answer.
1526 std::string receive_label = "answer_channel";
1527 std::string sdp;
1528 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001529 rtc::replace_substrs(offer_label.c_str(), offer_label.length(),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001530 receive_label.c_str(), receive_label.length(),
1531 &sdp);
1532 CreateAnswerAsRemoteDescription(sdp);
1533
1534 // Verify that a new incoming data channel has been created and that
1535 // it is open but can't we written to.
1536 ASSERT_TRUE(observer_.last_datachannel_ != NULL);
1537 DataChannelInterface* received_channel = observer_.last_datachannel_;
1538 EXPECT_EQ(DataChannelInterface::kConnecting, received_channel->state());
1539 EXPECT_EQ(receive_label, received_channel->label());
1540 EXPECT_FALSE(received_channel->Send(DataBuffer("something")));
1541
1542 // Verify that the channel we initially offered has been rejected.
1543 EXPECT_EQ(DataChannelInterface::kClosed, offer_channel->state());
1544
1545 // Do another offer / answer exchange and verify that the data channel is
1546 // opened.
1547 CreateOfferReceiveAnswer();
1548 EXPECT_EQ_WAIT(DataChannelInterface::kOpen, received_channel->state(),
1549 kTimeout);
1550}
1551
1552// This test that no data channel is returned if a reliable channel is
1553// requested.
1554// TODO(perkj): Remove this test once reliable channels are implemented.
1555TEST_F(PeerConnectionInterfaceTest, CreateReliableRtpDataChannelShouldFail) {
1556 FakeConstraints constraints;
1557 constraints.SetAllowRtpDataChannels();
1558 CreatePeerConnection(&constraints);
1559
1560 std::string label = "test";
1561 webrtc::DataChannelInit config;
1562 config.reliable = true;
1563 scoped_refptr<DataChannelInterface> channel =
1564 pc_->CreateDataChannel(label, &config);
1565 EXPECT_TRUE(channel == NULL);
1566}
1567
deadbeefab9b2d12015-10-14 11:33:11 -07001568// Verifies that duplicated label is not allowed for RTP data channel.
1569TEST_F(PeerConnectionInterfaceTest, RtpDuplicatedLabelNotAllowed) {
1570 FakeConstraints constraints;
1571 constraints.SetAllowRtpDataChannels();
1572 CreatePeerConnection(&constraints);
1573
1574 std::string label = "test";
1575 scoped_refptr<DataChannelInterface> channel =
1576 pc_->CreateDataChannel(label, nullptr);
1577 EXPECT_NE(channel, nullptr);
1578
1579 scoped_refptr<DataChannelInterface> dup_channel =
1580 pc_->CreateDataChannel(label, nullptr);
1581 EXPECT_EQ(dup_channel, nullptr);
1582}
1583
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001584// This tests that a SCTP data channel is returned using different
1585// DataChannelInit configurations.
1586TEST_F(PeerConnectionInterfaceTest, CreateSctpDataChannel) {
1587 FakeConstraints constraints;
1588 constraints.SetAllowDtlsSctpDataChannels();
1589 CreatePeerConnection(&constraints);
1590
1591 webrtc::DataChannelInit config;
1592
1593 scoped_refptr<DataChannelInterface> channel =
1594 pc_->CreateDataChannel("1", &config);
1595 EXPECT_TRUE(channel != NULL);
1596 EXPECT_TRUE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001597 EXPECT_TRUE(observer_.renegotiation_needed_);
1598 observer_.renegotiation_needed_ = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001599
1600 config.ordered = false;
1601 channel = pc_->CreateDataChannel("2", &config);
1602 EXPECT_TRUE(channel != NULL);
1603 EXPECT_TRUE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001604 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001605
1606 config.ordered = true;
1607 config.maxRetransmits = 0;
1608 channel = pc_->CreateDataChannel("3", &config);
1609 EXPECT_TRUE(channel != NULL);
1610 EXPECT_FALSE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001611 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001612
1613 config.maxRetransmits = -1;
1614 config.maxRetransmitTime = 0;
1615 channel = pc_->CreateDataChannel("4", &config);
1616 EXPECT_TRUE(channel != NULL);
1617 EXPECT_FALSE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001618 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001619}
1620
1621// This tests that no data channel is returned if both maxRetransmits and
1622// maxRetransmitTime are set for SCTP data channels.
1623TEST_F(PeerConnectionInterfaceTest,
1624 CreateSctpDataChannelShouldFailForInvalidConfig) {
1625 FakeConstraints constraints;
1626 constraints.SetAllowDtlsSctpDataChannels();
1627 CreatePeerConnection(&constraints);
1628
1629 std::string label = "test";
1630 webrtc::DataChannelInit config;
1631 config.maxRetransmits = 0;
1632 config.maxRetransmitTime = 0;
1633
1634 scoped_refptr<DataChannelInterface> channel =
1635 pc_->CreateDataChannel(label, &config);
1636 EXPECT_TRUE(channel == NULL);
1637}
1638
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001639// The test verifies that creating a SCTP data channel with an id already in use
1640// or out of range should fail.
1641TEST_F(PeerConnectionInterfaceTest,
1642 CreateSctpDataChannelWithInvalidIdShouldFail) {
1643 FakeConstraints constraints;
1644 constraints.SetAllowDtlsSctpDataChannels();
1645 CreatePeerConnection(&constraints);
1646
1647 webrtc::DataChannelInit config;
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001648 scoped_refptr<DataChannelInterface> channel;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001649
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001650 config.id = 1;
1651 channel = pc_->CreateDataChannel("1", &config);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001652 EXPECT_TRUE(channel != NULL);
1653 EXPECT_EQ(1, channel->id());
1654
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001655 channel = pc_->CreateDataChannel("x", &config);
1656 EXPECT_TRUE(channel == NULL);
1657
1658 config.id = cricket::kMaxSctpSid;
1659 channel = pc_->CreateDataChannel("max", &config);
1660 EXPECT_TRUE(channel != NULL);
1661 EXPECT_EQ(config.id, channel->id());
1662
1663 config.id = cricket::kMaxSctpSid + 1;
1664 channel = pc_->CreateDataChannel("x", &config);
1665 EXPECT_TRUE(channel == NULL);
1666}
1667
deadbeefab9b2d12015-10-14 11:33:11 -07001668// Verifies that duplicated label is allowed for SCTP data channel.
1669TEST_F(PeerConnectionInterfaceTest, SctpDuplicatedLabelAllowed) {
1670 FakeConstraints constraints;
1671 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
1672 true);
1673 CreatePeerConnection(&constraints);
1674
1675 std::string label = "test";
1676 scoped_refptr<DataChannelInterface> channel =
1677 pc_->CreateDataChannel(label, nullptr);
1678 EXPECT_NE(channel, nullptr);
1679
1680 scoped_refptr<DataChannelInterface> dup_channel =
1681 pc_->CreateDataChannel(label, nullptr);
1682 EXPECT_NE(dup_channel, nullptr);
1683}
1684
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001685// This test verifies that OnRenegotiationNeeded is fired for every new RTP
1686// DataChannel.
1687TEST_F(PeerConnectionInterfaceTest, RenegotiationNeededForNewRtpDataChannel) {
1688 FakeConstraints constraints;
1689 constraints.SetAllowRtpDataChannels();
1690 CreatePeerConnection(&constraints);
1691
1692 scoped_refptr<DataChannelInterface> dc1 =
1693 pc_->CreateDataChannel("test1", NULL);
1694 EXPECT_TRUE(observer_.renegotiation_needed_);
1695 observer_.renegotiation_needed_ = false;
1696
1697 scoped_refptr<DataChannelInterface> dc2 =
1698 pc_->CreateDataChannel("test2", NULL);
1699 EXPECT_TRUE(observer_.renegotiation_needed_);
1700}
1701
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001702// This test that a data channel closes when a PeerConnection is deleted/closed.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001703TEST_F(PeerConnectionInterfaceTest, DataChannelCloseWhenPeerConnectionClose) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001704 FakeConstraints constraints;
1705 constraints.SetAllowRtpDataChannels();
1706 CreatePeerConnection(&constraints);
1707
1708 scoped_refptr<DataChannelInterface> data1 =
1709 pc_->CreateDataChannel("test1", NULL);
1710 scoped_refptr<DataChannelInterface> data2 =
1711 pc_->CreateDataChannel("test2", NULL);
1712 ASSERT_TRUE(data1 != NULL);
kwibergd1fe2812016-04-27 06:47:29 -07001713 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001714 new MockDataChannelObserver(data1));
kwibergd1fe2812016-04-27 06:47:29 -07001715 std::unique_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001716 new MockDataChannelObserver(data2));
1717
1718 CreateOfferReceiveAnswer();
1719 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1720 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
1721
1722 ReleasePeerConnection();
1723 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
1724 EXPECT_EQ(DataChannelInterface::kClosed, data2->state());
1725}
1726
1727// This test that data channels can be rejected in an answer.
1728TEST_F(PeerConnectionInterfaceTest, TestRejectDataChannelInAnswer) {
1729 FakeConstraints constraints;
1730 constraints.SetAllowRtpDataChannels();
1731 CreatePeerConnection(&constraints);
1732
1733 scoped_refptr<DataChannelInterface> offer_channel(
1734 pc_->CreateDataChannel("offer_channel", NULL));
1735
1736 CreateOfferAsLocalDescription();
1737
1738 // Create an answer where the m-line for data channels are rejected.
1739 std::string sdp;
1740 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
1741 webrtc::JsepSessionDescription* answer = new webrtc::JsepSessionDescription(
1742 SessionDescriptionInterface::kAnswer);
1743 EXPECT_TRUE(answer->Initialize(sdp, NULL));
1744 cricket::ContentInfo* data_info =
1745 answer->description()->GetContentByName("data");
1746 data_info->rejected = true;
1747
1748 DoSetRemoteDescription(answer);
1749 EXPECT_EQ(DataChannelInterface::kClosed, offer_channel->state());
1750}
1751
1752// Test that we can create a session description from an SDP string from
1753// FireFox, use it as a remote session description, generate an answer and use
1754// the answer as a local description.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07001755TEST_F(PeerConnectionInterfaceTest, ReceiveFireFoxOffer) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001756 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001757 FakeConstraints constraints;
1758 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
1759 true);
1760 CreatePeerConnection(&constraints);
1761 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
1762 SessionDescriptionInterface* desc =
1763 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
jbauchfabe2c92015-07-16 13:43:14 -07001764 webrtc::kFireFoxSdpOffer, nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001765 EXPECT_TRUE(DoSetSessionDescription(desc, false));
1766 CreateAnswerAsLocalDescription();
1767 ASSERT_TRUE(pc_->local_description() != NULL);
1768 ASSERT_TRUE(pc_->remote_description() != NULL);
1769
1770 const cricket::ContentInfo* content =
1771 cricket::GetFirstAudioContent(pc_->local_description()->description());
1772 ASSERT_TRUE(content != NULL);
1773 EXPECT_FALSE(content->rejected);
1774
1775 content =
1776 cricket::GetFirstVideoContent(pc_->local_description()->description());
1777 ASSERT_TRUE(content != NULL);
1778 EXPECT_FALSE(content->rejected);
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00001779#ifdef HAVE_SCTP
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001780 content =
1781 cricket::GetFirstDataContent(pc_->local_description()->description());
1782 ASSERT_TRUE(content != NULL);
1783 EXPECT_TRUE(content->rejected);
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00001784#endif
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001785}
1786
1787// Test that we can create an audio only offer and receive an answer with a
1788// limited set of audio codecs and receive an updated offer with more audio
1789// codecs, where the added codecs are not supported.
1790TEST_F(PeerConnectionInterfaceTest, ReceiveUpdatedAudioOfferWithBadCodecs) {
1791 CreatePeerConnection();
1792 AddVoiceStream("audio_label");
1793 CreateOfferAsLocalDescription();
1794
1795 SessionDescriptionInterface* answer =
1796 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
jbauchfabe2c92015-07-16 13:43:14 -07001797 webrtc::kAudioSdp, nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001798 EXPECT_TRUE(DoSetSessionDescription(answer, false));
1799
1800 SessionDescriptionInterface* updated_offer =
1801 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
jbauchfabe2c92015-07-16 13:43:14 -07001802 webrtc::kAudioSdpWithUnsupportedCodecs,
1803 nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001804 EXPECT_TRUE(DoSetSessionDescription(updated_offer, false));
1805 CreateAnswerAsLocalDescription();
1806}
1807
deadbeefc80741f2015-10-22 13:14:45 -07001808// Test that if we're receiving (but not sending) a track, subsequent offers
1809// will have m-lines with a=recvonly.
1810TEST_F(PeerConnectionInterfaceTest, CreateSubsequentRecvOnlyOffer) {
1811 FakeConstraints constraints;
1812 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
1813 true);
1814 CreatePeerConnection(&constraints);
1815 CreateAndSetRemoteOffer(kSdpStringWithStream1);
1816 CreateAnswerAsLocalDescription();
1817
1818 // At this point we should be receiving stream 1, but not sending anything.
1819 // A new offer should be recvonly.
kwibergd1fe2812016-04-27 06:47:29 -07001820 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefc80741f2015-10-22 13:14:45 -07001821 DoCreateOffer(&offer, nullptr);
1822
1823 const cricket::ContentInfo* video_content =
1824 cricket::GetFirstVideoContent(offer->description());
1825 const cricket::VideoContentDescription* video_desc =
1826 static_cast<const cricket::VideoContentDescription*>(
1827 video_content->description);
1828 ASSERT_EQ(cricket::MD_RECVONLY, video_desc->direction());
1829
1830 const cricket::ContentInfo* audio_content =
1831 cricket::GetFirstAudioContent(offer->description());
1832 const cricket::AudioContentDescription* audio_desc =
1833 static_cast<const cricket::AudioContentDescription*>(
1834 audio_content->description);
1835 ASSERT_EQ(cricket::MD_RECVONLY, audio_desc->direction());
1836}
1837
1838// Test that if we're receiving (but not sending) a track, and the
1839// offerToReceiveVideo/offerToReceiveAudio constraints are explicitly set to
1840// false, the generated m-lines will be a=inactive.
1841TEST_F(PeerConnectionInterfaceTest, CreateSubsequentInactiveOffer) {
1842 FakeConstraints constraints;
1843 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
1844 true);
1845 CreatePeerConnection(&constraints);
1846 CreateAndSetRemoteOffer(kSdpStringWithStream1);
1847 CreateAnswerAsLocalDescription();
1848
1849 // At this point we should be receiving stream 1, but not sending anything.
1850 // A new offer would be recvonly, but we'll set the "no receive" constraints
1851 // to make it inactive.
kwibergd1fe2812016-04-27 06:47:29 -07001852 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefc80741f2015-10-22 13:14:45 -07001853 FakeConstraints offer_constraints;
1854 offer_constraints.AddMandatory(
1855 webrtc::MediaConstraintsInterface::kOfferToReceiveVideo, false);
1856 offer_constraints.AddMandatory(
1857 webrtc::MediaConstraintsInterface::kOfferToReceiveAudio, false);
1858 DoCreateOffer(&offer, &offer_constraints);
1859
1860 const cricket::ContentInfo* video_content =
1861 cricket::GetFirstVideoContent(offer->description());
1862 const cricket::VideoContentDescription* video_desc =
1863 static_cast<const cricket::VideoContentDescription*>(
1864 video_content->description);
1865 ASSERT_EQ(cricket::MD_INACTIVE, video_desc->direction());
1866
1867 const cricket::ContentInfo* audio_content =
1868 cricket::GetFirstAudioContent(offer->description());
1869 const cricket::AudioContentDescription* audio_desc =
1870 static_cast<const cricket::AudioContentDescription*>(
1871 audio_content->description);
1872 ASSERT_EQ(cricket::MD_INACTIVE, audio_desc->direction());
1873}
1874
deadbeef653b8e02015-11-11 12:55:10 -08001875// Test that we can use SetConfiguration to change the ICE servers of the
1876// PortAllocator.
1877TEST_F(PeerConnectionInterfaceTest, SetConfigurationChangesIceServers) {
1878 CreatePeerConnection();
1879
1880 PeerConnectionInterface::RTCConfiguration config;
1881 PeerConnectionInterface::IceServer server;
1882 server.uri = "stun:test_hostname";
1883 config.servers.push_back(server);
1884 EXPECT_TRUE(pc_->SetConfiguration(config));
1885
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08001886 EXPECT_EQ(1u, port_allocator_->stun_servers().size());
1887 EXPECT_EQ("test_hostname",
1888 port_allocator_->stun_servers().begin()->hostname());
deadbeef653b8e02015-11-11 12:55:10 -08001889}
1890
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001891// Test that PeerConnection::Close changes the states to closed and all remote
1892// tracks change state to ended.
1893TEST_F(PeerConnectionInterfaceTest, CloseAndTestStreamsAndStates) {
1894 // Initialize a PeerConnection and negotiate local and remote session
1895 // description.
1896 InitiateCall();
1897 ASSERT_EQ(1u, pc_->local_streams()->count());
1898 ASSERT_EQ(1u, pc_->remote_streams()->count());
1899
1900 pc_->Close();
1901
1902 EXPECT_EQ(PeerConnectionInterface::kClosed, pc_->signaling_state());
1903 EXPECT_EQ(PeerConnectionInterface::kIceConnectionClosed,
1904 pc_->ice_connection_state());
1905 EXPECT_EQ(PeerConnectionInterface::kIceGatheringComplete,
1906 pc_->ice_gathering_state());
1907
1908 EXPECT_EQ(1u, pc_->local_streams()->count());
1909 EXPECT_EQ(1u, pc_->remote_streams()->count());
1910
1911 scoped_refptr<MediaStreamInterface> remote_stream =
1912 pc_->remote_streams()->at(0);
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07001913 // Track state may be updated asynchronously.
perkjd61bf802016-03-24 03:16:19 -07001914 EXPECT_EQ_WAIT(MediaStreamTrackInterface::kEnded,
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07001915 remote_stream->GetAudioTracks()[0]->state(), kTimeout);
1916 EXPECT_EQ_WAIT(MediaStreamTrackInterface::kEnded,
1917 remote_stream->GetVideoTracks()[0]->state(), kTimeout);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001918}
1919
1920// Test that PeerConnection methods fails gracefully after
1921// PeerConnection::Close has been called.
1922TEST_F(PeerConnectionInterfaceTest, CloseAndTestMethods) {
1923 CreatePeerConnection();
1924 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
1925 CreateOfferAsRemoteDescription();
1926 CreateAnswerAsLocalDescription();
1927
1928 ASSERT_EQ(1u, pc_->local_streams()->count());
1929 scoped_refptr<MediaStreamInterface> local_stream =
1930 pc_->local_streams()->at(0);
1931
1932 pc_->Close();
1933
1934 pc_->RemoveStream(local_stream);
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +00001935 EXPECT_FALSE(pc_->AddStream(local_stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001936
1937 ASSERT_FALSE(local_stream->GetAudioTracks().empty());
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001938 rtc::scoped_refptr<webrtc::DtmfSenderInterface> dtmf_sender(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001939 pc_->CreateDtmfSender(local_stream->GetAudioTracks()[0]));
wu@webrtc.org66037362013-08-13 00:09:35 +00001940 EXPECT_TRUE(NULL == dtmf_sender); // local stream has been removed.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001941
1942 EXPECT_TRUE(pc_->CreateDataChannel("test", NULL) == NULL);
1943
1944 EXPECT_TRUE(pc_->local_description() != NULL);
1945 EXPECT_TRUE(pc_->remote_description() != NULL);
1946
kwibergd1fe2812016-04-27 06:47:29 -07001947 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001948 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
kwibergd1fe2812016-04-27 06:47:29 -07001949 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -07001950 EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001951
1952 std::string sdp;
1953 ASSERT_TRUE(pc_->remote_description()->ToString(&sdp));
1954 SessionDescriptionInterface* remote_offer =
1955 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
1956 sdp, NULL);
1957 EXPECT_FALSE(DoSetRemoteDescription(remote_offer));
1958
1959 ASSERT_TRUE(pc_->local_description()->ToString(&sdp));
1960 SessionDescriptionInterface* local_offer =
1961 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
1962 sdp, NULL);
1963 EXPECT_FALSE(DoSetLocalDescription(local_offer));
1964}
1965
1966// Test that GetStats can still be called after PeerConnection::Close.
1967TEST_F(PeerConnectionInterfaceTest, CloseAndGetStats) {
1968 InitiateCall();
1969 pc_->Close();
1970 DoGetStats(NULL);
1971}
deadbeefab9b2d12015-10-14 11:33:11 -07001972
1973// NOTE: The series of tests below come from what used to be
1974// mediastreamsignaling_unittest.cc, and are mostly aimed at testing that
1975// setting a remote or local description has the expected effects.
1976
1977// This test verifies that the remote MediaStreams corresponding to a received
1978// SDP string is created. In this test the two separate MediaStreams are
1979// signaled.
1980TEST_F(PeerConnectionInterfaceTest, UpdateRemoteStreams) {
1981 FakeConstraints constraints;
1982 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
1983 true);
1984 CreatePeerConnection(&constraints);
1985 CreateAndSetRemoteOffer(kSdpStringWithStream1);
1986
1987 rtc::scoped_refptr<StreamCollection> reference(CreateStreamCollection(1));
1988 EXPECT_TRUE(
1989 CompareStreamCollections(observer_.remote_streams(), reference.get()));
1990 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
1991 EXPECT_TRUE(remote_stream->GetVideoTracks()[0]->GetSource() != nullptr);
1992
1993 // Create a session description based on another SDP with another
1994 // MediaStream.
1995 CreateAndSetRemoteOffer(kSdpStringWithStream1And2);
1996
1997 rtc::scoped_refptr<StreamCollection> reference2(CreateStreamCollection(2));
1998 EXPECT_TRUE(
1999 CompareStreamCollections(observer_.remote_streams(), reference2.get()));
2000}
2001
2002// This test verifies that when remote tracks are added/removed from SDP, the
2003// created remote streams are updated appropriately.
2004TEST_F(PeerConnectionInterfaceTest,
2005 AddRemoveTrackFromExistingRemoteMediaStream) {
2006 FakeConstraints constraints;
2007 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2008 true);
2009 CreatePeerConnection(&constraints);
kwibergd1fe2812016-04-27 06:47:29 -07002010 std::unique_ptr<SessionDescriptionInterface> desc_ms1 =
kwiberg2bbff992016-03-16 11:03:04 -07002011 CreateSessionDescriptionAndReference(1, 1);
deadbeefab9b2d12015-10-14 11:33:11 -07002012 EXPECT_TRUE(DoSetRemoteDescription(desc_ms1.release()));
2013 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
2014 reference_collection_));
2015
2016 // Add extra audio and video tracks to the same MediaStream.
kwibergd1fe2812016-04-27 06:47:29 -07002017 std::unique_ptr<SessionDescriptionInterface> desc_ms1_two_tracks =
kwiberg2bbff992016-03-16 11:03:04 -07002018 CreateSessionDescriptionAndReference(2, 2);
deadbeefab9b2d12015-10-14 11:33:11 -07002019 EXPECT_TRUE(DoSetRemoteDescription(desc_ms1_two_tracks.release()));
2020 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
2021 reference_collection_));
perkjd61bf802016-03-24 03:16:19 -07002022 scoped_refptr<AudioTrackInterface> audio_track2 =
2023 observer_.remote_streams()->at(0)->GetAudioTracks()[1];
2024 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, audio_track2->state());
2025 scoped_refptr<VideoTrackInterface> video_track2 =
2026 observer_.remote_streams()->at(0)->GetVideoTracks()[1];
2027 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, video_track2->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002028
2029 // Remove the extra audio and video tracks.
kwibergd1fe2812016-04-27 06:47:29 -07002030 std::unique_ptr<SessionDescriptionInterface> desc_ms2 =
kwiberg2bbff992016-03-16 11:03:04 -07002031 CreateSessionDescriptionAndReference(1, 1);
perkjd61bf802016-03-24 03:16:19 -07002032 MockTrackObserver audio_track_observer(audio_track2);
2033 MockTrackObserver video_track_observer(video_track2);
2034
2035 EXPECT_CALL(audio_track_observer, OnChanged()).Times(Exactly(1));
2036 EXPECT_CALL(video_track_observer, OnChanged()).Times(Exactly(1));
deadbeefab9b2d12015-10-14 11:33:11 -07002037 EXPECT_TRUE(DoSetRemoteDescription(desc_ms2.release()));
2038 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
2039 reference_collection_));
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 audio_track2->state(), kTimeout);
2043 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
2044 video_track2->state(), kTimeout);
deadbeefab9b2d12015-10-14 11:33:11 -07002045}
2046
2047// This tests that remote tracks are ended if a local session description is set
2048// that rejects the media content type.
2049TEST_F(PeerConnectionInterfaceTest, RejectMediaContent) {
2050 FakeConstraints constraints;
2051 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2052 true);
2053 CreatePeerConnection(&constraints);
2054 // First create and set a remote offer, then reject its video content in our
2055 // answer.
2056 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2057 ASSERT_EQ(1u, observer_.remote_streams()->count());
2058 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2059 ASSERT_EQ(1u, remote_stream->GetVideoTracks().size());
2060 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2061
2062 rtc::scoped_refptr<webrtc::VideoTrackInterface> remote_video =
2063 remote_stream->GetVideoTracks()[0];
2064 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, remote_video->state());
2065 rtc::scoped_refptr<webrtc::AudioTrackInterface> remote_audio =
2066 remote_stream->GetAudioTracks()[0];
2067 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, remote_audio->state());
2068
kwibergd1fe2812016-04-27 06:47:29 -07002069 std::unique_ptr<SessionDescriptionInterface> local_answer;
kwiberg2bbff992016-03-16 11:03:04 -07002070 EXPECT_TRUE(DoCreateAnswer(&local_answer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07002071 cricket::ContentInfo* video_info =
2072 local_answer->description()->GetContentByName("video");
2073 video_info->rejected = true;
2074 EXPECT_TRUE(DoSetLocalDescription(local_answer.release()));
2075 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kEnded, remote_video->state());
2076 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, remote_audio->state());
2077
2078 // Now create an offer where we reject both video and audio.
kwibergd1fe2812016-04-27 06:47:29 -07002079 std::unique_ptr<SessionDescriptionInterface> local_offer;
kwiberg2bbff992016-03-16 11:03:04 -07002080 EXPECT_TRUE(DoCreateOffer(&local_offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07002081 video_info = local_offer->description()->GetContentByName("video");
2082 ASSERT_TRUE(video_info != nullptr);
2083 video_info->rejected = true;
2084 cricket::ContentInfo* audio_info =
2085 local_offer->description()->GetContentByName("audio");
2086 ASSERT_TRUE(audio_info != nullptr);
2087 audio_info->rejected = true;
2088 EXPECT_TRUE(DoSetLocalDescription(local_offer.release()));
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002089 // Track state may be updated asynchronously.
perkjd61bf802016-03-24 03:16:19 -07002090 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002091 remote_audio->state(), kTimeout);
2092 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
2093 remote_video->state(), kTimeout);
deadbeefab9b2d12015-10-14 11:33:11 -07002094}
2095
2096// This tests that we won't crash if the remote track has been removed outside
2097// of PeerConnection and then PeerConnection tries to reject the track.
2098TEST_F(PeerConnectionInterfaceTest, RemoveTrackThenRejectMediaContent) {
2099 FakeConstraints constraints;
2100 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2101 true);
2102 CreatePeerConnection(&constraints);
2103 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2104 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2105 remote_stream->RemoveTrack(remote_stream->GetVideoTracks()[0]);
2106 remote_stream->RemoveTrack(remote_stream->GetAudioTracks()[0]);
2107
kwibergd1fe2812016-04-27 06:47:29 -07002108 std::unique_ptr<SessionDescriptionInterface> local_answer(
deadbeefab9b2d12015-10-14 11:33:11 -07002109 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
2110 kSdpStringWithStream1, nullptr));
2111 cricket::ContentInfo* video_info =
2112 local_answer->description()->GetContentByName("video");
2113 video_info->rejected = true;
2114 cricket::ContentInfo* audio_info =
2115 local_answer->description()->GetContentByName("audio");
2116 audio_info->rejected = true;
2117 EXPECT_TRUE(DoSetLocalDescription(local_answer.release()));
2118
2119 // No crash is a pass.
2120}
2121
deadbeef5e97fb52015-10-15 12:49:08 -07002122// This tests that if a recvonly remote description is set, no remote streams
2123// will be created, even if the description contains SSRCs/MSIDs.
2124// See: https://code.google.com/p/webrtc/issues/detail?id=5054
2125TEST_F(PeerConnectionInterfaceTest, RecvonlyDescriptionDoesntCreateStream) {
2126 FakeConstraints constraints;
2127 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2128 true);
2129 CreatePeerConnection(&constraints);
2130
2131 std::string recvonly_offer = kSdpStringWithStream1;
2132 rtc::replace_substrs(kSendrecv, strlen(kSendrecv), kRecvonly,
2133 strlen(kRecvonly), &recvonly_offer);
2134 CreateAndSetRemoteOffer(recvonly_offer);
2135
2136 EXPECT_EQ(0u, observer_.remote_streams()->count());
2137}
2138
deadbeefab9b2d12015-10-14 11:33:11 -07002139// This tests that a default MediaStream is created if a remote session
2140// description doesn't contain any streams and no MSID support.
2141// It also tests that the default stream is updated if a video m-line is added
2142// in a subsequent session description.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002143TEST_F(PeerConnectionInterfaceTest, SdpWithoutMsidCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002144 FakeConstraints constraints;
2145 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2146 true);
2147 CreatePeerConnection(&constraints);
2148 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2149
2150 ASSERT_EQ(1u, observer_.remote_streams()->count());
2151 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2152
2153 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2154 EXPECT_EQ(0u, remote_stream->GetVideoTracks().size());
2155 EXPECT_EQ("default", remote_stream->label());
2156
2157 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2158 ASSERT_EQ(1u, observer_.remote_streams()->count());
2159 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2160 EXPECT_EQ("defaulta0", remote_stream->GetAudioTracks()[0]->id());
deadbeef884f5852016-01-15 09:20:04 -08002161 EXPECT_EQ(MediaStreamTrackInterface::kLive,
2162 remote_stream->GetAudioTracks()[0]->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002163 ASSERT_EQ(1u, remote_stream->GetVideoTracks().size());
2164 EXPECT_EQ("defaultv0", remote_stream->GetVideoTracks()[0]->id());
deadbeef884f5852016-01-15 09:20:04 -08002165 EXPECT_EQ(MediaStreamTrackInterface::kLive,
2166 remote_stream->GetVideoTracks()[0]->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002167}
2168
2169// This tests that a default MediaStream is created if a remote session
2170// description doesn't contain any streams and media direction is send only.
2171TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002172 SendOnlySdpWithoutMsidCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002173 FakeConstraints constraints;
2174 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2175 true);
2176 CreatePeerConnection(&constraints);
2177 CreateAndSetRemoteOffer(kSdpStringSendOnlyWithoutStreams);
2178
2179 ASSERT_EQ(1u, observer_.remote_streams()->count());
2180 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2181
2182 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2183 EXPECT_EQ(1u, remote_stream->GetVideoTracks().size());
2184 EXPECT_EQ("default", remote_stream->label());
2185}
2186
2187// This tests that it won't crash when PeerConnection tries to remove
2188// a remote track that as already been removed from the MediaStream.
2189TEST_F(PeerConnectionInterfaceTest, RemoveAlreadyGoneRemoteStream) {
2190 FakeConstraints constraints;
2191 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2192 true);
2193 CreatePeerConnection(&constraints);
2194 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2195 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2196 remote_stream->RemoveTrack(remote_stream->GetAudioTracks()[0]);
2197 remote_stream->RemoveTrack(remote_stream->GetVideoTracks()[0]);
2198
2199 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2200
2201 // No crash is a pass.
2202}
2203
2204// This tests that a default MediaStream is created if the remote session
2205// description doesn't contain any streams and don't contain an indication if
2206// MSID is supported.
2207TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002208 SdpWithoutMsidAndStreamsCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002209 FakeConstraints constraints;
2210 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2211 true);
2212 CreatePeerConnection(&constraints);
2213 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2214
2215 ASSERT_EQ(1u, observer_.remote_streams()->count());
2216 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2217 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2218 EXPECT_EQ(1u, remote_stream->GetVideoTracks().size());
2219}
2220
2221// This tests that a default MediaStream is not created if the remote session
2222// description doesn't contain any streams but does support MSID.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002223TEST_F(PeerConnectionInterfaceTest, SdpWithMsidDontCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002224 FakeConstraints constraints;
2225 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2226 true);
2227 CreatePeerConnection(&constraints);
2228 CreateAndSetRemoteOffer(kSdpStringWithMsidWithoutStreams);
2229 EXPECT_EQ(0u, observer_.remote_streams()->count());
2230}
2231
deadbeefbda7e0b2015-12-08 17:13:40 -08002232// This tests that when setting a new description, the old default tracks are
2233// not destroyed and recreated.
2234// See: https://bugs.chromium.org/p/webrtc/issues/detail?id=5250
Stefan Holmer102362b2016-03-18 09:39:07 +01002235TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002236 DefaultTracksNotDestroyedAndRecreated) {
deadbeefbda7e0b2015-12-08 17:13:40 -08002237 FakeConstraints constraints;
2238 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2239 true);
2240 CreatePeerConnection(&constraints);
2241 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2242
2243 ASSERT_EQ(1u, observer_.remote_streams()->count());
2244 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2245 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2246
2247 // Set the track to "disabled", then set a new description and ensure the
2248 // track is still disabled, which ensures it hasn't been recreated.
2249 remote_stream->GetAudioTracks()[0]->set_enabled(false);
2250 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2251 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2252 EXPECT_FALSE(remote_stream->GetAudioTracks()[0]->enabled());
2253}
2254
deadbeefab9b2d12015-10-14 11:33:11 -07002255// This tests that a default MediaStream is not created if a remote session
2256// description is updated to not have any MediaStreams.
2257TEST_F(PeerConnectionInterfaceTest, VerifyDefaultStreamIsNotCreated) {
2258 FakeConstraints constraints;
2259 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2260 true);
2261 CreatePeerConnection(&constraints);
2262 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2263 rtc::scoped_refptr<StreamCollection> reference(CreateStreamCollection(1));
2264 EXPECT_TRUE(
2265 CompareStreamCollections(observer_.remote_streams(), reference.get()));
2266
2267 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2268 EXPECT_EQ(0u, observer_.remote_streams()->count());
2269}
2270
2271// This tests that an RtpSender is created when the local description is set
2272// after adding a local stream.
2273// TODO(deadbeef): This test and the one below it need to be updated when
2274// an RtpSender's lifetime isn't determined by when a local description is set.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002275TEST_F(PeerConnectionInterfaceTest, LocalDescriptionChanged) {
deadbeefab9b2d12015-10-14 11:33:11 -07002276 FakeConstraints constraints;
2277 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2278 true);
2279 CreatePeerConnection(&constraints);
2280 // Create an offer just to ensure we have an identity before we manually
2281 // call SetLocalDescription.
kwibergd1fe2812016-04-27 06:47:29 -07002282 std::unique_ptr<SessionDescriptionInterface> throwaway;
kwiberg2bbff992016-03-16 11:03:04 -07002283 ASSERT_TRUE(DoCreateOffer(&throwaway, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07002284
kwibergd1fe2812016-04-27 06:47:29 -07002285 std::unique_ptr<SessionDescriptionInterface> desc_1 =
kwiberg2bbff992016-03-16 11:03:04 -07002286 CreateSessionDescriptionAndReference(2, 2);
deadbeefab9b2d12015-10-14 11:33:11 -07002287
2288 pc_->AddStream(reference_collection_->at(0));
2289 EXPECT_TRUE(DoSetLocalDescription(desc_1.release()));
2290 auto senders = pc_->GetSenders();
2291 EXPECT_EQ(4u, senders.size());
2292 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2293 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2294 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[1]));
2295 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[1]));
2296
2297 // Remove an audio and video track.
deadbeeffac06552015-11-25 11:26:01 -08002298 pc_->RemoveStream(reference_collection_->at(0));
kwibergd1fe2812016-04-27 06:47:29 -07002299 std::unique_ptr<SessionDescriptionInterface> desc_2 =
kwiberg2bbff992016-03-16 11:03:04 -07002300 CreateSessionDescriptionAndReference(1, 1);
deadbeeffac06552015-11-25 11:26:01 -08002301 pc_->AddStream(reference_collection_->at(0));
deadbeefab9b2d12015-10-14 11:33:11 -07002302 EXPECT_TRUE(DoSetLocalDescription(desc_2.release()));
2303 senders = pc_->GetSenders();
2304 EXPECT_EQ(2u, senders.size());
2305 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2306 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2307 EXPECT_FALSE(ContainsSender(senders, kAudioTracks[1]));
2308 EXPECT_FALSE(ContainsSender(senders, kVideoTracks[1]));
2309}
2310
2311// This tests that an RtpSender is created when the local description is set
2312// before adding a local stream.
2313TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002314 AddLocalStreamAfterLocalDescriptionChanged) {
deadbeefab9b2d12015-10-14 11:33:11 -07002315 FakeConstraints constraints;
2316 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2317 true);
2318 CreatePeerConnection(&constraints);
2319 // Create an offer just to ensure we have an identity before we manually
2320 // call SetLocalDescription.
kwibergd1fe2812016-04-27 06:47:29 -07002321 std::unique_ptr<SessionDescriptionInterface> throwaway;
kwiberg2bbff992016-03-16 11:03:04 -07002322 ASSERT_TRUE(DoCreateOffer(&throwaway, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07002323
kwibergd1fe2812016-04-27 06:47:29 -07002324 std::unique_ptr<SessionDescriptionInterface> desc_1 =
kwiberg2bbff992016-03-16 11:03:04 -07002325 CreateSessionDescriptionAndReference(2, 2);
deadbeefab9b2d12015-10-14 11:33:11 -07002326
2327 EXPECT_TRUE(DoSetLocalDescription(desc_1.release()));
2328 auto senders = pc_->GetSenders();
2329 EXPECT_EQ(0u, senders.size());
2330
2331 pc_->AddStream(reference_collection_->at(0));
2332 senders = pc_->GetSenders();
2333 EXPECT_EQ(4u, senders.size());
2334 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2335 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2336 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[1]));
2337 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[1]));
2338}
2339
2340// This tests that the expected behavior occurs if the SSRC on a local track is
2341// changed when SetLocalDescription is called.
2342TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002343 ChangeSsrcOnTrackInLocalSessionDescription) {
deadbeefab9b2d12015-10-14 11:33:11 -07002344 FakeConstraints constraints;
2345 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2346 true);
2347 CreatePeerConnection(&constraints);
2348 // Create an offer just to ensure we have an identity before we manually
2349 // call SetLocalDescription.
kwibergd1fe2812016-04-27 06:47:29 -07002350 std::unique_ptr<SessionDescriptionInterface> throwaway;
kwiberg2bbff992016-03-16 11:03:04 -07002351 ASSERT_TRUE(DoCreateOffer(&throwaway, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07002352
kwibergd1fe2812016-04-27 06:47:29 -07002353 std::unique_ptr<SessionDescriptionInterface> desc =
kwiberg2bbff992016-03-16 11:03:04 -07002354 CreateSessionDescriptionAndReference(1, 1);
deadbeefab9b2d12015-10-14 11:33:11 -07002355 std::string sdp;
2356 desc->ToString(&sdp);
2357
2358 pc_->AddStream(reference_collection_->at(0));
2359 EXPECT_TRUE(DoSetLocalDescription(desc.release()));
2360 auto senders = pc_->GetSenders();
2361 EXPECT_EQ(2u, senders.size());
2362 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2363 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2364
2365 // Change the ssrc of the audio and video track.
2366 std::string ssrc_org = "a=ssrc:1";
2367 std::string ssrc_to = "a=ssrc:97";
2368 rtc::replace_substrs(ssrc_org.c_str(), ssrc_org.length(), ssrc_to.c_str(),
2369 ssrc_to.length(), &sdp);
2370 ssrc_org = "a=ssrc:2";
2371 ssrc_to = "a=ssrc:98";
2372 rtc::replace_substrs(ssrc_org.c_str(), ssrc_org.length(), ssrc_to.c_str(),
2373 ssrc_to.length(), &sdp);
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 // TODO(deadbeef): Once RtpSenders expose parameters, check that the SSRC
2384 // changed.
2385}
2386
2387// This tests that the expected behavior occurs if a new session description is
2388// set with the same tracks, but on a different MediaStream.
Stefan Holmer55d6e7c2016-03-17 16:26:40 +01002389TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002390 SignalSameTracksInSeparateMediaStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002391 FakeConstraints constraints;
2392 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2393 true);
2394 CreatePeerConnection(&constraints);
2395 // Create an offer just to ensure we have an identity before we manually
2396 // call SetLocalDescription.
kwibergd1fe2812016-04-27 06:47:29 -07002397 std::unique_ptr<SessionDescriptionInterface> throwaway;
kwiberg2bbff992016-03-16 11:03:04 -07002398 ASSERT_TRUE(DoCreateOffer(&throwaway, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07002399
kwibergd1fe2812016-04-27 06:47:29 -07002400 std::unique_ptr<SessionDescriptionInterface> desc =
kwiberg2bbff992016-03-16 11:03:04 -07002401 CreateSessionDescriptionAndReference(1, 1);
deadbeefab9b2d12015-10-14 11:33:11 -07002402 std::string sdp;
2403 desc->ToString(&sdp);
2404
2405 pc_->AddStream(reference_collection_->at(0));
2406 EXPECT_TRUE(DoSetLocalDescription(desc.release()));
2407 auto senders = pc_->GetSenders();
2408 EXPECT_EQ(2u, senders.size());
2409 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2410 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2411
2412 // Add a new MediaStream but with the same tracks as in the first stream.
2413 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream_1(
2414 webrtc::MediaStream::Create(kStreams[1]));
2415 stream_1->AddTrack(reference_collection_->at(0)->GetVideoTracks()[0]);
2416 stream_1->AddTrack(reference_collection_->at(0)->GetAudioTracks()[0]);
2417 pc_->AddStream(stream_1);
2418
2419 // Replace msid in the original SDP.
2420 rtc::replace_substrs(kStreams[0], strlen(kStreams[0]), kStreams[1],
2421 strlen(kStreams[1]), &sdp);
2422
kwibergd1fe2812016-04-27 06:47:29 -07002423 std::unique_ptr<SessionDescriptionInterface> updated_desc(
deadbeefab9b2d12015-10-14 11:33:11 -07002424 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer, sdp,
2425 nullptr));
2426
2427 EXPECT_TRUE(DoSetLocalDescription(updated_desc.release()));
2428 senders = pc_->GetSenders();
2429 EXPECT_EQ(2u, senders.size());
2430 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2431 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2432}
2433
nisse51542be2016-02-12 02:27:06 -08002434// The PeerConnectionMediaConfig tests below verify that configuration
2435// and constraints are propagated into the MediaConfig passed to
2436// CreateMediaController. These settings are intended for MediaChannel
2437// constructors, but that is not exercised by these unittest.
2438class PeerConnectionFactoryForTest : public webrtc::PeerConnectionFactory {
2439 public:
2440 webrtc::MediaControllerInterface* CreateMediaController(
2441 const cricket::MediaConfig& config) const override {
2442 create_media_controller_called_ = true;
2443 create_media_controller_config_ = config;
2444
2445 webrtc::MediaControllerInterface* mc =
2446 PeerConnectionFactory::CreateMediaController(config);
2447 EXPECT_TRUE(mc != nullptr);
2448 return mc;
2449 }
2450
2451 // Mutable, so they can be modified in the above const-declared method.
2452 mutable bool create_media_controller_called_ = false;
2453 mutable cricket::MediaConfig create_media_controller_config_;
2454};
2455
2456class PeerConnectionMediaConfigTest : public testing::Test {
2457 protected:
2458 void SetUp() override {
nisseaf510af2016-03-21 08:20:42 -07002459 pcf_ = new rtc::RefCountedObject<PeerConnectionFactoryForTest>();
nisse51542be2016-02-12 02:27:06 -08002460 pcf_->Initialize();
2461 }
2462 const cricket::MediaConfig& TestCreatePeerConnection(
2463 const PeerConnectionInterface::RTCConfiguration& config,
2464 const MediaConstraintsInterface *constraints) {
2465 pcf_->create_media_controller_called_ = false;
2466
2467 scoped_refptr<PeerConnectionInterface> pc(
2468 pcf_->CreatePeerConnection(config, constraints, nullptr, nullptr,
2469 &observer_));
2470 EXPECT_TRUE(pc.get());
2471 EXPECT_TRUE(pcf_->create_media_controller_called_);
2472 return pcf_->create_media_controller_config_;
2473 }
2474
2475 scoped_refptr<PeerConnectionFactoryForTest> pcf_;
2476 MockPeerConnectionObserver observer_;
2477};
2478
2479// This test verifies the default behaviour with no constraints and a
2480// default RTCConfiguration.
2481TEST_F(PeerConnectionMediaConfigTest, TestDefaults) {
2482 PeerConnectionInterface::RTCConfiguration config;
2483 FakeConstraints constraints;
2484
2485 const cricket::MediaConfig& media_config =
2486 TestCreatePeerConnection(config, &constraints);
2487
2488 EXPECT_FALSE(media_config.enable_dscp);
nisse0db023a2016-03-01 04:29:59 -08002489 EXPECT_TRUE(media_config.video.enable_cpu_overuse_detection);
2490 EXPECT_FALSE(media_config.video.disable_prerenderer_smoothing);
2491 EXPECT_FALSE(media_config.video.suspend_below_min_bitrate);
nisse51542be2016-02-12 02:27:06 -08002492}
2493
2494// This test verifies the DSCP constraint is recognized and passed to
2495// the CreateMediaController call.
2496TEST_F(PeerConnectionMediaConfigTest, TestDscpConstraintTrue) {
2497 PeerConnectionInterface::RTCConfiguration config;
2498 FakeConstraints constraints;
2499
2500 constraints.AddOptional(webrtc::MediaConstraintsInterface::kEnableDscp, true);
2501 const cricket::MediaConfig& media_config =
2502 TestCreatePeerConnection(config, &constraints);
2503
2504 EXPECT_TRUE(media_config.enable_dscp);
2505}
2506
2507// This test verifies the cpu overuse detection constraint is
2508// recognized and passed to the CreateMediaController call.
2509TEST_F(PeerConnectionMediaConfigTest, TestCpuOveruseConstraintFalse) {
2510 PeerConnectionInterface::RTCConfiguration config;
2511 FakeConstraints constraints;
2512
2513 constraints.AddOptional(
2514 webrtc::MediaConstraintsInterface::kCpuOveruseDetection, false);
2515 const cricket::MediaConfig media_config =
2516 TestCreatePeerConnection(config, &constraints);
2517
nisse0db023a2016-03-01 04:29:59 -08002518 EXPECT_FALSE(media_config.video.enable_cpu_overuse_detection);
nisse51542be2016-02-12 02:27:06 -08002519}
2520
2521// This test verifies that the disable_prerenderer_smoothing flag is
2522// propagated from RTCConfiguration to the CreateMediaController call.
2523TEST_F(PeerConnectionMediaConfigTest, TestDisablePrerendererSmoothingTrue) {
2524 PeerConnectionInterface::RTCConfiguration config;
2525 FakeConstraints constraints;
2526
Niels Möller71bdda02016-03-31 12:59:59 +02002527 config.set_prerenderer_smoothing(false);
nisse51542be2016-02-12 02:27:06 -08002528 const cricket::MediaConfig& media_config =
2529 TestCreatePeerConnection(config, &constraints);
2530
nisse0db023a2016-03-01 04:29:59 -08002531 EXPECT_TRUE(media_config.video.disable_prerenderer_smoothing);
2532}
2533
2534// This test verifies the suspend below min bitrate constraint is
2535// recognized and passed to the CreateMediaController call.
2536TEST_F(PeerConnectionMediaConfigTest,
2537 TestSuspendBelowMinBitrateConstraintTrue) {
2538 PeerConnectionInterface::RTCConfiguration config;
2539 FakeConstraints constraints;
2540
2541 constraints.AddOptional(
2542 webrtc::MediaConstraintsInterface::kEnableVideoSuspendBelowMinBitrate,
2543 true);
2544 const cricket::MediaConfig media_config =
2545 TestCreatePeerConnection(config, &constraints);
2546
2547 EXPECT_TRUE(media_config.video.suspend_below_min_bitrate);
nisse51542be2016-02-12 02:27:06 -08002548}
2549
deadbeefab9b2d12015-10-14 11:33:11 -07002550// The following tests verify that session options are created correctly.
deadbeefc80741f2015-10-22 13:14:45 -07002551// TODO(deadbeef): Convert these tests to be more end-to-end. Instead of
2552// "verify options are converted correctly", should be "pass options into
2553// CreateOffer and verify the correct offer is produced."
deadbeefab9b2d12015-10-14 11:33:11 -07002554
2555TEST(CreateSessionOptionsTest, GetOptionsForOfferWithInvalidAudioOption) {
2556 RTCOfferAnswerOptions rtc_options;
2557 rtc_options.offer_to_receive_audio = RTCOfferAnswerOptions::kUndefined - 1;
2558
2559 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08002560 EXPECT_FALSE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002561
2562 rtc_options.offer_to_receive_audio =
2563 RTCOfferAnswerOptions::kMaxOfferToReceiveMedia + 1;
htaaac2dea2016-03-10 13:35:55 -08002564 EXPECT_FALSE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002565}
2566
2567TEST(CreateSessionOptionsTest, GetOptionsForOfferWithInvalidVideoOption) {
2568 RTCOfferAnswerOptions rtc_options;
2569 rtc_options.offer_to_receive_video = RTCOfferAnswerOptions::kUndefined - 1;
2570
2571 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08002572 EXPECT_FALSE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002573
2574 rtc_options.offer_to_receive_video =
2575 RTCOfferAnswerOptions::kMaxOfferToReceiveMedia + 1;
htaaac2dea2016-03-10 13:35:55 -08002576 EXPECT_FALSE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002577}
2578
2579// Test that a MediaSessionOptions is created for an offer if
deadbeefc80741f2015-10-22 13:14:45 -07002580// OfferToReceiveAudio and OfferToReceiveVideo options are set.
deadbeefab9b2d12015-10-14 11:33:11 -07002581TEST(CreateSessionOptionsTest, GetMediaSessionOptionsForOfferWithAudioVideo) {
2582 RTCOfferAnswerOptions rtc_options;
2583 rtc_options.offer_to_receive_audio = 1;
2584 rtc_options.offer_to_receive_video = 1;
2585
2586 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08002587 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002588 EXPECT_TRUE(options.has_audio());
2589 EXPECT_TRUE(options.has_video());
2590 EXPECT_TRUE(options.bundle_enabled);
2591}
2592
2593// Test that a correct MediaSessionOptions is created for an offer if
deadbeefc80741f2015-10-22 13:14:45 -07002594// OfferToReceiveAudio is set.
deadbeefab9b2d12015-10-14 11:33:11 -07002595TEST(CreateSessionOptionsTest, GetMediaSessionOptionsForOfferWithAudio) {
2596 RTCOfferAnswerOptions rtc_options;
2597 rtc_options.offer_to_receive_audio = 1;
2598
2599 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08002600 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002601 EXPECT_TRUE(options.has_audio());
2602 EXPECT_FALSE(options.has_video());
2603 EXPECT_TRUE(options.bundle_enabled);
2604}
2605
2606// Test that a correct MediaSessionOptions is created for an offer if
deadbeefc80741f2015-10-22 13:14:45 -07002607// the default OfferOptions are used.
deadbeefab9b2d12015-10-14 11:33:11 -07002608TEST(CreateSessionOptionsTest, GetDefaultMediaSessionOptionsForOffer) {
2609 RTCOfferAnswerOptions rtc_options;
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));
deadbeefc80741f2015-10-22 13:14:45 -07002615 EXPECT_TRUE(options.has_audio());
deadbeefab9b2d12015-10-14 11:33:11 -07002616 EXPECT_FALSE(options.has_video());
deadbeefc80741f2015-10-22 13:14:45 -07002617 EXPECT_TRUE(options.bundle_enabled);
deadbeefab9b2d12015-10-14 11:33:11 -07002618 EXPECT_TRUE(options.vad_enabled);
deadbeef0ed85b22016-02-23 17:24:52 -08002619 EXPECT_FALSE(options.transport_options["audio"].ice_restart);
2620 EXPECT_FALSE(options.transport_options["video"].ice_restart);
deadbeefab9b2d12015-10-14 11:33:11 -07002621}
2622
2623// Test that a correct MediaSessionOptions is created for an offer if
deadbeefc80741f2015-10-22 13:14:45 -07002624// OfferToReceiveVideo is set.
deadbeefab9b2d12015-10-14 11:33:11 -07002625TEST(CreateSessionOptionsTest, GetMediaSessionOptionsForOfferWithVideo) {
2626 RTCOfferAnswerOptions rtc_options;
2627 rtc_options.offer_to_receive_audio = 0;
2628 rtc_options.offer_to_receive_video = 1;
2629
2630 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08002631 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002632 EXPECT_FALSE(options.has_audio());
2633 EXPECT_TRUE(options.has_video());
2634 EXPECT_TRUE(options.bundle_enabled);
2635}
2636
2637// Test that a correct MediaSessionOptions is created for an offer if
2638// UseRtpMux is set to false.
2639TEST(CreateSessionOptionsTest,
2640 GetMediaSessionOptionsForOfferWithBundleDisabled) {
2641 RTCOfferAnswerOptions rtc_options;
2642 rtc_options.offer_to_receive_audio = 1;
2643 rtc_options.offer_to_receive_video = 1;
2644 rtc_options.use_rtp_mux = false;
2645
2646 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08002647 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002648 EXPECT_TRUE(options.has_audio());
2649 EXPECT_TRUE(options.has_video());
2650 EXPECT_FALSE(options.bundle_enabled);
2651}
2652
2653// Test that a correct MediaSessionOptions is created to restart ice if
2654// IceRestart is set. It also tests that subsequent MediaSessionOptions don't
Taylor Brandstetterf475d362016-01-08 15:35:57 -08002655// have |audio_transport_options.ice_restart| etc. set.
deadbeefab9b2d12015-10-14 11:33:11 -07002656TEST(CreateSessionOptionsTest, GetMediaSessionOptionsForOfferWithIceRestart) {
2657 RTCOfferAnswerOptions rtc_options;
2658 rtc_options.ice_restart = true;
2659
2660 cricket::MediaSessionOptions options;
deadbeef0ed85b22016-02-23 17:24:52 -08002661 options.transport_options["audio"] = cricket::TransportOptions();
2662 options.transport_options["video"] = cricket::TransportOptions();
htaaac2dea2016-03-10 13:35:55 -08002663 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeef0ed85b22016-02-23 17:24:52 -08002664 EXPECT_TRUE(options.transport_options["audio"].ice_restart);
2665 EXPECT_TRUE(options.transport_options["video"].ice_restart);
deadbeefab9b2d12015-10-14 11:33:11 -07002666
2667 rtc_options = RTCOfferAnswerOptions();
htaaac2dea2016-03-10 13:35:55 -08002668 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeef0ed85b22016-02-23 17:24:52 -08002669 EXPECT_FALSE(options.transport_options["audio"].ice_restart);
2670 EXPECT_FALSE(options.transport_options["video"].ice_restart);
deadbeefab9b2d12015-10-14 11:33:11 -07002671}
2672
2673// Test that the MediaConstraints in an answer don't affect if audio and video
2674// is offered in an offer but that if kOfferToReceiveAudio or
2675// kOfferToReceiveVideo constraints are true in an offer, the media type will be
2676// included in subsequent answers.
2677TEST(CreateSessionOptionsTest, MediaConstraintsInAnswer) {
2678 FakeConstraints answer_c;
2679 answer_c.SetMandatoryReceiveAudio(true);
2680 answer_c.SetMandatoryReceiveVideo(true);
2681
2682 cricket::MediaSessionOptions answer_options;
2683 EXPECT_TRUE(ParseConstraintsForAnswer(&answer_c, &answer_options));
2684 EXPECT_TRUE(answer_options.has_audio());
2685 EXPECT_TRUE(answer_options.has_video());
2686
deadbeefc80741f2015-10-22 13:14:45 -07002687 RTCOfferAnswerOptions rtc_offer_options;
deadbeefab9b2d12015-10-14 11:33:11 -07002688
2689 cricket::MediaSessionOptions offer_options;
htaaac2dea2016-03-10 13:35:55 -08002690 EXPECT_TRUE(
2691 ExtractMediaSessionOptions(rtc_offer_options, false, &offer_options));
deadbeefc80741f2015-10-22 13:14:45 -07002692 EXPECT_TRUE(offer_options.has_audio());
htaaac2dea2016-03-10 13:35:55 -08002693 EXPECT_TRUE(offer_options.has_video());
deadbeefab9b2d12015-10-14 11:33:11 -07002694
deadbeefc80741f2015-10-22 13:14:45 -07002695 RTCOfferAnswerOptions updated_rtc_offer_options;
2696 updated_rtc_offer_options.offer_to_receive_audio = 1;
2697 updated_rtc_offer_options.offer_to_receive_video = 1;
deadbeefab9b2d12015-10-14 11:33:11 -07002698
2699 cricket::MediaSessionOptions updated_offer_options;
htaaac2dea2016-03-10 13:35:55 -08002700 EXPECT_TRUE(ExtractMediaSessionOptions(updated_rtc_offer_options, false,
htaa2a49d92016-03-04 02:51:39 -08002701 &updated_offer_options));
deadbeefab9b2d12015-10-14 11:33:11 -07002702 EXPECT_TRUE(updated_offer_options.has_audio());
2703 EXPECT_TRUE(updated_offer_options.has_video());
2704
2705 // Since an offer has been created with both audio and video, subsequent
2706 // offers and answers should contain both audio and video.
2707 // Answers will only contain the media types that exist in the offer
2708 // regardless of the value of |updated_answer_options.has_audio| and
2709 // |updated_answer_options.has_video|.
2710 FakeConstraints updated_answer_c;
2711 answer_c.SetMandatoryReceiveAudio(false);
2712 answer_c.SetMandatoryReceiveVideo(false);
2713
2714 cricket::MediaSessionOptions updated_answer_options;
2715 EXPECT_TRUE(
2716 ParseConstraintsForAnswer(&updated_answer_c, &updated_answer_options));
2717 EXPECT_TRUE(updated_answer_options.has_audio());
2718 EXPECT_TRUE(updated_answer_options.has_video());
deadbeefab9b2d12015-10-14 11:33:11 -07002719}