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