blob: d673b4174a93a7a46aa2771477073b4c0b22ffe1 [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
kjellanderb24317b2016-02-10 07:54:43 -08002 * Copyright 2012 The WebRTC project authors. All Rights Reserved.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003 *
kjellanderb24317b2016-02-10 07:54:43 -08004 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00009 */
10
kwibergd1fe2812016-04-27 06:47:29 -070011#include <memory>
henrike@webrtc.org28e20752013-07-10 00:45:36 +000012#include <string>
kwiberg0eb15ed2015-12-17 03:04:15 -080013#include <utility>
henrike@webrtc.org28e20752013-07-10 00:45:36 +000014
Henrik Kjellander15583c12016-02-10 10:53:12 +010015#include "webrtc/api/audiotrack.h"
16#include "webrtc/api/jsepsessiondescription.h"
17#include "webrtc/api/mediastream.h"
18#include "webrtc/api/mediastreaminterface.h"
19#include "webrtc/api/peerconnection.h"
20#include "webrtc/api/peerconnectioninterface.h"
21#include "webrtc/api/rtpreceiverinterface.h"
22#include "webrtc/api/rtpsenderinterface.h"
23#include "webrtc/api/streamcollection.h"
Henrik Kjellander15583c12016-02-10 10:53:12 +010024#include "webrtc/api/test/fakeconstraints.h"
Henrik Boströmd79599d2016-06-01 13:58:50 +020025#include "webrtc/api/test/fakertccertificategenerator.h"
nisseaf510af2016-03-21 08:20:42 -070026#include "webrtc/api/test/fakevideotracksource.h"
Henrik Kjellander15583c12016-02-10 10:53:12 +010027#include "webrtc/api/test/mockpeerconnectionobservers.h"
28#include "webrtc/api/test/testsdpstrings.h"
perkja3ede6c2016-03-08 01:27:48 +010029#include "webrtc/api/videocapturertracksource.h"
Henrik Kjellander15583c12016-02-10 10:53:12 +010030#include "webrtc/api/videotrack.h"
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000031#include "webrtc/base/gunit.h"
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000032#include "webrtc/base/ssladapter.h"
33#include "webrtc/base/sslstreamadapter.h"
34#include "webrtc/base/stringutils.h"
35#include "webrtc/base/thread.h"
kjellandera96e2d72016-02-04 23:52:28 -080036#include "webrtc/media/base/fakevideocapturer.h"
37#include "webrtc/media/sctp/sctpdataengine.h"
Taylor Brandstettera1c30352016-05-13 08:15:11 -070038#include "webrtc/p2p/base/fakeportallocator.h"
zhihuang29ff8442016-07-27 11:07:25 -070039#include "webrtc/p2p/base/faketransportcontroller.h"
kjellander@webrtc.org9b8df252016-02-12 06:47:59 +010040#include "webrtc/pc/mediasession.h"
kwibergac9f8762016-09-30 22:29:43 -070041#include "webrtc/test/gmock.h"
42
43#ifdef WEBRTC_ANDROID
44#include "webrtc/api/test/androidtestinitializer.h"
45#endif
henrike@webrtc.org28e20752013-07-10 00:45:36 +000046
47static const char kStreamLabel1[] = "local_stream_1";
48static const char kStreamLabel2[] = "local_stream_2";
49static const char kStreamLabel3[] = "local_stream_3";
50static const int kDefaultStunPort = 3478;
51static const char kStunAddressOnly[] = "stun:address";
52static const char kStunInvalidPort[] = "stun:address:-1";
53static const char kStunAddressPortAndMore1[] = "stun:address:port:more";
54static const char kStunAddressPortAndMore2[] = "stun:address:port more";
55static const char kTurnIceServerUri[] = "turn:user@turn.example.org";
56static const char kTurnUsername[] = "user";
57static const char kTurnPassword[] = "password";
58static const char kTurnHostname[] = "turn.example.org";
Peter Boström0c4e06b2015-10-07 12:23:21 +020059static const uint32_t kTimeout = 10000U;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000060
deadbeefab9b2d12015-10-14 11:33:11 -070061static const char kStreams[][8] = {"stream1", "stream2"};
62static const char kAudioTracks[][32] = {"audiotrack0", "audiotrack1"};
63static const char kVideoTracks[][32] = {"videotrack0", "videotrack1"};
64
deadbeef5e97fb52015-10-15 12:49:08 -070065static const char kRecvonly[] = "recvonly";
66static const char kSendrecv[] = "sendrecv";
67
deadbeefab9b2d12015-10-14 11:33:11 -070068// Reference SDP with a MediaStream with label "stream1" and audio track with
69// id "audio_1" and a video track with id "video_1;
70static const char kSdpStringWithStream1[] =
71 "v=0\r\n"
72 "o=- 0 0 IN IP4 127.0.0.1\r\n"
73 "s=-\r\n"
74 "t=0 0\r\n"
75 "a=ice-ufrag:e5785931\r\n"
76 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
77 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
78 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
79 "m=audio 1 RTP/AVPF 103\r\n"
80 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -070081 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -080082 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -070083 "a=rtpmap:103 ISAC/16000\r\n"
84 "a=ssrc:1 cname:stream1\r\n"
85 "a=ssrc:1 mslabel:stream1\r\n"
86 "a=ssrc:1 label:audiotrack0\r\n"
87 "m=video 1 RTP/AVPF 120\r\n"
88 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -070089 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -080090 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -070091 "a=rtpmap:120 VP8/90000\r\n"
92 "a=ssrc:2 cname:stream1\r\n"
93 "a=ssrc:2 mslabel:stream1\r\n"
94 "a=ssrc:2 label:videotrack0\r\n";
95
zhihuang81c3a032016-11-17 12:06:24 -080096// Reference SDP with a MediaStream with label "stream1" and audio track with
97// id "audio_1";
98static const char kSdpStringWithStream1AudioTrackOnly[] =
99 "v=0\r\n"
100 "o=- 0 0 IN IP4 127.0.0.1\r\n"
101 "s=-\r\n"
102 "t=0 0\r\n"
103 "a=ice-ufrag:e5785931\r\n"
104 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
105 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
106 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
107 "m=audio 1 RTP/AVPF 103\r\n"
108 "a=mid:audio\r\n"
109 "a=sendrecv\r\n"
110 "a=rtpmap:103 ISAC/16000\r\n"
111 "a=ssrc:1 cname:stream1\r\n"
112 "a=ssrc:1 mslabel:stream1\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800113 "a=ssrc:1 label:audiotrack0\r\n"
114 "a=rtcp-mux\r\n";
zhihuang81c3a032016-11-17 12:06:24 -0800115
deadbeefab9b2d12015-10-14 11:33:11 -0700116// Reference SDP with two MediaStreams with label "stream1" and "stream2. Each
117// MediaStreams have one audio track and one video track.
118// This uses MSID.
119static const char kSdpStringWithStream1And2[] =
120 "v=0\r\n"
121 "o=- 0 0 IN IP4 127.0.0.1\r\n"
122 "s=-\r\n"
123 "t=0 0\r\n"
124 "a=ice-ufrag:e5785931\r\n"
125 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
126 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
127 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
128 "a=msid-semantic: WMS stream1 stream2\r\n"
129 "m=audio 1 RTP/AVPF 103\r\n"
130 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700131 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800132 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700133 "a=rtpmap:103 ISAC/16000\r\n"
134 "a=ssrc:1 cname:stream1\r\n"
135 "a=ssrc:1 msid:stream1 audiotrack0\r\n"
136 "a=ssrc:3 cname:stream2\r\n"
137 "a=ssrc:3 msid:stream2 audiotrack1\r\n"
138 "m=video 1 RTP/AVPF 120\r\n"
139 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700140 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800141 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700142 "a=rtpmap:120 VP8/0\r\n"
143 "a=ssrc:2 cname:stream1\r\n"
144 "a=ssrc:2 msid:stream1 videotrack0\r\n"
145 "a=ssrc:4 cname:stream2\r\n"
146 "a=ssrc:4 msid:stream2 videotrack1\r\n";
147
148// Reference SDP without MediaStreams. Msid is not supported.
149static const char kSdpStringWithoutStreams[] =
150 "v=0\r\n"
151 "o=- 0 0 IN IP4 127.0.0.1\r\n"
152 "s=-\r\n"
153 "t=0 0\r\n"
154 "a=ice-ufrag:e5785931\r\n"
155 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
156 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
157 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
158 "m=audio 1 RTP/AVPF 103\r\n"
159 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700160 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800161 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700162 "a=rtpmap:103 ISAC/16000\r\n"
163 "m=video 1 RTP/AVPF 120\r\n"
164 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700165 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800166 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700167 "a=rtpmap:120 VP8/90000\r\n";
168
169// Reference SDP without MediaStreams. Msid is supported.
170static const char kSdpStringWithMsidWithoutStreams[] =
171 "v=0\r\n"
172 "o=- 0 0 IN IP4 127.0.0.1\r\n"
173 "s=-\r\n"
174 "t=0 0\r\n"
175 "a=ice-ufrag:e5785931\r\n"
176 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
177 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
178 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
179 "a=msid-semantic: WMS\r\n"
180 "m=audio 1 RTP/AVPF 103\r\n"
181 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700182 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800183 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700184 "a=rtpmap:103 ISAC/16000\r\n"
185 "m=video 1 RTP/AVPF 120\r\n"
186 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700187 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800188 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700189 "a=rtpmap:120 VP8/90000\r\n";
190
191// Reference SDP without MediaStreams and audio only.
192static const char kSdpStringWithoutStreamsAudioOnly[] =
193 "v=0\r\n"
194 "o=- 0 0 IN IP4 127.0.0.1\r\n"
195 "s=-\r\n"
196 "t=0 0\r\n"
197 "a=ice-ufrag:e5785931\r\n"
198 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
199 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
200 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
201 "m=audio 1 RTP/AVPF 103\r\n"
202 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700203 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800204 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700205 "a=rtpmap:103 ISAC/16000\r\n";
206
207// Reference SENDONLY SDP without MediaStreams. Msid is not supported.
208static const char kSdpStringSendOnlyWithoutStreams[] =
209 "v=0\r\n"
210 "o=- 0 0 IN IP4 127.0.0.1\r\n"
211 "s=-\r\n"
212 "t=0 0\r\n"
213 "a=ice-ufrag:e5785931\r\n"
214 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
215 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
216 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
217 "m=audio 1 RTP/AVPF 103\r\n"
218 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700219 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700220 "a=sendonly\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800221 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700222 "a=rtpmap:103 ISAC/16000\r\n"
223 "m=video 1 RTP/AVPF 120\r\n"
224 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700225 "a=sendrecv\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700226 "a=sendonly\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800227 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700228 "a=rtpmap:120 VP8/90000\r\n";
229
230static const char kSdpStringInit[] =
231 "v=0\r\n"
232 "o=- 0 0 IN IP4 127.0.0.1\r\n"
233 "s=-\r\n"
234 "t=0 0\r\n"
235 "a=ice-ufrag:e5785931\r\n"
236 "a=ice-pwd:36fb7878390db89481c1d46daa4278d8\r\n"
237 "a=fingerprint:sha-256 58:AB:6E:F5:F1:E4:57:B7:E9:46:F4:86:04:28:F9:A7:ED:"
238 "BD:AB:AE:40:EF:CE:9A:51:2C:2A:B1:9B:8B:78:84\r\n"
239 "a=msid-semantic: WMS\r\n";
240
241static const char kSdpStringAudio[] =
242 "m=audio 1 RTP/AVPF 103\r\n"
243 "a=mid:audio\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700244 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800245 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700246 "a=rtpmap:103 ISAC/16000\r\n";
247
248static const char kSdpStringVideo[] =
249 "m=video 1 RTP/AVPF 120\r\n"
250 "a=mid:video\r\n"
deadbeef5e97fb52015-10-15 12:49:08 -0700251 "a=sendrecv\r\n"
zhihuang4dfb8ce2016-11-23 10:30:12 -0800252 "a=rtcp-mux\r\n"
deadbeefab9b2d12015-10-14 11:33:11 -0700253 "a=rtpmap:120 VP8/90000\r\n";
254
255static const char kSdpStringMs1Audio0[] =
256 "a=ssrc:1 cname:stream1\r\n"
257 "a=ssrc:1 msid:stream1 audiotrack0\r\n";
258
259static const char kSdpStringMs1Video0[] =
260 "a=ssrc:2 cname:stream1\r\n"
261 "a=ssrc:2 msid:stream1 videotrack0\r\n";
262
263static const char kSdpStringMs1Audio1[] =
264 "a=ssrc:3 cname:stream1\r\n"
265 "a=ssrc:3 msid:stream1 audiotrack1\r\n";
266
267static const char kSdpStringMs1Video1[] =
268 "a=ssrc:4 cname:stream1\r\n"
269 "a=ssrc:4 msid:stream1 videotrack1\r\n";
270
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000271#define MAYBE_SKIP_TEST(feature) \
272 if (!(feature())) { \
273 LOG(LS_INFO) << "Feature disabled... skipping"; \
274 return; \
275 }
276
perkjd61bf802016-03-24 03:16:19 -0700277using ::testing::Exactly;
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700278using cricket::StreamParams;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000279using webrtc::AudioSourceInterface;
deadbeefab9b2d12015-10-14 11:33:11 -0700280using webrtc::AudioTrack;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000281using webrtc::AudioTrackInterface;
282using webrtc::DataBuffer;
283using webrtc::DataChannelInterface;
284using webrtc::FakeConstraints;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000285using webrtc::IceCandidateInterface;
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700286using webrtc::JsepSessionDescription;
deadbeefc80741f2015-10-22 13:14:45 -0700287using webrtc::MediaConstraintsInterface;
deadbeefab9b2d12015-10-14 11:33:11 -0700288using webrtc::MediaStream;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000289using webrtc::MediaStreamInterface;
290using webrtc::MediaStreamTrackInterface;
291using webrtc::MockCreateSessionDescriptionObserver;
292using webrtc::MockDataChannelObserver;
293using webrtc::MockSetSessionDescriptionObserver;
294using webrtc::MockStatsObserver;
perkjd61bf802016-03-24 03:16:19 -0700295using webrtc::NotifierInterface;
296using webrtc::ObserverInterface;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000297using webrtc::PeerConnectionInterface;
298using webrtc::PeerConnectionObserver;
deadbeefab9b2d12015-10-14 11:33:11 -0700299using webrtc::RtpReceiverInterface;
300using webrtc::RtpSenderInterface;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000301using webrtc::SdpParseError;
302using webrtc::SessionDescriptionInterface;
deadbeefab9b2d12015-10-14 11:33:11 -0700303using webrtc::StreamCollection;
304using webrtc::StreamCollectionInterface;
perkja3ede6c2016-03-08 01:27:48 +0100305using webrtc::VideoTrackSourceInterface;
deadbeefab9b2d12015-10-14 11:33:11 -0700306using webrtc::VideoTrack;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000307using webrtc::VideoTrackInterface;
308
deadbeefab9b2d12015-10-14 11:33:11 -0700309typedef PeerConnectionInterface::RTCOfferAnswerOptions RTCOfferAnswerOptions;
310
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000311namespace {
312
313// Gets the first ssrc of given content type from the ContentInfo.
314bool GetFirstSsrc(const cricket::ContentInfo* content_info, int* ssrc) {
315 if (!content_info || !ssrc) {
316 return false;
317 }
318 const cricket::MediaContentDescription* media_desc =
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000319 static_cast<const cricket::MediaContentDescription*>(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000320 content_info->description);
321 if (!media_desc || media_desc->streams().empty()) {
322 return false;
323 }
324 *ssrc = media_desc->streams().begin()->first_ssrc();
325 return true;
326}
327
328void SetSsrcToZero(std::string* sdp) {
329 const char kSdpSsrcAtribute[] = "a=ssrc:";
330 const char kSdpSsrcAtributeZero[] = "a=ssrc:0";
331 size_t ssrc_pos = 0;
332 while ((ssrc_pos = sdp->find(kSdpSsrcAtribute, ssrc_pos)) !=
333 std::string::npos) {
334 size_t end_ssrc = sdp->find(" ", ssrc_pos);
335 sdp->replace(ssrc_pos, end_ssrc - ssrc_pos, kSdpSsrcAtributeZero);
336 ssrc_pos = end_ssrc;
337 }
338}
339
deadbeefab9b2d12015-10-14 11:33:11 -0700340// Check if |streams| contains the specified track.
341bool ContainsTrack(const std::vector<cricket::StreamParams>& streams,
342 const std::string& stream_label,
343 const std::string& track_id) {
344 for (const cricket::StreamParams& params : streams) {
345 if (params.sync_label == stream_label && params.id == track_id) {
346 return true;
347 }
348 }
349 return false;
350}
351
352// Check if |senders| contains the specified sender, by id.
353bool ContainsSender(
354 const std::vector<rtc::scoped_refptr<RtpSenderInterface>>& senders,
355 const std::string& id) {
356 for (const auto& sender : senders) {
357 if (sender->id() == id) {
358 return true;
359 }
360 }
361 return false;
362}
363
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700364// Check if |senders| contains the specified sender, by id and stream id.
365bool ContainsSender(
366 const std::vector<rtc::scoped_refptr<RtpSenderInterface>>& senders,
367 const std::string& id,
368 const std::string& stream_id) {
369 for (const auto& sender : senders) {
deadbeefa601f5c2016-06-06 14:27:39 -0700370 if (sender->id() == id && sender->stream_ids()[0] == stream_id) {
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700371 return true;
372 }
373 }
374 return false;
375}
376
deadbeefab9b2d12015-10-14 11:33:11 -0700377// Create a collection of streams.
378// CreateStreamCollection(1) creates a collection that
379// correspond to kSdpStringWithStream1.
380// CreateStreamCollection(2) correspond to kSdpStringWithStream1And2.
381rtc::scoped_refptr<StreamCollection> CreateStreamCollection(
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700382 int number_of_streams,
383 int tracks_per_stream) {
deadbeefab9b2d12015-10-14 11:33:11 -0700384 rtc::scoped_refptr<StreamCollection> local_collection(
385 StreamCollection::Create());
386
387 for (int i = 0; i < number_of_streams; ++i) {
388 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream(
389 webrtc::MediaStream::Create(kStreams[i]));
390
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700391 for (int j = 0; j < tracks_per_stream; ++j) {
392 // Add a local audio track.
393 rtc::scoped_refptr<webrtc::AudioTrackInterface> audio_track(
394 webrtc::AudioTrack::Create(kAudioTracks[i * tracks_per_stream + j],
395 nullptr));
396 stream->AddTrack(audio_track);
deadbeefab9b2d12015-10-14 11:33:11 -0700397
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -0700398 // Add a local video track.
399 rtc::scoped_refptr<webrtc::VideoTrackInterface> video_track(
400 webrtc::VideoTrack::Create(kVideoTracks[i * tracks_per_stream + j],
401 webrtc::FakeVideoTrackSource::Create()));
402 stream->AddTrack(video_track);
403 }
deadbeefab9b2d12015-10-14 11:33:11 -0700404
405 local_collection->AddStream(stream);
406 }
407 return local_collection;
408}
409
410// Check equality of StreamCollections.
411bool CompareStreamCollections(StreamCollectionInterface* s1,
412 StreamCollectionInterface* s2) {
413 if (s1 == nullptr || s2 == nullptr || s1->count() != s2->count()) {
414 return false;
415 }
416
417 for (size_t i = 0; i != s1->count(); ++i) {
418 if (s1->at(i)->label() != s2->at(i)->label()) {
419 return false;
420 }
421 webrtc::AudioTrackVector audio_tracks1 = s1->at(i)->GetAudioTracks();
422 webrtc::AudioTrackVector audio_tracks2 = s2->at(i)->GetAudioTracks();
423 webrtc::VideoTrackVector video_tracks1 = s1->at(i)->GetVideoTracks();
424 webrtc::VideoTrackVector video_tracks2 = s2->at(i)->GetVideoTracks();
425
426 if (audio_tracks1.size() != audio_tracks2.size()) {
427 return false;
428 }
429 for (size_t j = 0; j != audio_tracks1.size(); ++j) {
430 if (audio_tracks1[j]->id() != audio_tracks2[j]->id()) {
431 return false;
432 }
433 }
434 if (video_tracks1.size() != video_tracks2.size()) {
435 return false;
436 }
437 for (size_t j = 0; j != video_tracks1.size(); ++j) {
438 if (video_tracks1[j]->id() != video_tracks2[j]->id()) {
439 return false;
440 }
441 }
442 }
443 return true;
444}
445
perkjd61bf802016-03-24 03:16:19 -0700446// Helper class to test Observer.
447class MockTrackObserver : public ObserverInterface {
448 public:
449 explicit MockTrackObserver(NotifierInterface* notifier)
450 : notifier_(notifier) {
451 notifier_->RegisterObserver(this);
452 }
453
454 ~MockTrackObserver() { Unregister(); }
455
456 void Unregister() {
457 if (notifier_) {
458 notifier_->UnregisterObserver(this);
459 notifier_ = nullptr;
460 }
461 }
462
463 MOCK_METHOD0(OnChanged, void());
464
465 private:
466 NotifierInterface* notifier_;
467};
468
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000469class MockPeerConnectionObserver : public PeerConnectionObserver {
470 public:
kjellander71a1b612016-11-07 01:18:08 -0800471 // We need these using declarations because there are two versions of each of
472 // the below methods and we only override one of them.
473 // TODO(deadbeef): Remove once there's only one version of the methods.
474 using PeerConnectionObserver::OnAddStream;
475 using PeerConnectionObserver::OnRemoveStream;
476 using PeerConnectionObserver::OnDataChannel;
477
deadbeefab9b2d12015-10-14 11:33:11 -0700478 MockPeerConnectionObserver() : remote_streams_(StreamCollection::Create()) {}
Henrik Kjellander3fe372d2016-05-12 08:10:52 +0200479 virtual ~MockPeerConnectionObserver() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000480 }
481 void SetPeerConnectionInterface(PeerConnectionInterface* pc) {
482 pc_ = pc;
483 if (pc) {
484 state_ = pc_->signaling_state();
485 }
486 }
nisseef8b61e2016-04-29 06:09:15 -0700487 void OnSignalingChange(
488 PeerConnectionInterface::SignalingState new_state) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000489 EXPECT_EQ(pc_->signaling_state(), new_state);
490 state_ = new_state;
491 }
492 // TODO(bemasc): Remove this once callers transition to OnIceGatheringChange.
493 virtual void OnStateChange(StateType state_changed) {
494 if (pc_.get() == NULL)
495 return;
496 switch (state_changed) {
497 case kSignalingState:
498 // OnSignalingChange and OnStateChange(kSignalingState) should always
499 // be called approximately simultaneously. To ease testing, we require
500 // that they always be called in that order. This check verifies
501 // that OnSignalingChange has just been called.
502 EXPECT_EQ(pc_->signaling_state(), state_);
503 break;
504 case kIceState:
505 ADD_FAILURE();
506 break;
507 default:
508 ADD_FAILURE();
509 break;
510 }
511 }
deadbeefab9b2d12015-10-14 11:33:11 -0700512
513 MediaStreamInterface* RemoteStream(const std::string& label) {
514 return remote_streams_->find(label);
515 }
516 StreamCollectionInterface* remote_streams() const { return remote_streams_; }
Taylor Brandstetter98cde262016-05-31 13:02:21 -0700517 void OnAddStream(rtc::scoped_refptr<MediaStreamInterface> stream) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000518 last_added_stream_ = stream;
deadbeefab9b2d12015-10-14 11:33:11 -0700519 remote_streams_->AddStream(stream);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000520 }
Taylor Brandstetter98cde262016-05-31 13:02:21 -0700521 void OnRemoveStream(
522 rtc::scoped_refptr<MediaStreamInterface> stream) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000523 last_removed_stream_ = stream;
deadbeefab9b2d12015-10-14 11:33:11 -0700524 remote_streams_->RemoveStream(stream);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000525 }
perkjdfb769d2016-02-09 03:09:43 -0800526 void OnRenegotiationNeeded() override { renegotiation_needed_ = true; }
Taylor Brandstetter98cde262016-05-31 13:02:21 -0700527 void OnDataChannel(
528 rtc::scoped_refptr<DataChannelInterface> data_channel) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000529 last_datachannel_ = data_channel;
530 }
531
perkjdfb769d2016-02-09 03:09:43 -0800532 void OnIceConnectionChange(
533 PeerConnectionInterface::IceConnectionState new_state) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000534 EXPECT_EQ(pc_->ice_connection_state(), new_state);
zhihuang81c3a032016-11-17 12:06:24 -0800535 callback_triggered_ = true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000536 }
perkjdfb769d2016-02-09 03:09:43 -0800537 void OnIceGatheringChange(
538 PeerConnectionInterface::IceGatheringState new_state) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000539 EXPECT_EQ(pc_->ice_gathering_state(), new_state);
perkjdfb769d2016-02-09 03:09:43 -0800540 ice_complete_ = new_state == PeerConnectionInterface::kIceGatheringComplete;
zhihuang81c3a032016-11-17 12:06:24 -0800541 callback_triggered_ = true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000542 }
perkjdfb769d2016-02-09 03:09:43 -0800543 void OnIceCandidate(const webrtc::IceCandidateInterface* candidate) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000544 EXPECT_NE(PeerConnectionInterface::kIceGatheringNew,
545 pc_->ice_gathering_state());
546
547 std::string sdp;
548 EXPECT_TRUE(candidate->ToString(&sdp));
549 EXPECT_LT(0u, sdp.size());
550 last_candidate_.reset(webrtc::CreateIceCandidate(candidate->sdp_mid(),
551 candidate->sdp_mline_index(), sdp, NULL));
552 EXPECT_TRUE(last_candidate_.get() != NULL);
zhihuang81c3a032016-11-17 12:06:24 -0800553 callback_triggered_ = true;
zhihuang29ff8442016-07-27 11:07:25 -0700554 }
555
556 void OnIceCandidatesRemoved(
557 const std::vector<cricket::Candidate>& candidates) override {
zhihuang81c3a032016-11-17 12:06:24 -0800558 callback_triggered_ = true;
zhihuang29ff8442016-07-27 11:07:25 -0700559 }
560
561 void OnIceConnectionReceivingChange(bool receiving) override {
zhihuang81c3a032016-11-17 12:06:24 -0800562 callback_triggered_ = true;
563 }
564
zhihuangc63b8942016-12-02 15:41:10 -0800565 void OnAddTrack(
566 rtc::scoped_refptr<webrtc::RtpReceiverInterface> receiver,
567 const std::vector<rtc::scoped_refptr<webrtc::MediaStreamInterface>>&
568 streams) override {
zhihuang81c3a032016-11-17 12:06:24 -0800569 EXPECT_TRUE(receiver != nullptr);
570 num_added_tracks_++;
571 last_added_track_label_ = receiver->id();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000572 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000573
574 // Returns the label of the last added stream.
575 // Empty string if no stream have been added.
576 std::string GetLastAddedStreamLabel() {
577 if (last_added_stream_.get())
578 return last_added_stream_->label();
579 return "";
580 }
581 std::string GetLastRemovedStreamLabel() {
582 if (last_removed_stream_.get())
583 return last_removed_stream_->label();
584 return "";
585 }
586
zhihuang9763d562016-08-05 11:14:50 -0700587 rtc::scoped_refptr<PeerConnectionInterface> pc_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000588 PeerConnectionInterface::SignalingState state_;
kwibergd1fe2812016-04-27 06:47:29 -0700589 std::unique_ptr<IceCandidateInterface> last_candidate_;
zhihuang9763d562016-08-05 11:14:50 -0700590 rtc::scoped_refptr<DataChannelInterface> last_datachannel_;
deadbeefab9b2d12015-10-14 11:33:11 -0700591 rtc::scoped_refptr<StreamCollection> remote_streams_;
592 bool renegotiation_needed_ = false;
593 bool ice_complete_ = false;
zhihuang81c3a032016-11-17 12:06:24 -0800594 bool callback_triggered_ = false;
595 int num_added_tracks_ = 0;
596 std::string last_added_track_label_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000597
598 private:
zhihuang9763d562016-08-05 11:14:50 -0700599 rtc::scoped_refptr<MediaStreamInterface> last_added_stream_;
600 rtc::scoped_refptr<MediaStreamInterface> last_removed_stream_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000601};
602
603} // namespace
deadbeefab9b2d12015-10-14 11:33:11 -0700604
zhihuang29ff8442016-07-27 11:07:25 -0700605// The PeerConnectionMediaConfig tests below verify that configuration
606// and constraints are propagated into the MediaConfig passed to
607// CreateMediaController. These settings are intended for MediaChannel
608// constructors, but that is not exercised by these unittest.
609class PeerConnectionFactoryForTest : public webrtc::PeerConnectionFactory {
610 public:
611 webrtc::MediaControllerInterface* CreateMediaController(
skvlad11a9cbf2016-10-07 11:53:05 -0700612 const cricket::MediaConfig& config,
613 webrtc::RtcEventLog* event_log) const override {
zhihuang29ff8442016-07-27 11:07:25 -0700614 create_media_controller_called_ = true;
615 create_media_controller_config_ = config;
616
617 webrtc::MediaControllerInterface* mc =
skvlad11a9cbf2016-10-07 11:53:05 -0700618 PeerConnectionFactory::CreateMediaController(config, event_log);
zhihuang29ff8442016-07-27 11:07:25 -0700619 EXPECT_TRUE(mc != nullptr);
620 return mc;
621 }
622
623 cricket::TransportController* CreateTransportController(
Honghai Zhangbfd398c2016-08-30 22:07:42 -0700624 cricket::PortAllocator* port_allocator,
625 bool redetermine_role_on_ice_restart) override {
zhihuang29ff8442016-07-27 11:07:25 -0700626 transport_controller = new cricket::TransportController(
Honghai Zhangbfd398c2016-08-30 22:07:42 -0700627 rtc::Thread::Current(), rtc::Thread::Current(), port_allocator,
628 redetermine_role_on_ice_restart);
zhihuang29ff8442016-07-27 11:07:25 -0700629 return transport_controller;
630 }
631
632 cricket::TransportController* transport_controller;
633 // Mutable, so they can be modified in the above const-declared method.
634 mutable bool create_media_controller_called_ = false;
635 mutable cricket::MediaConfig create_media_controller_config_;
636};
637
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000638class PeerConnectionInterfaceTest : public testing::Test {
639 protected:
phoglund37ebcf02016-01-08 05:04:57 -0800640 PeerConnectionInterfaceTest() {
641#ifdef WEBRTC_ANDROID
642 webrtc::InitializeAndroidObjects();
643#endif
644 }
645
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000646 virtual void SetUp() {
647 pc_factory_ = webrtc::CreatePeerConnectionFactory(
danilchape9021a32016-05-17 01:52:02 -0700648 rtc::Thread::Current(), rtc::Thread::Current(), rtc::Thread::Current(),
649 nullptr, nullptr, nullptr);
650 ASSERT_TRUE(pc_factory_);
zhihuang29ff8442016-07-27 11:07:25 -0700651 pc_factory_for_test_ =
652 new rtc::RefCountedObject<PeerConnectionFactoryForTest>();
653 pc_factory_for_test_->Initialize();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000654 }
655
656 void CreatePeerConnection() {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700657 CreatePeerConnection(PeerConnectionInterface::RTCConfiguration(), nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000658 }
659
660 void CreatePeerConnection(webrtc::MediaConstraintsInterface* constraints) {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700661 CreatePeerConnection(PeerConnectionInterface::RTCConfiguration(),
662 constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000663 }
664
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700665 void CreatePeerConnectionWithIceTransportsType(
666 PeerConnectionInterface::IceTransportsType type) {
667 PeerConnectionInterface::RTCConfiguration config;
668 config.type = type;
669 return CreatePeerConnection(config, nullptr);
670 }
671
672 void CreatePeerConnectionWithIceServer(const std::string& uri,
673 const std::string& password) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800674 PeerConnectionInterface::RTCConfiguration config;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000675 PeerConnectionInterface::IceServer server;
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700676 server.uri = uri;
677 server.password = password;
678 config.servers.push_back(server);
679 CreatePeerConnection(config, nullptr);
680 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000681
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700682 void CreatePeerConnection(PeerConnectionInterface::RTCConfiguration config,
683 webrtc::MediaConstraintsInterface* constraints) {
kwibergd1fe2812016-04-27 06:47:29 -0700684 std::unique_ptr<cricket::FakePortAllocator> port_allocator(
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800685 new cricket::FakePortAllocator(rtc::Thread::Current(), nullptr));
686 port_allocator_ = port_allocator.get();
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000687
buildbot@webrtc.org61c1b8e2014-04-09 06:06:38 +0000688 // DTLS does not work in a loopback call, so is disabled for most of the
689 // tests in this file. We only create a FakeIdentityService if the test
690 // explicitly sets the constraint.
jiayl@webrtc.org61e00b02015-03-04 22:17:38 +0000691 FakeConstraints default_constraints;
692 if (!constraints) {
693 constraints = &default_constraints;
694
695 default_constraints.AddMandatory(
696 webrtc::MediaConstraintsInterface::kEnableDtlsSrtp, false);
697 }
698
Henrik Boströmd79599d2016-06-01 13:58:50 +0200699 std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator;
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000700 bool dtls;
701 if (FindConstraint(constraints,
702 webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
703 &dtls,
Henrik Boström5e56c592015-08-11 10:33:13 +0200704 nullptr) && dtls) {
Henrik Boströmd79599d2016-06-01 13:58:50 +0200705 cert_generator.reset(new FakeRTCCertificateGenerator());
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000706 }
Henrik Boströmd79599d2016-06-01 13:58:50 +0200707 pc_ = pc_factory_->CreatePeerConnection(
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800708 config, constraints, std::move(port_allocator),
Henrik Boströmd79599d2016-06-01 13:58:50 +0200709 std::move(cert_generator), &observer_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000710 ASSERT_TRUE(pc_.get() != NULL);
711 observer_.SetPeerConnectionInterface(pc_.get());
712 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
713 }
714
deadbeef0a6c4ca2015-10-06 11:38:28 -0700715 void CreatePeerConnectionExpectFail(const std::string& uri) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800716 PeerConnectionInterface::RTCConfiguration config;
deadbeef0a6c4ca2015-10-06 11:38:28 -0700717 PeerConnectionInterface::IceServer server;
deadbeef0a6c4ca2015-10-06 11:38:28 -0700718 server.uri = uri;
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800719 config.servers.push_back(server);
deadbeef0a6c4ca2015-10-06 11:38:28 -0700720
zhihuang9763d562016-08-05 11:14:50 -0700721 rtc::scoped_refptr<PeerConnectionInterface> pc;
hbosd7973cc2016-05-27 06:08:53 -0700722 pc = pc_factory_->CreatePeerConnection(config, nullptr, nullptr, nullptr,
723 &observer_);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800724 EXPECT_EQ(nullptr, pc);
deadbeef0a6c4ca2015-10-06 11:38:28 -0700725 }
726
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000727 void CreatePeerConnectionWithDifferentConfigurations() {
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700728 CreatePeerConnectionWithIceServer(kStunAddressOnly, "");
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800729 EXPECT_EQ(1u, port_allocator_->stun_servers().size());
730 EXPECT_EQ(0u, port_allocator_->turn_servers().size());
731 EXPECT_EQ("address", port_allocator_->stun_servers().begin()->hostname());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000732 EXPECT_EQ(kDefaultStunPort,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800733 port_allocator_->stun_servers().begin()->port());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000734
deadbeef0a6c4ca2015-10-06 11:38:28 -0700735 CreatePeerConnectionExpectFail(kStunInvalidPort);
736 CreatePeerConnectionExpectFail(kStunAddressPortAndMore1);
737 CreatePeerConnectionExpectFail(kStunAddressPortAndMore2);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000738
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700739 CreatePeerConnectionWithIceServer(kTurnIceServerUri, kTurnPassword);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800740 EXPECT_EQ(0u, port_allocator_->stun_servers().size());
741 EXPECT_EQ(1u, port_allocator_->turn_servers().size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000742 EXPECT_EQ(kTurnUsername,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800743 port_allocator_->turn_servers()[0].credentials.username);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000744 EXPECT_EQ(kTurnPassword,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800745 port_allocator_->turn_servers()[0].credentials.password);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000746 EXPECT_EQ(kTurnHostname,
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800747 port_allocator_->turn_servers()[0].ports[0].address.hostname());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000748 }
749
750 void ReleasePeerConnection() {
751 pc_ = NULL;
752 observer_.SetPeerConnectionInterface(NULL);
753 }
754
deadbeefab9b2d12015-10-14 11:33:11 -0700755 void AddVideoStream(const std::string& label) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000756 // Create a local stream.
zhihuang9763d562016-08-05 11:14:50 -0700757 rtc::scoped_refptr<MediaStreamInterface> stream(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000758 pc_factory_->CreateLocalMediaStream(label));
zhihuang9763d562016-08-05 11:14:50 -0700759 rtc::scoped_refptr<VideoTrackSourceInterface> video_source(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000760 pc_factory_->CreateVideoSource(new cricket::FakeVideoCapturer(), NULL));
zhihuang9763d562016-08-05 11:14:50 -0700761 rtc::scoped_refptr<VideoTrackInterface> video_track(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000762 pc_factory_->CreateVideoTrack(label + "v0", video_source));
763 stream->AddTrack(video_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +0000764 EXPECT_TRUE(pc_->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000765 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
766 observer_.renegotiation_needed_ = false;
767 }
768
769 void AddVoiceStream(const std::string& label) {
770 // Create a local stream.
zhihuang9763d562016-08-05 11:14:50 -0700771 rtc::scoped_refptr<MediaStreamInterface> stream(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000772 pc_factory_->CreateLocalMediaStream(label));
zhihuang9763d562016-08-05 11:14:50 -0700773 rtc::scoped_refptr<AudioTrackInterface> audio_track(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000774 pc_factory_->CreateAudioTrack(label + "a0", NULL));
775 stream->AddTrack(audio_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +0000776 EXPECT_TRUE(pc_->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000777 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
778 observer_.renegotiation_needed_ = false;
779 }
780
781 void AddAudioVideoStream(const std::string& stream_label,
782 const std::string& audio_track_label,
783 const std::string& video_track_label) {
784 // Create a local stream.
zhihuang9763d562016-08-05 11:14:50 -0700785 rtc::scoped_refptr<MediaStreamInterface> stream(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000786 pc_factory_->CreateLocalMediaStream(stream_label));
zhihuang9763d562016-08-05 11:14:50 -0700787 rtc::scoped_refptr<AudioTrackInterface> audio_track(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000788 pc_factory_->CreateAudioTrack(
789 audio_track_label, static_cast<AudioSourceInterface*>(NULL)));
790 stream->AddTrack(audio_track.get());
zhihuang9763d562016-08-05 11:14:50 -0700791 rtc::scoped_refptr<VideoTrackInterface> video_track(
nisseaf510af2016-03-21 08:20:42 -0700792 pc_factory_->CreateVideoTrack(
793 video_track_label,
794 pc_factory_->CreateVideoSource(new cricket::FakeVideoCapturer())));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000795 stream->AddTrack(video_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +0000796 EXPECT_TRUE(pc_->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000797 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
798 observer_.renegotiation_needed_ = false;
799 }
800
kwibergd1fe2812016-04-27 06:47:29 -0700801 bool DoCreateOfferAnswer(std::unique_ptr<SessionDescriptionInterface>* desc,
deadbeefc80741f2015-10-22 13:14:45 -0700802 bool offer,
803 MediaConstraintsInterface* constraints) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000804 rtc::scoped_refptr<MockCreateSessionDescriptionObserver>
805 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000806 MockCreateSessionDescriptionObserver>());
807 if (offer) {
deadbeefc80741f2015-10-22 13:14:45 -0700808 pc_->CreateOffer(observer, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000809 } else {
deadbeefc80741f2015-10-22 13:14:45 -0700810 pc_->CreateAnswer(observer, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000811 }
812 EXPECT_EQ_WAIT(true, observer->called(), kTimeout);
kwiberg2bbff992016-03-16 11:03:04 -0700813 desc->reset(observer->release_desc());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000814 return observer->result();
815 }
816
kwibergd1fe2812016-04-27 06:47:29 -0700817 bool DoCreateOffer(std::unique_ptr<SessionDescriptionInterface>* desc,
deadbeefc80741f2015-10-22 13:14:45 -0700818 MediaConstraintsInterface* constraints) {
819 return DoCreateOfferAnswer(desc, true, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000820 }
821
kwibergd1fe2812016-04-27 06:47:29 -0700822 bool DoCreateAnswer(std::unique_ptr<SessionDescriptionInterface>* desc,
deadbeefc80741f2015-10-22 13:14:45 -0700823 MediaConstraintsInterface* constraints) {
824 return DoCreateOfferAnswer(desc, false, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000825 }
826
827 bool DoSetSessionDescription(SessionDescriptionInterface* desc, bool local) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000828 rtc::scoped_refptr<MockSetSessionDescriptionObserver>
829 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000830 MockSetSessionDescriptionObserver>());
831 if (local) {
832 pc_->SetLocalDescription(observer, desc);
833 } else {
834 pc_->SetRemoteDescription(observer, desc);
835 }
zhihuang29ff8442016-07-27 11:07:25 -0700836 if (pc_->signaling_state() != PeerConnectionInterface::kClosed) {
837 EXPECT_EQ_WAIT(true, observer->called(), kTimeout);
838 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000839 return observer->result();
840 }
841
842 bool DoSetLocalDescription(SessionDescriptionInterface* desc) {
843 return DoSetSessionDescription(desc, true);
844 }
845
846 bool DoSetRemoteDescription(SessionDescriptionInterface* desc) {
847 return DoSetSessionDescription(desc, false);
848 }
849
850 // Calls PeerConnection::GetStats and check the return value.
851 // It does not verify the values in the StatReports since a RTCP packet might
852 // be required.
853 bool DoGetStats(MediaStreamTrackInterface* track) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000854 rtc::scoped_refptr<MockStatsObserver> observer(
855 new rtc::RefCountedObject<MockStatsObserver>());
jiayl@webrtc.orgdb41b4d2014-03-03 21:30:06 +0000856 if (!pc_->GetStats(
857 observer, track, PeerConnectionInterface::kStatsOutputLevelStandard))
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000858 return false;
859 EXPECT_TRUE_WAIT(observer->called(), kTimeout);
860 return observer->called();
861 }
862
863 void InitiateCall() {
864 CreatePeerConnection();
865 // Create a local stream with audio&video tracks.
866 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
867 CreateOfferReceiveAnswer();
868 }
869
870 // Verify that RTP Header extensions has been negotiated for audio and video.
871 void VerifyRemoteRtpHeaderExtensions() {
872 const cricket::MediaContentDescription* desc =
873 cricket::GetFirstAudioContentDescription(
874 pc_->remote_description()->description());
875 ASSERT_TRUE(desc != NULL);
876 EXPECT_GT(desc->rtp_header_extensions().size(), 0u);
877
878 desc = cricket::GetFirstVideoContentDescription(
879 pc_->remote_description()->description());
880 ASSERT_TRUE(desc != NULL);
881 EXPECT_GT(desc->rtp_header_extensions().size(), 0u);
882 }
883
884 void CreateOfferAsRemoteDescription() {
kwibergd1fe2812016-04-27 06:47:29 -0700885 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -0700886 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000887 std::string sdp;
888 EXPECT_TRUE(offer->ToString(&sdp));
889 SessionDescriptionInterface* remote_offer =
890 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
891 sdp, NULL);
892 EXPECT_TRUE(DoSetRemoteDescription(remote_offer));
893 EXPECT_EQ(PeerConnectionInterface::kHaveRemoteOffer, observer_.state_);
894 }
895
deadbeefab9b2d12015-10-14 11:33:11 -0700896 void CreateAndSetRemoteOffer(const std::string& sdp) {
897 SessionDescriptionInterface* remote_offer =
898 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
899 sdp, nullptr);
900 EXPECT_TRUE(DoSetRemoteDescription(remote_offer));
901 EXPECT_EQ(PeerConnectionInterface::kHaveRemoteOffer, observer_.state_);
902 }
903
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000904 void CreateAnswerAsLocalDescription() {
kwibergd1fe2812016-04-27 06:47:29 -0700905 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -0700906 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000907
908 // TODO(perkj): Currently SetLocalDescription fails if any parameters in an
909 // audio codec change, even if the parameter has nothing to do with
910 // receiving. Not all parameters are serialized to SDP.
911 // Since CreatePrAnswerAsLocalDescription serialize/deserialize
912 // the SessionDescription, it is necessary to do that here to in order to
913 // get ReceiveOfferCreatePrAnswerAndAnswer and RenegotiateAudioOnly to pass.
914 // https://code.google.com/p/webrtc/issues/detail?id=1356
915 std::string sdp;
916 EXPECT_TRUE(answer->ToString(&sdp));
917 SessionDescriptionInterface* new_answer =
918 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
919 sdp, NULL);
920 EXPECT_TRUE(DoSetLocalDescription(new_answer));
921 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
922 }
923
924 void CreatePrAnswerAsLocalDescription() {
kwibergd1fe2812016-04-27 06:47:29 -0700925 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -0700926 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000927
928 std::string sdp;
929 EXPECT_TRUE(answer->ToString(&sdp));
930 SessionDescriptionInterface* pr_answer =
931 webrtc::CreateSessionDescription(SessionDescriptionInterface::kPrAnswer,
932 sdp, NULL);
933 EXPECT_TRUE(DoSetLocalDescription(pr_answer));
934 EXPECT_EQ(PeerConnectionInterface::kHaveLocalPrAnswer, observer_.state_);
935 }
936
937 void CreateOfferReceiveAnswer() {
938 CreateOfferAsLocalDescription();
939 std::string sdp;
940 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
941 CreateAnswerAsRemoteDescription(sdp);
942 }
943
944 void CreateOfferAsLocalDescription() {
kwibergd1fe2812016-04-27 06:47:29 -0700945 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -0700946 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000947 // TODO(perkj): Currently SetLocalDescription fails if any parameters in an
948 // audio codec change, even if the parameter has nothing to do with
949 // receiving. Not all parameters are serialized to SDP.
950 // Since CreatePrAnswerAsLocalDescription serialize/deserialize
951 // the SessionDescription, it is necessary to do that here to in order to
952 // get ReceiveOfferCreatePrAnswerAndAnswer and RenegotiateAudioOnly to pass.
953 // https://code.google.com/p/webrtc/issues/detail?id=1356
954 std::string sdp;
955 EXPECT_TRUE(offer->ToString(&sdp));
956 SessionDescriptionInterface* new_offer =
957 webrtc::CreateSessionDescription(
958 SessionDescriptionInterface::kOffer,
959 sdp, NULL);
960
961 EXPECT_TRUE(DoSetLocalDescription(new_offer));
962 EXPECT_EQ(PeerConnectionInterface::kHaveLocalOffer, observer_.state_);
mallinath@webrtc.org68cbd012014-01-22 00:16:46 +0000963 // Wait for the ice_complete message, so that SDP will have candidates.
964 EXPECT_TRUE_WAIT(observer_.ice_complete_, kTimeout);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000965 }
966
deadbeefab9b2d12015-10-14 11:33:11 -0700967 void CreateAnswerAsRemoteDescription(const std::string& sdp) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000968 webrtc::JsepSessionDescription* answer = new webrtc::JsepSessionDescription(
969 SessionDescriptionInterface::kAnswer);
deadbeefab9b2d12015-10-14 11:33:11 -0700970 EXPECT_TRUE(answer->Initialize(sdp, NULL));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000971 EXPECT_TRUE(DoSetRemoteDescription(answer));
972 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
973 }
974
deadbeefab9b2d12015-10-14 11:33:11 -0700975 void CreatePrAnswerAndAnswerAsRemoteDescription(const std::string& sdp) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000976 webrtc::JsepSessionDescription* pr_answer =
977 new webrtc::JsepSessionDescription(
978 SessionDescriptionInterface::kPrAnswer);
deadbeefab9b2d12015-10-14 11:33:11 -0700979 EXPECT_TRUE(pr_answer->Initialize(sdp, NULL));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000980 EXPECT_TRUE(DoSetRemoteDescription(pr_answer));
981 EXPECT_EQ(PeerConnectionInterface::kHaveRemotePrAnswer, observer_.state_);
982 webrtc::JsepSessionDescription* answer =
983 new webrtc::JsepSessionDescription(
984 SessionDescriptionInterface::kAnswer);
deadbeefab9b2d12015-10-14 11:33:11 -0700985 EXPECT_TRUE(answer->Initialize(sdp, NULL));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000986 EXPECT_TRUE(DoSetRemoteDescription(answer));
987 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
988 }
989
990 // Help function used for waiting until a the last signaled remote stream has
991 // the same label as |stream_label|. In a few of the tests in this file we
992 // answer with the same session description as we offer and thus we can
993 // check if OnAddStream have been called with the same stream as we offer to
994 // send.
995 void WaitAndVerifyOnAddStream(const std::string& stream_label) {
996 EXPECT_EQ_WAIT(stream_label, observer_.GetLastAddedStreamLabel(), kTimeout);
997 }
998
999 // Creates an offer and applies it as a local session description.
1000 // Creates an answer with the same SDP an the offer but removes all lines
1001 // that start with a:ssrc"
1002 void CreateOfferReceiveAnswerWithoutSsrc() {
1003 CreateOfferAsLocalDescription();
1004 std::string sdp;
1005 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
1006 SetSsrcToZero(&sdp);
1007 CreateAnswerAsRemoteDescription(sdp);
1008 }
1009
deadbeefab9b2d12015-10-14 11:33:11 -07001010 // This function creates a MediaStream with label kStreams[0] and
1011 // |number_of_audio_tracks| and |number_of_video_tracks| tracks and the
1012 // corresponding SessionDescriptionInterface. The SessionDescriptionInterface
kwiberg2bbff992016-03-16 11:03:04 -07001013 // is returned and the MediaStream is stored in
deadbeefab9b2d12015-10-14 11:33:11 -07001014 // |reference_collection_|
kwibergd1fe2812016-04-27 06:47:29 -07001015 std::unique_ptr<SessionDescriptionInterface>
kwiberg2bbff992016-03-16 11:03:04 -07001016 CreateSessionDescriptionAndReference(size_t number_of_audio_tracks,
1017 size_t number_of_video_tracks) {
1018 EXPECT_LE(number_of_audio_tracks, 2u);
1019 EXPECT_LE(number_of_video_tracks, 2u);
deadbeefab9b2d12015-10-14 11:33:11 -07001020
1021 reference_collection_ = StreamCollection::Create();
1022 std::string sdp_ms1 = std::string(kSdpStringInit);
1023
1024 std::string mediastream_label = kStreams[0];
1025
1026 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream(
1027 webrtc::MediaStream::Create(mediastream_label));
1028 reference_collection_->AddStream(stream);
1029
1030 if (number_of_audio_tracks > 0) {
1031 sdp_ms1 += std::string(kSdpStringAudio);
1032 sdp_ms1 += std::string(kSdpStringMs1Audio0);
1033 AddAudioTrack(kAudioTracks[0], stream);
1034 }
1035 if (number_of_audio_tracks > 1) {
1036 sdp_ms1 += kSdpStringMs1Audio1;
1037 AddAudioTrack(kAudioTracks[1], stream);
1038 }
1039
1040 if (number_of_video_tracks > 0) {
1041 sdp_ms1 += std::string(kSdpStringVideo);
1042 sdp_ms1 += std::string(kSdpStringMs1Video0);
1043 AddVideoTrack(kVideoTracks[0], stream);
1044 }
1045 if (number_of_video_tracks > 1) {
1046 sdp_ms1 += kSdpStringMs1Video1;
1047 AddVideoTrack(kVideoTracks[1], stream);
1048 }
1049
kwibergd1fe2812016-04-27 06:47:29 -07001050 return std::unique_ptr<SessionDescriptionInterface>(
kwiberg2bbff992016-03-16 11:03:04 -07001051 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
1052 sdp_ms1, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07001053 }
1054
1055 void AddAudioTrack(const std::string& track_id,
1056 MediaStreamInterface* stream) {
1057 rtc::scoped_refptr<webrtc::AudioTrackInterface> audio_track(
1058 webrtc::AudioTrack::Create(track_id, nullptr));
1059 ASSERT_TRUE(stream->AddTrack(audio_track));
1060 }
1061
1062 void AddVideoTrack(const std::string& track_id,
1063 MediaStreamInterface* stream) {
1064 rtc::scoped_refptr<webrtc::VideoTrackInterface> video_track(
nisseaf510af2016-03-21 08:20:42 -07001065 webrtc::VideoTrack::Create(track_id,
1066 webrtc::FakeVideoTrackSource::Create()));
deadbeefab9b2d12015-10-14 11:33:11 -07001067 ASSERT_TRUE(stream->AddTrack(video_track));
1068 }
1069
kwibergfd8be342016-05-14 19:44:11 -07001070 std::unique_ptr<SessionDescriptionInterface> CreateOfferWithOneAudioStream() {
zhihuang8f65cdf2016-05-06 18:40:30 -07001071 CreatePeerConnection();
1072 AddVoiceStream(kStreamLabel1);
kwibergfd8be342016-05-14 19:44:11 -07001073 std::unique_ptr<SessionDescriptionInterface> offer;
zhihuang8f65cdf2016-05-06 18:40:30 -07001074 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
1075 return offer;
1076 }
1077
kwibergfd8be342016-05-14 19:44:11 -07001078 std::unique_ptr<SessionDescriptionInterface>
zhihuang8f65cdf2016-05-06 18:40:30 -07001079 CreateAnswerWithOneAudioStream() {
kwibergfd8be342016-05-14 19:44:11 -07001080 std::unique_ptr<SessionDescriptionInterface> offer =
zhihuang8f65cdf2016-05-06 18:40:30 -07001081 CreateOfferWithOneAudioStream();
1082 EXPECT_TRUE(DoSetRemoteDescription(offer.release()));
kwibergfd8be342016-05-14 19:44:11 -07001083 std::unique_ptr<SessionDescriptionInterface> answer;
zhihuang8f65cdf2016-05-06 18:40:30 -07001084 EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
1085 return answer;
1086 }
1087
1088 const std::string& GetFirstAudioStreamCname(
1089 const SessionDescriptionInterface* desc) {
1090 const cricket::ContentInfo* audio_content =
1091 cricket::GetFirstAudioContent(desc->description());
1092 const cricket::AudioContentDescription* audio_desc =
1093 static_cast<const cricket::AudioContentDescription*>(
1094 audio_content->description);
1095 return audio_desc->streams()[0].cname;
1096 }
1097
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08001098 cricket::FakePortAllocator* port_allocator_ = nullptr;
zhihuang9763d562016-08-05 11:14:50 -07001099 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface> pc_factory_;
1100 rtc::scoped_refptr<PeerConnectionFactoryForTest> pc_factory_for_test_;
1101 rtc::scoped_refptr<PeerConnectionInterface> pc_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001102 MockPeerConnectionObserver observer_;
deadbeefab9b2d12015-10-14 11:33:11 -07001103 rtc::scoped_refptr<StreamCollection> reference_collection_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001104};
1105
zhihuang29ff8442016-07-27 11:07:25 -07001106// Test that no callbacks on the PeerConnectionObserver are called after the
1107// PeerConnection is closed.
1108TEST_F(PeerConnectionInterfaceTest, CloseAndTestCallbackFunctions) {
zhihuang9763d562016-08-05 11:14:50 -07001109 rtc::scoped_refptr<PeerConnectionInterface> pc(
zhihuang29ff8442016-07-27 11:07:25 -07001110 pc_factory_for_test_->CreatePeerConnection(
1111 PeerConnectionInterface::RTCConfiguration(), nullptr, nullptr,
1112 nullptr, &observer_));
1113 observer_.SetPeerConnectionInterface(pc.get());
1114 pc->Close();
1115
1116 // No callbacks is expected to be called.
zhihuang81c3a032016-11-17 12:06:24 -08001117 observer_.callback_triggered_ = false;
zhihuang29ff8442016-07-27 11:07:25 -07001118 std::vector<cricket::Candidate> candidates;
1119 pc_factory_for_test_->transport_controller->SignalGatheringState(
1120 cricket::IceGatheringState{});
1121 pc_factory_for_test_->transport_controller->SignalCandidatesGathered(
1122 "", candidates);
1123 pc_factory_for_test_->transport_controller->SignalConnectionState(
1124 cricket::IceConnectionState{});
1125 pc_factory_for_test_->transport_controller->SignalCandidatesRemoved(
1126 candidates);
1127 pc_factory_for_test_->transport_controller->SignalReceiving(false);
zhihuang81c3a032016-11-17 12:06:24 -08001128 EXPECT_FALSE(observer_.callback_triggered_);
zhihuang29ff8442016-07-27 11:07:25 -07001129}
1130
zhihuang8f65cdf2016-05-06 18:40:30 -07001131// Generate different CNAMEs when PeerConnections are created.
1132// The CNAMEs are expected to be generated randomly. It is possible
1133// that the test fails, though the possibility is very low.
1134TEST_F(PeerConnectionInterfaceTest, CnameGenerationInOffer) {
kwibergfd8be342016-05-14 19:44:11 -07001135 std::unique_ptr<SessionDescriptionInterface> offer1 =
zhihuang8f65cdf2016-05-06 18:40:30 -07001136 CreateOfferWithOneAudioStream();
kwibergfd8be342016-05-14 19:44:11 -07001137 std::unique_ptr<SessionDescriptionInterface> offer2 =
zhihuang8f65cdf2016-05-06 18:40:30 -07001138 CreateOfferWithOneAudioStream();
1139 EXPECT_NE(GetFirstAudioStreamCname(offer1.get()),
1140 GetFirstAudioStreamCname(offer2.get()));
1141}
1142
1143TEST_F(PeerConnectionInterfaceTest, CnameGenerationInAnswer) {
kwibergfd8be342016-05-14 19:44:11 -07001144 std::unique_ptr<SessionDescriptionInterface> answer1 =
zhihuang8f65cdf2016-05-06 18:40:30 -07001145 CreateAnswerWithOneAudioStream();
kwibergfd8be342016-05-14 19:44:11 -07001146 std::unique_ptr<SessionDescriptionInterface> answer2 =
zhihuang8f65cdf2016-05-06 18:40:30 -07001147 CreateAnswerWithOneAudioStream();
1148 EXPECT_NE(GetFirstAudioStreamCname(answer1.get()),
1149 GetFirstAudioStreamCname(answer2.get()));
1150}
1151
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001152TEST_F(PeerConnectionInterfaceTest,
1153 CreatePeerConnectionWithDifferentConfigurations) {
1154 CreatePeerConnectionWithDifferentConfigurations();
1155}
1156
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001157TEST_F(PeerConnectionInterfaceTest,
1158 CreatePeerConnectionWithDifferentIceTransportsTypes) {
1159 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kNone);
1160 EXPECT_EQ(cricket::CF_NONE, port_allocator_->candidate_filter());
1161 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kRelay);
1162 EXPECT_EQ(cricket::CF_RELAY, port_allocator_->candidate_filter());
1163 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kNoHost);
1164 EXPECT_EQ(cricket::CF_ALL & ~cricket::CF_HOST,
1165 port_allocator_->candidate_filter());
1166 CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kAll);
1167 EXPECT_EQ(cricket::CF_ALL, port_allocator_->candidate_filter());
1168}
1169
1170// Test that when a PeerConnection is created with a nonzero candidate pool
1171// size, the pooled PortAllocatorSession is created with all the attributes
1172// in the RTCConfiguration.
1173TEST_F(PeerConnectionInterfaceTest, CreatePeerConnectionWithPooledCandidates) {
1174 PeerConnectionInterface::RTCConfiguration config;
1175 PeerConnectionInterface::IceServer server;
1176 server.uri = kStunAddressOnly;
1177 config.servers.push_back(server);
1178 config.type = PeerConnectionInterface::kRelay;
1179 config.disable_ipv6 = true;
1180 config.tcp_candidate_policy =
1181 PeerConnectionInterface::kTcpCandidatePolicyDisabled;
honghaiz60347052016-05-31 18:29:12 -07001182 config.candidate_network_policy =
1183 PeerConnectionInterface::kCandidateNetworkPolicyLowCost;
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001184 config.ice_candidate_pool_size = 1;
1185 CreatePeerConnection(config, nullptr);
1186
1187 const cricket::FakePortAllocatorSession* session =
1188 static_cast<const cricket::FakePortAllocatorSession*>(
1189 port_allocator_->GetPooledSession());
1190 ASSERT_NE(nullptr, session);
1191 EXPECT_EQ(1UL, session->stun_servers().size());
1192 EXPECT_EQ(0U, session->flags() & cricket::PORTALLOCATOR_ENABLE_IPV6);
1193 EXPECT_LT(0U, session->flags() & cricket::PORTALLOCATOR_DISABLE_TCP);
honghaiz60347052016-05-31 18:29:12 -07001194 EXPECT_LT(0U,
1195 session->flags() & cricket::PORTALLOCATOR_DISABLE_COSTLY_NETWORKS);
Taylor Brandstettera1c30352016-05-13 08:15:11 -07001196}
1197
Taylor Brandstetterf8e65772016-06-27 17:20:15 -07001198// Test that the PeerConnection initializes the port allocator passed into it,
1199// and on the correct thread.
1200TEST_F(PeerConnectionInterfaceTest,
1201 CreatePeerConnectionInitializesPortAllocator) {
1202 rtc::Thread network_thread;
1203 network_thread.Start();
1204 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface> pc_factory(
1205 webrtc::CreatePeerConnectionFactory(
1206 &network_thread, rtc::Thread::Current(), rtc::Thread::Current(),
1207 nullptr, nullptr, nullptr));
1208 std::unique_ptr<cricket::FakePortAllocator> port_allocator(
1209 new cricket::FakePortAllocator(&network_thread, nullptr));
1210 cricket::FakePortAllocator* raw_port_allocator = port_allocator.get();
1211 PeerConnectionInterface::RTCConfiguration config;
1212 rtc::scoped_refptr<PeerConnectionInterface> pc(
1213 pc_factory->CreatePeerConnection(
1214 config, nullptr, std::move(port_allocator), nullptr, &observer_));
1215 // FakePortAllocator RTC_CHECKs that it's initialized on the right thread,
1216 // so all we have to do here is check that it's initialized.
1217 EXPECT_TRUE(raw_port_allocator->initialized());
1218}
1219
deadbeef46c73892016-11-16 19:42:04 -08001220// Check that GetConfiguration returns the configuration the PeerConnection was
1221// constructed with, before SetConfiguration is called.
1222TEST_F(PeerConnectionInterfaceTest, GetConfigurationAfterCreatePeerConnection) {
1223 PeerConnectionInterface::RTCConfiguration config;
1224 config.type = PeerConnectionInterface::kRelay;
1225 CreatePeerConnection(config, nullptr);
1226
1227 PeerConnectionInterface::RTCConfiguration returned_config =
1228 pc_->GetConfiguration();
1229 EXPECT_EQ(PeerConnectionInterface::kRelay, returned_config.type);
1230}
1231
1232// Check that GetConfiguration returns the last configuration passed into
1233// SetConfiguration.
1234TEST_F(PeerConnectionInterfaceTest, GetConfigurationAfterSetConfiguration) {
1235 CreatePeerConnection();
1236
1237 PeerConnectionInterface::RTCConfiguration config;
1238 config.type = PeerConnectionInterface::kRelay;
1239 EXPECT_TRUE(pc_->SetConfiguration(config));
1240
1241 PeerConnectionInterface::RTCConfiguration returned_config =
1242 pc_->GetConfiguration();
1243 EXPECT_EQ(PeerConnectionInterface::kRelay, returned_config.type);
1244}
1245
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001246TEST_F(PeerConnectionInterfaceTest, AddStreams) {
1247 CreatePeerConnection();
deadbeefab9b2d12015-10-14 11:33:11 -07001248 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001249 AddVoiceStream(kStreamLabel2);
1250 ASSERT_EQ(2u, pc_->local_streams()->count());
1251
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001252 // Test we can add multiple local streams to one peerconnection.
zhihuang9763d562016-08-05 11:14:50 -07001253 rtc::scoped_refptr<MediaStreamInterface> stream(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001254 pc_factory_->CreateLocalMediaStream(kStreamLabel3));
zhihuang9763d562016-08-05 11:14:50 -07001255 rtc::scoped_refptr<AudioTrackInterface> audio_track(
1256 pc_factory_->CreateAudioTrack(kStreamLabel3,
1257 static_cast<AudioSourceInterface*>(NULL)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001258 stream->AddTrack(audio_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +00001259 EXPECT_TRUE(pc_->AddStream(stream));
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001260 EXPECT_EQ(3u, pc_->local_streams()->count());
1261
1262 // Remove the third stream.
1263 pc_->RemoveStream(pc_->local_streams()->at(2));
1264 EXPECT_EQ(2u, pc_->local_streams()->count());
1265
1266 // Remove the second stream.
1267 pc_->RemoveStream(pc_->local_streams()->at(1));
1268 EXPECT_EQ(1u, pc_->local_streams()->count());
1269
1270 // Remove the first stream.
1271 pc_->RemoveStream(pc_->local_streams()->at(0));
1272 EXPECT_EQ(0u, pc_->local_streams()->count());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001273}
1274
deadbeefab9b2d12015-10-14 11:33:11 -07001275// Test that the created offer includes streams we added.
1276TEST_F(PeerConnectionInterfaceTest, AddedStreamsPresentInOffer) {
1277 CreatePeerConnection();
1278 AddAudioVideoStream(kStreamLabel1, "audio_track", "video_track");
kwibergd1fe2812016-04-27 06:47:29 -07001279 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001280 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07001281
1282 const cricket::ContentInfo* audio_content =
1283 cricket::GetFirstAudioContent(offer->description());
1284 const cricket::AudioContentDescription* audio_desc =
1285 static_cast<const cricket::AudioContentDescription*>(
1286 audio_content->description);
1287 EXPECT_TRUE(
1288 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
1289
1290 const cricket::ContentInfo* video_content =
1291 cricket::GetFirstVideoContent(offer->description());
1292 const cricket::VideoContentDescription* video_desc =
1293 static_cast<const cricket::VideoContentDescription*>(
1294 video_content->description);
1295 EXPECT_TRUE(
1296 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1297
1298 // Add another stream and ensure the offer includes both the old and new
1299 // streams.
1300 AddAudioVideoStream(kStreamLabel2, "audio_track2", "video_track2");
kwiberg2bbff992016-03-16 11:03:04 -07001301 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07001302
1303 audio_content = cricket::GetFirstAudioContent(offer->description());
1304 audio_desc = static_cast<const cricket::AudioContentDescription*>(
1305 audio_content->description);
1306 EXPECT_TRUE(
1307 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
1308 EXPECT_TRUE(
1309 ContainsTrack(audio_desc->streams(), kStreamLabel2, "audio_track2"));
1310
1311 video_content = cricket::GetFirstVideoContent(offer->description());
1312 video_desc = static_cast<const cricket::VideoContentDescription*>(
1313 video_content->description);
1314 EXPECT_TRUE(
1315 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1316 EXPECT_TRUE(
1317 ContainsTrack(video_desc->streams(), kStreamLabel2, "video_track2"));
1318}
1319
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001320TEST_F(PeerConnectionInterfaceTest, RemoveStream) {
1321 CreatePeerConnection();
deadbeefab9b2d12015-10-14 11:33:11 -07001322 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001323 ASSERT_EQ(1u, pc_->local_streams()->count());
1324 pc_->RemoveStream(pc_->local_streams()->at(0));
1325 EXPECT_EQ(0u, pc_->local_streams()->count());
1326}
1327
deadbeefe1f9d832016-01-14 15:35:42 -08001328// Test for AddTrack and RemoveTrack methods.
1329// Tests that the created offer includes tracks we added,
1330// and that the RtpSenders are created correctly.
1331// Also tests that RemoveTrack removes the tracks from subsequent offers.
1332TEST_F(PeerConnectionInterfaceTest, AddTrackRemoveTrack) {
1333 CreatePeerConnection();
1334 // Create a dummy stream, so tracks share a stream label.
zhihuang9763d562016-08-05 11:14:50 -07001335 rtc::scoped_refptr<MediaStreamInterface> stream(
deadbeefe1f9d832016-01-14 15:35:42 -08001336 pc_factory_->CreateLocalMediaStream(kStreamLabel1));
1337 std::vector<MediaStreamInterface*> stream_list;
1338 stream_list.push_back(stream.get());
zhihuang9763d562016-08-05 11:14:50 -07001339 rtc::scoped_refptr<AudioTrackInterface> audio_track(
deadbeefe1f9d832016-01-14 15:35:42 -08001340 pc_factory_->CreateAudioTrack("audio_track", nullptr));
zhihuang9763d562016-08-05 11:14:50 -07001341 rtc::scoped_refptr<VideoTrackInterface> video_track(
1342 pc_factory_->CreateVideoTrack(
1343 "video_track",
1344 pc_factory_->CreateVideoSource(new cricket::FakeVideoCapturer())));
deadbeefe1f9d832016-01-14 15:35:42 -08001345 auto audio_sender = pc_->AddTrack(audio_track, stream_list);
1346 auto video_sender = pc_->AddTrack(video_track, stream_list);
deadbeefa601f5c2016-06-06 14:27:39 -07001347 EXPECT_EQ(1UL, audio_sender->stream_ids().size());
1348 EXPECT_EQ(kStreamLabel1, audio_sender->stream_ids()[0]);
deadbeefe1f9d832016-01-14 15:35:42 -08001349 EXPECT_EQ("audio_track", audio_sender->id());
1350 EXPECT_EQ(audio_track, audio_sender->track());
deadbeefa601f5c2016-06-06 14:27:39 -07001351 EXPECT_EQ(1UL, video_sender->stream_ids().size());
1352 EXPECT_EQ(kStreamLabel1, video_sender->stream_ids()[0]);
deadbeefe1f9d832016-01-14 15:35:42 -08001353 EXPECT_EQ("video_track", video_sender->id());
1354 EXPECT_EQ(video_track, video_sender->track());
1355
1356 // Now create an offer and check for the senders.
kwibergd1fe2812016-04-27 06:47:29 -07001357 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001358 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefe1f9d832016-01-14 15:35:42 -08001359
1360 const cricket::ContentInfo* audio_content =
1361 cricket::GetFirstAudioContent(offer->description());
1362 const cricket::AudioContentDescription* audio_desc =
1363 static_cast<const cricket::AudioContentDescription*>(
1364 audio_content->description);
1365 EXPECT_TRUE(
1366 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
1367
1368 const cricket::ContentInfo* video_content =
1369 cricket::GetFirstVideoContent(offer->description());
1370 const cricket::VideoContentDescription* video_desc =
1371 static_cast<const cricket::VideoContentDescription*>(
1372 video_content->description);
1373 EXPECT_TRUE(
1374 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1375
1376 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
1377
1378 // Now try removing the tracks.
1379 EXPECT_TRUE(pc_->RemoveTrack(audio_sender));
1380 EXPECT_TRUE(pc_->RemoveTrack(video_sender));
1381
1382 // Create a new offer and ensure it doesn't contain the removed senders.
kwiberg2bbff992016-03-16 11:03:04 -07001383 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefe1f9d832016-01-14 15:35:42 -08001384
1385 audio_content = cricket::GetFirstAudioContent(offer->description());
1386 audio_desc = static_cast<const cricket::AudioContentDescription*>(
1387 audio_content->description);
1388 EXPECT_FALSE(
1389 ContainsTrack(audio_desc->streams(), kStreamLabel1, "audio_track"));
1390
1391 video_content = cricket::GetFirstVideoContent(offer->description());
1392 video_desc = static_cast<const cricket::VideoContentDescription*>(
1393 video_content->description);
1394 EXPECT_FALSE(
1395 ContainsTrack(video_desc->streams(), kStreamLabel1, "video_track"));
1396
1397 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
1398
1399 // Calling RemoveTrack on a sender no longer attached to a PeerConnection
1400 // should return false.
1401 EXPECT_FALSE(pc_->RemoveTrack(audio_sender));
1402 EXPECT_FALSE(pc_->RemoveTrack(video_sender));
1403}
1404
1405// Test creating senders without a stream specified,
1406// expecting a random stream ID to be generated.
1407TEST_F(PeerConnectionInterfaceTest, AddTrackWithoutStream) {
1408 CreatePeerConnection();
1409 // Create a dummy stream, so tracks share a stream label.
zhihuang9763d562016-08-05 11:14:50 -07001410 rtc::scoped_refptr<AudioTrackInterface> audio_track(
deadbeefe1f9d832016-01-14 15:35:42 -08001411 pc_factory_->CreateAudioTrack("audio_track", nullptr));
zhihuang9763d562016-08-05 11:14:50 -07001412 rtc::scoped_refptr<VideoTrackInterface> video_track(
1413 pc_factory_->CreateVideoTrack(
1414 "video_track",
1415 pc_factory_->CreateVideoSource(new cricket::FakeVideoCapturer())));
deadbeefe1f9d832016-01-14 15:35:42 -08001416 auto audio_sender =
1417 pc_->AddTrack(audio_track, std::vector<MediaStreamInterface*>());
1418 auto video_sender =
1419 pc_->AddTrack(video_track, std::vector<MediaStreamInterface*>());
1420 EXPECT_EQ("audio_track", audio_sender->id());
1421 EXPECT_EQ(audio_track, audio_sender->track());
1422 EXPECT_EQ("video_track", video_sender->id());
1423 EXPECT_EQ(video_track, video_sender->track());
1424 // If the ID is truly a random GUID, it should be infinitely unlikely they
1425 // will be the same.
deadbeefa601f5c2016-06-06 14:27:39 -07001426 EXPECT_NE(video_sender->stream_ids(), audio_sender->stream_ids());
deadbeefe1f9d832016-01-14 15:35:42 -08001427}
1428
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001429TEST_F(PeerConnectionInterfaceTest, CreateOfferReceiveAnswer) {
1430 InitiateCall();
1431 WaitAndVerifyOnAddStream(kStreamLabel1);
1432 VerifyRemoteRtpHeaderExtensions();
1433}
1434
1435TEST_F(PeerConnectionInterfaceTest, CreateOfferReceivePrAnswerAndAnswer) {
1436 CreatePeerConnection();
deadbeefab9b2d12015-10-14 11:33:11 -07001437 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001438 CreateOfferAsLocalDescription();
1439 std::string offer;
1440 EXPECT_TRUE(pc_->local_description()->ToString(&offer));
1441 CreatePrAnswerAndAnswerAsRemoteDescription(offer);
1442 WaitAndVerifyOnAddStream(kStreamLabel1);
1443}
1444
1445TEST_F(PeerConnectionInterfaceTest, ReceiveOfferCreateAnswer) {
1446 CreatePeerConnection();
deadbeefab9b2d12015-10-14 11:33:11 -07001447 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001448
1449 CreateOfferAsRemoteDescription();
1450 CreateAnswerAsLocalDescription();
1451
1452 WaitAndVerifyOnAddStream(kStreamLabel1);
1453}
1454
1455TEST_F(PeerConnectionInterfaceTest, ReceiveOfferCreatePrAnswerAndAnswer) {
1456 CreatePeerConnection();
deadbeefab9b2d12015-10-14 11:33:11 -07001457 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001458
1459 CreateOfferAsRemoteDescription();
1460 CreatePrAnswerAsLocalDescription();
1461 CreateAnswerAsLocalDescription();
1462
1463 WaitAndVerifyOnAddStream(kStreamLabel1);
1464}
1465
1466TEST_F(PeerConnectionInterfaceTest, Renegotiate) {
1467 InitiateCall();
1468 ASSERT_EQ(1u, pc_->remote_streams()->count());
1469 pc_->RemoveStream(pc_->local_streams()->at(0));
1470 CreateOfferReceiveAnswer();
1471 EXPECT_EQ(0u, pc_->remote_streams()->count());
deadbeefab9b2d12015-10-14 11:33:11 -07001472 AddVideoStream(kStreamLabel1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001473 CreateOfferReceiveAnswer();
1474}
1475
1476// Tests that after negotiating an audio only call, the respondent can perform a
1477// renegotiation that removes the audio stream.
1478TEST_F(PeerConnectionInterfaceTest, RenegotiateAudioOnly) {
1479 CreatePeerConnection();
1480 AddVoiceStream(kStreamLabel1);
1481 CreateOfferAsRemoteDescription();
1482 CreateAnswerAsLocalDescription();
1483
1484 ASSERT_EQ(1u, pc_->remote_streams()->count());
1485 pc_->RemoveStream(pc_->local_streams()->at(0));
1486 CreateOfferReceiveAnswer();
1487 EXPECT_EQ(0u, pc_->remote_streams()->count());
1488}
1489
1490// Test that candidates are generated and that we can parse our own candidates.
1491TEST_F(PeerConnectionInterfaceTest, IceCandidates) {
1492 CreatePeerConnection();
1493
1494 EXPECT_FALSE(pc_->AddIceCandidate(observer_.last_candidate_.get()));
1495 // SetRemoteDescription takes ownership of offer.
kwibergd1fe2812016-04-27 06:47:29 -07001496 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefab9b2d12015-10-14 11:33:11 -07001497 AddVideoStream(kStreamLabel1);
deadbeefc80741f2015-10-22 13:14:45 -07001498 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
kwiberg2bbff992016-03-16 11:03:04 -07001499 EXPECT_TRUE(DoSetRemoteDescription(offer.release()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001500
1501 // SetLocalDescription takes ownership of answer.
kwibergd1fe2812016-04-27 06:47:29 -07001502 std::unique_ptr<SessionDescriptionInterface> answer;
deadbeefc80741f2015-10-22 13:14:45 -07001503 EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
kwiberg2bbff992016-03-16 11:03:04 -07001504 EXPECT_TRUE(DoSetLocalDescription(answer.release()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001505
1506 EXPECT_TRUE_WAIT(observer_.last_candidate_.get() != NULL, kTimeout);
1507 EXPECT_TRUE_WAIT(observer_.ice_complete_, kTimeout);
1508
1509 EXPECT_TRUE(pc_->AddIceCandidate(observer_.last_candidate_.get()));
1510}
1511
deadbeefab9b2d12015-10-14 11:33:11 -07001512// Test that CreateOffer and CreateAnswer will fail if the track labels are
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001513// not unique.
1514TEST_F(PeerConnectionInterfaceTest, CreateOfferAnswerWithInvalidStream) {
1515 CreatePeerConnection();
1516 // Create a regular offer for the CreateAnswer test later.
kwibergd1fe2812016-04-27 06:47:29 -07001517 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefc80741f2015-10-22 13:14:45 -07001518 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
kwiberg2bbff992016-03-16 11:03:04 -07001519 EXPECT_TRUE(offer);
1520 offer.reset();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001521
1522 // Create a local stream with audio&video tracks having same label.
1523 AddAudioVideoStream(kStreamLabel1, "track_label", "track_label");
1524
1525 // Test CreateOffer
deadbeefc80741f2015-10-22 13:14:45 -07001526 EXPECT_FALSE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001527
1528 // Test CreateAnswer
kwibergd1fe2812016-04-27 06:47:29 -07001529 std::unique_ptr<SessionDescriptionInterface> answer;
deadbeefc80741f2015-10-22 13:14:45 -07001530 EXPECT_FALSE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001531}
1532
1533// Test that we will get different SSRCs for each tracks in the offer and answer
1534// we created.
1535TEST_F(PeerConnectionInterfaceTest, SsrcInOfferAnswer) {
1536 CreatePeerConnection();
1537 // Create a local stream with audio&video tracks having different labels.
1538 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
1539
1540 // Test CreateOffer
kwibergd1fe2812016-04-27 06:47:29 -07001541 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001542 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001543 int audio_ssrc = 0;
1544 int video_ssrc = 0;
1545 EXPECT_TRUE(GetFirstSsrc(GetFirstAudioContent(offer->description()),
1546 &audio_ssrc));
1547 EXPECT_TRUE(GetFirstSsrc(GetFirstVideoContent(offer->description()),
1548 &video_ssrc));
1549 EXPECT_NE(audio_ssrc, video_ssrc);
1550
1551 // Test CreateAnswer
1552 EXPECT_TRUE(DoSetRemoteDescription(offer.release()));
kwibergd1fe2812016-04-27 06:47:29 -07001553 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -07001554 ASSERT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001555 audio_ssrc = 0;
1556 video_ssrc = 0;
1557 EXPECT_TRUE(GetFirstSsrc(GetFirstAudioContent(answer->description()),
1558 &audio_ssrc));
1559 EXPECT_TRUE(GetFirstSsrc(GetFirstVideoContent(answer->description()),
1560 &video_ssrc));
1561 EXPECT_NE(audio_ssrc, video_ssrc);
1562}
1563
deadbeefeb459812015-12-15 19:24:43 -08001564// Test that it's possible to call AddTrack on a MediaStream after adding
1565// the stream to a PeerConnection.
1566// TODO(deadbeef): Remove this test once this behavior is no longer supported.
1567TEST_F(PeerConnectionInterfaceTest, AddTrackAfterAddStream) {
1568 CreatePeerConnection();
1569 // Create audio stream and add to PeerConnection.
1570 AddVoiceStream(kStreamLabel1);
1571 MediaStreamInterface* stream = pc_->local_streams()->at(0);
1572
1573 // Add video track to the audio-only stream.
zhihuang9763d562016-08-05 11:14:50 -07001574 rtc::scoped_refptr<VideoTrackInterface> video_track(
1575 pc_factory_->CreateVideoTrack(
1576 "video_label",
1577 pc_factory_->CreateVideoSource(new cricket::FakeVideoCapturer())));
deadbeefeb459812015-12-15 19:24:43 -08001578 stream->AddTrack(video_track.get());
1579
kwibergd1fe2812016-04-27 06:47:29 -07001580 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001581 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefeb459812015-12-15 19:24:43 -08001582
1583 const cricket::MediaContentDescription* video_desc =
1584 cricket::GetFirstVideoContentDescription(offer->description());
1585 EXPECT_TRUE(video_desc != nullptr);
1586}
1587
1588// Test that it's possible to call RemoveTrack on a MediaStream after adding
1589// the stream to a PeerConnection.
1590// TODO(deadbeef): Remove this test once this behavior is no longer supported.
1591TEST_F(PeerConnectionInterfaceTest, RemoveTrackAfterAddStream) {
1592 CreatePeerConnection();
1593 // Create audio/video stream and add to PeerConnection.
1594 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
1595 MediaStreamInterface* stream = pc_->local_streams()->at(0);
1596
1597 // Remove the video track.
1598 stream->RemoveTrack(stream->GetVideoTracks()[0]);
1599
kwibergd1fe2812016-04-27 06:47:29 -07001600 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001601 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefeb459812015-12-15 19:24:43 -08001602
1603 const cricket::MediaContentDescription* video_desc =
1604 cricket::GetFirstVideoContentDescription(offer->description());
1605 EXPECT_TRUE(video_desc == nullptr);
1606}
1607
deadbeefbd7d8f72015-12-18 16:58:44 -08001608// Test creating a sender with a stream ID, and ensure the ID is populated
1609// in the offer.
1610TEST_F(PeerConnectionInterfaceTest, CreateSenderWithStream) {
1611 CreatePeerConnection();
1612 pc_->CreateSender("video", kStreamLabel1);
1613
kwibergd1fe2812016-04-27 06:47:29 -07001614 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07001615 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
deadbeefbd7d8f72015-12-18 16:58:44 -08001616
1617 const cricket::MediaContentDescription* video_desc =
1618 cricket::GetFirstVideoContentDescription(offer->description());
1619 ASSERT_TRUE(video_desc != nullptr);
1620 ASSERT_EQ(1u, video_desc->streams().size());
1621 EXPECT_EQ(kStreamLabel1, video_desc->streams()[0].sync_label);
1622}
1623
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001624// Test that we can specify a certain track that we want statistics about.
1625TEST_F(PeerConnectionInterfaceTest, GetStatsForSpecificTrack) {
1626 InitiateCall();
1627 ASSERT_LT(0u, pc_->remote_streams()->count());
1628 ASSERT_LT(0u, pc_->remote_streams()->at(0)->GetAudioTracks().size());
zhihuang9763d562016-08-05 11:14:50 -07001629 rtc::scoped_refptr<MediaStreamTrackInterface> remote_audio =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001630 pc_->remote_streams()->at(0)->GetAudioTracks()[0];
1631 EXPECT_TRUE(DoGetStats(remote_audio));
1632
1633 // Remove the stream. Since we are sending to our selves the local
1634 // and the remote stream is the same.
1635 pc_->RemoveStream(pc_->local_streams()->at(0));
1636 // Do a re-negotiation.
1637 CreateOfferReceiveAnswer();
1638
1639 ASSERT_EQ(0u, pc_->remote_streams()->count());
1640
1641 // Test that we still can get statistics for the old track. Even if it is not
1642 // sent any longer.
1643 EXPECT_TRUE(DoGetStats(remote_audio));
1644}
1645
1646// Test that we can get stats on a video track.
1647TEST_F(PeerConnectionInterfaceTest, GetStatsForVideoTrack) {
1648 InitiateCall();
1649 ASSERT_LT(0u, pc_->remote_streams()->count());
1650 ASSERT_LT(0u, pc_->remote_streams()->at(0)->GetVideoTracks().size());
zhihuang9763d562016-08-05 11:14:50 -07001651 rtc::scoped_refptr<MediaStreamTrackInterface> remote_video =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001652 pc_->remote_streams()->at(0)->GetVideoTracks()[0];
1653 EXPECT_TRUE(DoGetStats(remote_video));
1654}
1655
1656// Test that we don't get statistics for an invalid track.
zhihuange9e94c32016-11-04 11:38:15 -07001657TEST_F(PeerConnectionInterfaceTest, GetStatsForInvalidTrack) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001658 InitiateCall();
zhihuang9763d562016-08-05 11:14:50 -07001659 rtc::scoped_refptr<AudioTrackInterface> unknown_audio_track(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001660 pc_factory_->CreateAudioTrack("unknown track", NULL));
1661 EXPECT_FALSE(DoGetStats(unknown_audio_track));
1662}
1663
1664// This test setup two RTP data channels in loop back.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001665TEST_F(PeerConnectionInterfaceTest, TestDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001666 FakeConstraints constraints;
1667 constraints.SetAllowRtpDataChannels();
1668 CreatePeerConnection(&constraints);
zhihuang9763d562016-08-05 11:14:50 -07001669 rtc::scoped_refptr<DataChannelInterface> data1 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001670 pc_->CreateDataChannel("test1", NULL);
zhihuang9763d562016-08-05 11:14:50 -07001671 rtc::scoped_refptr<DataChannelInterface> data2 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001672 pc_->CreateDataChannel("test2", NULL);
1673 ASSERT_TRUE(data1 != NULL);
kwibergd1fe2812016-04-27 06:47:29 -07001674 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001675 new MockDataChannelObserver(data1));
kwibergd1fe2812016-04-27 06:47:29 -07001676 std::unique_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001677 new MockDataChannelObserver(data2));
1678
1679 EXPECT_EQ(DataChannelInterface::kConnecting, data1->state());
1680 EXPECT_EQ(DataChannelInterface::kConnecting, data2->state());
1681 std::string data_to_send1 = "testing testing";
1682 std::string data_to_send2 = "testing something else";
1683 EXPECT_FALSE(data1->Send(DataBuffer(data_to_send1)));
1684
1685 CreateOfferReceiveAnswer();
1686 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1687 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
1688
1689 EXPECT_EQ(DataChannelInterface::kOpen, data1->state());
1690 EXPECT_EQ(DataChannelInterface::kOpen, data2->state());
1691 EXPECT_TRUE(data1->Send(DataBuffer(data_to_send1)));
1692 EXPECT_TRUE(data2->Send(DataBuffer(data_to_send2)));
1693
1694 EXPECT_EQ_WAIT(data_to_send1, observer1->last_message(), kTimeout);
1695 EXPECT_EQ_WAIT(data_to_send2, observer2->last_message(), kTimeout);
1696
1697 data1->Close();
1698 EXPECT_EQ(DataChannelInterface::kClosing, data1->state());
1699 CreateOfferReceiveAnswer();
1700 EXPECT_FALSE(observer1->IsOpen());
1701 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
1702 EXPECT_TRUE(observer2->IsOpen());
1703
1704 data_to_send2 = "testing something else again";
1705 EXPECT_TRUE(data2->Send(DataBuffer(data_to_send2)));
1706
1707 EXPECT_EQ_WAIT(data_to_send2, observer2->last_message(), kTimeout);
1708}
1709
1710// This test verifies that sendnig binary data over RTP data channels should
1711// fail.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001712TEST_F(PeerConnectionInterfaceTest, TestSendBinaryOnRtpDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001713 FakeConstraints constraints;
1714 constraints.SetAllowRtpDataChannels();
1715 CreatePeerConnection(&constraints);
zhihuang9763d562016-08-05 11:14:50 -07001716 rtc::scoped_refptr<DataChannelInterface> data1 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001717 pc_->CreateDataChannel("test1", NULL);
zhihuang9763d562016-08-05 11:14:50 -07001718 rtc::scoped_refptr<DataChannelInterface> data2 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001719 pc_->CreateDataChannel("test2", NULL);
1720 ASSERT_TRUE(data1 != NULL);
kwibergd1fe2812016-04-27 06:47:29 -07001721 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001722 new MockDataChannelObserver(data1));
kwibergd1fe2812016-04-27 06:47:29 -07001723 std::unique_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001724 new MockDataChannelObserver(data2));
1725
1726 EXPECT_EQ(DataChannelInterface::kConnecting, data1->state());
1727 EXPECT_EQ(DataChannelInterface::kConnecting, data2->state());
1728
1729 CreateOfferReceiveAnswer();
1730 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1731 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
1732
1733 EXPECT_EQ(DataChannelInterface::kOpen, data1->state());
1734 EXPECT_EQ(DataChannelInterface::kOpen, data2->state());
1735
jbaucheec21bd2016-03-20 06:15:43 -07001736 rtc::CopyOnWriteBuffer buffer("test", 4);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001737 EXPECT_FALSE(data1->Send(DataBuffer(buffer, true)));
1738}
1739
1740// This test setup a RTP data channels in loop back and test that a channel is
1741// opened even if the remote end answer with a zero SSRC.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001742TEST_F(PeerConnectionInterfaceTest, TestSendOnlyDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001743 FakeConstraints constraints;
1744 constraints.SetAllowRtpDataChannels();
1745 CreatePeerConnection(&constraints);
zhihuang9763d562016-08-05 11:14:50 -07001746 rtc::scoped_refptr<DataChannelInterface> data1 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001747 pc_->CreateDataChannel("test1", NULL);
kwibergd1fe2812016-04-27 06:47:29 -07001748 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001749 new MockDataChannelObserver(data1));
1750
1751 CreateOfferReceiveAnswerWithoutSsrc();
1752
1753 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1754
1755 data1->Close();
1756 EXPECT_EQ(DataChannelInterface::kClosing, data1->state());
1757 CreateOfferReceiveAnswerWithoutSsrc();
1758 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
1759 EXPECT_FALSE(observer1->IsOpen());
1760}
1761
1762// This test that if a data channel is added in an answer a receive only channel
1763// channel is created.
1764TEST_F(PeerConnectionInterfaceTest, TestReceiveOnlyDataChannel) {
1765 FakeConstraints constraints;
1766 constraints.SetAllowRtpDataChannels();
1767 CreatePeerConnection(&constraints);
1768
1769 std::string offer_label = "offer_channel";
zhihuang9763d562016-08-05 11:14:50 -07001770 rtc::scoped_refptr<DataChannelInterface> offer_channel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001771 pc_->CreateDataChannel(offer_label, NULL);
1772
1773 CreateOfferAsLocalDescription();
1774
1775 // Replace the data channel label in the offer and apply it as an answer.
1776 std::string receive_label = "answer_channel";
1777 std::string sdp;
1778 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001779 rtc::replace_substrs(offer_label.c_str(), offer_label.length(),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001780 receive_label.c_str(), receive_label.length(),
1781 &sdp);
1782 CreateAnswerAsRemoteDescription(sdp);
1783
1784 // Verify that a new incoming data channel has been created and that
1785 // it is open but can't we written to.
1786 ASSERT_TRUE(observer_.last_datachannel_ != NULL);
1787 DataChannelInterface* received_channel = observer_.last_datachannel_;
1788 EXPECT_EQ(DataChannelInterface::kConnecting, received_channel->state());
1789 EXPECT_EQ(receive_label, received_channel->label());
1790 EXPECT_FALSE(received_channel->Send(DataBuffer("something")));
1791
1792 // Verify that the channel we initially offered has been rejected.
1793 EXPECT_EQ(DataChannelInterface::kClosed, offer_channel->state());
1794
1795 // Do another offer / answer exchange and verify that the data channel is
1796 // opened.
1797 CreateOfferReceiveAnswer();
1798 EXPECT_EQ_WAIT(DataChannelInterface::kOpen, received_channel->state(),
1799 kTimeout);
1800}
1801
1802// This test that no data channel is returned if a reliable channel is
1803// requested.
1804// TODO(perkj): Remove this test once reliable channels are implemented.
1805TEST_F(PeerConnectionInterfaceTest, CreateReliableRtpDataChannelShouldFail) {
1806 FakeConstraints constraints;
1807 constraints.SetAllowRtpDataChannels();
1808 CreatePeerConnection(&constraints);
1809
1810 std::string label = "test";
1811 webrtc::DataChannelInit config;
1812 config.reliable = true;
zhihuang9763d562016-08-05 11:14:50 -07001813 rtc::scoped_refptr<DataChannelInterface> channel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001814 pc_->CreateDataChannel(label, &config);
1815 EXPECT_TRUE(channel == NULL);
1816}
1817
deadbeefab9b2d12015-10-14 11:33:11 -07001818// Verifies that duplicated label is not allowed for RTP data channel.
1819TEST_F(PeerConnectionInterfaceTest, RtpDuplicatedLabelNotAllowed) {
1820 FakeConstraints constraints;
1821 constraints.SetAllowRtpDataChannels();
1822 CreatePeerConnection(&constraints);
1823
1824 std::string label = "test";
zhihuang9763d562016-08-05 11:14:50 -07001825 rtc::scoped_refptr<DataChannelInterface> channel =
deadbeefab9b2d12015-10-14 11:33:11 -07001826 pc_->CreateDataChannel(label, nullptr);
1827 EXPECT_NE(channel, nullptr);
1828
zhihuang9763d562016-08-05 11:14:50 -07001829 rtc::scoped_refptr<DataChannelInterface> dup_channel =
deadbeefab9b2d12015-10-14 11:33:11 -07001830 pc_->CreateDataChannel(label, nullptr);
1831 EXPECT_EQ(dup_channel, nullptr);
1832}
1833
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001834// This tests that a SCTP data channel is returned using different
1835// DataChannelInit configurations.
1836TEST_F(PeerConnectionInterfaceTest, CreateSctpDataChannel) {
1837 FakeConstraints constraints;
1838 constraints.SetAllowDtlsSctpDataChannels();
1839 CreatePeerConnection(&constraints);
1840
1841 webrtc::DataChannelInit config;
1842
zhihuang9763d562016-08-05 11:14:50 -07001843 rtc::scoped_refptr<DataChannelInterface> channel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001844 pc_->CreateDataChannel("1", &config);
1845 EXPECT_TRUE(channel != NULL);
1846 EXPECT_TRUE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001847 EXPECT_TRUE(observer_.renegotiation_needed_);
1848 observer_.renegotiation_needed_ = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001849
1850 config.ordered = false;
1851 channel = pc_->CreateDataChannel("2", &config);
1852 EXPECT_TRUE(channel != NULL);
1853 EXPECT_TRUE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001854 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001855
1856 config.ordered = true;
1857 config.maxRetransmits = 0;
1858 channel = pc_->CreateDataChannel("3", &config);
1859 EXPECT_TRUE(channel != NULL);
1860 EXPECT_FALSE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001861 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001862
1863 config.maxRetransmits = -1;
1864 config.maxRetransmitTime = 0;
1865 channel = pc_->CreateDataChannel("4", &config);
1866 EXPECT_TRUE(channel != NULL);
1867 EXPECT_FALSE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001868 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001869}
1870
1871// This tests that no data channel is returned if both maxRetransmits and
1872// maxRetransmitTime are set for SCTP data channels.
1873TEST_F(PeerConnectionInterfaceTest,
1874 CreateSctpDataChannelShouldFailForInvalidConfig) {
1875 FakeConstraints constraints;
1876 constraints.SetAllowDtlsSctpDataChannels();
1877 CreatePeerConnection(&constraints);
1878
1879 std::string label = "test";
1880 webrtc::DataChannelInit config;
1881 config.maxRetransmits = 0;
1882 config.maxRetransmitTime = 0;
1883
zhihuang9763d562016-08-05 11:14:50 -07001884 rtc::scoped_refptr<DataChannelInterface> channel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001885 pc_->CreateDataChannel(label, &config);
1886 EXPECT_TRUE(channel == NULL);
1887}
1888
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001889// The test verifies that creating a SCTP data channel with an id already in use
1890// or out of range should fail.
1891TEST_F(PeerConnectionInterfaceTest,
1892 CreateSctpDataChannelWithInvalidIdShouldFail) {
1893 FakeConstraints constraints;
1894 constraints.SetAllowDtlsSctpDataChannels();
1895 CreatePeerConnection(&constraints);
1896
1897 webrtc::DataChannelInit config;
zhihuang9763d562016-08-05 11:14:50 -07001898 rtc::scoped_refptr<DataChannelInterface> channel;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001899
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001900 config.id = 1;
1901 channel = pc_->CreateDataChannel("1", &config);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001902 EXPECT_TRUE(channel != NULL);
1903 EXPECT_EQ(1, channel->id());
1904
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001905 channel = pc_->CreateDataChannel("x", &config);
1906 EXPECT_TRUE(channel == NULL);
1907
1908 config.id = cricket::kMaxSctpSid;
1909 channel = pc_->CreateDataChannel("max", &config);
1910 EXPECT_TRUE(channel != NULL);
1911 EXPECT_EQ(config.id, channel->id());
1912
1913 config.id = cricket::kMaxSctpSid + 1;
1914 channel = pc_->CreateDataChannel("x", &config);
1915 EXPECT_TRUE(channel == NULL);
1916}
1917
deadbeefab9b2d12015-10-14 11:33:11 -07001918// Verifies that duplicated label is allowed for SCTP data channel.
1919TEST_F(PeerConnectionInterfaceTest, SctpDuplicatedLabelAllowed) {
1920 FakeConstraints constraints;
1921 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
1922 true);
1923 CreatePeerConnection(&constraints);
1924
1925 std::string label = "test";
zhihuang9763d562016-08-05 11:14:50 -07001926 rtc::scoped_refptr<DataChannelInterface> channel =
deadbeefab9b2d12015-10-14 11:33:11 -07001927 pc_->CreateDataChannel(label, nullptr);
1928 EXPECT_NE(channel, nullptr);
1929
zhihuang9763d562016-08-05 11:14:50 -07001930 rtc::scoped_refptr<DataChannelInterface> dup_channel =
deadbeefab9b2d12015-10-14 11:33:11 -07001931 pc_->CreateDataChannel(label, nullptr);
1932 EXPECT_NE(dup_channel, nullptr);
1933}
1934
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001935// This test verifies that OnRenegotiationNeeded is fired for every new RTP
1936// DataChannel.
1937TEST_F(PeerConnectionInterfaceTest, RenegotiationNeededForNewRtpDataChannel) {
1938 FakeConstraints constraints;
1939 constraints.SetAllowRtpDataChannels();
1940 CreatePeerConnection(&constraints);
1941
zhihuang9763d562016-08-05 11:14:50 -07001942 rtc::scoped_refptr<DataChannelInterface> dc1 =
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001943 pc_->CreateDataChannel("test1", NULL);
1944 EXPECT_TRUE(observer_.renegotiation_needed_);
1945 observer_.renegotiation_needed_ = false;
1946
zhihuang9763d562016-08-05 11:14:50 -07001947 rtc::scoped_refptr<DataChannelInterface> dc2 =
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001948 pc_->CreateDataChannel("test2", NULL);
1949 EXPECT_TRUE(observer_.renegotiation_needed_);
1950}
1951
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001952// This test that a data channel closes when a PeerConnection is deleted/closed.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001953TEST_F(PeerConnectionInterfaceTest, DataChannelCloseWhenPeerConnectionClose) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001954 FakeConstraints constraints;
1955 constraints.SetAllowRtpDataChannels();
1956 CreatePeerConnection(&constraints);
1957
zhihuang9763d562016-08-05 11:14:50 -07001958 rtc::scoped_refptr<DataChannelInterface> data1 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001959 pc_->CreateDataChannel("test1", NULL);
zhihuang9763d562016-08-05 11:14:50 -07001960 rtc::scoped_refptr<DataChannelInterface> data2 =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001961 pc_->CreateDataChannel("test2", NULL);
1962 ASSERT_TRUE(data1 != NULL);
kwibergd1fe2812016-04-27 06:47:29 -07001963 std::unique_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001964 new MockDataChannelObserver(data1));
kwibergd1fe2812016-04-27 06:47:29 -07001965 std::unique_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001966 new MockDataChannelObserver(data2));
1967
1968 CreateOfferReceiveAnswer();
1969 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1970 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
1971
1972 ReleasePeerConnection();
1973 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
1974 EXPECT_EQ(DataChannelInterface::kClosed, data2->state());
1975}
1976
1977// This test that data channels can be rejected in an answer.
1978TEST_F(PeerConnectionInterfaceTest, TestRejectDataChannelInAnswer) {
1979 FakeConstraints constraints;
1980 constraints.SetAllowRtpDataChannels();
1981 CreatePeerConnection(&constraints);
1982
zhihuang9763d562016-08-05 11:14:50 -07001983 rtc::scoped_refptr<DataChannelInterface> offer_channel(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001984 pc_->CreateDataChannel("offer_channel", NULL));
1985
1986 CreateOfferAsLocalDescription();
1987
1988 // Create an answer where the m-line for data channels are rejected.
1989 std::string sdp;
1990 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
1991 webrtc::JsepSessionDescription* answer = new webrtc::JsepSessionDescription(
1992 SessionDescriptionInterface::kAnswer);
1993 EXPECT_TRUE(answer->Initialize(sdp, NULL));
1994 cricket::ContentInfo* data_info =
1995 answer->description()->GetContentByName("data");
1996 data_info->rejected = true;
1997
1998 DoSetRemoteDescription(answer);
1999 EXPECT_EQ(DataChannelInterface::kClosed, offer_channel->state());
2000}
2001
2002// Test that we can create a session description from an SDP string from
2003// FireFox, use it as a remote session description, generate an answer and use
2004// the answer as a local description.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002005TEST_F(PeerConnectionInterfaceTest, ReceiveFireFoxOffer) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002006 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002007 FakeConstraints constraints;
2008 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2009 true);
2010 CreatePeerConnection(&constraints);
2011 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
2012 SessionDescriptionInterface* desc =
2013 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
jbauchfabe2c92015-07-16 13:43:14 -07002014 webrtc::kFireFoxSdpOffer, nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002015 EXPECT_TRUE(DoSetSessionDescription(desc, false));
2016 CreateAnswerAsLocalDescription();
2017 ASSERT_TRUE(pc_->local_description() != NULL);
2018 ASSERT_TRUE(pc_->remote_description() != NULL);
2019
2020 const cricket::ContentInfo* content =
2021 cricket::GetFirstAudioContent(pc_->local_description()->description());
2022 ASSERT_TRUE(content != NULL);
2023 EXPECT_FALSE(content->rejected);
2024
2025 content =
2026 cricket::GetFirstVideoContent(pc_->local_description()->description());
2027 ASSERT_TRUE(content != NULL);
2028 EXPECT_FALSE(content->rejected);
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00002029#ifdef HAVE_SCTP
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002030 content =
2031 cricket::GetFirstDataContent(pc_->local_description()->description());
2032 ASSERT_TRUE(content != NULL);
2033 EXPECT_TRUE(content->rejected);
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00002034#endif
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002035}
2036
2037// Test that we can create an audio only offer and receive an answer with a
2038// limited set of audio codecs and receive an updated offer with more audio
2039// codecs, where the added codecs are not supported.
2040TEST_F(PeerConnectionInterfaceTest, ReceiveUpdatedAudioOfferWithBadCodecs) {
2041 CreatePeerConnection();
2042 AddVoiceStream("audio_label");
2043 CreateOfferAsLocalDescription();
2044
2045 SessionDescriptionInterface* answer =
2046 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
jbauchfabe2c92015-07-16 13:43:14 -07002047 webrtc::kAudioSdp, nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002048 EXPECT_TRUE(DoSetSessionDescription(answer, false));
2049
2050 SessionDescriptionInterface* updated_offer =
2051 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
jbauchfabe2c92015-07-16 13:43:14 -07002052 webrtc::kAudioSdpWithUnsupportedCodecs,
2053 nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002054 EXPECT_TRUE(DoSetSessionDescription(updated_offer, false));
2055 CreateAnswerAsLocalDescription();
2056}
2057
deadbeefc80741f2015-10-22 13:14:45 -07002058// Test that if we're receiving (but not sending) a track, subsequent offers
2059// will have m-lines with a=recvonly.
2060TEST_F(PeerConnectionInterfaceTest, CreateSubsequentRecvOnlyOffer) {
2061 FakeConstraints constraints;
2062 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2063 true);
2064 CreatePeerConnection(&constraints);
2065 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2066 CreateAnswerAsLocalDescription();
2067
2068 // At this point we should be receiving stream 1, but not sending anything.
2069 // A new offer should be recvonly.
kwibergd1fe2812016-04-27 06:47:29 -07002070 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefc80741f2015-10-22 13:14:45 -07002071 DoCreateOffer(&offer, nullptr);
2072
2073 const cricket::ContentInfo* video_content =
2074 cricket::GetFirstVideoContent(offer->description());
2075 const cricket::VideoContentDescription* video_desc =
2076 static_cast<const cricket::VideoContentDescription*>(
2077 video_content->description);
2078 ASSERT_EQ(cricket::MD_RECVONLY, video_desc->direction());
2079
2080 const cricket::ContentInfo* audio_content =
2081 cricket::GetFirstAudioContent(offer->description());
2082 const cricket::AudioContentDescription* audio_desc =
2083 static_cast<const cricket::AudioContentDescription*>(
2084 audio_content->description);
2085 ASSERT_EQ(cricket::MD_RECVONLY, audio_desc->direction());
2086}
2087
2088// Test that if we're receiving (but not sending) a track, and the
2089// offerToReceiveVideo/offerToReceiveAudio constraints are explicitly set to
2090// false, the generated m-lines will be a=inactive.
2091TEST_F(PeerConnectionInterfaceTest, CreateSubsequentInactiveOffer) {
2092 FakeConstraints constraints;
2093 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2094 true);
2095 CreatePeerConnection(&constraints);
2096 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2097 CreateAnswerAsLocalDescription();
2098
2099 // At this point we should be receiving stream 1, but not sending anything.
2100 // A new offer would be recvonly, but we'll set the "no receive" constraints
2101 // to make it inactive.
kwibergd1fe2812016-04-27 06:47:29 -07002102 std::unique_ptr<SessionDescriptionInterface> offer;
deadbeefc80741f2015-10-22 13:14:45 -07002103 FakeConstraints offer_constraints;
2104 offer_constraints.AddMandatory(
2105 webrtc::MediaConstraintsInterface::kOfferToReceiveVideo, false);
2106 offer_constraints.AddMandatory(
2107 webrtc::MediaConstraintsInterface::kOfferToReceiveAudio, false);
2108 DoCreateOffer(&offer, &offer_constraints);
2109
2110 const cricket::ContentInfo* video_content =
2111 cricket::GetFirstVideoContent(offer->description());
2112 const cricket::VideoContentDescription* video_desc =
2113 static_cast<const cricket::VideoContentDescription*>(
2114 video_content->description);
2115 ASSERT_EQ(cricket::MD_INACTIVE, video_desc->direction());
2116
2117 const cricket::ContentInfo* audio_content =
2118 cricket::GetFirstAudioContent(offer->description());
2119 const cricket::AudioContentDescription* audio_desc =
2120 static_cast<const cricket::AudioContentDescription*>(
2121 audio_content->description);
2122 ASSERT_EQ(cricket::MD_INACTIVE, audio_desc->direction());
2123}
2124
deadbeef653b8e02015-11-11 12:55:10 -08002125// Test that we can use SetConfiguration to change the ICE servers of the
2126// PortAllocator.
2127TEST_F(PeerConnectionInterfaceTest, SetConfigurationChangesIceServers) {
2128 CreatePeerConnection();
2129
2130 PeerConnectionInterface::RTCConfiguration config;
2131 PeerConnectionInterface::IceServer server;
2132 server.uri = "stun:test_hostname";
2133 config.servers.push_back(server);
2134 EXPECT_TRUE(pc_->SetConfiguration(config));
2135
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002136 EXPECT_EQ(1u, port_allocator_->stun_servers().size());
2137 EXPECT_EQ("test_hostname",
2138 port_allocator_->stun_servers().begin()->hostname());
deadbeef653b8e02015-11-11 12:55:10 -08002139}
2140
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002141TEST_F(PeerConnectionInterfaceTest, SetConfigurationChangesCandidateFilter) {
2142 CreatePeerConnection();
2143 PeerConnectionInterface::RTCConfiguration config;
2144 config.type = PeerConnectionInterface::kRelay;
2145 EXPECT_TRUE(pc_->SetConfiguration(config));
2146 EXPECT_EQ(cricket::CF_RELAY, port_allocator_->candidate_filter());
2147}
2148
2149// Test that when SetConfiguration changes both the pool size and other
2150// attributes, the pooled session is created with the updated attributes.
2151TEST_F(PeerConnectionInterfaceTest,
2152 SetConfigurationCreatesPooledSessionCorrectly) {
2153 CreatePeerConnection();
2154 PeerConnectionInterface::RTCConfiguration config;
2155 config.ice_candidate_pool_size = 1;
2156 PeerConnectionInterface::IceServer server;
2157 server.uri = kStunAddressOnly;
2158 config.servers.push_back(server);
2159 config.type = PeerConnectionInterface::kRelay;
Taylor Brandstetter417eebe2016-05-23 16:02:19 -07002160 EXPECT_TRUE(pc_->SetConfiguration(config));
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002161
2162 const cricket::FakePortAllocatorSession* session =
2163 static_cast<const cricket::FakePortAllocatorSession*>(
2164 port_allocator_->GetPooledSession());
2165 ASSERT_NE(nullptr, session);
2166 EXPECT_EQ(1UL, session->stun_servers().size());
Taylor Brandstettera1c30352016-05-13 08:15:11 -07002167}
2168
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002169// Test that PeerConnection::Close changes the states to closed and all remote
2170// tracks change state to ended.
2171TEST_F(PeerConnectionInterfaceTest, CloseAndTestStreamsAndStates) {
2172 // Initialize a PeerConnection and negotiate local and remote session
2173 // description.
2174 InitiateCall();
2175 ASSERT_EQ(1u, pc_->local_streams()->count());
2176 ASSERT_EQ(1u, pc_->remote_streams()->count());
2177
2178 pc_->Close();
2179
2180 EXPECT_EQ(PeerConnectionInterface::kClosed, pc_->signaling_state());
2181 EXPECT_EQ(PeerConnectionInterface::kIceConnectionClosed,
2182 pc_->ice_connection_state());
2183 EXPECT_EQ(PeerConnectionInterface::kIceGatheringComplete,
2184 pc_->ice_gathering_state());
2185
2186 EXPECT_EQ(1u, pc_->local_streams()->count());
2187 EXPECT_EQ(1u, pc_->remote_streams()->count());
2188
zhihuang9763d562016-08-05 11:14:50 -07002189 rtc::scoped_refptr<MediaStreamInterface> remote_stream =
2190 pc_->remote_streams()->at(0);
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002191 // Track state may be updated asynchronously.
perkjd61bf802016-03-24 03:16:19 -07002192 EXPECT_EQ_WAIT(MediaStreamTrackInterface::kEnded,
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002193 remote_stream->GetAudioTracks()[0]->state(), kTimeout);
2194 EXPECT_EQ_WAIT(MediaStreamTrackInterface::kEnded,
2195 remote_stream->GetVideoTracks()[0]->state(), kTimeout);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002196}
2197
2198// Test that PeerConnection methods fails gracefully after
2199// PeerConnection::Close has been called.
2200TEST_F(PeerConnectionInterfaceTest, CloseAndTestMethods) {
2201 CreatePeerConnection();
2202 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
2203 CreateOfferAsRemoteDescription();
2204 CreateAnswerAsLocalDescription();
2205
2206 ASSERT_EQ(1u, pc_->local_streams()->count());
zhihuang9763d562016-08-05 11:14:50 -07002207 rtc::scoped_refptr<MediaStreamInterface> local_stream =
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002208 pc_->local_streams()->at(0);
2209
2210 pc_->Close();
2211
2212 pc_->RemoveStream(local_stream);
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +00002213 EXPECT_FALSE(pc_->AddStream(local_stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002214
2215 ASSERT_FALSE(local_stream->GetAudioTracks().empty());
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002216 rtc::scoped_refptr<webrtc::DtmfSenderInterface> dtmf_sender(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002217 pc_->CreateDtmfSender(local_stream->GetAudioTracks()[0]));
wu@webrtc.org66037362013-08-13 00:09:35 +00002218 EXPECT_TRUE(NULL == dtmf_sender); // local stream has been removed.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002219
2220 EXPECT_TRUE(pc_->CreateDataChannel("test", NULL) == NULL);
2221
2222 EXPECT_TRUE(pc_->local_description() != NULL);
2223 EXPECT_TRUE(pc_->remote_description() != NULL);
2224
kwibergd1fe2812016-04-27 06:47:29 -07002225 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -07002226 EXPECT_TRUE(DoCreateOffer(&offer, nullptr));
kwibergd1fe2812016-04-27 06:47:29 -07002227 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -07002228 EXPECT_TRUE(DoCreateAnswer(&answer, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002229
2230 std::string sdp;
2231 ASSERT_TRUE(pc_->remote_description()->ToString(&sdp));
2232 SessionDescriptionInterface* remote_offer =
2233 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
2234 sdp, NULL);
2235 EXPECT_FALSE(DoSetRemoteDescription(remote_offer));
2236
2237 ASSERT_TRUE(pc_->local_description()->ToString(&sdp));
2238 SessionDescriptionInterface* local_offer =
2239 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
2240 sdp, NULL);
2241 EXPECT_FALSE(DoSetLocalDescription(local_offer));
2242}
2243
2244// Test that GetStats can still be called after PeerConnection::Close.
2245TEST_F(PeerConnectionInterfaceTest, CloseAndGetStats) {
2246 InitiateCall();
2247 pc_->Close();
2248 DoGetStats(NULL);
2249}
deadbeefab9b2d12015-10-14 11:33:11 -07002250
2251// NOTE: The series of tests below come from what used to be
2252// mediastreamsignaling_unittest.cc, and are mostly aimed at testing that
2253// setting a remote or local description has the expected effects.
2254
2255// This test verifies that the remote MediaStreams corresponding to a received
2256// SDP string is created. In this test the two separate MediaStreams are
2257// signaled.
2258TEST_F(PeerConnectionInterfaceTest, UpdateRemoteStreams) {
2259 FakeConstraints constraints;
2260 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2261 true);
2262 CreatePeerConnection(&constraints);
2263 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2264
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002265 rtc::scoped_refptr<StreamCollection> reference(CreateStreamCollection(1, 1));
deadbeefab9b2d12015-10-14 11:33:11 -07002266 EXPECT_TRUE(
2267 CompareStreamCollections(observer_.remote_streams(), reference.get()));
2268 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2269 EXPECT_TRUE(remote_stream->GetVideoTracks()[0]->GetSource() != nullptr);
2270
2271 // Create a session description based on another SDP with another
2272 // MediaStream.
2273 CreateAndSetRemoteOffer(kSdpStringWithStream1And2);
2274
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002275 rtc::scoped_refptr<StreamCollection> reference2(CreateStreamCollection(2, 1));
deadbeefab9b2d12015-10-14 11:33:11 -07002276 EXPECT_TRUE(
2277 CompareStreamCollections(observer_.remote_streams(), reference2.get()));
2278}
2279
2280// This test verifies that when remote tracks are added/removed from SDP, the
2281// created remote streams are updated appropriately.
2282TEST_F(PeerConnectionInterfaceTest,
2283 AddRemoveTrackFromExistingRemoteMediaStream) {
2284 FakeConstraints constraints;
2285 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2286 true);
2287 CreatePeerConnection(&constraints);
kwibergd1fe2812016-04-27 06:47:29 -07002288 std::unique_ptr<SessionDescriptionInterface> desc_ms1 =
kwiberg2bbff992016-03-16 11:03:04 -07002289 CreateSessionDescriptionAndReference(1, 1);
deadbeefab9b2d12015-10-14 11:33:11 -07002290 EXPECT_TRUE(DoSetRemoteDescription(desc_ms1.release()));
2291 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
2292 reference_collection_));
2293
2294 // Add extra audio and video tracks to the same MediaStream.
kwibergd1fe2812016-04-27 06:47:29 -07002295 std::unique_ptr<SessionDescriptionInterface> desc_ms1_two_tracks =
kwiberg2bbff992016-03-16 11:03:04 -07002296 CreateSessionDescriptionAndReference(2, 2);
deadbeefab9b2d12015-10-14 11:33:11 -07002297 EXPECT_TRUE(DoSetRemoteDescription(desc_ms1_two_tracks.release()));
2298 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
2299 reference_collection_));
zhihuang9763d562016-08-05 11:14:50 -07002300 rtc::scoped_refptr<AudioTrackInterface> audio_track2 =
perkjd61bf802016-03-24 03:16:19 -07002301 observer_.remote_streams()->at(0)->GetAudioTracks()[1];
2302 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, audio_track2->state());
zhihuang9763d562016-08-05 11:14:50 -07002303 rtc::scoped_refptr<VideoTrackInterface> video_track2 =
perkjd61bf802016-03-24 03:16:19 -07002304 observer_.remote_streams()->at(0)->GetVideoTracks()[1];
2305 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, video_track2->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002306
2307 // Remove the extra audio and video tracks.
kwibergd1fe2812016-04-27 06:47:29 -07002308 std::unique_ptr<SessionDescriptionInterface> desc_ms2 =
kwiberg2bbff992016-03-16 11:03:04 -07002309 CreateSessionDescriptionAndReference(1, 1);
perkjd61bf802016-03-24 03:16:19 -07002310 MockTrackObserver audio_track_observer(audio_track2);
2311 MockTrackObserver video_track_observer(video_track2);
2312
2313 EXPECT_CALL(audio_track_observer, OnChanged()).Times(Exactly(1));
2314 EXPECT_CALL(video_track_observer, OnChanged()).Times(Exactly(1));
deadbeefab9b2d12015-10-14 11:33:11 -07002315 EXPECT_TRUE(DoSetRemoteDescription(desc_ms2.release()));
2316 EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
2317 reference_collection_));
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002318 // Track state may be updated asynchronously.
perkjd61bf802016-03-24 03:16:19 -07002319 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002320 audio_track2->state(), kTimeout);
2321 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
2322 video_track2->state(), kTimeout);
deadbeefab9b2d12015-10-14 11:33:11 -07002323}
2324
2325// This tests that remote tracks are ended if a local session description is set
2326// that rejects the media content type.
2327TEST_F(PeerConnectionInterfaceTest, RejectMediaContent) {
2328 FakeConstraints constraints;
2329 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2330 true);
2331 CreatePeerConnection(&constraints);
2332 // First create and set a remote offer, then reject its video content in our
2333 // answer.
2334 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2335 ASSERT_EQ(1u, observer_.remote_streams()->count());
2336 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2337 ASSERT_EQ(1u, remote_stream->GetVideoTracks().size());
2338 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2339
2340 rtc::scoped_refptr<webrtc::VideoTrackInterface> remote_video =
2341 remote_stream->GetVideoTracks()[0];
2342 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, remote_video->state());
2343 rtc::scoped_refptr<webrtc::AudioTrackInterface> remote_audio =
2344 remote_stream->GetAudioTracks()[0];
2345 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, remote_audio->state());
2346
kwibergd1fe2812016-04-27 06:47:29 -07002347 std::unique_ptr<SessionDescriptionInterface> local_answer;
kwiberg2bbff992016-03-16 11:03:04 -07002348 EXPECT_TRUE(DoCreateAnswer(&local_answer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07002349 cricket::ContentInfo* video_info =
2350 local_answer->description()->GetContentByName("video");
2351 video_info->rejected = true;
2352 EXPECT_TRUE(DoSetLocalDescription(local_answer.release()));
2353 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kEnded, remote_video->state());
2354 EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, remote_audio->state());
2355
2356 // Now create an offer where we reject both video and audio.
kwibergd1fe2812016-04-27 06:47:29 -07002357 std::unique_ptr<SessionDescriptionInterface> local_offer;
kwiberg2bbff992016-03-16 11:03:04 -07002358 EXPECT_TRUE(DoCreateOffer(&local_offer, nullptr));
deadbeefab9b2d12015-10-14 11:33:11 -07002359 video_info = local_offer->description()->GetContentByName("video");
2360 ASSERT_TRUE(video_info != nullptr);
2361 video_info->rejected = true;
2362 cricket::ContentInfo* audio_info =
2363 local_offer->description()->GetContentByName("audio");
2364 ASSERT_TRUE(audio_info != nullptr);
2365 audio_info->rejected = true;
2366 EXPECT_TRUE(DoSetLocalDescription(local_offer.release()));
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002367 // Track state may be updated asynchronously.
perkjd61bf802016-03-24 03:16:19 -07002368 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
Taylor Brandstetterd45b95c2016-03-29 13:16:52 -07002369 remote_audio->state(), kTimeout);
2370 EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
2371 remote_video->state(), kTimeout);
deadbeefab9b2d12015-10-14 11:33:11 -07002372}
2373
2374// This tests that we won't crash if the remote track has been removed outside
2375// of PeerConnection and then PeerConnection tries to reject the track.
2376TEST_F(PeerConnectionInterfaceTest, RemoveTrackThenRejectMediaContent) {
2377 FakeConstraints constraints;
2378 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2379 true);
2380 CreatePeerConnection(&constraints);
2381 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2382 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2383 remote_stream->RemoveTrack(remote_stream->GetVideoTracks()[0]);
2384 remote_stream->RemoveTrack(remote_stream->GetAudioTracks()[0]);
2385
kwibergd1fe2812016-04-27 06:47:29 -07002386 std::unique_ptr<SessionDescriptionInterface> local_answer(
deadbeefab9b2d12015-10-14 11:33:11 -07002387 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
2388 kSdpStringWithStream1, nullptr));
2389 cricket::ContentInfo* video_info =
2390 local_answer->description()->GetContentByName("video");
2391 video_info->rejected = true;
2392 cricket::ContentInfo* audio_info =
2393 local_answer->description()->GetContentByName("audio");
2394 audio_info->rejected = true;
2395 EXPECT_TRUE(DoSetLocalDescription(local_answer.release()));
2396
2397 // No crash is a pass.
2398}
2399
deadbeef5e97fb52015-10-15 12:49:08 -07002400// This tests that if a recvonly remote description is set, no remote streams
2401// will be created, even if the description contains SSRCs/MSIDs.
2402// See: https://code.google.com/p/webrtc/issues/detail?id=5054
2403TEST_F(PeerConnectionInterfaceTest, RecvonlyDescriptionDoesntCreateStream) {
2404 FakeConstraints constraints;
2405 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2406 true);
2407 CreatePeerConnection(&constraints);
2408
2409 std::string recvonly_offer = kSdpStringWithStream1;
2410 rtc::replace_substrs(kSendrecv, strlen(kSendrecv), kRecvonly,
2411 strlen(kRecvonly), &recvonly_offer);
2412 CreateAndSetRemoteOffer(recvonly_offer);
2413
2414 EXPECT_EQ(0u, observer_.remote_streams()->count());
2415}
2416
deadbeefab9b2d12015-10-14 11:33:11 -07002417// This tests that a default MediaStream is created if a remote session
2418// description doesn't contain any streams and no MSID support.
2419// It also tests that the default stream is updated if a video m-line is added
2420// in a subsequent session description.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002421TEST_F(PeerConnectionInterfaceTest, SdpWithoutMsidCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002422 FakeConstraints constraints;
2423 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2424 true);
2425 CreatePeerConnection(&constraints);
2426 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2427
2428 ASSERT_EQ(1u, observer_.remote_streams()->count());
2429 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2430
2431 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2432 EXPECT_EQ(0u, remote_stream->GetVideoTracks().size());
2433 EXPECT_EQ("default", remote_stream->label());
2434
2435 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2436 ASSERT_EQ(1u, observer_.remote_streams()->count());
2437 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2438 EXPECT_EQ("defaulta0", remote_stream->GetAudioTracks()[0]->id());
deadbeef884f5852016-01-15 09:20:04 -08002439 EXPECT_EQ(MediaStreamTrackInterface::kLive,
2440 remote_stream->GetAudioTracks()[0]->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002441 ASSERT_EQ(1u, remote_stream->GetVideoTracks().size());
2442 EXPECT_EQ("defaultv0", remote_stream->GetVideoTracks()[0]->id());
deadbeef884f5852016-01-15 09:20:04 -08002443 EXPECT_EQ(MediaStreamTrackInterface::kLive,
2444 remote_stream->GetVideoTracks()[0]->state());
deadbeefab9b2d12015-10-14 11:33:11 -07002445}
2446
2447// This tests that a default MediaStream is created if a remote session
2448// description doesn't contain any streams and media direction is send only.
2449TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002450 SendOnlySdpWithoutMsidCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002451 FakeConstraints constraints;
2452 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2453 true);
2454 CreatePeerConnection(&constraints);
2455 CreateAndSetRemoteOffer(kSdpStringSendOnlyWithoutStreams);
2456
2457 ASSERT_EQ(1u, observer_.remote_streams()->count());
2458 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2459
2460 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2461 EXPECT_EQ(1u, remote_stream->GetVideoTracks().size());
2462 EXPECT_EQ("default", remote_stream->label());
2463}
2464
2465// This tests that it won't crash when PeerConnection tries to remove
2466// a remote track that as already been removed from the MediaStream.
2467TEST_F(PeerConnectionInterfaceTest, RemoveAlreadyGoneRemoteStream) {
2468 FakeConstraints constraints;
2469 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2470 true);
2471 CreatePeerConnection(&constraints);
2472 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2473 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2474 remote_stream->RemoveTrack(remote_stream->GetAudioTracks()[0]);
2475 remote_stream->RemoveTrack(remote_stream->GetVideoTracks()[0]);
2476
2477 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2478
2479 // No crash is a pass.
2480}
2481
2482// This tests that a default MediaStream is created if the remote session
2483// description doesn't contain any streams and don't contain an indication if
2484// MSID is supported.
2485TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002486 SdpWithoutMsidAndStreamsCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002487 FakeConstraints constraints;
2488 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2489 true);
2490 CreatePeerConnection(&constraints);
2491 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2492
2493 ASSERT_EQ(1u, observer_.remote_streams()->count());
2494 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2495 EXPECT_EQ(1u, remote_stream->GetAudioTracks().size());
2496 EXPECT_EQ(1u, remote_stream->GetVideoTracks().size());
2497}
2498
2499// This tests that a default MediaStream is not created if the remote session
2500// description doesn't contain any streams but does support MSID.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002501TEST_F(PeerConnectionInterfaceTest, SdpWithMsidDontCreatesDefaultStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002502 FakeConstraints constraints;
2503 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2504 true);
2505 CreatePeerConnection(&constraints);
2506 CreateAndSetRemoteOffer(kSdpStringWithMsidWithoutStreams);
2507 EXPECT_EQ(0u, observer_.remote_streams()->count());
2508}
2509
deadbeefbda7e0b2015-12-08 17:13:40 -08002510// This tests that when setting a new description, the old default tracks are
2511// not destroyed and recreated.
2512// See: https://bugs.chromium.org/p/webrtc/issues/detail?id=5250
Stefan Holmer102362b2016-03-18 09:39:07 +01002513TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002514 DefaultTracksNotDestroyedAndRecreated) {
deadbeefbda7e0b2015-12-08 17:13:40 -08002515 FakeConstraints constraints;
2516 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2517 true);
2518 CreatePeerConnection(&constraints);
2519 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2520
2521 ASSERT_EQ(1u, observer_.remote_streams()->count());
2522 MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
2523 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2524
2525 // Set the track to "disabled", then set a new description and ensure the
2526 // track is still disabled, which ensures it hasn't been recreated.
2527 remote_stream->GetAudioTracks()[0]->set_enabled(false);
2528 CreateAndSetRemoteOffer(kSdpStringWithoutStreamsAudioOnly);
2529 ASSERT_EQ(1u, remote_stream->GetAudioTracks().size());
2530 EXPECT_FALSE(remote_stream->GetAudioTracks()[0]->enabled());
2531}
2532
deadbeefab9b2d12015-10-14 11:33:11 -07002533// This tests that a default MediaStream is not created if a remote session
2534// description is updated to not have any MediaStreams.
2535TEST_F(PeerConnectionInterfaceTest, VerifyDefaultStreamIsNotCreated) {
2536 FakeConstraints constraints;
2537 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2538 true);
2539 CreatePeerConnection(&constraints);
2540 CreateAndSetRemoteOffer(kSdpStringWithStream1);
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002541 rtc::scoped_refptr<StreamCollection> reference(CreateStreamCollection(1, 1));
deadbeefab9b2d12015-10-14 11:33:11 -07002542 EXPECT_TRUE(
2543 CompareStreamCollections(observer_.remote_streams(), reference.get()));
2544
2545 CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
2546 EXPECT_EQ(0u, observer_.remote_streams()->count());
2547}
2548
2549// This tests that an RtpSender is created when the local description is set
2550// after adding a local stream.
2551// TODO(deadbeef): This test and the one below it need to be updated when
2552// an RtpSender's lifetime isn't determined by when a local description is set.
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002553TEST_F(PeerConnectionInterfaceTest, LocalDescriptionChanged) {
deadbeefab9b2d12015-10-14 11:33:11 -07002554 FakeConstraints constraints;
2555 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2556 true);
2557 CreatePeerConnection(&constraints);
deadbeefab9b2d12015-10-14 11:33:11 -07002558
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002559 // Create an offer with 1 stream with 2 tracks of each type.
2560 rtc::scoped_refptr<StreamCollection> stream_collection =
2561 CreateStreamCollection(1, 2);
2562 pc_->AddStream(stream_collection->at(0));
2563 std::unique_ptr<SessionDescriptionInterface> offer;
2564 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2565 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
deadbeefab9b2d12015-10-14 11:33:11 -07002566
deadbeefab9b2d12015-10-14 11:33:11 -07002567 auto senders = pc_->GetSenders();
2568 EXPECT_EQ(4u, senders.size());
2569 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2570 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2571 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[1]));
2572 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[1]));
2573
2574 // Remove an audio and video track.
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002575 pc_->RemoveStream(stream_collection->at(0));
2576 stream_collection = CreateStreamCollection(1, 1);
2577 pc_->AddStream(stream_collection->at(0));
2578 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2579 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
2580
deadbeefab9b2d12015-10-14 11:33:11 -07002581 senders = pc_->GetSenders();
2582 EXPECT_EQ(2u, senders.size());
2583 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2584 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2585 EXPECT_FALSE(ContainsSender(senders, kAudioTracks[1]));
2586 EXPECT_FALSE(ContainsSender(senders, kVideoTracks[1]));
2587}
2588
2589// This tests that an RtpSender is created when the local description is set
2590// before adding a local stream.
2591TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002592 AddLocalStreamAfterLocalDescriptionChanged) {
deadbeefab9b2d12015-10-14 11:33:11 -07002593 FakeConstraints constraints;
2594 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2595 true);
2596 CreatePeerConnection(&constraints);
deadbeefab9b2d12015-10-14 11:33:11 -07002597
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002598 rtc::scoped_refptr<StreamCollection> stream_collection =
2599 CreateStreamCollection(1, 2);
2600 // Add a stream to create the offer, but remove it afterwards.
2601 pc_->AddStream(stream_collection->at(0));
2602 std::unique_ptr<SessionDescriptionInterface> offer;
2603 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2604 pc_->RemoveStream(stream_collection->at(0));
deadbeefab9b2d12015-10-14 11:33:11 -07002605
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002606 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
deadbeefab9b2d12015-10-14 11:33:11 -07002607 auto senders = pc_->GetSenders();
2608 EXPECT_EQ(0u, senders.size());
2609
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002610 pc_->AddStream(stream_collection->at(0));
deadbeefab9b2d12015-10-14 11:33:11 -07002611 senders = pc_->GetSenders();
2612 EXPECT_EQ(4u, senders.size());
2613 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2614 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2615 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[1]));
2616 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[1]));
2617}
2618
2619// This tests that the expected behavior occurs if the SSRC on a local track is
2620// changed when SetLocalDescription is called.
2621TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002622 ChangeSsrcOnTrackInLocalSessionDescription) {
deadbeefab9b2d12015-10-14 11:33:11 -07002623 FakeConstraints constraints;
2624 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2625 true);
2626 CreatePeerConnection(&constraints);
deadbeefab9b2d12015-10-14 11:33:11 -07002627
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002628 rtc::scoped_refptr<StreamCollection> stream_collection =
2629 CreateStreamCollection(2, 1);
2630 pc_->AddStream(stream_collection->at(0));
2631 std::unique_ptr<SessionDescriptionInterface> offer;
2632 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2633 // Grab a copy of the offer before it gets passed into the PC.
2634 std::unique_ptr<JsepSessionDescription> modified_offer(
2635 new JsepSessionDescription(JsepSessionDescription::kOffer));
2636 modified_offer->Initialize(offer->description()->Copy(), offer->session_id(),
2637 offer->session_version());
2638 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
deadbeefab9b2d12015-10-14 11:33:11 -07002639
deadbeefab9b2d12015-10-14 11:33:11 -07002640 auto senders = pc_->GetSenders();
2641 EXPECT_EQ(2u, senders.size());
2642 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2643 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2644
2645 // Change the ssrc of the audio and video track.
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002646 cricket::MediaContentDescription* desc =
2647 cricket::GetFirstAudioContentDescription(modified_offer->description());
2648 ASSERT_TRUE(desc != NULL);
2649 for (StreamParams& stream : desc->mutable_streams()) {
2650 for (unsigned int& ssrc : stream.ssrcs) {
2651 ++ssrc;
2652 }
2653 }
deadbeefab9b2d12015-10-14 11:33:11 -07002654
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002655 desc =
2656 cricket::GetFirstVideoContentDescription(modified_offer->description());
2657 ASSERT_TRUE(desc != NULL);
2658 for (StreamParams& stream : desc->mutable_streams()) {
2659 for (unsigned int& ssrc : stream.ssrcs) {
2660 ++ssrc;
2661 }
2662 }
2663
2664 EXPECT_TRUE(DoSetLocalDescription(modified_offer.release()));
deadbeefab9b2d12015-10-14 11:33:11 -07002665 senders = pc_->GetSenders();
2666 EXPECT_EQ(2u, senders.size());
2667 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0]));
2668 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0]));
2669 // TODO(deadbeef): Once RtpSenders expose parameters, check that the SSRC
2670 // changed.
2671}
2672
2673// This tests that the expected behavior occurs if a new session description is
2674// set with the same tracks, but on a different MediaStream.
Stefan Holmer55d6e7c2016-03-17 16:26:40 +01002675TEST_F(PeerConnectionInterfaceTest,
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002676 SignalSameTracksInSeparateMediaStream) {
deadbeefab9b2d12015-10-14 11:33:11 -07002677 FakeConstraints constraints;
2678 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2679 true);
2680 CreatePeerConnection(&constraints);
deadbeefab9b2d12015-10-14 11:33:11 -07002681
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002682 rtc::scoped_refptr<StreamCollection> stream_collection =
2683 CreateStreamCollection(2, 1);
2684 pc_->AddStream(stream_collection->at(0));
2685 std::unique_ptr<SessionDescriptionInterface> offer;
2686 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2687 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
deadbeefab9b2d12015-10-14 11:33:11 -07002688
deadbeefab9b2d12015-10-14 11:33:11 -07002689 auto senders = pc_->GetSenders();
2690 EXPECT_EQ(2u, senders.size());
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002691 EXPECT_TRUE(ContainsSender(senders, kAudioTracks[0], kStreams[0]));
2692 EXPECT_TRUE(ContainsSender(senders, kVideoTracks[0], kStreams[0]));
deadbeefab9b2d12015-10-14 11:33:11 -07002693
2694 // Add a new MediaStream but with the same tracks as in the first stream.
2695 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream_1(
2696 webrtc::MediaStream::Create(kStreams[1]));
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002697 stream_1->AddTrack(stream_collection->at(0)->GetVideoTracks()[0]);
2698 stream_1->AddTrack(stream_collection->at(0)->GetAudioTracks()[0]);
deadbeefab9b2d12015-10-14 11:33:11 -07002699 pc_->AddStream(stream_1);
2700
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002701 ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
2702 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
deadbeefab9b2d12015-10-14 11:33:11 -07002703
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002704 auto new_senders = pc_->GetSenders();
2705 // Should be the same senders as before, but with updated stream id.
2706 // Note that this behavior is subject to change in the future.
2707 // We may decide the PC should ignore existing tracks in AddStream.
2708 EXPECT_EQ(senders, new_senders);
2709 EXPECT_TRUE(ContainsSender(new_senders, kAudioTracks[0], kStreams[1]));
2710 EXPECT_TRUE(ContainsSender(new_senders, kVideoTracks[0], kStreams[1]));
deadbeefab9b2d12015-10-14 11:33:11 -07002711}
2712
zhihuang81c3a032016-11-17 12:06:24 -08002713// This tests that PeerConnectionObserver::OnAddTrack is correctly called.
2714TEST_F(PeerConnectionInterfaceTest, OnAddTrackCallback) {
2715 FakeConstraints constraints;
2716 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
2717 true);
2718 CreatePeerConnection(&constraints);
2719 CreateAndSetRemoteOffer(kSdpStringWithStream1AudioTrackOnly);
2720 EXPECT_EQ(observer_.num_added_tracks_, 1);
2721 EXPECT_EQ(observer_.last_added_track_label_, kAudioTracks[0]);
2722
2723 // Create and set the updated remote SDP.
2724 CreateAndSetRemoteOffer(kSdpStringWithStream1);
2725 EXPECT_EQ(observer_.num_added_tracks_, 2);
2726 EXPECT_EQ(observer_.last_added_track_label_, kVideoTracks[0]);
2727}
2728
nisse51542be2016-02-12 02:27:06 -08002729class PeerConnectionMediaConfigTest : public testing::Test {
2730 protected:
2731 void SetUp() override {
nisseaf510af2016-03-21 08:20:42 -07002732 pcf_ = new rtc::RefCountedObject<PeerConnectionFactoryForTest>();
nisse51542be2016-02-12 02:27:06 -08002733 pcf_->Initialize();
2734 }
2735 const cricket::MediaConfig& TestCreatePeerConnection(
2736 const PeerConnectionInterface::RTCConfiguration& config,
2737 const MediaConstraintsInterface *constraints) {
2738 pcf_->create_media_controller_called_ = false;
2739
zhihuang9763d562016-08-05 11:14:50 -07002740 rtc::scoped_refptr<PeerConnectionInterface> pc(pcf_->CreatePeerConnection(
2741 config, constraints, nullptr, nullptr, &observer_));
nisse51542be2016-02-12 02:27:06 -08002742 EXPECT_TRUE(pc.get());
2743 EXPECT_TRUE(pcf_->create_media_controller_called_);
2744 return pcf_->create_media_controller_config_;
2745 }
2746
zhihuang9763d562016-08-05 11:14:50 -07002747 rtc::scoped_refptr<PeerConnectionFactoryForTest> pcf_;
nisse51542be2016-02-12 02:27:06 -08002748 MockPeerConnectionObserver observer_;
2749};
2750
2751// This test verifies the default behaviour with no constraints and a
2752// default RTCConfiguration.
2753TEST_F(PeerConnectionMediaConfigTest, TestDefaults) {
2754 PeerConnectionInterface::RTCConfiguration config;
2755 FakeConstraints constraints;
2756
2757 const cricket::MediaConfig& media_config =
2758 TestCreatePeerConnection(config, &constraints);
2759
2760 EXPECT_FALSE(media_config.enable_dscp);
nisse0db023a2016-03-01 04:29:59 -08002761 EXPECT_TRUE(media_config.video.enable_cpu_overuse_detection);
2762 EXPECT_FALSE(media_config.video.disable_prerenderer_smoothing);
2763 EXPECT_FALSE(media_config.video.suspend_below_min_bitrate);
nisse51542be2016-02-12 02:27:06 -08002764}
2765
2766// This test verifies the DSCP constraint is recognized and passed to
2767// the CreateMediaController call.
2768TEST_F(PeerConnectionMediaConfigTest, TestDscpConstraintTrue) {
2769 PeerConnectionInterface::RTCConfiguration config;
2770 FakeConstraints constraints;
2771
2772 constraints.AddOptional(webrtc::MediaConstraintsInterface::kEnableDscp, true);
2773 const cricket::MediaConfig& media_config =
2774 TestCreatePeerConnection(config, &constraints);
2775
2776 EXPECT_TRUE(media_config.enable_dscp);
2777}
2778
2779// This test verifies the cpu overuse detection constraint is
2780// recognized and passed to the CreateMediaController call.
2781TEST_F(PeerConnectionMediaConfigTest, TestCpuOveruseConstraintFalse) {
2782 PeerConnectionInterface::RTCConfiguration config;
2783 FakeConstraints constraints;
2784
2785 constraints.AddOptional(
2786 webrtc::MediaConstraintsInterface::kCpuOveruseDetection, false);
2787 const cricket::MediaConfig media_config =
2788 TestCreatePeerConnection(config, &constraints);
2789
nisse0db023a2016-03-01 04:29:59 -08002790 EXPECT_FALSE(media_config.video.enable_cpu_overuse_detection);
nisse51542be2016-02-12 02:27:06 -08002791}
2792
2793// This test verifies that the disable_prerenderer_smoothing flag is
2794// propagated from RTCConfiguration to the CreateMediaController call.
2795TEST_F(PeerConnectionMediaConfigTest, TestDisablePrerendererSmoothingTrue) {
2796 PeerConnectionInterface::RTCConfiguration config;
2797 FakeConstraints constraints;
2798
Niels Möller71bdda02016-03-31 12:59:59 +02002799 config.set_prerenderer_smoothing(false);
nisse51542be2016-02-12 02:27:06 -08002800 const cricket::MediaConfig& media_config =
2801 TestCreatePeerConnection(config, &constraints);
2802
nisse0db023a2016-03-01 04:29:59 -08002803 EXPECT_TRUE(media_config.video.disable_prerenderer_smoothing);
2804}
2805
2806// This test verifies the suspend below min bitrate constraint is
2807// recognized and passed to the CreateMediaController call.
2808TEST_F(PeerConnectionMediaConfigTest,
2809 TestSuspendBelowMinBitrateConstraintTrue) {
2810 PeerConnectionInterface::RTCConfiguration config;
2811 FakeConstraints constraints;
2812
2813 constraints.AddOptional(
2814 webrtc::MediaConstraintsInterface::kEnableVideoSuspendBelowMinBitrate,
2815 true);
2816 const cricket::MediaConfig media_config =
2817 TestCreatePeerConnection(config, &constraints);
2818
2819 EXPECT_TRUE(media_config.video.suspend_below_min_bitrate);
nisse51542be2016-02-12 02:27:06 -08002820}
2821
deadbeefab9b2d12015-10-14 11:33:11 -07002822// The following tests verify that session options are created correctly.
deadbeefc80741f2015-10-22 13:14:45 -07002823// TODO(deadbeef): Convert these tests to be more end-to-end. Instead of
2824// "verify options are converted correctly", should be "pass options into
2825// CreateOffer and verify the correct offer is produced."
deadbeefab9b2d12015-10-14 11:33:11 -07002826
2827TEST(CreateSessionOptionsTest, GetOptionsForOfferWithInvalidAudioOption) {
2828 RTCOfferAnswerOptions rtc_options;
2829 rtc_options.offer_to_receive_audio = RTCOfferAnswerOptions::kUndefined - 1;
2830
2831 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08002832 EXPECT_FALSE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002833
2834 rtc_options.offer_to_receive_audio =
2835 RTCOfferAnswerOptions::kMaxOfferToReceiveMedia + 1;
htaaac2dea2016-03-10 13:35:55 -08002836 EXPECT_FALSE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002837}
2838
2839TEST(CreateSessionOptionsTest, GetOptionsForOfferWithInvalidVideoOption) {
2840 RTCOfferAnswerOptions rtc_options;
2841 rtc_options.offer_to_receive_video = RTCOfferAnswerOptions::kUndefined - 1;
2842
2843 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08002844 EXPECT_FALSE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002845
2846 rtc_options.offer_to_receive_video =
2847 RTCOfferAnswerOptions::kMaxOfferToReceiveMedia + 1;
htaaac2dea2016-03-10 13:35:55 -08002848 EXPECT_FALSE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002849}
2850
2851// Test that a MediaSessionOptions is created for an offer if
deadbeefc80741f2015-10-22 13:14:45 -07002852// OfferToReceiveAudio and OfferToReceiveVideo options are set.
deadbeefab9b2d12015-10-14 11:33:11 -07002853TEST(CreateSessionOptionsTest, GetMediaSessionOptionsForOfferWithAudioVideo) {
2854 RTCOfferAnswerOptions rtc_options;
2855 rtc_options.offer_to_receive_audio = 1;
2856 rtc_options.offer_to_receive_video = 1;
2857
2858 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08002859 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002860 EXPECT_TRUE(options.has_audio());
2861 EXPECT_TRUE(options.has_video());
2862 EXPECT_TRUE(options.bundle_enabled);
2863}
2864
2865// Test that a correct MediaSessionOptions is created for an offer if
deadbeefc80741f2015-10-22 13:14:45 -07002866// OfferToReceiveAudio is set.
deadbeefab9b2d12015-10-14 11:33:11 -07002867TEST(CreateSessionOptionsTest, GetMediaSessionOptionsForOfferWithAudio) {
2868 RTCOfferAnswerOptions rtc_options;
2869 rtc_options.offer_to_receive_audio = 1;
2870
2871 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08002872 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002873 EXPECT_TRUE(options.has_audio());
2874 EXPECT_FALSE(options.has_video());
2875 EXPECT_TRUE(options.bundle_enabled);
2876}
2877
2878// Test that a correct MediaSessionOptions is created for an offer if
deadbeefc80741f2015-10-22 13:14:45 -07002879// the default OfferOptions are used.
deadbeefab9b2d12015-10-14 11:33:11 -07002880TEST(CreateSessionOptionsTest, GetDefaultMediaSessionOptionsForOffer) {
2881 RTCOfferAnswerOptions rtc_options;
2882
2883 cricket::MediaSessionOptions options;
deadbeef0ed85b22016-02-23 17:24:52 -08002884 options.transport_options["audio"] = cricket::TransportOptions();
2885 options.transport_options["video"] = cricket::TransportOptions();
htaaac2dea2016-03-10 13:35:55 -08002886 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefc80741f2015-10-22 13:14:45 -07002887 EXPECT_TRUE(options.has_audio());
deadbeefab9b2d12015-10-14 11:33:11 -07002888 EXPECT_FALSE(options.has_video());
deadbeefc80741f2015-10-22 13:14:45 -07002889 EXPECT_TRUE(options.bundle_enabled);
deadbeefab9b2d12015-10-14 11:33:11 -07002890 EXPECT_TRUE(options.vad_enabled);
deadbeef0ed85b22016-02-23 17:24:52 -08002891 EXPECT_FALSE(options.transport_options["audio"].ice_restart);
2892 EXPECT_FALSE(options.transport_options["video"].ice_restart);
deadbeefab9b2d12015-10-14 11:33:11 -07002893}
2894
2895// Test that a correct MediaSessionOptions is created for an offer if
deadbeefc80741f2015-10-22 13:14:45 -07002896// OfferToReceiveVideo is set.
deadbeefab9b2d12015-10-14 11:33:11 -07002897TEST(CreateSessionOptionsTest, GetMediaSessionOptionsForOfferWithVideo) {
2898 RTCOfferAnswerOptions rtc_options;
2899 rtc_options.offer_to_receive_audio = 0;
2900 rtc_options.offer_to_receive_video = 1;
2901
2902 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08002903 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002904 EXPECT_FALSE(options.has_audio());
2905 EXPECT_TRUE(options.has_video());
2906 EXPECT_TRUE(options.bundle_enabled);
2907}
2908
2909// Test that a correct MediaSessionOptions is created for an offer if
2910// UseRtpMux is set to false.
2911TEST(CreateSessionOptionsTest,
2912 GetMediaSessionOptionsForOfferWithBundleDisabled) {
2913 RTCOfferAnswerOptions rtc_options;
2914 rtc_options.offer_to_receive_audio = 1;
2915 rtc_options.offer_to_receive_video = 1;
2916 rtc_options.use_rtp_mux = false;
2917
2918 cricket::MediaSessionOptions options;
htaaac2dea2016-03-10 13:35:55 -08002919 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeefab9b2d12015-10-14 11:33:11 -07002920 EXPECT_TRUE(options.has_audio());
2921 EXPECT_TRUE(options.has_video());
2922 EXPECT_FALSE(options.bundle_enabled);
2923}
2924
2925// Test that a correct MediaSessionOptions is created to restart ice if
2926// IceRestart is set. It also tests that subsequent MediaSessionOptions don't
Taylor Brandstetterf475d362016-01-08 15:35:57 -08002927// have |audio_transport_options.ice_restart| etc. set.
deadbeefab9b2d12015-10-14 11:33:11 -07002928TEST(CreateSessionOptionsTest, GetMediaSessionOptionsForOfferWithIceRestart) {
2929 RTCOfferAnswerOptions rtc_options;
2930 rtc_options.ice_restart = true;
2931
2932 cricket::MediaSessionOptions options;
deadbeef0ed85b22016-02-23 17:24:52 -08002933 options.transport_options["audio"] = cricket::TransportOptions();
2934 options.transport_options["video"] = cricket::TransportOptions();
htaaac2dea2016-03-10 13:35:55 -08002935 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeef0ed85b22016-02-23 17:24:52 -08002936 EXPECT_TRUE(options.transport_options["audio"].ice_restart);
2937 EXPECT_TRUE(options.transport_options["video"].ice_restart);
deadbeefab9b2d12015-10-14 11:33:11 -07002938
2939 rtc_options = RTCOfferAnswerOptions();
htaaac2dea2016-03-10 13:35:55 -08002940 EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
deadbeef0ed85b22016-02-23 17:24:52 -08002941 EXPECT_FALSE(options.transport_options["audio"].ice_restart);
2942 EXPECT_FALSE(options.transport_options["video"].ice_restart);
deadbeefab9b2d12015-10-14 11:33:11 -07002943}
2944
2945// Test that the MediaConstraints in an answer don't affect if audio and video
2946// is offered in an offer but that if kOfferToReceiveAudio or
2947// kOfferToReceiveVideo constraints are true in an offer, the media type will be
2948// included in subsequent answers.
2949TEST(CreateSessionOptionsTest, MediaConstraintsInAnswer) {
2950 FakeConstraints answer_c;
2951 answer_c.SetMandatoryReceiveAudio(true);
2952 answer_c.SetMandatoryReceiveVideo(true);
2953
2954 cricket::MediaSessionOptions answer_options;
2955 EXPECT_TRUE(ParseConstraintsForAnswer(&answer_c, &answer_options));
2956 EXPECT_TRUE(answer_options.has_audio());
2957 EXPECT_TRUE(answer_options.has_video());
2958
deadbeefc80741f2015-10-22 13:14:45 -07002959 RTCOfferAnswerOptions rtc_offer_options;
deadbeefab9b2d12015-10-14 11:33:11 -07002960
2961 cricket::MediaSessionOptions offer_options;
htaaac2dea2016-03-10 13:35:55 -08002962 EXPECT_TRUE(
2963 ExtractMediaSessionOptions(rtc_offer_options, false, &offer_options));
deadbeefc80741f2015-10-22 13:14:45 -07002964 EXPECT_TRUE(offer_options.has_audio());
htaaac2dea2016-03-10 13:35:55 -08002965 EXPECT_TRUE(offer_options.has_video());
deadbeefab9b2d12015-10-14 11:33:11 -07002966
deadbeefc80741f2015-10-22 13:14:45 -07002967 RTCOfferAnswerOptions updated_rtc_offer_options;
2968 updated_rtc_offer_options.offer_to_receive_audio = 1;
2969 updated_rtc_offer_options.offer_to_receive_video = 1;
deadbeefab9b2d12015-10-14 11:33:11 -07002970
2971 cricket::MediaSessionOptions updated_offer_options;
htaaac2dea2016-03-10 13:35:55 -08002972 EXPECT_TRUE(ExtractMediaSessionOptions(updated_rtc_offer_options, false,
htaa2a49d92016-03-04 02:51:39 -08002973 &updated_offer_options));
deadbeefab9b2d12015-10-14 11:33:11 -07002974 EXPECT_TRUE(updated_offer_options.has_audio());
2975 EXPECT_TRUE(updated_offer_options.has_video());
2976
2977 // Since an offer has been created with both audio and video, subsequent
2978 // offers and answers should contain both audio and video.
2979 // Answers will only contain the media types that exist in the offer
2980 // regardless of the value of |updated_answer_options.has_audio| and
2981 // |updated_answer_options.has_video|.
2982 FakeConstraints updated_answer_c;
2983 answer_c.SetMandatoryReceiveAudio(false);
2984 answer_c.SetMandatoryReceiveVideo(false);
2985
2986 cricket::MediaSessionOptions updated_answer_options;
2987 EXPECT_TRUE(
2988 ParseConstraintsForAnswer(&updated_answer_c, &updated_answer_options));
2989 EXPECT_TRUE(updated_answer_options.has_audio());
2990 EXPECT_TRUE(updated_answer_options.has_video());
deadbeefab9b2d12015-10-14 11:33:11 -07002991}