blob: 0ec747845264fe2340489343aaf7ae11613b7602 [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
kwibergd1fe2812016-04-27 06:47:29 -070011#include <memory>
henrike@webrtc.org28e20752013-07-10 00:45:36 +000012#include <string>
kwiberg0eb15ed2015-12-17 03:04:15 -080013#include <utility>
henrike@webrtc.org28e20752013-07-10 00:45:36 +000014
perkjd61bf802016-03-24 03:16:19 -070015#include "testing/gmock/include/gmock/gmock.h"
Henrik Kjellander15583c12016-02-10 10:53:12 +010016#include "webrtc/api/audiotrack.h"
17#include "webrtc/api/jsepsessiondescription.h"
18#include "webrtc/api/mediastream.h"
19#include "webrtc/api/mediastreaminterface.h"
20#include "webrtc/api/peerconnection.h"
21#include "webrtc/api/peerconnectioninterface.h"
22#include "webrtc/api/rtpreceiverinterface.h"
23#include "webrtc/api/rtpsenderinterface.h"
24#include "webrtc/api/streamcollection.h"
25#ifdef WEBRTC_ANDROID
26#include "webrtc/api/test/androidtestinitializer.h"
27#endif
28#include "webrtc/api/test/fakeconstraints.h"
29#include "webrtc/api/test/fakedtlsidentitystore.h"
nisseaf510af2016-03-21 08:20:42 -070030#include "webrtc/api/test/fakevideotracksource.h"
Henrik Kjellander15583c12016-02-10 10:53:12 +010031#include "webrtc/api/test/mockpeerconnectionobservers.h"
32#include "webrtc/api/test/testsdpstrings.h"
perkja3ede6c2016-03-08 01:27:48 +010033#include "webrtc/api/videocapturertracksource.h"
Henrik Kjellander15583c12016-02-10 10:53:12 +010034#include "webrtc/api/videotrack.h"
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000035#include "webrtc/base/gunit.h"
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000036#include "webrtc/base/ssladapter.h"
37#include "webrtc/base/sslstreamadapter.h"
38#include "webrtc/base/stringutils.h"
39#include "webrtc/base/thread.h"
kjellandera96e2d72016-02-04 23:52:28 -080040#include "webrtc/media/base/fakevideocapturer.h"
41#include "webrtc/media/sctp/sctpdataengine.h"
Taylor Brandstettera1c30352016-05-13 08:15:11 -070042#include "webrtc/p2p/base/fakeportallocator.h"
kjellander@webrtc.org9b8df252016-02-12 06:47:59 +010043#include "webrtc/pc/mediasession.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000044
45static const char kStreamLabel1[] = "local_stream_1";
46static const char kStreamLabel2[] = "local_stream_2";
47static const char kStreamLabel3[] = "local_stream_3";
48static const int kDefaultStunPort = 3478;
49static const char kStunAddressOnly[] = "stun:address";
50static const char kStunInvalidPort[] = "stun:address:-1";
51static const char kStunAddressPortAndMore1[] = "stun:address:port:more";
52static const char kStunAddressPortAndMore2[] = "stun:address:port more";
53static const char kTurnIceServerUri[] = "turn:user@turn.example.org";
54static const char kTurnUsername[] = "user";
55static const char kTurnPassword[] = "password";
56static const char kTurnHostname[] = "turn.example.org";
Peter Boström0c4e06b2015-10-07 12:23:21 +020057static const uint32_t kTimeout = 10000U;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000058
deadbeefab9b2d12015-10-14 11:33:11 -070059static const char kStreams[][8] = {"stream1", "stream2"};
60static const char kAudioTracks[][32] = {"audiotrack0", "audiotrack1"};
61static const char kVideoTracks[][32] = {"videotrack0", "videotrack1"};
62
deadbeef5e97fb52015-10-15 12:49:08 -070063static const char kRecvonly[] = "recvonly";
64static const char kSendrecv[] = "sendrecv";
65
deadbeefab9b2d12015-10-14 11:33:11 -070066// Reference SDP with a MediaStream with label "stream1" and audio track with
67// id "audio_1" and a video track with id "video_1;
68static const char kSdpStringWithStream1[] =
69 "v=0\r\n"
70 "o=- 0 0 IN IP4 127.0.0.1\r\n"
71 "s=-\r\n"
72 "t=0 0\r\n"
73 "a=ice-ufrag:e5785931\r\n"
74 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
75 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
76 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
77 "m=audio 1 RTP/AVPF 103\r\n"
78 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -070079 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -070080 "a=rtpmap:103 ISAC/16000\r\n"
81 "a=ssrc:1 cname:stream1\r\n"
82 "a=ssrc:1 mslabel:stream1\r\n"
83 "a=ssrc:1 label:audiotrack0\r\n"
84 "m=video 1 RTP/AVPF 120\r\n"
85 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -070086 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -070087 "a=rtpmap:120 VP8/90000\r\n"
88 "a=ssrc:2 cname:stream1\r\n"
89 "a=ssrc:2 mslabel:stream1\r\n"
90 "a=ssrc:2 label:videotrack0\r\n";
91
92// Reference SDP with two MediaStreams with label "stream1" and "stream2. Each
93// MediaStreams have one audio track and one video track.
94// This uses MSID.
95static const char kSdpStringWithStream1And2[] =
96 "v=0\r\n"
97 "o=- 0 0 IN IP4 127.0.0.1\r\n"
98 "s=-\r\n"
99 "t=0 0\r\n"
100 "a=ice-ufrag:e5785931\r\n"
101 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
102 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
103 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
104 "a=msid-semantic: WMS stream1 stream2\r\n"
105 "m=audio 1 RTP/AVPF 103\r\n"
106 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700107 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700108 "a=rtpmap:103 ISAC/16000\r\n"
109 "a=ssrc:1 cname:stream1\r\n"
110 "a=ssrc:1 msid:stream1 audiotrack0\r\n"
111 "a=ssrc:3 cname:stream2\r\n"
112 "a=ssrc:3 msid:stream2 audiotrack1\r\n"
113 "m=video 1 RTP/AVPF 120\r\n"
114 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700115 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700116 "a=rtpmap:120 VP8/0\r\n"
117 "a=ssrc:2 cname:stream1\r\n"
118 "a=ssrc:2 msid:stream1 videotrack0\r\n"
119 "a=ssrc:4 cname:stream2\r\n"
120 "a=ssrc:4 msid:stream2 videotrack1\r\n";
121
122// Reference SDP without MediaStreams. Msid is not supported.
123static const char kSdpStringWithoutStreams[] =
124 "v=0\r\n"
125 "o=- 0 0 IN IP4 127.0.0.1\r\n"
126 "s=-\r\n"
127 "t=0 0\r\n"
128 "a=ice-ufrag:e5785931\r\n"
129 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
130 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
131 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
132 "m=audio 1 RTP/AVPF 103\r\n"
133 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700134 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700135 "a=rtpmap:103 ISAC/16000\r\n"
136 "m=video 1 RTP/AVPF 120\r\n"
137 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700138 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700139 "a=rtpmap:120 VP8/90000\r\n";
140
141// Reference SDP without MediaStreams. Msid is supported.
142static const char kSdpStringWithMsidWithoutStreams[] =
143 "v=0\r\n"
144 "o=- 0 0 IN IP4 127.0.0.1\r\n"
145 "s=-\r\n"
146 "t=0 0\r\n"
147 "a=ice-ufrag:e5785931\r\n"
148 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
149 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
150 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
151 "a=msid-semantic: WMS\r\n"
152 "m=audio 1 RTP/AVPF 103\r\n"
153 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700154 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700155 "a=rtpmap:103 ISAC/16000\r\n"
156 "m=video 1 RTP/AVPF 120\r\n"
157 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700158 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700159 "a=rtpmap:120 VP8/90000\r\n";
160
161// Reference SDP without MediaStreams and audio only.
162static const char kSdpStringWithoutStreamsAudioOnly[] =
163 "v=0\r\n"
164 "o=- 0 0 IN IP4 127.0.0.1\r\n"
165 "s=-\r\n"
166 "t=0 0\r\n"
167 "a=ice-ufrag:e5785931\r\n"
168 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
169 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
170 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
171 "m=audio 1 RTP/AVPF 103\r\n"
172 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700173 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700174 "a=rtpmap:103 ISAC/16000\r\n";
175
176// Reference SENDONLY SDP without MediaStreams. Msid is not supported.
177static const char kSdpStringSendOnlyWithoutStreams[] =
178 "v=0\r\n"
179 "o=- 0 0 IN IP4 127.0.0.1\r\n"
180 "s=-\r\n"
181 "t=0 0\r\n"
182 "a=ice-ufrag:e5785931\r\n"
183 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
184 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
185 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
186 "m=audio 1 RTP/AVPF 103\r\n"
187 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700188 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700189 "a=sendonly\r\n"
190 "a=rtpmap:103 ISAC/16000\r\n"
191 "m=video 1 RTP/AVPF 120\r\n"
192 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700193 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700194 "a=sendonly\r\n"
195 "a=rtpmap:120 VP8/90000\r\n";
196
197static const char kSdpStringInit[] =
198 "v=0\r\n"
199 "o=- 0 0 IN IP4 127.0.0.1\r\n"
200 "s=-\r\n"
201 "t=0 0\r\n"
202 "a=ice-ufrag:e5785931\r\n"
203 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
204 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
205 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
206 "a=msid-semantic: WMS\r\n";
207
208static const char kSdpStringAudio[] =
209 "m=audio 1 RTP/AVPF 103\r\n"
210 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700211 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700212 "a=rtpmap:103 ISAC/16000\r\n";
213
214static const char kSdpStringVideo[] =
215 "m=video 1 RTP/AVPF 120\r\n"
216 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700217 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700218 "a=rtpmap:120 VP8/90000\r\n";
219
220static const char kSdpStringMs1Audio0[] =
221 "a=ssrc:1 cname:stream1\r\n"
222 "a=ssrc:1 msid:stream1 audiotrack0\r\n";
223
224static const char kSdpStringMs1Video0[] =
225 "a=ssrc:2 cname:stream1\r\n"
226 "a=ssrc:2 msid:stream1 videotrack0\r\n";
227
228static const char kSdpStringMs1Audio1[] =
229 "a=ssrc:3 cname:stream1\r\n"
230 "a=ssrc:3 msid:stream1 audiotrack1\r\n";
231
232static const char kSdpStringMs1Video1[] =
233 "a=ssrc:4 cname:stream1\r\n"
234 "a=ssrc:4 msid:stream1 videotrack1\r\n";
235
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000236#define MAYBE_SKIP_TEST(feature) \
237 if (!(feature())) { \
238 LOG(LS_INFO) << "Feature disabled... skipping"; \
239 return; \
240 }
241
perkjd61bf802016-03-24 03:16:19 -0700242using ::testing::Exactly;
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700243using cricket::StreamParams;
244using rtc::scoped_refptr;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000245using webrtc::AudioSourceInterface;
deadbeefab9b2d12015-10-14 11:33:11 -0700246using webrtc::AudioTrack;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000247using webrtc::AudioTrackInterface;
248using webrtc::DataBuffer;
249using webrtc::DataChannelInterface;
250using webrtc::FakeConstraints;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000251using webrtc::IceCandidateInterface;
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700252using webrtc::JsepSessionDescription;
deadbeefc80741f2015-10-22 13:14:45 -0700253using webrtc::MediaConstraintsInterface;
deadbeefab9b2d12015-10-14 11:33:11 -0700254using webrtc::MediaStream;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000255using webrtc::MediaStreamInterface;
256using webrtc::MediaStreamTrackInterface;
257using webrtc::MockCreateSessionDescriptionObserver;
258using webrtc::MockDataChannelObserver;
259using webrtc::MockSetSessionDescriptionObserver;
260using webrtc::MockStatsObserver;
perkjd61bf802016-03-24 03:16:19 -0700261using webrtc::NotifierInterface;
262using webrtc::ObserverInterface;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000263using webrtc::PeerConnectionInterface;
264using webrtc::PeerConnectionObserver;
deadbeefab9b2d12015-10-14 11:33:11 -0700265using webrtc::RtpReceiverInterface;
266using webrtc::RtpSenderInterface;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000267using webrtc::SdpParseError;
268using webrtc::SessionDescriptionInterface;
deadbeefab9b2d12015-10-14 11:33:11 -0700269using webrtc::StreamCollection;
270using webrtc::StreamCollectionInterface;
perkja3ede6c2016-03-08 01:27:48 +0100271using webrtc::VideoTrackSourceInterface;
deadbeefab9b2d12015-10-14 11:33:11 -0700272using webrtc::VideoTrack;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000273using webrtc::VideoTrackInterface;
274
deadbeefab9b2d12015-10-14 11:33:11 -0700275typedef PeerConnectionInterface::RTCOfferAnswerOptions RTCOfferAnswerOptions;
276
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000277namespace {
278
279// Gets the first ssrc of given content type from the ContentInfo.
280bool GetFirstSsrc(const cricket::ContentInfo* content_info, int* ssrc) {
281 if (!content_info || !ssrc) {
282 return false;
283 }
284 const cricket::MediaContentDescription* media_desc =
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000285 static_cast<const cricket::MediaContentDescription*>(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000286 content_info->description);
287 if (!media_desc || media_desc->streams().empty()) {
288 return false;
289 }
290 *ssrc = media_desc->streams().begin()->first_ssrc();
291 return true;
292}
293
294void SetSsrcToZero(std::string* sdp) {
295 const char kSdpSsrcAtribute[] = "a=ssrc:";
296 const char kSdpSsrcAtributeZero[] = "a=ssrc:0";
297 size_t ssrc_pos = 0;
298 while ((ssrc_pos = sdp->find(kSdpSsrcAtribute, ssrc_pos)) !=
299 std::string::npos) {
300 size_t end_ssrc = sdp->find(" ", ssrc_pos);
301 sdp->replace(ssrc_pos, end_ssrc - ssrc_pos, kSdpSsrcAtributeZero);
302 ssrc_pos = end_ssrc;
303 }
304}
305
deadbeefab9b2d12015-10-14 11:33:11 -0700306// Check if |streams| contains the specified track.
307bool ContainsTrack(const std::vector<cricket::StreamParams>& streams,
308 const std::string& stream_label,
309 const std::string& track_id) {
310 for (const cricket::StreamParams& params : streams) {
311 if (params.sync_label == stream_label && params.id == track_id) {
312 return true;
313 }
314 }
315 return false;
316}
317
318// Check if |senders| contains the specified sender, by id.
319bool ContainsSender(
320 const std::vector<rtc::scoped_refptr<RtpSenderInterface>>& senders,
321 const std::string& id) {
322 for (const auto& sender : senders) {
323 if (sender->id() == id) {
324 return true;
325 }
326 }
327 return false;
328}
329
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700330// Check if |senders| contains the specified sender, by id and stream id.
331bool ContainsSender(
332 const std::vector<rtc::scoped_refptr<RtpSenderInterface>>& senders,
333 const std::string& id,
334 const std::string& stream_id) {
335 for (const auto& sender : senders) {
336 if (sender->id() == id && sender->stream_id() == stream_id) {
337 return true;
338 }
339 }
340 return false;
341}
342
deadbeefab9b2d12015-10-14 11:33:11 -0700343// Create a collection of streams.
344// CreateStreamCollection(1) creates a collection that
345// correspond to kSdpStringWithStream1.
346// CreateStreamCollection(2) correspond to kSdpStringWithStream1And2.
347rtc::scoped_refptr<StreamCollection> CreateStreamCollection(
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700348 int number_of_streams,
349 int tracks_per_stream) {
deadbeefab9b2d12015-10-14 11:33:11 -0700350 rtc::scoped_refptr<StreamCollection> local_collection(
351 StreamCollection::Create());
352
353 for (int i = 0; i < number_of_streams; ++i) {
354 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream(
355 webrtc::MediaStream::Create(kStreams[i]));
356
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700357 for (int j = 0; j < tracks_per_stream; ++j) {
358 // Add a local audio track.
359 rtc::scoped_refptr<webrtc::AudioTrackInterface> audio_track(
360 webrtc::AudioTrack::Create(kAudioTracks[i * tracks_per_stream + j],
361 nullptr));
362 stream->AddTrack(audio_track);
deadbeefab9b2d12015-10-14 11:33:11 -0700363
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700364 // Add a local video track.
365 rtc::scoped_refptr<webrtc::VideoTrackInterface> video_track(
366 webrtc::VideoTrack::Create(kVideoTracks[i * tracks_per_stream + j],
367 webrtc::FakeVideoTrackSource::Create()));
368 stream->AddTrack(video_track);
369 }
deadbeefab9b2d12015-10-14 11:33:11 -0700370
371 local_collection->AddStream(stream);
372 }
373 return local_collection;
374}
375
376// Check equality of StreamCollections.
377bool CompareStreamCollections(StreamCollectionInterface* s1,
378 StreamCollectionInterface* s2) {
379 if (s1 == nullptr || s2 == nullptr || s1->count() != s2->count()) {
380 return false;
381 }
382
383 for (size_t i = 0; i != s1->count(); ++i) {
384 if (s1->at(i)->label() != s2->at(i)->label()) {
385 return false;
386 }
387 webrtc::AudioTrackVector audio_tracks1 = s1->at(i)->GetAudioTracks();
388 webrtc::AudioTrackVector audio_tracks2 = s2->at(i)->GetAudioTracks();
389 webrtc::VideoTrackVector video_tracks1 = s1->at(i)->GetVideoTracks();
390 webrtc::VideoTrackVector video_tracks2 = s2->at(i)->GetVideoTracks();
391
392 if (audio_tracks1.size() != audio_tracks2.size()) {
393 return false;
394 }
395 for (size_t j = 0; j != audio_tracks1.size(); ++j) {
396 if (audio_tracks1[j]->id() != audio_tracks2[j]->id()) {
397 return false;
398 }
399 }
400 if (video_tracks1.size() != video_tracks2.size()) {
401 return false;
402 }
403 for (size_t j = 0; j != video_tracks1.size(); ++j) {
404 if (video_tracks1[j]->id() != video_tracks2[j]->id()) {
405 return false;
406 }
407 }
408 }
409 return true;
410}
411
perkjd61bf802016-03-24 03:16:19 -0700412// Helper class to test Observer.
413class MockTrackObserver : public ObserverInterface {
414 public:
415 explicit MockTrackObserver(NotifierInterface* notifier)
416 : notifier_(notifier) {
417 notifier_->RegisterObserver(this);
418 }
419
420 ~MockTrackObserver() { Unregister(); }
421
422 void Unregister() {
423 if (notifier_) {
424 notifier_->UnregisterObserver(this);
425 notifier_ = nullptr;
426 }
427 }
428
429 MOCK_METHOD0(OnChanged, void());
430
431 private:
432 NotifierInterface* notifier_;
433};
434
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000435class MockPeerConnectionObserver : public PeerConnectionObserver {
436 public:
deadbeefab9b2d12015-10-14 11:33:11 -0700437 MockPeerConnectionObserver() : remote_streams_(StreamCollection::Create()) {}
Henrik Kjellander3fe372d2016-05-12 08:10:52 +0200438 virtual ~MockPeerConnectionObserver() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000439 }
440 void SetPeerConnectionInterface(PeerConnectionInterface* pc) {
441 pc_ = pc;
442 if (pc) {
443 state_ = pc_->signaling_state();
444 }
445 }
nisseef8b61e2016-04-29 06:09:15 -0700446 void OnSignalingChange(
447 PeerConnectionInterface::SignalingState new_state) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000448 EXPECT_EQ(pc_->signaling_state(), new_state);
449 state_ = new_state;
450 }
451 // TODO(bemasc): Remove this once callers transition to OnIceGatheringChange.
452 virtual void OnStateChange(StateType state_changed) {
453 if (pc_.get() == NULL)
454 return;
455 switch (state_changed) {
456 case kSignalingState:
457 // OnSignalingChange and OnStateChange(kSignalingState) should always
458 // be called approximately simultaneously. To ease testing, we require
459 // that they always be called in that order. This check verifies
460 // that OnSignalingChange has just been called.
461 EXPECT_EQ(pc_->signaling_state(), state_);
462 break;
463 case kIceState:
464 ADD_FAILURE();
465 break;
466 default:
467 ADD_FAILURE();
468 break;
469 }
470 }
deadbeefab9b2d12015-10-14 11:33:11 -0700471
472 MediaStreamInterface* RemoteStream(const std::string& label) {
473 return remote_streams_->find(label);
474 }
475 StreamCollectionInterface* remote_streams() const { return remote_streams_; }
Taylor Brandstetter98cde262016-05-31 13:02:21 -0700476 void OnAddStream(rtc::scoped_refptr<MediaStreamInterface> stream) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000477 last_added_stream_ = stream;
deadbeefab9b2d12015-10-14 11:33:11 -0700478 remote_streams_->AddStream(stream);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000479 }
Taylor Brandstetter98cde262016-05-31 13:02:21 -0700480 void OnRemoveStream(
481 rtc::scoped_refptr<MediaStreamInterface> stream) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000482 last_removed_stream_ = stream;
deadbeefab9b2d12015-10-14 11:33:11 -0700483 remote_streams_->RemoveStream(stream);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000484 }
perkjdfb769d2016-02-09 03:09:43 -0800485 void OnRenegotiationNeeded() override { renegotiation_needed_ = true; }
Taylor Brandstetter98cde262016-05-31 13:02:21 -0700486 void OnDataChannel(
487 rtc::scoped_refptr<DataChannelInterface> data_channel) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000488 last_datachannel_ = data_channel;
489 }
490
perkjdfb769d2016-02-09 03:09:43 -0800491 void OnIceConnectionChange(
492 PeerConnectionInterface::IceConnectionState new_state) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000493 EXPECT_EQ(pc_->ice_connection_state(), new_state);
494 }
perkjdfb769d2016-02-09 03:09:43 -0800495 void OnIceGatheringChange(
496 PeerConnectionInterface::IceGatheringState new_state) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000497 EXPECT_EQ(pc_->ice_gathering_state(), new_state);
perkjdfb769d2016-02-09 03:09:43 -0800498 ice_complete_ = new_state == PeerConnectionInterface::kIceGatheringComplete;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000499 }
perkjdfb769d2016-02-09 03:09:43 -0800500 void OnIceCandidate(const webrtc::IceCandidateInterface* candidate) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000501 EXPECT_NE(PeerConnectionInterface::kIceGatheringNew,
502 pc_->ice_gathering_state());
503
504 std::string sdp;
505 EXPECT_TRUE(candidate->ToString(&sdp));
506 EXPECT_LT(0u, sdp.size());
507 last_candidate_.reset(webrtc::CreateIceCandidate(candidate->sdp_mid(),
508 candidate->sdp_mline_index(), sdp, NULL));
509 EXPECT_TRUE(last_candidate_.get() != NULL);
510 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000511
512 // Returns the label of the last added stream.
513 // Empty string if no stream have been added.
514 std::string GetLastAddedStreamLabel() {
515 if (last_added_stream_.get())
516 return last_added_stream_->label();
517 return "";
518 }
519 std::string GetLastRemovedStreamLabel() {
520 if (last_removed_stream_.get())
521 return last_removed_stream_->label();
522 return "";
523 }
524
525 scoped_refptr<PeerConnectionInterface> pc_;
526 PeerConnectionInterface::SignalingState state_;
kwibergd1fe2812016-04-27 06:47:29 -0700527 std::unique_ptr<IceCandidateInterface> last_candidate_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000528 scoped_refptr<DataChannelInterface> last_datachannel_;
deadbeefab9b2d12015-10-14 11:33:11 -0700529 rtc::scoped_refptr<StreamCollection> remote_streams_;
530 bool renegotiation_needed_ = false;
531 bool ice_complete_ = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000532
533 private:
534 scoped_refptr<MediaStreamInterface> last_added_stream_;
535 scoped_refptr<MediaStreamInterface> last_removed_stream_;
536};
537
538} // namespace
deadbeefab9b2d12015-10-14 11:33:11 -0700539
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000540class PeerConnectionInterfaceTest : public testing::Test {
541 protected:
phoglund37ebcf02016-01-08 05:04:57 -0800542 PeerConnectionInterfaceTest() {
543#ifdef WEBRTC_ANDROID
544 webrtc::InitializeAndroidObjects();
545#endif
546 }
547
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000548 virtual void SetUp() {
549 pc_factory_ = webrtc::CreatePeerConnectionFactory(
danilchape9021a32016-05-17 01:52:02 -0700550 rtc::Thread::Current(), rtc::Thread::Current(), rtc::Thread::Current(),
551 nullptr, nullptr, nullptr);
552 ASSERT_TRUE(pc_factory_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000553 }
554
555 void CreatePeerConnection() {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700556 CreatePeerConnection(PeerConnectionInterface::RTCConfiguration(), nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000557 }
558
559 void CreatePeerConnection(webrtc::MediaConstraintsInterface* constraints) {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700560 CreatePeerConnection(PeerConnectionInterface::RTCConfiguration(),
561 constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000562 }
563
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700564 void CreatePeerConnectionWithIceTransportsType(
565 PeerConnectionInterface::IceTransportsType type) {
566 PeerConnectionInterface::RTCConfiguration config;
567 config.type = type;
568 return CreatePeerConnection(config, nullptr);
569 }
570
571 void CreatePeerConnectionWithIceServer(const std::string& uri,
572 const std::string& password) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800573 PeerConnectionInterface::RTCConfiguration config;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000574 PeerConnectionInterface::IceServer server;
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700575 server.uri = uri;
576 server.password = password;
577 config.servers.push_back(server);
578 CreatePeerConnection(config, nullptr);
579 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000580
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700581 void CreatePeerConnection(PeerConnectionInterface::RTCConfiguration config,
582 webrtc::MediaConstraintsInterface* constraints) {
kwibergd1fe2812016-04-27 06:47:29 -0700583 std::unique_ptr<cricket::FakePortAllocator> port_allocator(
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800584 new cricket::FakePortAllocator(rtc::Thread::Current(), nullptr));
585 port_allocator_ = port_allocator.get();
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000586
buildbot@webrtc.org61c1b8e2014-04-09 06:06:38 +0000587 // DTLS does not work in a loopback call, so is disabled for most of the
588 // tests in this file. We only create a FakeIdentityService if the test
589 // explicitly sets the constraint.
jiayl@webrtc.org61e00b02015-03-04 22:17:38 +0000590 FakeConstraints default_constraints;
591 if (!constraints) {
592 constraints = &default_constraints;
593
594 default_constraints.AddMandatory(
595 webrtc::MediaConstraintsInterface::kEnableDtlsSrtp, false);
596 }
597
kwibergd1fe2812016-04-27 06:47:29 -0700598 std::unique_ptr<webrtc::DtlsIdentityStoreInterface> dtls_identity_store;
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000599 bool dtls;
600 if (FindConstraint(constraints,
601 webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
602 &dtls,
Henrik Boström5e56c592015-08-11 10:33:13 +0200603 nullptr) && dtls) {
604 dtls_identity_store.reset(new FakeDtlsIdentityStore());
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000605 }
kwiberg0eb15ed2015-12-17 03:04:15 -0800606 pc_ = pc_factory_->CreatePeerConnection(
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800607 config, constraints, std::move(port_allocator),
kwiberg0eb15ed2015-12-17 03:04:15 -0800608 std::move(dtls_identity_store), &observer_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000609 ASSERT_TRUE(pc_.get() != NULL);
610 observer_.SetPeerConnectionInterface(pc_.get());
611 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
612 }
613
deadbeef0a6c4ca2015-10-06 11:38:28 -0700614 void CreatePeerConnectionExpectFail(const std::string& uri) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800615 PeerConnectionInterface::RTCConfiguration config;
deadbeef0a6c4ca2015-10-06 11:38:28 -0700616 PeerConnectionInterface::IceServer server;
deadbeef0a6c4ca2015-10-06 11:38:28 -0700617 server.uri = uri;
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800618 config.servers.push_back(server);
deadbeef0a6c4ca2015-10-06 11:38:28 -0700619
deadbeef0a6c4ca2015-10-06 11:38:28 -0700620 scoped_refptr<PeerConnectionInterface> pc;
hbosd7973cc2016-05-27 06:08:53 -0700621 pc = pc_factory_->CreatePeerConnection(config, nullptr, nullptr, nullptr,
622 &observer_);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800623 EXPECT_EQ(nullptr, pc);
deadbeef0a6c4ca2015-10-06 11:38:28 -0700624 }
625
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000626 void CreatePeerConnectionWithDifferentConfigurations() {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700627 CreatePeerConnectionWithIceServer(kStunAddressOnly, "");
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800628 EXPECT_EQ(1u, port_allocator_->stun_servers().size());
629 EXPECT_EQ(0u, port_allocator_->turn_servers().size());
630 EXPECT_EQ("address", port_allocator_->stun_servers().begin()->hostname());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000631 EXPECT_EQ(kDefaultStunPort,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800632 port_allocator_->stun_servers().begin()->port());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000633
deadbeef0a6c4ca2015-10-06 11:38:28 -0700634 CreatePeerConnectionExpectFail(kStunInvalidPort);
635 CreatePeerConnectionExpectFail(kStunAddressPortAndMore1);
636 CreatePeerConnectionExpectFail(kStunAddressPortAndMore2);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000637
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700638 CreatePeerConnectionWithIceServer(kTurnIceServerUri, kTurnPassword);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800639 EXPECT_EQ(0u, port_allocator_->stun_servers().size());
640 EXPECT_EQ(1u, port_allocator_->turn_servers().size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000641 EXPECT_EQ(kTurnUsername,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800642 port_allocator_->turn_servers()[0].credentials.username);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000643 EXPECT_EQ(kTurnPassword,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800644 port_allocator_->turn_servers()[0].credentials.password);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000645 EXPECT_EQ(kTurnHostname,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800646 port_allocator_->turn_servers()[0].ports[0].address.hostname());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000647 }
648
649 void ReleasePeerConnection() {
650 pc_ = NULL;
651 observer_.SetPeerConnectionInterface(NULL);
652 }
653
deadbeefab9b2d12015-10-14 11:33:11 -0700654 void AddVideoStream(const std::string& label) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000655 // Create a local stream.
656 scoped_refptr<MediaStreamInterface> stream(
657 pc_factory_->CreateLocalMediaStream(label));
perkja3ede6c2016-03-08 01:27:48 +0100658 scoped_refptr<VideoTrackSourceInterface> video_source(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000659 pc_factory_->CreateVideoSource(new cricket::FakeVideoCapturer(), NULL));
660 scoped_refptr<VideoTrackInterface> video_track(
661 pc_factory_->CreateVideoTrack(label + "v0", video_source));
662 stream->AddTrack(video_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +0000663 EXPECT_TRUE(pc_->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000664 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
665 observer_.renegotiation_needed_ = false;
666 }
667
668 void AddVoiceStream(const std::string& label) {
669 // Create a local stream.
670 scoped_refptr<MediaStreamInterface> stream(
671 pc_factory_->CreateLocalMediaStream(label));
672 scoped_refptr<AudioTrackInterface> audio_track(
673 pc_factory_->CreateAudioTrack(label + "a0", NULL));
674 stream->AddTrack(audio_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +0000675 EXPECT_TRUE(pc_->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000676 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
677 observer_.renegotiation_needed_ = false;
678 }
679
680 void AddAudioVideoStream(const std::string& stream_label,
681 const std::string& audio_track_label,
682 const std::string& video_track_label) {
683 // Create a local stream.
684 scoped_refptr<MediaStreamInterface> stream(
685 pc_factory_->CreateLocalMediaStream(stream_label));
686 scoped_refptr<AudioTrackInterface> audio_track(
687 pc_factory_->CreateAudioTrack(
688 audio_track_label, static_cast<AudioSourceInterface*>(NULL)));
689 stream->AddTrack(audio_track.get());
690 scoped_refptr<VideoTrackInterface> video_track(
nisseaf510af2016-03-21 08:20:42 -0700691 pc_factory_->CreateVideoTrack(
692 video_track_label,
693 pc_factory_->CreateVideoSource(new cricket::FakeVideoCapturer())));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000694 stream->AddTrack(video_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +0000695 EXPECT_TRUE(pc_->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000696 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
697 observer_.renegotiation_needed_ = false;
698 }
699
kwibergd1fe2812016-04-27 06:47:29 -0700700 bool DoCreateOfferAnswer(std::unique_ptr<SessionDescriptionInterface>* desc,
deadbeefc80741f2015-10-22 13:14:45 -0700701 bool offer,
702 MediaConstraintsInterface* constraints) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000703 rtc::scoped_refptr<MockCreateSessionDescriptionObserver>
704 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000705 MockCreateSessionDescriptionObserver>());
706 if (offer) {
deadbeefc80741f2015-10-22 13:14:45 -0700707 pc_->CreateOffer(observer, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000708 } else {
deadbeefc80741f2015-10-22 13:14:45 -0700709 pc_->CreateAnswer(observer, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000710 }
711 EXPECT_EQ_WAIT(true, observer->called(), kTimeout);
kwiberg2bbff992016-03-16 11:03:04 -0700712 desc->reset(observer->release_desc());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000713 return observer->result();
714 }
715
kwibergd1fe2812016-04-27 06:47:29 -0700716 bool DoCreateOffer(std::unique_ptr<SessionDescriptionInterface>* desc,
deadbeefc80741f2015-10-22 13:14:45 -0700717 MediaConstraintsInterface* constraints) {
718 return DoCreateOfferAnswer(desc, true, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000719 }
720
kwibergd1fe2812016-04-27 06:47:29 -0700721 bool DoCreateAnswer(std::unique_ptr<SessionDescriptionInterface>* desc,
deadbeefc80741f2015-10-22 13:14:45 -0700722 MediaConstraintsInterface* constraints) {
723 return DoCreateOfferAnswer(desc, false, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000724 }
725
726 bool DoSetSessionDescription(SessionDescriptionInterface* desc, bool local) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000727 rtc::scoped_refptr<MockSetSessionDescriptionObserver>
728 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000729 MockSetSessionDescriptionObserver>());
730 if (local) {
731 pc_->SetLocalDescription(observer, desc);
732 } else {
733 pc_->SetRemoteDescription(observer, desc);
734 }
735 EXPECT_EQ_WAIT(true, observer->called(), kTimeout);
736 return observer->result();
737 }
738
739 bool DoSetLocalDescription(SessionDescriptionInterface* desc) {
740 return DoSetSessionDescription(desc, true);
741 }
742
743 bool DoSetRemoteDescription(SessionDescriptionInterface* desc) {
744 return DoSetSessionDescription(desc, false);
745 }
746
747 // Calls PeerConnection::GetStats and check the return value.
748 // It does not verify the values in the StatReports since a RTCP packet might
749 // be required.
750 bool DoGetStats(MediaStreamTrackInterface* track) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000751 rtc::scoped_refptr<MockStatsObserver> observer(
752 new rtc::RefCountedObject<MockStatsObserver>());
jiayl@webrtc.orgdb41b4d2014-03-03 21:30:06 +0000753 if (!pc_->GetStats(
754 observer, track, PeerConnectionInterface::kStatsOutputLevelStandard))
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000755 return false;
756 EXPECT_TRUE_WAIT(observer->called(), kTimeout);
757 return observer->called();
758 }
759
760 void InitiateCall() {
761 CreatePeerConnection();
762 // Create a local stream with audio&video tracks.
763 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
764 CreateOfferReceiveAnswer();
765 }
766
767 // Verify that RTP Header extensions has been negotiated for audio and video.
768 void VerifyRemoteRtpHeaderExtensions() {
769 const cricket::MediaContentDescription* desc =
770 cricket::GetFirstAudioContentDescription(
771 pc_->remote_description()->description());
772 ASSERT_TRUE(desc != NULL);
773 EXPECT_GT(desc->rtp_header_extensions().size(), 0u);
774
775 desc = cricket::GetFirstVideoContentDescription(
776 pc_->remote_description()->description());
777 ASSERT_TRUE(desc != NULL);
778 EXPECT_GT(desc->rtp_header_extensions().size(), 0u);
779 }
780
781 void CreateOfferAsRemoteDescription() {
kwibergd1fe2812016-04-27 06:47:29 -0700782 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -0700783 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000784 std::string sdp;
785 EXPECT_TRUE(offer->ToString(&sdp));
786 SessionDescriptionInterface* remote_offer =
787 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
788 sdp, NULL);
789 EXPECT_TRUE(DoSetRemoteDescription(remote_offer));
790 EXPECT_EQ(PeerConnectionInterface::kHaveRemoteOffer, observer_.state_);
791 }
792
deadbeefab9b2d12015-10-14 11:33:11 -0700793 void CreateAndSetRemoteOffer(const std::string& sdp) {
794 SessionDescriptionInterface* remote_offer =
795 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
796 sdp, nullptr);
797 EXPECT_TRUE(DoSetRemoteDescription(remote_offer));
798 EXPECT_EQ(PeerConnectionInterface::kHaveRemoteOffer, observer_.state_);
799 }
800
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000801 void CreateAnswerAsLocalDescription() {
kwibergd1fe2812016-04-27 06:47:29 -0700802 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -0700803 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000804
805 // TODO(perkj): Currently SetLocalDescription fails if any parameters in an
806 // audio codec change, even if the parameter has nothing to do with
807 // receiving. Not all parameters are serialized to SDP.
808 // Since CreatePrAnswerAsLocalDescription serialize/deserialize
809 // the SessionDescription, it is necessary to do that here to in order to
810 // get ReceiveOfferCreatePrAnswerAndAnswer and RenegotiateAudioOnly to pass.
811 // https://code.google.com/p/webrtc/issues/detail?id=1356
812 std::string sdp;
813 EXPECT_TRUE(answer->ToString(&sdp));
814 SessionDescriptionInterface* new_answer =
815 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
816 sdp, NULL);
817 EXPECT_TRUE(DoSetLocalDescription(new_answer));
818 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
819 }
820
821 void CreatePrAnswerAsLocalDescription() {
kwibergd1fe2812016-04-27 06:47:29 -0700822 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -0700823 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000824
825 std::string sdp;
826 EXPECT_TRUE(answer->ToString(&sdp));
827 SessionDescriptionInterface* pr_answer =
828 webrtc::CreateSessionDescription(SessionDescriptionInterface::kPrAnswer,
829 sdp, NULL);
830 EXPECT_TRUE(DoSetLocalDescription(pr_answer));
831 EXPECT_EQ(PeerConnectionInterface::kHaveLocalPrAnswer, observer_.state_);
832 }
833
834 void CreateOfferReceiveAnswer() {
835 CreateOfferAsLocalDescription();
836 std::string sdp;
837 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
838 CreateAnswerAsRemoteDescription(sdp);
839 }
840
841 void CreateOfferAsLocalDescription() {
kwibergd1fe2812016-04-27 06:47:29 -0700842 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -0700843 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000844 // TODO(perkj): Currently SetLocalDescription fails if any parameters in an
845 // audio codec change, even if the parameter has nothing to do with
846 // receiving. Not all parameters are serialized to SDP.
847 // Since CreatePrAnswerAsLocalDescription serialize/deserialize
848 // the SessionDescription, it is necessary to do that here to in order to
849 // get ReceiveOfferCreatePrAnswerAndAnswer and RenegotiateAudioOnly to pass.
850 // https://code.google.com/p/webrtc/issues/detail?id=1356
851 std::string sdp;
852 EXPECT_TRUE(offer->ToString(&sdp));
853 SessionDescriptionInterface* new_offer =
854 webrtc::CreateSessionDescription(
855 SessionDescriptionInterface::kOffer,
856 sdp, NULL);
857
858 EXPECT_TRUE(DoSetLocalDescription(new_offer));
859 EXPECT_EQ(PeerConnectionInterface::kHaveLocalOffer, observer_.state_);
mallinath@webrtc.org68cbd012014-01-22 00:16:46 +0000860 // Wait for the ice_complete message, so that SDP will have candidates.
861 EXPECT_TRUE_WAIT(observer_.ice_complete_, kTimeout);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000862 }
863
deadbeefab9b2d12015-10-14 11:33:11 -0700864 void CreateAnswerAsRemoteDescription(const std::string& sdp) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000865 webrtc::JsepSessionDescription* answer = new webrtc::JsepSessionDescription(
866 SessionDescriptionInterface::kAnswer);
deadbeefab9b2d12015-10-14 11:33:11 -0700867 EXPECT_TRUE(answer->Initialize(sdp, NULL));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000868 EXPECT_TRUE(DoSetRemoteDescription(answer));
869 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
870 }
871
deadbeefab9b2d12015-10-14 11:33:11 -0700872 void CreatePrAnswerAndAnswerAsRemoteDescription(const std::string& sdp) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000873 webrtc::JsepSessionDescription* pr_answer =
874 new webrtc::JsepSessionDescription(
875 SessionDescriptionInterface::kPrAnswer);
deadbeefab9b2d12015-10-14 11:33:11 -0700876 EXPECT_TRUE(pr_answer->Initialize(sdp, NULL));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000877 EXPECT_TRUE(DoSetRemoteDescription(pr_answer));
878 EXPECT_EQ(PeerConnectionInterface::kHaveRemotePrAnswer, observer_.state_);
879 webrtc::JsepSessionDescription* answer =
880 new webrtc::JsepSessionDescription(
881 SessionDescriptionInterface::kAnswer);
deadbeefab9b2d12015-10-14 11:33:11 -0700882 EXPECT_TRUE(answer->Initialize(sdp, NULL));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000883 EXPECT_TRUE(DoSetRemoteDescription(answer));
884 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
885 }
886
887 // Help function used for waiting until a the last signaled remote stream has
888 // the same label as |stream_label|. In a few of the tests in this file we
889 // answer with the same session description as we offer and thus we can
890 // check if OnAddStream have been called with the same stream as we offer to
891 // send.
892 void WaitAndVerifyOnAddStream(const std::string& stream_label) {
893 EXPECT_EQ_WAIT(stream_label, observer_.GetLastAddedStreamLabel(), kTimeout);
894 }
895
896 // Creates an offer and applies it as a local session description.
897 // Creates an answer with the same SDP an the offer but removes all lines
898 // that start with a:ssrc"
899 void CreateOfferReceiveAnswerWithoutSsrc() {
900 CreateOfferAsLocalDescription();
901 std::string sdp;
902 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
903 SetSsrcToZero(&sdp);
904 CreateAnswerAsRemoteDescription(sdp);
905 }
906
deadbeefab9b2d12015-10-14 11:33:11 -0700907 // This function creates a MediaStream with label kStreams[0] and
908 // |number_of_audio_tracks| and |number_of_video_tracks| tracks and the
909 // corresponding SessionDescriptionInterface. The SessionDescriptionInterface
kwiberg2bbff992016-03-16 11:03:04 -0700910 // is returned and the MediaStream is stored in
deadbeefab9b2d12015-10-14 11:33:11 -0700911 // |reference_collection_|
kwibergd1fe2812016-04-27 06:47:29 -0700912 std::unique_ptr<SessionDescriptionInterface>
kwiberg2bbff992016-03-16 11:03:04 -0700913 CreateSessionDescriptionAndReference(size_t number_of_audio_tracks,
914 size_t number_of_video_tracks) {
915 EXPECT_LE(number_of_audio_tracks, 2u);
916 EXPECT_LE(number_of_video_tracks, 2u);
deadbeefab9b2d12015-10-14 11:33:11 -0700917
918 reference_collection_ = StreamCollection::Create();
919 std::string sdp_ms1 = std::string(kSdpStringInit);
920
921 std::string mediastream_label = kStreams[0];
922
923 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream(
924 webrtc::MediaStream::Create(mediastream_label));
925 reference_collection_->AddStream(stream);
926
927 if (number_of_audio_tracks > 0) {
928 sdp_ms1 += std::string(kSdpStringAudio);
929 sdp_ms1 += std::string(kSdpStringMs1Audio0);
930 AddAudioTrack(kAudioTracks[0], stream);
931 }
932 if (number_of_audio_tracks > 1) {
933 sdp_ms1 += kSdpStringMs1Audio1;
934 AddAudioTrack(kAudioTracks[1], stream);
935 }
936
937 if (number_of_video_tracks > 0) {
938 sdp_ms1 += std::string(kSdpStringVideo);
939 sdp_ms1 += std::string(kSdpStringMs1Video0);
940 AddVideoTrack(kVideoTracks[0], stream);
941 }
942 if (number_of_video_tracks > 1) {
943 sdp_ms1 += kSdpStringMs1Video1;
944 AddVideoTrack(kVideoTracks[1], stream);
945 }
946
kwibergd1fe2812016-04-27 06:47:29 -0700947 return std::unique_ptr<SessionDescriptionInterface>(
kwiberg2bbff992016-03-16 11:03:04 -0700948 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
949 sdp_ms1, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -0700950 }
951
952 void AddAudioTrack(const std::string& track_id,
953 MediaStreamInterface* stream) {
954 rtc::scoped_refptr<webrtc::AudioTrackInterface> audio_track(
955 webrtc::AudioTrack::Create(track_id, nullptr));
956 ASSERT_TRUE(stream->AddTrack(audio_track));
957 }
958
959 void AddVideoTrack(const std::string& track_id,
960 MediaStreamInterface* stream) {
961 rtc::scoped_refptr<webrtc::VideoTrackInterface> video_track(
nisseaf510af2016-03-21 08:20:42 -0700962 webrtc::VideoTrack::Create(track_id,
963 webrtc::FakeVideoTrackSource::Create()));
deadbeefab9b2d12015-10-14 11:33:11 -0700964 ASSERT_TRUE(stream->AddTrack(video_track));
965 }
966
kwibergfd8be342016-05-14 19:44:11 -0700967 std::unique_ptr<SessionDescriptionInterface> CreateOfferWithOneAudioStream() {
zhihuang8f65cdf2016-05-06 18:40:30 -0700968 CreatePeerConnection();
969 AddVoiceStream(kStreamLabel1);
kwibergfd8be342016-05-14 19:44:11 -0700970 std::unique_ptr<SessionDescriptionInterface> offer;
zhihuang8f65cdf2016-05-06 18:40:30 -0700971 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
972 return offer;
973 }
974
kwibergfd8be342016-05-14 19:44:11 -0700975 std::unique_ptr<SessionDescriptionInterface>
zhihuang8f65cdf2016-05-06 18:40:30 -0700976 CreateAnswerWithOneAudioStream() {
kwibergfd8be342016-05-14 19:44:11 -0700977 std::unique_ptr<SessionDescriptionInterface> offer =
zhihuang8f65cdf2016-05-06 18:40:30 -0700978 CreateOfferWithOneAudioStream();
979 EXPECT_TRUE(DoSetRemoteDescription(offer.release()));
kwibergfd8be342016-05-14 19:44:11 -0700980 std::unique_ptr<SessionDescriptionInterface> answer;
zhihuang8f65cdf2016-05-06 18:40:30 -0700981 EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
982 return answer;
983 }
984
985 const std::string& GetFirstAudioStreamCname(
986 const SessionDescriptionInterface* desc) {
987 const cricket::ContentInfo* audio_content =
988 cricket::GetFirstAudioContent(desc->description());
989 const cricket::AudioContentDescription* audio_desc =
990 static_cast<const cricket::AudioContentDescription*>(
991 audio_content->description);
992 return audio_desc->streams()[0].cname;
993 }
994
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800995 cricket::FakePortAllocator* port_allocator_ = nullptr;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000996 scoped_refptr<webrtc::PeerConnectionFactoryInterface> pc_factory_;
997 scoped_refptr<PeerConnectionInterface> pc_;
998 MockPeerConnectionObserver observer_;
deadbeefab9b2d12015-10-14 11:33:11 -0700999 rtc::scoped_refptr<StreamCollection> reference_collection_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001000};
1001
zhihuang8f65cdf2016-05-06 18:40:30 -07001002// Generate different CNAMEs when PeerConnections are created.
1003// The CNAMEs are expected to be generated randomly. It is possible
1004// that the test fails, though the possibility is very low.
1005TEST_F(PeerConnectionInterfaceTest, CnameGenerationInOffer) {
kwibergfd8be342016-05-14 19:44:11 -07001006 std::unique_ptr<SessionDescriptionInterface> offer1 =
zhihuang8f65cdf2016-05-06 18:40:30 -07001007 CreateOfferWithOneAudioStream();
kwibergfd8be342016-05-14 19:44:11 -07001008 std::unique_ptr<SessionDescriptionInterface> offer2 =
zhihuang8f65cdf2016-05-06 18:40:30 -07001009 CreateOfferWithOneAudioStream();
1010 EXPECT_NE(GetFirstAudioStreamCname(offer1.get()),
1011 GetFirstAudioStreamCname(offer2.get()));
1012}
1013
1014TEST_F(PeerConnectionInterfaceTest, CnameGenerationInAnswer) {
kwibergfd8be342016-05-14 19:44:11 -07001015 std::unique_ptr<SessionDescriptionInterface> answer1 =
zhihuang8f65cdf2016-05-06 18:40:30 -07001016 CreateAnswerWithOneAudioStream();
kwibergfd8be342016-05-14 19:44:11 -07001017 std::unique_ptr<SessionDescriptionInterface> answer2 =
zhihuang8f65cdf2016-05-06 18:40:30 -07001018 CreateAnswerWithOneAudioStream();
1019 EXPECT_NE(GetFirstAudioStreamCname(answer1.get()),
1020 GetFirstAudioStreamCname(answer2.get()));
1021}
1022
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001023TEST_F(PeerConnectionInterfaceTest,
1024 CreatePeerConnectionWithDifferentConfigurations) {
1025 CreatePeerConnectionWithDifferentConfigurations();
1026}
1027
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001028TEST_F(PeerConnectionInterfaceTest,
1029 CreatePeerConnectionWithDifferentIceTransportsTypes) {
1030 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kNone);
1031 EXPECT_EQ(cricket::CF_NONE, port_allocator_->candidate_filter());
1032 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kRelay);
1033 EXPECT_EQ(cricket::CF_RELAY, port_allocator_->candidate_filter());
1034 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kNoHost);
1035 EXPECT_EQ(cricket::CF_ALL & ~cricket::CF_HOST,
1036 port_allocator_->candidate_filter());
1037 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kAll);
1038 EXPECT_EQ(cricket::CF_ALL, port_allocator_->candidate_filter());
1039}
1040
1041// Test that when a PeerConnection is created with a nonzero candidate pool
1042// size, the pooled PortAllocatorSession is created with all the attributes
1043// in the RTCConfiguration.
1044TEST_F(PeerConnectionInterfaceTest, CreatePeerConnectionWithPooledCandidates) {
1045 PeerConnectionInterface::RTCConfiguration config;
1046 PeerConnectionInterface::IceServer server;
1047 server.uri = kStunAddressOnly;
1048 config.servers.push_back(server);
1049 config.type = PeerConnectionInterface::kRelay;
1050 config.disable_ipv6 = true;
1051 config.tcp_candidate_policy =
1052 PeerConnectionInterface::kTcpCandidatePolicyDisabled;
1053 config.ice_candidate_pool_size = 1;
1054 CreatePeerConnection(config, nullptr);
1055
1056 const cricket::FakePortAllocatorSession* session =
1057 static_cast<const cricket::FakePortAllocatorSession*>(
1058 port_allocator_->GetPooledSession());
1059 ASSERT_NE(nullptr, session);
1060 EXPECT_EQ(1UL, session->stun_servers().size());
1061 EXPECT_EQ(0U, session->flags() & cricket::PORTALLOCATOR_ENABLE_IPV6);
1062 EXPECT_LT(0U, session->flags() & cricket::PORTALLOCATOR_DISABLE_TCP);
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001063}
1064
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001065TEST_F(PeerConnectionInterfaceTest, AddStreams) {
1066 CreatePeerConnection();
deadbeefab9b2d12015-10-14 11:33:11 -07001067 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001068 AddVoiceStream(kStreamLabel2);
1069 ASSERT_EQ(2u, pc_->local_streams()->count());
1070
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001071 // Test we can add multiple local streams to one peerconnection.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001072 scoped_refptr<MediaStreamInterface> stream(
1073 pc_factory_->CreateLocalMediaStream(kStreamLabel3));
1074 scoped_refptr<AudioTrackInterface> audio_track(
1075 pc_factory_->CreateAudioTrack(
1076 kStreamLabel3, static_cast<AudioSourceInterface*>(NULL)));
1077 stream->AddTrack(audio_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +00001078 EXPECT_TRUE(pc_->AddStream(stream));
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001079 EXPECT_EQ(3u, pc_->local_streams()->count());
1080
1081 // Remove the third stream.
1082 pc_->RemoveStream(pc_->local_streams()->at(2));
1083 EXPECT_EQ(2u, pc_->local_streams()->count());
1084
1085 // Remove the second stream.
1086 pc_->RemoveStream(pc_->local_streams()->at(1));
1087 EXPECT_EQ(1u, pc_->local_streams()->count());
1088
1089 // Remove the first stream.
1090 pc_->RemoveStream(pc_->local_streams()->at(0));
1091 EXPECT_EQ(0u, pc_->local_streams()->count());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001092}
1093
deadbeefab9b2d12015-10-14 11:33:11 -07001094// Test that the created offer includes streams we added.
1095TEST_F(PeerConnectionInterfaceTest, AddedStreamsPresentInOffer) {
1096 CreatePeerConnection();
1097 AddAudioVideoStream(kStreamLabel1, "audio_track", "video_track");
kwibergd1fe2812016-04-27 06:47:29 -07001098 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001099 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07001100
1101 const cricket::ContentInfo* audio_content =
1102 cricket::GetFirstAudioContent(offer->description());
1103 const cricket::AudioContentDescription* audio_desc =
1104 static_cast<const cricket::AudioContentDescription*>(
1105 audio_content->description);
1106 EXPECT_TRUE(
1107 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
1108
1109 const cricket::ContentInfo* video_content =
1110 cricket::GetFirstVideoContent(offer->description());
1111 const cricket::VideoContentDescription* video_desc =
1112 static_cast<const cricket::VideoContentDescription*>(
1113 video_content->description);
1114 EXPECT_TRUE(
1115 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1116
1117 // Add another stream and ensure the offer includes both the old and new
1118 // streams.
1119 AddAudioVideoStream(kStreamLabel2, "audio_track2", "video_track2");
kwiberg2bbff992016-03-16 11:03:04 -07001120 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07001121
1122 audio_content = cricket::GetFirstAudioContent(offer->description());
1123 audio_desc = static_cast<const cricket::AudioContentDescription*>(
1124 audio_content->description);
1125 EXPECT_TRUE(
1126 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
1127 EXPECT_TRUE(
1128 ContainsTrack(audio_desc->streams(), kStreamLabel2, "audio_track2"));
1129
1130 video_content = cricket::GetFirstVideoContent(offer->description());
1131 video_desc = static_cast<const cricket::VideoContentDescription*>(
1132 video_content->description);
1133 EXPECT_TRUE(
1134 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1135 EXPECT_TRUE(
1136 ContainsTrack(video_desc->streams(), kStreamLabel2, "video_track2"));
1137}
1138
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001139TEST_F(PeerConnectionInterfaceTest, RemoveStream) {
1140 CreatePeerConnection();
deadbeefab9b2d12015-10-14 11:33:11 -07001141 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001142 ASSERT_EQ(1u, pc_->local_streams()->count());
1143 pc_->RemoveStream(pc_->local_streams()->at(0));
1144 EXPECT_EQ(0u, pc_->local_streams()->count());
1145}
1146
deadbeefe1f9d832016-01-14 15:35:42 -08001147// Test for AddTrack and RemoveTrack methods.
1148// Tests that the created offer includes tracks we added,
1149// and that the RtpSenders are created correctly.
1150// Also tests that RemoveTrack removes the tracks from subsequent offers.
1151TEST_F(PeerConnectionInterfaceTest, AddTrackRemoveTrack) {
1152 CreatePeerConnection();
1153 // Create a dummy stream, so tracks share a stream label.
1154 scoped_refptr<MediaStreamInterface> stream(
1155 pc_factory_->CreateLocalMediaStream(kStreamLabel1));
1156 std::vector<MediaStreamInterface*> stream_list;
1157 stream_list.push_back(stream.get());
1158 scoped_refptr<AudioTrackInterface> audio_track(
1159 pc_factory_->CreateAudioTrack("audio_track", nullptr));
nisseaf510af2016-03-21 08:20:42 -07001160 scoped_refptr<VideoTrackInterface> video_track(pc_factory_->CreateVideoTrack(
1161 "video_track",
1162 pc_factory_->CreateVideoSource(new cricket::FakeVideoCapturer())));
deadbeefe1f9d832016-01-14 15:35:42 -08001163 auto audio_sender = pc_->AddTrack(audio_track, stream_list);
1164 auto video_sender = pc_->AddTrack(video_track, stream_list);
1165 EXPECT_EQ(kStreamLabel1, audio_sender->stream_id());
1166 EXPECT_EQ("audio_track", audio_sender->id());
1167 EXPECT_EQ(audio_track, audio_sender->track());
1168 EXPECT_EQ(kStreamLabel1, video_sender->stream_id());
1169 EXPECT_EQ("video_track", video_sender->id());
1170 EXPECT_EQ(video_track, video_sender->track());
1171
1172 // Now create an offer and check for the senders.
kwibergd1fe2812016-04-27 06:47:29 -07001173 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001174 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefe1f9d832016-01-14 15:35:42 -08001175
1176 const cricket::ContentInfo* audio_content =
1177 cricket::GetFirstAudioContent(offer->description());
1178 const cricket::AudioContentDescription* audio_desc =
1179 static_cast<const cricket::AudioContentDescription*>(
1180 audio_content->description);
1181 EXPECT_TRUE(
1182 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
1183
1184 const cricket::ContentInfo* video_content =
1185 cricket::GetFirstVideoContent(offer->description());
1186 const cricket::VideoContentDescription* video_desc =
1187 static_cast<const cricket::VideoContentDescription*>(
1188 video_content->description);
1189 EXPECT_TRUE(
1190 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1191
1192 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
1193
1194 // Now try removing the tracks.
1195 EXPECT_TRUE(pc_->RemoveTrack(audio_sender));
1196 EXPECT_TRUE(pc_->RemoveTrack(video_sender));
1197
1198 // Create a new offer and ensure it doesn't contain the removed senders.
kwiberg2bbff992016-03-16 11:03:04 -07001199 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefe1f9d832016-01-14 15:35:42 -08001200
1201 audio_content = cricket::GetFirstAudioContent(offer->description());
1202 audio_desc = static_cast<const cricket::AudioContentDescription*>(
1203 audio_content->description);
1204 EXPECT_FALSE(
1205 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
1206
1207 video_content = cricket::GetFirstVideoContent(offer->description());
1208 video_desc = static_cast<const cricket::VideoContentDescription*>(
1209 video_content->description);
1210 EXPECT_FALSE(
1211 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1212
1213 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
1214
1215 // Calling RemoveTrack on a sender no longer attached to a PeerConnection
1216 // should return false.
1217 EXPECT_FALSE(pc_->RemoveTrack(audio_sender));
1218 EXPECT_FALSE(pc_->RemoveTrack(video_sender));
1219}
1220
1221// Test creating senders without a stream specified,
1222// expecting a random stream ID to be generated.
1223TEST_F(PeerConnectionInterfaceTest, AddTrackWithoutStream) {
1224 CreatePeerConnection();
1225 // Create a dummy stream, so tracks share a stream label.
1226 scoped_refptr<AudioTrackInterface> audio_track(
1227 pc_factory_->CreateAudioTrack("audio_track", nullptr));
nisseaf510af2016-03-21 08:20:42 -07001228 scoped_refptr<VideoTrackInterface> video_track(pc_factory_->CreateVideoTrack(
1229 "video_track",
1230 pc_factory_->CreateVideoSource(new cricket::FakeVideoCapturer())));
deadbeefe1f9d832016-01-14 15:35:42 -08001231 auto audio_sender =
1232 pc_->AddTrack(audio_track, std::vector<MediaStreamInterface*>());
1233 auto video_sender =
1234 pc_->AddTrack(video_track, std::vector<MediaStreamInterface*>());
1235 EXPECT_EQ("audio_track", audio_sender->id());
1236 EXPECT_EQ(audio_track, audio_sender->track());
1237 EXPECT_EQ("video_track", video_sender->id());
1238 EXPECT_EQ(video_track, video_sender->track());
1239 // If the ID is truly a random GUID, it should be infinitely unlikely they
1240 // will be the same.
1241 EXPECT_NE(video_sender->stream_id(), audio_sender->stream_id());
1242}
1243
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001244TEST_F(PeerConnectionInterfaceTest, CreateOfferReceiveAnswer) {
1245 InitiateCall();
1246 WaitAndVerifyOnAddStream(kStreamLabel1);
1247 VerifyRemoteRtpHeaderExtensions();
1248}
1249
1250TEST_F(PeerConnectionInterfaceTest, CreateOfferReceivePrAnswerAndAnswer) {
1251 CreatePeerConnection();
deadbeefab9b2d12015-10-14 11:33:11 -07001252 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001253 CreateOfferAsLocalDescription();
1254 std::string offer;
1255 EXPECT_TRUE(pc_->local_description()->ToString(&offer));
1256 CreatePrAnswerAndAnswerAsRemoteDescription(offer);
1257 WaitAndVerifyOnAddStream(kStreamLabel1);
1258}
1259
1260TEST_F(PeerConnectionInterfaceTest, ReceiveOfferCreateAnswer) {
1261 CreatePeerConnection();
deadbeefab9b2d12015-10-14 11:33:11 -07001262 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001263
1264 CreateOfferAsRemoteDescription();
1265 CreateAnswerAsLocalDescription();
1266
1267 WaitAndVerifyOnAddStream(kStreamLabel1);
1268}
1269
1270TEST_F(PeerConnectionInterfaceTest, ReceiveOfferCreatePrAnswerAndAnswer) {
1271 CreatePeerConnection();
deadbeefab9b2d12015-10-14 11:33:11 -07001272 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001273
1274 CreateOfferAsRemoteDescription();
1275 CreatePrAnswerAsLocalDescription();
1276 CreateAnswerAsLocalDescription();
1277
1278 WaitAndVerifyOnAddStream(kStreamLabel1);
1279}
1280
1281TEST_F(PeerConnectionInterfaceTest, Renegotiate) {
1282 InitiateCall();
1283 ASSERT_EQ(1u, pc_->remote_streams()->count());
1284 pc_->RemoveStream(pc_->local_streams()->at(0));
1285 CreateOfferReceiveAnswer();
1286 EXPECT_EQ(0u, pc_->remote_streams()->count());
deadbeefab9b2d12015-10-14 11:33:11 -07001287 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001288 CreateOfferReceiveAnswer();
1289}
1290
1291// Tests that after negotiating an audio only call, the respondent can perform a
1292// renegotiation that removes the audio stream.
1293TEST_F(PeerConnectionInterfaceTest, RenegotiateAudioOnly) {
1294 CreatePeerConnection();
1295 AddVoiceStream(kStreamLabel1);
1296 CreateOfferAsRemoteDescription();
1297 CreateAnswerAsLocalDescription();
1298
1299 ASSERT_EQ(1u, pc_->remote_streams()->count());
1300 pc_->RemoveStream(pc_->local_streams()->at(0));
1301 CreateOfferReceiveAnswer();
1302 EXPECT_EQ(0u, pc_->remote_streams()->count());
1303}
1304
1305// Test that candidates are generated and that we can parse our own candidates.
1306TEST_F(PeerConnectionInterfaceTest, IceCandidates) {
1307 CreatePeerConnection();
1308
1309 EXPECT_FALSE(pc_->AddIceCandidate(observer_.last_candidate_.get()));
1310 // SetRemoteDescription takes ownership of offer.
kwibergd1fe2812016-04-27 06:47:29 -07001311 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefab9b2d12015-10-14 11:33:11 -07001312 AddVideoStream(kStreamLabel1);
deadbeefc80741f2015-10-22 13:14:45 -07001313 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
kwiberg2bbff992016-03-16 11:03:04 -07001314 EXPECT_TRUE(DoSetRemoteDescription(offer.release()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001315
1316 // SetLocalDescription takes ownership of answer.
kwibergd1fe2812016-04-27 06:47:29 -07001317 std::unique_ptr<SessionDescriptionInterface> answer;
deadbeefc80741f2015-10-22 13:14:45 -07001318 EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
kwiberg2bbff992016-03-16 11:03:04 -07001319 EXPECT_TRUE(DoSetLocalDescription(answer.release()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001320
1321 EXPECT_TRUE_WAIT(observer_.last_candidate_.get() != NULL, kTimeout);
1322 EXPECT_TRUE_WAIT(observer_.ice_complete_, kTimeout);
1323
1324 EXPECT_TRUE(pc_->AddIceCandidate(observer_.last_candidate_.get()));
1325}
1326
deadbeefab9b2d12015-10-14 11:33:11 -07001327// Test that CreateOffer and CreateAnswer will fail if the track labels are
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001328// not unique.
1329TEST_F(PeerConnectionInterfaceTest, CreateOfferAnswerWithInvalidStream) {
1330 CreatePeerConnection();
1331 // Create a regular offer for the CreateAnswer test later.
kwibergd1fe2812016-04-27 06:47:29 -07001332 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefc80741f2015-10-22 13:14:45 -07001333 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
kwiberg2bbff992016-03-16 11:03:04 -07001334 EXPECT_TRUE(offer);
1335 offer.reset();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001336
1337 // Create a local stream with audio&video tracks having same label.
1338 AddAudioVideoStream(kStreamLabel1, "track_label", "track_label");
1339
1340 // Test CreateOffer
deadbeefc80741f2015-10-22 13:14:45 -07001341 EXPECT_FALSE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001342
1343 // Test CreateAnswer
kwibergd1fe2812016-04-27 06:47:29 -07001344 std::unique_ptr<SessionDescriptionInterface> answer;
deadbeefc80741f2015-10-22 13:14:45 -07001345 EXPECT_FALSE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001346}
1347
1348// Test that we will get different SSRCs for each tracks in the offer and answer
1349// we created.
1350TEST_F(PeerConnectionInterfaceTest, SsrcInOfferAnswer) {
1351 CreatePeerConnection();
1352 // Create a local stream with audio&video tracks having different labels.
1353 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
1354
1355 // Test CreateOffer
kwibergd1fe2812016-04-27 06:47:29 -07001356 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001357 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001358 int audio_ssrc = 0;
1359 int video_ssrc = 0;
1360 EXPECT_TRUE(GetFirstSsrc(GetFirstAudioContent(offer->description()),
1361 &audio_ssrc));
1362 EXPECT_TRUE(GetFirstSsrc(GetFirstVideoContent(offer->description()),
1363 &video_ssrc));
1364 EXPECT_NE(audio_ssrc, video_ssrc);
1365
1366 // Test CreateAnswer
1367 EXPECT_TRUE(DoSetRemoteDescription(offer.release()));
kwibergd1fe2812016-04-27 06:47:29 -07001368 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -07001369 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001370 audio_ssrc = 0;
1371 video_ssrc = 0;
1372 EXPECT_TRUE(GetFirstSsrc(GetFirstAudioContent(answer->description()),
1373 &audio_ssrc));
1374 EXPECT_TRUE(GetFirstSsrc(GetFirstVideoContent(answer->description()),
1375 &video_ssrc));
1376 EXPECT_NE(audio_ssrc, video_ssrc);
1377}
1378
deadbeefeb459812015-12-15 19:24:43 -08001379// Test that it's possible to call AddTrack on a MediaStream after adding
1380// the stream to a PeerConnection.
1381// TODO(deadbeef): Remove this test once this behavior is no longer supported.
1382TEST_F(PeerConnectionInterfaceTest, AddTrackAfterAddStream) {
1383 CreatePeerConnection();
1384 // Create audio stream and add to PeerConnection.
1385 AddVoiceStream(kStreamLabel1);
1386 MediaStreamInterface* stream = pc_->local_streams()->at(0);
1387
1388 // Add video track to the audio-only stream.
nisseaf510af2016-03-21 08:20:42 -07001389 scoped_refptr<VideoTrackInterface> video_track(pc_factory_->CreateVideoTrack(
1390 "video_label",
1391 pc_factory_->CreateVideoSource(new cricket::FakeVideoCapturer())));
deadbeefeb459812015-12-15 19:24:43 -08001392 stream->AddTrack(video_track.get());
1393
kwibergd1fe2812016-04-27 06:47:29 -07001394 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001395 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefeb459812015-12-15 19:24:43 -08001396
1397 const cricket::MediaContentDescription* video_desc =
1398 cricket::GetFirstVideoContentDescription(offer->description());
1399 EXPECT_TRUE(video_desc != nullptr);
1400}
1401
1402// Test that it's possible to call RemoveTrack on a MediaStream after adding
1403// the stream to a PeerConnection.
1404// TODO(deadbeef): Remove this test once this behavior is no longer supported.
1405TEST_F(PeerConnectionInterfaceTest, RemoveTrackAfterAddStream) {
1406 CreatePeerConnection();
1407 // Create audio/video stream and add to PeerConnection.
1408 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
1409 MediaStreamInterface* stream = pc_->local_streams()->at(0);
1410
1411 // Remove the video track.
1412 stream->RemoveTrack(stream->GetVideoTracks()[0]);
1413
kwibergd1fe2812016-04-27 06:47:29 -07001414 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001415 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefeb459812015-12-15 19:24:43 -08001416
1417 const cricket::MediaContentDescription* video_desc =
1418 cricket::GetFirstVideoContentDescription(offer->description());
1419 EXPECT_TRUE(video_desc == nullptr);
1420}
1421
deadbeefbd7d8f72015-12-18 16:58:44 -08001422// Test creating a sender with a stream ID, and ensure the ID is populated
1423// in the offer.
1424TEST_F(PeerConnectionInterfaceTest, CreateSenderWithStream) {
1425 CreatePeerConnection();
1426 pc_->CreateSender("video", kStreamLabel1);
1427
kwibergd1fe2812016-04-27 06:47:29 -07001428 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001429 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefbd7d8f72015-12-18 16:58:44 -08001430
1431 const cricket::MediaContentDescription* video_desc =
1432 cricket::GetFirstVideoContentDescription(offer->description());
1433 ASSERT_TRUE(video_desc != nullptr);
1434 ASSERT_EQ(1u, video_desc->streams().size());
1435 EXPECT_EQ(kStreamLabel1, video_desc->streams()[0].sync_label);
1436}
1437
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001438// Test that we can specify a certain track that we want statistics about.
1439TEST_F(PeerConnectionInterfaceTest, GetStatsForSpecificTrack) {
1440 InitiateCall();
1441 ASSERT_LT(0u, pc_->remote_streams()->count());
1442 ASSERT_LT(0u, pc_->remote_streams()->at(0)->GetAudioTracks().size());
1443 scoped_refptr<MediaStreamTrackInterface> remote_audio =
1444 pc_->remote_streams()->at(0)->GetAudioTracks()[0];
1445 EXPECT_TRUE(DoGetStats(remote_audio));
1446
1447 // Remove the stream. Since we are sending to our selves the local
1448 // and the remote stream is the same.
1449 pc_->RemoveStream(pc_->local_streams()->at(0));
1450 // Do a re-negotiation.
1451 CreateOfferReceiveAnswer();
1452
1453 ASSERT_EQ(0u, pc_->remote_streams()->count());
1454
1455 // Test that we still can get statistics for the old track. Even if it is not
1456 // sent any longer.
1457 EXPECT_TRUE(DoGetStats(remote_audio));
1458}
1459
1460// Test that we can get stats on a video track.
1461TEST_F(PeerConnectionInterfaceTest, GetStatsForVideoTrack) {
1462 InitiateCall();
1463 ASSERT_LT(0u, pc_->remote_streams()->count());
1464 ASSERT_LT(0u, pc_->remote_streams()->at(0)->GetVideoTracks().size());
1465 scoped_refptr<MediaStreamTrackInterface> remote_video =
1466 pc_->remote_streams()->at(0)->GetVideoTracks()[0];
1467 EXPECT_TRUE(DoGetStats(remote_video));
1468}
1469
1470// Test that we don't get statistics for an invalid track.
tommi@webrtc.org908f57e2014-07-21 11:44:39 +00001471// TODO(tommi): Fix this test. DoGetStats will return true
1472// for the unknown track (since GetStats is async), but no
1473// data is returned for the track.
1474TEST_F(PeerConnectionInterfaceTest, DISABLED_GetStatsForInvalidTrack) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001475 InitiateCall();
1476 scoped_refptr<AudioTrackInterface> unknown_audio_track(
1477 pc_factory_->CreateAudioTrack("unknown track", NULL));
1478 EXPECT_FALSE(DoGetStats(unknown_audio_track));
1479}
1480
1481// This test setup two RTP data channels in loop back.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001482TEST_F(PeerConnectionInterfaceTest, TestDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001483 FakeConstraints constraints;
1484 constraints.SetAllowRtpDataChannels();
1485 CreatePeerConnection(&constraints);
1486 scoped_refptr<DataChannelInterface> data1 =
1487 pc_->CreateDataChannel("test1", NULL);
1488 scoped_refptr<DataChannelInterface> data2 =
1489 pc_->CreateDataChannel("test2", NULL);
1490 ASSERT_TRUE(data1 != NULL);
kwibergd1fe2812016-04-27 06:47:29 -07001491 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001492 new MockDataChannelObserver(data1));
kwibergd1fe2812016-04-27 06:47:29 -07001493 std::unique_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001494 new MockDataChannelObserver(data2));
1495
1496 EXPECT_EQ(DataChannelInterface::kConnecting, data1->state());
1497 EXPECT_EQ(DataChannelInterface::kConnecting, data2->state());
1498 std::string data_to_send1 = "testing testing";
1499 std::string data_to_send2 = "testing something else";
1500 EXPECT_FALSE(data1->Send(DataBuffer(data_to_send1)));
1501
1502 CreateOfferReceiveAnswer();
1503 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1504 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
1505
1506 EXPECT_EQ(DataChannelInterface::kOpen, data1->state());
1507 EXPECT_EQ(DataChannelInterface::kOpen, data2->state());
1508 EXPECT_TRUE(data1->Send(DataBuffer(data_to_send1)));
1509 EXPECT_TRUE(data2->Send(DataBuffer(data_to_send2)));
1510
1511 EXPECT_EQ_WAIT(data_to_send1, observer1->last_message(), kTimeout);
1512 EXPECT_EQ_WAIT(data_to_send2, observer2->last_message(), kTimeout);
1513
1514 data1->Close();
1515 EXPECT_EQ(DataChannelInterface::kClosing, data1->state());
1516 CreateOfferReceiveAnswer();
1517 EXPECT_FALSE(observer1->IsOpen());
1518 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
1519 EXPECT_TRUE(observer2->IsOpen());
1520
1521 data_to_send2 = "testing something else again";
1522 EXPECT_TRUE(data2->Send(DataBuffer(data_to_send2)));
1523
1524 EXPECT_EQ_WAIT(data_to_send2, observer2->last_message(), kTimeout);
1525}
1526
1527// This test verifies that sendnig binary data over RTP data channels should
1528// fail.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001529TEST_F(PeerConnectionInterfaceTest, TestSendBinaryOnRtpDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001530 FakeConstraints constraints;
1531 constraints.SetAllowRtpDataChannels();
1532 CreatePeerConnection(&constraints);
1533 scoped_refptr<DataChannelInterface> data1 =
1534 pc_->CreateDataChannel("test1", NULL);
1535 scoped_refptr<DataChannelInterface> data2 =
1536 pc_->CreateDataChannel("test2", NULL);
1537 ASSERT_TRUE(data1 != NULL);
kwibergd1fe2812016-04-27 06:47:29 -07001538 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001539 new MockDataChannelObserver(data1));
kwibergd1fe2812016-04-27 06:47:29 -07001540 std::unique_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001541 new MockDataChannelObserver(data2));
1542
1543 EXPECT_EQ(DataChannelInterface::kConnecting, data1->state());
1544 EXPECT_EQ(DataChannelInterface::kConnecting, data2->state());
1545
1546 CreateOfferReceiveAnswer();
1547 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1548 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
1549
1550 EXPECT_EQ(DataChannelInterface::kOpen, data1->state());
1551 EXPECT_EQ(DataChannelInterface::kOpen, data2->state());
1552
jbaucheec21bd2016-03-20 06:15:43 -07001553 rtc::CopyOnWriteBuffer buffer("test", 4);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001554 EXPECT_FALSE(data1->Send(DataBuffer(buffer, true)));
1555}
1556
1557// This test setup a RTP data channels in loop back and test that a channel is
1558// opened even if the remote end answer with a zero SSRC.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001559TEST_F(PeerConnectionInterfaceTest, TestSendOnlyDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001560 FakeConstraints constraints;
1561 constraints.SetAllowRtpDataChannels();
1562 CreatePeerConnection(&constraints);
1563 scoped_refptr<DataChannelInterface> data1 =
1564 pc_->CreateDataChannel("test1", NULL);
kwibergd1fe2812016-04-27 06:47:29 -07001565 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001566 new MockDataChannelObserver(data1));
1567
1568 CreateOfferReceiveAnswerWithoutSsrc();
1569
1570 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1571
1572 data1->Close();
1573 EXPECT_EQ(DataChannelInterface::kClosing, data1->state());
1574 CreateOfferReceiveAnswerWithoutSsrc();
1575 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
1576 EXPECT_FALSE(observer1->IsOpen());
1577}
1578
1579// This test that if a data channel is added in an answer a receive only channel
1580// channel is created.
1581TEST_F(PeerConnectionInterfaceTest, TestReceiveOnlyDataChannel) {
1582 FakeConstraints constraints;
1583 constraints.SetAllowRtpDataChannels();
1584 CreatePeerConnection(&constraints);
1585
1586 std::string offer_label = "offer_channel";
1587 scoped_refptr<DataChannelInterface> offer_channel =
1588 pc_->CreateDataChannel(offer_label, NULL);
1589
1590 CreateOfferAsLocalDescription();
1591
1592 // Replace the data channel label in the offer and apply it as an answer.
1593 std::string receive_label = "answer_channel";
1594 std::string sdp;
1595 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001596 rtc::replace_substrs(offer_label.c_str(), offer_label.length(),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001597 receive_label.c_str(), receive_label.length(),
1598 &sdp);
1599 CreateAnswerAsRemoteDescription(sdp);
1600
1601 // Verify that a new incoming data channel has been created and that
1602 // it is open but can't we written to.
1603 ASSERT_TRUE(observer_.last_datachannel_ != NULL);
1604 DataChannelInterface* received_channel = observer_.last_datachannel_;
1605 EXPECT_EQ(DataChannelInterface::kConnecting, received_channel->state());
1606 EXPECT_EQ(receive_label, received_channel->label());
1607 EXPECT_FALSE(received_channel->Send(DataBuffer("something")));
1608
1609 // Verify that the channel we initially offered has been rejected.
1610 EXPECT_EQ(DataChannelInterface::kClosed, offer_channel->state());
1611
1612 // Do another offer / answer exchange and verify that the data channel is
1613 // opened.
1614 CreateOfferReceiveAnswer();
1615 EXPECT_EQ_WAIT(DataChannelInterface::kOpen, received_channel->state(),
1616 kTimeout);
1617}
1618
1619// This test that no data channel is returned if a reliable channel is
1620// requested.
1621// TODO(perkj): Remove this test once reliable channels are implemented.
1622TEST_F(PeerConnectionInterfaceTest, CreateReliableRtpDataChannelShouldFail) {
1623 FakeConstraints constraints;
1624 constraints.SetAllowRtpDataChannels();
1625 CreatePeerConnection(&constraints);
1626
1627 std::string label = "test";
1628 webrtc::DataChannelInit config;
1629 config.reliable = true;
1630 scoped_refptr<DataChannelInterface> channel =
1631 pc_->CreateDataChannel(label, &config);
1632 EXPECT_TRUE(channel == NULL);
1633}
1634
deadbeefab9b2d12015-10-14 11:33:11 -07001635// Verifies that duplicated label is not allowed for RTP data channel.
1636TEST_F(PeerConnectionInterfaceTest, RtpDuplicatedLabelNotAllowed) {
1637 FakeConstraints constraints;
1638 constraints.SetAllowRtpDataChannels();
1639 CreatePeerConnection(&constraints);
1640
1641 std::string label = "test";
1642 scoped_refptr<DataChannelInterface> channel =
1643 pc_->CreateDataChannel(label, nullptr);
1644 EXPECT_NE(channel, nullptr);
1645
1646 scoped_refptr<DataChannelInterface> dup_channel =
1647 pc_->CreateDataChannel(label, nullptr);
1648 EXPECT_EQ(dup_channel, nullptr);
1649}
1650
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001651// This tests that a SCTP data channel is returned using different
1652// DataChannelInit configurations.
1653TEST_F(PeerConnectionInterfaceTest, CreateSctpDataChannel) {
1654 FakeConstraints constraints;
1655 constraints.SetAllowDtlsSctpDataChannels();
1656 CreatePeerConnection(&constraints);
1657
1658 webrtc::DataChannelInit config;
1659
1660 scoped_refptr<DataChannelInterface> channel =
1661 pc_->CreateDataChannel("1", &config);
1662 EXPECT_TRUE(channel != NULL);
1663 EXPECT_TRUE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001664 EXPECT_TRUE(observer_.renegotiation_needed_);
1665 observer_.renegotiation_needed_ = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001666
1667 config.ordered = false;
1668 channel = pc_->CreateDataChannel("2", &config);
1669 EXPECT_TRUE(channel != NULL);
1670 EXPECT_TRUE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001671 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001672
1673 config.ordered = true;
1674 config.maxRetransmits = 0;
1675 channel = pc_->CreateDataChannel("3", &config);
1676 EXPECT_TRUE(channel != NULL);
1677 EXPECT_FALSE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001678 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001679
1680 config.maxRetransmits = -1;
1681 config.maxRetransmitTime = 0;
1682 channel = pc_->CreateDataChannel("4", &config);
1683 EXPECT_TRUE(channel != NULL);
1684 EXPECT_FALSE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001685 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001686}
1687
1688// This tests that no data channel is returned if both maxRetransmits and
1689// maxRetransmitTime are set for SCTP data channels.
1690TEST_F(PeerConnectionInterfaceTest,
1691 CreateSctpDataChannelShouldFailForInvalidConfig) {
1692 FakeConstraints constraints;
1693 constraints.SetAllowDtlsSctpDataChannels();
1694 CreatePeerConnection(&constraints);
1695
1696 std::string label = "test";
1697 webrtc::DataChannelInit config;
1698 config.maxRetransmits = 0;
1699 config.maxRetransmitTime = 0;
1700
1701 scoped_refptr<DataChannelInterface> channel =
1702 pc_->CreateDataChannel(label, &config);
1703 EXPECT_TRUE(channel == NULL);
1704}
1705
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001706// The test verifies that creating a SCTP data channel with an id already in use
1707// or out of range should fail.
1708TEST_F(PeerConnectionInterfaceTest,
1709 CreateSctpDataChannelWithInvalidIdShouldFail) {
1710 FakeConstraints constraints;
1711 constraints.SetAllowDtlsSctpDataChannels();
1712 CreatePeerConnection(&constraints);
1713
1714 webrtc::DataChannelInit config;
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001715 scoped_refptr<DataChannelInterface> channel;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001716
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001717 config.id = 1;
1718 channel = pc_->CreateDataChannel("1", &config);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001719 EXPECT_TRUE(channel != NULL);
1720 EXPECT_EQ(1, channel->id());
1721
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001722 channel = pc_->CreateDataChannel("x", &config);
1723 EXPECT_TRUE(channel == NULL);
1724
1725 config.id = cricket::kMaxSctpSid;
1726 channel = pc_->CreateDataChannel("max", &config);
1727 EXPECT_TRUE(channel != NULL);
1728 EXPECT_EQ(config.id, channel->id());
1729
1730 config.id = cricket::kMaxSctpSid + 1;
1731 channel = pc_->CreateDataChannel("x", &config);
1732 EXPECT_TRUE(channel == NULL);
1733}
1734
deadbeefab9b2d12015-10-14 11:33:11 -07001735// Verifies that duplicated label is allowed for SCTP data channel.
1736TEST_F(PeerConnectionInterfaceTest, SctpDuplicatedLabelAllowed) {
1737 FakeConstraints constraints;
1738 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
1739 true);
1740 CreatePeerConnection(&constraints);
1741
1742 std::string label = "test";
1743 scoped_refptr<DataChannelInterface> channel =
1744 pc_->CreateDataChannel(label, nullptr);
1745 EXPECT_NE(channel, nullptr);
1746
1747 scoped_refptr<DataChannelInterface> dup_channel =
1748 pc_->CreateDataChannel(label, nullptr);
1749 EXPECT_NE(dup_channel, nullptr);
1750}
1751
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001752// This test verifies that OnRenegotiationNeeded is fired for every new RTP
1753// DataChannel.
1754TEST_F(PeerConnectionInterfaceTest, RenegotiationNeededForNewRtpDataChannel) {
1755 FakeConstraints constraints;
1756 constraints.SetAllowRtpDataChannels();
1757 CreatePeerConnection(&constraints);
1758
1759 scoped_refptr<DataChannelInterface> dc1 =
1760 pc_->CreateDataChannel("test1", NULL);
1761 EXPECT_TRUE(observer_.renegotiation_needed_);
1762 observer_.renegotiation_needed_ = false;
1763
1764 scoped_refptr<DataChannelInterface> dc2 =
1765 pc_->CreateDataChannel("test2", NULL);
1766 EXPECT_TRUE(observer_.renegotiation_needed_);
1767}
1768
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001769// This test that a data channel closes when a PeerConnection is deleted/closed.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001770TEST_F(PeerConnectionInterfaceTest, DataChannelCloseWhenPeerConnectionClose) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001771 FakeConstraints constraints;
1772 constraints.SetAllowRtpDataChannels();
1773 CreatePeerConnection(&constraints);
1774
1775 scoped_refptr<DataChannelInterface> data1 =
1776 pc_->CreateDataChannel("test1", NULL);
1777 scoped_refptr<DataChannelInterface> data2 =
1778 pc_->CreateDataChannel("test2", NULL);
1779 ASSERT_TRUE(data1 != NULL);
kwibergd1fe2812016-04-27 06:47:29 -07001780 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001781 new MockDataChannelObserver(data1));
kwibergd1fe2812016-04-27 06:47:29 -07001782 std::unique_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001783 new MockDataChannelObserver(data2));
1784
1785 CreateOfferReceiveAnswer();
1786 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1787 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
1788
1789 ReleasePeerConnection();
1790 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
1791 EXPECT_EQ(DataChannelInterface::kClosed, data2->state());
1792}
1793
1794// This test that data channels can be rejected in an answer.
1795TEST_F(PeerConnectionInterfaceTest, TestRejectDataChannelInAnswer) {
1796 FakeConstraints constraints;
1797 constraints.SetAllowRtpDataChannels();
1798 CreatePeerConnection(&constraints);
1799
1800 scoped_refptr<DataChannelInterface> offer_channel(
1801 pc_->CreateDataChannel("offer_channel", NULL));
1802
1803 CreateOfferAsLocalDescription();
1804
1805 // Create an answer where the m-line for data channels are rejected.
1806 std::string sdp;
1807 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
1808 webrtc::JsepSessionDescription* answer = new webrtc::JsepSessionDescription(
1809 SessionDescriptionInterface::kAnswer);
1810 EXPECT_TRUE(answer->Initialize(sdp, NULL));
1811 cricket::ContentInfo* data_info =
1812 answer->description()->GetContentByName("data");
1813 data_info->rejected = true;
1814
1815 DoSetRemoteDescription(answer);
1816 EXPECT_EQ(DataChannelInterface::kClosed, offer_channel->state());
1817}
1818
1819// Test that we can create a session description from an SDP string from
1820// FireFox, use it as a remote session description, generate an answer and use
1821// the answer as a local description.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07001822TEST_F(PeerConnectionInterfaceTest, ReceiveFireFoxOffer) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001823 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001824 FakeConstraints constraints;
1825 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
1826 true);
1827 CreatePeerConnection(&constraints);
1828 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
1829 SessionDescriptionInterface* desc =
1830 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
jbauchfabe2c92015-07-16 13:43:14 -07001831 webrtc::kFireFoxSdpOffer, nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001832 EXPECT_TRUE(DoSetSessionDescription(desc, false));
1833 CreateAnswerAsLocalDescription();
1834 ASSERT_TRUE(pc_->local_description() != NULL);
1835 ASSERT_TRUE(pc_->remote_description() != NULL);
1836
1837 const cricket::ContentInfo* content =
1838 cricket::GetFirstAudioContent(pc_->local_description()->description());
1839 ASSERT_TRUE(content != NULL);
1840 EXPECT_FALSE(content->rejected);
1841
1842 content =
1843 cricket::GetFirstVideoContent(pc_->local_description()->description());
1844 ASSERT_TRUE(content != NULL);
1845 EXPECT_FALSE(content->rejected);
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00001846#ifdef HAVE_SCTP
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001847 content =
1848 cricket::GetFirstDataContent(pc_->local_description()->description());
1849 ASSERT_TRUE(content != NULL);
1850 EXPECT_TRUE(content->rejected);
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00001851#endif
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001852}
1853
1854// Test that we can create an audio only offer and receive an answer with a
1855// limited set of audio codecs and receive an updated offer with more audio
1856// codecs, where the added codecs are not supported.
1857TEST_F(PeerConnectionInterfaceTest, ReceiveUpdatedAudioOfferWithBadCodecs) {
1858 CreatePeerConnection();
1859 AddVoiceStream("audio_label");
1860 CreateOfferAsLocalDescription();
1861
1862 SessionDescriptionInterface* answer =
1863 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
jbauchfabe2c92015-07-16 13:43:14 -07001864 webrtc::kAudioSdp, nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001865 EXPECT_TRUE(DoSetSessionDescription(answer, false));
1866
1867 SessionDescriptionInterface* updated_offer =
1868 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
jbauchfabe2c92015-07-16 13:43:14 -07001869 webrtc::kAudioSdpWithUnsupportedCodecs,
1870 nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001871 EXPECT_TRUE(DoSetSessionDescription(updated_offer, false));
1872 CreateAnswerAsLocalDescription();
1873}
1874
deadbeefc80741f2015-10-22 13:14:45 -07001875// Test that if we're receiving (but not sending) a track, subsequent offers
1876// will have m-lines with a=recvonly.
1877TEST_F(PeerConnectionInterfaceTest, CreateSubsequentRecvOnlyOffer) {
1878 FakeConstraints constraints;
1879 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
1880 true);
1881 CreatePeerConnection(&constraints);
1882 CreateAndSetRemoteOffer(kSdpStringWithStream1);
1883 CreateAnswerAsLocalDescription();
1884
1885 // At this point we should be receiving stream 1, but not sending anything.
1886 // A new offer should be recvonly.
kwibergd1fe2812016-04-27 06:47:29 -07001887 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefc80741f2015-10-22 13:14:45 -07001888 DoCreateOffer(&offer, nullptr);
1889
1890 const cricket::ContentInfo* video_content =
1891 cricket::GetFirstVideoContent(offer->description());
1892 const cricket::VideoContentDescription* video_desc =
1893 static_cast<const cricket::VideoContentDescription*>(
1894 video_content->description);
1895 ASSERT_EQ(cricket::MD_RECVONLY, video_desc->direction());
1896
1897 const cricket::ContentInfo* audio_content =
1898 cricket::GetFirstAudioContent(offer->description());
1899 const cricket::AudioContentDescription* audio_desc =
1900 static_cast<const cricket::AudioContentDescription*>(
1901 audio_content->description);
1902 ASSERT_EQ(cricket::MD_RECVONLY, audio_desc->direction());
1903}
1904
1905// Test that if we're receiving (but not sending) a track, and the
1906// offerToReceiveVideo/offerToReceiveAudio constraints are explicitly set to
1907// false, the generated m-lines will be a=inactive.
1908TEST_F(PeerConnectionInterfaceTest, CreateSubsequentInactiveOffer) {
1909 FakeConstraints constraints;
1910 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
1911 true);
1912 CreatePeerConnection(&constraints);
1913 CreateAndSetRemoteOffer(kSdpStringWithStream1);
1914 CreateAnswerAsLocalDescription();
1915
1916 // At this point we should be receiving stream 1, but not sending anything.
1917 // A new offer would be recvonly, but we'll set the "no receive" constraints
1918 // to make it inactive.
kwibergd1fe2812016-04-27 06:47:29 -07001919 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefc80741f2015-10-22 13:14:45 -07001920 FakeConstraints offer_constraints;
1921 offer_constraints.AddMandatory(
1922 webrtc::MediaConstraintsInterface::kOfferToReceiveVideo, false);
1923 offer_constraints.AddMandatory(
1924 webrtc::MediaConstraintsInterface::kOfferToReceiveAudio, false);
1925 DoCreateOffer(&offer, &offer_constraints);
1926
1927 const cricket::ContentInfo* video_content =
1928 cricket::GetFirstVideoContent(offer->description());
1929 const cricket::VideoContentDescription* video_desc =
1930 static_cast<const cricket::VideoContentDescription*>(
1931 video_content->description);
1932 ASSERT_EQ(cricket::MD_INACTIVE, video_desc->direction());
1933
1934 const cricket::ContentInfo* audio_content =
1935 cricket::GetFirstAudioContent(offer->description());
1936 const cricket::AudioContentDescription* audio_desc =
1937 static_cast<const cricket::AudioContentDescription*>(
1938 audio_content->description);
1939 ASSERT_EQ(cricket::MD_INACTIVE, audio_desc->direction());
1940}
1941
deadbeef653b8e02015-11-11 12:55:10 -08001942// Test that we can use SetConfiguration to change the ICE servers of the
1943// PortAllocator.
1944TEST_F(PeerConnectionInterfaceTest, SetConfigurationChangesIceServers) {
1945 CreatePeerConnection();
1946
1947 PeerConnectionInterface::RTCConfiguration config;
1948 PeerConnectionInterface::IceServer server;
1949 server.uri = "stun:test_hostname";
1950 config.servers.push_back(server);
1951 EXPECT_TRUE(pc_->SetConfiguration(config));
1952
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08001953 EXPECT_EQ(1u, port_allocator_->stun_servers().size());
1954 EXPECT_EQ("test_hostname",
1955 port_allocator_->stun_servers().begin()->hostname());
deadbeef653b8e02015-11-11 12:55:10 -08001956}
1957
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001958TEST_F(PeerConnectionInterfaceTest, SetConfigurationChangesCandidateFilter) {
1959 CreatePeerConnection();
1960 PeerConnectionInterface::RTCConfiguration config;
1961 config.type = PeerConnectionInterface::kRelay;
1962 EXPECT_TRUE(pc_->SetConfiguration(config));
1963 EXPECT_EQ(cricket::CF_RELAY, port_allocator_->candidate_filter());
1964}
1965
1966// Test that when SetConfiguration changes both the pool size and other
1967// attributes, the pooled session is created with the updated attributes.
1968TEST_F(PeerConnectionInterfaceTest,
1969 SetConfigurationCreatesPooledSessionCorrectly) {
1970 CreatePeerConnection();
1971 PeerConnectionInterface::RTCConfiguration config;
1972 config.ice_candidate_pool_size = 1;
1973 PeerConnectionInterface::IceServer server;
1974 server.uri = kStunAddressOnly;
1975 config.servers.push_back(server);
1976 config.type = PeerConnectionInterface::kRelay;
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001977 EXPECT_TRUE(pc_->SetConfiguration(config));
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001978
1979 const cricket::FakePortAllocatorSession* session =
1980 static_cast<const cricket::FakePortAllocatorSession*>(
1981 port_allocator_->GetPooledSession());
1982 ASSERT_NE(nullptr, session);
1983 EXPECT_EQ(1UL, session->stun_servers().size());
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001984}
1985
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001986// Test that PeerConnection::Close changes the states to closed and all remote
1987// tracks change state to ended.
1988TEST_F(PeerConnectionInterfaceTest, CloseAndTestStreamsAndStates) {
1989 // Initialize a PeerConnection and negotiate local and remote session
1990 // description.
1991 InitiateCall();
1992 ASSERT_EQ(1u, pc_->local_streams()->count());
1993 ASSERT_EQ(1u, pc_->remote_streams()->count());
1994
1995 pc_->Close();
1996
1997 EXPECT_EQ(PeerConnectionInterface::kClosed, pc_->signaling_state());
1998 EXPECT_EQ(PeerConnectionInterface::kIceConnectionClosed,
1999 pc_->ice_connection_state());
2000 EXPECT_EQ(PeerConnectionInterface::kIceGatheringComplete,
2001 pc_->ice_gathering_state());
2002
2003 EXPECT_EQ(1u, pc_->local_streams()->count());
2004 EXPECT_EQ(1u, pc_->remote_streams()->count());
2005
2006 scoped_refptr<MediaStreamInterface> remote_stream =
2007 pc_->remote_streams()->at(0);
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002008 // Track state may be updated asynchronously.
perkjd61bf802016-03-24 03:16:19 -07002009 EXPECT_EQ_WAIT(MediaStreamTrackInterface::kEnded,
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002010 remote_stream->GetAudioTracks()[0]->state(), kTimeout);
2011 EXPECT_EQ_WAIT(MediaStreamTrackInterface::kEnded,
2012 remote_stream->GetVideoTracks()[0]->state(), kTimeout);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002013}
2014
2015// Test that PeerConnection methods fails gracefully after
2016// PeerConnection::Close has been called.
2017TEST_F(PeerConnectionInterfaceTest, CloseAndTestMethods) {
2018 CreatePeerConnection();
2019 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
2020 CreateOfferAsRemoteDescription();
2021 CreateAnswerAsLocalDescription();
2022
2023 ASSERT_EQ(1u, pc_->local_streams()->count());
2024 scoped_refptr<MediaStreamInterface> local_stream =
2025 pc_->local_streams()->at(0);
2026
2027 pc_->Close();
2028
2029 pc_->RemoveStream(local_stream);
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +00002030 EXPECT_FALSE(pc_->AddStream(local_stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002031
2032 ASSERT_FALSE(local_stream->GetAudioTracks().empty());
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002033 rtc::scoped_refptr<webrtc::DtmfSenderInterface> dtmf_sender(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002034 pc_->CreateDtmfSender(local_stream->GetAudioTracks()[0]));
wu@webrtc.org66037362013-08-13 00:09:35 +00002035 EXPECT_TRUE(NULL == dtmf_sender); // local stream has been removed.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002036
2037 EXPECT_TRUE(pc_->CreateDataChannel("test", NULL) == NULL);
2038
2039 EXPECT_TRUE(pc_->local_description() != NULL);
2040 EXPECT_TRUE(pc_->remote_description() != NULL);
2041
kwibergd1fe2812016-04-27 06:47:29 -07002042 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07002043 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
kwibergd1fe2812016-04-27 06:47:29 -07002044 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -07002045 EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002046
2047 std::string sdp;
2048 ASSERT_TRUE(pc_->remote_description()->ToString(&sdp));
2049 SessionDescriptionInterface* remote_offer =
2050 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
2051 sdp, NULL);
2052 EXPECT_FALSE(DoSetRemoteDescription(remote_offer));
2053
2054 ASSERT_TRUE(pc_->local_description()->ToString(&sdp));
2055 SessionDescriptionInterface* local_offer =
2056 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
2057 sdp, NULL);
2058 EXPECT_FALSE(DoSetLocalDescription(local_offer));
2059}
2060
2061// Test that GetStats can still be called after PeerConnection::Close.
2062TEST_F(PeerConnectionInterfaceTest, CloseAndGetStats) {
2063 InitiateCall();
2064 pc_->Close();
2065 DoGetStats(NULL);
2066}
deadbeefab9b2d12015-10-14 11:33:11 -07002067
2068// NOTE: The series of tests below come from what used to be
2069// mediastreamsignaling_unittest.cc, and are mostly aimed at testing that
2070// setting a remote or local description has the expected effects.
2071
2072// This test verifies that the remote MediaStreams corresponding to a received
2073// SDP string is created. In this test the two separate MediaStreams are
2074// signaled.
2075TEST_F(PeerConnectionInterfaceTest, UpdateRemoteStreams) {
2076 FakeConstraints constraints;
2077 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2078 true);
2079 CreatePeerConnection(&constraints);
2080 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2081
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002082 rtc::scoped_refptr<StreamCollection> reference(CreateStreamCollection(1, 1));
deadbeefab9b2d12015-10-14 11:33:11 -07002083 EXPECT_TRUE(
2084 CompareStreamCollections(observer_.remote_streams(), reference.get()));
2085 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2086 EXPECT_TRUE(remote_stream->GetVideoTracks()[0]->GetSource() != nullptr);
2087
2088 // Create a session description based on another SDP with another
2089 // MediaStream.
2090 CreateAndSetRemoteOffer(kSdpStringWithStream1And2);
2091
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002092 rtc::scoped_refptr<StreamCollection> reference2(CreateStreamCollection(2, 1));
deadbeefab9b2d12015-10-14 11:33:11 -07002093 EXPECT_TRUE(
2094 CompareStreamCollections(observer_.remote_streams(), reference2.get()));
2095}
2096
2097// This test verifies that when remote tracks are added/removed from SDP, the
2098// created remote streams are updated appropriately.
2099TEST_F(PeerConnectionInterfaceTest,
2100 AddRemoveTrackFromExistingRemoteMediaStream) {
2101 FakeConstraints constraints;
2102 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2103 true);
2104 CreatePeerConnection(&constraints);
kwibergd1fe2812016-04-27 06:47:29 -07002105 std::unique_ptr<SessionDescriptionInterface> desc_ms1 =
kwiberg2bbff992016-03-16 11:03:04 -07002106 CreateSessionDescriptionAndReference(1, 1);
deadbeefab9b2d12015-10-14 11:33:11 -07002107 EXPECT_TRUE(DoSetRemoteDescription(desc_ms1.release()));
2108 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
2109 reference_collection_));
2110
2111 // Add extra audio and video tracks to the same MediaStream.
kwibergd1fe2812016-04-27 06:47:29 -07002112 std::unique_ptr<SessionDescriptionInterface> desc_ms1_two_tracks =
kwiberg2bbff992016-03-16 11:03:04 -07002113 CreateSessionDescriptionAndReference(2, 2);
deadbeefab9b2d12015-10-14 11:33:11 -07002114 EXPECT_TRUE(DoSetRemoteDescription(desc_ms1_two_tracks.release()));
2115 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
2116 reference_collection_));
perkjd61bf802016-03-24 03:16:19 -07002117 scoped_refptr<AudioTrackInterface> audio_track2 =
2118 observer_.remote_streams()->at(0)->GetAudioTracks()[1];
2119 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, audio_track2->state());
2120 scoped_refptr<VideoTrackInterface> video_track2 =
2121 observer_.remote_streams()->at(0)->GetVideoTracks()[1];
2122 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, video_track2->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002123
2124 // Remove the extra audio and video tracks.
kwibergd1fe2812016-04-27 06:47:29 -07002125 std::unique_ptr<SessionDescriptionInterface> desc_ms2 =
kwiberg2bbff992016-03-16 11:03:04 -07002126 CreateSessionDescriptionAndReference(1, 1);
perkjd61bf802016-03-24 03:16:19 -07002127 MockTrackObserver audio_track_observer(audio_track2);
2128 MockTrackObserver video_track_observer(video_track2);
2129
2130 EXPECT_CALL(audio_track_observer, OnChanged()).Times(Exactly(1));
2131 EXPECT_CALL(video_track_observer, OnChanged()).Times(Exactly(1));
deadbeefab9b2d12015-10-14 11:33:11 -07002132 EXPECT_TRUE(DoSetRemoteDescription(desc_ms2.release()));
2133 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
2134 reference_collection_));
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002135 // Track state may be updated asynchronously.
perkjd61bf802016-03-24 03:16:19 -07002136 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002137 audio_track2->state(), kTimeout);
2138 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
2139 video_track2->state(), kTimeout);
deadbeefab9b2d12015-10-14 11:33:11 -07002140}
2141
2142// This tests that remote tracks are ended if a local session description is set
2143// that rejects the media content type.
2144TEST_F(PeerConnectionInterfaceTest, RejectMediaContent) {
2145 FakeConstraints constraints;
2146 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2147 true);
2148 CreatePeerConnection(&constraints);
2149 // First create and set a remote offer, then reject its video content in our
2150 // answer.
2151 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2152 ASSERT_EQ(1u, observer_.remote_streams()->count());
2153 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2154 ASSERT_EQ(1u, remote_stream->GetVideoTracks().size());
2155 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2156
2157 rtc::scoped_refptr<webrtc::VideoTrackInterface> remote_video =
2158 remote_stream->GetVideoTracks()[0];
2159 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, remote_video->state());
2160 rtc::scoped_refptr<webrtc::AudioTrackInterface> remote_audio =
2161 remote_stream->GetAudioTracks()[0];
2162 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, remote_audio->state());
2163
kwibergd1fe2812016-04-27 06:47:29 -07002164 std::unique_ptr<SessionDescriptionInterface> local_answer;
kwiberg2bbff992016-03-16 11:03:04 -07002165 EXPECT_TRUE(DoCreateAnswer(&local_answer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07002166 cricket::ContentInfo* video_info =
2167 local_answer->description()->GetContentByName("video");
2168 video_info->rejected = true;
2169 EXPECT_TRUE(DoSetLocalDescription(local_answer.release()));
2170 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kEnded, remote_video->state());
2171 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, remote_audio->state());
2172
2173 // Now create an offer where we reject both video and audio.
kwibergd1fe2812016-04-27 06:47:29 -07002174 std::unique_ptr<SessionDescriptionInterface> local_offer;
kwiberg2bbff992016-03-16 11:03:04 -07002175 EXPECT_TRUE(DoCreateOffer(&local_offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07002176 video_info = local_offer->description()->GetContentByName("video");
2177 ASSERT_TRUE(video_info != nullptr);
2178 video_info->rejected = true;
2179 cricket::ContentInfo* audio_info =
2180 local_offer->description()->GetContentByName("audio");
2181 ASSERT_TRUE(audio_info != nullptr);
2182 audio_info->rejected = true;
2183 EXPECT_TRUE(DoSetLocalDescription(local_offer.release()));
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002184 // Track state may be updated asynchronously.
perkjd61bf802016-03-24 03:16:19 -07002185 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002186 remote_audio->state(), kTimeout);
2187 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
2188 remote_video->state(), kTimeout);
deadbeefab9b2d12015-10-14 11:33:11 -07002189}
2190
2191// This tests that we won't crash if the remote track has been removed outside
2192// of PeerConnection and then PeerConnection tries to reject the track.
2193TEST_F(PeerConnectionInterfaceTest, RemoveTrackThenRejectMediaContent) {
2194 FakeConstraints constraints;
2195 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2196 true);
2197 CreatePeerConnection(&constraints);
2198 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2199 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2200 remote_stream->RemoveTrack(remote_stream->GetVideoTracks()[0]);
2201 remote_stream->RemoveTrack(remote_stream->GetAudioTracks()[0]);
2202
kwibergd1fe2812016-04-27 06:47:29 -07002203 std::unique_ptr<SessionDescriptionInterface> local_answer(
deadbeefab9b2d12015-10-14 11:33:11 -07002204 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
2205 kSdpStringWithStream1, nullptr));
2206 cricket::ContentInfo* video_info =
2207 local_answer->description()->GetContentByName("video");
2208 video_info->rejected = true;
2209 cricket::ContentInfo* audio_info =
2210 local_answer->description()->GetContentByName("audio");
2211 audio_info->rejected = true;
2212 EXPECT_TRUE(DoSetLocalDescription(local_answer.release()));
2213
2214 // No crash is a pass.
2215}
2216
deadbeef5e97fb52015-10-15 12:49:08 -07002217// This tests that if a recvonly remote description is set, no remote streams
2218// will be created, even if the description contains SSRCs/MSIDs.
2219// See: https://code.google.com/p/webrtc/issues/detail?id=5054
2220TEST_F(PeerConnectionInterfaceTest, RecvonlyDescriptionDoesntCreateStream) {
2221 FakeConstraints constraints;
2222 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2223 true);
2224 CreatePeerConnection(&constraints);
2225
2226 std::string recvonly_offer = kSdpStringWithStream1;
2227 rtc::replace_substrs(kSendrecv, strlen(kSendrecv), kRecvonly,
2228 strlen(kRecvonly), &recvonly_offer);
2229 CreateAndSetRemoteOffer(recvonly_offer);
2230
2231 EXPECT_EQ(0u, observer_.remote_streams()->count());
2232}
2233
deadbeefab9b2d12015-10-14 11:33:11 -07002234// This tests that a default MediaStream is created if a remote session
2235// description doesn't contain any streams and no MSID support.
2236// It also tests that the default stream is updated if a video m-line is added
2237// in a subsequent session description.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002238TEST_F(PeerConnectionInterfaceTest, SdpWithoutMsidCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002239 FakeConstraints constraints;
2240 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2241 true);
2242 CreatePeerConnection(&constraints);
2243 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2244
2245 ASSERT_EQ(1u, observer_.remote_streams()->count());
2246 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2247
2248 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2249 EXPECT_EQ(0u, remote_stream->GetVideoTracks().size());
2250 EXPECT_EQ("default", remote_stream->label());
2251
2252 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2253 ASSERT_EQ(1u, observer_.remote_streams()->count());
2254 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2255 EXPECT_EQ("defaulta0", remote_stream->GetAudioTracks()[0]->id());
deadbeef884f5852016-01-15 09:20:04 -08002256 EXPECT_EQ(MediaStreamTrackInterface::kLive,
2257 remote_stream->GetAudioTracks()[0]->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002258 ASSERT_EQ(1u, remote_stream->GetVideoTracks().size());
2259 EXPECT_EQ("defaultv0", remote_stream->GetVideoTracks()[0]->id());
deadbeef884f5852016-01-15 09:20:04 -08002260 EXPECT_EQ(MediaStreamTrackInterface::kLive,
2261 remote_stream->GetVideoTracks()[0]->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002262}
2263
2264// This tests that a default MediaStream is created if a remote session
2265// description doesn't contain any streams and media direction is send only.
2266TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002267 SendOnlySdpWithoutMsidCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002268 FakeConstraints constraints;
2269 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2270 true);
2271 CreatePeerConnection(&constraints);
2272 CreateAndSetRemoteOffer(kSdpStringSendOnlyWithoutStreams);
2273
2274 ASSERT_EQ(1u, observer_.remote_streams()->count());
2275 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2276
2277 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2278 EXPECT_EQ(1u, remote_stream->GetVideoTracks().size());
2279 EXPECT_EQ("default", remote_stream->label());
2280}
2281
2282// This tests that it won't crash when PeerConnection tries to remove
2283// a remote track that as already been removed from the MediaStream.
2284TEST_F(PeerConnectionInterfaceTest, RemoveAlreadyGoneRemoteStream) {
2285 FakeConstraints constraints;
2286 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2287 true);
2288 CreatePeerConnection(&constraints);
2289 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2290 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2291 remote_stream->RemoveTrack(remote_stream->GetAudioTracks()[0]);
2292 remote_stream->RemoveTrack(remote_stream->GetVideoTracks()[0]);
2293
2294 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2295
2296 // No crash is a pass.
2297}
2298
2299// This tests that a default MediaStream is created if the remote session
2300// description doesn't contain any streams and don't contain an indication if
2301// MSID is supported.
2302TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002303 SdpWithoutMsidAndStreamsCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002304 FakeConstraints constraints;
2305 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2306 true);
2307 CreatePeerConnection(&constraints);
2308 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2309
2310 ASSERT_EQ(1u, observer_.remote_streams()->count());
2311 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2312 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2313 EXPECT_EQ(1u, remote_stream->GetVideoTracks().size());
2314}
2315
2316// This tests that a default MediaStream is not created if the remote session
2317// description doesn't contain any streams but does support MSID.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002318TEST_F(PeerConnectionInterfaceTest, SdpWithMsidDontCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002319 FakeConstraints constraints;
2320 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2321 true);
2322 CreatePeerConnection(&constraints);
2323 CreateAndSetRemoteOffer(kSdpStringWithMsidWithoutStreams);
2324 EXPECT_EQ(0u, observer_.remote_streams()->count());
2325}
2326
deadbeefbda7e0b2015-12-08 17:13:40 -08002327// This tests that when setting a new description, the old default tracks are
2328// not destroyed and recreated.
2329// See: https://bugs.chromium.org/p/webrtc/issues/detail?id=5250
Stefan Holmer102362b2016-03-18 09:39:07 +01002330TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002331 DefaultTracksNotDestroyedAndRecreated) {
deadbeefbda7e0b2015-12-08 17:13:40 -08002332 FakeConstraints constraints;
2333 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2334 true);
2335 CreatePeerConnection(&constraints);
2336 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2337
2338 ASSERT_EQ(1u, observer_.remote_streams()->count());
2339 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2340 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2341
2342 // Set the track to "disabled", then set a new description and ensure the
2343 // track is still disabled, which ensures it hasn't been recreated.
2344 remote_stream->GetAudioTracks()[0]->set_enabled(false);
2345 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2346 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2347 EXPECT_FALSE(remote_stream->GetAudioTracks()[0]->enabled());
2348}
2349
deadbeefab9b2d12015-10-14 11:33:11 -07002350// This tests that a default MediaStream is not created if a remote session
2351// description is updated to not have any MediaStreams.
2352TEST_F(PeerConnectionInterfaceTest, VerifyDefaultStreamIsNotCreated) {
2353 FakeConstraints constraints;
2354 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2355 true);
2356 CreatePeerConnection(&constraints);
2357 CreateAndSetRemoteOffer(kSdpStringWithStream1);
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002358 rtc::scoped_refptr<StreamCollection> reference(CreateStreamCollection(1, 1));
deadbeefab9b2d12015-10-14 11:33:11 -07002359 EXPECT_TRUE(
2360 CompareStreamCollections(observer_.remote_streams(), reference.get()));
2361
2362 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2363 EXPECT_EQ(0u, observer_.remote_streams()->count());
2364}
2365
2366// This tests that an RtpSender is created when the local description is set
2367// after adding a local stream.
2368// TODO(deadbeef): This test and the one below it need to be updated when
2369// an RtpSender's lifetime isn't determined by when a local description is set.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002370TEST_F(PeerConnectionInterfaceTest, LocalDescriptionChanged) {
deadbeefab9b2d12015-10-14 11:33:11 -07002371 FakeConstraints constraints;
2372 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2373 true);
2374 CreatePeerConnection(&constraints);
deadbeefab9b2d12015-10-14 11:33:11 -07002375
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002376 // Create an offer with 1 stream with 2 tracks of each type.
2377 rtc::scoped_refptr<StreamCollection> stream_collection =
2378 CreateStreamCollection(1, 2);
2379 pc_->AddStream(stream_collection->at(0));
2380 std::unique_ptr<SessionDescriptionInterface> offer;
2381 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2382 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
deadbeefab9b2d12015-10-14 11:33:11 -07002383
deadbeefab9b2d12015-10-14 11:33:11 -07002384 auto senders = pc_->GetSenders();
2385 EXPECT_EQ(4u, senders.size());
2386 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2387 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2388 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[1]));
2389 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[1]));
2390
2391 // Remove an audio and video track.
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002392 pc_->RemoveStream(stream_collection->at(0));
2393 stream_collection = CreateStreamCollection(1, 1);
2394 pc_->AddStream(stream_collection->at(0));
2395 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2396 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
2397
deadbeefab9b2d12015-10-14 11:33:11 -07002398 senders = pc_->GetSenders();
2399 EXPECT_EQ(2u, senders.size());
2400 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2401 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2402 EXPECT_FALSE(ContainsSender(senders, kAudioTracks[1]));
2403 EXPECT_FALSE(ContainsSender(senders, kVideoTracks[1]));
2404}
2405
2406// This tests that an RtpSender is created when the local description is set
2407// before adding a local stream.
2408TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002409 AddLocalStreamAfterLocalDescriptionChanged) {
deadbeefab9b2d12015-10-14 11:33:11 -07002410 FakeConstraints constraints;
2411 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2412 true);
2413 CreatePeerConnection(&constraints);
deadbeefab9b2d12015-10-14 11:33:11 -07002414
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002415 rtc::scoped_refptr<StreamCollection> stream_collection =
2416 CreateStreamCollection(1, 2);
2417 // Add a stream to create the offer, but remove it afterwards.
2418 pc_->AddStream(stream_collection->at(0));
2419 std::unique_ptr<SessionDescriptionInterface> offer;
2420 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2421 pc_->RemoveStream(stream_collection->at(0));
deadbeefab9b2d12015-10-14 11:33:11 -07002422
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002423 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
deadbeefab9b2d12015-10-14 11:33:11 -07002424 auto senders = pc_->GetSenders();
2425 EXPECT_EQ(0u, senders.size());
2426
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002427 pc_->AddStream(stream_collection->at(0));
deadbeefab9b2d12015-10-14 11:33:11 -07002428 senders = pc_->GetSenders();
2429 EXPECT_EQ(4u, senders.size());
2430 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2431 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2432 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[1]));
2433 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[1]));
2434}
2435
2436// This tests that the expected behavior occurs if the SSRC on a local track is
2437// changed when SetLocalDescription is called.
2438TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002439 ChangeSsrcOnTrackInLocalSessionDescription) {
deadbeefab9b2d12015-10-14 11:33:11 -07002440 FakeConstraints constraints;
2441 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2442 true);
2443 CreatePeerConnection(&constraints);
deadbeefab9b2d12015-10-14 11:33:11 -07002444
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002445 rtc::scoped_refptr<StreamCollection> stream_collection =
2446 CreateStreamCollection(2, 1);
2447 pc_->AddStream(stream_collection->at(0));
2448 std::unique_ptr<SessionDescriptionInterface> offer;
2449 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2450 // Grab a copy of the offer before it gets passed into the PC.
2451 std::unique_ptr<JsepSessionDescription> modified_offer(
2452 new JsepSessionDescription(JsepSessionDescription::kOffer));
2453 modified_offer->Initialize(offer->description()->Copy(), offer->session_id(),
2454 offer->session_version());
2455 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
deadbeefab9b2d12015-10-14 11:33:11 -07002456
deadbeefab9b2d12015-10-14 11:33:11 -07002457 auto senders = pc_->GetSenders();
2458 EXPECT_EQ(2u, senders.size());
2459 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2460 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2461
2462 // Change the ssrc of the audio and video track.
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002463 cricket::MediaContentDescription* desc =
2464 cricket::GetFirstAudioContentDescription(modified_offer->description());
2465 ASSERT_TRUE(desc != NULL);
2466 for (StreamParams& stream : desc->mutable_streams()) {
2467 for (unsigned int& ssrc : stream.ssrcs) {
2468 ++ssrc;
2469 }
2470 }
deadbeefab9b2d12015-10-14 11:33:11 -07002471
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002472 desc =
2473 cricket::GetFirstVideoContentDescription(modified_offer->description());
2474 ASSERT_TRUE(desc != NULL);
2475 for (StreamParams& stream : desc->mutable_streams()) {
2476 for (unsigned int& ssrc : stream.ssrcs) {
2477 ++ssrc;
2478 }
2479 }
2480
2481 EXPECT_TRUE(DoSetLocalDescription(modified_offer.release()));
deadbeefab9b2d12015-10-14 11:33:11 -07002482 senders = pc_->GetSenders();
2483 EXPECT_EQ(2u, senders.size());
2484 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2485 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2486 // TODO(deadbeef): Once RtpSenders expose parameters, check that the SSRC
2487 // changed.
2488}
2489
2490// This tests that the expected behavior occurs if a new session description is
2491// set with the same tracks, but on a different MediaStream.
Stefan Holmer55d6e7c2016-03-17 16:26:40 +01002492TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002493 SignalSameTracksInSeparateMediaStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002494 FakeConstraints constraints;
2495 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2496 true);
2497 CreatePeerConnection(&constraints);
deadbeefab9b2d12015-10-14 11:33:11 -07002498
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002499 rtc::scoped_refptr<StreamCollection> stream_collection =
2500 CreateStreamCollection(2, 1);
2501 pc_->AddStream(stream_collection->at(0));
2502 std::unique_ptr<SessionDescriptionInterface> offer;
2503 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2504 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
deadbeefab9b2d12015-10-14 11:33:11 -07002505
deadbeefab9b2d12015-10-14 11:33:11 -07002506 auto senders = pc_->GetSenders();
2507 EXPECT_EQ(2u, senders.size());
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002508 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0], kStreams[0]));
2509 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0], kStreams[0]));
deadbeefab9b2d12015-10-14 11:33:11 -07002510
2511 // Add a new MediaStream but with the same tracks as in the first stream.
2512 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream_1(
2513 webrtc::MediaStream::Create(kStreams[1]));
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002514 stream_1->AddTrack(stream_collection->at(0)->GetVideoTracks()[0]);
2515 stream_1->AddTrack(stream_collection->at(0)->GetAudioTracks()[0]);
deadbeefab9b2d12015-10-14 11:33:11 -07002516 pc_->AddStream(stream_1);
2517
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002518 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2519 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
deadbeefab9b2d12015-10-14 11:33:11 -07002520
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002521 auto new_senders = pc_->GetSenders();
2522 // Should be the same senders as before, but with updated stream id.
2523 // Note that this behavior is subject to change in the future.
2524 // We may decide the PC should ignore existing tracks in AddStream.
2525 EXPECT_EQ(senders, new_senders);
2526 EXPECT_TRUE(ContainsSender(new_senders, kAudioTracks[0], kStreams[1]));
2527 EXPECT_TRUE(ContainsSender(new_senders, kVideoTracks[0], kStreams[1]));
deadbeefab9b2d12015-10-14 11:33:11 -07002528}
2529
nisse51542be2016-02-12 02:27:06 -08002530// The PeerConnectionMediaConfig tests below verify that configuration
2531// and constraints are propagated into the MediaConfig passed to
2532// CreateMediaController. These settings are intended for MediaChannel
2533// constructors, but that is not exercised by these unittest.
2534class PeerConnectionFactoryForTest : public webrtc::PeerConnectionFactory {
2535 public:
2536 webrtc::MediaControllerInterface* CreateMediaController(
2537 const cricket::MediaConfig& config) const override {
2538 create_media_controller_called_ = true;
2539 create_media_controller_config_ = config;
2540
2541 webrtc::MediaControllerInterface* mc =
2542 PeerConnectionFactory::CreateMediaController(config);
2543 EXPECT_TRUE(mc != nullptr);
2544 return mc;
2545 }
2546
2547 // Mutable, so they can be modified in the above const-declared method.
2548 mutable bool create_media_controller_called_ = false;
2549 mutable cricket::MediaConfig create_media_controller_config_;
2550};
2551
2552class PeerConnectionMediaConfigTest : public testing::Test {
2553 protected:
2554 void SetUp() override {
nisseaf510af2016-03-21 08:20:42 -07002555 pcf_ = new rtc::RefCountedObject<PeerConnectionFactoryForTest>();
nisse51542be2016-02-12 02:27:06 -08002556 pcf_->Initialize();
2557 }
2558 const cricket::MediaConfig& TestCreatePeerConnection(
2559 const PeerConnectionInterface::RTCConfiguration& config,
2560 const MediaConstraintsInterface *constraints) {
2561 pcf_->create_media_controller_called_ = false;
2562
2563 scoped_refptr<PeerConnectionInterface> pc(
2564 pcf_->CreatePeerConnection(config, constraints, nullptr, nullptr,
2565 &observer_));
2566 EXPECT_TRUE(pc.get());
2567 EXPECT_TRUE(pcf_->create_media_controller_called_);
2568 return pcf_->create_media_controller_config_;
2569 }
2570
2571 scoped_refptr<PeerConnectionFactoryForTest> pcf_;
2572 MockPeerConnectionObserver observer_;
2573};
2574
2575// This test verifies the default behaviour with no constraints and a
2576// default RTCConfiguration.
2577TEST_F(PeerConnectionMediaConfigTest, TestDefaults) {
2578 PeerConnectionInterface::RTCConfiguration config;
2579 FakeConstraints constraints;
2580
2581 const cricket::MediaConfig& media_config =
2582 TestCreatePeerConnection(config, &constraints);
2583
2584 EXPECT_FALSE(media_config.enable_dscp);
nisse0db023a2016-03-01 04:29:59 -08002585 EXPECT_TRUE(media_config.video.enable_cpu_overuse_detection);
2586 EXPECT_FALSE(media_config.video.disable_prerenderer_smoothing);
2587 EXPECT_FALSE(media_config.video.suspend_below_min_bitrate);
nisse51542be2016-02-12 02:27:06 -08002588}
2589
2590// This test verifies the DSCP constraint is recognized and passed to
2591// the CreateMediaController call.
2592TEST_F(PeerConnectionMediaConfigTest, TestDscpConstraintTrue) {
2593 PeerConnectionInterface::RTCConfiguration config;
2594 FakeConstraints constraints;
2595
2596 constraints.AddOptional(webrtc::MediaConstraintsInterface::kEnableDscp, true);
2597 const cricket::MediaConfig& media_config =
2598 TestCreatePeerConnection(config, &constraints);
2599
2600 EXPECT_TRUE(media_config.enable_dscp);
2601}
2602
2603// This test verifies the cpu overuse detection constraint is
2604// recognized and passed to the CreateMediaController call.
2605TEST_F(PeerConnectionMediaConfigTest, TestCpuOveruseConstraintFalse) {
2606 PeerConnectionInterface::RTCConfiguration config;
2607 FakeConstraints constraints;
2608
2609 constraints.AddOptional(
2610 webrtc::MediaConstraintsInterface::kCpuOveruseDetection, false);
2611 const cricket::MediaConfig media_config =
2612 TestCreatePeerConnection(config, &constraints);
2613
nisse0db023a2016-03-01 04:29:59 -08002614 EXPECT_FALSE(media_config.video.enable_cpu_overuse_detection);
nisse51542be2016-02-12 02:27:06 -08002615}
2616
2617// This test verifies that the disable_prerenderer_smoothing flag is
2618// propagated from RTCConfiguration to the CreateMediaController call.
2619TEST_F(PeerConnectionMediaConfigTest, TestDisablePrerendererSmoothingTrue) {
2620 PeerConnectionInterface::RTCConfiguration config;
2621 FakeConstraints constraints;
2622
Niels Möller71bdda02016-03-31 12:59:59 +02002623 config.set_prerenderer_smoothing(false);
nisse51542be2016-02-12 02:27:06 -08002624 const cricket::MediaConfig& media_config =
2625 TestCreatePeerConnection(config, &constraints);
2626
nisse0db023a2016-03-01 04:29:59 -08002627 EXPECT_TRUE(media_config.video.disable_prerenderer_smoothing);
2628}
2629
2630// This test verifies the suspend below min bitrate constraint is
2631// recognized and passed to the CreateMediaController call.
2632TEST_F(PeerConnectionMediaConfigTest,
2633 TestSuspendBelowMinBitrateConstraintTrue) {
2634 PeerConnectionInterface::RTCConfiguration config;
2635 FakeConstraints constraints;
2636
2637 constraints.AddOptional(
2638 webrtc::MediaConstraintsInterface::kEnableVideoSuspendBelowMinBitrate,
2639 true);
2640 const cricket::MediaConfig media_config =
2641 TestCreatePeerConnection(config, &constraints);
2642
2643 EXPECT_TRUE(media_config.video.suspend_below_min_bitrate);
nisse51542be2016-02-12 02:27:06 -08002644}
2645
deadbeefab9b2d12015-10-14 11:33:11 -07002646// The following tests verify that session options are created correctly.
deadbeefc80741f2015-10-22 13:14:45 -07002647// TODO(deadbeef): Convert these tests to be more end-to-end. Instead of
2648// "verify options are converted correctly", should be "pass options into
2649// CreateOffer and verify the correct offer is produced."
deadbeefab9b2d12015-10-14 11:33:11 -07002650
2651TEST(CreateSessionOptionsTest, GetOptionsForOfferWithInvalidAudioOption) {
2652 RTCOfferAnswerOptions rtc_options;
2653 rtc_options.offer_to_receive_audio = RTCOfferAnswerOptions::kUndefined - 1;
2654
2655 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08002656 EXPECT_FALSE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002657
2658 rtc_options.offer_to_receive_audio =
2659 RTCOfferAnswerOptions::kMaxOfferToReceiveMedia + 1;
htaaac2dea2016-03-10 13:35:55 -08002660 EXPECT_FALSE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002661}
2662
2663TEST(CreateSessionOptionsTest, GetOptionsForOfferWithInvalidVideoOption) {
2664 RTCOfferAnswerOptions rtc_options;
2665 rtc_options.offer_to_receive_video = RTCOfferAnswerOptions::kUndefined - 1;
2666
2667 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08002668 EXPECT_FALSE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002669
2670 rtc_options.offer_to_receive_video =
2671 RTCOfferAnswerOptions::kMaxOfferToReceiveMedia + 1;
htaaac2dea2016-03-10 13:35:55 -08002672 EXPECT_FALSE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002673}
2674
2675// Test that a MediaSessionOptions is created for an offer if
deadbeefc80741f2015-10-22 13:14:45 -07002676// OfferToReceiveAudio and OfferToReceiveVideo options are set.
deadbeefab9b2d12015-10-14 11:33:11 -07002677TEST(CreateSessionOptionsTest, GetMediaSessionOptionsForOfferWithAudioVideo) {
2678 RTCOfferAnswerOptions rtc_options;
2679 rtc_options.offer_to_receive_audio = 1;
2680 rtc_options.offer_to_receive_video = 1;
2681
2682 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08002683 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002684 EXPECT_TRUE(options.has_audio());
2685 EXPECT_TRUE(options.has_video());
2686 EXPECT_TRUE(options.bundle_enabled);
2687}
2688
2689// Test that a correct MediaSessionOptions is created for an offer if
deadbeefc80741f2015-10-22 13:14:45 -07002690// OfferToReceiveAudio is set.
deadbeefab9b2d12015-10-14 11:33:11 -07002691TEST(CreateSessionOptionsTest, GetMediaSessionOptionsForOfferWithAudio) {
2692 RTCOfferAnswerOptions rtc_options;
2693 rtc_options.offer_to_receive_audio = 1;
2694
2695 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08002696 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002697 EXPECT_TRUE(options.has_audio());
2698 EXPECT_FALSE(options.has_video());
2699 EXPECT_TRUE(options.bundle_enabled);
2700}
2701
2702// Test that a correct MediaSessionOptions is created for an offer if
deadbeefc80741f2015-10-22 13:14:45 -07002703// the default OfferOptions are used.
deadbeefab9b2d12015-10-14 11:33:11 -07002704TEST(CreateSessionOptionsTest, GetDefaultMediaSessionOptionsForOffer) {
2705 RTCOfferAnswerOptions rtc_options;
2706
2707 cricket::MediaSessionOptions options;
deadbeef0ed85b22016-02-23 17:24:52 -08002708 options.transport_options["audio"] = cricket::TransportOptions();
2709 options.transport_options["video"] = cricket::TransportOptions();
htaaac2dea2016-03-10 13:35:55 -08002710 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefc80741f2015-10-22 13:14:45 -07002711 EXPECT_TRUE(options.has_audio());
deadbeefab9b2d12015-10-14 11:33:11 -07002712 EXPECT_FALSE(options.has_video());
deadbeefc80741f2015-10-22 13:14:45 -07002713 EXPECT_TRUE(options.bundle_enabled);
deadbeefab9b2d12015-10-14 11:33:11 -07002714 EXPECT_TRUE(options.vad_enabled);
deadbeef0ed85b22016-02-23 17:24:52 -08002715 EXPECT_FALSE(options.transport_options["audio"].ice_restart);
2716 EXPECT_FALSE(options.transport_options["video"].ice_restart);
deadbeefab9b2d12015-10-14 11:33:11 -07002717}
2718
2719// Test that a correct MediaSessionOptions is created for an offer if
deadbeefc80741f2015-10-22 13:14:45 -07002720// OfferToReceiveVideo is set.
deadbeefab9b2d12015-10-14 11:33:11 -07002721TEST(CreateSessionOptionsTest, GetMediaSessionOptionsForOfferWithVideo) {
2722 RTCOfferAnswerOptions rtc_options;
2723 rtc_options.offer_to_receive_audio = 0;
2724 rtc_options.offer_to_receive_video = 1;
2725
2726 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08002727 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002728 EXPECT_FALSE(options.has_audio());
2729 EXPECT_TRUE(options.has_video());
2730 EXPECT_TRUE(options.bundle_enabled);
2731}
2732
2733// Test that a correct MediaSessionOptions is created for an offer if
2734// UseRtpMux is set to false.
2735TEST(CreateSessionOptionsTest,
2736 GetMediaSessionOptionsForOfferWithBundleDisabled) {
2737 RTCOfferAnswerOptions rtc_options;
2738 rtc_options.offer_to_receive_audio = 1;
2739 rtc_options.offer_to_receive_video = 1;
2740 rtc_options.use_rtp_mux = false;
2741
2742 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08002743 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002744 EXPECT_TRUE(options.has_audio());
2745 EXPECT_TRUE(options.has_video());
2746 EXPECT_FALSE(options.bundle_enabled);
2747}
2748
2749// Test that a correct MediaSessionOptions is created to restart ice if
2750// IceRestart is set. It also tests that subsequent MediaSessionOptions don't
Taylor Brandstetterf475d362016-01-08 15:35:57 -08002751// have |audio_transport_options.ice_restart| etc. set.
deadbeefab9b2d12015-10-14 11:33:11 -07002752TEST(CreateSessionOptionsTest, GetMediaSessionOptionsForOfferWithIceRestart) {
2753 RTCOfferAnswerOptions rtc_options;
2754 rtc_options.ice_restart = true;
2755
2756 cricket::MediaSessionOptions options;
deadbeef0ed85b22016-02-23 17:24:52 -08002757 options.transport_options["audio"] = cricket::TransportOptions();
2758 options.transport_options["video"] = cricket::TransportOptions();
htaaac2dea2016-03-10 13:35:55 -08002759 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeef0ed85b22016-02-23 17:24:52 -08002760 EXPECT_TRUE(options.transport_options["audio"].ice_restart);
2761 EXPECT_TRUE(options.transport_options["video"].ice_restart);
deadbeefab9b2d12015-10-14 11:33:11 -07002762
2763 rtc_options = RTCOfferAnswerOptions();
htaaac2dea2016-03-10 13:35:55 -08002764 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeef0ed85b22016-02-23 17:24:52 -08002765 EXPECT_FALSE(options.transport_options["audio"].ice_restart);
2766 EXPECT_FALSE(options.transport_options["video"].ice_restart);
deadbeefab9b2d12015-10-14 11:33:11 -07002767}
2768
2769// Test that the MediaConstraints in an answer don't affect if audio and video
2770// is offered in an offer but that if kOfferToReceiveAudio or
2771// kOfferToReceiveVideo constraints are true in an offer, the media type will be
2772// included in subsequent answers.
2773TEST(CreateSessionOptionsTest, MediaConstraintsInAnswer) {
2774 FakeConstraints answer_c;
2775 answer_c.SetMandatoryReceiveAudio(true);
2776 answer_c.SetMandatoryReceiveVideo(true);
2777
2778 cricket::MediaSessionOptions answer_options;
2779 EXPECT_TRUE(ParseConstraintsForAnswer(&answer_c, &answer_options));
2780 EXPECT_TRUE(answer_options.has_audio());
2781 EXPECT_TRUE(answer_options.has_video());
2782
deadbeefc80741f2015-10-22 13:14:45 -07002783 RTCOfferAnswerOptions rtc_offer_options;
deadbeefab9b2d12015-10-14 11:33:11 -07002784
2785 cricket::MediaSessionOptions offer_options;
htaaac2dea2016-03-10 13:35:55 -08002786 EXPECT_TRUE(
2787 ExtractMediaSessionOptions(rtc_offer_options, false, &offer_options));
deadbeefc80741f2015-10-22 13:14:45 -07002788 EXPECT_TRUE(offer_options.has_audio());
htaaac2dea2016-03-10 13:35:55 -08002789 EXPECT_TRUE(offer_options.has_video());
deadbeefab9b2d12015-10-14 11:33:11 -07002790
deadbeefc80741f2015-10-22 13:14:45 -07002791 RTCOfferAnswerOptions updated_rtc_offer_options;
2792 updated_rtc_offer_options.offer_to_receive_audio = 1;
2793 updated_rtc_offer_options.offer_to_receive_video = 1;
deadbeefab9b2d12015-10-14 11:33:11 -07002794
2795 cricket::MediaSessionOptions updated_offer_options;
htaaac2dea2016-03-10 13:35:55 -08002796 EXPECT_TRUE(ExtractMediaSessionOptions(updated_rtc_offer_options, false,
htaa2a49d92016-03-04 02:51:39 -08002797 &updated_offer_options));
deadbeefab9b2d12015-10-14 11:33:11 -07002798 EXPECT_TRUE(updated_offer_options.has_audio());
2799 EXPECT_TRUE(updated_offer_options.has_video());
2800
2801 // Since an offer has been created with both audio and video, subsequent
2802 // offers and answers should contain both audio and video.
2803 // Answers will only contain the media types that exist in the offer
2804 // regardless of the value of |updated_answer_options.has_audio| and
2805 // |updated_answer_options.has_video|.
2806 FakeConstraints updated_answer_c;
2807 answer_c.SetMandatoryReceiveAudio(false);
2808 answer_c.SetMandatoryReceiveVideo(false);
2809
2810 cricket::MediaSessionOptions updated_answer_options;
2811 EXPECT_TRUE(
2812 ParseConstraintsForAnswer(&updated_answer_c, &updated_answer_options));
2813 EXPECT_TRUE(updated_answer_options.has_audio());
2814 EXPECT_TRUE(updated_answer_options.has_video());
deadbeefab9b2d12015-10-14 11:33:11 -07002815}