blob: 4c0453198ddb6d4c6d1cd0741b93a0b97f5a9756 [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
Karl Wiberg918f50c2018-07-05 11:40:33 +020015#include "absl/memory/memory.h"
Steve Anton88f2cb92017-12-05 12:47:32 -080016#include "p2p/base/port.h"
Steve Anton10542f22019-01-11 09:11:00 -080017#include "pc/media_session.h"
18#include "pc/webrtc_sdp.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020019#include "rtc_base/arraysize.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000020
henrike@webrtc.org28e20752013-07-10 00:45:36 +000021using cricket::SessionDescription;
22
23namespace webrtc {
Steve Anton88f2cb92017-12-05 12:47:32 -080024namespace {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000025
zhihuang38989e52017-03-21 11:04:53 -070026// RFC 5245
27// It is RECOMMENDED that default candidates be chosen based on the
28// likelihood of those candidates to work with the peer that is being
29// contacted. It is RECOMMENDED that relayed > reflexive > host.
Steve Anton88f2cb92017-12-05 12:47:32 -080030constexpr int kPreferenceUnknown = 0;
31constexpr int kPreferenceHost = 1;
32constexpr int kPreferenceReflexive = 2;
33constexpr int kPreferenceRelayed = 3;
zhihuang38989e52017-03-21 11:04:53 -070034
Steve Anton88f2cb92017-12-05 12:47:32 -080035constexpr char kDummyAddress[] = "0.0.0.0";
36constexpr int kDummyPort = 9;
zhihuang38989e52017-03-21 11:04:53 -070037
Steve Anton88f2cb92017-12-05 12:47:32 -080038int GetCandidatePreferenceFromType(const std::string& type) {
zhihuang38989e52017-03-21 11:04:53 -070039 int preference = kPreferenceUnknown;
40 if (type == cricket::LOCAL_PORT_TYPE) {
41 preference = kPreferenceHost;
42 } else if (type == cricket::STUN_PORT_TYPE) {
43 preference = kPreferenceReflexive;
44 } else if (type == cricket::RELAY_PORT_TYPE) {
45 preference = kPreferenceRelayed;
46 } else {
zhihuang15238652017-03-23 10:32:12 -070047 preference = kPreferenceUnknown;
zhihuang38989e52017-03-21 11:04:53 -070048 }
49 return preference;
50}
51
52// Update the connection address for the MediaContentDescription based on the
53// candidates.
Steve Anton88f2cb92017-12-05 12:47:32 -080054void UpdateConnectionAddress(
zhihuang38989e52017-03-21 11:04:53 -070055 const JsepCandidateCollection& candidate_collection,
Steve Antonb1c1de12017-12-21 15:14:30 -080056 cricket::MediaContentDescription* media_desc) {
zhihuang38989e52017-03-21 11:04:53 -070057 int port = kDummyPort;
58 std::string ip = kDummyAddress;
Qingsi Wang56991422019-02-08 12:53:06 -080059 std::string hostname;
zhihuang38989e52017-03-21 11:04:53 -070060 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;
Qingsi Wang56991422019-02-08 12:53:06 -080085 const rtc::SocketAddress& candidate_addr =
86 jsep_candidate->candidate().address();
87 port = candidate_addr.port();
88 ip = candidate_addr.ipaddr().ToString();
89 hostname = candidate_addr.hostname();
zhihuang38989e52017-03-21 11:04:53 -070090 }
Qingsi Wang56991422019-02-08 12:53:06 -080091 rtc::SocketAddress connection_addr(ip, port);
92 if (rtc::IPIsUnspec(connection_addr.ipaddr()) && !hostname.empty()) {
Qingsi Wang3ae59d32019-07-11 21:53:45 -070093 // When a hostname candidate becomes the (default) connection address,
94 // we use the dummy address 0.0.0.0 and port 9 in the c= and the m= lines.
95 //
96 // We have observed in deployment that with a FQDN in a c= line, SDP parsing
97 // could fail in other JSEP implementations. We note that the wildcard
98 // addresses (0.0.0.0 or ::) with port 9 are given the exception as the
99 // connection address that will not result in an ICE mismatch
100 // (draft-ietf-mmusic-ice-sip-sdp). Also, 0.0.0.0 or :: can be used as the
101 // connection address in the initial offer or answer with trickle ICE
102 // if the offerer or answerer does not want to include the host IP address
103 // (draft-ietf-mmusic-trickle-ice-sip), and in particular 0.0.0.0 has been
104 // widely deployed for this use without outstanding compatibility issues.
105 // Combining the above considerations, we use 0.0.0.0 with port 9 to
106 // populate the c= and the m= lines. See |BuildMediaDescription| in
107 // webrtc_sdp.cc for the SDP generation with
108 // |media_desc->connection_address()|.
109 connection_addr = rtc::SocketAddress(kDummyAddress, kDummyPort);
Qingsi Wang56991422019-02-08 12:53:06 -0800110 }
Steve Antonb1c1de12017-12-21 15:14:30 -0800111 media_desc->set_connection_address(connection_addr);
zhihuang38989e52017-03-21 11:04:53 -0700112}
113
Steve Anton88f2cb92017-12-05 12:47:32 -0800114} // namespace
115
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000116const int JsepSessionDescription::kDefaultVideoCodecId = 100;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000117const char JsepSessionDescription::kDefaultVideoCodecName[] = "VP8";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000118
Steve Anton88f2cb92017-12-05 12:47:32 -0800119// TODO(steveanton): Remove this default implementation once Chromium has been
120// updated.
121SdpType SessionDescriptionInterface::GetType() const {
Danil Chapovalov66cadcc2018-06-19 16:47:43 +0200122 absl::optional<SdpType> maybe_type = SdpTypeFromString(type());
Steve Anton88f2cb92017-12-05 12:47:32 -0800123 if (maybe_type) {
124 return *maybe_type;
125 } else {
126 RTC_LOG(LS_WARNING) << "Default implementation of "
127 "SessionDescriptionInterface::GetType does not "
128 "recognize the result from type(), returning "
129 "kOffer.";
130 return SdpType::kOffer;
131 }
132}
133
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000134SessionDescriptionInterface* CreateSessionDescription(const std::string& type,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000135 const std::string& sdp,
136 SdpParseError* error) {
Danil Chapovalov66cadcc2018-06-19 16:47:43 +0200137 absl::optional<SdpType> maybe_type = SdpTypeFromString(type);
Steve Anton88f2cb92017-12-05 12:47:32 -0800138 if (!maybe_type) {
139 return nullptr;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000140 }
141
Steve Anton88f2cb92017-12-05 12:47:32 -0800142 return CreateSessionDescription(*maybe_type, sdp, error).release();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000143}
144
Steve Anton88f2cb92017-12-05 12:47:32 -0800145std::unique_ptr<SessionDescriptionInterface> CreateSessionDescription(
146 SdpType type,
147 const std::string& sdp) {
148 return CreateSessionDescription(type, sdp, nullptr);
149}
150
151std::unique_ptr<SessionDescriptionInterface> CreateSessionDescription(
152 SdpType type,
153 const std::string& sdp,
154 SdpParseError* error_out) {
Karl Wiberg918f50c2018-07-05 11:40:33 +0200155 auto jsep_desc = absl::make_unique<JsepSessionDescription>(type);
Steve Anton88f2cb92017-12-05 12:47:32 -0800156 if (!SdpDeserialize(sdp, jsep_desc.get(), error_out)) {
157 return nullptr;
158 }
159 return std::move(jsep_desc);
160}
161
Steve Antond9e4a062018-07-24 18:23:33 -0700162std::unique_ptr<SessionDescriptionInterface> CreateSessionDescription(
163 SdpType type,
164 const std::string& session_id,
165 const std::string& session_version,
166 std::unique_ptr<cricket::SessionDescription> description) {
167 auto jsep_description = absl::make_unique<JsepSessionDescription>(type);
168 bool initialize_success = jsep_description->Initialize(
Harald Alvestrand4d7160e2019-04-12 07:01:29 +0200169 std::move(description), session_id, session_version);
Steve Antond9e4a062018-07-24 18:23:33 -0700170 RTC_DCHECK(initialize_success);
171 return std::move(jsep_description);
172}
173
Steve Anton88f2cb92017-12-05 12:47:32 -0800174JsepSessionDescription::JsepSessionDescription(SdpType type) : type_(type) {}
175
176JsepSessionDescription::JsepSessionDescription(const std::string& type) {
Danil Chapovalov66cadcc2018-06-19 16:47:43 +0200177 absl::optional<SdpType> maybe_type = SdpTypeFromString(type);
Steve Anton88f2cb92017-12-05 12:47:32 -0800178 if (maybe_type) {
179 type_ = *maybe_type;
180 } else {
181 RTC_LOG(LS_WARNING)
182 << "JsepSessionDescription constructed with invalid type string: "
183 << type << ". Assuming it is an offer.";
184 type_ = SdpType::kOffer;
185 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000186}
187
Steve Anton6fe1fba2018-12-11 10:15:23 -0800188JsepSessionDescription::JsepSessionDescription(
189 SdpType type,
190 std::unique_ptr<cricket::SessionDescription> description,
191 absl::string_view session_id,
192 absl::string_view session_version)
193 : description_(std::move(description)),
194 session_id_(session_id),
195 session_version_(session_version),
196 type_(type) {
197 RTC_DCHECK(description_);
198 candidate_collection_.resize(number_of_mediasections());
199}
200
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000201JsepSessionDescription::~JsepSessionDescription() {}
202
203bool JsepSessionDescription::Initialize(
Harald Alvestrand4d7160e2019-04-12 07:01:29 +0200204 std::unique_ptr<cricket::SessionDescription> description,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000205 const std::string& session_id,
206 const std::string& session_version) {
207 if (!description)
208 return false;
209
210 session_id_ = session_id;
211 session_version_ = session_version;
Harald Alvestrand4d7160e2019-04-12 07:01:29 +0200212 description_ = std::move(description);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000213 candidate_collection_.resize(number_of_mediasections());
214 return true;
215}
216
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000217bool JsepSessionDescription::AddCandidate(
218 const IceCandidateInterface* candidate) {
Guido Urdaneta41633172019-05-23 20:12:29 +0200219 if (!candidate)
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000220 return false;
221 size_t mediasection_index = 0;
222 if (!GetMediasectionIndex(candidate, &mediasection_index)) {
223 return false;
224 }
225 if (mediasection_index >= number_of_mediasections())
226 return false;
jbauch083b73f2015-07-16 02:46:32 -0700227 const std::string& content_name =
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000228 description_->contents()[mediasection_index].name;
229 const cricket::TransportInfo* transport_info =
230 description_->GetTransportInfoByName(content_name);
231 if (!transport_info) {
232 return false;
233 }
234
235 cricket::Candidate updated_candidate = candidate->candidate();
236 if (updated_candidate.username().empty()) {
237 updated_candidate.set_username(transport_info->description.ice_ufrag);
238 }
239 if (updated_candidate.password().empty()) {
240 updated_candidate.set_password(transport_info->description.ice_pwd);
241 }
242
kwibergd1fe2812016-04-27 06:47:29 -0700243 std::unique_ptr<JsepIceCandidate> updated_candidate_wrapper(
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +0000244 new JsepIceCandidate(candidate->sdp_mid(),
245 static_cast<int>(mediasection_index),
246 updated_candidate));
247 if (!candidate_collection_[mediasection_index].HasCandidate(
zhihuang38989e52017-03-21 11:04:53 -0700248 updated_candidate_wrapper.get())) {
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +0000249 candidate_collection_[mediasection_index].add(
250 updated_candidate_wrapper.release());
zhihuang38989e52017-03-21 11:04:53 -0700251 UpdateConnectionAddress(
252 candidate_collection_[mediasection_index],
Steve Antonb1c1de12017-12-21 15:14:30 -0800253 description_->contents()[mediasection_index].media_description());
zhihuang38989e52017-03-21 11:04:53 -0700254 }
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +0000255
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000256 return true;
257}
258
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700259size_t JsepSessionDescription::RemoveCandidates(
260 const std::vector<cricket::Candidate>& candidates) {
261 size_t num_removed = 0;
262 for (auto& candidate : candidates) {
263 int mediasection_index = GetMediasectionIndex(candidate);
264 if (mediasection_index < 0) {
265 // Not found.
266 continue;
267 }
268 num_removed += candidate_collection_[mediasection_index].remove(candidate);
zhihuang38989e52017-03-21 11:04:53 -0700269 UpdateConnectionAddress(
270 candidate_collection_[mediasection_index],
Steve Antonb1c1de12017-12-21 15:14:30 -0800271 description_->contents()[mediasection_index].media_description());
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700272 }
273 return num_removed;
274}
275
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000276size_t JsepSessionDescription::number_of_mediasections() const {
277 if (!description_)
278 return 0;
279 return description_->contents().size();
280}
281
282const IceCandidateCollection* JsepSessionDescription::candidates(
283 size_t mediasection_index) const {
284 if (mediasection_index >= candidate_collection_.size())
285 return NULL;
286 return &candidate_collection_[mediasection_index];
287}
288
289bool JsepSessionDescription::ToString(std::string* out) const {
deadbeef9d3584c2016-02-16 17:54:10 -0800290 if (!description_ || !out) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000291 return false;
deadbeef9d3584c2016-02-16 17:54:10 -0800292 }
Steve Antone831b8c2018-02-01 12:22:16 -0800293 *out = SdpSerialize(*this);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000294 return !out->empty();
295}
296
297bool JsepSessionDescription::GetMediasectionIndex(
298 const IceCandidateInterface* candidate,
299 size_t* index) {
300 if (!candidate || !index) {
301 return false;
302 }
Guido Urdaneta41633172019-05-23 20:12:29 +0200303
304 // If the candidate has no valid mline index or sdp_mid, it is impossible
305 // to find a match.
306 if (candidate->sdp_mid().empty() &&
307 (candidate->sdp_mline_index() < 0 ||
308 static_cast<size_t>(candidate->sdp_mline_index()) >=
309 description_->contents().size())) {
310 return false;
311 }
312
313 if (candidate->sdp_mline_index() >= 0)
314 *index = static_cast<size_t>(candidate->sdp_mline_index());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000315 if (description_ && !candidate->sdp_mid().empty()) {
316 bool found = false;
317 // Try to match the sdp_mid with content name.
318 for (size_t i = 0; i < description_->contents().size(); ++i) {
319 if (candidate->sdp_mid() == description_->contents().at(i).name) {
320 *index = i;
321 found = true;
322 break;
323 }
324 }
325 if (!found) {
326 // If the sdp_mid is presented but we can't find a match, we consider
327 // this as an error.
328 return false;
329 }
330 }
331 return true;
332}
333
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700334int JsepSessionDescription::GetMediasectionIndex(
335 const cricket::Candidate& candidate) {
336 // Find the description with a matching transport name of the candidate.
337 const std::string& transport_name = candidate.transport_name();
338 for (size_t i = 0; i < description_->contents().size(); ++i) {
339 if (transport_name == description_->contents().at(i).name) {
340 return static_cast<int>(i);
341 }
342 }
343 return -1;
344}
345
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000346} // namespace webrtc