blob: be4dc06acfe4b8a5491550f89557edb726a3ff3c [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
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001176TEST_F(PeerConnectionInterfaceTest, AddStreams) {
1177 CreatePeerConnection();
deadbeefab9b2d12015-10-14 11:33:11 -07001178 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001179 AddVoiceStream(kStreamLabel2);
1180 ASSERT_EQ(2u, pc_->local_streams()->count());
1181
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001182 // Test we can add multiple local streams to one peerconnection.
zhihuang9763d562016-08-05 11:14:50 -07001183 rtc::scoped_refptr<MediaStreamInterface> stream(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001184 pc_factory_->CreateLocalMediaStream(kStreamLabel3));
zhihuang9763d562016-08-05 11:14:50 -07001185 rtc::scoped_refptr<AudioTrackInterface> audio_track(
1186 pc_factory_->CreateAudioTrack(kStreamLabel3,
1187 static_cast<AudioSourceInterface*>(NULL)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001188 stream->AddTrack(audio_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +00001189 EXPECT_TRUE(pc_->AddStream(stream));
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001190 EXPECT_EQ(3u, pc_->local_streams()->count());
1191
1192 // Remove the third stream.
1193 pc_->RemoveStream(pc_->local_streams()->at(2));
1194 EXPECT_EQ(2u, pc_->local_streams()->count());
1195
1196 // Remove the second stream.
1197 pc_->RemoveStream(pc_->local_streams()->at(1));
1198 EXPECT_EQ(1u, pc_->local_streams()->count());
1199
1200 // Remove the first stream.
1201 pc_->RemoveStream(pc_->local_streams()->at(0));
1202 EXPECT_EQ(0u, pc_->local_streams()->count());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001203}
1204
deadbeefab9b2d12015-10-14 11:33:11 -07001205// Test that the created offer includes streams we added.
1206TEST_F(PeerConnectionInterfaceTest, AddedStreamsPresentInOffer) {
1207 CreatePeerConnection();
1208 AddAudioVideoStream(kStreamLabel1, "audio_track", "video_track");
kwibergd1fe2812016-04-27 06:47:29 -07001209 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001210 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07001211
1212 const cricket::ContentInfo* audio_content =
1213 cricket::GetFirstAudioContent(offer->description());
1214 const cricket::AudioContentDescription* audio_desc =
1215 static_cast<const cricket::AudioContentDescription*>(
1216 audio_content->description);
1217 EXPECT_TRUE(
1218 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
1219
1220 const cricket::ContentInfo* video_content =
1221 cricket::GetFirstVideoContent(offer->description());
1222 const cricket::VideoContentDescription* video_desc =
1223 static_cast<const cricket::VideoContentDescription*>(
1224 video_content->description);
1225 EXPECT_TRUE(
1226 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1227
1228 // Add another stream and ensure the offer includes both the old and new
1229 // streams.
1230 AddAudioVideoStream(kStreamLabel2, "audio_track2", "video_track2");
kwiberg2bbff992016-03-16 11:03:04 -07001231 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07001232
1233 audio_content = cricket::GetFirstAudioContent(offer->description());
1234 audio_desc = static_cast<const cricket::AudioContentDescription*>(
1235 audio_content->description);
1236 EXPECT_TRUE(
1237 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
1238 EXPECT_TRUE(
1239 ContainsTrack(audio_desc->streams(), kStreamLabel2, "audio_track2"));
1240
1241 video_content = cricket::GetFirstVideoContent(offer->description());
1242 video_desc = static_cast<const cricket::VideoContentDescription*>(
1243 video_content->description);
1244 EXPECT_TRUE(
1245 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1246 EXPECT_TRUE(
1247 ContainsTrack(video_desc->streams(), kStreamLabel2, "video_track2"));
1248}
1249
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001250TEST_F(PeerConnectionInterfaceTest, RemoveStream) {
1251 CreatePeerConnection();
deadbeefab9b2d12015-10-14 11:33:11 -07001252 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001253 ASSERT_EQ(1u, pc_->local_streams()->count());
1254 pc_->RemoveStream(pc_->local_streams()->at(0));
1255 EXPECT_EQ(0u, pc_->local_streams()->count());
1256}
1257
deadbeefe1f9d832016-01-14 15:35:42 -08001258// Test for AddTrack and RemoveTrack methods.
1259// Tests that the created offer includes tracks we added,
1260// and that the RtpSenders are created correctly.
1261// Also tests that RemoveTrack removes the tracks from subsequent offers.
1262TEST_F(PeerConnectionInterfaceTest, AddTrackRemoveTrack) {
1263 CreatePeerConnection();
1264 // Create a dummy stream, so tracks share a stream label.
zhihuang9763d562016-08-05 11:14:50 -07001265 rtc::scoped_refptr<MediaStreamInterface> stream(
deadbeefe1f9d832016-01-14 15:35:42 -08001266 pc_factory_->CreateLocalMediaStream(kStreamLabel1));
1267 std::vector<MediaStreamInterface*> stream_list;
1268 stream_list.push_back(stream.get());
zhihuang9763d562016-08-05 11:14:50 -07001269 rtc::scoped_refptr<AudioTrackInterface> audio_track(
deadbeefe1f9d832016-01-14 15:35:42 -08001270 pc_factory_->CreateAudioTrack("audio_track", nullptr));
zhihuang9763d562016-08-05 11:14:50 -07001271 rtc::scoped_refptr<VideoTrackInterface> video_track(
1272 pc_factory_->CreateVideoTrack(
1273 "video_track",
1274 pc_factory_->CreateVideoSource(new cricket::FakeVideoCapturer())));
deadbeefe1f9d832016-01-14 15:35:42 -08001275 auto audio_sender = pc_->AddTrack(audio_track, stream_list);
1276 auto video_sender = pc_->AddTrack(video_track, stream_list);
deadbeefa601f5c2016-06-06 14:27:39 -07001277 EXPECT_EQ(1UL, audio_sender->stream_ids().size());
1278 EXPECT_EQ(kStreamLabel1, audio_sender->stream_ids()[0]);
deadbeefe1f9d832016-01-14 15:35:42 -08001279 EXPECT_EQ("audio_track", audio_sender->id());
1280 EXPECT_EQ(audio_track, audio_sender->track());
deadbeefa601f5c2016-06-06 14:27:39 -07001281 EXPECT_EQ(1UL, video_sender->stream_ids().size());
1282 EXPECT_EQ(kStreamLabel1, video_sender->stream_ids()[0]);
deadbeefe1f9d832016-01-14 15:35:42 -08001283 EXPECT_EQ("video_track", video_sender->id());
1284 EXPECT_EQ(video_track, video_sender->track());
1285
1286 // Now create an offer and check for the senders.
kwibergd1fe2812016-04-27 06:47:29 -07001287 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001288 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefe1f9d832016-01-14 15:35:42 -08001289
1290 const cricket::ContentInfo* audio_content =
1291 cricket::GetFirstAudioContent(offer->description());
1292 const cricket::AudioContentDescription* audio_desc =
1293 static_cast<const cricket::AudioContentDescription*>(
1294 audio_content->description);
1295 EXPECT_TRUE(
1296 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
1297
1298 const cricket::ContentInfo* video_content =
1299 cricket::GetFirstVideoContent(offer->description());
1300 const cricket::VideoContentDescription* video_desc =
1301 static_cast<const cricket::VideoContentDescription*>(
1302 video_content->description);
1303 EXPECT_TRUE(
1304 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1305
1306 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
1307
1308 // Now try removing the tracks.
1309 EXPECT_TRUE(pc_->RemoveTrack(audio_sender));
1310 EXPECT_TRUE(pc_->RemoveTrack(video_sender));
1311
1312 // Create a new offer and ensure it doesn't contain the removed senders.
kwiberg2bbff992016-03-16 11:03:04 -07001313 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefe1f9d832016-01-14 15:35:42 -08001314
1315 audio_content = cricket::GetFirstAudioContent(offer->description());
1316 audio_desc = static_cast<const cricket::AudioContentDescription*>(
1317 audio_content->description);
1318 EXPECT_FALSE(
1319 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
1320
1321 video_content = cricket::GetFirstVideoContent(offer->description());
1322 video_desc = static_cast<const cricket::VideoContentDescription*>(
1323 video_content->description);
1324 EXPECT_FALSE(
1325 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1326
1327 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
1328
1329 // Calling RemoveTrack on a sender no longer attached to a PeerConnection
1330 // should return false.
1331 EXPECT_FALSE(pc_->RemoveTrack(audio_sender));
1332 EXPECT_FALSE(pc_->RemoveTrack(video_sender));
1333}
1334
1335// Test creating senders without a stream specified,
1336// expecting a random stream ID to be generated.
1337TEST_F(PeerConnectionInterfaceTest, AddTrackWithoutStream) {
1338 CreatePeerConnection();
1339 // Create a dummy stream, so tracks share a stream label.
zhihuang9763d562016-08-05 11:14:50 -07001340 rtc::scoped_refptr<AudioTrackInterface> audio_track(
deadbeefe1f9d832016-01-14 15:35:42 -08001341 pc_factory_->CreateAudioTrack("audio_track", nullptr));
zhihuang9763d562016-08-05 11:14:50 -07001342 rtc::scoped_refptr<VideoTrackInterface> video_track(
1343 pc_factory_->CreateVideoTrack(
1344 "video_track",
1345 pc_factory_->CreateVideoSource(new cricket::FakeVideoCapturer())));
deadbeefe1f9d832016-01-14 15:35:42 -08001346 auto audio_sender =
1347 pc_->AddTrack(audio_track, std::vector<MediaStreamInterface*>());
1348 auto video_sender =
1349 pc_->AddTrack(video_track, std::vector<MediaStreamInterface*>());
1350 EXPECT_EQ("audio_track", audio_sender->id());
1351 EXPECT_EQ(audio_track, audio_sender->track());
1352 EXPECT_EQ("video_track", video_sender->id());
1353 EXPECT_EQ(video_track, video_sender->track());
1354 // If the ID is truly a random GUID, it should be infinitely unlikely they
1355 // will be the same.
deadbeefa601f5c2016-06-06 14:27:39 -07001356 EXPECT_NE(video_sender->stream_ids(), audio_sender->stream_ids());
deadbeefe1f9d832016-01-14 15:35:42 -08001357}
1358
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001359TEST_F(PeerConnectionInterfaceTest, CreateOfferReceiveAnswer) {
1360 InitiateCall();
1361 WaitAndVerifyOnAddStream(kStreamLabel1);
1362 VerifyRemoteRtpHeaderExtensions();
1363}
1364
1365TEST_F(PeerConnectionInterfaceTest, CreateOfferReceivePrAnswerAndAnswer) {
1366 CreatePeerConnection();
deadbeefab9b2d12015-10-14 11:33:11 -07001367 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001368 CreateOfferAsLocalDescription();
1369 std::string offer;
1370 EXPECT_TRUE(pc_->local_description()->ToString(&offer));
1371 CreatePrAnswerAndAnswerAsRemoteDescription(offer);
1372 WaitAndVerifyOnAddStream(kStreamLabel1);
1373}
1374
1375TEST_F(PeerConnectionInterfaceTest, ReceiveOfferCreateAnswer) {
1376 CreatePeerConnection();
deadbeefab9b2d12015-10-14 11:33:11 -07001377 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001378
1379 CreateOfferAsRemoteDescription();
1380 CreateAnswerAsLocalDescription();
1381
1382 WaitAndVerifyOnAddStream(kStreamLabel1);
1383}
1384
1385TEST_F(PeerConnectionInterfaceTest, ReceiveOfferCreatePrAnswerAndAnswer) {
1386 CreatePeerConnection();
deadbeefab9b2d12015-10-14 11:33:11 -07001387 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001388
1389 CreateOfferAsRemoteDescription();
1390 CreatePrAnswerAsLocalDescription();
1391 CreateAnswerAsLocalDescription();
1392
1393 WaitAndVerifyOnAddStream(kStreamLabel1);
1394}
1395
1396TEST_F(PeerConnectionInterfaceTest, Renegotiate) {
1397 InitiateCall();
1398 ASSERT_EQ(1u, pc_->remote_streams()->count());
1399 pc_->RemoveStream(pc_->local_streams()->at(0));
1400 CreateOfferReceiveAnswer();
1401 EXPECT_EQ(0u, pc_->remote_streams()->count());
deadbeefab9b2d12015-10-14 11:33:11 -07001402 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001403 CreateOfferReceiveAnswer();
1404}
1405
1406// Tests that after negotiating an audio only call, the respondent can perform a
1407// renegotiation that removes the audio stream.
1408TEST_F(PeerConnectionInterfaceTest, RenegotiateAudioOnly) {
1409 CreatePeerConnection();
1410 AddVoiceStream(kStreamLabel1);
1411 CreateOfferAsRemoteDescription();
1412 CreateAnswerAsLocalDescription();
1413
1414 ASSERT_EQ(1u, pc_->remote_streams()->count());
1415 pc_->RemoveStream(pc_->local_streams()->at(0));
1416 CreateOfferReceiveAnswer();
1417 EXPECT_EQ(0u, pc_->remote_streams()->count());
1418}
1419
1420// Test that candidates are generated and that we can parse our own candidates.
1421TEST_F(PeerConnectionInterfaceTest, IceCandidates) {
1422 CreatePeerConnection();
1423
1424 EXPECT_FALSE(pc_->AddIceCandidate(observer_.last_candidate_.get()));
1425 // SetRemoteDescription takes ownership of offer.
kwibergd1fe2812016-04-27 06:47:29 -07001426 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefab9b2d12015-10-14 11:33:11 -07001427 AddVideoStream(kStreamLabel1);
deadbeefc80741f2015-10-22 13:14:45 -07001428 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
kwiberg2bbff992016-03-16 11:03:04 -07001429 EXPECT_TRUE(DoSetRemoteDescription(offer.release()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001430
1431 // SetLocalDescription takes ownership of answer.
kwibergd1fe2812016-04-27 06:47:29 -07001432 std::unique_ptr<SessionDescriptionInterface> answer;
deadbeefc80741f2015-10-22 13:14:45 -07001433 EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
kwiberg2bbff992016-03-16 11:03:04 -07001434 EXPECT_TRUE(DoSetLocalDescription(answer.release()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001435
1436 EXPECT_TRUE_WAIT(observer_.last_candidate_.get() != NULL, kTimeout);
1437 EXPECT_TRUE_WAIT(observer_.ice_complete_, kTimeout);
1438
1439 EXPECT_TRUE(pc_->AddIceCandidate(observer_.last_candidate_.get()));
1440}
1441
deadbeefab9b2d12015-10-14 11:33:11 -07001442// Test that CreateOffer and CreateAnswer will fail if the track labels are
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001443// not unique.
1444TEST_F(PeerConnectionInterfaceTest, CreateOfferAnswerWithInvalidStream) {
1445 CreatePeerConnection();
1446 // Create a regular offer for the CreateAnswer test later.
kwibergd1fe2812016-04-27 06:47:29 -07001447 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefc80741f2015-10-22 13:14:45 -07001448 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
kwiberg2bbff992016-03-16 11:03:04 -07001449 EXPECT_TRUE(offer);
1450 offer.reset();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001451
1452 // Create a local stream with audio&video tracks having same label.
1453 AddAudioVideoStream(kStreamLabel1, "track_label", "track_label");
1454
1455 // Test CreateOffer
deadbeefc80741f2015-10-22 13:14:45 -07001456 EXPECT_FALSE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001457
1458 // Test CreateAnswer
kwibergd1fe2812016-04-27 06:47:29 -07001459 std::unique_ptr<SessionDescriptionInterface> answer;
deadbeefc80741f2015-10-22 13:14:45 -07001460 EXPECT_FALSE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001461}
1462
1463// Test that we will get different SSRCs for each tracks in the offer and answer
1464// we created.
1465TEST_F(PeerConnectionInterfaceTest, SsrcInOfferAnswer) {
1466 CreatePeerConnection();
1467 // Create a local stream with audio&video tracks having different labels.
1468 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
1469
1470 // Test CreateOffer
kwibergd1fe2812016-04-27 06:47:29 -07001471 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001472 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001473 int audio_ssrc = 0;
1474 int video_ssrc = 0;
1475 EXPECT_TRUE(GetFirstSsrc(GetFirstAudioContent(offer->description()),
1476 &audio_ssrc));
1477 EXPECT_TRUE(GetFirstSsrc(GetFirstVideoContent(offer->description()),
1478 &video_ssrc));
1479 EXPECT_NE(audio_ssrc, video_ssrc);
1480
1481 // Test CreateAnswer
1482 EXPECT_TRUE(DoSetRemoteDescription(offer.release()));
kwibergd1fe2812016-04-27 06:47:29 -07001483 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -07001484 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001485 audio_ssrc = 0;
1486 video_ssrc = 0;
1487 EXPECT_TRUE(GetFirstSsrc(GetFirstAudioContent(answer->description()),
1488 &audio_ssrc));
1489 EXPECT_TRUE(GetFirstSsrc(GetFirstVideoContent(answer->description()),
1490 &video_ssrc));
1491 EXPECT_NE(audio_ssrc, video_ssrc);
1492}
1493
deadbeefeb459812015-12-15 19:24:43 -08001494// Test that it's possible to call AddTrack on a MediaStream after adding
1495// the stream to a PeerConnection.
1496// TODO(deadbeef): Remove this test once this behavior is no longer supported.
1497TEST_F(PeerConnectionInterfaceTest, AddTrackAfterAddStream) {
1498 CreatePeerConnection();
1499 // Create audio stream and add to PeerConnection.
1500 AddVoiceStream(kStreamLabel1);
1501 MediaStreamInterface* stream = pc_->local_streams()->at(0);
1502
1503 // Add video track to the audio-only stream.
zhihuang9763d562016-08-05 11:14:50 -07001504 rtc::scoped_refptr<VideoTrackInterface> video_track(
1505 pc_factory_->CreateVideoTrack(
1506 "video_label",
1507 pc_factory_->CreateVideoSource(new cricket::FakeVideoCapturer())));
deadbeefeb459812015-12-15 19:24:43 -08001508 stream->AddTrack(video_track.get());
1509
kwibergd1fe2812016-04-27 06:47:29 -07001510 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001511 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefeb459812015-12-15 19:24:43 -08001512
1513 const cricket::MediaContentDescription* video_desc =
1514 cricket::GetFirstVideoContentDescription(offer->description());
1515 EXPECT_TRUE(video_desc != nullptr);
1516}
1517
1518// Test that it's possible to call RemoveTrack on a MediaStream after adding
1519// the stream to a PeerConnection.
1520// TODO(deadbeef): Remove this test once this behavior is no longer supported.
1521TEST_F(PeerConnectionInterfaceTest, RemoveTrackAfterAddStream) {
1522 CreatePeerConnection();
1523 // Create audio/video stream and add to PeerConnection.
1524 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
1525 MediaStreamInterface* stream = pc_->local_streams()->at(0);
1526
1527 // Remove the video track.
1528 stream->RemoveTrack(stream->GetVideoTracks()[0]);
1529
kwibergd1fe2812016-04-27 06:47:29 -07001530 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001531 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefeb459812015-12-15 19:24:43 -08001532
1533 const cricket::MediaContentDescription* video_desc =
1534 cricket::GetFirstVideoContentDescription(offer->description());
1535 EXPECT_TRUE(video_desc == nullptr);
1536}
1537
deadbeefbd7d8f72015-12-18 16:58:44 -08001538// Test creating a sender with a stream ID, and ensure the ID is populated
1539// in the offer.
1540TEST_F(PeerConnectionInterfaceTest, CreateSenderWithStream) {
1541 CreatePeerConnection();
1542 pc_->CreateSender("video", kStreamLabel1);
1543
kwibergd1fe2812016-04-27 06:47:29 -07001544 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001545 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefbd7d8f72015-12-18 16:58:44 -08001546
1547 const cricket::MediaContentDescription* video_desc =
1548 cricket::GetFirstVideoContentDescription(offer->description());
1549 ASSERT_TRUE(video_desc != nullptr);
1550 ASSERT_EQ(1u, video_desc->streams().size());
1551 EXPECT_EQ(kStreamLabel1, video_desc->streams()[0].sync_label);
1552}
1553
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001554// Test that we can specify a certain track that we want statistics about.
1555TEST_F(PeerConnectionInterfaceTest, GetStatsForSpecificTrack) {
1556 InitiateCall();
1557 ASSERT_LT(0u, pc_->remote_streams()->count());
1558 ASSERT_LT(0u, pc_->remote_streams()->at(0)->GetAudioTracks().size());
zhihuang9763d562016-08-05 11:14:50 -07001559 rtc::scoped_refptr<MediaStreamTrackInterface> remote_audio =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001560 pc_->remote_streams()->at(0)->GetAudioTracks()[0];
1561 EXPECT_TRUE(DoGetStats(remote_audio));
1562
1563 // Remove the stream. Since we are sending to our selves the local
1564 // and the remote stream is the same.
1565 pc_->RemoveStream(pc_->local_streams()->at(0));
1566 // Do a re-negotiation.
1567 CreateOfferReceiveAnswer();
1568
1569 ASSERT_EQ(0u, pc_->remote_streams()->count());
1570
1571 // Test that we still can get statistics for the old track. Even if it is not
1572 // sent any longer.
1573 EXPECT_TRUE(DoGetStats(remote_audio));
1574}
1575
1576// Test that we can get stats on a video track.
1577TEST_F(PeerConnectionInterfaceTest, GetStatsForVideoTrack) {
1578 InitiateCall();
1579 ASSERT_LT(0u, pc_->remote_streams()->count());
1580 ASSERT_LT(0u, pc_->remote_streams()->at(0)->GetVideoTracks().size());
zhihuang9763d562016-08-05 11:14:50 -07001581 rtc::scoped_refptr<MediaStreamTrackInterface> remote_video =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001582 pc_->remote_streams()->at(0)->GetVideoTracks()[0];
1583 EXPECT_TRUE(DoGetStats(remote_video));
1584}
1585
1586// Test that we don't get statistics for an invalid track.
zhihuange9e94c32016-11-04 11:38:15 -07001587TEST_F(PeerConnectionInterfaceTest, GetStatsForInvalidTrack) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001588 InitiateCall();
zhihuang9763d562016-08-05 11:14:50 -07001589 rtc::scoped_refptr<AudioTrackInterface> unknown_audio_track(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001590 pc_factory_->CreateAudioTrack("unknown track", NULL));
1591 EXPECT_FALSE(DoGetStats(unknown_audio_track));
1592}
1593
1594// This test setup two RTP data channels in loop back.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001595TEST_F(PeerConnectionInterfaceTest, TestDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001596 FakeConstraints constraints;
1597 constraints.SetAllowRtpDataChannels();
1598 CreatePeerConnection(&constraints);
zhihuang9763d562016-08-05 11:14:50 -07001599 rtc::scoped_refptr<DataChannelInterface> data1 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001600 pc_->CreateDataChannel("test1", NULL);
zhihuang9763d562016-08-05 11:14:50 -07001601 rtc::scoped_refptr<DataChannelInterface> data2 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001602 pc_->CreateDataChannel("test2", NULL);
1603 ASSERT_TRUE(data1 != NULL);
kwibergd1fe2812016-04-27 06:47:29 -07001604 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001605 new MockDataChannelObserver(data1));
kwibergd1fe2812016-04-27 06:47:29 -07001606 std::unique_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001607 new MockDataChannelObserver(data2));
1608
1609 EXPECT_EQ(DataChannelInterface::kConnecting, data1->state());
1610 EXPECT_EQ(DataChannelInterface::kConnecting, data2->state());
1611 std::string data_to_send1 = "testing testing";
1612 std::string data_to_send2 = "testing something else";
1613 EXPECT_FALSE(data1->Send(DataBuffer(data_to_send1)));
1614
1615 CreateOfferReceiveAnswer();
1616 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1617 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
1618
1619 EXPECT_EQ(DataChannelInterface::kOpen, data1->state());
1620 EXPECT_EQ(DataChannelInterface::kOpen, data2->state());
1621 EXPECT_TRUE(data1->Send(DataBuffer(data_to_send1)));
1622 EXPECT_TRUE(data2->Send(DataBuffer(data_to_send2)));
1623
1624 EXPECT_EQ_WAIT(data_to_send1, observer1->last_message(), kTimeout);
1625 EXPECT_EQ_WAIT(data_to_send2, observer2->last_message(), kTimeout);
1626
1627 data1->Close();
1628 EXPECT_EQ(DataChannelInterface::kClosing, data1->state());
1629 CreateOfferReceiveAnswer();
1630 EXPECT_FALSE(observer1->IsOpen());
1631 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
1632 EXPECT_TRUE(observer2->IsOpen());
1633
1634 data_to_send2 = "testing something else again";
1635 EXPECT_TRUE(data2->Send(DataBuffer(data_to_send2)));
1636
1637 EXPECT_EQ_WAIT(data_to_send2, observer2->last_message(), kTimeout);
1638}
1639
1640// This test verifies that sendnig binary data over RTP data channels should
1641// fail.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001642TEST_F(PeerConnectionInterfaceTest, TestSendBinaryOnRtpDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001643 FakeConstraints constraints;
1644 constraints.SetAllowRtpDataChannels();
1645 CreatePeerConnection(&constraints);
zhihuang9763d562016-08-05 11:14:50 -07001646 rtc::scoped_refptr<DataChannelInterface> data1 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001647 pc_->CreateDataChannel("test1", NULL);
zhihuang9763d562016-08-05 11:14:50 -07001648 rtc::scoped_refptr<DataChannelInterface> data2 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001649 pc_->CreateDataChannel("test2", NULL);
1650 ASSERT_TRUE(data1 != NULL);
kwibergd1fe2812016-04-27 06:47:29 -07001651 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001652 new MockDataChannelObserver(data1));
kwibergd1fe2812016-04-27 06:47:29 -07001653 std::unique_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001654 new MockDataChannelObserver(data2));
1655
1656 EXPECT_EQ(DataChannelInterface::kConnecting, data1->state());
1657 EXPECT_EQ(DataChannelInterface::kConnecting, data2->state());
1658
1659 CreateOfferReceiveAnswer();
1660 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1661 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
1662
1663 EXPECT_EQ(DataChannelInterface::kOpen, data1->state());
1664 EXPECT_EQ(DataChannelInterface::kOpen, data2->state());
1665
jbaucheec21bd2016-03-20 06:15:43 -07001666 rtc::CopyOnWriteBuffer buffer("test", 4);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001667 EXPECT_FALSE(data1->Send(DataBuffer(buffer, true)));
1668}
1669
1670// This test setup a RTP data channels in loop back and test that a channel is
1671// opened even if the remote end answer with a zero SSRC.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001672TEST_F(PeerConnectionInterfaceTest, TestSendOnlyDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001673 FakeConstraints constraints;
1674 constraints.SetAllowRtpDataChannels();
1675 CreatePeerConnection(&constraints);
zhihuang9763d562016-08-05 11:14:50 -07001676 rtc::scoped_refptr<DataChannelInterface> data1 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001677 pc_->CreateDataChannel("test1", NULL);
kwibergd1fe2812016-04-27 06:47:29 -07001678 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001679 new MockDataChannelObserver(data1));
1680
1681 CreateOfferReceiveAnswerWithoutSsrc();
1682
1683 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1684
1685 data1->Close();
1686 EXPECT_EQ(DataChannelInterface::kClosing, data1->state());
1687 CreateOfferReceiveAnswerWithoutSsrc();
1688 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
1689 EXPECT_FALSE(observer1->IsOpen());
1690}
1691
1692// This test that if a data channel is added in an answer a receive only channel
1693// channel is created.
1694TEST_F(PeerConnectionInterfaceTest, TestReceiveOnlyDataChannel) {
1695 FakeConstraints constraints;
1696 constraints.SetAllowRtpDataChannels();
1697 CreatePeerConnection(&constraints);
1698
1699 std::string offer_label = "offer_channel";
zhihuang9763d562016-08-05 11:14:50 -07001700 rtc::scoped_refptr<DataChannelInterface> offer_channel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001701 pc_->CreateDataChannel(offer_label, NULL);
1702
1703 CreateOfferAsLocalDescription();
1704
1705 // Replace the data channel label in the offer and apply it as an answer.
1706 std::string receive_label = "answer_channel";
1707 std::string sdp;
1708 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001709 rtc::replace_substrs(offer_label.c_str(), offer_label.length(),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001710 receive_label.c_str(), receive_label.length(),
1711 &sdp);
1712 CreateAnswerAsRemoteDescription(sdp);
1713
1714 // Verify that a new incoming data channel has been created and that
1715 // it is open but can't we written to.
1716 ASSERT_TRUE(observer_.last_datachannel_ != NULL);
1717 DataChannelInterface* received_channel = observer_.last_datachannel_;
1718 EXPECT_EQ(DataChannelInterface::kConnecting, received_channel->state());
1719 EXPECT_EQ(receive_label, received_channel->label());
1720 EXPECT_FALSE(received_channel->Send(DataBuffer("something")));
1721
1722 // Verify that the channel we initially offered has been rejected.
1723 EXPECT_EQ(DataChannelInterface::kClosed, offer_channel->state());
1724
1725 // Do another offer / answer exchange and verify that the data channel is
1726 // opened.
1727 CreateOfferReceiveAnswer();
1728 EXPECT_EQ_WAIT(DataChannelInterface::kOpen, received_channel->state(),
1729 kTimeout);
1730}
1731
1732// This test that no data channel is returned if a reliable channel is
1733// requested.
1734// TODO(perkj): Remove this test once reliable channels are implemented.
1735TEST_F(PeerConnectionInterfaceTest, CreateReliableRtpDataChannelShouldFail) {
1736 FakeConstraints constraints;
1737 constraints.SetAllowRtpDataChannels();
1738 CreatePeerConnection(&constraints);
1739
1740 std::string label = "test";
1741 webrtc::DataChannelInit config;
1742 config.reliable = true;
zhihuang9763d562016-08-05 11:14:50 -07001743 rtc::scoped_refptr<DataChannelInterface> channel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001744 pc_->CreateDataChannel(label, &config);
1745 EXPECT_TRUE(channel == NULL);
1746}
1747
deadbeefab9b2d12015-10-14 11:33:11 -07001748// Verifies that duplicated label is not allowed for RTP data channel.
1749TEST_F(PeerConnectionInterfaceTest, RtpDuplicatedLabelNotAllowed) {
1750 FakeConstraints constraints;
1751 constraints.SetAllowRtpDataChannels();
1752 CreatePeerConnection(&constraints);
1753
1754 std::string label = "test";
zhihuang9763d562016-08-05 11:14:50 -07001755 rtc::scoped_refptr<DataChannelInterface> channel =
deadbeefab9b2d12015-10-14 11:33:11 -07001756 pc_->CreateDataChannel(label, nullptr);
1757 EXPECT_NE(channel, nullptr);
1758
zhihuang9763d562016-08-05 11:14:50 -07001759 rtc::scoped_refptr<DataChannelInterface> dup_channel =
deadbeefab9b2d12015-10-14 11:33:11 -07001760 pc_->CreateDataChannel(label, nullptr);
1761 EXPECT_EQ(dup_channel, nullptr);
1762}
1763
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001764// This tests that a SCTP data channel is returned using different
1765// DataChannelInit configurations.
1766TEST_F(PeerConnectionInterfaceTest, CreateSctpDataChannel) {
1767 FakeConstraints constraints;
1768 constraints.SetAllowDtlsSctpDataChannels();
1769 CreatePeerConnection(&constraints);
1770
1771 webrtc::DataChannelInit config;
1772
zhihuang9763d562016-08-05 11:14:50 -07001773 rtc::scoped_refptr<DataChannelInterface> channel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001774 pc_->CreateDataChannel("1", &config);
1775 EXPECT_TRUE(channel != NULL);
1776 EXPECT_TRUE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001777 EXPECT_TRUE(observer_.renegotiation_needed_);
1778 observer_.renegotiation_needed_ = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001779
1780 config.ordered = false;
1781 channel = pc_->CreateDataChannel("2", &config);
1782 EXPECT_TRUE(channel != NULL);
1783 EXPECT_TRUE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001784 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001785
1786 config.ordered = true;
1787 config.maxRetransmits = 0;
1788 channel = pc_->CreateDataChannel("3", &config);
1789 EXPECT_TRUE(channel != NULL);
1790 EXPECT_FALSE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001791 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001792
1793 config.maxRetransmits = -1;
1794 config.maxRetransmitTime = 0;
1795 channel = pc_->CreateDataChannel("4", &config);
1796 EXPECT_TRUE(channel != NULL);
1797 EXPECT_FALSE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001798 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001799}
1800
1801// This tests that no data channel is returned if both maxRetransmits and
1802// maxRetransmitTime are set for SCTP data channels.
1803TEST_F(PeerConnectionInterfaceTest,
1804 CreateSctpDataChannelShouldFailForInvalidConfig) {
1805 FakeConstraints constraints;
1806 constraints.SetAllowDtlsSctpDataChannels();
1807 CreatePeerConnection(&constraints);
1808
1809 std::string label = "test";
1810 webrtc::DataChannelInit config;
1811 config.maxRetransmits = 0;
1812 config.maxRetransmitTime = 0;
1813
zhihuang9763d562016-08-05 11:14:50 -07001814 rtc::scoped_refptr<DataChannelInterface> channel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001815 pc_->CreateDataChannel(label, &config);
1816 EXPECT_TRUE(channel == NULL);
1817}
1818
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001819// The test verifies that creating a SCTP data channel with an id already in use
1820// or out of range should fail.
1821TEST_F(PeerConnectionInterfaceTest,
1822 CreateSctpDataChannelWithInvalidIdShouldFail) {
1823 FakeConstraints constraints;
1824 constraints.SetAllowDtlsSctpDataChannels();
1825 CreatePeerConnection(&constraints);
1826
1827 webrtc::DataChannelInit config;
zhihuang9763d562016-08-05 11:14:50 -07001828 rtc::scoped_refptr<DataChannelInterface> channel;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001829
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001830 config.id = 1;
1831 channel = pc_->CreateDataChannel("1", &config);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001832 EXPECT_TRUE(channel != NULL);
1833 EXPECT_EQ(1, channel->id());
1834
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001835 channel = pc_->CreateDataChannel("x", &config);
1836 EXPECT_TRUE(channel == NULL);
1837
1838 config.id = cricket::kMaxSctpSid;
1839 channel = pc_->CreateDataChannel("max", &config);
1840 EXPECT_TRUE(channel != NULL);
1841 EXPECT_EQ(config.id, channel->id());
1842
1843 config.id = cricket::kMaxSctpSid + 1;
1844 channel = pc_->CreateDataChannel("x", &config);
1845 EXPECT_TRUE(channel == NULL);
1846}
1847
deadbeefab9b2d12015-10-14 11:33:11 -07001848// Verifies that duplicated label is allowed for SCTP data channel.
1849TEST_F(PeerConnectionInterfaceTest, SctpDuplicatedLabelAllowed) {
1850 FakeConstraints constraints;
1851 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
1852 true);
1853 CreatePeerConnection(&constraints);
1854
1855 std::string label = "test";
zhihuang9763d562016-08-05 11:14:50 -07001856 rtc::scoped_refptr<DataChannelInterface> channel =
deadbeefab9b2d12015-10-14 11:33:11 -07001857 pc_->CreateDataChannel(label, nullptr);
1858 EXPECT_NE(channel, nullptr);
1859
zhihuang9763d562016-08-05 11:14:50 -07001860 rtc::scoped_refptr<DataChannelInterface> dup_channel =
deadbeefab9b2d12015-10-14 11:33:11 -07001861 pc_->CreateDataChannel(label, nullptr);
1862 EXPECT_NE(dup_channel, nullptr);
1863}
1864
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001865// This test verifies that OnRenegotiationNeeded is fired for every new RTP
1866// DataChannel.
1867TEST_F(PeerConnectionInterfaceTest, RenegotiationNeededForNewRtpDataChannel) {
1868 FakeConstraints constraints;
1869 constraints.SetAllowRtpDataChannels();
1870 CreatePeerConnection(&constraints);
1871
zhihuang9763d562016-08-05 11:14:50 -07001872 rtc::scoped_refptr<DataChannelInterface> dc1 =
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001873 pc_->CreateDataChannel("test1", NULL);
1874 EXPECT_TRUE(observer_.renegotiation_needed_);
1875 observer_.renegotiation_needed_ = false;
1876
zhihuang9763d562016-08-05 11:14:50 -07001877 rtc::scoped_refptr<DataChannelInterface> dc2 =
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001878 pc_->CreateDataChannel("test2", NULL);
1879 EXPECT_TRUE(observer_.renegotiation_needed_);
1880}
1881
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001882// This test that a data channel closes when a PeerConnection is deleted/closed.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001883TEST_F(PeerConnectionInterfaceTest, DataChannelCloseWhenPeerConnectionClose) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001884 FakeConstraints constraints;
1885 constraints.SetAllowRtpDataChannels();
1886 CreatePeerConnection(&constraints);
1887
zhihuang9763d562016-08-05 11:14:50 -07001888 rtc::scoped_refptr<DataChannelInterface> data1 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001889 pc_->CreateDataChannel("test1", NULL);
zhihuang9763d562016-08-05 11:14:50 -07001890 rtc::scoped_refptr<DataChannelInterface> data2 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001891 pc_->CreateDataChannel("test2", NULL);
1892 ASSERT_TRUE(data1 != NULL);
kwibergd1fe2812016-04-27 06:47:29 -07001893 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001894 new MockDataChannelObserver(data1));
kwibergd1fe2812016-04-27 06:47:29 -07001895 std::unique_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001896 new MockDataChannelObserver(data2));
1897
1898 CreateOfferReceiveAnswer();
1899 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1900 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
1901
1902 ReleasePeerConnection();
1903 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
1904 EXPECT_EQ(DataChannelInterface::kClosed, data2->state());
1905}
1906
1907// This test that data channels can be rejected in an answer.
1908TEST_F(PeerConnectionInterfaceTest, TestRejectDataChannelInAnswer) {
1909 FakeConstraints constraints;
1910 constraints.SetAllowRtpDataChannels();
1911 CreatePeerConnection(&constraints);
1912
zhihuang9763d562016-08-05 11:14:50 -07001913 rtc::scoped_refptr<DataChannelInterface> offer_channel(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001914 pc_->CreateDataChannel("offer_channel", NULL));
1915
1916 CreateOfferAsLocalDescription();
1917
1918 // Create an answer where the m-line for data channels are rejected.
1919 std::string sdp;
1920 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
1921 webrtc::JsepSessionDescription* answer = new webrtc::JsepSessionDescription(
1922 SessionDescriptionInterface::kAnswer);
1923 EXPECT_TRUE(answer->Initialize(sdp, NULL));
1924 cricket::ContentInfo* data_info =
1925 answer->description()->GetContentByName("data");
1926 data_info->rejected = true;
1927
1928 DoSetRemoteDescription(answer);
1929 EXPECT_EQ(DataChannelInterface::kClosed, offer_channel->state());
1930}
1931
1932// Test that we can create a session description from an SDP string from
1933// FireFox, use it as a remote session description, generate an answer and use
1934// the answer as a local description.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07001935TEST_F(PeerConnectionInterfaceTest, ReceiveFireFoxOffer) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001936 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001937 FakeConstraints constraints;
1938 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
1939 true);
1940 CreatePeerConnection(&constraints);
1941 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
1942 SessionDescriptionInterface* desc =
1943 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
jbauchfabe2c92015-07-16 13:43:14 -07001944 webrtc::kFireFoxSdpOffer, nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001945 EXPECT_TRUE(DoSetSessionDescription(desc, false));
1946 CreateAnswerAsLocalDescription();
1947 ASSERT_TRUE(pc_->local_description() != NULL);
1948 ASSERT_TRUE(pc_->remote_description() != NULL);
1949
1950 const cricket::ContentInfo* content =
1951 cricket::GetFirstAudioContent(pc_->local_description()->description());
1952 ASSERT_TRUE(content != NULL);
1953 EXPECT_FALSE(content->rejected);
1954
1955 content =
1956 cricket::GetFirstVideoContent(pc_->local_description()->description());
1957 ASSERT_TRUE(content != NULL);
1958 EXPECT_FALSE(content->rejected);
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00001959#ifdef HAVE_SCTP
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001960 content =
1961 cricket::GetFirstDataContent(pc_->local_description()->description());
1962 ASSERT_TRUE(content != NULL);
1963 EXPECT_TRUE(content->rejected);
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00001964#endif
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001965}
1966
1967// Test that we can create an audio only offer and receive an answer with a
1968// limited set of audio codecs and receive an updated offer with more audio
1969// codecs, where the added codecs are not supported.
1970TEST_F(PeerConnectionInterfaceTest, ReceiveUpdatedAudioOfferWithBadCodecs) {
1971 CreatePeerConnection();
1972 AddVoiceStream("audio_label");
1973 CreateOfferAsLocalDescription();
1974
1975 SessionDescriptionInterface* answer =
1976 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
jbauchfabe2c92015-07-16 13:43:14 -07001977 webrtc::kAudioSdp, nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001978 EXPECT_TRUE(DoSetSessionDescription(answer, false));
1979
1980 SessionDescriptionInterface* updated_offer =
1981 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
jbauchfabe2c92015-07-16 13:43:14 -07001982 webrtc::kAudioSdpWithUnsupportedCodecs,
1983 nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001984 EXPECT_TRUE(DoSetSessionDescription(updated_offer, false));
1985 CreateAnswerAsLocalDescription();
1986}
1987
deadbeefc80741f2015-10-22 13:14:45 -07001988// Test that if we're receiving (but not sending) a track, subsequent offers
1989// will have m-lines with a=recvonly.
1990TEST_F(PeerConnectionInterfaceTest, CreateSubsequentRecvOnlyOffer) {
1991 FakeConstraints constraints;
1992 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
1993 true);
1994 CreatePeerConnection(&constraints);
1995 CreateAndSetRemoteOffer(kSdpStringWithStream1);
1996 CreateAnswerAsLocalDescription();
1997
1998 // At this point we should be receiving stream 1, but not sending anything.
1999 // A new offer should be recvonly.
kwibergd1fe2812016-04-27 06:47:29 -07002000 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefc80741f2015-10-22 13:14:45 -07002001 DoCreateOffer(&offer, nullptr);
2002
2003 const cricket::ContentInfo* video_content =
2004 cricket::GetFirstVideoContent(offer->description());
2005 const cricket::VideoContentDescription* video_desc =
2006 static_cast<const cricket::VideoContentDescription*>(
2007 video_content->description);
2008 ASSERT_EQ(cricket::MD_RECVONLY, video_desc->direction());
2009
2010 const cricket::ContentInfo* audio_content =
2011 cricket::GetFirstAudioContent(offer->description());
2012 const cricket::AudioContentDescription* audio_desc =
2013 static_cast<const cricket::AudioContentDescription*>(
2014 audio_content->description);
2015 ASSERT_EQ(cricket::MD_RECVONLY, audio_desc->direction());
2016}
2017
2018// Test that if we're receiving (but not sending) a track, and the
2019// offerToReceiveVideo/offerToReceiveAudio constraints are explicitly set to
2020// false, the generated m-lines will be a=inactive.
2021TEST_F(PeerConnectionInterfaceTest, CreateSubsequentInactiveOffer) {
2022 FakeConstraints constraints;
2023 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2024 true);
2025 CreatePeerConnection(&constraints);
2026 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2027 CreateAnswerAsLocalDescription();
2028
2029 // At this point we should be receiving stream 1, but not sending anything.
2030 // A new offer would be recvonly, but we'll set the "no receive" constraints
2031 // to make it inactive.
kwibergd1fe2812016-04-27 06:47:29 -07002032 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefc80741f2015-10-22 13:14:45 -07002033 FakeConstraints offer_constraints;
2034 offer_constraints.AddMandatory(
2035 webrtc::MediaConstraintsInterface::kOfferToReceiveVideo, false);
2036 offer_constraints.AddMandatory(
2037 webrtc::MediaConstraintsInterface::kOfferToReceiveAudio, false);
2038 DoCreateOffer(&offer, &offer_constraints);
2039
2040 const cricket::ContentInfo* video_content =
2041 cricket::GetFirstVideoContent(offer->description());
2042 const cricket::VideoContentDescription* video_desc =
2043 static_cast<const cricket::VideoContentDescription*>(
2044 video_content->description);
2045 ASSERT_EQ(cricket::MD_INACTIVE, video_desc->direction());
2046
2047 const cricket::ContentInfo* audio_content =
2048 cricket::GetFirstAudioContent(offer->description());
2049 const cricket::AudioContentDescription* audio_desc =
2050 static_cast<const cricket::AudioContentDescription*>(
2051 audio_content->description);
2052 ASSERT_EQ(cricket::MD_INACTIVE, audio_desc->direction());
2053}
2054
deadbeef653b8e02015-11-11 12:55:10 -08002055// Test that we can use SetConfiguration to change the ICE servers of the
2056// PortAllocator.
2057TEST_F(PeerConnectionInterfaceTest, SetConfigurationChangesIceServers) {
2058 CreatePeerConnection();
2059
2060 PeerConnectionInterface::RTCConfiguration config;
2061 PeerConnectionInterface::IceServer server;
2062 server.uri = "stun:test_hostname";
2063 config.servers.push_back(server);
2064 EXPECT_TRUE(pc_->SetConfiguration(config));
2065
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002066 EXPECT_EQ(1u, port_allocator_->stun_servers().size());
2067 EXPECT_EQ("test_hostname",
2068 port_allocator_->stun_servers().begin()->hostname());
deadbeef653b8e02015-11-11 12:55:10 -08002069}
2070
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002071TEST_F(PeerConnectionInterfaceTest, SetConfigurationChangesCandidateFilter) {
2072 CreatePeerConnection();
2073 PeerConnectionInterface::RTCConfiguration config;
2074 config.type = PeerConnectionInterface::kRelay;
2075 EXPECT_TRUE(pc_->SetConfiguration(config));
2076 EXPECT_EQ(cricket::CF_RELAY, port_allocator_->candidate_filter());
2077}
2078
2079// Test that when SetConfiguration changes both the pool size and other
2080// attributes, the pooled session is created with the updated attributes.
2081TEST_F(PeerConnectionInterfaceTest,
2082 SetConfigurationCreatesPooledSessionCorrectly) {
2083 CreatePeerConnection();
2084 PeerConnectionInterface::RTCConfiguration config;
2085 config.ice_candidate_pool_size = 1;
2086 PeerConnectionInterface::IceServer server;
2087 server.uri = kStunAddressOnly;
2088 config.servers.push_back(server);
2089 config.type = PeerConnectionInterface::kRelay;
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07002090 EXPECT_TRUE(pc_->SetConfiguration(config));
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002091
2092 const cricket::FakePortAllocatorSession* session =
2093 static_cast<const cricket::FakePortAllocatorSession*>(
2094 port_allocator_->GetPooledSession());
2095 ASSERT_NE(nullptr, session);
2096 EXPECT_EQ(1UL, session->stun_servers().size());
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002097}
2098
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002099// Test that PeerConnection::Close changes the states to closed and all remote
2100// tracks change state to ended.
2101TEST_F(PeerConnectionInterfaceTest, CloseAndTestStreamsAndStates) {
2102 // Initialize a PeerConnection and negotiate local and remote session
2103 // description.
2104 InitiateCall();
2105 ASSERT_EQ(1u, pc_->local_streams()->count());
2106 ASSERT_EQ(1u, pc_->remote_streams()->count());
2107
2108 pc_->Close();
2109
2110 EXPECT_EQ(PeerConnectionInterface::kClosed, pc_->signaling_state());
2111 EXPECT_EQ(PeerConnectionInterface::kIceConnectionClosed,
2112 pc_->ice_connection_state());
2113 EXPECT_EQ(PeerConnectionInterface::kIceGatheringComplete,
2114 pc_->ice_gathering_state());
2115
2116 EXPECT_EQ(1u, pc_->local_streams()->count());
2117 EXPECT_EQ(1u, pc_->remote_streams()->count());
2118
zhihuang9763d562016-08-05 11:14:50 -07002119 rtc::scoped_refptr<MediaStreamInterface> remote_stream =
2120 pc_->remote_streams()->at(0);
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002121 // Track state may be updated asynchronously.
perkjd61bf802016-03-24 03:16:19 -07002122 EXPECT_EQ_WAIT(MediaStreamTrackInterface::kEnded,
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002123 remote_stream->GetAudioTracks()[0]->state(), kTimeout);
2124 EXPECT_EQ_WAIT(MediaStreamTrackInterface::kEnded,
2125 remote_stream->GetVideoTracks()[0]->state(), kTimeout);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002126}
2127
2128// Test that PeerConnection methods fails gracefully after
2129// PeerConnection::Close has been called.
2130TEST_F(PeerConnectionInterfaceTest, CloseAndTestMethods) {
2131 CreatePeerConnection();
2132 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
2133 CreateOfferAsRemoteDescription();
2134 CreateAnswerAsLocalDescription();
2135
2136 ASSERT_EQ(1u, pc_->local_streams()->count());
zhihuang9763d562016-08-05 11:14:50 -07002137 rtc::scoped_refptr<MediaStreamInterface> local_stream =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002138 pc_->local_streams()->at(0);
2139
2140 pc_->Close();
2141
2142 pc_->RemoveStream(local_stream);
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +00002143 EXPECT_FALSE(pc_->AddStream(local_stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002144
2145 ASSERT_FALSE(local_stream->GetAudioTracks().empty());
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002146 rtc::scoped_refptr<webrtc::DtmfSenderInterface> dtmf_sender(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002147 pc_->CreateDtmfSender(local_stream->GetAudioTracks()[0]));
wu@webrtc.org66037362013-08-13 00:09:35 +00002148 EXPECT_TRUE(NULL == dtmf_sender); // local stream has been removed.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002149
2150 EXPECT_TRUE(pc_->CreateDataChannel("test", NULL) == NULL);
2151
2152 EXPECT_TRUE(pc_->local_description() != NULL);
2153 EXPECT_TRUE(pc_->remote_description() != NULL);
2154
kwibergd1fe2812016-04-27 06:47:29 -07002155 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07002156 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
kwibergd1fe2812016-04-27 06:47:29 -07002157 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -07002158 EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002159
2160 std::string sdp;
2161 ASSERT_TRUE(pc_->remote_description()->ToString(&sdp));
2162 SessionDescriptionInterface* remote_offer =
2163 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
2164 sdp, NULL);
2165 EXPECT_FALSE(DoSetRemoteDescription(remote_offer));
2166
2167 ASSERT_TRUE(pc_->local_description()->ToString(&sdp));
2168 SessionDescriptionInterface* local_offer =
2169 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
2170 sdp, NULL);
2171 EXPECT_FALSE(DoSetLocalDescription(local_offer));
2172}
2173
2174// Test that GetStats can still be called after PeerConnection::Close.
2175TEST_F(PeerConnectionInterfaceTest, CloseAndGetStats) {
2176 InitiateCall();
2177 pc_->Close();
2178 DoGetStats(NULL);
2179}
deadbeefab9b2d12015-10-14 11:33:11 -07002180
2181// NOTE: The series of tests below come from what used to be
2182// mediastreamsignaling_unittest.cc, and are mostly aimed at testing that
2183// setting a remote or local description has the expected effects.
2184
2185// This test verifies that the remote MediaStreams corresponding to a received
2186// SDP string is created. In this test the two separate MediaStreams are
2187// signaled.
2188TEST_F(PeerConnectionInterfaceTest, UpdateRemoteStreams) {
2189 FakeConstraints constraints;
2190 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2191 true);
2192 CreatePeerConnection(&constraints);
2193 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2194
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002195 rtc::scoped_refptr<StreamCollection> reference(CreateStreamCollection(1, 1));
deadbeefab9b2d12015-10-14 11:33:11 -07002196 EXPECT_TRUE(
2197 CompareStreamCollections(observer_.remote_streams(), reference.get()));
2198 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2199 EXPECT_TRUE(remote_stream->GetVideoTracks()[0]->GetSource() != nullptr);
2200
2201 // Create a session description based on another SDP with another
2202 // MediaStream.
2203 CreateAndSetRemoteOffer(kSdpStringWithStream1And2);
2204
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002205 rtc::scoped_refptr<StreamCollection> reference2(CreateStreamCollection(2, 1));
deadbeefab9b2d12015-10-14 11:33:11 -07002206 EXPECT_TRUE(
2207 CompareStreamCollections(observer_.remote_streams(), reference2.get()));
2208}
2209
2210// This test verifies that when remote tracks are added/removed from SDP, the
2211// created remote streams are updated appropriately.
2212TEST_F(PeerConnectionInterfaceTest,
2213 AddRemoveTrackFromExistingRemoteMediaStream) {
2214 FakeConstraints constraints;
2215 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2216 true);
2217 CreatePeerConnection(&constraints);
kwibergd1fe2812016-04-27 06:47:29 -07002218 std::unique_ptr<SessionDescriptionInterface> desc_ms1 =
kwiberg2bbff992016-03-16 11:03:04 -07002219 CreateSessionDescriptionAndReference(1, 1);
deadbeefab9b2d12015-10-14 11:33:11 -07002220 EXPECT_TRUE(DoSetRemoteDescription(desc_ms1.release()));
2221 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
2222 reference_collection_));
2223
2224 // Add extra audio and video tracks to the same MediaStream.
kwibergd1fe2812016-04-27 06:47:29 -07002225 std::unique_ptr<SessionDescriptionInterface> desc_ms1_two_tracks =
kwiberg2bbff992016-03-16 11:03:04 -07002226 CreateSessionDescriptionAndReference(2, 2);
deadbeefab9b2d12015-10-14 11:33:11 -07002227 EXPECT_TRUE(DoSetRemoteDescription(desc_ms1_two_tracks.release()));
2228 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
2229 reference_collection_));
zhihuang9763d562016-08-05 11:14:50 -07002230 rtc::scoped_refptr<AudioTrackInterface> audio_track2 =
perkjd61bf802016-03-24 03:16:19 -07002231 observer_.remote_streams()->at(0)->GetAudioTracks()[1];
2232 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, audio_track2->state());
zhihuang9763d562016-08-05 11:14:50 -07002233 rtc::scoped_refptr<VideoTrackInterface> video_track2 =
perkjd61bf802016-03-24 03:16:19 -07002234 observer_.remote_streams()->at(0)->GetVideoTracks()[1];
2235 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, video_track2->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002236
2237 // Remove the extra audio and video tracks.
kwibergd1fe2812016-04-27 06:47:29 -07002238 std::unique_ptr<SessionDescriptionInterface> desc_ms2 =
kwiberg2bbff992016-03-16 11:03:04 -07002239 CreateSessionDescriptionAndReference(1, 1);
perkjd61bf802016-03-24 03:16:19 -07002240 MockTrackObserver audio_track_observer(audio_track2);
2241 MockTrackObserver video_track_observer(video_track2);
2242
2243 EXPECT_CALL(audio_track_observer, OnChanged()).Times(Exactly(1));
2244 EXPECT_CALL(video_track_observer, OnChanged()).Times(Exactly(1));
deadbeefab9b2d12015-10-14 11:33:11 -07002245 EXPECT_TRUE(DoSetRemoteDescription(desc_ms2.release()));
2246 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
2247 reference_collection_));
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002248 // Track state may be updated asynchronously.
perkjd61bf802016-03-24 03:16:19 -07002249 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002250 audio_track2->state(), kTimeout);
2251 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
2252 video_track2->state(), kTimeout);
deadbeefab9b2d12015-10-14 11:33:11 -07002253}
2254
2255// This tests that remote tracks are ended if a local session description is set
2256// that rejects the media content type.
2257TEST_F(PeerConnectionInterfaceTest, RejectMediaContent) {
2258 FakeConstraints constraints;
2259 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2260 true);
2261 CreatePeerConnection(&constraints);
2262 // First create and set a remote offer, then reject its video content in our
2263 // answer.
2264 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2265 ASSERT_EQ(1u, observer_.remote_streams()->count());
2266 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2267 ASSERT_EQ(1u, remote_stream->GetVideoTracks().size());
2268 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2269
2270 rtc::scoped_refptr<webrtc::VideoTrackInterface> remote_video =
2271 remote_stream->GetVideoTracks()[0];
2272 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, remote_video->state());
2273 rtc::scoped_refptr<webrtc::AudioTrackInterface> remote_audio =
2274 remote_stream->GetAudioTracks()[0];
2275 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, remote_audio->state());
2276
kwibergd1fe2812016-04-27 06:47:29 -07002277 std::unique_ptr<SessionDescriptionInterface> local_answer;
kwiberg2bbff992016-03-16 11:03:04 -07002278 EXPECT_TRUE(DoCreateAnswer(&local_answer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07002279 cricket::ContentInfo* video_info =
2280 local_answer->description()->GetContentByName("video");
2281 video_info->rejected = true;
2282 EXPECT_TRUE(DoSetLocalDescription(local_answer.release()));
2283 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kEnded, remote_video->state());
2284 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, remote_audio->state());
2285
2286 // Now create an offer where we reject both video and audio.
kwibergd1fe2812016-04-27 06:47:29 -07002287 std::unique_ptr<SessionDescriptionInterface> local_offer;
kwiberg2bbff992016-03-16 11:03:04 -07002288 EXPECT_TRUE(DoCreateOffer(&local_offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07002289 video_info = local_offer->description()->GetContentByName("video");
2290 ASSERT_TRUE(video_info != nullptr);
2291 video_info->rejected = true;
2292 cricket::ContentInfo* audio_info =
2293 local_offer->description()->GetContentByName("audio");
2294 ASSERT_TRUE(audio_info != nullptr);
2295 audio_info->rejected = true;
2296 EXPECT_TRUE(DoSetLocalDescription(local_offer.release()));
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002297 // Track state may be updated asynchronously.
perkjd61bf802016-03-24 03:16:19 -07002298 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002299 remote_audio->state(), kTimeout);
2300 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
2301 remote_video->state(), kTimeout);
deadbeefab9b2d12015-10-14 11:33:11 -07002302}
2303
2304// This tests that we won't crash if the remote track has been removed outside
2305// of PeerConnection and then PeerConnection tries to reject the track.
2306TEST_F(PeerConnectionInterfaceTest, RemoveTrackThenRejectMediaContent) {
2307 FakeConstraints constraints;
2308 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2309 true);
2310 CreatePeerConnection(&constraints);
2311 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2312 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2313 remote_stream->RemoveTrack(remote_stream->GetVideoTracks()[0]);
2314 remote_stream->RemoveTrack(remote_stream->GetAudioTracks()[0]);
2315
kwibergd1fe2812016-04-27 06:47:29 -07002316 std::unique_ptr<SessionDescriptionInterface> local_answer(
deadbeefab9b2d12015-10-14 11:33:11 -07002317 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
2318 kSdpStringWithStream1, nullptr));
2319 cricket::ContentInfo* video_info =
2320 local_answer->description()->GetContentByName("video");
2321 video_info->rejected = true;
2322 cricket::ContentInfo* audio_info =
2323 local_answer->description()->GetContentByName("audio");
2324 audio_info->rejected = true;
2325 EXPECT_TRUE(DoSetLocalDescription(local_answer.release()));
2326
2327 // No crash is a pass.
2328}
2329
deadbeef5e97fb52015-10-15 12:49:08 -07002330// This tests that if a recvonly remote description is set, no remote streams
2331// will be created, even if the description contains SSRCs/MSIDs.
2332// See: https://code.google.com/p/webrtc/issues/detail?id=5054
2333TEST_F(PeerConnectionInterfaceTest, RecvonlyDescriptionDoesntCreateStream) {
2334 FakeConstraints constraints;
2335 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2336 true);
2337 CreatePeerConnection(&constraints);
2338
2339 std::string recvonly_offer = kSdpStringWithStream1;
2340 rtc::replace_substrs(kSendrecv, strlen(kSendrecv), kRecvonly,
2341 strlen(kRecvonly), &recvonly_offer);
2342 CreateAndSetRemoteOffer(recvonly_offer);
2343
2344 EXPECT_EQ(0u, observer_.remote_streams()->count());
2345}
2346
deadbeefab9b2d12015-10-14 11:33:11 -07002347// This tests that a default MediaStream is created if a remote session
2348// description doesn't contain any streams and no MSID support.
2349// It also tests that the default stream is updated if a video m-line is added
2350// in a subsequent session description.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002351TEST_F(PeerConnectionInterfaceTest, SdpWithoutMsidCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002352 FakeConstraints constraints;
2353 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2354 true);
2355 CreatePeerConnection(&constraints);
2356 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2357
2358 ASSERT_EQ(1u, observer_.remote_streams()->count());
2359 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2360
2361 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2362 EXPECT_EQ(0u, remote_stream->GetVideoTracks().size());
2363 EXPECT_EQ("default", remote_stream->label());
2364
2365 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2366 ASSERT_EQ(1u, observer_.remote_streams()->count());
2367 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2368 EXPECT_EQ("defaulta0", remote_stream->GetAudioTracks()[0]->id());
deadbeef884f5852016-01-15 09:20:04 -08002369 EXPECT_EQ(MediaStreamTrackInterface::kLive,
2370 remote_stream->GetAudioTracks()[0]->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002371 ASSERT_EQ(1u, remote_stream->GetVideoTracks().size());
2372 EXPECT_EQ("defaultv0", remote_stream->GetVideoTracks()[0]->id());
deadbeef884f5852016-01-15 09:20:04 -08002373 EXPECT_EQ(MediaStreamTrackInterface::kLive,
2374 remote_stream->GetVideoTracks()[0]->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002375}
2376
2377// This tests that a default MediaStream is created if a remote session
2378// description doesn't contain any streams and media direction is send only.
2379TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002380 SendOnlySdpWithoutMsidCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002381 FakeConstraints constraints;
2382 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2383 true);
2384 CreatePeerConnection(&constraints);
2385 CreateAndSetRemoteOffer(kSdpStringSendOnlyWithoutStreams);
2386
2387 ASSERT_EQ(1u, observer_.remote_streams()->count());
2388 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2389
2390 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2391 EXPECT_EQ(1u, remote_stream->GetVideoTracks().size());
2392 EXPECT_EQ("default", remote_stream->label());
2393}
2394
2395// This tests that it won't crash when PeerConnection tries to remove
2396// a remote track that as already been removed from the MediaStream.
2397TEST_F(PeerConnectionInterfaceTest, RemoveAlreadyGoneRemoteStream) {
2398 FakeConstraints constraints;
2399 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2400 true);
2401 CreatePeerConnection(&constraints);
2402 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2403 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2404 remote_stream->RemoveTrack(remote_stream->GetAudioTracks()[0]);
2405 remote_stream->RemoveTrack(remote_stream->GetVideoTracks()[0]);
2406
2407 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2408
2409 // No crash is a pass.
2410}
2411
2412// This tests that a default MediaStream is created if the remote session
2413// description doesn't contain any streams and don't contain an indication if
2414// MSID is supported.
2415TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002416 SdpWithoutMsidAndStreamsCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002417 FakeConstraints constraints;
2418 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2419 true);
2420 CreatePeerConnection(&constraints);
2421 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2422
2423 ASSERT_EQ(1u, observer_.remote_streams()->count());
2424 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2425 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2426 EXPECT_EQ(1u, remote_stream->GetVideoTracks().size());
2427}
2428
2429// This tests that a default MediaStream is not created if the remote session
2430// description doesn't contain any streams but does support MSID.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002431TEST_F(PeerConnectionInterfaceTest, SdpWithMsidDontCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002432 FakeConstraints constraints;
2433 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2434 true);
2435 CreatePeerConnection(&constraints);
2436 CreateAndSetRemoteOffer(kSdpStringWithMsidWithoutStreams);
2437 EXPECT_EQ(0u, observer_.remote_streams()->count());
2438}
2439
deadbeefbda7e0b2015-12-08 17:13:40 -08002440// This tests that when setting a new description, the old default tracks are
2441// not destroyed and recreated.
2442// See: https://bugs.chromium.org/p/webrtc/issues/detail?id=5250
Stefan Holmer102362b2016-03-18 09:39:07 +01002443TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002444 DefaultTracksNotDestroyedAndRecreated) {
deadbeefbda7e0b2015-12-08 17:13:40 -08002445 FakeConstraints constraints;
2446 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2447 true);
2448 CreatePeerConnection(&constraints);
2449 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2450
2451 ASSERT_EQ(1u, observer_.remote_streams()->count());
2452 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2453 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2454
2455 // Set the track to "disabled", then set a new description and ensure the
2456 // track is still disabled, which ensures it hasn't been recreated.
2457 remote_stream->GetAudioTracks()[0]->set_enabled(false);
2458 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2459 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2460 EXPECT_FALSE(remote_stream->GetAudioTracks()[0]->enabled());
2461}
2462
deadbeefab9b2d12015-10-14 11:33:11 -07002463// This tests that a default MediaStream is not created if a remote session
2464// description is updated to not have any MediaStreams.
2465TEST_F(PeerConnectionInterfaceTest, VerifyDefaultStreamIsNotCreated) {
2466 FakeConstraints constraints;
2467 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2468 true);
2469 CreatePeerConnection(&constraints);
2470 CreateAndSetRemoteOffer(kSdpStringWithStream1);
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002471 rtc::scoped_refptr<StreamCollection> reference(CreateStreamCollection(1, 1));
deadbeefab9b2d12015-10-14 11:33:11 -07002472 EXPECT_TRUE(
2473 CompareStreamCollections(observer_.remote_streams(), reference.get()));
2474
2475 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2476 EXPECT_EQ(0u, observer_.remote_streams()->count());
2477}
2478
2479// This tests that an RtpSender is created when the local description is set
2480// after adding a local stream.
2481// TODO(deadbeef): This test and the one below it need to be updated when
2482// an RtpSender's lifetime isn't determined by when a local description is set.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002483TEST_F(PeerConnectionInterfaceTest, LocalDescriptionChanged) {
deadbeefab9b2d12015-10-14 11:33:11 -07002484 FakeConstraints constraints;
2485 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2486 true);
2487 CreatePeerConnection(&constraints);
deadbeefab9b2d12015-10-14 11:33:11 -07002488
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002489 // Create an offer with 1 stream with 2 tracks of each type.
2490 rtc::scoped_refptr<StreamCollection> stream_collection =
2491 CreateStreamCollection(1, 2);
2492 pc_->AddStream(stream_collection->at(0));
2493 std::unique_ptr<SessionDescriptionInterface> offer;
2494 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2495 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
deadbeefab9b2d12015-10-14 11:33:11 -07002496
deadbeefab9b2d12015-10-14 11:33:11 -07002497 auto senders = pc_->GetSenders();
2498 EXPECT_EQ(4u, senders.size());
2499 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2500 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2501 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[1]));
2502 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[1]));
2503
2504 // Remove an audio and video track.
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002505 pc_->RemoveStream(stream_collection->at(0));
2506 stream_collection = CreateStreamCollection(1, 1);
2507 pc_->AddStream(stream_collection->at(0));
2508 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2509 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
2510
deadbeefab9b2d12015-10-14 11:33:11 -07002511 senders = pc_->GetSenders();
2512 EXPECT_EQ(2u, senders.size());
2513 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2514 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2515 EXPECT_FALSE(ContainsSender(senders, kAudioTracks[1]));
2516 EXPECT_FALSE(ContainsSender(senders, kVideoTracks[1]));
2517}
2518
2519// This tests that an RtpSender is created when the local description is set
2520// before adding a local stream.
2521TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002522 AddLocalStreamAfterLocalDescriptionChanged) {
deadbeefab9b2d12015-10-14 11:33:11 -07002523 FakeConstraints constraints;
2524 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2525 true);
2526 CreatePeerConnection(&constraints);
deadbeefab9b2d12015-10-14 11:33:11 -07002527
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002528 rtc::scoped_refptr<StreamCollection> stream_collection =
2529 CreateStreamCollection(1, 2);
2530 // Add a stream to create the offer, but remove it afterwards.
2531 pc_->AddStream(stream_collection->at(0));
2532 std::unique_ptr<SessionDescriptionInterface> offer;
2533 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2534 pc_->RemoveStream(stream_collection->at(0));
deadbeefab9b2d12015-10-14 11:33:11 -07002535
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002536 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
deadbeefab9b2d12015-10-14 11:33:11 -07002537 auto senders = pc_->GetSenders();
2538 EXPECT_EQ(0u, senders.size());
2539
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002540 pc_->AddStream(stream_collection->at(0));
deadbeefab9b2d12015-10-14 11:33:11 -07002541 senders = pc_->GetSenders();
2542 EXPECT_EQ(4u, senders.size());
2543 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2544 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2545 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[1]));
2546 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[1]));
2547}
2548
2549// This tests that the expected behavior occurs if the SSRC on a local track is
2550// changed when SetLocalDescription is called.
2551TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002552 ChangeSsrcOnTrackInLocalSessionDescription) {
deadbeefab9b2d12015-10-14 11:33:11 -07002553 FakeConstraints constraints;
2554 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2555 true);
2556 CreatePeerConnection(&constraints);
deadbeefab9b2d12015-10-14 11:33:11 -07002557
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002558 rtc::scoped_refptr<StreamCollection> stream_collection =
2559 CreateStreamCollection(2, 1);
2560 pc_->AddStream(stream_collection->at(0));
2561 std::unique_ptr<SessionDescriptionInterface> offer;
2562 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2563 // Grab a copy of the offer before it gets passed into the PC.
2564 std::unique_ptr<JsepSessionDescription> modified_offer(
2565 new JsepSessionDescription(JsepSessionDescription::kOffer));
2566 modified_offer->Initialize(offer->description()->Copy(), offer->session_id(),
2567 offer->session_version());
2568 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
deadbeefab9b2d12015-10-14 11:33:11 -07002569
deadbeefab9b2d12015-10-14 11:33:11 -07002570 auto senders = pc_->GetSenders();
2571 EXPECT_EQ(2u, senders.size());
2572 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2573 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2574
2575 // Change the ssrc of the audio and video track.
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002576 cricket::MediaContentDescription* desc =
2577 cricket::GetFirstAudioContentDescription(modified_offer->description());
2578 ASSERT_TRUE(desc != NULL);
2579 for (StreamParams& stream : desc->mutable_streams()) {
2580 for (unsigned int& ssrc : stream.ssrcs) {
2581 ++ssrc;
2582 }
2583 }
deadbeefab9b2d12015-10-14 11:33:11 -07002584
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002585 desc =
2586 cricket::GetFirstVideoContentDescription(modified_offer->description());
2587 ASSERT_TRUE(desc != NULL);
2588 for (StreamParams& stream : desc->mutable_streams()) {
2589 for (unsigned int& ssrc : stream.ssrcs) {
2590 ++ssrc;
2591 }
2592 }
2593
2594 EXPECT_TRUE(DoSetLocalDescription(modified_offer.release()));
deadbeefab9b2d12015-10-14 11:33:11 -07002595 senders = pc_->GetSenders();
2596 EXPECT_EQ(2u, senders.size());
2597 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2598 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2599 // TODO(deadbeef): Once RtpSenders expose parameters, check that the SSRC
2600 // changed.
2601}
2602
2603// This tests that the expected behavior occurs if a new session description is
2604// set with the same tracks, but on a different MediaStream.
Stefan Holmer55d6e7c2016-03-17 16:26:40 +01002605TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002606 SignalSameTracksInSeparateMediaStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002607 FakeConstraints constraints;
2608 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2609 true);
2610 CreatePeerConnection(&constraints);
deadbeefab9b2d12015-10-14 11:33:11 -07002611
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002612 rtc::scoped_refptr<StreamCollection> stream_collection =
2613 CreateStreamCollection(2, 1);
2614 pc_->AddStream(stream_collection->at(0));
2615 std::unique_ptr<SessionDescriptionInterface> offer;
2616 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2617 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
deadbeefab9b2d12015-10-14 11:33:11 -07002618
deadbeefab9b2d12015-10-14 11:33:11 -07002619 auto senders = pc_->GetSenders();
2620 EXPECT_EQ(2u, senders.size());
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002621 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0], kStreams[0]));
2622 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0], kStreams[0]));
deadbeefab9b2d12015-10-14 11:33:11 -07002623
2624 // Add a new MediaStream but with the same tracks as in the first stream.
2625 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream_1(
2626 webrtc::MediaStream::Create(kStreams[1]));
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002627 stream_1->AddTrack(stream_collection->at(0)->GetVideoTracks()[0]);
2628 stream_1->AddTrack(stream_collection->at(0)->GetAudioTracks()[0]);
deadbeefab9b2d12015-10-14 11:33:11 -07002629 pc_->AddStream(stream_1);
2630
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002631 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2632 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
deadbeefab9b2d12015-10-14 11:33:11 -07002633
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002634 auto new_senders = pc_->GetSenders();
2635 // Should be the same senders as before, but with updated stream id.
2636 // Note that this behavior is subject to change in the future.
2637 // We may decide the PC should ignore existing tracks in AddStream.
2638 EXPECT_EQ(senders, new_senders);
2639 EXPECT_TRUE(ContainsSender(new_senders, kAudioTracks[0], kStreams[1]));
2640 EXPECT_TRUE(ContainsSender(new_senders, kVideoTracks[0], kStreams[1]));
deadbeefab9b2d12015-10-14 11:33:11 -07002641}
2642
nisse51542be2016-02-12 02:27:06 -08002643class PeerConnectionMediaConfigTest : public testing::Test {
2644 protected:
2645 void SetUp() override {
nisseaf510af2016-03-21 08:20:42 -07002646 pcf_ = new rtc::RefCountedObject<PeerConnectionFactoryForTest>();
nisse51542be2016-02-12 02:27:06 -08002647 pcf_->Initialize();
2648 }
2649 const cricket::MediaConfig& TestCreatePeerConnection(
2650 const PeerConnectionInterface::RTCConfiguration& config,
2651 const MediaConstraintsInterface *constraints) {
2652 pcf_->create_media_controller_called_ = false;
2653
zhihuang9763d562016-08-05 11:14:50 -07002654 rtc::scoped_refptr<PeerConnectionInterface> pc(pcf_->CreatePeerConnection(
2655 config, constraints, nullptr, nullptr, &observer_));
nisse51542be2016-02-12 02:27:06 -08002656 EXPECT_TRUE(pc.get());
2657 EXPECT_TRUE(pcf_->create_media_controller_called_);
2658 return pcf_->create_media_controller_config_;
2659 }
2660
zhihuang9763d562016-08-05 11:14:50 -07002661 rtc::scoped_refptr<PeerConnectionFactoryForTest> pcf_;
nisse51542be2016-02-12 02:27:06 -08002662 MockPeerConnectionObserver observer_;
2663};
2664
2665// This test verifies the default behaviour with no constraints and a
2666// default RTCConfiguration.
2667TEST_F(PeerConnectionMediaConfigTest, TestDefaults) {
2668 PeerConnectionInterface::RTCConfiguration config;
2669 FakeConstraints constraints;
2670
2671 const cricket::MediaConfig& media_config =
2672 TestCreatePeerConnection(config, &constraints);
2673
2674 EXPECT_FALSE(media_config.enable_dscp);
nisse0db023a2016-03-01 04:29:59 -08002675 EXPECT_TRUE(media_config.video.enable_cpu_overuse_detection);
2676 EXPECT_FALSE(media_config.video.disable_prerenderer_smoothing);
2677 EXPECT_FALSE(media_config.video.suspend_below_min_bitrate);
nisse51542be2016-02-12 02:27:06 -08002678}
2679
2680// This test verifies the DSCP constraint is recognized and passed to
2681// the CreateMediaController call.
2682TEST_F(PeerConnectionMediaConfigTest, TestDscpConstraintTrue) {
2683 PeerConnectionInterface::RTCConfiguration config;
2684 FakeConstraints constraints;
2685
2686 constraints.AddOptional(webrtc::MediaConstraintsInterface::kEnableDscp, true);
2687 const cricket::MediaConfig& media_config =
2688 TestCreatePeerConnection(config, &constraints);
2689
2690 EXPECT_TRUE(media_config.enable_dscp);
2691}
2692
2693// This test verifies the cpu overuse detection constraint is
2694// recognized and passed to the CreateMediaController call.
2695TEST_F(PeerConnectionMediaConfigTest, TestCpuOveruseConstraintFalse) {
2696 PeerConnectionInterface::RTCConfiguration config;
2697 FakeConstraints constraints;
2698
2699 constraints.AddOptional(
2700 webrtc::MediaConstraintsInterface::kCpuOveruseDetection, false);
2701 const cricket::MediaConfig media_config =
2702 TestCreatePeerConnection(config, &constraints);
2703
nisse0db023a2016-03-01 04:29:59 -08002704 EXPECT_FALSE(media_config.video.enable_cpu_overuse_detection);
nisse51542be2016-02-12 02:27:06 -08002705}
2706
2707// This test verifies that the disable_prerenderer_smoothing flag is
2708// propagated from RTCConfiguration to the CreateMediaController call.
2709TEST_F(PeerConnectionMediaConfigTest, TestDisablePrerendererSmoothingTrue) {
2710 PeerConnectionInterface::RTCConfiguration config;
2711 FakeConstraints constraints;
2712
Niels Möller71bdda02016-03-31 12:59:59 +02002713 config.set_prerenderer_smoothing(false);
nisse51542be2016-02-12 02:27:06 -08002714 const cricket::MediaConfig& media_config =
2715 TestCreatePeerConnection(config, &constraints);
2716
nisse0db023a2016-03-01 04:29:59 -08002717 EXPECT_TRUE(media_config.video.disable_prerenderer_smoothing);
2718}
2719
2720// This test verifies the suspend below min bitrate constraint is
2721// recognized and passed to the CreateMediaController call.
2722TEST_F(PeerConnectionMediaConfigTest,
2723 TestSuspendBelowMinBitrateConstraintTrue) {
2724 PeerConnectionInterface::RTCConfiguration config;
2725 FakeConstraints constraints;
2726
2727 constraints.AddOptional(
2728 webrtc::MediaConstraintsInterface::kEnableVideoSuspendBelowMinBitrate,
2729 true);
2730 const cricket::MediaConfig media_config =
2731 TestCreatePeerConnection(config, &constraints);
2732
2733 EXPECT_TRUE(media_config.video.suspend_below_min_bitrate);
nisse51542be2016-02-12 02:27:06 -08002734}
2735
deadbeefab9b2d12015-10-14 11:33:11 -07002736// The following tests verify that session options are created correctly.
deadbeefc80741f2015-10-22 13:14:45 -07002737// TODO(deadbeef): Convert these tests to be more end-to-end. Instead of
2738// "verify options are converted correctly", should be "pass options into
2739// CreateOffer and verify the correct offer is produced."
deadbeefab9b2d12015-10-14 11:33:11 -07002740
2741TEST(CreateSessionOptionsTest, GetOptionsForOfferWithInvalidAudioOption) {
2742 RTCOfferAnswerOptions rtc_options;
2743 rtc_options.offer_to_receive_audio = RTCOfferAnswerOptions::kUndefined - 1;
2744
2745 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08002746 EXPECT_FALSE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002747
2748 rtc_options.offer_to_receive_audio =
2749 RTCOfferAnswerOptions::kMaxOfferToReceiveMedia + 1;
htaaac2dea2016-03-10 13:35:55 -08002750 EXPECT_FALSE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002751}
2752
2753TEST(CreateSessionOptionsTest, GetOptionsForOfferWithInvalidVideoOption) {
2754 RTCOfferAnswerOptions rtc_options;
2755 rtc_options.offer_to_receive_video = RTCOfferAnswerOptions::kUndefined - 1;
2756
2757 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08002758 EXPECT_FALSE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002759
2760 rtc_options.offer_to_receive_video =
2761 RTCOfferAnswerOptions::kMaxOfferToReceiveMedia + 1;
htaaac2dea2016-03-10 13:35:55 -08002762 EXPECT_FALSE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002763}
2764
2765// Test that a MediaSessionOptions is created for an offer if
deadbeefc80741f2015-10-22 13:14:45 -07002766// OfferToReceiveAudio and OfferToReceiveVideo options are set.
deadbeefab9b2d12015-10-14 11:33:11 -07002767TEST(CreateSessionOptionsTest, GetMediaSessionOptionsForOfferWithAudioVideo) {
2768 RTCOfferAnswerOptions rtc_options;
2769 rtc_options.offer_to_receive_audio = 1;
2770 rtc_options.offer_to_receive_video = 1;
2771
2772 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08002773 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002774 EXPECT_TRUE(options.has_audio());
2775 EXPECT_TRUE(options.has_video());
2776 EXPECT_TRUE(options.bundle_enabled);
2777}
2778
2779// Test that a correct MediaSessionOptions is created for an offer if
deadbeefc80741f2015-10-22 13:14:45 -07002780// OfferToReceiveAudio is set.
deadbeefab9b2d12015-10-14 11:33:11 -07002781TEST(CreateSessionOptionsTest, GetMediaSessionOptionsForOfferWithAudio) {
2782 RTCOfferAnswerOptions rtc_options;
2783 rtc_options.offer_to_receive_audio = 1;
2784
2785 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08002786 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002787 EXPECT_TRUE(options.has_audio());
2788 EXPECT_FALSE(options.has_video());
2789 EXPECT_TRUE(options.bundle_enabled);
2790}
2791
2792// Test that a correct MediaSessionOptions is created for an offer if
deadbeefc80741f2015-10-22 13:14:45 -07002793// the default OfferOptions are used.
deadbeefab9b2d12015-10-14 11:33:11 -07002794TEST(CreateSessionOptionsTest, GetDefaultMediaSessionOptionsForOffer) {
2795 RTCOfferAnswerOptions rtc_options;
2796
2797 cricket::MediaSessionOptions options;
deadbeef0ed85b22016-02-23 17:24:52 -08002798 options.transport_options["audio"] = cricket::TransportOptions();
2799 options.transport_options["video"] = cricket::TransportOptions();
htaaac2dea2016-03-10 13:35:55 -08002800 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefc80741f2015-10-22 13:14:45 -07002801 EXPECT_TRUE(options.has_audio());
deadbeefab9b2d12015-10-14 11:33:11 -07002802 EXPECT_FALSE(options.has_video());
deadbeefc80741f2015-10-22 13:14:45 -07002803 EXPECT_TRUE(options.bundle_enabled);
deadbeefab9b2d12015-10-14 11:33:11 -07002804 EXPECT_TRUE(options.vad_enabled);
deadbeef0ed85b22016-02-23 17:24:52 -08002805 EXPECT_FALSE(options.transport_options["audio"].ice_restart);
2806 EXPECT_FALSE(options.transport_options["video"].ice_restart);
deadbeefab9b2d12015-10-14 11:33:11 -07002807}
2808
2809// Test that a correct MediaSessionOptions is created for an offer if
deadbeefc80741f2015-10-22 13:14:45 -07002810// OfferToReceiveVideo is set.
deadbeefab9b2d12015-10-14 11:33:11 -07002811TEST(CreateSessionOptionsTest, GetMediaSessionOptionsForOfferWithVideo) {
2812 RTCOfferAnswerOptions rtc_options;
2813 rtc_options.offer_to_receive_audio = 0;
2814 rtc_options.offer_to_receive_video = 1;
2815
2816 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08002817 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002818 EXPECT_FALSE(options.has_audio());
2819 EXPECT_TRUE(options.has_video());
2820 EXPECT_TRUE(options.bundle_enabled);
2821}
2822
2823// Test that a correct MediaSessionOptions is created for an offer if
2824// UseRtpMux is set to false.
2825TEST(CreateSessionOptionsTest,
2826 GetMediaSessionOptionsForOfferWithBundleDisabled) {
2827 RTCOfferAnswerOptions rtc_options;
2828 rtc_options.offer_to_receive_audio = 1;
2829 rtc_options.offer_to_receive_video = 1;
2830 rtc_options.use_rtp_mux = false;
2831
2832 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08002833 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002834 EXPECT_TRUE(options.has_audio());
2835 EXPECT_TRUE(options.has_video());
2836 EXPECT_FALSE(options.bundle_enabled);
2837}
2838
2839// Test that a correct MediaSessionOptions is created to restart ice if
2840// IceRestart is set. It also tests that subsequent MediaSessionOptions don't
Taylor Brandstetterf475d362016-01-08 15:35:57 -08002841// have |audio_transport_options.ice_restart| etc. set.
deadbeefab9b2d12015-10-14 11:33:11 -07002842TEST(CreateSessionOptionsTest, GetMediaSessionOptionsForOfferWithIceRestart) {
2843 RTCOfferAnswerOptions rtc_options;
2844 rtc_options.ice_restart = true;
2845
2846 cricket::MediaSessionOptions options;
deadbeef0ed85b22016-02-23 17:24:52 -08002847 options.transport_options["audio"] = cricket::TransportOptions();
2848 options.transport_options["video"] = cricket::TransportOptions();
htaaac2dea2016-03-10 13:35:55 -08002849 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeef0ed85b22016-02-23 17:24:52 -08002850 EXPECT_TRUE(options.transport_options["audio"].ice_restart);
2851 EXPECT_TRUE(options.transport_options["video"].ice_restart);
deadbeefab9b2d12015-10-14 11:33:11 -07002852
2853 rtc_options = RTCOfferAnswerOptions();
htaaac2dea2016-03-10 13:35:55 -08002854 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeef0ed85b22016-02-23 17:24:52 -08002855 EXPECT_FALSE(options.transport_options["audio"].ice_restart);
2856 EXPECT_FALSE(options.transport_options["video"].ice_restart);
deadbeefab9b2d12015-10-14 11:33:11 -07002857}
2858
2859// Test that the MediaConstraints in an answer don't affect if audio and video
2860// is offered in an offer but that if kOfferToReceiveAudio or
2861// kOfferToReceiveVideo constraints are true in an offer, the media type will be
2862// included in subsequent answers.
2863TEST(CreateSessionOptionsTest, MediaConstraintsInAnswer) {
2864 FakeConstraints answer_c;
2865 answer_c.SetMandatoryReceiveAudio(true);
2866 answer_c.SetMandatoryReceiveVideo(true);
2867
2868 cricket::MediaSessionOptions answer_options;
2869 EXPECT_TRUE(ParseConstraintsForAnswer(&answer_c, &answer_options));
2870 EXPECT_TRUE(answer_options.has_audio());
2871 EXPECT_TRUE(answer_options.has_video());
2872
deadbeefc80741f2015-10-22 13:14:45 -07002873 RTCOfferAnswerOptions rtc_offer_options;
deadbeefab9b2d12015-10-14 11:33:11 -07002874
2875 cricket::MediaSessionOptions offer_options;
htaaac2dea2016-03-10 13:35:55 -08002876 EXPECT_TRUE(
2877 ExtractMediaSessionOptions(rtc_offer_options, false, &offer_options));
deadbeefc80741f2015-10-22 13:14:45 -07002878 EXPECT_TRUE(offer_options.has_audio());
htaaac2dea2016-03-10 13:35:55 -08002879 EXPECT_TRUE(offer_options.has_video());
deadbeefab9b2d12015-10-14 11:33:11 -07002880
deadbeefc80741f2015-10-22 13:14:45 -07002881 RTCOfferAnswerOptions updated_rtc_offer_options;
2882 updated_rtc_offer_options.offer_to_receive_audio = 1;
2883 updated_rtc_offer_options.offer_to_receive_video = 1;
deadbeefab9b2d12015-10-14 11:33:11 -07002884
2885 cricket::MediaSessionOptions updated_offer_options;
htaaac2dea2016-03-10 13:35:55 -08002886 EXPECT_TRUE(ExtractMediaSessionOptions(updated_rtc_offer_options, false,
htaa2a49d92016-03-04 02:51:39 -08002887 &updated_offer_options));
deadbeefab9b2d12015-10-14 11:33:11 -07002888 EXPECT_TRUE(updated_offer_options.has_audio());
2889 EXPECT_TRUE(updated_offer_options.has_video());
2890
2891 // Since an offer has been created with both audio and video, subsequent
2892 // offers and answers should contain both audio and video.
2893 // Answers will only contain the media types that exist in the offer
2894 // regardless of the value of |updated_answer_options.has_audio| and
2895 // |updated_answer_options.has_video|.
2896 FakeConstraints updated_answer_c;
2897 answer_c.SetMandatoryReceiveAudio(false);
2898 answer_c.SetMandatoryReceiveVideo(false);
2899
2900 cricket::MediaSessionOptions updated_answer_options;
2901 EXPECT_TRUE(
2902 ParseConstraintsForAnswer(&updated_answer_c, &updated_answer_options));
2903 EXPECT_TRUE(updated_answer_options.has_audio());
2904 EXPECT_TRUE(updated_answer_options.has_video());
deadbeefab9b2d12015-10-14 11:33:11 -07002905}