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