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