blob: 6760a5464e15d935d3bf47f3dda6b0c9de5e4e0a [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
kjellanderb24317b2016-02-10 07:54:43 -08002 * Copyright 2012 The WebRTC project authors. All Rights Reserved.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003 *
kjellanderb24317b2016-02-10 07:54:43 -08004 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00009 */
10
kwibergd1fe2812016-04-27 06:47:29 -070011#include <memory>
henrike@webrtc.org28e20752013-07-10 00:45:36 +000012#include <string>
kwiberg0eb15ed2015-12-17 03:04:15 -080013#include <utility>
henrike@webrtc.org28e20752013-07-10 00:45:36 +000014
perkjd61bf802016-03-24 03:16:19 -070015#include "testing/gmock/include/gmock/gmock.h"
Henrik Kjellander15583c12016-02-10 10:53:12 +010016#include "webrtc/api/audiotrack.h"
17#include "webrtc/api/jsepsessiondescription.h"
18#include "webrtc/api/mediastream.h"
19#include "webrtc/api/mediastreaminterface.h"
20#include "webrtc/api/peerconnection.h"
21#include "webrtc/api/peerconnectioninterface.h"
22#include "webrtc/api/rtpreceiverinterface.h"
23#include "webrtc/api/rtpsenderinterface.h"
24#include "webrtc/api/streamcollection.h"
25#ifdef WEBRTC_ANDROID
26#include "webrtc/api/test/androidtestinitializer.h"
27#endif
28#include "webrtc/api/test/fakeconstraints.h"
29#include "webrtc/api/test/fakedtlsidentitystore.h"
nisseaf510af2016-03-21 08:20:42 -070030#include "webrtc/api/test/fakevideotracksource.h"
Henrik Kjellander15583c12016-02-10 10:53:12 +010031#include "webrtc/api/test/mockpeerconnectionobservers.h"
32#include "webrtc/api/test/testsdpstrings.h"
perkja3ede6c2016-03-08 01:27:48 +010033#include "webrtc/api/videocapturertracksource.h"
Henrik Kjellander15583c12016-02-10 10:53:12 +010034#include "webrtc/api/videotrack.h"
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000035#include "webrtc/base/gunit.h"
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000036#include "webrtc/base/ssladapter.h"
37#include "webrtc/base/sslstreamadapter.h"
38#include "webrtc/base/stringutils.h"
39#include "webrtc/base/thread.h"
kjellandera96e2d72016-02-04 23:52:28 -080040#include "webrtc/media/base/fakevideocapturer.h"
41#include "webrtc/media/sctp/sctpdataengine.h"
Taylor Brandstettera1c30352016-05-13 08:15:11 -070042#include "webrtc/p2p/base/fakeportallocator.h"
kjellander@webrtc.org9b8df252016-02-12 06:47:59 +010043#include "webrtc/pc/mediasession.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000044
45static const char kStreamLabel1[] = "local_stream_1";
46static const char kStreamLabel2[] = "local_stream_2";
47static const char kStreamLabel3[] = "local_stream_3";
48static const int kDefaultStunPort = 3478;
49static const char kStunAddressOnly[] = "stun:address";
50static const char kStunInvalidPort[] = "stun:address:-1";
51static const char kStunAddressPortAndMore1[] = "stun:address:port:more";
52static const char kStunAddressPortAndMore2[] = "stun:address:port more";
53static const char kTurnIceServerUri[] = "turn:user@turn.example.org";
54static const char kTurnUsername[] = "user";
55static const char kTurnPassword[] = "password";
56static const char kTurnHostname[] = "turn.example.org";
Peter Boström0c4e06b2015-10-07 12:23:21 +020057static const uint32_t kTimeout = 10000U;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000058
deadbeefab9b2d12015-10-14 11:33:11 -070059static const char kStreams[][8] = {"stream1", "stream2"};
60static const char kAudioTracks[][32] = {"audiotrack0", "audiotrack1"};
61static const char kVideoTracks[][32] = {"videotrack0", "videotrack1"};
62
deadbeef5e97fb52015-10-15 12:49:08 -070063static const char kRecvonly[] = "recvonly";
64static const char kSendrecv[] = "sendrecv";
65
deadbeefab9b2d12015-10-14 11:33:11 -070066// Reference SDP with a MediaStream with label "stream1" and audio track with
67// id "audio_1" and a video track with id "video_1;
68static const char kSdpStringWithStream1[] =
69 "v=0\r\n"
70 "o=- 0 0 IN IP4 127.0.0.1\r\n"
71 "s=-\r\n"
72 "t=0 0\r\n"
73 "a=ice-ufrag:e5785931\r\n"
74 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
75 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
76 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
77 "m=audio 1 RTP/AVPF 103\r\n"
78 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -070079 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -070080 "a=rtpmap:103 ISAC/16000\r\n"
81 "a=ssrc:1 cname:stream1\r\n"
82 "a=ssrc:1 mslabel:stream1\r\n"
83 "a=ssrc:1 label:audiotrack0\r\n"
84 "m=video 1 RTP/AVPF 120\r\n"
85 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -070086 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -070087 "a=rtpmap:120 VP8/90000\r\n"
88 "a=ssrc:2 cname:stream1\r\n"
89 "a=ssrc:2 mslabel:stream1\r\n"
90 "a=ssrc:2 label:videotrack0\r\n";
91
92// Reference SDP with two MediaStreams with label "stream1" and "stream2. Each
93// MediaStreams have one audio track and one video track.
94// This uses MSID.
95static const char kSdpStringWithStream1And2[] =
96 "v=0\r\n"
97 "o=- 0 0 IN IP4 127.0.0.1\r\n"
98 "s=-\r\n"
99 "t=0 0\r\n"
100 "a=ice-ufrag:e5785931\r\n"
101 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
102 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
103 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
104 "a=msid-semantic: WMS stream1 stream2\r\n"
105 "m=audio 1 RTP/AVPF 103\r\n"
106 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700107 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700108 "a=rtpmap:103 ISAC/16000\r\n"
109 "a=ssrc:1 cname:stream1\r\n"
110 "a=ssrc:1 msid:stream1 audiotrack0\r\n"
111 "a=ssrc:3 cname:stream2\r\n"
112 "a=ssrc:3 msid:stream2 audiotrack1\r\n"
113 "m=video 1 RTP/AVPF 120\r\n"
114 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700115 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700116 "a=rtpmap:120 VP8/0\r\n"
117 "a=ssrc:2 cname:stream1\r\n"
118 "a=ssrc:2 msid:stream1 videotrack0\r\n"
119 "a=ssrc:4 cname:stream2\r\n"
120 "a=ssrc:4 msid:stream2 videotrack1\r\n";
121
122// Reference SDP without MediaStreams. Msid is not supported.
123static const char kSdpStringWithoutStreams[] =
124 "v=0\r\n"
125 "o=- 0 0 IN IP4 127.0.0.1\r\n"
126 "s=-\r\n"
127 "t=0 0\r\n"
128 "a=ice-ufrag:e5785931\r\n"
129 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
130 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
131 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
132 "m=audio 1 RTP/AVPF 103\r\n"
133 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700134 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700135 "a=rtpmap:103 ISAC/16000\r\n"
136 "m=video 1 RTP/AVPF 120\r\n"
137 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700138 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700139 "a=rtpmap:120 VP8/90000\r\n";
140
141// Reference SDP without MediaStreams. Msid is supported.
142static const char kSdpStringWithMsidWithoutStreams[] =
143 "v=0\r\n"
144 "o=- 0 0 IN IP4 127.0.0.1\r\n"
145 "s=-\r\n"
146 "t=0 0\r\n"
147 "a=ice-ufrag:e5785931\r\n"
148 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
149 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
150 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
151 "a=msid-semantic: WMS\r\n"
152 "m=audio 1 RTP/AVPF 103\r\n"
153 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700154 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700155 "a=rtpmap:103 ISAC/16000\r\n"
156 "m=video 1 RTP/AVPF 120\r\n"
157 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700158 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700159 "a=rtpmap:120 VP8/90000\r\n";
160
161// Reference SDP without MediaStreams and audio only.
162static const char kSdpStringWithoutStreamsAudioOnly[] =
163 "v=0\r\n"
164 "o=- 0 0 IN IP4 127.0.0.1\r\n"
165 "s=-\r\n"
166 "t=0 0\r\n"
167 "a=ice-ufrag:e5785931\r\n"
168 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
169 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
170 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
171 "m=audio 1 RTP/AVPF 103\r\n"
172 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700173 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700174 "a=rtpmap:103 ISAC/16000\r\n";
175
176// Reference SENDONLY SDP without MediaStreams. Msid is not supported.
177static const char kSdpStringSendOnlyWithoutStreams[] =
178 "v=0\r\n"
179 "o=- 0 0 IN IP4 127.0.0.1\r\n"
180 "s=-\r\n"
181 "t=0 0\r\n"
182 "a=ice-ufrag:e5785931\r\n"
183 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
184 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
185 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
186 "m=audio 1 RTP/AVPF 103\r\n"
187 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700188 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700189 "a=sendonly\r\n"
190 "a=rtpmap:103 ISAC/16000\r\n"
191 "m=video 1 RTP/AVPF 120\r\n"
192 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700193 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700194 "a=sendonly\r\n"
195 "a=rtpmap:120 VP8/90000\r\n";
196
197static const char kSdpStringInit[] =
198 "v=0\r\n"
199 "o=- 0 0 IN IP4 127.0.0.1\r\n"
200 "s=-\r\n"
201 "t=0 0\r\n"
202 "a=ice-ufrag:e5785931\r\n"
203 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
204 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
205 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
206 "a=msid-semantic: WMS\r\n";
207
208static const char kSdpStringAudio[] =
209 "m=audio 1 RTP/AVPF 103\r\n"
210 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700211 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700212 "a=rtpmap:103 ISAC/16000\r\n";
213
214static const char kSdpStringVideo[] =
215 "m=video 1 RTP/AVPF 120\r\n"
216 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700217 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700218 "a=rtpmap:120 VP8/90000\r\n";
219
220static const char kSdpStringMs1Audio0[] =
221 "a=ssrc:1 cname:stream1\r\n"
222 "a=ssrc:1 msid:stream1 audiotrack0\r\n";
223
224static const char kSdpStringMs1Video0[] =
225 "a=ssrc:2 cname:stream1\r\n"
226 "a=ssrc:2 msid:stream1 videotrack0\r\n";
227
228static const char kSdpStringMs1Audio1[] =
229 "a=ssrc:3 cname:stream1\r\n"
230 "a=ssrc:3 msid:stream1 audiotrack1\r\n";
231
232static const char kSdpStringMs1Video1[] =
233 "a=ssrc:4 cname:stream1\r\n"
234 "a=ssrc:4 msid:stream1 videotrack1\r\n";
235
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000236#define MAYBE_SKIP_TEST(feature) \
237 if (!(feature())) { \
238 LOG(LS_INFO) << "Feature disabled... skipping"; \
239 return; \
240 }
241
perkjd61bf802016-03-24 03:16:19 -0700242using ::testing::Exactly;
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700243using cricket::StreamParams;
244using rtc::scoped_refptr;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000245using webrtc::AudioSourceInterface;
deadbeefab9b2d12015-10-14 11:33:11 -0700246using webrtc::AudioTrack;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000247using webrtc::AudioTrackInterface;
248using webrtc::DataBuffer;
249using webrtc::DataChannelInterface;
250using webrtc::FakeConstraints;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000251using webrtc::IceCandidateInterface;
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700252using webrtc::JsepSessionDescription;
deadbeefc80741f2015-10-22 13:14:45 -0700253using webrtc::MediaConstraintsInterface;
deadbeefab9b2d12015-10-14 11:33:11 -0700254using webrtc::MediaStream;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000255using webrtc::MediaStreamInterface;
256using webrtc::MediaStreamTrackInterface;
257using webrtc::MockCreateSessionDescriptionObserver;
258using webrtc::MockDataChannelObserver;
259using webrtc::MockSetSessionDescriptionObserver;
260using webrtc::MockStatsObserver;
perkjd61bf802016-03-24 03:16:19 -0700261using webrtc::NotifierInterface;
262using webrtc::ObserverInterface;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000263using webrtc::PeerConnectionInterface;
264using webrtc::PeerConnectionObserver;
deadbeefab9b2d12015-10-14 11:33:11 -0700265using webrtc::RtpReceiverInterface;
266using webrtc::RtpSenderInterface;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000267using webrtc::SdpParseError;
268using webrtc::SessionDescriptionInterface;
deadbeefab9b2d12015-10-14 11:33:11 -0700269using webrtc::StreamCollection;
270using webrtc::StreamCollectionInterface;
perkja3ede6c2016-03-08 01:27:48 +0100271using webrtc::VideoTrackSourceInterface;
deadbeefab9b2d12015-10-14 11:33:11 -0700272using webrtc::VideoTrack;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000273using webrtc::VideoTrackInterface;
274
deadbeefab9b2d12015-10-14 11:33:11 -0700275typedef PeerConnectionInterface::RTCOfferAnswerOptions RTCOfferAnswerOptions;
276
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000277namespace {
278
279// Gets the first ssrc of given content type from the ContentInfo.
280bool GetFirstSsrc(const cricket::ContentInfo* content_info, int* ssrc) {
281 if (!content_info || !ssrc) {
282 return false;
283 }
284 const cricket::MediaContentDescription* media_desc =
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000285 static_cast<const cricket::MediaContentDescription*>(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000286 content_info->description);
287 if (!media_desc || media_desc->streams().empty()) {
288 return false;
289 }
290 *ssrc = media_desc->streams().begin()->first_ssrc();
291 return true;
292}
293
294void SetSsrcToZero(std::string* sdp) {
295 const char kSdpSsrcAtribute[] = "a=ssrc:";
296 const char kSdpSsrcAtributeZero[] = "a=ssrc:0";
297 size_t ssrc_pos = 0;
298 while ((ssrc_pos = sdp->find(kSdpSsrcAtribute, ssrc_pos)) !=
299 std::string::npos) {
300 size_t end_ssrc = sdp->find(" ", ssrc_pos);
301 sdp->replace(ssrc_pos, end_ssrc - ssrc_pos, kSdpSsrcAtributeZero);
302 ssrc_pos = end_ssrc;
303 }
304}
305
deadbeefab9b2d12015-10-14 11:33:11 -0700306// Check if |streams| contains the specified track.
307bool ContainsTrack(const std::vector<cricket::StreamParams>& streams,
308 const std::string& stream_label,
309 const std::string& track_id) {
310 for (const cricket::StreamParams& params : streams) {
311 if (params.sync_label == stream_label && params.id == track_id) {
312 return true;
313 }
314 }
315 return false;
316}
317
318// Check if |senders| contains the specified sender, by id.
319bool ContainsSender(
320 const std::vector<rtc::scoped_refptr<RtpSenderInterface>>& senders,
321 const std::string& id) {
322 for (const auto& sender : senders) {
323 if (sender->id() == id) {
324 return true;
325 }
326 }
327 return false;
328}
329
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700330// Check if |senders| contains the specified sender, by id and stream id.
331bool ContainsSender(
332 const std::vector<rtc::scoped_refptr<RtpSenderInterface>>& senders,
333 const std::string& id,
334 const std::string& stream_id) {
335 for (const auto& sender : senders) {
336 if (sender->id() == id && sender->stream_id() == stream_id) {
337 return true;
338 }
339 }
340 return false;
341}
342
deadbeefab9b2d12015-10-14 11:33:11 -0700343// Create a collection of streams.
344// CreateStreamCollection(1) creates a collection that
345// correspond to kSdpStringWithStream1.
346// CreateStreamCollection(2) correspond to kSdpStringWithStream1And2.
347rtc::scoped_refptr<StreamCollection> CreateStreamCollection(
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700348 int number_of_streams,
349 int tracks_per_stream) {
deadbeefab9b2d12015-10-14 11:33:11 -0700350 rtc::scoped_refptr<StreamCollection> local_collection(
351 StreamCollection::Create());
352
353 for (int i = 0; i < number_of_streams; ++i) {
354 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream(
355 webrtc::MediaStream::Create(kStreams[i]));
356
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700357 for (int j = 0; j < tracks_per_stream; ++j) {
358 // Add a local audio track.
359 rtc::scoped_refptr<webrtc::AudioTrackInterface> audio_track(
360 webrtc::AudioTrack::Create(kAudioTracks[i * tracks_per_stream + j],
361 nullptr));
362 stream->AddTrack(audio_track);
deadbeefab9b2d12015-10-14 11:33:11 -0700363
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700364 // Add a local video track.
365 rtc::scoped_refptr<webrtc::VideoTrackInterface> video_track(
366 webrtc::VideoTrack::Create(kVideoTracks[i * tracks_per_stream + j],
367 webrtc::FakeVideoTrackSource::Create()));
368 stream->AddTrack(video_track);
369 }
deadbeefab9b2d12015-10-14 11:33:11 -0700370
371 local_collection->AddStream(stream);
372 }
373 return local_collection;
374}
375
376// Check equality of StreamCollections.
377bool CompareStreamCollections(StreamCollectionInterface* s1,
378 StreamCollectionInterface* s2) {
379 if (s1 == nullptr || s2 == nullptr || s1->count() != s2->count()) {
380 return false;
381 }
382
383 for (size_t i = 0; i != s1->count(); ++i) {
384 if (s1->at(i)->label() != s2->at(i)->label()) {
385 return false;
386 }
387 webrtc::AudioTrackVector audio_tracks1 = s1->at(i)->GetAudioTracks();
388 webrtc::AudioTrackVector audio_tracks2 = s2->at(i)->GetAudioTracks();
389 webrtc::VideoTrackVector video_tracks1 = s1->at(i)->GetVideoTracks();
390 webrtc::VideoTrackVector video_tracks2 = s2->at(i)->GetVideoTracks();
391
392 if (audio_tracks1.size() != audio_tracks2.size()) {
393 return false;
394 }
395 for (size_t j = 0; j != audio_tracks1.size(); ++j) {
396 if (audio_tracks1[j]->id() != audio_tracks2[j]->id()) {
397 return false;
398 }
399 }
400 if (video_tracks1.size() != video_tracks2.size()) {
401 return false;
402 }
403 for (size_t j = 0; j != video_tracks1.size(); ++j) {
404 if (video_tracks1[j]->id() != video_tracks2[j]->id()) {
405 return false;
406 }
407 }
408 }
409 return true;
410}
411
perkjd61bf802016-03-24 03:16:19 -0700412// Helper class to test Observer.
413class MockTrackObserver : public ObserverInterface {
414 public:
415 explicit MockTrackObserver(NotifierInterface* notifier)
416 : notifier_(notifier) {
417 notifier_->RegisterObserver(this);
418 }
419
420 ~MockTrackObserver() { Unregister(); }
421
422 void Unregister() {
423 if (notifier_) {
424 notifier_->UnregisterObserver(this);
425 notifier_ = nullptr;
426 }
427 }
428
429 MOCK_METHOD0(OnChanged, void());
430
431 private:
432 NotifierInterface* notifier_;
433};
434
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000435class MockPeerConnectionObserver : public PeerConnectionObserver {
436 public:
deadbeefab9b2d12015-10-14 11:33:11 -0700437 MockPeerConnectionObserver() : remote_streams_(StreamCollection::Create()) {}
Henrik Kjellander3fe372d2016-05-12 08:10:52 +0200438 virtual ~MockPeerConnectionObserver() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000439 }
440 void SetPeerConnectionInterface(PeerConnectionInterface* pc) {
441 pc_ = pc;
442 if (pc) {
443 state_ = pc_->signaling_state();
444 }
445 }
nisseef8b61e2016-04-29 06:09:15 -0700446 void OnSignalingChange(
447 PeerConnectionInterface::SignalingState new_state) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000448 EXPECT_EQ(pc_->signaling_state(), new_state);
449 state_ = new_state;
450 }
451 // TODO(bemasc): Remove this once callers transition to OnIceGatheringChange.
452 virtual void OnStateChange(StateType state_changed) {
453 if (pc_.get() == NULL)
454 return;
455 switch (state_changed) {
456 case kSignalingState:
457 // OnSignalingChange and OnStateChange(kSignalingState) should always
458 // be called approximately simultaneously. To ease testing, we require
459 // that they always be called in that order. This check verifies
460 // that OnSignalingChange has just been called.
461 EXPECT_EQ(pc_->signaling_state(), state_);
462 break;
463 case kIceState:
464 ADD_FAILURE();
465 break;
466 default:
467 ADD_FAILURE();
468 break;
469 }
470 }
deadbeefab9b2d12015-10-14 11:33:11 -0700471
472 MediaStreamInterface* RemoteStream(const std::string& label) {
473 return remote_streams_->find(label);
474 }
475 StreamCollectionInterface* remote_streams() const { return remote_streams_; }
Taylor Brandstetter98cde262016-05-31 13:02:21 -0700476 void OnAddStream(rtc::scoped_refptr<MediaStreamInterface> stream) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000477 last_added_stream_ = stream;
deadbeefab9b2d12015-10-14 11:33:11 -0700478 remote_streams_->AddStream(stream);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000479 }
Taylor Brandstetter98cde262016-05-31 13:02:21 -0700480 void OnRemoveStream(
481 rtc::scoped_refptr<MediaStreamInterface> stream) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000482 last_removed_stream_ = stream;
deadbeefab9b2d12015-10-14 11:33:11 -0700483 remote_streams_->RemoveStream(stream);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000484 }
perkjdfb769d2016-02-09 03:09:43 -0800485 void OnRenegotiationNeeded() override { renegotiation_needed_ = true; }
Taylor Brandstetter98cde262016-05-31 13:02:21 -0700486 void OnDataChannel(
487 rtc::scoped_refptr<DataChannelInterface> data_channel) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000488 last_datachannel_ = data_channel;
489 }
490
perkjdfb769d2016-02-09 03:09:43 -0800491 void OnIceConnectionChange(
492 PeerConnectionInterface::IceConnectionState new_state) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000493 EXPECT_EQ(pc_->ice_connection_state(), new_state);
494 }
perkjdfb769d2016-02-09 03:09:43 -0800495 void OnIceGatheringChange(
496 PeerConnectionInterface::IceGatheringState new_state) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000497 EXPECT_EQ(pc_->ice_gathering_state(), new_state);
perkjdfb769d2016-02-09 03:09:43 -0800498 ice_complete_ = new_state == PeerConnectionInterface::kIceGatheringComplete;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000499 }
perkjdfb769d2016-02-09 03:09:43 -0800500 void OnIceCandidate(const webrtc::IceCandidateInterface* candidate) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000501 EXPECT_NE(PeerConnectionInterface::kIceGatheringNew,
502 pc_->ice_gathering_state());
503
504 std::string sdp;
505 EXPECT_TRUE(candidate->ToString(&sdp));
506 EXPECT_LT(0u, sdp.size());
507 last_candidate_.reset(webrtc::CreateIceCandidate(candidate->sdp_mid(),
508 candidate->sdp_mline_index(), sdp, NULL));
509 EXPECT_TRUE(last_candidate_.get() != NULL);
510 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000511
512 // Returns the label of the last added stream.
513 // Empty string if no stream have been added.
514 std::string GetLastAddedStreamLabel() {
515 if (last_added_stream_.get())
516 return last_added_stream_->label();
517 return "";
518 }
519 std::string GetLastRemovedStreamLabel() {
520 if (last_removed_stream_.get())
521 return last_removed_stream_->label();
522 return "";
523 }
524
525 scoped_refptr<PeerConnectionInterface> pc_;
526 PeerConnectionInterface::SignalingState state_;
kwibergd1fe2812016-04-27 06:47:29 -0700527 std::unique_ptr<IceCandidateInterface> last_candidate_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000528 scoped_refptr<DataChannelInterface> last_datachannel_;
deadbeefab9b2d12015-10-14 11:33:11 -0700529 rtc::scoped_refptr<StreamCollection> remote_streams_;
530 bool renegotiation_needed_ = false;
531 bool ice_complete_ = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000532
533 private:
534 scoped_refptr<MediaStreamInterface> last_added_stream_;
535 scoped_refptr<MediaStreamInterface> last_removed_stream_;
536};
537
538} // namespace
deadbeefab9b2d12015-10-14 11:33:11 -0700539
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000540class PeerConnectionInterfaceTest : public testing::Test {
541 protected:
phoglund37ebcf02016-01-08 05:04:57 -0800542 PeerConnectionInterfaceTest() {
543#ifdef WEBRTC_ANDROID
544 webrtc::InitializeAndroidObjects();
545#endif
546 }
547
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000548 virtual void SetUp() {
549 pc_factory_ = webrtc::CreatePeerConnectionFactory(
danilchape9021a32016-05-17 01:52:02 -0700550 rtc::Thread::Current(), rtc::Thread::Current(), rtc::Thread::Current(),
551 nullptr, nullptr, nullptr);
552 ASSERT_TRUE(pc_factory_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000553 }
554
555 void CreatePeerConnection() {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700556 CreatePeerConnection(PeerConnectionInterface::RTCConfiguration(), nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000557 }
558
559 void CreatePeerConnection(webrtc::MediaConstraintsInterface* constraints) {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700560 CreatePeerConnection(PeerConnectionInterface::RTCConfiguration(),
561 constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000562 }
563
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700564 void CreatePeerConnectionWithIceTransportsType(
565 PeerConnectionInterface::IceTransportsType type) {
566 PeerConnectionInterface::RTCConfiguration config;
567 config.type = type;
568 return CreatePeerConnection(config, nullptr);
569 }
570
571 void CreatePeerConnectionWithIceServer(const std::string& uri,
572 const std::string& password) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800573 PeerConnectionInterface::RTCConfiguration config;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000574 PeerConnectionInterface::IceServer server;
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700575 server.uri = uri;
576 server.password = password;
577 config.servers.push_back(server);
578 CreatePeerConnection(config, nullptr);
579 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000580
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700581 void CreatePeerConnection(PeerConnectionInterface::RTCConfiguration config,
582 webrtc::MediaConstraintsInterface* constraints) {
kwibergd1fe2812016-04-27 06:47:29 -0700583 std::unique_ptr<cricket::FakePortAllocator> port_allocator(
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800584 new cricket::FakePortAllocator(rtc::Thread::Current(), nullptr));
585 port_allocator_ = port_allocator.get();
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000586
buildbot@webrtc.org61c1b8e2014-04-09 06:06:38 +0000587 // DTLS does not work in a loopback call, so is disabled for most of the
588 // tests in this file. We only create a FakeIdentityService if the test
589 // explicitly sets the constraint.
jiayl@webrtc.org61e00b02015-03-04 22:17:38 +0000590 FakeConstraints default_constraints;
591 if (!constraints) {
592 constraints = &default_constraints;
593
594 default_constraints.AddMandatory(
595 webrtc::MediaConstraintsInterface::kEnableDtlsSrtp, false);
596 }
597
kwibergd1fe2812016-04-27 06:47:29 -0700598 std::unique_ptr<webrtc::DtlsIdentityStoreInterface> dtls_identity_store;
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000599 bool dtls;
600 if (FindConstraint(constraints,
601 webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
602 &dtls,
Henrik Boström5e56c592015-08-11 10:33:13 +0200603 nullptr) && dtls) {
604 dtls_identity_store.reset(new FakeDtlsIdentityStore());
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000605 }
kwiberg0eb15ed2015-12-17 03:04:15 -0800606 pc_ = pc_factory_->CreatePeerConnection(
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800607 config, constraints, std::move(port_allocator),
kwiberg0eb15ed2015-12-17 03:04:15 -0800608 std::move(dtls_identity_store), &observer_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000609 ASSERT_TRUE(pc_.get() != NULL);
610 observer_.SetPeerConnectionInterface(pc_.get());
611 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
612 }
613
deadbeef0a6c4ca2015-10-06 11:38:28 -0700614 void CreatePeerConnectionExpectFail(const std::string& uri) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800615 PeerConnectionInterface::RTCConfiguration config;
deadbeef0a6c4ca2015-10-06 11:38:28 -0700616 PeerConnectionInterface::IceServer server;
deadbeef0a6c4ca2015-10-06 11:38:28 -0700617 server.uri = uri;
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800618 config.servers.push_back(server);
deadbeef0a6c4ca2015-10-06 11:38:28 -0700619
deadbeef0a6c4ca2015-10-06 11:38:28 -0700620 scoped_refptr<PeerConnectionInterface> pc;
hbosd7973cc2016-05-27 06:08:53 -0700621 pc = pc_factory_->CreatePeerConnection(config, nullptr, nullptr, nullptr,
622 &observer_);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800623 EXPECT_EQ(nullptr, pc);
deadbeef0a6c4ca2015-10-06 11:38:28 -0700624 }
625
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000626 void CreatePeerConnectionWithDifferentConfigurations() {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700627 CreatePeerConnectionWithIceServer(kStunAddressOnly, "");
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800628 EXPECT_EQ(1u, port_allocator_->stun_servers().size());
629 EXPECT_EQ(0u, port_allocator_->turn_servers().size());
630 EXPECT_EQ("address", port_allocator_->stun_servers().begin()->hostname());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000631 EXPECT_EQ(kDefaultStunPort,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800632 port_allocator_->stun_servers().begin()->port());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000633
deadbeef0a6c4ca2015-10-06 11:38:28 -0700634 CreatePeerConnectionExpectFail(kStunInvalidPort);
635 CreatePeerConnectionExpectFail(kStunAddressPortAndMore1);
636 CreatePeerConnectionExpectFail(kStunAddressPortAndMore2);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000637
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700638 CreatePeerConnectionWithIceServer(kTurnIceServerUri, kTurnPassword);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800639 EXPECT_EQ(0u, port_allocator_->stun_servers().size());
640 EXPECT_EQ(1u, port_allocator_->turn_servers().size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000641 EXPECT_EQ(kTurnUsername,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800642 port_allocator_->turn_servers()[0].credentials.username);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000643 EXPECT_EQ(kTurnPassword,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800644 port_allocator_->turn_servers()[0].credentials.password);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000645 EXPECT_EQ(kTurnHostname,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800646 port_allocator_->turn_servers()[0].ports[0].address.hostname());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000647 }
648
649 void ReleasePeerConnection() {
650 pc_ = NULL;
651 observer_.SetPeerConnectionInterface(NULL);
652 }
653
deadbeefab9b2d12015-10-14 11:33:11 -0700654 void AddVideoStream(const std::string& label) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000655 // Create a local stream.
656 scoped_refptr<MediaStreamInterface> stream(
657 pc_factory_->CreateLocalMediaStream(label));
perkja3ede6c2016-03-08 01:27:48 +0100658 scoped_refptr<VideoTrackSourceInterface> video_source(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000659 pc_factory_->CreateVideoSource(new cricket::FakeVideoCapturer(), NULL));
660 scoped_refptr<VideoTrackInterface> video_track(
661 pc_factory_->CreateVideoTrack(label + "v0", video_source));
662 stream->AddTrack(video_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +0000663 EXPECT_TRUE(pc_->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000664 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
665 observer_.renegotiation_needed_ = false;
666 }
667
668 void AddVoiceStream(const std::string& label) {
669 // Create a local stream.
670 scoped_refptr<MediaStreamInterface> stream(
671 pc_factory_->CreateLocalMediaStream(label));
672 scoped_refptr<AudioTrackInterface> audio_track(
673 pc_factory_->CreateAudioTrack(label + "a0", NULL));
674 stream->AddTrack(audio_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +0000675 EXPECT_TRUE(pc_->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000676 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
677 observer_.renegotiation_needed_ = false;
678 }
679
680 void AddAudioVideoStream(const std::string& stream_label,
681 const std::string& audio_track_label,
682 const std::string& video_track_label) {
683 // Create a local stream.
684 scoped_refptr<MediaStreamInterface> stream(
685 pc_factory_->CreateLocalMediaStream(stream_label));
686 scoped_refptr<AudioTrackInterface> audio_track(
687 pc_factory_->CreateAudioTrack(
688 audio_track_label, static_cast<AudioSourceInterface*>(NULL)));
689 stream->AddTrack(audio_track.get());
690 scoped_refptr<VideoTrackInterface> video_track(
nisseaf510af2016-03-21 08:20:42 -0700691 pc_factory_->CreateVideoTrack(
692 video_track_label,
693 pc_factory_->CreateVideoSource(new cricket::FakeVideoCapturer())));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000694 stream->AddTrack(video_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +0000695 EXPECT_TRUE(pc_->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000696 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
697 observer_.renegotiation_needed_ = false;
698 }
699
kwibergd1fe2812016-04-27 06:47:29 -0700700 bool DoCreateOfferAnswer(std::unique_ptr<SessionDescriptionInterface>* desc,
deadbeefc80741f2015-10-22 13:14:45 -0700701 bool offer,
702 MediaConstraintsInterface* constraints) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000703 rtc::scoped_refptr<MockCreateSessionDescriptionObserver>
704 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000705 MockCreateSessionDescriptionObserver>());
706 if (offer) {
deadbeefc80741f2015-10-22 13:14:45 -0700707 pc_->CreateOffer(observer, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000708 } else {
deadbeefc80741f2015-10-22 13:14:45 -0700709 pc_->CreateAnswer(observer, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000710 }
711 EXPECT_EQ_WAIT(true, observer->called(), kTimeout);
kwiberg2bbff992016-03-16 11:03:04 -0700712 desc->reset(observer->release_desc());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000713 return observer->result();
714 }
715
kwibergd1fe2812016-04-27 06:47:29 -0700716 bool DoCreateOffer(std::unique_ptr<SessionDescriptionInterface>* desc,
deadbeefc80741f2015-10-22 13:14:45 -0700717 MediaConstraintsInterface* constraints) {
718 return DoCreateOfferAnswer(desc, true, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000719 }
720
kwibergd1fe2812016-04-27 06:47:29 -0700721 bool DoCreateAnswer(std::unique_ptr<SessionDescriptionInterface>* desc,
deadbeefc80741f2015-10-22 13:14:45 -0700722 MediaConstraintsInterface* constraints) {
723 return DoCreateOfferAnswer(desc, false, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000724 }
725
726 bool DoSetSessionDescription(SessionDescriptionInterface* desc, bool local) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000727 rtc::scoped_refptr<MockSetSessionDescriptionObserver>
728 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000729 MockSetSessionDescriptionObserver>());
730 if (local) {
731 pc_->SetLocalDescription(observer, desc);
732 } else {
733 pc_->SetRemoteDescription(observer, desc);
734 }
735 EXPECT_EQ_WAIT(true, observer->called(), kTimeout);
736 return observer->result();
737 }
738
739 bool DoSetLocalDescription(SessionDescriptionInterface* desc) {
740 return DoSetSessionDescription(desc, true);
741 }
742
743 bool DoSetRemoteDescription(SessionDescriptionInterface* desc) {
744 return DoSetSessionDescription(desc, false);
745 }
746
747 // Calls PeerConnection::GetStats and check the return value.
748 // It does not verify the values in the StatReports since a RTCP packet might
749 // be required.
750 bool DoGetStats(MediaStreamTrackInterface* track) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000751 rtc::scoped_refptr<MockStatsObserver> observer(
752 new rtc::RefCountedObject<MockStatsObserver>());
jiayl@webrtc.orgdb41b4d2014-03-03 21:30:06 +0000753 if (!pc_->GetStats(
754 observer, track, PeerConnectionInterface::kStatsOutputLevelStandard))
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000755 return false;
756 EXPECT_TRUE_WAIT(observer->called(), kTimeout);
757 return observer->called();
758 }
759
760 void InitiateCall() {
761 CreatePeerConnection();
762 // Create a local stream with audio&video tracks.
763 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
764 CreateOfferReceiveAnswer();
765 }
766
767 // Verify that RTP Header extensions has been negotiated for audio and video.
768 void VerifyRemoteRtpHeaderExtensions() {
769 const cricket::MediaContentDescription* desc =
770 cricket::GetFirstAudioContentDescription(
771 pc_->remote_description()->description());
772 ASSERT_TRUE(desc != NULL);
773 EXPECT_GT(desc->rtp_header_extensions().size(), 0u);
774
775 desc = cricket::GetFirstVideoContentDescription(
776 pc_->remote_description()->description());
777 ASSERT_TRUE(desc != NULL);
778 EXPECT_GT(desc->rtp_header_extensions().size(), 0u);
779 }
780
781 void CreateOfferAsRemoteDescription() {
kwibergd1fe2812016-04-27 06:47:29 -0700782 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -0700783 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000784 std::string sdp;
785 EXPECT_TRUE(offer->ToString(&sdp));
786 SessionDescriptionInterface* remote_offer =
787 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
788 sdp, NULL);
789 EXPECT_TRUE(DoSetRemoteDescription(remote_offer));
790 EXPECT_EQ(PeerConnectionInterface::kHaveRemoteOffer, observer_.state_);
791 }
792
deadbeefab9b2d12015-10-14 11:33:11 -0700793 void CreateAndSetRemoteOffer(const std::string& sdp) {
794 SessionDescriptionInterface* remote_offer =
795 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
796 sdp, nullptr);
797 EXPECT_TRUE(DoSetRemoteDescription(remote_offer));
798 EXPECT_EQ(PeerConnectionInterface::kHaveRemoteOffer, observer_.state_);
799 }
800
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000801 void CreateAnswerAsLocalDescription() {
kwibergd1fe2812016-04-27 06:47:29 -0700802 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -0700803 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000804
805 // TODO(perkj): Currently SetLocalDescription fails if any parameters in an
806 // audio codec change, even if the parameter has nothing to do with
807 // receiving. Not all parameters are serialized to SDP.
808 // Since CreatePrAnswerAsLocalDescription serialize/deserialize
809 // the SessionDescription, it is necessary to do that here to in order to
810 // get ReceiveOfferCreatePrAnswerAndAnswer and RenegotiateAudioOnly to pass.
811 // https://code.google.com/p/webrtc/issues/detail?id=1356
812 std::string sdp;
813 EXPECT_TRUE(answer->ToString(&sdp));
814 SessionDescriptionInterface* new_answer =
815 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
816 sdp, NULL);
817 EXPECT_TRUE(DoSetLocalDescription(new_answer));
818 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
819 }
820
821 void CreatePrAnswerAsLocalDescription() {
kwibergd1fe2812016-04-27 06:47:29 -0700822 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -0700823 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000824
825 std::string sdp;
826 EXPECT_TRUE(answer->ToString(&sdp));
827 SessionDescriptionInterface* pr_answer =
828 webrtc::CreateSessionDescription(SessionDescriptionInterface::kPrAnswer,
829 sdp, NULL);
830 EXPECT_TRUE(DoSetLocalDescription(pr_answer));
831 EXPECT_EQ(PeerConnectionInterface::kHaveLocalPrAnswer, observer_.state_);
832 }
833
834 void CreateOfferReceiveAnswer() {
835 CreateOfferAsLocalDescription();
836 std::string sdp;
837 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
838 CreateAnswerAsRemoteDescription(sdp);
839 }
840
841 void CreateOfferAsLocalDescription() {
kwibergd1fe2812016-04-27 06:47:29 -0700842 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -0700843 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000844 // TODO(perkj): Currently SetLocalDescription fails if any parameters in an
845 // audio codec change, even if the parameter has nothing to do with
846 // receiving. Not all parameters are serialized to SDP.
847 // Since CreatePrAnswerAsLocalDescription serialize/deserialize
848 // the SessionDescription, it is necessary to do that here to in order to
849 // get ReceiveOfferCreatePrAnswerAndAnswer and RenegotiateAudioOnly to pass.
850 // https://code.google.com/p/webrtc/issues/detail?id=1356
851 std::string sdp;
852 EXPECT_TRUE(offer->ToString(&sdp));
853 SessionDescriptionInterface* new_offer =
854 webrtc::CreateSessionDescription(
855 SessionDescriptionInterface::kOffer,
856 sdp, NULL);
857
858 EXPECT_TRUE(DoSetLocalDescription(new_offer));
859 EXPECT_EQ(PeerConnectionInterface::kHaveLocalOffer, observer_.state_);
mallinath@webrtc.org68cbd012014-01-22 00:16:46 +0000860 // Wait for the ice_complete message, so that SDP will have candidates.
861 EXPECT_TRUE_WAIT(observer_.ice_complete_, kTimeout);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000862 }
863
deadbeefab9b2d12015-10-14 11:33:11 -0700864 void CreateAnswerAsRemoteDescription(const std::string& sdp) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000865 webrtc::JsepSessionDescription* answer = new webrtc::JsepSessionDescription(
866 SessionDescriptionInterface::kAnswer);
deadbeefab9b2d12015-10-14 11:33:11 -0700867 EXPECT_TRUE(answer->Initialize(sdp, NULL));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000868 EXPECT_TRUE(DoSetRemoteDescription(answer));
869 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
870 }
871
deadbeefab9b2d12015-10-14 11:33:11 -0700872 void CreatePrAnswerAndAnswerAsRemoteDescription(const std::string& sdp) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000873 webrtc::JsepSessionDescription* pr_answer =
874 new webrtc::JsepSessionDescription(
875 SessionDescriptionInterface::kPrAnswer);
deadbeefab9b2d12015-10-14 11:33:11 -0700876 EXPECT_TRUE(pr_answer->Initialize(sdp, NULL));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000877 EXPECT_TRUE(DoSetRemoteDescription(pr_answer));
878 EXPECT_EQ(PeerConnectionInterface::kHaveRemotePrAnswer, observer_.state_);
879 webrtc::JsepSessionDescription* answer =
880 new webrtc::JsepSessionDescription(
881 SessionDescriptionInterface::kAnswer);
deadbeefab9b2d12015-10-14 11:33:11 -0700882 EXPECT_TRUE(answer->Initialize(sdp, NULL));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000883 EXPECT_TRUE(DoSetRemoteDescription(answer));
884 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
885 }
886
887 // Help function used for waiting until a the last signaled remote stream has
888 // the same label as |stream_label|. In a few of the tests in this file we
889 // answer with the same session description as we offer and thus we can
890 // check if OnAddStream have been called with the same stream as we offer to
891 // send.
892 void WaitAndVerifyOnAddStream(const std::string& stream_label) {
893 EXPECT_EQ_WAIT(stream_label, observer_.GetLastAddedStreamLabel(), kTimeout);
894 }
895
896 // Creates an offer and applies it as a local session description.
897 // Creates an answer with the same SDP an the offer but removes all lines
898 // that start with a:ssrc"
899 void CreateOfferReceiveAnswerWithoutSsrc() {
900 CreateOfferAsLocalDescription();
901 std::string sdp;
902 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
903 SetSsrcToZero(&sdp);
904 CreateAnswerAsRemoteDescription(sdp);
905 }
906
deadbeefab9b2d12015-10-14 11:33:11 -0700907 // This function creates a MediaStream with label kStreams[0] and
908 // |number_of_audio_tracks| and |number_of_video_tracks| tracks and the
909 // corresponding SessionDescriptionInterface. The SessionDescriptionInterface
kwiberg2bbff992016-03-16 11:03:04 -0700910 // is returned and the MediaStream is stored in
deadbeefab9b2d12015-10-14 11:33:11 -0700911 // |reference_collection_|
kwibergd1fe2812016-04-27 06:47:29 -0700912 std::unique_ptr<SessionDescriptionInterface>
kwiberg2bbff992016-03-16 11:03:04 -0700913 CreateSessionDescriptionAndReference(size_t number_of_audio_tracks,
914 size_t number_of_video_tracks) {
915 EXPECT_LE(number_of_audio_tracks, 2u);
916 EXPECT_LE(number_of_video_tracks, 2u);
deadbeefab9b2d12015-10-14 11:33:11 -0700917
918 reference_collection_ = StreamCollection::Create();
919 std::string sdp_ms1 = std::string(kSdpStringInit);
920
921 std::string mediastream_label = kStreams[0];
922
923 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream(
924 webrtc::MediaStream::Create(mediastream_label));
925 reference_collection_->AddStream(stream);
926
927 if (number_of_audio_tracks > 0) {
928 sdp_ms1 += std::string(kSdpStringAudio);
929 sdp_ms1 += std::string(kSdpStringMs1Audio0);
930 AddAudioTrack(kAudioTracks[0], stream);
931 }
932 if (number_of_audio_tracks > 1) {
933 sdp_ms1 += kSdpStringMs1Audio1;
934 AddAudioTrack(kAudioTracks[1], stream);
935 }
936
937 if (number_of_video_tracks > 0) {
938 sdp_ms1 += std::string(kSdpStringVideo);
939 sdp_ms1 += std::string(kSdpStringMs1Video0);
940 AddVideoTrack(kVideoTracks[0], stream);
941 }
942 if (number_of_video_tracks > 1) {
943 sdp_ms1 += kSdpStringMs1Video1;
944 AddVideoTrack(kVideoTracks[1], stream);
945 }
946
kwibergd1fe2812016-04-27 06:47:29 -0700947 return std::unique_ptr<SessionDescriptionInterface>(
kwiberg2bbff992016-03-16 11:03:04 -0700948 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
949 sdp_ms1, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -0700950 }
951
952 void AddAudioTrack(const std::string& track_id,
953 MediaStreamInterface* stream) {
954 rtc::scoped_refptr<webrtc::AudioTrackInterface> audio_track(
955 webrtc::AudioTrack::Create(track_id, nullptr));
956 ASSERT_TRUE(stream->AddTrack(audio_track));
957 }
958
959 void AddVideoTrack(const std::string& track_id,
960 MediaStreamInterface* stream) {
961 rtc::scoped_refptr<webrtc::VideoTrackInterface> video_track(
nisseaf510af2016-03-21 08:20:42 -0700962 webrtc::VideoTrack::Create(track_id,
963 webrtc::FakeVideoTrackSource::Create()));
deadbeefab9b2d12015-10-14 11:33:11 -0700964 ASSERT_TRUE(stream->AddTrack(video_track));
965 }
966
kwibergfd8be342016-05-14 19:44:11 -0700967 std::unique_ptr<SessionDescriptionInterface> CreateOfferWithOneAudioStream() {
zhihuang8f65cdf2016-05-06 18:40:30 -0700968 CreatePeerConnection();
969 AddVoiceStream(kStreamLabel1);
kwibergfd8be342016-05-14 19:44:11 -0700970 std::unique_ptr<SessionDescriptionInterface> offer;
zhihuang8f65cdf2016-05-06 18:40:30 -0700971 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
972 return offer;
973 }
974
kwibergfd8be342016-05-14 19:44:11 -0700975 std::unique_ptr<SessionDescriptionInterface>
zhihuang8f65cdf2016-05-06 18:40:30 -0700976 CreateAnswerWithOneAudioStream() {
kwibergfd8be342016-05-14 19:44:11 -0700977 std::unique_ptr<SessionDescriptionInterface> offer =
zhihuang8f65cdf2016-05-06 18:40:30 -0700978 CreateOfferWithOneAudioStream();
979 EXPECT_TRUE(DoSetRemoteDescription(offer.release()));
kwibergfd8be342016-05-14 19:44:11 -0700980 std::unique_ptr<SessionDescriptionInterface> answer;
zhihuang8f65cdf2016-05-06 18:40:30 -0700981 EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
982 return answer;
983 }
984
985 const std::string& GetFirstAudioStreamCname(
986 const SessionDescriptionInterface* desc) {
987 const cricket::ContentInfo* audio_content =
988 cricket::GetFirstAudioContent(desc->description());
989 const cricket::AudioContentDescription* audio_desc =
990 static_cast<const cricket::AudioContentDescription*>(
991 audio_content->description);
992 return audio_desc->streams()[0].cname;
993 }
994
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800995 cricket::FakePortAllocator* port_allocator_ = nullptr;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000996 scoped_refptr<webrtc::PeerConnectionFactoryInterface> pc_factory_;
997 scoped_refptr<PeerConnectionInterface> pc_;
998 MockPeerConnectionObserver observer_;
deadbeefab9b2d12015-10-14 11:33:11 -0700999 rtc::scoped_refptr<StreamCollection> reference_collection_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001000};
1001
zhihuang8f65cdf2016-05-06 18:40:30 -07001002// Generate different CNAMEs when PeerConnections are created.
1003// The CNAMEs are expected to be generated randomly. It is possible
1004// that the test fails, though the possibility is very low.
1005TEST_F(PeerConnectionInterfaceTest, CnameGenerationInOffer) {
kwibergfd8be342016-05-14 19:44:11 -07001006 std::unique_ptr<SessionDescriptionInterface> offer1 =
zhihuang8f65cdf2016-05-06 18:40:30 -07001007 CreateOfferWithOneAudioStream();
kwibergfd8be342016-05-14 19:44:11 -07001008 std::unique_ptr<SessionDescriptionInterface> offer2 =
zhihuang8f65cdf2016-05-06 18:40:30 -07001009 CreateOfferWithOneAudioStream();
1010 EXPECT_NE(GetFirstAudioStreamCname(offer1.get()),
1011 GetFirstAudioStreamCname(offer2.get()));
1012}
1013
1014TEST_F(PeerConnectionInterfaceTest, CnameGenerationInAnswer) {
kwibergfd8be342016-05-14 19:44:11 -07001015 std::unique_ptr<SessionDescriptionInterface> answer1 =
zhihuang8f65cdf2016-05-06 18:40:30 -07001016 CreateAnswerWithOneAudioStream();
kwibergfd8be342016-05-14 19:44:11 -07001017 std::unique_ptr<SessionDescriptionInterface> answer2 =
zhihuang8f65cdf2016-05-06 18:40:30 -07001018 CreateAnswerWithOneAudioStream();
1019 EXPECT_NE(GetFirstAudioStreamCname(answer1.get()),
1020 GetFirstAudioStreamCname(answer2.get()));
1021}
1022
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001023TEST_F(PeerConnectionInterfaceTest,
1024 CreatePeerConnectionWithDifferentConfigurations) {
1025 CreatePeerConnectionWithDifferentConfigurations();
1026}
1027
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001028TEST_F(PeerConnectionInterfaceTest,
1029 CreatePeerConnectionWithDifferentIceTransportsTypes) {
1030 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kNone);
1031 EXPECT_EQ(cricket::CF_NONE, port_allocator_->candidate_filter());
1032 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kRelay);
1033 EXPECT_EQ(cricket::CF_RELAY, port_allocator_->candidate_filter());
1034 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kNoHost);
1035 EXPECT_EQ(cricket::CF_ALL & ~cricket::CF_HOST,
1036 port_allocator_->candidate_filter());
1037 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kAll);
1038 EXPECT_EQ(cricket::CF_ALL, port_allocator_->candidate_filter());
1039}
1040
1041// Test that when a PeerConnection is created with a nonzero candidate pool
1042// size, the pooled PortAllocatorSession is created with all the attributes
1043// in the RTCConfiguration.
1044TEST_F(PeerConnectionInterfaceTest, CreatePeerConnectionWithPooledCandidates) {
1045 PeerConnectionInterface::RTCConfiguration config;
1046 PeerConnectionInterface::IceServer server;
1047 server.uri = kStunAddressOnly;
1048 config.servers.push_back(server);
1049 config.type = PeerConnectionInterface::kRelay;
1050 config.disable_ipv6 = true;
1051 config.tcp_candidate_policy =
1052 PeerConnectionInterface::kTcpCandidatePolicyDisabled;
honghaiz60347052016-05-31 18:29:12 -07001053 config.candidate_network_policy =
1054 PeerConnectionInterface::kCandidateNetworkPolicyLowCost;
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001055 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);
honghaiz60347052016-05-31 18:29:12 -07001065 EXPECT_LT(0U,
1066 session->flags() & cricket::PORTALLOCATOR_DISABLE_COSTLY_NETWORKS);
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001067}
1068
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001069TEST_F(PeerConnectionInterfaceTest, AddStreams) {
1070 CreatePeerConnection();
deadbeefab9b2d12015-10-14 11:33:11 -07001071 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001072 AddVoiceStream(kStreamLabel2);
1073 ASSERT_EQ(2u, pc_->local_streams()->count());
1074
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001075 // Test we can add multiple local streams to one peerconnection.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001076 scoped_refptr<MediaStreamInterface> stream(
1077 pc_factory_->CreateLocalMediaStream(kStreamLabel3));
1078 scoped_refptr<AudioTrackInterface> audio_track(
1079 pc_factory_->CreateAudioTrack(
1080 kStreamLabel3, static_cast<AudioSourceInterface*>(NULL)));
1081 stream->AddTrack(audio_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +00001082 EXPECT_TRUE(pc_->AddStream(stream));
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001083 EXPECT_EQ(3u, pc_->local_streams()->count());
1084
1085 // Remove the third stream.
1086 pc_->RemoveStream(pc_->local_streams()->at(2));
1087 EXPECT_EQ(2u, pc_->local_streams()->count());
1088
1089 // Remove the second stream.
1090 pc_->RemoveStream(pc_->local_streams()->at(1));
1091 EXPECT_EQ(1u, pc_->local_streams()->count());
1092
1093 // Remove the first stream.
1094 pc_->RemoveStream(pc_->local_streams()->at(0));
1095 EXPECT_EQ(0u, pc_->local_streams()->count());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001096}
1097
deadbeefab9b2d12015-10-14 11:33:11 -07001098// Test that the created offer includes streams we added.
1099TEST_F(PeerConnectionInterfaceTest, AddedStreamsPresentInOffer) {
1100 CreatePeerConnection();
1101 AddAudioVideoStream(kStreamLabel1, "audio_track", "video_track");
kwibergd1fe2812016-04-27 06:47:29 -07001102 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001103 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07001104
1105 const cricket::ContentInfo* audio_content =
1106 cricket::GetFirstAudioContent(offer->description());
1107 const cricket::AudioContentDescription* audio_desc =
1108 static_cast<const cricket::AudioContentDescription*>(
1109 audio_content->description);
1110 EXPECT_TRUE(
1111 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
1112
1113 const cricket::ContentInfo* video_content =
1114 cricket::GetFirstVideoContent(offer->description());
1115 const cricket::VideoContentDescription* video_desc =
1116 static_cast<const cricket::VideoContentDescription*>(
1117 video_content->description);
1118 EXPECT_TRUE(
1119 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1120
1121 // Add another stream and ensure the offer includes both the old and new
1122 // streams.
1123 AddAudioVideoStream(kStreamLabel2, "audio_track2", "video_track2");
kwiberg2bbff992016-03-16 11:03:04 -07001124 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07001125
1126 audio_content = cricket::GetFirstAudioContent(offer->description());
1127 audio_desc = static_cast<const cricket::AudioContentDescription*>(
1128 audio_content->description);
1129 EXPECT_TRUE(
1130 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
1131 EXPECT_TRUE(
1132 ContainsTrack(audio_desc->streams(), kStreamLabel2, "audio_track2"));
1133
1134 video_content = cricket::GetFirstVideoContent(offer->description());
1135 video_desc = static_cast<const cricket::VideoContentDescription*>(
1136 video_content->description);
1137 EXPECT_TRUE(
1138 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1139 EXPECT_TRUE(
1140 ContainsTrack(video_desc->streams(), kStreamLabel2, "video_track2"));
1141}
1142
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001143TEST_F(PeerConnectionInterfaceTest, RemoveStream) {
1144 CreatePeerConnection();
deadbeefab9b2d12015-10-14 11:33:11 -07001145 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001146 ASSERT_EQ(1u, pc_->local_streams()->count());
1147 pc_->RemoveStream(pc_->local_streams()->at(0));
1148 EXPECT_EQ(0u, pc_->local_streams()->count());
1149}
1150
deadbeefe1f9d832016-01-14 15:35:42 -08001151// Test for AddTrack and RemoveTrack methods.
1152// Tests that the created offer includes tracks we added,
1153// and that the RtpSenders are created correctly.
1154// Also tests that RemoveTrack removes the tracks from subsequent offers.
1155TEST_F(PeerConnectionInterfaceTest, AddTrackRemoveTrack) {
1156 CreatePeerConnection();
1157 // Create a dummy stream, so tracks share a stream label.
1158 scoped_refptr<MediaStreamInterface> stream(
1159 pc_factory_->CreateLocalMediaStream(kStreamLabel1));
1160 std::vector<MediaStreamInterface*> stream_list;
1161 stream_list.push_back(stream.get());
1162 scoped_refptr<AudioTrackInterface> audio_track(
1163 pc_factory_->CreateAudioTrack("audio_track", nullptr));
nisseaf510af2016-03-21 08:20:42 -07001164 scoped_refptr<VideoTrackInterface> video_track(pc_factory_->CreateVideoTrack(
1165 "video_track",
1166 pc_factory_->CreateVideoSource(new cricket::FakeVideoCapturer())));
deadbeefe1f9d832016-01-14 15:35:42 -08001167 auto audio_sender = pc_->AddTrack(audio_track, stream_list);
1168 auto video_sender = pc_->AddTrack(video_track, stream_list);
1169 EXPECT_EQ(kStreamLabel1, audio_sender->stream_id());
1170 EXPECT_EQ("audio_track", audio_sender->id());
1171 EXPECT_EQ(audio_track, audio_sender->track());
1172 EXPECT_EQ(kStreamLabel1, video_sender->stream_id());
1173 EXPECT_EQ("video_track", video_sender->id());
1174 EXPECT_EQ(video_track, video_sender->track());
1175
1176 // Now create an offer and check for the senders.
kwibergd1fe2812016-04-27 06:47:29 -07001177 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001178 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefe1f9d832016-01-14 15:35:42 -08001179
1180 const cricket::ContentInfo* audio_content =
1181 cricket::GetFirstAudioContent(offer->description());
1182 const cricket::AudioContentDescription* audio_desc =
1183 static_cast<const cricket::AudioContentDescription*>(
1184 audio_content->description);
1185 EXPECT_TRUE(
1186 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
1187
1188 const cricket::ContentInfo* video_content =
1189 cricket::GetFirstVideoContent(offer->description());
1190 const cricket::VideoContentDescription* video_desc =
1191 static_cast<const cricket::VideoContentDescription*>(
1192 video_content->description);
1193 EXPECT_TRUE(
1194 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1195
1196 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
1197
1198 // Now try removing the tracks.
1199 EXPECT_TRUE(pc_->RemoveTrack(audio_sender));
1200 EXPECT_TRUE(pc_->RemoveTrack(video_sender));
1201
1202 // Create a new offer and ensure it doesn't contain the removed senders.
kwiberg2bbff992016-03-16 11:03:04 -07001203 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefe1f9d832016-01-14 15:35:42 -08001204
1205 audio_content = cricket::GetFirstAudioContent(offer->description());
1206 audio_desc = static_cast<const cricket::AudioContentDescription*>(
1207 audio_content->description);
1208 EXPECT_FALSE(
1209 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
1210
1211 video_content = cricket::GetFirstVideoContent(offer->description());
1212 video_desc = static_cast<const cricket::VideoContentDescription*>(
1213 video_content->description);
1214 EXPECT_FALSE(
1215 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1216
1217 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
1218
1219 // Calling RemoveTrack on a sender no longer attached to a PeerConnection
1220 // should return false.
1221 EXPECT_FALSE(pc_->RemoveTrack(audio_sender));
1222 EXPECT_FALSE(pc_->RemoveTrack(video_sender));
1223}
1224
1225// Test creating senders without a stream specified,
1226// expecting a random stream ID to be generated.
1227TEST_F(PeerConnectionInterfaceTest, AddTrackWithoutStream) {
1228 CreatePeerConnection();
1229 // Create a dummy stream, so tracks share a stream label.
1230 scoped_refptr<AudioTrackInterface> audio_track(
1231 pc_factory_->CreateAudioTrack("audio_track", nullptr));
nisseaf510af2016-03-21 08:20:42 -07001232 scoped_refptr<VideoTrackInterface> video_track(pc_factory_->CreateVideoTrack(
1233 "video_track",
1234 pc_factory_->CreateVideoSource(new cricket::FakeVideoCapturer())));
deadbeefe1f9d832016-01-14 15:35:42 -08001235 auto audio_sender =
1236 pc_->AddTrack(audio_track, std::vector<MediaStreamInterface*>());
1237 auto video_sender =
1238 pc_->AddTrack(video_track, std::vector<MediaStreamInterface*>());
1239 EXPECT_EQ("audio_track", audio_sender->id());
1240 EXPECT_EQ(audio_track, audio_sender->track());
1241 EXPECT_EQ("video_track", video_sender->id());
1242 EXPECT_EQ(video_track, video_sender->track());
1243 // If the ID is truly a random GUID, it should be infinitely unlikely they
1244 // will be the same.
1245 EXPECT_NE(video_sender->stream_id(), audio_sender->stream_id());
1246}
1247
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001248TEST_F(PeerConnectionInterfaceTest, CreateOfferReceiveAnswer) {
1249 InitiateCall();
1250 WaitAndVerifyOnAddStream(kStreamLabel1);
1251 VerifyRemoteRtpHeaderExtensions();
1252}
1253
1254TEST_F(PeerConnectionInterfaceTest, CreateOfferReceivePrAnswerAndAnswer) {
1255 CreatePeerConnection();
deadbeefab9b2d12015-10-14 11:33:11 -07001256 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001257 CreateOfferAsLocalDescription();
1258 std::string offer;
1259 EXPECT_TRUE(pc_->local_description()->ToString(&offer));
1260 CreatePrAnswerAndAnswerAsRemoteDescription(offer);
1261 WaitAndVerifyOnAddStream(kStreamLabel1);
1262}
1263
1264TEST_F(PeerConnectionInterfaceTest, ReceiveOfferCreateAnswer) {
1265 CreatePeerConnection();
deadbeefab9b2d12015-10-14 11:33:11 -07001266 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001267
1268 CreateOfferAsRemoteDescription();
1269 CreateAnswerAsLocalDescription();
1270
1271 WaitAndVerifyOnAddStream(kStreamLabel1);
1272}
1273
1274TEST_F(PeerConnectionInterfaceTest, ReceiveOfferCreatePrAnswerAndAnswer) {
1275 CreatePeerConnection();
deadbeefab9b2d12015-10-14 11:33:11 -07001276 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001277
1278 CreateOfferAsRemoteDescription();
1279 CreatePrAnswerAsLocalDescription();
1280 CreateAnswerAsLocalDescription();
1281
1282 WaitAndVerifyOnAddStream(kStreamLabel1);
1283}
1284
1285TEST_F(PeerConnectionInterfaceTest, Renegotiate) {
1286 InitiateCall();
1287 ASSERT_EQ(1u, pc_->remote_streams()->count());
1288 pc_->RemoveStream(pc_->local_streams()->at(0));
1289 CreateOfferReceiveAnswer();
1290 EXPECT_EQ(0u, pc_->remote_streams()->count());
deadbeefab9b2d12015-10-14 11:33:11 -07001291 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001292 CreateOfferReceiveAnswer();
1293}
1294
1295// Tests that after negotiating an audio only call, the respondent can perform a
1296// renegotiation that removes the audio stream.
1297TEST_F(PeerConnectionInterfaceTest, RenegotiateAudioOnly) {
1298 CreatePeerConnection();
1299 AddVoiceStream(kStreamLabel1);
1300 CreateOfferAsRemoteDescription();
1301 CreateAnswerAsLocalDescription();
1302
1303 ASSERT_EQ(1u, pc_->remote_streams()->count());
1304 pc_->RemoveStream(pc_->local_streams()->at(0));
1305 CreateOfferReceiveAnswer();
1306 EXPECT_EQ(0u, pc_->remote_streams()->count());
1307}
1308
1309// Test that candidates are generated and that we can parse our own candidates.
1310TEST_F(PeerConnectionInterfaceTest, IceCandidates) {
1311 CreatePeerConnection();
1312
1313 EXPECT_FALSE(pc_->AddIceCandidate(observer_.last_candidate_.get()));
1314 // SetRemoteDescription takes ownership of offer.
kwibergd1fe2812016-04-27 06:47:29 -07001315 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefab9b2d12015-10-14 11:33:11 -07001316 AddVideoStream(kStreamLabel1);
deadbeefc80741f2015-10-22 13:14:45 -07001317 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
kwiberg2bbff992016-03-16 11:03:04 -07001318 EXPECT_TRUE(DoSetRemoteDescription(offer.release()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001319
1320 // SetLocalDescription takes ownership of answer.
kwibergd1fe2812016-04-27 06:47:29 -07001321 std::unique_ptr<SessionDescriptionInterface> answer;
deadbeefc80741f2015-10-22 13:14:45 -07001322 EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
kwiberg2bbff992016-03-16 11:03:04 -07001323 EXPECT_TRUE(DoSetLocalDescription(answer.release()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001324
1325 EXPECT_TRUE_WAIT(observer_.last_candidate_.get() != NULL, kTimeout);
1326 EXPECT_TRUE_WAIT(observer_.ice_complete_, kTimeout);
1327
1328 EXPECT_TRUE(pc_->AddIceCandidate(observer_.last_candidate_.get()));
1329}
1330
deadbeefab9b2d12015-10-14 11:33:11 -07001331// Test that CreateOffer and CreateAnswer will fail if the track labels are
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001332// not unique.
1333TEST_F(PeerConnectionInterfaceTest, CreateOfferAnswerWithInvalidStream) {
1334 CreatePeerConnection();
1335 // Create a regular offer for the CreateAnswer test later.
kwibergd1fe2812016-04-27 06:47:29 -07001336 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefc80741f2015-10-22 13:14:45 -07001337 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
kwiberg2bbff992016-03-16 11:03:04 -07001338 EXPECT_TRUE(offer);
1339 offer.reset();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001340
1341 // Create a local stream with audio&video tracks having same label.
1342 AddAudioVideoStream(kStreamLabel1, "track_label", "track_label");
1343
1344 // Test CreateOffer
deadbeefc80741f2015-10-22 13:14:45 -07001345 EXPECT_FALSE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001346
1347 // Test CreateAnswer
kwibergd1fe2812016-04-27 06:47:29 -07001348 std::unique_ptr<SessionDescriptionInterface> answer;
deadbeefc80741f2015-10-22 13:14:45 -07001349 EXPECT_FALSE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001350}
1351
1352// Test that we will get different SSRCs for each tracks in the offer and answer
1353// we created.
1354TEST_F(PeerConnectionInterfaceTest, SsrcInOfferAnswer) {
1355 CreatePeerConnection();
1356 // Create a local stream with audio&video tracks having different labels.
1357 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
1358
1359 // Test CreateOffer
kwibergd1fe2812016-04-27 06:47:29 -07001360 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001361 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001362 int audio_ssrc = 0;
1363 int video_ssrc = 0;
1364 EXPECT_TRUE(GetFirstSsrc(GetFirstAudioContent(offer->description()),
1365 &audio_ssrc));
1366 EXPECT_TRUE(GetFirstSsrc(GetFirstVideoContent(offer->description()),
1367 &video_ssrc));
1368 EXPECT_NE(audio_ssrc, video_ssrc);
1369
1370 // Test CreateAnswer
1371 EXPECT_TRUE(DoSetRemoteDescription(offer.release()));
kwibergd1fe2812016-04-27 06:47:29 -07001372 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -07001373 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001374 audio_ssrc = 0;
1375 video_ssrc = 0;
1376 EXPECT_TRUE(GetFirstSsrc(GetFirstAudioContent(answer->description()),
1377 &audio_ssrc));
1378 EXPECT_TRUE(GetFirstSsrc(GetFirstVideoContent(answer->description()),
1379 &video_ssrc));
1380 EXPECT_NE(audio_ssrc, video_ssrc);
1381}
1382
deadbeefeb459812015-12-15 19:24:43 -08001383// Test that it's possible to call AddTrack on a MediaStream after adding
1384// the stream to a PeerConnection.
1385// TODO(deadbeef): Remove this test once this behavior is no longer supported.
1386TEST_F(PeerConnectionInterfaceTest, AddTrackAfterAddStream) {
1387 CreatePeerConnection();
1388 // Create audio stream and add to PeerConnection.
1389 AddVoiceStream(kStreamLabel1);
1390 MediaStreamInterface* stream = pc_->local_streams()->at(0);
1391
1392 // Add video track to the audio-only stream.
nisseaf510af2016-03-21 08:20:42 -07001393 scoped_refptr<VideoTrackInterface> video_track(pc_factory_->CreateVideoTrack(
1394 "video_label",
1395 pc_factory_->CreateVideoSource(new cricket::FakeVideoCapturer())));
deadbeefeb459812015-12-15 19:24:43 -08001396 stream->AddTrack(video_track.get());
1397
kwibergd1fe2812016-04-27 06:47:29 -07001398 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001399 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefeb459812015-12-15 19:24:43 -08001400
1401 const cricket::MediaContentDescription* video_desc =
1402 cricket::GetFirstVideoContentDescription(offer->description());
1403 EXPECT_TRUE(video_desc != nullptr);
1404}
1405
1406// Test that it's possible to call RemoveTrack on a MediaStream after adding
1407// the stream to a PeerConnection.
1408// TODO(deadbeef): Remove this test once this behavior is no longer supported.
1409TEST_F(PeerConnectionInterfaceTest, RemoveTrackAfterAddStream) {
1410 CreatePeerConnection();
1411 // Create audio/video stream and add to PeerConnection.
1412 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
1413 MediaStreamInterface* stream = pc_->local_streams()->at(0);
1414
1415 // Remove the video track.
1416 stream->RemoveTrack(stream->GetVideoTracks()[0]);
1417
kwibergd1fe2812016-04-27 06:47:29 -07001418 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001419 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefeb459812015-12-15 19:24:43 -08001420
1421 const cricket::MediaContentDescription* video_desc =
1422 cricket::GetFirstVideoContentDescription(offer->description());
1423 EXPECT_TRUE(video_desc == nullptr);
1424}
1425
deadbeefbd7d8f72015-12-18 16:58:44 -08001426// Test creating a sender with a stream ID, and ensure the ID is populated
1427// in the offer.
1428TEST_F(PeerConnectionInterfaceTest, CreateSenderWithStream) {
1429 CreatePeerConnection();
1430 pc_->CreateSender("video", kStreamLabel1);
1431
kwibergd1fe2812016-04-27 06:47:29 -07001432 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001433 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefbd7d8f72015-12-18 16:58:44 -08001434
1435 const cricket::MediaContentDescription* video_desc =
1436 cricket::GetFirstVideoContentDescription(offer->description());
1437 ASSERT_TRUE(video_desc != nullptr);
1438 ASSERT_EQ(1u, video_desc->streams().size());
1439 EXPECT_EQ(kStreamLabel1, video_desc->streams()[0].sync_label);
1440}
1441
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001442// Test that we can specify a certain track that we want statistics about.
1443TEST_F(PeerConnectionInterfaceTest, GetStatsForSpecificTrack) {
1444 InitiateCall();
1445 ASSERT_LT(0u, pc_->remote_streams()->count());
1446 ASSERT_LT(0u, pc_->remote_streams()->at(0)->GetAudioTracks().size());
1447 scoped_refptr<MediaStreamTrackInterface> remote_audio =
1448 pc_->remote_streams()->at(0)->GetAudioTracks()[0];
1449 EXPECT_TRUE(DoGetStats(remote_audio));
1450
1451 // Remove the stream. Since we are sending to our selves the local
1452 // and the remote stream is the same.
1453 pc_->RemoveStream(pc_->local_streams()->at(0));
1454 // Do a re-negotiation.
1455 CreateOfferReceiveAnswer();
1456
1457 ASSERT_EQ(0u, pc_->remote_streams()->count());
1458
1459 // Test that we still can get statistics for the old track. Even if it is not
1460 // sent any longer.
1461 EXPECT_TRUE(DoGetStats(remote_audio));
1462}
1463
1464// Test that we can get stats on a video track.
1465TEST_F(PeerConnectionInterfaceTest, GetStatsForVideoTrack) {
1466 InitiateCall();
1467 ASSERT_LT(0u, pc_->remote_streams()->count());
1468 ASSERT_LT(0u, pc_->remote_streams()->at(0)->GetVideoTracks().size());
1469 scoped_refptr<MediaStreamTrackInterface> remote_video =
1470 pc_->remote_streams()->at(0)->GetVideoTracks()[0];
1471 EXPECT_TRUE(DoGetStats(remote_video));
1472}
1473
1474// Test that we don't get statistics for an invalid track.
tommi@webrtc.org908f57e2014-07-21 11:44:39 +00001475// TODO(tommi): Fix this test. DoGetStats will return true
1476// for the unknown track (since GetStats is async), but no
1477// data is returned for the track.
1478TEST_F(PeerConnectionInterfaceTest, DISABLED_GetStatsForInvalidTrack) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001479 InitiateCall();
1480 scoped_refptr<AudioTrackInterface> unknown_audio_track(
1481 pc_factory_->CreateAudioTrack("unknown track", NULL));
1482 EXPECT_FALSE(DoGetStats(unknown_audio_track));
1483}
1484
1485// This test setup two RTP data channels in loop back.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001486TEST_F(PeerConnectionInterfaceTest, TestDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001487 FakeConstraints constraints;
1488 constraints.SetAllowRtpDataChannels();
1489 CreatePeerConnection(&constraints);
1490 scoped_refptr<DataChannelInterface> data1 =
1491 pc_->CreateDataChannel("test1", NULL);
1492 scoped_refptr<DataChannelInterface> data2 =
1493 pc_->CreateDataChannel("test2", NULL);
1494 ASSERT_TRUE(data1 != NULL);
kwibergd1fe2812016-04-27 06:47:29 -07001495 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001496 new MockDataChannelObserver(data1));
kwibergd1fe2812016-04-27 06:47:29 -07001497 std::unique_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001498 new MockDataChannelObserver(data2));
1499
1500 EXPECT_EQ(DataChannelInterface::kConnecting, data1->state());
1501 EXPECT_EQ(DataChannelInterface::kConnecting, data2->state());
1502 std::string data_to_send1 = "testing testing";
1503 std::string data_to_send2 = "testing something else";
1504 EXPECT_FALSE(data1->Send(DataBuffer(data_to_send1)));
1505
1506 CreateOfferReceiveAnswer();
1507 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1508 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
1509
1510 EXPECT_EQ(DataChannelInterface::kOpen, data1->state());
1511 EXPECT_EQ(DataChannelInterface::kOpen, data2->state());
1512 EXPECT_TRUE(data1->Send(DataBuffer(data_to_send1)));
1513 EXPECT_TRUE(data2->Send(DataBuffer(data_to_send2)));
1514
1515 EXPECT_EQ_WAIT(data_to_send1, observer1->last_message(), kTimeout);
1516 EXPECT_EQ_WAIT(data_to_send2, observer2->last_message(), kTimeout);
1517
1518 data1->Close();
1519 EXPECT_EQ(DataChannelInterface::kClosing, data1->state());
1520 CreateOfferReceiveAnswer();
1521 EXPECT_FALSE(observer1->IsOpen());
1522 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
1523 EXPECT_TRUE(observer2->IsOpen());
1524
1525 data_to_send2 = "testing something else again";
1526 EXPECT_TRUE(data2->Send(DataBuffer(data_to_send2)));
1527
1528 EXPECT_EQ_WAIT(data_to_send2, observer2->last_message(), kTimeout);
1529}
1530
1531// This test verifies that sendnig binary data over RTP data channels should
1532// fail.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001533TEST_F(PeerConnectionInterfaceTest, TestSendBinaryOnRtpDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001534 FakeConstraints constraints;
1535 constraints.SetAllowRtpDataChannels();
1536 CreatePeerConnection(&constraints);
1537 scoped_refptr<DataChannelInterface> data1 =
1538 pc_->CreateDataChannel("test1", NULL);
1539 scoped_refptr<DataChannelInterface> data2 =
1540 pc_->CreateDataChannel("test2", NULL);
1541 ASSERT_TRUE(data1 != NULL);
kwibergd1fe2812016-04-27 06:47:29 -07001542 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001543 new MockDataChannelObserver(data1));
kwibergd1fe2812016-04-27 06:47:29 -07001544 std::unique_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001545 new MockDataChannelObserver(data2));
1546
1547 EXPECT_EQ(DataChannelInterface::kConnecting, data1->state());
1548 EXPECT_EQ(DataChannelInterface::kConnecting, data2->state());
1549
1550 CreateOfferReceiveAnswer();
1551 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1552 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
1553
1554 EXPECT_EQ(DataChannelInterface::kOpen, data1->state());
1555 EXPECT_EQ(DataChannelInterface::kOpen, data2->state());
1556
jbaucheec21bd2016-03-20 06:15:43 -07001557 rtc::CopyOnWriteBuffer buffer("test", 4);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001558 EXPECT_FALSE(data1->Send(DataBuffer(buffer, true)));
1559}
1560
1561// This test setup a RTP data channels in loop back and test that a channel is
1562// opened even if the remote end answer with a zero SSRC.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001563TEST_F(PeerConnectionInterfaceTest, TestSendOnlyDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001564 FakeConstraints constraints;
1565 constraints.SetAllowRtpDataChannels();
1566 CreatePeerConnection(&constraints);
1567 scoped_refptr<DataChannelInterface> data1 =
1568 pc_->CreateDataChannel("test1", NULL);
kwibergd1fe2812016-04-27 06:47:29 -07001569 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001570 new MockDataChannelObserver(data1));
1571
1572 CreateOfferReceiveAnswerWithoutSsrc();
1573
1574 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1575
1576 data1->Close();
1577 EXPECT_EQ(DataChannelInterface::kClosing, data1->state());
1578 CreateOfferReceiveAnswerWithoutSsrc();
1579 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
1580 EXPECT_FALSE(observer1->IsOpen());
1581}
1582
1583// This test that if a data channel is added in an answer a receive only channel
1584// channel is created.
1585TEST_F(PeerConnectionInterfaceTest, TestReceiveOnlyDataChannel) {
1586 FakeConstraints constraints;
1587 constraints.SetAllowRtpDataChannels();
1588 CreatePeerConnection(&constraints);
1589
1590 std::string offer_label = "offer_channel";
1591 scoped_refptr<DataChannelInterface> offer_channel =
1592 pc_->CreateDataChannel(offer_label, NULL);
1593
1594 CreateOfferAsLocalDescription();
1595
1596 // Replace the data channel label in the offer and apply it as an answer.
1597 std::string receive_label = "answer_channel";
1598 std::string sdp;
1599 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001600 rtc::replace_substrs(offer_label.c_str(), offer_label.length(),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001601 receive_label.c_str(), receive_label.length(),
1602 &sdp);
1603 CreateAnswerAsRemoteDescription(sdp);
1604
1605 // Verify that a new incoming data channel has been created and that
1606 // it is open but can't we written to.
1607 ASSERT_TRUE(observer_.last_datachannel_ != NULL);
1608 DataChannelInterface* received_channel = observer_.last_datachannel_;
1609 EXPECT_EQ(DataChannelInterface::kConnecting, received_channel->state());
1610 EXPECT_EQ(receive_label, received_channel->label());
1611 EXPECT_FALSE(received_channel->Send(DataBuffer("something")));
1612
1613 // Verify that the channel we initially offered has been rejected.
1614 EXPECT_EQ(DataChannelInterface::kClosed, offer_channel->state());
1615
1616 // Do another offer / answer exchange and verify that the data channel is
1617 // opened.
1618 CreateOfferReceiveAnswer();
1619 EXPECT_EQ_WAIT(DataChannelInterface::kOpen, received_channel->state(),
1620 kTimeout);
1621}
1622
1623// This test that no data channel is returned if a reliable channel is
1624// requested.
1625// TODO(perkj): Remove this test once reliable channels are implemented.
1626TEST_F(PeerConnectionInterfaceTest, CreateReliableRtpDataChannelShouldFail) {
1627 FakeConstraints constraints;
1628 constraints.SetAllowRtpDataChannels();
1629 CreatePeerConnection(&constraints);
1630
1631 std::string label = "test";
1632 webrtc::DataChannelInit config;
1633 config.reliable = true;
1634 scoped_refptr<DataChannelInterface> channel =
1635 pc_->CreateDataChannel(label, &config);
1636 EXPECT_TRUE(channel == NULL);
1637}
1638
deadbeefab9b2d12015-10-14 11:33:11 -07001639// Verifies that duplicated label is not allowed for RTP data channel.
1640TEST_F(PeerConnectionInterfaceTest, RtpDuplicatedLabelNotAllowed) {
1641 FakeConstraints constraints;
1642 constraints.SetAllowRtpDataChannels();
1643 CreatePeerConnection(&constraints);
1644
1645 std::string label = "test";
1646 scoped_refptr<DataChannelInterface> channel =
1647 pc_->CreateDataChannel(label, nullptr);
1648 EXPECT_NE(channel, nullptr);
1649
1650 scoped_refptr<DataChannelInterface> dup_channel =
1651 pc_->CreateDataChannel(label, nullptr);
1652 EXPECT_EQ(dup_channel, nullptr);
1653}
1654
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001655// This tests that a SCTP data channel is returned using different
1656// DataChannelInit configurations.
1657TEST_F(PeerConnectionInterfaceTest, CreateSctpDataChannel) {
1658 FakeConstraints constraints;
1659 constraints.SetAllowDtlsSctpDataChannels();
1660 CreatePeerConnection(&constraints);
1661
1662 webrtc::DataChannelInit config;
1663
1664 scoped_refptr<DataChannelInterface> channel =
1665 pc_->CreateDataChannel("1", &config);
1666 EXPECT_TRUE(channel != NULL);
1667 EXPECT_TRUE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001668 EXPECT_TRUE(observer_.renegotiation_needed_);
1669 observer_.renegotiation_needed_ = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001670
1671 config.ordered = false;
1672 channel = pc_->CreateDataChannel("2", &config);
1673 EXPECT_TRUE(channel != NULL);
1674 EXPECT_TRUE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001675 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001676
1677 config.ordered = true;
1678 config.maxRetransmits = 0;
1679 channel = pc_->CreateDataChannel("3", &config);
1680 EXPECT_TRUE(channel != NULL);
1681 EXPECT_FALSE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001682 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001683
1684 config.maxRetransmits = -1;
1685 config.maxRetransmitTime = 0;
1686 channel = pc_->CreateDataChannel("4", &config);
1687 EXPECT_TRUE(channel != NULL);
1688 EXPECT_FALSE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001689 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001690}
1691
1692// This tests that no data channel is returned if both maxRetransmits and
1693// maxRetransmitTime are set for SCTP data channels.
1694TEST_F(PeerConnectionInterfaceTest,
1695 CreateSctpDataChannelShouldFailForInvalidConfig) {
1696 FakeConstraints constraints;
1697 constraints.SetAllowDtlsSctpDataChannels();
1698 CreatePeerConnection(&constraints);
1699
1700 std::string label = "test";
1701 webrtc::DataChannelInit config;
1702 config.maxRetransmits = 0;
1703 config.maxRetransmitTime = 0;
1704
1705 scoped_refptr<DataChannelInterface> channel =
1706 pc_->CreateDataChannel(label, &config);
1707 EXPECT_TRUE(channel == NULL);
1708}
1709
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001710// The test verifies that creating a SCTP data channel with an id already in use
1711// or out of range should fail.
1712TEST_F(PeerConnectionInterfaceTest,
1713 CreateSctpDataChannelWithInvalidIdShouldFail) {
1714 FakeConstraints constraints;
1715 constraints.SetAllowDtlsSctpDataChannels();
1716 CreatePeerConnection(&constraints);
1717
1718 webrtc::DataChannelInit config;
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001719 scoped_refptr<DataChannelInterface> channel;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001720
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001721 config.id = 1;
1722 channel = pc_->CreateDataChannel("1", &config);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001723 EXPECT_TRUE(channel != NULL);
1724 EXPECT_EQ(1, channel->id());
1725
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001726 channel = pc_->CreateDataChannel("x", &config);
1727 EXPECT_TRUE(channel == NULL);
1728
1729 config.id = cricket::kMaxSctpSid;
1730 channel = pc_->CreateDataChannel("max", &config);
1731 EXPECT_TRUE(channel != NULL);
1732 EXPECT_EQ(config.id, channel->id());
1733
1734 config.id = cricket::kMaxSctpSid + 1;
1735 channel = pc_->CreateDataChannel("x", &config);
1736 EXPECT_TRUE(channel == NULL);
1737}
1738
deadbeefab9b2d12015-10-14 11:33:11 -07001739// Verifies that duplicated label is allowed for SCTP data channel.
1740TEST_F(PeerConnectionInterfaceTest, SctpDuplicatedLabelAllowed) {
1741 FakeConstraints constraints;
1742 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
1743 true);
1744 CreatePeerConnection(&constraints);
1745
1746 std::string label = "test";
1747 scoped_refptr<DataChannelInterface> channel =
1748 pc_->CreateDataChannel(label, nullptr);
1749 EXPECT_NE(channel, nullptr);
1750
1751 scoped_refptr<DataChannelInterface> dup_channel =
1752 pc_->CreateDataChannel(label, nullptr);
1753 EXPECT_NE(dup_channel, nullptr);
1754}
1755
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001756// This test verifies that OnRenegotiationNeeded is fired for every new RTP
1757// DataChannel.
1758TEST_F(PeerConnectionInterfaceTest, RenegotiationNeededForNewRtpDataChannel) {
1759 FakeConstraints constraints;
1760 constraints.SetAllowRtpDataChannels();
1761 CreatePeerConnection(&constraints);
1762
1763 scoped_refptr<DataChannelInterface> dc1 =
1764 pc_->CreateDataChannel("test1", NULL);
1765 EXPECT_TRUE(observer_.renegotiation_needed_);
1766 observer_.renegotiation_needed_ = false;
1767
1768 scoped_refptr<DataChannelInterface> dc2 =
1769 pc_->CreateDataChannel("test2", NULL);
1770 EXPECT_TRUE(observer_.renegotiation_needed_);
1771}
1772
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001773// This test that a data channel closes when a PeerConnection is deleted/closed.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001774TEST_F(PeerConnectionInterfaceTest, DataChannelCloseWhenPeerConnectionClose) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001775 FakeConstraints constraints;
1776 constraints.SetAllowRtpDataChannels();
1777 CreatePeerConnection(&constraints);
1778
1779 scoped_refptr<DataChannelInterface> data1 =
1780 pc_->CreateDataChannel("test1", NULL);
1781 scoped_refptr<DataChannelInterface> data2 =
1782 pc_->CreateDataChannel("test2", NULL);
1783 ASSERT_TRUE(data1 != NULL);
kwibergd1fe2812016-04-27 06:47:29 -07001784 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001785 new MockDataChannelObserver(data1));
kwibergd1fe2812016-04-27 06:47:29 -07001786 std::unique_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001787 new MockDataChannelObserver(data2));
1788
1789 CreateOfferReceiveAnswer();
1790 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1791 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
1792
1793 ReleasePeerConnection();
1794 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
1795 EXPECT_EQ(DataChannelInterface::kClosed, data2->state());
1796}
1797
1798// This test that data channels can be rejected in an answer.
1799TEST_F(PeerConnectionInterfaceTest, TestRejectDataChannelInAnswer) {
1800 FakeConstraints constraints;
1801 constraints.SetAllowRtpDataChannels();
1802 CreatePeerConnection(&constraints);
1803
1804 scoped_refptr<DataChannelInterface> offer_channel(
1805 pc_->CreateDataChannel("offer_channel", NULL));
1806
1807 CreateOfferAsLocalDescription();
1808
1809 // Create an answer where the m-line for data channels are rejected.
1810 std::string sdp;
1811 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
1812 webrtc::JsepSessionDescription* answer = new webrtc::JsepSessionDescription(
1813 SessionDescriptionInterface::kAnswer);
1814 EXPECT_TRUE(answer->Initialize(sdp, NULL));
1815 cricket::ContentInfo* data_info =
1816 answer->description()->GetContentByName("data");
1817 data_info->rejected = true;
1818
1819 DoSetRemoteDescription(answer);
1820 EXPECT_EQ(DataChannelInterface::kClosed, offer_channel->state());
1821}
1822
1823// Test that we can create a session description from an SDP string from
1824// FireFox, use it as a remote session description, generate an answer and use
1825// the answer as a local description.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07001826TEST_F(PeerConnectionInterfaceTest, ReceiveFireFoxOffer) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001827 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001828 FakeConstraints constraints;
1829 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
1830 true);
1831 CreatePeerConnection(&constraints);
1832 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
1833 SessionDescriptionInterface* desc =
1834 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
jbauchfabe2c92015-07-16 13:43:14 -07001835 webrtc::kFireFoxSdpOffer, nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001836 EXPECT_TRUE(DoSetSessionDescription(desc, false));
1837 CreateAnswerAsLocalDescription();
1838 ASSERT_TRUE(pc_->local_description() != NULL);
1839 ASSERT_TRUE(pc_->remote_description() != NULL);
1840
1841 const cricket::ContentInfo* content =
1842 cricket::GetFirstAudioContent(pc_->local_description()->description());
1843 ASSERT_TRUE(content != NULL);
1844 EXPECT_FALSE(content->rejected);
1845
1846 content =
1847 cricket::GetFirstVideoContent(pc_->local_description()->description());
1848 ASSERT_TRUE(content != NULL);
1849 EXPECT_FALSE(content->rejected);
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00001850#ifdef HAVE_SCTP
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001851 content =
1852 cricket::GetFirstDataContent(pc_->local_description()->description());
1853 ASSERT_TRUE(content != NULL);
1854 EXPECT_TRUE(content->rejected);
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00001855#endif
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001856}
1857
1858// Test that we can create an audio only offer and receive an answer with a
1859// limited set of audio codecs and receive an updated offer with more audio
1860// codecs, where the added codecs are not supported.
1861TEST_F(PeerConnectionInterfaceTest, ReceiveUpdatedAudioOfferWithBadCodecs) {
1862 CreatePeerConnection();
1863 AddVoiceStream("audio_label");
1864 CreateOfferAsLocalDescription();
1865
1866 SessionDescriptionInterface* answer =
1867 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
jbauchfabe2c92015-07-16 13:43:14 -07001868 webrtc::kAudioSdp, nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001869 EXPECT_TRUE(DoSetSessionDescription(answer, false));
1870
1871 SessionDescriptionInterface* updated_offer =
1872 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
jbauchfabe2c92015-07-16 13:43:14 -07001873 webrtc::kAudioSdpWithUnsupportedCodecs,
1874 nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001875 EXPECT_TRUE(DoSetSessionDescription(updated_offer, false));
1876 CreateAnswerAsLocalDescription();
1877}
1878
deadbeefc80741f2015-10-22 13:14:45 -07001879// Test that if we're receiving (but not sending) a track, subsequent offers
1880// will have m-lines with a=recvonly.
1881TEST_F(PeerConnectionInterfaceTest, CreateSubsequentRecvOnlyOffer) {
1882 FakeConstraints constraints;
1883 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
1884 true);
1885 CreatePeerConnection(&constraints);
1886 CreateAndSetRemoteOffer(kSdpStringWithStream1);
1887 CreateAnswerAsLocalDescription();
1888
1889 // At this point we should be receiving stream 1, but not sending anything.
1890 // A new offer should be recvonly.
kwibergd1fe2812016-04-27 06:47:29 -07001891 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefc80741f2015-10-22 13:14:45 -07001892 DoCreateOffer(&offer, nullptr);
1893
1894 const cricket::ContentInfo* video_content =
1895 cricket::GetFirstVideoContent(offer->description());
1896 const cricket::VideoContentDescription* video_desc =
1897 static_cast<const cricket::VideoContentDescription*>(
1898 video_content->description);
1899 ASSERT_EQ(cricket::MD_RECVONLY, video_desc->direction());
1900
1901 const cricket::ContentInfo* audio_content =
1902 cricket::GetFirstAudioContent(offer->description());
1903 const cricket::AudioContentDescription* audio_desc =
1904 static_cast<const cricket::AudioContentDescription*>(
1905 audio_content->description);
1906 ASSERT_EQ(cricket::MD_RECVONLY, audio_desc->direction());
1907}
1908
1909// Test that if we're receiving (but not sending) a track, and the
1910// offerToReceiveVideo/offerToReceiveAudio constraints are explicitly set to
1911// false, the generated m-lines will be a=inactive.
1912TEST_F(PeerConnectionInterfaceTest, CreateSubsequentInactiveOffer) {
1913 FakeConstraints constraints;
1914 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
1915 true);
1916 CreatePeerConnection(&constraints);
1917 CreateAndSetRemoteOffer(kSdpStringWithStream1);
1918 CreateAnswerAsLocalDescription();
1919
1920 // At this point we should be receiving stream 1, but not sending anything.
1921 // A new offer would be recvonly, but we'll set the "no receive" constraints
1922 // to make it inactive.
kwibergd1fe2812016-04-27 06:47:29 -07001923 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefc80741f2015-10-22 13:14:45 -07001924 FakeConstraints offer_constraints;
1925 offer_constraints.AddMandatory(
1926 webrtc::MediaConstraintsInterface::kOfferToReceiveVideo, false);
1927 offer_constraints.AddMandatory(
1928 webrtc::MediaConstraintsInterface::kOfferToReceiveAudio, false);
1929 DoCreateOffer(&offer, &offer_constraints);
1930
1931 const cricket::ContentInfo* video_content =
1932 cricket::GetFirstVideoContent(offer->description());
1933 const cricket::VideoContentDescription* video_desc =
1934 static_cast<const cricket::VideoContentDescription*>(
1935 video_content->description);
1936 ASSERT_EQ(cricket::MD_INACTIVE, video_desc->direction());
1937
1938 const cricket::ContentInfo* audio_content =
1939 cricket::GetFirstAudioContent(offer->description());
1940 const cricket::AudioContentDescription* audio_desc =
1941 static_cast<const cricket::AudioContentDescription*>(
1942 audio_content->description);
1943 ASSERT_EQ(cricket::MD_INACTIVE, audio_desc->direction());
1944}
1945
deadbeef653b8e02015-11-11 12:55:10 -08001946// Test that we can use SetConfiguration to change the ICE servers of the
1947// PortAllocator.
1948TEST_F(PeerConnectionInterfaceTest, SetConfigurationChangesIceServers) {
1949 CreatePeerConnection();
1950
1951 PeerConnectionInterface::RTCConfiguration config;
1952 PeerConnectionInterface::IceServer server;
1953 server.uri = "stun:test_hostname";
1954 config.servers.push_back(server);
1955 EXPECT_TRUE(pc_->SetConfiguration(config));
1956
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08001957 EXPECT_EQ(1u, port_allocator_->stun_servers().size());
1958 EXPECT_EQ("test_hostname",
1959 port_allocator_->stun_servers().begin()->hostname());
deadbeef653b8e02015-11-11 12:55:10 -08001960}
1961
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001962TEST_F(PeerConnectionInterfaceTest, SetConfigurationChangesCandidateFilter) {
1963 CreatePeerConnection();
1964 PeerConnectionInterface::RTCConfiguration config;
1965 config.type = PeerConnectionInterface::kRelay;
1966 EXPECT_TRUE(pc_->SetConfiguration(config));
1967 EXPECT_EQ(cricket::CF_RELAY, port_allocator_->candidate_filter());
1968}
1969
1970// Test that when SetConfiguration changes both the pool size and other
1971// attributes, the pooled session is created with the updated attributes.
1972TEST_F(PeerConnectionInterfaceTest,
1973 SetConfigurationCreatesPooledSessionCorrectly) {
1974 CreatePeerConnection();
1975 PeerConnectionInterface::RTCConfiguration config;
1976 config.ice_candidate_pool_size = 1;
1977 PeerConnectionInterface::IceServer server;
1978 server.uri = kStunAddressOnly;
1979 config.servers.push_back(server);
1980 config.type = PeerConnectionInterface::kRelay;
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001981 EXPECT_TRUE(pc_->SetConfiguration(config));
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001982
1983 const cricket::FakePortAllocatorSession* session =
1984 static_cast<const cricket::FakePortAllocatorSession*>(
1985 port_allocator_->GetPooledSession());
1986 ASSERT_NE(nullptr, session);
1987 EXPECT_EQ(1UL, session->stun_servers().size());
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001988}
1989
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001990// Test that PeerConnection::Close changes the states to closed and all remote
1991// tracks change state to ended.
1992TEST_F(PeerConnectionInterfaceTest, CloseAndTestStreamsAndStates) {
1993 // Initialize a PeerConnection and negotiate local and remote session
1994 // description.
1995 InitiateCall();
1996 ASSERT_EQ(1u, pc_->local_streams()->count());
1997 ASSERT_EQ(1u, pc_->remote_streams()->count());
1998
1999 pc_->Close();
2000
2001 EXPECT_EQ(PeerConnectionInterface::kClosed, pc_->signaling_state());
2002 EXPECT_EQ(PeerConnectionInterface::kIceConnectionClosed,
2003 pc_->ice_connection_state());
2004 EXPECT_EQ(PeerConnectionInterface::kIceGatheringComplete,
2005 pc_->ice_gathering_state());
2006
2007 EXPECT_EQ(1u, pc_->local_streams()->count());
2008 EXPECT_EQ(1u, pc_->remote_streams()->count());
2009
2010 scoped_refptr<MediaStreamInterface> remote_stream =
2011 pc_->remote_streams()->at(0);
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002012 // Track state may be updated asynchronously.
perkjd61bf802016-03-24 03:16:19 -07002013 EXPECT_EQ_WAIT(MediaStreamTrackInterface::kEnded,
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002014 remote_stream->GetAudioTracks()[0]->state(), kTimeout);
2015 EXPECT_EQ_WAIT(MediaStreamTrackInterface::kEnded,
2016 remote_stream->GetVideoTracks()[0]->state(), kTimeout);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002017}
2018
2019// Test that PeerConnection methods fails gracefully after
2020// PeerConnection::Close has been called.
2021TEST_F(PeerConnectionInterfaceTest, CloseAndTestMethods) {
2022 CreatePeerConnection();
2023 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
2024 CreateOfferAsRemoteDescription();
2025 CreateAnswerAsLocalDescription();
2026
2027 ASSERT_EQ(1u, pc_->local_streams()->count());
2028 scoped_refptr<MediaStreamInterface> local_stream =
2029 pc_->local_streams()->at(0);
2030
2031 pc_->Close();
2032
2033 pc_->RemoveStream(local_stream);
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +00002034 EXPECT_FALSE(pc_->AddStream(local_stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002035
2036 ASSERT_FALSE(local_stream->GetAudioTracks().empty());
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002037 rtc::scoped_refptr<webrtc::DtmfSenderInterface> dtmf_sender(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002038 pc_->CreateDtmfSender(local_stream->GetAudioTracks()[0]));
wu@webrtc.org66037362013-08-13 00:09:35 +00002039 EXPECT_TRUE(NULL == dtmf_sender); // local stream has been removed.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002040
2041 EXPECT_TRUE(pc_->CreateDataChannel("test", NULL) == NULL);
2042
2043 EXPECT_TRUE(pc_->local_description() != NULL);
2044 EXPECT_TRUE(pc_->remote_description() != NULL);
2045
kwibergd1fe2812016-04-27 06:47:29 -07002046 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07002047 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
kwibergd1fe2812016-04-27 06:47:29 -07002048 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -07002049 EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002050
2051 std::string sdp;
2052 ASSERT_TRUE(pc_->remote_description()->ToString(&sdp));
2053 SessionDescriptionInterface* remote_offer =
2054 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
2055 sdp, NULL);
2056 EXPECT_FALSE(DoSetRemoteDescription(remote_offer));
2057
2058 ASSERT_TRUE(pc_->local_description()->ToString(&sdp));
2059 SessionDescriptionInterface* local_offer =
2060 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
2061 sdp, NULL);
2062 EXPECT_FALSE(DoSetLocalDescription(local_offer));
2063}
2064
2065// Test that GetStats can still be called after PeerConnection::Close.
2066TEST_F(PeerConnectionInterfaceTest, CloseAndGetStats) {
2067 InitiateCall();
2068 pc_->Close();
2069 DoGetStats(NULL);
2070}
deadbeefab9b2d12015-10-14 11:33:11 -07002071
2072// NOTE: The series of tests below come from what used to be
2073// mediastreamsignaling_unittest.cc, and are mostly aimed at testing that
2074// setting a remote or local description has the expected effects.
2075
2076// This test verifies that the remote MediaStreams corresponding to a received
2077// SDP string is created. In this test the two separate MediaStreams are
2078// signaled.
2079TEST_F(PeerConnectionInterfaceTest, UpdateRemoteStreams) {
2080 FakeConstraints constraints;
2081 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2082 true);
2083 CreatePeerConnection(&constraints);
2084 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2085
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002086 rtc::scoped_refptr<StreamCollection> reference(CreateStreamCollection(1, 1));
deadbeefab9b2d12015-10-14 11:33:11 -07002087 EXPECT_TRUE(
2088 CompareStreamCollections(observer_.remote_streams(), reference.get()));
2089 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2090 EXPECT_TRUE(remote_stream->GetVideoTracks()[0]->GetSource() != nullptr);
2091
2092 // Create a session description based on another SDP with another
2093 // MediaStream.
2094 CreateAndSetRemoteOffer(kSdpStringWithStream1And2);
2095
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002096 rtc::scoped_refptr<StreamCollection> reference2(CreateStreamCollection(2, 1));
deadbeefab9b2d12015-10-14 11:33:11 -07002097 EXPECT_TRUE(
2098 CompareStreamCollections(observer_.remote_streams(), reference2.get()));
2099}
2100
2101// This test verifies that when remote tracks are added/removed from SDP, the
2102// created remote streams are updated appropriately.
2103TEST_F(PeerConnectionInterfaceTest,
2104 AddRemoveTrackFromExistingRemoteMediaStream) {
2105 FakeConstraints constraints;
2106 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2107 true);
2108 CreatePeerConnection(&constraints);
kwibergd1fe2812016-04-27 06:47:29 -07002109 std::unique_ptr<SessionDescriptionInterface> desc_ms1 =
kwiberg2bbff992016-03-16 11:03:04 -07002110 CreateSessionDescriptionAndReference(1, 1);
deadbeefab9b2d12015-10-14 11:33:11 -07002111 EXPECT_TRUE(DoSetRemoteDescription(desc_ms1.release()));
2112 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
2113 reference_collection_));
2114
2115 // Add extra audio and video tracks to the same MediaStream.
kwibergd1fe2812016-04-27 06:47:29 -07002116 std::unique_ptr<SessionDescriptionInterface> desc_ms1_two_tracks =
kwiberg2bbff992016-03-16 11:03:04 -07002117 CreateSessionDescriptionAndReference(2, 2);
deadbeefab9b2d12015-10-14 11:33:11 -07002118 EXPECT_TRUE(DoSetRemoteDescription(desc_ms1_two_tracks.release()));
2119 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
2120 reference_collection_));
perkjd61bf802016-03-24 03:16:19 -07002121 scoped_refptr<AudioTrackInterface> audio_track2 =
2122 observer_.remote_streams()->at(0)->GetAudioTracks()[1];
2123 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, audio_track2->state());
2124 scoped_refptr<VideoTrackInterface> video_track2 =
2125 observer_.remote_streams()->at(0)->GetVideoTracks()[1];
2126 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, video_track2->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002127
2128 // Remove the extra audio and video tracks.
kwibergd1fe2812016-04-27 06:47:29 -07002129 std::unique_ptr<SessionDescriptionInterface> desc_ms2 =
kwiberg2bbff992016-03-16 11:03:04 -07002130 CreateSessionDescriptionAndReference(1, 1);
perkjd61bf802016-03-24 03:16:19 -07002131 MockTrackObserver audio_track_observer(audio_track2);
2132 MockTrackObserver video_track_observer(video_track2);
2133
2134 EXPECT_CALL(audio_track_observer, OnChanged()).Times(Exactly(1));
2135 EXPECT_CALL(video_track_observer, OnChanged()).Times(Exactly(1));
deadbeefab9b2d12015-10-14 11:33:11 -07002136 EXPECT_TRUE(DoSetRemoteDescription(desc_ms2.release()));
2137 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
2138 reference_collection_));
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002139 // Track state may be updated asynchronously.
perkjd61bf802016-03-24 03:16:19 -07002140 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002141 audio_track2->state(), kTimeout);
2142 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
2143 video_track2->state(), kTimeout);
deadbeefab9b2d12015-10-14 11:33:11 -07002144}
2145
2146// This tests that remote tracks are ended if a local session description is set
2147// that rejects the media content type.
2148TEST_F(PeerConnectionInterfaceTest, RejectMediaContent) {
2149 FakeConstraints constraints;
2150 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2151 true);
2152 CreatePeerConnection(&constraints);
2153 // First create and set a remote offer, then reject its video content in our
2154 // answer.
2155 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2156 ASSERT_EQ(1u, observer_.remote_streams()->count());
2157 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2158 ASSERT_EQ(1u, remote_stream->GetVideoTracks().size());
2159 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2160
2161 rtc::scoped_refptr<webrtc::VideoTrackInterface> remote_video =
2162 remote_stream->GetVideoTracks()[0];
2163 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, remote_video->state());
2164 rtc::scoped_refptr<webrtc::AudioTrackInterface> remote_audio =
2165 remote_stream->GetAudioTracks()[0];
2166 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, remote_audio->state());
2167
kwibergd1fe2812016-04-27 06:47:29 -07002168 std::unique_ptr<SessionDescriptionInterface> local_answer;
kwiberg2bbff992016-03-16 11:03:04 -07002169 EXPECT_TRUE(DoCreateAnswer(&local_answer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07002170 cricket::ContentInfo* video_info =
2171 local_answer->description()->GetContentByName("video");
2172 video_info->rejected = true;
2173 EXPECT_TRUE(DoSetLocalDescription(local_answer.release()));
2174 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kEnded, remote_video->state());
2175 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, remote_audio->state());
2176
2177 // Now create an offer where we reject both video and audio.
kwibergd1fe2812016-04-27 06:47:29 -07002178 std::unique_ptr<SessionDescriptionInterface> local_offer;
kwiberg2bbff992016-03-16 11:03:04 -07002179 EXPECT_TRUE(DoCreateOffer(&local_offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07002180 video_info = local_offer->description()->GetContentByName("video");
2181 ASSERT_TRUE(video_info != nullptr);
2182 video_info->rejected = true;
2183 cricket::ContentInfo* audio_info =
2184 local_offer->description()->GetContentByName("audio");
2185 ASSERT_TRUE(audio_info != nullptr);
2186 audio_info->rejected = true;
2187 EXPECT_TRUE(DoSetLocalDescription(local_offer.release()));
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002188 // Track state may be updated asynchronously.
perkjd61bf802016-03-24 03:16:19 -07002189 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002190 remote_audio->state(), kTimeout);
2191 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
2192 remote_video->state(), kTimeout);
deadbeefab9b2d12015-10-14 11:33:11 -07002193}
2194
2195// This tests that we won't crash if the remote track has been removed outside
2196// of PeerConnection and then PeerConnection tries to reject the track.
2197TEST_F(PeerConnectionInterfaceTest, RemoveTrackThenRejectMediaContent) {
2198 FakeConstraints constraints;
2199 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2200 true);
2201 CreatePeerConnection(&constraints);
2202 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2203 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2204 remote_stream->RemoveTrack(remote_stream->GetVideoTracks()[0]);
2205 remote_stream->RemoveTrack(remote_stream->GetAudioTracks()[0]);
2206
kwibergd1fe2812016-04-27 06:47:29 -07002207 std::unique_ptr<SessionDescriptionInterface> local_answer(
deadbeefab9b2d12015-10-14 11:33:11 -07002208 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
2209 kSdpStringWithStream1, nullptr));
2210 cricket::ContentInfo* video_info =
2211 local_answer->description()->GetContentByName("video");
2212 video_info->rejected = true;
2213 cricket::ContentInfo* audio_info =
2214 local_answer->description()->GetContentByName("audio");
2215 audio_info->rejected = true;
2216 EXPECT_TRUE(DoSetLocalDescription(local_answer.release()));
2217
2218 // No crash is a pass.
2219}
2220
deadbeef5e97fb52015-10-15 12:49:08 -07002221// This tests that if a recvonly remote description is set, no remote streams
2222// will be created, even if the description contains SSRCs/MSIDs.
2223// See: https://code.google.com/p/webrtc/issues/detail?id=5054
2224TEST_F(PeerConnectionInterfaceTest, RecvonlyDescriptionDoesntCreateStream) {
2225 FakeConstraints constraints;
2226 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2227 true);
2228 CreatePeerConnection(&constraints);
2229
2230 std::string recvonly_offer = kSdpStringWithStream1;
2231 rtc::replace_substrs(kSendrecv, strlen(kSendrecv), kRecvonly,
2232 strlen(kRecvonly), &recvonly_offer);
2233 CreateAndSetRemoteOffer(recvonly_offer);
2234
2235 EXPECT_EQ(0u, observer_.remote_streams()->count());
2236}
2237
deadbeefab9b2d12015-10-14 11:33:11 -07002238// This tests that a default MediaStream is created if a remote session
2239// description doesn't contain any streams and no MSID support.
2240// It also tests that the default stream is updated if a video m-line is added
2241// in a subsequent session description.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002242TEST_F(PeerConnectionInterfaceTest, SdpWithoutMsidCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002243 FakeConstraints constraints;
2244 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2245 true);
2246 CreatePeerConnection(&constraints);
2247 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2248
2249 ASSERT_EQ(1u, observer_.remote_streams()->count());
2250 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2251
2252 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2253 EXPECT_EQ(0u, remote_stream->GetVideoTracks().size());
2254 EXPECT_EQ("default", remote_stream->label());
2255
2256 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2257 ASSERT_EQ(1u, observer_.remote_streams()->count());
2258 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2259 EXPECT_EQ("defaulta0", remote_stream->GetAudioTracks()[0]->id());
deadbeef884f5852016-01-15 09:20:04 -08002260 EXPECT_EQ(MediaStreamTrackInterface::kLive,
2261 remote_stream->GetAudioTracks()[0]->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002262 ASSERT_EQ(1u, remote_stream->GetVideoTracks().size());
2263 EXPECT_EQ("defaultv0", remote_stream->GetVideoTracks()[0]->id());
deadbeef884f5852016-01-15 09:20:04 -08002264 EXPECT_EQ(MediaStreamTrackInterface::kLive,
2265 remote_stream->GetVideoTracks()[0]->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002266}
2267
2268// This tests that a default MediaStream is created if a remote session
2269// description doesn't contain any streams and media direction is send only.
2270TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002271 SendOnlySdpWithoutMsidCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002272 FakeConstraints constraints;
2273 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2274 true);
2275 CreatePeerConnection(&constraints);
2276 CreateAndSetRemoteOffer(kSdpStringSendOnlyWithoutStreams);
2277
2278 ASSERT_EQ(1u, observer_.remote_streams()->count());
2279 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2280
2281 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2282 EXPECT_EQ(1u, remote_stream->GetVideoTracks().size());
2283 EXPECT_EQ("default", remote_stream->label());
2284}
2285
2286// This tests that it won't crash when PeerConnection tries to remove
2287// a remote track that as already been removed from the MediaStream.
2288TEST_F(PeerConnectionInterfaceTest, RemoveAlreadyGoneRemoteStream) {
2289 FakeConstraints constraints;
2290 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2291 true);
2292 CreatePeerConnection(&constraints);
2293 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2294 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2295 remote_stream->RemoveTrack(remote_stream->GetAudioTracks()[0]);
2296 remote_stream->RemoveTrack(remote_stream->GetVideoTracks()[0]);
2297
2298 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2299
2300 // No crash is a pass.
2301}
2302
2303// This tests that a default MediaStream is created if the remote session
2304// description doesn't contain any streams and don't contain an indication if
2305// MSID is supported.
2306TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002307 SdpWithoutMsidAndStreamsCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002308 FakeConstraints constraints;
2309 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2310 true);
2311 CreatePeerConnection(&constraints);
2312 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2313
2314 ASSERT_EQ(1u, observer_.remote_streams()->count());
2315 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2316 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2317 EXPECT_EQ(1u, remote_stream->GetVideoTracks().size());
2318}
2319
2320// This tests that a default MediaStream is not created if the remote session
2321// description doesn't contain any streams but does support MSID.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002322TEST_F(PeerConnectionInterfaceTest, SdpWithMsidDontCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002323 FakeConstraints constraints;
2324 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2325 true);
2326 CreatePeerConnection(&constraints);
2327 CreateAndSetRemoteOffer(kSdpStringWithMsidWithoutStreams);
2328 EXPECT_EQ(0u, observer_.remote_streams()->count());
2329}
2330
deadbeefbda7e0b2015-12-08 17:13:40 -08002331// This tests that when setting a new description, the old default tracks are
2332// not destroyed and recreated.
2333// See: https://bugs.chromium.org/p/webrtc/issues/detail?id=5250
Stefan Holmer102362b2016-03-18 09:39:07 +01002334TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002335 DefaultTracksNotDestroyedAndRecreated) {
deadbeefbda7e0b2015-12-08 17:13:40 -08002336 FakeConstraints constraints;
2337 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2338 true);
2339 CreatePeerConnection(&constraints);
2340 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2341
2342 ASSERT_EQ(1u, observer_.remote_streams()->count());
2343 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2344 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2345
2346 // Set the track to "disabled", then set a new description and ensure the
2347 // track is still disabled, which ensures it hasn't been recreated.
2348 remote_stream->GetAudioTracks()[0]->set_enabled(false);
2349 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2350 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2351 EXPECT_FALSE(remote_stream->GetAudioTracks()[0]->enabled());
2352}
2353
deadbeefab9b2d12015-10-14 11:33:11 -07002354// This tests that a default MediaStream is not created if a remote session
2355// description is updated to not have any MediaStreams.
2356TEST_F(PeerConnectionInterfaceTest, VerifyDefaultStreamIsNotCreated) {
2357 FakeConstraints constraints;
2358 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2359 true);
2360 CreatePeerConnection(&constraints);
2361 CreateAndSetRemoteOffer(kSdpStringWithStream1);
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002362 rtc::scoped_refptr<StreamCollection> reference(CreateStreamCollection(1, 1));
deadbeefab9b2d12015-10-14 11:33:11 -07002363 EXPECT_TRUE(
2364 CompareStreamCollections(observer_.remote_streams(), reference.get()));
2365
2366 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2367 EXPECT_EQ(0u, observer_.remote_streams()->count());
2368}
2369
2370// This tests that an RtpSender is created when the local description is set
2371// after adding a local stream.
2372// TODO(deadbeef): This test and the one below it need to be updated when
2373// an RtpSender's lifetime isn't determined by when a local description is set.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002374TEST_F(PeerConnectionInterfaceTest, LocalDescriptionChanged) {
deadbeefab9b2d12015-10-14 11:33:11 -07002375 FakeConstraints constraints;
2376 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2377 true);
2378 CreatePeerConnection(&constraints);
deadbeefab9b2d12015-10-14 11:33:11 -07002379
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002380 // Create an offer with 1 stream with 2 tracks of each type.
2381 rtc::scoped_refptr<StreamCollection> stream_collection =
2382 CreateStreamCollection(1, 2);
2383 pc_->AddStream(stream_collection->at(0));
2384 std::unique_ptr<SessionDescriptionInterface> offer;
2385 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2386 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
deadbeefab9b2d12015-10-14 11:33:11 -07002387
deadbeefab9b2d12015-10-14 11:33:11 -07002388 auto senders = pc_->GetSenders();
2389 EXPECT_EQ(4u, senders.size());
2390 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2391 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2392 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[1]));
2393 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[1]));
2394
2395 // Remove an audio and video track.
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002396 pc_->RemoveStream(stream_collection->at(0));
2397 stream_collection = CreateStreamCollection(1, 1);
2398 pc_->AddStream(stream_collection->at(0));
2399 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2400 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
2401
deadbeefab9b2d12015-10-14 11:33:11 -07002402 senders = pc_->GetSenders();
2403 EXPECT_EQ(2u, senders.size());
2404 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2405 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2406 EXPECT_FALSE(ContainsSender(senders, kAudioTracks[1]));
2407 EXPECT_FALSE(ContainsSender(senders, kVideoTracks[1]));
2408}
2409
2410// This tests that an RtpSender is created when the local description is set
2411// before adding a local stream.
2412TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002413 AddLocalStreamAfterLocalDescriptionChanged) {
deadbeefab9b2d12015-10-14 11:33:11 -07002414 FakeConstraints constraints;
2415 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2416 true);
2417 CreatePeerConnection(&constraints);
deadbeefab9b2d12015-10-14 11:33:11 -07002418
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002419 rtc::scoped_refptr<StreamCollection> stream_collection =
2420 CreateStreamCollection(1, 2);
2421 // Add a stream to create the offer, but remove it afterwards.
2422 pc_->AddStream(stream_collection->at(0));
2423 std::unique_ptr<SessionDescriptionInterface> offer;
2424 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2425 pc_->RemoveStream(stream_collection->at(0));
deadbeefab9b2d12015-10-14 11:33:11 -07002426
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002427 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
deadbeefab9b2d12015-10-14 11:33:11 -07002428 auto senders = pc_->GetSenders();
2429 EXPECT_EQ(0u, senders.size());
2430
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002431 pc_->AddStream(stream_collection->at(0));
deadbeefab9b2d12015-10-14 11:33:11 -07002432 senders = pc_->GetSenders();
2433 EXPECT_EQ(4u, senders.size());
2434 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2435 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2436 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[1]));
2437 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[1]));
2438}
2439
2440// This tests that the expected behavior occurs if the SSRC on a local track is
2441// changed when SetLocalDescription is called.
2442TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002443 ChangeSsrcOnTrackInLocalSessionDescription) {
deadbeefab9b2d12015-10-14 11:33:11 -07002444 FakeConstraints constraints;
2445 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2446 true);
2447 CreatePeerConnection(&constraints);
deadbeefab9b2d12015-10-14 11:33:11 -07002448
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002449 rtc::scoped_refptr<StreamCollection> stream_collection =
2450 CreateStreamCollection(2, 1);
2451 pc_->AddStream(stream_collection->at(0));
2452 std::unique_ptr<SessionDescriptionInterface> offer;
2453 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2454 // Grab a copy of the offer before it gets passed into the PC.
2455 std::unique_ptr<JsepSessionDescription> modified_offer(
2456 new JsepSessionDescription(JsepSessionDescription::kOffer));
2457 modified_offer->Initialize(offer->description()->Copy(), offer->session_id(),
2458 offer->session_version());
2459 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
deadbeefab9b2d12015-10-14 11:33:11 -07002460
deadbeefab9b2d12015-10-14 11:33:11 -07002461 auto senders = pc_->GetSenders();
2462 EXPECT_EQ(2u, senders.size());
2463 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2464 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2465
2466 // Change the ssrc of the audio and video track.
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002467 cricket::MediaContentDescription* desc =
2468 cricket::GetFirstAudioContentDescription(modified_offer->description());
2469 ASSERT_TRUE(desc != NULL);
2470 for (StreamParams& stream : desc->mutable_streams()) {
2471 for (unsigned int& ssrc : stream.ssrcs) {
2472 ++ssrc;
2473 }
2474 }
deadbeefab9b2d12015-10-14 11:33:11 -07002475
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002476 desc =
2477 cricket::GetFirstVideoContentDescription(modified_offer->description());
2478 ASSERT_TRUE(desc != NULL);
2479 for (StreamParams& stream : desc->mutable_streams()) {
2480 for (unsigned int& ssrc : stream.ssrcs) {
2481 ++ssrc;
2482 }
2483 }
2484
2485 EXPECT_TRUE(DoSetLocalDescription(modified_offer.release()));
deadbeefab9b2d12015-10-14 11:33:11 -07002486 senders = pc_->GetSenders();
2487 EXPECT_EQ(2u, senders.size());
2488 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2489 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2490 // TODO(deadbeef): Once RtpSenders expose parameters, check that the SSRC
2491 // changed.
2492}
2493
2494// This tests that the expected behavior occurs if a new session description is
2495// set with the same tracks, but on a different MediaStream.
Stefan Holmer55d6e7c2016-03-17 16:26:40 +01002496TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002497 SignalSameTracksInSeparateMediaStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002498 FakeConstraints constraints;
2499 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2500 true);
2501 CreatePeerConnection(&constraints);
deadbeefab9b2d12015-10-14 11:33:11 -07002502
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002503 rtc::scoped_refptr<StreamCollection> stream_collection =
2504 CreateStreamCollection(2, 1);
2505 pc_->AddStream(stream_collection->at(0));
2506 std::unique_ptr<SessionDescriptionInterface> offer;
2507 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2508 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
deadbeefab9b2d12015-10-14 11:33:11 -07002509
deadbeefab9b2d12015-10-14 11:33:11 -07002510 auto senders = pc_->GetSenders();
2511 EXPECT_EQ(2u, senders.size());
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002512 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0], kStreams[0]));
2513 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0], kStreams[0]));
deadbeefab9b2d12015-10-14 11:33:11 -07002514
2515 // Add a new MediaStream but with the same tracks as in the first stream.
2516 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream_1(
2517 webrtc::MediaStream::Create(kStreams[1]));
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002518 stream_1->AddTrack(stream_collection->at(0)->GetVideoTracks()[0]);
2519 stream_1->AddTrack(stream_collection->at(0)->GetAudioTracks()[0]);
deadbeefab9b2d12015-10-14 11:33:11 -07002520 pc_->AddStream(stream_1);
2521
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002522 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2523 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
deadbeefab9b2d12015-10-14 11:33:11 -07002524
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002525 auto new_senders = pc_->GetSenders();
2526 // Should be the same senders as before, but with updated stream id.
2527 // Note that this behavior is subject to change in the future.
2528 // We may decide the PC should ignore existing tracks in AddStream.
2529 EXPECT_EQ(senders, new_senders);
2530 EXPECT_TRUE(ContainsSender(new_senders, kAudioTracks[0], kStreams[1]));
2531 EXPECT_TRUE(ContainsSender(new_senders, kVideoTracks[0], kStreams[1]));
deadbeefab9b2d12015-10-14 11:33:11 -07002532}
2533
nisse51542be2016-02-12 02:27:06 -08002534// The PeerConnectionMediaConfig tests below verify that configuration
2535// and constraints are propagated into the MediaConfig passed to
2536// CreateMediaController. These settings are intended for MediaChannel
2537// constructors, but that is not exercised by these unittest.
2538class PeerConnectionFactoryForTest : public webrtc::PeerConnectionFactory {
2539 public:
2540 webrtc::MediaControllerInterface* CreateMediaController(
2541 const cricket::MediaConfig& config) const override {
2542 create_media_controller_called_ = true;
2543 create_media_controller_config_ = config;
2544
2545 webrtc::MediaControllerInterface* mc =
2546 PeerConnectionFactory::CreateMediaController(config);
2547 EXPECT_TRUE(mc != nullptr);
2548 return mc;
2549 }
2550
2551 // Mutable, so they can be modified in the above const-declared method.
2552 mutable bool create_media_controller_called_ = false;
2553 mutable cricket::MediaConfig create_media_controller_config_;
2554};
2555
2556class PeerConnectionMediaConfigTest : public testing::Test {
2557 protected:
2558 void SetUp() override {
nisseaf510af2016-03-21 08:20:42 -07002559 pcf_ = new rtc::RefCountedObject<PeerConnectionFactoryForTest>();
nisse51542be2016-02-12 02:27:06 -08002560 pcf_->Initialize();
2561 }
2562 const cricket::MediaConfig& TestCreatePeerConnection(
2563 const PeerConnectionInterface::RTCConfiguration& config,
2564 const MediaConstraintsInterface *constraints) {
2565 pcf_->create_media_controller_called_ = false;
2566
2567 scoped_refptr<PeerConnectionInterface> pc(
2568 pcf_->CreatePeerConnection(config, constraints, nullptr, nullptr,
2569 &observer_));
2570 EXPECT_TRUE(pc.get());
2571 EXPECT_TRUE(pcf_->create_media_controller_called_);
2572 return pcf_->create_media_controller_config_;
2573 }
2574
2575 scoped_refptr<PeerConnectionFactoryForTest> pcf_;
2576 MockPeerConnectionObserver observer_;
2577};
2578
2579// This test verifies the default behaviour with no constraints and a
2580// default RTCConfiguration.
2581TEST_F(PeerConnectionMediaConfigTest, TestDefaults) {
2582 PeerConnectionInterface::RTCConfiguration config;
2583 FakeConstraints constraints;
2584
2585 const cricket::MediaConfig& media_config =
2586 TestCreatePeerConnection(config, &constraints);
2587
2588 EXPECT_FALSE(media_config.enable_dscp);
nisse0db023a2016-03-01 04:29:59 -08002589 EXPECT_TRUE(media_config.video.enable_cpu_overuse_detection);
2590 EXPECT_FALSE(media_config.video.disable_prerenderer_smoothing);
2591 EXPECT_FALSE(media_config.video.suspend_below_min_bitrate);
nisse51542be2016-02-12 02:27:06 -08002592}
2593
2594// This test verifies the DSCP constraint is recognized and passed to
2595// the CreateMediaController call.
2596TEST_F(PeerConnectionMediaConfigTest, TestDscpConstraintTrue) {
2597 PeerConnectionInterface::RTCConfiguration config;
2598 FakeConstraints constraints;
2599
2600 constraints.AddOptional(webrtc::MediaConstraintsInterface::kEnableDscp, true);
2601 const cricket::MediaConfig& media_config =
2602 TestCreatePeerConnection(config, &constraints);
2603
2604 EXPECT_TRUE(media_config.enable_dscp);
2605}
2606
2607// This test verifies the cpu overuse detection constraint is
2608// recognized and passed to the CreateMediaController call.
2609TEST_F(PeerConnectionMediaConfigTest, TestCpuOveruseConstraintFalse) {
2610 PeerConnectionInterface::RTCConfiguration config;
2611 FakeConstraints constraints;
2612
2613 constraints.AddOptional(
2614 webrtc::MediaConstraintsInterface::kCpuOveruseDetection, false);
2615 const cricket::MediaConfig media_config =
2616 TestCreatePeerConnection(config, &constraints);
2617
nisse0db023a2016-03-01 04:29:59 -08002618 EXPECT_FALSE(media_config.video.enable_cpu_overuse_detection);
nisse51542be2016-02-12 02:27:06 -08002619}
2620
2621// This test verifies that the disable_prerenderer_smoothing flag is
2622// propagated from RTCConfiguration to the CreateMediaController call.
2623TEST_F(PeerConnectionMediaConfigTest, TestDisablePrerendererSmoothingTrue) {
2624 PeerConnectionInterface::RTCConfiguration config;
2625 FakeConstraints constraints;
2626
Niels Möller71bdda02016-03-31 12:59:59 +02002627 config.set_prerenderer_smoothing(false);
nisse51542be2016-02-12 02:27:06 -08002628 const cricket::MediaConfig& media_config =
2629 TestCreatePeerConnection(config, &constraints);
2630
nisse0db023a2016-03-01 04:29:59 -08002631 EXPECT_TRUE(media_config.video.disable_prerenderer_smoothing);
2632}
2633
2634// This test verifies the suspend below min bitrate constraint is
2635// recognized and passed to the CreateMediaController call.
2636TEST_F(PeerConnectionMediaConfigTest,
2637 TestSuspendBelowMinBitrateConstraintTrue) {
2638 PeerConnectionInterface::RTCConfiguration config;
2639 FakeConstraints constraints;
2640
2641 constraints.AddOptional(
2642 webrtc::MediaConstraintsInterface::kEnableVideoSuspendBelowMinBitrate,
2643 true);
2644 const cricket::MediaConfig media_config =
2645 TestCreatePeerConnection(config, &constraints);
2646
2647 EXPECT_TRUE(media_config.video.suspend_below_min_bitrate);
nisse51542be2016-02-12 02:27:06 -08002648}
2649
deadbeefab9b2d12015-10-14 11:33:11 -07002650// The following tests verify that session options are created correctly.
deadbeefc80741f2015-10-22 13:14:45 -07002651// TODO(deadbeef): Convert these tests to be more end-to-end. Instead of
2652// "verify options are converted correctly", should be "pass options into
2653// CreateOffer and verify the correct offer is produced."
deadbeefab9b2d12015-10-14 11:33:11 -07002654
2655TEST(CreateSessionOptionsTest, GetOptionsForOfferWithInvalidAudioOption) {
2656 RTCOfferAnswerOptions rtc_options;
2657 rtc_options.offer_to_receive_audio = RTCOfferAnswerOptions::kUndefined - 1;
2658
2659 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08002660 EXPECT_FALSE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002661
2662 rtc_options.offer_to_receive_audio =
2663 RTCOfferAnswerOptions::kMaxOfferToReceiveMedia + 1;
htaaac2dea2016-03-10 13:35:55 -08002664 EXPECT_FALSE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002665}
2666
2667TEST(CreateSessionOptionsTest, GetOptionsForOfferWithInvalidVideoOption) {
2668 RTCOfferAnswerOptions rtc_options;
2669 rtc_options.offer_to_receive_video = RTCOfferAnswerOptions::kUndefined - 1;
2670
2671 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08002672 EXPECT_FALSE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002673
2674 rtc_options.offer_to_receive_video =
2675 RTCOfferAnswerOptions::kMaxOfferToReceiveMedia + 1;
htaaac2dea2016-03-10 13:35:55 -08002676 EXPECT_FALSE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002677}
2678
2679// Test that a MediaSessionOptions is created for an offer if
deadbeefc80741f2015-10-22 13:14:45 -07002680// OfferToReceiveAudio and OfferToReceiveVideo options are set.
deadbeefab9b2d12015-10-14 11:33:11 -07002681TEST(CreateSessionOptionsTest, GetMediaSessionOptionsForOfferWithAudioVideo) {
2682 RTCOfferAnswerOptions rtc_options;
2683 rtc_options.offer_to_receive_audio = 1;
2684 rtc_options.offer_to_receive_video = 1;
2685
2686 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08002687 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002688 EXPECT_TRUE(options.has_audio());
2689 EXPECT_TRUE(options.has_video());
2690 EXPECT_TRUE(options.bundle_enabled);
2691}
2692
2693// Test that a correct MediaSessionOptions is created for an offer if
deadbeefc80741f2015-10-22 13:14:45 -07002694// OfferToReceiveAudio is set.
deadbeefab9b2d12015-10-14 11:33:11 -07002695TEST(CreateSessionOptionsTest, GetMediaSessionOptionsForOfferWithAudio) {
2696 RTCOfferAnswerOptions rtc_options;
2697 rtc_options.offer_to_receive_audio = 1;
2698
2699 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08002700 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002701 EXPECT_TRUE(options.has_audio());
2702 EXPECT_FALSE(options.has_video());
2703 EXPECT_TRUE(options.bundle_enabled);
2704}
2705
2706// Test that a correct MediaSessionOptions is created for an offer if
deadbeefc80741f2015-10-22 13:14:45 -07002707// the default OfferOptions are used.
deadbeefab9b2d12015-10-14 11:33:11 -07002708TEST(CreateSessionOptionsTest, GetDefaultMediaSessionOptionsForOffer) {
2709 RTCOfferAnswerOptions rtc_options;
2710
2711 cricket::MediaSessionOptions options;
deadbeef0ed85b22016-02-23 17:24:52 -08002712 options.transport_options["audio"] = cricket::TransportOptions();
2713 options.transport_options["video"] = cricket::TransportOptions();
htaaac2dea2016-03-10 13:35:55 -08002714 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefc80741f2015-10-22 13:14:45 -07002715 EXPECT_TRUE(options.has_audio());
deadbeefab9b2d12015-10-14 11:33:11 -07002716 EXPECT_FALSE(options.has_video());
deadbeefc80741f2015-10-22 13:14:45 -07002717 EXPECT_TRUE(options.bundle_enabled);
deadbeefab9b2d12015-10-14 11:33:11 -07002718 EXPECT_TRUE(options.vad_enabled);
deadbeef0ed85b22016-02-23 17:24:52 -08002719 EXPECT_FALSE(options.transport_options["audio"].ice_restart);
2720 EXPECT_FALSE(options.transport_options["video"].ice_restart);
deadbeefab9b2d12015-10-14 11:33:11 -07002721}
2722
2723// Test that a correct MediaSessionOptions is created for an offer if
deadbeefc80741f2015-10-22 13:14:45 -07002724// OfferToReceiveVideo is set.
deadbeefab9b2d12015-10-14 11:33:11 -07002725TEST(CreateSessionOptionsTest, GetMediaSessionOptionsForOfferWithVideo) {
2726 RTCOfferAnswerOptions rtc_options;
2727 rtc_options.offer_to_receive_audio = 0;
2728 rtc_options.offer_to_receive_video = 1;
2729
2730 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08002731 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002732 EXPECT_FALSE(options.has_audio());
2733 EXPECT_TRUE(options.has_video());
2734 EXPECT_TRUE(options.bundle_enabled);
2735}
2736
2737// Test that a correct MediaSessionOptions is created for an offer if
2738// UseRtpMux is set to false.
2739TEST(CreateSessionOptionsTest,
2740 GetMediaSessionOptionsForOfferWithBundleDisabled) {
2741 RTCOfferAnswerOptions rtc_options;
2742 rtc_options.offer_to_receive_audio = 1;
2743 rtc_options.offer_to_receive_video = 1;
2744 rtc_options.use_rtp_mux = false;
2745
2746 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08002747 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002748 EXPECT_TRUE(options.has_audio());
2749 EXPECT_TRUE(options.has_video());
2750 EXPECT_FALSE(options.bundle_enabled);
2751}
2752
2753// Test that a correct MediaSessionOptions is created to restart ice if
2754// IceRestart is set. It also tests that subsequent MediaSessionOptions don't
Taylor Brandstetterf475d362016-01-08 15:35:57 -08002755// have |audio_transport_options.ice_restart| etc. set.
deadbeefab9b2d12015-10-14 11:33:11 -07002756TEST(CreateSessionOptionsTest, GetMediaSessionOptionsForOfferWithIceRestart) {
2757 RTCOfferAnswerOptions rtc_options;
2758 rtc_options.ice_restart = true;
2759
2760 cricket::MediaSessionOptions options;
deadbeef0ed85b22016-02-23 17:24:52 -08002761 options.transport_options["audio"] = cricket::TransportOptions();
2762 options.transport_options["video"] = cricket::TransportOptions();
htaaac2dea2016-03-10 13:35:55 -08002763 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeef0ed85b22016-02-23 17:24:52 -08002764 EXPECT_TRUE(options.transport_options["audio"].ice_restart);
2765 EXPECT_TRUE(options.transport_options["video"].ice_restart);
deadbeefab9b2d12015-10-14 11:33:11 -07002766
2767 rtc_options = RTCOfferAnswerOptions();
htaaac2dea2016-03-10 13:35:55 -08002768 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeef0ed85b22016-02-23 17:24:52 -08002769 EXPECT_FALSE(options.transport_options["audio"].ice_restart);
2770 EXPECT_FALSE(options.transport_options["video"].ice_restart);
deadbeefab9b2d12015-10-14 11:33:11 -07002771}
2772
2773// Test that the MediaConstraints in an answer don't affect if audio and video
2774// is offered in an offer but that if kOfferToReceiveAudio or
2775// kOfferToReceiveVideo constraints are true in an offer, the media type will be
2776// included in subsequent answers.
2777TEST(CreateSessionOptionsTest, MediaConstraintsInAnswer) {
2778 FakeConstraints answer_c;
2779 answer_c.SetMandatoryReceiveAudio(true);
2780 answer_c.SetMandatoryReceiveVideo(true);
2781
2782 cricket::MediaSessionOptions answer_options;
2783 EXPECT_TRUE(ParseConstraintsForAnswer(&answer_c, &answer_options));
2784 EXPECT_TRUE(answer_options.has_audio());
2785 EXPECT_TRUE(answer_options.has_video());
2786
deadbeefc80741f2015-10-22 13:14:45 -07002787 RTCOfferAnswerOptions rtc_offer_options;
deadbeefab9b2d12015-10-14 11:33:11 -07002788
2789 cricket::MediaSessionOptions offer_options;
htaaac2dea2016-03-10 13:35:55 -08002790 EXPECT_TRUE(
2791 ExtractMediaSessionOptions(rtc_offer_options, false, &offer_options));
deadbeefc80741f2015-10-22 13:14:45 -07002792 EXPECT_TRUE(offer_options.has_audio());
htaaac2dea2016-03-10 13:35:55 -08002793 EXPECT_TRUE(offer_options.has_video());
deadbeefab9b2d12015-10-14 11:33:11 -07002794
deadbeefc80741f2015-10-22 13:14:45 -07002795 RTCOfferAnswerOptions updated_rtc_offer_options;
2796 updated_rtc_offer_options.offer_to_receive_audio = 1;
2797 updated_rtc_offer_options.offer_to_receive_video = 1;
deadbeefab9b2d12015-10-14 11:33:11 -07002798
2799 cricket::MediaSessionOptions updated_offer_options;
htaaac2dea2016-03-10 13:35:55 -08002800 EXPECT_TRUE(ExtractMediaSessionOptions(updated_rtc_offer_options, false,
htaa2a49d92016-03-04 02:51:39 -08002801 &updated_offer_options));
deadbeefab9b2d12015-10-14 11:33:11 -07002802 EXPECT_TRUE(updated_offer_options.has_audio());
2803 EXPECT_TRUE(updated_offer_options.has_video());
2804
2805 // Since an offer has been created with both audio and video, subsequent
2806 // offers and answers should contain both audio and video.
2807 // Answers will only contain the media types that exist in the offer
2808 // regardless of the value of |updated_answer_options.has_audio| and
2809 // |updated_answer_options.has_video|.
2810 FakeConstraints updated_answer_c;
2811 answer_c.SetMandatoryReceiveAudio(false);
2812 answer_c.SetMandatoryReceiveVideo(false);
2813
2814 cricket::MediaSessionOptions updated_answer_options;
2815 EXPECT_TRUE(
2816 ParseConstraintsForAnswer(&updated_answer_c, &updated_answer_options));
2817 EXPECT_TRUE(updated_answer_options.has_audio());
2818 EXPECT_TRUE(updated_answer_options.has_video());
deadbeefab9b2d12015-10-14 11:33:11 -07002819}