jlmiller@webrtc.org | 5f93d0a | 2015-01-20 21:36:13 | [diff] [blame] | 1 | /* |
kjellander | b24317b | 2016-02-10 15:54:43 | [diff] [blame] | 2 | * Copyright 2012 The WebRTC project authors. All Rights Reserved. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 3 | * |
kjellander | b24317b | 2016-02-10 15:54:43 | [diff] [blame] | 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 9 | */ |
| 10 | |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 11 | #include "api/jsepsessiondescription.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 12 | |
kwiberg | d1fe281 | 2016-04-27 13:47:29 | [diff] [blame] | 13 | #include <memory> |
| 14 | |
Steve Anton | 88f2cb9 | 2017-12-05 20:47:32 | [diff] [blame] | 15 | #include "p2p/base/port.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 16 | #include "pc/mediasession.h" |
| 17 | #include "pc/webrtcsdp.h" |
| 18 | #include "rtc_base/arraysize.h" |
Steve Anton | 88f2cb9 | 2017-12-05 20:47:32 | [diff] [blame] | 19 | #include "rtc_base/ptr_util.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 20 | #include "rtc_base/stringencode.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 21 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 22 | using cricket::SessionDescription; |
| 23 | |
| 24 | namespace webrtc { |
Steve Anton | 88f2cb9 | 2017-12-05 20:47:32 | [diff] [blame] | 25 | namespace { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 26 | |
zhihuang | 38989e5 | 2017-03-21 18:04:53 | [diff] [blame] | 27 | // RFC 5245 |
| 28 | // It is RECOMMENDED that default candidates be chosen based on the |
| 29 | // likelihood of those candidates to work with the peer that is being |
| 30 | // contacted. It is RECOMMENDED that relayed > reflexive > host. |
Steve Anton | 88f2cb9 | 2017-12-05 20:47:32 | [diff] [blame] | 31 | constexpr int kPreferenceUnknown = 0; |
| 32 | constexpr int kPreferenceHost = 1; |
| 33 | constexpr int kPreferenceReflexive = 2; |
| 34 | constexpr int kPreferenceRelayed = 3; |
zhihuang | 38989e5 | 2017-03-21 18:04:53 | [diff] [blame] | 35 | |
Steve Anton | 88f2cb9 | 2017-12-05 20:47:32 | [diff] [blame] | 36 | constexpr char kDummyAddress[] = "0.0.0.0"; |
| 37 | constexpr int kDummyPort = 9; |
zhihuang | 38989e5 | 2017-03-21 18:04:53 | [diff] [blame] | 38 | |
Steve Anton | 88f2cb9 | 2017-12-05 20:47:32 | [diff] [blame] | 39 | int GetCandidatePreferenceFromType(const std::string& type) { |
zhihuang | 38989e5 | 2017-03-21 18:04:53 | [diff] [blame] | 40 | int preference = kPreferenceUnknown; |
| 41 | if (type == cricket::LOCAL_PORT_TYPE) { |
| 42 | preference = kPreferenceHost; |
| 43 | } else if (type == cricket::STUN_PORT_TYPE) { |
| 44 | preference = kPreferenceReflexive; |
| 45 | } else if (type == cricket::RELAY_PORT_TYPE) { |
| 46 | preference = kPreferenceRelayed; |
| 47 | } else { |
zhihuang | 1523865 | 2017-03-23 17:32:12 | [diff] [blame] | 48 | preference = kPreferenceUnknown; |
zhihuang | 38989e5 | 2017-03-21 18:04:53 | [diff] [blame] | 49 | } |
| 50 | return preference; |
| 51 | } |
| 52 | |
| 53 | // Update the connection address for the MediaContentDescription based on the |
| 54 | // candidates. |
Steve Anton | 88f2cb9 | 2017-12-05 20:47:32 | [diff] [blame] | 55 | void UpdateConnectionAddress( |
zhihuang | 38989e5 | 2017-03-21 18:04:53 | [diff] [blame] | 56 | const JsepCandidateCollection& candidate_collection, |
Steve Anton | b1c1de1 | 2017-12-21 23:14:30 | [diff] [blame] | 57 | cricket::MediaContentDescription* media_desc) { |
zhihuang | 38989e5 | 2017-03-21 18:04:53 | [diff] [blame] | 58 | int port = kDummyPort; |
| 59 | std::string ip = kDummyAddress; |
| 60 | int current_preference = kPreferenceUnknown; |
| 61 | int current_family = AF_UNSPEC; |
| 62 | for (size_t i = 0; i < candidate_collection.count(); ++i) { |
| 63 | const IceCandidateInterface* jsep_candidate = candidate_collection.at(i); |
| 64 | if (jsep_candidate->candidate().component() != |
| 65 | cricket::ICE_CANDIDATE_COMPONENT_RTP) { |
| 66 | continue; |
| 67 | } |
| 68 | // Default destination should be UDP only. |
| 69 | if (jsep_candidate->candidate().protocol() != cricket::UDP_PROTOCOL_NAME) { |
| 70 | continue; |
| 71 | } |
| 72 | const int preference = |
| 73 | GetCandidatePreferenceFromType(jsep_candidate->candidate().type()); |
| 74 | const int family = jsep_candidate->candidate().address().ipaddr().family(); |
| 75 | // See if this candidate is more preferable then the current one if it's the |
| 76 | // same family. Or if the current family is IPv4 already so we could safely |
| 77 | // ignore all IPv6 ones. WebRTC bug 4269. |
| 78 | // http://code.google.com/p/webrtc/issues/detail?id=4269 |
| 79 | if ((preference <= current_preference && current_family == family) || |
| 80 | (current_family == AF_INET && family == AF_INET6)) { |
| 81 | continue; |
| 82 | } |
| 83 | current_preference = preference; |
| 84 | current_family = family; |
| 85 | port = jsep_candidate->candidate().address().port(); |
| 86 | ip = jsep_candidate->candidate().address().ipaddr().ToString(); |
| 87 | } |
| 88 | rtc::SocketAddress connection_addr; |
| 89 | connection_addr.SetIP(ip); |
| 90 | connection_addr.SetPort(port); |
Steve Anton | b1c1de1 | 2017-12-21 23:14:30 | [diff] [blame] | 91 | media_desc->set_connection_address(connection_addr); |
zhihuang | 38989e5 | 2017-03-21 18:04:53 | [diff] [blame] | 92 | } |
| 93 | |
Steve Anton | 88f2cb9 | 2017-12-05 20:47:32 | [diff] [blame] | 94 | } // namespace |
| 95 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 96 | const char SessionDescriptionInterface::kOffer[] = "offer"; |
| 97 | const char SessionDescriptionInterface::kPrAnswer[] = "pranswer"; |
| 98 | const char SessionDescriptionInterface::kAnswer[] = "answer"; |
| 99 | |
| 100 | const int JsepSessionDescription::kDefaultVideoCodecId = 100; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 101 | const char JsepSessionDescription::kDefaultVideoCodecName[] = "VP8"; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 102 | |
Steve Anton | 88f2cb9 | 2017-12-05 20:47:32 | [diff] [blame] | 103 | const char* SdpTypeToString(SdpType type) { |
| 104 | switch (type) { |
| 105 | case SdpType::kOffer: |
| 106 | return SessionDescriptionInterface::kOffer; |
| 107 | case SdpType::kPrAnswer: |
| 108 | return SessionDescriptionInterface::kPrAnswer; |
| 109 | case SdpType::kAnswer: |
| 110 | return SessionDescriptionInterface::kAnswer; |
| 111 | } |
| 112 | return ""; |
| 113 | } |
| 114 | |
| 115 | rtc::Optional<SdpType> SdpTypeFromString(const std::string& type_str) { |
| 116 | if (type_str == SessionDescriptionInterface::kOffer) { |
| 117 | return SdpType::kOffer; |
| 118 | } else if (type_str == SessionDescriptionInterface::kPrAnswer) { |
| 119 | return SdpType::kPrAnswer; |
| 120 | } else if (type_str == SessionDescriptionInterface::kAnswer) { |
| 121 | return SdpType::kAnswer; |
| 122 | } else { |
| 123 | return rtc::nullopt; |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | // TODO(steveanton): Remove this default implementation once Chromium has been |
| 128 | // updated. |
| 129 | SdpType SessionDescriptionInterface::GetType() const { |
| 130 | rtc::Optional<SdpType> maybe_type = SdpTypeFromString(type()); |
| 131 | if (maybe_type) { |
| 132 | return *maybe_type; |
| 133 | } else { |
| 134 | RTC_LOG(LS_WARNING) << "Default implementation of " |
| 135 | "SessionDescriptionInterface::GetType does not " |
| 136 | "recognize the result from type(), returning " |
| 137 | "kOffer."; |
| 138 | return SdpType::kOffer; |
| 139 | } |
| 140 | } |
| 141 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 142 | SessionDescriptionInterface* CreateSessionDescription(const std::string& type, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 143 | const std::string& sdp, |
| 144 | SdpParseError* error) { |
Steve Anton | 88f2cb9 | 2017-12-05 20:47:32 | [diff] [blame] | 145 | rtc::Optional<SdpType> maybe_type = SdpTypeFromString(type); |
| 146 | if (!maybe_type) { |
| 147 | return nullptr; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 148 | } |
| 149 | |
Steve Anton | 88f2cb9 | 2017-12-05 20:47:32 | [diff] [blame] | 150 | return CreateSessionDescription(*maybe_type, sdp, error).release(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 151 | } |
| 152 | |
Steve Anton | 88f2cb9 | 2017-12-05 20:47:32 | [diff] [blame] | 153 | std::unique_ptr<SessionDescriptionInterface> CreateSessionDescription( |
| 154 | SdpType type, |
| 155 | const std::string& sdp) { |
| 156 | return CreateSessionDescription(type, sdp, nullptr); |
| 157 | } |
| 158 | |
| 159 | std::unique_ptr<SessionDescriptionInterface> CreateSessionDescription( |
| 160 | SdpType type, |
| 161 | const std::string& sdp, |
| 162 | SdpParseError* error_out) { |
| 163 | auto jsep_desc = rtc::MakeUnique<JsepSessionDescription>(type); |
| 164 | if (!SdpDeserialize(sdp, jsep_desc.get(), error_out)) { |
| 165 | return nullptr; |
| 166 | } |
| 167 | return std::move(jsep_desc); |
| 168 | } |
| 169 | |
| 170 | JsepSessionDescription::JsepSessionDescription(SdpType type) : type_(type) {} |
| 171 | |
| 172 | JsepSessionDescription::JsepSessionDescription(const std::string& type) { |
| 173 | rtc::Optional<SdpType> maybe_type = SdpTypeFromString(type); |
| 174 | if (maybe_type) { |
| 175 | type_ = *maybe_type; |
| 176 | } else { |
| 177 | RTC_LOG(LS_WARNING) |
| 178 | << "JsepSessionDescription constructed with invalid type string: " |
| 179 | << type << ". Assuming it is an offer."; |
| 180 | type_ = SdpType::kOffer; |
| 181 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | JsepSessionDescription::~JsepSessionDescription() {} |
| 185 | |
| 186 | bool JsepSessionDescription::Initialize( |
| 187 | cricket::SessionDescription* description, |
| 188 | const std::string& session_id, |
| 189 | const std::string& session_version) { |
| 190 | if (!description) |
| 191 | return false; |
| 192 | |
| 193 | session_id_ = session_id; |
| 194 | session_version_ = session_version; |
| 195 | description_.reset(description); |
| 196 | candidate_collection_.resize(number_of_mediasections()); |
| 197 | return true; |
| 198 | } |
| 199 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 200 | bool JsepSessionDescription::AddCandidate( |
| 201 | const IceCandidateInterface* candidate) { |
| 202 | if (!candidate || candidate->sdp_mline_index() < 0) |
| 203 | return false; |
| 204 | size_t mediasection_index = 0; |
| 205 | if (!GetMediasectionIndex(candidate, &mediasection_index)) { |
| 206 | return false; |
| 207 | } |
| 208 | if (mediasection_index >= number_of_mediasections()) |
| 209 | return false; |
jbauch | 083b73f | 2015-07-16 09:46:32 | [diff] [blame] | 210 | const std::string& content_name = |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 211 | description_->contents()[mediasection_index].name; |
| 212 | const cricket::TransportInfo* transport_info = |
| 213 | description_->GetTransportInfoByName(content_name); |
| 214 | if (!transport_info) { |
| 215 | return false; |
| 216 | } |
| 217 | |
| 218 | cricket::Candidate updated_candidate = candidate->candidate(); |
| 219 | if (updated_candidate.username().empty()) { |
| 220 | updated_candidate.set_username(transport_info->description.ice_ufrag); |
| 221 | } |
| 222 | if (updated_candidate.password().empty()) { |
| 223 | updated_candidate.set_password(transport_info->description.ice_pwd); |
| 224 | } |
| 225 | |
kwiberg | d1fe281 | 2016-04-27 13:47:29 | [diff] [blame] | 226 | std::unique_ptr<JsepIceCandidate> updated_candidate_wrapper( |
mallinath@webrtc.org | 67ee6b9 | 2014-02-03 16:57:16 | [diff] [blame] | 227 | new JsepIceCandidate(candidate->sdp_mid(), |
| 228 | static_cast<int>(mediasection_index), |
| 229 | updated_candidate)); |
| 230 | if (!candidate_collection_[mediasection_index].HasCandidate( |
zhihuang | 38989e5 | 2017-03-21 18:04:53 | [diff] [blame] | 231 | updated_candidate_wrapper.get())) { |
mallinath@webrtc.org | 67ee6b9 | 2014-02-03 16:57:16 | [diff] [blame] | 232 | candidate_collection_[mediasection_index].add( |
| 233 | updated_candidate_wrapper.release()); |
zhihuang | 38989e5 | 2017-03-21 18:04:53 | [diff] [blame] | 234 | UpdateConnectionAddress( |
| 235 | candidate_collection_[mediasection_index], |
Steve Anton | b1c1de1 | 2017-12-21 23:14:30 | [diff] [blame] | 236 | description_->contents()[mediasection_index].media_description()); |
zhihuang | 38989e5 | 2017-03-21 18:04:53 | [diff] [blame] | 237 | } |
mallinath@webrtc.org | 67ee6b9 | 2014-02-03 16:57:16 | [diff] [blame] | 238 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 239 | return true; |
| 240 | } |
| 241 | |
Honghai Zhang | 7fb69db | 2016-03-14 18:59:18 | [diff] [blame] | 242 | size_t JsepSessionDescription::RemoveCandidates( |
| 243 | const std::vector<cricket::Candidate>& candidates) { |
| 244 | size_t num_removed = 0; |
| 245 | for (auto& candidate : candidates) { |
| 246 | int mediasection_index = GetMediasectionIndex(candidate); |
| 247 | if (mediasection_index < 0) { |
| 248 | // Not found. |
| 249 | continue; |
| 250 | } |
| 251 | num_removed += candidate_collection_[mediasection_index].remove(candidate); |
zhihuang | 38989e5 | 2017-03-21 18:04:53 | [diff] [blame] | 252 | UpdateConnectionAddress( |
| 253 | candidate_collection_[mediasection_index], |
Steve Anton | b1c1de1 | 2017-12-21 23:14:30 | [diff] [blame] | 254 | description_->contents()[mediasection_index].media_description()); |
Honghai Zhang | 7fb69db | 2016-03-14 18:59:18 | [diff] [blame] | 255 | } |
| 256 | return num_removed; |
| 257 | } |
| 258 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 259 | size_t JsepSessionDescription::number_of_mediasections() const { |
| 260 | if (!description_) |
| 261 | return 0; |
| 262 | return description_->contents().size(); |
| 263 | } |
| 264 | |
| 265 | const IceCandidateCollection* JsepSessionDescription::candidates( |
| 266 | size_t mediasection_index) const { |
| 267 | if (mediasection_index >= candidate_collection_.size()) |
| 268 | return NULL; |
| 269 | return &candidate_collection_[mediasection_index]; |
| 270 | } |
| 271 | |
| 272 | bool JsepSessionDescription::ToString(std::string* out) const { |
deadbeef | 9d3584c | 2016-02-17 01:54:10 | [diff] [blame] | 273 | if (!description_ || !out) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 274 | return false; |
deadbeef | 9d3584c | 2016-02-17 01:54:10 | [diff] [blame] | 275 | } |
Steve Anton | e831b8c | 2018-02-01 20:22:16 | [diff] [blame^] | 276 | *out = SdpSerialize(*this); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 277 | return !out->empty(); |
| 278 | } |
| 279 | |
| 280 | bool JsepSessionDescription::GetMediasectionIndex( |
| 281 | const IceCandidateInterface* candidate, |
| 282 | size_t* index) { |
| 283 | if (!candidate || !index) { |
| 284 | return false; |
| 285 | } |
| 286 | *index = static_cast<size_t>(candidate->sdp_mline_index()); |
| 287 | if (description_ && !candidate->sdp_mid().empty()) { |
| 288 | bool found = false; |
| 289 | // Try to match the sdp_mid with content name. |
| 290 | for (size_t i = 0; i < description_->contents().size(); ++i) { |
| 291 | if (candidate->sdp_mid() == description_->contents().at(i).name) { |
| 292 | *index = i; |
| 293 | found = true; |
| 294 | break; |
| 295 | } |
| 296 | } |
| 297 | if (!found) { |
| 298 | // If the sdp_mid is presented but we can't find a match, we consider |
| 299 | // this as an error. |
| 300 | return false; |
| 301 | } |
| 302 | } |
| 303 | return true; |
| 304 | } |
| 305 | |
Honghai Zhang | 7fb69db | 2016-03-14 18:59:18 | [diff] [blame] | 306 | int JsepSessionDescription::GetMediasectionIndex( |
| 307 | const cricket::Candidate& candidate) { |
| 308 | // Find the description with a matching transport name of the candidate. |
| 309 | const std::string& transport_name = candidate.transport_name(); |
| 310 | for (size_t i = 0; i < description_->contents().size(); ++i) { |
| 311 | if (transport_name == description_->contents().at(i).name) { |
| 312 | return static_cast<int>(i); |
| 313 | } |
| 314 | } |
| 315 | return -1; |
| 316 | } |
| 317 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 318 | } // namespace webrtc |