blob: 3844f9494a56c95374f58043cea47764ad5247c4 [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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020015#include "pc/mediasession.h"
16#include "pc/webrtcsdp.h"
17#include "rtc_base/arraysize.h"
18#include "rtc_base/stringencode.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000019
henrike@webrtc.org28e20752013-07-10 00:45:36 +000020using cricket::SessionDescription;
21
22namespace webrtc {
23
24static const char* kSupportedTypes[] = {
25 JsepSessionDescription::kOffer,
26 JsepSessionDescription::kPrAnswer,
27 JsepSessionDescription::kAnswer
28};
29
30static bool IsTypeSupported(const std::string& type) {
31 bool type_supported = false;
tfarina5237aaf2015-11-10 23:44:30 -080032 for (size_t i = 0; i < arraysize(kSupportedTypes); ++i) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000033 if (kSupportedTypes[i] == type) {
34 type_supported = true;
35 break;
36 }
37 }
38 return type_supported;
39}
40
zhihuang38989e52017-03-21 11:04:53 -070041// RFC 5245
42// It is RECOMMENDED that default candidates be chosen based on the
43// likelihood of those candidates to work with the peer that is being
44// contacted. It is RECOMMENDED that relayed > reflexive > host.
45static const int kPreferenceUnknown = 0;
46static const int kPreferenceHost = 1;
47static const int kPreferenceReflexive = 2;
48static const int kPreferenceRelayed = 3;
49
50static const char kDummyAddress[] = "0.0.0.0";
51static const int kDummyPort = 9;
52
53static int GetCandidatePreferenceFromType(const std::string& type) {
54 int preference = kPreferenceUnknown;
55 if (type == cricket::LOCAL_PORT_TYPE) {
56 preference = kPreferenceHost;
57 } else if (type == cricket::STUN_PORT_TYPE) {
58 preference = kPreferenceReflexive;
59 } else if (type == cricket::RELAY_PORT_TYPE) {
60 preference = kPreferenceRelayed;
61 } else {
zhihuang15238652017-03-23 10:32:12 -070062 preference = kPreferenceUnknown;
zhihuang38989e52017-03-21 11:04:53 -070063 }
64 return preference;
65}
66
67// Update the connection address for the MediaContentDescription based on the
68// candidates.
69static void UpdateConnectionAddress(
70 const JsepCandidateCollection& candidate_collection,
71 cricket::ContentDescription* content_description) {
72 int port = kDummyPort;
73 std::string ip = kDummyAddress;
74 int current_preference = kPreferenceUnknown;
75 int current_family = AF_UNSPEC;
76 for (size_t i = 0; i < candidate_collection.count(); ++i) {
77 const IceCandidateInterface* jsep_candidate = candidate_collection.at(i);
78 if (jsep_candidate->candidate().component() !=
79 cricket::ICE_CANDIDATE_COMPONENT_RTP) {
80 continue;
81 }
82 // Default destination should be UDP only.
83 if (jsep_candidate->candidate().protocol() != cricket::UDP_PROTOCOL_NAME) {
84 continue;
85 }
86 const int preference =
87 GetCandidatePreferenceFromType(jsep_candidate->candidate().type());
88 const int family = jsep_candidate->candidate().address().ipaddr().family();
89 // See if this candidate is more preferable then the current one if it's the
90 // same family. Or if the current family is IPv4 already so we could safely
91 // ignore all IPv6 ones. WebRTC bug 4269.
92 // http://code.google.com/p/webrtc/issues/detail?id=4269
93 if ((preference <= current_preference && current_family == family) ||
94 (current_family == AF_INET && family == AF_INET6)) {
95 continue;
96 }
97 current_preference = preference;
98 current_family = family;
99 port = jsep_candidate->candidate().address().port();
100 ip = jsep_candidate->candidate().address().ipaddr().ToString();
101 }
102 rtc::SocketAddress connection_addr;
103 connection_addr.SetIP(ip);
104 connection_addr.SetPort(port);
105 static_cast<cricket::MediaContentDescription*>(content_description)
106 ->set_connection_address(connection_addr);
107}
108
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000109const char SessionDescriptionInterface::kOffer[] = "offer";
110const char SessionDescriptionInterface::kPrAnswer[] = "pranswer";
111const char SessionDescriptionInterface::kAnswer[] = "answer";
112
113const int JsepSessionDescription::kDefaultVideoCodecId = 100;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000114const char JsepSessionDescription::kDefaultVideoCodecName[] = "VP8";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000115
116SessionDescriptionInterface* CreateSessionDescription(const std::string& type,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000117 const std::string& sdp,
118 SdpParseError* error) {
119 if (!IsTypeSupported(type)) {
120 return NULL;
121 }
122
123 JsepSessionDescription* jsep_desc = new JsepSessionDescription(type);
Steve Anton6d64e9a2017-09-12 09:44:05 -0700124 if (!SdpDeserialize(sdp, jsep_desc, error)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000125 delete jsep_desc;
126 return NULL;
127 }
128 return jsep_desc;
129}
130
131JsepSessionDescription::JsepSessionDescription(const std::string& type)
132 : type_(type) {
133}
134
135JsepSessionDescription::~JsepSessionDescription() {}
136
137bool JsepSessionDescription::Initialize(
138 cricket::SessionDescription* description,
139 const std::string& session_id,
140 const std::string& session_version) {
141 if (!description)
142 return false;
143
144 session_id_ = session_id;
145 session_version_ = session_version;
146 description_.reset(description);
147 candidate_collection_.resize(number_of_mediasections());
148 return true;
149}
150
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000151bool JsepSessionDescription::AddCandidate(
152 const IceCandidateInterface* candidate) {
153 if (!candidate || candidate->sdp_mline_index() < 0)
154 return false;
155 size_t mediasection_index = 0;
156 if (!GetMediasectionIndex(candidate, &mediasection_index)) {
157 return false;
158 }
159 if (mediasection_index >= number_of_mediasections())
160 return false;
jbauch083b73f2015-07-16 02:46:32 -0700161 const std::string& content_name =
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000162 description_->contents()[mediasection_index].name;
163 const cricket::TransportInfo* transport_info =
164 description_->GetTransportInfoByName(content_name);
165 if (!transport_info) {
166 return false;
167 }
168
169 cricket::Candidate updated_candidate = candidate->candidate();
170 if (updated_candidate.username().empty()) {
171 updated_candidate.set_username(transport_info->description.ice_ufrag);
172 }
173 if (updated_candidate.password().empty()) {
174 updated_candidate.set_password(transport_info->description.ice_pwd);
175 }
176
kwibergd1fe2812016-04-27 06:47:29 -0700177 std::unique_ptr<JsepIceCandidate> updated_candidate_wrapper(
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +0000178 new JsepIceCandidate(candidate->sdp_mid(),
179 static_cast<int>(mediasection_index),
180 updated_candidate));
181 if (!candidate_collection_[mediasection_index].HasCandidate(
zhihuang38989e52017-03-21 11:04:53 -0700182 updated_candidate_wrapper.get())) {
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +0000183 candidate_collection_[mediasection_index].add(
184 updated_candidate_wrapper.release());
zhihuang38989e52017-03-21 11:04:53 -0700185 UpdateConnectionAddress(
186 candidate_collection_[mediasection_index],
187 description_->contents()[mediasection_index].description);
188 }
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +0000189
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000190 return true;
191}
192
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700193size_t JsepSessionDescription::RemoveCandidates(
194 const std::vector<cricket::Candidate>& candidates) {
195 size_t num_removed = 0;
196 for (auto& candidate : candidates) {
197 int mediasection_index = GetMediasectionIndex(candidate);
198 if (mediasection_index < 0) {
199 // Not found.
200 continue;
201 }
202 num_removed += candidate_collection_[mediasection_index].remove(candidate);
zhihuang38989e52017-03-21 11:04:53 -0700203 UpdateConnectionAddress(
204 candidate_collection_[mediasection_index],
205 description_->contents()[mediasection_index].description);
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700206 }
207 return num_removed;
208}
209
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000210size_t JsepSessionDescription::number_of_mediasections() const {
211 if (!description_)
212 return 0;
213 return description_->contents().size();
214}
215
216const IceCandidateCollection* JsepSessionDescription::candidates(
217 size_t mediasection_index) const {
218 if (mediasection_index >= candidate_collection_.size())
219 return NULL;
220 return &candidate_collection_[mediasection_index];
221}
222
223bool JsepSessionDescription::ToString(std::string* out) const {
deadbeef9d3584c2016-02-16 17:54:10 -0800224 if (!description_ || !out) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000225 return false;
deadbeef9d3584c2016-02-16 17:54:10 -0800226 }
227 *out = SdpSerialize(*this, false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000228 return !out->empty();
229}
230
231bool JsepSessionDescription::GetMediasectionIndex(
232 const IceCandidateInterface* candidate,
233 size_t* index) {
234 if (!candidate || !index) {
235 return false;
236 }
237 *index = static_cast<size_t>(candidate->sdp_mline_index());
238 if (description_ && !candidate->sdp_mid().empty()) {
239 bool found = false;
240 // Try to match the sdp_mid with content name.
241 for (size_t i = 0; i < description_->contents().size(); ++i) {
242 if (candidate->sdp_mid() == description_->contents().at(i).name) {
243 *index = i;
244 found = true;
245 break;
246 }
247 }
248 if (!found) {
249 // If the sdp_mid is presented but we can't find a match, we consider
250 // this as an error.
251 return false;
252 }
253 }
254 return true;
255}
256
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700257int JsepSessionDescription::GetMediasectionIndex(
258 const cricket::Candidate& candidate) {
259 // Find the description with a matching transport name of the candidate.
260 const std::string& transport_name = candidate.transport_name();
261 for (size_t i = 0; i < description_->contents().size(); ++i) {
262 if (transport_name == description_->contents().at(i).name) {
263 return static_cast<int>(i);
264 }
265 }
266 return -1;
267}
268
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000269} // namespace webrtc