blob: 8ced9a784153711f5b1ab0e344ed1375984c39c5 [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(
danilchape9021a32016-05-17 01:52:02 -0700548 rtc::Thread::Current(), rtc::Thread::Current(), rtc::Thread::Current(),
549 nullptr, nullptr, nullptr);
550 ASSERT_TRUE(pc_factory_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000551 }
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;
Henrik Boström400781a2016-05-27 14:51:55 +0200619 pc = pc_factory_->CreatePeerConnection(
620 config,
621 nullptr,
622 nullptr,
623 std::unique_ptr<rtc::RTCCertificateGeneratorInterface>(),
624 &observer_);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800625 EXPECT_EQ(nullptr, pc);
deadbeef0a6c4ca2015-10-06 11:38:28 -0700626 }
627
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000628 void CreatePeerConnectionWithDifferentConfigurations() {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700629 CreatePeerConnectionWithIceServer(kStunAddressOnly, "");
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800630 EXPECT_EQ(1u, port_allocator_->stun_servers().size());
631 EXPECT_EQ(0u, port_allocator_->turn_servers().size());
632 EXPECT_EQ("address", port_allocator_->stun_servers().begin()->hostname());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000633 EXPECT_EQ(kDefaultStunPort,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800634 port_allocator_->stun_servers().begin()->port());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000635
deadbeef0a6c4ca2015-10-06 11:38:28 -0700636 CreatePeerConnectionExpectFail(kStunInvalidPort);
637 CreatePeerConnectionExpectFail(kStunAddressPortAndMore1);
638 CreatePeerConnectionExpectFail(kStunAddressPortAndMore2);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000639
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700640 CreatePeerConnectionWithIceServer(kTurnIceServerUri, kTurnPassword);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800641 EXPECT_EQ(0u, port_allocator_->stun_servers().size());
642 EXPECT_EQ(1u, port_allocator_->turn_servers().size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000643 EXPECT_EQ(kTurnUsername,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800644 port_allocator_->turn_servers()[0].credentials.username);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000645 EXPECT_EQ(kTurnPassword,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800646 port_allocator_->turn_servers()[0].credentials.password);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000647 EXPECT_EQ(kTurnHostname,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800648 port_allocator_->turn_servers()[0].ports[0].address.hostname());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000649 }
650
651 void ReleasePeerConnection() {
652 pc_ = NULL;
653 observer_.SetPeerConnectionInterface(NULL);
654 }
655
deadbeefab9b2d12015-10-14 11:33:11 -0700656 void AddVideoStream(const std::string& label) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000657 // Create a local stream.
658 scoped_refptr<MediaStreamInterface> stream(
659 pc_factory_->CreateLocalMediaStream(label));
perkja3ede6c2016-03-08 01:27:48 +0100660 scoped_refptr<VideoTrackSourceInterface> video_source(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000661 pc_factory_->CreateVideoSource(new cricket::FakeVideoCapturer(), NULL));
662 scoped_refptr<VideoTrackInterface> video_track(
663 pc_factory_->CreateVideoTrack(label + "v0", video_source));
664 stream->AddTrack(video_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +0000665 EXPECT_TRUE(pc_->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000666 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
667 observer_.renegotiation_needed_ = false;
668 }
669
670 void AddVoiceStream(const std::string& label) {
671 // Create a local stream.
672 scoped_refptr<MediaStreamInterface> stream(
673 pc_factory_->CreateLocalMediaStream(label));
674 scoped_refptr<AudioTrackInterface> audio_track(
675 pc_factory_->CreateAudioTrack(label + "a0", NULL));
676 stream->AddTrack(audio_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +0000677 EXPECT_TRUE(pc_->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000678 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
679 observer_.renegotiation_needed_ = false;
680 }
681
682 void AddAudioVideoStream(const std::string& stream_label,
683 const std::string& audio_track_label,
684 const std::string& video_track_label) {
685 // Create a local stream.
686 scoped_refptr<MediaStreamInterface> stream(
687 pc_factory_->CreateLocalMediaStream(stream_label));
688 scoped_refptr<AudioTrackInterface> audio_track(
689 pc_factory_->CreateAudioTrack(
690 audio_track_label, static_cast<AudioSourceInterface*>(NULL)));
691 stream->AddTrack(audio_track.get());
692 scoped_refptr<VideoTrackInterface> video_track(
nisseaf510af2016-03-21 08:20:42 -0700693 pc_factory_->CreateVideoTrack(
694 video_track_label,
695 pc_factory_->CreateVideoSource(new cricket::FakeVideoCapturer())));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000696 stream->AddTrack(video_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +0000697 EXPECT_TRUE(pc_->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000698 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
699 observer_.renegotiation_needed_ = false;
700 }
701
kwibergd1fe2812016-04-27 06:47:29 -0700702 bool DoCreateOfferAnswer(std::unique_ptr<SessionDescriptionInterface>* desc,
deadbeefc80741f2015-10-22 13:14:45 -0700703 bool offer,
704 MediaConstraintsInterface* constraints) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000705 rtc::scoped_refptr<MockCreateSessionDescriptionObserver>
706 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000707 MockCreateSessionDescriptionObserver>());
708 if (offer) {
deadbeefc80741f2015-10-22 13:14:45 -0700709 pc_->CreateOffer(observer, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000710 } else {
deadbeefc80741f2015-10-22 13:14:45 -0700711 pc_->CreateAnswer(observer, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000712 }
713 EXPECT_EQ_WAIT(true, observer->called(), kTimeout);
kwiberg2bbff992016-03-16 11:03:04 -0700714 desc->reset(observer->release_desc());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000715 return observer->result();
716 }
717
kwibergd1fe2812016-04-27 06:47:29 -0700718 bool DoCreateOffer(std::unique_ptr<SessionDescriptionInterface>* desc,
deadbeefc80741f2015-10-22 13:14:45 -0700719 MediaConstraintsInterface* constraints) {
720 return DoCreateOfferAnswer(desc, true, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000721 }
722
kwibergd1fe2812016-04-27 06:47:29 -0700723 bool DoCreateAnswer(std::unique_ptr<SessionDescriptionInterface>* desc,
deadbeefc80741f2015-10-22 13:14:45 -0700724 MediaConstraintsInterface* constraints) {
725 return DoCreateOfferAnswer(desc, false, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000726 }
727
728 bool DoSetSessionDescription(SessionDescriptionInterface* desc, bool local) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000729 rtc::scoped_refptr<MockSetSessionDescriptionObserver>
730 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000731 MockSetSessionDescriptionObserver>());
732 if (local) {
733 pc_->SetLocalDescription(observer, desc);
734 } else {
735 pc_->SetRemoteDescription(observer, desc);
736 }
737 EXPECT_EQ_WAIT(true, observer->called(), kTimeout);
738 return observer->result();
739 }
740
741 bool DoSetLocalDescription(SessionDescriptionInterface* desc) {
742 return DoSetSessionDescription(desc, true);
743 }
744
745 bool DoSetRemoteDescription(SessionDescriptionInterface* desc) {
746 return DoSetSessionDescription(desc, false);
747 }
748
749 // Calls PeerConnection::GetStats and check the return value.
750 // It does not verify the values in the StatReports since a RTCP packet might
751 // be required.
752 bool DoGetStats(MediaStreamTrackInterface* track) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000753 rtc::scoped_refptr<MockStatsObserver> observer(
754 new rtc::RefCountedObject<MockStatsObserver>());
jiayl@webrtc.orgdb41b4d2014-03-03 21:30:06 +0000755 if (!pc_->GetStats(
756 observer, track, PeerConnectionInterface::kStatsOutputLevelStandard))
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000757 return false;
758 EXPECT_TRUE_WAIT(observer->called(), kTimeout);
759 return observer->called();
760 }
761
762 void InitiateCall() {
763 CreatePeerConnection();
764 // Create a local stream with audio&video tracks.
765 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
766 CreateOfferReceiveAnswer();
767 }
768
769 // Verify that RTP Header extensions has been negotiated for audio and video.
770 void VerifyRemoteRtpHeaderExtensions() {
771 const cricket::MediaContentDescription* desc =
772 cricket::GetFirstAudioContentDescription(
773 pc_->remote_description()->description());
774 ASSERT_TRUE(desc != NULL);
775 EXPECT_GT(desc->rtp_header_extensions().size(), 0u);
776
777 desc = cricket::GetFirstVideoContentDescription(
778 pc_->remote_description()->description());
779 ASSERT_TRUE(desc != NULL);
780 EXPECT_GT(desc->rtp_header_extensions().size(), 0u);
781 }
782
783 void CreateOfferAsRemoteDescription() {
kwibergd1fe2812016-04-27 06:47:29 -0700784 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -0700785 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000786 std::string sdp;
787 EXPECT_TRUE(offer->ToString(&sdp));
788 SessionDescriptionInterface* remote_offer =
789 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
790 sdp, NULL);
791 EXPECT_TRUE(DoSetRemoteDescription(remote_offer));
792 EXPECT_EQ(PeerConnectionInterface::kHaveRemoteOffer, observer_.state_);
793 }
794
deadbeefab9b2d12015-10-14 11:33:11 -0700795 void CreateAndSetRemoteOffer(const std::string& sdp) {
796 SessionDescriptionInterface* remote_offer =
797 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
798 sdp, nullptr);
799 EXPECT_TRUE(DoSetRemoteDescription(remote_offer));
800 EXPECT_EQ(PeerConnectionInterface::kHaveRemoteOffer, observer_.state_);
801 }
802
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000803 void CreateAnswerAsLocalDescription() {
kwibergd1fe2812016-04-27 06:47:29 -0700804 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -0700805 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000806
807 // TODO(perkj): Currently SetLocalDescription fails if any parameters in an
808 // audio codec change, even if the parameter has nothing to do with
809 // receiving. Not all parameters are serialized to SDP.
810 // Since CreatePrAnswerAsLocalDescription serialize/deserialize
811 // the SessionDescription, it is necessary to do that here to in order to
812 // get ReceiveOfferCreatePrAnswerAndAnswer and RenegotiateAudioOnly to pass.
813 // https://code.google.com/p/webrtc/issues/detail?id=1356
814 std::string sdp;
815 EXPECT_TRUE(answer->ToString(&sdp));
816 SessionDescriptionInterface* new_answer =
817 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
818 sdp, NULL);
819 EXPECT_TRUE(DoSetLocalDescription(new_answer));
820 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
821 }
822
823 void CreatePrAnswerAsLocalDescription() {
kwibergd1fe2812016-04-27 06:47:29 -0700824 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -0700825 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000826
827 std::string sdp;
828 EXPECT_TRUE(answer->ToString(&sdp));
829 SessionDescriptionInterface* pr_answer =
830 webrtc::CreateSessionDescription(SessionDescriptionInterface::kPrAnswer,
831 sdp, NULL);
832 EXPECT_TRUE(DoSetLocalDescription(pr_answer));
833 EXPECT_EQ(PeerConnectionInterface::kHaveLocalPrAnswer, observer_.state_);
834 }
835
836 void CreateOfferReceiveAnswer() {
837 CreateOfferAsLocalDescription();
838 std::string sdp;
839 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
840 CreateAnswerAsRemoteDescription(sdp);
841 }
842
843 void CreateOfferAsLocalDescription() {
kwibergd1fe2812016-04-27 06:47:29 -0700844 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -0700845 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000846 // TODO(perkj): Currently SetLocalDescription fails if any parameters in an
847 // audio codec change, even if the parameter has nothing to do with
848 // receiving. Not all parameters are serialized to SDP.
849 // Since CreatePrAnswerAsLocalDescription serialize/deserialize
850 // the SessionDescription, it is necessary to do that here to in order to
851 // get ReceiveOfferCreatePrAnswerAndAnswer and RenegotiateAudioOnly to pass.
852 // https://code.google.com/p/webrtc/issues/detail?id=1356
853 std::string sdp;
854 EXPECT_TRUE(offer->ToString(&sdp));
855 SessionDescriptionInterface* new_offer =
856 webrtc::CreateSessionDescription(
857 SessionDescriptionInterface::kOffer,
858 sdp, NULL);
859
860 EXPECT_TRUE(DoSetLocalDescription(new_offer));
861 EXPECT_EQ(PeerConnectionInterface::kHaveLocalOffer, observer_.state_);
mallinath@webrtc.org68cbd012014-01-22 00:16:46 +0000862 // Wait for the ice_complete message, so that SDP will have candidates.
863 EXPECT_TRUE_WAIT(observer_.ice_complete_, kTimeout);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000864 }
865
deadbeefab9b2d12015-10-14 11:33:11 -0700866 void CreateAnswerAsRemoteDescription(const std::string& sdp) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000867 webrtc::JsepSessionDescription* answer = new webrtc::JsepSessionDescription(
868 SessionDescriptionInterface::kAnswer);
deadbeefab9b2d12015-10-14 11:33:11 -0700869 EXPECT_TRUE(answer->Initialize(sdp, NULL));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000870 EXPECT_TRUE(DoSetRemoteDescription(answer));
871 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
872 }
873
deadbeefab9b2d12015-10-14 11:33:11 -0700874 void CreatePrAnswerAndAnswerAsRemoteDescription(const std::string& sdp) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000875 webrtc::JsepSessionDescription* pr_answer =
876 new webrtc::JsepSessionDescription(
877 SessionDescriptionInterface::kPrAnswer);
deadbeefab9b2d12015-10-14 11:33:11 -0700878 EXPECT_TRUE(pr_answer->Initialize(sdp, NULL));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000879 EXPECT_TRUE(DoSetRemoteDescription(pr_answer));
880 EXPECT_EQ(PeerConnectionInterface::kHaveRemotePrAnswer, observer_.state_);
881 webrtc::JsepSessionDescription* answer =
882 new webrtc::JsepSessionDescription(
883 SessionDescriptionInterface::kAnswer);
deadbeefab9b2d12015-10-14 11:33:11 -0700884 EXPECT_TRUE(answer->Initialize(sdp, NULL));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000885 EXPECT_TRUE(DoSetRemoteDescription(answer));
886 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
887 }
888
889 // Help function used for waiting until a the last signaled remote stream has
890 // the same label as |stream_label|. In a few of the tests in this file we
891 // answer with the same session description as we offer and thus we can
892 // check if OnAddStream have been called with the same stream as we offer to
893 // send.
894 void WaitAndVerifyOnAddStream(const std::string& stream_label) {
895 EXPECT_EQ_WAIT(stream_label, observer_.GetLastAddedStreamLabel(), kTimeout);
896 }
897
898 // Creates an offer and applies it as a local session description.
899 // Creates an answer with the same SDP an the offer but removes all lines
900 // that start with a:ssrc"
901 void CreateOfferReceiveAnswerWithoutSsrc() {
902 CreateOfferAsLocalDescription();
903 std::string sdp;
904 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
905 SetSsrcToZero(&sdp);
906 CreateAnswerAsRemoteDescription(sdp);
907 }
908
deadbeefab9b2d12015-10-14 11:33:11 -0700909 // This function creates a MediaStream with label kStreams[0] and
910 // |number_of_audio_tracks| and |number_of_video_tracks| tracks and the
911 // corresponding SessionDescriptionInterface. The SessionDescriptionInterface
kwiberg2bbff992016-03-16 11:03:04 -0700912 // is returned and the MediaStream is stored in
deadbeefab9b2d12015-10-14 11:33:11 -0700913 // |reference_collection_|
kwibergd1fe2812016-04-27 06:47:29 -0700914 std::unique_ptr<SessionDescriptionInterface>
kwiberg2bbff992016-03-16 11:03:04 -0700915 CreateSessionDescriptionAndReference(size_t number_of_audio_tracks,
916 size_t number_of_video_tracks) {
917 EXPECT_LE(number_of_audio_tracks, 2u);
918 EXPECT_LE(number_of_video_tracks, 2u);
deadbeefab9b2d12015-10-14 11:33:11 -0700919
920 reference_collection_ = StreamCollection::Create();
921 std::string sdp_ms1 = std::string(kSdpStringInit);
922
923 std::string mediastream_label = kStreams[0];
924
925 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream(
926 webrtc::MediaStream::Create(mediastream_label));
927 reference_collection_->AddStream(stream);
928
929 if (number_of_audio_tracks > 0) {
930 sdp_ms1 += std::string(kSdpStringAudio);
931 sdp_ms1 += std::string(kSdpStringMs1Audio0);
932 AddAudioTrack(kAudioTracks[0], stream);
933 }
934 if (number_of_audio_tracks > 1) {
935 sdp_ms1 += kSdpStringMs1Audio1;
936 AddAudioTrack(kAudioTracks[1], stream);
937 }
938
939 if (number_of_video_tracks > 0) {
940 sdp_ms1 += std::string(kSdpStringVideo);
941 sdp_ms1 += std::string(kSdpStringMs1Video0);
942 AddVideoTrack(kVideoTracks[0], stream);
943 }
944 if (number_of_video_tracks > 1) {
945 sdp_ms1 += kSdpStringMs1Video1;
946 AddVideoTrack(kVideoTracks[1], stream);
947 }
948
kwibergd1fe2812016-04-27 06:47:29 -0700949 return std::unique_ptr<SessionDescriptionInterface>(
kwiberg2bbff992016-03-16 11:03:04 -0700950 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
951 sdp_ms1, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -0700952 }
953
954 void AddAudioTrack(const std::string& track_id,
955 MediaStreamInterface* stream) {
956 rtc::scoped_refptr<webrtc::AudioTrackInterface> audio_track(
957 webrtc::AudioTrack::Create(track_id, nullptr));
958 ASSERT_TRUE(stream->AddTrack(audio_track));
959 }
960
961 void AddVideoTrack(const std::string& track_id,
962 MediaStreamInterface* stream) {
963 rtc::scoped_refptr<webrtc::VideoTrackInterface> video_track(
nisseaf510af2016-03-21 08:20:42 -0700964 webrtc::VideoTrack::Create(track_id,
965 webrtc::FakeVideoTrackSource::Create()));
deadbeefab9b2d12015-10-14 11:33:11 -0700966 ASSERT_TRUE(stream->AddTrack(video_track));
967 }
968
kwibergfd8be342016-05-14 19:44:11 -0700969 std::unique_ptr<SessionDescriptionInterface> CreateOfferWithOneAudioStream() {
zhihuang8f65cdf2016-05-06 18:40:30 -0700970 CreatePeerConnection();
971 AddVoiceStream(kStreamLabel1);
kwibergfd8be342016-05-14 19:44:11 -0700972 std::unique_ptr<SessionDescriptionInterface> offer;
zhihuang8f65cdf2016-05-06 18:40:30 -0700973 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
974 return offer;
975 }
976
kwibergfd8be342016-05-14 19:44:11 -0700977 std::unique_ptr<SessionDescriptionInterface>
zhihuang8f65cdf2016-05-06 18:40:30 -0700978 CreateAnswerWithOneAudioStream() {
kwibergfd8be342016-05-14 19:44:11 -0700979 std::unique_ptr<SessionDescriptionInterface> offer =
zhihuang8f65cdf2016-05-06 18:40:30 -0700980 CreateOfferWithOneAudioStream();
981 EXPECT_TRUE(DoSetRemoteDescription(offer.release()));
kwibergfd8be342016-05-14 19:44:11 -0700982 std::unique_ptr<SessionDescriptionInterface> answer;
zhihuang8f65cdf2016-05-06 18:40:30 -0700983 EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
984 return answer;
985 }
986
987 const std::string& GetFirstAudioStreamCname(
988 const SessionDescriptionInterface* desc) {
989 const cricket::ContentInfo* audio_content =
990 cricket::GetFirstAudioContent(desc->description());
991 const cricket::AudioContentDescription* audio_desc =
992 static_cast<const cricket::AudioContentDescription*>(
993 audio_content->description);
994 return audio_desc->streams()[0].cname;
995 }
996
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800997 cricket::FakePortAllocator* port_allocator_ = nullptr;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000998 scoped_refptr<webrtc::PeerConnectionFactoryInterface> pc_factory_;
999 scoped_refptr<PeerConnectionInterface> pc_;
1000 MockPeerConnectionObserver observer_;
deadbeefab9b2d12015-10-14 11:33:11 -07001001 rtc::scoped_refptr<StreamCollection> reference_collection_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001002};
1003
zhihuang8f65cdf2016-05-06 18:40:30 -07001004// Generate different CNAMEs when PeerConnections are created.
1005// The CNAMEs are expected to be generated randomly. It is possible
1006// that the test fails, though the possibility is very low.
1007TEST_F(PeerConnectionInterfaceTest, CnameGenerationInOffer) {
kwibergfd8be342016-05-14 19:44:11 -07001008 std::unique_ptr<SessionDescriptionInterface> offer1 =
zhihuang8f65cdf2016-05-06 18:40:30 -07001009 CreateOfferWithOneAudioStream();
kwibergfd8be342016-05-14 19:44:11 -07001010 std::unique_ptr<SessionDescriptionInterface> offer2 =
zhihuang8f65cdf2016-05-06 18:40:30 -07001011 CreateOfferWithOneAudioStream();
1012 EXPECT_NE(GetFirstAudioStreamCname(offer1.get()),
1013 GetFirstAudioStreamCname(offer2.get()));
1014}
1015
1016TEST_F(PeerConnectionInterfaceTest, CnameGenerationInAnswer) {
kwibergfd8be342016-05-14 19:44:11 -07001017 std::unique_ptr<SessionDescriptionInterface> answer1 =
zhihuang8f65cdf2016-05-06 18:40:30 -07001018 CreateAnswerWithOneAudioStream();
kwibergfd8be342016-05-14 19:44:11 -07001019 std::unique_ptr<SessionDescriptionInterface> answer2 =
zhihuang8f65cdf2016-05-06 18:40:30 -07001020 CreateAnswerWithOneAudioStream();
1021 EXPECT_NE(GetFirstAudioStreamCname(answer1.get()),
1022 GetFirstAudioStreamCname(answer2.get()));
1023}
1024
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001025TEST_F(PeerConnectionInterfaceTest,
1026 CreatePeerConnectionWithDifferentConfigurations) {
1027 CreatePeerConnectionWithDifferentConfigurations();
1028}
1029
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001030TEST_F(PeerConnectionInterfaceTest,
1031 CreatePeerConnectionWithDifferentIceTransportsTypes) {
1032 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kNone);
1033 EXPECT_EQ(cricket::CF_NONE, port_allocator_->candidate_filter());
1034 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kRelay);
1035 EXPECT_EQ(cricket::CF_RELAY, port_allocator_->candidate_filter());
1036 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kNoHost);
1037 EXPECT_EQ(cricket::CF_ALL & ~cricket::CF_HOST,
1038 port_allocator_->candidate_filter());
1039 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kAll);
1040 EXPECT_EQ(cricket::CF_ALL, port_allocator_->candidate_filter());
1041}
1042
1043// Test that when a PeerConnection is created with a nonzero candidate pool
1044// size, the pooled PortAllocatorSession is created with all the attributes
1045// in the RTCConfiguration.
1046TEST_F(PeerConnectionInterfaceTest, CreatePeerConnectionWithPooledCandidates) {
1047 PeerConnectionInterface::RTCConfiguration config;
1048 PeerConnectionInterface::IceServer server;
1049 server.uri = kStunAddressOnly;
1050 config.servers.push_back(server);
1051 config.type = PeerConnectionInterface::kRelay;
1052 config.disable_ipv6 = true;
1053 config.tcp_candidate_policy =
1054 PeerConnectionInterface::kTcpCandidatePolicyDisabled;
1055 config.ice_candidate_pool_size = 1;
1056 CreatePeerConnection(config, nullptr);
1057
1058 const cricket::FakePortAllocatorSession* session =
1059 static_cast<const cricket::FakePortAllocatorSession*>(
1060 port_allocator_->GetPooledSession());
1061 ASSERT_NE(nullptr, session);
1062 EXPECT_EQ(1UL, session->stun_servers().size());
1063 EXPECT_EQ(0U, session->flags() & cricket::PORTALLOCATOR_ENABLE_IPV6);
1064 EXPECT_LT(0U, session->flags() & cricket::PORTALLOCATOR_DISABLE_TCP);
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001065}
1066
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001067TEST_F(PeerConnectionInterfaceTest, AddStreams) {
1068 CreatePeerConnection();
deadbeefab9b2d12015-10-14 11:33:11 -07001069 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001070 AddVoiceStream(kStreamLabel2);
1071 ASSERT_EQ(2u, pc_->local_streams()->count());
1072
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001073 // Test we can add multiple local streams to one peerconnection.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001074 scoped_refptr<MediaStreamInterface> stream(
1075 pc_factory_->CreateLocalMediaStream(kStreamLabel3));
1076 scoped_refptr<AudioTrackInterface> audio_track(
1077 pc_factory_->CreateAudioTrack(
1078 kStreamLabel3, static_cast<AudioSourceInterface*>(NULL)));
1079 stream->AddTrack(audio_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +00001080 EXPECT_TRUE(pc_->AddStream(stream));
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001081 EXPECT_EQ(3u, pc_->local_streams()->count());
1082
1083 // Remove the third stream.
1084 pc_->RemoveStream(pc_->local_streams()->at(2));
1085 EXPECT_EQ(2u, pc_->local_streams()->count());
1086
1087 // Remove the second stream.
1088 pc_->RemoveStream(pc_->local_streams()->at(1));
1089 EXPECT_EQ(1u, pc_->local_streams()->count());
1090
1091 // Remove the first stream.
1092 pc_->RemoveStream(pc_->local_streams()->at(0));
1093 EXPECT_EQ(0u, pc_->local_streams()->count());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001094}
1095
deadbeefab9b2d12015-10-14 11:33:11 -07001096// Test that the created offer includes streams we added.
1097TEST_F(PeerConnectionInterfaceTest, AddedStreamsPresentInOffer) {
1098 CreatePeerConnection();
1099 AddAudioVideoStream(kStreamLabel1, "audio_track", "video_track");
kwibergd1fe2812016-04-27 06:47:29 -07001100 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001101 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07001102
1103 const cricket::ContentInfo* audio_content =
1104 cricket::GetFirstAudioContent(offer->description());
1105 const cricket::AudioContentDescription* audio_desc =
1106 static_cast<const cricket::AudioContentDescription*>(
1107 audio_content->description);
1108 EXPECT_TRUE(
1109 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
1110
1111 const cricket::ContentInfo* video_content =
1112 cricket::GetFirstVideoContent(offer->description());
1113 const cricket::VideoContentDescription* video_desc =
1114 static_cast<const cricket::VideoContentDescription*>(
1115 video_content->description);
1116 EXPECT_TRUE(
1117 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1118
1119 // Add another stream and ensure the offer includes both the old and new
1120 // streams.
1121 AddAudioVideoStream(kStreamLabel2, "audio_track2", "video_track2");
kwiberg2bbff992016-03-16 11:03:04 -07001122 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07001123
1124 audio_content = cricket::GetFirstAudioContent(offer->description());
1125 audio_desc = static_cast<const cricket::AudioContentDescription*>(
1126 audio_content->description);
1127 EXPECT_TRUE(
1128 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
1129 EXPECT_TRUE(
1130 ContainsTrack(audio_desc->streams(), kStreamLabel2, "audio_track2"));
1131
1132 video_content = cricket::GetFirstVideoContent(offer->description());
1133 video_desc = static_cast<const cricket::VideoContentDescription*>(
1134 video_content->description);
1135 EXPECT_TRUE(
1136 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1137 EXPECT_TRUE(
1138 ContainsTrack(video_desc->streams(), kStreamLabel2, "video_track2"));
1139}
1140
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001141TEST_F(PeerConnectionInterfaceTest, RemoveStream) {
1142 CreatePeerConnection();
deadbeefab9b2d12015-10-14 11:33:11 -07001143 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001144 ASSERT_EQ(1u, pc_->local_streams()->count());
1145 pc_->RemoveStream(pc_->local_streams()->at(0));
1146 EXPECT_EQ(0u, pc_->local_streams()->count());
1147}
1148
deadbeefe1f9d832016-01-14 15:35:42 -08001149// Test for AddTrack and RemoveTrack methods.
1150// Tests that the created offer includes tracks we added,
1151// and that the RtpSenders are created correctly.
1152// Also tests that RemoveTrack removes the tracks from subsequent offers.
1153TEST_F(PeerConnectionInterfaceTest, AddTrackRemoveTrack) {
1154 CreatePeerConnection();
1155 // Create a dummy stream, so tracks share a stream label.
1156 scoped_refptr<MediaStreamInterface> stream(
1157 pc_factory_->CreateLocalMediaStream(kStreamLabel1));
1158 std::vector<MediaStreamInterface*> stream_list;
1159 stream_list.push_back(stream.get());
1160 scoped_refptr<AudioTrackInterface> audio_track(
1161 pc_factory_->CreateAudioTrack("audio_track", nullptr));
nisseaf510af2016-03-21 08:20:42 -07001162 scoped_refptr<VideoTrackInterface> video_track(pc_factory_->CreateVideoTrack(
1163 "video_track",
1164 pc_factory_->CreateVideoSource(new cricket::FakeVideoCapturer())));
deadbeefe1f9d832016-01-14 15:35:42 -08001165 auto audio_sender = pc_->AddTrack(audio_track, stream_list);
1166 auto video_sender = pc_->AddTrack(video_track, stream_list);
1167 EXPECT_EQ(kStreamLabel1, audio_sender->stream_id());
1168 EXPECT_EQ("audio_track", audio_sender->id());
1169 EXPECT_EQ(audio_track, audio_sender->track());
1170 EXPECT_EQ(kStreamLabel1, video_sender->stream_id());
1171 EXPECT_EQ("video_track", video_sender->id());
1172 EXPECT_EQ(video_track, video_sender->track());
1173
1174 // Now create an offer and check for the senders.
kwibergd1fe2812016-04-27 06:47:29 -07001175 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001176 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefe1f9d832016-01-14 15:35:42 -08001177
1178 const cricket::ContentInfo* audio_content =
1179 cricket::GetFirstAudioContent(offer->description());
1180 const cricket::AudioContentDescription* audio_desc =
1181 static_cast<const cricket::AudioContentDescription*>(
1182 audio_content->description);
1183 EXPECT_TRUE(
1184 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
1185
1186 const cricket::ContentInfo* video_content =
1187 cricket::GetFirstVideoContent(offer->description());
1188 const cricket::VideoContentDescription* video_desc =
1189 static_cast<const cricket::VideoContentDescription*>(
1190 video_content->description);
1191 EXPECT_TRUE(
1192 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1193
1194 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
1195
1196 // Now try removing the tracks.
1197 EXPECT_TRUE(pc_->RemoveTrack(audio_sender));
1198 EXPECT_TRUE(pc_->RemoveTrack(video_sender));
1199
1200 // Create a new offer and ensure it doesn't contain the removed senders.
kwiberg2bbff992016-03-16 11:03:04 -07001201 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefe1f9d832016-01-14 15:35:42 -08001202
1203 audio_content = cricket::GetFirstAudioContent(offer->description());
1204 audio_desc = static_cast<const cricket::AudioContentDescription*>(
1205 audio_content->description);
1206 EXPECT_FALSE(
1207 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
1208
1209 video_content = cricket::GetFirstVideoContent(offer->description());
1210 video_desc = static_cast<const cricket::VideoContentDescription*>(
1211 video_content->description);
1212 EXPECT_FALSE(
1213 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1214
1215 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
1216
1217 // Calling RemoveTrack on a sender no longer attached to a PeerConnection
1218 // should return false.
1219 EXPECT_FALSE(pc_->RemoveTrack(audio_sender));
1220 EXPECT_FALSE(pc_->RemoveTrack(video_sender));
1221}
1222
1223// Test creating senders without a stream specified,
1224// expecting a random stream ID to be generated.
1225TEST_F(PeerConnectionInterfaceTest, AddTrackWithoutStream) {
1226 CreatePeerConnection();
1227 // Create a dummy stream, so tracks share a stream label.
1228 scoped_refptr<AudioTrackInterface> audio_track(
1229 pc_factory_->CreateAudioTrack("audio_track", nullptr));
nisseaf510af2016-03-21 08:20:42 -07001230 scoped_refptr<VideoTrackInterface> video_track(pc_factory_->CreateVideoTrack(
1231 "video_track",
1232 pc_factory_->CreateVideoSource(new cricket::FakeVideoCapturer())));
deadbeefe1f9d832016-01-14 15:35:42 -08001233 auto audio_sender =
1234 pc_->AddTrack(audio_track, std::vector<MediaStreamInterface*>());
1235 auto video_sender =
1236 pc_->AddTrack(video_track, std::vector<MediaStreamInterface*>());
1237 EXPECT_EQ("audio_track", audio_sender->id());
1238 EXPECT_EQ(audio_track, audio_sender->track());
1239 EXPECT_EQ("video_track", video_sender->id());
1240 EXPECT_EQ(video_track, video_sender->track());
1241 // If the ID is truly a random GUID, it should be infinitely unlikely they
1242 // will be the same.
1243 EXPECT_NE(video_sender->stream_id(), audio_sender->stream_id());
1244}
1245
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001246TEST_F(PeerConnectionInterfaceTest, CreateOfferReceiveAnswer) {
1247 InitiateCall();
1248 WaitAndVerifyOnAddStream(kStreamLabel1);
1249 VerifyRemoteRtpHeaderExtensions();
1250}
1251
1252TEST_F(PeerConnectionInterfaceTest, CreateOfferReceivePrAnswerAndAnswer) {
1253 CreatePeerConnection();
deadbeefab9b2d12015-10-14 11:33:11 -07001254 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001255 CreateOfferAsLocalDescription();
1256 std::string offer;
1257 EXPECT_TRUE(pc_->local_description()->ToString(&offer));
1258 CreatePrAnswerAndAnswerAsRemoteDescription(offer);
1259 WaitAndVerifyOnAddStream(kStreamLabel1);
1260}
1261
1262TEST_F(PeerConnectionInterfaceTest, ReceiveOfferCreateAnswer) {
1263 CreatePeerConnection();
deadbeefab9b2d12015-10-14 11:33:11 -07001264 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001265
1266 CreateOfferAsRemoteDescription();
1267 CreateAnswerAsLocalDescription();
1268
1269 WaitAndVerifyOnAddStream(kStreamLabel1);
1270}
1271
1272TEST_F(PeerConnectionInterfaceTest, ReceiveOfferCreatePrAnswerAndAnswer) {
1273 CreatePeerConnection();
deadbeefab9b2d12015-10-14 11:33:11 -07001274 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001275
1276 CreateOfferAsRemoteDescription();
1277 CreatePrAnswerAsLocalDescription();
1278 CreateAnswerAsLocalDescription();
1279
1280 WaitAndVerifyOnAddStream(kStreamLabel1);
1281}
1282
1283TEST_F(PeerConnectionInterfaceTest, Renegotiate) {
1284 InitiateCall();
1285 ASSERT_EQ(1u, pc_->remote_streams()->count());
1286 pc_->RemoveStream(pc_->local_streams()->at(0));
1287 CreateOfferReceiveAnswer();
1288 EXPECT_EQ(0u, pc_->remote_streams()->count());
deadbeefab9b2d12015-10-14 11:33:11 -07001289 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001290 CreateOfferReceiveAnswer();
1291}
1292
1293// Tests that after negotiating an audio only call, the respondent can perform a
1294// renegotiation that removes the audio stream.
1295TEST_F(PeerConnectionInterfaceTest, RenegotiateAudioOnly) {
1296 CreatePeerConnection();
1297 AddVoiceStream(kStreamLabel1);
1298 CreateOfferAsRemoteDescription();
1299 CreateAnswerAsLocalDescription();
1300
1301 ASSERT_EQ(1u, pc_->remote_streams()->count());
1302 pc_->RemoveStream(pc_->local_streams()->at(0));
1303 CreateOfferReceiveAnswer();
1304 EXPECT_EQ(0u, pc_->remote_streams()->count());
1305}
1306
1307// Test that candidates are generated and that we can parse our own candidates.
1308TEST_F(PeerConnectionInterfaceTest, IceCandidates) {
1309 CreatePeerConnection();
1310
1311 EXPECT_FALSE(pc_->AddIceCandidate(observer_.last_candidate_.get()));
1312 // SetRemoteDescription takes ownership of offer.
kwibergd1fe2812016-04-27 06:47:29 -07001313 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefab9b2d12015-10-14 11:33:11 -07001314 AddVideoStream(kStreamLabel1);
deadbeefc80741f2015-10-22 13:14:45 -07001315 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
kwiberg2bbff992016-03-16 11:03:04 -07001316 EXPECT_TRUE(DoSetRemoteDescription(offer.release()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001317
1318 // SetLocalDescription takes ownership of answer.
kwibergd1fe2812016-04-27 06:47:29 -07001319 std::unique_ptr<SessionDescriptionInterface> answer;
deadbeefc80741f2015-10-22 13:14:45 -07001320 EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
kwiberg2bbff992016-03-16 11:03:04 -07001321 EXPECT_TRUE(DoSetLocalDescription(answer.release()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001322
1323 EXPECT_TRUE_WAIT(observer_.last_candidate_.get() != NULL, kTimeout);
1324 EXPECT_TRUE_WAIT(observer_.ice_complete_, kTimeout);
1325
1326 EXPECT_TRUE(pc_->AddIceCandidate(observer_.last_candidate_.get()));
1327}
1328
deadbeefab9b2d12015-10-14 11:33:11 -07001329// Test that CreateOffer and CreateAnswer will fail if the track labels are
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001330// not unique.
1331TEST_F(PeerConnectionInterfaceTest, CreateOfferAnswerWithInvalidStream) {
1332 CreatePeerConnection();
1333 // Create a regular offer for the CreateAnswer test later.
kwibergd1fe2812016-04-27 06:47:29 -07001334 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefc80741f2015-10-22 13:14:45 -07001335 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
kwiberg2bbff992016-03-16 11:03:04 -07001336 EXPECT_TRUE(offer);
1337 offer.reset();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001338
1339 // Create a local stream with audio&video tracks having same label.
1340 AddAudioVideoStream(kStreamLabel1, "track_label", "track_label");
1341
1342 // Test CreateOffer
deadbeefc80741f2015-10-22 13:14:45 -07001343 EXPECT_FALSE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001344
1345 // Test CreateAnswer
kwibergd1fe2812016-04-27 06:47:29 -07001346 std::unique_ptr<SessionDescriptionInterface> answer;
deadbeefc80741f2015-10-22 13:14:45 -07001347 EXPECT_FALSE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001348}
1349
1350// Test that we will get different SSRCs for each tracks in the offer and answer
1351// we created.
1352TEST_F(PeerConnectionInterfaceTest, SsrcInOfferAnswer) {
1353 CreatePeerConnection();
1354 // Create a local stream with audio&video tracks having different labels.
1355 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
1356
1357 // Test CreateOffer
kwibergd1fe2812016-04-27 06:47:29 -07001358 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001359 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001360 int audio_ssrc = 0;
1361 int video_ssrc = 0;
1362 EXPECT_TRUE(GetFirstSsrc(GetFirstAudioContent(offer->description()),
1363 &audio_ssrc));
1364 EXPECT_TRUE(GetFirstSsrc(GetFirstVideoContent(offer->description()),
1365 &video_ssrc));
1366 EXPECT_NE(audio_ssrc, video_ssrc);
1367
1368 // Test CreateAnswer
1369 EXPECT_TRUE(DoSetRemoteDescription(offer.release()));
kwibergd1fe2812016-04-27 06:47:29 -07001370 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -07001371 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001372 audio_ssrc = 0;
1373 video_ssrc = 0;
1374 EXPECT_TRUE(GetFirstSsrc(GetFirstAudioContent(answer->description()),
1375 &audio_ssrc));
1376 EXPECT_TRUE(GetFirstSsrc(GetFirstVideoContent(answer->description()),
1377 &video_ssrc));
1378 EXPECT_NE(audio_ssrc, video_ssrc);
1379}
1380
deadbeefeb459812015-12-15 19:24:43 -08001381// Test that it's possible to call AddTrack on a MediaStream after adding
1382// the stream to a PeerConnection.
1383// TODO(deadbeef): Remove this test once this behavior is no longer supported.
1384TEST_F(PeerConnectionInterfaceTest, AddTrackAfterAddStream) {
1385 CreatePeerConnection();
1386 // Create audio stream and add to PeerConnection.
1387 AddVoiceStream(kStreamLabel1);
1388 MediaStreamInterface* stream = pc_->local_streams()->at(0);
1389
1390 // Add video track to the audio-only stream.
nisseaf510af2016-03-21 08:20:42 -07001391 scoped_refptr<VideoTrackInterface> video_track(pc_factory_->CreateVideoTrack(
1392 "video_label",
1393 pc_factory_->CreateVideoSource(new cricket::FakeVideoCapturer())));
deadbeefeb459812015-12-15 19:24:43 -08001394 stream->AddTrack(video_track.get());
1395
kwibergd1fe2812016-04-27 06:47:29 -07001396 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001397 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefeb459812015-12-15 19:24:43 -08001398
1399 const cricket::MediaContentDescription* video_desc =
1400 cricket::GetFirstVideoContentDescription(offer->description());
1401 EXPECT_TRUE(video_desc != nullptr);
1402}
1403
1404// Test that it's possible to call RemoveTrack on a MediaStream after adding
1405// the stream to a PeerConnection.
1406// TODO(deadbeef): Remove this test once this behavior is no longer supported.
1407TEST_F(PeerConnectionInterfaceTest, RemoveTrackAfterAddStream) {
1408 CreatePeerConnection();
1409 // Create audio/video stream and add to PeerConnection.
1410 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
1411 MediaStreamInterface* stream = pc_->local_streams()->at(0);
1412
1413 // Remove the video track.
1414 stream->RemoveTrack(stream->GetVideoTracks()[0]);
1415
kwibergd1fe2812016-04-27 06:47:29 -07001416 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001417 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefeb459812015-12-15 19:24:43 -08001418
1419 const cricket::MediaContentDescription* video_desc =
1420 cricket::GetFirstVideoContentDescription(offer->description());
1421 EXPECT_TRUE(video_desc == nullptr);
1422}
1423
deadbeefbd7d8f72015-12-18 16:58:44 -08001424// Test creating a sender with a stream ID, and ensure the ID is populated
1425// in the offer.
1426TEST_F(PeerConnectionInterfaceTest, CreateSenderWithStream) {
1427 CreatePeerConnection();
1428 pc_->CreateSender("video", kStreamLabel1);
1429
kwibergd1fe2812016-04-27 06:47:29 -07001430 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001431 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefbd7d8f72015-12-18 16:58:44 -08001432
1433 const cricket::MediaContentDescription* video_desc =
1434 cricket::GetFirstVideoContentDescription(offer->description());
1435 ASSERT_TRUE(video_desc != nullptr);
1436 ASSERT_EQ(1u, video_desc->streams().size());
1437 EXPECT_EQ(kStreamLabel1, video_desc->streams()[0].sync_label);
1438}
1439
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001440// Test that we can specify a certain track that we want statistics about.
1441TEST_F(PeerConnectionInterfaceTest, GetStatsForSpecificTrack) {
1442 InitiateCall();
1443 ASSERT_LT(0u, pc_->remote_streams()->count());
1444 ASSERT_LT(0u, pc_->remote_streams()->at(0)->GetAudioTracks().size());
1445 scoped_refptr<MediaStreamTrackInterface> remote_audio =
1446 pc_->remote_streams()->at(0)->GetAudioTracks()[0];
1447 EXPECT_TRUE(DoGetStats(remote_audio));
1448
1449 // Remove the stream. Since we are sending to our selves the local
1450 // and the remote stream is the same.
1451 pc_->RemoveStream(pc_->local_streams()->at(0));
1452 // Do a re-negotiation.
1453 CreateOfferReceiveAnswer();
1454
1455 ASSERT_EQ(0u, pc_->remote_streams()->count());
1456
1457 // Test that we still can get statistics for the old track. Even if it is not
1458 // sent any longer.
1459 EXPECT_TRUE(DoGetStats(remote_audio));
1460}
1461
1462// Test that we can get stats on a video track.
1463TEST_F(PeerConnectionInterfaceTest, GetStatsForVideoTrack) {
1464 InitiateCall();
1465 ASSERT_LT(0u, pc_->remote_streams()->count());
1466 ASSERT_LT(0u, pc_->remote_streams()->at(0)->GetVideoTracks().size());
1467 scoped_refptr<MediaStreamTrackInterface> remote_video =
1468 pc_->remote_streams()->at(0)->GetVideoTracks()[0];
1469 EXPECT_TRUE(DoGetStats(remote_video));
1470}
1471
1472// Test that we don't get statistics for an invalid track.
tommi@webrtc.org908f57e2014-07-21 11:44:39 +00001473// TODO(tommi): Fix this test. DoGetStats will return true
1474// for the unknown track (since GetStats is async), but no
1475// data is returned for the track.
1476TEST_F(PeerConnectionInterfaceTest, DISABLED_GetStatsForInvalidTrack) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001477 InitiateCall();
1478 scoped_refptr<AudioTrackInterface> unknown_audio_track(
1479 pc_factory_->CreateAudioTrack("unknown track", NULL));
1480 EXPECT_FALSE(DoGetStats(unknown_audio_track));
1481}
1482
1483// This test setup two RTP data channels in loop back.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001484TEST_F(PeerConnectionInterfaceTest, TestDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001485 FakeConstraints constraints;
1486 constraints.SetAllowRtpDataChannels();
1487 CreatePeerConnection(&constraints);
1488 scoped_refptr<DataChannelInterface> data1 =
1489 pc_->CreateDataChannel("test1", NULL);
1490 scoped_refptr<DataChannelInterface> data2 =
1491 pc_->CreateDataChannel("test2", NULL);
1492 ASSERT_TRUE(data1 != NULL);
kwibergd1fe2812016-04-27 06:47:29 -07001493 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001494 new MockDataChannelObserver(data1));
kwibergd1fe2812016-04-27 06:47:29 -07001495 std::unique_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001496 new MockDataChannelObserver(data2));
1497
1498 EXPECT_EQ(DataChannelInterface::kConnecting, data1->state());
1499 EXPECT_EQ(DataChannelInterface::kConnecting, data2->state());
1500 std::string data_to_send1 = "testing testing";
1501 std::string data_to_send2 = "testing something else";
1502 EXPECT_FALSE(data1->Send(DataBuffer(data_to_send1)));
1503
1504 CreateOfferReceiveAnswer();
1505 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1506 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
1507
1508 EXPECT_EQ(DataChannelInterface::kOpen, data1->state());
1509 EXPECT_EQ(DataChannelInterface::kOpen, data2->state());
1510 EXPECT_TRUE(data1->Send(DataBuffer(data_to_send1)));
1511 EXPECT_TRUE(data2->Send(DataBuffer(data_to_send2)));
1512
1513 EXPECT_EQ_WAIT(data_to_send1, observer1->last_message(), kTimeout);
1514 EXPECT_EQ_WAIT(data_to_send2, observer2->last_message(), kTimeout);
1515
1516 data1->Close();
1517 EXPECT_EQ(DataChannelInterface::kClosing, data1->state());
1518 CreateOfferReceiveAnswer();
1519 EXPECT_FALSE(observer1->IsOpen());
1520 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
1521 EXPECT_TRUE(observer2->IsOpen());
1522
1523 data_to_send2 = "testing something else again";
1524 EXPECT_TRUE(data2->Send(DataBuffer(data_to_send2)));
1525
1526 EXPECT_EQ_WAIT(data_to_send2, observer2->last_message(), kTimeout);
1527}
1528
1529// This test verifies that sendnig binary data over RTP data channels should
1530// fail.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001531TEST_F(PeerConnectionInterfaceTest, TestSendBinaryOnRtpDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001532 FakeConstraints constraints;
1533 constraints.SetAllowRtpDataChannels();
1534 CreatePeerConnection(&constraints);
1535 scoped_refptr<DataChannelInterface> data1 =
1536 pc_->CreateDataChannel("test1", NULL);
1537 scoped_refptr<DataChannelInterface> data2 =
1538 pc_->CreateDataChannel("test2", NULL);
1539 ASSERT_TRUE(data1 != NULL);
kwibergd1fe2812016-04-27 06:47:29 -07001540 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001541 new MockDataChannelObserver(data1));
kwibergd1fe2812016-04-27 06:47:29 -07001542 std::unique_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001543 new MockDataChannelObserver(data2));
1544
1545 EXPECT_EQ(DataChannelInterface::kConnecting, data1->state());
1546 EXPECT_EQ(DataChannelInterface::kConnecting, data2->state());
1547
1548 CreateOfferReceiveAnswer();
1549 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1550 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
1551
1552 EXPECT_EQ(DataChannelInterface::kOpen, data1->state());
1553 EXPECT_EQ(DataChannelInterface::kOpen, data2->state());
1554
jbaucheec21bd2016-03-20 06:15:43 -07001555 rtc::CopyOnWriteBuffer buffer("test", 4);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001556 EXPECT_FALSE(data1->Send(DataBuffer(buffer, true)));
1557}
1558
1559// This test setup a RTP data channels in loop back and test that a channel is
1560// opened even if the remote end answer with a zero SSRC.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001561TEST_F(PeerConnectionInterfaceTest, TestSendOnlyDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001562 FakeConstraints constraints;
1563 constraints.SetAllowRtpDataChannels();
1564 CreatePeerConnection(&constraints);
1565 scoped_refptr<DataChannelInterface> data1 =
1566 pc_->CreateDataChannel("test1", NULL);
kwibergd1fe2812016-04-27 06:47:29 -07001567 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001568 new MockDataChannelObserver(data1));
1569
1570 CreateOfferReceiveAnswerWithoutSsrc();
1571
1572 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1573
1574 data1->Close();
1575 EXPECT_EQ(DataChannelInterface::kClosing, data1->state());
1576 CreateOfferReceiveAnswerWithoutSsrc();
1577 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
1578 EXPECT_FALSE(observer1->IsOpen());
1579}
1580
1581// This test that if a data channel is added in an answer a receive only channel
1582// channel is created.
1583TEST_F(PeerConnectionInterfaceTest, TestReceiveOnlyDataChannel) {
1584 FakeConstraints constraints;
1585 constraints.SetAllowRtpDataChannels();
1586 CreatePeerConnection(&constraints);
1587
1588 std::string offer_label = "offer_channel";
1589 scoped_refptr<DataChannelInterface> offer_channel =
1590 pc_->CreateDataChannel(offer_label, NULL);
1591
1592 CreateOfferAsLocalDescription();
1593
1594 // Replace the data channel label in the offer and apply it as an answer.
1595 std::string receive_label = "answer_channel";
1596 std::string sdp;
1597 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001598 rtc::replace_substrs(offer_label.c_str(), offer_label.length(),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001599 receive_label.c_str(), receive_label.length(),
1600 &sdp);
1601 CreateAnswerAsRemoteDescription(sdp);
1602
1603 // Verify that a new incoming data channel has been created and that
1604 // it is open but can't we written to.
1605 ASSERT_TRUE(observer_.last_datachannel_ != NULL);
1606 DataChannelInterface* received_channel = observer_.last_datachannel_;
1607 EXPECT_EQ(DataChannelInterface::kConnecting, received_channel->state());
1608 EXPECT_EQ(receive_label, received_channel->label());
1609 EXPECT_FALSE(received_channel->Send(DataBuffer("something")));
1610
1611 // Verify that the channel we initially offered has been rejected.
1612 EXPECT_EQ(DataChannelInterface::kClosed, offer_channel->state());
1613
1614 // Do another offer / answer exchange and verify that the data channel is
1615 // opened.
1616 CreateOfferReceiveAnswer();
1617 EXPECT_EQ_WAIT(DataChannelInterface::kOpen, received_channel->state(),
1618 kTimeout);
1619}
1620
1621// This test that no data channel is returned if a reliable channel is
1622// requested.
1623// TODO(perkj): Remove this test once reliable channels are implemented.
1624TEST_F(PeerConnectionInterfaceTest, CreateReliableRtpDataChannelShouldFail) {
1625 FakeConstraints constraints;
1626 constraints.SetAllowRtpDataChannels();
1627 CreatePeerConnection(&constraints);
1628
1629 std::string label = "test";
1630 webrtc::DataChannelInit config;
1631 config.reliable = true;
1632 scoped_refptr<DataChannelInterface> channel =
1633 pc_->CreateDataChannel(label, &config);
1634 EXPECT_TRUE(channel == NULL);
1635}
1636
deadbeefab9b2d12015-10-14 11:33:11 -07001637// Verifies that duplicated label is not allowed for RTP data channel.
1638TEST_F(PeerConnectionInterfaceTest, RtpDuplicatedLabelNotAllowed) {
1639 FakeConstraints constraints;
1640 constraints.SetAllowRtpDataChannels();
1641 CreatePeerConnection(&constraints);
1642
1643 std::string label = "test";
1644 scoped_refptr<DataChannelInterface> channel =
1645 pc_->CreateDataChannel(label, nullptr);
1646 EXPECT_NE(channel, nullptr);
1647
1648 scoped_refptr<DataChannelInterface> dup_channel =
1649 pc_->CreateDataChannel(label, nullptr);
1650 EXPECT_EQ(dup_channel, nullptr);
1651}
1652
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001653// This tests that a SCTP data channel is returned using different
1654// DataChannelInit configurations.
1655TEST_F(PeerConnectionInterfaceTest, CreateSctpDataChannel) {
1656 FakeConstraints constraints;
1657 constraints.SetAllowDtlsSctpDataChannels();
1658 CreatePeerConnection(&constraints);
1659
1660 webrtc::DataChannelInit config;
1661
1662 scoped_refptr<DataChannelInterface> channel =
1663 pc_->CreateDataChannel("1", &config);
1664 EXPECT_TRUE(channel != NULL);
1665 EXPECT_TRUE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001666 EXPECT_TRUE(observer_.renegotiation_needed_);
1667 observer_.renegotiation_needed_ = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001668
1669 config.ordered = false;
1670 channel = pc_->CreateDataChannel("2", &config);
1671 EXPECT_TRUE(channel != NULL);
1672 EXPECT_TRUE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001673 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001674
1675 config.ordered = true;
1676 config.maxRetransmits = 0;
1677 channel = pc_->CreateDataChannel("3", &config);
1678 EXPECT_TRUE(channel != NULL);
1679 EXPECT_FALSE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001680 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001681
1682 config.maxRetransmits = -1;
1683 config.maxRetransmitTime = 0;
1684 channel = pc_->CreateDataChannel("4", &config);
1685 EXPECT_TRUE(channel != NULL);
1686 EXPECT_FALSE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001687 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001688}
1689
1690// This tests that no data channel is returned if both maxRetransmits and
1691// maxRetransmitTime are set for SCTP data channels.
1692TEST_F(PeerConnectionInterfaceTest,
1693 CreateSctpDataChannelShouldFailForInvalidConfig) {
1694 FakeConstraints constraints;
1695 constraints.SetAllowDtlsSctpDataChannels();
1696 CreatePeerConnection(&constraints);
1697
1698 std::string label = "test";
1699 webrtc::DataChannelInit config;
1700 config.maxRetransmits = 0;
1701 config.maxRetransmitTime = 0;
1702
1703 scoped_refptr<DataChannelInterface> channel =
1704 pc_->CreateDataChannel(label, &config);
1705 EXPECT_TRUE(channel == NULL);
1706}
1707
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001708// The test verifies that creating a SCTP data channel with an id already in use
1709// or out of range should fail.
1710TEST_F(PeerConnectionInterfaceTest,
1711 CreateSctpDataChannelWithInvalidIdShouldFail) {
1712 FakeConstraints constraints;
1713 constraints.SetAllowDtlsSctpDataChannels();
1714 CreatePeerConnection(&constraints);
1715
1716 webrtc::DataChannelInit config;
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001717 scoped_refptr<DataChannelInterface> channel;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001718
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001719 config.id = 1;
1720 channel = pc_->CreateDataChannel("1", &config);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001721 EXPECT_TRUE(channel != NULL);
1722 EXPECT_EQ(1, channel->id());
1723
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001724 channel = pc_->CreateDataChannel("x", &config);
1725 EXPECT_TRUE(channel == NULL);
1726
1727 config.id = cricket::kMaxSctpSid;
1728 channel = pc_->CreateDataChannel("max", &config);
1729 EXPECT_TRUE(channel != NULL);
1730 EXPECT_EQ(config.id, channel->id());
1731
1732 config.id = cricket::kMaxSctpSid + 1;
1733 channel = pc_->CreateDataChannel("x", &config);
1734 EXPECT_TRUE(channel == NULL);
1735}
1736
deadbeefab9b2d12015-10-14 11:33:11 -07001737// Verifies that duplicated label is allowed for SCTP data channel.
1738TEST_F(PeerConnectionInterfaceTest, SctpDuplicatedLabelAllowed) {
1739 FakeConstraints constraints;
1740 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
1741 true);
1742 CreatePeerConnection(&constraints);
1743
1744 std::string label = "test";
1745 scoped_refptr<DataChannelInterface> channel =
1746 pc_->CreateDataChannel(label, nullptr);
1747 EXPECT_NE(channel, nullptr);
1748
1749 scoped_refptr<DataChannelInterface> dup_channel =
1750 pc_->CreateDataChannel(label, nullptr);
1751 EXPECT_NE(dup_channel, nullptr);
1752}
1753
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001754// This test verifies that OnRenegotiationNeeded is fired for every new RTP
1755// DataChannel.
1756TEST_F(PeerConnectionInterfaceTest, RenegotiationNeededForNewRtpDataChannel) {
1757 FakeConstraints constraints;
1758 constraints.SetAllowRtpDataChannels();
1759 CreatePeerConnection(&constraints);
1760
1761 scoped_refptr<DataChannelInterface> dc1 =
1762 pc_->CreateDataChannel("test1", NULL);
1763 EXPECT_TRUE(observer_.renegotiation_needed_);
1764 observer_.renegotiation_needed_ = false;
1765
1766 scoped_refptr<DataChannelInterface> dc2 =
1767 pc_->CreateDataChannel("test2", NULL);
1768 EXPECT_TRUE(observer_.renegotiation_needed_);
1769}
1770
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001771// This test that a data channel closes when a PeerConnection is deleted/closed.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001772TEST_F(PeerConnectionInterfaceTest, DataChannelCloseWhenPeerConnectionClose) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001773 FakeConstraints constraints;
1774 constraints.SetAllowRtpDataChannels();
1775 CreatePeerConnection(&constraints);
1776
1777 scoped_refptr<DataChannelInterface> data1 =
1778 pc_->CreateDataChannel("test1", NULL);
1779 scoped_refptr<DataChannelInterface> data2 =
1780 pc_->CreateDataChannel("test2", NULL);
1781 ASSERT_TRUE(data1 != NULL);
kwibergd1fe2812016-04-27 06:47:29 -07001782 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001783 new MockDataChannelObserver(data1));
kwibergd1fe2812016-04-27 06:47:29 -07001784 std::unique_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001785 new MockDataChannelObserver(data2));
1786
1787 CreateOfferReceiveAnswer();
1788 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1789 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
1790
1791 ReleasePeerConnection();
1792 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
1793 EXPECT_EQ(DataChannelInterface::kClosed, data2->state());
1794}
1795
1796// This test that data channels can be rejected in an answer.
1797TEST_F(PeerConnectionInterfaceTest, TestRejectDataChannelInAnswer) {
1798 FakeConstraints constraints;
1799 constraints.SetAllowRtpDataChannels();
1800 CreatePeerConnection(&constraints);
1801
1802 scoped_refptr<DataChannelInterface> offer_channel(
1803 pc_->CreateDataChannel("offer_channel", NULL));
1804
1805 CreateOfferAsLocalDescription();
1806
1807 // Create an answer where the m-line for data channels are rejected.
1808 std::string sdp;
1809 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
1810 webrtc::JsepSessionDescription* answer = new webrtc::JsepSessionDescription(
1811 SessionDescriptionInterface::kAnswer);
1812 EXPECT_TRUE(answer->Initialize(sdp, NULL));
1813 cricket::ContentInfo* data_info =
1814 answer->description()->GetContentByName("data");
1815 data_info->rejected = true;
1816
1817 DoSetRemoteDescription(answer);
1818 EXPECT_EQ(DataChannelInterface::kClosed, offer_channel->state());
1819}
1820
1821// Test that we can create a session description from an SDP string from
1822// FireFox, use it as a remote session description, generate an answer and use
1823// the answer as a local description.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07001824TEST_F(PeerConnectionInterfaceTest, ReceiveFireFoxOffer) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001825 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001826 FakeConstraints constraints;
1827 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
1828 true);
1829 CreatePeerConnection(&constraints);
1830 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
1831 SessionDescriptionInterface* desc =
1832 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
jbauchfabe2c92015-07-16 13:43:14 -07001833 webrtc::kFireFoxSdpOffer, nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001834 EXPECT_TRUE(DoSetSessionDescription(desc, false));
1835 CreateAnswerAsLocalDescription();
1836 ASSERT_TRUE(pc_->local_description() != NULL);
1837 ASSERT_TRUE(pc_->remote_description() != NULL);
1838
1839 const cricket::ContentInfo* content =
1840 cricket::GetFirstAudioContent(pc_->local_description()->description());
1841 ASSERT_TRUE(content != NULL);
1842 EXPECT_FALSE(content->rejected);
1843
1844 content =
1845 cricket::GetFirstVideoContent(pc_->local_description()->description());
1846 ASSERT_TRUE(content != NULL);
1847 EXPECT_FALSE(content->rejected);
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00001848#ifdef HAVE_SCTP
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001849 content =
1850 cricket::GetFirstDataContent(pc_->local_description()->description());
1851 ASSERT_TRUE(content != NULL);
1852 EXPECT_TRUE(content->rejected);
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00001853#endif
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001854}
1855
1856// Test that we can create an audio only offer and receive an answer with a
1857// limited set of audio codecs and receive an updated offer with more audio
1858// codecs, where the added codecs are not supported.
1859TEST_F(PeerConnectionInterfaceTest, ReceiveUpdatedAudioOfferWithBadCodecs) {
1860 CreatePeerConnection();
1861 AddVoiceStream("audio_label");
1862 CreateOfferAsLocalDescription();
1863
1864 SessionDescriptionInterface* answer =
1865 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
jbauchfabe2c92015-07-16 13:43:14 -07001866 webrtc::kAudioSdp, nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001867 EXPECT_TRUE(DoSetSessionDescription(answer, false));
1868
1869 SessionDescriptionInterface* updated_offer =
1870 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
jbauchfabe2c92015-07-16 13:43:14 -07001871 webrtc::kAudioSdpWithUnsupportedCodecs,
1872 nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001873 EXPECT_TRUE(DoSetSessionDescription(updated_offer, false));
1874 CreateAnswerAsLocalDescription();
1875}
1876
deadbeefc80741f2015-10-22 13:14:45 -07001877// Test that if we're receiving (but not sending) a track, subsequent offers
1878// will have m-lines with a=recvonly.
1879TEST_F(PeerConnectionInterfaceTest, CreateSubsequentRecvOnlyOffer) {
1880 FakeConstraints constraints;
1881 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
1882 true);
1883 CreatePeerConnection(&constraints);
1884 CreateAndSetRemoteOffer(kSdpStringWithStream1);
1885 CreateAnswerAsLocalDescription();
1886
1887 // At this point we should be receiving stream 1, but not sending anything.
1888 // A new offer should be recvonly.
kwibergd1fe2812016-04-27 06:47:29 -07001889 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefc80741f2015-10-22 13:14:45 -07001890 DoCreateOffer(&offer, nullptr);
1891
1892 const cricket::ContentInfo* video_content =
1893 cricket::GetFirstVideoContent(offer->description());
1894 const cricket::VideoContentDescription* video_desc =
1895 static_cast<const cricket::VideoContentDescription*>(
1896 video_content->description);
1897 ASSERT_EQ(cricket::MD_RECVONLY, video_desc->direction());
1898
1899 const cricket::ContentInfo* audio_content =
1900 cricket::GetFirstAudioContent(offer->description());
1901 const cricket::AudioContentDescription* audio_desc =
1902 static_cast<const cricket::AudioContentDescription*>(
1903 audio_content->description);
1904 ASSERT_EQ(cricket::MD_RECVONLY, audio_desc->direction());
1905}
1906
1907// Test that if we're receiving (but not sending) a track, and the
1908// offerToReceiveVideo/offerToReceiveAudio constraints are explicitly set to
1909// false, the generated m-lines will be a=inactive.
1910TEST_F(PeerConnectionInterfaceTest, CreateSubsequentInactiveOffer) {
1911 FakeConstraints constraints;
1912 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
1913 true);
1914 CreatePeerConnection(&constraints);
1915 CreateAndSetRemoteOffer(kSdpStringWithStream1);
1916 CreateAnswerAsLocalDescription();
1917
1918 // At this point we should be receiving stream 1, but not sending anything.
1919 // A new offer would be recvonly, but we'll set the "no receive" constraints
1920 // to make it inactive.
kwibergd1fe2812016-04-27 06:47:29 -07001921 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefc80741f2015-10-22 13:14:45 -07001922 FakeConstraints offer_constraints;
1923 offer_constraints.AddMandatory(
1924 webrtc::MediaConstraintsInterface::kOfferToReceiveVideo, false);
1925 offer_constraints.AddMandatory(
1926 webrtc::MediaConstraintsInterface::kOfferToReceiveAudio, false);
1927 DoCreateOffer(&offer, &offer_constraints);
1928
1929 const cricket::ContentInfo* video_content =
1930 cricket::GetFirstVideoContent(offer->description());
1931 const cricket::VideoContentDescription* video_desc =
1932 static_cast<const cricket::VideoContentDescription*>(
1933 video_content->description);
1934 ASSERT_EQ(cricket::MD_INACTIVE, video_desc->direction());
1935
1936 const cricket::ContentInfo* audio_content =
1937 cricket::GetFirstAudioContent(offer->description());
1938 const cricket::AudioContentDescription* audio_desc =
1939 static_cast<const cricket::AudioContentDescription*>(
1940 audio_content->description);
1941 ASSERT_EQ(cricket::MD_INACTIVE, audio_desc->direction());
1942}
1943
deadbeef653b8e02015-11-11 12:55:10 -08001944// Test that we can use SetConfiguration to change the ICE servers of the
1945// PortAllocator.
1946TEST_F(PeerConnectionInterfaceTest, SetConfigurationChangesIceServers) {
1947 CreatePeerConnection();
1948
1949 PeerConnectionInterface::RTCConfiguration config;
1950 PeerConnectionInterface::IceServer server;
1951 server.uri = "stun:test_hostname";
1952 config.servers.push_back(server);
1953 EXPECT_TRUE(pc_->SetConfiguration(config));
1954
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08001955 EXPECT_EQ(1u, port_allocator_->stun_servers().size());
1956 EXPECT_EQ("test_hostname",
1957 port_allocator_->stun_servers().begin()->hostname());
deadbeef653b8e02015-11-11 12:55:10 -08001958}
1959
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001960TEST_F(PeerConnectionInterfaceTest, SetConfigurationChangesCandidateFilter) {
1961 CreatePeerConnection();
1962 PeerConnectionInterface::RTCConfiguration config;
1963 config.type = PeerConnectionInterface::kRelay;
1964 EXPECT_TRUE(pc_->SetConfiguration(config));
1965 EXPECT_EQ(cricket::CF_RELAY, port_allocator_->candidate_filter());
1966}
1967
1968// Test that when SetConfiguration changes both the pool size and other
1969// attributes, the pooled session is created with the updated attributes.
1970TEST_F(PeerConnectionInterfaceTest,
1971 SetConfigurationCreatesPooledSessionCorrectly) {
1972 CreatePeerConnection();
1973 PeerConnectionInterface::RTCConfiguration config;
1974 config.ice_candidate_pool_size = 1;
1975 PeerConnectionInterface::IceServer server;
1976 server.uri = kStunAddressOnly;
1977 config.servers.push_back(server);
1978 config.type = PeerConnectionInterface::kRelay;
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001979 EXPECT_TRUE(pc_->SetConfiguration(config));
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001980
1981 const cricket::FakePortAllocatorSession* session =
1982 static_cast<const cricket::FakePortAllocatorSession*>(
1983 port_allocator_->GetPooledSession());
1984 ASSERT_NE(nullptr, session);
1985 EXPECT_EQ(1UL, session->stun_servers().size());
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001986}
1987
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001988// Test that PeerConnection::Close changes the states to closed and all remote
1989// tracks change state to ended.
1990TEST_F(PeerConnectionInterfaceTest, CloseAndTestStreamsAndStates) {
1991 // Initialize a PeerConnection and negotiate local and remote session
1992 // description.
1993 InitiateCall();
1994 ASSERT_EQ(1u, pc_->local_streams()->count());
1995 ASSERT_EQ(1u, pc_->remote_streams()->count());
1996
1997 pc_->Close();
1998
1999 EXPECT_EQ(PeerConnectionInterface::kClosed, pc_->signaling_state());
2000 EXPECT_EQ(PeerConnectionInterface::kIceConnectionClosed,
2001 pc_->ice_connection_state());
2002 EXPECT_EQ(PeerConnectionInterface::kIceGatheringComplete,
2003 pc_->ice_gathering_state());
2004
2005 EXPECT_EQ(1u, pc_->local_streams()->count());
2006 EXPECT_EQ(1u, pc_->remote_streams()->count());
2007
2008 scoped_refptr<MediaStreamInterface> remote_stream =
2009 pc_->remote_streams()->at(0);
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002010 // Track state may be updated asynchronously.
perkjd61bf802016-03-24 03:16:19 -07002011 EXPECT_EQ_WAIT(MediaStreamTrackInterface::kEnded,
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002012 remote_stream->GetAudioTracks()[0]->state(), kTimeout);
2013 EXPECT_EQ_WAIT(MediaStreamTrackInterface::kEnded,
2014 remote_stream->GetVideoTracks()[0]->state(), kTimeout);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002015}
2016
2017// Test that PeerConnection methods fails gracefully after
2018// PeerConnection::Close has been called.
2019TEST_F(PeerConnectionInterfaceTest, CloseAndTestMethods) {
2020 CreatePeerConnection();
2021 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
2022 CreateOfferAsRemoteDescription();
2023 CreateAnswerAsLocalDescription();
2024
2025 ASSERT_EQ(1u, pc_->local_streams()->count());
2026 scoped_refptr<MediaStreamInterface> local_stream =
2027 pc_->local_streams()->at(0);
2028
2029 pc_->Close();
2030
2031 pc_->RemoveStream(local_stream);
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +00002032 EXPECT_FALSE(pc_->AddStream(local_stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002033
2034 ASSERT_FALSE(local_stream->GetAudioTracks().empty());
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002035 rtc::scoped_refptr<webrtc::DtmfSenderInterface> dtmf_sender(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002036 pc_->CreateDtmfSender(local_stream->GetAudioTracks()[0]));
wu@webrtc.org66037362013-08-13 00:09:35 +00002037 EXPECT_TRUE(NULL == dtmf_sender); // local stream has been removed.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002038
2039 EXPECT_TRUE(pc_->CreateDataChannel("test", NULL) == NULL);
2040
2041 EXPECT_TRUE(pc_->local_description() != NULL);
2042 EXPECT_TRUE(pc_->remote_description() != NULL);
2043
kwibergd1fe2812016-04-27 06:47:29 -07002044 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07002045 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
kwibergd1fe2812016-04-27 06:47:29 -07002046 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -07002047 EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002048
2049 std::string sdp;
2050 ASSERT_TRUE(pc_->remote_description()->ToString(&sdp));
2051 SessionDescriptionInterface* remote_offer =
2052 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
2053 sdp, NULL);
2054 EXPECT_FALSE(DoSetRemoteDescription(remote_offer));
2055
2056 ASSERT_TRUE(pc_->local_description()->ToString(&sdp));
2057 SessionDescriptionInterface* local_offer =
2058 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
2059 sdp, NULL);
2060 EXPECT_FALSE(DoSetLocalDescription(local_offer));
2061}
2062
2063// Test that GetStats can still be called after PeerConnection::Close.
2064TEST_F(PeerConnectionInterfaceTest, CloseAndGetStats) {
2065 InitiateCall();
2066 pc_->Close();
2067 DoGetStats(NULL);
2068}
deadbeefab9b2d12015-10-14 11:33:11 -07002069
2070// NOTE: The series of tests below come from what used to be
2071// mediastreamsignaling_unittest.cc, and are mostly aimed at testing that
2072// setting a remote or local description has the expected effects.
2073
2074// This test verifies that the remote MediaStreams corresponding to a received
2075// SDP string is created. In this test the two separate MediaStreams are
2076// signaled.
2077TEST_F(PeerConnectionInterfaceTest, UpdateRemoteStreams) {
2078 FakeConstraints constraints;
2079 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2080 true);
2081 CreatePeerConnection(&constraints);
2082 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2083
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002084 rtc::scoped_refptr<StreamCollection> reference(CreateStreamCollection(1, 1));
deadbeefab9b2d12015-10-14 11:33:11 -07002085 EXPECT_TRUE(
2086 CompareStreamCollections(observer_.remote_streams(), reference.get()));
2087 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2088 EXPECT_TRUE(remote_stream->GetVideoTracks()[0]->GetSource() != nullptr);
2089
2090 // Create a session description based on another SDP with another
2091 // MediaStream.
2092 CreateAndSetRemoteOffer(kSdpStringWithStream1And2);
2093
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002094 rtc::scoped_refptr<StreamCollection> reference2(CreateStreamCollection(2, 1));
deadbeefab9b2d12015-10-14 11:33:11 -07002095 EXPECT_TRUE(
2096 CompareStreamCollections(observer_.remote_streams(), reference2.get()));
2097}
2098
2099// This test verifies that when remote tracks are added/removed from SDP, the
2100// created remote streams are updated appropriately.
2101TEST_F(PeerConnectionInterfaceTest,
2102 AddRemoveTrackFromExistingRemoteMediaStream) {
2103 FakeConstraints constraints;
2104 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2105 true);
2106 CreatePeerConnection(&constraints);
kwibergd1fe2812016-04-27 06:47:29 -07002107 std::unique_ptr<SessionDescriptionInterface> desc_ms1 =
kwiberg2bbff992016-03-16 11:03:04 -07002108 CreateSessionDescriptionAndReference(1, 1);
deadbeefab9b2d12015-10-14 11:33:11 -07002109 EXPECT_TRUE(DoSetRemoteDescription(desc_ms1.release()));
2110 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
2111 reference_collection_));
2112
2113 // Add extra audio and video tracks to the same MediaStream.
kwibergd1fe2812016-04-27 06:47:29 -07002114 std::unique_ptr<SessionDescriptionInterface> desc_ms1_two_tracks =
kwiberg2bbff992016-03-16 11:03:04 -07002115 CreateSessionDescriptionAndReference(2, 2);
deadbeefab9b2d12015-10-14 11:33:11 -07002116 EXPECT_TRUE(DoSetRemoteDescription(desc_ms1_two_tracks.release()));
2117 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
2118 reference_collection_));
perkjd61bf802016-03-24 03:16:19 -07002119 scoped_refptr<AudioTrackInterface> audio_track2 =
2120 observer_.remote_streams()->at(0)->GetAudioTracks()[1];
2121 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, audio_track2->state());
2122 scoped_refptr<VideoTrackInterface> video_track2 =
2123 observer_.remote_streams()->at(0)->GetVideoTracks()[1];
2124 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, video_track2->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002125
2126 // Remove the extra audio and video tracks.
kwibergd1fe2812016-04-27 06:47:29 -07002127 std::unique_ptr<SessionDescriptionInterface> desc_ms2 =
kwiberg2bbff992016-03-16 11:03:04 -07002128 CreateSessionDescriptionAndReference(1, 1);
perkjd61bf802016-03-24 03:16:19 -07002129 MockTrackObserver audio_track_observer(audio_track2);
2130 MockTrackObserver video_track_observer(video_track2);
2131
2132 EXPECT_CALL(audio_track_observer, OnChanged()).Times(Exactly(1));
2133 EXPECT_CALL(video_track_observer, OnChanged()).Times(Exactly(1));
deadbeefab9b2d12015-10-14 11:33:11 -07002134 EXPECT_TRUE(DoSetRemoteDescription(desc_ms2.release()));
2135 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
2136 reference_collection_));
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002137 // Track state may be updated asynchronously.
perkjd61bf802016-03-24 03:16:19 -07002138 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002139 audio_track2->state(), kTimeout);
2140 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
2141 video_track2->state(), kTimeout);
deadbeefab9b2d12015-10-14 11:33:11 -07002142}
2143
2144// This tests that remote tracks are ended if a local session description is set
2145// that rejects the media content type.
2146TEST_F(PeerConnectionInterfaceTest, RejectMediaContent) {
2147 FakeConstraints constraints;
2148 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2149 true);
2150 CreatePeerConnection(&constraints);
2151 // First create and set a remote offer, then reject its video content in our
2152 // answer.
2153 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2154 ASSERT_EQ(1u, observer_.remote_streams()->count());
2155 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2156 ASSERT_EQ(1u, remote_stream->GetVideoTracks().size());
2157 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2158
2159 rtc::scoped_refptr<webrtc::VideoTrackInterface> remote_video =
2160 remote_stream->GetVideoTracks()[0];
2161 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, remote_video->state());
2162 rtc::scoped_refptr<webrtc::AudioTrackInterface> remote_audio =
2163 remote_stream->GetAudioTracks()[0];
2164 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, remote_audio->state());
2165
kwibergd1fe2812016-04-27 06:47:29 -07002166 std::unique_ptr<SessionDescriptionInterface> local_answer;
kwiberg2bbff992016-03-16 11:03:04 -07002167 EXPECT_TRUE(DoCreateAnswer(&local_answer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07002168 cricket::ContentInfo* video_info =
2169 local_answer->description()->GetContentByName("video");
2170 video_info->rejected = true;
2171 EXPECT_TRUE(DoSetLocalDescription(local_answer.release()));
2172 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kEnded, remote_video->state());
2173 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, remote_audio->state());
2174
2175 // Now create an offer where we reject both video and audio.
kwibergd1fe2812016-04-27 06:47:29 -07002176 std::unique_ptr<SessionDescriptionInterface> local_offer;
kwiberg2bbff992016-03-16 11:03:04 -07002177 EXPECT_TRUE(DoCreateOffer(&local_offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07002178 video_info = local_offer->description()->GetContentByName("video");
2179 ASSERT_TRUE(video_info != nullptr);
2180 video_info->rejected = true;
2181 cricket::ContentInfo* audio_info =
2182 local_offer->description()->GetContentByName("audio");
2183 ASSERT_TRUE(audio_info != nullptr);
2184 audio_info->rejected = true;
2185 EXPECT_TRUE(DoSetLocalDescription(local_offer.release()));
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002186 // Track state may be updated asynchronously.
perkjd61bf802016-03-24 03:16:19 -07002187 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002188 remote_audio->state(), kTimeout);
2189 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
2190 remote_video->state(), kTimeout);
deadbeefab9b2d12015-10-14 11:33:11 -07002191}
2192
2193// This tests that we won't crash if the remote track has been removed outside
2194// of PeerConnection and then PeerConnection tries to reject the track.
2195TEST_F(PeerConnectionInterfaceTest, RemoveTrackThenRejectMediaContent) {
2196 FakeConstraints constraints;
2197 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2198 true);
2199 CreatePeerConnection(&constraints);
2200 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2201 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2202 remote_stream->RemoveTrack(remote_stream->GetVideoTracks()[0]);
2203 remote_stream->RemoveTrack(remote_stream->GetAudioTracks()[0]);
2204
kwibergd1fe2812016-04-27 06:47:29 -07002205 std::unique_ptr<SessionDescriptionInterface> local_answer(
deadbeefab9b2d12015-10-14 11:33:11 -07002206 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
2207 kSdpStringWithStream1, nullptr));
2208 cricket::ContentInfo* video_info =
2209 local_answer->description()->GetContentByName("video");
2210 video_info->rejected = true;
2211 cricket::ContentInfo* audio_info =
2212 local_answer->description()->GetContentByName("audio");
2213 audio_info->rejected = true;
2214 EXPECT_TRUE(DoSetLocalDescription(local_answer.release()));
2215
2216 // No crash is a pass.
2217}
2218
deadbeef5e97fb52015-10-15 12:49:08 -07002219// This tests that if a recvonly remote description is set, no remote streams
2220// will be created, even if the description contains SSRCs/MSIDs.
2221// See: https://code.google.com/p/webrtc/issues/detail?id=5054
2222TEST_F(PeerConnectionInterfaceTest, RecvonlyDescriptionDoesntCreateStream) {
2223 FakeConstraints constraints;
2224 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2225 true);
2226 CreatePeerConnection(&constraints);
2227
2228 std::string recvonly_offer = kSdpStringWithStream1;
2229 rtc::replace_substrs(kSendrecv, strlen(kSendrecv), kRecvonly,
2230 strlen(kRecvonly), &recvonly_offer);
2231 CreateAndSetRemoteOffer(recvonly_offer);
2232
2233 EXPECT_EQ(0u, observer_.remote_streams()->count());
2234}
2235
deadbeefab9b2d12015-10-14 11:33:11 -07002236// This tests that a default MediaStream is created if a remote session
2237// description doesn't contain any streams and no MSID support.
2238// It also tests that the default stream is updated if a video m-line is added
2239// in a subsequent session description.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002240TEST_F(PeerConnectionInterfaceTest, SdpWithoutMsidCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002241 FakeConstraints constraints;
2242 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2243 true);
2244 CreatePeerConnection(&constraints);
2245 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2246
2247 ASSERT_EQ(1u, observer_.remote_streams()->count());
2248 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2249
2250 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2251 EXPECT_EQ(0u, remote_stream->GetVideoTracks().size());
2252 EXPECT_EQ("default", remote_stream->label());
2253
2254 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2255 ASSERT_EQ(1u, observer_.remote_streams()->count());
2256 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2257 EXPECT_EQ("defaulta0", remote_stream->GetAudioTracks()[0]->id());
deadbeef884f5852016-01-15 09:20:04 -08002258 EXPECT_EQ(MediaStreamTrackInterface::kLive,
2259 remote_stream->GetAudioTracks()[0]->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002260 ASSERT_EQ(1u, remote_stream->GetVideoTracks().size());
2261 EXPECT_EQ("defaultv0", remote_stream->GetVideoTracks()[0]->id());
deadbeef884f5852016-01-15 09:20:04 -08002262 EXPECT_EQ(MediaStreamTrackInterface::kLive,
2263 remote_stream->GetVideoTracks()[0]->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002264}
2265
2266// This tests that a default MediaStream is created if a remote session
2267// description doesn't contain any streams and media direction is send only.
2268TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002269 SendOnlySdpWithoutMsidCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002270 FakeConstraints constraints;
2271 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2272 true);
2273 CreatePeerConnection(&constraints);
2274 CreateAndSetRemoteOffer(kSdpStringSendOnlyWithoutStreams);
2275
2276 ASSERT_EQ(1u, observer_.remote_streams()->count());
2277 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2278
2279 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2280 EXPECT_EQ(1u, remote_stream->GetVideoTracks().size());
2281 EXPECT_EQ("default", remote_stream->label());
2282}
2283
2284// This tests that it won't crash when PeerConnection tries to remove
2285// a remote track that as already been removed from the MediaStream.
2286TEST_F(PeerConnectionInterfaceTest, RemoveAlreadyGoneRemoteStream) {
2287 FakeConstraints constraints;
2288 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2289 true);
2290 CreatePeerConnection(&constraints);
2291 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2292 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2293 remote_stream->RemoveTrack(remote_stream->GetAudioTracks()[0]);
2294 remote_stream->RemoveTrack(remote_stream->GetVideoTracks()[0]);
2295
2296 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2297
2298 // No crash is a pass.
2299}
2300
2301// This tests that a default MediaStream is created if the remote session
2302// description doesn't contain any streams and don't contain an indication if
2303// MSID is supported.
2304TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002305 SdpWithoutMsidAndStreamsCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002306 FakeConstraints constraints;
2307 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2308 true);
2309 CreatePeerConnection(&constraints);
2310 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2311
2312 ASSERT_EQ(1u, observer_.remote_streams()->count());
2313 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2314 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2315 EXPECT_EQ(1u, remote_stream->GetVideoTracks().size());
2316}
2317
2318// This tests that a default MediaStream is not created if the remote session
2319// description doesn't contain any streams but does support MSID.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002320TEST_F(PeerConnectionInterfaceTest, SdpWithMsidDontCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002321 FakeConstraints constraints;
2322 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2323 true);
2324 CreatePeerConnection(&constraints);
2325 CreateAndSetRemoteOffer(kSdpStringWithMsidWithoutStreams);
2326 EXPECT_EQ(0u, observer_.remote_streams()->count());
2327}
2328
deadbeefbda7e0b2015-12-08 17:13:40 -08002329// This tests that when setting a new description, the old default tracks are
2330// not destroyed and recreated.
2331// See: https://bugs.chromium.org/p/webrtc/issues/detail?id=5250
Stefan Holmer102362b2016-03-18 09:39:07 +01002332TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002333 DefaultTracksNotDestroyedAndRecreated) {
deadbeefbda7e0b2015-12-08 17:13:40 -08002334 FakeConstraints constraints;
2335 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2336 true);
2337 CreatePeerConnection(&constraints);
2338 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2339
2340 ASSERT_EQ(1u, observer_.remote_streams()->count());
2341 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2342 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2343
2344 // Set the track to "disabled", then set a new description and ensure the
2345 // track is still disabled, which ensures it hasn't been recreated.
2346 remote_stream->GetAudioTracks()[0]->set_enabled(false);
2347 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2348 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2349 EXPECT_FALSE(remote_stream->GetAudioTracks()[0]->enabled());
2350}
2351
deadbeefab9b2d12015-10-14 11:33:11 -07002352// This tests that a default MediaStream is not created if a remote session
2353// description is updated to not have any MediaStreams.
2354TEST_F(PeerConnectionInterfaceTest, VerifyDefaultStreamIsNotCreated) {
2355 FakeConstraints constraints;
2356 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2357 true);
2358 CreatePeerConnection(&constraints);
2359 CreateAndSetRemoteOffer(kSdpStringWithStream1);
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002360 rtc::scoped_refptr<StreamCollection> reference(CreateStreamCollection(1, 1));
deadbeefab9b2d12015-10-14 11:33:11 -07002361 EXPECT_TRUE(
2362 CompareStreamCollections(observer_.remote_streams(), reference.get()));
2363
2364 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2365 EXPECT_EQ(0u, observer_.remote_streams()->count());
2366}
2367
2368// This tests that an RtpSender is created when the local description is set
2369// after adding a local stream.
2370// TODO(deadbeef): This test and the one below it need to be updated when
2371// an RtpSender's lifetime isn't determined by when a local description is set.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002372TEST_F(PeerConnectionInterfaceTest, LocalDescriptionChanged) {
deadbeefab9b2d12015-10-14 11:33:11 -07002373 FakeConstraints constraints;
2374 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2375 true);
2376 CreatePeerConnection(&constraints);
deadbeefab9b2d12015-10-14 11:33:11 -07002377
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002378 // Create an offer with 1 stream with 2 tracks of each type.
2379 rtc::scoped_refptr<StreamCollection> stream_collection =
2380 CreateStreamCollection(1, 2);
2381 pc_->AddStream(stream_collection->at(0));
2382 std::unique_ptr<SessionDescriptionInterface> offer;
2383 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2384 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
deadbeefab9b2d12015-10-14 11:33:11 -07002385
deadbeefab9b2d12015-10-14 11:33:11 -07002386 auto senders = pc_->GetSenders();
2387 EXPECT_EQ(4u, senders.size());
2388 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2389 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2390 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[1]));
2391 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[1]));
2392
2393 // Remove an audio and video track.
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002394 pc_->RemoveStream(stream_collection->at(0));
2395 stream_collection = CreateStreamCollection(1, 1);
2396 pc_->AddStream(stream_collection->at(0));
2397 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2398 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
2399
deadbeefab9b2d12015-10-14 11:33:11 -07002400 senders = pc_->GetSenders();
2401 EXPECT_EQ(2u, senders.size());
2402 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2403 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2404 EXPECT_FALSE(ContainsSender(senders, kAudioTracks[1]));
2405 EXPECT_FALSE(ContainsSender(senders, kVideoTracks[1]));
2406}
2407
2408// This tests that an RtpSender is created when the local description is set
2409// before adding a local stream.
2410TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002411 AddLocalStreamAfterLocalDescriptionChanged) {
deadbeefab9b2d12015-10-14 11:33:11 -07002412 FakeConstraints constraints;
2413 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2414 true);
2415 CreatePeerConnection(&constraints);
deadbeefab9b2d12015-10-14 11:33:11 -07002416
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002417 rtc::scoped_refptr<StreamCollection> stream_collection =
2418 CreateStreamCollection(1, 2);
2419 // Add a stream to create the offer, but remove it afterwards.
2420 pc_->AddStream(stream_collection->at(0));
2421 std::unique_ptr<SessionDescriptionInterface> offer;
2422 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2423 pc_->RemoveStream(stream_collection->at(0));
deadbeefab9b2d12015-10-14 11:33:11 -07002424
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002425 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
deadbeefab9b2d12015-10-14 11:33:11 -07002426 auto senders = pc_->GetSenders();
2427 EXPECT_EQ(0u, senders.size());
2428
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002429 pc_->AddStream(stream_collection->at(0));
deadbeefab9b2d12015-10-14 11:33:11 -07002430 senders = pc_->GetSenders();
2431 EXPECT_EQ(4u, senders.size());
2432 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2433 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2434 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[1]));
2435 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[1]));
2436}
2437
2438// This tests that the expected behavior occurs if the SSRC on a local track is
2439// changed when SetLocalDescription is called.
2440TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002441 ChangeSsrcOnTrackInLocalSessionDescription) {
deadbeefab9b2d12015-10-14 11:33:11 -07002442 FakeConstraints constraints;
2443 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2444 true);
2445 CreatePeerConnection(&constraints);
deadbeefab9b2d12015-10-14 11:33:11 -07002446
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002447 rtc::scoped_refptr<StreamCollection> stream_collection =
2448 CreateStreamCollection(2, 1);
2449 pc_->AddStream(stream_collection->at(0));
2450 std::unique_ptr<SessionDescriptionInterface> offer;
2451 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2452 // Grab a copy of the offer before it gets passed into the PC.
2453 std::unique_ptr<JsepSessionDescription> modified_offer(
2454 new JsepSessionDescription(JsepSessionDescription::kOffer));
2455 modified_offer->Initialize(offer->description()->Copy(), offer->session_id(),
2456 offer->session_version());
2457 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
deadbeefab9b2d12015-10-14 11:33:11 -07002458
deadbeefab9b2d12015-10-14 11:33:11 -07002459 auto senders = pc_->GetSenders();
2460 EXPECT_EQ(2u, senders.size());
2461 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2462 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2463
2464 // Change the ssrc of the audio and video track.
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002465 cricket::MediaContentDescription* desc =
2466 cricket::GetFirstAudioContentDescription(modified_offer->description());
2467 ASSERT_TRUE(desc != NULL);
2468 for (StreamParams& stream : desc->mutable_streams()) {
2469 for (unsigned int& ssrc : stream.ssrcs) {
2470 ++ssrc;
2471 }
2472 }
deadbeefab9b2d12015-10-14 11:33:11 -07002473
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002474 desc =
2475 cricket::GetFirstVideoContentDescription(modified_offer->description());
2476 ASSERT_TRUE(desc != NULL);
2477 for (StreamParams& stream : desc->mutable_streams()) {
2478 for (unsigned int& ssrc : stream.ssrcs) {
2479 ++ssrc;
2480 }
2481 }
2482
2483 EXPECT_TRUE(DoSetLocalDescription(modified_offer.release()));
deadbeefab9b2d12015-10-14 11:33:11 -07002484 senders = pc_->GetSenders();
2485 EXPECT_EQ(2u, senders.size());
2486 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2487 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2488 // TODO(deadbeef): Once RtpSenders expose parameters, check that the SSRC
2489 // changed.
2490}
2491
2492// This tests that the expected behavior occurs if a new session description is
2493// set with the same tracks, but on a different MediaStream.
Stefan Holmer55d6e7c2016-03-17 16:26:40 +01002494TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002495 SignalSameTracksInSeparateMediaStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002496 FakeConstraints constraints;
2497 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2498 true);
2499 CreatePeerConnection(&constraints);
deadbeefab9b2d12015-10-14 11:33:11 -07002500
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002501 rtc::scoped_refptr<StreamCollection> stream_collection =
2502 CreateStreamCollection(2, 1);
2503 pc_->AddStream(stream_collection->at(0));
2504 std::unique_ptr<SessionDescriptionInterface> offer;
2505 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2506 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
deadbeefab9b2d12015-10-14 11:33:11 -07002507
deadbeefab9b2d12015-10-14 11:33:11 -07002508 auto senders = pc_->GetSenders();
2509 EXPECT_EQ(2u, senders.size());
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002510 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0], kStreams[0]));
2511 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0], kStreams[0]));
deadbeefab9b2d12015-10-14 11:33:11 -07002512
2513 // Add a new MediaStream but with the same tracks as in the first stream.
2514 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream_1(
2515 webrtc::MediaStream::Create(kStreams[1]));
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002516 stream_1->AddTrack(stream_collection->at(0)->GetVideoTracks()[0]);
2517 stream_1->AddTrack(stream_collection->at(0)->GetAudioTracks()[0]);
deadbeefab9b2d12015-10-14 11:33:11 -07002518 pc_->AddStream(stream_1);
2519
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002520 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2521 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
deadbeefab9b2d12015-10-14 11:33:11 -07002522
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002523 auto new_senders = pc_->GetSenders();
2524 // Should be the same senders as before, but with updated stream id.
2525 // Note that this behavior is subject to change in the future.
2526 // We may decide the PC should ignore existing tracks in AddStream.
2527 EXPECT_EQ(senders, new_senders);
2528 EXPECT_TRUE(ContainsSender(new_senders, kAudioTracks[0], kStreams[1]));
2529 EXPECT_TRUE(ContainsSender(new_senders, kVideoTracks[0], kStreams[1]));
deadbeefab9b2d12015-10-14 11:33:11 -07002530}
2531
nisse51542be2016-02-12 02:27:06 -08002532// The PeerConnectionMediaConfig tests below verify that configuration
2533// and constraints are propagated into the MediaConfig passed to
2534// CreateMediaController. These settings are intended for MediaChannel
2535// constructors, but that is not exercised by these unittest.
2536class PeerConnectionFactoryForTest : public webrtc::PeerConnectionFactory {
2537 public:
2538 webrtc::MediaControllerInterface* CreateMediaController(
2539 const cricket::MediaConfig& config) const override {
2540 create_media_controller_called_ = true;
2541 create_media_controller_config_ = config;
2542
2543 webrtc::MediaControllerInterface* mc =
2544 PeerConnectionFactory::CreateMediaController(config);
2545 EXPECT_TRUE(mc != nullptr);
2546 return mc;
2547 }
2548
2549 // Mutable, so they can be modified in the above const-declared method.
2550 mutable bool create_media_controller_called_ = false;
2551 mutable cricket::MediaConfig create_media_controller_config_;
2552};
2553
2554class PeerConnectionMediaConfigTest : public testing::Test {
2555 protected:
2556 void SetUp() override {
nisseaf510af2016-03-21 08:20:42 -07002557 pcf_ = new rtc::RefCountedObject<PeerConnectionFactoryForTest>();
nisse51542be2016-02-12 02:27:06 -08002558 pcf_->Initialize();
2559 }
2560 const cricket::MediaConfig& TestCreatePeerConnection(
2561 const PeerConnectionInterface::RTCConfiguration& config,
2562 const MediaConstraintsInterface *constraints) {
2563 pcf_->create_media_controller_called_ = false;
2564
2565 scoped_refptr<PeerConnectionInterface> pc(
2566 pcf_->CreatePeerConnection(config, constraints, nullptr, nullptr,
2567 &observer_));
2568 EXPECT_TRUE(pc.get());
2569 EXPECT_TRUE(pcf_->create_media_controller_called_);
2570 return pcf_->create_media_controller_config_;
2571 }
2572
2573 scoped_refptr<PeerConnectionFactoryForTest> pcf_;
2574 MockPeerConnectionObserver observer_;
2575};
2576
2577// This test verifies the default behaviour with no constraints and a
2578// default RTCConfiguration.
2579TEST_F(PeerConnectionMediaConfigTest, TestDefaults) {
2580 PeerConnectionInterface::RTCConfiguration config;
2581 FakeConstraints constraints;
2582
2583 const cricket::MediaConfig& media_config =
2584 TestCreatePeerConnection(config, &constraints);
2585
2586 EXPECT_FALSE(media_config.enable_dscp);
nisse0db023a2016-03-01 04:29:59 -08002587 EXPECT_TRUE(media_config.video.enable_cpu_overuse_detection);
2588 EXPECT_FALSE(media_config.video.disable_prerenderer_smoothing);
2589 EXPECT_FALSE(media_config.video.suspend_below_min_bitrate);
nisse51542be2016-02-12 02:27:06 -08002590}
2591
2592// This test verifies the DSCP constraint is recognized and passed to
2593// the CreateMediaController call.
2594TEST_F(PeerConnectionMediaConfigTest, TestDscpConstraintTrue) {
2595 PeerConnectionInterface::RTCConfiguration config;
2596 FakeConstraints constraints;
2597
2598 constraints.AddOptional(webrtc::MediaConstraintsInterface::kEnableDscp, true);
2599 const cricket::MediaConfig& media_config =
2600 TestCreatePeerConnection(config, &constraints);
2601
2602 EXPECT_TRUE(media_config.enable_dscp);
2603}
2604
2605// This test verifies the cpu overuse detection constraint is
2606// recognized and passed to the CreateMediaController call.
2607TEST_F(PeerConnectionMediaConfigTest, TestCpuOveruseConstraintFalse) {
2608 PeerConnectionInterface::RTCConfiguration config;
2609 FakeConstraints constraints;
2610
2611 constraints.AddOptional(
2612 webrtc::MediaConstraintsInterface::kCpuOveruseDetection, false);
2613 const cricket::MediaConfig media_config =
2614 TestCreatePeerConnection(config, &constraints);
2615
nisse0db023a2016-03-01 04:29:59 -08002616 EXPECT_FALSE(media_config.video.enable_cpu_overuse_detection);
nisse51542be2016-02-12 02:27:06 -08002617}
2618
2619// This test verifies that the disable_prerenderer_smoothing flag is
2620// propagated from RTCConfiguration to the CreateMediaController call.
2621TEST_F(PeerConnectionMediaConfigTest, TestDisablePrerendererSmoothingTrue) {
2622 PeerConnectionInterface::RTCConfiguration config;
2623 FakeConstraints constraints;
2624
Niels Möller71bdda02016-03-31 12:59:59 +02002625 config.set_prerenderer_smoothing(false);
nisse51542be2016-02-12 02:27:06 -08002626 const cricket::MediaConfig& media_config =
2627 TestCreatePeerConnection(config, &constraints);
2628
nisse0db023a2016-03-01 04:29:59 -08002629 EXPECT_TRUE(media_config.video.disable_prerenderer_smoothing);
2630}
2631
2632// This test verifies the suspend below min bitrate constraint is
2633// recognized and passed to the CreateMediaController call.
2634TEST_F(PeerConnectionMediaConfigTest,
2635 TestSuspendBelowMinBitrateConstraintTrue) {
2636 PeerConnectionInterface::RTCConfiguration config;
2637 FakeConstraints constraints;
2638
2639 constraints.AddOptional(
2640 webrtc::MediaConstraintsInterface::kEnableVideoSuspendBelowMinBitrate,
2641 true);
2642 const cricket::MediaConfig media_config =
2643 TestCreatePeerConnection(config, &constraints);
2644
2645 EXPECT_TRUE(media_config.video.suspend_below_min_bitrate);
nisse51542be2016-02-12 02:27:06 -08002646}
2647
deadbeefab9b2d12015-10-14 11:33:11 -07002648// The following tests verify that session options are created correctly.
deadbeefc80741f2015-10-22 13:14:45 -07002649// TODO(deadbeef): Convert these tests to be more end-to-end. Instead of
2650// "verify options are converted correctly", should be "pass options into
2651// CreateOffer and verify the correct offer is produced."
deadbeefab9b2d12015-10-14 11:33:11 -07002652
2653TEST(CreateSessionOptionsTest, GetOptionsForOfferWithInvalidAudioOption) {
2654 RTCOfferAnswerOptions rtc_options;
2655 rtc_options.offer_to_receive_audio = RTCOfferAnswerOptions::kUndefined - 1;
2656
2657 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08002658 EXPECT_FALSE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002659
2660 rtc_options.offer_to_receive_audio =
2661 RTCOfferAnswerOptions::kMaxOfferToReceiveMedia + 1;
htaaac2dea2016-03-10 13:35:55 -08002662 EXPECT_FALSE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002663}
2664
2665TEST(CreateSessionOptionsTest, GetOptionsForOfferWithInvalidVideoOption) {
2666 RTCOfferAnswerOptions rtc_options;
2667 rtc_options.offer_to_receive_video = RTCOfferAnswerOptions::kUndefined - 1;
2668
2669 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08002670 EXPECT_FALSE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002671
2672 rtc_options.offer_to_receive_video =
2673 RTCOfferAnswerOptions::kMaxOfferToReceiveMedia + 1;
htaaac2dea2016-03-10 13:35:55 -08002674 EXPECT_FALSE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002675}
2676
2677// Test that a MediaSessionOptions is created for an offer if
deadbeefc80741f2015-10-22 13:14:45 -07002678// OfferToReceiveAudio and OfferToReceiveVideo options are set.
deadbeefab9b2d12015-10-14 11:33:11 -07002679TEST(CreateSessionOptionsTest, GetMediaSessionOptionsForOfferWithAudioVideo) {
2680 RTCOfferAnswerOptions rtc_options;
2681 rtc_options.offer_to_receive_audio = 1;
2682 rtc_options.offer_to_receive_video = 1;
2683
2684 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08002685 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002686 EXPECT_TRUE(options.has_audio());
2687 EXPECT_TRUE(options.has_video());
2688 EXPECT_TRUE(options.bundle_enabled);
2689}
2690
2691// Test that a correct MediaSessionOptions is created for an offer if
deadbeefc80741f2015-10-22 13:14:45 -07002692// OfferToReceiveAudio is set.
deadbeefab9b2d12015-10-14 11:33:11 -07002693TEST(CreateSessionOptionsTest, GetMediaSessionOptionsForOfferWithAudio) {
2694 RTCOfferAnswerOptions rtc_options;
2695 rtc_options.offer_to_receive_audio = 1;
2696
2697 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08002698 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002699 EXPECT_TRUE(options.has_audio());
2700 EXPECT_FALSE(options.has_video());
2701 EXPECT_TRUE(options.bundle_enabled);
2702}
2703
2704// Test that a correct MediaSessionOptions is created for an offer if
deadbeefc80741f2015-10-22 13:14:45 -07002705// the default OfferOptions are used.
deadbeefab9b2d12015-10-14 11:33:11 -07002706TEST(CreateSessionOptionsTest, GetDefaultMediaSessionOptionsForOffer) {
2707 RTCOfferAnswerOptions rtc_options;
2708
2709 cricket::MediaSessionOptions options;
deadbeef0ed85b22016-02-23 17:24:52 -08002710 options.transport_options["audio"] = cricket::TransportOptions();
2711 options.transport_options["video"] = cricket::TransportOptions();
htaaac2dea2016-03-10 13:35:55 -08002712 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefc80741f2015-10-22 13:14:45 -07002713 EXPECT_TRUE(options.has_audio());
deadbeefab9b2d12015-10-14 11:33:11 -07002714 EXPECT_FALSE(options.has_video());
deadbeefc80741f2015-10-22 13:14:45 -07002715 EXPECT_TRUE(options.bundle_enabled);
deadbeefab9b2d12015-10-14 11:33:11 -07002716 EXPECT_TRUE(options.vad_enabled);
deadbeef0ed85b22016-02-23 17:24:52 -08002717 EXPECT_FALSE(options.transport_options["audio"].ice_restart);
2718 EXPECT_FALSE(options.transport_options["video"].ice_restart);
deadbeefab9b2d12015-10-14 11:33:11 -07002719}
2720
2721// Test that a correct MediaSessionOptions is created for an offer if
deadbeefc80741f2015-10-22 13:14:45 -07002722// OfferToReceiveVideo is set.
deadbeefab9b2d12015-10-14 11:33:11 -07002723TEST(CreateSessionOptionsTest, GetMediaSessionOptionsForOfferWithVideo) {
2724 RTCOfferAnswerOptions rtc_options;
2725 rtc_options.offer_to_receive_audio = 0;
2726 rtc_options.offer_to_receive_video = 1;
2727
2728 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08002729 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002730 EXPECT_FALSE(options.has_audio());
2731 EXPECT_TRUE(options.has_video());
2732 EXPECT_TRUE(options.bundle_enabled);
2733}
2734
2735// Test that a correct MediaSessionOptions is created for an offer if
2736// UseRtpMux is set to false.
2737TEST(CreateSessionOptionsTest,
2738 GetMediaSessionOptionsForOfferWithBundleDisabled) {
2739 RTCOfferAnswerOptions rtc_options;
2740 rtc_options.offer_to_receive_audio = 1;
2741 rtc_options.offer_to_receive_video = 1;
2742 rtc_options.use_rtp_mux = false;
2743
2744 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08002745 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002746 EXPECT_TRUE(options.has_audio());
2747 EXPECT_TRUE(options.has_video());
2748 EXPECT_FALSE(options.bundle_enabled);
2749}
2750
2751// Test that a correct MediaSessionOptions is created to restart ice if
2752// IceRestart is set. It also tests that subsequent MediaSessionOptions don't
Taylor Brandstetterf475d362016-01-08 15:35:57 -08002753// have |audio_transport_options.ice_restart| etc. set.
deadbeefab9b2d12015-10-14 11:33:11 -07002754TEST(CreateSessionOptionsTest, GetMediaSessionOptionsForOfferWithIceRestart) {
2755 RTCOfferAnswerOptions rtc_options;
2756 rtc_options.ice_restart = true;
2757
2758 cricket::MediaSessionOptions options;
deadbeef0ed85b22016-02-23 17:24:52 -08002759 options.transport_options["audio"] = cricket::TransportOptions();
2760 options.transport_options["video"] = cricket::TransportOptions();
htaaac2dea2016-03-10 13:35:55 -08002761 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeef0ed85b22016-02-23 17:24:52 -08002762 EXPECT_TRUE(options.transport_options["audio"].ice_restart);
2763 EXPECT_TRUE(options.transport_options["video"].ice_restart);
deadbeefab9b2d12015-10-14 11:33:11 -07002764
2765 rtc_options = RTCOfferAnswerOptions();
htaaac2dea2016-03-10 13:35:55 -08002766 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeef0ed85b22016-02-23 17:24:52 -08002767 EXPECT_FALSE(options.transport_options["audio"].ice_restart);
2768 EXPECT_FALSE(options.transport_options["video"].ice_restart);
deadbeefab9b2d12015-10-14 11:33:11 -07002769}
2770
2771// Test that the MediaConstraints in an answer don't affect if audio and video
2772// is offered in an offer but that if kOfferToReceiveAudio or
2773// kOfferToReceiveVideo constraints are true in an offer, the media type will be
2774// included in subsequent answers.
2775TEST(CreateSessionOptionsTest, MediaConstraintsInAnswer) {
2776 FakeConstraints answer_c;
2777 answer_c.SetMandatoryReceiveAudio(true);
2778 answer_c.SetMandatoryReceiveVideo(true);
2779
2780 cricket::MediaSessionOptions answer_options;
2781 EXPECT_TRUE(ParseConstraintsForAnswer(&answer_c, &answer_options));
2782 EXPECT_TRUE(answer_options.has_audio());
2783 EXPECT_TRUE(answer_options.has_video());
2784
deadbeefc80741f2015-10-22 13:14:45 -07002785 RTCOfferAnswerOptions rtc_offer_options;
deadbeefab9b2d12015-10-14 11:33:11 -07002786
2787 cricket::MediaSessionOptions offer_options;
htaaac2dea2016-03-10 13:35:55 -08002788 EXPECT_TRUE(
2789 ExtractMediaSessionOptions(rtc_offer_options, false, &offer_options));
deadbeefc80741f2015-10-22 13:14:45 -07002790 EXPECT_TRUE(offer_options.has_audio());
htaaac2dea2016-03-10 13:35:55 -08002791 EXPECT_TRUE(offer_options.has_video());
deadbeefab9b2d12015-10-14 11:33:11 -07002792
deadbeefc80741f2015-10-22 13:14:45 -07002793 RTCOfferAnswerOptions updated_rtc_offer_options;
2794 updated_rtc_offer_options.offer_to_receive_audio = 1;
2795 updated_rtc_offer_options.offer_to_receive_video = 1;
deadbeefab9b2d12015-10-14 11:33:11 -07002796
2797 cricket::MediaSessionOptions updated_offer_options;
htaaac2dea2016-03-10 13:35:55 -08002798 EXPECT_TRUE(ExtractMediaSessionOptions(updated_rtc_offer_options, false,
htaa2a49d92016-03-04 02:51:39 -08002799 &updated_offer_options));
deadbeefab9b2d12015-10-14 11:33:11 -07002800 EXPECT_TRUE(updated_offer_options.has_audio());
2801 EXPECT_TRUE(updated_offer_options.has_video());
2802
2803 // Since an offer has been created with both audio and video, subsequent
2804 // offers and answers should contain both audio and video.
2805 // Answers will only contain the media types that exist in the offer
2806 // regardless of the value of |updated_answer_options.has_audio| and
2807 // |updated_answer_options.has_video|.
2808 FakeConstraints updated_answer_c;
2809 answer_c.SetMandatoryReceiveAudio(false);
2810 answer_c.SetMandatoryReceiveVideo(false);
2811
2812 cricket::MediaSessionOptions updated_answer_options;
2813 EXPECT_TRUE(
2814 ParseConstraintsForAnswer(&updated_answer_c, &updated_answer_options));
2815 EXPECT_TRUE(updated_answer_options.has_audio());
2816 EXPECT_TRUE(updated_answer_options.has_video());
deadbeefab9b2d12015-10-14 11:33:11 -07002817}