blob: 4d5eb966f5e2607556e0f460621b860bd85826fd [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
Henrik Kjellander15583c12016-02-10 10:53:12 +010014#include "webrtc/api/audiotrack.h"
15#include "webrtc/api/jsepsessiondescription.h"
16#include "webrtc/api/mediastream.h"
17#include "webrtc/api/mediastreaminterface.h"
18#include "webrtc/api/peerconnection.h"
19#include "webrtc/api/peerconnectioninterface.h"
20#include "webrtc/api/rtpreceiverinterface.h"
21#include "webrtc/api/rtpsenderinterface.h"
22#include "webrtc/api/streamcollection.h"
23#ifdef WEBRTC_ANDROID
24#include "webrtc/api/test/androidtestinitializer.h"
25#endif
26#include "webrtc/api/test/fakeconstraints.h"
27#include "webrtc/api/test/fakedtlsidentitystore.h"
nisseaf510af2016-03-21 08:20:42 -070028#include "webrtc/api/test/fakevideotracksource.h"
Henrik Kjellander15583c12016-02-10 10:53:12 +010029#include "webrtc/api/test/mockpeerconnectionobservers.h"
30#include "webrtc/api/test/testsdpstrings.h"
perkja3ede6c2016-03-08 01:27:48 +010031#include "webrtc/api/videocapturertracksource.h"
Henrik Kjellander15583c12016-02-10 10:53:12 +010032#include "webrtc/api/videotrack.h"
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000033#include "webrtc/base/gunit.h"
34#include "webrtc/base/scoped_ptr.h"
35#include "webrtc/base/ssladapter.h"
36#include "webrtc/base/sslstreamadapter.h"
37#include "webrtc/base/stringutils.h"
38#include "webrtc/base/thread.h"
kjellandera96e2d72016-02-04 23:52:28 -080039#include "webrtc/media/base/fakevideocapturer.h"
40#include "webrtc/media/sctp/sctpdataengine.h"
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -080041#include "webrtc/p2p/client/fakeportallocator.h"
kjellander@webrtc.org9b8df252016-02-12 06:47:59 +010042#include "webrtc/pc/mediasession.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000043
44static const char kStreamLabel1[] = "local_stream_1";
45static const char kStreamLabel2[] = "local_stream_2";
46static const char kStreamLabel3[] = "local_stream_3";
47static const int kDefaultStunPort = 3478;
48static const char kStunAddressOnly[] = "stun:address";
49static const char kStunInvalidPort[] = "stun:address:-1";
50static const char kStunAddressPortAndMore1[] = "stun:address:port:more";
51static const char kStunAddressPortAndMore2[] = "stun:address:port more";
52static const char kTurnIceServerUri[] = "turn:user@turn.example.org";
53static const char kTurnUsername[] = "user";
54static const char kTurnPassword[] = "password";
55static const char kTurnHostname[] = "turn.example.org";
Peter Boström0c4e06b2015-10-07 12:23:21 +020056static const uint32_t kTimeout = 10000U;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000057
deadbeefab9b2d12015-10-14 11:33:11 -070058static const char kStreams[][8] = {"stream1", "stream2"};
59static const char kAudioTracks[][32] = {"audiotrack0", "audiotrack1"};
60static const char kVideoTracks[][32] = {"videotrack0", "videotrack1"};
61
deadbeef5e97fb52015-10-15 12:49:08 -070062static const char kRecvonly[] = "recvonly";
63static const char kSendrecv[] = "sendrecv";
64
deadbeefab9b2d12015-10-14 11:33:11 -070065// Reference SDP with a MediaStream with label "stream1" and audio track with
66// id "audio_1" and a video track with id "video_1;
67static const char kSdpStringWithStream1[] =
68 "v=0\r\n"
69 "o=- 0 0 IN IP4 127.0.0.1\r\n"
70 "s=-\r\n"
71 "t=0 0\r\n"
72 "a=ice-ufrag:e5785931\r\n"
73 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
74 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
75 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
76 "m=audio 1 RTP/AVPF 103\r\n"
77 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -070078 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -070079 "a=rtpmap:103 ISAC/16000\r\n"
80 "a=ssrc:1 cname:stream1\r\n"
81 "a=ssrc:1 mslabel:stream1\r\n"
82 "a=ssrc:1 label:audiotrack0\r\n"
83 "m=video 1 RTP/AVPF 120\r\n"
84 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -070085 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -070086 "a=rtpmap:120 VP8/90000\r\n"
87 "a=ssrc:2 cname:stream1\r\n"
88 "a=ssrc:2 mslabel:stream1\r\n"
89 "a=ssrc:2 label:videotrack0\r\n";
90
91// Reference SDP with two MediaStreams with label "stream1" and "stream2. Each
92// MediaStreams have one audio track and one video track.
93// This uses MSID.
94static const char kSdpStringWithStream1And2[] =
95 "v=0\r\n"
96 "o=- 0 0 IN IP4 127.0.0.1\r\n"
97 "s=-\r\n"
98 "t=0 0\r\n"
99 "a=ice-ufrag:e5785931\r\n"
100 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
101 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
102 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
103 "a=msid-semantic: WMS stream1 stream2\r\n"
104 "m=audio 1 RTP/AVPF 103\r\n"
105 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700106 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700107 "a=rtpmap:103 ISAC/16000\r\n"
108 "a=ssrc:1 cname:stream1\r\n"
109 "a=ssrc:1 msid:stream1 audiotrack0\r\n"
110 "a=ssrc:3 cname:stream2\r\n"
111 "a=ssrc:3 msid:stream2 audiotrack1\r\n"
112 "m=video 1 RTP/AVPF 120\r\n"
113 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700114 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700115 "a=rtpmap:120 VP8/0\r\n"
116 "a=ssrc:2 cname:stream1\r\n"
117 "a=ssrc:2 msid:stream1 videotrack0\r\n"
118 "a=ssrc:4 cname:stream2\r\n"
119 "a=ssrc:4 msid:stream2 videotrack1\r\n";
120
121// Reference SDP without MediaStreams. Msid is not supported.
122static const char kSdpStringWithoutStreams[] =
123 "v=0\r\n"
124 "o=- 0 0 IN IP4 127.0.0.1\r\n"
125 "s=-\r\n"
126 "t=0 0\r\n"
127 "a=ice-ufrag:e5785931\r\n"
128 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
129 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
130 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
131 "m=audio 1 RTP/AVPF 103\r\n"
132 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700133 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700134 "a=rtpmap:103 ISAC/16000\r\n"
135 "m=video 1 RTP/AVPF 120\r\n"
136 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700137 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700138 "a=rtpmap:120 VP8/90000\r\n";
139
140// Reference SDP without MediaStreams. Msid is supported.
141static const char kSdpStringWithMsidWithoutStreams[] =
142 "v=0\r\n"
143 "o=- 0 0 IN IP4 127.0.0.1\r\n"
144 "s=-\r\n"
145 "t=0 0\r\n"
146 "a=ice-ufrag:e5785931\r\n"
147 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
148 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
149 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
150 "a=msid-semantic: WMS\r\n"
151 "m=audio 1 RTP/AVPF 103\r\n"
152 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700153 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700154 "a=rtpmap:103 ISAC/16000\r\n"
155 "m=video 1 RTP/AVPF 120\r\n"
156 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700157 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700158 "a=rtpmap:120 VP8/90000\r\n";
159
160// Reference SDP without MediaStreams and audio only.
161static const char kSdpStringWithoutStreamsAudioOnly[] =
162 "v=0\r\n"
163 "o=- 0 0 IN IP4 127.0.0.1\r\n"
164 "s=-\r\n"
165 "t=0 0\r\n"
166 "a=ice-ufrag:e5785931\r\n"
167 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
168 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
169 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
170 "m=audio 1 RTP/AVPF 103\r\n"
171 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700172 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700173 "a=rtpmap:103 ISAC/16000\r\n";
174
175// Reference SENDONLY SDP without MediaStreams. Msid is not supported.
176static const char kSdpStringSendOnlyWithoutStreams[] =
177 "v=0\r\n"
178 "o=- 0 0 IN IP4 127.0.0.1\r\n"
179 "s=-\r\n"
180 "t=0 0\r\n"
181 "a=ice-ufrag:e5785931\r\n"
182 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
183 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
184 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
185 "m=audio 1 RTP/AVPF 103\r\n"
186 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700187 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700188 "a=sendonly\r\n"
189 "a=rtpmap:103 ISAC/16000\r\n"
190 "m=video 1 RTP/AVPF 120\r\n"
191 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700192 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700193 "a=sendonly\r\n"
194 "a=rtpmap:120 VP8/90000\r\n";
195
196static const char kSdpStringInit[] =
197 "v=0\r\n"
198 "o=- 0 0 IN IP4 127.0.0.1\r\n"
199 "s=-\r\n"
200 "t=0 0\r\n"
201 "a=ice-ufrag:e5785931\r\n"
202 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
203 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
204 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
205 "a=msid-semantic: WMS\r\n";
206
207static const char kSdpStringAudio[] =
208 "m=audio 1 RTP/AVPF 103\r\n"
209 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700210 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700211 "a=rtpmap:103 ISAC/16000\r\n";
212
213static const char kSdpStringVideo[] =
214 "m=video 1 RTP/AVPF 120\r\n"
215 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700216 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700217 "a=rtpmap:120 VP8/90000\r\n";
218
219static const char kSdpStringMs1Audio0[] =
220 "a=ssrc:1 cname:stream1\r\n"
221 "a=ssrc:1 msid:stream1 audiotrack0\r\n";
222
223static const char kSdpStringMs1Video0[] =
224 "a=ssrc:2 cname:stream1\r\n"
225 "a=ssrc:2 msid:stream1 videotrack0\r\n";
226
227static const char kSdpStringMs1Audio1[] =
228 "a=ssrc:3 cname:stream1\r\n"
229 "a=ssrc:3 msid:stream1 audiotrack1\r\n";
230
231static const char kSdpStringMs1Video1[] =
232 "a=ssrc:4 cname:stream1\r\n"
233 "a=ssrc:4 msid:stream1 videotrack1\r\n";
234
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000235#define MAYBE_SKIP_TEST(feature) \
236 if (!(feature())) { \
237 LOG(LS_INFO) << "Feature disabled... skipping"; \
238 return; \
239 }
240
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000241using rtc::scoped_ptr;
242using rtc::scoped_refptr;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000243using webrtc::AudioSourceInterface;
deadbeefab9b2d12015-10-14 11:33:11 -0700244using webrtc::AudioTrack;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000245using webrtc::AudioTrackInterface;
246using webrtc::DataBuffer;
247using webrtc::DataChannelInterface;
248using webrtc::FakeConstraints;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000249using webrtc::IceCandidateInterface;
deadbeefc80741f2015-10-22 13:14:45 -0700250using webrtc::MediaConstraintsInterface;
deadbeefab9b2d12015-10-14 11:33:11 -0700251using webrtc::MediaStream;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000252using webrtc::MediaStreamInterface;
253using webrtc::MediaStreamTrackInterface;
254using webrtc::MockCreateSessionDescriptionObserver;
255using webrtc::MockDataChannelObserver;
256using webrtc::MockSetSessionDescriptionObserver;
257using webrtc::MockStatsObserver;
258using webrtc::PeerConnectionInterface;
259using webrtc::PeerConnectionObserver;
deadbeefab9b2d12015-10-14 11:33:11 -0700260using webrtc::RtpReceiverInterface;
261using webrtc::RtpSenderInterface;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000262using webrtc::SdpParseError;
263using webrtc::SessionDescriptionInterface;
deadbeefab9b2d12015-10-14 11:33:11 -0700264using webrtc::StreamCollection;
265using webrtc::StreamCollectionInterface;
perkja3ede6c2016-03-08 01:27:48 +0100266using webrtc::VideoTrackSourceInterface;
deadbeefab9b2d12015-10-14 11:33:11 -0700267using webrtc::VideoTrack;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000268using webrtc::VideoTrackInterface;
269
deadbeefab9b2d12015-10-14 11:33:11 -0700270typedef PeerConnectionInterface::RTCOfferAnswerOptions RTCOfferAnswerOptions;
271
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000272namespace {
273
274// Gets the first ssrc of given content type from the ContentInfo.
275bool GetFirstSsrc(const cricket::ContentInfo* content_info, int* ssrc) {
276 if (!content_info || !ssrc) {
277 return false;
278 }
279 const cricket::MediaContentDescription* media_desc =
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000280 static_cast<const cricket::MediaContentDescription*>(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000281 content_info->description);
282 if (!media_desc || media_desc->streams().empty()) {
283 return false;
284 }
285 *ssrc = media_desc->streams().begin()->first_ssrc();
286 return true;
287}
288
289void SetSsrcToZero(std::string* sdp) {
290 const char kSdpSsrcAtribute[] = "a=ssrc:";
291 const char kSdpSsrcAtributeZero[] = "a=ssrc:0";
292 size_t ssrc_pos = 0;
293 while ((ssrc_pos = sdp->find(kSdpSsrcAtribute, ssrc_pos)) !=
294 std::string::npos) {
295 size_t end_ssrc = sdp->find(" ", ssrc_pos);
296 sdp->replace(ssrc_pos, end_ssrc - ssrc_pos, kSdpSsrcAtributeZero);
297 ssrc_pos = end_ssrc;
298 }
299}
300
deadbeefab9b2d12015-10-14 11:33:11 -0700301// Check if |streams| contains the specified track.
302bool ContainsTrack(const std::vector<cricket::StreamParams>& streams,
303 const std::string& stream_label,
304 const std::string& track_id) {
305 for (const cricket::StreamParams& params : streams) {
306 if (params.sync_label == stream_label && params.id == track_id) {
307 return true;
308 }
309 }
310 return false;
311}
312
313// Check if |senders| contains the specified sender, by id.
314bool ContainsSender(
315 const std::vector<rtc::scoped_refptr<RtpSenderInterface>>& senders,
316 const std::string& id) {
317 for (const auto& sender : senders) {
318 if (sender->id() == id) {
319 return true;
320 }
321 }
322 return false;
323}
324
325// Create a collection of streams.
326// CreateStreamCollection(1) creates a collection that
327// correspond to kSdpStringWithStream1.
328// CreateStreamCollection(2) correspond to kSdpStringWithStream1And2.
329rtc::scoped_refptr<StreamCollection> CreateStreamCollection(
330 int number_of_streams) {
331 rtc::scoped_refptr<StreamCollection> local_collection(
332 StreamCollection::Create());
333
334 for (int i = 0; i < number_of_streams; ++i) {
335 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream(
336 webrtc::MediaStream::Create(kStreams[i]));
337
338 // Add a local audio track.
339 rtc::scoped_refptr<webrtc::AudioTrackInterface> audio_track(
340 webrtc::AudioTrack::Create(kAudioTracks[i], nullptr));
341 stream->AddTrack(audio_track);
342
343 // Add a local video track.
344 rtc::scoped_refptr<webrtc::VideoTrackInterface> video_track(
nisseaf510af2016-03-21 08:20:42 -0700345 webrtc::VideoTrack::Create(kVideoTracks[i],
346 webrtc::FakeVideoTrackSource::Create()));
deadbeefab9b2d12015-10-14 11:33:11 -0700347 stream->AddTrack(video_track);
348
349 local_collection->AddStream(stream);
350 }
351 return local_collection;
352}
353
354// Check equality of StreamCollections.
355bool CompareStreamCollections(StreamCollectionInterface* s1,
356 StreamCollectionInterface* s2) {
357 if (s1 == nullptr || s2 == nullptr || s1->count() != s2->count()) {
358 return false;
359 }
360
361 for (size_t i = 0; i != s1->count(); ++i) {
362 if (s1->at(i)->label() != s2->at(i)->label()) {
363 return false;
364 }
365 webrtc::AudioTrackVector audio_tracks1 = s1->at(i)->GetAudioTracks();
366 webrtc::AudioTrackVector audio_tracks2 = s2->at(i)->GetAudioTracks();
367 webrtc::VideoTrackVector video_tracks1 = s1->at(i)->GetVideoTracks();
368 webrtc::VideoTrackVector video_tracks2 = s2->at(i)->GetVideoTracks();
369
370 if (audio_tracks1.size() != audio_tracks2.size()) {
371 return false;
372 }
373 for (size_t j = 0; j != audio_tracks1.size(); ++j) {
374 if (audio_tracks1[j]->id() != audio_tracks2[j]->id()) {
375 return false;
376 }
377 }
378 if (video_tracks1.size() != video_tracks2.size()) {
379 return false;
380 }
381 for (size_t j = 0; j != video_tracks1.size(); ++j) {
382 if (video_tracks1[j]->id() != video_tracks2[j]->id()) {
383 return false;
384 }
385 }
386 }
387 return true;
388}
389
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000390class MockPeerConnectionObserver : public PeerConnectionObserver {
391 public:
deadbeefab9b2d12015-10-14 11:33:11 -0700392 MockPeerConnectionObserver() : remote_streams_(StreamCollection::Create()) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000393 ~MockPeerConnectionObserver() {
394 }
395 void SetPeerConnectionInterface(PeerConnectionInterface* pc) {
396 pc_ = pc;
397 if (pc) {
398 state_ = pc_->signaling_state();
399 }
400 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000401 virtual void OnSignalingChange(
402 PeerConnectionInterface::SignalingState new_state) {
403 EXPECT_EQ(pc_->signaling_state(), new_state);
404 state_ = new_state;
405 }
406 // TODO(bemasc): Remove this once callers transition to OnIceGatheringChange.
407 virtual void OnStateChange(StateType state_changed) {
408 if (pc_.get() == NULL)
409 return;
410 switch (state_changed) {
411 case kSignalingState:
412 // OnSignalingChange and OnStateChange(kSignalingState) should always
413 // be called approximately simultaneously. To ease testing, we require
414 // that they always be called in that order. This check verifies
415 // that OnSignalingChange has just been called.
416 EXPECT_EQ(pc_->signaling_state(), state_);
417 break;
418 case kIceState:
419 ADD_FAILURE();
420 break;
421 default:
422 ADD_FAILURE();
423 break;
424 }
425 }
deadbeefab9b2d12015-10-14 11:33:11 -0700426
427 MediaStreamInterface* RemoteStream(const std::string& label) {
428 return remote_streams_->find(label);
429 }
430 StreamCollectionInterface* remote_streams() const { return remote_streams_; }
perkjdfb769d2016-02-09 03:09:43 -0800431 void OnAddStream(MediaStreamInterface* stream) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000432 last_added_stream_ = stream;
deadbeefab9b2d12015-10-14 11:33:11 -0700433 remote_streams_->AddStream(stream);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000434 }
perkjdfb769d2016-02-09 03:09:43 -0800435 void OnRemoveStream(MediaStreamInterface* stream) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000436 last_removed_stream_ = stream;
deadbeefab9b2d12015-10-14 11:33:11 -0700437 remote_streams_->RemoveStream(stream);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000438 }
perkjdfb769d2016-02-09 03:09:43 -0800439 void OnRenegotiationNeeded() override { renegotiation_needed_ = true; }
440 void OnDataChannel(DataChannelInterface* data_channel) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000441 last_datachannel_ = data_channel;
442 }
443
perkjdfb769d2016-02-09 03:09:43 -0800444 void OnIceConnectionChange(
445 PeerConnectionInterface::IceConnectionState new_state) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000446 EXPECT_EQ(pc_->ice_connection_state(), new_state);
447 }
perkjdfb769d2016-02-09 03:09:43 -0800448 void OnIceGatheringChange(
449 PeerConnectionInterface::IceGatheringState new_state) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000450 EXPECT_EQ(pc_->ice_gathering_state(), new_state);
perkjdfb769d2016-02-09 03:09:43 -0800451 ice_complete_ = new_state == PeerConnectionInterface::kIceGatheringComplete;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000452 }
perkjdfb769d2016-02-09 03:09:43 -0800453 void OnIceCandidate(const webrtc::IceCandidateInterface* candidate) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000454 EXPECT_NE(PeerConnectionInterface::kIceGatheringNew,
455 pc_->ice_gathering_state());
456
457 std::string sdp;
458 EXPECT_TRUE(candidate->ToString(&sdp));
459 EXPECT_LT(0u, sdp.size());
460 last_candidate_.reset(webrtc::CreateIceCandidate(candidate->sdp_mid(),
461 candidate->sdp_mline_index(), sdp, NULL));
462 EXPECT_TRUE(last_candidate_.get() != NULL);
463 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000464
465 // Returns the label of the last added stream.
466 // Empty string if no stream have been added.
467 std::string GetLastAddedStreamLabel() {
468 if (last_added_stream_.get())
469 return last_added_stream_->label();
470 return "";
471 }
472 std::string GetLastRemovedStreamLabel() {
473 if (last_removed_stream_.get())
474 return last_removed_stream_->label();
475 return "";
476 }
477
478 scoped_refptr<PeerConnectionInterface> pc_;
479 PeerConnectionInterface::SignalingState state_;
480 scoped_ptr<IceCandidateInterface> last_candidate_;
481 scoped_refptr<DataChannelInterface> last_datachannel_;
deadbeefab9b2d12015-10-14 11:33:11 -0700482 rtc::scoped_refptr<StreamCollection> remote_streams_;
483 bool renegotiation_needed_ = false;
484 bool ice_complete_ = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000485
486 private:
487 scoped_refptr<MediaStreamInterface> last_added_stream_;
488 scoped_refptr<MediaStreamInterface> last_removed_stream_;
489};
490
491} // namespace
deadbeefab9b2d12015-10-14 11:33:11 -0700492
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000493class PeerConnectionInterfaceTest : public testing::Test {
494 protected:
phoglund37ebcf02016-01-08 05:04:57 -0800495 PeerConnectionInterfaceTest() {
496#ifdef WEBRTC_ANDROID
497 webrtc::InitializeAndroidObjects();
498#endif
499 }
500
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000501 virtual void SetUp() {
502 pc_factory_ = webrtc::CreatePeerConnectionFactory(
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000503 rtc::Thread::Current(), rtc::Thread::Current(), NULL, NULL,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000504 NULL);
505 ASSERT_TRUE(pc_factory_.get() != NULL);
506 }
507
508 void CreatePeerConnection() {
509 CreatePeerConnection("", "", NULL);
510 }
511
512 void CreatePeerConnection(webrtc::MediaConstraintsInterface* constraints) {
513 CreatePeerConnection("", "", constraints);
514 }
515
516 void CreatePeerConnection(const std::string& uri,
517 const std::string& password,
518 webrtc::MediaConstraintsInterface* constraints) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800519 PeerConnectionInterface::RTCConfiguration config;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000520 PeerConnectionInterface::IceServer server;
deadbeef0a6c4ca2015-10-06 11:38:28 -0700521 if (!uri.empty()) {
522 server.uri = uri;
523 server.password = password;
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800524 config.servers.push_back(server);
deadbeef0a6c4ca2015-10-06 11:38:28 -0700525 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000526
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800527 rtc::scoped_ptr<cricket::FakePortAllocator> port_allocator(
528 new cricket::FakePortAllocator(rtc::Thread::Current(), nullptr));
529 port_allocator_ = port_allocator.get();
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000530
buildbot@webrtc.org61c1b8e2014-04-09 06:06:38 +0000531 // DTLS does not work in a loopback call, so is disabled for most of the
532 // tests in this file. We only create a FakeIdentityService if the test
533 // explicitly sets the constraint.
jiayl@webrtc.org61e00b02015-03-04 22:17:38 +0000534 FakeConstraints default_constraints;
535 if (!constraints) {
536 constraints = &default_constraints;
537
538 default_constraints.AddMandatory(
539 webrtc::MediaConstraintsInterface::kEnableDtlsSrtp, false);
540 }
541
Henrik Boström5e56c592015-08-11 10:33:13 +0200542 scoped_ptr<webrtc::DtlsIdentityStoreInterface> dtls_identity_store;
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000543 bool dtls;
544 if (FindConstraint(constraints,
545 webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
546 &dtls,
Henrik Boström5e56c592015-08-11 10:33:13 +0200547 nullptr) && dtls) {
548 dtls_identity_store.reset(new FakeDtlsIdentityStore());
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000549 }
kwiberg0eb15ed2015-12-17 03:04:15 -0800550 pc_ = pc_factory_->CreatePeerConnection(
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800551 config, constraints, std::move(port_allocator),
kwiberg0eb15ed2015-12-17 03:04:15 -0800552 std::move(dtls_identity_store), &observer_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000553 ASSERT_TRUE(pc_.get() != NULL);
554 observer_.SetPeerConnectionInterface(pc_.get());
555 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
556 }
557
deadbeef0a6c4ca2015-10-06 11:38:28 -0700558 void CreatePeerConnectionExpectFail(const std::string& uri) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800559 PeerConnectionInterface::RTCConfiguration config;
deadbeef0a6c4ca2015-10-06 11:38:28 -0700560 PeerConnectionInterface::IceServer server;
deadbeef0a6c4ca2015-10-06 11:38:28 -0700561 server.uri = uri;
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800562 config.servers.push_back(server);
deadbeef0a6c4ca2015-10-06 11:38:28 -0700563
deadbeef0a6c4ca2015-10-06 11:38:28 -0700564 scoped_refptr<PeerConnectionInterface> pc;
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800565 pc = pc_factory_->CreatePeerConnection(config, nullptr, nullptr, nullptr,
566 &observer_);
567 EXPECT_EQ(nullptr, pc);
deadbeef0a6c4ca2015-10-06 11:38:28 -0700568 }
569
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000570 void CreatePeerConnectionWithDifferentConfigurations() {
571 CreatePeerConnection(kStunAddressOnly, "", NULL);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800572 EXPECT_EQ(1u, port_allocator_->stun_servers().size());
573 EXPECT_EQ(0u, port_allocator_->turn_servers().size());
574 EXPECT_EQ("address", port_allocator_->stun_servers().begin()->hostname());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000575 EXPECT_EQ(kDefaultStunPort,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800576 port_allocator_->stun_servers().begin()->port());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000577
deadbeef0a6c4ca2015-10-06 11:38:28 -0700578 CreatePeerConnectionExpectFail(kStunInvalidPort);
579 CreatePeerConnectionExpectFail(kStunAddressPortAndMore1);
580 CreatePeerConnectionExpectFail(kStunAddressPortAndMore2);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000581
582 CreatePeerConnection(kTurnIceServerUri, kTurnPassword, NULL);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800583 EXPECT_EQ(0u, port_allocator_->stun_servers().size());
584 EXPECT_EQ(1u, port_allocator_->turn_servers().size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000585 EXPECT_EQ(kTurnUsername,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800586 port_allocator_->turn_servers()[0].credentials.username);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000587 EXPECT_EQ(kTurnPassword,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800588 port_allocator_->turn_servers()[0].credentials.password);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000589 EXPECT_EQ(kTurnHostname,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800590 port_allocator_->turn_servers()[0].ports[0].address.hostname());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000591 }
592
593 void ReleasePeerConnection() {
594 pc_ = NULL;
595 observer_.SetPeerConnectionInterface(NULL);
596 }
597
deadbeefab9b2d12015-10-14 11:33:11 -0700598 void AddVideoStream(const std::string& label) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000599 // Create a local stream.
600 scoped_refptr<MediaStreamInterface> stream(
601 pc_factory_->CreateLocalMediaStream(label));
perkja3ede6c2016-03-08 01:27:48 +0100602 scoped_refptr<VideoTrackSourceInterface> video_source(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000603 pc_factory_->CreateVideoSource(new cricket::FakeVideoCapturer(), NULL));
604 scoped_refptr<VideoTrackInterface> video_track(
605 pc_factory_->CreateVideoTrack(label + "v0", video_source));
606 stream->AddTrack(video_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +0000607 EXPECT_TRUE(pc_->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000608 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
609 observer_.renegotiation_needed_ = false;
610 }
611
612 void AddVoiceStream(const std::string& label) {
613 // Create a local stream.
614 scoped_refptr<MediaStreamInterface> stream(
615 pc_factory_->CreateLocalMediaStream(label));
616 scoped_refptr<AudioTrackInterface> audio_track(
617 pc_factory_->CreateAudioTrack(label + "a0", NULL));
618 stream->AddTrack(audio_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +0000619 EXPECT_TRUE(pc_->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000620 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
621 observer_.renegotiation_needed_ = false;
622 }
623
624 void AddAudioVideoStream(const std::string& stream_label,
625 const std::string& audio_track_label,
626 const std::string& video_track_label) {
627 // Create a local stream.
628 scoped_refptr<MediaStreamInterface> stream(
629 pc_factory_->CreateLocalMediaStream(stream_label));
630 scoped_refptr<AudioTrackInterface> audio_track(
631 pc_factory_->CreateAudioTrack(
632 audio_track_label, static_cast<AudioSourceInterface*>(NULL)));
633 stream->AddTrack(audio_track.get());
634 scoped_refptr<VideoTrackInterface> video_track(
nisseaf510af2016-03-21 08:20:42 -0700635 pc_factory_->CreateVideoTrack(
636 video_track_label,
637 pc_factory_->CreateVideoSource(new cricket::FakeVideoCapturer())));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000638 stream->AddTrack(video_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +0000639 EXPECT_TRUE(pc_->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000640 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
641 observer_.renegotiation_needed_ = false;
642 }
643
kwiberg2bbff992016-03-16 11:03:04 -0700644 bool DoCreateOfferAnswer(rtc::scoped_ptr<SessionDescriptionInterface>* desc,
deadbeefc80741f2015-10-22 13:14:45 -0700645 bool offer,
646 MediaConstraintsInterface* constraints) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000647 rtc::scoped_refptr<MockCreateSessionDescriptionObserver>
648 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000649 MockCreateSessionDescriptionObserver>());
650 if (offer) {
deadbeefc80741f2015-10-22 13:14:45 -0700651 pc_->CreateOffer(observer, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000652 } else {
deadbeefc80741f2015-10-22 13:14:45 -0700653 pc_->CreateAnswer(observer, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000654 }
655 EXPECT_EQ_WAIT(true, observer->called(), kTimeout);
kwiberg2bbff992016-03-16 11:03:04 -0700656 desc->reset(observer->release_desc());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000657 return observer->result();
658 }
659
kwiberg2bbff992016-03-16 11:03:04 -0700660 bool DoCreateOffer(rtc::scoped_ptr<SessionDescriptionInterface>* desc,
deadbeefc80741f2015-10-22 13:14:45 -0700661 MediaConstraintsInterface* constraints) {
662 return DoCreateOfferAnswer(desc, true, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000663 }
664
kwiberg2bbff992016-03-16 11:03:04 -0700665 bool DoCreateAnswer(rtc::scoped_ptr<SessionDescriptionInterface>* desc,
deadbeefc80741f2015-10-22 13:14:45 -0700666 MediaConstraintsInterface* constraints) {
667 return DoCreateOfferAnswer(desc, false, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000668 }
669
670 bool DoSetSessionDescription(SessionDescriptionInterface* desc, bool local) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000671 rtc::scoped_refptr<MockSetSessionDescriptionObserver>
672 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000673 MockSetSessionDescriptionObserver>());
674 if (local) {
675 pc_->SetLocalDescription(observer, desc);
676 } else {
677 pc_->SetRemoteDescription(observer, desc);
678 }
679 EXPECT_EQ_WAIT(true, observer->called(), kTimeout);
680 return observer->result();
681 }
682
683 bool DoSetLocalDescription(SessionDescriptionInterface* desc) {
684 return DoSetSessionDescription(desc, true);
685 }
686
687 bool DoSetRemoteDescription(SessionDescriptionInterface* desc) {
688 return DoSetSessionDescription(desc, false);
689 }
690
691 // Calls PeerConnection::GetStats and check the return value.
692 // It does not verify the values in the StatReports since a RTCP packet might
693 // be required.
694 bool DoGetStats(MediaStreamTrackInterface* track) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000695 rtc::scoped_refptr<MockStatsObserver> observer(
696 new rtc::RefCountedObject<MockStatsObserver>());
jiayl@webrtc.orgdb41b4d2014-03-03 21:30:06 +0000697 if (!pc_->GetStats(
698 observer, track, PeerConnectionInterface::kStatsOutputLevelStandard))
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000699 return false;
700 EXPECT_TRUE_WAIT(observer->called(), kTimeout);
701 return observer->called();
702 }
703
704 void InitiateCall() {
705 CreatePeerConnection();
706 // Create a local stream with audio&video tracks.
707 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
708 CreateOfferReceiveAnswer();
709 }
710
711 // Verify that RTP Header extensions has been negotiated for audio and video.
712 void VerifyRemoteRtpHeaderExtensions() {
713 const cricket::MediaContentDescription* desc =
714 cricket::GetFirstAudioContentDescription(
715 pc_->remote_description()->description());
716 ASSERT_TRUE(desc != NULL);
717 EXPECT_GT(desc->rtp_header_extensions().size(), 0u);
718
719 desc = cricket::GetFirstVideoContentDescription(
720 pc_->remote_description()->description());
721 ASSERT_TRUE(desc != NULL);
722 EXPECT_GT(desc->rtp_header_extensions().size(), 0u);
723 }
724
725 void CreateOfferAsRemoteDescription() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000726 rtc::scoped_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -0700727 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000728 std::string sdp;
729 EXPECT_TRUE(offer->ToString(&sdp));
730 SessionDescriptionInterface* remote_offer =
731 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
732 sdp, NULL);
733 EXPECT_TRUE(DoSetRemoteDescription(remote_offer));
734 EXPECT_EQ(PeerConnectionInterface::kHaveRemoteOffer, observer_.state_);
735 }
736
deadbeefab9b2d12015-10-14 11:33:11 -0700737 void CreateAndSetRemoteOffer(const std::string& sdp) {
738 SessionDescriptionInterface* remote_offer =
739 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
740 sdp, nullptr);
741 EXPECT_TRUE(DoSetRemoteDescription(remote_offer));
742 EXPECT_EQ(PeerConnectionInterface::kHaveRemoteOffer, observer_.state_);
743 }
744
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000745 void CreateAnswerAsLocalDescription() {
746 scoped_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -0700747 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000748
749 // TODO(perkj): Currently SetLocalDescription fails if any parameters in an
750 // audio codec change, even if the parameter has nothing to do with
751 // receiving. Not all parameters are serialized to SDP.
752 // Since CreatePrAnswerAsLocalDescription serialize/deserialize
753 // the SessionDescription, it is necessary to do that here to in order to
754 // get ReceiveOfferCreatePrAnswerAndAnswer and RenegotiateAudioOnly to pass.
755 // https://code.google.com/p/webrtc/issues/detail?id=1356
756 std::string sdp;
757 EXPECT_TRUE(answer->ToString(&sdp));
758 SessionDescriptionInterface* new_answer =
759 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
760 sdp, NULL);
761 EXPECT_TRUE(DoSetLocalDescription(new_answer));
762 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
763 }
764
765 void CreatePrAnswerAsLocalDescription() {
766 scoped_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -0700767 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000768
769 std::string sdp;
770 EXPECT_TRUE(answer->ToString(&sdp));
771 SessionDescriptionInterface* pr_answer =
772 webrtc::CreateSessionDescription(SessionDescriptionInterface::kPrAnswer,
773 sdp, NULL);
774 EXPECT_TRUE(DoSetLocalDescription(pr_answer));
775 EXPECT_EQ(PeerConnectionInterface::kHaveLocalPrAnswer, observer_.state_);
776 }
777
778 void CreateOfferReceiveAnswer() {
779 CreateOfferAsLocalDescription();
780 std::string sdp;
781 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
782 CreateAnswerAsRemoteDescription(sdp);
783 }
784
785 void CreateOfferAsLocalDescription() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000786 rtc::scoped_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -0700787 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000788 // TODO(perkj): Currently SetLocalDescription fails if any parameters in an
789 // audio codec change, even if the parameter has nothing to do with
790 // receiving. Not all parameters are serialized to SDP.
791 // Since CreatePrAnswerAsLocalDescription serialize/deserialize
792 // the SessionDescription, it is necessary to do that here to in order to
793 // get ReceiveOfferCreatePrAnswerAndAnswer and RenegotiateAudioOnly to pass.
794 // https://code.google.com/p/webrtc/issues/detail?id=1356
795 std::string sdp;
796 EXPECT_TRUE(offer->ToString(&sdp));
797 SessionDescriptionInterface* new_offer =
798 webrtc::CreateSessionDescription(
799 SessionDescriptionInterface::kOffer,
800 sdp, NULL);
801
802 EXPECT_TRUE(DoSetLocalDescription(new_offer));
803 EXPECT_EQ(PeerConnectionInterface::kHaveLocalOffer, observer_.state_);
mallinath@webrtc.org68cbd012014-01-22 00:16:46 +0000804 // Wait for the ice_complete message, so that SDP will have candidates.
805 EXPECT_TRUE_WAIT(observer_.ice_complete_, kTimeout);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000806 }
807
deadbeefab9b2d12015-10-14 11:33:11 -0700808 void CreateAnswerAsRemoteDescription(const std::string& sdp) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000809 webrtc::JsepSessionDescription* answer = new webrtc::JsepSessionDescription(
810 SessionDescriptionInterface::kAnswer);
deadbeefab9b2d12015-10-14 11:33:11 -0700811 EXPECT_TRUE(answer->Initialize(sdp, NULL));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000812 EXPECT_TRUE(DoSetRemoteDescription(answer));
813 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
814 }
815
deadbeefab9b2d12015-10-14 11:33:11 -0700816 void CreatePrAnswerAndAnswerAsRemoteDescription(const std::string& sdp) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000817 webrtc::JsepSessionDescription* pr_answer =
818 new webrtc::JsepSessionDescription(
819 SessionDescriptionInterface::kPrAnswer);
deadbeefab9b2d12015-10-14 11:33:11 -0700820 EXPECT_TRUE(pr_answer->Initialize(sdp, NULL));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000821 EXPECT_TRUE(DoSetRemoteDescription(pr_answer));
822 EXPECT_EQ(PeerConnectionInterface::kHaveRemotePrAnswer, observer_.state_);
823 webrtc::JsepSessionDescription* answer =
824 new webrtc::JsepSessionDescription(
825 SessionDescriptionInterface::kAnswer);
deadbeefab9b2d12015-10-14 11:33:11 -0700826 EXPECT_TRUE(answer->Initialize(sdp, NULL));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000827 EXPECT_TRUE(DoSetRemoteDescription(answer));
828 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
829 }
830
831 // Help function used for waiting until a the last signaled remote stream has
832 // the same label as |stream_label|. In a few of the tests in this file we
833 // answer with the same session description as we offer and thus we can
834 // check if OnAddStream have been called with the same stream as we offer to
835 // send.
836 void WaitAndVerifyOnAddStream(const std::string& stream_label) {
837 EXPECT_EQ_WAIT(stream_label, observer_.GetLastAddedStreamLabel(), kTimeout);
838 }
839
840 // Creates an offer and applies it as a local session description.
841 // Creates an answer with the same SDP an the offer but removes all lines
842 // that start with a:ssrc"
843 void CreateOfferReceiveAnswerWithoutSsrc() {
844 CreateOfferAsLocalDescription();
845 std::string sdp;
846 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
847 SetSsrcToZero(&sdp);
848 CreateAnswerAsRemoteDescription(sdp);
849 }
850
deadbeefab9b2d12015-10-14 11:33:11 -0700851 // This function creates a MediaStream with label kStreams[0] and
852 // |number_of_audio_tracks| and |number_of_video_tracks| tracks and the
853 // corresponding SessionDescriptionInterface. The SessionDescriptionInterface
kwiberg2bbff992016-03-16 11:03:04 -0700854 // is returned and the MediaStream is stored in
deadbeefab9b2d12015-10-14 11:33:11 -0700855 // |reference_collection_|
kwiberg2bbff992016-03-16 11:03:04 -0700856 rtc::scoped_ptr<SessionDescriptionInterface>
857 CreateSessionDescriptionAndReference(size_t number_of_audio_tracks,
858 size_t number_of_video_tracks) {
859 EXPECT_LE(number_of_audio_tracks, 2u);
860 EXPECT_LE(number_of_video_tracks, 2u);
deadbeefab9b2d12015-10-14 11:33:11 -0700861
862 reference_collection_ = StreamCollection::Create();
863 std::string sdp_ms1 = std::string(kSdpStringInit);
864
865 std::string mediastream_label = kStreams[0];
866
867 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream(
868 webrtc::MediaStream::Create(mediastream_label));
869 reference_collection_->AddStream(stream);
870
871 if (number_of_audio_tracks > 0) {
872 sdp_ms1 += std::string(kSdpStringAudio);
873 sdp_ms1 += std::string(kSdpStringMs1Audio0);
874 AddAudioTrack(kAudioTracks[0], stream);
875 }
876 if (number_of_audio_tracks > 1) {
877 sdp_ms1 += kSdpStringMs1Audio1;
878 AddAudioTrack(kAudioTracks[1], stream);
879 }
880
881 if (number_of_video_tracks > 0) {
882 sdp_ms1 += std::string(kSdpStringVideo);
883 sdp_ms1 += std::string(kSdpStringMs1Video0);
884 AddVideoTrack(kVideoTracks[0], stream);
885 }
886 if (number_of_video_tracks > 1) {
887 sdp_ms1 += kSdpStringMs1Video1;
888 AddVideoTrack(kVideoTracks[1], stream);
889 }
890
kwiberg2bbff992016-03-16 11:03:04 -0700891 return rtc::scoped_ptr<SessionDescriptionInterface>(
892 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
893 sdp_ms1, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -0700894 }
895
896 void AddAudioTrack(const std::string& track_id,
897 MediaStreamInterface* stream) {
898 rtc::scoped_refptr<webrtc::AudioTrackInterface> audio_track(
899 webrtc::AudioTrack::Create(track_id, nullptr));
900 ASSERT_TRUE(stream->AddTrack(audio_track));
901 }
902
903 void AddVideoTrack(const std::string& track_id,
904 MediaStreamInterface* stream) {
905 rtc::scoped_refptr<webrtc::VideoTrackInterface> video_track(
nisseaf510af2016-03-21 08:20:42 -0700906 webrtc::VideoTrack::Create(track_id,
907 webrtc::FakeVideoTrackSource::Create()));
deadbeefab9b2d12015-10-14 11:33:11 -0700908 ASSERT_TRUE(stream->AddTrack(video_track));
909 }
910
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800911 cricket::FakePortAllocator* port_allocator_ = nullptr;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000912 scoped_refptr<webrtc::PeerConnectionFactoryInterface> pc_factory_;
913 scoped_refptr<PeerConnectionInterface> pc_;
914 MockPeerConnectionObserver observer_;
deadbeefab9b2d12015-10-14 11:33:11 -0700915 rtc::scoped_refptr<StreamCollection> reference_collection_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000916};
917
918TEST_F(PeerConnectionInterfaceTest,
919 CreatePeerConnectionWithDifferentConfigurations) {
920 CreatePeerConnectionWithDifferentConfigurations();
921}
922
923TEST_F(PeerConnectionInterfaceTest, AddStreams) {
924 CreatePeerConnection();
deadbeefab9b2d12015-10-14 11:33:11 -0700925 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000926 AddVoiceStream(kStreamLabel2);
927 ASSERT_EQ(2u, pc_->local_streams()->count());
928
wu@webrtc.org9dba5252013-08-05 20:36:57 +0000929 // Test we can add multiple local streams to one peerconnection.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000930 scoped_refptr<MediaStreamInterface> stream(
931 pc_factory_->CreateLocalMediaStream(kStreamLabel3));
932 scoped_refptr<AudioTrackInterface> audio_track(
933 pc_factory_->CreateAudioTrack(
934 kStreamLabel3, static_cast<AudioSourceInterface*>(NULL)));
935 stream->AddTrack(audio_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +0000936 EXPECT_TRUE(pc_->AddStream(stream));
wu@webrtc.org9dba5252013-08-05 20:36:57 +0000937 EXPECT_EQ(3u, pc_->local_streams()->count());
938
939 // Remove the third stream.
940 pc_->RemoveStream(pc_->local_streams()->at(2));
941 EXPECT_EQ(2u, pc_->local_streams()->count());
942
943 // Remove the second stream.
944 pc_->RemoveStream(pc_->local_streams()->at(1));
945 EXPECT_EQ(1u, pc_->local_streams()->count());
946
947 // Remove the first stream.
948 pc_->RemoveStream(pc_->local_streams()->at(0));
949 EXPECT_EQ(0u, pc_->local_streams()->count());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000950}
951
deadbeefab9b2d12015-10-14 11:33:11 -0700952// Test that the created offer includes streams we added.
953TEST_F(PeerConnectionInterfaceTest, AddedStreamsPresentInOffer) {
954 CreatePeerConnection();
955 AddAudioVideoStream(kStreamLabel1, "audio_track", "video_track");
956 scoped_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -0700957 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -0700958
959 const cricket::ContentInfo* audio_content =
960 cricket::GetFirstAudioContent(offer->description());
961 const cricket::AudioContentDescription* audio_desc =
962 static_cast<const cricket::AudioContentDescription*>(
963 audio_content->description);
964 EXPECT_TRUE(
965 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
966
967 const cricket::ContentInfo* video_content =
968 cricket::GetFirstVideoContent(offer->description());
969 const cricket::VideoContentDescription* video_desc =
970 static_cast<const cricket::VideoContentDescription*>(
971 video_content->description);
972 EXPECT_TRUE(
973 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
974
975 // Add another stream and ensure the offer includes both the old and new
976 // streams.
977 AddAudioVideoStream(kStreamLabel2, "audio_track2", "video_track2");
kwiberg2bbff992016-03-16 11:03:04 -0700978 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -0700979
980 audio_content = cricket::GetFirstAudioContent(offer->description());
981 audio_desc = static_cast<const cricket::AudioContentDescription*>(
982 audio_content->description);
983 EXPECT_TRUE(
984 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
985 EXPECT_TRUE(
986 ContainsTrack(audio_desc->streams(), kStreamLabel2, "audio_track2"));
987
988 video_content = cricket::GetFirstVideoContent(offer->description());
989 video_desc = static_cast<const cricket::VideoContentDescription*>(
990 video_content->description);
991 EXPECT_TRUE(
992 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
993 EXPECT_TRUE(
994 ContainsTrack(video_desc->streams(), kStreamLabel2, "video_track2"));
995}
996
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000997TEST_F(PeerConnectionInterfaceTest, RemoveStream) {
998 CreatePeerConnection();
deadbeefab9b2d12015-10-14 11:33:11 -0700999 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001000 ASSERT_EQ(1u, pc_->local_streams()->count());
1001 pc_->RemoveStream(pc_->local_streams()->at(0));
1002 EXPECT_EQ(0u, pc_->local_streams()->count());
1003}
1004
deadbeefe1f9d832016-01-14 15:35:42 -08001005// Test for AddTrack and RemoveTrack methods.
1006// Tests that the created offer includes tracks we added,
1007// and that the RtpSenders are created correctly.
1008// Also tests that RemoveTrack removes the tracks from subsequent offers.
1009TEST_F(PeerConnectionInterfaceTest, AddTrackRemoveTrack) {
1010 CreatePeerConnection();
1011 // Create a dummy stream, so tracks share a stream label.
1012 scoped_refptr<MediaStreamInterface> stream(
1013 pc_factory_->CreateLocalMediaStream(kStreamLabel1));
1014 std::vector<MediaStreamInterface*> stream_list;
1015 stream_list.push_back(stream.get());
1016 scoped_refptr<AudioTrackInterface> audio_track(
1017 pc_factory_->CreateAudioTrack("audio_track", nullptr));
nisseaf510af2016-03-21 08:20:42 -07001018 scoped_refptr<VideoTrackInterface> video_track(pc_factory_->CreateVideoTrack(
1019 "video_track",
1020 pc_factory_->CreateVideoSource(new cricket::FakeVideoCapturer())));
deadbeefe1f9d832016-01-14 15:35:42 -08001021 auto audio_sender = pc_->AddTrack(audio_track, stream_list);
1022 auto video_sender = pc_->AddTrack(video_track, stream_list);
1023 EXPECT_EQ(kStreamLabel1, audio_sender->stream_id());
1024 EXPECT_EQ("audio_track", audio_sender->id());
1025 EXPECT_EQ(audio_track, audio_sender->track());
1026 EXPECT_EQ(kStreamLabel1, video_sender->stream_id());
1027 EXPECT_EQ("video_track", video_sender->id());
1028 EXPECT_EQ(video_track, video_sender->track());
1029
1030 // Now create an offer and check for the senders.
1031 scoped_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001032 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefe1f9d832016-01-14 15:35:42 -08001033
1034 const cricket::ContentInfo* audio_content =
1035 cricket::GetFirstAudioContent(offer->description());
1036 const cricket::AudioContentDescription* audio_desc =
1037 static_cast<const cricket::AudioContentDescription*>(
1038 audio_content->description);
1039 EXPECT_TRUE(
1040 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
1041
1042 const cricket::ContentInfo* video_content =
1043 cricket::GetFirstVideoContent(offer->description());
1044 const cricket::VideoContentDescription* video_desc =
1045 static_cast<const cricket::VideoContentDescription*>(
1046 video_content->description);
1047 EXPECT_TRUE(
1048 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1049
1050 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
1051
1052 // Now try removing the tracks.
1053 EXPECT_TRUE(pc_->RemoveTrack(audio_sender));
1054 EXPECT_TRUE(pc_->RemoveTrack(video_sender));
1055
1056 // Create a new offer and ensure it doesn't contain the removed senders.
kwiberg2bbff992016-03-16 11:03:04 -07001057 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefe1f9d832016-01-14 15:35:42 -08001058
1059 audio_content = cricket::GetFirstAudioContent(offer->description());
1060 audio_desc = static_cast<const cricket::AudioContentDescription*>(
1061 audio_content->description);
1062 EXPECT_FALSE(
1063 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
1064
1065 video_content = cricket::GetFirstVideoContent(offer->description());
1066 video_desc = static_cast<const cricket::VideoContentDescription*>(
1067 video_content->description);
1068 EXPECT_FALSE(
1069 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1070
1071 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
1072
1073 // Calling RemoveTrack on a sender no longer attached to a PeerConnection
1074 // should return false.
1075 EXPECT_FALSE(pc_->RemoveTrack(audio_sender));
1076 EXPECT_FALSE(pc_->RemoveTrack(video_sender));
1077}
1078
1079// Test creating senders without a stream specified,
1080// expecting a random stream ID to be generated.
1081TEST_F(PeerConnectionInterfaceTest, AddTrackWithoutStream) {
1082 CreatePeerConnection();
1083 // Create a dummy stream, so tracks share a stream label.
1084 scoped_refptr<AudioTrackInterface> audio_track(
1085 pc_factory_->CreateAudioTrack("audio_track", nullptr));
nisseaf510af2016-03-21 08:20:42 -07001086 scoped_refptr<VideoTrackInterface> video_track(pc_factory_->CreateVideoTrack(
1087 "video_track",
1088 pc_factory_->CreateVideoSource(new cricket::FakeVideoCapturer())));
deadbeefe1f9d832016-01-14 15:35:42 -08001089 auto audio_sender =
1090 pc_->AddTrack(audio_track, std::vector<MediaStreamInterface*>());
1091 auto video_sender =
1092 pc_->AddTrack(video_track, std::vector<MediaStreamInterface*>());
1093 EXPECT_EQ("audio_track", audio_sender->id());
1094 EXPECT_EQ(audio_track, audio_sender->track());
1095 EXPECT_EQ("video_track", video_sender->id());
1096 EXPECT_EQ(video_track, video_sender->track());
1097 // If the ID is truly a random GUID, it should be infinitely unlikely they
1098 // will be the same.
1099 EXPECT_NE(video_sender->stream_id(), audio_sender->stream_id());
1100}
1101
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001102TEST_F(PeerConnectionInterfaceTest, CreateOfferReceiveAnswer) {
1103 InitiateCall();
1104 WaitAndVerifyOnAddStream(kStreamLabel1);
1105 VerifyRemoteRtpHeaderExtensions();
1106}
1107
1108TEST_F(PeerConnectionInterfaceTest, CreateOfferReceivePrAnswerAndAnswer) {
1109 CreatePeerConnection();
deadbeefab9b2d12015-10-14 11:33:11 -07001110 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001111 CreateOfferAsLocalDescription();
1112 std::string offer;
1113 EXPECT_TRUE(pc_->local_description()->ToString(&offer));
1114 CreatePrAnswerAndAnswerAsRemoteDescription(offer);
1115 WaitAndVerifyOnAddStream(kStreamLabel1);
1116}
1117
1118TEST_F(PeerConnectionInterfaceTest, ReceiveOfferCreateAnswer) {
1119 CreatePeerConnection();
deadbeefab9b2d12015-10-14 11:33:11 -07001120 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001121
1122 CreateOfferAsRemoteDescription();
1123 CreateAnswerAsLocalDescription();
1124
1125 WaitAndVerifyOnAddStream(kStreamLabel1);
1126}
1127
1128TEST_F(PeerConnectionInterfaceTest, ReceiveOfferCreatePrAnswerAndAnswer) {
1129 CreatePeerConnection();
deadbeefab9b2d12015-10-14 11:33:11 -07001130 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001131
1132 CreateOfferAsRemoteDescription();
1133 CreatePrAnswerAsLocalDescription();
1134 CreateAnswerAsLocalDescription();
1135
1136 WaitAndVerifyOnAddStream(kStreamLabel1);
1137}
1138
1139TEST_F(PeerConnectionInterfaceTest, Renegotiate) {
1140 InitiateCall();
1141 ASSERT_EQ(1u, pc_->remote_streams()->count());
1142 pc_->RemoveStream(pc_->local_streams()->at(0));
1143 CreateOfferReceiveAnswer();
1144 EXPECT_EQ(0u, pc_->remote_streams()->count());
deadbeefab9b2d12015-10-14 11:33:11 -07001145 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001146 CreateOfferReceiveAnswer();
1147}
1148
1149// Tests that after negotiating an audio only call, the respondent can perform a
1150// renegotiation that removes the audio stream.
1151TEST_F(PeerConnectionInterfaceTest, RenegotiateAudioOnly) {
1152 CreatePeerConnection();
1153 AddVoiceStream(kStreamLabel1);
1154 CreateOfferAsRemoteDescription();
1155 CreateAnswerAsLocalDescription();
1156
1157 ASSERT_EQ(1u, pc_->remote_streams()->count());
1158 pc_->RemoveStream(pc_->local_streams()->at(0));
1159 CreateOfferReceiveAnswer();
1160 EXPECT_EQ(0u, pc_->remote_streams()->count());
1161}
1162
1163// Test that candidates are generated and that we can parse our own candidates.
1164TEST_F(PeerConnectionInterfaceTest, IceCandidates) {
1165 CreatePeerConnection();
1166
1167 EXPECT_FALSE(pc_->AddIceCandidate(observer_.last_candidate_.get()));
1168 // SetRemoteDescription takes ownership of offer.
kwiberg2bbff992016-03-16 11:03:04 -07001169 rtc::scoped_ptr<SessionDescriptionInterface> offer;
deadbeefab9b2d12015-10-14 11:33:11 -07001170 AddVideoStream(kStreamLabel1);
deadbeefc80741f2015-10-22 13:14:45 -07001171 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
kwiberg2bbff992016-03-16 11:03:04 -07001172 EXPECT_TRUE(DoSetRemoteDescription(offer.release()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001173
1174 // SetLocalDescription takes ownership of answer.
kwiberg2bbff992016-03-16 11:03:04 -07001175 rtc::scoped_ptr<SessionDescriptionInterface> answer;
deadbeefc80741f2015-10-22 13:14:45 -07001176 EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
kwiberg2bbff992016-03-16 11:03:04 -07001177 EXPECT_TRUE(DoSetLocalDescription(answer.release()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001178
1179 EXPECT_TRUE_WAIT(observer_.last_candidate_.get() != NULL, kTimeout);
1180 EXPECT_TRUE_WAIT(observer_.ice_complete_, kTimeout);
1181
1182 EXPECT_TRUE(pc_->AddIceCandidate(observer_.last_candidate_.get()));
1183}
1184
deadbeefab9b2d12015-10-14 11:33:11 -07001185// Test that CreateOffer and CreateAnswer will fail if the track labels are
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001186// not unique.
1187TEST_F(PeerConnectionInterfaceTest, CreateOfferAnswerWithInvalidStream) {
1188 CreatePeerConnection();
1189 // Create a regular offer for the CreateAnswer test later.
kwiberg2bbff992016-03-16 11:03:04 -07001190 rtc::scoped_ptr<SessionDescriptionInterface> offer;
deadbeefc80741f2015-10-22 13:14:45 -07001191 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
kwiberg2bbff992016-03-16 11:03:04 -07001192 EXPECT_TRUE(offer);
1193 offer.reset();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001194
1195 // Create a local stream with audio&video tracks having same label.
1196 AddAudioVideoStream(kStreamLabel1, "track_label", "track_label");
1197
1198 // Test CreateOffer
deadbeefc80741f2015-10-22 13:14:45 -07001199 EXPECT_FALSE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001200
1201 // Test CreateAnswer
kwiberg2bbff992016-03-16 11:03:04 -07001202 rtc::scoped_ptr<SessionDescriptionInterface> answer;
deadbeefc80741f2015-10-22 13:14:45 -07001203 EXPECT_FALSE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001204}
1205
1206// Test that we will get different SSRCs for each tracks in the offer and answer
1207// we created.
1208TEST_F(PeerConnectionInterfaceTest, SsrcInOfferAnswer) {
1209 CreatePeerConnection();
1210 // Create a local stream with audio&video tracks having different labels.
1211 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
1212
1213 // Test CreateOffer
1214 scoped_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001215 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001216 int audio_ssrc = 0;
1217 int video_ssrc = 0;
1218 EXPECT_TRUE(GetFirstSsrc(GetFirstAudioContent(offer->description()),
1219 &audio_ssrc));
1220 EXPECT_TRUE(GetFirstSsrc(GetFirstVideoContent(offer->description()),
1221 &video_ssrc));
1222 EXPECT_NE(audio_ssrc, video_ssrc);
1223
1224 // Test CreateAnswer
1225 EXPECT_TRUE(DoSetRemoteDescription(offer.release()));
1226 scoped_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -07001227 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001228 audio_ssrc = 0;
1229 video_ssrc = 0;
1230 EXPECT_TRUE(GetFirstSsrc(GetFirstAudioContent(answer->description()),
1231 &audio_ssrc));
1232 EXPECT_TRUE(GetFirstSsrc(GetFirstVideoContent(answer->description()),
1233 &video_ssrc));
1234 EXPECT_NE(audio_ssrc, video_ssrc);
1235}
1236
deadbeefeb459812015-12-15 19:24:43 -08001237// Test that it's possible to call AddTrack on a MediaStream after adding
1238// the stream to a PeerConnection.
1239// TODO(deadbeef): Remove this test once this behavior is no longer supported.
1240TEST_F(PeerConnectionInterfaceTest, AddTrackAfterAddStream) {
1241 CreatePeerConnection();
1242 // Create audio stream and add to PeerConnection.
1243 AddVoiceStream(kStreamLabel1);
1244 MediaStreamInterface* stream = pc_->local_streams()->at(0);
1245
1246 // Add video track to the audio-only stream.
nisseaf510af2016-03-21 08:20:42 -07001247 scoped_refptr<VideoTrackInterface> video_track(pc_factory_->CreateVideoTrack(
1248 "video_label",
1249 pc_factory_->CreateVideoSource(new cricket::FakeVideoCapturer())));
deadbeefeb459812015-12-15 19:24:43 -08001250 stream->AddTrack(video_track.get());
1251
1252 scoped_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001253 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefeb459812015-12-15 19:24:43 -08001254
1255 const cricket::MediaContentDescription* video_desc =
1256 cricket::GetFirstVideoContentDescription(offer->description());
1257 EXPECT_TRUE(video_desc != nullptr);
1258}
1259
1260// Test that it's possible to call RemoveTrack on a MediaStream after adding
1261// the stream to a PeerConnection.
1262// TODO(deadbeef): Remove this test once this behavior is no longer supported.
1263TEST_F(PeerConnectionInterfaceTest, RemoveTrackAfterAddStream) {
1264 CreatePeerConnection();
1265 // Create audio/video stream and add to PeerConnection.
1266 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
1267 MediaStreamInterface* stream = pc_->local_streams()->at(0);
1268
1269 // Remove the video track.
1270 stream->RemoveTrack(stream->GetVideoTracks()[0]);
1271
1272 scoped_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001273 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefeb459812015-12-15 19:24:43 -08001274
1275 const cricket::MediaContentDescription* video_desc =
1276 cricket::GetFirstVideoContentDescription(offer->description());
1277 EXPECT_TRUE(video_desc == nullptr);
1278}
1279
deadbeefbd7d8f72015-12-18 16:58:44 -08001280// Test creating a sender with a stream ID, and ensure the ID is populated
1281// in the offer.
1282TEST_F(PeerConnectionInterfaceTest, CreateSenderWithStream) {
1283 CreatePeerConnection();
1284 pc_->CreateSender("video", kStreamLabel1);
1285
1286 scoped_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001287 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefbd7d8f72015-12-18 16:58:44 -08001288
1289 const cricket::MediaContentDescription* video_desc =
1290 cricket::GetFirstVideoContentDescription(offer->description());
1291 ASSERT_TRUE(video_desc != nullptr);
1292 ASSERT_EQ(1u, video_desc->streams().size());
1293 EXPECT_EQ(kStreamLabel1, video_desc->streams()[0].sync_label);
1294}
1295
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001296// Test that we can specify a certain track that we want statistics about.
1297TEST_F(PeerConnectionInterfaceTest, GetStatsForSpecificTrack) {
1298 InitiateCall();
1299 ASSERT_LT(0u, pc_->remote_streams()->count());
1300 ASSERT_LT(0u, pc_->remote_streams()->at(0)->GetAudioTracks().size());
1301 scoped_refptr<MediaStreamTrackInterface> remote_audio =
1302 pc_->remote_streams()->at(0)->GetAudioTracks()[0];
1303 EXPECT_TRUE(DoGetStats(remote_audio));
1304
1305 // Remove the stream. Since we are sending to our selves the local
1306 // and the remote stream is the same.
1307 pc_->RemoveStream(pc_->local_streams()->at(0));
1308 // Do a re-negotiation.
1309 CreateOfferReceiveAnswer();
1310
1311 ASSERT_EQ(0u, pc_->remote_streams()->count());
1312
1313 // Test that we still can get statistics for the old track. Even if it is not
1314 // sent any longer.
1315 EXPECT_TRUE(DoGetStats(remote_audio));
1316}
1317
1318// Test that we can get stats on a video track.
1319TEST_F(PeerConnectionInterfaceTest, GetStatsForVideoTrack) {
1320 InitiateCall();
1321 ASSERT_LT(0u, pc_->remote_streams()->count());
1322 ASSERT_LT(0u, pc_->remote_streams()->at(0)->GetVideoTracks().size());
1323 scoped_refptr<MediaStreamTrackInterface> remote_video =
1324 pc_->remote_streams()->at(0)->GetVideoTracks()[0];
1325 EXPECT_TRUE(DoGetStats(remote_video));
1326}
1327
1328// Test that we don't get statistics for an invalid track.
tommi@webrtc.org908f57e2014-07-21 11:44:39 +00001329// TODO(tommi): Fix this test. DoGetStats will return true
1330// for the unknown track (since GetStats is async), but no
1331// data is returned for the track.
1332TEST_F(PeerConnectionInterfaceTest, DISABLED_GetStatsForInvalidTrack) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001333 InitiateCall();
1334 scoped_refptr<AudioTrackInterface> unknown_audio_track(
1335 pc_factory_->CreateAudioTrack("unknown track", NULL));
1336 EXPECT_FALSE(DoGetStats(unknown_audio_track));
1337}
1338
1339// This test setup two RTP data channels in loop back.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001340TEST_F(PeerConnectionInterfaceTest, TestDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001341 FakeConstraints constraints;
1342 constraints.SetAllowRtpDataChannels();
1343 CreatePeerConnection(&constraints);
1344 scoped_refptr<DataChannelInterface> data1 =
1345 pc_->CreateDataChannel("test1", NULL);
1346 scoped_refptr<DataChannelInterface> data2 =
1347 pc_->CreateDataChannel("test2", NULL);
1348 ASSERT_TRUE(data1 != NULL);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001349 rtc::scoped_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001350 new MockDataChannelObserver(data1));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001351 rtc::scoped_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001352 new MockDataChannelObserver(data2));
1353
1354 EXPECT_EQ(DataChannelInterface::kConnecting, data1->state());
1355 EXPECT_EQ(DataChannelInterface::kConnecting, data2->state());
1356 std::string data_to_send1 = "testing testing";
1357 std::string data_to_send2 = "testing something else";
1358 EXPECT_FALSE(data1->Send(DataBuffer(data_to_send1)));
1359
1360 CreateOfferReceiveAnswer();
1361 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1362 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
1363
1364 EXPECT_EQ(DataChannelInterface::kOpen, data1->state());
1365 EXPECT_EQ(DataChannelInterface::kOpen, data2->state());
1366 EXPECT_TRUE(data1->Send(DataBuffer(data_to_send1)));
1367 EXPECT_TRUE(data2->Send(DataBuffer(data_to_send2)));
1368
1369 EXPECT_EQ_WAIT(data_to_send1, observer1->last_message(), kTimeout);
1370 EXPECT_EQ_WAIT(data_to_send2, observer2->last_message(), kTimeout);
1371
1372 data1->Close();
1373 EXPECT_EQ(DataChannelInterface::kClosing, data1->state());
1374 CreateOfferReceiveAnswer();
1375 EXPECT_FALSE(observer1->IsOpen());
1376 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
1377 EXPECT_TRUE(observer2->IsOpen());
1378
1379 data_to_send2 = "testing something else again";
1380 EXPECT_TRUE(data2->Send(DataBuffer(data_to_send2)));
1381
1382 EXPECT_EQ_WAIT(data_to_send2, observer2->last_message(), kTimeout);
1383}
1384
1385// This test verifies that sendnig binary data over RTP data channels should
1386// fail.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001387TEST_F(PeerConnectionInterfaceTest, TestSendBinaryOnRtpDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001388 FakeConstraints constraints;
1389 constraints.SetAllowRtpDataChannels();
1390 CreatePeerConnection(&constraints);
1391 scoped_refptr<DataChannelInterface> data1 =
1392 pc_->CreateDataChannel("test1", NULL);
1393 scoped_refptr<DataChannelInterface> data2 =
1394 pc_->CreateDataChannel("test2", NULL);
1395 ASSERT_TRUE(data1 != NULL);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001396 rtc::scoped_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001397 new MockDataChannelObserver(data1));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001398 rtc::scoped_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001399 new MockDataChannelObserver(data2));
1400
1401 EXPECT_EQ(DataChannelInterface::kConnecting, data1->state());
1402 EXPECT_EQ(DataChannelInterface::kConnecting, data2->state());
1403
1404 CreateOfferReceiveAnswer();
1405 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1406 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
1407
1408 EXPECT_EQ(DataChannelInterface::kOpen, data1->state());
1409 EXPECT_EQ(DataChannelInterface::kOpen, data2->state());
1410
jbaucheec21bd2016-03-20 06:15:43 -07001411 rtc::CopyOnWriteBuffer buffer("test", 4);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001412 EXPECT_FALSE(data1->Send(DataBuffer(buffer, true)));
1413}
1414
1415// This test setup a RTP data channels in loop back and test that a channel is
1416// opened even if the remote end answer with a zero SSRC.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001417TEST_F(PeerConnectionInterfaceTest, TestSendOnlyDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001418 FakeConstraints constraints;
1419 constraints.SetAllowRtpDataChannels();
1420 CreatePeerConnection(&constraints);
1421 scoped_refptr<DataChannelInterface> data1 =
1422 pc_->CreateDataChannel("test1", 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));
1425
1426 CreateOfferReceiveAnswerWithoutSsrc();
1427
1428 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1429
1430 data1->Close();
1431 EXPECT_EQ(DataChannelInterface::kClosing, data1->state());
1432 CreateOfferReceiveAnswerWithoutSsrc();
1433 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
1434 EXPECT_FALSE(observer1->IsOpen());
1435}
1436
1437// This test that if a data channel is added in an answer a receive only channel
1438// channel is created.
1439TEST_F(PeerConnectionInterfaceTest, TestReceiveOnlyDataChannel) {
1440 FakeConstraints constraints;
1441 constraints.SetAllowRtpDataChannels();
1442 CreatePeerConnection(&constraints);
1443
1444 std::string offer_label = "offer_channel";
1445 scoped_refptr<DataChannelInterface> offer_channel =
1446 pc_->CreateDataChannel(offer_label, NULL);
1447
1448 CreateOfferAsLocalDescription();
1449
1450 // Replace the data channel label in the offer and apply it as an answer.
1451 std::string receive_label = "answer_channel";
1452 std::string sdp;
1453 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001454 rtc::replace_substrs(offer_label.c_str(), offer_label.length(),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001455 receive_label.c_str(), receive_label.length(),
1456 &sdp);
1457 CreateAnswerAsRemoteDescription(sdp);
1458
1459 // Verify that a new incoming data channel has been created and that
1460 // it is open but can't we written to.
1461 ASSERT_TRUE(observer_.last_datachannel_ != NULL);
1462 DataChannelInterface* received_channel = observer_.last_datachannel_;
1463 EXPECT_EQ(DataChannelInterface::kConnecting, received_channel->state());
1464 EXPECT_EQ(receive_label, received_channel->label());
1465 EXPECT_FALSE(received_channel->Send(DataBuffer("something")));
1466
1467 // Verify that the channel we initially offered has been rejected.
1468 EXPECT_EQ(DataChannelInterface::kClosed, offer_channel->state());
1469
1470 // Do another offer / answer exchange and verify that the data channel is
1471 // opened.
1472 CreateOfferReceiveAnswer();
1473 EXPECT_EQ_WAIT(DataChannelInterface::kOpen, received_channel->state(),
1474 kTimeout);
1475}
1476
1477// This test that no data channel is returned if a reliable channel is
1478// requested.
1479// TODO(perkj): Remove this test once reliable channels are implemented.
1480TEST_F(PeerConnectionInterfaceTest, CreateReliableRtpDataChannelShouldFail) {
1481 FakeConstraints constraints;
1482 constraints.SetAllowRtpDataChannels();
1483 CreatePeerConnection(&constraints);
1484
1485 std::string label = "test";
1486 webrtc::DataChannelInit config;
1487 config.reliable = true;
1488 scoped_refptr<DataChannelInterface> channel =
1489 pc_->CreateDataChannel(label, &config);
1490 EXPECT_TRUE(channel == NULL);
1491}
1492
deadbeefab9b2d12015-10-14 11:33:11 -07001493// Verifies that duplicated label is not allowed for RTP data channel.
1494TEST_F(PeerConnectionInterfaceTest, RtpDuplicatedLabelNotAllowed) {
1495 FakeConstraints constraints;
1496 constraints.SetAllowRtpDataChannels();
1497 CreatePeerConnection(&constraints);
1498
1499 std::string label = "test";
1500 scoped_refptr<DataChannelInterface> channel =
1501 pc_->CreateDataChannel(label, nullptr);
1502 EXPECT_NE(channel, nullptr);
1503
1504 scoped_refptr<DataChannelInterface> dup_channel =
1505 pc_->CreateDataChannel(label, nullptr);
1506 EXPECT_EQ(dup_channel, nullptr);
1507}
1508
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001509// This tests that a SCTP data channel is returned using different
1510// DataChannelInit configurations.
1511TEST_F(PeerConnectionInterfaceTest, CreateSctpDataChannel) {
1512 FakeConstraints constraints;
1513 constraints.SetAllowDtlsSctpDataChannels();
1514 CreatePeerConnection(&constraints);
1515
1516 webrtc::DataChannelInit config;
1517
1518 scoped_refptr<DataChannelInterface> channel =
1519 pc_->CreateDataChannel("1", &config);
1520 EXPECT_TRUE(channel != NULL);
1521 EXPECT_TRUE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001522 EXPECT_TRUE(observer_.renegotiation_needed_);
1523 observer_.renegotiation_needed_ = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001524
1525 config.ordered = false;
1526 channel = pc_->CreateDataChannel("2", &config);
1527 EXPECT_TRUE(channel != NULL);
1528 EXPECT_TRUE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001529 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001530
1531 config.ordered = true;
1532 config.maxRetransmits = 0;
1533 channel = pc_->CreateDataChannel("3", &config);
1534 EXPECT_TRUE(channel != NULL);
1535 EXPECT_FALSE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001536 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001537
1538 config.maxRetransmits = -1;
1539 config.maxRetransmitTime = 0;
1540 channel = pc_->CreateDataChannel("4", &config);
1541 EXPECT_TRUE(channel != NULL);
1542 EXPECT_FALSE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001543 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001544}
1545
1546// This tests that no data channel is returned if both maxRetransmits and
1547// maxRetransmitTime are set for SCTP data channels.
1548TEST_F(PeerConnectionInterfaceTest,
1549 CreateSctpDataChannelShouldFailForInvalidConfig) {
1550 FakeConstraints constraints;
1551 constraints.SetAllowDtlsSctpDataChannels();
1552 CreatePeerConnection(&constraints);
1553
1554 std::string label = "test";
1555 webrtc::DataChannelInit config;
1556 config.maxRetransmits = 0;
1557 config.maxRetransmitTime = 0;
1558
1559 scoped_refptr<DataChannelInterface> channel =
1560 pc_->CreateDataChannel(label, &config);
1561 EXPECT_TRUE(channel == NULL);
1562}
1563
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001564// The test verifies that creating a SCTP data channel with an id already in use
1565// or out of range should fail.
1566TEST_F(PeerConnectionInterfaceTest,
1567 CreateSctpDataChannelWithInvalidIdShouldFail) {
1568 FakeConstraints constraints;
1569 constraints.SetAllowDtlsSctpDataChannels();
1570 CreatePeerConnection(&constraints);
1571
1572 webrtc::DataChannelInit config;
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001573 scoped_refptr<DataChannelInterface> channel;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001574
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001575 config.id = 1;
1576 channel = pc_->CreateDataChannel("1", &config);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001577 EXPECT_TRUE(channel != NULL);
1578 EXPECT_EQ(1, channel->id());
1579
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001580 channel = pc_->CreateDataChannel("x", &config);
1581 EXPECT_TRUE(channel == NULL);
1582
1583 config.id = cricket::kMaxSctpSid;
1584 channel = pc_->CreateDataChannel("max", &config);
1585 EXPECT_TRUE(channel != NULL);
1586 EXPECT_EQ(config.id, channel->id());
1587
1588 config.id = cricket::kMaxSctpSid + 1;
1589 channel = pc_->CreateDataChannel("x", &config);
1590 EXPECT_TRUE(channel == NULL);
1591}
1592
deadbeefab9b2d12015-10-14 11:33:11 -07001593// Verifies that duplicated label is allowed for SCTP data channel.
1594TEST_F(PeerConnectionInterfaceTest, SctpDuplicatedLabelAllowed) {
1595 FakeConstraints constraints;
1596 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
1597 true);
1598 CreatePeerConnection(&constraints);
1599
1600 std::string label = "test";
1601 scoped_refptr<DataChannelInterface> channel =
1602 pc_->CreateDataChannel(label, nullptr);
1603 EXPECT_NE(channel, nullptr);
1604
1605 scoped_refptr<DataChannelInterface> dup_channel =
1606 pc_->CreateDataChannel(label, nullptr);
1607 EXPECT_NE(dup_channel, nullptr);
1608}
1609
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001610// This test verifies that OnRenegotiationNeeded is fired for every new RTP
1611// DataChannel.
1612TEST_F(PeerConnectionInterfaceTest, RenegotiationNeededForNewRtpDataChannel) {
1613 FakeConstraints constraints;
1614 constraints.SetAllowRtpDataChannels();
1615 CreatePeerConnection(&constraints);
1616
1617 scoped_refptr<DataChannelInterface> dc1 =
1618 pc_->CreateDataChannel("test1", NULL);
1619 EXPECT_TRUE(observer_.renegotiation_needed_);
1620 observer_.renegotiation_needed_ = false;
1621
1622 scoped_refptr<DataChannelInterface> dc2 =
1623 pc_->CreateDataChannel("test2", NULL);
1624 EXPECT_TRUE(observer_.renegotiation_needed_);
1625}
1626
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001627// This test that a data channel closes when a PeerConnection is deleted/closed.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001628TEST_F(PeerConnectionInterfaceTest, DataChannelCloseWhenPeerConnectionClose) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001629 FakeConstraints constraints;
1630 constraints.SetAllowRtpDataChannels();
1631 CreatePeerConnection(&constraints);
1632
1633 scoped_refptr<DataChannelInterface> data1 =
1634 pc_->CreateDataChannel("test1", NULL);
1635 scoped_refptr<DataChannelInterface> data2 =
1636 pc_->CreateDataChannel("test2", NULL);
1637 ASSERT_TRUE(data1 != NULL);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001638 rtc::scoped_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001639 new MockDataChannelObserver(data1));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001640 rtc::scoped_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001641 new MockDataChannelObserver(data2));
1642
1643 CreateOfferReceiveAnswer();
1644 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1645 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
1646
1647 ReleasePeerConnection();
1648 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
1649 EXPECT_EQ(DataChannelInterface::kClosed, data2->state());
1650}
1651
1652// This test that data channels can be rejected in an answer.
1653TEST_F(PeerConnectionInterfaceTest, TestRejectDataChannelInAnswer) {
1654 FakeConstraints constraints;
1655 constraints.SetAllowRtpDataChannels();
1656 CreatePeerConnection(&constraints);
1657
1658 scoped_refptr<DataChannelInterface> offer_channel(
1659 pc_->CreateDataChannel("offer_channel", NULL));
1660
1661 CreateOfferAsLocalDescription();
1662
1663 // Create an answer where the m-line for data channels are rejected.
1664 std::string sdp;
1665 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
1666 webrtc::JsepSessionDescription* answer = new webrtc::JsepSessionDescription(
1667 SessionDescriptionInterface::kAnswer);
1668 EXPECT_TRUE(answer->Initialize(sdp, NULL));
1669 cricket::ContentInfo* data_info =
1670 answer->description()->GetContentByName("data");
1671 data_info->rejected = true;
1672
1673 DoSetRemoteDescription(answer);
1674 EXPECT_EQ(DataChannelInterface::kClosed, offer_channel->state());
1675}
1676
Stefan Holmer55d6e7c2016-03-17 16:26:40 +01001677// Disabled on Win dbg: https://bugs.chromium.org/p/webrtc/issues/detail?id=5659
Stefan Holmer102362b2016-03-18 09:39:07 +01001678#if defined(WEBRTC_WIN) && defined(_DEBUG)
Stefan Holmer55d6e7c2016-03-17 16:26:40 +01001679#define MAYBE_ReceiveFireFoxOffer DISABLED_ReceiveFireFoxOffer
1680#else
1681#define MAYBE_ReceiveFireFoxOffer ReceiveFireFoxOffer
1682#endif
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001683// Test that we can create a session description from an SDP string from
1684// FireFox, use it as a remote session description, generate an answer and use
1685// the answer as a local description.
Stefan Holmer55d6e7c2016-03-17 16:26:40 +01001686TEST_F(PeerConnectionInterfaceTest, MAYBE_ReceiveFireFoxOffer) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001687 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001688 FakeConstraints constraints;
1689 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
1690 true);
1691 CreatePeerConnection(&constraints);
1692 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
1693 SessionDescriptionInterface* desc =
1694 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
jbauchfabe2c92015-07-16 13:43:14 -07001695 webrtc::kFireFoxSdpOffer, nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001696 EXPECT_TRUE(DoSetSessionDescription(desc, false));
1697 CreateAnswerAsLocalDescription();
1698 ASSERT_TRUE(pc_->local_description() != NULL);
1699 ASSERT_TRUE(pc_->remote_description() != NULL);
1700
1701 const cricket::ContentInfo* content =
1702 cricket::GetFirstAudioContent(pc_->local_description()->description());
1703 ASSERT_TRUE(content != NULL);
1704 EXPECT_FALSE(content->rejected);
1705
1706 content =
1707 cricket::GetFirstVideoContent(pc_->local_description()->description());
1708 ASSERT_TRUE(content != NULL);
1709 EXPECT_FALSE(content->rejected);
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00001710#ifdef HAVE_SCTP
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001711 content =
1712 cricket::GetFirstDataContent(pc_->local_description()->description());
1713 ASSERT_TRUE(content != NULL);
1714 EXPECT_TRUE(content->rejected);
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00001715#endif
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001716}
1717
1718// Test that we can create an audio only offer and receive an answer with a
1719// limited set of audio codecs and receive an updated offer with more audio
1720// codecs, where the added codecs are not supported.
1721TEST_F(PeerConnectionInterfaceTest, ReceiveUpdatedAudioOfferWithBadCodecs) {
1722 CreatePeerConnection();
1723 AddVoiceStream("audio_label");
1724 CreateOfferAsLocalDescription();
1725
1726 SessionDescriptionInterface* answer =
1727 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
jbauchfabe2c92015-07-16 13:43:14 -07001728 webrtc::kAudioSdp, nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001729 EXPECT_TRUE(DoSetSessionDescription(answer, false));
1730
1731 SessionDescriptionInterface* updated_offer =
1732 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
jbauchfabe2c92015-07-16 13:43:14 -07001733 webrtc::kAudioSdpWithUnsupportedCodecs,
1734 nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001735 EXPECT_TRUE(DoSetSessionDescription(updated_offer, false));
1736 CreateAnswerAsLocalDescription();
1737}
1738
deadbeefc80741f2015-10-22 13:14:45 -07001739// Test that if we're receiving (but not sending) a track, subsequent offers
1740// will have m-lines with a=recvonly.
1741TEST_F(PeerConnectionInterfaceTest, CreateSubsequentRecvOnlyOffer) {
1742 FakeConstraints constraints;
1743 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
1744 true);
1745 CreatePeerConnection(&constraints);
1746 CreateAndSetRemoteOffer(kSdpStringWithStream1);
1747 CreateAnswerAsLocalDescription();
1748
1749 // At this point we should be receiving stream 1, but not sending anything.
1750 // A new offer should be recvonly.
kwiberg2bbff992016-03-16 11:03:04 -07001751 rtc::scoped_ptr<SessionDescriptionInterface> offer;
deadbeefc80741f2015-10-22 13:14:45 -07001752 DoCreateOffer(&offer, nullptr);
1753
1754 const cricket::ContentInfo* video_content =
1755 cricket::GetFirstVideoContent(offer->description());
1756 const cricket::VideoContentDescription* video_desc =
1757 static_cast<const cricket::VideoContentDescription*>(
1758 video_content->description);
1759 ASSERT_EQ(cricket::MD_RECVONLY, video_desc->direction());
1760
1761 const cricket::ContentInfo* audio_content =
1762 cricket::GetFirstAudioContent(offer->description());
1763 const cricket::AudioContentDescription* audio_desc =
1764 static_cast<const cricket::AudioContentDescription*>(
1765 audio_content->description);
1766 ASSERT_EQ(cricket::MD_RECVONLY, audio_desc->direction());
1767}
1768
1769// Test that if we're receiving (but not sending) a track, and the
1770// offerToReceiveVideo/offerToReceiveAudio constraints are explicitly set to
1771// false, the generated m-lines will be a=inactive.
1772TEST_F(PeerConnectionInterfaceTest, CreateSubsequentInactiveOffer) {
1773 FakeConstraints constraints;
1774 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
1775 true);
1776 CreatePeerConnection(&constraints);
1777 CreateAndSetRemoteOffer(kSdpStringWithStream1);
1778 CreateAnswerAsLocalDescription();
1779
1780 // At this point we should be receiving stream 1, but not sending anything.
1781 // A new offer would be recvonly, but we'll set the "no receive" constraints
1782 // to make it inactive.
kwiberg2bbff992016-03-16 11:03:04 -07001783 rtc::scoped_ptr<SessionDescriptionInterface> offer;
deadbeefc80741f2015-10-22 13:14:45 -07001784 FakeConstraints offer_constraints;
1785 offer_constraints.AddMandatory(
1786 webrtc::MediaConstraintsInterface::kOfferToReceiveVideo, false);
1787 offer_constraints.AddMandatory(
1788 webrtc::MediaConstraintsInterface::kOfferToReceiveAudio, false);
1789 DoCreateOffer(&offer, &offer_constraints);
1790
1791 const cricket::ContentInfo* video_content =
1792 cricket::GetFirstVideoContent(offer->description());
1793 const cricket::VideoContentDescription* video_desc =
1794 static_cast<const cricket::VideoContentDescription*>(
1795 video_content->description);
1796 ASSERT_EQ(cricket::MD_INACTIVE, video_desc->direction());
1797
1798 const cricket::ContentInfo* audio_content =
1799 cricket::GetFirstAudioContent(offer->description());
1800 const cricket::AudioContentDescription* audio_desc =
1801 static_cast<const cricket::AudioContentDescription*>(
1802 audio_content->description);
1803 ASSERT_EQ(cricket::MD_INACTIVE, audio_desc->direction());
1804}
1805
deadbeef653b8e02015-11-11 12:55:10 -08001806// Test that we can use SetConfiguration to change the ICE servers of the
1807// PortAllocator.
1808TEST_F(PeerConnectionInterfaceTest, SetConfigurationChangesIceServers) {
1809 CreatePeerConnection();
1810
1811 PeerConnectionInterface::RTCConfiguration config;
1812 PeerConnectionInterface::IceServer server;
1813 server.uri = "stun:test_hostname";
1814 config.servers.push_back(server);
1815 EXPECT_TRUE(pc_->SetConfiguration(config));
1816
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08001817 EXPECT_EQ(1u, port_allocator_->stun_servers().size());
1818 EXPECT_EQ("test_hostname",
1819 port_allocator_->stun_servers().begin()->hostname());
deadbeef653b8e02015-11-11 12:55:10 -08001820}
1821
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001822// Test that PeerConnection::Close changes the states to closed and all remote
1823// tracks change state to ended.
1824TEST_F(PeerConnectionInterfaceTest, CloseAndTestStreamsAndStates) {
1825 // Initialize a PeerConnection and negotiate local and remote session
1826 // description.
1827 InitiateCall();
1828 ASSERT_EQ(1u, pc_->local_streams()->count());
1829 ASSERT_EQ(1u, pc_->remote_streams()->count());
1830
1831 pc_->Close();
1832
1833 EXPECT_EQ(PeerConnectionInterface::kClosed, pc_->signaling_state());
1834 EXPECT_EQ(PeerConnectionInterface::kIceConnectionClosed,
1835 pc_->ice_connection_state());
1836 EXPECT_EQ(PeerConnectionInterface::kIceGatheringComplete,
1837 pc_->ice_gathering_state());
1838
1839 EXPECT_EQ(1u, pc_->local_streams()->count());
1840 EXPECT_EQ(1u, pc_->remote_streams()->count());
1841
1842 scoped_refptr<MediaStreamInterface> remote_stream =
1843 pc_->remote_streams()->at(0);
1844 EXPECT_EQ(MediaStreamTrackInterface::kEnded,
1845 remote_stream->GetVideoTracks()[0]->state());
1846 EXPECT_EQ(MediaStreamTrackInterface::kEnded,
1847 remote_stream->GetAudioTracks()[0]->state());
1848}
1849
1850// Test that PeerConnection methods fails gracefully after
1851// PeerConnection::Close has been called.
1852TEST_F(PeerConnectionInterfaceTest, CloseAndTestMethods) {
1853 CreatePeerConnection();
1854 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
1855 CreateOfferAsRemoteDescription();
1856 CreateAnswerAsLocalDescription();
1857
1858 ASSERT_EQ(1u, pc_->local_streams()->count());
1859 scoped_refptr<MediaStreamInterface> local_stream =
1860 pc_->local_streams()->at(0);
1861
1862 pc_->Close();
1863
1864 pc_->RemoveStream(local_stream);
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +00001865 EXPECT_FALSE(pc_->AddStream(local_stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001866
1867 ASSERT_FALSE(local_stream->GetAudioTracks().empty());
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001868 rtc::scoped_refptr<webrtc::DtmfSenderInterface> dtmf_sender(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001869 pc_->CreateDtmfSender(local_stream->GetAudioTracks()[0]));
wu@webrtc.org66037362013-08-13 00:09:35 +00001870 EXPECT_TRUE(NULL == dtmf_sender); // local stream has been removed.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001871
1872 EXPECT_TRUE(pc_->CreateDataChannel("test", NULL) == NULL);
1873
1874 EXPECT_TRUE(pc_->local_description() != NULL);
1875 EXPECT_TRUE(pc_->remote_description() != NULL);
1876
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001877 rtc::scoped_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001878 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001879 rtc::scoped_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -07001880 EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001881
1882 std::string sdp;
1883 ASSERT_TRUE(pc_->remote_description()->ToString(&sdp));
1884 SessionDescriptionInterface* remote_offer =
1885 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
1886 sdp, NULL);
1887 EXPECT_FALSE(DoSetRemoteDescription(remote_offer));
1888
1889 ASSERT_TRUE(pc_->local_description()->ToString(&sdp));
1890 SessionDescriptionInterface* local_offer =
1891 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
1892 sdp, NULL);
1893 EXPECT_FALSE(DoSetLocalDescription(local_offer));
1894}
1895
1896// Test that GetStats can still be called after PeerConnection::Close.
1897TEST_F(PeerConnectionInterfaceTest, CloseAndGetStats) {
1898 InitiateCall();
1899 pc_->Close();
1900 DoGetStats(NULL);
1901}
deadbeefab9b2d12015-10-14 11:33:11 -07001902
1903// NOTE: The series of tests below come from what used to be
1904// mediastreamsignaling_unittest.cc, and are mostly aimed at testing that
1905// setting a remote or local description has the expected effects.
1906
1907// This test verifies that the remote MediaStreams corresponding to a received
1908// SDP string is created. In this test the two separate MediaStreams are
1909// signaled.
1910TEST_F(PeerConnectionInterfaceTest, UpdateRemoteStreams) {
1911 FakeConstraints constraints;
1912 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
1913 true);
1914 CreatePeerConnection(&constraints);
1915 CreateAndSetRemoteOffer(kSdpStringWithStream1);
1916
1917 rtc::scoped_refptr<StreamCollection> reference(CreateStreamCollection(1));
1918 EXPECT_TRUE(
1919 CompareStreamCollections(observer_.remote_streams(), reference.get()));
1920 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
1921 EXPECT_TRUE(remote_stream->GetVideoTracks()[0]->GetSource() != nullptr);
1922
1923 // Create a session description based on another SDP with another
1924 // MediaStream.
1925 CreateAndSetRemoteOffer(kSdpStringWithStream1And2);
1926
1927 rtc::scoped_refptr<StreamCollection> reference2(CreateStreamCollection(2));
1928 EXPECT_TRUE(
1929 CompareStreamCollections(observer_.remote_streams(), reference2.get()));
1930}
1931
1932// This test verifies that when remote tracks are added/removed from SDP, the
1933// created remote streams are updated appropriately.
1934TEST_F(PeerConnectionInterfaceTest,
1935 AddRemoveTrackFromExistingRemoteMediaStream) {
1936 FakeConstraints constraints;
1937 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
1938 true);
1939 CreatePeerConnection(&constraints);
kwiberg2bbff992016-03-16 11:03:04 -07001940 rtc::scoped_ptr<SessionDescriptionInterface> desc_ms1 =
1941 CreateSessionDescriptionAndReference(1, 1);
deadbeefab9b2d12015-10-14 11:33:11 -07001942 EXPECT_TRUE(DoSetRemoteDescription(desc_ms1.release()));
1943 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
1944 reference_collection_));
1945
1946 // Add extra audio and video tracks to the same MediaStream.
kwiberg2bbff992016-03-16 11:03:04 -07001947 rtc::scoped_ptr<SessionDescriptionInterface> desc_ms1_two_tracks =
1948 CreateSessionDescriptionAndReference(2, 2);
deadbeefab9b2d12015-10-14 11:33:11 -07001949 EXPECT_TRUE(DoSetRemoteDescription(desc_ms1_two_tracks.release()));
1950 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
1951 reference_collection_));
1952
1953 // Remove the extra audio and video tracks.
kwiberg2bbff992016-03-16 11:03:04 -07001954 rtc::scoped_ptr<SessionDescriptionInterface> desc_ms2 =
1955 CreateSessionDescriptionAndReference(1, 1);
deadbeefab9b2d12015-10-14 11:33:11 -07001956 EXPECT_TRUE(DoSetRemoteDescription(desc_ms2.release()));
1957 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
1958 reference_collection_));
1959}
1960
1961// This tests that remote tracks are ended if a local session description is set
1962// that rejects the media content type.
1963TEST_F(PeerConnectionInterfaceTest, RejectMediaContent) {
1964 FakeConstraints constraints;
1965 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
1966 true);
1967 CreatePeerConnection(&constraints);
1968 // First create and set a remote offer, then reject its video content in our
1969 // answer.
1970 CreateAndSetRemoteOffer(kSdpStringWithStream1);
1971 ASSERT_EQ(1u, observer_.remote_streams()->count());
1972 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
1973 ASSERT_EQ(1u, remote_stream->GetVideoTracks().size());
1974 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
1975
1976 rtc::scoped_refptr<webrtc::VideoTrackInterface> remote_video =
1977 remote_stream->GetVideoTracks()[0];
1978 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, remote_video->state());
1979 rtc::scoped_refptr<webrtc::AudioTrackInterface> remote_audio =
1980 remote_stream->GetAudioTracks()[0];
1981 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, remote_audio->state());
1982
1983 rtc::scoped_ptr<SessionDescriptionInterface> local_answer;
kwiberg2bbff992016-03-16 11:03:04 -07001984 EXPECT_TRUE(DoCreateAnswer(&local_answer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07001985 cricket::ContentInfo* video_info =
1986 local_answer->description()->GetContentByName("video");
1987 video_info->rejected = true;
1988 EXPECT_TRUE(DoSetLocalDescription(local_answer.release()));
1989 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kEnded, remote_video->state());
1990 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, remote_audio->state());
1991
1992 // Now create an offer where we reject both video and audio.
1993 rtc::scoped_ptr<SessionDescriptionInterface> local_offer;
kwiberg2bbff992016-03-16 11:03:04 -07001994 EXPECT_TRUE(DoCreateOffer(&local_offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07001995 video_info = local_offer->description()->GetContentByName("video");
1996 ASSERT_TRUE(video_info != nullptr);
1997 video_info->rejected = true;
1998 cricket::ContentInfo* audio_info =
1999 local_offer->description()->GetContentByName("audio");
2000 ASSERT_TRUE(audio_info != nullptr);
2001 audio_info->rejected = true;
2002 EXPECT_TRUE(DoSetLocalDescription(local_offer.release()));
2003 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kEnded, remote_video->state());
2004 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kEnded, remote_audio->state());
2005}
2006
2007// This tests that we won't crash if the remote track has been removed outside
2008// of PeerConnection and then PeerConnection tries to reject the track.
2009TEST_F(PeerConnectionInterfaceTest, RemoveTrackThenRejectMediaContent) {
2010 FakeConstraints constraints;
2011 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2012 true);
2013 CreatePeerConnection(&constraints);
2014 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2015 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2016 remote_stream->RemoveTrack(remote_stream->GetVideoTracks()[0]);
2017 remote_stream->RemoveTrack(remote_stream->GetAudioTracks()[0]);
2018
2019 rtc::scoped_ptr<SessionDescriptionInterface> local_answer(
2020 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
2021 kSdpStringWithStream1, nullptr));
2022 cricket::ContentInfo* video_info =
2023 local_answer->description()->GetContentByName("video");
2024 video_info->rejected = true;
2025 cricket::ContentInfo* audio_info =
2026 local_answer->description()->GetContentByName("audio");
2027 audio_info->rejected = true;
2028 EXPECT_TRUE(DoSetLocalDescription(local_answer.release()));
2029
2030 // No crash is a pass.
2031}
2032
deadbeef5e97fb52015-10-15 12:49:08 -07002033// This tests that if a recvonly remote description is set, no remote streams
2034// will be created, even if the description contains SSRCs/MSIDs.
2035// See: https://code.google.com/p/webrtc/issues/detail?id=5054
2036TEST_F(PeerConnectionInterfaceTest, RecvonlyDescriptionDoesntCreateStream) {
2037 FakeConstraints constraints;
2038 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2039 true);
2040 CreatePeerConnection(&constraints);
2041
2042 std::string recvonly_offer = kSdpStringWithStream1;
2043 rtc::replace_substrs(kSendrecv, strlen(kSendrecv), kRecvonly,
2044 strlen(kRecvonly), &recvonly_offer);
2045 CreateAndSetRemoteOffer(recvonly_offer);
2046
2047 EXPECT_EQ(0u, observer_.remote_streams()->count());
2048}
2049
Stefan Holmer55d6e7c2016-03-17 16:26:40 +01002050// Disabled on Win dbg: https://bugs.chromium.org/p/webrtc/issues/detail?id=5659
Stefan Holmer102362b2016-03-18 09:39:07 +01002051#if defined(WEBRTC_WIN) && defined(_DEBUG)
Stefan Holmer55d6e7c2016-03-17 16:26:40 +01002052#define MAYBE_SdpWithoutMsidCreatesDefaultStream \
2053 DISABLED_SdpWithoutMsidCreatesDefaultStream
2054#else
2055#define MAYBE_SdpWithoutMsidCreatesDefaultStream \
2056 SdpWithoutMsidCreatesDefaultStream
2057#endif
deadbeefab9b2d12015-10-14 11:33:11 -07002058// This tests that a default MediaStream is created if a remote session
2059// description doesn't contain any streams and no MSID support.
2060// It also tests that the default stream is updated if a video m-line is added
2061// in a subsequent session description.
Stefan Holmer102362b2016-03-18 09:39:07 +01002062TEST_F(PeerConnectionInterfaceTest, MAYBE_SdpWithoutMsidCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002063 FakeConstraints constraints;
2064 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2065 true);
2066 CreatePeerConnection(&constraints);
2067 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2068
2069 ASSERT_EQ(1u, observer_.remote_streams()->count());
2070 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2071
2072 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2073 EXPECT_EQ(0u, remote_stream->GetVideoTracks().size());
2074 EXPECT_EQ("default", remote_stream->label());
2075
2076 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2077 ASSERT_EQ(1u, observer_.remote_streams()->count());
2078 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2079 EXPECT_EQ("defaulta0", remote_stream->GetAudioTracks()[0]->id());
deadbeef884f5852016-01-15 09:20:04 -08002080 EXPECT_EQ(MediaStreamTrackInterface::kLive,
2081 remote_stream->GetAudioTracks()[0]->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002082 ASSERT_EQ(1u, remote_stream->GetVideoTracks().size());
2083 EXPECT_EQ("defaultv0", remote_stream->GetVideoTracks()[0]->id());
deadbeef884f5852016-01-15 09:20:04 -08002084 EXPECT_EQ(MediaStreamTrackInterface::kLive,
2085 remote_stream->GetVideoTracks()[0]->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002086}
2087
Stefan Holmer55d6e7c2016-03-17 16:26:40 +01002088// Disabled on Win dbg: https://bugs.chromium.org/p/webrtc/issues/detail?id=5659
Stefan Holmer102362b2016-03-18 09:39:07 +01002089#if defined(WEBRTC_WIN) && defined(_DEBUG)
Stefan Holmer55d6e7c2016-03-17 16:26:40 +01002090#define MAYBE_SendOnlySdpWithoutMsidCreatesDefaultStream \
2091 DISABLED_SendOnlySdpWithoutMsidCreatesDefaultStream
2092#else
2093#define MAYBE_SendOnlySdpWithoutMsidCreatesDefaultStream \
2094 SendOnlySdpWithoutMsidCreatesDefaultStream
2095#endif
deadbeefab9b2d12015-10-14 11:33:11 -07002096// This tests that a default MediaStream is created if a remote session
2097// description doesn't contain any streams and media direction is send only.
2098TEST_F(PeerConnectionInterfaceTest,
Stefan Holmer55d6e7c2016-03-17 16:26:40 +01002099 MAYBE_SendOnlySdpWithoutMsidCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002100 FakeConstraints constraints;
2101 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2102 true);
2103 CreatePeerConnection(&constraints);
2104 CreateAndSetRemoteOffer(kSdpStringSendOnlyWithoutStreams);
2105
2106 ASSERT_EQ(1u, observer_.remote_streams()->count());
2107 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2108
2109 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2110 EXPECT_EQ(1u, remote_stream->GetVideoTracks().size());
2111 EXPECT_EQ("default", remote_stream->label());
2112}
2113
2114// This tests that it won't crash when PeerConnection tries to remove
2115// a remote track that as already been removed from the MediaStream.
2116TEST_F(PeerConnectionInterfaceTest, RemoveAlreadyGoneRemoteStream) {
2117 FakeConstraints constraints;
2118 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2119 true);
2120 CreatePeerConnection(&constraints);
2121 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2122 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2123 remote_stream->RemoveTrack(remote_stream->GetAudioTracks()[0]);
2124 remote_stream->RemoveTrack(remote_stream->GetVideoTracks()[0]);
2125
2126 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2127
2128 // No crash is a pass.
2129}
2130
Stefan Holmer55d6e7c2016-03-17 16:26:40 +01002131// Disabled on Win dbg: https://bugs.chromium.org/p/webrtc/issues/detail?id=5659
Stefan Holmer102362b2016-03-18 09:39:07 +01002132#if defined(WEBRTC_WIN) && defined(_DEBUG)
Stefan Holmer55d6e7c2016-03-17 16:26:40 +01002133#define MAYBE_SdpWithoutMsidAndStreamsCreatesDefaultStream \
2134 DISABLED_SdpWithoutMsidAndStreamsCreatesDefaultStream
2135#else
2136#define MAYBE_SdpWithoutMsidAndStreamsCreatesDefaultStream \
2137 SdpWithoutMsidAndStreamsCreatesDefaultStream
2138#endif
deadbeefab9b2d12015-10-14 11:33:11 -07002139// This tests that a default MediaStream is created if the remote session
2140// description doesn't contain any streams and don't contain an indication if
2141// MSID is supported.
2142TEST_F(PeerConnectionInterfaceTest,
Stefan Holmer55d6e7c2016-03-17 16:26:40 +01002143 MAYBE_SdpWithoutMsidAndStreamsCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002144 FakeConstraints constraints;
2145 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2146 true);
2147 CreatePeerConnection(&constraints);
2148 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2149
2150 ASSERT_EQ(1u, observer_.remote_streams()->count());
2151 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2152 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2153 EXPECT_EQ(1u, remote_stream->GetVideoTracks().size());
2154}
2155
Stefan Holmer55d6e7c2016-03-17 16:26:40 +01002156// Disabled on Win dbg: https://bugs.chromium.org/p/webrtc/issues/detail?id=5659
Stefan Holmer102362b2016-03-18 09:39:07 +01002157#if defined(WEBRTC_WIN) && defined(_DEBUG)
Stefan Holmer55d6e7c2016-03-17 16:26:40 +01002158#define MAYBE_SdpWithMsidDontCreatesDefaultStream \
2159 DISABLED_SdpWithMsidDontCreatesDefaultStream
2160#else
2161#define MAYBE_SdpWithMsidDontCreatesDefaultStream \
2162 SdpWithMsidDontCreatesDefaultStream
2163#endif
deadbeefab9b2d12015-10-14 11:33:11 -07002164// This tests that a default MediaStream is not created if the remote session
2165// description doesn't contain any streams but does support MSID.
Stefan Holmer55d6e7c2016-03-17 16:26:40 +01002166TEST_F(PeerConnectionInterfaceTest, MAYBE_SdpWithMsidDontCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002167 FakeConstraints constraints;
2168 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2169 true);
2170 CreatePeerConnection(&constraints);
2171 CreateAndSetRemoteOffer(kSdpStringWithMsidWithoutStreams);
2172 EXPECT_EQ(0u, observer_.remote_streams()->count());
2173}
2174
Stefan Holmer55d6e7c2016-03-17 16:26:40 +01002175// Disabled on Win dbg: https://bugs.chromium.org/p/webrtc/issues/detail?id=5659
Stefan Holmer102362b2016-03-18 09:39:07 +01002176#if defined(WEBRTC_WIN) && defined(_DEBUG)
Stefan Holmer55d6e7c2016-03-17 16:26:40 +01002177#define MAYBE_DefaultTracksNotDestroyedAndRecreated \
2178 DISABLED_DefaultTracksNotDestroyedAndRecreated
2179#else
2180#define MAYBE_DefaultTracksNotDestroyedAndRecreated \
2181 DefaultTracksNotDestroyedAndRecreated
2182#endif
deadbeefbda7e0b2015-12-08 17:13:40 -08002183// This tests that when setting a new description, the old default tracks are
2184// not destroyed and recreated.
2185// See: https://bugs.chromium.org/p/webrtc/issues/detail?id=5250
Stefan Holmer102362b2016-03-18 09:39:07 +01002186TEST_F(PeerConnectionInterfaceTest,
2187 MAYBE_DefaultTracksNotDestroyedAndRecreated) {
deadbeefbda7e0b2015-12-08 17:13:40 -08002188 FakeConstraints constraints;
2189 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2190 true);
2191 CreatePeerConnection(&constraints);
2192 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2193
2194 ASSERT_EQ(1u, observer_.remote_streams()->count());
2195 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2196 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2197
2198 // Set the track to "disabled", then set a new description and ensure the
2199 // track is still disabled, which ensures it hasn't been recreated.
2200 remote_stream->GetAudioTracks()[0]->set_enabled(false);
2201 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2202 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2203 EXPECT_FALSE(remote_stream->GetAudioTracks()[0]->enabled());
2204}
2205
deadbeefab9b2d12015-10-14 11:33:11 -07002206// This tests that a default MediaStream is not created if a remote session
2207// description is updated to not have any MediaStreams.
2208TEST_F(PeerConnectionInterfaceTest, VerifyDefaultStreamIsNotCreated) {
2209 FakeConstraints constraints;
2210 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2211 true);
2212 CreatePeerConnection(&constraints);
2213 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2214 rtc::scoped_refptr<StreamCollection> reference(CreateStreamCollection(1));
2215 EXPECT_TRUE(
2216 CompareStreamCollections(observer_.remote_streams(), reference.get()));
2217
2218 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2219 EXPECT_EQ(0u, observer_.remote_streams()->count());
2220}
2221
Stefan Holmer55d6e7c2016-03-17 16:26:40 +01002222// Disabled on Win dbg: https://bugs.chromium.org/p/webrtc/issues/detail?id=5659
Stefan Holmer102362b2016-03-18 09:39:07 +01002223#if defined(WEBRTC_WIN) && defined(_DEBUG)
Stefan Holmer55d6e7c2016-03-17 16:26:40 +01002224#define MAYBE_LocalDescriptionChanged DISABLED_LocalDescriptionChanged
2225#else
2226#define MAYBE_LocalDescriptionChanged LocalDescriptionChanged
2227#endif
deadbeefab9b2d12015-10-14 11:33:11 -07002228// This tests that an RtpSender is created when the local description is set
2229// after adding a local stream.
2230// TODO(deadbeef): This test and the one below it need to be updated when
2231// an RtpSender's lifetime isn't determined by when a local description is set.
Stefan Holmer55d6e7c2016-03-17 16:26:40 +01002232TEST_F(PeerConnectionInterfaceTest, MAYBE_LocalDescriptionChanged) {
deadbeefab9b2d12015-10-14 11:33:11 -07002233 FakeConstraints constraints;
2234 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2235 true);
2236 CreatePeerConnection(&constraints);
2237 // Create an offer just to ensure we have an identity before we manually
2238 // call SetLocalDescription.
2239 rtc::scoped_ptr<SessionDescriptionInterface> throwaway;
kwiberg2bbff992016-03-16 11:03:04 -07002240 ASSERT_TRUE(DoCreateOffer(&throwaway, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07002241
kwiberg2bbff992016-03-16 11:03:04 -07002242 rtc::scoped_ptr<SessionDescriptionInterface> desc_1 =
2243 CreateSessionDescriptionAndReference(2, 2);
deadbeefab9b2d12015-10-14 11:33:11 -07002244
2245 pc_->AddStream(reference_collection_->at(0));
2246 EXPECT_TRUE(DoSetLocalDescription(desc_1.release()));
2247 auto senders = pc_->GetSenders();
2248 EXPECT_EQ(4u, senders.size());
2249 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2250 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2251 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[1]));
2252 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[1]));
2253
2254 // Remove an audio and video track.
deadbeeffac06552015-11-25 11:26:01 -08002255 pc_->RemoveStream(reference_collection_->at(0));
kwiberg2bbff992016-03-16 11:03:04 -07002256 rtc::scoped_ptr<SessionDescriptionInterface> desc_2 =
2257 CreateSessionDescriptionAndReference(1, 1);
deadbeeffac06552015-11-25 11:26:01 -08002258 pc_->AddStream(reference_collection_->at(0));
deadbeefab9b2d12015-10-14 11:33:11 -07002259 EXPECT_TRUE(DoSetLocalDescription(desc_2.release()));
2260 senders = pc_->GetSenders();
2261 EXPECT_EQ(2u, senders.size());
2262 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2263 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2264 EXPECT_FALSE(ContainsSender(senders, kAudioTracks[1]));
2265 EXPECT_FALSE(ContainsSender(senders, kVideoTracks[1]));
2266}
2267
Stefan Holmer55d6e7c2016-03-17 16:26:40 +01002268// Disabled on Win dbg: https://bugs.chromium.org/p/webrtc/issues/detail?id=5659
Stefan Holmer102362b2016-03-18 09:39:07 +01002269#if defined(WEBRTC_WIN) && defined(_DEBUG)
Stefan Holmer55d6e7c2016-03-17 16:26:40 +01002270#define MAYBE_AddLocalStreamAfterLocalDescriptionChanged \
2271 DISABLED_AddLocalStreamAfterLocalDescriptionChanged
2272#else
2273#define MAYBE_AddLocalStreamAfterLocalDescriptionChanged \
2274 AddLocalStreamAfterLocalDescriptionChanged
2275#endif
deadbeefab9b2d12015-10-14 11:33:11 -07002276// This tests that an RtpSender is created when the local description is set
2277// before adding a local stream.
2278TEST_F(PeerConnectionInterfaceTest,
Stefan Holmer55d6e7c2016-03-17 16:26:40 +01002279 MAYBE_AddLocalStreamAfterLocalDescriptionChanged) {
deadbeefab9b2d12015-10-14 11:33:11 -07002280 FakeConstraints constraints;
2281 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2282 true);
2283 CreatePeerConnection(&constraints);
2284 // Create an offer just to ensure we have an identity before we manually
2285 // call SetLocalDescription.
2286 rtc::scoped_ptr<SessionDescriptionInterface> throwaway;
kwiberg2bbff992016-03-16 11:03:04 -07002287 ASSERT_TRUE(DoCreateOffer(&throwaway, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07002288
kwiberg2bbff992016-03-16 11:03:04 -07002289 rtc::scoped_ptr<SessionDescriptionInterface> desc_1 =
2290 CreateSessionDescriptionAndReference(2, 2);
deadbeefab9b2d12015-10-14 11:33:11 -07002291
2292 EXPECT_TRUE(DoSetLocalDescription(desc_1.release()));
2293 auto senders = pc_->GetSenders();
2294 EXPECT_EQ(0u, senders.size());
2295
2296 pc_->AddStream(reference_collection_->at(0));
2297 senders = pc_->GetSenders();
2298 EXPECT_EQ(4u, senders.size());
2299 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2300 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2301 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[1]));
2302 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[1]));
2303}
2304
Stefan Holmer55d6e7c2016-03-17 16:26:40 +01002305// Disabled on Win dbg: https://bugs.chromium.org/p/webrtc/issues/detail?id=5659
Stefan Holmer102362b2016-03-18 09:39:07 +01002306#if defined(WEBRTC_WIN) && defined(_DEBUG)
Stefan Holmer55d6e7c2016-03-17 16:26:40 +01002307#define MAYBE_ChangeSsrcOnTrackInLocalSessionDescription \
2308 DISABLED_ChangeSsrcOnTrackInLocalSessionDescription
2309#else
2310#define MAYBE_ChangeSsrcOnTrackInLocalSessionDescription \
2311 ChangeSsrcOnTrackInLocalSessionDescription
2312#endif
deadbeefab9b2d12015-10-14 11:33:11 -07002313// This tests that the expected behavior occurs if the SSRC on a local track is
2314// changed when SetLocalDescription is called.
2315TEST_F(PeerConnectionInterfaceTest,
Stefan Holmer55d6e7c2016-03-17 16:26:40 +01002316 MAYBE_ChangeSsrcOnTrackInLocalSessionDescription) {
deadbeefab9b2d12015-10-14 11:33:11 -07002317 FakeConstraints constraints;
2318 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2319 true);
2320 CreatePeerConnection(&constraints);
2321 // Create an offer just to ensure we have an identity before we manually
2322 // call SetLocalDescription.
2323 rtc::scoped_ptr<SessionDescriptionInterface> throwaway;
kwiberg2bbff992016-03-16 11:03:04 -07002324 ASSERT_TRUE(DoCreateOffer(&throwaway, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07002325
kwiberg2bbff992016-03-16 11:03:04 -07002326 rtc::scoped_ptr<SessionDescriptionInterface> desc =
2327 CreateSessionDescriptionAndReference(1, 1);
deadbeefab9b2d12015-10-14 11:33:11 -07002328 std::string sdp;
2329 desc->ToString(&sdp);
2330
2331 pc_->AddStream(reference_collection_->at(0));
2332 EXPECT_TRUE(DoSetLocalDescription(desc.release()));
2333 auto senders = pc_->GetSenders();
2334 EXPECT_EQ(2u, senders.size());
2335 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2336 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2337
2338 // Change the ssrc of the audio and video track.
2339 std::string ssrc_org = "a=ssrc:1";
2340 std::string ssrc_to = "a=ssrc:97";
2341 rtc::replace_substrs(ssrc_org.c_str(), ssrc_org.length(), ssrc_to.c_str(),
2342 ssrc_to.length(), &sdp);
2343 ssrc_org = "a=ssrc:2";
2344 ssrc_to = "a=ssrc:98";
2345 rtc::replace_substrs(ssrc_org.c_str(), ssrc_org.length(), ssrc_to.c_str(),
2346 ssrc_to.length(), &sdp);
2347 rtc::scoped_ptr<SessionDescriptionInterface> updated_desc(
2348 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer, sdp,
2349 nullptr));
2350
2351 EXPECT_TRUE(DoSetLocalDescription(updated_desc.release()));
2352 senders = pc_->GetSenders();
2353 EXPECT_EQ(2u, senders.size());
2354 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2355 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2356 // TODO(deadbeef): Once RtpSenders expose parameters, check that the SSRC
2357 // changed.
2358}
2359
Stefan Holmer55d6e7c2016-03-17 16:26:40 +01002360// Disabled on Win dbg: https://bugs.chromium.org/p/webrtc/issues/detail?id=5659
Stefan Holmer102362b2016-03-18 09:39:07 +01002361#if defined(WEBRTC_WIN) && defined(_DEBUG)
Stefan Holmer55d6e7c2016-03-17 16:26:40 +01002362#define MAYBE_SignalSameTracksInSeparateMediaStream \
2363 DISABLED_SignalSameTracksInSeparateMediaStream
2364#else
2365#define MAYBE_SignalSameTracksInSeparateMediaStream \
2366 SignalSameTracksInSeparateMediaStream
2367#endif
deadbeefab9b2d12015-10-14 11:33:11 -07002368// This tests that the expected behavior occurs if a new session description is
2369// set with the same tracks, but on a different MediaStream.
Stefan Holmer55d6e7c2016-03-17 16:26:40 +01002370TEST_F(PeerConnectionInterfaceTest,
2371 MAYBE_SignalSameTracksInSeparateMediaStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002372 FakeConstraints constraints;
2373 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2374 true);
2375 CreatePeerConnection(&constraints);
2376 // Create an offer just to ensure we have an identity before we manually
2377 // call SetLocalDescription.
2378 rtc::scoped_ptr<SessionDescriptionInterface> throwaway;
kwiberg2bbff992016-03-16 11:03:04 -07002379 ASSERT_TRUE(DoCreateOffer(&throwaway, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07002380
kwiberg2bbff992016-03-16 11:03:04 -07002381 rtc::scoped_ptr<SessionDescriptionInterface> desc =
2382 CreateSessionDescriptionAndReference(1, 1);
deadbeefab9b2d12015-10-14 11:33:11 -07002383 std::string sdp;
2384 desc->ToString(&sdp);
2385
2386 pc_->AddStream(reference_collection_->at(0));
2387 EXPECT_TRUE(DoSetLocalDescription(desc.release()));
2388 auto senders = pc_->GetSenders();
2389 EXPECT_EQ(2u, senders.size());
2390 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2391 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2392
2393 // Add a new MediaStream but with the same tracks as in the first stream.
2394 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream_1(
2395 webrtc::MediaStream::Create(kStreams[1]));
2396 stream_1->AddTrack(reference_collection_->at(0)->GetVideoTracks()[0]);
2397 stream_1->AddTrack(reference_collection_->at(0)->GetAudioTracks()[0]);
2398 pc_->AddStream(stream_1);
2399
2400 // Replace msid in the original SDP.
2401 rtc::replace_substrs(kStreams[0], strlen(kStreams[0]), kStreams[1],
2402 strlen(kStreams[1]), &sdp);
2403
2404 rtc::scoped_ptr<SessionDescriptionInterface> updated_desc(
2405 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer, sdp,
2406 nullptr));
2407
2408 EXPECT_TRUE(DoSetLocalDescription(updated_desc.release()));
2409 senders = pc_->GetSenders();
2410 EXPECT_EQ(2u, senders.size());
2411 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2412 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2413}
2414
nisse51542be2016-02-12 02:27:06 -08002415// The PeerConnectionMediaConfig tests below verify that configuration
2416// and constraints are propagated into the MediaConfig passed to
2417// CreateMediaController. These settings are intended for MediaChannel
2418// constructors, but that is not exercised by these unittest.
2419class PeerConnectionFactoryForTest : public webrtc::PeerConnectionFactory {
2420 public:
2421 webrtc::MediaControllerInterface* CreateMediaController(
2422 const cricket::MediaConfig& config) const override {
2423 create_media_controller_called_ = true;
2424 create_media_controller_config_ = config;
2425
2426 webrtc::MediaControllerInterface* mc =
2427 PeerConnectionFactory::CreateMediaController(config);
2428 EXPECT_TRUE(mc != nullptr);
2429 return mc;
2430 }
2431
2432 // Mutable, so they can be modified in the above const-declared method.
2433 mutable bool create_media_controller_called_ = false;
2434 mutable cricket::MediaConfig create_media_controller_config_;
2435};
2436
2437class PeerConnectionMediaConfigTest : public testing::Test {
2438 protected:
2439 void SetUp() override {
nisseaf510af2016-03-21 08:20:42 -07002440 pcf_ = new rtc::RefCountedObject<PeerConnectionFactoryForTest>();
nisse51542be2016-02-12 02:27:06 -08002441 pcf_->Initialize();
2442 }
2443 const cricket::MediaConfig& TestCreatePeerConnection(
2444 const PeerConnectionInterface::RTCConfiguration& config,
2445 const MediaConstraintsInterface *constraints) {
2446 pcf_->create_media_controller_called_ = false;
2447
2448 scoped_refptr<PeerConnectionInterface> pc(
2449 pcf_->CreatePeerConnection(config, constraints, nullptr, nullptr,
2450 &observer_));
2451 EXPECT_TRUE(pc.get());
2452 EXPECT_TRUE(pcf_->create_media_controller_called_);
2453 return pcf_->create_media_controller_config_;
2454 }
2455
2456 scoped_refptr<PeerConnectionFactoryForTest> pcf_;
2457 MockPeerConnectionObserver observer_;
2458};
2459
2460// This test verifies the default behaviour with no constraints and a
2461// default RTCConfiguration.
2462TEST_F(PeerConnectionMediaConfigTest, TestDefaults) {
2463 PeerConnectionInterface::RTCConfiguration config;
2464 FakeConstraints constraints;
2465
2466 const cricket::MediaConfig& media_config =
2467 TestCreatePeerConnection(config, &constraints);
2468
2469 EXPECT_FALSE(media_config.enable_dscp);
nisse0db023a2016-03-01 04:29:59 -08002470 EXPECT_TRUE(media_config.video.enable_cpu_overuse_detection);
2471 EXPECT_FALSE(media_config.video.disable_prerenderer_smoothing);
2472 EXPECT_FALSE(media_config.video.suspend_below_min_bitrate);
nisse51542be2016-02-12 02:27:06 -08002473}
2474
2475// This test verifies the DSCP constraint is recognized and passed to
2476// the CreateMediaController call.
2477TEST_F(PeerConnectionMediaConfigTest, TestDscpConstraintTrue) {
2478 PeerConnectionInterface::RTCConfiguration config;
2479 FakeConstraints constraints;
2480
2481 constraints.AddOptional(webrtc::MediaConstraintsInterface::kEnableDscp, true);
2482 const cricket::MediaConfig& media_config =
2483 TestCreatePeerConnection(config, &constraints);
2484
2485 EXPECT_TRUE(media_config.enable_dscp);
2486}
2487
2488// This test verifies the cpu overuse detection constraint is
2489// recognized and passed to the CreateMediaController call.
2490TEST_F(PeerConnectionMediaConfigTest, TestCpuOveruseConstraintFalse) {
2491 PeerConnectionInterface::RTCConfiguration config;
2492 FakeConstraints constraints;
2493
2494 constraints.AddOptional(
2495 webrtc::MediaConstraintsInterface::kCpuOveruseDetection, false);
2496 const cricket::MediaConfig media_config =
2497 TestCreatePeerConnection(config, &constraints);
2498
nisse0db023a2016-03-01 04:29:59 -08002499 EXPECT_FALSE(media_config.video.enable_cpu_overuse_detection);
nisse51542be2016-02-12 02:27:06 -08002500}
2501
2502// This test verifies that the disable_prerenderer_smoothing flag is
2503// propagated from RTCConfiguration to the CreateMediaController call.
2504TEST_F(PeerConnectionMediaConfigTest, TestDisablePrerendererSmoothingTrue) {
2505 PeerConnectionInterface::RTCConfiguration config;
2506 FakeConstraints constraints;
2507
2508 config.disable_prerenderer_smoothing = true;
2509 const cricket::MediaConfig& media_config =
2510 TestCreatePeerConnection(config, &constraints);
2511
nisse0db023a2016-03-01 04:29:59 -08002512 EXPECT_TRUE(media_config.video.disable_prerenderer_smoothing);
2513}
2514
2515// This test verifies the suspend below min bitrate constraint is
2516// recognized and passed to the CreateMediaController call.
2517TEST_F(PeerConnectionMediaConfigTest,
2518 TestSuspendBelowMinBitrateConstraintTrue) {
2519 PeerConnectionInterface::RTCConfiguration config;
2520 FakeConstraints constraints;
2521
2522 constraints.AddOptional(
2523 webrtc::MediaConstraintsInterface::kEnableVideoSuspendBelowMinBitrate,
2524 true);
2525 const cricket::MediaConfig media_config =
2526 TestCreatePeerConnection(config, &constraints);
2527
2528 EXPECT_TRUE(media_config.video.suspend_below_min_bitrate);
nisse51542be2016-02-12 02:27:06 -08002529}
2530
deadbeefab9b2d12015-10-14 11:33:11 -07002531// The following tests verify that session options are created correctly.
deadbeefc80741f2015-10-22 13:14:45 -07002532// TODO(deadbeef): Convert these tests to be more end-to-end. Instead of
2533// "verify options are converted correctly", should be "pass options into
2534// CreateOffer and verify the correct offer is produced."
deadbeefab9b2d12015-10-14 11:33:11 -07002535
2536TEST(CreateSessionOptionsTest, GetOptionsForOfferWithInvalidAudioOption) {
2537 RTCOfferAnswerOptions rtc_options;
2538 rtc_options.offer_to_receive_audio = RTCOfferAnswerOptions::kUndefined - 1;
2539
2540 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08002541 EXPECT_FALSE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002542
2543 rtc_options.offer_to_receive_audio =
2544 RTCOfferAnswerOptions::kMaxOfferToReceiveMedia + 1;
htaaac2dea2016-03-10 13:35:55 -08002545 EXPECT_FALSE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002546}
2547
2548TEST(CreateSessionOptionsTest, GetOptionsForOfferWithInvalidVideoOption) {
2549 RTCOfferAnswerOptions rtc_options;
2550 rtc_options.offer_to_receive_video = RTCOfferAnswerOptions::kUndefined - 1;
2551
2552 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08002553 EXPECT_FALSE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002554
2555 rtc_options.offer_to_receive_video =
2556 RTCOfferAnswerOptions::kMaxOfferToReceiveMedia + 1;
htaaac2dea2016-03-10 13:35:55 -08002557 EXPECT_FALSE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002558}
2559
2560// Test that a MediaSessionOptions is created for an offer if
deadbeefc80741f2015-10-22 13:14:45 -07002561// OfferToReceiveAudio and OfferToReceiveVideo options are set.
deadbeefab9b2d12015-10-14 11:33:11 -07002562TEST(CreateSessionOptionsTest, GetMediaSessionOptionsForOfferWithAudioVideo) {
2563 RTCOfferAnswerOptions rtc_options;
2564 rtc_options.offer_to_receive_audio = 1;
2565 rtc_options.offer_to_receive_video = 1;
2566
2567 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08002568 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002569 EXPECT_TRUE(options.has_audio());
2570 EXPECT_TRUE(options.has_video());
2571 EXPECT_TRUE(options.bundle_enabled);
2572}
2573
2574// Test that a correct MediaSessionOptions is created for an offer if
deadbeefc80741f2015-10-22 13:14:45 -07002575// OfferToReceiveAudio is set.
deadbeefab9b2d12015-10-14 11:33:11 -07002576TEST(CreateSessionOptionsTest, GetMediaSessionOptionsForOfferWithAudio) {
2577 RTCOfferAnswerOptions rtc_options;
2578 rtc_options.offer_to_receive_audio = 1;
2579
2580 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08002581 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002582 EXPECT_TRUE(options.has_audio());
2583 EXPECT_FALSE(options.has_video());
2584 EXPECT_TRUE(options.bundle_enabled);
2585}
2586
2587// Test that a correct MediaSessionOptions is created for an offer if
deadbeefc80741f2015-10-22 13:14:45 -07002588// the default OfferOptions are used.
deadbeefab9b2d12015-10-14 11:33:11 -07002589TEST(CreateSessionOptionsTest, GetDefaultMediaSessionOptionsForOffer) {
2590 RTCOfferAnswerOptions rtc_options;
2591
2592 cricket::MediaSessionOptions options;
deadbeef0ed85b22016-02-23 17:24:52 -08002593 options.transport_options["audio"] = cricket::TransportOptions();
2594 options.transport_options["video"] = cricket::TransportOptions();
htaaac2dea2016-03-10 13:35:55 -08002595 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefc80741f2015-10-22 13:14:45 -07002596 EXPECT_TRUE(options.has_audio());
deadbeefab9b2d12015-10-14 11:33:11 -07002597 EXPECT_FALSE(options.has_video());
deadbeefc80741f2015-10-22 13:14:45 -07002598 EXPECT_TRUE(options.bundle_enabled);
deadbeefab9b2d12015-10-14 11:33:11 -07002599 EXPECT_TRUE(options.vad_enabled);
deadbeef0ed85b22016-02-23 17:24:52 -08002600 EXPECT_FALSE(options.transport_options["audio"].ice_restart);
2601 EXPECT_FALSE(options.transport_options["video"].ice_restart);
deadbeefab9b2d12015-10-14 11:33:11 -07002602}
2603
2604// Test that a correct MediaSessionOptions is created for an offer if
deadbeefc80741f2015-10-22 13:14:45 -07002605// OfferToReceiveVideo is set.
deadbeefab9b2d12015-10-14 11:33:11 -07002606TEST(CreateSessionOptionsTest, GetMediaSessionOptionsForOfferWithVideo) {
2607 RTCOfferAnswerOptions rtc_options;
2608 rtc_options.offer_to_receive_audio = 0;
2609 rtc_options.offer_to_receive_video = 1;
2610
2611 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08002612 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002613 EXPECT_FALSE(options.has_audio());
2614 EXPECT_TRUE(options.has_video());
2615 EXPECT_TRUE(options.bundle_enabled);
2616}
2617
2618// Test that a correct MediaSessionOptions is created for an offer if
2619// UseRtpMux is set to false.
2620TEST(CreateSessionOptionsTest,
2621 GetMediaSessionOptionsForOfferWithBundleDisabled) {
2622 RTCOfferAnswerOptions rtc_options;
2623 rtc_options.offer_to_receive_audio = 1;
2624 rtc_options.offer_to_receive_video = 1;
2625 rtc_options.use_rtp_mux = false;
2626
2627 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08002628 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002629 EXPECT_TRUE(options.has_audio());
2630 EXPECT_TRUE(options.has_video());
2631 EXPECT_FALSE(options.bundle_enabled);
2632}
2633
2634// Test that a correct MediaSessionOptions is created to restart ice if
2635// IceRestart is set. It also tests that subsequent MediaSessionOptions don't
Taylor Brandstetterf475d362016-01-08 15:35:57 -08002636// have |audio_transport_options.ice_restart| etc. set.
deadbeefab9b2d12015-10-14 11:33:11 -07002637TEST(CreateSessionOptionsTest, GetMediaSessionOptionsForOfferWithIceRestart) {
2638 RTCOfferAnswerOptions rtc_options;
2639 rtc_options.ice_restart = true;
2640
2641 cricket::MediaSessionOptions options;
deadbeef0ed85b22016-02-23 17:24:52 -08002642 options.transport_options["audio"] = cricket::TransportOptions();
2643 options.transport_options["video"] = cricket::TransportOptions();
htaaac2dea2016-03-10 13:35:55 -08002644 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeef0ed85b22016-02-23 17:24:52 -08002645 EXPECT_TRUE(options.transport_options["audio"].ice_restart);
2646 EXPECT_TRUE(options.transport_options["video"].ice_restart);
deadbeefab9b2d12015-10-14 11:33:11 -07002647
2648 rtc_options = RTCOfferAnswerOptions();
htaaac2dea2016-03-10 13:35:55 -08002649 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeef0ed85b22016-02-23 17:24:52 -08002650 EXPECT_FALSE(options.transport_options["audio"].ice_restart);
2651 EXPECT_FALSE(options.transport_options["video"].ice_restart);
deadbeefab9b2d12015-10-14 11:33:11 -07002652}
2653
2654// Test that the MediaConstraints in an answer don't affect if audio and video
2655// is offered in an offer but that if kOfferToReceiveAudio or
2656// kOfferToReceiveVideo constraints are true in an offer, the media type will be
2657// included in subsequent answers.
2658TEST(CreateSessionOptionsTest, MediaConstraintsInAnswer) {
2659 FakeConstraints answer_c;
2660 answer_c.SetMandatoryReceiveAudio(true);
2661 answer_c.SetMandatoryReceiveVideo(true);
2662
2663 cricket::MediaSessionOptions answer_options;
2664 EXPECT_TRUE(ParseConstraintsForAnswer(&answer_c, &answer_options));
2665 EXPECT_TRUE(answer_options.has_audio());
2666 EXPECT_TRUE(answer_options.has_video());
2667
deadbeefc80741f2015-10-22 13:14:45 -07002668 RTCOfferAnswerOptions rtc_offer_options;
deadbeefab9b2d12015-10-14 11:33:11 -07002669
2670 cricket::MediaSessionOptions offer_options;
htaaac2dea2016-03-10 13:35:55 -08002671 EXPECT_TRUE(
2672 ExtractMediaSessionOptions(rtc_offer_options, false, &offer_options));
deadbeefc80741f2015-10-22 13:14:45 -07002673 EXPECT_TRUE(offer_options.has_audio());
htaaac2dea2016-03-10 13:35:55 -08002674 EXPECT_TRUE(offer_options.has_video());
deadbeefab9b2d12015-10-14 11:33:11 -07002675
deadbeefc80741f2015-10-22 13:14:45 -07002676 RTCOfferAnswerOptions updated_rtc_offer_options;
2677 updated_rtc_offer_options.offer_to_receive_audio = 1;
2678 updated_rtc_offer_options.offer_to_receive_video = 1;
deadbeefab9b2d12015-10-14 11:33:11 -07002679
2680 cricket::MediaSessionOptions updated_offer_options;
htaaac2dea2016-03-10 13:35:55 -08002681 EXPECT_TRUE(ExtractMediaSessionOptions(updated_rtc_offer_options, false,
htaa2a49d92016-03-04 02:51:39 -08002682 &updated_offer_options));
deadbeefab9b2d12015-10-14 11:33:11 -07002683 EXPECT_TRUE(updated_offer_options.has_audio());
2684 EXPECT_TRUE(updated_offer_options.has_video());
2685
2686 // Since an offer has been created with both audio and video, subsequent
2687 // offers and answers should contain both audio and video.
2688 // Answers will only contain the media types that exist in the offer
2689 // regardless of the value of |updated_answer_options.has_audio| and
2690 // |updated_answer_options.has_video|.
2691 FakeConstraints updated_answer_c;
2692 answer_c.SetMandatoryReceiveAudio(false);
2693 answer_c.SetMandatoryReceiveVideo(false);
2694
2695 cricket::MediaSessionOptions updated_answer_options;
2696 EXPECT_TRUE(
2697 ParseConstraintsForAnswer(&updated_answer_c, &updated_answer_options));
2698 EXPECT_TRUE(updated_answer_options.has_audio());
2699 EXPECT_TRUE(updated_answer_options.has_video());
deadbeefab9b2d12015-10-14 11:33:11 -07002700}