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