blob: bee6380bf46a17275bf2bd3fb1c3e2e7ae42497c [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
2 * libjingle
jlmiller@webrtc.org5f93d0a2015-01-20 21:36:13 +00003 * Copyright 2012 Google Inc.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00004 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28#include <string>
29
deadbeefab9b2d12015-10-14 11:33:11 -070030#include "talk/app/webrtc/audiotrack.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000031#include "talk/app/webrtc/fakeportallocatorfactory.h"
32#include "talk/app/webrtc/jsepsessiondescription.h"
deadbeefab9b2d12015-10-14 11:33:11 -070033#include "talk/app/webrtc/mediastream.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000034#include "talk/app/webrtc/mediastreaminterface.h"
deadbeefab9b2d12015-10-14 11:33:11 -070035#include "talk/app/webrtc/peerconnection.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000036#include "talk/app/webrtc/peerconnectioninterface.h"
deadbeefab9b2d12015-10-14 11:33:11 -070037#include "talk/app/webrtc/rtpreceiverinterface.h"
38#include "talk/app/webrtc/rtpsenderinterface.h"
39#include "talk/app/webrtc/streamcollection.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000040#include "talk/app/webrtc/test/fakeconstraints.h"
Henrik Boström5e56c592015-08-11 10:33:13 +020041#include "talk/app/webrtc/test/fakedtlsidentitystore.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000042#include "talk/app/webrtc/test/mockpeerconnectionobservers.h"
43#include "talk/app/webrtc/test/testsdpstrings.h"
wu@webrtc.org967bfff2013-09-19 05:49:50 +000044#include "talk/app/webrtc/videosource.h"
deadbeefab9b2d12015-10-14 11:33:11 -070045#include "talk/app/webrtc/videotrack.h"
buildbot@webrtc.orga09a9992014-08-13 17:26:08 +000046#include "talk/media/base/fakevideocapturer.h"
47#include "talk/media/sctp/sctpdataengine.h"
48#include "talk/session/media/mediasession.h"
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000049#include "webrtc/base/gunit.h"
50#include "webrtc/base/scoped_ptr.h"
51#include "webrtc/base/ssladapter.h"
52#include "webrtc/base/sslstreamadapter.h"
53#include "webrtc/base/stringutils.h"
54#include "webrtc/base/thread.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000055
56static const char kStreamLabel1[] = "local_stream_1";
57static const char kStreamLabel2[] = "local_stream_2";
58static const char kStreamLabel3[] = "local_stream_3";
59static const int kDefaultStunPort = 3478;
60static const char kStunAddressOnly[] = "stun:address";
61static const char kStunInvalidPort[] = "stun:address:-1";
62static const char kStunAddressPortAndMore1[] = "stun:address:port:more";
63static const char kStunAddressPortAndMore2[] = "stun:address:port more";
64static const char kTurnIceServerUri[] = "turn:user@turn.example.org";
65static const char kTurnUsername[] = "user";
66static const char kTurnPassword[] = "password";
67static const char kTurnHostname[] = "turn.example.org";
Peter Boström0c4e06b2015-10-07 12:23:21 +020068static const uint32_t kTimeout = 10000U;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000069
deadbeefab9b2d12015-10-14 11:33:11 -070070static const char kStreams[][8] = {"stream1", "stream2"};
71static const char kAudioTracks[][32] = {"audiotrack0", "audiotrack1"};
72static const char kVideoTracks[][32] = {"videotrack0", "videotrack1"};
73
deadbeef5e97fb52015-10-15 12:49:08 -070074static const char kRecvonly[] = "recvonly";
75static const char kSendrecv[] = "sendrecv";
76
deadbeefab9b2d12015-10-14 11:33:11 -070077// Reference SDP with a MediaStream with label "stream1" and audio track with
78// id "audio_1" and a video track with id "video_1;
79static const char kSdpStringWithStream1[] =
80 "v=0\r\n"
81 "o=- 0 0 IN IP4 127.0.0.1\r\n"
82 "s=-\r\n"
83 "t=0 0\r\n"
84 "a=ice-ufrag:e5785931\r\n"
85 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
86 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
87 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
88 "m=audio 1 RTP/AVPF 103\r\n"
89 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -070090 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -070091 "a=rtpmap:103 ISAC/16000\r\n"
92 "a=ssrc:1 cname:stream1\r\n"
93 "a=ssrc:1 mslabel:stream1\r\n"
94 "a=ssrc:1 label:audiotrack0\r\n"
95 "m=video 1 RTP/AVPF 120\r\n"
96 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -070097 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -070098 "a=rtpmap:120 VP8/90000\r\n"
99 "a=ssrc:2 cname:stream1\r\n"
100 "a=ssrc:2 mslabel:stream1\r\n"
101 "a=ssrc:2 label:videotrack0\r\n";
102
103// Reference SDP with two MediaStreams with label "stream1" and "stream2. Each
104// MediaStreams have one audio track and one video track.
105// This uses MSID.
106static const char kSdpStringWithStream1And2[] =
107 "v=0\r\n"
108 "o=- 0 0 IN IP4 127.0.0.1\r\n"
109 "s=-\r\n"
110 "t=0 0\r\n"
111 "a=ice-ufrag:e5785931\r\n"
112 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
113 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
114 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
115 "a=msid-semantic: WMS stream1 stream2\r\n"
116 "m=audio 1 RTP/AVPF 103\r\n"
117 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700118 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700119 "a=rtpmap:103 ISAC/16000\r\n"
120 "a=ssrc:1 cname:stream1\r\n"
121 "a=ssrc:1 msid:stream1 audiotrack0\r\n"
122 "a=ssrc:3 cname:stream2\r\n"
123 "a=ssrc:3 msid:stream2 audiotrack1\r\n"
124 "m=video 1 RTP/AVPF 120\r\n"
125 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700126 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700127 "a=rtpmap:120 VP8/0\r\n"
128 "a=ssrc:2 cname:stream1\r\n"
129 "a=ssrc:2 msid:stream1 videotrack0\r\n"
130 "a=ssrc:4 cname:stream2\r\n"
131 "a=ssrc:4 msid:stream2 videotrack1\r\n";
132
133// Reference SDP without MediaStreams. Msid is not supported.
134static const char kSdpStringWithoutStreams[] =
135 "v=0\r\n"
136 "o=- 0 0 IN IP4 127.0.0.1\r\n"
137 "s=-\r\n"
138 "t=0 0\r\n"
139 "a=ice-ufrag:e5785931\r\n"
140 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
141 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
142 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
143 "m=audio 1 RTP/AVPF 103\r\n"
144 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700145 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700146 "a=rtpmap:103 ISAC/16000\r\n"
147 "m=video 1 RTP/AVPF 120\r\n"
148 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700149 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700150 "a=rtpmap:120 VP8/90000\r\n";
151
152// Reference SDP without MediaStreams. Msid is supported.
153static const char kSdpStringWithMsidWithoutStreams[] =
154 "v=0\r\n"
155 "o=- 0 0 IN IP4 127.0.0.1\r\n"
156 "s=-\r\n"
157 "t=0 0\r\n"
158 "a=ice-ufrag:e5785931\r\n"
159 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
160 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
161 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
162 "a=msid-semantic: WMS\r\n"
163 "m=audio 1 RTP/AVPF 103\r\n"
164 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700165 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700166 "a=rtpmap:103 ISAC/16000\r\n"
167 "m=video 1 RTP/AVPF 120\r\n"
168 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700169 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700170 "a=rtpmap:120 VP8/90000\r\n";
171
172// Reference SDP without MediaStreams and audio only.
173static const char kSdpStringWithoutStreamsAudioOnly[] =
174 "v=0\r\n"
175 "o=- 0 0 IN IP4 127.0.0.1\r\n"
176 "s=-\r\n"
177 "t=0 0\r\n"
178 "a=ice-ufrag:e5785931\r\n"
179 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
180 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
181 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
182 "m=audio 1 RTP/AVPF 103\r\n"
183 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700184 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700185 "a=rtpmap:103 ISAC/16000\r\n";
186
187// Reference SENDONLY SDP without MediaStreams. Msid is not supported.
188static const char kSdpStringSendOnlyWithoutStreams[] =
189 "v=0\r\n"
190 "o=- 0 0 IN IP4 127.0.0.1\r\n"
191 "s=-\r\n"
192 "t=0 0\r\n"
193 "a=ice-ufrag:e5785931\r\n"
194 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
195 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
196 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
197 "m=audio 1 RTP/AVPF 103\r\n"
198 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700199 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700200 "a=sendonly\r\n"
201 "a=rtpmap:103 ISAC/16000\r\n"
202 "m=video 1 RTP/AVPF 120\r\n"
203 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700204 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700205 "a=sendonly\r\n"
206 "a=rtpmap:120 VP8/90000\r\n";
207
208static const char kSdpStringInit[] =
209 "v=0\r\n"
210 "o=- 0 0 IN IP4 127.0.0.1\r\n"
211 "s=-\r\n"
212 "t=0 0\r\n"
213 "a=ice-ufrag:e5785931\r\n"
214 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
215 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
216 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
217 "a=msid-semantic: WMS\r\n";
218
219static const char kSdpStringAudio[] =
220 "m=audio 1 RTP/AVPF 103\r\n"
221 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700222 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700223 "a=rtpmap:103 ISAC/16000\r\n";
224
225static const char kSdpStringVideo[] =
226 "m=video 1 RTP/AVPF 120\r\n"
227 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700228 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700229 "a=rtpmap:120 VP8/90000\r\n";
230
231static const char kSdpStringMs1Audio0[] =
232 "a=ssrc:1 cname:stream1\r\n"
233 "a=ssrc:1 msid:stream1 audiotrack0\r\n";
234
235static const char kSdpStringMs1Video0[] =
236 "a=ssrc:2 cname:stream1\r\n"
237 "a=ssrc:2 msid:stream1 videotrack0\r\n";
238
239static const char kSdpStringMs1Audio1[] =
240 "a=ssrc:3 cname:stream1\r\n"
241 "a=ssrc:3 msid:stream1 audiotrack1\r\n";
242
243static const char kSdpStringMs1Video1[] =
244 "a=ssrc:4 cname:stream1\r\n"
245 "a=ssrc:4 msid:stream1 videotrack1\r\n";
246
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000247#define MAYBE_SKIP_TEST(feature) \
248 if (!(feature())) { \
249 LOG(LS_INFO) << "Feature disabled... skipping"; \
250 return; \
251 }
252
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000253using rtc::scoped_ptr;
254using rtc::scoped_refptr;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000255using webrtc::AudioSourceInterface;
deadbeefab9b2d12015-10-14 11:33:11 -0700256using webrtc::AudioTrack;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000257using webrtc::AudioTrackInterface;
258using webrtc::DataBuffer;
259using webrtc::DataChannelInterface;
260using webrtc::FakeConstraints;
261using webrtc::FakePortAllocatorFactory;
262using webrtc::IceCandidateInterface;
deadbeefab9b2d12015-10-14 11:33:11 -0700263using webrtc::MediaStream;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000264using webrtc::MediaStreamInterface;
265using webrtc::MediaStreamTrackInterface;
266using webrtc::MockCreateSessionDescriptionObserver;
267using webrtc::MockDataChannelObserver;
268using webrtc::MockSetSessionDescriptionObserver;
269using webrtc::MockStatsObserver;
270using webrtc::PeerConnectionInterface;
271using webrtc::PeerConnectionObserver;
272using webrtc::PortAllocatorFactoryInterface;
deadbeefab9b2d12015-10-14 11:33:11 -0700273using webrtc::RtpReceiverInterface;
274using webrtc::RtpSenderInterface;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000275using webrtc::SdpParseError;
276using webrtc::SessionDescriptionInterface;
deadbeefab9b2d12015-10-14 11:33:11 -0700277using webrtc::StreamCollection;
278using webrtc::StreamCollectionInterface;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000279using webrtc::VideoSourceInterface;
deadbeefab9b2d12015-10-14 11:33:11 -0700280using webrtc::VideoTrack;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000281using webrtc::VideoTrackInterface;
282
deadbeefab9b2d12015-10-14 11:33:11 -0700283typedef PeerConnectionInterface::RTCOfferAnswerOptions RTCOfferAnswerOptions;
284
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000285namespace {
286
287// Gets the first ssrc of given content type from the ContentInfo.
288bool GetFirstSsrc(const cricket::ContentInfo* content_info, int* ssrc) {
289 if (!content_info || !ssrc) {
290 return false;
291 }
292 const cricket::MediaContentDescription* media_desc =
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000293 static_cast<const cricket::MediaContentDescription*>(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000294 content_info->description);
295 if (!media_desc || media_desc->streams().empty()) {
296 return false;
297 }
298 *ssrc = media_desc->streams().begin()->first_ssrc();
299 return true;
300}
301
302void SetSsrcToZero(std::string* sdp) {
303 const char kSdpSsrcAtribute[] = "a=ssrc:";
304 const char kSdpSsrcAtributeZero[] = "a=ssrc:0";
305 size_t ssrc_pos = 0;
306 while ((ssrc_pos = sdp->find(kSdpSsrcAtribute, ssrc_pos)) !=
307 std::string::npos) {
308 size_t end_ssrc = sdp->find(" ", ssrc_pos);
309 sdp->replace(ssrc_pos, end_ssrc - ssrc_pos, kSdpSsrcAtributeZero);
310 ssrc_pos = end_ssrc;
311 }
312}
313
deadbeefab9b2d12015-10-14 11:33:11 -0700314// Check if |streams| contains the specified track.
315bool ContainsTrack(const std::vector<cricket::StreamParams>& streams,
316 const std::string& stream_label,
317 const std::string& track_id) {
318 for (const cricket::StreamParams& params : streams) {
319 if (params.sync_label == stream_label && params.id == track_id) {
320 return true;
321 }
322 }
323 return false;
324}
325
326// Check if |senders| contains the specified sender, by id.
327bool ContainsSender(
328 const std::vector<rtc::scoped_refptr<RtpSenderInterface>>& senders,
329 const std::string& id) {
330 for (const auto& sender : senders) {
331 if (sender->id() == id) {
332 return true;
333 }
334 }
335 return false;
336}
337
338// Create a collection of streams.
339// CreateStreamCollection(1) creates a collection that
340// correspond to kSdpStringWithStream1.
341// CreateStreamCollection(2) correspond to kSdpStringWithStream1And2.
342rtc::scoped_refptr<StreamCollection> CreateStreamCollection(
343 int number_of_streams) {
344 rtc::scoped_refptr<StreamCollection> local_collection(
345 StreamCollection::Create());
346
347 for (int i = 0; i < number_of_streams; ++i) {
348 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream(
349 webrtc::MediaStream::Create(kStreams[i]));
350
351 // Add a local audio track.
352 rtc::scoped_refptr<webrtc::AudioTrackInterface> audio_track(
353 webrtc::AudioTrack::Create(kAudioTracks[i], nullptr));
354 stream->AddTrack(audio_track);
355
356 // Add a local video track.
357 rtc::scoped_refptr<webrtc::VideoTrackInterface> video_track(
358 webrtc::VideoTrack::Create(kVideoTracks[i], nullptr));
359 stream->AddTrack(video_track);
360
361 local_collection->AddStream(stream);
362 }
363 return local_collection;
364}
365
366// Check equality of StreamCollections.
367bool CompareStreamCollections(StreamCollectionInterface* s1,
368 StreamCollectionInterface* s2) {
369 if (s1 == nullptr || s2 == nullptr || s1->count() != s2->count()) {
370 return false;
371 }
372
373 for (size_t i = 0; i != s1->count(); ++i) {
374 if (s1->at(i)->label() != s2->at(i)->label()) {
375 return false;
376 }
377 webrtc::AudioTrackVector audio_tracks1 = s1->at(i)->GetAudioTracks();
378 webrtc::AudioTrackVector audio_tracks2 = s2->at(i)->GetAudioTracks();
379 webrtc::VideoTrackVector video_tracks1 = s1->at(i)->GetVideoTracks();
380 webrtc::VideoTrackVector video_tracks2 = s2->at(i)->GetVideoTracks();
381
382 if (audio_tracks1.size() != audio_tracks2.size()) {
383 return false;
384 }
385 for (size_t j = 0; j != audio_tracks1.size(); ++j) {
386 if (audio_tracks1[j]->id() != audio_tracks2[j]->id()) {
387 return false;
388 }
389 }
390 if (video_tracks1.size() != video_tracks2.size()) {
391 return false;
392 }
393 for (size_t j = 0; j != video_tracks1.size(); ++j) {
394 if (video_tracks1[j]->id() != video_tracks2[j]->id()) {
395 return false;
396 }
397 }
398 }
399 return true;
400}
401
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000402class MockPeerConnectionObserver : public PeerConnectionObserver {
403 public:
deadbeefab9b2d12015-10-14 11:33:11 -0700404 MockPeerConnectionObserver() : remote_streams_(StreamCollection::Create()) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000405 ~MockPeerConnectionObserver() {
406 }
407 void SetPeerConnectionInterface(PeerConnectionInterface* pc) {
408 pc_ = pc;
409 if (pc) {
410 state_ = pc_->signaling_state();
411 }
412 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000413 virtual void OnSignalingChange(
414 PeerConnectionInterface::SignalingState new_state) {
415 EXPECT_EQ(pc_->signaling_state(), new_state);
416 state_ = new_state;
417 }
418 // TODO(bemasc): Remove this once callers transition to OnIceGatheringChange.
419 virtual void OnStateChange(StateType state_changed) {
420 if (pc_.get() == NULL)
421 return;
422 switch (state_changed) {
423 case kSignalingState:
424 // OnSignalingChange and OnStateChange(kSignalingState) should always
425 // be called approximately simultaneously. To ease testing, we require
426 // that they always be called in that order. This check verifies
427 // that OnSignalingChange has just been called.
428 EXPECT_EQ(pc_->signaling_state(), state_);
429 break;
430 case kIceState:
431 ADD_FAILURE();
432 break;
433 default:
434 ADD_FAILURE();
435 break;
436 }
437 }
deadbeefab9b2d12015-10-14 11:33:11 -0700438
439 MediaStreamInterface* RemoteStream(const std::string& label) {
440 return remote_streams_->find(label);
441 }
442 StreamCollectionInterface* remote_streams() const { return remote_streams_; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000443 virtual void OnAddStream(MediaStreamInterface* stream) {
444 last_added_stream_ = stream;
deadbeefab9b2d12015-10-14 11:33:11 -0700445 remote_streams_->AddStream(stream);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000446 }
447 virtual void OnRemoveStream(MediaStreamInterface* stream) {
448 last_removed_stream_ = stream;
deadbeefab9b2d12015-10-14 11:33:11 -0700449 remote_streams_->RemoveStream(stream);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000450 }
451 virtual void OnRenegotiationNeeded() {
452 renegotiation_needed_ = true;
453 }
454 virtual void OnDataChannel(DataChannelInterface* data_channel) {
455 last_datachannel_ = data_channel;
456 }
457
458 virtual void OnIceConnectionChange(
459 PeerConnectionInterface::IceConnectionState new_state) {
460 EXPECT_EQ(pc_->ice_connection_state(), new_state);
461 }
462 virtual void OnIceGatheringChange(
463 PeerConnectionInterface::IceGatheringState new_state) {
464 EXPECT_EQ(pc_->ice_gathering_state(), new_state);
465 }
466 virtual void OnIceCandidate(const webrtc::IceCandidateInterface* candidate) {
467 EXPECT_NE(PeerConnectionInterface::kIceGatheringNew,
468 pc_->ice_gathering_state());
469
470 std::string sdp;
471 EXPECT_TRUE(candidate->ToString(&sdp));
472 EXPECT_LT(0u, sdp.size());
473 last_candidate_.reset(webrtc::CreateIceCandidate(candidate->sdp_mid(),
474 candidate->sdp_mline_index(), sdp, NULL));
475 EXPECT_TRUE(last_candidate_.get() != NULL);
476 }
477 // TODO(bemasc): Remove this once callers transition to OnSignalingChange.
478 virtual void OnIceComplete() {
479 ice_complete_ = true;
480 // OnIceGatheringChange(IceGatheringCompleted) and OnIceComplete() should
481 // be called approximately simultaneously. For ease of testing, this
482 // check additionally requires that they be called in the above order.
483 EXPECT_EQ(PeerConnectionInterface::kIceGatheringComplete,
484 pc_->ice_gathering_state());
485 }
486
487 // Returns the label of the last added stream.
488 // Empty string if no stream have been added.
489 std::string GetLastAddedStreamLabel() {
490 if (last_added_stream_.get())
491 return last_added_stream_->label();
492 return "";
493 }
494 std::string GetLastRemovedStreamLabel() {
495 if (last_removed_stream_.get())
496 return last_removed_stream_->label();
497 return "";
498 }
499
500 scoped_refptr<PeerConnectionInterface> pc_;
501 PeerConnectionInterface::SignalingState state_;
502 scoped_ptr<IceCandidateInterface> last_candidate_;
503 scoped_refptr<DataChannelInterface> last_datachannel_;
deadbeefab9b2d12015-10-14 11:33:11 -0700504 rtc::scoped_refptr<StreamCollection> remote_streams_;
505 bool renegotiation_needed_ = false;
506 bool ice_complete_ = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000507
508 private:
509 scoped_refptr<MediaStreamInterface> last_added_stream_;
510 scoped_refptr<MediaStreamInterface> last_removed_stream_;
511};
512
513} // namespace
deadbeefab9b2d12015-10-14 11:33:11 -0700514
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000515class PeerConnectionInterfaceTest : public testing::Test {
516 protected:
517 virtual void SetUp() {
518 pc_factory_ = webrtc::CreatePeerConnectionFactory(
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000519 rtc::Thread::Current(), rtc::Thread::Current(), NULL, NULL,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000520 NULL);
521 ASSERT_TRUE(pc_factory_.get() != NULL);
522 }
523
524 void CreatePeerConnection() {
525 CreatePeerConnection("", "", NULL);
526 }
527
528 void CreatePeerConnection(webrtc::MediaConstraintsInterface* constraints) {
529 CreatePeerConnection("", "", constraints);
530 }
531
532 void CreatePeerConnection(const std::string& uri,
533 const std::string& password,
534 webrtc::MediaConstraintsInterface* constraints) {
535 PeerConnectionInterface::IceServer server;
536 PeerConnectionInterface::IceServers servers;
deadbeef0a6c4ca2015-10-06 11:38:28 -0700537 if (!uri.empty()) {
538 server.uri = uri;
539 server.password = password;
540 servers.push_back(server);
541 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000542
543 port_allocator_factory_ = FakePortAllocatorFactory::Create();
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000544
buildbot@webrtc.org61c1b8e2014-04-09 06:06:38 +0000545 // DTLS does not work in a loopback call, so is disabled for most of the
546 // tests in this file. We only create a FakeIdentityService if the test
547 // explicitly sets the constraint.
jiayl@webrtc.org61e00b02015-03-04 22:17:38 +0000548 FakeConstraints default_constraints;
549 if (!constraints) {
550 constraints = &default_constraints;
551
552 default_constraints.AddMandatory(
553 webrtc::MediaConstraintsInterface::kEnableDtlsSrtp, false);
554 }
555
Henrik Boström5e56c592015-08-11 10:33:13 +0200556 scoped_ptr<webrtc::DtlsIdentityStoreInterface> dtls_identity_store;
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000557 bool dtls;
558 if (FindConstraint(constraints,
559 webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
560 &dtls,
Henrik Boström5e56c592015-08-11 10:33:13 +0200561 nullptr) && dtls) {
562 dtls_identity_store.reset(new FakeDtlsIdentityStore());
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000563 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000564 pc_ = pc_factory_->CreatePeerConnection(servers, constraints,
565 port_allocator_factory_.get(),
Henrik Boström5e56c592015-08-11 10:33:13 +0200566 dtls_identity_store.Pass(),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000567 &observer_);
568 ASSERT_TRUE(pc_.get() != NULL);
569 observer_.SetPeerConnectionInterface(pc_.get());
570 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
571 }
572
deadbeef0a6c4ca2015-10-06 11:38:28 -0700573 void CreatePeerConnectionExpectFail(const std::string& uri) {
574 PeerConnectionInterface::IceServer server;
575 PeerConnectionInterface::IceServers servers;
576 server.uri = uri;
577 servers.push_back(server);
578
579 scoped_ptr<webrtc::DtlsIdentityStoreInterface> dtls_identity_store;
580 port_allocator_factory_ = FakePortAllocatorFactory::Create();
581 scoped_refptr<PeerConnectionInterface> pc;
582 pc = pc_factory_->CreatePeerConnection(
583 servers, nullptr, port_allocator_factory_.get(),
584 dtls_identity_store.Pass(), &observer_);
585 ASSERT_EQ(nullptr, pc);
586 }
587
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000588 void CreatePeerConnectionWithDifferentConfigurations() {
589 CreatePeerConnection(kStunAddressOnly, "", NULL);
590 EXPECT_EQ(1u, port_allocator_factory_->stun_configs().size());
591 EXPECT_EQ(0u, port_allocator_factory_->turn_configs().size());
592 EXPECT_EQ("address",
593 port_allocator_factory_->stun_configs()[0].server.hostname());
594 EXPECT_EQ(kDefaultStunPort,
595 port_allocator_factory_->stun_configs()[0].server.port());
596
deadbeef0a6c4ca2015-10-06 11:38:28 -0700597 CreatePeerConnectionExpectFail(kStunInvalidPort);
598 CreatePeerConnectionExpectFail(kStunAddressPortAndMore1);
599 CreatePeerConnectionExpectFail(kStunAddressPortAndMore2);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000600
601 CreatePeerConnection(kTurnIceServerUri, kTurnPassword, NULL);
buildbot@webrtc.orgf875f152014-04-14 16:06:21 +0000602 EXPECT_EQ(0u, port_allocator_factory_->stun_configs().size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000603 EXPECT_EQ(1u, port_allocator_factory_->turn_configs().size());
604 EXPECT_EQ(kTurnUsername,
605 port_allocator_factory_->turn_configs()[0].username);
606 EXPECT_EQ(kTurnPassword,
607 port_allocator_factory_->turn_configs()[0].password);
608 EXPECT_EQ(kTurnHostname,
609 port_allocator_factory_->turn_configs()[0].server.hostname());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000610 }
611
612 void ReleasePeerConnection() {
613 pc_ = NULL;
614 observer_.SetPeerConnectionInterface(NULL);
615 }
616
deadbeefab9b2d12015-10-14 11:33:11 -0700617 void AddVideoStream(const std::string& label) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000618 // Create a local stream.
619 scoped_refptr<MediaStreamInterface> stream(
620 pc_factory_->CreateLocalMediaStream(label));
621 scoped_refptr<VideoSourceInterface> video_source(
622 pc_factory_->CreateVideoSource(new cricket::FakeVideoCapturer(), NULL));
623 scoped_refptr<VideoTrackInterface> video_track(
624 pc_factory_->CreateVideoTrack(label + "v0", video_source));
625 stream->AddTrack(video_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +0000626 EXPECT_TRUE(pc_->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000627 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
628 observer_.renegotiation_needed_ = false;
629 }
630
631 void AddVoiceStream(const std::string& label) {
632 // Create a local stream.
633 scoped_refptr<MediaStreamInterface> stream(
634 pc_factory_->CreateLocalMediaStream(label));
635 scoped_refptr<AudioTrackInterface> audio_track(
636 pc_factory_->CreateAudioTrack(label + "a0", NULL));
637 stream->AddTrack(audio_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +0000638 EXPECT_TRUE(pc_->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000639 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
640 observer_.renegotiation_needed_ = false;
641 }
642
643 void AddAudioVideoStream(const std::string& stream_label,
644 const std::string& audio_track_label,
645 const std::string& video_track_label) {
646 // Create a local stream.
647 scoped_refptr<MediaStreamInterface> stream(
648 pc_factory_->CreateLocalMediaStream(stream_label));
649 scoped_refptr<AudioTrackInterface> audio_track(
650 pc_factory_->CreateAudioTrack(
651 audio_track_label, static_cast<AudioSourceInterface*>(NULL)));
652 stream->AddTrack(audio_track.get());
653 scoped_refptr<VideoTrackInterface> video_track(
654 pc_factory_->CreateVideoTrack(video_track_label, NULL));
655 stream->AddTrack(video_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +0000656 EXPECT_TRUE(pc_->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000657 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
658 observer_.renegotiation_needed_ = false;
659 }
660
661 bool DoCreateOfferAnswer(SessionDescriptionInterface** desc, bool offer) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000662 rtc::scoped_refptr<MockCreateSessionDescriptionObserver>
663 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000664 MockCreateSessionDescriptionObserver>());
665 if (offer) {
666 pc_->CreateOffer(observer, NULL);
667 } else {
668 pc_->CreateAnswer(observer, NULL);
669 }
670 EXPECT_EQ_WAIT(true, observer->called(), kTimeout);
671 *desc = observer->release_desc();
672 return observer->result();
673 }
674
675 bool DoCreateOffer(SessionDescriptionInterface** desc) {
676 return DoCreateOfferAnswer(desc, true);
677 }
678
679 bool DoCreateAnswer(SessionDescriptionInterface** desc) {
680 return DoCreateOfferAnswer(desc, false);
681 }
682
683 bool DoSetSessionDescription(SessionDescriptionInterface* desc, bool local) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000684 rtc::scoped_refptr<MockSetSessionDescriptionObserver>
685 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000686 MockSetSessionDescriptionObserver>());
687 if (local) {
688 pc_->SetLocalDescription(observer, desc);
689 } else {
690 pc_->SetRemoteDescription(observer, desc);
691 }
692 EXPECT_EQ_WAIT(true, observer->called(), kTimeout);
693 return observer->result();
694 }
695
696 bool DoSetLocalDescription(SessionDescriptionInterface* desc) {
697 return DoSetSessionDescription(desc, true);
698 }
699
700 bool DoSetRemoteDescription(SessionDescriptionInterface* desc) {
701 return DoSetSessionDescription(desc, false);
702 }
703
704 // Calls PeerConnection::GetStats and check the return value.
705 // It does not verify the values in the StatReports since a RTCP packet might
706 // be required.
707 bool DoGetStats(MediaStreamTrackInterface* track) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000708 rtc::scoped_refptr<MockStatsObserver> observer(
709 new rtc::RefCountedObject<MockStatsObserver>());
jiayl@webrtc.orgdb41b4d2014-03-03 21:30:06 +0000710 if (!pc_->GetStats(
711 observer, track, PeerConnectionInterface::kStatsOutputLevelStandard))
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000712 return false;
713 EXPECT_TRUE_WAIT(observer->called(), kTimeout);
714 return observer->called();
715 }
716
717 void InitiateCall() {
718 CreatePeerConnection();
719 // Create a local stream with audio&video tracks.
720 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
721 CreateOfferReceiveAnswer();
722 }
723
724 // Verify that RTP Header extensions has been negotiated for audio and video.
725 void VerifyRemoteRtpHeaderExtensions() {
726 const cricket::MediaContentDescription* desc =
727 cricket::GetFirstAudioContentDescription(
728 pc_->remote_description()->description());
729 ASSERT_TRUE(desc != NULL);
730 EXPECT_GT(desc->rtp_header_extensions().size(), 0u);
731
732 desc = cricket::GetFirstVideoContentDescription(
733 pc_->remote_description()->description());
734 ASSERT_TRUE(desc != NULL);
735 EXPECT_GT(desc->rtp_header_extensions().size(), 0u);
736 }
737
738 void CreateOfferAsRemoteDescription() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000739 rtc::scoped_ptr<SessionDescriptionInterface> offer;
pkasting@chromium.org005b6ff2015-01-30 19:41:42 +0000740 ASSERT_TRUE(DoCreateOffer(offer.use()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000741 std::string sdp;
742 EXPECT_TRUE(offer->ToString(&sdp));
743 SessionDescriptionInterface* remote_offer =
744 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
745 sdp, NULL);
746 EXPECT_TRUE(DoSetRemoteDescription(remote_offer));
747 EXPECT_EQ(PeerConnectionInterface::kHaveRemoteOffer, observer_.state_);
748 }
749
deadbeefab9b2d12015-10-14 11:33:11 -0700750 void CreateAndSetRemoteOffer(const std::string& sdp) {
751 SessionDescriptionInterface* remote_offer =
752 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
753 sdp, nullptr);
754 EXPECT_TRUE(DoSetRemoteDescription(remote_offer));
755 EXPECT_EQ(PeerConnectionInterface::kHaveRemoteOffer, observer_.state_);
756 }
757
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000758 void CreateAnswerAsLocalDescription() {
759 scoped_ptr<SessionDescriptionInterface> answer;
pkasting@chromium.org005b6ff2015-01-30 19:41:42 +0000760 ASSERT_TRUE(DoCreateAnswer(answer.use()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000761
762 // TODO(perkj): Currently SetLocalDescription fails if any parameters in an
763 // audio codec change, even if the parameter has nothing to do with
764 // receiving. Not all parameters are serialized to SDP.
765 // Since CreatePrAnswerAsLocalDescription serialize/deserialize
766 // the SessionDescription, it is necessary to do that here to in order to
767 // get ReceiveOfferCreatePrAnswerAndAnswer and RenegotiateAudioOnly to pass.
768 // https://code.google.com/p/webrtc/issues/detail?id=1356
769 std::string sdp;
770 EXPECT_TRUE(answer->ToString(&sdp));
771 SessionDescriptionInterface* new_answer =
772 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
773 sdp, NULL);
774 EXPECT_TRUE(DoSetLocalDescription(new_answer));
775 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
776 }
777
778 void CreatePrAnswerAsLocalDescription() {
779 scoped_ptr<SessionDescriptionInterface> answer;
pkasting@chromium.org005b6ff2015-01-30 19:41:42 +0000780 ASSERT_TRUE(DoCreateAnswer(answer.use()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000781
782 std::string sdp;
783 EXPECT_TRUE(answer->ToString(&sdp));
784 SessionDescriptionInterface* pr_answer =
785 webrtc::CreateSessionDescription(SessionDescriptionInterface::kPrAnswer,
786 sdp, NULL);
787 EXPECT_TRUE(DoSetLocalDescription(pr_answer));
788 EXPECT_EQ(PeerConnectionInterface::kHaveLocalPrAnswer, observer_.state_);
789 }
790
791 void CreateOfferReceiveAnswer() {
792 CreateOfferAsLocalDescription();
793 std::string sdp;
794 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
795 CreateAnswerAsRemoteDescription(sdp);
796 }
797
798 void CreateOfferAsLocalDescription() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000799 rtc::scoped_ptr<SessionDescriptionInterface> offer;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000800 ASSERT_TRUE(DoCreateOffer(offer.use()));
801 // TODO(perkj): Currently SetLocalDescription fails if any parameters in an
802 // audio codec change, even if the parameter has nothing to do with
803 // receiving. Not all parameters are serialized to SDP.
804 // Since CreatePrAnswerAsLocalDescription serialize/deserialize
805 // the SessionDescription, it is necessary to do that here to in order to
806 // get ReceiveOfferCreatePrAnswerAndAnswer and RenegotiateAudioOnly to pass.
807 // https://code.google.com/p/webrtc/issues/detail?id=1356
808 std::string sdp;
809 EXPECT_TRUE(offer->ToString(&sdp));
810 SessionDescriptionInterface* new_offer =
811 webrtc::CreateSessionDescription(
812 SessionDescriptionInterface::kOffer,
813 sdp, NULL);
814
815 EXPECT_TRUE(DoSetLocalDescription(new_offer));
816 EXPECT_EQ(PeerConnectionInterface::kHaveLocalOffer, observer_.state_);
mallinath@webrtc.org68cbd012014-01-22 00:16:46 +0000817 // Wait for the ice_complete message, so that SDP will have candidates.
818 EXPECT_TRUE_WAIT(observer_.ice_complete_, kTimeout);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000819 }
820
deadbeefab9b2d12015-10-14 11:33:11 -0700821 void CreateAnswerAsRemoteDescription(const std::string& sdp) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000822 webrtc::JsepSessionDescription* answer = new webrtc::JsepSessionDescription(
823 SessionDescriptionInterface::kAnswer);
deadbeefab9b2d12015-10-14 11:33:11 -0700824 EXPECT_TRUE(answer->Initialize(sdp, NULL));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000825 EXPECT_TRUE(DoSetRemoteDescription(answer));
826 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
827 }
828
deadbeefab9b2d12015-10-14 11:33:11 -0700829 void CreatePrAnswerAndAnswerAsRemoteDescription(const std::string& sdp) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000830 webrtc::JsepSessionDescription* pr_answer =
831 new webrtc::JsepSessionDescription(
832 SessionDescriptionInterface::kPrAnswer);
deadbeefab9b2d12015-10-14 11:33:11 -0700833 EXPECT_TRUE(pr_answer->Initialize(sdp, NULL));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000834 EXPECT_TRUE(DoSetRemoteDescription(pr_answer));
835 EXPECT_EQ(PeerConnectionInterface::kHaveRemotePrAnswer, observer_.state_);
836 webrtc::JsepSessionDescription* answer =
837 new webrtc::JsepSessionDescription(
838 SessionDescriptionInterface::kAnswer);
deadbeefab9b2d12015-10-14 11:33:11 -0700839 EXPECT_TRUE(answer->Initialize(sdp, NULL));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000840 EXPECT_TRUE(DoSetRemoteDescription(answer));
841 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
842 }
843
844 // Help function used for waiting until a the last signaled remote stream has
845 // the same label as |stream_label|. In a few of the tests in this file we
846 // answer with the same session description as we offer and thus we can
847 // check if OnAddStream have been called with the same stream as we offer to
848 // send.
849 void WaitAndVerifyOnAddStream(const std::string& stream_label) {
850 EXPECT_EQ_WAIT(stream_label, observer_.GetLastAddedStreamLabel(), kTimeout);
851 }
852
853 // Creates an offer and applies it as a local session description.
854 // Creates an answer with the same SDP an the offer but removes all lines
855 // that start with a:ssrc"
856 void CreateOfferReceiveAnswerWithoutSsrc() {
857 CreateOfferAsLocalDescription();
858 std::string sdp;
859 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
860 SetSsrcToZero(&sdp);
861 CreateAnswerAsRemoteDescription(sdp);
862 }
863
deadbeefab9b2d12015-10-14 11:33:11 -0700864 // This function creates a MediaStream with label kStreams[0] and
865 // |number_of_audio_tracks| and |number_of_video_tracks| tracks and the
866 // corresponding SessionDescriptionInterface. The SessionDescriptionInterface
867 // is returned in |desc| and the MediaStream is stored in
868 // |reference_collection_|
869 void CreateSessionDescriptionAndReference(
870 size_t number_of_audio_tracks,
871 size_t number_of_video_tracks,
872 SessionDescriptionInterface** desc) {
873 ASSERT_TRUE(desc != nullptr);
874 ASSERT_LE(number_of_audio_tracks, 2u);
875 ASSERT_LE(number_of_video_tracks, 2u);
876
877 reference_collection_ = StreamCollection::Create();
878 std::string sdp_ms1 = std::string(kSdpStringInit);
879
880 std::string mediastream_label = kStreams[0];
881
882 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream(
883 webrtc::MediaStream::Create(mediastream_label));
884 reference_collection_->AddStream(stream);
885
886 if (number_of_audio_tracks > 0) {
887 sdp_ms1 += std::string(kSdpStringAudio);
888 sdp_ms1 += std::string(kSdpStringMs1Audio0);
889 AddAudioTrack(kAudioTracks[0], stream);
890 }
891 if (number_of_audio_tracks > 1) {
892 sdp_ms1 += kSdpStringMs1Audio1;
893 AddAudioTrack(kAudioTracks[1], stream);
894 }
895
896 if (number_of_video_tracks > 0) {
897 sdp_ms1 += std::string(kSdpStringVideo);
898 sdp_ms1 += std::string(kSdpStringMs1Video0);
899 AddVideoTrack(kVideoTracks[0], stream);
900 }
901 if (number_of_video_tracks > 1) {
902 sdp_ms1 += kSdpStringMs1Video1;
903 AddVideoTrack(kVideoTracks[1], stream);
904 }
905
906 *desc = webrtc::CreateSessionDescription(
907 SessionDescriptionInterface::kOffer, sdp_ms1, nullptr);
908 }
909
910 void AddAudioTrack(const std::string& track_id,
911 MediaStreamInterface* stream) {
912 rtc::scoped_refptr<webrtc::AudioTrackInterface> audio_track(
913 webrtc::AudioTrack::Create(track_id, nullptr));
914 ASSERT_TRUE(stream->AddTrack(audio_track));
915 }
916
917 void AddVideoTrack(const std::string& track_id,
918 MediaStreamInterface* stream) {
919 rtc::scoped_refptr<webrtc::VideoTrackInterface> video_track(
920 webrtc::VideoTrack::Create(track_id, nullptr));
921 ASSERT_TRUE(stream->AddTrack(video_track));
922 }
923
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000924 scoped_refptr<FakePortAllocatorFactory> port_allocator_factory_;
925 scoped_refptr<webrtc::PeerConnectionFactoryInterface> pc_factory_;
926 scoped_refptr<PeerConnectionInterface> pc_;
927 MockPeerConnectionObserver observer_;
deadbeefab9b2d12015-10-14 11:33:11 -0700928 rtc::scoped_refptr<StreamCollection> reference_collection_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000929};
930
931TEST_F(PeerConnectionInterfaceTest,
932 CreatePeerConnectionWithDifferentConfigurations) {
933 CreatePeerConnectionWithDifferentConfigurations();
934}
935
936TEST_F(PeerConnectionInterfaceTest, AddStreams) {
937 CreatePeerConnection();
deadbeefab9b2d12015-10-14 11:33:11 -0700938 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000939 AddVoiceStream(kStreamLabel2);
940 ASSERT_EQ(2u, pc_->local_streams()->count());
941
wu@webrtc.org9dba5252013-08-05 20:36:57 +0000942 // Test we can add multiple local streams to one peerconnection.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000943 scoped_refptr<MediaStreamInterface> stream(
944 pc_factory_->CreateLocalMediaStream(kStreamLabel3));
945 scoped_refptr<AudioTrackInterface> audio_track(
946 pc_factory_->CreateAudioTrack(
947 kStreamLabel3, static_cast<AudioSourceInterface*>(NULL)));
948 stream->AddTrack(audio_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +0000949 EXPECT_TRUE(pc_->AddStream(stream));
wu@webrtc.org9dba5252013-08-05 20:36:57 +0000950 EXPECT_EQ(3u, pc_->local_streams()->count());
951
952 // Remove the third stream.
953 pc_->RemoveStream(pc_->local_streams()->at(2));
954 EXPECT_EQ(2u, pc_->local_streams()->count());
955
956 // Remove the second stream.
957 pc_->RemoveStream(pc_->local_streams()->at(1));
958 EXPECT_EQ(1u, pc_->local_streams()->count());
959
960 // Remove the first stream.
961 pc_->RemoveStream(pc_->local_streams()->at(0));
962 EXPECT_EQ(0u, pc_->local_streams()->count());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000963}
964
deadbeefab9b2d12015-10-14 11:33:11 -0700965// Test that the created offer includes streams we added.
966TEST_F(PeerConnectionInterfaceTest, AddedStreamsPresentInOffer) {
967 CreatePeerConnection();
968 AddAudioVideoStream(kStreamLabel1, "audio_track", "video_track");
969 scoped_ptr<SessionDescriptionInterface> offer;
970 ASSERT_TRUE(DoCreateOffer(offer.accept()));
971
972 const cricket::ContentInfo* audio_content =
973 cricket::GetFirstAudioContent(offer->description());
974 const cricket::AudioContentDescription* audio_desc =
975 static_cast<const cricket::AudioContentDescription*>(
976 audio_content->description);
977 EXPECT_TRUE(
978 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
979
980 const cricket::ContentInfo* video_content =
981 cricket::GetFirstVideoContent(offer->description());
982 const cricket::VideoContentDescription* video_desc =
983 static_cast<const cricket::VideoContentDescription*>(
984 video_content->description);
985 EXPECT_TRUE(
986 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
987
988 // Add another stream and ensure the offer includes both the old and new
989 // streams.
990 AddAudioVideoStream(kStreamLabel2, "audio_track2", "video_track2");
991 ASSERT_TRUE(DoCreateOffer(offer.accept()));
992
993 audio_content = cricket::GetFirstAudioContent(offer->description());
994 audio_desc = static_cast<const cricket::AudioContentDescription*>(
995 audio_content->description);
996 EXPECT_TRUE(
997 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
998 EXPECT_TRUE(
999 ContainsTrack(audio_desc->streams(), kStreamLabel2, "audio_track2"));
1000
1001 video_content = cricket::GetFirstVideoContent(offer->description());
1002 video_desc = static_cast<const cricket::VideoContentDescription*>(
1003 video_content->description);
1004 EXPECT_TRUE(
1005 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1006 EXPECT_TRUE(
1007 ContainsTrack(video_desc->streams(), kStreamLabel2, "video_track2"));
1008}
1009
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001010TEST_F(PeerConnectionInterfaceTest, RemoveStream) {
1011 CreatePeerConnection();
deadbeefab9b2d12015-10-14 11:33:11 -07001012 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001013 ASSERT_EQ(1u, pc_->local_streams()->count());
1014 pc_->RemoveStream(pc_->local_streams()->at(0));
1015 EXPECT_EQ(0u, pc_->local_streams()->count());
1016}
1017
1018TEST_F(PeerConnectionInterfaceTest, CreateOfferReceiveAnswer) {
1019 InitiateCall();
1020 WaitAndVerifyOnAddStream(kStreamLabel1);
1021 VerifyRemoteRtpHeaderExtensions();
1022}
1023
1024TEST_F(PeerConnectionInterfaceTest, CreateOfferReceivePrAnswerAndAnswer) {
1025 CreatePeerConnection();
deadbeefab9b2d12015-10-14 11:33:11 -07001026 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001027 CreateOfferAsLocalDescription();
1028 std::string offer;
1029 EXPECT_TRUE(pc_->local_description()->ToString(&offer));
1030 CreatePrAnswerAndAnswerAsRemoteDescription(offer);
1031 WaitAndVerifyOnAddStream(kStreamLabel1);
1032}
1033
1034TEST_F(PeerConnectionInterfaceTest, ReceiveOfferCreateAnswer) {
1035 CreatePeerConnection();
deadbeefab9b2d12015-10-14 11:33:11 -07001036 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001037
1038 CreateOfferAsRemoteDescription();
1039 CreateAnswerAsLocalDescription();
1040
1041 WaitAndVerifyOnAddStream(kStreamLabel1);
1042}
1043
1044TEST_F(PeerConnectionInterfaceTest, ReceiveOfferCreatePrAnswerAndAnswer) {
1045 CreatePeerConnection();
deadbeefab9b2d12015-10-14 11:33:11 -07001046 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001047
1048 CreateOfferAsRemoteDescription();
1049 CreatePrAnswerAsLocalDescription();
1050 CreateAnswerAsLocalDescription();
1051
1052 WaitAndVerifyOnAddStream(kStreamLabel1);
1053}
1054
1055TEST_F(PeerConnectionInterfaceTest, Renegotiate) {
1056 InitiateCall();
1057 ASSERT_EQ(1u, pc_->remote_streams()->count());
1058 pc_->RemoveStream(pc_->local_streams()->at(0));
1059 CreateOfferReceiveAnswer();
1060 EXPECT_EQ(0u, pc_->remote_streams()->count());
deadbeefab9b2d12015-10-14 11:33:11 -07001061 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001062 CreateOfferReceiveAnswer();
1063}
1064
1065// Tests that after negotiating an audio only call, the respondent can perform a
1066// renegotiation that removes the audio stream.
1067TEST_F(PeerConnectionInterfaceTest, RenegotiateAudioOnly) {
1068 CreatePeerConnection();
1069 AddVoiceStream(kStreamLabel1);
1070 CreateOfferAsRemoteDescription();
1071 CreateAnswerAsLocalDescription();
1072
1073 ASSERT_EQ(1u, pc_->remote_streams()->count());
1074 pc_->RemoveStream(pc_->local_streams()->at(0));
1075 CreateOfferReceiveAnswer();
1076 EXPECT_EQ(0u, pc_->remote_streams()->count());
1077}
1078
1079// Test that candidates are generated and that we can parse our own candidates.
1080TEST_F(PeerConnectionInterfaceTest, IceCandidates) {
1081 CreatePeerConnection();
1082
1083 EXPECT_FALSE(pc_->AddIceCandidate(observer_.last_candidate_.get()));
1084 // SetRemoteDescription takes ownership of offer.
1085 SessionDescriptionInterface* offer = NULL;
deadbeefab9b2d12015-10-14 11:33:11 -07001086 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001087 EXPECT_TRUE(DoCreateOffer(&offer));
1088 EXPECT_TRUE(DoSetRemoteDescription(offer));
1089
1090 // SetLocalDescription takes ownership of answer.
1091 SessionDescriptionInterface* answer = NULL;
1092 EXPECT_TRUE(DoCreateAnswer(&answer));
1093 EXPECT_TRUE(DoSetLocalDescription(answer));
1094
1095 EXPECT_TRUE_WAIT(observer_.last_candidate_.get() != NULL, kTimeout);
1096 EXPECT_TRUE_WAIT(observer_.ice_complete_, kTimeout);
1097
1098 EXPECT_TRUE(pc_->AddIceCandidate(observer_.last_candidate_.get()));
1099}
1100
deadbeefab9b2d12015-10-14 11:33:11 -07001101// Test that CreateOffer and CreateAnswer will fail if the track labels are
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001102// not unique.
1103TEST_F(PeerConnectionInterfaceTest, CreateOfferAnswerWithInvalidStream) {
1104 CreatePeerConnection();
1105 // Create a regular offer for the CreateAnswer test later.
1106 SessionDescriptionInterface* offer = NULL;
1107 EXPECT_TRUE(DoCreateOffer(&offer));
1108 EXPECT_TRUE(offer != NULL);
1109 delete offer;
1110 offer = NULL;
1111
1112 // Create a local stream with audio&video tracks having same label.
1113 AddAudioVideoStream(kStreamLabel1, "track_label", "track_label");
1114
1115 // Test CreateOffer
1116 EXPECT_FALSE(DoCreateOffer(&offer));
1117
1118 // Test CreateAnswer
1119 SessionDescriptionInterface* answer = NULL;
1120 EXPECT_FALSE(DoCreateAnswer(&answer));
1121}
1122
1123// Test that we will get different SSRCs for each tracks in the offer and answer
1124// we created.
1125TEST_F(PeerConnectionInterfaceTest, SsrcInOfferAnswer) {
1126 CreatePeerConnection();
1127 // Create a local stream with audio&video tracks having different labels.
1128 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
1129
1130 // Test CreateOffer
1131 scoped_ptr<SessionDescriptionInterface> offer;
pkasting@chromium.org005b6ff2015-01-30 19:41:42 +00001132 ASSERT_TRUE(DoCreateOffer(offer.use()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001133 int audio_ssrc = 0;
1134 int video_ssrc = 0;
1135 EXPECT_TRUE(GetFirstSsrc(GetFirstAudioContent(offer->description()),
1136 &audio_ssrc));
1137 EXPECT_TRUE(GetFirstSsrc(GetFirstVideoContent(offer->description()),
1138 &video_ssrc));
1139 EXPECT_NE(audio_ssrc, video_ssrc);
1140
1141 // Test CreateAnswer
1142 EXPECT_TRUE(DoSetRemoteDescription(offer.release()));
1143 scoped_ptr<SessionDescriptionInterface> answer;
pkasting@chromium.org005b6ff2015-01-30 19:41:42 +00001144 ASSERT_TRUE(DoCreateAnswer(answer.use()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001145 audio_ssrc = 0;
1146 video_ssrc = 0;
1147 EXPECT_TRUE(GetFirstSsrc(GetFirstAudioContent(answer->description()),
1148 &audio_ssrc));
1149 EXPECT_TRUE(GetFirstSsrc(GetFirstVideoContent(answer->description()),
1150 &video_ssrc));
1151 EXPECT_NE(audio_ssrc, video_ssrc);
1152}
1153
1154// Test that we can specify a certain track that we want statistics about.
1155TEST_F(PeerConnectionInterfaceTest, GetStatsForSpecificTrack) {
1156 InitiateCall();
1157 ASSERT_LT(0u, pc_->remote_streams()->count());
1158 ASSERT_LT(0u, pc_->remote_streams()->at(0)->GetAudioTracks().size());
1159 scoped_refptr<MediaStreamTrackInterface> remote_audio =
1160 pc_->remote_streams()->at(0)->GetAudioTracks()[0];
1161 EXPECT_TRUE(DoGetStats(remote_audio));
1162
1163 // Remove the stream. Since we are sending to our selves the local
1164 // and the remote stream is the same.
1165 pc_->RemoveStream(pc_->local_streams()->at(0));
1166 // Do a re-negotiation.
1167 CreateOfferReceiveAnswer();
1168
1169 ASSERT_EQ(0u, pc_->remote_streams()->count());
1170
1171 // Test that we still can get statistics for the old track. Even if it is not
1172 // sent any longer.
1173 EXPECT_TRUE(DoGetStats(remote_audio));
1174}
1175
1176// Test that we can get stats on a video track.
1177TEST_F(PeerConnectionInterfaceTest, GetStatsForVideoTrack) {
1178 InitiateCall();
1179 ASSERT_LT(0u, pc_->remote_streams()->count());
1180 ASSERT_LT(0u, pc_->remote_streams()->at(0)->GetVideoTracks().size());
1181 scoped_refptr<MediaStreamTrackInterface> remote_video =
1182 pc_->remote_streams()->at(0)->GetVideoTracks()[0];
1183 EXPECT_TRUE(DoGetStats(remote_video));
1184}
1185
1186// Test that we don't get statistics for an invalid track.
tommi@webrtc.org908f57e2014-07-21 11:44:39 +00001187// TODO(tommi): Fix this test. DoGetStats will return true
1188// for the unknown track (since GetStats is async), but no
1189// data is returned for the track.
1190TEST_F(PeerConnectionInterfaceTest, DISABLED_GetStatsForInvalidTrack) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001191 InitiateCall();
1192 scoped_refptr<AudioTrackInterface> unknown_audio_track(
1193 pc_factory_->CreateAudioTrack("unknown track", NULL));
1194 EXPECT_FALSE(DoGetStats(unknown_audio_track));
1195}
1196
1197// This test setup two RTP data channels in loop back.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001198TEST_F(PeerConnectionInterfaceTest, TestDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001199 FakeConstraints constraints;
1200 constraints.SetAllowRtpDataChannels();
1201 CreatePeerConnection(&constraints);
1202 scoped_refptr<DataChannelInterface> data1 =
1203 pc_->CreateDataChannel("test1", NULL);
1204 scoped_refptr<DataChannelInterface> data2 =
1205 pc_->CreateDataChannel("test2", NULL);
1206 ASSERT_TRUE(data1 != NULL);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001207 rtc::scoped_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001208 new MockDataChannelObserver(data1));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001209 rtc::scoped_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001210 new MockDataChannelObserver(data2));
1211
1212 EXPECT_EQ(DataChannelInterface::kConnecting, data1->state());
1213 EXPECT_EQ(DataChannelInterface::kConnecting, data2->state());
1214 std::string data_to_send1 = "testing testing";
1215 std::string data_to_send2 = "testing something else";
1216 EXPECT_FALSE(data1->Send(DataBuffer(data_to_send1)));
1217
1218 CreateOfferReceiveAnswer();
1219 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1220 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
1221
1222 EXPECT_EQ(DataChannelInterface::kOpen, data1->state());
1223 EXPECT_EQ(DataChannelInterface::kOpen, data2->state());
1224 EXPECT_TRUE(data1->Send(DataBuffer(data_to_send1)));
1225 EXPECT_TRUE(data2->Send(DataBuffer(data_to_send2)));
1226
1227 EXPECT_EQ_WAIT(data_to_send1, observer1->last_message(), kTimeout);
1228 EXPECT_EQ_WAIT(data_to_send2, observer2->last_message(), kTimeout);
1229
1230 data1->Close();
1231 EXPECT_EQ(DataChannelInterface::kClosing, data1->state());
1232 CreateOfferReceiveAnswer();
1233 EXPECT_FALSE(observer1->IsOpen());
1234 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
1235 EXPECT_TRUE(observer2->IsOpen());
1236
1237 data_to_send2 = "testing something else again";
1238 EXPECT_TRUE(data2->Send(DataBuffer(data_to_send2)));
1239
1240 EXPECT_EQ_WAIT(data_to_send2, observer2->last_message(), kTimeout);
1241}
1242
1243// This test verifies that sendnig binary data over RTP data channels should
1244// fail.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001245TEST_F(PeerConnectionInterfaceTest, TestSendBinaryOnRtpDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001246 FakeConstraints constraints;
1247 constraints.SetAllowRtpDataChannels();
1248 CreatePeerConnection(&constraints);
1249 scoped_refptr<DataChannelInterface> data1 =
1250 pc_->CreateDataChannel("test1", NULL);
1251 scoped_refptr<DataChannelInterface> data2 =
1252 pc_->CreateDataChannel("test2", NULL);
1253 ASSERT_TRUE(data1 != NULL);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001254 rtc::scoped_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001255 new MockDataChannelObserver(data1));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001256 rtc::scoped_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001257 new MockDataChannelObserver(data2));
1258
1259 EXPECT_EQ(DataChannelInterface::kConnecting, data1->state());
1260 EXPECT_EQ(DataChannelInterface::kConnecting, data2->state());
1261
1262 CreateOfferReceiveAnswer();
1263 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1264 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
1265
1266 EXPECT_EQ(DataChannelInterface::kOpen, data1->state());
1267 EXPECT_EQ(DataChannelInterface::kOpen, data2->state());
1268
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001269 rtc::Buffer buffer("test", 4);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001270 EXPECT_FALSE(data1->Send(DataBuffer(buffer, true)));
1271}
1272
1273// This test setup a RTP data channels in loop back and test that a channel is
1274// opened even if the remote end answer with a zero SSRC.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001275TEST_F(PeerConnectionInterfaceTest, TestSendOnlyDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001276 FakeConstraints constraints;
1277 constraints.SetAllowRtpDataChannels();
1278 CreatePeerConnection(&constraints);
1279 scoped_refptr<DataChannelInterface> data1 =
1280 pc_->CreateDataChannel("test1", NULL);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001281 rtc::scoped_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001282 new MockDataChannelObserver(data1));
1283
1284 CreateOfferReceiveAnswerWithoutSsrc();
1285
1286 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1287
1288 data1->Close();
1289 EXPECT_EQ(DataChannelInterface::kClosing, data1->state());
1290 CreateOfferReceiveAnswerWithoutSsrc();
1291 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
1292 EXPECT_FALSE(observer1->IsOpen());
1293}
1294
1295// This test that if a data channel is added in an answer a receive only channel
1296// channel is created.
1297TEST_F(PeerConnectionInterfaceTest, TestReceiveOnlyDataChannel) {
1298 FakeConstraints constraints;
1299 constraints.SetAllowRtpDataChannels();
1300 CreatePeerConnection(&constraints);
1301
1302 std::string offer_label = "offer_channel";
1303 scoped_refptr<DataChannelInterface> offer_channel =
1304 pc_->CreateDataChannel(offer_label, NULL);
1305
1306 CreateOfferAsLocalDescription();
1307
1308 // Replace the data channel label in the offer and apply it as an answer.
1309 std::string receive_label = "answer_channel";
1310 std::string sdp;
1311 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001312 rtc::replace_substrs(offer_label.c_str(), offer_label.length(),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001313 receive_label.c_str(), receive_label.length(),
1314 &sdp);
1315 CreateAnswerAsRemoteDescription(sdp);
1316
1317 // Verify that a new incoming data channel has been created and that
1318 // it is open but can't we written to.
1319 ASSERT_TRUE(observer_.last_datachannel_ != NULL);
1320 DataChannelInterface* received_channel = observer_.last_datachannel_;
1321 EXPECT_EQ(DataChannelInterface::kConnecting, received_channel->state());
1322 EXPECT_EQ(receive_label, received_channel->label());
1323 EXPECT_FALSE(received_channel->Send(DataBuffer("something")));
1324
1325 // Verify that the channel we initially offered has been rejected.
1326 EXPECT_EQ(DataChannelInterface::kClosed, offer_channel->state());
1327
1328 // Do another offer / answer exchange and verify that the data channel is
1329 // opened.
1330 CreateOfferReceiveAnswer();
1331 EXPECT_EQ_WAIT(DataChannelInterface::kOpen, received_channel->state(),
1332 kTimeout);
1333}
1334
1335// This test that no data channel is returned if a reliable channel is
1336// requested.
1337// TODO(perkj): Remove this test once reliable channels are implemented.
1338TEST_F(PeerConnectionInterfaceTest, CreateReliableRtpDataChannelShouldFail) {
1339 FakeConstraints constraints;
1340 constraints.SetAllowRtpDataChannels();
1341 CreatePeerConnection(&constraints);
1342
1343 std::string label = "test";
1344 webrtc::DataChannelInit config;
1345 config.reliable = true;
1346 scoped_refptr<DataChannelInterface> channel =
1347 pc_->CreateDataChannel(label, &config);
1348 EXPECT_TRUE(channel == NULL);
1349}
1350
deadbeefab9b2d12015-10-14 11:33:11 -07001351// Verifies that duplicated label is not allowed for RTP data channel.
1352TEST_F(PeerConnectionInterfaceTest, RtpDuplicatedLabelNotAllowed) {
1353 FakeConstraints constraints;
1354 constraints.SetAllowRtpDataChannels();
1355 CreatePeerConnection(&constraints);
1356
1357 std::string label = "test";
1358 scoped_refptr<DataChannelInterface> channel =
1359 pc_->CreateDataChannel(label, nullptr);
1360 EXPECT_NE(channel, nullptr);
1361
1362 scoped_refptr<DataChannelInterface> dup_channel =
1363 pc_->CreateDataChannel(label, nullptr);
1364 EXPECT_EQ(dup_channel, nullptr);
1365}
1366
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001367// This tests that a SCTP data channel is returned using different
1368// DataChannelInit configurations.
1369TEST_F(PeerConnectionInterfaceTest, CreateSctpDataChannel) {
1370 FakeConstraints constraints;
1371 constraints.SetAllowDtlsSctpDataChannels();
1372 CreatePeerConnection(&constraints);
1373
1374 webrtc::DataChannelInit config;
1375
1376 scoped_refptr<DataChannelInterface> channel =
1377 pc_->CreateDataChannel("1", &config);
1378 EXPECT_TRUE(channel != NULL);
1379 EXPECT_TRUE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001380 EXPECT_TRUE(observer_.renegotiation_needed_);
1381 observer_.renegotiation_needed_ = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001382
1383 config.ordered = false;
1384 channel = pc_->CreateDataChannel("2", &config);
1385 EXPECT_TRUE(channel != NULL);
1386 EXPECT_TRUE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001387 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001388
1389 config.ordered = true;
1390 config.maxRetransmits = 0;
1391 channel = pc_->CreateDataChannel("3", &config);
1392 EXPECT_TRUE(channel != NULL);
1393 EXPECT_FALSE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001394 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001395
1396 config.maxRetransmits = -1;
1397 config.maxRetransmitTime = 0;
1398 channel = pc_->CreateDataChannel("4", &config);
1399 EXPECT_TRUE(channel != NULL);
1400 EXPECT_FALSE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001401 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001402}
1403
1404// This tests that no data channel is returned if both maxRetransmits and
1405// maxRetransmitTime are set for SCTP data channels.
1406TEST_F(PeerConnectionInterfaceTest,
1407 CreateSctpDataChannelShouldFailForInvalidConfig) {
1408 FakeConstraints constraints;
1409 constraints.SetAllowDtlsSctpDataChannels();
1410 CreatePeerConnection(&constraints);
1411
1412 std::string label = "test";
1413 webrtc::DataChannelInit config;
1414 config.maxRetransmits = 0;
1415 config.maxRetransmitTime = 0;
1416
1417 scoped_refptr<DataChannelInterface> channel =
1418 pc_->CreateDataChannel(label, &config);
1419 EXPECT_TRUE(channel == NULL);
1420}
1421
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001422// The test verifies that creating a SCTP data channel with an id already in use
1423// or out of range should fail.
1424TEST_F(PeerConnectionInterfaceTest,
1425 CreateSctpDataChannelWithInvalidIdShouldFail) {
1426 FakeConstraints constraints;
1427 constraints.SetAllowDtlsSctpDataChannels();
1428 CreatePeerConnection(&constraints);
1429
1430 webrtc::DataChannelInit config;
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001431 scoped_refptr<DataChannelInterface> channel;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001432
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001433 config.id = 1;
1434 channel = pc_->CreateDataChannel("1", &config);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001435 EXPECT_TRUE(channel != NULL);
1436 EXPECT_EQ(1, channel->id());
1437
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001438 channel = pc_->CreateDataChannel("x", &config);
1439 EXPECT_TRUE(channel == NULL);
1440
1441 config.id = cricket::kMaxSctpSid;
1442 channel = pc_->CreateDataChannel("max", &config);
1443 EXPECT_TRUE(channel != NULL);
1444 EXPECT_EQ(config.id, channel->id());
1445
1446 config.id = cricket::kMaxSctpSid + 1;
1447 channel = pc_->CreateDataChannel("x", &config);
1448 EXPECT_TRUE(channel == NULL);
1449}
1450
deadbeefab9b2d12015-10-14 11:33:11 -07001451// Verifies that duplicated label is allowed for SCTP data channel.
1452TEST_F(PeerConnectionInterfaceTest, SctpDuplicatedLabelAllowed) {
1453 FakeConstraints constraints;
1454 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
1455 true);
1456 CreatePeerConnection(&constraints);
1457
1458 std::string label = "test";
1459 scoped_refptr<DataChannelInterface> channel =
1460 pc_->CreateDataChannel(label, nullptr);
1461 EXPECT_NE(channel, nullptr);
1462
1463 scoped_refptr<DataChannelInterface> dup_channel =
1464 pc_->CreateDataChannel(label, nullptr);
1465 EXPECT_NE(dup_channel, nullptr);
1466}
1467
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001468// This test verifies that OnRenegotiationNeeded is fired for every new RTP
1469// DataChannel.
1470TEST_F(PeerConnectionInterfaceTest, RenegotiationNeededForNewRtpDataChannel) {
1471 FakeConstraints constraints;
1472 constraints.SetAllowRtpDataChannels();
1473 CreatePeerConnection(&constraints);
1474
1475 scoped_refptr<DataChannelInterface> dc1 =
1476 pc_->CreateDataChannel("test1", NULL);
1477 EXPECT_TRUE(observer_.renegotiation_needed_);
1478 observer_.renegotiation_needed_ = false;
1479
1480 scoped_refptr<DataChannelInterface> dc2 =
1481 pc_->CreateDataChannel("test2", NULL);
1482 EXPECT_TRUE(observer_.renegotiation_needed_);
1483}
1484
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001485// This test that a data channel closes when a PeerConnection is deleted/closed.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001486TEST_F(PeerConnectionInterfaceTest, DataChannelCloseWhenPeerConnectionClose) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001487 FakeConstraints constraints;
1488 constraints.SetAllowRtpDataChannels();
1489 CreatePeerConnection(&constraints);
1490
1491 scoped_refptr<DataChannelInterface> data1 =
1492 pc_->CreateDataChannel("test1", NULL);
1493 scoped_refptr<DataChannelInterface> data2 =
1494 pc_->CreateDataChannel("test2", NULL);
1495 ASSERT_TRUE(data1 != NULL);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001496 rtc::scoped_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001497 new MockDataChannelObserver(data1));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001498 rtc::scoped_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001499 new MockDataChannelObserver(data2));
1500
1501 CreateOfferReceiveAnswer();
1502 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1503 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
1504
1505 ReleasePeerConnection();
1506 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
1507 EXPECT_EQ(DataChannelInterface::kClosed, data2->state());
1508}
1509
1510// This test that data channels can be rejected in an answer.
1511TEST_F(PeerConnectionInterfaceTest, TestRejectDataChannelInAnswer) {
1512 FakeConstraints constraints;
1513 constraints.SetAllowRtpDataChannels();
1514 CreatePeerConnection(&constraints);
1515
1516 scoped_refptr<DataChannelInterface> offer_channel(
1517 pc_->CreateDataChannel("offer_channel", NULL));
1518
1519 CreateOfferAsLocalDescription();
1520
1521 // Create an answer where the m-line for data channels are rejected.
1522 std::string sdp;
1523 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
1524 webrtc::JsepSessionDescription* answer = new webrtc::JsepSessionDescription(
1525 SessionDescriptionInterface::kAnswer);
1526 EXPECT_TRUE(answer->Initialize(sdp, NULL));
1527 cricket::ContentInfo* data_info =
1528 answer->description()->GetContentByName("data");
1529 data_info->rejected = true;
1530
1531 DoSetRemoteDescription(answer);
1532 EXPECT_EQ(DataChannelInterface::kClosed, offer_channel->state());
1533}
1534
1535// Test that we can create a session description from an SDP string from
1536// FireFox, use it as a remote session description, generate an answer and use
1537// the answer as a local description.
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00001538TEST_F(PeerConnectionInterfaceTest, ReceiveFireFoxOffer) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001539 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001540 FakeConstraints constraints;
1541 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
1542 true);
1543 CreatePeerConnection(&constraints);
1544 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
1545 SessionDescriptionInterface* desc =
1546 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
jbauchfabe2c92015-07-16 13:43:14 -07001547 webrtc::kFireFoxSdpOffer, nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001548 EXPECT_TRUE(DoSetSessionDescription(desc, false));
1549 CreateAnswerAsLocalDescription();
1550 ASSERT_TRUE(pc_->local_description() != NULL);
1551 ASSERT_TRUE(pc_->remote_description() != NULL);
1552
1553 const cricket::ContentInfo* content =
1554 cricket::GetFirstAudioContent(pc_->local_description()->description());
1555 ASSERT_TRUE(content != NULL);
1556 EXPECT_FALSE(content->rejected);
1557
1558 content =
1559 cricket::GetFirstVideoContent(pc_->local_description()->description());
1560 ASSERT_TRUE(content != NULL);
1561 EXPECT_FALSE(content->rejected);
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00001562#ifdef HAVE_SCTP
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001563 content =
1564 cricket::GetFirstDataContent(pc_->local_description()->description());
1565 ASSERT_TRUE(content != NULL);
1566 EXPECT_TRUE(content->rejected);
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00001567#endif
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001568}
1569
1570// Test that we can create an audio only offer and receive an answer with a
1571// limited set of audio codecs and receive an updated offer with more audio
1572// codecs, where the added codecs are not supported.
1573TEST_F(PeerConnectionInterfaceTest, ReceiveUpdatedAudioOfferWithBadCodecs) {
1574 CreatePeerConnection();
1575 AddVoiceStream("audio_label");
1576 CreateOfferAsLocalDescription();
1577
1578 SessionDescriptionInterface* answer =
1579 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
jbauchfabe2c92015-07-16 13:43:14 -07001580 webrtc::kAudioSdp, nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001581 EXPECT_TRUE(DoSetSessionDescription(answer, false));
1582
1583 SessionDescriptionInterface* updated_offer =
1584 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
jbauchfabe2c92015-07-16 13:43:14 -07001585 webrtc::kAudioSdpWithUnsupportedCodecs,
1586 nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001587 EXPECT_TRUE(DoSetSessionDescription(updated_offer, false));
1588 CreateAnswerAsLocalDescription();
1589}
1590
1591// Test that PeerConnection::Close changes the states to closed and all remote
1592// tracks change state to ended.
1593TEST_F(PeerConnectionInterfaceTest, CloseAndTestStreamsAndStates) {
1594 // Initialize a PeerConnection and negotiate local and remote session
1595 // description.
1596 InitiateCall();
1597 ASSERT_EQ(1u, pc_->local_streams()->count());
1598 ASSERT_EQ(1u, pc_->remote_streams()->count());
1599
1600 pc_->Close();
1601
1602 EXPECT_EQ(PeerConnectionInterface::kClosed, pc_->signaling_state());
1603 EXPECT_EQ(PeerConnectionInterface::kIceConnectionClosed,
1604 pc_->ice_connection_state());
1605 EXPECT_EQ(PeerConnectionInterface::kIceGatheringComplete,
1606 pc_->ice_gathering_state());
1607
1608 EXPECT_EQ(1u, pc_->local_streams()->count());
1609 EXPECT_EQ(1u, pc_->remote_streams()->count());
1610
1611 scoped_refptr<MediaStreamInterface> remote_stream =
1612 pc_->remote_streams()->at(0);
1613 EXPECT_EQ(MediaStreamTrackInterface::kEnded,
1614 remote_stream->GetVideoTracks()[0]->state());
1615 EXPECT_EQ(MediaStreamTrackInterface::kEnded,
1616 remote_stream->GetAudioTracks()[0]->state());
1617}
1618
1619// Test that PeerConnection methods fails gracefully after
1620// PeerConnection::Close has been called.
1621TEST_F(PeerConnectionInterfaceTest, CloseAndTestMethods) {
1622 CreatePeerConnection();
1623 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
1624 CreateOfferAsRemoteDescription();
1625 CreateAnswerAsLocalDescription();
1626
1627 ASSERT_EQ(1u, pc_->local_streams()->count());
1628 scoped_refptr<MediaStreamInterface> local_stream =
1629 pc_->local_streams()->at(0);
1630
1631 pc_->Close();
1632
1633 pc_->RemoveStream(local_stream);
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +00001634 EXPECT_FALSE(pc_->AddStream(local_stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001635
1636 ASSERT_FALSE(local_stream->GetAudioTracks().empty());
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001637 rtc::scoped_refptr<webrtc::DtmfSenderInterface> dtmf_sender(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001638 pc_->CreateDtmfSender(local_stream->GetAudioTracks()[0]));
wu@webrtc.org66037362013-08-13 00:09:35 +00001639 EXPECT_TRUE(NULL == dtmf_sender); // local stream has been removed.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001640
1641 EXPECT_TRUE(pc_->CreateDataChannel("test", NULL) == NULL);
1642
1643 EXPECT_TRUE(pc_->local_description() != NULL);
1644 EXPECT_TRUE(pc_->remote_description() != NULL);
1645
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001646 rtc::scoped_ptr<SessionDescriptionInterface> offer;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001647 EXPECT_TRUE(DoCreateOffer(offer.use()));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001648 rtc::scoped_ptr<SessionDescriptionInterface> answer;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001649 EXPECT_TRUE(DoCreateAnswer(answer.use()));
1650
1651 std::string sdp;
1652 ASSERT_TRUE(pc_->remote_description()->ToString(&sdp));
1653 SessionDescriptionInterface* remote_offer =
1654 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
1655 sdp, NULL);
1656 EXPECT_FALSE(DoSetRemoteDescription(remote_offer));
1657
1658 ASSERT_TRUE(pc_->local_description()->ToString(&sdp));
1659 SessionDescriptionInterface* local_offer =
1660 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
1661 sdp, NULL);
1662 EXPECT_FALSE(DoSetLocalDescription(local_offer));
1663}
1664
1665// Test that GetStats can still be called after PeerConnection::Close.
1666TEST_F(PeerConnectionInterfaceTest, CloseAndGetStats) {
1667 InitiateCall();
1668 pc_->Close();
1669 DoGetStats(NULL);
1670}
deadbeefab9b2d12015-10-14 11:33:11 -07001671
1672// NOTE: The series of tests below come from what used to be
1673// mediastreamsignaling_unittest.cc, and are mostly aimed at testing that
1674// setting a remote or local description has the expected effects.
1675
1676// This test verifies that the remote MediaStreams corresponding to a received
1677// SDP string is created. In this test the two separate MediaStreams are
1678// signaled.
1679TEST_F(PeerConnectionInterfaceTest, UpdateRemoteStreams) {
1680 FakeConstraints constraints;
1681 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
1682 true);
1683 CreatePeerConnection(&constraints);
1684 CreateAndSetRemoteOffer(kSdpStringWithStream1);
1685
1686 rtc::scoped_refptr<StreamCollection> reference(CreateStreamCollection(1));
1687 EXPECT_TRUE(
1688 CompareStreamCollections(observer_.remote_streams(), reference.get()));
1689 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
1690 EXPECT_TRUE(remote_stream->GetVideoTracks()[0]->GetSource() != nullptr);
1691
1692 // Create a session description based on another SDP with another
1693 // MediaStream.
1694 CreateAndSetRemoteOffer(kSdpStringWithStream1And2);
1695
1696 rtc::scoped_refptr<StreamCollection> reference2(CreateStreamCollection(2));
1697 EXPECT_TRUE(
1698 CompareStreamCollections(observer_.remote_streams(), reference2.get()));
1699}
1700
1701// This test verifies that when remote tracks are added/removed from SDP, the
1702// created remote streams are updated appropriately.
1703TEST_F(PeerConnectionInterfaceTest,
1704 AddRemoveTrackFromExistingRemoteMediaStream) {
1705 FakeConstraints constraints;
1706 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
1707 true);
1708 CreatePeerConnection(&constraints);
1709 rtc::scoped_ptr<SessionDescriptionInterface> desc_ms1;
1710 CreateSessionDescriptionAndReference(1, 1, desc_ms1.accept());
1711 EXPECT_TRUE(DoSetRemoteDescription(desc_ms1.release()));
1712 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
1713 reference_collection_));
1714
1715 // Add extra audio and video tracks to the same MediaStream.
1716 rtc::scoped_ptr<SessionDescriptionInterface> desc_ms1_two_tracks;
1717 CreateSessionDescriptionAndReference(2, 2, desc_ms1_two_tracks.accept());
1718 EXPECT_TRUE(DoSetRemoteDescription(desc_ms1_two_tracks.release()));
1719 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
1720 reference_collection_));
1721
1722 // Remove the extra audio and video tracks.
1723 rtc::scoped_ptr<SessionDescriptionInterface> desc_ms2;
1724 CreateSessionDescriptionAndReference(1, 1, desc_ms2.accept());
1725 EXPECT_TRUE(DoSetRemoteDescription(desc_ms2.release()));
1726 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
1727 reference_collection_));
1728}
1729
1730// This tests that remote tracks are ended if a local session description is set
1731// that rejects the media content type.
1732TEST_F(PeerConnectionInterfaceTest, RejectMediaContent) {
1733 FakeConstraints constraints;
1734 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
1735 true);
1736 CreatePeerConnection(&constraints);
1737 // First create and set a remote offer, then reject its video content in our
1738 // answer.
1739 CreateAndSetRemoteOffer(kSdpStringWithStream1);
1740 ASSERT_EQ(1u, observer_.remote_streams()->count());
1741 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
1742 ASSERT_EQ(1u, remote_stream->GetVideoTracks().size());
1743 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
1744
1745 rtc::scoped_refptr<webrtc::VideoTrackInterface> remote_video =
1746 remote_stream->GetVideoTracks()[0];
1747 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, remote_video->state());
1748 rtc::scoped_refptr<webrtc::AudioTrackInterface> remote_audio =
1749 remote_stream->GetAudioTracks()[0];
1750 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, remote_audio->state());
1751
1752 rtc::scoped_ptr<SessionDescriptionInterface> local_answer;
1753 EXPECT_TRUE(DoCreateAnswer(local_answer.accept()));
1754 cricket::ContentInfo* video_info =
1755 local_answer->description()->GetContentByName("video");
1756 video_info->rejected = true;
1757 EXPECT_TRUE(DoSetLocalDescription(local_answer.release()));
1758 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kEnded, remote_video->state());
1759 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, remote_audio->state());
1760
1761 // Now create an offer where we reject both video and audio.
1762 rtc::scoped_ptr<SessionDescriptionInterface> local_offer;
1763 EXPECT_TRUE(DoCreateOffer(local_offer.accept()));
1764 video_info = local_offer->description()->GetContentByName("video");
1765 ASSERT_TRUE(video_info != nullptr);
1766 video_info->rejected = true;
1767 cricket::ContentInfo* audio_info =
1768 local_offer->description()->GetContentByName("audio");
1769 ASSERT_TRUE(audio_info != nullptr);
1770 audio_info->rejected = true;
1771 EXPECT_TRUE(DoSetLocalDescription(local_offer.release()));
1772 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kEnded, remote_video->state());
1773 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kEnded, remote_audio->state());
1774}
1775
1776// This tests that we won't crash if the remote track has been removed outside
1777// of PeerConnection and then PeerConnection tries to reject the track.
1778TEST_F(PeerConnectionInterfaceTest, RemoveTrackThenRejectMediaContent) {
1779 FakeConstraints constraints;
1780 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
1781 true);
1782 CreatePeerConnection(&constraints);
1783 CreateAndSetRemoteOffer(kSdpStringWithStream1);
1784 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
1785 remote_stream->RemoveTrack(remote_stream->GetVideoTracks()[0]);
1786 remote_stream->RemoveTrack(remote_stream->GetAudioTracks()[0]);
1787
1788 rtc::scoped_ptr<SessionDescriptionInterface> local_answer(
1789 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
1790 kSdpStringWithStream1, nullptr));
1791 cricket::ContentInfo* video_info =
1792 local_answer->description()->GetContentByName("video");
1793 video_info->rejected = true;
1794 cricket::ContentInfo* audio_info =
1795 local_answer->description()->GetContentByName("audio");
1796 audio_info->rejected = true;
1797 EXPECT_TRUE(DoSetLocalDescription(local_answer.release()));
1798
1799 // No crash is a pass.
1800}
1801
deadbeef5e97fb52015-10-15 12:49:08 -07001802// This tests that if a recvonly remote description is set, no remote streams
1803// will be created, even if the description contains SSRCs/MSIDs.
1804// See: https://code.google.com/p/webrtc/issues/detail?id=5054
1805TEST_F(PeerConnectionInterfaceTest, RecvonlyDescriptionDoesntCreateStream) {
1806 FakeConstraints constraints;
1807 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
1808 true);
1809 CreatePeerConnection(&constraints);
1810
1811 std::string recvonly_offer = kSdpStringWithStream1;
1812 rtc::replace_substrs(kSendrecv, strlen(kSendrecv), kRecvonly,
1813 strlen(kRecvonly), &recvonly_offer);
1814 CreateAndSetRemoteOffer(recvonly_offer);
1815
1816 EXPECT_EQ(0u, observer_.remote_streams()->count());
1817}
1818
deadbeefab9b2d12015-10-14 11:33:11 -07001819// This tests that a default MediaStream is created if a remote session
1820// description doesn't contain any streams and no MSID support.
1821// It also tests that the default stream is updated if a video m-line is added
1822// in a subsequent session description.
1823TEST_F(PeerConnectionInterfaceTest, SdpWithoutMsidCreatesDefaultStream) {
1824 FakeConstraints constraints;
1825 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
1826 true);
1827 CreatePeerConnection(&constraints);
1828 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
1829
1830 ASSERT_EQ(1u, observer_.remote_streams()->count());
1831 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
1832
1833 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
1834 EXPECT_EQ(0u, remote_stream->GetVideoTracks().size());
1835 EXPECT_EQ("default", remote_stream->label());
1836
1837 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
1838 ASSERT_EQ(1u, observer_.remote_streams()->count());
1839 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
1840 EXPECT_EQ("defaulta0", remote_stream->GetAudioTracks()[0]->id());
1841 ASSERT_EQ(1u, remote_stream->GetVideoTracks().size());
1842 EXPECT_EQ("defaultv0", remote_stream->GetVideoTracks()[0]->id());
1843}
1844
1845// This tests that a default MediaStream is created if a remote session
1846// description doesn't contain any streams and media direction is send only.
1847TEST_F(PeerConnectionInterfaceTest,
1848 SendOnlySdpWithoutMsidCreatesDefaultStream) {
1849 FakeConstraints constraints;
1850 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
1851 true);
1852 CreatePeerConnection(&constraints);
1853 CreateAndSetRemoteOffer(kSdpStringSendOnlyWithoutStreams);
1854
1855 ASSERT_EQ(1u, observer_.remote_streams()->count());
1856 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
1857
1858 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
1859 EXPECT_EQ(1u, remote_stream->GetVideoTracks().size());
1860 EXPECT_EQ("default", remote_stream->label());
1861}
1862
1863// This tests that it won't crash when PeerConnection tries to remove
1864// a remote track that as already been removed from the MediaStream.
1865TEST_F(PeerConnectionInterfaceTest, RemoveAlreadyGoneRemoteStream) {
1866 FakeConstraints constraints;
1867 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
1868 true);
1869 CreatePeerConnection(&constraints);
1870 CreateAndSetRemoteOffer(kSdpStringWithStream1);
1871 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
1872 remote_stream->RemoveTrack(remote_stream->GetAudioTracks()[0]);
1873 remote_stream->RemoveTrack(remote_stream->GetVideoTracks()[0]);
1874
1875 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
1876
1877 // No crash is a pass.
1878}
1879
1880// This tests that a default MediaStream is created if the remote session
1881// description doesn't contain any streams and don't contain an indication if
1882// MSID is supported.
1883TEST_F(PeerConnectionInterfaceTest,
1884 SdpWithoutMsidAndStreamsCreatesDefaultStream) {
1885 FakeConstraints constraints;
1886 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
1887 true);
1888 CreatePeerConnection(&constraints);
1889 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
1890
1891 ASSERT_EQ(1u, observer_.remote_streams()->count());
1892 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
1893 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
1894 EXPECT_EQ(1u, remote_stream->GetVideoTracks().size());
1895}
1896
1897// This tests that a default MediaStream is not created if the remote session
1898// description doesn't contain any streams but does support MSID.
1899TEST_F(PeerConnectionInterfaceTest, SdpWithMsidDontCreatesDefaultStream) {
1900 FakeConstraints constraints;
1901 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
1902 true);
1903 CreatePeerConnection(&constraints);
1904 CreateAndSetRemoteOffer(kSdpStringWithMsidWithoutStreams);
1905 EXPECT_EQ(0u, observer_.remote_streams()->count());
1906}
1907
1908// This tests that a default MediaStream is not created if a remote session
1909// description is updated to not have any MediaStreams.
1910TEST_F(PeerConnectionInterfaceTest, VerifyDefaultStreamIsNotCreated) {
1911 FakeConstraints constraints;
1912 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
1913 true);
1914 CreatePeerConnection(&constraints);
1915 CreateAndSetRemoteOffer(kSdpStringWithStream1);
1916 rtc::scoped_refptr<StreamCollection> reference(CreateStreamCollection(1));
1917 EXPECT_TRUE(
1918 CompareStreamCollections(observer_.remote_streams(), reference.get()));
1919
1920 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
1921 EXPECT_EQ(0u, observer_.remote_streams()->count());
1922}
1923
1924// This tests that an RtpSender is created when the local description is set
1925// after adding a local stream.
1926// TODO(deadbeef): This test and the one below it need to be updated when
1927// an RtpSender's lifetime isn't determined by when a local description is set.
1928TEST_F(PeerConnectionInterfaceTest, LocalDescriptionChanged) {
1929 FakeConstraints constraints;
1930 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
1931 true);
1932 CreatePeerConnection(&constraints);
1933 // Create an offer just to ensure we have an identity before we manually
1934 // call SetLocalDescription.
1935 rtc::scoped_ptr<SessionDescriptionInterface> throwaway;
1936 ASSERT_TRUE(DoCreateOffer(throwaway.accept()));
1937
1938 rtc::scoped_ptr<SessionDescriptionInterface> desc_1;
1939 CreateSessionDescriptionAndReference(2, 2, desc_1.accept());
1940
1941 pc_->AddStream(reference_collection_->at(0));
1942 EXPECT_TRUE(DoSetLocalDescription(desc_1.release()));
1943 auto senders = pc_->GetSenders();
1944 EXPECT_EQ(4u, senders.size());
1945 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
1946 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
1947 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[1]));
1948 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[1]));
1949
1950 // Remove an audio and video track.
1951 rtc::scoped_ptr<SessionDescriptionInterface> desc_2;
1952 CreateSessionDescriptionAndReference(1, 1, desc_2.accept());
1953 EXPECT_TRUE(DoSetLocalDescription(desc_2.release()));
1954 senders = pc_->GetSenders();
1955 EXPECT_EQ(2u, senders.size());
1956 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
1957 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
1958 EXPECT_FALSE(ContainsSender(senders, kAudioTracks[1]));
1959 EXPECT_FALSE(ContainsSender(senders, kVideoTracks[1]));
1960}
1961
1962// This tests that an RtpSender is created when the local description is set
1963// before adding a local stream.
1964TEST_F(PeerConnectionInterfaceTest,
1965 AddLocalStreamAfterLocalDescriptionChanged) {
1966 FakeConstraints constraints;
1967 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
1968 true);
1969 CreatePeerConnection(&constraints);
1970 // Create an offer just to ensure we have an identity before we manually
1971 // call SetLocalDescription.
1972 rtc::scoped_ptr<SessionDescriptionInterface> throwaway;
1973 ASSERT_TRUE(DoCreateOffer(throwaway.accept()));
1974
1975 rtc::scoped_ptr<SessionDescriptionInterface> desc_1;
1976 CreateSessionDescriptionAndReference(2, 2, desc_1.accept());
1977
1978 EXPECT_TRUE(DoSetLocalDescription(desc_1.release()));
1979 auto senders = pc_->GetSenders();
1980 EXPECT_EQ(0u, senders.size());
1981
1982 pc_->AddStream(reference_collection_->at(0));
1983 senders = pc_->GetSenders();
1984 EXPECT_EQ(4u, senders.size());
1985 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
1986 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
1987 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[1]));
1988 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[1]));
1989}
1990
1991// This tests that the expected behavior occurs if the SSRC on a local track is
1992// changed when SetLocalDescription is called.
1993TEST_F(PeerConnectionInterfaceTest,
1994 ChangeSsrcOnTrackInLocalSessionDescription) {
1995 FakeConstraints constraints;
1996 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
1997 true);
1998 CreatePeerConnection(&constraints);
1999 // Create an offer just to ensure we have an identity before we manually
2000 // call SetLocalDescription.
2001 rtc::scoped_ptr<SessionDescriptionInterface> throwaway;
2002 ASSERT_TRUE(DoCreateOffer(throwaway.accept()));
2003
2004 rtc::scoped_ptr<SessionDescriptionInterface> desc;
2005 CreateSessionDescriptionAndReference(1, 1, desc.accept());
2006 std::string sdp;
2007 desc->ToString(&sdp);
2008
2009 pc_->AddStream(reference_collection_->at(0));
2010 EXPECT_TRUE(DoSetLocalDescription(desc.release()));
2011 auto senders = pc_->GetSenders();
2012 EXPECT_EQ(2u, senders.size());
2013 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2014 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2015
2016 // Change the ssrc of the audio and video track.
2017 std::string ssrc_org = "a=ssrc:1";
2018 std::string ssrc_to = "a=ssrc:97";
2019 rtc::replace_substrs(ssrc_org.c_str(), ssrc_org.length(), ssrc_to.c_str(),
2020 ssrc_to.length(), &sdp);
2021 ssrc_org = "a=ssrc:2";
2022 ssrc_to = "a=ssrc:98";
2023 rtc::replace_substrs(ssrc_org.c_str(), ssrc_org.length(), ssrc_to.c_str(),
2024 ssrc_to.length(), &sdp);
2025 rtc::scoped_ptr<SessionDescriptionInterface> updated_desc(
2026 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer, sdp,
2027 nullptr));
2028
2029 EXPECT_TRUE(DoSetLocalDescription(updated_desc.release()));
2030 senders = pc_->GetSenders();
2031 EXPECT_EQ(2u, senders.size());
2032 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2033 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2034 // TODO(deadbeef): Once RtpSenders expose parameters, check that the SSRC
2035 // changed.
2036}
2037
2038// This tests that the expected behavior occurs if a new session description is
2039// set with the same tracks, but on a different MediaStream.
2040TEST_F(PeerConnectionInterfaceTest, SignalSameTracksInSeparateMediaStream) {
2041 FakeConstraints constraints;
2042 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2043 true);
2044 CreatePeerConnection(&constraints);
2045 // Create an offer just to ensure we have an identity before we manually
2046 // call SetLocalDescription.
2047 rtc::scoped_ptr<SessionDescriptionInterface> throwaway;
2048 ASSERT_TRUE(DoCreateOffer(throwaway.accept()));
2049
2050 rtc::scoped_ptr<SessionDescriptionInterface> desc;
2051 CreateSessionDescriptionAndReference(1, 1, desc.accept());
2052 std::string sdp;
2053 desc->ToString(&sdp);
2054
2055 pc_->AddStream(reference_collection_->at(0));
2056 EXPECT_TRUE(DoSetLocalDescription(desc.release()));
2057 auto senders = pc_->GetSenders();
2058 EXPECT_EQ(2u, senders.size());
2059 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2060 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2061
2062 // Add a new MediaStream but with the same tracks as in the first stream.
2063 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream_1(
2064 webrtc::MediaStream::Create(kStreams[1]));
2065 stream_1->AddTrack(reference_collection_->at(0)->GetVideoTracks()[0]);
2066 stream_1->AddTrack(reference_collection_->at(0)->GetAudioTracks()[0]);
2067 pc_->AddStream(stream_1);
2068
2069 // Replace msid in the original SDP.
2070 rtc::replace_substrs(kStreams[0], strlen(kStreams[0]), kStreams[1],
2071 strlen(kStreams[1]), &sdp);
2072
2073 rtc::scoped_ptr<SessionDescriptionInterface> updated_desc(
2074 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer, sdp,
2075 nullptr));
2076
2077 EXPECT_TRUE(DoSetLocalDescription(updated_desc.release()));
2078 senders = pc_->GetSenders();
2079 EXPECT_EQ(2u, senders.size());
2080 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2081 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2082}
2083
2084// The following tests verify that session options are created correctly.
2085
2086TEST(CreateSessionOptionsTest, GetOptionsForOfferWithInvalidAudioOption) {
2087 RTCOfferAnswerOptions rtc_options;
2088 rtc_options.offer_to_receive_audio = RTCOfferAnswerOptions::kUndefined - 1;
2089
2090 cricket::MediaSessionOptions options;
2091 EXPECT_FALSE(ConvertRtcOptionsForOffer(rtc_options, &options));
2092
2093 rtc_options.offer_to_receive_audio =
2094 RTCOfferAnswerOptions::kMaxOfferToReceiveMedia + 1;
2095 EXPECT_FALSE(ConvertRtcOptionsForOffer(rtc_options, &options));
2096}
2097
2098TEST(CreateSessionOptionsTest, GetOptionsForOfferWithInvalidVideoOption) {
2099 RTCOfferAnswerOptions rtc_options;
2100 rtc_options.offer_to_receive_video = RTCOfferAnswerOptions::kUndefined - 1;
2101
2102 cricket::MediaSessionOptions options;
2103 EXPECT_FALSE(ConvertRtcOptionsForOffer(rtc_options, &options));
2104
2105 rtc_options.offer_to_receive_video =
2106 RTCOfferAnswerOptions::kMaxOfferToReceiveMedia + 1;
2107 EXPECT_FALSE(ConvertRtcOptionsForOffer(rtc_options, &options));
2108}
2109
2110// Test that a MediaSessionOptions is created for an offer if
2111// OfferToReceiveAudio and OfferToReceiveVideo options are set but no
2112// MediaStreams are sent.
2113TEST(CreateSessionOptionsTest, GetMediaSessionOptionsForOfferWithAudioVideo) {
2114 RTCOfferAnswerOptions rtc_options;
2115 rtc_options.offer_to_receive_audio = 1;
2116 rtc_options.offer_to_receive_video = 1;
2117
2118 cricket::MediaSessionOptions options;
2119 EXPECT_TRUE(ConvertRtcOptionsForOffer(rtc_options, &options));
2120 EXPECT_TRUE(options.has_audio());
2121 EXPECT_TRUE(options.has_video());
2122 EXPECT_TRUE(options.bundle_enabled);
2123}
2124
2125// Test that a correct MediaSessionOptions is created for an offer if
2126// OfferToReceiveAudio is set but no MediaStreams are sent.
2127TEST(CreateSessionOptionsTest, GetMediaSessionOptionsForOfferWithAudio) {
2128 RTCOfferAnswerOptions rtc_options;
2129 rtc_options.offer_to_receive_audio = 1;
2130
2131 cricket::MediaSessionOptions options;
2132 EXPECT_TRUE(ConvertRtcOptionsForOffer(rtc_options, &options));
2133 EXPECT_TRUE(options.has_audio());
2134 EXPECT_FALSE(options.has_video());
2135 EXPECT_TRUE(options.bundle_enabled);
2136}
2137
2138// Test that a correct MediaSessionOptions is created for an offer if
2139// the default OfferOptons is used or MediaStreams are sent.
2140TEST(CreateSessionOptionsTest, GetDefaultMediaSessionOptionsForOffer) {
2141 RTCOfferAnswerOptions rtc_options;
2142
2143 cricket::MediaSessionOptions options;
2144 EXPECT_TRUE(ConvertRtcOptionsForOffer(rtc_options, &options));
2145 EXPECT_FALSE(options.has_audio());
2146 EXPECT_FALSE(options.has_video());
2147 EXPECT_FALSE(options.bundle_enabled);
2148 EXPECT_TRUE(options.vad_enabled);
2149 EXPECT_FALSE(options.transport_options.ice_restart);
2150}
2151
2152// Test that a correct MediaSessionOptions is created for an offer if
2153// OfferToReceiveVideo is set but no MediaStreams are sent.
2154TEST(CreateSessionOptionsTest, GetMediaSessionOptionsForOfferWithVideo) {
2155 RTCOfferAnswerOptions rtc_options;
2156 rtc_options.offer_to_receive_audio = 0;
2157 rtc_options.offer_to_receive_video = 1;
2158
2159 cricket::MediaSessionOptions options;
2160 EXPECT_TRUE(ConvertRtcOptionsForOffer(rtc_options, &options));
2161 EXPECT_FALSE(options.has_audio());
2162 EXPECT_TRUE(options.has_video());
2163 EXPECT_TRUE(options.bundle_enabled);
2164}
2165
2166// Test that a correct MediaSessionOptions is created for an offer if
2167// UseRtpMux is set to false.
2168TEST(CreateSessionOptionsTest,
2169 GetMediaSessionOptionsForOfferWithBundleDisabled) {
2170 RTCOfferAnswerOptions rtc_options;
2171 rtc_options.offer_to_receive_audio = 1;
2172 rtc_options.offer_to_receive_video = 1;
2173 rtc_options.use_rtp_mux = false;
2174
2175 cricket::MediaSessionOptions options;
2176 EXPECT_TRUE(ConvertRtcOptionsForOffer(rtc_options, &options));
2177 EXPECT_TRUE(options.has_audio());
2178 EXPECT_TRUE(options.has_video());
2179 EXPECT_FALSE(options.bundle_enabled);
2180}
2181
2182// Test that a correct MediaSessionOptions is created to restart ice if
2183// IceRestart is set. It also tests that subsequent MediaSessionOptions don't
2184// have |transport_options.ice_restart| set.
2185TEST(CreateSessionOptionsTest, GetMediaSessionOptionsForOfferWithIceRestart) {
2186 RTCOfferAnswerOptions rtc_options;
2187 rtc_options.ice_restart = true;
2188
2189 cricket::MediaSessionOptions options;
2190 EXPECT_TRUE(ConvertRtcOptionsForOffer(rtc_options, &options));
2191 EXPECT_TRUE(options.transport_options.ice_restart);
2192
2193 rtc_options = RTCOfferAnswerOptions();
2194 EXPECT_TRUE(ConvertRtcOptionsForOffer(rtc_options, &options));
2195 EXPECT_FALSE(options.transport_options.ice_restart);
2196}
2197
2198// Test that the MediaConstraints in an answer don't affect if audio and video
2199// is offered in an offer but that if kOfferToReceiveAudio or
2200// kOfferToReceiveVideo constraints are true in an offer, the media type will be
2201// included in subsequent answers.
2202TEST(CreateSessionOptionsTest, MediaConstraintsInAnswer) {
2203 FakeConstraints answer_c;
2204 answer_c.SetMandatoryReceiveAudio(true);
2205 answer_c.SetMandatoryReceiveVideo(true);
2206
2207 cricket::MediaSessionOptions answer_options;
2208 EXPECT_TRUE(ParseConstraintsForAnswer(&answer_c, &answer_options));
2209 EXPECT_TRUE(answer_options.has_audio());
2210 EXPECT_TRUE(answer_options.has_video());
2211
2212 RTCOfferAnswerOptions rtc_offer_optoins;
2213
2214 cricket::MediaSessionOptions offer_options;
2215 EXPECT_TRUE(ConvertRtcOptionsForOffer(rtc_offer_optoins, &offer_options));
2216 EXPECT_FALSE(offer_options.has_audio());
2217 EXPECT_FALSE(offer_options.has_video());
2218
2219 RTCOfferAnswerOptions updated_rtc_offer_optoins;
2220 updated_rtc_offer_optoins.offer_to_receive_audio = 1;
2221 updated_rtc_offer_optoins.offer_to_receive_video = 1;
2222
2223 cricket::MediaSessionOptions updated_offer_options;
2224 EXPECT_TRUE(ConvertRtcOptionsForOffer(updated_rtc_offer_optoins,
2225 &updated_offer_options));
2226 EXPECT_TRUE(updated_offer_options.has_audio());
2227 EXPECT_TRUE(updated_offer_options.has_video());
2228
2229 // Since an offer has been created with both audio and video, subsequent
2230 // offers and answers should contain both audio and video.
2231 // Answers will only contain the media types that exist in the offer
2232 // regardless of the value of |updated_answer_options.has_audio| and
2233 // |updated_answer_options.has_video|.
2234 FakeConstraints updated_answer_c;
2235 answer_c.SetMandatoryReceiveAudio(false);
2236 answer_c.SetMandatoryReceiveVideo(false);
2237
2238 cricket::MediaSessionOptions updated_answer_options;
2239 EXPECT_TRUE(
2240 ParseConstraintsForAnswer(&updated_answer_c, &updated_answer_options));
2241 EXPECT_TRUE(updated_answer_options.has_audio());
2242 EXPECT_TRUE(updated_answer_options.has_video());
2243
2244 RTCOfferAnswerOptions default_rtc_options;
2245 EXPECT_TRUE(
2246 ConvertRtcOptionsForOffer(default_rtc_options, &updated_offer_options));
2247 // By default, |has_audio| or |has_video| are false if there is no media
2248 // track.
2249 EXPECT_FALSE(updated_offer_options.has_audio());
2250 EXPECT_FALSE(updated_offer_options.has_video());
2251}