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 | |
Henrik Kjellander | 15583c1 | 2016-02-10 10:53:12 +0100 | [diff] [blame] | 11 | #include "webrtc/api/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 | |
Henrik Kjellander | 15583c1 | 2016-02-10 10:53:12 +0100 | [diff] [blame] | 20 | #include "webrtc/api/jsepicecandidate.h" |
| 21 | #include "webrtc/api/jsepsessiondescription.h" |
Henrik Kjellander | 15583c1 | 2016-02-10 10:53:12 +0100 | [diff] [blame] | 22 | #include "webrtc/api/peerconnectioninterface.h" |
| 23 | #include "webrtc/api/sctputils.h" |
| 24 | #include "webrtc/api/webrtcsessiondescriptionfactory.h" |
kjellander@webrtc.org | 7ffeab5 | 2016-02-26 22:46:09 +0100 | [diff] [blame] | 25 | #include "webrtc/audio_sink.h" |
buildbot@webrtc.org | a09a999 | 2014-08-13 17:26:08 +0000 | [diff] [blame] | 26 | #include "webrtc/base/basictypes.h" |
jbauch | ac8869e | 2015-07-03 01:36:14 -0700 | [diff] [blame] | 27 | #include "webrtc/base/checks.h" |
buildbot@webrtc.org | a09a999 | 2014-08-13 17:26:08 +0000 | [diff] [blame] | 28 | #include "webrtc/base/helpers.h" |
| 29 | #include "webrtc/base/logging.h" |
| 30 | #include "webrtc/base/stringencode.h" |
| 31 | #include "webrtc/base/stringutils.h" |
stefan | c1aeaf0 | 2015-10-15 07:26:07 -0700 | [diff] [blame] | 32 | #include "webrtc/call.h" |
kjellander | f475277 | 2016-03-02 05:42:30 -0800 | [diff] [blame] | 33 | #include "webrtc/media/base/mediaconstants.h" |
kjellander | a96e2d7 | 2016-02-04 23:52:28 -0800 | [diff] [blame] | 34 | #include "webrtc/media/base/videocapturer.h" |
pthatcher@webrtc.org | 40b276e | 2014-12-12 02:44:30 +0000 | [diff] [blame] | 35 | #include "webrtc/p2p/base/portallocator.h" |
stefan | c1aeaf0 | 2015-10-15 07:26:07 -0700 | [diff] [blame] | 36 | #include "webrtc/p2p/base/transportchannel.h" |
kjellander@webrtc.org | 9b8df25 | 2016-02-12 06:47:59 +0100 | [diff] [blame] | 37 | #include "webrtc/pc/channel.h" |
| 38 | #include "webrtc/pc/channelmanager.h" |
| 39 | #include "webrtc/pc/mediasession.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 |
henrike@webrtc.org | 1e09a71 | 2013-07-26 19:17:59 +0000 | [diff] [blame] | 55 | const char kBundleWithoutRtcpMux[] = "RTCP-MUX must be enabled when BUNDLE " |
| 56 | "is enabled."; |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 57 | const char kCreateChannelFailed[] = "Failed to create channels."; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 58 | const char kInvalidCandidates[] = "Description contains invalid candidates."; |
| 59 | const char kInvalidSdp[] = "Invalid session description."; |
| 60 | const char kMlineMismatch[] = |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 61 | "Offer and answer descriptions m-lines are not matching. Rejecting answer."; |
| 62 | const char kPushDownTDFailed[] = |
| 63 | "Failed to push down transport description:"; |
henrike@webrtc.org | b90991d | 2014-03-04 19:54:57 +0000 | [diff] [blame] | 64 | const char kSdpWithoutDtlsFingerprint[] = |
| 65 | "Called with SDP without DTLS fingerprint."; |
| 66 | const char kSdpWithoutSdesCrypto[] = |
| 67 | "Called with SDP without SDES crypto."; |
mallinath@webrtc.org | 19f27e6 | 2013-10-13 17:18:27 +0000 | [diff] [blame] | 68 | const char kSdpWithoutIceUfragPwd[] = |
henrike@webrtc.org | b90991d | 2014-03-04 19:54:57 +0000 | [diff] [blame] | 69 | "Called with SDP without ice-ufrag and ice-pwd."; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 70 | const char kSessionError[] = "Session error code: "; |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 71 | const char kSessionErrorDesc[] = "Session error description: "; |
pthatcher@webrtc.org | 4eeef58 | 2015-03-16 19:34:23 +0000 | [diff] [blame] | 72 | const char kDtlsSetupFailureRtp[] = |
| 73 | "Couldn't set up DTLS-SRTP on RTP channel."; |
| 74 | const char kDtlsSetupFailureRtcp[] = |
| 75 | "Couldn't set up DTLS-SRTP on RTCP channel."; |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 76 | const char kEnableBundleFailed[] = "Failed to enable BUNDLE."; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 77 | |
Guo-wei Shieh | 3d564c1 | 2015-08-19 16:51:15 -0700 | [diff] [blame] | 78 | IceCandidatePairType GetIceCandidatePairCounter( |
| 79 | const cricket::Candidate& local, |
| 80 | const cricket::Candidate& remote) { |
| 81 | const auto& l = local.type(); |
| 82 | const auto& r = remote.type(); |
| 83 | const auto& host = LOCAL_PORT_TYPE; |
| 84 | const auto& srflx = STUN_PORT_TYPE; |
| 85 | const auto& relay = RELAY_PORT_TYPE; |
| 86 | const auto& prflx = PRFLX_PORT_TYPE; |
Guo-wei Shieh | 3cc834a | 2015-09-04 15:52:14 -0700 | [diff] [blame] | 87 | if (l == host && r == host) { |
| 88 | bool local_private = IPIsPrivate(local.address().ipaddr()); |
| 89 | bool remote_private = IPIsPrivate(remote.address().ipaddr()); |
| 90 | if (local_private) { |
| 91 | if (remote_private) { |
| 92 | return kIceCandidatePairHostPrivateHostPrivate; |
| 93 | } else { |
| 94 | return kIceCandidatePairHostPrivateHostPublic; |
| 95 | } |
| 96 | } else { |
| 97 | if (remote_private) { |
| 98 | return kIceCandidatePairHostPublicHostPrivate; |
| 99 | } else { |
| 100 | return kIceCandidatePairHostPublicHostPublic; |
| 101 | } |
| 102 | } |
| 103 | } |
Guo-wei Shieh | 3d564c1 | 2015-08-19 16:51:15 -0700 | [diff] [blame] | 104 | if (l == host && r == srflx) |
| 105 | return kIceCandidatePairHostSrflx; |
| 106 | if (l == host && r == relay) |
| 107 | return kIceCandidatePairHostRelay; |
| 108 | if (l == host && r == prflx) |
| 109 | return kIceCandidatePairHostPrflx; |
| 110 | if (l == srflx && r == host) |
| 111 | return kIceCandidatePairSrflxHost; |
| 112 | if (l == srflx && r == srflx) |
| 113 | return kIceCandidatePairSrflxSrflx; |
| 114 | if (l == srflx && r == relay) |
| 115 | return kIceCandidatePairSrflxRelay; |
| 116 | if (l == srflx && r == prflx) |
| 117 | return kIceCandidatePairSrflxPrflx; |
| 118 | if (l == relay && r == host) |
| 119 | return kIceCandidatePairRelayHost; |
| 120 | if (l == relay && r == srflx) |
| 121 | return kIceCandidatePairRelaySrflx; |
| 122 | if (l == relay && r == relay) |
| 123 | return kIceCandidatePairRelayRelay; |
| 124 | if (l == relay && r == prflx) |
| 125 | return kIceCandidatePairRelayPrflx; |
| 126 | if (l == prflx && r == host) |
| 127 | return kIceCandidatePairPrflxHost; |
| 128 | if (l == prflx && r == srflx) |
| 129 | return kIceCandidatePairPrflxSrflx; |
| 130 | if (l == prflx && r == relay) |
| 131 | return kIceCandidatePairPrflxRelay; |
| 132 | return kIceCandidatePairMax; |
| 133 | } |
| 134 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 135 | // Compares |answer| against |offer|. Comparision is done |
| 136 | // for number of m-lines in answer against offer. If matches true will be |
| 137 | // returned otherwise false. |
| 138 | static bool VerifyMediaDescriptions( |
| 139 | const SessionDescription* answer, const SessionDescription* offer) { |
| 140 | if (offer->contents().size() != answer->contents().size()) |
| 141 | return false; |
| 142 | |
| 143 | for (size_t i = 0; i < offer->contents().size(); ++i) { |
| 144 | if ((offer->contents()[i].name) != answer->contents()[i].name) { |
| 145 | return false; |
| 146 | } |
wu@webrtc.org | 4e39307 | 2014-04-07 17:04:35 +0000 | [diff] [blame] | 147 | const MediaContentDescription* offer_mdesc = |
| 148 | static_cast<const MediaContentDescription*>( |
| 149 | offer->contents()[i].description); |
| 150 | const MediaContentDescription* answer_mdesc = |
| 151 | static_cast<const MediaContentDescription*>( |
| 152 | answer->contents()[i].description); |
| 153 | if (offer_mdesc->type() != answer_mdesc->type()) { |
| 154 | return false; |
| 155 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 156 | } |
| 157 | return true; |
| 158 | } |
| 159 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 160 | // Checks that each non-rejected content has SDES crypto keys or a DTLS |
| 161 | // fingerprint. Mismatches, such as replying with a DTLS fingerprint to SDES |
| 162 | // keys, will be caught in Transport negotiation, and backstopped by Channel's |
| 163 | // |secure_required| check. |
mallinath@webrtc.org | a27be8e | 2013-09-27 23:04:10 +0000 | [diff] [blame] | 164 | static bool VerifyCrypto(const SessionDescription* desc, |
| 165 | bool dtls_enabled, |
| 166 | std::string* error) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 167 | const ContentInfos& contents = desc->contents(); |
| 168 | for (size_t index = 0; index < contents.size(); ++index) { |
| 169 | const ContentInfo* cinfo = &contents[index]; |
| 170 | if (cinfo->rejected) { |
| 171 | continue; |
| 172 | } |
| 173 | |
| 174 | // If the content isn't rejected, crypto must be present. |
| 175 | const MediaContentDescription* media = |
| 176 | static_cast<const MediaContentDescription*>(cinfo->description); |
| 177 | const TransportInfo* tinfo = desc->GetTransportInfoByName(cinfo->name); |
| 178 | if (!media || !tinfo) { |
| 179 | // Something is not right. |
| 180 | LOG(LS_ERROR) << kInvalidSdp; |
mallinath@webrtc.org | a27be8e | 2013-09-27 23:04:10 +0000 | [diff] [blame] | 181 | *error = kInvalidSdp; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 182 | return false; |
| 183 | } |
henrike@webrtc.org | b90991d | 2014-03-04 19:54:57 +0000 | [diff] [blame] | 184 | if (dtls_enabled) { |
mallinath@webrtc.org | a27be8e | 2013-09-27 23:04:10 +0000 | [diff] [blame] | 185 | if (!tinfo->description.identity_fingerprint) { |
henrike@webrtc.org | b90991d | 2014-03-04 19:54:57 +0000 | [diff] [blame] | 186 | LOG(LS_WARNING) << |
| 187 | "Session description must have DTLS fingerprint if DTLS enabled."; |
| 188 | *error = kSdpWithoutDtlsFingerprint; |
mallinath@webrtc.org | a27be8e | 2013-09-27 23:04:10 +0000 | [diff] [blame] | 189 | return false; |
| 190 | } |
henrike@webrtc.org | b90991d | 2014-03-04 19:54:57 +0000 | [diff] [blame] | 191 | } else { |
| 192 | if (media->cryptos().empty()) { |
mallinath@webrtc.org | a27be8e | 2013-09-27 23:04:10 +0000 | [diff] [blame] | 193 | LOG(LS_WARNING) << |
| 194 | "Session description must have SDES when DTLS disabled."; |
henrike@webrtc.org | b90991d | 2014-03-04 19:54:57 +0000 | [diff] [blame] | 195 | *error = kSdpWithoutSdesCrypto; |
mallinath@webrtc.org | a27be8e | 2013-09-27 23:04:10 +0000 | [diff] [blame] | 196 | return false; |
| 197 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 198 | } |
| 199 | } |
| 200 | |
| 201 | return true; |
| 202 | } |
| 203 | |
mallinath@webrtc.org | 19f27e6 | 2013-10-13 17:18:27 +0000 | [diff] [blame] | 204 | // Checks that each non-rejected content has ice-ufrag and ice-pwd set. |
| 205 | static bool VerifyIceUfragPwdPresent(const SessionDescription* desc) { |
| 206 | const ContentInfos& contents = desc->contents(); |
| 207 | for (size_t index = 0; index < contents.size(); ++index) { |
| 208 | const ContentInfo* cinfo = &contents[index]; |
| 209 | if (cinfo->rejected) { |
| 210 | continue; |
| 211 | } |
| 212 | |
| 213 | // If the content isn't rejected, ice-ufrag and ice-pwd must be present. |
| 214 | const TransportInfo* tinfo = desc->GetTransportInfoByName(cinfo->name); |
| 215 | if (!tinfo) { |
| 216 | // Something is not right. |
| 217 | LOG(LS_ERROR) << kInvalidSdp; |
| 218 | return false; |
| 219 | } |
| 220 | if (tinfo->description.ice_ufrag.empty() || |
| 221 | tinfo->description.ice_pwd.empty()) { |
| 222 | LOG(LS_ERROR) << "Session description must have ice ufrag and pwd."; |
| 223 | return false; |
| 224 | } |
| 225 | } |
| 226 | return true; |
| 227 | } |
| 228 | |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 229 | // Forces |sdesc->crypto_required| to the appropriate state based on the |
| 230 | // current security policy, to ensure a failure occurs if there is an error |
| 231 | // in crypto negotiation. |
| 232 | // Called when processing the local session description. |
henrike@webrtc.org | b90991d | 2014-03-04 19:54:57 +0000 | [diff] [blame] | 233 | static void UpdateSessionDescriptionSecurePolicy(cricket::CryptoType type, |
| 234 | SessionDescription* sdesc) { |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 235 | if (!sdesc) { |
| 236 | return; |
| 237 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 238 | |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 239 | // Updating the |crypto_required_| in MediaContentDescription to the |
| 240 | // appropriate state based on the current security policy. |
| 241 | for (cricket::ContentInfos::iterator iter = sdesc->contents().begin(); |
| 242 | iter != sdesc->contents().end(); ++iter) { |
| 243 | if (cricket::IsMediaContent(&*iter)) { |
| 244 | MediaContentDescription* mdesc = |
| 245 | static_cast<MediaContentDescription*> (iter->description); |
| 246 | if (mdesc) { |
henrike@webrtc.org | b90991d | 2014-03-04 19:54:57 +0000 | [diff] [blame] | 247 | mdesc->set_crypto_required(type); |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 248 | } |
| 249 | } |
| 250 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 251 | } |
| 252 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 253 | static bool GetAudioSsrcByTrackId(const SessionDescription* session_description, |
| 254 | const std::string& track_id, |
| 255 | uint32_t* ssrc) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 256 | const cricket::ContentInfo* audio_info = |
| 257 | cricket::GetFirstAudioContent(session_description); |
| 258 | if (!audio_info) { |
| 259 | LOG(LS_ERROR) << "Audio not used in this call"; |
| 260 | return false; |
| 261 | } |
| 262 | |
| 263 | const cricket::MediaContentDescription* audio_content = |
| 264 | static_cast<const cricket::MediaContentDescription*>( |
| 265 | audio_info->description); |
tommi@webrtc.org | 586f2ed | 2015-01-22 23:00:41 +0000 | [diff] [blame] | 266 | const cricket::StreamParams* stream = |
| 267 | cricket::GetStreamByIds(audio_content->streams(), "", track_id); |
| 268 | if (!stream) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 269 | return false; |
| 270 | } |
tommi@webrtc.org | 586f2ed | 2015-01-22 23:00:41 +0000 | [diff] [blame] | 271 | |
| 272 | *ssrc = stream->first_ssrc(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 273 | return true; |
| 274 | } |
| 275 | |
| 276 | static bool GetTrackIdBySsrc(const SessionDescription* session_description, |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 277 | uint32_t ssrc, |
| 278 | std::string* track_id) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 279 | ASSERT(track_id != NULL); |
| 280 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 281 | const cricket::ContentInfo* audio_info = |
| 282 | cricket::GetFirstAudioContent(session_description); |
jiayl@webrtc.org | e21cc9a | 2014-08-28 22:21:34 +0000 | [diff] [blame] | 283 | if (audio_info) { |
| 284 | const cricket::MediaContentDescription* audio_content = |
| 285 | static_cast<const cricket::MediaContentDescription*>( |
| 286 | audio_info->description); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 287 | |
tommi@webrtc.org | 586f2ed | 2015-01-22 23:00:41 +0000 | [diff] [blame] | 288 | const auto* found = |
| 289 | cricket::GetStreamBySsrc(audio_content->streams(), ssrc); |
| 290 | if (found) { |
| 291 | *track_id = found->id; |
jiayl@webrtc.org | e21cc9a | 2014-08-28 22:21:34 +0000 | [diff] [blame] | 292 | return true; |
| 293 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | const cricket::ContentInfo* video_info = |
| 297 | cricket::GetFirstVideoContent(session_description); |
jiayl@webrtc.org | e21cc9a | 2014-08-28 22:21:34 +0000 | [diff] [blame] | 298 | if (video_info) { |
| 299 | const cricket::MediaContentDescription* video_content = |
| 300 | static_cast<const cricket::MediaContentDescription*>( |
| 301 | video_info->description); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 302 | |
tommi@webrtc.org | 586f2ed | 2015-01-22 23:00:41 +0000 | [diff] [blame] | 303 | const auto* found = |
| 304 | cricket::GetStreamBySsrc(video_content->streams(), ssrc); |
| 305 | if (found) { |
| 306 | *track_id = found->id; |
jiayl@webrtc.org | e21cc9a | 2014-08-28 22:21:34 +0000 | [diff] [blame] | 307 | return true; |
| 308 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 309 | } |
| 310 | return false; |
| 311 | } |
| 312 | |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 313 | static bool BadSdp(const std::string& source, |
| 314 | const std::string& type, |
| 315 | const std::string& reason, |
| 316 | std::string* err_desc) { |
| 317 | std::ostringstream desc; |
deadbeef | d59daf8 | 2015-10-14 15:02:44 -0700 | [diff] [blame] | 318 | desc << "Failed to set " << source; |
| 319 | if (!type.empty()) { |
| 320 | desc << " " << type; |
| 321 | } |
| 322 | desc << " sdp: " << reason; |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 323 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 324 | if (err_desc) { |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 325 | *err_desc = desc.str(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 326 | } |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 327 | LOG(LS_ERROR) << desc.str(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 328 | return false; |
| 329 | } |
| 330 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 331 | static bool BadSdp(cricket::ContentSource source, |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 332 | const std::string& type, |
| 333 | const std::string& reason, |
| 334 | std::string* err_desc) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 335 | if (source == cricket::CS_LOCAL) { |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 336 | return BadSdp("local", type, reason, err_desc); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 337 | } else { |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 338 | return BadSdp("remote", type, reason, err_desc); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 339 | } |
| 340 | } |
| 341 | |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 342 | static bool BadLocalSdp(const std::string& type, |
| 343 | const std::string& reason, |
| 344 | std::string* err_desc) { |
| 345 | return BadSdp(cricket::CS_LOCAL, type, reason, err_desc); |
| 346 | } |
| 347 | |
| 348 | static bool BadRemoteSdp(const std::string& type, |
| 349 | const std::string& reason, |
| 350 | std::string* err_desc) { |
| 351 | return BadSdp(cricket::CS_REMOTE, type, reason, err_desc); |
| 352 | } |
| 353 | |
| 354 | static bool BadOfferSdp(cricket::ContentSource source, |
| 355 | const std::string& reason, |
| 356 | std::string* err_desc) { |
| 357 | return BadSdp(source, SessionDescriptionInterface::kOffer, reason, err_desc); |
| 358 | } |
| 359 | |
| 360 | static bool BadPranswerSdp(cricket::ContentSource source, |
| 361 | const std::string& reason, |
| 362 | std::string* err_desc) { |
| 363 | return BadSdp(source, SessionDescriptionInterface::kPrAnswer, |
| 364 | reason, err_desc); |
| 365 | } |
| 366 | |
| 367 | static bool BadAnswerSdp(cricket::ContentSource source, |
| 368 | const std::string& reason, |
| 369 | std::string* err_desc) { |
| 370 | return BadSdp(source, SessionDescriptionInterface::kAnswer, reason, err_desc); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 371 | } |
| 372 | |
deadbeef | d59daf8 | 2015-10-14 15:02:44 -0700 | [diff] [blame] | 373 | #define GET_STRING_OF_STATE(state) \ |
| 374 | case webrtc::WebRtcSession::state: \ |
| 375 | result = #state; \ |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 376 | break; |
| 377 | |
deadbeef | d59daf8 | 2015-10-14 15:02:44 -0700 | [diff] [blame] | 378 | static std::string GetStateString(webrtc::WebRtcSession::State state) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 379 | std::string result; |
| 380 | switch (state) { |
| 381 | GET_STRING_OF_STATE(STATE_INIT) |
deadbeef | d59daf8 | 2015-10-14 15:02:44 -0700 | [diff] [blame] | 382 | GET_STRING_OF_STATE(STATE_SENTOFFER) |
| 383 | GET_STRING_OF_STATE(STATE_RECEIVEDOFFER) |
| 384 | GET_STRING_OF_STATE(STATE_SENTPRANSWER) |
| 385 | GET_STRING_OF_STATE(STATE_RECEIVEDPRANSWER) |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 386 | GET_STRING_OF_STATE(STATE_INPROGRESS) |
deadbeef | d59daf8 | 2015-10-14 15:02:44 -0700 | [diff] [blame] | 387 | GET_STRING_OF_STATE(STATE_CLOSED) |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 388 | default: |
| 389 | ASSERT(false); |
| 390 | break; |
| 391 | } |
| 392 | return result; |
| 393 | } |
| 394 | |
deadbeef | d59daf8 | 2015-10-14 15:02:44 -0700 | [diff] [blame] | 395 | #define GET_STRING_OF_ERROR_CODE(err) \ |
| 396 | case webrtc::WebRtcSession::err: \ |
| 397 | result = #err; \ |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 398 | break; |
| 399 | |
deadbeef | d59daf8 | 2015-10-14 15:02:44 -0700 | [diff] [blame] | 400 | static std::string GetErrorCodeString(webrtc::WebRtcSession::Error err) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 401 | std::string result; |
| 402 | switch (err) { |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 403 | GET_STRING_OF_ERROR_CODE(ERROR_NONE) |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 404 | GET_STRING_OF_ERROR_CODE(ERROR_CONTENT) |
| 405 | GET_STRING_OF_ERROR_CODE(ERROR_TRANSPORT) |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 406 | default: |
deadbeef | d59daf8 | 2015-10-14 15:02:44 -0700 | [diff] [blame] | 407 | RTC_DCHECK(false); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 408 | break; |
| 409 | } |
| 410 | return result; |
| 411 | } |
| 412 | |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 413 | static std::string MakeErrorString(const std::string& error, |
| 414 | const std::string& desc) { |
| 415 | std::ostringstream ret; |
| 416 | ret << error << " " << desc; |
| 417 | return ret.str(); |
| 418 | } |
| 419 | |
| 420 | static std::string MakeTdErrorString(const std::string& desc) { |
| 421 | return MakeErrorString(kPushDownTDFailed, desc); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 422 | } |
| 423 | |
deadbeef | 0ed85b2 | 2016-02-23 17:24:52 -0800 | [diff] [blame] | 424 | // Returns true if |new_desc| requests an ICE restart (i.e., new ufrag/pwd). |
| 425 | bool CheckForRemoteIceRestart(const SessionDescriptionInterface* old_desc, |
| 426 | const SessionDescriptionInterface* new_desc, |
| 427 | const std::string& content_name) { |
| 428 | if (!old_desc) { |
honghaiz | 503726c | 2015-07-31 12:37:38 -0700 | [diff] [blame] | 429 | return false; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 430 | } |
deadbeef | 0ed85b2 | 2016-02-23 17:24:52 -0800 | [diff] [blame] | 431 | const SessionDescription* new_sd = new_desc->description(); |
| 432 | const SessionDescription* old_sd = old_desc->description(); |
| 433 | const ContentInfo* cinfo = new_sd->GetContentByName(content_name); |
| 434 | if (!cinfo || cinfo->rejected) { |
| 435 | return false; |
| 436 | } |
| 437 | // If the content isn't rejected, check if ufrag and password has changed. |
| 438 | const cricket::TransportDescription* new_transport_desc = |
| 439 | new_sd->GetTransportDescriptionByName(content_name); |
| 440 | const cricket::TransportDescription* old_transport_desc = |
| 441 | old_sd->GetTransportDescriptionByName(content_name); |
| 442 | if (!new_transport_desc || !old_transport_desc) { |
| 443 | // No transport description exists. This is not an ICE restart. |
| 444 | return false; |
| 445 | } |
| 446 | if (cricket::IceCredentialsChanged( |
| 447 | old_transport_desc->ice_ufrag, old_transport_desc->ice_pwd, |
| 448 | new_transport_desc->ice_ufrag, new_transport_desc->ice_pwd)) { |
| 449 | LOG(LS_INFO) << "Remote peer requests ICE restart for " << content_name |
| 450 | << "."; |
| 451 | return true; |
| 452 | } |
| 453 | return false; |
| 454 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 455 | |
stefan | c1aeaf0 | 2015-10-15 07:26:07 -0700 | [diff] [blame] | 456 | WebRtcSession::WebRtcSession(webrtc::MediaControllerInterface* media_controller, |
danilchap | e9021a3 | 2016-05-17 01:52:02 -0700 | [diff] [blame] | 457 | rtc::Thread* network_thread, |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 458 | rtc::Thread* worker_thread, |
danilchap | e9021a3 | 2016-05-17 01:52:02 -0700 | [diff] [blame] | 459 | rtc::Thread* signaling_thread, |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 460 | cricket::PortAllocator* port_allocator) |
danilchap | e9021a3 | 2016-05-17 01:52:02 -0700 | [diff] [blame] | 461 | : worker_thread_(worker_thread), |
| 462 | signaling_thread_(signaling_thread), |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 463 | // RFC 3264: The numeric value of the session id and version in the |
| 464 | // o line MUST be representable with a "64 bit signed integer". |
| 465 | // Due to this constraint session id |sid_| is max limited to LLONG_MAX. |
deadbeef | d59daf8 | 2015-10-14 15:02:44 -0700 | [diff] [blame] | 466 | sid_(rtc::ToString(rtc::CreateRandomId64() & LLONG_MAX)), |
| 467 | transport_controller_(new cricket::TransportController(signaling_thread, |
danilchap | e9021a3 | 2016-05-17 01:52:02 -0700 | [diff] [blame] | 468 | network_thread, |
deadbeef | d59daf8 | 2015-10-14 15:02:44 -0700 | [diff] [blame] | 469 | port_allocator)), |
stefan | c1aeaf0 | 2015-10-15 07:26:07 -0700 | [diff] [blame] | 470 | media_controller_(media_controller), |
| 471 | channel_manager_(media_controller_->channel_manager()), |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 472 | ice_observer_(NULL), |
| 473 | ice_connection_state_(PeerConnectionInterface::kIceConnectionNew), |
Peter Thatcher | 5436051 | 2015-07-08 11:08:35 -0700 | [diff] [blame] | 474 | ice_connection_receiving_(true), |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 475 | older_version_remote_peer_(false), |
mallinath@webrtc.org | a27be8e | 2013-09-27 23:04:10 +0000 | [diff] [blame] | 476 | dtls_enabled_(false), |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 477 | data_channel_type_(cricket::DCT_NONE), |
guoweis@webrtc.org | 7169afd | 2014-12-04 17:59:29 +0000 | [diff] [blame] | 478 | metrics_observer_(NULL) { |
deadbeef | d59daf8 | 2015-10-14 15:02:44 -0700 | [diff] [blame] | 479 | transport_controller_->SetIceRole(cricket::ICEROLE_CONTROLLED); |
| 480 | transport_controller_->SignalConnectionState.connect( |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 481 | this, &WebRtcSession::OnTransportControllerConnectionState); |
deadbeef | d59daf8 | 2015-10-14 15:02:44 -0700 | [diff] [blame] | 482 | transport_controller_->SignalReceiving.connect( |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 483 | this, &WebRtcSession::OnTransportControllerReceiving); |
deadbeef | d59daf8 | 2015-10-14 15:02:44 -0700 | [diff] [blame] | 484 | transport_controller_->SignalGatheringState.connect( |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 485 | this, &WebRtcSession::OnTransportControllerGatheringState); |
deadbeef | d59daf8 | 2015-10-14 15:02:44 -0700 | [diff] [blame] | 486 | transport_controller_->SignalCandidatesGathered.connect( |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 487 | this, &WebRtcSession::OnTransportControllerCandidatesGathered); |
Honghai Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 488 | transport_controller_->SignalCandidatesRemoved.connect( |
| 489 | this, &WebRtcSession::OnTransportControllerCandidatesRemoved); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 490 | } |
| 491 | |
| 492 | WebRtcSession::~WebRtcSession() { |
tommi | 0f620f4 | 2015-07-09 03:25:02 -0700 | [diff] [blame] | 493 | ASSERT(signaling_thread()->IsCurrent()); |
buildbot@webrtc.org | b4c7b09 | 2014-08-25 12:11:58 +0000 | [diff] [blame] | 494 | // Destroy video_channel_ first since it may have a pointer to the |
| 495 | // voice_channel_. |
pthatcher@webrtc.org | 4eeef58 | 2015-03-16 19:34:23 +0000 | [diff] [blame] | 496 | if (video_channel_) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 497 | SignalVideoChannelDestroyed(); |
| 498 | channel_manager_->DestroyVideoChannel(video_channel_.release()); |
| 499 | } |
pthatcher@webrtc.org | 4eeef58 | 2015-03-16 19:34:23 +0000 | [diff] [blame] | 500 | if (voice_channel_) { |
buildbot@webrtc.org | b4c7b09 | 2014-08-25 12:11:58 +0000 | [diff] [blame] | 501 | SignalVoiceChannelDestroyed(); |
Fredrik Solenberg | 709ed67 | 2015-09-15 12:26:33 +0200 | [diff] [blame] | 502 | channel_manager_->DestroyVoiceChannel(voice_channel_.release()); |
buildbot@webrtc.org | b4c7b09 | 2014-08-25 12:11:58 +0000 | [diff] [blame] | 503 | } |
pthatcher@webrtc.org | 4eeef58 | 2015-03-16 19:34:23 +0000 | [diff] [blame] | 504 | if (data_channel_) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 505 | SignalDataChannelDestroyed(); |
| 506 | channel_manager_->DestroyDataChannel(data_channel_.release()); |
| 507 | } |
deadbeef | 057ecf0 | 2016-01-20 14:30:43 -0800 | [diff] [blame] | 508 | SignalDestroyed(); |
deadbeef | d59daf8 | 2015-10-14 15:02:44 -0700 | [diff] [blame] | 509 | |
| 510 | LOG(LS_INFO) << "Session: " << id() << " is destroyed."; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 511 | } |
| 512 | |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 513 | bool WebRtcSession::Initialize( |
wu@webrtc.org | 97077a3 | 2013-10-25 21:18:33 +0000 | [diff] [blame] | 514 | const PeerConnectionFactoryInterface::Options& options, |
Henrik Boström | d03c23b | 2016-06-01 11:44:18 +0200 | [diff] [blame] | 515 | std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator, |
Henrik Lundin | 64dad83 | 2015-05-11 12:44:23 +0200 | [diff] [blame] | 516 | const PeerConnectionInterface::RTCConfiguration& rtc_configuration) { |
| 517 | bundle_policy_ = rtc_configuration.bundle_policy; |
Peter Thatcher | af55ccc | 2015-05-21 07:48:41 -0700 | [diff] [blame] | 518 | rtcp_mux_policy_ = rtc_configuration.rtcp_mux_policy; |
deadbeef | d59daf8 | 2015-10-14 15:02:44 -0700 | [diff] [blame] | 519 | transport_controller_->SetSslMaxProtocolVersion(options.ssl_max_version); |
pthatcher@webrtc.org | 877ac76 | 2015-02-04 22:03:09 +0000 | [diff] [blame] | 520 | |
Henrik Boström | 87713d0 | 2015-08-25 09:53:21 +0200 | [diff] [blame] | 521 | // Obtain a certificate from RTCConfiguration if any were provided (optional). |
| 522 | rtc::scoped_refptr<rtc::RTCCertificate> certificate; |
| 523 | if (!rtc_configuration.certificates.empty()) { |
| 524 | // TODO(hbos,torbjorng): Decide on certificate-selection strategy instead of |
| 525 | // just picking the first one. The decision should be made based on the DTLS |
| 526 | // handshake. The DTLS negotiations need to know about all certificates. |
| 527 | certificate = rtc_configuration.certificates[0]; |
| 528 | } |
| 529 | |
honghaiz | 1f429e3 | 2015-09-28 07:57:34 -0700 | [diff] [blame] | 530 | SetIceConfig(ParseIceConfig(rtc_configuration)); |
honghaiz | 4edc39c | 2015-09-01 09:53:56 -0700 | [diff] [blame] | 531 | |
henrike@webrtc.org | b90991d | 2014-03-04 19:54:57 +0000 | [diff] [blame] | 532 | if (options.disable_encryption) { |
| 533 | dtls_enabled_ = false; |
| 534 | } else { |
Henrik Boström | 87713d0 | 2015-08-25 09:53:21 +0200 | [diff] [blame] | 535 | // 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] | 536 | dtls_enabled_ = (cert_generator || certificate); |
hta | a2a49d9 | 2016-03-04 02:51:39 -0800 | [diff] [blame] | 537 | // |rtc_configuration| can override the default |dtls_enabled_| value. |
| 538 | if (rtc_configuration.enable_dtls_srtp) { |
| 539 | dtls_enabled_ = *(rtc_configuration.enable_dtls_srtp); |
henrike@webrtc.org | b90991d | 2014-03-04 19:54:57 +0000 | [diff] [blame] | 540 | } |
sergeyu@chromium.org | a59696b | 2013-09-13 23:48:58 +0000 | [diff] [blame] | 541 | } |
| 542 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 543 | // Enable creation of RTP data channels if the kEnableRtpDataChannels is set. |
wu@webrtc.org | 97077a3 | 2013-10-25 21:18:33 +0000 | [diff] [blame] | 544 | // It takes precendence over the disable_sctp_data_channels |
| 545 | // PeerConnectionFactoryInterface::Options. |
hta | a2a49d9 | 2016-03-04 02:51:39 -0800 | [diff] [blame] | 546 | if (rtc_configuration.enable_rtp_data_channel) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 547 | data_channel_type_ = cricket::DCT_RTP; |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 548 | } else { |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 549 | // DTLS has to be enabled to use SCTP. |
wu@webrtc.org | 97077a3 | 2013-10-25 21:18:33 +0000 | [diff] [blame] | 550 | if (!options.disable_sctp_data_channels && dtls_enabled_) { |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 551 | data_channel_type_ = cricket::DCT_SCTP; |
| 552 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 553 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 554 | |
hta | a2a49d9 | 2016-03-04 02:51:39 -0800 | [diff] [blame] | 555 | video_options_.screencast_min_bitrate_kbps = |
| 556 | rtc_configuration.screencast_min_bitrate; |
| 557 | audio_options_.combined_audio_video_bwe = |
| 558 | rtc_configuration.combined_audio_video_bwe; |
buildbot@webrtc.org | b4c7b09 | 2014-08-25 12:11:58 +0000 | [diff] [blame] | 559 | |
kwiberg | 102c6a6 | 2015-10-30 02:47:38 -0700 | [diff] [blame] | 560 | audio_options_.audio_jitter_buffer_max_packets = |
Karl Wiberg | be57983 | 2015-11-10 22:34:18 +0100 | [diff] [blame] | 561 | rtc::Optional<int>(rtc_configuration.audio_jitter_buffer_max_packets); |
Henrik Lundin | 64dad83 | 2015-05-11 12:44:23 +0200 | [diff] [blame] | 562 | |
Karl Wiberg | be57983 | 2015-11-10 22:34:18 +0100 | [diff] [blame] | 563 | audio_options_.audio_jitter_buffer_fast_accelerate = rtc::Optional<bool>( |
| 564 | rtc_configuration.audio_jitter_buffer_fast_accelerate); |
Henrik Lundin | 5263b3c | 2015-06-01 10:29:41 +0200 | [diff] [blame] | 565 | |
Henrik Boström | 87713d0 | 2015-08-25 09:53:21 +0200 | [diff] [blame] | 566 | if (!dtls_enabled_) { |
| 567 | // Construct with DTLS disabled. |
| 568 | webrtc_session_desc_factory_.reset(new WebRtcSessionDescriptionFactory( |
Henrik Boström | d03c23b | 2016-06-01 11:44:18 +0200 | [diff] [blame] | 569 | signaling_thread(), channel_manager_, this, id(), |
| 570 | std::unique_ptr<rtc::RTCCertificateGeneratorInterface>())); |
Henrik Boström | 87713d0 | 2015-08-25 09:53:21 +0200 | [diff] [blame] | 571 | } else { |
| 572 | // Construct with DTLS enabled. |
| 573 | if (!certificate) { |
Henrik Boström | 87713d0 | 2015-08-25 09:53:21 +0200 | [diff] [blame] | 574 | webrtc_session_desc_factory_.reset(new WebRtcSessionDescriptionFactory( |
Henrik Boström | d03c23b | 2016-06-01 11:44:18 +0200 | [diff] [blame] | 575 | signaling_thread(), channel_manager_, this, id(), |
| 576 | std::move(cert_generator))); |
Henrik Boström | 87713d0 | 2015-08-25 09:53:21 +0200 | [diff] [blame] | 577 | } else { |
| 578 | // Use the already generated certificate. |
| 579 | webrtc_session_desc_factory_.reset(new WebRtcSessionDescriptionFactory( |
Henrik Boström | d03c23b | 2016-06-01 11:44:18 +0200 | [diff] [blame] | 580 | signaling_thread(), channel_manager_, this, id(), certificate)); |
Henrik Boström | 87713d0 | 2015-08-25 09:53:21 +0200 | [diff] [blame] | 581 | } |
| 582 | } |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 583 | |
Henrik Boström | d828198 | 2015-08-27 10:12:24 +0200 | [diff] [blame] | 584 | webrtc_session_desc_factory_->SignalCertificateReady.connect( |
| 585 | this, &WebRtcSession::OnCertificateReady); |
mallinath@webrtc.org | 7e809c3 | 2013-09-30 18:59:08 +0000 | [diff] [blame] | 586 | |
wu@webrtc.org | 97077a3 | 2013-10-25 21:18:33 +0000 | [diff] [blame] | 587 | if (options.disable_encryption) { |
henrike@webrtc.org | b90991d | 2014-03-04 19:54:57 +0000 | [diff] [blame] | 588 | webrtc_session_desc_factory_->SetSdesPolicy(cricket::SEC_DISABLED); |
mallinath@webrtc.org | 7e809c3 | 2013-09-30 18:59:08 +0000 | [diff] [blame] | 589 | } |
Guo-wei Shieh | fe3bc9d | 2015-08-20 08:48:20 -0700 | [diff] [blame] | 590 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 591 | return true; |
| 592 | } |
| 593 | |
deadbeef | d59daf8 | 2015-10-14 15:02:44 -0700 | [diff] [blame] | 594 | void WebRtcSession::Close() { |
| 595 | SetState(STATE_CLOSED); |
| 596 | RemoveUnusedChannels(nullptr); |
pthatcher@webrtc.org | 4eeef58 | 2015-03-16 19:34:23 +0000 | [diff] [blame] | 597 | ASSERT(!voice_channel_); |
| 598 | ASSERT(!video_channel_); |
| 599 | ASSERT(!data_channel_); |
solenberg | 03d6d57 | 2016-03-01 12:42:03 -0800 | [diff] [blame] | 600 | media_controller_->Close(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 601 | } |
| 602 | |
deadbeef | 0ed85b2 | 2016-02-23 17:24:52 -0800 | [diff] [blame] | 603 | cricket::BaseChannel* WebRtcSession::GetChannel( |
| 604 | const std::string& content_name) { |
| 605 | if (voice_channel() && voice_channel()->content_name() == content_name) { |
| 606 | return voice_channel(); |
| 607 | } |
| 608 | if (video_channel() && video_channel()->content_name() == content_name) { |
| 609 | return video_channel(); |
| 610 | } |
| 611 | if (data_channel() && data_channel()->content_name() == content_name) { |
| 612 | return data_channel(); |
| 613 | } |
| 614 | return nullptr; |
| 615 | } |
| 616 | |
henrike@webrtc.org | b90991d | 2014-03-04 19:54:57 +0000 | [diff] [blame] | 617 | void WebRtcSession::SetSdesPolicy(cricket::SecurePolicy secure_policy) { |
| 618 | webrtc_session_desc_factory_->SetSdesPolicy(secure_policy); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 619 | } |
| 620 | |
henrike@webrtc.org | b90991d | 2014-03-04 19:54:57 +0000 | [diff] [blame] | 621 | cricket::SecurePolicy WebRtcSession::SdesPolicy() const { |
| 622 | return webrtc_session_desc_factory_->SdesPolicy(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 623 | } |
| 624 | |
Taylor Brandstetter | f475d36 | 2016-01-08 15:35:57 -0800 | [diff] [blame] | 625 | bool WebRtcSession::GetSslRole(const std::string& transport_name, |
| 626 | rtc::SSLRole* role) { |
deadbeef | d59daf8 | 2015-10-14 15:02:44 -0700 | [diff] [blame] | 627 | if (!local_desc_ || !remote_desc_) { |
sergeyu@chromium.org | 0be6aa0 | 2013-08-23 23:21:25 +0000 | [diff] [blame] | 628 | LOG(LS_INFO) << "Local and Remote descriptions must be applied to get " |
| 629 | << "SSL Role of the session."; |
| 630 | return false; |
| 631 | } |
| 632 | |
Taylor Brandstetter | f475d36 | 2016-01-08 15:35:57 -0800 | [diff] [blame] | 633 | return transport_controller_->GetSslRole(transport_name, role); |
| 634 | } |
| 635 | |
| 636 | bool WebRtcSession::GetSslRole(const cricket::BaseChannel* channel, |
| 637 | rtc::SSLRole* role) { |
| 638 | return channel && GetSslRole(channel->transport_name(), role); |
sergeyu@chromium.org | 0be6aa0 | 2013-08-23 23:21:25 +0000 | [diff] [blame] | 639 | } |
| 640 | |
jiayl@webrtc.org | b18bf5e | 2014-08-04 18:34:16 +0000 | [diff] [blame] | 641 | void WebRtcSession::CreateOffer( |
| 642 | CreateSessionDescriptionObserver* observer, |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 643 | const PeerConnectionInterface::RTCOfferAnswerOptions& options, |
| 644 | const cricket::MediaSessionOptions& session_options) { |
| 645 | webrtc_session_desc_factory_->CreateOffer(observer, options, session_options); |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 646 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 647 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 648 | void WebRtcSession::CreateAnswer( |
| 649 | CreateSessionDescriptionObserver* observer, |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 650 | const cricket::MediaSessionOptions& session_options) { |
hta | a2a49d9 | 2016-03-04 02:51:39 -0800 | [diff] [blame] | 651 | webrtc_session_desc_factory_->CreateAnswer(observer, session_options); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 652 | } |
| 653 | |
| 654 | bool WebRtcSession::SetLocalDescription(SessionDescriptionInterface* desc, |
| 655 | std::string* err_desc) { |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 656 | ASSERT(signaling_thread()->IsCurrent()); |
| 657 | |
henrike@webrtc.org | 28654cb | 2013-07-22 21:07:49 +0000 | [diff] [blame] | 658 | // Takes the ownership of |desc| regardless of the result. |
kwiberg | d1fe281 | 2016-04-27 06:47:29 -0700 | [diff] [blame] | 659 | std::unique_ptr<SessionDescriptionInterface> desc_temp(desc); |
henrike@webrtc.org | 28654cb | 2013-07-22 21:07:49 +0000 | [diff] [blame] | 660 | |
sergeyu@chromium.org | 0be6aa0 | 2013-08-23 23:21:25 +0000 | [diff] [blame] | 661 | // Validate SDP. |
| 662 | if (!ValidateSessionDescription(desc, cricket::CS_LOCAL, err_desc)) { |
| 663 | return false; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 664 | } |
| 665 | |
deadbeef | d59daf8 | 2015-10-14 15:02:44 -0700 | [diff] [blame] | 666 | // 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] | 667 | Action action = GetAction(desc->type()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 668 | if (state() == STATE_INIT && action == kOffer) { |
deadbeef | d59daf8 | 2015-10-14 15:02:44 -0700 | [diff] [blame] | 669 | initial_offerer_ = true; |
| 670 | transport_controller_->SetIceRole(cricket::ICEROLE_CONTROLLING); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 671 | } |
| 672 | |
henrike@webrtc.org | b90991d | 2014-03-04 19:54:57 +0000 | [diff] [blame] | 673 | cricket::SecurePolicy sdes_policy = |
| 674 | webrtc_session_desc_factory_->SdesPolicy(); |
| 675 | cricket::CryptoType crypto_required = dtls_enabled_ ? |
| 676 | cricket::CT_DTLS : (sdes_policy == cricket::SEC_REQUIRED ? |
| 677 | cricket::CT_SDES : cricket::CT_NONE); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 678 | // Update the MediaContentDescription crypto settings as per the policy set. |
henrike@webrtc.org | b90991d | 2014-03-04 19:54:57 +0000 | [diff] [blame] | 679 | UpdateSessionDescriptionSecurePolicy(crypto_required, desc->description()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 680 | |
henrike@webrtc.org | 28654cb | 2013-07-22 21:07:49 +0000 | [diff] [blame] | 681 | local_desc_.reset(desc_temp.release()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 682 | |
| 683 | // Transport and Media channels will be created only when offer is set. |
henrike@webrtc.org | 28654cb | 2013-07-22 21:07:49 +0000 | [diff] [blame] | 684 | if (action == kOffer && !CreateChannels(local_desc_->description())) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 685 | // TODO(mallinath) - Handle CreateChannel failure, as new local description |
| 686 | // is applied. Restore back to old description. |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 687 | return BadLocalSdp(desc->type(), kCreateChannelFailed, err_desc); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 688 | } |
| 689 | |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 690 | // Remove unused channels if MediaContentDescription is rejected. |
| 691 | RemoveUnusedChannels(local_desc_->description()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 692 | |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 693 | if (!UpdateSessionState(action, cricket::CS_LOCAL, err_desc)) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 694 | return false; |
| 695 | } |
mallinath@webrtc.org | 3d81b1b | 2014-09-09 14:38:10 +0000 | [diff] [blame] | 696 | |
deadbeef | d59daf8 | 2015-10-14 15:02:44 -0700 | [diff] [blame] | 697 | if (remote_desc_) { |
| 698 | // Now that we have a local description, we can push down remote candidates. |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 699 | UseCandidatesInSessionDescription(remote_desc_.get()); |
| 700 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 701 | |
deadbeef | 0ed85b2 | 2016-02-23 17:24:52 -0800 | [diff] [blame] | 702 | pending_ice_restarts_.clear(); |
| 703 | |
deadbeef | d59daf8 | 2015-10-14 15:02:44 -0700 | [diff] [blame] | 704 | if (error() != ERROR_NONE) { |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 705 | return BadLocalSdp(desc->type(), GetSessionErrorMsg(), err_desc); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 706 | } |
| 707 | return true; |
| 708 | } |
| 709 | |
| 710 | bool WebRtcSession::SetRemoteDescription(SessionDescriptionInterface* desc, |
| 711 | std::string* err_desc) { |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 712 | ASSERT(signaling_thread()->IsCurrent()); |
| 713 | |
henrike@webrtc.org | 28654cb | 2013-07-22 21:07:49 +0000 | [diff] [blame] | 714 | // Takes the ownership of |desc| regardless of the result. |
kwiberg | d1fe281 | 2016-04-27 06:47:29 -0700 | [diff] [blame] | 715 | std::unique_ptr<SessionDescriptionInterface> desc_temp(desc); |
henrike@webrtc.org | 28654cb | 2013-07-22 21:07:49 +0000 | [diff] [blame] | 716 | |
sergeyu@chromium.org | 0be6aa0 | 2013-08-23 23:21:25 +0000 | [diff] [blame] | 717 | // Validate SDP. |
| 718 | if (!ValidateSessionDescription(desc, cricket::CS_REMOTE, err_desc)) { |
| 719 | return false; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 720 | } |
| 721 | |
kwiberg | d1fe281 | 2016-04-27 06:47:29 -0700 | [diff] [blame] | 722 | std::unique_ptr<SessionDescriptionInterface> old_remote_desc( |
deadbeef | d59daf8 | 2015-10-14 15:02:44 -0700 | [diff] [blame] | 723 | remote_desc_.release()); |
| 724 | remote_desc_.reset(desc_temp.release()); |
| 725 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 726 | // Transport and Media channels will be created only when offer is set. |
sergeyu@chromium.org | 0be6aa0 | 2013-08-23 23:21:25 +0000 | [diff] [blame] | 727 | Action action = GetAction(desc->type()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 728 | if (action == kOffer && !CreateChannels(desc->description())) { |
| 729 | // TODO(mallinath) - Handle CreateChannel failure, as new local description |
| 730 | // is applied. Restore back to old description. |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 731 | return BadRemoteSdp(desc->type(), kCreateChannelFailed, err_desc); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 732 | } |
| 733 | |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 734 | // Remove unused channels if MediaContentDescription is rejected. |
| 735 | RemoveUnusedChannels(desc->description()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 736 | |
| 737 | // NOTE: Candidates allocation will be initiated only when SetLocalDescription |
| 738 | // is called. |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 739 | if (!UpdateSessionState(action, cricket::CS_REMOTE, err_desc)) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 740 | return false; |
| 741 | } |
| 742 | |
deadbeef | d59daf8 | 2015-10-14 15:02:44 -0700 | [diff] [blame] | 743 | if (local_desc_ && !UseCandidatesInSessionDescription(desc)) { |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 744 | return BadRemoteSdp(desc->type(), kInvalidCandidates, err_desc); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 745 | } |
| 746 | |
deadbeef | 0ed85b2 | 2016-02-23 17:24:52 -0800 | [diff] [blame] | 747 | if (old_remote_desc) { |
| 748 | for (const cricket::ContentInfo& content : |
| 749 | old_remote_desc->description()->contents()) { |
| 750 | // Check if this new SessionDescription contains new ICE ufrag and |
| 751 | // password that indicates the remote peer requests an ICE restart. |
| 752 | // TODO(deadbeef): When we start storing both the current and pending |
| 753 | // remote description, this should reset pending_ice_restarts and compare |
| 754 | // against the current description. |
| 755 | if (CheckForRemoteIceRestart(old_remote_desc.get(), desc, content.name)) { |
| 756 | if (action == kOffer) { |
| 757 | pending_ice_restarts_.insert(content.name); |
| 758 | } |
| 759 | } else { |
| 760 | // We retain all received candidates only if ICE is not restarted. |
| 761 | // When ICE is restarted, all previous candidates belong to an old |
| 762 | // generation and should not be kept. |
| 763 | // TODO(deadbeef): This goes against the W3C spec which says the remote |
| 764 | // description should only contain candidates from the last set remote |
| 765 | // description plus any candidates added since then. We should remove |
| 766 | // this once we're sure it won't break anything. |
| 767 | WebRtcSessionDescriptionFactory::CopyCandidatesFromSessionDescription( |
| 768 | old_remote_desc.get(), content.name, desc); |
| 769 | } |
| 770 | } |
honghaiz | 503726c | 2015-07-31 12:37:38 -0700 | [diff] [blame] | 771 | } |
| 772 | |
deadbeef | d59daf8 | 2015-10-14 15:02:44 -0700 | [diff] [blame] | 773 | if (error() != ERROR_NONE) { |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 774 | return BadRemoteSdp(desc->type(), GetSessionErrorMsg(), err_desc); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 775 | } |
jiayl@webrtc.org | dacdd94 | 2015-01-23 17:33:34 +0000 | [diff] [blame] | 776 | |
| 777 | // Set the the ICE connection state to connecting since the connection may |
| 778 | // become writable with peer reflexive candidates before any remote candidate |
| 779 | // is signaled. |
| 780 | // TODO(pthatcher): This is a short-term solution for crbug/446908. A real fix |
| 781 | // is to have a new signal the indicates a change in checking state from the |
| 782 | // transport and expose a new checking() member from transport that can be |
| 783 | // read to determine the current checking state. The existing SignalConnecting |
| 784 | // actually means "gathering candidates", so cannot be be used here. |
| 785 | if (desc->type() != SessionDescriptionInterface::kOffer && |
| 786 | ice_connection_state_ == PeerConnectionInterface::kIceConnectionNew) { |
| 787 | SetIceConnectionState(PeerConnectionInterface::kIceConnectionChecking); |
| 788 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 789 | return true; |
| 790 | } |
| 791 | |
deadbeef | d59daf8 | 2015-10-14 15:02:44 -0700 | [diff] [blame] | 792 | void WebRtcSession::LogState(State old_state, State new_state) { |
| 793 | LOG(LS_INFO) << "Session:" << id() |
| 794 | << " Old state:" << GetStateString(old_state) |
| 795 | << " New state:" << GetStateString(new_state); |
| 796 | } |
| 797 | |
| 798 | void WebRtcSession::SetState(State state) { |
| 799 | ASSERT(signaling_thread_->IsCurrent()); |
| 800 | if (state != state_) { |
| 801 | LogState(state_, state); |
| 802 | state_ = state; |
| 803 | SignalState(this, state_); |
| 804 | } |
| 805 | } |
| 806 | |
| 807 | void WebRtcSession::SetError(Error error, const std::string& error_desc) { |
| 808 | ASSERT(signaling_thread_->IsCurrent()); |
| 809 | if (error != error_) { |
| 810 | error_ = error; |
| 811 | error_desc_ = error_desc; |
| 812 | } |
| 813 | } |
| 814 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 815 | bool WebRtcSession::UpdateSessionState( |
| 816 | Action action, cricket::ContentSource source, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 817 | std::string* err_desc) { |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 818 | ASSERT(signaling_thread()->IsCurrent()); |
| 819 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 820 | // If there's already a pending error then no state transition should happen. |
| 821 | // But all call-sites should be verifying this before calling us! |
deadbeef | d59daf8 | 2015-10-14 15:02:44 -0700 | [diff] [blame] | 822 | ASSERT(error() == ERROR_NONE); |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 823 | std::string td_err; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 824 | if (action == kOffer) { |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 825 | if (!PushdownTransportDescription(source, cricket::CA_OFFER, &td_err)) { |
| 826 | return BadOfferSdp(source, MakeTdErrorString(td_err), err_desc); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 827 | } |
deadbeef | d59daf8 | 2015-10-14 15:02:44 -0700 | [diff] [blame] | 828 | SetState(source == cricket::CS_LOCAL ? STATE_SENTOFFER |
| 829 | : STATE_RECEIVEDOFFER); |
pthatcher@webrtc.org | 592470b | 2015-03-16 21:15:37 +0000 | [diff] [blame] | 830 | if (!PushdownMediaDescription(cricket::CA_OFFER, source, err_desc)) { |
deadbeef | d59daf8 | 2015-10-14 15:02:44 -0700 | [diff] [blame] | 831 | SetError(ERROR_CONTENT, *err_desc); |
pthatcher@webrtc.org | 592470b | 2015-03-16 21:15:37 +0000 | [diff] [blame] | 832 | } |
deadbeef | d59daf8 | 2015-10-14 15:02:44 -0700 | [diff] [blame] | 833 | if (error() != ERROR_NONE) { |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 834 | return BadOfferSdp(source, GetSessionErrorMsg(), err_desc); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 835 | } |
| 836 | } else if (action == kPrAnswer) { |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 837 | if (!PushdownTransportDescription(source, cricket::CA_PRANSWER, &td_err)) { |
| 838 | return BadPranswerSdp(source, MakeTdErrorString(td_err), err_desc); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 839 | } |
| 840 | EnableChannels(); |
deadbeef | d59daf8 | 2015-10-14 15:02:44 -0700 | [diff] [blame] | 841 | SetState(source == cricket::CS_LOCAL ? STATE_SENTPRANSWER |
| 842 | : STATE_RECEIVEDPRANSWER); |
pthatcher@webrtc.org | 592470b | 2015-03-16 21:15:37 +0000 | [diff] [blame] | 843 | if (!PushdownMediaDescription(cricket::CA_PRANSWER, source, err_desc)) { |
deadbeef | d59daf8 | 2015-10-14 15:02:44 -0700 | [diff] [blame] | 844 | SetError(ERROR_CONTENT, *err_desc); |
pthatcher@webrtc.org | 592470b | 2015-03-16 21:15:37 +0000 | [diff] [blame] | 845 | } |
deadbeef | d59daf8 | 2015-10-14 15:02:44 -0700 | [diff] [blame] | 846 | if (error() != ERROR_NONE) { |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 847 | return BadPranswerSdp(source, GetSessionErrorMsg(), err_desc); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 848 | } |
| 849 | } else if (action == kAnswer) { |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 850 | const cricket::ContentGroup* local_bundle = |
deadbeef | d59daf8 | 2015-10-14 15:02:44 -0700 | [diff] [blame] | 851 | local_desc_->description()->GetGroupByName(cricket::GROUP_TYPE_BUNDLE); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 852 | const cricket::ContentGroup* remote_bundle = |
deadbeef | d59daf8 | 2015-10-14 15:02:44 -0700 | [diff] [blame] | 853 | remote_desc_->description()->GetGroupByName(cricket::GROUP_TYPE_BUNDLE); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 854 | if (local_bundle && remote_bundle) { |
Taylor Brandstetter | f475d36 | 2016-01-08 15:35:57 -0800 | [diff] [blame] | 855 | // The answerer decides the transport to bundle on. |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 856 | const cricket::ContentGroup* answer_bundle = |
| 857 | (source == cricket::CS_LOCAL ? local_bundle : remote_bundle); |
| 858 | if (!EnableBundle(*answer_bundle)) { |
| 859 | LOG(LS_WARNING) << "Failed to enable BUNDLE."; |
| 860 | return BadAnswerSdp(source, kEnableBundleFailed, err_desc); |
| 861 | } |
| 862 | } |
Taylor Brandstetter | f475d36 | 2016-01-08 15:35:57 -0800 | [diff] [blame] | 863 | // Only push down the transport description after enabling BUNDLE; we don't |
| 864 | // want to push down a description on a transport about to be destroyed. |
| 865 | if (!PushdownTransportDescription(source, cricket::CA_ANSWER, &td_err)) { |
| 866 | return BadAnswerSdp(source, MakeTdErrorString(td_err), err_desc); |
| 867 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 868 | EnableChannels(); |
deadbeef | d59daf8 | 2015-10-14 15:02:44 -0700 | [diff] [blame] | 869 | SetState(STATE_INPROGRESS); |
pthatcher@webrtc.org | 592470b | 2015-03-16 21:15:37 +0000 | [diff] [blame] | 870 | if (!PushdownMediaDescription(cricket::CA_ANSWER, source, err_desc)) { |
deadbeef | d59daf8 | 2015-10-14 15:02:44 -0700 | [diff] [blame] | 871 | SetError(ERROR_CONTENT, *err_desc); |
pthatcher@webrtc.org | 592470b | 2015-03-16 21:15:37 +0000 | [diff] [blame] | 872 | } |
deadbeef | d59daf8 | 2015-10-14 15:02:44 -0700 | [diff] [blame] | 873 | if (error() != ERROR_NONE) { |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 874 | return BadAnswerSdp(source, GetSessionErrorMsg(), err_desc); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 875 | } |
| 876 | } |
| 877 | return true; |
| 878 | } |
| 879 | |
| 880 | WebRtcSession::Action WebRtcSession::GetAction(const std::string& type) { |
| 881 | if (type == SessionDescriptionInterface::kOffer) { |
| 882 | return WebRtcSession::kOffer; |
| 883 | } else if (type == SessionDescriptionInterface::kPrAnswer) { |
| 884 | return WebRtcSession::kPrAnswer; |
| 885 | } else if (type == SessionDescriptionInterface::kAnswer) { |
| 886 | return WebRtcSession::kAnswer; |
| 887 | } |
| 888 | ASSERT(false && "unknown action type"); |
| 889 | return WebRtcSession::kOffer; |
| 890 | } |
| 891 | |
deadbeef | d59daf8 | 2015-10-14 15:02:44 -0700 | [diff] [blame] | 892 | bool WebRtcSession::PushdownMediaDescription( |
| 893 | cricket::ContentAction action, |
| 894 | cricket::ContentSource source, |
| 895 | std::string* err) { |
| 896 | auto set_content = [this, action, source, err](cricket::BaseChannel* ch) { |
| 897 | if (!ch) { |
| 898 | return true; |
| 899 | } else if (source == cricket::CS_LOCAL) { |
| 900 | return ch->PushdownLocalDescription(local_desc_->description(), action, |
| 901 | err); |
| 902 | } else { |
| 903 | return ch->PushdownRemoteDescription(remote_desc_->description(), action, |
| 904 | err); |
| 905 | } |
| 906 | }; |
| 907 | |
| 908 | return (set_content(voice_channel()) && |
| 909 | set_content(video_channel()) && |
| 910 | set_content(data_channel())); |
| 911 | } |
| 912 | |
| 913 | bool WebRtcSession::PushdownTransportDescription(cricket::ContentSource source, |
| 914 | cricket::ContentAction action, |
| 915 | std::string* error_desc) { |
| 916 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
| 917 | |
| 918 | if (source == cricket::CS_LOCAL) { |
| 919 | return PushdownLocalTransportDescription(local_desc_->description(), action, |
| 920 | error_desc); |
| 921 | } |
| 922 | return PushdownRemoteTransportDescription(remote_desc_->description(), action, |
| 923 | error_desc); |
| 924 | } |
| 925 | |
| 926 | bool WebRtcSession::PushdownLocalTransportDescription( |
| 927 | const SessionDescription* sdesc, |
| 928 | cricket::ContentAction action, |
| 929 | std::string* err) { |
| 930 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
| 931 | |
| 932 | if (!sdesc) { |
| 933 | return false; |
| 934 | } |
| 935 | |
| 936 | for (const TransportInfo& tinfo : sdesc->transport_infos()) { |
| 937 | if (!transport_controller_->SetLocalTransportDescription( |
| 938 | tinfo.content_name, tinfo.description, action, err)) { |
| 939 | return false; |
| 940 | } |
| 941 | } |
| 942 | |
| 943 | return true; |
| 944 | } |
| 945 | |
| 946 | bool WebRtcSession::PushdownRemoteTransportDescription( |
| 947 | const SessionDescription* sdesc, |
| 948 | cricket::ContentAction action, |
| 949 | std::string* err) { |
| 950 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
| 951 | |
| 952 | if (!sdesc) { |
| 953 | return false; |
| 954 | } |
| 955 | |
| 956 | for (const TransportInfo& tinfo : sdesc->transport_infos()) { |
| 957 | if (!transport_controller_->SetRemoteTransportDescription( |
| 958 | tinfo.content_name, tinfo.description, action, err)) { |
| 959 | return false; |
| 960 | } |
| 961 | } |
| 962 | |
| 963 | return true; |
| 964 | } |
| 965 | |
| 966 | bool WebRtcSession::GetTransportDescription( |
| 967 | const SessionDescription* description, |
| 968 | const std::string& content_name, |
| 969 | cricket::TransportDescription* tdesc) { |
| 970 | if (!description || !tdesc) { |
| 971 | return false; |
| 972 | } |
| 973 | const TransportInfo* transport_info = |
| 974 | description->GetTransportInfoByName(content_name); |
| 975 | if (!transport_info) { |
| 976 | return false; |
| 977 | } |
| 978 | *tdesc = transport_info->description; |
| 979 | return true; |
| 980 | } |
| 981 | |
| 982 | bool WebRtcSession::GetTransportStats(SessionStats* stats) { |
pthatcher@webrtc.org | c04a97f | 2015-03-16 19:31:40 +0000 | [diff] [blame] | 983 | ASSERT(signaling_thread()->IsCurrent()); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 984 | return (GetChannelTransportStats(voice_channel(), stats) && |
| 985 | GetChannelTransportStats(video_channel(), stats) && |
| 986 | GetChannelTransportStats(data_channel(), stats)); |
| 987 | } |
pthatcher@webrtc.org | c04a97f | 2015-03-16 19:31:40 +0000 | [diff] [blame] | 988 | |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 989 | bool WebRtcSession::GetChannelTransportStats(cricket::BaseChannel* ch, |
deadbeef | d59daf8 | 2015-10-14 15:02:44 -0700 | [diff] [blame] | 990 | SessionStats* stats) { |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 991 | ASSERT(signaling_thread()->IsCurrent()); |
| 992 | if (!ch) { |
| 993 | // Not using this channel. |
| 994 | return true; |
| 995 | } |
| 996 | |
| 997 | const std::string& content_name = ch->content_name(); |
| 998 | const std::string& transport_name = ch->transport_name(); |
| 999 | stats->proxy_to_transport[content_name] = transport_name; |
| 1000 | if (stats->transport_stats.find(transport_name) != |
| 1001 | stats->transport_stats.end()) { |
| 1002 | // Transport stats already done for this transport. |
| 1003 | return true; |
| 1004 | } |
| 1005 | |
| 1006 | cricket::TransportStats tstats; |
deadbeef | d59daf8 | 2015-10-14 15:02:44 -0700 | [diff] [blame] | 1007 | if (!transport_controller_->GetStats(transport_name, &tstats)) { |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 1008 | return false; |
| 1009 | } |
| 1010 | |
| 1011 | stats->transport_stats[transport_name] = tstats; |
| 1012 | return true; |
| 1013 | } |
| 1014 | |
| 1015 | bool WebRtcSession::GetLocalCertificate( |
| 1016 | const std::string& transport_name, |
| 1017 | rtc::scoped_refptr<rtc::RTCCertificate>* certificate) { |
| 1018 | ASSERT(signaling_thread()->IsCurrent()); |
deadbeef | d59daf8 | 2015-10-14 15:02:44 -0700 | [diff] [blame] | 1019 | return transport_controller_->GetLocalCertificate(transport_name, |
| 1020 | certificate); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 1021 | } |
| 1022 | |
jbauch | 555604a | 2016-04-26 03:13:22 -0700 | [diff] [blame] | 1023 | std::unique_ptr<rtc::SSLCertificate> WebRtcSession::GetRemoteSSLCertificate( |
kwiberg | b4d01c4 | 2016-04-06 05:15:06 -0700 | [diff] [blame] | 1024 | const std::string& transport_name) { |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 1025 | ASSERT(signaling_thread()->IsCurrent()); |
kwiberg | b4d01c4 | 2016-04-06 05:15:06 -0700 | [diff] [blame] | 1026 | return transport_controller_->GetRemoteSSLCertificate(transport_name); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 1027 | } |
| 1028 | |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 1029 | bool WebRtcSession::EnableBundle(const cricket::ContentGroup& bundle) { |
| 1030 | const std::string* first_content_name = bundle.FirstContentName(); |
| 1031 | if (!first_content_name) { |
| 1032 | LOG(LS_WARNING) << "Tried to BUNDLE with no contents."; |
| 1033 | return false; |
| 1034 | } |
| 1035 | const std::string& transport_name = *first_content_name; |
| 1036 | cricket::BaseChannel* first_channel = GetChannel(transport_name); |
| 1037 | |
| 1038 | auto maybe_set_transport = [this, bundle, transport_name, |
| 1039 | first_channel](cricket::BaseChannel* ch) { |
| 1040 | if (!ch || !bundle.HasContentName(ch->content_name())) { |
pthatcher@webrtc.org | c04a97f | 2015-03-16 19:31:40 +0000 | [diff] [blame] | 1041 | return true; |
| 1042 | } |
| 1043 | |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 1044 | if (ch->transport_name() == transport_name) { |
| 1045 | LOG(LS_INFO) << "BUNDLE already enabled for " << ch->content_name() |
| 1046 | << " on " << transport_name << "."; |
| 1047 | return true; |
deadbeef | 47ee2f3 | 2015-09-22 15:08:23 -0700 | [diff] [blame] | 1048 | } |
torbjorng | a81a42f | 2015-09-23 02:16:58 -0700 | [diff] [blame] | 1049 | |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 1050 | if (!ch->SetTransport(transport_name)) { |
| 1051 | LOG(LS_WARNING) << "Failed to enable BUNDLE for " << ch->content_name(); |
| 1052 | return false; |
| 1053 | } |
| 1054 | LOG(LS_INFO) << "Enabled BUNDLE for " << ch->content_name() << " on " |
| 1055 | << transport_name << "."; |
pthatcher@webrtc.org | c04a97f | 2015-03-16 19:31:40 +0000 | [diff] [blame] | 1056 | return true; |
| 1057 | }; |
| 1058 | |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 1059 | if (!maybe_set_transport(voice_channel()) || |
| 1060 | !maybe_set_transport(video_channel()) || |
| 1061 | !maybe_set_transport(data_channel())) { |
| 1062 | return false; |
pthatcher@webrtc.org | c04a97f | 2015-03-16 19:31:40 +0000 | [diff] [blame] | 1063 | } |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 1064 | |
pthatcher@webrtc.org | c04a97f | 2015-03-16 19:31:40 +0000 | [diff] [blame] | 1065 | return true; |
| 1066 | } |
| 1067 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1068 | bool WebRtcSession::ProcessIceMessage(const IceCandidateInterface* candidate) { |
deadbeef | d59daf8 | 2015-10-14 15:02:44 -0700 | [diff] [blame] | 1069 | if (!remote_desc_) { |
| 1070 | LOG(LS_ERROR) << "ProcessIceMessage: ICE candidates can't be added " |
| 1071 | << "without any remote session description."; |
Honghai Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 1072 | return false; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1073 | } |
| 1074 | |
| 1075 | if (!candidate) { |
deadbeef | d59daf8 | 2015-10-14 15:02:44 -0700 | [diff] [blame] | 1076 | LOG(LS_ERROR) << "ProcessIceMessage: Candidate is NULL."; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1077 | return false; |
| 1078 | } |
| 1079 | |
jiayl@webrtc.org | e10d28c | 2014-07-17 17:07:49 +0000 | [diff] [blame] | 1080 | bool valid = false; |
deadbeef | d59daf8 | 2015-10-14 15:02:44 -0700 | [diff] [blame] | 1081 | bool ready = ReadyToUseRemoteCandidate(candidate, NULL, &valid); |
| 1082 | if (!valid) { |
| 1083 | return false; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1084 | } |
| 1085 | |
| 1086 | // Add this candidate to the remote session description. |
| 1087 | if (!remote_desc_->AddCandidate(candidate)) { |
deadbeef | d59daf8 | 2015-10-14 15:02:44 -0700 | [diff] [blame] | 1088 | LOG(LS_ERROR) << "ProcessIceMessage: Candidate cannot be used."; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1089 | return false; |
| 1090 | } |
| 1091 | |
deadbeef | d59daf8 | 2015-10-14 15:02:44 -0700 | [diff] [blame] | 1092 | if (ready) { |
| 1093 | return UseCandidate(candidate); |
| 1094 | } else { |
| 1095 | LOG(LS_INFO) << "ProcessIceMessage: Not ready to use candidate."; |
| 1096 | return true; |
| 1097 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1098 | } |
| 1099 | |
Honghai Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 1100 | bool WebRtcSession::RemoveRemoteIceCandidates( |
| 1101 | const std::vector<cricket::Candidate>& candidates) { |
| 1102 | if (!remote_desc_) { |
| 1103 | LOG(LS_ERROR) << "RemoveRemoteIceCandidates: ICE candidates can't be " |
| 1104 | << "removed without any remote session description."; |
| 1105 | return false; |
| 1106 | } |
| 1107 | |
| 1108 | if (candidates.empty()) { |
| 1109 | LOG(LS_ERROR) << "RemoveRemoteIceCandidates: candidates are empty."; |
| 1110 | return false; |
| 1111 | } |
| 1112 | |
| 1113 | size_t number_removed = remote_desc_->RemoveCandidates(candidates); |
| 1114 | if (number_removed != candidates.size()) { |
| 1115 | LOG(LS_ERROR) << "RemoveRemoteIceCandidates: Failed to remove candidates. " |
| 1116 | << "Requested " << candidates.size() << " but only " |
| 1117 | << number_removed << " are removed."; |
| 1118 | } |
| 1119 | |
| 1120 | // Remove the candidates from the transport controller. |
| 1121 | std::string error; |
| 1122 | bool res = transport_controller_->RemoveRemoteCandidates(candidates, &error); |
| 1123 | if (!res && !error.empty()) { |
| 1124 | LOG(LS_ERROR) << "Error when removing remote candidates: " << error; |
| 1125 | } |
| 1126 | return true; |
| 1127 | } |
| 1128 | |
deadbeef | d59daf8 | 2015-10-14 15:02:44 -0700 | [diff] [blame] | 1129 | cricket::IceConfig WebRtcSession::ParseIceConfig( |
| 1130 | const PeerConnectionInterface::RTCConfiguration& config) const { |
| 1131 | cricket::IceConfig ice_config; |
Honghai Zhang | 049fbb1 | 2016-03-07 11:13:07 -0800 | [diff] [blame] | 1132 | ice_config.receiving_timeout = config.ice_connection_receiving_timeout; |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 1133 | ice_config.prioritize_most_likely_candidate_pairs = |
| 1134 | config.prioritize_most_likely_ice_candidate_pairs; |
Honghai Zhang | 381b421 | 2015-12-04 12:24:03 -0800 | [diff] [blame] | 1135 | ice_config.backup_connection_ping_interval = |
| 1136 | config.ice_backup_candidate_pair_ping_interval; |
deadbeef | d59daf8 | 2015-10-14 15:02:44 -0700 | [diff] [blame] | 1137 | ice_config.gather_continually = (config.continual_gathering_policy == |
| 1138 | PeerConnectionInterface::GATHER_CONTINUALLY); |
| 1139 | return ice_config; |
| 1140 | } |
| 1141 | |
| 1142 | void WebRtcSession::SetIceConfig(const cricket::IceConfig& config) { |
| 1143 | transport_controller_->SetIceConfig(config); |
| 1144 | } |
| 1145 | |
| 1146 | void WebRtcSession::MaybeStartGathering() { |
| 1147 | transport_controller_->MaybeStartGathering(); |
| 1148 | } |
| 1149 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 1150 | bool WebRtcSession::GetLocalTrackIdBySsrc(uint32_t ssrc, |
| 1151 | std::string* track_id) { |
deadbeef | d59daf8 | 2015-10-14 15:02:44 -0700 | [diff] [blame] | 1152 | if (!local_desc_) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1153 | return false; |
deadbeef | d59daf8 | 2015-10-14 15:02:44 -0700 | [diff] [blame] | 1154 | } |
| 1155 | return webrtc::GetTrackIdBySsrc(local_desc_->description(), ssrc, track_id); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1156 | } |
| 1157 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 1158 | bool WebRtcSession::GetRemoteTrackIdBySsrc(uint32_t ssrc, |
| 1159 | std::string* track_id) { |
deadbeef | d59daf8 | 2015-10-14 15:02:44 -0700 | [diff] [blame] | 1160 | if (!remote_desc_) { |
xians@webrtc.org | 4cb0128 | 2014-06-12 14:57:05 +0000 | [diff] [blame] | 1161 | return false; |
deadbeef | d59daf8 | 2015-10-14 15:02:44 -0700 | [diff] [blame] | 1162 | } |
| 1163 | return webrtc::GetTrackIdBySsrc(remote_desc_->description(), ssrc, track_id); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1164 | } |
| 1165 | |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 1166 | std::string WebRtcSession::BadStateErrMsg(State state) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1167 | std::ostringstream desc; |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 1168 | desc << "Called in wrong state: " << GetStateString(state); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1169 | return desc.str(); |
| 1170 | } |
| 1171 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1172 | bool WebRtcSession::CanInsertDtmf(const std::string& track_id) { |
| 1173 | ASSERT(signaling_thread()->IsCurrent()); |
| 1174 | if (!voice_channel_) { |
| 1175 | LOG(LS_ERROR) << "CanInsertDtmf: No audio channel exists."; |
| 1176 | return false; |
| 1177 | } |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 1178 | uint32_t send_ssrc = 0; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1179 | // The Dtmf is negotiated per channel not ssrc, so we only check if the ssrc |
| 1180 | // exists. |
deadbeef | d59daf8 | 2015-10-14 15:02:44 -0700 | [diff] [blame] | 1181 | if (!local_desc_ || |
| 1182 | !GetAudioSsrcByTrackId(local_desc_->description(), track_id, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1183 | &send_ssrc)) { |
| 1184 | LOG(LS_ERROR) << "CanInsertDtmf: Track does not exist: " << track_id; |
| 1185 | return false; |
| 1186 | } |
| 1187 | return voice_channel_->CanInsertDtmf(); |
| 1188 | } |
| 1189 | |
| 1190 | bool WebRtcSession::InsertDtmf(const std::string& track_id, |
| 1191 | int code, int duration) { |
| 1192 | ASSERT(signaling_thread()->IsCurrent()); |
| 1193 | if (!voice_channel_) { |
| 1194 | LOG(LS_ERROR) << "InsertDtmf: No audio channel exists."; |
| 1195 | return false; |
| 1196 | } |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 1197 | uint32_t send_ssrc = 0; |
deadbeef | d59daf8 | 2015-10-14 15:02:44 -0700 | [diff] [blame] | 1198 | if (!VERIFY(local_desc_ && GetAudioSsrcByTrackId(local_desc_->description(), |
| 1199 | track_id, &send_ssrc))) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1200 | LOG(LS_ERROR) << "InsertDtmf: Track does not exist: " << track_id; |
| 1201 | return false; |
| 1202 | } |
solenberg | 1d63dd0 | 2015-12-02 12:35:09 -0800 | [diff] [blame] | 1203 | if (!voice_channel_->InsertDtmf(send_ssrc, code, duration)) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1204 | LOG(LS_ERROR) << "Failed to insert DTMF to channel."; |
| 1205 | return false; |
| 1206 | } |
| 1207 | return true; |
| 1208 | } |
| 1209 | |
| 1210 | sigslot::signal0<>* WebRtcSession::GetOnDestroyedSignal() { |
deadbeef | 057ecf0 | 2016-01-20 14:30:43 -0800 | [diff] [blame] | 1211 | return &SignalDestroyed; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1212 | } |
| 1213 | |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 1214 | bool WebRtcSession::SendData(const cricket::SendDataParams& params, |
jbauch | eec21bd | 2016-03-20 06:15:43 -0700 | [diff] [blame] | 1215 | const rtc::CopyOnWriteBuffer& payload, |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 1216 | cricket::SendDataResult* result) { |
pthatcher@webrtc.org | 4eeef58 | 2015-03-16 19:34:23 +0000 | [diff] [blame] | 1217 | if (!data_channel_) { |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 1218 | LOG(LS_ERROR) << "SendData called when data_channel_ is NULL."; |
| 1219 | return false; |
| 1220 | } |
| 1221 | return data_channel_->SendData(params, payload, result); |
| 1222 | } |
| 1223 | |
| 1224 | bool WebRtcSession::ConnectDataChannel(DataChannel* webrtc_data_channel) { |
pthatcher@webrtc.org | 4eeef58 | 2015-03-16 19:34:23 +0000 | [diff] [blame] | 1225 | if (!data_channel_) { |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 1226 | LOG(LS_ERROR) << "ConnectDataChannel called when data_channel_ is NULL."; |
| 1227 | return false; |
| 1228 | } |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 1229 | data_channel_->SignalReadyToSendData.connect(webrtc_data_channel, |
| 1230 | &DataChannel::OnChannelReady); |
| 1231 | data_channel_->SignalDataReceived.connect(webrtc_data_channel, |
| 1232 | &DataChannel::OnDataReceived); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1233 | data_channel_->SignalStreamClosedRemotely.connect( |
| 1234 | webrtc_data_channel, &DataChannel::OnStreamClosedRemotely); |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 1235 | return true; |
| 1236 | } |
| 1237 | |
| 1238 | void WebRtcSession::DisconnectDataChannel(DataChannel* webrtc_data_channel) { |
pthatcher@webrtc.org | 4eeef58 | 2015-03-16 19:34:23 +0000 | [diff] [blame] | 1239 | if (!data_channel_) { |
wu@webrtc.org | cecfd18 | 2013-10-30 05:18:12 +0000 | [diff] [blame] | 1240 | LOG(LS_ERROR) << "DisconnectDataChannel called when data_channel_ is NULL."; |
| 1241 | return; |
| 1242 | } |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 1243 | data_channel_->SignalReadyToSendData.disconnect(webrtc_data_channel); |
| 1244 | data_channel_->SignalDataReceived.disconnect(webrtc_data_channel); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1245 | data_channel_->SignalStreamClosedRemotely.disconnect(webrtc_data_channel); |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 1246 | } |
| 1247 | |
bemasc@webrtc.org | 9b5467e | 2014-12-04 23:16:52 +0000 | [diff] [blame] | 1248 | void WebRtcSession::AddSctpDataStream(int sid) { |
pthatcher@webrtc.org | 4eeef58 | 2015-03-16 19:34:23 +0000 | [diff] [blame] | 1249 | if (!data_channel_) { |
wu@webrtc.org | cecfd18 | 2013-10-30 05:18:12 +0000 | [diff] [blame] | 1250 | LOG(LS_ERROR) << "AddDataChannelStreams called when data_channel_ is NULL."; |
| 1251 | return; |
| 1252 | } |
sergeyu@chromium.org | a23f0ca | 2013-11-13 22:48:52 +0000 | [diff] [blame] | 1253 | data_channel_->AddRecvStream(cricket::StreamParams::CreateLegacy(sid)); |
| 1254 | data_channel_->AddSendStream(cricket::StreamParams::CreateLegacy(sid)); |
wu@webrtc.org | cecfd18 | 2013-10-30 05:18:12 +0000 | [diff] [blame] | 1255 | } |
| 1256 | |
bemasc@webrtc.org | 9b5467e | 2014-12-04 23:16:52 +0000 | [diff] [blame] | 1257 | void WebRtcSession::RemoveSctpDataStream(int sid) { |
pthatcher@webrtc.org | 4eeef58 | 2015-03-16 19:34:23 +0000 | [diff] [blame] | 1258 | if (!data_channel_) { |
wu@webrtc.org | cecfd18 | 2013-10-30 05:18:12 +0000 | [diff] [blame] | 1259 | LOG(LS_ERROR) << "RemoveDataChannelStreams called when data_channel_ is " |
| 1260 | << "NULL."; |
| 1261 | return; |
| 1262 | } |
sergeyu@chromium.org | a23f0ca | 2013-11-13 22:48:52 +0000 | [diff] [blame] | 1263 | data_channel_->RemoveRecvStream(sid); |
| 1264 | data_channel_->RemoveSendStream(sid); |
wu@webrtc.org | cecfd18 | 2013-10-30 05:18:12 +0000 | [diff] [blame] | 1265 | } |
| 1266 | |
wu@webrtc.org | 07a6fbe | 2013-11-04 18:41:34 +0000 | [diff] [blame] | 1267 | bool WebRtcSession::ReadyToSendData() const { |
pthatcher@webrtc.org | 4eeef58 | 2015-03-16 19:34:23 +0000 | [diff] [blame] | 1268 | return data_channel_ && data_channel_->ready_to_send_data(); |
wu@webrtc.org | 07a6fbe | 2013-11-04 18:41:34 +0000 | [diff] [blame] | 1269 | } |
| 1270 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1271 | cricket::DataChannelType WebRtcSession::data_channel_type() const { |
| 1272 | return data_channel_type_; |
| 1273 | } |
| 1274 | |
deadbeef | 0ed85b2 | 2016-02-23 17:24:52 -0800 | [diff] [blame] | 1275 | bool WebRtcSession::IceRestartPending(const std::string& content_name) const { |
| 1276 | return pending_ice_restarts_.find(content_name) != |
| 1277 | pending_ice_restarts_.end(); |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 1278 | } |
| 1279 | |
Henrik Boström | d828198 | 2015-08-27 10:12:24 +0200 | [diff] [blame] | 1280 | void WebRtcSession::OnCertificateReady( |
| 1281 | const rtc::scoped_refptr<rtc::RTCCertificate>& certificate) { |
deadbeef | d59daf8 | 2015-10-14 15:02:44 -0700 | [diff] [blame] | 1282 | transport_controller_->SetLocalCertificate(certificate); |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 1283 | } |
| 1284 | |
Henrik Boström | d828198 | 2015-08-27 10:12:24 +0200 | [diff] [blame] | 1285 | bool WebRtcSession::waiting_for_certificate_for_testing() const { |
Henrik Boström | 87713d0 | 2015-08-25 09:53:21 +0200 | [diff] [blame] | 1286 | return webrtc_session_desc_factory_->waiting_for_certificate_for_testing(); |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 1287 | } |
| 1288 | |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 1289 | const rtc::scoped_refptr<rtc::RTCCertificate>& |
| 1290 | WebRtcSession::certificate_for_testing() { |
deadbeef | d59daf8 | 2015-10-14 15:02:44 -0700 | [diff] [blame] | 1291 | return transport_controller_->certificate_for_testing(); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 1292 | } |
| 1293 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1294 | void WebRtcSession::SetIceConnectionState( |
| 1295 | PeerConnectionInterface::IceConnectionState state) { |
| 1296 | if (ice_connection_state_ == state) { |
| 1297 | return; |
| 1298 | } |
| 1299 | |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 1300 | LOG(LS_INFO) << "Changing IceConnectionState " << ice_connection_state_ |
| 1301 | << " => " << state; |
Taylor Brandstetter | 6aefc63 | 2016-05-26 16:08:23 -0700 | [diff] [blame] | 1302 | RTC_DCHECK(ice_connection_state_ != |
| 1303 | PeerConnectionInterface::kIceConnectionClosed); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1304 | ice_connection_state_ = state; |
| 1305 | if (ice_observer_) { |
| 1306 | ice_observer_->OnIceConnectionChange(ice_connection_state_); |
| 1307 | } |
| 1308 | } |
| 1309 | |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 1310 | void WebRtcSession::OnTransportControllerConnectionState( |
| 1311 | cricket::IceConnectionState state) { |
| 1312 | switch (state) { |
| 1313 | case cricket::kIceConnectionConnecting: |
| 1314 | // If the current state is Connected or Completed, then there were |
| 1315 | // writable channels but now there are not, so the next state must |
| 1316 | // be Disconnected. |
| 1317 | // kIceConnectionConnecting is currently used as the default, |
| 1318 | // un-connected state by the TransportController, so its only use is |
| 1319 | // detecting disconnections. |
| 1320 | if (ice_connection_state_ == |
| 1321 | PeerConnectionInterface::kIceConnectionConnected || |
| 1322 | ice_connection_state_ == |
| 1323 | PeerConnectionInterface::kIceConnectionCompleted) { |
| 1324 | SetIceConnectionState( |
| 1325 | PeerConnectionInterface::kIceConnectionDisconnected); |
| 1326 | } |
torbjorng | a81a42f | 2015-09-23 02:16:58 -0700 | [diff] [blame] | 1327 | break; |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 1328 | case cricket::kIceConnectionFailed: |
| 1329 | SetIceConnectionState(PeerConnectionInterface::kIceConnectionFailed); |
| 1330 | break; |
| 1331 | case cricket::kIceConnectionConnected: |
| 1332 | LOG(LS_INFO) << "Changing to ICE connected state because " |
| 1333 | << "all transports are writable."; |
| 1334 | SetIceConnectionState(PeerConnectionInterface::kIceConnectionConnected); |
| 1335 | break; |
| 1336 | case cricket::kIceConnectionCompleted: |
| 1337 | LOG(LS_INFO) << "Changing to ICE completed state because " |
| 1338 | << "all transports are complete."; |
| 1339 | if (ice_connection_state_ != |
| 1340 | PeerConnectionInterface::kIceConnectionConnected) { |
| 1341 | // If jumping directly from "checking" to "connected", |
| 1342 | // signal "connected" first. |
| 1343 | SetIceConnectionState(PeerConnectionInterface::kIceConnectionConnected); |
| 1344 | } |
| 1345 | SetIceConnectionState(PeerConnectionInterface::kIceConnectionCompleted); |
| 1346 | if (metrics_observer_) { |
| 1347 | ReportTransportStats(); |
| 1348 | } |
| 1349 | break; |
| 1350 | default: |
| 1351 | ASSERT(false); |
torbjorng | a81a42f | 2015-09-23 02:16:58 -0700 | [diff] [blame] | 1352 | } |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 1353 | } |
| 1354 | |
| 1355 | void WebRtcSession::OnTransportControllerReceiving(bool receiving) { |
Peter Thatcher | 5436051 | 2015-07-08 11:08:35 -0700 | [diff] [blame] | 1356 | SetIceConnectionReceiving(receiving); |
| 1357 | } |
| 1358 | |
| 1359 | void WebRtcSession::SetIceConnectionReceiving(bool receiving) { |
| 1360 | if (ice_connection_receiving_ == receiving) { |
| 1361 | return; |
| 1362 | } |
| 1363 | ice_connection_receiving_ = receiving; |
| 1364 | if (ice_observer_) { |
| 1365 | ice_observer_->OnIceConnectionReceivingChange(receiving); |
| 1366 | } |
| 1367 | } |
| 1368 | |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 1369 | void WebRtcSession::OnTransportControllerCandidatesGathered( |
| 1370 | const std::string& transport_name, |
| 1371 | const cricket::Candidates& candidates) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1372 | ASSERT(signaling_thread()->IsCurrent()); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 1373 | int sdp_mline_index; |
| 1374 | if (!GetLocalCandidateMediaIndex(transport_name, &sdp_mline_index)) { |
| 1375 | LOG(LS_ERROR) << "OnTransportControllerCandidatesGathered: content name " |
| 1376 | << transport_name << " not found"; |
| 1377 | return; |
| 1378 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1379 | |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 1380 | for (cricket::Candidates::const_iterator citer = candidates.begin(); |
| 1381 | citer != candidates.end(); ++citer) { |
| 1382 | // Use transport_name as the candidate media id. |
| 1383 | JsepIceCandidate candidate(transport_name, sdp_mline_index, *citer); |
| 1384 | if (ice_observer_) { |
| 1385 | ice_observer_->OnIceCandidate(&candidate); |
| 1386 | } |
| 1387 | if (local_desc_) { |
| 1388 | local_desc_->AddCandidate(&candidate); |
| 1389 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1390 | } |
| 1391 | } |
| 1392 | |
Honghai Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 1393 | void WebRtcSession::OnTransportControllerCandidatesRemoved( |
| 1394 | const std::vector<cricket::Candidate>& candidates) { |
| 1395 | ASSERT(signaling_thread()->IsCurrent()); |
| 1396 | // Sanity check. |
| 1397 | for (const cricket::Candidate& candidate : candidates) { |
| 1398 | if (candidate.transport_name().empty()) { |
| 1399 | LOG(LS_ERROR) << "OnTransportControllerCandidatesRemoved: " |
| 1400 | << "empty content name in candidate " |
| 1401 | << candidate.ToString(); |
| 1402 | return; |
| 1403 | } |
| 1404 | } |
| 1405 | |
| 1406 | if (local_desc_) { |
| 1407 | local_desc_->RemoveCandidates(candidates); |
| 1408 | } |
| 1409 | if (ice_observer_) { |
| 1410 | ice_observer_->OnIceCandidatesRemoved(candidates); |
| 1411 | } |
| 1412 | } |
| 1413 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1414 | // Enabling voice and video channel. |
| 1415 | void WebRtcSession::EnableChannels() { |
| 1416 | if (voice_channel_ && !voice_channel_->enabled()) |
| 1417 | voice_channel_->Enable(true); |
| 1418 | |
| 1419 | if (video_channel_ && !video_channel_->enabled()) |
| 1420 | video_channel_->Enable(true); |
| 1421 | |
pthatcher@webrtc.org | 4eeef58 | 2015-03-16 19:34:23 +0000 | [diff] [blame] | 1422 | if (data_channel_ && !data_channel_->enabled()) |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1423 | data_channel_->Enable(true); |
| 1424 | } |
| 1425 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1426 | // Returns the media index for a local ice candidate given the content name. |
| 1427 | bool WebRtcSession::GetLocalCandidateMediaIndex(const std::string& content_name, |
| 1428 | int* sdp_mline_index) { |
deadbeef | d59daf8 | 2015-10-14 15:02:44 -0700 | [diff] [blame] | 1429 | if (!local_desc_ || !sdp_mline_index) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1430 | return false; |
deadbeef | d59daf8 | 2015-10-14 15:02:44 -0700 | [diff] [blame] | 1431 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1432 | |
| 1433 | bool content_found = false; |
deadbeef | d59daf8 | 2015-10-14 15:02:44 -0700 | [diff] [blame] | 1434 | const ContentInfos& contents = local_desc_->description()->contents(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1435 | for (size_t index = 0; index < contents.size(); ++index) { |
| 1436 | if (contents[index].name == content_name) { |
henrike@webrtc.org | 28654cb | 2013-07-22 21:07:49 +0000 | [diff] [blame] | 1437 | *sdp_mline_index = static_cast<int>(index); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1438 | content_found = true; |
| 1439 | break; |
| 1440 | } |
| 1441 | } |
| 1442 | return content_found; |
| 1443 | } |
| 1444 | |
| 1445 | bool WebRtcSession::UseCandidatesInSessionDescription( |
| 1446 | const SessionDescriptionInterface* remote_desc) { |
deadbeef | d59daf8 | 2015-10-14 15:02:44 -0700 | [diff] [blame] | 1447 | if (!remote_desc) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1448 | return true; |
deadbeef | d59daf8 | 2015-10-14 15:02:44 -0700 | [diff] [blame] | 1449 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1450 | bool ret = true; |
jiayl@webrtc.org | e10d28c | 2014-07-17 17:07:49 +0000 | [diff] [blame] | 1451 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1452 | for (size_t m = 0; m < remote_desc->number_of_mediasections(); ++m) { |
| 1453 | const IceCandidateCollection* candidates = remote_desc->candidates(m); |
deadbeef | d59daf8 | 2015-10-14 15:02:44 -0700 | [diff] [blame] | 1454 | for (size_t n = 0; n < candidates->count(); ++n) { |
jiayl@webrtc.org | e10d28c | 2014-07-17 17:07:49 +0000 | [diff] [blame] | 1455 | const IceCandidateInterface* candidate = candidates->at(n); |
| 1456 | bool valid = false; |
| 1457 | if (!ReadyToUseRemoteCandidate(candidate, remote_desc, &valid)) { |
| 1458 | if (valid) { |
deadbeef | d59daf8 | 2015-10-14 15:02:44 -0700 | [diff] [blame] | 1459 | LOG(LS_INFO) << "UseCandidatesInSessionDescription: Not ready to use " |
| 1460 | << "candidate."; |
jiayl@webrtc.org | e10d28c | 2014-07-17 17:07:49 +0000 | [diff] [blame] | 1461 | } |
| 1462 | continue; |
| 1463 | } |
jiayl@webrtc.org | e10d28c | 2014-07-17 17:07:49 +0000 | [diff] [blame] | 1464 | ret = UseCandidate(candidate); |
deadbeef | d59daf8 | 2015-10-14 15:02:44 -0700 | [diff] [blame] | 1465 | if (!ret) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1466 | break; |
deadbeef | d59daf8 | 2015-10-14 15:02:44 -0700 | [diff] [blame] | 1467 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1468 | } |
| 1469 | } |
| 1470 | return ret; |
| 1471 | } |
| 1472 | |
Honghai Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 1473 | bool WebRtcSession::UseCandidate(const IceCandidateInterface* candidate) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1474 | size_t mediacontent_index = static_cast<size_t>(candidate->sdp_mline_index()); |
deadbeef | d59daf8 | 2015-10-14 15:02:44 -0700 | [diff] [blame] | 1475 | size_t remote_content_size = remote_desc_->description()->contents().size(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1476 | if (mediacontent_index >= remote_content_size) { |
Honghai Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 1477 | LOG(LS_ERROR) << "UseCandidate: Invalid candidate media index."; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1478 | return false; |
| 1479 | } |
| 1480 | |
| 1481 | cricket::ContentInfo content = |
deadbeef | d59daf8 | 2015-10-14 15:02:44 -0700 | [diff] [blame] | 1482 | remote_desc_->description()->contents()[mediacontent_index]; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1483 | std::vector<cricket::Candidate> candidates; |
| 1484 | candidates.push_back(candidate->candidate()); |
| 1485 | // Invoking BaseSession method to handle remote candidates. |
| 1486 | std::string error; |
deadbeef | d59daf8 | 2015-10-14 15:02:44 -0700 | [diff] [blame] | 1487 | if (transport_controller_->AddRemoteCandidates(content.name, candidates, |
| 1488 | &error)) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1489 | // Candidates successfully submitted for checking. |
| 1490 | if (ice_connection_state_ == PeerConnectionInterface::kIceConnectionNew || |
| 1491 | ice_connection_state_ == |
| 1492 | PeerConnectionInterface::kIceConnectionDisconnected) { |
| 1493 | // If state is New, then the session has just gotten its first remote ICE |
| 1494 | // candidates, so go to Checking. |
| 1495 | // If state is Disconnected, the session is re-using old candidates or |
| 1496 | // receiving additional ones, so go to Checking. |
| 1497 | // If state is Connected, stay Connected. |
| 1498 | // TODO(bemasc): If state is Connected, and the new candidates are for a |
| 1499 | // newly added transport, then the state actually _should_ move to |
| 1500 | // checking. Add a way to distinguish that case. |
| 1501 | SetIceConnectionState(PeerConnectionInterface::kIceConnectionChecking); |
| 1502 | } |
| 1503 | // TODO(bemasc): If state is Completed, go back to Connected. |
| 1504 | } else { |
fischman@webrtc.org | 4f2bd68 | 2014-03-28 18:13:34 +0000 | [diff] [blame] | 1505 | if (!error.empty()) { |
| 1506 | LOG(LS_WARNING) << error; |
| 1507 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1508 | } |
| 1509 | return true; |
| 1510 | } |
| 1511 | |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 1512 | void WebRtcSession::RemoveUnusedChannels(const SessionDescription* desc) { |
buildbot@webrtc.org | b4c7b09 | 2014-08-25 12:11:58 +0000 | [diff] [blame] | 1513 | // Destroy video_channel_ first since it may have a pointer to the |
| 1514 | // voice_channel_. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1515 | const cricket::ContentInfo* video_info = |
| 1516 | cricket::GetFirstVideoContent(desc); |
| 1517 | if ((!video_info || video_info->rejected) && video_channel_) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1518 | SignalVideoChannelDestroyed(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1519 | channel_manager_->DestroyVideoChannel(video_channel_.release()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1520 | } |
| 1521 | |
buildbot@webrtc.org | b4c7b09 | 2014-08-25 12:11:58 +0000 | [diff] [blame] | 1522 | const cricket::ContentInfo* voice_info = |
| 1523 | cricket::GetFirstAudioContent(desc); |
| 1524 | if ((!voice_info || voice_info->rejected) && voice_channel_) { |
buildbot@webrtc.org | b4c7b09 | 2014-08-25 12:11:58 +0000 | [diff] [blame] | 1525 | SignalVoiceChannelDestroyed(); |
Fredrik Solenberg | 709ed67 | 2015-09-15 12:26:33 +0200 | [diff] [blame] | 1526 | channel_manager_->DestroyVoiceChannel(voice_channel_.release()); |
buildbot@webrtc.org | b4c7b09 | 2014-08-25 12:11:58 +0000 | [diff] [blame] | 1527 | } |
| 1528 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1529 | const cricket::ContentInfo* data_info = |
| 1530 | cricket::GetFirstDataContent(desc); |
| 1531 | if ((!data_info || data_info->rejected) && data_channel_) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1532 | SignalDataChannelDestroyed(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1533 | channel_manager_->DestroyDataChannel(data_channel_.release()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1534 | } |
| 1535 | } |
| 1536 | |
skvlad | 6c87a67 | 2016-05-17 17:49:52 -0700 | [diff] [blame] | 1537 | // Returns the name of the transport channel when BUNDLE is enabled, or nullptr |
| 1538 | // if the channel is not part of any bundle. |
| 1539 | const std::string* WebRtcSession::GetBundleTransportName( |
| 1540 | const cricket::ContentInfo* content, |
| 1541 | const cricket::ContentGroup* bundle) { |
| 1542 | if (!bundle) { |
| 1543 | return nullptr; |
| 1544 | } |
| 1545 | const std::string* first_content_name = bundle->FirstContentName(); |
| 1546 | if (!first_content_name) { |
| 1547 | LOG(LS_WARNING) << "Tried to BUNDLE with no contents."; |
| 1548 | return nullptr; |
| 1549 | } |
| 1550 | if (!bundle->HasContentName(content->name)) { |
| 1551 | LOG(LS_WARNING) << content->name << " is not part of any bundle group"; |
| 1552 | return nullptr; |
| 1553 | } |
| 1554 | LOG(LS_INFO) << "Bundling " << content->name << " on " << *first_content_name; |
| 1555 | return first_content_name; |
| 1556 | } |
| 1557 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1558 | bool WebRtcSession::CreateChannels(const SessionDescription* desc) { |
skvlad | 6c87a67 | 2016-05-17 17:49:52 -0700 | [diff] [blame] | 1559 | const cricket::ContentGroup* bundle_group = nullptr; |
| 1560 | if (bundle_policy_ == PeerConnectionInterface::kBundlePolicyMaxBundle) { |
| 1561 | bundle_group = desc->GetGroupByName(cricket::GROUP_TYPE_BUNDLE); |
| 1562 | if (!bundle_group) { |
| 1563 | LOG(LS_WARNING) << "max-bundle specified without BUNDLE specified"; |
| 1564 | return false; |
| 1565 | } |
| 1566 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1567 | // Creating the media channels and transport proxies. |
| 1568 | const cricket::ContentInfo* voice = cricket::GetFirstAudioContent(desc); |
| 1569 | if (voice && !voice->rejected && !voice_channel_) { |
skvlad | 6c87a67 | 2016-05-17 17:49:52 -0700 | [diff] [blame] | 1570 | if (!CreateVoiceChannel(voice, |
| 1571 | GetBundleTransportName(voice, bundle_group))) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1572 | LOG(LS_ERROR) << "Failed to create voice channel."; |
| 1573 | return false; |
| 1574 | } |
| 1575 | } |
| 1576 | |
| 1577 | const cricket::ContentInfo* video = cricket::GetFirstVideoContent(desc); |
| 1578 | if (video && !video->rejected && !video_channel_) { |
skvlad | 6c87a67 | 2016-05-17 17:49:52 -0700 | [diff] [blame] | 1579 | if (!CreateVideoChannel(video, |
| 1580 | GetBundleTransportName(video, bundle_group))) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1581 | LOG(LS_ERROR) << "Failed to create video channel."; |
| 1582 | return false; |
| 1583 | } |
| 1584 | } |
| 1585 | |
| 1586 | const cricket::ContentInfo* data = cricket::GetFirstDataContent(desc); |
| 1587 | if (data_channel_type_ != cricket::DCT_NONE && |
pthatcher@webrtc.org | 4eeef58 | 2015-03-16 19:34:23 +0000 | [diff] [blame] | 1588 | data && !data->rejected && !data_channel_) { |
skvlad | 6c87a67 | 2016-05-17 17:49:52 -0700 | [diff] [blame] | 1589 | if (!CreateDataChannel(data, GetBundleTransportName(data, bundle_group))) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1590 | LOG(LS_ERROR) << "Failed to create data channel."; |
| 1591 | return false; |
| 1592 | } |
| 1593 | } |
| 1594 | |
| 1595 | return true; |
| 1596 | } |
| 1597 | |
skvlad | 6c87a67 | 2016-05-17 17:49:52 -0700 | [diff] [blame] | 1598 | bool WebRtcSession::CreateVoiceChannel(const cricket::ContentInfo* content, |
| 1599 | const std::string* bundle_transport) { |
| 1600 | bool require_rtcp_mux = |
| 1601 | rtcp_mux_policy_ == PeerConnectionInterface::kRtcpMuxPolicyRequire; |
| 1602 | bool create_rtcp_transport_channel = !require_rtcp_mux; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1603 | voice_channel_.reset(channel_manager_->CreateVoiceChannel( |
skvlad | 6c87a67 | 2016-05-17 17:49:52 -0700 | [diff] [blame] | 1604 | media_controller_, transport_controller_.get(), content->name, |
| 1605 | bundle_transport, create_rtcp_transport_channel, audio_options_)); |
pthatcher@webrtc.org | 4eeef58 | 2015-03-16 19:34:23 +0000 | [diff] [blame] | 1606 | if (!voice_channel_) { |
wu@webrtc.org | de30501 | 2013-10-31 15:40:38 +0000 | [diff] [blame] | 1607 | return false; |
pthatcher@webrtc.org | 4eeef58 | 2015-03-16 19:34:23 +0000 | [diff] [blame] | 1608 | } |
skvlad | 6c87a67 | 2016-05-17 17:49:52 -0700 | [diff] [blame] | 1609 | if (require_rtcp_mux) { |
| 1610 | voice_channel_->ActivateRtcpMux(); |
| 1611 | } |
wu@webrtc.org | de30501 | 2013-10-31 15:40:38 +0000 | [diff] [blame] | 1612 | |
pthatcher@webrtc.org | 4eeef58 | 2015-03-16 19:34:23 +0000 | [diff] [blame] | 1613 | voice_channel_->SignalDtlsSetupFailure.connect( |
| 1614 | this, &WebRtcSession::OnDtlsSetupFailure); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1615 | |
| 1616 | SignalVoiceChannelCreated(); |
Danil Chapovalov | 33b01f2 | 2016-05-11 19:55:27 +0200 | [diff] [blame] | 1617 | voice_channel_->SignalSentPacket.connect(this, |
| 1618 | &WebRtcSession::OnSentPacket_w); |
wu@webrtc.org | de30501 | 2013-10-31 15:40:38 +0000 | [diff] [blame] | 1619 | return true; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1620 | } |
| 1621 | |
skvlad | 6c87a67 | 2016-05-17 17:49:52 -0700 | [diff] [blame] | 1622 | bool WebRtcSession::CreateVideoChannel(const cricket::ContentInfo* content, |
| 1623 | const std::string* bundle_transport) { |
| 1624 | bool require_rtcp_mux = |
| 1625 | rtcp_mux_policy_ == PeerConnectionInterface::kRtcpMuxPolicyRequire; |
| 1626 | bool create_rtcp_transport_channel = !require_rtcp_mux; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1627 | video_channel_.reset(channel_manager_->CreateVideoChannel( |
skvlad | 6c87a67 | 2016-05-17 17:49:52 -0700 | [diff] [blame] | 1628 | media_controller_, transport_controller_.get(), content->name, |
| 1629 | bundle_transport, create_rtcp_transport_channel, video_options_)); |
pthatcher@webrtc.org | 4eeef58 | 2015-03-16 19:34:23 +0000 | [diff] [blame] | 1630 | if (!video_channel_) { |
| 1631 | return false; |
| 1632 | } |
skvlad | 6c87a67 | 2016-05-17 17:49:52 -0700 | [diff] [blame] | 1633 | if (require_rtcp_mux) { |
| 1634 | video_channel_->ActivateRtcpMux(); |
| 1635 | } |
pthatcher@webrtc.org | 4eeef58 | 2015-03-16 19:34:23 +0000 | [diff] [blame] | 1636 | video_channel_->SignalDtlsSetupFailure.connect( |
| 1637 | this, &WebRtcSession::OnDtlsSetupFailure); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1638 | |
| 1639 | SignalVideoChannelCreated(); |
Danil Chapovalov | 33b01f2 | 2016-05-11 19:55:27 +0200 | [diff] [blame] | 1640 | video_channel_->SignalSentPacket.connect(this, |
| 1641 | &WebRtcSession::OnSentPacket_w); |
pthatcher@webrtc.org | 4eeef58 | 2015-03-16 19:34:23 +0000 | [diff] [blame] | 1642 | return true; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1643 | } |
| 1644 | |
skvlad | 6c87a67 | 2016-05-17 17:49:52 -0700 | [diff] [blame] | 1645 | bool WebRtcSession::CreateDataChannel(const cricket::ContentInfo* content, |
| 1646 | const std::string* bundle_transport) { |
wu@webrtc.org | cecfd18 | 2013-10-30 05:18:12 +0000 | [diff] [blame] | 1647 | bool sctp = (data_channel_type_ == cricket::DCT_SCTP); |
skvlad | 6c87a67 | 2016-05-17 17:49:52 -0700 | [diff] [blame] | 1648 | bool require_rtcp_mux = |
| 1649 | rtcp_mux_policy_ == PeerConnectionInterface::kRtcpMuxPolicyRequire; |
| 1650 | bool create_rtcp_transport_channel = !sctp && !require_rtcp_mux; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1651 | data_channel_.reset(channel_manager_->CreateDataChannel( |
skvlad | 6c87a67 | 2016-05-17 17:49:52 -0700 | [diff] [blame] | 1652 | transport_controller_.get(), content->name, bundle_transport, |
| 1653 | create_rtcp_transport_channel, data_channel_type_)); |
pthatcher@webrtc.org | 4eeef58 | 2015-03-16 19:34:23 +0000 | [diff] [blame] | 1654 | if (!data_channel_) { |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 1655 | return false; |
| 1656 | } |
skvlad | 6c87a67 | 2016-05-17 17:49:52 -0700 | [diff] [blame] | 1657 | if (require_rtcp_mux) { |
| 1658 | data_channel_->ActivateRtcpMux(); |
| 1659 | } |
pthatcher@webrtc.org | 4eeef58 | 2015-03-16 19:34:23 +0000 | [diff] [blame] | 1660 | |
wu@webrtc.org | cecfd18 | 2013-10-30 05:18:12 +0000 | [diff] [blame] | 1661 | if (sctp) { |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 1662 | data_channel_->SignalDataReceived.connect( |
| 1663 | this, &WebRtcSession::OnDataChannelMessageReceived); |
wu@webrtc.org | cecfd18 | 2013-10-30 05:18:12 +0000 | [diff] [blame] | 1664 | } |
pthatcher@webrtc.org | 4eeef58 | 2015-03-16 19:34:23 +0000 | [diff] [blame] | 1665 | |
| 1666 | data_channel_->SignalDtlsSetupFailure.connect( |
| 1667 | this, &WebRtcSession::OnDtlsSetupFailure); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1668 | |
| 1669 | SignalDataChannelCreated(); |
Danil Chapovalov | 33b01f2 | 2016-05-11 19:55:27 +0200 | [diff] [blame] | 1670 | data_channel_->SignalSentPacket.connect(this, &WebRtcSession::OnSentPacket_w); |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 1671 | return true; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1672 | } |
| 1673 | |
pthatcher@webrtc.org | 4eeef58 | 2015-03-16 19:34:23 +0000 | [diff] [blame] | 1674 | void WebRtcSession::OnDtlsSetupFailure(cricket::BaseChannel*, bool rtcp) { |
deadbeef | d59daf8 | 2015-10-14 15:02:44 -0700 | [diff] [blame] | 1675 | SetError(ERROR_TRANSPORT, |
| 1676 | rtcp ? kDtlsSetupFailureRtcp : kDtlsSetupFailureRtp); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1677 | } |
| 1678 | |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 1679 | void WebRtcSession::OnDataChannelMessageReceived( |
| 1680 | cricket::DataChannel* channel, |
| 1681 | const cricket::ReceiveDataParams& params, |
jbauch | eec21bd | 2016-03-20 06:15:43 -0700 | [diff] [blame] | 1682 | const rtc::CopyOnWriteBuffer& payload) { |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1683 | RTC_DCHECK(data_channel_type_ == cricket::DCT_SCTP); |
| 1684 | if (params.type == cricket::DMT_CONTROL && IsOpenMessage(payload)) { |
| 1685 | // Received OPEN message; parse and signal that a new data channel should |
| 1686 | // be created. |
| 1687 | std::string label; |
| 1688 | InternalDataChannelInit config; |
| 1689 | config.id = params.ssrc; |
| 1690 | if (!ParseDataChannelOpenMessage(payload, &label, &config)) { |
| 1691 | LOG(LS_WARNING) << "Failed to parse the OPEN message for sid " |
| 1692 | << params.ssrc; |
| 1693 | return; |
| 1694 | } |
| 1695 | config.open_handshake_role = InternalDataChannelInit::kAcker; |
| 1696 | SignalDataChannelOpenMessage(label, config); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1697 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1698 | // Otherwise ignore the message. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1699 | } |
| 1700 | |
henrike@webrtc.org | 1e09a71 | 2013-07-26 19:17:59 +0000 | [diff] [blame] | 1701 | // Returns false if bundle is enabled and rtcp_mux is disabled. |
sergeyu@chromium.org | 0be6aa0 | 2013-08-23 23:21:25 +0000 | [diff] [blame] | 1702 | bool WebRtcSession::ValidateBundleSettings(const SessionDescription* desc) { |
henrike@webrtc.org | 1e09a71 | 2013-07-26 19:17:59 +0000 | [diff] [blame] | 1703 | bool bundle_enabled = desc->HasGroup(cricket::GROUP_TYPE_BUNDLE); |
| 1704 | if (!bundle_enabled) |
| 1705 | return true; |
| 1706 | |
| 1707 | const cricket::ContentGroup* bundle_group = |
| 1708 | desc->GetGroupByName(cricket::GROUP_TYPE_BUNDLE); |
| 1709 | ASSERT(bundle_group != NULL); |
| 1710 | |
| 1711 | const cricket::ContentInfos& contents = desc->contents(); |
| 1712 | for (cricket::ContentInfos::const_iterator citer = contents.begin(); |
| 1713 | citer != contents.end(); ++citer) { |
| 1714 | const cricket::ContentInfo* content = (&*citer); |
| 1715 | ASSERT(content != NULL); |
| 1716 | if (bundle_group->HasContentName(content->name) && |
| 1717 | !content->rejected && content->type == cricket::NS_JINGLE_RTP) { |
| 1718 | if (!HasRtcpMuxEnabled(content)) |
| 1719 | return false; |
| 1720 | } |
| 1721 | } |
| 1722 | // RTCP-MUX is enabled in all the contents. |
| 1723 | return true; |
| 1724 | } |
| 1725 | |
| 1726 | bool WebRtcSession::HasRtcpMuxEnabled( |
| 1727 | const cricket::ContentInfo* content) { |
| 1728 | const cricket::MediaContentDescription* description = |
| 1729 | static_cast<cricket::MediaContentDescription*>(content->description); |
| 1730 | return description->rtcp_mux(); |
| 1731 | } |
| 1732 | |
sergeyu@chromium.org | 0be6aa0 | 2013-08-23 23:21:25 +0000 | [diff] [blame] | 1733 | bool WebRtcSession::ValidateSessionDescription( |
| 1734 | const SessionDescriptionInterface* sdesc, |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 1735 | cricket::ContentSource source, std::string* err_desc) { |
| 1736 | std::string type; |
deadbeef | d59daf8 | 2015-10-14 15:02:44 -0700 | [diff] [blame] | 1737 | if (error() != ERROR_NONE) { |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 1738 | return BadSdp(source, type, GetSessionErrorMsg(), err_desc); |
sergeyu@chromium.org | 0be6aa0 | 2013-08-23 23:21:25 +0000 | [diff] [blame] | 1739 | } |
| 1740 | |
| 1741 | if (!sdesc || !sdesc->description()) { |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 1742 | return BadSdp(source, type, kInvalidSdp, err_desc); |
sergeyu@chromium.org | 0be6aa0 | 2013-08-23 23:21:25 +0000 | [diff] [blame] | 1743 | } |
| 1744 | |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 1745 | type = sdesc->type(); |
sergeyu@chromium.org | 0be6aa0 | 2013-08-23 23:21:25 +0000 | [diff] [blame] | 1746 | Action action = GetAction(sdesc->type()); |
| 1747 | if (source == cricket::CS_LOCAL) { |
| 1748 | if (!ExpectSetLocalDescription(action)) |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 1749 | return BadLocalSdp(type, BadStateErrMsg(state()), err_desc); |
sergeyu@chromium.org | 0be6aa0 | 2013-08-23 23:21:25 +0000 | [diff] [blame] | 1750 | } else { |
| 1751 | if (!ExpectSetRemoteDescription(action)) |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 1752 | return BadRemoteSdp(type, BadStateErrMsg(state()), err_desc); |
sergeyu@chromium.org | 0be6aa0 | 2013-08-23 23:21:25 +0000 | [diff] [blame] | 1753 | } |
| 1754 | |
| 1755 | // Verify crypto settings. |
mallinath@webrtc.org | a27be8e | 2013-09-27 23:04:10 +0000 | [diff] [blame] | 1756 | std::string crypto_error; |
henrike@webrtc.org | b90991d | 2014-03-04 19:54:57 +0000 | [diff] [blame] | 1757 | if ((webrtc_session_desc_factory_->SdesPolicy() == cricket::SEC_REQUIRED || |
| 1758 | dtls_enabled_) && |
mallinath@webrtc.org | a27be8e | 2013-09-27 23:04:10 +0000 | [diff] [blame] | 1759 | !VerifyCrypto(sdesc->description(), dtls_enabled_, &crypto_error)) { |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 1760 | return BadSdp(source, type, crypto_error, err_desc); |
sergeyu@chromium.org | 0be6aa0 | 2013-08-23 23:21:25 +0000 | [diff] [blame] | 1761 | } |
| 1762 | |
mallinath@webrtc.org | 19f27e6 | 2013-10-13 17:18:27 +0000 | [diff] [blame] | 1763 | // Verify ice-ufrag and ice-pwd. |
| 1764 | if (!VerifyIceUfragPwdPresent(sdesc->description())) { |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 1765 | return BadSdp(source, type, kSdpWithoutIceUfragPwd, err_desc); |
mallinath@webrtc.org | 19f27e6 | 2013-10-13 17:18:27 +0000 | [diff] [blame] | 1766 | } |
| 1767 | |
sergeyu@chromium.org | 0be6aa0 | 2013-08-23 23:21:25 +0000 | [diff] [blame] | 1768 | if (!ValidateBundleSettings(sdesc->description())) { |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 1769 | return BadSdp(source, type, kBundleWithoutRtcpMux, err_desc); |
sergeyu@chromium.org | 0be6aa0 | 2013-08-23 23:21:25 +0000 | [diff] [blame] | 1770 | } |
| 1771 | |
skvlad | 6c87a67 | 2016-05-17 17:49:52 -0700 | [diff] [blame] | 1772 | // TODO(skvlad): When the local rtcp-mux policy is Require, reject any |
| 1773 | // m-lines that do not rtcp-mux enabled. |
| 1774 | |
sergeyu@chromium.org | 0be6aa0 | 2013-08-23 23:21:25 +0000 | [diff] [blame] | 1775 | // Verify m-lines in Answer when compared against Offer. |
| 1776 | if (action == kAnswer) { |
| 1777 | const cricket::SessionDescription* offer_desc = |
deadbeef | d59daf8 | 2015-10-14 15:02:44 -0700 | [diff] [blame] | 1778 | (source == cricket::CS_LOCAL) ? remote_desc_->description() |
| 1779 | : local_desc_->description(); |
sergeyu@chromium.org | 0be6aa0 | 2013-08-23 23:21:25 +0000 | [diff] [blame] | 1780 | if (!VerifyMediaDescriptions(sdesc->description(), offer_desc)) { |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 1781 | return BadAnswerSdp(source, kMlineMismatch, err_desc); |
sergeyu@chromium.org | 0be6aa0 | 2013-08-23 23:21:25 +0000 | [diff] [blame] | 1782 | } |
| 1783 | } |
| 1784 | |
| 1785 | return true; |
| 1786 | } |
| 1787 | |
| 1788 | bool WebRtcSession::ExpectSetLocalDescription(Action action) { |
| 1789 | return ((action == kOffer && state() == STATE_INIT) || |
| 1790 | // update local offer |
deadbeef | d59daf8 | 2015-10-14 15:02:44 -0700 | [diff] [blame] | 1791 | (action == kOffer && state() == STATE_SENTOFFER) || |
sergeyu@chromium.org | 0be6aa0 | 2013-08-23 23:21:25 +0000 | [diff] [blame] | 1792 | // update the current ongoing session. |
sergeyu@chromium.org | 0be6aa0 | 2013-08-23 23:21:25 +0000 | [diff] [blame] | 1793 | (action == kOffer && state() == STATE_INPROGRESS) || |
| 1794 | // accept remote offer |
deadbeef | d59daf8 | 2015-10-14 15:02:44 -0700 | [diff] [blame] | 1795 | (action == kAnswer && state() == STATE_RECEIVEDOFFER) || |
| 1796 | (action == kAnswer && state() == STATE_SENTPRANSWER) || |
| 1797 | (action == kPrAnswer && state() == STATE_RECEIVEDOFFER) || |
| 1798 | (action == kPrAnswer && state() == STATE_SENTPRANSWER)); |
sergeyu@chromium.org | 0be6aa0 | 2013-08-23 23:21:25 +0000 | [diff] [blame] | 1799 | } |
| 1800 | |
| 1801 | bool WebRtcSession::ExpectSetRemoteDescription(Action action) { |
| 1802 | return ((action == kOffer && state() == STATE_INIT) || |
| 1803 | // update remote offer |
deadbeef | d59daf8 | 2015-10-14 15:02:44 -0700 | [diff] [blame] | 1804 | (action == kOffer && state() == STATE_RECEIVEDOFFER) || |
sergeyu@chromium.org | 0be6aa0 | 2013-08-23 23:21:25 +0000 | [diff] [blame] | 1805 | // update the current ongoing session |
sergeyu@chromium.org | 0be6aa0 | 2013-08-23 23:21:25 +0000 | [diff] [blame] | 1806 | (action == kOffer && state() == STATE_INPROGRESS) || |
| 1807 | // accept local offer |
deadbeef | d59daf8 | 2015-10-14 15:02:44 -0700 | [diff] [blame] | 1808 | (action == kAnswer && state() == STATE_SENTOFFER) || |
| 1809 | (action == kAnswer && state() == STATE_RECEIVEDPRANSWER) || |
| 1810 | (action == kPrAnswer && state() == STATE_SENTOFFER) || |
| 1811 | (action == kPrAnswer && state() == STATE_RECEIVEDPRANSWER)); |
sergeyu@chromium.org | 0be6aa0 | 2013-08-23 23:21:25 +0000 | [diff] [blame] | 1812 | } |
| 1813 | |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 1814 | std::string WebRtcSession::GetSessionErrorMsg() { |
| 1815 | std::ostringstream desc; |
| 1816 | desc << kSessionError << GetErrorCodeString(error()) << ". "; |
| 1817 | desc << kSessionErrorDesc << error_desc() << "."; |
| 1818 | return desc.str(); |
| 1819 | } |
| 1820 | |
jiayl@webrtc.org | e10d28c | 2014-07-17 17:07:49 +0000 | [diff] [blame] | 1821 | // We need to check the local/remote description for the Transport instead of |
| 1822 | // the session, because a new Transport added during renegotiation may have |
| 1823 | // them unset while the session has them set from the previous negotiation. |
| 1824 | // Not doing so may trigger the auto generation of transport description and |
| 1825 | // mess up DTLS identity information, ICE credential, etc. |
| 1826 | bool WebRtcSession::ReadyToUseRemoteCandidate( |
| 1827 | const IceCandidateInterface* candidate, |
| 1828 | const SessionDescriptionInterface* remote_desc, |
| 1829 | bool* valid) { |
| 1830 | *valid = true;; |
jiayl@webrtc.org | e10d28c | 2014-07-17 17:07:49 +0000 | [diff] [blame] | 1831 | |
| 1832 | const SessionDescriptionInterface* current_remote_desc = |
deadbeef | d59daf8 | 2015-10-14 15:02:44 -0700 | [diff] [blame] | 1833 | remote_desc ? remote_desc : remote_desc_.get(); |
jiayl@webrtc.org | e10d28c | 2014-07-17 17:07:49 +0000 | [diff] [blame] | 1834 | |
deadbeef | d59daf8 | 2015-10-14 15:02:44 -0700 | [diff] [blame] | 1835 | if (!current_remote_desc) { |
jiayl@webrtc.org | e10d28c | 2014-07-17 17:07:49 +0000 | [diff] [blame] | 1836 | return false; |
deadbeef | d59daf8 | 2015-10-14 15:02:44 -0700 | [diff] [blame] | 1837 | } |
jiayl@webrtc.org | e10d28c | 2014-07-17 17:07:49 +0000 | [diff] [blame] | 1838 | |
| 1839 | size_t mediacontent_index = |
| 1840 | static_cast<size_t>(candidate->sdp_mline_index()); |
| 1841 | size_t remote_content_size = |
| 1842 | current_remote_desc->description()->contents().size(); |
| 1843 | if (mediacontent_index >= remote_content_size) { |
Honghai Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 1844 | LOG(LS_ERROR) << "ReadyToUseRemoteCandidate: Invalid candidate media index " |
| 1845 | << mediacontent_index; |
jiayl@webrtc.org | e10d28c | 2014-07-17 17:07:49 +0000 | [diff] [blame] | 1846 | |
| 1847 | *valid = false; |
| 1848 | return false; |
| 1849 | } |
| 1850 | |
| 1851 | cricket::ContentInfo content = |
| 1852 | current_remote_desc->description()->contents()[mediacontent_index]; |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 1853 | cricket::BaseChannel* channel = GetChannel(content.name); |
| 1854 | if (!channel) { |
| 1855 | return false; |
| 1856 | } |
jiayl@webrtc.org | e10d28c | 2014-07-17 17:07:49 +0000 | [diff] [blame] | 1857 | |
deadbeef | d59daf8 | 2015-10-14 15:02:44 -0700 | [diff] [blame] | 1858 | return transport_controller_->ReadyForRemoteCandidates( |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 1859 | channel->transport_name()); |
jiayl@webrtc.org | e10d28c | 2014-07-17 17:07:49 +0000 | [diff] [blame] | 1860 | } |
| 1861 | |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 1862 | void WebRtcSession::OnTransportControllerGatheringState( |
| 1863 | cricket::IceGatheringState state) { |
| 1864 | ASSERT(signaling_thread()->IsCurrent()); |
| 1865 | if (state == cricket::kIceGatheringGathering) { |
| 1866 | if (ice_observer_) { |
| 1867 | ice_observer_->OnIceGatheringChange( |
| 1868 | PeerConnectionInterface::kIceGatheringGathering); |
| 1869 | } |
| 1870 | } else if (state == cricket::kIceGatheringComplete) { |
| 1871 | if (ice_observer_) { |
| 1872 | ice_observer_->OnIceGatheringChange( |
| 1873 | PeerConnectionInterface::kIceGatheringComplete); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 1874 | } |
| 1875 | } |
| 1876 | } |
| 1877 | |
| 1878 | void WebRtcSession::ReportTransportStats() { |
| 1879 | // Use a set so we don't report the same stats twice if two channels share |
| 1880 | // a transport. |
| 1881 | std::set<std::string> transport_names; |
| 1882 | if (voice_channel()) { |
| 1883 | transport_names.insert(voice_channel()->transport_name()); |
| 1884 | } |
| 1885 | if (video_channel()) { |
| 1886 | transport_names.insert(video_channel()->transport_name()); |
| 1887 | } |
| 1888 | if (data_channel()) { |
| 1889 | transport_names.insert(data_channel()->transport_name()); |
| 1890 | } |
| 1891 | for (const auto& name : transport_names) { |
| 1892 | cricket::TransportStats stats; |
deadbeef | d59daf8 | 2015-10-14 15:02:44 -0700 | [diff] [blame] | 1893 | if (transport_controller_->GetStats(name, &stats)) { |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 1894 | ReportBestConnectionState(stats); |
| 1895 | ReportNegotiatedCiphers(stats); |
| 1896 | } |
| 1897 | } |
| 1898 | } |
guoweis@webrtc.org | 7169afd | 2014-12-04 17:59:29 +0000 | [diff] [blame] | 1899 | // Walk through the ConnectionInfos to gather best connection usage |
| 1900 | // for IPv4 and IPv6. |
jbauch | ac8869e | 2015-07-03 01:36:14 -0700 | [diff] [blame] | 1901 | void WebRtcSession::ReportBestConnectionState( |
| 1902 | const cricket::TransportStats& stats) { |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 1903 | RTC_DCHECK(metrics_observer_ != NULL); |
guoweis@webrtc.org | 7169afd | 2014-12-04 17:59:29 +0000 | [diff] [blame] | 1904 | for (cricket::TransportChannelStatsList::const_iterator it = |
| 1905 | stats.channel_stats.begin(); |
| 1906 | it != stats.channel_stats.end(); ++it) { |
| 1907 | for (cricket::ConnectionInfos::const_iterator it_info = |
| 1908 | it->connection_infos.begin(); |
| 1909 | it_info != it->connection_infos.end(); ++it_info) { |
| 1910 | if (!it_info->best_connection) { |
| 1911 | continue; |
| 1912 | } |
Guo-wei Shieh | 3d564c1 | 2015-08-19 16:51:15 -0700 | [diff] [blame] | 1913 | |
| 1914 | PeerConnectionEnumCounterType type = kPeerConnectionEnumCounterMax; |
| 1915 | const cricket::Candidate& local = it_info->local_candidate; |
| 1916 | const cricket::Candidate& remote = it_info->remote_candidate; |
| 1917 | |
| 1918 | // Increment the counter for IceCandidatePairType. |
| 1919 | if (local.protocol() == cricket::TCP_PROTOCOL_NAME || |
| 1920 | (local.type() == RELAY_PORT_TYPE && |
| 1921 | local.relay_protocol() == cricket::TCP_PROTOCOL_NAME)) { |
| 1922 | type = kEnumCounterIceCandidatePairTypeTcp; |
| 1923 | } else if (local.protocol() == cricket::UDP_PROTOCOL_NAME) { |
| 1924 | type = kEnumCounterIceCandidatePairTypeUdp; |
guoweis@webrtc.org | 7169afd | 2014-12-04 17:59:29 +0000 | [diff] [blame] | 1925 | } else { |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 1926 | RTC_CHECK(0); |
guoweis@webrtc.org | 7169afd | 2014-12-04 17:59:29 +0000 | [diff] [blame] | 1927 | } |
Guo-wei Shieh | 3d564c1 | 2015-08-19 16:51:15 -0700 | [diff] [blame] | 1928 | metrics_observer_->IncrementEnumCounter( |
| 1929 | type, GetIceCandidatePairCounter(local, remote), |
| 1930 | kIceCandidatePairMax); |
| 1931 | |
| 1932 | // Increment the counter for IP type. |
| 1933 | if (local.address().family() == AF_INET) { |
Guo-wei Shieh | 3d564c1 | 2015-08-19 16:51:15 -0700 | [diff] [blame] | 1934 | metrics_observer_->IncrementEnumCounter( |
| 1935 | kEnumCounterAddressFamily, kBestConnections_IPv4, |
| 1936 | kPeerConnectionAddressFamilyCounter_Max); |
| 1937 | |
| 1938 | } else if (local.address().family() == AF_INET6) { |
Guo-wei Shieh | 3d564c1 | 2015-08-19 16:51:15 -0700 | [diff] [blame] | 1939 | metrics_observer_->IncrementEnumCounter( |
| 1940 | kEnumCounterAddressFamily, kBestConnections_IPv6, |
| 1941 | kPeerConnectionAddressFamilyCounter_Max); |
| 1942 | } else { |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 1943 | RTC_CHECK(0); |
Guo-wei Shieh | 3d564c1 | 2015-08-19 16:51:15 -0700 | [diff] [blame] | 1944 | } |
| 1945 | |
guoweis@webrtc.org | 7169afd | 2014-12-04 17:59:29 +0000 | [diff] [blame] | 1946 | return; |
| 1947 | } |
| 1948 | } |
| 1949 | } |
| 1950 | |
jbauch | ac8869e | 2015-07-03 01:36:14 -0700 | [diff] [blame] | 1951 | void WebRtcSession::ReportNegotiatedCiphers( |
| 1952 | const cricket::TransportStats& stats) { |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 1953 | RTC_DCHECK(metrics_observer_ != NULL); |
jbauch | ac8869e | 2015-07-03 01:36:14 -0700 | [diff] [blame] | 1954 | if (!dtls_enabled_ || stats.channel_stats.empty()) { |
| 1955 | return; |
| 1956 | } |
| 1957 | |
Guo-wei Shieh | 521ed7b | 2015-11-18 19:41:53 -0800 | [diff] [blame] | 1958 | int srtp_crypto_suite = stats.channel_stats[0].srtp_crypto_suite; |
| 1959 | int ssl_cipher_suite = stats.channel_stats[0].ssl_cipher_suite; |
| 1960 | if (srtp_crypto_suite == rtc::SRTP_INVALID_CRYPTO_SUITE && |
| 1961 | ssl_cipher_suite == rtc::TLS_NULL_WITH_NULL_NULL) { |
jbauch | ac8869e | 2015-07-03 01:36:14 -0700 | [diff] [blame] | 1962 | return; |
| 1963 | } |
| 1964 | |
Guo-wei Shieh | 456696a | 2015-09-30 21:48:54 -0700 | [diff] [blame] | 1965 | PeerConnectionEnumCounterType srtp_counter_type; |
| 1966 | PeerConnectionEnumCounterType ssl_counter_type; |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 1967 | if (stats.transport_name == cricket::CN_AUDIO) { |
Guo-wei Shieh | 456696a | 2015-09-30 21:48:54 -0700 | [diff] [blame] | 1968 | srtp_counter_type = kEnumCounterAudioSrtpCipher; |
| 1969 | ssl_counter_type = kEnumCounterAudioSslCipher; |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 1970 | } else if (stats.transport_name == cricket::CN_VIDEO) { |
Guo-wei Shieh | 456696a | 2015-09-30 21:48:54 -0700 | [diff] [blame] | 1971 | srtp_counter_type = kEnumCounterVideoSrtpCipher; |
| 1972 | ssl_counter_type = kEnumCounterVideoSslCipher; |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 1973 | } else if (stats.transport_name == cricket::CN_DATA) { |
Guo-wei Shieh | 456696a | 2015-09-30 21:48:54 -0700 | [diff] [blame] | 1974 | srtp_counter_type = kEnumCounterDataSrtpCipher; |
| 1975 | ssl_counter_type = kEnumCounterDataSslCipher; |
jbauch | ac8869e | 2015-07-03 01:36:14 -0700 | [diff] [blame] | 1976 | } else { |
| 1977 | RTC_NOTREACHED(); |
| 1978 | return; |
| 1979 | } |
| 1980 | |
Guo-wei Shieh | 521ed7b | 2015-11-18 19:41:53 -0800 | [diff] [blame] | 1981 | if (srtp_crypto_suite != rtc::SRTP_INVALID_CRYPTO_SUITE) { |
| 1982 | metrics_observer_->IncrementSparseEnumCounter(srtp_counter_type, |
| 1983 | srtp_crypto_suite); |
jbauch | ac8869e | 2015-07-03 01:36:14 -0700 | [diff] [blame] | 1984 | } |
Guo-wei Shieh | 521ed7b | 2015-11-18 19:41:53 -0800 | [diff] [blame] | 1985 | if (ssl_cipher_suite != rtc::TLS_NULL_WITH_NULL_NULL) { |
| 1986 | metrics_observer_->IncrementSparseEnumCounter(ssl_counter_type, |
| 1987 | ssl_cipher_suite); |
jbauch | ac8869e | 2015-07-03 01:36:14 -0700 | [diff] [blame] | 1988 | } |
| 1989 | } |
| 1990 | |
Danil Chapovalov | 33b01f2 | 2016-05-11 19:55:27 +0200 | [diff] [blame] | 1991 | void WebRtcSession::OnSentPacket_w(const rtc::SentPacket& sent_packet) { |
stefan | c1aeaf0 | 2015-10-15 07:26:07 -0700 | [diff] [blame] | 1992 | RTC_DCHECK(worker_thread()->IsCurrent()); |
| 1993 | media_controller_->call_w()->OnSentPacket(sent_packet); |
| 1994 | } |
| 1995 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1996 | } // namespace webrtc |