Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(315)

Side by Side Diff: webrtc/api/peerconnectioninterface_unittest.cc

Issue 2514883002: Create //webrtc/api:libjingle_peerconnection_api + refactorings. (Closed)
Patch Set: Rebase Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 /*
2 * Copyright 2012 The WebRTC project authors. All Rights Reserved.
3 *
4 * 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.
9 */
10
11 #include <memory>
12 #include <sstream>
13 #include <string>
14 #include <utility>
15
16 #include "webrtc/api/audiotrack.h"
17 #include "webrtc/api/jsepsessiondescription.h"
18 #include "webrtc/api/mediastream.h"
19 #include "webrtc/api/mediastreaminterface.h"
20 #include "webrtc/api/peerconnection.h"
21 #include "webrtc/api/peerconnectioninterface.h"
22 #include "webrtc/api/rtpreceiverinterface.h"
23 #include "webrtc/api/rtpsenderinterface.h"
24 #include "webrtc/api/streamcollection.h"
25 #include "webrtc/api/test/fakeconstraints.h"
26 #include "webrtc/api/test/fakertccertificategenerator.h"
27 #include "webrtc/api/test/fakevideotracksource.h"
28 #include "webrtc/api/test/mockpeerconnectionobservers.h"
29 #include "webrtc/api/test/testsdpstrings.h"
30 #include "webrtc/api/videocapturertracksource.h"
31 #include "webrtc/api/videotrack.h"
32 #include "webrtc/base/gunit.h"
33 #include "webrtc/base/ssladapter.h"
34 #include "webrtc/base/sslstreamadapter.h"
35 #include "webrtc/base/stringutils.h"
36 #include "webrtc/base/thread.h"
37 #include "webrtc/media/base/fakevideocapturer.h"
38 #include "webrtc/media/sctp/sctpdataengine.h"
39 #include "webrtc/p2p/base/fakeportallocator.h"
40 #include "webrtc/p2p/base/faketransportcontroller.h"
41 #include "webrtc/pc/mediasession.h"
42 #include "webrtc/test/gmock.h"
43
44 #ifdef WEBRTC_ANDROID
45 #include "webrtc/api/test/androidtestinitializer.h"
46 #endif
47
48 static const char kStreamLabel1[] = "local_stream_1";
49 static const char kStreamLabel2[] = "local_stream_2";
50 static const char kStreamLabel3[] = "local_stream_3";
51 static const int kDefaultStunPort = 3478;
52 static const char kStunAddressOnly[] = "stun:address";
53 static const char kStunInvalidPort[] = "stun:address:-1";
54 static const char kStunAddressPortAndMore1[] = "stun:address:port:more";
55 static const char kStunAddressPortAndMore2[] = "stun:address:port more";
56 static const char kTurnIceServerUri[] = "turn:user@turn.example.org";
57 static const char kTurnUsername[] = "user";
58 static const char kTurnPassword[] = "password";
59 static const char kTurnHostname[] = "turn.example.org";
60 static const uint32_t kTimeout = 10000U;
61
62 static const char kStreams[][8] = {"stream1", "stream2"};
63 static const char kAudioTracks[][32] = {"audiotrack0", "audiotrack1"};
64 static const char kVideoTracks[][32] = {"videotrack0", "videotrack1"};
65
66 static const char kRecvonly[] = "recvonly";
67 static const char kSendrecv[] = "sendrecv";
68
69 // Reference SDP with a MediaStream with label "stream1" and audio track with
70 // id "audio_1" and a video track with id "video_1;
71 static const char kSdpStringWithStream1[] =
72 "v=0\r\n"
73 "o=- 0 0 IN IP4 127.0.0.1\r\n"
74 "s=-\r\n"
75 "t=0 0\r\n"
76 "m=audio 1 RTP/AVPF 103\r\n"
77 "a=ice-ufrag:e5785931\r\n"
78 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
79 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
80 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
81 "a=mid:audio\r\n"
82 "a=sendrecv\r\n"
83 "a=rtcp-mux\r\n"
84 "a=rtpmap:103 ISAC/16000\r\n"
85 "a=ssrc:1 cname:stream1\r\n"
86 "a=ssrc:1 mslabel:stream1\r\n"
87 "a=ssrc:1 label:audiotrack0\r\n"
88 "m=video 1 RTP/AVPF 120\r\n"
89 "a=ice-ufrag:e5785931\r\n"
90 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
91 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
92 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
93 "a=mid:video\r\n"
94 "a=sendrecv\r\n"
95 "a=rtcp-mux\r\n"
96 "a=rtpmap:120 VP8/90000\r\n"
97 "a=ssrc:2 cname:stream1\r\n"
98 "a=ssrc:2 mslabel:stream1\r\n"
99 "a=ssrc:2 label:videotrack0\r\n";
100
101 // Reference SDP with a MediaStream with label "stream1" and audio track with
102 // id "audio_1";
103 static const char kSdpStringWithStream1AudioTrackOnly[] =
104 "v=0\r\n"
105 "o=- 0 0 IN IP4 127.0.0.1\r\n"
106 "s=-\r\n"
107 "t=0 0\r\n"
108 "m=audio 1 RTP/AVPF 103\r\n"
109 "a=ice-ufrag:e5785931\r\n"
110 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
111 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
112 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
113 "a=mid:audio\r\n"
114 "a=sendrecv\r\n"
115 "a=rtpmap:103 ISAC/16000\r\n"
116 "a=ssrc:1 cname:stream1\r\n"
117 "a=ssrc:1 mslabel:stream1\r\n"
118 "a=ssrc:1 label:audiotrack0\r\n"
119 "a=rtcp-mux\r\n";
120
121 // Reference SDP with two MediaStreams with label "stream1" and "stream2. Each
122 // MediaStreams have one audio track and one video track.
123 // This uses MSID.
124 static const char kSdpStringWithStream1And2[] =
125 "v=0\r\n"
126 "o=- 0 0 IN IP4 127.0.0.1\r\n"
127 "s=-\r\n"
128 "t=0 0\r\n"
129 "a=msid-semantic: WMS stream1 stream2\r\n"
130 "m=audio 1 RTP/AVPF 103\r\n"
131 "a=ice-ufrag:e5785931\r\n"
132 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
133 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
134 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
135 "a=mid:audio\r\n"
136 "a=sendrecv\r\n"
137 "a=rtcp-mux\r\n"
138 "a=rtpmap:103 ISAC/16000\r\n"
139 "a=ssrc:1 cname:stream1\r\n"
140 "a=ssrc:1 msid:stream1 audiotrack0\r\n"
141 "a=ssrc:3 cname:stream2\r\n"
142 "a=ssrc:3 msid:stream2 audiotrack1\r\n"
143 "m=video 1 RTP/AVPF 120\r\n"
144 "a=ice-ufrag:e5785931\r\n"
145 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
146 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
147 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
148 "a=mid:video\r\n"
149 "a=sendrecv\r\n"
150 "a=rtcp-mux\r\n"
151 "a=rtpmap:120 VP8/0\r\n"
152 "a=ssrc:2 cname:stream1\r\n"
153 "a=ssrc:2 msid:stream1 videotrack0\r\n"
154 "a=ssrc:4 cname:stream2\r\n"
155 "a=ssrc:4 msid:stream2 videotrack1\r\n";
156
157 // Reference SDP without MediaStreams. Msid is not supported.
158 static const char kSdpStringWithoutStreams[] =
159 "v=0\r\n"
160 "o=- 0 0 IN IP4 127.0.0.1\r\n"
161 "s=-\r\n"
162 "t=0 0\r\n"
163 "m=audio 1 RTP/AVPF 103\r\n"
164 "a=ice-ufrag:e5785931\r\n"
165 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
166 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
167 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
168 "a=mid:audio\r\n"
169 "a=sendrecv\r\n"
170 "a=rtcp-mux\r\n"
171 "a=rtpmap:103 ISAC/16000\r\n"
172 "m=video 1 RTP/AVPF 120\r\n"
173 "a=ice-ufrag:e5785931\r\n"
174 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
175 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
176 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
177 "a=mid:video\r\n"
178 "a=sendrecv\r\n"
179 "a=rtcp-mux\r\n"
180 "a=rtpmap:120 VP8/90000\r\n";
181
182 // Reference SDP without MediaStreams. Msid is supported.
183 static const char kSdpStringWithMsidWithoutStreams[] =
184 "v=0\r\n"
185 "o=- 0 0 IN IP4 127.0.0.1\r\n"
186 "s=-\r\n"
187 "t=0 0\r\n"
188 "a=msid-semantic: WMS\r\n"
189 "m=audio 1 RTP/AVPF 103\r\n"
190 "a=ice-ufrag:e5785931\r\n"
191 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
192 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
193 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
194 "a=mid:audio\r\n"
195 "a=sendrecv\r\n"
196 "a=rtcp-mux\r\n"
197 "a=rtpmap:103 ISAC/16000\r\n"
198 "m=video 1 RTP/AVPF 120\r\n"
199 "a=ice-ufrag:e5785931\r\n"
200 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
201 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
202 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
203 "a=mid:video\r\n"
204 "a=sendrecv\r\n"
205 "a=rtcp-mux\r\n"
206 "a=rtpmap:120 VP8/90000\r\n";
207
208 // Reference SDP without MediaStreams and audio only.
209 static const char kSdpStringWithoutStreamsAudioOnly[] =
210 "v=0\r\n"
211 "o=- 0 0 IN IP4 127.0.0.1\r\n"
212 "s=-\r\n"
213 "t=0 0\r\n"
214 "m=audio 1 RTP/AVPF 103\r\n"
215 "a=ice-ufrag:e5785931\r\n"
216 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
217 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
218 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
219 "a=mid:audio\r\n"
220 "a=sendrecv\r\n"
221 "a=rtcp-mux\r\n"
222 "a=rtpmap:103 ISAC/16000\r\n";
223
224 // Reference SENDONLY SDP without MediaStreams. Msid is not supported.
225 static const char kSdpStringSendOnlyWithoutStreams[] =
226 "v=0\r\n"
227 "o=- 0 0 IN IP4 127.0.0.1\r\n"
228 "s=-\r\n"
229 "t=0 0\r\n"
230 "m=audio 1 RTP/AVPF 103\r\n"
231 "a=ice-ufrag:e5785931\r\n"
232 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
233 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
234 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
235 "a=mid:audio\r\n"
236 "a=sendrecv\r\n"
237 "a=sendonly\r\n"
238 "a=rtcp-mux\r\n"
239 "a=rtpmap:103 ISAC/16000\r\n"
240 "m=video 1 RTP/AVPF 120\r\n"
241 "a=ice-ufrag:e5785931\r\n"
242 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
243 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
244 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
245 "a=mid:video\r\n"
246 "a=sendrecv\r\n"
247 "a=sendonly\r\n"
248 "a=rtcp-mux\r\n"
249 "a=rtpmap:120 VP8/90000\r\n";
250
251 static const char kSdpStringInit[] =
252 "v=0\r\n"
253 "o=- 0 0 IN IP4 127.0.0.1\r\n"
254 "s=-\r\n"
255 "t=0 0\r\n"
256 "a=msid-semantic: WMS\r\n";
257
258 static const char kSdpStringAudio[] =
259 "m=audio 1 RTP/AVPF 103\r\n"
260 "a=ice-ufrag:e5785931\r\n"
261 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
262 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
263 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
264 "a=mid:audio\r\n"
265 "a=sendrecv\r\n"
266 "a=rtcp-mux\r\n"
267 "a=rtpmap:103 ISAC/16000\r\n";
268
269 static const char kSdpStringVideo[] =
270 "m=video 1 RTP/AVPF 120\r\n"
271 "a=ice-ufrag:e5785931\r\n"
272 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
273 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
274 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
275 "a=mid:video\r\n"
276 "a=sendrecv\r\n"
277 "a=rtcp-mux\r\n"
278 "a=rtpmap:120 VP8/90000\r\n";
279
280 static const char kSdpStringMs1Audio0[] =
281 "a=ssrc:1 cname:stream1\r\n"
282 "a=ssrc:1 msid:stream1 audiotrack0\r\n";
283
284 static const char kSdpStringMs1Video0[] =
285 "a=ssrc:2 cname:stream1\r\n"
286 "a=ssrc:2 msid:stream1 videotrack0\r\n";
287
288 static const char kSdpStringMs1Audio1[] =
289 "a=ssrc:3 cname:stream1\r\n"
290 "a=ssrc:3 msid:stream1 audiotrack1\r\n";
291
292 static const char kSdpStringMs1Video1[] =
293 "a=ssrc:4 cname:stream1\r\n"
294 "a=ssrc:4 msid:stream1 videotrack1\r\n";
295
296 #define MAYBE_SKIP_TEST(feature) \
297 if (!(feature())) { \
298 LOG(LS_INFO) << "Feature disabled... skipping"; \
299 return; \
300 }
301
302 using ::testing::Exactly;
303 using cricket::StreamParams;
304 using webrtc::AudioSourceInterface;
305 using webrtc::AudioTrack;
306 using webrtc::AudioTrackInterface;
307 using webrtc::DataBuffer;
308 using webrtc::DataChannelInterface;
309 using webrtc::FakeConstraints;
310 using webrtc::IceCandidateInterface;
311 using webrtc::JsepSessionDescription;
312 using webrtc::MediaConstraintsInterface;
313 using webrtc::MediaStream;
314 using webrtc::MediaStreamInterface;
315 using webrtc::MediaStreamTrackInterface;
316 using webrtc::MockCreateSessionDescriptionObserver;
317 using webrtc::MockDataChannelObserver;
318 using webrtc::MockSetSessionDescriptionObserver;
319 using webrtc::MockStatsObserver;
320 using webrtc::NotifierInterface;
321 using webrtc::ObserverInterface;
322 using webrtc::PeerConnectionInterface;
323 using webrtc::PeerConnectionObserver;
324 using webrtc::RtpReceiverInterface;
325 using webrtc::RtpSenderInterface;
326 using webrtc::SdpParseError;
327 using webrtc::SessionDescriptionInterface;
328 using webrtc::StreamCollection;
329 using webrtc::StreamCollectionInterface;
330 using webrtc::VideoTrackSourceInterface;
331 using webrtc::VideoTrack;
332 using webrtc::VideoTrackInterface;
333
334 typedef PeerConnectionInterface::RTCOfferAnswerOptions RTCOfferAnswerOptions;
335
336 namespace {
337
338 // Gets the first ssrc of given content type from the ContentInfo.
339 bool GetFirstSsrc(const cricket::ContentInfo* content_info, int* ssrc) {
340 if (!content_info || !ssrc) {
341 return false;
342 }
343 const cricket::MediaContentDescription* media_desc =
344 static_cast<const cricket::MediaContentDescription*>(
345 content_info->description);
346 if (!media_desc || media_desc->streams().empty()) {
347 return false;
348 }
349 *ssrc = media_desc->streams().begin()->first_ssrc();
350 return true;
351 }
352
353 // Get the ufrags out of an SDP blob. Useful for testing ICE restart
354 // behavior.
355 std::vector<std::string> GetUfrags(
356 const webrtc::SessionDescriptionInterface* desc) {
357 std::vector<std::string> ufrags;
358 for (const cricket::TransportInfo& info :
359 desc->description()->transport_infos()) {
360 ufrags.push_back(info.description.ice_ufrag);
361 }
362 return ufrags;
363 }
364
365 void SetSsrcToZero(std::string* sdp) {
366 const char kSdpSsrcAtribute[] = "a=ssrc:";
367 const char kSdpSsrcAtributeZero[] = "a=ssrc:0";
368 size_t ssrc_pos = 0;
369 while ((ssrc_pos = sdp->find(kSdpSsrcAtribute, ssrc_pos)) !=
370 std::string::npos) {
371 size_t end_ssrc = sdp->find(" ", ssrc_pos);
372 sdp->replace(ssrc_pos, end_ssrc - ssrc_pos, kSdpSsrcAtributeZero);
373 ssrc_pos = end_ssrc;
374 }
375 }
376
377 // Check if |streams| contains the specified track.
378 bool ContainsTrack(const std::vector<cricket::StreamParams>& streams,
379 const std::string& stream_label,
380 const std::string& track_id) {
381 for (const cricket::StreamParams& params : streams) {
382 if (params.sync_label == stream_label && params.id == track_id) {
383 return true;
384 }
385 }
386 return false;
387 }
388
389 // Check if |senders| contains the specified sender, by id.
390 bool ContainsSender(
391 const std::vector<rtc::scoped_refptr<RtpSenderInterface>>& senders,
392 const std::string& id) {
393 for (const auto& sender : senders) {
394 if (sender->id() == id) {
395 return true;
396 }
397 }
398 return false;
399 }
400
401 // Check if |senders| contains the specified sender, by id and stream id.
402 bool ContainsSender(
403 const std::vector<rtc::scoped_refptr<RtpSenderInterface>>& senders,
404 const std::string& id,
405 const std::string& stream_id) {
406 for (const auto& sender : senders) {
407 if (sender->id() == id && sender->stream_ids()[0] == stream_id) {
408 return true;
409 }
410 }
411 return false;
412 }
413
414 // Create a collection of streams.
415 // CreateStreamCollection(1) creates a collection that
416 // correspond to kSdpStringWithStream1.
417 // CreateStreamCollection(2) correspond to kSdpStringWithStream1And2.
418 rtc::scoped_refptr<StreamCollection> CreateStreamCollection(
419 int number_of_streams,
420 int tracks_per_stream) {
421 rtc::scoped_refptr<StreamCollection> local_collection(
422 StreamCollection::Create());
423
424 for (int i = 0; i < number_of_streams; ++i) {
425 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream(
426 webrtc::MediaStream::Create(kStreams[i]));
427
428 for (int j = 0; j < tracks_per_stream; ++j) {
429 // Add a local audio track.
430 rtc::scoped_refptr<webrtc::AudioTrackInterface> audio_track(
431 webrtc::AudioTrack::Create(kAudioTracks[i * tracks_per_stream + j],
432 nullptr));
433 stream->AddTrack(audio_track);
434
435 // Add a local video track.
436 rtc::scoped_refptr<webrtc::VideoTrackInterface> video_track(
437 webrtc::VideoTrack::Create(kVideoTracks[i * tracks_per_stream + j],
438 webrtc::FakeVideoTrackSource::Create()));
439 stream->AddTrack(video_track);
440 }
441
442 local_collection->AddStream(stream);
443 }
444 return local_collection;
445 }
446
447 // Check equality of StreamCollections.
448 bool CompareStreamCollections(StreamCollectionInterface* s1,
449 StreamCollectionInterface* s2) {
450 if (s1 == nullptr || s2 == nullptr || s1->count() != s2->count()) {
451 return false;
452 }
453
454 for (size_t i = 0; i != s1->count(); ++i) {
455 if (s1->at(i)->label() != s2->at(i)->label()) {
456 return false;
457 }
458 webrtc::AudioTrackVector audio_tracks1 = s1->at(i)->GetAudioTracks();
459 webrtc::AudioTrackVector audio_tracks2 = s2->at(i)->GetAudioTracks();
460 webrtc::VideoTrackVector video_tracks1 = s1->at(i)->GetVideoTracks();
461 webrtc::VideoTrackVector video_tracks2 = s2->at(i)->GetVideoTracks();
462
463 if (audio_tracks1.size() != audio_tracks2.size()) {
464 return false;
465 }
466 for (size_t j = 0; j != audio_tracks1.size(); ++j) {
467 if (audio_tracks1[j]->id() != audio_tracks2[j]->id()) {
468 return false;
469 }
470 }
471 if (video_tracks1.size() != video_tracks2.size()) {
472 return false;
473 }
474 for (size_t j = 0; j != video_tracks1.size(); ++j) {
475 if (video_tracks1[j]->id() != video_tracks2[j]->id()) {
476 return false;
477 }
478 }
479 }
480 return true;
481 }
482
483 // Helper class to test Observer.
484 class MockTrackObserver : public ObserverInterface {
485 public:
486 explicit MockTrackObserver(NotifierInterface* notifier)
487 : notifier_(notifier) {
488 notifier_->RegisterObserver(this);
489 }
490
491 ~MockTrackObserver() { Unregister(); }
492
493 void Unregister() {
494 if (notifier_) {
495 notifier_->UnregisterObserver(this);
496 notifier_ = nullptr;
497 }
498 }
499
500 MOCK_METHOD0(OnChanged, void());
501
502 private:
503 NotifierInterface* notifier_;
504 };
505
506 class MockPeerConnectionObserver : public PeerConnectionObserver {
507 public:
508 // We need these using declarations because there are two versions of each of
509 // the below methods and we only override one of them.
510 // TODO(deadbeef): Remove once there's only one version of the methods.
511 using PeerConnectionObserver::OnAddStream;
512 using PeerConnectionObserver::OnRemoveStream;
513 using PeerConnectionObserver::OnDataChannel;
514
515 MockPeerConnectionObserver() : remote_streams_(StreamCollection::Create()) {}
516 virtual ~MockPeerConnectionObserver() {
517 }
518 void SetPeerConnectionInterface(PeerConnectionInterface* pc) {
519 pc_ = pc;
520 if (pc) {
521 state_ = pc_->signaling_state();
522 }
523 }
524 void OnSignalingChange(
525 PeerConnectionInterface::SignalingState new_state) override {
526 EXPECT_EQ(pc_->signaling_state(), new_state);
527 state_ = new_state;
528 }
529 // TODO(bemasc): Remove this once callers transition to OnIceGatheringChange.
530 virtual void OnStateChange(StateType state_changed) {
531 if (pc_.get() == NULL)
532 return;
533 switch (state_changed) {
534 case kSignalingState:
535 // OnSignalingChange and OnStateChange(kSignalingState) should always
536 // be called approximately simultaneously. To ease testing, we require
537 // that they always be called in that order. This check verifies
538 // that OnSignalingChange has just been called.
539 EXPECT_EQ(pc_->signaling_state(), state_);
540 break;
541 case kIceState:
542 ADD_FAILURE();
543 break;
544 default:
545 ADD_FAILURE();
546 break;
547 }
548 }
549
550 MediaStreamInterface* RemoteStream(const std::string& label) {
551 return remote_streams_->find(label);
552 }
553 StreamCollectionInterface* remote_streams() const { return remote_streams_; }
554 void OnAddStream(rtc::scoped_refptr<MediaStreamInterface> stream) override {
555 last_added_stream_ = stream;
556 remote_streams_->AddStream(stream);
557 }
558 void OnRemoveStream(
559 rtc::scoped_refptr<MediaStreamInterface> stream) override {
560 last_removed_stream_ = stream;
561 remote_streams_->RemoveStream(stream);
562 }
563 void OnRenegotiationNeeded() override { renegotiation_needed_ = true; }
564 void OnDataChannel(
565 rtc::scoped_refptr<DataChannelInterface> data_channel) override {
566 last_datachannel_ = data_channel;
567 }
568
569 void OnIceConnectionChange(
570 PeerConnectionInterface::IceConnectionState new_state) override {
571 EXPECT_EQ(pc_->ice_connection_state(), new_state);
572 callback_triggered_ = true;
573 }
574 void OnIceGatheringChange(
575 PeerConnectionInterface::IceGatheringState new_state) override {
576 EXPECT_EQ(pc_->ice_gathering_state(), new_state);
577 ice_complete_ = new_state == PeerConnectionInterface::kIceGatheringComplete;
578 callback_triggered_ = true;
579 }
580 void OnIceCandidate(const webrtc::IceCandidateInterface* candidate) override {
581 EXPECT_NE(PeerConnectionInterface::kIceGatheringNew,
582 pc_->ice_gathering_state());
583
584 std::string sdp;
585 EXPECT_TRUE(candidate->ToString(&sdp));
586 EXPECT_LT(0u, sdp.size());
587 last_candidate_.reset(webrtc::CreateIceCandidate(candidate->sdp_mid(),
588 candidate->sdp_mline_index(), sdp, NULL));
589 EXPECT_TRUE(last_candidate_.get() != NULL);
590 callback_triggered_ = true;
591 }
592
593 void OnIceCandidatesRemoved(
594 const std::vector<cricket::Candidate>& candidates) override {
595 callback_triggered_ = true;
596 }
597
598 void OnIceConnectionReceivingChange(bool receiving) override {
599 callback_triggered_ = true;
600 }
601
602 void OnAddTrack(
603 rtc::scoped_refptr<webrtc::RtpReceiverInterface> receiver,
604 const std::vector<rtc::scoped_refptr<webrtc::MediaStreamInterface>>&
605 streams) override {
606 EXPECT_TRUE(receiver != nullptr);
607 num_added_tracks_++;
608 last_added_track_label_ = receiver->id();
609 }
610
611 // Returns the label of the last added stream.
612 // Empty string if no stream have been added.
613 std::string GetLastAddedStreamLabel() {
614 if (last_added_stream_.get())
615 return last_added_stream_->label();
616 return "";
617 }
618 std::string GetLastRemovedStreamLabel() {
619 if (last_removed_stream_.get())
620 return last_removed_stream_->label();
621 return "";
622 }
623
624 rtc::scoped_refptr<PeerConnectionInterface> pc_;
625 PeerConnectionInterface::SignalingState state_;
626 std::unique_ptr<IceCandidateInterface> last_candidate_;
627 rtc::scoped_refptr<DataChannelInterface> last_datachannel_;
628 rtc::scoped_refptr<StreamCollection> remote_streams_;
629 bool renegotiation_needed_ = false;
630 bool ice_complete_ = false;
631 bool callback_triggered_ = false;
632 int num_added_tracks_ = 0;
633 std::string last_added_track_label_;
634
635 private:
636 rtc::scoped_refptr<MediaStreamInterface> last_added_stream_;
637 rtc::scoped_refptr<MediaStreamInterface> last_removed_stream_;
638 };
639
640 } // namespace
641
642 // The PeerConnectionMediaConfig tests below verify that configuration
643 // and constraints are propagated into the MediaConfig passed to
644 // CreateMediaController. These settings are intended for MediaChannel
645 // constructors, but that is not exercised by these unittest.
646 class PeerConnectionFactoryForTest : public webrtc::PeerConnectionFactory {
647 public:
648 webrtc::MediaControllerInterface* CreateMediaController(
649 const cricket::MediaConfig& config,
650 webrtc::RtcEventLog* event_log) const override {
651 create_media_controller_called_ = true;
652 create_media_controller_config_ = config;
653
654 webrtc::MediaControllerInterface* mc =
655 PeerConnectionFactory::CreateMediaController(config, event_log);
656 EXPECT_TRUE(mc != nullptr);
657 return mc;
658 }
659
660 cricket::TransportController* CreateTransportController(
661 cricket::PortAllocator* port_allocator,
662 bool redetermine_role_on_ice_restart) override {
663 transport_controller = new cricket::TransportController(
664 rtc::Thread::Current(), rtc::Thread::Current(), port_allocator,
665 redetermine_role_on_ice_restart);
666 return transport_controller;
667 }
668
669 cricket::TransportController* transport_controller;
670 // Mutable, so they can be modified in the above const-declared method.
671 mutable bool create_media_controller_called_ = false;
672 mutable cricket::MediaConfig create_media_controller_config_;
673 };
674
675 class PeerConnectionInterfaceTest : public testing::Test {
676 protected:
677 PeerConnectionInterfaceTest() {
678 #ifdef WEBRTC_ANDROID
679 webrtc::InitializeAndroidObjects();
680 #endif
681 }
682
683 virtual void SetUp() {
684 pc_factory_ = webrtc::CreatePeerConnectionFactory(
685 rtc::Thread::Current(), rtc::Thread::Current(), rtc::Thread::Current(),
686 nullptr, nullptr, nullptr);
687 ASSERT_TRUE(pc_factory_);
688 pc_factory_for_test_ =
689 new rtc::RefCountedObject<PeerConnectionFactoryForTest>();
690 pc_factory_for_test_->Initialize();
691 }
692
693 void CreatePeerConnection() {
694 CreatePeerConnection(PeerConnectionInterface::RTCConfiguration(), nullptr);
695 }
696
697 void CreatePeerConnection(webrtc::MediaConstraintsInterface* constraints) {
698 CreatePeerConnection(PeerConnectionInterface::RTCConfiguration(),
699 constraints);
700 }
701
702 void CreatePeerConnectionWithIceTransportsType(
703 PeerConnectionInterface::IceTransportsType type) {
704 PeerConnectionInterface::RTCConfiguration config;
705 config.type = type;
706 return CreatePeerConnection(config, nullptr);
707 }
708
709 void CreatePeerConnectionWithIceServer(const std::string& uri,
710 const std::string& password) {
711 PeerConnectionInterface::RTCConfiguration config;
712 PeerConnectionInterface::IceServer server;
713 server.uri = uri;
714 server.password = password;
715 config.servers.push_back(server);
716 CreatePeerConnection(config, nullptr);
717 }
718
719 void CreatePeerConnection(PeerConnectionInterface::RTCConfiguration config,
720 webrtc::MediaConstraintsInterface* constraints) {
721 std::unique_ptr<cricket::FakePortAllocator> port_allocator(
722 new cricket::FakePortAllocator(rtc::Thread::Current(), nullptr));
723 port_allocator_ = port_allocator.get();
724
725 // DTLS does not work in a loopback call, so is disabled for most of the
726 // tests in this file. We only create a FakeIdentityService if the test
727 // explicitly sets the constraint.
728 FakeConstraints default_constraints;
729 if (!constraints) {
730 constraints = &default_constraints;
731
732 default_constraints.AddMandatory(
733 webrtc::MediaConstraintsInterface::kEnableDtlsSrtp, false);
734 }
735
736 std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator;
737 bool dtls;
738 if (FindConstraint(constraints,
739 webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
740 &dtls,
741 nullptr) && dtls) {
742 cert_generator.reset(new FakeRTCCertificateGenerator());
743 }
744 pc_ = pc_factory_->CreatePeerConnection(
745 config, constraints, std::move(port_allocator),
746 std::move(cert_generator), &observer_);
747 ASSERT_TRUE(pc_.get() != NULL);
748 observer_.SetPeerConnectionInterface(pc_.get());
749 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
750 }
751
752 void CreatePeerConnectionExpectFail(const std::string& uri) {
753 PeerConnectionInterface::RTCConfiguration config;
754 PeerConnectionInterface::IceServer server;
755 server.uri = uri;
756 config.servers.push_back(server);
757
758 rtc::scoped_refptr<PeerConnectionInterface> pc;
759 pc = pc_factory_->CreatePeerConnection(config, nullptr, nullptr, nullptr,
760 &observer_);
761 EXPECT_EQ(nullptr, pc);
762 }
763
764 void CreatePeerConnectionWithDifferentConfigurations() {
765 CreatePeerConnectionWithIceServer(kStunAddressOnly, "");
766 EXPECT_EQ(1u, port_allocator_->stun_servers().size());
767 EXPECT_EQ(0u, port_allocator_->turn_servers().size());
768 EXPECT_EQ("address", port_allocator_->stun_servers().begin()->hostname());
769 EXPECT_EQ(kDefaultStunPort,
770 port_allocator_->stun_servers().begin()->port());
771
772 CreatePeerConnectionExpectFail(kStunInvalidPort);
773 CreatePeerConnectionExpectFail(kStunAddressPortAndMore1);
774 CreatePeerConnectionExpectFail(kStunAddressPortAndMore2);
775
776 CreatePeerConnectionWithIceServer(kTurnIceServerUri, kTurnPassword);
777 EXPECT_EQ(0u, port_allocator_->stun_servers().size());
778 EXPECT_EQ(1u, port_allocator_->turn_servers().size());
779 EXPECT_EQ(kTurnUsername,
780 port_allocator_->turn_servers()[0].credentials.username);
781 EXPECT_EQ(kTurnPassword,
782 port_allocator_->turn_servers()[0].credentials.password);
783 EXPECT_EQ(kTurnHostname,
784 port_allocator_->turn_servers()[0].ports[0].address.hostname());
785 }
786
787 void ReleasePeerConnection() {
788 pc_ = NULL;
789 observer_.SetPeerConnectionInterface(NULL);
790 }
791
792 void AddVideoStream(const std::string& label) {
793 // Create a local stream.
794 rtc::scoped_refptr<MediaStreamInterface> stream(
795 pc_factory_->CreateLocalMediaStream(label));
796 rtc::scoped_refptr<VideoTrackSourceInterface> video_source(
797 pc_factory_->CreateVideoSource(new cricket::FakeVideoCapturer(), NULL));
798 rtc::scoped_refptr<VideoTrackInterface> video_track(
799 pc_factory_->CreateVideoTrack(label + "v0", video_source));
800 stream->AddTrack(video_track.get());
801 EXPECT_TRUE(pc_->AddStream(stream));
802 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
803 observer_.renegotiation_needed_ = false;
804 }
805
806 void AddVoiceStream(const std::string& label) {
807 // Create a local stream.
808 rtc::scoped_refptr<MediaStreamInterface> stream(
809 pc_factory_->CreateLocalMediaStream(label));
810 rtc::scoped_refptr<AudioTrackInterface> audio_track(
811 pc_factory_->CreateAudioTrack(label + "a0", NULL));
812 stream->AddTrack(audio_track.get());
813 EXPECT_TRUE(pc_->AddStream(stream));
814 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
815 observer_.renegotiation_needed_ = false;
816 }
817
818 void AddAudioVideoStream(const std::string& stream_label,
819 const std::string& audio_track_label,
820 const std::string& video_track_label) {
821 // Create a local stream.
822 rtc::scoped_refptr<MediaStreamInterface> stream(
823 pc_factory_->CreateLocalMediaStream(stream_label));
824 rtc::scoped_refptr<AudioTrackInterface> audio_track(
825 pc_factory_->CreateAudioTrack(
826 audio_track_label, static_cast<AudioSourceInterface*>(NULL)));
827 stream->AddTrack(audio_track.get());
828 rtc::scoped_refptr<VideoTrackInterface> video_track(
829 pc_factory_->CreateVideoTrack(
830 video_track_label,
831 pc_factory_->CreateVideoSource(new cricket::FakeVideoCapturer())));
832 stream->AddTrack(video_track.get());
833 EXPECT_TRUE(pc_->AddStream(stream));
834 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
835 observer_.renegotiation_needed_ = false;
836 }
837
838 bool DoCreateOfferAnswer(std::unique_ptr<SessionDescriptionInterface>* desc,
839 bool offer,
840 MediaConstraintsInterface* constraints) {
841 rtc::scoped_refptr<MockCreateSessionDescriptionObserver>
842 observer(new rtc::RefCountedObject<
843 MockCreateSessionDescriptionObserver>());
844 if (offer) {
845 pc_->CreateOffer(observer, constraints);
846 } else {
847 pc_->CreateAnswer(observer, constraints);
848 }
849 EXPECT_EQ_WAIT(true, observer->called(), kTimeout);
850 desc->reset(observer->release_desc());
851 return observer->result();
852 }
853
854 bool DoCreateOffer(std::unique_ptr<SessionDescriptionInterface>* desc,
855 MediaConstraintsInterface* constraints) {
856 return DoCreateOfferAnswer(desc, true, constraints);
857 }
858
859 bool DoCreateAnswer(std::unique_ptr<SessionDescriptionInterface>* desc,
860 MediaConstraintsInterface* constraints) {
861 return DoCreateOfferAnswer(desc, false, constraints);
862 }
863
864 bool DoSetSessionDescription(SessionDescriptionInterface* desc, bool local) {
865 rtc::scoped_refptr<MockSetSessionDescriptionObserver>
866 observer(new rtc::RefCountedObject<
867 MockSetSessionDescriptionObserver>());
868 if (local) {
869 pc_->SetLocalDescription(observer, desc);
870 } else {
871 pc_->SetRemoteDescription(observer, desc);
872 }
873 if (pc_->signaling_state() != PeerConnectionInterface::kClosed) {
874 EXPECT_EQ_WAIT(true, observer->called(), kTimeout);
875 }
876 return observer->result();
877 }
878
879 bool DoSetLocalDescription(SessionDescriptionInterface* desc) {
880 return DoSetSessionDescription(desc, true);
881 }
882
883 bool DoSetRemoteDescription(SessionDescriptionInterface* desc) {
884 return DoSetSessionDescription(desc, false);
885 }
886
887 // Calls PeerConnection::GetStats and check the return value.
888 // It does not verify the values in the StatReports since a RTCP packet might
889 // be required.
890 bool DoGetStats(MediaStreamTrackInterface* track) {
891 rtc::scoped_refptr<MockStatsObserver> observer(
892 new rtc::RefCountedObject<MockStatsObserver>());
893 if (!pc_->GetStats(
894 observer, track, PeerConnectionInterface::kStatsOutputLevelStandard))
895 return false;
896 EXPECT_TRUE_WAIT(observer->called(), kTimeout);
897 return observer->called();
898 }
899
900 void InitiateCall() {
901 CreatePeerConnection();
902 // Create a local stream with audio&video tracks.
903 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
904 CreateOfferReceiveAnswer();
905 }
906
907 // Verify that RTP Header extensions has been negotiated for audio and video.
908 void VerifyRemoteRtpHeaderExtensions() {
909 const cricket::MediaContentDescription* desc =
910 cricket::GetFirstAudioContentDescription(
911 pc_->remote_description()->description());
912 ASSERT_TRUE(desc != NULL);
913 EXPECT_GT(desc->rtp_header_extensions().size(), 0u);
914
915 desc = cricket::GetFirstVideoContentDescription(
916 pc_->remote_description()->description());
917 ASSERT_TRUE(desc != NULL);
918 EXPECT_GT(desc->rtp_header_extensions().size(), 0u);
919 }
920
921 void CreateOfferAsRemoteDescription() {
922 std::unique_ptr<SessionDescriptionInterface> offer;
923 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
924 std::string sdp;
925 EXPECT_TRUE(offer->ToString(&sdp));
926 SessionDescriptionInterface* remote_offer =
927 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
928 sdp, NULL);
929 EXPECT_TRUE(DoSetRemoteDescription(remote_offer));
930 EXPECT_EQ(PeerConnectionInterface::kHaveRemoteOffer, observer_.state_);
931 }
932
933 void CreateAndSetRemoteOffer(const std::string& sdp) {
934 SessionDescriptionInterface* remote_offer =
935 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
936 sdp, nullptr);
937 EXPECT_TRUE(DoSetRemoteDescription(remote_offer));
938 EXPECT_EQ(PeerConnectionInterface::kHaveRemoteOffer, observer_.state_);
939 }
940
941 void CreateAnswerAsLocalDescription() {
942 std::unique_ptr<SessionDescriptionInterface> answer;
943 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
944
945 // TODO(perkj): Currently SetLocalDescription fails if any parameters in an
946 // audio codec change, even if the parameter has nothing to do with
947 // receiving. Not all parameters are serialized to SDP.
948 // Since CreatePrAnswerAsLocalDescription serialize/deserialize
949 // the SessionDescription, it is necessary to do that here to in order to
950 // get ReceiveOfferCreatePrAnswerAndAnswer and RenegotiateAudioOnly to pass.
951 // https://code.google.com/p/webrtc/issues/detail?id=1356
952 std::string sdp;
953 EXPECT_TRUE(answer->ToString(&sdp));
954 SessionDescriptionInterface* new_answer =
955 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
956 sdp, NULL);
957 EXPECT_TRUE(DoSetLocalDescription(new_answer));
958 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
959 }
960
961 void CreatePrAnswerAsLocalDescription() {
962 std::unique_ptr<SessionDescriptionInterface> answer;
963 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
964
965 std::string sdp;
966 EXPECT_TRUE(answer->ToString(&sdp));
967 SessionDescriptionInterface* pr_answer =
968 webrtc::CreateSessionDescription(SessionDescriptionInterface::kPrAnswer,
969 sdp, NULL);
970 EXPECT_TRUE(DoSetLocalDescription(pr_answer));
971 EXPECT_EQ(PeerConnectionInterface::kHaveLocalPrAnswer, observer_.state_);
972 }
973
974 void CreateOfferReceiveAnswer() {
975 CreateOfferAsLocalDescription();
976 std::string sdp;
977 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
978 CreateAnswerAsRemoteDescription(sdp);
979 }
980
981 void CreateOfferAsLocalDescription() {
982 std::unique_ptr<SessionDescriptionInterface> offer;
983 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
984 // TODO(perkj): Currently SetLocalDescription fails if any parameters in an
985 // audio codec change, even if the parameter has nothing to do with
986 // receiving. Not all parameters are serialized to SDP.
987 // Since CreatePrAnswerAsLocalDescription serialize/deserialize
988 // the SessionDescription, it is necessary to do that here to in order to
989 // get ReceiveOfferCreatePrAnswerAndAnswer and RenegotiateAudioOnly to pass.
990 // https://code.google.com/p/webrtc/issues/detail?id=1356
991 std::string sdp;
992 EXPECT_TRUE(offer->ToString(&sdp));
993 SessionDescriptionInterface* new_offer =
994 webrtc::CreateSessionDescription(
995 SessionDescriptionInterface::kOffer,
996 sdp, NULL);
997
998 EXPECT_TRUE(DoSetLocalDescription(new_offer));
999 EXPECT_EQ(PeerConnectionInterface::kHaveLocalOffer, observer_.state_);
1000 // Wait for the ice_complete message, so that SDP will have candidates.
1001 EXPECT_TRUE_WAIT(observer_.ice_complete_, kTimeout);
1002 }
1003
1004 void CreateAnswerAsRemoteDescription(const std::string& sdp) {
1005 webrtc::JsepSessionDescription* answer = new webrtc::JsepSessionDescription(
1006 SessionDescriptionInterface::kAnswer);
1007 EXPECT_TRUE(answer->Initialize(sdp, NULL));
1008 EXPECT_TRUE(DoSetRemoteDescription(answer));
1009 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
1010 }
1011
1012 void CreatePrAnswerAndAnswerAsRemoteDescription(const std::string& sdp) {
1013 webrtc::JsepSessionDescription* pr_answer =
1014 new webrtc::JsepSessionDescription(
1015 SessionDescriptionInterface::kPrAnswer);
1016 EXPECT_TRUE(pr_answer->Initialize(sdp, NULL));
1017 EXPECT_TRUE(DoSetRemoteDescription(pr_answer));
1018 EXPECT_EQ(PeerConnectionInterface::kHaveRemotePrAnswer, observer_.state_);
1019 webrtc::JsepSessionDescription* answer =
1020 new webrtc::JsepSessionDescription(
1021 SessionDescriptionInterface::kAnswer);
1022 EXPECT_TRUE(answer->Initialize(sdp, NULL));
1023 EXPECT_TRUE(DoSetRemoteDescription(answer));
1024 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
1025 }
1026
1027 // Help function used for waiting until a the last signaled remote stream has
1028 // the same label as |stream_label|. In a few of the tests in this file we
1029 // answer with the same session description as we offer and thus we can
1030 // check if OnAddStream have been called with the same stream as we offer to
1031 // send.
1032 void WaitAndVerifyOnAddStream(const std::string& stream_label) {
1033 EXPECT_EQ_WAIT(stream_label, observer_.GetLastAddedStreamLabel(), kTimeout);
1034 }
1035
1036 // Creates an offer and applies it as a local session description.
1037 // Creates an answer with the same SDP an the offer but removes all lines
1038 // that start with a:ssrc"
1039 void CreateOfferReceiveAnswerWithoutSsrc() {
1040 CreateOfferAsLocalDescription();
1041 std::string sdp;
1042 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
1043 SetSsrcToZero(&sdp);
1044 CreateAnswerAsRemoteDescription(sdp);
1045 }
1046
1047 // This function creates a MediaStream with label kStreams[0] and
1048 // |number_of_audio_tracks| and |number_of_video_tracks| tracks and the
1049 // corresponding SessionDescriptionInterface. The SessionDescriptionInterface
1050 // is returned and the MediaStream is stored in
1051 // |reference_collection_|
1052 std::unique_ptr<SessionDescriptionInterface>
1053 CreateSessionDescriptionAndReference(size_t number_of_audio_tracks,
1054 size_t number_of_video_tracks) {
1055 EXPECT_LE(number_of_audio_tracks, 2u);
1056 EXPECT_LE(number_of_video_tracks, 2u);
1057
1058 reference_collection_ = StreamCollection::Create();
1059 std::string sdp_ms1 = std::string(kSdpStringInit);
1060
1061 std::string mediastream_label = kStreams[0];
1062
1063 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream(
1064 webrtc::MediaStream::Create(mediastream_label));
1065 reference_collection_->AddStream(stream);
1066
1067 if (number_of_audio_tracks > 0) {
1068 sdp_ms1 += std::string(kSdpStringAudio);
1069 sdp_ms1 += std::string(kSdpStringMs1Audio0);
1070 AddAudioTrack(kAudioTracks[0], stream);
1071 }
1072 if (number_of_audio_tracks > 1) {
1073 sdp_ms1 += kSdpStringMs1Audio1;
1074 AddAudioTrack(kAudioTracks[1], stream);
1075 }
1076
1077 if (number_of_video_tracks > 0) {
1078 sdp_ms1 += std::string(kSdpStringVideo);
1079 sdp_ms1 += std::string(kSdpStringMs1Video0);
1080 AddVideoTrack(kVideoTracks[0], stream);
1081 }
1082 if (number_of_video_tracks > 1) {
1083 sdp_ms1 += kSdpStringMs1Video1;
1084 AddVideoTrack(kVideoTracks[1], stream);
1085 }
1086
1087 return std::unique_ptr<SessionDescriptionInterface>(
1088 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
1089 sdp_ms1, nullptr));
1090 }
1091
1092 void AddAudioTrack(const std::string& track_id,
1093 MediaStreamInterface* stream) {
1094 rtc::scoped_refptr<webrtc::AudioTrackInterface> audio_track(
1095 webrtc::AudioTrack::Create(track_id, nullptr));
1096 ASSERT_TRUE(stream->AddTrack(audio_track));
1097 }
1098
1099 void AddVideoTrack(const std::string& track_id,
1100 MediaStreamInterface* stream) {
1101 rtc::scoped_refptr<webrtc::VideoTrackInterface> video_track(
1102 webrtc::VideoTrack::Create(track_id,
1103 webrtc::FakeVideoTrackSource::Create()));
1104 ASSERT_TRUE(stream->AddTrack(video_track));
1105 }
1106
1107 std::unique_ptr<SessionDescriptionInterface> CreateOfferWithOneAudioStream() {
1108 CreatePeerConnection();
1109 AddVoiceStream(kStreamLabel1);
1110 std::unique_ptr<SessionDescriptionInterface> offer;
1111 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
1112 return offer;
1113 }
1114
1115 std::unique_ptr<SessionDescriptionInterface>
1116 CreateAnswerWithOneAudioStream() {
1117 std::unique_ptr<SessionDescriptionInterface> offer =
1118 CreateOfferWithOneAudioStream();
1119 EXPECT_TRUE(DoSetRemoteDescription(offer.release()));
1120 std::unique_ptr<SessionDescriptionInterface> answer;
1121 EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
1122 return answer;
1123 }
1124
1125 const std::string& GetFirstAudioStreamCname(
1126 const SessionDescriptionInterface* desc) {
1127 const cricket::ContentInfo* audio_content =
1128 cricket::GetFirstAudioContent(desc->description());
1129 const cricket::AudioContentDescription* audio_desc =
1130 static_cast<const cricket::AudioContentDescription*>(
1131 audio_content->description);
1132 return audio_desc->streams()[0].cname;
1133 }
1134
1135 cricket::FakePortAllocator* port_allocator_ = nullptr;
1136 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface> pc_factory_;
1137 rtc::scoped_refptr<PeerConnectionFactoryForTest> pc_factory_for_test_;
1138 rtc::scoped_refptr<PeerConnectionInterface> pc_;
1139 MockPeerConnectionObserver observer_;
1140 rtc::scoped_refptr<StreamCollection> reference_collection_;
1141 };
1142
1143 // Test that no callbacks on the PeerConnectionObserver are called after the
1144 // PeerConnection is closed.
1145 TEST_F(PeerConnectionInterfaceTest, CloseAndTestCallbackFunctions) {
1146 rtc::scoped_refptr<PeerConnectionInterface> pc(
1147 pc_factory_for_test_->CreatePeerConnection(
1148 PeerConnectionInterface::RTCConfiguration(), nullptr, nullptr,
1149 nullptr, &observer_));
1150 observer_.SetPeerConnectionInterface(pc.get());
1151 pc->Close();
1152
1153 // No callbacks is expected to be called.
1154 observer_.callback_triggered_ = false;
1155 std::vector<cricket::Candidate> candidates;
1156 pc_factory_for_test_->transport_controller->SignalGatheringState(
1157 cricket::IceGatheringState{});
1158 pc_factory_for_test_->transport_controller->SignalCandidatesGathered(
1159 "", candidates);
1160 pc_factory_for_test_->transport_controller->SignalConnectionState(
1161 cricket::IceConnectionState{});
1162 pc_factory_for_test_->transport_controller->SignalCandidatesRemoved(
1163 candidates);
1164 pc_factory_for_test_->transport_controller->SignalReceiving(false);
1165 EXPECT_FALSE(observer_.callback_triggered_);
1166 }
1167
1168 // Generate different CNAMEs when PeerConnections are created.
1169 // The CNAMEs are expected to be generated randomly. It is possible
1170 // that the test fails, though the possibility is very low.
1171 TEST_F(PeerConnectionInterfaceTest, CnameGenerationInOffer) {
1172 std::unique_ptr<SessionDescriptionInterface> offer1 =
1173 CreateOfferWithOneAudioStream();
1174 std::unique_ptr<SessionDescriptionInterface> offer2 =
1175 CreateOfferWithOneAudioStream();
1176 EXPECT_NE(GetFirstAudioStreamCname(offer1.get()),
1177 GetFirstAudioStreamCname(offer2.get()));
1178 }
1179
1180 TEST_F(PeerConnectionInterfaceTest, CnameGenerationInAnswer) {
1181 std::unique_ptr<SessionDescriptionInterface> answer1 =
1182 CreateAnswerWithOneAudioStream();
1183 std::unique_ptr<SessionDescriptionInterface> answer2 =
1184 CreateAnswerWithOneAudioStream();
1185 EXPECT_NE(GetFirstAudioStreamCname(answer1.get()),
1186 GetFirstAudioStreamCname(answer2.get()));
1187 }
1188
1189 TEST_F(PeerConnectionInterfaceTest,
1190 CreatePeerConnectionWithDifferentConfigurations) {
1191 CreatePeerConnectionWithDifferentConfigurations();
1192 }
1193
1194 TEST_F(PeerConnectionInterfaceTest,
1195 CreatePeerConnectionWithDifferentIceTransportsTypes) {
1196 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kNone);
1197 EXPECT_EQ(cricket::CF_NONE, port_allocator_->candidate_filter());
1198 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kRelay);
1199 EXPECT_EQ(cricket::CF_RELAY, port_allocator_->candidate_filter());
1200 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kNoHost);
1201 EXPECT_EQ(cricket::CF_ALL & ~cricket::CF_HOST,
1202 port_allocator_->candidate_filter());
1203 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kAll);
1204 EXPECT_EQ(cricket::CF_ALL, port_allocator_->candidate_filter());
1205 }
1206
1207 // Test that when a PeerConnection is created with a nonzero candidate pool
1208 // size, the pooled PortAllocatorSession is created with all the attributes
1209 // in the RTCConfiguration.
1210 TEST_F(PeerConnectionInterfaceTest, CreatePeerConnectionWithPooledCandidates) {
1211 PeerConnectionInterface::RTCConfiguration config;
1212 PeerConnectionInterface::IceServer server;
1213 server.uri = kStunAddressOnly;
1214 config.servers.push_back(server);
1215 config.type = PeerConnectionInterface::kRelay;
1216 config.disable_ipv6 = true;
1217 config.tcp_candidate_policy =
1218 PeerConnectionInterface::kTcpCandidatePolicyDisabled;
1219 config.candidate_network_policy =
1220 PeerConnectionInterface::kCandidateNetworkPolicyLowCost;
1221 config.ice_candidate_pool_size = 1;
1222 CreatePeerConnection(config, nullptr);
1223
1224 const cricket::FakePortAllocatorSession* session =
1225 static_cast<const cricket::FakePortAllocatorSession*>(
1226 port_allocator_->GetPooledSession());
1227 ASSERT_NE(nullptr, session);
1228 EXPECT_EQ(1UL, session->stun_servers().size());
1229 EXPECT_EQ(0U, session->flags() & cricket::PORTALLOCATOR_ENABLE_IPV6);
1230 EXPECT_LT(0U, session->flags() & cricket::PORTALLOCATOR_DISABLE_TCP);
1231 EXPECT_LT(0U,
1232 session->flags() & cricket::PORTALLOCATOR_DISABLE_COSTLY_NETWORKS);
1233 }
1234
1235 // Test that the PeerConnection initializes the port allocator passed into it,
1236 // and on the correct thread.
1237 TEST_F(PeerConnectionInterfaceTest,
1238 CreatePeerConnectionInitializesPortAllocator) {
1239 rtc::Thread network_thread;
1240 network_thread.Start();
1241 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface> pc_factory(
1242 webrtc::CreatePeerConnectionFactory(
1243 &network_thread, rtc::Thread::Current(), rtc::Thread::Current(),
1244 nullptr, nullptr, nullptr));
1245 std::unique_ptr<cricket::FakePortAllocator> port_allocator(
1246 new cricket::FakePortAllocator(&network_thread, nullptr));
1247 cricket::FakePortAllocator* raw_port_allocator = port_allocator.get();
1248 PeerConnectionInterface::RTCConfiguration config;
1249 rtc::scoped_refptr<PeerConnectionInterface> pc(
1250 pc_factory->CreatePeerConnection(
1251 config, nullptr, std::move(port_allocator), nullptr, &observer_));
1252 // FakePortAllocator RTC_CHECKs that it's initialized on the right thread,
1253 // so all we have to do here is check that it's initialized.
1254 EXPECT_TRUE(raw_port_allocator->initialized());
1255 }
1256
1257 // Check that GetConfiguration returns the configuration the PeerConnection was
1258 // constructed with, before SetConfiguration is called.
1259 TEST_F(PeerConnectionInterfaceTest, GetConfigurationAfterCreatePeerConnection) {
1260 PeerConnectionInterface::RTCConfiguration config;
1261 config.type = PeerConnectionInterface::kRelay;
1262 CreatePeerConnection(config, nullptr);
1263
1264 PeerConnectionInterface::RTCConfiguration returned_config =
1265 pc_->GetConfiguration();
1266 EXPECT_EQ(PeerConnectionInterface::kRelay, returned_config.type);
1267 }
1268
1269 // Check that GetConfiguration returns the last configuration passed into
1270 // SetConfiguration.
1271 TEST_F(PeerConnectionInterfaceTest, GetConfigurationAfterSetConfiguration) {
1272 CreatePeerConnection();
1273
1274 PeerConnectionInterface::RTCConfiguration config;
1275 config.type = PeerConnectionInterface::kRelay;
1276 EXPECT_TRUE(pc_->SetConfiguration(config));
1277
1278 PeerConnectionInterface::RTCConfiguration returned_config =
1279 pc_->GetConfiguration();
1280 EXPECT_EQ(PeerConnectionInterface::kRelay, returned_config.type);
1281 }
1282
1283 TEST_F(PeerConnectionInterfaceTest, AddStreams) {
1284 CreatePeerConnection();
1285 AddVideoStream(kStreamLabel1);
1286 AddVoiceStream(kStreamLabel2);
1287 ASSERT_EQ(2u, pc_->local_streams()->count());
1288
1289 // Test we can add multiple local streams to one peerconnection.
1290 rtc::scoped_refptr<MediaStreamInterface> stream(
1291 pc_factory_->CreateLocalMediaStream(kStreamLabel3));
1292 rtc::scoped_refptr<AudioTrackInterface> audio_track(
1293 pc_factory_->CreateAudioTrack(kStreamLabel3,
1294 static_cast<AudioSourceInterface*>(NULL)));
1295 stream->AddTrack(audio_track.get());
1296 EXPECT_TRUE(pc_->AddStream(stream));
1297 EXPECT_EQ(3u, pc_->local_streams()->count());
1298
1299 // Remove the third stream.
1300 pc_->RemoveStream(pc_->local_streams()->at(2));
1301 EXPECT_EQ(2u, pc_->local_streams()->count());
1302
1303 // Remove the second stream.
1304 pc_->RemoveStream(pc_->local_streams()->at(1));
1305 EXPECT_EQ(1u, pc_->local_streams()->count());
1306
1307 // Remove the first stream.
1308 pc_->RemoveStream(pc_->local_streams()->at(0));
1309 EXPECT_EQ(0u, pc_->local_streams()->count());
1310 }
1311
1312 // Test that the created offer includes streams we added.
1313 TEST_F(PeerConnectionInterfaceTest, AddedStreamsPresentInOffer) {
1314 CreatePeerConnection();
1315 AddAudioVideoStream(kStreamLabel1, "audio_track", "video_track");
1316 std::unique_ptr<SessionDescriptionInterface> offer;
1317 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
1318
1319 const cricket::ContentInfo* audio_content =
1320 cricket::GetFirstAudioContent(offer->description());
1321 const cricket::AudioContentDescription* audio_desc =
1322 static_cast<const cricket::AudioContentDescription*>(
1323 audio_content->description);
1324 EXPECT_TRUE(
1325 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
1326
1327 const cricket::ContentInfo* video_content =
1328 cricket::GetFirstVideoContent(offer->description());
1329 const cricket::VideoContentDescription* video_desc =
1330 static_cast<const cricket::VideoContentDescription*>(
1331 video_content->description);
1332 EXPECT_TRUE(
1333 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1334
1335 // Add another stream and ensure the offer includes both the old and new
1336 // streams.
1337 AddAudioVideoStream(kStreamLabel2, "audio_track2", "video_track2");
1338 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
1339
1340 audio_content = cricket::GetFirstAudioContent(offer->description());
1341 audio_desc = static_cast<const cricket::AudioContentDescription*>(
1342 audio_content->description);
1343 EXPECT_TRUE(
1344 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
1345 EXPECT_TRUE(
1346 ContainsTrack(audio_desc->streams(), kStreamLabel2, "audio_track2"));
1347
1348 video_content = cricket::GetFirstVideoContent(offer->description());
1349 video_desc = static_cast<const cricket::VideoContentDescription*>(
1350 video_content->description);
1351 EXPECT_TRUE(
1352 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1353 EXPECT_TRUE(
1354 ContainsTrack(video_desc->streams(), kStreamLabel2, "video_track2"));
1355 }
1356
1357 TEST_F(PeerConnectionInterfaceTest, RemoveStream) {
1358 CreatePeerConnection();
1359 AddVideoStream(kStreamLabel1);
1360 ASSERT_EQ(1u, pc_->local_streams()->count());
1361 pc_->RemoveStream(pc_->local_streams()->at(0));
1362 EXPECT_EQ(0u, pc_->local_streams()->count());
1363 }
1364
1365 // Test for AddTrack and RemoveTrack methods.
1366 // Tests that the created offer includes tracks we added,
1367 // and that the RtpSenders are created correctly.
1368 // Also tests that RemoveTrack removes the tracks from subsequent offers.
1369 TEST_F(PeerConnectionInterfaceTest, AddTrackRemoveTrack) {
1370 CreatePeerConnection();
1371 // Create a dummy stream, so tracks share a stream label.
1372 rtc::scoped_refptr<MediaStreamInterface> stream(
1373 pc_factory_->CreateLocalMediaStream(kStreamLabel1));
1374 std::vector<MediaStreamInterface*> stream_list;
1375 stream_list.push_back(stream.get());
1376 rtc::scoped_refptr<AudioTrackInterface> audio_track(
1377 pc_factory_->CreateAudioTrack("audio_track", nullptr));
1378 rtc::scoped_refptr<VideoTrackInterface> video_track(
1379 pc_factory_->CreateVideoTrack(
1380 "video_track",
1381 pc_factory_->CreateVideoSource(new cricket::FakeVideoCapturer())));
1382 auto audio_sender = pc_->AddTrack(audio_track, stream_list);
1383 auto video_sender = pc_->AddTrack(video_track, stream_list);
1384 EXPECT_EQ(1UL, audio_sender->stream_ids().size());
1385 EXPECT_EQ(kStreamLabel1, audio_sender->stream_ids()[0]);
1386 EXPECT_EQ("audio_track", audio_sender->id());
1387 EXPECT_EQ(audio_track, audio_sender->track());
1388 EXPECT_EQ(1UL, video_sender->stream_ids().size());
1389 EXPECT_EQ(kStreamLabel1, video_sender->stream_ids()[0]);
1390 EXPECT_EQ("video_track", video_sender->id());
1391 EXPECT_EQ(video_track, video_sender->track());
1392
1393 // Now create an offer and check for the senders.
1394 std::unique_ptr<SessionDescriptionInterface> offer;
1395 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
1396
1397 const cricket::ContentInfo* audio_content =
1398 cricket::GetFirstAudioContent(offer->description());
1399 const cricket::AudioContentDescription* audio_desc =
1400 static_cast<const cricket::AudioContentDescription*>(
1401 audio_content->description);
1402 EXPECT_TRUE(
1403 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
1404
1405 const cricket::ContentInfo* video_content =
1406 cricket::GetFirstVideoContent(offer->description());
1407 const cricket::VideoContentDescription* video_desc =
1408 static_cast<const cricket::VideoContentDescription*>(
1409 video_content->description);
1410 EXPECT_TRUE(
1411 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1412
1413 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
1414
1415 // Now try removing the tracks.
1416 EXPECT_TRUE(pc_->RemoveTrack(audio_sender));
1417 EXPECT_TRUE(pc_->RemoveTrack(video_sender));
1418
1419 // Create a new offer and ensure it doesn't contain the removed senders.
1420 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
1421
1422 audio_content = cricket::GetFirstAudioContent(offer->description());
1423 audio_desc = static_cast<const cricket::AudioContentDescription*>(
1424 audio_content->description);
1425 EXPECT_FALSE(
1426 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
1427
1428 video_content = cricket::GetFirstVideoContent(offer->description());
1429 video_desc = static_cast<const cricket::VideoContentDescription*>(
1430 video_content->description);
1431 EXPECT_FALSE(
1432 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1433
1434 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
1435
1436 // Calling RemoveTrack on a sender no longer attached to a PeerConnection
1437 // should return false.
1438 EXPECT_FALSE(pc_->RemoveTrack(audio_sender));
1439 EXPECT_FALSE(pc_->RemoveTrack(video_sender));
1440 }
1441
1442 // Test creating senders without a stream specified,
1443 // expecting a random stream ID to be generated.
1444 TEST_F(PeerConnectionInterfaceTest, AddTrackWithoutStream) {
1445 CreatePeerConnection();
1446 // Create a dummy stream, so tracks share a stream label.
1447 rtc::scoped_refptr<AudioTrackInterface> audio_track(
1448 pc_factory_->CreateAudioTrack("audio_track", nullptr));
1449 rtc::scoped_refptr<VideoTrackInterface> video_track(
1450 pc_factory_->CreateVideoTrack(
1451 "video_track",
1452 pc_factory_->CreateVideoSource(new cricket::FakeVideoCapturer())));
1453 auto audio_sender =
1454 pc_->AddTrack(audio_track, std::vector<MediaStreamInterface*>());
1455 auto video_sender =
1456 pc_->AddTrack(video_track, std::vector<MediaStreamInterface*>());
1457 EXPECT_EQ("audio_track", audio_sender->id());
1458 EXPECT_EQ(audio_track, audio_sender->track());
1459 EXPECT_EQ("video_track", video_sender->id());
1460 EXPECT_EQ(video_track, video_sender->track());
1461 // If the ID is truly a random GUID, it should be infinitely unlikely they
1462 // will be the same.
1463 EXPECT_NE(video_sender->stream_ids(), audio_sender->stream_ids());
1464 }
1465
1466 TEST_F(PeerConnectionInterfaceTest, CreateOfferReceiveAnswer) {
1467 InitiateCall();
1468 WaitAndVerifyOnAddStream(kStreamLabel1);
1469 VerifyRemoteRtpHeaderExtensions();
1470 }
1471
1472 TEST_F(PeerConnectionInterfaceTest, CreateOfferReceivePrAnswerAndAnswer) {
1473 CreatePeerConnection();
1474 AddVideoStream(kStreamLabel1);
1475 CreateOfferAsLocalDescription();
1476 std::string offer;
1477 EXPECT_TRUE(pc_->local_description()->ToString(&offer));
1478 CreatePrAnswerAndAnswerAsRemoteDescription(offer);
1479 WaitAndVerifyOnAddStream(kStreamLabel1);
1480 }
1481
1482 TEST_F(PeerConnectionInterfaceTest, ReceiveOfferCreateAnswer) {
1483 CreatePeerConnection();
1484 AddVideoStream(kStreamLabel1);
1485
1486 CreateOfferAsRemoteDescription();
1487 CreateAnswerAsLocalDescription();
1488
1489 WaitAndVerifyOnAddStream(kStreamLabel1);
1490 }
1491
1492 TEST_F(PeerConnectionInterfaceTest, ReceiveOfferCreatePrAnswerAndAnswer) {
1493 CreatePeerConnection();
1494 AddVideoStream(kStreamLabel1);
1495
1496 CreateOfferAsRemoteDescription();
1497 CreatePrAnswerAsLocalDescription();
1498 CreateAnswerAsLocalDescription();
1499
1500 WaitAndVerifyOnAddStream(kStreamLabel1);
1501 }
1502
1503 TEST_F(PeerConnectionInterfaceTest, Renegotiate) {
1504 InitiateCall();
1505 ASSERT_EQ(1u, pc_->remote_streams()->count());
1506 pc_->RemoveStream(pc_->local_streams()->at(0));
1507 CreateOfferReceiveAnswer();
1508 EXPECT_EQ(0u, pc_->remote_streams()->count());
1509 AddVideoStream(kStreamLabel1);
1510 CreateOfferReceiveAnswer();
1511 }
1512
1513 // Tests that after negotiating an audio only call, the respondent can perform a
1514 // renegotiation that removes the audio stream.
1515 TEST_F(PeerConnectionInterfaceTest, RenegotiateAudioOnly) {
1516 CreatePeerConnection();
1517 AddVoiceStream(kStreamLabel1);
1518 CreateOfferAsRemoteDescription();
1519 CreateAnswerAsLocalDescription();
1520
1521 ASSERT_EQ(1u, pc_->remote_streams()->count());
1522 pc_->RemoveStream(pc_->local_streams()->at(0));
1523 CreateOfferReceiveAnswer();
1524 EXPECT_EQ(0u, pc_->remote_streams()->count());
1525 }
1526
1527 // Test that candidates are generated and that we can parse our own candidates.
1528 TEST_F(PeerConnectionInterfaceTest, IceCandidates) {
1529 CreatePeerConnection();
1530
1531 EXPECT_FALSE(pc_->AddIceCandidate(observer_.last_candidate_.get()));
1532 // SetRemoteDescription takes ownership of offer.
1533 std::unique_ptr<SessionDescriptionInterface> offer;
1534 AddVideoStream(kStreamLabel1);
1535 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
1536 EXPECT_TRUE(DoSetRemoteDescription(offer.release()));
1537
1538 // SetLocalDescription takes ownership of answer.
1539 std::unique_ptr<SessionDescriptionInterface> answer;
1540 EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
1541 EXPECT_TRUE(DoSetLocalDescription(answer.release()));
1542
1543 EXPECT_TRUE_WAIT(observer_.last_candidate_.get() != NULL, kTimeout);
1544 EXPECT_TRUE_WAIT(observer_.ice_complete_, kTimeout);
1545
1546 EXPECT_TRUE(pc_->AddIceCandidate(observer_.last_candidate_.get()));
1547 }
1548
1549 // Test that CreateOffer and CreateAnswer will fail if the track labels are
1550 // not unique.
1551 TEST_F(PeerConnectionInterfaceTest, CreateOfferAnswerWithInvalidStream) {
1552 CreatePeerConnection();
1553 // Create a regular offer for the CreateAnswer test later.
1554 std::unique_ptr<SessionDescriptionInterface> offer;
1555 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
1556 EXPECT_TRUE(offer);
1557 offer.reset();
1558
1559 // Create a local stream with audio&video tracks having same label.
1560 AddAudioVideoStream(kStreamLabel1, "track_label", "track_label");
1561
1562 // Test CreateOffer
1563 EXPECT_FALSE(DoCreateOffer(&offer, nullptr));
1564
1565 // Test CreateAnswer
1566 std::unique_ptr<SessionDescriptionInterface> answer;
1567 EXPECT_FALSE(DoCreateAnswer(&answer, nullptr));
1568 }
1569
1570 // Test that we will get different SSRCs for each tracks in the offer and answer
1571 // we created.
1572 TEST_F(PeerConnectionInterfaceTest, SsrcInOfferAnswer) {
1573 CreatePeerConnection();
1574 // Create a local stream with audio&video tracks having different labels.
1575 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
1576
1577 // Test CreateOffer
1578 std::unique_ptr<SessionDescriptionInterface> offer;
1579 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
1580 int audio_ssrc = 0;
1581 int video_ssrc = 0;
1582 EXPECT_TRUE(GetFirstSsrc(GetFirstAudioContent(offer->description()),
1583 &audio_ssrc));
1584 EXPECT_TRUE(GetFirstSsrc(GetFirstVideoContent(offer->description()),
1585 &video_ssrc));
1586 EXPECT_NE(audio_ssrc, video_ssrc);
1587
1588 // Test CreateAnswer
1589 EXPECT_TRUE(DoSetRemoteDescription(offer.release()));
1590 std::unique_ptr<SessionDescriptionInterface> answer;
1591 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
1592 audio_ssrc = 0;
1593 video_ssrc = 0;
1594 EXPECT_TRUE(GetFirstSsrc(GetFirstAudioContent(answer->description()),
1595 &audio_ssrc));
1596 EXPECT_TRUE(GetFirstSsrc(GetFirstVideoContent(answer->description()),
1597 &video_ssrc));
1598 EXPECT_NE(audio_ssrc, video_ssrc);
1599 }
1600
1601 // Test that it's possible to call AddTrack on a MediaStream after adding
1602 // the stream to a PeerConnection.
1603 // TODO(deadbeef): Remove this test once this behavior is no longer supported.
1604 TEST_F(PeerConnectionInterfaceTest, AddTrackAfterAddStream) {
1605 CreatePeerConnection();
1606 // Create audio stream and add to PeerConnection.
1607 AddVoiceStream(kStreamLabel1);
1608 MediaStreamInterface* stream = pc_->local_streams()->at(0);
1609
1610 // Add video track to the audio-only stream.
1611 rtc::scoped_refptr<VideoTrackInterface> video_track(
1612 pc_factory_->CreateVideoTrack(
1613 "video_label",
1614 pc_factory_->CreateVideoSource(new cricket::FakeVideoCapturer())));
1615 stream->AddTrack(video_track.get());
1616
1617 std::unique_ptr<SessionDescriptionInterface> offer;
1618 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
1619
1620 const cricket::MediaContentDescription* video_desc =
1621 cricket::GetFirstVideoContentDescription(offer->description());
1622 EXPECT_TRUE(video_desc != nullptr);
1623 }
1624
1625 // Test that it's possible to call RemoveTrack on a MediaStream after adding
1626 // the stream to a PeerConnection.
1627 // TODO(deadbeef): Remove this test once this behavior is no longer supported.
1628 TEST_F(PeerConnectionInterfaceTest, RemoveTrackAfterAddStream) {
1629 CreatePeerConnection();
1630 // Create audio/video stream and add to PeerConnection.
1631 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
1632 MediaStreamInterface* stream = pc_->local_streams()->at(0);
1633
1634 // Remove the video track.
1635 stream->RemoveTrack(stream->GetVideoTracks()[0]);
1636
1637 std::unique_ptr<SessionDescriptionInterface> offer;
1638 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
1639
1640 const cricket::MediaContentDescription* video_desc =
1641 cricket::GetFirstVideoContentDescription(offer->description());
1642 EXPECT_TRUE(video_desc == nullptr);
1643 }
1644
1645 // Test creating a sender with a stream ID, and ensure the ID is populated
1646 // in the offer.
1647 TEST_F(PeerConnectionInterfaceTest, CreateSenderWithStream) {
1648 CreatePeerConnection();
1649 pc_->CreateSender("video", kStreamLabel1);
1650
1651 std::unique_ptr<SessionDescriptionInterface> offer;
1652 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
1653
1654 const cricket::MediaContentDescription* video_desc =
1655 cricket::GetFirstVideoContentDescription(offer->description());
1656 ASSERT_TRUE(video_desc != nullptr);
1657 ASSERT_EQ(1u, video_desc->streams().size());
1658 EXPECT_EQ(kStreamLabel1, video_desc->streams()[0].sync_label);
1659 }
1660
1661 // Test that we can specify a certain track that we want statistics about.
1662 TEST_F(PeerConnectionInterfaceTest, GetStatsForSpecificTrack) {
1663 InitiateCall();
1664 ASSERT_LT(0u, pc_->remote_streams()->count());
1665 ASSERT_LT(0u, pc_->remote_streams()->at(0)->GetAudioTracks().size());
1666 rtc::scoped_refptr<MediaStreamTrackInterface> remote_audio =
1667 pc_->remote_streams()->at(0)->GetAudioTracks()[0];
1668 EXPECT_TRUE(DoGetStats(remote_audio));
1669
1670 // Remove the stream. Since we are sending to our selves the local
1671 // and the remote stream is the same.
1672 pc_->RemoveStream(pc_->local_streams()->at(0));
1673 // Do a re-negotiation.
1674 CreateOfferReceiveAnswer();
1675
1676 ASSERT_EQ(0u, pc_->remote_streams()->count());
1677
1678 // Test that we still can get statistics for the old track. Even if it is not
1679 // sent any longer.
1680 EXPECT_TRUE(DoGetStats(remote_audio));
1681 }
1682
1683 // Test that we can get stats on a video track.
1684 TEST_F(PeerConnectionInterfaceTest, GetStatsForVideoTrack) {
1685 InitiateCall();
1686 ASSERT_LT(0u, pc_->remote_streams()->count());
1687 ASSERT_LT(0u, pc_->remote_streams()->at(0)->GetVideoTracks().size());
1688 rtc::scoped_refptr<MediaStreamTrackInterface> remote_video =
1689 pc_->remote_streams()->at(0)->GetVideoTracks()[0];
1690 EXPECT_TRUE(DoGetStats(remote_video));
1691 }
1692
1693 // Test that we don't get statistics for an invalid track.
1694 TEST_F(PeerConnectionInterfaceTest, GetStatsForInvalidTrack) {
1695 InitiateCall();
1696 rtc::scoped_refptr<AudioTrackInterface> unknown_audio_track(
1697 pc_factory_->CreateAudioTrack("unknown track", NULL));
1698 EXPECT_FALSE(DoGetStats(unknown_audio_track));
1699 }
1700
1701 // This test setup two RTP data channels in loop back.
1702 TEST_F(PeerConnectionInterfaceTest, TestDataChannel) {
1703 FakeConstraints constraints;
1704 constraints.SetAllowRtpDataChannels();
1705 CreatePeerConnection(&constraints);
1706 rtc::scoped_refptr<DataChannelInterface> data1 =
1707 pc_->CreateDataChannel("test1", NULL);
1708 rtc::scoped_refptr<DataChannelInterface> data2 =
1709 pc_->CreateDataChannel("test2", NULL);
1710 ASSERT_TRUE(data1 != NULL);
1711 std::unique_ptr<MockDataChannelObserver> observer1(
1712 new MockDataChannelObserver(data1));
1713 std::unique_ptr<MockDataChannelObserver> observer2(
1714 new MockDataChannelObserver(data2));
1715
1716 EXPECT_EQ(DataChannelInterface::kConnecting, data1->state());
1717 EXPECT_EQ(DataChannelInterface::kConnecting, data2->state());
1718 std::string data_to_send1 = "testing testing";
1719 std::string data_to_send2 = "testing something else";
1720 EXPECT_FALSE(data1->Send(DataBuffer(data_to_send1)));
1721
1722 CreateOfferReceiveAnswer();
1723 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1724 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
1725
1726 EXPECT_EQ(DataChannelInterface::kOpen, data1->state());
1727 EXPECT_EQ(DataChannelInterface::kOpen, data2->state());
1728 EXPECT_TRUE(data1->Send(DataBuffer(data_to_send1)));
1729 EXPECT_TRUE(data2->Send(DataBuffer(data_to_send2)));
1730
1731 EXPECT_EQ_WAIT(data_to_send1, observer1->last_message(), kTimeout);
1732 EXPECT_EQ_WAIT(data_to_send2, observer2->last_message(), kTimeout);
1733
1734 data1->Close();
1735 EXPECT_EQ(DataChannelInterface::kClosing, data1->state());
1736 CreateOfferReceiveAnswer();
1737 EXPECT_FALSE(observer1->IsOpen());
1738 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
1739 EXPECT_TRUE(observer2->IsOpen());
1740
1741 data_to_send2 = "testing something else again";
1742 EXPECT_TRUE(data2->Send(DataBuffer(data_to_send2)));
1743
1744 EXPECT_EQ_WAIT(data_to_send2, observer2->last_message(), kTimeout);
1745 }
1746
1747 // This test verifies that sendnig binary data over RTP data channels should
1748 // fail.
1749 TEST_F(PeerConnectionInterfaceTest, TestSendBinaryOnRtpDataChannel) {
1750 FakeConstraints constraints;
1751 constraints.SetAllowRtpDataChannels();
1752 CreatePeerConnection(&constraints);
1753 rtc::scoped_refptr<DataChannelInterface> data1 =
1754 pc_->CreateDataChannel("test1", NULL);
1755 rtc::scoped_refptr<DataChannelInterface> data2 =
1756 pc_->CreateDataChannel("test2", NULL);
1757 ASSERT_TRUE(data1 != NULL);
1758 std::unique_ptr<MockDataChannelObserver> observer1(
1759 new MockDataChannelObserver(data1));
1760 std::unique_ptr<MockDataChannelObserver> observer2(
1761 new MockDataChannelObserver(data2));
1762
1763 EXPECT_EQ(DataChannelInterface::kConnecting, data1->state());
1764 EXPECT_EQ(DataChannelInterface::kConnecting, data2->state());
1765
1766 CreateOfferReceiveAnswer();
1767 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1768 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
1769
1770 EXPECT_EQ(DataChannelInterface::kOpen, data1->state());
1771 EXPECT_EQ(DataChannelInterface::kOpen, data2->state());
1772
1773 rtc::CopyOnWriteBuffer buffer("test", 4);
1774 EXPECT_FALSE(data1->Send(DataBuffer(buffer, true)));
1775 }
1776
1777 // This test setup a RTP data channels in loop back and test that a channel is
1778 // opened even if the remote end answer with a zero SSRC.
1779 TEST_F(PeerConnectionInterfaceTest, TestSendOnlyDataChannel) {
1780 FakeConstraints constraints;
1781 constraints.SetAllowRtpDataChannels();
1782 CreatePeerConnection(&constraints);
1783 rtc::scoped_refptr<DataChannelInterface> data1 =
1784 pc_->CreateDataChannel("test1", NULL);
1785 std::unique_ptr<MockDataChannelObserver> observer1(
1786 new MockDataChannelObserver(data1));
1787
1788 CreateOfferReceiveAnswerWithoutSsrc();
1789
1790 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1791
1792 data1->Close();
1793 EXPECT_EQ(DataChannelInterface::kClosing, data1->state());
1794 CreateOfferReceiveAnswerWithoutSsrc();
1795 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
1796 EXPECT_FALSE(observer1->IsOpen());
1797 }
1798
1799 // This test that if a data channel is added in an answer a receive only channel
1800 // channel is created.
1801 TEST_F(PeerConnectionInterfaceTest, TestReceiveOnlyDataChannel) {
1802 FakeConstraints constraints;
1803 constraints.SetAllowRtpDataChannels();
1804 CreatePeerConnection(&constraints);
1805
1806 std::string offer_label = "offer_channel";
1807 rtc::scoped_refptr<DataChannelInterface> offer_channel =
1808 pc_->CreateDataChannel(offer_label, NULL);
1809
1810 CreateOfferAsLocalDescription();
1811
1812 // Replace the data channel label in the offer and apply it as an answer.
1813 std::string receive_label = "answer_channel";
1814 std::string sdp;
1815 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
1816 rtc::replace_substrs(offer_label.c_str(), offer_label.length(),
1817 receive_label.c_str(), receive_label.length(),
1818 &sdp);
1819 CreateAnswerAsRemoteDescription(sdp);
1820
1821 // Verify that a new incoming data channel has been created and that
1822 // it is open but can't we written to.
1823 ASSERT_TRUE(observer_.last_datachannel_ != NULL);
1824 DataChannelInterface* received_channel = observer_.last_datachannel_;
1825 EXPECT_EQ(DataChannelInterface::kConnecting, received_channel->state());
1826 EXPECT_EQ(receive_label, received_channel->label());
1827 EXPECT_FALSE(received_channel->Send(DataBuffer("something")));
1828
1829 // Verify that the channel we initially offered has been rejected.
1830 EXPECT_EQ(DataChannelInterface::kClosed, offer_channel->state());
1831
1832 // Do another offer / answer exchange and verify that the data channel is
1833 // opened.
1834 CreateOfferReceiveAnswer();
1835 EXPECT_EQ_WAIT(DataChannelInterface::kOpen, received_channel->state(),
1836 kTimeout);
1837 }
1838
1839 // This test that no data channel is returned if a reliable channel is
1840 // requested.
1841 // TODO(perkj): Remove this test once reliable channels are implemented.
1842 TEST_F(PeerConnectionInterfaceTest, CreateReliableRtpDataChannelShouldFail) {
1843 FakeConstraints constraints;
1844 constraints.SetAllowRtpDataChannels();
1845 CreatePeerConnection(&constraints);
1846
1847 std::string label = "test";
1848 webrtc::DataChannelInit config;
1849 config.reliable = true;
1850 rtc::scoped_refptr<DataChannelInterface> channel =
1851 pc_->CreateDataChannel(label, &config);
1852 EXPECT_TRUE(channel == NULL);
1853 }
1854
1855 // Verifies that duplicated label is not allowed for RTP data channel.
1856 TEST_F(PeerConnectionInterfaceTest, RtpDuplicatedLabelNotAllowed) {
1857 FakeConstraints constraints;
1858 constraints.SetAllowRtpDataChannels();
1859 CreatePeerConnection(&constraints);
1860
1861 std::string label = "test";
1862 rtc::scoped_refptr<DataChannelInterface> channel =
1863 pc_->CreateDataChannel(label, nullptr);
1864 EXPECT_NE(channel, nullptr);
1865
1866 rtc::scoped_refptr<DataChannelInterface> dup_channel =
1867 pc_->CreateDataChannel(label, nullptr);
1868 EXPECT_EQ(dup_channel, nullptr);
1869 }
1870
1871 // This tests that a SCTP data channel is returned using different
1872 // DataChannelInit configurations.
1873 TEST_F(PeerConnectionInterfaceTest, CreateSctpDataChannel) {
1874 FakeConstraints constraints;
1875 constraints.SetAllowDtlsSctpDataChannels();
1876 CreatePeerConnection(&constraints);
1877
1878 webrtc::DataChannelInit config;
1879
1880 rtc::scoped_refptr<DataChannelInterface> channel =
1881 pc_->CreateDataChannel("1", &config);
1882 EXPECT_TRUE(channel != NULL);
1883 EXPECT_TRUE(channel->reliable());
1884 EXPECT_TRUE(observer_.renegotiation_needed_);
1885 observer_.renegotiation_needed_ = false;
1886
1887 config.ordered = false;
1888 channel = pc_->CreateDataChannel("2", &config);
1889 EXPECT_TRUE(channel != NULL);
1890 EXPECT_TRUE(channel->reliable());
1891 EXPECT_FALSE(observer_.renegotiation_needed_);
1892
1893 config.ordered = true;
1894 config.maxRetransmits = 0;
1895 channel = pc_->CreateDataChannel("3", &config);
1896 EXPECT_TRUE(channel != NULL);
1897 EXPECT_FALSE(channel->reliable());
1898 EXPECT_FALSE(observer_.renegotiation_needed_);
1899
1900 config.maxRetransmits = -1;
1901 config.maxRetransmitTime = 0;
1902 channel = pc_->CreateDataChannel("4", &config);
1903 EXPECT_TRUE(channel != NULL);
1904 EXPECT_FALSE(channel->reliable());
1905 EXPECT_FALSE(observer_.renegotiation_needed_);
1906 }
1907
1908 // This tests that no data channel is returned if both maxRetransmits and
1909 // maxRetransmitTime are set for SCTP data channels.
1910 TEST_F(PeerConnectionInterfaceTest,
1911 CreateSctpDataChannelShouldFailForInvalidConfig) {
1912 FakeConstraints constraints;
1913 constraints.SetAllowDtlsSctpDataChannels();
1914 CreatePeerConnection(&constraints);
1915
1916 std::string label = "test";
1917 webrtc::DataChannelInit config;
1918 config.maxRetransmits = 0;
1919 config.maxRetransmitTime = 0;
1920
1921 rtc::scoped_refptr<DataChannelInterface> channel =
1922 pc_->CreateDataChannel(label, &config);
1923 EXPECT_TRUE(channel == NULL);
1924 }
1925
1926 // The test verifies that creating a SCTP data channel with an id already in use
1927 // or out of range should fail.
1928 TEST_F(PeerConnectionInterfaceTest,
1929 CreateSctpDataChannelWithInvalidIdShouldFail) {
1930 FakeConstraints constraints;
1931 constraints.SetAllowDtlsSctpDataChannels();
1932 CreatePeerConnection(&constraints);
1933
1934 webrtc::DataChannelInit config;
1935 rtc::scoped_refptr<DataChannelInterface> channel;
1936
1937 config.id = 1;
1938 channel = pc_->CreateDataChannel("1", &config);
1939 EXPECT_TRUE(channel != NULL);
1940 EXPECT_EQ(1, channel->id());
1941
1942 channel = pc_->CreateDataChannel("x", &config);
1943 EXPECT_TRUE(channel == NULL);
1944
1945 config.id = cricket::kMaxSctpSid;
1946 channel = pc_->CreateDataChannel("max", &config);
1947 EXPECT_TRUE(channel != NULL);
1948 EXPECT_EQ(config.id, channel->id());
1949
1950 config.id = cricket::kMaxSctpSid + 1;
1951 channel = pc_->CreateDataChannel("x", &config);
1952 EXPECT_TRUE(channel == NULL);
1953 }
1954
1955 // Verifies that duplicated label is allowed for SCTP data channel.
1956 TEST_F(PeerConnectionInterfaceTest, SctpDuplicatedLabelAllowed) {
1957 FakeConstraints constraints;
1958 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
1959 true);
1960 CreatePeerConnection(&constraints);
1961
1962 std::string label = "test";
1963 rtc::scoped_refptr<DataChannelInterface> channel =
1964 pc_->CreateDataChannel(label, nullptr);
1965 EXPECT_NE(channel, nullptr);
1966
1967 rtc::scoped_refptr<DataChannelInterface> dup_channel =
1968 pc_->CreateDataChannel(label, nullptr);
1969 EXPECT_NE(dup_channel, nullptr);
1970 }
1971
1972 // This test verifies that OnRenegotiationNeeded is fired for every new RTP
1973 // DataChannel.
1974 TEST_F(PeerConnectionInterfaceTest, RenegotiationNeededForNewRtpDataChannel) {
1975 FakeConstraints constraints;
1976 constraints.SetAllowRtpDataChannels();
1977 CreatePeerConnection(&constraints);
1978
1979 rtc::scoped_refptr<DataChannelInterface> dc1 =
1980 pc_->CreateDataChannel("test1", NULL);
1981 EXPECT_TRUE(observer_.renegotiation_needed_);
1982 observer_.renegotiation_needed_ = false;
1983
1984 rtc::scoped_refptr<DataChannelInterface> dc2 =
1985 pc_->CreateDataChannel("test2", NULL);
1986 EXPECT_TRUE(observer_.renegotiation_needed_);
1987 }
1988
1989 // This test that a data channel closes when a PeerConnection is deleted/closed.
1990 TEST_F(PeerConnectionInterfaceTest, DataChannelCloseWhenPeerConnectionClose) {
1991 FakeConstraints constraints;
1992 constraints.SetAllowRtpDataChannels();
1993 CreatePeerConnection(&constraints);
1994
1995 rtc::scoped_refptr<DataChannelInterface> data1 =
1996 pc_->CreateDataChannel("test1", NULL);
1997 rtc::scoped_refptr<DataChannelInterface> data2 =
1998 pc_->CreateDataChannel("test2", NULL);
1999 ASSERT_TRUE(data1 != NULL);
2000 std::unique_ptr<MockDataChannelObserver> observer1(
2001 new MockDataChannelObserver(data1));
2002 std::unique_ptr<MockDataChannelObserver> observer2(
2003 new MockDataChannelObserver(data2));
2004
2005 CreateOfferReceiveAnswer();
2006 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
2007 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
2008
2009 ReleasePeerConnection();
2010 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
2011 EXPECT_EQ(DataChannelInterface::kClosed, data2->state());
2012 }
2013
2014 // This test that data channels can be rejected in an answer.
2015 TEST_F(PeerConnectionInterfaceTest, TestRejectDataChannelInAnswer) {
2016 FakeConstraints constraints;
2017 constraints.SetAllowRtpDataChannels();
2018 CreatePeerConnection(&constraints);
2019
2020 rtc::scoped_refptr<DataChannelInterface> offer_channel(
2021 pc_->CreateDataChannel("offer_channel", NULL));
2022
2023 CreateOfferAsLocalDescription();
2024
2025 // Create an answer where the m-line for data channels are rejected.
2026 std::string sdp;
2027 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
2028 webrtc::JsepSessionDescription* answer = new webrtc::JsepSessionDescription(
2029 SessionDescriptionInterface::kAnswer);
2030 EXPECT_TRUE(answer->Initialize(sdp, NULL));
2031 cricket::ContentInfo* data_info =
2032 answer->description()->GetContentByName("data");
2033 data_info->rejected = true;
2034
2035 DoSetRemoteDescription(answer);
2036 EXPECT_EQ(DataChannelInterface::kClosed, offer_channel->state());
2037 }
2038
2039 // Test that we can create a session description from an SDP string from
2040 // FireFox, use it as a remote session description, generate an answer and use
2041 // the answer as a local description.
2042 TEST_F(PeerConnectionInterfaceTest, ReceiveFireFoxOffer) {
2043 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
2044 FakeConstraints constraints;
2045 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2046 true);
2047 CreatePeerConnection(&constraints);
2048 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
2049 SessionDescriptionInterface* desc =
2050 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
2051 webrtc::kFireFoxSdpOffer, nullptr);
2052 EXPECT_TRUE(DoSetSessionDescription(desc, false));
2053 CreateAnswerAsLocalDescription();
2054 ASSERT_TRUE(pc_->local_description() != NULL);
2055 ASSERT_TRUE(pc_->remote_description() != NULL);
2056
2057 const cricket::ContentInfo* content =
2058 cricket::GetFirstAudioContent(pc_->local_description()->description());
2059 ASSERT_TRUE(content != NULL);
2060 EXPECT_FALSE(content->rejected);
2061
2062 content =
2063 cricket::GetFirstVideoContent(pc_->local_description()->description());
2064 ASSERT_TRUE(content != NULL);
2065 EXPECT_FALSE(content->rejected);
2066 #ifdef HAVE_SCTP
2067 content =
2068 cricket::GetFirstDataContent(pc_->local_description()->description());
2069 ASSERT_TRUE(content != NULL);
2070 EXPECT_TRUE(content->rejected);
2071 #endif
2072 }
2073
2074 // Test that we can create an audio only offer and receive an answer with a
2075 // limited set of audio codecs and receive an updated offer with more audio
2076 // codecs, where the added codecs are not supported.
2077 TEST_F(PeerConnectionInterfaceTest, ReceiveUpdatedAudioOfferWithBadCodecs) {
2078 CreatePeerConnection();
2079 AddVoiceStream("audio_label");
2080 CreateOfferAsLocalDescription();
2081
2082 SessionDescriptionInterface* answer =
2083 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
2084 webrtc::kAudioSdp, nullptr);
2085 EXPECT_TRUE(DoSetSessionDescription(answer, false));
2086
2087 SessionDescriptionInterface* updated_offer =
2088 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
2089 webrtc::kAudioSdpWithUnsupportedCodecs,
2090 nullptr);
2091 EXPECT_TRUE(DoSetSessionDescription(updated_offer, false));
2092 CreateAnswerAsLocalDescription();
2093 }
2094
2095 // Test that if we're receiving (but not sending) a track, subsequent offers
2096 // will have m-lines with a=recvonly.
2097 TEST_F(PeerConnectionInterfaceTest, CreateSubsequentRecvOnlyOffer) {
2098 FakeConstraints constraints;
2099 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2100 true);
2101 CreatePeerConnection(&constraints);
2102 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2103 CreateAnswerAsLocalDescription();
2104
2105 // At this point we should be receiving stream 1, but not sending anything.
2106 // A new offer should be recvonly.
2107 std::unique_ptr<SessionDescriptionInterface> offer;
2108 DoCreateOffer(&offer, nullptr);
2109
2110 const cricket::ContentInfo* video_content =
2111 cricket::GetFirstVideoContent(offer->description());
2112 const cricket::VideoContentDescription* video_desc =
2113 static_cast<const cricket::VideoContentDescription*>(
2114 video_content->description);
2115 ASSERT_EQ(cricket::MD_RECVONLY, video_desc->direction());
2116
2117 const cricket::ContentInfo* audio_content =
2118 cricket::GetFirstAudioContent(offer->description());
2119 const cricket::AudioContentDescription* audio_desc =
2120 static_cast<const cricket::AudioContentDescription*>(
2121 audio_content->description);
2122 ASSERT_EQ(cricket::MD_RECVONLY, audio_desc->direction());
2123 }
2124
2125 // Test that if we're receiving (but not sending) a track, and the
2126 // offerToReceiveVideo/offerToReceiveAudio constraints are explicitly set to
2127 // false, the generated m-lines will be a=inactive.
2128 TEST_F(PeerConnectionInterfaceTest, CreateSubsequentInactiveOffer) {
2129 FakeConstraints constraints;
2130 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2131 true);
2132 CreatePeerConnection(&constraints);
2133 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2134 CreateAnswerAsLocalDescription();
2135
2136 // At this point we should be receiving stream 1, but not sending anything.
2137 // A new offer would be recvonly, but we'll set the "no receive" constraints
2138 // to make it inactive.
2139 std::unique_ptr<SessionDescriptionInterface> offer;
2140 FakeConstraints offer_constraints;
2141 offer_constraints.AddMandatory(
2142 webrtc::MediaConstraintsInterface::kOfferToReceiveVideo, false);
2143 offer_constraints.AddMandatory(
2144 webrtc::MediaConstraintsInterface::kOfferToReceiveAudio, false);
2145 DoCreateOffer(&offer, &offer_constraints);
2146
2147 const cricket::ContentInfo* video_content =
2148 cricket::GetFirstVideoContent(offer->description());
2149 const cricket::VideoContentDescription* video_desc =
2150 static_cast<const cricket::VideoContentDescription*>(
2151 video_content->description);
2152 ASSERT_EQ(cricket::MD_INACTIVE, video_desc->direction());
2153
2154 const cricket::ContentInfo* audio_content =
2155 cricket::GetFirstAudioContent(offer->description());
2156 const cricket::AudioContentDescription* audio_desc =
2157 static_cast<const cricket::AudioContentDescription*>(
2158 audio_content->description);
2159 ASSERT_EQ(cricket::MD_INACTIVE, audio_desc->direction());
2160 }
2161
2162 // Test that we can use SetConfiguration to change the ICE servers of the
2163 // PortAllocator.
2164 TEST_F(PeerConnectionInterfaceTest, SetConfigurationChangesIceServers) {
2165 CreatePeerConnection();
2166
2167 PeerConnectionInterface::RTCConfiguration config;
2168 PeerConnectionInterface::IceServer server;
2169 server.uri = "stun:test_hostname";
2170 config.servers.push_back(server);
2171 EXPECT_TRUE(pc_->SetConfiguration(config));
2172
2173 EXPECT_EQ(1u, port_allocator_->stun_servers().size());
2174 EXPECT_EQ("test_hostname",
2175 port_allocator_->stun_servers().begin()->hostname());
2176 }
2177
2178 TEST_F(PeerConnectionInterfaceTest, SetConfigurationChangesCandidateFilter) {
2179 CreatePeerConnection();
2180 PeerConnectionInterface::RTCConfiguration config;
2181 config.type = PeerConnectionInterface::kRelay;
2182 EXPECT_TRUE(pc_->SetConfiguration(config));
2183 EXPECT_EQ(cricket::CF_RELAY, port_allocator_->candidate_filter());
2184 }
2185
2186 // Test that when SetConfiguration changes both the pool size and other
2187 // attributes, the pooled session is created with the updated attributes.
2188 TEST_F(PeerConnectionInterfaceTest,
2189 SetConfigurationCreatesPooledSessionCorrectly) {
2190 CreatePeerConnection();
2191 PeerConnectionInterface::RTCConfiguration config;
2192 config.ice_candidate_pool_size = 1;
2193 PeerConnectionInterface::IceServer server;
2194 server.uri = kStunAddressOnly;
2195 config.servers.push_back(server);
2196 config.type = PeerConnectionInterface::kRelay;
2197 EXPECT_TRUE(pc_->SetConfiguration(config));
2198
2199 const cricket::FakePortAllocatorSession* session =
2200 static_cast<const cricket::FakePortAllocatorSession*>(
2201 port_allocator_->GetPooledSession());
2202 ASSERT_NE(nullptr, session);
2203 EXPECT_EQ(1UL, session->stun_servers().size());
2204 }
2205
2206 // Test that after SetLocalDescription, changing the pool size is not allowed.
2207 TEST_F(PeerConnectionInterfaceTest,
2208 CantChangePoolSizeAfterSetLocalDescription) {
2209 CreatePeerConnection();
2210 // Start by setting a size of 1.
2211 PeerConnectionInterface::RTCConfiguration config;
2212 config.ice_candidate_pool_size = 1;
2213 EXPECT_TRUE(pc_->SetConfiguration(config));
2214
2215 // Set remote offer; can still change pool size at this point.
2216 CreateOfferAsRemoteDescription();
2217 config.ice_candidate_pool_size = 2;
2218 EXPECT_TRUE(pc_->SetConfiguration(config));
2219
2220 // Set local answer; now it's too late.
2221 CreateAnswerAsLocalDescription();
2222 config.ice_candidate_pool_size = 3;
2223 EXPECT_FALSE(pc_->SetConfiguration(config));
2224 }
2225
2226 // Test that PeerConnection::Close changes the states to closed and all remote
2227 // tracks change state to ended.
2228 TEST_F(PeerConnectionInterfaceTest, CloseAndTestStreamsAndStates) {
2229 // Initialize a PeerConnection and negotiate local and remote session
2230 // description.
2231 InitiateCall();
2232 ASSERT_EQ(1u, pc_->local_streams()->count());
2233 ASSERT_EQ(1u, pc_->remote_streams()->count());
2234
2235 pc_->Close();
2236
2237 EXPECT_EQ(PeerConnectionInterface::kClosed, pc_->signaling_state());
2238 EXPECT_EQ(PeerConnectionInterface::kIceConnectionClosed,
2239 pc_->ice_connection_state());
2240 EXPECT_EQ(PeerConnectionInterface::kIceGatheringComplete,
2241 pc_->ice_gathering_state());
2242
2243 EXPECT_EQ(1u, pc_->local_streams()->count());
2244 EXPECT_EQ(1u, pc_->remote_streams()->count());
2245
2246 rtc::scoped_refptr<MediaStreamInterface> remote_stream =
2247 pc_->remote_streams()->at(0);
2248 // Track state may be updated asynchronously.
2249 EXPECT_EQ_WAIT(MediaStreamTrackInterface::kEnded,
2250 remote_stream->GetAudioTracks()[0]->state(), kTimeout);
2251 EXPECT_EQ_WAIT(MediaStreamTrackInterface::kEnded,
2252 remote_stream->GetVideoTracks()[0]->state(), kTimeout);
2253 }
2254
2255 // Test that PeerConnection methods fails gracefully after
2256 // PeerConnection::Close has been called.
2257 TEST_F(PeerConnectionInterfaceTest, CloseAndTestMethods) {
2258 CreatePeerConnection();
2259 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
2260 CreateOfferAsRemoteDescription();
2261 CreateAnswerAsLocalDescription();
2262
2263 ASSERT_EQ(1u, pc_->local_streams()->count());
2264 rtc::scoped_refptr<MediaStreamInterface> local_stream =
2265 pc_->local_streams()->at(0);
2266
2267 pc_->Close();
2268
2269 pc_->RemoveStream(local_stream);
2270 EXPECT_FALSE(pc_->AddStream(local_stream));
2271
2272 ASSERT_FALSE(local_stream->GetAudioTracks().empty());
2273 rtc::scoped_refptr<webrtc::DtmfSenderInterface> dtmf_sender(
2274 pc_->CreateDtmfSender(local_stream->GetAudioTracks()[0]));
2275 EXPECT_TRUE(NULL == dtmf_sender); // local stream has been removed.
2276
2277 EXPECT_TRUE(pc_->CreateDataChannel("test", NULL) == NULL);
2278
2279 EXPECT_TRUE(pc_->local_description() != NULL);
2280 EXPECT_TRUE(pc_->remote_description() != NULL);
2281
2282 std::unique_ptr<SessionDescriptionInterface> offer;
2283 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
2284 std::unique_ptr<SessionDescriptionInterface> answer;
2285 EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
2286
2287 std::string sdp;
2288 ASSERT_TRUE(pc_->remote_description()->ToString(&sdp));
2289 SessionDescriptionInterface* remote_offer =
2290 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
2291 sdp, NULL);
2292 EXPECT_FALSE(DoSetRemoteDescription(remote_offer));
2293
2294 ASSERT_TRUE(pc_->local_description()->ToString(&sdp));
2295 SessionDescriptionInterface* local_offer =
2296 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
2297 sdp, NULL);
2298 EXPECT_FALSE(DoSetLocalDescription(local_offer));
2299 }
2300
2301 // Test that GetStats can still be called after PeerConnection::Close.
2302 TEST_F(PeerConnectionInterfaceTest, CloseAndGetStats) {
2303 InitiateCall();
2304 pc_->Close();
2305 DoGetStats(NULL);
2306 }
2307
2308 // NOTE: The series of tests below come from what used to be
2309 // mediastreamsignaling_unittest.cc, and are mostly aimed at testing that
2310 // setting a remote or local description has the expected effects.
2311
2312 // This test verifies that the remote MediaStreams corresponding to a received
2313 // SDP string is created. In this test the two separate MediaStreams are
2314 // signaled.
2315 TEST_F(PeerConnectionInterfaceTest, UpdateRemoteStreams) {
2316 FakeConstraints constraints;
2317 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2318 true);
2319 CreatePeerConnection(&constraints);
2320 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2321
2322 rtc::scoped_refptr<StreamCollection> reference(CreateStreamCollection(1, 1));
2323 EXPECT_TRUE(
2324 CompareStreamCollections(observer_.remote_streams(), reference.get()));
2325 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2326 EXPECT_TRUE(remote_stream->GetVideoTracks()[0]->GetSource() != nullptr);
2327
2328 // Create a session description based on another SDP with another
2329 // MediaStream.
2330 CreateAndSetRemoteOffer(kSdpStringWithStream1And2);
2331
2332 rtc::scoped_refptr<StreamCollection> reference2(CreateStreamCollection(2, 1));
2333 EXPECT_TRUE(
2334 CompareStreamCollections(observer_.remote_streams(), reference2.get()));
2335 }
2336
2337 // This test verifies that when remote tracks are added/removed from SDP, the
2338 // created remote streams are updated appropriately.
2339 TEST_F(PeerConnectionInterfaceTest,
2340 AddRemoveTrackFromExistingRemoteMediaStream) {
2341 FakeConstraints constraints;
2342 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2343 true);
2344 CreatePeerConnection(&constraints);
2345 std::unique_ptr<SessionDescriptionInterface> desc_ms1 =
2346 CreateSessionDescriptionAndReference(1, 1);
2347 EXPECT_TRUE(DoSetRemoteDescription(desc_ms1.release()));
2348 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
2349 reference_collection_));
2350
2351 // Add extra audio and video tracks to the same MediaStream.
2352 std::unique_ptr<SessionDescriptionInterface> desc_ms1_two_tracks =
2353 CreateSessionDescriptionAndReference(2, 2);
2354 EXPECT_TRUE(DoSetRemoteDescription(desc_ms1_two_tracks.release()));
2355 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
2356 reference_collection_));
2357 rtc::scoped_refptr<AudioTrackInterface> audio_track2 =
2358 observer_.remote_streams()->at(0)->GetAudioTracks()[1];
2359 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, audio_track2->state());
2360 rtc::scoped_refptr<VideoTrackInterface> video_track2 =
2361 observer_.remote_streams()->at(0)->GetVideoTracks()[1];
2362 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, video_track2->state());
2363
2364 // Remove the extra audio and video tracks.
2365 std::unique_ptr<SessionDescriptionInterface> desc_ms2 =
2366 CreateSessionDescriptionAndReference(1, 1);
2367 MockTrackObserver audio_track_observer(audio_track2);
2368 MockTrackObserver video_track_observer(video_track2);
2369
2370 EXPECT_CALL(audio_track_observer, OnChanged()).Times(Exactly(1));
2371 EXPECT_CALL(video_track_observer, OnChanged()).Times(Exactly(1));
2372 EXPECT_TRUE(DoSetRemoteDescription(desc_ms2.release()));
2373 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
2374 reference_collection_));
2375 // Track state may be updated asynchronously.
2376 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
2377 audio_track2->state(), kTimeout);
2378 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
2379 video_track2->state(), kTimeout);
2380 }
2381
2382 // This tests that remote tracks are ended if a local session description is set
2383 // that rejects the media content type.
2384 TEST_F(PeerConnectionInterfaceTest, RejectMediaContent) {
2385 FakeConstraints constraints;
2386 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2387 true);
2388 CreatePeerConnection(&constraints);
2389 // First create and set a remote offer, then reject its video content in our
2390 // answer.
2391 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2392 ASSERT_EQ(1u, observer_.remote_streams()->count());
2393 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2394 ASSERT_EQ(1u, remote_stream->GetVideoTracks().size());
2395 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2396
2397 rtc::scoped_refptr<webrtc::VideoTrackInterface> remote_video =
2398 remote_stream->GetVideoTracks()[0];
2399 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, remote_video->state());
2400 rtc::scoped_refptr<webrtc::AudioTrackInterface> remote_audio =
2401 remote_stream->GetAudioTracks()[0];
2402 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, remote_audio->state());
2403
2404 std::unique_ptr<SessionDescriptionInterface> local_answer;
2405 EXPECT_TRUE(DoCreateAnswer(&local_answer, nullptr));
2406 cricket::ContentInfo* video_info =
2407 local_answer->description()->GetContentByName("video");
2408 video_info->rejected = true;
2409 EXPECT_TRUE(DoSetLocalDescription(local_answer.release()));
2410 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kEnded, remote_video->state());
2411 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, remote_audio->state());
2412
2413 // Now create an offer where we reject both video and audio.
2414 std::unique_ptr<SessionDescriptionInterface> local_offer;
2415 EXPECT_TRUE(DoCreateOffer(&local_offer, nullptr));
2416 video_info = local_offer->description()->GetContentByName("video");
2417 ASSERT_TRUE(video_info != nullptr);
2418 video_info->rejected = true;
2419 cricket::ContentInfo* audio_info =
2420 local_offer->description()->GetContentByName("audio");
2421 ASSERT_TRUE(audio_info != nullptr);
2422 audio_info->rejected = true;
2423 EXPECT_TRUE(DoSetLocalDescription(local_offer.release()));
2424 // Track state may be updated asynchronously.
2425 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
2426 remote_audio->state(), kTimeout);
2427 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
2428 remote_video->state(), kTimeout);
2429 }
2430
2431 // This tests that we won't crash if the remote track has been removed outside
2432 // of PeerConnection and then PeerConnection tries to reject the track.
2433 TEST_F(PeerConnectionInterfaceTest, RemoveTrackThenRejectMediaContent) {
2434 FakeConstraints constraints;
2435 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2436 true);
2437 CreatePeerConnection(&constraints);
2438 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2439 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2440 remote_stream->RemoveTrack(remote_stream->GetVideoTracks()[0]);
2441 remote_stream->RemoveTrack(remote_stream->GetAudioTracks()[0]);
2442
2443 std::unique_ptr<SessionDescriptionInterface> local_answer(
2444 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
2445 kSdpStringWithStream1, nullptr));
2446 cricket::ContentInfo* video_info =
2447 local_answer->description()->GetContentByName("video");
2448 video_info->rejected = true;
2449 cricket::ContentInfo* audio_info =
2450 local_answer->description()->GetContentByName("audio");
2451 audio_info->rejected = true;
2452 EXPECT_TRUE(DoSetLocalDescription(local_answer.release()));
2453
2454 // No crash is a pass.
2455 }
2456
2457 // This tests that if a recvonly remote description is set, no remote streams
2458 // will be created, even if the description contains SSRCs/MSIDs.
2459 // See: https://code.google.com/p/webrtc/issues/detail?id=5054
2460 TEST_F(PeerConnectionInterfaceTest, RecvonlyDescriptionDoesntCreateStream) {
2461 FakeConstraints constraints;
2462 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2463 true);
2464 CreatePeerConnection(&constraints);
2465
2466 std::string recvonly_offer = kSdpStringWithStream1;
2467 rtc::replace_substrs(kSendrecv, strlen(kSendrecv), kRecvonly,
2468 strlen(kRecvonly), &recvonly_offer);
2469 CreateAndSetRemoteOffer(recvonly_offer);
2470
2471 EXPECT_EQ(0u, observer_.remote_streams()->count());
2472 }
2473
2474 // This tests that a default MediaStream is created if a remote session
2475 // description doesn't contain any streams and no MSID support.
2476 // It also tests that the default stream is updated if a video m-line is added
2477 // in a subsequent session description.
2478 TEST_F(PeerConnectionInterfaceTest, SdpWithoutMsidCreatesDefaultStream) {
2479 FakeConstraints constraints;
2480 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2481 true);
2482 CreatePeerConnection(&constraints);
2483 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2484
2485 ASSERT_EQ(1u, observer_.remote_streams()->count());
2486 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2487
2488 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2489 EXPECT_EQ(0u, remote_stream->GetVideoTracks().size());
2490 EXPECT_EQ("default", remote_stream->label());
2491
2492 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2493 ASSERT_EQ(1u, observer_.remote_streams()->count());
2494 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2495 EXPECT_EQ("defaulta0", remote_stream->GetAudioTracks()[0]->id());
2496 EXPECT_EQ(MediaStreamTrackInterface::kLive,
2497 remote_stream->GetAudioTracks()[0]->state());
2498 ASSERT_EQ(1u, remote_stream->GetVideoTracks().size());
2499 EXPECT_EQ("defaultv0", remote_stream->GetVideoTracks()[0]->id());
2500 EXPECT_EQ(MediaStreamTrackInterface::kLive,
2501 remote_stream->GetVideoTracks()[0]->state());
2502 }
2503
2504 // This tests that a default MediaStream is created if a remote session
2505 // description doesn't contain any streams and media direction is send only.
2506 TEST_F(PeerConnectionInterfaceTest,
2507 SendOnlySdpWithoutMsidCreatesDefaultStream) {
2508 FakeConstraints constraints;
2509 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2510 true);
2511 CreatePeerConnection(&constraints);
2512 CreateAndSetRemoteOffer(kSdpStringSendOnlyWithoutStreams);
2513
2514 ASSERT_EQ(1u, observer_.remote_streams()->count());
2515 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2516
2517 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2518 EXPECT_EQ(1u, remote_stream->GetVideoTracks().size());
2519 EXPECT_EQ("default", remote_stream->label());
2520 }
2521
2522 // This tests that it won't crash when PeerConnection tries to remove
2523 // a remote track that as already been removed from the MediaStream.
2524 TEST_F(PeerConnectionInterfaceTest, RemoveAlreadyGoneRemoteStream) {
2525 FakeConstraints constraints;
2526 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2527 true);
2528 CreatePeerConnection(&constraints);
2529 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2530 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2531 remote_stream->RemoveTrack(remote_stream->GetAudioTracks()[0]);
2532 remote_stream->RemoveTrack(remote_stream->GetVideoTracks()[0]);
2533
2534 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2535
2536 // No crash is a pass.
2537 }
2538
2539 // This tests that a default MediaStream is created if the remote session
2540 // description doesn't contain any streams and don't contain an indication if
2541 // MSID is supported.
2542 TEST_F(PeerConnectionInterfaceTest,
2543 SdpWithoutMsidAndStreamsCreatesDefaultStream) {
2544 FakeConstraints constraints;
2545 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2546 true);
2547 CreatePeerConnection(&constraints);
2548 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2549
2550 ASSERT_EQ(1u, observer_.remote_streams()->count());
2551 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2552 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2553 EXPECT_EQ(1u, remote_stream->GetVideoTracks().size());
2554 }
2555
2556 // This tests that a default MediaStream is not created if the remote session
2557 // description doesn't contain any streams but does support MSID.
2558 TEST_F(PeerConnectionInterfaceTest, SdpWithMsidDontCreatesDefaultStream) {
2559 FakeConstraints constraints;
2560 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2561 true);
2562 CreatePeerConnection(&constraints);
2563 CreateAndSetRemoteOffer(kSdpStringWithMsidWithoutStreams);
2564 EXPECT_EQ(0u, observer_.remote_streams()->count());
2565 }
2566
2567 // This tests that when setting a new description, the old default tracks are
2568 // not destroyed and recreated.
2569 // See: https://bugs.chromium.org/p/webrtc/issues/detail?id=5250
2570 TEST_F(PeerConnectionInterfaceTest,
2571 DefaultTracksNotDestroyedAndRecreated) {
2572 FakeConstraints constraints;
2573 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2574 true);
2575 CreatePeerConnection(&constraints);
2576 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2577
2578 ASSERT_EQ(1u, observer_.remote_streams()->count());
2579 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2580 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2581
2582 // Set the track to "disabled", then set a new description and ensure the
2583 // track is still disabled, which ensures it hasn't been recreated.
2584 remote_stream->GetAudioTracks()[0]->set_enabled(false);
2585 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2586 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2587 EXPECT_FALSE(remote_stream->GetAudioTracks()[0]->enabled());
2588 }
2589
2590 // This tests that a default MediaStream is not created if a remote session
2591 // description is updated to not have any MediaStreams.
2592 TEST_F(PeerConnectionInterfaceTest, VerifyDefaultStreamIsNotCreated) {
2593 FakeConstraints constraints;
2594 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2595 true);
2596 CreatePeerConnection(&constraints);
2597 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2598 rtc::scoped_refptr<StreamCollection> reference(CreateStreamCollection(1, 1));
2599 EXPECT_TRUE(
2600 CompareStreamCollections(observer_.remote_streams(), reference.get()));
2601
2602 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2603 EXPECT_EQ(0u, observer_.remote_streams()->count());
2604 }
2605
2606 // This tests that an RtpSender is created when the local description is set
2607 // after adding a local stream.
2608 // TODO(deadbeef): This test and the one below it need to be updated when
2609 // an RtpSender's lifetime isn't determined by when a local description is set.
2610 TEST_F(PeerConnectionInterfaceTest, LocalDescriptionChanged) {
2611 FakeConstraints constraints;
2612 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2613 true);
2614 CreatePeerConnection(&constraints);
2615
2616 // Create an offer with 1 stream with 2 tracks of each type.
2617 rtc::scoped_refptr<StreamCollection> stream_collection =
2618 CreateStreamCollection(1, 2);
2619 pc_->AddStream(stream_collection->at(0));
2620 std::unique_ptr<SessionDescriptionInterface> offer;
2621 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2622 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
2623
2624 auto senders = pc_->GetSenders();
2625 EXPECT_EQ(4u, senders.size());
2626 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2627 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2628 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[1]));
2629 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[1]));
2630
2631 // Remove an audio and video track.
2632 pc_->RemoveStream(stream_collection->at(0));
2633 stream_collection = CreateStreamCollection(1, 1);
2634 pc_->AddStream(stream_collection->at(0));
2635 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2636 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
2637
2638 senders = pc_->GetSenders();
2639 EXPECT_EQ(2u, senders.size());
2640 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2641 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2642 EXPECT_FALSE(ContainsSender(senders, kAudioTracks[1]));
2643 EXPECT_FALSE(ContainsSender(senders, kVideoTracks[1]));
2644 }
2645
2646 // This tests that an RtpSender is created when the local description is set
2647 // before adding a local stream.
2648 TEST_F(PeerConnectionInterfaceTest,
2649 AddLocalStreamAfterLocalDescriptionChanged) {
2650 FakeConstraints constraints;
2651 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2652 true);
2653 CreatePeerConnection(&constraints);
2654
2655 rtc::scoped_refptr<StreamCollection> stream_collection =
2656 CreateStreamCollection(1, 2);
2657 // Add a stream to create the offer, but remove it afterwards.
2658 pc_->AddStream(stream_collection->at(0));
2659 std::unique_ptr<SessionDescriptionInterface> offer;
2660 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2661 pc_->RemoveStream(stream_collection->at(0));
2662
2663 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
2664 auto senders = pc_->GetSenders();
2665 EXPECT_EQ(0u, senders.size());
2666
2667 pc_->AddStream(stream_collection->at(0));
2668 senders = pc_->GetSenders();
2669 EXPECT_EQ(4u, senders.size());
2670 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2671 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2672 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[1]));
2673 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[1]));
2674 }
2675
2676 // This tests that the expected behavior occurs if the SSRC on a local track is
2677 // changed when SetLocalDescription is called.
2678 TEST_F(PeerConnectionInterfaceTest,
2679 ChangeSsrcOnTrackInLocalSessionDescription) {
2680 FakeConstraints constraints;
2681 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2682 true);
2683 CreatePeerConnection(&constraints);
2684
2685 rtc::scoped_refptr<StreamCollection> stream_collection =
2686 CreateStreamCollection(2, 1);
2687 pc_->AddStream(stream_collection->at(0));
2688 std::unique_ptr<SessionDescriptionInterface> offer;
2689 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2690 // Grab a copy of the offer before it gets passed into the PC.
2691 std::unique_ptr<JsepSessionDescription> modified_offer(
2692 new JsepSessionDescription(JsepSessionDescription::kOffer));
2693 modified_offer->Initialize(offer->description()->Copy(), offer->session_id(),
2694 offer->session_version());
2695 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
2696
2697 auto senders = pc_->GetSenders();
2698 EXPECT_EQ(2u, senders.size());
2699 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2700 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2701
2702 // Change the ssrc of the audio and video track.
2703 cricket::MediaContentDescription* desc =
2704 cricket::GetFirstAudioContentDescription(modified_offer->description());
2705 ASSERT_TRUE(desc != NULL);
2706 for (StreamParams& stream : desc->mutable_streams()) {
2707 for (unsigned int& ssrc : stream.ssrcs) {
2708 ++ssrc;
2709 }
2710 }
2711
2712 desc =
2713 cricket::GetFirstVideoContentDescription(modified_offer->description());
2714 ASSERT_TRUE(desc != NULL);
2715 for (StreamParams& stream : desc->mutable_streams()) {
2716 for (unsigned int& ssrc : stream.ssrcs) {
2717 ++ssrc;
2718 }
2719 }
2720
2721 EXPECT_TRUE(DoSetLocalDescription(modified_offer.release()));
2722 senders = pc_->GetSenders();
2723 EXPECT_EQ(2u, senders.size());
2724 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2725 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2726 // TODO(deadbeef): Once RtpSenders expose parameters, check that the SSRC
2727 // changed.
2728 }
2729
2730 // This tests that the expected behavior occurs if a new session description is
2731 // set with the same tracks, but on a different MediaStream.
2732 TEST_F(PeerConnectionInterfaceTest,
2733 SignalSameTracksInSeparateMediaStream) {
2734 FakeConstraints constraints;
2735 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2736 true);
2737 CreatePeerConnection(&constraints);
2738
2739 rtc::scoped_refptr<StreamCollection> stream_collection =
2740 CreateStreamCollection(2, 1);
2741 pc_->AddStream(stream_collection->at(0));
2742 std::unique_ptr<SessionDescriptionInterface> offer;
2743 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2744 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
2745
2746 auto senders = pc_->GetSenders();
2747 EXPECT_EQ(2u, senders.size());
2748 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0], kStreams[0]));
2749 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0], kStreams[0]));
2750
2751 // Add a new MediaStream but with the same tracks as in the first stream.
2752 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream_1(
2753 webrtc::MediaStream::Create(kStreams[1]));
2754 stream_1->AddTrack(stream_collection->at(0)->GetVideoTracks()[0]);
2755 stream_1->AddTrack(stream_collection->at(0)->GetAudioTracks()[0]);
2756 pc_->AddStream(stream_1);
2757
2758 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2759 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
2760
2761 auto new_senders = pc_->GetSenders();
2762 // Should be the same senders as before, but with updated stream id.
2763 // Note that this behavior is subject to change in the future.
2764 // We may decide the PC should ignore existing tracks in AddStream.
2765 EXPECT_EQ(senders, new_senders);
2766 EXPECT_TRUE(ContainsSender(new_senders, kAudioTracks[0], kStreams[1]));
2767 EXPECT_TRUE(ContainsSender(new_senders, kVideoTracks[0], kStreams[1]));
2768 }
2769
2770 // This tests that PeerConnectionObserver::OnAddTrack is correctly called.
2771 TEST_F(PeerConnectionInterfaceTest, OnAddTrackCallback) {
2772 FakeConstraints constraints;
2773 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2774 true);
2775 CreatePeerConnection(&constraints);
2776 CreateAndSetRemoteOffer(kSdpStringWithStream1AudioTrackOnly);
2777 EXPECT_EQ(observer_.num_added_tracks_, 1);
2778 EXPECT_EQ(observer_.last_added_track_label_, kAudioTracks[0]);
2779
2780 // Create and set the updated remote SDP.
2781 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2782 EXPECT_EQ(observer_.num_added_tracks_, 2);
2783 EXPECT_EQ(observer_.last_added_track_label_, kVideoTracks[0]);
2784 }
2785
2786 // Test that when SetConfiguration is called and the configuration is
2787 // changing, the next offer causes an ICE restart.
2788 TEST_F(PeerConnectionInterfaceTest, SetConfigurationCausingIceRetart) {
2789 PeerConnectionInterface::RTCConfiguration config;
2790 config.type = PeerConnectionInterface::kRelay;
2791 // Need to pass default constraints to prevent disabling of DTLS...
2792 FakeConstraints default_constraints;
2793 CreatePeerConnection(config, &default_constraints);
2794 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
2795
2796 // Do initial offer/answer so there's something to restart.
2797 CreateOfferAsLocalDescription();
2798 CreateAnswerAsRemoteDescription(kSdpStringWithStream1);
2799
2800 // Grab the ufrags.
2801 std::vector<std::string> initial_ufrags = GetUfrags(pc_->local_description());
2802
2803 // Change ICE policy, which should trigger an ICE restart on the next offer.
2804 config.type = PeerConnectionInterface::kAll;
2805 EXPECT_TRUE(pc_->SetConfiguration(config));
2806 CreateOfferAsLocalDescription();
2807
2808 // Grab the new ufrags.
2809 std::vector<std::string> subsequent_ufrags =
2810 GetUfrags(pc_->local_description());
2811
2812 // Sanity check.
2813 EXPECT_EQ(initial_ufrags.size(), subsequent_ufrags.size());
2814 // Check that each ufrag is different.
2815 for (int i = 0; i < static_cast<int>(initial_ufrags.size()); ++i) {
2816 EXPECT_NE(initial_ufrags[i], subsequent_ufrags[i]);
2817 }
2818 }
2819
2820 // Test that when SetConfiguration is called and the configuration *isn't*
2821 // changing, the next offer does *not* cause an ICE restart.
2822 TEST_F(PeerConnectionInterfaceTest, SetConfigurationNotCausingIceRetart) {
2823 PeerConnectionInterface::RTCConfiguration config;
2824 config.type = PeerConnectionInterface::kRelay;
2825 // Need to pass default constraints to prevent disabling of DTLS...
2826 FakeConstraints default_constraints;
2827 CreatePeerConnection(config, &default_constraints);
2828 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
2829
2830 // Do initial offer/answer so there's something to restart.
2831 CreateOfferAsLocalDescription();
2832 CreateAnswerAsRemoteDescription(kSdpStringWithStream1);
2833
2834 // Grab the ufrags.
2835 std::vector<std::string> initial_ufrags = GetUfrags(pc_->local_description());
2836
2837 // Call SetConfiguration with a config identical to what the PC was
2838 // constructed with.
2839 EXPECT_TRUE(pc_->SetConfiguration(config));
2840 CreateOfferAsLocalDescription();
2841
2842 // Grab the new ufrags.
2843 std::vector<std::string> subsequent_ufrags =
2844 GetUfrags(pc_->local_description());
2845
2846 EXPECT_EQ(initial_ufrags, subsequent_ufrags);
2847 }
2848
2849 // Test for a weird corner case scenario:
2850 // 1. Audio/video session established.
2851 // 2. SetConfiguration changes ICE config; ICE restart needed.
2852 // 3. ICE restart initiated by remote peer, but only for one m= section.
2853 // 4. Next createOffer should initiate an ICE restart, but only for the other
2854 // m= section; it would be pointless to do an ICE restart for the m= section
2855 // that was already restarted.
2856 TEST_F(PeerConnectionInterfaceTest, SetConfigurationCausingPartialIceRestart) {
2857 PeerConnectionInterface::RTCConfiguration config;
2858 config.type = PeerConnectionInterface::kRelay;
2859 // Need to pass default constraints to prevent disabling of DTLS...
2860 FakeConstraints default_constraints;
2861 CreatePeerConnection(config, &default_constraints);
2862 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
2863
2864 // Do initial offer/answer so there's something to restart.
2865 CreateOfferAsLocalDescription();
2866 CreateAnswerAsRemoteDescription(kSdpStringWithStream1);
2867
2868 // Change ICE policy, which should set the "needs-ice-restart" flag.
2869 config.type = PeerConnectionInterface::kAll;
2870 EXPECT_TRUE(pc_->SetConfiguration(config));
2871
2872 // Do ICE restart for the first m= section, initiated by remote peer.
2873 webrtc::JsepSessionDescription* remote_offer =
2874 new webrtc::JsepSessionDescription(SessionDescriptionInterface::kOffer);
2875 EXPECT_TRUE(remote_offer->Initialize(kSdpStringWithStream1, nullptr));
2876 remote_offer->description()->transport_infos()[0].description.ice_ufrag =
2877 "modified";
2878 EXPECT_TRUE(DoSetRemoteDescription(remote_offer));
2879 CreateAnswerAsLocalDescription();
2880
2881 // Grab the ufrags.
2882 std::vector<std::string> initial_ufrags = GetUfrags(pc_->local_description());
2883 ASSERT_EQ(2, initial_ufrags.size());
2884
2885 // Create offer and grab the new ufrags.
2886 CreateOfferAsLocalDescription();
2887 std::vector<std::string> subsequent_ufrags =
2888 GetUfrags(pc_->local_description());
2889 ASSERT_EQ(2, subsequent_ufrags.size());
2890
2891 // Ensure that only the ufrag for the second m= section changed.
2892 EXPECT_EQ(initial_ufrags[0], subsequent_ufrags[0]);
2893 EXPECT_NE(initial_ufrags[1], subsequent_ufrags[1]);
2894 }
2895
2896 class PeerConnectionMediaConfigTest : public testing::Test {
2897 protected:
2898 void SetUp() override {
2899 pcf_ = new rtc::RefCountedObject<PeerConnectionFactoryForTest>();
2900 pcf_->Initialize();
2901 }
2902 const cricket::MediaConfig& TestCreatePeerConnection(
2903 const PeerConnectionInterface::RTCConfiguration& config,
2904 const MediaConstraintsInterface *constraints) {
2905 pcf_->create_media_controller_called_ = false;
2906
2907 rtc::scoped_refptr<PeerConnectionInterface> pc(pcf_->CreatePeerConnection(
2908 config, constraints, nullptr, nullptr, &observer_));
2909 EXPECT_TRUE(pc.get());
2910 EXPECT_TRUE(pcf_->create_media_controller_called_);
2911 return pcf_->create_media_controller_config_;
2912 }
2913
2914 rtc::scoped_refptr<PeerConnectionFactoryForTest> pcf_;
2915 MockPeerConnectionObserver observer_;
2916 };
2917
2918 // This test verifies the default behaviour with no constraints and a
2919 // default RTCConfiguration.
2920 TEST_F(PeerConnectionMediaConfigTest, TestDefaults) {
2921 PeerConnectionInterface::RTCConfiguration config;
2922 FakeConstraints constraints;
2923
2924 const cricket::MediaConfig& media_config =
2925 TestCreatePeerConnection(config, &constraints);
2926
2927 EXPECT_FALSE(media_config.enable_dscp);
2928 EXPECT_TRUE(media_config.video.enable_cpu_overuse_detection);
2929 EXPECT_FALSE(media_config.video.disable_prerenderer_smoothing);
2930 EXPECT_FALSE(media_config.video.suspend_below_min_bitrate);
2931 }
2932
2933 // This test verifies the DSCP constraint is recognized and passed to
2934 // the CreateMediaController call.
2935 TEST_F(PeerConnectionMediaConfigTest, TestDscpConstraintTrue) {
2936 PeerConnectionInterface::RTCConfiguration config;
2937 FakeConstraints constraints;
2938
2939 constraints.AddOptional(webrtc::MediaConstraintsInterface::kEnableDscp, true);
2940 const cricket::MediaConfig& media_config =
2941 TestCreatePeerConnection(config, &constraints);
2942
2943 EXPECT_TRUE(media_config.enable_dscp);
2944 }
2945
2946 // This test verifies the cpu overuse detection constraint is
2947 // recognized and passed to the CreateMediaController call.
2948 TEST_F(PeerConnectionMediaConfigTest, TestCpuOveruseConstraintFalse) {
2949 PeerConnectionInterface::RTCConfiguration config;
2950 FakeConstraints constraints;
2951
2952 constraints.AddOptional(
2953 webrtc::MediaConstraintsInterface::kCpuOveruseDetection, false);
2954 const cricket::MediaConfig media_config =
2955 TestCreatePeerConnection(config, &constraints);
2956
2957 EXPECT_FALSE(media_config.video.enable_cpu_overuse_detection);
2958 }
2959
2960 // This test verifies that the disable_prerenderer_smoothing flag is
2961 // propagated from RTCConfiguration to the CreateMediaController call.
2962 TEST_F(PeerConnectionMediaConfigTest, TestDisablePrerendererSmoothingTrue) {
2963 PeerConnectionInterface::RTCConfiguration config;
2964 FakeConstraints constraints;
2965
2966 config.set_prerenderer_smoothing(false);
2967 const cricket::MediaConfig& media_config =
2968 TestCreatePeerConnection(config, &constraints);
2969
2970 EXPECT_TRUE(media_config.video.disable_prerenderer_smoothing);
2971 }
2972
2973 // This test verifies the suspend below min bitrate constraint is
2974 // recognized and passed to the CreateMediaController call.
2975 TEST_F(PeerConnectionMediaConfigTest,
2976 TestSuspendBelowMinBitrateConstraintTrue) {
2977 PeerConnectionInterface::RTCConfiguration config;
2978 FakeConstraints constraints;
2979
2980 constraints.AddOptional(
2981 webrtc::MediaConstraintsInterface::kEnableVideoSuspendBelowMinBitrate,
2982 true);
2983 const cricket::MediaConfig media_config =
2984 TestCreatePeerConnection(config, &constraints);
2985
2986 EXPECT_TRUE(media_config.video.suspend_below_min_bitrate);
2987 }
2988
2989 // The following tests verify that session options are created correctly.
2990 // TODO(deadbeef): Convert these tests to be more end-to-end. Instead of
2991 // "verify options are converted correctly", should be "pass options into
2992 // CreateOffer and verify the correct offer is produced."
2993
2994 TEST(CreateSessionOptionsTest, GetOptionsForOfferWithInvalidAudioOption) {
2995 RTCOfferAnswerOptions rtc_options;
2996 rtc_options.offer_to_receive_audio = RTCOfferAnswerOptions::kUndefined - 1;
2997
2998 cricket::MediaSessionOptions options;
2999 EXPECT_FALSE(ExtractMediaSessionOptions(rtc_options, true, &options));
3000
3001 rtc_options.offer_to_receive_audio =
3002 RTCOfferAnswerOptions::kMaxOfferToReceiveMedia + 1;
3003 EXPECT_FALSE(ExtractMediaSessionOptions(rtc_options, true, &options));
3004 }
3005
3006 TEST(CreateSessionOptionsTest, GetOptionsForOfferWithInvalidVideoOption) {
3007 RTCOfferAnswerOptions rtc_options;
3008 rtc_options.offer_to_receive_video = RTCOfferAnswerOptions::kUndefined - 1;
3009
3010 cricket::MediaSessionOptions options;
3011 EXPECT_FALSE(ExtractMediaSessionOptions(rtc_options, true, &options));
3012
3013 rtc_options.offer_to_receive_video =
3014 RTCOfferAnswerOptions::kMaxOfferToReceiveMedia + 1;
3015 EXPECT_FALSE(ExtractMediaSessionOptions(rtc_options, true, &options));
3016 }
3017
3018 // Test that a MediaSessionOptions is created for an offer if
3019 // OfferToReceiveAudio and OfferToReceiveVideo options are set.
3020 TEST(CreateSessionOptionsTest, GetMediaSessionOptionsForOfferWithAudioVideo) {
3021 RTCOfferAnswerOptions rtc_options;
3022 rtc_options.offer_to_receive_audio = 1;
3023 rtc_options.offer_to_receive_video = 1;
3024
3025 cricket::MediaSessionOptions options;
3026 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
3027 EXPECT_TRUE(options.has_audio());
3028 EXPECT_TRUE(options.has_video());
3029 EXPECT_TRUE(options.bundle_enabled);
3030 }
3031
3032 // Test that a correct MediaSessionOptions is created for an offer if
3033 // OfferToReceiveAudio is set.
3034 TEST(CreateSessionOptionsTest, GetMediaSessionOptionsForOfferWithAudio) {
3035 RTCOfferAnswerOptions rtc_options;
3036 rtc_options.offer_to_receive_audio = 1;
3037
3038 cricket::MediaSessionOptions options;
3039 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
3040 EXPECT_TRUE(options.has_audio());
3041 EXPECT_FALSE(options.has_video());
3042 EXPECT_TRUE(options.bundle_enabled);
3043 }
3044
3045 // Test that a correct MediaSessionOptions is created for an offer if
3046 // the default OfferOptions are used.
3047 TEST(CreateSessionOptionsTest, GetDefaultMediaSessionOptionsForOffer) {
3048 RTCOfferAnswerOptions rtc_options;
3049
3050 cricket::MediaSessionOptions options;
3051 options.transport_options["audio"] = cricket::TransportOptions();
3052 options.transport_options["video"] = cricket::TransportOptions();
3053 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
3054 EXPECT_TRUE(options.has_audio());
3055 EXPECT_FALSE(options.has_video());
3056 EXPECT_TRUE(options.bundle_enabled);
3057 EXPECT_TRUE(options.vad_enabled);
3058 EXPECT_FALSE(options.transport_options["audio"].ice_restart);
3059 EXPECT_FALSE(options.transport_options["video"].ice_restart);
3060 }
3061
3062 // Test that a correct MediaSessionOptions is created for an offer if
3063 // OfferToReceiveVideo is set.
3064 TEST(CreateSessionOptionsTest, GetMediaSessionOptionsForOfferWithVideo) {
3065 RTCOfferAnswerOptions rtc_options;
3066 rtc_options.offer_to_receive_audio = 0;
3067 rtc_options.offer_to_receive_video = 1;
3068
3069 cricket::MediaSessionOptions options;
3070 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
3071 EXPECT_FALSE(options.has_audio());
3072 EXPECT_TRUE(options.has_video());
3073 EXPECT_TRUE(options.bundle_enabled);
3074 }
3075
3076 // Test that a correct MediaSessionOptions is created for an offer if
3077 // UseRtpMux is set to false.
3078 TEST(CreateSessionOptionsTest,
3079 GetMediaSessionOptionsForOfferWithBundleDisabled) {
3080 RTCOfferAnswerOptions rtc_options;
3081 rtc_options.offer_to_receive_audio = 1;
3082 rtc_options.offer_to_receive_video = 1;
3083 rtc_options.use_rtp_mux = false;
3084
3085 cricket::MediaSessionOptions options;
3086 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
3087 EXPECT_TRUE(options.has_audio());
3088 EXPECT_TRUE(options.has_video());
3089 EXPECT_FALSE(options.bundle_enabled);
3090 }
3091
3092 // Test that a correct MediaSessionOptions is created to restart ice if
3093 // IceRestart is set. It also tests that subsequent MediaSessionOptions don't
3094 // have |audio_transport_options.ice_restart| etc. set.
3095 TEST(CreateSessionOptionsTest, GetMediaSessionOptionsForOfferWithIceRestart) {
3096 RTCOfferAnswerOptions rtc_options;
3097 rtc_options.ice_restart = true;
3098
3099 cricket::MediaSessionOptions options;
3100 options.transport_options["audio"] = cricket::TransportOptions();
3101 options.transport_options["video"] = cricket::TransportOptions();
3102 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
3103 EXPECT_TRUE(options.transport_options["audio"].ice_restart);
3104 EXPECT_TRUE(options.transport_options["video"].ice_restart);
3105
3106 rtc_options = RTCOfferAnswerOptions();
3107 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
3108 EXPECT_FALSE(options.transport_options["audio"].ice_restart);
3109 EXPECT_FALSE(options.transport_options["video"].ice_restart);
3110 }
3111
3112 // Test that the MediaConstraints in an answer don't affect if audio and video
3113 // is offered in an offer but that if kOfferToReceiveAudio or
3114 // kOfferToReceiveVideo constraints are true in an offer, the media type will be
3115 // included in subsequent answers.
3116 TEST(CreateSessionOptionsTest, MediaConstraintsInAnswer) {
3117 FakeConstraints answer_c;
3118 answer_c.SetMandatoryReceiveAudio(true);
3119 answer_c.SetMandatoryReceiveVideo(true);
3120
3121 cricket::MediaSessionOptions answer_options;
3122 EXPECT_TRUE(ParseConstraintsForAnswer(&answer_c, &answer_options));
3123 EXPECT_TRUE(answer_options.has_audio());
3124 EXPECT_TRUE(answer_options.has_video());
3125
3126 RTCOfferAnswerOptions rtc_offer_options;
3127
3128 cricket::MediaSessionOptions offer_options;
3129 EXPECT_TRUE(
3130 ExtractMediaSessionOptions(rtc_offer_options, false, &offer_options));
3131 EXPECT_TRUE(offer_options.has_audio());
3132 EXPECT_TRUE(offer_options.has_video());
3133
3134 RTCOfferAnswerOptions updated_rtc_offer_options;
3135 updated_rtc_offer_options.offer_to_receive_audio = 1;
3136 updated_rtc_offer_options.offer_to_receive_video = 1;
3137
3138 cricket::MediaSessionOptions updated_offer_options;
3139 EXPECT_TRUE(ExtractMediaSessionOptions(updated_rtc_offer_options, false,
3140 &updated_offer_options));
3141 EXPECT_TRUE(updated_offer_options.has_audio());
3142 EXPECT_TRUE(updated_offer_options.has_video());
3143
3144 // Since an offer has been created with both audio and video, subsequent
3145 // offers and answers should contain both audio and video.
3146 // Answers will only contain the media types that exist in the offer
3147 // regardless of the value of |updated_answer_options.has_audio| and
3148 // |updated_answer_options.has_video|.
3149 FakeConstraints updated_answer_c;
3150 answer_c.SetMandatoryReceiveAudio(false);
3151 answer_c.SetMandatoryReceiveVideo(false);
3152
3153 cricket::MediaSessionOptions updated_answer_options;
3154 EXPECT_TRUE(
3155 ParseConstraintsForAnswer(&updated_answer_c, &updated_answer_options));
3156 EXPECT_TRUE(updated_answer_options.has_audio());
3157 EXPECT_TRUE(updated_answer_options.has_video());
3158 }
3159
3160 TEST(RtcErrorTest, OstreamOperator) {
3161 std::ostringstream oss;
3162 oss << webrtc::RtcError::NONE << ' '
3163 << webrtc::RtcError::INVALID_PARAMETER << ' '
3164 << webrtc::RtcError::INTERNAL_ERROR;
3165 EXPECT_EQ("NONE INVALID_PARAMETER INTERNAL_ERROR", oss.str());
3166 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698