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