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 | |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 11 | #include "api/jsep_session_description.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> |
Harald Alvestrand | c24a218 | 2022-02-23 13:44:59 | [diff] [blame] | 14 | #include <utility> |
kwiberg | d1fe281 | 2016-04-27 13:47:29 | [diff] [blame] | 15 | |
Harald Alvestrand | c24a218 | 2022-02-23 13:44:59 | [diff] [blame] | 16 | #include "absl/types/optional.h" |
| 17 | #include "p2p/base/p2p_constants.h" |
Steve Anton | 88f2cb9 | 2017-12-05 20:47:32 | [diff] [blame] | 18 | #include "p2p/base/port.h" |
Harald Alvestrand | c24a218 | 2022-02-23 13:44:59 | [diff] [blame] | 19 | #include "p2p/base/transport_description.h" |
| 20 | #include "p2p/base/transport_info.h" |
| 21 | #include "pc/media_session.h" // IWYU pragma: keep |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 22 | #include "pc/webrtc_sdp.h" |
Harald Alvestrand | c24a218 | 2022-02-23 13:44:59 | [diff] [blame] | 23 | #include "rtc_base/checks.h" |
| 24 | #include "rtc_base/ip_address.h" |
| 25 | #include "rtc_base/logging.h" |
| 26 | #include "rtc_base/net_helper.h" |
| 27 | #include "rtc_base/socket_address.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 28 | |
Tommi | 0a7fc84 | 2024-01-19 12:11:37 | [diff] [blame] | 29 | using cricket::Candidate; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 30 | using cricket::SessionDescription; |
| 31 | |
| 32 | namespace webrtc { |
Steve Anton | 88f2cb9 | 2017-12-05 20:47:32 | [diff] [blame] | 33 | namespace { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 34 | |
zhihuang | 38989e5 | 2017-03-21 18:04:53 | [diff] [blame] | 35 | // RFC 5245 |
| 36 | // It is RECOMMENDED that default candidates be chosen based on the |
| 37 | // likelihood of those candidates to work with the peer that is being |
| 38 | // contacted. It is RECOMMENDED that relayed > reflexive > host. |
Steve Anton | 88f2cb9 | 2017-12-05 20:47:32 | [diff] [blame] | 39 | constexpr int kPreferenceUnknown = 0; |
| 40 | constexpr int kPreferenceHost = 1; |
| 41 | constexpr int kPreferenceReflexive = 2; |
| 42 | constexpr int kPreferenceRelayed = 3; |
zhihuang | 38989e5 | 2017-03-21 18:04:53 | [diff] [blame] | 43 | |
Steve Anton | 88f2cb9 | 2017-12-05 20:47:32 | [diff] [blame] | 44 | constexpr char kDummyAddress[] = "0.0.0.0"; |
| 45 | constexpr int kDummyPort = 9; |
zhihuang | 38989e5 | 2017-03-21 18:04:53 | [diff] [blame] | 46 | |
Tommi | 0a7fc84 | 2024-01-19 12:11:37 | [diff] [blame] | 47 | int GetCandidatePreferenceFromType(const Candidate& c) { |
zhihuang | 38989e5 | 2017-03-21 18:04:53 | [diff] [blame] | 48 | int preference = kPreferenceUnknown; |
Tommi | 0a7fc84 | 2024-01-19 12:11:37 | [diff] [blame] | 49 | if (c.is_local()) { |
zhihuang | 38989e5 | 2017-03-21 18:04:53 | [diff] [blame] | 50 | preference = kPreferenceHost; |
Tommi | 0a7fc84 | 2024-01-19 12:11:37 | [diff] [blame] | 51 | } else if (c.is_stun()) { |
zhihuang | 38989e5 | 2017-03-21 18:04:53 | [diff] [blame] | 52 | preference = kPreferenceReflexive; |
Tommi | 0a7fc84 | 2024-01-19 12:11:37 | [diff] [blame] | 53 | } else if (c.is_relay()) { |
zhihuang | 38989e5 | 2017-03-21 18:04:53 | [diff] [blame] | 54 | preference = kPreferenceRelayed; |
| 55 | } else { |
zhihuang | 1523865 | 2017-03-23 17:32:12 | [diff] [blame] | 56 | preference = kPreferenceUnknown; |
zhihuang | 38989e5 | 2017-03-21 18:04:53 | [diff] [blame] | 57 | } |
| 58 | return preference; |
| 59 | } |
| 60 | |
| 61 | // Update the connection address for the MediaContentDescription based on the |
| 62 | // candidates. |
Steve Anton | 88f2cb9 | 2017-12-05 20:47:32 | [diff] [blame] | 63 | void UpdateConnectionAddress( |
zhihuang | 38989e5 | 2017-03-21 18:04:53 | [diff] [blame] | 64 | const JsepCandidateCollection& candidate_collection, |
Steve Anton | b1c1de1 | 2017-12-21 23:14:30 | [diff] [blame] | 65 | cricket::MediaContentDescription* media_desc) { |
zhihuang | 38989e5 | 2017-03-21 18:04:53 | [diff] [blame] | 66 | int port = kDummyPort; |
| 67 | std::string ip = kDummyAddress; |
Qingsi Wang | 5699142 | 2019-02-08 20:53:06 | [diff] [blame] | 68 | std::string hostname; |
zhihuang | 38989e5 | 2017-03-21 18:04:53 | [diff] [blame] | 69 | int current_preference = kPreferenceUnknown; |
| 70 | int current_family = AF_UNSPEC; |
| 71 | for (size_t i = 0; i < candidate_collection.count(); ++i) { |
| 72 | const IceCandidateInterface* jsep_candidate = candidate_collection.at(i); |
| 73 | if (jsep_candidate->candidate().component() != |
| 74 | cricket::ICE_CANDIDATE_COMPONENT_RTP) { |
| 75 | continue; |
| 76 | } |
| 77 | // Default destination should be UDP only. |
| 78 | if (jsep_candidate->candidate().protocol() != cricket::UDP_PROTOCOL_NAME) { |
| 79 | continue; |
| 80 | } |
| 81 | const int preference = |
Tommi | 0a7fc84 | 2024-01-19 12:11:37 | [diff] [blame] | 82 | GetCandidatePreferenceFromType(jsep_candidate->candidate()); |
zhihuang | 38989e5 | 2017-03-21 18:04:53 | [diff] [blame] | 83 | const int family = jsep_candidate->candidate().address().ipaddr().family(); |
| 84 | // See if this candidate is more preferable then the current one if it's the |
| 85 | // same family. Or if the current family is IPv4 already so we could safely |
| 86 | // ignore all IPv6 ones. WebRTC bug 4269. |
| 87 | // http://code.google.com/p/webrtc/issues/detail?id=4269 |
| 88 | if ((preference <= current_preference && current_family == family) || |
| 89 | (current_family == AF_INET && family == AF_INET6)) { |
| 90 | continue; |
| 91 | } |
| 92 | current_preference = preference; |
| 93 | current_family = family; |
Qingsi Wang | 5699142 | 2019-02-08 20:53:06 | [diff] [blame] | 94 | const rtc::SocketAddress& candidate_addr = |
| 95 | jsep_candidate->candidate().address(); |
| 96 | port = candidate_addr.port(); |
| 97 | ip = candidate_addr.ipaddr().ToString(); |
| 98 | hostname = candidate_addr.hostname(); |
zhihuang | 38989e5 | 2017-03-21 18:04:53 | [diff] [blame] | 99 | } |
Qingsi Wang | 5699142 | 2019-02-08 20:53:06 | [diff] [blame] | 100 | rtc::SocketAddress connection_addr(ip, port); |
| 101 | if (rtc::IPIsUnspec(connection_addr.ipaddr()) && !hostname.empty()) { |
Qingsi Wang | 3ae59d3 | 2019-07-12 04:53:45 | [diff] [blame] | 102 | // When a hostname candidate becomes the (default) connection address, |
| 103 | // we use the dummy address 0.0.0.0 and port 9 in the c= and the m= lines. |
| 104 | // |
| 105 | // We have observed in deployment that with a FQDN in a c= line, SDP parsing |
| 106 | // could fail in other JSEP implementations. We note that the wildcard |
| 107 | // addresses (0.0.0.0 or ::) with port 9 are given the exception as the |
| 108 | // connection address that will not result in an ICE mismatch |
| 109 | // (draft-ietf-mmusic-ice-sip-sdp). Also, 0.0.0.0 or :: can be used as the |
| 110 | // connection address in the initial offer or answer with trickle ICE |
| 111 | // if the offerer or answerer does not want to include the host IP address |
| 112 | // (draft-ietf-mmusic-trickle-ice-sip), and in particular 0.0.0.0 has been |
| 113 | // widely deployed for this use without outstanding compatibility issues. |
| 114 | // Combining the above considerations, we use 0.0.0.0 with port 9 to |
Artem Titov | 880fa81 | 2021-07-30 20:30:23 | [diff] [blame] | 115 | // populate the c= and the m= lines. See `BuildMediaDescription` in |
Qingsi Wang | 3ae59d3 | 2019-07-12 04:53:45 | [diff] [blame] | 116 | // webrtc_sdp.cc for the SDP generation with |
Artem Titov | cfea218 | 2021-08-09 23:22:31 | [diff] [blame] | 117 | // `media_desc->connection_address()`. |
Qingsi Wang | 3ae59d3 | 2019-07-12 04:53:45 | [diff] [blame] | 118 | connection_addr = rtc::SocketAddress(kDummyAddress, kDummyPort); |
Qingsi Wang | 5699142 | 2019-02-08 20:53:06 | [diff] [blame] | 119 | } |
Steve Anton | b1c1de1 | 2017-12-21 23:14:30 | [diff] [blame] | 120 | media_desc->set_connection_address(connection_addr); |
zhihuang | 38989e5 | 2017-03-21 18:04:53 | [diff] [blame] | 121 | } |
| 122 | |
Steve Anton | 88f2cb9 | 2017-12-05 20:47:32 | [diff] [blame] | 123 | } // namespace |
| 124 | |
Steve Anton | 88f2cb9 | 2017-12-05 20:47:32 | [diff] [blame] | 125 | // TODO(steveanton): Remove this default implementation once Chromium has been |
| 126 | // updated. |
| 127 | SdpType SessionDescriptionInterface::GetType() const { |
Danil Chapovalov | 66cadcc | 2018-06-19 14:47:43 | [diff] [blame] | 128 | absl::optional<SdpType> maybe_type = SdpTypeFromString(type()); |
Steve Anton | 88f2cb9 | 2017-12-05 20:47:32 | [diff] [blame] | 129 | if (maybe_type) { |
| 130 | return *maybe_type; |
| 131 | } else { |
| 132 | RTC_LOG(LS_WARNING) << "Default implementation of " |
| 133 | "SessionDescriptionInterface::GetType does not " |
| 134 | "recognize the result from type(), returning " |
| 135 | "kOffer."; |
| 136 | return SdpType::kOffer; |
| 137 | } |
| 138 | } |
| 139 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 140 | SessionDescriptionInterface* CreateSessionDescription(const std::string& type, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 141 | const std::string& sdp, |
| 142 | SdpParseError* error) { |
Danil Chapovalov | 66cadcc | 2018-06-19 14:47:43 | [diff] [blame] | 143 | absl::optional<SdpType> maybe_type = SdpTypeFromString(type); |
Steve Anton | 88f2cb9 | 2017-12-05 20:47:32 | [diff] [blame] | 144 | if (!maybe_type) { |
| 145 | return nullptr; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 146 | } |
| 147 | |
Steve Anton | 88f2cb9 | 2017-12-05 20:47:32 | [diff] [blame] | 148 | return CreateSessionDescription(*maybe_type, sdp, error).release(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 149 | } |
| 150 | |
Steve Anton | 88f2cb9 | 2017-12-05 20:47:32 | [diff] [blame] | 151 | std::unique_ptr<SessionDescriptionInterface> CreateSessionDescription( |
| 152 | SdpType type, |
| 153 | const std::string& sdp) { |
| 154 | return CreateSessionDescription(type, sdp, nullptr); |
| 155 | } |
| 156 | |
| 157 | std::unique_ptr<SessionDescriptionInterface> CreateSessionDescription( |
| 158 | SdpType type, |
| 159 | const std::string& sdp, |
| 160 | SdpParseError* error_out) { |
Mirko Bonadei | 317a1f0 | 2019-09-17 15:06:18 | [diff] [blame] | 161 | auto jsep_desc = std::make_unique<JsepSessionDescription>(type); |
Eldar Rello | 5ab79e6 | 2019-10-09 15:29:44 | [diff] [blame] | 162 | if (type != SdpType::kRollback) { |
| 163 | if (!SdpDeserialize(sdp, jsep_desc.get(), error_out)) { |
| 164 | return nullptr; |
| 165 | } |
Steve Anton | 88f2cb9 | 2017-12-05 20:47:32 | [diff] [blame] | 166 | } |
| 167 | return std::move(jsep_desc); |
| 168 | } |
| 169 | |
Steve Anton | d9e4a06 | 2018-07-25 01:23:33 | [diff] [blame] | 170 | std::unique_ptr<SessionDescriptionInterface> CreateSessionDescription( |
| 171 | SdpType type, |
| 172 | const std::string& session_id, |
| 173 | const std::string& session_version, |
| 174 | std::unique_ptr<cricket::SessionDescription> description) { |
Mirko Bonadei | 317a1f0 | 2019-09-17 15:06:18 | [diff] [blame] | 175 | auto jsep_description = std::make_unique<JsepSessionDescription>(type); |
Steve Anton | d9e4a06 | 2018-07-25 01:23:33 | [diff] [blame] | 176 | bool initialize_success = jsep_description->Initialize( |
Harald Alvestrand | 4d7160e | 2019-04-12 05:01:29 | [diff] [blame] | 177 | std::move(description), session_id, session_version); |
Steve Anton | d9e4a06 | 2018-07-25 01:23:33 | [diff] [blame] | 178 | RTC_DCHECK(initialize_success); |
| 179 | return std::move(jsep_description); |
| 180 | } |
| 181 | |
Steve Anton | 88f2cb9 | 2017-12-05 20:47:32 | [diff] [blame] | 182 | JsepSessionDescription::JsepSessionDescription(SdpType type) : type_(type) {} |
| 183 | |
| 184 | JsepSessionDescription::JsepSessionDescription(const std::string& type) { |
Danil Chapovalov | 66cadcc | 2018-06-19 14:47:43 | [diff] [blame] | 185 | absl::optional<SdpType> maybe_type = SdpTypeFromString(type); |
Steve Anton | 88f2cb9 | 2017-12-05 20:47:32 | [diff] [blame] | 186 | if (maybe_type) { |
| 187 | type_ = *maybe_type; |
| 188 | } else { |
| 189 | RTC_LOG(LS_WARNING) |
| 190 | << "JsepSessionDescription constructed with invalid type string: " |
| 191 | << type << ". Assuming it is an offer."; |
| 192 | type_ = SdpType::kOffer; |
| 193 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 194 | } |
| 195 | |
Steve Anton | 6fe1fba | 2018-12-11 18:15:23 | [diff] [blame] | 196 | JsepSessionDescription::JsepSessionDescription( |
| 197 | SdpType type, |
| 198 | std::unique_ptr<cricket::SessionDescription> description, |
| 199 | absl::string_view session_id, |
| 200 | absl::string_view session_version) |
| 201 | : description_(std::move(description)), |
| 202 | session_id_(session_id), |
| 203 | session_version_(session_version), |
| 204 | type_(type) { |
| 205 | RTC_DCHECK(description_); |
| 206 | candidate_collection_.resize(number_of_mediasections()); |
| 207 | } |
| 208 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 209 | JsepSessionDescription::~JsepSessionDescription() {} |
| 210 | |
| 211 | bool JsepSessionDescription::Initialize( |
Harald Alvestrand | 4d7160e | 2019-04-12 05:01:29 | [diff] [blame] | 212 | std::unique_ptr<cricket::SessionDescription> description, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 213 | const std::string& session_id, |
| 214 | const std::string& session_version) { |
| 215 | if (!description) |
| 216 | return false; |
| 217 | |
| 218 | session_id_ = session_id; |
| 219 | session_version_ = session_version; |
Harald Alvestrand | 4d7160e | 2019-04-12 05:01:29 | [diff] [blame] | 220 | description_ = std::move(description); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 221 | candidate_collection_.resize(number_of_mediasections()); |
| 222 | return true; |
| 223 | } |
| 224 | |
Harald Alvestrand | c908f1c | 2020-12-17 19:28:29 | [diff] [blame] | 225 | std::unique_ptr<SessionDescriptionInterface> JsepSessionDescription::Clone() |
| 226 | const { |
Harald Alvestrand | 0e7b3a9 | 2020-12-15 15:18:28 | [diff] [blame] | 227 | auto new_description = std::make_unique<JsepSessionDescription>(type_); |
Harald Alvestrand | fc6b871 | 2021-01-05 10:51:08 | [diff] [blame] | 228 | new_description->session_id_ = session_id_; |
| 229 | new_description->session_version_ = session_version_; |
Byoungchan Lee | cb42e10 | 2021-07-15 05:09:40 | [diff] [blame] | 230 | if (description_) { |
| 231 | new_description->description_ = description_->Clone(); |
| 232 | } |
Harald Alvestrand | fc6b871 | 2021-01-05 10:51:08 | [diff] [blame] | 233 | for (const auto& collection : candidate_collection_) { |
| 234 | new_description->candidate_collection_.push_back(collection.Clone()); |
| 235 | } |
Harald Alvestrand | 0e7b3a9 | 2020-12-15 15:18:28 | [diff] [blame] | 236 | return new_description; |
| 237 | } |
| 238 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 239 | bool JsepSessionDescription::AddCandidate( |
| 240 | const IceCandidateInterface* candidate) { |
Guido Urdaneta | 4163317 | 2019-05-23 18:12:29 | [diff] [blame] | 241 | if (!candidate) |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 242 | return false; |
| 243 | size_t mediasection_index = 0; |
| 244 | if (!GetMediasectionIndex(candidate, &mediasection_index)) { |
| 245 | return false; |
| 246 | } |
| 247 | if (mediasection_index >= number_of_mediasections()) |
| 248 | return false; |
jbauch | 083b73f | 2015-07-16 09:46:32 | [diff] [blame] | 249 | const std::string& content_name = |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 250 | description_->contents()[mediasection_index].name; |
| 251 | const cricket::TransportInfo* transport_info = |
| 252 | description_->GetTransportInfoByName(content_name); |
| 253 | if (!transport_info) { |
| 254 | return false; |
| 255 | } |
| 256 | |
Tommi | 0a7fc84 | 2024-01-19 12:11:37 | [diff] [blame] | 257 | Candidate updated_candidate = candidate->candidate(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 258 | if (updated_candidate.username().empty()) { |
| 259 | updated_candidate.set_username(transport_info->description.ice_ufrag); |
| 260 | } |
| 261 | if (updated_candidate.password().empty()) { |
| 262 | updated_candidate.set_password(transport_info->description.ice_pwd); |
| 263 | } |
| 264 | |
kwiberg | d1fe281 | 2016-04-27 13:47:29 | [diff] [blame] | 265 | std::unique_ptr<JsepIceCandidate> updated_candidate_wrapper( |
mallinath@webrtc.org | 67ee6b9 | 2014-02-03 16:57:16 | [diff] [blame] | 266 | new JsepIceCandidate(candidate->sdp_mid(), |
| 267 | static_cast<int>(mediasection_index), |
| 268 | updated_candidate)); |
| 269 | if (!candidate_collection_[mediasection_index].HasCandidate( |
zhihuang | 38989e5 | 2017-03-21 18:04:53 | [diff] [blame] | 270 | updated_candidate_wrapper.get())) { |
mallinath@webrtc.org | 67ee6b9 | 2014-02-03 16:57:16 | [diff] [blame] | 271 | candidate_collection_[mediasection_index].add( |
| 272 | updated_candidate_wrapper.release()); |
zhihuang | 38989e5 | 2017-03-21 18:04:53 | [diff] [blame] | 273 | UpdateConnectionAddress( |
| 274 | candidate_collection_[mediasection_index], |
Steve Anton | b1c1de1 | 2017-12-21 23:14:30 | [diff] [blame] | 275 | description_->contents()[mediasection_index].media_description()); |
zhihuang | 38989e5 | 2017-03-21 18:04:53 | [diff] [blame] | 276 | } |
mallinath@webrtc.org | 67ee6b9 | 2014-02-03 16:57:16 | [diff] [blame] | 277 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 278 | return true; |
| 279 | } |
| 280 | |
Honghai Zhang | 7fb69db | 2016-03-14 18:59:18 | [diff] [blame] | 281 | size_t JsepSessionDescription::RemoveCandidates( |
Tommi | 0a7fc84 | 2024-01-19 12:11:37 | [diff] [blame] | 282 | const std::vector<Candidate>& candidates) { |
Honghai Zhang | 7fb69db | 2016-03-14 18:59:18 | [diff] [blame] | 283 | size_t num_removed = 0; |
| 284 | for (auto& candidate : candidates) { |
| 285 | int mediasection_index = GetMediasectionIndex(candidate); |
| 286 | if (mediasection_index < 0) { |
| 287 | // Not found. |
| 288 | continue; |
| 289 | } |
| 290 | num_removed += candidate_collection_[mediasection_index].remove(candidate); |
zhihuang | 38989e5 | 2017-03-21 18:04:53 | [diff] [blame] | 291 | UpdateConnectionAddress( |
| 292 | candidate_collection_[mediasection_index], |
Steve Anton | b1c1de1 | 2017-12-21 23:14:30 | [diff] [blame] | 293 | description_->contents()[mediasection_index].media_description()); |
Honghai Zhang | 7fb69db | 2016-03-14 18:59:18 | [diff] [blame] | 294 | } |
| 295 | return num_removed; |
| 296 | } |
| 297 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 298 | size_t JsepSessionDescription::number_of_mediasections() const { |
| 299 | if (!description_) |
| 300 | return 0; |
| 301 | return description_->contents().size(); |
| 302 | } |
| 303 | |
| 304 | const IceCandidateCollection* JsepSessionDescription::candidates( |
| 305 | size_t mediasection_index) const { |
| 306 | if (mediasection_index >= candidate_collection_.size()) |
| 307 | return NULL; |
| 308 | return &candidate_collection_[mediasection_index]; |
| 309 | } |
| 310 | |
| 311 | bool JsepSessionDescription::ToString(std::string* out) const { |
deadbeef | 9d3584c | 2016-02-17 01:54:10 | [diff] [blame] | 312 | if (!description_ || !out) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 313 | return false; |
deadbeef | 9d3584c | 2016-02-17 01:54:10 | [diff] [blame] | 314 | } |
Steve Anton | e831b8c | 2018-02-01 20:22:16 | [diff] [blame] | 315 | *out = SdpSerialize(*this); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 316 | return !out->empty(); |
| 317 | } |
| 318 | |
| 319 | bool JsepSessionDescription::GetMediasectionIndex( |
| 320 | const IceCandidateInterface* candidate, |
| 321 | size_t* index) { |
| 322 | if (!candidate || !index) { |
| 323 | return false; |
| 324 | } |
Guido Urdaneta | 4163317 | 2019-05-23 18:12:29 | [diff] [blame] | 325 | |
| 326 | // If the candidate has no valid mline index or sdp_mid, it is impossible |
| 327 | // to find a match. |
| 328 | if (candidate->sdp_mid().empty() && |
| 329 | (candidate->sdp_mline_index() < 0 || |
| 330 | static_cast<size_t>(candidate->sdp_mline_index()) >= |
| 331 | description_->contents().size())) { |
| 332 | return false; |
| 333 | } |
| 334 | |
| 335 | if (candidate->sdp_mline_index() >= 0) |
| 336 | *index = static_cast<size_t>(candidate->sdp_mline_index()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 337 | if (description_ && !candidate->sdp_mid().empty()) { |
| 338 | bool found = false; |
| 339 | // Try to match the sdp_mid with content name. |
| 340 | for (size_t i = 0; i < description_->contents().size(); ++i) { |
| 341 | if (candidate->sdp_mid() == description_->contents().at(i).name) { |
| 342 | *index = i; |
| 343 | found = true; |
| 344 | break; |
| 345 | } |
| 346 | } |
| 347 | if (!found) { |
| 348 | // If the sdp_mid is presented but we can't find a match, we consider |
| 349 | // this as an error. |
| 350 | return false; |
| 351 | } |
| 352 | } |
| 353 | return true; |
| 354 | } |
| 355 | |
Tommi | 0a7fc84 | 2024-01-19 12:11:37 | [diff] [blame] | 356 | int JsepSessionDescription::GetMediasectionIndex(const Candidate& candidate) { |
Honghai Zhang | 7fb69db | 2016-03-14 18:59:18 | [diff] [blame] | 357 | // Find the description with a matching transport name of the candidate. |
| 358 | const std::string& transport_name = candidate.transport_name(); |
| 359 | for (size_t i = 0; i < description_->contents().size(); ++i) { |
| 360 | if (transport_name == description_->contents().at(i).name) { |
| 361 | return static_cast<int>(i); |
| 362 | } |
| 363 | } |
| 364 | return -1; |
| 365 | } |
| 366 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 367 | } // namespace webrtc |