blob: 6bff11a4b8793f7584d9b0100f352acb191572ae [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"
28#include "webrtc/api/test/mockpeerconnectionobservers.h"
29#include "webrtc/api/test/testsdpstrings.h"
perkja3ede6c2016-03-08 01:27:48 +010030#include "webrtc/api/videocapturertracksource.h"
Henrik Kjellander15583c12016-02-10 10:53:12 +010031#include "webrtc/api/videotrack.h"
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000032#include "webrtc/base/gunit.h"
33#include "webrtc/base/scoped_ptr.h"
34#include "webrtc/base/ssladapter.h"
35#include "webrtc/base/sslstreamadapter.h"
36#include "webrtc/base/stringutils.h"
37#include "webrtc/base/thread.h"
kjellandera96e2d72016-02-04 23:52:28 -080038#include "webrtc/media/base/fakevideocapturer.h"
39#include "webrtc/media/sctp/sctpdataengine.h"
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -080040#include "webrtc/p2p/client/fakeportallocator.h"
kjellander@webrtc.org9b8df252016-02-12 06:47:59 +010041#include "webrtc/pc/mediasession.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000042
43static const char kStreamLabel1[] = "local_stream_1";
44static const char kStreamLabel2[] = "local_stream_2";
45static const char kStreamLabel3[] = "local_stream_3";
46static const int kDefaultStunPort = 3478;
47static const char kStunAddressOnly[] = "stun:address";
48static const char kStunInvalidPort[] = "stun:address:-1";
49static const char kStunAddressPortAndMore1[] = "stun:address:port:more";
50static const char kStunAddressPortAndMore2[] = "stun:address:port more";
51static const char kTurnIceServerUri[] = "turn:user@turn.example.org";
52static const char kTurnUsername[] = "user";
53static const char kTurnPassword[] = "password";
54static const char kTurnHostname[] = "turn.example.org";
Peter Boström0c4e06b2015-10-07 12:23:21 +020055static const uint32_t kTimeout = 10000U;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000056
deadbeefab9b2d12015-10-14 11:33:11 -070057static const char kStreams[][8] = {"stream1", "stream2"};
58static const char kAudioTracks[][32] = {"audiotrack0", "audiotrack1"};
59static const char kVideoTracks[][32] = {"videotrack0", "videotrack1"};
60
deadbeef5e97fb52015-10-15 12:49:08 -070061static const char kRecvonly[] = "recvonly";
62static const char kSendrecv[] = "sendrecv";
63
deadbeefab9b2d12015-10-14 11:33:11 -070064// Reference SDP with a MediaStream with label "stream1" and audio track with
65// id "audio_1" and a video track with id "video_1;
66static const char kSdpStringWithStream1[] =
67 "v=0\r\n"
68 "o=- 0 0 IN IP4 127.0.0.1\r\n"
69 "s=-\r\n"
70 "t=0 0\r\n"
71 "a=ice-ufrag:e5785931\r\n"
72 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
73 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
74 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
75 "m=audio 1 RTP/AVPF 103\r\n"
76 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -070077 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -070078 "a=rtpmap:103 ISAC/16000\r\n"
79 "a=ssrc:1 cname:stream1\r\n"
80 "a=ssrc:1 mslabel:stream1\r\n"
81 "a=ssrc:1 label:audiotrack0\r\n"
82 "m=video 1 RTP/AVPF 120\r\n"
83 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -070084 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -070085 "a=rtpmap:120 VP8/90000\r\n"
86 "a=ssrc:2 cname:stream1\r\n"
87 "a=ssrc:2 mslabel:stream1\r\n"
88 "a=ssrc:2 label:videotrack0\r\n";
89
90// Reference SDP with two MediaStreams with label "stream1" and "stream2. Each
91// MediaStreams have one audio track and one video track.
92// This uses MSID.
93static const char kSdpStringWithStream1And2[] =
94 "v=0\r\n"
95 "o=- 0 0 IN IP4 127.0.0.1\r\n"
96 "s=-\r\n"
97 "t=0 0\r\n"
98 "a=ice-ufrag:e5785931\r\n"
99 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
100 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
101 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
102 "a=msid-semantic: WMS stream1 stream2\r\n"
103 "m=audio 1 RTP/AVPF 103\r\n"
104 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700105 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700106 "a=rtpmap:103 ISAC/16000\r\n"
107 "a=ssrc:1 cname:stream1\r\n"
108 "a=ssrc:1 msid:stream1 audiotrack0\r\n"
109 "a=ssrc:3 cname:stream2\r\n"
110 "a=ssrc:3 msid:stream2 audiotrack1\r\n"
111 "m=video 1 RTP/AVPF 120\r\n"
112 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700113 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700114 "a=rtpmap:120 VP8/0\r\n"
115 "a=ssrc:2 cname:stream1\r\n"
116 "a=ssrc:2 msid:stream1 videotrack0\r\n"
117 "a=ssrc:4 cname:stream2\r\n"
118 "a=ssrc:4 msid:stream2 videotrack1\r\n";
119
120// Reference SDP without MediaStreams. Msid is not supported.
121static const char kSdpStringWithoutStreams[] =
122 "v=0\r\n"
123 "o=- 0 0 IN IP4 127.0.0.1\r\n"
124 "s=-\r\n"
125 "t=0 0\r\n"
126 "a=ice-ufrag:e5785931\r\n"
127 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
128 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
129 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
130 "m=audio 1 RTP/AVPF 103\r\n"
131 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700132 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700133 "a=rtpmap:103 ISAC/16000\r\n"
134 "m=video 1 RTP/AVPF 120\r\n"
135 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700136 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700137 "a=rtpmap:120 VP8/90000\r\n";
138
139// Reference SDP without MediaStreams. Msid is supported.
140static const char kSdpStringWithMsidWithoutStreams[] =
141 "v=0\r\n"
142 "o=- 0 0 IN IP4 127.0.0.1\r\n"
143 "s=-\r\n"
144 "t=0 0\r\n"
145 "a=ice-ufrag:e5785931\r\n"
146 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
147 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
148 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
149 "a=msid-semantic: WMS\r\n"
150 "m=audio 1 RTP/AVPF 103\r\n"
151 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700152 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700153 "a=rtpmap:103 ISAC/16000\r\n"
154 "m=video 1 RTP/AVPF 120\r\n"
155 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700156 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700157 "a=rtpmap:120 VP8/90000\r\n";
158
159// Reference SDP without MediaStreams and audio only.
160static const char kSdpStringWithoutStreamsAudioOnly[] =
161 "v=0\r\n"
162 "o=- 0 0 IN IP4 127.0.0.1\r\n"
163 "s=-\r\n"
164 "t=0 0\r\n"
165 "a=ice-ufrag:e5785931\r\n"
166 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
167 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
168 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
169 "m=audio 1 RTP/AVPF 103\r\n"
170 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700171 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700172 "a=rtpmap:103 ISAC/16000\r\n";
173
174// Reference SENDONLY SDP without MediaStreams. Msid is not supported.
175static const char kSdpStringSendOnlyWithoutStreams[] =
176 "v=0\r\n"
177 "o=- 0 0 IN IP4 127.0.0.1\r\n"
178 "s=-\r\n"
179 "t=0 0\r\n"
180 "a=ice-ufrag:e5785931\r\n"
181 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
182 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
183 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
184 "m=audio 1 RTP/AVPF 103\r\n"
185 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700186 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700187 "a=sendonly\r\n"
188 "a=rtpmap:103 ISAC/16000\r\n"
189 "m=video 1 RTP/AVPF 120\r\n"
190 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700191 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700192 "a=sendonly\r\n"
193 "a=rtpmap:120 VP8/90000\r\n";
194
195static const char kSdpStringInit[] =
196 "v=0\r\n"
197 "o=- 0 0 IN IP4 127.0.0.1\r\n"
198 "s=-\r\n"
199 "t=0 0\r\n"
200 "a=ice-ufrag:e5785931\r\n"
201 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
202 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
203 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
204 "a=msid-semantic: WMS\r\n";
205
206static const char kSdpStringAudio[] =
207 "m=audio 1 RTP/AVPF 103\r\n"
208 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700209 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700210 "a=rtpmap:103 ISAC/16000\r\n";
211
212static const char kSdpStringVideo[] =
213 "m=video 1 RTP/AVPF 120\r\n"
214 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700215 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700216 "a=rtpmap:120 VP8/90000\r\n";
217
218static const char kSdpStringMs1Audio0[] =
219 "a=ssrc:1 cname:stream1\r\n"
220 "a=ssrc:1 msid:stream1 audiotrack0\r\n";
221
222static const char kSdpStringMs1Video0[] =
223 "a=ssrc:2 cname:stream1\r\n"
224 "a=ssrc:2 msid:stream1 videotrack0\r\n";
225
226static const char kSdpStringMs1Audio1[] =
227 "a=ssrc:3 cname:stream1\r\n"
228 "a=ssrc:3 msid:stream1 audiotrack1\r\n";
229
230static const char kSdpStringMs1Video1[] =
231 "a=ssrc:4 cname:stream1\r\n"
232 "a=ssrc:4 msid:stream1 videotrack1\r\n";
233
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000234#define MAYBE_SKIP_TEST(feature) \
235 if (!(feature())) { \
236 LOG(LS_INFO) << "Feature disabled... skipping"; \
237 return; \
238 }
239
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000240using rtc::scoped_ptr;
241using rtc::scoped_refptr;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000242using webrtc::AudioSourceInterface;
deadbeefab9b2d12015-10-14 11:33:11 -0700243using webrtc::AudioTrack;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000244using webrtc::AudioTrackInterface;
245using webrtc::DataBuffer;
246using webrtc::DataChannelInterface;
247using webrtc::FakeConstraints;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000248using webrtc::IceCandidateInterface;
deadbeefc80741f2015-10-22 13:14:45 -0700249using webrtc::MediaConstraintsInterface;
deadbeefab9b2d12015-10-14 11:33:11 -0700250using webrtc::MediaStream;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000251using webrtc::MediaStreamInterface;
252using webrtc::MediaStreamTrackInterface;
253using webrtc::MockCreateSessionDescriptionObserver;
254using webrtc::MockDataChannelObserver;
255using webrtc::MockSetSessionDescriptionObserver;
256using webrtc::MockStatsObserver;
257using webrtc::PeerConnectionInterface;
258using webrtc::PeerConnectionObserver;
deadbeefab9b2d12015-10-14 11:33:11 -0700259using webrtc::RtpReceiverInterface;
260using webrtc::RtpSenderInterface;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000261using webrtc::SdpParseError;
262using webrtc::SessionDescriptionInterface;
deadbeefab9b2d12015-10-14 11:33:11 -0700263using webrtc::StreamCollection;
264using webrtc::StreamCollectionInterface;
perkja3ede6c2016-03-08 01:27:48 +0100265using webrtc::VideoTrackSourceInterface;
deadbeefab9b2d12015-10-14 11:33:11 -0700266using webrtc::VideoTrack;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000267using webrtc::VideoTrackInterface;
268
deadbeefab9b2d12015-10-14 11:33:11 -0700269typedef PeerConnectionInterface::RTCOfferAnswerOptions RTCOfferAnswerOptions;
270
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000271namespace {
272
273// Gets the first ssrc of given content type from the ContentInfo.
274bool GetFirstSsrc(const cricket::ContentInfo* content_info, int* ssrc) {
275 if (!content_info || !ssrc) {
276 return false;
277 }
278 const cricket::MediaContentDescription* media_desc =
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000279 static_cast<const cricket::MediaContentDescription*>(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000280 content_info->description);
281 if (!media_desc || media_desc->streams().empty()) {
282 return false;
283 }
284 *ssrc = media_desc->streams().begin()->first_ssrc();
285 return true;
286}
287
288void SetSsrcToZero(std::string* sdp) {
289 const char kSdpSsrcAtribute[] = "a=ssrc:";
290 const char kSdpSsrcAtributeZero[] = "a=ssrc:0";
291 size_t ssrc_pos = 0;
292 while ((ssrc_pos = sdp->find(kSdpSsrcAtribute, ssrc_pos)) !=
293 std::string::npos) {
294 size_t end_ssrc = sdp->find(" ", ssrc_pos);
295 sdp->replace(ssrc_pos, end_ssrc - ssrc_pos, kSdpSsrcAtributeZero);
296 ssrc_pos = end_ssrc;
297 }
298}
299
deadbeefab9b2d12015-10-14 11:33:11 -0700300// Check if |streams| contains the specified track.
301bool ContainsTrack(const std::vector<cricket::StreamParams>& streams,
302 const std::string& stream_label,
303 const std::string& track_id) {
304 for (const cricket::StreamParams& params : streams) {
305 if (params.sync_label == stream_label && params.id == track_id) {
306 return true;
307 }
308 }
309 return false;
310}
311
312// Check if |senders| contains the specified sender, by id.
313bool ContainsSender(
314 const std::vector<rtc::scoped_refptr<RtpSenderInterface>>& senders,
315 const std::string& id) {
316 for (const auto& sender : senders) {
317 if (sender->id() == id) {
318 return true;
319 }
320 }
321 return false;
322}
323
324// Create a collection of streams.
325// CreateStreamCollection(1) creates a collection that
326// correspond to kSdpStringWithStream1.
327// CreateStreamCollection(2) correspond to kSdpStringWithStream1And2.
328rtc::scoped_refptr<StreamCollection> CreateStreamCollection(
329 int number_of_streams) {
330 rtc::scoped_refptr<StreamCollection> local_collection(
331 StreamCollection::Create());
332
333 for (int i = 0; i < number_of_streams; ++i) {
334 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream(
335 webrtc::MediaStream::Create(kStreams[i]));
336
337 // Add a local audio track.
338 rtc::scoped_refptr<webrtc::AudioTrackInterface> audio_track(
339 webrtc::AudioTrack::Create(kAudioTracks[i], nullptr));
340 stream->AddTrack(audio_track);
341
342 // Add a local video track.
343 rtc::scoped_refptr<webrtc::VideoTrackInterface> video_track(
344 webrtc::VideoTrack::Create(kVideoTracks[i], nullptr));
345 stream->AddTrack(video_track);
346
347 local_collection->AddStream(stream);
348 }
349 return local_collection;
350}
351
352// Check equality of StreamCollections.
353bool CompareStreamCollections(StreamCollectionInterface* s1,
354 StreamCollectionInterface* s2) {
355 if (s1 == nullptr || s2 == nullptr || s1->count() != s2->count()) {
356 return false;
357 }
358
359 for (size_t i = 0; i != s1->count(); ++i) {
360 if (s1->at(i)->label() != s2->at(i)->label()) {
361 return false;
362 }
363 webrtc::AudioTrackVector audio_tracks1 = s1->at(i)->GetAudioTracks();
364 webrtc::AudioTrackVector audio_tracks2 = s2->at(i)->GetAudioTracks();
365 webrtc::VideoTrackVector video_tracks1 = s1->at(i)->GetVideoTracks();
366 webrtc::VideoTrackVector video_tracks2 = s2->at(i)->GetVideoTracks();
367
368 if (audio_tracks1.size() != audio_tracks2.size()) {
369 return false;
370 }
371 for (size_t j = 0; j != audio_tracks1.size(); ++j) {
372 if (audio_tracks1[j]->id() != audio_tracks2[j]->id()) {
373 return false;
374 }
375 }
376 if (video_tracks1.size() != video_tracks2.size()) {
377 return false;
378 }
379 for (size_t j = 0; j != video_tracks1.size(); ++j) {
380 if (video_tracks1[j]->id() != video_tracks2[j]->id()) {
381 return false;
382 }
383 }
384 }
385 return true;
386}
387
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000388class MockPeerConnectionObserver : public PeerConnectionObserver {
389 public:
deadbeefab9b2d12015-10-14 11:33:11 -0700390 MockPeerConnectionObserver() : remote_streams_(StreamCollection::Create()) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000391 ~MockPeerConnectionObserver() {
392 }
393 void SetPeerConnectionInterface(PeerConnectionInterface* pc) {
394 pc_ = pc;
395 if (pc) {
396 state_ = pc_->signaling_state();
397 }
398 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000399 virtual void OnSignalingChange(
400 PeerConnectionInterface::SignalingState new_state) {
401 EXPECT_EQ(pc_->signaling_state(), new_state);
402 state_ = new_state;
403 }
404 // TODO(bemasc): Remove this once callers transition to OnIceGatheringChange.
405 virtual void OnStateChange(StateType state_changed) {
406 if (pc_.get() == NULL)
407 return;
408 switch (state_changed) {
409 case kSignalingState:
410 // OnSignalingChange and OnStateChange(kSignalingState) should always
411 // be called approximately simultaneously. To ease testing, we require
412 // that they always be called in that order. This check verifies
413 // that OnSignalingChange has just been called.
414 EXPECT_EQ(pc_->signaling_state(), state_);
415 break;
416 case kIceState:
417 ADD_FAILURE();
418 break;
419 default:
420 ADD_FAILURE();
421 break;
422 }
423 }
deadbeefab9b2d12015-10-14 11:33:11 -0700424
425 MediaStreamInterface* RemoteStream(const std::string& label) {
426 return remote_streams_->find(label);
427 }
428 StreamCollectionInterface* remote_streams() const { return remote_streams_; }
perkjdfb769d2016-02-09 03:09:43 -0800429 void OnAddStream(MediaStreamInterface* stream) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000430 last_added_stream_ = stream;
deadbeefab9b2d12015-10-14 11:33:11 -0700431 remote_streams_->AddStream(stream);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000432 }
perkjdfb769d2016-02-09 03:09:43 -0800433 void OnRemoveStream(MediaStreamInterface* stream) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000434 last_removed_stream_ = stream;
deadbeefab9b2d12015-10-14 11:33:11 -0700435 remote_streams_->RemoveStream(stream);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000436 }
perkjdfb769d2016-02-09 03:09:43 -0800437 void OnRenegotiationNeeded() override { renegotiation_needed_ = true; }
438 void OnDataChannel(DataChannelInterface* data_channel) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000439 last_datachannel_ = data_channel;
440 }
441
perkjdfb769d2016-02-09 03:09:43 -0800442 void OnIceConnectionChange(
443 PeerConnectionInterface::IceConnectionState new_state) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000444 EXPECT_EQ(pc_->ice_connection_state(), new_state);
445 }
perkjdfb769d2016-02-09 03:09:43 -0800446 void OnIceGatheringChange(
447 PeerConnectionInterface::IceGatheringState new_state) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000448 EXPECT_EQ(pc_->ice_gathering_state(), new_state);
perkjdfb769d2016-02-09 03:09:43 -0800449 ice_complete_ = new_state == PeerConnectionInterface::kIceGatheringComplete;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000450 }
perkjdfb769d2016-02-09 03:09:43 -0800451 void OnIceCandidate(const webrtc::IceCandidateInterface* candidate) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000452 EXPECT_NE(PeerConnectionInterface::kIceGatheringNew,
453 pc_->ice_gathering_state());
454
455 std::string sdp;
456 EXPECT_TRUE(candidate->ToString(&sdp));
457 EXPECT_LT(0u, sdp.size());
458 last_candidate_.reset(webrtc::CreateIceCandidate(candidate->sdp_mid(),
459 candidate->sdp_mline_index(), sdp, NULL));
460 EXPECT_TRUE(last_candidate_.get() != NULL);
461 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000462
463 // Returns the label of the last added stream.
464 // Empty string if no stream have been added.
465 std::string GetLastAddedStreamLabel() {
466 if (last_added_stream_.get())
467 return last_added_stream_->label();
468 return "";
469 }
470 std::string GetLastRemovedStreamLabel() {
471 if (last_removed_stream_.get())
472 return last_removed_stream_->label();
473 return "";
474 }
475
476 scoped_refptr<PeerConnectionInterface> pc_;
477 PeerConnectionInterface::SignalingState state_;
478 scoped_ptr<IceCandidateInterface> last_candidate_;
479 scoped_refptr<DataChannelInterface> last_datachannel_;
deadbeefab9b2d12015-10-14 11:33:11 -0700480 rtc::scoped_refptr<StreamCollection> remote_streams_;
481 bool renegotiation_needed_ = false;
482 bool ice_complete_ = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000483
484 private:
485 scoped_refptr<MediaStreamInterface> last_added_stream_;
486 scoped_refptr<MediaStreamInterface> last_removed_stream_;
487};
488
489} // namespace
deadbeefab9b2d12015-10-14 11:33:11 -0700490
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000491class PeerConnectionInterfaceTest : public testing::Test {
492 protected:
phoglund37ebcf02016-01-08 05:04:57 -0800493 PeerConnectionInterfaceTest() {
494#ifdef WEBRTC_ANDROID
495 webrtc::InitializeAndroidObjects();
496#endif
497 }
498
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000499 virtual void SetUp() {
500 pc_factory_ = webrtc::CreatePeerConnectionFactory(
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000501 rtc::Thread::Current(), rtc::Thread::Current(), NULL, NULL,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000502 NULL);
503 ASSERT_TRUE(pc_factory_.get() != NULL);
504 }
505
506 void CreatePeerConnection() {
507 CreatePeerConnection("", "", NULL);
508 }
509
510 void CreatePeerConnection(webrtc::MediaConstraintsInterface* constraints) {
511 CreatePeerConnection("", "", constraints);
512 }
513
514 void CreatePeerConnection(const std::string& uri,
515 const std::string& password,
516 webrtc::MediaConstraintsInterface* constraints) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800517 PeerConnectionInterface::RTCConfiguration config;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000518 PeerConnectionInterface::IceServer server;
deadbeef0a6c4ca2015-10-06 11:38:28 -0700519 if (!uri.empty()) {
520 server.uri = uri;
521 server.password = password;
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800522 config.servers.push_back(server);
deadbeef0a6c4ca2015-10-06 11:38:28 -0700523 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000524
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800525 rtc::scoped_ptr<cricket::FakePortAllocator> port_allocator(
526 new cricket::FakePortAllocator(rtc::Thread::Current(), nullptr));
527 port_allocator_ = port_allocator.get();
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000528
buildbot@webrtc.org61c1b8e2014-04-09 06:06:38 +0000529 // DTLS does not work in a loopback call, so is disabled for most of the
530 // tests in this file. We only create a FakeIdentityService if the test
531 // explicitly sets the constraint.
jiayl@webrtc.org61e00b02015-03-04 22:17:38 +0000532 FakeConstraints default_constraints;
533 if (!constraints) {
534 constraints = &default_constraints;
535
536 default_constraints.AddMandatory(
537 webrtc::MediaConstraintsInterface::kEnableDtlsSrtp, false);
538 }
539
Henrik Boström5e56c592015-08-11 10:33:13 +0200540 scoped_ptr<webrtc::DtlsIdentityStoreInterface> dtls_identity_store;
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000541 bool dtls;
542 if (FindConstraint(constraints,
543 webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
544 &dtls,
Henrik Boström5e56c592015-08-11 10:33:13 +0200545 nullptr) && dtls) {
546 dtls_identity_store.reset(new FakeDtlsIdentityStore());
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000547 }
kwiberg0eb15ed2015-12-17 03:04:15 -0800548 pc_ = pc_factory_->CreatePeerConnection(
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800549 config, constraints, std::move(port_allocator),
kwiberg0eb15ed2015-12-17 03:04:15 -0800550 std::move(dtls_identity_store), &observer_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000551 ASSERT_TRUE(pc_.get() != NULL);
552 observer_.SetPeerConnectionInterface(pc_.get());
553 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
554 }
555
deadbeef0a6c4ca2015-10-06 11:38:28 -0700556 void CreatePeerConnectionExpectFail(const std::string& uri) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800557 PeerConnectionInterface::RTCConfiguration config;
deadbeef0a6c4ca2015-10-06 11:38:28 -0700558 PeerConnectionInterface::IceServer server;
deadbeef0a6c4ca2015-10-06 11:38:28 -0700559 server.uri = uri;
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800560 config.servers.push_back(server);
deadbeef0a6c4ca2015-10-06 11:38:28 -0700561
deadbeef0a6c4ca2015-10-06 11:38:28 -0700562 scoped_refptr<PeerConnectionInterface> pc;
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800563 pc = pc_factory_->CreatePeerConnection(config, nullptr, nullptr, nullptr,
564 &observer_);
565 EXPECT_EQ(nullptr, pc);
deadbeef0a6c4ca2015-10-06 11:38:28 -0700566 }
567
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000568 void CreatePeerConnectionWithDifferentConfigurations() {
569 CreatePeerConnection(kStunAddressOnly, "", NULL);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800570 EXPECT_EQ(1u, port_allocator_->stun_servers().size());
571 EXPECT_EQ(0u, port_allocator_->turn_servers().size());
572 EXPECT_EQ("address", port_allocator_->stun_servers().begin()->hostname());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000573 EXPECT_EQ(kDefaultStunPort,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800574 port_allocator_->stun_servers().begin()->port());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000575
deadbeef0a6c4ca2015-10-06 11:38:28 -0700576 CreatePeerConnectionExpectFail(kStunInvalidPort);
577 CreatePeerConnectionExpectFail(kStunAddressPortAndMore1);
578 CreatePeerConnectionExpectFail(kStunAddressPortAndMore2);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000579
580 CreatePeerConnection(kTurnIceServerUri, kTurnPassword, NULL);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800581 EXPECT_EQ(0u, port_allocator_->stun_servers().size());
582 EXPECT_EQ(1u, port_allocator_->turn_servers().size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000583 EXPECT_EQ(kTurnUsername,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800584 port_allocator_->turn_servers()[0].credentials.username);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000585 EXPECT_EQ(kTurnPassword,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800586 port_allocator_->turn_servers()[0].credentials.password);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000587 EXPECT_EQ(kTurnHostname,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800588 port_allocator_->turn_servers()[0].ports[0].address.hostname());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000589 }
590
591 void ReleasePeerConnection() {
592 pc_ = NULL;
593 observer_.SetPeerConnectionInterface(NULL);
594 }
595
deadbeefab9b2d12015-10-14 11:33:11 -0700596 void AddVideoStream(const std::string& label) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000597 // Create a local stream.
598 scoped_refptr<MediaStreamInterface> stream(
599 pc_factory_->CreateLocalMediaStream(label));
perkja3ede6c2016-03-08 01:27:48 +0100600 scoped_refptr<VideoTrackSourceInterface> video_source(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000601 pc_factory_->CreateVideoSource(new cricket::FakeVideoCapturer(), NULL));
602 scoped_refptr<VideoTrackInterface> video_track(
603 pc_factory_->CreateVideoTrack(label + "v0", video_source));
604 stream->AddTrack(video_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +0000605 EXPECT_TRUE(pc_->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000606 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
607 observer_.renegotiation_needed_ = false;
608 }
609
610 void AddVoiceStream(const std::string& label) {
611 // Create a local stream.
612 scoped_refptr<MediaStreamInterface> stream(
613 pc_factory_->CreateLocalMediaStream(label));
614 scoped_refptr<AudioTrackInterface> audio_track(
615 pc_factory_->CreateAudioTrack(label + "a0", NULL));
616 stream->AddTrack(audio_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +0000617 EXPECT_TRUE(pc_->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000618 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
619 observer_.renegotiation_needed_ = false;
620 }
621
622 void AddAudioVideoStream(const std::string& stream_label,
623 const std::string& audio_track_label,
624 const std::string& video_track_label) {
625 // Create a local stream.
626 scoped_refptr<MediaStreamInterface> stream(
627 pc_factory_->CreateLocalMediaStream(stream_label));
628 scoped_refptr<AudioTrackInterface> audio_track(
629 pc_factory_->CreateAudioTrack(
630 audio_track_label, static_cast<AudioSourceInterface*>(NULL)));
631 stream->AddTrack(audio_track.get());
632 scoped_refptr<VideoTrackInterface> video_track(
633 pc_factory_->CreateVideoTrack(video_track_label, NULL));
634 stream->AddTrack(video_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +0000635 EXPECT_TRUE(pc_->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000636 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
637 observer_.renegotiation_needed_ = false;
638 }
639
kwiberg2bbff992016-03-16 11:03:04 -0700640 bool DoCreateOfferAnswer(rtc::scoped_ptr<SessionDescriptionInterface>* desc,
deadbeefc80741f2015-10-22 13:14:45 -0700641 bool offer,
642 MediaConstraintsInterface* constraints) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000643 rtc::scoped_refptr<MockCreateSessionDescriptionObserver>
644 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000645 MockCreateSessionDescriptionObserver>());
646 if (offer) {
deadbeefc80741f2015-10-22 13:14:45 -0700647 pc_->CreateOffer(observer, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000648 } else {
deadbeefc80741f2015-10-22 13:14:45 -0700649 pc_->CreateAnswer(observer, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000650 }
651 EXPECT_EQ_WAIT(true, observer->called(), kTimeout);
kwiberg2bbff992016-03-16 11:03:04 -0700652 desc->reset(observer->release_desc());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000653 return observer->result();
654 }
655
kwiberg2bbff992016-03-16 11:03:04 -0700656 bool DoCreateOffer(rtc::scoped_ptr<SessionDescriptionInterface>* desc,
deadbeefc80741f2015-10-22 13:14:45 -0700657 MediaConstraintsInterface* constraints) {
658 return DoCreateOfferAnswer(desc, true, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000659 }
660
kwiberg2bbff992016-03-16 11:03:04 -0700661 bool DoCreateAnswer(rtc::scoped_ptr<SessionDescriptionInterface>* desc,
deadbeefc80741f2015-10-22 13:14:45 -0700662 MediaConstraintsInterface* constraints) {
663 return DoCreateOfferAnswer(desc, false, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000664 }
665
666 bool DoSetSessionDescription(SessionDescriptionInterface* desc, bool local) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000667 rtc::scoped_refptr<MockSetSessionDescriptionObserver>
668 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000669 MockSetSessionDescriptionObserver>());
670 if (local) {
671 pc_->SetLocalDescription(observer, desc);
672 } else {
673 pc_->SetRemoteDescription(observer, desc);
674 }
675 EXPECT_EQ_WAIT(true, observer->called(), kTimeout);
676 return observer->result();
677 }
678
679 bool DoSetLocalDescription(SessionDescriptionInterface* desc) {
680 return DoSetSessionDescription(desc, true);
681 }
682
683 bool DoSetRemoteDescription(SessionDescriptionInterface* desc) {
684 return DoSetSessionDescription(desc, false);
685 }
686
687 // Calls PeerConnection::GetStats and check the return value.
688 // It does not verify the values in the StatReports since a RTCP packet might
689 // be required.
690 bool DoGetStats(MediaStreamTrackInterface* track) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000691 rtc::scoped_refptr<MockStatsObserver> observer(
692 new rtc::RefCountedObject<MockStatsObserver>());
jiayl@webrtc.orgdb41b4d2014-03-03 21:30:06 +0000693 if (!pc_->GetStats(
694 observer, track, PeerConnectionInterface::kStatsOutputLevelStandard))
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000695 return false;
696 EXPECT_TRUE_WAIT(observer->called(), kTimeout);
697 return observer->called();
698 }
699
700 void InitiateCall() {
701 CreatePeerConnection();
702 // Create a local stream with audio&video tracks.
703 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
704 CreateOfferReceiveAnswer();
705 }
706
707 // Verify that RTP Header extensions has been negotiated for audio and video.
708 void VerifyRemoteRtpHeaderExtensions() {
709 const cricket::MediaContentDescription* desc =
710 cricket::GetFirstAudioContentDescription(
711 pc_->remote_description()->description());
712 ASSERT_TRUE(desc != NULL);
713 EXPECT_GT(desc->rtp_header_extensions().size(), 0u);
714
715 desc = cricket::GetFirstVideoContentDescription(
716 pc_->remote_description()->description());
717 ASSERT_TRUE(desc != NULL);
718 EXPECT_GT(desc->rtp_header_extensions().size(), 0u);
719 }
720
721 void CreateOfferAsRemoteDescription() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000722 rtc::scoped_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -0700723 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000724 std::string sdp;
725 EXPECT_TRUE(offer->ToString(&sdp));
726 SessionDescriptionInterface* remote_offer =
727 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
728 sdp, NULL);
729 EXPECT_TRUE(DoSetRemoteDescription(remote_offer));
730 EXPECT_EQ(PeerConnectionInterface::kHaveRemoteOffer, observer_.state_);
731 }
732
deadbeefab9b2d12015-10-14 11:33:11 -0700733 void CreateAndSetRemoteOffer(const std::string& sdp) {
734 SessionDescriptionInterface* remote_offer =
735 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
736 sdp, nullptr);
737 EXPECT_TRUE(DoSetRemoteDescription(remote_offer));
738 EXPECT_EQ(PeerConnectionInterface::kHaveRemoteOffer, observer_.state_);
739 }
740
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000741 void CreateAnswerAsLocalDescription() {
742 scoped_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -0700743 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000744
745 // TODO(perkj): Currently SetLocalDescription fails if any parameters in an
746 // audio codec change, even if the parameter has nothing to do with
747 // receiving. Not all parameters are serialized to SDP.
748 // Since CreatePrAnswerAsLocalDescription serialize/deserialize
749 // the SessionDescription, it is necessary to do that here to in order to
750 // get ReceiveOfferCreatePrAnswerAndAnswer and RenegotiateAudioOnly to pass.
751 // https://code.google.com/p/webrtc/issues/detail?id=1356
752 std::string sdp;
753 EXPECT_TRUE(answer->ToString(&sdp));
754 SessionDescriptionInterface* new_answer =
755 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
756 sdp, NULL);
757 EXPECT_TRUE(DoSetLocalDescription(new_answer));
758 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
759 }
760
761 void CreatePrAnswerAsLocalDescription() {
762 scoped_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -0700763 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000764
765 std::string sdp;
766 EXPECT_TRUE(answer->ToString(&sdp));
767 SessionDescriptionInterface* pr_answer =
768 webrtc::CreateSessionDescription(SessionDescriptionInterface::kPrAnswer,
769 sdp, NULL);
770 EXPECT_TRUE(DoSetLocalDescription(pr_answer));
771 EXPECT_EQ(PeerConnectionInterface::kHaveLocalPrAnswer, observer_.state_);
772 }
773
774 void CreateOfferReceiveAnswer() {
775 CreateOfferAsLocalDescription();
776 std::string sdp;
777 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
778 CreateAnswerAsRemoteDescription(sdp);
779 }
780
781 void CreateOfferAsLocalDescription() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000782 rtc::scoped_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -0700783 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000784 // TODO(perkj): Currently SetLocalDescription fails if any parameters in an
785 // audio codec change, even if the parameter has nothing to do with
786 // receiving. Not all parameters are serialized to SDP.
787 // Since CreatePrAnswerAsLocalDescription serialize/deserialize
788 // the SessionDescription, it is necessary to do that here to in order to
789 // get ReceiveOfferCreatePrAnswerAndAnswer and RenegotiateAudioOnly to pass.
790 // https://code.google.com/p/webrtc/issues/detail?id=1356
791 std::string sdp;
792 EXPECT_TRUE(offer->ToString(&sdp));
793 SessionDescriptionInterface* new_offer =
794 webrtc::CreateSessionDescription(
795 SessionDescriptionInterface::kOffer,
796 sdp, NULL);
797
798 EXPECT_TRUE(DoSetLocalDescription(new_offer));
799 EXPECT_EQ(PeerConnectionInterface::kHaveLocalOffer, observer_.state_);
mallinath@webrtc.org68cbd012014-01-22 00:16:46 +0000800 // Wait for the ice_complete message, so that SDP will have candidates.
801 EXPECT_TRUE_WAIT(observer_.ice_complete_, kTimeout);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000802 }
803
deadbeefab9b2d12015-10-14 11:33:11 -0700804 void CreateAnswerAsRemoteDescription(const std::string& sdp) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000805 webrtc::JsepSessionDescription* answer = new webrtc::JsepSessionDescription(
806 SessionDescriptionInterface::kAnswer);
deadbeefab9b2d12015-10-14 11:33:11 -0700807 EXPECT_TRUE(answer->Initialize(sdp, NULL));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000808 EXPECT_TRUE(DoSetRemoteDescription(answer));
809 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
810 }
811
deadbeefab9b2d12015-10-14 11:33:11 -0700812 void CreatePrAnswerAndAnswerAsRemoteDescription(const std::string& sdp) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000813 webrtc::JsepSessionDescription* pr_answer =
814 new webrtc::JsepSessionDescription(
815 SessionDescriptionInterface::kPrAnswer);
deadbeefab9b2d12015-10-14 11:33:11 -0700816 EXPECT_TRUE(pr_answer->Initialize(sdp, NULL));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000817 EXPECT_TRUE(DoSetRemoteDescription(pr_answer));
818 EXPECT_EQ(PeerConnectionInterface::kHaveRemotePrAnswer, observer_.state_);
819 webrtc::JsepSessionDescription* answer =
820 new webrtc::JsepSessionDescription(
821 SessionDescriptionInterface::kAnswer);
deadbeefab9b2d12015-10-14 11:33:11 -0700822 EXPECT_TRUE(answer->Initialize(sdp, NULL));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000823 EXPECT_TRUE(DoSetRemoteDescription(answer));
824 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
825 }
826
827 // Help function used for waiting until a the last signaled remote stream has
828 // the same label as |stream_label|. In a few of the tests in this file we
829 // answer with the same session description as we offer and thus we can
830 // check if OnAddStream have been called with the same stream as we offer to
831 // send.
832 void WaitAndVerifyOnAddStream(const std::string& stream_label) {
833 EXPECT_EQ_WAIT(stream_label, observer_.GetLastAddedStreamLabel(), kTimeout);
834 }
835
836 // Creates an offer and applies it as a local session description.
837 // Creates an answer with the same SDP an the offer but removes all lines
838 // that start with a:ssrc"
839 void CreateOfferReceiveAnswerWithoutSsrc() {
840 CreateOfferAsLocalDescription();
841 std::string sdp;
842 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
843 SetSsrcToZero(&sdp);
844 CreateAnswerAsRemoteDescription(sdp);
845 }
846
deadbeefab9b2d12015-10-14 11:33:11 -0700847 // This function creates a MediaStream with label kStreams[0] and
848 // |number_of_audio_tracks| and |number_of_video_tracks| tracks and the
849 // corresponding SessionDescriptionInterface. The SessionDescriptionInterface
kwiberg2bbff992016-03-16 11:03:04 -0700850 // is returned and the MediaStream is stored in
deadbeefab9b2d12015-10-14 11:33:11 -0700851 // |reference_collection_|
kwiberg2bbff992016-03-16 11:03:04 -0700852 rtc::scoped_ptr<SessionDescriptionInterface>
853 CreateSessionDescriptionAndReference(size_t number_of_audio_tracks,
854 size_t number_of_video_tracks) {
855 EXPECT_LE(number_of_audio_tracks, 2u);
856 EXPECT_LE(number_of_video_tracks, 2u);
deadbeefab9b2d12015-10-14 11:33:11 -0700857
858 reference_collection_ = StreamCollection::Create();
859 std::string sdp_ms1 = std::string(kSdpStringInit);
860
861 std::string mediastream_label = kStreams[0];
862
863 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream(
864 webrtc::MediaStream::Create(mediastream_label));
865 reference_collection_->AddStream(stream);
866
867 if (number_of_audio_tracks > 0) {
868 sdp_ms1 += std::string(kSdpStringAudio);
869 sdp_ms1 += std::string(kSdpStringMs1Audio0);
870 AddAudioTrack(kAudioTracks[0], stream);
871 }
872 if (number_of_audio_tracks > 1) {
873 sdp_ms1 += kSdpStringMs1Audio1;
874 AddAudioTrack(kAudioTracks[1], stream);
875 }
876
877 if (number_of_video_tracks > 0) {
878 sdp_ms1 += std::string(kSdpStringVideo);
879 sdp_ms1 += std::string(kSdpStringMs1Video0);
880 AddVideoTrack(kVideoTracks[0], stream);
881 }
882 if (number_of_video_tracks > 1) {
883 sdp_ms1 += kSdpStringMs1Video1;
884 AddVideoTrack(kVideoTracks[1], stream);
885 }
886
kwiberg2bbff992016-03-16 11:03:04 -0700887 return rtc::scoped_ptr<SessionDescriptionInterface>(
888 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
889 sdp_ms1, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -0700890 }
891
892 void AddAudioTrack(const std::string& track_id,
893 MediaStreamInterface* stream) {
894 rtc::scoped_refptr<webrtc::AudioTrackInterface> audio_track(
895 webrtc::AudioTrack::Create(track_id, nullptr));
896 ASSERT_TRUE(stream->AddTrack(audio_track));
897 }
898
899 void AddVideoTrack(const std::string& track_id,
900 MediaStreamInterface* stream) {
901 rtc::scoped_refptr<webrtc::VideoTrackInterface> video_track(
902 webrtc::VideoTrack::Create(track_id, nullptr));
903 ASSERT_TRUE(stream->AddTrack(video_track));
904 }
905
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800906 cricket::FakePortAllocator* port_allocator_ = nullptr;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000907 scoped_refptr<webrtc::PeerConnectionFactoryInterface> pc_factory_;
908 scoped_refptr<PeerConnectionInterface> pc_;
909 MockPeerConnectionObserver observer_;
deadbeefab9b2d12015-10-14 11:33:11 -0700910 rtc::scoped_refptr<StreamCollection> reference_collection_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000911};
912
913TEST_F(PeerConnectionInterfaceTest,
914 CreatePeerConnectionWithDifferentConfigurations) {
915 CreatePeerConnectionWithDifferentConfigurations();
916}
917
918TEST_F(PeerConnectionInterfaceTest, AddStreams) {
919 CreatePeerConnection();
deadbeefab9b2d12015-10-14 11:33:11 -0700920 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000921 AddVoiceStream(kStreamLabel2);
922 ASSERT_EQ(2u, pc_->local_streams()->count());
923
wu@webrtc.org9dba5252013-08-05 20:36:57 +0000924 // Test we can add multiple local streams to one peerconnection.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000925 scoped_refptr<MediaStreamInterface> stream(
926 pc_factory_->CreateLocalMediaStream(kStreamLabel3));
927 scoped_refptr<AudioTrackInterface> audio_track(
928 pc_factory_->CreateAudioTrack(
929 kStreamLabel3, static_cast<AudioSourceInterface*>(NULL)));
930 stream->AddTrack(audio_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +0000931 EXPECT_TRUE(pc_->AddStream(stream));
wu@webrtc.org9dba5252013-08-05 20:36:57 +0000932 EXPECT_EQ(3u, pc_->local_streams()->count());
933
934 // Remove the third stream.
935 pc_->RemoveStream(pc_->local_streams()->at(2));
936 EXPECT_EQ(2u, pc_->local_streams()->count());
937
938 // Remove the second stream.
939 pc_->RemoveStream(pc_->local_streams()->at(1));
940 EXPECT_EQ(1u, pc_->local_streams()->count());
941
942 // Remove the first stream.
943 pc_->RemoveStream(pc_->local_streams()->at(0));
944 EXPECT_EQ(0u, pc_->local_streams()->count());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000945}
946
deadbeefab9b2d12015-10-14 11:33:11 -0700947// Test that the created offer includes streams we added.
948TEST_F(PeerConnectionInterfaceTest, AddedStreamsPresentInOffer) {
949 CreatePeerConnection();
950 AddAudioVideoStream(kStreamLabel1, "audio_track", "video_track");
951 scoped_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -0700952 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -0700953
954 const cricket::ContentInfo* audio_content =
955 cricket::GetFirstAudioContent(offer->description());
956 const cricket::AudioContentDescription* audio_desc =
957 static_cast<const cricket::AudioContentDescription*>(
958 audio_content->description);
959 EXPECT_TRUE(
960 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
961
962 const cricket::ContentInfo* video_content =
963 cricket::GetFirstVideoContent(offer->description());
964 const cricket::VideoContentDescription* video_desc =
965 static_cast<const cricket::VideoContentDescription*>(
966 video_content->description);
967 EXPECT_TRUE(
968 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
969
970 // Add another stream and ensure the offer includes both the old and new
971 // streams.
972 AddAudioVideoStream(kStreamLabel2, "audio_track2", "video_track2");
kwiberg2bbff992016-03-16 11:03:04 -0700973 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -0700974
975 audio_content = cricket::GetFirstAudioContent(offer->description());
976 audio_desc = static_cast<const cricket::AudioContentDescription*>(
977 audio_content->description);
978 EXPECT_TRUE(
979 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
980 EXPECT_TRUE(
981 ContainsTrack(audio_desc->streams(), kStreamLabel2, "audio_track2"));
982
983 video_content = cricket::GetFirstVideoContent(offer->description());
984 video_desc = static_cast<const cricket::VideoContentDescription*>(
985 video_content->description);
986 EXPECT_TRUE(
987 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
988 EXPECT_TRUE(
989 ContainsTrack(video_desc->streams(), kStreamLabel2, "video_track2"));
990}
991
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000992TEST_F(PeerConnectionInterfaceTest, RemoveStream) {
993 CreatePeerConnection();
deadbeefab9b2d12015-10-14 11:33:11 -0700994 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000995 ASSERT_EQ(1u, pc_->local_streams()->count());
996 pc_->RemoveStream(pc_->local_streams()->at(0));
997 EXPECT_EQ(0u, pc_->local_streams()->count());
998}
999
deadbeefe1f9d832016-01-14 15:35:42 -08001000// Test for AddTrack and RemoveTrack methods.
1001// Tests that the created offer includes tracks we added,
1002// and that the RtpSenders are created correctly.
1003// Also tests that RemoveTrack removes the tracks from subsequent offers.
1004TEST_F(PeerConnectionInterfaceTest, AddTrackRemoveTrack) {
1005 CreatePeerConnection();
1006 // Create a dummy stream, so tracks share a stream label.
1007 scoped_refptr<MediaStreamInterface> stream(
1008 pc_factory_->CreateLocalMediaStream(kStreamLabel1));
1009 std::vector<MediaStreamInterface*> stream_list;
1010 stream_list.push_back(stream.get());
1011 scoped_refptr<AudioTrackInterface> audio_track(
1012 pc_factory_->CreateAudioTrack("audio_track", nullptr));
1013 scoped_refptr<VideoTrackInterface> video_track(
1014 pc_factory_->CreateVideoTrack("video_track", nullptr));
1015 auto audio_sender = pc_->AddTrack(audio_track, stream_list);
1016 auto video_sender = pc_->AddTrack(video_track, stream_list);
1017 EXPECT_EQ(kStreamLabel1, audio_sender->stream_id());
1018 EXPECT_EQ("audio_track", audio_sender->id());
1019 EXPECT_EQ(audio_track, audio_sender->track());
1020 EXPECT_EQ(kStreamLabel1, video_sender->stream_id());
1021 EXPECT_EQ("video_track", video_sender->id());
1022 EXPECT_EQ(video_track, video_sender->track());
1023
1024 // Now create an offer and check for the senders.
1025 scoped_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001026 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefe1f9d832016-01-14 15:35:42 -08001027
1028 const cricket::ContentInfo* audio_content =
1029 cricket::GetFirstAudioContent(offer->description());
1030 const cricket::AudioContentDescription* audio_desc =
1031 static_cast<const cricket::AudioContentDescription*>(
1032 audio_content->description);
1033 EXPECT_TRUE(
1034 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
1035
1036 const cricket::ContentInfo* video_content =
1037 cricket::GetFirstVideoContent(offer->description());
1038 const cricket::VideoContentDescription* video_desc =
1039 static_cast<const cricket::VideoContentDescription*>(
1040 video_content->description);
1041 EXPECT_TRUE(
1042 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1043
1044 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
1045
1046 // Now try removing the tracks.
1047 EXPECT_TRUE(pc_->RemoveTrack(audio_sender));
1048 EXPECT_TRUE(pc_->RemoveTrack(video_sender));
1049
1050 // Create a new offer and ensure it doesn't contain the removed senders.
kwiberg2bbff992016-03-16 11:03:04 -07001051 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefe1f9d832016-01-14 15:35:42 -08001052
1053 audio_content = cricket::GetFirstAudioContent(offer->description());
1054 audio_desc = static_cast<const cricket::AudioContentDescription*>(
1055 audio_content->description);
1056 EXPECT_FALSE(
1057 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
1058
1059 video_content = cricket::GetFirstVideoContent(offer->description());
1060 video_desc = static_cast<const cricket::VideoContentDescription*>(
1061 video_content->description);
1062 EXPECT_FALSE(
1063 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1064
1065 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
1066
1067 // Calling RemoveTrack on a sender no longer attached to a PeerConnection
1068 // should return false.
1069 EXPECT_FALSE(pc_->RemoveTrack(audio_sender));
1070 EXPECT_FALSE(pc_->RemoveTrack(video_sender));
1071}
1072
1073// Test creating senders without a stream specified,
1074// expecting a random stream ID to be generated.
1075TEST_F(PeerConnectionInterfaceTest, AddTrackWithoutStream) {
1076 CreatePeerConnection();
1077 // Create a dummy stream, so tracks share a stream label.
1078 scoped_refptr<AudioTrackInterface> audio_track(
1079 pc_factory_->CreateAudioTrack("audio_track", nullptr));
1080 scoped_refptr<VideoTrackInterface> video_track(
1081 pc_factory_->CreateVideoTrack("video_track", nullptr));
1082 auto audio_sender =
1083 pc_->AddTrack(audio_track, std::vector<MediaStreamInterface*>());
1084 auto video_sender =
1085 pc_->AddTrack(video_track, std::vector<MediaStreamInterface*>());
1086 EXPECT_EQ("audio_track", audio_sender->id());
1087 EXPECT_EQ(audio_track, audio_sender->track());
1088 EXPECT_EQ("video_track", video_sender->id());
1089 EXPECT_EQ(video_track, video_sender->track());
1090 // If the ID is truly a random GUID, it should be infinitely unlikely they
1091 // will be the same.
1092 EXPECT_NE(video_sender->stream_id(), audio_sender->stream_id());
1093}
1094
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001095TEST_F(PeerConnectionInterfaceTest, CreateOfferReceiveAnswer) {
1096 InitiateCall();
1097 WaitAndVerifyOnAddStream(kStreamLabel1);
1098 VerifyRemoteRtpHeaderExtensions();
1099}
1100
1101TEST_F(PeerConnectionInterfaceTest, CreateOfferReceivePrAnswerAndAnswer) {
1102 CreatePeerConnection();
deadbeefab9b2d12015-10-14 11:33:11 -07001103 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001104 CreateOfferAsLocalDescription();
1105 std::string offer;
1106 EXPECT_TRUE(pc_->local_description()->ToString(&offer));
1107 CreatePrAnswerAndAnswerAsRemoteDescription(offer);
1108 WaitAndVerifyOnAddStream(kStreamLabel1);
1109}
1110
1111TEST_F(PeerConnectionInterfaceTest, ReceiveOfferCreateAnswer) {
1112 CreatePeerConnection();
deadbeefab9b2d12015-10-14 11:33:11 -07001113 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001114
1115 CreateOfferAsRemoteDescription();
1116 CreateAnswerAsLocalDescription();
1117
1118 WaitAndVerifyOnAddStream(kStreamLabel1);
1119}
1120
1121TEST_F(PeerConnectionInterfaceTest, ReceiveOfferCreatePrAnswerAndAnswer) {
1122 CreatePeerConnection();
deadbeefab9b2d12015-10-14 11:33:11 -07001123 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001124
1125 CreateOfferAsRemoteDescription();
1126 CreatePrAnswerAsLocalDescription();
1127 CreateAnswerAsLocalDescription();
1128
1129 WaitAndVerifyOnAddStream(kStreamLabel1);
1130}
1131
1132TEST_F(PeerConnectionInterfaceTest, Renegotiate) {
1133 InitiateCall();
1134 ASSERT_EQ(1u, pc_->remote_streams()->count());
1135 pc_->RemoveStream(pc_->local_streams()->at(0));
1136 CreateOfferReceiveAnswer();
1137 EXPECT_EQ(0u, pc_->remote_streams()->count());
deadbeefab9b2d12015-10-14 11:33:11 -07001138 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001139 CreateOfferReceiveAnswer();
1140}
1141
1142// Tests that after negotiating an audio only call, the respondent can perform a
1143// renegotiation that removes the audio stream.
1144TEST_F(PeerConnectionInterfaceTest, RenegotiateAudioOnly) {
1145 CreatePeerConnection();
1146 AddVoiceStream(kStreamLabel1);
1147 CreateOfferAsRemoteDescription();
1148 CreateAnswerAsLocalDescription();
1149
1150 ASSERT_EQ(1u, pc_->remote_streams()->count());
1151 pc_->RemoveStream(pc_->local_streams()->at(0));
1152 CreateOfferReceiveAnswer();
1153 EXPECT_EQ(0u, pc_->remote_streams()->count());
1154}
1155
1156// Test that candidates are generated and that we can parse our own candidates.
1157TEST_F(PeerConnectionInterfaceTest, IceCandidates) {
1158 CreatePeerConnection();
1159
1160 EXPECT_FALSE(pc_->AddIceCandidate(observer_.last_candidate_.get()));
1161 // SetRemoteDescription takes ownership of offer.
kwiberg2bbff992016-03-16 11:03:04 -07001162 rtc::scoped_ptr<SessionDescriptionInterface> offer;
deadbeefab9b2d12015-10-14 11:33:11 -07001163 AddVideoStream(kStreamLabel1);
deadbeefc80741f2015-10-22 13:14:45 -07001164 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
kwiberg2bbff992016-03-16 11:03:04 -07001165 EXPECT_TRUE(DoSetRemoteDescription(offer.release()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001166
1167 // SetLocalDescription takes ownership of answer.
kwiberg2bbff992016-03-16 11:03:04 -07001168 rtc::scoped_ptr<SessionDescriptionInterface> answer;
deadbeefc80741f2015-10-22 13:14:45 -07001169 EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
kwiberg2bbff992016-03-16 11:03:04 -07001170 EXPECT_TRUE(DoSetLocalDescription(answer.release()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001171
1172 EXPECT_TRUE_WAIT(observer_.last_candidate_.get() != NULL, kTimeout);
1173 EXPECT_TRUE_WAIT(observer_.ice_complete_, kTimeout);
1174
1175 EXPECT_TRUE(pc_->AddIceCandidate(observer_.last_candidate_.get()));
1176}
1177
deadbeefab9b2d12015-10-14 11:33:11 -07001178// Test that CreateOffer and CreateAnswer will fail if the track labels are
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001179// not unique.
1180TEST_F(PeerConnectionInterfaceTest, CreateOfferAnswerWithInvalidStream) {
1181 CreatePeerConnection();
1182 // Create a regular offer for the CreateAnswer test later.
kwiberg2bbff992016-03-16 11:03:04 -07001183 rtc::scoped_ptr<SessionDescriptionInterface> offer;
deadbeefc80741f2015-10-22 13:14:45 -07001184 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
kwiberg2bbff992016-03-16 11:03:04 -07001185 EXPECT_TRUE(offer);
1186 offer.reset();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001187
1188 // Create a local stream with audio&video tracks having same label.
1189 AddAudioVideoStream(kStreamLabel1, "track_label", "track_label");
1190
1191 // Test CreateOffer
deadbeefc80741f2015-10-22 13:14:45 -07001192 EXPECT_FALSE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001193
1194 // Test CreateAnswer
kwiberg2bbff992016-03-16 11:03:04 -07001195 rtc::scoped_ptr<SessionDescriptionInterface> answer;
deadbeefc80741f2015-10-22 13:14:45 -07001196 EXPECT_FALSE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001197}
1198
1199// Test that we will get different SSRCs for each tracks in the offer and answer
1200// we created.
1201TEST_F(PeerConnectionInterfaceTest, SsrcInOfferAnswer) {
1202 CreatePeerConnection();
1203 // Create a local stream with audio&video tracks having different labels.
1204 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
1205
1206 // Test CreateOffer
1207 scoped_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001208 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001209 int audio_ssrc = 0;
1210 int video_ssrc = 0;
1211 EXPECT_TRUE(GetFirstSsrc(GetFirstAudioContent(offer->description()),
1212 &audio_ssrc));
1213 EXPECT_TRUE(GetFirstSsrc(GetFirstVideoContent(offer->description()),
1214 &video_ssrc));
1215 EXPECT_NE(audio_ssrc, video_ssrc);
1216
1217 // Test CreateAnswer
1218 EXPECT_TRUE(DoSetRemoteDescription(offer.release()));
1219 scoped_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -07001220 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001221 audio_ssrc = 0;
1222 video_ssrc = 0;
1223 EXPECT_TRUE(GetFirstSsrc(GetFirstAudioContent(answer->description()),
1224 &audio_ssrc));
1225 EXPECT_TRUE(GetFirstSsrc(GetFirstVideoContent(answer->description()),
1226 &video_ssrc));
1227 EXPECT_NE(audio_ssrc, video_ssrc);
1228}
1229
deadbeefeb459812015-12-15 19:24:43 -08001230// Test that it's possible to call AddTrack on a MediaStream after adding
1231// the stream to a PeerConnection.
1232// TODO(deadbeef): Remove this test once this behavior is no longer supported.
1233TEST_F(PeerConnectionInterfaceTest, AddTrackAfterAddStream) {
1234 CreatePeerConnection();
1235 // Create audio stream and add to PeerConnection.
1236 AddVoiceStream(kStreamLabel1);
1237 MediaStreamInterface* stream = pc_->local_streams()->at(0);
1238
1239 // Add video track to the audio-only stream.
1240 scoped_refptr<VideoTrackInterface> video_track(
1241 pc_factory_->CreateVideoTrack("video_label", nullptr));
1242 stream->AddTrack(video_track.get());
1243
1244 scoped_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001245 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefeb459812015-12-15 19:24:43 -08001246
1247 const cricket::MediaContentDescription* video_desc =
1248 cricket::GetFirstVideoContentDescription(offer->description());
1249 EXPECT_TRUE(video_desc != nullptr);
1250}
1251
1252// Test that it's possible to call RemoveTrack on a MediaStream after adding
1253// the stream to a PeerConnection.
1254// TODO(deadbeef): Remove this test once this behavior is no longer supported.
1255TEST_F(PeerConnectionInterfaceTest, RemoveTrackAfterAddStream) {
1256 CreatePeerConnection();
1257 // Create audio/video stream and add to PeerConnection.
1258 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
1259 MediaStreamInterface* stream = pc_->local_streams()->at(0);
1260
1261 // Remove the video track.
1262 stream->RemoveTrack(stream->GetVideoTracks()[0]);
1263
1264 scoped_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001265 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefeb459812015-12-15 19:24:43 -08001266
1267 const cricket::MediaContentDescription* video_desc =
1268 cricket::GetFirstVideoContentDescription(offer->description());
1269 EXPECT_TRUE(video_desc == nullptr);
1270}
1271
deadbeefbd7d8f72015-12-18 16:58:44 -08001272// Test creating a sender with a stream ID, and ensure the ID is populated
1273// in the offer.
1274TEST_F(PeerConnectionInterfaceTest, CreateSenderWithStream) {
1275 CreatePeerConnection();
1276 pc_->CreateSender("video", kStreamLabel1);
1277
1278 scoped_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001279 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefbd7d8f72015-12-18 16:58:44 -08001280
1281 const cricket::MediaContentDescription* video_desc =
1282 cricket::GetFirstVideoContentDescription(offer->description());
1283 ASSERT_TRUE(video_desc != nullptr);
1284 ASSERT_EQ(1u, video_desc->streams().size());
1285 EXPECT_EQ(kStreamLabel1, video_desc->streams()[0].sync_label);
1286}
1287
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001288// Test that we can specify a certain track that we want statistics about.
1289TEST_F(PeerConnectionInterfaceTest, GetStatsForSpecificTrack) {
1290 InitiateCall();
1291 ASSERT_LT(0u, pc_->remote_streams()->count());
1292 ASSERT_LT(0u, pc_->remote_streams()->at(0)->GetAudioTracks().size());
1293 scoped_refptr<MediaStreamTrackInterface> remote_audio =
1294 pc_->remote_streams()->at(0)->GetAudioTracks()[0];
1295 EXPECT_TRUE(DoGetStats(remote_audio));
1296
1297 // Remove the stream. Since we are sending to our selves the local
1298 // and the remote stream is the same.
1299 pc_->RemoveStream(pc_->local_streams()->at(0));
1300 // Do a re-negotiation.
1301 CreateOfferReceiveAnswer();
1302
1303 ASSERT_EQ(0u, pc_->remote_streams()->count());
1304
1305 // Test that we still can get statistics for the old track. Even if it is not
1306 // sent any longer.
1307 EXPECT_TRUE(DoGetStats(remote_audio));
1308}
1309
1310// Test that we can get stats on a video track.
1311TEST_F(PeerConnectionInterfaceTest, GetStatsForVideoTrack) {
1312 InitiateCall();
1313 ASSERT_LT(0u, pc_->remote_streams()->count());
1314 ASSERT_LT(0u, pc_->remote_streams()->at(0)->GetVideoTracks().size());
1315 scoped_refptr<MediaStreamTrackInterface> remote_video =
1316 pc_->remote_streams()->at(0)->GetVideoTracks()[0];
1317 EXPECT_TRUE(DoGetStats(remote_video));
1318}
1319
1320// Test that we don't get statistics for an invalid track.
tommi@webrtc.org908f57e2014-07-21 11:44:39 +00001321// TODO(tommi): Fix this test. DoGetStats will return true
1322// for the unknown track (since GetStats is async), but no
1323// data is returned for the track.
1324TEST_F(PeerConnectionInterfaceTest, DISABLED_GetStatsForInvalidTrack) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001325 InitiateCall();
1326 scoped_refptr<AudioTrackInterface> unknown_audio_track(
1327 pc_factory_->CreateAudioTrack("unknown track", NULL));
1328 EXPECT_FALSE(DoGetStats(unknown_audio_track));
1329}
1330
1331// This test setup two RTP data channels in loop back.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001332TEST_F(PeerConnectionInterfaceTest, TestDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001333 FakeConstraints constraints;
1334 constraints.SetAllowRtpDataChannels();
1335 CreatePeerConnection(&constraints);
1336 scoped_refptr<DataChannelInterface> data1 =
1337 pc_->CreateDataChannel("test1", NULL);
1338 scoped_refptr<DataChannelInterface> data2 =
1339 pc_->CreateDataChannel("test2", NULL);
1340 ASSERT_TRUE(data1 != NULL);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001341 rtc::scoped_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001342 new MockDataChannelObserver(data1));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001343 rtc::scoped_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001344 new MockDataChannelObserver(data2));
1345
1346 EXPECT_EQ(DataChannelInterface::kConnecting, data1->state());
1347 EXPECT_EQ(DataChannelInterface::kConnecting, data2->state());
1348 std::string data_to_send1 = "testing testing";
1349 std::string data_to_send2 = "testing something else";
1350 EXPECT_FALSE(data1->Send(DataBuffer(data_to_send1)));
1351
1352 CreateOfferReceiveAnswer();
1353 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1354 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
1355
1356 EXPECT_EQ(DataChannelInterface::kOpen, data1->state());
1357 EXPECT_EQ(DataChannelInterface::kOpen, data2->state());
1358 EXPECT_TRUE(data1->Send(DataBuffer(data_to_send1)));
1359 EXPECT_TRUE(data2->Send(DataBuffer(data_to_send2)));
1360
1361 EXPECT_EQ_WAIT(data_to_send1, observer1->last_message(), kTimeout);
1362 EXPECT_EQ_WAIT(data_to_send2, observer2->last_message(), kTimeout);
1363
1364 data1->Close();
1365 EXPECT_EQ(DataChannelInterface::kClosing, data1->state());
1366 CreateOfferReceiveAnswer();
1367 EXPECT_FALSE(observer1->IsOpen());
1368 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
1369 EXPECT_TRUE(observer2->IsOpen());
1370
1371 data_to_send2 = "testing something else again";
1372 EXPECT_TRUE(data2->Send(DataBuffer(data_to_send2)));
1373
1374 EXPECT_EQ_WAIT(data_to_send2, observer2->last_message(), kTimeout);
1375}
1376
1377// This test verifies that sendnig binary data over RTP data channels should
1378// fail.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001379TEST_F(PeerConnectionInterfaceTest, TestSendBinaryOnRtpDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001380 FakeConstraints constraints;
1381 constraints.SetAllowRtpDataChannels();
1382 CreatePeerConnection(&constraints);
1383 scoped_refptr<DataChannelInterface> data1 =
1384 pc_->CreateDataChannel("test1", NULL);
1385 scoped_refptr<DataChannelInterface> data2 =
1386 pc_->CreateDataChannel("test2", NULL);
1387 ASSERT_TRUE(data1 != NULL);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001388 rtc::scoped_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001389 new MockDataChannelObserver(data1));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001390 rtc::scoped_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001391 new MockDataChannelObserver(data2));
1392
1393 EXPECT_EQ(DataChannelInterface::kConnecting, data1->state());
1394 EXPECT_EQ(DataChannelInterface::kConnecting, data2->state());
1395
1396 CreateOfferReceiveAnswer();
1397 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1398 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
1399
1400 EXPECT_EQ(DataChannelInterface::kOpen, data1->state());
1401 EXPECT_EQ(DataChannelInterface::kOpen, data2->state());
1402
kjellander194e3bc2016-03-19 12:12:52 -07001403 rtc::Buffer buffer("test", 4);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001404 EXPECT_FALSE(data1->Send(DataBuffer(buffer, true)));
1405}
1406
1407// This test setup a RTP data channels in loop back and test that a channel is
1408// opened even if the remote end answer with a zero SSRC.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001409TEST_F(PeerConnectionInterfaceTest, TestSendOnlyDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001410 FakeConstraints constraints;
1411 constraints.SetAllowRtpDataChannels();
1412 CreatePeerConnection(&constraints);
1413 scoped_refptr<DataChannelInterface> data1 =
1414 pc_->CreateDataChannel("test1", NULL);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001415 rtc::scoped_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001416 new MockDataChannelObserver(data1));
1417
1418 CreateOfferReceiveAnswerWithoutSsrc();
1419
1420 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1421
1422 data1->Close();
1423 EXPECT_EQ(DataChannelInterface::kClosing, data1->state());
1424 CreateOfferReceiveAnswerWithoutSsrc();
1425 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
1426 EXPECT_FALSE(observer1->IsOpen());
1427}
1428
1429// This test that if a data channel is added in an answer a receive only channel
1430// channel is created.
1431TEST_F(PeerConnectionInterfaceTest, TestReceiveOnlyDataChannel) {
1432 FakeConstraints constraints;
1433 constraints.SetAllowRtpDataChannels();
1434 CreatePeerConnection(&constraints);
1435
1436 std::string offer_label = "offer_channel";
1437 scoped_refptr<DataChannelInterface> offer_channel =
1438 pc_->CreateDataChannel(offer_label, NULL);
1439
1440 CreateOfferAsLocalDescription();
1441
1442 // Replace the data channel label in the offer and apply it as an answer.
1443 std::string receive_label = "answer_channel";
1444 std::string sdp;
1445 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001446 rtc::replace_substrs(offer_label.c_str(), offer_label.length(),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001447 receive_label.c_str(), receive_label.length(),
1448 &sdp);
1449 CreateAnswerAsRemoteDescription(sdp);
1450
1451 // Verify that a new incoming data channel has been created and that
1452 // it is open but can't we written to.
1453 ASSERT_TRUE(observer_.last_datachannel_ != NULL);
1454 DataChannelInterface* received_channel = observer_.last_datachannel_;
1455 EXPECT_EQ(DataChannelInterface::kConnecting, received_channel->state());
1456 EXPECT_EQ(receive_label, received_channel->label());
1457 EXPECT_FALSE(received_channel->Send(DataBuffer("something")));
1458
1459 // Verify that the channel we initially offered has been rejected.
1460 EXPECT_EQ(DataChannelInterface::kClosed, offer_channel->state());
1461
1462 // Do another offer / answer exchange and verify that the data channel is
1463 // opened.
1464 CreateOfferReceiveAnswer();
1465 EXPECT_EQ_WAIT(DataChannelInterface::kOpen, received_channel->state(),
1466 kTimeout);
1467}
1468
1469// This test that no data channel is returned if a reliable channel is
1470// requested.
1471// TODO(perkj): Remove this test once reliable channels are implemented.
1472TEST_F(PeerConnectionInterfaceTest, CreateReliableRtpDataChannelShouldFail) {
1473 FakeConstraints constraints;
1474 constraints.SetAllowRtpDataChannels();
1475 CreatePeerConnection(&constraints);
1476
1477 std::string label = "test";
1478 webrtc::DataChannelInit config;
1479 config.reliable = true;
1480 scoped_refptr<DataChannelInterface> channel =
1481 pc_->CreateDataChannel(label, &config);
1482 EXPECT_TRUE(channel == NULL);
1483}
1484
deadbeefab9b2d12015-10-14 11:33:11 -07001485// Verifies that duplicated label is not allowed for RTP data channel.
1486TEST_F(PeerConnectionInterfaceTest, RtpDuplicatedLabelNotAllowed) {
1487 FakeConstraints constraints;
1488 constraints.SetAllowRtpDataChannels();
1489 CreatePeerConnection(&constraints);
1490
1491 std::string label = "test";
1492 scoped_refptr<DataChannelInterface> channel =
1493 pc_->CreateDataChannel(label, nullptr);
1494 EXPECT_NE(channel, nullptr);
1495
1496 scoped_refptr<DataChannelInterface> dup_channel =
1497 pc_->CreateDataChannel(label, nullptr);
1498 EXPECT_EQ(dup_channel, nullptr);
1499}
1500
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001501// This tests that a SCTP data channel is returned using different
1502// DataChannelInit configurations.
1503TEST_F(PeerConnectionInterfaceTest, CreateSctpDataChannel) {
1504 FakeConstraints constraints;
1505 constraints.SetAllowDtlsSctpDataChannels();
1506 CreatePeerConnection(&constraints);
1507
1508 webrtc::DataChannelInit config;
1509
1510 scoped_refptr<DataChannelInterface> channel =
1511 pc_->CreateDataChannel("1", &config);
1512 EXPECT_TRUE(channel != NULL);
1513 EXPECT_TRUE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001514 EXPECT_TRUE(observer_.renegotiation_needed_);
1515 observer_.renegotiation_needed_ = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001516
1517 config.ordered = false;
1518 channel = pc_->CreateDataChannel("2", &config);
1519 EXPECT_TRUE(channel != NULL);
1520 EXPECT_TRUE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001521 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001522
1523 config.ordered = true;
1524 config.maxRetransmits = 0;
1525 channel = pc_->CreateDataChannel("3", &config);
1526 EXPECT_TRUE(channel != NULL);
1527 EXPECT_FALSE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001528 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001529
1530 config.maxRetransmits = -1;
1531 config.maxRetransmitTime = 0;
1532 channel = pc_->CreateDataChannel("4", &config);
1533 EXPECT_TRUE(channel != NULL);
1534 EXPECT_FALSE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001535 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001536}
1537
1538// This tests that no data channel is returned if both maxRetransmits and
1539// maxRetransmitTime are set for SCTP data channels.
1540TEST_F(PeerConnectionInterfaceTest,
1541 CreateSctpDataChannelShouldFailForInvalidConfig) {
1542 FakeConstraints constraints;
1543 constraints.SetAllowDtlsSctpDataChannels();
1544 CreatePeerConnection(&constraints);
1545
1546 std::string label = "test";
1547 webrtc::DataChannelInit config;
1548 config.maxRetransmits = 0;
1549 config.maxRetransmitTime = 0;
1550
1551 scoped_refptr<DataChannelInterface> channel =
1552 pc_->CreateDataChannel(label, &config);
1553 EXPECT_TRUE(channel == NULL);
1554}
1555
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001556// The test verifies that creating a SCTP data channel with an id already in use
1557// or out of range should fail.
1558TEST_F(PeerConnectionInterfaceTest,
1559 CreateSctpDataChannelWithInvalidIdShouldFail) {
1560 FakeConstraints constraints;
1561 constraints.SetAllowDtlsSctpDataChannels();
1562 CreatePeerConnection(&constraints);
1563
1564 webrtc::DataChannelInit config;
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001565 scoped_refptr<DataChannelInterface> channel;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001566
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001567 config.id = 1;
1568 channel = pc_->CreateDataChannel("1", &config);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001569 EXPECT_TRUE(channel != NULL);
1570 EXPECT_EQ(1, channel->id());
1571
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001572 channel = pc_->CreateDataChannel("x", &config);
1573 EXPECT_TRUE(channel == NULL);
1574
1575 config.id = cricket::kMaxSctpSid;
1576 channel = pc_->CreateDataChannel("max", &config);
1577 EXPECT_TRUE(channel != NULL);
1578 EXPECT_EQ(config.id, channel->id());
1579
1580 config.id = cricket::kMaxSctpSid + 1;
1581 channel = pc_->CreateDataChannel("x", &config);
1582 EXPECT_TRUE(channel == NULL);
1583}
1584
deadbeefab9b2d12015-10-14 11:33:11 -07001585// Verifies that duplicated label is allowed for SCTP data channel.
1586TEST_F(PeerConnectionInterfaceTest, SctpDuplicatedLabelAllowed) {
1587 FakeConstraints constraints;
1588 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
1589 true);
1590 CreatePeerConnection(&constraints);
1591
1592 std::string label = "test";
1593 scoped_refptr<DataChannelInterface> channel =
1594 pc_->CreateDataChannel(label, nullptr);
1595 EXPECT_NE(channel, nullptr);
1596
1597 scoped_refptr<DataChannelInterface> dup_channel =
1598 pc_->CreateDataChannel(label, nullptr);
1599 EXPECT_NE(dup_channel, nullptr);
1600}
1601
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001602// This test verifies that OnRenegotiationNeeded is fired for every new RTP
1603// DataChannel.
1604TEST_F(PeerConnectionInterfaceTest, RenegotiationNeededForNewRtpDataChannel) {
1605 FakeConstraints constraints;
1606 constraints.SetAllowRtpDataChannels();
1607 CreatePeerConnection(&constraints);
1608
1609 scoped_refptr<DataChannelInterface> dc1 =
1610 pc_->CreateDataChannel("test1", NULL);
1611 EXPECT_TRUE(observer_.renegotiation_needed_);
1612 observer_.renegotiation_needed_ = false;
1613
1614 scoped_refptr<DataChannelInterface> dc2 =
1615 pc_->CreateDataChannel("test2", NULL);
1616 EXPECT_TRUE(observer_.renegotiation_needed_);
1617}
1618
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001619// This test that a data channel closes when a PeerConnection is deleted/closed.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001620TEST_F(PeerConnectionInterfaceTest, DataChannelCloseWhenPeerConnectionClose) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001621 FakeConstraints constraints;
1622 constraints.SetAllowRtpDataChannels();
1623 CreatePeerConnection(&constraints);
1624
1625 scoped_refptr<DataChannelInterface> data1 =
1626 pc_->CreateDataChannel("test1", NULL);
1627 scoped_refptr<DataChannelInterface> data2 =
1628 pc_->CreateDataChannel("test2", NULL);
1629 ASSERT_TRUE(data1 != NULL);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001630 rtc::scoped_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001631 new MockDataChannelObserver(data1));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001632 rtc::scoped_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001633 new MockDataChannelObserver(data2));
1634
1635 CreateOfferReceiveAnswer();
1636 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1637 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
1638
1639 ReleasePeerConnection();
1640 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
1641 EXPECT_EQ(DataChannelInterface::kClosed, data2->state());
1642}
1643
1644// This test that data channels can be rejected in an answer.
1645TEST_F(PeerConnectionInterfaceTest, TestRejectDataChannelInAnswer) {
1646 FakeConstraints constraints;
1647 constraints.SetAllowRtpDataChannels();
1648 CreatePeerConnection(&constraints);
1649
1650 scoped_refptr<DataChannelInterface> offer_channel(
1651 pc_->CreateDataChannel("offer_channel", NULL));
1652
1653 CreateOfferAsLocalDescription();
1654
1655 // Create an answer where the m-line for data channels are rejected.
1656 std::string sdp;
1657 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
1658 webrtc::JsepSessionDescription* answer = new webrtc::JsepSessionDescription(
1659 SessionDescriptionInterface::kAnswer);
1660 EXPECT_TRUE(answer->Initialize(sdp, NULL));
1661 cricket::ContentInfo* data_info =
1662 answer->description()->GetContentByName("data");
1663 data_info->rejected = true;
1664
1665 DoSetRemoteDescription(answer);
1666 EXPECT_EQ(DataChannelInterface::kClosed, offer_channel->state());
1667}
1668
Stefan Holmer55d6e7c2016-03-17 16:26:40 +01001669// Disabled on Win dbg: https://bugs.chromium.org/p/webrtc/issues/detail?id=5659
Stefan Holmer102362b2016-03-18 09:39:07 +01001670#if defined(WEBRTC_WIN) && defined(_DEBUG)
Stefan Holmer55d6e7c2016-03-17 16:26:40 +01001671#define MAYBE_ReceiveFireFoxOffer DISABLED_ReceiveFireFoxOffer
1672#else
1673#define MAYBE_ReceiveFireFoxOffer ReceiveFireFoxOffer
1674#endif
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001675// Test that we can create a session description from an SDP string from
1676// FireFox, use it as a remote session description, generate an answer and use
1677// the answer as a local description.
Stefan Holmer55d6e7c2016-03-17 16:26:40 +01001678TEST_F(PeerConnectionInterfaceTest, MAYBE_ReceiveFireFoxOffer) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001679 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001680 FakeConstraints constraints;
1681 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
1682 true);
1683 CreatePeerConnection(&constraints);
1684 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
1685 SessionDescriptionInterface* desc =
1686 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
jbauchfabe2c92015-07-16 13:43:14 -07001687 webrtc::kFireFoxSdpOffer, nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001688 EXPECT_TRUE(DoSetSessionDescription(desc, false));
1689 CreateAnswerAsLocalDescription();
1690 ASSERT_TRUE(pc_->local_description() != NULL);
1691 ASSERT_TRUE(pc_->remote_description() != NULL);
1692
1693 const cricket::ContentInfo* content =
1694 cricket::GetFirstAudioContent(pc_->local_description()->description());
1695 ASSERT_TRUE(content != NULL);
1696 EXPECT_FALSE(content->rejected);
1697
1698 content =
1699 cricket::GetFirstVideoContent(pc_->local_description()->description());
1700 ASSERT_TRUE(content != NULL);
1701 EXPECT_FALSE(content->rejected);
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00001702#ifdef HAVE_SCTP
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001703 content =
1704 cricket::GetFirstDataContent(pc_->local_description()->description());
1705 ASSERT_TRUE(content != NULL);
1706 EXPECT_TRUE(content->rejected);
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00001707#endif
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001708}
1709
1710// Test that we can create an audio only offer and receive an answer with a
1711// limited set of audio codecs and receive an updated offer with more audio
1712// codecs, where the added codecs are not supported.
1713TEST_F(PeerConnectionInterfaceTest, ReceiveUpdatedAudioOfferWithBadCodecs) {
1714 CreatePeerConnection();
1715 AddVoiceStream("audio_label");
1716 CreateOfferAsLocalDescription();
1717
1718 SessionDescriptionInterface* answer =
1719 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
jbauchfabe2c92015-07-16 13:43:14 -07001720 webrtc::kAudioSdp, nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001721 EXPECT_TRUE(DoSetSessionDescription(answer, false));
1722
1723 SessionDescriptionInterface* updated_offer =
1724 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
jbauchfabe2c92015-07-16 13:43:14 -07001725 webrtc::kAudioSdpWithUnsupportedCodecs,
1726 nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001727 EXPECT_TRUE(DoSetSessionDescription(updated_offer, false));
1728 CreateAnswerAsLocalDescription();
1729}
1730
deadbeefc80741f2015-10-22 13:14:45 -07001731// Test that if we're receiving (but not sending) a track, subsequent offers
1732// will have m-lines with a=recvonly.
1733TEST_F(PeerConnectionInterfaceTest, CreateSubsequentRecvOnlyOffer) {
1734 FakeConstraints constraints;
1735 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
1736 true);
1737 CreatePeerConnection(&constraints);
1738 CreateAndSetRemoteOffer(kSdpStringWithStream1);
1739 CreateAnswerAsLocalDescription();
1740
1741 // At this point we should be receiving stream 1, but not sending anything.
1742 // A new offer should be recvonly.
kwiberg2bbff992016-03-16 11:03:04 -07001743 rtc::scoped_ptr<SessionDescriptionInterface> offer;
deadbeefc80741f2015-10-22 13:14:45 -07001744 DoCreateOffer(&offer, nullptr);
1745
1746 const cricket::ContentInfo* video_content =
1747 cricket::GetFirstVideoContent(offer->description());
1748 const cricket::VideoContentDescription* video_desc =
1749 static_cast<const cricket::VideoContentDescription*>(
1750 video_content->description);
1751 ASSERT_EQ(cricket::MD_RECVONLY, video_desc->direction());
1752
1753 const cricket::ContentInfo* audio_content =
1754 cricket::GetFirstAudioContent(offer->description());
1755 const cricket::AudioContentDescription* audio_desc =
1756 static_cast<const cricket::AudioContentDescription*>(
1757 audio_content->description);
1758 ASSERT_EQ(cricket::MD_RECVONLY, audio_desc->direction());
1759}
1760
1761// Test that if we're receiving (but not sending) a track, and the
1762// offerToReceiveVideo/offerToReceiveAudio constraints are explicitly set to
1763// false, the generated m-lines will be a=inactive.
1764TEST_F(PeerConnectionInterfaceTest, CreateSubsequentInactiveOffer) {
1765 FakeConstraints constraints;
1766 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
1767 true);
1768 CreatePeerConnection(&constraints);
1769 CreateAndSetRemoteOffer(kSdpStringWithStream1);
1770 CreateAnswerAsLocalDescription();
1771
1772 // At this point we should be receiving stream 1, but not sending anything.
1773 // A new offer would be recvonly, but we'll set the "no receive" constraints
1774 // to make it inactive.
kwiberg2bbff992016-03-16 11:03:04 -07001775 rtc::scoped_ptr<SessionDescriptionInterface> offer;
deadbeefc80741f2015-10-22 13:14:45 -07001776 FakeConstraints offer_constraints;
1777 offer_constraints.AddMandatory(
1778 webrtc::MediaConstraintsInterface::kOfferToReceiveVideo, false);
1779 offer_constraints.AddMandatory(
1780 webrtc::MediaConstraintsInterface::kOfferToReceiveAudio, false);
1781 DoCreateOffer(&offer, &offer_constraints);
1782
1783 const cricket::ContentInfo* video_content =
1784 cricket::GetFirstVideoContent(offer->description());
1785 const cricket::VideoContentDescription* video_desc =
1786 static_cast<const cricket::VideoContentDescription*>(
1787 video_content->description);
1788 ASSERT_EQ(cricket::MD_INACTIVE, video_desc->direction());
1789
1790 const cricket::ContentInfo* audio_content =
1791 cricket::GetFirstAudioContent(offer->description());
1792 const cricket::AudioContentDescription* audio_desc =
1793 static_cast<const cricket::AudioContentDescription*>(
1794 audio_content->description);
1795 ASSERT_EQ(cricket::MD_INACTIVE, audio_desc->direction());
1796}
1797
deadbeef653b8e02015-11-11 12:55:10 -08001798// Test that we can use SetConfiguration to change the ICE servers of the
1799// PortAllocator.
1800TEST_F(PeerConnectionInterfaceTest, SetConfigurationChangesIceServers) {
1801 CreatePeerConnection();
1802
1803 PeerConnectionInterface::RTCConfiguration config;
1804 PeerConnectionInterface::IceServer server;
1805 server.uri = "stun:test_hostname";
1806 config.servers.push_back(server);
1807 EXPECT_TRUE(pc_->SetConfiguration(config));
1808
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08001809 EXPECT_EQ(1u, port_allocator_->stun_servers().size());
1810 EXPECT_EQ("test_hostname",
1811 port_allocator_->stun_servers().begin()->hostname());
deadbeef653b8e02015-11-11 12:55:10 -08001812}
1813
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001814// Test that PeerConnection::Close changes the states to closed and all remote
1815// tracks change state to ended.
1816TEST_F(PeerConnectionInterfaceTest, CloseAndTestStreamsAndStates) {
1817 // Initialize a PeerConnection and negotiate local and remote session
1818 // description.
1819 InitiateCall();
1820 ASSERT_EQ(1u, pc_->local_streams()->count());
1821 ASSERT_EQ(1u, pc_->remote_streams()->count());
1822
1823 pc_->Close();
1824
1825 EXPECT_EQ(PeerConnectionInterface::kClosed, pc_->signaling_state());
1826 EXPECT_EQ(PeerConnectionInterface::kIceConnectionClosed,
1827 pc_->ice_connection_state());
1828 EXPECT_EQ(PeerConnectionInterface::kIceGatheringComplete,
1829 pc_->ice_gathering_state());
1830
1831 EXPECT_EQ(1u, pc_->local_streams()->count());
1832 EXPECT_EQ(1u, pc_->remote_streams()->count());
1833
1834 scoped_refptr<MediaStreamInterface> remote_stream =
1835 pc_->remote_streams()->at(0);
1836 EXPECT_EQ(MediaStreamTrackInterface::kEnded,
1837 remote_stream->GetVideoTracks()[0]->state());
1838 EXPECT_EQ(MediaStreamTrackInterface::kEnded,
1839 remote_stream->GetAudioTracks()[0]->state());
1840}
1841
1842// Test that PeerConnection methods fails gracefully after
1843// PeerConnection::Close has been called.
1844TEST_F(PeerConnectionInterfaceTest, CloseAndTestMethods) {
1845 CreatePeerConnection();
1846 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
1847 CreateOfferAsRemoteDescription();
1848 CreateAnswerAsLocalDescription();
1849
1850 ASSERT_EQ(1u, pc_->local_streams()->count());
1851 scoped_refptr<MediaStreamInterface> local_stream =
1852 pc_->local_streams()->at(0);
1853
1854 pc_->Close();
1855
1856 pc_->RemoveStream(local_stream);
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +00001857 EXPECT_FALSE(pc_->AddStream(local_stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001858
1859 ASSERT_FALSE(local_stream->GetAudioTracks().empty());
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001860 rtc::scoped_refptr<webrtc::DtmfSenderInterface> dtmf_sender(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001861 pc_->CreateDtmfSender(local_stream->GetAudioTracks()[0]));
wu@webrtc.org66037362013-08-13 00:09:35 +00001862 EXPECT_TRUE(NULL == dtmf_sender); // local stream has been removed.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001863
1864 EXPECT_TRUE(pc_->CreateDataChannel("test", NULL) == NULL);
1865
1866 EXPECT_TRUE(pc_->local_description() != NULL);
1867 EXPECT_TRUE(pc_->remote_description() != NULL);
1868
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001869 rtc::scoped_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001870 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001871 rtc::scoped_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -07001872 EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001873
1874 std::string sdp;
1875 ASSERT_TRUE(pc_->remote_description()->ToString(&sdp));
1876 SessionDescriptionInterface* remote_offer =
1877 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
1878 sdp, NULL);
1879 EXPECT_FALSE(DoSetRemoteDescription(remote_offer));
1880
1881 ASSERT_TRUE(pc_->local_description()->ToString(&sdp));
1882 SessionDescriptionInterface* local_offer =
1883 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
1884 sdp, NULL);
1885 EXPECT_FALSE(DoSetLocalDescription(local_offer));
1886}
1887
1888// Test that GetStats can still be called after PeerConnection::Close.
1889TEST_F(PeerConnectionInterfaceTest, CloseAndGetStats) {
1890 InitiateCall();
1891 pc_->Close();
1892 DoGetStats(NULL);
1893}
deadbeefab9b2d12015-10-14 11:33:11 -07001894
1895// NOTE: The series of tests below come from what used to be
1896// mediastreamsignaling_unittest.cc, and are mostly aimed at testing that
1897// setting a remote or local description has the expected effects.
1898
1899// This test verifies that the remote MediaStreams corresponding to a received
1900// SDP string is created. In this test the two separate MediaStreams are
1901// signaled.
1902TEST_F(PeerConnectionInterfaceTest, UpdateRemoteStreams) {
1903 FakeConstraints constraints;
1904 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
1905 true);
1906 CreatePeerConnection(&constraints);
1907 CreateAndSetRemoteOffer(kSdpStringWithStream1);
1908
1909 rtc::scoped_refptr<StreamCollection> reference(CreateStreamCollection(1));
1910 EXPECT_TRUE(
1911 CompareStreamCollections(observer_.remote_streams(), reference.get()));
1912 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
1913 EXPECT_TRUE(remote_stream->GetVideoTracks()[0]->GetSource() != nullptr);
1914
1915 // Create a session description based on another SDP with another
1916 // MediaStream.
1917 CreateAndSetRemoteOffer(kSdpStringWithStream1And2);
1918
1919 rtc::scoped_refptr<StreamCollection> reference2(CreateStreamCollection(2));
1920 EXPECT_TRUE(
1921 CompareStreamCollections(observer_.remote_streams(), reference2.get()));
1922}
1923
1924// This test verifies that when remote tracks are added/removed from SDP, the
1925// created remote streams are updated appropriately.
1926TEST_F(PeerConnectionInterfaceTest,
1927 AddRemoveTrackFromExistingRemoteMediaStream) {
1928 FakeConstraints constraints;
1929 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
1930 true);
1931 CreatePeerConnection(&constraints);
kwiberg2bbff992016-03-16 11:03:04 -07001932 rtc::scoped_ptr<SessionDescriptionInterface> desc_ms1 =
1933 CreateSessionDescriptionAndReference(1, 1);
deadbeefab9b2d12015-10-14 11:33:11 -07001934 EXPECT_TRUE(DoSetRemoteDescription(desc_ms1.release()));
1935 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
1936 reference_collection_));
1937
1938 // Add extra audio and video tracks to the same MediaStream.
kwiberg2bbff992016-03-16 11:03:04 -07001939 rtc::scoped_ptr<SessionDescriptionInterface> desc_ms1_two_tracks =
1940 CreateSessionDescriptionAndReference(2, 2);
deadbeefab9b2d12015-10-14 11:33:11 -07001941 EXPECT_TRUE(DoSetRemoteDescription(desc_ms1_two_tracks.release()));
1942 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
1943 reference_collection_));
1944
1945 // Remove the extra audio and video tracks.
kwiberg2bbff992016-03-16 11:03:04 -07001946 rtc::scoped_ptr<SessionDescriptionInterface> desc_ms2 =
1947 CreateSessionDescriptionAndReference(1, 1);
deadbeefab9b2d12015-10-14 11:33:11 -07001948 EXPECT_TRUE(DoSetRemoteDescription(desc_ms2.release()));
1949 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
1950 reference_collection_));
1951}
1952
1953// This tests that remote tracks are ended if a local session description is set
1954// that rejects the media content type.
1955TEST_F(PeerConnectionInterfaceTest, RejectMediaContent) {
1956 FakeConstraints constraints;
1957 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
1958 true);
1959 CreatePeerConnection(&constraints);
1960 // First create and set a remote offer, then reject its video content in our
1961 // answer.
1962 CreateAndSetRemoteOffer(kSdpStringWithStream1);
1963 ASSERT_EQ(1u, observer_.remote_streams()->count());
1964 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
1965 ASSERT_EQ(1u, remote_stream->GetVideoTracks().size());
1966 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
1967
1968 rtc::scoped_refptr<webrtc::VideoTrackInterface> remote_video =
1969 remote_stream->GetVideoTracks()[0];
1970 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, remote_video->state());
1971 rtc::scoped_refptr<webrtc::AudioTrackInterface> remote_audio =
1972 remote_stream->GetAudioTracks()[0];
1973 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, remote_audio->state());
1974
1975 rtc::scoped_ptr<SessionDescriptionInterface> local_answer;
kwiberg2bbff992016-03-16 11:03:04 -07001976 EXPECT_TRUE(DoCreateAnswer(&local_answer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07001977 cricket::ContentInfo* video_info =
1978 local_answer->description()->GetContentByName("video");
1979 video_info->rejected = true;
1980 EXPECT_TRUE(DoSetLocalDescription(local_answer.release()));
1981 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kEnded, remote_video->state());
1982 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, remote_audio->state());
1983
1984 // Now create an offer where we reject both video and audio.
1985 rtc::scoped_ptr<SessionDescriptionInterface> local_offer;
kwiberg2bbff992016-03-16 11:03:04 -07001986 EXPECT_TRUE(DoCreateOffer(&local_offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07001987 video_info = local_offer->description()->GetContentByName("video");
1988 ASSERT_TRUE(video_info != nullptr);
1989 video_info->rejected = true;
1990 cricket::ContentInfo* audio_info =
1991 local_offer->description()->GetContentByName("audio");
1992 ASSERT_TRUE(audio_info != nullptr);
1993 audio_info->rejected = true;
1994 EXPECT_TRUE(DoSetLocalDescription(local_offer.release()));
1995 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kEnded, remote_video->state());
1996 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kEnded, remote_audio->state());
1997}
1998
1999// This tests that we won't crash if the remote track has been removed outside
2000// of PeerConnection and then PeerConnection tries to reject the track.
2001TEST_F(PeerConnectionInterfaceTest, RemoveTrackThenRejectMediaContent) {
2002 FakeConstraints constraints;
2003 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2004 true);
2005 CreatePeerConnection(&constraints);
2006 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2007 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2008 remote_stream->RemoveTrack(remote_stream->GetVideoTracks()[0]);
2009 remote_stream->RemoveTrack(remote_stream->GetAudioTracks()[0]);
2010
2011 rtc::scoped_ptr<SessionDescriptionInterface> local_answer(
2012 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
2013 kSdpStringWithStream1, nullptr));
2014 cricket::ContentInfo* video_info =
2015 local_answer->description()->GetContentByName("video");
2016 video_info->rejected = true;
2017 cricket::ContentInfo* audio_info =
2018 local_answer->description()->GetContentByName("audio");
2019 audio_info->rejected = true;
2020 EXPECT_TRUE(DoSetLocalDescription(local_answer.release()));
2021
2022 // No crash is a pass.
2023}
2024
deadbeef5e97fb52015-10-15 12:49:08 -07002025// This tests that if a recvonly remote description is set, no remote streams
2026// will be created, even if the description contains SSRCs/MSIDs.
2027// See: https://code.google.com/p/webrtc/issues/detail?id=5054
2028TEST_F(PeerConnectionInterfaceTest, RecvonlyDescriptionDoesntCreateStream) {
2029 FakeConstraints constraints;
2030 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2031 true);
2032 CreatePeerConnection(&constraints);
2033
2034 std::string recvonly_offer = kSdpStringWithStream1;
2035 rtc::replace_substrs(kSendrecv, strlen(kSendrecv), kRecvonly,
2036 strlen(kRecvonly), &recvonly_offer);
2037 CreateAndSetRemoteOffer(recvonly_offer);
2038
2039 EXPECT_EQ(0u, observer_.remote_streams()->count());
2040}
2041
Stefan Holmer55d6e7c2016-03-17 16:26:40 +01002042// Disabled on Win dbg: https://bugs.chromium.org/p/webrtc/issues/detail?id=5659
Stefan Holmer102362b2016-03-18 09:39:07 +01002043#if defined(WEBRTC_WIN) && defined(_DEBUG)
Stefan Holmer55d6e7c2016-03-17 16:26:40 +01002044#define MAYBE_SdpWithoutMsidCreatesDefaultStream \
2045 DISABLED_SdpWithoutMsidCreatesDefaultStream
2046#else
2047#define MAYBE_SdpWithoutMsidCreatesDefaultStream \
2048 SdpWithoutMsidCreatesDefaultStream
2049#endif
deadbeefab9b2d12015-10-14 11:33:11 -07002050// This tests that a default MediaStream is created if a remote session
2051// description doesn't contain any streams and no MSID support.
2052// It also tests that the default stream is updated if a video m-line is added
2053// in a subsequent session description.
Stefan Holmer102362b2016-03-18 09:39:07 +01002054TEST_F(PeerConnectionInterfaceTest, MAYBE_SdpWithoutMsidCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002055 FakeConstraints constraints;
2056 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2057 true);
2058 CreatePeerConnection(&constraints);
2059 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2060
2061 ASSERT_EQ(1u, observer_.remote_streams()->count());
2062 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2063
2064 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2065 EXPECT_EQ(0u, remote_stream->GetVideoTracks().size());
2066 EXPECT_EQ("default", remote_stream->label());
2067
2068 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2069 ASSERT_EQ(1u, observer_.remote_streams()->count());
2070 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2071 EXPECT_EQ("defaulta0", remote_stream->GetAudioTracks()[0]->id());
deadbeef884f5852016-01-15 09:20:04 -08002072 EXPECT_EQ(MediaStreamTrackInterface::kLive,
2073 remote_stream->GetAudioTracks()[0]->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002074 ASSERT_EQ(1u, remote_stream->GetVideoTracks().size());
2075 EXPECT_EQ("defaultv0", remote_stream->GetVideoTracks()[0]->id());
deadbeef884f5852016-01-15 09:20:04 -08002076 EXPECT_EQ(MediaStreamTrackInterface::kLive,
2077 remote_stream->GetVideoTracks()[0]->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002078}
2079
Stefan Holmer55d6e7c2016-03-17 16:26:40 +01002080// Disabled on Win dbg: https://bugs.chromium.org/p/webrtc/issues/detail?id=5659
Stefan Holmer102362b2016-03-18 09:39:07 +01002081#if defined(WEBRTC_WIN) && defined(_DEBUG)
Stefan Holmer55d6e7c2016-03-17 16:26:40 +01002082#define MAYBE_SendOnlySdpWithoutMsidCreatesDefaultStream \
2083 DISABLED_SendOnlySdpWithoutMsidCreatesDefaultStream
2084#else
2085#define MAYBE_SendOnlySdpWithoutMsidCreatesDefaultStream \
2086 SendOnlySdpWithoutMsidCreatesDefaultStream
2087#endif
deadbeefab9b2d12015-10-14 11:33:11 -07002088// This tests that a default MediaStream is created if a remote session
2089// description doesn't contain any streams and media direction is send only.
2090TEST_F(PeerConnectionInterfaceTest,
Stefan Holmer55d6e7c2016-03-17 16:26:40 +01002091 MAYBE_SendOnlySdpWithoutMsidCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002092 FakeConstraints constraints;
2093 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2094 true);
2095 CreatePeerConnection(&constraints);
2096 CreateAndSetRemoteOffer(kSdpStringSendOnlyWithoutStreams);
2097
2098 ASSERT_EQ(1u, observer_.remote_streams()->count());
2099 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2100
2101 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2102 EXPECT_EQ(1u, remote_stream->GetVideoTracks().size());
2103 EXPECT_EQ("default", remote_stream->label());
2104}
2105
2106// This tests that it won't crash when PeerConnection tries to remove
2107// a remote track that as already been removed from the MediaStream.
2108TEST_F(PeerConnectionInterfaceTest, RemoveAlreadyGoneRemoteStream) {
2109 FakeConstraints constraints;
2110 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2111 true);
2112 CreatePeerConnection(&constraints);
2113 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2114 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2115 remote_stream->RemoveTrack(remote_stream->GetAudioTracks()[0]);
2116 remote_stream->RemoveTrack(remote_stream->GetVideoTracks()[0]);
2117
2118 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2119
2120 // No crash is a pass.
2121}
2122
Stefan Holmer55d6e7c2016-03-17 16:26:40 +01002123// Disabled on Win dbg: https://bugs.chromium.org/p/webrtc/issues/detail?id=5659
Stefan Holmer102362b2016-03-18 09:39:07 +01002124#if defined(WEBRTC_WIN) && defined(_DEBUG)
Stefan Holmer55d6e7c2016-03-17 16:26:40 +01002125#define MAYBE_SdpWithoutMsidAndStreamsCreatesDefaultStream \
2126 DISABLED_SdpWithoutMsidAndStreamsCreatesDefaultStream
2127#else
2128#define MAYBE_SdpWithoutMsidAndStreamsCreatesDefaultStream \
2129 SdpWithoutMsidAndStreamsCreatesDefaultStream
2130#endif
deadbeefab9b2d12015-10-14 11:33:11 -07002131// This tests that a default MediaStream is created if the remote session
2132// description doesn't contain any streams and don't contain an indication if
2133// MSID is supported.
2134TEST_F(PeerConnectionInterfaceTest,
Stefan Holmer55d6e7c2016-03-17 16:26:40 +01002135 MAYBE_SdpWithoutMsidAndStreamsCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002136 FakeConstraints constraints;
2137 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2138 true);
2139 CreatePeerConnection(&constraints);
2140 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2141
2142 ASSERT_EQ(1u, observer_.remote_streams()->count());
2143 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2144 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2145 EXPECT_EQ(1u, remote_stream->GetVideoTracks().size());
2146}
2147
Stefan Holmer55d6e7c2016-03-17 16:26:40 +01002148// Disabled on Win dbg: https://bugs.chromium.org/p/webrtc/issues/detail?id=5659
Stefan Holmer102362b2016-03-18 09:39:07 +01002149#if defined(WEBRTC_WIN) && defined(_DEBUG)
Stefan Holmer55d6e7c2016-03-17 16:26:40 +01002150#define MAYBE_SdpWithMsidDontCreatesDefaultStream \
2151 DISABLED_SdpWithMsidDontCreatesDefaultStream
2152#else
2153#define MAYBE_SdpWithMsidDontCreatesDefaultStream \
2154 SdpWithMsidDontCreatesDefaultStream
2155#endif
deadbeefab9b2d12015-10-14 11:33:11 -07002156// This tests that a default MediaStream is not created if the remote session
2157// description doesn't contain any streams but does support MSID.
Stefan Holmer55d6e7c2016-03-17 16:26:40 +01002158TEST_F(PeerConnectionInterfaceTest, MAYBE_SdpWithMsidDontCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002159 FakeConstraints constraints;
2160 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2161 true);
2162 CreatePeerConnection(&constraints);
2163 CreateAndSetRemoteOffer(kSdpStringWithMsidWithoutStreams);
2164 EXPECT_EQ(0u, observer_.remote_streams()->count());
2165}
2166
Stefan Holmer55d6e7c2016-03-17 16:26:40 +01002167// Disabled on Win dbg: https://bugs.chromium.org/p/webrtc/issues/detail?id=5659
Stefan Holmer102362b2016-03-18 09:39:07 +01002168#if defined(WEBRTC_WIN) && defined(_DEBUG)
Stefan Holmer55d6e7c2016-03-17 16:26:40 +01002169#define MAYBE_DefaultTracksNotDestroyedAndRecreated \
2170 DISABLED_DefaultTracksNotDestroyedAndRecreated
2171#else
2172#define MAYBE_DefaultTracksNotDestroyedAndRecreated \
2173 DefaultTracksNotDestroyedAndRecreated
2174#endif
deadbeefbda7e0b2015-12-08 17:13:40 -08002175// This tests that when setting a new description, the old default tracks are
2176// not destroyed and recreated.
2177// See: https://bugs.chromium.org/p/webrtc/issues/detail?id=5250
Stefan Holmer102362b2016-03-18 09:39:07 +01002178TEST_F(PeerConnectionInterfaceTest,
2179 MAYBE_DefaultTracksNotDestroyedAndRecreated) {
deadbeefbda7e0b2015-12-08 17:13:40 -08002180 FakeConstraints constraints;
2181 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2182 true);
2183 CreatePeerConnection(&constraints);
2184 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2185
2186 ASSERT_EQ(1u, observer_.remote_streams()->count());
2187 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2188 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2189
2190 // Set the track to "disabled", then set a new description and ensure the
2191 // track is still disabled, which ensures it hasn't been recreated.
2192 remote_stream->GetAudioTracks()[0]->set_enabled(false);
2193 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2194 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2195 EXPECT_FALSE(remote_stream->GetAudioTracks()[0]->enabled());
2196}
2197
deadbeefab9b2d12015-10-14 11:33:11 -07002198// This tests that a default MediaStream is not created if a remote session
2199// description is updated to not have any MediaStreams.
2200TEST_F(PeerConnectionInterfaceTest, VerifyDefaultStreamIsNotCreated) {
2201 FakeConstraints constraints;
2202 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2203 true);
2204 CreatePeerConnection(&constraints);
2205 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2206 rtc::scoped_refptr<StreamCollection> reference(CreateStreamCollection(1));
2207 EXPECT_TRUE(
2208 CompareStreamCollections(observer_.remote_streams(), reference.get()));
2209
2210 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2211 EXPECT_EQ(0u, observer_.remote_streams()->count());
2212}
2213
Stefan Holmer55d6e7c2016-03-17 16:26:40 +01002214// Disabled on Win dbg: https://bugs.chromium.org/p/webrtc/issues/detail?id=5659
Stefan Holmer102362b2016-03-18 09:39:07 +01002215#if defined(WEBRTC_WIN) && defined(_DEBUG)
Stefan Holmer55d6e7c2016-03-17 16:26:40 +01002216#define MAYBE_LocalDescriptionChanged DISABLED_LocalDescriptionChanged
2217#else
2218#define MAYBE_LocalDescriptionChanged LocalDescriptionChanged
2219#endif
deadbeefab9b2d12015-10-14 11:33:11 -07002220// This tests that an RtpSender is created when the local description is set
2221// after adding a local stream.
2222// TODO(deadbeef): This test and the one below it need to be updated when
2223// an RtpSender's lifetime isn't determined by when a local description is set.
Stefan Holmer55d6e7c2016-03-17 16:26:40 +01002224TEST_F(PeerConnectionInterfaceTest, MAYBE_LocalDescriptionChanged) {
deadbeefab9b2d12015-10-14 11:33:11 -07002225 FakeConstraints constraints;
2226 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2227 true);
2228 CreatePeerConnection(&constraints);
2229 // Create an offer just to ensure we have an identity before we manually
2230 // call SetLocalDescription.
2231 rtc::scoped_ptr<SessionDescriptionInterface> throwaway;
kwiberg2bbff992016-03-16 11:03:04 -07002232 ASSERT_TRUE(DoCreateOffer(&throwaway, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07002233
kwiberg2bbff992016-03-16 11:03:04 -07002234 rtc::scoped_ptr<SessionDescriptionInterface> desc_1 =
2235 CreateSessionDescriptionAndReference(2, 2);
deadbeefab9b2d12015-10-14 11:33:11 -07002236
2237 pc_->AddStream(reference_collection_->at(0));
2238 EXPECT_TRUE(DoSetLocalDescription(desc_1.release()));
2239 auto senders = pc_->GetSenders();
2240 EXPECT_EQ(4u, senders.size());
2241 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2242 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2243 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[1]));
2244 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[1]));
2245
2246 // Remove an audio and video track.
deadbeeffac06552015-11-25 11:26:01 -08002247 pc_->RemoveStream(reference_collection_->at(0));
kwiberg2bbff992016-03-16 11:03:04 -07002248 rtc::scoped_ptr<SessionDescriptionInterface> desc_2 =
2249 CreateSessionDescriptionAndReference(1, 1);
deadbeeffac06552015-11-25 11:26:01 -08002250 pc_->AddStream(reference_collection_->at(0));
deadbeefab9b2d12015-10-14 11:33:11 -07002251 EXPECT_TRUE(DoSetLocalDescription(desc_2.release()));
2252 senders = pc_->GetSenders();
2253 EXPECT_EQ(2u, senders.size());
2254 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2255 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2256 EXPECT_FALSE(ContainsSender(senders, kAudioTracks[1]));
2257 EXPECT_FALSE(ContainsSender(senders, kVideoTracks[1]));
2258}
2259
Stefan Holmer55d6e7c2016-03-17 16:26:40 +01002260// Disabled on Win dbg: https://bugs.chromium.org/p/webrtc/issues/detail?id=5659
Stefan Holmer102362b2016-03-18 09:39:07 +01002261#if defined(WEBRTC_WIN) && defined(_DEBUG)
Stefan Holmer55d6e7c2016-03-17 16:26:40 +01002262#define MAYBE_AddLocalStreamAfterLocalDescriptionChanged \
2263 DISABLED_AddLocalStreamAfterLocalDescriptionChanged
2264#else
2265#define MAYBE_AddLocalStreamAfterLocalDescriptionChanged \
2266 AddLocalStreamAfterLocalDescriptionChanged
2267#endif
deadbeefab9b2d12015-10-14 11:33:11 -07002268// This tests that an RtpSender is created when the local description is set
2269// before adding a local stream.
2270TEST_F(PeerConnectionInterfaceTest,
Stefan Holmer55d6e7c2016-03-17 16:26:40 +01002271 MAYBE_AddLocalStreamAfterLocalDescriptionChanged) {
deadbeefab9b2d12015-10-14 11:33:11 -07002272 FakeConstraints constraints;
2273 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2274 true);
2275 CreatePeerConnection(&constraints);
2276 // Create an offer just to ensure we have an identity before we manually
2277 // call SetLocalDescription.
2278 rtc::scoped_ptr<SessionDescriptionInterface> throwaway;
kwiberg2bbff992016-03-16 11:03:04 -07002279 ASSERT_TRUE(DoCreateOffer(&throwaway, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07002280
kwiberg2bbff992016-03-16 11:03:04 -07002281 rtc::scoped_ptr<SessionDescriptionInterface> desc_1 =
2282 CreateSessionDescriptionAndReference(2, 2);
deadbeefab9b2d12015-10-14 11:33:11 -07002283
2284 EXPECT_TRUE(DoSetLocalDescription(desc_1.release()));
2285 auto senders = pc_->GetSenders();
2286 EXPECT_EQ(0u, senders.size());
2287
2288 pc_->AddStream(reference_collection_->at(0));
2289 senders = pc_->GetSenders();
2290 EXPECT_EQ(4u, senders.size());
2291 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2292 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2293 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[1]));
2294 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[1]));
2295}
2296
Stefan Holmer55d6e7c2016-03-17 16:26:40 +01002297// Disabled on Win dbg: https://bugs.chromium.org/p/webrtc/issues/detail?id=5659
Stefan Holmer102362b2016-03-18 09:39:07 +01002298#if defined(WEBRTC_WIN) && defined(_DEBUG)
Stefan Holmer55d6e7c2016-03-17 16:26:40 +01002299#define MAYBE_ChangeSsrcOnTrackInLocalSessionDescription \
2300 DISABLED_ChangeSsrcOnTrackInLocalSessionDescription
2301#else
2302#define MAYBE_ChangeSsrcOnTrackInLocalSessionDescription \
2303 ChangeSsrcOnTrackInLocalSessionDescription
2304#endif
deadbeefab9b2d12015-10-14 11:33:11 -07002305// This tests that the expected behavior occurs if the SSRC on a local track is
2306// changed when SetLocalDescription is called.
2307TEST_F(PeerConnectionInterfaceTest,
Stefan Holmer55d6e7c2016-03-17 16:26:40 +01002308 MAYBE_ChangeSsrcOnTrackInLocalSessionDescription) {
deadbeefab9b2d12015-10-14 11:33:11 -07002309 FakeConstraints constraints;
2310 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2311 true);
2312 CreatePeerConnection(&constraints);
2313 // Create an offer just to ensure we have an identity before we manually
2314 // call SetLocalDescription.
2315 rtc::scoped_ptr<SessionDescriptionInterface> throwaway;
kwiberg2bbff992016-03-16 11:03:04 -07002316 ASSERT_TRUE(DoCreateOffer(&throwaway, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07002317
kwiberg2bbff992016-03-16 11:03:04 -07002318 rtc::scoped_ptr<SessionDescriptionInterface> desc =
2319 CreateSessionDescriptionAndReference(1, 1);
deadbeefab9b2d12015-10-14 11:33:11 -07002320 std::string sdp;
2321 desc->ToString(&sdp);
2322
2323 pc_->AddStream(reference_collection_->at(0));
2324 EXPECT_TRUE(DoSetLocalDescription(desc.release()));
2325 auto senders = pc_->GetSenders();
2326 EXPECT_EQ(2u, senders.size());
2327 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2328 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2329
2330 // Change the ssrc of the audio and video track.
2331 std::string ssrc_org = "a=ssrc:1";
2332 std::string ssrc_to = "a=ssrc:97";
2333 rtc::replace_substrs(ssrc_org.c_str(), ssrc_org.length(), ssrc_to.c_str(),
2334 ssrc_to.length(), &sdp);
2335 ssrc_org = "a=ssrc:2";
2336 ssrc_to = "a=ssrc:98";
2337 rtc::replace_substrs(ssrc_org.c_str(), ssrc_org.length(), ssrc_to.c_str(),
2338 ssrc_to.length(), &sdp);
2339 rtc::scoped_ptr<SessionDescriptionInterface> updated_desc(
2340 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer, sdp,
2341 nullptr));
2342
2343 EXPECT_TRUE(DoSetLocalDescription(updated_desc.release()));
2344 senders = pc_->GetSenders();
2345 EXPECT_EQ(2u, senders.size());
2346 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2347 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2348 // TODO(deadbeef): Once RtpSenders expose parameters, check that the SSRC
2349 // changed.
2350}
2351
Stefan Holmer55d6e7c2016-03-17 16:26:40 +01002352// Disabled on Win dbg: https://bugs.chromium.org/p/webrtc/issues/detail?id=5659
Stefan Holmer102362b2016-03-18 09:39:07 +01002353#if defined(WEBRTC_WIN) && defined(_DEBUG)
Stefan Holmer55d6e7c2016-03-17 16:26:40 +01002354#define MAYBE_SignalSameTracksInSeparateMediaStream \
2355 DISABLED_SignalSameTracksInSeparateMediaStream
2356#else
2357#define MAYBE_SignalSameTracksInSeparateMediaStream \
2358 SignalSameTracksInSeparateMediaStream
2359#endif
deadbeefab9b2d12015-10-14 11:33:11 -07002360// This tests that the expected behavior occurs if a new session description is
2361// set with the same tracks, but on a different MediaStream.
Stefan Holmer55d6e7c2016-03-17 16:26:40 +01002362TEST_F(PeerConnectionInterfaceTest,
2363 MAYBE_SignalSameTracksInSeparateMediaStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002364 FakeConstraints constraints;
2365 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2366 true);
2367 CreatePeerConnection(&constraints);
2368 // Create an offer just to ensure we have an identity before we manually
2369 // call SetLocalDescription.
2370 rtc::scoped_ptr<SessionDescriptionInterface> throwaway;
kwiberg2bbff992016-03-16 11:03:04 -07002371 ASSERT_TRUE(DoCreateOffer(&throwaway, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07002372
kwiberg2bbff992016-03-16 11:03:04 -07002373 rtc::scoped_ptr<SessionDescriptionInterface> desc =
2374 CreateSessionDescriptionAndReference(1, 1);
deadbeefab9b2d12015-10-14 11:33:11 -07002375 std::string sdp;
2376 desc->ToString(&sdp);
2377
2378 pc_->AddStream(reference_collection_->at(0));
2379 EXPECT_TRUE(DoSetLocalDescription(desc.release()));
2380 auto senders = pc_->GetSenders();
2381 EXPECT_EQ(2u, senders.size());
2382 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2383 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2384
2385 // Add a new MediaStream but with the same tracks as in the first stream.
2386 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream_1(
2387 webrtc::MediaStream::Create(kStreams[1]));
2388 stream_1->AddTrack(reference_collection_->at(0)->GetVideoTracks()[0]);
2389 stream_1->AddTrack(reference_collection_->at(0)->GetAudioTracks()[0]);
2390 pc_->AddStream(stream_1);
2391
2392 // Replace msid in the original SDP.
2393 rtc::replace_substrs(kStreams[0], strlen(kStreams[0]), kStreams[1],
2394 strlen(kStreams[1]), &sdp);
2395
2396 rtc::scoped_ptr<SessionDescriptionInterface> updated_desc(
2397 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer, sdp,
2398 nullptr));
2399
2400 EXPECT_TRUE(DoSetLocalDescription(updated_desc.release()));
2401 senders = pc_->GetSenders();
2402 EXPECT_EQ(2u, senders.size());
2403 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2404 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2405}
2406
nisse51542be2016-02-12 02:27:06 -08002407// The PeerConnectionMediaConfig tests below verify that configuration
2408// and constraints are propagated into the MediaConfig passed to
2409// CreateMediaController. These settings are intended for MediaChannel
2410// constructors, but that is not exercised by these unittest.
2411class PeerConnectionFactoryForTest : public webrtc::PeerConnectionFactory {
2412 public:
2413 webrtc::MediaControllerInterface* CreateMediaController(
2414 const cricket::MediaConfig& config) const override {
2415 create_media_controller_called_ = true;
2416 create_media_controller_config_ = config;
2417
2418 webrtc::MediaControllerInterface* mc =
2419 PeerConnectionFactory::CreateMediaController(config);
2420 EXPECT_TRUE(mc != nullptr);
2421 return mc;
2422 }
2423
2424 // Mutable, so they can be modified in the above const-declared method.
2425 mutable bool create_media_controller_called_ = false;
2426 mutable cricket::MediaConfig create_media_controller_config_;
2427};
2428
2429class PeerConnectionMediaConfigTest : public testing::Test {
2430 protected:
2431 void SetUp() override {
2432 pcf_= new rtc::RefCountedObject<PeerConnectionFactoryForTest>();
2433 pcf_->Initialize();
2434 }
2435 const cricket::MediaConfig& TestCreatePeerConnection(
2436 const PeerConnectionInterface::RTCConfiguration& config,
2437 const MediaConstraintsInterface *constraints) {
2438 pcf_->create_media_controller_called_ = false;
2439
2440 scoped_refptr<PeerConnectionInterface> pc(
2441 pcf_->CreatePeerConnection(config, constraints, nullptr, nullptr,
2442 &observer_));
2443 EXPECT_TRUE(pc.get());
2444 EXPECT_TRUE(pcf_->create_media_controller_called_);
2445 return pcf_->create_media_controller_config_;
2446 }
2447
2448 scoped_refptr<PeerConnectionFactoryForTest> pcf_;
2449 MockPeerConnectionObserver observer_;
2450};
2451
2452// This test verifies the default behaviour with no constraints and a
2453// default RTCConfiguration.
2454TEST_F(PeerConnectionMediaConfigTest, TestDefaults) {
2455 PeerConnectionInterface::RTCConfiguration config;
2456 FakeConstraints constraints;
2457
2458 const cricket::MediaConfig& media_config =
2459 TestCreatePeerConnection(config, &constraints);
2460
2461 EXPECT_FALSE(media_config.enable_dscp);
nisse0db023a2016-03-01 04:29:59 -08002462 EXPECT_TRUE(media_config.video.enable_cpu_overuse_detection);
2463 EXPECT_FALSE(media_config.video.disable_prerenderer_smoothing);
2464 EXPECT_FALSE(media_config.video.suspend_below_min_bitrate);
nisse51542be2016-02-12 02:27:06 -08002465}
2466
2467// This test verifies the DSCP constraint is recognized and passed to
2468// the CreateMediaController call.
2469TEST_F(PeerConnectionMediaConfigTest, TestDscpConstraintTrue) {
2470 PeerConnectionInterface::RTCConfiguration config;
2471 FakeConstraints constraints;
2472
2473 constraints.AddOptional(webrtc::MediaConstraintsInterface::kEnableDscp, true);
2474 const cricket::MediaConfig& media_config =
2475 TestCreatePeerConnection(config, &constraints);
2476
2477 EXPECT_TRUE(media_config.enable_dscp);
2478}
2479
2480// This test verifies the cpu overuse detection constraint is
2481// recognized and passed to the CreateMediaController call.
2482TEST_F(PeerConnectionMediaConfigTest, TestCpuOveruseConstraintFalse) {
2483 PeerConnectionInterface::RTCConfiguration config;
2484 FakeConstraints constraints;
2485
2486 constraints.AddOptional(
2487 webrtc::MediaConstraintsInterface::kCpuOveruseDetection, false);
2488 const cricket::MediaConfig media_config =
2489 TestCreatePeerConnection(config, &constraints);
2490
nisse0db023a2016-03-01 04:29:59 -08002491 EXPECT_FALSE(media_config.video.enable_cpu_overuse_detection);
nisse51542be2016-02-12 02:27:06 -08002492}
2493
2494// This test verifies that the disable_prerenderer_smoothing flag is
2495// propagated from RTCConfiguration to the CreateMediaController call.
2496TEST_F(PeerConnectionMediaConfigTest, TestDisablePrerendererSmoothingTrue) {
2497 PeerConnectionInterface::RTCConfiguration config;
2498 FakeConstraints constraints;
2499
2500 config.disable_prerenderer_smoothing = true;
2501 const cricket::MediaConfig& media_config =
2502 TestCreatePeerConnection(config, &constraints);
2503
nisse0db023a2016-03-01 04:29:59 -08002504 EXPECT_TRUE(media_config.video.disable_prerenderer_smoothing);
2505}
2506
2507// This test verifies the suspend below min bitrate constraint is
2508// recognized and passed to the CreateMediaController call.
2509TEST_F(PeerConnectionMediaConfigTest,
2510 TestSuspendBelowMinBitrateConstraintTrue) {
2511 PeerConnectionInterface::RTCConfiguration config;
2512 FakeConstraints constraints;
2513
2514 constraints.AddOptional(
2515 webrtc::MediaConstraintsInterface::kEnableVideoSuspendBelowMinBitrate,
2516 true);
2517 const cricket::MediaConfig media_config =
2518 TestCreatePeerConnection(config, &constraints);
2519
2520 EXPECT_TRUE(media_config.video.suspend_below_min_bitrate);
nisse51542be2016-02-12 02:27:06 -08002521}
2522
deadbeefab9b2d12015-10-14 11:33:11 -07002523// The following tests verify that session options are created correctly.
deadbeefc80741f2015-10-22 13:14:45 -07002524// TODO(deadbeef): Convert these tests to be more end-to-end. Instead of
2525// "verify options are converted correctly", should be "pass options into
2526// CreateOffer and verify the correct offer is produced."
deadbeefab9b2d12015-10-14 11:33:11 -07002527
2528TEST(CreateSessionOptionsTest, GetOptionsForOfferWithInvalidAudioOption) {
2529 RTCOfferAnswerOptions rtc_options;
2530 rtc_options.offer_to_receive_audio = RTCOfferAnswerOptions::kUndefined - 1;
2531
2532 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08002533 EXPECT_FALSE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002534
2535 rtc_options.offer_to_receive_audio =
2536 RTCOfferAnswerOptions::kMaxOfferToReceiveMedia + 1;
htaaac2dea2016-03-10 13:35:55 -08002537 EXPECT_FALSE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002538}
2539
2540TEST(CreateSessionOptionsTest, GetOptionsForOfferWithInvalidVideoOption) {
2541 RTCOfferAnswerOptions rtc_options;
2542 rtc_options.offer_to_receive_video = RTCOfferAnswerOptions::kUndefined - 1;
2543
2544 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08002545 EXPECT_FALSE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002546
2547 rtc_options.offer_to_receive_video =
2548 RTCOfferAnswerOptions::kMaxOfferToReceiveMedia + 1;
htaaac2dea2016-03-10 13:35:55 -08002549 EXPECT_FALSE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002550}
2551
2552// Test that a MediaSessionOptions is created for an offer if
deadbeefc80741f2015-10-22 13:14:45 -07002553// OfferToReceiveAudio and OfferToReceiveVideo options are set.
deadbeefab9b2d12015-10-14 11:33:11 -07002554TEST(CreateSessionOptionsTest, GetMediaSessionOptionsForOfferWithAudioVideo) {
2555 RTCOfferAnswerOptions rtc_options;
2556 rtc_options.offer_to_receive_audio = 1;
2557 rtc_options.offer_to_receive_video = 1;
2558
2559 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08002560 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002561 EXPECT_TRUE(options.has_audio());
2562 EXPECT_TRUE(options.has_video());
2563 EXPECT_TRUE(options.bundle_enabled);
2564}
2565
2566// Test that a correct MediaSessionOptions is created for an offer if
deadbeefc80741f2015-10-22 13:14:45 -07002567// OfferToReceiveAudio is set.
deadbeefab9b2d12015-10-14 11:33:11 -07002568TEST(CreateSessionOptionsTest, GetMediaSessionOptionsForOfferWithAudio) {
2569 RTCOfferAnswerOptions rtc_options;
2570 rtc_options.offer_to_receive_audio = 1;
2571
2572 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08002573 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002574 EXPECT_TRUE(options.has_audio());
2575 EXPECT_FALSE(options.has_video());
2576 EXPECT_TRUE(options.bundle_enabled);
2577}
2578
2579// Test that a correct MediaSessionOptions is created for an offer if
deadbeefc80741f2015-10-22 13:14:45 -07002580// the default OfferOptions are used.
deadbeefab9b2d12015-10-14 11:33:11 -07002581TEST(CreateSessionOptionsTest, GetDefaultMediaSessionOptionsForOffer) {
2582 RTCOfferAnswerOptions rtc_options;
2583
2584 cricket::MediaSessionOptions options;
deadbeef0ed85b22016-02-23 17:24:52 -08002585 options.transport_options["audio"] = cricket::TransportOptions();
2586 options.transport_options["video"] = cricket::TransportOptions();
htaaac2dea2016-03-10 13:35:55 -08002587 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefc80741f2015-10-22 13:14:45 -07002588 EXPECT_TRUE(options.has_audio());
deadbeefab9b2d12015-10-14 11:33:11 -07002589 EXPECT_FALSE(options.has_video());
deadbeefc80741f2015-10-22 13:14:45 -07002590 EXPECT_TRUE(options.bundle_enabled);
deadbeefab9b2d12015-10-14 11:33:11 -07002591 EXPECT_TRUE(options.vad_enabled);
deadbeef0ed85b22016-02-23 17:24:52 -08002592 EXPECT_FALSE(options.transport_options["audio"].ice_restart);
2593 EXPECT_FALSE(options.transport_options["video"].ice_restart);
deadbeefab9b2d12015-10-14 11:33:11 -07002594}
2595
2596// Test that a correct MediaSessionOptions is created for an offer if
deadbeefc80741f2015-10-22 13:14:45 -07002597// OfferToReceiveVideo is set.
deadbeefab9b2d12015-10-14 11:33:11 -07002598TEST(CreateSessionOptionsTest, GetMediaSessionOptionsForOfferWithVideo) {
2599 RTCOfferAnswerOptions rtc_options;
2600 rtc_options.offer_to_receive_audio = 0;
2601 rtc_options.offer_to_receive_video = 1;
2602
2603 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08002604 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002605 EXPECT_FALSE(options.has_audio());
2606 EXPECT_TRUE(options.has_video());
2607 EXPECT_TRUE(options.bundle_enabled);
2608}
2609
2610// Test that a correct MediaSessionOptions is created for an offer if
2611// UseRtpMux is set to false.
2612TEST(CreateSessionOptionsTest,
2613 GetMediaSessionOptionsForOfferWithBundleDisabled) {
2614 RTCOfferAnswerOptions rtc_options;
2615 rtc_options.offer_to_receive_audio = 1;
2616 rtc_options.offer_to_receive_video = 1;
2617 rtc_options.use_rtp_mux = false;
2618
2619 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08002620 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002621 EXPECT_TRUE(options.has_audio());
2622 EXPECT_TRUE(options.has_video());
2623 EXPECT_FALSE(options.bundle_enabled);
2624}
2625
2626// Test that a correct MediaSessionOptions is created to restart ice if
2627// IceRestart is set. It also tests that subsequent MediaSessionOptions don't
Taylor Brandstetterf475d362016-01-08 15:35:57 -08002628// have |audio_transport_options.ice_restart| etc. set.
deadbeefab9b2d12015-10-14 11:33:11 -07002629TEST(CreateSessionOptionsTest, GetMediaSessionOptionsForOfferWithIceRestart) {
2630 RTCOfferAnswerOptions rtc_options;
2631 rtc_options.ice_restart = true;
2632
2633 cricket::MediaSessionOptions options;
deadbeef0ed85b22016-02-23 17:24:52 -08002634 options.transport_options["audio"] = cricket::TransportOptions();
2635 options.transport_options["video"] = cricket::TransportOptions();
htaaac2dea2016-03-10 13:35:55 -08002636 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeef0ed85b22016-02-23 17:24:52 -08002637 EXPECT_TRUE(options.transport_options["audio"].ice_restart);
2638 EXPECT_TRUE(options.transport_options["video"].ice_restart);
deadbeefab9b2d12015-10-14 11:33:11 -07002639
2640 rtc_options = RTCOfferAnswerOptions();
htaaac2dea2016-03-10 13:35:55 -08002641 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeef0ed85b22016-02-23 17:24:52 -08002642 EXPECT_FALSE(options.transport_options["audio"].ice_restart);
2643 EXPECT_FALSE(options.transport_options["video"].ice_restart);
deadbeefab9b2d12015-10-14 11:33:11 -07002644}
2645
2646// Test that the MediaConstraints in an answer don't affect if audio and video
2647// is offered in an offer but that if kOfferToReceiveAudio or
2648// kOfferToReceiveVideo constraints are true in an offer, the media type will be
2649// included in subsequent answers.
2650TEST(CreateSessionOptionsTest, MediaConstraintsInAnswer) {
2651 FakeConstraints answer_c;
2652 answer_c.SetMandatoryReceiveAudio(true);
2653 answer_c.SetMandatoryReceiveVideo(true);
2654
2655 cricket::MediaSessionOptions answer_options;
2656 EXPECT_TRUE(ParseConstraintsForAnswer(&answer_c, &answer_options));
2657 EXPECT_TRUE(answer_options.has_audio());
2658 EXPECT_TRUE(answer_options.has_video());
2659
deadbeefc80741f2015-10-22 13:14:45 -07002660 RTCOfferAnswerOptions rtc_offer_options;
deadbeefab9b2d12015-10-14 11:33:11 -07002661
2662 cricket::MediaSessionOptions offer_options;
htaaac2dea2016-03-10 13:35:55 -08002663 EXPECT_TRUE(
2664 ExtractMediaSessionOptions(rtc_offer_options, false, &offer_options));
deadbeefc80741f2015-10-22 13:14:45 -07002665 EXPECT_TRUE(offer_options.has_audio());
htaaac2dea2016-03-10 13:35:55 -08002666 EXPECT_TRUE(offer_options.has_video());
deadbeefab9b2d12015-10-14 11:33:11 -07002667
deadbeefc80741f2015-10-22 13:14:45 -07002668 RTCOfferAnswerOptions updated_rtc_offer_options;
2669 updated_rtc_offer_options.offer_to_receive_audio = 1;
2670 updated_rtc_offer_options.offer_to_receive_video = 1;
deadbeefab9b2d12015-10-14 11:33:11 -07002671
2672 cricket::MediaSessionOptions updated_offer_options;
htaaac2dea2016-03-10 13:35:55 -08002673 EXPECT_TRUE(ExtractMediaSessionOptions(updated_rtc_offer_options, false,
htaa2a49d92016-03-04 02:51:39 -08002674 &updated_offer_options));
deadbeefab9b2d12015-10-14 11:33:11 -07002675 EXPECT_TRUE(updated_offer_options.has_audio());
2676 EXPECT_TRUE(updated_offer_options.has_video());
2677
2678 // Since an offer has been created with both audio and video, subsequent
2679 // offers and answers should contain both audio and video.
2680 // Answers will only contain the media types that exist in the offer
2681 // regardless of the value of |updated_answer_options.has_audio| and
2682 // |updated_answer_options.has_video|.
2683 FakeConstraints updated_answer_c;
2684 answer_c.SetMandatoryReceiveAudio(false);
2685 answer_c.SetMandatoryReceiveVideo(false);
2686
2687 cricket::MediaSessionOptions updated_answer_options;
2688 EXPECT_TRUE(
2689 ParseConstraintsForAnswer(&updated_answer_c, &updated_answer_options));
2690 EXPECT_TRUE(updated_answer_options.has_audio());
2691 EXPECT_TRUE(updated_answer_options.has_video());
deadbeefab9b2d12015-10-14 11:33:11 -07002692}