blob: 88db17414f8f28ba0542ff03f8fcb470e9d934ca [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()) {
93 connection_addr = rtc::SocketAddress(hostname, port);
94 }
Steve Antonb1c1de12017-12-21 15:14:30 -080095 media_desc->set_connection_address(connection_addr);
zhihuang38989e52017-03-21 11:04:53 -070096}
97
Steve Anton88f2cb92017-12-05 12:47:32 -080098} // namespace
99
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000100const int JsepSessionDescription::kDefaultVideoCodecId = 100;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000101const char JsepSessionDescription::kDefaultVideoCodecName[] = "VP8";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000102
Steve Anton88f2cb92017-12-05 12:47:32 -0800103// TODO(steveanton): Remove this default implementation once Chromium has been
104// updated.
105SdpType SessionDescriptionInterface::GetType() const {
Danil Chapovalov66cadcc2018-06-19 16:47:43 +0200106 absl::optional<SdpType> maybe_type = SdpTypeFromString(type());
Steve Anton88f2cb92017-12-05 12:47:32 -0800107 if (maybe_type) {
108 return *maybe_type;
109 } else {
110 RTC_LOG(LS_WARNING) << "Default implementation of "
111 "SessionDescriptionInterface::GetType does not "
112 "recognize the result from type(), returning "
113 "kOffer.";
114 return SdpType::kOffer;
115 }
116}
117
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000118SessionDescriptionInterface* CreateSessionDescription(const std::string& type,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000119 const std::string& sdp,
120 SdpParseError* error) {
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 nullptr;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000124 }
125
Steve Anton88f2cb92017-12-05 12:47:32 -0800126 return CreateSessionDescription(*maybe_type, sdp, error).release();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000127}
128
Steve Anton88f2cb92017-12-05 12:47:32 -0800129std::unique_ptr<SessionDescriptionInterface> CreateSessionDescription(
130 SdpType type,
131 const std::string& sdp) {
132 return CreateSessionDescription(type, sdp, nullptr);
133}
134
135std::unique_ptr<SessionDescriptionInterface> CreateSessionDescription(
136 SdpType type,
137 const std::string& sdp,
138 SdpParseError* error_out) {
Karl Wiberg918f50c2018-07-05 11:40:33 +0200139 auto jsep_desc = absl::make_unique<JsepSessionDescription>(type);
Steve Anton88f2cb92017-12-05 12:47:32 -0800140 if (!SdpDeserialize(sdp, jsep_desc.get(), error_out)) {
141 return nullptr;
142 }
143 return std::move(jsep_desc);
144}
145
Steve Antond9e4a062018-07-24 18:23:33 -0700146std::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 = absl::make_unique<JsepSessionDescription>(type);
152 bool initialize_success = jsep_description->Initialize(
153 description.release(), session_id, session_version);
154 RTC_DCHECK(initialize_success);
155 return std::move(jsep_description);
156}
157
Steve Anton88f2cb92017-12-05 12:47:32 -0800158JsepSessionDescription::JsepSessionDescription(SdpType type) : type_(type) {}
159
160JsepSessionDescription::JsepSessionDescription(const std::string& type) {
Danil Chapovalov66cadcc2018-06-19 16:47:43 +0200161 absl::optional<SdpType> maybe_type = SdpTypeFromString(type);
Steve Anton88f2cb92017-12-05 12:47:32 -0800162 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;
169 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000170}
171
Steve Anton6fe1fba2018-12-11 10:15:23 -0800172JsepSessionDescription::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());
183}
184
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000185JsepSessionDescription::~JsepSessionDescription() {}
186
187bool JsepSessionDescription::Initialize(
188 cricket::SessionDescription* description,
189 const std::string& session_id,
190 const std::string& session_version) {
191 if (!description)
192 return false;
193
194 session_id_ = session_id;
195 session_version_ = session_version;
196 description_.reset(description);
197 candidate_collection_.resize(number_of_mediasections());
198 return true;
199}
200
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000201bool JsepSessionDescription::AddCandidate(
202 const IceCandidateInterface* candidate) {
203 if (!candidate || candidate->sdp_mline_index() < 0)
204 return false;
205 size_t mediasection_index = 0;
206 if (!GetMediasectionIndex(candidate, &mediasection_index)) {
207 return false;
208 }
209 if (mediasection_index >= number_of_mediasections())
210 return false;
jbauch083b73f2015-07-16 02:46:32 -0700211 const std::string& content_name =
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000212 description_->contents()[mediasection_index].name;
213 const cricket::TransportInfo* transport_info =
214 description_->GetTransportInfoByName(content_name);
215 if (!transport_info) {
216 return false;
217 }
218
219 cricket::Candidate updated_candidate = candidate->candidate();
220 if (updated_candidate.username().empty()) {
221 updated_candidate.set_username(transport_info->description.ice_ufrag);
222 }
223 if (updated_candidate.password().empty()) {
224 updated_candidate.set_password(transport_info->description.ice_pwd);
225 }
226
kwibergd1fe2812016-04-27 06:47:29 -0700227 std::unique_ptr<JsepIceCandidate> updated_candidate_wrapper(
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +0000228 new JsepIceCandidate(candidate->sdp_mid(),
229 static_cast<int>(mediasection_index),
230 updated_candidate));
231 if (!candidate_collection_[mediasection_index].HasCandidate(
zhihuang38989e52017-03-21 11:04:53 -0700232 updated_candidate_wrapper.get())) {
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +0000233 candidate_collection_[mediasection_index].add(
234 updated_candidate_wrapper.release());
zhihuang38989e52017-03-21 11:04:53 -0700235 UpdateConnectionAddress(
236 candidate_collection_[mediasection_index],
Steve Antonb1c1de12017-12-21 15:14:30 -0800237 description_->contents()[mediasection_index].media_description());
zhihuang38989e52017-03-21 11:04:53 -0700238 }
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +0000239
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000240 return true;
241}
242
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700243size_t JsepSessionDescription::RemoveCandidates(
244 const std::vector<cricket::Candidate>& candidates) {
245 size_t num_removed = 0;
246 for (auto& candidate : candidates) {
247 int mediasection_index = GetMediasectionIndex(candidate);
248 if (mediasection_index < 0) {
249 // Not found.
250 continue;
251 }
252 num_removed += candidate_collection_[mediasection_index].remove(candidate);
zhihuang38989e52017-03-21 11:04:53 -0700253 UpdateConnectionAddress(
254 candidate_collection_[mediasection_index],
Steve Antonb1c1de12017-12-21 15:14:30 -0800255 description_->contents()[mediasection_index].media_description());
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700256 }
257 return num_removed;
258}
259
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000260size_t JsepSessionDescription::number_of_mediasections() const {
261 if (!description_)
262 return 0;
263 return description_->contents().size();
264}
265
266const IceCandidateCollection* JsepSessionDescription::candidates(
267 size_t mediasection_index) const {
268 if (mediasection_index >= candidate_collection_.size())
269 return NULL;
270 return &candidate_collection_[mediasection_index];
271}
272
273bool JsepSessionDescription::ToString(std::string* out) const {
deadbeef9d3584c2016-02-16 17:54:10 -0800274 if (!description_ || !out) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000275 return false;
deadbeef9d3584c2016-02-16 17:54:10 -0800276 }
Steve Antone831b8c2018-02-01 12:22:16 -0800277 *out = SdpSerialize(*this);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000278 return !out->empty();
279}
280
281bool JsepSessionDescription::GetMediasectionIndex(
282 const IceCandidateInterface* candidate,
283 size_t* index) {
284 if (!candidate || !index) {
285 return false;
286 }
287 *index = static_cast<size_t>(candidate->sdp_mline_index());
288 if (description_ && !candidate->sdp_mid().empty()) {
289 bool found = false;
290 // Try to match the sdp_mid with content name.
291 for (size_t i = 0; i < description_->contents().size(); ++i) {
292 if (candidate->sdp_mid() == description_->contents().at(i).name) {
293 *index = i;
294 found = true;
295 break;
296 }
297 }
298 if (!found) {
299 // If the sdp_mid is presented but we can't find a match, we consider
300 // this as an error.
301 return false;
302 }
303 }
304 return true;
305}
306
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700307int JsepSessionDescription::GetMediasectionIndex(
308 const cricket::Candidate& candidate) {
309 // Find the description with a matching transport name of the candidate.
310 const std::string& transport_name = candidate.transport_name();
311 for (size_t i = 0; i < description_->contents().size(); ++i) {
312 if (transport_name == description_->contents().at(i).name) {
313 return static_cast<int>(i);
314 }
315 }
316 return -1;
317}
318
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000319} // namespace webrtc