blob: 91840e36547a6a20a1f6f950f998bef9988ef0d0 [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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "api/jsepsessiondescription.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"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020016#include "pc/mediasession.h"
17#include "pc/webrtcsdp.h"
18#include "rtc_base/arraysize.h"
Steve Anton88f2cb92017-12-05 12:47:32 -080019#include "rtc_base/ptr_util.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020020#include "rtc_base/stringencode.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000021
henrike@webrtc.org28e20752013-07-10 00:45:36 +000022using cricket::SessionDescription;
23
24namespace webrtc {
Steve Anton88f2cb92017-12-05 12:47:32 -080025namespace {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000026
zhihuang38989e52017-03-21 11:04:53 -070027// RFC 5245
28// It is RECOMMENDED that default candidates be chosen based on the
29// likelihood of those candidates to work with the peer that is being
30// contacted. It is RECOMMENDED that relayed > reflexive > host.
Steve Anton88f2cb92017-12-05 12:47:32 -080031constexpr int kPreferenceUnknown = 0;
32constexpr int kPreferenceHost = 1;
33constexpr int kPreferenceReflexive = 2;
34constexpr int kPreferenceRelayed = 3;
zhihuang38989e52017-03-21 11:04:53 -070035
Steve Anton88f2cb92017-12-05 12:47:32 -080036constexpr char kDummyAddress[] = "0.0.0.0";
37constexpr int kDummyPort = 9;
zhihuang38989e52017-03-21 11:04:53 -070038
Steve Anton88f2cb92017-12-05 12:47:32 -080039int GetCandidatePreferenceFromType(const std::string& type) {
zhihuang38989e52017-03-21 11:04:53 -070040 int preference = kPreferenceUnknown;
41 if (type == cricket::LOCAL_PORT_TYPE) {
42 preference = kPreferenceHost;
43 } else if (type == cricket::STUN_PORT_TYPE) {
44 preference = kPreferenceReflexive;
45 } else if (type == cricket::RELAY_PORT_TYPE) {
46 preference = kPreferenceRelayed;
47 } else {
zhihuang15238652017-03-23 10:32:12 -070048 preference = kPreferenceUnknown;
zhihuang38989e52017-03-21 11:04:53 -070049 }
50 return preference;
51}
52
53// Update the connection address for the MediaContentDescription based on the
54// candidates.
Steve Anton88f2cb92017-12-05 12:47:32 -080055void UpdateConnectionAddress(
zhihuang38989e52017-03-21 11:04:53 -070056 const JsepCandidateCollection& candidate_collection,
Steve Antonb1c1de12017-12-21 15:14:30 -080057 cricket::MediaContentDescription* media_desc) {
zhihuang38989e52017-03-21 11:04:53 -070058 int port = kDummyPort;
59 std::string ip = kDummyAddress;
60 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;
85 port = jsep_candidate->candidate().address().port();
86 ip = jsep_candidate->candidate().address().ipaddr().ToString();
87 }
88 rtc::SocketAddress connection_addr;
89 connection_addr.SetIP(ip);
90 connection_addr.SetPort(port);
Steve Antonb1c1de12017-12-21 15:14:30 -080091 media_desc->set_connection_address(connection_addr);
zhihuang38989e52017-03-21 11:04:53 -070092}
93
Steve Anton88f2cb92017-12-05 12:47:32 -080094} // namespace
95
henrike@webrtc.org28e20752013-07-10 00:45:36 +000096const char SessionDescriptionInterface::kOffer[] = "offer";
97const char SessionDescriptionInterface::kPrAnswer[] = "pranswer";
98const char SessionDescriptionInterface::kAnswer[] = "answer";
99
100const 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 -0800103const char* SdpTypeToString(SdpType type) {
104 switch (type) {
105 case SdpType::kOffer:
106 return SessionDescriptionInterface::kOffer;
107 case SdpType::kPrAnswer:
108 return SessionDescriptionInterface::kPrAnswer;
109 case SdpType::kAnswer:
110 return SessionDescriptionInterface::kAnswer;
111 }
112 return "";
113}
114
115rtc::Optional<SdpType> SdpTypeFromString(const std::string& type_str) {
116 if (type_str == SessionDescriptionInterface::kOffer) {
117 return SdpType::kOffer;
118 } else if (type_str == SessionDescriptionInterface::kPrAnswer) {
119 return SdpType::kPrAnswer;
120 } else if (type_str == SessionDescriptionInterface::kAnswer) {
121 return SdpType::kAnswer;
122 } else {
123 return rtc::nullopt;
124 }
125}
126
127// TODO(steveanton): Remove this default implementation once Chromium has been
128// updated.
129SdpType SessionDescriptionInterface::GetType() const {
130 rtc::Optional<SdpType> maybe_type = SdpTypeFromString(type());
131 if (maybe_type) {
132 return *maybe_type;
133 } else {
134 RTC_LOG(LS_WARNING) << "Default implementation of "
135 "SessionDescriptionInterface::GetType does not "
136 "recognize the result from type(), returning "
137 "kOffer.";
138 return SdpType::kOffer;
139 }
140}
141
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000142SessionDescriptionInterface* CreateSessionDescription(const std::string& type,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000143 const std::string& sdp,
144 SdpParseError* error) {
Steve Anton88f2cb92017-12-05 12:47:32 -0800145 rtc::Optional<SdpType> maybe_type = SdpTypeFromString(type);
146 if (!maybe_type) {
147 return nullptr;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000148 }
149
Steve Anton88f2cb92017-12-05 12:47:32 -0800150 return CreateSessionDescription(*maybe_type, sdp, error).release();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000151}
152
Steve Anton88f2cb92017-12-05 12:47:32 -0800153std::unique_ptr<SessionDescriptionInterface> CreateSessionDescription(
154 SdpType type,
155 const std::string& sdp) {
156 return CreateSessionDescription(type, sdp, nullptr);
157}
158
159std::unique_ptr<SessionDescriptionInterface> CreateSessionDescription(
160 SdpType type,
161 const std::string& sdp,
162 SdpParseError* error_out) {
163 auto jsep_desc = rtc::MakeUnique<JsepSessionDescription>(type);
164 if (!SdpDeserialize(sdp, jsep_desc.get(), error_out)) {
165 return nullptr;
166 }
167 return std::move(jsep_desc);
168}
169
170JsepSessionDescription::JsepSessionDescription(SdpType type) : type_(type) {}
171
172JsepSessionDescription::JsepSessionDescription(const std::string& type) {
173 rtc::Optional<SdpType> maybe_type = SdpTypeFromString(type);
174 if (maybe_type) {
175 type_ = *maybe_type;
176 } else {
177 RTC_LOG(LS_WARNING)
178 << "JsepSessionDescription constructed with invalid type string: "
179 << type << ". Assuming it is an offer.";
180 type_ = SdpType::kOffer;
181 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000182}
183
184JsepSessionDescription::~JsepSessionDescription() {}
185
186bool JsepSessionDescription::Initialize(
187 cricket::SessionDescription* description,
188 const std::string& session_id,
189 const std::string& session_version) {
190 if (!description)
191 return false;
192
193 session_id_ = session_id;
194 session_version_ = session_version;
195 description_.reset(description);
196 candidate_collection_.resize(number_of_mediasections());
197 return true;
198}
199
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000200bool JsepSessionDescription::AddCandidate(
201 const IceCandidateInterface* candidate) {
202 if (!candidate || candidate->sdp_mline_index() < 0)
203 return false;
204 size_t mediasection_index = 0;
205 if (!GetMediasectionIndex(candidate, &mediasection_index)) {
206 return false;
207 }
208 if (mediasection_index >= number_of_mediasections())
209 return false;
jbauch083b73f2015-07-16 02:46:32 -0700210 const std::string& content_name =
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000211 description_->contents()[mediasection_index].name;
212 const cricket::TransportInfo* transport_info =
213 description_->GetTransportInfoByName(content_name);
214 if (!transport_info) {
215 return false;
216 }
217
218 cricket::Candidate updated_candidate = candidate->candidate();
219 if (updated_candidate.username().empty()) {
220 updated_candidate.set_username(transport_info->description.ice_ufrag);
221 }
222 if (updated_candidate.password().empty()) {
223 updated_candidate.set_password(transport_info->description.ice_pwd);
224 }
225
kwibergd1fe2812016-04-27 06:47:29 -0700226 std::unique_ptr<JsepIceCandidate> updated_candidate_wrapper(
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +0000227 new JsepIceCandidate(candidate->sdp_mid(),
228 static_cast<int>(mediasection_index),
229 updated_candidate));
230 if (!candidate_collection_[mediasection_index].HasCandidate(
zhihuang38989e52017-03-21 11:04:53 -0700231 updated_candidate_wrapper.get())) {
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +0000232 candidate_collection_[mediasection_index].add(
233 updated_candidate_wrapper.release());
zhihuang38989e52017-03-21 11:04:53 -0700234 UpdateConnectionAddress(
235 candidate_collection_[mediasection_index],
Steve Antonb1c1de12017-12-21 15:14:30 -0800236 description_->contents()[mediasection_index].media_description());
zhihuang38989e52017-03-21 11:04:53 -0700237 }
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +0000238
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000239 return true;
240}
241
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700242size_t JsepSessionDescription::RemoveCandidates(
243 const std::vector<cricket::Candidate>& candidates) {
244 size_t num_removed = 0;
245 for (auto& candidate : candidates) {
246 int mediasection_index = GetMediasectionIndex(candidate);
247 if (mediasection_index < 0) {
248 // Not found.
249 continue;
250 }
251 num_removed += candidate_collection_[mediasection_index].remove(candidate);
zhihuang38989e52017-03-21 11:04:53 -0700252 UpdateConnectionAddress(
253 candidate_collection_[mediasection_index],
Steve Antonb1c1de12017-12-21 15:14:30 -0800254 description_->contents()[mediasection_index].media_description());
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700255 }
256 return num_removed;
257}
258
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000259size_t JsepSessionDescription::number_of_mediasections() const {
260 if (!description_)
261 return 0;
262 return description_->contents().size();
263}
264
265const IceCandidateCollection* JsepSessionDescription::candidates(
266 size_t mediasection_index) const {
267 if (mediasection_index >= candidate_collection_.size())
268 return NULL;
269 return &candidate_collection_[mediasection_index];
270}
271
272bool JsepSessionDescription::ToString(std::string* out) const {
deadbeef9d3584c2016-02-16 17:54:10 -0800273 if (!description_ || !out) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000274 return false;
deadbeef9d3584c2016-02-16 17:54:10 -0800275 }
276 *out = SdpSerialize(*this, false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000277 return !out->empty();
278}
279
280bool JsepSessionDescription::GetMediasectionIndex(
281 const IceCandidateInterface* candidate,
282 size_t* index) {
283 if (!candidate || !index) {
284 return false;
285 }
286 *index = static_cast<size_t>(candidate->sdp_mline_index());
287 if (description_ && !candidate->sdp_mid().empty()) {
288 bool found = false;
289 // Try to match the sdp_mid with content name.
290 for (size_t i = 0; i < description_->contents().size(); ++i) {
291 if (candidate->sdp_mid() == description_->contents().at(i).name) {
292 *index = i;
293 found = true;
294 break;
295 }
296 }
297 if (!found) {
298 // If the sdp_mid is presented but we can't find a match, we consider
299 // this as an error.
300 return false;
301 }
302 }
303 return true;
304}
305
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700306int JsepSessionDescription::GetMediasectionIndex(
307 const cricket::Candidate& candidate) {
308 // Find the description with a matching transport name of the candidate.
309 const std::string& transport_name = candidate.transport_name();
310 for (size_t i = 0; i < description_->contents().size(); ++i) {
311 if (transport_name == description_->contents().at(i).name) {
312 return static_cast<int>(i);
313 }
314 }
315 return -1;
316}
317
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000318} // namespace webrtc