blob: d33a40289656d86d570838782a78a93350ef3069 [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_; }
perkjdfb769d2016-02-09 03:09:43 -0800476 void OnAddStream(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 }
perkjdfb769d2016-02-09 03:09:43 -0800480 void OnRemoveStream(MediaStreamInterface* stream) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000481 last_removed_stream_ = stream;
deadbeefab9b2d12015-10-14 11:33:11 -0700482 remote_streams_->RemoveStream(stream);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000483 }
perkjdfb769d2016-02-09 03:09:43 -0800484 void OnRenegotiationNeeded() override { renegotiation_needed_ = true; }
485 void OnDataChannel(DataChannelInterface* data_channel) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000486 last_datachannel_ = data_channel;
487 }
488
perkjdfb769d2016-02-09 03:09:43 -0800489 void OnIceConnectionChange(
490 PeerConnectionInterface::IceConnectionState new_state) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000491 EXPECT_EQ(pc_->ice_connection_state(), new_state);
492 }
perkjdfb769d2016-02-09 03:09:43 -0800493 void OnIceGatheringChange(
494 PeerConnectionInterface::IceGatheringState new_state) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000495 EXPECT_EQ(pc_->ice_gathering_state(), new_state);
perkjdfb769d2016-02-09 03:09:43 -0800496 ice_complete_ = new_state == PeerConnectionInterface::kIceGatheringComplete;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000497 }
perkjdfb769d2016-02-09 03:09:43 -0800498 void OnIceCandidate(const webrtc::IceCandidateInterface* candidate) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000499 EXPECT_NE(PeerConnectionInterface::kIceGatheringNew,
500 pc_->ice_gathering_state());
501
502 std::string sdp;
503 EXPECT_TRUE(candidate->ToString(&sdp));
504 EXPECT_LT(0u, sdp.size());
505 last_candidate_.reset(webrtc::CreateIceCandidate(candidate->sdp_mid(),
506 candidate->sdp_mline_index(), sdp, NULL));
507 EXPECT_TRUE(last_candidate_.get() != NULL);
508 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000509
510 // Returns the label of the last added stream.
511 // Empty string if no stream have been added.
512 std::string GetLastAddedStreamLabel() {
513 if (last_added_stream_.get())
514 return last_added_stream_->label();
515 return "";
516 }
517 std::string GetLastRemovedStreamLabel() {
518 if (last_removed_stream_.get())
519 return last_removed_stream_->label();
520 return "";
521 }
522
523 scoped_refptr<PeerConnectionInterface> pc_;
524 PeerConnectionInterface::SignalingState state_;
kwibergd1fe2812016-04-27 06:47:29 -0700525 std::unique_ptr<IceCandidateInterface> last_candidate_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000526 scoped_refptr<DataChannelInterface> last_datachannel_;
deadbeefab9b2d12015-10-14 11:33:11 -0700527 rtc::scoped_refptr<StreamCollection> remote_streams_;
528 bool renegotiation_needed_ = false;
529 bool ice_complete_ = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000530
531 private:
532 scoped_refptr<MediaStreamInterface> last_added_stream_;
533 scoped_refptr<MediaStreamInterface> last_removed_stream_;
534};
535
536} // namespace
deadbeefab9b2d12015-10-14 11:33:11 -0700537
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000538class PeerConnectionInterfaceTest : public testing::Test {
539 protected:
phoglund37ebcf02016-01-08 05:04:57 -0800540 PeerConnectionInterfaceTest() {
541#ifdef WEBRTC_ANDROID
542 webrtc::InitializeAndroidObjects();
543#endif
544 }
545
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000546 virtual void SetUp() {
547 pc_factory_ = webrtc::CreatePeerConnectionFactory(
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000548 rtc::Thread::Current(), rtc::Thread::Current(), NULL, NULL,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000549 NULL);
550 ASSERT_TRUE(pc_factory_.get() != NULL);
551 }
552
553 void CreatePeerConnection() {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700554 CreatePeerConnection(PeerConnectionInterface::RTCConfiguration(), nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000555 }
556
557 void CreatePeerConnection(webrtc::MediaConstraintsInterface* constraints) {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700558 CreatePeerConnection(PeerConnectionInterface::RTCConfiguration(),
559 constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000560 }
561
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700562 void CreatePeerConnectionWithIceTransportsType(
563 PeerConnectionInterface::IceTransportsType type) {
564 PeerConnectionInterface::RTCConfiguration config;
565 config.type = type;
566 return CreatePeerConnection(config, nullptr);
567 }
568
569 void CreatePeerConnectionWithIceServer(const std::string& uri,
570 const std::string& password) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800571 PeerConnectionInterface::RTCConfiguration config;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000572 PeerConnectionInterface::IceServer server;
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700573 server.uri = uri;
574 server.password = password;
575 config.servers.push_back(server);
576 CreatePeerConnection(config, nullptr);
577 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000578
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700579 void CreatePeerConnection(PeerConnectionInterface::RTCConfiguration config,
580 webrtc::MediaConstraintsInterface* constraints) {
kwibergd1fe2812016-04-27 06:47:29 -0700581 std::unique_ptr<cricket::FakePortAllocator> port_allocator(
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800582 new cricket::FakePortAllocator(rtc::Thread::Current(), nullptr));
583 port_allocator_ = port_allocator.get();
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000584
buildbot@webrtc.org61c1b8e2014-04-09 06:06:38 +0000585 // DTLS does not work in a loopback call, so is disabled for most of the
586 // tests in this file. We only create a FakeIdentityService if the test
587 // explicitly sets the constraint.
jiayl@webrtc.org61e00b02015-03-04 22:17:38 +0000588 FakeConstraints default_constraints;
589 if (!constraints) {
590 constraints = &default_constraints;
591
592 default_constraints.AddMandatory(
593 webrtc::MediaConstraintsInterface::kEnableDtlsSrtp, false);
594 }
595
kwibergd1fe2812016-04-27 06:47:29 -0700596 std::unique_ptr<webrtc::DtlsIdentityStoreInterface> dtls_identity_store;
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000597 bool dtls;
598 if (FindConstraint(constraints,
599 webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
600 &dtls,
Henrik Boström5e56c592015-08-11 10:33:13 +0200601 nullptr) && dtls) {
602 dtls_identity_store.reset(new FakeDtlsIdentityStore());
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000603 }
kwiberg0eb15ed2015-12-17 03:04:15 -0800604 pc_ = pc_factory_->CreatePeerConnection(
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800605 config, constraints, std::move(port_allocator),
kwiberg0eb15ed2015-12-17 03:04:15 -0800606 std::move(dtls_identity_store), &observer_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000607 ASSERT_TRUE(pc_.get() != NULL);
608 observer_.SetPeerConnectionInterface(pc_.get());
609 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
610 }
611
deadbeef0a6c4ca2015-10-06 11:38:28 -0700612 void CreatePeerConnectionExpectFail(const std::string& uri) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800613 PeerConnectionInterface::RTCConfiguration config;
deadbeef0a6c4ca2015-10-06 11:38:28 -0700614 PeerConnectionInterface::IceServer server;
deadbeef0a6c4ca2015-10-06 11:38:28 -0700615 server.uri = uri;
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800616 config.servers.push_back(server);
deadbeef0a6c4ca2015-10-06 11:38:28 -0700617
deadbeef0a6c4ca2015-10-06 11:38:28 -0700618 scoped_refptr<PeerConnectionInterface> pc;
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800619 pc = pc_factory_->CreatePeerConnection(config, nullptr, nullptr, nullptr,
620 &observer_);
621 EXPECT_EQ(nullptr, pc);
deadbeef0a6c4ca2015-10-06 11:38:28 -0700622 }
623
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000624 void CreatePeerConnectionWithDifferentConfigurations() {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700625 CreatePeerConnectionWithIceServer(kStunAddressOnly, "");
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800626 EXPECT_EQ(1u, port_allocator_->stun_servers().size());
627 EXPECT_EQ(0u, port_allocator_->turn_servers().size());
628 EXPECT_EQ("address", port_allocator_->stun_servers().begin()->hostname());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000629 EXPECT_EQ(kDefaultStunPort,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800630 port_allocator_->stun_servers().begin()->port());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000631
deadbeef0a6c4ca2015-10-06 11:38:28 -0700632 CreatePeerConnectionExpectFail(kStunInvalidPort);
633 CreatePeerConnectionExpectFail(kStunAddressPortAndMore1);
634 CreatePeerConnectionExpectFail(kStunAddressPortAndMore2);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000635
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700636 CreatePeerConnectionWithIceServer(kTurnIceServerUri, kTurnPassword);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800637 EXPECT_EQ(0u, port_allocator_->stun_servers().size());
638 EXPECT_EQ(1u, port_allocator_->turn_servers().size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000639 EXPECT_EQ(kTurnUsername,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800640 port_allocator_->turn_servers()[0].credentials.username);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000641 EXPECT_EQ(kTurnPassword,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800642 port_allocator_->turn_servers()[0].credentials.password);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000643 EXPECT_EQ(kTurnHostname,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800644 port_allocator_->turn_servers()[0].ports[0].address.hostname());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000645 }
646
647 void ReleasePeerConnection() {
648 pc_ = NULL;
649 observer_.SetPeerConnectionInterface(NULL);
650 }
651
deadbeefab9b2d12015-10-14 11:33:11 -0700652 void AddVideoStream(const std::string& label) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000653 // Create a local stream.
654 scoped_refptr<MediaStreamInterface> stream(
655 pc_factory_->CreateLocalMediaStream(label));
perkja3ede6c2016-03-08 01:27:48 +0100656 scoped_refptr<VideoTrackSourceInterface> video_source(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000657 pc_factory_->CreateVideoSource(new cricket::FakeVideoCapturer(), NULL));
658 scoped_refptr<VideoTrackInterface> video_track(
659 pc_factory_->CreateVideoTrack(label + "v0", video_source));
660 stream->AddTrack(video_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +0000661 EXPECT_TRUE(pc_->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000662 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
663 observer_.renegotiation_needed_ = false;
664 }
665
666 void AddVoiceStream(const std::string& label) {
667 // Create a local stream.
668 scoped_refptr<MediaStreamInterface> stream(
669 pc_factory_->CreateLocalMediaStream(label));
670 scoped_refptr<AudioTrackInterface> audio_track(
671 pc_factory_->CreateAudioTrack(label + "a0", NULL));
672 stream->AddTrack(audio_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +0000673 EXPECT_TRUE(pc_->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000674 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
675 observer_.renegotiation_needed_ = false;
676 }
677
678 void AddAudioVideoStream(const std::string& stream_label,
679 const std::string& audio_track_label,
680 const std::string& video_track_label) {
681 // Create a local stream.
682 scoped_refptr<MediaStreamInterface> stream(
683 pc_factory_->CreateLocalMediaStream(stream_label));
684 scoped_refptr<AudioTrackInterface> audio_track(
685 pc_factory_->CreateAudioTrack(
686 audio_track_label, static_cast<AudioSourceInterface*>(NULL)));
687 stream->AddTrack(audio_track.get());
688 scoped_refptr<VideoTrackInterface> video_track(
nisseaf510af2016-03-21 08:20:42 -0700689 pc_factory_->CreateVideoTrack(
690 video_track_label,
691 pc_factory_->CreateVideoSource(new cricket::FakeVideoCapturer())));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000692 stream->AddTrack(video_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +0000693 EXPECT_TRUE(pc_->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000694 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
695 observer_.renegotiation_needed_ = false;
696 }
697
kwibergd1fe2812016-04-27 06:47:29 -0700698 bool DoCreateOfferAnswer(std::unique_ptr<SessionDescriptionInterface>* desc,
deadbeefc80741f2015-10-22 13:14:45 -0700699 bool offer,
700 MediaConstraintsInterface* constraints) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000701 rtc::scoped_refptr<MockCreateSessionDescriptionObserver>
702 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000703 MockCreateSessionDescriptionObserver>());
704 if (offer) {
deadbeefc80741f2015-10-22 13:14:45 -0700705 pc_->CreateOffer(observer, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000706 } else {
deadbeefc80741f2015-10-22 13:14:45 -0700707 pc_->CreateAnswer(observer, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000708 }
709 EXPECT_EQ_WAIT(true, observer->called(), kTimeout);
kwiberg2bbff992016-03-16 11:03:04 -0700710 desc->reset(observer->release_desc());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000711 return observer->result();
712 }
713
kwibergd1fe2812016-04-27 06:47:29 -0700714 bool DoCreateOffer(std::unique_ptr<SessionDescriptionInterface>* desc,
deadbeefc80741f2015-10-22 13:14:45 -0700715 MediaConstraintsInterface* constraints) {
716 return DoCreateOfferAnswer(desc, true, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000717 }
718
kwibergd1fe2812016-04-27 06:47:29 -0700719 bool DoCreateAnswer(std::unique_ptr<SessionDescriptionInterface>* desc,
deadbeefc80741f2015-10-22 13:14:45 -0700720 MediaConstraintsInterface* constraints) {
721 return DoCreateOfferAnswer(desc, false, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000722 }
723
724 bool DoSetSessionDescription(SessionDescriptionInterface* desc, bool local) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000725 rtc::scoped_refptr<MockSetSessionDescriptionObserver>
726 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000727 MockSetSessionDescriptionObserver>());
728 if (local) {
729 pc_->SetLocalDescription(observer, desc);
730 } else {
731 pc_->SetRemoteDescription(observer, desc);
732 }
733 EXPECT_EQ_WAIT(true, observer->called(), kTimeout);
734 return observer->result();
735 }
736
737 bool DoSetLocalDescription(SessionDescriptionInterface* desc) {
738 return DoSetSessionDescription(desc, true);
739 }
740
741 bool DoSetRemoteDescription(SessionDescriptionInterface* desc) {
742 return DoSetSessionDescription(desc, false);
743 }
744
745 // Calls PeerConnection::GetStats and check the return value.
746 // It does not verify the values in the StatReports since a RTCP packet might
747 // be required.
748 bool DoGetStats(MediaStreamTrackInterface* track) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000749 rtc::scoped_refptr<MockStatsObserver> observer(
750 new rtc::RefCountedObject<MockStatsObserver>());
jiayl@webrtc.orgdb41b4d2014-03-03 21:30:06 +0000751 if (!pc_->GetStats(
752 observer, track, PeerConnectionInterface::kStatsOutputLevelStandard))
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000753 return false;
754 EXPECT_TRUE_WAIT(observer->called(), kTimeout);
755 return observer->called();
756 }
757
758 void InitiateCall() {
759 CreatePeerConnection();
760 // Create a local stream with audio&video tracks.
761 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
762 CreateOfferReceiveAnswer();
763 }
764
765 // Verify that RTP Header extensions has been negotiated for audio and video.
766 void VerifyRemoteRtpHeaderExtensions() {
767 const cricket::MediaContentDescription* desc =
768 cricket::GetFirstAudioContentDescription(
769 pc_->remote_description()->description());
770 ASSERT_TRUE(desc != NULL);
771 EXPECT_GT(desc->rtp_header_extensions().size(), 0u);
772
773 desc = cricket::GetFirstVideoContentDescription(
774 pc_->remote_description()->description());
775 ASSERT_TRUE(desc != NULL);
776 EXPECT_GT(desc->rtp_header_extensions().size(), 0u);
777 }
778
779 void CreateOfferAsRemoteDescription() {
kwibergd1fe2812016-04-27 06:47:29 -0700780 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -0700781 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000782 std::string sdp;
783 EXPECT_TRUE(offer->ToString(&sdp));
784 SessionDescriptionInterface* remote_offer =
785 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
786 sdp, NULL);
787 EXPECT_TRUE(DoSetRemoteDescription(remote_offer));
788 EXPECT_EQ(PeerConnectionInterface::kHaveRemoteOffer, observer_.state_);
789 }
790
deadbeefab9b2d12015-10-14 11:33:11 -0700791 void CreateAndSetRemoteOffer(const std::string& sdp) {
792 SessionDescriptionInterface* remote_offer =
793 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
794 sdp, nullptr);
795 EXPECT_TRUE(DoSetRemoteDescription(remote_offer));
796 EXPECT_EQ(PeerConnectionInterface::kHaveRemoteOffer, observer_.state_);
797 }
798
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000799 void CreateAnswerAsLocalDescription() {
kwibergd1fe2812016-04-27 06:47:29 -0700800 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -0700801 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000802
803 // TODO(perkj): Currently SetLocalDescription fails if any parameters in an
804 // audio codec change, even if the parameter has nothing to do with
805 // receiving. Not all parameters are serialized to SDP.
806 // Since CreatePrAnswerAsLocalDescription serialize/deserialize
807 // the SessionDescription, it is necessary to do that here to in order to
808 // get ReceiveOfferCreatePrAnswerAndAnswer and RenegotiateAudioOnly to pass.
809 // https://code.google.com/p/webrtc/issues/detail?id=1356
810 std::string sdp;
811 EXPECT_TRUE(answer->ToString(&sdp));
812 SessionDescriptionInterface* new_answer =
813 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
814 sdp, NULL);
815 EXPECT_TRUE(DoSetLocalDescription(new_answer));
816 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
817 }
818
819 void CreatePrAnswerAsLocalDescription() {
kwibergd1fe2812016-04-27 06:47:29 -0700820 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -0700821 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000822
823 std::string sdp;
824 EXPECT_TRUE(answer->ToString(&sdp));
825 SessionDescriptionInterface* pr_answer =
826 webrtc::CreateSessionDescription(SessionDescriptionInterface::kPrAnswer,
827 sdp, NULL);
828 EXPECT_TRUE(DoSetLocalDescription(pr_answer));
829 EXPECT_EQ(PeerConnectionInterface::kHaveLocalPrAnswer, observer_.state_);
830 }
831
832 void CreateOfferReceiveAnswer() {
833 CreateOfferAsLocalDescription();
834 std::string sdp;
835 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
836 CreateAnswerAsRemoteDescription(sdp);
837 }
838
839 void CreateOfferAsLocalDescription() {
kwibergd1fe2812016-04-27 06:47:29 -0700840 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -0700841 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000842 // TODO(perkj): Currently SetLocalDescription fails if any parameters in an
843 // audio codec change, even if the parameter has nothing to do with
844 // receiving. Not all parameters are serialized to SDP.
845 // Since CreatePrAnswerAsLocalDescription serialize/deserialize
846 // the SessionDescription, it is necessary to do that here to in order to
847 // get ReceiveOfferCreatePrAnswerAndAnswer and RenegotiateAudioOnly to pass.
848 // https://code.google.com/p/webrtc/issues/detail?id=1356
849 std::string sdp;
850 EXPECT_TRUE(offer->ToString(&sdp));
851 SessionDescriptionInterface* new_offer =
852 webrtc::CreateSessionDescription(
853 SessionDescriptionInterface::kOffer,
854 sdp, NULL);
855
856 EXPECT_TRUE(DoSetLocalDescription(new_offer));
857 EXPECT_EQ(PeerConnectionInterface::kHaveLocalOffer, observer_.state_);
mallinath@webrtc.org68cbd012014-01-22 00:16:46 +0000858 // Wait for the ice_complete message, so that SDP will have candidates.
859 EXPECT_TRUE_WAIT(observer_.ice_complete_, kTimeout);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000860 }
861
deadbeefab9b2d12015-10-14 11:33:11 -0700862 void CreateAnswerAsRemoteDescription(const std::string& sdp) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000863 webrtc::JsepSessionDescription* answer = new webrtc::JsepSessionDescription(
864 SessionDescriptionInterface::kAnswer);
deadbeefab9b2d12015-10-14 11:33:11 -0700865 EXPECT_TRUE(answer->Initialize(sdp, NULL));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000866 EXPECT_TRUE(DoSetRemoteDescription(answer));
867 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
868 }
869
deadbeefab9b2d12015-10-14 11:33:11 -0700870 void CreatePrAnswerAndAnswerAsRemoteDescription(const std::string& sdp) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000871 webrtc::JsepSessionDescription* pr_answer =
872 new webrtc::JsepSessionDescription(
873 SessionDescriptionInterface::kPrAnswer);
deadbeefab9b2d12015-10-14 11:33:11 -0700874 EXPECT_TRUE(pr_answer->Initialize(sdp, NULL));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000875 EXPECT_TRUE(DoSetRemoteDescription(pr_answer));
876 EXPECT_EQ(PeerConnectionInterface::kHaveRemotePrAnswer, observer_.state_);
877 webrtc::JsepSessionDescription* answer =
878 new webrtc::JsepSessionDescription(
879 SessionDescriptionInterface::kAnswer);
deadbeefab9b2d12015-10-14 11:33:11 -0700880 EXPECT_TRUE(answer->Initialize(sdp, NULL));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000881 EXPECT_TRUE(DoSetRemoteDescription(answer));
882 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
883 }
884
885 // Help function used for waiting until a the last signaled remote stream has
886 // the same label as |stream_label|. In a few of the tests in this file we
887 // answer with the same session description as we offer and thus we can
888 // check if OnAddStream have been called with the same stream as we offer to
889 // send.
890 void WaitAndVerifyOnAddStream(const std::string& stream_label) {
891 EXPECT_EQ_WAIT(stream_label, observer_.GetLastAddedStreamLabel(), kTimeout);
892 }
893
894 // Creates an offer and applies it as a local session description.
895 // Creates an answer with the same SDP an the offer but removes all lines
896 // that start with a:ssrc"
897 void CreateOfferReceiveAnswerWithoutSsrc() {
898 CreateOfferAsLocalDescription();
899 std::string sdp;
900 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
901 SetSsrcToZero(&sdp);
902 CreateAnswerAsRemoteDescription(sdp);
903 }
904
deadbeefab9b2d12015-10-14 11:33:11 -0700905 // This function creates a MediaStream with label kStreams[0] and
906 // |number_of_audio_tracks| and |number_of_video_tracks| tracks and the
907 // corresponding SessionDescriptionInterface. The SessionDescriptionInterface
kwiberg2bbff992016-03-16 11:03:04 -0700908 // is returned and the MediaStream is stored in
deadbeefab9b2d12015-10-14 11:33:11 -0700909 // |reference_collection_|
kwibergd1fe2812016-04-27 06:47:29 -0700910 std::unique_ptr<SessionDescriptionInterface>
kwiberg2bbff992016-03-16 11:03:04 -0700911 CreateSessionDescriptionAndReference(size_t number_of_audio_tracks,
912 size_t number_of_video_tracks) {
913 EXPECT_LE(number_of_audio_tracks, 2u);
914 EXPECT_LE(number_of_video_tracks, 2u);
deadbeefab9b2d12015-10-14 11:33:11 -0700915
916 reference_collection_ = StreamCollection::Create();
917 std::string sdp_ms1 = std::string(kSdpStringInit);
918
919 std::string mediastream_label = kStreams[0];
920
921 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream(
922 webrtc::MediaStream::Create(mediastream_label));
923 reference_collection_->AddStream(stream);
924
925 if (number_of_audio_tracks > 0) {
926 sdp_ms1 += std::string(kSdpStringAudio);
927 sdp_ms1 += std::string(kSdpStringMs1Audio0);
928 AddAudioTrack(kAudioTracks[0], stream);
929 }
930 if (number_of_audio_tracks > 1) {
931 sdp_ms1 += kSdpStringMs1Audio1;
932 AddAudioTrack(kAudioTracks[1], stream);
933 }
934
935 if (number_of_video_tracks > 0) {
936 sdp_ms1 += std::string(kSdpStringVideo);
937 sdp_ms1 += std::string(kSdpStringMs1Video0);
938 AddVideoTrack(kVideoTracks[0], stream);
939 }
940 if (number_of_video_tracks > 1) {
941 sdp_ms1 += kSdpStringMs1Video1;
942 AddVideoTrack(kVideoTracks[1], stream);
943 }
944
kwibergd1fe2812016-04-27 06:47:29 -0700945 return std::unique_ptr<SessionDescriptionInterface>(
kwiberg2bbff992016-03-16 11:03:04 -0700946 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
947 sdp_ms1, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -0700948 }
949
950 void AddAudioTrack(const std::string& track_id,
951 MediaStreamInterface* stream) {
952 rtc::scoped_refptr<webrtc::AudioTrackInterface> audio_track(
953 webrtc::AudioTrack::Create(track_id, nullptr));
954 ASSERT_TRUE(stream->AddTrack(audio_track));
955 }
956
957 void AddVideoTrack(const std::string& track_id,
958 MediaStreamInterface* stream) {
959 rtc::scoped_refptr<webrtc::VideoTrackInterface> video_track(
nisseaf510af2016-03-21 08:20:42 -0700960 webrtc::VideoTrack::Create(track_id,
961 webrtc::FakeVideoTrackSource::Create()));
deadbeefab9b2d12015-10-14 11:33:11 -0700962 ASSERT_TRUE(stream->AddTrack(video_track));
963 }
964
kwibergfd8be342016-05-14 19:44:11 -0700965 std::unique_ptr<SessionDescriptionInterface> CreateOfferWithOneAudioStream() {
zhihuang8f65cdf2016-05-06 18:40:30 -0700966 CreatePeerConnection();
967 AddVoiceStream(kStreamLabel1);
kwibergfd8be342016-05-14 19:44:11 -0700968 std::unique_ptr<SessionDescriptionInterface> offer;
zhihuang8f65cdf2016-05-06 18:40:30 -0700969 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
970 return offer;
971 }
972
kwibergfd8be342016-05-14 19:44:11 -0700973 std::unique_ptr<SessionDescriptionInterface>
zhihuang8f65cdf2016-05-06 18:40:30 -0700974 CreateAnswerWithOneAudioStream() {
kwibergfd8be342016-05-14 19:44:11 -0700975 std::unique_ptr<SessionDescriptionInterface> offer =
zhihuang8f65cdf2016-05-06 18:40:30 -0700976 CreateOfferWithOneAudioStream();
977 EXPECT_TRUE(DoSetRemoteDescription(offer.release()));
kwibergfd8be342016-05-14 19:44:11 -0700978 std::unique_ptr<SessionDescriptionInterface> answer;
zhihuang8f65cdf2016-05-06 18:40:30 -0700979 EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
980 return answer;
981 }
982
983 const std::string& GetFirstAudioStreamCname(
984 const SessionDescriptionInterface* desc) {
985 const cricket::ContentInfo* audio_content =
986 cricket::GetFirstAudioContent(desc->description());
987 const cricket::AudioContentDescription* audio_desc =
988 static_cast<const cricket::AudioContentDescription*>(
989 audio_content->description);
990 return audio_desc->streams()[0].cname;
991 }
992
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800993 cricket::FakePortAllocator* port_allocator_ = nullptr;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000994 scoped_refptr<webrtc::PeerConnectionFactoryInterface> pc_factory_;
995 scoped_refptr<PeerConnectionInterface> pc_;
996 MockPeerConnectionObserver observer_;
deadbeefab9b2d12015-10-14 11:33:11 -0700997 rtc::scoped_refptr<StreamCollection> reference_collection_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000998};
999
zhihuang8f65cdf2016-05-06 18:40:30 -07001000// Generate different CNAMEs when PeerConnections are created.
1001// The CNAMEs are expected to be generated randomly. It is possible
1002// that the test fails, though the possibility is very low.
1003TEST_F(PeerConnectionInterfaceTest, CnameGenerationInOffer) {
kwibergfd8be342016-05-14 19:44:11 -07001004 std::unique_ptr<SessionDescriptionInterface> offer1 =
zhihuang8f65cdf2016-05-06 18:40:30 -07001005 CreateOfferWithOneAudioStream();
kwibergfd8be342016-05-14 19:44:11 -07001006 std::unique_ptr<SessionDescriptionInterface> offer2 =
zhihuang8f65cdf2016-05-06 18:40:30 -07001007 CreateOfferWithOneAudioStream();
1008 EXPECT_NE(GetFirstAudioStreamCname(offer1.get()),
1009 GetFirstAudioStreamCname(offer2.get()));
1010}
1011
1012TEST_F(PeerConnectionInterfaceTest, CnameGenerationInAnswer) {
kwibergfd8be342016-05-14 19:44:11 -07001013 std::unique_ptr<SessionDescriptionInterface> answer1 =
zhihuang8f65cdf2016-05-06 18:40:30 -07001014 CreateAnswerWithOneAudioStream();
kwibergfd8be342016-05-14 19:44:11 -07001015 std::unique_ptr<SessionDescriptionInterface> answer2 =
zhihuang8f65cdf2016-05-06 18:40:30 -07001016 CreateAnswerWithOneAudioStream();
1017 EXPECT_NE(GetFirstAudioStreamCname(answer1.get()),
1018 GetFirstAudioStreamCname(answer2.get()));
1019}
1020
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001021TEST_F(PeerConnectionInterfaceTest,
1022 CreatePeerConnectionWithDifferentConfigurations) {
1023 CreatePeerConnectionWithDifferentConfigurations();
1024}
1025
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001026TEST_F(PeerConnectionInterfaceTest,
1027 CreatePeerConnectionWithDifferentIceTransportsTypes) {
1028 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kNone);
1029 EXPECT_EQ(cricket::CF_NONE, port_allocator_->candidate_filter());
1030 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kRelay);
1031 EXPECT_EQ(cricket::CF_RELAY, port_allocator_->candidate_filter());
1032 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kNoHost);
1033 EXPECT_EQ(cricket::CF_ALL & ~cricket::CF_HOST,
1034 port_allocator_->candidate_filter());
1035 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kAll);
1036 EXPECT_EQ(cricket::CF_ALL, port_allocator_->candidate_filter());
1037}
1038
1039// Test that when a PeerConnection is created with a nonzero candidate pool
1040// size, the pooled PortAllocatorSession is created with all the attributes
1041// in the RTCConfiguration.
1042TEST_F(PeerConnectionInterfaceTest, CreatePeerConnectionWithPooledCandidates) {
1043 PeerConnectionInterface::RTCConfiguration config;
1044 PeerConnectionInterface::IceServer server;
1045 server.uri = kStunAddressOnly;
1046 config.servers.push_back(server);
1047 config.type = PeerConnectionInterface::kRelay;
1048 config.disable_ipv6 = true;
1049 config.tcp_candidate_policy =
1050 PeerConnectionInterface::kTcpCandidatePolicyDisabled;
1051 config.ice_candidate_pool_size = 1;
1052 CreatePeerConnection(config, nullptr);
1053
1054 const cricket::FakePortAllocatorSession* session =
1055 static_cast<const cricket::FakePortAllocatorSession*>(
1056 port_allocator_->GetPooledSession());
1057 ASSERT_NE(nullptr, session);
1058 EXPECT_EQ(1UL, session->stun_servers().size());
1059 EXPECT_EQ(0U, session->flags() & cricket::PORTALLOCATOR_ENABLE_IPV6);
1060 EXPECT_LT(0U, session->flags() & cricket::PORTALLOCATOR_DISABLE_TCP);
1061 EXPECT_EQ(cricket::CF_RELAY, session->candidate_filter());
1062}
1063
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001064TEST_F(PeerConnectionInterfaceTest, AddStreams) {
1065 CreatePeerConnection();
deadbeefab9b2d12015-10-14 11:33:11 -07001066 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001067 AddVoiceStream(kStreamLabel2);
1068 ASSERT_EQ(2u, pc_->local_streams()->count());
1069
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001070 // Test we can add multiple local streams to one peerconnection.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001071 scoped_refptr<MediaStreamInterface> stream(
1072 pc_factory_->CreateLocalMediaStream(kStreamLabel3));
1073 scoped_refptr<AudioTrackInterface> audio_track(
1074 pc_factory_->CreateAudioTrack(
1075 kStreamLabel3, static_cast<AudioSourceInterface*>(NULL)));
1076 stream->AddTrack(audio_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +00001077 EXPECT_TRUE(pc_->AddStream(stream));
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001078 EXPECT_EQ(3u, pc_->local_streams()->count());
1079
1080 // Remove the third stream.
1081 pc_->RemoveStream(pc_->local_streams()->at(2));
1082 EXPECT_EQ(2u, pc_->local_streams()->count());
1083
1084 // Remove the second stream.
1085 pc_->RemoveStream(pc_->local_streams()->at(1));
1086 EXPECT_EQ(1u, pc_->local_streams()->count());
1087
1088 // Remove the first stream.
1089 pc_->RemoveStream(pc_->local_streams()->at(0));
1090 EXPECT_EQ(0u, pc_->local_streams()->count());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001091}
1092
deadbeefab9b2d12015-10-14 11:33:11 -07001093// Test that the created offer includes streams we added.
1094TEST_F(PeerConnectionInterfaceTest, AddedStreamsPresentInOffer) {
1095 CreatePeerConnection();
1096 AddAudioVideoStream(kStreamLabel1, "audio_track", "video_track");
kwibergd1fe2812016-04-27 06:47:29 -07001097 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001098 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07001099
1100 const cricket::ContentInfo* audio_content =
1101 cricket::GetFirstAudioContent(offer->description());
1102 const cricket::AudioContentDescription* audio_desc =
1103 static_cast<const cricket::AudioContentDescription*>(
1104 audio_content->description);
1105 EXPECT_TRUE(
1106 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
1107
1108 const cricket::ContentInfo* video_content =
1109 cricket::GetFirstVideoContent(offer->description());
1110 const cricket::VideoContentDescription* video_desc =
1111 static_cast<const cricket::VideoContentDescription*>(
1112 video_content->description);
1113 EXPECT_TRUE(
1114 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1115
1116 // Add another stream and ensure the offer includes both the old and new
1117 // streams.
1118 AddAudioVideoStream(kStreamLabel2, "audio_track2", "video_track2");
kwiberg2bbff992016-03-16 11:03:04 -07001119 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07001120
1121 audio_content = cricket::GetFirstAudioContent(offer->description());
1122 audio_desc = static_cast<const cricket::AudioContentDescription*>(
1123 audio_content->description);
1124 EXPECT_TRUE(
1125 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
1126 EXPECT_TRUE(
1127 ContainsTrack(audio_desc->streams(), kStreamLabel2, "audio_track2"));
1128
1129 video_content = cricket::GetFirstVideoContent(offer->description());
1130 video_desc = static_cast<const cricket::VideoContentDescription*>(
1131 video_content->description);
1132 EXPECT_TRUE(
1133 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1134 EXPECT_TRUE(
1135 ContainsTrack(video_desc->streams(), kStreamLabel2, "video_track2"));
1136}
1137
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001138TEST_F(PeerConnectionInterfaceTest, RemoveStream) {
1139 CreatePeerConnection();
deadbeefab9b2d12015-10-14 11:33:11 -07001140 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001141 ASSERT_EQ(1u, pc_->local_streams()->count());
1142 pc_->RemoveStream(pc_->local_streams()->at(0));
1143 EXPECT_EQ(0u, pc_->local_streams()->count());
1144}
1145
deadbeefe1f9d832016-01-14 15:35:42 -08001146// Test for AddTrack and RemoveTrack methods.
1147// Tests that the created offer includes tracks we added,
1148// and that the RtpSenders are created correctly.
1149// Also tests that RemoveTrack removes the tracks from subsequent offers.
1150TEST_F(PeerConnectionInterfaceTest, AddTrackRemoveTrack) {
1151 CreatePeerConnection();
1152 // Create a dummy stream, so tracks share a stream label.
1153 scoped_refptr<MediaStreamInterface> stream(
1154 pc_factory_->CreateLocalMediaStream(kStreamLabel1));
1155 std::vector<MediaStreamInterface*> stream_list;
1156 stream_list.push_back(stream.get());
1157 scoped_refptr<AudioTrackInterface> audio_track(
1158 pc_factory_->CreateAudioTrack("audio_track", nullptr));
nisseaf510af2016-03-21 08:20:42 -07001159 scoped_refptr<VideoTrackInterface> video_track(pc_factory_->CreateVideoTrack(
1160 "video_track",
1161 pc_factory_->CreateVideoSource(new cricket::FakeVideoCapturer())));
deadbeefe1f9d832016-01-14 15:35:42 -08001162 auto audio_sender = pc_->AddTrack(audio_track, stream_list);
1163 auto video_sender = pc_->AddTrack(video_track, stream_list);
1164 EXPECT_EQ(kStreamLabel1, audio_sender->stream_id());
1165 EXPECT_EQ("audio_track", audio_sender->id());
1166 EXPECT_EQ(audio_track, audio_sender->track());
1167 EXPECT_EQ(kStreamLabel1, video_sender->stream_id());
1168 EXPECT_EQ("video_track", video_sender->id());
1169 EXPECT_EQ(video_track, video_sender->track());
1170
1171 // Now create an offer and check for the senders.
kwibergd1fe2812016-04-27 06:47:29 -07001172 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001173 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefe1f9d832016-01-14 15:35:42 -08001174
1175 const cricket::ContentInfo* audio_content =
1176 cricket::GetFirstAudioContent(offer->description());
1177 const cricket::AudioContentDescription* audio_desc =
1178 static_cast<const cricket::AudioContentDescription*>(
1179 audio_content->description);
1180 EXPECT_TRUE(
1181 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
1182
1183 const cricket::ContentInfo* video_content =
1184 cricket::GetFirstVideoContent(offer->description());
1185 const cricket::VideoContentDescription* video_desc =
1186 static_cast<const cricket::VideoContentDescription*>(
1187 video_content->description);
1188 EXPECT_TRUE(
1189 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1190
1191 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
1192
1193 // Now try removing the tracks.
1194 EXPECT_TRUE(pc_->RemoveTrack(audio_sender));
1195 EXPECT_TRUE(pc_->RemoveTrack(video_sender));
1196
1197 // Create a new offer and ensure it doesn't contain the removed senders.
kwiberg2bbff992016-03-16 11:03:04 -07001198 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefe1f9d832016-01-14 15:35:42 -08001199
1200 audio_content = cricket::GetFirstAudioContent(offer->description());
1201 audio_desc = static_cast<const cricket::AudioContentDescription*>(
1202 audio_content->description);
1203 EXPECT_FALSE(
1204 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
1205
1206 video_content = cricket::GetFirstVideoContent(offer->description());
1207 video_desc = static_cast<const cricket::VideoContentDescription*>(
1208 video_content->description);
1209 EXPECT_FALSE(
1210 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1211
1212 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
1213
1214 // Calling RemoveTrack on a sender no longer attached to a PeerConnection
1215 // should return false.
1216 EXPECT_FALSE(pc_->RemoveTrack(audio_sender));
1217 EXPECT_FALSE(pc_->RemoveTrack(video_sender));
1218}
1219
1220// Test creating senders without a stream specified,
1221// expecting a random stream ID to be generated.
1222TEST_F(PeerConnectionInterfaceTest, AddTrackWithoutStream) {
1223 CreatePeerConnection();
1224 // Create a dummy stream, so tracks share a stream label.
1225 scoped_refptr<AudioTrackInterface> audio_track(
1226 pc_factory_->CreateAudioTrack("audio_track", nullptr));
nisseaf510af2016-03-21 08:20:42 -07001227 scoped_refptr<VideoTrackInterface> video_track(pc_factory_->CreateVideoTrack(
1228 "video_track",
1229 pc_factory_->CreateVideoSource(new cricket::FakeVideoCapturer())));
deadbeefe1f9d832016-01-14 15:35:42 -08001230 auto audio_sender =
1231 pc_->AddTrack(audio_track, std::vector<MediaStreamInterface*>());
1232 auto video_sender =
1233 pc_->AddTrack(video_track, std::vector<MediaStreamInterface*>());
1234 EXPECT_EQ("audio_track", audio_sender->id());
1235 EXPECT_EQ(audio_track, audio_sender->track());
1236 EXPECT_EQ("video_track", video_sender->id());
1237 EXPECT_EQ(video_track, video_sender->track());
1238 // If the ID is truly a random GUID, it should be infinitely unlikely they
1239 // will be the same.
1240 EXPECT_NE(video_sender->stream_id(), audio_sender->stream_id());
1241}
1242
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001243TEST_F(PeerConnectionInterfaceTest, CreateOfferReceiveAnswer) {
1244 InitiateCall();
1245 WaitAndVerifyOnAddStream(kStreamLabel1);
1246 VerifyRemoteRtpHeaderExtensions();
1247}
1248
1249TEST_F(PeerConnectionInterfaceTest, CreateOfferReceivePrAnswerAndAnswer) {
1250 CreatePeerConnection();
deadbeefab9b2d12015-10-14 11:33:11 -07001251 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001252 CreateOfferAsLocalDescription();
1253 std::string offer;
1254 EXPECT_TRUE(pc_->local_description()->ToString(&offer));
1255 CreatePrAnswerAndAnswerAsRemoteDescription(offer);
1256 WaitAndVerifyOnAddStream(kStreamLabel1);
1257}
1258
1259TEST_F(PeerConnectionInterfaceTest, ReceiveOfferCreateAnswer) {
1260 CreatePeerConnection();
deadbeefab9b2d12015-10-14 11:33:11 -07001261 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001262
1263 CreateOfferAsRemoteDescription();
1264 CreateAnswerAsLocalDescription();
1265
1266 WaitAndVerifyOnAddStream(kStreamLabel1);
1267}
1268
1269TEST_F(PeerConnectionInterfaceTest, ReceiveOfferCreatePrAnswerAndAnswer) {
1270 CreatePeerConnection();
deadbeefab9b2d12015-10-14 11:33:11 -07001271 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001272
1273 CreateOfferAsRemoteDescription();
1274 CreatePrAnswerAsLocalDescription();
1275 CreateAnswerAsLocalDescription();
1276
1277 WaitAndVerifyOnAddStream(kStreamLabel1);
1278}
1279
1280TEST_F(PeerConnectionInterfaceTest, Renegotiate) {
1281 InitiateCall();
1282 ASSERT_EQ(1u, pc_->remote_streams()->count());
1283 pc_->RemoveStream(pc_->local_streams()->at(0));
1284 CreateOfferReceiveAnswer();
1285 EXPECT_EQ(0u, pc_->remote_streams()->count());
deadbeefab9b2d12015-10-14 11:33:11 -07001286 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001287 CreateOfferReceiveAnswer();
1288}
1289
1290// Tests that after negotiating an audio only call, the respondent can perform a
1291// renegotiation that removes the audio stream.
1292TEST_F(PeerConnectionInterfaceTest, RenegotiateAudioOnly) {
1293 CreatePeerConnection();
1294 AddVoiceStream(kStreamLabel1);
1295 CreateOfferAsRemoteDescription();
1296 CreateAnswerAsLocalDescription();
1297
1298 ASSERT_EQ(1u, pc_->remote_streams()->count());
1299 pc_->RemoveStream(pc_->local_streams()->at(0));
1300 CreateOfferReceiveAnswer();
1301 EXPECT_EQ(0u, pc_->remote_streams()->count());
1302}
1303
1304// Test that candidates are generated and that we can parse our own candidates.
1305TEST_F(PeerConnectionInterfaceTest, IceCandidates) {
1306 CreatePeerConnection();
1307
1308 EXPECT_FALSE(pc_->AddIceCandidate(observer_.last_candidate_.get()));
1309 // SetRemoteDescription takes ownership of offer.
kwibergd1fe2812016-04-27 06:47:29 -07001310 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefab9b2d12015-10-14 11:33:11 -07001311 AddVideoStream(kStreamLabel1);
deadbeefc80741f2015-10-22 13:14:45 -07001312 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
kwiberg2bbff992016-03-16 11:03:04 -07001313 EXPECT_TRUE(DoSetRemoteDescription(offer.release()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001314
1315 // SetLocalDescription takes ownership of answer.
kwibergd1fe2812016-04-27 06:47:29 -07001316 std::unique_ptr<SessionDescriptionInterface> answer;
deadbeefc80741f2015-10-22 13:14:45 -07001317 EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
kwiberg2bbff992016-03-16 11:03:04 -07001318 EXPECT_TRUE(DoSetLocalDescription(answer.release()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001319
1320 EXPECT_TRUE_WAIT(observer_.last_candidate_.get() != NULL, kTimeout);
1321 EXPECT_TRUE_WAIT(observer_.ice_complete_, kTimeout);
1322
1323 EXPECT_TRUE(pc_->AddIceCandidate(observer_.last_candidate_.get()));
1324}
1325
deadbeefab9b2d12015-10-14 11:33:11 -07001326// Test that CreateOffer and CreateAnswer will fail if the track labels are
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001327// not unique.
1328TEST_F(PeerConnectionInterfaceTest, CreateOfferAnswerWithInvalidStream) {
1329 CreatePeerConnection();
1330 // Create a regular offer for the CreateAnswer test later.
kwibergd1fe2812016-04-27 06:47:29 -07001331 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefc80741f2015-10-22 13:14:45 -07001332 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
kwiberg2bbff992016-03-16 11:03:04 -07001333 EXPECT_TRUE(offer);
1334 offer.reset();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001335
1336 // Create a local stream with audio&video tracks having same label.
1337 AddAudioVideoStream(kStreamLabel1, "track_label", "track_label");
1338
1339 // Test CreateOffer
deadbeefc80741f2015-10-22 13:14:45 -07001340 EXPECT_FALSE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001341
1342 // Test CreateAnswer
kwibergd1fe2812016-04-27 06:47:29 -07001343 std::unique_ptr<SessionDescriptionInterface> answer;
deadbeefc80741f2015-10-22 13:14:45 -07001344 EXPECT_FALSE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001345}
1346
1347// Test that we will get different SSRCs for each tracks in the offer and answer
1348// we created.
1349TEST_F(PeerConnectionInterfaceTest, SsrcInOfferAnswer) {
1350 CreatePeerConnection();
1351 // Create a local stream with audio&video tracks having different labels.
1352 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
1353
1354 // Test CreateOffer
kwibergd1fe2812016-04-27 06:47:29 -07001355 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001356 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001357 int audio_ssrc = 0;
1358 int video_ssrc = 0;
1359 EXPECT_TRUE(GetFirstSsrc(GetFirstAudioContent(offer->description()),
1360 &audio_ssrc));
1361 EXPECT_TRUE(GetFirstSsrc(GetFirstVideoContent(offer->description()),
1362 &video_ssrc));
1363 EXPECT_NE(audio_ssrc, video_ssrc);
1364
1365 // Test CreateAnswer
1366 EXPECT_TRUE(DoSetRemoteDescription(offer.release()));
kwibergd1fe2812016-04-27 06:47:29 -07001367 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -07001368 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001369 audio_ssrc = 0;
1370 video_ssrc = 0;
1371 EXPECT_TRUE(GetFirstSsrc(GetFirstAudioContent(answer->description()),
1372 &audio_ssrc));
1373 EXPECT_TRUE(GetFirstSsrc(GetFirstVideoContent(answer->description()),
1374 &video_ssrc));
1375 EXPECT_NE(audio_ssrc, video_ssrc);
1376}
1377
deadbeefeb459812015-12-15 19:24:43 -08001378// Test that it's possible to call AddTrack on a MediaStream after adding
1379// the stream to a PeerConnection.
1380// TODO(deadbeef): Remove this test once this behavior is no longer supported.
1381TEST_F(PeerConnectionInterfaceTest, AddTrackAfterAddStream) {
1382 CreatePeerConnection();
1383 // Create audio stream and add to PeerConnection.
1384 AddVoiceStream(kStreamLabel1);
1385 MediaStreamInterface* stream = pc_->local_streams()->at(0);
1386
1387 // Add video track to the audio-only stream.
nisseaf510af2016-03-21 08:20:42 -07001388 scoped_refptr<VideoTrackInterface> video_track(pc_factory_->CreateVideoTrack(
1389 "video_label",
1390 pc_factory_->CreateVideoSource(new cricket::FakeVideoCapturer())));
deadbeefeb459812015-12-15 19:24:43 -08001391 stream->AddTrack(video_track.get());
1392
kwibergd1fe2812016-04-27 06:47:29 -07001393 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001394 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefeb459812015-12-15 19:24:43 -08001395
1396 const cricket::MediaContentDescription* video_desc =
1397 cricket::GetFirstVideoContentDescription(offer->description());
1398 EXPECT_TRUE(video_desc != nullptr);
1399}
1400
1401// Test that it's possible to call RemoveTrack on a MediaStream after adding
1402// the stream to a PeerConnection.
1403// TODO(deadbeef): Remove this test once this behavior is no longer supported.
1404TEST_F(PeerConnectionInterfaceTest, RemoveTrackAfterAddStream) {
1405 CreatePeerConnection();
1406 // Create audio/video stream and add to PeerConnection.
1407 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
1408 MediaStreamInterface* stream = pc_->local_streams()->at(0);
1409
1410 // Remove the video track.
1411 stream->RemoveTrack(stream->GetVideoTracks()[0]);
1412
kwibergd1fe2812016-04-27 06:47:29 -07001413 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001414 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefeb459812015-12-15 19:24:43 -08001415
1416 const cricket::MediaContentDescription* video_desc =
1417 cricket::GetFirstVideoContentDescription(offer->description());
1418 EXPECT_TRUE(video_desc == nullptr);
1419}
1420
deadbeefbd7d8f72015-12-18 16:58:44 -08001421// Test creating a sender with a stream ID, and ensure the ID is populated
1422// in the offer.
1423TEST_F(PeerConnectionInterfaceTest, CreateSenderWithStream) {
1424 CreatePeerConnection();
1425 pc_->CreateSender("video", kStreamLabel1);
1426
kwibergd1fe2812016-04-27 06:47:29 -07001427 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001428 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefbd7d8f72015-12-18 16:58:44 -08001429
1430 const cricket::MediaContentDescription* video_desc =
1431 cricket::GetFirstVideoContentDescription(offer->description());
1432 ASSERT_TRUE(video_desc != nullptr);
1433 ASSERT_EQ(1u, video_desc->streams().size());
1434 EXPECT_EQ(kStreamLabel1, video_desc->streams()[0].sync_label);
1435}
1436
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001437// Test that we can specify a certain track that we want statistics about.
1438TEST_F(PeerConnectionInterfaceTest, GetStatsForSpecificTrack) {
1439 InitiateCall();
1440 ASSERT_LT(0u, pc_->remote_streams()->count());
1441 ASSERT_LT(0u, pc_->remote_streams()->at(0)->GetAudioTracks().size());
1442 scoped_refptr<MediaStreamTrackInterface> remote_audio =
1443 pc_->remote_streams()->at(0)->GetAudioTracks()[0];
1444 EXPECT_TRUE(DoGetStats(remote_audio));
1445
1446 // Remove the stream. Since we are sending to our selves the local
1447 // and the remote stream is the same.
1448 pc_->RemoveStream(pc_->local_streams()->at(0));
1449 // Do a re-negotiation.
1450 CreateOfferReceiveAnswer();
1451
1452 ASSERT_EQ(0u, pc_->remote_streams()->count());
1453
1454 // Test that we still can get statistics for the old track. Even if it is not
1455 // sent any longer.
1456 EXPECT_TRUE(DoGetStats(remote_audio));
1457}
1458
1459// Test that we can get stats on a video track.
1460TEST_F(PeerConnectionInterfaceTest, GetStatsForVideoTrack) {
1461 InitiateCall();
1462 ASSERT_LT(0u, pc_->remote_streams()->count());
1463 ASSERT_LT(0u, pc_->remote_streams()->at(0)->GetVideoTracks().size());
1464 scoped_refptr<MediaStreamTrackInterface> remote_video =
1465 pc_->remote_streams()->at(0)->GetVideoTracks()[0];
1466 EXPECT_TRUE(DoGetStats(remote_video));
1467}
1468
1469// Test that we don't get statistics for an invalid track.
tommi@webrtc.org908f57e2014-07-21 11:44:39 +00001470// TODO(tommi): Fix this test. DoGetStats will return true
1471// for the unknown track (since GetStats is async), but no
1472// data is returned for the track.
1473TEST_F(PeerConnectionInterfaceTest, DISABLED_GetStatsForInvalidTrack) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001474 InitiateCall();
1475 scoped_refptr<AudioTrackInterface> unknown_audio_track(
1476 pc_factory_->CreateAudioTrack("unknown track", NULL));
1477 EXPECT_FALSE(DoGetStats(unknown_audio_track));
1478}
1479
1480// This test setup two RTP data channels in loop back.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001481TEST_F(PeerConnectionInterfaceTest, TestDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001482 FakeConstraints constraints;
1483 constraints.SetAllowRtpDataChannels();
1484 CreatePeerConnection(&constraints);
1485 scoped_refptr<DataChannelInterface> data1 =
1486 pc_->CreateDataChannel("test1", NULL);
1487 scoped_refptr<DataChannelInterface> data2 =
1488 pc_->CreateDataChannel("test2", NULL);
1489 ASSERT_TRUE(data1 != NULL);
kwibergd1fe2812016-04-27 06:47:29 -07001490 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001491 new MockDataChannelObserver(data1));
kwibergd1fe2812016-04-27 06:47:29 -07001492 std::unique_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001493 new MockDataChannelObserver(data2));
1494
1495 EXPECT_EQ(DataChannelInterface::kConnecting, data1->state());
1496 EXPECT_EQ(DataChannelInterface::kConnecting, data2->state());
1497 std::string data_to_send1 = "testing testing";
1498 std::string data_to_send2 = "testing something else";
1499 EXPECT_FALSE(data1->Send(DataBuffer(data_to_send1)));
1500
1501 CreateOfferReceiveAnswer();
1502 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1503 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
1504
1505 EXPECT_EQ(DataChannelInterface::kOpen, data1->state());
1506 EXPECT_EQ(DataChannelInterface::kOpen, data2->state());
1507 EXPECT_TRUE(data1->Send(DataBuffer(data_to_send1)));
1508 EXPECT_TRUE(data2->Send(DataBuffer(data_to_send2)));
1509
1510 EXPECT_EQ_WAIT(data_to_send1, observer1->last_message(), kTimeout);
1511 EXPECT_EQ_WAIT(data_to_send2, observer2->last_message(), kTimeout);
1512
1513 data1->Close();
1514 EXPECT_EQ(DataChannelInterface::kClosing, data1->state());
1515 CreateOfferReceiveAnswer();
1516 EXPECT_FALSE(observer1->IsOpen());
1517 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
1518 EXPECT_TRUE(observer2->IsOpen());
1519
1520 data_to_send2 = "testing something else again";
1521 EXPECT_TRUE(data2->Send(DataBuffer(data_to_send2)));
1522
1523 EXPECT_EQ_WAIT(data_to_send2, observer2->last_message(), kTimeout);
1524}
1525
1526// This test verifies that sendnig binary data over RTP data channels should
1527// fail.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001528TEST_F(PeerConnectionInterfaceTest, TestSendBinaryOnRtpDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001529 FakeConstraints constraints;
1530 constraints.SetAllowRtpDataChannels();
1531 CreatePeerConnection(&constraints);
1532 scoped_refptr<DataChannelInterface> data1 =
1533 pc_->CreateDataChannel("test1", NULL);
1534 scoped_refptr<DataChannelInterface> data2 =
1535 pc_->CreateDataChannel("test2", NULL);
1536 ASSERT_TRUE(data1 != NULL);
kwibergd1fe2812016-04-27 06:47:29 -07001537 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001538 new MockDataChannelObserver(data1));
kwibergd1fe2812016-04-27 06:47:29 -07001539 std::unique_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001540 new MockDataChannelObserver(data2));
1541
1542 EXPECT_EQ(DataChannelInterface::kConnecting, data1->state());
1543 EXPECT_EQ(DataChannelInterface::kConnecting, data2->state());
1544
1545 CreateOfferReceiveAnswer();
1546 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1547 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
1548
1549 EXPECT_EQ(DataChannelInterface::kOpen, data1->state());
1550 EXPECT_EQ(DataChannelInterface::kOpen, data2->state());
1551
jbaucheec21bd2016-03-20 06:15:43 -07001552 rtc::CopyOnWriteBuffer buffer("test", 4);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001553 EXPECT_FALSE(data1->Send(DataBuffer(buffer, true)));
1554}
1555
1556// This test setup a RTP data channels in loop back and test that a channel is
1557// opened even if the remote end answer with a zero SSRC.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001558TEST_F(PeerConnectionInterfaceTest, TestSendOnlyDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001559 FakeConstraints constraints;
1560 constraints.SetAllowRtpDataChannels();
1561 CreatePeerConnection(&constraints);
1562 scoped_refptr<DataChannelInterface> data1 =
1563 pc_->CreateDataChannel("test1", NULL);
kwibergd1fe2812016-04-27 06:47:29 -07001564 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001565 new MockDataChannelObserver(data1));
1566
1567 CreateOfferReceiveAnswerWithoutSsrc();
1568
1569 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1570
1571 data1->Close();
1572 EXPECT_EQ(DataChannelInterface::kClosing, data1->state());
1573 CreateOfferReceiveAnswerWithoutSsrc();
1574 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
1575 EXPECT_FALSE(observer1->IsOpen());
1576}
1577
1578// This test that if a data channel is added in an answer a receive only channel
1579// channel is created.
1580TEST_F(PeerConnectionInterfaceTest, TestReceiveOnlyDataChannel) {
1581 FakeConstraints constraints;
1582 constraints.SetAllowRtpDataChannels();
1583 CreatePeerConnection(&constraints);
1584
1585 std::string offer_label = "offer_channel";
1586 scoped_refptr<DataChannelInterface> offer_channel =
1587 pc_->CreateDataChannel(offer_label, NULL);
1588
1589 CreateOfferAsLocalDescription();
1590
1591 // Replace the data channel label in the offer and apply it as an answer.
1592 std::string receive_label = "answer_channel";
1593 std::string sdp;
1594 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001595 rtc::replace_substrs(offer_label.c_str(), offer_label.length(),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001596 receive_label.c_str(), receive_label.length(),
1597 &sdp);
1598 CreateAnswerAsRemoteDescription(sdp);
1599
1600 // Verify that a new incoming data channel has been created and that
1601 // it is open but can't we written to.
1602 ASSERT_TRUE(observer_.last_datachannel_ != NULL);
1603 DataChannelInterface* received_channel = observer_.last_datachannel_;
1604 EXPECT_EQ(DataChannelInterface::kConnecting, received_channel->state());
1605 EXPECT_EQ(receive_label, received_channel->label());
1606 EXPECT_FALSE(received_channel->Send(DataBuffer("something")));
1607
1608 // Verify that the channel we initially offered has been rejected.
1609 EXPECT_EQ(DataChannelInterface::kClosed, offer_channel->state());
1610
1611 // Do another offer / answer exchange and verify that the data channel is
1612 // opened.
1613 CreateOfferReceiveAnswer();
1614 EXPECT_EQ_WAIT(DataChannelInterface::kOpen, received_channel->state(),
1615 kTimeout);
1616}
1617
1618// This test that no data channel is returned if a reliable channel is
1619// requested.
1620// TODO(perkj): Remove this test once reliable channels are implemented.
1621TEST_F(PeerConnectionInterfaceTest, CreateReliableRtpDataChannelShouldFail) {
1622 FakeConstraints constraints;
1623 constraints.SetAllowRtpDataChannels();
1624 CreatePeerConnection(&constraints);
1625
1626 std::string label = "test";
1627 webrtc::DataChannelInit config;
1628 config.reliable = true;
1629 scoped_refptr<DataChannelInterface> channel =
1630 pc_->CreateDataChannel(label, &config);
1631 EXPECT_TRUE(channel == NULL);
1632}
1633
deadbeefab9b2d12015-10-14 11:33:11 -07001634// Verifies that duplicated label is not allowed for RTP data channel.
1635TEST_F(PeerConnectionInterfaceTest, RtpDuplicatedLabelNotAllowed) {
1636 FakeConstraints constraints;
1637 constraints.SetAllowRtpDataChannels();
1638 CreatePeerConnection(&constraints);
1639
1640 std::string label = "test";
1641 scoped_refptr<DataChannelInterface> channel =
1642 pc_->CreateDataChannel(label, nullptr);
1643 EXPECT_NE(channel, nullptr);
1644
1645 scoped_refptr<DataChannelInterface> dup_channel =
1646 pc_->CreateDataChannel(label, nullptr);
1647 EXPECT_EQ(dup_channel, nullptr);
1648}
1649
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001650// This tests that a SCTP data channel is returned using different
1651// DataChannelInit configurations.
1652TEST_F(PeerConnectionInterfaceTest, CreateSctpDataChannel) {
1653 FakeConstraints constraints;
1654 constraints.SetAllowDtlsSctpDataChannels();
1655 CreatePeerConnection(&constraints);
1656
1657 webrtc::DataChannelInit config;
1658
1659 scoped_refptr<DataChannelInterface> channel =
1660 pc_->CreateDataChannel("1", &config);
1661 EXPECT_TRUE(channel != NULL);
1662 EXPECT_TRUE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001663 EXPECT_TRUE(observer_.renegotiation_needed_);
1664 observer_.renegotiation_needed_ = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001665
1666 config.ordered = false;
1667 channel = pc_->CreateDataChannel("2", &config);
1668 EXPECT_TRUE(channel != NULL);
1669 EXPECT_TRUE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001670 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001671
1672 config.ordered = true;
1673 config.maxRetransmits = 0;
1674 channel = pc_->CreateDataChannel("3", &config);
1675 EXPECT_TRUE(channel != NULL);
1676 EXPECT_FALSE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001677 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001678
1679 config.maxRetransmits = -1;
1680 config.maxRetransmitTime = 0;
1681 channel = pc_->CreateDataChannel("4", &config);
1682 EXPECT_TRUE(channel != NULL);
1683 EXPECT_FALSE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001684 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001685}
1686
1687// This tests that no data channel is returned if both maxRetransmits and
1688// maxRetransmitTime are set for SCTP data channels.
1689TEST_F(PeerConnectionInterfaceTest,
1690 CreateSctpDataChannelShouldFailForInvalidConfig) {
1691 FakeConstraints constraints;
1692 constraints.SetAllowDtlsSctpDataChannels();
1693 CreatePeerConnection(&constraints);
1694
1695 std::string label = "test";
1696 webrtc::DataChannelInit config;
1697 config.maxRetransmits = 0;
1698 config.maxRetransmitTime = 0;
1699
1700 scoped_refptr<DataChannelInterface> channel =
1701 pc_->CreateDataChannel(label, &config);
1702 EXPECT_TRUE(channel == NULL);
1703}
1704
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001705// The test verifies that creating a SCTP data channel with an id already in use
1706// or out of range should fail.
1707TEST_F(PeerConnectionInterfaceTest,
1708 CreateSctpDataChannelWithInvalidIdShouldFail) {
1709 FakeConstraints constraints;
1710 constraints.SetAllowDtlsSctpDataChannels();
1711 CreatePeerConnection(&constraints);
1712
1713 webrtc::DataChannelInit config;
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001714 scoped_refptr<DataChannelInterface> channel;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001715
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001716 config.id = 1;
1717 channel = pc_->CreateDataChannel("1", &config);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001718 EXPECT_TRUE(channel != NULL);
1719 EXPECT_EQ(1, channel->id());
1720
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001721 channel = pc_->CreateDataChannel("x", &config);
1722 EXPECT_TRUE(channel == NULL);
1723
1724 config.id = cricket::kMaxSctpSid;
1725 channel = pc_->CreateDataChannel("max", &config);
1726 EXPECT_TRUE(channel != NULL);
1727 EXPECT_EQ(config.id, channel->id());
1728
1729 config.id = cricket::kMaxSctpSid + 1;
1730 channel = pc_->CreateDataChannel("x", &config);
1731 EXPECT_TRUE(channel == NULL);
1732}
1733
deadbeefab9b2d12015-10-14 11:33:11 -07001734// Verifies that duplicated label is allowed for SCTP data channel.
1735TEST_F(PeerConnectionInterfaceTest, SctpDuplicatedLabelAllowed) {
1736 FakeConstraints constraints;
1737 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
1738 true);
1739 CreatePeerConnection(&constraints);
1740
1741 std::string label = "test";
1742 scoped_refptr<DataChannelInterface> channel =
1743 pc_->CreateDataChannel(label, nullptr);
1744 EXPECT_NE(channel, nullptr);
1745
1746 scoped_refptr<DataChannelInterface> dup_channel =
1747 pc_->CreateDataChannel(label, nullptr);
1748 EXPECT_NE(dup_channel, nullptr);
1749}
1750
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001751// This test verifies that OnRenegotiationNeeded is fired for every new RTP
1752// DataChannel.
1753TEST_F(PeerConnectionInterfaceTest, RenegotiationNeededForNewRtpDataChannel) {
1754 FakeConstraints constraints;
1755 constraints.SetAllowRtpDataChannels();
1756 CreatePeerConnection(&constraints);
1757
1758 scoped_refptr<DataChannelInterface> dc1 =
1759 pc_->CreateDataChannel("test1", NULL);
1760 EXPECT_TRUE(observer_.renegotiation_needed_);
1761 observer_.renegotiation_needed_ = false;
1762
1763 scoped_refptr<DataChannelInterface> dc2 =
1764 pc_->CreateDataChannel("test2", NULL);
1765 EXPECT_TRUE(observer_.renegotiation_needed_);
1766}
1767
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001768// This test that a data channel closes when a PeerConnection is deleted/closed.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001769TEST_F(PeerConnectionInterfaceTest, DataChannelCloseWhenPeerConnectionClose) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001770 FakeConstraints constraints;
1771 constraints.SetAllowRtpDataChannels();
1772 CreatePeerConnection(&constraints);
1773
1774 scoped_refptr<DataChannelInterface> data1 =
1775 pc_->CreateDataChannel("test1", NULL);
1776 scoped_refptr<DataChannelInterface> data2 =
1777 pc_->CreateDataChannel("test2", NULL);
1778 ASSERT_TRUE(data1 != NULL);
kwibergd1fe2812016-04-27 06:47:29 -07001779 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001780 new MockDataChannelObserver(data1));
kwibergd1fe2812016-04-27 06:47:29 -07001781 std::unique_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001782 new MockDataChannelObserver(data2));
1783
1784 CreateOfferReceiveAnswer();
1785 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1786 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
1787
1788 ReleasePeerConnection();
1789 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
1790 EXPECT_EQ(DataChannelInterface::kClosed, data2->state());
1791}
1792
1793// This test that data channels can be rejected in an answer.
1794TEST_F(PeerConnectionInterfaceTest, TestRejectDataChannelInAnswer) {
1795 FakeConstraints constraints;
1796 constraints.SetAllowRtpDataChannels();
1797 CreatePeerConnection(&constraints);
1798
1799 scoped_refptr<DataChannelInterface> offer_channel(
1800 pc_->CreateDataChannel("offer_channel", NULL));
1801
1802 CreateOfferAsLocalDescription();
1803
1804 // Create an answer where the m-line for data channels are rejected.
1805 std::string sdp;
1806 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
1807 webrtc::JsepSessionDescription* answer = new webrtc::JsepSessionDescription(
1808 SessionDescriptionInterface::kAnswer);
1809 EXPECT_TRUE(answer->Initialize(sdp, NULL));
1810 cricket::ContentInfo* data_info =
1811 answer->description()->GetContentByName("data");
1812 data_info->rejected = true;
1813
1814 DoSetRemoteDescription(answer);
1815 EXPECT_EQ(DataChannelInterface::kClosed, offer_channel->state());
1816}
1817
1818// Test that we can create a session description from an SDP string from
1819// FireFox, use it as a remote session description, generate an answer and use
1820// the answer as a local description.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07001821TEST_F(PeerConnectionInterfaceTest, ReceiveFireFoxOffer) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001822 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001823 FakeConstraints constraints;
1824 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
1825 true);
1826 CreatePeerConnection(&constraints);
1827 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
1828 SessionDescriptionInterface* desc =
1829 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
jbauchfabe2c92015-07-16 13:43:14 -07001830 webrtc::kFireFoxSdpOffer, nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001831 EXPECT_TRUE(DoSetSessionDescription(desc, false));
1832 CreateAnswerAsLocalDescription();
1833 ASSERT_TRUE(pc_->local_description() != NULL);
1834 ASSERT_TRUE(pc_->remote_description() != NULL);
1835
1836 const cricket::ContentInfo* content =
1837 cricket::GetFirstAudioContent(pc_->local_description()->description());
1838 ASSERT_TRUE(content != NULL);
1839 EXPECT_FALSE(content->rejected);
1840
1841 content =
1842 cricket::GetFirstVideoContent(pc_->local_description()->description());
1843 ASSERT_TRUE(content != NULL);
1844 EXPECT_FALSE(content->rejected);
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00001845#ifdef HAVE_SCTP
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001846 content =
1847 cricket::GetFirstDataContent(pc_->local_description()->description());
1848 ASSERT_TRUE(content != NULL);
1849 EXPECT_TRUE(content->rejected);
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00001850#endif
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001851}
1852
1853// Test that we can create an audio only offer and receive an answer with a
1854// limited set of audio codecs and receive an updated offer with more audio
1855// codecs, where the added codecs are not supported.
1856TEST_F(PeerConnectionInterfaceTest, ReceiveUpdatedAudioOfferWithBadCodecs) {
1857 CreatePeerConnection();
1858 AddVoiceStream("audio_label");
1859 CreateOfferAsLocalDescription();
1860
1861 SessionDescriptionInterface* answer =
1862 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
jbauchfabe2c92015-07-16 13:43:14 -07001863 webrtc::kAudioSdp, nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001864 EXPECT_TRUE(DoSetSessionDescription(answer, false));
1865
1866 SessionDescriptionInterface* updated_offer =
1867 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
jbauchfabe2c92015-07-16 13:43:14 -07001868 webrtc::kAudioSdpWithUnsupportedCodecs,
1869 nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001870 EXPECT_TRUE(DoSetSessionDescription(updated_offer, false));
1871 CreateAnswerAsLocalDescription();
1872}
1873
deadbeefc80741f2015-10-22 13:14:45 -07001874// Test that if we're receiving (but not sending) a track, subsequent offers
1875// will have m-lines with a=recvonly.
1876TEST_F(PeerConnectionInterfaceTest, CreateSubsequentRecvOnlyOffer) {
1877 FakeConstraints constraints;
1878 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
1879 true);
1880 CreatePeerConnection(&constraints);
1881 CreateAndSetRemoteOffer(kSdpStringWithStream1);
1882 CreateAnswerAsLocalDescription();
1883
1884 // At this point we should be receiving stream 1, but not sending anything.
1885 // A new offer should be recvonly.
kwibergd1fe2812016-04-27 06:47:29 -07001886 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefc80741f2015-10-22 13:14:45 -07001887 DoCreateOffer(&offer, nullptr);
1888
1889 const cricket::ContentInfo* video_content =
1890 cricket::GetFirstVideoContent(offer->description());
1891 const cricket::VideoContentDescription* video_desc =
1892 static_cast<const cricket::VideoContentDescription*>(
1893 video_content->description);
1894 ASSERT_EQ(cricket::MD_RECVONLY, video_desc->direction());
1895
1896 const cricket::ContentInfo* audio_content =
1897 cricket::GetFirstAudioContent(offer->description());
1898 const cricket::AudioContentDescription* audio_desc =
1899 static_cast<const cricket::AudioContentDescription*>(
1900 audio_content->description);
1901 ASSERT_EQ(cricket::MD_RECVONLY, audio_desc->direction());
1902}
1903
1904// Test that if we're receiving (but not sending) a track, and the
1905// offerToReceiveVideo/offerToReceiveAudio constraints are explicitly set to
1906// false, the generated m-lines will be a=inactive.
1907TEST_F(PeerConnectionInterfaceTest, CreateSubsequentInactiveOffer) {
1908 FakeConstraints constraints;
1909 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
1910 true);
1911 CreatePeerConnection(&constraints);
1912 CreateAndSetRemoteOffer(kSdpStringWithStream1);
1913 CreateAnswerAsLocalDescription();
1914
1915 // At this point we should be receiving stream 1, but not sending anything.
1916 // A new offer would be recvonly, but we'll set the "no receive" constraints
1917 // to make it inactive.
kwibergd1fe2812016-04-27 06:47:29 -07001918 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefc80741f2015-10-22 13:14:45 -07001919 FakeConstraints offer_constraints;
1920 offer_constraints.AddMandatory(
1921 webrtc::MediaConstraintsInterface::kOfferToReceiveVideo, false);
1922 offer_constraints.AddMandatory(
1923 webrtc::MediaConstraintsInterface::kOfferToReceiveAudio, false);
1924 DoCreateOffer(&offer, &offer_constraints);
1925
1926 const cricket::ContentInfo* video_content =
1927 cricket::GetFirstVideoContent(offer->description());
1928 const cricket::VideoContentDescription* video_desc =
1929 static_cast<const cricket::VideoContentDescription*>(
1930 video_content->description);
1931 ASSERT_EQ(cricket::MD_INACTIVE, video_desc->direction());
1932
1933 const cricket::ContentInfo* audio_content =
1934 cricket::GetFirstAudioContent(offer->description());
1935 const cricket::AudioContentDescription* audio_desc =
1936 static_cast<const cricket::AudioContentDescription*>(
1937 audio_content->description);
1938 ASSERT_EQ(cricket::MD_INACTIVE, audio_desc->direction());
1939}
1940
deadbeef653b8e02015-11-11 12:55:10 -08001941// Test that we can use SetConfiguration to change the ICE servers of the
1942// PortAllocator.
1943TEST_F(PeerConnectionInterfaceTest, SetConfigurationChangesIceServers) {
1944 CreatePeerConnection();
1945
1946 PeerConnectionInterface::RTCConfiguration config;
1947 PeerConnectionInterface::IceServer server;
1948 server.uri = "stun:test_hostname";
1949 config.servers.push_back(server);
1950 EXPECT_TRUE(pc_->SetConfiguration(config));
1951
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08001952 EXPECT_EQ(1u, port_allocator_->stun_servers().size());
1953 EXPECT_EQ("test_hostname",
1954 port_allocator_->stun_servers().begin()->hostname());
deadbeef653b8e02015-11-11 12:55:10 -08001955}
1956
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001957TEST_F(PeerConnectionInterfaceTest, SetConfigurationChangesCandidateFilter) {
1958 CreatePeerConnection();
1959 PeerConnectionInterface::RTCConfiguration config;
1960 config.type = PeerConnectionInterface::kRelay;
1961 EXPECT_TRUE(pc_->SetConfiguration(config));
1962 EXPECT_EQ(cricket::CF_RELAY, port_allocator_->candidate_filter());
1963}
1964
1965// Test that when SetConfiguration changes both the pool size and other
1966// attributes, the pooled session is created with the updated attributes.
1967TEST_F(PeerConnectionInterfaceTest,
1968 SetConfigurationCreatesPooledSessionCorrectly) {
1969 CreatePeerConnection();
1970 PeerConnectionInterface::RTCConfiguration config;
1971 config.ice_candidate_pool_size = 1;
1972 PeerConnectionInterface::IceServer server;
1973 server.uri = kStunAddressOnly;
1974 config.servers.push_back(server);
1975 config.type = PeerConnectionInterface::kRelay;
1976 CreatePeerConnection(config, nullptr);
1977
1978 const cricket::FakePortAllocatorSession* session =
1979 static_cast<const cricket::FakePortAllocatorSession*>(
1980 port_allocator_->GetPooledSession());
1981 ASSERT_NE(nullptr, session);
1982 EXPECT_EQ(1UL, session->stun_servers().size());
1983 EXPECT_EQ(cricket::CF_RELAY, session->candidate_filter());
1984}
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}