blob: a4a11e1252e5720d5cc71386a43bb125c7f5cb7d [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:
deadbeefab9b2d12015-10-14 11:33:11 -0700438 MockPeerConnectionObserver() : remote_streams_(StreamCollection::Create()) {}
Henrik Kjellander3fe372d2016-05-12 08:10:52 +0200439 virtual ~MockPeerConnectionObserver() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000440 }
441 void SetPeerConnectionInterface(PeerConnectionInterface* pc) {
442 pc_ = pc;
443 if (pc) {
444 state_ = pc_->signaling_state();
445 }
446 }
nisseef8b61e2016-04-29 06:09:15 -0700447 void OnSignalingChange(
448 PeerConnectionInterface::SignalingState new_state) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000449 EXPECT_EQ(pc_->signaling_state(), new_state);
450 state_ = new_state;
451 }
452 // TODO(bemasc): Remove this once callers transition to OnIceGatheringChange.
453 virtual void OnStateChange(StateType state_changed) {
454 if (pc_.get() == NULL)
455 return;
456 switch (state_changed) {
457 case kSignalingState:
458 // OnSignalingChange and OnStateChange(kSignalingState) should always
459 // be called approximately simultaneously. To ease testing, we require
460 // that they always be called in that order. This check verifies
461 // that OnSignalingChange has just been called.
462 EXPECT_EQ(pc_->signaling_state(), state_);
463 break;
464 case kIceState:
465 ADD_FAILURE();
466 break;
467 default:
468 ADD_FAILURE();
469 break;
470 }
471 }
deadbeefab9b2d12015-10-14 11:33:11 -0700472
473 MediaStreamInterface* RemoteStream(const std::string& label) {
474 return remote_streams_->find(label);
475 }
476 StreamCollectionInterface* remote_streams() const { return remote_streams_; }
Taylor Brandstetter98cde262016-05-31 13:02:21 -0700477 void OnAddStream(rtc::scoped_refptr<MediaStreamInterface> stream) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000478 last_added_stream_ = stream;
deadbeefab9b2d12015-10-14 11:33:11 -0700479 remote_streams_->AddStream(stream);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000480 }
Taylor Brandstetter98cde262016-05-31 13:02:21 -0700481 void OnRemoveStream(
482 rtc::scoped_refptr<MediaStreamInterface> stream) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000483 last_removed_stream_ = stream;
deadbeefab9b2d12015-10-14 11:33:11 -0700484 remote_streams_->RemoveStream(stream);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000485 }
perkjdfb769d2016-02-09 03:09:43 -0800486 void OnRenegotiationNeeded() override { renegotiation_needed_ = true; }
Taylor Brandstetter98cde262016-05-31 13:02:21 -0700487 void OnDataChannel(
488 rtc::scoped_refptr<DataChannelInterface> data_channel) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000489 last_datachannel_ = data_channel;
490 }
491
perkjdfb769d2016-02-09 03:09:43 -0800492 void OnIceConnectionChange(
493 PeerConnectionInterface::IceConnectionState new_state) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000494 EXPECT_EQ(pc_->ice_connection_state(), new_state);
zhihuang29ff8442016-07-27 11:07:25 -0700495 callback_triggered = true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000496 }
perkjdfb769d2016-02-09 03:09:43 -0800497 void OnIceGatheringChange(
498 PeerConnectionInterface::IceGatheringState new_state) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000499 EXPECT_EQ(pc_->ice_gathering_state(), new_state);
perkjdfb769d2016-02-09 03:09:43 -0800500 ice_complete_ = new_state == PeerConnectionInterface::kIceGatheringComplete;
zhihuang29ff8442016-07-27 11:07:25 -0700501 callback_triggered = true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000502 }
perkjdfb769d2016-02-09 03:09:43 -0800503 void OnIceCandidate(const webrtc::IceCandidateInterface* candidate) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000504 EXPECT_NE(PeerConnectionInterface::kIceGatheringNew,
505 pc_->ice_gathering_state());
506
507 std::string sdp;
508 EXPECT_TRUE(candidate->ToString(&sdp));
509 EXPECT_LT(0u, sdp.size());
510 last_candidate_.reset(webrtc::CreateIceCandidate(candidate->sdp_mid(),
511 candidate->sdp_mline_index(), sdp, NULL));
512 EXPECT_TRUE(last_candidate_.get() != NULL);
zhihuang29ff8442016-07-27 11:07:25 -0700513 callback_triggered = true;
514 }
515
516 void OnIceCandidatesRemoved(
517 const std::vector<cricket::Candidate>& candidates) override {
518 callback_triggered = true;
519 }
520
521 void OnIceConnectionReceivingChange(bool receiving) override {
522 callback_triggered = true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000523 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000524
525 // Returns the label of the last added stream.
526 // Empty string if no stream have been added.
527 std::string GetLastAddedStreamLabel() {
528 if (last_added_stream_.get())
529 return last_added_stream_->label();
530 return "";
531 }
532 std::string GetLastRemovedStreamLabel() {
533 if (last_removed_stream_.get())
534 return last_removed_stream_->label();
535 return "";
536 }
537
zhihuang9763d562016-08-05 11:14:50 -0700538 rtc::scoped_refptr<PeerConnectionInterface> pc_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000539 PeerConnectionInterface::SignalingState state_;
kwibergd1fe2812016-04-27 06:47:29 -0700540 std::unique_ptr<IceCandidateInterface> last_candidate_;
zhihuang9763d562016-08-05 11:14:50 -0700541 rtc::scoped_refptr<DataChannelInterface> last_datachannel_;
deadbeefab9b2d12015-10-14 11:33:11 -0700542 rtc::scoped_refptr<StreamCollection> remote_streams_;
543 bool renegotiation_needed_ = false;
544 bool ice_complete_ = false;
zhihuang29ff8442016-07-27 11:07:25 -0700545 bool callback_triggered = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000546
547 private:
zhihuang9763d562016-08-05 11:14:50 -0700548 rtc::scoped_refptr<MediaStreamInterface> last_added_stream_;
549 rtc::scoped_refptr<MediaStreamInterface> last_removed_stream_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000550};
551
552} // namespace
deadbeefab9b2d12015-10-14 11:33:11 -0700553
zhihuang29ff8442016-07-27 11:07:25 -0700554// The PeerConnectionMediaConfig tests below verify that configuration
555// and constraints are propagated into the MediaConfig passed to
556// CreateMediaController. These settings are intended for MediaChannel
557// constructors, but that is not exercised by these unittest.
558class PeerConnectionFactoryForTest : public webrtc::PeerConnectionFactory {
559 public:
560 webrtc::MediaControllerInterface* CreateMediaController(
skvlad11a9cbf2016-10-07 11:53:05 -0700561 const cricket::MediaConfig& config,
562 webrtc::RtcEventLog* event_log) const override {
zhihuang29ff8442016-07-27 11:07:25 -0700563 create_media_controller_called_ = true;
564 create_media_controller_config_ = config;
565
566 webrtc::MediaControllerInterface* mc =
skvlad11a9cbf2016-10-07 11:53:05 -0700567 PeerConnectionFactory::CreateMediaController(config, event_log);
zhihuang29ff8442016-07-27 11:07:25 -0700568 EXPECT_TRUE(mc != nullptr);
569 return mc;
570 }
571
572 cricket::TransportController* CreateTransportController(
Honghai Zhangbfd398c2016-08-30 22:07:42 -0700573 cricket::PortAllocator* port_allocator,
574 bool redetermine_role_on_ice_restart) override {
zhihuang29ff8442016-07-27 11:07:25 -0700575 transport_controller = new cricket::TransportController(
Honghai Zhangbfd398c2016-08-30 22:07:42 -0700576 rtc::Thread::Current(), rtc::Thread::Current(), port_allocator,
577 redetermine_role_on_ice_restart);
zhihuang29ff8442016-07-27 11:07:25 -0700578 return transport_controller;
579 }
580
581 cricket::TransportController* transport_controller;
582 // Mutable, so they can be modified in the above const-declared method.
583 mutable bool create_media_controller_called_ = false;
584 mutable cricket::MediaConfig create_media_controller_config_;
585};
586
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000587class PeerConnectionInterfaceTest : public testing::Test {
588 protected:
phoglund37ebcf02016-01-08 05:04:57 -0800589 PeerConnectionInterfaceTest() {
590#ifdef WEBRTC_ANDROID
591 webrtc::InitializeAndroidObjects();
592#endif
593 }
594
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000595 virtual void SetUp() {
596 pc_factory_ = webrtc::CreatePeerConnectionFactory(
danilchape9021a32016-05-17 01:52:02 -0700597 rtc::Thread::Current(), rtc::Thread::Current(), rtc::Thread::Current(),
598 nullptr, nullptr, nullptr);
599 ASSERT_TRUE(pc_factory_);
zhihuang29ff8442016-07-27 11:07:25 -0700600 pc_factory_for_test_ =
601 new rtc::RefCountedObject<PeerConnectionFactoryForTest>();
602 pc_factory_for_test_->Initialize();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000603 }
604
605 void CreatePeerConnection() {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700606 CreatePeerConnection(PeerConnectionInterface::RTCConfiguration(), nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000607 }
608
609 void CreatePeerConnection(webrtc::MediaConstraintsInterface* constraints) {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700610 CreatePeerConnection(PeerConnectionInterface::RTCConfiguration(),
611 constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000612 }
613
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700614 void CreatePeerConnectionWithIceTransportsType(
615 PeerConnectionInterface::IceTransportsType type) {
616 PeerConnectionInterface::RTCConfiguration config;
617 config.type = type;
618 return CreatePeerConnection(config, nullptr);
619 }
620
621 void CreatePeerConnectionWithIceServer(const std::string& uri,
622 const std::string& password) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800623 PeerConnectionInterface::RTCConfiguration config;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000624 PeerConnectionInterface::IceServer server;
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700625 server.uri = uri;
626 server.password = password;
627 config.servers.push_back(server);
628 CreatePeerConnection(config, nullptr);
629 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000630
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700631 void CreatePeerConnection(PeerConnectionInterface::RTCConfiguration config,
632 webrtc::MediaConstraintsInterface* constraints) {
kwibergd1fe2812016-04-27 06:47:29 -0700633 std::unique_ptr<cricket::FakePortAllocator> port_allocator(
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800634 new cricket::FakePortAllocator(rtc::Thread::Current(), nullptr));
635 port_allocator_ = port_allocator.get();
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000636
buildbot@webrtc.org61c1b8e2014-04-09 06:06:38 +0000637 // DTLS does not work in a loopback call, so is disabled for most of the
638 // tests in this file. We only create a FakeIdentityService if the test
639 // explicitly sets the constraint.
jiayl@webrtc.org61e00b02015-03-04 22:17:38 +0000640 FakeConstraints default_constraints;
641 if (!constraints) {
642 constraints = &default_constraints;
643
644 default_constraints.AddMandatory(
645 webrtc::MediaConstraintsInterface::kEnableDtlsSrtp, false);
646 }
647
Henrik Boströmd79599d2016-06-01 13:58:50 +0200648 std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator;
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000649 bool dtls;
650 if (FindConstraint(constraints,
651 webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
652 &dtls,
Henrik Boström5e56c592015-08-11 10:33:13 +0200653 nullptr) && dtls) {
Henrik Boströmd79599d2016-06-01 13:58:50 +0200654 cert_generator.reset(new FakeRTCCertificateGenerator());
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000655 }
Henrik Boströmd79599d2016-06-01 13:58:50 +0200656 pc_ = pc_factory_->CreatePeerConnection(
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800657 config, constraints, std::move(port_allocator),
Henrik Boströmd79599d2016-06-01 13:58:50 +0200658 std::move(cert_generator), &observer_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000659 ASSERT_TRUE(pc_.get() != NULL);
660 observer_.SetPeerConnectionInterface(pc_.get());
661 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
662 }
663
deadbeef0a6c4ca2015-10-06 11:38:28 -0700664 void CreatePeerConnectionExpectFail(const std::string& uri) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800665 PeerConnectionInterface::RTCConfiguration config;
deadbeef0a6c4ca2015-10-06 11:38:28 -0700666 PeerConnectionInterface::IceServer server;
deadbeef0a6c4ca2015-10-06 11:38:28 -0700667 server.uri = uri;
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800668 config.servers.push_back(server);
deadbeef0a6c4ca2015-10-06 11:38:28 -0700669
zhihuang9763d562016-08-05 11:14:50 -0700670 rtc::scoped_refptr<PeerConnectionInterface> pc;
hbosd7973cc2016-05-27 06:08:53 -0700671 pc = pc_factory_->CreatePeerConnection(config, nullptr, nullptr, nullptr,
672 &observer_);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800673 EXPECT_EQ(nullptr, pc);
deadbeef0a6c4ca2015-10-06 11:38:28 -0700674 }
675
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000676 void CreatePeerConnectionWithDifferentConfigurations() {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700677 CreatePeerConnectionWithIceServer(kStunAddressOnly, "");
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800678 EXPECT_EQ(1u, port_allocator_->stun_servers().size());
679 EXPECT_EQ(0u, port_allocator_->turn_servers().size());
680 EXPECT_EQ("address", port_allocator_->stun_servers().begin()->hostname());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000681 EXPECT_EQ(kDefaultStunPort,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800682 port_allocator_->stun_servers().begin()->port());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000683
deadbeef0a6c4ca2015-10-06 11:38:28 -0700684 CreatePeerConnectionExpectFail(kStunInvalidPort);
685 CreatePeerConnectionExpectFail(kStunAddressPortAndMore1);
686 CreatePeerConnectionExpectFail(kStunAddressPortAndMore2);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000687
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700688 CreatePeerConnectionWithIceServer(kTurnIceServerUri, kTurnPassword);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800689 EXPECT_EQ(0u, port_allocator_->stun_servers().size());
690 EXPECT_EQ(1u, port_allocator_->turn_servers().size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000691 EXPECT_EQ(kTurnUsername,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800692 port_allocator_->turn_servers()[0].credentials.username);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000693 EXPECT_EQ(kTurnPassword,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800694 port_allocator_->turn_servers()[0].credentials.password);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000695 EXPECT_EQ(kTurnHostname,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800696 port_allocator_->turn_servers()[0].ports[0].address.hostname());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000697 }
698
699 void ReleasePeerConnection() {
700 pc_ = NULL;
701 observer_.SetPeerConnectionInterface(NULL);
702 }
703
deadbeefab9b2d12015-10-14 11:33:11 -0700704 void AddVideoStream(const std::string& label) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000705 // Create a local stream.
zhihuang9763d562016-08-05 11:14:50 -0700706 rtc::scoped_refptr<MediaStreamInterface> stream(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000707 pc_factory_->CreateLocalMediaStream(label));
zhihuang9763d562016-08-05 11:14:50 -0700708 rtc::scoped_refptr<VideoTrackSourceInterface> video_source(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000709 pc_factory_->CreateVideoSource(new cricket::FakeVideoCapturer(), NULL));
zhihuang9763d562016-08-05 11:14:50 -0700710 rtc::scoped_refptr<VideoTrackInterface> video_track(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000711 pc_factory_->CreateVideoTrack(label + "v0", video_source));
712 stream->AddTrack(video_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +0000713 EXPECT_TRUE(pc_->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000714 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
715 observer_.renegotiation_needed_ = false;
716 }
717
718 void AddVoiceStream(const std::string& label) {
719 // Create a local stream.
zhihuang9763d562016-08-05 11:14:50 -0700720 rtc::scoped_refptr<MediaStreamInterface> stream(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000721 pc_factory_->CreateLocalMediaStream(label));
zhihuang9763d562016-08-05 11:14:50 -0700722 rtc::scoped_refptr<AudioTrackInterface> audio_track(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000723 pc_factory_->CreateAudioTrack(label + "a0", NULL));
724 stream->AddTrack(audio_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +0000725 EXPECT_TRUE(pc_->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000726 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
727 observer_.renegotiation_needed_ = false;
728 }
729
730 void AddAudioVideoStream(const std::string& stream_label,
731 const std::string& audio_track_label,
732 const std::string& video_track_label) {
733 // Create a local stream.
zhihuang9763d562016-08-05 11:14:50 -0700734 rtc::scoped_refptr<MediaStreamInterface> stream(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000735 pc_factory_->CreateLocalMediaStream(stream_label));
zhihuang9763d562016-08-05 11:14:50 -0700736 rtc::scoped_refptr<AudioTrackInterface> audio_track(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000737 pc_factory_->CreateAudioTrack(
738 audio_track_label, static_cast<AudioSourceInterface*>(NULL)));
739 stream->AddTrack(audio_track.get());
zhihuang9763d562016-08-05 11:14:50 -0700740 rtc::scoped_refptr<VideoTrackInterface> video_track(
nisseaf510af2016-03-21 08:20:42 -0700741 pc_factory_->CreateVideoTrack(
742 video_track_label,
743 pc_factory_->CreateVideoSource(new cricket::FakeVideoCapturer())));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000744 stream->AddTrack(video_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +0000745 EXPECT_TRUE(pc_->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000746 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
747 observer_.renegotiation_needed_ = false;
748 }
749
kwibergd1fe2812016-04-27 06:47:29 -0700750 bool DoCreateOfferAnswer(std::unique_ptr<SessionDescriptionInterface>* desc,
deadbeefc80741f2015-10-22 13:14:45 -0700751 bool offer,
752 MediaConstraintsInterface* constraints) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000753 rtc::scoped_refptr<MockCreateSessionDescriptionObserver>
754 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000755 MockCreateSessionDescriptionObserver>());
756 if (offer) {
deadbeefc80741f2015-10-22 13:14:45 -0700757 pc_->CreateOffer(observer, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000758 } else {
deadbeefc80741f2015-10-22 13:14:45 -0700759 pc_->CreateAnswer(observer, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000760 }
761 EXPECT_EQ_WAIT(true, observer->called(), kTimeout);
kwiberg2bbff992016-03-16 11:03:04 -0700762 desc->reset(observer->release_desc());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000763 return observer->result();
764 }
765
kwibergd1fe2812016-04-27 06:47:29 -0700766 bool DoCreateOffer(std::unique_ptr<SessionDescriptionInterface>* desc,
deadbeefc80741f2015-10-22 13:14:45 -0700767 MediaConstraintsInterface* constraints) {
768 return DoCreateOfferAnswer(desc, true, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000769 }
770
kwibergd1fe2812016-04-27 06:47:29 -0700771 bool DoCreateAnswer(std::unique_ptr<SessionDescriptionInterface>* desc,
deadbeefc80741f2015-10-22 13:14:45 -0700772 MediaConstraintsInterface* constraints) {
773 return DoCreateOfferAnswer(desc, false, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000774 }
775
776 bool DoSetSessionDescription(SessionDescriptionInterface* desc, bool local) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000777 rtc::scoped_refptr<MockSetSessionDescriptionObserver>
778 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000779 MockSetSessionDescriptionObserver>());
780 if (local) {
781 pc_->SetLocalDescription(observer, desc);
782 } else {
783 pc_->SetRemoteDescription(observer, desc);
784 }
zhihuang29ff8442016-07-27 11:07:25 -0700785 if (pc_->signaling_state() != PeerConnectionInterface::kClosed) {
786 EXPECT_EQ_WAIT(true, observer->called(), kTimeout);
787 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000788 return observer->result();
789 }
790
791 bool DoSetLocalDescription(SessionDescriptionInterface* desc) {
792 return DoSetSessionDescription(desc, true);
793 }
794
795 bool DoSetRemoteDescription(SessionDescriptionInterface* desc) {
796 return DoSetSessionDescription(desc, false);
797 }
798
799 // Calls PeerConnection::GetStats and check the return value.
800 // It does not verify the values in the StatReports since a RTCP packet might
801 // be required.
802 bool DoGetStats(MediaStreamTrackInterface* track) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000803 rtc::scoped_refptr<MockStatsObserver> observer(
804 new rtc::RefCountedObject<MockStatsObserver>());
jiayl@webrtc.orgdb41b4d2014-03-03 21:30:06 +0000805 if (!pc_->GetStats(
806 observer, track, PeerConnectionInterface::kStatsOutputLevelStandard))
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000807 return false;
808 EXPECT_TRUE_WAIT(observer->called(), kTimeout);
809 return observer->called();
810 }
811
812 void InitiateCall() {
813 CreatePeerConnection();
814 // Create a local stream with audio&video tracks.
815 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
816 CreateOfferReceiveAnswer();
817 }
818
819 // Verify that RTP Header extensions has been negotiated for audio and video.
820 void VerifyRemoteRtpHeaderExtensions() {
821 const cricket::MediaContentDescription* desc =
822 cricket::GetFirstAudioContentDescription(
823 pc_->remote_description()->description());
824 ASSERT_TRUE(desc != NULL);
825 EXPECT_GT(desc->rtp_header_extensions().size(), 0u);
826
827 desc = cricket::GetFirstVideoContentDescription(
828 pc_->remote_description()->description());
829 ASSERT_TRUE(desc != NULL);
830 EXPECT_GT(desc->rtp_header_extensions().size(), 0u);
831 }
832
833 void CreateOfferAsRemoteDescription() {
kwibergd1fe2812016-04-27 06:47:29 -0700834 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -0700835 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000836 std::string sdp;
837 EXPECT_TRUE(offer->ToString(&sdp));
838 SessionDescriptionInterface* remote_offer =
839 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
840 sdp, NULL);
841 EXPECT_TRUE(DoSetRemoteDescription(remote_offer));
842 EXPECT_EQ(PeerConnectionInterface::kHaveRemoteOffer, observer_.state_);
843 }
844
deadbeefab9b2d12015-10-14 11:33:11 -0700845 void CreateAndSetRemoteOffer(const std::string& sdp) {
846 SessionDescriptionInterface* remote_offer =
847 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
848 sdp, nullptr);
849 EXPECT_TRUE(DoSetRemoteDescription(remote_offer));
850 EXPECT_EQ(PeerConnectionInterface::kHaveRemoteOffer, observer_.state_);
851 }
852
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000853 void CreateAnswerAsLocalDescription() {
kwibergd1fe2812016-04-27 06:47:29 -0700854 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -0700855 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000856
857 // TODO(perkj): Currently SetLocalDescription fails if any parameters in an
858 // audio codec change, even if the parameter has nothing to do with
859 // receiving. Not all parameters are serialized to SDP.
860 // Since CreatePrAnswerAsLocalDescription serialize/deserialize
861 // the SessionDescription, it is necessary to do that here to in order to
862 // get ReceiveOfferCreatePrAnswerAndAnswer and RenegotiateAudioOnly to pass.
863 // https://code.google.com/p/webrtc/issues/detail?id=1356
864 std::string sdp;
865 EXPECT_TRUE(answer->ToString(&sdp));
866 SessionDescriptionInterface* new_answer =
867 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
868 sdp, NULL);
869 EXPECT_TRUE(DoSetLocalDescription(new_answer));
870 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
871 }
872
873 void CreatePrAnswerAsLocalDescription() {
kwibergd1fe2812016-04-27 06:47:29 -0700874 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -0700875 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000876
877 std::string sdp;
878 EXPECT_TRUE(answer->ToString(&sdp));
879 SessionDescriptionInterface* pr_answer =
880 webrtc::CreateSessionDescription(SessionDescriptionInterface::kPrAnswer,
881 sdp, NULL);
882 EXPECT_TRUE(DoSetLocalDescription(pr_answer));
883 EXPECT_EQ(PeerConnectionInterface::kHaveLocalPrAnswer, observer_.state_);
884 }
885
886 void CreateOfferReceiveAnswer() {
887 CreateOfferAsLocalDescription();
888 std::string sdp;
889 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
890 CreateAnswerAsRemoteDescription(sdp);
891 }
892
893 void CreateOfferAsLocalDescription() {
kwibergd1fe2812016-04-27 06:47:29 -0700894 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -0700895 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000896 // TODO(perkj): Currently SetLocalDescription fails if any parameters in an
897 // audio codec change, even if the parameter has nothing to do with
898 // receiving. Not all parameters are serialized to SDP.
899 // Since CreatePrAnswerAsLocalDescription serialize/deserialize
900 // the SessionDescription, it is necessary to do that here to in order to
901 // get ReceiveOfferCreatePrAnswerAndAnswer and RenegotiateAudioOnly to pass.
902 // https://code.google.com/p/webrtc/issues/detail?id=1356
903 std::string sdp;
904 EXPECT_TRUE(offer->ToString(&sdp));
905 SessionDescriptionInterface* new_offer =
906 webrtc::CreateSessionDescription(
907 SessionDescriptionInterface::kOffer,
908 sdp, NULL);
909
910 EXPECT_TRUE(DoSetLocalDescription(new_offer));
911 EXPECT_EQ(PeerConnectionInterface::kHaveLocalOffer, observer_.state_);
mallinath@webrtc.org68cbd012014-01-22 00:16:46 +0000912 // Wait for the ice_complete message, so that SDP will have candidates.
913 EXPECT_TRUE_WAIT(observer_.ice_complete_, kTimeout);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000914 }
915
deadbeefab9b2d12015-10-14 11:33:11 -0700916 void CreateAnswerAsRemoteDescription(const std::string& sdp) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000917 webrtc::JsepSessionDescription* answer = new webrtc::JsepSessionDescription(
918 SessionDescriptionInterface::kAnswer);
deadbeefab9b2d12015-10-14 11:33:11 -0700919 EXPECT_TRUE(answer->Initialize(sdp, NULL));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000920 EXPECT_TRUE(DoSetRemoteDescription(answer));
921 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
922 }
923
deadbeefab9b2d12015-10-14 11:33:11 -0700924 void CreatePrAnswerAndAnswerAsRemoteDescription(const std::string& sdp) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000925 webrtc::JsepSessionDescription* pr_answer =
926 new webrtc::JsepSessionDescription(
927 SessionDescriptionInterface::kPrAnswer);
deadbeefab9b2d12015-10-14 11:33:11 -0700928 EXPECT_TRUE(pr_answer->Initialize(sdp, NULL));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000929 EXPECT_TRUE(DoSetRemoteDescription(pr_answer));
930 EXPECT_EQ(PeerConnectionInterface::kHaveRemotePrAnswer, observer_.state_);
931 webrtc::JsepSessionDescription* answer =
932 new webrtc::JsepSessionDescription(
933 SessionDescriptionInterface::kAnswer);
deadbeefab9b2d12015-10-14 11:33:11 -0700934 EXPECT_TRUE(answer->Initialize(sdp, NULL));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000935 EXPECT_TRUE(DoSetRemoteDescription(answer));
936 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
937 }
938
939 // Help function used for waiting until a the last signaled remote stream has
940 // the same label as |stream_label|. In a few of the tests in this file we
941 // answer with the same session description as we offer and thus we can
942 // check if OnAddStream have been called with the same stream as we offer to
943 // send.
944 void WaitAndVerifyOnAddStream(const std::string& stream_label) {
945 EXPECT_EQ_WAIT(stream_label, observer_.GetLastAddedStreamLabel(), kTimeout);
946 }
947
948 // Creates an offer and applies it as a local session description.
949 // Creates an answer with the same SDP an the offer but removes all lines
950 // that start with a:ssrc"
951 void CreateOfferReceiveAnswerWithoutSsrc() {
952 CreateOfferAsLocalDescription();
953 std::string sdp;
954 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
955 SetSsrcToZero(&sdp);
956 CreateAnswerAsRemoteDescription(sdp);
957 }
958
deadbeefab9b2d12015-10-14 11:33:11 -0700959 // This function creates a MediaStream with label kStreams[0] and
960 // |number_of_audio_tracks| and |number_of_video_tracks| tracks and the
961 // corresponding SessionDescriptionInterface. The SessionDescriptionInterface
kwiberg2bbff992016-03-16 11:03:04 -0700962 // is returned and the MediaStream is stored in
deadbeefab9b2d12015-10-14 11:33:11 -0700963 // |reference_collection_|
kwibergd1fe2812016-04-27 06:47:29 -0700964 std::unique_ptr<SessionDescriptionInterface>
kwiberg2bbff992016-03-16 11:03:04 -0700965 CreateSessionDescriptionAndReference(size_t number_of_audio_tracks,
966 size_t number_of_video_tracks) {
967 EXPECT_LE(number_of_audio_tracks, 2u);
968 EXPECT_LE(number_of_video_tracks, 2u);
deadbeefab9b2d12015-10-14 11:33:11 -0700969
970 reference_collection_ = StreamCollection::Create();
971 std::string sdp_ms1 = std::string(kSdpStringInit);
972
973 std::string mediastream_label = kStreams[0];
974
975 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream(
976 webrtc::MediaStream::Create(mediastream_label));
977 reference_collection_->AddStream(stream);
978
979 if (number_of_audio_tracks > 0) {
980 sdp_ms1 += std::string(kSdpStringAudio);
981 sdp_ms1 += std::string(kSdpStringMs1Audio0);
982 AddAudioTrack(kAudioTracks[0], stream);
983 }
984 if (number_of_audio_tracks > 1) {
985 sdp_ms1 += kSdpStringMs1Audio1;
986 AddAudioTrack(kAudioTracks[1], stream);
987 }
988
989 if (number_of_video_tracks > 0) {
990 sdp_ms1 += std::string(kSdpStringVideo);
991 sdp_ms1 += std::string(kSdpStringMs1Video0);
992 AddVideoTrack(kVideoTracks[0], stream);
993 }
994 if (number_of_video_tracks > 1) {
995 sdp_ms1 += kSdpStringMs1Video1;
996 AddVideoTrack(kVideoTracks[1], stream);
997 }
998
kwibergd1fe2812016-04-27 06:47:29 -0700999 return std::unique_ptr<SessionDescriptionInterface>(
kwiberg2bbff992016-03-16 11:03:04 -07001000 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
1001 sdp_ms1, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07001002 }
1003
1004 void AddAudioTrack(const std::string& track_id,
1005 MediaStreamInterface* stream) {
1006 rtc::scoped_refptr<webrtc::AudioTrackInterface> audio_track(
1007 webrtc::AudioTrack::Create(track_id, nullptr));
1008 ASSERT_TRUE(stream->AddTrack(audio_track));
1009 }
1010
1011 void AddVideoTrack(const std::string& track_id,
1012 MediaStreamInterface* stream) {
1013 rtc::scoped_refptr<webrtc::VideoTrackInterface> video_track(
nisseaf510af2016-03-21 08:20:42 -07001014 webrtc::VideoTrack::Create(track_id,
1015 webrtc::FakeVideoTrackSource::Create()));
deadbeefab9b2d12015-10-14 11:33:11 -07001016 ASSERT_TRUE(stream->AddTrack(video_track));
1017 }
1018
kwibergfd8be342016-05-14 19:44:11 -07001019 std::unique_ptr<SessionDescriptionInterface> CreateOfferWithOneAudioStream() {
zhihuang8f65cdf2016-05-06 18:40:30 -07001020 CreatePeerConnection();
1021 AddVoiceStream(kStreamLabel1);
kwibergfd8be342016-05-14 19:44:11 -07001022 std::unique_ptr<SessionDescriptionInterface> offer;
zhihuang8f65cdf2016-05-06 18:40:30 -07001023 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
1024 return offer;
1025 }
1026
kwibergfd8be342016-05-14 19:44:11 -07001027 std::unique_ptr<SessionDescriptionInterface>
zhihuang8f65cdf2016-05-06 18:40:30 -07001028 CreateAnswerWithOneAudioStream() {
kwibergfd8be342016-05-14 19:44:11 -07001029 std::unique_ptr<SessionDescriptionInterface> offer =
zhihuang8f65cdf2016-05-06 18:40:30 -07001030 CreateOfferWithOneAudioStream();
1031 EXPECT_TRUE(DoSetRemoteDescription(offer.release()));
kwibergfd8be342016-05-14 19:44:11 -07001032 std::unique_ptr<SessionDescriptionInterface> answer;
zhihuang8f65cdf2016-05-06 18:40:30 -07001033 EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
1034 return answer;
1035 }
1036
1037 const std::string& GetFirstAudioStreamCname(
1038 const SessionDescriptionInterface* desc) {
1039 const cricket::ContentInfo* audio_content =
1040 cricket::GetFirstAudioContent(desc->description());
1041 const cricket::AudioContentDescription* audio_desc =
1042 static_cast<const cricket::AudioContentDescription*>(
1043 audio_content->description);
1044 return audio_desc->streams()[0].cname;
1045 }
1046
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08001047 cricket::FakePortAllocator* port_allocator_ = nullptr;
zhihuang9763d562016-08-05 11:14:50 -07001048 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface> pc_factory_;
1049 rtc::scoped_refptr<PeerConnectionFactoryForTest> pc_factory_for_test_;
1050 rtc::scoped_refptr<PeerConnectionInterface> pc_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001051 MockPeerConnectionObserver observer_;
deadbeefab9b2d12015-10-14 11:33:11 -07001052 rtc::scoped_refptr<StreamCollection> reference_collection_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001053};
1054
zhihuang29ff8442016-07-27 11:07:25 -07001055// Test that no callbacks on the PeerConnectionObserver are called after the
1056// PeerConnection is closed.
1057TEST_F(PeerConnectionInterfaceTest, CloseAndTestCallbackFunctions) {
zhihuang9763d562016-08-05 11:14:50 -07001058 rtc::scoped_refptr<PeerConnectionInterface> pc(
zhihuang29ff8442016-07-27 11:07:25 -07001059 pc_factory_for_test_->CreatePeerConnection(
1060 PeerConnectionInterface::RTCConfiguration(), nullptr, nullptr,
1061 nullptr, &observer_));
1062 observer_.SetPeerConnectionInterface(pc.get());
1063 pc->Close();
1064
1065 // No callbacks is expected to be called.
1066 observer_.callback_triggered = false;
1067 std::vector<cricket::Candidate> candidates;
1068 pc_factory_for_test_->transport_controller->SignalGatheringState(
1069 cricket::IceGatheringState{});
1070 pc_factory_for_test_->transport_controller->SignalCandidatesGathered(
1071 "", candidates);
1072 pc_factory_for_test_->transport_controller->SignalConnectionState(
1073 cricket::IceConnectionState{});
1074 pc_factory_for_test_->transport_controller->SignalCandidatesRemoved(
1075 candidates);
1076 pc_factory_for_test_->transport_controller->SignalReceiving(false);
1077 EXPECT_FALSE(observer_.callback_triggered);
1078}
1079
zhihuang8f65cdf2016-05-06 18:40:30 -07001080// Generate different CNAMEs when PeerConnections are created.
1081// The CNAMEs are expected to be generated randomly. It is possible
1082// that the test fails, though the possibility is very low.
1083TEST_F(PeerConnectionInterfaceTest, CnameGenerationInOffer) {
kwibergfd8be342016-05-14 19:44:11 -07001084 std::unique_ptr<SessionDescriptionInterface> offer1 =
zhihuang8f65cdf2016-05-06 18:40:30 -07001085 CreateOfferWithOneAudioStream();
kwibergfd8be342016-05-14 19:44:11 -07001086 std::unique_ptr<SessionDescriptionInterface> offer2 =
zhihuang8f65cdf2016-05-06 18:40:30 -07001087 CreateOfferWithOneAudioStream();
1088 EXPECT_NE(GetFirstAudioStreamCname(offer1.get()),
1089 GetFirstAudioStreamCname(offer2.get()));
1090}
1091
1092TEST_F(PeerConnectionInterfaceTest, CnameGenerationInAnswer) {
kwibergfd8be342016-05-14 19:44:11 -07001093 std::unique_ptr<SessionDescriptionInterface> answer1 =
zhihuang8f65cdf2016-05-06 18:40:30 -07001094 CreateAnswerWithOneAudioStream();
kwibergfd8be342016-05-14 19:44:11 -07001095 std::unique_ptr<SessionDescriptionInterface> answer2 =
zhihuang8f65cdf2016-05-06 18:40:30 -07001096 CreateAnswerWithOneAudioStream();
1097 EXPECT_NE(GetFirstAudioStreamCname(answer1.get()),
1098 GetFirstAudioStreamCname(answer2.get()));
1099}
1100
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001101TEST_F(PeerConnectionInterfaceTest,
1102 CreatePeerConnectionWithDifferentConfigurations) {
1103 CreatePeerConnectionWithDifferentConfigurations();
1104}
1105
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001106TEST_F(PeerConnectionInterfaceTest,
1107 CreatePeerConnectionWithDifferentIceTransportsTypes) {
1108 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kNone);
1109 EXPECT_EQ(cricket::CF_NONE, port_allocator_->candidate_filter());
1110 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kRelay);
1111 EXPECT_EQ(cricket::CF_RELAY, port_allocator_->candidate_filter());
1112 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kNoHost);
1113 EXPECT_EQ(cricket::CF_ALL & ~cricket::CF_HOST,
1114 port_allocator_->candidate_filter());
1115 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kAll);
1116 EXPECT_EQ(cricket::CF_ALL, port_allocator_->candidate_filter());
1117}
1118
1119// Test that when a PeerConnection is created with a nonzero candidate pool
1120// size, the pooled PortAllocatorSession is created with all the attributes
1121// in the RTCConfiguration.
1122TEST_F(PeerConnectionInterfaceTest, CreatePeerConnectionWithPooledCandidates) {
1123 PeerConnectionInterface::RTCConfiguration config;
1124 PeerConnectionInterface::IceServer server;
1125 server.uri = kStunAddressOnly;
1126 config.servers.push_back(server);
1127 config.type = PeerConnectionInterface::kRelay;
1128 config.disable_ipv6 = true;
1129 config.tcp_candidate_policy =
1130 PeerConnectionInterface::kTcpCandidatePolicyDisabled;
honghaiz60347052016-05-31 18:29:12 -07001131 config.candidate_network_policy =
1132 PeerConnectionInterface::kCandidateNetworkPolicyLowCost;
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001133 config.ice_candidate_pool_size = 1;
1134 CreatePeerConnection(config, nullptr);
1135
1136 const cricket::FakePortAllocatorSession* session =
1137 static_cast<const cricket::FakePortAllocatorSession*>(
1138 port_allocator_->GetPooledSession());
1139 ASSERT_NE(nullptr, session);
1140 EXPECT_EQ(1UL, session->stun_servers().size());
1141 EXPECT_EQ(0U, session->flags() & cricket::PORTALLOCATOR_ENABLE_IPV6);
1142 EXPECT_LT(0U, session->flags() & cricket::PORTALLOCATOR_DISABLE_TCP);
honghaiz60347052016-05-31 18:29:12 -07001143 EXPECT_LT(0U,
1144 session->flags() & cricket::PORTALLOCATOR_DISABLE_COSTLY_NETWORKS);
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001145}
1146
Taylor Brandstetterf8e65772016-06-27 17:20:15 -07001147// Test that the PeerConnection initializes the port allocator passed into it,
1148// and on the correct thread.
1149TEST_F(PeerConnectionInterfaceTest,
1150 CreatePeerConnectionInitializesPortAllocator) {
1151 rtc::Thread network_thread;
1152 network_thread.Start();
1153 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface> pc_factory(
1154 webrtc::CreatePeerConnectionFactory(
1155 &network_thread, rtc::Thread::Current(), rtc::Thread::Current(),
1156 nullptr, nullptr, nullptr));
1157 std::unique_ptr<cricket::FakePortAllocator> port_allocator(
1158 new cricket::FakePortAllocator(&network_thread, nullptr));
1159 cricket::FakePortAllocator* raw_port_allocator = port_allocator.get();
1160 PeerConnectionInterface::RTCConfiguration config;
1161 rtc::scoped_refptr<PeerConnectionInterface> pc(
1162 pc_factory->CreatePeerConnection(
1163 config, nullptr, std::move(port_allocator), nullptr, &observer_));
1164 // FakePortAllocator RTC_CHECKs that it's initialized on the right thread,
1165 // so all we have to do here is check that it's initialized.
1166 EXPECT_TRUE(raw_port_allocator->initialized());
1167}
1168
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001169TEST_F(PeerConnectionInterfaceTest, AddStreams) {
1170 CreatePeerConnection();
deadbeefab9b2d12015-10-14 11:33:11 -07001171 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001172 AddVoiceStream(kStreamLabel2);
1173 ASSERT_EQ(2u, pc_->local_streams()->count());
1174
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001175 // Test we can add multiple local streams to one peerconnection.
zhihuang9763d562016-08-05 11:14:50 -07001176 rtc::scoped_refptr<MediaStreamInterface> stream(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001177 pc_factory_->CreateLocalMediaStream(kStreamLabel3));
zhihuang9763d562016-08-05 11:14:50 -07001178 rtc::scoped_refptr<AudioTrackInterface> audio_track(
1179 pc_factory_->CreateAudioTrack(kStreamLabel3,
1180 static_cast<AudioSourceInterface*>(NULL)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001181 stream->AddTrack(audio_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +00001182 EXPECT_TRUE(pc_->AddStream(stream));
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001183 EXPECT_EQ(3u, pc_->local_streams()->count());
1184
1185 // Remove the third stream.
1186 pc_->RemoveStream(pc_->local_streams()->at(2));
1187 EXPECT_EQ(2u, pc_->local_streams()->count());
1188
1189 // Remove the second stream.
1190 pc_->RemoveStream(pc_->local_streams()->at(1));
1191 EXPECT_EQ(1u, pc_->local_streams()->count());
1192
1193 // Remove the first stream.
1194 pc_->RemoveStream(pc_->local_streams()->at(0));
1195 EXPECT_EQ(0u, pc_->local_streams()->count());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001196}
1197
deadbeefab9b2d12015-10-14 11:33:11 -07001198// Test that the created offer includes streams we added.
1199TEST_F(PeerConnectionInterfaceTest, AddedStreamsPresentInOffer) {
1200 CreatePeerConnection();
1201 AddAudioVideoStream(kStreamLabel1, "audio_track", "video_track");
kwibergd1fe2812016-04-27 06:47:29 -07001202 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001203 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07001204
1205 const cricket::ContentInfo* audio_content =
1206 cricket::GetFirstAudioContent(offer->description());
1207 const cricket::AudioContentDescription* audio_desc =
1208 static_cast<const cricket::AudioContentDescription*>(
1209 audio_content->description);
1210 EXPECT_TRUE(
1211 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
1212
1213 const cricket::ContentInfo* video_content =
1214 cricket::GetFirstVideoContent(offer->description());
1215 const cricket::VideoContentDescription* video_desc =
1216 static_cast<const cricket::VideoContentDescription*>(
1217 video_content->description);
1218 EXPECT_TRUE(
1219 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1220
1221 // Add another stream and ensure the offer includes both the old and new
1222 // streams.
1223 AddAudioVideoStream(kStreamLabel2, "audio_track2", "video_track2");
kwiberg2bbff992016-03-16 11:03:04 -07001224 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07001225
1226 audio_content = cricket::GetFirstAudioContent(offer->description());
1227 audio_desc = static_cast<const cricket::AudioContentDescription*>(
1228 audio_content->description);
1229 EXPECT_TRUE(
1230 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
1231 EXPECT_TRUE(
1232 ContainsTrack(audio_desc->streams(), kStreamLabel2, "audio_track2"));
1233
1234 video_content = cricket::GetFirstVideoContent(offer->description());
1235 video_desc = static_cast<const cricket::VideoContentDescription*>(
1236 video_content->description);
1237 EXPECT_TRUE(
1238 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1239 EXPECT_TRUE(
1240 ContainsTrack(video_desc->streams(), kStreamLabel2, "video_track2"));
1241}
1242
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001243TEST_F(PeerConnectionInterfaceTest, RemoveStream) {
1244 CreatePeerConnection();
deadbeefab9b2d12015-10-14 11:33:11 -07001245 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001246 ASSERT_EQ(1u, pc_->local_streams()->count());
1247 pc_->RemoveStream(pc_->local_streams()->at(0));
1248 EXPECT_EQ(0u, pc_->local_streams()->count());
1249}
1250
deadbeefe1f9d832016-01-14 15:35:42 -08001251// Test for AddTrack and RemoveTrack methods.
1252// Tests that the created offer includes tracks we added,
1253// and that the RtpSenders are created correctly.
1254// Also tests that RemoveTrack removes the tracks from subsequent offers.
1255TEST_F(PeerConnectionInterfaceTest, AddTrackRemoveTrack) {
1256 CreatePeerConnection();
1257 // Create a dummy stream, so tracks share a stream label.
zhihuang9763d562016-08-05 11:14:50 -07001258 rtc::scoped_refptr<MediaStreamInterface> stream(
deadbeefe1f9d832016-01-14 15:35:42 -08001259 pc_factory_->CreateLocalMediaStream(kStreamLabel1));
1260 std::vector<MediaStreamInterface*> stream_list;
1261 stream_list.push_back(stream.get());
zhihuang9763d562016-08-05 11:14:50 -07001262 rtc::scoped_refptr<AudioTrackInterface> audio_track(
deadbeefe1f9d832016-01-14 15:35:42 -08001263 pc_factory_->CreateAudioTrack("audio_track", nullptr));
zhihuang9763d562016-08-05 11:14:50 -07001264 rtc::scoped_refptr<VideoTrackInterface> video_track(
1265 pc_factory_->CreateVideoTrack(
1266 "video_track",
1267 pc_factory_->CreateVideoSource(new cricket::FakeVideoCapturer())));
deadbeefe1f9d832016-01-14 15:35:42 -08001268 auto audio_sender = pc_->AddTrack(audio_track, stream_list);
1269 auto video_sender = pc_->AddTrack(video_track, stream_list);
deadbeefa601f5c2016-06-06 14:27:39 -07001270 EXPECT_EQ(1UL, audio_sender->stream_ids().size());
1271 EXPECT_EQ(kStreamLabel1, audio_sender->stream_ids()[0]);
deadbeefe1f9d832016-01-14 15:35:42 -08001272 EXPECT_EQ("audio_track", audio_sender->id());
1273 EXPECT_EQ(audio_track, audio_sender->track());
deadbeefa601f5c2016-06-06 14:27:39 -07001274 EXPECT_EQ(1UL, video_sender->stream_ids().size());
1275 EXPECT_EQ(kStreamLabel1, video_sender->stream_ids()[0]);
deadbeefe1f9d832016-01-14 15:35:42 -08001276 EXPECT_EQ("video_track", video_sender->id());
1277 EXPECT_EQ(video_track, video_sender->track());
1278
1279 // Now create an offer and check for the senders.
kwibergd1fe2812016-04-27 06:47:29 -07001280 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001281 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefe1f9d832016-01-14 15:35:42 -08001282
1283 const cricket::ContentInfo* audio_content =
1284 cricket::GetFirstAudioContent(offer->description());
1285 const cricket::AudioContentDescription* audio_desc =
1286 static_cast<const cricket::AudioContentDescription*>(
1287 audio_content->description);
1288 EXPECT_TRUE(
1289 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
1290
1291 const cricket::ContentInfo* video_content =
1292 cricket::GetFirstVideoContent(offer->description());
1293 const cricket::VideoContentDescription* video_desc =
1294 static_cast<const cricket::VideoContentDescription*>(
1295 video_content->description);
1296 EXPECT_TRUE(
1297 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1298
1299 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
1300
1301 // Now try removing the tracks.
1302 EXPECT_TRUE(pc_->RemoveTrack(audio_sender));
1303 EXPECT_TRUE(pc_->RemoveTrack(video_sender));
1304
1305 // Create a new offer and ensure it doesn't contain the removed senders.
kwiberg2bbff992016-03-16 11:03:04 -07001306 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefe1f9d832016-01-14 15:35:42 -08001307
1308 audio_content = cricket::GetFirstAudioContent(offer->description());
1309 audio_desc = static_cast<const cricket::AudioContentDescription*>(
1310 audio_content->description);
1311 EXPECT_FALSE(
1312 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
1313
1314 video_content = cricket::GetFirstVideoContent(offer->description());
1315 video_desc = static_cast<const cricket::VideoContentDescription*>(
1316 video_content->description);
1317 EXPECT_FALSE(
1318 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1319
1320 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
1321
1322 // Calling RemoveTrack on a sender no longer attached to a PeerConnection
1323 // should return false.
1324 EXPECT_FALSE(pc_->RemoveTrack(audio_sender));
1325 EXPECT_FALSE(pc_->RemoveTrack(video_sender));
1326}
1327
1328// Test creating senders without a stream specified,
1329// expecting a random stream ID to be generated.
1330TEST_F(PeerConnectionInterfaceTest, AddTrackWithoutStream) {
1331 CreatePeerConnection();
1332 // Create a dummy stream, so tracks share a stream label.
zhihuang9763d562016-08-05 11:14:50 -07001333 rtc::scoped_refptr<AudioTrackInterface> audio_track(
deadbeefe1f9d832016-01-14 15:35:42 -08001334 pc_factory_->CreateAudioTrack("audio_track", nullptr));
zhihuang9763d562016-08-05 11:14:50 -07001335 rtc::scoped_refptr<VideoTrackInterface> video_track(
1336 pc_factory_->CreateVideoTrack(
1337 "video_track",
1338 pc_factory_->CreateVideoSource(new cricket::FakeVideoCapturer())));
deadbeefe1f9d832016-01-14 15:35:42 -08001339 auto audio_sender =
1340 pc_->AddTrack(audio_track, std::vector<MediaStreamInterface*>());
1341 auto video_sender =
1342 pc_->AddTrack(video_track, std::vector<MediaStreamInterface*>());
1343 EXPECT_EQ("audio_track", audio_sender->id());
1344 EXPECT_EQ(audio_track, audio_sender->track());
1345 EXPECT_EQ("video_track", video_sender->id());
1346 EXPECT_EQ(video_track, video_sender->track());
1347 // If the ID is truly a random GUID, it should be infinitely unlikely they
1348 // will be the same.
deadbeefa601f5c2016-06-06 14:27:39 -07001349 EXPECT_NE(video_sender->stream_ids(), audio_sender->stream_ids());
deadbeefe1f9d832016-01-14 15:35:42 -08001350}
1351
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001352TEST_F(PeerConnectionInterfaceTest, CreateOfferReceiveAnswer) {
1353 InitiateCall();
1354 WaitAndVerifyOnAddStream(kStreamLabel1);
1355 VerifyRemoteRtpHeaderExtensions();
1356}
1357
1358TEST_F(PeerConnectionInterfaceTest, CreateOfferReceivePrAnswerAndAnswer) {
1359 CreatePeerConnection();
deadbeefab9b2d12015-10-14 11:33:11 -07001360 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001361 CreateOfferAsLocalDescription();
1362 std::string offer;
1363 EXPECT_TRUE(pc_->local_description()->ToString(&offer));
1364 CreatePrAnswerAndAnswerAsRemoteDescription(offer);
1365 WaitAndVerifyOnAddStream(kStreamLabel1);
1366}
1367
1368TEST_F(PeerConnectionInterfaceTest, ReceiveOfferCreateAnswer) {
1369 CreatePeerConnection();
deadbeefab9b2d12015-10-14 11:33:11 -07001370 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001371
1372 CreateOfferAsRemoteDescription();
1373 CreateAnswerAsLocalDescription();
1374
1375 WaitAndVerifyOnAddStream(kStreamLabel1);
1376}
1377
1378TEST_F(PeerConnectionInterfaceTest, ReceiveOfferCreatePrAnswerAndAnswer) {
1379 CreatePeerConnection();
deadbeefab9b2d12015-10-14 11:33:11 -07001380 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001381
1382 CreateOfferAsRemoteDescription();
1383 CreatePrAnswerAsLocalDescription();
1384 CreateAnswerAsLocalDescription();
1385
1386 WaitAndVerifyOnAddStream(kStreamLabel1);
1387}
1388
1389TEST_F(PeerConnectionInterfaceTest, Renegotiate) {
1390 InitiateCall();
1391 ASSERT_EQ(1u, pc_->remote_streams()->count());
1392 pc_->RemoveStream(pc_->local_streams()->at(0));
1393 CreateOfferReceiveAnswer();
1394 EXPECT_EQ(0u, pc_->remote_streams()->count());
deadbeefab9b2d12015-10-14 11:33:11 -07001395 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001396 CreateOfferReceiveAnswer();
1397}
1398
1399// Tests that after negotiating an audio only call, the respondent can perform a
1400// renegotiation that removes the audio stream.
1401TEST_F(PeerConnectionInterfaceTest, RenegotiateAudioOnly) {
1402 CreatePeerConnection();
1403 AddVoiceStream(kStreamLabel1);
1404 CreateOfferAsRemoteDescription();
1405 CreateAnswerAsLocalDescription();
1406
1407 ASSERT_EQ(1u, pc_->remote_streams()->count());
1408 pc_->RemoveStream(pc_->local_streams()->at(0));
1409 CreateOfferReceiveAnswer();
1410 EXPECT_EQ(0u, pc_->remote_streams()->count());
1411}
1412
1413// Test that candidates are generated and that we can parse our own candidates.
1414TEST_F(PeerConnectionInterfaceTest, IceCandidates) {
1415 CreatePeerConnection();
1416
1417 EXPECT_FALSE(pc_->AddIceCandidate(observer_.last_candidate_.get()));
1418 // SetRemoteDescription takes ownership of offer.
kwibergd1fe2812016-04-27 06:47:29 -07001419 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefab9b2d12015-10-14 11:33:11 -07001420 AddVideoStream(kStreamLabel1);
deadbeefc80741f2015-10-22 13:14:45 -07001421 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
kwiberg2bbff992016-03-16 11:03:04 -07001422 EXPECT_TRUE(DoSetRemoteDescription(offer.release()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001423
1424 // SetLocalDescription takes ownership of answer.
kwibergd1fe2812016-04-27 06:47:29 -07001425 std::unique_ptr<SessionDescriptionInterface> answer;
deadbeefc80741f2015-10-22 13:14:45 -07001426 EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
kwiberg2bbff992016-03-16 11:03:04 -07001427 EXPECT_TRUE(DoSetLocalDescription(answer.release()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001428
1429 EXPECT_TRUE_WAIT(observer_.last_candidate_.get() != NULL, kTimeout);
1430 EXPECT_TRUE_WAIT(observer_.ice_complete_, kTimeout);
1431
1432 EXPECT_TRUE(pc_->AddIceCandidate(observer_.last_candidate_.get()));
1433}
1434
deadbeefab9b2d12015-10-14 11:33:11 -07001435// Test that CreateOffer and CreateAnswer will fail if the track labels are
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001436// not unique.
1437TEST_F(PeerConnectionInterfaceTest, CreateOfferAnswerWithInvalidStream) {
1438 CreatePeerConnection();
1439 // Create a regular offer for the CreateAnswer test later.
kwibergd1fe2812016-04-27 06:47:29 -07001440 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefc80741f2015-10-22 13:14:45 -07001441 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
kwiberg2bbff992016-03-16 11:03:04 -07001442 EXPECT_TRUE(offer);
1443 offer.reset();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001444
1445 // Create a local stream with audio&video tracks having same label.
1446 AddAudioVideoStream(kStreamLabel1, "track_label", "track_label");
1447
1448 // Test CreateOffer
deadbeefc80741f2015-10-22 13:14:45 -07001449 EXPECT_FALSE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001450
1451 // Test CreateAnswer
kwibergd1fe2812016-04-27 06:47:29 -07001452 std::unique_ptr<SessionDescriptionInterface> answer;
deadbeefc80741f2015-10-22 13:14:45 -07001453 EXPECT_FALSE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001454}
1455
1456// Test that we will get different SSRCs for each tracks in the offer and answer
1457// we created.
1458TEST_F(PeerConnectionInterfaceTest, SsrcInOfferAnswer) {
1459 CreatePeerConnection();
1460 // Create a local stream with audio&video tracks having different labels.
1461 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
1462
1463 // Test CreateOffer
kwibergd1fe2812016-04-27 06:47:29 -07001464 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001465 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001466 int audio_ssrc = 0;
1467 int video_ssrc = 0;
1468 EXPECT_TRUE(GetFirstSsrc(GetFirstAudioContent(offer->description()),
1469 &audio_ssrc));
1470 EXPECT_TRUE(GetFirstSsrc(GetFirstVideoContent(offer->description()),
1471 &video_ssrc));
1472 EXPECT_NE(audio_ssrc, video_ssrc);
1473
1474 // Test CreateAnswer
1475 EXPECT_TRUE(DoSetRemoteDescription(offer.release()));
kwibergd1fe2812016-04-27 06:47:29 -07001476 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -07001477 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001478 audio_ssrc = 0;
1479 video_ssrc = 0;
1480 EXPECT_TRUE(GetFirstSsrc(GetFirstAudioContent(answer->description()),
1481 &audio_ssrc));
1482 EXPECT_TRUE(GetFirstSsrc(GetFirstVideoContent(answer->description()),
1483 &video_ssrc));
1484 EXPECT_NE(audio_ssrc, video_ssrc);
1485}
1486
deadbeefeb459812015-12-15 19:24:43 -08001487// Test that it's possible to call AddTrack on a MediaStream after adding
1488// the stream to a PeerConnection.
1489// TODO(deadbeef): Remove this test once this behavior is no longer supported.
1490TEST_F(PeerConnectionInterfaceTest, AddTrackAfterAddStream) {
1491 CreatePeerConnection();
1492 // Create audio stream and add to PeerConnection.
1493 AddVoiceStream(kStreamLabel1);
1494 MediaStreamInterface* stream = pc_->local_streams()->at(0);
1495
1496 // Add video track to the audio-only stream.
zhihuang9763d562016-08-05 11:14:50 -07001497 rtc::scoped_refptr<VideoTrackInterface> video_track(
1498 pc_factory_->CreateVideoTrack(
1499 "video_label",
1500 pc_factory_->CreateVideoSource(new cricket::FakeVideoCapturer())));
deadbeefeb459812015-12-15 19:24:43 -08001501 stream->AddTrack(video_track.get());
1502
kwibergd1fe2812016-04-27 06:47:29 -07001503 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001504 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefeb459812015-12-15 19:24:43 -08001505
1506 const cricket::MediaContentDescription* video_desc =
1507 cricket::GetFirstVideoContentDescription(offer->description());
1508 EXPECT_TRUE(video_desc != nullptr);
1509}
1510
1511// Test that it's possible to call RemoveTrack on a MediaStream after adding
1512// the stream to a PeerConnection.
1513// TODO(deadbeef): Remove this test once this behavior is no longer supported.
1514TEST_F(PeerConnectionInterfaceTest, RemoveTrackAfterAddStream) {
1515 CreatePeerConnection();
1516 // Create audio/video stream and add to PeerConnection.
1517 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
1518 MediaStreamInterface* stream = pc_->local_streams()->at(0);
1519
1520 // Remove the video track.
1521 stream->RemoveTrack(stream->GetVideoTracks()[0]);
1522
kwibergd1fe2812016-04-27 06:47:29 -07001523 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001524 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefeb459812015-12-15 19:24:43 -08001525
1526 const cricket::MediaContentDescription* video_desc =
1527 cricket::GetFirstVideoContentDescription(offer->description());
1528 EXPECT_TRUE(video_desc == nullptr);
1529}
1530
deadbeefbd7d8f72015-12-18 16:58:44 -08001531// Test creating a sender with a stream ID, and ensure the ID is populated
1532// in the offer.
1533TEST_F(PeerConnectionInterfaceTest, CreateSenderWithStream) {
1534 CreatePeerConnection();
1535 pc_->CreateSender("video", kStreamLabel1);
1536
kwibergd1fe2812016-04-27 06:47:29 -07001537 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001538 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefbd7d8f72015-12-18 16:58:44 -08001539
1540 const cricket::MediaContentDescription* video_desc =
1541 cricket::GetFirstVideoContentDescription(offer->description());
1542 ASSERT_TRUE(video_desc != nullptr);
1543 ASSERT_EQ(1u, video_desc->streams().size());
1544 EXPECT_EQ(kStreamLabel1, video_desc->streams()[0].sync_label);
1545}
1546
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001547// Test that we can specify a certain track that we want statistics about.
1548TEST_F(PeerConnectionInterfaceTest, GetStatsForSpecificTrack) {
1549 InitiateCall();
1550 ASSERT_LT(0u, pc_->remote_streams()->count());
1551 ASSERT_LT(0u, pc_->remote_streams()->at(0)->GetAudioTracks().size());
zhihuang9763d562016-08-05 11:14:50 -07001552 rtc::scoped_refptr<MediaStreamTrackInterface> remote_audio =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001553 pc_->remote_streams()->at(0)->GetAudioTracks()[0];
1554 EXPECT_TRUE(DoGetStats(remote_audio));
1555
1556 // Remove the stream. Since we are sending to our selves the local
1557 // and the remote stream is the same.
1558 pc_->RemoveStream(pc_->local_streams()->at(0));
1559 // Do a re-negotiation.
1560 CreateOfferReceiveAnswer();
1561
1562 ASSERT_EQ(0u, pc_->remote_streams()->count());
1563
1564 // Test that we still can get statistics for the old track. Even if it is not
1565 // sent any longer.
1566 EXPECT_TRUE(DoGetStats(remote_audio));
1567}
1568
1569// Test that we can get stats on a video track.
1570TEST_F(PeerConnectionInterfaceTest, GetStatsForVideoTrack) {
1571 InitiateCall();
1572 ASSERT_LT(0u, pc_->remote_streams()->count());
1573 ASSERT_LT(0u, pc_->remote_streams()->at(0)->GetVideoTracks().size());
zhihuang9763d562016-08-05 11:14:50 -07001574 rtc::scoped_refptr<MediaStreamTrackInterface> remote_video =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001575 pc_->remote_streams()->at(0)->GetVideoTracks()[0];
1576 EXPECT_TRUE(DoGetStats(remote_video));
1577}
1578
1579// Test that we don't get statistics for an invalid track.
tommi@webrtc.org908f57e2014-07-21 11:44:39 +00001580// TODO(tommi): Fix this test. DoGetStats will return true
1581// for the unknown track (since GetStats is async), but no
1582// data is returned for the track.
1583TEST_F(PeerConnectionInterfaceTest, DISABLED_GetStatsForInvalidTrack) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001584 InitiateCall();
zhihuang9763d562016-08-05 11:14:50 -07001585 rtc::scoped_refptr<AudioTrackInterface> unknown_audio_track(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001586 pc_factory_->CreateAudioTrack("unknown track", NULL));
1587 EXPECT_FALSE(DoGetStats(unknown_audio_track));
1588}
1589
1590// This test setup two RTP data channels in loop back.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001591TEST_F(PeerConnectionInterfaceTest, TestDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001592 FakeConstraints constraints;
1593 constraints.SetAllowRtpDataChannels();
1594 CreatePeerConnection(&constraints);
zhihuang9763d562016-08-05 11:14:50 -07001595 rtc::scoped_refptr<DataChannelInterface> data1 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001596 pc_->CreateDataChannel("test1", NULL);
zhihuang9763d562016-08-05 11:14:50 -07001597 rtc::scoped_refptr<DataChannelInterface> data2 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001598 pc_->CreateDataChannel("test2", NULL);
1599 ASSERT_TRUE(data1 != NULL);
kwibergd1fe2812016-04-27 06:47:29 -07001600 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001601 new MockDataChannelObserver(data1));
kwibergd1fe2812016-04-27 06:47:29 -07001602 std::unique_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001603 new MockDataChannelObserver(data2));
1604
1605 EXPECT_EQ(DataChannelInterface::kConnecting, data1->state());
1606 EXPECT_EQ(DataChannelInterface::kConnecting, data2->state());
1607 std::string data_to_send1 = "testing testing";
1608 std::string data_to_send2 = "testing something else";
1609 EXPECT_FALSE(data1->Send(DataBuffer(data_to_send1)));
1610
1611 CreateOfferReceiveAnswer();
1612 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1613 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
1614
1615 EXPECT_EQ(DataChannelInterface::kOpen, data1->state());
1616 EXPECT_EQ(DataChannelInterface::kOpen, data2->state());
1617 EXPECT_TRUE(data1->Send(DataBuffer(data_to_send1)));
1618 EXPECT_TRUE(data2->Send(DataBuffer(data_to_send2)));
1619
1620 EXPECT_EQ_WAIT(data_to_send1, observer1->last_message(), kTimeout);
1621 EXPECT_EQ_WAIT(data_to_send2, observer2->last_message(), kTimeout);
1622
1623 data1->Close();
1624 EXPECT_EQ(DataChannelInterface::kClosing, data1->state());
1625 CreateOfferReceiveAnswer();
1626 EXPECT_FALSE(observer1->IsOpen());
1627 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
1628 EXPECT_TRUE(observer2->IsOpen());
1629
1630 data_to_send2 = "testing something else again";
1631 EXPECT_TRUE(data2->Send(DataBuffer(data_to_send2)));
1632
1633 EXPECT_EQ_WAIT(data_to_send2, observer2->last_message(), kTimeout);
1634}
1635
1636// This test verifies that sendnig binary data over RTP data channels should
1637// fail.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001638TEST_F(PeerConnectionInterfaceTest, TestSendBinaryOnRtpDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001639 FakeConstraints constraints;
1640 constraints.SetAllowRtpDataChannels();
1641 CreatePeerConnection(&constraints);
zhihuang9763d562016-08-05 11:14:50 -07001642 rtc::scoped_refptr<DataChannelInterface> data1 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001643 pc_->CreateDataChannel("test1", NULL);
zhihuang9763d562016-08-05 11:14:50 -07001644 rtc::scoped_refptr<DataChannelInterface> data2 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001645 pc_->CreateDataChannel("test2", NULL);
1646 ASSERT_TRUE(data1 != NULL);
kwibergd1fe2812016-04-27 06:47:29 -07001647 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001648 new MockDataChannelObserver(data1));
kwibergd1fe2812016-04-27 06:47:29 -07001649 std::unique_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001650 new MockDataChannelObserver(data2));
1651
1652 EXPECT_EQ(DataChannelInterface::kConnecting, data1->state());
1653 EXPECT_EQ(DataChannelInterface::kConnecting, data2->state());
1654
1655 CreateOfferReceiveAnswer();
1656 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1657 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
1658
1659 EXPECT_EQ(DataChannelInterface::kOpen, data1->state());
1660 EXPECT_EQ(DataChannelInterface::kOpen, data2->state());
1661
jbaucheec21bd2016-03-20 06:15:43 -07001662 rtc::CopyOnWriteBuffer buffer("test", 4);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001663 EXPECT_FALSE(data1->Send(DataBuffer(buffer, true)));
1664}
1665
1666// This test setup a RTP data channels in loop back and test that a channel is
1667// opened even if the remote end answer with a zero SSRC.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001668TEST_F(PeerConnectionInterfaceTest, TestSendOnlyDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001669 FakeConstraints constraints;
1670 constraints.SetAllowRtpDataChannels();
1671 CreatePeerConnection(&constraints);
zhihuang9763d562016-08-05 11:14:50 -07001672 rtc::scoped_refptr<DataChannelInterface> data1 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001673 pc_->CreateDataChannel("test1", NULL);
kwibergd1fe2812016-04-27 06:47:29 -07001674 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001675 new MockDataChannelObserver(data1));
1676
1677 CreateOfferReceiveAnswerWithoutSsrc();
1678
1679 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1680
1681 data1->Close();
1682 EXPECT_EQ(DataChannelInterface::kClosing, data1->state());
1683 CreateOfferReceiveAnswerWithoutSsrc();
1684 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
1685 EXPECT_FALSE(observer1->IsOpen());
1686}
1687
1688// This test that if a data channel is added in an answer a receive only channel
1689// channel is created.
1690TEST_F(PeerConnectionInterfaceTest, TestReceiveOnlyDataChannel) {
1691 FakeConstraints constraints;
1692 constraints.SetAllowRtpDataChannels();
1693 CreatePeerConnection(&constraints);
1694
1695 std::string offer_label = "offer_channel";
zhihuang9763d562016-08-05 11:14:50 -07001696 rtc::scoped_refptr<DataChannelInterface> offer_channel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001697 pc_->CreateDataChannel(offer_label, NULL);
1698
1699 CreateOfferAsLocalDescription();
1700
1701 // Replace the data channel label in the offer and apply it as an answer.
1702 std::string receive_label = "answer_channel";
1703 std::string sdp;
1704 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001705 rtc::replace_substrs(offer_label.c_str(), offer_label.length(),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001706 receive_label.c_str(), receive_label.length(),
1707 &sdp);
1708 CreateAnswerAsRemoteDescription(sdp);
1709
1710 // Verify that a new incoming data channel has been created and that
1711 // it is open but can't we written to.
1712 ASSERT_TRUE(observer_.last_datachannel_ != NULL);
1713 DataChannelInterface* received_channel = observer_.last_datachannel_;
1714 EXPECT_EQ(DataChannelInterface::kConnecting, received_channel->state());
1715 EXPECT_EQ(receive_label, received_channel->label());
1716 EXPECT_FALSE(received_channel->Send(DataBuffer("something")));
1717
1718 // Verify that the channel we initially offered has been rejected.
1719 EXPECT_EQ(DataChannelInterface::kClosed, offer_channel->state());
1720
1721 // Do another offer / answer exchange and verify that the data channel is
1722 // opened.
1723 CreateOfferReceiveAnswer();
1724 EXPECT_EQ_WAIT(DataChannelInterface::kOpen, received_channel->state(),
1725 kTimeout);
1726}
1727
1728// This test that no data channel is returned if a reliable channel is
1729// requested.
1730// TODO(perkj): Remove this test once reliable channels are implemented.
1731TEST_F(PeerConnectionInterfaceTest, CreateReliableRtpDataChannelShouldFail) {
1732 FakeConstraints constraints;
1733 constraints.SetAllowRtpDataChannels();
1734 CreatePeerConnection(&constraints);
1735
1736 std::string label = "test";
1737 webrtc::DataChannelInit config;
1738 config.reliable = true;
zhihuang9763d562016-08-05 11:14:50 -07001739 rtc::scoped_refptr<DataChannelInterface> channel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001740 pc_->CreateDataChannel(label, &config);
1741 EXPECT_TRUE(channel == NULL);
1742}
1743
deadbeefab9b2d12015-10-14 11:33:11 -07001744// Verifies that duplicated label is not allowed for RTP data channel.
1745TEST_F(PeerConnectionInterfaceTest, RtpDuplicatedLabelNotAllowed) {
1746 FakeConstraints constraints;
1747 constraints.SetAllowRtpDataChannels();
1748 CreatePeerConnection(&constraints);
1749
1750 std::string label = "test";
zhihuang9763d562016-08-05 11:14:50 -07001751 rtc::scoped_refptr<DataChannelInterface> channel =
deadbeefab9b2d12015-10-14 11:33:11 -07001752 pc_->CreateDataChannel(label, nullptr);
1753 EXPECT_NE(channel, nullptr);
1754
zhihuang9763d562016-08-05 11:14:50 -07001755 rtc::scoped_refptr<DataChannelInterface> dup_channel =
deadbeefab9b2d12015-10-14 11:33:11 -07001756 pc_->CreateDataChannel(label, nullptr);
1757 EXPECT_EQ(dup_channel, nullptr);
1758}
1759
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001760// This tests that a SCTP data channel is returned using different
1761// DataChannelInit configurations.
1762TEST_F(PeerConnectionInterfaceTest, CreateSctpDataChannel) {
1763 FakeConstraints constraints;
1764 constraints.SetAllowDtlsSctpDataChannels();
1765 CreatePeerConnection(&constraints);
1766
1767 webrtc::DataChannelInit config;
1768
zhihuang9763d562016-08-05 11:14:50 -07001769 rtc::scoped_refptr<DataChannelInterface> channel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001770 pc_->CreateDataChannel("1", &config);
1771 EXPECT_TRUE(channel != NULL);
1772 EXPECT_TRUE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001773 EXPECT_TRUE(observer_.renegotiation_needed_);
1774 observer_.renegotiation_needed_ = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001775
1776 config.ordered = false;
1777 channel = pc_->CreateDataChannel("2", &config);
1778 EXPECT_TRUE(channel != NULL);
1779 EXPECT_TRUE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001780 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001781
1782 config.ordered = true;
1783 config.maxRetransmits = 0;
1784 channel = pc_->CreateDataChannel("3", &config);
1785 EXPECT_TRUE(channel != NULL);
1786 EXPECT_FALSE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001787 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001788
1789 config.maxRetransmits = -1;
1790 config.maxRetransmitTime = 0;
1791 channel = pc_->CreateDataChannel("4", &config);
1792 EXPECT_TRUE(channel != NULL);
1793 EXPECT_FALSE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001794 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001795}
1796
1797// This tests that no data channel is returned if both maxRetransmits and
1798// maxRetransmitTime are set for SCTP data channels.
1799TEST_F(PeerConnectionInterfaceTest,
1800 CreateSctpDataChannelShouldFailForInvalidConfig) {
1801 FakeConstraints constraints;
1802 constraints.SetAllowDtlsSctpDataChannels();
1803 CreatePeerConnection(&constraints);
1804
1805 std::string label = "test";
1806 webrtc::DataChannelInit config;
1807 config.maxRetransmits = 0;
1808 config.maxRetransmitTime = 0;
1809
zhihuang9763d562016-08-05 11:14:50 -07001810 rtc::scoped_refptr<DataChannelInterface> channel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001811 pc_->CreateDataChannel(label, &config);
1812 EXPECT_TRUE(channel == NULL);
1813}
1814
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001815// The test verifies that creating a SCTP data channel with an id already in use
1816// or out of range should fail.
1817TEST_F(PeerConnectionInterfaceTest,
1818 CreateSctpDataChannelWithInvalidIdShouldFail) {
1819 FakeConstraints constraints;
1820 constraints.SetAllowDtlsSctpDataChannels();
1821 CreatePeerConnection(&constraints);
1822
1823 webrtc::DataChannelInit config;
zhihuang9763d562016-08-05 11:14:50 -07001824 rtc::scoped_refptr<DataChannelInterface> channel;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001825
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001826 config.id = 1;
1827 channel = pc_->CreateDataChannel("1", &config);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001828 EXPECT_TRUE(channel != NULL);
1829 EXPECT_EQ(1, channel->id());
1830
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001831 channel = pc_->CreateDataChannel("x", &config);
1832 EXPECT_TRUE(channel == NULL);
1833
1834 config.id = cricket::kMaxSctpSid;
1835 channel = pc_->CreateDataChannel("max", &config);
1836 EXPECT_TRUE(channel != NULL);
1837 EXPECT_EQ(config.id, channel->id());
1838
1839 config.id = cricket::kMaxSctpSid + 1;
1840 channel = pc_->CreateDataChannel("x", &config);
1841 EXPECT_TRUE(channel == NULL);
1842}
1843
deadbeefab9b2d12015-10-14 11:33:11 -07001844// Verifies that duplicated label is allowed for SCTP data channel.
1845TEST_F(PeerConnectionInterfaceTest, SctpDuplicatedLabelAllowed) {
1846 FakeConstraints constraints;
1847 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
1848 true);
1849 CreatePeerConnection(&constraints);
1850
1851 std::string label = "test";
zhihuang9763d562016-08-05 11:14:50 -07001852 rtc::scoped_refptr<DataChannelInterface> channel =
deadbeefab9b2d12015-10-14 11:33:11 -07001853 pc_->CreateDataChannel(label, nullptr);
1854 EXPECT_NE(channel, nullptr);
1855
zhihuang9763d562016-08-05 11:14:50 -07001856 rtc::scoped_refptr<DataChannelInterface> dup_channel =
deadbeefab9b2d12015-10-14 11:33:11 -07001857 pc_->CreateDataChannel(label, nullptr);
1858 EXPECT_NE(dup_channel, nullptr);
1859}
1860
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001861// This test verifies that OnRenegotiationNeeded is fired for every new RTP
1862// DataChannel.
1863TEST_F(PeerConnectionInterfaceTest, RenegotiationNeededForNewRtpDataChannel) {
1864 FakeConstraints constraints;
1865 constraints.SetAllowRtpDataChannels();
1866 CreatePeerConnection(&constraints);
1867
zhihuang9763d562016-08-05 11:14:50 -07001868 rtc::scoped_refptr<DataChannelInterface> dc1 =
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001869 pc_->CreateDataChannel("test1", NULL);
1870 EXPECT_TRUE(observer_.renegotiation_needed_);
1871 observer_.renegotiation_needed_ = false;
1872
zhihuang9763d562016-08-05 11:14:50 -07001873 rtc::scoped_refptr<DataChannelInterface> dc2 =
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001874 pc_->CreateDataChannel("test2", NULL);
1875 EXPECT_TRUE(observer_.renegotiation_needed_);
1876}
1877
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001878// This test that a data channel closes when a PeerConnection is deleted/closed.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001879TEST_F(PeerConnectionInterfaceTest, DataChannelCloseWhenPeerConnectionClose) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001880 FakeConstraints constraints;
1881 constraints.SetAllowRtpDataChannels();
1882 CreatePeerConnection(&constraints);
1883
zhihuang9763d562016-08-05 11:14:50 -07001884 rtc::scoped_refptr<DataChannelInterface> data1 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001885 pc_->CreateDataChannel("test1", NULL);
zhihuang9763d562016-08-05 11:14:50 -07001886 rtc::scoped_refptr<DataChannelInterface> data2 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001887 pc_->CreateDataChannel("test2", NULL);
1888 ASSERT_TRUE(data1 != NULL);
kwibergd1fe2812016-04-27 06:47:29 -07001889 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001890 new MockDataChannelObserver(data1));
kwibergd1fe2812016-04-27 06:47:29 -07001891 std::unique_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001892 new MockDataChannelObserver(data2));
1893
1894 CreateOfferReceiveAnswer();
1895 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1896 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
1897
1898 ReleasePeerConnection();
1899 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
1900 EXPECT_EQ(DataChannelInterface::kClosed, data2->state());
1901}
1902
1903// This test that data channels can be rejected in an answer.
1904TEST_F(PeerConnectionInterfaceTest, TestRejectDataChannelInAnswer) {
1905 FakeConstraints constraints;
1906 constraints.SetAllowRtpDataChannels();
1907 CreatePeerConnection(&constraints);
1908
zhihuang9763d562016-08-05 11:14:50 -07001909 rtc::scoped_refptr<DataChannelInterface> offer_channel(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001910 pc_->CreateDataChannel("offer_channel", NULL));
1911
1912 CreateOfferAsLocalDescription();
1913
1914 // Create an answer where the m-line for data channels are rejected.
1915 std::string sdp;
1916 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
1917 webrtc::JsepSessionDescription* answer = new webrtc::JsepSessionDescription(
1918 SessionDescriptionInterface::kAnswer);
1919 EXPECT_TRUE(answer->Initialize(sdp, NULL));
1920 cricket::ContentInfo* data_info =
1921 answer->description()->GetContentByName("data");
1922 data_info->rejected = true;
1923
1924 DoSetRemoteDescription(answer);
1925 EXPECT_EQ(DataChannelInterface::kClosed, offer_channel->state());
1926}
1927
1928// Test that we can create a session description from an SDP string from
1929// FireFox, use it as a remote session description, generate an answer and use
1930// the answer as a local description.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07001931TEST_F(PeerConnectionInterfaceTest, ReceiveFireFoxOffer) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001932 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001933 FakeConstraints constraints;
1934 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
1935 true);
1936 CreatePeerConnection(&constraints);
1937 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
1938 SessionDescriptionInterface* desc =
1939 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
jbauchfabe2c92015-07-16 13:43:14 -07001940 webrtc::kFireFoxSdpOffer, nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001941 EXPECT_TRUE(DoSetSessionDescription(desc, false));
1942 CreateAnswerAsLocalDescription();
1943 ASSERT_TRUE(pc_->local_description() != NULL);
1944 ASSERT_TRUE(pc_->remote_description() != NULL);
1945
1946 const cricket::ContentInfo* content =
1947 cricket::GetFirstAudioContent(pc_->local_description()->description());
1948 ASSERT_TRUE(content != NULL);
1949 EXPECT_FALSE(content->rejected);
1950
1951 content =
1952 cricket::GetFirstVideoContent(pc_->local_description()->description());
1953 ASSERT_TRUE(content != NULL);
1954 EXPECT_FALSE(content->rejected);
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00001955#ifdef HAVE_SCTP
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001956 content =
1957 cricket::GetFirstDataContent(pc_->local_description()->description());
1958 ASSERT_TRUE(content != NULL);
1959 EXPECT_TRUE(content->rejected);
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00001960#endif
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001961}
1962
1963// Test that we can create an audio only offer and receive an answer with a
1964// limited set of audio codecs and receive an updated offer with more audio
1965// codecs, where the added codecs are not supported.
1966TEST_F(PeerConnectionInterfaceTest, ReceiveUpdatedAudioOfferWithBadCodecs) {
1967 CreatePeerConnection();
1968 AddVoiceStream("audio_label");
1969 CreateOfferAsLocalDescription();
1970
1971 SessionDescriptionInterface* answer =
1972 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
jbauchfabe2c92015-07-16 13:43:14 -07001973 webrtc::kAudioSdp, nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001974 EXPECT_TRUE(DoSetSessionDescription(answer, false));
1975
1976 SessionDescriptionInterface* updated_offer =
1977 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
jbauchfabe2c92015-07-16 13:43:14 -07001978 webrtc::kAudioSdpWithUnsupportedCodecs,
1979 nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001980 EXPECT_TRUE(DoSetSessionDescription(updated_offer, false));
1981 CreateAnswerAsLocalDescription();
1982}
1983
deadbeefc80741f2015-10-22 13:14:45 -07001984// Test that if we're receiving (but not sending) a track, subsequent offers
1985// will have m-lines with a=recvonly.
1986TEST_F(PeerConnectionInterfaceTest, CreateSubsequentRecvOnlyOffer) {
1987 FakeConstraints constraints;
1988 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
1989 true);
1990 CreatePeerConnection(&constraints);
1991 CreateAndSetRemoteOffer(kSdpStringWithStream1);
1992 CreateAnswerAsLocalDescription();
1993
1994 // At this point we should be receiving stream 1, but not sending anything.
1995 // A new offer should be recvonly.
kwibergd1fe2812016-04-27 06:47:29 -07001996 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefc80741f2015-10-22 13:14:45 -07001997 DoCreateOffer(&offer, nullptr);
1998
1999 const cricket::ContentInfo* video_content =
2000 cricket::GetFirstVideoContent(offer->description());
2001 const cricket::VideoContentDescription* video_desc =
2002 static_cast<const cricket::VideoContentDescription*>(
2003 video_content->description);
2004 ASSERT_EQ(cricket::MD_RECVONLY, video_desc->direction());
2005
2006 const cricket::ContentInfo* audio_content =
2007 cricket::GetFirstAudioContent(offer->description());
2008 const cricket::AudioContentDescription* audio_desc =
2009 static_cast<const cricket::AudioContentDescription*>(
2010 audio_content->description);
2011 ASSERT_EQ(cricket::MD_RECVONLY, audio_desc->direction());
2012}
2013
2014// Test that if we're receiving (but not sending) a track, and the
2015// offerToReceiveVideo/offerToReceiveAudio constraints are explicitly set to
2016// false, the generated m-lines will be a=inactive.
2017TEST_F(PeerConnectionInterfaceTest, CreateSubsequentInactiveOffer) {
2018 FakeConstraints constraints;
2019 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2020 true);
2021 CreatePeerConnection(&constraints);
2022 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2023 CreateAnswerAsLocalDescription();
2024
2025 // At this point we should be receiving stream 1, but not sending anything.
2026 // A new offer would be recvonly, but we'll set the "no receive" constraints
2027 // to make it inactive.
kwibergd1fe2812016-04-27 06:47:29 -07002028 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefc80741f2015-10-22 13:14:45 -07002029 FakeConstraints offer_constraints;
2030 offer_constraints.AddMandatory(
2031 webrtc::MediaConstraintsInterface::kOfferToReceiveVideo, false);
2032 offer_constraints.AddMandatory(
2033 webrtc::MediaConstraintsInterface::kOfferToReceiveAudio, false);
2034 DoCreateOffer(&offer, &offer_constraints);
2035
2036 const cricket::ContentInfo* video_content =
2037 cricket::GetFirstVideoContent(offer->description());
2038 const cricket::VideoContentDescription* video_desc =
2039 static_cast<const cricket::VideoContentDescription*>(
2040 video_content->description);
2041 ASSERT_EQ(cricket::MD_INACTIVE, video_desc->direction());
2042
2043 const cricket::ContentInfo* audio_content =
2044 cricket::GetFirstAudioContent(offer->description());
2045 const cricket::AudioContentDescription* audio_desc =
2046 static_cast<const cricket::AudioContentDescription*>(
2047 audio_content->description);
2048 ASSERT_EQ(cricket::MD_INACTIVE, audio_desc->direction());
2049}
2050
deadbeef653b8e02015-11-11 12:55:10 -08002051// Test that we can use SetConfiguration to change the ICE servers of the
2052// PortAllocator.
2053TEST_F(PeerConnectionInterfaceTest, SetConfigurationChangesIceServers) {
2054 CreatePeerConnection();
2055
2056 PeerConnectionInterface::RTCConfiguration config;
2057 PeerConnectionInterface::IceServer server;
2058 server.uri = "stun:test_hostname";
2059 config.servers.push_back(server);
2060 EXPECT_TRUE(pc_->SetConfiguration(config));
2061
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002062 EXPECT_EQ(1u, port_allocator_->stun_servers().size());
2063 EXPECT_EQ("test_hostname",
2064 port_allocator_->stun_servers().begin()->hostname());
deadbeef653b8e02015-11-11 12:55:10 -08002065}
2066
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002067TEST_F(PeerConnectionInterfaceTest, SetConfigurationChangesCandidateFilter) {
2068 CreatePeerConnection();
2069 PeerConnectionInterface::RTCConfiguration config;
2070 config.type = PeerConnectionInterface::kRelay;
2071 EXPECT_TRUE(pc_->SetConfiguration(config));
2072 EXPECT_EQ(cricket::CF_RELAY, port_allocator_->candidate_filter());
2073}
2074
2075// Test that when SetConfiguration changes both the pool size and other
2076// attributes, the pooled session is created with the updated attributes.
2077TEST_F(PeerConnectionInterfaceTest,
2078 SetConfigurationCreatesPooledSessionCorrectly) {
2079 CreatePeerConnection();
2080 PeerConnectionInterface::RTCConfiguration config;
2081 config.ice_candidate_pool_size = 1;
2082 PeerConnectionInterface::IceServer server;
2083 server.uri = kStunAddressOnly;
2084 config.servers.push_back(server);
2085 config.type = PeerConnectionInterface::kRelay;
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07002086 EXPECT_TRUE(pc_->SetConfiguration(config));
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002087
2088 const cricket::FakePortAllocatorSession* session =
2089 static_cast<const cricket::FakePortAllocatorSession*>(
2090 port_allocator_->GetPooledSession());
2091 ASSERT_NE(nullptr, session);
2092 EXPECT_EQ(1UL, session->stun_servers().size());
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002093}
2094
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002095// Test that PeerConnection::Close changes the states to closed and all remote
2096// tracks change state to ended.
2097TEST_F(PeerConnectionInterfaceTest, CloseAndTestStreamsAndStates) {
2098 // Initialize a PeerConnection and negotiate local and remote session
2099 // description.
2100 InitiateCall();
2101 ASSERT_EQ(1u, pc_->local_streams()->count());
2102 ASSERT_EQ(1u, pc_->remote_streams()->count());
2103
2104 pc_->Close();
2105
2106 EXPECT_EQ(PeerConnectionInterface::kClosed, pc_->signaling_state());
2107 EXPECT_EQ(PeerConnectionInterface::kIceConnectionClosed,
2108 pc_->ice_connection_state());
2109 EXPECT_EQ(PeerConnectionInterface::kIceGatheringComplete,
2110 pc_->ice_gathering_state());
2111
2112 EXPECT_EQ(1u, pc_->local_streams()->count());
2113 EXPECT_EQ(1u, pc_->remote_streams()->count());
2114
zhihuang9763d562016-08-05 11:14:50 -07002115 rtc::scoped_refptr<MediaStreamInterface> remote_stream =
2116 pc_->remote_streams()->at(0);
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002117 // Track state may be updated asynchronously.
perkjd61bf802016-03-24 03:16:19 -07002118 EXPECT_EQ_WAIT(MediaStreamTrackInterface::kEnded,
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002119 remote_stream->GetAudioTracks()[0]->state(), kTimeout);
2120 EXPECT_EQ_WAIT(MediaStreamTrackInterface::kEnded,
2121 remote_stream->GetVideoTracks()[0]->state(), kTimeout);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002122}
2123
2124// Test that PeerConnection methods fails gracefully after
2125// PeerConnection::Close has been called.
2126TEST_F(PeerConnectionInterfaceTest, CloseAndTestMethods) {
2127 CreatePeerConnection();
2128 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
2129 CreateOfferAsRemoteDescription();
2130 CreateAnswerAsLocalDescription();
2131
2132 ASSERT_EQ(1u, pc_->local_streams()->count());
zhihuang9763d562016-08-05 11:14:50 -07002133 rtc::scoped_refptr<MediaStreamInterface> local_stream =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002134 pc_->local_streams()->at(0);
2135
2136 pc_->Close();
2137
2138 pc_->RemoveStream(local_stream);
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +00002139 EXPECT_FALSE(pc_->AddStream(local_stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002140
2141 ASSERT_FALSE(local_stream->GetAudioTracks().empty());
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002142 rtc::scoped_refptr<webrtc::DtmfSenderInterface> dtmf_sender(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002143 pc_->CreateDtmfSender(local_stream->GetAudioTracks()[0]));
wu@webrtc.org66037362013-08-13 00:09:35 +00002144 EXPECT_TRUE(NULL == dtmf_sender); // local stream has been removed.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002145
2146 EXPECT_TRUE(pc_->CreateDataChannel("test", NULL) == NULL);
2147
2148 EXPECT_TRUE(pc_->local_description() != NULL);
2149 EXPECT_TRUE(pc_->remote_description() != NULL);
2150
kwibergd1fe2812016-04-27 06:47:29 -07002151 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07002152 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
kwibergd1fe2812016-04-27 06:47:29 -07002153 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -07002154 EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002155
2156 std::string sdp;
2157 ASSERT_TRUE(pc_->remote_description()->ToString(&sdp));
2158 SessionDescriptionInterface* remote_offer =
2159 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
2160 sdp, NULL);
2161 EXPECT_FALSE(DoSetRemoteDescription(remote_offer));
2162
2163 ASSERT_TRUE(pc_->local_description()->ToString(&sdp));
2164 SessionDescriptionInterface* local_offer =
2165 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
2166 sdp, NULL);
2167 EXPECT_FALSE(DoSetLocalDescription(local_offer));
2168}
2169
2170// Test that GetStats can still be called after PeerConnection::Close.
2171TEST_F(PeerConnectionInterfaceTest, CloseAndGetStats) {
2172 InitiateCall();
2173 pc_->Close();
2174 DoGetStats(NULL);
2175}
deadbeefab9b2d12015-10-14 11:33:11 -07002176
2177// NOTE: The series of tests below come from what used to be
2178// mediastreamsignaling_unittest.cc, and are mostly aimed at testing that
2179// setting a remote or local description has the expected effects.
2180
2181// This test verifies that the remote MediaStreams corresponding to a received
2182// SDP string is created. In this test the two separate MediaStreams are
2183// signaled.
2184TEST_F(PeerConnectionInterfaceTest, UpdateRemoteStreams) {
2185 FakeConstraints constraints;
2186 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2187 true);
2188 CreatePeerConnection(&constraints);
2189 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2190
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002191 rtc::scoped_refptr<StreamCollection> reference(CreateStreamCollection(1, 1));
deadbeefab9b2d12015-10-14 11:33:11 -07002192 EXPECT_TRUE(
2193 CompareStreamCollections(observer_.remote_streams(), reference.get()));
2194 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2195 EXPECT_TRUE(remote_stream->GetVideoTracks()[0]->GetSource() != nullptr);
2196
2197 // Create a session description based on another SDP with another
2198 // MediaStream.
2199 CreateAndSetRemoteOffer(kSdpStringWithStream1And2);
2200
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002201 rtc::scoped_refptr<StreamCollection> reference2(CreateStreamCollection(2, 1));
deadbeefab9b2d12015-10-14 11:33:11 -07002202 EXPECT_TRUE(
2203 CompareStreamCollections(observer_.remote_streams(), reference2.get()));
2204}
2205
2206// This test verifies that when remote tracks are added/removed from SDP, the
2207// created remote streams are updated appropriately.
2208TEST_F(PeerConnectionInterfaceTest,
2209 AddRemoveTrackFromExistingRemoteMediaStream) {
2210 FakeConstraints constraints;
2211 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2212 true);
2213 CreatePeerConnection(&constraints);
kwibergd1fe2812016-04-27 06:47:29 -07002214 std::unique_ptr<SessionDescriptionInterface> desc_ms1 =
kwiberg2bbff992016-03-16 11:03:04 -07002215 CreateSessionDescriptionAndReference(1, 1);
deadbeefab9b2d12015-10-14 11:33:11 -07002216 EXPECT_TRUE(DoSetRemoteDescription(desc_ms1.release()));
2217 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
2218 reference_collection_));
2219
2220 // Add extra audio and video tracks to the same MediaStream.
kwibergd1fe2812016-04-27 06:47:29 -07002221 std::unique_ptr<SessionDescriptionInterface> desc_ms1_two_tracks =
kwiberg2bbff992016-03-16 11:03:04 -07002222 CreateSessionDescriptionAndReference(2, 2);
deadbeefab9b2d12015-10-14 11:33:11 -07002223 EXPECT_TRUE(DoSetRemoteDescription(desc_ms1_two_tracks.release()));
2224 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
2225 reference_collection_));
zhihuang9763d562016-08-05 11:14:50 -07002226 rtc::scoped_refptr<AudioTrackInterface> audio_track2 =
perkjd61bf802016-03-24 03:16:19 -07002227 observer_.remote_streams()->at(0)->GetAudioTracks()[1];
2228 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, audio_track2->state());
zhihuang9763d562016-08-05 11:14:50 -07002229 rtc::scoped_refptr<VideoTrackInterface> video_track2 =
perkjd61bf802016-03-24 03:16:19 -07002230 observer_.remote_streams()->at(0)->GetVideoTracks()[1];
2231 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, video_track2->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002232
2233 // Remove the extra audio and video tracks.
kwibergd1fe2812016-04-27 06:47:29 -07002234 std::unique_ptr<SessionDescriptionInterface> desc_ms2 =
kwiberg2bbff992016-03-16 11:03:04 -07002235 CreateSessionDescriptionAndReference(1, 1);
perkjd61bf802016-03-24 03:16:19 -07002236 MockTrackObserver audio_track_observer(audio_track2);
2237 MockTrackObserver video_track_observer(video_track2);
2238
2239 EXPECT_CALL(audio_track_observer, OnChanged()).Times(Exactly(1));
2240 EXPECT_CALL(video_track_observer, OnChanged()).Times(Exactly(1));
deadbeefab9b2d12015-10-14 11:33:11 -07002241 EXPECT_TRUE(DoSetRemoteDescription(desc_ms2.release()));
2242 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
2243 reference_collection_));
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002244 // Track state may be updated asynchronously.
perkjd61bf802016-03-24 03:16:19 -07002245 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002246 audio_track2->state(), kTimeout);
2247 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
2248 video_track2->state(), kTimeout);
deadbeefab9b2d12015-10-14 11:33:11 -07002249}
2250
2251// This tests that remote tracks are ended if a local session description is set
2252// that rejects the media content type.
2253TEST_F(PeerConnectionInterfaceTest, RejectMediaContent) {
2254 FakeConstraints constraints;
2255 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2256 true);
2257 CreatePeerConnection(&constraints);
2258 // First create and set a remote offer, then reject its video content in our
2259 // answer.
2260 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2261 ASSERT_EQ(1u, observer_.remote_streams()->count());
2262 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2263 ASSERT_EQ(1u, remote_stream->GetVideoTracks().size());
2264 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2265
2266 rtc::scoped_refptr<webrtc::VideoTrackInterface> remote_video =
2267 remote_stream->GetVideoTracks()[0];
2268 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, remote_video->state());
2269 rtc::scoped_refptr<webrtc::AudioTrackInterface> remote_audio =
2270 remote_stream->GetAudioTracks()[0];
2271 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, remote_audio->state());
2272
kwibergd1fe2812016-04-27 06:47:29 -07002273 std::unique_ptr<SessionDescriptionInterface> local_answer;
kwiberg2bbff992016-03-16 11:03:04 -07002274 EXPECT_TRUE(DoCreateAnswer(&local_answer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07002275 cricket::ContentInfo* video_info =
2276 local_answer->description()->GetContentByName("video");
2277 video_info->rejected = true;
2278 EXPECT_TRUE(DoSetLocalDescription(local_answer.release()));
2279 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kEnded, remote_video->state());
2280 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, remote_audio->state());
2281
2282 // Now create an offer where we reject both video and audio.
kwibergd1fe2812016-04-27 06:47:29 -07002283 std::unique_ptr<SessionDescriptionInterface> local_offer;
kwiberg2bbff992016-03-16 11:03:04 -07002284 EXPECT_TRUE(DoCreateOffer(&local_offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07002285 video_info = local_offer->description()->GetContentByName("video");
2286 ASSERT_TRUE(video_info != nullptr);
2287 video_info->rejected = true;
2288 cricket::ContentInfo* audio_info =
2289 local_offer->description()->GetContentByName("audio");
2290 ASSERT_TRUE(audio_info != nullptr);
2291 audio_info->rejected = true;
2292 EXPECT_TRUE(DoSetLocalDescription(local_offer.release()));
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002293 // Track state may be updated asynchronously.
perkjd61bf802016-03-24 03:16:19 -07002294 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002295 remote_audio->state(), kTimeout);
2296 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
2297 remote_video->state(), kTimeout);
deadbeefab9b2d12015-10-14 11:33:11 -07002298}
2299
2300// This tests that we won't crash if the remote track has been removed outside
2301// of PeerConnection and then PeerConnection tries to reject the track.
2302TEST_F(PeerConnectionInterfaceTest, RemoveTrackThenRejectMediaContent) {
2303 FakeConstraints constraints;
2304 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2305 true);
2306 CreatePeerConnection(&constraints);
2307 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2308 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2309 remote_stream->RemoveTrack(remote_stream->GetVideoTracks()[0]);
2310 remote_stream->RemoveTrack(remote_stream->GetAudioTracks()[0]);
2311
kwibergd1fe2812016-04-27 06:47:29 -07002312 std::unique_ptr<SessionDescriptionInterface> local_answer(
deadbeefab9b2d12015-10-14 11:33:11 -07002313 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
2314 kSdpStringWithStream1, nullptr));
2315 cricket::ContentInfo* video_info =
2316 local_answer->description()->GetContentByName("video");
2317 video_info->rejected = true;
2318 cricket::ContentInfo* audio_info =
2319 local_answer->description()->GetContentByName("audio");
2320 audio_info->rejected = true;
2321 EXPECT_TRUE(DoSetLocalDescription(local_answer.release()));
2322
2323 // No crash is a pass.
2324}
2325
deadbeef5e97fb52015-10-15 12:49:08 -07002326// This tests that if a recvonly remote description is set, no remote streams
2327// will be created, even if the description contains SSRCs/MSIDs.
2328// See: https://code.google.com/p/webrtc/issues/detail?id=5054
2329TEST_F(PeerConnectionInterfaceTest, RecvonlyDescriptionDoesntCreateStream) {
2330 FakeConstraints constraints;
2331 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2332 true);
2333 CreatePeerConnection(&constraints);
2334
2335 std::string recvonly_offer = kSdpStringWithStream1;
2336 rtc::replace_substrs(kSendrecv, strlen(kSendrecv), kRecvonly,
2337 strlen(kRecvonly), &recvonly_offer);
2338 CreateAndSetRemoteOffer(recvonly_offer);
2339
2340 EXPECT_EQ(0u, observer_.remote_streams()->count());
2341}
2342
deadbeefab9b2d12015-10-14 11:33:11 -07002343// This tests that a default MediaStream is created if a remote session
2344// description doesn't contain any streams and no MSID support.
2345// It also tests that the default stream is updated if a video m-line is added
2346// in a subsequent session description.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002347TEST_F(PeerConnectionInterfaceTest, SdpWithoutMsidCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002348 FakeConstraints constraints;
2349 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2350 true);
2351 CreatePeerConnection(&constraints);
2352 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2353
2354 ASSERT_EQ(1u, observer_.remote_streams()->count());
2355 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2356
2357 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2358 EXPECT_EQ(0u, remote_stream->GetVideoTracks().size());
2359 EXPECT_EQ("default", remote_stream->label());
2360
2361 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2362 ASSERT_EQ(1u, observer_.remote_streams()->count());
2363 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2364 EXPECT_EQ("defaulta0", remote_stream->GetAudioTracks()[0]->id());
deadbeef884f5852016-01-15 09:20:04 -08002365 EXPECT_EQ(MediaStreamTrackInterface::kLive,
2366 remote_stream->GetAudioTracks()[0]->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002367 ASSERT_EQ(1u, remote_stream->GetVideoTracks().size());
2368 EXPECT_EQ("defaultv0", remote_stream->GetVideoTracks()[0]->id());
deadbeef884f5852016-01-15 09:20:04 -08002369 EXPECT_EQ(MediaStreamTrackInterface::kLive,
2370 remote_stream->GetVideoTracks()[0]->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002371}
2372
2373// This tests that a default MediaStream is created if a remote session
2374// description doesn't contain any streams and media direction is send only.
2375TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002376 SendOnlySdpWithoutMsidCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002377 FakeConstraints constraints;
2378 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2379 true);
2380 CreatePeerConnection(&constraints);
2381 CreateAndSetRemoteOffer(kSdpStringSendOnlyWithoutStreams);
2382
2383 ASSERT_EQ(1u, observer_.remote_streams()->count());
2384 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2385
2386 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2387 EXPECT_EQ(1u, remote_stream->GetVideoTracks().size());
2388 EXPECT_EQ("default", remote_stream->label());
2389}
2390
2391// This tests that it won't crash when PeerConnection tries to remove
2392// a remote track that as already been removed from the MediaStream.
2393TEST_F(PeerConnectionInterfaceTest, RemoveAlreadyGoneRemoteStream) {
2394 FakeConstraints constraints;
2395 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2396 true);
2397 CreatePeerConnection(&constraints);
2398 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2399 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2400 remote_stream->RemoveTrack(remote_stream->GetAudioTracks()[0]);
2401 remote_stream->RemoveTrack(remote_stream->GetVideoTracks()[0]);
2402
2403 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2404
2405 // No crash is a pass.
2406}
2407
2408// This tests that a default MediaStream is created if the remote session
2409// description doesn't contain any streams and don't contain an indication if
2410// MSID is supported.
2411TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002412 SdpWithoutMsidAndStreamsCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002413 FakeConstraints constraints;
2414 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2415 true);
2416 CreatePeerConnection(&constraints);
2417 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2418
2419 ASSERT_EQ(1u, observer_.remote_streams()->count());
2420 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2421 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2422 EXPECT_EQ(1u, remote_stream->GetVideoTracks().size());
2423}
2424
2425// This tests that a default MediaStream is not created if the remote session
2426// description doesn't contain any streams but does support MSID.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002427TEST_F(PeerConnectionInterfaceTest, SdpWithMsidDontCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002428 FakeConstraints constraints;
2429 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2430 true);
2431 CreatePeerConnection(&constraints);
2432 CreateAndSetRemoteOffer(kSdpStringWithMsidWithoutStreams);
2433 EXPECT_EQ(0u, observer_.remote_streams()->count());
2434}
2435
deadbeefbda7e0b2015-12-08 17:13:40 -08002436// This tests that when setting a new description, the old default tracks are
2437// not destroyed and recreated.
2438// See: https://bugs.chromium.org/p/webrtc/issues/detail?id=5250
Stefan Holmer102362b2016-03-18 09:39:07 +01002439TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002440 DefaultTracksNotDestroyedAndRecreated) {
deadbeefbda7e0b2015-12-08 17:13:40 -08002441 FakeConstraints constraints;
2442 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2443 true);
2444 CreatePeerConnection(&constraints);
2445 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2446
2447 ASSERT_EQ(1u, observer_.remote_streams()->count());
2448 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2449 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2450
2451 // Set the track to "disabled", then set a new description and ensure the
2452 // track is still disabled, which ensures it hasn't been recreated.
2453 remote_stream->GetAudioTracks()[0]->set_enabled(false);
2454 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2455 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2456 EXPECT_FALSE(remote_stream->GetAudioTracks()[0]->enabled());
2457}
2458
deadbeefab9b2d12015-10-14 11:33:11 -07002459// This tests that a default MediaStream is not created if a remote session
2460// description is updated to not have any MediaStreams.
2461TEST_F(PeerConnectionInterfaceTest, VerifyDefaultStreamIsNotCreated) {
2462 FakeConstraints constraints;
2463 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2464 true);
2465 CreatePeerConnection(&constraints);
2466 CreateAndSetRemoteOffer(kSdpStringWithStream1);
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002467 rtc::scoped_refptr<StreamCollection> reference(CreateStreamCollection(1, 1));
deadbeefab9b2d12015-10-14 11:33:11 -07002468 EXPECT_TRUE(
2469 CompareStreamCollections(observer_.remote_streams(), reference.get()));
2470
2471 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2472 EXPECT_EQ(0u, observer_.remote_streams()->count());
2473}
2474
2475// This tests that an RtpSender is created when the local description is set
2476// after adding a local stream.
2477// TODO(deadbeef): This test and the one below it need to be updated when
2478// an RtpSender's lifetime isn't determined by when a local description is set.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002479TEST_F(PeerConnectionInterfaceTest, LocalDescriptionChanged) {
deadbeefab9b2d12015-10-14 11:33:11 -07002480 FakeConstraints constraints;
2481 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2482 true);
2483 CreatePeerConnection(&constraints);
deadbeefab9b2d12015-10-14 11:33:11 -07002484
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002485 // Create an offer with 1 stream with 2 tracks of each type.
2486 rtc::scoped_refptr<StreamCollection> stream_collection =
2487 CreateStreamCollection(1, 2);
2488 pc_->AddStream(stream_collection->at(0));
2489 std::unique_ptr<SessionDescriptionInterface> offer;
2490 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2491 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
deadbeefab9b2d12015-10-14 11:33:11 -07002492
deadbeefab9b2d12015-10-14 11:33:11 -07002493 auto senders = pc_->GetSenders();
2494 EXPECT_EQ(4u, senders.size());
2495 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2496 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2497 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[1]));
2498 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[1]));
2499
2500 // Remove an audio and video track.
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002501 pc_->RemoveStream(stream_collection->at(0));
2502 stream_collection = CreateStreamCollection(1, 1);
2503 pc_->AddStream(stream_collection->at(0));
2504 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2505 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
2506
deadbeefab9b2d12015-10-14 11:33:11 -07002507 senders = pc_->GetSenders();
2508 EXPECT_EQ(2u, senders.size());
2509 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2510 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2511 EXPECT_FALSE(ContainsSender(senders, kAudioTracks[1]));
2512 EXPECT_FALSE(ContainsSender(senders, kVideoTracks[1]));
2513}
2514
2515// This tests that an RtpSender is created when the local description is set
2516// before adding a local stream.
2517TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002518 AddLocalStreamAfterLocalDescriptionChanged) {
deadbeefab9b2d12015-10-14 11:33:11 -07002519 FakeConstraints constraints;
2520 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2521 true);
2522 CreatePeerConnection(&constraints);
deadbeefab9b2d12015-10-14 11:33:11 -07002523
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002524 rtc::scoped_refptr<StreamCollection> stream_collection =
2525 CreateStreamCollection(1, 2);
2526 // Add a stream to create the offer, but remove it afterwards.
2527 pc_->AddStream(stream_collection->at(0));
2528 std::unique_ptr<SessionDescriptionInterface> offer;
2529 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2530 pc_->RemoveStream(stream_collection->at(0));
deadbeefab9b2d12015-10-14 11:33:11 -07002531
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002532 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
deadbeefab9b2d12015-10-14 11:33:11 -07002533 auto senders = pc_->GetSenders();
2534 EXPECT_EQ(0u, senders.size());
2535
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002536 pc_->AddStream(stream_collection->at(0));
deadbeefab9b2d12015-10-14 11:33:11 -07002537 senders = pc_->GetSenders();
2538 EXPECT_EQ(4u, senders.size());
2539 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2540 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2541 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[1]));
2542 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[1]));
2543}
2544
2545// This tests that the expected behavior occurs if the SSRC on a local track is
2546// changed when SetLocalDescription is called.
2547TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002548 ChangeSsrcOnTrackInLocalSessionDescription) {
deadbeefab9b2d12015-10-14 11:33:11 -07002549 FakeConstraints constraints;
2550 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2551 true);
2552 CreatePeerConnection(&constraints);
deadbeefab9b2d12015-10-14 11:33:11 -07002553
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002554 rtc::scoped_refptr<StreamCollection> stream_collection =
2555 CreateStreamCollection(2, 1);
2556 pc_->AddStream(stream_collection->at(0));
2557 std::unique_ptr<SessionDescriptionInterface> offer;
2558 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2559 // Grab a copy of the offer before it gets passed into the PC.
2560 std::unique_ptr<JsepSessionDescription> modified_offer(
2561 new JsepSessionDescription(JsepSessionDescription::kOffer));
2562 modified_offer->Initialize(offer->description()->Copy(), offer->session_id(),
2563 offer->session_version());
2564 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
deadbeefab9b2d12015-10-14 11:33:11 -07002565
deadbeefab9b2d12015-10-14 11:33:11 -07002566 auto senders = pc_->GetSenders();
2567 EXPECT_EQ(2u, senders.size());
2568 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2569 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2570
2571 // Change the ssrc of the audio and video track.
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002572 cricket::MediaContentDescription* desc =
2573 cricket::GetFirstAudioContentDescription(modified_offer->description());
2574 ASSERT_TRUE(desc != NULL);
2575 for (StreamParams& stream : desc->mutable_streams()) {
2576 for (unsigned int& ssrc : stream.ssrcs) {
2577 ++ssrc;
2578 }
2579 }
deadbeefab9b2d12015-10-14 11:33:11 -07002580
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002581 desc =
2582 cricket::GetFirstVideoContentDescription(modified_offer->description());
2583 ASSERT_TRUE(desc != NULL);
2584 for (StreamParams& stream : desc->mutable_streams()) {
2585 for (unsigned int& ssrc : stream.ssrcs) {
2586 ++ssrc;
2587 }
2588 }
2589
2590 EXPECT_TRUE(DoSetLocalDescription(modified_offer.release()));
deadbeefab9b2d12015-10-14 11:33:11 -07002591 senders = pc_->GetSenders();
2592 EXPECT_EQ(2u, senders.size());
2593 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2594 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2595 // TODO(deadbeef): Once RtpSenders expose parameters, check that the SSRC
2596 // changed.
2597}
2598
2599// This tests that the expected behavior occurs if a new session description is
2600// set with the same tracks, but on a different MediaStream.
Stefan Holmer55d6e7c2016-03-17 16:26:40 +01002601TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002602 SignalSameTracksInSeparateMediaStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002603 FakeConstraints constraints;
2604 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2605 true);
2606 CreatePeerConnection(&constraints);
deadbeefab9b2d12015-10-14 11:33:11 -07002607
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002608 rtc::scoped_refptr<StreamCollection> stream_collection =
2609 CreateStreamCollection(2, 1);
2610 pc_->AddStream(stream_collection->at(0));
2611 std::unique_ptr<SessionDescriptionInterface> offer;
2612 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2613 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
deadbeefab9b2d12015-10-14 11:33:11 -07002614
deadbeefab9b2d12015-10-14 11:33:11 -07002615 auto senders = pc_->GetSenders();
2616 EXPECT_EQ(2u, senders.size());
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002617 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0], kStreams[0]));
2618 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0], kStreams[0]));
deadbeefab9b2d12015-10-14 11:33:11 -07002619
2620 // Add a new MediaStream but with the same tracks as in the first stream.
2621 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream_1(
2622 webrtc::MediaStream::Create(kStreams[1]));
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002623 stream_1->AddTrack(stream_collection->at(0)->GetVideoTracks()[0]);
2624 stream_1->AddTrack(stream_collection->at(0)->GetAudioTracks()[0]);
deadbeefab9b2d12015-10-14 11:33:11 -07002625 pc_->AddStream(stream_1);
2626
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002627 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2628 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
deadbeefab9b2d12015-10-14 11:33:11 -07002629
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002630 auto new_senders = pc_->GetSenders();
2631 // Should be the same senders as before, but with updated stream id.
2632 // Note that this behavior is subject to change in the future.
2633 // We may decide the PC should ignore existing tracks in AddStream.
2634 EXPECT_EQ(senders, new_senders);
2635 EXPECT_TRUE(ContainsSender(new_senders, kAudioTracks[0], kStreams[1]));
2636 EXPECT_TRUE(ContainsSender(new_senders, kVideoTracks[0], kStreams[1]));
deadbeefab9b2d12015-10-14 11:33:11 -07002637}
2638
nisse51542be2016-02-12 02:27:06 -08002639class PeerConnectionMediaConfigTest : public testing::Test {
2640 protected:
2641 void SetUp() override {
nisseaf510af2016-03-21 08:20:42 -07002642 pcf_ = new rtc::RefCountedObject<PeerConnectionFactoryForTest>();
nisse51542be2016-02-12 02:27:06 -08002643 pcf_->Initialize();
2644 }
2645 const cricket::MediaConfig& TestCreatePeerConnection(
2646 const PeerConnectionInterface::RTCConfiguration& config,
2647 const MediaConstraintsInterface *constraints) {
2648 pcf_->create_media_controller_called_ = false;
2649
zhihuang9763d562016-08-05 11:14:50 -07002650 rtc::scoped_refptr<PeerConnectionInterface> pc(pcf_->CreatePeerConnection(
2651 config, constraints, nullptr, nullptr, &observer_));
nisse51542be2016-02-12 02:27:06 -08002652 EXPECT_TRUE(pc.get());
2653 EXPECT_TRUE(pcf_->create_media_controller_called_);
2654 return pcf_->create_media_controller_config_;
2655 }
2656
zhihuang9763d562016-08-05 11:14:50 -07002657 rtc::scoped_refptr<PeerConnectionFactoryForTest> pcf_;
nisse51542be2016-02-12 02:27:06 -08002658 MockPeerConnectionObserver observer_;
2659};
2660
2661// This test verifies the default behaviour with no constraints and a
2662// default RTCConfiguration.
2663TEST_F(PeerConnectionMediaConfigTest, TestDefaults) {
2664 PeerConnectionInterface::RTCConfiguration config;
2665 FakeConstraints constraints;
2666
2667 const cricket::MediaConfig& media_config =
2668 TestCreatePeerConnection(config, &constraints);
2669
2670 EXPECT_FALSE(media_config.enable_dscp);
nisse0db023a2016-03-01 04:29:59 -08002671 EXPECT_TRUE(media_config.video.enable_cpu_overuse_detection);
2672 EXPECT_FALSE(media_config.video.disable_prerenderer_smoothing);
2673 EXPECT_FALSE(media_config.video.suspend_below_min_bitrate);
nisse51542be2016-02-12 02:27:06 -08002674}
2675
2676// This test verifies the DSCP constraint is recognized and passed to
2677// the CreateMediaController call.
2678TEST_F(PeerConnectionMediaConfigTest, TestDscpConstraintTrue) {
2679 PeerConnectionInterface::RTCConfiguration config;
2680 FakeConstraints constraints;
2681
2682 constraints.AddOptional(webrtc::MediaConstraintsInterface::kEnableDscp, true);
2683 const cricket::MediaConfig& media_config =
2684 TestCreatePeerConnection(config, &constraints);
2685
2686 EXPECT_TRUE(media_config.enable_dscp);
2687}
2688
2689// This test verifies the cpu overuse detection constraint is
2690// recognized and passed to the CreateMediaController call.
2691TEST_F(PeerConnectionMediaConfigTest, TestCpuOveruseConstraintFalse) {
2692 PeerConnectionInterface::RTCConfiguration config;
2693 FakeConstraints constraints;
2694
2695 constraints.AddOptional(
2696 webrtc::MediaConstraintsInterface::kCpuOveruseDetection, false);
2697 const cricket::MediaConfig media_config =
2698 TestCreatePeerConnection(config, &constraints);
2699
nisse0db023a2016-03-01 04:29:59 -08002700 EXPECT_FALSE(media_config.video.enable_cpu_overuse_detection);
nisse51542be2016-02-12 02:27:06 -08002701}
2702
2703// This test verifies that the disable_prerenderer_smoothing flag is
2704// propagated from RTCConfiguration to the CreateMediaController call.
2705TEST_F(PeerConnectionMediaConfigTest, TestDisablePrerendererSmoothingTrue) {
2706 PeerConnectionInterface::RTCConfiguration config;
2707 FakeConstraints constraints;
2708
Niels Möller71bdda02016-03-31 12:59:59 +02002709 config.set_prerenderer_smoothing(false);
nisse51542be2016-02-12 02:27:06 -08002710 const cricket::MediaConfig& media_config =
2711 TestCreatePeerConnection(config, &constraints);
2712
nisse0db023a2016-03-01 04:29:59 -08002713 EXPECT_TRUE(media_config.video.disable_prerenderer_smoothing);
2714}
2715
2716// This test verifies the suspend below min bitrate constraint is
2717// recognized and passed to the CreateMediaController call.
2718TEST_F(PeerConnectionMediaConfigTest,
2719 TestSuspendBelowMinBitrateConstraintTrue) {
2720 PeerConnectionInterface::RTCConfiguration config;
2721 FakeConstraints constraints;
2722
2723 constraints.AddOptional(
2724 webrtc::MediaConstraintsInterface::kEnableVideoSuspendBelowMinBitrate,
2725 true);
2726 const cricket::MediaConfig media_config =
2727 TestCreatePeerConnection(config, &constraints);
2728
2729 EXPECT_TRUE(media_config.video.suspend_below_min_bitrate);
nisse51542be2016-02-12 02:27:06 -08002730}
2731
deadbeefab9b2d12015-10-14 11:33:11 -07002732// The following tests verify that session options are created correctly.
deadbeefc80741f2015-10-22 13:14:45 -07002733// TODO(deadbeef): Convert these tests to be more end-to-end. Instead of
2734// "verify options are converted correctly", should be "pass options into
2735// CreateOffer and verify the correct offer is produced."
deadbeefab9b2d12015-10-14 11:33:11 -07002736
2737TEST(CreateSessionOptionsTest, GetOptionsForOfferWithInvalidAudioOption) {
2738 RTCOfferAnswerOptions rtc_options;
2739 rtc_options.offer_to_receive_audio = RTCOfferAnswerOptions::kUndefined - 1;
2740
2741 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08002742 EXPECT_FALSE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002743
2744 rtc_options.offer_to_receive_audio =
2745 RTCOfferAnswerOptions::kMaxOfferToReceiveMedia + 1;
htaaac2dea2016-03-10 13:35:55 -08002746 EXPECT_FALSE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002747}
2748
2749TEST(CreateSessionOptionsTest, GetOptionsForOfferWithInvalidVideoOption) {
2750 RTCOfferAnswerOptions rtc_options;
2751 rtc_options.offer_to_receive_video = RTCOfferAnswerOptions::kUndefined - 1;
2752
2753 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08002754 EXPECT_FALSE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002755
2756 rtc_options.offer_to_receive_video =
2757 RTCOfferAnswerOptions::kMaxOfferToReceiveMedia + 1;
htaaac2dea2016-03-10 13:35:55 -08002758 EXPECT_FALSE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002759}
2760
2761// Test that a MediaSessionOptions is created for an offer if
deadbeefc80741f2015-10-22 13:14:45 -07002762// OfferToReceiveAudio and OfferToReceiveVideo options are set.
deadbeefab9b2d12015-10-14 11:33:11 -07002763TEST(CreateSessionOptionsTest, GetMediaSessionOptionsForOfferWithAudioVideo) {
2764 RTCOfferAnswerOptions rtc_options;
2765 rtc_options.offer_to_receive_audio = 1;
2766 rtc_options.offer_to_receive_video = 1;
2767
2768 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08002769 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002770 EXPECT_TRUE(options.has_audio());
2771 EXPECT_TRUE(options.has_video());
2772 EXPECT_TRUE(options.bundle_enabled);
2773}
2774
2775// Test that a correct MediaSessionOptions is created for an offer if
deadbeefc80741f2015-10-22 13:14:45 -07002776// OfferToReceiveAudio is set.
deadbeefab9b2d12015-10-14 11:33:11 -07002777TEST(CreateSessionOptionsTest, GetMediaSessionOptionsForOfferWithAudio) {
2778 RTCOfferAnswerOptions rtc_options;
2779 rtc_options.offer_to_receive_audio = 1;
2780
2781 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08002782 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002783 EXPECT_TRUE(options.has_audio());
2784 EXPECT_FALSE(options.has_video());
2785 EXPECT_TRUE(options.bundle_enabled);
2786}
2787
2788// Test that a correct MediaSessionOptions is created for an offer if
deadbeefc80741f2015-10-22 13:14:45 -07002789// the default OfferOptions are used.
deadbeefab9b2d12015-10-14 11:33:11 -07002790TEST(CreateSessionOptionsTest, GetDefaultMediaSessionOptionsForOffer) {
2791 RTCOfferAnswerOptions rtc_options;
2792
2793 cricket::MediaSessionOptions options;
deadbeef0ed85b22016-02-23 17:24:52 -08002794 options.transport_options["audio"] = cricket::TransportOptions();
2795 options.transport_options["video"] = cricket::TransportOptions();
htaaac2dea2016-03-10 13:35:55 -08002796 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefc80741f2015-10-22 13:14:45 -07002797 EXPECT_TRUE(options.has_audio());
deadbeefab9b2d12015-10-14 11:33:11 -07002798 EXPECT_FALSE(options.has_video());
deadbeefc80741f2015-10-22 13:14:45 -07002799 EXPECT_TRUE(options.bundle_enabled);
deadbeefab9b2d12015-10-14 11:33:11 -07002800 EXPECT_TRUE(options.vad_enabled);
deadbeef0ed85b22016-02-23 17:24:52 -08002801 EXPECT_FALSE(options.transport_options["audio"].ice_restart);
2802 EXPECT_FALSE(options.transport_options["video"].ice_restart);
deadbeefab9b2d12015-10-14 11:33:11 -07002803}
2804
2805// Test that a correct MediaSessionOptions is created for an offer if
deadbeefc80741f2015-10-22 13:14:45 -07002806// OfferToReceiveVideo is set.
deadbeefab9b2d12015-10-14 11:33:11 -07002807TEST(CreateSessionOptionsTest, GetMediaSessionOptionsForOfferWithVideo) {
2808 RTCOfferAnswerOptions rtc_options;
2809 rtc_options.offer_to_receive_audio = 0;
2810 rtc_options.offer_to_receive_video = 1;
2811
2812 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08002813 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002814 EXPECT_FALSE(options.has_audio());
2815 EXPECT_TRUE(options.has_video());
2816 EXPECT_TRUE(options.bundle_enabled);
2817}
2818
2819// Test that a correct MediaSessionOptions is created for an offer if
2820// UseRtpMux is set to false.
2821TEST(CreateSessionOptionsTest,
2822 GetMediaSessionOptionsForOfferWithBundleDisabled) {
2823 RTCOfferAnswerOptions rtc_options;
2824 rtc_options.offer_to_receive_audio = 1;
2825 rtc_options.offer_to_receive_video = 1;
2826 rtc_options.use_rtp_mux = false;
2827
2828 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08002829 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002830 EXPECT_TRUE(options.has_audio());
2831 EXPECT_TRUE(options.has_video());
2832 EXPECT_FALSE(options.bundle_enabled);
2833}
2834
2835// Test that a correct MediaSessionOptions is created to restart ice if
2836// IceRestart is set. It also tests that subsequent MediaSessionOptions don't
Taylor Brandstetterf475d362016-01-08 15:35:57 -08002837// have |audio_transport_options.ice_restart| etc. set.
deadbeefab9b2d12015-10-14 11:33:11 -07002838TEST(CreateSessionOptionsTest, GetMediaSessionOptionsForOfferWithIceRestart) {
2839 RTCOfferAnswerOptions rtc_options;
2840 rtc_options.ice_restart = true;
2841
2842 cricket::MediaSessionOptions options;
deadbeef0ed85b22016-02-23 17:24:52 -08002843 options.transport_options["audio"] = cricket::TransportOptions();
2844 options.transport_options["video"] = cricket::TransportOptions();
htaaac2dea2016-03-10 13:35:55 -08002845 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeef0ed85b22016-02-23 17:24:52 -08002846 EXPECT_TRUE(options.transport_options["audio"].ice_restart);
2847 EXPECT_TRUE(options.transport_options["video"].ice_restart);
deadbeefab9b2d12015-10-14 11:33:11 -07002848
2849 rtc_options = RTCOfferAnswerOptions();
htaaac2dea2016-03-10 13:35:55 -08002850 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeef0ed85b22016-02-23 17:24:52 -08002851 EXPECT_FALSE(options.transport_options["audio"].ice_restart);
2852 EXPECT_FALSE(options.transport_options["video"].ice_restart);
deadbeefab9b2d12015-10-14 11:33:11 -07002853}
2854
2855// Test that the MediaConstraints in an answer don't affect if audio and video
2856// is offered in an offer but that if kOfferToReceiveAudio or
2857// kOfferToReceiveVideo constraints are true in an offer, the media type will be
2858// included in subsequent answers.
2859TEST(CreateSessionOptionsTest, MediaConstraintsInAnswer) {
2860 FakeConstraints answer_c;
2861 answer_c.SetMandatoryReceiveAudio(true);
2862 answer_c.SetMandatoryReceiveVideo(true);
2863
2864 cricket::MediaSessionOptions answer_options;
2865 EXPECT_TRUE(ParseConstraintsForAnswer(&answer_c, &answer_options));
2866 EXPECT_TRUE(answer_options.has_audio());
2867 EXPECT_TRUE(answer_options.has_video());
2868
deadbeefc80741f2015-10-22 13:14:45 -07002869 RTCOfferAnswerOptions rtc_offer_options;
deadbeefab9b2d12015-10-14 11:33:11 -07002870
2871 cricket::MediaSessionOptions offer_options;
htaaac2dea2016-03-10 13:35:55 -08002872 EXPECT_TRUE(
2873 ExtractMediaSessionOptions(rtc_offer_options, false, &offer_options));
deadbeefc80741f2015-10-22 13:14:45 -07002874 EXPECT_TRUE(offer_options.has_audio());
htaaac2dea2016-03-10 13:35:55 -08002875 EXPECT_TRUE(offer_options.has_video());
deadbeefab9b2d12015-10-14 11:33:11 -07002876
deadbeefc80741f2015-10-22 13:14:45 -07002877 RTCOfferAnswerOptions updated_rtc_offer_options;
2878 updated_rtc_offer_options.offer_to_receive_audio = 1;
2879 updated_rtc_offer_options.offer_to_receive_video = 1;
deadbeefab9b2d12015-10-14 11:33:11 -07002880
2881 cricket::MediaSessionOptions updated_offer_options;
htaaac2dea2016-03-10 13:35:55 -08002882 EXPECT_TRUE(ExtractMediaSessionOptions(updated_rtc_offer_options, false,
htaa2a49d92016-03-04 02:51:39 -08002883 &updated_offer_options));
deadbeefab9b2d12015-10-14 11:33:11 -07002884 EXPECT_TRUE(updated_offer_options.has_audio());
2885 EXPECT_TRUE(updated_offer_options.has_video());
2886
2887 // Since an offer has been created with both audio and video, subsequent
2888 // offers and answers should contain both audio and video.
2889 // Answers will only contain the media types that exist in the offer
2890 // regardless of the value of |updated_answer_options.has_audio| and
2891 // |updated_answer_options.has_video|.
2892 FakeConstraints updated_answer_c;
2893 answer_c.SetMandatoryReceiveAudio(false);
2894 answer_c.SetMandatoryReceiveVideo(false);
2895
2896 cricket::MediaSessionOptions updated_answer_options;
2897 EXPECT_TRUE(
2898 ParseConstraintsForAnswer(&updated_answer_c, &updated_answer_options));
2899 EXPECT_TRUE(updated_answer_options.has_audio());
2900 EXPECT_TRUE(updated_answer_options.has_video());
deadbeefab9b2d12015-10-14 11:33:11 -07002901}