blob: 7b4787c9739ad1938ac5c03c7fb6d8c0ebbb589e [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
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001403 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
1669// Test that we can create a session description from an SDP string from
1670// FireFox, use it as a remote session description, generate an answer and use
1671// the answer as a local description.
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00001672TEST_F(PeerConnectionInterfaceTest, ReceiveFireFoxOffer) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001673 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001674 FakeConstraints constraints;
1675 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
1676 true);
1677 CreatePeerConnection(&constraints);
1678 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
1679 SessionDescriptionInterface* desc =
1680 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
jbauchfabe2c92015-07-16 13:43:14 -07001681 webrtc::kFireFoxSdpOffer, nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001682 EXPECT_TRUE(DoSetSessionDescription(desc, false));
1683 CreateAnswerAsLocalDescription();
1684 ASSERT_TRUE(pc_->local_description() != NULL);
1685 ASSERT_TRUE(pc_->remote_description() != NULL);
1686
1687 const cricket::ContentInfo* content =
1688 cricket::GetFirstAudioContent(pc_->local_description()->description());
1689 ASSERT_TRUE(content != NULL);
1690 EXPECT_FALSE(content->rejected);
1691
1692 content =
1693 cricket::GetFirstVideoContent(pc_->local_description()->description());
1694 ASSERT_TRUE(content != NULL);
1695 EXPECT_FALSE(content->rejected);
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00001696#ifdef HAVE_SCTP
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001697 content =
1698 cricket::GetFirstDataContent(pc_->local_description()->description());
1699 ASSERT_TRUE(content != NULL);
1700 EXPECT_TRUE(content->rejected);
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00001701#endif
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001702}
1703
1704// Test that we can create an audio only offer and receive an answer with a
1705// limited set of audio codecs and receive an updated offer with more audio
1706// codecs, where the added codecs are not supported.
1707TEST_F(PeerConnectionInterfaceTest, ReceiveUpdatedAudioOfferWithBadCodecs) {
1708 CreatePeerConnection();
1709 AddVoiceStream("audio_label");
1710 CreateOfferAsLocalDescription();
1711
1712 SessionDescriptionInterface* answer =
1713 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
jbauchfabe2c92015-07-16 13:43:14 -07001714 webrtc::kAudioSdp, nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001715 EXPECT_TRUE(DoSetSessionDescription(answer, false));
1716
1717 SessionDescriptionInterface* updated_offer =
1718 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
jbauchfabe2c92015-07-16 13:43:14 -07001719 webrtc::kAudioSdpWithUnsupportedCodecs,
1720 nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001721 EXPECT_TRUE(DoSetSessionDescription(updated_offer, false));
1722 CreateAnswerAsLocalDescription();
1723}
1724
deadbeefc80741f2015-10-22 13:14:45 -07001725// Test that if we're receiving (but not sending) a track, subsequent offers
1726// will have m-lines with a=recvonly.
1727TEST_F(PeerConnectionInterfaceTest, CreateSubsequentRecvOnlyOffer) {
1728 FakeConstraints constraints;
1729 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
1730 true);
1731 CreatePeerConnection(&constraints);
1732 CreateAndSetRemoteOffer(kSdpStringWithStream1);
1733 CreateAnswerAsLocalDescription();
1734
1735 // At this point we should be receiving stream 1, but not sending anything.
1736 // A new offer should be recvonly.
kwiberg2bbff992016-03-16 11:03:04 -07001737 rtc::scoped_ptr<SessionDescriptionInterface> offer;
deadbeefc80741f2015-10-22 13:14:45 -07001738 DoCreateOffer(&offer, nullptr);
1739
1740 const cricket::ContentInfo* video_content =
1741 cricket::GetFirstVideoContent(offer->description());
1742 const cricket::VideoContentDescription* video_desc =
1743 static_cast<const cricket::VideoContentDescription*>(
1744 video_content->description);
1745 ASSERT_EQ(cricket::MD_RECVONLY, video_desc->direction());
1746
1747 const cricket::ContentInfo* audio_content =
1748 cricket::GetFirstAudioContent(offer->description());
1749 const cricket::AudioContentDescription* audio_desc =
1750 static_cast<const cricket::AudioContentDescription*>(
1751 audio_content->description);
1752 ASSERT_EQ(cricket::MD_RECVONLY, audio_desc->direction());
1753}
1754
1755// Test that if we're receiving (but not sending) a track, and the
1756// offerToReceiveVideo/offerToReceiveAudio constraints are explicitly set to
1757// false, the generated m-lines will be a=inactive.
1758TEST_F(PeerConnectionInterfaceTest, CreateSubsequentInactiveOffer) {
1759 FakeConstraints constraints;
1760 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
1761 true);
1762 CreatePeerConnection(&constraints);
1763 CreateAndSetRemoteOffer(kSdpStringWithStream1);
1764 CreateAnswerAsLocalDescription();
1765
1766 // At this point we should be receiving stream 1, but not sending anything.
1767 // A new offer would be recvonly, but we'll set the "no receive" constraints
1768 // to make it inactive.
kwiberg2bbff992016-03-16 11:03:04 -07001769 rtc::scoped_ptr<SessionDescriptionInterface> offer;
deadbeefc80741f2015-10-22 13:14:45 -07001770 FakeConstraints offer_constraints;
1771 offer_constraints.AddMandatory(
1772 webrtc::MediaConstraintsInterface::kOfferToReceiveVideo, false);
1773 offer_constraints.AddMandatory(
1774 webrtc::MediaConstraintsInterface::kOfferToReceiveAudio, false);
1775 DoCreateOffer(&offer, &offer_constraints);
1776
1777 const cricket::ContentInfo* video_content =
1778 cricket::GetFirstVideoContent(offer->description());
1779 const cricket::VideoContentDescription* video_desc =
1780 static_cast<const cricket::VideoContentDescription*>(
1781 video_content->description);
1782 ASSERT_EQ(cricket::MD_INACTIVE, video_desc->direction());
1783
1784 const cricket::ContentInfo* audio_content =
1785 cricket::GetFirstAudioContent(offer->description());
1786 const cricket::AudioContentDescription* audio_desc =
1787 static_cast<const cricket::AudioContentDescription*>(
1788 audio_content->description);
1789 ASSERT_EQ(cricket::MD_INACTIVE, audio_desc->direction());
1790}
1791
deadbeef653b8e02015-11-11 12:55:10 -08001792// Test that we can use SetConfiguration to change the ICE servers of the
1793// PortAllocator.
1794TEST_F(PeerConnectionInterfaceTest, SetConfigurationChangesIceServers) {
1795 CreatePeerConnection();
1796
1797 PeerConnectionInterface::RTCConfiguration config;
1798 PeerConnectionInterface::IceServer server;
1799 server.uri = "stun:test_hostname";
1800 config.servers.push_back(server);
1801 EXPECT_TRUE(pc_->SetConfiguration(config));
1802
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08001803 EXPECT_EQ(1u, port_allocator_->stun_servers().size());
1804 EXPECT_EQ("test_hostname",
1805 port_allocator_->stun_servers().begin()->hostname());
deadbeef653b8e02015-11-11 12:55:10 -08001806}
1807
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001808// Test that PeerConnection::Close changes the states to closed and all remote
1809// tracks change state to ended.
1810TEST_F(PeerConnectionInterfaceTest, CloseAndTestStreamsAndStates) {
1811 // Initialize a PeerConnection and negotiate local and remote session
1812 // description.
1813 InitiateCall();
1814 ASSERT_EQ(1u, pc_->local_streams()->count());
1815 ASSERT_EQ(1u, pc_->remote_streams()->count());
1816
1817 pc_->Close();
1818
1819 EXPECT_EQ(PeerConnectionInterface::kClosed, pc_->signaling_state());
1820 EXPECT_EQ(PeerConnectionInterface::kIceConnectionClosed,
1821 pc_->ice_connection_state());
1822 EXPECT_EQ(PeerConnectionInterface::kIceGatheringComplete,
1823 pc_->ice_gathering_state());
1824
1825 EXPECT_EQ(1u, pc_->local_streams()->count());
1826 EXPECT_EQ(1u, pc_->remote_streams()->count());
1827
1828 scoped_refptr<MediaStreamInterface> remote_stream =
1829 pc_->remote_streams()->at(0);
1830 EXPECT_EQ(MediaStreamTrackInterface::kEnded,
1831 remote_stream->GetVideoTracks()[0]->state());
1832 EXPECT_EQ(MediaStreamTrackInterface::kEnded,
1833 remote_stream->GetAudioTracks()[0]->state());
1834}
1835
1836// Test that PeerConnection methods fails gracefully after
1837// PeerConnection::Close has been called.
1838TEST_F(PeerConnectionInterfaceTest, CloseAndTestMethods) {
1839 CreatePeerConnection();
1840 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
1841 CreateOfferAsRemoteDescription();
1842 CreateAnswerAsLocalDescription();
1843
1844 ASSERT_EQ(1u, pc_->local_streams()->count());
1845 scoped_refptr<MediaStreamInterface> local_stream =
1846 pc_->local_streams()->at(0);
1847
1848 pc_->Close();
1849
1850 pc_->RemoveStream(local_stream);
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +00001851 EXPECT_FALSE(pc_->AddStream(local_stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001852
1853 ASSERT_FALSE(local_stream->GetAudioTracks().empty());
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001854 rtc::scoped_refptr<webrtc::DtmfSenderInterface> dtmf_sender(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001855 pc_->CreateDtmfSender(local_stream->GetAudioTracks()[0]));
wu@webrtc.org66037362013-08-13 00:09:35 +00001856 EXPECT_TRUE(NULL == dtmf_sender); // local stream has been removed.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001857
1858 EXPECT_TRUE(pc_->CreateDataChannel("test", NULL) == NULL);
1859
1860 EXPECT_TRUE(pc_->local_description() != NULL);
1861 EXPECT_TRUE(pc_->remote_description() != NULL);
1862
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001863 rtc::scoped_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001864 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001865 rtc::scoped_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -07001866 EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001867
1868 std::string sdp;
1869 ASSERT_TRUE(pc_->remote_description()->ToString(&sdp));
1870 SessionDescriptionInterface* remote_offer =
1871 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
1872 sdp, NULL);
1873 EXPECT_FALSE(DoSetRemoteDescription(remote_offer));
1874
1875 ASSERT_TRUE(pc_->local_description()->ToString(&sdp));
1876 SessionDescriptionInterface* local_offer =
1877 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
1878 sdp, NULL);
1879 EXPECT_FALSE(DoSetLocalDescription(local_offer));
1880}
1881
1882// Test that GetStats can still be called after PeerConnection::Close.
1883TEST_F(PeerConnectionInterfaceTest, CloseAndGetStats) {
1884 InitiateCall();
1885 pc_->Close();
1886 DoGetStats(NULL);
1887}
deadbeefab9b2d12015-10-14 11:33:11 -07001888
1889// NOTE: The series of tests below come from what used to be
1890// mediastreamsignaling_unittest.cc, and are mostly aimed at testing that
1891// setting a remote or local description has the expected effects.
1892
1893// This test verifies that the remote MediaStreams corresponding to a received
1894// SDP string is created. In this test the two separate MediaStreams are
1895// signaled.
1896TEST_F(PeerConnectionInterfaceTest, UpdateRemoteStreams) {
1897 FakeConstraints constraints;
1898 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
1899 true);
1900 CreatePeerConnection(&constraints);
1901 CreateAndSetRemoteOffer(kSdpStringWithStream1);
1902
1903 rtc::scoped_refptr<StreamCollection> reference(CreateStreamCollection(1));
1904 EXPECT_TRUE(
1905 CompareStreamCollections(observer_.remote_streams(), reference.get()));
1906 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
1907 EXPECT_TRUE(remote_stream->GetVideoTracks()[0]->GetSource() != nullptr);
1908
1909 // Create a session description based on another SDP with another
1910 // MediaStream.
1911 CreateAndSetRemoteOffer(kSdpStringWithStream1And2);
1912
1913 rtc::scoped_refptr<StreamCollection> reference2(CreateStreamCollection(2));
1914 EXPECT_TRUE(
1915 CompareStreamCollections(observer_.remote_streams(), reference2.get()));
1916}
1917
1918// This test verifies that when remote tracks are added/removed from SDP, the
1919// created remote streams are updated appropriately.
1920TEST_F(PeerConnectionInterfaceTest,
1921 AddRemoveTrackFromExistingRemoteMediaStream) {
1922 FakeConstraints constraints;
1923 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
1924 true);
1925 CreatePeerConnection(&constraints);
kwiberg2bbff992016-03-16 11:03:04 -07001926 rtc::scoped_ptr<SessionDescriptionInterface> desc_ms1 =
1927 CreateSessionDescriptionAndReference(1, 1);
deadbeefab9b2d12015-10-14 11:33:11 -07001928 EXPECT_TRUE(DoSetRemoteDescription(desc_ms1.release()));
1929 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
1930 reference_collection_));
1931
1932 // Add extra audio and video tracks to the same MediaStream.
kwiberg2bbff992016-03-16 11:03:04 -07001933 rtc::scoped_ptr<SessionDescriptionInterface> desc_ms1_two_tracks =
1934 CreateSessionDescriptionAndReference(2, 2);
deadbeefab9b2d12015-10-14 11:33:11 -07001935 EXPECT_TRUE(DoSetRemoteDescription(desc_ms1_two_tracks.release()));
1936 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
1937 reference_collection_));
1938
1939 // Remove the extra audio and video tracks.
kwiberg2bbff992016-03-16 11:03:04 -07001940 rtc::scoped_ptr<SessionDescriptionInterface> desc_ms2 =
1941 CreateSessionDescriptionAndReference(1, 1);
deadbeefab9b2d12015-10-14 11:33:11 -07001942 EXPECT_TRUE(DoSetRemoteDescription(desc_ms2.release()));
1943 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
1944 reference_collection_));
1945}
1946
1947// This tests that remote tracks are ended if a local session description is set
1948// that rejects the media content type.
1949TEST_F(PeerConnectionInterfaceTest, RejectMediaContent) {
1950 FakeConstraints constraints;
1951 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
1952 true);
1953 CreatePeerConnection(&constraints);
1954 // First create and set a remote offer, then reject its video content in our
1955 // answer.
1956 CreateAndSetRemoteOffer(kSdpStringWithStream1);
1957 ASSERT_EQ(1u, observer_.remote_streams()->count());
1958 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
1959 ASSERT_EQ(1u, remote_stream->GetVideoTracks().size());
1960 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
1961
1962 rtc::scoped_refptr<webrtc::VideoTrackInterface> remote_video =
1963 remote_stream->GetVideoTracks()[0];
1964 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, remote_video->state());
1965 rtc::scoped_refptr<webrtc::AudioTrackInterface> remote_audio =
1966 remote_stream->GetAudioTracks()[0];
1967 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, remote_audio->state());
1968
1969 rtc::scoped_ptr<SessionDescriptionInterface> local_answer;
kwiberg2bbff992016-03-16 11:03:04 -07001970 EXPECT_TRUE(DoCreateAnswer(&local_answer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07001971 cricket::ContentInfo* video_info =
1972 local_answer->description()->GetContentByName("video");
1973 video_info->rejected = true;
1974 EXPECT_TRUE(DoSetLocalDescription(local_answer.release()));
1975 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kEnded, remote_video->state());
1976 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, remote_audio->state());
1977
1978 // Now create an offer where we reject both video and audio.
1979 rtc::scoped_ptr<SessionDescriptionInterface> local_offer;
kwiberg2bbff992016-03-16 11:03:04 -07001980 EXPECT_TRUE(DoCreateOffer(&local_offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07001981 video_info = local_offer->description()->GetContentByName("video");
1982 ASSERT_TRUE(video_info != nullptr);
1983 video_info->rejected = true;
1984 cricket::ContentInfo* audio_info =
1985 local_offer->description()->GetContentByName("audio");
1986 ASSERT_TRUE(audio_info != nullptr);
1987 audio_info->rejected = true;
1988 EXPECT_TRUE(DoSetLocalDescription(local_offer.release()));
1989 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kEnded, remote_video->state());
1990 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kEnded, remote_audio->state());
1991}
1992
1993// This tests that we won't crash if the remote track has been removed outside
1994// of PeerConnection and then PeerConnection tries to reject the track.
1995TEST_F(PeerConnectionInterfaceTest, RemoveTrackThenRejectMediaContent) {
1996 FakeConstraints constraints;
1997 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
1998 true);
1999 CreatePeerConnection(&constraints);
2000 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2001 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2002 remote_stream->RemoveTrack(remote_stream->GetVideoTracks()[0]);
2003 remote_stream->RemoveTrack(remote_stream->GetAudioTracks()[0]);
2004
2005 rtc::scoped_ptr<SessionDescriptionInterface> local_answer(
2006 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
2007 kSdpStringWithStream1, nullptr));
2008 cricket::ContentInfo* video_info =
2009 local_answer->description()->GetContentByName("video");
2010 video_info->rejected = true;
2011 cricket::ContentInfo* audio_info =
2012 local_answer->description()->GetContentByName("audio");
2013 audio_info->rejected = true;
2014 EXPECT_TRUE(DoSetLocalDescription(local_answer.release()));
2015
2016 // No crash is a pass.
2017}
2018
deadbeef5e97fb52015-10-15 12:49:08 -07002019// This tests that if a recvonly remote description is set, no remote streams
2020// will be created, even if the description contains SSRCs/MSIDs.
2021// See: https://code.google.com/p/webrtc/issues/detail?id=5054
2022TEST_F(PeerConnectionInterfaceTest, RecvonlyDescriptionDoesntCreateStream) {
2023 FakeConstraints constraints;
2024 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2025 true);
2026 CreatePeerConnection(&constraints);
2027
2028 std::string recvonly_offer = kSdpStringWithStream1;
2029 rtc::replace_substrs(kSendrecv, strlen(kSendrecv), kRecvonly,
2030 strlen(kRecvonly), &recvonly_offer);
2031 CreateAndSetRemoteOffer(recvonly_offer);
2032
2033 EXPECT_EQ(0u, observer_.remote_streams()->count());
2034}
2035
deadbeefab9b2d12015-10-14 11:33:11 -07002036// This tests that a default MediaStream is created if a remote session
2037// description doesn't contain any streams and no MSID support.
2038// It also tests that the default stream is updated if a video m-line is added
2039// in a subsequent session description.
2040TEST_F(PeerConnectionInterfaceTest, SdpWithoutMsidCreatesDefaultStream) {
2041 FakeConstraints constraints;
2042 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2043 true);
2044 CreatePeerConnection(&constraints);
2045 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2046
2047 ASSERT_EQ(1u, observer_.remote_streams()->count());
2048 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2049
2050 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2051 EXPECT_EQ(0u, remote_stream->GetVideoTracks().size());
2052 EXPECT_EQ("default", remote_stream->label());
2053
2054 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2055 ASSERT_EQ(1u, observer_.remote_streams()->count());
2056 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2057 EXPECT_EQ("defaulta0", remote_stream->GetAudioTracks()[0]->id());
deadbeef884f5852016-01-15 09:20:04 -08002058 EXPECT_EQ(MediaStreamTrackInterface::kLive,
2059 remote_stream->GetAudioTracks()[0]->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002060 ASSERT_EQ(1u, remote_stream->GetVideoTracks().size());
2061 EXPECT_EQ("defaultv0", remote_stream->GetVideoTracks()[0]->id());
deadbeef884f5852016-01-15 09:20:04 -08002062 EXPECT_EQ(MediaStreamTrackInterface::kLive,
2063 remote_stream->GetVideoTracks()[0]->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002064}
2065
2066// This tests that a default MediaStream is created if a remote session
2067// description doesn't contain any streams and media direction is send only.
2068TEST_F(PeerConnectionInterfaceTest,
2069 SendOnlySdpWithoutMsidCreatesDefaultStream) {
2070 FakeConstraints constraints;
2071 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2072 true);
2073 CreatePeerConnection(&constraints);
2074 CreateAndSetRemoteOffer(kSdpStringSendOnlyWithoutStreams);
2075
2076 ASSERT_EQ(1u, observer_.remote_streams()->count());
2077 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2078
2079 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2080 EXPECT_EQ(1u, remote_stream->GetVideoTracks().size());
2081 EXPECT_EQ("default", remote_stream->label());
2082}
2083
2084// This tests that it won't crash when PeerConnection tries to remove
2085// a remote track that as already been removed from the MediaStream.
2086TEST_F(PeerConnectionInterfaceTest, RemoveAlreadyGoneRemoteStream) {
2087 FakeConstraints constraints;
2088 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2089 true);
2090 CreatePeerConnection(&constraints);
2091 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2092 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2093 remote_stream->RemoveTrack(remote_stream->GetAudioTracks()[0]);
2094 remote_stream->RemoveTrack(remote_stream->GetVideoTracks()[0]);
2095
2096 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2097
2098 // No crash is a pass.
2099}
2100
2101// This tests that a default MediaStream is created if the remote session
2102// description doesn't contain any streams and don't contain an indication if
2103// MSID is supported.
2104TEST_F(PeerConnectionInterfaceTest,
2105 SdpWithoutMsidAndStreamsCreatesDefaultStream) {
2106 FakeConstraints constraints;
2107 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2108 true);
2109 CreatePeerConnection(&constraints);
2110 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2111
2112 ASSERT_EQ(1u, observer_.remote_streams()->count());
2113 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2114 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2115 EXPECT_EQ(1u, remote_stream->GetVideoTracks().size());
2116}
2117
2118// This tests that a default MediaStream is not created if the remote session
2119// description doesn't contain any streams but does support MSID.
2120TEST_F(PeerConnectionInterfaceTest, SdpWithMsidDontCreatesDefaultStream) {
2121 FakeConstraints constraints;
2122 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2123 true);
2124 CreatePeerConnection(&constraints);
2125 CreateAndSetRemoteOffer(kSdpStringWithMsidWithoutStreams);
2126 EXPECT_EQ(0u, observer_.remote_streams()->count());
2127}
2128
deadbeefbda7e0b2015-12-08 17:13:40 -08002129// This tests that when setting a new description, the old default tracks are
2130// not destroyed and recreated.
2131// See: https://bugs.chromium.org/p/webrtc/issues/detail?id=5250
2132TEST_F(PeerConnectionInterfaceTest, DefaultTracksNotDestroyedAndRecreated) {
2133 FakeConstraints constraints;
2134 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2135 true);
2136 CreatePeerConnection(&constraints);
2137 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2138
2139 ASSERT_EQ(1u, observer_.remote_streams()->count());
2140 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2141 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2142
2143 // Set the track to "disabled", then set a new description and ensure the
2144 // track is still disabled, which ensures it hasn't been recreated.
2145 remote_stream->GetAudioTracks()[0]->set_enabled(false);
2146 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2147 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2148 EXPECT_FALSE(remote_stream->GetAudioTracks()[0]->enabled());
2149}
2150
deadbeefab9b2d12015-10-14 11:33:11 -07002151// This tests that a default MediaStream is not created if a remote session
2152// description is updated to not have any MediaStreams.
2153TEST_F(PeerConnectionInterfaceTest, VerifyDefaultStreamIsNotCreated) {
2154 FakeConstraints constraints;
2155 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2156 true);
2157 CreatePeerConnection(&constraints);
2158 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2159 rtc::scoped_refptr<StreamCollection> reference(CreateStreamCollection(1));
2160 EXPECT_TRUE(
2161 CompareStreamCollections(observer_.remote_streams(), reference.get()));
2162
2163 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2164 EXPECT_EQ(0u, observer_.remote_streams()->count());
2165}
2166
2167// This tests that an RtpSender is created when the local description is set
2168// after adding a local stream.
2169// TODO(deadbeef): This test and the one below it need to be updated when
2170// an RtpSender's lifetime isn't determined by when a local description is set.
2171TEST_F(PeerConnectionInterfaceTest, LocalDescriptionChanged) {
2172 FakeConstraints constraints;
2173 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2174 true);
2175 CreatePeerConnection(&constraints);
2176 // Create an offer just to ensure we have an identity before we manually
2177 // call SetLocalDescription.
2178 rtc::scoped_ptr<SessionDescriptionInterface> throwaway;
kwiberg2bbff992016-03-16 11:03:04 -07002179 ASSERT_TRUE(DoCreateOffer(&throwaway, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07002180
kwiberg2bbff992016-03-16 11:03:04 -07002181 rtc::scoped_ptr<SessionDescriptionInterface> desc_1 =
2182 CreateSessionDescriptionAndReference(2, 2);
deadbeefab9b2d12015-10-14 11:33:11 -07002183
2184 pc_->AddStream(reference_collection_->at(0));
2185 EXPECT_TRUE(DoSetLocalDescription(desc_1.release()));
2186 auto senders = pc_->GetSenders();
2187 EXPECT_EQ(4u, senders.size());
2188 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2189 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2190 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[1]));
2191 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[1]));
2192
2193 // Remove an audio and video track.
deadbeeffac06552015-11-25 11:26:01 -08002194 pc_->RemoveStream(reference_collection_->at(0));
kwiberg2bbff992016-03-16 11:03:04 -07002195 rtc::scoped_ptr<SessionDescriptionInterface> desc_2 =
2196 CreateSessionDescriptionAndReference(1, 1);
deadbeeffac06552015-11-25 11:26:01 -08002197 pc_->AddStream(reference_collection_->at(0));
deadbeefab9b2d12015-10-14 11:33:11 -07002198 EXPECT_TRUE(DoSetLocalDescription(desc_2.release()));
2199 senders = pc_->GetSenders();
2200 EXPECT_EQ(2u, senders.size());
2201 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2202 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2203 EXPECT_FALSE(ContainsSender(senders, kAudioTracks[1]));
2204 EXPECT_FALSE(ContainsSender(senders, kVideoTracks[1]));
2205}
2206
2207// This tests that an RtpSender is created when the local description is set
2208// before adding a local stream.
2209TEST_F(PeerConnectionInterfaceTest,
2210 AddLocalStreamAfterLocalDescriptionChanged) {
2211 FakeConstraints constraints;
2212 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2213 true);
2214 CreatePeerConnection(&constraints);
2215 // Create an offer just to ensure we have an identity before we manually
2216 // call SetLocalDescription.
2217 rtc::scoped_ptr<SessionDescriptionInterface> throwaway;
kwiberg2bbff992016-03-16 11:03:04 -07002218 ASSERT_TRUE(DoCreateOffer(&throwaway, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07002219
kwiberg2bbff992016-03-16 11:03:04 -07002220 rtc::scoped_ptr<SessionDescriptionInterface> desc_1 =
2221 CreateSessionDescriptionAndReference(2, 2);
deadbeefab9b2d12015-10-14 11:33:11 -07002222
2223 EXPECT_TRUE(DoSetLocalDescription(desc_1.release()));
2224 auto senders = pc_->GetSenders();
2225 EXPECT_EQ(0u, senders.size());
2226
2227 pc_->AddStream(reference_collection_->at(0));
2228 senders = pc_->GetSenders();
2229 EXPECT_EQ(4u, senders.size());
2230 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2231 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2232 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[1]));
2233 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[1]));
2234}
2235
2236// This tests that the expected behavior occurs if the SSRC on a local track is
2237// changed when SetLocalDescription is called.
2238TEST_F(PeerConnectionInterfaceTest,
2239 ChangeSsrcOnTrackInLocalSessionDescription) {
2240 FakeConstraints constraints;
2241 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2242 true);
2243 CreatePeerConnection(&constraints);
2244 // Create an offer just to ensure we have an identity before we manually
2245 // call SetLocalDescription.
2246 rtc::scoped_ptr<SessionDescriptionInterface> throwaway;
kwiberg2bbff992016-03-16 11:03:04 -07002247 ASSERT_TRUE(DoCreateOffer(&throwaway, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07002248
kwiberg2bbff992016-03-16 11:03:04 -07002249 rtc::scoped_ptr<SessionDescriptionInterface> desc =
2250 CreateSessionDescriptionAndReference(1, 1);
deadbeefab9b2d12015-10-14 11:33:11 -07002251 std::string sdp;
2252 desc->ToString(&sdp);
2253
2254 pc_->AddStream(reference_collection_->at(0));
2255 EXPECT_TRUE(DoSetLocalDescription(desc.release()));
2256 auto senders = pc_->GetSenders();
2257 EXPECT_EQ(2u, senders.size());
2258 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2259 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2260
2261 // Change the ssrc of the audio and video track.
2262 std::string ssrc_org = "a=ssrc:1";
2263 std::string ssrc_to = "a=ssrc:97";
2264 rtc::replace_substrs(ssrc_org.c_str(), ssrc_org.length(), ssrc_to.c_str(),
2265 ssrc_to.length(), &sdp);
2266 ssrc_org = "a=ssrc:2";
2267 ssrc_to = "a=ssrc:98";
2268 rtc::replace_substrs(ssrc_org.c_str(), ssrc_org.length(), ssrc_to.c_str(),
2269 ssrc_to.length(), &sdp);
2270 rtc::scoped_ptr<SessionDescriptionInterface> updated_desc(
2271 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer, sdp,
2272 nullptr));
2273
2274 EXPECT_TRUE(DoSetLocalDescription(updated_desc.release()));
2275 senders = pc_->GetSenders();
2276 EXPECT_EQ(2u, senders.size());
2277 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2278 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2279 // TODO(deadbeef): Once RtpSenders expose parameters, check that the SSRC
2280 // changed.
2281}
2282
2283// This tests that the expected behavior occurs if a new session description is
2284// set with the same tracks, but on a different MediaStream.
2285TEST_F(PeerConnectionInterfaceTest, SignalSameTracksInSeparateMediaStream) {
2286 FakeConstraints constraints;
2287 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2288 true);
2289 CreatePeerConnection(&constraints);
2290 // Create an offer just to ensure we have an identity before we manually
2291 // call SetLocalDescription.
2292 rtc::scoped_ptr<SessionDescriptionInterface> throwaway;
kwiberg2bbff992016-03-16 11:03:04 -07002293 ASSERT_TRUE(DoCreateOffer(&throwaway, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07002294
kwiberg2bbff992016-03-16 11:03:04 -07002295 rtc::scoped_ptr<SessionDescriptionInterface> desc =
2296 CreateSessionDescriptionAndReference(1, 1);
deadbeefab9b2d12015-10-14 11:33:11 -07002297 std::string sdp;
2298 desc->ToString(&sdp);
2299
2300 pc_->AddStream(reference_collection_->at(0));
2301 EXPECT_TRUE(DoSetLocalDescription(desc.release()));
2302 auto senders = pc_->GetSenders();
2303 EXPECT_EQ(2u, senders.size());
2304 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2305 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2306
2307 // Add a new MediaStream but with the same tracks as in the first stream.
2308 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream_1(
2309 webrtc::MediaStream::Create(kStreams[1]));
2310 stream_1->AddTrack(reference_collection_->at(0)->GetVideoTracks()[0]);
2311 stream_1->AddTrack(reference_collection_->at(0)->GetAudioTracks()[0]);
2312 pc_->AddStream(stream_1);
2313
2314 // Replace msid in the original SDP.
2315 rtc::replace_substrs(kStreams[0], strlen(kStreams[0]), kStreams[1],
2316 strlen(kStreams[1]), &sdp);
2317
2318 rtc::scoped_ptr<SessionDescriptionInterface> updated_desc(
2319 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer, sdp,
2320 nullptr));
2321
2322 EXPECT_TRUE(DoSetLocalDescription(updated_desc.release()));
2323 senders = pc_->GetSenders();
2324 EXPECT_EQ(2u, senders.size());
2325 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2326 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2327}
2328
nisse51542be2016-02-12 02:27:06 -08002329// The PeerConnectionMediaConfig tests below verify that configuration
2330// and constraints are propagated into the MediaConfig passed to
2331// CreateMediaController. These settings are intended for MediaChannel
2332// constructors, but that is not exercised by these unittest.
2333class PeerConnectionFactoryForTest : public webrtc::PeerConnectionFactory {
2334 public:
2335 webrtc::MediaControllerInterface* CreateMediaController(
2336 const cricket::MediaConfig& config) const override {
2337 create_media_controller_called_ = true;
2338 create_media_controller_config_ = config;
2339
2340 webrtc::MediaControllerInterface* mc =
2341 PeerConnectionFactory::CreateMediaController(config);
2342 EXPECT_TRUE(mc != nullptr);
2343 return mc;
2344 }
2345
2346 // Mutable, so they can be modified in the above const-declared method.
2347 mutable bool create_media_controller_called_ = false;
2348 mutable cricket::MediaConfig create_media_controller_config_;
2349};
2350
2351class PeerConnectionMediaConfigTest : public testing::Test {
2352 protected:
2353 void SetUp() override {
2354 pcf_= new rtc::RefCountedObject<PeerConnectionFactoryForTest>();
2355 pcf_->Initialize();
2356 }
2357 const cricket::MediaConfig& TestCreatePeerConnection(
2358 const PeerConnectionInterface::RTCConfiguration& config,
2359 const MediaConstraintsInterface *constraints) {
2360 pcf_->create_media_controller_called_ = false;
2361
2362 scoped_refptr<PeerConnectionInterface> pc(
2363 pcf_->CreatePeerConnection(config, constraints, nullptr, nullptr,
2364 &observer_));
2365 EXPECT_TRUE(pc.get());
2366 EXPECT_TRUE(pcf_->create_media_controller_called_);
2367 return pcf_->create_media_controller_config_;
2368 }
2369
2370 scoped_refptr<PeerConnectionFactoryForTest> pcf_;
2371 MockPeerConnectionObserver observer_;
2372};
2373
2374// This test verifies the default behaviour with no constraints and a
2375// default RTCConfiguration.
2376TEST_F(PeerConnectionMediaConfigTest, TestDefaults) {
2377 PeerConnectionInterface::RTCConfiguration config;
2378 FakeConstraints constraints;
2379
2380 const cricket::MediaConfig& media_config =
2381 TestCreatePeerConnection(config, &constraints);
2382
2383 EXPECT_FALSE(media_config.enable_dscp);
nisse0db023a2016-03-01 04:29:59 -08002384 EXPECT_TRUE(media_config.video.enable_cpu_overuse_detection);
2385 EXPECT_FALSE(media_config.video.disable_prerenderer_smoothing);
2386 EXPECT_FALSE(media_config.video.suspend_below_min_bitrate);
nisse51542be2016-02-12 02:27:06 -08002387}
2388
2389// This test verifies the DSCP constraint is recognized and passed to
2390// the CreateMediaController call.
2391TEST_F(PeerConnectionMediaConfigTest, TestDscpConstraintTrue) {
2392 PeerConnectionInterface::RTCConfiguration config;
2393 FakeConstraints constraints;
2394
2395 constraints.AddOptional(webrtc::MediaConstraintsInterface::kEnableDscp, true);
2396 const cricket::MediaConfig& media_config =
2397 TestCreatePeerConnection(config, &constraints);
2398
2399 EXPECT_TRUE(media_config.enable_dscp);
2400}
2401
2402// This test verifies the cpu overuse detection constraint is
2403// recognized and passed to the CreateMediaController call.
2404TEST_F(PeerConnectionMediaConfigTest, TestCpuOveruseConstraintFalse) {
2405 PeerConnectionInterface::RTCConfiguration config;
2406 FakeConstraints constraints;
2407
2408 constraints.AddOptional(
2409 webrtc::MediaConstraintsInterface::kCpuOveruseDetection, false);
2410 const cricket::MediaConfig media_config =
2411 TestCreatePeerConnection(config, &constraints);
2412
nisse0db023a2016-03-01 04:29:59 -08002413 EXPECT_FALSE(media_config.video.enable_cpu_overuse_detection);
nisse51542be2016-02-12 02:27:06 -08002414}
2415
2416// This test verifies that the disable_prerenderer_smoothing flag is
2417// propagated from RTCConfiguration to the CreateMediaController call.
2418TEST_F(PeerConnectionMediaConfigTest, TestDisablePrerendererSmoothingTrue) {
2419 PeerConnectionInterface::RTCConfiguration config;
2420 FakeConstraints constraints;
2421
2422 config.disable_prerenderer_smoothing = true;
2423 const cricket::MediaConfig& media_config =
2424 TestCreatePeerConnection(config, &constraints);
2425
nisse0db023a2016-03-01 04:29:59 -08002426 EXPECT_TRUE(media_config.video.disable_prerenderer_smoothing);
2427}
2428
2429// This test verifies the suspend below min bitrate constraint is
2430// recognized and passed to the CreateMediaController call.
2431TEST_F(PeerConnectionMediaConfigTest,
2432 TestSuspendBelowMinBitrateConstraintTrue) {
2433 PeerConnectionInterface::RTCConfiguration config;
2434 FakeConstraints constraints;
2435
2436 constraints.AddOptional(
2437 webrtc::MediaConstraintsInterface::kEnableVideoSuspendBelowMinBitrate,
2438 true);
2439 const cricket::MediaConfig media_config =
2440 TestCreatePeerConnection(config, &constraints);
2441
2442 EXPECT_TRUE(media_config.video.suspend_below_min_bitrate);
nisse51542be2016-02-12 02:27:06 -08002443}
2444
deadbeefab9b2d12015-10-14 11:33:11 -07002445// The following tests verify that session options are created correctly.
deadbeefc80741f2015-10-22 13:14:45 -07002446// TODO(deadbeef): Convert these tests to be more end-to-end. Instead of
2447// "verify options are converted correctly", should be "pass options into
2448// CreateOffer and verify the correct offer is produced."
deadbeefab9b2d12015-10-14 11:33:11 -07002449
2450TEST(CreateSessionOptionsTest, GetOptionsForOfferWithInvalidAudioOption) {
2451 RTCOfferAnswerOptions rtc_options;
2452 rtc_options.offer_to_receive_audio = RTCOfferAnswerOptions::kUndefined - 1;
2453
2454 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08002455 EXPECT_FALSE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002456
2457 rtc_options.offer_to_receive_audio =
2458 RTCOfferAnswerOptions::kMaxOfferToReceiveMedia + 1;
htaaac2dea2016-03-10 13:35:55 -08002459 EXPECT_FALSE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002460}
2461
2462TEST(CreateSessionOptionsTest, GetOptionsForOfferWithInvalidVideoOption) {
2463 RTCOfferAnswerOptions rtc_options;
2464 rtc_options.offer_to_receive_video = RTCOfferAnswerOptions::kUndefined - 1;
2465
2466 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08002467 EXPECT_FALSE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002468
2469 rtc_options.offer_to_receive_video =
2470 RTCOfferAnswerOptions::kMaxOfferToReceiveMedia + 1;
htaaac2dea2016-03-10 13:35:55 -08002471 EXPECT_FALSE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002472}
2473
2474// Test that a MediaSessionOptions is created for an offer if
deadbeefc80741f2015-10-22 13:14:45 -07002475// OfferToReceiveAudio and OfferToReceiveVideo options are set.
deadbeefab9b2d12015-10-14 11:33:11 -07002476TEST(CreateSessionOptionsTest, GetMediaSessionOptionsForOfferWithAudioVideo) {
2477 RTCOfferAnswerOptions rtc_options;
2478 rtc_options.offer_to_receive_audio = 1;
2479 rtc_options.offer_to_receive_video = 1;
2480
2481 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08002482 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002483 EXPECT_TRUE(options.has_audio());
2484 EXPECT_TRUE(options.has_video());
2485 EXPECT_TRUE(options.bundle_enabled);
2486}
2487
2488// Test that a correct MediaSessionOptions is created for an offer if
deadbeefc80741f2015-10-22 13:14:45 -07002489// OfferToReceiveAudio is set.
deadbeefab9b2d12015-10-14 11:33:11 -07002490TEST(CreateSessionOptionsTest, GetMediaSessionOptionsForOfferWithAudio) {
2491 RTCOfferAnswerOptions rtc_options;
2492 rtc_options.offer_to_receive_audio = 1;
2493
2494 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08002495 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002496 EXPECT_TRUE(options.has_audio());
2497 EXPECT_FALSE(options.has_video());
2498 EXPECT_TRUE(options.bundle_enabled);
2499}
2500
2501// Test that a correct MediaSessionOptions is created for an offer if
deadbeefc80741f2015-10-22 13:14:45 -07002502// the default OfferOptions are used.
deadbeefab9b2d12015-10-14 11:33:11 -07002503TEST(CreateSessionOptionsTest, GetDefaultMediaSessionOptionsForOffer) {
2504 RTCOfferAnswerOptions rtc_options;
2505
2506 cricket::MediaSessionOptions options;
deadbeef0ed85b22016-02-23 17:24:52 -08002507 options.transport_options["audio"] = cricket::TransportOptions();
2508 options.transport_options["video"] = cricket::TransportOptions();
htaaac2dea2016-03-10 13:35:55 -08002509 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefc80741f2015-10-22 13:14:45 -07002510 EXPECT_TRUE(options.has_audio());
deadbeefab9b2d12015-10-14 11:33:11 -07002511 EXPECT_FALSE(options.has_video());
deadbeefc80741f2015-10-22 13:14:45 -07002512 EXPECT_TRUE(options.bundle_enabled);
deadbeefab9b2d12015-10-14 11:33:11 -07002513 EXPECT_TRUE(options.vad_enabled);
deadbeef0ed85b22016-02-23 17:24:52 -08002514 EXPECT_FALSE(options.transport_options["audio"].ice_restart);
2515 EXPECT_FALSE(options.transport_options["video"].ice_restart);
deadbeefab9b2d12015-10-14 11:33:11 -07002516}
2517
2518// Test that a correct MediaSessionOptions is created for an offer if
deadbeefc80741f2015-10-22 13:14:45 -07002519// OfferToReceiveVideo is set.
deadbeefab9b2d12015-10-14 11:33:11 -07002520TEST(CreateSessionOptionsTest, GetMediaSessionOptionsForOfferWithVideo) {
2521 RTCOfferAnswerOptions rtc_options;
2522 rtc_options.offer_to_receive_audio = 0;
2523 rtc_options.offer_to_receive_video = 1;
2524
2525 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08002526 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002527 EXPECT_FALSE(options.has_audio());
2528 EXPECT_TRUE(options.has_video());
2529 EXPECT_TRUE(options.bundle_enabled);
2530}
2531
2532// Test that a correct MediaSessionOptions is created for an offer if
2533// UseRtpMux is set to false.
2534TEST(CreateSessionOptionsTest,
2535 GetMediaSessionOptionsForOfferWithBundleDisabled) {
2536 RTCOfferAnswerOptions rtc_options;
2537 rtc_options.offer_to_receive_audio = 1;
2538 rtc_options.offer_to_receive_video = 1;
2539 rtc_options.use_rtp_mux = false;
2540
2541 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08002542 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002543 EXPECT_TRUE(options.has_audio());
2544 EXPECT_TRUE(options.has_video());
2545 EXPECT_FALSE(options.bundle_enabled);
2546}
2547
2548// Test that a correct MediaSessionOptions is created to restart ice if
2549// IceRestart is set. It also tests that subsequent MediaSessionOptions don't
Taylor Brandstetterf475d362016-01-08 15:35:57 -08002550// have |audio_transport_options.ice_restart| etc. set.
deadbeefab9b2d12015-10-14 11:33:11 -07002551TEST(CreateSessionOptionsTest, GetMediaSessionOptionsForOfferWithIceRestart) {
2552 RTCOfferAnswerOptions rtc_options;
2553 rtc_options.ice_restart = true;
2554
2555 cricket::MediaSessionOptions options;
deadbeef0ed85b22016-02-23 17:24:52 -08002556 options.transport_options["audio"] = cricket::TransportOptions();
2557 options.transport_options["video"] = cricket::TransportOptions();
htaaac2dea2016-03-10 13:35:55 -08002558 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeef0ed85b22016-02-23 17:24:52 -08002559 EXPECT_TRUE(options.transport_options["audio"].ice_restart);
2560 EXPECT_TRUE(options.transport_options["video"].ice_restart);
deadbeefab9b2d12015-10-14 11:33:11 -07002561
2562 rtc_options = RTCOfferAnswerOptions();
htaaac2dea2016-03-10 13:35:55 -08002563 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeef0ed85b22016-02-23 17:24:52 -08002564 EXPECT_FALSE(options.transport_options["audio"].ice_restart);
2565 EXPECT_FALSE(options.transport_options["video"].ice_restart);
deadbeefab9b2d12015-10-14 11:33:11 -07002566}
2567
2568// Test that the MediaConstraints in an answer don't affect if audio and video
2569// is offered in an offer but that if kOfferToReceiveAudio or
2570// kOfferToReceiveVideo constraints are true in an offer, the media type will be
2571// included in subsequent answers.
2572TEST(CreateSessionOptionsTest, MediaConstraintsInAnswer) {
2573 FakeConstraints answer_c;
2574 answer_c.SetMandatoryReceiveAudio(true);
2575 answer_c.SetMandatoryReceiveVideo(true);
2576
2577 cricket::MediaSessionOptions answer_options;
2578 EXPECT_TRUE(ParseConstraintsForAnswer(&answer_c, &answer_options));
2579 EXPECT_TRUE(answer_options.has_audio());
2580 EXPECT_TRUE(answer_options.has_video());
2581
deadbeefc80741f2015-10-22 13:14:45 -07002582 RTCOfferAnswerOptions rtc_offer_options;
deadbeefab9b2d12015-10-14 11:33:11 -07002583
2584 cricket::MediaSessionOptions offer_options;
htaaac2dea2016-03-10 13:35:55 -08002585 EXPECT_TRUE(
2586 ExtractMediaSessionOptions(rtc_offer_options, false, &offer_options));
deadbeefc80741f2015-10-22 13:14:45 -07002587 EXPECT_TRUE(offer_options.has_audio());
htaaac2dea2016-03-10 13:35:55 -08002588 EXPECT_TRUE(offer_options.has_video());
deadbeefab9b2d12015-10-14 11:33:11 -07002589
deadbeefc80741f2015-10-22 13:14:45 -07002590 RTCOfferAnswerOptions updated_rtc_offer_options;
2591 updated_rtc_offer_options.offer_to_receive_audio = 1;
2592 updated_rtc_offer_options.offer_to_receive_video = 1;
deadbeefab9b2d12015-10-14 11:33:11 -07002593
2594 cricket::MediaSessionOptions updated_offer_options;
htaaac2dea2016-03-10 13:35:55 -08002595 EXPECT_TRUE(ExtractMediaSessionOptions(updated_rtc_offer_options, false,
htaa2a49d92016-03-04 02:51:39 -08002596 &updated_offer_options));
deadbeefab9b2d12015-10-14 11:33:11 -07002597 EXPECT_TRUE(updated_offer_options.has_audio());
2598 EXPECT_TRUE(updated_offer_options.has_video());
2599
2600 // Since an offer has been created with both audio and video, subsequent
2601 // offers and answers should contain both audio and video.
2602 // Answers will only contain the media types that exist in the offer
2603 // regardless of the value of |updated_answer_options.has_audio| and
2604 // |updated_answer_options.has_video|.
2605 FakeConstraints updated_answer_c;
2606 answer_c.SetMandatoryReceiveAudio(false);
2607 answer_c.SetMandatoryReceiveVideo(false);
2608
2609 cricket::MediaSessionOptions updated_answer_options;
2610 EXPECT_TRUE(
2611 ParseConstraintsForAnswer(&updated_answer_c, &updated_answer_options));
2612 EXPECT_TRUE(updated_answer_options.has_audio());
2613 EXPECT_TRUE(updated_answer_options.has_video());
deadbeefab9b2d12015-10-14 11:33:11 -07002614}