Merge mozilla-central to autoland. a=merge CLOSED TREE
[gecko.git] / third_party / libwebrtc / pc / jsep_session_description.cc
blob7fae4459ec2ba736ce1552d7ec42cec9c16187d7
1 /*
2 * Copyright 2012 The WebRTC project authors. All Rights Reserved.
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.
9 */
11 #include "api/jsep_session_description.h"
13 #include <memory>
14 #include <utility>
16 #include "absl/types/optional.h"
17 #include "p2p/base/p2p_constants.h"
18 #include "p2p/base/port.h"
19 #include "p2p/base/transport_description.h"
20 #include "p2p/base/transport_info.h"
21 #include "pc/media_session.h" // IWYU pragma: keep
22 #include "pc/webrtc_sdp.h"
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"
29 using cricket::Candidate;
30 using cricket::SessionDescription;
32 namespace webrtc {
33 namespace {
35 constexpr char kDummyAddress[] = "0.0.0.0";
36 constexpr int kDummyPort = 9;
38 // Update the connection address for the MediaContentDescription based on the
39 // candidates.
40 void UpdateConnectionAddress(
41 const JsepCandidateCollection& candidate_collection,
42 cricket::MediaContentDescription* media_desc) {
43 int port = kDummyPort;
44 std::string ip = kDummyAddress;
45 std::string hostname;
46 int current_preference = 0; // Start with lowest preference.
47 int current_family = AF_UNSPEC;
48 for (size_t i = 0; i < candidate_collection.count(); ++i) {
49 const IceCandidateInterface* jsep_candidate = candidate_collection.at(i);
50 if (jsep_candidate->candidate().component() !=
51 cricket::ICE_CANDIDATE_COMPONENT_RTP) {
52 continue;
54 // Default destination should be UDP only.
55 if (jsep_candidate->candidate().protocol() != cricket::UDP_PROTOCOL_NAME) {
56 continue;
58 const int preference = jsep_candidate->candidate().type_preference();
59 const int family = jsep_candidate->candidate().address().ipaddr().family();
60 // See if this candidate is more preferable then the current one if it's the
61 // same family. Or if the current family is IPv4 already so we could safely
62 // ignore all IPv6 ones. WebRTC bug 4269.
63 // http://code.google.com/p/webrtc/issues/detail?id=4269
64 if ((preference <= current_preference && current_family == family) ||
65 (current_family == AF_INET && family == AF_INET6)) {
66 continue;
68 current_preference = preference;
69 current_family = family;
70 const rtc::SocketAddress& candidate_addr =
71 jsep_candidate->candidate().address();
72 port = candidate_addr.port();
73 ip = candidate_addr.ipaddr().ToString();
74 hostname = candidate_addr.hostname();
76 rtc::SocketAddress connection_addr(ip, port);
77 if (rtc::IPIsUnspec(connection_addr.ipaddr()) && !hostname.empty()) {
78 // When a hostname candidate becomes the (default) connection address,
79 // we use the dummy address 0.0.0.0 and port 9 in the c= and the m= lines.
81 // We have observed in deployment that with a FQDN in a c= line, SDP parsing
82 // could fail in other JSEP implementations. We note that the wildcard
83 // addresses (0.0.0.0 or ::) with port 9 are given the exception as the
84 // connection address that will not result in an ICE mismatch
85 // (draft-ietf-mmusic-ice-sip-sdp). Also, 0.0.0.0 or :: can be used as the
86 // connection address in the initial offer or answer with trickle ICE
87 // if the offerer or answerer does not want to include the host IP address
88 // (draft-ietf-mmusic-trickle-ice-sip), and in particular 0.0.0.0 has been
89 // widely deployed for this use without outstanding compatibility issues.
90 // Combining the above considerations, we use 0.0.0.0 with port 9 to
91 // populate the c= and the m= lines. See `BuildMediaDescription` in
92 // webrtc_sdp.cc for the SDP generation with
93 // `media_desc->connection_address()`.
94 connection_addr = rtc::SocketAddress(kDummyAddress, kDummyPort);
96 media_desc->set_connection_address(connection_addr);
99 } // namespace
101 // TODO(steveanton): Remove this default implementation once Chromium has been
102 // updated.
103 SdpType SessionDescriptionInterface::GetType() const {
104 absl::optional<SdpType> maybe_type = SdpTypeFromString(type());
105 if (maybe_type) {
106 return *maybe_type;
107 } else {
108 RTC_LOG(LS_WARNING) << "Default implementation of "
109 "SessionDescriptionInterface::GetType does not "
110 "recognize the result from type(), returning "
111 "kOffer.";
112 return SdpType::kOffer;
116 SessionDescriptionInterface* CreateSessionDescription(const std::string& type,
117 const std::string& sdp,
118 SdpParseError* error) {
119 absl::optional<SdpType> maybe_type = SdpTypeFromString(type);
120 if (!maybe_type) {
121 return nullptr;
124 return CreateSessionDescription(*maybe_type, sdp, error).release();
127 std::unique_ptr<SessionDescriptionInterface> CreateSessionDescription(
128 SdpType type,
129 const std::string& sdp) {
130 return CreateSessionDescription(type, sdp, nullptr);
133 std::unique_ptr<SessionDescriptionInterface> CreateSessionDescription(
134 SdpType type,
135 const std::string& sdp,
136 SdpParseError* error_out) {
137 auto jsep_desc = std::make_unique<JsepSessionDescription>(type);
138 if (type != SdpType::kRollback) {
139 if (!SdpDeserialize(sdp, jsep_desc.get(), error_out)) {
140 return nullptr;
143 return std::move(jsep_desc);
146 std::unique_ptr<SessionDescriptionInterface> CreateSessionDescription(
147 SdpType type,
148 const std::string& session_id,
149 const std::string& session_version,
150 std::unique_ptr<cricket::SessionDescription> description) {
151 auto jsep_description = std::make_unique<JsepSessionDescription>(type);
152 bool initialize_success = jsep_description->Initialize(
153 std::move(description), session_id, session_version);
154 RTC_DCHECK(initialize_success);
155 return std::move(jsep_description);
158 JsepSessionDescription::JsepSessionDescription(SdpType type) : type_(type) {}
160 JsepSessionDescription::JsepSessionDescription(const std::string& type) {
161 absl::optional<SdpType> maybe_type = SdpTypeFromString(type);
162 if (maybe_type) {
163 type_ = *maybe_type;
164 } else {
165 RTC_LOG(LS_WARNING)
166 << "JsepSessionDescription constructed with invalid type string: "
167 << type << ". Assuming it is an offer.";
168 type_ = SdpType::kOffer;
172 JsepSessionDescription::JsepSessionDescription(
173 SdpType type,
174 std::unique_ptr<cricket::SessionDescription> description,
175 absl::string_view session_id,
176 absl::string_view session_version)
177 : description_(std::move(description)),
178 session_id_(session_id),
179 session_version_(session_version),
180 type_(type) {
181 RTC_DCHECK(description_);
182 candidate_collection_.resize(number_of_mediasections());
185 JsepSessionDescription::~JsepSessionDescription() {}
187 bool JsepSessionDescription::Initialize(
188 std::unique_ptr<cricket::SessionDescription> description,
189 const std::string& session_id,
190 const std::string& session_version) {
191 if (!description)
192 return false;
194 session_id_ = session_id;
195 session_version_ = session_version;
196 description_ = std::move(description);
197 candidate_collection_.resize(number_of_mediasections());
198 return true;
201 std::unique_ptr<SessionDescriptionInterface> JsepSessionDescription::Clone()
202 const {
203 auto new_description = std::make_unique<JsepSessionDescription>(type_);
204 new_description->session_id_ = session_id_;
205 new_description->session_version_ = session_version_;
206 if (description_) {
207 new_description->description_ = description_->Clone();
209 for (const auto& collection : candidate_collection_) {
210 new_description->candidate_collection_.push_back(collection.Clone());
212 return new_description;
215 bool JsepSessionDescription::AddCandidate(
216 const IceCandidateInterface* candidate) {
217 if (!candidate)
218 return false;
219 size_t mediasection_index = 0;
220 if (!GetMediasectionIndex(candidate, &mediasection_index)) {
221 return false;
223 if (mediasection_index >= number_of_mediasections())
224 return false;
225 const std::string& content_name =
226 description_->contents()[mediasection_index].name;
227 const cricket::TransportInfo* transport_info =
228 description_->GetTransportInfoByName(content_name);
229 if (!transport_info) {
230 return false;
233 Candidate updated_candidate = candidate->candidate();
234 if (updated_candidate.username().empty()) {
235 updated_candidate.set_username(transport_info->description.ice_ufrag);
237 if (updated_candidate.password().empty()) {
238 updated_candidate.set_password(transport_info->description.ice_pwd);
241 std::unique_ptr<JsepIceCandidate> updated_candidate_wrapper(
242 new JsepIceCandidate(candidate->sdp_mid(),
243 static_cast<int>(mediasection_index),
244 updated_candidate));
245 if (!candidate_collection_[mediasection_index].HasCandidate(
246 updated_candidate_wrapper.get())) {
247 candidate_collection_[mediasection_index].add(
248 updated_candidate_wrapper.release());
249 UpdateConnectionAddress(
250 candidate_collection_[mediasection_index],
251 description_->contents()[mediasection_index].media_description());
254 return true;
257 size_t JsepSessionDescription::RemoveCandidates(
258 const std::vector<Candidate>& candidates) {
259 size_t num_removed = 0;
260 for (auto& candidate : candidates) {
261 int mediasection_index = GetMediasectionIndex(candidate);
262 if (mediasection_index < 0) {
263 // Not found.
264 continue;
266 num_removed += candidate_collection_[mediasection_index].remove(candidate);
267 UpdateConnectionAddress(
268 candidate_collection_[mediasection_index],
269 description_->contents()[mediasection_index].media_description());
271 return num_removed;
274 size_t JsepSessionDescription::number_of_mediasections() const {
275 if (!description_)
276 return 0;
277 return description_->contents().size();
280 const IceCandidateCollection* JsepSessionDescription::candidates(
281 size_t mediasection_index) const {
282 if (mediasection_index >= candidate_collection_.size())
283 return NULL;
284 return &candidate_collection_[mediasection_index];
287 bool JsepSessionDescription::ToString(std::string* out) const {
288 if (!description_ || !out) {
289 return false;
291 *out = SdpSerialize(*this);
292 return !out->empty();
295 bool JsepSessionDescription::GetMediasectionIndex(
296 const IceCandidateInterface* candidate,
297 size_t* index) {
298 if (!candidate || !index) {
299 return false;
302 // If the candidate has no valid mline index or sdp_mid, it is impossible
303 // to find a match.
304 if (candidate->sdp_mid().empty() &&
305 (candidate->sdp_mline_index() < 0 ||
306 static_cast<size_t>(candidate->sdp_mline_index()) >=
307 description_->contents().size())) {
308 return false;
311 if (candidate->sdp_mline_index() >= 0)
312 *index = static_cast<size_t>(candidate->sdp_mline_index());
313 if (description_ && !candidate->sdp_mid().empty()) {
314 bool found = false;
315 // Try to match the sdp_mid with content name.
316 for (size_t i = 0; i < description_->contents().size(); ++i) {
317 if (candidate->sdp_mid() == description_->contents().at(i).name) {
318 *index = i;
319 found = true;
320 break;
323 if (!found) {
324 // If the sdp_mid is presented but we can't find a match, we consider
325 // this as an error.
326 return false;
329 return true;
332 int JsepSessionDescription::GetMediasectionIndex(const Candidate& candidate) {
333 // Find the description with a matching transport name of the candidate.
334 const std::string& transport_name = candidate.transport_name();
335 for (size_t i = 0; i < description_->contents().size(); ++i) {
336 if (transport_name == description_->contents().at(i).name) {
337 return static_cast<int>(i);
340 return -1;
343 } // namespace webrtc