blob: cc544dc5e1032c50fe9ddbb7878f40a228e52ca5 [file] [log] [blame]
jlmiller@webrtc.org5f93d0a2015-01-20 21:36:13 +00001/*
kjellanderb24317b2016-02-10 07:54:43 -08002 * Copyright 2012 The WebRTC project authors. All Rights Reserved.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003 *
kjellanderb24317b2016-02-10 07:54:43 -08004 * 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.org28e20752013-07-10 00:45:36 +00009 */
10
Steve Anton10542f22019-01-11 09:11:00 -080011#include "api/jsep_session_description.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000012
kwibergd1fe2812016-04-27 06:47:29 -070013#include <memory>
14
Steve Anton88f2cb92017-12-05 12:47:32 -080015#include "p2p/base/port.h"
Steve Anton10542f22019-01-11 09:11:00 -080016#include "pc/media_session.h"
17#include "pc/webrtc_sdp.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#include "rtc_base/arraysize.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000019
henrike@webrtc.org28e20752013-07-10 00:45:36 +000020using cricket::SessionDescription;
21
22namespace webrtc {
Steve Anton88f2cb92017-12-05 12:47:32 -080023namespace {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000024
zhihuang38989e52017-03-21 11:04:53 -070025// RFC 5245
26// It is RECOMMENDED that default candidates be chosen based on the
27// likelihood of those candidates to work with the peer that is being
28// contacted. It is RECOMMENDED that relayed > reflexive > host.
Steve Anton88f2cb92017-12-05 12:47:32 -080029constexpr int kPreferenceUnknown = 0;
30constexpr int kPreferenceHost = 1;
31constexpr int kPreferenceReflexive = 2;
32constexpr int kPreferenceRelayed = 3;
zhihuang38989e52017-03-21 11:04:53 -070033
Steve Anton88f2cb92017-12-05 12:47:32 -080034constexpr char kDummyAddress[] = "0.0.0.0";
35constexpr int kDummyPort = 9;
zhihuang38989e52017-03-21 11:04:53 -070036
Steve Anton88f2cb92017-12-05 12:47:32 -080037int GetCandidatePreferenceFromType(const std::string& type) {
zhihuang38989e52017-03-21 11:04:53 -070038 int preference = kPreferenceUnknown;
39 if (type == cricket::LOCAL_PORT_TYPE) {
40 preference = kPreferenceHost;
41 } else if (type == cricket::STUN_PORT_TYPE) {
42 preference = kPreferenceReflexive;
43 } else if (type == cricket::RELAY_PORT_TYPE) {
44 preference = kPreferenceRelayed;
45 } else {
zhihuang15238652017-03-23 10:32:12 -070046 preference = kPreferenceUnknown;
zhihuang38989e52017-03-21 11:04:53 -070047 }
48 return preference;
49}
50
51// Update the connection address for the MediaContentDescription based on the
52// candidates.
Steve Anton88f2cb92017-12-05 12:47:32 -080053void UpdateConnectionAddress(
zhihuang38989e52017-03-21 11:04:53 -070054 const JsepCandidateCollection& candidate_collection,
Steve Antonb1c1de12017-12-21 15:14:30 -080055 cricket::MediaContentDescription* media_desc) {
zhihuang38989e52017-03-21 11:04:53 -070056 int port = kDummyPort;
57 std::string ip = kDummyAddress;
Qingsi Wang56991422019-02-08 12:53:06 -080058 std::string hostname;
zhihuang38989e52017-03-21 11:04:53 -070059 int current_preference = kPreferenceUnknown;
60 int current_family = AF_UNSPEC;
61 for (size_t i = 0; i < candidate_collection.count(); ++i) {
62 const IceCandidateInterface* jsep_candidate = candidate_collection.at(i);
63 if (jsep_candidate->candidate().component() !=
64 cricket::ICE_CANDIDATE_COMPONENT_RTP) {
65 continue;
66 }
67 // Default destination should be UDP only.
68 if (jsep_candidate->candidate().protocol() != cricket::UDP_PROTOCOL_NAME) {
69 continue;
70 }
71 const int preference =
72 GetCandidatePreferenceFromType(jsep_candidate->candidate().type());
73 const int family = jsep_candidate->candidate().address().ipaddr().family();
74 // See if this candidate is more preferable then the current one if it's the
75 // same family. Or if the current family is IPv4 already so we could safely
76 // ignore all IPv6 ones. WebRTC bug 4269.
77 // http://code.google.com/p/webrtc/issues/detail?id=4269
78 if ((preference <= current_preference && current_family == family) ||
79 (current_family == AF_INET && family == AF_INET6)) {
80 continue;
81 }
82 current_preference = preference;
83 current_family = family;
Qingsi Wang56991422019-02-08 12:53:06 -080084 const rtc::SocketAddress& candidate_addr =
85 jsep_candidate->candidate().address();
86 port = candidate_addr.port();
87 ip = candidate_addr.ipaddr().ToString();
88 hostname = candidate_addr.hostname();
zhihuang38989e52017-03-21 11:04:53 -070089 }
Qingsi Wang56991422019-02-08 12:53:06 -080090 rtc::SocketAddress connection_addr(ip, port);
91 if (rtc::IPIsUnspec(connection_addr.ipaddr()) && !hostname.empty()) {
Qingsi Wang3ae59d32019-07-11 21:53:45 -070092 // When a hostname candidate becomes the (default) connection address,
93 // we use the dummy address 0.0.0.0 and port 9 in the c= and the m= lines.
94 //
95 // We have observed in deployment that with a FQDN in a c= line, SDP parsing
96 // could fail in other JSEP implementations. We note that the wildcard
97 // addresses (0.0.0.0 or ::) with port 9 are given the exception as the
98 // connection address that will not result in an ICE mismatch
99 // (draft-ietf-mmusic-ice-sip-sdp). Also, 0.0.0.0 or :: can be used as the
100 // connection address in the initial offer or answer with trickle ICE
101 // if the offerer or answerer does not want to include the host IP address
102 // (draft-ietf-mmusic-trickle-ice-sip), and in particular 0.0.0.0 has been
103 // widely deployed for this use without outstanding compatibility issues.
104 // Combining the above considerations, we use 0.0.0.0 with port 9 to
105 // populate the c= and the m= lines. See |BuildMediaDescription| in
106 // webrtc_sdp.cc for the SDP generation with
107 // |media_desc->connection_address()|.
108 connection_addr = rtc::SocketAddress(kDummyAddress, kDummyPort);
Qingsi Wang56991422019-02-08 12:53:06 -0800109 }
Steve Antonb1c1de12017-12-21 15:14:30 -0800110 media_desc->set_connection_address(connection_addr);
zhihuang38989e52017-03-21 11:04:53 -0700111}
112
Steve Anton88f2cb92017-12-05 12:47:32 -0800113} // namespace
114
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000115const int JsepSessionDescription::kDefaultVideoCodecId = 100;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000116const char JsepSessionDescription::kDefaultVideoCodecName[] = "VP8";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000117
Steve Anton88f2cb92017-12-05 12:47:32 -0800118// TODO(steveanton): Remove this default implementation once Chromium has been
119// updated.
120SdpType SessionDescriptionInterface::GetType() const {
Danil Chapovalov66cadcc2018-06-19 16:47:43 +0200121 absl::optional<SdpType> maybe_type = SdpTypeFromString(type());
Steve Anton88f2cb92017-12-05 12:47:32 -0800122 if (maybe_type) {
123 return *maybe_type;
124 } else {
125 RTC_LOG(LS_WARNING) << "Default implementation of "
126 "SessionDescriptionInterface::GetType does not "
127 "recognize the result from type(), returning "
128 "kOffer.";
129 return SdpType::kOffer;
130 }
131}
132
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000133SessionDescriptionInterface* CreateSessionDescription(const std::string& type,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000134 const std::string& sdp,
135 SdpParseError* error) {
Danil Chapovalov66cadcc2018-06-19 16:47:43 +0200136 absl::optional<SdpType> maybe_type = SdpTypeFromString(type);
Steve Anton88f2cb92017-12-05 12:47:32 -0800137 if (!maybe_type) {
138 return nullptr;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000139 }
140
Steve Anton88f2cb92017-12-05 12:47:32 -0800141 return CreateSessionDescription(*maybe_type, sdp, error).release();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000142}
143
Steve Anton88f2cb92017-12-05 12:47:32 -0800144std::unique_ptr<SessionDescriptionInterface> CreateSessionDescription(
145 SdpType type,
146 const std::string& sdp) {
147 return CreateSessionDescription(type, sdp, nullptr);
148}
149
150std::unique_ptr<SessionDescriptionInterface> CreateSessionDescription(
151 SdpType type,
152 const std::string& sdp,
153 SdpParseError* error_out) {
Mirko Bonadei317a1f02019-09-17 17:06:18 +0200154 auto jsep_desc = std::make_unique<JsepSessionDescription>(type);
Steve Anton88f2cb92017-12-05 12:47:32 -0800155 if (!SdpDeserialize(sdp, jsep_desc.get(), error_out)) {
156 return nullptr;
157 }
158 return std::move(jsep_desc);
159}
160
Steve Antond9e4a062018-07-24 18:23:33 -0700161std::unique_ptr<SessionDescriptionInterface> CreateSessionDescription(
162 SdpType type,
163 const std::string& session_id,
164 const std::string& session_version,
165 std::unique_ptr<cricket::SessionDescription> description) {
Mirko Bonadei317a1f02019-09-17 17:06:18 +0200166 auto jsep_description = std::make_unique<JsepSessionDescription>(type);
Steve Antond9e4a062018-07-24 18:23:33 -0700167 bool initialize_success = jsep_description->Initialize(
Harald Alvestrand4d7160e2019-04-12 07:01:29 +0200168 std::move(description), session_id, session_version);
Steve Antond9e4a062018-07-24 18:23:33 -0700169 RTC_DCHECK(initialize_success);
170 return std::move(jsep_description);
171}
172
Steve Anton88f2cb92017-12-05 12:47:32 -0800173JsepSessionDescription::JsepSessionDescription(SdpType type) : type_(type) {}
174
175JsepSessionDescription::JsepSessionDescription(const std::string& type) {
Danil Chapovalov66cadcc2018-06-19 16:47:43 +0200176 absl::optional<SdpType> maybe_type = SdpTypeFromString(type);
Steve Anton88f2cb92017-12-05 12:47:32 -0800177 if (maybe_type) {
178 type_ = *maybe_type;
179 } else {
180 RTC_LOG(LS_WARNING)
181 << "JsepSessionDescription constructed with invalid type string: "
182 << type << ". Assuming it is an offer.";
183 type_ = SdpType::kOffer;
184 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000185}
186
Steve Anton6fe1fba2018-12-11 10:15:23 -0800187JsepSessionDescription::JsepSessionDescription(
188 SdpType type,
189 std::unique_ptr<cricket::SessionDescription> description,
190 absl::string_view session_id,
191 absl::string_view session_version)
192 : description_(std::move(description)),
193 session_id_(session_id),
194 session_version_(session_version),
195 type_(type) {
196 RTC_DCHECK(description_);
197 candidate_collection_.resize(number_of_mediasections());
198}
199
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000200JsepSessionDescription::~JsepSessionDescription() {}
201
202bool JsepSessionDescription::Initialize(
Harald Alvestrand4d7160e2019-04-12 07:01:29 +0200203 std::unique_ptr<cricket::SessionDescription> description,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000204 const std::string& session_id,
205 const std::string& session_version) {
206 if (!description)
207 return false;
208
209 session_id_ = session_id;
210 session_version_ = session_version;
Harald Alvestrand4d7160e2019-04-12 07:01:29 +0200211 description_ = std::move(description);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000212 candidate_collection_.resize(number_of_mediasections());
213 return true;
214}
215
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000216bool JsepSessionDescription::AddCandidate(
217 const IceCandidateInterface* candidate) {
Guido Urdaneta41633172019-05-23 20:12:29 +0200218 if (!candidate)
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000219 return false;
220 size_t mediasection_index = 0;
221 if (!GetMediasectionIndex(candidate, &mediasection_index)) {
222 return false;
223 }
224 if (mediasection_index >= number_of_mediasections())
225 return false;
jbauch083b73f2015-07-16 02:46:32 -0700226 const std::string& content_name =
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000227 description_->contents()[mediasection_index].name;
228 const cricket::TransportInfo* transport_info =
229 description_->GetTransportInfoByName(content_name);
230 if (!transport_info) {
231 return false;
232 }
233
234 cricket::Candidate updated_candidate = candidate->candidate();
235 if (updated_candidate.username().empty()) {
236 updated_candidate.set_username(transport_info->description.ice_ufrag);
237 }
238 if (updated_candidate.password().empty()) {
239 updated_candidate.set_password(transport_info->description.ice_pwd);
240 }
241
kwibergd1fe2812016-04-27 06:47:29 -0700242 std::unique_ptr<JsepIceCandidate> updated_candidate_wrapper(
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +0000243 new JsepIceCandidate(candidate->sdp_mid(),
244 static_cast<int>(mediasection_index),
245 updated_candidate));
246 if (!candidate_collection_[mediasection_index].HasCandidate(
zhihuang38989e52017-03-21 11:04:53 -0700247 updated_candidate_wrapper.get())) {
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +0000248 candidate_collection_[mediasection_index].add(
249 updated_candidate_wrapper.release());
zhihuang38989e52017-03-21 11:04:53 -0700250 UpdateConnectionAddress(
251 candidate_collection_[mediasection_index],
Steve Antonb1c1de12017-12-21 15:14:30 -0800252 description_->contents()[mediasection_index].media_description());
zhihuang38989e52017-03-21 11:04:53 -0700253 }
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +0000254
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000255 return true;
256}
257
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700258size_t JsepSessionDescription::RemoveCandidates(
259 const std::vector<cricket::Candidate>& candidates) {
260 size_t num_removed = 0;
261 for (auto& candidate : candidates) {
262 int mediasection_index = GetMediasectionIndex(candidate);
263 if (mediasection_index < 0) {
264 // Not found.
265 continue;
266 }
267 num_removed += candidate_collection_[mediasection_index].remove(candidate);
zhihuang38989e52017-03-21 11:04:53 -0700268 UpdateConnectionAddress(
269 candidate_collection_[mediasection_index],
Steve Antonb1c1de12017-12-21 15:14:30 -0800270 description_->contents()[mediasection_index].media_description());
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700271 }
272 return num_removed;
273}
274
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000275size_t JsepSessionDescription::number_of_mediasections() const {
276 if (!description_)
277 return 0;
278 return description_->contents().size();
279}
280
281const IceCandidateCollection* JsepSessionDescription::candidates(
282 size_t mediasection_index) const {
283 if (mediasection_index >= candidate_collection_.size())
284 return NULL;
285 return &candidate_collection_[mediasection_index];
286}
287
288bool JsepSessionDescription::ToString(std::string* out) const {
deadbeef9d3584c2016-02-16 17:54:10 -0800289 if (!description_ || !out) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000290 return false;
deadbeef9d3584c2016-02-16 17:54:10 -0800291 }
Steve Antone831b8c2018-02-01 12:22:16 -0800292 *out = SdpSerialize(*this);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000293 return !out->empty();
294}
295
296bool JsepSessionDescription::GetMediasectionIndex(
297 const IceCandidateInterface* candidate,
298 size_t* index) {
299 if (!candidate || !index) {
300 return false;
301 }
Guido Urdaneta41633172019-05-23 20:12:29 +0200302
303 // If the candidate has no valid mline index or sdp_mid, it is impossible
304 // to find a match.
305 if (candidate->sdp_mid().empty() &&
306 (candidate->sdp_mline_index() < 0 ||
307 static_cast<size_t>(candidate->sdp_mline_index()) >=
308 description_->contents().size())) {
309 return false;
310 }
311
312 if (candidate->sdp_mline_index() >= 0)
313 *index = static_cast<size_t>(candidate->sdp_mline_index());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000314 if (description_ && !candidate->sdp_mid().empty()) {
315 bool found = false;
316 // Try to match the sdp_mid with content name.
317 for (size_t i = 0; i < description_->contents().size(); ++i) {
318 if (candidate->sdp_mid() == description_->contents().at(i).name) {
319 *index = i;
320 found = true;
321 break;
322 }
323 }
324 if (!found) {
325 // If the sdp_mid is presented but we can't find a match, we consider
326 // this as an error.
327 return false;
328 }
329 }
330 return true;
331}
332
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700333int JsepSessionDescription::GetMediasectionIndex(
334 const cricket::Candidate& candidate) {
335 // Find the description with a matching transport name of the candidate.
336 const std::string& transport_name = candidate.transport_name();
337 for (size_t i = 0; i < description_->contents().size(); ++i) {
338 if (transport_name == description_->contents().at(i).name) {
339 return static_cast<int>(i);
340 }
341 }
342 return -1;
343}
344
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000345} // namespace webrtc