blob: 179d793f5a04e9bdd5155f79652f5e6e7725c6dd [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 }
solenberg1dd98f32015-09-10 01:57:14 -07001185 if (!voice_channel_->SetAudioSend(ssrc, !enable, &options, renderer)) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001186 LOG(LS_ERROR) << "SetAudioSend: ssrc is incorrect: " << ssrc;
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001187 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001188}
1189
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +00001190void WebRtcSession::SetAudioPlayoutVolume(uint32 ssrc, double volume) {
1191 ASSERT(signaling_thread()->IsCurrent());
1192 ASSERT(volume >= 0 && volume <= 10);
1193 if (!voice_channel_) {
1194 LOG(LS_ERROR) << "SetAudioPlayoutVolume: No audio channel exists.";
1195 return;
1196 }
1197
solenbergbb741b32015-09-07 03:56:38 -07001198 if (!voice_channel_->SetOutputScaling(ssrc, volume, volume)) {
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +00001199 ASSERT(false);
solenbergbb741b32015-09-07 03:56:38 -07001200 }
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +00001201}
1202
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001203bool WebRtcSession::SetCaptureDevice(uint32 ssrc,
1204 cricket::VideoCapturer* camera) {
1205 ASSERT(signaling_thread()->IsCurrent());
1206
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001207 if (!video_channel_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001208 // |video_channel_| doesnt't exist. Probably because the remote end doesnt't
1209 // support video.
1210 LOG(LS_WARNING) << "Video not used in this call.";
1211 return false;
1212 }
1213 if (!video_channel_->SetCapturer(ssrc, camera)) {
1214 // Allow that SetCapturer fail if |camera| is NULL but assert otherwise.
1215 // This in the normal case when the underlying media channel has already
1216 // been deleted.
1217 ASSERT(camera == NULL);
1218 return false;
1219 }
1220 return true;
1221}
1222
1223void WebRtcSession::SetVideoPlayout(uint32 ssrc,
1224 bool enable,
1225 cricket::VideoRenderer* renderer) {
1226 ASSERT(signaling_thread()->IsCurrent());
1227 if (!video_channel_) {
1228 LOG(LS_WARNING) << "SetVideoPlayout: No video channel exists.";
1229 return;
1230 }
1231 if (!video_channel_->SetRenderer(ssrc, enable ? renderer : NULL)) {
1232 // Allow that SetRenderer fail if |renderer| is NULL but assert otherwise.
1233 // This in the normal case when the underlying media channel has already
1234 // been deleted.
1235 ASSERT(renderer == NULL);
1236 }
1237}
1238
1239void WebRtcSession::SetVideoSend(uint32 ssrc, bool enable,
1240 const cricket::VideoOptions* options) {
1241 ASSERT(signaling_thread()->IsCurrent());
1242 if (!video_channel_) {
1243 LOG(LS_WARNING) << "SetVideoSend: No video channel exists.";
1244 return;
1245 }
solenberg1dd98f32015-09-10 01:57:14 -07001246 if (!video_channel_->SetVideoSend(ssrc, !enable, options)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001247 // Allow that MuteStream fail if |enable| is false but assert otherwise.
1248 // This in the normal case when the underlying media channel has already
1249 // been deleted.
1250 ASSERT(enable == false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001251 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001252}
1253
1254bool WebRtcSession::CanInsertDtmf(const std::string& track_id) {
1255 ASSERT(signaling_thread()->IsCurrent());
1256 if (!voice_channel_) {
1257 LOG(LS_ERROR) << "CanInsertDtmf: No audio channel exists.";
1258 return false;
1259 }
1260 uint32 send_ssrc = 0;
1261 // The Dtmf is negotiated per channel not ssrc, so we only check if the ssrc
1262 // exists.
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +00001263 if (!GetAudioSsrcByTrackId(base_local_description(), track_id,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001264 &send_ssrc)) {
1265 LOG(LS_ERROR) << "CanInsertDtmf: Track does not exist: " << track_id;
1266 return false;
1267 }
1268 return voice_channel_->CanInsertDtmf();
1269}
1270
1271bool WebRtcSession::InsertDtmf(const std::string& track_id,
1272 int code, int duration) {
1273 ASSERT(signaling_thread()->IsCurrent());
1274 if (!voice_channel_) {
1275 LOG(LS_ERROR) << "InsertDtmf: No audio channel exists.";
1276 return false;
1277 }
1278 uint32 send_ssrc = 0;
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +00001279 if (!VERIFY(GetAudioSsrcByTrackId(base_local_description(),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001280 track_id, &send_ssrc))) {
1281 LOG(LS_ERROR) << "InsertDtmf: Track does not exist: " << track_id;
1282 return false;
1283 }
1284 if (!voice_channel_->InsertDtmf(send_ssrc, code, duration,
1285 cricket::DF_SEND)) {
1286 LOG(LS_ERROR) << "Failed to insert DTMF to channel.";
1287 return false;
1288 }
1289 return true;
1290}
1291
1292sigslot::signal0<>* WebRtcSession::GetOnDestroyedSignal() {
1293 return &SignalVoiceChannelDestroyed;
1294}
1295
wu@webrtc.org78187522013-10-07 23:32:02 +00001296bool WebRtcSession::SendData(const cricket::SendDataParams& params,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001297 const rtc::Buffer& payload,
wu@webrtc.org78187522013-10-07 23:32:02 +00001298 cricket::SendDataResult* result) {
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001299 if (!data_channel_) {
wu@webrtc.org78187522013-10-07 23:32:02 +00001300 LOG(LS_ERROR) << "SendData called when data_channel_ is NULL.";
1301 return false;
1302 }
1303 return data_channel_->SendData(params, payload, result);
1304}
1305
1306bool WebRtcSession::ConnectDataChannel(DataChannel* webrtc_data_channel) {
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001307 if (!data_channel_) {
wu@webrtc.org78187522013-10-07 23:32:02 +00001308 LOG(LS_ERROR) << "ConnectDataChannel called when data_channel_ is NULL.";
1309 return false;
1310 }
wu@webrtc.org78187522013-10-07 23:32:02 +00001311 data_channel_->SignalReadyToSendData.connect(webrtc_data_channel,
1312 &DataChannel::OnChannelReady);
1313 data_channel_->SignalDataReceived.connect(webrtc_data_channel,
1314 &DataChannel::OnDataReceived);
wu@webrtc.org78187522013-10-07 23:32:02 +00001315 return true;
1316}
1317
1318void WebRtcSession::DisconnectDataChannel(DataChannel* webrtc_data_channel) {
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001319 if (!data_channel_) {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001320 LOG(LS_ERROR) << "DisconnectDataChannel called when data_channel_ is NULL.";
1321 return;
1322 }
wu@webrtc.org78187522013-10-07 23:32:02 +00001323 data_channel_->SignalReadyToSendData.disconnect(webrtc_data_channel);
1324 data_channel_->SignalDataReceived.disconnect(webrtc_data_channel);
1325}
1326
bemasc@webrtc.org9b5467e2014-12-04 23:16:52 +00001327void WebRtcSession::AddSctpDataStream(int sid) {
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001328 if (!data_channel_) {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001329 LOG(LS_ERROR) << "AddDataChannelStreams called when data_channel_ is NULL.";
1330 return;
1331 }
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00001332 data_channel_->AddRecvStream(cricket::StreamParams::CreateLegacy(sid));
1333 data_channel_->AddSendStream(cricket::StreamParams::CreateLegacy(sid));
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001334}
1335
bemasc@webrtc.org9b5467e2014-12-04 23:16:52 +00001336void WebRtcSession::RemoveSctpDataStream(int sid) {
1337 mediastream_signaling_->RemoveSctpDataChannel(sid);
jiayl@webrtc.org2eaac182014-06-17 16:02:46 +00001338
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001339 if (!data_channel_) {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001340 LOG(LS_ERROR) << "RemoveDataChannelStreams called when data_channel_ is "
1341 << "NULL.";
1342 return;
1343 }
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00001344 data_channel_->RemoveRecvStream(sid);
1345 data_channel_->RemoveSendStream(sid);
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001346}
1347
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +00001348bool WebRtcSession::ReadyToSendData() const {
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001349 return data_channel_ && data_channel_->ready_to_send_data();
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +00001350}
1351
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001352rtc::scoped_refptr<DataChannel> WebRtcSession::CreateDataChannel(
wu@webrtc.org78187522013-10-07 23:32:02 +00001353 const std::string& label,
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +00001354 const InternalDataChannelInit* config) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001355 if (state() == STATE_RECEIVEDTERMINATE) {
1356 return NULL;
1357 }
1358 if (data_channel_type_ == cricket::DCT_NONE) {
1359 LOG(LS_ERROR) << "CreateDataChannel: Data is not supported in this call.";
1360 return NULL;
1361 }
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +00001362 InternalDataChannelInit new_config =
1363 config ? (*config) : InternalDataChannelInit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001364 if (data_channel_type_ == cricket::DCT_SCTP) {
1365 if (new_config.id < 0) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001366 rtc::SSLRole role;
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001367 if (GetSslRole(&role) &&
1368 !mediastream_signaling_->AllocateSctpSid(role, &new_config.id)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001369 LOG(LS_ERROR) << "No id can be allocated for the SCTP data channel.";
1370 return NULL;
1371 }
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001372 } else if (!mediastream_signaling_->IsSctpSidAvailable(new_config.id)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001373 LOG(LS_ERROR) << "Failed to create a SCTP data channel "
1374 << "because the id is already in use or out of range.";
1375 return NULL;
1376 }
1377 }
wu@webrtc.org91053e72013-08-10 07:18:04 +00001378
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001379 rtc::scoped_refptr<DataChannel> channel(DataChannel::Create(
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +00001380 this, data_channel_type_, label, new_config));
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001381 if (channel && !mediastream_signaling_->AddDataChannel(channel))
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001382 return NULL;
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001383
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001384 return channel;
1385}
1386
1387cricket::DataChannelType WebRtcSession::data_channel_type() const {
1388 return data_channel_type_;
1389}
1390
wu@webrtc.org91053e72013-08-10 07:18:04 +00001391bool WebRtcSession::IceRestartPending() const {
1392 return ice_restart_latch_->Get();
1393}
1394
1395void WebRtcSession::ResetIceRestartLatch() {
1396 ice_restart_latch_->Reset();
1397}
1398
Henrik Boströmd8281982015-08-27 10:12:24 +02001399void WebRtcSession::OnCertificateReady(
1400 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate) {
1401 SetCertificate(certificate);
wu@webrtc.org91053e72013-08-10 07:18:04 +00001402}
1403
Henrik Boströmd8281982015-08-27 10:12:24 +02001404bool WebRtcSession::waiting_for_certificate_for_testing() const {
Henrik Boström87713d02015-08-25 09:53:21 +02001405 return webrtc_session_desc_factory_->waiting_for_certificate_for_testing();
wu@webrtc.org91053e72013-08-10 07:18:04 +00001406}
1407
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001408void WebRtcSession::SetIceConnectionState(
1409 PeerConnectionInterface::IceConnectionState state) {
1410 if (ice_connection_state_ == state) {
1411 return;
1412 }
1413
1414 // ASSERT that the requested transition is allowed. Note that
1415 // WebRtcSession does not implement "kIceConnectionClosed" (that is handled
1416 // within PeerConnection). This switch statement should compile away when
1417 // ASSERTs are disabled.
1418 switch (ice_connection_state_) {
1419 case PeerConnectionInterface::kIceConnectionNew:
1420 ASSERT(state == PeerConnectionInterface::kIceConnectionChecking);
1421 break;
1422 case PeerConnectionInterface::kIceConnectionChecking:
1423 ASSERT(state == PeerConnectionInterface::kIceConnectionFailed ||
1424 state == PeerConnectionInterface::kIceConnectionConnected);
1425 break;
1426 case PeerConnectionInterface::kIceConnectionConnected:
1427 ASSERT(state == PeerConnectionInterface::kIceConnectionDisconnected ||
1428 state == PeerConnectionInterface::kIceConnectionChecking ||
1429 state == PeerConnectionInterface::kIceConnectionCompleted);
1430 break;
1431 case PeerConnectionInterface::kIceConnectionCompleted:
1432 ASSERT(state == PeerConnectionInterface::kIceConnectionConnected ||
1433 state == PeerConnectionInterface::kIceConnectionDisconnected);
1434 break;
1435 case PeerConnectionInterface::kIceConnectionFailed:
1436 ASSERT(state == PeerConnectionInterface::kIceConnectionNew);
1437 break;
1438 case PeerConnectionInterface::kIceConnectionDisconnected:
1439 ASSERT(state == PeerConnectionInterface::kIceConnectionChecking ||
1440 state == PeerConnectionInterface::kIceConnectionConnected ||
1441 state == PeerConnectionInterface::kIceConnectionCompleted ||
1442 state == PeerConnectionInterface::kIceConnectionFailed);
1443 break;
1444 case PeerConnectionInterface::kIceConnectionClosed:
1445 ASSERT(false);
1446 break;
1447 default:
1448 ASSERT(false);
1449 break;
1450 }
1451
1452 ice_connection_state_ = state;
1453 if (ice_observer_) {
1454 ice_observer_->OnIceConnectionChange(ice_connection_state_);
1455 }
1456}
1457
1458void WebRtcSession::OnTransportRequestSignaling(
1459 cricket::Transport* transport) {
1460 ASSERT(signaling_thread()->IsCurrent());
1461 transport->OnSignalingReady();
1462 if (ice_observer_) {
1463 ice_observer_->OnIceGatheringChange(
1464 PeerConnectionInterface::kIceGatheringGathering);
1465 }
1466}
1467
1468void WebRtcSession::OnTransportConnecting(cricket::Transport* transport) {
1469 ASSERT(signaling_thread()->IsCurrent());
1470 // start monitoring for the write state of the transport.
1471 OnTransportWritable(transport);
1472}
1473
1474void WebRtcSession::OnTransportWritable(cricket::Transport* transport) {
1475 ASSERT(signaling_thread()->IsCurrent());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001476 if (transport->all_channels_writable()) {
henrike@webrtc.org05376342014-03-10 15:53:12 +00001477 SetIceConnectionState(PeerConnectionInterface::kIceConnectionConnected);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001478 } else if (transport->HasChannels()) {
1479 // If the current state is Connected or Completed, then there were writable
1480 // channels but now there are not, so the next state must be Disconnected.
1481 if (ice_connection_state_ ==
1482 PeerConnectionInterface::kIceConnectionConnected ||
1483 ice_connection_state_ ==
1484 PeerConnectionInterface::kIceConnectionCompleted) {
1485 SetIceConnectionState(
1486 PeerConnectionInterface::kIceConnectionDisconnected);
1487 }
1488 }
1489}
1490
mallinath@webrtc.org385857d2014-02-14 00:56:12 +00001491void WebRtcSession::OnTransportCompleted(cricket::Transport* transport) {
1492 ASSERT(signaling_thread()->IsCurrent());
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00001493 PeerConnectionInterface::IceConnectionState old_state = ice_connection_state_;
mallinath@webrtc.org385857d2014-02-14 00:56:12 +00001494 SetIceConnectionState(PeerConnectionInterface::kIceConnectionCompleted);
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00001495 // Only report once when Ice connection is completed.
1496 if (old_state != PeerConnectionInterface::kIceConnectionCompleted) {
jbauchac8869e2015-07-03 01:36:14 -07001497 cricket::TransportStats stats;
1498 if (metrics_observer_ && transport->GetStats(&stats)) {
1499 ReportBestConnectionState(stats);
1500 ReportNegotiatedCiphers(stats);
1501 }
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00001502 }
mallinath@webrtc.org385857d2014-02-14 00:56:12 +00001503}
1504
1505void WebRtcSession::OnTransportFailed(cricket::Transport* transport) {
1506 ASSERT(signaling_thread()->IsCurrent());
1507 SetIceConnectionState(PeerConnectionInterface::kIceConnectionFailed);
1508}
1509
Peter Thatcher54360512015-07-08 11:08:35 -07001510void WebRtcSession::OnTransportReceiving(cricket::Transport* transport) {
1511 ASSERT(signaling_thread()->IsCurrent());
1512 // The ice connection is considered receiving if at least one transport is
1513 // receiving on any channels.
1514 bool receiving = false;
1515 for (const auto& kv : transport_proxies()) {
1516 cricket::Transport* transport = kv.second->impl();
1517 if (transport && transport->any_channel_receiving()) {
1518 receiving = true;
1519 break;
1520 }
1521 }
1522 SetIceConnectionReceiving(receiving);
1523}
1524
1525void WebRtcSession::SetIceConnectionReceiving(bool receiving) {
1526 if (ice_connection_receiving_ == receiving) {
1527 return;
1528 }
1529 ice_connection_receiving_ = receiving;
1530 if (ice_observer_) {
1531 ice_observer_->OnIceConnectionReceivingChange(receiving);
1532 }
1533}
1534
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001535void WebRtcSession::OnTransportProxyCandidatesReady(
1536 cricket::TransportProxy* proxy, const cricket::Candidates& candidates) {
1537 ASSERT(signaling_thread()->IsCurrent());
1538 ProcessNewLocalCandidate(proxy->content_name(), candidates);
1539}
1540
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001541void WebRtcSession::OnCandidatesAllocationDone() {
1542 ASSERT(signaling_thread()->IsCurrent());
1543 if (ice_observer_) {
1544 ice_observer_->OnIceGatheringChange(
1545 PeerConnectionInterface::kIceGatheringComplete);
1546 ice_observer_->OnIceComplete();
1547 }
1548}
1549
1550// Enabling voice and video channel.
1551void WebRtcSession::EnableChannels() {
1552 if (voice_channel_ && !voice_channel_->enabled())
1553 voice_channel_->Enable(true);
1554
1555 if (video_channel_ && !video_channel_->enabled())
1556 video_channel_->Enable(true);
1557
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001558 if (data_channel_ && !data_channel_->enabled())
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001559 data_channel_->Enable(true);
1560}
1561
1562void WebRtcSession::ProcessNewLocalCandidate(
1563 const std::string& content_name,
1564 const cricket::Candidates& candidates) {
1565 int sdp_mline_index;
1566 if (!GetLocalCandidateMediaIndex(content_name, &sdp_mline_index)) {
1567 LOG(LS_ERROR) << "ProcessNewLocalCandidate: content name "
1568 << content_name << " not found";
1569 return;
1570 }
1571
1572 for (cricket::Candidates::const_iterator citer = candidates.begin();
1573 citer != candidates.end(); ++citer) {
1574 // Use content_name as the candidate media id.
1575 JsepIceCandidate candidate(content_name, sdp_mline_index, *citer);
1576 if (ice_observer_) {
1577 ice_observer_->OnIceCandidate(&candidate);
1578 }
1579 if (local_desc_) {
1580 local_desc_->AddCandidate(&candidate);
1581 }
1582 }
1583}
1584
1585// Returns the media index for a local ice candidate given the content name.
1586bool WebRtcSession::GetLocalCandidateMediaIndex(const std::string& content_name,
1587 int* sdp_mline_index) {
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +00001588 if (!base_local_description() || !sdp_mline_index)
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001589 return false;
1590
1591 bool content_found = false;
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +00001592 const ContentInfos& contents = base_local_description()->contents();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001593 for (size_t index = 0; index < contents.size(); ++index) {
1594 if (contents[index].name == content_name) {
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00001595 *sdp_mline_index = static_cast<int>(index);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001596 content_found = true;
1597 break;
1598 }
1599 }
1600 return content_found;
1601}
1602
1603bool WebRtcSession::UseCandidatesInSessionDescription(
1604 const SessionDescriptionInterface* remote_desc) {
1605 if (!remote_desc)
1606 return true;
1607 bool ret = true;
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001608
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001609 for (size_t m = 0; m < remote_desc->number_of_mediasections(); ++m) {
1610 const IceCandidateCollection* candidates = remote_desc->candidates(m);
1611 for (size_t n = 0; n < candidates->count(); ++n) {
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001612 const IceCandidateInterface* candidate = candidates->at(n);
1613 bool valid = false;
1614 if (!ReadyToUseRemoteCandidate(candidate, remote_desc, &valid)) {
1615 if (valid) {
1616 LOG(LS_INFO) << "UseCandidatesInSessionDescription: Candidate saved.";
1617 saved_candidates_.push_back(
1618 new JsepIceCandidate(candidate->sdp_mid(),
1619 candidate->sdp_mline_index(),
1620 candidate->candidate()));
1621 }
1622 continue;
1623 }
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001624 ret = UseCandidate(candidate);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001625 if (!ret)
1626 break;
1627 }
1628 }
1629 return ret;
1630}
1631
1632bool WebRtcSession::UseCandidate(
1633 const IceCandidateInterface* candidate) {
1634
1635 size_t mediacontent_index = static_cast<size_t>(candidate->sdp_mline_index());
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +00001636 size_t remote_content_size = base_remote_description()->contents().size();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001637 if (mediacontent_index >= remote_content_size) {
1638 LOG(LS_ERROR)
1639 << "UseRemoteCandidateInSession: Invalid candidate media index.";
1640 return false;
1641 }
1642
1643 cricket::ContentInfo content =
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +00001644 base_remote_description()->contents()[mediacontent_index];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001645 std::vector<cricket::Candidate> candidates;
1646 candidates.push_back(candidate->candidate());
1647 // Invoking BaseSession method to handle remote candidates.
1648 std::string error;
1649 if (OnRemoteCandidates(content.name, candidates, &error)) {
1650 // Candidates successfully submitted for checking.
1651 if (ice_connection_state_ == PeerConnectionInterface::kIceConnectionNew ||
1652 ice_connection_state_ ==
1653 PeerConnectionInterface::kIceConnectionDisconnected) {
1654 // If state is New, then the session has just gotten its first remote ICE
1655 // candidates, so go to Checking.
1656 // If state is Disconnected, the session is re-using old candidates or
1657 // receiving additional ones, so go to Checking.
1658 // If state is Connected, stay Connected.
1659 // TODO(bemasc): If state is Connected, and the new candidates are for a
1660 // newly added transport, then the state actually _should_ move to
1661 // checking. Add a way to distinguish that case.
1662 SetIceConnectionState(PeerConnectionInterface::kIceConnectionChecking);
1663 }
1664 // TODO(bemasc): If state is Completed, go back to Connected.
1665 } else {
fischman@webrtc.org4f2bd682014-03-28 18:13:34 +00001666 if (!error.empty()) {
1667 LOG(LS_WARNING) << error;
1668 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001669 }
1670 return true;
1671}
1672
1673void WebRtcSession::RemoveUnusedChannelsAndTransports(
1674 const SessionDescription* desc) {
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001675 // Destroy video_channel_ first since it may have a pointer to the
1676 // voice_channel_.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001677 const cricket::ContentInfo* video_info =
1678 cricket::GetFirstVideoContent(desc);
1679 if ((!video_info || video_info->rejected) && video_channel_) {
1680 mediastream_signaling_->OnVideoChannelClose();
1681 SignalVideoChannelDestroyed();
1682 const std::string content_name = video_channel_->content_name();
1683 channel_manager_->DestroyVideoChannel(video_channel_.release());
bjornv@webrtc.org520a69e2015-02-04 12:45:44 +00001684 DestroyTransportProxy(content_name);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001685 }
1686
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001687 const cricket::ContentInfo* voice_info =
1688 cricket::GetFirstAudioContent(desc);
1689 if ((!voice_info || voice_info->rejected) && voice_channel_) {
1690 mediastream_signaling_->OnAudioChannelClose();
1691 SignalVoiceChannelDestroyed();
1692 const std::string content_name = voice_channel_->content_name();
Fredrik Solenberg4b60c732015-05-07 14:07:48 +02001693 channel_manager_->DestroyVoiceChannel(voice_channel_.release(),
1694 video_channel_.get());
bjornv@webrtc.org520a69e2015-02-04 12:45:44 +00001695 DestroyTransportProxy(content_name);
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001696 }
1697
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001698 const cricket::ContentInfo* data_info =
1699 cricket::GetFirstDataContent(desc);
1700 if ((!data_info || data_info->rejected) && data_channel_) {
1701 mediastream_signaling_->OnDataChannelClose();
1702 SignalDataChannelDestroyed();
1703 const std::string content_name = data_channel_->content_name();
1704 channel_manager_->DestroyDataChannel(data_channel_.release());
bjornv@webrtc.org520a69e2015-02-04 12:45:44 +00001705 DestroyTransportProxy(content_name);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001706 }
1707}
1708
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001709// TODO(mallinath) - Add a correct error code if the channels are not creatued
1710// due to BUNDLE is enabled but rtcp-mux is disabled.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001711bool WebRtcSession::CreateChannels(const SessionDescription* desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001712 // Creating the media channels and transport proxies.
1713 const cricket::ContentInfo* voice = cricket::GetFirstAudioContent(desc);
1714 if (voice && !voice->rejected && !voice_channel_) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001715 if (!CreateVoiceChannel(voice)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001716 LOG(LS_ERROR) << "Failed to create voice channel.";
1717 return false;
1718 }
1719 }
1720
1721 const cricket::ContentInfo* video = cricket::GetFirstVideoContent(desc);
1722 if (video && !video->rejected && !video_channel_) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001723 if (!CreateVideoChannel(video)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001724 LOG(LS_ERROR) << "Failed to create video channel.";
1725 return false;
1726 }
1727 }
1728
1729 const cricket::ContentInfo* data = cricket::GetFirstDataContent(desc);
1730 if (data_channel_type_ != cricket::DCT_NONE &&
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001731 data && !data->rejected && !data_channel_) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001732 if (!CreateDataChannel(data)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001733 LOG(LS_ERROR) << "Failed to create data channel.";
1734 return false;
1735 }
1736 }
1737
Peter Thatcheraf55ccc2015-05-21 07:48:41 -07001738 if (rtcp_mux_policy_ == PeerConnectionInterface::kRtcpMuxPolicyRequire) {
1739 if (voice_channel()) {
1740 voice_channel()->ActivateRtcpMux();
1741 }
1742 if (video_channel()) {
1743 video_channel()->ActivateRtcpMux();
1744 }
1745 if (data_channel()) {
1746 data_channel()->ActivateRtcpMux();
1747 }
1748 }
1749
Donald Curtis0e209b02015-03-24 09:29:54 -07001750 // Enable bundle before when kMaxBundle policy is in effect.
1751 if (bundle_policy_ == PeerConnectionInterface::kBundlePolicyMaxBundle) {
Peter Thatcher4eddf182015-04-30 10:55:59 -07001752 const cricket::ContentGroup* bundle_group = desc->GetGroupByName(
1753 cricket::GROUP_TYPE_BUNDLE);
1754 if (!bundle_group) {
Donald Curtis0e209b02015-03-24 09:29:54 -07001755 LOG(LS_WARNING) << "max-bundle specified without BUNDLE specified";
1756 return false;
1757 }
Peter Thatcher4eddf182015-04-30 10:55:59 -07001758 if (!BaseSession::BundleContentGroup(bundle_group)) {
Donald Curtis0e209b02015-03-24 09:29:54 -07001759 LOG(LS_WARNING) << "max-bundle failed to enable bundling.";
1760 return false;
1761 }
1762 }
1763
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001764 return true;
1765}
1766
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001767bool WebRtcSession::CreateVoiceChannel(const cricket::ContentInfo* content) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001768 voice_channel_.reset(channel_manager_->CreateVoiceChannel(
Jelena Marusicc28a8962015-05-29 15:05:44 +02001769 this, content->name, true, audio_options_));
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001770 if (!voice_channel_) {
wu@webrtc.orgde305012013-10-31 15:40:38 +00001771 return false;
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001772 }
wu@webrtc.orgde305012013-10-31 15:40:38 +00001773
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001774 voice_channel_->SignalDtlsSetupFailure.connect(
1775 this, &WebRtcSession::OnDtlsSetupFailure);
wu@webrtc.orgde305012013-10-31 15:40:38 +00001776 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001777}
1778
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001779bool WebRtcSession::CreateVideoChannel(const cricket::ContentInfo* content) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001780 video_channel_.reset(channel_manager_->CreateVideoChannel(
buildbot@webrtc.org1ecbe452014-10-14 20:29:28 +00001781 this, content->name, true, video_options_, voice_channel_.get()));
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001782 if (!video_channel_) {
1783 return false;
1784 }
1785
1786 video_channel_->SignalDtlsSetupFailure.connect(
1787 this, &WebRtcSession::OnDtlsSetupFailure);
1788 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001789}
1790
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001791bool WebRtcSession::CreateDataChannel(const cricket::ContentInfo* content) {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001792 bool sctp = (data_channel_type_ == cricket::DCT_SCTP);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001793 data_channel_.reset(channel_manager_->CreateDataChannel(
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001794 this, content->name, !sctp, data_channel_type_));
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001795 if (!data_channel_) {
wu@webrtc.org91053e72013-08-10 07:18:04 +00001796 return false;
1797 }
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001798
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001799 if (sctp) {
1800 mediastream_signaling_->OnDataTransportCreatedForSctp();
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +00001801 data_channel_->SignalDataReceived.connect(
1802 this, &WebRtcSession::OnDataChannelMessageReceived);
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +00001803 data_channel_->SignalStreamClosedRemotely.connect(
1804 mediastream_signaling_,
1805 &MediaStreamSignaling::OnRemoteSctpDataChannelClosed);
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001806 }
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001807
1808 data_channel_->SignalDtlsSetupFailure.connect(
1809 this, &WebRtcSession::OnDtlsSetupFailure);
wu@webrtc.org91053e72013-08-10 07:18:04 +00001810 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001811}
1812
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001813void WebRtcSession::OnDtlsSetupFailure(cricket::BaseChannel*, bool rtcp) {
1814 SetError(BaseSession::ERROR_TRANSPORT, rtcp ? kDtlsSetupFailureRtcp :
1815 kDtlsSetupFailureRtp);
1816}
1817
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001818void WebRtcSession::CopySavedCandidates(
1819 SessionDescriptionInterface* dest_desc) {
1820 if (!dest_desc) {
1821 ASSERT(false);
1822 return;
1823 }
1824 for (size_t i = 0; i < saved_candidates_.size(); ++i) {
1825 dest_desc->AddCandidate(saved_candidates_[i]);
1826 delete saved_candidates_[i];
1827 }
1828 saved_candidates_.clear();
1829}
1830
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +00001831void WebRtcSession::OnDataChannelMessageReceived(
1832 cricket::DataChannel* channel,
1833 const cricket::ReceiveDataParams& params,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001834 const rtc::Buffer& payload) {
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +00001835 ASSERT(data_channel_type_ == cricket::DCT_SCTP);
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +00001836 if (params.type == cricket::DMT_CONTROL &&
1837 mediastream_signaling_->IsSctpSidAvailable(params.ssrc)) {
1838 // Received CONTROL on unused sid, process as an OPEN message.
1839 mediastream_signaling_->AddDataChannelFromOpenMessage(params, payload);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001840 }
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +00001841 // otherwise ignore the message.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001842}
1843
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001844// Returns false if bundle is enabled and rtcp_mux is disabled.
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001845bool WebRtcSession::ValidateBundleSettings(const SessionDescription* desc) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001846 bool bundle_enabled = desc->HasGroup(cricket::GROUP_TYPE_BUNDLE);
1847 if (!bundle_enabled)
1848 return true;
1849
1850 const cricket::ContentGroup* bundle_group =
1851 desc->GetGroupByName(cricket::GROUP_TYPE_BUNDLE);
1852 ASSERT(bundle_group != NULL);
1853
1854 const cricket::ContentInfos& contents = desc->contents();
1855 for (cricket::ContentInfos::const_iterator citer = contents.begin();
1856 citer != contents.end(); ++citer) {
1857 const cricket::ContentInfo* content = (&*citer);
1858 ASSERT(content != NULL);
1859 if (bundle_group->HasContentName(content->name) &&
1860 !content->rejected && content->type == cricket::NS_JINGLE_RTP) {
1861 if (!HasRtcpMuxEnabled(content))
1862 return false;
1863 }
1864 }
1865 // RTCP-MUX is enabled in all the contents.
1866 return true;
1867}
1868
1869bool WebRtcSession::HasRtcpMuxEnabled(
1870 const cricket::ContentInfo* content) {
1871 const cricket::MediaContentDescription* description =
1872 static_cast<cricket::MediaContentDescription*>(content->description);
1873 return description->rtcp_mux();
1874}
1875
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001876bool WebRtcSession::ValidateSessionDescription(
1877 const SessionDescriptionInterface* sdesc,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001878 cricket::ContentSource source, std::string* err_desc) {
1879 std::string type;
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001880 if (error() != cricket::BaseSession::ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001881 return BadSdp(source, type, GetSessionErrorMsg(), err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001882 }
1883
1884 if (!sdesc || !sdesc->description()) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001885 return BadSdp(source, type, kInvalidSdp, err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001886 }
1887
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001888 type = sdesc->type();
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001889 Action action = GetAction(sdesc->type());
1890 if (source == cricket::CS_LOCAL) {
1891 if (!ExpectSetLocalDescription(action))
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001892 return BadLocalSdp(type, BadStateErrMsg(state()), err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001893 } else {
1894 if (!ExpectSetRemoteDescription(action))
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001895 return BadRemoteSdp(type, BadStateErrMsg(state()), err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001896 }
1897
1898 // Verify crypto settings.
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +00001899 std::string crypto_error;
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +00001900 if ((webrtc_session_desc_factory_->SdesPolicy() == cricket::SEC_REQUIRED ||
1901 dtls_enabled_) &&
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +00001902 !VerifyCrypto(sdesc->description(), dtls_enabled_, &crypto_error)) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001903 return BadSdp(source, type, crypto_error, err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001904 }
1905
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001906 // Verify ice-ufrag and ice-pwd.
1907 if (!VerifyIceUfragPwdPresent(sdesc->description())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001908 return BadSdp(source, type, kSdpWithoutIceUfragPwd, err_desc);
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001909 }
1910
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001911 if (!ValidateBundleSettings(sdesc->description())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001912 return BadSdp(source, type, kBundleWithoutRtcpMux, err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001913 }
1914
1915 // Verify m-lines in Answer when compared against Offer.
1916 if (action == kAnswer) {
1917 const cricket::SessionDescription* offer_desc =
1918 (source == cricket::CS_LOCAL) ? remote_description()->description() :
1919 local_description()->description();
1920 if (!VerifyMediaDescriptions(sdesc->description(), offer_desc)) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001921 return BadAnswerSdp(source, kMlineMismatch, err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001922 }
1923 }
1924
1925 return true;
1926}
1927
1928bool WebRtcSession::ExpectSetLocalDescription(Action action) {
1929 return ((action == kOffer && state() == STATE_INIT) ||
1930 // update local offer
1931 (action == kOffer && state() == STATE_SENTINITIATE) ||
1932 // update the current ongoing session.
1933 (action == kOffer && state() == STATE_RECEIVEDACCEPT) ||
1934 (action == kOffer && state() == STATE_SENTACCEPT) ||
1935 (action == kOffer && state() == STATE_INPROGRESS) ||
1936 // accept remote offer
1937 (action == kAnswer && state() == STATE_RECEIVEDINITIATE) ||
1938 (action == kAnswer && state() == STATE_SENTPRACCEPT) ||
1939 (action == kPrAnswer && state() == STATE_RECEIVEDINITIATE) ||
1940 (action == kPrAnswer && state() == STATE_SENTPRACCEPT));
1941}
1942
1943bool WebRtcSession::ExpectSetRemoteDescription(Action action) {
1944 return ((action == kOffer && state() == STATE_INIT) ||
1945 // update remote offer
1946 (action == kOffer && state() == STATE_RECEIVEDINITIATE) ||
1947 // update the current ongoing session
1948 (action == kOffer && state() == STATE_RECEIVEDACCEPT) ||
1949 (action == kOffer && state() == STATE_SENTACCEPT) ||
1950 (action == kOffer && state() == STATE_INPROGRESS) ||
1951 // accept local offer
1952 (action == kAnswer && state() == STATE_SENTINITIATE) ||
1953 (action == kAnswer && state() == STATE_RECEIVEDPRACCEPT) ||
1954 (action == kPrAnswer && state() == STATE_SENTINITIATE) ||
1955 (action == kPrAnswer && state() == STATE_RECEIVEDPRACCEPT));
1956}
1957
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001958std::string WebRtcSession::GetSessionErrorMsg() {
1959 std::ostringstream desc;
1960 desc << kSessionError << GetErrorCodeString(error()) << ". ";
1961 desc << kSessionErrorDesc << error_desc() << ".";
1962 return desc.str();
1963}
1964
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001965// We need to check the local/remote description for the Transport instead of
1966// the session, because a new Transport added during renegotiation may have
1967// them unset while the session has them set from the previous negotiation.
1968// Not doing so may trigger the auto generation of transport description and
1969// mess up DTLS identity information, ICE credential, etc.
1970bool WebRtcSession::ReadyToUseRemoteCandidate(
1971 const IceCandidateInterface* candidate,
1972 const SessionDescriptionInterface* remote_desc,
1973 bool* valid) {
1974 *valid = true;;
1975 cricket::TransportProxy* transport_proxy = NULL;
1976
1977 const SessionDescriptionInterface* current_remote_desc =
1978 remote_desc ? remote_desc : remote_description();
1979
1980 if (!current_remote_desc)
1981 return false;
1982
1983 size_t mediacontent_index =
1984 static_cast<size_t>(candidate->sdp_mline_index());
1985 size_t remote_content_size =
1986 current_remote_desc->description()->contents().size();
1987 if (mediacontent_index >= remote_content_size) {
1988 LOG(LS_ERROR)
1989 << "ReadyToUseRemoteCandidate: Invalid candidate media index.";
1990
1991 *valid = false;
1992 return false;
1993 }
1994
1995 cricket::ContentInfo content =
1996 current_remote_desc->description()->contents()[mediacontent_index];
1997 transport_proxy = GetTransportProxy(content.name);
1998
1999 return transport_proxy && transport_proxy->local_description_set() &&
2000 transport_proxy->remote_description_set();
2001}
2002
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00002003// Walk through the ConnectionInfos to gather best connection usage
2004// for IPv4 and IPv6.
jbauchac8869e2015-07-03 01:36:14 -07002005void WebRtcSession::ReportBestConnectionState(
2006 const cricket::TransportStats& stats) {
2007 DCHECK(metrics_observer_ != NULL);
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00002008 for (cricket::TransportChannelStatsList::const_iterator it =
2009 stats.channel_stats.begin();
2010 it != stats.channel_stats.end(); ++it) {
2011 for (cricket::ConnectionInfos::const_iterator it_info =
2012 it->connection_infos.begin();
2013 it_info != it->connection_infos.end(); ++it_info) {
2014 if (!it_info->best_connection) {
2015 continue;
2016 }
Guo-wei Shieh3d564c12015-08-19 16:51:15 -07002017
2018 PeerConnectionEnumCounterType type = kPeerConnectionEnumCounterMax;
2019 const cricket::Candidate& local = it_info->local_candidate;
2020 const cricket::Candidate& remote = it_info->remote_candidate;
2021
2022 // Increment the counter for IceCandidatePairType.
2023 if (local.protocol() == cricket::TCP_PROTOCOL_NAME ||
2024 (local.type() == RELAY_PORT_TYPE &&
2025 local.relay_protocol() == cricket::TCP_PROTOCOL_NAME)) {
2026 type = kEnumCounterIceCandidatePairTypeTcp;
2027 } else if (local.protocol() == cricket::UDP_PROTOCOL_NAME) {
2028 type = kEnumCounterIceCandidatePairTypeUdp;
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00002029 } else {
Guo-wei Shieh3d564c12015-08-19 16:51:15 -07002030 CHECK(0);
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00002031 }
Guo-wei Shieh3d564c12015-08-19 16:51:15 -07002032 metrics_observer_->IncrementEnumCounter(
2033 type, GetIceCandidatePairCounter(local, remote),
2034 kIceCandidatePairMax);
2035
2036 // Increment the counter for IP type.
2037 if (local.address().family() == AF_INET) {
Guo-wei Shieh3d564c12015-08-19 16:51:15 -07002038 metrics_observer_->IncrementEnumCounter(
2039 kEnumCounterAddressFamily, kBestConnections_IPv4,
2040 kPeerConnectionAddressFamilyCounter_Max);
2041
2042 } else if (local.address().family() == AF_INET6) {
Guo-wei Shieh3d564c12015-08-19 16:51:15 -07002043 metrics_observer_->IncrementEnumCounter(
2044 kEnumCounterAddressFamily, kBestConnections_IPv6,
2045 kPeerConnectionAddressFamilyCounter_Max);
2046 } else {
2047 CHECK(0);
2048 }
2049
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00002050 return;
2051 }
2052 }
2053}
2054
jbauchac8869e2015-07-03 01:36:14 -07002055void WebRtcSession::ReportNegotiatedCiphers(
2056 const cricket::TransportStats& stats) {
2057 DCHECK(metrics_observer_ != NULL);
2058 if (!dtls_enabled_ || stats.channel_stats.empty()) {
2059 return;
2060 }
2061
2062 const std::string& srtp_cipher = stats.channel_stats[0].srtp_cipher;
2063 const std::string& ssl_cipher = stats.channel_stats[0].ssl_cipher;
2064 if (srtp_cipher.empty() && ssl_cipher.empty()) {
2065 return;
2066 }
2067
2068 PeerConnectionMetricsName srtp_name;
2069 PeerConnectionMetricsName ssl_name;
2070 if (stats.content_name == cricket::CN_AUDIO) {
2071 srtp_name = kAudioSrtpCipher;
2072 ssl_name = kAudioSslCipher;
2073 } else if (stats.content_name == cricket::CN_VIDEO) {
2074 srtp_name = kVideoSrtpCipher;
2075 ssl_name = kVideoSslCipher;
2076 } else if (stats.content_name == cricket::CN_DATA) {
2077 srtp_name = kDataSrtpCipher;
2078 ssl_name = kDataSslCipher;
2079 } else {
2080 RTC_NOTREACHED();
2081 return;
2082 }
2083
2084 if (!srtp_cipher.empty()) {
2085 metrics_observer_->AddHistogramSample(srtp_name, srtp_cipher);
2086 }
2087 if (!ssl_cipher.empty()) {
2088 metrics_observer_->AddHistogramSample(ssl_name, ssl_cipher);
2089 }
2090}
2091
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002092} // namespace webrtc