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