blob: 5e88658a4e25ce3c464853dfbba53a1335829c63 [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
74// Reference SDP with a MediaStream with label "stream1" and audio track with
75// id "audio_1" and a video track with id "video_1;
76static const char kSdpStringWithStream1[] =
77 "v=0\r\n"
78 "o=- 0 0 IN IP4 127.0.0.1\r\n"
79 "s=-\r\n"
80 "t=0 0\r\n"
81 "a=ice-ufrag:e5785931\r\n"
82 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
83 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
84 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
85 "m=audio 1 RTP/AVPF 103\r\n"
86 "a=mid:audio\r\n"
87 "a=rtpmap:103 ISAC/16000\r\n"
88 "a=ssrc:1 cname:stream1\r\n"
89 "a=ssrc:1 mslabel:stream1\r\n"
90 "a=ssrc:1 label:audiotrack0\r\n"
91 "m=video 1 RTP/AVPF 120\r\n"
92 "a=mid:video\r\n"
93 "a=rtpmap:120 VP8/90000\r\n"
94 "a=ssrc:2 cname:stream1\r\n"
95 "a=ssrc:2 mslabel:stream1\r\n"
96 "a=ssrc:2 label:videotrack0\r\n";
97
98// Reference SDP with two MediaStreams with label "stream1" and "stream2. Each
99// MediaStreams have one audio track and one video track.
100// This uses MSID.
101static const char kSdpStringWithStream1And2[] =
102 "v=0\r\n"
103 "o=- 0 0 IN IP4 127.0.0.1\r\n"
104 "s=-\r\n"
105 "t=0 0\r\n"
106 "a=ice-ufrag:e5785931\r\n"
107 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
108 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
109 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
110 "a=msid-semantic: WMS stream1 stream2\r\n"
111 "m=audio 1 RTP/AVPF 103\r\n"
112 "a=mid:audio\r\n"
113 "a=rtpmap:103 ISAC/16000\r\n"
114 "a=ssrc:1 cname:stream1\r\n"
115 "a=ssrc:1 msid:stream1 audiotrack0\r\n"
116 "a=ssrc:3 cname:stream2\r\n"
117 "a=ssrc:3 msid:stream2 audiotrack1\r\n"
118 "m=video 1 RTP/AVPF 120\r\n"
119 "a=mid:video\r\n"
120 "a=rtpmap:120 VP8/0\r\n"
121 "a=ssrc:2 cname:stream1\r\n"
122 "a=ssrc:2 msid:stream1 videotrack0\r\n"
123 "a=ssrc:4 cname:stream2\r\n"
124 "a=ssrc:4 msid:stream2 videotrack1\r\n";
125
126// Reference SDP without MediaStreams. Msid is not supported.
127static const char kSdpStringWithoutStreams[] =
128 "v=0\r\n"
129 "o=- 0 0 IN IP4 127.0.0.1\r\n"
130 "s=-\r\n"
131 "t=0 0\r\n"
132 "a=ice-ufrag:e5785931\r\n"
133 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
134 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
135 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
136 "m=audio 1 RTP/AVPF 103\r\n"
137 "a=mid:audio\r\n"
138 "a=rtpmap:103 ISAC/16000\r\n"
139 "m=video 1 RTP/AVPF 120\r\n"
140 "a=mid:video\r\n"
141 "a=rtpmap:120 VP8/90000\r\n";
142
143// Reference SDP without MediaStreams. Msid is supported.
144static const char kSdpStringWithMsidWithoutStreams[] =
145 "v=0\r\n"
146 "o=- 0 0 IN IP4 127.0.0.1\r\n"
147 "s=-\r\n"
148 "t=0 0\r\n"
149 "a=ice-ufrag:e5785931\r\n"
150 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
151 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
152 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
153 "a=msid-semantic: WMS\r\n"
154 "m=audio 1 RTP/AVPF 103\r\n"
155 "a=mid:audio\r\n"
156 "a=rtpmap:103 ISAC/16000\r\n"
157 "m=video 1 RTP/AVPF 120\r\n"
158 "a=mid:video\r\n"
159 "a=rtpmap:120 VP8/90000\r\n";
160
161// Reference SDP without MediaStreams and audio only.
162static const char kSdpStringWithoutStreamsAudioOnly[] =
163 "v=0\r\n"
164 "o=- 0 0 IN IP4 127.0.0.1\r\n"
165 "s=-\r\n"
166 "t=0 0\r\n"
167 "a=ice-ufrag:e5785931\r\n"
168 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
169 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
170 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
171 "m=audio 1 RTP/AVPF 103\r\n"
172 "a=mid:audio\r\n"
173 "a=rtpmap:103 ISAC/16000\r\n";
174
175// Reference SENDONLY SDP without MediaStreams. Msid is not supported.
176static const char kSdpStringSendOnlyWithoutStreams[] =
177 "v=0\r\n"
178 "o=- 0 0 IN IP4 127.0.0.1\r\n"
179 "s=-\r\n"
180 "t=0 0\r\n"
181 "a=ice-ufrag:e5785931\r\n"
182 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
183 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
184 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
185 "m=audio 1 RTP/AVPF 103\r\n"
186 "a=mid:audio\r\n"
187 "a=sendonly\r\n"
188 "a=rtpmap:103 ISAC/16000\r\n"
189 "m=video 1 RTP/AVPF 120\r\n"
190 "a=mid:video\r\n"
191 "a=sendonly\r\n"
192 "a=rtpmap:120 VP8/90000\r\n";
193
194static const char kSdpStringInit[] =
195 "v=0\r\n"
196 "o=- 0 0 IN IP4 127.0.0.1\r\n"
197 "s=-\r\n"
198 "t=0 0\r\n"
199 "a=ice-ufrag:e5785931\r\n"
200 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
201 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
202 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
203 "a=msid-semantic: WMS\r\n";
204
205static const char kSdpStringAudio[] =
206 "m=audio 1 RTP/AVPF 103\r\n"
207 "a=mid:audio\r\n"
208 "a=rtpmap:103 ISAC/16000\r\n";
209
210static const char kSdpStringVideo[] =
211 "m=video 1 RTP/AVPF 120\r\n"
212 "a=mid:video\r\n"
213 "a=rtpmap:120 VP8/90000\r\n";
214
215static const char kSdpStringMs1Audio0[] =
216 "a=ssrc:1 cname:stream1\r\n"
217 "a=ssrc:1 msid:stream1 audiotrack0\r\n";
218
219static const char kSdpStringMs1Video0[] =
220 "a=ssrc:2 cname:stream1\r\n"
221 "a=ssrc:2 msid:stream1 videotrack0\r\n";
222
223static const char kSdpStringMs1Audio1[] =
224 "a=ssrc:3 cname:stream1\r\n"
225 "a=ssrc:3 msid:stream1 audiotrack1\r\n";
226
227static const char kSdpStringMs1Video1[] =
228 "a=ssrc:4 cname:stream1\r\n"
229 "a=ssrc:4 msid:stream1 videotrack1\r\n";
230
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000231#define MAYBE_SKIP_TEST(feature) \
232 if (!(feature())) { \
233 LOG(LS_INFO) << "Feature disabled... skipping"; \
234 return; \
235 }
236
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000237using rtc::scoped_ptr;
238using rtc::scoped_refptr;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000239using webrtc::AudioSourceInterface;
deadbeefab9b2d12015-10-14 11:33:11 -0700240using webrtc::AudioTrack;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000241using webrtc::AudioTrackInterface;
242using webrtc::DataBuffer;
243using webrtc::DataChannelInterface;
244using webrtc::FakeConstraints;
245using webrtc::FakePortAllocatorFactory;
246using webrtc::IceCandidateInterface;
deadbeefab9b2d12015-10-14 11:33:11 -0700247using webrtc::MediaStream;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000248using webrtc::MediaStreamInterface;
249using webrtc::MediaStreamTrackInterface;
250using webrtc::MockCreateSessionDescriptionObserver;
251using webrtc::MockDataChannelObserver;
252using webrtc::MockSetSessionDescriptionObserver;
253using webrtc::MockStatsObserver;
254using webrtc::PeerConnectionInterface;
255using webrtc::PeerConnectionObserver;
256using webrtc::PortAllocatorFactoryInterface;
deadbeefab9b2d12015-10-14 11:33:11 -0700257using webrtc::RtpReceiverInterface;
258using webrtc::RtpSenderInterface;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000259using webrtc::SdpParseError;
260using webrtc::SessionDescriptionInterface;
deadbeefab9b2d12015-10-14 11:33:11 -0700261using webrtc::StreamCollection;
262using webrtc::StreamCollectionInterface;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000263using webrtc::VideoSourceInterface;
deadbeefab9b2d12015-10-14 11:33:11 -0700264using webrtc::VideoTrack;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000265using webrtc::VideoTrackInterface;
266
deadbeefab9b2d12015-10-14 11:33:11 -0700267typedef PeerConnectionInterface::RTCOfferAnswerOptions RTCOfferAnswerOptions;
268
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000269namespace {
270
271// Gets the first ssrc of given content type from the ContentInfo.
272bool GetFirstSsrc(const cricket::ContentInfo* content_info, int* ssrc) {
273 if (!content_info || !ssrc) {
274 return false;
275 }
276 const cricket::MediaContentDescription* media_desc =
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000277 static_cast<const cricket::MediaContentDescription*>(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000278 content_info->description);
279 if (!media_desc || media_desc->streams().empty()) {
280 return false;
281 }
282 *ssrc = media_desc->streams().begin()->first_ssrc();
283 return true;
284}
285
286void SetSsrcToZero(std::string* sdp) {
287 const char kSdpSsrcAtribute[] = "a=ssrc:";
288 const char kSdpSsrcAtributeZero[] = "a=ssrc:0";
289 size_t ssrc_pos = 0;
290 while ((ssrc_pos = sdp->find(kSdpSsrcAtribute, ssrc_pos)) !=
291 std::string::npos) {
292 size_t end_ssrc = sdp->find(" ", ssrc_pos);
293 sdp->replace(ssrc_pos, end_ssrc - ssrc_pos, kSdpSsrcAtributeZero);
294 ssrc_pos = end_ssrc;
295 }
296}
297
deadbeefab9b2d12015-10-14 11:33:11 -0700298// Check if |streams| contains the specified track.
299bool ContainsTrack(const std::vector<cricket::StreamParams>& streams,
300 const std::string& stream_label,
301 const std::string& track_id) {
302 for (const cricket::StreamParams& params : streams) {
303 if (params.sync_label == stream_label && params.id == track_id) {
304 return true;
305 }
306 }
307 return false;
308}
309
310// Check if |senders| contains the specified sender, by id.
311bool ContainsSender(
312 const std::vector<rtc::scoped_refptr<RtpSenderInterface>>& senders,
313 const std::string& id) {
314 for (const auto& sender : senders) {
315 if (sender->id() == id) {
316 return true;
317 }
318 }
319 return false;
320}
321
322// Create a collection of streams.
323// CreateStreamCollection(1) creates a collection that
324// correspond to kSdpStringWithStream1.
325// CreateStreamCollection(2) correspond to kSdpStringWithStream1And2.
326rtc::scoped_refptr<StreamCollection> CreateStreamCollection(
327 int number_of_streams) {
328 rtc::scoped_refptr<StreamCollection> local_collection(
329 StreamCollection::Create());
330
331 for (int i = 0; i < number_of_streams; ++i) {
332 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream(
333 webrtc::MediaStream::Create(kStreams[i]));
334
335 // Add a local audio track.
336 rtc::scoped_refptr<webrtc::AudioTrackInterface> audio_track(
337 webrtc::AudioTrack::Create(kAudioTracks[i], nullptr));
338 stream->AddTrack(audio_track);
339
340 // Add a local video track.
341 rtc::scoped_refptr<webrtc::VideoTrackInterface> video_track(
342 webrtc::VideoTrack::Create(kVideoTracks[i], nullptr));
343 stream->AddTrack(video_track);
344
345 local_collection->AddStream(stream);
346 }
347 return local_collection;
348}
349
350// Check equality of StreamCollections.
351bool CompareStreamCollections(StreamCollectionInterface* s1,
352 StreamCollectionInterface* s2) {
353 if (s1 == nullptr || s2 == nullptr || s1->count() != s2->count()) {
354 return false;
355 }
356
357 for (size_t i = 0; i != s1->count(); ++i) {
358 if (s1->at(i)->label() != s2->at(i)->label()) {
359 return false;
360 }
361 webrtc::AudioTrackVector audio_tracks1 = s1->at(i)->GetAudioTracks();
362 webrtc::AudioTrackVector audio_tracks2 = s2->at(i)->GetAudioTracks();
363 webrtc::VideoTrackVector video_tracks1 = s1->at(i)->GetVideoTracks();
364 webrtc::VideoTrackVector video_tracks2 = s2->at(i)->GetVideoTracks();
365
366 if (audio_tracks1.size() != audio_tracks2.size()) {
367 return false;
368 }
369 for (size_t j = 0; j != audio_tracks1.size(); ++j) {
370 if (audio_tracks1[j]->id() != audio_tracks2[j]->id()) {
371 return false;
372 }
373 }
374 if (video_tracks1.size() != video_tracks2.size()) {
375 return false;
376 }
377 for (size_t j = 0; j != video_tracks1.size(); ++j) {
378 if (video_tracks1[j]->id() != video_tracks2[j]->id()) {
379 return false;
380 }
381 }
382 }
383 return true;
384}
385
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000386class MockPeerConnectionObserver : public PeerConnectionObserver {
387 public:
deadbeefab9b2d12015-10-14 11:33:11 -0700388 MockPeerConnectionObserver() : remote_streams_(StreamCollection::Create()) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000389 ~MockPeerConnectionObserver() {
390 }
391 void SetPeerConnectionInterface(PeerConnectionInterface* pc) {
392 pc_ = pc;
393 if (pc) {
394 state_ = pc_->signaling_state();
395 }
396 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000397 virtual void OnSignalingChange(
398 PeerConnectionInterface::SignalingState new_state) {
399 EXPECT_EQ(pc_->signaling_state(), new_state);
400 state_ = new_state;
401 }
402 // TODO(bemasc): Remove this once callers transition to OnIceGatheringChange.
403 virtual void OnStateChange(StateType state_changed) {
404 if (pc_.get() == NULL)
405 return;
406 switch (state_changed) {
407 case kSignalingState:
408 // OnSignalingChange and OnStateChange(kSignalingState) should always
409 // be called approximately simultaneously. To ease testing, we require
410 // that they always be called in that order. This check verifies
411 // that OnSignalingChange has just been called.
412 EXPECT_EQ(pc_->signaling_state(), state_);
413 break;
414 case kIceState:
415 ADD_FAILURE();
416 break;
417 default:
418 ADD_FAILURE();
419 break;
420 }
421 }
deadbeefab9b2d12015-10-14 11:33:11 -0700422
423 MediaStreamInterface* RemoteStream(const std::string& label) {
424 return remote_streams_->find(label);
425 }
426 StreamCollectionInterface* remote_streams() const { return remote_streams_; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000427 virtual void OnAddStream(MediaStreamInterface* stream) {
428 last_added_stream_ = stream;
deadbeefab9b2d12015-10-14 11:33:11 -0700429 remote_streams_->AddStream(stream);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000430 }
431 virtual void OnRemoveStream(MediaStreamInterface* stream) {
432 last_removed_stream_ = stream;
deadbeefab9b2d12015-10-14 11:33:11 -0700433 remote_streams_->RemoveStream(stream);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000434 }
435 virtual void OnRenegotiationNeeded() {
436 renegotiation_needed_ = true;
437 }
438 virtual void OnDataChannel(DataChannelInterface* data_channel) {
439 last_datachannel_ = data_channel;
440 }
441
442 virtual void OnIceConnectionChange(
443 PeerConnectionInterface::IceConnectionState new_state) {
444 EXPECT_EQ(pc_->ice_connection_state(), new_state);
445 }
446 virtual void OnIceGatheringChange(
447 PeerConnectionInterface::IceGatheringState new_state) {
448 EXPECT_EQ(pc_->ice_gathering_state(), new_state);
449 }
450 virtual void OnIceCandidate(const webrtc::IceCandidateInterface* candidate) {
451 EXPECT_NE(PeerConnectionInterface::kIceGatheringNew,
452 pc_->ice_gathering_state());
453
454 std::string sdp;
455 EXPECT_TRUE(candidate->ToString(&sdp));
456 EXPECT_LT(0u, sdp.size());
457 last_candidate_.reset(webrtc::CreateIceCandidate(candidate->sdp_mid(),
458 candidate->sdp_mline_index(), sdp, NULL));
459 EXPECT_TRUE(last_candidate_.get() != NULL);
460 }
461 // TODO(bemasc): Remove this once callers transition to OnSignalingChange.
462 virtual void OnIceComplete() {
463 ice_complete_ = true;
464 // OnIceGatheringChange(IceGatheringCompleted) and OnIceComplete() should
465 // be called approximately simultaneously. For ease of testing, this
466 // check additionally requires that they be called in the above order.
467 EXPECT_EQ(PeerConnectionInterface::kIceGatheringComplete,
468 pc_->ice_gathering_state());
469 }
470
471 // Returns the label of the last added stream.
472 // Empty string if no stream have been added.
473 std::string GetLastAddedStreamLabel() {
474 if (last_added_stream_.get())
475 return last_added_stream_->label();
476 return "";
477 }
478 std::string GetLastRemovedStreamLabel() {
479 if (last_removed_stream_.get())
480 return last_removed_stream_->label();
481 return "";
482 }
483
484 scoped_refptr<PeerConnectionInterface> pc_;
485 PeerConnectionInterface::SignalingState state_;
486 scoped_ptr<IceCandidateInterface> last_candidate_;
487 scoped_refptr<DataChannelInterface> last_datachannel_;
deadbeefab9b2d12015-10-14 11:33:11 -0700488 rtc::scoped_refptr<StreamCollection> remote_streams_;
489 bool renegotiation_needed_ = false;
490 bool ice_complete_ = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000491
492 private:
493 scoped_refptr<MediaStreamInterface> last_added_stream_;
494 scoped_refptr<MediaStreamInterface> last_removed_stream_;
495};
496
497} // namespace
deadbeefab9b2d12015-10-14 11:33:11 -0700498
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000499class PeerConnectionInterfaceTest : public testing::Test {
500 protected:
501 virtual void SetUp() {
502 pc_factory_ = webrtc::CreatePeerConnectionFactory(
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000503 rtc::Thread::Current(), rtc::Thread::Current(), NULL, NULL,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000504 NULL);
505 ASSERT_TRUE(pc_factory_.get() != NULL);
506 }
507
508 void CreatePeerConnection() {
509 CreatePeerConnection("", "", NULL);
510 }
511
512 void CreatePeerConnection(webrtc::MediaConstraintsInterface* constraints) {
513 CreatePeerConnection("", "", constraints);
514 }
515
516 void CreatePeerConnection(const std::string& uri,
517 const std::string& password,
518 webrtc::MediaConstraintsInterface* constraints) {
519 PeerConnectionInterface::IceServer server;
520 PeerConnectionInterface::IceServers servers;
deadbeef0a6c4ca2015-10-06 11:38:28 -0700521 if (!uri.empty()) {
522 server.uri = uri;
523 server.password = password;
524 servers.push_back(server);
525 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000526
527 port_allocator_factory_ = FakePortAllocatorFactory::Create();
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000528
buildbot@webrtc.org61c1b8e2014-04-09 06:06:38 +0000529 // DTLS does not work in a loopback call, so is disabled for most of the
530 // tests in this file. We only create a FakeIdentityService if the test
531 // explicitly sets the constraint.
jiayl@webrtc.org61e00b02015-03-04 22:17:38 +0000532 FakeConstraints default_constraints;
533 if (!constraints) {
534 constraints = &default_constraints;
535
536 default_constraints.AddMandatory(
537 webrtc::MediaConstraintsInterface::kEnableDtlsSrtp, false);
538 }
539
Henrik Boström5e56c592015-08-11 10:33:13 +0200540 scoped_ptr<webrtc::DtlsIdentityStoreInterface> dtls_identity_store;
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000541 bool dtls;
542 if (FindConstraint(constraints,
543 webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
544 &dtls,
Henrik Boström5e56c592015-08-11 10:33:13 +0200545 nullptr) && dtls) {
546 dtls_identity_store.reset(new FakeDtlsIdentityStore());
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000547 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000548 pc_ = pc_factory_->CreatePeerConnection(servers, constraints,
549 port_allocator_factory_.get(),
Henrik Boström5e56c592015-08-11 10:33:13 +0200550 dtls_identity_store.Pass(),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000551 &observer_);
552 ASSERT_TRUE(pc_.get() != NULL);
553 observer_.SetPeerConnectionInterface(pc_.get());
554 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
555 }
556
deadbeef0a6c4ca2015-10-06 11:38:28 -0700557 void CreatePeerConnectionExpectFail(const std::string& uri) {
558 PeerConnectionInterface::IceServer server;
559 PeerConnectionInterface::IceServers servers;
560 server.uri = uri;
561 servers.push_back(server);
562
563 scoped_ptr<webrtc::DtlsIdentityStoreInterface> dtls_identity_store;
564 port_allocator_factory_ = FakePortAllocatorFactory::Create();
565 scoped_refptr<PeerConnectionInterface> pc;
566 pc = pc_factory_->CreatePeerConnection(
567 servers, nullptr, port_allocator_factory_.get(),
568 dtls_identity_store.Pass(), &observer_);
569 ASSERT_EQ(nullptr, pc);
570 }
571
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000572 void CreatePeerConnectionWithDifferentConfigurations() {
573 CreatePeerConnection(kStunAddressOnly, "", NULL);
574 EXPECT_EQ(1u, port_allocator_factory_->stun_configs().size());
575 EXPECT_EQ(0u, port_allocator_factory_->turn_configs().size());
576 EXPECT_EQ("address",
577 port_allocator_factory_->stun_configs()[0].server.hostname());
578 EXPECT_EQ(kDefaultStunPort,
579 port_allocator_factory_->stun_configs()[0].server.port());
580
deadbeef0a6c4ca2015-10-06 11:38:28 -0700581 CreatePeerConnectionExpectFail(kStunInvalidPort);
582 CreatePeerConnectionExpectFail(kStunAddressPortAndMore1);
583 CreatePeerConnectionExpectFail(kStunAddressPortAndMore2);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000584
585 CreatePeerConnection(kTurnIceServerUri, kTurnPassword, NULL);
buildbot@webrtc.orgf875f152014-04-14 16:06:21 +0000586 EXPECT_EQ(0u, port_allocator_factory_->stun_configs().size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000587 EXPECT_EQ(1u, port_allocator_factory_->turn_configs().size());
588 EXPECT_EQ(kTurnUsername,
589 port_allocator_factory_->turn_configs()[0].username);
590 EXPECT_EQ(kTurnPassword,
591 port_allocator_factory_->turn_configs()[0].password);
592 EXPECT_EQ(kTurnHostname,
593 port_allocator_factory_->turn_configs()[0].server.hostname());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000594 }
595
596 void ReleasePeerConnection() {
597 pc_ = NULL;
598 observer_.SetPeerConnectionInterface(NULL);
599 }
600
deadbeefab9b2d12015-10-14 11:33:11 -0700601 void AddVideoStream(const std::string& label) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000602 // Create a local stream.
603 scoped_refptr<MediaStreamInterface> stream(
604 pc_factory_->CreateLocalMediaStream(label));
605 scoped_refptr<VideoSourceInterface> video_source(
606 pc_factory_->CreateVideoSource(new cricket::FakeVideoCapturer(), NULL));
607 scoped_refptr<VideoTrackInterface> video_track(
608 pc_factory_->CreateVideoTrack(label + "v0", video_source));
609 stream->AddTrack(video_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +0000610 EXPECT_TRUE(pc_->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000611 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
612 observer_.renegotiation_needed_ = false;
613 }
614
615 void AddVoiceStream(const std::string& label) {
616 // Create a local stream.
617 scoped_refptr<MediaStreamInterface> stream(
618 pc_factory_->CreateLocalMediaStream(label));
619 scoped_refptr<AudioTrackInterface> audio_track(
620 pc_factory_->CreateAudioTrack(label + "a0", NULL));
621 stream->AddTrack(audio_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +0000622 EXPECT_TRUE(pc_->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000623 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
624 observer_.renegotiation_needed_ = false;
625 }
626
627 void AddAudioVideoStream(const std::string& stream_label,
628 const std::string& audio_track_label,
629 const std::string& video_track_label) {
630 // Create a local stream.
631 scoped_refptr<MediaStreamInterface> stream(
632 pc_factory_->CreateLocalMediaStream(stream_label));
633 scoped_refptr<AudioTrackInterface> audio_track(
634 pc_factory_->CreateAudioTrack(
635 audio_track_label, static_cast<AudioSourceInterface*>(NULL)));
636 stream->AddTrack(audio_track.get());
637 scoped_refptr<VideoTrackInterface> video_track(
638 pc_factory_->CreateVideoTrack(video_track_label, NULL));
639 stream->AddTrack(video_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +0000640 EXPECT_TRUE(pc_->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000641 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
642 observer_.renegotiation_needed_ = false;
643 }
644
645 bool DoCreateOfferAnswer(SessionDescriptionInterface** desc, bool offer) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000646 rtc::scoped_refptr<MockCreateSessionDescriptionObserver>
647 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000648 MockCreateSessionDescriptionObserver>());
649 if (offer) {
650 pc_->CreateOffer(observer, NULL);
651 } else {
652 pc_->CreateAnswer(observer, NULL);
653 }
654 EXPECT_EQ_WAIT(true, observer->called(), kTimeout);
655 *desc = observer->release_desc();
656 return observer->result();
657 }
658
659 bool DoCreateOffer(SessionDescriptionInterface** desc) {
660 return DoCreateOfferAnswer(desc, true);
661 }
662
663 bool DoCreateAnswer(SessionDescriptionInterface** desc) {
664 return DoCreateOfferAnswer(desc, false);
665 }
666
667 bool DoSetSessionDescription(SessionDescriptionInterface* desc, bool local) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000668 rtc::scoped_refptr<MockSetSessionDescriptionObserver>
669 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000670 MockSetSessionDescriptionObserver>());
671 if (local) {
672 pc_->SetLocalDescription(observer, desc);
673 } else {
674 pc_->SetRemoteDescription(observer, desc);
675 }
676 EXPECT_EQ_WAIT(true, observer->called(), kTimeout);
677 return observer->result();
678 }
679
680 bool DoSetLocalDescription(SessionDescriptionInterface* desc) {
681 return DoSetSessionDescription(desc, true);
682 }
683
684 bool DoSetRemoteDescription(SessionDescriptionInterface* desc) {
685 return DoSetSessionDescription(desc, false);
686 }
687
688 // Calls PeerConnection::GetStats and check the return value.
689 // It does not verify the values in the StatReports since a RTCP packet might
690 // be required.
691 bool DoGetStats(MediaStreamTrackInterface* track) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000692 rtc::scoped_refptr<MockStatsObserver> observer(
693 new rtc::RefCountedObject<MockStatsObserver>());
jiayl@webrtc.orgdb41b4d2014-03-03 21:30:06 +0000694 if (!pc_->GetStats(
695 observer, track, PeerConnectionInterface::kStatsOutputLevelStandard))
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000696 return false;
697 EXPECT_TRUE_WAIT(observer->called(), kTimeout);
698 return observer->called();
699 }
700
701 void InitiateCall() {
702 CreatePeerConnection();
703 // Create a local stream with audio&video tracks.
704 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
705 CreateOfferReceiveAnswer();
706 }
707
708 // Verify that RTP Header extensions has been negotiated for audio and video.
709 void VerifyRemoteRtpHeaderExtensions() {
710 const cricket::MediaContentDescription* desc =
711 cricket::GetFirstAudioContentDescription(
712 pc_->remote_description()->description());
713 ASSERT_TRUE(desc != NULL);
714 EXPECT_GT(desc->rtp_header_extensions().size(), 0u);
715
716 desc = cricket::GetFirstVideoContentDescription(
717 pc_->remote_description()->description());
718 ASSERT_TRUE(desc != NULL);
719 EXPECT_GT(desc->rtp_header_extensions().size(), 0u);
720 }
721
722 void CreateOfferAsRemoteDescription() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000723 rtc::scoped_ptr<SessionDescriptionInterface> offer;
pkasting@chromium.org005b6ff2015-01-30 19:41:42 +0000724 ASSERT_TRUE(DoCreateOffer(offer.use()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000725 std::string sdp;
726 EXPECT_TRUE(offer->ToString(&sdp));
727 SessionDescriptionInterface* remote_offer =
728 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
729 sdp, NULL);
730 EXPECT_TRUE(DoSetRemoteDescription(remote_offer));
731 EXPECT_EQ(PeerConnectionInterface::kHaveRemoteOffer, observer_.state_);
732 }
733
deadbeefab9b2d12015-10-14 11:33:11 -0700734 void CreateAndSetRemoteOffer(const std::string& sdp) {
735 SessionDescriptionInterface* remote_offer =
736 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
737 sdp, nullptr);
738 EXPECT_TRUE(DoSetRemoteDescription(remote_offer));
739 EXPECT_EQ(PeerConnectionInterface::kHaveRemoteOffer, observer_.state_);
740 }
741
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000742 void CreateAnswerAsLocalDescription() {
743 scoped_ptr<SessionDescriptionInterface> answer;
pkasting@chromium.org005b6ff2015-01-30 19:41:42 +0000744 ASSERT_TRUE(DoCreateAnswer(answer.use()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000745
746 // TODO(perkj): Currently SetLocalDescription fails if any parameters in an
747 // audio codec change, even if the parameter has nothing to do with
748 // receiving. Not all parameters are serialized to SDP.
749 // Since CreatePrAnswerAsLocalDescription serialize/deserialize
750 // the SessionDescription, it is necessary to do that here to in order to
751 // get ReceiveOfferCreatePrAnswerAndAnswer and RenegotiateAudioOnly to pass.
752 // https://code.google.com/p/webrtc/issues/detail?id=1356
753 std::string sdp;
754 EXPECT_TRUE(answer->ToString(&sdp));
755 SessionDescriptionInterface* new_answer =
756 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
757 sdp, NULL);
758 EXPECT_TRUE(DoSetLocalDescription(new_answer));
759 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
760 }
761
762 void CreatePrAnswerAsLocalDescription() {
763 scoped_ptr<SessionDescriptionInterface> answer;
pkasting@chromium.org005b6ff2015-01-30 19:41:42 +0000764 ASSERT_TRUE(DoCreateAnswer(answer.use()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000765
766 std::string sdp;
767 EXPECT_TRUE(answer->ToString(&sdp));
768 SessionDescriptionInterface* pr_answer =
769 webrtc::CreateSessionDescription(SessionDescriptionInterface::kPrAnswer,
770 sdp, NULL);
771 EXPECT_TRUE(DoSetLocalDescription(pr_answer));
772 EXPECT_EQ(PeerConnectionInterface::kHaveLocalPrAnswer, observer_.state_);
773 }
774
775 void CreateOfferReceiveAnswer() {
776 CreateOfferAsLocalDescription();
777 std::string sdp;
778 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
779 CreateAnswerAsRemoteDescription(sdp);
780 }
781
782 void CreateOfferAsLocalDescription() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000783 rtc::scoped_ptr<SessionDescriptionInterface> offer;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000784 ASSERT_TRUE(DoCreateOffer(offer.use()));
785 // TODO(perkj): Currently SetLocalDescription fails if any parameters in an
786 // audio codec change, even if the parameter has nothing to do with
787 // receiving. Not all parameters are serialized to SDP.
788 // Since CreatePrAnswerAsLocalDescription serialize/deserialize
789 // the SessionDescription, it is necessary to do that here to in order to
790 // get ReceiveOfferCreatePrAnswerAndAnswer and RenegotiateAudioOnly to pass.
791 // https://code.google.com/p/webrtc/issues/detail?id=1356
792 std::string sdp;
793 EXPECT_TRUE(offer->ToString(&sdp));
794 SessionDescriptionInterface* new_offer =
795 webrtc::CreateSessionDescription(
796 SessionDescriptionInterface::kOffer,
797 sdp, NULL);
798
799 EXPECT_TRUE(DoSetLocalDescription(new_offer));
800 EXPECT_EQ(PeerConnectionInterface::kHaveLocalOffer, observer_.state_);
mallinath@webrtc.org68cbd012014-01-22 00:16:46 +0000801 // Wait for the ice_complete message, so that SDP will have candidates.
802 EXPECT_TRUE_WAIT(observer_.ice_complete_, kTimeout);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000803 }
804
deadbeefab9b2d12015-10-14 11:33:11 -0700805 void CreateAnswerAsRemoteDescription(const std::string& sdp) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000806 webrtc::JsepSessionDescription* answer = new webrtc::JsepSessionDescription(
807 SessionDescriptionInterface::kAnswer);
deadbeefab9b2d12015-10-14 11:33:11 -0700808 EXPECT_TRUE(answer->Initialize(sdp, NULL));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000809 EXPECT_TRUE(DoSetRemoteDescription(answer));
810 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
811 }
812
deadbeefab9b2d12015-10-14 11:33:11 -0700813 void CreatePrAnswerAndAnswerAsRemoteDescription(const std::string& sdp) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000814 webrtc::JsepSessionDescription* pr_answer =
815 new webrtc::JsepSessionDescription(
816 SessionDescriptionInterface::kPrAnswer);
deadbeefab9b2d12015-10-14 11:33:11 -0700817 EXPECT_TRUE(pr_answer->Initialize(sdp, NULL));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000818 EXPECT_TRUE(DoSetRemoteDescription(pr_answer));
819 EXPECT_EQ(PeerConnectionInterface::kHaveRemotePrAnswer, observer_.state_);
820 webrtc::JsepSessionDescription* answer =
821 new webrtc::JsepSessionDescription(
822 SessionDescriptionInterface::kAnswer);
deadbeefab9b2d12015-10-14 11:33:11 -0700823 EXPECT_TRUE(answer->Initialize(sdp, NULL));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000824 EXPECT_TRUE(DoSetRemoteDescription(answer));
825 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
826 }
827
828 // Help function used for waiting until a the last signaled remote stream has
829 // the same label as |stream_label|. In a few of the tests in this file we
830 // answer with the same session description as we offer and thus we can
831 // check if OnAddStream have been called with the same stream as we offer to
832 // send.
833 void WaitAndVerifyOnAddStream(const std::string& stream_label) {
834 EXPECT_EQ_WAIT(stream_label, observer_.GetLastAddedStreamLabel(), kTimeout);
835 }
836
837 // Creates an offer and applies it as a local session description.
838 // Creates an answer with the same SDP an the offer but removes all lines
839 // that start with a:ssrc"
840 void CreateOfferReceiveAnswerWithoutSsrc() {
841 CreateOfferAsLocalDescription();
842 std::string sdp;
843 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
844 SetSsrcToZero(&sdp);
845 CreateAnswerAsRemoteDescription(sdp);
846 }
847
deadbeefab9b2d12015-10-14 11:33:11 -0700848 // This function creates a MediaStream with label kStreams[0] and
849 // |number_of_audio_tracks| and |number_of_video_tracks| tracks and the
850 // corresponding SessionDescriptionInterface. The SessionDescriptionInterface
851 // is returned in |desc| and the MediaStream is stored in
852 // |reference_collection_|
853 void CreateSessionDescriptionAndReference(
854 size_t number_of_audio_tracks,
855 size_t number_of_video_tracks,
856 SessionDescriptionInterface** desc) {
857 ASSERT_TRUE(desc != nullptr);
858 ASSERT_LE(number_of_audio_tracks, 2u);
859 ASSERT_LE(number_of_video_tracks, 2u);
860
861 reference_collection_ = StreamCollection::Create();
862 std::string sdp_ms1 = std::string(kSdpStringInit);
863
864 std::string mediastream_label = kStreams[0];
865
866 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream(
867 webrtc::MediaStream::Create(mediastream_label));
868 reference_collection_->AddStream(stream);
869
870 if (number_of_audio_tracks > 0) {
871 sdp_ms1 += std::string(kSdpStringAudio);
872 sdp_ms1 += std::string(kSdpStringMs1Audio0);
873 AddAudioTrack(kAudioTracks[0], stream);
874 }
875 if (number_of_audio_tracks > 1) {
876 sdp_ms1 += kSdpStringMs1Audio1;
877 AddAudioTrack(kAudioTracks[1], stream);
878 }
879
880 if (number_of_video_tracks > 0) {
881 sdp_ms1 += std::string(kSdpStringVideo);
882 sdp_ms1 += std::string(kSdpStringMs1Video0);
883 AddVideoTrack(kVideoTracks[0], stream);
884 }
885 if (number_of_video_tracks > 1) {
886 sdp_ms1 += kSdpStringMs1Video1;
887 AddVideoTrack(kVideoTracks[1], stream);
888 }
889
890 *desc = webrtc::CreateSessionDescription(
891 SessionDescriptionInterface::kOffer, sdp_ms1, nullptr);
892 }
893
894 void AddAudioTrack(const std::string& track_id,
895 MediaStreamInterface* stream) {
896 rtc::scoped_refptr<webrtc::AudioTrackInterface> audio_track(
897 webrtc::AudioTrack::Create(track_id, nullptr));
898 ASSERT_TRUE(stream->AddTrack(audio_track));
899 }
900
901 void AddVideoTrack(const std::string& track_id,
902 MediaStreamInterface* stream) {
903 rtc::scoped_refptr<webrtc::VideoTrackInterface> video_track(
904 webrtc::VideoTrack::Create(track_id, nullptr));
905 ASSERT_TRUE(stream->AddTrack(video_track));
906 }
907
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000908 scoped_refptr<FakePortAllocatorFactory> port_allocator_factory_;
909 scoped_refptr<webrtc::PeerConnectionFactoryInterface> pc_factory_;
910 scoped_refptr<PeerConnectionInterface> pc_;
911 MockPeerConnectionObserver observer_;
deadbeefab9b2d12015-10-14 11:33:11 -0700912 rtc::scoped_refptr<StreamCollection> reference_collection_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000913};
914
915TEST_F(PeerConnectionInterfaceTest,
916 CreatePeerConnectionWithDifferentConfigurations) {
917 CreatePeerConnectionWithDifferentConfigurations();
918}
919
920TEST_F(PeerConnectionInterfaceTest, AddStreams) {
921 CreatePeerConnection();
deadbeefab9b2d12015-10-14 11:33:11 -0700922 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000923 AddVoiceStream(kStreamLabel2);
924 ASSERT_EQ(2u, pc_->local_streams()->count());
925
wu@webrtc.org9dba5252013-08-05 20:36:57 +0000926 // Test we can add multiple local streams to one peerconnection.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000927 scoped_refptr<MediaStreamInterface> stream(
928 pc_factory_->CreateLocalMediaStream(kStreamLabel3));
929 scoped_refptr<AudioTrackInterface> audio_track(
930 pc_factory_->CreateAudioTrack(
931 kStreamLabel3, static_cast<AudioSourceInterface*>(NULL)));
932 stream->AddTrack(audio_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +0000933 EXPECT_TRUE(pc_->AddStream(stream));
wu@webrtc.org9dba5252013-08-05 20:36:57 +0000934 EXPECT_EQ(3u, pc_->local_streams()->count());
935
936 // Remove the third stream.
937 pc_->RemoveStream(pc_->local_streams()->at(2));
938 EXPECT_EQ(2u, pc_->local_streams()->count());
939
940 // Remove the second stream.
941 pc_->RemoveStream(pc_->local_streams()->at(1));
942 EXPECT_EQ(1u, pc_->local_streams()->count());
943
944 // Remove the first stream.
945 pc_->RemoveStream(pc_->local_streams()->at(0));
946 EXPECT_EQ(0u, pc_->local_streams()->count());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000947}
948
deadbeefab9b2d12015-10-14 11:33:11 -0700949// Test that the created offer includes streams we added.
950TEST_F(PeerConnectionInterfaceTest, AddedStreamsPresentInOffer) {
951 CreatePeerConnection();
952 AddAudioVideoStream(kStreamLabel1, "audio_track", "video_track");
953 scoped_ptr<SessionDescriptionInterface> offer;
954 ASSERT_TRUE(DoCreateOffer(offer.accept()));
955
956 const cricket::ContentInfo* audio_content =
957 cricket::GetFirstAudioContent(offer->description());
958 const cricket::AudioContentDescription* audio_desc =
959 static_cast<const cricket::AudioContentDescription*>(
960 audio_content->description);
961 EXPECT_TRUE(
962 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
963
964 const cricket::ContentInfo* video_content =
965 cricket::GetFirstVideoContent(offer->description());
966 const cricket::VideoContentDescription* video_desc =
967 static_cast<const cricket::VideoContentDescription*>(
968 video_content->description);
969 EXPECT_TRUE(
970 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
971
972 // Add another stream and ensure the offer includes both the old and new
973 // streams.
974 AddAudioVideoStream(kStreamLabel2, "audio_track2", "video_track2");
975 ASSERT_TRUE(DoCreateOffer(offer.accept()));
976
977 audio_content = cricket::GetFirstAudioContent(offer->description());
978 audio_desc = static_cast<const cricket::AudioContentDescription*>(
979 audio_content->description);
980 EXPECT_TRUE(
981 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
982 EXPECT_TRUE(
983 ContainsTrack(audio_desc->streams(), kStreamLabel2, "audio_track2"));
984
985 video_content = cricket::GetFirstVideoContent(offer->description());
986 video_desc = static_cast<const cricket::VideoContentDescription*>(
987 video_content->description);
988 EXPECT_TRUE(
989 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
990 EXPECT_TRUE(
991 ContainsTrack(video_desc->streams(), kStreamLabel2, "video_track2"));
992}
993
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000994TEST_F(PeerConnectionInterfaceTest, RemoveStream) {
995 CreatePeerConnection();
deadbeefab9b2d12015-10-14 11:33:11 -0700996 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000997 ASSERT_EQ(1u, pc_->local_streams()->count());
998 pc_->RemoveStream(pc_->local_streams()->at(0));
999 EXPECT_EQ(0u, pc_->local_streams()->count());
1000}
1001
1002TEST_F(PeerConnectionInterfaceTest, CreateOfferReceiveAnswer) {
1003 InitiateCall();
1004 WaitAndVerifyOnAddStream(kStreamLabel1);
1005 VerifyRemoteRtpHeaderExtensions();
1006}
1007
1008TEST_F(PeerConnectionInterfaceTest, CreateOfferReceivePrAnswerAndAnswer) {
1009 CreatePeerConnection();
deadbeefab9b2d12015-10-14 11:33:11 -07001010 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001011 CreateOfferAsLocalDescription();
1012 std::string offer;
1013 EXPECT_TRUE(pc_->local_description()->ToString(&offer));
1014 CreatePrAnswerAndAnswerAsRemoteDescription(offer);
1015 WaitAndVerifyOnAddStream(kStreamLabel1);
1016}
1017
1018TEST_F(PeerConnectionInterfaceTest, ReceiveOfferCreateAnswer) {
1019 CreatePeerConnection();
deadbeefab9b2d12015-10-14 11:33:11 -07001020 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001021
1022 CreateOfferAsRemoteDescription();
1023 CreateAnswerAsLocalDescription();
1024
1025 WaitAndVerifyOnAddStream(kStreamLabel1);
1026}
1027
1028TEST_F(PeerConnectionInterfaceTest, ReceiveOfferCreatePrAnswerAndAnswer) {
1029 CreatePeerConnection();
deadbeefab9b2d12015-10-14 11:33:11 -07001030 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001031
1032 CreateOfferAsRemoteDescription();
1033 CreatePrAnswerAsLocalDescription();
1034 CreateAnswerAsLocalDescription();
1035
1036 WaitAndVerifyOnAddStream(kStreamLabel1);
1037}
1038
1039TEST_F(PeerConnectionInterfaceTest, Renegotiate) {
1040 InitiateCall();
1041 ASSERT_EQ(1u, pc_->remote_streams()->count());
1042 pc_->RemoveStream(pc_->local_streams()->at(0));
1043 CreateOfferReceiveAnswer();
1044 EXPECT_EQ(0u, pc_->remote_streams()->count());
deadbeefab9b2d12015-10-14 11:33:11 -07001045 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001046 CreateOfferReceiveAnswer();
1047}
1048
1049// Tests that after negotiating an audio only call, the respondent can perform a
1050// renegotiation that removes the audio stream.
1051TEST_F(PeerConnectionInterfaceTest, RenegotiateAudioOnly) {
1052 CreatePeerConnection();
1053 AddVoiceStream(kStreamLabel1);
1054 CreateOfferAsRemoteDescription();
1055 CreateAnswerAsLocalDescription();
1056
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());
1061}
1062
1063// Test that candidates are generated and that we can parse our own candidates.
1064TEST_F(PeerConnectionInterfaceTest, IceCandidates) {
1065 CreatePeerConnection();
1066
1067 EXPECT_FALSE(pc_->AddIceCandidate(observer_.last_candidate_.get()));
1068 // SetRemoteDescription takes ownership of offer.
1069 SessionDescriptionInterface* offer = NULL;
deadbeefab9b2d12015-10-14 11:33:11 -07001070 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001071 EXPECT_TRUE(DoCreateOffer(&offer));
1072 EXPECT_TRUE(DoSetRemoteDescription(offer));
1073
1074 // SetLocalDescription takes ownership of answer.
1075 SessionDescriptionInterface* answer = NULL;
1076 EXPECT_TRUE(DoCreateAnswer(&answer));
1077 EXPECT_TRUE(DoSetLocalDescription(answer));
1078
1079 EXPECT_TRUE_WAIT(observer_.last_candidate_.get() != NULL, kTimeout);
1080 EXPECT_TRUE_WAIT(observer_.ice_complete_, kTimeout);
1081
1082 EXPECT_TRUE(pc_->AddIceCandidate(observer_.last_candidate_.get()));
1083}
1084
deadbeefab9b2d12015-10-14 11:33:11 -07001085// Test that CreateOffer and CreateAnswer will fail if the track labels are
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001086// not unique.
1087TEST_F(PeerConnectionInterfaceTest, CreateOfferAnswerWithInvalidStream) {
1088 CreatePeerConnection();
1089 // Create a regular offer for the CreateAnswer test later.
1090 SessionDescriptionInterface* offer = NULL;
1091 EXPECT_TRUE(DoCreateOffer(&offer));
1092 EXPECT_TRUE(offer != NULL);
1093 delete offer;
1094 offer = NULL;
1095
1096 // Create a local stream with audio&video tracks having same label.
1097 AddAudioVideoStream(kStreamLabel1, "track_label", "track_label");
1098
1099 // Test CreateOffer
1100 EXPECT_FALSE(DoCreateOffer(&offer));
1101
1102 // Test CreateAnswer
1103 SessionDescriptionInterface* answer = NULL;
1104 EXPECT_FALSE(DoCreateAnswer(&answer));
1105}
1106
1107// Test that we will get different SSRCs for each tracks in the offer and answer
1108// we created.
1109TEST_F(PeerConnectionInterfaceTest, SsrcInOfferAnswer) {
1110 CreatePeerConnection();
1111 // Create a local stream with audio&video tracks having different labels.
1112 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
1113
1114 // Test CreateOffer
1115 scoped_ptr<SessionDescriptionInterface> offer;
pkasting@chromium.org005b6ff2015-01-30 19:41:42 +00001116 ASSERT_TRUE(DoCreateOffer(offer.use()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001117 int audio_ssrc = 0;
1118 int video_ssrc = 0;
1119 EXPECT_TRUE(GetFirstSsrc(GetFirstAudioContent(offer->description()),
1120 &audio_ssrc));
1121 EXPECT_TRUE(GetFirstSsrc(GetFirstVideoContent(offer->description()),
1122 &video_ssrc));
1123 EXPECT_NE(audio_ssrc, video_ssrc);
1124
1125 // Test CreateAnswer
1126 EXPECT_TRUE(DoSetRemoteDescription(offer.release()));
1127 scoped_ptr<SessionDescriptionInterface> answer;
pkasting@chromium.org005b6ff2015-01-30 19:41:42 +00001128 ASSERT_TRUE(DoCreateAnswer(answer.use()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001129 audio_ssrc = 0;
1130 video_ssrc = 0;
1131 EXPECT_TRUE(GetFirstSsrc(GetFirstAudioContent(answer->description()),
1132 &audio_ssrc));
1133 EXPECT_TRUE(GetFirstSsrc(GetFirstVideoContent(answer->description()),
1134 &video_ssrc));
1135 EXPECT_NE(audio_ssrc, video_ssrc);
1136}
1137
1138// Test that we can specify a certain track that we want statistics about.
1139TEST_F(PeerConnectionInterfaceTest, GetStatsForSpecificTrack) {
1140 InitiateCall();
1141 ASSERT_LT(0u, pc_->remote_streams()->count());
1142 ASSERT_LT(0u, pc_->remote_streams()->at(0)->GetAudioTracks().size());
1143 scoped_refptr<MediaStreamTrackInterface> remote_audio =
1144 pc_->remote_streams()->at(0)->GetAudioTracks()[0];
1145 EXPECT_TRUE(DoGetStats(remote_audio));
1146
1147 // Remove the stream. Since we are sending to our selves the local
1148 // and the remote stream is the same.
1149 pc_->RemoveStream(pc_->local_streams()->at(0));
1150 // Do a re-negotiation.
1151 CreateOfferReceiveAnswer();
1152
1153 ASSERT_EQ(0u, pc_->remote_streams()->count());
1154
1155 // Test that we still can get statistics for the old track. Even if it is not
1156 // sent any longer.
1157 EXPECT_TRUE(DoGetStats(remote_audio));
1158}
1159
1160// Test that we can get stats on a video track.
1161TEST_F(PeerConnectionInterfaceTest, GetStatsForVideoTrack) {
1162 InitiateCall();
1163 ASSERT_LT(0u, pc_->remote_streams()->count());
1164 ASSERT_LT(0u, pc_->remote_streams()->at(0)->GetVideoTracks().size());
1165 scoped_refptr<MediaStreamTrackInterface> remote_video =
1166 pc_->remote_streams()->at(0)->GetVideoTracks()[0];
1167 EXPECT_TRUE(DoGetStats(remote_video));
1168}
1169
1170// Test that we don't get statistics for an invalid track.
tommi@webrtc.org908f57e2014-07-21 11:44:39 +00001171// TODO(tommi): Fix this test. DoGetStats will return true
1172// for the unknown track (since GetStats is async), but no
1173// data is returned for the track.
1174TEST_F(PeerConnectionInterfaceTest, DISABLED_GetStatsForInvalidTrack) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001175 InitiateCall();
1176 scoped_refptr<AudioTrackInterface> unknown_audio_track(
1177 pc_factory_->CreateAudioTrack("unknown track", NULL));
1178 EXPECT_FALSE(DoGetStats(unknown_audio_track));
1179}
1180
1181// This test setup two RTP data channels in loop back.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001182TEST_F(PeerConnectionInterfaceTest, TestDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001183 FakeConstraints constraints;
1184 constraints.SetAllowRtpDataChannels();
1185 CreatePeerConnection(&constraints);
1186 scoped_refptr<DataChannelInterface> data1 =
1187 pc_->CreateDataChannel("test1", NULL);
1188 scoped_refptr<DataChannelInterface> data2 =
1189 pc_->CreateDataChannel("test2", NULL);
1190 ASSERT_TRUE(data1 != NULL);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001191 rtc::scoped_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001192 new MockDataChannelObserver(data1));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001193 rtc::scoped_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001194 new MockDataChannelObserver(data2));
1195
1196 EXPECT_EQ(DataChannelInterface::kConnecting, data1->state());
1197 EXPECT_EQ(DataChannelInterface::kConnecting, data2->state());
1198 std::string data_to_send1 = "testing testing";
1199 std::string data_to_send2 = "testing something else";
1200 EXPECT_FALSE(data1->Send(DataBuffer(data_to_send1)));
1201
1202 CreateOfferReceiveAnswer();
1203 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1204 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
1205
1206 EXPECT_EQ(DataChannelInterface::kOpen, data1->state());
1207 EXPECT_EQ(DataChannelInterface::kOpen, data2->state());
1208 EXPECT_TRUE(data1->Send(DataBuffer(data_to_send1)));
1209 EXPECT_TRUE(data2->Send(DataBuffer(data_to_send2)));
1210
1211 EXPECT_EQ_WAIT(data_to_send1, observer1->last_message(), kTimeout);
1212 EXPECT_EQ_WAIT(data_to_send2, observer2->last_message(), kTimeout);
1213
1214 data1->Close();
1215 EXPECT_EQ(DataChannelInterface::kClosing, data1->state());
1216 CreateOfferReceiveAnswer();
1217 EXPECT_FALSE(observer1->IsOpen());
1218 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
1219 EXPECT_TRUE(observer2->IsOpen());
1220
1221 data_to_send2 = "testing something else again";
1222 EXPECT_TRUE(data2->Send(DataBuffer(data_to_send2)));
1223
1224 EXPECT_EQ_WAIT(data_to_send2, observer2->last_message(), kTimeout);
1225}
1226
1227// This test verifies that sendnig binary data over RTP data channels should
1228// fail.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001229TEST_F(PeerConnectionInterfaceTest, TestSendBinaryOnRtpDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001230 FakeConstraints constraints;
1231 constraints.SetAllowRtpDataChannels();
1232 CreatePeerConnection(&constraints);
1233 scoped_refptr<DataChannelInterface> data1 =
1234 pc_->CreateDataChannel("test1", NULL);
1235 scoped_refptr<DataChannelInterface> data2 =
1236 pc_->CreateDataChannel("test2", NULL);
1237 ASSERT_TRUE(data1 != NULL);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001238 rtc::scoped_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001239 new MockDataChannelObserver(data1));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001240 rtc::scoped_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001241 new MockDataChannelObserver(data2));
1242
1243 EXPECT_EQ(DataChannelInterface::kConnecting, data1->state());
1244 EXPECT_EQ(DataChannelInterface::kConnecting, data2->state());
1245
1246 CreateOfferReceiveAnswer();
1247 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1248 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
1249
1250 EXPECT_EQ(DataChannelInterface::kOpen, data1->state());
1251 EXPECT_EQ(DataChannelInterface::kOpen, data2->state());
1252
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001253 rtc::Buffer buffer("test", 4);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001254 EXPECT_FALSE(data1->Send(DataBuffer(buffer, true)));
1255}
1256
1257// This test setup a RTP data channels in loop back and test that a channel is
1258// opened even if the remote end answer with a zero SSRC.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001259TEST_F(PeerConnectionInterfaceTest, TestSendOnlyDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001260 FakeConstraints constraints;
1261 constraints.SetAllowRtpDataChannels();
1262 CreatePeerConnection(&constraints);
1263 scoped_refptr<DataChannelInterface> data1 =
1264 pc_->CreateDataChannel("test1", NULL);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001265 rtc::scoped_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001266 new MockDataChannelObserver(data1));
1267
1268 CreateOfferReceiveAnswerWithoutSsrc();
1269
1270 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1271
1272 data1->Close();
1273 EXPECT_EQ(DataChannelInterface::kClosing, data1->state());
1274 CreateOfferReceiveAnswerWithoutSsrc();
1275 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
1276 EXPECT_FALSE(observer1->IsOpen());
1277}
1278
1279// This test that if a data channel is added in an answer a receive only channel
1280// channel is created.
1281TEST_F(PeerConnectionInterfaceTest, TestReceiveOnlyDataChannel) {
1282 FakeConstraints constraints;
1283 constraints.SetAllowRtpDataChannels();
1284 CreatePeerConnection(&constraints);
1285
1286 std::string offer_label = "offer_channel";
1287 scoped_refptr<DataChannelInterface> offer_channel =
1288 pc_->CreateDataChannel(offer_label, NULL);
1289
1290 CreateOfferAsLocalDescription();
1291
1292 // Replace the data channel label in the offer and apply it as an answer.
1293 std::string receive_label = "answer_channel";
1294 std::string sdp;
1295 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001296 rtc::replace_substrs(offer_label.c_str(), offer_label.length(),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001297 receive_label.c_str(), receive_label.length(),
1298 &sdp);
1299 CreateAnswerAsRemoteDescription(sdp);
1300
1301 // Verify that a new incoming data channel has been created and that
1302 // it is open but can't we written to.
1303 ASSERT_TRUE(observer_.last_datachannel_ != NULL);
1304 DataChannelInterface* received_channel = observer_.last_datachannel_;
1305 EXPECT_EQ(DataChannelInterface::kConnecting, received_channel->state());
1306 EXPECT_EQ(receive_label, received_channel->label());
1307 EXPECT_FALSE(received_channel->Send(DataBuffer("something")));
1308
1309 // Verify that the channel we initially offered has been rejected.
1310 EXPECT_EQ(DataChannelInterface::kClosed, offer_channel->state());
1311
1312 // Do another offer / answer exchange and verify that the data channel is
1313 // opened.
1314 CreateOfferReceiveAnswer();
1315 EXPECT_EQ_WAIT(DataChannelInterface::kOpen, received_channel->state(),
1316 kTimeout);
1317}
1318
1319// This test that no data channel is returned if a reliable channel is
1320// requested.
1321// TODO(perkj): Remove this test once reliable channels are implemented.
1322TEST_F(PeerConnectionInterfaceTest, CreateReliableRtpDataChannelShouldFail) {
1323 FakeConstraints constraints;
1324 constraints.SetAllowRtpDataChannels();
1325 CreatePeerConnection(&constraints);
1326
1327 std::string label = "test";
1328 webrtc::DataChannelInit config;
1329 config.reliable = true;
1330 scoped_refptr<DataChannelInterface> channel =
1331 pc_->CreateDataChannel(label, &config);
1332 EXPECT_TRUE(channel == NULL);
1333}
1334
deadbeefab9b2d12015-10-14 11:33:11 -07001335// Verifies that duplicated label is not allowed for RTP data channel.
1336TEST_F(PeerConnectionInterfaceTest, RtpDuplicatedLabelNotAllowed) {
1337 FakeConstraints constraints;
1338 constraints.SetAllowRtpDataChannels();
1339 CreatePeerConnection(&constraints);
1340
1341 std::string label = "test";
1342 scoped_refptr<DataChannelInterface> channel =
1343 pc_->CreateDataChannel(label, nullptr);
1344 EXPECT_NE(channel, nullptr);
1345
1346 scoped_refptr<DataChannelInterface> dup_channel =
1347 pc_->CreateDataChannel(label, nullptr);
1348 EXPECT_EQ(dup_channel, nullptr);
1349}
1350
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001351// This tests that a SCTP data channel is returned using different
1352// DataChannelInit configurations.
1353TEST_F(PeerConnectionInterfaceTest, CreateSctpDataChannel) {
1354 FakeConstraints constraints;
1355 constraints.SetAllowDtlsSctpDataChannels();
1356 CreatePeerConnection(&constraints);
1357
1358 webrtc::DataChannelInit config;
1359
1360 scoped_refptr<DataChannelInterface> channel =
1361 pc_->CreateDataChannel("1", &config);
1362 EXPECT_TRUE(channel != NULL);
1363 EXPECT_TRUE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001364 EXPECT_TRUE(observer_.renegotiation_needed_);
1365 observer_.renegotiation_needed_ = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001366
1367 config.ordered = false;
1368 channel = pc_->CreateDataChannel("2", &config);
1369 EXPECT_TRUE(channel != NULL);
1370 EXPECT_TRUE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001371 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001372
1373 config.ordered = true;
1374 config.maxRetransmits = 0;
1375 channel = pc_->CreateDataChannel("3", &config);
1376 EXPECT_TRUE(channel != NULL);
1377 EXPECT_FALSE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001378 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001379
1380 config.maxRetransmits = -1;
1381 config.maxRetransmitTime = 0;
1382 channel = pc_->CreateDataChannel("4", &config);
1383 EXPECT_TRUE(channel != NULL);
1384 EXPECT_FALSE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001385 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001386}
1387
1388// This tests that no data channel is returned if both maxRetransmits and
1389// maxRetransmitTime are set for SCTP data channels.
1390TEST_F(PeerConnectionInterfaceTest,
1391 CreateSctpDataChannelShouldFailForInvalidConfig) {
1392 FakeConstraints constraints;
1393 constraints.SetAllowDtlsSctpDataChannels();
1394 CreatePeerConnection(&constraints);
1395
1396 std::string label = "test";
1397 webrtc::DataChannelInit config;
1398 config.maxRetransmits = 0;
1399 config.maxRetransmitTime = 0;
1400
1401 scoped_refptr<DataChannelInterface> channel =
1402 pc_->CreateDataChannel(label, &config);
1403 EXPECT_TRUE(channel == NULL);
1404}
1405
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001406// The test verifies that creating a SCTP data channel with an id already in use
1407// or out of range should fail.
1408TEST_F(PeerConnectionInterfaceTest,
1409 CreateSctpDataChannelWithInvalidIdShouldFail) {
1410 FakeConstraints constraints;
1411 constraints.SetAllowDtlsSctpDataChannels();
1412 CreatePeerConnection(&constraints);
1413
1414 webrtc::DataChannelInit config;
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001415 scoped_refptr<DataChannelInterface> channel;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001416
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001417 config.id = 1;
1418 channel = pc_->CreateDataChannel("1", &config);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001419 EXPECT_TRUE(channel != NULL);
1420 EXPECT_EQ(1, channel->id());
1421
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001422 channel = pc_->CreateDataChannel("x", &config);
1423 EXPECT_TRUE(channel == NULL);
1424
1425 config.id = cricket::kMaxSctpSid;
1426 channel = pc_->CreateDataChannel("max", &config);
1427 EXPECT_TRUE(channel != NULL);
1428 EXPECT_EQ(config.id, channel->id());
1429
1430 config.id = cricket::kMaxSctpSid + 1;
1431 channel = pc_->CreateDataChannel("x", &config);
1432 EXPECT_TRUE(channel == NULL);
1433}
1434
deadbeefab9b2d12015-10-14 11:33:11 -07001435// Verifies that duplicated label is allowed for SCTP data channel.
1436TEST_F(PeerConnectionInterfaceTest, SctpDuplicatedLabelAllowed) {
1437 FakeConstraints constraints;
1438 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
1439 true);
1440 CreatePeerConnection(&constraints);
1441
1442 std::string label = "test";
1443 scoped_refptr<DataChannelInterface> channel =
1444 pc_->CreateDataChannel(label, nullptr);
1445 EXPECT_NE(channel, nullptr);
1446
1447 scoped_refptr<DataChannelInterface> dup_channel =
1448 pc_->CreateDataChannel(label, nullptr);
1449 EXPECT_NE(dup_channel, nullptr);
1450}
1451
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001452// This test verifies that OnRenegotiationNeeded is fired for every new RTP
1453// DataChannel.
1454TEST_F(PeerConnectionInterfaceTest, RenegotiationNeededForNewRtpDataChannel) {
1455 FakeConstraints constraints;
1456 constraints.SetAllowRtpDataChannels();
1457 CreatePeerConnection(&constraints);
1458
1459 scoped_refptr<DataChannelInterface> dc1 =
1460 pc_->CreateDataChannel("test1", NULL);
1461 EXPECT_TRUE(observer_.renegotiation_needed_);
1462 observer_.renegotiation_needed_ = false;
1463
1464 scoped_refptr<DataChannelInterface> dc2 =
1465 pc_->CreateDataChannel("test2", NULL);
1466 EXPECT_TRUE(observer_.renegotiation_needed_);
1467}
1468
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001469// This test that a data channel closes when a PeerConnection is deleted/closed.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001470TEST_F(PeerConnectionInterfaceTest, DataChannelCloseWhenPeerConnectionClose) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001471 FakeConstraints constraints;
1472 constraints.SetAllowRtpDataChannels();
1473 CreatePeerConnection(&constraints);
1474
1475 scoped_refptr<DataChannelInterface> data1 =
1476 pc_->CreateDataChannel("test1", NULL);
1477 scoped_refptr<DataChannelInterface> data2 =
1478 pc_->CreateDataChannel("test2", NULL);
1479 ASSERT_TRUE(data1 != NULL);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001480 rtc::scoped_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001481 new MockDataChannelObserver(data1));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001482 rtc::scoped_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001483 new MockDataChannelObserver(data2));
1484
1485 CreateOfferReceiveAnswer();
1486 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1487 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
1488
1489 ReleasePeerConnection();
1490 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
1491 EXPECT_EQ(DataChannelInterface::kClosed, data2->state());
1492}
1493
1494// This test that data channels can be rejected in an answer.
1495TEST_F(PeerConnectionInterfaceTest, TestRejectDataChannelInAnswer) {
1496 FakeConstraints constraints;
1497 constraints.SetAllowRtpDataChannels();
1498 CreatePeerConnection(&constraints);
1499
1500 scoped_refptr<DataChannelInterface> offer_channel(
1501 pc_->CreateDataChannel("offer_channel", NULL));
1502
1503 CreateOfferAsLocalDescription();
1504
1505 // Create an answer where the m-line for data channels are rejected.
1506 std::string sdp;
1507 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
1508 webrtc::JsepSessionDescription* answer = new webrtc::JsepSessionDescription(
1509 SessionDescriptionInterface::kAnswer);
1510 EXPECT_TRUE(answer->Initialize(sdp, NULL));
1511 cricket::ContentInfo* data_info =
1512 answer->description()->GetContentByName("data");
1513 data_info->rejected = true;
1514
1515 DoSetRemoteDescription(answer);
1516 EXPECT_EQ(DataChannelInterface::kClosed, offer_channel->state());
1517}
1518
1519// Test that we can create a session description from an SDP string from
1520// FireFox, use it as a remote session description, generate an answer and use
1521// the answer as a local description.
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00001522TEST_F(PeerConnectionInterfaceTest, ReceiveFireFoxOffer) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001523 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001524 FakeConstraints constraints;
1525 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
1526 true);
1527 CreatePeerConnection(&constraints);
1528 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
1529 SessionDescriptionInterface* desc =
1530 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
jbauchfabe2c92015-07-16 13:43:14 -07001531 webrtc::kFireFoxSdpOffer, nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001532 EXPECT_TRUE(DoSetSessionDescription(desc, false));
1533 CreateAnswerAsLocalDescription();
1534 ASSERT_TRUE(pc_->local_description() != NULL);
1535 ASSERT_TRUE(pc_->remote_description() != NULL);
1536
1537 const cricket::ContentInfo* content =
1538 cricket::GetFirstAudioContent(pc_->local_description()->description());
1539 ASSERT_TRUE(content != NULL);
1540 EXPECT_FALSE(content->rejected);
1541
1542 content =
1543 cricket::GetFirstVideoContent(pc_->local_description()->description());
1544 ASSERT_TRUE(content != NULL);
1545 EXPECT_FALSE(content->rejected);
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00001546#ifdef HAVE_SCTP
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001547 content =
1548 cricket::GetFirstDataContent(pc_->local_description()->description());
1549 ASSERT_TRUE(content != NULL);
1550 EXPECT_TRUE(content->rejected);
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00001551#endif
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001552}
1553
1554// Test that we can create an audio only offer and receive an answer with a
1555// limited set of audio codecs and receive an updated offer with more audio
1556// codecs, where the added codecs are not supported.
1557TEST_F(PeerConnectionInterfaceTest, ReceiveUpdatedAudioOfferWithBadCodecs) {
1558 CreatePeerConnection();
1559 AddVoiceStream("audio_label");
1560 CreateOfferAsLocalDescription();
1561
1562 SessionDescriptionInterface* answer =
1563 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
jbauchfabe2c92015-07-16 13:43:14 -07001564 webrtc::kAudioSdp, nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001565 EXPECT_TRUE(DoSetSessionDescription(answer, false));
1566
1567 SessionDescriptionInterface* updated_offer =
1568 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
jbauchfabe2c92015-07-16 13:43:14 -07001569 webrtc::kAudioSdpWithUnsupportedCodecs,
1570 nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001571 EXPECT_TRUE(DoSetSessionDescription(updated_offer, false));
1572 CreateAnswerAsLocalDescription();
1573}
1574
1575// Test that PeerConnection::Close changes the states to closed and all remote
1576// tracks change state to ended.
1577TEST_F(PeerConnectionInterfaceTest, CloseAndTestStreamsAndStates) {
1578 // Initialize a PeerConnection and negotiate local and remote session
1579 // description.
1580 InitiateCall();
1581 ASSERT_EQ(1u, pc_->local_streams()->count());
1582 ASSERT_EQ(1u, pc_->remote_streams()->count());
1583
1584 pc_->Close();
1585
1586 EXPECT_EQ(PeerConnectionInterface::kClosed, pc_->signaling_state());
1587 EXPECT_EQ(PeerConnectionInterface::kIceConnectionClosed,
1588 pc_->ice_connection_state());
1589 EXPECT_EQ(PeerConnectionInterface::kIceGatheringComplete,
1590 pc_->ice_gathering_state());
1591
1592 EXPECT_EQ(1u, pc_->local_streams()->count());
1593 EXPECT_EQ(1u, pc_->remote_streams()->count());
1594
1595 scoped_refptr<MediaStreamInterface> remote_stream =
1596 pc_->remote_streams()->at(0);
1597 EXPECT_EQ(MediaStreamTrackInterface::kEnded,
1598 remote_stream->GetVideoTracks()[0]->state());
1599 EXPECT_EQ(MediaStreamTrackInterface::kEnded,
1600 remote_stream->GetAudioTracks()[0]->state());
1601}
1602
1603// Test that PeerConnection methods fails gracefully after
1604// PeerConnection::Close has been called.
1605TEST_F(PeerConnectionInterfaceTest, CloseAndTestMethods) {
1606 CreatePeerConnection();
1607 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
1608 CreateOfferAsRemoteDescription();
1609 CreateAnswerAsLocalDescription();
1610
1611 ASSERT_EQ(1u, pc_->local_streams()->count());
1612 scoped_refptr<MediaStreamInterface> local_stream =
1613 pc_->local_streams()->at(0);
1614
1615 pc_->Close();
1616
1617 pc_->RemoveStream(local_stream);
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +00001618 EXPECT_FALSE(pc_->AddStream(local_stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001619
1620 ASSERT_FALSE(local_stream->GetAudioTracks().empty());
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001621 rtc::scoped_refptr<webrtc::DtmfSenderInterface> dtmf_sender(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001622 pc_->CreateDtmfSender(local_stream->GetAudioTracks()[0]));
wu@webrtc.org66037362013-08-13 00:09:35 +00001623 EXPECT_TRUE(NULL == dtmf_sender); // local stream has been removed.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001624
1625 EXPECT_TRUE(pc_->CreateDataChannel("test", NULL) == NULL);
1626
1627 EXPECT_TRUE(pc_->local_description() != NULL);
1628 EXPECT_TRUE(pc_->remote_description() != NULL);
1629
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001630 rtc::scoped_ptr<SessionDescriptionInterface> offer;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001631 EXPECT_TRUE(DoCreateOffer(offer.use()));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001632 rtc::scoped_ptr<SessionDescriptionInterface> answer;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001633 EXPECT_TRUE(DoCreateAnswer(answer.use()));
1634
1635 std::string sdp;
1636 ASSERT_TRUE(pc_->remote_description()->ToString(&sdp));
1637 SessionDescriptionInterface* remote_offer =
1638 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
1639 sdp, NULL);
1640 EXPECT_FALSE(DoSetRemoteDescription(remote_offer));
1641
1642 ASSERT_TRUE(pc_->local_description()->ToString(&sdp));
1643 SessionDescriptionInterface* local_offer =
1644 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
1645 sdp, NULL);
1646 EXPECT_FALSE(DoSetLocalDescription(local_offer));
1647}
1648
1649// Test that GetStats can still be called after PeerConnection::Close.
1650TEST_F(PeerConnectionInterfaceTest, CloseAndGetStats) {
1651 InitiateCall();
1652 pc_->Close();
1653 DoGetStats(NULL);
1654}
deadbeefab9b2d12015-10-14 11:33:11 -07001655
1656// NOTE: The series of tests below come from what used to be
1657// mediastreamsignaling_unittest.cc, and are mostly aimed at testing that
1658// setting a remote or local description has the expected effects.
1659
1660// This test verifies that the remote MediaStreams corresponding to a received
1661// SDP string is created. In this test the two separate MediaStreams are
1662// signaled.
1663TEST_F(PeerConnectionInterfaceTest, UpdateRemoteStreams) {
1664 FakeConstraints constraints;
1665 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
1666 true);
1667 CreatePeerConnection(&constraints);
1668 CreateAndSetRemoteOffer(kSdpStringWithStream1);
1669
1670 rtc::scoped_refptr<StreamCollection> reference(CreateStreamCollection(1));
1671 EXPECT_TRUE(
1672 CompareStreamCollections(observer_.remote_streams(), reference.get()));
1673 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
1674 EXPECT_TRUE(remote_stream->GetVideoTracks()[0]->GetSource() != nullptr);
1675
1676 // Create a session description based on another SDP with another
1677 // MediaStream.
1678 CreateAndSetRemoteOffer(kSdpStringWithStream1And2);
1679
1680 rtc::scoped_refptr<StreamCollection> reference2(CreateStreamCollection(2));
1681 EXPECT_TRUE(
1682 CompareStreamCollections(observer_.remote_streams(), reference2.get()));
1683}
1684
1685// This test verifies that when remote tracks are added/removed from SDP, the
1686// created remote streams are updated appropriately.
1687TEST_F(PeerConnectionInterfaceTest,
1688 AddRemoveTrackFromExistingRemoteMediaStream) {
1689 FakeConstraints constraints;
1690 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
1691 true);
1692 CreatePeerConnection(&constraints);
1693 rtc::scoped_ptr<SessionDescriptionInterface> desc_ms1;
1694 CreateSessionDescriptionAndReference(1, 1, desc_ms1.accept());
1695 EXPECT_TRUE(DoSetRemoteDescription(desc_ms1.release()));
1696 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
1697 reference_collection_));
1698
1699 // Add extra audio and video tracks to the same MediaStream.
1700 rtc::scoped_ptr<SessionDescriptionInterface> desc_ms1_two_tracks;
1701 CreateSessionDescriptionAndReference(2, 2, desc_ms1_two_tracks.accept());
1702 EXPECT_TRUE(DoSetRemoteDescription(desc_ms1_two_tracks.release()));
1703 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
1704 reference_collection_));
1705
1706 // Remove the extra audio and video tracks.
1707 rtc::scoped_ptr<SessionDescriptionInterface> desc_ms2;
1708 CreateSessionDescriptionAndReference(1, 1, desc_ms2.accept());
1709 EXPECT_TRUE(DoSetRemoteDescription(desc_ms2.release()));
1710 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
1711 reference_collection_));
1712}
1713
1714// This tests that remote tracks are ended if a local session description is set
1715// that rejects the media content type.
1716TEST_F(PeerConnectionInterfaceTest, RejectMediaContent) {
1717 FakeConstraints constraints;
1718 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
1719 true);
1720 CreatePeerConnection(&constraints);
1721 // First create and set a remote offer, then reject its video content in our
1722 // answer.
1723 CreateAndSetRemoteOffer(kSdpStringWithStream1);
1724 ASSERT_EQ(1u, observer_.remote_streams()->count());
1725 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
1726 ASSERT_EQ(1u, remote_stream->GetVideoTracks().size());
1727 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
1728
1729 rtc::scoped_refptr<webrtc::VideoTrackInterface> remote_video =
1730 remote_stream->GetVideoTracks()[0];
1731 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, remote_video->state());
1732 rtc::scoped_refptr<webrtc::AudioTrackInterface> remote_audio =
1733 remote_stream->GetAudioTracks()[0];
1734 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, remote_audio->state());
1735
1736 rtc::scoped_ptr<SessionDescriptionInterface> local_answer;
1737 EXPECT_TRUE(DoCreateAnswer(local_answer.accept()));
1738 cricket::ContentInfo* video_info =
1739 local_answer->description()->GetContentByName("video");
1740 video_info->rejected = true;
1741 EXPECT_TRUE(DoSetLocalDescription(local_answer.release()));
1742 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kEnded, remote_video->state());
1743 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, remote_audio->state());
1744
1745 // Now create an offer where we reject both video and audio.
1746 rtc::scoped_ptr<SessionDescriptionInterface> local_offer;
1747 EXPECT_TRUE(DoCreateOffer(local_offer.accept()));
1748 video_info = local_offer->description()->GetContentByName("video");
1749 ASSERT_TRUE(video_info != nullptr);
1750 video_info->rejected = true;
1751 cricket::ContentInfo* audio_info =
1752 local_offer->description()->GetContentByName("audio");
1753 ASSERT_TRUE(audio_info != nullptr);
1754 audio_info->rejected = true;
1755 EXPECT_TRUE(DoSetLocalDescription(local_offer.release()));
1756 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kEnded, remote_video->state());
1757 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kEnded, remote_audio->state());
1758}
1759
1760// This tests that we won't crash if the remote track has been removed outside
1761// of PeerConnection and then PeerConnection tries to reject the track.
1762TEST_F(PeerConnectionInterfaceTest, RemoveTrackThenRejectMediaContent) {
1763 FakeConstraints constraints;
1764 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
1765 true);
1766 CreatePeerConnection(&constraints);
1767 CreateAndSetRemoteOffer(kSdpStringWithStream1);
1768 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
1769 remote_stream->RemoveTrack(remote_stream->GetVideoTracks()[0]);
1770 remote_stream->RemoveTrack(remote_stream->GetAudioTracks()[0]);
1771
1772 rtc::scoped_ptr<SessionDescriptionInterface> local_answer(
1773 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
1774 kSdpStringWithStream1, nullptr));
1775 cricket::ContentInfo* video_info =
1776 local_answer->description()->GetContentByName("video");
1777 video_info->rejected = true;
1778 cricket::ContentInfo* audio_info =
1779 local_answer->description()->GetContentByName("audio");
1780 audio_info->rejected = true;
1781 EXPECT_TRUE(DoSetLocalDescription(local_answer.release()));
1782
1783 // No crash is a pass.
1784}
1785
1786// This tests that a default MediaStream is created if a remote session
1787// description doesn't contain any streams and no MSID support.
1788// It also tests that the default stream is updated if a video m-line is added
1789// in a subsequent session description.
1790TEST_F(PeerConnectionInterfaceTest, SdpWithoutMsidCreatesDefaultStream) {
1791 FakeConstraints constraints;
1792 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
1793 true);
1794 CreatePeerConnection(&constraints);
1795 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
1796
1797 ASSERT_EQ(1u, observer_.remote_streams()->count());
1798 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
1799
1800 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
1801 EXPECT_EQ(0u, remote_stream->GetVideoTracks().size());
1802 EXPECT_EQ("default", remote_stream->label());
1803
1804 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
1805 ASSERT_EQ(1u, observer_.remote_streams()->count());
1806 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
1807 EXPECT_EQ("defaulta0", remote_stream->GetAudioTracks()[0]->id());
1808 ASSERT_EQ(1u, remote_stream->GetVideoTracks().size());
1809 EXPECT_EQ("defaultv0", remote_stream->GetVideoTracks()[0]->id());
1810}
1811
1812// This tests that a default MediaStream is created if a remote session
1813// description doesn't contain any streams and media direction is send only.
1814TEST_F(PeerConnectionInterfaceTest,
1815 SendOnlySdpWithoutMsidCreatesDefaultStream) {
1816 FakeConstraints constraints;
1817 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
1818 true);
1819 CreatePeerConnection(&constraints);
1820 CreateAndSetRemoteOffer(kSdpStringSendOnlyWithoutStreams);
1821
1822 ASSERT_EQ(1u, observer_.remote_streams()->count());
1823 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
1824
1825 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
1826 EXPECT_EQ(1u, remote_stream->GetVideoTracks().size());
1827 EXPECT_EQ("default", remote_stream->label());
1828}
1829
1830// This tests that it won't crash when PeerConnection tries to remove
1831// a remote track that as already been removed from the MediaStream.
1832TEST_F(PeerConnectionInterfaceTest, RemoveAlreadyGoneRemoteStream) {
1833 FakeConstraints constraints;
1834 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
1835 true);
1836 CreatePeerConnection(&constraints);
1837 CreateAndSetRemoteOffer(kSdpStringWithStream1);
1838 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
1839 remote_stream->RemoveTrack(remote_stream->GetAudioTracks()[0]);
1840 remote_stream->RemoveTrack(remote_stream->GetVideoTracks()[0]);
1841
1842 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
1843
1844 // No crash is a pass.
1845}
1846
1847// This tests that a default MediaStream is created if the remote session
1848// description doesn't contain any streams and don't contain an indication if
1849// MSID is supported.
1850TEST_F(PeerConnectionInterfaceTest,
1851 SdpWithoutMsidAndStreamsCreatesDefaultStream) {
1852 FakeConstraints constraints;
1853 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
1854 true);
1855 CreatePeerConnection(&constraints);
1856 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
1857
1858 ASSERT_EQ(1u, observer_.remote_streams()->count());
1859 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
1860 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
1861 EXPECT_EQ(1u, remote_stream->GetVideoTracks().size());
1862}
1863
1864// This tests that a default MediaStream is not created if the remote session
1865// description doesn't contain any streams but does support MSID.
1866TEST_F(PeerConnectionInterfaceTest, SdpWithMsidDontCreatesDefaultStream) {
1867 FakeConstraints constraints;
1868 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
1869 true);
1870 CreatePeerConnection(&constraints);
1871 CreateAndSetRemoteOffer(kSdpStringWithMsidWithoutStreams);
1872 EXPECT_EQ(0u, observer_.remote_streams()->count());
1873}
1874
1875// This tests that a default MediaStream is not created if a remote session
1876// description is updated to not have any MediaStreams.
1877TEST_F(PeerConnectionInterfaceTest, VerifyDefaultStreamIsNotCreated) {
1878 FakeConstraints constraints;
1879 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
1880 true);
1881 CreatePeerConnection(&constraints);
1882 CreateAndSetRemoteOffer(kSdpStringWithStream1);
1883 rtc::scoped_refptr<StreamCollection> reference(CreateStreamCollection(1));
1884 EXPECT_TRUE(
1885 CompareStreamCollections(observer_.remote_streams(), reference.get()));
1886
1887 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
1888 EXPECT_EQ(0u, observer_.remote_streams()->count());
1889}
1890
1891// This tests that an RtpSender is created when the local description is set
1892// after adding a local stream.
1893// TODO(deadbeef): This test and the one below it need to be updated when
1894// an RtpSender's lifetime isn't determined by when a local description is set.
1895TEST_F(PeerConnectionInterfaceTest, LocalDescriptionChanged) {
1896 FakeConstraints constraints;
1897 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
1898 true);
1899 CreatePeerConnection(&constraints);
1900 // Create an offer just to ensure we have an identity before we manually
1901 // call SetLocalDescription.
1902 rtc::scoped_ptr<SessionDescriptionInterface> throwaway;
1903 ASSERT_TRUE(DoCreateOffer(throwaway.accept()));
1904
1905 rtc::scoped_ptr<SessionDescriptionInterface> desc_1;
1906 CreateSessionDescriptionAndReference(2, 2, desc_1.accept());
1907
1908 pc_->AddStream(reference_collection_->at(0));
1909 EXPECT_TRUE(DoSetLocalDescription(desc_1.release()));
1910 auto senders = pc_->GetSenders();
1911 EXPECT_EQ(4u, senders.size());
1912 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
1913 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
1914 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[1]));
1915 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[1]));
1916
1917 // Remove an audio and video track.
1918 rtc::scoped_ptr<SessionDescriptionInterface> desc_2;
1919 CreateSessionDescriptionAndReference(1, 1, desc_2.accept());
1920 EXPECT_TRUE(DoSetLocalDescription(desc_2.release()));
1921 senders = pc_->GetSenders();
1922 EXPECT_EQ(2u, senders.size());
1923 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
1924 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
1925 EXPECT_FALSE(ContainsSender(senders, kAudioTracks[1]));
1926 EXPECT_FALSE(ContainsSender(senders, kVideoTracks[1]));
1927}
1928
1929// This tests that an RtpSender is created when the local description is set
1930// before adding a local stream.
1931TEST_F(PeerConnectionInterfaceTest,
1932 AddLocalStreamAfterLocalDescriptionChanged) {
1933 FakeConstraints constraints;
1934 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
1935 true);
1936 CreatePeerConnection(&constraints);
1937 // Create an offer just to ensure we have an identity before we manually
1938 // call SetLocalDescription.
1939 rtc::scoped_ptr<SessionDescriptionInterface> throwaway;
1940 ASSERT_TRUE(DoCreateOffer(throwaway.accept()));
1941
1942 rtc::scoped_ptr<SessionDescriptionInterface> desc_1;
1943 CreateSessionDescriptionAndReference(2, 2, desc_1.accept());
1944
1945 EXPECT_TRUE(DoSetLocalDescription(desc_1.release()));
1946 auto senders = pc_->GetSenders();
1947 EXPECT_EQ(0u, senders.size());
1948
1949 pc_->AddStream(reference_collection_->at(0));
1950 senders = pc_->GetSenders();
1951 EXPECT_EQ(4u, senders.size());
1952 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
1953 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
1954 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[1]));
1955 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[1]));
1956}
1957
1958// This tests that the expected behavior occurs if the SSRC on a local track is
1959// changed when SetLocalDescription is called.
1960TEST_F(PeerConnectionInterfaceTest,
1961 ChangeSsrcOnTrackInLocalSessionDescription) {
1962 FakeConstraints constraints;
1963 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
1964 true);
1965 CreatePeerConnection(&constraints);
1966 // Create an offer just to ensure we have an identity before we manually
1967 // call SetLocalDescription.
1968 rtc::scoped_ptr<SessionDescriptionInterface> throwaway;
1969 ASSERT_TRUE(DoCreateOffer(throwaway.accept()));
1970
1971 rtc::scoped_ptr<SessionDescriptionInterface> desc;
1972 CreateSessionDescriptionAndReference(1, 1, desc.accept());
1973 std::string sdp;
1974 desc->ToString(&sdp);
1975
1976 pc_->AddStream(reference_collection_->at(0));
1977 EXPECT_TRUE(DoSetLocalDescription(desc.release()));
1978 auto senders = pc_->GetSenders();
1979 EXPECT_EQ(2u, senders.size());
1980 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
1981 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
1982
1983 // Change the ssrc of the audio and video track.
1984 std::string ssrc_org = "a=ssrc:1";
1985 std::string ssrc_to = "a=ssrc:97";
1986 rtc::replace_substrs(ssrc_org.c_str(), ssrc_org.length(), ssrc_to.c_str(),
1987 ssrc_to.length(), &sdp);
1988 ssrc_org = "a=ssrc:2";
1989 ssrc_to = "a=ssrc:98";
1990 rtc::replace_substrs(ssrc_org.c_str(), ssrc_org.length(), ssrc_to.c_str(),
1991 ssrc_to.length(), &sdp);
1992 rtc::scoped_ptr<SessionDescriptionInterface> updated_desc(
1993 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer, sdp,
1994 nullptr));
1995
1996 EXPECT_TRUE(DoSetLocalDescription(updated_desc.release()));
1997 senders = pc_->GetSenders();
1998 EXPECT_EQ(2u, senders.size());
1999 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2000 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2001 // TODO(deadbeef): Once RtpSenders expose parameters, check that the SSRC
2002 // changed.
2003}
2004
2005// This tests that the expected behavior occurs if a new session description is
2006// set with the same tracks, but on a different MediaStream.
2007TEST_F(PeerConnectionInterfaceTest, SignalSameTracksInSeparateMediaStream) {
2008 FakeConstraints constraints;
2009 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2010 true);
2011 CreatePeerConnection(&constraints);
2012 // Create an offer just to ensure we have an identity before we manually
2013 // call SetLocalDescription.
2014 rtc::scoped_ptr<SessionDescriptionInterface> throwaway;
2015 ASSERT_TRUE(DoCreateOffer(throwaway.accept()));
2016
2017 rtc::scoped_ptr<SessionDescriptionInterface> desc;
2018 CreateSessionDescriptionAndReference(1, 1, desc.accept());
2019 std::string sdp;
2020 desc->ToString(&sdp);
2021
2022 pc_->AddStream(reference_collection_->at(0));
2023 EXPECT_TRUE(DoSetLocalDescription(desc.release()));
2024 auto senders = pc_->GetSenders();
2025 EXPECT_EQ(2u, senders.size());
2026 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2027 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2028
2029 // Add a new MediaStream but with the same tracks as in the first stream.
2030 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream_1(
2031 webrtc::MediaStream::Create(kStreams[1]));
2032 stream_1->AddTrack(reference_collection_->at(0)->GetVideoTracks()[0]);
2033 stream_1->AddTrack(reference_collection_->at(0)->GetAudioTracks()[0]);
2034 pc_->AddStream(stream_1);
2035
2036 // Replace msid in the original SDP.
2037 rtc::replace_substrs(kStreams[0], strlen(kStreams[0]), kStreams[1],
2038 strlen(kStreams[1]), &sdp);
2039
2040 rtc::scoped_ptr<SessionDescriptionInterface> updated_desc(
2041 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer, sdp,
2042 nullptr));
2043
2044 EXPECT_TRUE(DoSetLocalDescription(updated_desc.release()));
2045 senders = pc_->GetSenders();
2046 EXPECT_EQ(2u, senders.size());
2047 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2048 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2049}
2050
2051// The following tests verify that session options are created correctly.
2052
2053TEST(CreateSessionOptionsTest, GetOptionsForOfferWithInvalidAudioOption) {
2054 RTCOfferAnswerOptions rtc_options;
2055 rtc_options.offer_to_receive_audio = RTCOfferAnswerOptions::kUndefined - 1;
2056
2057 cricket::MediaSessionOptions options;
2058 EXPECT_FALSE(ConvertRtcOptionsForOffer(rtc_options, &options));
2059
2060 rtc_options.offer_to_receive_audio =
2061 RTCOfferAnswerOptions::kMaxOfferToReceiveMedia + 1;
2062 EXPECT_FALSE(ConvertRtcOptionsForOffer(rtc_options, &options));
2063}
2064
2065TEST(CreateSessionOptionsTest, GetOptionsForOfferWithInvalidVideoOption) {
2066 RTCOfferAnswerOptions rtc_options;
2067 rtc_options.offer_to_receive_video = RTCOfferAnswerOptions::kUndefined - 1;
2068
2069 cricket::MediaSessionOptions options;
2070 EXPECT_FALSE(ConvertRtcOptionsForOffer(rtc_options, &options));
2071
2072 rtc_options.offer_to_receive_video =
2073 RTCOfferAnswerOptions::kMaxOfferToReceiveMedia + 1;
2074 EXPECT_FALSE(ConvertRtcOptionsForOffer(rtc_options, &options));
2075}
2076
2077// Test that a MediaSessionOptions is created for an offer if
2078// OfferToReceiveAudio and OfferToReceiveVideo options are set but no
2079// MediaStreams are sent.
2080TEST(CreateSessionOptionsTest, GetMediaSessionOptionsForOfferWithAudioVideo) {
2081 RTCOfferAnswerOptions rtc_options;
2082 rtc_options.offer_to_receive_audio = 1;
2083 rtc_options.offer_to_receive_video = 1;
2084
2085 cricket::MediaSessionOptions options;
2086 EXPECT_TRUE(ConvertRtcOptionsForOffer(rtc_options, &options));
2087 EXPECT_TRUE(options.has_audio());
2088 EXPECT_TRUE(options.has_video());
2089 EXPECT_TRUE(options.bundle_enabled);
2090}
2091
2092// Test that a correct MediaSessionOptions is created for an offer if
2093// OfferToReceiveAudio is set but no MediaStreams are sent.
2094TEST(CreateSessionOptionsTest, GetMediaSessionOptionsForOfferWithAudio) {
2095 RTCOfferAnswerOptions rtc_options;
2096 rtc_options.offer_to_receive_audio = 1;
2097
2098 cricket::MediaSessionOptions options;
2099 EXPECT_TRUE(ConvertRtcOptionsForOffer(rtc_options, &options));
2100 EXPECT_TRUE(options.has_audio());
2101 EXPECT_FALSE(options.has_video());
2102 EXPECT_TRUE(options.bundle_enabled);
2103}
2104
2105// Test that a correct MediaSessionOptions is created for an offer if
2106// the default OfferOptons is used or MediaStreams are sent.
2107TEST(CreateSessionOptionsTest, GetDefaultMediaSessionOptionsForOffer) {
2108 RTCOfferAnswerOptions rtc_options;
2109
2110 cricket::MediaSessionOptions options;
2111 EXPECT_TRUE(ConvertRtcOptionsForOffer(rtc_options, &options));
2112 EXPECT_FALSE(options.has_audio());
2113 EXPECT_FALSE(options.has_video());
2114 EXPECT_FALSE(options.bundle_enabled);
2115 EXPECT_TRUE(options.vad_enabled);
2116 EXPECT_FALSE(options.transport_options.ice_restart);
2117}
2118
2119// Test that a correct MediaSessionOptions is created for an offer if
2120// OfferToReceiveVideo is set but no MediaStreams are sent.
2121TEST(CreateSessionOptionsTest, GetMediaSessionOptionsForOfferWithVideo) {
2122 RTCOfferAnswerOptions rtc_options;
2123 rtc_options.offer_to_receive_audio = 0;
2124 rtc_options.offer_to_receive_video = 1;
2125
2126 cricket::MediaSessionOptions options;
2127 EXPECT_TRUE(ConvertRtcOptionsForOffer(rtc_options, &options));
2128 EXPECT_FALSE(options.has_audio());
2129 EXPECT_TRUE(options.has_video());
2130 EXPECT_TRUE(options.bundle_enabled);
2131}
2132
2133// Test that a correct MediaSessionOptions is created for an offer if
2134// UseRtpMux is set to false.
2135TEST(CreateSessionOptionsTest,
2136 GetMediaSessionOptionsForOfferWithBundleDisabled) {
2137 RTCOfferAnswerOptions rtc_options;
2138 rtc_options.offer_to_receive_audio = 1;
2139 rtc_options.offer_to_receive_video = 1;
2140 rtc_options.use_rtp_mux = false;
2141
2142 cricket::MediaSessionOptions options;
2143 EXPECT_TRUE(ConvertRtcOptionsForOffer(rtc_options, &options));
2144 EXPECT_TRUE(options.has_audio());
2145 EXPECT_TRUE(options.has_video());
2146 EXPECT_FALSE(options.bundle_enabled);
2147}
2148
2149// Test that a correct MediaSessionOptions is created to restart ice if
2150// IceRestart is set. It also tests that subsequent MediaSessionOptions don't
2151// have |transport_options.ice_restart| set.
2152TEST(CreateSessionOptionsTest, GetMediaSessionOptionsForOfferWithIceRestart) {
2153 RTCOfferAnswerOptions rtc_options;
2154 rtc_options.ice_restart = true;
2155
2156 cricket::MediaSessionOptions options;
2157 EXPECT_TRUE(ConvertRtcOptionsForOffer(rtc_options, &options));
2158 EXPECT_TRUE(options.transport_options.ice_restart);
2159
2160 rtc_options = RTCOfferAnswerOptions();
2161 EXPECT_TRUE(ConvertRtcOptionsForOffer(rtc_options, &options));
2162 EXPECT_FALSE(options.transport_options.ice_restart);
2163}
2164
2165// Test that the MediaConstraints in an answer don't affect if audio and video
2166// is offered in an offer but that if kOfferToReceiveAudio or
2167// kOfferToReceiveVideo constraints are true in an offer, the media type will be
2168// included in subsequent answers.
2169TEST(CreateSessionOptionsTest, MediaConstraintsInAnswer) {
2170 FakeConstraints answer_c;
2171 answer_c.SetMandatoryReceiveAudio(true);
2172 answer_c.SetMandatoryReceiveVideo(true);
2173
2174 cricket::MediaSessionOptions answer_options;
2175 EXPECT_TRUE(ParseConstraintsForAnswer(&answer_c, &answer_options));
2176 EXPECT_TRUE(answer_options.has_audio());
2177 EXPECT_TRUE(answer_options.has_video());
2178
2179 RTCOfferAnswerOptions rtc_offer_optoins;
2180
2181 cricket::MediaSessionOptions offer_options;
2182 EXPECT_TRUE(ConvertRtcOptionsForOffer(rtc_offer_optoins, &offer_options));
2183 EXPECT_FALSE(offer_options.has_audio());
2184 EXPECT_FALSE(offer_options.has_video());
2185
2186 RTCOfferAnswerOptions updated_rtc_offer_optoins;
2187 updated_rtc_offer_optoins.offer_to_receive_audio = 1;
2188 updated_rtc_offer_optoins.offer_to_receive_video = 1;
2189
2190 cricket::MediaSessionOptions updated_offer_options;
2191 EXPECT_TRUE(ConvertRtcOptionsForOffer(updated_rtc_offer_optoins,
2192 &updated_offer_options));
2193 EXPECT_TRUE(updated_offer_options.has_audio());
2194 EXPECT_TRUE(updated_offer_options.has_video());
2195
2196 // Since an offer has been created with both audio and video, subsequent
2197 // offers and answers should contain both audio and video.
2198 // Answers will only contain the media types that exist in the offer
2199 // regardless of the value of |updated_answer_options.has_audio| and
2200 // |updated_answer_options.has_video|.
2201 FakeConstraints updated_answer_c;
2202 answer_c.SetMandatoryReceiveAudio(false);
2203 answer_c.SetMandatoryReceiveVideo(false);
2204
2205 cricket::MediaSessionOptions updated_answer_options;
2206 EXPECT_TRUE(
2207 ParseConstraintsForAnswer(&updated_answer_c, &updated_answer_options));
2208 EXPECT_TRUE(updated_answer_options.has_audio());
2209 EXPECT_TRUE(updated_answer_options.has_video());
2210
2211 RTCOfferAnswerOptions default_rtc_options;
2212 EXPECT_TRUE(
2213 ConvertRtcOptionsForOffer(default_rtc_options, &updated_offer_options));
2214 // By default, |has_audio| or |has_video| are false if there is no media
2215 // track.
2216 EXPECT_FALSE(updated_offer_options.has_audio());
2217 EXPECT_FALSE(updated_offer_options.has_video());
2218}