blob: 245e81c1c12c1d247b09ebcd37033a7073d6940c [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
Henrik Kjellander15583c12016-02-10 10:53:12 +010015#include "webrtc/api/audiotrack.h"
16#include "webrtc/api/jsepsessiondescription.h"
17#include "webrtc/api/mediastream.h"
18#include "webrtc/api/mediastreaminterface.h"
19#include "webrtc/api/peerconnection.h"
20#include "webrtc/api/peerconnectioninterface.h"
21#include "webrtc/api/rtpreceiverinterface.h"
22#include "webrtc/api/rtpsenderinterface.h"
23#include "webrtc/api/streamcollection.h"
Henrik Kjellander15583c12016-02-10 10:53:12 +010024#include "webrtc/api/test/fakeconstraints.h"
Henrik Boströmd79599d2016-06-01 13:58:50 +020025#include "webrtc/api/test/fakertccertificategenerator.h"
nisseaf510af2016-03-21 08:20:42 -070026#include "webrtc/api/test/fakevideotracksource.h"
Henrik Kjellander15583c12016-02-10 10:53:12 +010027#include "webrtc/api/test/mockpeerconnectionobservers.h"
28#include "webrtc/api/test/testsdpstrings.h"
perkja3ede6c2016-03-08 01:27:48 +010029#include "webrtc/api/videocapturertracksource.h"
Henrik Kjellander15583c12016-02-10 10:53:12 +010030#include "webrtc/api/videotrack.h"
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000031#include "webrtc/base/gunit.h"
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000032#include "webrtc/base/ssladapter.h"
33#include "webrtc/base/sslstreamadapter.h"
34#include "webrtc/base/stringutils.h"
35#include "webrtc/base/thread.h"
kjellandera96e2d72016-02-04 23:52:28 -080036#include "webrtc/media/base/fakevideocapturer.h"
37#include "webrtc/media/sctp/sctpdataengine.h"
Taylor Brandstettera1c30352016-05-13 08:15:11 -070038#include "webrtc/p2p/base/fakeportallocator.h"
zhihuang29ff8442016-07-27 11:07:25 -070039#include "webrtc/p2p/base/faketransportcontroller.h"
kjellander@webrtc.org9b8df252016-02-12 06:47:59 +010040#include "webrtc/pc/mediasession.h"
kwibergac9f8762016-09-30 22:29:43 -070041#include "webrtc/test/gmock.h"
42
43#ifdef WEBRTC_ANDROID
44#include "webrtc/api/test/androidtestinitializer.h"
45#endif
henrike@webrtc.org28e20752013-07-10 00:45:36 +000046
47static const char kStreamLabel1[] = "local_stream_1";
48static const char kStreamLabel2[] = "local_stream_2";
49static const char kStreamLabel3[] = "local_stream_3";
50static const int kDefaultStunPort = 3478;
51static const char kStunAddressOnly[] = "stun:address";
52static const char kStunInvalidPort[] = "stun:address:-1";
53static const char kStunAddressPortAndMore1[] = "stun:address:port:more";
54static const char kStunAddressPortAndMore2[] = "stun:address:port more";
55static const char kTurnIceServerUri[] = "turn:user@turn.example.org";
56static const char kTurnUsername[] = "user";
57static const char kTurnPassword[] = "password";
58static const char kTurnHostname[] = "turn.example.org";
Peter Boström0c4e06b2015-10-07 12:23:21 +020059static const uint32_t kTimeout = 10000U;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000060
deadbeefab9b2d12015-10-14 11:33:11 -070061static const char kStreams[][8] = {"stream1", "stream2"};
62static const char kAudioTracks[][32] = {"audiotrack0", "audiotrack1"};
63static const char kVideoTracks[][32] = {"videotrack0", "videotrack1"};
64
deadbeef5e97fb52015-10-15 12:49:08 -070065static const char kRecvonly[] = "recvonly";
66static const char kSendrecv[] = "sendrecv";
67
deadbeefab9b2d12015-10-14 11:33:11 -070068// Reference SDP with a MediaStream with label "stream1" and audio track with
69// id "audio_1" and a video track with id "video_1;
70static const char kSdpStringWithStream1[] =
71 "v=0\r\n"
72 "o=- 0 0 IN IP4 127.0.0.1\r\n"
73 "s=-\r\n"
74 "t=0 0\r\n"
75 "a=ice-ufrag:e5785931\r\n"
76 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
77 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
78 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
79 "m=audio 1 RTP/AVPF 103\r\n"
80 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -070081 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -070082 "a=rtpmap:103 ISAC/16000\r\n"
83 "a=ssrc:1 cname:stream1\r\n"
84 "a=ssrc:1 mslabel:stream1\r\n"
85 "a=ssrc:1 label:audiotrack0\r\n"
86 "m=video 1 RTP/AVPF 120\r\n"
87 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -070088 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -070089 "a=rtpmap:120 VP8/90000\r\n"
90 "a=ssrc:2 cname:stream1\r\n"
91 "a=ssrc:2 mslabel:stream1\r\n"
92 "a=ssrc:2 label:videotrack0\r\n";
93
94// Reference SDP with two MediaStreams with label "stream1" and "stream2. Each
95// MediaStreams have one audio track and one video track.
96// This uses MSID.
97static const char kSdpStringWithStream1And2[] =
98 "v=0\r\n"
99 "o=- 0 0 IN IP4 127.0.0.1\r\n"
100 "s=-\r\n"
101 "t=0 0\r\n"
102 "a=ice-ufrag:e5785931\r\n"
103 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
104 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
105 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
106 "a=msid-semantic: WMS stream1 stream2\r\n"
107 "m=audio 1 RTP/AVPF 103\r\n"
108 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700109 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700110 "a=rtpmap:103 ISAC/16000\r\n"
111 "a=ssrc:1 cname:stream1\r\n"
112 "a=ssrc:1 msid:stream1 audiotrack0\r\n"
113 "a=ssrc:3 cname:stream2\r\n"
114 "a=ssrc:3 msid:stream2 audiotrack1\r\n"
115 "m=video 1 RTP/AVPF 120\r\n"
116 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700117 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700118 "a=rtpmap:120 VP8/0\r\n"
119 "a=ssrc:2 cname:stream1\r\n"
120 "a=ssrc:2 msid:stream1 videotrack0\r\n"
121 "a=ssrc:4 cname:stream2\r\n"
122 "a=ssrc:4 msid:stream2 videotrack1\r\n";
123
124// Reference SDP without MediaStreams. Msid is not supported.
125static const char kSdpStringWithoutStreams[] =
126 "v=0\r\n"
127 "o=- 0 0 IN IP4 127.0.0.1\r\n"
128 "s=-\r\n"
129 "t=0 0\r\n"
130 "a=ice-ufrag:e5785931\r\n"
131 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
132 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
133 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
134 "m=audio 1 RTP/AVPF 103\r\n"
135 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700136 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700137 "a=rtpmap:103 ISAC/16000\r\n"
138 "m=video 1 RTP/AVPF 120\r\n"
139 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700140 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700141 "a=rtpmap:120 VP8/90000\r\n";
142
143// Reference SDP without MediaStreams. Msid is supported.
144static const char kSdpStringWithMsidWithoutStreams[] =
145 "v=0\r\n"
146 "o=- 0 0 IN IP4 127.0.0.1\r\n"
147 "s=-\r\n"
148 "t=0 0\r\n"
149 "a=ice-ufrag:e5785931\r\n"
150 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
151 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
152 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
153 "a=msid-semantic: WMS\r\n"
154 "m=audio 1 RTP/AVPF 103\r\n"
155 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700156 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700157 "a=rtpmap:103 ISAC/16000\r\n"
158 "m=video 1 RTP/AVPF 120\r\n"
159 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700160 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700161 "a=rtpmap:120 VP8/90000\r\n";
162
163// Reference SDP without MediaStreams and audio only.
164static const char kSdpStringWithoutStreamsAudioOnly[] =
165 "v=0\r\n"
166 "o=- 0 0 IN IP4 127.0.0.1\r\n"
167 "s=-\r\n"
168 "t=0 0\r\n"
169 "a=ice-ufrag:e5785931\r\n"
170 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
171 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
172 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
173 "m=audio 1 RTP/AVPF 103\r\n"
174 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700175 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700176 "a=rtpmap:103 ISAC/16000\r\n";
177
178// Reference SENDONLY SDP without MediaStreams. Msid is not supported.
179static const char kSdpStringSendOnlyWithoutStreams[] =
180 "v=0\r\n"
181 "o=- 0 0 IN IP4 127.0.0.1\r\n"
182 "s=-\r\n"
183 "t=0 0\r\n"
184 "a=ice-ufrag:e5785931\r\n"
185 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
186 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
187 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
188 "m=audio 1 RTP/AVPF 103\r\n"
189 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700190 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700191 "a=sendonly\r\n"
192 "a=rtpmap:103 ISAC/16000\r\n"
193 "m=video 1 RTP/AVPF 120\r\n"
194 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700195 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700196 "a=sendonly\r\n"
197 "a=rtpmap:120 VP8/90000\r\n";
198
199static const char kSdpStringInit[] =
200 "v=0\r\n"
201 "o=- 0 0 IN IP4 127.0.0.1\r\n"
202 "s=-\r\n"
203 "t=0 0\r\n"
204 "a=ice-ufrag:e5785931\r\n"
205 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
206 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
207 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
208 "a=msid-semantic: WMS\r\n";
209
210static const char kSdpStringAudio[] =
211 "m=audio 1 RTP/AVPF 103\r\n"
212 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700213 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700214 "a=rtpmap:103 ISAC/16000\r\n";
215
216static const char kSdpStringVideo[] =
217 "m=video 1 RTP/AVPF 120\r\n"
218 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700219 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700220 "a=rtpmap:120 VP8/90000\r\n";
221
222static const char kSdpStringMs1Audio0[] =
223 "a=ssrc:1 cname:stream1\r\n"
224 "a=ssrc:1 msid:stream1 audiotrack0\r\n";
225
226static const char kSdpStringMs1Video0[] =
227 "a=ssrc:2 cname:stream1\r\n"
228 "a=ssrc:2 msid:stream1 videotrack0\r\n";
229
230static const char kSdpStringMs1Audio1[] =
231 "a=ssrc:3 cname:stream1\r\n"
232 "a=ssrc:3 msid:stream1 audiotrack1\r\n";
233
234static const char kSdpStringMs1Video1[] =
235 "a=ssrc:4 cname:stream1\r\n"
236 "a=ssrc:4 msid:stream1 videotrack1\r\n";
237
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000238#define MAYBE_SKIP_TEST(feature) \
239 if (!(feature())) { \
240 LOG(LS_INFO) << "Feature disabled... skipping"; \
241 return; \
242 }
243
perkjd61bf802016-03-24 03:16:19 -0700244using ::testing::Exactly;
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700245using cricket::StreamParams;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000246using webrtc::AudioSourceInterface;
deadbeefab9b2d12015-10-14 11:33:11 -0700247using webrtc::AudioTrack;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000248using webrtc::AudioTrackInterface;
249using webrtc::DataBuffer;
250using webrtc::DataChannelInterface;
251using webrtc::FakeConstraints;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000252using webrtc::IceCandidateInterface;
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700253using webrtc::JsepSessionDescription;
deadbeefc80741f2015-10-22 13:14:45 -0700254using webrtc::MediaConstraintsInterface;
deadbeefab9b2d12015-10-14 11:33:11 -0700255using webrtc::MediaStream;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000256using webrtc::MediaStreamInterface;
257using webrtc::MediaStreamTrackInterface;
258using webrtc::MockCreateSessionDescriptionObserver;
259using webrtc::MockDataChannelObserver;
260using webrtc::MockSetSessionDescriptionObserver;
261using webrtc::MockStatsObserver;
perkjd61bf802016-03-24 03:16:19 -0700262using webrtc::NotifierInterface;
263using webrtc::ObserverInterface;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000264using webrtc::PeerConnectionInterface;
265using webrtc::PeerConnectionObserver;
deadbeefab9b2d12015-10-14 11:33:11 -0700266using webrtc::RtpReceiverInterface;
267using webrtc::RtpSenderInterface;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000268using webrtc::SdpParseError;
269using webrtc::SessionDescriptionInterface;
deadbeefab9b2d12015-10-14 11:33:11 -0700270using webrtc::StreamCollection;
271using webrtc::StreamCollectionInterface;
perkja3ede6c2016-03-08 01:27:48 +0100272using webrtc::VideoTrackSourceInterface;
deadbeefab9b2d12015-10-14 11:33:11 -0700273using webrtc::VideoTrack;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000274using webrtc::VideoTrackInterface;
275
deadbeefab9b2d12015-10-14 11:33:11 -0700276typedef PeerConnectionInterface::RTCOfferAnswerOptions RTCOfferAnswerOptions;
277
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000278namespace {
279
280// Gets the first ssrc of given content type from the ContentInfo.
281bool GetFirstSsrc(const cricket::ContentInfo* content_info, int* ssrc) {
282 if (!content_info || !ssrc) {
283 return false;
284 }
285 const cricket::MediaContentDescription* media_desc =
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000286 static_cast<const cricket::MediaContentDescription*>(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000287 content_info->description);
288 if (!media_desc || media_desc->streams().empty()) {
289 return false;
290 }
291 *ssrc = media_desc->streams().begin()->first_ssrc();
292 return true;
293}
294
295void SetSsrcToZero(std::string* sdp) {
296 const char kSdpSsrcAtribute[] = "a=ssrc:";
297 const char kSdpSsrcAtributeZero[] = "a=ssrc:0";
298 size_t ssrc_pos = 0;
299 while ((ssrc_pos = sdp->find(kSdpSsrcAtribute, ssrc_pos)) !=
300 std::string::npos) {
301 size_t end_ssrc = sdp->find(" ", ssrc_pos);
302 sdp->replace(ssrc_pos, end_ssrc - ssrc_pos, kSdpSsrcAtributeZero);
303 ssrc_pos = end_ssrc;
304 }
305}
306
deadbeefab9b2d12015-10-14 11:33:11 -0700307// Check if |streams| contains the specified track.
308bool ContainsTrack(const std::vector<cricket::StreamParams>& streams,
309 const std::string& stream_label,
310 const std::string& track_id) {
311 for (const cricket::StreamParams& params : streams) {
312 if (params.sync_label == stream_label && params.id == track_id) {
313 return true;
314 }
315 }
316 return false;
317}
318
319// Check if |senders| contains the specified sender, by id.
320bool ContainsSender(
321 const std::vector<rtc::scoped_refptr<RtpSenderInterface>>& senders,
322 const std::string& id) {
323 for (const auto& sender : senders) {
324 if (sender->id() == id) {
325 return true;
326 }
327 }
328 return false;
329}
330
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700331// Check if |senders| contains the specified sender, by id and stream id.
332bool ContainsSender(
333 const std::vector<rtc::scoped_refptr<RtpSenderInterface>>& senders,
334 const std::string& id,
335 const std::string& stream_id) {
336 for (const auto& sender : senders) {
deadbeefa601f5c2016-06-06 14:27:39 -0700337 if (sender->id() == id && sender->stream_ids()[0] == stream_id) {
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700338 return true;
339 }
340 }
341 return false;
342}
343
deadbeefab9b2d12015-10-14 11:33:11 -0700344// Create a collection of streams.
345// CreateStreamCollection(1) creates a collection that
346// correspond to kSdpStringWithStream1.
347// CreateStreamCollection(2) correspond to kSdpStringWithStream1And2.
348rtc::scoped_refptr<StreamCollection> CreateStreamCollection(
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700349 int number_of_streams,
350 int tracks_per_stream) {
deadbeefab9b2d12015-10-14 11:33:11 -0700351 rtc::scoped_refptr<StreamCollection> local_collection(
352 StreamCollection::Create());
353
354 for (int i = 0; i < number_of_streams; ++i) {
355 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream(
356 webrtc::MediaStream::Create(kStreams[i]));
357
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700358 for (int j = 0; j < tracks_per_stream; ++j) {
359 // Add a local audio track.
360 rtc::scoped_refptr<webrtc::AudioTrackInterface> audio_track(
361 webrtc::AudioTrack::Create(kAudioTracks[i * tracks_per_stream + j],
362 nullptr));
363 stream->AddTrack(audio_track);
deadbeefab9b2d12015-10-14 11:33:11 -0700364
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700365 // Add a local video track.
366 rtc::scoped_refptr<webrtc::VideoTrackInterface> video_track(
367 webrtc::VideoTrack::Create(kVideoTracks[i * tracks_per_stream + j],
368 webrtc::FakeVideoTrackSource::Create()));
369 stream->AddTrack(video_track);
370 }
deadbeefab9b2d12015-10-14 11:33:11 -0700371
372 local_collection->AddStream(stream);
373 }
374 return local_collection;
375}
376
377// Check equality of StreamCollections.
378bool CompareStreamCollections(StreamCollectionInterface* s1,
379 StreamCollectionInterface* s2) {
380 if (s1 == nullptr || s2 == nullptr || s1->count() != s2->count()) {
381 return false;
382 }
383
384 for (size_t i = 0; i != s1->count(); ++i) {
385 if (s1->at(i)->label() != s2->at(i)->label()) {
386 return false;
387 }
388 webrtc::AudioTrackVector audio_tracks1 = s1->at(i)->GetAudioTracks();
389 webrtc::AudioTrackVector audio_tracks2 = s2->at(i)->GetAudioTracks();
390 webrtc::VideoTrackVector video_tracks1 = s1->at(i)->GetVideoTracks();
391 webrtc::VideoTrackVector video_tracks2 = s2->at(i)->GetVideoTracks();
392
393 if (audio_tracks1.size() != audio_tracks2.size()) {
394 return false;
395 }
396 for (size_t j = 0; j != audio_tracks1.size(); ++j) {
397 if (audio_tracks1[j]->id() != audio_tracks2[j]->id()) {
398 return false;
399 }
400 }
401 if (video_tracks1.size() != video_tracks2.size()) {
402 return false;
403 }
404 for (size_t j = 0; j != video_tracks1.size(); ++j) {
405 if (video_tracks1[j]->id() != video_tracks2[j]->id()) {
406 return false;
407 }
408 }
409 }
410 return true;
411}
412
perkjd61bf802016-03-24 03:16:19 -0700413// Helper class to test Observer.
414class MockTrackObserver : public ObserverInterface {
415 public:
416 explicit MockTrackObserver(NotifierInterface* notifier)
417 : notifier_(notifier) {
418 notifier_->RegisterObserver(this);
419 }
420
421 ~MockTrackObserver() { Unregister(); }
422
423 void Unregister() {
424 if (notifier_) {
425 notifier_->UnregisterObserver(this);
426 notifier_ = nullptr;
427 }
428 }
429
430 MOCK_METHOD0(OnChanged, void());
431
432 private:
433 NotifierInterface* notifier_;
434};
435
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000436class MockPeerConnectionObserver : public PeerConnectionObserver {
437 public:
kjellander71a1b612016-11-07 01:18:08 -0800438 // We need these using declarations because there are two versions of each of
439 // the below methods and we only override one of them.
440 // TODO(deadbeef): Remove once there's only one version of the methods.
441 using PeerConnectionObserver::OnAddStream;
442 using PeerConnectionObserver::OnRemoveStream;
443 using PeerConnectionObserver::OnDataChannel;
444
deadbeefab9b2d12015-10-14 11:33:11 -0700445 MockPeerConnectionObserver() : remote_streams_(StreamCollection::Create()) {}
Henrik Kjellander3fe372d2016-05-12 08:10:52 +0200446 virtual ~MockPeerConnectionObserver() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000447 }
448 void SetPeerConnectionInterface(PeerConnectionInterface* pc) {
449 pc_ = pc;
450 if (pc) {
451 state_ = pc_->signaling_state();
452 }
453 }
nisseef8b61e2016-04-29 06:09:15 -0700454 void OnSignalingChange(
455 PeerConnectionInterface::SignalingState new_state) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000456 EXPECT_EQ(pc_->signaling_state(), new_state);
457 state_ = new_state;
458 }
459 // TODO(bemasc): Remove this once callers transition to OnIceGatheringChange.
460 virtual void OnStateChange(StateType state_changed) {
461 if (pc_.get() == NULL)
462 return;
463 switch (state_changed) {
464 case kSignalingState:
465 // OnSignalingChange and OnStateChange(kSignalingState) should always
466 // be called approximately simultaneously. To ease testing, we require
467 // that they always be called in that order. This check verifies
468 // that OnSignalingChange has just been called.
469 EXPECT_EQ(pc_->signaling_state(), state_);
470 break;
471 case kIceState:
472 ADD_FAILURE();
473 break;
474 default:
475 ADD_FAILURE();
476 break;
477 }
478 }
deadbeefab9b2d12015-10-14 11:33:11 -0700479
480 MediaStreamInterface* RemoteStream(const std::string& label) {
481 return remote_streams_->find(label);
482 }
483 StreamCollectionInterface* remote_streams() const { return remote_streams_; }
Taylor Brandstetter98cde262016-05-31 13:02:21 -0700484 void OnAddStream(rtc::scoped_refptr<MediaStreamInterface> stream) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000485 last_added_stream_ = stream;
deadbeefab9b2d12015-10-14 11:33:11 -0700486 remote_streams_->AddStream(stream);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000487 }
Taylor Brandstetter98cde262016-05-31 13:02:21 -0700488 void OnRemoveStream(
489 rtc::scoped_refptr<MediaStreamInterface> stream) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000490 last_removed_stream_ = stream;
deadbeefab9b2d12015-10-14 11:33:11 -0700491 remote_streams_->RemoveStream(stream);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000492 }
perkjdfb769d2016-02-09 03:09:43 -0800493 void OnRenegotiationNeeded() override { renegotiation_needed_ = true; }
Taylor Brandstetter98cde262016-05-31 13:02:21 -0700494 void OnDataChannel(
495 rtc::scoped_refptr<DataChannelInterface> data_channel) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000496 last_datachannel_ = data_channel;
497 }
498
perkjdfb769d2016-02-09 03:09:43 -0800499 void OnIceConnectionChange(
500 PeerConnectionInterface::IceConnectionState new_state) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000501 EXPECT_EQ(pc_->ice_connection_state(), new_state);
zhihuang29ff8442016-07-27 11:07:25 -0700502 callback_triggered = true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000503 }
perkjdfb769d2016-02-09 03:09:43 -0800504 void OnIceGatheringChange(
505 PeerConnectionInterface::IceGatheringState new_state) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000506 EXPECT_EQ(pc_->ice_gathering_state(), new_state);
perkjdfb769d2016-02-09 03:09:43 -0800507 ice_complete_ = new_state == PeerConnectionInterface::kIceGatheringComplete;
zhihuang29ff8442016-07-27 11:07:25 -0700508 callback_triggered = true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000509 }
perkjdfb769d2016-02-09 03:09:43 -0800510 void OnIceCandidate(const webrtc::IceCandidateInterface* candidate) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000511 EXPECT_NE(PeerConnectionInterface::kIceGatheringNew,
512 pc_->ice_gathering_state());
513
514 std::string sdp;
515 EXPECT_TRUE(candidate->ToString(&sdp));
516 EXPECT_LT(0u, sdp.size());
517 last_candidate_.reset(webrtc::CreateIceCandidate(candidate->sdp_mid(),
518 candidate->sdp_mline_index(), sdp, NULL));
519 EXPECT_TRUE(last_candidate_.get() != NULL);
zhihuang29ff8442016-07-27 11:07:25 -0700520 callback_triggered = true;
521 }
522
523 void OnIceCandidatesRemoved(
524 const std::vector<cricket::Candidate>& candidates) override {
525 callback_triggered = true;
526 }
527
528 void OnIceConnectionReceivingChange(bool receiving) override {
529 callback_triggered = true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000530 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000531
532 // Returns the label of the last added stream.
533 // Empty string if no stream have been added.
534 std::string GetLastAddedStreamLabel() {
535 if (last_added_stream_.get())
536 return last_added_stream_->label();
537 return "";
538 }
539 std::string GetLastRemovedStreamLabel() {
540 if (last_removed_stream_.get())
541 return last_removed_stream_->label();
542 return "";
543 }
544
zhihuang9763d562016-08-05 11:14:50 -0700545 rtc::scoped_refptr<PeerConnectionInterface> pc_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000546 PeerConnectionInterface::SignalingState state_;
kwibergd1fe2812016-04-27 06:47:29 -0700547 std::unique_ptr<IceCandidateInterface> last_candidate_;
zhihuang9763d562016-08-05 11:14:50 -0700548 rtc::scoped_refptr<DataChannelInterface> last_datachannel_;
deadbeefab9b2d12015-10-14 11:33:11 -0700549 rtc::scoped_refptr<StreamCollection> remote_streams_;
550 bool renegotiation_needed_ = false;
551 bool ice_complete_ = false;
zhihuang29ff8442016-07-27 11:07:25 -0700552 bool callback_triggered = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000553
554 private:
zhihuang9763d562016-08-05 11:14:50 -0700555 rtc::scoped_refptr<MediaStreamInterface> last_added_stream_;
556 rtc::scoped_refptr<MediaStreamInterface> last_removed_stream_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000557};
558
559} // namespace
deadbeefab9b2d12015-10-14 11:33:11 -0700560
zhihuang29ff8442016-07-27 11:07:25 -0700561// The PeerConnectionMediaConfig tests below verify that configuration
562// and constraints are propagated into the MediaConfig passed to
563// CreateMediaController. These settings are intended for MediaChannel
564// constructors, but that is not exercised by these unittest.
565class PeerConnectionFactoryForTest : public webrtc::PeerConnectionFactory {
566 public:
567 webrtc::MediaControllerInterface* CreateMediaController(
skvlad11a9cbf2016-10-07 11:53:05 -0700568 const cricket::MediaConfig& config,
569 webrtc::RtcEventLog* event_log) const override {
zhihuang29ff8442016-07-27 11:07:25 -0700570 create_media_controller_called_ = true;
571 create_media_controller_config_ = config;
572
573 webrtc::MediaControllerInterface* mc =
skvlad11a9cbf2016-10-07 11:53:05 -0700574 PeerConnectionFactory::CreateMediaController(config, event_log);
zhihuang29ff8442016-07-27 11:07:25 -0700575 EXPECT_TRUE(mc != nullptr);
576 return mc;
577 }
578
579 cricket::TransportController* CreateTransportController(
Honghai Zhangbfd398c2016-08-30 22:07:42 -0700580 cricket::PortAllocator* port_allocator,
581 bool redetermine_role_on_ice_restart) override {
zhihuang29ff8442016-07-27 11:07:25 -0700582 transport_controller = new cricket::TransportController(
Honghai Zhangbfd398c2016-08-30 22:07:42 -0700583 rtc::Thread::Current(), rtc::Thread::Current(), port_allocator,
584 redetermine_role_on_ice_restart);
zhihuang29ff8442016-07-27 11:07:25 -0700585 return transport_controller;
586 }
587
588 cricket::TransportController* transport_controller;
589 // Mutable, so they can be modified in the above const-declared method.
590 mutable bool create_media_controller_called_ = false;
591 mutable cricket::MediaConfig create_media_controller_config_;
592};
593
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000594class PeerConnectionInterfaceTest : public testing::Test {
595 protected:
phoglund37ebcf02016-01-08 05:04:57 -0800596 PeerConnectionInterfaceTest() {
597#ifdef WEBRTC_ANDROID
598 webrtc::InitializeAndroidObjects();
599#endif
600 }
601
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000602 virtual void SetUp() {
603 pc_factory_ = webrtc::CreatePeerConnectionFactory(
danilchape9021a32016-05-17 01:52:02 -0700604 rtc::Thread::Current(), rtc::Thread::Current(), rtc::Thread::Current(),
605 nullptr, nullptr, nullptr);
606 ASSERT_TRUE(pc_factory_);
zhihuang29ff8442016-07-27 11:07:25 -0700607 pc_factory_for_test_ =
608 new rtc::RefCountedObject<PeerConnectionFactoryForTest>();
609 pc_factory_for_test_->Initialize();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000610 }
611
612 void CreatePeerConnection() {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700613 CreatePeerConnection(PeerConnectionInterface::RTCConfiguration(), nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000614 }
615
616 void CreatePeerConnection(webrtc::MediaConstraintsInterface* constraints) {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700617 CreatePeerConnection(PeerConnectionInterface::RTCConfiguration(),
618 constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000619 }
620
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700621 void CreatePeerConnectionWithIceTransportsType(
622 PeerConnectionInterface::IceTransportsType type) {
623 PeerConnectionInterface::RTCConfiguration config;
624 config.type = type;
625 return CreatePeerConnection(config, nullptr);
626 }
627
628 void CreatePeerConnectionWithIceServer(const std::string& uri,
629 const std::string& password) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800630 PeerConnectionInterface::RTCConfiguration config;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000631 PeerConnectionInterface::IceServer server;
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700632 server.uri = uri;
633 server.password = password;
634 config.servers.push_back(server);
635 CreatePeerConnection(config, nullptr);
636 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000637
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700638 void CreatePeerConnection(PeerConnectionInterface::RTCConfiguration config,
639 webrtc::MediaConstraintsInterface* constraints) {
kwibergd1fe2812016-04-27 06:47:29 -0700640 std::unique_ptr<cricket::FakePortAllocator> port_allocator(
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800641 new cricket::FakePortAllocator(rtc::Thread::Current(), nullptr));
642 port_allocator_ = port_allocator.get();
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000643
buildbot@webrtc.org61c1b8e2014-04-09 06:06:38 +0000644 // DTLS does not work in a loopback call, so is disabled for most of the
645 // tests in this file. We only create a FakeIdentityService if the test
646 // explicitly sets the constraint.
jiayl@webrtc.org61e00b02015-03-04 22:17:38 +0000647 FakeConstraints default_constraints;
648 if (!constraints) {
649 constraints = &default_constraints;
650
651 default_constraints.AddMandatory(
652 webrtc::MediaConstraintsInterface::kEnableDtlsSrtp, false);
653 }
654
Henrik Boströmd79599d2016-06-01 13:58:50 +0200655 std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator;
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000656 bool dtls;
657 if (FindConstraint(constraints,
658 webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
659 &dtls,
Henrik Boström5e56c592015-08-11 10:33:13 +0200660 nullptr) && dtls) {
Henrik Boströmd79599d2016-06-01 13:58:50 +0200661 cert_generator.reset(new FakeRTCCertificateGenerator());
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000662 }
Henrik Boströmd79599d2016-06-01 13:58:50 +0200663 pc_ = pc_factory_->CreatePeerConnection(
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800664 config, constraints, std::move(port_allocator),
Henrik Boströmd79599d2016-06-01 13:58:50 +0200665 std::move(cert_generator), &observer_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000666 ASSERT_TRUE(pc_.get() != NULL);
667 observer_.SetPeerConnectionInterface(pc_.get());
668 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
669 }
670
deadbeef0a6c4ca2015-10-06 11:38:28 -0700671 void CreatePeerConnectionExpectFail(const std::string& uri) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800672 PeerConnectionInterface::RTCConfiguration config;
deadbeef0a6c4ca2015-10-06 11:38:28 -0700673 PeerConnectionInterface::IceServer server;
deadbeef0a6c4ca2015-10-06 11:38:28 -0700674 server.uri = uri;
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800675 config.servers.push_back(server);
deadbeef0a6c4ca2015-10-06 11:38:28 -0700676
zhihuang9763d562016-08-05 11:14:50 -0700677 rtc::scoped_refptr<PeerConnectionInterface> pc;
hbosd7973cc2016-05-27 06:08:53 -0700678 pc = pc_factory_->CreatePeerConnection(config, nullptr, nullptr, nullptr,
679 &observer_);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800680 EXPECT_EQ(nullptr, pc);
deadbeef0a6c4ca2015-10-06 11:38:28 -0700681 }
682
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000683 void CreatePeerConnectionWithDifferentConfigurations() {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700684 CreatePeerConnectionWithIceServer(kStunAddressOnly, "");
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800685 EXPECT_EQ(1u, port_allocator_->stun_servers().size());
686 EXPECT_EQ(0u, port_allocator_->turn_servers().size());
687 EXPECT_EQ("address", port_allocator_->stun_servers().begin()->hostname());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000688 EXPECT_EQ(kDefaultStunPort,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800689 port_allocator_->stun_servers().begin()->port());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000690
deadbeef0a6c4ca2015-10-06 11:38:28 -0700691 CreatePeerConnectionExpectFail(kStunInvalidPort);
692 CreatePeerConnectionExpectFail(kStunAddressPortAndMore1);
693 CreatePeerConnectionExpectFail(kStunAddressPortAndMore2);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000694
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700695 CreatePeerConnectionWithIceServer(kTurnIceServerUri, kTurnPassword);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800696 EXPECT_EQ(0u, port_allocator_->stun_servers().size());
697 EXPECT_EQ(1u, port_allocator_->turn_servers().size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000698 EXPECT_EQ(kTurnUsername,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800699 port_allocator_->turn_servers()[0].credentials.username);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000700 EXPECT_EQ(kTurnPassword,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800701 port_allocator_->turn_servers()[0].credentials.password);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000702 EXPECT_EQ(kTurnHostname,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800703 port_allocator_->turn_servers()[0].ports[0].address.hostname());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000704 }
705
706 void ReleasePeerConnection() {
707 pc_ = NULL;
708 observer_.SetPeerConnectionInterface(NULL);
709 }
710
deadbeefab9b2d12015-10-14 11:33:11 -0700711 void AddVideoStream(const std::string& label) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000712 // Create a local stream.
zhihuang9763d562016-08-05 11:14:50 -0700713 rtc::scoped_refptr<MediaStreamInterface> stream(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000714 pc_factory_->CreateLocalMediaStream(label));
zhihuang9763d562016-08-05 11:14:50 -0700715 rtc::scoped_refptr<VideoTrackSourceInterface> video_source(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000716 pc_factory_->CreateVideoSource(new cricket::FakeVideoCapturer(), NULL));
zhihuang9763d562016-08-05 11:14:50 -0700717 rtc::scoped_refptr<VideoTrackInterface> video_track(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000718 pc_factory_->CreateVideoTrack(label + "v0", video_source));
719 stream->AddTrack(video_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +0000720 EXPECT_TRUE(pc_->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000721 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
722 observer_.renegotiation_needed_ = false;
723 }
724
725 void AddVoiceStream(const std::string& label) {
726 // Create a local stream.
zhihuang9763d562016-08-05 11:14:50 -0700727 rtc::scoped_refptr<MediaStreamInterface> stream(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000728 pc_factory_->CreateLocalMediaStream(label));
zhihuang9763d562016-08-05 11:14:50 -0700729 rtc::scoped_refptr<AudioTrackInterface> audio_track(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000730 pc_factory_->CreateAudioTrack(label + "a0", NULL));
731 stream->AddTrack(audio_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +0000732 EXPECT_TRUE(pc_->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000733 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
734 observer_.renegotiation_needed_ = false;
735 }
736
737 void AddAudioVideoStream(const std::string& stream_label,
738 const std::string& audio_track_label,
739 const std::string& video_track_label) {
740 // Create a local stream.
zhihuang9763d562016-08-05 11:14:50 -0700741 rtc::scoped_refptr<MediaStreamInterface> stream(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000742 pc_factory_->CreateLocalMediaStream(stream_label));
zhihuang9763d562016-08-05 11:14:50 -0700743 rtc::scoped_refptr<AudioTrackInterface> audio_track(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000744 pc_factory_->CreateAudioTrack(
745 audio_track_label, static_cast<AudioSourceInterface*>(NULL)));
746 stream->AddTrack(audio_track.get());
zhihuang9763d562016-08-05 11:14:50 -0700747 rtc::scoped_refptr<VideoTrackInterface> video_track(
nisseaf510af2016-03-21 08:20:42 -0700748 pc_factory_->CreateVideoTrack(
749 video_track_label,
750 pc_factory_->CreateVideoSource(new cricket::FakeVideoCapturer())));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000751 stream->AddTrack(video_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +0000752 EXPECT_TRUE(pc_->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000753 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
754 observer_.renegotiation_needed_ = false;
755 }
756
kwibergd1fe2812016-04-27 06:47:29 -0700757 bool DoCreateOfferAnswer(std::unique_ptr<SessionDescriptionInterface>* desc,
deadbeefc80741f2015-10-22 13:14:45 -0700758 bool offer,
759 MediaConstraintsInterface* constraints) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000760 rtc::scoped_refptr<MockCreateSessionDescriptionObserver>
761 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000762 MockCreateSessionDescriptionObserver>());
763 if (offer) {
deadbeefc80741f2015-10-22 13:14:45 -0700764 pc_->CreateOffer(observer, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000765 } else {
deadbeefc80741f2015-10-22 13:14:45 -0700766 pc_->CreateAnswer(observer, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000767 }
768 EXPECT_EQ_WAIT(true, observer->called(), kTimeout);
kwiberg2bbff992016-03-16 11:03:04 -0700769 desc->reset(observer->release_desc());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000770 return observer->result();
771 }
772
kwibergd1fe2812016-04-27 06:47:29 -0700773 bool DoCreateOffer(std::unique_ptr<SessionDescriptionInterface>* desc,
deadbeefc80741f2015-10-22 13:14:45 -0700774 MediaConstraintsInterface* constraints) {
775 return DoCreateOfferAnswer(desc, true, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000776 }
777
kwibergd1fe2812016-04-27 06:47:29 -0700778 bool DoCreateAnswer(std::unique_ptr<SessionDescriptionInterface>* desc,
deadbeefc80741f2015-10-22 13:14:45 -0700779 MediaConstraintsInterface* constraints) {
780 return DoCreateOfferAnswer(desc, false, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000781 }
782
783 bool DoSetSessionDescription(SessionDescriptionInterface* desc, bool local) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000784 rtc::scoped_refptr<MockSetSessionDescriptionObserver>
785 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000786 MockSetSessionDescriptionObserver>());
787 if (local) {
788 pc_->SetLocalDescription(observer, desc);
789 } else {
790 pc_->SetRemoteDescription(observer, desc);
791 }
zhihuang29ff8442016-07-27 11:07:25 -0700792 if (pc_->signaling_state() != PeerConnectionInterface::kClosed) {
793 EXPECT_EQ_WAIT(true, observer->called(), kTimeout);
794 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000795 return observer->result();
796 }
797
798 bool DoSetLocalDescription(SessionDescriptionInterface* desc) {
799 return DoSetSessionDescription(desc, true);
800 }
801
802 bool DoSetRemoteDescription(SessionDescriptionInterface* desc) {
803 return DoSetSessionDescription(desc, false);
804 }
805
806 // Calls PeerConnection::GetStats and check the return value.
807 // It does not verify the values in the StatReports since a RTCP packet might
808 // be required.
809 bool DoGetStats(MediaStreamTrackInterface* track) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000810 rtc::scoped_refptr<MockStatsObserver> observer(
811 new rtc::RefCountedObject<MockStatsObserver>());
jiayl@webrtc.orgdb41b4d2014-03-03 21:30:06 +0000812 if (!pc_->GetStats(
813 observer, track, PeerConnectionInterface::kStatsOutputLevelStandard))
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000814 return false;
815 EXPECT_TRUE_WAIT(observer->called(), kTimeout);
816 return observer->called();
817 }
818
819 void InitiateCall() {
820 CreatePeerConnection();
821 // Create a local stream with audio&video tracks.
822 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
823 CreateOfferReceiveAnswer();
824 }
825
826 // Verify that RTP Header extensions has been negotiated for audio and video.
827 void VerifyRemoteRtpHeaderExtensions() {
828 const cricket::MediaContentDescription* desc =
829 cricket::GetFirstAudioContentDescription(
830 pc_->remote_description()->description());
831 ASSERT_TRUE(desc != NULL);
832 EXPECT_GT(desc->rtp_header_extensions().size(), 0u);
833
834 desc = cricket::GetFirstVideoContentDescription(
835 pc_->remote_description()->description());
836 ASSERT_TRUE(desc != NULL);
837 EXPECT_GT(desc->rtp_header_extensions().size(), 0u);
838 }
839
840 void CreateOfferAsRemoteDescription() {
kwibergd1fe2812016-04-27 06:47:29 -0700841 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -0700842 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000843 std::string sdp;
844 EXPECT_TRUE(offer->ToString(&sdp));
845 SessionDescriptionInterface* remote_offer =
846 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
847 sdp, NULL);
848 EXPECT_TRUE(DoSetRemoteDescription(remote_offer));
849 EXPECT_EQ(PeerConnectionInterface::kHaveRemoteOffer, observer_.state_);
850 }
851
deadbeefab9b2d12015-10-14 11:33:11 -0700852 void CreateAndSetRemoteOffer(const std::string& sdp) {
853 SessionDescriptionInterface* remote_offer =
854 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
855 sdp, nullptr);
856 EXPECT_TRUE(DoSetRemoteDescription(remote_offer));
857 EXPECT_EQ(PeerConnectionInterface::kHaveRemoteOffer, observer_.state_);
858 }
859
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000860 void CreateAnswerAsLocalDescription() {
kwibergd1fe2812016-04-27 06:47:29 -0700861 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -0700862 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000863
864 // TODO(perkj): Currently SetLocalDescription fails if any parameters in an
865 // audio codec change, even if the parameter has nothing to do with
866 // receiving. Not all parameters are serialized to SDP.
867 // Since CreatePrAnswerAsLocalDescription serialize/deserialize
868 // the SessionDescription, it is necessary to do that here to in order to
869 // get ReceiveOfferCreatePrAnswerAndAnswer and RenegotiateAudioOnly to pass.
870 // https://code.google.com/p/webrtc/issues/detail?id=1356
871 std::string sdp;
872 EXPECT_TRUE(answer->ToString(&sdp));
873 SessionDescriptionInterface* new_answer =
874 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
875 sdp, NULL);
876 EXPECT_TRUE(DoSetLocalDescription(new_answer));
877 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
878 }
879
880 void CreatePrAnswerAsLocalDescription() {
kwibergd1fe2812016-04-27 06:47:29 -0700881 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -0700882 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000883
884 std::string sdp;
885 EXPECT_TRUE(answer->ToString(&sdp));
886 SessionDescriptionInterface* pr_answer =
887 webrtc::CreateSessionDescription(SessionDescriptionInterface::kPrAnswer,
888 sdp, NULL);
889 EXPECT_TRUE(DoSetLocalDescription(pr_answer));
890 EXPECT_EQ(PeerConnectionInterface::kHaveLocalPrAnswer, observer_.state_);
891 }
892
893 void CreateOfferReceiveAnswer() {
894 CreateOfferAsLocalDescription();
895 std::string sdp;
896 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
897 CreateAnswerAsRemoteDescription(sdp);
898 }
899
900 void CreateOfferAsLocalDescription() {
kwibergd1fe2812016-04-27 06:47:29 -0700901 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -0700902 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000903 // TODO(perkj): Currently SetLocalDescription fails if any parameters in an
904 // audio codec change, even if the parameter has nothing to do with
905 // receiving. Not all parameters are serialized to SDP.
906 // Since CreatePrAnswerAsLocalDescription serialize/deserialize
907 // the SessionDescription, it is necessary to do that here to in order to
908 // get ReceiveOfferCreatePrAnswerAndAnswer and RenegotiateAudioOnly to pass.
909 // https://code.google.com/p/webrtc/issues/detail?id=1356
910 std::string sdp;
911 EXPECT_TRUE(offer->ToString(&sdp));
912 SessionDescriptionInterface* new_offer =
913 webrtc::CreateSessionDescription(
914 SessionDescriptionInterface::kOffer,
915 sdp, NULL);
916
917 EXPECT_TRUE(DoSetLocalDescription(new_offer));
918 EXPECT_EQ(PeerConnectionInterface::kHaveLocalOffer, observer_.state_);
mallinath@webrtc.org68cbd012014-01-22 00:16:46 +0000919 // Wait for the ice_complete message, so that SDP will have candidates.
920 EXPECT_TRUE_WAIT(observer_.ice_complete_, kTimeout);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000921 }
922
deadbeefab9b2d12015-10-14 11:33:11 -0700923 void CreateAnswerAsRemoteDescription(const std::string& sdp) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000924 webrtc::JsepSessionDescription* answer = new webrtc::JsepSessionDescription(
925 SessionDescriptionInterface::kAnswer);
deadbeefab9b2d12015-10-14 11:33:11 -0700926 EXPECT_TRUE(answer->Initialize(sdp, NULL));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000927 EXPECT_TRUE(DoSetRemoteDescription(answer));
928 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
929 }
930
deadbeefab9b2d12015-10-14 11:33:11 -0700931 void CreatePrAnswerAndAnswerAsRemoteDescription(const std::string& sdp) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000932 webrtc::JsepSessionDescription* pr_answer =
933 new webrtc::JsepSessionDescription(
934 SessionDescriptionInterface::kPrAnswer);
deadbeefab9b2d12015-10-14 11:33:11 -0700935 EXPECT_TRUE(pr_answer->Initialize(sdp, NULL));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000936 EXPECT_TRUE(DoSetRemoteDescription(pr_answer));
937 EXPECT_EQ(PeerConnectionInterface::kHaveRemotePrAnswer, observer_.state_);
938 webrtc::JsepSessionDescription* answer =
939 new webrtc::JsepSessionDescription(
940 SessionDescriptionInterface::kAnswer);
deadbeefab9b2d12015-10-14 11:33:11 -0700941 EXPECT_TRUE(answer->Initialize(sdp, NULL));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000942 EXPECT_TRUE(DoSetRemoteDescription(answer));
943 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
944 }
945
946 // Help function used for waiting until a the last signaled remote stream has
947 // the same label as |stream_label|. In a few of the tests in this file we
948 // answer with the same session description as we offer and thus we can
949 // check if OnAddStream have been called with the same stream as we offer to
950 // send.
951 void WaitAndVerifyOnAddStream(const std::string& stream_label) {
952 EXPECT_EQ_WAIT(stream_label, observer_.GetLastAddedStreamLabel(), kTimeout);
953 }
954
955 // Creates an offer and applies it as a local session description.
956 // Creates an answer with the same SDP an the offer but removes all lines
957 // that start with a:ssrc"
958 void CreateOfferReceiveAnswerWithoutSsrc() {
959 CreateOfferAsLocalDescription();
960 std::string sdp;
961 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
962 SetSsrcToZero(&sdp);
963 CreateAnswerAsRemoteDescription(sdp);
964 }
965
deadbeefab9b2d12015-10-14 11:33:11 -0700966 // This function creates a MediaStream with label kStreams[0] and
967 // |number_of_audio_tracks| and |number_of_video_tracks| tracks and the
968 // corresponding SessionDescriptionInterface. The SessionDescriptionInterface
kwiberg2bbff992016-03-16 11:03:04 -0700969 // is returned and the MediaStream is stored in
deadbeefab9b2d12015-10-14 11:33:11 -0700970 // |reference_collection_|
kwibergd1fe2812016-04-27 06:47:29 -0700971 std::unique_ptr<SessionDescriptionInterface>
kwiberg2bbff992016-03-16 11:03:04 -0700972 CreateSessionDescriptionAndReference(size_t number_of_audio_tracks,
973 size_t number_of_video_tracks) {
974 EXPECT_LE(number_of_audio_tracks, 2u);
975 EXPECT_LE(number_of_video_tracks, 2u);
deadbeefab9b2d12015-10-14 11:33:11 -0700976
977 reference_collection_ = StreamCollection::Create();
978 std::string sdp_ms1 = std::string(kSdpStringInit);
979
980 std::string mediastream_label = kStreams[0];
981
982 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream(
983 webrtc::MediaStream::Create(mediastream_label));
984 reference_collection_->AddStream(stream);
985
986 if (number_of_audio_tracks > 0) {
987 sdp_ms1 += std::string(kSdpStringAudio);
988 sdp_ms1 += std::string(kSdpStringMs1Audio0);
989 AddAudioTrack(kAudioTracks[0], stream);
990 }
991 if (number_of_audio_tracks > 1) {
992 sdp_ms1 += kSdpStringMs1Audio1;
993 AddAudioTrack(kAudioTracks[1], stream);
994 }
995
996 if (number_of_video_tracks > 0) {
997 sdp_ms1 += std::string(kSdpStringVideo);
998 sdp_ms1 += std::string(kSdpStringMs1Video0);
999 AddVideoTrack(kVideoTracks[0], stream);
1000 }
1001 if (number_of_video_tracks > 1) {
1002 sdp_ms1 += kSdpStringMs1Video1;
1003 AddVideoTrack(kVideoTracks[1], stream);
1004 }
1005
kwibergd1fe2812016-04-27 06:47:29 -07001006 return std::unique_ptr<SessionDescriptionInterface>(
kwiberg2bbff992016-03-16 11:03:04 -07001007 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
1008 sdp_ms1, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07001009 }
1010
1011 void AddAudioTrack(const std::string& track_id,
1012 MediaStreamInterface* stream) {
1013 rtc::scoped_refptr<webrtc::AudioTrackInterface> audio_track(
1014 webrtc::AudioTrack::Create(track_id, nullptr));
1015 ASSERT_TRUE(stream->AddTrack(audio_track));
1016 }
1017
1018 void AddVideoTrack(const std::string& track_id,
1019 MediaStreamInterface* stream) {
1020 rtc::scoped_refptr<webrtc::VideoTrackInterface> video_track(
nisseaf510af2016-03-21 08:20:42 -07001021 webrtc::VideoTrack::Create(track_id,
1022 webrtc::FakeVideoTrackSource::Create()));
deadbeefab9b2d12015-10-14 11:33:11 -07001023 ASSERT_TRUE(stream->AddTrack(video_track));
1024 }
1025
kwibergfd8be342016-05-14 19:44:11 -07001026 std::unique_ptr<SessionDescriptionInterface> CreateOfferWithOneAudioStream() {
zhihuang8f65cdf2016-05-06 18:40:30 -07001027 CreatePeerConnection();
1028 AddVoiceStream(kStreamLabel1);
kwibergfd8be342016-05-14 19:44:11 -07001029 std::unique_ptr<SessionDescriptionInterface> offer;
zhihuang8f65cdf2016-05-06 18:40:30 -07001030 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
1031 return offer;
1032 }
1033
kwibergfd8be342016-05-14 19:44:11 -07001034 std::unique_ptr<SessionDescriptionInterface>
zhihuang8f65cdf2016-05-06 18:40:30 -07001035 CreateAnswerWithOneAudioStream() {
kwibergfd8be342016-05-14 19:44:11 -07001036 std::unique_ptr<SessionDescriptionInterface> offer =
zhihuang8f65cdf2016-05-06 18:40:30 -07001037 CreateOfferWithOneAudioStream();
1038 EXPECT_TRUE(DoSetRemoteDescription(offer.release()));
kwibergfd8be342016-05-14 19:44:11 -07001039 std::unique_ptr<SessionDescriptionInterface> answer;
zhihuang8f65cdf2016-05-06 18:40:30 -07001040 EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
1041 return answer;
1042 }
1043
1044 const std::string& GetFirstAudioStreamCname(
1045 const SessionDescriptionInterface* desc) {
1046 const cricket::ContentInfo* audio_content =
1047 cricket::GetFirstAudioContent(desc->description());
1048 const cricket::AudioContentDescription* audio_desc =
1049 static_cast<const cricket::AudioContentDescription*>(
1050 audio_content->description);
1051 return audio_desc->streams()[0].cname;
1052 }
1053
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08001054 cricket::FakePortAllocator* port_allocator_ = nullptr;
zhihuang9763d562016-08-05 11:14:50 -07001055 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface> pc_factory_;
1056 rtc::scoped_refptr<PeerConnectionFactoryForTest> pc_factory_for_test_;
1057 rtc::scoped_refptr<PeerConnectionInterface> pc_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001058 MockPeerConnectionObserver observer_;
deadbeefab9b2d12015-10-14 11:33:11 -07001059 rtc::scoped_refptr<StreamCollection> reference_collection_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001060};
1061
zhihuang29ff8442016-07-27 11:07:25 -07001062// Test that no callbacks on the PeerConnectionObserver are called after the
1063// PeerConnection is closed.
1064TEST_F(PeerConnectionInterfaceTest, CloseAndTestCallbackFunctions) {
zhihuang9763d562016-08-05 11:14:50 -07001065 rtc::scoped_refptr<PeerConnectionInterface> pc(
zhihuang29ff8442016-07-27 11:07:25 -07001066 pc_factory_for_test_->CreatePeerConnection(
1067 PeerConnectionInterface::RTCConfiguration(), nullptr, nullptr,
1068 nullptr, &observer_));
1069 observer_.SetPeerConnectionInterface(pc.get());
1070 pc->Close();
1071
1072 // No callbacks is expected to be called.
1073 observer_.callback_triggered = false;
1074 std::vector<cricket::Candidate> candidates;
1075 pc_factory_for_test_->transport_controller->SignalGatheringState(
1076 cricket::IceGatheringState{});
1077 pc_factory_for_test_->transport_controller->SignalCandidatesGathered(
1078 "", candidates);
1079 pc_factory_for_test_->transport_controller->SignalConnectionState(
1080 cricket::IceConnectionState{});
1081 pc_factory_for_test_->transport_controller->SignalCandidatesRemoved(
1082 candidates);
1083 pc_factory_for_test_->transport_controller->SignalReceiving(false);
1084 EXPECT_FALSE(observer_.callback_triggered);
1085}
1086
zhihuang8f65cdf2016-05-06 18:40:30 -07001087// Generate different CNAMEs when PeerConnections are created.
1088// The CNAMEs are expected to be generated randomly. It is possible
1089// that the test fails, though the possibility is very low.
1090TEST_F(PeerConnectionInterfaceTest, CnameGenerationInOffer) {
kwibergfd8be342016-05-14 19:44:11 -07001091 std::unique_ptr<SessionDescriptionInterface> offer1 =
zhihuang8f65cdf2016-05-06 18:40:30 -07001092 CreateOfferWithOneAudioStream();
kwibergfd8be342016-05-14 19:44:11 -07001093 std::unique_ptr<SessionDescriptionInterface> offer2 =
zhihuang8f65cdf2016-05-06 18:40:30 -07001094 CreateOfferWithOneAudioStream();
1095 EXPECT_NE(GetFirstAudioStreamCname(offer1.get()),
1096 GetFirstAudioStreamCname(offer2.get()));
1097}
1098
1099TEST_F(PeerConnectionInterfaceTest, CnameGenerationInAnswer) {
kwibergfd8be342016-05-14 19:44:11 -07001100 std::unique_ptr<SessionDescriptionInterface> answer1 =
zhihuang8f65cdf2016-05-06 18:40:30 -07001101 CreateAnswerWithOneAudioStream();
kwibergfd8be342016-05-14 19:44:11 -07001102 std::unique_ptr<SessionDescriptionInterface> answer2 =
zhihuang8f65cdf2016-05-06 18:40:30 -07001103 CreateAnswerWithOneAudioStream();
1104 EXPECT_NE(GetFirstAudioStreamCname(answer1.get()),
1105 GetFirstAudioStreamCname(answer2.get()));
1106}
1107
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001108TEST_F(PeerConnectionInterfaceTest,
1109 CreatePeerConnectionWithDifferentConfigurations) {
1110 CreatePeerConnectionWithDifferentConfigurations();
1111}
1112
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001113TEST_F(PeerConnectionInterfaceTest,
1114 CreatePeerConnectionWithDifferentIceTransportsTypes) {
1115 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kNone);
1116 EXPECT_EQ(cricket::CF_NONE, port_allocator_->candidate_filter());
1117 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kRelay);
1118 EXPECT_EQ(cricket::CF_RELAY, port_allocator_->candidate_filter());
1119 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kNoHost);
1120 EXPECT_EQ(cricket::CF_ALL & ~cricket::CF_HOST,
1121 port_allocator_->candidate_filter());
1122 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kAll);
1123 EXPECT_EQ(cricket::CF_ALL, port_allocator_->candidate_filter());
1124}
1125
1126// Test that when a PeerConnection is created with a nonzero candidate pool
1127// size, the pooled PortAllocatorSession is created with all the attributes
1128// in the RTCConfiguration.
1129TEST_F(PeerConnectionInterfaceTest, CreatePeerConnectionWithPooledCandidates) {
1130 PeerConnectionInterface::RTCConfiguration config;
1131 PeerConnectionInterface::IceServer server;
1132 server.uri = kStunAddressOnly;
1133 config.servers.push_back(server);
1134 config.type = PeerConnectionInterface::kRelay;
1135 config.disable_ipv6 = true;
1136 config.tcp_candidate_policy =
1137 PeerConnectionInterface::kTcpCandidatePolicyDisabled;
honghaiz60347052016-05-31 18:29:12 -07001138 config.candidate_network_policy =
1139 PeerConnectionInterface::kCandidateNetworkPolicyLowCost;
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001140 config.ice_candidate_pool_size = 1;
1141 CreatePeerConnection(config, nullptr);
1142
1143 const cricket::FakePortAllocatorSession* session =
1144 static_cast<const cricket::FakePortAllocatorSession*>(
1145 port_allocator_->GetPooledSession());
1146 ASSERT_NE(nullptr, session);
1147 EXPECT_EQ(1UL, session->stun_servers().size());
1148 EXPECT_EQ(0U, session->flags() & cricket::PORTALLOCATOR_ENABLE_IPV6);
1149 EXPECT_LT(0U, session->flags() & cricket::PORTALLOCATOR_DISABLE_TCP);
honghaiz60347052016-05-31 18:29:12 -07001150 EXPECT_LT(0U,
1151 session->flags() & cricket::PORTALLOCATOR_DISABLE_COSTLY_NETWORKS);
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001152}
1153
Taylor Brandstetterf8e65772016-06-27 17:20:15 -07001154// Test that the PeerConnection initializes the port allocator passed into it,
1155// and on the correct thread.
1156TEST_F(PeerConnectionInterfaceTest,
1157 CreatePeerConnectionInitializesPortAllocator) {
1158 rtc::Thread network_thread;
1159 network_thread.Start();
1160 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface> pc_factory(
1161 webrtc::CreatePeerConnectionFactory(
1162 &network_thread, rtc::Thread::Current(), rtc::Thread::Current(),
1163 nullptr, nullptr, nullptr));
1164 std::unique_ptr<cricket::FakePortAllocator> port_allocator(
1165 new cricket::FakePortAllocator(&network_thread, nullptr));
1166 cricket::FakePortAllocator* raw_port_allocator = port_allocator.get();
1167 PeerConnectionInterface::RTCConfiguration config;
1168 rtc::scoped_refptr<PeerConnectionInterface> pc(
1169 pc_factory->CreatePeerConnection(
1170 config, nullptr, std::move(port_allocator), nullptr, &observer_));
1171 // FakePortAllocator RTC_CHECKs that it's initialized on the right thread,
1172 // so all we have to do here is check that it's initialized.
1173 EXPECT_TRUE(raw_port_allocator->initialized());
1174}
1175
deadbeef46c73892016-11-16 19:42:04 -08001176// Check that GetConfiguration returns the configuration the PeerConnection was
1177// constructed with, before SetConfiguration is called.
1178TEST_F(PeerConnectionInterfaceTest, GetConfigurationAfterCreatePeerConnection) {
1179 PeerConnectionInterface::RTCConfiguration config;
1180 config.type = PeerConnectionInterface::kRelay;
1181 CreatePeerConnection(config, nullptr);
1182
1183 PeerConnectionInterface::RTCConfiguration returned_config =
1184 pc_->GetConfiguration();
1185 EXPECT_EQ(PeerConnectionInterface::kRelay, returned_config.type);
1186}
1187
1188// Check that GetConfiguration returns the last configuration passed into
1189// SetConfiguration.
1190TEST_F(PeerConnectionInterfaceTest, GetConfigurationAfterSetConfiguration) {
1191 CreatePeerConnection();
1192
1193 PeerConnectionInterface::RTCConfiguration config;
1194 config.type = PeerConnectionInterface::kRelay;
1195 EXPECT_TRUE(pc_->SetConfiguration(config));
1196
1197 PeerConnectionInterface::RTCConfiguration returned_config =
1198 pc_->GetConfiguration();
1199 EXPECT_EQ(PeerConnectionInterface::kRelay, returned_config.type);
1200}
1201
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001202TEST_F(PeerConnectionInterfaceTest, AddStreams) {
1203 CreatePeerConnection();
deadbeefab9b2d12015-10-14 11:33:11 -07001204 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001205 AddVoiceStream(kStreamLabel2);
1206 ASSERT_EQ(2u, pc_->local_streams()->count());
1207
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001208 // Test we can add multiple local streams to one peerconnection.
zhihuang9763d562016-08-05 11:14:50 -07001209 rtc::scoped_refptr<MediaStreamInterface> stream(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001210 pc_factory_->CreateLocalMediaStream(kStreamLabel3));
zhihuang9763d562016-08-05 11:14:50 -07001211 rtc::scoped_refptr<AudioTrackInterface> audio_track(
1212 pc_factory_->CreateAudioTrack(kStreamLabel3,
1213 static_cast<AudioSourceInterface*>(NULL)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001214 stream->AddTrack(audio_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +00001215 EXPECT_TRUE(pc_->AddStream(stream));
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001216 EXPECT_EQ(3u, pc_->local_streams()->count());
1217
1218 // Remove the third stream.
1219 pc_->RemoveStream(pc_->local_streams()->at(2));
1220 EXPECT_EQ(2u, pc_->local_streams()->count());
1221
1222 // Remove the second stream.
1223 pc_->RemoveStream(pc_->local_streams()->at(1));
1224 EXPECT_EQ(1u, pc_->local_streams()->count());
1225
1226 // Remove the first stream.
1227 pc_->RemoveStream(pc_->local_streams()->at(0));
1228 EXPECT_EQ(0u, pc_->local_streams()->count());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001229}
1230
deadbeefab9b2d12015-10-14 11:33:11 -07001231// Test that the created offer includes streams we added.
1232TEST_F(PeerConnectionInterfaceTest, AddedStreamsPresentInOffer) {
1233 CreatePeerConnection();
1234 AddAudioVideoStream(kStreamLabel1, "audio_track", "video_track");
kwibergd1fe2812016-04-27 06:47:29 -07001235 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001236 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07001237
1238 const cricket::ContentInfo* audio_content =
1239 cricket::GetFirstAudioContent(offer->description());
1240 const cricket::AudioContentDescription* audio_desc =
1241 static_cast<const cricket::AudioContentDescription*>(
1242 audio_content->description);
1243 EXPECT_TRUE(
1244 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
1245
1246 const cricket::ContentInfo* video_content =
1247 cricket::GetFirstVideoContent(offer->description());
1248 const cricket::VideoContentDescription* video_desc =
1249 static_cast<const cricket::VideoContentDescription*>(
1250 video_content->description);
1251 EXPECT_TRUE(
1252 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1253
1254 // Add another stream and ensure the offer includes both the old and new
1255 // streams.
1256 AddAudioVideoStream(kStreamLabel2, "audio_track2", "video_track2");
kwiberg2bbff992016-03-16 11:03:04 -07001257 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07001258
1259 audio_content = cricket::GetFirstAudioContent(offer->description());
1260 audio_desc = static_cast<const cricket::AudioContentDescription*>(
1261 audio_content->description);
1262 EXPECT_TRUE(
1263 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
1264 EXPECT_TRUE(
1265 ContainsTrack(audio_desc->streams(), kStreamLabel2, "audio_track2"));
1266
1267 video_content = cricket::GetFirstVideoContent(offer->description());
1268 video_desc = static_cast<const cricket::VideoContentDescription*>(
1269 video_content->description);
1270 EXPECT_TRUE(
1271 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1272 EXPECT_TRUE(
1273 ContainsTrack(video_desc->streams(), kStreamLabel2, "video_track2"));
1274}
1275
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001276TEST_F(PeerConnectionInterfaceTest, RemoveStream) {
1277 CreatePeerConnection();
deadbeefab9b2d12015-10-14 11:33:11 -07001278 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001279 ASSERT_EQ(1u, pc_->local_streams()->count());
1280 pc_->RemoveStream(pc_->local_streams()->at(0));
1281 EXPECT_EQ(0u, pc_->local_streams()->count());
1282}
1283
deadbeefe1f9d832016-01-14 15:35:42 -08001284// Test for AddTrack and RemoveTrack methods.
1285// Tests that the created offer includes tracks we added,
1286// and that the RtpSenders are created correctly.
1287// Also tests that RemoveTrack removes the tracks from subsequent offers.
1288TEST_F(PeerConnectionInterfaceTest, AddTrackRemoveTrack) {
1289 CreatePeerConnection();
1290 // Create a dummy stream, so tracks share a stream label.
zhihuang9763d562016-08-05 11:14:50 -07001291 rtc::scoped_refptr<MediaStreamInterface> stream(
deadbeefe1f9d832016-01-14 15:35:42 -08001292 pc_factory_->CreateLocalMediaStream(kStreamLabel1));
1293 std::vector<MediaStreamInterface*> stream_list;
1294 stream_list.push_back(stream.get());
zhihuang9763d562016-08-05 11:14:50 -07001295 rtc::scoped_refptr<AudioTrackInterface> audio_track(
deadbeefe1f9d832016-01-14 15:35:42 -08001296 pc_factory_->CreateAudioTrack("audio_track", nullptr));
zhihuang9763d562016-08-05 11:14:50 -07001297 rtc::scoped_refptr<VideoTrackInterface> video_track(
1298 pc_factory_->CreateVideoTrack(
1299 "video_track",
1300 pc_factory_->CreateVideoSource(new cricket::FakeVideoCapturer())));
deadbeefe1f9d832016-01-14 15:35:42 -08001301 auto audio_sender = pc_->AddTrack(audio_track, stream_list);
1302 auto video_sender = pc_->AddTrack(video_track, stream_list);
deadbeefa601f5c2016-06-06 14:27:39 -07001303 EXPECT_EQ(1UL, audio_sender->stream_ids().size());
1304 EXPECT_EQ(kStreamLabel1, audio_sender->stream_ids()[0]);
deadbeefe1f9d832016-01-14 15:35:42 -08001305 EXPECT_EQ("audio_track", audio_sender->id());
1306 EXPECT_EQ(audio_track, audio_sender->track());
deadbeefa601f5c2016-06-06 14:27:39 -07001307 EXPECT_EQ(1UL, video_sender->stream_ids().size());
1308 EXPECT_EQ(kStreamLabel1, video_sender->stream_ids()[0]);
deadbeefe1f9d832016-01-14 15:35:42 -08001309 EXPECT_EQ("video_track", video_sender->id());
1310 EXPECT_EQ(video_track, video_sender->track());
1311
1312 // Now create an offer and check for the senders.
kwibergd1fe2812016-04-27 06:47:29 -07001313 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001314 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefe1f9d832016-01-14 15:35:42 -08001315
1316 const cricket::ContentInfo* audio_content =
1317 cricket::GetFirstAudioContent(offer->description());
1318 const cricket::AudioContentDescription* audio_desc =
1319 static_cast<const cricket::AudioContentDescription*>(
1320 audio_content->description);
1321 EXPECT_TRUE(
1322 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
1323
1324 const cricket::ContentInfo* video_content =
1325 cricket::GetFirstVideoContent(offer->description());
1326 const cricket::VideoContentDescription* video_desc =
1327 static_cast<const cricket::VideoContentDescription*>(
1328 video_content->description);
1329 EXPECT_TRUE(
1330 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1331
1332 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
1333
1334 // Now try removing the tracks.
1335 EXPECT_TRUE(pc_->RemoveTrack(audio_sender));
1336 EXPECT_TRUE(pc_->RemoveTrack(video_sender));
1337
1338 // Create a new offer and ensure it doesn't contain the removed senders.
kwiberg2bbff992016-03-16 11:03:04 -07001339 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefe1f9d832016-01-14 15:35:42 -08001340
1341 audio_content = cricket::GetFirstAudioContent(offer->description());
1342 audio_desc = static_cast<const cricket::AudioContentDescription*>(
1343 audio_content->description);
1344 EXPECT_FALSE(
1345 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
1346
1347 video_content = cricket::GetFirstVideoContent(offer->description());
1348 video_desc = static_cast<const cricket::VideoContentDescription*>(
1349 video_content->description);
1350 EXPECT_FALSE(
1351 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1352
1353 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
1354
1355 // Calling RemoveTrack on a sender no longer attached to a PeerConnection
1356 // should return false.
1357 EXPECT_FALSE(pc_->RemoveTrack(audio_sender));
1358 EXPECT_FALSE(pc_->RemoveTrack(video_sender));
1359}
1360
1361// Test creating senders without a stream specified,
1362// expecting a random stream ID to be generated.
1363TEST_F(PeerConnectionInterfaceTest, AddTrackWithoutStream) {
1364 CreatePeerConnection();
1365 // Create a dummy stream, so tracks share a stream label.
zhihuang9763d562016-08-05 11:14:50 -07001366 rtc::scoped_refptr<AudioTrackInterface> audio_track(
deadbeefe1f9d832016-01-14 15:35:42 -08001367 pc_factory_->CreateAudioTrack("audio_track", nullptr));
zhihuang9763d562016-08-05 11:14:50 -07001368 rtc::scoped_refptr<VideoTrackInterface> video_track(
1369 pc_factory_->CreateVideoTrack(
1370 "video_track",
1371 pc_factory_->CreateVideoSource(new cricket::FakeVideoCapturer())));
deadbeefe1f9d832016-01-14 15:35:42 -08001372 auto audio_sender =
1373 pc_->AddTrack(audio_track, std::vector<MediaStreamInterface*>());
1374 auto video_sender =
1375 pc_->AddTrack(video_track, std::vector<MediaStreamInterface*>());
1376 EXPECT_EQ("audio_track", audio_sender->id());
1377 EXPECT_EQ(audio_track, audio_sender->track());
1378 EXPECT_EQ("video_track", video_sender->id());
1379 EXPECT_EQ(video_track, video_sender->track());
1380 // If the ID is truly a random GUID, it should be infinitely unlikely they
1381 // will be the same.
deadbeefa601f5c2016-06-06 14:27:39 -07001382 EXPECT_NE(video_sender->stream_ids(), audio_sender->stream_ids());
deadbeefe1f9d832016-01-14 15:35:42 -08001383}
1384
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001385TEST_F(PeerConnectionInterfaceTest, CreateOfferReceiveAnswer) {
1386 InitiateCall();
1387 WaitAndVerifyOnAddStream(kStreamLabel1);
1388 VerifyRemoteRtpHeaderExtensions();
1389}
1390
1391TEST_F(PeerConnectionInterfaceTest, CreateOfferReceivePrAnswerAndAnswer) {
1392 CreatePeerConnection();
deadbeefab9b2d12015-10-14 11:33:11 -07001393 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001394 CreateOfferAsLocalDescription();
1395 std::string offer;
1396 EXPECT_TRUE(pc_->local_description()->ToString(&offer));
1397 CreatePrAnswerAndAnswerAsRemoteDescription(offer);
1398 WaitAndVerifyOnAddStream(kStreamLabel1);
1399}
1400
1401TEST_F(PeerConnectionInterfaceTest, ReceiveOfferCreateAnswer) {
1402 CreatePeerConnection();
deadbeefab9b2d12015-10-14 11:33:11 -07001403 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001404
1405 CreateOfferAsRemoteDescription();
1406 CreateAnswerAsLocalDescription();
1407
1408 WaitAndVerifyOnAddStream(kStreamLabel1);
1409}
1410
1411TEST_F(PeerConnectionInterfaceTest, ReceiveOfferCreatePrAnswerAndAnswer) {
1412 CreatePeerConnection();
deadbeefab9b2d12015-10-14 11:33:11 -07001413 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001414
1415 CreateOfferAsRemoteDescription();
1416 CreatePrAnswerAsLocalDescription();
1417 CreateAnswerAsLocalDescription();
1418
1419 WaitAndVerifyOnAddStream(kStreamLabel1);
1420}
1421
1422TEST_F(PeerConnectionInterfaceTest, Renegotiate) {
1423 InitiateCall();
1424 ASSERT_EQ(1u, pc_->remote_streams()->count());
1425 pc_->RemoveStream(pc_->local_streams()->at(0));
1426 CreateOfferReceiveAnswer();
1427 EXPECT_EQ(0u, pc_->remote_streams()->count());
deadbeefab9b2d12015-10-14 11:33:11 -07001428 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001429 CreateOfferReceiveAnswer();
1430}
1431
1432// Tests that after negotiating an audio only call, the respondent can perform a
1433// renegotiation that removes the audio stream.
1434TEST_F(PeerConnectionInterfaceTest, RenegotiateAudioOnly) {
1435 CreatePeerConnection();
1436 AddVoiceStream(kStreamLabel1);
1437 CreateOfferAsRemoteDescription();
1438 CreateAnswerAsLocalDescription();
1439
1440 ASSERT_EQ(1u, pc_->remote_streams()->count());
1441 pc_->RemoveStream(pc_->local_streams()->at(0));
1442 CreateOfferReceiveAnswer();
1443 EXPECT_EQ(0u, pc_->remote_streams()->count());
1444}
1445
1446// Test that candidates are generated and that we can parse our own candidates.
1447TEST_F(PeerConnectionInterfaceTest, IceCandidates) {
1448 CreatePeerConnection();
1449
1450 EXPECT_FALSE(pc_->AddIceCandidate(observer_.last_candidate_.get()));
1451 // SetRemoteDescription takes ownership of offer.
kwibergd1fe2812016-04-27 06:47:29 -07001452 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefab9b2d12015-10-14 11:33:11 -07001453 AddVideoStream(kStreamLabel1);
deadbeefc80741f2015-10-22 13:14:45 -07001454 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
kwiberg2bbff992016-03-16 11:03:04 -07001455 EXPECT_TRUE(DoSetRemoteDescription(offer.release()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001456
1457 // SetLocalDescription takes ownership of answer.
kwibergd1fe2812016-04-27 06:47:29 -07001458 std::unique_ptr<SessionDescriptionInterface> answer;
deadbeefc80741f2015-10-22 13:14:45 -07001459 EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
kwiberg2bbff992016-03-16 11:03:04 -07001460 EXPECT_TRUE(DoSetLocalDescription(answer.release()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001461
1462 EXPECT_TRUE_WAIT(observer_.last_candidate_.get() != NULL, kTimeout);
1463 EXPECT_TRUE_WAIT(observer_.ice_complete_, kTimeout);
1464
1465 EXPECT_TRUE(pc_->AddIceCandidate(observer_.last_candidate_.get()));
1466}
1467
deadbeefab9b2d12015-10-14 11:33:11 -07001468// Test that CreateOffer and CreateAnswer will fail if the track labels are
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001469// not unique.
1470TEST_F(PeerConnectionInterfaceTest, CreateOfferAnswerWithInvalidStream) {
1471 CreatePeerConnection();
1472 // Create a regular offer for the CreateAnswer test later.
kwibergd1fe2812016-04-27 06:47:29 -07001473 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefc80741f2015-10-22 13:14:45 -07001474 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
kwiberg2bbff992016-03-16 11:03:04 -07001475 EXPECT_TRUE(offer);
1476 offer.reset();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001477
1478 // Create a local stream with audio&video tracks having same label.
1479 AddAudioVideoStream(kStreamLabel1, "track_label", "track_label");
1480
1481 // Test CreateOffer
deadbeefc80741f2015-10-22 13:14:45 -07001482 EXPECT_FALSE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001483
1484 // Test CreateAnswer
kwibergd1fe2812016-04-27 06:47:29 -07001485 std::unique_ptr<SessionDescriptionInterface> answer;
deadbeefc80741f2015-10-22 13:14:45 -07001486 EXPECT_FALSE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001487}
1488
1489// Test that we will get different SSRCs for each tracks in the offer and answer
1490// we created.
1491TEST_F(PeerConnectionInterfaceTest, SsrcInOfferAnswer) {
1492 CreatePeerConnection();
1493 // Create a local stream with audio&video tracks having different labels.
1494 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
1495
1496 // Test CreateOffer
kwibergd1fe2812016-04-27 06:47:29 -07001497 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001498 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001499 int audio_ssrc = 0;
1500 int video_ssrc = 0;
1501 EXPECT_TRUE(GetFirstSsrc(GetFirstAudioContent(offer->description()),
1502 &audio_ssrc));
1503 EXPECT_TRUE(GetFirstSsrc(GetFirstVideoContent(offer->description()),
1504 &video_ssrc));
1505 EXPECT_NE(audio_ssrc, video_ssrc);
1506
1507 // Test CreateAnswer
1508 EXPECT_TRUE(DoSetRemoteDescription(offer.release()));
kwibergd1fe2812016-04-27 06:47:29 -07001509 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -07001510 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001511 audio_ssrc = 0;
1512 video_ssrc = 0;
1513 EXPECT_TRUE(GetFirstSsrc(GetFirstAudioContent(answer->description()),
1514 &audio_ssrc));
1515 EXPECT_TRUE(GetFirstSsrc(GetFirstVideoContent(answer->description()),
1516 &video_ssrc));
1517 EXPECT_NE(audio_ssrc, video_ssrc);
1518}
1519
deadbeefeb459812015-12-15 19:24:43 -08001520// Test that it's possible to call AddTrack on a MediaStream after adding
1521// the stream to a PeerConnection.
1522// TODO(deadbeef): Remove this test once this behavior is no longer supported.
1523TEST_F(PeerConnectionInterfaceTest, AddTrackAfterAddStream) {
1524 CreatePeerConnection();
1525 // Create audio stream and add to PeerConnection.
1526 AddVoiceStream(kStreamLabel1);
1527 MediaStreamInterface* stream = pc_->local_streams()->at(0);
1528
1529 // Add video track to the audio-only stream.
zhihuang9763d562016-08-05 11:14:50 -07001530 rtc::scoped_refptr<VideoTrackInterface> video_track(
1531 pc_factory_->CreateVideoTrack(
1532 "video_label",
1533 pc_factory_->CreateVideoSource(new cricket::FakeVideoCapturer())));
deadbeefeb459812015-12-15 19:24:43 -08001534 stream->AddTrack(video_track.get());
1535
kwibergd1fe2812016-04-27 06:47:29 -07001536 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001537 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefeb459812015-12-15 19:24:43 -08001538
1539 const cricket::MediaContentDescription* video_desc =
1540 cricket::GetFirstVideoContentDescription(offer->description());
1541 EXPECT_TRUE(video_desc != nullptr);
1542}
1543
1544// Test that it's possible to call RemoveTrack on a MediaStream after adding
1545// the stream to a PeerConnection.
1546// TODO(deadbeef): Remove this test once this behavior is no longer supported.
1547TEST_F(PeerConnectionInterfaceTest, RemoveTrackAfterAddStream) {
1548 CreatePeerConnection();
1549 // Create audio/video stream and add to PeerConnection.
1550 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
1551 MediaStreamInterface* stream = pc_->local_streams()->at(0);
1552
1553 // Remove the video track.
1554 stream->RemoveTrack(stream->GetVideoTracks()[0]);
1555
kwibergd1fe2812016-04-27 06:47:29 -07001556 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001557 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefeb459812015-12-15 19:24:43 -08001558
1559 const cricket::MediaContentDescription* video_desc =
1560 cricket::GetFirstVideoContentDescription(offer->description());
1561 EXPECT_TRUE(video_desc == nullptr);
1562}
1563
deadbeefbd7d8f72015-12-18 16:58:44 -08001564// Test creating a sender with a stream ID, and ensure the ID is populated
1565// in the offer.
1566TEST_F(PeerConnectionInterfaceTest, CreateSenderWithStream) {
1567 CreatePeerConnection();
1568 pc_->CreateSender("video", kStreamLabel1);
1569
kwibergd1fe2812016-04-27 06:47:29 -07001570 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001571 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefbd7d8f72015-12-18 16:58:44 -08001572
1573 const cricket::MediaContentDescription* video_desc =
1574 cricket::GetFirstVideoContentDescription(offer->description());
1575 ASSERT_TRUE(video_desc != nullptr);
1576 ASSERT_EQ(1u, video_desc->streams().size());
1577 EXPECT_EQ(kStreamLabel1, video_desc->streams()[0].sync_label);
1578}
1579
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001580// Test that we can specify a certain track that we want statistics about.
1581TEST_F(PeerConnectionInterfaceTest, GetStatsForSpecificTrack) {
1582 InitiateCall();
1583 ASSERT_LT(0u, pc_->remote_streams()->count());
1584 ASSERT_LT(0u, pc_->remote_streams()->at(0)->GetAudioTracks().size());
zhihuang9763d562016-08-05 11:14:50 -07001585 rtc::scoped_refptr<MediaStreamTrackInterface> remote_audio =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001586 pc_->remote_streams()->at(0)->GetAudioTracks()[0];
1587 EXPECT_TRUE(DoGetStats(remote_audio));
1588
1589 // Remove the stream. Since we are sending to our selves the local
1590 // and the remote stream is the same.
1591 pc_->RemoveStream(pc_->local_streams()->at(0));
1592 // Do a re-negotiation.
1593 CreateOfferReceiveAnswer();
1594
1595 ASSERT_EQ(0u, pc_->remote_streams()->count());
1596
1597 // Test that we still can get statistics for the old track. Even if it is not
1598 // sent any longer.
1599 EXPECT_TRUE(DoGetStats(remote_audio));
1600}
1601
1602// Test that we can get stats on a video track.
1603TEST_F(PeerConnectionInterfaceTest, GetStatsForVideoTrack) {
1604 InitiateCall();
1605 ASSERT_LT(0u, pc_->remote_streams()->count());
1606 ASSERT_LT(0u, pc_->remote_streams()->at(0)->GetVideoTracks().size());
zhihuang9763d562016-08-05 11:14:50 -07001607 rtc::scoped_refptr<MediaStreamTrackInterface> remote_video =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001608 pc_->remote_streams()->at(0)->GetVideoTracks()[0];
1609 EXPECT_TRUE(DoGetStats(remote_video));
1610}
1611
1612// Test that we don't get statistics for an invalid track.
zhihuange9e94c32016-11-04 11:38:15 -07001613TEST_F(PeerConnectionInterfaceTest, GetStatsForInvalidTrack) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001614 InitiateCall();
zhihuang9763d562016-08-05 11:14:50 -07001615 rtc::scoped_refptr<AudioTrackInterface> unknown_audio_track(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001616 pc_factory_->CreateAudioTrack("unknown track", NULL));
1617 EXPECT_FALSE(DoGetStats(unknown_audio_track));
1618}
1619
1620// This test setup two RTP data channels in loop back.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001621TEST_F(PeerConnectionInterfaceTest, TestDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001622 FakeConstraints constraints;
1623 constraints.SetAllowRtpDataChannels();
1624 CreatePeerConnection(&constraints);
zhihuang9763d562016-08-05 11:14:50 -07001625 rtc::scoped_refptr<DataChannelInterface> data1 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001626 pc_->CreateDataChannel("test1", NULL);
zhihuang9763d562016-08-05 11:14:50 -07001627 rtc::scoped_refptr<DataChannelInterface> data2 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001628 pc_->CreateDataChannel("test2", NULL);
1629 ASSERT_TRUE(data1 != NULL);
kwibergd1fe2812016-04-27 06:47:29 -07001630 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001631 new MockDataChannelObserver(data1));
kwibergd1fe2812016-04-27 06:47:29 -07001632 std::unique_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001633 new MockDataChannelObserver(data2));
1634
1635 EXPECT_EQ(DataChannelInterface::kConnecting, data1->state());
1636 EXPECT_EQ(DataChannelInterface::kConnecting, data2->state());
1637 std::string data_to_send1 = "testing testing";
1638 std::string data_to_send2 = "testing something else";
1639 EXPECT_FALSE(data1->Send(DataBuffer(data_to_send1)));
1640
1641 CreateOfferReceiveAnswer();
1642 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1643 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
1644
1645 EXPECT_EQ(DataChannelInterface::kOpen, data1->state());
1646 EXPECT_EQ(DataChannelInterface::kOpen, data2->state());
1647 EXPECT_TRUE(data1->Send(DataBuffer(data_to_send1)));
1648 EXPECT_TRUE(data2->Send(DataBuffer(data_to_send2)));
1649
1650 EXPECT_EQ_WAIT(data_to_send1, observer1->last_message(), kTimeout);
1651 EXPECT_EQ_WAIT(data_to_send2, observer2->last_message(), kTimeout);
1652
1653 data1->Close();
1654 EXPECT_EQ(DataChannelInterface::kClosing, data1->state());
1655 CreateOfferReceiveAnswer();
1656 EXPECT_FALSE(observer1->IsOpen());
1657 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
1658 EXPECT_TRUE(observer2->IsOpen());
1659
1660 data_to_send2 = "testing something else again";
1661 EXPECT_TRUE(data2->Send(DataBuffer(data_to_send2)));
1662
1663 EXPECT_EQ_WAIT(data_to_send2, observer2->last_message(), kTimeout);
1664}
1665
1666// This test verifies that sendnig binary data over RTP data channels should
1667// fail.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001668TEST_F(PeerConnectionInterfaceTest, TestSendBinaryOnRtpDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001669 FakeConstraints constraints;
1670 constraints.SetAllowRtpDataChannels();
1671 CreatePeerConnection(&constraints);
zhihuang9763d562016-08-05 11:14:50 -07001672 rtc::scoped_refptr<DataChannelInterface> data1 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001673 pc_->CreateDataChannel("test1", NULL);
zhihuang9763d562016-08-05 11:14:50 -07001674 rtc::scoped_refptr<DataChannelInterface> data2 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001675 pc_->CreateDataChannel("test2", NULL);
1676 ASSERT_TRUE(data1 != NULL);
kwibergd1fe2812016-04-27 06:47:29 -07001677 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001678 new MockDataChannelObserver(data1));
kwibergd1fe2812016-04-27 06:47:29 -07001679 std::unique_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001680 new MockDataChannelObserver(data2));
1681
1682 EXPECT_EQ(DataChannelInterface::kConnecting, data1->state());
1683 EXPECT_EQ(DataChannelInterface::kConnecting, data2->state());
1684
1685 CreateOfferReceiveAnswer();
1686 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1687 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
1688
1689 EXPECT_EQ(DataChannelInterface::kOpen, data1->state());
1690 EXPECT_EQ(DataChannelInterface::kOpen, data2->state());
1691
jbaucheec21bd2016-03-20 06:15:43 -07001692 rtc::CopyOnWriteBuffer buffer("test", 4);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001693 EXPECT_FALSE(data1->Send(DataBuffer(buffer, true)));
1694}
1695
1696// This test setup a RTP data channels in loop back and test that a channel is
1697// opened even if the remote end answer with a zero SSRC.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001698TEST_F(PeerConnectionInterfaceTest, TestSendOnlyDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001699 FakeConstraints constraints;
1700 constraints.SetAllowRtpDataChannels();
1701 CreatePeerConnection(&constraints);
zhihuang9763d562016-08-05 11:14:50 -07001702 rtc::scoped_refptr<DataChannelInterface> data1 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001703 pc_->CreateDataChannel("test1", NULL);
kwibergd1fe2812016-04-27 06:47:29 -07001704 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001705 new MockDataChannelObserver(data1));
1706
1707 CreateOfferReceiveAnswerWithoutSsrc();
1708
1709 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1710
1711 data1->Close();
1712 EXPECT_EQ(DataChannelInterface::kClosing, data1->state());
1713 CreateOfferReceiveAnswerWithoutSsrc();
1714 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
1715 EXPECT_FALSE(observer1->IsOpen());
1716}
1717
1718// This test that if a data channel is added in an answer a receive only channel
1719// channel is created.
1720TEST_F(PeerConnectionInterfaceTest, TestReceiveOnlyDataChannel) {
1721 FakeConstraints constraints;
1722 constraints.SetAllowRtpDataChannels();
1723 CreatePeerConnection(&constraints);
1724
1725 std::string offer_label = "offer_channel";
zhihuang9763d562016-08-05 11:14:50 -07001726 rtc::scoped_refptr<DataChannelInterface> offer_channel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001727 pc_->CreateDataChannel(offer_label, NULL);
1728
1729 CreateOfferAsLocalDescription();
1730
1731 // Replace the data channel label in the offer and apply it as an answer.
1732 std::string receive_label = "answer_channel";
1733 std::string sdp;
1734 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001735 rtc::replace_substrs(offer_label.c_str(), offer_label.length(),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001736 receive_label.c_str(), receive_label.length(),
1737 &sdp);
1738 CreateAnswerAsRemoteDescription(sdp);
1739
1740 // Verify that a new incoming data channel has been created and that
1741 // it is open but can't we written to.
1742 ASSERT_TRUE(observer_.last_datachannel_ != NULL);
1743 DataChannelInterface* received_channel = observer_.last_datachannel_;
1744 EXPECT_EQ(DataChannelInterface::kConnecting, received_channel->state());
1745 EXPECT_EQ(receive_label, received_channel->label());
1746 EXPECT_FALSE(received_channel->Send(DataBuffer("something")));
1747
1748 // Verify that the channel we initially offered has been rejected.
1749 EXPECT_EQ(DataChannelInterface::kClosed, offer_channel->state());
1750
1751 // Do another offer / answer exchange and verify that the data channel is
1752 // opened.
1753 CreateOfferReceiveAnswer();
1754 EXPECT_EQ_WAIT(DataChannelInterface::kOpen, received_channel->state(),
1755 kTimeout);
1756}
1757
1758// This test that no data channel is returned if a reliable channel is
1759// requested.
1760// TODO(perkj): Remove this test once reliable channels are implemented.
1761TEST_F(PeerConnectionInterfaceTest, CreateReliableRtpDataChannelShouldFail) {
1762 FakeConstraints constraints;
1763 constraints.SetAllowRtpDataChannels();
1764 CreatePeerConnection(&constraints);
1765
1766 std::string label = "test";
1767 webrtc::DataChannelInit config;
1768 config.reliable = true;
zhihuang9763d562016-08-05 11:14:50 -07001769 rtc::scoped_refptr<DataChannelInterface> channel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001770 pc_->CreateDataChannel(label, &config);
1771 EXPECT_TRUE(channel == NULL);
1772}
1773
deadbeefab9b2d12015-10-14 11:33:11 -07001774// Verifies that duplicated label is not allowed for RTP data channel.
1775TEST_F(PeerConnectionInterfaceTest, RtpDuplicatedLabelNotAllowed) {
1776 FakeConstraints constraints;
1777 constraints.SetAllowRtpDataChannels();
1778 CreatePeerConnection(&constraints);
1779
1780 std::string label = "test";
zhihuang9763d562016-08-05 11:14:50 -07001781 rtc::scoped_refptr<DataChannelInterface> channel =
deadbeefab9b2d12015-10-14 11:33:11 -07001782 pc_->CreateDataChannel(label, nullptr);
1783 EXPECT_NE(channel, nullptr);
1784
zhihuang9763d562016-08-05 11:14:50 -07001785 rtc::scoped_refptr<DataChannelInterface> dup_channel =
deadbeefab9b2d12015-10-14 11:33:11 -07001786 pc_->CreateDataChannel(label, nullptr);
1787 EXPECT_EQ(dup_channel, nullptr);
1788}
1789
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001790// This tests that a SCTP data channel is returned using different
1791// DataChannelInit configurations.
1792TEST_F(PeerConnectionInterfaceTest, CreateSctpDataChannel) {
1793 FakeConstraints constraints;
1794 constraints.SetAllowDtlsSctpDataChannels();
1795 CreatePeerConnection(&constraints);
1796
1797 webrtc::DataChannelInit config;
1798
zhihuang9763d562016-08-05 11:14:50 -07001799 rtc::scoped_refptr<DataChannelInterface> channel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001800 pc_->CreateDataChannel("1", &config);
1801 EXPECT_TRUE(channel != NULL);
1802 EXPECT_TRUE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001803 EXPECT_TRUE(observer_.renegotiation_needed_);
1804 observer_.renegotiation_needed_ = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001805
1806 config.ordered = false;
1807 channel = pc_->CreateDataChannel("2", &config);
1808 EXPECT_TRUE(channel != NULL);
1809 EXPECT_TRUE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001810 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001811
1812 config.ordered = true;
1813 config.maxRetransmits = 0;
1814 channel = pc_->CreateDataChannel("3", &config);
1815 EXPECT_TRUE(channel != NULL);
1816 EXPECT_FALSE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001817 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001818
1819 config.maxRetransmits = -1;
1820 config.maxRetransmitTime = 0;
1821 channel = pc_->CreateDataChannel("4", &config);
1822 EXPECT_TRUE(channel != NULL);
1823 EXPECT_FALSE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001824 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001825}
1826
1827// This tests that no data channel is returned if both maxRetransmits and
1828// maxRetransmitTime are set for SCTP data channels.
1829TEST_F(PeerConnectionInterfaceTest,
1830 CreateSctpDataChannelShouldFailForInvalidConfig) {
1831 FakeConstraints constraints;
1832 constraints.SetAllowDtlsSctpDataChannels();
1833 CreatePeerConnection(&constraints);
1834
1835 std::string label = "test";
1836 webrtc::DataChannelInit config;
1837 config.maxRetransmits = 0;
1838 config.maxRetransmitTime = 0;
1839
zhihuang9763d562016-08-05 11:14:50 -07001840 rtc::scoped_refptr<DataChannelInterface> channel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001841 pc_->CreateDataChannel(label, &config);
1842 EXPECT_TRUE(channel == NULL);
1843}
1844
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001845// The test verifies that creating a SCTP data channel with an id already in use
1846// or out of range should fail.
1847TEST_F(PeerConnectionInterfaceTest,
1848 CreateSctpDataChannelWithInvalidIdShouldFail) {
1849 FakeConstraints constraints;
1850 constraints.SetAllowDtlsSctpDataChannels();
1851 CreatePeerConnection(&constraints);
1852
1853 webrtc::DataChannelInit config;
zhihuang9763d562016-08-05 11:14:50 -07001854 rtc::scoped_refptr<DataChannelInterface> channel;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001855
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001856 config.id = 1;
1857 channel = pc_->CreateDataChannel("1", &config);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001858 EXPECT_TRUE(channel != NULL);
1859 EXPECT_EQ(1, channel->id());
1860
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001861 channel = pc_->CreateDataChannel("x", &config);
1862 EXPECT_TRUE(channel == NULL);
1863
1864 config.id = cricket::kMaxSctpSid;
1865 channel = pc_->CreateDataChannel("max", &config);
1866 EXPECT_TRUE(channel != NULL);
1867 EXPECT_EQ(config.id, channel->id());
1868
1869 config.id = cricket::kMaxSctpSid + 1;
1870 channel = pc_->CreateDataChannel("x", &config);
1871 EXPECT_TRUE(channel == NULL);
1872}
1873
deadbeefab9b2d12015-10-14 11:33:11 -07001874// Verifies that duplicated label is allowed for SCTP data channel.
1875TEST_F(PeerConnectionInterfaceTest, SctpDuplicatedLabelAllowed) {
1876 FakeConstraints constraints;
1877 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
1878 true);
1879 CreatePeerConnection(&constraints);
1880
1881 std::string label = "test";
zhihuang9763d562016-08-05 11:14:50 -07001882 rtc::scoped_refptr<DataChannelInterface> channel =
deadbeefab9b2d12015-10-14 11:33:11 -07001883 pc_->CreateDataChannel(label, nullptr);
1884 EXPECT_NE(channel, nullptr);
1885
zhihuang9763d562016-08-05 11:14:50 -07001886 rtc::scoped_refptr<DataChannelInterface> dup_channel =
deadbeefab9b2d12015-10-14 11:33:11 -07001887 pc_->CreateDataChannel(label, nullptr);
1888 EXPECT_NE(dup_channel, nullptr);
1889}
1890
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001891// This test verifies that OnRenegotiationNeeded is fired for every new RTP
1892// DataChannel.
1893TEST_F(PeerConnectionInterfaceTest, RenegotiationNeededForNewRtpDataChannel) {
1894 FakeConstraints constraints;
1895 constraints.SetAllowRtpDataChannels();
1896 CreatePeerConnection(&constraints);
1897
zhihuang9763d562016-08-05 11:14:50 -07001898 rtc::scoped_refptr<DataChannelInterface> dc1 =
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001899 pc_->CreateDataChannel("test1", NULL);
1900 EXPECT_TRUE(observer_.renegotiation_needed_);
1901 observer_.renegotiation_needed_ = false;
1902
zhihuang9763d562016-08-05 11:14:50 -07001903 rtc::scoped_refptr<DataChannelInterface> dc2 =
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001904 pc_->CreateDataChannel("test2", NULL);
1905 EXPECT_TRUE(observer_.renegotiation_needed_);
1906}
1907
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001908// This test that a data channel closes when a PeerConnection is deleted/closed.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001909TEST_F(PeerConnectionInterfaceTest, DataChannelCloseWhenPeerConnectionClose) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001910 FakeConstraints constraints;
1911 constraints.SetAllowRtpDataChannels();
1912 CreatePeerConnection(&constraints);
1913
zhihuang9763d562016-08-05 11:14:50 -07001914 rtc::scoped_refptr<DataChannelInterface> data1 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001915 pc_->CreateDataChannel("test1", NULL);
zhihuang9763d562016-08-05 11:14:50 -07001916 rtc::scoped_refptr<DataChannelInterface> data2 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001917 pc_->CreateDataChannel("test2", NULL);
1918 ASSERT_TRUE(data1 != NULL);
kwibergd1fe2812016-04-27 06:47:29 -07001919 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001920 new MockDataChannelObserver(data1));
kwibergd1fe2812016-04-27 06:47:29 -07001921 std::unique_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001922 new MockDataChannelObserver(data2));
1923
1924 CreateOfferReceiveAnswer();
1925 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1926 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
1927
1928 ReleasePeerConnection();
1929 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
1930 EXPECT_EQ(DataChannelInterface::kClosed, data2->state());
1931}
1932
1933// This test that data channels can be rejected in an answer.
1934TEST_F(PeerConnectionInterfaceTest, TestRejectDataChannelInAnswer) {
1935 FakeConstraints constraints;
1936 constraints.SetAllowRtpDataChannels();
1937 CreatePeerConnection(&constraints);
1938
zhihuang9763d562016-08-05 11:14:50 -07001939 rtc::scoped_refptr<DataChannelInterface> offer_channel(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001940 pc_->CreateDataChannel("offer_channel", NULL));
1941
1942 CreateOfferAsLocalDescription();
1943
1944 // Create an answer where the m-line for data channels are rejected.
1945 std::string sdp;
1946 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
1947 webrtc::JsepSessionDescription* answer = new webrtc::JsepSessionDescription(
1948 SessionDescriptionInterface::kAnswer);
1949 EXPECT_TRUE(answer->Initialize(sdp, NULL));
1950 cricket::ContentInfo* data_info =
1951 answer->description()->GetContentByName("data");
1952 data_info->rejected = true;
1953
1954 DoSetRemoteDescription(answer);
1955 EXPECT_EQ(DataChannelInterface::kClosed, offer_channel->state());
1956}
1957
1958// Test that we can create a session description from an SDP string from
1959// FireFox, use it as a remote session description, generate an answer and use
1960// the answer as a local description.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07001961TEST_F(PeerConnectionInterfaceTest, ReceiveFireFoxOffer) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001962 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001963 FakeConstraints constraints;
1964 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
1965 true);
1966 CreatePeerConnection(&constraints);
1967 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
1968 SessionDescriptionInterface* desc =
1969 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
jbauchfabe2c92015-07-16 13:43:14 -07001970 webrtc::kFireFoxSdpOffer, nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001971 EXPECT_TRUE(DoSetSessionDescription(desc, false));
1972 CreateAnswerAsLocalDescription();
1973 ASSERT_TRUE(pc_->local_description() != NULL);
1974 ASSERT_TRUE(pc_->remote_description() != NULL);
1975
1976 const cricket::ContentInfo* content =
1977 cricket::GetFirstAudioContent(pc_->local_description()->description());
1978 ASSERT_TRUE(content != NULL);
1979 EXPECT_FALSE(content->rejected);
1980
1981 content =
1982 cricket::GetFirstVideoContent(pc_->local_description()->description());
1983 ASSERT_TRUE(content != NULL);
1984 EXPECT_FALSE(content->rejected);
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00001985#ifdef HAVE_SCTP
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001986 content =
1987 cricket::GetFirstDataContent(pc_->local_description()->description());
1988 ASSERT_TRUE(content != NULL);
1989 EXPECT_TRUE(content->rejected);
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00001990#endif
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001991}
1992
1993// Test that we can create an audio only offer and receive an answer with a
1994// limited set of audio codecs and receive an updated offer with more audio
1995// codecs, where the added codecs are not supported.
1996TEST_F(PeerConnectionInterfaceTest, ReceiveUpdatedAudioOfferWithBadCodecs) {
1997 CreatePeerConnection();
1998 AddVoiceStream("audio_label");
1999 CreateOfferAsLocalDescription();
2000
2001 SessionDescriptionInterface* answer =
2002 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
jbauchfabe2c92015-07-16 13:43:14 -07002003 webrtc::kAudioSdp, nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002004 EXPECT_TRUE(DoSetSessionDescription(answer, false));
2005
2006 SessionDescriptionInterface* updated_offer =
2007 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
jbauchfabe2c92015-07-16 13:43:14 -07002008 webrtc::kAudioSdpWithUnsupportedCodecs,
2009 nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002010 EXPECT_TRUE(DoSetSessionDescription(updated_offer, false));
2011 CreateAnswerAsLocalDescription();
2012}
2013
deadbeefc80741f2015-10-22 13:14:45 -07002014// Test that if we're receiving (but not sending) a track, subsequent offers
2015// will have m-lines with a=recvonly.
2016TEST_F(PeerConnectionInterfaceTest, CreateSubsequentRecvOnlyOffer) {
2017 FakeConstraints constraints;
2018 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2019 true);
2020 CreatePeerConnection(&constraints);
2021 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2022 CreateAnswerAsLocalDescription();
2023
2024 // At this point we should be receiving stream 1, but not sending anything.
2025 // A new offer should be recvonly.
kwibergd1fe2812016-04-27 06:47:29 -07002026 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefc80741f2015-10-22 13:14:45 -07002027 DoCreateOffer(&offer, nullptr);
2028
2029 const cricket::ContentInfo* video_content =
2030 cricket::GetFirstVideoContent(offer->description());
2031 const cricket::VideoContentDescription* video_desc =
2032 static_cast<const cricket::VideoContentDescription*>(
2033 video_content->description);
2034 ASSERT_EQ(cricket::MD_RECVONLY, video_desc->direction());
2035
2036 const cricket::ContentInfo* audio_content =
2037 cricket::GetFirstAudioContent(offer->description());
2038 const cricket::AudioContentDescription* audio_desc =
2039 static_cast<const cricket::AudioContentDescription*>(
2040 audio_content->description);
2041 ASSERT_EQ(cricket::MD_RECVONLY, audio_desc->direction());
2042}
2043
2044// Test that if we're receiving (but not sending) a track, and the
2045// offerToReceiveVideo/offerToReceiveAudio constraints are explicitly set to
2046// false, the generated m-lines will be a=inactive.
2047TEST_F(PeerConnectionInterfaceTest, CreateSubsequentInactiveOffer) {
2048 FakeConstraints constraints;
2049 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2050 true);
2051 CreatePeerConnection(&constraints);
2052 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2053 CreateAnswerAsLocalDescription();
2054
2055 // At this point we should be receiving stream 1, but not sending anything.
2056 // A new offer would be recvonly, but we'll set the "no receive" constraints
2057 // to make it inactive.
kwibergd1fe2812016-04-27 06:47:29 -07002058 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefc80741f2015-10-22 13:14:45 -07002059 FakeConstraints offer_constraints;
2060 offer_constraints.AddMandatory(
2061 webrtc::MediaConstraintsInterface::kOfferToReceiveVideo, false);
2062 offer_constraints.AddMandatory(
2063 webrtc::MediaConstraintsInterface::kOfferToReceiveAudio, false);
2064 DoCreateOffer(&offer, &offer_constraints);
2065
2066 const cricket::ContentInfo* video_content =
2067 cricket::GetFirstVideoContent(offer->description());
2068 const cricket::VideoContentDescription* video_desc =
2069 static_cast<const cricket::VideoContentDescription*>(
2070 video_content->description);
2071 ASSERT_EQ(cricket::MD_INACTIVE, video_desc->direction());
2072
2073 const cricket::ContentInfo* audio_content =
2074 cricket::GetFirstAudioContent(offer->description());
2075 const cricket::AudioContentDescription* audio_desc =
2076 static_cast<const cricket::AudioContentDescription*>(
2077 audio_content->description);
2078 ASSERT_EQ(cricket::MD_INACTIVE, audio_desc->direction());
2079}
2080
deadbeef653b8e02015-11-11 12:55:10 -08002081// Test that we can use SetConfiguration to change the ICE servers of the
2082// PortAllocator.
2083TEST_F(PeerConnectionInterfaceTest, SetConfigurationChangesIceServers) {
2084 CreatePeerConnection();
2085
2086 PeerConnectionInterface::RTCConfiguration config;
2087 PeerConnectionInterface::IceServer server;
2088 server.uri = "stun:test_hostname";
2089 config.servers.push_back(server);
2090 EXPECT_TRUE(pc_->SetConfiguration(config));
2091
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002092 EXPECT_EQ(1u, port_allocator_->stun_servers().size());
2093 EXPECT_EQ("test_hostname",
2094 port_allocator_->stun_servers().begin()->hostname());
deadbeef653b8e02015-11-11 12:55:10 -08002095}
2096
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002097TEST_F(PeerConnectionInterfaceTest, SetConfigurationChangesCandidateFilter) {
2098 CreatePeerConnection();
2099 PeerConnectionInterface::RTCConfiguration config;
2100 config.type = PeerConnectionInterface::kRelay;
2101 EXPECT_TRUE(pc_->SetConfiguration(config));
2102 EXPECT_EQ(cricket::CF_RELAY, port_allocator_->candidate_filter());
2103}
2104
2105// Test that when SetConfiguration changes both the pool size and other
2106// attributes, the pooled session is created with the updated attributes.
2107TEST_F(PeerConnectionInterfaceTest,
2108 SetConfigurationCreatesPooledSessionCorrectly) {
2109 CreatePeerConnection();
2110 PeerConnectionInterface::RTCConfiguration config;
2111 config.ice_candidate_pool_size = 1;
2112 PeerConnectionInterface::IceServer server;
2113 server.uri = kStunAddressOnly;
2114 config.servers.push_back(server);
2115 config.type = PeerConnectionInterface::kRelay;
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07002116 EXPECT_TRUE(pc_->SetConfiguration(config));
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002117
2118 const cricket::FakePortAllocatorSession* session =
2119 static_cast<const cricket::FakePortAllocatorSession*>(
2120 port_allocator_->GetPooledSession());
2121 ASSERT_NE(nullptr, session);
2122 EXPECT_EQ(1UL, session->stun_servers().size());
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002123}
2124
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002125// Test that PeerConnection::Close changes the states to closed and all remote
2126// tracks change state to ended.
2127TEST_F(PeerConnectionInterfaceTest, CloseAndTestStreamsAndStates) {
2128 // Initialize a PeerConnection and negotiate local and remote session
2129 // description.
2130 InitiateCall();
2131 ASSERT_EQ(1u, pc_->local_streams()->count());
2132 ASSERT_EQ(1u, pc_->remote_streams()->count());
2133
2134 pc_->Close();
2135
2136 EXPECT_EQ(PeerConnectionInterface::kClosed, pc_->signaling_state());
2137 EXPECT_EQ(PeerConnectionInterface::kIceConnectionClosed,
2138 pc_->ice_connection_state());
2139 EXPECT_EQ(PeerConnectionInterface::kIceGatheringComplete,
2140 pc_->ice_gathering_state());
2141
2142 EXPECT_EQ(1u, pc_->local_streams()->count());
2143 EXPECT_EQ(1u, pc_->remote_streams()->count());
2144
zhihuang9763d562016-08-05 11:14:50 -07002145 rtc::scoped_refptr<MediaStreamInterface> remote_stream =
2146 pc_->remote_streams()->at(0);
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002147 // Track state may be updated asynchronously.
perkjd61bf802016-03-24 03:16:19 -07002148 EXPECT_EQ_WAIT(MediaStreamTrackInterface::kEnded,
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002149 remote_stream->GetAudioTracks()[0]->state(), kTimeout);
2150 EXPECT_EQ_WAIT(MediaStreamTrackInterface::kEnded,
2151 remote_stream->GetVideoTracks()[0]->state(), kTimeout);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002152}
2153
2154// Test that PeerConnection methods fails gracefully after
2155// PeerConnection::Close has been called.
2156TEST_F(PeerConnectionInterfaceTest, CloseAndTestMethods) {
2157 CreatePeerConnection();
2158 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
2159 CreateOfferAsRemoteDescription();
2160 CreateAnswerAsLocalDescription();
2161
2162 ASSERT_EQ(1u, pc_->local_streams()->count());
zhihuang9763d562016-08-05 11:14:50 -07002163 rtc::scoped_refptr<MediaStreamInterface> local_stream =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002164 pc_->local_streams()->at(0);
2165
2166 pc_->Close();
2167
2168 pc_->RemoveStream(local_stream);
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +00002169 EXPECT_FALSE(pc_->AddStream(local_stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002170
2171 ASSERT_FALSE(local_stream->GetAudioTracks().empty());
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002172 rtc::scoped_refptr<webrtc::DtmfSenderInterface> dtmf_sender(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002173 pc_->CreateDtmfSender(local_stream->GetAudioTracks()[0]));
wu@webrtc.org66037362013-08-13 00:09:35 +00002174 EXPECT_TRUE(NULL == dtmf_sender); // local stream has been removed.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002175
2176 EXPECT_TRUE(pc_->CreateDataChannel("test", NULL) == NULL);
2177
2178 EXPECT_TRUE(pc_->local_description() != NULL);
2179 EXPECT_TRUE(pc_->remote_description() != NULL);
2180
kwibergd1fe2812016-04-27 06:47:29 -07002181 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07002182 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
kwibergd1fe2812016-04-27 06:47:29 -07002183 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -07002184 EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002185
2186 std::string sdp;
2187 ASSERT_TRUE(pc_->remote_description()->ToString(&sdp));
2188 SessionDescriptionInterface* remote_offer =
2189 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
2190 sdp, NULL);
2191 EXPECT_FALSE(DoSetRemoteDescription(remote_offer));
2192
2193 ASSERT_TRUE(pc_->local_description()->ToString(&sdp));
2194 SessionDescriptionInterface* local_offer =
2195 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
2196 sdp, NULL);
2197 EXPECT_FALSE(DoSetLocalDescription(local_offer));
2198}
2199
2200// Test that GetStats can still be called after PeerConnection::Close.
2201TEST_F(PeerConnectionInterfaceTest, CloseAndGetStats) {
2202 InitiateCall();
2203 pc_->Close();
2204 DoGetStats(NULL);
2205}
deadbeefab9b2d12015-10-14 11:33:11 -07002206
2207// NOTE: The series of tests below come from what used to be
2208// mediastreamsignaling_unittest.cc, and are mostly aimed at testing that
2209// setting a remote or local description has the expected effects.
2210
2211// This test verifies that the remote MediaStreams corresponding to a received
2212// SDP string is created. In this test the two separate MediaStreams are
2213// signaled.
2214TEST_F(PeerConnectionInterfaceTest, UpdateRemoteStreams) {
2215 FakeConstraints constraints;
2216 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2217 true);
2218 CreatePeerConnection(&constraints);
2219 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2220
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002221 rtc::scoped_refptr<StreamCollection> reference(CreateStreamCollection(1, 1));
deadbeefab9b2d12015-10-14 11:33:11 -07002222 EXPECT_TRUE(
2223 CompareStreamCollections(observer_.remote_streams(), reference.get()));
2224 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2225 EXPECT_TRUE(remote_stream->GetVideoTracks()[0]->GetSource() != nullptr);
2226
2227 // Create a session description based on another SDP with another
2228 // MediaStream.
2229 CreateAndSetRemoteOffer(kSdpStringWithStream1And2);
2230
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002231 rtc::scoped_refptr<StreamCollection> reference2(CreateStreamCollection(2, 1));
deadbeefab9b2d12015-10-14 11:33:11 -07002232 EXPECT_TRUE(
2233 CompareStreamCollections(observer_.remote_streams(), reference2.get()));
2234}
2235
2236// This test verifies that when remote tracks are added/removed from SDP, the
2237// created remote streams are updated appropriately.
2238TEST_F(PeerConnectionInterfaceTest,
2239 AddRemoveTrackFromExistingRemoteMediaStream) {
2240 FakeConstraints constraints;
2241 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2242 true);
2243 CreatePeerConnection(&constraints);
kwibergd1fe2812016-04-27 06:47:29 -07002244 std::unique_ptr<SessionDescriptionInterface> desc_ms1 =
kwiberg2bbff992016-03-16 11:03:04 -07002245 CreateSessionDescriptionAndReference(1, 1);
deadbeefab9b2d12015-10-14 11:33:11 -07002246 EXPECT_TRUE(DoSetRemoteDescription(desc_ms1.release()));
2247 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
2248 reference_collection_));
2249
2250 // Add extra audio and video tracks to the same MediaStream.
kwibergd1fe2812016-04-27 06:47:29 -07002251 std::unique_ptr<SessionDescriptionInterface> desc_ms1_two_tracks =
kwiberg2bbff992016-03-16 11:03:04 -07002252 CreateSessionDescriptionAndReference(2, 2);
deadbeefab9b2d12015-10-14 11:33:11 -07002253 EXPECT_TRUE(DoSetRemoteDescription(desc_ms1_two_tracks.release()));
2254 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
2255 reference_collection_));
zhihuang9763d562016-08-05 11:14:50 -07002256 rtc::scoped_refptr<AudioTrackInterface> audio_track2 =
perkjd61bf802016-03-24 03:16:19 -07002257 observer_.remote_streams()->at(0)->GetAudioTracks()[1];
2258 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, audio_track2->state());
zhihuang9763d562016-08-05 11:14:50 -07002259 rtc::scoped_refptr<VideoTrackInterface> video_track2 =
perkjd61bf802016-03-24 03:16:19 -07002260 observer_.remote_streams()->at(0)->GetVideoTracks()[1];
2261 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, video_track2->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002262
2263 // Remove the extra audio and video tracks.
kwibergd1fe2812016-04-27 06:47:29 -07002264 std::unique_ptr<SessionDescriptionInterface> desc_ms2 =
kwiberg2bbff992016-03-16 11:03:04 -07002265 CreateSessionDescriptionAndReference(1, 1);
perkjd61bf802016-03-24 03:16:19 -07002266 MockTrackObserver audio_track_observer(audio_track2);
2267 MockTrackObserver video_track_observer(video_track2);
2268
2269 EXPECT_CALL(audio_track_observer, OnChanged()).Times(Exactly(1));
2270 EXPECT_CALL(video_track_observer, OnChanged()).Times(Exactly(1));
deadbeefab9b2d12015-10-14 11:33:11 -07002271 EXPECT_TRUE(DoSetRemoteDescription(desc_ms2.release()));
2272 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
2273 reference_collection_));
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002274 // Track state may be updated asynchronously.
perkjd61bf802016-03-24 03:16:19 -07002275 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002276 audio_track2->state(), kTimeout);
2277 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
2278 video_track2->state(), kTimeout);
deadbeefab9b2d12015-10-14 11:33:11 -07002279}
2280
2281// This tests that remote tracks are ended if a local session description is set
2282// that rejects the media content type.
2283TEST_F(PeerConnectionInterfaceTest, RejectMediaContent) {
2284 FakeConstraints constraints;
2285 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2286 true);
2287 CreatePeerConnection(&constraints);
2288 // First create and set a remote offer, then reject its video content in our
2289 // answer.
2290 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2291 ASSERT_EQ(1u, observer_.remote_streams()->count());
2292 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2293 ASSERT_EQ(1u, remote_stream->GetVideoTracks().size());
2294 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2295
2296 rtc::scoped_refptr<webrtc::VideoTrackInterface> remote_video =
2297 remote_stream->GetVideoTracks()[0];
2298 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, remote_video->state());
2299 rtc::scoped_refptr<webrtc::AudioTrackInterface> remote_audio =
2300 remote_stream->GetAudioTracks()[0];
2301 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, remote_audio->state());
2302
kwibergd1fe2812016-04-27 06:47:29 -07002303 std::unique_ptr<SessionDescriptionInterface> local_answer;
kwiberg2bbff992016-03-16 11:03:04 -07002304 EXPECT_TRUE(DoCreateAnswer(&local_answer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07002305 cricket::ContentInfo* video_info =
2306 local_answer->description()->GetContentByName("video");
2307 video_info->rejected = true;
2308 EXPECT_TRUE(DoSetLocalDescription(local_answer.release()));
2309 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kEnded, remote_video->state());
2310 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, remote_audio->state());
2311
2312 // Now create an offer where we reject both video and audio.
kwibergd1fe2812016-04-27 06:47:29 -07002313 std::unique_ptr<SessionDescriptionInterface> local_offer;
kwiberg2bbff992016-03-16 11:03:04 -07002314 EXPECT_TRUE(DoCreateOffer(&local_offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07002315 video_info = local_offer->description()->GetContentByName("video");
2316 ASSERT_TRUE(video_info != nullptr);
2317 video_info->rejected = true;
2318 cricket::ContentInfo* audio_info =
2319 local_offer->description()->GetContentByName("audio");
2320 ASSERT_TRUE(audio_info != nullptr);
2321 audio_info->rejected = true;
2322 EXPECT_TRUE(DoSetLocalDescription(local_offer.release()));
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002323 // Track state may be updated asynchronously.
perkjd61bf802016-03-24 03:16:19 -07002324 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002325 remote_audio->state(), kTimeout);
2326 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
2327 remote_video->state(), kTimeout);
deadbeefab9b2d12015-10-14 11:33:11 -07002328}
2329
2330// This tests that we won't crash if the remote track has been removed outside
2331// of PeerConnection and then PeerConnection tries to reject the track.
2332TEST_F(PeerConnectionInterfaceTest, RemoveTrackThenRejectMediaContent) {
2333 FakeConstraints constraints;
2334 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2335 true);
2336 CreatePeerConnection(&constraints);
2337 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2338 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2339 remote_stream->RemoveTrack(remote_stream->GetVideoTracks()[0]);
2340 remote_stream->RemoveTrack(remote_stream->GetAudioTracks()[0]);
2341
kwibergd1fe2812016-04-27 06:47:29 -07002342 std::unique_ptr<SessionDescriptionInterface> local_answer(
deadbeefab9b2d12015-10-14 11:33:11 -07002343 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
2344 kSdpStringWithStream1, nullptr));
2345 cricket::ContentInfo* video_info =
2346 local_answer->description()->GetContentByName("video");
2347 video_info->rejected = true;
2348 cricket::ContentInfo* audio_info =
2349 local_answer->description()->GetContentByName("audio");
2350 audio_info->rejected = true;
2351 EXPECT_TRUE(DoSetLocalDescription(local_answer.release()));
2352
2353 // No crash is a pass.
2354}
2355
deadbeef5e97fb52015-10-15 12:49:08 -07002356// This tests that if a recvonly remote description is set, no remote streams
2357// will be created, even if the description contains SSRCs/MSIDs.
2358// See: https://code.google.com/p/webrtc/issues/detail?id=5054
2359TEST_F(PeerConnectionInterfaceTest, RecvonlyDescriptionDoesntCreateStream) {
2360 FakeConstraints constraints;
2361 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2362 true);
2363 CreatePeerConnection(&constraints);
2364
2365 std::string recvonly_offer = kSdpStringWithStream1;
2366 rtc::replace_substrs(kSendrecv, strlen(kSendrecv), kRecvonly,
2367 strlen(kRecvonly), &recvonly_offer);
2368 CreateAndSetRemoteOffer(recvonly_offer);
2369
2370 EXPECT_EQ(0u, observer_.remote_streams()->count());
2371}
2372
deadbeefab9b2d12015-10-14 11:33:11 -07002373// This tests that a default MediaStream is created if a remote session
2374// description doesn't contain any streams and no MSID support.
2375// It also tests that the default stream is updated if a video m-line is added
2376// in a subsequent session description.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002377TEST_F(PeerConnectionInterfaceTest, SdpWithoutMsidCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002378 FakeConstraints constraints;
2379 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2380 true);
2381 CreatePeerConnection(&constraints);
2382 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2383
2384 ASSERT_EQ(1u, observer_.remote_streams()->count());
2385 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2386
2387 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2388 EXPECT_EQ(0u, remote_stream->GetVideoTracks().size());
2389 EXPECT_EQ("default", remote_stream->label());
2390
2391 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2392 ASSERT_EQ(1u, observer_.remote_streams()->count());
2393 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2394 EXPECT_EQ("defaulta0", remote_stream->GetAudioTracks()[0]->id());
deadbeef884f5852016-01-15 09:20:04 -08002395 EXPECT_EQ(MediaStreamTrackInterface::kLive,
2396 remote_stream->GetAudioTracks()[0]->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002397 ASSERT_EQ(1u, remote_stream->GetVideoTracks().size());
2398 EXPECT_EQ("defaultv0", remote_stream->GetVideoTracks()[0]->id());
deadbeef884f5852016-01-15 09:20:04 -08002399 EXPECT_EQ(MediaStreamTrackInterface::kLive,
2400 remote_stream->GetVideoTracks()[0]->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002401}
2402
2403// This tests that a default MediaStream is created if a remote session
2404// description doesn't contain any streams and media direction is send only.
2405TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002406 SendOnlySdpWithoutMsidCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002407 FakeConstraints constraints;
2408 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2409 true);
2410 CreatePeerConnection(&constraints);
2411 CreateAndSetRemoteOffer(kSdpStringSendOnlyWithoutStreams);
2412
2413 ASSERT_EQ(1u, observer_.remote_streams()->count());
2414 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2415
2416 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2417 EXPECT_EQ(1u, remote_stream->GetVideoTracks().size());
2418 EXPECT_EQ("default", remote_stream->label());
2419}
2420
2421// This tests that it won't crash when PeerConnection tries to remove
2422// a remote track that as already been removed from the MediaStream.
2423TEST_F(PeerConnectionInterfaceTest, RemoveAlreadyGoneRemoteStream) {
2424 FakeConstraints constraints;
2425 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2426 true);
2427 CreatePeerConnection(&constraints);
2428 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2429 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2430 remote_stream->RemoveTrack(remote_stream->GetAudioTracks()[0]);
2431 remote_stream->RemoveTrack(remote_stream->GetVideoTracks()[0]);
2432
2433 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2434
2435 // No crash is a pass.
2436}
2437
2438// This tests that a default MediaStream is created if the remote session
2439// description doesn't contain any streams and don't contain an indication if
2440// MSID is supported.
2441TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002442 SdpWithoutMsidAndStreamsCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002443 FakeConstraints constraints;
2444 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2445 true);
2446 CreatePeerConnection(&constraints);
2447 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2448
2449 ASSERT_EQ(1u, observer_.remote_streams()->count());
2450 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2451 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2452 EXPECT_EQ(1u, remote_stream->GetVideoTracks().size());
2453}
2454
2455// This tests that a default MediaStream is not created if the remote session
2456// description doesn't contain any streams but does support MSID.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002457TEST_F(PeerConnectionInterfaceTest, SdpWithMsidDontCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002458 FakeConstraints constraints;
2459 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2460 true);
2461 CreatePeerConnection(&constraints);
2462 CreateAndSetRemoteOffer(kSdpStringWithMsidWithoutStreams);
2463 EXPECT_EQ(0u, observer_.remote_streams()->count());
2464}
2465
deadbeefbda7e0b2015-12-08 17:13:40 -08002466// This tests that when setting a new description, the old default tracks are
2467// not destroyed and recreated.
2468// See: https://bugs.chromium.org/p/webrtc/issues/detail?id=5250
Stefan Holmer102362b2016-03-18 09:39:07 +01002469TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002470 DefaultTracksNotDestroyedAndRecreated) {
deadbeefbda7e0b2015-12-08 17:13:40 -08002471 FakeConstraints constraints;
2472 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2473 true);
2474 CreatePeerConnection(&constraints);
2475 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2476
2477 ASSERT_EQ(1u, observer_.remote_streams()->count());
2478 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2479 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2480
2481 // Set the track to "disabled", then set a new description and ensure the
2482 // track is still disabled, which ensures it hasn't been recreated.
2483 remote_stream->GetAudioTracks()[0]->set_enabled(false);
2484 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2485 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2486 EXPECT_FALSE(remote_stream->GetAudioTracks()[0]->enabled());
2487}
2488
deadbeefab9b2d12015-10-14 11:33:11 -07002489// This tests that a default MediaStream is not created if a remote session
2490// description is updated to not have any MediaStreams.
2491TEST_F(PeerConnectionInterfaceTest, VerifyDefaultStreamIsNotCreated) {
2492 FakeConstraints constraints;
2493 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2494 true);
2495 CreatePeerConnection(&constraints);
2496 CreateAndSetRemoteOffer(kSdpStringWithStream1);
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002497 rtc::scoped_refptr<StreamCollection> reference(CreateStreamCollection(1, 1));
deadbeefab9b2d12015-10-14 11:33:11 -07002498 EXPECT_TRUE(
2499 CompareStreamCollections(observer_.remote_streams(), reference.get()));
2500
2501 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2502 EXPECT_EQ(0u, observer_.remote_streams()->count());
2503}
2504
2505// This tests that an RtpSender is created when the local description is set
2506// after adding a local stream.
2507// TODO(deadbeef): This test and the one below it need to be updated when
2508// an RtpSender's lifetime isn't determined by when a local description is set.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002509TEST_F(PeerConnectionInterfaceTest, LocalDescriptionChanged) {
deadbeefab9b2d12015-10-14 11:33:11 -07002510 FakeConstraints constraints;
2511 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2512 true);
2513 CreatePeerConnection(&constraints);
deadbeefab9b2d12015-10-14 11:33:11 -07002514
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002515 // Create an offer with 1 stream with 2 tracks of each type.
2516 rtc::scoped_refptr<StreamCollection> stream_collection =
2517 CreateStreamCollection(1, 2);
2518 pc_->AddStream(stream_collection->at(0));
2519 std::unique_ptr<SessionDescriptionInterface> offer;
2520 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2521 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
deadbeefab9b2d12015-10-14 11:33:11 -07002522
deadbeefab9b2d12015-10-14 11:33:11 -07002523 auto senders = pc_->GetSenders();
2524 EXPECT_EQ(4u, senders.size());
2525 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2526 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2527 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[1]));
2528 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[1]));
2529
2530 // Remove an audio and video track.
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002531 pc_->RemoveStream(stream_collection->at(0));
2532 stream_collection = CreateStreamCollection(1, 1);
2533 pc_->AddStream(stream_collection->at(0));
2534 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2535 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
2536
deadbeefab9b2d12015-10-14 11:33:11 -07002537 senders = pc_->GetSenders();
2538 EXPECT_EQ(2u, senders.size());
2539 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2540 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2541 EXPECT_FALSE(ContainsSender(senders, kAudioTracks[1]));
2542 EXPECT_FALSE(ContainsSender(senders, kVideoTracks[1]));
2543}
2544
2545// This tests that an RtpSender is created when the local description is set
2546// before adding a local stream.
2547TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002548 AddLocalStreamAfterLocalDescriptionChanged) {
deadbeefab9b2d12015-10-14 11:33:11 -07002549 FakeConstraints constraints;
2550 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2551 true);
2552 CreatePeerConnection(&constraints);
deadbeefab9b2d12015-10-14 11:33:11 -07002553
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002554 rtc::scoped_refptr<StreamCollection> stream_collection =
2555 CreateStreamCollection(1, 2);
2556 // Add a stream to create the offer, but remove it afterwards.
2557 pc_->AddStream(stream_collection->at(0));
2558 std::unique_ptr<SessionDescriptionInterface> offer;
2559 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2560 pc_->RemoveStream(stream_collection->at(0));
deadbeefab9b2d12015-10-14 11:33:11 -07002561
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002562 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
deadbeefab9b2d12015-10-14 11:33:11 -07002563 auto senders = pc_->GetSenders();
2564 EXPECT_EQ(0u, senders.size());
2565
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002566 pc_->AddStream(stream_collection->at(0));
deadbeefab9b2d12015-10-14 11:33:11 -07002567 senders = pc_->GetSenders();
2568 EXPECT_EQ(4u, senders.size());
2569 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2570 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2571 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[1]));
2572 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[1]));
2573}
2574
2575// This tests that the expected behavior occurs if the SSRC on a local track is
2576// changed when SetLocalDescription is called.
2577TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002578 ChangeSsrcOnTrackInLocalSessionDescription) {
deadbeefab9b2d12015-10-14 11:33:11 -07002579 FakeConstraints constraints;
2580 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2581 true);
2582 CreatePeerConnection(&constraints);
deadbeefab9b2d12015-10-14 11:33:11 -07002583
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002584 rtc::scoped_refptr<StreamCollection> stream_collection =
2585 CreateStreamCollection(2, 1);
2586 pc_->AddStream(stream_collection->at(0));
2587 std::unique_ptr<SessionDescriptionInterface> offer;
2588 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2589 // Grab a copy of the offer before it gets passed into the PC.
2590 std::unique_ptr<JsepSessionDescription> modified_offer(
2591 new JsepSessionDescription(JsepSessionDescription::kOffer));
2592 modified_offer->Initialize(offer->description()->Copy(), offer->session_id(),
2593 offer->session_version());
2594 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
deadbeefab9b2d12015-10-14 11:33:11 -07002595
deadbeefab9b2d12015-10-14 11:33:11 -07002596 auto senders = pc_->GetSenders();
2597 EXPECT_EQ(2u, senders.size());
2598 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2599 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2600
2601 // Change the ssrc of the audio and video track.
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002602 cricket::MediaContentDescription* desc =
2603 cricket::GetFirstAudioContentDescription(modified_offer->description());
2604 ASSERT_TRUE(desc != NULL);
2605 for (StreamParams& stream : desc->mutable_streams()) {
2606 for (unsigned int& ssrc : stream.ssrcs) {
2607 ++ssrc;
2608 }
2609 }
deadbeefab9b2d12015-10-14 11:33:11 -07002610
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002611 desc =
2612 cricket::GetFirstVideoContentDescription(modified_offer->description());
2613 ASSERT_TRUE(desc != NULL);
2614 for (StreamParams& stream : desc->mutable_streams()) {
2615 for (unsigned int& ssrc : stream.ssrcs) {
2616 ++ssrc;
2617 }
2618 }
2619
2620 EXPECT_TRUE(DoSetLocalDescription(modified_offer.release()));
deadbeefab9b2d12015-10-14 11:33:11 -07002621 senders = pc_->GetSenders();
2622 EXPECT_EQ(2u, senders.size());
2623 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2624 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2625 // TODO(deadbeef): Once RtpSenders expose parameters, check that the SSRC
2626 // changed.
2627}
2628
2629// This tests that the expected behavior occurs if a new session description is
2630// set with the same tracks, but on a different MediaStream.
Stefan Holmer55d6e7c2016-03-17 16:26:40 +01002631TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002632 SignalSameTracksInSeparateMediaStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002633 FakeConstraints constraints;
2634 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2635 true);
2636 CreatePeerConnection(&constraints);
deadbeefab9b2d12015-10-14 11:33:11 -07002637
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002638 rtc::scoped_refptr<StreamCollection> stream_collection =
2639 CreateStreamCollection(2, 1);
2640 pc_->AddStream(stream_collection->at(0));
2641 std::unique_ptr<SessionDescriptionInterface> offer;
2642 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2643 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
deadbeefab9b2d12015-10-14 11:33:11 -07002644
deadbeefab9b2d12015-10-14 11:33:11 -07002645 auto senders = pc_->GetSenders();
2646 EXPECT_EQ(2u, senders.size());
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002647 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0], kStreams[0]));
2648 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0], kStreams[0]));
deadbeefab9b2d12015-10-14 11:33:11 -07002649
2650 // Add a new MediaStream but with the same tracks as in the first stream.
2651 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream_1(
2652 webrtc::MediaStream::Create(kStreams[1]));
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002653 stream_1->AddTrack(stream_collection->at(0)->GetVideoTracks()[0]);
2654 stream_1->AddTrack(stream_collection->at(0)->GetAudioTracks()[0]);
deadbeefab9b2d12015-10-14 11:33:11 -07002655 pc_->AddStream(stream_1);
2656
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002657 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2658 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
deadbeefab9b2d12015-10-14 11:33:11 -07002659
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002660 auto new_senders = pc_->GetSenders();
2661 // Should be the same senders as before, but with updated stream id.
2662 // Note that this behavior is subject to change in the future.
2663 // We may decide the PC should ignore existing tracks in AddStream.
2664 EXPECT_EQ(senders, new_senders);
2665 EXPECT_TRUE(ContainsSender(new_senders, kAudioTracks[0], kStreams[1]));
2666 EXPECT_TRUE(ContainsSender(new_senders, kVideoTracks[0], kStreams[1]));
deadbeefab9b2d12015-10-14 11:33:11 -07002667}
2668
nisse51542be2016-02-12 02:27:06 -08002669class PeerConnectionMediaConfigTest : public testing::Test {
2670 protected:
2671 void SetUp() override {
nisseaf510af2016-03-21 08:20:42 -07002672 pcf_ = new rtc::RefCountedObject<PeerConnectionFactoryForTest>();
nisse51542be2016-02-12 02:27:06 -08002673 pcf_->Initialize();
2674 }
2675 const cricket::MediaConfig& TestCreatePeerConnection(
2676 const PeerConnectionInterface::RTCConfiguration& config,
2677 const MediaConstraintsInterface *constraints) {
2678 pcf_->create_media_controller_called_ = false;
2679
zhihuang9763d562016-08-05 11:14:50 -07002680 rtc::scoped_refptr<PeerConnectionInterface> pc(pcf_->CreatePeerConnection(
2681 config, constraints, nullptr, nullptr, &observer_));
nisse51542be2016-02-12 02:27:06 -08002682 EXPECT_TRUE(pc.get());
2683 EXPECT_TRUE(pcf_->create_media_controller_called_);
2684 return pcf_->create_media_controller_config_;
2685 }
2686
zhihuang9763d562016-08-05 11:14:50 -07002687 rtc::scoped_refptr<PeerConnectionFactoryForTest> pcf_;
nisse51542be2016-02-12 02:27:06 -08002688 MockPeerConnectionObserver observer_;
2689};
2690
2691// This test verifies the default behaviour with no constraints and a
2692// default RTCConfiguration.
2693TEST_F(PeerConnectionMediaConfigTest, TestDefaults) {
2694 PeerConnectionInterface::RTCConfiguration config;
2695 FakeConstraints constraints;
2696
2697 const cricket::MediaConfig& media_config =
2698 TestCreatePeerConnection(config, &constraints);
2699
2700 EXPECT_FALSE(media_config.enable_dscp);
nisse0db023a2016-03-01 04:29:59 -08002701 EXPECT_TRUE(media_config.video.enable_cpu_overuse_detection);
2702 EXPECT_FALSE(media_config.video.disable_prerenderer_smoothing);
2703 EXPECT_FALSE(media_config.video.suspend_below_min_bitrate);
nisse51542be2016-02-12 02:27:06 -08002704}
2705
2706// This test verifies the DSCP constraint is recognized and passed to
2707// the CreateMediaController call.
2708TEST_F(PeerConnectionMediaConfigTest, TestDscpConstraintTrue) {
2709 PeerConnectionInterface::RTCConfiguration config;
2710 FakeConstraints constraints;
2711
2712 constraints.AddOptional(webrtc::MediaConstraintsInterface::kEnableDscp, true);
2713 const cricket::MediaConfig& media_config =
2714 TestCreatePeerConnection(config, &constraints);
2715
2716 EXPECT_TRUE(media_config.enable_dscp);
2717}
2718
2719// This test verifies the cpu overuse detection constraint is
2720// recognized and passed to the CreateMediaController call.
2721TEST_F(PeerConnectionMediaConfigTest, TestCpuOveruseConstraintFalse) {
2722 PeerConnectionInterface::RTCConfiguration config;
2723 FakeConstraints constraints;
2724
2725 constraints.AddOptional(
2726 webrtc::MediaConstraintsInterface::kCpuOveruseDetection, false);
2727 const cricket::MediaConfig media_config =
2728 TestCreatePeerConnection(config, &constraints);
2729
nisse0db023a2016-03-01 04:29:59 -08002730 EXPECT_FALSE(media_config.video.enable_cpu_overuse_detection);
nisse51542be2016-02-12 02:27:06 -08002731}
2732
2733// This test verifies that the disable_prerenderer_smoothing flag is
2734// propagated from RTCConfiguration to the CreateMediaController call.
2735TEST_F(PeerConnectionMediaConfigTest, TestDisablePrerendererSmoothingTrue) {
2736 PeerConnectionInterface::RTCConfiguration config;
2737 FakeConstraints constraints;
2738
Niels Möller71bdda02016-03-31 12:59:59 +02002739 config.set_prerenderer_smoothing(false);
nisse51542be2016-02-12 02:27:06 -08002740 const cricket::MediaConfig& media_config =
2741 TestCreatePeerConnection(config, &constraints);
2742
nisse0db023a2016-03-01 04:29:59 -08002743 EXPECT_TRUE(media_config.video.disable_prerenderer_smoothing);
2744}
2745
2746// This test verifies the suspend below min bitrate constraint is
2747// recognized and passed to the CreateMediaController call.
2748TEST_F(PeerConnectionMediaConfigTest,
2749 TestSuspendBelowMinBitrateConstraintTrue) {
2750 PeerConnectionInterface::RTCConfiguration config;
2751 FakeConstraints constraints;
2752
2753 constraints.AddOptional(
2754 webrtc::MediaConstraintsInterface::kEnableVideoSuspendBelowMinBitrate,
2755 true);
2756 const cricket::MediaConfig media_config =
2757 TestCreatePeerConnection(config, &constraints);
2758
2759 EXPECT_TRUE(media_config.video.suspend_below_min_bitrate);
nisse51542be2016-02-12 02:27:06 -08002760}
2761
deadbeefab9b2d12015-10-14 11:33:11 -07002762// The following tests verify that session options are created correctly.
deadbeefc80741f2015-10-22 13:14:45 -07002763// TODO(deadbeef): Convert these tests to be more end-to-end. Instead of
2764// "verify options are converted correctly", should be "pass options into
2765// CreateOffer and verify the correct offer is produced."
deadbeefab9b2d12015-10-14 11:33:11 -07002766
2767TEST(CreateSessionOptionsTest, GetOptionsForOfferWithInvalidAudioOption) {
2768 RTCOfferAnswerOptions rtc_options;
2769 rtc_options.offer_to_receive_audio = RTCOfferAnswerOptions::kUndefined - 1;
2770
2771 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08002772 EXPECT_FALSE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002773
2774 rtc_options.offer_to_receive_audio =
2775 RTCOfferAnswerOptions::kMaxOfferToReceiveMedia + 1;
htaaac2dea2016-03-10 13:35:55 -08002776 EXPECT_FALSE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002777}
2778
2779TEST(CreateSessionOptionsTest, GetOptionsForOfferWithInvalidVideoOption) {
2780 RTCOfferAnswerOptions rtc_options;
2781 rtc_options.offer_to_receive_video = RTCOfferAnswerOptions::kUndefined - 1;
2782
2783 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08002784 EXPECT_FALSE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002785
2786 rtc_options.offer_to_receive_video =
2787 RTCOfferAnswerOptions::kMaxOfferToReceiveMedia + 1;
htaaac2dea2016-03-10 13:35:55 -08002788 EXPECT_FALSE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002789}
2790
2791// Test that a MediaSessionOptions is created for an offer if
deadbeefc80741f2015-10-22 13:14:45 -07002792// OfferToReceiveAudio and OfferToReceiveVideo options are set.
deadbeefab9b2d12015-10-14 11:33:11 -07002793TEST(CreateSessionOptionsTest, GetMediaSessionOptionsForOfferWithAudioVideo) {
2794 RTCOfferAnswerOptions rtc_options;
2795 rtc_options.offer_to_receive_audio = 1;
2796 rtc_options.offer_to_receive_video = 1;
2797
2798 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08002799 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002800 EXPECT_TRUE(options.has_audio());
2801 EXPECT_TRUE(options.has_video());
2802 EXPECT_TRUE(options.bundle_enabled);
2803}
2804
2805// Test that a correct MediaSessionOptions is created for an offer if
deadbeefc80741f2015-10-22 13:14:45 -07002806// OfferToReceiveAudio is set.
deadbeefab9b2d12015-10-14 11:33:11 -07002807TEST(CreateSessionOptionsTest, GetMediaSessionOptionsForOfferWithAudio) {
2808 RTCOfferAnswerOptions rtc_options;
2809 rtc_options.offer_to_receive_audio = 1;
2810
2811 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08002812 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002813 EXPECT_TRUE(options.has_audio());
2814 EXPECT_FALSE(options.has_video());
2815 EXPECT_TRUE(options.bundle_enabled);
2816}
2817
2818// Test that a correct MediaSessionOptions is created for an offer if
deadbeefc80741f2015-10-22 13:14:45 -07002819// the default OfferOptions are used.
deadbeefab9b2d12015-10-14 11:33:11 -07002820TEST(CreateSessionOptionsTest, GetDefaultMediaSessionOptionsForOffer) {
2821 RTCOfferAnswerOptions rtc_options;
2822
2823 cricket::MediaSessionOptions options;
deadbeef0ed85b22016-02-23 17:24:52 -08002824 options.transport_options["audio"] = cricket::TransportOptions();
2825 options.transport_options["video"] = cricket::TransportOptions();
htaaac2dea2016-03-10 13:35:55 -08002826 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefc80741f2015-10-22 13:14:45 -07002827 EXPECT_TRUE(options.has_audio());
deadbeefab9b2d12015-10-14 11:33:11 -07002828 EXPECT_FALSE(options.has_video());
deadbeefc80741f2015-10-22 13:14:45 -07002829 EXPECT_TRUE(options.bundle_enabled);
deadbeefab9b2d12015-10-14 11:33:11 -07002830 EXPECT_TRUE(options.vad_enabled);
deadbeef0ed85b22016-02-23 17:24:52 -08002831 EXPECT_FALSE(options.transport_options["audio"].ice_restart);
2832 EXPECT_FALSE(options.transport_options["video"].ice_restart);
deadbeefab9b2d12015-10-14 11:33:11 -07002833}
2834
2835// Test that a correct MediaSessionOptions is created for an offer if
deadbeefc80741f2015-10-22 13:14:45 -07002836// OfferToReceiveVideo is set.
deadbeefab9b2d12015-10-14 11:33:11 -07002837TEST(CreateSessionOptionsTest, GetMediaSessionOptionsForOfferWithVideo) {
2838 RTCOfferAnswerOptions rtc_options;
2839 rtc_options.offer_to_receive_audio = 0;
2840 rtc_options.offer_to_receive_video = 1;
2841
2842 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08002843 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002844 EXPECT_FALSE(options.has_audio());
2845 EXPECT_TRUE(options.has_video());
2846 EXPECT_TRUE(options.bundle_enabled);
2847}
2848
2849// Test that a correct MediaSessionOptions is created for an offer if
2850// UseRtpMux is set to false.
2851TEST(CreateSessionOptionsTest,
2852 GetMediaSessionOptionsForOfferWithBundleDisabled) {
2853 RTCOfferAnswerOptions rtc_options;
2854 rtc_options.offer_to_receive_audio = 1;
2855 rtc_options.offer_to_receive_video = 1;
2856 rtc_options.use_rtp_mux = false;
2857
2858 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08002859 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002860 EXPECT_TRUE(options.has_audio());
2861 EXPECT_TRUE(options.has_video());
2862 EXPECT_FALSE(options.bundle_enabled);
2863}
2864
2865// Test that a correct MediaSessionOptions is created to restart ice if
2866// IceRestart is set. It also tests that subsequent MediaSessionOptions don't
Taylor Brandstetterf475d362016-01-08 15:35:57 -08002867// have |audio_transport_options.ice_restart| etc. set.
deadbeefab9b2d12015-10-14 11:33:11 -07002868TEST(CreateSessionOptionsTest, GetMediaSessionOptionsForOfferWithIceRestart) {
2869 RTCOfferAnswerOptions rtc_options;
2870 rtc_options.ice_restart = true;
2871
2872 cricket::MediaSessionOptions options;
deadbeef0ed85b22016-02-23 17:24:52 -08002873 options.transport_options["audio"] = cricket::TransportOptions();
2874 options.transport_options["video"] = cricket::TransportOptions();
htaaac2dea2016-03-10 13:35:55 -08002875 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeef0ed85b22016-02-23 17:24:52 -08002876 EXPECT_TRUE(options.transport_options["audio"].ice_restart);
2877 EXPECT_TRUE(options.transport_options["video"].ice_restart);
deadbeefab9b2d12015-10-14 11:33:11 -07002878
2879 rtc_options = RTCOfferAnswerOptions();
htaaac2dea2016-03-10 13:35:55 -08002880 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeef0ed85b22016-02-23 17:24:52 -08002881 EXPECT_FALSE(options.transport_options["audio"].ice_restart);
2882 EXPECT_FALSE(options.transport_options["video"].ice_restart);
deadbeefab9b2d12015-10-14 11:33:11 -07002883}
2884
2885// Test that the MediaConstraints in an answer don't affect if audio and video
2886// is offered in an offer but that if kOfferToReceiveAudio or
2887// kOfferToReceiveVideo constraints are true in an offer, the media type will be
2888// included in subsequent answers.
2889TEST(CreateSessionOptionsTest, MediaConstraintsInAnswer) {
2890 FakeConstraints answer_c;
2891 answer_c.SetMandatoryReceiveAudio(true);
2892 answer_c.SetMandatoryReceiveVideo(true);
2893
2894 cricket::MediaSessionOptions answer_options;
2895 EXPECT_TRUE(ParseConstraintsForAnswer(&answer_c, &answer_options));
2896 EXPECT_TRUE(answer_options.has_audio());
2897 EXPECT_TRUE(answer_options.has_video());
2898
deadbeefc80741f2015-10-22 13:14:45 -07002899 RTCOfferAnswerOptions rtc_offer_options;
deadbeefab9b2d12015-10-14 11:33:11 -07002900
2901 cricket::MediaSessionOptions offer_options;
htaaac2dea2016-03-10 13:35:55 -08002902 EXPECT_TRUE(
2903 ExtractMediaSessionOptions(rtc_offer_options, false, &offer_options));
deadbeefc80741f2015-10-22 13:14:45 -07002904 EXPECT_TRUE(offer_options.has_audio());
htaaac2dea2016-03-10 13:35:55 -08002905 EXPECT_TRUE(offer_options.has_video());
deadbeefab9b2d12015-10-14 11:33:11 -07002906
deadbeefc80741f2015-10-22 13:14:45 -07002907 RTCOfferAnswerOptions updated_rtc_offer_options;
2908 updated_rtc_offer_options.offer_to_receive_audio = 1;
2909 updated_rtc_offer_options.offer_to_receive_video = 1;
deadbeefab9b2d12015-10-14 11:33:11 -07002910
2911 cricket::MediaSessionOptions updated_offer_options;
htaaac2dea2016-03-10 13:35:55 -08002912 EXPECT_TRUE(ExtractMediaSessionOptions(updated_rtc_offer_options, false,
htaa2a49d92016-03-04 02:51:39 -08002913 &updated_offer_options));
deadbeefab9b2d12015-10-14 11:33:11 -07002914 EXPECT_TRUE(updated_offer_options.has_audio());
2915 EXPECT_TRUE(updated_offer_options.has_video());
2916
2917 // Since an offer has been created with both audio and video, subsequent
2918 // offers and answers should contain both audio and video.
2919 // Answers will only contain the media types that exist in the offer
2920 // regardless of the value of |updated_answer_options.has_audio| and
2921 // |updated_answer_options.has_video|.
2922 FakeConstraints updated_answer_c;
2923 answer_c.SetMandatoryReceiveAudio(false);
2924 answer_c.SetMandatoryReceiveVideo(false);
2925
2926 cricket::MediaSessionOptions updated_answer_options;
2927 EXPECT_TRUE(
2928 ParseConstraintsForAnswer(&updated_answer_c, &updated_answer_options));
2929 EXPECT_TRUE(updated_answer_options.has_audio());
2930 EXPECT_TRUE(updated_answer_options.has_video());
deadbeefab9b2d12015-10-14 11:33:11 -07002931}