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