blob: 76f9c5defa4fd0dbd0c59db8a0026498fd16a95e [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
11#include <string>
kwiberg0eb15ed2015-12-17 03:04:15 -080012#include <utility>
henrike@webrtc.org28e20752013-07-10 00:45:36 +000013
perkjd61bf802016-03-24 03:16:19 -070014#include "testing/gmock/include/gmock/gmock.h"
Henrik Kjellander15583c12016-02-10 10:53:12 +010015#include "webrtc/api/audiotrack.h"
16#include "webrtc/api/jsepsessiondescription.h"
17#include "webrtc/api/mediastream.h"
18#include "webrtc/api/mediastreaminterface.h"
19#include "webrtc/api/peerconnection.h"
20#include "webrtc/api/peerconnectioninterface.h"
21#include "webrtc/api/rtpreceiverinterface.h"
22#include "webrtc/api/rtpsenderinterface.h"
23#include "webrtc/api/streamcollection.h"
24#ifdef WEBRTC_ANDROID
25#include "webrtc/api/test/androidtestinitializer.h"
26#endif
27#include "webrtc/api/test/fakeconstraints.h"
28#include "webrtc/api/test/fakedtlsidentitystore.h"
nisseaf510af2016-03-21 08:20:42 -070029#include "webrtc/api/test/fakevideotracksource.h"
Henrik Kjellander15583c12016-02-10 10:53:12 +010030#include "webrtc/api/test/mockpeerconnectionobservers.h"
31#include "webrtc/api/test/testsdpstrings.h"
perkja3ede6c2016-03-08 01:27:48 +010032#include "webrtc/api/videocapturertracksource.h"
Henrik Kjellander15583c12016-02-10 10:53:12 +010033#include "webrtc/api/videotrack.h"
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000034#include "webrtc/base/gunit.h"
35#include "webrtc/base/scoped_ptr.h"
36#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_ptr;
243using rtc::scoped_refptr;
perkjd61bf802016-03-24 03:16:19 -0700244using ::testing::Exactly;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000245using webrtc::AudioSourceInterface;
deadbeefab9b2d12015-10-14 11:33:11 -0700246using webrtc::AudioTrack;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000247using webrtc::AudioTrackInterface;
248using webrtc::DataBuffer;
249using webrtc::DataChannelInterface;
250using webrtc::FakeConstraints;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000251using webrtc::IceCandidateInterface;
deadbeefc80741f2015-10-22 13:14:45 -0700252using webrtc::MediaConstraintsInterface;
deadbeefab9b2d12015-10-14 11:33:11 -0700253using webrtc::MediaStream;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000254using webrtc::MediaStreamInterface;
255using webrtc::MediaStreamTrackInterface;
256using webrtc::MockCreateSessionDescriptionObserver;
257using webrtc::MockDataChannelObserver;
258using webrtc::MockSetSessionDescriptionObserver;
259using webrtc::MockStatsObserver;
perkjd61bf802016-03-24 03:16:19 -0700260using webrtc::NotifierInterface;
261using webrtc::ObserverInterface;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000262using webrtc::PeerConnectionInterface;
263using webrtc::PeerConnectionObserver;
deadbeefab9b2d12015-10-14 11:33:11 -0700264using webrtc::RtpReceiverInterface;
265using webrtc::RtpSenderInterface;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000266using webrtc::SdpParseError;
267using webrtc::SessionDescriptionInterface;
deadbeefab9b2d12015-10-14 11:33:11 -0700268using webrtc::StreamCollection;
269using webrtc::StreamCollectionInterface;
perkja3ede6c2016-03-08 01:27:48 +0100270using webrtc::VideoTrackSourceInterface;
deadbeefab9b2d12015-10-14 11:33:11 -0700271using webrtc::VideoTrack;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000272using webrtc::VideoTrackInterface;
273
deadbeefab9b2d12015-10-14 11:33:11 -0700274typedef PeerConnectionInterface::RTCOfferAnswerOptions RTCOfferAnswerOptions;
275
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000276namespace {
277
278// Gets the first ssrc of given content type from the ContentInfo.
279bool GetFirstSsrc(const cricket::ContentInfo* content_info, int* ssrc) {
280 if (!content_info || !ssrc) {
281 return false;
282 }
283 const cricket::MediaContentDescription* media_desc =
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000284 static_cast<const cricket::MediaContentDescription*>(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000285 content_info->description);
286 if (!media_desc || media_desc->streams().empty()) {
287 return false;
288 }
289 *ssrc = media_desc->streams().begin()->first_ssrc();
290 return true;
291}
292
293void SetSsrcToZero(std::string* sdp) {
294 const char kSdpSsrcAtribute[] = "a=ssrc:";
295 const char kSdpSsrcAtributeZero[] = "a=ssrc:0";
296 size_t ssrc_pos = 0;
297 while ((ssrc_pos = sdp->find(kSdpSsrcAtribute, ssrc_pos)) !=
298 std::string::npos) {
299 size_t end_ssrc = sdp->find(" ", ssrc_pos);
300 sdp->replace(ssrc_pos, end_ssrc - ssrc_pos, kSdpSsrcAtributeZero);
301 ssrc_pos = end_ssrc;
302 }
303}
304
deadbeefab9b2d12015-10-14 11:33:11 -0700305// Check if |streams| contains the specified track.
306bool ContainsTrack(const std::vector<cricket::StreamParams>& streams,
307 const std::string& stream_label,
308 const std::string& track_id) {
309 for (const cricket::StreamParams& params : streams) {
310 if (params.sync_label == stream_label && params.id == track_id) {
311 return true;
312 }
313 }
314 return false;
315}
316
317// Check if |senders| contains the specified sender, by id.
318bool ContainsSender(
319 const std::vector<rtc::scoped_refptr<RtpSenderInterface>>& senders,
320 const std::string& id) {
321 for (const auto& sender : senders) {
322 if (sender->id() == id) {
323 return true;
324 }
325 }
326 return false;
327}
328
329// Create a collection of streams.
330// CreateStreamCollection(1) creates a collection that
331// correspond to kSdpStringWithStream1.
332// CreateStreamCollection(2) correspond to kSdpStringWithStream1And2.
333rtc::scoped_refptr<StreamCollection> CreateStreamCollection(
334 int number_of_streams) {
335 rtc::scoped_refptr<StreamCollection> local_collection(
336 StreamCollection::Create());
337
338 for (int i = 0; i < number_of_streams; ++i) {
339 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream(
340 webrtc::MediaStream::Create(kStreams[i]));
341
342 // Add a local audio track.
343 rtc::scoped_refptr<webrtc::AudioTrackInterface> audio_track(
344 webrtc::AudioTrack::Create(kAudioTracks[i], nullptr));
345 stream->AddTrack(audio_track);
346
347 // Add a local video track.
348 rtc::scoped_refptr<webrtc::VideoTrackInterface> video_track(
nisseaf510af2016-03-21 08:20:42 -0700349 webrtc::VideoTrack::Create(kVideoTracks[i],
350 webrtc::FakeVideoTrackSource::Create()));
deadbeefab9b2d12015-10-14 11:33:11 -0700351 stream->AddTrack(video_track);
352
353 local_collection->AddStream(stream);
354 }
355 return local_collection;
356}
357
358// Check equality of StreamCollections.
359bool CompareStreamCollections(StreamCollectionInterface* s1,
360 StreamCollectionInterface* s2) {
361 if (s1 == nullptr || s2 == nullptr || s1->count() != s2->count()) {
362 return false;
363 }
364
365 for (size_t i = 0; i != s1->count(); ++i) {
366 if (s1->at(i)->label() != s2->at(i)->label()) {
367 return false;
368 }
369 webrtc::AudioTrackVector audio_tracks1 = s1->at(i)->GetAudioTracks();
370 webrtc::AudioTrackVector audio_tracks2 = s2->at(i)->GetAudioTracks();
371 webrtc::VideoTrackVector video_tracks1 = s1->at(i)->GetVideoTracks();
372 webrtc::VideoTrackVector video_tracks2 = s2->at(i)->GetVideoTracks();
373
374 if (audio_tracks1.size() != audio_tracks2.size()) {
375 return false;
376 }
377 for (size_t j = 0; j != audio_tracks1.size(); ++j) {
378 if (audio_tracks1[j]->id() != audio_tracks2[j]->id()) {
379 return false;
380 }
381 }
382 if (video_tracks1.size() != video_tracks2.size()) {
383 return false;
384 }
385 for (size_t j = 0; j != video_tracks1.size(); ++j) {
386 if (video_tracks1[j]->id() != video_tracks2[j]->id()) {
387 return false;
388 }
389 }
390 }
391 return true;
392}
393
perkjd61bf802016-03-24 03:16:19 -0700394// Helper class to test Observer.
395class MockTrackObserver : public ObserverInterface {
396 public:
397 explicit MockTrackObserver(NotifierInterface* notifier)
398 : notifier_(notifier) {
399 notifier_->RegisterObserver(this);
400 }
401
402 ~MockTrackObserver() { Unregister(); }
403
404 void Unregister() {
405 if (notifier_) {
406 notifier_->UnregisterObserver(this);
407 notifier_ = nullptr;
408 }
409 }
410
411 MOCK_METHOD0(OnChanged, void());
412
413 private:
414 NotifierInterface* notifier_;
415};
416
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000417class MockPeerConnectionObserver : public PeerConnectionObserver {
418 public:
deadbeefab9b2d12015-10-14 11:33:11 -0700419 MockPeerConnectionObserver() : remote_streams_(StreamCollection::Create()) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000420 ~MockPeerConnectionObserver() {
421 }
422 void SetPeerConnectionInterface(PeerConnectionInterface* pc) {
423 pc_ = pc;
424 if (pc) {
425 state_ = pc_->signaling_state();
426 }
427 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000428 virtual void OnSignalingChange(
429 PeerConnectionInterface::SignalingState new_state) {
430 EXPECT_EQ(pc_->signaling_state(), new_state);
431 state_ = new_state;
432 }
433 // TODO(bemasc): Remove this once callers transition to OnIceGatheringChange.
434 virtual void OnStateChange(StateType state_changed) {
435 if (pc_.get() == NULL)
436 return;
437 switch (state_changed) {
438 case kSignalingState:
439 // OnSignalingChange and OnStateChange(kSignalingState) should always
440 // be called approximately simultaneously. To ease testing, we require
441 // that they always be called in that order. This check verifies
442 // that OnSignalingChange has just been called.
443 EXPECT_EQ(pc_->signaling_state(), state_);
444 break;
445 case kIceState:
446 ADD_FAILURE();
447 break;
448 default:
449 ADD_FAILURE();
450 break;
451 }
452 }
deadbeefab9b2d12015-10-14 11:33:11 -0700453
454 MediaStreamInterface* RemoteStream(const std::string& label) {
455 return remote_streams_->find(label);
456 }
457 StreamCollectionInterface* remote_streams() const { return remote_streams_; }
perkjdfb769d2016-02-09 03:09:43 -0800458 void OnAddStream(MediaStreamInterface* stream) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000459 last_added_stream_ = stream;
deadbeefab9b2d12015-10-14 11:33:11 -0700460 remote_streams_->AddStream(stream);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000461 }
perkjdfb769d2016-02-09 03:09:43 -0800462 void OnRemoveStream(MediaStreamInterface* stream) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000463 last_removed_stream_ = stream;
deadbeefab9b2d12015-10-14 11:33:11 -0700464 remote_streams_->RemoveStream(stream);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000465 }
perkjdfb769d2016-02-09 03:09:43 -0800466 void OnRenegotiationNeeded() override { renegotiation_needed_ = true; }
467 void OnDataChannel(DataChannelInterface* data_channel) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000468 last_datachannel_ = data_channel;
469 }
470
perkjdfb769d2016-02-09 03:09:43 -0800471 void OnIceConnectionChange(
472 PeerConnectionInterface::IceConnectionState new_state) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000473 EXPECT_EQ(pc_->ice_connection_state(), new_state);
474 }
perkjdfb769d2016-02-09 03:09:43 -0800475 void OnIceGatheringChange(
476 PeerConnectionInterface::IceGatheringState new_state) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000477 EXPECT_EQ(pc_->ice_gathering_state(), new_state);
perkjdfb769d2016-02-09 03:09:43 -0800478 ice_complete_ = new_state == PeerConnectionInterface::kIceGatheringComplete;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000479 }
perkjdfb769d2016-02-09 03:09:43 -0800480 void OnIceCandidate(const webrtc::IceCandidateInterface* candidate) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000481 EXPECT_NE(PeerConnectionInterface::kIceGatheringNew,
482 pc_->ice_gathering_state());
483
484 std::string sdp;
485 EXPECT_TRUE(candidate->ToString(&sdp));
486 EXPECT_LT(0u, sdp.size());
487 last_candidate_.reset(webrtc::CreateIceCandidate(candidate->sdp_mid(),
488 candidate->sdp_mline_index(), sdp, NULL));
489 EXPECT_TRUE(last_candidate_.get() != NULL);
490 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000491
492 // Returns the label of the last added stream.
493 // Empty string if no stream have been added.
494 std::string GetLastAddedStreamLabel() {
495 if (last_added_stream_.get())
496 return last_added_stream_->label();
497 return "";
498 }
499 std::string GetLastRemovedStreamLabel() {
500 if (last_removed_stream_.get())
501 return last_removed_stream_->label();
502 return "";
503 }
504
505 scoped_refptr<PeerConnectionInterface> pc_;
506 PeerConnectionInterface::SignalingState state_;
507 scoped_ptr<IceCandidateInterface> last_candidate_;
508 scoped_refptr<DataChannelInterface> last_datachannel_;
deadbeefab9b2d12015-10-14 11:33:11 -0700509 rtc::scoped_refptr<StreamCollection> remote_streams_;
510 bool renegotiation_needed_ = false;
511 bool ice_complete_ = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000512
513 private:
514 scoped_refptr<MediaStreamInterface> last_added_stream_;
515 scoped_refptr<MediaStreamInterface> last_removed_stream_;
516};
517
518} // namespace
deadbeefab9b2d12015-10-14 11:33:11 -0700519
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000520class PeerConnectionInterfaceTest : public testing::Test {
521 protected:
phoglund37ebcf02016-01-08 05:04:57 -0800522 PeerConnectionInterfaceTest() {
523#ifdef WEBRTC_ANDROID
524 webrtc::InitializeAndroidObjects();
525#endif
526 }
527
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000528 virtual void SetUp() {
529 pc_factory_ = webrtc::CreatePeerConnectionFactory(
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000530 rtc::Thread::Current(), rtc::Thread::Current(), NULL, NULL,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000531 NULL);
532 ASSERT_TRUE(pc_factory_.get() != NULL);
533 }
534
535 void CreatePeerConnection() {
536 CreatePeerConnection("", "", NULL);
537 }
538
539 void CreatePeerConnection(webrtc::MediaConstraintsInterface* constraints) {
540 CreatePeerConnection("", "", constraints);
541 }
542
543 void CreatePeerConnection(const std::string& uri,
544 const std::string& password,
545 webrtc::MediaConstraintsInterface* constraints) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800546 PeerConnectionInterface::RTCConfiguration config;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000547 PeerConnectionInterface::IceServer server;
deadbeef0a6c4ca2015-10-06 11:38:28 -0700548 if (!uri.empty()) {
549 server.uri = uri;
550 server.password = password;
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800551 config.servers.push_back(server);
deadbeef0a6c4ca2015-10-06 11:38:28 -0700552 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000553
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800554 rtc::scoped_ptr<cricket::FakePortAllocator> port_allocator(
555 new cricket::FakePortAllocator(rtc::Thread::Current(), nullptr));
556 port_allocator_ = port_allocator.get();
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000557
buildbot@webrtc.org61c1b8e2014-04-09 06:06:38 +0000558 // DTLS does not work in a loopback call, so is disabled for most of the
559 // tests in this file. We only create a FakeIdentityService if the test
560 // explicitly sets the constraint.
jiayl@webrtc.org61e00b02015-03-04 22:17:38 +0000561 FakeConstraints default_constraints;
562 if (!constraints) {
563 constraints = &default_constraints;
564
565 default_constraints.AddMandatory(
566 webrtc::MediaConstraintsInterface::kEnableDtlsSrtp, false);
567 }
568
Henrik Boström5e56c592015-08-11 10:33:13 +0200569 scoped_ptr<webrtc::DtlsIdentityStoreInterface> dtls_identity_store;
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000570 bool dtls;
571 if (FindConstraint(constraints,
572 webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
573 &dtls,
Henrik Boström5e56c592015-08-11 10:33:13 +0200574 nullptr) && dtls) {
575 dtls_identity_store.reset(new FakeDtlsIdentityStore());
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000576 }
kwiberg0eb15ed2015-12-17 03:04:15 -0800577 pc_ = pc_factory_->CreatePeerConnection(
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800578 config, constraints, std::move(port_allocator),
kwiberg0eb15ed2015-12-17 03:04:15 -0800579 std::move(dtls_identity_store), &observer_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000580 ASSERT_TRUE(pc_.get() != NULL);
581 observer_.SetPeerConnectionInterface(pc_.get());
582 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
583 }
584
deadbeef0a6c4ca2015-10-06 11:38:28 -0700585 void CreatePeerConnectionExpectFail(const std::string& uri) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800586 PeerConnectionInterface::RTCConfiguration config;
deadbeef0a6c4ca2015-10-06 11:38:28 -0700587 PeerConnectionInterface::IceServer server;
deadbeef0a6c4ca2015-10-06 11:38:28 -0700588 server.uri = uri;
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800589 config.servers.push_back(server);
deadbeef0a6c4ca2015-10-06 11:38:28 -0700590
deadbeef0a6c4ca2015-10-06 11:38:28 -0700591 scoped_refptr<PeerConnectionInterface> pc;
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800592 pc = pc_factory_->CreatePeerConnection(config, nullptr, nullptr, nullptr,
593 &observer_);
594 EXPECT_EQ(nullptr, pc);
deadbeef0a6c4ca2015-10-06 11:38:28 -0700595 }
596
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000597 void CreatePeerConnectionWithDifferentConfigurations() {
598 CreatePeerConnection(kStunAddressOnly, "", NULL);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800599 EXPECT_EQ(1u, port_allocator_->stun_servers().size());
600 EXPECT_EQ(0u, port_allocator_->turn_servers().size());
601 EXPECT_EQ("address", port_allocator_->stun_servers().begin()->hostname());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000602 EXPECT_EQ(kDefaultStunPort,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800603 port_allocator_->stun_servers().begin()->port());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000604
deadbeef0a6c4ca2015-10-06 11:38:28 -0700605 CreatePeerConnectionExpectFail(kStunInvalidPort);
606 CreatePeerConnectionExpectFail(kStunAddressPortAndMore1);
607 CreatePeerConnectionExpectFail(kStunAddressPortAndMore2);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000608
609 CreatePeerConnection(kTurnIceServerUri, kTurnPassword, NULL);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800610 EXPECT_EQ(0u, port_allocator_->stun_servers().size());
611 EXPECT_EQ(1u, port_allocator_->turn_servers().size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000612 EXPECT_EQ(kTurnUsername,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800613 port_allocator_->turn_servers()[0].credentials.username);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000614 EXPECT_EQ(kTurnPassword,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800615 port_allocator_->turn_servers()[0].credentials.password);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000616 EXPECT_EQ(kTurnHostname,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800617 port_allocator_->turn_servers()[0].ports[0].address.hostname());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000618 }
619
620 void ReleasePeerConnection() {
621 pc_ = NULL;
622 observer_.SetPeerConnectionInterface(NULL);
623 }
624
deadbeefab9b2d12015-10-14 11:33:11 -0700625 void AddVideoStream(const std::string& label) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000626 // Create a local stream.
627 scoped_refptr<MediaStreamInterface> stream(
628 pc_factory_->CreateLocalMediaStream(label));
perkja3ede6c2016-03-08 01:27:48 +0100629 scoped_refptr<VideoTrackSourceInterface> video_source(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000630 pc_factory_->CreateVideoSource(new cricket::FakeVideoCapturer(), NULL));
631 scoped_refptr<VideoTrackInterface> video_track(
632 pc_factory_->CreateVideoTrack(label + "v0", video_source));
633 stream->AddTrack(video_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +0000634 EXPECT_TRUE(pc_->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000635 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
636 observer_.renegotiation_needed_ = false;
637 }
638
639 void AddVoiceStream(const std::string& label) {
640 // Create a local stream.
641 scoped_refptr<MediaStreamInterface> stream(
642 pc_factory_->CreateLocalMediaStream(label));
643 scoped_refptr<AudioTrackInterface> audio_track(
644 pc_factory_->CreateAudioTrack(label + "a0", NULL));
645 stream->AddTrack(audio_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +0000646 EXPECT_TRUE(pc_->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000647 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
648 observer_.renegotiation_needed_ = false;
649 }
650
651 void AddAudioVideoStream(const std::string& stream_label,
652 const std::string& audio_track_label,
653 const std::string& video_track_label) {
654 // Create a local stream.
655 scoped_refptr<MediaStreamInterface> stream(
656 pc_factory_->CreateLocalMediaStream(stream_label));
657 scoped_refptr<AudioTrackInterface> audio_track(
658 pc_factory_->CreateAudioTrack(
659 audio_track_label, static_cast<AudioSourceInterface*>(NULL)));
660 stream->AddTrack(audio_track.get());
661 scoped_refptr<VideoTrackInterface> video_track(
nisseaf510af2016-03-21 08:20:42 -0700662 pc_factory_->CreateVideoTrack(
663 video_track_label,
664 pc_factory_->CreateVideoSource(new cricket::FakeVideoCapturer())));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000665 stream->AddTrack(video_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +0000666 EXPECT_TRUE(pc_->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000667 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
668 observer_.renegotiation_needed_ = false;
669 }
670
kwiberg2bbff992016-03-16 11:03:04 -0700671 bool DoCreateOfferAnswer(rtc::scoped_ptr<SessionDescriptionInterface>* desc,
deadbeefc80741f2015-10-22 13:14:45 -0700672 bool offer,
673 MediaConstraintsInterface* constraints) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000674 rtc::scoped_refptr<MockCreateSessionDescriptionObserver>
675 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000676 MockCreateSessionDescriptionObserver>());
677 if (offer) {
deadbeefc80741f2015-10-22 13:14:45 -0700678 pc_->CreateOffer(observer, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000679 } else {
deadbeefc80741f2015-10-22 13:14:45 -0700680 pc_->CreateAnswer(observer, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000681 }
682 EXPECT_EQ_WAIT(true, observer->called(), kTimeout);
kwiberg2bbff992016-03-16 11:03:04 -0700683 desc->reset(observer->release_desc());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000684 return observer->result();
685 }
686
kwiberg2bbff992016-03-16 11:03:04 -0700687 bool DoCreateOffer(rtc::scoped_ptr<SessionDescriptionInterface>* desc,
deadbeefc80741f2015-10-22 13:14:45 -0700688 MediaConstraintsInterface* constraints) {
689 return DoCreateOfferAnswer(desc, true, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000690 }
691
kwiberg2bbff992016-03-16 11:03:04 -0700692 bool DoCreateAnswer(rtc::scoped_ptr<SessionDescriptionInterface>* desc,
deadbeefc80741f2015-10-22 13:14:45 -0700693 MediaConstraintsInterface* constraints) {
694 return DoCreateOfferAnswer(desc, false, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000695 }
696
697 bool DoSetSessionDescription(SessionDescriptionInterface* desc, bool local) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000698 rtc::scoped_refptr<MockSetSessionDescriptionObserver>
699 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000700 MockSetSessionDescriptionObserver>());
701 if (local) {
702 pc_->SetLocalDescription(observer, desc);
703 } else {
704 pc_->SetRemoteDescription(observer, desc);
705 }
706 EXPECT_EQ_WAIT(true, observer->called(), kTimeout);
707 return observer->result();
708 }
709
710 bool DoSetLocalDescription(SessionDescriptionInterface* desc) {
711 return DoSetSessionDescription(desc, true);
712 }
713
714 bool DoSetRemoteDescription(SessionDescriptionInterface* desc) {
715 return DoSetSessionDescription(desc, false);
716 }
717
718 // Calls PeerConnection::GetStats and check the return value.
719 // It does not verify the values in the StatReports since a RTCP packet might
720 // be required.
721 bool DoGetStats(MediaStreamTrackInterface* track) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000722 rtc::scoped_refptr<MockStatsObserver> observer(
723 new rtc::RefCountedObject<MockStatsObserver>());
jiayl@webrtc.orgdb41b4d2014-03-03 21:30:06 +0000724 if (!pc_->GetStats(
725 observer, track, PeerConnectionInterface::kStatsOutputLevelStandard))
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000726 return false;
727 EXPECT_TRUE_WAIT(observer->called(), kTimeout);
728 return observer->called();
729 }
730
731 void InitiateCall() {
732 CreatePeerConnection();
733 // Create a local stream with audio&video tracks.
734 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
735 CreateOfferReceiveAnswer();
736 }
737
738 // Verify that RTP Header extensions has been negotiated for audio and video.
739 void VerifyRemoteRtpHeaderExtensions() {
740 const cricket::MediaContentDescription* desc =
741 cricket::GetFirstAudioContentDescription(
742 pc_->remote_description()->description());
743 ASSERT_TRUE(desc != NULL);
744 EXPECT_GT(desc->rtp_header_extensions().size(), 0u);
745
746 desc = cricket::GetFirstVideoContentDescription(
747 pc_->remote_description()->description());
748 ASSERT_TRUE(desc != NULL);
749 EXPECT_GT(desc->rtp_header_extensions().size(), 0u);
750 }
751
752 void CreateOfferAsRemoteDescription() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000753 rtc::scoped_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -0700754 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000755 std::string sdp;
756 EXPECT_TRUE(offer->ToString(&sdp));
757 SessionDescriptionInterface* remote_offer =
758 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
759 sdp, NULL);
760 EXPECT_TRUE(DoSetRemoteDescription(remote_offer));
761 EXPECT_EQ(PeerConnectionInterface::kHaveRemoteOffer, observer_.state_);
762 }
763
deadbeefab9b2d12015-10-14 11:33:11 -0700764 void CreateAndSetRemoteOffer(const std::string& sdp) {
765 SessionDescriptionInterface* remote_offer =
766 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
767 sdp, nullptr);
768 EXPECT_TRUE(DoSetRemoteDescription(remote_offer));
769 EXPECT_EQ(PeerConnectionInterface::kHaveRemoteOffer, observer_.state_);
770 }
771
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000772 void CreateAnswerAsLocalDescription() {
773 scoped_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -0700774 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000775
776 // TODO(perkj): Currently SetLocalDescription fails if any parameters in an
777 // audio codec change, even if the parameter has nothing to do with
778 // receiving. Not all parameters are serialized to SDP.
779 // Since CreatePrAnswerAsLocalDescription serialize/deserialize
780 // the SessionDescription, it is necessary to do that here to in order to
781 // get ReceiveOfferCreatePrAnswerAndAnswer and RenegotiateAudioOnly to pass.
782 // https://code.google.com/p/webrtc/issues/detail?id=1356
783 std::string sdp;
784 EXPECT_TRUE(answer->ToString(&sdp));
785 SessionDescriptionInterface* new_answer =
786 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
787 sdp, NULL);
788 EXPECT_TRUE(DoSetLocalDescription(new_answer));
789 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
790 }
791
792 void CreatePrAnswerAsLocalDescription() {
793 scoped_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -0700794 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000795
796 std::string sdp;
797 EXPECT_TRUE(answer->ToString(&sdp));
798 SessionDescriptionInterface* pr_answer =
799 webrtc::CreateSessionDescription(SessionDescriptionInterface::kPrAnswer,
800 sdp, NULL);
801 EXPECT_TRUE(DoSetLocalDescription(pr_answer));
802 EXPECT_EQ(PeerConnectionInterface::kHaveLocalPrAnswer, observer_.state_);
803 }
804
805 void CreateOfferReceiveAnswer() {
806 CreateOfferAsLocalDescription();
807 std::string sdp;
808 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
809 CreateAnswerAsRemoteDescription(sdp);
810 }
811
812 void CreateOfferAsLocalDescription() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000813 rtc::scoped_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -0700814 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000815 // TODO(perkj): Currently SetLocalDescription fails if any parameters in an
816 // audio codec change, even if the parameter has nothing to do with
817 // receiving. Not all parameters are serialized to SDP.
818 // Since CreatePrAnswerAsLocalDescription serialize/deserialize
819 // the SessionDescription, it is necessary to do that here to in order to
820 // get ReceiveOfferCreatePrAnswerAndAnswer and RenegotiateAudioOnly to pass.
821 // https://code.google.com/p/webrtc/issues/detail?id=1356
822 std::string sdp;
823 EXPECT_TRUE(offer->ToString(&sdp));
824 SessionDescriptionInterface* new_offer =
825 webrtc::CreateSessionDescription(
826 SessionDescriptionInterface::kOffer,
827 sdp, NULL);
828
829 EXPECT_TRUE(DoSetLocalDescription(new_offer));
830 EXPECT_EQ(PeerConnectionInterface::kHaveLocalOffer, observer_.state_);
mallinath@webrtc.org68cbd012014-01-22 00:16:46 +0000831 // Wait for the ice_complete message, so that SDP will have candidates.
832 EXPECT_TRUE_WAIT(observer_.ice_complete_, kTimeout);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000833 }
834
deadbeefab9b2d12015-10-14 11:33:11 -0700835 void CreateAnswerAsRemoteDescription(const std::string& sdp) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000836 webrtc::JsepSessionDescription* answer = new webrtc::JsepSessionDescription(
837 SessionDescriptionInterface::kAnswer);
deadbeefab9b2d12015-10-14 11:33:11 -0700838 EXPECT_TRUE(answer->Initialize(sdp, NULL));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000839 EXPECT_TRUE(DoSetRemoteDescription(answer));
840 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
841 }
842
deadbeefab9b2d12015-10-14 11:33:11 -0700843 void CreatePrAnswerAndAnswerAsRemoteDescription(const std::string& sdp) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000844 webrtc::JsepSessionDescription* pr_answer =
845 new webrtc::JsepSessionDescription(
846 SessionDescriptionInterface::kPrAnswer);
deadbeefab9b2d12015-10-14 11:33:11 -0700847 EXPECT_TRUE(pr_answer->Initialize(sdp, NULL));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000848 EXPECT_TRUE(DoSetRemoteDescription(pr_answer));
849 EXPECT_EQ(PeerConnectionInterface::kHaveRemotePrAnswer, observer_.state_);
850 webrtc::JsepSessionDescription* answer =
851 new webrtc::JsepSessionDescription(
852 SessionDescriptionInterface::kAnswer);
deadbeefab9b2d12015-10-14 11:33:11 -0700853 EXPECT_TRUE(answer->Initialize(sdp, NULL));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000854 EXPECT_TRUE(DoSetRemoteDescription(answer));
855 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
856 }
857
858 // Help function used for waiting until a the last signaled remote stream has
859 // the same label as |stream_label|. In a few of the tests in this file we
860 // answer with the same session description as we offer and thus we can
861 // check if OnAddStream have been called with the same stream as we offer to
862 // send.
863 void WaitAndVerifyOnAddStream(const std::string& stream_label) {
864 EXPECT_EQ_WAIT(stream_label, observer_.GetLastAddedStreamLabel(), kTimeout);
865 }
866
867 // Creates an offer and applies it as a local session description.
868 // Creates an answer with the same SDP an the offer but removes all lines
869 // that start with a:ssrc"
870 void CreateOfferReceiveAnswerWithoutSsrc() {
871 CreateOfferAsLocalDescription();
872 std::string sdp;
873 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
874 SetSsrcToZero(&sdp);
875 CreateAnswerAsRemoteDescription(sdp);
876 }
877
deadbeefab9b2d12015-10-14 11:33:11 -0700878 // This function creates a MediaStream with label kStreams[0] and
879 // |number_of_audio_tracks| and |number_of_video_tracks| tracks and the
880 // corresponding SessionDescriptionInterface. The SessionDescriptionInterface
kwiberg2bbff992016-03-16 11:03:04 -0700881 // is returned and the MediaStream is stored in
deadbeefab9b2d12015-10-14 11:33:11 -0700882 // |reference_collection_|
kwiberg2bbff992016-03-16 11:03:04 -0700883 rtc::scoped_ptr<SessionDescriptionInterface>
884 CreateSessionDescriptionAndReference(size_t number_of_audio_tracks,
885 size_t number_of_video_tracks) {
886 EXPECT_LE(number_of_audio_tracks, 2u);
887 EXPECT_LE(number_of_video_tracks, 2u);
deadbeefab9b2d12015-10-14 11:33:11 -0700888
889 reference_collection_ = StreamCollection::Create();
890 std::string sdp_ms1 = std::string(kSdpStringInit);
891
892 std::string mediastream_label = kStreams[0];
893
894 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream(
895 webrtc::MediaStream::Create(mediastream_label));
896 reference_collection_->AddStream(stream);
897
898 if (number_of_audio_tracks > 0) {
899 sdp_ms1 += std::string(kSdpStringAudio);
900 sdp_ms1 += std::string(kSdpStringMs1Audio0);
901 AddAudioTrack(kAudioTracks[0], stream);
902 }
903 if (number_of_audio_tracks > 1) {
904 sdp_ms1 += kSdpStringMs1Audio1;
905 AddAudioTrack(kAudioTracks[1], stream);
906 }
907
908 if (number_of_video_tracks > 0) {
909 sdp_ms1 += std::string(kSdpStringVideo);
910 sdp_ms1 += std::string(kSdpStringMs1Video0);
911 AddVideoTrack(kVideoTracks[0], stream);
912 }
913 if (number_of_video_tracks > 1) {
914 sdp_ms1 += kSdpStringMs1Video1;
915 AddVideoTrack(kVideoTracks[1], stream);
916 }
917
kwiberg2bbff992016-03-16 11:03:04 -0700918 return rtc::scoped_ptr<SessionDescriptionInterface>(
919 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
920 sdp_ms1, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -0700921 }
922
923 void AddAudioTrack(const std::string& track_id,
924 MediaStreamInterface* stream) {
925 rtc::scoped_refptr<webrtc::AudioTrackInterface> audio_track(
926 webrtc::AudioTrack::Create(track_id, nullptr));
927 ASSERT_TRUE(stream->AddTrack(audio_track));
928 }
929
930 void AddVideoTrack(const std::string& track_id,
931 MediaStreamInterface* stream) {
932 rtc::scoped_refptr<webrtc::VideoTrackInterface> video_track(
nisseaf510af2016-03-21 08:20:42 -0700933 webrtc::VideoTrack::Create(track_id,
934 webrtc::FakeVideoTrackSource::Create()));
deadbeefab9b2d12015-10-14 11:33:11 -0700935 ASSERT_TRUE(stream->AddTrack(video_track));
936 }
937
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800938 cricket::FakePortAllocator* port_allocator_ = nullptr;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000939 scoped_refptr<webrtc::PeerConnectionFactoryInterface> pc_factory_;
940 scoped_refptr<PeerConnectionInterface> pc_;
941 MockPeerConnectionObserver observer_;
deadbeefab9b2d12015-10-14 11:33:11 -0700942 rtc::scoped_refptr<StreamCollection> reference_collection_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000943};
944
945TEST_F(PeerConnectionInterfaceTest,
946 CreatePeerConnectionWithDifferentConfigurations) {
947 CreatePeerConnectionWithDifferentConfigurations();
948}
949
950TEST_F(PeerConnectionInterfaceTest, AddStreams) {
951 CreatePeerConnection();
deadbeefab9b2d12015-10-14 11:33:11 -0700952 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000953 AddVoiceStream(kStreamLabel2);
954 ASSERT_EQ(2u, pc_->local_streams()->count());
955
wu@webrtc.org9dba5252013-08-05 20:36:57 +0000956 // Test we can add multiple local streams to one peerconnection.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000957 scoped_refptr<MediaStreamInterface> stream(
958 pc_factory_->CreateLocalMediaStream(kStreamLabel3));
959 scoped_refptr<AudioTrackInterface> audio_track(
960 pc_factory_->CreateAudioTrack(
961 kStreamLabel3, static_cast<AudioSourceInterface*>(NULL)));
962 stream->AddTrack(audio_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +0000963 EXPECT_TRUE(pc_->AddStream(stream));
wu@webrtc.org9dba5252013-08-05 20:36:57 +0000964 EXPECT_EQ(3u, pc_->local_streams()->count());
965
966 // Remove the third stream.
967 pc_->RemoveStream(pc_->local_streams()->at(2));
968 EXPECT_EQ(2u, pc_->local_streams()->count());
969
970 // Remove the second stream.
971 pc_->RemoveStream(pc_->local_streams()->at(1));
972 EXPECT_EQ(1u, pc_->local_streams()->count());
973
974 // Remove the first stream.
975 pc_->RemoveStream(pc_->local_streams()->at(0));
976 EXPECT_EQ(0u, pc_->local_streams()->count());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000977}
978
deadbeefab9b2d12015-10-14 11:33:11 -0700979// Test that the created offer includes streams we added.
980TEST_F(PeerConnectionInterfaceTest, AddedStreamsPresentInOffer) {
981 CreatePeerConnection();
982 AddAudioVideoStream(kStreamLabel1, "audio_track", "video_track");
983 scoped_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -0700984 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -0700985
986 const cricket::ContentInfo* audio_content =
987 cricket::GetFirstAudioContent(offer->description());
988 const cricket::AudioContentDescription* audio_desc =
989 static_cast<const cricket::AudioContentDescription*>(
990 audio_content->description);
991 EXPECT_TRUE(
992 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
993
994 const cricket::ContentInfo* video_content =
995 cricket::GetFirstVideoContent(offer->description());
996 const cricket::VideoContentDescription* video_desc =
997 static_cast<const cricket::VideoContentDescription*>(
998 video_content->description);
999 EXPECT_TRUE(
1000 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1001
1002 // Add another stream and ensure the offer includes both the old and new
1003 // streams.
1004 AddAudioVideoStream(kStreamLabel2, "audio_track2", "video_track2");
kwiberg2bbff992016-03-16 11:03:04 -07001005 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07001006
1007 audio_content = cricket::GetFirstAudioContent(offer->description());
1008 audio_desc = static_cast<const cricket::AudioContentDescription*>(
1009 audio_content->description);
1010 EXPECT_TRUE(
1011 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
1012 EXPECT_TRUE(
1013 ContainsTrack(audio_desc->streams(), kStreamLabel2, "audio_track2"));
1014
1015 video_content = cricket::GetFirstVideoContent(offer->description());
1016 video_desc = static_cast<const cricket::VideoContentDescription*>(
1017 video_content->description);
1018 EXPECT_TRUE(
1019 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1020 EXPECT_TRUE(
1021 ContainsTrack(video_desc->streams(), kStreamLabel2, "video_track2"));
1022}
1023
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001024TEST_F(PeerConnectionInterfaceTest, RemoveStream) {
1025 CreatePeerConnection();
deadbeefab9b2d12015-10-14 11:33:11 -07001026 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001027 ASSERT_EQ(1u, pc_->local_streams()->count());
1028 pc_->RemoveStream(pc_->local_streams()->at(0));
1029 EXPECT_EQ(0u, pc_->local_streams()->count());
1030}
1031
deadbeefe1f9d832016-01-14 15:35:42 -08001032// Test for AddTrack and RemoveTrack methods.
1033// Tests that the created offer includes tracks we added,
1034// and that the RtpSenders are created correctly.
1035// Also tests that RemoveTrack removes the tracks from subsequent offers.
1036TEST_F(PeerConnectionInterfaceTest, AddTrackRemoveTrack) {
1037 CreatePeerConnection();
1038 // Create a dummy stream, so tracks share a stream label.
1039 scoped_refptr<MediaStreamInterface> stream(
1040 pc_factory_->CreateLocalMediaStream(kStreamLabel1));
1041 std::vector<MediaStreamInterface*> stream_list;
1042 stream_list.push_back(stream.get());
1043 scoped_refptr<AudioTrackInterface> audio_track(
1044 pc_factory_->CreateAudioTrack("audio_track", nullptr));
nisseaf510af2016-03-21 08:20:42 -07001045 scoped_refptr<VideoTrackInterface> video_track(pc_factory_->CreateVideoTrack(
1046 "video_track",
1047 pc_factory_->CreateVideoSource(new cricket::FakeVideoCapturer())));
deadbeefe1f9d832016-01-14 15:35:42 -08001048 auto audio_sender = pc_->AddTrack(audio_track, stream_list);
1049 auto video_sender = pc_->AddTrack(video_track, stream_list);
1050 EXPECT_EQ(kStreamLabel1, audio_sender->stream_id());
1051 EXPECT_EQ("audio_track", audio_sender->id());
1052 EXPECT_EQ(audio_track, audio_sender->track());
1053 EXPECT_EQ(kStreamLabel1, video_sender->stream_id());
1054 EXPECT_EQ("video_track", video_sender->id());
1055 EXPECT_EQ(video_track, video_sender->track());
1056
1057 // Now create an offer and check for the senders.
1058 scoped_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001059 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefe1f9d832016-01-14 15:35:42 -08001060
1061 const cricket::ContentInfo* audio_content =
1062 cricket::GetFirstAudioContent(offer->description());
1063 const cricket::AudioContentDescription* audio_desc =
1064 static_cast<const cricket::AudioContentDescription*>(
1065 audio_content->description);
1066 EXPECT_TRUE(
1067 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
1068
1069 const cricket::ContentInfo* video_content =
1070 cricket::GetFirstVideoContent(offer->description());
1071 const cricket::VideoContentDescription* video_desc =
1072 static_cast<const cricket::VideoContentDescription*>(
1073 video_content->description);
1074 EXPECT_TRUE(
1075 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1076
1077 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
1078
1079 // Now try removing the tracks.
1080 EXPECT_TRUE(pc_->RemoveTrack(audio_sender));
1081 EXPECT_TRUE(pc_->RemoveTrack(video_sender));
1082
1083 // Create a new offer and ensure it doesn't contain the removed senders.
kwiberg2bbff992016-03-16 11:03:04 -07001084 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefe1f9d832016-01-14 15:35:42 -08001085
1086 audio_content = cricket::GetFirstAudioContent(offer->description());
1087 audio_desc = static_cast<const cricket::AudioContentDescription*>(
1088 audio_content->description);
1089 EXPECT_FALSE(
1090 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
1091
1092 video_content = cricket::GetFirstVideoContent(offer->description());
1093 video_desc = static_cast<const cricket::VideoContentDescription*>(
1094 video_content->description);
1095 EXPECT_FALSE(
1096 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1097
1098 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
1099
1100 // Calling RemoveTrack on a sender no longer attached to a PeerConnection
1101 // should return false.
1102 EXPECT_FALSE(pc_->RemoveTrack(audio_sender));
1103 EXPECT_FALSE(pc_->RemoveTrack(video_sender));
1104}
1105
1106// Test creating senders without a stream specified,
1107// expecting a random stream ID to be generated.
1108TEST_F(PeerConnectionInterfaceTest, AddTrackWithoutStream) {
1109 CreatePeerConnection();
1110 // Create a dummy stream, so tracks share a stream label.
1111 scoped_refptr<AudioTrackInterface> audio_track(
1112 pc_factory_->CreateAudioTrack("audio_track", nullptr));
nisseaf510af2016-03-21 08:20:42 -07001113 scoped_refptr<VideoTrackInterface> video_track(pc_factory_->CreateVideoTrack(
1114 "video_track",
1115 pc_factory_->CreateVideoSource(new cricket::FakeVideoCapturer())));
deadbeefe1f9d832016-01-14 15:35:42 -08001116 auto audio_sender =
1117 pc_->AddTrack(audio_track, std::vector<MediaStreamInterface*>());
1118 auto video_sender =
1119 pc_->AddTrack(video_track, std::vector<MediaStreamInterface*>());
1120 EXPECT_EQ("audio_track", audio_sender->id());
1121 EXPECT_EQ(audio_track, audio_sender->track());
1122 EXPECT_EQ("video_track", video_sender->id());
1123 EXPECT_EQ(video_track, video_sender->track());
1124 // If the ID is truly a random GUID, it should be infinitely unlikely they
1125 // will be the same.
1126 EXPECT_NE(video_sender->stream_id(), audio_sender->stream_id());
1127}
1128
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001129TEST_F(PeerConnectionInterfaceTest, CreateOfferReceiveAnswer) {
1130 InitiateCall();
1131 WaitAndVerifyOnAddStream(kStreamLabel1);
1132 VerifyRemoteRtpHeaderExtensions();
1133}
1134
1135TEST_F(PeerConnectionInterfaceTest, CreateOfferReceivePrAnswerAndAnswer) {
1136 CreatePeerConnection();
deadbeefab9b2d12015-10-14 11:33:11 -07001137 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001138 CreateOfferAsLocalDescription();
1139 std::string offer;
1140 EXPECT_TRUE(pc_->local_description()->ToString(&offer));
1141 CreatePrAnswerAndAnswerAsRemoteDescription(offer);
1142 WaitAndVerifyOnAddStream(kStreamLabel1);
1143}
1144
1145TEST_F(PeerConnectionInterfaceTest, ReceiveOfferCreateAnswer) {
1146 CreatePeerConnection();
deadbeefab9b2d12015-10-14 11:33:11 -07001147 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001148
1149 CreateOfferAsRemoteDescription();
1150 CreateAnswerAsLocalDescription();
1151
1152 WaitAndVerifyOnAddStream(kStreamLabel1);
1153}
1154
1155TEST_F(PeerConnectionInterfaceTest, ReceiveOfferCreatePrAnswerAndAnswer) {
1156 CreatePeerConnection();
deadbeefab9b2d12015-10-14 11:33:11 -07001157 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001158
1159 CreateOfferAsRemoteDescription();
1160 CreatePrAnswerAsLocalDescription();
1161 CreateAnswerAsLocalDescription();
1162
1163 WaitAndVerifyOnAddStream(kStreamLabel1);
1164}
1165
1166TEST_F(PeerConnectionInterfaceTest, Renegotiate) {
1167 InitiateCall();
1168 ASSERT_EQ(1u, pc_->remote_streams()->count());
1169 pc_->RemoveStream(pc_->local_streams()->at(0));
1170 CreateOfferReceiveAnswer();
1171 EXPECT_EQ(0u, pc_->remote_streams()->count());
deadbeefab9b2d12015-10-14 11:33:11 -07001172 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001173 CreateOfferReceiveAnswer();
1174}
1175
1176// Tests that after negotiating an audio only call, the respondent can perform a
1177// renegotiation that removes the audio stream.
1178TEST_F(PeerConnectionInterfaceTest, RenegotiateAudioOnly) {
1179 CreatePeerConnection();
1180 AddVoiceStream(kStreamLabel1);
1181 CreateOfferAsRemoteDescription();
1182 CreateAnswerAsLocalDescription();
1183
1184 ASSERT_EQ(1u, pc_->remote_streams()->count());
1185 pc_->RemoveStream(pc_->local_streams()->at(0));
1186 CreateOfferReceiveAnswer();
1187 EXPECT_EQ(0u, pc_->remote_streams()->count());
1188}
1189
1190// Test that candidates are generated and that we can parse our own candidates.
1191TEST_F(PeerConnectionInterfaceTest, IceCandidates) {
1192 CreatePeerConnection();
1193
1194 EXPECT_FALSE(pc_->AddIceCandidate(observer_.last_candidate_.get()));
1195 // SetRemoteDescription takes ownership of offer.
kwiberg2bbff992016-03-16 11:03:04 -07001196 rtc::scoped_ptr<SessionDescriptionInterface> offer;
deadbeefab9b2d12015-10-14 11:33:11 -07001197 AddVideoStream(kStreamLabel1);
deadbeefc80741f2015-10-22 13:14:45 -07001198 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
kwiberg2bbff992016-03-16 11:03:04 -07001199 EXPECT_TRUE(DoSetRemoteDescription(offer.release()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001200
1201 // SetLocalDescription takes ownership of answer.
kwiberg2bbff992016-03-16 11:03:04 -07001202 rtc::scoped_ptr<SessionDescriptionInterface> answer;
deadbeefc80741f2015-10-22 13:14:45 -07001203 EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
kwiberg2bbff992016-03-16 11:03:04 -07001204 EXPECT_TRUE(DoSetLocalDescription(answer.release()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001205
1206 EXPECT_TRUE_WAIT(observer_.last_candidate_.get() != NULL, kTimeout);
1207 EXPECT_TRUE_WAIT(observer_.ice_complete_, kTimeout);
1208
1209 EXPECT_TRUE(pc_->AddIceCandidate(observer_.last_candidate_.get()));
1210}
1211
deadbeefab9b2d12015-10-14 11:33:11 -07001212// Test that CreateOffer and CreateAnswer will fail if the track labels are
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001213// not unique.
1214TEST_F(PeerConnectionInterfaceTest, CreateOfferAnswerWithInvalidStream) {
1215 CreatePeerConnection();
1216 // Create a regular offer for the CreateAnswer test later.
kwiberg2bbff992016-03-16 11:03:04 -07001217 rtc::scoped_ptr<SessionDescriptionInterface> offer;
deadbeefc80741f2015-10-22 13:14:45 -07001218 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
kwiberg2bbff992016-03-16 11:03:04 -07001219 EXPECT_TRUE(offer);
1220 offer.reset();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001221
1222 // Create a local stream with audio&video tracks having same label.
1223 AddAudioVideoStream(kStreamLabel1, "track_label", "track_label");
1224
1225 // Test CreateOffer
deadbeefc80741f2015-10-22 13:14:45 -07001226 EXPECT_FALSE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001227
1228 // Test CreateAnswer
kwiberg2bbff992016-03-16 11:03:04 -07001229 rtc::scoped_ptr<SessionDescriptionInterface> answer;
deadbeefc80741f2015-10-22 13:14:45 -07001230 EXPECT_FALSE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001231}
1232
1233// Test that we will get different SSRCs for each tracks in the offer and answer
1234// we created.
1235TEST_F(PeerConnectionInterfaceTest, SsrcInOfferAnswer) {
1236 CreatePeerConnection();
1237 // Create a local stream with audio&video tracks having different labels.
1238 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
1239
1240 // Test CreateOffer
1241 scoped_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001242 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001243 int audio_ssrc = 0;
1244 int video_ssrc = 0;
1245 EXPECT_TRUE(GetFirstSsrc(GetFirstAudioContent(offer->description()),
1246 &audio_ssrc));
1247 EXPECT_TRUE(GetFirstSsrc(GetFirstVideoContent(offer->description()),
1248 &video_ssrc));
1249 EXPECT_NE(audio_ssrc, video_ssrc);
1250
1251 // Test CreateAnswer
1252 EXPECT_TRUE(DoSetRemoteDescription(offer.release()));
1253 scoped_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -07001254 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001255 audio_ssrc = 0;
1256 video_ssrc = 0;
1257 EXPECT_TRUE(GetFirstSsrc(GetFirstAudioContent(answer->description()),
1258 &audio_ssrc));
1259 EXPECT_TRUE(GetFirstSsrc(GetFirstVideoContent(answer->description()),
1260 &video_ssrc));
1261 EXPECT_NE(audio_ssrc, video_ssrc);
1262}
1263
deadbeefeb459812015-12-15 19:24:43 -08001264// Test that it's possible to call AddTrack on a MediaStream after adding
1265// the stream to a PeerConnection.
1266// TODO(deadbeef): Remove this test once this behavior is no longer supported.
1267TEST_F(PeerConnectionInterfaceTest, AddTrackAfterAddStream) {
1268 CreatePeerConnection();
1269 // Create audio stream and add to PeerConnection.
1270 AddVoiceStream(kStreamLabel1);
1271 MediaStreamInterface* stream = pc_->local_streams()->at(0);
1272
1273 // Add video track to the audio-only stream.
nisseaf510af2016-03-21 08:20:42 -07001274 scoped_refptr<VideoTrackInterface> video_track(pc_factory_->CreateVideoTrack(
1275 "video_label",
1276 pc_factory_->CreateVideoSource(new cricket::FakeVideoCapturer())));
deadbeefeb459812015-12-15 19:24:43 -08001277 stream->AddTrack(video_track.get());
1278
1279 scoped_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001280 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefeb459812015-12-15 19:24:43 -08001281
1282 const cricket::MediaContentDescription* video_desc =
1283 cricket::GetFirstVideoContentDescription(offer->description());
1284 EXPECT_TRUE(video_desc != nullptr);
1285}
1286
1287// Test that it's possible to call RemoveTrack on a MediaStream after adding
1288// the stream to a PeerConnection.
1289// TODO(deadbeef): Remove this test once this behavior is no longer supported.
1290TEST_F(PeerConnectionInterfaceTest, RemoveTrackAfterAddStream) {
1291 CreatePeerConnection();
1292 // Create audio/video stream and add to PeerConnection.
1293 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
1294 MediaStreamInterface* stream = pc_->local_streams()->at(0);
1295
1296 // Remove the video track.
1297 stream->RemoveTrack(stream->GetVideoTracks()[0]);
1298
1299 scoped_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001300 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefeb459812015-12-15 19:24:43 -08001301
1302 const cricket::MediaContentDescription* video_desc =
1303 cricket::GetFirstVideoContentDescription(offer->description());
1304 EXPECT_TRUE(video_desc == nullptr);
1305}
1306
deadbeefbd7d8f72015-12-18 16:58:44 -08001307// Test creating a sender with a stream ID, and ensure the ID is populated
1308// in the offer.
1309TEST_F(PeerConnectionInterfaceTest, CreateSenderWithStream) {
1310 CreatePeerConnection();
1311 pc_->CreateSender("video", kStreamLabel1);
1312
1313 scoped_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001314 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefbd7d8f72015-12-18 16:58:44 -08001315
1316 const cricket::MediaContentDescription* video_desc =
1317 cricket::GetFirstVideoContentDescription(offer->description());
1318 ASSERT_TRUE(video_desc != nullptr);
1319 ASSERT_EQ(1u, video_desc->streams().size());
1320 EXPECT_EQ(kStreamLabel1, video_desc->streams()[0].sync_label);
1321}
1322
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001323// Test that we can specify a certain track that we want statistics about.
1324TEST_F(PeerConnectionInterfaceTest, GetStatsForSpecificTrack) {
1325 InitiateCall();
1326 ASSERT_LT(0u, pc_->remote_streams()->count());
1327 ASSERT_LT(0u, pc_->remote_streams()->at(0)->GetAudioTracks().size());
1328 scoped_refptr<MediaStreamTrackInterface> remote_audio =
1329 pc_->remote_streams()->at(0)->GetAudioTracks()[0];
1330 EXPECT_TRUE(DoGetStats(remote_audio));
1331
1332 // Remove the stream. Since we are sending to our selves the local
1333 // and the remote stream is the same.
1334 pc_->RemoveStream(pc_->local_streams()->at(0));
1335 // Do a re-negotiation.
1336 CreateOfferReceiveAnswer();
1337
1338 ASSERT_EQ(0u, pc_->remote_streams()->count());
1339
1340 // Test that we still can get statistics for the old track. Even if it is not
1341 // sent any longer.
1342 EXPECT_TRUE(DoGetStats(remote_audio));
1343}
1344
1345// Test that we can get stats on a video track.
1346TEST_F(PeerConnectionInterfaceTest, GetStatsForVideoTrack) {
1347 InitiateCall();
1348 ASSERT_LT(0u, pc_->remote_streams()->count());
1349 ASSERT_LT(0u, pc_->remote_streams()->at(0)->GetVideoTracks().size());
1350 scoped_refptr<MediaStreamTrackInterface> remote_video =
1351 pc_->remote_streams()->at(0)->GetVideoTracks()[0];
1352 EXPECT_TRUE(DoGetStats(remote_video));
1353}
1354
1355// Test that we don't get statistics for an invalid track.
tommi@webrtc.org908f57e2014-07-21 11:44:39 +00001356// TODO(tommi): Fix this test. DoGetStats will return true
1357// for the unknown track (since GetStats is async), but no
1358// data is returned for the track.
1359TEST_F(PeerConnectionInterfaceTest, DISABLED_GetStatsForInvalidTrack) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001360 InitiateCall();
1361 scoped_refptr<AudioTrackInterface> unknown_audio_track(
1362 pc_factory_->CreateAudioTrack("unknown track", NULL));
1363 EXPECT_FALSE(DoGetStats(unknown_audio_track));
1364}
1365
1366// This test setup two RTP data channels in loop back.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001367TEST_F(PeerConnectionInterfaceTest, TestDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001368 FakeConstraints constraints;
1369 constraints.SetAllowRtpDataChannels();
1370 CreatePeerConnection(&constraints);
1371 scoped_refptr<DataChannelInterface> data1 =
1372 pc_->CreateDataChannel("test1", NULL);
1373 scoped_refptr<DataChannelInterface> data2 =
1374 pc_->CreateDataChannel("test2", NULL);
1375 ASSERT_TRUE(data1 != NULL);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001376 rtc::scoped_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001377 new MockDataChannelObserver(data1));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001378 rtc::scoped_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001379 new MockDataChannelObserver(data2));
1380
1381 EXPECT_EQ(DataChannelInterface::kConnecting, data1->state());
1382 EXPECT_EQ(DataChannelInterface::kConnecting, data2->state());
1383 std::string data_to_send1 = "testing testing";
1384 std::string data_to_send2 = "testing something else";
1385 EXPECT_FALSE(data1->Send(DataBuffer(data_to_send1)));
1386
1387 CreateOfferReceiveAnswer();
1388 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1389 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
1390
1391 EXPECT_EQ(DataChannelInterface::kOpen, data1->state());
1392 EXPECT_EQ(DataChannelInterface::kOpen, data2->state());
1393 EXPECT_TRUE(data1->Send(DataBuffer(data_to_send1)));
1394 EXPECT_TRUE(data2->Send(DataBuffer(data_to_send2)));
1395
1396 EXPECT_EQ_WAIT(data_to_send1, observer1->last_message(), kTimeout);
1397 EXPECT_EQ_WAIT(data_to_send2, observer2->last_message(), kTimeout);
1398
1399 data1->Close();
1400 EXPECT_EQ(DataChannelInterface::kClosing, data1->state());
1401 CreateOfferReceiveAnswer();
1402 EXPECT_FALSE(observer1->IsOpen());
1403 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
1404 EXPECT_TRUE(observer2->IsOpen());
1405
1406 data_to_send2 = "testing something else again";
1407 EXPECT_TRUE(data2->Send(DataBuffer(data_to_send2)));
1408
1409 EXPECT_EQ_WAIT(data_to_send2, observer2->last_message(), kTimeout);
1410}
1411
1412// This test verifies that sendnig binary data over RTP data channels should
1413// fail.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001414TEST_F(PeerConnectionInterfaceTest, TestSendBinaryOnRtpDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001415 FakeConstraints constraints;
1416 constraints.SetAllowRtpDataChannels();
1417 CreatePeerConnection(&constraints);
1418 scoped_refptr<DataChannelInterface> data1 =
1419 pc_->CreateDataChannel("test1", NULL);
1420 scoped_refptr<DataChannelInterface> data2 =
1421 pc_->CreateDataChannel("test2", NULL);
1422 ASSERT_TRUE(data1 != NULL);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001423 rtc::scoped_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001424 new MockDataChannelObserver(data1));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001425 rtc::scoped_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001426 new MockDataChannelObserver(data2));
1427
1428 EXPECT_EQ(DataChannelInterface::kConnecting, data1->state());
1429 EXPECT_EQ(DataChannelInterface::kConnecting, data2->state());
1430
1431 CreateOfferReceiveAnswer();
1432 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1433 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
1434
1435 EXPECT_EQ(DataChannelInterface::kOpen, data1->state());
1436 EXPECT_EQ(DataChannelInterface::kOpen, data2->state());
1437
jbaucheec21bd2016-03-20 06:15:43 -07001438 rtc::CopyOnWriteBuffer buffer("test", 4);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001439 EXPECT_FALSE(data1->Send(DataBuffer(buffer, true)));
1440}
1441
1442// This test setup a RTP data channels in loop back and test that a channel is
1443// opened even if the remote end answer with a zero SSRC.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001444TEST_F(PeerConnectionInterfaceTest, TestSendOnlyDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001445 FakeConstraints constraints;
1446 constraints.SetAllowRtpDataChannels();
1447 CreatePeerConnection(&constraints);
1448 scoped_refptr<DataChannelInterface> data1 =
1449 pc_->CreateDataChannel("test1", NULL);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001450 rtc::scoped_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001451 new MockDataChannelObserver(data1));
1452
1453 CreateOfferReceiveAnswerWithoutSsrc();
1454
1455 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1456
1457 data1->Close();
1458 EXPECT_EQ(DataChannelInterface::kClosing, data1->state());
1459 CreateOfferReceiveAnswerWithoutSsrc();
1460 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
1461 EXPECT_FALSE(observer1->IsOpen());
1462}
1463
1464// This test that if a data channel is added in an answer a receive only channel
1465// channel is created.
1466TEST_F(PeerConnectionInterfaceTest, TestReceiveOnlyDataChannel) {
1467 FakeConstraints constraints;
1468 constraints.SetAllowRtpDataChannels();
1469 CreatePeerConnection(&constraints);
1470
1471 std::string offer_label = "offer_channel";
1472 scoped_refptr<DataChannelInterface> offer_channel =
1473 pc_->CreateDataChannel(offer_label, NULL);
1474
1475 CreateOfferAsLocalDescription();
1476
1477 // Replace the data channel label in the offer and apply it as an answer.
1478 std::string receive_label = "answer_channel";
1479 std::string sdp;
1480 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001481 rtc::replace_substrs(offer_label.c_str(), offer_label.length(),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001482 receive_label.c_str(), receive_label.length(),
1483 &sdp);
1484 CreateAnswerAsRemoteDescription(sdp);
1485
1486 // Verify that a new incoming data channel has been created and that
1487 // it is open but can't we written to.
1488 ASSERT_TRUE(observer_.last_datachannel_ != NULL);
1489 DataChannelInterface* received_channel = observer_.last_datachannel_;
1490 EXPECT_EQ(DataChannelInterface::kConnecting, received_channel->state());
1491 EXPECT_EQ(receive_label, received_channel->label());
1492 EXPECT_FALSE(received_channel->Send(DataBuffer("something")));
1493
1494 // Verify that the channel we initially offered has been rejected.
1495 EXPECT_EQ(DataChannelInterface::kClosed, offer_channel->state());
1496
1497 // Do another offer / answer exchange and verify that the data channel is
1498 // opened.
1499 CreateOfferReceiveAnswer();
1500 EXPECT_EQ_WAIT(DataChannelInterface::kOpen, received_channel->state(),
1501 kTimeout);
1502}
1503
1504// This test that no data channel is returned if a reliable channel is
1505// requested.
1506// TODO(perkj): Remove this test once reliable channels are implemented.
1507TEST_F(PeerConnectionInterfaceTest, CreateReliableRtpDataChannelShouldFail) {
1508 FakeConstraints constraints;
1509 constraints.SetAllowRtpDataChannels();
1510 CreatePeerConnection(&constraints);
1511
1512 std::string label = "test";
1513 webrtc::DataChannelInit config;
1514 config.reliable = true;
1515 scoped_refptr<DataChannelInterface> channel =
1516 pc_->CreateDataChannel(label, &config);
1517 EXPECT_TRUE(channel == NULL);
1518}
1519
deadbeefab9b2d12015-10-14 11:33:11 -07001520// Verifies that duplicated label is not allowed for RTP data channel.
1521TEST_F(PeerConnectionInterfaceTest, RtpDuplicatedLabelNotAllowed) {
1522 FakeConstraints constraints;
1523 constraints.SetAllowRtpDataChannels();
1524 CreatePeerConnection(&constraints);
1525
1526 std::string label = "test";
1527 scoped_refptr<DataChannelInterface> channel =
1528 pc_->CreateDataChannel(label, nullptr);
1529 EXPECT_NE(channel, nullptr);
1530
1531 scoped_refptr<DataChannelInterface> dup_channel =
1532 pc_->CreateDataChannel(label, nullptr);
1533 EXPECT_EQ(dup_channel, nullptr);
1534}
1535
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001536// This tests that a SCTP data channel is returned using different
1537// DataChannelInit configurations.
1538TEST_F(PeerConnectionInterfaceTest, CreateSctpDataChannel) {
1539 FakeConstraints constraints;
1540 constraints.SetAllowDtlsSctpDataChannels();
1541 CreatePeerConnection(&constraints);
1542
1543 webrtc::DataChannelInit config;
1544
1545 scoped_refptr<DataChannelInterface> channel =
1546 pc_->CreateDataChannel("1", &config);
1547 EXPECT_TRUE(channel != NULL);
1548 EXPECT_TRUE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001549 EXPECT_TRUE(observer_.renegotiation_needed_);
1550 observer_.renegotiation_needed_ = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001551
1552 config.ordered = false;
1553 channel = pc_->CreateDataChannel("2", &config);
1554 EXPECT_TRUE(channel != NULL);
1555 EXPECT_TRUE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001556 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001557
1558 config.ordered = true;
1559 config.maxRetransmits = 0;
1560 channel = pc_->CreateDataChannel("3", &config);
1561 EXPECT_TRUE(channel != NULL);
1562 EXPECT_FALSE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001563 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001564
1565 config.maxRetransmits = -1;
1566 config.maxRetransmitTime = 0;
1567 channel = pc_->CreateDataChannel("4", &config);
1568 EXPECT_TRUE(channel != NULL);
1569 EXPECT_FALSE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001570 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001571}
1572
1573// This tests that no data channel is returned if both maxRetransmits and
1574// maxRetransmitTime are set for SCTP data channels.
1575TEST_F(PeerConnectionInterfaceTest,
1576 CreateSctpDataChannelShouldFailForInvalidConfig) {
1577 FakeConstraints constraints;
1578 constraints.SetAllowDtlsSctpDataChannels();
1579 CreatePeerConnection(&constraints);
1580
1581 std::string label = "test";
1582 webrtc::DataChannelInit config;
1583 config.maxRetransmits = 0;
1584 config.maxRetransmitTime = 0;
1585
1586 scoped_refptr<DataChannelInterface> channel =
1587 pc_->CreateDataChannel(label, &config);
1588 EXPECT_TRUE(channel == NULL);
1589}
1590
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001591// The test verifies that creating a SCTP data channel with an id already in use
1592// or out of range should fail.
1593TEST_F(PeerConnectionInterfaceTest,
1594 CreateSctpDataChannelWithInvalidIdShouldFail) {
1595 FakeConstraints constraints;
1596 constraints.SetAllowDtlsSctpDataChannels();
1597 CreatePeerConnection(&constraints);
1598
1599 webrtc::DataChannelInit config;
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001600 scoped_refptr<DataChannelInterface> channel;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001601
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001602 config.id = 1;
1603 channel = pc_->CreateDataChannel("1", &config);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001604 EXPECT_TRUE(channel != NULL);
1605 EXPECT_EQ(1, channel->id());
1606
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001607 channel = pc_->CreateDataChannel("x", &config);
1608 EXPECT_TRUE(channel == NULL);
1609
1610 config.id = cricket::kMaxSctpSid;
1611 channel = pc_->CreateDataChannel("max", &config);
1612 EXPECT_TRUE(channel != NULL);
1613 EXPECT_EQ(config.id, channel->id());
1614
1615 config.id = cricket::kMaxSctpSid + 1;
1616 channel = pc_->CreateDataChannel("x", &config);
1617 EXPECT_TRUE(channel == NULL);
1618}
1619
deadbeefab9b2d12015-10-14 11:33:11 -07001620// Verifies that duplicated label is allowed for SCTP data channel.
1621TEST_F(PeerConnectionInterfaceTest, SctpDuplicatedLabelAllowed) {
1622 FakeConstraints constraints;
1623 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
1624 true);
1625 CreatePeerConnection(&constraints);
1626
1627 std::string label = "test";
1628 scoped_refptr<DataChannelInterface> channel =
1629 pc_->CreateDataChannel(label, nullptr);
1630 EXPECT_NE(channel, nullptr);
1631
1632 scoped_refptr<DataChannelInterface> dup_channel =
1633 pc_->CreateDataChannel(label, nullptr);
1634 EXPECT_NE(dup_channel, nullptr);
1635}
1636
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001637// This test verifies that OnRenegotiationNeeded is fired for every new RTP
1638// DataChannel.
1639TEST_F(PeerConnectionInterfaceTest, RenegotiationNeededForNewRtpDataChannel) {
1640 FakeConstraints constraints;
1641 constraints.SetAllowRtpDataChannels();
1642 CreatePeerConnection(&constraints);
1643
1644 scoped_refptr<DataChannelInterface> dc1 =
1645 pc_->CreateDataChannel("test1", NULL);
1646 EXPECT_TRUE(observer_.renegotiation_needed_);
1647 observer_.renegotiation_needed_ = false;
1648
1649 scoped_refptr<DataChannelInterface> dc2 =
1650 pc_->CreateDataChannel("test2", NULL);
1651 EXPECT_TRUE(observer_.renegotiation_needed_);
1652}
1653
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001654// This test that a data channel closes when a PeerConnection is deleted/closed.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001655TEST_F(PeerConnectionInterfaceTest, DataChannelCloseWhenPeerConnectionClose) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001656 FakeConstraints constraints;
1657 constraints.SetAllowRtpDataChannels();
1658 CreatePeerConnection(&constraints);
1659
1660 scoped_refptr<DataChannelInterface> data1 =
1661 pc_->CreateDataChannel("test1", NULL);
1662 scoped_refptr<DataChannelInterface> data2 =
1663 pc_->CreateDataChannel("test2", NULL);
1664 ASSERT_TRUE(data1 != NULL);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001665 rtc::scoped_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001666 new MockDataChannelObserver(data1));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001667 rtc::scoped_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001668 new MockDataChannelObserver(data2));
1669
1670 CreateOfferReceiveAnswer();
1671 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1672 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
1673
1674 ReleasePeerConnection();
1675 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
1676 EXPECT_EQ(DataChannelInterface::kClosed, data2->state());
1677}
1678
1679// This test that data channels can be rejected in an answer.
1680TEST_F(PeerConnectionInterfaceTest, TestRejectDataChannelInAnswer) {
1681 FakeConstraints constraints;
1682 constraints.SetAllowRtpDataChannels();
1683 CreatePeerConnection(&constraints);
1684
1685 scoped_refptr<DataChannelInterface> offer_channel(
1686 pc_->CreateDataChannel("offer_channel", NULL));
1687
1688 CreateOfferAsLocalDescription();
1689
1690 // Create an answer where the m-line for data channels are rejected.
1691 std::string sdp;
1692 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
1693 webrtc::JsepSessionDescription* answer = new webrtc::JsepSessionDescription(
1694 SessionDescriptionInterface::kAnswer);
1695 EXPECT_TRUE(answer->Initialize(sdp, NULL));
1696 cricket::ContentInfo* data_info =
1697 answer->description()->GetContentByName("data");
1698 data_info->rejected = true;
1699
1700 DoSetRemoteDescription(answer);
1701 EXPECT_EQ(DataChannelInterface::kClosed, offer_channel->state());
1702}
1703
Stefan Holmer55d6e7c2016-03-17 16:26:40 +01001704// Disabled on Win dbg: https://bugs.chromium.org/p/webrtc/issues/detail?id=5659
Stefan Holmer102362b2016-03-18 09:39:07 +01001705#if defined(WEBRTC_WIN) && defined(_DEBUG)
Stefan Holmer55d6e7c2016-03-17 16:26:40 +01001706#define MAYBE_ReceiveFireFoxOffer DISABLED_ReceiveFireFoxOffer
1707#else
1708#define MAYBE_ReceiveFireFoxOffer ReceiveFireFoxOffer
1709#endif
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001710// Test that we can create a session description from an SDP string from
1711// FireFox, use it as a remote session description, generate an answer and use
1712// the answer as a local description.
Stefan Holmer55d6e7c2016-03-17 16:26:40 +01001713TEST_F(PeerConnectionInterfaceTest, MAYBE_ReceiveFireFoxOffer) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001714 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001715 FakeConstraints constraints;
1716 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
1717 true);
1718 CreatePeerConnection(&constraints);
1719 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
1720 SessionDescriptionInterface* desc =
1721 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
jbauchfabe2c92015-07-16 13:43:14 -07001722 webrtc::kFireFoxSdpOffer, nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001723 EXPECT_TRUE(DoSetSessionDescription(desc, false));
1724 CreateAnswerAsLocalDescription();
1725 ASSERT_TRUE(pc_->local_description() != NULL);
1726 ASSERT_TRUE(pc_->remote_description() != NULL);
1727
1728 const cricket::ContentInfo* content =
1729 cricket::GetFirstAudioContent(pc_->local_description()->description());
1730 ASSERT_TRUE(content != NULL);
1731 EXPECT_FALSE(content->rejected);
1732
1733 content =
1734 cricket::GetFirstVideoContent(pc_->local_description()->description());
1735 ASSERT_TRUE(content != NULL);
1736 EXPECT_FALSE(content->rejected);
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00001737#ifdef HAVE_SCTP
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001738 content =
1739 cricket::GetFirstDataContent(pc_->local_description()->description());
1740 ASSERT_TRUE(content != NULL);
1741 EXPECT_TRUE(content->rejected);
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00001742#endif
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001743}
1744
1745// Test that we can create an audio only offer and receive an answer with a
1746// limited set of audio codecs and receive an updated offer with more audio
1747// codecs, where the added codecs are not supported.
1748TEST_F(PeerConnectionInterfaceTest, ReceiveUpdatedAudioOfferWithBadCodecs) {
1749 CreatePeerConnection();
1750 AddVoiceStream("audio_label");
1751 CreateOfferAsLocalDescription();
1752
1753 SessionDescriptionInterface* answer =
1754 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
jbauchfabe2c92015-07-16 13:43:14 -07001755 webrtc::kAudioSdp, nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001756 EXPECT_TRUE(DoSetSessionDescription(answer, false));
1757
1758 SessionDescriptionInterface* updated_offer =
1759 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
jbauchfabe2c92015-07-16 13:43:14 -07001760 webrtc::kAudioSdpWithUnsupportedCodecs,
1761 nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001762 EXPECT_TRUE(DoSetSessionDescription(updated_offer, false));
1763 CreateAnswerAsLocalDescription();
1764}
1765
deadbeefc80741f2015-10-22 13:14:45 -07001766// Test that if we're receiving (but not sending) a track, subsequent offers
1767// will have m-lines with a=recvonly.
1768TEST_F(PeerConnectionInterfaceTest, CreateSubsequentRecvOnlyOffer) {
1769 FakeConstraints constraints;
1770 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
1771 true);
1772 CreatePeerConnection(&constraints);
1773 CreateAndSetRemoteOffer(kSdpStringWithStream1);
1774 CreateAnswerAsLocalDescription();
1775
1776 // At this point we should be receiving stream 1, but not sending anything.
1777 // A new offer should be recvonly.
kwiberg2bbff992016-03-16 11:03:04 -07001778 rtc::scoped_ptr<SessionDescriptionInterface> offer;
deadbeefc80741f2015-10-22 13:14:45 -07001779 DoCreateOffer(&offer, nullptr);
1780
1781 const cricket::ContentInfo* video_content =
1782 cricket::GetFirstVideoContent(offer->description());
1783 const cricket::VideoContentDescription* video_desc =
1784 static_cast<const cricket::VideoContentDescription*>(
1785 video_content->description);
1786 ASSERT_EQ(cricket::MD_RECVONLY, video_desc->direction());
1787
1788 const cricket::ContentInfo* audio_content =
1789 cricket::GetFirstAudioContent(offer->description());
1790 const cricket::AudioContentDescription* audio_desc =
1791 static_cast<const cricket::AudioContentDescription*>(
1792 audio_content->description);
1793 ASSERT_EQ(cricket::MD_RECVONLY, audio_desc->direction());
1794}
1795
1796// Test that if we're receiving (but not sending) a track, and the
1797// offerToReceiveVideo/offerToReceiveAudio constraints are explicitly set to
1798// false, the generated m-lines will be a=inactive.
1799TEST_F(PeerConnectionInterfaceTest, CreateSubsequentInactiveOffer) {
1800 FakeConstraints constraints;
1801 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
1802 true);
1803 CreatePeerConnection(&constraints);
1804 CreateAndSetRemoteOffer(kSdpStringWithStream1);
1805 CreateAnswerAsLocalDescription();
1806
1807 // At this point we should be receiving stream 1, but not sending anything.
1808 // A new offer would be recvonly, but we'll set the "no receive" constraints
1809 // to make it inactive.
kwiberg2bbff992016-03-16 11:03:04 -07001810 rtc::scoped_ptr<SessionDescriptionInterface> offer;
deadbeefc80741f2015-10-22 13:14:45 -07001811 FakeConstraints offer_constraints;
1812 offer_constraints.AddMandatory(
1813 webrtc::MediaConstraintsInterface::kOfferToReceiveVideo, false);
1814 offer_constraints.AddMandatory(
1815 webrtc::MediaConstraintsInterface::kOfferToReceiveAudio, false);
1816 DoCreateOffer(&offer, &offer_constraints);
1817
1818 const cricket::ContentInfo* video_content =
1819 cricket::GetFirstVideoContent(offer->description());
1820 const cricket::VideoContentDescription* video_desc =
1821 static_cast<const cricket::VideoContentDescription*>(
1822 video_content->description);
1823 ASSERT_EQ(cricket::MD_INACTIVE, video_desc->direction());
1824
1825 const cricket::ContentInfo* audio_content =
1826 cricket::GetFirstAudioContent(offer->description());
1827 const cricket::AudioContentDescription* audio_desc =
1828 static_cast<const cricket::AudioContentDescription*>(
1829 audio_content->description);
1830 ASSERT_EQ(cricket::MD_INACTIVE, audio_desc->direction());
1831}
1832
deadbeef653b8e02015-11-11 12:55:10 -08001833// Test that we can use SetConfiguration to change the ICE servers of the
1834// PortAllocator.
1835TEST_F(PeerConnectionInterfaceTest, SetConfigurationChangesIceServers) {
1836 CreatePeerConnection();
1837
1838 PeerConnectionInterface::RTCConfiguration config;
1839 PeerConnectionInterface::IceServer server;
1840 server.uri = "stun:test_hostname";
1841 config.servers.push_back(server);
1842 EXPECT_TRUE(pc_->SetConfiguration(config));
1843
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08001844 EXPECT_EQ(1u, port_allocator_->stun_servers().size());
1845 EXPECT_EQ("test_hostname",
1846 port_allocator_->stun_servers().begin()->hostname());
deadbeef653b8e02015-11-11 12:55:10 -08001847}
1848
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001849// Test that PeerConnection::Close changes the states to closed and all remote
1850// tracks change state to ended.
1851TEST_F(PeerConnectionInterfaceTest, CloseAndTestStreamsAndStates) {
1852 // Initialize a PeerConnection and negotiate local and remote session
1853 // description.
1854 InitiateCall();
1855 ASSERT_EQ(1u, pc_->local_streams()->count());
1856 ASSERT_EQ(1u, pc_->remote_streams()->count());
1857
1858 pc_->Close();
1859
1860 EXPECT_EQ(PeerConnectionInterface::kClosed, pc_->signaling_state());
1861 EXPECT_EQ(PeerConnectionInterface::kIceConnectionClosed,
1862 pc_->ice_connection_state());
1863 EXPECT_EQ(PeerConnectionInterface::kIceGatheringComplete,
1864 pc_->ice_gathering_state());
1865
1866 EXPECT_EQ(1u, pc_->local_streams()->count());
1867 EXPECT_EQ(1u, pc_->remote_streams()->count());
1868
1869 scoped_refptr<MediaStreamInterface> remote_stream =
1870 pc_->remote_streams()->at(0);
1871 EXPECT_EQ(MediaStreamTrackInterface::kEnded,
1872 remote_stream->GetVideoTracks()[0]->state());
perkjd61bf802016-03-24 03:16:19 -07001873 // Audio source state changes are posted.
1874 EXPECT_EQ_WAIT(MediaStreamTrackInterface::kEnded,
1875 remote_stream->GetAudioTracks()[0]->state(), 1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001876}
1877
1878// Test that PeerConnection methods fails gracefully after
1879// PeerConnection::Close has been called.
1880TEST_F(PeerConnectionInterfaceTest, CloseAndTestMethods) {
1881 CreatePeerConnection();
1882 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
1883 CreateOfferAsRemoteDescription();
1884 CreateAnswerAsLocalDescription();
1885
1886 ASSERT_EQ(1u, pc_->local_streams()->count());
1887 scoped_refptr<MediaStreamInterface> local_stream =
1888 pc_->local_streams()->at(0);
1889
1890 pc_->Close();
1891
1892 pc_->RemoveStream(local_stream);
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +00001893 EXPECT_FALSE(pc_->AddStream(local_stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001894
1895 ASSERT_FALSE(local_stream->GetAudioTracks().empty());
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001896 rtc::scoped_refptr<webrtc::DtmfSenderInterface> dtmf_sender(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001897 pc_->CreateDtmfSender(local_stream->GetAudioTracks()[0]));
wu@webrtc.org66037362013-08-13 00:09:35 +00001898 EXPECT_TRUE(NULL == dtmf_sender); // local stream has been removed.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001899
1900 EXPECT_TRUE(pc_->CreateDataChannel("test", NULL) == NULL);
1901
1902 EXPECT_TRUE(pc_->local_description() != NULL);
1903 EXPECT_TRUE(pc_->remote_description() != NULL);
1904
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001905 rtc::scoped_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001906 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001907 rtc::scoped_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -07001908 EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001909
1910 std::string sdp;
1911 ASSERT_TRUE(pc_->remote_description()->ToString(&sdp));
1912 SessionDescriptionInterface* remote_offer =
1913 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
1914 sdp, NULL);
1915 EXPECT_FALSE(DoSetRemoteDescription(remote_offer));
1916
1917 ASSERT_TRUE(pc_->local_description()->ToString(&sdp));
1918 SessionDescriptionInterface* local_offer =
1919 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
1920 sdp, NULL);
1921 EXPECT_FALSE(DoSetLocalDescription(local_offer));
1922}
1923
1924// Test that GetStats can still be called after PeerConnection::Close.
1925TEST_F(PeerConnectionInterfaceTest, CloseAndGetStats) {
1926 InitiateCall();
1927 pc_->Close();
1928 DoGetStats(NULL);
1929}
deadbeefab9b2d12015-10-14 11:33:11 -07001930
1931// NOTE: The series of tests below come from what used to be
1932// mediastreamsignaling_unittest.cc, and are mostly aimed at testing that
1933// setting a remote or local description has the expected effects.
1934
1935// This test verifies that the remote MediaStreams corresponding to a received
1936// SDP string is created. In this test the two separate MediaStreams are
1937// signaled.
1938TEST_F(PeerConnectionInterfaceTest, UpdateRemoteStreams) {
1939 FakeConstraints constraints;
1940 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
1941 true);
1942 CreatePeerConnection(&constraints);
1943 CreateAndSetRemoteOffer(kSdpStringWithStream1);
1944
1945 rtc::scoped_refptr<StreamCollection> reference(CreateStreamCollection(1));
1946 EXPECT_TRUE(
1947 CompareStreamCollections(observer_.remote_streams(), reference.get()));
1948 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
1949 EXPECT_TRUE(remote_stream->GetVideoTracks()[0]->GetSource() != nullptr);
1950
1951 // Create a session description based on another SDP with another
1952 // MediaStream.
1953 CreateAndSetRemoteOffer(kSdpStringWithStream1And2);
1954
1955 rtc::scoped_refptr<StreamCollection> reference2(CreateStreamCollection(2));
1956 EXPECT_TRUE(
1957 CompareStreamCollections(observer_.remote_streams(), reference2.get()));
1958}
1959
1960// This test verifies that when remote tracks are added/removed from SDP, the
1961// created remote streams are updated appropriately.
1962TEST_F(PeerConnectionInterfaceTest,
1963 AddRemoveTrackFromExistingRemoteMediaStream) {
1964 FakeConstraints constraints;
1965 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
1966 true);
1967 CreatePeerConnection(&constraints);
kwiberg2bbff992016-03-16 11:03:04 -07001968 rtc::scoped_ptr<SessionDescriptionInterface> desc_ms1 =
1969 CreateSessionDescriptionAndReference(1, 1);
deadbeefab9b2d12015-10-14 11:33:11 -07001970 EXPECT_TRUE(DoSetRemoteDescription(desc_ms1.release()));
1971 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
1972 reference_collection_));
1973
1974 // Add extra audio and video tracks to the same MediaStream.
kwiberg2bbff992016-03-16 11:03:04 -07001975 rtc::scoped_ptr<SessionDescriptionInterface> desc_ms1_two_tracks =
1976 CreateSessionDescriptionAndReference(2, 2);
deadbeefab9b2d12015-10-14 11:33:11 -07001977 EXPECT_TRUE(DoSetRemoteDescription(desc_ms1_two_tracks.release()));
1978 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
1979 reference_collection_));
perkjd61bf802016-03-24 03:16:19 -07001980 scoped_refptr<AudioTrackInterface> audio_track2 =
1981 observer_.remote_streams()->at(0)->GetAudioTracks()[1];
1982 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, audio_track2->state());
1983 scoped_refptr<VideoTrackInterface> video_track2 =
1984 observer_.remote_streams()->at(0)->GetVideoTracks()[1];
1985 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, video_track2->state());
deadbeefab9b2d12015-10-14 11:33:11 -07001986
1987 // Remove the extra audio and video tracks.
kwiberg2bbff992016-03-16 11:03:04 -07001988 rtc::scoped_ptr<SessionDescriptionInterface> desc_ms2 =
1989 CreateSessionDescriptionAndReference(1, 1);
perkjd61bf802016-03-24 03:16:19 -07001990 MockTrackObserver audio_track_observer(audio_track2);
1991 MockTrackObserver video_track_observer(video_track2);
1992
1993 EXPECT_CALL(audio_track_observer, OnChanged()).Times(Exactly(1));
1994 EXPECT_CALL(video_track_observer, OnChanged()).Times(Exactly(1));
deadbeefab9b2d12015-10-14 11:33:11 -07001995 EXPECT_TRUE(DoSetRemoteDescription(desc_ms2.release()));
1996 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
1997 reference_collection_));
perkjd61bf802016-03-24 03:16:19 -07001998 // Audio source state changes are posted.
1999 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
2000 audio_track2->state(), 1);
2001 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kEnded, video_track2->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002002}
2003
2004// This tests that remote tracks are ended if a local session description is set
2005// that rejects the media content type.
2006TEST_F(PeerConnectionInterfaceTest, RejectMediaContent) {
2007 FakeConstraints constraints;
2008 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2009 true);
2010 CreatePeerConnection(&constraints);
2011 // First create and set a remote offer, then reject its video content in our
2012 // answer.
2013 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2014 ASSERT_EQ(1u, observer_.remote_streams()->count());
2015 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2016 ASSERT_EQ(1u, remote_stream->GetVideoTracks().size());
2017 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2018
2019 rtc::scoped_refptr<webrtc::VideoTrackInterface> remote_video =
2020 remote_stream->GetVideoTracks()[0];
2021 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, remote_video->state());
2022 rtc::scoped_refptr<webrtc::AudioTrackInterface> remote_audio =
2023 remote_stream->GetAudioTracks()[0];
2024 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, remote_audio->state());
2025
2026 rtc::scoped_ptr<SessionDescriptionInterface> local_answer;
kwiberg2bbff992016-03-16 11:03:04 -07002027 EXPECT_TRUE(DoCreateAnswer(&local_answer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07002028 cricket::ContentInfo* video_info =
2029 local_answer->description()->GetContentByName("video");
2030 video_info->rejected = true;
2031 EXPECT_TRUE(DoSetLocalDescription(local_answer.release()));
2032 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kEnded, remote_video->state());
2033 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, remote_audio->state());
2034
2035 // Now create an offer where we reject both video and audio.
2036 rtc::scoped_ptr<SessionDescriptionInterface> local_offer;
kwiberg2bbff992016-03-16 11:03:04 -07002037 EXPECT_TRUE(DoCreateOffer(&local_offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07002038 video_info = local_offer->description()->GetContentByName("video");
2039 ASSERT_TRUE(video_info != nullptr);
2040 video_info->rejected = true;
2041 cricket::ContentInfo* audio_info =
2042 local_offer->description()->GetContentByName("audio");
2043 ASSERT_TRUE(audio_info != nullptr);
2044 audio_info->rejected = true;
2045 EXPECT_TRUE(DoSetLocalDescription(local_offer.release()));
2046 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kEnded, remote_video->state());
perkjd61bf802016-03-24 03:16:19 -07002047 // Audio source state changes are posted.
2048 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
2049 remote_audio->state(), 1);
deadbeefab9b2d12015-10-14 11:33:11 -07002050}
2051
2052// This tests that we won't crash if the remote track has been removed outside
2053// of PeerConnection and then PeerConnection tries to reject the track.
2054TEST_F(PeerConnectionInterfaceTest, RemoveTrackThenRejectMediaContent) {
2055 FakeConstraints constraints;
2056 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2057 true);
2058 CreatePeerConnection(&constraints);
2059 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2060 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2061 remote_stream->RemoveTrack(remote_stream->GetVideoTracks()[0]);
2062 remote_stream->RemoveTrack(remote_stream->GetAudioTracks()[0]);
2063
2064 rtc::scoped_ptr<SessionDescriptionInterface> local_answer(
2065 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
2066 kSdpStringWithStream1, nullptr));
2067 cricket::ContentInfo* video_info =
2068 local_answer->description()->GetContentByName("video");
2069 video_info->rejected = true;
2070 cricket::ContentInfo* audio_info =
2071 local_answer->description()->GetContentByName("audio");
2072 audio_info->rejected = true;
2073 EXPECT_TRUE(DoSetLocalDescription(local_answer.release()));
2074
2075 // No crash is a pass.
2076}
2077
deadbeef5e97fb52015-10-15 12:49:08 -07002078// This tests that if a recvonly remote description is set, no remote streams
2079// will be created, even if the description contains SSRCs/MSIDs.
2080// See: https://code.google.com/p/webrtc/issues/detail?id=5054
2081TEST_F(PeerConnectionInterfaceTest, RecvonlyDescriptionDoesntCreateStream) {
2082 FakeConstraints constraints;
2083 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2084 true);
2085 CreatePeerConnection(&constraints);
2086
2087 std::string recvonly_offer = kSdpStringWithStream1;
2088 rtc::replace_substrs(kSendrecv, strlen(kSendrecv), kRecvonly,
2089 strlen(kRecvonly), &recvonly_offer);
2090 CreateAndSetRemoteOffer(recvonly_offer);
2091
2092 EXPECT_EQ(0u, observer_.remote_streams()->count());
2093}
2094
Stefan Holmer55d6e7c2016-03-17 16:26:40 +01002095// Disabled on Win dbg: https://bugs.chromium.org/p/webrtc/issues/detail?id=5659
Stefan Holmer102362b2016-03-18 09:39:07 +01002096#if defined(WEBRTC_WIN) && defined(_DEBUG)
Stefan Holmer55d6e7c2016-03-17 16:26:40 +01002097#define MAYBE_SdpWithoutMsidCreatesDefaultStream \
2098 DISABLED_SdpWithoutMsidCreatesDefaultStream
2099#else
2100#define MAYBE_SdpWithoutMsidCreatesDefaultStream \
2101 SdpWithoutMsidCreatesDefaultStream
2102#endif
deadbeefab9b2d12015-10-14 11:33:11 -07002103// This tests that a default MediaStream is created if a remote session
2104// description doesn't contain any streams and no MSID support.
2105// It also tests that the default stream is updated if a video m-line is added
2106// in a subsequent session description.
Stefan Holmer102362b2016-03-18 09:39:07 +01002107TEST_F(PeerConnectionInterfaceTest, MAYBE_SdpWithoutMsidCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002108 FakeConstraints constraints;
2109 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2110 true);
2111 CreatePeerConnection(&constraints);
2112 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2113
2114 ASSERT_EQ(1u, observer_.remote_streams()->count());
2115 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2116
2117 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2118 EXPECT_EQ(0u, remote_stream->GetVideoTracks().size());
2119 EXPECT_EQ("default", remote_stream->label());
2120
2121 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2122 ASSERT_EQ(1u, observer_.remote_streams()->count());
2123 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2124 EXPECT_EQ("defaulta0", remote_stream->GetAudioTracks()[0]->id());
deadbeef884f5852016-01-15 09:20:04 -08002125 EXPECT_EQ(MediaStreamTrackInterface::kLive,
2126 remote_stream->GetAudioTracks()[0]->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002127 ASSERT_EQ(1u, remote_stream->GetVideoTracks().size());
2128 EXPECT_EQ("defaultv0", remote_stream->GetVideoTracks()[0]->id());
deadbeef884f5852016-01-15 09:20:04 -08002129 EXPECT_EQ(MediaStreamTrackInterface::kLive,
2130 remote_stream->GetVideoTracks()[0]->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002131}
2132
Stefan Holmer55d6e7c2016-03-17 16:26:40 +01002133// Disabled on Win dbg: https://bugs.chromium.org/p/webrtc/issues/detail?id=5659
Stefan Holmer102362b2016-03-18 09:39:07 +01002134#if defined(WEBRTC_WIN) && defined(_DEBUG)
Stefan Holmer55d6e7c2016-03-17 16:26:40 +01002135#define MAYBE_SendOnlySdpWithoutMsidCreatesDefaultStream \
2136 DISABLED_SendOnlySdpWithoutMsidCreatesDefaultStream
2137#else
2138#define MAYBE_SendOnlySdpWithoutMsidCreatesDefaultStream \
2139 SendOnlySdpWithoutMsidCreatesDefaultStream
2140#endif
deadbeefab9b2d12015-10-14 11:33:11 -07002141// This tests that a default MediaStream is created if a remote session
2142// description doesn't contain any streams and media direction is send only.
2143TEST_F(PeerConnectionInterfaceTest,
Stefan Holmer55d6e7c2016-03-17 16:26:40 +01002144 MAYBE_SendOnlySdpWithoutMsidCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002145 FakeConstraints constraints;
2146 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2147 true);
2148 CreatePeerConnection(&constraints);
2149 CreateAndSetRemoteOffer(kSdpStringSendOnlyWithoutStreams);
2150
2151 ASSERT_EQ(1u, observer_.remote_streams()->count());
2152 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2153
2154 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2155 EXPECT_EQ(1u, remote_stream->GetVideoTracks().size());
2156 EXPECT_EQ("default", remote_stream->label());
2157}
2158
2159// This tests that it won't crash when PeerConnection tries to remove
2160// a remote track that as already been removed from the MediaStream.
2161TEST_F(PeerConnectionInterfaceTest, RemoveAlreadyGoneRemoteStream) {
2162 FakeConstraints constraints;
2163 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2164 true);
2165 CreatePeerConnection(&constraints);
2166 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2167 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2168 remote_stream->RemoveTrack(remote_stream->GetAudioTracks()[0]);
2169 remote_stream->RemoveTrack(remote_stream->GetVideoTracks()[0]);
2170
2171 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2172
2173 // No crash is a pass.
2174}
2175
Stefan Holmer55d6e7c2016-03-17 16:26:40 +01002176// Disabled on Win dbg: https://bugs.chromium.org/p/webrtc/issues/detail?id=5659
Stefan Holmer102362b2016-03-18 09:39:07 +01002177#if defined(WEBRTC_WIN) && defined(_DEBUG)
Stefan Holmer55d6e7c2016-03-17 16:26:40 +01002178#define MAYBE_SdpWithoutMsidAndStreamsCreatesDefaultStream \
2179 DISABLED_SdpWithoutMsidAndStreamsCreatesDefaultStream
2180#else
2181#define MAYBE_SdpWithoutMsidAndStreamsCreatesDefaultStream \
2182 SdpWithoutMsidAndStreamsCreatesDefaultStream
2183#endif
deadbeefab9b2d12015-10-14 11:33:11 -07002184// This tests that a default MediaStream is created if the remote session
2185// description doesn't contain any streams and don't contain an indication if
2186// MSID is supported.
2187TEST_F(PeerConnectionInterfaceTest,
Stefan Holmer55d6e7c2016-03-17 16:26:40 +01002188 MAYBE_SdpWithoutMsidAndStreamsCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002189 FakeConstraints constraints;
2190 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2191 true);
2192 CreatePeerConnection(&constraints);
2193 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2194
2195 ASSERT_EQ(1u, observer_.remote_streams()->count());
2196 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2197 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2198 EXPECT_EQ(1u, remote_stream->GetVideoTracks().size());
2199}
2200
Stefan Holmer55d6e7c2016-03-17 16:26:40 +01002201// Disabled on Win dbg: https://bugs.chromium.org/p/webrtc/issues/detail?id=5659
Stefan Holmer102362b2016-03-18 09:39:07 +01002202#if defined(WEBRTC_WIN) && defined(_DEBUG)
Stefan Holmer55d6e7c2016-03-17 16:26:40 +01002203#define MAYBE_SdpWithMsidDontCreatesDefaultStream \
2204 DISABLED_SdpWithMsidDontCreatesDefaultStream
2205#else
2206#define MAYBE_SdpWithMsidDontCreatesDefaultStream \
2207 SdpWithMsidDontCreatesDefaultStream
2208#endif
deadbeefab9b2d12015-10-14 11:33:11 -07002209// This tests that a default MediaStream is not created if the remote session
2210// description doesn't contain any streams but does support MSID.
Stefan Holmer55d6e7c2016-03-17 16:26:40 +01002211TEST_F(PeerConnectionInterfaceTest, MAYBE_SdpWithMsidDontCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002212 FakeConstraints constraints;
2213 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2214 true);
2215 CreatePeerConnection(&constraints);
2216 CreateAndSetRemoteOffer(kSdpStringWithMsidWithoutStreams);
2217 EXPECT_EQ(0u, observer_.remote_streams()->count());
2218}
2219
Stefan Holmer55d6e7c2016-03-17 16:26:40 +01002220// Disabled on Win dbg: https://bugs.chromium.org/p/webrtc/issues/detail?id=5659
Stefan Holmer102362b2016-03-18 09:39:07 +01002221#if defined(WEBRTC_WIN) && defined(_DEBUG)
Stefan Holmer55d6e7c2016-03-17 16:26:40 +01002222#define MAYBE_DefaultTracksNotDestroyedAndRecreated \
2223 DISABLED_DefaultTracksNotDestroyedAndRecreated
2224#else
2225#define MAYBE_DefaultTracksNotDestroyedAndRecreated \
2226 DefaultTracksNotDestroyedAndRecreated
2227#endif
deadbeefbda7e0b2015-12-08 17:13:40 -08002228// This tests that when setting a new description, the old default tracks are
2229// not destroyed and recreated.
2230// See: https://bugs.chromium.org/p/webrtc/issues/detail?id=5250
Stefan Holmer102362b2016-03-18 09:39:07 +01002231TEST_F(PeerConnectionInterfaceTest,
2232 MAYBE_DefaultTracksNotDestroyedAndRecreated) {
deadbeefbda7e0b2015-12-08 17:13:40 -08002233 FakeConstraints constraints;
2234 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2235 true);
2236 CreatePeerConnection(&constraints);
2237 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2238
2239 ASSERT_EQ(1u, observer_.remote_streams()->count());
2240 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2241 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2242
2243 // Set the track to "disabled", then set a new description and ensure the
2244 // track is still disabled, which ensures it hasn't been recreated.
2245 remote_stream->GetAudioTracks()[0]->set_enabled(false);
2246 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2247 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2248 EXPECT_FALSE(remote_stream->GetAudioTracks()[0]->enabled());
2249}
2250
deadbeefab9b2d12015-10-14 11:33:11 -07002251// This tests that a default MediaStream is not created if a remote session
2252// description is updated to not have any MediaStreams.
2253TEST_F(PeerConnectionInterfaceTest, VerifyDefaultStreamIsNotCreated) {
2254 FakeConstraints constraints;
2255 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2256 true);
2257 CreatePeerConnection(&constraints);
2258 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2259 rtc::scoped_refptr<StreamCollection> reference(CreateStreamCollection(1));
2260 EXPECT_TRUE(
2261 CompareStreamCollections(observer_.remote_streams(), reference.get()));
2262
2263 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2264 EXPECT_EQ(0u, observer_.remote_streams()->count());
2265}
2266
Stefan Holmer55d6e7c2016-03-17 16:26:40 +01002267// Disabled on Win dbg: https://bugs.chromium.org/p/webrtc/issues/detail?id=5659
Stefan Holmer102362b2016-03-18 09:39:07 +01002268#if defined(WEBRTC_WIN) && defined(_DEBUG)
Stefan Holmer55d6e7c2016-03-17 16:26:40 +01002269#define MAYBE_LocalDescriptionChanged DISABLED_LocalDescriptionChanged
2270#else
2271#define MAYBE_LocalDescriptionChanged LocalDescriptionChanged
2272#endif
deadbeefab9b2d12015-10-14 11:33:11 -07002273// This tests that an RtpSender is created when the local description is set
2274// after adding a local stream.
2275// TODO(deadbeef): This test and the one below it need to be updated when
2276// an RtpSender's lifetime isn't determined by when a local description is set.
Stefan Holmer55d6e7c2016-03-17 16:26:40 +01002277TEST_F(PeerConnectionInterfaceTest, MAYBE_LocalDescriptionChanged) {
deadbeefab9b2d12015-10-14 11:33:11 -07002278 FakeConstraints constraints;
2279 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2280 true);
2281 CreatePeerConnection(&constraints);
2282 // Create an offer just to ensure we have an identity before we manually
2283 // call SetLocalDescription.
2284 rtc::scoped_ptr<SessionDescriptionInterface> throwaway;
kwiberg2bbff992016-03-16 11:03:04 -07002285 ASSERT_TRUE(DoCreateOffer(&throwaway, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07002286
kwiberg2bbff992016-03-16 11:03:04 -07002287 rtc::scoped_ptr<SessionDescriptionInterface> desc_1 =
2288 CreateSessionDescriptionAndReference(2, 2);
deadbeefab9b2d12015-10-14 11:33:11 -07002289
2290 pc_->AddStream(reference_collection_->at(0));
2291 EXPECT_TRUE(DoSetLocalDescription(desc_1.release()));
2292 auto senders = pc_->GetSenders();
2293 EXPECT_EQ(4u, senders.size());
2294 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2295 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2296 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[1]));
2297 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[1]));
2298
2299 // Remove an audio and video track.
deadbeeffac06552015-11-25 11:26:01 -08002300 pc_->RemoveStream(reference_collection_->at(0));
kwiberg2bbff992016-03-16 11:03:04 -07002301 rtc::scoped_ptr<SessionDescriptionInterface> desc_2 =
2302 CreateSessionDescriptionAndReference(1, 1);
deadbeeffac06552015-11-25 11:26:01 -08002303 pc_->AddStream(reference_collection_->at(0));
deadbeefab9b2d12015-10-14 11:33:11 -07002304 EXPECT_TRUE(DoSetLocalDescription(desc_2.release()));
2305 senders = pc_->GetSenders();
2306 EXPECT_EQ(2u, senders.size());
2307 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2308 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2309 EXPECT_FALSE(ContainsSender(senders, kAudioTracks[1]));
2310 EXPECT_FALSE(ContainsSender(senders, kVideoTracks[1]));
2311}
2312
Stefan Holmer55d6e7c2016-03-17 16:26:40 +01002313// Disabled on Win dbg: https://bugs.chromium.org/p/webrtc/issues/detail?id=5659
Stefan Holmer102362b2016-03-18 09:39:07 +01002314#if defined(WEBRTC_WIN) && defined(_DEBUG)
Stefan Holmer55d6e7c2016-03-17 16:26:40 +01002315#define MAYBE_AddLocalStreamAfterLocalDescriptionChanged \
2316 DISABLED_AddLocalStreamAfterLocalDescriptionChanged
2317#else
2318#define MAYBE_AddLocalStreamAfterLocalDescriptionChanged \
2319 AddLocalStreamAfterLocalDescriptionChanged
2320#endif
deadbeefab9b2d12015-10-14 11:33:11 -07002321// This tests that an RtpSender is created when the local description is set
2322// before adding a local stream.
2323TEST_F(PeerConnectionInterfaceTest,
Stefan Holmer55d6e7c2016-03-17 16:26:40 +01002324 MAYBE_AddLocalStreamAfterLocalDescriptionChanged) {
deadbeefab9b2d12015-10-14 11:33:11 -07002325 FakeConstraints constraints;
2326 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2327 true);
2328 CreatePeerConnection(&constraints);
2329 // Create an offer just to ensure we have an identity before we manually
2330 // call SetLocalDescription.
2331 rtc::scoped_ptr<SessionDescriptionInterface> throwaway;
kwiberg2bbff992016-03-16 11:03:04 -07002332 ASSERT_TRUE(DoCreateOffer(&throwaway, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07002333
kwiberg2bbff992016-03-16 11:03:04 -07002334 rtc::scoped_ptr<SessionDescriptionInterface> desc_1 =
2335 CreateSessionDescriptionAndReference(2, 2);
deadbeefab9b2d12015-10-14 11:33:11 -07002336
2337 EXPECT_TRUE(DoSetLocalDescription(desc_1.release()));
2338 auto senders = pc_->GetSenders();
2339 EXPECT_EQ(0u, senders.size());
2340
2341 pc_->AddStream(reference_collection_->at(0));
2342 senders = pc_->GetSenders();
2343 EXPECT_EQ(4u, senders.size());
2344 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2345 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2346 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[1]));
2347 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[1]));
2348}
2349
Stefan Holmer55d6e7c2016-03-17 16:26:40 +01002350// Disabled on Win dbg: https://bugs.chromium.org/p/webrtc/issues/detail?id=5659
Stefan Holmer102362b2016-03-18 09:39:07 +01002351#if defined(WEBRTC_WIN) && defined(_DEBUG)
Stefan Holmer55d6e7c2016-03-17 16:26:40 +01002352#define MAYBE_ChangeSsrcOnTrackInLocalSessionDescription \
2353 DISABLED_ChangeSsrcOnTrackInLocalSessionDescription
2354#else
2355#define MAYBE_ChangeSsrcOnTrackInLocalSessionDescription \
2356 ChangeSsrcOnTrackInLocalSessionDescription
2357#endif
deadbeefab9b2d12015-10-14 11:33:11 -07002358// This tests that the expected behavior occurs if the SSRC on a local track is
2359// changed when SetLocalDescription is called.
2360TEST_F(PeerConnectionInterfaceTest,
Stefan Holmer55d6e7c2016-03-17 16:26:40 +01002361 MAYBE_ChangeSsrcOnTrackInLocalSessionDescription) {
deadbeefab9b2d12015-10-14 11:33:11 -07002362 FakeConstraints constraints;
2363 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2364 true);
2365 CreatePeerConnection(&constraints);
2366 // Create an offer just to ensure we have an identity before we manually
2367 // call SetLocalDescription.
2368 rtc::scoped_ptr<SessionDescriptionInterface> throwaway;
kwiberg2bbff992016-03-16 11:03:04 -07002369 ASSERT_TRUE(DoCreateOffer(&throwaway, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07002370
kwiberg2bbff992016-03-16 11:03:04 -07002371 rtc::scoped_ptr<SessionDescriptionInterface> desc =
2372 CreateSessionDescriptionAndReference(1, 1);
deadbeefab9b2d12015-10-14 11:33:11 -07002373 std::string sdp;
2374 desc->ToString(&sdp);
2375
2376 pc_->AddStream(reference_collection_->at(0));
2377 EXPECT_TRUE(DoSetLocalDescription(desc.release()));
2378 auto senders = pc_->GetSenders();
2379 EXPECT_EQ(2u, senders.size());
2380 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2381 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2382
2383 // Change the ssrc of the audio and video track.
2384 std::string ssrc_org = "a=ssrc:1";
2385 std::string ssrc_to = "a=ssrc:97";
2386 rtc::replace_substrs(ssrc_org.c_str(), ssrc_org.length(), ssrc_to.c_str(),
2387 ssrc_to.length(), &sdp);
2388 ssrc_org = "a=ssrc:2";
2389 ssrc_to = "a=ssrc:98";
2390 rtc::replace_substrs(ssrc_org.c_str(), ssrc_org.length(), ssrc_to.c_str(),
2391 ssrc_to.length(), &sdp);
2392 rtc::scoped_ptr<SessionDescriptionInterface> updated_desc(
2393 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer, sdp,
2394 nullptr));
2395
2396 EXPECT_TRUE(DoSetLocalDescription(updated_desc.release()));
2397 senders = pc_->GetSenders();
2398 EXPECT_EQ(2u, senders.size());
2399 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2400 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2401 // TODO(deadbeef): Once RtpSenders expose parameters, check that the SSRC
2402 // changed.
2403}
2404
Stefan Holmer55d6e7c2016-03-17 16:26:40 +01002405// Disabled on Win dbg: https://bugs.chromium.org/p/webrtc/issues/detail?id=5659
Stefan Holmer102362b2016-03-18 09:39:07 +01002406#if defined(WEBRTC_WIN) && defined(_DEBUG)
Stefan Holmer55d6e7c2016-03-17 16:26:40 +01002407#define MAYBE_SignalSameTracksInSeparateMediaStream \
2408 DISABLED_SignalSameTracksInSeparateMediaStream
2409#else
2410#define MAYBE_SignalSameTracksInSeparateMediaStream \
2411 SignalSameTracksInSeparateMediaStream
2412#endif
deadbeefab9b2d12015-10-14 11:33:11 -07002413// This tests that the expected behavior occurs if a new session description is
2414// set with the same tracks, but on a different MediaStream.
Stefan Holmer55d6e7c2016-03-17 16:26:40 +01002415TEST_F(PeerConnectionInterfaceTest,
2416 MAYBE_SignalSameTracksInSeparateMediaStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002417 FakeConstraints constraints;
2418 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2419 true);
2420 CreatePeerConnection(&constraints);
2421 // Create an offer just to ensure we have an identity before we manually
2422 // call SetLocalDescription.
2423 rtc::scoped_ptr<SessionDescriptionInterface> throwaway;
kwiberg2bbff992016-03-16 11:03:04 -07002424 ASSERT_TRUE(DoCreateOffer(&throwaway, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07002425
kwiberg2bbff992016-03-16 11:03:04 -07002426 rtc::scoped_ptr<SessionDescriptionInterface> desc =
2427 CreateSessionDescriptionAndReference(1, 1);
deadbeefab9b2d12015-10-14 11:33:11 -07002428 std::string sdp;
2429 desc->ToString(&sdp);
2430
2431 pc_->AddStream(reference_collection_->at(0));
2432 EXPECT_TRUE(DoSetLocalDescription(desc.release()));
2433 auto senders = pc_->GetSenders();
2434 EXPECT_EQ(2u, senders.size());
2435 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2436 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2437
2438 // Add a new MediaStream but with the same tracks as in the first stream.
2439 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream_1(
2440 webrtc::MediaStream::Create(kStreams[1]));
2441 stream_1->AddTrack(reference_collection_->at(0)->GetVideoTracks()[0]);
2442 stream_1->AddTrack(reference_collection_->at(0)->GetAudioTracks()[0]);
2443 pc_->AddStream(stream_1);
2444
2445 // Replace msid in the original SDP.
2446 rtc::replace_substrs(kStreams[0], strlen(kStreams[0]), kStreams[1],
2447 strlen(kStreams[1]), &sdp);
2448
2449 rtc::scoped_ptr<SessionDescriptionInterface> updated_desc(
2450 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer, sdp,
2451 nullptr));
2452
2453 EXPECT_TRUE(DoSetLocalDescription(updated_desc.release()));
2454 senders = pc_->GetSenders();
2455 EXPECT_EQ(2u, senders.size());
2456 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2457 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2458}
2459
nisse51542be2016-02-12 02:27:06 -08002460// The PeerConnectionMediaConfig tests below verify that configuration
2461// and constraints are propagated into the MediaConfig passed to
2462// CreateMediaController. These settings are intended for MediaChannel
2463// constructors, but that is not exercised by these unittest.
2464class PeerConnectionFactoryForTest : public webrtc::PeerConnectionFactory {
2465 public:
2466 webrtc::MediaControllerInterface* CreateMediaController(
2467 const cricket::MediaConfig& config) const override {
2468 create_media_controller_called_ = true;
2469 create_media_controller_config_ = config;
2470
2471 webrtc::MediaControllerInterface* mc =
2472 PeerConnectionFactory::CreateMediaController(config);
2473 EXPECT_TRUE(mc != nullptr);
2474 return mc;
2475 }
2476
2477 // Mutable, so they can be modified in the above const-declared method.
2478 mutable bool create_media_controller_called_ = false;
2479 mutable cricket::MediaConfig create_media_controller_config_;
2480};
2481
2482class PeerConnectionMediaConfigTest : public testing::Test {
2483 protected:
2484 void SetUp() override {
nisseaf510af2016-03-21 08:20:42 -07002485 pcf_ = new rtc::RefCountedObject<PeerConnectionFactoryForTest>();
nisse51542be2016-02-12 02:27:06 -08002486 pcf_->Initialize();
2487 }
2488 const cricket::MediaConfig& TestCreatePeerConnection(
2489 const PeerConnectionInterface::RTCConfiguration& config,
2490 const MediaConstraintsInterface *constraints) {
2491 pcf_->create_media_controller_called_ = false;
2492
2493 scoped_refptr<PeerConnectionInterface> pc(
2494 pcf_->CreatePeerConnection(config, constraints, nullptr, nullptr,
2495 &observer_));
2496 EXPECT_TRUE(pc.get());
2497 EXPECT_TRUE(pcf_->create_media_controller_called_);
2498 return pcf_->create_media_controller_config_;
2499 }
2500
2501 scoped_refptr<PeerConnectionFactoryForTest> pcf_;
2502 MockPeerConnectionObserver observer_;
2503};
2504
2505// This test verifies the default behaviour with no constraints and a
2506// default RTCConfiguration.
2507TEST_F(PeerConnectionMediaConfigTest, TestDefaults) {
2508 PeerConnectionInterface::RTCConfiguration config;
2509 FakeConstraints constraints;
2510
2511 const cricket::MediaConfig& media_config =
2512 TestCreatePeerConnection(config, &constraints);
2513
2514 EXPECT_FALSE(media_config.enable_dscp);
nisse0db023a2016-03-01 04:29:59 -08002515 EXPECT_TRUE(media_config.video.enable_cpu_overuse_detection);
2516 EXPECT_FALSE(media_config.video.disable_prerenderer_smoothing);
2517 EXPECT_FALSE(media_config.video.suspend_below_min_bitrate);
nisse51542be2016-02-12 02:27:06 -08002518}
2519
2520// This test verifies the DSCP constraint is recognized and passed to
2521// the CreateMediaController call.
2522TEST_F(PeerConnectionMediaConfigTest, TestDscpConstraintTrue) {
2523 PeerConnectionInterface::RTCConfiguration config;
2524 FakeConstraints constraints;
2525
2526 constraints.AddOptional(webrtc::MediaConstraintsInterface::kEnableDscp, true);
2527 const cricket::MediaConfig& media_config =
2528 TestCreatePeerConnection(config, &constraints);
2529
2530 EXPECT_TRUE(media_config.enable_dscp);
2531}
2532
2533// This test verifies the cpu overuse detection constraint is
2534// recognized and passed to the CreateMediaController call.
2535TEST_F(PeerConnectionMediaConfigTest, TestCpuOveruseConstraintFalse) {
2536 PeerConnectionInterface::RTCConfiguration config;
2537 FakeConstraints constraints;
2538
2539 constraints.AddOptional(
2540 webrtc::MediaConstraintsInterface::kCpuOveruseDetection, false);
2541 const cricket::MediaConfig media_config =
2542 TestCreatePeerConnection(config, &constraints);
2543
nisse0db023a2016-03-01 04:29:59 -08002544 EXPECT_FALSE(media_config.video.enable_cpu_overuse_detection);
nisse51542be2016-02-12 02:27:06 -08002545}
2546
2547// This test verifies that the disable_prerenderer_smoothing flag is
2548// propagated from RTCConfiguration to the CreateMediaController call.
2549TEST_F(PeerConnectionMediaConfigTest, TestDisablePrerendererSmoothingTrue) {
2550 PeerConnectionInterface::RTCConfiguration config;
2551 FakeConstraints constraints;
2552
2553 config.disable_prerenderer_smoothing = true;
2554 const cricket::MediaConfig& media_config =
2555 TestCreatePeerConnection(config, &constraints);
2556
nisse0db023a2016-03-01 04:29:59 -08002557 EXPECT_TRUE(media_config.video.disable_prerenderer_smoothing);
2558}
2559
2560// This test verifies the suspend below min bitrate constraint is
2561// recognized and passed to the CreateMediaController call.
2562TEST_F(PeerConnectionMediaConfigTest,
2563 TestSuspendBelowMinBitrateConstraintTrue) {
2564 PeerConnectionInterface::RTCConfiguration config;
2565 FakeConstraints constraints;
2566
2567 constraints.AddOptional(
2568 webrtc::MediaConstraintsInterface::kEnableVideoSuspendBelowMinBitrate,
2569 true);
2570 const cricket::MediaConfig media_config =
2571 TestCreatePeerConnection(config, &constraints);
2572
2573 EXPECT_TRUE(media_config.video.suspend_below_min_bitrate);
nisse51542be2016-02-12 02:27:06 -08002574}
2575
deadbeefab9b2d12015-10-14 11:33:11 -07002576// The following tests verify that session options are created correctly.
deadbeefc80741f2015-10-22 13:14:45 -07002577// TODO(deadbeef): Convert these tests to be more end-to-end. Instead of
2578// "verify options are converted correctly", should be "pass options into
2579// CreateOffer and verify the correct offer is produced."
deadbeefab9b2d12015-10-14 11:33:11 -07002580
2581TEST(CreateSessionOptionsTest, GetOptionsForOfferWithInvalidAudioOption) {
2582 RTCOfferAnswerOptions rtc_options;
2583 rtc_options.offer_to_receive_audio = RTCOfferAnswerOptions::kUndefined - 1;
2584
2585 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08002586 EXPECT_FALSE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002587
2588 rtc_options.offer_to_receive_audio =
2589 RTCOfferAnswerOptions::kMaxOfferToReceiveMedia + 1;
htaaac2dea2016-03-10 13:35:55 -08002590 EXPECT_FALSE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002591}
2592
2593TEST(CreateSessionOptionsTest, GetOptionsForOfferWithInvalidVideoOption) {
2594 RTCOfferAnswerOptions rtc_options;
2595 rtc_options.offer_to_receive_video = RTCOfferAnswerOptions::kUndefined - 1;
2596
2597 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08002598 EXPECT_FALSE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002599
2600 rtc_options.offer_to_receive_video =
2601 RTCOfferAnswerOptions::kMaxOfferToReceiveMedia + 1;
htaaac2dea2016-03-10 13:35:55 -08002602 EXPECT_FALSE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002603}
2604
2605// Test that a MediaSessionOptions is created for an offer if
deadbeefc80741f2015-10-22 13:14:45 -07002606// OfferToReceiveAudio and OfferToReceiveVideo options are set.
deadbeefab9b2d12015-10-14 11:33:11 -07002607TEST(CreateSessionOptionsTest, GetMediaSessionOptionsForOfferWithAudioVideo) {
2608 RTCOfferAnswerOptions rtc_options;
2609 rtc_options.offer_to_receive_audio = 1;
2610 rtc_options.offer_to_receive_video = 1;
2611
2612 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08002613 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002614 EXPECT_TRUE(options.has_audio());
2615 EXPECT_TRUE(options.has_video());
2616 EXPECT_TRUE(options.bundle_enabled);
2617}
2618
2619// Test that a correct MediaSessionOptions is created for an offer if
deadbeefc80741f2015-10-22 13:14:45 -07002620// OfferToReceiveAudio is set.
deadbeefab9b2d12015-10-14 11:33:11 -07002621TEST(CreateSessionOptionsTest, GetMediaSessionOptionsForOfferWithAudio) {
2622 RTCOfferAnswerOptions rtc_options;
2623 rtc_options.offer_to_receive_audio = 1;
2624
2625 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08002626 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002627 EXPECT_TRUE(options.has_audio());
2628 EXPECT_FALSE(options.has_video());
2629 EXPECT_TRUE(options.bundle_enabled);
2630}
2631
2632// Test that a correct MediaSessionOptions is created for an offer if
deadbeefc80741f2015-10-22 13:14:45 -07002633// the default OfferOptions are used.
deadbeefab9b2d12015-10-14 11:33:11 -07002634TEST(CreateSessionOptionsTest, GetDefaultMediaSessionOptionsForOffer) {
2635 RTCOfferAnswerOptions rtc_options;
2636
2637 cricket::MediaSessionOptions options;
deadbeef0ed85b22016-02-23 17:24:52 -08002638 options.transport_options["audio"] = cricket::TransportOptions();
2639 options.transport_options["video"] = cricket::TransportOptions();
htaaac2dea2016-03-10 13:35:55 -08002640 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefc80741f2015-10-22 13:14:45 -07002641 EXPECT_TRUE(options.has_audio());
deadbeefab9b2d12015-10-14 11:33:11 -07002642 EXPECT_FALSE(options.has_video());
deadbeefc80741f2015-10-22 13:14:45 -07002643 EXPECT_TRUE(options.bundle_enabled);
deadbeefab9b2d12015-10-14 11:33:11 -07002644 EXPECT_TRUE(options.vad_enabled);
deadbeef0ed85b22016-02-23 17:24:52 -08002645 EXPECT_FALSE(options.transport_options["audio"].ice_restart);
2646 EXPECT_FALSE(options.transport_options["video"].ice_restart);
deadbeefab9b2d12015-10-14 11:33:11 -07002647}
2648
2649// Test that a correct MediaSessionOptions is created for an offer if
deadbeefc80741f2015-10-22 13:14:45 -07002650// OfferToReceiveVideo is set.
deadbeefab9b2d12015-10-14 11:33:11 -07002651TEST(CreateSessionOptionsTest, GetMediaSessionOptionsForOfferWithVideo) {
2652 RTCOfferAnswerOptions rtc_options;
2653 rtc_options.offer_to_receive_audio = 0;
2654 rtc_options.offer_to_receive_video = 1;
2655
2656 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08002657 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002658 EXPECT_FALSE(options.has_audio());
2659 EXPECT_TRUE(options.has_video());
2660 EXPECT_TRUE(options.bundle_enabled);
2661}
2662
2663// Test that a correct MediaSessionOptions is created for an offer if
2664// UseRtpMux is set to false.
2665TEST(CreateSessionOptionsTest,
2666 GetMediaSessionOptionsForOfferWithBundleDisabled) {
2667 RTCOfferAnswerOptions rtc_options;
2668 rtc_options.offer_to_receive_audio = 1;
2669 rtc_options.offer_to_receive_video = 1;
2670 rtc_options.use_rtp_mux = false;
2671
2672 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08002673 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002674 EXPECT_TRUE(options.has_audio());
2675 EXPECT_TRUE(options.has_video());
2676 EXPECT_FALSE(options.bundle_enabled);
2677}
2678
2679// Test that a correct MediaSessionOptions is created to restart ice if
2680// IceRestart is set. It also tests that subsequent MediaSessionOptions don't
Taylor Brandstetterf475d362016-01-08 15:35:57 -08002681// have |audio_transport_options.ice_restart| etc. set.
deadbeefab9b2d12015-10-14 11:33:11 -07002682TEST(CreateSessionOptionsTest, GetMediaSessionOptionsForOfferWithIceRestart) {
2683 RTCOfferAnswerOptions rtc_options;
2684 rtc_options.ice_restart = true;
2685
2686 cricket::MediaSessionOptions options;
deadbeef0ed85b22016-02-23 17:24:52 -08002687 options.transport_options["audio"] = cricket::TransportOptions();
2688 options.transport_options["video"] = cricket::TransportOptions();
htaaac2dea2016-03-10 13:35:55 -08002689 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeef0ed85b22016-02-23 17:24:52 -08002690 EXPECT_TRUE(options.transport_options["audio"].ice_restart);
2691 EXPECT_TRUE(options.transport_options["video"].ice_restart);
deadbeefab9b2d12015-10-14 11:33:11 -07002692
2693 rtc_options = RTCOfferAnswerOptions();
htaaac2dea2016-03-10 13:35:55 -08002694 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeef0ed85b22016-02-23 17:24:52 -08002695 EXPECT_FALSE(options.transport_options["audio"].ice_restart);
2696 EXPECT_FALSE(options.transport_options["video"].ice_restart);
deadbeefab9b2d12015-10-14 11:33:11 -07002697}
2698
2699// Test that the MediaConstraints in an answer don't affect if audio and video
2700// is offered in an offer but that if kOfferToReceiveAudio or
2701// kOfferToReceiveVideo constraints are true in an offer, the media type will be
2702// included in subsequent answers.
2703TEST(CreateSessionOptionsTest, MediaConstraintsInAnswer) {
2704 FakeConstraints answer_c;
2705 answer_c.SetMandatoryReceiveAudio(true);
2706 answer_c.SetMandatoryReceiveVideo(true);
2707
2708 cricket::MediaSessionOptions answer_options;
2709 EXPECT_TRUE(ParseConstraintsForAnswer(&answer_c, &answer_options));
2710 EXPECT_TRUE(answer_options.has_audio());
2711 EXPECT_TRUE(answer_options.has_video());
2712
deadbeefc80741f2015-10-22 13:14:45 -07002713 RTCOfferAnswerOptions rtc_offer_options;
deadbeefab9b2d12015-10-14 11:33:11 -07002714
2715 cricket::MediaSessionOptions offer_options;
htaaac2dea2016-03-10 13:35:55 -08002716 EXPECT_TRUE(
2717 ExtractMediaSessionOptions(rtc_offer_options, false, &offer_options));
deadbeefc80741f2015-10-22 13:14:45 -07002718 EXPECT_TRUE(offer_options.has_audio());
htaaac2dea2016-03-10 13:35:55 -08002719 EXPECT_TRUE(offer_options.has_video());
deadbeefab9b2d12015-10-14 11:33:11 -07002720
deadbeefc80741f2015-10-22 13:14:45 -07002721 RTCOfferAnswerOptions updated_rtc_offer_options;
2722 updated_rtc_offer_options.offer_to_receive_audio = 1;
2723 updated_rtc_offer_options.offer_to_receive_video = 1;
deadbeefab9b2d12015-10-14 11:33:11 -07002724
2725 cricket::MediaSessionOptions updated_offer_options;
htaaac2dea2016-03-10 13:35:55 -08002726 EXPECT_TRUE(ExtractMediaSessionOptions(updated_rtc_offer_options, false,
htaa2a49d92016-03-04 02:51:39 -08002727 &updated_offer_options));
deadbeefab9b2d12015-10-14 11:33:11 -07002728 EXPECT_TRUE(updated_offer_options.has_audio());
2729 EXPECT_TRUE(updated_offer_options.has_video());
2730
2731 // Since an offer has been created with both audio and video, subsequent
2732 // offers and answers should contain both audio and video.
2733 // Answers will only contain the media types that exist in the offer
2734 // regardless of the value of |updated_answer_options.has_audio| and
2735 // |updated_answer_options.has_video|.
2736 FakeConstraints updated_answer_c;
2737 answer_c.SetMandatoryReceiveAudio(false);
2738 answer_c.SetMandatoryReceiveVideo(false);
2739
2740 cricket::MediaSessionOptions updated_answer_options;
2741 EXPECT_TRUE(
2742 ParseConstraintsForAnswer(&updated_answer_c, &updated_answer_options));
2743 EXPECT_TRUE(updated_answer_options.has_audio());
2744 EXPECT_TRUE(updated_answer_options.has_video());
deadbeefab9b2d12015-10-14 11:33:11 -07002745}