blob: 14a067995b42205c698b5581d32b73f1972e1449 [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
1704// Test that we can create a session description from an SDP string from
1705// FireFox, use it as a remote session description, generate an answer and use
1706// the answer as a local description.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07001707TEST_F(PeerConnectionInterfaceTest, ReceiveFireFoxOffer) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001708 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001709 FakeConstraints constraints;
1710 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
1711 true);
1712 CreatePeerConnection(&constraints);
1713 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
1714 SessionDescriptionInterface* desc =
1715 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
jbauchfabe2c92015-07-16 13:43:14 -07001716 webrtc::kFireFoxSdpOffer, nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001717 EXPECT_TRUE(DoSetSessionDescription(desc, false));
1718 CreateAnswerAsLocalDescription();
1719 ASSERT_TRUE(pc_->local_description() != NULL);
1720 ASSERT_TRUE(pc_->remote_description() != NULL);
1721
1722 const cricket::ContentInfo* content =
1723 cricket::GetFirstAudioContent(pc_->local_description()->description());
1724 ASSERT_TRUE(content != NULL);
1725 EXPECT_FALSE(content->rejected);
1726
1727 content =
1728 cricket::GetFirstVideoContent(pc_->local_description()->description());
1729 ASSERT_TRUE(content != NULL);
1730 EXPECT_FALSE(content->rejected);
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00001731#ifdef HAVE_SCTP
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001732 content =
1733 cricket::GetFirstDataContent(pc_->local_description()->description());
1734 ASSERT_TRUE(content != NULL);
1735 EXPECT_TRUE(content->rejected);
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00001736#endif
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001737}
1738
1739// Test that we can create an audio only offer and receive an answer with a
1740// limited set of audio codecs and receive an updated offer with more audio
1741// codecs, where the added codecs are not supported.
1742TEST_F(PeerConnectionInterfaceTest, ReceiveUpdatedAudioOfferWithBadCodecs) {
1743 CreatePeerConnection();
1744 AddVoiceStream("audio_label");
1745 CreateOfferAsLocalDescription();
1746
1747 SessionDescriptionInterface* answer =
1748 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
jbauchfabe2c92015-07-16 13:43:14 -07001749 webrtc::kAudioSdp, nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001750 EXPECT_TRUE(DoSetSessionDescription(answer, false));
1751
1752 SessionDescriptionInterface* updated_offer =
1753 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
jbauchfabe2c92015-07-16 13:43:14 -07001754 webrtc::kAudioSdpWithUnsupportedCodecs,
1755 nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001756 EXPECT_TRUE(DoSetSessionDescription(updated_offer, false));
1757 CreateAnswerAsLocalDescription();
1758}
1759
deadbeefc80741f2015-10-22 13:14:45 -07001760// Test that if we're receiving (but not sending) a track, subsequent offers
1761// will have m-lines with a=recvonly.
1762TEST_F(PeerConnectionInterfaceTest, CreateSubsequentRecvOnlyOffer) {
1763 FakeConstraints constraints;
1764 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
1765 true);
1766 CreatePeerConnection(&constraints);
1767 CreateAndSetRemoteOffer(kSdpStringWithStream1);
1768 CreateAnswerAsLocalDescription();
1769
1770 // At this point we should be receiving stream 1, but not sending anything.
1771 // A new offer should be recvonly.
kwiberg2bbff992016-03-16 11:03:04 -07001772 rtc::scoped_ptr<SessionDescriptionInterface> offer;
deadbeefc80741f2015-10-22 13:14:45 -07001773 DoCreateOffer(&offer, nullptr);
1774
1775 const cricket::ContentInfo* video_content =
1776 cricket::GetFirstVideoContent(offer->description());
1777 const cricket::VideoContentDescription* video_desc =
1778 static_cast<const cricket::VideoContentDescription*>(
1779 video_content->description);
1780 ASSERT_EQ(cricket::MD_RECVONLY, video_desc->direction());
1781
1782 const cricket::ContentInfo* audio_content =
1783 cricket::GetFirstAudioContent(offer->description());
1784 const cricket::AudioContentDescription* audio_desc =
1785 static_cast<const cricket::AudioContentDescription*>(
1786 audio_content->description);
1787 ASSERT_EQ(cricket::MD_RECVONLY, audio_desc->direction());
1788}
1789
1790// Test that if we're receiving (but not sending) a track, and the
1791// offerToReceiveVideo/offerToReceiveAudio constraints are explicitly set to
1792// false, the generated m-lines will be a=inactive.
1793TEST_F(PeerConnectionInterfaceTest, CreateSubsequentInactiveOffer) {
1794 FakeConstraints constraints;
1795 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
1796 true);
1797 CreatePeerConnection(&constraints);
1798 CreateAndSetRemoteOffer(kSdpStringWithStream1);
1799 CreateAnswerAsLocalDescription();
1800
1801 // At this point we should be receiving stream 1, but not sending anything.
1802 // A new offer would be recvonly, but we'll set the "no receive" constraints
1803 // to make it inactive.
kwiberg2bbff992016-03-16 11:03:04 -07001804 rtc::scoped_ptr<SessionDescriptionInterface> offer;
deadbeefc80741f2015-10-22 13:14:45 -07001805 FakeConstraints offer_constraints;
1806 offer_constraints.AddMandatory(
1807 webrtc::MediaConstraintsInterface::kOfferToReceiveVideo, false);
1808 offer_constraints.AddMandatory(
1809 webrtc::MediaConstraintsInterface::kOfferToReceiveAudio, false);
1810 DoCreateOffer(&offer, &offer_constraints);
1811
1812 const cricket::ContentInfo* video_content =
1813 cricket::GetFirstVideoContent(offer->description());
1814 const cricket::VideoContentDescription* video_desc =
1815 static_cast<const cricket::VideoContentDescription*>(
1816 video_content->description);
1817 ASSERT_EQ(cricket::MD_INACTIVE, video_desc->direction());
1818
1819 const cricket::ContentInfo* audio_content =
1820 cricket::GetFirstAudioContent(offer->description());
1821 const cricket::AudioContentDescription* audio_desc =
1822 static_cast<const cricket::AudioContentDescription*>(
1823 audio_content->description);
1824 ASSERT_EQ(cricket::MD_INACTIVE, audio_desc->direction());
1825}
1826
deadbeef653b8e02015-11-11 12:55:10 -08001827// Test that we can use SetConfiguration to change the ICE servers of the
1828// PortAllocator.
1829TEST_F(PeerConnectionInterfaceTest, SetConfigurationChangesIceServers) {
1830 CreatePeerConnection();
1831
1832 PeerConnectionInterface::RTCConfiguration config;
1833 PeerConnectionInterface::IceServer server;
1834 server.uri = "stun:test_hostname";
1835 config.servers.push_back(server);
1836 EXPECT_TRUE(pc_->SetConfiguration(config));
1837
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08001838 EXPECT_EQ(1u, port_allocator_->stun_servers().size());
1839 EXPECT_EQ("test_hostname",
1840 port_allocator_->stun_servers().begin()->hostname());
deadbeef653b8e02015-11-11 12:55:10 -08001841}
1842
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001843// Test that PeerConnection::Close changes the states to closed and all remote
1844// tracks change state to ended.
1845TEST_F(PeerConnectionInterfaceTest, CloseAndTestStreamsAndStates) {
1846 // Initialize a PeerConnection and negotiate local and remote session
1847 // description.
1848 InitiateCall();
1849 ASSERT_EQ(1u, pc_->local_streams()->count());
1850 ASSERT_EQ(1u, pc_->remote_streams()->count());
1851
1852 pc_->Close();
1853
1854 EXPECT_EQ(PeerConnectionInterface::kClosed, pc_->signaling_state());
1855 EXPECT_EQ(PeerConnectionInterface::kIceConnectionClosed,
1856 pc_->ice_connection_state());
1857 EXPECT_EQ(PeerConnectionInterface::kIceGatheringComplete,
1858 pc_->ice_gathering_state());
1859
1860 EXPECT_EQ(1u, pc_->local_streams()->count());
1861 EXPECT_EQ(1u, pc_->remote_streams()->count());
1862
1863 scoped_refptr<MediaStreamInterface> remote_stream =
1864 pc_->remote_streams()->at(0);
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07001865 // Track state may be updated asynchronously.
perkjd61bf802016-03-24 03:16:19 -07001866 EXPECT_EQ_WAIT(MediaStreamTrackInterface::kEnded,
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07001867 remote_stream->GetAudioTracks()[0]->state(), kTimeout);
1868 EXPECT_EQ_WAIT(MediaStreamTrackInterface::kEnded,
1869 remote_stream->GetVideoTracks()[0]->state(), kTimeout);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001870}
1871
1872// Test that PeerConnection methods fails gracefully after
1873// PeerConnection::Close has been called.
1874TEST_F(PeerConnectionInterfaceTest, CloseAndTestMethods) {
1875 CreatePeerConnection();
1876 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
1877 CreateOfferAsRemoteDescription();
1878 CreateAnswerAsLocalDescription();
1879
1880 ASSERT_EQ(1u, pc_->local_streams()->count());
1881 scoped_refptr<MediaStreamInterface> local_stream =
1882 pc_->local_streams()->at(0);
1883
1884 pc_->Close();
1885
1886 pc_->RemoveStream(local_stream);
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +00001887 EXPECT_FALSE(pc_->AddStream(local_stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001888
1889 ASSERT_FALSE(local_stream->GetAudioTracks().empty());
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001890 rtc::scoped_refptr<webrtc::DtmfSenderInterface> dtmf_sender(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001891 pc_->CreateDtmfSender(local_stream->GetAudioTracks()[0]));
wu@webrtc.org66037362013-08-13 00:09:35 +00001892 EXPECT_TRUE(NULL == dtmf_sender); // local stream has been removed.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001893
1894 EXPECT_TRUE(pc_->CreateDataChannel("test", NULL) == NULL);
1895
1896 EXPECT_TRUE(pc_->local_description() != NULL);
1897 EXPECT_TRUE(pc_->remote_description() != NULL);
1898
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001899 rtc::scoped_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001900 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001901 rtc::scoped_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -07001902 EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001903
1904 std::string sdp;
1905 ASSERT_TRUE(pc_->remote_description()->ToString(&sdp));
1906 SessionDescriptionInterface* remote_offer =
1907 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
1908 sdp, NULL);
1909 EXPECT_FALSE(DoSetRemoteDescription(remote_offer));
1910
1911 ASSERT_TRUE(pc_->local_description()->ToString(&sdp));
1912 SessionDescriptionInterface* local_offer =
1913 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
1914 sdp, NULL);
1915 EXPECT_FALSE(DoSetLocalDescription(local_offer));
1916}
1917
1918// Test that GetStats can still be called after PeerConnection::Close.
1919TEST_F(PeerConnectionInterfaceTest, CloseAndGetStats) {
1920 InitiateCall();
1921 pc_->Close();
1922 DoGetStats(NULL);
1923}
deadbeefab9b2d12015-10-14 11:33:11 -07001924
1925// NOTE: The series of tests below come from what used to be
1926// mediastreamsignaling_unittest.cc, and are mostly aimed at testing that
1927// setting a remote or local description has the expected effects.
1928
1929// This test verifies that the remote MediaStreams corresponding to a received
1930// SDP string is created. In this test the two separate MediaStreams are
1931// signaled.
1932TEST_F(PeerConnectionInterfaceTest, UpdateRemoteStreams) {
1933 FakeConstraints constraints;
1934 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
1935 true);
1936 CreatePeerConnection(&constraints);
1937 CreateAndSetRemoteOffer(kSdpStringWithStream1);
1938
1939 rtc::scoped_refptr<StreamCollection> reference(CreateStreamCollection(1));
1940 EXPECT_TRUE(
1941 CompareStreamCollections(observer_.remote_streams(), reference.get()));
1942 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
1943 EXPECT_TRUE(remote_stream->GetVideoTracks()[0]->GetSource() != nullptr);
1944
1945 // Create a session description based on another SDP with another
1946 // MediaStream.
1947 CreateAndSetRemoteOffer(kSdpStringWithStream1And2);
1948
1949 rtc::scoped_refptr<StreamCollection> reference2(CreateStreamCollection(2));
1950 EXPECT_TRUE(
1951 CompareStreamCollections(observer_.remote_streams(), reference2.get()));
1952}
1953
1954// This test verifies that when remote tracks are added/removed from SDP, the
1955// created remote streams are updated appropriately.
1956TEST_F(PeerConnectionInterfaceTest,
1957 AddRemoveTrackFromExistingRemoteMediaStream) {
1958 FakeConstraints constraints;
1959 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
1960 true);
1961 CreatePeerConnection(&constraints);
kwiberg2bbff992016-03-16 11:03:04 -07001962 rtc::scoped_ptr<SessionDescriptionInterface> desc_ms1 =
1963 CreateSessionDescriptionAndReference(1, 1);
deadbeefab9b2d12015-10-14 11:33:11 -07001964 EXPECT_TRUE(DoSetRemoteDescription(desc_ms1.release()));
1965 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
1966 reference_collection_));
1967
1968 // Add extra audio and video tracks to the same MediaStream.
kwiberg2bbff992016-03-16 11:03:04 -07001969 rtc::scoped_ptr<SessionDescriptionInterface> desc_ms1_two_tracks =
1970 CreateSessionDescriptionAndReference(2, 2);
deadbeefab9b2d12015-10-14 11:33:11 -07001971 EXPECT_TRUE(DoSetRemoteDescription(desc_ms1_two_tracks.release()));
1972 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
1973 reference_collection_));
perkjd61bf802016-03-24 03:16:19 -07001974 scoped_refptr<AudioTrackInterface> audio_track2 =
1975 observer_.remote_streams()->at(0)->GetAudioTracks()[1];
1976 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, audio_track2->state());
1977 scoped_refptr<VideoTrackInterface> video_track2 =
1978 observer_.remote_streams()->at(0)->GetVideoTracks()[1];
1979 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, video_track2->state());
deadbeefab9b2d12015-10-14 11:33:11 -07001980
1981 // Remove the extra audio and video tracks.
kwiberg2bbff992016-03-16 11:03:04 -07001982 rtc::scoped_ptr<SessionDescriptionInterface> desc_ms2 =
1983 CreateSessionDescriptionAndReference(1, 1);
perkjd61bf802016-03-24 03:16:19 -07001984 MockTrackObserver audio_track_observer(audio_track2);
1985 MockTrackObserver video_track_observer(video_track2);
1986
1987 EXPECT_CALL(audio_track_observer, OnChanged()).Times(Exactly(1));
1988 EXPECT_CALL(video_track_observer, OnChanged()).Times(Exactly(1));
deadbeefab9b2d12015-10-14 11:33:11 -07001989 EXPECT_TRUE(DoSetRemoteDescription(desc_ms2.release()));
1990 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
1991 reference_collection_));
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07001992 // Track state may be updated asynchronously.
perkjd61bf802016-03-24 03:16:19 -07001993 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07001994 audio_track2->state(), kTimeout);
1995 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
1996 video_track2->state(), kTimeout);
deadbeefab9b2d12015-10-14 11:33:11 -07001997}
1998
1999// This tests that remote tracks are ended if a local session description is set
2000// that rejects the media content type.
2001TEST_F(PeerConnectionInterfaceTest, RejectMediaContent) {
2002 FakeConstraints constraints;
2003 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2004 true);
2005 CreatePeerConnection(&constraints);
2006 // First create and set a remote offer, then reject its video content in our
2007 // answer.
2008 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2009 ASSERT_EQ(1u, observer_.remote_streams()->count());
2010 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2011 ASSERT_EQ(1u, remote_stream->GetVideoTracks().size());
2012 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2013
2014 rtc::scoped_refptr<webrtc::VideoTrackInterface> remote_video =
2015 remote_stream->GetVideoTracks()[0];
2016 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, remote_video->state());
2017 rtc::scoped_refptr<webrtc::AudioTrackInterface> remote_audio =
2018 remote_stream->GetAudioTracks()[0];
2019 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, remote_audio->state());
2020
2021 rtc::scoped_ptr<SessionDescriptionInterface> local_answer;
kwiberg2bbff992016-03-16 11:03:04 -07002022 EXPECT_TRUE(DoCreateAnswer(&local_answer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07002023 cricket::ContentInfo* video_info =
2024 local_answer->description()->GetContentByName("video");
2025 video_info->rejected = true;
2026 EXPECT_TRUE(DoSetLocalDescription(local_answer.release()));
2027 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kEnded, remote_video->state());
2028 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, remote_audio->state());
2029
2030 // Now create an offer where we reject both video and audio.
2031 rtc::scoped_ptr<SessionDescriptionInterface> local_offer;
kwiberg2bbff992016-03-16 11:03:04 -07002032 EXPECT_TRUE(DoCreateOffer(&local_offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07002033 video_info = local_offer->description()->GetContentByName("video");
2034 ASSERT_TRUE(video_info != nullptr);
2035 video_info->rejected = true;
2036 cricket::ContentInfo* audio_info =
2037 local_offer->description()->GetContentByName("audio");
2038 ASSERT_TRUE(audio_info != nullptr);
2039 audio_info->rejected = true;
2040 EXPECT_TRUE(DoSetLocalDescription(local_offer.release()));
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002041 // Track state may be updated asynchronously.
perkjd61bf802016-03-24 03:16:19 -07002042 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002043 remote_audio->state(), kTimeout);
2044 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
2045 remote_video->state(), kTimeout);
deadbeefab9b2d12015-10-14 11:33:11 -07002046}
2047
2048// This tests that we won't crash if the remote track has been removed outside
2049// of PeerConnection and then PeerConnection tries to reject the track.
2050TEST_F(PeerConnectionInterfaceTest, RemoveTrackThenRejectMediaContent) {
2051 FakeConstraints constraints;
2052 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2053 true);
2054 CreatePeerConnection(&constraints);
2055 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2056 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2057 remote_stream->RemoveTrack(remote_stream->GetVideoTracks()[0]);
2058 remote_stream->RemoveTrack(remote_stream->GetAudioTracks()[0]);
2059
2060 rtc::scoped_ptr<SessionDescriptionInterface> local_answer(
2061 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
2062 kSdpStringWithStream1, nullptr));
2063 cricket::ContentInfo* video_info =
2064 local_answer->description()->GetContentByName("video");
2065 video_info->rejected = true;
2066 cricket::ContentInfo* audio_info =
2067 local_answer->description()->GetContentByName("audio");
2068 audio_info->rejected = true;
2069 EXPECT_TRUE(DoSetLocalDescription(local_answer.release()));
2070
2071 // No crash is a pass.
2072}
2073
deadbeef5e97fb52015-10-15 12:49:08 -07002074// This tests that if a recvonly remote description is set, no remote streams
2075// will be created, even if the description contains SSRCs/MSIDs.
2076// See: https://code.google.com/p/webrtc/issues/detail?id=5054
2077TEST_F(PeerConnectionInterfaceTest, RecvonlyDescriptionDoesntCreateStream) {
2078 FakeConstraints constraints;
2079 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2080 true);
2081 CreatePeerConnection(&constraints);
2082
2083 std::string recvonly_offer = kSdpStringWithStream1;
2084 rtc::replace_substrs(kSendrecv, strlen(kSendrecv), kRecvonly,
2085 strlen(kRecvonly), &recvonly_offer);
2086 CreateAndSetRemoteOffer(recvonly_offer);
2087
2088 EXPECT_EQ(0u, observer_.remote_streams()->count());
2089}
2090
deadbeefab9b2d12015-10-14 11:33:11 -07002091// This tests that a default MediaStream is created if a remote session
2092// description doesn't contain any streams and no MSID support.
2093// It also tests that the default stream is updated if a video m-line is added
2094// in a subsequent session description.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002095TEST_F(PeerConnectionInterfaceTest, SdpWithoutMsidCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002096 FakeConstraints constraints;
2097 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2098 true);
2099 CreatePeerConnection(&constraints);
2100 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2101
2102 ASSERT_EQ(1u, observer_.remote_streams()->count());
2103 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2104
2105 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2106 EXPECT_EQ(0u, remote_stream->GetVideoTracks().size());
2107 EXPECT_EQ("default", remote_stream->label());
2108
2109 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2110 ASSERT_EQ(1u, observer_.remote_streams()->count());
2111 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2112 EXPECT_EQ("defaulta0", remote_stream->GetAudioTracks()[0]->id());
deadbeef884f5852016-01-15 09:20:04 -08002113 EXPECT_EQ(MediaStreamTrackInterface::kLive,
2114 remote_stream->GetAudioTracks()[0]->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002115 ASSERT_EQ(1u, remote_stream->GetVideoTracks().size());
2116 EXPECT_EQ("defaultv0", remote_stream->GetVideoTracks()[0]->id());
deadbeef884f5852016-01-15 09:20:04 -08002117 EXPECT_EQ(MediaStreamTrackInterface::kLive,
2118 remote_stream->GetVideoTracks()[0]->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002119}
2120
2121// This tests that a default MediaStream is created if a remote session
2122// description doesn't contain any streams and media direction is send only.
2123TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002124 SendOnlySdpWithoutMsidCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002125 FakeConstraints constraints;
2126 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2127 true);
2128 CreatePeerConnection(&constraints);
2129 CreateAndSetRemoteOffer(kSdpStringSendOnlyWithoutStreams);
2130
2131 ASSERT_EQ(1u, observer_.remote_streams()->count());
2132 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2133
2134 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2135 EXPECT_EQ(1u, remote_stream->GetVideoTracks().size());
2136 EXPECT_EQ("default", remote_stream->label());
2137}
2138
2139// This tests that it won't crash when PeerConnection tries to remove
2140// a remote track that as already been removed from the MediaStream.
2141TEST_F(PeerConnectionInterfaceTest, RemoveAlreadyGoneRemoteStream) {
2142 FakeConstraints constraints;
2143 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2144 true);
2145 CreatePeerConnection(&constraints);
2146 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2147 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2148 remote_stream->RemoveTrack(remote_stream->GetAudioTracks()[0]);
2149 remote_stream->RemoveTrack(remote_stream->GetVideoTracks()[0]);
2150
2151 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2152
2153 // No crash is a pass.
2154}
2155
2156// This tests that a default MediaStream is created if the remote session
2157// description doesn't contain any streams and don't contain an indication if
2158// MSID is supported.
2159TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002160 SdpWithoutMsidAndStreamsCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002161 FakeConstraints constraints;
2162 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2163 true);
2164 CreatePeerConnection(&constraints);
2165 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2166
2167 ASSERT_EQ(1u, observer_.remote_streams()->count());
2168 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2169 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2170 EXPECT_EQ(1u, remote_stream->GetVideoTracks().size());
2171}
2172
2173// This tests that a default MediaStream is not created if the remote session
2174// description doesn't contain any streams but does support MSID.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002175TEST_F(PeerConnectionInterfaceTest, SdpWithMsidDontCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002176 FakeConstraints constraints;
2177 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2178 true);
2179 CreatePeerConnection(&constraints);
2180 CreateAndSetRemoteOffer(kSdpStringWithMsidWithoutStreams);
2181 EXPECT_EQ(0u, observer_.remote_streams()->count());
2182}
2183
deadbeefbda7e0b2015-12-08 17:13:40 -08002184// This tests that when setting a new description, the old default tracks are
2185// not destroyed and recreated.
2186// See: https://bugs.chromium.org/p/webrtc/issues/detail?id=5250
Stefan Holmer102362b2016-03-18 09:39:07 +01002187TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002188 DefaultTracksNotDestroyedAndRecreated) {
deadbeefbda7e0b2015-12-08 17:13:40 -08002189 FakeConstraints constraints;
2190 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2191 true);
2192 CreatePeerConnection(&constraints);
2193 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2194
2195 ASSERT_EQ(1u, observer_.remote_streams()->count());
2196 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2197 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2198
2199 // Set the track to "disabled", then set a new description and ensure the
2200 // track is still disabled, which ensures it hasn't been recreated.
2201 remote_stream->GetAudioTracks()[0]->set_enabled(false);
2202 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2203 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2204 EXPECT_FALSE(remote_stream->GetAudioTracks()[0]->enabled());
2205}
2206
deadbeefab9b2d12015-10-14 11:33:11 -07002207// This tests that a default MediaStream is not created if a remote session
2208// description is updated to not have any MediaStreams.
2209TEST_F(PeerConnectionInterfaceTest, VerifyDefaultStreamIsNotCreated) {
2210 FakeConstraints constraints;
2211 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2212 true);
2213 CreatePeerConnection(&constraints);
2214 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2215 rtc::scoped_refptr<StreamCollection> reference(CreateStreamCollection(1));
2216 EXPECT_TRUE(
2217 CompareStreamCollections(observer_.remote_streams(), reference.get()));
2218
2219 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2220 EXPECT_EQ(0u, observer_.remote_streams()->count());
2221}
2222
2223// This tests that an RtpSender is created when the local description is set
2224// after adding a local stream.
2225// TODO(deadbeef): This test and the one below it need to be updated when
2226// an RtpSender's lifetime isn't determined by when a local description is set.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002227TEST_F(PeerConnectionInterfaceTest, LocalDescriptionChanged) {
deadbeefab9b2d12015-10-14 11:33:11 -07002228 FakeConstraints constraints;
2229 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2230 true);
2231 CreatePeerConnection(&constraints);
2232 // Create an offer just to ensure we have an identity before we manually
2233 // call SetLocalDescription.
2234 rtc::scoped_ptr<SessionDescriptionInterface> throwaway;
kwiberg2bbff992016-03-16 11:03:04 -07002235 ASSERT_TRUE(DoCreateOffer(&throwaway, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07002236
kwiberg2bbff992016-03-16 11:03:04 -07002237 rtc::scoped_ptr<SessionDescriptionInterface> desc_1 =
2238 CreateSessionDescriptionAndReference(2, 2);
deadbeefab9b2d12015-10-14 11:33:11 -07002239
2240 pc_->AddStream(reference_collection_->at(0));
2241 EXPECT_TRUE(DoSetLocalDescription(desc_1.release()));
2242 auto senders = pc_->GetSenders();
2243 EXPECT_EQ(4u, senders.size());
2244 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2245 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2246 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[1]));
2247 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[1]));
2248
2249 // Remove an audio and video track.
deadbeeffac06552015-11-25 11:26:01 -08002250 pc_->RemoveStream(reference_collection_->at(0));
kwiberg2bbff992016-03-16 11:03:04 -07002251 rtc::scoped_ptr<SessionDescriptionInterface> desc_2 =
2252 CreateSessionDescriptionAndReference(1, 1);
deadbeeffac06552015-11-25 11:26:01 -08002253 pc_->AddStream(reference_collection_->at(0));
deadbeefab9b2d12015-10-14 11:33:11 -07002254 EXPECT_TRUE(DoSetLocalDescription(desc_2.release()));
2255 senders = pc_->GetSenders();
2256 EXPECT_EQ(2u, senders.size());
2257 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2258 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2259 EXPECT_FALSE(ContainsSender(senders, kAudioTracks[1]));
2260 EXPECT_FALSE(ContainsSender(senders, kVideoTracks[1]));
2261}
2262
2263// This tests that an RtpSender is created when the local description is set
2264// before adding a local stream.
2265TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002266 AddLocalStreamAfterLocalDescriptionChanged) {
deadbeefab9b2d12015-10-14 11:33:11 -07002267 FakeConstraints constraints;
2268 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2269 true);
2270 CreatePeerConnection(&constraints);
2271 // Create an offer just to ensure we have an identity before we manually
2272 // call SetLocalDescription.
2273 rtc::scoped_ptr<SessionDescriptionInterface> throwaway;
kwiberg2bbff992016-03-16 11:03:04 -07002274 ASSERT_TRUE(DoCreateOffer(&throwaway, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07002275
kwiberg2bbff992016-03-16 11:03:04 -07002276 rtc::scoped_ptr<SessionDescriptionInterface> desc_1 =
2277 CreateSessionDescriptionAndReference(2, 2);
deadbeefab9b2d12015-10-14 11:33:11 -07002278
2279 EXPECT_TRUE(DoSetLocalDescription(desc_1.release()));
2280 auto senders = pc_->GetSenders();
2281 EXPECT_EQ(0u, senders.size());
2282
2283 pc_->AddStream(reference_collection_->at(0));
2284 senders = pc_->GetSenders();
2285 EXPECT_EQ(4u, senders.size());
2286 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2287 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2288 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[1]));
2289 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[1]));
2290}
2291
2292// This tests that the expected behavior occurs if the SSRC on a local track is
2293// changed when SetLocalDescription is called.
2294TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002295 ChangeSsrcOnTrackInLocalSessionDescription) {
deadbeefab9b2d12015-10-14 11:33:11 -07002296 FakeConstraints constraints;
2297 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2298 true);
2299 CreatePeerConnection(&constraints);
2300 // Create an offer just to ensure we have an identity before we manually
2301 // call SetLocalDescription.
2302 rtc::scoped_ptr<SessionDescriptionInterface> throwaway;
kwiberg2bbff992016-03-16 11:03:04 -07002303 ASSERT_TRUE(DoCreateOffer(&throwaway, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07002304
kwiberg2bbff992016-03-16 11:03:04 -07002305 rtc::scoped_ptr<SessionDescriptionInterface> desc =
2306 CreateSessionDescriptionAndReference(1, 1);
deadbeefab9b2d12015-10-14 11:33:11 -07002307 std::string sdp;
2308 desc->ToString(&sdp);
2309
2310 pc_->AddStream(reference_collection_->at(0));
2311 EXPECT_TRUE(DoSetLocalDescription(desc.release()));
2312 auto senders = pc_->GetSenders();
2313 EXPECT_EQ(2u, senders.size());
2314 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2315 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2316
2317 // Change the ssrc of the audio and video track.
2318 std::string ssrc_org = "a=ssrc:1";
2319 std::string ssrc_to = "a=ssrc:97";
2320 rtc::replace_substrs(ssrc_org.c_str(), ssrc_org.length(), ssrc_to.c_str(),
2321 ssrc_to.length(), &sdp);
2322 ssrc_org = "a=ssrc:2";
2323 ssrc_to = "a=ssrc:98";
2324 rtc::replace_substrs(ssrc_org.c_str(), ssrc_org.length(), ssrc_to.c_str(),
2325 ssrc_to.length(), &sdp);
2326 rtc::scoped_ptr<SessionDescriptionInterface> updated_desc(
2327 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer, sdp,
2328 nullptr));
2329
2330 EXPECT_TRUE(DoSetLocalDescription(updated_desc.release()));
2331 senders = pc_->GetSenders();
2332 EXPECT_EQ(2u, senders.size());
2333 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2334 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2335 // TODO(deadbeef): Once RtpSenders expose parameters, check that the SSRC
2336 // changed.
2337}
2338
2339// This tests that the expected behavior occurs if a new session description is
2340// set with the same tracks, but on a different MediaStream.
Stefan Holmer55d6e7c2016-03-17 16:26:40 +01002341TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002342 SignalSameTracksInSeparateMediaStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002343 FakeConstraints constraints;
2344 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2345 true);
2346 CreatePeerConnection(&constraints);
2347 // Create an offer just to ensure we have an identity before we manually
2348 // call SetLocalDescription.
2349 rtc::scoped_ptr<SessionDescriptionInterface> throwaway;
kwiberg2bbff992016-03-16 11:03:04 -07002350 ASSERT_TRUE(DoCreateOffer(&throwaway, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07002351
kwiberg2bbff992016-03-16 11:03:04 -07002352 rtc::scoped_ptr<SessionDescriptionInterface> desc =
2353 CreateSessionDescriptionAndReference(1, 1);
deadbeefab9b2d12015-10-14 11:33:11 -07002354 std::string sdp;
2355 desc->ToString(&sdp);
2356
2357 pc_->AddStream(reference_collection_->at(0));
2358 EXPECT_TRUE(DoSetLocalDescription(desc.release()));
2359 auto senders = pc_->GetSenders();
2360 EXPECT_EQ(2u, senders.size());
2361 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2362 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2363
2364 // Add a new MediaStream but with the same tracks as in the first stream.
2365 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream_1(
2366 webrtc::MediaStream::Create(kStreams[1]));
2367 stream_1->AddTrack(reference_collection_->at(0)->GetVideoTracks()[0]);
2368 stream_1->AddTrack(reference_collection_->at(0)->GetAudioTracks()[0]);
2369 pc_->AddStream(stream_1);
2370
2371 // Replace msid in the original SDP.
2372 rtc::replace_substrs(kStreams[0], strlen(kStreams[0]), kStreams[1],
2373 strlen(kStreams[1]), &sdp);
2374
2375 rtc::scoped_ptr<SessionDescriptionInterface> updated_desc(
2376 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer, sdp,
2377 nullptr));
2378
2379 EXPECT_TRUE(DoSetLocalDescription(updated_desc.release()));
2380 senders = pc_->GetSenders();
2381 EXPECT_EQ(2u, senders.size());
2382 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2383 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2384}
2385
nisse51542be2016-02-12 02:27:06 -08002386// The PeerConnectionMediaConfig tests below verify that configuration
2387// and constraints are propagated into the MediaConfig passed to
2388// CreateMediaController. These settings are intended for MediaChannel
2389// constructors, but that is not exercised by these unittest.
2390class PeerConnectionFactoryForTest : public webrtc::PeerConnectionFactory {
2391 public:
2392 webrtc::MediaControllerInterface* CreateMediaController(
2393 const cricket::MediaConfig& config) const override {
2394 create_media_controller_called_ = true;
2395 create_media_controller_config_ = config;
2396
2397 webrtc::MediaControllerInterface* mc =
2398 PeerConnectionFactory::CreateMediaController(config);
2399 EXPECT_TRUE(mc != nullptr);
2400 return mc;
2401 }
2402
2403 // Mutable, so they can be modified in the above const-declared method.
2404 mutable bool create_media_controller_called_ = false;
2405 mutable cricket::MediaConfig create_media_controller_config_;
2406};
2407
2408class PeerConnectionMediaConfigTest : public testing::Test {
2409 protected:
2410 void SetUp() override {
nisseaf510af2016-03-21 08:20:42 -07002411 pcf_ = new rtc::RefCountedObject<PeerConnectionFactoryForTest>();
nisse51542be2016-02-12 02:27:06 -08002412 pcf_->Initialize();
2413 }
2414 const cricket::MediaConfig& TestCreatePeerConnection(
2415 const PeerConnectionInterface::RTCConfiguration& config,
2416 const MediaConstraintsInterface *constraints) {
2417 pcf_->create_media_controller_called_ = false;
2418
2419 scoped_refptr<PeerConnectionInterface> pc(
2420 pcf_->CreatePeerConnection(config, constraints, nullptr, nullptr,
2421 &observer_));
2422 EXPECT_TRUE(pc.get());
2423 EXPECT_TRUE(pcf_->create_media_controller_called_);
2424 return pcf_->create_media_controller_config_;
2425 }
2426
2427 scoped_refptr<PeerConnectionFactoryForTest> pcf_;
2428 MockPeerConnectionObserver observer_;
2429};
2430
2431// This test verifies the default behaviour with no constraints and a
2432// default RTCConfiguration.
2433TEST_F(PeerConnectionMediaConfigTest, TestDefaults) {
2434 PeerConnectionInterface::RTCConfiguration config;
2435 FakeConstraints constraints;
2436
2437 const cricket::MediaConfig& media_config =
2438 TestCreatePeerConnection(config, &constraints);
2439
2440 EXPECT_FALSE(media_config.enable_dscp);
nisse0db023a2016-03-01 04:29:59 -08002441 EXPECT_TRUE(media_config.video.enable_cpu_overuse_detection);
2442 EXPECT_FALSE(media_config.video.disable_prerenderer_smoothing);
2443 EXPECT_FALSE(media_config.video.suspend_below_min_bitrate);
nisse51542be2016-02-12 02:27:06 -08002444}
2445
2446// This test verifies the DSCP constraint is recognized and passed to
2447// the CreateMediaController call.
2448TEST_F(PeerConnectionMediaConfigTest, TestDscpConstraintTrue) {
2449 PeerConnectionInterface::RTCConfiguration config;
2450 FakeConstraints constraints;
2451
2452 constraints.AddOptional(webrtc::MediaConstraintsInterface::kEnableDscp, true);
2453 const cricket::MediaConfig& media_config =
2454 TestCreatePeerConnection(config, &constraints);
2455
2456 EXPECT_TRUE(media_config.enable_dscp);
2457}
2458
2459// This test verifies the cpu overuse detection constraint is
2460// recognized and passed to the CreateMediaController call.
2461TEST_F(PeerConnectionMediaConfigTest, TestCpuOveruseConstraintFalse) {
2462 PeerConnectionInterface::RTCConfiguration config;
2463 FakeConstraints constraints;
2464
2465 constraints.AddOptional(
2466 webrtc::MediaConstraintsInterface::kCpuOveruseDetection, false);
2467 const cricket::MediaConfig media_config =
2468 TestCreatePeerConnection(config, &constraints);
2469
nisse0db023a2016-03-01 04:29:59 -08002470 EXPECT_FALSE(media_config.video.enable_cpu_overuse_detection);
nisse51542be2016-02-12 02:27:06 -08002471}
2472
2473// This test verifies that the disable_prerenderer_smoothing flag is
2474// propagated from RTCConfiguration to the CreateMediaController call.
2475TEST_F(PeerConnectionMediaConfigTest, TestDisablePrerendererSmoothingTrue) {
2476 PeerConnectionInterface::RTCConfiguration config;
2477 FakeConstraints constraints;
2478
Niels Möller71bdda02016-03-31 12:59:59 +02002479 config.set_prerenderer_smoothing(false);
nisse51542be2016-02-12 02:27:06 -08002480 const cricket::MediaConfig& media_config =
2481 TestCreatePeerConnection(config, &constraints);
2482
nisse0db023a2016-03-01 04:29:59 -08002483 EXPECT_TRUE(media_config.video.disable_prerenderer_smoothing);
2484}
2485
2486// This test verifies the suspend below min bitrate constraint is
2487// recognized and passed to the CreateMediaController call.
2488TEST_F(PeerConnectionMediaConfigTest,
2489 TestSuspendBelowMinBitrateConstraintTrue) {
2490 PeerConnectionInterface::RTCConfiguration config;
2491 FakeConstraints constraints;
2492
2493 constraints.AddOptional(
2494 webrtc::MediaConstraintsInterface::kEnableVideoSuspendBelowMinBitrate,
2495 true);
2496 const cricket::MediaConfig media_config =
2497 TestCreatePeerConnection(config, &constraints);
2498
2499 EXPECT_TRUE(media_config.video.suspend_below_min_bitrate);
nisse51542be2016-02-12 02:27:06 -08002500}
2501
deadbeefab9b2d12015-10-14 11:33:11 -07002502// The following tests verify that session options are created correctly.
deadbeefc80741f2015-10-22 13:14:45 -07002503// TODO(deadbeef): Convert these tests to be more end-to-end. Instead of
2504// "verify options are converted correctly", should be "pass options into
2505// CreateOffer and verify the correct offer is produced."
deadbeefab9b2d12015-10-14 11:33:11 -07002506
2507TEST(CreateSessionOptionsTest, GetOptionsForOfferWithInvalidAudioOption) {
2508 RTCOfferAnswerOptions rtc_options;
2509 rtc_options.offer_to_receive_audio = RTCOfferAnswerOptions::kUndefined - 1;
2510
2511 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08002512 EXPECT_FALSE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002513
2514 rtc_options.offer_to_receive_audio =
2515 RTCOfferAnswerOptions::kMaxOfferToReceiveMedia + 1;
htaaac2dea2016-03-10 13:35:55 -08002516 EXPECT_FALSE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002517}
2518
2519TEST(CreateSessionOptionsTest, GetOptionsForOfferWithInvalidVideoOption) {
2520 RTCOfferAnswerOptions rtc_options;
2521 rtc_options.offer_to_receive_video = RTCOfferAnswerOptions::kUndefined - 1;
2522
2523 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08002524 EXPECT_FALSE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002525
2526 rtc_options.offer_to_receive_video =
2527 RTCOfferAnswerOptions::kMaxOfferToReceiveMedia + 1;
htaaac2dea2016-03-10 13:35:55 -08002528 EXPECT_FALSE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002529}
2530
2531// Test that a MediaSessionOptions is created for an offer if
deadbeefc80741f2015-10-22 13:14:45 -07002532// OfferToReceiveAudio and OfferToReceiveVideo options are set.
deadbeefab9b2d12015-10-14 11:33:11 -07002533TEST(CreateSessionOptionsTest, GetMediaSessionOptionsForOfferWithAudioVideo) {
2534 RTCOfferAnswerOptions rtc_options;
2535 rtc_options.offer_to_receive_audio = 1;
2536 rtc_options.offer_to_receive_video = 1;
2537
2538 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08002539 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002540 EXPECT_TRUE(options.has_audio());
2541 EXPECT_TRUE(options.has_video());
2542 EXPECT_TRUE(options.bundle_enabled);
2543}
2544
2545// Test that a correct MediaSessionOptions is created for an offer if
deadbeefc80741f2015-10-22 13:14:45 -07002546// OfferToReceiveAudio is set.
deadbeefab9b2d12015-10-14 11:33:11 -07002547TEST(CreateSessionOptionsTest, GetMediaSessionOptionsForOfferWithAudio) {
2548 RTCOfferAnswerOptions rtc_options;
2549 rtc_options.offer_to_receive_audio = 1;
2550
2551 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08002552 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002553 EXPECT_TRUE(options.has_audio());
2554 EXPECT_FALSE(options.has_video());
2555 EXPECT_TRUE(options.bundle_enabled);
2556}
2557
2558// Test that a correct MediaSessionOptions is created for an offer if
deadbeefc80741f2015-10-22 13:14:45 -07002559// the default OfferOptions are used.
deadbeefab9b2d12015-10-14 11:33:11 -07002560TEST(CreateSessionOptionsTest, GetDefaultMediaSessionOptionsForOffer) {
2561 RTCOfferAnswerOptions rtc_options;
2562
2563 cricket::MediaSessionOptions options;
deadbeef0ed85b22016-02-23 17:24:52 -08002564 options.transport_options["audio"] = cricket::TransportOptions();
2565 options.transport_options["video"] = cricket::TransportOptions();
htaaac2dea2016-03-10 13:35:55 -08002566 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefc80741f2015-10-22 13:14:45 -07002567 EXPECT_TRUE(options.has_audio());
deadbeefab9b2d12015-10-14 11:33:11 -07002568 EXPECT_FALSE(options.has_video());
deadbeefc80741f2015-10-22 13:14:45 -07002569 EXPECT_TRUE(options.bundle_enabled);
deadbeefab9b2d12015-10-14 11:33:11 -07002570 EXPECT_TRUE(options.vad_enabled);
deadbeef0ed85b22016-02-23 17:24:52 -08002571 EXPECT_FALSE(options.transport_options["audio"].ice_restart);
2572 EXPECT_FALSE(options.transport_options["video"].ice_restart);
deadbeefab9b2d12015-10-14 11:33:11 -07002573}
2574
2575// Test that a correct MediaSessionOptions is created for an offer if
deadbeefc80741f2015-10-22 13:14:45 -07002576// OfferToReceiveVideo is set.
deadbeefab9b2d12015-10-14 11:33:11 -07002577TEST(CreateSessionOptionsTest, GetMediaSessionOptionsForOfferWithVideo) {
2578 RTCOfferAnswerOptions rtc_options;
2579 rtc_options.offer_to_receive_audio = 0;
2580 rtc_options.offer_to_receive_video = 1;
2581
2582 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08002583 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002584 EXPECT_FALSE(options.has_audio());
2585 EXPECT_TRUE(options.has_video());
2586 EXPECT_TRUE(options.bundle_enabled);
2587}
2588
2589// Test that a correct MediaSessionOptions is created for an offer if
2590// UseRtpMux is set to false.
2591TEST(CreateSessionOptionsTest,
2592 GetMediaSessionOptionsForOfferWithBundleDisabled) {
2593 RTCOfferAnswerOptions rtc_options;
2594 rtc_options.offer_to_receive_audio = 1;
2595 rtc_options.offer_to_receive_video = 1;
2596 rtc_options.use_rtp_mux = false;
2597
2598 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08002599 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002600 EXPECT_TRUE(options.has_audio());
2601 EXPECT_TRUE(options.has_video());
2602 EXPECT_FALSE(options.bundle_enabled);
2603}
2604
2605// Test that a correct MediaSessionOptions is created to restart ice if
2606// IceRestart is set. It also tests that subsequent MediaSessionOptions don't
Taylor Brandstetterf475d362016-01-08 15:35:57 -08002607// have |audio_transport_options.ice_restart| etc. set.
deadbeefab9b2d12015-10-14 11:33:11 -07002608TEST(CreateSessionOptionsTest, GetMediaSessionOptionsForOfferWithIceRestart) {
2609 RTCOfferAnswerOptions rtc_options;
2610 rtc_options.ice_restart = true;
2611
2612 cricket::MediaSessionOptions options;
deadbeef0ed85b22016-02-23 17:24:52 -08002613 options.transport_options["audio"] = cricket::TransportOptions();
2614 options.transport_options["video"] = cricket::TransportOptions();
htaaac2dea2016-03-10 13:35:55 -08002615 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeef0ed85b22016-02-23 17:24:52 -08002616 EXPECT_TRUE(options.transport_options["audio"].ice_restart);
2617 EXPECT_TRUE(options.transport_options["video"].ice_restart);
deadbeefab9b2d12015-10-14 11:33:11 -07002618
2619 rtc_options = RTCOfferAnswerOptions();
htaaac2dea2016-03-10 13:35:55 -08002620 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeef0ed85b22016-02-23 17:24:52 -08002621 EXPECT_FALSE(options.transport_options["audio"].ice_restart);
2622 EXPECT_FALSE(options.transport_options["video"].ice_restart);
deadbeefab9b2d12015-10-14 11:33:11 -07002623}
2624
2625// Test that the MediaConstraints in an answer don't affect if audio and video
2626// is offered in an offer but that if kOfferToReceiveAudio or
2627// kOfferToReceiveVideo constraints are true in an offer, the media type will be
2628// included in subsequent answers.
2629TEST(CreateSessionOptionsTest, MediaConstraintsInAnswer) {
2630 FakeConstraints answer_c;
2631 answer_c.SetMandatoryReceiveAudio(true);
2632 answer_c.SetMandatoryReceiveVideo(true);
2633
2634 cricket::MediaSessionOptions answer_options;
2635 EXPECT_TRUE(ParseConstraintsForAnswer(&answer_c, &answer_options));
2636 EXPECT_TRUE(answer_options.has_audio());
2637 EXPECT_TRUE(answer_options.has_video());
2638
deadbeefc80741f2015-10-22 13:14:45 -07002639 RTCOfferAnswerOptions rtc_offer_options;
deadbeefab9b2d12015-10-14 11:33:11 -07002640
2641 cricket::MediaSessionOptions offer_options;
htaaac2dea2016-03-10 13:35:55 -08002642 EXPECT_TRUE(
2643 ExtractMediaSessionOptions(rtc_offer_options, false, &offer_options));
deadbeefc80741f2015-10-22 13:14:45 -07002644 EXPECT_TRUE(offer_options.has_audio());
htaaac2dea2016-03-10 13:35:55 -08002645 EXPECT_TRUE(offer_options.has_video());
deadbeefab9b2d12015-10-14 11:33:11 -07002646
deadbeefc80741f2015-10-22 13:14:45 -07002647 RTCOfferAnswerOptions updated_rtc_offer_options;
2648 updated_rtc_offer_options.offer_to_receive_audio = 1;
2649 updated_rtc_offer_options.offer_to_receive_video = 1;
deadbeefab9b2d12015-10-14 11:33:11 -07002650
2651 cricket::MediaSessionOptions updated_offer_options;
htaaac2dea2016-03-10 13:35:55 -08002652 EXPECT_TRUE(ExtractMediaSessionOptions(updated_rtc_offer_options, false,
htaa2a49d92016-03-04 02:51:39 -08002653 &updated_offer_options));
deadbeefab9b2d12015-10-14 11:33:11 -07002654 EXPECT_TRUE(updated_offer_options.has_audio());
2655 EXPECT_TRUE(updated_offer_options.has_video());
2656
2657 // Since an offer has been created with both audio and video, subsequent
2658 // offers and answers should contain both audio and video.
2659 // Answers will only contain the media types that exist in the offer
2660 // regardless of the value of |updated_answer_options.has_audio| and
2661 // |updated_answer_options.has_video|.
2662 FakeConstraints updated_answer_c;
2663 answer_c.SetMandatoryReceiveAudio(false);
2664 answer_c.SetMandatoryReceiveVideo(false);
2665
2666 cricket::MediaSessionOptions updated_answer_options;
2667 EXPECT_TRUE(
2668 ParseConstraintsForAnswer(&updated_answer_c, &updated_answer_options));
2669 EXPECT_TRUE(updated_answer_options.has_audio());
2670 EXPECT_TRUE(updated_answer_options.has_video());
deadbeefab9b2d12015-10-14 11:33:11 -07002671}