blob: 6a761e6ab95a75d80fec9c2dd741860a2f99d967 [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
2 * libjingle
jlmiller@webrtc.org5f93d0a2015-01-20 21:36:13 +00003 * Copyright 2012 Google Inc.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00004 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28#include "talk/app/webrtc/webrtcsession.h"
29
pbos@webrtc.org371243d2014-03-07 15:22:04 +000030#include <limits.h>
31
henrike@webrtc.org28e20752013-07-10 00:45:36 +000032#include <algorithm>
henrike@webrtc.org28e20752013-07-10 00:45:36 +000033#include <vector>
34
35#include "talk/app/webrtc/jsepicecandidate.h"
36#include "talk/app/webrtc/jsepsessiondescription.h"
37#include "talk/app/webrtc/mediaconstraintsinterface.h"
38#include "talk/app/webrtc/mediastreamsignaling.h"
39#include "talk/app/webrtc/peerconnectioninterface.h"
wu@webrtc.org91053e72013-08-10 07:18:04 +000040#include "talk/app/webrtc/webrtcsessiondescriptionfactory.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000041#include "talk/media/base/constants.h"
42#include "talk/media/base/videocapturer.h"
43#include "talk/session/media/channel.h"
44#include "talk/session/media/channelmanager.h"
45#include "talk/session/media/mediasession.h"
buildbot@webrtc.orga09a9992014-08-13 17:26:08 +000046#include "webrtc/base/basictypes.h"
jbauchac8869e2015-07-03 01:36:14 -070047#include "webrtc/base/checks.h"
buildbot@webrtc.orga09a9992014-08-13 17:26:08 +000048#include "webrtc/base/helpers.h"
49#include "webrtc/base/logging.h"
50#include "webrtc/base/stringencode.h"
51#include "webrtc/base/stringutils.h"
pthatcher@webrtc.org40b276e2014-12-12 02:44:30 +000052#include "webrtc/p2p/base/portallocator.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000053
54using cricket::ContentInfo;
55using cricket::ContentInfos;
56using cricket::MediaContentDescription;
57using cricket::SessionDescription;
58using cricket::TransportInfo;
59
Guo-wei Shieh3d564c12015-08-19 16:51:15 -070060using cricket::LOCAL_PORT_TYPE;
61using cricket::STUN_PORT_TYPE;
62using cricket::RELAY_PORT_TYPE;
63using cricket::PRFLX_PORT_TYPE;
64
henrike@webrtc.org28e20752013-07-10 00:45:36 +000065namespace webrtc {
66
henrike@webrtc.org28e20752013-07-10 00:45:36 +000067// Error messages
henrike@webrtc.org1e09a712013-07-26 19:17:59 +000068const char kBundleWithoutRtcpMux[] = "RTCP-MUX must be enabled when BUNDLE "
69 "is enabled.";
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +000070const char kCreateChannelFailed[] = "Failed to create channels.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +000071const char kInvalidCandidates[] = "Description contains invalid candidates.";
72const char kInvalidSdp[] = "Invalid session description.";
73const char kMlineMismatch[] =
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +000074 "Offer and answer descriptions m-lines are not matching. Rejecting answer.";
75const char kPushDownTDFailed[] =
76 "Failed to push down transport description:";
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +000077const char kSdpWithoutDtlsFingerprint[] =
78 "Called with SDP without DTLS fingerprint.";
79const char kSdpWithoutSdesCrypto[] =
80 "Called with SDP without SDES crypto.";
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +000081const char kSdpWithoutIceUfragPwd[] =
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +000082 "Called with SDP without ice-ufrag and ice-pwd.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +000083const char kSessionError[] = "Session error code: ";
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +000084const char kSessionErrorDesc[] = "Session error description: ";
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +000085const char kDtlsSetupFailureRtp[] =
86 "Couldn't set up DTLS-SRTP on RTP channel.";
87const char kDtlsSetupFailureRtcp[] =
88 "Couldn't set up DTLS-SRTP on RTCP channel.";
buildbot@webrtc.org53df88c2014-08-07 22:46:01 +000089const int kMaxUnsignalledRecvStreams = 20;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000090
Guo-wei Shieh3d564c12015-08-19 16:51:15 -070091IceCandidatePairType GetIceCandidatePairCounter(
92 const cricket::Candidate& local,
93 const cricket::Candidate& remote) {
94 const auto& l = local.type();
95 const auto& r = remote.type();
96 const auto& host = LOCAL_PORT_TYPE;
97 const auto& srflx = STUN_PORT_TYPE;
98 const auto& relay = RELAY_PORT_TYPE;
99 const auto& prflx = PRFLX_PORT_TYPE;
Guo-wei Shieh3cc834a2015-09-04 15:52:14 -0700100 if (l == host && r == host) {
101 bool local_private = IPIsPrivate(local.address().ipaddr());
102 bool remote_private = IPIsPrivate(remote.address().ipaddr());
103 if (local_private) {
104 if (remote_private) {
105 return kIceCandidatePairHostPrivateHostPrivate;
106 } else {
107 return kIceCandidatePairHostPrivateHostPublic;
108 }
109 } else {
110 if (remote_private) {
111 return kIceCandidatePairHostPublicHostPrivate;
112 } else {
113 return kIceCandidatePairHostPublicHostPublic;
114 }
115 }
116 }
Guo-wei Shieh3d564c12015-08-19 16:51:15 -0700117 if (l == host && r == srflx)
118 return kIceCandidatePairHostSrflx;
119 if (l == host && r == relay)
120 return kIceCandidatePairHostRelay;
121 if (l == host && r == prflx)
122 return kIceCandidatePairHostPrflx;
123 if (l == srflx && r == host)
124 return kIceCandidatePairSrflxHost;
125 if (l == srflx && r == srflx)
126 return kIceCandidatePairSrflxSrflx;
127 if (l == srflx && r == relay)
128 return kIceCandidatePairSrflxRelay;
129 if (l == srflx && r == prflx)
130 return kIceCandidatePairSrflxPrflx;
131 if (l == relay && r == host)
132 return kIceCandidatePairRelayHost;
133 if (l == relay && r == srflx)
134 return kIceCandidatePairRelaySrflx;
135 if (l == relay && r == relay)
136 return kIceCandidatePairRelayRelay;
137 if (l == relay && r == prflx)
138 return kIceCandidatePairRelayPrflx;
139 if (l == prflx && r == host)
140 return kIceCandidatePairPrflxHost;
141 if (l == prflx && r == srflx)
142 return kIceCandidatePairPrflxSrflx;
143 if (l == prflx && r == relay)
144 return kIceCandidatePairPrflxRelay;
145 return kIceCandidatePairMax;
146}
147
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000148// Compares |answer| against |offer|. Comparision is done
149// for number of m-lines in answer against offer. If matches true will be
150// returned otherwise false.
151static bool VerifyMediaDescriptions(
152 const SessionDescription* answer, const SessionDescription* offer) {
153 if (offer->contents().size() != answer->contents().size())
154 return false;
155
156 for (size_t i = 0; i < offer->contents().size(); ++i) {
157 if ((offer->contents()[i].name) != answer->contents()[i].name) {
158 return false;
159 }
wu@webrtc.org4e393072014-04-07 17:04:35 +0000160 const MediaContentDescription* offer_mdesc =
161 static_cast<const MediaContentDescription*>(
162 offer->contents()[i].description);
163 const MediaContentDescription* answer_mdesc =
164 static_cast<const MediaContentDescription*>(
165 answer->contents()[i].description);
166 if (offer_mdesc->type() != answer_mdesc->type()) {
167 return false;
168 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000169 }
170 return true;
171}
172
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000173// Checks that each non-rejected content has SDES crypto keys or a DTLS
174// fingerprint. Mismatches, such as replying with a DTLS fingerprint to SDES
175// keys, will be caught in Transport negotiation, and backstopped by Channel's
176// |secure_required| check.
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000177static bool VerifyCrypto(const SessionDescription* desc,
178 bool dtls_enabled,
179 std::string* error) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000180 const ContentInfos& contents = desc->contents();
181 for (size_t index = 0; index < contents.size(); ++index) {
182 const ContentInfo* cinfo = &contents[index];
183 if (cinfo->rejected) {
184 continue;
185 }
186
187 // If the content isn't rejected, crypto must be present.
188 const MediaContentDescription* media =
189 static_cast<const MediaContentDescription*>(cinfo->description);
190 const TransportInfo* tinfo = desc->GetTransportInfoByName(cinfo->name);
191 if (!media || !tinfo) {
192 // Something is not right.
193 LOG(LS_ERROR) << kInvalidSdp;
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000194 *error = kInvalidSdp;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000195 return false;
196 }
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000197 if (dtls_enabled) {
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000198 if (!tinfo->description.identity_fingerprint) {
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000199 LOG(LS_WARNING) <<
200 "Session description must have DTLS fingerprint if DTLS enabled.";
201 *error = kSdpWithoutDtlsFingerprint;
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000202 return false;
203 }
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000204 } else {
205 if (media->cryptos().empty()) {
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000206 LOG(LS_WARNING) <<
207 "Session description must have SDES when DTLS disabled.";
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000208 *error = kSdpWithoutSdesCrypto;
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000209 return false;
210 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000211 }
212 }
213
214 return true;
215}
216
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000217// Checks that each non-rejected content has ice-ufrag and ice-pwd set.
218static bool VerifyIceUfragPwdPresent(const SessionDescription* desc) {
219 const ContentInfos& contents = desc->contents();
220 for (size_t index = 0; index < contents.size(); ++index) {
221 const ContentInfo* cinfo = &contents[index];
222 if (cinfo->rejected) {
223 continue;
224 }
225
226 // If the content isn't rejected, ice-ufrag and ice-pwd must be present.
227 const TransportInfo* tinfo = desc->GetTransportInfoByName(cinfo->name);
228 if (!tinfo) {
229 // Something is not right.
230 LOG(LS_ERROR) << kInvalidSdp;
231 return false;
232 }
233 if (tinfo->description.ice_ufrag.empty() ||
234 tinfo->description.ice_pwd.empty()) {
235 LOG(LS_ERROR) << "Session description must have ice ufrag and pwd.";
236 return false;
237 }
238 }
239 return true;
240}
241
wu@webrtc.org91053e72013-08-10 07:18:04 +0000242// Forces |sdesc->crypto_required| to the appropriate state based on the
243// current security policy, to ensure a failure occurs if there is an error
244// in crypto negotiation.
245// Called when processing the local session description.
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000246static void UpdateSessionDescriptionSecurePolicy(cricket::CryptoType type,
247 SessionDescription* sdesc) {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000248 if (!sdesc) {
249 return;
250 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000251
wu@webrtc.org91053e72013-08-10 07:18:04 +0000252 // Updating the |crypto_required_| in MediaContentDescription to the
253 // appropriate state based on the current security policy.
254 for (cricket::ContentInfos::iterator iter = sdesc->contents().begin();
255 iter != sdesc->contents().end(); ++iter) {
256 if (cricket::IsMediaContent(&*iter)) {
257 MediaContentDescription* mdesc =
258 static_cast<MediaContentDescription*> (iter->description);
259 if (mdesc) {
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000260 mdesc->set_crypto_required(type);
wu@webrtc.org91053e72013-08-10 07:18:04 +0000261 }
262 }
263 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000264}
265
266static bool GetAudioSsrcByTrackId(
267 const SessionDescription* session_description,
268 const std::string& track_id, uint32 *ssrc) {
269 const cricket::ContentInfo* audio_info =
270 cricket::GetFirstAudioContent(session_description);
271 if (!audio_info) {
272 LOG(LS_ERROR) << "Audio not used in this call";
273 return false;
274 }
275
276 const cricket::MediaContentDescription* audio_content =
277 static_cast<const cricket::MediaContentDescription*>(
278 audio_info->description);
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +0000279 const cricket::StreamParams* stream =
280 cricket::GetStreamByIds(audio_content->streams(), "", track_id);
281 if (!stream) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000282 return false;
283 }
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +0000284
285 *ssrc = stream->first_ssrc();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000286 return true;
287}
288
289static bool GetTrackIdBySsrc(const SessionDescription* session_description,
290 uint32 ssrc, std::string* track_id) {
291 ASSERT(track_id != NULL);
292
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000293 const cricket::ContentInfo* audio_info =
294 cricket::GetFirstAudioContent(session_description);
jiayl@webrtc.orge21cc9a2014-08-28 22:21:34 +0000295 if (audio_info) {
296 const cricket::MediaContentDescription* audio_content =
297 static_cast<const cricket::MediaContentDescription*>(
298 audio_info->description);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000299
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +0000300 const auto* found =
301 cricket::GetStreamBySsrc(audio_content->streams(), ssrc);
302 if (found) {
303 *track_id = found->id;
jiayl@webrtc.orge21cc9a2014-08-28 22:21:34 +0000304 return true;
305 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000306 }
307
308 const cricket::ContentInfo* video_info =
309 cricket::GetFirstVideoContent(session_description);
jiayl@webrtc.orge21cc9a2014-08-28 22:21:34 +0000310 if (video_info) {
311 const cricket::MediaContentDescription* video_content =
312 static_cast<const cricket::MediaContentDescription*>(
313 video_info->description);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000314
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +0000315 const auto* found =
316 cricket::GetStreamBySsrc(video_content->streams(), ssrc);
317 if (found) {
318 *track_id = found->id;
jiayl@webrtc.orge21cc9a2014-08-28 22:21:34 +0000319 return true;
320 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000321 }
322 return false;
323}
324
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000325static bool BadSdp(const std::string& source,
326 const std::string& type,
327 const std::string& reason,
328 std::string* err_desc) {
329 std::ostringstream desc;
330 desc << "Failed to set " << source << " " << type << " sdp: " << reason;
331
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000332 if (err_desc) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000333 *err_desc = desc.str();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000334 }
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000335 LOG(LS_ERROR) << desc.str();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000336 return false;
337}
338
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000339static bool BadSdp(cricket::ContentSource source,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000340 const std::string& type,
341 const std::string& reason,
342 std::string* err_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000343 if (source == cricket::CS_LOCAL) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000344 return BadSdp("local", type, reason, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000345 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000346 return BadSdp("remote", type, reason, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000347 }
348}
349
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000350static bool BadLocalSdp(const std::string& type,
351 const std::string& reason,
352 std::string* err_desc) {
353 return BadSdp(cricket::CS_LOCAL, type, reason, err_desc);
354}
355
356static bool BadRemoteSdp(const std::string& type,
357 const std::string& reason,
358 std::string* err_desc) {
359 return BadSdp(cricket::CS_REMOTE, type, reason, err_desc);
360}
361
362static bool BadOfferSdp(cricket::ContentSource source,
363 const std::string& reason,
364 std::string* err_desc) {
365 return BadSdp(source, SessionDescriptionInterface::kOffer, reason, err_desc);
366}
367
368static bool BadPranswerSdp(cricket::ContentSource source,
369 const std::string& reason,
370 std::string* err_desc) {
371 return BadSdp(source, SessionDescriptionInterface::kPrAnswer,
372 reason, err_desc);
373}
374
375static bool BadAnswerSdp(cricket::ContentSource source,
376 const std::string& reason,
377 std::string* err_desc) {
378 return BadSdp(source, SessionDescriptionInterface::kAnswer, reason, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000379}
380
381#define GET_STRING_OF_STATE(state) \
382 case cricket::BaseSession::state: \
383 result = #state; \
384 break;
385
386static std::string GetStateString(cricket::BaseSession::State state) {
387 std::string result;
388 switch (state) {
389 GET_STRING_OF_STATE(STATE_INIT)
390 GET_STRING_OF_STATE(STATE_SENTINITIATE)
391 GET_STRING_OF_STATE(STATE_RECEIVEDINITIATE)
392 GET_STRING_OF_STATE(STATE_SENTPRACCEPT)
393 GET_STRING_OF_STATE(STATE_SENTACCEPT)
394 GET_STRING_OF_STATE(STATE_RECEIVEDPRACCEPT)
395 GET_STRING_OF_STATE(STATE_RECEIVEDACCEPT)
396 GET_STRING_OF_STATE(STATE_SENTMODIFY)
397 GET_STRING_OF_STATE(STATE_RECEIVEDMODIFY)
398 GET_STRING_OF_STATE(STATE_SENTREJECT)
399 GET_STRING_OF_STATE(STATE_RECEIVEDREJECT)
400 GET_STRING_OF_STATE(STATE_SENTREDIRECT)
401 GET_STRING_OF_STATE(STATE_SENTTERMINATE)
402 GET_STRING_OF_STATE(STATE_RECEIVEDTERMINATE)
403 GET_STRING_OF_STATE(STATE_INPROGRESS)
404 GET_STRING_OF_STATE(STATE_DEINIT)
405 default:
406 ASSERT(false);
407 break;
408 }
409 return result;
410}
411
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000412#define GET_STRING_OF_ERROR_CODE(err) \
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000413 case cricket::BaseSession::err: \
414 result = #err; \
415 break;
416
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000417static std::string GetErrorCodeString(cricket::BaseSession::Error err) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000418 std::string result;
419 switch (err) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000420 GET_STRING_OF_ERROR_CODE(ERROR_NONE)
421 GET_STRING_OF_ERROR_CODE(ERROR_TIME)
422 GET_STRING_OF_ERROR_CODE(ERROR_RESPONSE)
423 GET_STRING_OF_ERROR_CODE(ERROR_NETWORK)
424 GET_STRING_OF_ERROR_CODE(ERROR_CONTENT)
425 GET_STRING_OF_ERROR_CODE(ERROR_TRANSPORT)
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000426 default:
427 ASSERT(false);
428 break;
429 }
430 return result;
431}
432
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000433static std::string MakeErrorString(const std::string& error,
434 const std::string& desc) {
435 std::ostringstream ret;
436 ret << error << " " << desc;
437 return ret.str();
438}
439
440static std::string MakeTdErrorString(const std::string& desc) {
441 return MakeErrorString(kPushDownTDFailed, desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000442}
443
henrike@webrtc.orgb0ecc1c2014-03-26 22:44:28 +0000444// Set |option| to the highest-priority value of |key| in the optional
445// constraints if the key is found and has a valid value.
buildbot@webrtc.org44a317a2014-06-17 07:49:15 +0000446template<typename T>
henrike@webrtc.orgb0ecc1c2014-03-26 22:44:28 +0000447static void SetOptionFromOptionalConstraint(
448 const MediaConstraintsInterface* constraints,
buildbot@webrtc.org44a317a2014-06-17 07:49:15 +0000449 const std::string& key, cricket::Settable<T>* option) {
henrike@webrtc.orgb0ecc1c2014-03-26 22:44:28 +0000450 if (!constraints) {
451 return;
452 }
453 std::string string_value;
buildbot@webrtc.org44a317a2014-06-17 07:49:15 +0000454 T value;
henrike@webrtc.orgb0ecc1c2014-03-26 22:44:28 +0000455 if (constraints->GetOptional().FindFirst(key, &string_value)) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000456 if (rtc::FromString(string_value, &value)) {
henrike@webrtc.orgb0ecc1c2014-03-26 22:44:28 +0000457 option->Set(value);
458 }
459 }
460}
461
mallinath@webrtc.org3d81b1b2014-09-09 14:38:10 +0000462uint32 ConvertIceTransportTypeToCandidateFilter(
463 PeerConnectionInterface::IceTransportsType type) {
464 switch (type) {
465 case PeerConnectionInterface::kNone:
466 return cricket::CF_NONE;
467 case PeerConnectionInterface::kRelay:
468 return cricket::CF_RELAY;
469 case PeerConnectionInterface::kNoHost:
470 return (cricket::CF_ALL & ~cricket::CF_HOST);
471 case PeerConnectionInterface::kAll:
472 return cricket::CF_ALL;
473 default: ASSERT(false);
474 }
475 return cricket::CF_NONE;
476}
477
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000478// Help class used to remember if a a remote peer has requested ice restart by
479// by sending a description with new ice ufrag and password.
480class IceRestartAnswerLatch {
481 public:
482 IceRestartAnswerLatch() : ice_restart_(false) { }
483
wu@webrtc.org91053e72013-08-10 07:18:04 +0000484 // Returns true if CheckForRemoteIceRestart has been called with a new session
485 // description where ice password and ufrag has changed since last time
486 // Reset() was called.
487 bool Get() const {
488 return ice_restart_;
489 }
490
491 void Reset() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000492 if (ice_restart_) {
493 ice_restart_ = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000494 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000495 }
496
honghaiz503726c2015-07-31 12:37:38 -0700497 bool CheckForRemoteIceRestart(const SessionDescriptionInterface* old_desc,
498 const SessionDescriptionInterface* new_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000499 if (!old_desc || new_desc->type() != SessionDescriptionInterface::kOffer) {
honghaiz503726c2015-07-31 12:37:38 -0700500 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000501 }
502 const SessionDescription* new_sd = new_desc->description();
503 const SessionDescription* old_sd = old_desc->description();
504 const ContentInfos& contents = new_sd->contents();
505 for (size_t index = 0; index < contents.size(); ++index) {
506 const ContentInfo* cinfo = &contents[index];
507 if (cinfo->rejected) {
508 continue;
509 }
510 // If the content isn't rejected, check if ufrag and password has
511 // changed.
512 const cricket::TransportDescription* new_transport_desc =
513 new_sd->GetTransportDescriptionByName(cinfo->name);
514 const cricket::TransportDescription* old_transport_desc =
515 old_sd->GetTransportDescriptionByName(cinfo->name);
516 if (!new_transport_desc || !old_transport_desc) {
517 // No transport description exist. This is not an ice restart.
518 continue;
519 }
jiayl@webrtc.orgdb397e52014-06-20 16:32:09 +0000520 if (cricket::IceCredentialsChanged(old_transport_desc->ice_ufrag,
521 old_transport_desc->ice_pwd,
522 new_transport_desc->ice_ufrag,
523 new_transport_desc->ice_pwd)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000524 LOG(LS_INFO) << "Remote peer request ice restart.";
525 ice_restart_ = true;
honghaiz503726c2015-07-31 12:37:38 -0700526 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000527 }
528 }
honghaiz503726c2015-07-31 12:37:38 -0700529 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000530 }
531
532 private:
533 bool ice_restart_;
534};
535
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +0000536WebRtcSession::WebRtcSession(
537 cricket::ChannelManager* channel_manager,
538 rtc::Thread* signaling_thread,
539 rtc::Thread* worker_thread,
540 cricket::PortAllocator* port_allocator,
541 MediaStreamSignaling* mediastream_signaling)
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +0000542 : cricket::BaseSession(signaling_thread,
543 worker_thread,
544 port_allocator,
545 rtc::ToString(rtc::CreateRandomId64() & LLONG_MAX),
546 cricket::NS_JINGLE_RTP,
547 false),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000548 // RFC 3264: The numeric value of the session id and version in the
549 // o line MUST be representable with a "64 bit signed integer".
550 // Due to this constraint session id |sid_| is max limited to LLONG_MAX.
551 channel_manager_(channel_manager),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000552 mediastream_signaling_(mediastream_signaling),
553 ice_observer_(NULL),
554 ice_connection_state_(PeerConnectionInterface::kIceConnectionNew),
Peter Thatcher54360512015-07-08 11:08:35 -0700555 ice_connection_receiving_(true),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000556 older_version_remote_peer_(false),
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000557 dtls_enabled_(false),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000558 data_channel_type_(cricket::DCT_NONE),
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +0000559 ice_restart_latch_(new IceRestartAnswerLatch),
560 metrics_observer_(NULL) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000561}
562
563WebRtcSession::~WebRtcSession() {
tommi0f620f42015-07-09 03:25:02 -0700564 ASSERT(signaling_thread()->IsCurrent());
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000565 // Destroy video_channel_ first since it may have a pointer to the
566 // voice_channel_.
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +0000567 if (video_channel_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000568 SignalVideoChannelDestroyed();
569 channel_manager_->DestroyVideoChannel(video_channel_.release());
570 }
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +0000571 if (voice_channel_) {
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000572 SignalVoiceChannelDestroyed();
Fredrik Solenberg4b60c732015-05-07 14:07:48 +0200573 channel_manager_->DestroyVoiceChannel(voice_channel_.release(), nullptr);
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000574 }
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +0000575 if (data_channel_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000576 SignalDataChannelDestroyed();
577 channel_manager_->DestroyDataChannel(data_channel_.release());
578 }
579 for (size_t i = 0; i < saved_candidates_.size(); ++i) {
580 delete saved_candidates_[i];
581 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000582}
583
wu@webrtc.org91053e72013-08-10 07:18:04 +0000584bool WebRtcSession::Initialize(
wu@webrtc.org97077a32013-10-25 21:18:33 +0000585 const PeerConnectionFactoryInterface::Options& options,
buildbot@webrtc.org41451d42014-05-03 05:39:45 +0000586 const MediaConstraintsInterface* constraints,
Henrik Boström5e56c592015-08-11 10:33:13 +0200587 rtc::scoped_ptr<DtlsIdentityStoreInterface> dtls_identity_store,
Henrik Lundin64dad832015-05-11 12:44:23 +0200588 const PeerConnectionInterface::RTCConfiguration& rtc_configuration) {
589 bundle_policy_ = rtc_configuration.bundle_policy;
Peter Thatcheraf55ccc2015-05-21 07:48:41 -0700590 rtcp_mux_policy_ = rtc_configuration.rtcp_mux_policy;
Joachim Bauch04e5b492015-05-29 09:40:39 +0200591 SetSslMaxProtocolVersion(options.ssl_max_version);
pthatcher@webrtc.org877ac762015-02-04 22:03:09 +0000592
Henrik Boström87713d02015-08-25 09:53:21 +0200593 // Obtain a certificate from RTCConfiguration if any were provided (optional).
594 rtc::scoped_refptr<rtc::RTCCertificate> certificate;
595 if (!rtc_configuration.certificates.empty()) {
596 // TODO(hbos,torbjorng): Decide on certificate-selection strategy instead of
597 // just picking the first one. The decision should be made based on the DTLS
598 // handshake. The DTLS negotiations need to know about all certificates.
599 certificate = rtc_configuration.certificates[0];
600 }
601
honghaiz4edc39c2015-09-01 09:53:56 -0700602 SetIceConnectionReceivingTimeout(
603 rtc_configuration.ice_connection_receiving_timeout);
604
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000605 // TODO(perkj): Take |constraints| into consideration. Return false if not all
606 // mandatory constraints can be fulfilled. Note that |constraints|
607 // can be null.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000608 bool value;
sergeyu@chromium.orga59696b2013-09-13 23:48:58 +0000609
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000610 if (options.disable_encryption) {
611 dtls_enabled_ = false;
612 } else {
Henrik Boström87713d02015-08-25 09:53:21 +0200613 // Enable DTLS by default if we have an identity store or a certificate.
614 dtls_enabled_ = (dtls_identity_store || certificate);
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000615 // |constraints| can override the default |dtls_enabled_| value.
616 if (FindConstraint(
617 constraints,
618 MediaConstraintsInterface::kEnableDtlsSrtp,
Henrik Boström87713d02015-08-25 09:53:21 +0200619 &value, nullptr)) {
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000620 dtls_enabled_ = value;
621 }
sergeyu@chromium.orga59696b2013-09-13 23:48:58 +0000622 }
623
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000624 // Enable creation of RTP data channels if the kEnableRtpDataChannels is set.
wu@webrtc.org97077a32013-10-25 21:18:33 +0000625 // It takes precendence over the disable_sctp_data_channels
626 // PeerConnectionFactoryInterface::Options.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000627 if (FindConstraint(
628 constraints, MediaConstraintsInterface::kEnableRtpDataChannels,
629 &value, NULL) && value) {
630 LOG(LS_INFO) << "Allowing RTP data engine.";
631 data_channel_type_ = cricket::DCT_RTP;
wu@webrtc.org91053e72013-08-10 07:18:04 +0000632 } else {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000633 // DTLS has to be enabled to use SCTP.
wu@webrtc.org97077a32013-10-25 21:18:33 +0000634 if (!options.disable_sctp_data_channels && dtls_enabled_) {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000635 LOG(LS_INFO) << "Allowing SCTP data engine.";
636 data_channel_type_ = cricket::DCT_SCTP;
637 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000638 }
639 if (data_channel_type_ != cricket::DCT_NONE) {
640 mediastream_signaling_->SetDataChannelFactory(this);
641 }
642
wu@webrtc.orgde305012013-10-31 15:40:38 +0000643 // Find DSCP constraint.
644 if (FindConstraint(
645 constraints,
646 MediaConstraintsInterface::kEnableDscp,
647 &value, NULL)) {
henrike@webrtc.org6e3dbc22014-03-25 17:09:47 +0000648 audio_options_.dscp.Set(value);
649 video_options_.dscp.Set(value);
650 }
651
652 // Find Suspend Below Min Bitrate constraint.
653 if (FindConstraint(
654 constraints,
655 MediaConstraintsInterface::kEnableVideoSuspendBelowMinBitrate,
656 &value,
657 NULL)) {
658 video_options_.suspend_below_min_bitrate.Set(value);
wu@webrtc.orgde305012013-10-31 15:40:38 +0000659 }
660
henrike@webrtc.orgb0ecc1c2014-03-26 22:44:28 +0000661 SetOptionFromOptionalConstraint(constraints,
662 MediaConstraintsInterface::kScreencastMinBitrate,
663 &video_options_.screencast_min_bitrate);
664
665 // Find constraints for cpu overuse detection.
666 SetOptionFromOptionalConstraint(constraints,
667 MediaConstraintsInterface::kCpuUnderuseThreshold,
668 &video_options_.cpu_underuse_threshold);
669 SetOptionFromOptionalConstraint(constraints,
670 MediaConstraintsInterface::kCpuOveruseThreshold,
671 &video_options_.cpu_overuse_threshold);
buildbot@webrtc.org27626a62014-06-16 13:39:40 +0000672 SetOptionFromOptionalConstraint(constraints,
buildbot@webrtc.org44a317a2014-06-17 07:49:15 +0000673 MediaConstraintsInterface::kCpuOveruseDetection,
674 &video_options_.cpu_overuse_detection);
675 SetOptionFromOptionalConstraint(constraints,
676 MediaConstraintsInterface::kCpuOveruseEncodeUsage,
677 &video_options_.cpu_overuse_encode_usage);
678 SetOptionFromOptionalConstraint(constraints,
buildbot@webrtc.org27626a62014-06-16 13:39:40 +0000679 MediaConstraintsInterface::kCpuUnderuseEncodeRsdThreshold,
680 &video_options_.cpu_underuse_encode_rsd_threshold);
681 SetOptionFromOptionalConstraint(constraints,
682 MediaConstraintsInterface::kCpuOveruseEncodeRsdThreshold,
683 &video_options_.cpu_overuse_encode_rsd_threshold);
buildbot@webrtc.orgdb563902014-06-13 13:05:48 +0000684
buildbot@webrtc.org44a317a2014-06-17 07:49:15 +0000685 SetOptionFromOptionalConstraint(constraints,
buildbot@webrtc.org53df88c2014-08-07 22:46:01 +0000686 MediaConstraintsInterface::kNumUnsignalledRecvStreams,
687 &video_options_.unsignalled_recv_stream_limit);
688 if (video_options_.unsignalled_recv_stream_limit.IsSet()) {
689 int stream_limit;
690 video_options_.unsignalled_recv_stream_limit.Get(&stream_limit);
andresp@webrtc.orgff689be2015-02-12 11:54:26 +0000691 stream_limit = std::min(kMaxUnsignalledRecvStreams, stream_limit);
692 stream_limit = std::max(0, stream_limit);
buildbot@webrtc.org53df88c2014-08-07 22:46:01 +0000693 video_options_.unsignalled_recv_stream_limit.Set(stream_limit);
694 }
695
696 SetOptionFromOptionalConstraint(constraints,
buildbot@webrtc.org44a317a2014-06-17 07:49:15 +0000697 MediaConstraintsInterface::kHighStartBitrate,
698 &video_options_.video_start_bitrate);
wu@webrtc.orgcfe5e9c2014-03-27 17:03:58 +0000699
700 if (FindConstraint(
701 constraints,
702 MediaConstraintsInterface::kVeryHighBitrate,
703 &value,
704 NULL)) {
705 video_options_.video_highest_bitrate.Set(
706 cricket::VideoOptions::VERY_HIGH);
707 } else if (FindConstraint(
708 constraints,
709 MediaConstraintsInterface::kHighBitrate,
710 &value,
711 NULL)) {
712 video_options_.video_highest_bitrate.Set(
713 cricket::VideoOptions::HIGH);
714 }
715
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000716 SetOptionFromOptionalConstraint(constraints,
717 MediaConstraintsInterface::kCombinedAudioVideoBwe,
718 &audio_options_.combined_audio_video_bwe);
719
Henrik Lundin64dad832015-05-11 12:44:23 +0200720 audio_options_.audio_jitter_buffer_max_packets.Set(
721 rtc_configuration.audio_jitter_buffer_max_packets);
722
Henrik Lundin5263b3c2015-06-01 10:29:41 +0200723 audio_options_.audio_jitter_buffer_fast_accelerate.Set(
724 rtc_configuration.audio_jitter_buffer_fast_accelerate);
725
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000726 const cricket::VideoCodec default_codec(
727 JsepSessionDescription::kDefaultVideoCodecId,
728 JsepSessionDescription::kDefaultVideoCodecName,
729 JsepSessionDescription::kMaxVideoCodecWidth,
730 JsepSessionDescription::kMaxVideoCodecHeight,
731 JsepSessionDescription::kDefaultVideoCodecFramerate,
732 JsepSessionDescription::kDefaultVideoCodecPreference);
733 channel_manager_->SetDefaultVideoEncoderConfig(
734 cricket::VideoEncoderConfig(default_codec));
wu@webrtc.org91053e72013-08-10 07:18:04 +0000735
Henrik Boström87713d02015-08-25 09:53:21 +0200736 if (!dtls_enabled_) {
737 // Construct with DTLS disabled.
738 webrtc_session_desc_factory_.reset(new WebRtcSessionDescriptionFactory(
739 signaling_thread(),
740 channel_manager_,
741 mediastream_signaling_,
742 this,
743 id(),
744 data_channel_type_));
745 } else {
746 // Construct with DTLS enabled.
747 if (!certificate) {
748 // Use the |dtls_identity_store| to generate a certificate.
749 DCHECK(dtls_identity_store);
750 webrtc_session_desc_factory_.reset(new WebRtcSessionDescriptionFactory(
751 signaling_thread(),
752 channel_manager_,
753 mediastream_signaling_,
754 dtls_identity_store.Pass(),
755 this,
756 id(),
757 data_channel_type_));
758 } else {
759 // Use the already generated certificate.
760 webrtc_session_desc_factory_.reset(new WebRtcSessionDescriptionFactory(
761 signaling_thread(),
762 channel_manager_,
763 mediastream_signaling_,
764 certificate,
765 this,
766 id(),
767 data_channel_type_));
768 }
769 }
wu@webrtc.org91053e72013-08-10 07:18:04 +0000770
Henrik Boströmd8281982015-08-27 10:12:24 +0200771 webrtc_session_desc_factory_->SignalCertificateReady.connect(
772 this, &WebRtcSession::OnCertificateReady);
mallinath@webrtc.org7e809c32013-09-30 18:59:08 +0000773
wu@webrtc.org97077a32013-10-25 21:18:33 +0000774 if (options.disable_encryption) {
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000775 webrtc_session_desc_factory_->SetSdesPolicy(cricket::SEC_DISABLED);
mallinath@webrtc.org7e809c32013-09-30 18:59:08 +0000776 }
mallinath@webrtc.org3d81b1b2014-09-09 14:38:10 +0000777 port_allocator()->set_candidate_filter(
Henrik Lundin64dad832015-05-11 12:44:23 +0200778 ConvertIceTransportTypeToCandidateFilter(rtc_configuration.type));
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700779
780 if (rtc_configuration.enable_localhost_ice_candidate) {
781 port_allocator()->set_flags(
782 port_allocator()->flags() |
783 cricket::PORTALLOCATOR_ENABLE_LOCALHOST_CANDIDATE);
784 }
785
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000786 return true;
787}
788
789void WebRtcSession::Terminate() {
790 SetState(STATE_RECEIVEDTERMINATE);
791 RemoveUnusedChannelsAndTransports(NULL);
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +0000792 ASSERT(!voice_channel_);
793 ASSERT(!video_channel_);
794 ASSERT(!data_channel_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000795}
796
797bool WebRtcSession::StartCandidatesAllocation() {
798 // SpeculativelyConnectTransportChannels, will call ConnectChannels method
799 // from TransportProxy to start gathering ice candidates.
800 SpeculativelyConnectAllTransportChannels();
801 if (!saved_candidates_.empty()) {
802 // If there are saved candidates which arrived before local description is
803 // set, copy those to remote description.
804 CopySavedCandidates(remote_desc_.get());
805 }
806 // Push remote candidates present in remote description to transport channels.
807 UseCandidatesInSessionDescription(remote_desc_.get());
808 return true;
809}
810
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000811void WebRtcSession::SetSdesPolicy(cricket::SecurePolicy secure_policy) {
812 webrtc_session_desc_factory_->SetSdesPolicy(secure_policy);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000813}
814
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000815cricket::SecurePolicy WebRtcSession::SdesPolicy() const {
816 return webrtc_session_desc_factory_->SdesPolicy();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000817}
818
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000819bool WebRtcSession::GetSslRole(rtc::SSLRole* role) {
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000820 if (local_description() == NULL || remote_description() == NULL) {
821 LOG(LS_INFO) << "Local and Remote descriptions must be applied to get "
822 << "SSL Role of the session.";
823 return false;
824 }
825
826 // TODO(mallinath) - Return role of each transport, as role may differ from
827 // one another.
828 // In current implementaion we just return the role of first transport in the
829 // transport map.
830 for (cricket::TransportMap::const_iterator iter = transport_proxies().begin();
831 iter != transport_proxies().end(); ++iter) {
832 if (iter->second->impl()) {
833 return iter->second->impl()->GetSslRole(role);
834 }
835 }
836 return false;
837}
838
jiayl@webrtc.orgb18bf5e2014-08-04 18:34:16 +0000839void WebRtcSession::CreateOffer(
840 CreateSessionDescriptionObserver* observer,
841 const PeerConnectionInterface::RTCOfferAnswerOptions& options) {
842 webrtc_session_desc_factory_->CreateOffer(observer, options);
wu@webrtc.org91053e72013-08-10 07:18:04 +0000843}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000844
wu@webrtc.org91053e72013-08-10 07:18:04 +0000845void WebRtcSession::CreateAnswer(CreateSessionDescriptionObserver* observer,
846 const MediaConstraintsInterface* constraints) {
847 webrtc_session_desc_factory_->CreateAnswer(observer, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000848}
849
850bool WebRtcSession::SetLocalDescription(SessionDescriptionInterface* desc,
851 std::string* err_desc) {
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000852 // Takes the ownership of |desc| regardless of the result.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000853 rtc::scoped_ptr<SessionDescriptionInterface> desc_temp(desc);
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000854
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000855 // Validate SDP.
856 if (!ValidateSessionDescription(desc, cricket::CS_LOCAL, err_desc)) {
857 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000858 }
859
860 // Update the initiator flag if this session is the initiator.
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000861 Action action = GetAction(desc->type());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000862 if (state() == STATE_INIT && action == kOffer) {
863 set_initiator(true);
864 }
865
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000866 cricket::SecurePolicy sdes_policy =
867 webrtc_session_desc_factory_->SdesPolicy();
868 cricket::CryptoType crypto_required = dtls_enabled_ ?
869 cricket::CT_DTLS : (sdes_policy == cricket::SEC_REQUIRED ?
870 cricket::CT_SDES : cricket::CT_NONE);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000871 // Update the MediaContentDescription crypto settings as per the policy set.
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000872 UpdateSessionDescriptionSecurePolicy(crypto_required, desc->description());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000873
874 set_local_description(desc->description()->Copy());
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000875 local_desc_.reset(desc_temp.release());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000876
877 // Transport and Media channels will be created only when offer is set.
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000878 if (action == kOffer && !CreateChannels(local_desc_->description())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000879 // TODO(mallinath) - Handle CreateChannel failure, as new local description
880 // is applied. Restore back to old description.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000881 return BadLocalSdp(desc->type(), kCreateChannelFailed, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000882 }
883
884 // Remove channel and transport proxies, if MediaContentDescription is
885 // rejected.
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000886 RemoveUnusedChannelsAndTransports(local_desc_->description());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000887
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000888 if (!UpdateSessionState(action, cricket::CS_LOCAL, err_desc)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000889 return false;
890 }
mallinath@webrtc.org3d81b1b2014-09-09 14:38:10 +0000891
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000892 // Kick starting the ice candidates allocation.
893 StartCandidatesAllocation();
894
895 // Update state and SSRC of local MediaStreams and DataChannels based on the
896 // local session description.
897 mediastream_signaling_->OnLocalDescriptionChanged(local_desc_.get());
898
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000899 rtc::SSLRole role;
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000900 if (data_channel_type_ == cricket::DCT_SCTP && GetSslRole(&role)) {
901 mediastream_signaling_->OnDtlsRoleReadyForSctp(role);
902 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000903 if (error() != cricket::BaseSession::ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000904 return BadLocalSdp(desc->type(), GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000905 }
906 return true;
907}
908
909bool WebRtcSession::SetRemoteDescription(SessionDescriptionInterface* desc,
910 std::string* err_desc) {
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000911 // Takes the ownership of |desc| regardless of the result.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000912 rtc::scoped_ptr<SessionDescriptionInterface> desc_temp(desc);
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000913
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000914 // Validate SDP.
915 if (!ValidateSessionDescription(desc, cricket::CS_REMOTE, err_desc)) {
916 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000917 }
918
919 // Transport and Media channels will be created only when offer is set.
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000920 Action action = GetAction(desc->type());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000921 if (action == kOffer && !CreateChannels(desc->description())) {
922 // TODO(mallinath) - Handle CreateChannel failure, as new local description
923 // is applied. Restore back to old description.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000924 return BadRemoteSdp(desc->type(), kCreateChannelFailed, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000925 }
926
927 // Remove channel and transport proxies, if MediaContentDescription is
928 // rejected.
929 RemoveUnusedChannelsAndTransports(desc->description());
930
931 // NOTE: Candidates allocation will be initiated only when SetLocalDescription
932 // is called.
933 set_remote_description(desc->description()->Copy());
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000934 if (!UpdateSessionState(action, cricket::CS_REMOTE, err_desc)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000935 return false;
936 }
937
938 // Update remote MediaStreams.
939 mediastream_signaling_->OnRemoteDescriptionChanged(desc);
940 if (local_description() && !UseCandidatesInSessionDescription(desc)) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000941 return BadRemoteSdp(desc->type(), kInvalidCandidates, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000942 }
943
944 // Copy all saved candidates.
945 CopySavedCandidates(desc);
honghaiz503726c2015-07-31 12:37:38 -0700946
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000947 // Check if this new SessionDescription contains new ice ufrag and password
948 // that indicates the remote peer requests ice restart.
honghaiz503726c2015-07-31 12:37:38 -0700949 bool ice_restart =
950 ice_restart_latch_->CheckForRemoteIceRestart(remote_desc_.get(), desc);
951 // We retain all received candidates only if ICE is not restarted.
952 // When ICE is restarted, all previous candidates belong to an old generation
953 // and should not be kept.
954 if (!ice_restart) {
955 WebRtcSessionDescriptionFactory::CopyCandidatesFromSessionDescription(
956 remote_desc_.get(), desc);
957 }
958
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000959 remote_desc_.reset(desc_temp.release());
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000960
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000961 rtc::SSLRole role;
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000962 if (data_channel_type_ == cricket::DCT_SCTP && GetSslRole(&role)) {
963 mediastream_signaling_->OnDtlsRoleReadyForSctp(role);
964 }
965
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000966 if (error() != cricket::BaseSession::ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000967 return BadRemoteSdp(desc->type(), GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000968 }
jiayl@webrtc.orgdacdd942015-01-23 17:33:34 +0000969
970 // Set the the ICE connection state to connecting since the connection may
971 // become writable with peer reflexive candidates before any remote candidate
972 // is signaled.
973 // TODO(pthatcher): This is a short-term solution for crbug/446908. A real fix
974 // is to have a new signal the indicates a change in checking state from the
975 // transport and expose a new checking() member from transport that can be
976 // read to determine the current checking state. The existing SignalConnecting
977 // actually means "gathering candidates", so cannot be be used here.
978 if (desc->type() != SessionDescriptionInterface::kOffer &&
979 ice_connection_state_ == PeerConnectionInterface::kIceConnectionNew) {
980 SetIceConnectionState(PeerConnectionInterface::kIceConnectionChecking);
981 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000982 return true;
983}
984
985bool WebRtcSession::UpdateSessionState(
986 Action action, cricket::ContentSource source,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000987 std::string* err_desc) {
988 // If there's already a pending error then no state transition should happen.
989 // But all call-sites should be verifying this before calling us!
990 ASSERT(error() == cricket::BaseSession::ERROR_NONE);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000991 std::string td_err;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000992 if (action == kOffer) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000993 if (!PushdownTransportDescription(source, cricket::CA_OFFER, &td_err)) {
994 return BadOfferSdp(source, MakeTdErrorString(td_err), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000995 }
996 SetState(source == cricket::CS_LOCAL ?
997 STATE_SENTINITIATE : STATE_RECEIVEDINITIATE);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000998 if (!PushdownMediaDescription(cricket::CA_OFFER, source, err_desc)) {
999 SetError(BaseSession::ERROR_CONTENT, *err_desc);
1000 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001001 if (error() != cricket::BaseSession::ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001002 return BadOfferSdp(source, GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001003 }
1004 } else if (action == kPrAnswer) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001005 if (!PushdownTransportDescription(source, cricket::CA_PRANSWER, &td_err)) {
1006 return BadPranswerSdp(source, MakeTdErrorString(td_err), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001007 }
1008 EnableChannels();
1009 SetState(source == cricket::CS_LOCAL ?
1010 STATE_SENTPRACCEPT : STATE_RECEIVEDPRACCEPT);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +00001011 if (!PushdownMediaDescription(cricket::CA_PRANSWER, source, err_desc)) {
1012 SetError(BaseSession::ERROR_CONTENT, *err_desc);
1013 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001014 if (error() != cricket::BaseSession::ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001015 return BadPranswerSdp(source, GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001016 }
1017 } else if (action == kAnswer) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001018 if (!PushdownTransportDescription(source, cricket::CA_ANSWER, &td_err)) {
1019 return BadAnswerSdp(source, MakeTdErrorString(td_err), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001020 }
1021 MaybeEnableMuxingSupport();
1022 EnableChannels();
1023 SetState(source == cricket::CS_LOCAL ?
1024 STATE_SENTACCEPT : STATE_RECEIVEDACCEPT);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +00001025 if (!PushdownMediaDescription(cricket::CA_ANSWER, source, err_desc)) {
1026 SetError(BaseSession::ERROR_CONTENT, *err_desc);
1027 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001028 if (error() != cricket::BaseSession::ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001029 return BadAnswerSdp(source, GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001030 }
1031 }
1032 return true;
1033}
1034
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +00001035bool WebRtcSession::PushdownMediaDescription(
1036 cricket::ContentAction action,
1037 cricket::ContentSource source,
1038 std::string* err) {
1039 auto set_content = [this, action, source, err](cricket::BaseChannel* ch) {
1040 if (!ch) {
1041 return true;
1042 } else if (source == cricket::CS_LOCAL) {
1043 return ch->PushdownLocalDescription(
1044 base_local_description(), action, err);
1045 } else {
1046 return ch->PushdownRemoteDescription(
1047 base_remote_description(), action, err);
1048 }
1049 };
1050
1051 return (set_content(voice_channel()) &&
1052 set_content(video_channel()) &&
1053 set_content(data_channel()));
1054}
1055
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001056WebRtcSession::Action WebRtcSession::GetAction(const std::string& type) {
1057 if (type == SessionDescriptionInterface::kOffer) {
1058 return WebRtcSession::kOffer;
1059 } else if (type == SessionDescriptionInterface::kPrAnswer) {
1060 return WebRtcSession::kPrAnswer;
1061 } else if (type == SessionDescriptionInterface::kAnswer) {
1062 return WebRtcSession::kAnswer;
1063 }
1064 ASSERT(false && "unknown action type");
1065 return WebRtcSession::kOffer;
1066}
1067
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +00001068bool WebRtcSession::GetTransportStats(cricket::SessionStats* stats) {
1069 ASSERT(signaling_thread()->IsCurrent());
1070
1071 const auto get_transport_stats = [stats](const std::string& content_name,
1072 cricket::Transport* transport) {
1073 const std::string& transport_id = transport->content_name();
1074 stats->proxy_to_transport[content_name] = transport_id;
1075 if (stats->transport_stats.find(transport_id)
1076 != stats->transport_stats.end()) {
1077 // Transport stats already done for this transport.
1078 return true;
1079 }
1080
1081 cricket::TransportStats tstats;
1082 if (!transport->GetStats(&tstats)) {
1083 return false;
1084 }
1085
1086 stats->transport_stats[transport_id] = tstats;
1087 return true;
1088 };
1089
1090 for (const auto& kv : transport_proxies()) {
1091 cricket::Transport* transport = kv.second->impl();
1092 if (transport && !get_transport_stats(kv.first, transport)) {
1093 return false;
1094 }
1095 }
1096 return true;
1097}
1098
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001099bool WebRtcSession::ProcessIceMessage(const IceCandidateInterface* candidate) {
1100 if (state() == STATE_INIT) {
1101 LOG(LS_ERROR) << "ProcessIceMessage: ICE candidates can't be added "
1102 << "without any offer (local or remote) "
1103 << "session description.";
1104 return false;
1105 }
1106
1107 if (!candidate) {
1108 LOG(LS_ERROR) << "ProcessIceMessage: Candidate is NULL";
1109 return false;
1110 }
1111
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001112 bool valid = false;
1113 if (!ReadyToUseRemoteCandidate(candidate, NULL, &valid)) {
1114 if (valid) {
1115 LOG(LS_INFO) << "ProcessIceMessage: Candidate saved";
1116 saved_candidates_.push_back(
1117 new JsepIceCandidate(candidate->sdp_mid(),
1118 candidate->sdp_mline_index(),
1119 candidate->candidate()));
buildbot@webrtc.org61c1b8e2014-04-09 06:06:38 +00001120 }
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001121 return valid;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001122 }
1123
1124 // Add this candidate to the remote session description.
1125 if (!remote_desc_->AddCandidate(candidate)) {
1126 LOG(LS_ERROR) << "ProcessIceMessage: Candidate cannot be used";
1127 return false;
1128 }
1129
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001130 return UseCandidate(candidate);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001131}
1132
mallinath@webrtc.org3d81b1b2014-09-09 14:38:10 +00001133bool WebRtcSession::SetIceTransports(
1134 PeerConnectionInterface::IceTransportsType type) {
1135 return port_allocator()->set_candidate_filter(
1136 ConvertIceTransportTypeToCandidateFilter(type));
buildbot@webrtc.org41451d42014-05-03 05:39:45 +00001137}
1138
xians@webrtc.org4cb01282014-06-12 14:57:05 +00001139bool WebRtcSession::GetLocalTrackIdBySsrc(uint32 ssrc, std::string* track_id) {
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +00001140 if (!base_local_description())
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001141 return false;
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +00001142 return webrtc::GetTrackIdBySsrc(base_local_description(), ssrc, track_id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001143}
1144
xians@webrtc.org4cb01282014-06-12 14:57:05 +00001145bool WebRtcSession::GetRemoteTrackIdBySsrc(uint32 ssrc, std::string* track_id) {
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +00001146 if (!base_remote_description())
xians@webrtc.org4cb01282014-06-12 14:57:05 +00001147 return false;
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +00001148 return webrtc::GetTrackIdBySsrc(base_remote_description(), ssrc, track_id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001149}
1150
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001151std::string WebRtcSession::BadStateErrMsg(State state) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001152 std::ostringstream desc;
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001153 desc << "Called in wrong state: " << GetStateString(state);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001154 return desc.str();
1155}
1156
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001157void WebRtcSession::SetAudioPlayout(uint32 ssrc, bool enable,
1158 cricket::AudioRenderer* renderer) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001159 ASSERT(signaling_thread()->IsCurrent());
1160 if (!voice_channel_) {
1161 LOG(LS_ERROR) << "SetAudioPlayout: No audio channel exists.";
1162 return;
1163 }
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001164 if (!voice_channel_->SetRemoteRenderer(ssrc, renderer)) {
1165 // SetRenderer() can fail if the ssrc does not match any playout channel.
1166 LOG(LS_ERROR) << "SetAudioPlayout: ssrc is incorrect: " << ssrc;
1167 return;
1168 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001169 if (!voice_channel_->SetOutputScaling(ssrc, enable ? 1 : 0, enable ? 1 : 0)) {
1170 // Allow that SetOutputScaling fail if |enable| is false but assert
1171 // otherwise. This in the normal case when the underlying media channel has
1172 // already been deleted.
1173 ASSERT(enable == false);
1174 }
1175}
1176
1177void WebRtcSession::SetAudioSend(uint32 ssrc, bool enable,
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001178 const cricket::AudioOptions& options,
1179 cricket::AudioRenderer* renderer) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001180 ASSERT(signaling_thread()->IsCurrent());
1181 if (!voice_channel_) {
1182 LOG(LS_ERROR) << "SetAudioSend: No audio channel exists.";
1183 return;
1184 }
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001185 if (!voice_channel_->SetLocalRenderer(ssrc, renderer)) {
1186 // SetRenderer() can fail if the ssrc does not match any send channel.
1187 LOG(LS_ERROR) << "SetAudioSend: ssrc is incorrect: " << ssrc;
1188 return;
1189 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001190 if (!voice_channel_->MuteStream(ssrc, !enable)) {
1191 // Allow that MuteStream fail if |enable| is false but assert otherwise.
1192 // This in the normal case when the underlying media channel has already
1193 // been deleted.
1194 ASSERT(enable == false);
1195 return;
1196 }
1197 if (enable)
1198 voice_channel_->SetChannelOptions(options);
1199}
1200
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +00001201void WebRtcSession::SetAudioPlayoutVolume(uint32 ssrc, double volume) {
1202 ASSERT(signaling_thread()->IsCurrent());
1203 ASSERT(volume >= 0 && volume <= 10);
1204 if (!voice_channel_) {
1205 LOG(LS_ERROR) << "SetAudioPlayoutVolume: No audio channel exists.";
1206 return;
1207 }
1208
1209 if (!voice_channel_->SetOutputScaling(ssrc, volume, volume))
1210 ASSERT(false);
1211}
1212
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001213bool WebRtcSession::SetCaptureDevice(uint32 ssrc,
1214 cricket::VideoCapturer* camera) {
1215 ASSERT(signaling_thread()->IsCurrent());
1216
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001217 if (!video_channel_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001218 // |video_channel_| doesnt't exist. Probably because the remote end doesnt't
1219 // support video.
1220 LOG(LS_WARNING) << "Video not used in this call.";
1221 return false;
1222 }
1223 if (!video_channel_->SetCapturer(ssrc, camera)) {
1224 // Allow that SetCapturer fail if |camera| is NULL but assert otherwise.
1225 // This in the normal case when the underlying media channel has already
1226 // been deleted.
1227 ASSERT(camera == NULL);
1228 return false;
1229 }
1230 return true;
1231}
1232
1233void WebRtcSession::SetVideoPlayout(uint32 ssrc,
1234 bool enable,
1235 cricket::VideoRenderer* renderer) {
1236 ASSERT(signaling_thread()->IsCurrent());
1237 if (!video_channel_) {
1238 LOG(LS_WARNING) << "SetVideoPlayout: No video channel exists.";
1239 return;
1240 }
1241 if (!video_channel_->SetRenderer(ssrc, enable ? renderer : NULL)) {
1242 // Allow that SetRenderer fail if |renderer| is NULL but assert otherwise.
1243 // This in the normal case when the underlying media channel has already
1244 // been deleted.
1245 ASSERT(renderer == NULL);
1246 }
1247}
1248
1249void WebRtcSession::SetVideoSend(uint32 ssrc, bool enable,
1250 const cricket::VideoOptions* options) {
1251 ASSERT(signaling_thread()->IsCurrent());
1252 if (!video_channel_) {
1253 LOG(LS_WARNING) << "SetVideoSend: No video channel exists.";
1254 return;
1255 }
1256 if (!video_channel_->MuteStream(ssrc, !enable)) {
1257 // Allow that MuteStream fail if |enable| is false but assert otherwise.
1258 // This in the normal case when the underlying media channel has already
1259 // been deleted.
1260 ASSERT(enable == false);
1261 return;
1262 }
1263 if (enable && options)
1264 video_channel_->SetChannelOptions(*options);
1265}
1266
1267bool WebRtcSession::CanInsertDtmf(const std::string& track_id) {
1268 ASSERT(signaling_thread()->IsCurrent());
1269 if (!voice_channel_) {
1270 LOG(LS_ERROR) << "CanInsertDtmf: No audio channel exists.";
1271 return false;
1272 }
1273 uint32 send_ssrc = 0;
1274 // The Dtmf is negotiated per channel not ssrc, so we only check if the ssrc
1275 // exists.
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +00001276 if (!GetAudioSsrcByTrackId(base_local_description(), track_id,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001277 &send_ssrc)) {
1278 LOG(LS_ERROR) << "CanInsertDtmf: Track does not exist: " << track_id;
1279 return false;
1280 }
1281 return voice_channel_->CanInsertDtmf();
1282}
1283
1284bool WebRtcSession::InsertDtmf(const std::string& track_id,
1285 int code, int duration) {
1286 ASSERT(signaling_thread()->IsCurrent());
1287 if (!voice_channel_) {
1288 LOG(LS_ERROR) << "InsertDtmf: No audio channel exists.";
1289 return false;
1290 }
1291 uint32 send_ssrc = 0;
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +00001292 if (!VERIFY(GetAudioSsrcByTrackId(base_local_description(),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001293 track_id, &send_ssrc))) {
1294 LOG(LS_ERROR) << "InsertDtmf: Track does not exist: " << track_id;
1295 return false;
1296 }
1297 if (!voice_channel_->InsertDtmf(send_ssrc, code, duration,
1298 cricket::DF_SEND)) {
1299 LOG(LS_ERROR) << "Failed to insert DTMF to channel.";
1300 return false;
1301 }
1302 return true;
1303}
1304
1305sigslot::signal0<>* WebRtcSession::GetOnDestroyedSignal() {
1306 return &SignalVoiceChannelDestroyed;
1307}
1308
wu@webrtc.org78187522013-10-07 23:32:02 +00001309bool WebRtcSession::SendData(const cricket::SendDataParams& params,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001310 const rtc::Buffer& payload,
wu@webrtc.org78187522013-10-07 23:32:02 +00001311 cricket::SendDataResult* result) {
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001312 if (!data_channel_) {
wu@webrtc.org78187522013-10-07 23:32:02 +00001313 LOG(LS_ERROR) << "SendData called when data_channel_ is NULL.";
1314 return false;
1315 }
1316 return data_channel_->SendData(params, payload, result);
1317}
1318
1319bool WebRtcSession::ConnectDataChannel(DataChannel* webrtc_data_channel) {
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001320 if (!data_channel_) {
wu@webrtc.org78187522013-10-07 23:32:02 +00001321 LOG(LS_ERROR) << "ConnectDataChannel called when data_channel_ is NULL.";
1322 return false;
1323 }
wu@webrtc.org78187522013-10-07 23:32:02 +00001324 data_channel_->SignalReadyToSendData.connect(webrtc_data_channel,
1325 &DataChannel::OnChannelReady);
1326 data_channel_->SignalDataReceived.connect(webrtc_data_channel,
1327 &DataChannel::OnDataReceived);
wu@webrtc.org78187522013-10-07 23:32:02 +00001328 return true;
1329}
1330
1331void WebRtcSession::DisconnectDataChannel(DataChannel* webrtc_data_channel) {
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001332 if (!data_channel_) {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001333 LOG(LS_ERROR) << "DisconnectDataChannel called when data_channel_ is NULL.";
1334 return;
1335 }
wu@webrtc.org78187522013-10-07 23:32:02 +00001336 data_channel_->SignalReadyToSendData.disconnect(webrtc_data_channel);
1337 data_channel_->SignalDataReceived.disconnect(webrtc_data_channel);
1338}
1339
bemasc@webrtc.org9b5467e2014-12-04 23:16:52 +00001340void WebRtcSession::AddSctpDataStream(int sid) {
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001341 if (!data_channel_) {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001342 LOG(LS_ERROR) << "AddDataChannelStreams called when data_channel_ is NULL.";
1343 return;
1344 }
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00001345 data_channel_->AddRecvStream(cricket::StreamParams::CreateLegacy(sid));
1346 data_channel_->AddSendStream(cricket::StreamParams::CreateLegacy(sid));
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001347}
1348
bemasc@webrtc.org9b5467e2014-12-04 23:16:52 +00001349void WebRtcSession::RemoveSctpDataStream(int sid) {
1350 mediastream_signaling_->RemoveSctpDataChannel(sid);
jiayl@webrtc.org2eaac182014-06-17 16:02:46 +00001351
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001352 if (!data_channel_) {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001353 LOG(LS_ERROR) << "RemoveDataChannelStreams called when data_channel_ is "
1354 << "NULL.";
1355 return;
1356 }
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00001357 data_channel_->RemoveRecvStream(sid);
1358 data_channel_->RemoveSendStream(sid);
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001359}
1360
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +00001361bool WebRtcSession::ReadyToSendData() const {
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001362 return data_channel_ && data_channel_->ready_to_send_data();
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +00001363}
1364
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001365rtc::scoped_refptr<DataChannel> WebRtcSession::CreateDataChannel(
wu@webrtc.org78187522013-10-07 23:32:02 +00001366 const std::string& label,
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +00001367 const InternalDataChannelInit* config) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001368 if (state() == STATE_RECEIVEDTERMINATE) {
1369 return NULL;
1370 }
1371 if (data_channel_type_ == cricket::DCT_NONE) {
1372 LOG(LS_ERROR) << "CreateDataChannel: Data is not supported in this call.";
1373 return NULL;
1374 }
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +00001375 InternalDataChannelInit new_config =
1376 config ? (*config) : InternalDataChannelInit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001377 if (data_channel_type_ == cricket::DCT_SCTP) {
1378 if (new_config.id < 0) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001379 rtc::SSLRole role;
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001380 if (GetSslRole(&role) &&
1381 !mediastream_signaling_->AllocateSctpSid(role, &new_config.id)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001382 LOG(LS_ERROR) << "No id can be allocated for the SCTP data channel.";
1383 return NULL;
1384 }
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001385 } else if (!mediastream_signaling_->IsSctpSidAvailable(new_config.id)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001386 LOG(LS_ERROR) << "Failed to create a SCTP data channel "
1387 << "because the id is already in use or out of range.";
1388 return NULL;
1389 }
1390 }
wu@webrtc.org91053e72013-08-10 07:18:04 +00001391
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001392 rtc::scoped_refptr<DataChannel> channel(DataChannel::Create(
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +00001393 this, data_channel_type_, label, new_config));
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001394 if (channel && !mediastream_signaling_->AddDataChannel(channel))
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001395 return NULL;
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001396
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001397 return channel;
1398}
1399
1400cricket::DataChannelType WebRtcSession::data_channel_type() const {
1401 return data_channel_type_;
1402}
1403
wu@webrtc.org91053e72013-08-10 07:18:04 +00001404bool WebRtcSession::IceRestartPending() const {
1405 return ice_restart_latch_->Get();
1406}
1407
1408void WebRtcSession::ResetIceRestartLatch() {
1409 ice_restart_latch_->Reset();
1410}
1411
Henrik Boströmd8281982015-08-27 10:12:24 +02001412void WebRtcSession::OnCertificateReady(
1413 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate) {
1414 SetCertificate(certificate);
wu@webrtc.org91053e72013-08-10 07:18:04 +00001415}
1416
Henrik Boströmd8281982015-08-27 10:12:24 +02001417bool WebRtcSession::waiting_for_certificate_for_testing() const {
Henrik Boström87713d02015-08-25 09:53:21 +02001418 return webrtc_session_desc_factory_->waiting_for_certificate_for_testing();
wu@webrtc.org91053e72013-08-10 07:18:04 +00001419}
1420
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001421void WebRtcSession::SetIceConnectionState(
1422 PeerConnectionInterface::IceConnectionState state) {
1423 if (ice_connection_state_ == state) {
1424 return;
1425 }
1426
1427 // ASSERT that the requested transition is allowed. Note that
1428 // WebRtcSession does not implement "kIceConnectionClosed" (that is handled
1429 // within PeerConnection). This switch statement should compile away when
1430 // ASSERTs are disabled.
1431 switch (ice_connection_state_) {
1432 case PeerConnectionInterface::kIceConnectionNew:
1433 ASSERT(state == PeerConnectionInterface::kIceConnectionChecking);
1434 break;
1435 case PeerConnectionInterface::kIceConnectionChecking:
1436 ASSERT(state == PeerConnectionInterface::kIceConnectionFailed ||
1437 state == PeerConnectionInterface::kIceConnectionConnected);
1438 break;
1439 case PeerConnectionInterface::kIceConnectionConnected:
1440 ASSERT(state == PeerConnectionInterface::kIceConnectionDisconnected ||
1441 state == PeerConnectionInterface::kIceConnectionChecking ||
1442 state == PeerConnectionInterface::kIceConnectionCompleted);
1443 break;
1444 case PeerConnectionInterface::kIceConnectionCompleted:
1445 ASSERT(state == PeerConnectionInterface::kIceConnectionConnected ||
1446 state == PeerConnectionInterface::kIceConnectionDisconnected);
1447 break;
1448 case PeerConnectionInterface::kIceConnectionFailed:
1449 ASSERT(state == PeerConnectionInterface::kIceConnectionNew);
1450 break;
1451 case PeerConnectionInterface::kIceConnectionDisconnected:
1452 ASSERT(state == PeerConnectionInterface::kIceConnectionChecking ||
1453 state == PeerConnectionInterface::kIceConnectionConnected ||
1454 state == PeerConnectionInterface::kIceConnectionCompleted ||
1455 state == PeerConnectionInterface::kIceConnectionFailed);
1456 break;
1457 case PeerConnectionInterface::kIceConnectionClosed:
1458 ASSERT(false);
1459 break;
1460 default:
1461 ASSERT(false);
1462 break;
1463 }
1464
1465 ice_connection_state_ = state;
1466 if (ice_observer_) {
1467 ice_observer_->OnIceConnectionChange(ice_connection_state_);
1468 }
1469}
1470
1471void WebRtcSession::OnTransportRequestSignaling(
1472 cricket::Transport* transport) {
1473 ASSERT(signaling_thread()->IsCurrent());
1474 transport->OnSignalingReady();
1475 if (ice_observer_) {
1476 ice_observer_->OnIceGatheringChange(
1477 PeerConnectionInterface::kIceGatheringGathering);
1478 }
1479}
1480
1481void WebRtcSession::OnTransportConnecting(cricket::Transport* transport) {
1482 ASSERT(signaling_thread()->IsCurrent());
1483 // start monitoring for the write state of the transport.
1484 OnTransportWritable(transport);
1485}
1486
1487void WebRtcSession::OnTransportWritable(cricket::Transport* transport) {
1488 ASSERT(signaling_thread()->IsCurrent());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001489 if (transport->all_channels_writable()) {
henrike@webrtc.org05376342014-03-10 15:53:12 +00001490 SetIceConnectionState(PeerConnectionInterface::kIceConnectionConnected);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001491 } else if (transport->HasChannels()) {
1492 // If the current state is Connected or Completed, then there were writable
1493 // channels but now there are not, so the next state must be Disconnected.
1494 if (ice_connection_state_ ==
1495 PeerConnectionInterface::kIceConnectionConnected ||
1496 ice_connection_state_ ==
1497 PeerConnectionInterface::kIceConnectionCompleted) {
1498 SetIceConnectionState(
1499 PeerConnectionInterface::kIceConnectionDisconnected);
1500 }
1501 }
1502}
1503
mallinath@webrtc.org385857d2014-02-14 00:56:12 +00001504void WebRtcSession::OnTransportCompleted(cricket::Transport* transport) {
1505 ASSERT(signaling_thread()->IsCurrent());
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00001506 PeerConnectionInterface::IceConnectionState old_state = ice_connection_state_;
mallinath@webrtc.org385857d2014-02-14 00:56:12 +00001507 SetIceConnectionState(PeerConnectionInterface::kIceConnectionCompleted);
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00001508 // Only report once when Ice connection is completed.
1509 if (old_state != PeerConnectionInterface::kIceConnectionCompleted) {
jbauchac8869e2015-07-03 01:36:14 -07001510 cricket::TransportStats stats;
1511 if (metrics_observer_ && transport->GetStats(&stats)) {
1512 ReportBestConnectionState(stats);
1513 ReportNegotiatedCiphers(stats);
1514 }
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00001515 }
mallinath@webrtc.org385857d2014-02-14 00:56:12 +00001516}
1517
1518void WebRtcSession::OnTransportFailed(cricket::Transport* transport) {
1519 ASSERT(signaling_thread()->IsCurrent());
1520 SetIceConnectionState(PeerConnectionInterface::kIceConnectionFailed);
1521}
1522
Peter Thatcher54360512015-07-08 11:08:35 -07001523void WebRtcSession::OnTransportReceiving(cricket::Transport* transport) {
1524 ASSERT(signaling_thread()->IsCurrent());
1525 // The ice connection is considered receiving if at least one transport is
1526 // receiving on any channels.
1527 bool receiving = false;
1528 for (const auto& kv : transport_proxies()) {
1529 cricket::Transport* transport = kv.second->impl();
1530 if (transport && transport->any_channel_receiving()) {
1531 receiving = true;
1532 break;
1533 }
1534 }
1535 SetIceConnectionReceiving(receiving);
1536}
1537
1538void WebRtcSession::SetIceConnectionReceiving(bool receiving) {
1539 if (ice_connection_receiving_ == receiving) {
1540 return;
1541 }
1542 ice_connection_receiving_ = receiving;
1543 if (ice_observer_) {
1544 ice_observer_->OnIceConnectionReceivingChange(receiving);
1545 }
1546}
1547
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001548void WebRtcSession::OnTransportProxyCandidatesReady(
1549 cricket::TransportProxy* proxy, const cricket::Candidates& candidates) {
1550 ASSERT(signaling_thread()->IsCurrent());
1551 ProcessNewLocalCandidate(proxy->content_name(), candidates);
1552}
1553
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001554void WebRtcSession::OnCandidatesAllocationDone() {
1555 ASSERT(signaling_thread()->IsCurrent());
1556 if (ice_observer_) {
1557 ice_observer_->OnIceGatheringChange(
1558 PeerConnectionInterface::kIceGatheringComplete);
1559 ice_observer_->OnIceComplete();
1560 }
1561}
1562
1563// Enabling voice and video channel.
1564void WebRtcSession::EnableChannels() {
1565 if (voice_channel_ && !voice_channel_->enabled())
1566 voice_channel_->Enable(true);
1567
1568 if (video_channel_ && !video_channel_->enabled())
1569 video_channel_->Enable(true);
1570
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001571 if (data_channel_ && !data_channel_->enabled())
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001572 data_channel_->Enable(true);
1573}
1574
1575void WebRtcSession::ProcessNewLocalCandidate(
1576 const std::string& content_name,
1577 const cricket::Candidates& candidates) {
1578 int sdp_mline_index;
1579 if (!GetLocalCandidateMediaIndex(content_name, &sdp_mline_index)) {
1580 LOG(LS_ERROR) << "ProcessNewLocalCandidate: content name "
1581 << content_name << " not found";
1582 return;
1583 }
1584
1585 for (cricket::Candidates::const_iterator citer = candidates.begin();
1586 citer != candidates.end(); ++citer) {
1587 // Use content_name as the candidate media id.
1588 JsepIceCandidate candidate(content_name, sdp_mline_index, *citer);
1589 if (ice_observer_) {
1590 ice_observer_->OnIceCandidate(&candidate);
1591 }
1592 if (local_desc_) {
1593 local_desc_->AddCandidate(&candidate);
1594 }
1595 }
1596}
1597
1598// Returns the media index for a local ice candidate given the content name.
1599bool WebRtcSession::GetLocalCandidateMediaIndex(const std::string& content_name,
1600 int* sdp_mline_index) {
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +00001601 if (!base_local_description() || !sdp_mline_index)
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001602 return false;
1603
1604 bool content_found = false;
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +00001605 const ContentInfos& contents = base_local_description()->contents();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001606 for (size_t index = 0; index < contents.size(); ++index) {
1607 if (contents[index].name == content_name) {
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00001608 *sdp_mline_index = static_cast<int>(index);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001609 content_found = true;
1610 break;
1611 }
1612 }
1613 return content_found;
1614}
1615
1616bool WebRtcSession::UseCandidatesInSessionDescription(
1617 const SessionDescriptionInterface* remote_desc) {
1618 if (!remote_desc)
1619 return true;
1620 bool ret = true;
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001621
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001622 for (size_t m = 0; m < remote_desc->number_of_mediasections(); ++m) {
1623 const IceCandidateCollection* candidates = remote_desc->candidates(m);
1624 for (size_t n = 0; n < candidates->count(); ++n) {
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001625 const IceCandidateInterface* candidate = candidates->at(n);
1626 bool valid = false;
1627 if (!ReadyToUseRemoteCandidate(candidate, remote_desc, &valid)) {
1628 if (valid) {
1629 LOG(LS_INFO) << "UseCandidatesInSessionDescription: Candidate saved.";
1630 saved_candidates_.push_back(
1631 new JsepIceCandidate(candidate->sdp_mid(),
1632 candidate->sdp_mline_index(),
1633 candidate->candidate()));
1634 }
1635 continue;
1636 }
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001637 ret = UseCandidate(candidate);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001638 if (!ret)
1639 break;
1640 }
1641 }
1642 return ret;
1643}
1644
1645bool WebRtcSession::UseCandidate(
1646 const IceCandidateInterface* candidate) {
1647
1648 size_t mediacontent_index = static_cast<size_t>(candidate->sdp_mline_index());
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +00001649 size_t remote_content_size = base_remote_description()->contents().size();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001650 if (mediacontent_index >= remote_content_size) {
1651 LOG(LS_ERROR)
1652 << "UseRemoteCandidateInSession: Invalid candidate media index.";
1653 return false;
1654 }
1655
1656 cricket::ContentInfo content =
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +00001657 base_remote_description()->contents()[mediacontent_index];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001658 std::vector<cricket::Candidate> candidates;
1659 candidates.push_back(candidate->candidate());
1660 // Invoking BaseSession method to handle remote candidates.
1661 std::string error;
1662 if (OnRemoteCandidates(content.name, candidates, &error)) {
1663 // Candidates successfully submitted for checking.
1664 if (ice_connection_state_ == PeerConnectionInterface::kIceConnectionNew ||
1665 ice_connection_state_ ==
1666 PeerConnectionInterface::kIceConnectionDisconnected) {
1667 // If state is New, then the session has just gotten its first remote ICE
1668 // candidates, so go to Checking.
1669 // If state is Disconnected, the session is re-using old candidates or
1670 // receiving additional ones, so go to Checking.
1671 // If state is Connected, stay Connected.
1672 // TODO(bemasc): If state is Connected, and the new candidates are for a
1673 // newly added transport, then the state actually _should_ move to
1674 // checking. Add a way to distinguish that case.
1675 SetIceConnectionState(PeerConnectionInterface::kIceConnectionChecking);
1676 }
1677 // TODO(bemasc): If state is Completed, go back to Connected.
1678 } else {
fischman@webrtc.org4f2bd682014-03-28 18:13:34 +00001679 if (!error.empty()) {
1680 LOG(LS_WARNING) << error;
1681 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001682 }
1683 return true;
1684}
1685
1686void WebRtcSession::RemoveUnusedChannelsAndTransports(
1687 const SessionDescription* desc) {
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001688 // Destroy video_channel_ first since it may have a pointer to the
1689 // voice_channel_.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001690 const cricket::ContentInfo* video_info =
1691 cricket::GetFirstVideoContent(desc);
1692 if ((!video_info || video_info->rejected) && video_channel_) {
1693 mediastream_signaling_->OnVideoChannelClose();
1694 SignalVideoChannelDestroyed();
1695 const std::string content_name = video_channel_->content_name();
1696 channel_manager_->DestroyVideoChannel(video_channel_.release());
bjornv@webrtc.org520a69e2015-02-04 12:45:44 +00001697 DestroyTransportProxy(content_name);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001698 }
1699
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001700 const cricket::ContentInfo* voice_info =
1701 cricket::GetFirstAudioContent(desc);
1702 if ((!voice_info || voice_info->rejected) && voice_channel_) {
1703 mediastream_signaling_->OnAudioChannelClose();
1704 SignalVoiceChannelDestroyed();
1705 const std::string content_name = voice_channel_->content_name();
Fredrik Solenberg4b60c732015-05-07 14:07:48 +02001706 channel_manager_->DestroyVoiceChannel(voice_channel_.release(),
1707 video_channel_.get());
bjornv@webrtc.org520a69e2015-02-04 12:45:44 +00001708 DestroyTransportProxy(content_name);
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001709 }
1710
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001711 const cricket::ContentInfo* data_info =
1712 cricket::GetFirstDataContent(desc);
1713 if ((!data_info || data_info->rejected) && data_channel_) {
1714 mediastream_signaling_->OnDataChannelClose();
1715 SignalDataChannelDestroyed();
1716 const std::string content_name = data_channel_->content_name();
1717 channel_manager_->DestroyDataChannel(data_channel_.release());
bjornv@webrtc.org520a69e2015-02-04 12:45:44 +00001718 DestroyTransportProxy(content_name);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001719 }
1720}
1721
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001722// TODO(mallinath) - Add a correct error code if the channels are not creatued
1723// due to BUNDLE is enabled but rtcp-mux is disabled.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001724bool WebRtcSession::CreateChannels(const SessionDescription* desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001725 // Creating the media channels and transport proxies.
1726 const cricket::ContentInfo* voice = cricket::GetFirstAudioContent(desc);
1727 if (voice && !voice->rejected && !voice_channel_) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001728 if (!CreateVoiceChannel(voice)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001729 LOG(LS_ERROR) << "Failed to create voice channel.";
1730 return false;
1731 }
1732 }
1733
1734 const cricket::ContentInfo* video = cricket::GetFirstVideoContent(desc);
1735 if (video && !video->rejected && !video_channel_) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001736 if (!CreateVideoChannel(video)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001737 LOG(LS_ERROR) << "Failed to create video channel.";
1738 return false;
1739 }
1740 }
1741
1742 const cricket::ContentInfo* data = cricket::GetFirstDataContent(desc);
1743 if (data_channel_type_ != cricket::DCT_NONE &&
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001744 data && !data->rejected && !data_channel_) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001745 if (!CreateDataChannel(data)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001746 LOG(LS_ERROR) << "Failed to create data channel.";
1747 return false;
1748 }
1749 }
1750
Peter Thatcheraf55ccc2015-05-21 07:48:41 -07001751 if (rtcp_mux_policy_ == PeerConnectionInterface::kRtcpMuxPolicyRequire) {
1752 if (voice_channel()) {
1753 voice_channel()->ActivateRtcpMux();
1754 }
1755 if (video_channel()) {
1756 video_channel()->ActivateRtcpMux();
1757 }
1758 if (data_channel()) {
1759 data_channel()->ActivateRtcpMux();
1760 }
1761 }
1762
Donald Curtis0e209b02015-03-24 09:29:54 -07001763 // Enable bundle before when kMaxBundle policy is in effect.
1764 if (bundle_policy_ == PeerConnectionInterface::kBundlePolicyMaxBundle) {
Peter Thatcher4eddf182015-04-30 10:55:59 -07001765 const cricket::ContentGroup* bundle_group = desc->GetGroupByName(
1766 cricket::GROUP_TYPE_BUNDLE);
1767 if (!bundle_group) {
Donald Curtis0e209b02015-03-24 09:29:54 -07001768 LOG(LS_WARNING) << "max-bundle specified without BUNDLE specified";
1769 return false;
1770 }
Peter Thatcher4eddf182015-04-30 10:55:59 -07001771 if (!BaseSession::BundleContentGroup(bundle_group)) {
Donald Curtis0e209b02015-03-24 09:29:54 -07001772 LOG(LS_WARNING) << "max-bundle failed to enable bundling.";
1773 return false;
1774 }
1775 }
1776
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001777 return true;
1778}
1779
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001780bool WebRtcSession::CreateVoiceChannel(const cricket::ContentInfo* content) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001781 voice_channel_.reset(channel_manager_->CreateVoiceChannel(
Jelena Marusicc28a8962015-05-29 15:05:44 +02001782 this, content->name, true, audio_options_));
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001783 if (!voice_channel_) {
wu@webrtc.orgde305012013-10-31 15:40:38 +00001784 return false;
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001785 }
wu@webrtc.orgde305012013-10-31 15:40:38 +00001786
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001787 voice_channel_->SignalDtlsSetupFailure.connect(
1788 this, &WebRtcSession::OnDtlsSetupFailure);
wu@webrtc.orgde305012013-10-31 15:40:38 +00001789 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001790}
1791
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001792bool WebRtcSession::CreateVideoChannel(const cricket::ContentInfo* content) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001793 video_channel_.reset(channel_manager_->CreateVideoChannel(
buildbot@webrtc.org1ecbe452014-10-14 20:29:28 +00001794 this, content->name, true, video_options_, voice_channel_.get()));
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001795 if (!video_channel_) {
1796 return false;
1797 }
1798
1799 video_channel_->SignalDtlsSetupFailure.connect(
1800 this, &WebRtcSession::OnDtlsSetupFailure);
1801 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001802}
1803
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001804bool WebRtcSession::CreateDataChannel(const cricket::ContentInfo* content) {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001805 bool sctp = (data_channel_type_ == cricket::DCT_SCTP);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001806 data_channel_.reset(channel_manager_->CreateDataChannel(
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001807 this, content->name, !sctp, data_channel_type_));
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001808 if (!data_channel_) {
wu@webrtc.org91053e72013-08-10 07:18:04 +00001809 return false;
1810 }
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001811
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001812 if (sctp) {
1813 mediastream_signaling_->OnDataTransportCreatedForSctp();
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +00001814 data_channel_->SignalDataReceived.connect(
1815 this, &WebRtcSession::OnDataChannelMessageReceived);
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +00001816 data_channel_->SignalStreamClosedRemotely.connect(
1817 mediastream_signaling_,
1818 &MediaStreamSignaling::OnRemoteSctpDataChannelClosed);
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001819 }
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001820
1821 data_channel_->SignalDtlsSetupFailure.connect(
1822 this, &WebRtcSession::OnDtlsSetupFailure);
wu@webrtc.org91053e72013-08-10 07:18:04 +00001823 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001824}
1825
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001826void WebRtcSession::OnDtlsSetupFailure(cricket::BaseChannel*, bool rtcp) {
1827 SetError(BaseSession::ERROR_TRANSPORT, rtcp ? kDtlsSetupFailureRtcp :
1828 kDtlsSetupFailureRtp);
1829}
1830
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001831void WebRtcSession::CopySavedCandidates(
1832 SessionDescriptionInterface* dest_desc) {
1833 if (!dest_desc) {
1834 ASSERT(false);
1835 return;
1836 }
1837 for (size_t i = 0; i < saved_candidates_.size(); ++i) {
1838 dest_desc->AddCandidate(saved_candidates_[i]);
1839 delete saved_candidates_[i];
1840 }
1841 saved_candidates_.clear();
1842}
1843
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +00001844void WebRtcSession::OnDataChannelMessageReceived(
1845 cricket::DataChannel* channel,
1846 const cricket::ReceiveDataParams& params,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001847 const rtc::Buffer& payload) {
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +00001848 ASSERT(data_channel_type_ == cricket::DCT_SCTP);
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +00001849 if (params.type == cricket::DMT_CONTROL &&
1850 mediastream_signaling_->IsSctpSidAvailable(params.ssrc)) {
1851 // Received CONTROL on unused sid, process as an OPEN message.
1852 mediastream_signaling_->AddDataChannelFromOpenMessage(params, payload);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001853 }
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +00001854 // otherwise ignore the message.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001855}
1856
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001857// Returns false if bundle is enabled and rtcp_mux is disabled.
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001858bool WebRtcSession::ValidateBundleSettings(const SessionDescription* desc) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001859 bool bundle_enabled = desc->HasGroup(cricket::GROUP_TYPE_BUNDLE);
1860 if (!bundle_enabled)
1861 return true;
1862
1863 const cricket::ContentGroup* bundle_group =
1864 desc->GetGroupByName(cricket::GROUP_TYPE_BUNDLE);
1865 ASSERT(bundle_group != NULL);
1866
1867 const cricket::ContentInfos& contents = desc->contents();
1868 for (cricket::ContentInfos::const_iterator citer = contents.begin();
1869 citer != contents.end(); ++citer) {
1870 const cricket::ContentInfo* content = (&*citer);
1871 ASSERT(content != NULL);
1872 if (bundle_group->HasContentName(content->name) &&
1873 !content->rejected && content->type == cricket::NS_JINGLE_RTP) {
1874 if (!HasRtcpMuxEnabled(content))
1875 return false;
1876 }
1877 }
1878 // RTCP-MUX is enabled in all the contents.
1879 return true;
1880}
1881
1882bool WebRtcSession::HasRtcpMuxEnabled(
1883 const cricket::ContentInfo* content) {
1884 const cricket::MediaContentDescription* description =
1885 static_cast<cricket::MediaContentDescription*>(content->description);
1886 return description->rtcp_mux();
1887}
1888
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001889bool WebRtcSession::ValidateSessionDescription(
1890 const SessionDescriptionInterface* sdesc,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001891 cricket::ContentSource source, std::string* err_desc) {
1892 std::string type;
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001893 if (error() != cricket::BaseSession::ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001894 return BadSdp(source, type, GetSessionErrorMsg(), err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001895 }
1896
1897 if (!sdesc || !sdesc->description()) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001898 return BadSdp(source, type, kInvalidSdp, err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001899 }
1900
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001901 type = sdesc->type();
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001902 Action action = GetAction(sdesc->type());
1903 if (source == cricket::CS_LOCAL) {
1904 if (!ExpectSetLocalDescription(action))
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001905 return BadLocalSdp(type, BadStateErrMsg(state()), err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001906 } else {
1907 if (!ExpectSetRemoteDescription(action))
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001908 return BadRemoteSdp(type, BadStateErrMsg(state()), err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001909 }
1910
1911 // Verify crypto settings.
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +00001912 std::string crypto_error;
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +00001913 if ((webrtc_session_desc_factory_->SdesPolicy() == cricket::SEC_REQUIRED ||
1914 dtls_enabled_) &&
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +00001915 !VerifyCrypto(sdesc->description(), dtls_enabled_, &crypto_error)) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001916 return BadSdp(source, type, crypto_error, err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001917 }
1918
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001919 // Verify ice-ufrag and ice-pwd.
1920 if (!VerifyIceUfragPwdPresent(sdesc->description())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001921 return BadSdp(source, type, kSdpWithoutIceUfragPwd, err_desc);
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001922 }
1923
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001924 if (!ValidateBundleSettings(sdesc->description())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001925 return BadSdp(source, type, kBundleWithoutRtcpMux, err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001926 }
1927
1928 // Verify m-lines in Answer when compared against Offer.
1929 if (action == kAnswer) {
1930 const cricket::SessionDescription* offer_desc =
1931 (source == cricket::CS_LOCAL) ? remote_description()->description() :
1932 local_description()->description();
1933 if (!VerifyMediaDescriptions(sdesc->description(), offer_desc)) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001934 return BadAnswerSdp(source, kMlineMismatch, err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001935 }
1936 }
1937
1938 return true;
1939}
1940
1941bool WebRtcSession::ExpectSetLocalDescription(Action action) {
1942 return ((action == kOffer && state() == STATE_INIT) ||
1943 // update local offer
1944 (action == kOffer && state() == STATE_SENTINITIATE) ||
1945 // update the current ongoing session.
1946 (action == kOffer && state() == STATE_RECEIVEDACCEPT) ||
1947 (action == kOffer && state() == STATE_SENTACCEPT) ||
1948 (action == kOffer && state() == STATE_INPROGRESS) ||
1949 // accept remote offer
1950 (action == kAnswer && state() == STATE_RECEIVEDINITIATE) ||
1951 (action == kAnswer && state() == STATE_SENTPRACCEPT) ||
1952 (action == kPrAnswer && state() == STATE_RECEIVEDINITIATE) ||
1953 (action == kPrAnswer && state() == STATE_SENTPRACCEPT));
1954}
1955
1956bool WebRtcSession::ExpectSetRemoteDescription(Action action) {
1957 return ((action == kOffer && state() == STATE_INIT) ||
1958 // update remote offer
1959 (action == kOffer && state() == STATE_RECEIVEDINITIATE) ||
1960 // update the current ongoing session
1961 (action == kOffer && state() == STATE_RECEIVEDACCEPT) ||
1962 (action == kOffer && state() == STATE_SENTACCEPT) ||
1963 (action == kOffer && state() == STATE_INPROGRESS) ||
1964 // accept local offer
1965 (action == kAnswer && state() == STATE_SENTINITIATE) ||
1966 (action == kAnswer && state() == STATE_RECEIVEDPRACCEPT) ||
1967 (action == kPrAnswer && state() == STATE_SENTINITIATE) ||
1968 (action == kPrAnswer && state() == STATE_RECEIVEDPRACCEPT));
1969}
1970
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001971std::string WebRtcSession::GetSessionErrorMsg() {
1972 std::ostringstream desc;
1973 desc << kSessionError << GetErrorCodeString(error()) << ". ";
1974 desc << kSessionErrorDesc << error_desc() << ".";
1975 return desc.str();
1976}
1977
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001978// We need to check the local/remote description for the Transport instead of
1979// the session, because a new Transport added during renegotiation may have
1980// them unset while the session has them set from the previous negotiation.
1981// Not doing so may trigger the auto generation of transport description and
1982// mess up DTLS identity information, ICE credential, etc.
1983bool WebRtcSession::ReadyToUseRemoteCandidate(
1984 const IceCandidateInterface* candidate,
1985 const SessionDescriptionInterface* remote_desc,
1986 bool* valid) {
1987 *valid = true;;
1988 cricket::TransportProxy* transport_proxy = NULL;
1989
1990 const SessionDescriptionInterface* current_remote_desc =
1991 remote_desc ? remote_desc : remote_description();
1992
1993 if (!current_remote_desc)
1994 return false;
1995
1996 size_t mediacontent_index =
1997 static_cast<size_t>(candidate->sdp_mline_index());
1998 size_t remote_content_size =
1999 current_remote_desc->description()->contents().size();
2000 if (mediacontent_index >= remote_content_size) {
2001 LOG(LS_ERROR)
2002 << "ReadyToUseRemoteCandidate: Invalid candidate media index.";
2003
2004 *valid = false;
2005 return false;
2006 }
2007
2008 cricket::ContentInfo content =
2009 current_remote_desc->description()->contents()[mediacontent_index];
2010 transport_proxy = GetTransportProxy(content.name);
2011
2012 return transport_proxy && transport_proxy->local_description_set() &&
2013 transport_proxy->remote_description_set();
2014}
2015
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00002016// Walk through the ConnectionInfos to gather best connection usage
2017// for IPv4 and IPv6.
jbauchac8869e2015-07-03 01:36:14 -07002018void WebRtcSession::ReportBestConnectionState(
2019 const cricket::TransportStats& stats) {
2020 DCHECK(metrics_observer_ != NULL);
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00002021 for (cricket::TransportChannelStatsList::const_iterator it =
2022 stats.channel_stats.begin();
2023 it != stats.channel_stats.end(); ++it) {
2024 for (cricket::ConnectionInfos::const_iterator it_info =
2025 it->connection_infos.begin();
2026 it_info != it->connection_infos.end(); ++it_info) {
2027 if (!it_info->best_connection) {
2028 continue;
2029 }
Guo-wei Shieh3d564c12015-08-19 16:51:15 -07002030
2031 PeerConnectionEnumCounterType type = kPeerConnectionEnumCounterMax;
2032 const cricket::Candidate& local = it_info->local_candidate;
2033 const cricket::Candidate& remote = it_info->remote_candidate;
2034
2035 // Increment the counter for IceCandidatePairType.
2036 if (local.protocol() == cricket::TCP_PROTOCOL_NAME ||
2037 (local.type() == RELAY_PORT_TYPE &&
2038 local.relay_protocol() == cricket::TCP_PROTOCOL_NAME)) {
2039 type = kEnumCounterIceCandidatePairTypeTcp;
2040 } else if (local.protocol() == cricket::UDP_PROTOCOL_NAME) {
2041 type = kEnumCounterIceCandidatePairTypeUdp;
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00002042 } else {
Guo-wei Shieh3d564c12015-08-19 16:51:15 -07002043 CHECK(0);
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00002044 }
Guo-wei Shieh3d564c12015-08-19 16:51:15 -07002045 metrics_observer_->IncrementEnumCounter(
2046 type, GetIceCandidatePairCounter(local, remote),
2047 kIceCandidatePairMax);
2048
2049 // Increment the counter for IP type.
2050 if (local.address().family() == AF_INET) {
Guo-wei Shieh3d564c12015-08-19 16:51:15 -07002051 metrics_observer_->IncrementEnumCounter(
2052 kEnumCounterAddressFamily, kBestConnections_IPv4,
2053 kPeerConnectionAddressFamilyCounter_Max);
2054
2055 } else if (local.address().family() == AF_INET6) {
Guo-wei Shieh3d564c12015-08-19 16:51:15 -07002056 metrics_observer_->IncrementEnumCounter(
2057 kEnumCounterAddressFamily, kBestConnections_IPv6,
2058 kPeerConnectionAddressFamilyCounter_Max);
2059 } else {
2060 CHECK(0);
2061 }
2062
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00002063 return;
2064 }
2065 }
2066}
2067
jbauchac8869e2015-07-03 01:36:14 -07002068void WebRtcSession::ReportNegotiatedCiphers(
2069 const cricket::TransportStats& stats) {
2070 DCHECK(metrics_observer_ != NULL);
2071 if (!dtls_enabled_ || stats.channel_stats.empty()) {
2072 return;
2073 }
2074
2075 const std::string& srtp_cipher = stats.channel_stats[0].srtp_cipher;
2076 const std::string& ssl_cipher = stats.channel_stats[0].ssl_cipher;
2077 if (srtp_cipher.empty() && ssl_cipher.empty()) {
2078 return;
2079 }
2080
2081 PeerConnectionMetricsName srtp_name;
2082 PeerConnectionMetricsName ssl_name;
2083 if (stats.content_name == cricket::CN_AUDIO) {
2084 srtp_name = kAudioSrtpCipher;
2085 ssl_name = kAudioSslCipher;
2086 } else if (stats.content_name == cricket::CN_VIDEO) {
2087 srtp_name = kVideoSrtpCipher;
2088 ssl_name = kVideoSslCipher;
2089 } else if (stats.content_name == cricket::CN_DATA) {
2090 srtp_name = kDataSrtpCipher;
2091 ssl_name = kDataSslCipher;
2092 } else {
2093 RTC_NOTREACHED();
2094 return;
2095 }
2096
2097 if (!srtp_cipher.empty()) {
2098 metrics_observer_->AddHistogramSample(srtp_name, srtp_cipher);
2099 }
2100 if (!ssl_cipher.empty()) {
2101 metrics_observer_->AddHistogramSample(ssl_name, ssl_cipher);
2102 }
2103}
2104
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002105} // namespace webrtc