blob: d2f0ad1c56eec69a1e0fb36e540fb5d3236c2df2 [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
kjellanderb24317b2016-02-10 07:54:43 -08002 * Copyright 2012 The WebRTC project authors. All Rights Reserved.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003 *
kjellanderb24317b2016-02-10 07:54:43 -08004 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00009 */
10
kwibergd1fe2812016-04-27 06:47:29 -070011#include <memory>
henrike@webrtc.org28e20752013-07-10 00:45:36 +000012#include <string>
kwiberg0eb15ed2015-12-17 03:04:15 -080013#include <utility>
henrike@webrtc.org28e20752013-07-10 00:45:36 +000014
perkjd61bf802016-03-24 03:16:19 -070015#include "testing/gmock/include/gmock/gmock.h"
Henrik Kjellander15583c12016-02-10 10:53:12 +010016#include "webrtc/api/audiotrack.h"
17#include "webrtc/api/jsepsessiondescription.h"
18#include "webrtc/api/mediastream.h"
19#include "webrtc/api/mediastreaminterface.h"
20#include "webrtc/api/peerconnection.h"
21#include "webrtc/api/peerconnectioninterface.h"
22#include "webrtc/api/rtpreceiverinterface.h"
23#include "webrtc/api/rtpsenderinterface.h"
24#include "webrtc/api/streamcollection.h"
25#ifdef WEBRTC_ANDROID
26#include "webrtc/api/test/androidtestinitializer.h"
27#endif
28#include "webrtc/api/test/fakeconstraints.h"
29#include "webrtc/api/test/fakedtlsidentitystore.h"
nisseaf510af2016-03-21 08:20:42 -070030#include "webrtc/api/test/fakevideotracksource.h"
Henrik Kjellander15583c12016-02-10 10:53:12 +010031#include "webrtc/api/test/mockpeerconnectionobservers.h"
32#include "webrtc/api/test/testsdpstrings.h"
perkja3ede6c2016-03-08 01:27:48 +010033#include "webrtc/api/videocapturertracksource.h"
Henrik Kjellander15583c12016-02-10 10:53:12 +010034#include "webrtc/api/videotrack.h"
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000035#include "webrtc/base/gunit.h"
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000036#include "webrtc/base/ssladapter.h"
37#include "webrtc/base/sslstreamadapter.h"
38#include "webrtc/base/stringutils.h"
39#include "webrtc/base/thread.h"
kjellandera96e2d72016-02-04 23:52:28 -080040#include "webrtc/media/base/fakevideocapturer.h"
41#include "webrtc/media/sctp/sctpdataengine.h"
Taylor Brandstettera1c30352016-05-13 08:15:11 -070042#include "webrtc/p2p/base/fakeportallocator.h"
kjellander@webrtc.org9b8df252016-02-12 06:47:59 +010043#include "webrtc/pc/mediasession.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000044
45static const char kStreamLabel1[] = "local_stream_1";
46static const char kStreamLabel2[] = "local_stream_2";
47static const char kStreamLabel3[] = "local_stream_3";
48static const int kDefaultStunPort = 3478;
49static const char kStunAddressOnly[] = "stun:address";
50static const char kStunInvalidPort[] = "stun:address:-1";
51static const char kStunAddressPortAndMore1[] = "stun:address:port:more";
52static const char kStunAddressPortAndMore2[] = "stun:address:port more";
53static const char kTurnIceServerUri[] = "turn:user@turn.example.org";
54static const char kTurnUsername[] = "user";
55static const char kTurnPassword[] = "password";
56static const char kTurnHostname[] = "turn.example.org";
Peter Boström0c4e06b2015-10-07 12:23:21 +020057static const uint32_t kTimeout = 10000U;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000058
deadbeefab9b2d12015-10-14 11:33:11 -070059static const char kStreams[][8] = {"stream1", "stream2"};
60static const char kAudioTracks[][32] = {"audiotrack0", "audiotrack1"};
61static const char kVideoTracks[][32] = {"videotrack0", "videotrack1"};
62
deadbeef5e97fb52015-10-15 12:49:08 -070063static const char kRecvonly[] = "recvonly";
64static const char kSendrecv[] = "sendrecv";
65
deadbeefab9b2d12015-10-14 11:33:11 -070066// Reference SDP with a MediaStream with label "stream1" and audio track with
67// id "audio_1" and a video track with id "video_1;
68static const char kSdpStringWithStream1[] =
69 "v=0\r\n"
70 "o=- 0 0 IN IP4 127.0.0.1\r\n"
71 "s=-\r\n"
72 "t=0 0\r\n"
73 "a=ice-ufrag:e5785931\r\n"
74 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
75 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
76 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
77 "m=audio 1 RTP/AVPF 103\r\n"
78 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -070079 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -070080 "a=rtpmap:103 ISAC/16000\r\n"
81 "a=ssrc:1 cname:stream1\r\n"
82 "a=ssrc:1 mslabel:stream1\r\n"
83 "a=ssrc:1 label:audiotrack0\r\n"
84 "m=video 1 RTP/AVPF 120\r\n"
85 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -070086 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -070087 "a=rtpmap:120 VP8/90000\r\n"
88 "a=ssrc:2 cname:stream1\r\n"
89 "a=ssrc:2 mslabel:stream1\r\n"
90 "a=ssrc:2 label:videotrack0\r\n";
91
92// Reference SDP with two MediaStreams with label "stream1" and "stream2. Each
93// MediaStreams have one audio track and one video track.
94// This uses MSID.
95static const char kSdpStringWithStream1And2[] =
96 "v=0\r\n"
97 "o=- 0 0 IN IP4 127.0.0.1\r\n"
98 "s=-\r\n"
99 "t=0 0\r\n"
100 "a=ice-ufrag:e5785931\r\n"
101 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
102 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
103 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
104 "a=msid-semantic: WMS stream1 stream2\r\n"
105 "m=audio 1 RTP/AVPF 103\r\n"
106 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700107 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700108 "a=rtpmap:103 ISAC/16000\r\n"
109 "a=ssrc:1 cname:stream1\r\n"
110 "a=ssrc:1 msid:stream1 audiotrack0\r\n"
111 "a=ssrc:3 cname:stream2\r\n"
112 "a=ssrc:3 msid:stream2 audiotrack1\r\n"
113 "m=video 1 RTP/AVPF 120\r\n"
114 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700115 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700116 "a=rtpmap:120 VP8/0\r\n"
117 "a=ssrc:2 cname:stream1\r\n"
118 "a=ssrc:2 msid:stream1 videotrack0\r\n"
119 "a=ssrc:4 cname:stream2\r\n"
120 "a=ssrc:4 msid:stream2 videotrack1\r\n";
121
122// Reference SDP without MediaStreams. Msid is not supported.
123static const char kSdpStringWithoutStreams[] =
124 "v=0\r\n"
125 "o=- 0 0 IN IP4 127.0.0.1\r\n"
126 "s=-\r\n"
127 "t=0 0\r\n"
128 "a=ice-ufrag:e5785931\r\n"
129 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
130 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
131 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
132 "m=audio 1 RTP/AVPF 103\r\n"
133 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700134 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700135 "a=rtpmap:103 ISAC/16000\r\n"
136 "m=video 1 RTP/AVPF 120\r\n"
137 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700138 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700139 "a=rtpmap:120 VP8/90000\r\n";
140
141// Reference SDP without MediaStreams. Msid is supported.
142static const char kSdpStringWithMsidWithoutStreams[] =
143 "v=0\r\n"
144 "o=- 0 0 IN IP4 127.0.0.1\r\n"
145 "s=-\r\n"
146 "t=0 0\r\n"
147 "a=ice-ufrag:e5785931\r\n"
148 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
149 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
150 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
151 "a=msid-semantic: WMS\r\n"
152 "m=audio 1 RTP/AVPF 103\r\n"
153 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700154 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700155 "a=rtpmap:103 ISAC/16000\r\n"
156 "m=video 1 RTP/AVPF 120\r\n"
157 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700158 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700159 "a=rtpmap:120 VP8/90000\r\n";
160
161// Reference SDP without MediaStreams and audio only.
162static const char kSdpStringWithoutStreamsAudioOnly[] =
163 "v=0\r\n"
164 "o=- 0 0 IN IP4 127.0.0.1\r\n"
165 "s=-\r\n"
166 "t=0 0\r\n"
167 "a=ice-ufrag:e5785931\r\n"
168 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
169 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
170 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
171 "m=audio 1 RTP/AVPF 103\r\n"
172 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700173 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700174 "a=rtpmap:103 ISAC/16000\r\n";
175
176// Reference SENDONLY SDP without MediaStreams. Msid is not supported.
177static const char kSdpStringSendOnlyWithoutStreams[] =
178 "v=0\r\n"
179 "o=- 0 0 IN IP4 127.0.0.1\r\n"
180 "s=-\r\n"
181 "t=0 0\r\n"
182 "a=ice-ufrag:e5785931\r\n"
183 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
184 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
185 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
186 "m=audio 1 RTP/AVPF 103\r\n"
187 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700188 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700189 "a=sendonly\r\n"
190 "a=rtpmap:103 ISAC/16000\r\n"
191 "m=video 1 RTP/AVPF 120\r\n"
192 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700193 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700194 "a=sendonly\r\n"
195 "a=rtpmap:120 VP8/90000\r\n";
196
197static const char kSdpStringInit[] =
198 "v=0\r\n"
199 "o=- 0 0 IN IP4 127.0.0.1\r\n"
200 "s=-\r\n"
201 "t=0 0\r\n"
202 "a=ice-ufrag:e5785931\r\n"
203 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
204 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
205 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
206 "a=msid-semantic: WMS\r\n";
207
208static const char kSdpStringAudio[] =
209 "m=audio 1 RTP/AVPF 103\r\n"
210 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700211 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700212 "a=rtpmap:103 ISAC/16000\r\n";
213
214static const char kSdpStringVideo[] =
215 "m=video 1 RTP/AVPF 120\r\n"
216 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700217 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700218 "a=rtpmap:120 VP8/90000\r\n";
219
220static const char kSdpStringMs1Audio0[] =
221 "a=ssrc:1 cname:stream1\r\n"
222 "a=ssrc:1 msid:stream1 audiotrack0\r\n";
223
224static const char kSdpStringMs1Video0[] =
225 "a=ssrc:2 cname:stream1\r\n"
226 "a=ssrc:2 msid:stream1 videotrack0\r\n";
227
228static const char kSdpStringMs1Audio1[] =
229 "a=ssrc:3 cname:stream1\r\n"
230 "a=ssrc:3 msid:stream1 audiotrack1\r\n";
231
232static const char kSdpStringMs1Video1[] =
233 "a=ssrc:4 cname:stream1\r\n"
234 "a=ssrc:4 msid:stream1 videotrack1\r\n";
235
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000236#define MAYBE_SKIP_TEST(feature) \
237 if (!(feature())) { \
238 LOG(LS_INFO) << "Feature disabled... skipping"; \
239 return; \
240 }
241
perkjd61bf802016-03-24 03:16:19 -0700242using ::testing::Exactly;
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700243using cricket::StreamParams;
244using rtc::scoped_refptr;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000245using webrtc::AudioSourceInterface;
deadbeefab9b2d12015-10-14 11:33:11 -0700246using webrtc::AudioTrack;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000247using webrtc::AudioTrackInterface;
248using webrtc::DataBuffer;
249using webrtc::DataChannelInterface;
250using webrtc::FakeConstraints;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000251using webrtc::IceCandidateInterface;
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700252using webrtc::JsepSessionDescription;
deadbeefc80741f2015-10-22 13:14:45 -0700253using webrtc::MediaConstraintsInterface;
deadbeefab9b2d12015-10-14 11:33:11 -0700254using webrtc::MediaStream;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000255using webrtc::MediaStreamInterface;
256using webrtc::MediaStreamTrackInterface;
257using webrtc::MockCreateSessionDescriptionObserver;
258using webrtc::MockDataChannelObserver;
259using webrtc::MockSetSessionDescriptionObserver;
260using webrtc::MockStatsObserver;
perkjd61bf802016-03-24 03:16:19 -0700261using webrtc::NotifierInterface;
262using webrtc::ObserverInterface;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000263using webrtc::PeerConnectionInterface;
264using webrtc::PeerConnectionObserver;
deadbeefab9b2d12015-10-14 11:33:11 -0700265using webrtc::RtpReceiverInterface;
266using webrtc::RtpSenderInterface;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000267using webrtc::SdpParseError;
268using webrtc::SessionDescriptionInterface;
deadbeefab9b2d12015-10-14 11:33:11 -0700269using webrtc::StreamCollection;
270using webrtc::StreamCollectionInterface;
perkja3ede6c2016-03-08 01:27:48 +0100271using webrtc::VideoTrackSourceInterface;
deadbeefab9b2d12015-10-14 11:33:11 -0700272using webrtc::VideoTrack;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000273using webrtc::VideoTrackInterface;
274
deadbeefab9b2d12015-10-14 11:33:11 -0700275typedef PeerConnectionInterface::RTCOfferAnswerOptions RTCOfferAnswerOptions;
276
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000277namespace {
278
279// Gets the first ssrc of given content type from the ContentInfo.
280bool GetFirstSsrc(const cricket::ContentInfo* content_info, int* ssrc) {
281 if (!content_info || !ssrc) {
282 return false;
283 }
284 const cricket::MediaContentDescription* media_desc =
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000285 static_cast<const cricket::MediaContentDescription*>(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000286 content_info->description);
287 if (!media_desc || media_desc->streams().empty()) {
288 return false;
289 }
290 *ssrc = media_desc->streams().begin()->first_ssrc();
291 return true;
292}
293
294void SetSsrcToZero(std::string* sdp) {
295 const char kSdpSsrcAtribute[] = "a=ssrc:";
296 const char kSdpSsrcAtributeZero[] = "a=ssrc:0";
297 size_t ssrc_pos = 0;
298 while ((ssrc_pos = sdp->find(kSdpSsrcAtribute, ssrc_pos)) !=
299 std::string::npos) {
300 size_t end_ssrc = sdp->find(" ", ssrc_pos);
301 sdp->replace(ssrc_pos, end_ssrc - ssrc_pos, kSdpSsrcAtributeZero);
302 ssrc_pos = end_ssrc;
303 }
304}
305
deadbeefab9b2d12015-10-14 11:33:11 -0700306// Check if |streams| contains the specified track.
307bool ContainsTrack(const std::vector<cricket::StreamParams>& streams,
308 const std::string& stream_label,
309 const std::string& track_id) {
310 for (const cricket::StreamParams& params : streams) {
311 if (params.sync_label == stream_label && params.id == track_id) {
312 return true;
313 }
314 }
315 return false;
316}
317
318// Check if |senders| contains the specified sender, by id.
319bool ContainsSender(
320 const std::vector<rtc::scoped_refptr<RtpSenderInterface>>& senders,
321 const std::string& id) {
322 for (const auto& sender : senders) {
323 if (sender->id() == id) {
324 return true;
325 }
326 }
327 return false;
328}
329
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700330// Check if |senders| contains the specified sender, by id and stream id.
331bool ContainsSender(
332 const std::vector<rtc::scoped_refptr<RtpSenderInterface>>& senders,
333 const std::string& id,
334 const std::string& stream_id) {
335 for (const auto& sender : senders) {
336 if (sender->id() == id && sender->stream_id() == stream_id) {
337 return true;
338 }
339 }
340 return false;
341}
342
deadbeefab9b2d12015-10-14 11:33:11 -0700343// Create a collection of streams.
344// CreateStreamCollection(1) creates a collection that
345// correspond to kSdpStringWithStream1.
346// CreateStreamCollection(2) correspond to kSdpStringWithStream1And2.
347rtc::scoped_refptr<StreamCollection> CreateStreamCollection(
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700348 int number_of_streams,
349 int tracks_per_stream) {
deadbeefab9b2d12015-10-14 11:33:11 -0700350 rtc::scoped_refptr<StreamCollection> local_collection(
351 StreamCollection::Create());
352
353 for (int i = 0; i < number_of_streams; ++i) {
354 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream(
355 webrtc::MediaStream::Create(kStreams[i]));
356
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700357 for (int j = 0; j < tracks_per_stream; ++j) {
358 // Add a local audio track.
359 rtc::scoped_refptr<webrtc::AudioTrackInterface> audio_track(
360 webrtc::AudioTrack::Create(kAudioTracks[i * tracks_per_stream + j],
361 nullptr));
362 stream->AddTrack(audio_track);
deadbeefab9b2d12015-10-14 11:33:11 -0700363
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700364 // Add a local video track.
365 rtc::scoped_refptr<webrtc::VideoTrackInterface> video_track(
366 webrtc::VideoTrack::Create(kVideoTracks[i * tracks_per_stream + j],
367 webrtc::FakeVideoTrackSource::Create()));
368 stream->AddTrack(video_track);
369 }
deadbeefab9b2d12015-10-14 11:33:11 -0700370
371 local_collection->AddStream(stream);
372 }
373 return local_collection;
374}
375
376// Check equality of StreamCollections.
377bool CompareStreamCollections(StreamCollectionInterface* s1,
378 StreamCollectionInterface* s2) {
379 if (s1 == nullptr || s2 == nullptr || s1->count() != s2->count()) {
380 return false;
381 }
382
383 for (size_t i = 0; i != s1->count(); ++i) {
384 if (s1->at(i)->label() != s2->at(i)->label()) {
385 return false;
386 }
387 webrtc::AudioTrackVector audio_tracks1 = s1->at(i)->GetAudioTracks();
388 webrtc::AudioTrackVector audio_tracks2 = s2->at(i)->GetAudioTracks();
389 webrtc::VideoTrackVector video_tracks1 = s1->at(i)->GetVideoTracks();
390 webrtc::VideoTrackVector video_tracks2 = s2->at(i)->GetVideoTracks();
391
392 if (audio_tracks1.size() != audio_tracks2.size()) {
393 return false;
394 }
395 for (size_t j = 0; j != audio_tracks1.size(); ++j) {
396 if (audio_tracks1[j]->id() != audio_tracks2[j]->id()) {
397 return false;
398 }
399 }
400 if (video_tracks1.size() != video_tracks2.size()) {
401 return false;
402 }
403 for (size_t j = 0; j != video_tracks1.size(); ++j) {
404 if (video_tracks1[j]->id() != video_tracks2[j]->id()) {
405 return false;
406 }
407 }
408 }
409 return true;
410}
411
perkjd61bf802016-03-24 03:16:19 -0700412// Helper class to test Observer.
413class MockTrackObserver : public ObserverInterface {
414 public:
415 explicit MockTrackObserver(NotifierInterface* notifier)
416 : notifier_(notifier) {
417 notifier_->RegisterObserver(this);
418 }
419
420 ~MockTrackObserver() { Unregister(); }
421
422 void Unregister() {
423 if (notifier_) {
424 notifier_->UnregisterObserver(this);
425 notifier_ = nullptr;
426 }
427 }
428
429 MOCK_METHOD0(OnChanged, void());
430
431 private:
432 NotifierInterface* notifier_;
433};
434
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000435class MockPeerConnectionObserver : public PeerConnectionObserver {
436 public:
deadbeefab9b2d12015-10-14 11:33:11 -0700437 MockPeerConnectionObserver() : remote_streams_(StreamCollection::Create()) {}
Henrik Kjellander3fe372d2016-05-12 08:10:52 +0200438 virtual ~MockPeerConnectionObserver() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000439 }
440 void SetPeerConnectionInterface(PeerConnectionInterface* pc) {
441 pc_ = pc;
442 if (pc) {
443 state_ = pc_->signaling_state();
444 }
445 }
nisseef8b61e2016-04-29 06:09:15 -0700446 void OnSignalingChange(
447 PeerConnectionInterface::SignalingState new_state) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000448 EXPECT_EQ(pc_->signaling_state(), new_state);
449 state_ = new_state;
450 }
451 // TODO(bemasc): Remove this once callers transition to OnIceGatheringChange.
452 virtual void OnStateChange(StateType state_changed) {
453 if (pc_.get() == NULL)
454 return;
455 switch (state_changed) {
456 case kSignalingState:
457 // OnSignalingChange and OnStateChange(kSignalingState) should always
458 // be called approximately simultaneously. To ease testing, we require
459 // that they always be called in that order. This check verifies
460 // that OnSignalingChange has just been called.
461 EXPECT_EQ(pc_->signaling_state(), state_);
462 break;
463 case kIceState:
464 ADD_FAILURE();
465 break;
466 default:
467 ADD_FAILURE();
468 break;
469 }
470 }
deadbeefab9b2d12015-10-14 11:33:11 -0700471
472 MediaStreamInterface* RemoteStream(const std::string& label) {
473 return remote_streams_->find(label);
474 }
475 StreamCollectionInterface* remote_streams() const { return remote_streams_; }
perkjdfb769d2016-02-09 03:09:43 -0800476 void OnAddStream(MediaStreamInterface* stream) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000477 last_added_stream_ = stream;
deadbeefab9b2d12015-10-14 11:33:11 -0700478 remote_streams_->AddStream(stream);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000479 }
perkjdfb769d2016-02-09 03:09:43 -0800480 void OnRemoveStream(MediaStreamInterface* stream) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000481 last_removed_stream_ = stream;
deadbeefab9b2d12015-10-14 11:33:11 -0700482 remote_streams_->RemoveStream(stream);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000483 }
perkjdfb769d2016-02-09 03:09:43 -0800484 void OnRenegotiationNeeded() override { renegotiation_needed_ = true; }
485 void OnDataChannel(DataChannelInterface* data_channel) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000486 last_datachannel_ = data_channel;
487 }
488
perkjdfb769d2016-02-09 03:09:43 -0800489 void OnIceConnectionChange(
490 PeerConnectionInterface::IceConnectionState new_state) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000491 EXPECT_EQ(pc_->ice_connection_state(), new_state);
492 }
perkjdfb769d2016-02-09 03:09:43 -0800493 void OnIceGatheringChange(
494 PeerConnectionInterface::IceGatheringState new_state) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000495 EXPECT_EQ(pc_->ice_gathering_state(), new_state);
perkjdfb769d2016-02-09 03:09:43 -0800496 ice_complete_ = new_state == PeerConnectionInterface::kIceGatheringComplete;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000497 }
perkjdfb769d2016-02-09 03:09:43 -0800498 void OnIceCandidate(const webrtc::IceCandidateInterface* candidate) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000499 EXPECT_NE(PeerConnectionInterface::kIceGatheringNew,
500 pc_->ice_gathering_state());
501
502 std::string sdp;
503 EXPECT_TRUE(candidate->ToString(&sdp));
504 EXPECT_LT(0u, sdp.size());
505 last_candidate_.reset(webrtc::CreateIceCandidate(candidate->sdp_mid(),
506 candidate->sdp_mline_index(), sdp, NULL));
507 EXPECT_TRUE(last_candidate_.get() != NULL);
508 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000509
510 // Returns the label of the last added stream.
511 // Empty string if no stream have been added.
512 std::string GetLastAddedStreamLabel() {
513 if (last_added_stream_.get())
514 return last_added_stream_->label();
515 return "";
516 }
517 std::string GetLastRemovedStreamLabel() {
518 if (last_removed_stream_.get())
519 return last_removed_stream_->label();
520 return "";
521 }
522
523 scoped_refptr<PeerConnectionInterface> pc_;
524 PeerConnectionInterface::SignalingState state_;
kwibergd1fe2812016-04-27 06:47:29 -0700525 std::unique_ptr<IceCandidateInterface> last_candidate_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000526 scoped_refptr<DataChannelInterface> last_datachannel_;
deadbeefab9b2d12015-10-14 11:33:11 -0700527 rtc::scoped_refptr<StreamCollection> remote_streams_;
528 bool renegotiation_needed_ = false;
529 bool ice_complete_ = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000530
531 private:
532 scoped_refptr<MediaStreamInterface> last_added_stream_;
533 scoped_refptr<MediaStreamInterface> last_removed_stream_;
534};
535
536} // namespace
deadbeefab9b2d12015-10-14 11:33:11 -0700537
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000538class PeerConnectionInterfaceTest : public testing::Test {
539 protected:
phoglund37ebcf02016-01-08 05:04:57 -0800540 PeerConnectionInterfaceTest() {
541#ifdef WEBRTC_ANDROID
542 webrtc::InitializeAndroidObjects();
543#endif
544 }
545
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000546 virtual void SetUp() {
547 pc_factory_ = webrtc::CreatePeerConnectionFactory(
danilchape9021a32016-05-17 01:52:02 -0700548 rtc::Thread::Current(), rtc::Thread::Current(), rtc::Thread::Current(),
549 nullptr, nullptr, nullptr);
550 ASSERT_TRUE(pc_factory_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000551 }
552
553 void CreatePeerConnection() {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700554 CreatePeerConnection(PeerConnectionInterface::RTCConfiguration(), nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000555 }
556
557 void CreatePeerConnection(webrtc::MediaConstraintsInterface* constraints) {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700558 CreatePeerConnection(PeerConnectionInterface::RTCConfiguration(),
559 constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000560 }
561
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700562 void CreatePeerConnectionWithIceTransportsType(
563 PeerConnectionInterface::IceTransportsType type) {
564 PeerConnectionInterface::RTCConfiguration config;
565 config.type = type;
566 return CreatePeerConnection(config, nullptr);
567 }
568
569 void CreatePeerConnectionWithIceServer(const std::string& uri,
570 const std::string& password) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800571 PeerConnectionInterface::RTCConfiguration config;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000572 PeerConnectionInterface::IceServer server;
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700573 server.uri = uri;
574 server.password = password;
575 config.servers.push_back(server);
576 CreatePeerConnection(config, nullptr);
577 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000578
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700579 void CreatePeerConnection(PeerConnectionInterface::RTCConfiguration config,
580 webrtc::MediaConstraintsInterface* constraints) {
kwibergd1fe2812016-04-27 06:47:29 -0700581 std::unique_ptr<cricket::FakePortAllocator> port_allocator(
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800582 new cricket::FakePortAllocator(rtc::Thread::Current(), nullptr));
583 port_allocator_ = port_allocator.get();
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000584
buildbot@webrtc.org61c1b8e2014-04-09 06:06:38 +0000585 // DTLS does not work in a loopback call, so is disabled for most of the
586 // tests in this file. We only create a FakeIdentityService if the test
587 // explicitly sets the constraint.
jiayl@webrtc.org61e00b02015-03-04 22:17:38 +0000588 FakeConstraints default_constraints;
589 if (!constraints) {
590 constraints = &default_constraints;
591
592 default_constraints.AddMandatory(
593 webrtc::MediaConstraintsInterface::kEnableDtlsSrtp, false);
594 }
595
kwibergd1fe2812016-04-27 06:47:29 -0700596 std::unique_ptr<webrtc::DtlsIdentityStoreInterface> dtls_identity_store;
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000597 bool dtls;
598 if (FindConstraint(constraints,
599 webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
600 &dtls,
Henrik Boström5e56c592015-08-11 10:33:13 +0200601 nullptr) && dtls) {
602 dtls_identity_store.reset(new FakeDtlsIdentityStore());
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000603 }
kwiberg0eb15ed2015-12-17 03:04:15 -0800604 pc_ = pc_factory_->CreatePeerConnection(
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800605 config, constraints, std::move(port_allocator),
kwiberg0eb15ed2015-12-17 03:04:15 -0800606 std::move(dtls_identity_store), &observer_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000607 ASSERT_TRUE(pc_.get() != NULL);
608 observer_.SetPeerConnectionInterface(pc_.get());
609 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
610 }
611
deadbeef0a6c4ca2015-10-06 11:38:28 -0700612 void CreatePeerConnectionExpectFail(const std::string& uri) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800613 PeerConnectionInterface::RTCConfiguration config;
deadbeef0a6c4ca2015-10-06 11:38:28 -0700614 PeerConnectionInterface::IceServer server;
deadbeef0a6c4ca2015-10-06 11:38:28 -0700615 server.uri = uri;
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800616 config.servers.push_back(server);
deadbeef0a6c4ca2015-10-06 11:38:28 -0700617
deadbeef0a6c4ca2015-10-06 11:38:28 -0700618 scoped_refptr<PeerConnectionInterface> pc;
hbosd7973cc2016-05-27 06:08:53 -0700619 pc = pc_factory_->CreatePeerConnection(config, nullptr, nullptr, nullptr,
620 &observer_);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800621 EXPECT_EQ(nullptr, pc);
deadbeef0a6c4ca2015-10-06 11:38:28 -0700622 }
623
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000624 void CreatePeerConnectionWithDifferentConfigurations() {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700625 CreatePeerConnectionWithIceServer(kStunAddressOnly, "");
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800626 EXPECT_EQ(1u, port_allocator_->stun_servers().size());
627 EXPECT_EQ(0u, port_allocator_->turn_servers().size());
628 EXPECT_EQ("address", port_allocator_->stun_servers().begin()->hostname());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000629 EXPECT_EQ(kDefaultStunPort,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800630 port_allocator_->stun_servers().begin()->port());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000631
deadbeef0a6c4ca2015-10-06 11:38:28 -0700632 CreatePeerConnectionExpectFail(kStunInvalidPort);
633 CreatePeerConnectionExpectFail(kStunAddressPortAndMore1);
634 CreatePeerConnectionExpectFail(kStunAddressPortAndMore2);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000635
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700636 CreatePeerConnectionWithIceServer(kTurnIceServerUri, kTurnPassword);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800637 EXPECT_EQ(0u, port_allocator_->stun_servers().size());
638 EXPECT_EQ(1u, port_allocator_->turn_servers().size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000639 EXPECT_EQ(kTurnUsername,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800640 port_allocator_->turn_servers()[0].credentials.username);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000641 EXPECT_EQ(kTurnPassword,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800642 port_allocator_->turn_servers()[0].credentials.password);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000643 EXPECT_EQ(kTurnHostname,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800644 port_allocator_->turn_servers()[0].ports[0].address.hostname());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000645 }
646
647 void ReleasePeerConnection() {
648 pc_ = NULL;
649 observer_.SetPeerConnectionInterface(NULL);
650 }
651
deadbeefab9b2d12015-10-14 11:33:11 -0700652 void AddVideoStream(const std::string& label) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000653 // Create a local stream.
654 scoped_refptr<MediaStreamInterface> stream(
655 pc_factory_->CreateLocalMediaStream(label));
perkja3ede6c2016-03-08 01:27:48 +0100656 scoped_refptr<VideoTrackSourceInterface> video_source(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000657 pc_factory_->CreateVideoSource(new cricket::FakeVideoCapturer(), NULL));
658 scoped_refptr<VideoTrackInterface> video_track(
659 pc_factory_->CreateVideoTrack(label + "v0", video_source));
660 stream->AddTrack(video_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +0000661 EXPECT_TRUE(pc_->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000662 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
663 observer_.renegotiation_needed_ = false;
664 }
665
666 void AddVoiceStream(const std::string& label) {
667 // Create a local stream.
668 scoped_refptr<MediaStreamInterface> stream(
669 pc_factory_->CreateLocalMediaStream(label));
670 scoped_refptr<AudioTrackInterface> audio_track(
671 pc_factory_->CreateAudioTrack(label + "a0", NULL));
672 stream->AddTrack(audio_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +0000673 EXPECT_TRUE(pc_->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000674 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
675 observer_.renegotiation_needed_ = false;
676 }
677
678 void AddAudioVideoStream(const std::string& stream_label,
679 const std::string& audio_track_label,
680 const std::string& video_track_label) {
681 // Create a local stream.
682 scoped_refptr<MediaStreamInterface> stream(
683 pc_factory_->CreateLocalMediaStream(stream_label));
684 scoped_refptr<AudioTrackInterface> audio_track(
685 pc_factory_->CreateAudioTrack(
686 audio_track_label, static_cast<AudioSourceInterface*>(NULL)));
687 stream->AddTrack(audio_track.get());
688 scoped_refptr<VideoTrackInterface> video_track(
nisseaf510af2016-03-21 08:20:42 -0700689 pc_factory_->CreateVideoTrack(
690 video_track_label,
691 pc_factory_->CreateVideoSource(new cricket::FakeVideoCapturer())));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000692 stream->AddTrack(video_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +0000693 EXPECT_TRUE(pc_->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000694 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
695 observer_.renegotiation_needed_ = false;
696 }
697
kwibergd1fe2812016-04-27 06:47:29 -0700698 bool DoCreateOfferAnswer(std::unique_ptr<SessionDescriptionInterface>* desc,
deadbeefc80741f2015-10-22 13:14:45 -0700699 bool offer,
700 MediaConstraintsInterface* constraints) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000701 rtc::scoped_refptr<MockCreateSessionDescriptionObserver>
702 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000703 MockCreateSessionDescriptionObserver>());
704 if (offer) {
deadbeefc80741f2015-10-22 13:14:45 -0700705 pc_->CreateOffer(observer, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000706 } else {
deadbeefc80741f2015-10-22 13:14:45 -0700707 pc_->CreateAnswer(observer, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000708 }
709 EXPECT_EQ_WAIT(true, observer->called(), kTimeout);
kwiberg2bbff992016-03-16 11:03:04 -0700710 desc->reset(observer->release_desc());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000711 return observer->result();
712 }
713
kwibergd1fe2812016-04-27 06:47:29 -0700714 bool DoCreateOffer(std::unique_ptr<SessionDescriptionInterface>* desc,
deadbeefc80741f2015-10-22 13:14:45 -0700715 MediaConstraintsInterface* constraints) {
716 return DoCreateOfferAnswer(desc, true, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000717 }
718
kwibergd1fe2812016-04-27 06:47:29 -0700719 bool DoCreateAnswer(std::unique_ptr<SessionDescriptionInterface>* desc,
deadbeefc80741f2015-10-22 13:14:45 -0700720 MediaConstraintsInterface* constraints) {
721 return DoCreateOfferAnswer(desc, false, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000722 }
723
724 bool DoSetSessionDescription(SessionDescriptionInterface* desc, bool local) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000725 rtc::scoped_refptr<MockSetSessionDescriptionObserver>
726 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000727 MockSetSessionDescriptionObserver>());
728 if (local) {
729 pc_->SetLocalDescription(observer, desc);
730 } else {
731 pc_->SetRemoteDescription(observer, desc);
732 }
733 EXPECT_EQ_WAIT(true, observer->called(), kTimeout);
734 return observer->result();
735 }
736
737 bool DoSetLocalDescription(SessionDescriptionInterface* desc) {
738 return DoSetSessionDescription(desc, true);
739 }
740
741 bool DoSetRemoteDescription(SessionDescriptionInterface* desc) {
742 return DoSetSessionDescription(desc, false);
743 }
744
745 // Calls PeerConnection::GetStats and check the return value.
746 // It does not verify the values in the StatReports since a RTCP packet might
747 // be required.
748 bool DoGetStats(MediaStreamTrackInterface* track) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000749 rtc::scoped_refptr<MockStatsObserver> observer(
750 new rtc::RefCountedObject<MockStatsObserver>());
jiayl@webrtc.orgdb41b4d2014-03-03 21:30:06 +0000751 if (!pc_->GetStats(
752 observer, track, PeerConnectionInterface::kStatsOutputLevelStandard))
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000753 return false;
754 EXPECT_TRUE_WAIT(observer->called(), kTimeout);
755 return observer->called();
756 }
757
758 void InitiateCall() {
759 CreatePeerConnection();
760 // Create a local stream with audio&video tracks.
761 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
762 CreateOfferReceiveAnswer();
763 }
764
765 // Verify that RTP Header extensions has been negotiated for audio and video.
766 void VerifyRemoteRtpHeaderExtensions() {
767 const cricket::MediaContentDescription* desc =
768 cricket::GetFirstAudioContentDescription(
769 pc_->remote_description()->description());
770 ASSERT_TRUE(desc != NULL);
771 EXPECT_GT(desc->rtp_header_extensions().size(), 0u);
772
773 desc = cricket::GetFirstVideoContentDescription(
774 pc_->remote_description()->description());
775 ASSERT_TRUE(desc != NULL);
776 EXPECT_GT(desc->rtp_header_extensions().size(), 0u);
777 }
778
779 void CreateOfferAsRemoteDescription() {
kwibergd1fe2812016-04-27 06:47:29 -0700780 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -0700781 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000782 std::string sdp;
783 EXPECT_TRUE(offer->ToString(&sdp));
784 SessionDescriptionInterface* remote_offer =
785 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
786 sdp, NULL);
787 EXPECT_TRUE(DoSetRemoteDescription(remote_offer));
788 EXPECT_EQ(PeerConnectionInterface::kHaveRemoteOffer, observer_.state_);
789 }
790
deadbeefab9b2d12015-10-14 11:33:11 -0700791 void CreateAndSetRemoteOffer(const std::string& sdp) {
792 SessionDescriptionInterface* remote_offer =
793 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
794 sdp, nullptr);
795 EXPECT_TRUE(DoSetRemoteDescription(remote_offer));
796 EXPECT_EQ(PeerConnectionInterface::kHaveRemoteOffer, observer_.state_);
797 }
798
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000799 void CreateAnswerAsLocalDescription() {
kwibergd1fe2812016-04-27 06:47:29 -0700800 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -0700801 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000802
803 // TODO(perkj): Currently SetLocalDescription fails if any parameters in an
804 // audio codec change, even if the parameter has nothing to do with
805 // receiving. Not all parameters are serialized to SDP.
806 // Since CreatePrAnswerAsLocalDescription serialize/deserialize
807 // the SessionDescription, it is necessary to do that here to in order to
808 // get ReceiveOfferCreatePrAnswerAndAnswer and RenegotiateAudioOnly to pass.
809 // https://code.google.com/p/webrtc/issues/detail?id=1356
810 std::string sdp;
811 EXPECT_TRUE(answer->ToString(&sdp));
812 SessionDescriptionInterface* new_answer =
813 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
814 sdp, NULL);
815 EXPECT_TRUE(DoSetLocalDescription(new_answer));
816 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
817 }
818
819 void CreatePrAnswerAsLocalDescription() {
kwibergd1fe2812016-04-27 06:47:29 -0700820 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -0700821 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000822
823 std::string sdp;
824 EXPECT_TRUE(answer->ToString(&sdp));
825 SessionDescriptionInterface* pr_answer =
826 webrtc::CreateSessionDescription(SessionDescriptionInterface::kPrAnswer,
827 sdp, NULL);
828 EXPECT_TRUE(DoSetLocalDescription(pr_answer));
829 EXPECT_EQ(PeerConnectionInterface::kHaveLocalPrAnswer, observer_.state_);
830 }
831
832 void CreateOfferReceiveAnswer() {
833 CreateOfferAsLocalDescription();
834 std::string sdp;
835 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
836 CreateAnswerAsRemoteDescription(sdp);
837 }
838
839 void CreateOfferAsLocalDescription() {
kwibergd1fe2812016-04-27 06:47:29 -0700840 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -0700841 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000842 // TODO(perkj): Currently SetLocalDescription fails if any parameters in an
843 // audio codec change, even if the parameter has nothing to do with
844 // receiving. Not all parameters are serialized to SDP.
845 // Since CreatePrAnswerAsLocalDescription serialize/deserialize
846 // the SessionDescription, it is necessary to do that here to in order to
847 // get ReceiveOfferCreatePrAnswerAndAnswer and RenegotiateAudioOnly to pass.
848 // https://code.google.com/p/webrtc/issues/detail?id=1356
849 std::string sdp;
850 EXPECT_TRUE(offer->ToString(&sdp));
851 SessionDescriptionInterface* new_offer =
852 webrtc::CreateSessionDescription(
853 SessionDescriptionInterface::kOffer,
854 sdp, NULL);
855
856 EXPECT_TRUE(DoSetLocalDescription(new_offer));
857 EXPECT_EQ(PeerConnectionInterface::kHaveLocalOffer, observer_.state_);
mallinath@webrtc.org68cbd012014-01-22 00:16:46 +0000858 // Wait for the ice_complete message, so that SDP will have candidates.
859 EXPECT_TRUE_WAIT(observer_.ice_complete_, kTimeout);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000860 }
861
deadbeefab9b2d12015-10-14 11:33:11 -0700862 void CreateAnswerAsRemoteDescription(const std::string& sdp) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000863 webrtc::JsepSessionDescription* answer = new webrtc::JsepSessionDescription(
864 SessionDescriptionInterface::kAnswer);
deadbeefab9b2d12015-10-14 11:33:11 -0700865 EXPECT_TRUE(answer->Initialize(sdp, NULL));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000866 EXPECT_TRUE(DoSetRemoteDescription(answer));
867 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
868 }
869
deadbeefab9b2d12015-10-14 11:33:11 -0700870 void CreatePrAnswerAndAnswerAsRemoteDescription(const std::string& sdp) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000871 webrtc::JsepSessionDescription* pr_answer =
872 new webrtc::JsepSessionDescription(
873 SessionDescriptionInterface::kPrAnswer);
deadbeefab9b2d12015-10-14 11:33:11 -0700874 EXPECT_TRUE(pr_answer->Initialize(sdp, NULL));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000875 EXPECT_TRUE(DoSetRemoteDescription(pr_answer));
876 EXPECT_EQ(PeerConnectionInterface::kHaveRemotePrAnswer, observer_.state_);
877 webrtc::JsepSessionDescription* answer =
878 new webrtc::JsepSessionDescription(
879 SessionDescriptionInterface::kAnswer);
deadbeefab9b2d12015-10-14 11:33:11 -0700880 EXPECT_TRUE(answer->Initialize(sdp, NULL));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000881 EXPECT_TRUE(DoSetRemoteDescription(answer));
882 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
883 }
884
885 // Help function used for waiting until a the last signaled remote stream has
886 // the same label as |stream_label|. In a few of the tests in this file we
887 // answer with the same session description as we offer and thus we can
888 // check if OnAddStream have been called with the same stream as we offer to
889 // send.
890 void WaitAndVerifyOnAddStream(const std::string& stream_label) {
891 EXPECT_EQ_WAIT(stream_label, observer_.GetLastAddedStreamLabel(), kTimeout);
892 }
893
894 // Creates an offer and applies it as a local session description.
895 // Creates an answer with the same SDP an the offer but removes all lines
896 // that start with a:ssrc"
897 void CreateOfferReceiveAnswerWithoutSsrc() {
898 CreateOfferAsLocalDescription();
899 std::string sdp;
900 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
901 SetSsrcToZero(&sdp);
902 CreateAnswerAsRemoteDescription(sdp);
903 }
904
deadbeefab9b2d12015-10-14 11:33:11 -0700905 // This function creates a MediaStream with label kStreams[0] and
906 // |number_of_audio_tracks| and |number_of_video_tracks| tracks and the
907 // corresponding SessionDescriptionInterface. The SessionDescriptionInterface
kwiberg2bbff992016-03-16 11:03:04 -0700908 // is returned and the MediaStream is stored in
deadbeefab9b2d12015-10-14 11:33:11 -0700909 // |reference_collection_|
kwibergd1fe2812016-04-27 06:47:29 -0700910 std::unique_ptr<SessionDescriptionInterface>
kwiberg2bbff992016-03-16 11:03:04 -0700911 CreateSessionDescriptionAndReference(size_t number_of_audio_tracks,
912 size_t number_of_video_tracks) {
913 EXPECT_LE(number_of_audio_tracks, 2u);
914 EXPECT_LE(number_of_video_tracks, 2u);
deadbeefab9b2d12015-10-14 11:33:11 -0700915
916 reference_collection_ = StreamCollection::Create();
917 std::string sdp_ms1 = std::string(kSdpStringInit);
918
919 std::string mediastream_label = kStreams[0];
920
921 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream(
922 webrtc::MediaStream::Create(mediastream_label));
923 reference_collection_->AddStream(stream);
924
925 if (number_of_audio_tracks > 0) {
926 sdp_ms1 += std::string(kSdpStringAudio);
927 sdp_ms1 += std::string(kSdpStringMs1Audio0);
928 AddAudioTrack(kAudioTracks[0], stream);
929 }
930 if (number_of_audio_tracks > 1) {
931 sdp_ms1 += kSdpStringMs1Audio1;
932 AddAudioTrack(kAudioTracks[1], stream);
933 }
934
935 if (number_of_video_tracks > 0) {
936 sdp_ms1 += std::string(kSdpStringVideo);
937 sdp_ms1 += std::string(kSdpStringMs1Video0);
938 AddVideoTrack(kVideoTracks[0], stream);
939 }
940 if (number_of_video_tracks > 1) {
941 sdp_ms1 += kSdpStringMs1Video1;
942 AddVideoTrack(kVideoTracks[1], stream);
943 }
944
kwibergd1fe2812016-04-27 06:47:29 -0700945 return std::unique_ptr<SessionDescriptionInterface>(
kwiberg2bbff992016-03-16 11:03:04 -0700946 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
947 sdp_ms1, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -0700948 }
949
950 void AddAudioTrack(const std::string& track_id,
951 MediaStreamInterface* stream) {
952 rtc::scoped_refptr<webrtc::AudioTrackInterface> audio_track(
953 webrtc::AudioTrack::Create(track_id, nullptr));
954 ASSERT_TRUE(stream->AddTrack(audio_track));
955 }
956
957 void AddVideoTrack(const std::string& track_id,
958 MediaStreamInterface* stream) {
959 rtc::scoped_refptr<webrtc::VideoTrackInterface> video_track(
nisseaf510af2016-03-21 08:20:42 -0700960 webrtc::VideoTrack::Create(track_id,
961 webrtc::FakeVideoTrackSource::Create()));
deadbeefab9b2d12015-10-14 11:33:11 -0700962 ASSERT_TRUE(stream->AddTrack(video_track));
963 }
964
kwibergfd8be342016-05-14 19:44:11 -0700965 std::unique_ptr<SessionDescriptionInterface> CreateOfferWithOneAudioStream() {
zhihuang8f65cdf2016-05-06 18:40:30 -0700966 CreatePeerConnection();
967 AddVoiceStream(kStreamLabel1);
kwibergfd8be342016-05-14 19:44:11 -0700968 std::unique_ptr<SessionDescriptionInterface> offer;
zhihuang8f65cdf2016-05-06 18:40:30 -0700969 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
970 return offer;
971 }
972
kwibergfd8be342016-05-14 19:44:11 -0700973 std::unique_ptr<SessionDescriptionInterface>
zhihuang8f65cdf2016-05-06 18:40:30 -0700974 CreateAnswerWithOneAudioStream() {
kwibergfd8be342016-05-14 19:44:11 -0700975 std::unique_ptr<SessionDescriptionInterface> offer =
zhihuang8f65cdf2016-05-06 18:40:30 -0700976 CreateOfferWithOneAudioStream();
977 EXPECT_TRUE(DoSetRemoteDescription(offer.release()));
kwibergfd8be342016-05-14 19:44:11 -0700978 std::unique_ptr<SessionDescriptionInterface> answer;
zhihuang8f65cdf2016-05-06 18:40:30 -0700979 EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
980 return answer;
981 }
982
983 const std::string& GetFirstAudioStreamCname(
984 const SessionDescriptionInterface* desc) {
985 const cricket::ContentInfo* audio_content =
986 cricket::GetFirstAudioContent(desc->description());
987 const cricket::AudioContentDescription* audio_desc =
988 static_cast<const cricket::AudioContentDescription*>(
989 audio_content->description);
990 return audio_desc->streams()[0].cname;
991 }
992
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800993 cricket::FakePortAllocator* port_allocator_ = nullptr;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000994 scoped_refptr<webrtc::PeerConnectionFactoryInterface> pc_factory_;
995 scoped_refptr<PeerConnectionInterface> pc_;
996 MockPeerConnectionObserver observer_;
deadbeefab9b2d12015-10-14 11:33:11 -0700997 rtc::scoped_refptr<StreamCollection> reference_collection_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000998};
999
zhihuang8f65cdf2016-05-06 18:40:30 -07001000// Generate different CNAMEs when PeerConnections are created.
1001// The CNAMEs are expected to be generated randomly. It is possible
1002// that the test fails, though the possibility is very low.
1003TEST_F(PeerConnectionInterfaceTest, CnameGenerationInOffer) {
kwibergfd8be342016-05-14 19:44:11 -07001004 std::unique_ptr<SessionDescriptionInterface> offer1 =
zhihuang8f65cdf2016-05-06 18:40:30 -07001005 CreateOfferWithOneAudioStream();
kwibergfd8be342016-05-14 19:44:11 -07001006 std::unique_ptr<SessionDescriptionInterface> offer2 =
zhihuang8f65cdf2016-05-06 18:40:30 -07001007 CreateOfferWithOneAudioStream();
1008 EXPECT_NE(GetFirstAudioStreamCname(offer1.get()),
1009 GetFirstAudioStreamCname(offer2.get()));
1010}
1011
1012TEST_F(PeerConnectionInterfaceTest, CnameGenerationInAnswer) {
kwibergfd8be342016-05-14 19:44:11 -07001013 std::unique_ptr<SessionDescriptionInterface> answer1 =
zhihuang8f65cdf2016-05-06 18:40:30 -07001014 CreateAnswerWithOneAudioStream();
kwibergfd8be342016-05-14 19:44:11 -07001015 std::unique_ptr<SessionDescriptionInterface> answer2 =
zhihuang8f65cdf2016-05-06 18:40:30 -07001016 CreateAnswerWithOneAudioStream();
1017 EXPECT_NE(GetFirstAudioStreamCname(answer1.get()),
1018 GetFirstAudioStreamCname(answer2.get()));
1019}
1020
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001021TEST_F(PeerConnectionInterfaceTest,
1022 CreatePeerConnectionWithDifferentConfigurations) {
1023 CreatePeerConnectionWithDifferentConfigurations();
1024}
1025
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001026TEST_F(PeerConnectionInterfaceTest,
1027 CreatePeerConnectionWithDifferentIceTransportsTypes) {
1028 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kNone);
1029 EXPECT_EQ(cricket::CF_NONE, port_allocator_->candidate_filter());
1030 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kRelay);
1031 EXPECT_EQ(cricket::CF_RELAY, port_allocator_->candidate_filter());
1032 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kNoHost);
1033 EXPECT_EQ(cricket::CF_ALL & ~cricket::CF_HOST,
1034 port_allocator_->candidate_filter());
1035 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kAll);
1036 EXPECT_EQ(cricket::CF_ALL, port_allocator_->candidate_filter());
1037}
1038
1039// Test that when a PeerConnection is created with a nonzero candidate pool
1040// size, the pooled PortAllocatorSession is created with all the attributes
1041// in the RTCConfiguration.
1042TEST_F(PeerConnectionInterfaceTest, CreatePeerConnectionWithPooledCandidates) {
1043 PeerConnectionInterface::RTCConfiguration config;
1044 PeerConnectionInterface::IceServer server;
1045 server.uri = kStunAddressOnly;
1046 config.servers.push_back(server);
1047 config.type = PeerConnectionInterface::kRelay;
1048 config.disable_ipv6 = true;
1049 config.tcp_candidate_policy =
1050 PeerConnectionInterface::kTcpCandidatePolicyDisabled;
1051 config.ice_candidate_pool_size = 1;
1052 CreatePeerConnection(config, nullptr);
1053
1054 const cricket::FakePortAllocatorSession* session =
1055 static_cast<const cricket::FakePortAllocatorSession*>(
1056 port_allocator_->GetPooledSession());
1057 ASSERT_NE(nullptr, session);
1058 EXPECT_EQ(1UL, session->stun_servers().size());
1059 EXPECT_EQ(0U, session->flags() & cricket::PORTALLOCATOR_ENABLE_IPV6);
1060 EXPECT_LT(0U, session->flags() & cricket::PORTALLOCATOR_DISABLE_TCP);
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001061}
1062
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001063TEST_F(PeerConnectionInterfaceTest, AddStreams) {
1064 CreatePeerConnection();
deadbeefab9b2d12015-10-14 11:33:11 -07001065 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001066 AddVoiceStream(kStreamLabel2);
1067 ASSERT_EQ(2u, pc_->local_streams()->count());
1068
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001069 // Test we can add multiple local streams to one peerconnection.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001070 scoped_refptr<MediaStreamInterface> stream(
1071 pc_factory_->CreateLocalMediaStream(kStreamLabel3));
1072 scoped_refptr<AudioTrackInterface> audio_track(
1073 pc_factory_->CreateAudioTrack(
1074 kStreamLabel3, static_cast<AudioSourceInterface*>(NULL)));
1075 stream->AddTrack(audio_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +00001076 EXPECT_TRUE(pc_->AddStream(stream));
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001077 EXPECT_EQ(3u, pc_->local_streams()->count());
1078
1079 // Remove the third stream.
1080 pc_->RemoveStream(pc_->local_streams()->at(2));
1081 EXPECT_EQ(2u, pc_->local_streams()->count());
1082
1083 // Remove the second stream.
1084 pc_->RemoveStream(pc_->local_streams()->at(1));
1085 EXPECT_EQ(1u, pc_->local_streams()->count());
1086
1087 // Remove the first stream.
1088 pc_->RemoveStream(pc_->local_streams()->at(0));
1089 EXPECT_EQ(0u, pc_->local_streams()->count());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001090}
1091
deadbeefab9b2d12015-10-14 11:33:11 -07001092// Test that the created offer includes streams we added.
1093TEST_F(PeerConnectionInterfaceTest, AddedStreamsPresentInOffer) {
1094 CreatePeerConnection();
1095 AddAudioVideoStream(kStreamLabel1, "audio_track", "video_track");
kwibergd1fe2812016-04-27 06:47:29 -07001096 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001097 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07001098
1099 const cricket::ContentInfo* audio_content =
1100 cricket::GetFirstAudioContent(offer->description());
1101 const cricket::AudioContentDescription* audio_desc =
1102 static_cast<const cricket::AudioContentDescription*>(
1103 audio_content->description);
1104 EXPECT_TRUE(
1105 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
1106
1107 const cricket::ContentInfo* video_content =
1108 cricket::GetFirstVideoContent(offer->description());
1109 const cricket::VideoContentDescription* video_desc =
1110 static_cast<const cricket::VideoContentDescription*>(
1111 video_content->description);
1112 EXPECT_TRUE(
1113 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1114
1115 // Add another stream and ensure the offer includes both the old and new
1116 // streams.
1117 AddAudioVideoStream(kStreamLabel2, "audio_track2", "video_track2");
kwiberg2bbff992016-03-16 11:03:04 -07001118 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07001119
1120 audio_content = cricket::GetFirstAudioContent(offer->description());
1121 audio_desc = static_cast<const cricket::AudioContentDescription*>(
1122 audio_content->description);
1123 EXPECT_TRUE(
1124 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
1125 EXPECT_TRUE(
1126 ContainsTrack(audio_desc->streams(), kStreamLabel2, "audio_track2"));
1127
1128 video_content = cricket::GetFirstVideoContent(offer->description());
1129 video_desc = static_cast<const cricket::VideoContentDescription*>(
1130 video_content->description);
1131 EXPECT_TRUE(
1132 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1133 EXPECT_TRUE(
1134 ContainsTrack(video_desc->streams(), kStreamLabel2, "video_track2"));
1135}
1136
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001137TEST_F(PeerConnectionInterfaceTest, RemoveStream) {
1138 CreatePeerConnection();
deadbeefab9b2d12015-10-14 11:33:11 -07001139 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001140 ASSERT_EQ(1u, pc_->local_streams()->count());
1141 pc_->RemoveStream(pc_->local_streams()->at(0));
1142 EXPECT_EQ(0u, pc_->local_streams()->count());
1143}
1144
deadbeefe1f9d832016-01-14 15:35:42 -08001145// Test for AddTrack and RemoveTrack methods.
1146// Tests that the created offer includes tracks we added,
1147// and that the RtpSenders are created correctly.
1148// Also tests that RemoveTrack removes the tracks from subsequent offers.
1149TEST_F(PeerConnectionInterfaceTest, AddTrackRemoveTrack) {
1150 CreatePeerConnection();
1151 // Create a dummy stream, so tracks share a stream label.
1152 scoped_refptr<MediaStreamInterface> stream(
1153 pc_factory_->CreateLocalMediaStream(kStreamLabel1));
1154 std::vector<MediaStreamInterface*> stream_list;
1155 stream_list.push_back(stream.get());
1156 scoped_refptr<AudioTrackInterface> audio_track(
1157 pc_factory_->CreateAudioTrack("audio_track", nullptr));
nisseaf510af2016-03-21 08:20:42 -07001158 scoped_refptr<VideoTrackInterface> video_track(pc_factory_->CreateVideoTrack(
1159 "video_track",
1160 pc_factory_->CreateVideoSource(new cricket::FakeVideoCapturer())));
deadbeefe1f9d832016-01-14 15:35:42 -08001161 auto audio_sender = pc_->AddTrack(audio_track, stream_list);
1162 auto video_sender = pc_->AddTrack(video_track, stream_list);
1163 EXPECT_EQ(kStreamLabel1, audio_sender->stream_id());
1164 EXPECT_EQ("audio_track", audio_sender->id());
1165 EXPECT_EQ(audio_track, audio_sender->track());
1166 EXPECT_EQ(kStreamLabel1, video_sender->stream_id());
1167 EXPECT_EQ("video_track", video_sender->id());
1168 EXPECT_EQ(video_track, video_sender->track());
1169
1170 // Now create an offer and check for the senders.
kwibergd1fe2812016-04-27 06:47:29 -07001171 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001172 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefe1f9d832016-01-14 15:35:42 -08001173
1174 const cricket::ContentInfo* audio_content =
1175 cricket::GetFirstAudioContent(offer->description());
1176 const cricket::AudioContentDescription* audio_desc =
1177 static_cast<const cricket::AudioContentDescription*>(
1178 audio_content->description);
1179 EXPECT_TRUE(
1180 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
1181
1182 const cricket::ContentInfo* video_content =
1183 cricket::GetFirstVideoContent(offer->description());
1184 const cricket::VideoContentDescription* video_desc =
1185 static_cast<const cricket::VideoContentDescription*>(
1186 video_content->description);
1187 EXPECT_TRUE(
1188 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1189
1190 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
1191
1192 // Now try removing the tracks.
1193 EXPECT_TRUE(pc_->RemoveTrack(audio_sender));
1194 EXPECT_TRUE(pc_->RemoveTrack(video_sender));
1195
1196 // Create a new offer and ensure it doesn't contain the removed senders.
kwiberg2bbff992016-03-16 11:03:04 -07001197 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefe1f9d832016-01-14 15:35:42 -08001198
1199 audio_content = cricket::GetFirstAudioContent(offer->description());
1200 audio_desc = static_cast<const cricket::AudioContentDescription*>(
1201 audio_content->description);
1202 EXPECT_FALSE(
1203 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
1204
1205 video_content = cricket::GetFirstVideoContent(offer->description());
1206 video_desc = static_cast<const cricket::VideoContentDescription*>(
1207 video_content->description);
1208 EXPECT_FALSE(
1209 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1210
1211 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
1212
1213 // Calling RemoveTrack on a sender no longer attached to a PeerConnection
1214 // should return false.
1215 EXPECT_FALSE(pc_->RemoveTrack(audio_sender));
1216 EXPECT_FALSE(pc_->RemoveTrack(video_sender));
1217}
1218
1219// Test creating senders without a stream specified,
1220// expecting a random stream ID to be generated.
1221TEST_F(PeerConnectionInterfaceTest, AddTrackWithoutStream) {
1222 CreatePeerConnection();
1223 // Create a dummy stream, so tracks share a stream label.
1224 scoped_refptr<AudioTrackInterface> audio_track(
1225 pc_factory_->CreateAudioTrack("audio_track", nullptr));
nisseaf510af2016-03-21 08:20:42 -07001226 scoped_refptr<VideoTrackInterface> video_track(pc_factory_->CreateVideoTrack(
1227 "video_track",
1228 pc_factory_->CreateVideoSource(new cricket::FakeVideoCapturer())));
deadbeefe1f9d832016-01-14 15:35:42 -08001229 auto audio_sender =
1230 pc_->AddTrack(audio_track, std::vector<MediaStreamInterface*>());
1231 auto video_sender =
1232 pc_->AddTrack(video_track, std::vector<MediaStreamInterface*>());
1233 EXPECT_EQ("audio_track", audio_sender->id());
1234 EXPECT_EQ(audio_track, audio_sender->track());
1235 EXPECT_EQ("video_track", video_sender->id());
1236 EXPECT_EQ(video_track, video_sender->track());
1237 // If the ID is truly a random GUID, it should be infinitely unlikely they
1238 // will be the same.
1239 EXPECT_NE(video_sender->stream_id(), audio_sender->stream_id());
1240}
1241
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001242TEST_F(PeerConnectionInterfaceTest, CreateOfferReceiveAnswer) {
1243 InitiateCall();
1244 WaitAndVerifyOnAddStream(kStreamLabel1);
1245 VerifyRemoteRtpHeaderExtensions();
1246}
1247
1248TEST_F(PeerConnectionInterfaceTest, CreateOfferReceivePrAnswerAndAnswer) {
1249 CreatePeerConnection();
deadbeefab9b2d12015-10-14 11:33:11 -07001250 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001251 CreateOfferAsLocalDescription();
1252 std::string offer;
1253 EXPECT_TRUE(pc_->local_description()->ToString(&offer));
1254 CreatePrAnswerAndAnswerAsRemoteDescription(offer);
1255 WaitAndVerifyOnAddStream(kStreamLabel1);
1256}
1257
1258TEST_F(PeerConnectionInterfaceTest, ReceiveOfferCreateAnswer) {
1259 CreatePeerConnection();
deadbeefab9b2d12015-10-14 11:33:11 -07001260 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001261
1262 CreateOfferAsRemoteDescription();
1263 CreateAnswerAsLocalDescription();
1264
1265 WaitAndVerifyOnAddStream(kStreamLabel1);
1266}
1267
1268TEST_F(PeerConnectionInterfaceTest, ReceiveOfferCreatePrAnswerAndAnswer) {
1269 CreatePeerConnection();
deadbeefab9b2d12015-10-14 11:33:11 -07001270 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001271
1272 CreateOfferAsRemoteDescription();
1273 CreatePrAnswerAsLocalDescription();
1274 CreateAnswerAsLocalDescription();
1275
1276 WaitAndVerifyOnAddStream(kStreamLabel1);
1277}
1278
1279TEST_F(PeerConnectionInterfaceTest, Renegotiate) {
1280 InitiateCall();
1281 ASSERT_EQ(1u, pc_->remote_streams()->count());
1282 pc_->RemoveStream(pc_->local_streams()->at(0));
1283 CreateOfferReceiveAnswer();
1284 EXPECT_EQ(0u, pc_->remote_streams()->count());
deadbeefab9b2d12015-10-14 11:33:11 -07001285 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001286 CreateOfferReceiveAnswer();
1287}
1288
1289// Tests that after negotiating an audio only call, the respondent can perform a
1290// renegotiation that removes the audio stream.
1291TEST_F(PeerConnectionInterfaceTest, RenegotiateAudioOnly) {
1292 CreatePeerConnection();
1293 AddVoiceStream(kStreamLabel1);
1294 CreateOfferAsRemoteDescription();
1295 CreateAnswerAsLocalDescription();
1296
1297 ASSERT_EQ(1u, pc_->remote_streams()->count());
1298 pc_->RemoveStream(pc_->local_streams()->at(0));
1299 CreateOfferReceiveAnswer();
1300 EXPECT_EQ(0u, pc_->remote_streams()->count());
1301}
1302
1303// Test that candidates are generated and that we can parse our own candidates.
1304TEST_F(PeerConnectionInterfaceTest, IceCandidates) {
1305 CreatePeerConnection();
1306
1307 EXPECT_FALSE(pc_->AddIceCandidate(observer_.last_candidate_.get()));
1308 // SetRemoteDescription takes ownership of offer.
kwibergd1fe2812016-04-27 06:47:29 -07001309 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefab9b2d12015-10-14 11:33:11 -07001310 AddVideoStream(kStreamLabel1);
deadbeefc80741f2015-10-22 13:14:45 -07001311 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
kwiberg2bbff992016-03-16 11:03:04 -07001312 EXPECT_TRUE(DoSetRemoteDescription(offer.release()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001313
1314 // SetLocalDescription takes ownership of answer.
kwibergd1fe2812016-04-27 06:47:29 -07001315 std::unique_ptr<SessionDescriptionInterface> answer;
deadbeefc80741f2015-10-22 13:14:45 -07001316 EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
kwiberg2bbff992016-03-16 11:03:04 -07001317 EXPECT_TRUE(DoSetLocalDescription(answer.release()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001318
1319 EXPECT_TRUE_WAIT(observer_.last_candidate_.get() != NULL, kTimeout);
1320 EXPECT_TRUE_WAIT(observer_.ice_complete_, kTimeout);
1321
1322 EXPECT_TRUE(pc_->AddIceCandidate(observer_.last_candidate_.get()));
1323}
1324
deadbeefab9b2d12015-10-14 11:33:11 -07001325// Test that CreateOffer and CreateAnswer will fail if the track labels are
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001326// not unique.
1327TEST_F(PeerConnectionInterfaceTest, CreateOfferAnswerWithInvalidStream) {
1328 CreatePeerConnection();
1329 // Create a regular offer for the CreateAnswer test later.
kwibergd1fe2812016-04-27 06:47:29 -07001330 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefc80741f2015-10-22 13:14:45 -07001331 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
kwiberg2bbff992016-03-16 11:03:04 -07001332 EXPECT_TRUE(offer);
1333 offer.reset();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001334
1335 // Create a local stream with audio&video tracks having same label.
1336 AddAudioVideoStream(kStreamLabel1, "track_label", "track_label");
1337
1338 // Test CreateOffer
deadbeefc80741f2015-10-22 13:14:45 -07001339 EXPECT_FALSE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001340
1341 // Test CreateAnswer
kwibergd1fe2812016-04-27 06:47:29 -07001342 std::unique_ptr<SessionDescriptionInterface> answer;
deadbeefc80741f2015-10-22 13:14:45 -07001343 EXPECT_FALSE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001344}
1345
1346// Test that we will get different SSRCs for each tracks in the offer and answer
1347// we created.
1348TEST_F(PeerConnectionInterfaceTest, SsrcInOfferAnswer) {
1349 CreatePeerConnection();
1350 // Create a local stream with audio&video tracks having different labels.
1351 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
1352
1353 // Test CreateOffer
kwibergd1fe2812016-04-27 06:47:29 -07001354 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001355 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001356 int audio_ssrc = 0;
1357 int video_ssrc = 0;
1358 EXPECT_TRUE(GetFirstSsrc(GetFirstAudioContent(offer->description()),
1359 &audio_ssrc));
1360 EXPECT_TRUE(GetFirstSsrc(GetFirstVideoContent(offer->description()),
1361 &video_ssrc));
1362 EXPECT_NE(audio_ssrc, video_ssrc);
1363
1364 // Test CreateAnswer
1365 EXPECT_TRUE(DoSetRemoteDescription(offer.release()));
kwibergd1fe2812016-04-27 06:47:29 -07001366 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -07001367 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001368 audio_ssrc = 0;
1369 video_ssrc = 0;
1370 EXPECT_TRUE(GetFirstSsrc(GetFirstAudioContent(answer->description()),
1371 &audio_ssrc));
1372 EXPECT_TRUE(GetFirstSsrc(GetFirstVideoContent(answer->description()),
1373 &video_ssrc));
1374 EXPECT_NE(audio_ssrc, video_ssrc);
1375}
1376
deadbeefeb459812015-12-15 19:24:43 -08001377// Test that it's possible to call AddTrack on a MediaStream after adding
1378// the stream to a PeerConnection.
1379// TODO(deadbeef): Remove this test once this behavior is no longer supported.
1380TEST_F(PeerConnectionInterfaceTest, AddTrackAfterAddStream) {
1381 CreatePeerConnection();
1382 // Create audio stream and add to PeerConnection.
1383 AddVoiceStream(kStreamLabel1);
1384 MediaStreamInterface* stream = pc_->local_streams()->at(0);
1385
1386 // Add video track to the audio-only stream.
nisseaf510af2016-03-21 08:20:42 -07001387 scoped_refptr<VideoTrackInterface> video_track(pc_factory_->CreateVideoTrack(
1388 "video_label",
1389 pc_factory_->CreateVideoSource(new cricket::FakeVideoCapturer())));
deadbeefeb459812015-12-15 19:24:43 -08001390 stream->AddTrack(video_track.get());
1391
kwibergd1fe2812016-04-27 06:47:29 -07001392 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001393 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefeb459812015-12-15 19:24:43 -08001394
1395 const cricket::MediaContentDescription* video_desc =
1396 cricket::GetFirstVideoContentDescription(offer->description());
1397 EXPECT_TRUE(video_desc != nullptr);
1398}
1399
1400// Test that it's possible to call RemoveTrack on a MediaStream after adding
1401// the stream to a PeerConnection.
1402// TODO(deadbeef): Remove this test once this behavior is no longer supported.
1403TEST_F(PeerConnectionInterfaceTest, RemoveTrackAfterAddStream) {
1404 CreatePeerConnection();
1405 // Create audio/video stream and add to PeerConnection.
1406 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
1407 MediaStreamInterface* stream = pc_->local_streams()->at(0);
1408
1409 // Remove the video track.
1410 stream->RemoveTrack(stream->GetVideoTracks()[0]);
1411
kwibergd1fe2812016-04-27 06:47:29 -07001412 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001413 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefeb459812015-12-15 19:24:43 -08001414
1415 const cricket::MediaContentDescription* video_desc =
1416 cricket::GetFirstVideoContentDescription(offer->description());
1417 EXPECT_TRUE(video_desc == nullptr);
1418}
1419
deadbeefbd7d8f72015-12-18 16:58:44 -08001420// Test creating a sender with a stream ID, and ensure the ID is populated
1421// in the offer.
1422TEST_F(PeerConnectionInterfaceTest, CreateSenderWithStream) {
1423 CreatePeerConnection();
1424 pc_->CreateSender("video", kStreamLabel1);
1425
kwibergd1fe2812016-04-27 06:47:29 -07001426 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001427 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefbd7d8f72015-12-18 16:58:44 -08001428
1429 const cricket::MediaContentDescription* video_desc =
1430 cricket::GetFirstVideoContentDescription(offer->description());
1431 ASSERT_TRUE(video_desc != nullptr);
1432 ASSERT_EQ(1u, video_desc->streams().size());
1433 EXPECT_EQ(kStreamLabel1, video_desc->streams()[0].sync_label);
1434}
1435
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001436// Test that we can specify a certain track that we want statistics about.
1437TEST_F(PeerConnectionInterfaceTest, GetStatsForSpecificTrack) {
1438 InitiateCall();
1439 ASSERT_LT(0u, pc_->remote_streams()->count());
1440 ASSERT_LT(0u, pc_->remote_streams()->at(0)->GetAudioTracks().size());
1441 scoped_refptr<MediaStreamTrackInterface> remote_audio =
1442 pc_->remote_streams()->at(0)->GetAudioTracks()[0];
1443 EXPECT_TRUE(DoGetStats(remote_audio));
1444
1445 // Remove the stream. Since we are sending to our selves the local
1446 // and the remote stream is the same.
1447 pc_->RemoveStream(pc_->local_streams()->at(0));
1448 // Do a re-negotiation.
1449 CreateOfferReceiveAnswer();
1450
1451 ASSERT_EQ(0u, pc_->remote_streams()->count());
1452
1453 // Test that we still can get statistics for the old track. Even if it is not
1454 // sent any longer.
1455 EXPECT_TRUE(DoGetStats(remote_audio));
1456}
1457
1458// Test that we can get stats on a video track.
1459TEST_F(PeerConnectionInterfaceTest, GetStatsForVideoTrack) {
1460 InitiateCall();
1461 ASSERT_LT(0u, pc_->remote_streams()->count());
1462 ASSERT_LT(0u, pc_->remote_streams()->at(0)->GetVideoTracks().size());
1463 scoped_refptr<MediaStreamTrackInterface> remote_video =
1464 pc_->remote_streams()->at(0)->GetVideoTracks()[0];
1465 EXPECT_TRUE(DoGetStats(remote_video));
1466}
1467
1468// Test that we don't get statistics for an invalid track.
tommi@webrtc.org908f57e2014-07-21 11:44:39 +00001469// TODO(tommi): Fix this test. DoGetStats will return true
1470// for the unknown track (since GetStats is async), but no
1471// data is returned for the track.
1472TEST_F(PeerConnectionInterfaceTest, DISABLED_GetStatsForInvalidTrack) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001473 InitiateCall();
1474 scoped_refptr<AudioTrackInterface> unknown_audio_track(
1475 pc_factory_->CreateAudioTrack("unknown track", NULL));
1476 EXPECT_FALSE(DoGetStats(unknown_audio_track));
1477}
1478
1479// This test setup two RTP data channels in loop back.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001480TEST_F(PeerConnectionInterfaceTest, TestDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001481 FakeConstraints constraints;
1482 constraints.SetAllowRtpDataChannels();
1483 CreatePeerConnection(&constraints);
1484 scoped_refptr<DataChannelInterface> data1 =
1485 pc_->CreateDataChannel("test1", NULL);
1486 scoped_refptr<DataChannelInterface> data2 =
1487 pc_->CreateDataChannel("test2", NULL);
1488 ASSERT_TRUE(data1 != NULL);
kwibergd1fe2812016-04-27 06:47:29 -07001489 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001490 new MockDataChannelObserver(data1));
kwibergd1fe2812016-04-27 06:47:29 -07001491 std::unique_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001492 new MockDataChannelObserver(data2));
1493
1494 EXPECT_EQ(DataChannelInterface::kConnecting, data1->state());
1495 EXPECT_EQ(DataChannelInterface::kConnecting, data2->state());
1496 std::string data_to_send1 = "testing testing";
1497 std::string data_to_send2 = "testing something else";
1498 EXPECT_FALSE(data1->Send(DataBuffer(data_to_send1)));
1499
1500 CreateOfferReceiveAnswer();
1501 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1502 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
1503
1504 EXPECT_EQ(DataChannelInterface::kOpen, data1->state());
1505 EXPECT_EQ(DataChannelInterface::kOpen, data2->state());
1506 EXPECT_TRUE(data1->Send(DataBuffer(data_to_send1)));
1507 EXPECT_TRUE(data2->Send(DataBuffer(data_to_send2)));
1508
1509 EXPECT_EQ_WAIT(data_to_send1, observer1->last_message(), kTimeout);
1510 EXPECT_EQ_WAIT(data_to_send2, observer2->last_message(), kTimeout);
1511
1512 data1->Close();
1513 EXPECT_EQ(DataChannelInterface::kClosing, data1->state());
1514 CreateOfferReceiveAnswer();
1515 EXPECT_FALSE(observer1->IsOpen());
1516 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
1517 EXPECT_TRUE(observer2->IsOpen());
1518
1519 data_to_send2 = "testing something else again";
1520 EXPECT_TRUE(data2->Send(DataBuffer(data_to_send2)));
1521
1522 EXPECT_EQ_WAIT(data_to_send2, observer2->last_message(), kTimeout);
1523}
1524
1525// This test verifies that sendnig binary data over RTP data channels should
1526// fail.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001527TEST_F(PeerConnectionInterfaceTest, TestSendBinaryOnRtpDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001528 FakeConstraints constraints;
1529 constraints.SetAllowRtpDataChannels();
1530 CreatePeerConnection(&constraints);
1531 scoped_refptr<DataChannelInterface> data1 =
1532 pc_->CreateDataChannel("test1", NULL);
1533 scoped_refptr<DataChannelInterface> data2 =
1534 pc_->CreateDataChannel("test2", NULL);
1535 ASSERT_TRUE(data1 != NULL);
kwibergd1fe2812016-04-27 06:47:29 -07001536 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001537 new MockDataChannelObserver(data1));
kwibergd1fe2812016-04-27 06:47:29 -07001538 std::unique_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001539 new MockDataChannelObserver(data2));
1540
1541 EXPECT_EQ(DataChannelInterface::kConnecting, data1->state());
1542 EXPECT_EQ(DataChannelInterface::kConnecting, data2->state());
1543
1544 CreateOfferReceiveAnswer();
1545 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1546 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
1547
1548 EXPECT_EQ(DataChannelInterface::kOpen, data1->state());
1549 EXPECT_EQ(DataChannelInterface::kOpen, data2->state());
1550
jbaucheec21bd2016-03-20 06:15:43 -07001551 rtc::CopyOnWriteBuffer buffer("test", 4);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001552 EXPECT_FALSE(data1->Send(DataBuffer(buffer, true)));
1553}
1554
1555// This test setup a RTP data channels in loop back and test that a channel is
1556// opened even if the remote end answer with a zero SSRC.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001557TEST_F(PeerConnectionInterfaceTest, TestSendOnlyDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001558 FakeConstraints constraints;
1559 constraints.SetAllowRtpDataChannels();
1560 CreatePeerConnection(&constraints);
1561 scoped_refptr<DataChannelInterface> data1 =
1562 pc_->CreateDataChannel("test1", NULL);
kwibergd1fe2812016-04-27 06:47:29 -07001563 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001564 new MockDataChannelObserver(data1));
1565
1566 CreateOfferReceiveAnswerWithoutSsrc();
1567
1568 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1569
1570 data1->Close();
1571 EXPECT_EQ(DataChannelInterface::kClosing, data1->state());
1572 CreateOfferReceiveAnswerWithoutSsrc();
1573 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
1574 EXPECT_FALSE(observer1->IsOpen());
1575}
1576
1577// This test that if a data channel is added in an answer a receive only channel
1578// channel is created.
1579TEST_F(PeerConnectionInterfaceTest, TestReceiveOnlyDataChannel) {
1580 FakeConstraints constraints;
1581 constraints.SetAllowRtpDataChannels();
1582 CreatePeerConnection(&constraints);
1583
1584 std::string offer_label = "offer_channel";
1585 scoped_refptr<DataChannelInterface> offer_channel =
1586 pc_->CreateDataChannel(offer_label, NULL);
1587
1588 CreateOfferAsLocalDescription();
1589
1590 // Replace the data channel label in the offer and apply it as an answer.
1591 std::string receive_label = "answer_channel";
1592 std::string sdp;
1593 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001594 rtc::replace_substrs(offer_label.c_str(), offer_label.length(),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001595 receive_label.c_str(), receive_label.length(),
1596 &sdp);
1597 CreateAnswerAsRemoteDescription(sdp);
1598
1599 // Verify that a new incoming data channel has been created and that
1600 // it is open but can't we written to.
1601 ASSERT_TRUE(observer_.last_datachannel_ != NULL);
1602 DataChannelInterface* received_channel = observer_.last_datachannel_;
1603 EXPECT_EQ(DataChannelInterface::kConnecting, received_channel->state());
1604 EXPECT_EQ(receive_label, received_channel->label());
1605 EXPECT_FALSE(received_channel->Send(DataBuffer("something")));
1606
1607 // Verify that the channel we initially offered has been rejected.
1608 EXPECT_EQ(DataChannelInterface::kClosed, offer_channel->state());
1609
1610 // Do another offer / answer exchange and verify that the data channel is
1611 // opened.
1612 CreateOfferReceiveAnswer();
1613 EXPECT_EQ_WAIT(DataChannelInterface::kOpen, received_channel->state(),
1614 kTimeout);
1615}
1616
1617// This test that no data channel is returned if a reliable channel is
1618// requested.
1619// TODO(perkj): Remove this test once reliable channels are implemented.
1620TEST_F(PeerConnectionInterfaceTest, CreateReliableRtpDataChannelShouldFail) {
1621 FakeConstraints constraints;
1622 constraints.SetAllowRtpDataChannels();
1623 CreatePeerConnection(&constraints);
1624
1625 std::string label = "test";
1626 webrtc::DataChannelInit config;
1627 config.reliable = true;
1628 scoped_refptr<DataChannelInterface> channel =
1629 pc_->CreateDataChannel(label, &config);
1630 EXPECT_TRUE(channel == NULL);
1631}
1632
deadbeefab9b2d12015-10-14 11:33:11 -07001633// Verifies that duplicated label is not allowed for RTP data channel.
1634TEST_F(PeerConnectionInterfaceTest, RtpDuplicatedLabelNotAllowed) {
1635 FakeConstraints constraints;
1636 constraints.SetAllowRtpDataChannels();
1637 CreatePeerConnection(&constraints);
1638
1639 std::string label = "test";
1640 scoped_refptr<DataChannelInterface> channel =
1641 pc_->CreateDataChannel(label, nullptr);
1642 EXPECT_NE(channel, nullptr);
1643
1644 scoped_refptr<DataChannelInterface> dup_channel =
1645 pc_->CreateDataChannel(label, nullptr);
1646 EXPECT_EQ(dup_channel, nullptr);
1647}
1648
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001649// This tests that a SCTP data channel is returned using different
1650// DataChannelInit configurations.
1651TEST_F(PeerConnectionInterfaceTest, CreateSctpDataChannel) {
1652 FakeConstraints constraints;
1653 constraints.SetAllowDtlsSctpDataChannels();
1654 CreatePeerConnection(&constraints);
1655
1656 webrtc::DataChannelInit config;
1657
1658 scoped_refptr<DataChannelInterface> channel =
1659 pc_->CreateDataChannel("1", &config);
1660 EXPECT_TRUE(channel != NULL);
1661 EXPECT_TRUE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001662 EXPECT_TRUE(observer_.renegotiation_needed_);
1663 observer_.renegotiation_needed_ = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001664
1665 config.ordered = false;
1666 channel = pc_->CreateDataChannel("2", &config);
1667 EXPECT_TRUE(channel != NULL);
1668 EXPECT_TRUE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001669 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001670
1671 config.ordered = true;
1672 config.maxRetransmits = 0;
1673 channel = pc_->CreateDataChannel("3", &config);
1674 EXPECT_TRUE(channel != NULL);
1675 EXPECT_FALSE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001676 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001677
1678 config.maxRetransmits = -1;
1679 config.maxRetransmitTime = 0;
1680 channel = pc_->CreateDataChannel("4", &config);
1681 EXPECT_TRUE(channel != NULL);
1682 EXPECT_FALSE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001683 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001684}
1685
1686// This tests that no data channel is returned if both maxRetransmits and
1687// maxRetransmitTime are set for SCTP data channels.
1688TEST_F(PeerConnectionInterfaceTest,
1689 CreateSctpDataChannelShouldFailForInvalidConfig) {
1690 FakeConstraints constraints;
1691 constraints.SetAllowDtlsSctpDataChannels();
1692 CreatePeerConnection(&constraints);
1693
1694 std::string label = "test";
1695 webrtc::DataChannelInit config;
1696 config.maxRetransmits = 0;
1697 config.maxRetransmitTime = 0;
1698
1699 scoped_refptr<DataChannelInterface> channel =
1700 pc_->CreateDataChannel(label, &config);
1701 EXPECT_TRUE(channel == NULL);
1702}
1703
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001704// The test verifies that creating a SCTP data channel with an id already in use
1705// or out of range should fail.
1706TEST_F(PeerConnectionInterfaceTest,
1707 CreateSctpDataChannelWithInvalidIdShouldFail) {
1708 FakeConstraints constraints;
1709 constraints.SetAllowDtlsSctpDataChannels();
1710 CreatePeerConnection(&constraints);
1711
1712 webrtc::DataChannelInit config;
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001713 scoped_refptr<DataChannelInterface> channel;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001714
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001715 config.id = 1;
1716 channel = pc_->CreateDataChannel("1", &config);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001717 EXPECT_TRUE(channel != NULL);
1718 EXPECT_EQ(1, channel->id());
1719
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001720 channel = pc_->CreateDataChannel("x", &config);
1721 EXPECT_TRUE(channel == NULL);
1722
1723 config.id = cricket::kMaxSctpSid;
1724 channel = pc_->CreateDataChannel("max", &config);
1725 EXPECT_TRUE(channel != NULL);
1726 EXPECT_EQ(config.id, channel->id());
1727
1728 config.id = cricket::kMaxSctpSid + 1;
1729 channel = pc_->CreateDataChannel("x", &config);
1730 EXPECT_TRUE(channel == NULL);
1731}
1732
deadbeefab9b2d12015-10-14 11:33:11 -07001733// Verifies that duplicated label is allowed for SCTP data channel.
1734TEST_F(PeerConnectionInterfaceTest, SctpDuplicatedLabelAllowed) {
1735 FakeConstraints constraints;
1736 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
1737 true);
1738 CreatePeerConnection(&constraints);
1739
1740 std::string label = "test";
1741 scoped_refptr<DataChannelInterface> channel =
1742 pc_->CreateDataChannel(label, nullptr);
1743 EXPECT_NE(channel, nullptr);
1744
1745 scoped_refptr<DataChannelInterface> dup_channel =
1746 pc_->CreateDataChannel(label, nullptr);
1747 EXPECT_NE(dup_channel, nullptr);
1748}
1749
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001750// This test verifies that OnRenegotiationNeeded is fired for every new RTP
1751// DataChannel.
1752TEST_F(PeerConnectionInterfaceTest, RenegotiationNeededForNewRtpDataChannel) {
1753 FakeConstraints constraints;
1754 constraints.SetAllowRtpDataChannels();
1755 CreatePeerConnection(&constraints);
1756
1757 scoped_refptr<DataChannelInterface> dc1 =
1758 pc_->CreateDataChannel("test1", NULL);
1759 EXPECT_TRUE(observer_.renegotiation_needed_);
1760 observer_.renegotiation_needed_ = false;
1761
1762 scoped_refptr<DataChannelInterface> dc2 =
1763 pc_->CreateDataChannel("test2", NULL);
1764 EXPECT_TRUE(observer_.renegotiation_needed_);
1765}
1766
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001767// This test that a data channel closes when a PeerConnection is deleted/closed.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001768TEST_F(PeerConnectionInterfaceTest, DataChannelCloseWhenPeerConnectionClose) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001769 FakeConstraints constraints;
1770 constraints.SetAllowRtpDataChannels();
1771 CreatePeerConnection(&constraints);
1772
1773 scoped_refptr<DataChannelInterface> data1 =
1774 pc_->CreateDataChannel("test1", NULL);
1775 scoped_refptr<DataChannelInterface> data2 =
1776 pc_->CreateDataChannel("test2", NULL);
1777 ASSERT_TRUE(data1 != NULL);
kwibergd1fe2812016-04-27 06:47:29 -07001778 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001779 new MockDataChannelObserver(data1));
kwibergd1fe2812016-04-27 06:47:29 -07001780 std::unique_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001781 new MockDataChannelObserver(data2));
1782
1783 CreateOfferReceiveAnswer();
1784 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1785 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
1786
1787 ReleasePeerConnection();
1788 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
1789 EXPECT_EQ(DataChannelInterface::kClosed, data2->state());
1790}
1791
1792// This test that data channels can be rejected in an answer.
1793TEST_F(PeerConnectionInterfaceTest, TestRejectDataChannelInAnswer) {
1794 FakeConstraints constraints;
1795 constraints.SetAllowRtpDataChannels();
1796 CreatePeerConnection(&constraints);
1797
1798 scoped_refptr<DataChannelInterface> offer_channel(
1799 pc_->CreateDataChannel("offer_channel", NULL));
1800
1801 CreateOfferAsLocalDescription();
1802
1803 // Create an answer where the m-line for data channels are rejected.
1804 std::string sdp;
1805 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
1806 webrtc::JsepSessionDescription* answer = new webrtc::JsepSessionDescription(
1807 SessionDescriptionInterface::kAnswer);
1808 EXPECT_TRUE(answer->Initialize(sdp, NULL));
1809 cricket::ContentInfo* data_info =
1810 answer->description()->GetContentByName("data");
1811 data_info->rejected = true;
1812
1813 DoSetRemoteDescription(answer);
1814 EXPECT_EQ(DataChannelInterface::kClosed, offer_channel->state());
1815}
1816
1817// Test that we can create a session description from an SDP string from
1818// FireFox, use it as a remote session description, generate an answer and use
1819// the answer as a local description.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07001820TEST_F(PeerConnectionInterfaceTest, ReceiveFireFoxOffer) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001821 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001822 FakeConstraints constraints;
1823 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
1824 true);
1825 CreatePeerConnection(&constraints);
1826 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
1827 SessionDescriptionInterface* desc =
1828 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
jbauchfabe2c92015-07-16 13:43:14 -07001829 webrtc::kFireFoxSdpOffer, nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001830 EXPECT_TRUE(DoSetSessionDescription(desc, false));
1831 CreateAnswerAsLocalDescription();
1832 ASSERT_TRUE(pc_->local_description() != NULL);
1833 ASSERT_TRUE(pc_->remote_description() != NULL);
1834
1835 const cricket::ContentInfo* content =
1836 cricket::GetFirstAudioContent(pc_->local_description()->description());
1837 ASSERT_TRUE(content != NULL);
1838 EXPECT_FALSE(content->rejected);
1839
1840 content =
1841 cricket::GetFirstVideoContent(pc_->local_description()->description());
1842 ASSERT_TRUE(content != NULL);
1843 EXPECT_FALSE(content->rejected);
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00001844#ifdef HAVE_SCTP
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001845 content =
1846 cricket::GetFirstDataContent(pc_->local_description()->description());
1847 ASSERT_TRUE(content != NULL);
1848 EXPECT_TRUE(content->rejected);
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00001849#endif
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001850}
1851
1852// Test that we can create an audio only offer and receive an answer with a
1853// limited set of audio codecs and receive an updated offer with more audio
1854// codecs, where the added codecs are not supported.
1855TEST_F(PeerConnectionInterfaceTest, ReceiveUpdatedAudioOfferWithBadCodecs) {
1856 CreatePeerConnection();
1857 AddVoiceStream("audio_label");
1858 CreateOfferAsLocalDescription();
1859
1860 SessionDescriptionInterface* answer =
1861 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
jbauchfabe2c92015-07-16 13:43:14 -07001862 webrtc::kAudioSdp, nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001863 EXPECT_TRUE(DoSetSessionDescription(answer, false));
1864
1865 SessionDescriptionInterface* updated_offer =
1866 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
jbauchfabe2c92015-07-16 13:43:14 -07001867 webrtc::kAudioSdpWithUnsupportedCodecs,
1868 nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001869 EXPECT_TRUE(DoSetSessionDescription(updated_offer, false));
1870 CreateAnswerAsLocalDescription();
1871}
1872
deadbeefc80741f2015-10-22 13:14:45 -07001873// Test that if we're receiving (but not sending) a track, subsequent offers
1874// will have m-lines with a=recvonly.
1875TEST_F(PeerConnectionInterfaceTest, CreateSubsequentRecvOnlyOffer) {
1876 FakeConstraints constraints;
1877 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
1878 true);
1879 CreatePeerConnection(&constraints);
1880 CreateAndSetRemoteOffer(kSdpStringWithStream1);
1881 CreateAnswerAsLocalDescription();
1882
1883 // At this point we should be receiving stream 1, but not sending anything.
1884 // A new offer should be recvonly.
kwibergd1fe2812016-04-27 06:47:29 -07001885 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefc80741f2015-10-22 13:14:45 -07001886 DoCreateOffer(&offer, nullptr);
1887
1888 const cricket::ContentInfo* video_content =
1889 cricket::GetFirstVideoContent(offer->description());
1890 const cricket::VideoContentDescription* video_desc =
1891 static_cast<const cricket::VideoContentDescription*>(
1892 video_content->description);
1893 ASSERT_EQ(cricket::MD_RECVONLY, video_desc->direction());
1894
1895 const cricket::ContentInfo* audio_content =
1896 cricket::GetFirstAudioContent(offer->description());
1897 const cricket::AudioContentDescription* audio_desc =
1898 static_cast<const cricket::AudioContentDescription*>(
1899 audio_content->description);
1900 ASSERT_EQ(cricket::MD_RECVONLY, audio_desc->direction());
1901}
1902
1903// Test that if we're receiving (but not sending) a track, and the
1904// offerToReceiveVideo/offerToReceiveAudio constraints are explicitly set to
1905// false, the generated m-lines will be a=inactive.
1906TEST_F(PeerConnectionInterfaceTest, CreateSubsequentInactiveOffer) {
1907 FakeConstraints constraints;
1908 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
1909 true);
1910 CreatePeerConnection(&constraints);
1911 CreateAndSetRemoteOffer(kSdpStringWithStream1);
1912 CreateAnswerAsLocalDescription();
1913
1914 // At this point we should be receiving stream 1, but not sending anything.
1915 // A new offer would be recvonly, but we'll set the "no receive" constraints
1916 // to make it inactive.
kwibergd1fe2812016-04-27 06:47:29 -07001917 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefc80741f2015-10-22 13:14:45 -07001918 FakeConstraints offer_constraints;
1919 offer_constraints.AddMandatory(
1920 webrtc::MediaConstraintsInterface::kOfferToReceiveVideo, false);
1921 offer_constraints.AddMandatory(
1922 webrtc::MediaConstraintsInterface::kOfferToReceiveAudio, false);
1923 DoCreateOffer(&offer, &offer_constraints);
1924
1925 const cricket::ContentInfo* video_content =
1926 cricket::GetFirstVideoContent(offer->description());
1927 const cricket::VideoContentDescription* video_desc =
1928 static_cast<const cricket::VideoContentDescription*>(
1929 video_content->description);
1930 ASSERT_EQ(cricket::MD_INACTIVE, video_desc->direction());
1931
1932 const cricket::ContentInfo* audio_content =
1933 cricket::GetFirstAudioContent(offer->description());
1934 const cricket::AudioContentDescription* audio_desc =
1935 static_cast<const cricket::AudioContentDescription*>(
1936 audio_content->description);
1937 ASSERT_EQ(cricket::MD_INACTIVE, audio_desc->direction());
1938}
1939
deadbeef653b8e02015-11-11 12:55:10 -08001940// Test that we can use SetConfiguration to change the ICE servers of the
1941// PortAllocator.
1942TEST_F(PeerConnectionInterfaceTest, SetConfigurationChangesIceServers) {
1943 CreatePeerConnection();
1944
1945 PeerConnectionInterface::RTCConfiguration config;
1946 PeerConnectionInterface::IceServer server;
1947 server.uri = "stun:test_hostname";
1948 config.servers.push_back(server);
1949 EXPECT_TRUE(pc_->SetConfiguration(config));
1950
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08001951 EXPECT_EQ(1u, port_allocator_->stun_servers().size());
1952 EXPECT_EQ("test_hostname",
1953 port_allocator_->stun_servers().begin()->hostname());
deadbeef653b8e02015-11-11 12:55:10 -08001954}
1955
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001956TEST_F(PeerConnectionInterfaceTest, SetConfigurationChangesCandidateFilter) {
1957 CreatePeerConnection();
1958 PeerConnectionInterface::RTCConfiguration config;
1959 config.type = PeerConnectionInterface::kRelay;
1960 EXPECT_TRUE(pc_->SetConfiguration(config));
1961 EXPECT_EQ(cricket::CF_RELAY, port_allocator_->candidate_filter());
1962}
1963
1964// Test that when SetConfiguration changes both the pool size and other
1965// attributes, the pooled session is created with the updated attributes.
1966TEST_F(PeerConnectionInterfaceTest,
1967 SetConfigurationCreatesPooledSessionCorrectly) {
1968 CreatePeerConnection();
1969 PeerConnectionInterface::RTCConfiguration config;
1970 config.ice_candidate_pool_size = 1;
1971 PeerConnectionInterface::IceServer server;
1972 server.uri = kStunAddressOnly;
1973 config.servers.push_back(server);
1974 config.type = PeerConnectionInterface::kRelay;
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07001975 EXPECT_TRUE(pc_->SetConfiguration(config));
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001976
1977 const cricket::FakePortAllocatorSession* session =
1978 static_cast<const cricket::FakePortAllocatorSession*>(
1979 port_allocator_->GetPooledSession());
1980 ASSERT_NE(nullptr, session);
1981 EXPECT_EQ(1UL, session->stun_servers().size());
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001982}
1983
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001984// Test that PeerConnection::Close changes the states to closed and all remote
1985// tracks change state to ended.
1986TEST_F(PeerConnectionInterfaceTest, CloseAndTestStreamsAndStates) {
1987 // Initialize a PeerConnection and negotiate local and remote session
1988 // description.
1989 InitiateCall();
1990 ASSERT_EQ(1u, pc_->local_streams()->count());
1991 ASSERT_EQ(1u, pc_->remote_streams()->count());
1992
1993 pc_->Close();
1994
1995 EXPECT_EQ(PeerConnectionInterface::kClosed, pc_->signaling_state());
1996 EXPECT_EQ(PeerConnectionInterface::kIceConnectionClosed,
1997 pc_->ice_connection_state());
1998 EXPECT_EQ(PeerConnectionInterface::kIceGatheringComplete,
1999 pc_->ice_gathering_state());
2000
2001 EXPECT_EQ(1u, pc_->local_streams()->count());
2002 EXPECT_EQ(1u, pc_->remote_streams()->count());
2003
2004 scoped_refptr<MediaStreamInterface> remote_stream =
2005 pc_->remote_streams()->at(0);
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002006 // Track state may be updated asynchronously.
perkjd61bf802016-03-24 03:16:19 -07002007 EXPECT_EQ_WAIT(MediaStreamTrackInterface::kEnded,
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002008 remote_stream->GetAudioTracks()[0]->state(), kTimeout);
2009 EXPECT_EQ_WAIT(MediaStreamTrackInterface::kEnded,
2010 remote_stream->GetVideoTracks()[0]->state(), kTimeout);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002011}
2012
2013// Test that PeerConnection methods fails gracefully after
2014// PeerConnection::Close has been called.
2015TEST_F(PeerConnectionInterfaceTest, CloseAndTestMethods) {
2016 CreatePeerConnection();
2017 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
2018 CreateOfferAsRemoteDescription();
2019 CreateAnswerAsLocalDescription();
2020
2021 ASSERT_EQ(1u, pc_->local_streams()->count());
2022 scoped_refptr<MediaStreamInterface> local_stream =
2023 pc_->local_streams()->at(0);
2024
2025 pc_->Close();
2026
2027 pc_->RemoveStream(local_stream);
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +00002028 EXPECT_FALSE(pc_->AddStream(local_stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002029
2030 ASSERT_FALSE(local_stream->GetAudioTracks().empty());
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002031 rtc::scoped_refptr<webrtc::DtmfSenderInterface> dtmf_sender(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002032 pc_->CreateDtmfSender(local_stream->GetAudioTracks()[0]));
wu@webrtc.org66037362013-08-13 00:09:35 +00002033 EXPECT_TRUE(NULL == dtmf_sender); // local stream has been removed.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002034
2035 EXPECT_TRUE(pc_->CreateDataChannel("test", NULL) == NULL);
2036
2037 EXPECT_TRUE(pc_->local_description() != NULL);
2038 EXPECT_TRUE(pc_->remote_description() != NULL);
2039
kwibergd1fe2812016-04-27 06:47:29 -07002040 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07002041 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
kwibergd1fe2812016-04-27 06:47:29 -07002042 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -07002043 EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002044
2045 std::string sdp;
2046 ASSERT_TRUE(pc_->remote_description()->ToString(&sdp));
2047 SessionDescriptionInterface* remote_offer =
2048 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
2049 sdp, NULL);
2050 EXPECT_FALSE(DoSetRemoteDescription(remote_offer));
2051
2052 ASSERT_TRUE(pc_->local_description()->ToString(&sdp));
2053 SessionDescriptionInterface* local_offer =
2054 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
2055 sdp, NULL);
2056 EXPECT_FALSE(DoSetLocalDescription(local_offer));
2057}
2058
2059// Test that GetStats can still be called after PeerConnection::Close.
2060TEST_F(PeerConnectionInterfaceTest, CloseAndGetStats) {
2061 InitiateCall();
2062 pc_->Close();
2063 DoGetStats(NULL);
2064}
deadbeefab9b2d12015-10-14 11:33:11 -07002065
2066// NOTE: The series of tests below come from what used to be
2067// mediastreamsignaling_unittest.cc, and are mostly aimed at testing that
2068// setting a remote or local description has the expected effects.
2069
2070// This test verifies that the remote MediaStreams corresponding to a received
2071// SDP string is created. In this test the two separate MediaStreams are
2072// signaled.
2073TEST_F(PeerConnectionInterfaceTest, UpdateRemoteStreams) {
2074 FakeConstraints constraints;
2075 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2076 true);
2077 CreatePeerConnection(&constraints);
2078 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2079
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002080 rtc::scoped_refptr<StreamCollection> reference(CreateStreamCollection(1, 1));
deadbeefab9b2d12015-10-14 11:33:11 -07002081 EXPECT_TRUE(
2082 CompareStreamCollections(observer_.remote_streams(), reference.get()));
2083 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2084 EXPECT_TRUE(remote_stream->GetVideoTracks()[0]->GetSource() != nullptr);
2085
2086 // Create a session description based on another SDP with another
2087 // MediaStream.
2088 CreateAndSetRemoteOffer(kSdpStringWithStream1And2);
2089
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002090 rtc::scoped_refptr<StreamCollection> reference2(CreateStreamCollection(2, 1));
deadbeefab9b2d12015-10-14 11:33:11 -07002091 EXPECT_TRUE(
2092 CompareStreamCollections(observer_.remote_streams(), reference2.get()));
2093}
2094
2095// This test verifies that when remote tracks are added/removed from SDP, the
2096// created remote streams are updated appropriately.
2097TEST_F(PeerConnectionInterfaceTest,
2098 AddRemoveTrackFromExistingRemoteMediaStream) {
2099 FakeConstraints constraints;
2100 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2101 true);
2102 CreatePeerConnection(&constraints);
kwibergd1fe2812016-04-27 06:47:29 -07002103 std::unique_ptr<SessionDescriptionInterface> desc_ms1 =
kwiberg2bbff992016-03-16 11:03:04 -07002104 CreateSessionDescriptionAndReference(1, 1);
deadbeefab9b2d12015-10-14 11:33:11 -07002105 EXPECT_TRUE(DoSetRemoteDescription(desc_ms1.release()));
2106 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
2107 reference_collection_));
2108
2109 // Add extra audio and video tracks to the same MediaStream.
kwibergd1fe2812016-04-27 06:47:29 -07002110 std::unique_ptr<SessionDescriptionInterface> desc_ms1_two_tracks =
kwiberg2bbff992016-03-16 11:03:04 -07002111 CreateSessionDescriptionAndReference(2, 2);
deadbeefab9b2d12015-10-14 11:33:11 -07002112 EXPECT_TRUE(DoSetRemoteDescription(desc_ms1_two_tracks.release()));
2113 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
2114 reference_collection_));
perkjd61bf802016-03-24 03:16:19 -07002115 scoped_refptr<AudioTrackInterface> audio_track2 =
2116 observer_.remote_streams()->at(0)->GetAudioTracks()[1];
2117 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, audio_track2->state());
2118 scoped_refptr<VideoTrackInterface> video_track2 =
2119 observer_.remote_streams()->at(0)->GetVideoTracks()[1];
2120 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, video_track2->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002121
2122 // Remove the extra audio and video tracks.
kwibergd1fe2812016-04-27 06:47:29 -07002123 std::unique_ptr<SessionDescriptionInterface> desc_ms2 =
kwiberg2bbff992016-03-16 11:03:04 -07002124 CreateSessionDescriptionAndReference(1, 1);
perkjd61bf802016-03-24 03:16:19 -07002125 MockTrackObserver audio_track_observer(audio_track2);
2126 MockTrackObserver video_track_observer(video_track2);
2127
2128 EXPECT_CALL(audio_track_observer, OnChanged()).Times(Exactly(1));
2129 EXPECT_CALL(video_track_observer, OnChanged()).Times(Exactly(1));
deadbeefab9b2d12015-10-14 11:33:11 -07002130 EXPECT_TRUE(DoSetRemoteDescription(desc_ms2.release()));
2131 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
2132 reference_collection_));
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002133 // Track state may be updated asynchronously.
perkjd61bf802016-03-24 03:16:19 -07002134 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002135 audio_track2->state(), kTimeout);
2136 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
2137 video_track2->state(), kTimeout);
deadbeefab9b2d12015-10-14 11:33:11 -07002138}
2139
2140// This tests that remote tracks are ended if a local session description is set
2141// that rejects the media content type.
2142TEST_F(PeerConnectionInterfaceTest, RejectMediaContent) {
2143 FakeConstraints constraints;
2144 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2145 true);
2146 CreatePeerConnection(&constraints);
2147 // First create and set a remote offer, then reject its video content in our
2148 // answer.
2149 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2150 ASSERT_EQ(1u, observer_.remote_streams()->count());
2151 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2152 ASSERT_EQ(1u, remote_stream->GetVideoTracks().size());
2153 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2154
2155 rtc::scoped_refptr<webrtc::VideoTrackInterface> remote_video =
2156 remote_stream->GetVideoTracks()[0];
2157 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, remote_video->state());
2158 rtc::scoped_refptr<webrtc::AudioTrackInterface> remote_audio =
2159 remote_stream->GetAudioTracks()[0];
2160 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, remote_audio->state());
2161
kwibergd1fe2812016-04-27 06:47:29 -07002162 std::unique_ptr<SessionDescriptionInterface> local_answer;
kwiberg2bbff992016-03-16 11:03:04 -07002163 EXPECT_TRUE(DoCreateAnswer(&local_answer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07002164 cricket::ContentInfo* video_info =
2165 local_answer->description()->GetContentByName("video");
2166 video_info->rejected = true;
2167 EXPECT_TRUE(DoSetLocalDescription(local_answer.release()));
2168 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kEnded, remote_video->state());
2169 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, remote_audio->state());
2170
2171 // Now create an offer where we reject both video and audio.
kwibergd1fe2812016-04-27 06:47:29 -07002172 std::unique_ptr<SessionDescriptionInterface> local_offer;
kwiberg2bbff992016-03-16 11:03:04 -07002173 EXPECT_TRUE(DoCreateOffer(&local_offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07002174 video_info = local_offer->description()->GetContentByName("video");
2175 ASSERT_TRUE(video_info != nullptr);
2176 video_info->rejected = true;
2177 cricket::ContentInfo* audio_info =
2178 local_offer->description()->GetContentByName("audio");
2179 ASSERT_TRUE(audio_info != nullptr);
2180 audio_info->rejected = true;
2181 EXPECT_TRUE(DoSetLocalDescription(local_offer.release()));
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002182 // Track state may be updated asynchronously.
perkjd61bf802016-03-24 03:16:19 -07002183 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002184 remote_audio->state(), kTimeout);
2185 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
2186 remote_video->state(), kTimeout);
deadbeefab9b2d12015-10-14 11:33:11 -07002187}
2188
2189// This tests that we won't crash if the remote track has been removed outside
2190// of PeerConnection and then PeerConnection tries to reject the track.
2191TEST_F(PeerConnectionInterfaceTest, RemoveTrackThenRejectMediaContent) {
2192 FakeConstraints constraints;
2193 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2194 true);
2195 CreatePeerConnection(&constraints);
2196 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2197 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2198 remote_stream->RemoveTrack(remote_stream->GetVideoTracks()[0]);
2199 remote_stream->RemoveTrack(remote_stream->GetAudioTracks()[0]);
2200
kwibergd1fe2812016-04-27 06:47:29 -07002201 std::unique_ptr<SessionDescriptionInterface> local_answer(
deadbeefab9b2d12015-10-14 11:33:11 -07002202 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
2203 kSdpStringWithStream1, nullptr));
2204 cricket::ContentInfo* video_info =
2205 local_answer->description()->GetContentByName("video");
2206 video_info->rejected = true;
2207 cricket::ContentInfo* audio_info =
2208 local_answer->description()->GetContentByName("audio");
2209 audio_info->rejected = true;
2210 EXPECT_TRUE(DoSetLocalDescription(local_answer.release()));
2211
2212 // No crash is a pass.
2213}
2214
deadbeef5e97fb52015-10-15 12:49:08 -07002215// This tests that if a recvonly remote description is set, no remote streams
2216// will be created, even if the description contains SSRCs/MSIDs.
2217// See: https://code.google.com/p/webrtc/issues/detail?id=5054
2218TEST_F(PeerConnectionInterfaceTest, RecvonlyDescriptionDoesntCreateStream) {
2219 FakeConstraints constraints;
2220 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2221 true);
2222 CreatePeerConnection(&constraints);
2223
2224 std::string recvonly_offer = kSdpStringWithStream1;
2225 rtc::replace_substrs(kSendrecv, strlen(kSendrecv), kRecvonly,
2226 strlen(kRecvonly), &recvonly_offer);
2227 CreateAndSetRemoteOffer(recvonly_offer);
2228
2229 EXPECT_EQ(0u, observer_.remote_streams()->count());
2230}
2231
deadbeefab9b2d12015-10-14 11:33:11 -07002232// This tests that a default MediaStream is created if a remote session
2233// description doesn't contain any streams and no MSID support.
2234// It also tests that the default stream is updated if a video m-line is added
2235// in a subsequent session description.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002236TEST_F(PeerConnectionInterfaceTest, SdpWithoutMsidCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002237 FakeConstraints constraints;
2238 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2239 true);
2240 CreatePeerConnection(&constraints);
2241 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2242
2243 ASSERT_EQ(1u, observer_.remote_streams()->count());
2244 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2245
2246 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2247 EXPECT_EQ(0u, remote_stream->GetVideoTracks().size());
2248 EXPECT_EQ("default", remote_stream->label());
2249
2250 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2251 ASSERT_EQ(1u, observer_.remote_streams()->count());
2252 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2253 EXPECT_EQ("defaulta0", remote_stream->GetAudioTracks()[0]->id());
deadbeef884f5852016-01-15 09:20:04 -08002254 EXPECT_EQ(MediaStreamTrackInterface::kLive,
2255 remote_stream->GetAudioTracks()[0]->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002256 ASSERT_EQ(1u, remote_stream->GetVideoTracks().size());
2257 EXPECT_EQ("defaultv0", remote_stream->GetVideoTracks()[0]->id());
deadbeef884f5852016-01-15 09:20:04 -08002258 EXPECT_EQ(MediaStreamTrackInterface::kLive,
2259 remote_stream->GetVideoTracks()[0]->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002260}
2261
2262// This tests that a default MediaStream is created if a remote session
2263// description doesn't contain any streams and media direction is send only.
2264TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002265 SendOnlySdpWithoutMsidCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002266 FakeConstraints constraints;
2267 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2268 true);
2269 CreatePeerConnection(&constraints);
2270 CreateAndSetRemoteOffer(kSdpStringSendOnlyWithoutStreams);
2271
2272 ASSERT_EQ(1u, observer_.remote_streams()->count());
2273 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2274
2275 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2276 EXPECT_EQ(1u, remote_stream->GetVideoTracks().size());
2277 EXPECT_EQ("default", remote_stream->label());
2278}
2279
2280// This tests that it won't crash when PeerConnection tries to remove
2281// a remote track that as already been removed from the MediaStream.
2282TEST_F(PeerConnectionInterfaceTest, RemoveAlreadyGoneRemoteStream) {
2283 FakeConstraints constraints;
2284 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2285 true);
2286 CreatePeerConnection(&constraints);
2287 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2288 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2289 remote_stream->RemoveTrack(remote_stream->GetAudioTracks()[0]);
2290 remote_stream->RemoveTrack(remote_stream->GetVideoTracks()[0]);
2291
2292 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2293
2294 // No crash is a pass.
2295}
2296
2297// This tests that a default MediaStream is created if the remote session
2298// description doesn't contain any streams and don't contain an indication if
2299// MSID is supported.
2300TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002301 SdpWithoutMsidAndStreamsCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002302 FakeConstraints constraints;
2303 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2304 true);
2305 CreatePeerConnection(&constraints);
2306 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2307
2308 ASSERT_EQ(1u, observer_.remote_streams()->count());
2309 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2310 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2311 EXPECT_EQ(1u, remote_stream->GetVideoTracks().size());
2312}
2313
2314// This tests that a default MediaStream is not created if the remote session
2315// description doesn't contain any streams but does support MSID.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002316TEST_F(PeerConnectionInterfaceTest, SdpWithMsidDontCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002317 FakeConstraints constraints;
2318 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2319 true);
2320 CreatePeerConnection(&constraints);
2321 CreateAndSetRemoteOffer(kSdpStringWithMsidWithoutStreams);
2322 EXPECT_EQ(0u, observer_.remote_streams()->count());
2323}
2324
deadbeefbda7e0b2015-12-08 17:13:40 -08002325// This tests that when setting a new description, the old default tracks are
2326// not destroyed and recreated.
2327// See: https://bugs.chromium.org/p/webrtc/issues/detail?id=5250
Stefan Holmer102362b2016-03-18 09:39:07 +01002328TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002329 DefaultTracksNotDestroyedAndRecreated) {
deadbeefbda7e0b2015-12-08 17:13:40 -08002330 FakeConstraints constraints;
2331 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2332 true);
2333 CreatePeerConnection(&constraints);
2334 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2335
2336 ASSERT_EQ(1u, observer_.remote_streams()->count());
2337 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2338 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2339
2340 // Set the track to "disabled", then set a new description and ensure the
2341 // track is still disabled, which ensures it hasn't been recreated.
2342 remote_stream->GetAudioTracks()[0]->set_enabled(false);
2343 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2344 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2345 EXPECT_FALSE(remote_stream->GetAudioTracks()[0]->enabled());
2346}
2347
deadbeefab9b2d12015-10-14 11:33:11 -07002348// This tests that a default MediaStream is not created if a remote session
2349// description is updated to not have any MediaStreams.
2350TEST_F(PeerConnectionInterfaceTest, VerifyDefaultStreamIsNotCreated) {
2351 FakeConstraints constraints;
2352 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2353 true);
2354 CreatePeerConnection(&constraints);
2355 CreateAndSetRemoteOffer(kSdpStringWithStream1);
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002356 rtc::scoped_refptr<StreamCollection> reference(CreateStreamCollection(1, 1));
deadbeefab9b2d12015-10-14 11:33:11 -07002357 EXPECT_TRUE(
2358 CompareStreamCollections(observer_.remote_streams(), reference.get()));
2359
2360 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2361 EXPECT_EQ(0u, observer_.remote_streams()->count());
2362}
2363
2364// This tests that an RtpSender is created when the local description is set
2365// after adding a local stream.
2366// TODO(deadbeef): This test and the one below it need to be updated when
2367// an RtpSender's lifetime isn't determined by when a local description is set.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002368TEST_F(PeerConnectionInterfaceTest, LocalDescriptionChanged) {
deadbeefab9b2d12015-10-14 11:33:11 -07002369 FakeConstraints constraints;
2370 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2371 true);
2372 CreatePeerConnection(&constraints);
deadbeefab9b2d12015-10-14 11:33:11 -07002373
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002374 // Create an offer with 1 stream with 2 tracks of each type.
2375 rtc::scoped_refptr<StreamCollection> stream_collection =
2376 CreateStreamCollection(1, 2);
2377 pc_->AddStream(stream_collection->at(0));
2378 std::unique_ptr<SessionDescriptionInterface> offer;
2379 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2380 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
deadbeefab9b2d12015-10-14 11:33:11 -07002381
deadbeefab9b2d12015-10-14 11:33:11 -07002382 auto senders = pc_->GetSenders();
2383 EXPECT_EQ(4u, senders.size());
2384 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2385 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2386 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[1]));
2387 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[1]));
2388
2389 // Remove an audio and video track.
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002390 pc_->RemoveStream(stream_collection->at(0));
2391 stream_collection = CreateStreamCollection(1, 1);
2392 pc_->AddStream(stream_collection->at(0));
2393 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2394 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
2395
deadbeefab9b2d12015-10-14 11:33:11 -07002396 senders = pc_->GetSenders();
2397 EXPECT_EQ(2u, senders.size());
2398 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2399 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2400 EXPECT_FALSE(ContainsSender(senders, kAudioTracks[1]));
2401 EXPECT_FALSE(ContainsSender(senders, kVideoTracks[1]));
2402}
2403
2404// This tests that an RtpSender is created when the local description is set
2405// before adding a local stream.
2406TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002407 AddLocalStreamAfterLocalDescriptionChanged) {
deadbeefab9b2d12015-10-14 11:33:11 -07002408 FakeConstraints constraints;
2409 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2410 true);
2411 CreatePeerConnection(&constraints);
deadbeefab9b2d12015-10-14 11:33:11 -07002412
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002413 rtc::scoped_refptr<StreamCollection> stream_collection =
2414 CreateStreamCollection(1, 2);
2415 // Add a stream to create the offer, but remove it afterwards.
2416 pc_->AddStream(stream_collection->at(0));
2417 std::unique_ptr<SessionDescriptionInterface> offer;
2418 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2419 pc_->RemoveStream(stream_collection->at(0));
deadbeefab9b2d12015-10-14 11:33:11 -07002420
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002421 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
deadbeefab9b2d12015-10-14 11:33:11 -07002422 auto senders = pc_->GetSenders();
2423 EXPECT_EQ(0u, senders.size());
2424
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002425 pc_->AddStream(stream_collection->at(0));
deadbeefab9b2d12015-10-14 11:33:11 -07002426 senders = pc_->GetSenders();
2427 EXPECT_EQ(4u, senders.size());
2428 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2429 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2430 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[1]));
2431 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[1]));
2432}
2433
2434// This tests that the expected behavior occurs if the SSRC on a local track is
2435// changed when SetLocalDescription is called.
2436TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002437 ChangeSsrcOnTrackInLocalSessionDescription) {
deadbeefab9b2d12015-10-14 11:33:11 -07002438 FakeConstraints constraints;
2439 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2440 true);
2441 CreatePeerConnection(&constraints);
deadbeefab9b2d12015-10-14 11:33:11 -07002442
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002443 rtc::scoped_refptr<StreamCollection> stream_collection =
2444 CreateStreamCollection(2, 1);
2445 pc_->AddStream(stream_collection->at(0));
2446 std::unique_ptr<SessionDescriptionInterface> offer;
2447 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2448 // Grab a copy of the offer before it gets passed into the PC.
2449 std::unique_ptr<JsepSessionDescription> modified_offer(
2450 new JsepSessionDescription(JsepSessionDescription::kOffer));
2451 modified_offer->Initialize(offer->description()->Copy(), offer->session_id(),
2452 offer->session_version());
2453 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
deadbeefab9b2d12015-10-14 11:33:11 -07002454
deadbeefab9b2d12015-10-14 11:33:11 -07002455 auto senders = pc_->GetSenders();
2456 EXPECT_EQ(2u, senders.size());
2457 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2458 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2459
2460 // Change the ssrc of the audio and video track.
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002461 cricket::MediaContentDescription* desc =
2462 cricket::GetFirstAudioContentDescription(modified_offer->description());
2463 ASSERT_TRUE(desc != NULL);
2464 for (StreamParams& stream : desc->mutable_streams()) {
2465 for (unsigned int& ssrc : stream.ssrcs) {
2466 ++ssrc;
2467 }
2468 }
deadbeefab9b2d12015-10-14 11:33:11 -07002469
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002470 desc =
2471 cricket::GetFirstVideoContentDescription(modified_offer->description());
2472 ASSERT_TRUE(desc != NULL);
2473 for (StreamParams& stream : desc->mutable_streams()) {
2474 for (unsigned int& ssrc : stream.ssrcs) {
2475 ++ssrc;
2476 }
2477 }
2478
2479 EXPECT_TRUE(DoSetLocalDescription(modified_offer.release()));
deadbeefab9b2d12015-10-14 11:33:11 -07002480 senders = pc_->GetSenders();
2481 EXPECT_EQ(2u, senders.size());
2482 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2483 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2484 // TODO(deadbeef): Once RtpSenders expose parameters, check that the SSRC
2485 // changed.
2486}
2487
2488// This tests that the expected behavior occurs if a new session description is
2489// set with the same tracks, but on a different MediaStream.
Stefan Holmer55d6e7c2016-03-17 16:26:40 +01002490TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002491 SignalSameTracksInSeparateMediaStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002492 FakeConstraints constraints;
2493 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2494 true);
2495 CreatePeerConnection(&constraints);
deadbeefab9b2d12015-10-14 11:33:11 -07002496
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002497 rtc::scoped_refptr<StreamCollection> stream_collection =
2498 CreateStreamCollection(2, 1);
2499 pc_->AddStream(stream_collection->at(0));
2500 std::unique_ptr<SessionDescriptionInterface> offer;
2501 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2502 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
deadbeefab9b2d12015-10-14 11:33:11 -07002503
deadbeefab9b2d12015-10-14 11:33:11 -07002504 auto senders = pc_->GetSenders();
2505 EXPECT_EQ(2u, senders.size());
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002506 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0], kStreams[0]));
2507 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0], kStreams[0]));
deadbeefab9b2d12015-10-14 11:33:11 -07002508
2509 // Add a new MediaStream but with the same tracks as in the first stream.
2510 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream_1(
2511 webrtc::MediaStream::Create(kStreams[1]));
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002512 stream_1->AddTrack(stream_collection->at(0)->GetVideoTracks()[0]);
2513 stream_1->AddTrack(stream_collection->at(0)->GetAudioTracks()[0]);
deadbeefab9b2d12015-10-14 11:33:11 -07002514 pc_->AddStream(stream_1);
2515
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002516 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2517 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
deadbeefab9b2d12015-10-14 11:33:11 -07002518
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002519 auto new_senders = pc_->GetSenders();
2520 // Should be the same senders as before, but with updated stream id.
2521 // Note that this behavior is subject to change in the future.
2522 // We may decide the PC should ignore existing tracks in AddStream.
2523 EXPECT_EQ(senders, new_senders);
2524 EXPECT_TRUE(ContainsSender(new_senders, kAudioTracks[0], kStreams[1]));
2525 EXPECT_TRUE(ContainsSender(new_senders, kVideoTracks[0], kStreams[1]));
deadbeefab9b2d12015-10-14 11:33:11 -07002526}
2527
nisse51542be2016-02-12 02:27:06 -08002528// The PeerConnectionMediaConfig tests below verify that configuration
2529// and constraints are propagated into the MediaConfig passed to
2530// CreateMediaController. These settings are intended for MediaChannel
2531// constructors, but that is not exercised by these unittest.
2532class PeerConnectionFactoryForTest : public webrtc::PeerConnectionFactory {
2533 public:
2534 webrtc::MediaControllerInterface* CreateMediaController(
2535 const cricket::MediaConfig& config) const override {
2536 create_media_controller_called_ = true;
2537 create_media_controller_config_ = config;
2538
2539 webrtc::MediaControllerInterface* mc =
2540 PeerConnectionFactory::CreateMediaController(config);
2541 EXPECT_TRUE(mc != nullptr);
2542 return mc;
2543 }
2544
2545 // Mutable, so they can be modified in the above const-declared method.
2546 mutable bool create_media_controller_called_ = false;
2547 mutable cricket::MediaConfig create_media_controller_config_;
2548};
2549
2550class PeerConnectionMediaConfigTest : public testing::Test {
2551 protected:
2552 void SetUp() override {
nisseaf510af2016-03-21 08:20:42 -07002553 pcf_ = new rtc::RefCountedObject<PeerConnectionFactoryForTest>();
nisse51542be2016-02-12 02:27:06 -08002554 pcf_->Initialize();
2555 }
2556 const cricket::MediaConfig& TestCreatePeerConnection(
2557 const PeerConnectionInterface::RTCConfiguration& config,
2558 const MediaConstraintsInterface *constraints) {
2559 pcf_->create_media_controller_called_ = false;
2560
2561 scoped_refptr<PeerConnectionInterface> pc(
2562 pcf_->CreatePeerConnection(config, constraints, nullptr, nullptr,
2563 &observer_));
2564 EXPECT_TRUE(pc.get());
2565 EXPECT_TRUE(pcf_->create_media_controller_called_);
2566 return pcf_->create_media_controller_config_;
2567 }
2568
2569 scoped_refptr<PeerConnectionFactoryForTest> pcf_;
2570 MockPeerConnectionObserver observer_;
2571};
2572
2573// This test verifies the default behaviour with no constraints and a
2574// default RTCConfiguration.
2575TEST_F(PeerConnectionMediaConfigTest, TestDefaults) {
2576 PeerConnectionInterface::RTCConfiguration config;
2577 FakeConstraints constraints;
2578
2579 const cricket::MediaConfig& media_config =
2580 TestCreatePeerConnection(config, &constraints);
2581
2582 EXPECT_FALSE(media_config.enable_dscp);
nisse0db023a2016-03-01 04:29:59 -08002583 EXPECT_TRUE(media_config.video.enable_cpu_overuse_detection);
2584 EXPECT_FALSE(media_config.video.disable_prerenderer_smoothing);
2585 EXPECT_FALSE(media_config.video.suspend_below_min_bitrate);
nisse51542be2016-02-12 02:27:06 -08002586}
2587
2588// This test verifies the DSCP constraint is recognized and passed to
2589// the CreateMediaController call.
2590TEST_F(PeerConnectionMediaConfigTest, TestDscpConstraintTrue) {
2591 PeerConnectionInterface::RTCConfiguration config;
2592 FakeConstraints constraints;
2593
2594 constraints.AddOptional(webrtc::MediaConstraintsInterface::kEnableDscp, true);
2595 const cricket::MediaConfig& media_config =
2596 TestCreatePeerConnection(config, &constraints);
2597
2598 EXPECT_TRUE(media_config.enable_dscp);
2599}
2600
2601// This test verifies the cpu overuse detection constraint is
2602// recognized and passed to the CreateMediaController call.
2603TEST_F(PeerConnectionMediaConfigTest, TestCpuOveruseConstraintFalse) {
2604 PeerConnectionInterface::RTCConfiguration config;
2605 FakeConstraints constraints;
2606
2607 constraints.AddOptional(
2608 webrtc::MediaConstraintsInterface::kCpuOveruseDetection, false);
2609 const cricket::MediaConfig media_config =
2610 TestCreatePeerConnection(config, &constraints);
2611
nisse0db023a2016-03-01 04:29:59 -08002612 EXPECT_FALSE(media_config.video.enable_cpu_overuse_detection);
nisse51542be2016-02-12 02:27:06 -08002613}
2614
2615// This test verifies that the disable_prerenderer_smoothing flag is
2616// propagated from RTCConfiguration to the CreateMediaController call.
2617TEST_F(PeerConnectionMediaConfigTest, TestDisablePrerendererSmoothingTrue) {
2618 PeerConnectionInterface::RTCConfiguration config;
2619 FakeConstraints constraints;
2620
Niels Möller71bdda02016-03-31 12:59:59 +02002621 config.set_prerenderer_smoothing(false);
nisse51542be2016-02-12 02:27:06 -08002622 const cricket::MediaConfig& media_config =
2623 TestCreatePeerConnection(config, &constraints);
2624
nisse0db023a2016-03-01 04:29:59 -08002625 EXPECT_TRUE(media_config.video.disable_prerenderer_smoothing);
2626}
2627
2628// This test verifies the suspend below min bitrate constraint is
2629// recognized and passed to the CreateMediaController call.
2630TEST_F(PeerConnectionMediaConfigTest,
2631 TestSuspendBelowMinBitrateConstraintTrue) {
2632 PeerConnectionInterface::RTCConfiguration config;
2633 FakeConstraints constraints;
2634
2635 constraints.AddOptional(
2636 webrtc::MediaConstraintsInterface::kEnableVideoSuspendBelowMinBitrate,
2637 true);
2638 const cricket::MediaConfig media_config =
2639 TestCreatePeerConnection(config, &constraints);
2640
2641 EXPECT_TRUE(media_config.video.suspend_below_min_bitrate);
nisse51542be2016-02-12 02:27:06 -08002642}
2643
deadbeefab9b2d12015-10-14 11:33:11 -07002644// The following tests verify that session options are created correctly.
deadbeefc80741f2015-10-22 13:14:45 -07002645// TODO(deadbeef): Convert these tests to be more end-to-end. Instead of
2646// "verify options are converted correctly", should be "pass options into
2647// CreateOffer and verify the correct offer is produced."
deadbeefab9b2d12015-10-14 11:33:11 -07002648
2649TEST(CreateSessionOptionsTest, GetOptionsForOfferWithInvalidAudioOption) {
2650 RTCOfferAnswerOptions rtc_options;
2651 rtc_options.offer_to_receive_audio = RTCOfferAnswerOptions::kUndefined - 1;
2652
2653 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08002654 EXPECT_FALSE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002655
2656 rtc_options.offer_to_receive_audio =
2657 RTCOfferAnswerOptions::kMaxOfferToReceiveMedia + 1;
htaaac2dea2016-03-10 13:35:55 -08002658 EXPECT_FALSE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002659}
2660
2661TEST(CreateSessionOptionsTest, GetOptionsForOfferWithInvalidVideoOption) {
2662 RTCOfferAnswerOptions rtc_options;
2663 rtc_options.offer_to_receive_video = RTCOfferAnswerOptions::kUndefined - 1;
2664
2665 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08002666 EXPECT_FALSE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002667
2668 rtc_options.offer_to_receive_video =
2669 RTCOfferAnswerOptions::kMaxOfferToReceiveMedia + 1;
htaaac2dea2016-03-10 13:35:55 -08002670 EXPECT_FALSE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002671}
2672
2673// Test that a MediaSessionOptions is created for an offer if
deadbeefc80741f2015-10-22 13:14:45 -07002674// OfferToReceiveAudio and OfferToReceiveVideo options are set.
deadbeefab9b2d12015-10-14 11:33:11 -07002675TEST(CreateSessionOptionsTest, GetMediaSessionOptionsForOfferWithAudioVideo) {
2676 RTCOfferAnswerOptions rtc_options;
2677 rtc_options.offer_to_receive_audio = 1;
2678 rtc_options.offer_to_receive_video = 1;
2679
2680 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08002681 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002682 EXPECT_TRUE(options.has_audio());
2683 EXPECT_TRUE(options.has_video());
2684 EXPECT_TRUE(options.bundle_enabled);
2685}
2686
2687// Test that a correct MediaSessionOptions is created for an offer if
deadbeefc80741f2015-10-22 13:14:45 -07002688// OfferToReceiveAudio is set.
deadbeefab9b2d12015-10-14 11:33:11 -07002689TEST(CreateSessionOptionsTest, GetMediaSessionOptionsForOfferWithAudio) {
2690 RTCOfferAnswerOptions rtc_options;
2691 rtc_options.offer_to_receive_audio = 1;
2692
2693 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08002694 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002695 EXPECT_TRUE(options.has_audio());
2696 EXPECT_FALSE(options.has_video());
2697 EXPECT_TRUE(options.bundle_enabled);
2698}
2699
2700// Test that a correct MediaSessionOptions is created for an offer if
deadbeefc80741f2015-10-22 13:14:45 -07002701// the default OfferOptions are used.
deadbeefab9b2d12015-10-14 11:33:11 -07002702TEST(CreateSessionOptionsTest, GetDefaultMediaSessionOptionsForOffer) {
2703 RTCOfferAnswerOptions rtc_options;
2704
2705 cricket::MediaSessionOptions options;
deadbeef0ed85b22016-02-23 17:24:52 -08002706 options.transport_options["audio"] = cricket::TransportOptions();
2707 options.transport_options["video"] = cricket::TransportOptions();
htaaac2dea2016-03-10 13:35:55 -08002708 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefc80741f2015-10-22 13:14:45 -07002709 EXPECT_TRUE(options.has_audio());
deadbeefab9b2d12015-10-14 11:33:11 -07002710 EXPECT_FALSE(options.has_video());
deadbeefc80741f2015-10-22 13:14:45 -07002711 EXPECT_TRUE(options.bundle_enabled);
deadbeefab9b2d12015-10-14 11:33:11 -07002712 EXPECT_TRUE(options.vad_enabled);
deadbeef0ed85b22016-02-23 17:24:52 -08002713 EXPECT_FALSE(options.transport_options["audio"].ice_restart);
2714 EXPECT_FALSE(options.transport_options["video"].ice_restart);
deadbeefab9b2d12015-10-14 11:33:11 -07002715}
2716
2717// Test that a correct MediaSessionOptions is created for an offer if
deadbeefc80741f2015-10-22 13:14:45 -07002718// OfferToReceiveVideo is set.
deadbeefab9b2d12015-10-14 11:33:11 -07002719TEST(CreateSessionOptionsTest, GetMediaSessionOptionsForOfferWithVideo) {
2720 RTCOfferAnswerOptions rtc_options;
2721 rtc_options.offer_to_receive_audio = 0;
2722 rtc_options.offer_to_receive_video = 1;
2723
2724 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08002725 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002726 EXPECT_FALSE(options.has_audio());
2727 EXPECT_TRUE(options.has_video());
2728 EXPECT_TRUE(options.bundle_enabled);
2729}
2730
2731// Test that a correct MediaSessionOptions is created for an offer if
2732// UseRtpMux is set to false.
2733TEST(CreateSessionOptionsTest,
2734 GetMediaSessionOptionsForOfferWithBundleDisabled) {
2735 RTCOfferAnswerOptions rtc_options;
2736 rtc_options.offer_to_receive_audio = 1;
2737 rtc_options.offer_to_receive_video = 1;
2738 rtc_options.use_rtp_mux = false;
2739
2740 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08002741 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002742 EXPECT_TRUE(options.has_audio());
2743 EXPECT_TRUE(options.has_video());
2744 EXPECT_FALSE(options.bundle_enabled);
2745}
2746
2747// Test that a correct MediaSessionOptions is created to restart ice if
2748// IceRestart is set. It also tests that subsequent MediaSessionOptions don't
Taylor Brandstetterf475d362016-01-08 15:35:57 -08002749// have |audio_transport_options.ice_restart| etc. set.
deadbeefab9b2d12015-10-14 11:33:11 -07002750TEST(CreateSessionOptionsTest, GetMediaSessionOptionsForOfferWithIceRestart) {
2751 RTCOfferAnswerOptions rtc_options;
2752 rtc_options.ice_restart = true;
2753
2754 cricket::MediaSessionOptions options;
deadbeef0ed85b22016-02-23 17:24:52 -08002755 options.transport_options["audio"] = cricket::TransportOptions();
2756 options.transport_options["video"] = cricket::TransportOptions();
htaaac2dea2016-03-10 13:35:55 -08002757 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeef0ed85b22016-02-23 17:24:52 -08002758 EXPECT_TRUE(options.transport_options["audio"].ice_restart);
2759 EXPECT_TRUE(options.transport_options["video"].ice_restart);
deadbeefab9b2d12015-10-14 11:33:11 -07002760
2761 rtc_options = RTCOfferAnswerOptions();
htaaac2dea2016-03-10 13:35:55 -08002762 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeef0ed85b22016-02-23 17:24:52 -08002763 EXPECT_FALSE(options.transport_options["audio"].ice_restart);
2764 EXPECT_FALSE(options.transport_options["video"].ice_restart);
deadbeefab9b2d12015-10-14 11:33:11 -07002765}
2766
2767// Test that the MediaConstraints in an answer don't affect if audio and video
2768// is offered in an offer but that if kOfferToReceiveAudio or
2769// kOfferToReceiveVideo constraints are true in an offer, the media type will be
2770// included in subsequent answers.
2771TEST(CreateSessionOptionsTest, MediaConstraintsInAnswer) {
2772 FakeConstraints answer_c;
2773 answer_c.SetMandatoryReceiveAudio(true);
2774 answer_c.SetMandatoryReceiveVideo(true);
2775
2776 cricket::MediaSessionOptions answer_options;
2777 EXPECT_TRUE(ParseConstraintsForAnswer(&answer_c, &answer_options));
2778 EXPECT_TRUE(answer_options.has_audio());
2779 EXPECT_TRUE(answer_options.has_video());
2780
deadbeefc80741f2015-10-22 13:14:45 -07002781 RTCOfferAnswerOptions rtc_offer_options;
deadbeefab9b2d12015-10-14 11:33:11 -07002782
2783 cricket::MediaSessionOptions offer_options;
htaaac2dea2016-03-10 13:35:55 -08002784 EXPECT_TRUE(
2785 ExtractMediaSessionOptions(rtc_offer_options, false, &offer_options));
deadbeefc80741f2015-10-22 13:14:45 -07002786 EXPECT_TRUE(offer_options.has_audio());
htaaac2dea2016-03-10 13:35:55 -08002787 EXPECT_TRUE(offer_options.has_video());
deadbeefab9b2d12015-10-14 11:33:11 -07002788
deadbeefc80741f2015-10-22 13:14:45 -07002789 RTCOfferAnswerOptions updated_rtc_offer_options;
2790 updated_rtc_offer_options.offer_to_receive_audio = 1;
2791 updated_rtc_offer_options.offer_to_receive_video = 1;
deadbeefab9b2d12015-10-14 11:33:11 -07002792
2793 cricket::MediaSessionOptions updated_offer_options;
htaaac2dea2016-03-10 13:35:55 -08002794 EXPECT_TRUE(ExtractMediaSessionOptions(updated_rtc_offer_options, false,
htaa2a49d92016-03-04 02:51:39 -08002795 &updated_offer_options));
deadbeefab9b2d12015-10-14 11:33:11 -07002796 EXPECT_TRUE(updated_offer_options.has_audio());
2797 EXPECT_TRUE(updated_offer_options.has_video());
2798
2799 // Since an offer has been created with both audio and video, subsequent
2800 // offers and answers should contain both audio and video.
2801 // Answers will only contain the media types that exist in the offer
2802 // regardless of the value of |updated_answer_options.has_audio| and
2803 // |updated_answer_options.has_video|.
2804 FakeConstraints updated_answer_c;
2805 answer_c.SetMandatoryReceiveAudio(false);
2806 answer_c.SetMandatoryReceiveVideo(false);
2807
2808 cricket::MediaSessionOptions updated_answer_options;
2809 EXPECT_TRUE(
2810 ParseConstraintsForAnswer(&updated_answer_c, &updated_answer_options));
2811 EXPECT_TRUE(updated_answer_options.has_audio());
2812 EXPECT_TRUE(updated_answer_options.has_video());
deadbeefab9b2d12015-10-14 11:33:11 -07002813}