blob: 04485267536ad913eff7c2ac5a7f41e16910e664 [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
deadbeefc80741f2015-10-22 13:14:45 -0700640 bool DoCreateOfferAnswer(SessionDescriptionInterface** desc,
641 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);
652 *desc = observer->release_desc();
653 return observer->result();
654 }
655
deadbeefc80741f2015-10-22 13:14:45 -0700656 bool DoCreateOffer(SessionDescriptionInterface** desc,
657 MediaConstraintsInterface* constraints) {
658 return DoCreateOfferAnswer(desc, true, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000659 }
660
deadbeefc80741f2015-10-22 13:14:45 -0700661 bool DoCreateAnswer(SessionDescriptionInterface** desc,
662 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;
deadbeefc80741f2015-10-22 13:14:45 -0700723 ASSERT_TRUE(DoCreateOffer(offer.use(), 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;
deadbeefc80741f2015-10-22 13:14:45 -0700743 ASSERT_TRUE(DoCreateAnswer(answer.use(), 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;
deadbeefc80741f2015-10-22 13:14:45 -0700763 ASSERT_TRUE(DoCreateAnswer(answer.use(), 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;
deadbeefc80741f2015-10-22 13:14:45 -0700783 ASSERT_TRUE(DoCreateOffer(offer.use(), 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
850 // is returned in |desc| and the MediaStream is stored in
851 // |reference_collection_|
852 void CreateSessionDescriptionAndReference(
853 size_t number_of_audio_tracks,
854 size_t number_of_video_tracks,
855 SessionDescriptionInterface** desc) {
856 ASSERT_TRUE(desc != nullptr);
857 ASSERT_LE(number_of_audio_tracks, 2u);
858 ASSERT_LE(number_of_video_tracks, 2u);
859
860 reference_collection_ = StreamCollection::Create();
861 std::string sdp_ms1 = std::string(kSdpStringInit);
862
863 std::string mediastream_label = kStreams[0];
864
865 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream(
866 webrtc::MediaStream::Create(mediastream_label));
867 reference_collection_->AddStream(stream);
868
869 if (number_of_audio_tracks > 0) {
870 sdp_ms1 += std::string(kSdpStringAudio);
871 sdp_ms1 += std::string(kSdpStringMs1Audio0);
872 AddAudioTrack(kAudioTracks[0], stream);
873 }
874 if (number_of_audio_tracks > 1) {
875 sdp_ms1 += kSdpStringMs1Audio1;
876 AddAudioTrack(kAudioTracks[1], stream);
877 }
878
879 if (number_of_video_tracks > 0) {
880 sdp_ms1 += std::string(kSdpStringVideo);
881 sdp_ms1 += std::string(kSdpStringMs1Video0);
882 AddVideoTrack(kVideoTracks[0], stream);
883 }
884 if (number_of_video_tracks > 1) {
885 sdp_ms1 += kSdpStringMs1Video1;
886 AddVideoTrack(kVideoTracks[1], stream);
887 }
888
889 *desc = webrtc::CreateSessionDescription(
890 SessionDescriptionInterface::kOffer, sdp_ms1, nullptr);
891 }
892
893 void AddAudioTrack(const std::string& track_id,
894 MediaStreamInterface* stream) {
895 rtc::scoped_refptr<webrtc::AudioTrackInterface> audio_track(
896 webrtc::AudioTrack::Create(track_id, nullptr));
897 ASSERT_TRUE(stream->AddTrack(audio_track));
898 }
899
900 void AddVideoTrack(const std::string& track_id,
901 MediaStreamInterface* stream) {
902 rtc::scoped_refptr<webrtc::VideoTrackInterface> video_track(
903 webrtc::VideoTrack::Create(track_id, nullptr));
904 ASSERT_TRUE(stream->AddTrack(video_track));
905 }
906
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800907 cricket::FakePortAllocator* port_allocator_ = nullptr;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000908 scoped_refptr<webrtc::PeerConnectionFactoryInterface> pc_factory_;
909 scoped_refptr<PeerConnectionInterface> pc_;
910 MockPeerConnectionObserver observer_;
deadbeefab9b2d12015-10-14 11:33:11 -0700911 rtc::scoped_refptr<StreamCollection> reference_collection_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000912};
913
914TEST_F(PeerConnectionInterfaceTest,
915 CreatePeerConnectionWithDifferentConfigurations) {
916 CreatePeerConnectionWithDifferentConfigurations();
917}
918
919TEST_F(PeerConnectionInterfaceTest, AddStreams) {
920 CreatePeerConnection();
deadbeefab9b2d12015-10-14 11:33:11 -0700921 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000922 AddVoiceStream(kStreamLabel2);
923 ASSERT_EQ(2u, pc_->local_streams()->count());
924
wu@webrtc.org9dba5252013-08-05 20:36:57 +0000925 // Test we can add multiple local streams to one peerconnection.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000926 scoped_refptr<MediaStreamInterface> stream(
927 pc_factory_->CreateLocalMediaStream(kStreamLabel3));
928 scoped_refptr<AudioTrackInterface> audio_track(
929 pc_factory_->CreateAudioTrack(
930 kStreamLabel3, static_cast<AudioSourceInterface*>(NULL)));
931 stream->AddTrack(audio_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +0000932 EXPECT_TRUE(pc_->AddStream(stream));
wu@webrtc.org9dba5252013-08-05 20:36:57 +0000933 EXPECT_EQ(3u, pc_->local_streams()->count());
934
935 // Remove the third stream.
936 pc_->RemoveStream(pc_->local_streams()->at(2));
937 EXPECT_EQ(2u, pc_->local_streams()->count());
938
939 // Remove the second stream.
940 pc_->RemoveStream(pc_->local_streams()->at(1));
941 EXPECT_EQ(1u, pc_->local_streams()->count());
942
943 // Remove the first stream.
944 pc_->RemoveStream(pc_->local_streams()->at(0));
945 EXPECT_EQ(0u, pc_->local_streams()->count());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000946}
947
deadbeefab9b2d12015-10-14 11:33:11 -0700948// Test that the created offer includes streams we added.
949TEST_F(PeerConnectionInterfaceTest, AddedStreamsPresentInOffer) {
950 CreatePeerConnection();
951 AddAudioVideoStream(kStreamLabel1, "audio_track", "video_track");
952 scoped_ptr<SessionDescriptionInterface> offer;
deadbeefc80741f2015-10-22 13:14:45 -0700953 ASSERT_TRUE(DoCreateOffer(offer.accept(), nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -0700954
955 const cricket::ContentInfo* audio_content =
956 cricket::GetFirstAudioContent(offer->description());
957 const cricket::AudioContentDescription* audio_desc =
958 static_cast<const cricket::AudioContentDescription*>(
959 audio_content->description);
960 EXPECT_TRUE(
961 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
962
963 const cricket::ContentInfo* video_content =
964 cricket::GetFirstVideoContent(offer->description());
965 const cricket::VideoContentDescription* video_desc =
966 static_cast<const cricket::VideoContentDescription*>(
967 video_content->description);
968 EXPECT_TRUE(
969 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
970
971 // Add another stream and ensure the offer includes both the old and new
972 // streams.
973 AddAudioVideoStream(kStreamLabel2, "audio_track2", "video_track2");
deadbeefc80741f2015-10-22 13:14:45 -0700974 ASSERT_TRUE(DoCreateOffer(offer.accept(), nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -0700975
976 audio_content = cricket::GetFirstAudioContent(offer->description());
977 audio_desc = static_cast<const cricket::AudioContentDescription*>(
978 audio_content->description);
979 EXPECT_TRUE(
980 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
981 EXPECT_TRUE(
982 ContainsTrack(audio_desc->streams(), kStreamLabel2, "audio_track2"));
983
984 video_content = cricket::GetFirstVideoContent(offer->description());
985 video_desc = static_cast<const cricket::VideoContentDescription*>(
986 video_content->description);
987 EXPECT_TRUE(
988 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
989 EXPECT_TRUE(
990 ContainsTrack(video_desc->streams(), kStreamLabel2, "video_track2"));
991}
992
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000993TEST_F(PeerConnectionInterfaceTest, RemoveStream) {
994 CreatePeerConnection();
deadbeefab9b2d12015-10-14 11:33:11 -0700995 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000996 ASSERT_EQ(1u, pc_->local_streams()->count());
997 pc_->RemoveStream(pc_->local_streams()->at(0));
998 EXPECT_EQ(0u, pc_->local_streams()->count());
999}
1000
deadbeefe1f9d832016-01-14 15:35:42 -08001001// Test for AddTrack and RemoveTrack methods.
1002// Tests that the created offer includes tracks we added,
1003// and that the RtpSenders are created correctly.
1004// Also tests that RemoveTrack removes the tracks from subsequent offers.
1005TEST_F(PeerConnectionInterfaceTest, AddTrackRemoveTrack) {
1006 CreatePeerConnection();
1007 // Create a dummy stream, so tracks share a stream label.
1008 scoped_refptr<MediaStreamInterface> stream(
1009 pc_factory_->CreateLocalMediaStream(kStreamLabel1));
1010 std::vector<MediaStreamInterface*> stream_list;
1011 stream_list.push_back(stream.get());
1012 scoped_refptr<AudioTrackInterface> audio_track(
1013 pc_factory_->CreateAudioTrack("audio_track", nullptr));
1014 scoped_refptr<VideoTrackInterface> video_track(
1015 pc_factory_->CreateVideoTrack("video_track", nullptr));
1016 auto audio_sender = pc_->AddTrack(audio_track, stream_list);
1017 auto video_sender = pc_->AddTrack(video_track, stream_list);
1018 EXPECT_EQ(kStreamLabel1, audio_sender->stream_id());
1019 EXPECT_EQ("audio_track", audio_sender->id());
1020 EXPECT_EQ(audio_track, audio_sender->track());
1021 EXPECT_EQ(kStreamLabel1, video_sender->stream_id());
1022 EXPECT_EQ("video_track", video_sender->id());
1023 EXPECT_EQ(video_track, video_sender->track());
1024
1025 // Now create an offer and check for the senders.
1026 scoped_ptr<SessionDescriptionInterface> offer;
1027 ASSERT_TRUE(DoCreateOffer(offer.accept(), nullptr));
1028
1029 const cricket::ContentInfo* audio_content =
1030 cricket::GetFirstAudioContent(offer->description());
1031 const cricket::AudioContentDescription* audio_desc =
1032 static_cast<const cricket::AudioContentDescription*>(
1033 audio_content->description);
1034 EXPECT_TRUE(
1035 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
1036
1037 const cricket::ContentInfo* video_content =
1038 cricket::GetFirstVideoContent(offer->description());
1039 const cricket::VideoContentDescription* video_desc =
1040 static_cast<const cricket::VideoContentDescription*>(
1041 video_content->description);
1042 EXPECT_TRUE(
1043 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1044
1045 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
1046
1047 // Now try removing the tracks.
1048 EXPECT_TRUE(pc_->RemoveTrack(audio_sender));
1049 EXPECT_TRUE(pc_->RemoveTrack(video_sender));
1050
1051 // Create a new offer and ensure it doesn't contain the removed senders.
1052 ASSERT_TRUE(DoCreateOffer(offer.accept(), nullptr));
1053
1054 audio_content = cricket::GetFirstAudioContent(offer->description());
1055 audio_desc = static_cast<const cricket::AudioContentDescription*>(
1056 audio_content->description);
1057 EXPECT_FALSE(
1058 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
1059
1060 video_content = cricket::GetFirstVideoContent(offer->description());
1061 video_desc = static_cast<const cricket::VideoContentDescription*>(
1062 video_content->description);
1063 EXPECT_FALSE(
1064 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1065
1066 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
1067
1068 // Calling RemoveTrack on a sender no longer attached to a PeerConnection
1069 // should return false.
1070 EXPECT_FALSE(pc_->RemoveTrack(audio_sender));
1071 EXPECT_FALSE(pc_->RemoveTrack(video_sender));
1072}
1073
1074// Test creating senders without a stream specified,
1075// expecting a random stream ID to be generated.
1076TEST_F(PeerConnectionInterfaceTest, AddTrackWithoutStream) {
1077 CreatePeerConnection();
1078 // Create a dummy stream, so tracks share a stream label.
1079 scoped_refptr<AudioTrackInterface> audio_track(
1080 pc_factory_->CreateAudioTrack("audio_track", nullptr));
1081 scoped_refptr<VideoTrackInterface> video_track(
1082 pc_factory_->CreateVideoTrack("video_track", nullptr));
1083 auto audio_sender =
1084 pc_->AddTrack(audio_track, std::vector<MediaStreamInterface*>());
1085 auto video_sender =
1086 pc_->AddTrack(video_track, std::vector<MediaStreamInterface*>());
1087 EXPECT_EQ("audio_track", audio_sender->id());
1088 EXPECT_EQ(audio_track, audio_sender->track());
1089 EXPECT_EQ("video_track", video_sender->id());
1090 EXPECT_EQ(video_track, video_sender->track());
1091 // If the ID is truly a random GUID, it should be infinitely unlikely they
1092 // will be the same.
1093 EXPECT_NE(video_sender->stream_id(), audio_sender->stream_id());
1094}
1095
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001096TEST_F(PeerConnectionInterfaceTest, CreateOfferReceiveAnswer) {
1097 InitiateCall();
1098 WaitAndVerifyOnAddStream(kStreamLabel1);
1099 VerifyRemoteRtpHeaderExtensions();
1100}
1101
1102TEST_F(PeerConnectionInterfaceTest, CreateOfferReceivePrAnswerAndAnswer) {
1103 CreatePeerConnection();
deadbeefab9b2d12015-10-14 11:33:11 -07001104 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001105 CreateOfferAsLocalDescription();
1106 std::string offer;
1107 EXPECT_TRUE(pc_->local_description()->ToString(&offer));
1108 CreatePrAnswerAndAnswerAsRemoteDescription(offer);
1109 WaitAndVerifyOnAddStream(kStreamLabel1);
1110}
1111
1112TEST_F(PeerConnectionInterfaceTest, ReceiveOfferCreateAnswer) {
1113 CreatePeerConnection();
deadbeefab9b2d12015-10-14 11:33:11 -07001114 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001115
1116 CreateOfferAsRemoteDescription();
1117 CreateAnswerAsLocalDescription();
1118
1119 WaitAndVerifyOnAddStream(kStreamLabel1);
1120}
1121
1122TEST_F(PeerConnectionInterfaceTest, ReceiveOfferCreatePrAnswerAndAnswer) {
1123 CreatePeerConnection();
deadbeefab9b2d12015-10-14 11:33:11 -07001124 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001125
1126 CreateOfferAsRemoteDescription();
1127 CreatePrAnswerAsLocalDescription();
1128 CreateAnswerAsLocalDescription();
1129
1130 WaitAndVerifyOnAddStream(kStreamLabel1);
1131}
1132
1133TEST_F(PeerConnectionInterfaceTest, Renegotiate) {
1134 InitiateCall();
1135 ASSERT_EQ(1u, pc_->remote_streams()->count());
1136 pc_->RemoveStream(pc_->local_streams()->at(0));
1137 CreateOfferReceiveAnswer();
1138 EXPECT_EQ(0u, pc_->remote_streams()->count());
deadbeefab9b2d12015-10-14 11:33:11 -07001139 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001140 CreateOfferReceiveAnswer();
1141}
1142
1143// Tests that after negotiating an audio only call, the respondent can perform a
1144// renegotiation that removes the audio stream.
1145TEST_F(PeerConnectionInterfaceTest, RenegotiateAudioOnly) {
1146 CreatePeerConnection();
1147 AddVoiceStream(kStreamLabel1);
1148 CreateOfferAsRemoteDescription();
1149 CreateAnswerAsLocalDescription();
1150
1151 ASSERT_EQ(1u, pc_->remote_streams()->count());
1152 pc_->RemoveStream(pc_->local_streams()->at(0));
1153 CreateOfferReceiveAnswer();
1154 EXPECT_EQ(0u, pc_->remote_streams()->count());
1155}
1156
1157// Test that candidates are generated and that we can parse our own candidates.
1158TEST_F(PeerConnectionInterfaceTest, IceCandidates) {
1159 CreatePeerConnection();
1160
1161 EXPECT_FALSE(pc_->AddIceCandidate(observer_.last_candidate_.get()));
1162 // SetRemoteDescription takes ownership of offer.
1163 SessionDescriptionInterface* offer = NULL;
deadbeefab9b2d12015-10-14 11:33:11 -07001164 AddVideoStream(kStreamLabel1);
deadbeefc80741f2015-10-22 13:14:45 -07001165 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001166 EXPECT_TRUE(DoSetRemoteDescription(offer));
1167
1168 // SetLocalDescription takes ownership of answer.
1169 SessionDescriptionInterface* answer = NULL;
deadbeefc80741f2015-10-22 13:14:45 -07001170 EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001171 EXPECT_TRUE(DoSetLocalDescription(answer));
1172
1173 EXPECT_TRUE_WAIT(observer_.last_candidate_.get() != NULL, kTimeout);
1174 EXPECT_TRUE_WAIT(observer_.ice_complete_, kTimeout);
1175
1176 EXPECT_TRUE(pc_->AddIceCandidate(observer_.last_candidate_.get()));
1177}
1178
deadbeefab9b2d12015-10-14 11:33:11 -07001179// Test that CreateOffer and CreateAnswer will fail if the track labels are
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001180// not unique.
1181TEST_F(PeerConnectionInterfaceTest, CreateOfferAnswerWithInvalidStream) {
1182 CreatePeerConnection();
1183 // Create a regular offer for the CreateAnswer test later.
1184 SessionDescriptionInterface* offer = NULL;
deadbeefc80741f2015-10-22 13:14:45 -07001185 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001186 EXPECT_TRUE(offer != NULL);
1187 delete offer;
1188 offer = NULL;
1189
1190 // Create a local stream with audio&video tracks having same label.
1191 AddAudioVideoStream(kStreamLabel1, "track_label", "track_label");
1192
1193 // Test CreateOffer
deadbeefc80741f2015-10-22 13:14:45 -07001194 EXPECT_FALSE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001195
1196 // Test CreateAnswer
1197 SessionDescriptionInterface* answer = NULL;
deadbeefc80741f2015-10-22 13:14:45 -07001198 EXPECT_FALSE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001199}
1200
1201// Test that we will get different SSRCs for each tracks in the offer and answer
1202// we created.
1203TEST_F(PeerConnectionInterfaceTest, SsrcInOfferAnswer) {
1204 CreatePeerConnection();
1205 // Create a local stream with audio&video tracks having different labels.
1206 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
1207
1208 // Test CreateOffer
1209 scoped_ptr<SessionDescriptionInterface> offer;
deadbeefc80741f2015-10-22 13:14:45 -07001210 ASSERT_TRUE(DoCreateOffer(offer.use(), nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001211 int audio_ssrc = 0;
1212 int video_ssrc = 0;
1213 EXPECT_TRUE(GetFirstSsrc(GetFirstAudioContent(offer->description()),
1214 &audio_ssrc));
1215 EXPECT_TRUE(GetFirstSsrc(GetFirstVideoContent(offer->description()),
1216 &video_ssrc));
1217 EXPECT_NE(audio_ssrc, video_ssrc);
1218
1219 // Test CreateAnswer
1220 EXPECT_TRUE(DoSetRemoteDescription(offer.release()));
1221 scoped_ptr<SessionDescriptionInterface> answer;
deadbeefc80741f2015-10-22 13:14:45 -07001222 ASSERT_TRUE(DoCreateAnswer(answer.use(), nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001223 audio_ssrc = 0;
1224 video_ssrc = 0;
1225 EXPECT_TRUE(GetFirstSsrc(GetFirstAudioContent(answer->description()),
1226 &audio_ssrc));
1227 EXPECT_TRUE(GetFirstSsrc(GetFirstVideoContent(answer->description()),
1228 &video_ssrc));
1229 EXPECT_NE(audio_ssrc, video_ssrc);
1230}
1231
deadbeefeb459812015-12-15 19:24:43 -08001232// Test that it's possible to call AddTrack on a MediaStream after adding
1233// the stream to a PeerConnection.
1234// TODO(deadbeef): Remove this test once this behavior is no longer supported.
1235TEST_F(PeerConnectionInterfaceTest, AddTrackAfterAddStream) {
1236 CreatePeerConnection();
1237 // Create audio stream and add to PeerConnection.
1238 AddVoiceStream(kStreamLabel1);
1239 MediaStreamInterface* stream = pc_->local_streams()->at(0);
1240
1241 // Add video track to the audio-only stream.
1242 scoped_refptr<VideoTrackInterface> video_track(
1243 pc_factory_->CreateVideoTrack("video_label", nullptr));
1244 stream->AddTrack(video_track.get());
1245
1246 scoped_ptr<SessionDescriptionInterface> offer;
1247 ASSERT_TRUE(DoCreateOffer(offer.use(), nullptr));
1248
1249 const cricket::MediaContentDescription* video_desc =
1250 cricket::GetFirstVideoContentDescription(offer->description());
1251 EXPECT_TRUE(video_desc != nullptr);
1252}
1253
1254// Test that it's possible to call RemoveTrack on a MediaStream after adding
1255// the stream to a PeerConnection.
1256// TODO(deadbeef): Remove this test once this behavior is no longer supported.
1257TEST_F(PeerConnectionInterfaceTest, RemoveTrackAfterAddStream) {
1258 CreatePeerConnection();
1259 // Create audio/video stream and add to PeerConnection.
1260 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
1261 MediaStreamInterface* stream = pc_->local_streams()->at(0);
1262
1263 // Remove the video track.
1264 stream->RemoveTrack(stream->GetVideoTracks()[0]);
1265
1266 scoped_ptr<SessionDescriptionInterface> offer;
1267 ASSERT_TRUE(DoCreateOffer(offer.use(), nullptr));
1268
1269 const cricket::MediaContentDescription* video_desc =
1270 cricket::GetFirstVideoContentDescription(offer->description());
1271 EXPECT_TRUE(video_desc == nullptr);
1272}
1273
deadbeefbd7d8f72015-12-18 16:58:44 -08001274// Test creating a sender with a stream ID, and ensure the ID is populated
1275// in the offer.
1276TEST_F(PeerConnectionInterfaceTest, CreateSenderWithStream) {
1277 CreatePeerConnection();
1278 pc_->CreateSender("video", kStreamLabel1);
1279
1280 scoped_ptr<SessionDescriptionInterface> offer;
1281 ASSERT_TRUE(DoCreateOffer(offer.use(), nullptr));
1282
1283 const cricket::MediaContentDescription* video_desc =
1284 cricket::GetFirstVideoContentDescription(offer->description());
1285 ASSERT_TRUE(video_desc != nullptr);
1286 ASSERT_EQ(1u, video_desc->streams().size());
1287 EXPECT_EQ(kStreamLabel1, video_desc->streams()[0].sync_label);
1288}
1289
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001290// Test that we can specify a certain track that we want statistics about.
1291TEST_F(PeerConnectionInterfaceTest, GetStatsForSpecificTrack) {
1292 InitiateCall();
1293 ASSERT_LT(0u, pc_->remote_streams()->count());
1294 ASSERT_LT(0u, pc_->remote_streams()->at(0)->GetAudioTracks().size());
1295 scoped_refptr<MediaStreamTrackInterface> remote_audio =
1296 pc_->remote_streams()->at(0)->GetAudioTracks()[0];
1297 EXPECT_TRUE(DoGetStats(remote_audio));
1298
1299 // Remove the stream. Since we are sending to our selves the local
1300 // and the remote stream is the same.
1301 pc_->RemoveStream(pc_->local_streams()->at(0));
1302 // Do a re-negotiation.
1303 CreateOfferReceiveAnswer();
1304
1305 ASSERT_EQ(0u, pc_->remote_streams()->count());
1306
1307 // Test that we still can get statistics for the old track. Even if it is not
1308 // sent any longer.
1309 EXPECT_TRUE(DoGetStats(remote_audio));
1310}
1311
1312// Test that we can get stats on a video track.
1313TEST_F(PeerConnectionInterfaceTest, GetStatsForVideoTrack) {
1314 InitiateCall();
1315 ASSERT_LT(0u, pc_->remote_streams()->count());
1316 ASSERT_LT(0u, pc_->remote_streams()->at(0)->GetVideoTracks().size());
1317 scoped_refptr<MediaStreamTrackInterface> remote_video =
1318 pc_->remote_streams()->at(0)->GetVideoTracks()[0];
1319 EXPECT_TRUE(DoGetStats(remote_video));
1320}
1321
1322// Test that we don't get statistics for an invalid track.
tommi@webrtc.org908f57e2014-07-21 11:44:39 +00001323// TODO(tommi): Fix this test. DoGetStats will return true
1324// for the unknown track (since GetStats is async), but no
1325// data is returned for the track.
1326TEST_F(PeerConnectionInterfaceTest, DISABLED_GetStatsForInvalidTrack) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001327 InitiateCall();
1328 scoped_refptr<AudioTrackInterface> unknown_audio_track(
1329 pc_factory_->CreateAudioTrack("unknown track", NULL));
1330 EXPECT_FALSE(DoGetStats(unknown_audio_track));
1331}
1332
1333// This test setup two RTP data channels in loop back.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001334TEST_F(PeerConnectionInterfaceTest, TestDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001335 FakeConstraints constraints;
1336 constraints.SetAllowRtpDataChannels();
1337 CreatePeerConnection(&constraints);
1338 scoped_refptr<DataChannelInterface> data1 =
1339 pc_->CreateDataChannel("test1", NULL);
1340 scoped_refptr<DataChannelInterface> data2 =
1341 pc_->CreateDataChannel("test2", NULL);
1342 ASSERT_TRUE(data1 != NULL);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001343 rtc::scoped_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001344 new MockDataChannelObserver(data1));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001345 rtc::scoped_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001346 new MockDataChannelObserver(data2));
1347
1348 EXPECT_EQ(DataChannelInterface::kConnecting, data1->state());
1349 EXPECT_EQ(DataChannelInterface::kConnecting, data2->state());
1350 std::string data_to_send1 = "testing testing";
1351 std::string data_to_send2 = "testing something else";
1352 EXPECT_FALSE(data1->Send(DataBuffer(data_to_send1)));
1353
1354 CreateOfferReceiveAnswer();
1355 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1356 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
1357
1358 EXPECT_EQ(DataChannelInterface::kOpen, data1->state());
1359 EXPECT_EQ(DataChannelInterface::kOpen, data2->state());
1360 EXPECT_TRUE(data1->Send(DataBuffer(data_to_send1)));
1361 EXPECT_TRUE(data2->Send(DataBuffer(data_to_send2)));
1362
1363 EXPECT_EQ_WAIT(data_to_send1, observer1->last_message(), kTimeout);
1364 EXPECT_EQ_WAIT(data_to_send2, observer2->last_message(), kTimeout);
1365
1366 data1->Close();
1367 EXPECT_EQ(DataChannelInterface::kClosing, data1->state());
1368 CreateOfferReceiveAnswer();
1369 EXPECT_FALSE(observer1->IsOpen());
1370 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
1371 EXPECT_TRUE(observer2->IsOpen());
1372
1373 data_to_send2 = "testing something else again";
1374 EXPECT_TRUE(data2->Send(DataBuffer(data_to_send2)));
1375
1376 EXPECT_EQ_WAIT(data_to_send2, observer2->last_message(), kTimeout);
1377}
1378
1379// This test verifies that sendnig binary data over RTP data channels should
1380// fail.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001381TEST_F(PeerConnectionInterfaceTest, TestSendBinaryOnRtpDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001382 FakeConstraints constraints;
1383 constraints.SetAllowRtpDataChannels();
1384 CreatePeerConnection(&constraints);
1385 scoped_refptr<DataChannelInterface> data1 =
1386 pc_->CreateDataChannel("test1", NULL);
1387 scoped_refptr<DataChannelInterface> data2 =
1388 pc_->CreateDataChannel("test2", NULL);
1389 ASSERT_TRUE(data1 != NULL);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001390 rtc::scoped_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001391 new MockDataChannelObserver(data1));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001392 rtc::scoped_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001393 new MockDataChannelObserver(data2));
1394
1395 EXPECT_EQ(DataChannelInterface::kConnecting, data1->state());
1396 EXPECT_EQ(DataChannelInterface::kConnecting, data2->state());
1397
1398 CreateOfferReceiveAnswer();
1399 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1400 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
1401
1402 EXPECT_EQ(DataChannelInterface::kOpen, data1->state());
1403 EXPECT_EQ(DataChannelInterface::kOpen, data2->state());
1404
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001405 rtc::Buffer buffer("test", 4);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001406 EXPECT_FALSE(data1->Send(DataBuffer(buffer, true)));
1407}
1408
1409// This test setup a RTP data channels in loop back and test that a channel is
1410// opened even if the remote end answer with a zero SSRC.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001411TEST_F(PeerConnectionInterfaceTest, TestSendOnlyDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001412 FakeConstraints constraints;
1413 constraints.SetAllowRtpDataChannels();
1414 CreatePeerConnection(&constraints);
1415 scoped_refptr<DataChannelInterface> data1 =
1416 pc_->CreateDataChannel("test1", NULL);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001417 rtc::scoped_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001418 new MockDataChannelObserver(data1));
1419
1420 CreateOfferReceiveAnswerWithoutSsrc();
1421
1422 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1423
1424 data1->Close();
1425 EXPECT_EQ(DataChannelInterface::kClosing, data1->state());
1426 CreateOfferReceiveAnswerWithoutSsrc();
1427 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
1428 EXPECT_FALSE(observer1->IsOpen());
1429}
1430
1431// This test that if a data channel is added in an answer a receive only channel
1432// channel is created.
1433TEST_F(PeerConnectionInterfaceTest, TestReceiveOnlyDataChannel) {
1434 FakeConstraints constraints;
1435 constraints.SetAllowRtpDataChannels();
1436 CreatePeerConnection(&constraints);
1437
1438 std::string offer_label = "offer_channel";
1439 scoped_refptr<DataChannelInterface> offer_channel =
1440 pc_->CreateDataChannel(offer_label, NULL);
1441
1442 CreateOfferAsLocalDescription();
1443
1444 // Replace the data channel label in the offer and apply it as an answer.
1445 std::string receive_label = "answer_channel";
1446 std::string sdp;
1447 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001448 rtc::replace_substrs(offer_label.c_str(), offer_label.length(),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001449 receive_label.c_str(), receive_label.length(),
1450 &sdp);
1451 CreateAnswerAsRemoteDescription(sdp);
1452
1453 // Verify that a new incoming data channel has been created and that
1454 // it is open but can't we written to.
1455 ASSERT_TRUE(observer_.last_datachannel_ != NULL);
1456 DataChannelInterface* received_channel = observer_.last_datachannel_;
1457 EXPECT_EQ(DataChannelInterface::kConnecting, received_channel->state());
1458 EXPECT_EQ(receive_label, received_channel->label());
1459 EXPECT_FALSE(received_channel->Send(DataBuffer("something")));
1460
1461 // Verify that the channel we initially offered has been rejected.
1462 EXPECT_EQ(DataChannelInterface::kClosed, offer_channel->state());
1463
1464 // Do another offer / answer exchange and verify that the data channel is
1465 // opened.
1466 CreateOfferReceiveAnswer();
1467 EXPECT_EQ_WAIT(DataChannelInterface::kOpen, received_channel->state(),
1468 kTimeout);
1469}
1470
1471// This test that no data channel is returned if a reliable channel is
1472// requested.
1473// TODO(perkj): Remove this test once reliable channels are implemented.
1474TEST_F(PeerConnectionInterfaceTest, CreateReliableRtpDataChannelShouldFail) {
1475 FakeConstraints constraints;
1476 constraints.SetAllowRtpDataChannels();
1477 CreatePeerConnection(&constraints);
1478
1479 std::string label = "test";
1480 webrtc::DataChannelInit config;
1481 config.reliable = true;
1482 scoped_refptr<DataChannelInterface> channel =
1483 pc_->CreateDataChannel(label, &config);
1484 EXPECT_TRUE(channel == NULL);
1485}
1486
deadbeefab9b2d12015-10-14 11:33:11 -07001487// Verifies that duplicated label is not allowed for RTP data channel.
1488TEST_F(PeerConnectionInterfaceTest, RtpDuplicatedLabelNotAllowed) {
1489 FakeConstraints constraints;
1490 constraints.SetAllowRtpDataChannels();
1491 CreatePeerConnection(&constraints);
1492
1493 std::string label = "test";
1494 scoped_refptr<DataChannelInterface> channel =
1495 pc_->CreateDataChannel(label, nullptr);
1496 EXPECT_NE(channel, nullptr);
1497
1498 scoped_refptr<DataChannelInterface> dup_channel =
1499 pc_->CreateDataChannel(label, nullptr);
1500 EXPECT_EQ(dup_channel, nullptr);
1501}
1502
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001503// This tests that a SCTP data channel is returned using different
1504// DataChannelInit configurations.
1505TEST_F(PeerConnectionInterfaceTest, CreateSctpDataChannel) {
1506 FakeConstraints constraints;
1507 constraints.SetAllowDtlsSctpDataChannels();
1508 CreatePeerConnection(&constraints);
1509
1510 webrtc::DataChannelInit config;
1511
1512 scoped_refptr<DataChannelInterface> channel =
1513 pc_->CreateDataChannel("1", &config);
1514 EXPECT_TRUE(channel != NULL);
1515 EXPECT_TRUE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001516 EXPECT_TRUE(observer_.renegotiation_needed_);
1517 observer_.renegotiation_needed_ = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001518
1519 config.ordered = false;
1520 channel = pc_->CreateDataChannel("2", &config);
1521 EXPECT_TRUE(channel != NULL);
1522 EXPECT_TRUE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001523 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001524
1525 config.ordered = true;
1526 config.maxRetransmits = 0;
1527 channel = pc_->CreateDataChannel("3", &config);
1528 EXPECT_TRUE(channel != NULL);
1529 EXPECT_FALSE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001530 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001531
1532 config.maxRetransmits = -1;
1533 config.maxRetransmitTime = 0;
1534 channel = pc_->CreateDataChannel("4", &config);
1535 EXPECT_TRUE(channel != NULL);
1536 EXPECT_FALSE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001537 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001538}
1539
1540// This tests that no data channel is returned if both maxRetransmits and
1541// maxRetransmitTime are set for SCTP data channels.
1542TEST_F(PeerConnectionInterfaceTest,
1543 CreateSctpDataChannelShouldFailForInvalidConfig) {
1544 FakeConstraints constraints;
1545 constraints.SetAllowDtlsSctpDataChannels();
1546 CreatePeerConnection(&constraints);
1547
1548 std::string label = "test";
1549 webrtc::DataChannelInit config;
1550 config.maxRetransmits = 0;
1551 config.maxRetransmitTime = 0;
1552
1553 scoped_refptr<DataChannelInterface> channel =
1554 pc_->CreateDataChannel(label, &config);
1555 EXPECT_TRUE(channel == NULL);
1556}
1557
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001558// The test verifies that creating a SCTP data channel with an id already in use
1559// or out of range should fail.
1560TEST_F(PeerConnectionInterfaceTest,
1561 CreateSctpDataChannelWithInvalidIdShouldFail) {
1562 FakeConstraints constraints;
1563 constraints.SetAllowDtlsSctpDataChannels();
1564 CreatePeerConnection(&constraints);
1565
1566 webrtc::DataChannelInit config;
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001567 scoped_refptr<DataChannelInterface> channel;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001568
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001569 config.id = 1;
1570 channel = pc_->CreateDataChannel("1", &config);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001571 EXPECT_TRUE(channel != NULL);
1572 EXPECT_EQ(1, channel->id());
1573
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001574 channel = pc_->CreateDataChannel("x", &config);
1575 EXPECT_TRUE(channel == NULL);
1576
1577 config.id = cricket::kMaxSctpSid;
1578 channel = pc_->CreateDataChannel("max", &config);
1579 EXPECT_TRUE(channel != NULL);
1580 EXPECT_EQ(config.id, channel->id());
1581
1582 config.id = cricket::kMaxSctpSid + 1;
1583 channel = pc_->CreateDataChannel("x", &config);
1584 EXPECT_TRUE(channel == NULL);
1585}
1586
deadbeefab9b2d12015-10-14 11:33:11 -07001587// Verifies that duplicated label is allowed for SCTP data channel.
1588TEST_F(PeerConnectionInterfaceTest, SctpDuplicatedLabelAllowed) {
1589 FakeConstraints constraints;
1590 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
1591 true);
1592 CreatePeerConnection(&constraints);
1593
1594 std::string label = "test";
1595 scoped_refptr<DataChannelInterface> channel =
1596 pc_->CreateDataChannel(label, nullptr);
1597 EXPECT_NE(channel, nullptr);
1598
1599 scoped_refptr<DataChannelInterface> dup_channel =
1600 pc_->CreateDataChannel(label, nullptr);
1601 EXPECT_NE(dup_channel, nullptr);
1602}
1603
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001604// This test verifies that OnRenegotiationNeeded is fired for every new RTP
1605// DataChannel.
1606TEST_F(PeerConnectionInterfaceTest, RenegotiationNeededForNewRtpDataChannel) {
1607 FakeConstraints constraints;
1608 constraints.SetAllowRtpDataChannels();
1609 CreatePeerConnection(&constraints);
1610
1611 scoped_refptr<DataChannelInterface> dc1 =
1612 pc_->CreateDataChannel("test1", NULL);
1613 EXPECT_TRUE(observer_.renegotiation_needed_);
1614 observer_.renegotiation_needed_ = false;
1615
1616 scoped_refptr<DataChannelInterface> dc2 =
1617 pc_->CreateDataChannel("test2", NULL);
1618 EXPECT_TRUE(observer_.renegotiation_needed_);
1619}
1620
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001621// This test that a data channel closes when a PeerConnection is deleted/closed.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001622TEST_F(PeerConnectionInterfaceTest, DataChannelCloseWhenPeerConnectionClose) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001623 FakeConstraints constraints;
1624 constraints.SetAllowRtpDataChannels();
1625 CreatePeerConnection(&constraints);
1626
1627 scoped_refptr<DataChannelInterface> data1 =
1628 pc_->CreateDataChannel("test1", NULL);
1629 scoped_refptr<DataChannelInterface> data2 =
1630 pc_->CreateDataChannel("test2", NULL);
1631 ASSERT_TRUE(data1 != NULL);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001632 rtc::scoped_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001633 new MockDataChannelObserver(data1));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001634 rtc::scoped_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001635 new MockDataChannelObserver(data2));
1636
1637 CreateOfferReceiveAnswer();
1638 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1639 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
1640
1641 ReleasePeerConnection();
1642 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
1643 EXPECT_EQ(DataChannelInterface::kClosed, data2->state());
1644}
1645
1646// This test that data channels can be rejected in an answer.
1647TEST_F(PeerConnectionInterfaceTest, TestRejectDataChannelInAnswer) {
1648 FakeConstraints constraints;
1649 constraints.SetAllowRtpDataChannels();
1650 CreatePeerConnection(&constraints);
1651
1652 scoped_refptr<DataChannelInterface> offer_channel(
1653 pc_->CreateDataChannel("offer_channel", NULL));
1654
1655 CreateOfferAsLocalDescription();
1656
1657 // Create an answer where the m-line for data channels are rejected.
1658 std::string sdp;
1659 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
1660 webrtc::JsepSessionDescription* answer = new webrtc::JsepSessionDescription(
1661 SessionDescriptionInterface::kAnswer);
1662 EXPECT_TRUE(answer->Initialize(sdp, NULL));
1663 cricket::ContentInfo* data_info =
1664 answer->description()->GetContentByName("data");
1665 data_info->rejected = true;
1666
1667 DoSetRemoteDescription(answer);
1668 EXPECT_EQ(DataChannelInterface::kClosed, offer_channel->state());
1669}
1670
1671// Test that we can create a session description from an SDP string from
1672// FireFox, use it as a remote session description, generate an answer and use
1673// the answer as a local description.
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00001674TEST_F(PeerConnectionInterfaceTest, ReceiveFireFoxOffer) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001675 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001676 FakeConstraints constraints;
1677 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
1678 true);
1679 CreatePeerConnection(&constraints);
1680 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
1681 SessionDescriptionInterface* desc =
1682 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
jbauchfabe2c92015-07-16 13:43:14 -07001683 webrtc::kFireFoxSdpOffer, nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001684 EXPECT_TRUE(DoSetSessionDescription(desc, false));
1685 CreateAnswerAsLocalDescription();
1686 ASSERT_TRUE(pc_->local_description() != NULL);
1687 ASSERT_TRUE(pc_->remote_description() != NULL);
1688
1689 const cricket::ContentInfo* content =
1690 cricket::GetFirstAudioContent(pc_->local_description()->description());
1691 ASSERT_TRUE(content != NULL);
1692 EXPECT_FALSE(content->rejected);
1693
1694 content =
1695 cricket::GetFirstVideoContent(pc_->local_description()->description());
1696 ASSERT_TRUE(content != NULL);
1697 EXPECT_FALSE(content->rejected);
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00001698#ifdef HAVE_SCTP
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001699 content =
1700 cricket::GetFirstDataContent(pc_->local_description()->description());
1701 ASSERT_TRUE(content != NULL);
1702 EXPECT_TRUE(content->rejected);
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00001703#endif
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001704}
1705
1706// Test that we can create an audio only offer and receive an answer with a
1707// limited set of audio codecs and receive an updated offer with more audio
1708// codecs, where the added codecs are not supported.
1709TEST_F(PeerConnectionInterfaceTest, ReceiveUpdatedAudioOfferWithBadCodecs) {
1710 CreatePeerConnection();
1711 AddVoiceStream("audio_label");
1712 CreateOfferAsLocalDescription();
1713
1714 SessionDescriptionInterface* answer =
1715 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
jbauchfabe2c92015-07-16 13:43:14 -07001716 webrtc::kAudioSdp, nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001717 EXPECT_TRUE(DoSetSessionDescription(answer, false));
1718
1719 SessionDescriptionInterface* updated_offer =
1720 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
jbauchfabe2c92015-07-16 13:43:14 -07001721 webrtc::kAudioSdpWithUnsupportedCodecs,
1722 nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001723 EXPECT_TRUE(DoSetSessionDescription(updated_offer, false));
1724 CreateAnswerAsLocalDescription();
1725}
1726
deadbeefc80741f2015-10-22 13:14:45 -07001727// Test that if we're receiving (but not sending) a track, subsequent offers
1728// will have m-lines with a=recvonly.
1729TEST_F(PeerConnectionInterfaceTest, CreateSubsequentRecvOnlyOffer) {
1730 FakeConstraints constraints;
1731 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
1732 true);
1733 CreatePeerConnection(&constraints);
1734 CreateAndSetRemoteOffer(kSdpStringWithStream1);
1735 CreateAnswerAsLocalDescription();
1736
1737 // At this point we should be receiving stream 1, but not sending anything.
1738 // A new offer should be recvonly.
1739 SessionDescriptionInterface* offer;
1740 DoCreateOffer(&offer, nullptr);
1741
1742 const cricket::ContentInfo* video_content =
1743 cricket::GetFirstVideoContent(offer->description());
1744 const cricket::VideoContentDescription* video_desc =
1745 static_cast<const cricket::VideoContentDescription*>(
1746 video_content->description);
1747 ASSERT_EQ(cricket::MD_RECVONLY, video_desc->direction());
1748
1749 const cricket::ContentInfo* audio_content =
1750 cricket::GetFirstAudioContent(offer->description());
1751 const cricket::AudioContentDescription* audio_desc =
1752 static_cast<const cricket::AudioContentDescription*>(
1753 audio_content->description);
1754 ASSERT_EQ(cricket::MD_RECVONLY, audio_desc->direction());
1755}
1756
1757// Test that if we're receiving (but not sending) a track, and the
1758// offerToReceiveVideo/offerToReceiveAudio constraints are explicitly set to
1759// false, the generated m-lines will be a=inactive.
1760TEST_F(PeerConnectionInterfaceTest, CreateSubsequentInactiveOffer) {
1761 FakeConstraints constraints;
1762 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
1763 true);
1764 CreatePeerConnection(&constraints);
1765 CreateAndSetRemoteOffer(kSdpStringWithStream1);
1766 CreateAnswerAsLocalDescription();
1767
1768 // At this point we should be receiving stream 1, but not sending anything.
1769 // A new offer would be recvonly, but we'll set the "no receive" constraints
1770 // to make it inactive.
1771 SessionDescriptionInterface* offer;
1772 FakeConstraints offer_constraints;
1773 offer_constraints.AddMandatory(
1774 webrtc::MediaConstraintsInterface::kOfferToReceiveVideo, false);
1775 offer_constraints.AddMandatory(
1776 webrtc::MediaConstraintsInterface::kOfferToReceiveAudio, false);
1777 DoCreateOffer(&offer, &offer_constraints);
1778
1779 const cricket::ContentInfo* video_content =
1780 cricket::GetFirstVideoContent(offer->description());
1781 const cricket::VideoContentDescription* video_desc =
1782 static_cast<const cricket::VideoContentDescription*>(
1783 video_content->description);
1784 ASSERT_EQ(cricket::MD_INACTIVE, video_desc->direction());
1785
1786 const cricket::ContentInfo* audio_content =
1787 cricket::GetFirstAudioContent(offer->description());
1788 const cricket::AudioContentDescription* audio_desc =
1789 static_cast<const cricket::AudioContentDescription*>(
1790 audio_content->description);
1791 ASSERT_EQ(cricket::MD_INACTIVE, audio_desc->direction());
1792}
1793
deadbeef653b8e02015-11-11 12:55:10 -08001794// Test that we can use SetConfiguration to change the ICE servers of the
1795// PortAllocator.
1796TEST_F(PeerConnectionInterfaceTest, SetConfigurationChangesIceServers) {
1797 CreatePeerConnection();
1798
1799 PeerConnectionInterface::RTCConfiguration config;
1800 PeerConnectionInterface::IceServer server;
1801 server.uri = "stun:test_hostname";
1802 config.servers.push_back(server);
1803 EXPECT_TRUE(pc_->SetConfiguration(config));
1804
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08001805 EXPECT_EQ(1u, port_allocator_->stun_servers().size());
1806 EXPECT_EQ("test_hostname",
1807 port_allocator_->stun_servers().begin()->hostname());
deadbeef653b8e02015-11-11 12:55:10 -08001808}
1809
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001810// Test that PeerConnection::Close changes the states to closed and all remote
1811// tracks change state to ended.
1812TEST_F(PeerConnectionInterfaceTest, CloseAndTestStreamsAndStates) {
1813 // Initialize a PeerConnection and negotiate local and remote session
1814 // description.
1815 InitiateCall();
1816 ASSERT_EQ(1u, pc_->local_streams()->count());
1817 ASSERT_EQ(1u, pc_->remote_streams()->count());
1818
1819 pc_->Close();
1820
1821 EXPECT_EQ(PeerConnectionInterface::kClosed, pc_->signaling_state());
1822 EXPECT_EQ(PeerConnectionInterface::kIceConnectionClosed,
1823 pc_->ice_connection_state());
1824 EXPECT_EQ(PeerConnectionInterface::kIceGatheringComplete,
1825 pc_->ice_gathering_state());
1826
1827 EXPECT_EQ(1u, pc_->local_streams()->count());
1828 EXPECT_EQ(1u, pc_->remote_streams()->count());
1829
1830 scoped_refptr<MediaStreamInterface> remote_stream =
1831 pc_->remote_streams()->at(0);
1832 EXPECT_EQ(MediaStreamTrackInterface::kEnded,
1833 remote_stream->GetVideoTracks()[0]->state());
1834 EXPECT_EQ(MediaStreamTrackInterface::kEnded,
1835 remote_stream->GetAudioTracks()[0]->state());
1836}
1837
1838// Test that PeerConnection methods fails gracefully after
1839// PeerConnection::Close has been called.
1840TEST_F(PeerConnectionInterfaceTest, CloseAndTestMethods) {
1841 CreatePeerConnection();
1842 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
1843 CreateOfferAsRemoteDescription();
1844 CreateAnswerAsLocalDescription();
1845
1846 ASSERT_EQ(1u, pc_->local_streams()->count());
1847 scoped_refptr<MediaStreamInterface> local_stream =
1848 pc_->local_streams()->at(0);
1849
1850 pc_->Close();
1851
1852 pc_->RemoveStream(local_stream);
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +00001853 EXPECT_FALSE(pc_->AddStream(local_stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001854
1855 ASSERT_FALSE(local_stream->GetAudioTracks().empty());
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001856 rtc::scoped_refptr<webrtc::DtmfSenderInterface> dtmf_sender(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001857 pc_->CreateDtmfSender(local_stream->GetAudioTracks()[0]));
wu@webrtc.org66037362013-08-13 00:09:35 +00001858 EXPECT_TRUE(NULL == dtmf_sender); // local stream has been removed.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001859
1860 EXPECT_TRUE(pc_->CreateDataChannel("test", NULL) == NULL);
1861
1862 EXPECT_TRUE(pc_->local_description() != NULL);
1863 EXPECT_TRUE(pc_->remote_description() != NULL);
1864
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001865 rtc::scoped_ptr<SessionDescriptionInterface> offer;
deadbeefc80741f2015-10-22 13:14:45 -07001866 EXPECT_TRUE(DoCreateOffer(offer.use(), nullptr));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001867 rtc::scoped_ptr<SessionDescriptionInterface> answer;
deadbeefc80741f2015-10-22 13:14:45 -07001868 EXPECT_TRUE(DoCreateAnswer(answer.use(), nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001869
1870 std::string sdp;
1871 ASSERT_TRUE(pc_->remote_description()->ToString(&sdp));
1872 SessionDescriptionInterface* remote_offer =
1873 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
1874 sdp, NULL);
1875 EXPECT_FALSE(DoSetRemoteDescription(remote_offer));
1876
1877 ASSERT_TRUE(pc_->local_description()->ToString(&sdp));
1878 SessionDescriptionInterface* local_offer =
1879 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
1880 sdp, NULL);
1881 EXPECT_FALSE(DoSetLocalDescription(local_offer));
1882}
1883
1884// Test that GetStats can still be called after PeerConnection::Close.
1885TEST_F(PeerConnectionInterfaceTest, CloseAndGetStats) {
1886 InitiateCall();
1887 pc_->Close();
1888 DoGetStats(NULL);
1889}
deadbeefab9b2d12015-10-14 11:33:11 -07001890
1891// NOTE: The series of tests below come from what used to be
1892// mediastreamsignaling_unittest.cc, and are mostly aimed at testing that
1893// setting a remote or local description has the expected effects.
1894
1895// This test verifies that the remote MediaStreams corresponding to a received
1896// SDP string is created. In this test the two separate MediaStreams are
1897// signaled.
1898TEST_F(PeerConnectionInterfaceTest, UpdateRemoteStreams) {
1899 FakeConstraints constraints;
1900 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
1901 true);
1902 CreatePeerConnection(&constraints);
1903 CreateAndSetRemoteOffer(kSdpStringWithStream1);
1904
1905 rtc::scoped_refptr<StreamCollection> reference(CreateStreamCollection(1));
1906 EXPECT_TRUE(
1907 CompareStreamCollections(observer_.remote_streams(), reference.get()));
1908 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
1909 EXPECT_TRUE(remote_stream->GetVideoTracks()[0]->GetSource() != nullptr);
1910
1911 // Create a session description based on another SDP with another
1912 // MediaStream.
1913 CreateAndSetRemoteOffer(kSdpStringWithStream1And2);
1914
1915 rtc::scoped_refptr<StreamCollection> reference2(CreateStreamCollection(2));
1916 EXPECT_TRUE(
1917 CompareStreamCollections(observer_.remote_streams(), reference2.get()));
1918}
1919
1920// This test verifies that when remote tracks are added/removed from SDP, the
1921// created remote streams are updated appropriately.
1922TEST_F(PeerConnectionInterfaceTest,
1923 AddRemoveTrackFromExistingRemoteMediaStream) {
1924 FakeConstraints constraints;
1925 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
1926 true);
1927 CreatePeerConnection(&constraints);
1928 rtc::scoped_ptr<SessionDescriptionInterface> desc_ms1;
1929 CreateSessionDescriptionAndReference(1, 1, desc_ms1.accept());
1930 EXPECT_TRUE(DoSetRemoteDescription(desc_ms1.release()));
1931 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
1932 reference_collection_));
1933
1934 // Add extra audio and video tracks to the same MediaStream.
1935 rtc::scoped_ptr<SessionDescriptionInterface> desc_ms1_two_tracks;
1936 CreateSessionDescriptionAndReference(2, 2, desc_ms1_two_tracks.accept());
1937 EXPECT_TRUE(DoSetRemoteDescription(desc_ms1_two_tracks.release()));
1938 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
1939 reference_collection_));
1940
1941 // Remove the extra audio and video tracks.
1942 rtc::scoped_ptr<SessionDescriptionInterface> desc_ms2;
1943 CreateSessionDescriptionAndReference(1, 1, desc_ms2.accept());
1944 EXPECT_TRUE(DoSetRemoteDescription(desc_ms2.release()));
1945 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
1946 reference_collection_));
1947}
1948
1949// This tests that remote tracks are ended if a local session description is set
1950// that rejects the media content type.
1951TEST_F(PeerConnectionInterfaceTest, RejectMediaContent) {
1952 FakeConstraints constraints;
1953 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
1954 true);
1955 CreatePeerConnection(&constraints);
1956 // First create and set a remote offer, then reject its video content in our
1957 // answer.
1958 CreateAndSetRemoteOffer(kSdpStringWithStream1);
1959 ASSERT_EQ(1u, observer_.remote_streams()->count());
1960 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
1961 ASSERT_EQ(1u, remote_stream->GetVideoTracks().size());
1962 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
1963
1964 rtc::scoped_refptr<webrtc::VideoTrackInterface> remote_video =
1965 remote_stream->GetVideoTracks()[0];
1966 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, remote_video->state());
1967 rtc::scoped_refptr<webrtc::AudioTrackInterface> remote_audio =
1968 remote_stream->GetAudioTracks()[0];
1969 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, remote_audio->state());
1970
1971 rtc::scoped_ptr<SessionDescriptionInterface> local_answer;
deadbeefc80741f2015-10-22 13:14:45 -07001972 EXPECT_TRUE(DoCreateAnswer(local_answer.accept(), nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07001973 cricket::ContentInfo* video_info =
1974 local_answer->description()->GetContentByName("video");
1975 video_info->rejected = true;
1976 EXPECT_TRUE(DoSetLocalDescription(local_answer.release()));
1977 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kEnded, remote_video->state());
1978 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, remote_audio->state());
1979
1980 // Now create an offer where we reject both video and audio.
1981 rtc::scoped_ptr<SessionDescriptionInterface> local_offer;
deadbeefc80741f2015-10-22 13:14:45 -07001982 EXPECT_TRUE(DoCreateOffer(local_offer.accept(), nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07001983 video_info = local_offer->description()->GetContentByName("video");
1984 ASSERT_TRUE(video_info != nullptr);
1985 video_info->rejected = true;
1986 cricket::ContentInfo* audio_info =
1987 local_offer->description()->GetContentByName("audio");
1988 ASSERT_TRUE(audio_info != nullptr);
1989 audio_info->rejected = true;
1990 EXPECT_TRUE(DoSetLocalDescription(local_offer.release()));
1991 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kEnded, remote_video->state());
1992 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kEnded, remote_audio->state());
1993}
1994
1995// This tests that we won't crash if the remote track has been removed outside
1996// of PeerConnection and then PeerConnection tries to reject the track.
1997TEST_F(PeerConnectionInterfaceTest, RemoveTrackThenRejectMediaContent) {
1998 FakeConstraints constraints;
1999 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2000 true);
2001 CreatePeerConnection(&constraints);
2002 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2003 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2004 remote_stream->RemoveTrack(remote_stream->GetVideoTracks()[0]);
2005 remote_stream->RemoveTrack(remote_stream->GetAudioTracks()[0]);
2006
2007 rtc::scoped_ptr<SessionDescriptionInterface> local_answer(
2008 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
2009 kSdpStringWithStream1, nullptr));
2010 cricket::ContentInfo* video_info =
2011 local_answer->description()->GetContentByName("video");
2012 video_info->rejected = true;
2013 cricket::ContentInfo* audio_info =
2014 local_answer->description()->GetContentByName("audio");
2015 audio_info->rejected = true;
2016 EXPECT_TRUE(DoSetLocalDescription(local_answer.release()));
2017
2018 // No crash is a pass.
2019}
2020
deadbeef5e97fb52015-10-15 12:49:08 -07002021// This tests that if a recvonly remote description is set, no remote streams
2022// will be created, even if the description contains SSRCs/MSIDs.
2023// See: https://code.google.com/p/webrtc/issues/detail?id=5054
2024TEST_F(PeerConnectionInterfaceTest, RecvonlyDescriptionDoesntCreateStream) {
2025 FakeConstraints constraints;
2026 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2027 true);
2028 CreatePeerConnection(&constraints);
2029
2030 std::string recvonly_offer = kSdpStringWithStream1;
2031 rtc::replace_substrs(kSendrecv, strlen(kSendrecv), kRecvonly,
2032 strlen(kRecvonly), &recvonly_offer);
2033 CreateAndSetRemoteOffer(recvonly_offer);
2034
2035 EXPECT_EQ(0u, observer_.remote_streams()->count());
2036}
2037
deadbeefab9b2d12015-10-14 11:33:11 -07002038// This tests that a default MediaStream is created if a remote session
2039// description doesn't contain any streams and no MSID support.
2040// It also tests that the default stream is updated if a video m-line is added
2041// in a subsequent session description.
2042TEST_F(PeerConnectionInterfaceTest, SdpWithoutMsidCreatesDefaultStream) {
2043 FakeConstraints constraints;
2044 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2045 true);
2046 CreatePeerConnection(&constraints);
2047 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2048
2049 ASSERT_EQ(1u, observer_.remote_streams()->count());
2050 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2051
2052 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2053 EXPECT_EQ(0u, remote_stream->GetVideoTracks().size());
2054 EXPECT_EQ("default", remote_stream->label());
2055
2056 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2057 ASSERT_EQ(1u, observer_.remote_streams()->count());
2058 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2059 EXPECT_EQ("defaulta0", remote_stream->GetAudioTracks()[0]->id());
deadbeef884f5852016-01-15 09:20:04 -08002060 EXPECT_EQ(MediaStreamTrackInterface::kLive,
2061 remote_stream->GetAudioTracks()[0]->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002062 ASSERT_EQ(1u, remote_stream->GetVideoTracks().size());
2063 EXPECT_EQ("defaultv0", remote_stream->GetVideoTracks()[0]->id());
deadbeef884f5852016-01-15 09:20:04 -08002064 EXPECT_EQ(MediaStreamTrackInterface::kLive,
2065 remote_stream->GetVideoTracks()[0]->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002066}
2067
2068// This tests that a default MediaStream is created if a remote session
2069// description doesn't contain any streams and media direction is send only.
2070TEST_F(PeerConnectionInterfaceTest,
2071 SendOnlySdpWithoutMsidCreatesDefaultStream) {
2072 FakeConstraints constraints;
2073 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2074 true);
2075 CreatePeerConnection(&constraints);
2076 CreateAndSetRemoteOffer(kSdpStringSendOnlyWithoutStreams);
2077
2078 ASSERT_EQ(1u, observer_.remote_streams()->count());
2079 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2080
2081 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2082 EXPECT_EQ(1u, remote_stream->GetVideoTracks().size());
2083 EXPECT_EQ("default", remote_stream->label());
2084}
2085
2086// This tests that it won't crash when PeerConnection tries to remove
2087// a remote track that as already been removed from the MediaStream.
2088TEST_F(PeerConnectionInterfaceTest, RemoveAlreadyGoneRemoteStream) {
2089 FakeConstraints constraints;
2090 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2091 true);
2092 CreatePeerConnection(&constraints);
2093 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2094 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2095 remote_stream->RemoveTrack(remote_stream->GetAudioTracks()[0]);
2096 remote_stream->RemoveTrack(remote_stream->GetVideoTracks()[0]);
2097
2098 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2099
2100 // No crash is a pass.
2101}
2102
2103// This tests that a default MediaStream is created if the remote session
2104// description doesn't contain any streams and don't contain an indication if
2105// MSID is supported.
2106TEST_F(PeerConnectionInterfaceTest,
2107 SdpWithoutMsidAndStreamsCreatesDefaultStream) {
2108 FakeConstraints constraints;
2109 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2110 true);
2111 CreatePeerConnection(&constraints);
2112 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2113
2114 ASSERT_EQ(1u, observer_.remote_streams()->count());
2115 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2116 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2117 EXPECT_EQ(1u, remote_stream->GetVideoTracks().size());
2118}
2119
2120// This tests that a default MediaStream is not created if the remote session
2121// description doesn't contain any streams but does support MSID.
2122TEST_F(PeerConnectionInterfaceTest, SdpWithMsidDontCreatesDefaultStream) {
2123 FakeConstraints constraints;
2124 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2125 true);
2126 CreatePeerConnection(&constraints);
2127 CreateAndSetRemoteOffer(kSdpStringWithMsidWithoutStreams);
2128 EXPECT_EQ(0u, observer_.remote_streams()->count());
2129}
2130
deadbeefbda7e0b2015-12-08 17:13:40 -08002131// This tests that when setting a new description, the old default tracks are
2132// not destroyed and recreated.
2133// See: https://bugs.chromium.org/p/webrtc/issues/detail?id=5250
2134TEST_F(PeerConnectionInterfaceTest, DefaultTracksNotDestroyedAndRecreated) {
2135 FakeConstraints constraints;
2136 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2137 true);
2138 CreatePeerConnection(&constraints);
2139 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2140
2141 ASSERT_EQ(1u, observer_.remote_streams()->count());
2142 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2143 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2144
2145 // Set the track to "disabled", then set a new description and ensure the
2146 // track is still disabled, which ensures it hasn't been recreated.
2147 remote_stream->GetAudioTracks()[0]->set_enabled(false);
2148 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2149 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2150 EXPECT_FALSE(remote_stream->GetAudioTracks()[0]->enabled());
2151}
2152
deadbeefab9b2d12015-10-14 11:33:11 -07002153// This tests that a default MediaStream is not created if a remote session
2154// description is updated to not have any MediaStreams.
2155TEST_F(PeerConnectionInterfaceTest, VerifyDefaultStreamIsNotCreated) {
2156 FakeConstraints constraints;
2157 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2158 true);
2159 CreatePeerConnection(&constraints);
2160 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2161 rtc::scoped_refptr<StreamCollection> reference(CreateStreamCollection(1));
2162 EXPECT_TRUE(
2163 CompareStreamCollections(observer_.remote_streams(), reference.get()));
2164
2165 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2166 EXPECT_EQ(0u, observer_.remote_streams()->count());
2167}
2168
2169// This tests that an RtpSender is created when the local description is set
2170// after adding a local stream.
2171// TODO(deadbeef): This test and the one below it need to be updated when
2172// an RtpSender's lifetime isn't determined by when a local description is set.
2173TEST_F(PeerConnectionInterfaceTest, LocalDescriptionChanged) {
2174 FakeConstraints constraints;
2175 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2176 true);
2177 CreatePeerConnection(&constraints);
2178 // Create an offer just to ensure we have an identity before we manually
2179 // call SetLocalDescription.
2180 rtc::scoped_ptr<SessionDescriptionInterface> throwaway;
deadbeefc80741f2015-10-22 13:14:45 -07002181 ASSERT_TRUE(DoCreateOffer(throwaway.accept(), nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07002182
2183 rtc::scoped_ptr<SessionDescriptionInterface> desc_1;
2184 CreateSessionDescriptionAndReference(2, 2, desc_1.accept());
2185
2186 pc_->AddStream(reference_collection_->at(0));
2187 EXPECT_TRUE(DoSetLocalDescription(desc_1.release()));
2188 auto senders = pc_->GetSenders();
2189 EXPECT_EQ(4u, senders.size());
2190 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2191 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2192 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[1]));
2193 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[1]));
2194
2195 // Remove an audio and video track.
deadbeeffac06552015-11-25 11:26:01 -08002196 pc_->RemoveStream(reference_collection_->at(0));
deadbeefab9b2d12015-10-14 11:33:11 -07002197 rtc::scoped_ptr<SessionDescriptionInterface> desc_2;
2198 CreateSessionDescriptionAndReference(1, 1, desc_2.accept());
deadbeeffac06552015-11-25 11:26:01 -08002199 pc_->AddStream(reference_collection_->at(0));
deadbeefab9b2d12015-10-14 11:33:11 -07002200 EXPECT_TRUE(DoSetLocalDescription(desc_2.release()));
2201 senders = pc_->GetSenders();
2202 EXPECT_EQ(2u, senders.size());
2203 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2204 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2205 EXPECT_FALSE(ContainsSender(senders, kAudioTracks[1]));
2206 EXPECT_FALSE(ContainsSender(senders, kVideoTracks[1]));
2207}
2208
2209// This tests that an RtpSender is created when the local description is set
2210// before adding a local stream.
2211TEST_F(PeerConnectionInterfaceTest,
2212 AddLocalStreamAfterLocalDescriptionChanged) {
2213 FakeConstraints constraints;
2214 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2215 true);
2216 CreatePeerConnection(&constraints);
2217 // Create an offer just to ensure we have an identity before we manually
2218 // call SetLocalDescription.
2219 rtc::scoped_ptr<SessionDescriptionInterface> throwaway;
deadbeefc80741f2015-10-22 13:14:45 -07002220 ASSERT_TRUE(DoCreateOffer(throwaway.accept(), nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07002221
2222 rtc::scoped_ptr<SessionDescriptionInterface> desc_1;
2223 CreateSessionDescriptionAndReference(2, 2, desc_1.accept());
2224
2225 EXPECT_TRUE(DoSetLocalDescription(desc_1.release()));
2226 auto senders = pc_->GetSenders();
2227 EXPECT_EQ(0u, senders.size());
2228
2229 pc_->AddStream(reference_collection_->at(0));
2230 senders = pc_->GetSenders();
2231 EXPECT_EQ(4u, senders.size());
2232 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2233 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2234 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[1]));
2235 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[1]));
2236}
2237
2238// This tests that the expected behavior occurs if the SSRC on a local track is
2239// changed when SetLocalDescription is called.
2240TEST_F(PeerConnectionInterfaceTest,
2241 ChangeSsrcOnTrackInLocalSessionDescription) {
2242 FakeConstraints constraints;
2243 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2244 true);
2245 CreatePeerConnection(&constraints);
2246 // Create an offer just to ensure we have an identity before we manually
2247 // call SetLocalDescription.
2248 rtc::scoped_ptr<SessionDescriptionInterface> throwaway;
deadbeefc80741f2015-10-22 13:14:45 -07002249 ASSERT_TRUE(DoCreateOffer(throwaway.accept(), nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07002250
2251 rtc::scoped_ptr<SessionDescriptionInterface> desc;
2252 CreateSessionDescriptionAndReference(1, 1, desc.accept());
2253 std::string sdp;
2254 desc->ToString(&sdp);
2255
2256 pc_->AddStream(reference_collection_->at(0));
2257 EXPECT_TRUE(DoSetLocalDescription(desc.release()));
2258 auto senders = pc_->GetSenders();
2259 EXPECT_EQ(2u, senders.size());
2260 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2261 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2262
2263 // Change the ssrc of the audio and video track.
2264 std::string ssrc_org = "a=ssrc:1";
2265 std::string ssrc_to = "a=ssrc:97";
2266 rtc::replace_substrs(ssrc_org.c_str(), ssrc_org.length(), ssrc_to.c_str(),
2267 ssrc_to.length(), &sdp);
2268 ssrc_org = "a=ssrc:2";
2269 ssrc_to = "a=ssrc:98";
2270 rtc::replace_substrs(ssrc_org.c_str(), ssrc_org.length(), ssrc_to.c_str(),
2271 ssrc_to.length(), &sdp);
2272 rtc::scoped_ptr<SessionDescriptionInterface> updated_desc(
2273 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer, sdp,
2274 nullptr));
2275
2276 EXPECT_TRUE(DoSetLocalDescription(updated_desc.release()));
2277 senders = pc_->GetSenders();
2278 EXPECT_EQ(2u, senders.size());
2279 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2280 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2281 // TODO(deadbeef): Once RtpSenders expose parameters, check that the SSRC
2282 // changed.
2283}
2284
2285// This tests that the expected behavior occurs if a new session description is
2286// set with the same tracks, but on a different MediaStream.
2287TEST_F(PeerConnectionInterfaceTest, SignalSameTracksInSeparateMediaStream) {
2288 FakeConstraints constraints;
2289 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2290 true);
2291 CreatePeerConnection(&constraints);
2292 // Create an offer just to ensure we have an identity before we manually
2293 // call SetLocalDescription.
2294 rtc::scoped_ptr<SessionDescriptionInterface> throwaway;
deadbeefc80741f2015-10-22 13:14:45 -07002295 ASSERT_TRUE(DoCreateOffer(throwaway.accept(), nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07002296
2297 rtc::scoped_ptr<SessionDescriptionInterface> desc;
2298 CreateSessionDescriptionAndReference(1, 1, desc.accept());
2299 std::string sdp;
2300 desc->ToString(&sdp);
2301
2302 pc_->AddStream(reference_collection_->at(0));
2303 EXPECT_TRUE(DoSetLocalDescription(desc.release()));
2304 auto senders = pc_->GetSenders();
2305 EXPECT_EQ(2u, senders.size());
2306 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2307 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2308
2309 // Add a new MediaStream but with the same tracks as in the first stream.
2310 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream_1(
2311 webrtc::MediaStream::Create(kStreams[1]));
2312 stream_1->AddTrack(reference_collection_->at(0)->GetVideoTracks()[0]);
2313 stream_1->AddTrack(reference_collection_->at(0)->GetAudioTracks()[0]);
2314 pc_->AddStream(stream_1);
2315
2316 // Replace msid in the original SDP.
2317 rtc::replace_substrs(kStreams[0], strlen(kStreams[0]), kStreams[1],
2318 strlen(kStreams[1]), &sdp);
2319
2320 rtc::scoped_ptr<SessionDescriptionInterface> updated_desc(
2321 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer, sdp,
2322 nullptr));
2323
2324 EXPECT_TRUE(DoSetLocalDescription(updated_desc.release()));
2325 senders = pc_->GetSenders();
2326 EXPECT_EQ(2u, senders.size());
2327 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2328 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2329}
2330
nisse51542be2016-02-12 02:27:06 -08002331// The PeerConnectionMediaConfig tests below verify that configuration
2332// and constraints are propagated into the MediaConfig passed to
2333// CreateMediaController. These settings are intended for MediaChannel
2334// constructors, but that is not exercised by these unittest.
2335class PeerConnectionFactoryForTest : public webrtc::PeerConnectionFactory {
2336 public:
2337 webrtc::MediaControllerInterface* CreateMediaController(
2338 const cricket::MediaConfig& config) const override {
2339 create_media_controller_called_ = true;
2340 create_media_controller_config_ = config;
2341
2342 webrtc::MediaControllerInterface* mc =
2343 PeerConnectionFactory::CreateMediaController(config);
2344 EXPECT_TRUE(mc != nullptr);
2345 return mc;
2346 }
2347
2348 // Mutable, so they can be modified in the above const-declared method.
2349 mutable bool create_media_controller_called_ = false;
2350 mutable cricket::MediaConfig create_media_controller_config_;
2351};
2352
2353class PeerConnectionMediaConfigTest : public testing::Test {
2354 protected:
2355 void SetUp() override {
2356 pcf_= new rtc::RefCountedObject<PeerConnectionFactoryForTest>();
2357 pcf_->Initialize();
2358 }
2359 const cricket::MediaConfig& TestCreatePeerConnection(
2360 const PeerConnectionInterface::RTCConfiguration& config,
2361 const MediaConstraintsInterface *constraints) {
2362 pcf_->create_media_controller_called_ = false;
2363
2364 scoped_refptr<PeerConnectionInterface> pc(
2365 pcf_->CreatePeerConnection(config, constraints, nullptr, nullptr,
2366 &observer_));
2367 EXPECT_TRUE(pc.get());
2368 EXPECT_TRUE(pcf_->create_media_controller_called_);
2369 return pcf_->create_media_controller_config_;
2370 }
2371
2372 scoped_refptr<PeerConnectionFactoryForTest> pcf_;
2373 MockPeerConnectionObserver observer_;
2374};
2375
2376// This test verifies the default behaviour with no constraints and a
2377// default RTCConfiguration.
2378TEST_F(PeerConnectionMediaConfigTest, TestDefaults) {
2379 PeerConnectionInterface::RTCConfiguration config;
2380 FakeConstraints constraints;
2381
2382 const cricket::MediaConfig& media_config =
2383 TestCreatePeerConnection(config, &constraints);
2384
2385 EXPECT_FALSE(media_config.enable_dscp);
nisse0db023a2016-03-01 04:29:59 -08002386 EXPECT_TRUE(media_config.video.enable_cpu_overuse_detection);
2387 EXPECT_FALSE(media_config.video.disable_prerenderer_smoothing);
2388 EXPECT_FALSE(media_config.video.suspend_below_min_bitrate);
nisse51542be2016-02-12 02:27:06 -08002389}
2390
2391// This test verifies the DSCP constraint is recognized and passed to
2392// the CreateMediaController call.
2393TEST_F(PeerConnectionMediaConfigTest, TestDscpConstraintTrue) {
2394 PeerConnectionInterface::RTCConfiguration config;
2395 FakeConstraints constraints;
2396
2397 constraints.AddOptional(webrtc::MediaConstraintsInterface::kEnableDscp, true);
2398 const cricket::MediaConfig& media_config =
2399 TestCreatePeerConnection(config, &constraints);
2400
2401 EXPECT_TRUE(media_config.enable_dscp);
2402}
2403
2404// This test verifies the cpu overuse detection constraint is
2405// recognized and passed to the CreateMediaController call.
2406TEST_F(PeerConnectionMediaConfigTest, TestCpuOveruseConstraintFalse) {
2407 PeerConnectionInterface::RTCConfiguration config;
2408 FakeConstraints constraints;
2409
2410 constraints.AddOptional(
2411 webrtc::MediaConstraintsInterface::kCpuOveruseDetection, false);
2412 const cricket::MediaConfig media_config =
2413 TestCreatePeerConnection(config, &constraints);
2414
nisse0db023a2016-03-01 04:29:59 -08002415 EXPECT_FALSE(media_config.video.enable_cpu_overuse_detection);
nisse51542be2016-02-12 02:27:06 -08002416}
2417
2418// This test verifies that the disable_prerenderer_smoothing flag is
2419// propagated from RTCConfiguration to the CreateMediaController call.
2420TEST_F(PeerConnectionMediaConfigTest, TestDisablePrerendererSmoothingTrue) {
2421 PeerConnectionInterface::RTCConfiguration config;
2422 FakeConstraints constraints;
2423
2424 config.disable_prerenderer_smoothing = true;
2425 const cricket::MediaConfig& media_config =
2426 TestCreatePeerConnection(config, &constraints);
2427
nisse0db023a2016-03-01 04:29:59 -08002428 EXPECT_TRUE(media_config.video.disable_prerenderer_smoothing);
2429}
2430
2431// This test verifies the suspend below min bitrate constraint is
2432// recognized and passed to the CreateMediaController call.
2433TEST_F(PeerConnectionMediaConfigTest,
2434 TestSuspendBelowMinBitrateConstraintTrue) {
2435 PeerConnectionInterface::RTCConfiguration config;
2436 FakeConstraints constraints;
2437
2438 constraints.AddOptional(
2439 webrtc::MediaConstraintsInterface::kEnableVideoSuspendBelowMinBitrate,
2440 true);
2441 const cricket::MediaConfig media_config =
2442 TestCreatePeerConnection(config, &constraints);
2443
2444 EXPECT_TRUE(media_config.video.suspend_below_min_bitrate);
nisse51542be2016-02-12 02:27:06 -08002445}
2446
deadbeefab9b2d12015-10-14 11:33:11 -07002447// The following tests verify that session options are created correctly.
deadbeefc80741f2015-10-22 13:14:45 -07002448// TODO(deadbeef): Convert these tests to be more end-to-end. Instead of
2449// "verify options are converted correctly", should be "pass options into
2450// CreateOffer and verify the correct offer is produced."
deadbeefab9b2d12015-10-14 11:33:11 -07002451
2452TEST(CreateSessionOptionsTest, GetOptionsForOfferWithInvalidAudioOption) {
2453 RTCOfferAnswerOptions rtc_options;
2454 rtc_options.offer_to_receive_audio = RTCOfferAnswerOptions::kUndefined - 1;
2455
2456 cricket::MediaSessionOptions options;
htaa2a49d92016-03-04 02:51:39 -08002457 EXPECT_FALSE(ExtractMediaSessionOptions(rtc_options, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002458
2459 rtc_options.offer_to_receive_audio =
2460 RTCOfferAnswerOptions::kMaxOfferToReceiveMedia + 1;
htaa2a49d92016-03-04 02:51:39 -08002461 EXPECT_FALSE(ExtractMediaSessionOptions(rtc_options, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002462}
2463
2464TEST(CreateSessionOptionsTest, GetOptionsForOfferWithInvalidVideoOption) {
2465 RTCOfferAnswerOptions rtc_options;
2466 rtc_options.offer_to_receive_video = RTCOfferAnswerOptions::kUndefined - 1;
2467
2468 cricket::MediaSessionOptions options;
htaa2a49d92016-03-04 02:51:39 -08002469 EXPECT_FALSE(ExtractMediaSessionOptions(rtc_options, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002470
2471 rtc_options.offer_to_receive_video =
2472 RTCOfferAnswerOptions::kMaxOfferToReceiveMedia + 1;
htaa2a49d92016-03-04 02:51:39 -08002473 EXPECT_FALSE(ExtractMediaSessionOptions(rtc_options, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002474}
2475
2476// Test that a MediaSessionOptions is created for an offer if
deadbeefc80741f2015-10-22 13:14:45 -07002477// OfferToReceiveAudio and OfferToReceiveVideo options are set.
deadbeefab9b2d12015-10-14 11:33:11 -07002478TEST(CreateSessionOptionsTest, GetMediaSessionOptionsForOfferWithAudioVideo) {
2479 RTCOfferAnswerOptions rtc_options;
2480 rtc_options.offer_to_receive_audio = 1;
2481 rtc_options.offer_to_receive_video = 1;
2482
2483 cricket::MediaSessionOptions options;
htaa2a49d92016-03-04 02:51:39 -08002484 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002485 EXPECT_TRUE(options.has_audio());
2486 EXPECT_TRUE(options.has_video());
2487 EXPECT_TRUE(options.bundle_enabled);
2488}
2489
2490// Test that a correct MediaSessionOptions is created for an offer if
deadbeefc80741f2015-10-22 13:14:45 -07002491// OfferToReceiveAudio is set.
deadbeefab9b2d12015-10-14 11:33:11 -07002492TEST(CreateSessionOptionsTest, GetMediaSessionOptionsForOfferWithAudio) {
2493 RTCOfferAnswerOptions rtc_options;
2494 rtc_options.offer_to_receive_audio = 1;
2495
2496 cricket::MediaSessionOptions options;
htaa2a49d92016-03-04 02:51:39 -08002497 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002498 EXPECT_TRUE(options.has_audio());
2499 EXPECT_FALSE(options.has_video());
2500 EXPECT_TRUE(options.bundle_enabled);
2501}
2502
2503// Test that a correct MediaSessionOptions is created for an offer if
deadbeefc80741f2015-10-22 13:14:45 -07002504// the default OfferOptions are used.
deadbeefab9b2d12015-10-14 11:33:11 -07002505TEST(CreateSessionOptionsTest, GetDefaultMediaSessionOptionsForOffer) {
2506 RTCOfferAnswerOptions rtc_options;
2507
2508 cricket::MediaSessionOptions options;
deadbeef0ed85b22016-02-23 17:24:52 -08002509 options.transport_options["audio"] = cricket::TransportOptions();
2510 options.transport_options["video"] = cricket::TransportOptions();
htaa2a49d92016-03-04 02:51:39 -08002511 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, &options));
deadbeefc80741f2015-10-22 13:14:45 -07002512 EXPECT_TRUE(options.has_audio());
deadbeefab9b2d12015-10-14 11:33:11 -07002513 EXPECT_FALSE(options.has_video());
deadbeefc80741f2015-10-22 13:14:45 -07002514 EXPECT_TRUE(options.bundle_enabled);
deadbeefab9b2d12015-10-14 11:33:11 -07002515 EXPECT_TRUE(options.vad_enabled);
deadbeef0ed85b22016-02-23 17:24:52 -08002516 EXPECT_FALSE(options.transport_options["audio"].ice_restart);
2517 EXPECT_FALSE(options.transport_options["video"].ice_restart);
deadbeefab9b2d12015-10-14 11:33:11 -07002518}
2519
2520// Test that a correct MediaSessionOptions is created for an offer if
deadbeefc80741f2015-10-22 13:14:45 -07002521// OfferToReceiveVideo is set.
deadbeefab9b2d12015-10-14 11:33:11 -07002522TEST(CreateSessionOptionsTest, GetMediaSessionOptionsForOfferWithVideo) {
2523 RTCOfferAnswerOptions rtc_options;
2524 rtc_options.offer_to_receive_audio = 0;
2525 rtc_options.offer_to_receive_video = 1;
2526
2527 cricket::MediaSessionOptions options;
htaa2a49d92016-03-04 02:51:39 -08002528 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002529 EXPECT_FALSE(options.has_audio());
2530 EXPECT_TRUE(options.has_video());
2531 EXPECT_TRUE(options.bundle_enabled);
2532}
2533
2534// Test that a correct MediaSessionOptions is created for an offer if
2535// UseRtpMux is set to false.
2536TEST(CreateSessionOptionsTest,
2537 GetMediaSessionOptionsForOfferWithBundleDisabled) {
2538 RTCOfferAnswerOptions rtc_options;
2539 rtc_options.offer_to_receive_audio = 1;
2540 rtc_options.offer_to_receive_video = 1;
2541 rtc_options.use_rtp_mux = false;
2542
2543 cricket::MediaSessionOptions options;
htaa2a49d92016-03-04 02:51:39 -08002544 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002545 EXPECT_TRUE(options.has_audio());
2546 EXPECT_TRUE(options.has_video());
2547 EXPECT_FALSE(options.bundle_enabled);
2548}
2549
2550// Test that a correct MediaSessionOptions is created to restart ice if
2551// IceRestart is set. It also tests that subsequent MediaSessionOptions don't
Taylor Brandstetterf475d362016-01-08 15:35:57 -08002552// have |audio_transport_options.ice_restart| etc. set.
deadbeefab9b2d12015-10-14 11:33:11 -07002553TEST(CreateSessionOptionsTest, GetMediaSessionOptionsForOfferWithIceRestart) {
2554 RTCOfferAnswerOptions rtc_options;
2555 rtc_options.ice_restart = true;
2556
2557 cricket::MediaSessionOptions options;
deadbeef0ed85b22016-02-23 17:24:52 -08002558 options.transport_options["audio"] = cricket::TransportOptions();
2559 options.transport_options["video"] = cricket::TransportOptions();
htaa2a49d92016-03-04 02:51:39 -08002560 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, &options));
deadbeef0ed85b22016-02-23 17:24:52 -08002561 EXPECT_TRUE(options.transport_options["audio"].ice_restart);
2562 EXPECT_TRUE(options.transport_options["video"].ice_restart);
deadbeefab9b2d12015-10-14 11:33:11 -07002563
2564 rtc_options = RTCOfferAnswerOptions();
htaa2a49d92016-03-04 02:51:39 -08002565 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, &options));
deadbeef0ed85b22016-02-23 17:24:52 -08002566 EXPECT_FALSE(options.transport_options["audio"].ice_restart);
2567 EXPECT_FALSE(options.transport_options["video"].ice_restart);
deadbeefab9b2d12015-10-14 11:33:11 -07002568}
2569
2570// Test that the MediaConstraints in an answer don't affect if audio and video
2571// is offered in an offer but that if kOfferToReceiveAudio or
2572// kOfferToReceiveVideo constraints are true in an offer, the media type will be
2573// included in subsequent answers.
2574TEST(CreateSessionOptionsTest, MediaConstraintsInAnswer) {
2575 FakeConstraints answer_c;
2576 answer_c.SetMandatoryReceiveAudio(true);
2577 answer_c.SetMandatoryReceiveVideo(true);
2578
2579 cricket::MediaSessionOptions answer_options;
2580 EXPECT_TRUE(ParseConstraintsForAnswer(&answer_c, &answer_options));
2581 EXPECT_TRUE(answer_options.has_audio());
2582 EXPECT_TRUE(answer_options.has_video());
2583
deadbeefc80741f2015-10-22 13:14:45 -07002584 RTCOfferAnswerOptions rtc_offer_options;
deadbeefab9b2d12015-10-14 11:33:11 -07002585
2586 cricket::MediaSessionOptions offer_options;
htaa2a49d92016-03-04 02:51:39 -08002587 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_offer_options, &offer_options));
deadbeefc80741f2015-10-22 13:14:45 -07002588 EXPECT_TRUE(offer_options.has_audio());
deadbeefab9b2d12015-10-14 11:33:11 -07002589 EXPECT_FALSE(offer_options.has_video());
2590
deadbeefc80741f2015-10-22 13:14:45 -07002591 RTCOfferAnswerOptions updated_rtc_offer_options;
2592 updated_rtc_offer_options.offer_to_receive_audio = 1;
2593 updated_rtc_offer_options.offer_to_receive_video = 1;
deadbeefab9b2d12015-10-14 11:33:11 -07002594
2595 cricket::MediaSessionOptions updated_offer_options;
htaa2a49d92016-03-04 02:51:39 -08002596 EXPECT_TRUE(ExtractMediaSessionOptions(updated_rtc_offer_options,
2597 &updated_offer_options));
deadbeefab9b2d12015-10-14 11:33:11 -07002598 EXPECT_TRUE(updated_offer_options.has_audio());
2599 EXPECT_TRUE(updated_offer_options.has_video());
2600
2601 // Since an offer has been created with both audio and video, subsequent
2602 // offers and answers should contain both audio and video.
2603 // Answers will only contain the media types that exist in the offer
2604 // regardless of the value of |updated_answer_options.has_audio| and
2605 // |updated_answer_options.has_video|.
2606 FakeConstraints updated_answer_c;
2607 answer_c.SetMandatoryReceiveAudio(false);
2608 answer_c.SetMandatoryReceiveVideo(false);
2609
2610 cricket::MediaSessionOptions updated_answer_options;
2611 EXPECT_TRUE(
2612 ParseConstraintsForAnswer(&updated_answer_c, &updated_answer_options));
2613 EXPECT_TRUE(updated_answer_options.has_audio());
2614 EXPECT_TRUE(updated_answer_options.has_video());
deadbeefab9b2d12015-10-14 11:33:11 -07002615}