blob: 2e972ea3b4bd505b3d0546449d48c5e03ed0d8a5 [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
Henrik Kjellander15583c12016-02-10 10:53:12 +010011#include "webrtc/api/jsepsessiondescription.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000012
kwibergd1fe2812016-04-27 06:47:29 -070013#include <memory>
14
kjellander@webrtc.org9b8df252016-02-12 06:47:59 +010015#include "webrtc/pc/mediasession.h"
ossu7bb87ee2017-01-23 04:56:25 -080016#include "webrtc/pc/webrtcsdp.h"
Edward Lemurc20978e2017-07-06 19:44:34 +020017#include "webrtc/rtc_base/arraysize.h"
18#include "webrtc/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);
124 if (!jsep_desc->Initialize(sdp, error)) {
125 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
151bool JsepSessionDescription::Initialize(const std::string& sdp,
152 SdpParseError* error) {
153 return SdpDeserialize(sdp, this, error);
154}
155
156bool JsepSessionDescription::AddCandidate(
157 const IceCandidateInterface* candidate) {
158 if (!candidate || candidate->sdp_mline_index() < 0)
159 return false;
160 size_t mediasection_index = 0;
161 if (!GetMediasectionIndex(candidate, &mediasection_index)) {
162 return false;
163 }
164 if (mediasection_index >= number_of_mediasections())
165 return false;
jbauch083b73f2015-07-16 02:46:32 -0700166 const std::string& content_name =
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000167 description_->contents()[mediasection_index].name;
168 const cricket::TransportInfo* transport_info =
169 description_->GetTransportInfoByName(content_name);
170 if (!transport_info) {
171 return false;
172 }
173
174 cricket::Candidate updated_candidate = candidate->candidate();
175 if (updated_candidate.username().empty()) {
176 updated_candidate.set_username(transport_info->description.ice_ufrag);
177 }
178 if (updated_candidate.password().empty()) {
179 updated_candidate.set_password(transport_info->description.ice_pwd);
180 }
181
kwibergd1fe2812016-04-27 06:47:29 -0700182 std::unique_ptr<JsepIceCandidate> updated_candidate_wrapper(
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +0000183 new JsepIceCandidate(candidate->sdp_mid(),
184 static_cast<int>(mediasection_index),
185 updated_candidate));
186 if (!candidate_collection_[mediasection_index].HasCandidate(
zhihuang38989e52017-03-21 11:04:53 -0700187 updated_candidate_wrapper.get())) {
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +0000188 candidate_collection_[mediasection_index].add(
189 updated_candidate_wrapper.release());
zhihuang38989e52017-03-21 11:04:53 -0700190 UpdateConnectionAddress(
191 candidate_collection_[mediasection_index],
192 description_->contents()[mediasection_index].description);
193 }
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +0000194
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000195 return true;
196}
197
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700198size_t JsepSessionDescription::RemoveCandidates(
199 const std::vector<cricket::Candidate>& candidates) {
200 size_t num_removed = 0;
201 for (auto& candidate : candidates) {
202 int mediasection_index = GetMediasectionIndex(candidate);
203 if (mediasection_index < 0) {
204 // Not found.
205 continue;
206 }
207 num_removed += candidate_collection_[mediasection_index].remove(candidate);
zhihuang38989e52017-03-21 11:04:53 -0700208 UpdateConnectionAddress(
209 candidate_collection_[mediasection_index],
210 description_->contents()[mediasection_index].description);
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700211 }
212 return num_removed;
213}
214
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000215size_t JsepSessionDescription::number_of_mediasections() const {
216 if (!description_)
217 return 0;
218 return description_->contents().size();
219}
220
221const IceCandidateCollection* JsepSessionDescription::candidates(
222 size_t mediasection_index) const {
223 if (mediasection_index >= candidate_collection_.size())
224 return NULL;
225 return &candidate_collection_[mediasection_index];
226}
227
228bool JsepSessionDescription::ToString(std::string* out) const {
deadbeef9d3584c2016-02-16 17:54:10 -0800229 if (!description_ || !out) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000230 return false;
deadbeef9d3584c2016-02-16 17:54:10 -0800231 }
232 *out = SdpSerialize(*this, false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000233 return !out->empty();
234}
235
236bool JsepSessionDescription::GetMediasectionIndex(
237 const IceCandidateInterface* candidate,
238 size_t* index) {
239 if (!candidate || !index) {
240 return false;
241 }
242 *index = static_cast<size_t>(candidate->sdp_mline_index());
243 if (description_ && !candidate->sdp_mid().empty()) {
244 bool found = false;
245 // Try to match the sdp_mid with content name.
246 for (size_t i = 0; i < description_->contents().size(); ++i) {
247 if (candidate->sdp_mid() == description_->contents().at(i).name) {
248 *index = i;
249 found = true;
250 break;
251 }
252 }
253 if (!found) {
254 // If the sdp_mid is presented but we can't find a match, we consider
255 // this as an error.
256 return false;
257 }
258 }
259 return true;
260}
261
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700262int JsepSessionDescription::GetMediasectionIndex(
263 const cricket::Candidate& candidate) {
264 // Find the description with a matching transport name of the candidate.
265 const std::string& transport_name = candidate.transport_name();
266 for (size_t i = 0; i < description_->contents().size(); ++i) {
267 if (transport_name == description_->contents().at(i).name) {
268 return static_cast<int>(i);
269 }
270 }
271 return -1;
272}
273
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000274} // namespace webrtc