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