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