henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1 | /* |
| 2 | * libjingle |
| 3 | * Copyright 2012, Google Inc. |
| 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 | |
| 30 | #include <algorithm> |
| 31 | #include <climits> |
| 32 | #include <vector> |
| 33 | |
| 34 | #include "talk/app/webrtc/jsepicecandidate.h" |
| 35 | #include "talk/app/webrtc/jsepsessiondescription.h" |
| 36 | #include "talk/app/webrtc/mediaconstraintsinterface.h" |
| 37 | #include "talk/app/webrtc/mediastreamsignaling.h" |
| 38 | #include "talk/app/webrtc/peerconnectioninterface.h" |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 39 | #include "talk/app/webrtc/webrtcsessiondescriptionfactory.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 40 | #include "talk/base/helpers.h" |
| 41 | #include "talk/base/logging.h" |
| 42 | #include "talk/base/stringencode.h" |
| 43 | #include "talk/media/base/constants.h" |
| 44 | #include "talk/media/base/videocapturer.h" |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 +0000 | [diff] [blame] | 45 | #include "talk/media/sctp/sctputils.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 46 | #include "talk/session/media/channel.h" |
| 47 | #include "talk/session/media/channelmanager.h" |
| 48 | #include "talk/session/media/mediasession.h" |
| 49 | |
| 50 | using cricket::ContentInfo; |
| 51 | using cricket::ContentInfos; |
| 52 | using cricket::MediaContentDescription; |
| 53 | using cricket::SessionDescription; |
| 54 | using cricket::TransportInfo; |
| 55 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 56 | namespace webrtc { |
| 57 | |
mallinath@webrtc.org | a27be8e | 2013-09-27 23:04:10 +0000 | [diff] [blame] | 58 | const char MediaConstraintsInterface::kInternalConstraintPrefix[] = "internal"; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 59 | |
| 60 | // Supported MediaConstraints. |
| 61 | // DTLS-SRTP pseudo-constraints. |
| 62 | const char MediaConstraintsInterface::kEnableDtlsSrtp[] = |
| 63 | "DtlsSrtpKeyAgreement"; |
| 64 | // DataChannel pseudo constraints. |
| 65 | const char MediaConstraintsInterface::kEnableRtpDataChannels[] = |
| 66 | "RtpDataChannels"; |
| 67 | // This constraint is for internal use only, representing the Chrome command |
| 68 | // line flag. So it is prefixed with kInternalConstraintPrefix so JS values |
| 69 | // will be removed. |
| 70 | const char MediaConstraintsInterface::kEnableSctpDataChannels[] = |
wu@webrtc.org | 97077a3 | 2013-10-25 21:18:33 +0000 | [diff] [blame^] | 71 | "deprecatedSctpDataChannels"; |
mallinath@webrtc.org | 7e809c3 | 2013-09-30 18:59:08 +0000 | [diff] [blame] | 72 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 73 | // Error messages |
| 74 | const char kSetLocalSdpFailed[] = "SetLocalDescription failed: "; |
| 75 | const char kSetRemoteSdpFailed[] = "SetRemoteDescription failed: "; |
| 76 | const char kCreateChannelFailed[] = "Failed to create channels."; |
henrike@webrtc.org | 1e09a71 | 2013-07-26 19:17:59 +0000 | [diff] [blame] | 77 | const char kBundleWithoutRtcpMux[] = "RTCP-MUX must be enabled when BUNDLE " |
| 78 | "is enabled."; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 79 | const char kInvalidCandidates[] = "Description contains invalid candidates."; |
| 80 | const char kInvalidSdp[] = "Invalid session description."; |
| 81 | const char kMlineMismatch[] = |
| 82 | "Offer and answer descriptions m-lines are not matching. " |
| 83 | "Rejecting answer."; |
| 84 | const char kSdpWithoutCrypto[] = "Called with a SDP without crypto enabled."; |
mallinath@webrtc.org | a27be8e | 2013-09-27 23:04:10 +0000 | [diff] [blame] | 85 | const char kSdpWithoutSdesAndDtlsDisabled[] = |
mallinath@webrtc.org | 19f27e6 | 2013-10-13 17:18:27 +0000 | [diff] [blame] | 86 | "Called with an SDP without SDES crypto and DTLS disabled locally."; |
| 87 | const char kSdpWithoutIceUfragPwd[] = |
| 88 | "Called with an SDP without ice-ufrag and ice-pwd."; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 89 | const char kSessionError[] = "Session error code: "; |
| 90 | const char kUpdateStateFailed[] = "Failed to update session state: "; |
| 91 | const char kPushDownOfferTDFailed[] = |
| 92 | "Failed to push down offer transport description."; |
| 93 | const char kPushDownPranswerTDFailed[] = |
| 94 | "Failed to push down pranswer transport description."; |
| 95 | const char kPushDownAnswerTDFailed[] = |
| 96 | "Failed to push down answer transport description."; |
| 97 | |
| 98 | // Compares |answer| against |offer|. Comparision is done |
| 99 | // for number of m-lines in answer against offer. If matches true will be |
| 100 | // returned otherwise false. |
| 101 | static bool VerifyMediaDescriptions( |
| 102 | const SessionDescription* answer, const SessionDescription* offer) { |
| 103 | if (offer->contents().size() != answer->contents().size()) |
| 104 | return false; |
| 105 | |
| 106 | for (size_t i = 0; i < offer->contents().size(); ++i) { |
| 107 | if ((offer->contents()[i].name) != answer->contents()[i].name) { |
| 108 | return false; |
| 109 | } |
| 110 | } |
| 111 | return true; |
| 112 | } |
| 113 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 114 | // Checks that each non-rejected content has SDES crypto keys or a DTLS |
| 115 | // fingerprint. Mismatches, such as replying with a DTLS fingerprint to SDES |
| 116 | // keys, will be caught in Transport negotiation, and backstopped by Channel's |
| 117 | // |secure_required| check. |
mallinath@webrtc.org | a27be8e | 2013-09-27 23:04:10 +0000 | [diff] [blame] | 118 | static bool VerifyCrypto(const SessionDescription* desc, |
| 119 | bool dtls_enabled, |
| 120 | std::string* error) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 121 | const ContentInfos& contents = desc->contents(); |
| 122 | for (size_t index = 0; index < contents.size(); ++index) { |
| 123 | const ContentInfo* cinfo = &contents[index]; |
| 124 | if (cinfo->rejected) { |
| 125 | continue; |
| 126 | } |
| 127 | |
| 128 | // If the content isn't rejected, crypto must be present. |
| 129 | const MediaContentDescription* media = |
| 130 | static_cast<const MediaContentDescription*>(cinfo->description); |
| 131 | const TransportInfo* tinfo = desc->GetTransportInfoByName(cinfo->name); |
| 132 | if (!media || !tinfo) { |
| 133 | // Something is not right. |
| 134 | LOG(LS_ERROR) << kInvalidSdp; |
mallinath@webrtc.org | a27be8e | 2013-09-27 23:04:10 +0000 | [diff] [blame] | 135 | *error = kInvalidSdp; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 136 | return false; |
| 137 | } |
mallinath@webrtc.org | a27be8e | 2013-09-27 23:04:10 +0000 | [diff] [blame] | 138 | if (media->cryptos().empty()) { |
| 139 | if (!tinfo->description.identity_fingerprint) { |
| 140 | // Crypto must be supplied. |
| 141 | LOG(LS_WARNING) << "Session description must have SDES or DTLS-SRTP."; |
| 142 | *error = kSdpWithoutCrypto; |
| 143 | return false; |
| 144 | } |
| 145 | if (!dtls_enabled) { |
| 146 | LOG(LS_WARNING) << |
| 147 | "Session description must have SDES when DTLS disabled."; |
| 148 | *error = kSdpWithoutSdesAndDtlsDisabled; |
| 149 | return false; |
| 150 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 151 | } |
| 152 | } |
| 153 | |
| 154 | return true; |
| 155 | } |
| 156 | |
mallinath@webrtc.org | 19f27e6 | 2013-10-13 17:18:27 +0000 | [diff] [blame] | 157 | // Checks that each non-rejected content has ice-ufrag and ice-pwd set. |
| 158 | static bool VerifyIceUfragPwdPresent(const SessionDescription* desc) { |
| 159 | const ContentInfos& contents = desc->contents(); |
| 160 | for (size_t index = 0; index < contents.size(); ++index) { |
| 161 | const ContentInfo* cinfo = &contents[index]; |
| 162 | if (cinfo->rejected) { |
| 163 | continue; |
| 164 | } |
| 165 | |
| 166 | // If the content isn't rejected, ice-ufrag and ice-pwd must be present. |
| 167 | const TransportInfo* tinfo = desc->GetTransportInfoByName(cinfo->name); |
| 168 | if (!tinfo) { |
| 169 | // Something is not right. |
| 170 | LOG(LS_ERROR) << kInvalidSdp; |
| 171 | return false; |
| 172 | } |
| 173 | if (tinfo->description.ice_ufrag.empty() || |
| 174 | tinfo->description.ice_pwd.empty()) { |
| 175 | LOG(LS_ERROR) << "Session description must have ice ufrag and pwd."; |
| 176 | return false; |
| 177 | } |
| 178 | } |
| 179 | return true; |
| 180 | } |
| 181 | |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 182 | // Forces |sdesc->crypto_required| to the appropriate state based on the |
| 183 | // current security policy, to ensure a failure occurs if there is an error |
| 184 | // in crypto negotiation. |
| 185 | // Called when processing the local session description. |
| 186 | static void UpdateSessionDescriptionSecurePolicy( |
| 187 | cricket::SecureMediaPolicy secure_policy, |
| 188 | SessionDescription* sdesc) { |
| 189 | if (!sdesc) { |
| 190 | return; |
| 191 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 192 | |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 193 | // Updating the |crypto_required_| in MediaContentDescription to the |
| 194 | // appropriate state based on the current security policy. |
| 195 | for (cricket::ContentInfos::iterator iter = sdesc->contents().begin(); |
| 196 | iter != sdesc->contents().end(); ++iter) { |
| 197 | if (cricket::IsMediaContent(&*iter)) { |
| 198 | MediaContentDescription* mdesc = |
| 199 | static_cast<MediaContentDescription*> (iter->description); |
| 200 | if (mdesc) { |
| 201 | mdesc->set_crypto_required(secure_policy == cricket::SEC_REQUIRED); |
| 202 | } |
| 203 | } |
| 204 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | static bool GetAudioSsrcByTrackId( |
| 208 | const SessionDescription* session_description, |
| 209 | const std::string& track_id, uint32 *ssrc) { |
| 210 | const cricket::ContentInfo* audio_info = |
| 211 | cricket::GetFirstAudioContent(session_description); |
| 212 | if (!audio_info) { |
| 213 | LOG(LS_ERROR) << "Audio not used in this call"; |
| 214 | return false; |
| 215 | } |
| 216 | |
| 217 | const cricket::MediaContentDescription* audio_content = |
| 218 | static_cast<const cricket::MediaContentDescription*>( |
| 219 | audio_info->description); |
| 220 | cricket::StreamParams stream; |
| 221 | if (!cricket::GetStreamByIds(audio_content->streams(), "", track_id, |
| 222 | &stream)) { |
| 223 | return false; |
| 224 | } |
| 225 | *ssrc = stream.first_ssrc(); |
| 226 | return true; |
| 227 | } |
| 228 | |
| 229 | static bool GetTrackIdBySsrc(const SessionDescription* session_description, |
| 230 | uint32 ssrc, std::string* track_id) { |
| 231 | ASSERT(track_id != NULL); |
| 232 | |
| 233 | cricket::StreamParams stream_out; |
| 234 | const cricket::ContentInfo* audio_info = |
| 235 | cricket::GetFirstAudioContent(session_description); |
| 236 | if (!audio_info) { |
| 237 | return false; |
| 238 | } |
| 239 | const cricket::MediaContentDescription* audio_content = |
| 240 | static_cast<const cricket::MediaContentDescription*>( |
| 241 | audio_info->description); |
| 242 | |
| 243 | if (cricket::GetStreamBySsrc(audio_content->streams(), ssrc, &stream_out)) { |
| 244 | *track_id = stream_out.id; |
| 245 | return true; |
| 246 | } |
| 247 | |
| 248 | const cricket::ContentInfo* video_info = |
| 249 | cricket::GetFirstVideoContent(session_description); |
| 250 | if (!video_info) { |
| 251 | return false; |
| 252 | } |
| 253 | const cricket::MediaContentDescription* video_content = |
| 254 | static_cast<const cricket::MediaContentDescription*>( |
| 255 | video_info->description); |
| 256 | |
| 257 | if (cricket::GetStreamBySsrc(video_content->streams(), ssrc, &stream_out)) { |
| 258 | *track_id = stream_out.id; |
| 259 | return true; |
| 260 | } |
| 261 | return false; |
| 262 | } |
| 263 | |
| 264 | static bool BadSdp(const std::string& desc, std::string* err_desc) { |
| 265 | if (err_desc) { |
| 266 | *err_desc = desc; |
| 267 | } |
| 268 | LOG(LS_ERROR) << desc; |
| 269 | return false; |
| 270 | } |
| 271 | |
| 272 | static bool BadLocalSdp(const std::string& desc, std::string* err_desc) { |
| 273 | std::string set_local_sdp_failed = kSetLocalSdpFailed; |
| 274 | set_local_sdp_failed.append(desc); |
| 275 | return BadSdp(set_local_sdp_failed, err_desc); |
| 276 | } |
| 277 | |
| 278 | static bool BadRemoteSdp(const std::string& desc, std::string* err_desc) { |
| 279 | std::string set_remote_sdp_failed = kSetRemoteSdpFailed; |
| 280 | set_remote_sdp_failed.append(desc); |
| 281 | return BadSdp(set_remote_sdp_failed, err_desc); |
| 282 | } |
| 283 | |
| 284 | static bool BadSdp(cricket::ContentSource source, |
| 285 | const std::string& desc, std::string* err_desc) { |
| 286 | if (source == cricket::CS_LOCAL) { |
| 287 | return BadLocalSdp(desc, err_desc); |
| 288 | } else { |
| 289 | return BadRemoteSdp(desc, err_desc); |
| 290 | } |
| 291 | } |
| 292 | |
| 293 | static std::string SessionErrorMsg(cricket::BaseSession::Error error) { |
| 294 | std::ostringstream desc; |
| 295 | desc << kSessionError << error; |
| 296 | return desc.str(); |
| 297 | } |
| 298 | |
| 299 | #define GET_STRING_OF_STATE(state) \ |
| 300 | case cricket::BaseSession::state: \ |
| 301 | result = #state; \ |
| 302 | break; |
| 303 | |
| 304 | static std::string GetStateString(cricket::BaseSession::State state) { |
| 305 | std::string result; |
| 306 | switch (state) { |
| 307 | GET_STRING_OF_STATE(STATE_INIT) |
| 308 | GET_STRING_OF_STATE(STATE_SENTINITIATE) |
| 309 | GET_STRING_OF_STATE(STATE_RECEIVEDINITIATE) |
| 310 | GET_STRING_OF_STATE(STATE_SENTPRACCEPT) |
| 311 | GET_STRING_OF_STATE(STATE_SENTACCEPT) |
| 312 | GET_STRING_OF_STATE(STATE_RECEIVEDPRACCEPT) |
| 313 | GET_STRING_OF_STATE(STATE_RECEIVEDACCEPT) |
| 314 | GET_STRING_OF_STATE(STATE_SENTMODIFY) |
| 315 | GET_STRING_OF_STATE(STATE_RECEIVEDMODIFY) |
| 316 | GET_STRING_OF_STATE(STATE_SENTREJECT) |
| 317 | GET_STRING_OF_STATE(STATE_RECEIVEDREJECT) |
| 318 | GET_STRING_OF_STATE(STATE_SENTREDIRECT) |
| 319 | GET_STRING_OF_STATE(STATE_SENTTERMINATE) |
| 320 | GET_STRING_OF_STATE(STATE_RECEIVEDTERMINATE) |
| 321 | GET_STRING_OF_STATE(STATE_INPROGRESS) |
| 322 | GET_STRING_OF_STATE(STATE_DEINIT) |
| 323 | default: |
| 324 | ASSERT(false); |
| 325 | break; |
| 326 | } |
| 327 | return result; |
| 328 | } |
| 329 | |
| 330 | #define GET_STRING_OF_ERROR(err) \ |
| 331 | case cricket::BaseSession::err: \ |
| 332 | result = #err; \ |
| 333 | break; |
| 334 | |
| 335 | static std::string GetErrorString(cricket::BaseSession::Error err) { |
| 336 | std::string result; |
| 337 | switch (err) { |
| 338 | GET_STRING_OF_ERROR(ERROR_NONE) |
| 339 | GET_STRING_OF_ERROR(ERROR_TIME) |
| 340 | GET_STRING_OF_ERROR(ERROR_RESPONSE) |
| 341 | GET_STRING_OF_ERROR(ERROR_NETWORK) |
| 342 | GET_STRING_OF_ERROR(ERROR_CONTENT) |
| 343 | GET_STRING_OF_ERROR(ERROR_TRANSPORT) |
| 344 | default: |
| 345 | ASSERT(false); |
| 346 | break; |
| 347 | } |
| 348 | return result; |
| 349 | } |
| 350 | |
| 351 | static bool SetSessionStateFailed(cricket::ContentSource source, |
| 352 | cricket::BaseSession::Error err, |
| 353 | std::string* err_desc) { |
| 354 | std::string set_state_err = kUpdateStateFailed; |
| 355 | set_state_err.append(GetErrorString(err)); |
| 356 | return BadSdp(source, set_state_err, err_desc); |
| 357 | } |
| 358 | |
| 359 | // Help class used to remember if a a remote peer has requested ice restart by |
| 360 | // by sending a description with new ice ufrag and password. |
| 361 | class IceRestartAnswerLatch { |
| 362 | public: |
| 363 | IceRestartAnswerLatch() : ice_restart_(false) { } |
| 364 | |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 365 | // Returns true if CheckForRemoteIceRestart has been called with a new session |
| 366 | // description where ice password and ufrag has changed since last time |
| 367 | // Reset() was called. |
| 368 | bool Get() const { |
| 369 | return ice_restart_; |
| 370 | } |
| 371 | |
| 372 | void Reset() { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 373 | if (ice_restart_) { |
| 374 | ice_restart_ = false; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 375 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 376 | } |
| 377 | |
| 378 | void CheckForRemoteIceRestart( |
| 379 | const SessionDescriptionInterface* old_desc, |
| 380 | const SessionDescriptionInterface* new_desc) { |
| 381 | if (!old_desc || new_desc->type() != SessionDescriptionInterface::kOffer) { |
| 382 | return; |
| 383 | } |
| 384 | const SessionDescription* new_sd = new_desc->description(); |
| 385 | const SessionDescription* old_sd = old_desc->description(); |
| 386 | const ContentInfos& contents = new_sd->contents(); |
| 387 | for (size_t index = 0; index < contents.size(); ++index) { |
| 388 | const ContentInfo* cinfo = &contents[index]; |
| 389 | if (cinfo->rejected) { |
| 390 | continue; |
| 391 | } |
| 392 | // If the content isn't rejected, check if ufrag and password has |
| 393 | // changed. |
| 394 | const cricket::TransportDescription* new_transport_desc = |
| 395 | new_sd->GetTransportDescriptionByName(cinfo->name); |
| 396 | const cricket::TransportDescription* old_transport_desc = |
| 397 | old_sd->GetTransportDescriptionByName(cinfo->name); |
| 398 | if (!new_transport_desc || !old_transport_desc) { |
| 399 | // No transport description exist. This is not an ice restart. |
| 400 | continue; |
| 401 | } |
| 402 | if (new_transport_desc->ice_pwd != old_transport_desc->ice_pwd && |
| 403 | new_transport_desc->ice_ufrag != old_transport_desc->ice_ufrag) { |
| 404 | LOG(LS_INFO) << "Remote peer request ice restart."; |
| 405 | ice_restart_ = true; |
| 406 | break; |
| 407 | } |
| 408 | } |
| 409 | } |
| 410 | |
| 411 | private: |
| 412 | bool ice_restart_; |
| 413 | }; |
| 414 | |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 415 | WebRtcSession::WebRtcSession( |
| 416 | cricket::ChannelManager* channel_manager, |
| 417 | talk_base::Thread* signaling_thread, |
| 418 | talk_base::Thread* worker_thread, |
| 419 | cricket::PortAllocator* port_allocator, |
| 420 | MediaStreamSignaling* mediastream_signaling) |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 421 | : cricket::BaseSession(signaling_thread, worker_thread, port_allocator, |
| 422 | talk_base::ToString(talk_base::CreateRandomId64() & |
| 423 | LLONG_MAX), |
| 424 | cricket::NS_JINGLE_RTP, false), |
| 425 | // RFC 3264: The numeric value of the session id and version in the |
| 426 | // o line MUST be representable with a "64 bit signed integer". |
| 427 | // Due to this constraint session id |sid_| is max limited to LLONG_MAX. |
| 428 | channel_manager_(channel_manager), |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 429 | mediastream_signaling_(mediastream_signaling), |
| 430 | ice_observer_(NULL), |
| 431 | ice_connection_state_(PeerConnectionInterface::kIceConnectionNew), |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 432 | older_version_remote_peer_(false), |
mallinath@webrtc.org | a27be8e | 2013-09-27 23:04:10 +0000 | [diff] [blame] | 433 | dtls_enabled_(false), |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 434 | data_channel_type_(cricket::DCT_NONE), |
| 435 | ice_restart_latch_(new IceRestartAnswerLatch) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 436 | } |
| 437 | |
| 438 | WebRtcSession::~WebRtcSession() { |
| 439 | if (voice_channel_.get()) { |
| 440 | SignalVoiceChannelDestroyed(); |
| 441 | channel_manager_->DestroyVoiceChannel(voice_channel_.release()); |
| 442 | } |
| 443 | if (video_channel_.get()) { |
| 444 | SignalVideoChannelDestroyed(); |
| 445 | channel_manager_->DestroyVideoChannel(video_channel_.release()); |
| 446 | } |
| 447 | if (data_channel_.get()) { |
| 448 | SignalDataChannelDestroyed(); |
| 449 | channel_manager_->DestroyDataChannel(data_channel_.release()); |
| 450 | } |
| 451 | for (size_t i = 0; i < saved_candidates_.size(); ++i) { |
| 452 | delete saved_candidates_[i]; |
| 453 | } |
| 454 | delete identity(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 455 | } |
| 456 | |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 457 | bool WebRtcSession::Initialize( |
wu@webrtc.org | 97077a3 | 2013-10-25 21:18:33 +0000 | [diff] [blame^] | 458 | const PeerConnectionFactoryInterface::Options& options, |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 459 | const MediaConstraintsInterface* constraints, |
| 460 | DTLSIdentityServiceInterface* dtls_identity_service) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 461 | // TODO(perkj): Take |constraints| into consideration. Return false if not all |
| 462 | // mandatory constraints can be fulfilled. Note that |constraints| |
| 463 | // can be null. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 464 | bool value; |
sergeyu@chromium.org | a59696b | 2013-09-13 23:48:58 +0000 | [diff] [blame] | 465 | |
| 466 | // Enable DTLS by default if |dtls_identity_service| is valid. |
mallinath@webrtc.org | a27be8e | 2013-09-27 23:04:10 +0000 | [diff] [blame] | 467 | dtls_enabled_ = (dtls_identity_service != NULL); |
| 468 | // |constraints| can override the default |dtls_enabled_| value. |
sergeyu@chromium.org | a59696b | 2013-09-13 23:48:58 +0000 | [diff] [blame] | 469 | if (FindConstraint( |
| 470 | constraints, |
| 471 | MediaConstraintsInterface::kEnableDtlsSrtp, |
| 472 | &value, NULL)) { |
mallinath@webrtc.org | a27be8e | 2013-09-27 23:04:10 +0000 | [diff] [blame] | 473 | dtls_enabled_ = value; |
sergeyu@chromium.org | a59696b | 2013-09-13 23:48:58 +0000 | [diff] [blame] | 474 | } |
| 475 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 476 | // Enable creation of RTP data channels if the kEnableRtpDataChannels is set. |
wu@webrtc.org | 97077a3 | 2013-10-25 21:18:33 +0000 | [diff] [blame^] | 477 | // It takes precendence over the disable_sctp_data_channels |
| 478 | // PeerConnectionFactoryInterface::Options. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 479 | if (FindConstraint( |
| 480 | constraints, MediaConstraintsInterface::kEnableRtpDataChannels, |
| 481 | &value, NULL) && value) { |
| 482 | LOG(LS_INFO) << "Allowing RTP data engine."; |
| 483 | data_channel_type_ = cricket::DCT_RTP; |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 484 | } else { |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 485 | // DTLS has to be enabled to use SCTP. |
wu@webrtc.org | 97077a3 | 2013-10-25 21:18:33 +0000 | [diff] [blame^] | 486 | if (!options.disable_sctp_data_channels && dtls_enabled_) { |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 487 | LOG(LS_INFO) << "Allowing SCTP data engine."; |
| 488 | data_channel_type_ = cricket::DCT_SCTP; |
| 489 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 490 | } |
| 491 | if (data_channel_type_ != cricket::DCT_NONE) { |
| 492 | mediastream_signaling_->SetDataChannelFactory(this); |
| 493 | } |
| 494 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 495 | const cricket::VideoCodec default_codec( |
| 496 | JsepSessionDescription::kDefaultVideoCodecId, |
| 497 | JsepSessionDescription::kDefaultVideoCodecName, |
| 498 | JsepSessionDescription::kMaxVideoCodecWidth, |
| 499 | JsepSessionDescription::kMaxVideoCodecHeight, |
| 500 | JsepSessionDescription::kDefaultVideoCodecFramerate, |
| 501 | JsepSessionDescription::kDefaultVideoCodecPreference); |
| 502 | channel_manager_->SetDefaultVideoEncoderConfig( |
| 503 | cricket::VideoEncoderConfig(default_codec)); |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 504 | |
| 505 | webrtc_session_desc_factory_.reset(new WebRtcSessionDescriptionFactory( |
| 506 | signaling_thread(), |
| 507 | channel_manager_, |
| 508 | mediastream_signaling_, |
| 509 | dtls_identity_service, |
| 510 | this, |
| 511 | id(), |
| 512 | data_channel_type_, |
mallinath@webrtc.org | a27be8e | 2013-09-27 23:04:10 +0000 | [diff] [blame] | 513 | dtls_enabled_)); |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 514 | |
| 515 | webrtc_session_desc_factory_->SignalIdentityReady.connect( |
| 516 | this, &WebRtcSession::OnIdentityReady); |
mallinath@webrtc.org | 7e809c3 | 2013-09-30 18:59:08 +0000 | [diff] [blame] | 517 | |
wu@webrtc.org | 97077a3 | 2013-10-25 21:18:33 +0000 | [diff] [blame^] | 518 | if (options.disable_encryption) { |
mallinath@webrtc.org | 7e809c3 | 2013-09-30 18:59:08 +0000 | [diff] [blame] | 519 | webrtc_session_desc_factory_->set_secure(cricket::SEC_DISABLED); |
| 520 | } |
| 521 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 522 | return true; |
| 523 | } |
| 524 | |
| 525 | void WebRtcSession::Terminate() { |
| 526 | SetState(STATE_RECEIVEDTERMINATE); |
| 527 | RemoveUnusedChannelsAndTransports(NULL); |
| 528 | ASSERT(voice_channel_.get() == NULL); |
| 529 | ASSERT(video_channel_.get() == NULL); |
| 530 | ASSERT(data_channel_.get() == NULL); |
| 531 | } |
| 532 | |
| 533 | bool WebRtcSession::StartCandidatesAllocation() { |
| 534 | // SpeculativelyConnectTransportChannels, will call ConnectChannels method |
| 535 | // from TransportProxy to start gathering ice candidates. |
| 536 | SpeculativelyConnectAllTransportChannels(); |
| 537 | if (!saved_candidates_.empty()) { |
| 538 | // If there are saved candidates which arrived before local description is |
| 539 | // set, copy those to remote description. |
| 540 | CopySavedCandidates(remote_desc_.get()); |
| 541 | } |
| 542 | // Push remote candidates present in remote description to transport channels. |
| 543 | UseCandidatesInSessionDescription(remote_desc_.get()); |
| 544 | return true; |
| 545 | } |
| 546 | |
| 547 | void WebRtcSession::set_secure_policy( |
| 548 | cricket::SecureMediaPolicy secure_policy) { |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 549 | webrtc_session_desc_factory_->set_secure(secure_policy); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 550 | } |
| 551 | |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 552 | cricket::SecureMediaPolicy WebRtcSession::secure_policy() const { |
| 553 | return webrtc_session_desc_factory_->secure(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 554 | } |
| 555 | |
sergeyu@chromium.org | 0be6aa0 | 2013-08-23 23:21:25 +0000 | [diff] [blame] | 556 | bool WebRtcSession::GetSslRole(talk_base::SSLRole* role) { |
| 557 | if (local_description() == NULL || remote_description() == NULL) { |
| 558 | LOG(LS_INFO) << "Local and Remote descriptions must be applied to get " |
| 559 | << "SSL Role of the session."; |
| 560 | return false; |
| 561 | } |
| 562 | |
| 563 | // TODO(mallinath) - Return role of each transport, as role may differ from |
| 564 | // one another. |
| 565 | // In current implementaion we just return the role of first transport in the |
| 566 | // transport map. |
| 567 | for (cricket::TransportMap::const_iterator iter = transport_proxies().begin(); |
| 568 | iter != transport_proxies().end(); ++iter) { |
| 569 | if (iter->second->impl()) { |
| 570 | return iter->second->impl()->GetSslRole(role); |
| 571 | } |
| 572 | } |
| 573 | return false; |
| 574 | } |
| 575 | |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 576 | void WebRtcSession::CreateOffer(CreateSessionDescriptionObserver* observer, |
| 577 | const MediaConstraintsInterface* constraints) { |
| 578 | webrtc_session_desc_factory_->CreateOffer(observer, constraints); |
| 579 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 580 | |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 581 | void WebRtcSession::CreateAnswer(CreateSessionDescriptionObserver* observer, |
| 582 | const MediaConstraintsInterface* constraints) { |
| 583 | webrtc_session_desc_factory_->CreateAnswer(observer, constraints); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 584 | } |
| 585 | |
| 586 | bool WebRtcSession::SetLocalDescription(SessionDescriptionInterface* desc, |
| 587 | std::string* err_desc) { |
henrike@webrtc.org | 28654cb | 2013-07-22 21:07:49 +0000 | [diff] [blame] | 588 | // Takes the ownership of |desc| regardless of the result. |
| 589 | talk_base::scoped_ptr<SessionDescriptionInterface> desc_temp(desc); |
| 590 | |
sergeyu@chromium.org | 0be6aa0 | 2013-08-23 23:21:25 +0000 | [diff] [blame] | 591 | // Validate SDP. |
| 592 | if (!ValidateSessionDescription(desc, cricket::CS_LOCAL, err_desc)) { |
| 593 | return false; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 594 | } |
| 595 | |
| 596 | // Update the initiator flag if this session is the initiator. |
sergeyu@chromium.org | 0be6aa0 | 2013-08-23 23:21:25 +0000 | [diff] [blame] | 597 | Action action = GetAction(desc->type()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 598 | if (state() == STATE_INIT && action == kOffer) { |
| 599 | set_initiator(true); |
| 600 | } |
| 601 | |
sergeyu@chromium.org | 0be6aa0 | 2013-08-23 23:21:25 +0000 | [diff] [blame] | 602 | cricket::SecureMediaPolicy secure_policy = |
| 603 | webrtc_session_desc_factory_->secure(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 604 | // Update the MediaContentDescription crypto settings as per the policy set. |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 605 | UpdateSessionDescriptionSecurePolicy(secure_policy, desc->description()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 606 | |
| 607 | set_local_description(desc->description()->Copy()); |
henrike@webrtc.org | 28654cb | 2013-07-22 21:07:49 +0000 | [diff] [blame] | 608 | local_desc_.reset(desc_temp.release()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 609 | |
| 610 | // 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] | 611 | if (action == kOffer && !CreateChannels(local_desc_->description())) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 612 | // TODO(mallinath) - Handle CreateChannel failure, as new local description |
| 613 | // is applied. Restore back to old description. |
| 614 | return BadLocalSdp(kCreateChannelFailed, err_desc); |
| 615 | } |
| 616 | |
| 617 | // Remove channel and transport proxies, if MediaContentDescription is |
| 618 | // rejected. |
henrike@webrtc.org | 28654cb | 2013-07-22 21:07:49 +0000 | [diff] [blame] | 619 | RemoveUnusedChannelsAndTransports(local_desc_->description()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 620 | |
| 621 | if (!UpdateSessionState(action, cricket::CS_LOCAL, |
henrike@webrtc.org | 28654cb | 2013-07-22 21:07:49 +0000 | [diff] [blame] | 622 | local_desc_->description(), err_desc)) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 623 | return false; |
| 624 | } |
| 625 | // Kick starting the ice candidates allocation. |
| 626 | StartCandidatesAllocation(); |
| 627 | |
| 628 | // Update state and SSRC of local MediaStreams and DataChannels based on the |
| 629 | // local session description. |
| 630 | mediastream_signaling_->OnLocalDescriptionChanged(local_desc_.get()); |
| 631 | |
| 632 | if (error() != cricket::BaseSession::ERROR_NONE) { |
| 633 | return BadLocalSdp(SessionErrorMsg(error()), err_desc); |
| 634 | } |
| 635 | return true; |
| 636 | } |
| 637 | |
| 638 | bool WebRtcSession::SetRemoteDescription(SessionDescriptionInterface* desc, |
| 639 | std::string* err_desc) { |
henrike@webrtc.org | 28654cb | 2013-07-22 21:07:49 +0000 | [diff] [blame] | 640 | // Takes the ownership of |desc| regardless of the result. |
| 641 | talk_base::scoped_ptr<SessionDescriptionInterface> desc_temp(desc); |
| 642 | |
sergeyu@chromium.org | 0be6aa0 | 2013-08-23 23:21:25 +0000 | [diff] [blame] | 643 | // Validate SDP. |
| 644 | if (!ValidateSessionDescription(desc, cricket::CS_REMOTE, err_desc)) { |
| 645 | return false; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 646 | } |
| 647 | |
| 648 | // 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] | 649 | Action action = GetAction(desc->type()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 650 | if (action == kOffer && !CreateChannels(desc->description())) { |
| 651 | // TODO(mallinath) - Handle CreateChannel failure, as new local description |
| 652 | // is applied. Restore back to old description. |
| 653 | return BadRemoteSdp(kCreateChannelFailed, err_desc); |
| 654 | } |
| 655 | |
| 656 | // Remove channel and transport proxies, if MediaContentDescription is |
| 657 | // rejected. |
| 658 | RemoveUnusedChannelsAndTransports(desc->description()); |
| 659 | |
| 660 | // NOTE: Candidates allocation will be initiated only when SetLocalDescription |
| 661 | // is called. |
| 662 | set_remote_description(desc->description()->Copy()); |
| 663 | if (!UpdateSessionState(action, cricket::CS_REMOTE, |
| 664 | desc->description(), err_desc)) { |
| 665 | return false; |
| 666 | } |
| 667 | |
| 668 | // Update remote MediaStreams. |
| 669 | mediastream_signaling_->OnRemoteDescriptionChanged(desc); |
| 670 | if (local_description() && !UseCandidatesInSessionDescription(desc)) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 671 | return BadRemoteSdp(kInvalidCandidates, err_desc); |
| 672 | } |
| 673 | |
| 674 | // Copy all saved candidates. |
| 675 | CopySavedCandidates(desc); |
| 676 | // We retain all received candidates. |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 677 | WebRtcSessionDescriptionFactory::CopyCandidatesFromSessionDescription( |
| 678 | remote_desc_.get(), desc); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 679 | // Check if this new SessionDescription contains new ice ufrag and password |
| 680 | // that indicates the remote peer requests ice restart. |
| 681 | ice_restart_latch_->CheckForRemoteIceRestart(remote_desc_.get(), |
| 682 | desc); |
henrike@webrtc.org | 28654cb | 2013-07-22 21:07:49 +0000 | [diff] [blame] | 683 | remote_desc_.reset(desc_temp.release()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 684 | if (error() != cricket::BaseSession::ERROR_NONE) { |
| 685 | return BadRemoteSdp(SessionErrorMsg(error()), err_desc); |
| 686 | } |
| 687 | return true; |
| 688 | } |
| 689 | |
| 690 | bool WebRtcSession::UpdateSessionState( |
| 691 | Action action, cricket::ContentSource source, |
| 692 | const cricket::SessionDescription* desc, |
| 693 | std::string* err_desc) { |
| 694 | // If there's already a pending error then no state transition should happen. |
| 695 | // But all call-sites should be verifying this before calling us! |
| 696 | ASSERT(error() == cricket::BaseSession::ERROR_NONE); |
| 697 | if (action == kOffer) { |
| 698 | if (!PushdownTransportDescription(source, cricket::CA_OFFER)) { |
| 699 | return BadSdp(source, kPushDownOfferTDFailed, err_desc); |
| 700 | } |
| 701 | SetState(source == cricket::CS_LOCAL ? |
| 702 | STATE_SENTINITIATE : STATE_RECEIVEDINITIATE); |
| 703 | if (error() != cricket::BaseSession::ERROR_NONE) { |
| 704 | return SetSessionStateFailed(source, error(), err_desc); |
| 705 | } |
| 706 | } else if (action == kPrAnswer) { |
| 707 | if (!PushdownTransportDescription(source, cricket::CA_PRANSWER)) { |
| 708 | return BadSdp(source, kPushDownPranswerTDFailed, err_desc); |
| 709 | } |
| 710 | EnableChannels(); |
| 711 | SetState(source == cricket::CS_LOCAL ? |
| 712 | STATE_SENTPRACCEPT : STATE_RECEIVEDPRACCEPT); |
| 713 | if (error() != cricket::BaseSession::ERROR_NONE) { |
| 714 | return SetSessionStateFailed(source, error(), err_desc); |
| 715 | } |
| 716 | } else if (action == kAnswer) { |
| 717 | if (!PushdownTransportDescription(source, cricket::CA_ANSWER)) { |
| 718 | return BadSdp(source, kPushDownAnswerTDFailed, err_desc); |
| 719 | } |
| 720 | MaybeEnableMuxingSupport(); |
| 721 | EnableChannels(); |
| 722 | SetState(source == cricket::CS_LOCAL ? |
| 723 | STATE_SENTACCEPT : STATE_RECEIVEDACCEPT); |
| 724 | if (error() != cricket::BaseSession::ERROR_NONE) { |
| 725 | return SetSessionStateFailed(source, error(), err_desc); |
| 726 | } |
| 727 | } |
| 728 | return true; |
| 729 | } |
| 730 | |
| 731 | WebRtcSession::Action WebRtcSession::GetAction(const std::string& type) { |
| 732 | if (type == SessionDescriptionInterface::kOffer) { |
| 733 | return WebRtcSession::kOffer; |
| 734 | } else if (type == SessionDescriptionInterface::kPrAnswer) { |
| 735 | return WebRtcSession::kPrAnswer; |
| 736 | } else if (type == SessionDescriptionInterface::kAnswer) { |
| 737 | return WebRtcSession::kAnswer; |
| 738 | } |
| 739 | ASSERT(false && "unknown action type"); |
| 740 | return WebRtcSession::kOffer; |
| 741 | } |
| 742 | |
| 743 | bool WebRtcSession::ProcessIceMessage(const IceCandidateInterface* candidate) { |
| 744 | if (state() == STATE_INIT) { |
| 745 | LOG(LS_ERROR) << "ProcessIceMessage: ICE candidates can't be added " |
| 746 | << "without any offer (local or remote) " |
| 747 | << "session description."; |
| 748 | return false; |
| 749 | } |
| 750 | |
| 751 | if (!candidate) { |
| 752 | LOG(LS_ERROR) << "ProcessIceMessage: Candidate is NULL"; |
| 753 | return false; |
| 754 | } |
| 755 | |
| 756 | if (!local_description() || !remote_description()) { |
| 757 | LOG(LS_INFO) << "ProcessIceMessage: Remote description not set, " |
| 758 | << "save the candidate for later use."; |
| 759 | saved_candidates_.push_back( |
| 760 | new JsepIceCandidate(candidate->sdp_mid(), candidate->sdp_mline_index(), |
| 761 | candidate->candidate())); |
| 762 | return true; |
| 763 | } |
| 764 | |
| 765 | // Add this candidate to the remote session description. |
| 766 | if (!remote_desc_->AddCandidate(candidate)) { |
| 767 | LOG(LS_ERROR) << "ProcessIceMessage: Candidate cannot be used"; |
| 768 | return false; |
| 769 | } |
| 770 | |
| 771 | return UseCandidatesInSessionDescription(remote_desc_.get()); |
| 772 | } |
| 773 | |
| 774 | bool WebRtcSession::GetTrackIdBySsrc(uint32 ssrc, std::string* id) { |
| 775 | if (GetLocalTrackId(ssrc, id)) { |
| 776 | if (GetRemoteTrackId(ssrc, id)) { |
| 777 | LOG(LS_WARNING) << "SSRC " << ssrc |
| 778 | << " exists in both local and remote descriptions"; |
| 779 | return true; // We return the remote track id. |
| 780 | } |
| 781 | return true; |
| 782 | } else { |
| 783 | return GetRemoteTrackId(ssrc, id); |
| 784 | } |
| 785 | } |
| 786 | |
| 787 | bool WebRtcSession::GetLocalTrackId(uint32 ssrc, std::string* track_id) { |
| 788 | if (!BaseSession::local_description()) |
| 789 | return false; |
| 790 | return webrtc::GetTrackIdBySsrc( |
| 791 | BaseSession::local_description(), ssrc, track_id); |
| 792 | } |
| 793 | |
| 794 | bool WebRtcSession::GetRemoteTrackId(uint32 ssrc, std::string* track_id) { |
| 795 | if (!BaseSession::remote_description()) |
| 796 | return false; |
| 797 | return webrtc::GetTrackIdBySsrc( |
| 798 | BaseSession::remote_description(), ssrc, track_id); |
| 799 | } |
| 800 | |
| 801 | std::string WebRtcSession::BadStateErrMsg( |
| 802 | const std::string& type, State state) { |
| 803 | std::ostringstream desc; |
| 804 | desc << "Called with type in wrong state, " |
| 805 | << "type: " << type << " state: " << GetStateString(state); |
| 806 | return desc.str(); |
| 807 | } |
| 808 | |
henrike@webrtc.org | 1e09a71 | 2013-07-26 19:17:59 +0000 | [diff] [blame] | 809 | void WebRtcSession::SetAudioPlayout(uint32 ssrc, bool enable, |
| 810 | cricket::AudioRenderer* renderer) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 811 | ASSERT(signaling_thread()->IsCurrent()); |
| 812 | if (!voice_channel_) { |
| 813 | LOG(LS_ERROR) << "SetAudioPlayout: No audio channel exists."; |
| 814 | return; |
| 815 | } |
henrike@webrtc.org | 1e09a71 | 2013-07-26 19:17:59 +0000 | [diff] [blame] | 816 | if (!voice_channel_->SetRemoteRenderer(ssrc, renderer)) { |
| 817 | // SetRenderer() can fail if the ssrc does not match any playout channel. |
| 818 | LOG(LS_ERROR) << "SetAudioPlayout: ssrc is incorrect: " << ssrc; |
| 819 | return; |
| 820 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 821 | if (!voice_channel_->SetOutputScaling(ssrc, enable ? 1 : 0, enable ? 1 : 0)) { |
| 822 | // Allow that SetOutputScaling fail if |enable| is false but assert |
| 823 | // otherwise. This in the normal case when the underlying media channel has |
| 824 | // already been deleted. |
| 825 | ASSERT(enable == false); |
| 826 | } |
| 827 | } |
| 828 | |
| 829 | void WebRtcSession::SetAudioSend(uint32 ssrc, bool enable, |
henrike@webrtc.org | 1e09a71 | 2013-07-26 19:17:59 +0000 | [diff] [blame] | 830 | const cricket::AudioOptions& options, |
| 831 | cricket::AudioRenderer* renderer) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 832 | ASSERT(signaling_thread()->IsCurrent()); |
| 833 | if (!voice_channel_) { |
| 834 | LOG(LS_ERROR) << "SetAudioSend: No audio channel exists."; |
| 835 | return; |
| 836 | } |
henrike@webrtc.org | 1e09a71 | 2013-07-26 19:17:59 +0000 | [diff] [blame] | 837 | if (!voice_channel_->SetLocalRenderer(ssrc, renderer)) { |
| 838 | // SetRenderer() can fail if the ssrc does not match any send channel. |
| 839 | LOG(LS_ERROR) << "SetAudioSend: ssrc is incorrect: " << ssrc; |
| 840 | return; |
| 841 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 842 | if (!voice_channel_->MuteStream(ssrc, !enable)) { |
| 843 | // Allow that MuteStream fail if |enable| is false but assert otherwise. |
| 844 | // This in the normal case when the underlying media channel has already |
| 845 | // been deleted. |
| 846 | ASSERT(enable == false); |
| 847 | return; |
| 848 | } |
| 849 | if (enable) |
| 850 | voice_channel_->SetChannelOptions(options); |
| 851 | } |
| 852 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 853 | bool WebRtcSession::SetCaptureDevice(uint32 ssrc, |
| 854 | cricket::VideoCapturer* camera) { |
| 855 | ASSERT(signaling_thread()->IsCurrent()); |
| 856 | |
| 857 | if (!video_channel_.get()) { |
| 858 | // |video_channel_| doesnt't exist. Probably because the remote end doesnt't |
| 859 | // support video. |
| 860 | LOG(LS_WARNING) << "Video not used in this call."; |
| 861 | return false; |
| 862 | } |
| 863 | if (!video_channel_->SetCapturer(ssrc, camera)) { |
| 864 | // Allow that SetCapturer fail if |camera| is NULL but assert otherwise. |
| 865 | // This in the normal case when the underlying media channel has already |
| 866 | // been deleted. |
| 867 | ASSERT(camera == NULL); |
| 868 | return false; |
| 869 | } |
| 870 | return true; |
| 871 | } |
| 872 | |
| 873 | void WebRtcSession::SetVideoPlayout(uint32 ssrc, |
| 874 | bool enable, |
| 875 | cricket::VideoRenderer* renderer) { |
| 876 | ASSERT(signaling_thread()->IsCurrent()); |
| 877 | if (!video_channel_) { |
| 878 | LOG(LS_WARNING) << "SetVideoPlayout: No video channel exists."; |
| 879 | return; |
| 880 | } |
| 881 | if (!video_channel_->SetRenderer(ssrc, enable ? renderer : NULL)) { |
| 882 | // Allow that SetRenderer fail if |renderer| is NULL but assert otherwise. |
| 883 | // This in the normal case when the underlying media channel has already |
| 884 | // been deleted. |
| 885 | ASSERT(renderer == NULL); |
| 886 | } |
| 887 | } |
| 888 | |
| 889 | void WebRtcSession::SetVideoSend(uint32 ssrc, bool enable, |
| 890 | const cricket::VideoOptions* options) { |
| 891 | ASSERT(signaling_thread()->IsCurrent()); |
| 892 | if (!video_channel_) { |
| 893 | LOG(LS_WARNING) << "SetVideoSend: No video channel exists."; |
| 894 | return; |
| 895 | } |
| 896 | if (!video_channel_->MuteStream(ssrc, !enable)) { |
| 897 | // Allow that MuteStream fail if |enable| is false but assert otherwise. |
| 898 | // This in the normal case when the underlying media channel has already |
| 899 | // been deleted. |
| 900 | ASSERT(enable == false); |
| 901 | return; |
| 902 | } |
| 903 | if (enable && options) |
| 904 | video_channel_->SetChannelOptions(*options); |
| 905 | } |
| 906 | |
| 907 | bool WebRtcSession::CanInsertDtmf(const std::string& track_id) { |
| 908 | ASSERT(signaling_thread()->IsCurrent()); |
| 909 | if (!voice_channel_) { |
| 910 | LOG(LS_ERROR) << "CanInsertDtmf: No audio channel exists."; |
| 911 | return false; |
| 912 | } |
| 913 | uint32 send_ssrc = 0; |
| 914 | // The Dtmf is negotiated per channel not ssrc, so we only check if the ssrc |
| 915 | // exists. |
| 916 | if (!GetAudioSsrcByTrackId(BaseSession::local_description(), track_id, |
| 917 | &send_ssrc)) { |
| 918 | LOG(LS_ERROR) << "CanInsertDtmf: Track does not exist: " << track_id; |
| 919 | return false; |
| 920 | } |
| 921 | return voice_channel_->CanInsertDtmf(); |
| 922 | } |
| 923 | |
| 924 | bool WebRtcSession::InsertDtmf(const std::string& track_id, |
| 925 | int code, int duration) { |
| 926 | ASSERT(signaling_thread()->IsCurrent()); |
| 927 | if (!voice_channel_) { |
| 928 | LOG(LS_ERROR) << "InsertDtmf: No audio channel exists."; |
| 929 | return false; |
| 930 | } |
| 931 | uint32 send_ssrc = 0; |
| 932 | if (!VERIFY(GetAudioSsrcByTrackId(BaseSession::local_description(), |
| 933 | track_id, &send_ssrc))) { |
| 934 | LOG(LS_ERROR) << "InsertDtmf: Track does not exist: " << track_id; |
| 935 | return false; |
| 936 | } |
| 937 | if (!voice_channel_->InsertDtmf(send_ssrc, code, duration, |
| 938 | cricket::DF_SEND)) { |
| 939 | LOG(LS_ERROR) << "Failed to insert DTMF to channel."; |
| 940 | return false; |
| 941 | } |
| 942 | return true; |
| 943 | } |
| 944 | |
| 945 | sigslot::signal0<>* WebRtcSession::GetOnDestroyedSignal() { |
| 946 | return &SignalVoiceChannelDestroyed; |
| 947 | } |
| 948 | |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 949 | bool WebRtcSession::SendData(const cricket::SendDataParams& params, |
| 950 | const talk_base::Buffer& payload, |
| 951 | cricket::SendDataResult* result) { |
| 952 | if (!data_channel_.get()) { |
| 953 | LOG(LS_ERROR) << "SendData called when data_channel_ is NULL."; |
| 954 | return false; |
| 955 | } |
| 956 | return data_channel_->SendData(params, payload, result); |
| 957 | } |
| 958 | |
| 959 | bool WebRtcSession::ConnectDataChannel(DataChannel* webrtc_data_channel) { |
| 960 | if (!data_channel_.get()) { |
| 961 | LOG(LS_ERROR) << "ConnectDataChannel called when data_channel_ is NULL."; |
| 962 | return false; |
| 963 | } |
| 964 | |
| 965 | data_channel_->SignalReadyToSendData.connect(webrtc_data_channel, |
| 966 | &DataChannel::OnChannelReady); |
| 967 | data_channel_->SignalDataReceived.connect(webrtc_data_channel, |
| 968 | &DataChannel::OnDataReceived); |
| 969 | cricket::StreamParams params = |
| 970 | cricket::StreamParams::CreateLegacy(webrtc_data_channel->id()); |
| 971 | data_channel_->AddRecvStream(params); |
| 972 | data_channel_->AddSendStream(params); |
| 973 | return true; |
| 974 | } |
| 975 | |
| 976 | void WebRtcSession::DisconnectDataChannel(DataChannel* webrtc_data_channel) { |
| 977 | data_channel_->RemoveSendStream(webrtc_data_channel->id()); |
| 978 | data_channel_->RemoveRecvStream(webrtc_data_channel->id()); |
| 979 | data_channel_->SignalReadyToSendData.disconnect(webrtc_data_channel); |
| 980 | data_channel_->SignalDataReceived.disconnect(webrtc_data_channel); |
| 981 | } |
| 982 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 983 | talk_base::scoped_refptr<DataChannel> WebRtcSession::CreateDataChannel( |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 984 | const std::string& label, |
| 985 | const DataChannelInit* config) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 986 | if (state() == STATE_RECEIVEDTERMINATE) { |
| 987 | return NULL; |
| 988 | } |
| 989 | if (data_channel_type_ == cricket::DCT_NONE) { |
| 990 | LOG(LS_ERROR) << "CreateDataChannel: Data is not supported in this call."; |
| 991 | return NULL; |
| 992 | } |
| 993 | DataChannelInit new_config = config ? (*config) : DataChannelInit(); |
| 994 | |
| 995 | if (data_channel_type_ == cricket::DCT_SCTP) { |
| 996 | if (new_config.id < 0) { |
| 997 | if (!mediastream_signaling_->AllocateSctpId(&new_config.id)) { |
| 998 | LOG(LS_ERROR) << "No id can be allocated for the SCTP data channel."; |
| 999 | return NULL; |
| 1000 | } |
| 1001 | } else if (!mediastream_signaling_->IsSctpIdAvailable(new_config.id)) { |
| 1002 | LOG(LS_ERROR) << "Failed to create a SCTP data channel " |
| 1003 | << "because the id is already in use or out of range."; |
| 1004 | return NULL; |
| 1005 | } |
| 1006 | } |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 1007 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1008 | talk_base::scoped_refptr<DataChannel> channel( |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 1009 | DataChannel::Create(this, data_channel_type_, label, &new_config)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1010 | if (channel == NULL) |
| 1011 | return NULL; |
| 1012 | if (!mediastream_signaling_->AddDataChannel(channel)) |
| 1013 | return NULL; |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 1014 | if (data_channel_type_ == cricket::DCT_SCTP) { |
| 1015 | if (config == NULL) { |
| 1016 | LOG(LS_WARNING) << "Could not send data channel OPEN message" |
| 1017 | << " because of NULL config."; |
| 1018 | return NULL; |
| 1019 | } |
| 1020 | if (data_channel_.get()) { |
| 1021 | channel->SetReceiveSsrc(new_config.id); |
| 1022 | channel->SetSendSsrc(new_config.id); |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 1023 | } |
| 1024 | if (!config->negotiated) { |
| 1025 | talk_base::Buffer *payload = new talk_base::Buffer; |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 +0000 | [diff] [blame] | 1026 | if (!cricket::WriteDataChannelOpenMessage(label, *config, payload)) { |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 1027 | LOG(LS_WARNING) << "Could not write data channel OPEN message"; |
| 1028 | } |
| 1029 | // SendControl may queue the message until the data channel's set up, |
| 1030 | // or congestion clears. |
| 1031 | channel->SendControl(payload); |
| 1032 | } |
| 1033 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1034 | return channel; |
| 1035 | } |
| 1036 | |
| 1037 | cricket::DataChannelType WebRtcSession::data_channel_type() const { |
| 1038 | return data_channel_type_; |
| 1039 | } |
| 1040 | |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 1041 | bool WebRtcSession::IceRestartPending() const { |
| 1042 | return ice_restart_latch_->Get(); |
| 1043 | } |
| 1044 | |
| 1045 | void WebRtcSession::ResetIceRestartLatch() { |
| 1046 | ice_restart_latch_->Reset(); |
| 1047 | } |
| 1048 | |
| 1049 | void WebRtcSession::OnIdentityReady(talk_base::SSLIdentity* identity) { |
| 1050 | SetIdentity(identity); |
| 1051 | } |
| 1052 | |
| 1053 | bool WebRtcSession::waiting_for_identity() const { |
| 1054 | return webrtc_session_desc_factory_->waiting_for_identity(); |
| 1055 | } |
| 1056 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1057 | void WebRtcSession::SetIceConnectionState( |
| 1058 | PeerConnectionInterface::IceConnectionState state) { |
| 1059 | if (ice_connection_state_ == state) { |
| 1060 | return; |
| 1061 | } |
| 1062 | |
| 1063 | // ASSERT that the requested transition is allowed. Note that |
| 1064 | // WebRtcSession does not implement "kIceConnectionClosed" (that is handled |
| 1065 | // within PeerConnection). This switch statement should compile away when |
| 1066 | // ASSERTs are disabled. |
| 1067 | switch (ice_connection_state_) { |
| 1068 | case PeerConnectionInterface::kIceConnectionNew: |
| 1069 | ASSERT(state == PeerConnectionInterface::kIceConnectionChecking); |
| 1070 | break; |
| 1071 | case PeerConnectionInterface::kIceConnectionChecking: |
| 1072 | ASSERT(state == PeerConnectionInterface::kIceConnectionFailed || |
| 1073 | state == PeerConnectionInterface::kIceConnectionConnected); |
| 1074 | break; |
| 1075 | case PeerConnectionInterface::kIceConnectionConnected: |
| 1076 | ASSERT(state == PeerConnectionInterface::kIceConnectionDisconnected || |
| 1077 | state == PeerConnectionInterface::kIceConnectionChecking || |
| 1078 | state == PeerConnectionInterface::kIceConnectionCompleted); |
| 1079 | break; |
| 1080 | case PeerConnectionInterface::kIceConnectionCompleted: |
| 1081 | ASSERT(state == PeerConnectionInterface::kIceConnectionConnected || |
| 1082 | state == PeerConnectionInterface::kIceConnectionDisconnected); |
| 1083 | break; |
| 1084 | case PeerConnectionInterface::kIceConnectionFailed: |
| 1085 | ASSERT(state == PeerConnectionInterface::kIceConnectionNew); |
| 1086 | break; |
| 1087 | case PeerConnectionInterface::kIceConnectionDisconnected: |
| 1088 | ASSERT(state == PeerConnectionInterface::kIceConnectionChecking || |
| 1089 | state == PeerConnectionInterface::kIceConnectionConnected || |
| 1090 | state == PeerConnectionInterface::kIceConnectionCompleted || |
| 1091 | state == PeerConnectionInterface::kIceConnectionFailed); |
| 1092 | break; |
| 1093 | case PeerConnectionInterface::kIceConnectionClosed: |
| 1094 | ASSERT(false); |
| 1095 | break; |
| 1096 | default: |
| 1097 | ASSERT(false); |
| 1098 | break; |
| 1099 | } |
| 1100 | |
| 1101 | ice_connection_state_ = state; |
| 1102 | if (ice_observer_) { |
| 1103 | ice_observer_->OnIceConnectionChange(ice_connection_state_); |
| 1104 | } |
| 1105 | } |
| 1106 | |
| 1107 | void WebRtcSession::OnTransportRequestSignaling( |
| 1108 | cricket::Transport* transport) { |
| 1109 | ASSERT(signaling_thread()->IsCurrent()); |
| 1110 | transport->OnSignalingReady(); |
| 1111 | if (ice_observer_) { |
| 1112 | ice_observer_->OnIceGatheringChange( |
| 1113 | PeerConnectionInterface::kIceGatheringGathering); |
| 1114 | } |
| 1115 | } |
| 1116 | |
| 1117 | void WebRtcSession::OnTransportConnecting(cricket::Transport* transport) { |
| 1118 | ASSERT(signaling_thread()->IsCurrent()); |
| 1119 | // start monitoring for the write state of the transport. |
| 1120 | OnTransportWritable(transport); |
| 1121 | } |
| 1122 | |
| 1123 | void WebRtcSession::OnTransportWritable(cricket::Transport* transport) { |
| 1124 | ASSERT(signaling_thread()->IsCurrent()); |
| 1125 | // TODO(bemasc): Expose more API from Transport to detect when |
| 1126 | // candidate selection starts or stops, due to success or failure. |
| 1127 | if (transport->all_channels_writable()) { |
| 1128 | if (ice_connection_state_ == |
| 1129 | PeerConnectionInterface::kIceConnectionChecking || |
| 1130 | ice_connection_state_ == |
| 1131 | PeerConnectionInterface::kIceConnectionDisconnected) { |
| 1132 | SetIceConnectionState(PeerConnectionInterface::kIceConnectionConnected); |
| 1133 | } |
| 1134 | } else if (transport->HasChannels()) { |
| 1135 | // If the current state is Connected or Completed, then there were writable |
| 1136 | // channels but now there are not, so the next state must be Disconnected. |
| 1137 | if (ice_connection_state_ == |
| 1138 | PeerConnectionInterface::kIceConnectionConnected || |
| 1139 | ice_connection_state_ == |
| 1140 | PeerConnectionInterface::kIceConnectionCompleted) { |
| 1141 | SetIceConnectionState( |
| 1142 | PeerConnectionInterface::kIceConnectionDisconnected); |
| 1143 | } |
| 1144 | } |
| 1145 | } |
| 1146 | |
| 1147 | void WebRtcSession::OnTransportProxyCandidatesReady( |
| 1148 | cricket::TransportProxy* proxy, const cricket::Candidates& candidates) { |
| 1149 | ASSERT(signaling_thread()->IsCurrent()); |
| 1150 | ProcessNewLocalCandidate(proxy->content_name(), candidates); |
| 1151 | } |
| 1152 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1153 | void WebRtcSession::OnCandidatesAllocationDone() { |
| 1154 | ASSERT(signaling_thread()->IsCurrent()); |
| 1155 | if (ice_observer_) { |
| 1156 | ice_observer_->OnIceGatheringChange( |
| 1157 | PeerConnectionInterface::kIceGatheringComplete); |
| 1158 | ice_observer_->OnIceComplete(); |
| 1159 | } |
| 1160 | } |
| 1161 | |
| 1162 | // Enabling voice and video channel. |
| 1163 | void WebRtcSession::EnableChannels() { |
| 1164 | if (voice_channel_ && !voice_channel_->enabled()) |
| 1165 | voice_channel_->Enable(true); |
| 1166 | |
| 1167 | if (video_channel_ && !video_channel_->enabled()) |
| 1168 | video_channel_->Enable(true); |
| 1169 | |
| 1170 | if (data_channel_.get() && !data_channel_->enabled()) |
| 1171 | data_channel_->Enable(true); |
| 1172 | } |
| 1173 | |
| 1174 | void WebRtcSession::ProcessNewLocalCandidate( |
| 1175 | const std::string& content_name, |
| 1176 | const cricket::Candidates& candidates) { |
| 1177 | int sdp_mline_index; |
| 1178 | if (!GetLocalCandidateMediaIndex(content_name, &sdp_mline_index)) { |
| 1179 | LOG(LS_ERROR) << "ProcessNewLocalCandidate: content name " |
| 1180 | << content_name << " not found"; |
| 1181 | return; |
| 1182 | } |
| 1183 | |
| 1184 | for (cricket::Candidates::const_iterator citer = candidates.begin(); |
| 1185 | citer != candidates.end(); ++citer) { |
| 1186 | // Use content_name as the candidate media id. |
| 1187 | JsepIceCandidate candidate(content_name, sdp_mline_index, *citer); |
| 1188 | if (ice_observer_) { |
| 1189 | ice_observer_->OnIceCandidate(&candidate); |
| 1190 | } |
| 1191 | if (local_desc_) { |
| 1192 | local_desc_->AddCandidate(&candidate); |
| 1193 | } |
| 1194 | } |
| 1195 | } |
| 1196 | |
| 1197 | // Returns the media index for a local ice candidate given the content name. |
| 1198 | bool WebRtcSession::GetLocalCandidateMediaIndex(const std::string& content_name, |
| 1199 | int* sdp_mline_index) { |
| 1200 | if (!BaseSession::local_description() || !sdp_mline_index) |
| 1201 | return false; |
| 1202 | |
| 1203 | bool content_found = false; |
| 1204 | const ContentInfos& contents = BaseSession::local_description()->contents(); |
| 1205 | for (size_t index = 0; index < contents.size(); ++index) { |
| 1206 | if (contents[index].name == content_name) { |
henrike@webrtc.org | 28654cb | 2013-07-22 21:07:49 +0000 | [diff] [blame] | 1207 | *sdp_mline_index = static_cast<int>(index); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1208 | content_found = true; |
| 1209 | break; |
| 1210 | } |
| 1211 | } |
| 1212 | return content_found; |
| 1213 | } |
| 1214 | |
| 1215 | bool WebRtcSession::UseCandidatesInSessionDescription( |
| 1216 | const SessionDescriptionInterface* remote_desc) { |
| 1217 | if (!remote_desc) |
| 1218 | return true; |
| 1219 | bool ret = true; |
| 1220 | for (size_t m = 0; m < remote_desc->number_of_mediasections(); ++m) { |
| 1221 | const IceCandidateCollection* candidates = remote_desc->candidates(m); |
| 1222 | for (size_t n = 0; n < candidates->count(); ++n) { |
| 1223 | ret = UseCandidate(candidates->at(n)); |
| 1224 | if (!ret) |
| 1225 | break; |
| 1226 | } |
| 1227 | } |
| 1228 | return ret; |
| 1229 | } |
| 1230 | |
| 1231 | bool WebRtcSession::UseCandidate( |
| 1232 | const IceCandidateInterface* candidate) { |
| 1233 | |
| 1234 | size_t mediacontent_index = static_cast<size_t>(candidate->sdp_mline_index()); |
| 1235 | size_t remote_content_size = |
| 1236 | BaseSession::remote_description()->contents().size(); |
| 1237 | if (mediacontent_index >= remote_content_size) { |
| 1238 | LOG(LS_ERROR) |
| 1239 | << "UseRemoteCandidateInSession: Invalid candidate media index."; |
| 1240 | return false; |
| 1241 | } |
| 1242 | |
| 1243 | cricket::ContentInfo content = |
| 1244 | BaseSession::remote_description()->contents()[mediacontent_index]; |
| 1245 | std::vector<cricket::Candidate> candidates; |
| 1246 | candidates.push_back(candidate->candidate()); |
| 1247 | // Invoking BaseSession method to handle remote candidates. |
| 1248 | std::string error; |
| 1249 | if (OnRemoteCandidates(content.name, candidates, &error)) { |
| 1250 | // Candidates successfully submitted for checking. |
| 1251 | if (ice_connection_state_ == PeerConnectionInterface::kIceConnectionNew || |
| 1252 | ice_connection_state_ == |
| 1253 | PeerConnectionInterface::kIceConnectionDisconnected) { |
| 1254 | // If state is New, then the session has just gotten its first remote ICE |
| 1255 | // candidates, so go to Checking. |
| 1256 | // If state is Disconnected, the session is re-using old candidates or |
| 1257 | // receiving additional ones, so go to Checking. |
| 1258 | // If state is Connected, stay Connected. |
| 1259 | // TODO(bemasc): If state is Connected, and the new candidates are for a |
| 1260 | // newly added transport, then the state actually _should_ move to |
| 1261 | // checking. Add a way to distinguish that case. |
| 1262 | SetIceConnectionState(PeerConnectionInterface::kIceConnectionChecking); |
| 1263 | } |
| 1264 | // TODO(bemasc): If state is Completed, go back to Connected. |
| 1265 | } else { |
| 1266 | LOG(LS_WARNING) << error; |
| 1267 | } |
| 1268 | return true; |
| 1269 | } |
| 1270 | |
| 1271 | void WebRtcSession::RemoveUnusedChannelsAndTransports( |
| 1272 | const SessionDescription* desc) { |
| 1273 | const cricket::ContentInfo* voice_info = |
| 1274 | cricket::GetFirstAudioContent(desc); |
| 1275 | if ((!voice_info || voice_info->rejected) && voice_channel_) { |
| 1276 | mediastream_signaling_->OnAudioChannelClose(); |
| 1277 | SignalVoiceChannelDestroyed(); |
| 1278 | const std::string content_name = voice_channel_->content_name(); |
| 1279 | channel_manager_->DestroyVoiceChannel(voice_channel_.release()); |
| 1280 | DestroyTransportProxy(content_name); |
| 1281 | } |
| 1282 | |
| 1283 | const cricket::ContentInfo* video_info = |
| 1284 | cricket::GetFirstVideoContent(desc); |
| 1285 | if ((!video_info || video_info->rejected) && video_channel_) { |
| 1286 | mediastream_signaling_->OnVideoChannelClose(); |
| 1287 | SignalVideoChannelDestroyed(); |
| 1288 | const std::string content_name = video_channel_->content_name(); |
| 1289 | channel_manager_->DestroyVideoChannel(video_channel_.release()); |
| 1290 | DestroyTransportProxy(content_name); |
| 1291 | } |
| 1292 | |
| 1293 | const cricket::ContentInfo* data_info = |
| 1294 | cricket::GetFirstDataContent(desc); |
| 1295 | if ((!data_info || data_info->rejected) && data_channel_) { |
| 1296 | mediastream_signaling_->OnDataChannelClose(); |
| 1297 | SignalDataChannelDestroyed(); |
| 1298 | const std::string content_name = data_channel_->content_name(); |
| 1299 | channel_manager_->DestroyDataChannel(data_channel_.release()); |
| 1300 | DestroyTransportProxy(content_name); |
| 1301 | } |
| 1302 | } |
| 1303 | |
henrike@webrtc.org | 1e09a71 | 2013-07-26 19:17:59 +0000 | [diff] [blame] | 1304 | // TODO(mallinath) - Add a correct error code if the channels are not creatued |
| 1305 | // due to BUNDLE is enabled but rtcp-mux is disabled. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1306 | bool WebRtcSession::CreateChannels(const SessionDescription* desc) { |
| 1307 | // Disabling the BUNDLE flag in PortAllocator if offer disabled it. |
henrike@webrtc.org | 1e09a71 | 2013-07-26 19:17:59 +0000 | [diff] [blame] | 1308 | bool bundle_enabled = desc->HasGroup(cricket::GROUP_TYPE_BUNDLE); |
| 1309 | if (state() == STATE_INIT && !bundle_enabled) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1310 | port_allocator()->set_flags(port_allocator()->flags() & |
| 1311 | ~cricket::PORTALLOCATOR_ENABLE_BUNDLE); |
| 1312 | } |
| 1313 | |
| 1314 | // Creating the media channels and transport proxies. |
| 1315 | const cricket::ContentInfo* voice = cricket::GetFirstAudioContent(desc); |
| 1316 | if (voice && !voice->rejected && !voice_channel_) { |
henrike@webrtc.org | 1e09a71 | 2013-07-26 19:17:59 +0000 | [diff] [blame] | 1317 | if (!CreateVoiceChannel(voice)) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1318 | LOG(LS_ERROR) << "Failed to create voice channel."; |
| 1319 | return false; |
| 1320 | } |
| 1321 | } |
| 1322 | |
| 1323 | const cricket::ContentInfo* video = cricket::GetFirstVideoContent(desc); |
| 1324 | if (video && !video->rejected && !video_channel_) { |
henrike@webrtc.org | 1e09a71 | 2013-07-26 19:17:59 +0000 | [diff] [blame] | 1325 | if (!CreateVideoChannel(video)) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1326 | LOG(LS_ERROR) << "Failed to create video channel."; |
| 1327 | return false; |
| 1328 | } |
| 1329 | } |
| 1330 | |
| 1331 | const cricket::ContentInfo* data = cricket::GetFirstDataContent(desc); |
| 1332 | if (data_channel_type_ != cricket::DCT_NONE && |
| 1333 | data && !data->rejected && !data_channel_.get()) { |
henrike@webrtc.org | 1e09a71 | 2013-07-26 19:17:59 +0000 | [diff] [blame] | 1334 | if (!CreateDataChannel(data)) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1335 | LOG(LS_ERROR) << "Failed to create data channel."; |
| 1336 | return false; |
| 1337 | } |
| 1338 | } |
| 1339 | |
| 1340 | return true; |
| 1341 | } |
| 1342 | |
henrike@webrtc.org | 1e09a71 | 2013-07-26 19:17:59 +0000 | [diff] [blame] | 1343 | bool WebRtcSession::CreateVoiceChannel(const cricket::ContentInfo* content) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1344 | voice_channel_.reset(channel_manager_->CreateVoiceChannel( |
henrike@webrtc.org | 1e09a71 | 2013-07-26 19:17:59 +0000 | [diff] [blame] | 1345 | this, content->name, true)); |
| 1346 | return (voice_channel_ != NULL); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1347 | } |
| 1348 | |
henrike@webrtc.org | 1e09a71 | 2013-07-26 19:17:59 +0000 | [diff] [blame] | 1349 | bool WebRtcSession::CreateVideoChannel(const cricket::ContentInfo* content) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1350 | video_channel_.reset(channel_manager_->CreateVideoChannel( |
henrike@webrtc.org | 1e09a71 | 2013-07-26 19:17:59 +0000 | [diff] [blame] | 1351 | this, content->name, true, voice_channel_.get())); |
| 1352 | return (video_channel_ != NULL); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1353 | } |
| 1354 | |
henrike@webrtc.org | 1e09a71 | 2013-07-26 19:17:59 +0000 | [diff] [blame] | 1355 | bool WebRtcSession::CreateDataChannel(const cricket::ContentInfo* content) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1356 | bool rtcp = (data_channel_type_ == cricket::DCT_RTP); |
| 1357 | data_channel_.reset(channel_manager_->CreateDataChannel( |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 1358 | this, content->name, rtcp, data_channel_type_)); |
| 1359 | if (!data_channel_.get()) { |
| 1360 | return false; |
| 1361 | } |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 +0000 | [diff] [blame] | 1362 | data_channel_->SignalNewStreamReceived.connect( |
| 1363 | this, &WebRtcSession::OnNewDataChannelReceived); |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 1364 | return true; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1365 | } |
| 1366 | |
| 1367 | void WebRtcSession::CopySavedCandidates( |
| 1368 | SessionDescriptionInterface* dest_desc) { |
| 1369 | if (!dest_desc) { |
| 1370 | ASSERT(false); |
| 1371 | return; |
| 1372 | } |
| 1373 | for (size_t i = 0; i < saved_candidates_.size(); ++i) { |
| 1374 | dest_desc->AddCandidate(saved_candidates_[i]); |
| 1375 | delete saved_candidates_[i]; |
| 1376 | } |
| 1377 | saved_candidates_.clear(); |
| 1378 | } |
| 1379 | |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 +0000 | [diff] [blame] | 1380 | void WebRtcSession::OnNewDataChannelReceived( |
| 1381 | const std::string& label, const DataChannelInit& init) { |
| 1382 | ASSERT(data_channel_type_ == cricket::DCT_SCTP); |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 1383 | if (!mediastream_signaling_->AddDataChannelFromOpenMessage( |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 +0000 | [diff] [blame] | 1384 | label, init)) { |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 1385 | LOG(LS_WARNING) << "Failed to create data channel from OPEN message."; |
| 1386 | return; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1387 | } |
| 1388 | } |
| 1389 | |
henrike@webrtc.org | 1e09a71 | 2013-07-26 19:17:59 +0000 | [diff] [blame] | 1390 | // Returns false if bundle is enabled and rtcp_mux is disabled. |
sergeyu@chromium.org | 0be6aa0 | 2013-08-23 23:21:25 +0000 | [diff] [blame] | 1391 | bool WebRtcSession::ValidateBundleSettings(const SessionDescription* desc) { |
henrike@webrtc.org | 1e09a71 | 2013-07-26 19:17:59 +0000 | [diff] [blame] | 1392 | bool bundle_enabled = desc->HasGroup(cricket::GROUP_TYPE_BUNDLE); |
| 1393 | if (!bundle_enabled) |
| 1394 | return true; |
| 1395 | |
| 1396 | const cricket::ContentGroup* bundle_group = |
| 1397 | desc->GetGroupByName(cricket::GROUP_TYPE_BUNDLE); |
| 1398 | ASSERT(bundle_group != NULL); |
| 1399 | |
| 1400 | const cricket::ContentInfos& contents = desc->contents(); |
| 1401 | for (cricket::ContentInfos::const_iterator citer = contents.begin(); |
| 1402 | citer != contents.end(); ++citer) { |
| 1403 | const cricket::ContentInfo* content = (&*citer); |
| 1404 | ASSERT(content != NULL); |
| 1405 | if (bundle_group->HasContentName(content->name) && |
| 1406 | !content->rejected && content->type == cricket::NS_JINGLE_RTP) { |
| 1407 | if (!HasRtcpMuxEnabled(content)) |
| 1408 | return false; |
| 1409 | } |
| 1410 | } |
| 1411 | // RTCP-MUX is enabled in all the contents. |
| 1412 | return true; |
| 1413 | } |
| 1414 | |
| 1415 | bool WebRtcSession::HasRtcpMuxEnabled( |
| 1416 | const cricket::ContentInfo* content) { |
| 1417 | const cricket::MediaContentDescription* description = |
| 1418 | static_cast<cricket::MediaContentDescription*>(content->description); |
| 1419 | return description->rtcp_mux(); |
| 1420 | } |
| 1421 | |
sergeyu@chromium.org | 0be6aa0 | 2013-08-23 23:21:25 +0000 | [diff] [blame] | 1422 | bool WebRtcSession::ValidateSessionDescription( |
| 1423 | const SessionDescriptionInterface* sdesc, |
| 1424 | cricket::ContentSource source, std::string* error_desc) { |
| 1425 | |
| 1426 | if (error() != cricket::BaseSession::ERROR_NONE) { |
| 1427 | return BadSdp(source, SessionErrorMsg(error()), error_desc); |
| 1428 | } |
| 1429 | |
| 1430 | if (!sdesc || !sdesc->description()) { |
| 1431 | return BadSdp(source, kInvalidSdp, error_desc); |
| 1432 | } |
| 1433 | |
| 1434 | std::string type = sdesc->type(); |
| 1435 | Action action = GetAction(sdesc->type()); |
| 1436 | if (source == cricket::CS_LOCAL) { |
| 1437 | if (!ExpectSetLocalDescription(action)) |
| 1438 | return BadSdp(source, BadStateErrMsg(type, state()), error_desc); |
| 1439 | } else { |
| 1440 | if (!ExpectSetRemoteDescription(action)) |
| 1441 | return BadSdp(source, BadStateErrMsg(type, state()), error_desc); |
| 1442 | } |
| 1443 | |
| 1444 | // Verify crypto settings. |
mallinath@webrtc.org | a27be8e | 2013-09-27 23:04:10 +0000 | [diff] [blame] | 1445 | std::string crypto_error; |
sergeyu@chromium.org | 0be6aa0 | 2013-08-23 23:21:25 +0000 | [diff] [blame] | 1446 | if (webrtc_session_desc_factory_->secure() == cricket::SEC_REQUIRED && |
mallinath@webrtc.org | a27be8e | 2013-09-27 23:04:10 +0000 | [diff] [blame] | 1447 | !VerifyCrypto(sdesc->description(), dtls_enabled_, &crypto_error)) { |
| 1448 | return BadSdp(source, crypto_error, error_desc); |
sergeyu@chromium.org | 0be6aa0 | 2013-08-23 23:21:25 +0000 | [diff] [blame] | 1449 | } |
| 1450 | |
mallinath@webrtc.org | 19f27e6 | 2013-10-13 17:18:27 +0000 | [diff] [blame] | 1451 | // Verify ice-ufrag and ice-pwd. |
| 1452 | if (!VerifyIceUfragPwdPresent(sdesc->description())) { |
| 1453 | return BadSdp(source, kSdpWithoutIceUfragPwd, error_desc); |
| 1454 | } |
| 1455 | |
sergeyu@chromium.org | 0be6aa0 | 2013-08-23 23:21:25 +0000 | [diff] [blame] | 1456 | if (!ValidateBundleSettings(sdesc->description())) { |
| 1457 | return BadSdp(source, kBundleWithoutRtcpMux, error_desc); |
| 1458 | } |
| 1459 | |
| 1460 | // Verify m-lines in Answer when compared against Offer. |
| 1461 | if (action == kAnswer) { |
| 1462 | const cricket::SessionDescription* offer_desc = |
| 1463 | (source == cricket::CS_LOCAL) ? remote_description()->description() : |
| 1464 | local_description()->description(); |
| 1465 | if (!VerifyMediaDescriptions(sdesc->description(), offer_desc)) { |
| 1466 | return BadSdp(source, kMlineMismatch, error_desc); |
| 1467 | } |
| 1468 | } |
| 1469 | |
| 1470 | return true; |
| 1471 | } |
| 1472 | |
| 1473 | bool WebRtcSession::ExpectSetLocalDescription(Action action) { |
| 1474 | return ((action == kOffer && state() == STATE_INIT) || |
| 1475 | // update local offer |
| 1476 | (action == kOffer && state() == STATE_SENTINITIATE) || |
| 1477 | // update the current ongoing session. |
| 1478 | (action == kOffer && state() == STATE_RECEIVEDACCEPT) || |
| 1479 | (action == kOffer && state() == STATE_SENTACCEPT) || |
| 1480 | (action == kOffer && state() == STATE_INPROGRESS) || |
| 1481 | // accept remote offer |
| 1482 | (action == kAnswer && state() == STATE_RECEIVEDINITIATE) || |
| 1483 | (action == kAnswer && state() == STATE_SENTPRACCEPT) || |
| 1484 | (action == kPrAnswer && state() == STATE_RECEIVEDINITIATE) || |
| 1485 | (action == kPrAnswer && state() == STATE_SENTPRACCEPT)); |
| 1486 | } |
| 1487 | |
| 1488 | bool WebRtcSession::ExpectSetRemoteDescription(Action action) { |
| 1489 | return ((action == kOffer && state() == STATE_INIT) || |
| 1490 | // update remote offer |
| 1491 | (action == kOffer && state() == STATE_RECEIVEDINITIATE) || |
| 1492 | // update the current ongoing session |
| 1493 | (action == kOffer && state() == STATE_RECEIVEDACCEPT) || |
| 1494 | (action == kOffer && state() == STATE_SENTACCEPT) || |
| 1495 | (action == kOffer && state() == STATE_INPROGRESS) || |
| 1496 | // accept local offer |
| 1497 | (action == kAnswer && state() == STATE_SENTINITIATE) || |
| 1498 | (action == kAnswer && state() == STATE_RECEIVEDPRACCEPT) || |
| 1499 | (action == kPrAnswer && state() == STATE_SENTINITIATE) || |
| 1500 | (action == kPrAnswer && state() == STATE_RECEIVEDPRACCEPT)); |
| 1501 | } |
| 1502 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1503 | } // namespace webrtc |