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