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