blob: 26a9505e89851e98eb7decfda571c55eb4da601c [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 Solenberg709ed672015-09-15 12:26:33 +0200573 channel_manager_->DestroyVoiceChannel(voice_channel_.release());
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
Fredrik Solenberg709ed672015-09-15 12:26:33 +0200786 media_controller_.reset(MediaControllerInterface::Create(
787 worker_thread(), channel_manager_->media_engine()->GetVoE()));
788
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000789 return true;
790}
791
792void WebRtcSession::Terminate() {
793 SetState(STATE_RECEIVEDTERMINATE);
794 RemoveUnusedChannelsAndTransports(NULL);
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +0000795 ASSERT(!voice_channel_);
796 ASSERT(!video_channel_);
797 ASSERT(!data_channel_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000798}
799
800bool WebRtcSession::StartCandidatesAllocation() {
801 // SpeculativelyConnectTransportChannels, will call ConnectChannels method
802 // from TransportProxy to start gathering ice candidates.
803 SpeculativelyConnectAllTransportChannels();
804 if (!saved_candidates_.empty()) {
805 // If there are saved candidates which arrived before local description is
806 // set, copy those to remote description.
807 CopySavedCandidates(remote_desc_.get());
808 }
809 // Push remote candidates present in remote description to transport channels.
810 UseCandidatesInSessionDescription(remote_desc_.get());
811 return true;
812}
813
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000814void WebRtcSession::SetSdesPolicy(cricket::SecurePolicy secure_policy) {
815 webrtc_session_desc_factory_->SetSdesPolicy(secure_policy);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000816}
817
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000818cricket::SecurePolicy WebRtcSession::SdesPolicy() const {
819 return webrtc_session_desc_factory_->SdesPolicy();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000820}
821
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000822bool WebRtcSession::GetSslRole(rtc::SSLRole* role) {
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000823 if (local_description() == NULL || remote_description() == NULL) {
824 LOG(LS_INFO) << "Local and Remote descriptions must be applied to get "
825 << "SSL Role of the session.";
826 return false;
827 }
828
829 // TODO(mallinath) - Return role of each transport, as role may differ from
830 // one another.
831 // In current implementaion we just return the role of first transport in the
832 // transport map.
833 for (cricket::TransportMap::const_iterator iter = transport_proxies().begin();
834 iter != transport_proxies().end(); ++iter) {
835 if (iter->second->impl()) {
836 return iter->second->impl()->GetSslRole(role);
837 }
838 }
839 return false;
840}
841
jiayl@webrtc.orgb18bf5e2014-08-04 18:34:16 +0000842void WebRtcSession::CreateOffer(
843 CreateSessionDescriptionObserver* observer,
844 const PeerConnectionInterface::RTCOfferAnswerOptions& options) {
845 webrtc_session_desc_factory_->CreateOffer(observer, options);
wu@webrtc.org91053e72013-08-10 07:18:04 +0000846}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000847
wu@webrtc.org91053e72013-08-10 07:18:04 +0000848void WebRtcSession::CreateAnswer(CreateSessionDescriptionObserver* observer,
849 const MediaConstraintsInterface* constraints) {
850 webrtc_session_desc_factory_->CreateAnswer(observer, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000851}
852
853bool WebRtcSession::SetLocalDescription(SessionDescriptionInterface* desc,
854 std::string* err_desc) {
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000855 // Takes the ownership of |desc| regardless of the result.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000856 rtc::scoped_ptr<SessionDescriptionInterface> desc_temp(desc);
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000857
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000858 // Validate SDP.
859 if (!ValidateSessionDescription(desc, cricket::CS_LOCAL, err_desc)) {
860 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000861 }
862
863 // Update the initiator flag if this session is the initiator.
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000864 Action action = GetAction(desc->type());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000865 if (state() == STATE_INIT && action == kOffer) {
866 set_initiator(true);
867 }
868
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000869 cricket::SecurePolicy sdes_policy =
870 webrtc_session_desc_factory_->SdesPolicy();
871 cricket::CryptoType crypto_required = dtls_enabled_ ?
872 cricket::CT_DTLS : (sdes_policy == cricket::SEC_REQUIRED ?
873 cricket::CT_SDES : cricket::CT_NONE);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000874 // Update the MediaContentDescription crypto settings as per the policy set.
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000875 UpdateSessionDescriptionSecurePolicy(crypto_required, desc->description());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000876
877 set_local_description(desc->description()->Copy());
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000878 local_desc_.reset(desc_temp.release());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000879
880 // Transport and Media channels will be created only when offer is set.
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000881 if (action == kOffer && !CreateChannels(local_desc_->description())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000882 // TODO(mallinath) - Handle CreateChannel failure, as new local description
883 // is applied. Restore back to old description.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000884 return BadLocalSdp(desc->type(), kCreateChannelFailed, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000885 }
886
887 // Remove channel and transport proxies, if MediaContentDescription is
888 // rejected.
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000889 RemoveUnusedChannelsAndTransports(local_desc_->description());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000890
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000891 if (!UpdateSessionState(action, cricket::CS_LOCAL, err_desc)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000892 return false;
893 }
mallinath@webrtc.org3d81b1b2014-09-09 14:38:10 +0000894
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000895 // Kick starting the ice candidates allocation.
896 StartCandidatesAllocation();
897
898 // Update state and SSRC of local MediaStreams and DataChannels based on the
899 // local session description.
900 mediastream_signaling_->OnLocalDescriptionChanged(local_desc_.get());
901
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000902 rtc::SSLRole role;
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000903 if (data_channel_type_ == cricket::DCT_SCTP && GetSslRole(&role)) {
904 mediastream_signaling_->OnDtlsRoleReadyForSctp(role);
905 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000906 if (error() != cricket::BaseSession::ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000907 return BadLocalSdp(desc->type(), GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000908 }
909 return true;
910}
911
912bool WebRtcSession::SetRemoteDescription(SessionDescriptionInterface* desc,
913 std::string* err_desc) {
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000914 // Takes the ownership of |desc| regardless of the result.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000915 rtc::scoped_ptr<SessionDescriptionInterface> desc_temp(desc);
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000916
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000917 // Validate SDP.
918 if (!ValidateSessionDescription(desc, cricket::CS_REMOTE, err_desc)) {
919 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000920 }
921
922 // Transport and Media channels will be created only when offer is set.
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000923 Action action = GetAction(desc->type());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000924 if (action == kOffer && !CreateChannels(desc->description())) {
925 // TODO(mallinath) - Handle CreateChannel failure, as new local description
926 // is applied. Restore back to old description.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000927 return BadRemoteSdp(desc->type(), kCreateChannelFailed, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000928 }
929
930 // Remove channel and transport proxies, if MediaContentDescription is
931 // rejected.
932 RemoveUnusedChannelsAndTransports(desc->description());
933
934 // NOTE: Candidates allocation will be initiated only when SetLocalDescription
935 // is called.
936 set_remote_description(desc->description()->Copy());
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000937 if (!UpdateSessionState(action, cricket::CS_REMOTE, err_desc)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000938 return false;
939 }
940
941 // Update remote MediaStreams.
942 mediastream_signaling_->OnRemoteDescriptionChanged(desc);
943 if (local_description() && !UseCandidatesInSessionDescription(desc)) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000944 return BadRemoteSdp(desc->type(), kInvalidCandidates, err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000945 }
946
947 // Copy all saved candidates.
948 CopySavedCandidates(desc);
honghaiz503726c2015-07-31 12:37:38 -0700949
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000950 // Check if this new SessionDescription contains new ice ufrag and password
951 // that indicates the remote peer requests ice restart.
honghaiz503726c2015-07-31 12:37:38 -0700952 bool ice_restart =
953 ice_restart_latch_->CheckForRemoteIceRestart(remote_desc_.get(), desc);
954 // We retain all received candidates only if ICE is not restarted.
955 // When ICE is restarted, all previous candidates belong to an old generation
956 // and should not be kept.
957 if (!ice_restart) {
958 WebRtcSessionDescriptionFactory::CopyCandidatesFromSessionDescription(
959 remote_desc_.get(), desc);
960 }
961
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000962 remote_desc_.reset(desc_temp.release());
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000963
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000964 rtc::SSLRole role;
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000965 if (data_channel_type_ == cricket::DCT_SCTP && GetSslRole(&role)) {
966 mediastream_signaling_->OnDtlsRoleReadyForSctp(role);
967 }
968
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000969 if (error() != cricket::BaseSession::ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000970 return BadRemoteSdp(desc->type(), GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000971 }
jiayl@webrtc.orgdacdd942015-01-23 17:33:34 +0000972
973 // Set the the ICE connection state to connecting since the connection may
974 // become writable with peer reflexive candidates before any remote candidate
975 // is signaled.
976 // TODO(pthatcher): This is a short-term solution for crbug/446908. A real fix
977 // is to have a new signal the indicates a change in checking state from the
978 // transport and expose a new checking() member from transport that can be
979 // read to determine the current checking state. The existing SignalConnecting
980 // actually means "gathering candidates", so cannot be be used here.
981 if (desc->type() != SessionDescriptionInterface::kOffer &&
982 ice_connection_state_ == PeerConnectionInterface::kIceConnectionNew) {
983 SetIceConnectionState(PeerConnectionInterface::kIceConnectionChecking);
984 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000985 return true;
986}
987
988bool WebRtcSession::UpdateSessionState(
989 Action action, cricket::ContentSource source,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000990 std::string* err_desc) {
991 // If there's already a pending error then no state transition should happen.
992 // But all call-sites should be verifying this before calling us!
993 ASSERT(error() == cricket::BaseSession::ERROR_NONE);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000994 std::string td_err;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000995 if (action == kOffer) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000996 if (!PushdownTransportDescription(source, cricket::CA_OFFER, &td_err)) {
997 return BadOfferSdp(source, MakeTdErrorString(td_err), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000998 }
999 SetState(source == cricket::CS_LOCAL ?
1000 STATE_SENTINITIATE : STATE_RECEIVEDINITIATE);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +00001001 if (!PushdownMediaDescription(cricket::CA_OFFER, source, err_desc)) {
1002 SetError(BaseSession::ERROR_CONTENT, *err_desc);
1003 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001004 if (error() != cricket::BaseSession::ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001005 return BadOfferSdp(source, GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001006 }
1007 } else if (action == kPrAnswer) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001008 if (!PushdownTransportDescription(source, cricket::CA_PRANSWER, &td_err)) {
1009 return BadPranswerSdp(source, MakeTdErrorString(td_err), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001010 }
1011 EnableChannels();
1012 SetState(source == cricket::CS_LOCAL ?
1013 STATE_SENTPRACCEPT : STATE_RECEIVEDPRACCEPT);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +00001014 if (!PushdownMediaDescription(cricket::CA_PRANSWER, source, err_desc)) {
1015 SetError(BaseSession::ERROR_CONTENT, *err_desc);
1016 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001017 if (error() != cricket::BaseSession::ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001018 return BadPranswerSdp(source, GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001019 }
1020 } else if (action == kAnswer) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001021 if (!PushdownTransportDescription(source, cricket::CA_ANSWER, &td_err)) {
1022 return BadAnswerSdp(source, MakeTdErrorString(td_err), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001023 }
1024 MaybeEnableMuxingSupport();
1025 EnableChannels();
1026 SetState(source == cricket::CS_LOCAL ?
1027 STATE_SENTACCEPT : STATE_RECEIVEDACCEPT);
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +00001028 if (!PushdownMediaDescription(cricket::CA_ANSWER, source, err_desc)) {
1029 SetError(BaseSession::ERROR_CONTENT, *err_desc);
1030 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001031 if (error() != cricket::BaseSession::ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001032 return BadAnswerSdp(source, GetSessionErrorMsg(), err_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001033 }
1034 }
1035 return true;
1036}
1037
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +00001038bool WebRtcSession::PushdownMediaDescription(
1039 cricket::ContentAction action,
1040 cricket::ContentSource source,
1041 std::string* err) {
1042 auto set_content = [this, action, source, err](cricket::BaseChannel* ch) {
1043 if (!ch) {
1044 return true;
1045 } else if (source == cricket::CS_LOCAL) {
1046 return ch->PushdownLocalDescription(
1047 base_local_description(), action, err);
1048 } else {
1049 return ch->PushdownRemoteDescription(
1050 base_remote_description(), action, err);
1051 }
1052 };
1053
1054 return (set_content(voice_channel()) &&
1055 set_content(video_channel()) &&
1056 set_content(data_channel()));
1057}
1058
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001059WebRtcSession::Action WebRtcSession::GetAction(const std::string& type) {
1060 if (type == SessionDescriptionInterface::kOffer) {
1061 return WebRtcSession::kOffer;
1062 } else if (type == SessionDescriptionInterface::kPrAnswer) {
1063 return WebRtcSession::kPrAnswer;
1064 } else if (type == SessionDescriptionInterface::kAnswer) {
1065 return WebRtcSession::kAnswer;
1066 }
1067 ASSERT(false && "unknown action type");
1068 return WebRtcSession::kOffer;
1069}
1070
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +00001071bool WebRtcSession::GetTransportStats(cricket::SessionStats* stats) {
1072 ASSERT(signaling_thread()->IsCurrent());
1073
1074 const auto get_transport_stats = [stats](const std::string& content_name,
1075 cricket::Transport* transport) {
1076 const std::string& transport_id = transport->content_name();
1077 stats->proxy_to_transport[content_name] = transport_id;
1078 if (stats->transport_stats.find(transport_id)
1079 != stats->transport_stats.end()) {
1080 // Transport stats already done for this transport.
1081 return true;
1082 }
1083
1084 cricket::TransportStats tstats;
1085 if (!transport->GetStats(&tstats)) {
1086 return false;
1087 }
1088
1089 stats->transport_stats[transport_id] = tstats;
1090 return true;
1091 };
1092
1093 for (const auto& kv : transport_proxies()) {
1094 cricket::Transport* transport = kv.second->impl();
1095 if (transport && !get_transport_stats(kv.first, transport)) {
1096 return false;
1097 }
1098 }
1099 return true;
1100}
1101
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001102bool WebRtcSession::ProcessIceMessage(const IceCandidateInterface* candidate) {
1103 if (state() == STATE_INIT) {
1104 LOG(LS_ERROR) << "ProcessIceMessage: ICE candidates can't be added "
1105 << "without any offer (local or remote) "
1106 << "session description.";
1107 return false;
1108 }
1109
1110 if (!candidate) {
1111 LOG(LS_ERROR) << "ProcessIceMessage: Candidate is NULL";
1112 return false;
1113 }
1114
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001115 bool valid = false;
1116 if (!ReadyToUseRemoteCandidate(candidate, NULL, &valid)) {
1117 if (valid) {
1118 LOG(LS_INFO) << "ProcessIceMessage: Candidate saved";
1119 saved_candidates_.push_back(
1120 new JsepIceCandidate(candidate->sdp_mid(),
1121 candidate->sdp_mline_index(),
1122 candidate->candidate()));
buildbot@webrtc.org61c1b8e2014-04-09 06:06:38 +00001123 }
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001124 return valid;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001125 }
1126
1127 // Add this candidate to the remote session description.
1128 if (!remote_desc_->AddCandidate(candidate)) {
1129 LOG(LS_ERROR) << "ProcessIceMessage: Candidate cannot be used";
1130 return false;
1131 }
1132
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001133 return UseCandidate(candidate);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001134}
1135
mallinath@webrtc.org3d81b1b2014-09-09 14:38:10 +00001136bool WebRtcSession::SetIceTransports(
1137 PeerConnectionInterface::IceTransportsType type) {
1138 return port_allocator()->set_candidate_filter(
1139 ConvertIceTransportTypeToCandidateFilter(type));
buildbot@webrtc.org41451d42014-05-03 05:39:45 +00001140}
1141
xians@webrtc.org4cb01282014-06-12 14:57:05 +00001142bool WebRtcSession::GetLocalTrackIdBySsrc(uint32 ssrc, std::string* track_id) {
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +00001143 if (!base_local_description())
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001144 return false;
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +00001145 return webrtc::GetTrackIdBySsrc(base_local_description(), ssrc, track_id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001146}
1147
xians@webrtc.org4cb01282014-06-12 14:57:05 +00001148bool WebRtcSession::GetRemoteTrackIdBySsrc(uint32 ssrc, std::string* track_id) {
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +00001149 if (!base_remote_description())
xians@webrtc.org4cb01282014-06-12 14:57:05 +00001150 return false;
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +00001151 return webrtc::GetTrackIdBySsrc(base_remote_description(), ssrc, track_id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001152}
1153
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001154std::string WebRtcSession::BadStateErrMsg(State state) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001155 std::ostringstream desc;
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001156 desc << "Called in wrong state: " << GetStateString(state);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001157 return desc.str();
1158}
1159
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001160void WebRtcSession::SetAudioPlayout(uint32 ssrc, bool enable,
1161 cricket::AudioRenderer* renderer) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001162 ASSERT(signaling_thread()->IsCurrent());
1163 if (!voice_channel_) {
1164 LOG(LS_ERROR) << "SetAudioPlayout: No audio channel exists.";
1165 return;
1166 }
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001167 if (!voice_channel_->SetRemoteRenderer(ssrc, renderer)) {
1168 // SetRenderer() can fail if the ssrc does not match any playout channel.
1169 LOG(LS_ERROR) << "SetAudioPlayout: ssrc is incorrect: " << ssrc;
1170 return;
1171 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001172 if (!voice_channel_->SetOutputScaling(ssrc, enable ? 1 : 0, enable ? 1 : 0)) {
1173 // Allow that SetOutputScaling fail if |enable| is false but assert
1174 // otherwise. This in the normal case when the underlying media channel has
1175 // already been deleted.
1176 ASSERT(enable == false);
1177 }
1178}
1179
1180void WebRtcSession::SetAudioSend(uint32 ssrc, bool enable,
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001181 const cricket::AudioOptions& options,
1182 cricket::AudioRenderer* renderer) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001183 ASSERT(signaling_thread()->IsCurrent());
1184 if (!voice_channel_) {
1185 LOG(LS_ERROR) << "SetAudioSend: No audio channel exists.";
1186 return;
1187 }
solenberg1dd98f32015-09-10 01:57:14 -07001188 if (!voice_channel_->SetAudioSend(ssrc, !enable, &options, renderer)) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001189 LOG(LS_ERROR) << "SetAudioSend: ssrc is incorrect: " << ssrc;
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001190 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001191}
1192
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +00001193void WebRtcSession::SetAudioPlayoutVolume(uint32 ssrc, double volume) {
1194 ASSERT(signaling_thread()->IsCurrent());
1195 ASSERT(volume >= 0 && volume <= 10);
1196 if (!voice_channel_) {
1197 LOG(LS_ERROR) << "SetAudioPlayoutVolume: No audio channel exists.";
1198 return;
1199 }
1200
solenbergbb741b32015-09-07 03:56:38 -07001201 if (!voice_channel_->SetOutputScaling(ssrc, volume, volume)) {
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +00001202 ASSERT(false);
solenbergbb741b32015-09-07 03:56:38 -07001203 }
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +00001204}
1205
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001206bool WebRtcSession::SetCaptureDevice(uint32 ssrc,
1207 cricket::VideoCapturer* camera) {
1208 ASSERT(signaling_thread()->IsCurrent());
1209
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001210 if (!video_channel_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001211 // |video_channel_| doesnt't exist. Probably because the remote end doesnt't
1212 // support video.
1213 LOG(LS_WARNING) << "Video not used in this call.";
1214 return false;
1215 }
1216 if (!video_channel_->SetCapturer(ssrc, camera)) {
1217 // Allow that SetCapturer fail if |camera| is NULL but assert otherwise.
1218 // This in the normal case when the underlying media channel has already
1219 // been deleted.
1220 ASSERT(camera == NULL);
1221 return false;
1222 }
1223 return true;
1224}
1225
1226void WebRtcSession::SetVideoPlayout(uint32 ssrc,
1227 bool enable,
1228 cricket::VideoRenderer* renderer) {
1229 ASSERT(signaling_thread()->IsCurrent());
1230 if (!video_channel_) {
1231 LOG(LS_WARNING) << "SetVideoPlayout: No video channel exists.";
1232 return;
1233 }
1234 if (!video_channel_->SetRenderer(ssrc, enable ? renderer : NULL)) {
1235 // Allow that SetRenderer fail if |renderer| is NULL but assert otherwise.
1236 // This in the normal case when the underlying media channel has already
1237 // been deleted.
1238 ASSERT(renderer == NULL);
1239 }
1240}
1241
1242void WebRtcSession::SetVideoSend(uint32 ssrc, bool enable,
1243 const cricket::VideoOptions* options) {
1244 ASSERT(signaling_thread()->IsCurrent());
1245 if (!video_channel_) {
1246 LOG(LS_WARNING) << "SetVideoSend: No video channel exists.";
1247 return;
1248 }
solenberg1dd98f32015-09-10 01:57:14 -07001249 if (!video_channel_->SetVideoSend(ssrc, !enable, options)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001250 // Allow that MuteStream fail if |enable| is false but assert otherwise.
1251 // This in the normal case when the underlying media channel has already
1252 // been deleted.
1253 ASSERT(enable == false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001254 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001255}
1256
1257bool WebRtcSession::CanInsertDtmf(const std::string& track_id) {
1258 ASSERT(signaling_thread()->IsCurrent());
1259 if (!voice_channel_) {
1260 LOG(LS_ERROR) << "CanInsertDtmf: No audio channel exists.";
1261 return false;
1262 }
1263 uint32 send_ssrc = 0;
1264 // The Dtmf is negotiated per channel not ssrc, so we only check if the ssrc
1265 // exists.
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +00001266 if (!GetAudioSsrcByTrackId(base_local_description(), track_id,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001267 &send_ssrc)) {
1268 LOG(LS_ERROR) << "CanInsertDtmf: Track does not exist: " << track_id;
1269 return false;
1270 }
1271 return voice_channel_->CanInsertDtmf();
1272}
1273
1274bool WebRtcSession::InsertDtmf(const std::string& track_id,
1275 int code, int duration) {
1276 ASSERT(signaling_thread()->IsCurrent());
1277 if (!voice_channel_) {
1278 LOG(LS_ERROR) << "InsertDtmf: No audio channel exists.";
1279 return false;
1280 }
1281 uint32 send_ssrc = 0;
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +00001282 if (!VERIFY(GetAudioSsrcByTrackId(base_local_description(),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001283 track_id, &send_ssrc))) {
1284 LOG(LS_ERROR) << "InsertDtmf: Track does not exist: " << track_id;
1285 return false;
1286 }
1287 if (!voice_channel_->InsertDtmf(send_ssrc, code, duration,
1288 cricket::DF_SEND)) {
1289 LOG(LS_ERROR) << "Failed to insert DTMF to channel.";
1290 return false;
1291 }
1292 return true;
1293}
1294
1295sigslot::signal0<>* WebRtcSession::GetOnDestroyedSignal() {
1296 return &SignalVoiceChannelDestroyed;
1297}
1298
wu@webrtc.org78187522013-10-07 23:32:02 +00001299bool WebRtcSession::SendData(const cricket::SendDataParams& params,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001300 const rtc::Buffer& payload,
wu@webrtc.org78187522013-10-07 23:32:02 +00001301 cricket::SendDataResult* result) {
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001302 if (!data_channel_) {
wu@webrtc.org78187522013-10-07 23:32:02 +00001303 LOG(LS_ERROR) << "SendData called when data_channel_ is NULL.";
1304 return false;
1305 }
1306 return data_channel_->SendData(params, payload, result);
1307}
1308
1309bool WebRtcSession::ConnectDataChannel(DataChannel* webrtc_data_channel) {
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001310 if (!data_channel_) {
wu@webrtc.org78187522013-10-07 23:32:02 +00001311 LOG(LS_ERROR) << "ConnectDataChannel called when data_channel_ is NULL.";
1312 return false;
1313 }
wu@webrtc.org78187522013-10-07 23:32:02 +00001314 data_channel_->SignalReadyToSendData.connect(webrtc_data_channel,
1315 &DataChannel::OnChannelReady);
1316 data_channel_->SignalDataReceived.connect(webrtc_data_channel,
1317 &DataChannel::OnDataReceived);
wu@webrtc.org78187522013-10-07 23:32:02 +00001318 return true;
1319}
1320
1321void WebRtcSession::DisconnectDataChannel(DataChannel* webrtc_data_channel) {
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001322 if (!data_channel_) {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001323 LOG(LS_ERROR) << "DisconnectDataChannel called when data_channel_ is NULL.";
1324 return;
1325 }
wu@webrtc.org78187522013-10-07 23:32:02 +00001326 data_channel_->SignalReadyToSendData.disconnect(webrtc_data_channel);
1327 data_channel_->SignalDataReceived.disconnect(webrtc_data_channel);
1328}
1329
bemasc@webrtc.org9b5467e2014-12-04 23:16:52 +00001330void WebRtcSession::AddSctpDataStream(int sid) {
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001331 if (!data_channel_) {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001332 LOG(LS_ERROR) << "AddDataChannelStreams called when data_channel_ is NULL.";
1333 return;
1334 }
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00001335 data_channel_->AddRecvStream(cricket::StreamParams::CreateLegacy(sid));
1336 data_channel_->AddSendStream(cricket::StreamParams::CreateLegacy(sid));
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001337}
1338
bemasc@webrtc.org9b5467e2014-12-04 23:16:52 +00001339void WebRtcSession::RemoveSctpDataStream(int sid) {
1340 mediastream_signaling_->RemoveSctpDataChannel(sid);
jiayl@webrtc.org2eaac182014-06-17 16:02:46 +00001341
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001342 if (!data_channel_) {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001343 LOG(LS_ERROR) << "RemoveDataChannelStreams called when data_channel_ is "
1344 << "NULL.";
1345 return;
1346 }
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00001347 data_channel_->RemoveRecvStream(sid);
1348 data_channel_->RemoveSendStream(sid);
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001349}
1350
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +00001351bool WebRtcSession::ReadyToSendData() const {
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001352 return data_channel_ && data_channel_->ready_to_send_data();
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +00001353}
1354
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001355rtc::scoped_refptr<DataChannel> WebRtcSession::CreateDataChannel(
wu@webrtc.org78187522013-10-07 23:32:02 +00001356 const std::string& label,
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +00001357 const InternalDataChannelInit* config) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001358 if (state() == STATE_RECEIVEDTERMINATE) {
1359 return NULL;
1360 }
1361 if (data_channel_type_ == cricket::DCT_NONE) {
1362 LOG(LS_ERROR) << "CreateDataChannel: Data is not supported in this call.";
1363 return NULL;
1364 }
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +00001365 InternalDataChannelInit new_config =
1366 config ? (*config) : InternalDataChannelInit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001367 if (data_channel_type_ == cricket::DCT_SCTP) {
1368 if (new_config.id < 0) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001369 rtc::SSLRole role;
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001370 if (GetSslRole(&role) &&
1371 !mediastream_signaling_->AllocateSctpSid(role, &new_config.id)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001372 LOG(LS_ERROR) << "No id can be allocated for the SCTP data channel.";
1373 return NULL;
1374 }
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001375 } else if (!mediastream_signaling_->IsSctpSidAvailable(new_config.id)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001376 LOG(LS_ERROR) << "Failed to create a SCTP data channel "
1377 << "because the id is already in use or out of range.";
1378 return NULL;
1379 }
1380 }
wu@webrtc.org91053e72013-08-10 07:18:04 +00001381
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001382 rtc::scoped_refptr<DataChannel> channel(DataChannel::Create(
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +00001383 this, data_channel_type_, label, new_config));
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001384 if (channel && !mediastream_signaling_->AddDataChannel(channel))
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001385 return NULL;
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001386
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001387 return channel;
1388}
1389
1390cricket::DataChannelType WebRtcSession::data_channel_type() const {
1391 return data_channel_type_;
1392}
1393
wu@webrtc.org91053e72013-08-10 07:18:04 +00001394bool WebRtcSession::IceRestartPending() const {
1395 return ice_restart_latch_->Get();
1396}
1397
1398void WebRtcSession::ResetIceRestartLatch() {
1399 ice_restart_latch_->Reset();
1400}
1401
Henrik Boströmd8281982015-08-27 10:12:24 +02001402void WebRtcSession::OnCertificateReady(
1403 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate) {
1404 SetCertificate(certificate);
wu@webrtc.org91053e72013-08-10 07:18:04 +00001405}
1406
Henrik Boströmd8281982015-08-27 10:12:24 +02001407bool WebRtcSession::waiting_for_certificate_for_testing() const {
Henrik Boström87713d02015-08-25 09:53:21 +02001408 return webrtc_session_desc_factory_->waiting_for_certificate_for_testing();
wu@webrtc.org91053e72013-08-10 07:18:04 +00001409}
1410
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001411void WebRtcSession::SetIceConnectionState(
1412 PeerConnectionInterface::IceConnectionState state) {
1413 if (ice_connection_state_ == state) {
1414 return;
1415 }
1416
1417 // ASSERT that the requested transition is allowed. Note that
1418 // WebRtcSession does not implement "kIceConnectionClosed" (that is handled
1419 // within PeerConnection). This switch statement should compile away when
1420 // ASSERTs are disabled.
1421 switch (ice_connection_state_) {
1422 case PeerConnectionInterface::kIceConnectionNew:
1423 ASSERT(state == PeerConnectionInterface::kIceConnectionChecking);
1424 break;
1425 case PeerConnectionInterface::kIceConnectionChecking:
1426 ASSERT(state == PeerConnectionInterface::kIceConnectionFailed ||
1427 state == PeerConnectionInterface::kIceConnectionConnected);
1428 break;
1429 case PeerConnectionInterface::kIceConnectionConnected:
1430 ASSERT(state == PeerConnectionInterface::kIceConnectionDisconnected ||
1431 state == PeerConnectionInterface::kIceConnectionChecking ||
1432 state == PeerConnectionInterface::kIceConnectionCompleted);
1433 break;
1434 case PeerConnectionInterface::kIceConnectionCompleted:
1435 ASSERT(state == PeerConnectionInterface::kIceConnectionConnected ||
1436 state == PeerConnectionInterface::kIceConnectionDisconnected);
1437 break;
1438 case PeerConnectionInterface::kIceConnectionFailed:
1439 ASSERT(state == PeerConnectionInterface::kIceConnectionNew);
1440 break;
1441 case PeerConnectionInterface::kIceConnectionDisconnected:
1442 ASSERT(state == PeerConnectionInterface::kIceConnectionChecking ||
1443 state == PeerConnectionInterface::kIceConnectionConnected ||
1444 state == PeerConnectionInterface::kIceConnectionCompleted ||
1445 state == PeerConnectionInterface::kIceConnectionFailed);
1446 break;
1447 case PeerConnectionInterface::kIceConnectionClosed:
1448 ASSERT(false);
1449 break;
1450 default:
1451 ASSERT(false);
1452 break;
1453 }
1454
1455 ice_connection_state_ = state;
1456 if (ice_observer_) {
1457 ice_observer_->OnIceConnectionChange(ice_connection_state_);
1458 }
1459}
1460
1461void WebRtcSession::OnTransportRequestSignaling(
1462 cricket::Transport* transport) {
1463 ASSERT(signaling_thread()->IsCurrent());
1464 transport->OnSignalingReady();
1465 if (ice_observer_) {
1466 ice_observer_->OnIceGatheringChange(
1467 PeerConnectionInterface::kIceGatheringGathering);
1468 }
1469}
1470
1471void WebRtcSession::OnTransportConnecting(cricket::Transport* transport) {
1472 ASSERT(signaling_thread()->IsCurrent());
1473 // start monitoring for the write state of the transport.
1474 OnTransportWritable(transport);
1475}
1476
1477void WebRtcSession::OnTransportWritable(cricket::Transport* transport) {
1478 ASSERT(signaling_thread()->IsCurrent());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001479 if (transport->all_channels_writable()) {
henrike@webrtc.org05376342014-03-10 15:53:12 +00001480 SetIceConnectionState(PeerConnectionInterface::kIceConnectionConnected);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001481 } else if (transport->HasChannels()) {
1482 // If the current state is Connected or Completed, then there were writable
1483 // channels but now there are not, so the next state must be Disconnected.
1484 if (ice_connection_state_ ==
1485 PeerConnectionInterface::kIceConnectionConnected ||
1486 ice_connection_state_ ==
1487 PeerConnectionInterface::kIceConnectionCompleted) {
1488 SetIceConnectionState(
1489 PeerConnectionInterface::kIceConnectionDisconnected);
1490 }
1491 }
1492}
1493
mallinath@webrtc.org385857d2014-02-14 00:56:12 +00001494void WebRtcSession::OnTransportCompleted(cricket::Transport* transport) {
1495 ASSERT(signaling_thread()->IsCurrent());
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00001496 PeerConnectionInterface::IceConnectionState old_state = ice_connection_state_;
mallinath@webrtc.org385857d2014-02-14 00:56:12 +00001497 SetIceConnectionState(PeerConnectionInterface::kIceConnectionCompleted);
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00001498 // Only report once when Ice connection is completed.
1499 if (old_state != PeerConnectionInterface::kIceConnectionCompleted) {
jbauchac8869e2015-07-03 01:36:14 -07001500 cricket::TransportStats stats;
1501 if (metrics_observer_ && transport->GetStats(&stats)) {
1502 ReportBestConnectionState(stats);
1503 ReportNegotiatedCiphers(stats);
1504 }
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00001505 }
mallinath@webrtc.org385857d2014-02-14 00:56:12 +00001506}
1507
1508void WebRtcSession::OnTransportFailed(cricket::Transport* transport) {
1509 ASSERT(signaling_thread()->IsCurrent());
1510 SetIceConnectionState(PeerConnectionInterface::kIceConnectionFailed);
1511}
1512
Peter Thatcher54360512015-07-08 11:08:35 -07001513void WebRtcSession::OnTransportReceiving(cricket::Transport* transport) {
1514 ASSERT(signaling_thread()->IsCurrent());
1515 // The ice connection is considered receiving if at least one transport is
1516 // receiving on any channels.
1517 bool receiving = false;
1518 for (const auto& kv : transport_proxies()) {
1519 cricket::Transport* transport = kv.second->impl();
1520 if (transport && transport->any_channel_receiving()) {
1521 receiving = true;
1522 break;
1523 }
1524 }
1525 SetIceConnectionReceiving(receiving);
1526}
1527
1528void WebRtcSession::SetIceConnectionReceiving(bool receiving) {
1529 if (ice_connection_receiving_ == receiving) {
1530 return;
1531 }
1532 ice_connection_receiving_ = receiving;
1533 if (ice_observer_) {
1534 ice_observer_->OnIceConnectionReceivingChange(receiving);
1535 }
1536}
1537
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001538void WebRtcSession::OnTransportProxyCandidatesReady(
1539 cricket::TransportProxy* proxy, const cricket::Candidates& candidates) {
1540 ASSERT(signaling_thread()->IsCurrent());
1541 ProcessNewLocalCandidate(proxy->content_name(), candidates);
1542}
1543
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001544void WebRtcSession::OnCandidatesAllocationDone() {
1545 ASSERT(signaling_thread()->IsCurrent());
1546 if (ice_observer_) {
1547 ice_observer_->OnIceGatheringChange(
1548 PeerConnectionInterface::kIceGatheringComplete);
1549 ice_observer_->OnIceComplete();
1550 }
1551}
1552
1553// Enabling voice and video channel.
1554void WebRtcSession::EnableChannels() {
1555 if (voice_channel_ && !voice_channel_->enabled())
1556 voice_channel_->Enable(true);
1557
1558 if (video_channel_ && !video_channel_->enabled())
1559 video_channel_->Enable(true);
1560
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001561 if (data_channel_ && !data_channel_->enabled())
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001562 data_channel_->Enable(true);
1563}
1564
1565void WebRtcSession::ProcessNewLocalCandidate(
1566 const std::string& content_name,
1567 const cricket::Candidates& candidates) {
1568 int sdp_mline_index;
1569 if (!GetLocalCandidateMediaIndex(content_name, &sdp_mline_index)) {
1570 LOG(LS_ERROR) << "ProcessNewLocalCandidate: content name "
1571 << content_name << " not found";
1572 return;
1573 }
1574
1575 for (cricket::Candidates::const_iterator citer = candidates.begin();
1576 citer != candidates.end(); ++citer) {
1577 // Use content_name as the candidate media id.
1578 JsepIceCandidate candidate(content_name, sdp_mline_index, *citer);
1579 if (ice_observer_) {
1580 ice_observer_->OnIceCandidate(&candidate);
1581 }
1582 if (local_desc_) {
1583 local_desc_->AddCandidate(&candidate);
1584 }
1585 }
1586}
1587
1588// Returns the media index for a local ice candidate given the content name.
1589bool WebRtcSession::GetLocalCandidateMediaIndex(const std::string& content_name,
1590 int* sdp_mline_index) {
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +00001591 if (!base_local_description() || !sdp_mline_index)
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001592 return false;
1593
1594 bool content_found = false;
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +00001595 const ContentInfos& contents = base_local_description()->contents();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001596 for (size_t index = 0; index < contents.size(); ++index) {
1597 if (contents[index].name == content_name) {
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00001598 *sdp_mline_index = static_cast<int>(index);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001599 content_found = true;
1600 break;
1601 }
1602 }
1603 return content_found;
1604}
1605
1606bool WebRtcSession::UseCandidatesInSessionDescription(
1607 const SessionDescriptionInterface* remote_desc) {
1608 if (!remote_desc)
1609 return true;
1610 bool ret = true;
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001611
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001612 for (size_t m = 0; m < remote_desc->number_of_mediasections(); ++m) {
1613 const IceCandidateCollection* candidates = remote_desc->candidates(m);
1614 for (size_t n = 0; n < candidates->count(); ++n) {
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001615 const IceCandidateInterface* candidate = candidates->at(n);
1616 bool valid = false;
1617 if (!ReadyToUseRemoteCandidate(candidate, remote_desc, &valid)) {
1618 if (valid) {
1619 LOG(LS_INFO) << "UseCandidatesInSessionDescription: Candidate saved.";
1620 saved_candidates_.push_back(
1621 new JsepIceCandidate(candidate->sdp_mid(),
1622 candidate->sdp_mline_index(),
1623 candidate->candidate()));
1624 }
1625 continue;
1626 }
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001627 ret = UseCandidate(candidate);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001628 if (!ret)
1629 break;
1630 }
1631 }
1632 return ret;
1633}
1634
1635bool WebRtcSession::UseCandidate(
1636 const IceCandidateInterface* candidate) {
1637
1638 size_t mediacontent_index = static_cast<size_t>(candidate->sdp_mline_index());
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +00001639 size_t remote_content_size = base_remote_description()->contents().size();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001640 if (mediacontent_index >= remote_content_size) {
1641 LOG(LS_ERROR)
1642 << "UseRemoteCandidateInSession: Invalid candidate media index.";
1643 return false;
1644 }
1645
1646 cricket::ContentInfo content =
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +00001647 base_remote_description()->contents()[mediacontent_index];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001648 std::vector<cricket::Candidate> candidates;
1649 candidates.push_back(candidate->candidate());
1650 // Invoking BaseSession method to handle remote candidates.
1651 std::string error;
1652 if (OnRemoteCandidates(content.name, candidates, &error)) {
1653 // Candidates successfully submitted for checking.
1654 if (ice_connection_state_ == PeerConnectionInterface::kIceConnectionNew ||
1655 ice_connection_state_ ==
1656 PeerConnectionInterface::kIceConnectionDisconnected) {
1657 // If state is New, then the session has just gotten its first remote ICE
1658 // candidates, so go to Checking.
1659 // If state is Disconnected, the session is re-using old candidates or
1660 // receiving additional ones, so go to Checking.
1661 // If state is Connected, stay Connected.
1662 // TODO(bemasc): If state is Connected, and the new candidates are for a
1663 // newly added transport, then the state actually _should_ move to
1664 // checking. Add a way to distinguish that case.
1665 SetIceConnectionState(PeerConnectionInterface::kIceConnectionChecking);
1666 }
1667 // TODO(bemasc): If state is Completed, go back to Connected.
1668 } else {
fischman@webrtc.org4f2bd682014-03-28 18:13:34 +00001669 if (!error.empty()) {
1670 LOG(LS_WARNING) << error;
1671 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001672 }
1673 return true;
1674}
1675
1676void WebRtcSession::RemoveUnusedChannelsAndTransports(
1677 const SessionDescription* desc) {
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001678 // Destroy video_channel_ first since it may have a pointer to the
1679 // voice_channel_.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001680 const cricket::ContentInfo* video_info =
1681 cricket::GetFirstVideoContent(desc);
1682 if ((!video_info || video_info->rejected) && video_channel_) {
1683 mediastream_signaling_->OnVideoChannelClose();
1684 SignalVideoChannelDestroyed();
1685 const std::string content_name = video_channel_->content_name();
1686 channel_manager_->DestroyVideoChannel(video_channel_.release());
bjornv@webrtc.org520a69e2015-02-04 12:45:44 +00001687 DestroyTransportProxy(content_name);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001688 }
1689
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001690 const cricket::ContentInfo* voice_info =
1691 cricket::GetFirstAudioContent(desc);
1692 if ((!voice_info || voice_info->rejected) && voice_channel_) {
1693 mediastream_signaling_->OnAudioChannelClose();
1694 SignalVoiceChannelDestroyed();
1695 const std::string content_name = voice_channel_->content_name();
Fredrik Solenberg709ed672015-09-15 12:26:33 +02001696 channel_manager_->DestroyVoiceChannel(voice_channel_.release());
bjornv@webrtc.org520a69e2015-02-04 12:45:44 +00001697 DestroyTransportProxy(content_name);
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001698 }
1699
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001700 const cricket::ContentInfo* data_info =
1701 cricket::GetFirstDataContent(desc);
1702 if ((!data_info || data_info->rejected) && data_channel_) {
1703 mediastream_signaling_->OnDataChannelClose();
1704 SignalDataChannelDestroyed();
1705 const std::string content_name = data_channel_->content_name();
1706 channel_manager_->DestroyDataChannel(data_channel_.release());
bjornv@webrtc.org520a69e2015-02-04 12:45:44 +00001707 DestroyTransportProxy(content_name);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001708 }
1709}
1710
Fredrik Solenberg709ed672015-09-15 12:26:33 +02001711// TODO(mallinath) - Add a correct error code if the channels are not created
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001712// due to BUNDLE is enabled but rtcp-mux is disabled.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001713bool WebRtcSession::CreateChannels(const SessionDescription* desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001714 // Creating the media channels and transport proxies.
1715 const cricket::ContentInfo* voice = cricket::GetFirstAudioContent(desc);
1716 if (voice && !voice->rejected && !voice_channel_) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001717 if (!CreateVoiceChannel(voice)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001718 LOG(LS_ERROR) << "Failed to create voice channel.";
1719 return false;
1720 }
1721 }
1722
1723 const cricket::ContentInfo* video = cricket::GetFirstVideoContent(desc);
1724 if (video && !video->rejected && !video_channel_) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001725 if (!CreateVideoChannel(video)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001726 LOG(LS_ERROR) << "Failed to create video channel.";
1727 return false;
1728 }
1729 }
1730
1731 const cricket::ContentInfo* data = cricket::GetFirstDataContent(desc);
1732 if (data_channel_type_ != cricket::DCT_NONE &&
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001733 data && !data->rejected && !data_channel_) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001734 if (!CreateDataChannel(data)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001735 LOG(LS_ERROR) << "Failed to create data channel.";
1736 return false;
1737 }
1738 }
1739
Peter Thatcheraf55ccc2015-05-21 07:48:41 -07001740 if (rtcp_mux_policy_ == PeerConnectionInterface::kRtcpMuxPolicyRequire) {
1741 if (voice_channel()) {
1742 voice_channel()->ActivateRtcpMux();
1743 }
1744 if (video_channel()) {
1745 video_channel()->ActivateRtcpMux();
1746 }
1747 if (data_channel()) {
1748 data_channel()->ActivateRtcpMux();
1749 }
1750 }
1751
Donald Curtis0e209b02015-03-24 09:29:54 -07001752 // Enable bundle before when kMaxBundle policy is in effect.
1753 if (bundle_policy_ == PeerConnectionInterface::kBundlePolicyMaxBundle) {
Peter Thatcher4eddf182015-04-30 10:55:59 -07001754 const cricket::ContentGroup* bundle_group = desc->GetGroupByName(
1755 cricket::GROUP_TYPE_BUNDLE);
1756 if (!bundle_group) {
Donald Curtis0e209b02015-03-24 09:29:54 -07001757 LOG(LS_WARNING) << "max-bundle specified without BUNDLE specified";
1758 return false;
1759 }
Peter Thatcher4eddf182015-04-30 10:55:59 -07001760 if (!BaseSession::BundleContentGroup(bundle_group)) {
Donald Curtis0e209b02015-03-24 09:29:54 -07001761 LOG(LS_WARNING) << "max-bundle failed to enable bundling.";
1762 return false;
1763 }
1764 }
1765
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001766 return true;
1767}
1768
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001769bool WebRtcSession::CreateVoiceChannel(const cricket::ContentInfo* content) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001770 voice_channel_.reset(channel_manager_->CreateVoiceChannel(
Fredrik Solenberg709ed672015-09-15 12:26:33 +02001771 media_controller_.get(), this, content->name, true, audio_options_));
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001772 if (!voice_channel_) {
wu@webrtc.orgde305012013-10-31 15:40:38 +00001773 return false;
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001774 }
wu@webrtc.orgde305012013-10-31 15:40:38 +00001775
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001776 voice_channel_->SignalDtlsSetupFailure.connect(
1777 this, &WebRtcSession::OnDtlsSetupFailure);
wu@webrtc.orgde305012013-10-31 15:40:38 +00001778 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001779}
1780
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001781bool WebRtcSession::CreateVideoChannel(const cricket::ContentInfo* content) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001782 video_channel_.reset(channel_manager_->CreateVideoChannel(
Fredrik Solenberg709ed672015-09-15 12:26:33 +02001783 media_controller_.get(), this, content->name, true, video_options_));
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001784 if (!video_channel_) {
1785 return false;
1786 }
1787
1788 video_channel_->SignalDtlsSetupFailure.connect(
1789 this, &WebRtcSession::OnDtlsSetupFailure);
1790 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001791}
1792
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001793bool WebRtcSession::CreateDataChannel(const cricket::ContentInfo* content) {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001794 bool sctp = (data_channel_type_ == cricket::DCT_SCTP);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001795 data_channel_.reset(channel_manager_->CreateDataChannel(
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001796 this, content->name, !sctp, data_channel_type_));
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001797 if (!data_channel_) {
wu@webrtc.org91053e72013-08-10 07:18:04 +00001798 return false;
1799 }
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001800
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001801 if (sctp) {
1802 mediastream_signaling_->OnDataTransportCreatedForSctp();
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +00001803 data_channel_->SignalDataReceived.connect(
1804 this, &WebRtcSession::OnDataChannelMessageReceived);
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +00001805 data_channel_->SignalStreamClosedRemotely.connect(
1806 mediastream_signaling_,
1807 &MediaStreamSignaling::OnRemoteSctpDataChannelClosed);
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001808 }
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001809
1810 data_channel_->SignalDtlsSetupFailure.connect(
1811 this, &WebRtcSession::OnDtlsSetupFailure);
wu@webrtc.org91053e72013-08-10 07:18:04 +00001812 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001813}
1814
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001815void WebRtcSession::OnDtlsSetupFailure(cricket::BaseChannel*, bool rtcp) {
1816 SetError(BaseSession::ERROR_TRANSPORT, rtcp ? kDtlsSetupFailureRtcp :
1817 kDtlsSetupFailureRtp);
1818}
1819
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001820void WebRtcSession::CopySavedCandidates(
1821 SessionDescriptionInterface* dest_desc) {
1822 if (!dest_desc) {
1823 ASSERT(false);
1824 return;
1825 }
1826 for (size_t i = 0; i < saved_candidates_.size(); ++i) {
1827 dest_desc->AddCandidate(saved_candidates_[i]);
1828 delete saved_candidates_[i];
1829 }
1830 saved_candidates_.clear();
1831}
1832
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +00001833void WebRtcSession::OnDataChannelMessageReceived(
1834 cricket::DataChannel* channel,
1835 const cricket::ReceiveDataParams& params,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001836 const rtc::Buffer& payload) {
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +00001837 ASSERT(data_channel_type_ == cricket::DCT_SCTP);
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +00001838 if (params.type == cricket::DMT_CONTROL &&
1839 mediastream_signaling_->IsSctpSidAvailable(params.ssrc)) {
1840 // Received CONTROL on unused sid, process as an OPEN message.
1841 mediastream_signaling_->AddDataChannelFromOpenMessage(params, payload);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001842 }
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +00001843 // otherwise ignore the message.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001844}
1845
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001846// Returns false if bundle is enabled and rtcp_mux is disabled.
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001847bool WebRtcSession::ValidateBundleSettings(const SessionDescription* desc) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001848 bool bundle_enabled = desc->HasGroup(cricket::GROUP_TYPE_BUNDLE);
1849 if (!bundle_enabled)
1850 return true;
1851
1852 const cricket::ContentGroup* bundle_group =
1853 desc->GetGroupByName(cricket::GROUP_TYPE_BUNDLE);
1854 ASSERT(bundle_group != NULL);
1855
1856 const cricket::ContentInfos& contents = desc->contents();
1857 for (cricket::ContentInfos::const_iterator citer = contents.begin();
1858 citer != contents.end(); ++citer) {
1859 const cricket::ContentInfo* content = (&*citer);
1860 ASSERT(content != NULL);
1861 if (bundle_group->HasContentName(content->name) &&
1862 !content->rejected && content->type == cricket::NS_JINGLE_RTP) {
1863 if (!HasRtcpMuxEnabled(content))
1864 return false;
1865 }
1866 }
1867 // RTCP-MUX is enabled in all the contents.
1868 return true;
1869}
1870
1871bool WebRtcSession::HasRtcpMuxEnabled(
1872 const cricket::ContentInfo* content) {
1873 const cricket::MediaContentDescription* description =
1874 static_cast<cricket::MediaContentDescription*>(content->description);
1875 return description->rtcp_mux();
1876}
1877
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001878bool WebRtcSession::ValidateSessionDescription(
1879 const SessionDescriptionInterface* sdesc,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001880 cricket::ContentSource source, std::string* err_desc) {
1881 std::string type;
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001882 if (error() != cricket::BaseSession::ERROR_NONE) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001883 return BadSdp(source, type, GetSessionErrorMsg(), err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001884 }
1885
1886 if (!sdesc || !sdesc->description()) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001887 return BadSdp(source, type, kInvalidSdp, err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001888 }
1889
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001890 type = sdesc->type();
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001891 Action action = GetAction(sdesc->type());
1892 if (source == cricket::CS_LOCAL) {
1893 if (!ExpectSetLocalDescription(action))
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001894 return BadLocalSdp(type, BadStateErrMsg(state()), err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001895 } else {
1896 if (!ExpectSetRemoteDescription(action))
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001897 return BadRemoteSdp(type, BadStateErrMsg(state()), err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001898 }
1899
1900 // Verify crypto settings.
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +00001901 std::string crypto_error;
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +00001902 if ((webrtc_session_desc_factory_->SdesPolicy() == cricket::SEC_REQUIRED ||
1903 dtls_enabled_) &&
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +00001904 !VerifyCrypto(sdesc->description(), dtls_enabled_, &crypto_error)) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001905 return BadSdp(source, type, crypto_error, err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001906 }
1907
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001908 // Verify ice-ufrag and ice-pwd.
1909 if (!VerifyIceUfragPwdPresent(sdesc->description())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001910 return BadSdp(source, type, kSdpWithoutIceUfragPwd, err_desc);
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001911 }
1912
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001913 if (!ValidateBundleSettings(sdesc->description())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001914 return BadSdp(source, type, kBundleWithoutRtcpMux, err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001915 }
1916
1917 // Verify m-lines in Answer when compared against Offer.
1918 if (action == kAnswer) {
1919 const cricket::SessionDescription* offer_desc =
1920 (source == cricket::CS_LOCAL) ? remote_description()->description() :
1921 local_description()->description();
1922 if (!VerifyMediaDescriptions(sdesc->description(), offer_desc)) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001923 return BadAnswerSdp(source, kMlineMismatch, err_desc);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001924 }
1925 }
1926
1927 return true;
1928}
1929
1930bool WebRtcSession::ExpectSetLocalDescription(Action action) {
1931 return ((action == kOffer && state() == STATE_INIT) ||
1932 // update local offer
1933 (action == kOffer && state() == STATE_SENTINITIATE) ||
1934 // update the current ongoing session.
1935 (action == kOffer && state() == STATE_RECEIVEDACCEPT) ||
1936 (action == kOffer && state() == STATE_SENTACCEPT) ||
1937 (action == kOffer && state() == STATE_INPROGRESS) ||
1938 // accept remote offer
1939 (action == kAnswer && state() == STATE_RECEIVEDINITIATE) ||
1940 (action == kAnswer && state() == STATE_SENTPRACCEPT) ||
1941 (action == kPrAnswer && state() == STATE_RECEIVEDINITIATE) ||
1942 (action == kPrAnswer && state() == STATE_SENTPRACCEPT));
1943}
1944
1945bool WebRtcSession::ExpectSetRemoteDescription(Action action) {
1946 return ((action == kOffer && state() == STATE_INIT) ||
1947 // update remote offer
1948 (action == kOffer && state() == STATE_RECEIVEDINITIATE) ||
1949 // update the current ongoing session
1950 (action == kOffer && state() == STATE_RECEIVEDACCEPT) ||
1951 (action == kOffer && state() == STATE_SENTACCEPT) ||
1952 (action == kOffer && state() == STATE_INPROGRESS) ||
1953 // accept local offer
1954 (action == kAnswer && state() == STATE_SENTINITIATE) ||
1955 (action == kAnswer && state() == STATE_RECEIVEDPRACCEPT) ||
1956 (action == kPrAnswer && state() == STATE_SENTINITIATE) ||
1957 (action == kPrAnswer && state() == STATE_RECEIVEDPRACCEPT));
1958}
1959
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001960std::string WebRtcSession::GetSessionErrorMsg() {
1961 std::ostringstream desc;
1962 desc << kSessionError << GetErrorCodeString(error()) << ". ";
1963 desc << kSessionErrorDesc << error_desc() << ".";
1964 return desc.str();
1965}
1966
jiayl@webrtc.orge10d28c2014-07-17 17:07:49 +00001967// We need to check the local/remote description for the Transport instead of
1968// the session, because a new Transport added during renegotiation may have
1969// them unset while the session has them set from the previous negotiation.
1970// Not doing so may trigger the auto generation of transport description and
1971// mess up DTLS identity information, ICE credential, etc.
1972bool WebRtcSession::ReadyToUseRemoteCandidate(
1973 const IceCandidateInterface* candidate,
1974 const SessionDescriptionInterface* remote_desc,
1975 bool* valid) {
1976 *valid = true;;
1977 cricket::TransportProxy* transport_proxy = NULL;
1978
1979 const SessionDescriptionInterface* current_remote_desc =
1980 remote_desc ? remote_desc : remote_description();
1981
1982 if (!current_remote_desc)
1983 return false;
1984
1985 size_t mediacontent_index =
1986 static_cast<size_t>(candidate->sdp_mline_index());
1987 size_t remote_content_size =
1988 current_remote_desc->description()->contents().size();
1989 if (mediacontent_index >= remote_content_size) {
1990 LOG(LS_ERROR)
1991 << "ReadyToUseRemoteCandidate: Invalid candidate media index.";
1992
1993 *valid = false;
1994 return false;
1995 }
1996
1997 cricket::ContentInfo content =
1998 current_remote_desc->description()->contents()[mediacontent_index];
1999 transport_proxy = GetTransportProxy(content.name);
2000
2001 return transport_proxy && transport_proxy->local_description_set() &&
2002 transport_proxy->remote_description_set();
2003}
2004
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00002005// Walk through the ConnectionInfos to gather best connection usage
2006// for IPv4 and IPv6.
jbauchac8869e2015-07-03 01:36:14 -07002007void WebRtcSession::ReportBestConnectionState(
2008 const cricket::TransportStats& stats) {
2009 DCHECK(metrics_observer_ != NULL);
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00002010 for (cricket::TransportChannelStatsList::const_iterator it =
2011 stats.channel_stats.begin();
2012 it != stats.channel_stats.end(); ++it) {
2013 for (cricket::ConnectionInfos::const_iterator it_info =
2014 it->connection_infos.begin();
2015 it_info != it->connection_infos.end(); ++it_info) {
2016 if (!it_info->best_connection) {
2017 continue;
2018 }
Guo-wei Shieh3d564c12015-08-19 16:51:15 -07002019
2020 PeerConnectionEnumCounterType type = kPeerConnectionEnumCounterMax;
2021 const cricket::Candidate& local = it_info->local_candidate;
2022 const cricket::Candidate& remote = it_info->remote_candidate;
2023
2024 // Increment the counter for IceCandidatePairType.
2025 if (local.protocol() == cricket::TCP_PROTOCOL_NAME ||
2026 (local.type() == RELAY_PORT_TYPE &&
2027 local.relay_protocol() == cricket::TCP_PROTOCOL_NAME)) {
2028 type = kEnumCounterIceCandidatePairTypeTcp;
2029 } else if (local.protocol() == cricket::UDP_PROTOCOL_NAME) {
2030 type = kEnumCounterIceCandidatePairTypeUdp;
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00002031 } else {
Guo-wei Shieh3d564c12015-08-19 16:51:15 -07002032 CHECK(0);
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00002033 }
Guo-wei Shieh3d564c12015-08-19 16:51:15 -07002034 metrics_observer_->IncrementEnumCounter(
2035 type, GetIceCandidatePairCounter(local, remote),
2036 kIceCandidatePairMax);
2037
2038 // Increment the counter for IP type.
2039 if (local.address().family() == AF_INET) {
Guo-wei Shieh3d564c12015-08-19 16:51:15 -07002040 metrics_observer_->IncrementEnumCounter(
2041 kEnumCounterAddressFamily, kBestConnections_IPv4,
2042 kPeerConnectionAddressFamilyCounter_Max);
2043
2044 } else if (local.address().family() == AF_INET6) {
Guo-wei Shieh3d564c12015-08-19 16:51:15 -07002045 metrics_observer_->IncrementEnumCounter(
2046 kEnumCounterAddressFamily, kBestConnections_IPv6,
2047 kPeerConnectionAddressFamilyCounter_Max);
2048 } else {
2049 CHECK(0);
2050 }
2051
guoweis@webrtc.org7169afd2014-12-04 17:59:29 +00002052 return;
2053 }
2054 }
2055}
2056
jbauchac8869e2015-07-03 01:36:14 -07002057void WebRtcSession::ReportNegotiatedCiphers(
2058 const cricket::TransportStats& stats) {
2059 DCHECK(metrics_observer_ != NULL);
2060 if (!dtls_enabled_ || stats.channel_stats.empty()) {
2061 return;
2062 }
2063
2064 const std::string& srtp_cipher = stats.channel_stats[0].srtp_cipher;
2065 const std::string& ssl_cipher = stats.channel_stats[0].ssl_cipher;
2066 if (srtp_cipher.empty() && ssl_cipher.empty()) {
2067 return;
2068 }
2069
2070 PeerConnectionMetricsName srtp_name;
2071 PeerConnectionMetricsName ssl_name;
2072 if (stats.content_name == cricket::CN_AUDIO) {
2073 srtp_name = kAudioSrtpCipher;
2074 ssl_name = kAudioSslCipher;
2075 } else if (stats.content_name == cricket::CN_VIDEO) {
2076 srtp_name = kVideoSrtpCipher;
2077 ssl_name = kVideoSslCipher;
2078 } else if (stats.content_name == cricket::CN_DATA) {
2079 srtp_name = kDataSrtpCipher;
2080 ssl_name = kDataSslCipher;
2081 } else {
2082 RTC_NOTREACHED();
2083 return;
2084 }
2085
2086 if (!srtp_cipher.empty()) {
2087 metrics_observer_->AddHistogramSample(srtp_name, srtp_cipher);
2088 }
2089 if (!ssl_cipher.empty()) {
2090 metrics_observer_->AddHistogramSample(ssl_name, ssl_cipher);
2091 }
2092}
2093
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002094} // namespace webrtc