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