blob: 5e6a1cb2038a08779f814daa16714970f97ec253 [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,
57 cricket::ContentDescription* content_description) {
58 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);
91 static_cast<cricket::MediaContentDescription*>(content_description)
92 ->set_connection_address(connection_addr);
93}
94
Steve Anton88f2cb92017-12-05 12:47:32 -080095} // namespace
96
henrike@webrtc.org28e20752013-07-10 00:45:36 +000097const char SessionDescriptionInterface::kOffer[] = "offer";
98const char SessionDescriptionInterface::kPrAnswer[] = "pranswer";
99const char SessionDescriptionInterface::kAnswer[] = "answer";
100
101const int JsepSessionDescription::kDefaultVideoCodecId = 100;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000102const char JsepSessionDescription::kDefaultVideoCodecName[] = "VP8";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000103
Steve Anton88f2cb92017-12-05 12:47:32 -0800104const char* SdpTypeToString(SdpType type) {
105 switch (type) {
106 case SdpType::kOffer:
107 return SessionDescriptionInterface::kOffer;
108 case SdpType::kPrAnswer:
109 return SessionDescriptionInterface::kPrAnswer;
110 case SdpType::kAnswer:
111 return SessionDescriptionInterface::kAnswer;
112 }
113 return "";
114}
115
116rtc::Optional<SdpType> SdpTypeFromString(const std::string& type_str) {
117 if (type_str == SessionDescriptionInterface::kOffer) {
118 return SdpType::kOffer;
119 } else if (type_str == SessionDescriptionInterface::kPrAnswer) {
120 return SdpType::kPrAnswer;
121 } else if (type_str == SessionDescriptionInterface::kAnswer) {
122 return SdpType::kAnswer;
123 } else {
124 return rtc::nullopt;
125 }
126}
127
128// TODO(steveanton): Remove this default implementation once Chromium has been
129// updated.
130SdpType SessionDescriptionInterface::GetType() const {
131 rtc::Optional<SdpType> maybe_type = SdpTypeFromString(type());
132 if (maybe_type) {
133 return *maybe_type;
134 } else {
135 RTC_LOG(LS_WARNING) << "Default implementation of "
136 "SessionDescriptionInterface::GetType does not "
137 "recognize the result from type(), returning "
138 "kOffer.";
139 return SdpType::kOffer;
140 }
141}
142
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000143SessionDescriptionInterface* CreateSessionDescription(const std::string& type,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000144 const std::string& sdp,
145 SdpParseError* error) {
Steve Anton88f2cb92017-12-05 12:47:32 -0800146 rtc::Optional<SdpType> maybe_type = SdpTypeFromString(type);
147 if (!maybe_type) {
148 return nullptr;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000149 }
150
Steve Anton88f2cb92017-12-05 12:47:32 -0800151 return CreateSessionDescription(*maybe_type, sdp, error).release();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000152}
153
Steve Anton88f2cb92017-12-05 12:47:32 -0800154std::unique_ptr<SessionDescriptionInterface> CreateSessionDescription(
155 SdpType type,
156 const std::string& sdp) {
157 return CreateSessionDescription(type, sdp, nullptr);
158}
159
160std::unique_ptr<SessionDescriptionInterface> CreateSessionDescription(
161 SdpType type,
162 const std::string& sdp,
163 SdpParseError* error_out) {
164 auto jsep_desc = rtc::MakeUnique<JsepSessionDescription>(type);
165 if (!SdpDeserialize(sdp, jsep_desc.get(), error_out)) {
166 return nullptr;
167 }
168 return std::move(jsep_desc);
169}
170
171JsepSessionDescription::JsepSessionDescription(SdpType type) : type_(type) {}
172
173JsepSessionDescription::JsepSessionDescription(const std::string& type) {
174 rtc::Optional<SdpType> maybe_type = SdpTypeFromString(type);
175 if (maybe_type) {
176 type_ = *maybe_type;
177 } else {
178 RTC_LOG(LS_WARNING)
179 << "JsepSessionDescription constructed with invalid type string: "
180 << type << ". Assuming it is an offer.";
181 type_ = SdpType::kOffer;
182 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000183}
184
185JsepSessionDescription::~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],
237 description_->contents()[mediasection_index].description);
238 }
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],
255 description_->contents()[mediasection_index].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 }
277 *out = SdpSerialize(*this, false);
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