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