blob: 3f84c3d75feaf7e88fdbeacd5b5f81f492c5d148 [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.
zhihuange9e94c32016-11-04 11:38:15 -07001580TEST_F(PeerConnectionInterfaceTest, GetStatsForInvalidTrack) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001581 InitiateCall();
zhihuang9763d562016-08-05 11:14:50 -07001582 rtc::scoped_refptr<AudioTrackInterface> unknown_audio_track(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001583 pc_factory_->CreateAudioTrack("unknown track", NULL));
1584 EXPECT_FALSE(DoGetStats(unknown_audio_track));
1585}
1586
1587// This test setup two RTP data channels in loop back.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001588TEST_F(PeerConnectionInterfaceTest, TestDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001589 FakeConstraints constraints;
1590 constraints.SetAllowRtpDataChannels();
1591 CreatePeerConnection(&constraints);
zhihuang9763d562016-08-05 11:14:50 -07001592 rtc::scoped_refptr<DataChannelInterface> data1 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001593 pc_->CreateDataChannel("test1", NULL);
zhihuang9763d562016-08-05 11:14:50 -07001594 rtc::scoped_refptr<DataChannelInterface> data2 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001595 pc_->CreateDataChannel("test2", NULL);
1596 ASSERT_TRUE(data1 != NULL);
kwibergd1fe2812016-04-27 06:47:29 -07001597 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001598 new MockDataChannelObserver(data1));
kwibergd1fe2812016-04-27 06:47:29 -07001599 std::unique_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001600 new MockDataChannelObserver(data2));
1601
1602 EXPECT_EQ(DataChannelInterface::kConnecting, data1->state());
1603 EXPECT_EQ(DataChannelInterface::kConnecting, data2->state());
1604 std::string data_to_send1 = "testing testing";
1605 std::string data_to_send2 = "testing something else";
1606 EXPECT_FALSE(data1->Send(DataBuffer(data_to_send1)));
1607
1608 CreateOfferReceiveAnswer();
1609 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1610 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
1611
1612 EXPECT_EQ(DataChannelInterface::kOpen, data1->state());
1613 EXPECT_EQ(DataChannelInterface::kOpen, data2->state());
1614 EXPECT_TRUE(data1->Send(DataBuffer(data_to_send1)));
1615 EXPECT_TRUE(data2->Send(DataBuffer(data_to_send2)));
1616
1617 EXPECT_EQ_WAIT(data_to_send1, observer1->last_message(), kTimeout);
1618 EXPECT_EQ_WAIT(data_to_send2, observer2->last_message(), kTimeout);
1619
1620 data1->Close();
1621 EXPECT_EQ(DataChannelInterface::kClosing, data1->state());
1622 CreateOfferReceiveAnswer();
1623 EXPECT_FALSE(observer1->IsOpen());
1624 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
1625 EXPECT_TRUE(observer2->IsOpen());
1626
1627 data_to_send2 = "testing something else again";
1628 EXPECT_TRUE(data2->Send(DataBuffer(data_to_send2)));
1629
1630 EXPECT_EQ_WAIT(data_to_send2, observer2->last_message(), kTimeout);
1631}
1632
1633// This test verifies that sendnig binary data over RTP data channels should
1634// fail.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001635TEST_F(PeerConnectionInterfaceTest, TestSendBinaryOnRtpDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001636 FakeConstraints constraints;
1637 constraints.SetAllowRtpDataChannels();
1638 CreatePeerConnection(&constraints);
zhihuang9763d562016-08-05 11:14:50 -07001639 rtc::scoped_refptr<DataChannelInterface> data1 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001640 pc_->CreateDataChannel("test1", NULL);
zhihuang9763d562016-08-05 11:14:50 -07001641 rtc::scoped_refptr<DataChannelInterface> data2 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001642 pc_->CreateDataChannel("test2", NULL);
1643 ASSERT_TRUE(data1 != NULL);
kwibergd1fe2812016-04-27 06:47:29 -07001644 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001645 new MockDataChannelObserver(data1));
kwibergd1fe2812016-04-27 06:47:29 -07001646 std::unique_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001647 new MockDataChannelObserver(data2));
1648
1649 EXPECT_EQ(DataChannelInterface::kConnecting, data1->state());
1650 EXPECT_EQ(DataChannelInterface::kConnecting, data2->state());
1651
1652 CreateOfferReceiveAnswer();
1653 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1654 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
1655
1656 EXPECT_EQ(DataChannelInterface::kOpen, data1->state());
1657 EXPECT_EQ(DataChannelInterface::kOpen, data2->state());
1658
jbaucheec21bd2016-03-20 06:15:43 -07001659 rtc::CopyOnWriteBuffer buffer("test", 4);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001660 EXPECT_FALSE(data1->Send(DataBuffer(buffer, true)));
1661}
1662
1663// This test setup a RTP data channels in loop back and test that a channel is
1664// opened even if the remote end answer with a zero SSRC.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001665TEST_F(PeerConnectionInterfaceTest, TestSendOnlyDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001666 FakeConstraints constraints;
1667 constraints.SetAllowRtpDataChannels();
1668 CreatePeerConnection(&constraints);
zhihuang9763d562016-08-05 11:14:50 -07001669 rtc::scoped_refptr<DataChannelInterface> data1 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001670 pc_->CreateDataChannel("test1", NULL);
kwibergd1fe2812016-04-27 06:47:29 -07001671 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001672 new MockDataChannelObserver(data1));
1673
1674 CreateOfferReceiveAnswerWithoutSsrc();
1675
1676 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1677
1678 data1->Close();
1679 EXPECT_EQ(DataChannelInterface::kClosing, data1->state());
1680 CreateOfferReceiveAnswerWithoutSsrc();
1681 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
1682 EXPECT_FALSE(observer1->IsOpen());
1683}
1684
1685// This test that if a data channel is added in an answer a receive only channel
1686// channel is created.
1687TEST_F(PeerConnectionInterfaceTest, TestReceiveOnlyDataChannel) {
1688 FakeConstraints constraints;
1689 constraints.SetAllowRtpDataChannels();
1690 CreatePeerConnection(&constraints);
1691
1692 std::string offer_label = "offer_channel";
zhihuang9763d562016-08-05 11:14:50 -07001693 rtc::scoped_refptr<DataChannelInterface> offer_channel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001694 pc_->CreateDataChannel(offer_label, NULL);
1695
1696 CreateOfferAsLocalDescription();
1697
1698 // Replace the data channel label in the offer and apply it as an answer.
1699 std::string receive_label = "answer_channel";
1700 std::string sdp;
1701 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001702 rtc::replace_substrs(offer_label.c_str(), offer_label.length(),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001703 receive_label.c_str(), receive_label.length(),
1704 &sdp);
1705 CreateAnswerAsRemoteDescription(sdp);
1706
1707 // Verify that a new incoming data channel has been created and that
1708 // it is open but can't we written to.
1709 ASSERT_TRUE(observer_.last_datachannel_ != NULL);
1710 DataChannelInterface* received_channel = observer_.last_datachannel_;
1711 EXPECT_EQ(DataChannelInterface::kConnecting, received_channel->state());
1712 EXPECT_EQ(receive_label, received_channel->label());
1713 EXPECT_FALSE(received_channel->Send(DataBuffer("something")));
1714
1715 // Verify that the channel we initially offered has been rejected.
1716 EXPECT_EQ(DataChannelInterface::kClosed, offer_channel->state());
1717
1718 // Do another offer / answer exchange and verify that the data channel is
1719 // opened.
1720 CreateOfferReceiveAnswer();
1721 EXPECT_EQ_WAIT(DataChannelInterface::kOpen, received_channel->state(),
1722 kTimeout);
1723}
1724
1725// This test that no data channel is returned if a reliable channel is
1726// requested.
1727// TODO(perkj): Remove this test once reliable channels are implemented.
1728TEST_F(PeerConnectionInterfaceTest, CreateReliableRtpDataChannelShouldFail) {
1729 FakeConstraints constraints;
1730 constraints.SetAllowRtpDataChannels();
1731 CreatePeerConnection(&constraints);
1732
1733 std::string label = "test";
1734 webrtc::DataChannelInit config;
1735 config.reliable = true;
zhihuang9763d562016-08-05 11:14:50 -07001736 rtc::scoped_refptr<DataChannelInterface> channel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001737 pc_->CreateDataChannel(label, &config);
1738 EXPECT_TRUE(channel == NULL);
1739}
1740
deadbeefab9b2d12015-10-14 11:33:11 -07001741// Verifies that duplicated label is not allowed for RTP data channel.
1742TEST_F(PeerConnectionInterfaceTest, RtpDuplicatedLabelNotAllowed) {
1743 FakeConstraints constraints;
1744 constraints.SetAllowRtpDataChannels();
1745 CreatePeerConnection(&constraints);
1746
1747 std::string label = "test";
zhihuang9763d562016-08-05 11:14:50 -07001748 rtc::scoped_refptr<DataChannelInterface> channel =
deadbeefab9b2d12015-10-14 11:33:11 -07001749 pc_->CreateDataChannel(label, nullptr);
1750 EXPECT_NE(channel, nullptr);
1751
zhihuang9763d562016-08-05 11:14:50 -07001752 rtc::scoped_refptr<DataChannelInterface> dup_channel =
deadbeefab9b2d12015-10-14 11:33:11 -07001753 pc_->CreateDataChannel(label, nullptr);
1754 EXPECT_EQ(dup_channel, nullptr);
1755}
1756
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001757// This tests that a SCTP data channel is returned using different
1758// DataChannelInit configurations.
1759TEST_F(PeerConnectionInterfaceTest, CreateSctpDataChannel) {
1760 FakeConstraints constraints;
1761 constraints.SetAllowDtlsSctpDataChannels();
1762 CreatePeerConnection(&constraints);
1763
1764 webrtc::DataChannelInit config;
1765
zhihuang9763d562016-08-05 11:14:50 -07001766 rtc::scoped_refptr<DataChannelInterface> channel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001767 pc_->CreateDataChannel("1", &config);
1768 EXPECT_TRUE(channel != NULL);
1769 EXPECT_TRUE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001770 EXPECT_TRUE(observer_.renegotiation_needed_);
1771 observer_.renegotiation_needed_ = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001772
1773 config.ordered = false;
1774 channel = pc_->CreateDataChannel("2", &config);
1775 EXPECT_TRUE(channel != NULL);
1776 EXPECT_TRUE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001777 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001778
1779 config.ordered = true;
1780 config.maxRetransmits = 0;
1781 channel = pc_->CreateDataChannel("3", &config);
1782 EXPECT_TRUE(channel != NULL);
1783 EXPECT_FALSE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001784 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001785
1786 config.maxRetransmits = -1;
1787 config.maxRetransmitTime = 0;
1788 channel = pc_->CreateDataChannel("4", &config);
1789 EXPECT_TRUE(channel != NULL);
1790 EXPECT_FALSE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001791 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001792}
1793
1794// This tests that no data channel is returned if both maxRetransmits and
1795// maxRetransmitTime are set for SCTP data channels.
1796TEST_F(PeerConnectionInterfaceTest,
1797 CreateSctpDataChannelShouldFailForInvalidConfig) {
1798 FakeConstraints constraints;
1799 constraints.SetAllowDtlsSctpDataChannels();
1800 CreatePeerConnection(&constraints);
1801
1802 std::string label = "test";
1803 webrtc::DataChannelInit config;
1804 config.maxRetransmits = 0;
1805 config.maxRetransmitTime = 0;
1806
zhihuang9763d562016-08-05 11:14:50 -07001807 rtc::scoped_refptr<DataChannelInterface> channel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001808 pc_->CreateDataChannel(label, &config);
1809 EXPECT_TRUE(channel == NULL);
1810}
1811
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001812// The test verifies that creating a SCTP data channel with an id already in use
1813// or out of range should fail.
1814TEST_F(PeerConnectionInterfaceTest,
1815 CreateSctpDataChannelWithInvalidIdShouldFail) {
1816 FakeConstraints constraints;
1817 constraints.SetAllowDtlsSctpDataChannels();
1818 CreatePeerConnection(&constraints);
1819
1820 webrtc::DataChannelInit config;
zhihuang9763d562016-08-05 11:14:50 -07001821 rtc::scoped_refptr<DataChannelInterface> channel;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001822
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001823 config.id = 1;
1824 channel = pc_->CreateDataChannel("1", &config);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001825 EXPECT_TRUE(channel != NULL);
1826 EXPECT_EQ(1, channel->id());
1827
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001828 channel = pc_->CreateDataChannel("x", &config);
1829 EXPECT_TRUE(channel == NULL);
1830
1831 config.id = cricket::kMaxSctpSid;
1832 channel = pc_->CreateDataChannel("max", &config);
1833 EXPECT_TRUE(channel != NULL);
1834 EXPECT_EQ(config.id, channel->id());
1835
1836 config.id = cricket::kMaxSctpSid + 1;
1837 channel = pc_->CreateDataChannel("x", &config);
1838 EXPECT_TRUE(channel == NULL);
1839}
1840
deadbeefab9b2d12015-10-14 11:33:11 -07001841// Verifies that duplicated label is allowed for SCTP data channel.
1842TEST_F(PeerConnectionInterfaceTest, SctpDuplicatedLabelAllowed) {
1843 FakeConstraints constraints;
1844 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
1845 true);
1846 CreatePeerConnection(&constraints);
1847
1848 std::string label = "test";
zhihuang9763d562016-08-05 11:14:50 -07001849 rtc::scoped_refptr<DataChannelInterface> channel =
deadbeefab9b2d12015-10-14 11:33:11 -07001850 pc_->CreateDataChannel(label, nullptr);
1851 EXPECT_NE(channel, nullptr);
1852
zhihuang9763d562016-08-05 11:14:50 -07001853 rtc::scoped_refptr<DataChannelInterface> dup_channel =
deadbeefab9b2d12015-10-14 11:33:11 -07001854 pc_->CreateDataChannel(label, nullptr);
1855 EXPECT_NE(dup_channel, nullptr);
1856}
1857
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001858// This test verifies that OnRenegotiationNeeded is fired for every new RTP
1859// DataChannel.
1860TEST_F(PeerConnectionInterfaceTest, RenegotiationNeededForNewRtpDataChannel) {
1861 FakeConstraints constraints;
1862 constraints.SetAllowRtpDataChannels();
1863 CreatePeerConnection(&constraints);
1864
zhihuang9763d562016-08-05 11:14:50 -07001865 rtc::scoped_refptr<DataChannelInterface> dc1 =
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001866 pc_->CreateDataChannel("test1", NULL);
1867 EXPECT_TRUE(observer_.renegotiation_needed_);
1868 observer_.renegotiation_needed_ = false;
1869
zhihuang9763d562016-08-05 11:14:50 -07001870 rtc::scoped_refptr<DataChannelInterface> dc2 =
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001871 pc_->CreateDataChannel("test2", NULL);
1872 EXPECT_TRUE(observer_.renegotiation_needed_);
1873}
1874
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001875// This test that a data channel closes when a PeerConnection is deleted/closed.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001876TEST_F(PeerConnectionInterfaceTest, DataChannelCloseWhenPeerConnectionClose) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001877 FakeConstraints constraints;
1878 constraints.SetAllowRtpDataChannels();
1879 CreatePeerConnection(&constraints);
1880
zhihuang9763d562016-08-05 11:14:50 -07001881 rtc::scoped_refptr<DataChannelInterface> data1 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001882 pc_->CreateDataChannel("test1", NULL);
zhihuang9763d562016-08-05 11:14:50 -07001883 rtc::scoped_refptr<DataChannelInterface> data2 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001884 pc_->CreateDataChannel("test2", NULL);
1885 ASSERT_TRUE(data1 != NULL);
kwibergd1fe2812016-04-27 06:47:29 -07001886 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001887 new MockDataChannelObserver(data1));
kwibergd1fe2812016-04-27 06:47:29 -07001888 std::unique_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001889 new MockDataChannelObserver(data2));
1890
1891 CreateOfferReceiveAnswer();
1892 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1893 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
1894
1895 ReleasePeerConnection();
1896 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
1897 EXPECT_EQ(DataChannelInterface::kClosed, data2->state());
1898}
1899
1900// This test that data channels can be rejected in an answer.
1901TEST_F(PeerConnectionInterfaceTest, TestRejectDataChannelInAnswer) {
1902 FakeConstraints constraints;
1903 constraints.SetAllowRtpDataChannels();
1904 CreatePeerConnection(&constraints);
1905
zhihuang9763d562016-08-05 11:14:50 -07001906 rtc::scoped_refptr<DataChannelInterface> offer_channel(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001907 pc_->CreateDataChannel("offer_channel", NULL));
1908
1909 CreateOfferAsLocalDescription();
1910
1911 // Create an answer where the m-line for data channels are rejected.
1912 std::string sdp;
1913 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
1914 webrtc::JsepSessionDescription* answer = new webrtc::JsepSessionDescription(
1915 SessionDescriptionInterface::kAnswer);
1916 EXPECT_TRUE(answer->Initialize(sdp, NULL));
1917 cricket::ContentInfo* data_info =
1918 answer->description()->GetContentByName("data");
1919 data_info->rejected = true;
1920
1921 DoSetRemoteDescription(answer);
1922 EXPECT_EQ(DataChannelInterface::kClosed, offer_channel->state());
1923}
1924
1925// Test that we can create a session description from an SDP string from
1926// FireFox, use it as a remote session description, generate an answer and use
1927// the answer as a local description.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07001928TEST_F(PeerConnectionInterfaceTest, ReceiveFireFoxOffer) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001929 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001930 FakeConstraints constraints;
1931 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
1932 true);
1933 CreatePeerConnection(&constraints);
1934 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
1935 SessionDescriptionInterface* desc =
1936 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
jbauchfabe2c92015-07-16 13:43:14 -07001937 webrtc::kFireFoxSdpOffer, nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001938 EXPECT_TRUE(DoSetSessionDescription(desc, false));
1939 CreateAnswerAsLocalDescription();
1940 ASSERT_TRUE(pc_->local_description() != NULL);
1941 ASSERT_TRUE(pc_->remote_description() != NULL);
1942
1943 const cricket::ContentInfo* content =
1944 cricket::GetFirstAudioContent(pc_->local_description()->description());
1945 ASSERT_TRUE(content != NULL);
1946 EXPECT_FALSE(content->rejected);
1947
1948 content =
1949 cricket::GetFirstVideoContent(pc_->local_description()->description());
1950 ASSERT_TRUE(content != NULL);
1951 EXPECT_FALSE(content->rejected);
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00001952#ifdef HAVE_SCTP
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001953 content =
1954 cricket::GetFirstDataContent(pc_->local_description()->description());
1955 ASSERT_TRUE(content != NULL);
1956 EXPECT_TRUE(content->rejected);
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00001957#endif
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001958}
1959
1960// Test that we can create an audio only offer and receive an answer with a
1961// limited set of audio codecs and receive an updated offer with more audio
1962// codecs, where the added codecs are not supported.
1963TEST_F(PeerConnectionInterfaceTest, ReceiveUpdatedAudioOfferWithBadCodecs) {
1964 CreatePeerConnection();
1965 AddVoiceStream("audio_label");
1966 CreateOfferAsLocalDescription();
1967
1968 SessionDescriptionInterface* answer =
1969 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
jbauchfabe2c92015-07-16 13:43:14 -07001970 webrtc::kAudioSdp, nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001971 EXPECT_TRUE(DoSetSessionDescription(answer, false));
1972
1973 SessionDescriptionInterface* updated_offer =
1974 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
jbauchfabe2c92015-07-16 13:43:14 -07001975 webrtc::kAudioSdpWithUnsupportedCodecs,
1976 nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001977 EXPECT_TRUE(DoSetSessionDescription(updated_offer, false));
1978 CreateAnswerAsLocalDescription();
1979}
1980
deadbeefc80741f2015-10-22 13:14:45 -07001981// Test that if we're receiving (but not sending) a track, subsequent offers
1982// will have m-lines with a=recvonly.
1983TEST_F(PeerConnectionInterfaceTest, CreateSubsequentRecvOnlyOffer) {
1984 FakeConstraints constraints;
1985 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
1986 true);
1987 CreatePeerConnection(&constraints);
1988 CreateAndSetRemoteOffer(kSdpStringWithStream1);
1989 CreateAnswerAsLocalDescription();
1990
1991 // At this point we should be receiving stream 1, but not sending anything.
1992 // A new offer should be recvonly.
kwibergd1fe2812016-04-27 06:47:29 -07001993 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefc80741f2015-10-22 13:14:45 -07001994 DoCreateOffer(&offer, nullptr);
1995
1996 const cricket::ContentInfo* video_content =
1997 cricket::GetFirstVideoContent(offer->description());
1998 const cricket::VideoContentDescription* video_desc =
1999 static_cast<const cricket::VideoContentDescription*>(
2000 video_content->description);
2001 ASSERT_EQ(cricket::MD_RECVONLY, video_desc->direction());
2002
2003 const cricket::ContentInfo* audio_content =
2004 cricket::GetFirstAudioContent(offer->description());
2005 const cricket::AudioContentDescription* audio_desc =
2006 static_cast<const cricket::AudioContentDescription*>(
2007 audio_content->description);
2008 ASSERT_EQ(cricket::MD_RECVONLY, audio_desc->direction());
2009}
2010
2011// Test that if we're receiving (but not sending) a track, and the
2012// offerToReceiveVideo/offerToReceiveAudio constraints are explicitly set to
2013// false, the generated m-lines will be a=inactive.
2014TEST_F(PeerConnectionInterfaceTest, CreateSubsequentInactiveOffer) {
2015 FakeConstraints constraints;
2016 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2017 true);
2018 CreatePeerConnection(&constraints);
2019 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2020 CreateAnswerAsLocalDescription();
2021
2022 // At this point we should be receiving stream 1, but not sending anything.
2023 // A new offer would be recvonly, but we'll set the "no receive" constraints
2024 // to make it inactive.
kwibergd1fe2812016-04-27 06:47:29 -07002025 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefc80741f2015-10-22 13:14:45 -07002026 FakeConstraints offer_constraints;
2027 offer_constraints.AddMandatory(
2028 webrtc::MediaConstraintsInterface::kOfferToReceiveVideo, false);
2029 offer_constraints.AddMandatory(
2030 webrtc::MediaConstraintsInterface::kOfferToReceiveAudio, false);
2031 DoCreateOffer(&offer, &offer_constraints);
2032
2033 const cricket::ContentInfo* video_content =
2034 cricket::GetFirstVideoContent(offer->description());
2035 const cricket::VideoContentDescription* video_desc =
2036 static_cast<const cricket::VideoContentDescription*>(
2037 video_content->description);
2038 ASSERT_EQ(cricket::MD_INACTIVE, video_desc->direction());
2039
2040 const cricket::ContentInfo* audio_content =
2041 cricket::GetFirstAudioContent(offer->description());
2042 const cricket::AudioContentDescription* audio_desc =
2043 static_cast<const cricket::AudioContentDescription*>(
2044 audio_content->description);
2045 ASSERT_EQ(cricket::MD_INACTIVE, audio_desc->direction());
2046}
2047
deadbeef653b8e02015-11-11 12:55:10 -08002048// Test that we can use SetConfiguration to change the ICE servers of the
2049// PortAllocator.
2050TEST_F(PeerConnectionInterfaceTest, SetConfigurationChangesIceServers) {
2051 CreatePeerConnection();
2052
2053 PeerConnectionInterface::RTCConfiguration config;
2054 PeerConnectionInterface::IceServer server;
2055 server.uri = "stun:test_hostname";
2056 config.servers.push_back(server);
2057 EXPECT_TRUE(pc_->SetConfiguration(config));
2058
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002059 EXPECT_EQ(1u, port_allocator_->stun_servers().size());
2060 EXPECT_EQ("test_hostname",
2061 port_allocator_->stun_servers().begin()->hostname());
deadbeef653b8e02015-11-11 12:55:10 -08002062}
2063
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002064TEST_F(PeerConnectionInterfaceTest, SetConfigurationChangesCandidateFilter) {
2065 CreatePeerConnection();
2066 PeerConnectionInterface::RTCConfiguration config;
2067 config.type = PeerConnectionInterface::kRelay;
2068 EXPECT_TRUE(pc_->SetConfiguration(config));
2069 EXPECT_EQ(cricket::CF_RELAY, port_allocator_->candidate_filter());
2070}
2071
2072// Test that when SetConfiguration changes both the pool size and other
2073// attributes, the pooled session is created with the updated attributes.
2074TEST_F(PeerConnectionInterfaceTest,
2075 SetConfigurationCreatesPooledSessionCorrectly) {
2076 CreatePeerConnection();
2077 PeerConnectionInterface::RTCConfiguration config;
2078 config.ice_candidate_pool_size = 1;
2079 PeerConnectionInterface::IceServer server;
2080 server.uri = kStunAddressOnly;
2081 config.servers.push_back(server);
2082 config.type = PeerConnectionInterface::kRelay;
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07002083 EXPECT_TRUE(pc_->SetConfiguration(config));
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002084
2085 const cricket::FakePortAllocatorSession* session =
2086 static_cast<const cricket::FakePortAllocatorSession*>(
2087 port_allocator_->GetPooledSession());
2088 ASSERT_NE(nullptr, session);
2089 EXPECT_EQ(1UL, session->stun_servers().size());
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002090}
2091
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002092// Test that PeerConnection::Close changes the states to closed and all remote
2093// tracks change state to ended.
2094TEST_F(PeerConnectionInterfaceTest, CloseAndTestStreamsAndStates) {
2095 // Initialize a PeerConnection and negotiate local and remote session
2096 // description.
2097 InitiateCall();
2098 ASSERT_EQ(1u, pc_->local_streams()->count());
2099 ASSERT_EQ(1u, pc_->remote_streams()->count());
2100
2101 pc_->Close();
2102
2103 EXPECT_EQ(PeerConnectionInterface::kClosed, pc_->signaling_state());
2104 EXPECT_EQ(PeerConnectionInterface::kIceConnectionClosed,
2105 pc_->ice_connection_state());
2106 EXPECT_EQ(PeerConnectionInterface::kIceGatheringComplete,
2107 pc_->ice_gathering_state());
2108
2109 EXPECT_EQ(1u, pc_->local_streams()->count());
2110 EXPECT_EQ(1u, pc_->remote_streams()->count());
2111
zhihuang9763d562016-08-05 11:14:50 -07002112 rtc::scoped_refptr<MediaStreamInterface> remote_stream =
2113 pc_->remote_streams()->at(0);
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002114 // Track state may be updated asynchronously.
perkjd61bf802016-03-24 03:16:19 -07002115 EXPECT_EQ_WAIT(MediaStreamTrackInterface::kEnded,
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002116 remote_stream->GetAudioTracks()[0]->state(), kTimeout);
2117 EXPECT_EQ_WAIT(MediaStreamTrackInterface::kEnded,
2118 remote_stream->GetVideoTracks()[0]->state(), kTimeout);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002119}
2120
2121// Test that PeerConnection methods fails gracefully after
2122// PeerConnection::Close has been called.
2123TEST_F(PeerConnectionInterfaceTest, CloseAndTestMethods) {
2124 CreatePeerConnection();
2125 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
2126 CreateOfferAsRemoteDescription();
2127 CreateAnswerAsLocalDescription();
2128
2129 ASSERT_EQ(1u, pc_->local_streams()->count());
zhihuang9763d562016-08-05 11:14:50 -07002130 rtc::scoped_refptr<MediaStreamInterface> local_stream =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002131 pc_->local_streams()->at(0);
2132
2133 pc_->Close();
2134
2135 pc_->RemoveStream(local_stream);
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +00002136 EXPECT_FALSE(pc_->AddStream(local_stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002137
2138 ASSERT_FALSE(local_stream->GetAudioTracks().empty());
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002139 rtc::scoped_refptr<webrtc::DtmfSenderInterface> dtmf_sender(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002140 pc_->CreateDtmfSender(local_stream->GetAudioTracks()[0]));
wu@webrtc.org66037362013-08-13 00:09:35 +00002141 EXPECT_TRUE(NULL == dtmf_sender); // local stream has been removed.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002142
2143 EXPECT_TRUE(pc_->CreateDataChannel("test", NULL) == NULL);
2144
2145 EXPECT_TRUE(pc_->local_description() != NULL);
2146 EXPECT_TRUE(pc_->remote_description() != NULL);
2147
kwibergd1fe2812016-04-27 06:47:29 -07002148 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07002149 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
kwibergd1fe2812016-04-27 06:47:29 -07002150 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -07002151 EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002152
2153 std::string sdp;
2154 ASSERT_TRUE(pc_->remote_description()->ToString(&sdp));
2155 SessionDescriptionInterface* remote_offer =
2156 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
2157 sdp, NULL);
2158 EXPECT_FALSE(DoSetRemoteDescription(remote_offer));
2159
2160 ASSERT_TRUE(pc_->local_description()->ToString(&sdp));
2161 SessionDescriptionInterface* local_offer =
2162 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
2163 sdp, NULL);
2164 EXPECT_FALSE(DoSetLocalDescription(local_offer));
2165}
2166
2167// Test that GetStats can still be called after PeerConnection::Close.
2168TEST_F(PeerConnectionInterfaceTest, CloseAndGetStats) {
2169 InitiateCall();
2170 pc_->Close();
2171 DoGetStats(NULL);
2172}
deadbeefab9b2d12015-10-14 11:33:11 -07002173
2174// NOTE: The series of tests below come from what used to be
2175// mediastreamsignaling_unittest.cc, and are mostly aimed at testing that
2176// setting a remote or local description has the expected effects.
2177
2178// This test verifies that the remote MediaStreams corresponding to a received
2179// SDP string is created. In this test the two separate MediaStreams are
2180// signaled.
2181TEST_F(PeerConnectionInterfaceTest, UpdateRemoteStreams) {
2182 FakeConstraints constraints;
2183 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2184 true);
2185 CreatePeerConnection(&constraints);
2186 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2187
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002188 rtc::scoped_refptr<StreamCollection> reference(CreateStreamCollection(1, 1));
deadbeefab9b2d12015-10-14 11:33:11 -07002189 EXPECT_TRUE(
2190 CompareStreamCollections(observer_.remote_streams(), reference.get()));
2191 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2192 EXPECT_TRUE(remote_stream->GetVideoTracks()[0]->GetSource() != nullptr);
2193
2194 // Create a session description based on another SDP with another
2195 // MediaStream.
2196 CreateAndSetRemoteOffer(kSdpStringWithStream1And2);
2197
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002198 rtc::scoped_refptr<StreamCollection> reference2(CreateStreamCollection(2, 1));
deadbeefab9b2d12015-10-14 11:33:11 -07002199 EXPECT_TRUE(
2200 CompareStreamCollections(observer_.remote_streams(), reference2.get()));
2201}
2202
2203// This test verifies that when remote tracks are added/removed from SDP, the
2204// created remote streams are updated appropriately.
2205TEST_F(PeerConnectionInterfaceTest,
2206 AddRemoveTrackFromExistingRemoteMediaStream) {
2207 FakeConstraints constraints;
2208 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2209 true);
2210 CreatePeerConnection(&constraints);
kwibergd1fe2812016-04-27 06:47:29 -07002211 std::unique_ptr<SessionDescriptionInterface> desc_ms1 =
kwiberg2bbff992016-03-16 11:03:04 -07002212 CreateSessionDescriptionAndReference(1, 1);
deadbeefab9b2d12015-10-14 11:33:11 -07002213 EXPECT_TRUE(DoSetRemoteDescription(desc_ms1.release()));
2214 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
2215 reference_collection_));
2216
2217 // Add extra audio and video tracks to the same MediaStream.
kwibergd1fe2812016-04-27 06:47:29 -07002218 std::unique_ptr<SessionDescriptionInterface> desc_ms1_two_tracks =
kwiberg2bbff992016-03-16 11:03:04 -07002219 CreateSessionDescriptionAndReference(2, 2);
deadbeefab9b2d12015-10-14 11:33:11 -07002220 EXPECT_TRUE(DoSetRemoteDescription(desc_ms1_two_tracks.release()));
2221 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
2222 reference_collection_));
zhihuang9763d562016-08-05 11:14:50 -07002223 rtc::scoped_refptr<AudioTrackInterface> audio_track2 =
perkjd61bf802016-03-24 03:16:19 -07002224 observer_.remote_streams()->at(0)->GetAudioTracks()[1];
2225 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, audio_track2->state());
zhihuang9763d562016-08-05 11:14:50 -07002226 rtc::scoped_refptr<VideoTrackInterface> video_track2 =
perkjd61bf802016-03-24 03:16:19 -07002227 observer_.remote_streams()->at(0)->GetVideoTracks()[1];
2228 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, video_track2->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002229
2230 // Remove the extra audio and video tracks.
kwibergd1fe2812016-04-27 06:47:29 -07002231 std::unique_ptr<SessionDescriptionInterface> desc_ms2 =
kwiberg2bbff992016-03-16 11:03:04 -07002232 CreateSessionDescriptionAndReference(1, 1);
perkjd61bf802016-03-24 03:16:19 -07002233 MockTrackObserver audio_track_observer(audio_track2);
2234 MockTrackObserver video_track_observer(video_track2);
2235
2236 EXPECT_CALL(audio_track_observer, OnChanged()).Times(Exactly(1));
2237 EXPECT_CALL(video_track_observer, OnChanged()).Times(Exactly(1));
deadbeefab9b2d12015-10-14 11:33:11 -07002238 EXPECT_TRUE(DoSetRemoteDescription(desc_ms2.release()));
2239 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
2240 reference_collection_));
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002241 // Track state may be updated asynchronously.
perkjd61bf802016-03-24 03:16:19 -07002242 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002243 audio_track2->state(), kTimeout);
2244 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
2245 video_track2->state(), kTimeout);
deadbeefab9b2d12015-10-14 11:33:11 -07002246}
2247
2248// This tests that remote tracks are ended if a local session description is set
2249// that rejects the media content type.
2250TEST_F(PeerConnectionInterfaceTest, RejectMediaContent) {
2251 FakeConstraints constraints;
2252 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2253 true);
2254 CreatePeerConnection(&constraints);
2255 // First create and set a remote offer, then reject its video content in our
2256 // answer.
2257 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2258 ASSERT_EQ(1u, observer_.remote_streams()->count());
2259 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2260 ASSERT_EQ(1u, remote_stream->GetVideoTracks().size());
2261 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2262
2263 rtc::scoped_refptr<webrtc::VideoTrackInterface> remote_video =
2264 remote_stream->GetVideoTracks()[0];
2265 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, remote_video->state());
2266 rtc::scoped_refptr<webrtc::AudioTrackInterface> remote_audio =
2267 remote_stream->GetAudioTracks()[0];
2268 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, remote_audio->state());
2269
kwibergd1fe2812016-04-27 06:47:29 -07002270 std::unique_ptr<SessionDescriptionInterface> local_answer;
kwiberg2bbff992016-03-16 11:03:04 -07002271 EXPECT_TRUE(DoCreateAnswer(&local_answer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07002272 cricket::ContentInfo* video_info =
2273 local_answer->description()->GetContentByName("video");
2274 video_info->rejected = true;
2275 EXPECT_TRUE(DoSetLocalDescription(local_answer.release()));
2276 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kEnded, remote_video->state());
2277 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, remote_audio->state());
2278
2279 // Now create an offer where we reject both video and audio.
kwibergd1fe2812016-04-27 06:47:29 -07002280 std::unique_ptr<SessionDescriptionInterface> local_offer;
kwiberg2bbff992016-03-16 11:03:04 -07002281 EXPECT_TRUE(DoCreateOffer(&local_offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07002282 video_info = local_offer->description()->GetContentByName("video");
2283 ASSERT_TRUE(video_info != nullptr);
2284 video_info->rejected = true;
2285 cricket::ContentInfo* audio_info =
2286 local_offer->description()->GetContentByName("audio");
2287 ASSERT_TRUE(audio_info != nullptr);
2288 audio_info->rejected = true;
2289 EXPECT_TRUE(DoSetLocalDescription(local_offer.release()));
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002290 // Track state may be updated asynchronously.
perkjd61bf802016-03-24 03:16:19 -07002291 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002292 remote_audio->state(), kTimeout);
2293 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
2294 remote_video->state(), kTimeout);
deadbeefab9b2d12015-10-14 11:33:11 -07002295}
2296
2297// This tests that we won't crash if the remote track has been removed outside
2298// of PeerConnection and then PeerConnection tries to reject the track.
2299TEST_F(PeerConnectionInterfaceTest, RemoveTrackThenRejectMediaContent) {
2300 FakeConstraints constraints;
2301 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2302 true);
2303 CreatePeerConnection(&constraints);
2304 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2305 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2306 remote_stream->RemoveTrack(remote_stream->GetVideoTracks()[0]);
2307 remote_stream->RemoveTrack(remote_stream->GetAudioTracks()[0]);
2308
kwibergd1fe2812016-04-27 06:47:29 -07002309 std::unique_ptr<SessionDescriptionInterface> local_answer(
deadbeefab9b2d12015-10-14 11:33:11 -07002310 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
2311 kSdpStringWithStream1, nullptr));
2312 cricket::ContentInfo* video_info =
2313 local_answer->description()->GetContentByName("video");
2314 video_info->rejected = true;
2315 cricket::ContentInfo* audio_info =
2316 local_answer->description()->GetContentByName("audio");
2317 audio_info->rejected = true;
2318 EXPECT_TRUE(DoSetLocalDescription(local_answer.release()));
2319
2320 // No crash is a pass.
2321}
2322
deadbeef5e97fb52015-10-15 12:49:08 -07002323// This tests that if a recvonly remote description is set, no remote streams
2324// will be created, even if the description contains SSRCs/MSIDs.
2325// See: https://code.google.com/p/webrtc/issues/detail?id=5054
2326TEST_F(PeerConnectionInterfaceTest, RecvonlyDescriptionDoesntCreateStream) {
2327 FakeConstraints constraints;
2328 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2329 true);
2330 CreatePeerConnection(&constraints);
2331
2332 std::string recvonly_offer = kSdpStringWithStream1;
2333 rtc::replace_substrs(kSendrecv, strlen(kSendrecv), kRecvonly,
2334 strlen(kRecvonly), &recvonly_offer);
2335 CreateAndSetRemoteOffer(recvonly_offer);
2336
2337 EXPECT_EQ(0u, observer_.remote_streams()->count());
2338}
2339
deadbeefab9b2d12015-10-14 11:33:11 -07002340// This tests that a default MediaStream is created if a remote session
2341// description doesn't contain any streams and no MSID support.
2342// It also tests that the default stream is updated if a video m-line is added
2343// in a subsequent session description.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002344TEST_F(PeerConnectionInterfaceTest, SdpWithoutMsidCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002345 FakeConstraints constraints;
2346 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2347 true);
2348 CreatePeerConnection(&constraints);
2349 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2350
2351 ASSERT_EQ(1u, observer_.remote_streams()->count());
2352 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2353
2354 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2355 EXPECT_EQ(0u, remote_stream->GetVideoTracks().size());
2356 EXPECT_EQ("default", remote_stream->label());
2357
2358 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2359 ASSERT_EQ(1u, observer_.remote_streams()->count());
2360 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2361 EXPECT_EQ("defaulta0", remote_stream->GetAudioTracks()[0]->id());
deadbeef884f5852016-01-15 09:20:04 -08002362 EXPECT_EQ(MediaStreamTrackInterface::kLive,
2363 remote_stream->GetAudioTracks()[0]->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002364 ASSERT_EQ(1u, remote_stream->GetVideoTracks().size());
2365 EXPECT_EQ("defaultv0", remote_stream->GetVideoTracks()[0]->id());
deadbeef884f5852016-01-15 09:20:04 -08002366 EXPECT_EQ(MediaStreamTrackInterface::kLive,
2367 remote_stream->GetVideoTracks()[0]->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002368}
2369
2370// This tests that a default MediaStream is created if a remote session
2371// description doesn't contain any streams and media direction is send only.
2372TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002373 SendOnlySdpWithoutMsidCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002374 FakeConstraints constraints;
2375 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2376 true);
2377 CreatePeerConnection(&constraints);
2378 CreateAndSetRemoteOffer(kSdpStringSendOnlyWithoutStreams);
2379
2380 ASSERT_EQ(1u, observer_.remote_streams()->count());
2381 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2382
2383 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2384 EXPECT_EQ(1u, remote_stream->GetVideoTracks().size());
2385 EXPECT_EQ("default", remote_stream->label());
2386}
2387
2388// This tests that it won't crash when PeerConnection tries to remove
2389// a remote track that as already been removed from the MediaStream.
2390TEST_F(PeerConnectionInterfaceTest, RemoveAlreadyGoneRemoteStream) {
2391 FakeConstraints constraints;
2392 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2393 true);
2394 CreatePeerConnection(&constraints);
2395 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2396 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2397 remote_stream->RemoveTrack(remote_stream->GetAudioTracks()[0]);
2398 remote_stream->RemoveTrack(remote_stream->GetVideoTracks()[0]);
2399
2400 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2401
2402 // No crash is a pass.
2403}
2404
2405// This tests that a default MediaStream is created if the remote session
2406// description doesn't contain any streams and don't contain an indication if
2407// MSID is supported.
2408TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002409 SdpWithoutMsidAndStreamsCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002410 FakeConstraints constraints;
2411 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2412 true);
2413 CreatePeerConnection(&constraints);
2414 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2415
2416 ASSERT_EQ(1u, observer_.remote_streams()->count());
2417 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2418 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2419 EXPECT_EQ(1u, remote_stream->GetVideoTracks().size());
2420}
2421
2422// This tests that a default MediaStream is not created if the remote session
2423// description doesn't contain any streams but does support MSID.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002424TEST_F(PeerConnectionInterfaceTest, SdpWithMsidDontCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002425 FakeConstraints constraints;
2426 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2427 true);
2428 CreatePeerConnection(&constraints);
2429 CreateAndSetRemoteOffer(kSdpStringWithMsidWithoutStreams);
2430 EXPECT_EQ(0u, observer_.remote_streams()->count());
2431}
2432
deadbeefbda7e0b2015-12-08 17:13:40 -08002433// This tests that when setting a new description, the old default tracks are
2434// not destroyed and recreated.
2435// See: https://bugs.chromium.org/p/webrtc/issues/detail?id=5250
Stefan Holmer102362b2016-03-18 09:39:07 +01002436TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002437 DefaultTracksNotDestroyedAndRecreated) {
deadbeefbda7e0b2015-12-08 17:13:40 -08002438 FakeConstraints constraints;
2439 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2440 true);
2441 CreatePeerConnection(&constraints);
2442 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2443
2444 ASSERT_EQ(1u, observer_.remote_streams()->count());
2445 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2446 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2447
2448 // Set the track to "disabled", then set a new description and ensure the
2449 // track is still disabled, which ensures it hasn't been recreated.
2450 remote_stream->GetAudioTracks()[0]->set_enabled(false);
2451 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2452 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2453 EXPECT_FALSE(remote_stream->GetAudioTracks()[0]->enabled());
2454}
2455
deadbeefab9b2d12015-10-14 11:33:11 -07002456// This tests that a default MediaStream is not created if a remote session
2457// description is updated to not have any MediaStreams.
2458TEST_F(PeerConnectionInterfaceTest, VerifyDefaultStreamIsNotCreated) {
2459 FakeConstraints constraints;
2460 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2461 true);
2462 CreatePeerConnection(&constraints);
2463 CreateAndSetRemoteOffer(kSdpStringWithStream1);
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002464 rtc::scoped_refptr<StreamCollection> reference(CreateStreamCollection(1, 1));
deadbeefab9b2d12015-10-14 11:33:11 -07002465 EXPECT_TRUE(
2466 CompareStreamCollections(observer_.remote_streams(), reference.get()));
2467
2468 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2469 EXPECT_EQ(0u, observer_.remote_streams()->count());
2470}
2471
2472// This tests that an RtpSender is created when the local description is set
2473// after adding a local stream.
2474// TODO(deadbeef): This test and the one below it need to be updated when
2475// an RtpSender's lifetime isn't determined by when a local description is set.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002476TEST_F(PeerConnectionInterfaceTest, LocalDescriptionChanged) {
deadbeefab9b2d12015-10-14 11:33:11 -07002477 FakeConstraints constraints;
2478 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2479 true);
2480 CreatePeerConnection(&constraints);
deadbeefab9b2d12015-10-14 11:33:11 -07002481
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002482 // Create an offer with 1 stream with 2 tracks of each type.
2483 rtc::scoped_refptr<StreamCollection> stream_collection =
2484 CreateStreamCollection(1, 2);
2485 pc_->AddStream(stream_collection->at(0));
2486 std::unique_ptr<SessionDescriptionInterface> offer;
2487 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2488 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
deadbeefab9b2d12015-10-14 11:33:11 -07002489
deadbeefab9b2d12015-10-14 11:33:11 -07002490 auto senders = pc_->GetSenders();
2491 EXPECT_EQ(4u, senders.size());
2492 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2493 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2494 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[1]));
2495 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[1]));
2496
2497 // Remove an audio and video track.
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002498 pc_->RemoveStream(stream_collection->at(0));
2499 stream_collection = CreateStreamCollection(1, 1);
2500 pc_->AddStream(stream_collection->at(0));
2501 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2502 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
2503
deadbeefab9b2d12015-10-14 11:33:11 -07002504 senders = pc_->GetSenders();
2505 EXPECT_EQ(2u, senders.size());
2506 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2507 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2508 EXPECT_FALSE(ContainsSender(senders, kAudioTracks[1]));
2509 EXPECT_FALSE(ContainsSender(senders, kVideoTracks[1]));
2510}
2511
2512// This tests that an RtpSender is created when the local description is set
2513// before adding a local stream.
2514TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002515 AddLocalStreamAfterLocalDescriptionChanged) {
deadbeefab9b2d12015-10-14 11:33:11 -07002516 FakeConstraints constraints;
2517 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2518 true);
2519 CreatePeerConnection(&constraints);
deadbeefab9b2d12015-10-14 11:33:11 -07002520
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002521 rtc::scoped_refptr<StreamCollection> stream_collection =
2522 CreateStreamCollection(1, 2);
2523 // Add a stream to create the offer, but remove it afterwards.
2524 pc_->AddStream(stream_collection->at(0));
2525 std::unique_ptr<SessionDescriptionInterface> offer;
2526 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2527 pc_->RemoveStream(stream_collection->at(0));
deadbeefab9b2d12015-10-14 11:33:11 -07002528
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002529 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
deadbeefab9b2d12015-10-14 11:33:11 -07002530 auto senders = pc_->GetSenders();
2531 EXPECT_EQ(0u, senders.size());
2532
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002533 pc_->AddStream(stream_collection->at(0));
deadbeefab9b2d12015-10-14 11:33:11 -07002534 senders = pc_->GetSenders();
2535 EXPECT_EQ(4u, senders.size());
2536 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2537 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2538 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[1]));
2539 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[1]));
2540}
2541
2542// This tests that the expected behavior occurs if the SSRC on a local track is
2543// changed when SetLocalDescription is called.
2544TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002545 ChangeSsrcOnTrackInLocalSessionDescription) {
deadbeefab9b2d12015-10-14 11:33:11 -07002546 FakeConstraints constraints;
2547 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2548 true);
2549 CreatePeerConnection(&constraints);
deadbeefab9b2d12015-10-14 11:33:11 -07002550
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002551 rtc::scoped_refptr<StreamCollection> stream_collection =
2552 CreateStreamCollection(2, 1);
2553 pc_->AddStream(stream_collection->at(0));
2554 std::unique_ptr<SessionDescriptionInterface> offer;
2555 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2556 // Grab a copy of the offer before it gets passed into the PC.
2557 std::unique_ptr<JsepSessionDescription> modified_offer(
2558 new JsepSessionDescription(JsepSessionDescription::kOffer));
2559 modified_offer->Initialize(offer->description()->Copy(), offer->session_id(),
2560 offer->session_version());
2561 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
deadbeefab9b2d12015-10-14 11:33:11 -07002562
deadbeefab9b2d12015-10-14 11:33:11 -07002563 auto senders = pc_->GetSenders();
2564 EXPECT_EQ(2u, senders.size());
2565 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2566 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2567
2568 // Change the ssrc of the audio and video track.
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002569 cricket::MediaContentDescription* desc =
2570 cricket::GetFirstAudioContentDescription(modified_offer->description());
2571 ASSERT_TRUE(desc != NULL);
2572 for (StreamParams& stream : desc->mutable_streams()) {
2573 for (unsigned int& ssrc : stream.ssrcs) {
2574 ++ssrc;
2575 }
2576 }
deadbeefab9b2d12015-10-14 11:33:11 -07002577
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002578 desc =
2579 cricket::GetFirstVideoContentDescription(modified_offer->description());
2580 ASSERT_TRUE(desc != NULL);
2581 for (StreamParams& stream : desc->mutable_streams()) {
2582 for (unsigned int& ssrc : stream.ssrcs) {
2583 ++ssrc;
2584 }
2585 }
2586
2587 EXPECT_TRUE(DoSetLocalDescription(modified_offer.release()));
deadbeefab9b2d12015-10-14 11:33:11 -07002588 senders = pc_->GetSenders();
2589 EXPECT_EQ(2u, senders.size());
2590 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2591 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2592 // TODO(deadbeef): Once RtpSenders expose parameters, check that the SSRC
2593 // changed.
2594}
2595
2596// This tests that the expected behavior occurs if a new session description is
2597// set with the same tracks, but on a different MediaStream.
Stefan Holmer55d6e7c2016-03-17 16:26:40 +01002598TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002599 SignalSameTracksInSeparateMediaStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002600 FakeConstraints constraints;
2601 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2602 true);
2603 CreatePeerConnection(&constraints);
deadbeefab9b2d12015-10-14 11:33:11 -07002604
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002605 rtc::scoped_refptr<StreamCollection> stream_collection =
2606 CreateStreamCollection(2, 1);
2607 pc_->AddStream(stream_collection->at(0));
2608 std::unique_ptr<SessionDescriptionInterface> offer;
2609 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2610 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
deadbeefab9b2d12015-10-14 11:33:11 -07002611
deadbeefab9b2d12015-10-14 11:33:11 -07002612 auto senders = pc_->GetSenders();
2613 EXPECT_EQ(2u, senders.size());
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002614 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0], kStreams[0]));
2615 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0], kStreams[0]));
deadbeefab9b2d12015-10-14 11:33:11 -07002616
2617 // Add a new MediaStream but with the same tracks as in the first stream.
2618 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream_1(
2619 webrtc::MediaStream::Create(kStreams[1]));
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002620 stream_1->AddTrack(stream_collection->at(0)->GetVideoTracks()[0]);
2621 stream_1->AddTrack(stream_collection->at(0)->GetAudioTracks()[0]);
deadbeefab9b2d12015-10-14 11:33:11 -07002622 pc_->AddStream(stream_1);
2623
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002624 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2625 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
deadbeefab9b2d12015-10-14 11:33:11 -07002626
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002627 auto new_senders = pc_->GetSenders();
2628 // Should be the same senders as before, but with updated stream id.
2629 // Note that this behavior is subject to change in the future.
2630 // We may decide the PC should ignore existing tracks in AddStream.
2631 EXPECT_EQ(senders, new_senders);
2632 EXPECT_TRUE(ContainsSender(new_senders, kAudioTracks[0], kStreams[1]));
2633 EXPECT_TRUE(ContainsSender(new_senders, kVideoTracks[0], kStreams[1]));
deadbeefab9b2d12015-10-14 11:33:11 -07002634}
2635
nisse51542be2016-02-12 02:27:06 -08002636class PeerConnectionMediaConfigTest : public testing::Test {
2637 protected:
2638 void SetUp() override {
nisseaf510af2016-03-21 08:20:42 -07002639 pcf_ = new rtc::RefCountedObject<PeerConnectionFactoryForTest>();
nisse51542be2016-02-12 02:27:06 -08002640 pcf_->Initialize();
2641 }
2642 const cricket::MediaConfig& TestCreatePeerConnection(
2643 const PeerConnectionInterface::RTCConfiguration& config,
2644 const MediaConstraintsInterface *constraints) {
2645 pcf_->create_media_controller_called_ = false;
2646
zhihuang9763d562016-08-05 11:14:50 -07002647 rtc::scoped_refptr<PeerConnectionInterface> pc(pcf_->CreatePeerConnection(
2648 config, constraints, nullptr, nullptr, &observer_));
nisse51542be2016-02-12 02:27:06 -08002649 EXPECT_TRUE(pc.get());
2650 EXPECT_TRUE(pcf_->create_media_controller_called_);
2651 return pcf_->create_media_controller_config_;
2652 }
2653
zhihuang9763d562016-08-05 11:14:50 -07002654 rtc::scoped_refptr<PeerConnectionFactoryForTest> pcf_;
nisse51542be2016-02-12 02:27:06 -08002655 MockPeerConnectionObserver observer_;
2656};
2657
2658// This test verifies the default behaviour with no constraints and a
2659// default RTCConfiguration.
2660TEST_F(PeerConnectionMediaConfigTest, TestDefaults) {
2661 PeerConnectionInterface::RTCConfiguration config;
2662 FakeConstraints constraints;
2663
2664 const cricket::MediaConfig& media_config =
2665 TestCreatePeerConnection(config, &constraints);
2666
2667 EXPECT_FALSE(media_config.enable_dscp);
nisse0db023a2016-03-01 04:29:59 -08002668 EXPECT_TRUE(media_config.video.enable_cpu_overuse_detection);
2669 EXPECT_FALSE(media_config.video.disable_prerenderer_smoothing);
2670 EXPECT_FALSE(media_config.video.suspend_below_min_bitrate);
nisse51542be2016-02-12 02:27:06 -08002671}
2672
2673// This test verifies the DSCP constraint is recognized and passed to
2674// the CreateMediaController call.
2675TEST_F(PeerConnectionMediaConfigTest, TestDscpConstraintTrue) {
2676 PeerConnectionInterface::RTCConfiguration config;
2677 FakeConstraints constraints;
2678
2679 constraints.AddOptional(webrtc::MediaConstraintsInterface::kEnableDscp, true);
2680 const cricket::MediaConfig& media_config =
2681 TestCreatePeerConnection(config, &constraints);
2682
2683 EXPECT_TRUE(media_config.enable_dscp);
2684}
2685
2686// This test verifies the cpu overuse detection constraint is
2687// recognized and passed to the CreateMediaController call.
2688TEST_F(PeerConnectionMediaConfigTest, TestCpuOveruseConstraintFalse) {
2689 PeerConnectionInterface::RTCConfiguration config;
2690 FakeConstraints constraints;
2691
2692 constraints.AddOptional(
2693 webrtc::MediaConstraintsInterface::kCpuOveruseDetection, false);
2694 const cricket::MediaConfig media_config =
2695 TestCreatePeerConnection(config, &constraints);
2696
nisse0db023a2016-03-01 04:29:59 -08002697 EXPECT_FALSE(media_config.video.enable_cpu_overuse_detection);
nisse51542be2016-02-12 02:27:06 -08002698}
2699
2700// This test verifies that the disable_prerenderer_smoothing flag is
2701// propagated from RTCConfiguration to the CreateMediaController call.
2702TEST_F(PeerConnectionMediaConfigTest, TestDisablePrerendererSmoothingTrue) {
2703 PeerConnectionInterface::RTCConfiguration config;
2704 FakeConstraints constraints;
2705
Niels Möller71bdda02016-03-31 12:59:59 +02002706 config.set_prerenderer_smoothing(false);
nisse51542be2016-02-12 02:27:06 -08002707 const cricket::MediaConfig& media_config =
2708 TestCreatePeerConnection(config, &constraints);
2709
nisse0db023a2016-03-01 04:29:59 -08002710 EXPECT_TRUE(media_config.video.disable_prerenderer_smoothing);
2711}
2712
2713// This test verifies the suspend below min bitrate constraint is
2714// recognized and passed to the CreateMediaController call.
2715TEST_F(PeerConnectionMediaConfigTest,
2716 TestSuspendBelowMinBitrateConstraintTrue) {
2717 PeerConnectionInterface::RTCConfiguration config;
2718 FakeConstraints constraints;
2719
2720 constraints.AddOptional(
2721 webrtc::MediaConstraintsInterface::kEnableVideoSuspendBelowMinBitrate,
2722 true);
2723 const cricket::MediaConfig media_config =
2724 TestCreatePeerConnection(config, &constraints);
2725
2726 EXPECT_TRUE(media_config.video.suspend_below_min_bitrate);
nisse51542be2016-02-12 02:27:06 -08002727}
2728
deadbeefab9b2d12015-10-14 11:33:11 -07002729// The following tests verify that session options are created correctly.
deadbeefc80741f2015-10-22 13:14:45 -07002730// TODO(deadbeef): Convert these tests to be more end-to-end. Instead of
2731// "verify options are converted correctly", should be "pass options into
2732// CreateOffer and verify the correct offer is produced."
deadbeefab9b2d12015-10-14 11:33:11 -07002733
2734TEST(CreateSessionOptionsTest, GetOptionsForOfferWithInvalidAudioOption) {
2735 RTCOfferAnswerOptions rtc_options;
2736 rtc_options.offer_to_receive_audio = RTCOfferAnswerOptions::kUndefined - 1;
2737
2738 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08002739 EXPECT_FALSE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002740
2741 rtc_options.offer_to_receive_audio =
2742 RTCOfferAnswerOptions::kMaxOfferToReceiveMedia + 1;
htaaac2dea2016-03-10 13:35:55 -08002743 EXPECT_FALSE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002744}
2745
2746TEST(CreateSessionOptionsTest, GetOptionsForOfferWithInvalidVideoOption) {
2747 RTCOfferAnswerOptions rtc_options;
2748 rtc_options.offer_to_receive_video = RTCOfferAnswerOptions::kUndefined - 1;
2749
2750 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08002751 EXPECT_FALSE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002752
2753 rtc_options.offer_to_receive_video =
2754 RTCOfferAnswerOptions::kMaxOfferToReceiveMedia + 1;
htaaac2dea2016-03-10 13:35:55 -08002755 EXPECT_FALSE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002756}
2757
2758// Test that a MediaSessionOptions is created for an offer if
deadbeefc80741f2015-10-22 13:14:45 -07002759// OfferToReceiveAudio and OfferToReceiveVideo options are set.
deadbeefab9b2d12015-10-14 11:33:11 -07002760TEST(CreateSessionOptionsTest, GetMediaSessionOptionsForOfferWithAudioVideo) {
2761 RTCOfferAnswerOptions rtc_options;
2762 rtc_options.offer_to_receive_audio = 1;
2763 rtc_options.offer_to_receive_video = 1;
2764
2765 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08002766 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002767 EXPECT_TRUE(options.has_audio());
2768 EXPECT_TRUE(options.has_video());
2769 EXPECT_TRUE(options.bundle_enabled);
2770}
2771
2772// Test that a correct MediaSessionOptions is created for an offer if
deadbeefc80741f2015-10-22 13:14:45 -07002773// OfferToReceiveAudio is set.
deadbeefab9b2d12015-10-14 11:33:11 -07002774TEST(CreateSessionOptionsTest, GetMediaSessionOptionsForOfferWithAudio) {
2775 RTCOfferAnswerOptions rtc_options;
2776 rtc_options.offer_to_receive_audio = 1;
2777
2778 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08002779 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002780 EXPECT_TRUE(options.has_audio());
2781 EXPECT_FALSE(options.has_video());
2782 EXPECT_TRUE(options.bundle_enabled);
2783}
2784
2785// Test that a correct MediaSessionOptions is created for an offer if
deadbeefc80741f2015-10-22 13:14:45 -07002786// the default OfferOptions are used.
deadbeefab9b2d12015-10-14 11:33:11 -07002787TEST(CreateSessionOptionsTest, GetDefaultMediaSessionOptionsForOffer) {
2788 RTCOfferAnswerOptions rtc_options;
2789
2790 cricket::MediaSessionOptions options;
deadbeef0ed85b22016-02-23 17:24:52 -08002791 options.transport_options["audio"] = cricket::TransportOptions();
2792 options.transport_options["video"] = cricket::TransportOptions();
htaaac2dea2016-03-10 13:35:55 -08002793 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefc80741f2015-10-22 13:14:45 -07002794 EXPECT_TRUE(options.has_audio());
deadbeefab9b2d12015-10-14 11:33:11 -07002795 EXPECT_FALSE(options.has_video());
deadbeefc80741f2015-10-22 13:14:45 -07002796 EXPECT_TRUE(options.bundle_enabled);
deadbeefab9b2d12015-10-14 11:33:11 -07002797 EXPECT_TRUE(options.vad_enabled);
deadbeef0ed85b22016-02-23 17:24:52 -08002798 EXPECT_FALSE(options.transport_options["audio"].ice_restart);
2799 EXPECT_FALSE(options.transport_options["video"].ice_restart);
deadbeefab9b2d12015-10-14 11:33:11 -07002800}
2801
2802// Test that a correct MediaSessionOptions is created for an offer if
deadbeefc80741f2015-10-22 13:14:45 -07002803// OfferToReceiveVideo is set.
deadbeefab9b2d12015-10-14 11:33:11 -07002804TEST(CreateSessionOptionsTest, GetMediaSessionOptionsForOfferWithVideo) {
2805 RTCOfferAnswerOptions rtc_options;
2806 rtc_options.offer_to_receive_audio = 0;
2807 rtc_options.offer_to_receive_video = 1;
2808
2809 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08002810 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002811 EXPECT_FALSE(options.has_audio());
2812 EXPECT_TRUE(options.has_video());
2813 EXPECT_TRUE(options.bundle_enabled);
2814}
2815
2816// Test that a correct MediaSessionOptions is created for an offer if
2817// UseRtpMux is set to false.
2818TEST(CreateSessionOptionsTest,
2819 GetMediaSessionOptionsForOfferWithBundleDisabled) {
2820 RTCOfferAnswerOptions rtc_options;
2821 rtc_options.offer_to_receive_audio = 1;
2822 rtc_options.offer_to_receive_video = 1;
2823 rtc_options.use_rtp_mux = false;
2824
2825 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08002826 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002827 EXPECT_TRUE(options.has_audio());
2828 EXPECT_TRUE(options.has_video());
2829 EXPECT_FALSE(options.bundle_enabled);
2830}
2831
2832// Test that a correct MediaSessionOptions is created to restart ice if
2833// IceRestart is set. It also tests that subsequent MediaSessionOptions don't
Taylor Brandstetterf475d362016-01-08 15:35:57 -08002834// have |audio_transport_options.ice_restart| etc. set.
deadbeefab9b2d12015-10-14 11:33:11 -07002835TEST(CreateSessionOptionsTest, GetMediaSessionOptionsForOfferWithIceRestart) {
2836 RTCOfferAnswerOptions rtc_options;
2837 rtc_options.ice_restart = true;
2838
2839 cricket::MediaSessionOptions options;
deadbeef0ed85b22016-02-23 17:24:52 -08002840 options.transport_options["audio"] = cricket::TransportOptions();
2841 options.transport_options["video"] = cricket::TransportOptions();
htaaac2dea2016-03-10 13:35:55 -08002842 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeef0ed85b22016-02-23 17:24:52 -08002843 EXPECT_TRUE(options.transport_options["audio"].ice_restart);
2844 EXPECT_TRUE(options.transport_options["video"].ice_restart);
deadbeefab9b2d12015-10-14 11:33:11 -07002845
2846 rtc_options = RTCOfferAnswerOptions();
htaaac2dea2016-03-10 13:35:55 -08002847 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeef0ed85b22016-02-23 17:24:52 -08002848 EXPECT_FALSE(options.transport_options["audio"].ice_restart);
2849 EXPECT_FALSE(options.transport_options["video"].ice_restart);
deadbeefab9b2d12015-10-14 11:33:11 -07002850}
2851
2852// Test that the MediaConstraints in an answer don't affect if audio and video
2853// is offered in an offer but that if kOfferToReceiveAudio or
2854// kOfferToReceiveVideo constraints are true in an offer, the media type will be
2855// included in subsequent answers.
2856TEST(CreateSessionOptionsTest, MediaConstraintsInAnswer) {
2857 FakeConstraints answer_c;
2858 answer_c.SetMandatoryReceiveAudio(true);
2859 answer_c.SetMandatoryReceiveVideo(true);
2860
2861 cricket::MediaSessionOptions answer_options;
2862 EXPECT_TRUE(ParseConstraintsForAnswer(&answer_c, &answer_options));
2863 EXPECT_TRUE(answer_options.has_audio());
2864 EXPECT_TRUE(answer_options.has_video());
2865
deadbeefc80741f2015-10-22 13:14:45 -07002866 RTCOfferAnswerOptions rtc_offer_options;
deadbeefab9b2d12015-10-14 11:33:11 -07002867
2868 cricket::MediaSessionOptions offer_options;
htaaac2dea2016-03-10 13:35:55 -08002869 EXPECT_TRUE(
2870 ExtractMediaSessionOptions(rtc_offer_options, false, &offer_options));
deadbeefc80741f2015-10-22 13:14:45 -07002871 EXPECT_TRUE(offer_options.has_audio());
htaaac2dea2016-03-10 13:35:55 -08002872 EXPECT_TRUE(offer_options.has_video());
deadbeefab9b2d12015-10-14 11:33:11 -07002873
deadbeefc80741f2015-10-22 13:14:45 -07002874 RTCOfferAnswerOptions updated_rtc_offer_options;
2875 updated_rtc_offer_options.offer_to_receive_audio = 1;
2876 updated_rtc_offer_options.offer_to_receive_video = 1;
deadbeefab9b2d12015-10-14 11:33:11 -07002877
2878 cricket::MediaSessionOptions updated_offer_options;
htaaac2dea2016-03-10 13:35:55 -08002879 EXPECT_TRUE(ExtractMediaSessionOptions(updated_rtc_offer_options, false,
htaa2a49d92016-03-04 02:51:39 -08002880 &updated_offer_options));
deadbeefab9b2d12015-10-14 11:33:11 -07002881 EXPECT_TRUE(updated_offer_options.has_audio());
2882 EXPECT_TRUE(updated_offer_options.has_video());
2883
2884 // Since an offer has been created with both audio and video, subsequent
2885 // offers and answers should contain both audio and video.
2886 // Answers will only contain the media types that exist in the offer
2887 // regardless of the value of |updated_answer_options.has_audio| and
2888 // |updated_answer_options.has_video|.
2889 FakeConstraints updated_answer_c;
2890 answer_c.SetMandatoryReceiveAudio(false);
2891 answer_c.SetMandatoryReceiveVideo(false);
2892
2893 cricket::MediaSessionOptions updated_answer_options;
2894 EXPECT_TRUE(
2895 ParseConstraintsForAnswer(&updated_answer_c, &updated_answer_options));
2896 EXPECT_TRUE(updated_answer_options.has_audio());
2897 EXPECT_TRUE(updated_answer_options.has_video());
deadbeefab9b2d12015-10-14 11:33:11 -07002898}