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