henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1 | /* |
kjellander | b24317b | 2016-02-10 07:54:43 -0800 | [diff] [blame] | 2 | * Copyright 2012 The WebRTC project authors. All Rights Reserved. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3 | * |
kjellander | b24317b | 2016-02-10 07:54:43 -0800 | [diff] [blame] | 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 9 | */ |
| 10 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "pc/webrtcsession.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 12 | |
pbos@webrtc.org | 371243d | 2014-03-07 15:22:04 +0000 | [diff] [blame] | 13 | #include <limits.h> |
| 14 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 15 | #include <algorithm> |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 16 | #include <set> |
Tommi | f888bb5 | 2015-12-12 01:37:01 +0100 | [diff] [blame] | 17 | #include <utility> |
| 18 | #include <vector> |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 19 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 20 | #include "api/call/audio_sink.h" |
| 21 | #include "api/jsepicecandidate.h" |
| 22 | #include "api/jsepsessiondescription.h" |
| 23 | #include "api/peerconnectioninterface.h" |
| 24 | #include "call/call.h" |
| 25 | #include "media/base/mediaconstants.h" |
| 26 | #include "media/sctp/sctptransportinternal.h" |
| 27 | #include "p2p/base/portallocator.h" |
| 28 | #include "pc/channel.h" |
| 29 | #include "pc/channelmanager.h" |
| 30 | #include "pc/mediasession.h" |
| 31 | #include "pc/sctputils.h" |
| 32 | #include "pc/webrtcsessiondescriptionfactory.h" |
| 33 | #include "rtc_base/basictypes.h" |
| 34 | #include "rtc_base/bind.h" |
| 35 | #include "rtc_base/checks.h" |
| 36 | #include "rtc_base/helpers.h" |
| 37 | #include "rtc_base/logging.h" |
| 38 | #include "rtc_base/stringencode.h" |
| 39 | #include "rtc_base/stringutils.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 40 | |
| 41 | using cricket::ContentInfo; |
| 42 | using cricket::ContentInfos; |
| 43 | using cricket::MediaContentDescription; |
| 44 | using cricket::SessionDescription; |
| 45 | using cricket::TransportInfo; |
| 46 | |
Guo-wei Shieh | 3d564c1 | 2015-08-19 16:51:15 -0700 | [diff] [blame] | 47 | using cricket::LOCAL_PORT_TYPE; |
| 48 | using cricket::STUN_PORT_TYPE; |
| 49 | using cricket::RELAY_PORT_TYPE; |
| 50 | using cricket::PRFLX_PORT_TYPE; |
| 51 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 52 | namespace webrtc { |
| 53 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 54 | // Error messages |
Steve Anton | 6f25b09 | 2017-10-23 09:39:20 -0700 | [diff] [blame] | 55 | const char kBundleWithoutRtcpMux[] = |
| 56 | "rtcp-mux must be enabled when BUNDLE " |
| 57 | "is enabled."; |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 58 | const char kCreateChannelFailed[] = "Failed to create channels."; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 59 | const char kInvalidCandidates[] = "Description contains invalid candidates."; |
| 60 | const char kInvalidSdp[] = "Invalid session description."; |
Zhi Huang | 2a5e426 | 2017-09-14 01:15:03 -0700 | [diff] [blame] | 61 | const char kMlineMismatchInAnswer[] = |
| 62 | "The order of m-lines in answer doesn't match order in offer. Rejecting " |
| 63 | "answer."; |
| 64 | const char kMlineMismatchInSubsequentOffer[] = |
| 65 | "The order of m-lines in subsequent offer doesn't match order from " |
| 66 | "previous offer/answer."; |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 67 | const char kPushDownTDFailed[] = |
| 68 | "Failed to push down transport description:"; |
henrike@webrtc.org | b90991d | 2014-03-04 19:54:57 +0000 | [diff] [blame] | 69 | const char kSdpWithoutDtlsFingerprint[] = |
| 70 | "Called with SDP without DTLS fingerprint."; |
| 71 | const char kSdpWithoutSdesCrypto[] = |
| 72 | "Called with SDP without SDES crypto."; |
mallinath@webrtc.org | 19f27e6 | 2013-10-13 17:18:27 +0000 | [diff] [blame] | 73 | const char kSdpWithoutIceUfragPwd[] = |
henrike@webrtc.org | b90991d | 2014-03-04 19:54:57 +0000 | [diff] [blame] | 74 | "Called with SDP without ice-ufrag and ice-pwd."; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 75 | const char kSessionError[] = "Session error code: "; |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 76 | const char kSessionErrorDesc[] = "Session error description: "; |
deadbeef | 953c2ce | 2017-01-09 14:53:41 -0800 | [diff] [blame] | 77 | const char kDtlsSrtpSetupFailureRtp[] = |
pthatcher@webrtc.org | 4eeef58 | 2015-03-16 19:34:23 +0000 | [diff] [blame] | 78 | "Couldn't set up DTLS-SRTP on RTP channel."; |
deadbeef | 953c2ce | 2017-01-09 14:53:41 -0800 | [diff] [blame] | 79 | const char kDtlsSrtpSetupFailureRtcp[] = |
pthatcher@webrtc.org | 4eeef58 | 2015-03-16 19:34:23 +0000 | [diff] [blame] | 80 | "Couldn't set up DTLS-SRTP on RTCP channel."; |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 81 | const char kEnableBundleFailed[] = "Failed to enable BUNDLE."; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 82 | |
Guo-wei Shieh | 3d564c1 | 2015-08-19 16:51:15 -0700 | [diff] [blame] | 83 | IceCandidatePairType GetIceCandidatePairCounter( |
| 84 | const cricket::Candidate& local, |
| 85 | const cricket::Candidate& remote) { |
| 86 | const auto& l = local.type(); |
| 87 | const auto& r = remote.type(); |
| 88 | const auto& host = LOCAL_PORT_TYPE; |
| 89 | const auto& srflx = STUN_PORT_TYPE; |
| 90 | const auto& relay = RELAY_PORT_TYPE; |
| 91 | const auto& prflx = PRFLX_PORT_TYPE; |
Guo-wei Shieh | 3cc834a | 2015-09-04 15:52:14 -0700 | [diff] [blame] | 92 | if (l == host && r == host) { |
| 93 | bool local_private = IPIsPrivate(local.address().ipaddr()); |
| 94 | bool remote_private = IPIsPrivate(remote.address().ipaddr()); |
| 95 | if (local_private) { |
| 96 | if (remote_private) { |
| 97 | return kIceCandidatePairHostPrivateHostPrivate; |
| 98 | } else { |
| 99 | return kIceCandidatePairHostPrivateHostPublic; |
| 100 | } |
| 101 | } else { |
| 102 | if (remote_private) { |
| 103 | return kIceCandidatePairHostPublicHostPrivate; |
| 104 | } else { |
| 105 | return kIceCandidatePairHostPublicHostPublic; |
| 106 | } |
| 107 | } |
| 108 | } |
Guo-wei Shieh | 3d564c1 | 2015-08-19 16:51:15 -0700 | [diff] [blame] | 109 | if (l == host && r == srflx) |
| 110 | return kIceCandidatePairHostSrflx; |
| 111 | if (l == host && r == relay) |
| 112 | return kIceCandidatePairHostRelay; |
| 113 | if (l == host && r == prflx) |
| 114 | return kIceCandidatePairHostPrflx; |
| 115 | if (l == srflx && r == host) |
| 116 | return kIceCandidatePairSrflxHost; |
| 117 | if (l == srflx && r == srflx) |
| 118 | return kIceCandidatePairSrflxSrflx; |
| 119 | if (l == srflx && r == relay) |
| 120 | return kIceCandidatePairSrflxRelay; |
| 121 | if (l == srflx && r == prflx) |
| 122 | return kIceCandidatePairSrflxPrflx; |
| 123 | if (l == relay && r == host) |
| 124 | return kIceCandidatePairRelayHost; |
| 125 | if (l == relay && r == srflx) |
| 126 | return kIceCandidatePairRelaySrflx; |
| 127 | if (l == relay && r == relay) |
| 128 | return kIceCandidatePairRelayRelay; |
| 129 | if (l == relay && r == prflx) |
| 130 | return kIceCandidatePairRelayPrflx; |
| 131 | if (l == prflx && r == host) |
| 132 | return kIceCandidatePairPrflxHost; |
| 133 | if (l == prflx && r == srflx) |
| 134 | return kIceCandidatePairPrflxSrflx; |
| 135 | if (l == prflx && r == relay) |
| 136 | return kIceCandidatePairPrflxRelay; |
| 137 | return kIceCandidatePairMax; |
| 138 | } |
| 139 | |
Zhi Huang | b2d355e | 2017-10-26 17:26:37 -0700 | [diff] [blame] | 140 | // Verify that the order of media sections in |new_desc| matches |
| 141 | // |existing_desc|. The number of m= sections in |new_desc| should be no less |
| 142 | // than |existing_desc|. |
| 143 | static bool MediaSectionsInSameOrder(const SessionDescription* existing_desc, |
| 144 | const SessionDescription* new_desc) { |
| 145 | if (!existing_desc || !new_desc) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 146 | return false; |
Zhi Huang | 2a5e426 | 2017-09-14 01:15:03 -0700 | [diff] [blame] | 147 | } |
Zhi Huang | b2d355e | 2017-10-26 17:26:37 -0700 | [diff] [blame] | 148 | |
| 149 | if (existing_desc->contents().size() > new_desc->contents().size()) { |
| 150 | return false; |
| 151 | } |
| 152 | |
| 153 | for (size_t i = 0; i < existing_desc->contents().size(); ++i) { |
| 154 | if (new_desc->contents()[i].name != existing_desc->contents()[i].name) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 155 | return false; |
| 156 | } |
Zhi Huang | b2d355e | 2017-10-26 17:26:37 -0700 | [diff] [blame] | 157 | const MediaContentDescription* new_desc_mdesc = |
wu@webrtc.org | 4e39307 | 2014-04-07 17:04:35 +0000 | [diff] [blame] | 158 | static_cast<const MediaContentDescription*>( |
Zhi Huang | b2d355e | 2017-10-26 17:26:37 -0700 | [diff] [blame] | 159 | new_desc->contents()[i].description); |
| 160 | const MediaContentDescription* existing_desc_mdesc = |
wu@webrtc.org | 4e39307 | 2014-04-07 17:04:35 +0000 | [diff] [blame] | 161 | static_cast<const MediaContentDescription*>( |
Zhi Huang | b2d355e | 2017-10-26 17:26:37 -0700 | [diff] [blame] | 162 | existing_desc->contents()[i].description); |
| 163 | if (new_desc_mdesc->type() != existing_desc_mdesc->type()) { |
wu@webrtc.org | 4e39307 | 2014-04-07 17:04:35 +0000 | [diff] [blame] | 164 | return false; |
| 165 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 166 | } |
| 167 | return true; |
| 168 | } |
| 169 | |
Zhi Huang | 2a5e426 | 2017-09-14 01:15:03 -0700 | [diff] [blame] | 170 | static bool MediaSectionsHaveSameCount(const SessionDescription* desc1, |
| 171 | const SessionDescription* desc2) { |
| 172 | if (!desc1 || !desc2) { |
| 173 | return false; |
| 174 | } |
| 175 | return desc1->contents().size() == desc2->contents().size(); |
| 176 | } |
| 177 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 178 | // Checks that each non-rejected content has SDES crypto keys or a DTLS |
deadbeef | b789253 | 2017-02-22 19:35:18 -0800 | [diff] [blame] | 179 | // fingerprint, unless it's in a BUNDLE group, in which case only the |
| 180 | // BUNDLE-tag section (first media section/description in the BUNDLE group) |
| 181 | // needs a ufrag and pwd. Mismatches, such as replying with a DTLS fingerprint |
| 182 | // to SDES keys, will be caught in JsepTransport negotiation, and backstopped |
| 183 | // by Channel's |srtp_required| check. |
mallinath@webrtc.org | a27be8e | 2013-09-27 23:04:10 +0000 | [diff] [blame] | 184 | static bool VerifyCrypto(const SessionDescription* desc, |
| 185 | bool dtls_enabled, |
| 186 | std::string* error) { |
deadbeef | b789253 | 2017-02-22 19:35:18 -0800 | [diff] [blame] | 187 | const cricket::ContentGroup* bundle = |
| 188 | desc->GetGroupByName(cricket::GROUP_TYPE_BUNDLE); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 189 | const ContentInfos& contents = desc->contents(); |
| 190 | for (size_t index = 0; index < contents.size(); ++index) { |
| 191 | const ContentInfo* cinfo = &contents[index]; |
| 192 | if (cinfo->rejected) { |
| 193 | continue; |
| 194 | } |
deadbeef | b789253 | 2017-02-22 19:35:18 -0800 | [diff] [blame] | 195 | if (bundle && bundle->HasContentName(cinfo->name) && |
| 196 | cinfo->name != *(bundle->FirstContentName())) { |
| 197 | // This isn't the first media section in the BUNDLE group, so it's not |
| 198 | // required to have crypto attributes, since only the crypto attributes |
| 199 | // from the first section actually get used. |
| 200 | continue; |
| 201 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 202 | |
deadbeef | b789253 | 2017-02-22 19:35:18 -0800 | [diff] [blame] | 203 | // If the content isn't rejected or bundled into another m= section, crypto |
| 204 | // must be present. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 205 | const MediaContentDescription* media = |
| 206 | static_cast<const MediaContentDescription*>(cinfo->description); |
| 207 | const TransportInfo* tinfo = desc->GetTransportInfoByName(cinfo->name); |
| 208 | if (!media || !tinfo) { |
| 209 | // Something is not right. |
| 210 | LOG(LS_ERROR) << kInvalidSdp; |
mallinath@webrtc.org | a27be8e | 2013-09-27 23:04:10 +0000 | [diff] [blame] | 211 | *error = kInvalidSdp; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 212 | return false; |
| 213 | } |
henrike@webrtc.org | b90991d | 2014-03-04 19:54:57 +0000 | [diff] [blame] | 214 | if (dtls_enabled) { |
mallinath@webrtc.org | a27be8e | 2013-09-27 23:04:10 +0000 | [diff] [blame] | 215 | if (!tinfo->description.identity_fingerprint) { |
henrike@webrtc.org | b90991d | 2014-03-04 19:54:57 +0000 | [diff] [blame] | 216 | LOG(LS_WARNING) << |
| 217 | "Session description must have DTLS fingerprint if DTLS enabled."; |
| 218 | *error = kSdpWithoutDtlsFingerprint; |
mallinath@webrtc.org | a27be8e | 2013-09-27 23:04:10 +0000 | [diff] [blame] | 219 | return false; |
| 220 | } |
henrike@webrtc.org | b90991d | 2014-03-04 19:54:57 +0000 | [diff] [blame] | 221 | } else { |
| 222 | if (media->cryptos().empty()) { |
mallinath@webrtc.org | a27be8e | 2013-09-27 23:04:10 +0000 | [diff] [blame] | 223 | LOG(LS_WARNING) << |
| 224 | "Session description must have SDES when DTLS disabled."; |
henrike@webrtc.org | b90991d | 2014-03-04 19:54:57 +0000 | [diff] [blame] | 225 | *error = kSdpWithoutSdesCrypto; |
mallinath@webrtc.org | a27be8e | 2013-09-27 23:04:10 +0000 | [diff] [blame] | 226 | return false; |
| 227 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 228 | } |
| 229 | } |
| 230 | |
| 231 | return true; |
| 232 | } |
| 233 | |
deadbeef | b789253 | 2017-02-22 19:35:18 -0800 | [diff] [blame] | 234 | // Checks that each non-rejected content has ice-ufrag and ice-pwd set, unless |
| 235 | // it's in a BUNDLE group, in which case only the BUNDLE-tag section (first |
| 236 | // media section/description in the BUNDLE group) needs a ufrag and pwd. |
mallinath@webrtc.org | 19f27e6 | 2013-10-13 17:18:27 +0000 | [diff] [blame] | 237 | static bool VerifyIceUfragPwdPresent(const SessionDescription* desc) { |
deadbeef | b789253 | 2017-02-22 19:35:18 -0800 | [diff] [blame] | 238 | const cricket::ContentGroup* bundle = |
| 239 | desc->GetGroupByName(cricket::GROUP_TYPE_BUNDLE); |
mallinath@webrtc.org | 19f27e6 | 2013-10-13 17:18:27 +0000 | [diff] [blame] | 240 | const ContentInfos& contents = desc->contents(); |
| 241 | for (size_t index = 0; index < contents.size(); ++index) { |
| 242 | const ContentInfo* cinfo = &contents[index]; |
| 243 | if (cinfo->rejected) { |
| 244 | continue; |
| 245 | } |
deadbeef | b789253 | 2017-02-22 19:35:18 -0800 | [diff] [blame] | 246 | if (bundle && bundle->HasContentName(cinfo->name) && |
| 247 | cinfo->name != *(bundle->FirstContentName())) { |
| 248 | // This isn't the first media section in the BUNDLE group, so it's not |
| 249 | // required to have ufrag/password, since only the ufrag/password from |
| 250 | // the first section actually get used. |
| 251 | continue; |
| 252 | } |
mallinath@webrtc.org | 19f27e6 | 2013-10-13 17:18:27 +0000 | [diff] [blame] | 253 | |
deadbeef | b789253 | 2017-02-22 19:35:18 -0800 | [diff] [blame] | 254 | // If the content isn't rejected or bundled into another m= section, |
| 255 | // ice-ufrag and ice-pwd must be present. |
mallinath@webrtc.org | 19f27e6 | 2013-10-13 17:18:27 +0000 | [diff] [blame] | 256 | const TransportInfo* tinfo = desc->GetTransportInfoByName(cinfo->name); |
| 257 | if (!tinfo) { |
| 258 | // Something is not right. |
| 259 | LOG(LS_ERROR) << kInvalidSdp; |
| 260 | return false; |
| 261 | } |
| 262 | if (tinfo->description.ice_ufrag.empty() || |
| 263 | tinfo->description.ice_pwd.empty()) { |
| 264 | LOG(LS_ERROR) << "Session description must have ice ufrag and pwd."; |
| 265 | return false; |
| 266 | } |
| 267 | } |
| 268 | return true; |
| 269 | } |
| 270 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 271 | static bool GetTrackIdBySsrc(const SessionDescription* session_description, |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 272 | uint32_t ssrc, |
| 273 | std::string* track_id) { |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 274 | RTC_DCHECK(track_id != NULL); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 275 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 276 | const cricket::ContentInfo* audio_info = |
| 277 | cricket::GetFirstAudioContent(session_description); |
jiayl@webrtc.org | e21cc9a | 2014-08-28 22:21:34 +0000 | [diff] [blame] | 278 | if (audio_info) { |
| 279 | const cricket::MediaContentDescription* audio_content = |
| 280 | static_cast<const cricket::MediaContentDescription*>( |
| 281 | audio_info->description); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 282 | |
tommi@webrtc.org | 586f2ed | 2015-01-22 23:00:41 +0000 | [diff] [blame] | 283 | const auto* found = |
| 284 | cricket::GetStreamBySsrc(audio_content->streams(), ssrc); |
| 285 | if (found) { |
| 286 | *track_id = found->id; |
jiayl@webrtc.org | e21cc9a | 2014-08-28 22:21:34 +0000 | [diff] [blame] | 287 | return true; |
| 288 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 289 | } |
| 290 | |
| 291 | const cricket::ContentInfo* video_info = |
| 292 | cricket::GetFirstVideoContent(session_description); |
jiayl@webrtc.org | e21cc9a | 2014-08-28 22:21:34 +0000 | [diff] [blame] | 293 | if (video_info) { |
| 294 | const cricket::MediaContentDescription* video_content = |
| 295 | static_cast<const cricket::MediaContentDescription*>( |
| 296 | video_info->description); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 297 | |
tommi@webrtc.org | 586f2ed | 2015-01-22 23:00:41 +0000 | [diff] [blame] | 298 | const auto* found = |
| 299 | cricket::GetStreamBySsrc(video_content->streams(), ssrc); |
| 300 | if (found) { |
| 301 | *track_id = found->id; |
jiayl@webrtc.org | e21cc9a | 2014-08-28 22:21:34 +0000 | [diff] [blame] | 302 | return true; |
| 303 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 304 | } |
| 305 | return false; |
| 306 | } |
| 307 | |
deadbeef | 953c2ce | 2017-01-09 14:53:41 -0800 | [diff] [blame] | 308 | // Get the SCTP port out of a SessionDescription. |
| 309 | // Return -1 if not found. |
| 310 | static int GetSctpPort(const SessionDescription* session_description) { |
| 311 | const ContentInfo* content_info = GetFirstDataContent(session_description); |
| 312 | RTC_DCHECK(content_info); |
| 313 | if (!content_info) { |
| 314 | return -1; |
| 315 | } |
| 316 | const cricket::DataContentDescription* data = |
| 317 | static_cast<const cricket::DataContentDescription*>( |
| 318 | (content_info->description)); |
| 319 | std::string value; |
| 320 | cricket::DataCodec match_pattern(cricket::kGoogleSctpDataCodecPlType, |
| 321 | cricket::kGoogleSctpDataCodecName); |
| 322 | for (const cricket::DataCodec& codec : data->codecs()) { |
| 323 | if (!codec.Matches(match_pattern)) { |
| 324 | continue; |
| 325 | } |
| 326 | if (codec.GetParam(cricket::kCodecParamPort, &value)) { |
| 327 | return rtc::FromString<int>(value); |
| 328 | } |
| 329 | } |
| 330 | return -1; |
| 331 | } |
| 332 | |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 333 | static bool BadSdp(const std::string& source, |
| 334 | const std::string& type, |
| 335 | const std::string& reason, |
| 336 | std::string* err_desc) { |
| 337 | std::ostringstream desc; |
deadbeef | d59daf8 | 2015-10-14 15:02:44 -0700 | [diff] [blame] | 338 | desc << "Failed to set " << source; |
| 339 | if (!type.empty()) { |
| 340 | desc << " " << type; |
| 341 | } |
Olga Sharonova | f2662f0 | 2017-10-20 09:42:08 +0000 | [diff] [blame] | 342 | desc << " sdp: " << reason; |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 343 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 344 | if (err_desc) { |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 345 | *err_desc = desc.str(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 346 | } |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 347 | LOG(LS_ERROR) << desc.str(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 348 | return false; |
| 349 | } |
| 350 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 351 | static bool BadSdp(cricket::ContentSource source, |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 352 | const std::string& type, |
| 353 | const std::string& reason, |
| 354 | std::string* err_desc) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 355 | if (source == cricket::CS_LOCAL) { |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 356 | return BadSdp("local", type, reason, err_desc); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 357 | } else { |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 358 | return BadSdp("remote", type, reason, err_desc); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 359 | } |
| 360 | } |
| 361 | |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 362 | static bool BadLocalSdp(const std::string& type, |
| 363 | const std::string& reason, |
| 364 | std::string* err_desc) { |
| 365 | return BadSdp(cricket::CS_LOCAL, type, reason, err_desc); |
| 366 | } |
| 367 | |
| 368 | static bool BadRemoteSdp(const std::string& type, |
| 369 | const std::string& reason, |
| 370 | std::string* err_desc) { |
| 371 | return BadSdp(cricket::CS_REMOTE, type, reason, err_desc); |
| 372 | } |
| 373 | |
| 374 | static bool BadOfferSdp(cricket::ContentSource source, |
| 375 | const std::string& reason, |
| 376 | std::string* err_desc) { |
| 377 | return BadSdp(source, SessionDescriptionInterface::kOffer, reason, err_desc); |
| 378 | } |
| 379 | |
| 380 | static bool BadPranswerSdp(cricket::ContentSource source, |
| 381 | const std::string& reason, |
| 382 | std::string* err_desc) { |
| 383 | return BadSdp(source, SessionDescriptionInterface::kPrAnswer, |
| 384 | reason, err_desc); |
| 385 | } |
| 386 | |
| 387 | static bool BadAnswerSdp(cricket::ContentSource source, |
| 388 | const std::string& reason, |
| 389 | std::string* err_desc) { |
| 390 | return BadSdp(source, SessionDescriptionInterface::kAnswer, reason, err_desc); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 391 | } |
| 392 | |
deadbeef | d59daf8 | 2015-10-14 15:02:44 -0700 | [diff] [blame] | 393 | #define GET_STRING_OF_STATE(state) \ |
| 394 | case webrtc::WebRtcSession::state: \ |
| 395 | result = #state; \ |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 396 | break; |
| 397 | |
deadbeef | d59daf8 | 2015-10-14 15:02:44 -0700 | [diff] [blame] | 398 | static std::string GetStateString(webrtc::WebRtcSession::State state) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 399 | std::string result; |
| 400 | switch (state) { |
| 401 | GET_STRING_OF_STATE(STATE_INIT) |
deadbeef | d59daf8 | 2015-10-14 15:02:44 -0700 | [diff] [blame] | 402 | GET_STRING_OF_STATE(STATE_SENTOFFER) |
| 403 | GET_STRING_OF_STATE(STATE_RECEIVEDOFFER) |
| 404 | GET_STRING_OF_STATE(STATE_SENTPRANSWER) |
| 405 | GET_STRING_OF_STATE(STATE_RECEIVEDPRANSWER) |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 406 | GET_STRING_OF_STATE(STATE_INPROGRESS) |
deadbeef | d59daf8 | 2015-10-14 15:02:44 -0700 | [diff] [blame] | 407 | GET_STRING_OF_STATE(STATE_CLOSED) |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 408 | default: |
nisse | c80e741 | 2017-01-11 05:56:46 -0800 | [diff] [blame] | 409 | RTC_NOTREACHED(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 410 | break; |
| 411 | } |
| 412 | return result; |
| 413 | } |
| 414 | |
deadbeef | d59daf8 | 2015-10-14 15:02:44 -0700 | [diff] [blame] | 415 | #define GET_STRING_OF_ERROR_CODE(err) \ |
| 416 | case webrtc::WebRtcSession::err: \ |
| 417 | result = #err; \ |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 418 | break; |
| 419 | |
deadbeef | d59daf8 | 2015-10-14 15:02:44 -0700 | [diff] [blame] | 420 | static std::string GetErrorCodeString(webrtc::WebRtcSession::Error err) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 421 | std::string result; |
| 422 | switch (err) { |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 423 | GET_STRING_OF_ERROR_CODE(ERROR_NONE) |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 424 | GET_STRING_OF_ERROR_CODE(ERROR_CONTENT) |
| 425 | GET_STRING_OF_ERROR_CODE(ERROR_TRANSPORT) |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 426 | default: |
nisse | eb4ca4e | 2017-01-12 02:24:27 -0800 | [diff] [blame] | 427 | RTC_NOTREACHED(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 428 | break; |
| 429 | } |
| 430 | return result; |
| 431 | } |
| 432 | |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 433 | static 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 | |
| 440 | static std::string MakeTdErrorString(const std::string& desc) { |
| 441 | return MakeErrorString(kPushDownTDFailed, desc); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 442 | } |
| 443 | |
deadbeef | 0ed85b2 | 2016-02-23 17:24:52 -0800 | [diff] [blame] | 444 | // Returns true if |new_desc| requests an ICE restart (i.e., new ufrag/pwd). |
| 445 | bool CheckForRemoteIceRestart(const SessionDescriptionInterface* old_desc, |
| 446 | const SessionDescriptionInterface* new_desc, |
| 447 | const std::string& content_name) { |
| 448 | if (!old_desc) { |
honghaiz | 503726c | 2015-07-31 12:37:38 -0700 | [diff] [blame] | 449 | return false; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 450 | } |
deadbeef | 0ed85b2 | 2016-02-23 17:24:52 -0800 | [diff] [blame] | 451 | const SessionDescription* new_sd = new_desc->description(); |
| 452 | const SessionDescription* old_sd = old_desc->description(); |
| 453 | const ContentInfo* cinfo = new_sd->GetContentByName(content_name); |
| 454 | if (!cinfo || cinfo->rejected) { |
| 455 | return false; |
| 456 | } |
| 457 | // If the content isn't rejected, check if ufrag and password has changed. |
| 458 | const cricket::TransportDescription* new_transport_desc = |
| 459 | new_sd->GetTransportDescriptionByName(content_name); |
| 460 | const cricket::TransportDescription* old_transport_desc = |
| 461 | old_sd->GetTransportDescriptionByName(content_name); |
| 462 | if (!new_transport_desc || !old_transport_desc) { |
| 463 | // No transport description exists. This is not an ICE restart. |
| 464 | return false; |
| 465 | } |
| 466 | if (cricket::IceCredentialsChanged( |
| 467 | old_transport_desc->ice_ufrag, old_transport_desc->ice_pwd, |
| 468 | new_transport_desc->ice_ufrag, new_transport_desc->ice_pwd)) { |
| 469 | LOG(LS_INFO) << "Remote peer requests ICE restart for " << content_name |
| 470 | << "."; |
| 471 | return true; |
| 472 | } |
| 473 | return false; |
| 474 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 475 | |
zhihuang | 29ff844 | 2016-07-27 11:07:25 -0700 | [diff] [blame] | 476 | WebRtcSession::WebRtcSession( |
nisse | eaabdf6 | 2017-05-05 02:23:02 -0700 | [diff] [blame] | 477 | Call* call, |
| 478 | cricket::ChannelManager* channel_manager, |
| 479 | const cricket::MediaConfig& media_config, |
| 480 | RtcEventLog* event_log, |
zhihuang | 29ff844 | 2016-07-27 11:07:25 -0700 | [diff] [blame] | 481 | rtc::Thread* network_thread, |
| 482 | rtc::Thread* worker_thread, |
| 483 | rtc::Thread* signaling_thread, |
| 484 | cricket::PortAllocator* port_allocator, |
deadbeef | 953c2ce | 2017-01-09 14:53:41 -0800 | [diff] [blame] | 485 | std::unique_ptr<cricket::TransportController> transport_controller, |
| 486 | std::unique_ptr<cricket::SctpTransportInternalFactory> sctp_factory) |
zhihuang | 9763d56 | 2016-08-05 11:14:50 -0700 | [diff] [blame] | 487 | : network_thread_(network_thread), |
| 488 | worker_thread_(worker_thread), |
danilchap | e9021a3 | 2016-05-17 01:52:02 -0700 | [diff] [blame] | 489 | signaling_thread_(signaling_thread), |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 490 | // RFC 3264: The numeric value of the session id and version in the |
| 491 | // o line MUST be representable with a "64 bit signed integer". |
| 492 | // Due to this constraint session id |sid_| is max limited to LLONG_MAX. |
deadbeef | d59daf8 | 2015-10-14 15:02:44 -0700 | [diff] [blame] | 493 | sid_(rtc::ToString(rtc::CreateRandomId64() & LLONG_MAX)), |
zhihuang | 29ff844 | 2016-07-27 11:07:25 -0700 | [diff] [blame] | 494 | transport_controller_(std::move(transport_controller)), |
deadbeef | 953c2ce | 2017-01-09 14:53:41 -0800 | [diff] [blame] | 495 | sctp_factory_(std::move(sctp_factory)), |
nisse | eaabdf6 | 2017-05-05 02:23:02 -0700 | [diff] [blame] | 496 | media_config_(media_config), |
| 497 | event_log_(event_log), |
| 498 | call_(call), |
| 499 | channel_manager_(channel_manager), |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 500 | ice_observer_(NULL), |
| 501 | ice_connection_state_(PeerConnectionInterface::kIceConnectionNew), |
Peter Thatcher | 5436051 | 2015-07-08 11:08:35 -0700 | [diff] [blame] | 502 | ice_connection_receiving_(true), |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 503 | older_version_remote_peer_(false), |
mallinath@webrtc.org | a27be8e | 2013-09-27 23:04:10 +0000 | [diff] [blame] | 504 | dtls_enabled_(false), |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 505 | data_channel_type_(cricket::DCT_NONE), |
guoweis@webrtc.org | 7169afd | 2014-12-04 17:59:29 +0000 | [diff] [blame] | 506 | metrics_observer_(NULL) { |
deadbeef | d59daf8 | 2015-10-14 15:02:44 -0700 | [diff] [blame] | 507 | transport_controller_->SetIceRole(cricket::ICEROLE_CONTROLLED); |
| 508 | transport_controller_->SignalConnectionState.connect( |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 509 | this, &WebRtcSession::OnTransportControllerConnectionState); |
deadbeef | d59daf8 | 2015-10-14 15:02:44 -0700 | [diff] [blame] | 510 | transport_controller_->SignalReceiving.connect( |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 511 | this, &WebRtcSession::OnTransportControllerReceiving); |
deadbeef | d59daf8 | 2015-10-14 15:02:44 -0700 | [diff] [blame] | 512 | transport_controller_->SignalGatheringState.connect( |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 513 | this, &WebRtcSession::OnTransportControllerGatheringState); |
deadbeef | d59daf8 | 2015-10-14 15:02:44 -0700 | [diff] [blame] | 514 | transport_controller_->SignalCandidatesGathered.connect( |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 515 | this, &WebRtcSession::OnTransportControllerCandidatesGathered); |
Honghai Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 516 | transport_controller_->SignalCandidatesRemoved.connect( |
| 517 | this, &WebRtcSession::OnTransportControllerCandidatesRemoved); |
zhihuang | d82eee0 | 2016-08-26 11:25:05 -0700 | [diff] [blame] | 518 | transport_controller_->SignalDtlsHandshakeError.connect( |
deadbeef | 953c2ce | 2017-01-09 14:53:41 -0800 | [diff] [blame] | 519 | this, &WebRtcSession::OnTransportControllerDtlsHandshakeError); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 520 | } |
| 521 | |
| 522 | WebRtcSession::~WebRtcSession() { |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 523 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
Steve Anton | 169629a | 2017-08-30 17:36:36 -0700 | [diff] [blame] | 524 | // Destroy video channels first since they may have a pointer to a voice |
| 525 | // channel. |
| 526 | for (auto* channel : video_channels_) { |
| 527 | DestroyVideoChannel(channel); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 528 | } |
Steve Anton | 169629a | 2017-08-30 17:36:36 -0700 | [diff] [blame] | 529 | for (auto* channel : voice_channels_) { |
| 530 | DestroyVoiceChannel(channel); |
buildbot@webrtc.org | b4c7b09 | 2014-08-25 12:11:58 +0000 | [diff] [blame] | 531 | } |
deadbeef | 953c2ce | 2017-01-09 14:53:41 -0800 | [diff] [blame] | 532 | if (rtp_data_channel_) { |
zhihuang | f5b251b | 2017-01-12 19:37:48 -0800 | [diff] [blame] | 533 | DestroyDataChannel(); |
deadbeef | 953c2ce | 2017-01-09 14:53:41 -0800 | [diff] [blame] | 534 | } |
| 535 | if (sctp_transport_) { |
| 536 | SignalDataChannelDestroyed(); |
| 537 | network_thread_->Invoke<void>( |
| 538 | RTC_FROM_HERE, rtc::Bind(&WebRtcSession::DestroySctpTransport_n, this)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 539 | } |
deadbeef | d59daf8 | 2015-10-14 15:02:44 -0700 | [diff] [blame] | 540 | |
| 541 | LOG(LS_INFO) << "Session: " << id() << " is destroyed."; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 542 | } |
| 543 | |
Steve Anton | d5585ca | 2017-10-23 14:49:26 -0700 | [diff] [blame] | 544 | void WebRtcSession::Initialize( |
wu@webrtc.org | 97077a3 | 2013-10-25 21:18:33 +0000 | [diff] [blame] | 545 | const PeerConnectionFactoryInterface::Options& options, |
Henrik Boström | d03c23b | 2016-06-01 11:44:18 +0200 | [diff] [blame] | 546 | std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator, |
Steve Anton | d5585ca | 2017-10-23 14:49:26 -0700 | [diff] [blame] | 547 | const PeerConnectionInterface::RTCConfiguration& rtc_configuration, |
| 548 | PeerConnection* pc) { |
Henrik Lundin | 64dad83 | 2015-05-11 12:44:23 +0200 | [diff] [blame] | 549 | bundle_policy_ = rtc_configuration.bundle_policy; |
Peter Thatcher | af55ccc | 2015-05-21 07:48:41 -0700 | [diff] [blame] | 550 | rtcp_mux_policy_ = rtc_configuration.rtcp_mux_policy; |
deadbeef | d59daf8 | 2015-10-14 15:02:44 -0700 | [diff] [blame] | 551 | transport_controller_->SetSslMaxProtocolVersion(options.ssl_max_version); |
pthatcher@webrtc.org | 877ac76 | 2015-02-04 22:03:09 +0000 | [diff] [blame] | 552 | |
Henrik Boström | 87713d0 | 2015-08-25 09:53:21 +0200 | [diff] [blame] | 553 | // Obtain a certificate from RTCConfiguration if any were provided (optional). |
| 554 | rtc::scoped_refptr<rtc::RTCCertificate> certificate; |
| 555 | if (!rtc_configuration.certificates.empty()) { |
| 556 | // TODO(hbos,torbjorng): Decide on certificate-selection strategy instead of |
| 557 | // just picking the first one. The decision should be made based on the DTLS |
| 558 | // handshake. The DTLS negotiations need to know about all certificates. |
| 559 | certificate = rtc_configuration.certificates[0]; |
| 560 | } |
| 561 | |
honghaiz | 1f429e3 | 2015-09-28 07:57:34 -0700 | [diff] [blame] | 562 | SetIceConfig(ParseIceConfig(rtc_configuration)); |
honghaiz | 4edc39c | 2015-09-01 09:53:56 -0700 | [diff] [blame] | 563 | |
henrike@webrtc.org | b90991d | 2014-03-04 19:54:57 +0000 | [diff] [blame] | 564 | if (options.disable_encryption) { |
| 565 | dtls_enabled_ = false; |
| 566 | } else { |
Henrik Boström | 87713d0 | 2015-08-25 09:53:21 +0200 | [diff] [blame] | 567 | // Enable DTLS by default if we have an identity store or a certificate. |
Henrik Boström | d03c23b | 2016-06-01 11:44:18 +0200 | [diff] [blame] | 568 | dtls_enabled_ = (cert_generator || certificate); |
hta | a2a49d9 | 2016-03-04 02:51:39 -0800 | [diff] [blame] | 569 | // |rtc_configuration| can override the default |dtls_enabled_| value. |
| 570 | if (rtc_configuration.enable_dtls_srtp) { |
| 571 | dtls_enabled_ = *(rtc_configuration.enable_dtls_srtp); |
henrike@webrtc.org | b90991d | 2014-03-04 19:54:57 +0000 | [diff] [blame] | 572 | } |
sergeyu@chromium.org | a59696b | 2013-09-13 23:48:58 +0000 | [diff] [blame] | 573 | } |
| 574 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 575 | // Enable creation of RTP data channels if the kEnableRtpDataChannels is set. |
wu@webrtc.org | 97077a3 | 2013-10-25 21:18:33 +0000 | [diff] [blame] | 576 | // It takes precendence over the disable_sctp_data_channels |
| 577 | // PeerConnectionFactoryInterface::Options. |
hta | a2a49d9 | 2016-03-04 02:51:39 -0800 | [diff] [blame] | 578 | if (rtc_configuration.enable_rtp_data_channel) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 579 | data_channel_type_ = cricket::DCT_RTP; |
Steve Anton | c4faa9c | 2017-10-23 14:44:03 -0700 | [diff] [blame] | 580 | } else { |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 581 | // DTLS has to be enabled to use SCTP. |
wu@webrtc.org | 97077a3 | 2013-10-25 21:18:33 +0000 | [diff] [blame] | 582 | if (!options.disable_sctp_data_channels && dtls_enabled_) { |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 583 | data_channel_type_ = cricket::DCT_SCTP; |
| 584 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 585 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 586 | |
hta | a2a49d9 | 2016-03-04 02:51:39 -0800 | [diff] [blame] | 587 | video_options_.screencast_min_bitrate_kbps = |
| 588 | rtc_configuration.screencast_min_bitrate; |
| 589 | audio_options_.combined_audio_video_bwe = |
| 590 | rtc_configuration.combined_audio_video_bwe; |
buildbot@webrtc.org | b4c7b09 | 2014-08-25 12:11:58 +0000 | [diff] [blame] | 591 | |
kwiberg | 102c6a6 | 2015-10-30 02:47:38 -0700 | [diff] [blame] | 592 | audio_options_.audio_jitter_buffer_max_packets = |
Karl Wiberg | be57983 | 2015-11-10 22:34:18 +0100 | [diff] [blame] | 593 | rtc::Optional<int>(rtc_configuration.audio_jitter_buffer_max_packets); |
Henrik Lundin | 64dad83 | 2015-05-11 12:44:23 +0200 | [diff] [blame] | 594 | |
Karl Wiberg | be57983 | 2015-11-10 22:34:18 +0100 | [diff] [blame] | 595 | audio_options_.audio_jitter_buffer_fast_accelerate = rtc::Optional<bool>( |
| 596 | rtc_configuration.audio_jitter_buffer_fast_accelerate); |
Henrik Lundin | 5263b3c | 2015-06-01 10:29:41 +0200 | [diff] [blame] | 597 | |
Steve Anton | d5585ca | 2017-10-23 14:49:26 -0700 | [diff] [blame] | 598 | // Whether the certificate generator/certificate is null or not determines |
| 599 | // what WebRtcSessionDescriptionFactory will do, so make sure that we give it |
| 600 | // the right instructions by clearing the variables if needed. |
Henrik Boström | 87713d0 | 2015-08-25 09:53:21 +0200 | [diff] [blame] | 601 | if (!dtls_enabled_) { |
Steve Anton | d5585ca | 2017-10-23 14:49:26 -0700 | [diff] [blame] | 602 | cert_generator.reset(); |
| 603 | certificate = nullptr; |
| 604 | } else if (certificate) { |
| 605 | // Favor generated certificate over the certificate generator. |
| 606 | cert_generator.reset(); |
Henrik Boström | 87713d0 | 2015-08-25 09:53:21 +0200 | [diff] [blame] | 607 | } |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 608 | |
Steve Anton | d5585ca | 2017-10-23 14:49:26 -0700 | [diff] [blame] | 609 | webrtc_session_desc_factory_.reset(new WebRtcSessionDescriptionFactory( |
| 610 | signaling_thread(), channel_manager_, pc, id(), std::move(cert_generator), |
| 611 | certificate)); |
Henrik Boström | d828198 | 2015-08-27 10:12:24 +0200 | [diff] [blame] | 612 | webrtc_session_desc_factory_->SignalCertificateReady.connect( |
| 613 | this, &WebRtcSession::OnCertificateReady); |
mallinath@webrtc.org | 7e809c3 | 2013-09-30 18:59:08 +0000 | [diff] [blame] | 614 | |
wu@webrtc.org | 97077a3 | 2013-10-25 21:18:33 +0000 | [diff] [blame] | 615 | if (options.disable_encryption) { |
henrike@webrtc.org | b90991d | 2014-03-04 19:54:57 +0000 | [diff] [blame] | 616 | webrtc_session_desc_factory_->SetSdesPolicy(cricket::SEC_DISABLED); |
mallinath@webrtc.org | 7e809c3 | 2013-09-30 18:59:08 +0000 | [diff] [blame] | 617 | } |
Guo-wei Shieh | fe3bc9d | 2015-08-20 08:48:20 -0700 | [diff] [blame] | 618 | |
jbauch | 5869f50 | 2017-06-29 12:31:36 -0700 | [diff] [blame] | 619 | webrtc_session_desc_factory_->set_enable_encrypted_rtp_header_extensions( |
| 620 | options.crypto_options.enable_encrypted_rtp_header_extensions); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 621 | } |
| 622 | |
deadbeef | d59daf8 | 2015-10-14 15:02:44 -0700 | [diff] [blame] | 623 | void WebRtcSession::Close() { |
| 624 | SetState(STATE_CLOSED); |
| 625 | RemoveUnusedChannels(nullptr); |
stefan | f79ade1 | 2017-06-02 06:44:03 -0700 | [diff] [blame] | 626 | call_ = nullptr; |
Steve Anton | 169629a | 2017-08-30 17:36:36 -0700 | [diff] [blame] | 627 | RTC_DCHECK(voice_channels_.empty()); |
| 628 | RTC_DCHECK(video_channels_.empty()); |
deadbeef | 953c2ce | 2017-01-09 14:53:41 -0800 | [diff] [blame] | 629 | RTC_DCHECK(!rtp_data_channel_); |
| 630 | RTC_DCHECK(!sctp_transport_); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 631 | } |
| 632 | |
deadbeef | 0ed85b2 | 2016-02-23 17:24:52 -0800 | [diff] [blame] | 633 | cricket::BaseChannel* WebRtcSession::GetChannel( |
| 634 | const std::string& content_name) { |
| 635 | if (voice_channel() && voice_channel()->content_name() == content_name) { |
| 636 | return voice_channel(); |
| 637 | } |
| 638 | if (video_channel() && video_channel()->content_name() == content_name) { |
| 639 | return video_channel(); |
| 640 | } |
deadbeef | 953c2ce | 2017-01-09 14:53:41 -0800 | [diff] [blame] | 641 | if (rtp_data_channel() && |
| 642 | rtp_data_channel()->content_name() == content_name) { |
| 643 | return rtp_data_channel(); |
deadbeef | 0ed85b2 | 2016-02-23 17:24:52 -0800 | [diff] [blame] | 644 | } |
| 645 | return nullptr; |
| 646 | } |
| 647 | |
deadbeef | 953c2ce | 2017-01-09 14:53:41 -0800 | [diff] [blame] | 648 | bool WebRtcSession::GetSctpSslRole(rtc::SSLRole* role) { |
| 649 | if (!local_description() || !remote_description()) { |
| 650 | LOG(LS_INFO) << "Local and Remote descriptions must be applied to get the " |
| 651 | << "SSL Role of the SCTP transport."; |
| 652 | return false; |
| 653 | } |
| 654 | if (!sctp_transport_) { |
| 655 | LOG(LS_INFO) << "Non-rejected SCTP m= section is needed to get the " |
| 656 | << "SSL Role of the SCTP transport."; |
| 657 | return false; |
| 658 | } |
| 659 | |
| 660 | return transport_controller_->GetSslRole(*sctp_transport_name_, role); |
| 661 | } |
| 662 | |
| 663 | bool WebRtcSession::GetSslRole(const std::string& content_name, |
Taylor Brandstetter | f475d36 | 2016-01-08 15:35:57 -0800 | [diff] [blame] | 664 | rtc::SSLRole* role) { |
deadbeef | fe4a8a4 | 2016-12-20 17:56:17 -0800 | [diff] [blame] | 665 | if (!local_description() || !remote_description()) { |
deadbeef | 953c2ce | 2017-01-09 14:53:41 -0800 | [diff] [blame] | 666 | LOG(LS_INFO) << "Local and Remote descriptions must be applied to get the " |
sergeyu@chromium.org | 0be6aa0 | 2013-08-23 23:21:25 +0000 | [diff] [blame] | 667 | << "SSL Role of the session."; |
| 668 | return false; |
| 669 | } |
| 670 | |
deadbeef | 953c2ce | 2017-01-09 14:53:41 -0800 | [diff] [blame] | 671 | return transport_controller_->GetSslRole(GetTransportName(content_name), |
| 672 | role); |
sergeyu@chromium.org | 0be6aa0 | 2013-08-23 23:21:25 +0000 | [diff] [blame] | 673 | } |
| 674 | |
jiayl@webrtc.org | b18bf5e | 2014-08-04 18:34:16 +0000 | [diff] [blame] | 675 | void WebRtcSession::CreateOffer( |
| 676 | CreateSessionDescriptionObserver* observer, |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 677 | const PeerConnectionInterface::RTCOfferAnswerOptions& options, |
| 678 | const cricket::MediaSessionOptions& session_options) { |
| 679 | webrtc_session_desc_factory_->CreateOffer(observer, options, session_options); |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 680 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 681 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 682 | void WebRtcSession::CreateAnswer( |
| 683 | CreateSessionDescriptionObserver* observer, |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 684 | const cricket::MediaSessionOptions& session_options) { |
hta | a2a49d9 | 2016-03-04 02:51:39 -0800 | [diff] [blame] | 685 | webrtc_session_desc_factory_->CreateAnswer(observer, session_options); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 686 | } |
| 687 | |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 688 | bool WebRtcSession::SetLocalDescription( |
| 689 | std::unique_ptr<SessionDescriptionInterface> desc, |
| 690 | std::string* err_desc) { |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 691 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 692 | |
sergeyu@chromium.org | 0be6aa0 | 2013-08-23 23:21:25 +0000 | [diff] [blame] | 693 | // Validate SDP. |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 694 | if (!ValidateSessionDescription(desc.get(), cricket::CS_LOCAL, err_desc)) { |
sergeyu@chromium.org | 0be6aa0 | 2013-08-23 23:21:25 +0000 | [diff] [blame] | 695 | return false; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 696 | } |
| 697 | |
deadbeef | d59daf8 | 2015-10-14 15:02:44 -0700 | [diff] [blame] | 698 | // Update the initial_offerer flag if this session is the initial_offerer. |
sergeyu@chromium.org | 0be6aa0 | 2013-08-23 23:21:25 +0000 | [diff] [blame] | 699 | Action action = GetAction(desc->type()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 700 | if (state() == STATE_INIT && action == kOffer) { |
deadbeef | d59daf8 | 2015-10-14 15:02:44 -0700 | [diff] [blame] | 701 | initial_offerer_ = true; |
| 702 | transport_controller_->SetIceRole(cricket::ICEROLE_CONTROLLING); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 703 | } |
| 704 | |
deadbeef | fe4a8a4 | 2016-12-20 17:56:17 -0800 | [diff] [blame] | 705 | if (action == kAnswer) { |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 706 | current_local_description_ = std::move(desc); |
| 707 | pending_local_description_ = nullptr; |
| 708 | current_remote_description_ = std::move(pending_remote_description_); |
deadbeef | fe4a8a4 | 2016-12-20 17:56:17 -0800 | [diff] [blame] | 709 | } else { |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 710 | pending_local_description_ = std::move(desc); |
deadbeef | fe4a8a4 | 2016-12-20 17:56:17 -0800 | [diff] [blame] | 711 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 712 | |
| 713 | // Transport and Media channels will be created only when offer is set. |
deadbeef | fe4a8a4 | 2016-12-20 17:56:17 -0800 | [diff] [blame] | 714 | if (action == kOffer && !CreateChannels(local_description()->description())) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 715 | // TODO(mallinath) - Handle CreateChannel failure, as new local description |
| 716 | // is applied. Restore back to old description. |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 717 | return BadLocalSdp(local_description()->type(), kCreateChannelFailed, |
| 718 | err_desc); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 719 | } |
| 720 | |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 721 | // Remove unused channels if MediaContentDescription is rejected. |
deadbeef | fe4a8a4 | 2016-12-20 17:56:17 -0800 | [diff] [blame] | 722 | RemoveUnusedChannels(local_description()->description()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 723 | |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 724 | if (!UpdateSessionState(action, cricket::CS_LOCAL, err_desc)) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 725 | return false; |
| 726 | } |
deadbeef | fe4a8a4 | 2016-12-20 17:56:17 -0800 | [diff] [blame] | 727 | if (remote_description()) { |
deadbeef | d59daf8 | 2015-10-14 15:02:44 -0700 | [diff] [blame] | 728 | // Now that we have a local description, we can push down remote candidates. |
deadbeef | fe4a8a4 | 2016-12-20 17:56:17 -0800 | [diff] [blame] | 729 | UseCandidatesInSessionDescription(remote_description()); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 730 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 731 | |
deadbeef | 0ed85b2 | 2016-02-23 17:24:52 -0800 | [diff] [blame] | 732 | pending_ice_restarts_.clear(); |
deadbeef | d59daf8 | 2015-10-14 15:02:44 -0700 | [diff] [blame] | 733 | if (error() != ERROR_NONE) { |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 734 | return BadLocalSdp(local_description()->type(), GetSessionErrorMsg(), |
| 735 | err_desc); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 736 | } |
| 737 | return true; |
| 738 | } |
| 739 | |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 740 | bool WebRtcSession::SetRemoteDescription( |
| 741 | std::unique_ptr<SessionDescriptionInterface> desc, |
| 742 | std::string* err_desc) { |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 743 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 744 | |
sergeyu@chromium.org | 0be6aa0 | 2013-08-23 23:21:25 +0000 | [diff] [blame] | 745 | // Validate SDP. |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 746 | if (!ValidateSessionDescription(desc.get(), cricket::CS_REMOTE, err_desc)) { |
sergeyu@chromium.org | 0be6aa0 | 2013-08-23 23:21:25 +0000 | [diff] [blame] | 747 | return false; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 748 | } |
| 749 | |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 750 | // Hold this pointer so candidates can be copied to it later in the method. |
| 751 | SessionDescriptionInterface* desc_ptr = desc.get(); |
| 752 | |
deadbeef | fe4a8a4 | 2016-12-20 17:56:17 -0800 | [diff] [blame] | 753 | const SessionDescriptionInterface* old_remote_description = |
| 754 | remote_description(); |
| 755 | // Grab ownership of the description being replaced for the remainder of this |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 756 | // method, since it's used below as |old_remote_description|. |
deadbeef | fe4a8a4 | 2016-12-20 17:56:17 -0800 | [diff] [blame] | 757 | std::unique_ptr<SessionDescriptionInterface> replaced_remote_description; |
| 758 | Action action = GetAction(desc->type()); |
| 759 | if (action == kAnswer) { |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 760 | replaced_remote_description = pending_remote_description_ |
| 761 | ? std::move(pending_remote_description_) |
| 762 | : std::move(current_remote_description_); |
| 763 | current_remote_description_ = std::move(desc); |
| 764 | pending_remote_description_ = nullptr; |
| 765 | current_local_description_ = std::move(pending_local_description_); |
deadbeef | fe4a8a4 | 2016-12-20 17:56:17 -0800 | [diff] [blame] | 766 | } else { |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 767 | replaced_remote_description = std::move(pending_remote_description_); |
| 768 | pending_remote_description_ = std::move(desc); |
deadbeef | fe4a8a4 | 2016-12-20 17:56:17 -0800 | [diff] [blame] | 769 | } |
deadbeef | d59daf8 | 2015-10-14 15:02:44 -0700 | [diff] [blame] | 770 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 771 | // Transport and Media channels will be created only when offer is set. |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 772 | if (action == kOffer && |
| 773 | !CreateChannels(remote_description()->description())) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 774 | // TODO(mallinath) - Handle CreateChannel failure, as new local description |
| 775 | // is applied. Restore back to old description. |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 776 | return BadRemoteSdp(remote_description()->type(), kCreateChannelFailed, |
| 777 | err_desc); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 778 | } |
| 779 | |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 780 | // Remove unused channels if MediaContentDescription is rejected. |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 781 | RemoveUnusedChannels(remote_description()->description()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 782 | |
| 783 | // NOTE: Candidates allocation will be initiated only when SetLocalDescription |
| 784 | // is called. |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 785 | if (!UpdateSessionState(action, cricket::CS_REMOTE, err_desc)) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 786 | return false; |
| 787 | } |
| 788 | |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 789 | if (local_description() && |
| 790 | !UseCandidatesInSessionDescription(remote_description())) { |
| 791 | return BadRemoteSdp(remote_description()->type(), kInvalidCandidates, |
| 792 | err_desc); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 793 | } |
| 794 | |
deadbeef | fe4a8a4 | 2016-12-20 17:56:17 -0800 | [diff] [blame] | 795 | if (old_remote_description) { |
deadbeef | 0ed85b2 | 2016-02-23 17:24:52 -0800 | [diff] [blame] | 796 | for (const cricket::ContentInfo& content : |
deadbeef | fe4a8a4 | 2016-12-20 17:56:17 -0800 | [diff] [blame] | 797 | old_remote_description->description()->contents()) { |
deadbeef | 0ed85b2 | 2016-02-23 17:24:52 -0800 | [diff] [blame] | 798 | // Check if this new SessionDescription contains new ICE ufrag and |
| 799 | // password that indicates the remote peer requests an ICE restart. |
| 800 | // TODO(deadbeef): When we start storing both the current and pending |
| 801 | // remote description, this should reset pending_ice_restarts and compare |
| 802 | // against the current description. |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 803 | if (CheckForRemoteIceRestart(old_remote_description, remote_description(), |
deadbeef | fe4a8a4 | 2016-12-20 17:56:17 -0800 | [diff] [blame] | 804 | content.name)) { |
deadbeef | 0ed85b2 | 2016-02-23 17:24:52 -0800 | [diff] [blame] | 805 | if (action == kOffer) { |
| 806 | pending_ice_restarts_.insert(content.name); |
| 807 | } |
| 808 | } else { |
| 809 | // We retain all received candidates only if ICE is not restarted. |
| 810 | // When ICE is restarted, all previous candidates belong to an old |
| 811 | // generation and should not be kept. |
| 812 | // TODO(deadbeef): This goes against the W3C spec which says the remote |
| 813 | // description should only contain candidates from the last set remote |
| 814 | // description plus any candidates added since then. We should remove |
| 815 | // this once we're sure it won't break anything. |
| 816 | WebRtcSessionDescriptionFactory::CopyCandidatesFromSessionDescription( |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 817 | old_remote_description, content.name, desc_ptr); |
deadbeef | 0ed85b2 | 2016-02-23 17:24:52 -0800 | [diff] [blame] | 818 | } |
| 819 | } |
honghaiz | 503726c | 2015-07-31 12:37:38 -0700 | [diff] [blame] | 820 | } |
| 821 | |
deadbeef | d59daf8 | 2015-10-14 15:02:44 -0700 | [diff] [blame] | 822 | if (error() != ERROR_NONE) { |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 823 | return BadRemoteSdp(remote_description()->type(), GetSessionErrorMsg(), |
| 824 | err_desc); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 825 | } |
jiayl@webrtc.org | dacdd94 | 2015-01-23 17:33:34 +0000 | [diff] [blame] | 826 | |
| 827 | // Set the the ICE connection state to connecting since the connection may |
| 828 | // become writable with peer reflexive candidates before any remote candidate |
| 829 | // is signaled. |
| 830 | // TODO(pthatcher): This is a short-term solution for crbug/446908. A real fix |
| 831 | // is to have a new signal the indicates a change in checking state from the |
| 832 | // transport and expose a new checking() member from transport that can be |
| 833 | // read to determine the current checking state. The existing SignalConnecting |
| 834 | // actually means "gathering candidates", so cannot be be used here. |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 835 | if (remote_description()->type() != SessionDescriptionInterface::kOffer && |
jiayl@webrtc.org | dacdd94 | 2015-01-23 17:33:34 +0000 | [diff] [blame] | 836 | ice_connection_state_ == PeerConnectionInterface::kIceConnectionNew) { |
| 837 | SetIceConnectionState(PeerConnectionInterface::kIceConnectionChecking); |
| 838 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 839 | return true; |
| 840 | } |
| 841 | |
Steve Anton | 18ee1d5 | 2017-09-11 11:32:35 -0700 | [diff] [blame] | 842 | // TODO(steveanton): Eventually it'd be nice to store the channels as a single |
| 843 | // vector of BaseChannel pointers instead of separate voice and video channel |
| 844 | // vectors. At that point, this will become a simple getter. |
| 845 | std::vector<cricket::BaseChannel*> WebRtcSession::Channels() const { |
| 846 | std::vector<cricket::BaseChannel*> channels; |
| 847 | channels.insert(channels.end(), voice_channels_.begin(), |
| 848 | voice_channels_.end()); |
| 849 | channels.insert(channels.end(), video_channels_.begin(), |
| 850 | video_channels_.end()); |
| 851 | if (rtp_data_channel_) { |
| 852 | channels.push_back(rtp_data_channel_.get()); |
| 853 | } |
| 854 | return channels; |
| 855 | } |
| 856 | |
deadbeef | d59daf8 | 2015-10-14 15:02:44 -0700 | [diff] [blame] | 857 | void WebRtcSession::LogState(State old_state, State new_state) { |
| 858 | LOG(LS_INFO) << "Session:" << id() |
| 859 | << " Old state:" << GetStateString(old_state) |
| 860 | << " New state:" << GetStateString(new_state); |
| 861 | } |
| 862 | |
| 863 | void WebRtcSession::SetState(State state) { |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 864 | RTC_DCHECK(signaling_thread_->IsCurrent()); |
deadbeef | d59daf8 | 2015-10-14 15:02:44 -0700 | [diff] [blame] | 865 | if (state != state_) { |
| 866 | LogState(state_, state); |
| 867 | state_ = state; |
| 868 | SignalState(this, state_); |
| 869 | } |
| 870 | } |
| 871 | |
| 872 | void WebRtcSession::SetError(Error error, const std::string& error_desc) { |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 873 | RTC_DCHECK(signaling_thread_->IsCurrent()); |
deadbeef | d59daf8 | 2015-10-14 15:02:44 -0700 | [diff] [blame] | 874 | if (error != error_) { |
| 875 | error_ = error; |
| 876 | error_desc_ = error_desc; |
| 877 | } |
| 878 | } |
| 879 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 880 | bool WebRtcSession::UpdateSessionState( |
| 881 | Action action, cricket::ContentSource source, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 882 | std::string* err_desc) { |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 883 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 884 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 885 | // If there's already a pending error then no state transition should happen. |
| 886 | // But all call-sites should be verifying this before calling us! |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 887 | RTC_DCHECK(error() == ERROR_NONE); |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 888 | std::string td_err; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 889 | if (action == kOffer) { |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 890 | if (!PushdownTransportDescription(source, cricket::CA_OFFER, &td_err)) { |
| 891 | return BadOfferSdp(source, MakeTdErrorString(td_err), err_desc); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 892 | } |
deadbeef | d59daf8 | 2015-10-14 15:02:44 -0700 | [diff] [blame] | 893 | SetState(source == cricket::CS_LOCAL ? STATE_SENTOFFER |
| 894 | : STATE_RECEIVEDOFFER); |
pthatcher@webrtc.org | 592470b | 2015-03-16 21:15:37 +0000 | [diff] [blame] | 895 | if (!PushdownMediaDescription(cricket::CA_OFFER, source, err_desc)) { |
deadbeef | d59daf8 | 2015-10-14 15:02:44 -0700 | [diff] [blame] | 896 | SetError(ERROR_CONTENT, *err_desc); |
pthatcher@webrtc.org | 592470b | 2015-03-16 21:15:37 +0000 | [diff] [blame] | 897 | } |
deadbeef | d59daf8 | 2015-10-14 15:02:44 -0700 | [diff] [blame] | 898 | if (error() != ERROR_NONE) { |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 899 | return BadOfferSdp(source, GetSessionErrorMsg(), err_desc); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 900 | } |
| 901 | } else if (action == kPrAnswer) { |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 902 | if (!PushdownTransportDescription(source, cricket::CA_PRANSWER, &td_err)) { |
| 903 | return BadPranswerSdp(source, MakeTdErrorString(td_err), err_desc); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 904 | } |
| 905 | EnableChannels(); |
deadbeef | d59daf8 | 2015-10-14 15:02:44 -0700 | [diff] [blame] | 906 | SetState(source == cricket::CS_LOCAL ? STATE_SENTPRANSWER |
| 907 | : STATE_RECEIVEDPRANSWER); |
pthatcher@webrtc.org | 592470b | 2015-03-16 21:15:37 +0000 | [diff] [blame] | 908 | if (!PushdownMediaDescription(cricket::CA_PRANSWER, source, err_desc)) { |
deadbeef | d59daf8 | 2015-10-14 15:02:44 -0700 | [diff] [blame] | 909 | SetError(ERROR_CONTENT, *err_desc); |
pthatcher@webrtc.org | 592470b | 2015-03-16 21:15:37 +0000 | [diff] [blame] | 910 | } |
deadbeef | d59daf8 | 2015-10-14 15:02:44 -0700 | [diff] [blame] | 911 | if (error() != ERROR_NONE) { |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 912 | return BadPranswerSdp(source, GetSessionErrorMsg(), err_desc); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 913 | } |
| 914 | } else if (action == kAnswer) { |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 915 | const cricket::ContentGroup* local_bundle = |
deadbeef | fe4a8a4 | 2016-12-20 17:56:17 -0800 | [diff] [blame] | 916 | local_description()->description()->GetGroupByName( |
| 917 | cricket::GROUP_TYPE_BUNDLE); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 918 | const cricket::ContentGroup* remote_bundle = |
deadbeef | fe4a8a4 | 2016-12-20 17:56:17 -0800 | [diff] [blame] | 919 | remote_description()->description()->GetGroupByName( |
| 920 | cricket::GROUP_TYPE_BUNDLE); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 921 | if (local_bundle && remote_bundle) { |
Taylor Brandstetter | f475d36 | 2016-01-08 15:35:57 -0800 | [diff] [blame] | 922 | // The answerer decides the transport to bundle on. |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 923 | const cricket::ContentGroup* answer_bundle = |
| 924 | (source == cricket::CS_LOCAL ? local_bundle : remote_bundle); |
| 925 | if (!EnableBundle(*answer_bundle)) { |
| 926 | LOG(LS_WARNING) << "Failed to enable BUNDLE."; |
| 927 | return BadAnswerSdp(source, kEnableBundleFailed, err_desc); |
| 928 | } |
| 929 | } |
Taylor Brandstetter | f475d36 | 2016-01-08 15:35:57 -0800 | [diff] [blame] | 930 | // Only push down the transport description after enabling BUNDLE; we don't |
| 931 | // want to push down a description on a transport about to be destroyed. |
| 932 | if (!PushdownTransportDescription(source, cricket::CA_ANSWER, &td_err)) { |
| 933 | return BadAnswerSdp(source, MakeTdErrorString(td_err), err_desc); |
| 934 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 935 | EnableChannels(); |
deadbeef | d59daf8 | 2015-10-14 15:02:44 -0700 | [diff] [blame] | 936 | SetState(STATE_INPROGRESS); |
pthatcher@webrtc.org | 592470b | 2015-03-16 21:15:37 +0000 | [diff] [blame] | 937 | if (!PushdownMediaDescription(cricket::CA_ANSWER, source, err_desc)) { |
deadbeef | d59daf8 | 2015-10-14 15:02:44 -0700 | [diff] [blame] | 938 | SetError(ERROR_CONTENT, *err_desc); |
pthatcher@webrtc.org | 592470b | 2015-03-16 21:15:37 +0000 | [diff] [blame] | 939 | } |
deadbeef | d59daf8 | 2015-10-14 15:02:44 -0700 | [diff] [blame] | 940 | if (error() != ERROR_NONE) { |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 941 | return BadAnswerSdp(source, GetSessionErrorMsg(), err_desc); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 942 | } |
| 943 | } |
| 944 | return true; |
| 945 | } |
| 946 | |
| 947 | WebRtcSession::Action WebRtcSession::GetAction(const std::string& type) { |
| 948 | if (type == SessionDescriptionInterface::kOffer) { |
| 949 | return WebRtcSession::kOffer; |
| 950 | } else if (type == SessionDescriptionInterface::kPrAnswer) { |
| 951 | return WebRtcSession::kPrAnswer; |
| 952 | } else if (type == SessionDescriptionInterface::kAnswer) { |
| 953 | return WebRtcSession::kAnswer; |
| 954 | } |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 955 | RTC_NOTREACHED() << "unknown action type"; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 956 | return WebRtcSession::kOffer; |
| 957 | } |
| 958 | |
deadbeef | d59daf8 | 2015-10-14 15:02:44 -0700 | [diff] [blame] | 959 | bool WebRtcSession::PushdownMediaDescription( |
| 960 | cricket::ContentAction action, |
| 961 | cricket::ContentSource source, |
| 962 | std::string* err) { |
Steve Anton | 18ee1d5 | 2017-09-11 11:32:35 -0700 | [diff] [blame] | 963 | const SessionDescription* sdesc = |
| 964 | (source == cricket::CS_LOCAL ? local_description() : remote_description()) |
| 965 | ->description(); |
| 966 | RTC_DCHECK(sdesc); |
| 967 | bool all_success = true; |
| 968 | for (auto* channel : Channels()) { |
| 969 | // TODO(steveanton): Add support for multiple channels of the same type. |
| 970 | const ContentInfo* content_info = |
| 971 | cricket::GetFirstMediaContent(sdesc->contents(), channel->media_type()); |
| 972 | if (!content_info) { |
| 973 | continue; |
deadbeef | d59daf8 | 2015-10-14 15:02:44 -0700 | [diff] [blame] | 974 | } |
Steve Anton | 18ee1d5 | 2017-09-11 11:32:35 -0700 | [diff] [blame] | 975 | const MediaContentDescription* content_desc = |
| 976 | static_cast<const MediaContentDescription*>(content_info->description); |
| 977 | if (content_desc && !content_info->rejected) { |
| 978 | bool success = (source == cricket::CS_LOCAL) |
| 979 | ? channel->SetLocalContent(content_desc, action, err) |
| 980 | : channel->SetRemoteContent(content_desc, action, err); |
| 981 | if (!success) { |
| 982 | all_success = false; |
| 983 | break; |
| 984 | } |
| 985 | } |
| 986 | } |
deadbeef | 953c2ce | 2017-01-09 14:53:41 -0800 | [diff] [blame] | 987 | // Need complete offer/answer with an SCTP m= section before starting SCTP, |
| 988 | // according to https://tools.ietf.org/html/draft-ietf-mmusic-sctp-sdp-19 |
| 989 | if (sctp_transport_ && local_description() && remote_description() && |
| 990 | cricket::GetFirstDataContent(local_description()->description()) && |
| 991 | cricket::GetFirstDataContent(remote_description()->description())) { |
Steve Anton | 18ee1d5 | 2017-09-11 11:32:35 -0700 | [diff] [blame] | 992 | all_success &= network_thread_->Invoke<bool>( |
deadbeef | 953c2ce | 2017-01-09 14:53:41 -0800 | [diff] [blame] | 993 | RTC_FROM_HERE, |
| 994 | rtc::Bind(&WebRtcSession::PushdownSctpParameters_n, this, source)); |
| 995 | } |
Steve Anton | 18ee1d5 | 2017-09-11 11:32:35 -0700 | [diff] [blame] | 996 | return all_success; |
deadbeef | 953c2ce | 2017-01-09 14:53:41 -0800 | [diff] [blame] | 997 | } |
| 998 | |
| 999 | bool WebRtcSession::PushdownSctpParameters_n(cricket::ContentSource source) { |
| 1000 | RTC_DCHECK(network_thread_->IsCurrent()); |
| 1001 | RTC_DCHECK(local_description()); |
| 1002 | RTC_DCHECK(remote_description()); |
| 1003 | // Apply the SCTP port (which is hidden inside a DataCodec structure...) |
| 1004 | // When we support "max-message-size", that would also be pushed down here. |
| 1005 | return sctp_transport_->Start( |
| 1006 | GetSctpPort(local_description()->description()), |
| 1007 | GetSctpPort(remote_description()->description())); |
deadbeef | d59daf8 | 2015-10-14 15:02:44 -0700 | [diff] [blame] | 1008 | } |
| 1009 | |
| 1010 | bool WebRtcSession::PushdownTransportDescription(cricket::ContentSource source, |
| 1011 | cricket::ContentAction action, |
| 1012 | std::string* error_desc) { |
| 1013 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
| 1014 | |
| 1015 | if (source == cricket::CS_LOCAL) { |
deadbeef | fe4a8a4 | 2016-12-20 17:56:17 -0800 | [diff] [blame] | 1016 | return PushdownLocalTransportDescription(local_description()->description(), |
| 1017 | action, error_desc); |
deadbeef | d59daf8 | 2015-10-14 15:02:44 -0700 | [diff] [blame] | 1018 | } |
deadbeef | fe4a8a4 | 2016-12-20 17:56:17 -0800 | [diff] [blame] | 1019 | return PushdownRemoteTransportDescription(remote_description()->description(), |
| 1020 | action, error_desc); |
deadbeef | d59daf8 | 2015-10-14 15:02:44 -0700 | [diff] [blame] | 1021 | } |
| 1022 | |
| 1023 | bool WebRtcSession::PushdownLocalTransportDescription( |
| 1024 | const SessionDescription* sdesc, |
| 1025 | cricket::ContentAction action, |
| 1026 | std::string* err) { |
| 1027 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
| 1028 | |
| 1029 | if (!sdesc) { |
| 1030 | return false; |
| 1031 | } |
| 1032 | |
| 1033 | for (const TransportInfo& tinfo : sdesc->transport_infos()) { |
| 1034 | if (!transport_controller_->SetLocalTransportDescription( |
| 1035 | tinfo.content_name, tinfo.description, action, err)) { |
| 1036 | return false; |
| 1037 | } |
| 1038 | } |
| 1039 | |
| 1040 | return true; |
| 1041 | } |
| 1042 | |
| 1043 | bool WebRtcSession::PushdownRemoteTransportDescription( |
| 1044 | const SessionDescription* sdesc, |
| 1045 | cricket::ContentAction action, |
| 1046 | std::string* err) { |
| 1047 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
| 1048 | |
| 1049 | if (!sdesc) { |
| 1050 | return false; |
| 1051 | } |
| 1052 | |
| 1053 | for (const TransportInfo& tinfo : sdesc->transport_infos()) { |
| 1054 | if (!transport_controller_->SetRemoteTransportDescription( |
| 1055 | tinfo.content_name, tinfo.description, action, err)) { |
| 1056 | return false; |
| 1057 | } |
| 1058 | } |
| 1059 | |
| 1060 | return true; |
| 1061 | } |
| 1062 | |
| 1063 | bool WebRtcSession::GetTransportDescription( |
| 1064 | const SessionDescription* description, |
| 1065 | const std::string& content_name, |
| 1066 | cricket::TransportDescription* tdesc) { |
| 1067 | if (!description || !tdesc) { |
| 1068 | return false; |
| 1069 | } |
| 1070 | const TransportInfo* transport_info = |
| 1071 | description->GetTransportInfoByName(content_name); |
| 1072 | if (!transport_info) { |
| 1073 | return false; |
| 1074 | } |
| 1075 | *tdesc = transport_info->description; |
| 1076 | return true; |
| 1077 | } |
| 1078 | |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 1079 | bool WebRtcSession::EnableBundle(const cricket::ContentGroup& bundle) { |
| 1080 | const std::string* first_content_name = bundle.FirstContentName(); |
| 1081 | if (!first_content_name) { |
| 1082 | LOG(LS_WARNING) << "Tried to BUNDLE with no contents."; |
| 1083 | return false; |
| 1084 | } |
| 1085 | const std::string& transport_name = *first_content_name; |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 1086 | |
deadbeef | 953c2ce | 2017-01-09 14:53:41 -0800 | [diff] [blame] | 1087 | auto maybe_set_transport = [this, bundle, |
| 1088 | transport_name](cricket::BaseChannel* ch) { |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 1089 | if (!ch || !bundle.HasContentName(ch->content_name())) { |
pthatcher@webrtc.org | c04a97f | 2015-03-16 19:31:40 +0000 | [diff] [blame] | 1090 | return true; |
| 1091 | } |
| 1092 | |
zhihuang | f5b251b | 2017-01-12 19:37:48 -0800 | [diff] [blame] | 1093 | std::string old_transport_name = ch->transport_name(); |
| 1094 | if (old_transport_name == transport_name) { |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 1095 | LOG(LS_INFO) << "BUNDLE already enabled for " << ch->content_name() |
| 1096 | << " on " << transport_name << "."; |
| 1097 | return true; |
deadbeef | 47ee2f3 | 2015-09-22 15:08:23 -0700 | [diff] [blame] | 1098 | } |
torbjorng | a81a42f | 2015-09-23 02:16:58 -0700 | [diff] [blame] | 1099 | |
zhihuang | b2cdd93 | 2017-01-19 16:54:25 -0800 | [diff] [blame] | 1100 | cricket::DtlsTransportInternal* rtp_dtls_transport = |
| 1101 | transport_controller_->CreateDtlsTransport( |
zhihuang | f5b251b | 2017-01-12 19:37:48 -0800 | [diff] [blame] | 1102 | transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP); |
zhihuang | b2cdd93 | 2017-01-19 16:54:25 -0800 | [diff] [blame] | 1103 | bool need_rtcp = (ch->rtcp_dtls_transport() != nullptr); |
| 1104 | cricket::DtlsTransportInternal* rtcp_dtls_transport = nullptr; |
zhihuang | f5b251b | 2017-01-12 19:37:48 -0800 | [diff] [blame] | 1105 | if (need_rtcp) { |
deadbeef | d8cf08f | 2017-07-10 20:06:59 -0700 | [diff] [blame] | 1106 | rtcp_dtls_transport = transport_controller_->CreateDtlsTransport( |
zhihuang | f5b251b | 2017-01-12 19:37:48 -0800 | [diff] [blame] | 1107 | transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTCP); |
| 1108 | } |
| 1109 | |
zhihuang | b2cdd93 | 2017-01-19 16:54:25 -0800 | [diff] [blame] | 1110 | ch->SetTransports(rtp_dtls_transport, rtcp_dtls_transport); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 1111 | LOG(LS_INFO) << "Enabled BUNDLE for " << ch->content_name() << " on " |
| 1112 | << transport_name << "."; |
zhihuang | b2cdd93 | 2017-01-19 16:54:25 -0800 | [diff] [blame] | 1113 | transport_controller_->DestroyDtlsTransport( |
deadbeef | bad5dad | 2017-01-17 18:32:35 -0800 | [diff] [blame] | 1114 | old_transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP); |
zhihuang | f5b251b | 2017-01-12 19:37:48 -0800 | [diff] [blame] | 1115 | // If the channel needs rtcp, it means that the channel used to have a |
| 1116 | // rtcp transport which needs to be deleted now. |
| 1117 | if (need_rtcp) { |
zhihuang | b2cdd93 | 2017-01-19 16:54:25 -0800 | [diff] [blame] | 1118 | transport_controller_->DestroyDtlsTransport( |
deadbeef | bad5dad | 2017-01-17 18:32:35 -0800 | [diff] [blame] | 1119 | old_transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTCP); |
zhihuang | f5b251b | 2017-01-12 19:37:48 -0800 | [diff] [blame] | 1120 | } |
pthatcher@webrtc.org | c04a97f | 2015-03-16 19:31:40 +0000 | [diff] [blame] | 1121 | return true; |
| 1122 | }; |
| 1123 | |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 1124 | if (!maybe_set_transport(voice_channel()) || |
| 1125 | !maybe_set_transport(video_channel()) || |
deadbeef | 953c2ce | 2017-01-09 14:53:41 -0800 | [diff] [blame] | 1126 | !maybe_set_transport(rtp_data_channel())) { |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 1127 | return false; |
pthatcher@webrtc.org | c04a97f | 2015-03-16 19:31:40 +0000 | [diff] [blame] | 1128 | } |
deadbeef | 953c2ce | 2017-01-09 14:53:41 -0800 | [diff] [blame] | 1129 | // For SCTP, transport creation/deletion happens here instead of in the |
| 1130 | // object itself. |
| 1131 | if (sctp_transport_) { |
| 1132 | RTC_DCHECK(sctp_transport_name_); |
| 1133 | RTC_DCHECK(sctp_content_name_); |
| 1134 | if (transport_name != *sctp_transport_name_ && |
| 1135 | bundle.HasContentName(*sctp_content_name_)) { |
| 1136 | network_thread_->Invoke<void>( |
| 1137 | RTC_FROM_HERE, rtc::Bind(&WebRtcSession::ChangeSctpTransport_n, this, |
| 1138 | transport_name)); |
| 1139 | } |
| 1140 | } |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 1141 | |
pthatcher@webrtc.org | c04a97f | 2015-03-16 19:31:40 +0000 | [diff] [blame] | 1142 | return true; |
| 1143 | } |
| 1144 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1145 | bool WebRtcSession::ProcessIceMessage(const IceCandidateInterface* candidate) { |
deadbeef | fe4a8a4 | 2016-12-20 17:56:17 -0800 | [diff] [blame] | 1146 | if (!remote_description()) { |
deadbeef | d59daf8 | 2015-10-14 15:02:44 -0700 | [diff] [blame] | 1147 | LOG(LS_ERROR) << "ProcessIceMessage: ICE candidates can't be added " |
| 1148 | << "without any remote session description."; |
Honghai Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 1149 | return false; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1150 | } |
| 1151 | |
| 1152 | if (!candidate) { |
deadbeef | d59daf8 | 2015-10-14 15:02:44 -0700 | [diff] [blame] | 1153 | LOG(LS_ERROR) << "ProcessIceMessage: Candidate is NULL."; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1154 | return false; |
| 1155 | } |
| 1156 | |
jiayl@webrtc.org | e10d28c | 2014-07-17 17:07:49 +0000 | [diff] [blame] | 1157 | bool valid = false; |
deadbeef | d59daf8 | 2015-10-14 15:02:44 -0700 | [diff] [blame] | 1158 | bool ready = ReadyToUseRemoteCandidate(candidate, NULL, &valid); |
| 1159 | if (!valid) { |
| 1160 | return false; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1161 | } |
| 1162 | |
| 1163 | // Add this candidate to the remote session description. |
deadbeef | fe4a8a4 | 2016-12-20 17:56:17 -0800 | [diff] [blame] | 1164 | if (!mutable_remote_description()->AddCandidate(candidate)) { |
deadbeef | d59daf8 | 2015-10-14 15:02:44 -0700 | [diff] [blame] | 1165 | LOG(LS_ERROR) << "ProcessIceMessage: Candidate cannot be used."; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1166 | return false; |
| 1167 | } |
| 1168 | |
deadbeef | d59daf8 | 2015-10-14 15:02:44 -0700 | [diff] [blame] | 1169 | if (ready) { |
| 1170 | return UseCandidate(candidate); |
| 1171 | } else { |
| 1172 | LOG(LS_INFO) << "ProcessIceMessage: Not ready to use candidate."; |
| 1173 | return true; |
| 1174 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1175 | } |
| 1176 | |
Honghai Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 1177 | bool WebRtcSession::RemoveRemoteIceCandidates( |
| 1178 | const std::vector<cricket::Candidate>& candidates) { |
deadbeef | fe4a8a4 | 2016-12-20 17:56:17 -0800 | [diff] [blame] | 1179 | if (!remote_description()) { |
Honghai Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 1180 | LOG(LS_ERROR) << "RemoveRemoteIceCandidates: ICE candidates can't be " |
| 1181 | << "removed without any remote session description."; |
| 1182 | return false; |
| 1183 | } |
| 1184 | |
| 1185 | if (candidates.empty()) { |
| 1186 | LOG(LS_ERROR) << "RemoveRemoteIceCandidates: candidates are empty."; |
| 1187 | return false; |
| 1188 | } |
| 1189 | |
deadbeef | fe4a8a4 | 2016-12-20 17:56:17 -0800 | [diff] [blame] | 1190 | size_t number_removed = |
| 1191 | mutable_remote_description()->RemoveCandidates(candidates); |
Honghai Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 1192 | if (number_removed != candidates.size()) { |
| 1193 | LOG(LS_ERROR) << "RemoveRemoteIceCandidates: Failed to remove candidates. " |
| 1194 | << "Requested " << candidates.size() << " but only " |
| 1195 | << number_removed << " are removed."; |
| 1196 | } |
| 1197 | |
| 1198 | // Remove the candidates from the transport controller. |
| 1199 | std::string error; |
| 1200 | bool res = transport_controller_->RemoveRemoteCandidates(candidates, &error); |
| 1201 | if (!res && !error.empty()) { |
| 1202 | LOG(LS_ERROR) << "Error when removing remote candidates: " << error; |
| 1203 | } |
| 1204 | return true; |
| 1205 | } |
| 1206 | |
deadbeef | d59daf8 | 2015-10-14 15:02:44 -0700 | [diff] [blame] | 1207 | cricket::IceConfig WebRtcSession::ParseIceConfig( |
| 1208 | const PeerConnectionInterface::RTCConfiguration& config) const { |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 1209 | cricket::ContinualGatheringPolicy gathering_policy; |
| 1210 | // TODO(honghaiz): Add the third continual gathering policy in |
| 1211 | // PeerConnectionInterface and map it to GATHER_CONTINUALLY_AND_RECOVER. |
| 1212 | switch (config.continual_gathering_policy) { |
| 1213 | case PeerConnectionInterface::GATHER_ONCE: |
| 1214 | gathering_policy = cricket::GATHER_ONCE; |
| 1215 | break; |
| 1216 | case PeerConnectionInterface::GATHER_CONTINUALLY: |
| 1217 | gathering_policy = cricket::GATHER_CONTINUALLY; |
| 1218 | break; |
| 1219 | default: |
nisse | eb4ca4e | 2017-01-12 02:24:27 -0800 | [diff] [blame] | 1220 | RTC_NOTREACHED(); |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 1221 | gathering_policy = cricket::GATHER_ONCE; |
| 1222 | } |
deadbeef | d59daf8 | 2015-10-14 15:02:44 -0700 | [diff] [blame] | 1223 | cricket::IceConfig ice_config; |
Honghai Zhang | 049fbb1 | 2016-03-07 11:13:07 -0800 | [diff] [blame] | 1224 | ice_config.receiving_timeout = config.ice_connection_receiving_timeout; |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 1225 | ice_config.prioritize_most_likely_candidate_pairs = |
| 1226 | config.prioritize_most_likely_ice_candidate_pairs; |
Honghai Zhang | 381b421 | 2015-12-04 12:24:03 -0800 | [diff] [blame] | 1227 | ice_config.backup_connection_ping_interval = |
| 1228 | config.ice_backup_candidate_pair_ping_interval; |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 1229 | ice_config.continual_gathering_policy = gathering_policy; |
Taylor Brandstetter | e985111 | 2016-07-01 11:11:13 -0700 | [diff] [blame] | 1230 | ice_config.presume_writable_when_fully_relayed = |
| 1231 | config.presume_writable_when_fully_relayed; |
skvlad | 5107246 | 2017-02-02 11:50:14 -0800 | [diff] [blame] | 1232 | ice_config.ice_check_min_interval = config.ice_check_min_interval; |
Steve Anton | 300bf8e | 2017-07-14 10:13:10 -0700 | [diff] [blame] | 1233 | ice_config.regather_all_networks_interval_range = |
| 1234 | config.ice_regather_interval_range; |
deadbeef | d59daf8 | 2015-10-14 15:02:44 -0700 | [diff] [blame] | 1235 | return ice_config; |
| 1236 | } |
| 1237 | |
| 1238 | void WebRtcSession::SetIceConfig(const cricket::IceConfig& config) { |
| 1239 | transport_controller_->SetIceConfig(config); |
| 1240 | } |
| 1241 | |
| 1242 | void WebRtcSession::MaybeStartGathering() { |
| 1243 | transport_controller_->MaybeStartGathering(); |
| 1244 | } |
| 1245 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 1246 | bool WebRtcSession::GetLocalTrackIdBySsrc(uint32_t ssrc, |
| 1247 | std::string* track_id) { |
deadbeef | fe4a8a4 | 2016-12-20 17:56:17 -0800 | [diff] [blame] | 1248 | if (!local_description()) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1249 | return false; |
deadbeef | d59daf8 | 2015-10-14 15:02:44 -0700 | [diff] [blame] | 1250 | } |
deadbeef | fe4a8a4 | 2016-12-20 17:56:17 -0800 | [diff] [blame] | 1251 | return webrtc::GetTrackIdBySsrc(local_description()->description(), ssrc, |
| 1252 | track_id); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1253 | } |
| 1254 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 1255 | bool WebRtcSession::GetRemoteTrackIdBySsrc(uint32_t ssrc, |
| 1256 | std::string* track_id) { |
deadbeef | fe4a8a4 | 2016-12-20 17:56:17 -0800 | [diff] [blame] | 1257 | if (!remote_description()) { |
xians@webrtc.org | 4cb0128 | 2014-06-12 14:57:05 +0000 | [diff] [blame] | 1258 | return false; |
deadbeef | d59daf8 | 2015-10-14 15:02:44 -0700 | [diff] [blame] | 1259 | } |
deadbeef | fe4a8a4 | 2016-12-20 17:56:17 -0800 | [diff] [blame] | 1260 | return webrtc::GetTrackIdBySsrc(remote_description()->description(), ssrc, |
| 1261 | track_id); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1262 | } |
| 1263 | |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 1264 | std::string WebRtcSession::BadStateErrMsg(State state) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1265 | std::ostringstream desc; |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 1266 | desc << "Called in wrong state: " << GetStateString(state); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1267 | return desc.str(); |
| 1268 | } |
| 1269 | |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 1270 | bool WebRtcSession::SendData(const cricket::SendDataParams& params, |
jbauch | eec21bd | 2016-03-20 06:15:43 -0700 | [diff] [blame] | 1271 | const rtc::CopyOnWriteBuffer& payload, |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 1272 | cricket::SendDataResult* result) { |
deadbeef | 953c2ce | 2017-01-09 14:53:41 -0800 | [diff] [blame] | 1273 | if (!rtp_data_channel_ && !sctp_transport_) { |
| 1274 | LOG(LS_ERROR) << "SendData called when rtp_data_channel_ " |
| 1275 | << "and sctp_transport_ are NULL."; |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 1276 | return false; |
| 1277 | } |
deadbeef | 953c2ce | 2017-01-09 14:53:41 -0800 | [diff] [blame] | 1278 | return rtp_data_channel_ |
| 1279 | ? rtp_data_channel_->SendData(params, payload, result) |
| 1280 | : network_thread_->Invoke<bool>( |
| 1281 | RTC_FROM_HERE, |
| 1282 | Bind(&cricket::SctpTransportInternal::SendData, |
| 1283 | sctp_transport_.get(), params, payload, result)); |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 1284 | } |
| 1285 | |
| 1286 | bool WebRtcSession::ConnectDataChannel(DataChannel* webrtc_data_channel) { |
deadbeef | 953c2ce | 2017-01-09 14:53:41 -0800 | [diff] [blame] | 1287 | if (!rtp_data_channel_ && !sctp_transport_) { |
deadbeef | daf88b1 | 2016-10-05 22:29:30 -0700 | [diff] [blame] | 1288 | // Don't log an error here, because DataChannels are expected to call |
| 1289 | // ConnectDataChannel in this state. It's the only way to initially tell |
| 1290 | // whether or not the underlying transport is ready. |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 1291 | return false; |
| 1292 | } |
deadbeef | 953c2ce | 2017-01-09 14:53:41 -0800 | [diff] [blame] | 1293 | if (rtp_data_channel_) { |
| 1294 | rtp_data_channel_->SignalReadyToSendData.connect( |
| 1295 | webrtc_data_channel, &DataChannel::OnChannelReady); |
| 1296 | rtp_data_channel_->SignalDataReceived.connect(webrtc_data_channel, |
| 1297 | &DataChannel::OnDataReceived); |
| 1298 | } else { |
| 1299 | SignalSctpReadyToSendData.connect(webrtc_data_channel, |
| 1300 | &DataChannel::OnChannelReady); |
| 1301 | SignalSctpDataReceived.connect(webrtc_data_channel, |
| 1302 | &DataChannel::OnDataReceived); |
| 1303 | SignalSctpStreamClosedRemotely.connect( |
| 1304 | webrtc_data_channel, &DataChannel::OnStreamClosedRemotely); |
| 1305 | } |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 1306 | return true; |
| 1307 | } |
| 1308 | |
| 1309 | void WebRtcSession::DisconnectDataChannel(DataChannel* webrtc_data_channel) { |
deadbeef | 953c2ce | 2017-01-09 14:53:41 -0800 | [diff] [blame] | 1310 | if (!rtp_data_channel_ && !sctp_transport_) { |
| 1311 | LOG(LS_ERROR) << "DisconnectDataChannel called when rtp_data_channel_ and " |
| 1312 | "sctp_transport_ are NULL."; |
wu@webrtc.org | cecfd18 | 2013-10-30 05:18:12 +0000 | [diff] [blame] | 1313 | return; |
| 1314 | } |
deadbeef | 953c2ce | 2017-01-09 14:53:41 -0800 | [diff] [blame] | 1315 | if (rtp_data_channel_) { |
| 1316 | rtp_data_channel_->SignalReadyToSendData.disconnect(webrtc_data_channel); |
| 1317 | rtp_data_channel_->SignalDataReceived.disconnect(webrtc_data_channel); |
| 1318 | } else { |
| 1319 | SignalSctpReadyToSendData.disconnect(webrtc_data_channel); |
| 1320 | SignalSctpDataReceived.disconnect(webrtc_data_channel); |
| 1321 | SignalSctpStreamClosedRemotely.disconnect(webrtc_data_channel); |
| 1322 | } |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 1323 | } |
| 1324 | |
bemasc@webrtc.org | 9b5467e | 2014-12-04 23:16:52 +0000 | [diff] [blame] | 1325 | void WebRtcSession::AddSctpDataStream(int sid) { |
deadbeef | 953c2ce | 2017-01-09 14:53:41 -0800 | [diff] [blame] | 1326 | if (!sctp_transport_) { |
| 1327 | LOG(LS_ERROR) << "AddSctpDataStream called when sctp_transport_ is NULL."; |
wu@webrtc.org | cecfd18 | 2013-10-30 05:18:12 +0000 | [diff] [blame] | 1328 | return; |
| 1329 | } |
deadbeef | 953c2ce | 2017-01-09 14:53:41 -0800 | [diff] [blame] | 1330 | network_thread_->Invoke<void>( |
| 1331 | RTC_FROM_HERE, rtc::Bind(&cricket::SctpTransportInternal::OpenStream, |
| 1332 | sctp_transport_.get(), sid)); |
wu@webrtc.org | cecfd18 | 2013-10-30 05:18:12 +0000 | [diff] [blame] | 1333 | } |
| 1334 | |
bemasc@webrtc.org | 9b5467e | 2014-12-04 23:16:52 +0000 | [diff] [blame] | 1335 | void WebRtcSession::RemoveSctpDataStream(int sid) { |
deadbeef | 953c2ce | 2017-01-09 14:53:41 -0800 | [diff] [blame] | 1336 | if (!sctp_transport_) { |
| 1337 | LOG(LS_ERROR) << "RemoveSctpDataStream called when sctp_transport_ is " |
wu@webrtc.org | cecfd18 | 2013-10-30 05:18:12 +0000 | [diff] [blame] | 1338 | << "NULL."; |
| 1339 | return; |
| 1340 | } |
deadbeef | 953c2ce | 2017-01-09 14:53:41 -0800 | [diff] [blame] | 1341 | network_thread_->Invoke<void>( |
| 1342 | RTC_FROM_HERE, rtc::Bind(&cricket::SctpTransportInternal::ResetStream, |
| 1343 | sctp_transport_.get(), sid)); |
wu@webrtc.org | cecfd18 | 2013-10-30 05:18:12 +0000 | [diff] [blame] | 1344 | } |
| 1345 | |
wu@webrtc.org | 07a6fbe | 2013-11-04 18:41:34 +0000 | [diff] [blame] | 1346 | bool WebRtcSession::ReadyToSendData() const { |
deadbeef | 953c2ce | 2017-01-09 14:53:41 -0800 | [diff] [blame] | 1347 | return (rtp_data_channel_ && rtp_data_channel_->ready_to_send_data()) || |
| 1348 | sctp_ready_to_send_data_; |
| 1349 | } |
| 1350 | |
| 1351 | std::unique_ptr<SessionStats> WebRtcSession::GetStats_s() { |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 1352 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
deadbeef | 953c2ce | 2017-01-09 14:53:41 -0800 | [diff] [blame] | 1353 | ChannelNamePairs channel_name_pairs; |
| 1354 | if (voice_channel()) { |
| 1355 | channel_name_pairs.voice = rtc::Optional<ChannelNamePair>(ChannelNamePair( |
| 1356 | voice_channel()->content_name(), voice_channel()->transport_name())); |
| 1357 | } |
| 1358 | if (video_channel()) { |
| 1359 | channel_name_pairs.video = rtc::Optional<ChannelNamePair>(ChannelNamePair( |
| 1360 | video_channel()->content_name(), video_channel()->transport_name())); |
| 1361 | } |
| 1362 | if (rtp_data_channel()) { |
| 1363 | channel_name_pairs.data = rtc::Optional<ChannelNamePair>( |
| 1364 | ChannelNamePair(rtp_data_channel()->content_name(), |
| 1365 | rtp_data_channel()->transport_name())); |
| 1366 | } |
| 1367 | if (sctp_transport_) { |
| 1368 | RTC_DCHECK(sctp_content_name_); |
| 1369 | RTC_DCHECK(sctp_transport_name_); |
| 1370 | channel_name_pairs.data = rtc::Optional<ChannelNamePair>( |
| 1371 | ChannelNamePair(*sctp_content_name_, *sctp_transport_name_)); |
| 1372 | } |
| 1373 | return GetStats(channel_name_pairs); |
| 1374 | } |
| 1375 | |
| 1376 | std::unique_ptr<SessionStats> WebRtcSession::GetStats( |
| 1377 | const ChannelNamePairs& channel_name_pairs) { |
| 1378 | if (network_thread()->IsCurrent()) { |
| 1379 | return GetStats_n(channel_name_pairs); |
| 1380 | } |
| 1381 | return network_thread()->Invoke<std::unique_ptr<SessionStats>>( |
| 1382 | RTC_FROM_HERE, |
| 1383 | rtc::Bind(&WebRtcSession::GetStats_n, this, channel_name_pairs)); |
| 1384 | } |
| 1385 | |
| 1386 | bool WebRtcSession::GetLocalCertificate( |
| 1387 | const std::string& transport_name, |
| 1388 | rtc::scoped_refptr<rtc::RTCCertificate>* certificate) { |
| 1389 | return transport_controller_->GetLocalCertificate(transport_name, |
| 1390 | certificate); |
| 1391 | } |
| 1392 | |
| 1393 | std::unique_ptr<rtc::SSLCertificate> WebRtcSession::GetRemoteSSLCertificate( |
| 1394 | const std::string& transport_name) { |
| 1395 | return transport_controller_->GetRemoteSSLCertificate(transport_name); |
wu@webrtc.org | 07a6fbe | 2013-11-04 18:41:34 +0000 | [diff] [blame] | 1396 | } |
| 1397 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1398 | cricket::DataChannelType WebRtcSession::data_channel_type() const { |
| 1399 | return data_channel_type_; |
| 1400 | } |
| 1401 | |
deadbeef | 0ed85b2 | 2016-02-23 17:24:52 -0800 | [diff] [blame] | 1402 | bool WebRtcSession::IceRestartPending(const std::string& content_name) const { |
| 1403 | return pending_ice_restarts_.find(content_name) != |
| 1404 | pending_ice_restarts_.end(); |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 1405 | } |
| 1406 | |
deadbeef | d1a38b5 | 2016-12-10 13:15:33 -0800 | [diff] [blame] | 1407 | void WebRtcSession::SetNeedsIceRestartFlag() { |
| 1408 | transport_controller_->SetNeedsIceRestartFlag(); |
| 1409 | } |
| 1410 | |
| 1411 | bool WebRtcSession::NeedsIceRestart(const std::string& content_name) const { |
| 1412 | return transport_controller_->NeedsIceRestart(content_name); |
| 1413 | } |
| 1414 | |
Henrik Boström | d828198 | 2015-08-27 10:12:24 +0200 | [diff] [blame] | 1415 | void WebRtcSession::OnCertificateReady( |
| 1416 | const rtc::scoped_refptr<rtc::RTCCertificate>& certificate) { |
deadbeef | d59daf8 | 2015-10-14 15:02:44 -0700 | [diff] [blame] | 1417 | transport_controller_->SetLocalCertificate(certificate); |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 1418 | } |
| 1419 | |
deadbeef | 953c2ce | 2017-01-09 14:53:41 -0800 | [diff] [blame] | 1420 | void WebRtcSession::OnDtlsSrtpSetupFailure(cricket::BaseChannel*, bool rtcp) { |
| 1421 | SetError(ERROR_TRANSPORT, |
| 1422 | rtcp ? kDtlsSrtpSetupFailureRtcp : kDtlsSrtpSetupFailureRtp); |
| 1423 | } |
| 1424 | |
Henrik Boström | d828198 | 2015-08-27 10:12:24 +0200 | [diff] [blame] | 1425 | bool WebRtcSession::waiting_for_certificate_for_testing() const { |
Henrik Boström | 87713d0 | 2015-08-25 09:53:21 +0200 | [diff] [blame] | 1426 | return webrtc_session_desc_factory_->waiting_for_certificate_for_testing(); |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 1427 | } |
| 1428 | |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 1429 | const rtc::scoped_refptr<rtc::RTCCertificate>& |
| 1430 | WebRtcSession::certificate_for_testing() { |
deadbeef | d59daf8 | 2015-10-14 15:02:44 -0700 | [diff] [blame] | 1431 | return transport_controller_->certificate_for_testing(); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 1432 | } |
| 1433 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1434 | void WebRtcSession::SetIceConnectionState( |
| 1435 | PeerConnectionInterface::IceConnectionState state) { |
| 1436 | if (ice_connection_state_ == state) { |
| 1437 | return; |
| 1438 | } |
| 1439 | |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 1440 | LOG(LS_INFO) << "Changing IceConnectionState " << ice_connection_state_ |
| 1441 | << " => " << state; |
Taylor Brandstetter | 6aefc63 | 2016-05-26 16:08:23 -0700 | [diff] [blame] | 1442 | RTC_DCHECK(ice_connection_state_ != |
| 1443 | PeerConnectionInterface::kIceConnectionClosed); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1444 | ice_connection_state_ = state; |
| 1445 | if (ice_observer_) { |
zstein | 6dfd53a | 2017-03-06 13:49:03 -0800 | [diff] [blame] | 1446 | ice_observer_->OnIceConnectionStateChange(ice_connection_state_); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1447 | } |
| 1448 | } |
| 1449 | |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 1450 | void WebRtcSession::OnTransportControllerConnectionState( |
| 1451 | cricket::IceConnectionState state) { |
| 1452 | switch (state) { |
| 1453 | case cricket::kIceConnectionConnecting: |
| 1454 | // If the current state is Connected or Completed, then there were |
| 1455 | // writable channels but now there are not, so the next state must |
| 1456 | // be Disconnected. |
| 1457 | // kIceConnectionConnecting is currently used as the default, |
| 1458 | // un-connected state by the TransportController, so its only use is |
| 1459 | // detecting disconnections. |
| 1460 | if (ice_connection_state_ == |
| 1461 | PeerConnectionInterface::kIceConnectionConnected || |
| 1462 | ice_connection_state_ == |
| 1463 | PeerConnectionInterface::kIceConnectionCompleted) { |
| 1464 | SetIceConnectionState( |
| 1465 | PeerConnectionInterface::kIceConnectionDisconnected); |
| 1466 | } |
torbjorng | a81a42f | 2015-09-23 02:16:58 -0700 | [diff] [blame] | 1467 | break; |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 1468 | case cricket::kIceConnectionFailed: |
| 1469 | SetIceConnectionState(PeerConnectionInterface::kIceConnectionFailed); |
| 1470 | break; |
| 1471 | case cricket::kIceConnectionConnected: |
| 1472 | LOG(LS_INFO) << "Changing to ICE connected state because " |
| 1473 | << "all transports are writable."; |
| 1474 | SetIceConnectionState(PeerConnectionInterface::kIceConnectionConnected); |
| 1475 | break; |
| 1476 | case cricket::kIceConnectionCompleted: |
| 1477 | LOG(LS_INFO) << "Changing to ICE completed state because " |
| 1478 | << "all transports are complete."; |
| 1479 | if (ice_connection_state_ != |
| 1480 | PeerConnectionInterface::kIceConnectionConnected) { |
| 1481 | // If jumping directly from "checking" to "connected", |
| 1482 | // signal "connected" first. |
| 1483 | SetIceConnectionState(PeerConnectionInterface::kIceConnectionConnected); |
| 1484 | } |
| 1485 | SetIceConnectionState(PeerConnectionInterface::kIceConnectionCompleted); |
| 1486 | if (metrics_observer_) { |
| 1487 | ReportTransportStats(); |
| 1488 | } |
| 1489 | break; |
| 1490 | default: |
nisse | c80e741 | 2017-01-11 05:56:46 -0800 | [diff] [blame] | 1491 | RTC_NOTREACHED(); |
torbjorng | a81a42f | 2015-09-23 02:16:58 -0700 | [diff] [blame] | 1492 | } |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 1493 | } |
| 1494 | |
| 1495 | void WebRtcSession::OnTransportControllerReceiving(bool receiving) { |
Peter Thatcher | 5436051 | 2015-07-08 11:08:35 -0700 | [diff] [blame] | 1496 | SetIceConnectionReceiving(receiving); |
| 1497 | } |
| 1498 | |
| 1499 | void WebRtcSession::SetIceConnectionReceiving(bool receiving) { |
| 1500 | if (ice_connection_receiving_ == receiving) { |
| 1501 | return; |
| 1502 | } |
| 1503 | ice_connection_receiving_ = receiving; |
| 1504 | if (ice_observer_) { |
| 1505 | ice_observer_->OnIceConnectionReceivingChange(receiving); |
| 1506 | } |
| 1507 | } |
| 1508 | |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 1509 | void WebRtcSession::OnTransportControllerCandidatesGathered( |
| 1510 | const std::string& transport_name, |
| 1511 | const cricket::Candidates& candidates) { |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 1512 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 1513 | int sdp_mline_index; |
| 1514 | if (!GetLocalCandidateMediaIndex(transport_name, &sdp_mline_index)) { |
| 1515 | LOG(LS_ERROR) << "OnTransportControllerCandidatesGathered: content name " |
| 1516 | << transport_name << " not found"; |
| 1517 | return; |
| 1518 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1519 | |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 1520 | for (cricket::Candidates::const_iterator citer = candidates.begin(); |
| 1521 | citer != candidates.end(); ++citer) { |
| 1522 | // Use transport_name as the candidate media id. |
jbauch | 81bf7b0 | 2017-03-25 08:31:12 -0700 | [diff] [blame] | 1523 | std::unique_ptr<JsepIceCandidate> candidate( |
| 1524 | new JsepIceCandidate(transport_name, sdp_mline_index, *citer)); |
deadbeef | fe4a8a4 | 2016-12-20 17:56:17 -0800 | [diff] [blame] | 1525 | if (local_description()) { |
jbauch | 81bf7b0 | 2017-03-25 08:31:12 -0700 | [diff] [blame] | 1526 | mutable_local_description()->AddCandidate(candidate.get()); |
| 1527 | } |
| 1528 | if (ice_observer_) { |
| 1529 | ice_observer_->OnIceCandidate(std::move(candidate)); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 1530 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1531 | } |
| 1532 | } |
| 1533 | |
Honghai Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 1534 | void WebRtcSession::OnTransportControllerCandidatesRemoved( |
| 1535 | const std::vector<cricket::Candidate>& candidates) { |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 1536 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
Honghai Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 1537 | // Sanity check. |
| 1538 | for (const cricket::Candidate& candidate : candidates) { |
| 1539 | if (candidate.transport_name().empty()) { |
| 1540 | LOG(LS_ERROR) << "OnTransportControllerCandidatesRemoved: " |
| 1541 | << "empty content name in candidate " |
| 1542 | << candidate.ToString(); |
| 1543 | return; |
| 1544 | } |
| 1545 | } |
| 1546 | |
deadbeef | fe4a8a4 | 2016-12-20 17:56:17 -0800 | [diff] [blame] | 1547 | if (local_description()) { |
| 1548 | mutable_local_description()->RemoveCandidates(candidates); |
Honghai Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 1549 | } |
| 1550 | if (ice_observer_) { |
| 1551 | ice_observer_->OnIceCandidatesRemoved(candidates); |
| 1552 | } |
| 1553 | } |
| 1554 | |
deadbeef | 953c2ce | 2017-01-09 14:53:41 -0800 | [diff] [blame] | 1555 | void WebRtcSession::OnTransportControllerDtlsHandshakeError( |
| 1556 | rtc::SSLHandshakeError error) { |
| 1557 | if (metrics_observer_) { |
| 1558 | metrics_observer_->IncrementEnumCounter( |
| 1559 | webrtc::kEnumCounterDtlsHandshakeError, static_cast<int>(error), |
| 1560 | static_cast<int>(rtc::SSLHandshakeError::MAX_VALUE)); |
| 1561 | } |
| 1562 | } |
| 1563 | |
Steve Anton | 169629a | 2017-08-30 17:36:36 -0700 | [diff] [blame] | 1564 | // Enabling voice and video (and RTP data) channels. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1565 | void WebRtcSession::EnableChannels() { |
Steve Anton | 169629a | 2017-08-30 17:36:36 -0700 | [diff] [blame] | 1566 | for (cricket::VoiceChannel* voice_channel : voice_channels_) { |
| 1567 | if (!voice_channel->enabled()) { |
| 1568 | voice_channel->Enable(true); |
| 1569 | } |
| 1570 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1571 | |
Steve Anton | 169629a | 2017-08-30 17:36:36 -0700 | [diff] [blame] | 1572 | for (cricket::VideoChannel* video_channel : video_channels_) { |
| 1573 | if (!video_channel->enabled()) { |
| 1574 | video_channel->Enable(true); |
| 1575 | } |
| 1576 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1577 | |
deadbeef | 953c2ce | 2017-01-09 14:53:41 -0800 | [diff] [blame] | 1578 | if (rtp_data_channel_ && !rtp_data_channel_->enabled()) |
| 1579 | rtp_data_channel_->Enable(true); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1580 | } |
| 1581 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1582 | // Returns the media index for a local ice candidate given the content name. |
| 1583 | bool WebRtcSession::GetLocalCandidateMediaIndex(const std::string& content_name, |
| 1584 | int* sdp_mline_index) { |
deadbeef | fe4a8a4 | 2016-12-20 17:56:17 -0800 | [diff] [blame] | 1585 | if (!local_description() || !sdp_mline_index) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1586 | return false; |
deadbeef | d59daf8 | 2015-10-14 15:02:44 -0700 | [diff] [blame] | 1587 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1588 | |
| 1589 | bool content_found = false; |
deadbeef | fe4a8a4 | 2016-12-20 17:56:17 -0800 | [diff] [blame] | 1590 | const ContentInfos& contents = local_description()->description()->contents(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1591 | for (size_t index = 0; index < contents.size(); ++index) { |
| 1592 | if (contents[index].name == content_name) { |
henrike@webrtc.org | 28654cb | 2013-07-22 21:07:49 +0000 | [diff] [blame] | 1593 | *sdp_mline_index = static_cast<int>(index); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1594 | content_found = true; |
| 1595 | break; |
| 1596 | } |
| 1597 | } |
| 1598 | return content_found; |
| 1599 | } |
| 1600 | |
| 1601 | bool WebRtcSession::UseCandidatesInSessionDescription( |
| 1602 | const SessionDescriptionInterface* remote_desc) { |
deadbeef | d59daf8 | 2015-10-14 15:02:44 -0700 | [diff] [blame] | 1603 | if (!remote_desc) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1604 | return true; |
deadbeef | d59daf8 | 2015-10-14 15:02:44 -0700 | [diff] [blame] | 1605 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1606 | bool ret = true; |
jiayl@webrtc.org | e10d28c | 2014-07-17 17:07:49 +0000 | [diff] [blame] | 1607 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1608 | for (size_t m = 0; m < remote_desc->number_of_mediasections(); ++m) { |
| 1609 | const IceCandidateCollection* candidates = remote_desc->candidates(m); |
deadbeef | d59daf8 | 2015-10-14 15:02:44 -0700 | [diff] [blame] | 1610 | for (size_t n = 0; n < candidates->count(); ++n) { |
jiayl@webrtc.org | e10d28c | 2014-07-17 17:07:49 +0000 | [diff] [blame] | 1611 | const IceCandidateInterface* candidate = candidates->at(n); |
| 1612 | bool valid = false; |
| 1613 | if (!ReadyToUseRemoteCandidate(candidate, remote_desc, &valid)) { |
| 1614 | if (valid) { |
deadbeef | d59daf8 | 2015-10-14 15:02:44 -0700 | [diff] [blame] | 1615 | LOG(LS_INFO) << "UseCandidatesInSessionDescription: Not ready to use " |
| 1616 | << "candidate."; |
jiayl@webrtc.org | e10d28c | 2014-07-17 17:07:49 +0000 | [diff] [blame] | 1617 | } |
| 1618 | continue; |
| 1619 | } |
jiayl@webrtc.org | e10d28c | 2014-07-17 17:07:49 +0000 | [diff] [blame] | 1620 | ret = UseCandidate(candidate); |
deadbeef | d59daf8 | 2015-10-14 15:02:44 -0700 | [diff] [blame] | 1621 | if (!ret) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1622 | break; |
deadbeef | d59daf8 | 2015-10-14 15:02:44 -0700 | [diff] [blame] | 1623 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1624 | } |
| 1625 | } |
| 1626 | return ret; |
| 1627 | } |
| 1628 | |
Honghai Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 1629 | bool WebRtcSession::UseCandidate(const IceCandidateInterface* candidate) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1630 | size_t mediacontent_index = static_cast<size_t>(candidate->sdp_mline_index()); |
deadbeef | fe4a8a4 | 2016-12-20 17:56:17 -0800 | [diff] [blame] | 1631 | size_t remote_content_size = |
| 1632 | remote_description()->description()->contents().size(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1633 | if (mediacontent_index >= remote_content_size) { |
Honghai Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 1634 | LOG(LS_ERROR) << "UseCandidate: Invalid candidate media index."; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1635 | return false; |
| 1636 | } |
| 1637 | |
| 1638 | cricket::ContentInfo content = |
deadbeef | fe4a8a4 | 2016-12-20 17:56:17 -0800 | [diff] [blame] | 1639 | remote_description()->description()->contents()[mediacontent_index]; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1640 | std::vector<cricket::Candidate> candidates; |
| 1641 | candidates.push_back(candidate->candidate()); |
| 1642 | // Invoking BaseSession method to handle remote candidates. |
| 1643 | std::string error; |
deadbeef | d59daf8 | 2015-10-14 15:02:44 -0700 | [diff] [blame] | 1644 | if (transport_controller_->AddRemoteCandidates(content.name, candidates, |
| 1645 | &error)) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1646 | // Candidates successfully submitted for checking. |
| 1647 | if (ice_connection_state_ == PeerConnectionInterface::kIceConnectionNew || |
| 1648 | ice_connection_state_ == |
| 1649 | PeerConnectionInterface::kIceConnectionDisconnected) { |
| 1650 | // If state is New, then the session has just gotten its first remote ICE |
| 1651 | // candidates, so go to Checking. |
| 1652 | // If state is Disconnected, the session is re-using old candidates or |
| 1653 | // receiving additional ones, so go to Checking. |
| 1654 | // If state is Connected, stay Connected. |
| 1655 | // TODO(bemasc): If state is Connected, and the new candidates are for a |
| 1656 | // newly added transport, then the state actually _should_ move to |
| 1657 | // checking. Add a way to distinguish that case. |
| 1658 | SetIceConnectionState(PeerConnectionInterface::kIceConnectionChecking); |
| 1659 | } |
| 1660 | // TODO(bemasc): If state is Completed, go back to Connected. |
| 1661 | } else { |
fischman@webrtc.org | 4f2bd68 | 2014-03-28 18:13:34 +0000 | [diff] [blame] | 1662 | if (!error.empty()) { |
| 1663 | LOG(LS_WARNING) << error; |
| 1664 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1665 | } |
| 1666 | return true; |
| 1667 | } |
| 1668 | |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 1669 | void WebRtcSession::RemoveUnusedChannels(const SessionDescription* desc) { |
Steve Anton | 169629a | 2017-08-30 17:36:36 -0700 | [diff] [blame] | 1670 | // TODO(steveanton): Add support for multiple audio/video channels. |
| 1671 | // Destroy video channel first since it may have a pointer to the |
| 1672 | // voice channel. |
| 1673 | const cricket::ContentInfo* video_info = cricket::GetFirstVideoContent(desc); |
| 1674 | if ((!video_info || video_info->rejected) && video_channel()) { |
| 1675 | RemoveAndDestroyVideoChannel(video_channel()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1676 | } |
| 1677 | |
Steve Anton | 169629a | 2017-08-30 17:36:36 -0700 | [diff] [blame] | 1678 | const cricket::ContentInfo* voice_info = cricket::GetFirstAudioContent(desc); |
| 1679 | if ((!voice_info || voice_info->rejected) && voice_channel()) { |
| 1680 | RemoveAndDestroyVoiceChannel(voice_channel()); |
buildbot@webrtc.org | b4c7b09 | 2014-08-25 12:11:58 +0000 | [diff] [blame] | 1681 | } |
| 1682 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1683 | const cricket::ContentInfo* data_info = |
| 1684 | cricket::GetFirstDataContent(desc); |
zhihuang | 9763d56 | 2016-08-05 11:14:50 -0700 | [diff] [blame] | 1685 | if (!data_info || data_info->rejected) { |
deadbeef | 953c2ce | 2017-01-09 14:53:41 -0800 | [diff] [blame] | 1686 | if (rtp_data_channel_) { |
zhihuang | f5b251b | 2017-01-12 19:37:48 -0800 | [diff] [blame] | 1687 | DestroyDataChannel(); |
deadbeef | 953c2ce | 2017-01-09 14:53:41 -0800 | [diff] [blame] | 1688 | } |
| 1689 | if (sctp_transport_) { |
| 1690 | SignalDataChannelDestroyed(); |
| 1691 | network_thread_->Invoke<void>( |
| 1692 | RTC_FROM_HERE, |
| 1693 | rtc::Bind(&WebRtcSession::DestroySctpTransport_n, this)); |
zhihuang | 9763d56 | 2016-08-05 11:14:50 -0700 | [diff] [blame] | 1694 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1695 | } |
| 1696 | } |
| 1697 | |
skvlad | 6c87a67 | 2016-05-17 17:49:52 -0700 | [diff] [blame] | 1698 | // Returns the name of the transport channel when BUNDLE is enabled, or nullptr |
| 1699 | // if the channel is not part of any bundle. |
| 1700 | const std::string* WebRtcSession::GetBundleTransportName( |
| 1701 | const cricket::ContentInfo* content, |
| 1702 | const cricket::ContentGroup* bundle) { |
| 1703 | if (!bundle) { |
| 1704 | return nullptr; |
| 1705 | } |
| 1706 | const std::string* first_content_name = bundle->FirstContentName(); |
| 1707 | if (!first_content_name) { |
| 1708 | LOG(LS_WARNING) << "Tried to BUNDLE with no contents."; |
| 1709 | return nullptr; |
| 1710 | } |
| 1711 | if (!bundle->HasContentName(content->name)) { |
| 1712 | LOG(LS_WARNING) << content->name << " is not part of any bundle group"; |
| 1713 | return nullptr; |
| 1714 | } |
| 1715 | LOG(LS_INFO) << "Bundling " << content->name << " on " << *first_content_name; |
| 1716 | return first_content_name; |
| 1717 | } |
| 1718 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1719 | bool WebRtcSession::CreateChannels(const SessionDescription* desc) { |
Steve Anton | 169629a | 2017-08-30 17:36:36 -0700 | [diff] [blame] | 1720 | // TODO(steveanton): Add support for multiple audio/video channels. |
skvlad | 6c87a67 | 2016-05-17 17:49:52 -0700 | [diff] [blame] | 1721 | const cricket::ContentGroup* bundle_group = nullptr; |
| 1722 | if (bundle_policy_ == PeerConnectionInterface::kBundlePolicyMaxBundle) { |
| 1723 | bundle_group = desc->GetGroupByName(cricket::GROUP_TYPE_BUNDLE); |
| 1724 | if (!bundle_group) { |
| 1725 | LOG(LS_WARNING) << "max-bundle specified without BUNDLE specified"; |
| 1726 | return false; |
| 1727 | } |
| 1728 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1729 | // Creating the media channels and transport proxies. |
| 1730 | const cricket::ContentInfo* voice = cricket::GetFirstAudioContent(desc); |
Steve Anton | 169629a | 2017-08-30 17:36:36 -0700 | [diff] [blame] | 1731 | if (voice && !voice->rejected && !voice_channel()) { |
skvlad | 6c87a67 | 2016-05-17 17:49:52 -0700 | [diff] [blame] | 1732 | if (!CreateVoiceChannel(voice, |
| 1733 | GetBundleTransportName(voice, bundle_group))) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1734 | LOG(LS_ERROR) << "Failed to create voice channel."; |
| 1735 | return false; |
| 1736 | } |
| 1737 | } |
| 1738 | |
| 1739 | const cricket::ContentInfo* video = cricket::GetFirstVideoContent(desc); |
Steve Anton | 169629a | 2017-08-30 17:36:36 -0700 | [diff] [blame] | 1740 | if (video && !video->rejected && !video_channel()) { |
skvlad | 6c87a67 | 2016-05-17 17:49:52 -0700 | [diff] [blame] | 1741 | if (!CreateVideoChannel(video, |
| 1742 | GetBundleTransportName(video, bundle_group))) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1743 | LOG(LS_ERROR) << "Failed to create video channel."; |
| 1744 | return false; |
| 1745 | } |
| 1746 | } |
| 1747 | |
| 1748 | const cricket::ContentInfo* data = cricket::GetFirstDataContent(desc); |
deadbeef | 953c2ce | 2017-01-09 14:53:41 -0800 | [diff] [blame] | 1749 | if (data_channel_type_ != cricket::DCT_NONE && data && !data->rejected && |
| 1750 | !rtp_data_channel_ && !sctp_transport_) { |
skvlad | 6c87a67 | 2016-05-17 17:49:52 -0700 | [diff] [blame] | 1751 | if (!CreateDataChannel(data, GetBundleTransportName(data, bundle_group))) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1752 | LOG(LS_ERROR) << "Failed to create data channel."; |
| 1753 | return false; |
| 1754 | } |
| 1755 | } |
| 1756 | |
| 1757 | return true; |
| 1758 | } |
| 1759 | |
skvlad | 6c87a67 | 2016-05-17 17:49:52 -0700 | [diff] [blame] | 1760 | bool WebRtcSession::CreateVoiceChannel(const cricket::ContentInfo* content, |
| 1761 | const std::string* bundle_transport) { |
Steve Anton | 169629a | 2017-08-30 17:36:36 -0700 | [diff] [blame] | 1762 | // TODO(steveanton): Check to see if it's safe to create multiple voice |
| 1763 | // channels. |
| 1764 | RTC_DCHECK(voice_channels_.empty()); |
| 1765 | |
skvlad | 6c87a67 | 2016-05-17 17:49:52 -0700 | [diff] [blame] | 1766 | bool require_rtcp_mux = |
| 1767 | rtcp_mux_policy_ == PeerConnectionInterface::kRtcpMuxPolicyRequire; |
zhihuang | f5b251b | 2017-01-12 19:37:48 -0800 | [diff] [blame] | 1768 | |
| 1769 | std::string transport_name = |
| 1770 | bundle_transport ? *bundle_transport : content->name; |
| 1771 | |
zhihuang | b2cdd93 | 2017-01-19 16:54:25 -0800 | [diff] [blame] | 1772 | cricket::DtlsTransportInternal* rtp_dtls_transport = |
| 1773 | transport_controller_->CreateDtlsTransport( |
zhihuang | f5b251b | 2017-01-12 19:37:48 -0800 | [diff] [blame] | 1774 | transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP); |
zhihuang | b2cdd93 | 2017-01-19 16:54:25 -0800 | [diff] [blame] | 1775 | cricket::DtlsTransportInternal* rtcp_dtls_transport = nullptr; |
deadbeef | ac22f70 | 2017-01-12 21:59:29 -0800 | [diff] [blame] | 1776 | if (!require_rtcp_mux) { |
zhihuang | b2cdd93 | 2017-01-19 16:54:25 -0800 | [diff] [blame] | 1777 | rtcp_dtls_transport = transport_controller_->CreateDtlsTransport( |
zhihuang | f5b251b | 2017-01-12 19:37:48 -0800 | [diff] [blame] | 1778 | transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTCP); |
| 1779 | } |
| 1780 | |
Steve Anton | 169629a | 2017-08-30 17:36:36 -0700 | [diff] [blame] | 1781 | cricket::VoiceChannel* voice_channel = channel_manager_->CreateVoiceChannel( |
nisse | eaabdf6 | 2017-05-05 02:23:02 -0700 | [diff] [blame] | 1782 | call_, media_config_, rtp_dtls_transport, rtcp_dtls_transport, |
deadbeef | 1a2183d | 2017-02-10 23:44:49 -0800 | [diff] [blame] | 1783 | transport_controller_->signaling_thread(), content->name, SrtpRequired(), |
Steve Anton | 169629a | 2017-08-30 17:36:36 -0700 | [diff] [blame] | 1784 | audio_options_); |
| 1785 | if (!voice_channel) { |
zhihuang | b2cdd93 | 2017-01-19 16:54:25 -0800 | [diff] [blame] | 1786 | transport_controller_->DestroyDtlsTransport( |
deadbeef | bad5dad | 2017-01-17 18:32:35 -0800 | [diff] [blame] | 1787 | transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP); |
zhihuang | b2cdd93 | 2017-01-19 16:54:25 -0800 | [diff] [blame] | 1788 | if (rtcp_dtls_transport) { |
| 1789 | transport_controller_->DestroyDtlsTransport( |
Steve Anton | 24efa72 | 2017-08-29 18:12:51 -0700 | [diff] [blame] | 1790 | transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTCP); |
deadbeef | bad5dad | 2017-01-17 18:32:35 -0800 | [diff] [blame] | 1791 | } |
wu@webrtc.org | de30501 | 2013-10-31 15:40:38 +0000 | [diff] [blame] | 1792 | return false; |
pthatcher@webrtc.org | 4eeef58 | 2015-03-16 19:34:23 +0000 | [diff] [blame] | 1793 | } |
zhihuang | f5b251b | 2017-01-12 19:37:48 -0800 | [diff] [blame] | 1794 | |
Steve Anton | 169629a | 2017-08-30 17:36:36 -0700 | [diff] [blame] | 1795 | voice_channels_.push_back(voice_channel); |
| 1796 | |
| 1797 | voice_channel->SignalRtcpMuxFullyActive.connect( |
deadbeef | ac22f70 | 2017-01-12 21:59:29 -0800 | [diff] [blame] | 1798 | this, &WebRtcSession::DestroyRtcpTransport_n); |
Steve Anton | 169629a | 2017-08-30 17:36:36 -0700 | [diff] [blame] | 1799 | voice_channel->SignalDtlsSrtpSetupFailure.connect( |
deadbeef | 953c2ce | 2017-01-09 14:53:41 -0800 | [diff] [blame] | 1800 | this, &WebRtcSession::OnDtlsSrtpSetupFailure); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1801 | |
Steve Anton | 169629a | 2017-08-30 17:36:36 -0700 | [diff] [blame] | 1802 | // TODO(steveanton): This should signal which voice channel was created since |
| 1803 | // we can have multiple. |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1804 | SignalVoiceChannelCreated(); |
Steve Anton | 169629a | 2017-08-30 17:36:36 -0700 | [diff] [blame] | 1805 | voice_channel->SignalSentPacket.connect(this, &WebRtcSession::OnSentPacket_w); |
wu@webrtc.org | de30501 | 2013-10-31 15:40:38 +0000 | [diff] [blame] | 1806 | return true; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1807 | } |
| 1808 | |
skvlad | 6c87a67 | 2016-05-17 17:49:52 -0700 | [diff] [blame] | 1809 | bool WebRtcSession::CreateVideoChannel(const cricket::ContentInfo* content, |
| 1810 | const std::string* bundle_transport) { |
Steve Anton | 169629a | 2017-08-30 17:36:36 -0700 | [diff] [blame] | 1811 | // TODO(steveanton): Check to see if it's safe to create multiple video |
| 1812 | // channels. |
| 1813 | RTC_DCHECK(video_channels_.empty()); |
| 1814 | |
skvlad | 6c87a67 | 2016-05-17 17:49:52 -0700 | [diff] [blame] | 1815 | bool require_rtcp_mux = |
| 1816 | rtcp_mux_policy_ == PeerConnectionInterface::kRtcpMuxPolicyRequire; |
zhihuang | f5b251b | 2017-01-12 19:37:48 -0800 | [diff] [blame] | 1817 | |
| 1818 | std::string transport_name = |
| 1819 | bundle_transport ? *bundle_transport : content->name; |
| 1820 | |
zhihuang | b2cdd93 | 2017-01-19 16:54:25 -0800 | [diff] [blame] | 1821 | cricket::DtlsTransportInternal* rtp_dtls_transport = |
| 1822 | transport_controller_->CreateDtlsTransport( |
zhihuang | f5b251b | 2017-01-12 19:37:48 -0800 | [diff] [blame] | 1823 | transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP); |
zhihuang | b2cdd93 | 2017-01-19 16:54:25 -0800 | [diff] [blame] | 1824 | cricket::DtlsTransportInternal* rtcp_dtls_transport = nullptr; |
deadbeef | ac22f70 | 2017-01-12 21:59:29 -0800 | [diff] [blame] | 1825 | if (!require_rtcp_mux) { |
zhihuang | b2cdd93 | 2017-01-19 16:54:25 -0800 | [diff] [blame] | 1826 | rtcp_dtls_transport = transport_controller_->CreateDtlsTransport( |
zhihuang | f5b251b | 2017-01-12 19:37:48 -0800 | [diff] [blame] | 1827 | transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTCP); |
| 1828 | } |
| 1829 | |
Steve Anton | 169629a | 2017-08-30 17:36:36 -0700 | [diff] [blame] | 1830 | cricket::VideoChannel* video_channel = channel_manager_->CreateVideoChannel( |
nisse | eaabdf6 | 2017-05-05 02:23:02 -0700 | [diff] [blame] | 1831 | call_, media_config_, rtp_dtls_transport, rtcp_dtls_transport, |
deadbeef | 1a2183d | 2017-02-10 23:44:49 -0800 | [diff] [blame] | 1832 | transport_controller_->signaling_thread(), content->name, SrtpRequired(), |
Steve Anton | 169629a | 2017-08-30 17:36:36 -0700 | [diff] [blame] | 1833 | video_options_); |
zhihuang | f5b251b | 2017-01-12 19:37:48 -0800 | [diff] [blame] | 1834 | |
Steve Anton | 169629a | 2017-08-30 17:36:36 -0700 | [diff] [blame] | 1835 | if (!video_channel) { |
zhihuang | b2cdd93 | 2017-01-19 16:54:25 -0800 | [diff] [blame] | 1836 | transport_controller_->DestroyDtlsTransport( |
deadbeef | bad5dad | 2017-01-17 18:32:35 -0800 | [diff] [blame] | 1837 | transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP); |
zhihuang | b2cdd93 | 2017-01-19 16:54:25 -0800 | [diff] [blame] | 1838 | if (rtcp_dtls_transport) { |
| 1839 | transport_controller_->DestroyDtlsTransport( |
Steve Anton | 24efa72 | 2017-08-29 18:12:51 -0700 | [diff] [blame] | 1840 | transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTCP); |
deadbeef | bad5dad | 2017-01-17 18:32:35 -0800 | [diff] [blame] | 1841 | } |
pthatcher@webrtc.org | 4eeef58 | 2015-03-16 19:34:23 +0000 | [diff] [blame] | 1842 | return false; |
| 1843 | } |
zhihuang | f5b251b | 2017-01-12 19:37:48 -0800 | [diff] [blame] | 1844 | |
Steve Anton | 169629a | 2017-08-30 17:36:36 -0700 | [diff] [blame] | 1845 | video_channels_.push_back(video_channel); |
| 1846 | |
| 1847 | video_channel->SignalRtcpMuxFullyActive.connect( |
deadbeef | ac22f70 | 2017-01-12 21:59:29 -0800 | [diff] [blame] | 1848 | this, &WebRtcSession::DestroyRtcpTransport_n); |
Steve Anton | 169629a | 2017-08-30 17:36:36 -0700 | [diff] [blame] | 1849 | video_channel->SignalDtlsSrtpSetupFailure.connect( |
deadbeef | 953c2ce | 2017-01-09 14:53:41 -0800 | [diff] [blame] | 1850 | this, &WebRtcSession::OnDtlsSrtpSetupFailure); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1851 | |
Steve Anton | 169629a | 2017-08-30 17:36:36 -0700 | [diff] [blame] | 1852 | // TODO(steveanton): This should signal which video channel was created since |
| 1853 | // we can have multiple. |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1854 | SignalVideoChannelCreated(); |
Steve Anton | 169629a | 2017-08-30 17:36:36 -0700 | [diff] [blame] | 1855 | video_channel->SignalSentPacket.connect(this, &WebRtcSession::OnSentPacket_w); |
pthatcher@webrtc.org | 4eeef58 | 2015-03-16 19:34:23 +0000 | [diff] [blame] | 1856 | return true; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1857 | } |
| 1858 | |
skvlad | 6c87a67 | 2016-05-17 17:49:52 -0700 | [diff] [blame] | 1859 | bool WebRtcSession::CreateDataChannel(const cricket::ContentInfo* content, |
| 1860 | const std::string* bundle_transport) { |
deadbeef | 953c2ce | 2017-01-09 14:53:41 -0800 | [diff] [blame] | 1861 | const std::string transport_name = |
| 1862 | bundle_transport ? *bundle_transport : content->name; |
wu@webrtc.org | cecfd18 | 2013-10-30 05:18:12 +0000 | [diff] [blame] | 1863 | bool sctp = (data_channel_type_ == cricket::DCT_SCTP); |
deadbeef | c0dad89 | 2017-01-04 20:28:21 -0800 | [diff] [blame] | 1864 | if (sctp) { |
deadbeef | 953c2ce | 2017-01-09 14:53:41 -0800 | [diff] [blame] | 1865 | if (!sctp_factory_) { |
| 1866 | LOG(LS_ERROR) |
| 1867 | << "Trying to create SCTP transport, but didn't compile with " |
| 1868 | "SCTP support (HAVE_SCTP)"; |
| 1869 | return false; |
| 1870 | } |
| 1871 | if (!network_thread_->Invoke<bool>( |
| 1872 | RTC_FROM_HERE, rtc::Bind(&WebRtcSession::CreateSctpTransport_n, |
| 1873 | this, content->name, transport_name))) { |
| 1874 | return false; |
Steve Anton | 36b29d1 | 2017-10-30 09:57:42 -0700 | [diff] [blame] | 1875 | } |
deadbeef | 953c2ce | 2017-01-09 14:53:41 -0800 | [diff] [blame] | 1876 | } else { |
| 1877 | bool require_rtcp_mux = |
| 1878 | rtcp_mux_policy_ == PeerConnectionInterface::kRtcpMuxPolicyRequire; |
zhihuang | f5b251b | 2017-01-12 19:37:48 -0800 | [diff] [blame] | 1879 | |
| 1880 | std::string transport_name = |
| 1881 | bundle_transport ? *bundle_transport : content->name; |
zhihuang | b2cdd93 | 2017-01-19 16:54:25 -0800 | [diff] [blame] | 1882 | cricket::DtlsTransportInternal* rtp_dtls_transport = |
| 1883 | transport_controller_->CreateDtlsTransport( |
zhihuang | f5b251b | 2017-01-12 19:37:48 -0800 | [diff] [blame] | 1884 | transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP); |
zhihuang | b2cdd93 | 2017-01-19 16:54:25 -0800 | [diff] [blame] | 1885 | cricket::DtlsTransportInternal* rtcp_dtls_transport = nullptr; |
deadbeef | ac22f70 | 2017-01-12 21:59:29 -0800 | [diff] [blame] | 1886 | if (!require_rtcp_mux) { |
zhihuang | b2cdd93 | 2017-01-19 16:54:25 -0800 | [diff] [blame] | 1887 | rtcp_dtls_transport = transport_controller_->CreateDtlsTransport( |
zhihuang | f5b251b | 2017-01-12 19:37:48 -0800 | [diff] [blame] | 1888 | transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTCP); |
| 1889 | } |
| 1890 | |
deadbeef | 953c2ce | 2017-01-09 14:53:41 -0800 | [diff] [blame] | 1891 | rtp_data_channel_.reset(channel_manager_->CreateRtpDataChannel( |
nisse | eaabdf6 | 2017-05-05 02:23:02 -0700 | [diff] [blame] | 1892 | media_config_, rtp_dtls_transport, rtcp_dtls_transport, |
zhihuang | f5b251b | 2017-01-12 19:37:48 -0800 | [diff] [blame] | 1893 | transport_controller_->signaling_thread(), content->name, |
deadbeef | 1a2183d | 2017-02-10 23:44:49 -0800 | [diff] [blame] | 1894 | SrtpRequired())); |
zhihuang | f5b251b | 2017-01-12 19:37:48 -0800 | [diff] [blame] | 1895 | |
deadbeef | 953c2ce | 2017-01-09 14:53:41 -0800 | [diff] [blame] | 1896 | if (!rtp_data_channel_) { |
zhihuang | b2cdd93 | 2017-01-19 16:54:25 -0800 | [diff] [blame] | 1897 | transport_controller_->DestroyDtlsTransport( |
deadbeef | bad5dad | 2017-01-17 18:32:35 -0800 | [diff] [blame] | 1898 | transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP); |
zhihuang | b2cdd93 | 2017-01-19 16:54:25 -0800 | [diff] [blame] | 1899 | if (rtcp_dtls_transport) { |
| 1900 | transport_controller_->DestroyDtlsTransport( |
Steve Anton | 24efa72 | 2017-08-29 18:12:51 -0700 | [diff] [blame] | 1901 | transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTCP); |
deadbeef | bad5dad | 2017-01-17 18:32:35 -0800 | [diff] [blame] | 1902 | } |
deadbeef | 953c2ce | 2017-01-09 14:53:41 -0800 | [diff] [blame] | 1903 | return false; |
| 1904 | } |
zhihuang | f5b251b | 2017-01-12 19:37:48 -0800 | [diff] [blame] | 1905 | |
deadbeef | ac22f70 | 2017-01-12 21:59:29 -0800 | [diff] [blame] | 1906 | rtp_data_channel_->SignalRtcpMuxFullyActive.connect( |
| 1907 | this, &WebRtcSession::DestroyRtcpTransport_n); |
deadbeef | 953c2ce | 2017-01-09 14:53:41 -0800 | [diff] [blame] | 1908 | rtp_data_channel_->SignalDtlsSrtpSetupFailure.connect( |
| 1909 | this, &WebRtcSession::OnDtlsSrtpSetupFailure); |
| 1910 | rtp_data_channel_->SignalSentPacket.connect(this, |
| 1911 | &WebRtcSession::OnSentPacket_w); |
deadbeef | c0dad89 | 2017-01-04 20:28:21 -0800 | [diff] [blame] | 1912 | } |
| 1913 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1914 | SignalDataChannelCreated(); |
zhihuang | f5b251b | 2017-01-12 19:37:48 -0800 | [diff] [blame] | 1915 | |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 1916 | return true; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1917 | } |
| 1918 | |
stefan | f79ade1 | 2017-06-02 06:44:03 -0700 | [diff] [blame] | 1919 | Call::Stats WebRtcSession::GetCallStats() { |
| 1920 | if (!worker_thread()->IsCurrent()) { |
| 1921 | return worker_thread()->Invoke<Call::Stats>( |
| 1922 | RTC_FROM_HERE, rtc::Bind(&WebRtcSession::GetCallStats, this)); |
| 1923 | } |
zhihuang | 38ede13 | 2017-06-15 12:52:32 -0700 | [diff] [blame] | 1924 | if (!call_) |
| 1925 | return Call::Stats(); |
stefan | f79ade1 | 2017-06-02 06:44:03 -0700 | [diff] [blame] | 1926 | return call_->GetStats(); |
| 1927 | } |
| 1928 | |
hbos | df6075a | 2016-12-19 04:58:02 -0800 | [diff] [blame] | 1929 | std::unique_ptr<SessionStats> WebRtcSession::GetStats_n( |
| 1930 | const ChannelNamePairs& channel_name_pairs) { |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 1931 | RTC_DCHECK(network_thread()->IsCurrent()); |
hbos | df6075a | 2016-12-19 04:58:02 -0800 | [diff] [blame] | 1932 | std::unique_ptr<SessionStats> session_stats(new SessionStats()); |
| 1933 | for (const auto channel_name_pair : { &channel_name_pairs.voice, |
| 1934 | &channel_name_pairs.video, |
| 1935 | &channel_name_pairs.data }) { |
| 1936 | if (*channel_name_pair) { |
| 1937 | cricket::TransportStats transport_stats; |
| 1938 | if (!transport_controller_->GetStats((*channel_name_pair)->transport_name, |
| 1939 | &transport_stats)) { |
| 1940 | return nullptr; |
| 1941 | } |
| 1942 | session_stats->proxy_to_transport[(*channel_name_pair)->content_name] = |
| 1943 | (*channel_name_pair)->transport_name; |
| 1944 | session_stats->transport_stats[(*channel_name_pair)->transport_name] = |
| 1945 | std::move(transport_stats); |
| 1946 | } |
| 1947 | } |
| 1948 | return session_stats; |
| 1949 | } |
| 1950 | |
deadbeef | 953c2ce | 2017-01-09 14:53:41 -0800 | [diff] [blame] | 1951 | bool WebRtcSession::CreateSctpTransport_n(const std::string& content_name, |
| 1952 | const std::string& transport_name) { |
| 1953 | RTC_DCHECK(network_thread_->IsCurrent()); |
| 1954 | RTC_DCHECK(sctp_factory_); |
zhihuang | b2cdd93 | 2017-01-19 16:54:25 -0800 | [diff] [blame] | 1955 | cricket::DtlsTransportInternal* tc = |
| 1956 | transport_controller_->CreateDtlsTransport_n( |
deadbeef | 953c2ce | 2017-01-09 14:53:41 -0800 | [diff] [blame] | 1957 | transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP); |
| 1958 | sctp_transport_ = sctp_factory_->CreateSctpTransport(tc); |
| 1959 | RTC_DCHECK(sctp_transport_); |
| 1960 | sctp_invoker_.reset(new rtc::AsyncInvoker()); |
| 1961 | sctp_transport_->SignalReadyToSendData.connect( |
| 1962 | this, &WebRtcSession::OnSctpTransportReadyToSendData_n); |
| 1963 | sctp_transport_->SignalDataReceived.connect( |
| 1964 | this, &WebRtcSession::OnSctpTransportDataReceived_n); |
| 1965 | sctp_transport_->SignalStreamClosedRemotely.connect( |
| 1966 | this, &WebRtcSession::OnSctpStreamClosedRemotely_n); |
| 1967 | sctp_transport_name_ = rtc::Optional<std::string>(transport_name); |
| 1968 | sctp_content_name_ = rtc::Optional<std::string>(content_name); |
| 1969 | return true; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1970 | } |
| 1971 | |
deadbeef | 953c2ce | 2017-01-09 14:53:41 -0800 | [diff] [blame] | 1972 | void WebRtcSession::ChangeSctpTransport_n(const std::string& transport_name) { |
| 1973 | RTC_DCHECK(network_thread_->IsCurrent()); |
| 1974 | RTC_DCHECK(sctp_transport_); |
| 1975 | RTC_DCHECK(sctp_transport_name_); |
| 1976 | std::string old_sctp_transport_name = *sctp_transport_name_; |
| 1977 | sctp_transport_name_ = rtc::Optional<std::string>(transport_name); |
zhihuang | b2cdd93 | 2017-01-19 16:54:25 -0800 | [diff] [blame] | 1978 | cricket::DtlsTransportInternal* tc = |
| 1979 | transport_controller_->CreateDtlsTransport_n( |
deadbeef | 953c2ce | 2017-01-09 14:53:41 -0800 | [diff] [blame] | 1980 | transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP); |
| 1981 | sctp_transport_->SetTransportChannel(tc); |
zhihuang | b2cdd93 | 2017-01-19 16:54:25 -0800 | [diff] [blame] | 1982 | transport_controller_->DestroyDtlsTransport_n( |
deadbeef | 953c2ce | 2017-01-09 14:53:41 -0800 | [diff] [blame] | 1983 | old_sctp_transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP); |
| 1984 | } |
| 1985 | |
| 1986 | void WebRtcSession::DestroySctpTransport_n() { |
| 1987 | RTC_DCHECK(network_thread_->IsCurrent()); |
| 1988 | sctp_transport_.reset(nullptr); |
| 1989 | sctp_content_name_.reset(); |
| 1990 | sctp_transport_name_.reset(); |
| 1991 | sctp_invoker_.reset(nullptr); |
| 1992 | sctp_ready_to_send_data_ = false; |
| 1993 | } |
| 1994 | |
| 1995 | void WebRtcSession::OnSctpTransportReadyToSendData_n() { |
| 1996 | RTC_DCHECK(data_channel_type_ == cricket::DCT_SCTP); |
| 1997 | RTC_DCHECK(network_thread_->IsCurrent()); |
| 1998 | sctp_invoker_->AsyncInvoke<void>( |
| 1999 | RTC_FROM_HERE, signaling_thread_, |
| 2000 | rtc::Bind(&WebRtcSession::OnSctpTransportReadyToSendData_s, this, true)); |
| 2001 | } |
| 2002 | |
| 2003 | void WebRtcSession::OnSctpTransportReadyToSendData_s(bool ready) { |
| 2004 | RTC_DCHECK(signaling_thread_->IsCurrent()); |
| 2005 | sctp_ready_to_send_data_ = ready; |
| 2006 | SignalSctpReadyToSendData(ready); |
| 2007 | } |
| 2008 | |
| 2009 | void WebRtcSession::OnSctpTransportDataReceived_n( |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 2010 | const cricket::ReceiveDataParams& params, |
jbauch | eec21bd | 2016-03-20 06:15:43 -0700 | [diff] [blame] | 2011 | const rtc::CopyOnWriteBuffer& payload) { |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2012 | RTC_DCHECK(data_channel_type_ == cricket::DCT_SCTP); |
deadbeef | 953c2ce | 2017-01-09 14:53:41 -0800 | [diff] [blame] | 2013 | RTC_DCHECK(network_thread_->IsCurrent()); |
| 2014 | sctp_invoker_->AsyncInvoke<void>( |
| 2015 | RTC_FROM_HERE, signaling_thread_, |
| 2016 | rtc::Bind(&WebRtcSession::OnSctpTransportDataReceived_s, this, params, |
| 2017 | payload)); |
| 2018 | } |
| 2019 | |
| 2020 | void WebRtcSession::OnSctpTransportDataReceived_s( |
| 2021 | const cricket::ReceiveDataParams& params, |
| 2022 | const rtc::CopyOnWriteBuffer& payload) { |
| 2023 | RTC_DCHECK(signaling_thread_->IsCurrent()); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2024 | if (params.type == cricket::DMT_CONTROL && IsOpenMessage(payload)) { |
| 2025 | // Received OPEN message; parse and signal that a new data channel should |
| 2026 | // be created. |
| 2027 | std::string label; |
| 2028 | InternalDataChannelInit config; |
| 2029 | config.id = params.ssrc; |
| 2030 | if (!ParseDataChannelOpenMessage(payload, &label, &config)) { |
| 2031 | LOG(LS_WARNING) << "Failed to parse the OPEN message for sid " |
| 2032 | << params.ssrc; |
| 2033 | return; |
| 2034 | } |
| 2035 | config.open_handshake_role = InternalDataChannelInit::kAcker; |
| 2036 | SignalDataChannelOpenMessage(label, config); |
deadbeef | 953c2ce | 2017-01-09 14:53:41 -0800 | [diff] [blame] | 2037 | } else { |
| 2038 | // Otherwise just forward the signal. |
| 2039 | SignalSctpDataReceived(params, payload); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2040 | } |
deadbeef | 953c2ce | 2017-01-09 14:53:41 -0800 | [diff] [blame] | 2041 | } |
| 2042 | |
| 2043 | void WebRtcSession::OnSctpStreamClosedRemotely_n(int sid) { |
| 2044 | RTC_DCHECK(data_channel_type_ == cricket::DCT_SCTP); |
| 2045 | RTC_DCHECK(network_thread_->IsCurrent()); |
| 2046 | sctp_invoker_->AsyncInvoke<void>( |
| 2047 | RTC_FROM_HERE, signaling_thread_, |
| 2048 | rtc::Bind(&sigslot::signal1<int>::operator(), |
| 2049 | &SignalSctpStreamClosedRemotely, sid)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2050 | } |
| 2051 | |
henrike@webrtc.org | 1e09a71 | 2013-07-26 19:17:59 +0000 | [diff] [blame] | 2052 | // Returns false if bundle is enabled and rtcp_mux is disabled. |
sergeyu@chromium.org | 0be6aa0 | 2013-08-23 23:21:25 +0000 | [diff] [blame] | 2053 | bool WebRtcSession::ValidateBundleSettings(const SessionDescription* desc) { |
henrike@webrtc.org | 1e09a71 | 2013-07-26 19:17:59 +0000 | [diff] [blame] | 2054 | bool bundle_enabled = desc->HasGroup(cricket::GROUP_TYPE_BUNDLE); |
| 2055 | if (!bundle_enabled) |
| 2056 | return true; |
| 2057 | |
| 2058 | const cricket::ContentGroup* bundle_group = |
| 2059 | desc->GetGroupByName(cricket::GROUP_TYPE_BUNDLE); |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 2060 | RTC_DCHECK(bundle_group != NULL); |
henrike@webrtc.org | 1e09a71 | 2013-07-26 19:17:59 +0000 | [diff] [blame] | 2061 | |
| 2062 | const cricket::ContentInfos& contents = desc->contents(); |
| 2063 | for (cricket::ContentInfos::const_iterator citer = contents.begin(); |
| 2064 | citer != contents.end(); ++citer) { |
| 2065 | const cricket::ContentInfo* content = (&*citer); |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 2066 | RTC_DCHECK(content != NULL); |
henrike@webrtc.org | 1e09a71 | 2013-07-26 19:17:59 +0000 | [diff] [blame] | 2067 | if (bundle_group->HasContentName(content->name) && |
| 2068 | !content->rejected && content->type == cricket::NS_JINGLE_RTP) { |
| 2069 | if (!HasRtcpMuxEnabled(content)) |
| 2070 | return false; |
| 2071 | } |
| 2072 | } |
| 2073 | // RTCP-MUX is enabled in all the contents. |
| 2074 | return true; |
| 2075 | } |
| 2076 | |
| 2077 | bool WebRtcSession::HasRtcpMuxEnabled( |
| 2078 | const cricket::ContentInfo* content) { |
| 2079 | const cricket::MediaContentDescription* description = |
| 2080 | static_cast<cricket::MediaContentDescription*>(content->description); |
| 2081 | return description->rtcp_mux(); |
| 2082 | } |
| 2083 | |
sergeyu@chromium.org | 0be6aa0 | 2013-08-23 23:21:25 +0000 | [diff] [blame] | 2084 | bool WebRtcSession::ValidateSessionDescription( |
| 2085 | const SessionDescriptionInterface* sdesc, |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 2086 | cricket::ContentSource source, std::string* err_desc) { |
| 2087 | std::string type; |
deadbeef | d59daf8 | 2015-10-14 15:02:44 -0700 | [diff] [blame] | 2088 | if (error() != ERROR_NONE) { |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 2089 | return BadSdp(source, type, GetSessionErrorMsg(), err_desc); |
sergeyu@chromium.org | 0be6aa0 | 2013-08-23 23:21:25 +0000 | [diff] [blame] | 2090 | } |
| 2091 | |
| 2092 | if (!sdesc || !sdesc->description()) { |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 2093 | return BadSdp(source, type, kInvalidSdp, err_desc); |
sergeyu@chromium.org | 0be6aa0 | 2013-08-23 23:21:25 +0000 | [diff] [blame] | 2094 | } |
| 2095 | |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 2096 | type = sdesc->type(); |
sergeyu@chromium.org | 0be6aa0 | 2013-08-23 23:21:25 +0000 | [diff] [blame] | 2097 | Action action = GetAction(sdesc->type()); |
| 2098 | if (source == cricket::CS_LOCAL) { |
| 2099 | if (!ExpectSetLocalDescription(action)) |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 2100 | return BadLocalSdp(type, BadStateErrMsg(state()), err_desc); |
sergeyu@chromium.org | 0be6aa0 | 2013-08-23 23:21:25 +0000 | [diff] [blame] | 2101 | } else { |
| 2102 | if (!ExpectSetRemoteDescription(action)) |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 2103 | return BadRemoteSdp(type, BadStateErrMsg(state()), err_desc); |
sergeyu@chromium.org | 0be6aa0 | 2013-08-23 23:21:25 +0000 | [diff] [blame] | 2104 | } |
| 2105 | |
| 2106 | // Verify crypto settings. |
mallinath@webrtc.org | a27be8e | 2013-09-27 23:04:10 +0000 | [diff] [blame] | 2107 | std::string crypto_error; |
henrike@webrtc.org | b90991d | 2014-03-04 19:54:57 +0000 | [diff] [blame] | 2108 | if ((webrtc_session_desc_factory_->SdesPolicy() == cricket::SEC_REQUIRED || |
| 2109 | dtls_enabled_) && |
mallinath@webrtc.org | a27be8e | 2013-09-27 23:04:10 +0000 | [diff] [blame] | 2110 | !VerifyCrypto(sdesc->description(), dtls_enabled_, &crypto_error)) { |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 2111 | return BadSdp(source, type, crypto_error, err_desc); |
sergeyu@chromium.org | 0be6aa0 | 2013-08-23 23:21:25 +0000 | [diff] [blame] | 2112 | } |
| 2113 | |
mallinath@webrtc.org | 19f27e6 | 2013-10-13 17:18:27 +0000 | [diff] [blame] | 2114 | // Verify ice-ufrag and ice-pwd. |
| 2115 | if (!VerifyIceUfragPwdPresent(sdesc->description())) { |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 2116 | return BadSdp(source, type, kSdpWithoutIceUfragPwd, err_desc); |
mallinath@webrtc.org | 19f27e6 | 2013-10-13 17:18:27 +0000 | [diff] [blame] | 2117 | } |
| 2118 | |
sergeyu@chromium.org | 0be6aa0 | 2013-08-23 23:21:25 +0000 | [diff] [blame] | 2119 | if (!ValidateBundleSettings(sdesc->description())) { |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 2120 | return BadSdp(source, type, kBundleWithoutRtcpMux, err_desc); |
sergeyu@chromium.org | 0be6aa0 | 2013-08-23 23:21:25 +0000 | [diff] [blame] | 2121 | } |
| 2122 | |
skvlad | 6c87a67 | 2016-05-17 17:49:52 -0700 | [diff] [blame] | 2123 | // TODO(skvlad): When the local rtcp-mux policy is Require, reject any |
| 2124 | // m-lines that do not rtcp-mux enabled. |
| 2125 | |
sergeyu@chromium.org | 0be6aa0 | 2013-08-23 23:21:25 +0000 | [diff] [blame] | 2126 | // Verify m-lines in Answer when compared against Offer. |
Zhi Huang | 2a5e426 | 2017-09-14 01:15:03 -0700 | [diff] [blame] | 2127 | if (action == kAnswer || action == kPrAnswer) { |
sergeyu@chromium.org | 0be6aa0 | 2013-08-23 23:21:25 +0000 | [diff] [blame] | 2128 | const cricket::SessionDescription* offer_desc = |
deadbeef | fe4a8a4 | 2016-12-20 17:56:17 -0800 | [diff] [blame] | 2129 | (source == cricket::CS_LOCAL) ? remote_description()->description() |
| 2130 | : local_description()->description(); |
Zhi Huang | b2d355e | 2017-10-26 17:26:37 -0700 | [diff] [blame] | 2131 | if (!MediaSectionsHaveSameCount(offer_desc, sdesc->description()) || |
| 2132 | !MediaSectionsInSameOrder(offer_desc, sdesc->description())) { |
Zhi Huang | 2a5e426 | 2017-09-14 01:15:03 -0700 | [diff] [blame] | 2133 | return BadAnswerSdp(source, kMlineMismatchInAnswer, err_desc); |
| 2134 | } |
| 2135 | } else { |
Zhi Huang | b2d355e | 2017-10-26 17:26:37 -0700 | [diff] [blame] | 2136 | const cricket::SessionDescription* current_desc = nullptr; |
| 2137 | if (source == cricket::CS_LOCAL && local_description()) { |
| 2138 | current_desc = local_description()->description(); |
| 2139 | } else if (source == cricket::CS_REMOTE && remote_description()) { |
| 2140 | current_desc = remote_description()->description(); |
| 2141 | } |
| 2142 | // The re-offers should respect the order of m= sections in current |
Zhi Huang | 2a5e426 | 2017-09-14 01:15:03 -0700 | [diff] [blame] | 2143 | // description. See RFC3264 Section 8 paragraph 4 for more details. |
Zhi Huang | b2d355e | 2017-10-26 17:26:37 -0700 | [diff] [blame] | 2144 | if (current_desc && |
| 2145 | !MediaSectionsInSameOrder(current_desc, sdesc->description())) { |
Zhi Huang | 2a5e426 | 2017-09-14 01:15:03 -0700 | [diff] [blame] | 2146 | return BadOfferSdp(source, kMlineMismatchInSubsequentOffer, err_desc); |
sergeyu@chromium.org | 0be6aa0 | 2013-08-23 23:21:25 +0000 | [diff] [blame] | 2147 | } |
| 2148 | } |
| 2149 | |
| 2150 | return true; |
| 2151 | } |
| 2152 | |
| 2153 | bool WebRtcSession::ExpectSetLocalDescription(Action action) { |
| 2154 | return ((action == kOffer && state() == STATE_INIT) || |
| 2155 | // update local offer |
deadbeef | d59daf8 | 2015-10-14 15:02:44 -0700 | [diff] [blame] | 2156 | (action == kOffer && state() == STATE_SENTOFFER) || |
sergeyu@chromium.org | 0be6aa0 | 2013-08-23 23:21:25 +0000 | [diff] [blame] | 2157 | // update the current ongoing session. |
sergeyu@chromium.org | 0be6aa0 | 2013-08-23 23:21:25 +0000 | [diff] [blame] | 2158 | (action == kOffer && state() == STATE_INPROGRESS) || |
| 2159 | // accept remote offer |
deadbeef | d59daf8 | 2015-10-14 15:02:44 -0700 | [diff] [blame] | 2160 | (action == kAnswer && state() == STATE_RECEIVEDOFFER) || |
| 2161 | (action == kAnswer && state() == STATE_SENTPRANSWER) || |
| 2162 | (action == kPrAnswer && state() == STATE_RECEIVEDOFFER) || |
| 2163 | (action == kPrAnswer && state() == STATE_SENTPRANSWER)); |
sergeyu@chromium.org | 0be6aa0 | 2013-08-23 23:21:25 +0000 | [diff] [blame] | 2164 | } |
| 2165 | |
| 2166 | bool WebRtcSession::ExpectSetRemoteDescription(Action action) { |
| 2167 | return ((action == kOffer && state() == STATE_INIT) || |
| 2168 | // update remote offer |
deadbeef | d59daf8 | 2015-10-14 15:02:44 -0700 | [diff] [blame] | 2169 | (action == kOffer && state() == STATE_RECEIVEDOFFER) || |
sergeyu@chromium.org | 0be6aa0 | 2013-08-23 23:21:25 +0000 | [diff] [blame] | 2170 | // update the current ongoing session |
sergeyu@chromium.org | 0be6aa0 | 2013-08-23 23:21:25 +0000 | [diff] [blame] | 2171 | (action == kOffer && state() == STATE_INPROGRESS) || |
| 2172 | // accept local offer |
deadbeef | d59daf8 | 2015-10-14 15:02:44 -0700 | [diff] [blame] | 2173 | (action == kAnswer && state() == STATE_SENTOFFER) || |
| 2174 | (action == kAnswer && state() == STATE_RECEIVEDPRANSWER) || |
| 2175 | (action == kPrAnswer && state() == STATE_SENTOFFER) || |
| 2176 | (action == kPrAnswer && state() == STATE_RECEIVEDPRANSWER)); |
sergeyu@chromium.org | 0be6aa0 | 2013-08-23 23:21:25 +0000 | [diff] [blame] | 2177 | } |
| 2178 | |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 2179 | std::string WebRtcSession::GetSessionErrorMsg() { |
| 2180 | std::ostringstream desc; |
| 2181 | desc << kSessionError << GetErrorCodeString(error()) << ". "; |
| 2182 | desc << kSessionErrorDesc << error_desc() << "."; |
| 2183 | return desc.str(); |
| 2184 | } |
| 2185 | |
jiayl@webrtc.org | e10d28c | 2014-07-17 17:07:49 +0000 | [diff] [blame] | 2186 | // We need to check the local/remote description for the Transport instead of |
| 2187 | // the session, because a new Transport added during renegotiation may have |
| 2188 | // them unset while the session has them set from the previous negotiation. |
| 2189 | // Not doing so may trigger the auto generation of transport description and |
| 2190 | // mess up DTLS identity information, ICE credential, etc. |
| 2191 | bool WebRtcSession::ReadyToUseRemoteCandidate( |
| 2192 | const IceCandidateInterface* candidate, |
| 2193 | const SessionDescriptionInterface* remote_desc, |
| 2194 | bool* valid) { |
zhihuang | 9763d56 | 2016-08-05 11:14:50 -0700 | [diff] [blame] | 2195 | *valid = true; |
jiayl@webrtc.org | e10d28c | 2014-07-17 17:07:49 +0000 | [diff] [blame] | 2196 | |
| 2197 | const SessionDescriptionInterface* current_remote_desc = |
deadbeef | fe4a8a4 | 2016-12-20 17:56:17 -0800 | [diff] [blame] | 2198 | remote_desc ? remote_desc : remote_description(); |
jiayl@webrtc.org | e10d28c | 2014-07-17 17:07:49 +0000 | [diff] [blame] | 2199 | |
deadbeef | d59daf8 | 2015-10-14 15:02:44 -0700 | [diff] [blame] | 2200 | if (!current_remote_desc) { |
jiayl@webrtc.org | e10d28c | 2014-07-17 17:07:49 +0000 | [diff] [blame] | 2201 | return false; |
deadbeef | d59daf8 | 2015-10-14 15:02:44 -0700 | [diff] [blame] | 2202 | } |
jiayl@webrtc.org | e10d28c | 2014-07-17 17:07:49 +0000 | [diff] [blame] | 2203 | |
| 2204 | size_t mediacontent_index = |
| 2205 | static_cast<size_t>(candidate->sdp_mline_index()); |
| 2206 | size_t remote_content_size = |
| 2207 | current_remote_desc->description()->contents().size(); |
| 2208 | if (mediacontent_index >= remote_content_size) { |
Honghai Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 2209 | LOG(LS_ERROR) << "ReadyToUseRemoteCandidate: Invalid candidate media index " |
| 2210 | << mediacontent_index; |
jiayl@webrtc.org | e10d28c | 2014-07-17 17:07:49 +0000 | [diff] [blame] | 2211 | |
| 2212 | *valid = false; |
| 2213 | return false; |
| 2214 | } |
| 2215 | |
| 2216 | cricket::ContentInfo content = |
| 2217 | current_remote_desc->description()->contents()[mediacontent_index]; |
zhihuang | 9763d56 | 2016-08-05 11:14:50 -0700 | [diff] [blame] | 2218 | |
| 2219 | const std::string transport_name = GetTransportName(content.name); |
| 2220 | if (transport_name.empty()) { |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 2221 | return false; |
| 2222 | } |
zhihuang | 9763d56 | 2016-08-05 11:14:50 -0700 | [diff] [blame] | 2223 | return transport_controller_->ReadyForRemoteCandidates(transport_name); |
jiayl@webrtc.org | e10d28c | 2014-07-17 17:07:49 +0000 | [diff] [blame] | 2224 | } |
| 2225 | |
deadbeef | 7af91dd | 2016-12-13 11:29:11 -0800 | [diff] [blame] | 2226 | bool WebRtcSession::SrtpRequired() const { |
| 2227 | return dtls_enabled_ || |
| 2228 | webrtc_session_desc_factory_->SdesPolicy() == cricket::SEC_REQUIRED; |
| 2229 | } |
| 2230 | |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 2231 | void WebRtcSession::OnTransportControllerGatheringState( |
| 2232 | cricket::IceGatheringState state) { |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 2233 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 2234 | if (state == cricket::kIceGatheringGathering) { |
| 2235 | if (ice_observer_) { |
| 2236 | ice_observer_->OnIceGatheringChange( |
| 2237 | PeerConnectionInterface::kIceGatheringGathering); |
| 2238 | } |
| 2239 | } else if (state == cricket::kIceGatheringComplete) { |
| 2240 | if (ice_observer_) { |
| 2241 | ice_observer_->OnIceGatheringChange( |
| 2242 | PeerConnectionInterface::kIceGatheringComplete); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 2243 | } |
| 2244 | } |
| 2245 | } |
| 2246 | |
| 2247 | void WebRtcSession::ReportTransportStats() { |
| 2248 | // Use a set so we don't report the same stats twice if two channels share |
| 2249 | // a transport. |
| 2250 | std::set<std::string> transport_names; |
| 2251 | if (voice_channel()) { |
| 2252 | transport_names.insert(voice_channel()->transport_name()); |
| 2253 | } |
| 2254 | if (video_channel()) { |
| 2255 | transport_names.insert(video_channel()->transport_name()); |
| 2256 | } |
deadbeef | 953c2ce | 2017-01-09 14:53:41 -0800 | [diff] [blame] | 2257 | if (rtp_data_channel()) { |
| 2258 | transport_names.insert(rtp_data_channel()->transport_name()); |
| 2259 | } |
| 2260 | if (sctp_transport_name_) { |
| 2261 | transport_names.insert(*sctp_transport_name_); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 2262 | } |
| 2263 | for (const auto& name : transport_names) { |
| 2264 | cricket::TransportStats stats; |
deadbeef | d59daf8 | 2015-10-14 15:02:44 -0700 | [diff] [blame] | 2265 | if (transport_controller_->GetStats(name, &stats)) { |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 2266 | ReportBestConnectionState(stats); |
| 2267 | ReportNegotiatedCiphers(stats); |
| 2268 | } |
| 2269 | } |
| 2270 | } |
guoweis@webrtc.org | 7169afd | 2014-12-04 17:59:29 +0000 | [diff] [blame] | 2271 | // Walk through the ConnectionInfos to gather best connection usage |
| 2272 | // for IPv4 and IPv6. |
jbauch | ac8869e | 2015-07-03 01:36:14 -0700 | [diff] [blame] | 2273 | void WebRtcSession::ReportBestConnectionState( |
| 2274 | const cricket::TransportStats& stats) { |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 2275 | RTC_DCHECK(metrics_observer_ != NULL); |
guoweis@webrtc.org | 7169afd | 2014-12-04 17:59:29 +0000 | [diff] [blame] | 2276 | for (cricket::TransportChannelStatsList::const_iterator it = |
| 2277 | stats.channel_stats.begin(); |
| 2278 | it != stats.channel_stats.end(); ++it) { |
| 2279 | for (cricket::ConnectionInfos::const_iterator it_info = |
| 2280 | it->connection_infos.begin(); |
| 2281 | it_info != it->connection_infos.end(); ++it_info) { |
| 2282 | if (!it_info->best_connection) { |
| 2283 | continue; |
| 2284 | } |
Guo-wei Shieh | 3d564c1 | 2015-08-19 16:51:15 -0700 | [diff] [blame] | 2285 | |
| 2286 | PeerConnectionEnumCounterType type = kPeerConnectionEnumCounterMax; |
| 2287 | const cricket::Candidate& local = it_info->local_candidate; |
| 2288 | const cricket::Candidate& remote = it_info->remote_candidate; |
| 2289 | |
| 2290 | // Increment the counter for IceCandidatePairType. |
| 2291 | if (local.protocol() == cricket::TCP_PROTOCOL_NAME || |
| 2292 | (local.type() == RELAY_PORT_TYPE && |
| 2293 | local.relay_protocol() == cricket::TCP_PROTOCOL_NAME)) { |
| 2294 | type = kEnumCounterIceCandidatePairTypeTcp; |
| 2295 | } else if (local.protocol() == cricket::UDP_PROTOCOL_NAME) { |
| 2296 | type = kEnumCounterIceCandidatePairTypeUdp; |
guoweis@webrtc.org | 7169afd | 2014-12-04 17:59:29 +0000 | [diff] [blame] | 2297 | } else { |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 2298 | RTC_CHECK(0); |
guoweis@webrtc.org | 7169afd | 2014-12-04 17:59:29 +0000 | [diff] [blame] | 2299 | } |
Guo-wei Shieh | 3d564c1 | 2015-08-19 16:51:15 -0700 | [diff] [blame] | 2300 | metrics_observer_->IncrementEnumCounter( |
| 2301 | type, GetIceCandidatePairCounter(local, remote), |
| 2302 | kIceCandidatePairMax); |
| 2303 | |
| 2304 | // Increment the counter for IP type. |
| 2305 | if (local.address().family() == AF_INET) { |
Guo-wei Shieh | 3d564c1 | 2015-08-19 16:51:15 -0700 | [diff] [blame] | 2306 | metrics_observer_->IncrementEnumCounter( |
| 2307 | kEnumCounterAddressFamily, kBestConnections_IPv4, |
| 2308 | kPeerConnectionAddressFamilyCounter_Max); |
| 2309 | |
| 2310 | } else if (local.address().family() == AF_INET6) { |
Guo-wei Shieh | 3d564c1 | 2015-08-19 16:51:15 -0700 | [diff] [blame] | 2311 | metrics_observer_->IncrementEnumCounter( |
| 2312 | kEnumCounterAddressFamily, kBestConnections_IPv6, |
| 2313 | kPeerConnectionAddressFamilyCounter_Max); |
| 2314 | } else { |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 2315 | RTC_CHECK(0); |
Guo-wei Shieh | 3d564c1 | 2015-08-19 16:51:15 -0700 | [diff] [blame] | 2316 | } |
| 2317 | |
guoweis@webrtc.org | 7169afd | 2014-12-04 17:59:29 +0000 | [diff] [blame] | 2318 | return; |
| 2319 | } |
| 2320 | } |
| 2321 | } |
| 2322 | |
jbauch | ac8869e | 2015-07-03 01:36:14 -0700 | [diff] [blame] | 2323 | void WebRtcSession::ReportNegotiatedCiphers( |
| 2324 | const cricket::TransportStats& stats) { |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 2325 | RTC_DCHECK(metrics_observer_ != NULL); |
jbauch | ac8869e | 2015-07-03 01:36:14 -0700 | [diff] [blame] | 2326 | if (!dtls_enabled_ || stats.channel_stats.empty()) { |
| 2327 | return; |
| 2328 | } |
| 2329 | |
Guo-wei Shieh | 521ed7b | 2015-11-18 19:41:53 -0800 | [diff] [blame] | 2330 | int srtp_crypto_suite = stats.channel_stats[0].srtp_crypto_suite; |
| 2331 | int ssl_cipher_suite = stats.channel_stats[0].ssl_cipher_suite; |
| 2332 | if (srtp_crypto_suite == rtc::SRTP_INVALID_CRYPTO_SUITE && |
| 2333 | ssl_cipher_suite == rtc::TLS_NULL_WITH_NULL_NULL) { |
jbauch | ac8869e | 2015-07-03 01:36:14 -0700 | [diff] [blame] | 2334 | return; |
| 2335 | } |
| 2336 | |
Guo-wei Shieh | 456696a | 2015-09-30 21:48:54 -0700 | [diff] [blame] | 2337 | PeerConnectionEnumCounterType srtp_counter_type; |
| 2338 | PeerConnectionEnumCounterType ssl_counter_type; |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 2339 | if (stats.transport_name == cricket::CN_AUDIO) { |
Guo-wei Shieh | 456696a | 2015-09-30 21:48:54 -0700 | [diff] [blame] | 2340 | srtp_counter_type = kEnumCounterAudioSrtpCipher; |
| 2341 | ssl_counter_type = kEnumCounterAudioSslCipher; |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 2342 | } else if (stats.transport_name == cricket::CN_VIDEO) { |
Guo-wei Shieh | 456696a | 2015-09-30 21:48:54 -0700 | [diff] [blame] | 2343 | srtp_counter_type = kEnumCounterVideoSrtpCipher; |
| 2344 | ssl_counter_type = kEnumCounterVideoSslCipher; |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 2345 | } else if (stats.transport_name == cricket::CN_DATA) { |
Guo-wei Shieh | 456696a | 2015-09-30 21:48:54 -0700 | [diff] [blame] | 2346 | srtp_counter_type = kEnumCounterDataSrtpCipher; |
| 2347 | ssl_counter_type = kEnumCounterDataSslCipher; |
jbauch | ac8869e | 2015-07-03 01:36:14 -0700 | [diff] [blame] | 2348 | } else { |
| 2349 | RTC_NOTREACHED(); |
| 2350 | return; |
| 2351 | } |
| 2352 | |
Guo-wei Shieh | 521ed7b | 2015-11-18 19:41:53 -0800 | [diff] [blame] | 2353 | if (srtp_crypto_suite != rtc::SRTP_INVALID_CRYPTO_SUITE) { |
| 2354 | metrics_observer_->IncrementSparseEnumCounter(srtp_counter_type, |
| 2355 | srtp_crypto_suite); |
jbauch | ac8869e | 2015-07-03 01:36:14 -0700 | [diff] [blame] | 2356 | } |
Guo-wei Shieh | 521ed7b | 2015-11-18 19:41:53 -0800 | [diff] [blame] | 2357 | if (ssl_cipher_suite != rtc::TLS_NULL_WITH_NULL_NULL) { |
| 2358 | metrics_observer_->IncrementSparseEnumCounter(ssl_counter_type, |
| 2359 | ssl_cipher_suite); |
jbauch | ac8869e | 2015-07-03 01:36:14 -0700 | [diff] [blame] | 2360 | } |
| 2361 | } |
| 2362 | |
Danil Chapovalov | 33b01f2 | 2016-05-11 19:55:27 +0200 | [diff] [blame] | 2363 | void WebRtcSession::OnSentPacket_w(const rtc::SentPacket& sent_packet) { |
stefan | c1aeaf0 | 2015-10-15 07:26:07 -0700 | [diff] [blame] | 2364 | RTC_DCHECK(worker_thread()->IsCurrent()); |
stefan | f79ade1 | 2017-06-02 06:44:03 -0700 | [diff] [blame] | 2365 | RTC_DCHECK(call_); |
nisse | eaabdf6 | 2017-05-05 02:23:02 -0700 | [diff] [blame] | 2366 | call_->OnSentPacket(sent_packet); |
stefan | c1aeaf0 | 2015-10-15 07:26:07 -0700 | [diff] [blame] | 2367 | } |
| 2368 | |
zhihuang | 9763d56 | 2016-08-05 11:14:50 -0700 | [diff] [blame] | 2369 | const std::string WebRtcSession::GetTransportName( |
| 2370 | const std::string& content_name) { |
| 2371 | cricket::BaseChannel* channel = GetChannel(content_name); |
| 2372 | if (!channel) { |
deadbeef | 953c2ce | 2017-01-09 14:53:41 -0800 | [diff] [blame] | 2373 | if (sctp_transport_) { |
| 2374 | RTC_DCHECK(sctp_content_name_); |
| 2375 | RTC_DCHECK(sctp_transport_name_); |
| 2376 | if (content_name == *sctp_content_name_) { |
| 2377 | return *sctp_transport_name_; |
| 2378 | } |
| 2379 | } |
zhihuang | 9763d56 | 2016-08-05 11:14:50 -0700 | [diff] [blame] | 2380 | // Return an empty string if failed to retrieve the transport name. |
| 2381 | return ""; |
| 2382 | } |
| 2383 | return channel->transport_name(); |
| 2384 | } |
zhihuang | d82eee0 | 2016-08-26 11:25:05 -0700 | [diff] [blame] | 2385 | |
deadbeef | ac22f70 | 2017-01-12 21:59:29 -0800 | [diff] [blame] | 2386 | void WebRtcSession::DestroyRtcpTransport_n(const std::string& transport_name) { |
nisse | a9dd4a1 | 2017-01-13 07:08:34 -0800 | [diff] [blame] | 2387 | RTC_DCHECK(network_thread()->IsCurrent()); |
zhihuang | b2cdd93 | 2017-01-19 16:54:25 -0800 | [diff] [blame] | 2388 | transport_controller_->DestroyDtlsTransport_n( |
zhihuang | f5b251b | 2017-01-12 19:37:48 -0800 | [diff] [blame] | 2389 | transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTCP); |
| 2390 | } |
| 2391 | |
Steve Anton | 169629a | 2017-08-30 17:36:36 -0700 | [diff] [blame] | 2392 | void WebRtcSession::RemoveAndDestroyVideoChannel( |
| 2393 | cricket::VideoChannel* video_channel) { |
| 2394 | auto it = |
| 2395 | std::find(video_channels_.begin(), video_channels_.end(), video_channel); |
| 2396 | RTC_DCHECK(it != video_channels_.end()); |
| 2397 | if (it == video_channels_.end()) { |
| 2398 | return; |
| 2399 | } |
| 2400 | video_channels_.erase(it); |
| 2401 | DestroyVideoChannel(video_channel); |
| 2402 | } |
| 2403 | |
| 2404 | void WebRtcSession::DestroyVideoChannel(cricket::VideoChannel* video_channel) { |
| 2405 | // TODO(steveanton): This should take an identifier for the video channel |
| 2406 | // since we now support more than one. |
zhihuang | f5b251b | 2017-01-12 19:37:48 -0800 | [diff] [blame] | 2407 | SignalVideoChannelDestroyed(); |
Steve Anton | 169629a | 2017-08-30 17:36:36 -0700 | [diff] [blame] | 2408 | RTC_DCHECK(video_channel->rtp_dtls_transport()); |
| 2409 | const std::string transport_name = |
| 2410 | video_channel->rtp_dtls_transport()->transport_name(); |
| 2411 | const bool need_to_delete_rtcp = |
| 2412 | (video_channel->rtcp_dtls_transport() != nullptr); |
| 2413 | // The above need to be cached before destroying the video channel so that we |
| 2414 | // do not access uninitialized memory. |
| 2415 | channel_manager_->DestroyVideoChannel(video_channel); |
zhihuang | b2cdd93 | 2017-01-19 16:54:25 -0800 | [diff] [blame] | 2416 | transport_controller_->DestroyDtlsTransport( |
deadbeef | bad5dad | 2017-01-17 18:32:35 -0800 | [diff] [blame] | 2417 | transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP); |
zhihuang | f5b251b | 2017-01-12 19:37:48 -0800 | [diff] [blame] | 2418 | if (need_to_delete_rtcp) { |
zhihuang | b2cdd93 | 2017-01-19 16:54:25 -0800 | [diff] [blame] | 2419 | transport_controller_->DestroyDtlsTransport( |
deadbeef | bad5dad | 2017-01-17 18:32:35 -0800 | [diff] [blame] | 2420 | transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTCP); |
zhihuang | f5b251b | 2017-01-12 19:37:48 -0800 | [diff] [blame] | 2421 | } |
| 2422 | } |
| 2423 | |
Steve Anton | 169629a | 2017-08-30 17:36:36 -0700 | [diff] [blame] | 2424 | void WebRtcSession::RemoveAndDestroyVoiceChannel( |
| 2425 | cricket::VoiceChannel* voice_channel) { |
| 2426 | auto it = |
| 2427 | std::find(voice_channels_.begin(), voice_channels_.end(), voice_channel); |
| 2428 | RTC_DCHECK(it != voice_channels_.end()); |
| 2429 | if (it == voice_channels_.end()) { |
| 2430 | return; |
| 2431 | } |
| 2432 | voice_channels_.erase(it); |
| 2433 | DestroyVoiceChannel(voice_channel); |
| 2434 | } |
| 2435 | |
| 2436 | void WebRtcSession::DestroyVoiceChannel(cricket::VoiceChannel* voice_channel) { |
| 2437 | // TODO(steveanton): This should take an identifier for the voice channel |
| 2438 | // since we now support more than one. |
zhihuang | f5b251b | 2017-01-12 19:37:48 -0800 | [diff] [blame] | 2439 | SignalVoiceChannelDestroyed(); |
Steve Anton | 169629a | 2017-08-30 17:36:36 -0700 | [diff] [blame] | 2440 | RTC_DCHECK(voice_channel->rtp_dtls_transport()); |
| 2441 | const std::string transport_name = |
| 2442 | voice_channel->rtp_dtls_transport()->transport_name(); |
| 2443 | const bool need_to_delete_rtcp = |
| 2444 | (voice_channel->rtcp_dtls_transport() != nullptr); |
| 2445 | // The above need to be cached before destroying the video channel so that we |
| 2446 | // do not access uninitialized memory. |
| 2447 | channel_manager_->DestroyVoiceChannel(voice_channel); |
zhihuang | b2cdd93 | 2017-01-19 16:54:25 -0800 | [diff] [blame] | 2448 | transport_controller_->DestroyDtlsTransport( |
deadbeef | bad5dad | 2017-01-17 18:32:35 -0800 | [diff] [blame] | 2449 | transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP); |
zhihuang | f5b251b | 2017-01-12 19:37:48 -0800 | [diff] [blame] | 2450 | if (need_to_delete_rtcp) { |
zhihuang | b2cdd93 | 2017-01-19 16:54:25 -0800 | [diff] [blame] | 2451 | transport_controller_->DestroyDtlsTransport( |
deadbeef | bad5dad | 2017-01-17 18:32:35 -0800 | [diff] [blame] | 2452 | transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTCP); |
zhihuang | f5b251b | 2017-01-12 19:37:48 -0800 | [diff] [blame] | 2453 | } |
| 2454 | } |
| 2455 | |
| 2456 | void WebRtcSession::DestroyDataChannel() { |
| 2457 | SignalDataChannelDestroyed(); |
zhihuang | b2cdd93 | 2017-01-19 16:54:25 -0800 | [diff] [blame] | 2458 | RTC_DCHECK(rtp_data_channel_->rtp_dtls_transport()); |
zhihuang | f5b251b | 2017-01-12 19:37:48 -0800 | [diff] [blame] | 2459 | std::string transport_name; |
zhihuang | b2cdd93 | 2017-01-19 16:54:25 -0800 | [diff] [blame] | 2460 | transport_name = rtp_data_channel_->rtp_dtls_transport()->transport_name(); |
| 2461 | bool need_to_delete_rtcp = |
| 2462 | (rtp_data_channel_->rtcp_dtls_transport() != nullptr); |
zhihuang | f5b251b | 2017-01-12 19:37:48 -0800 | [diff] [blame] | 2463 | channel_manager_->DestroyRtpDataChannel(rtp_data_channel_.release()); |
zhihuang | b2cdd93 | 2017-01-19 16:54:25 -0800 | [diff] [blame] | 2464 | transport_controller_->DestroyDtlsTransport( |
deadbeef | bad5dad | 2017-01-17 18:32:35 -0800 | [diff] [blame] | 2465 | transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP); |
zhihuang | f5b251b | 2017-01-12 19:37:48 -0800 | [diff] [blame] | 2466 | if (need_to_delete_rtcp) { |
zhihuang | b2cdd93 | 2017-01-19 16:54:25 -0800 | [diff] [blame] | 2467 | transport_controller_->DestroyDtlsTransport( |
deadbeef | bad5dad | 2017-01-17 18:32:35 -0800 | [diff] [blame] | 2468 | transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTCP); |
zhihuang | f5b251b | 2017-01-12 19:37:48 -0800 | [diff] [blame] | 2469 | } |
| 2470 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2471 | } // namespace webrtc |