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