jlmiller@webrtc.org | 5f93d0a | 2015-01-20 21:36:13 +0000 | [diff] [blame] | 1 | /* |
kjellander | b24317b | 2016-02-10 07:54:43 -0800 | [diff] [blame] | 2 | * Copyright 2012 The WebRTC project authors. All Rights Reserved. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3 | * |
kjellander | b24317b | 2016-02-10 07:54:43 -0800 | [diff] [blame] | 4 | * 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.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 9 | */ |
| 10 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "api/jsepsessiondescription.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 12 | |
kwiberg | d1fe281 | 2016-04-27 06:47:29 -0700 | [diff] [blame] | 13 | #include <memory> |
| 14 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 15 | #include "pc/mediasession.h" |
| 16 | #include "pc/webrtcsdp.h" |
| 17 | #include "rtc_base/arraysize.h" |
| 18 | #include "rtc_base/stringencode.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 19 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 20 | using cricket::SessionDescription; |
| 21 | |
| 22 | namespace webrtc { |
| 23 | |
| 24 | static const char* kSupportedTypes[] = { |
| 25 | JsepSessionDescription::kOffer, |
| 26 | JsepSessionDescription::kPrAnswer, |
| 27 | JsepSessionDescription::kAnswer |
| 28 | }; |
| 29 | |
| 30 | static bool IsTypeSupported(const std::string& type) { |
| 31 | bool type_supported = false; |
tfarina | 5237aaf | 2015-11-10 23:44:30 -0800 | [diff] [blame] | 32 | for (size_t i = 0; i < arraysize(kSupportedTypes); ++i) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 33 | if (kSupportedTypes[i] == type) { |
| 34 | type_supported = true; |
| 35 | break; |
| 36 | } |
| 37 | } |
| 38 | return type_supported; |
| 39 | } |
| 40 | |
zhihuang | 38989e5 | 2017-03-21 11:04:53 -0700 | [diff] [blame] | 41 | // 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. |
| 45 | static const int kPreferenceUnknown = 0; |
| 46 | static const int kPreferenceHost = 1; |
| 47 | static const int kPreferenceReflexive = 2; |
| 48 | static const int kPreferenceRelayed = 3; |
| 49 | |
| 50 | static const char kDummyAddress[] = "0.0.0.0"; |
| 51 | static const int kDummyPort = 9; |
| 52 | |
| 53 | static 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 { |
zhihuang | 1523865 | 2017-03-23 10:32:12 -0700 | [diff] [blame] | 62 | preference = kPreferenceUnknown; |
zhihuang | 38989e5 | 2017-03-21 11:04:53 -0700 | [diff] [blame] | 63 | } |
| 64 | return preference; |
| 65 | } |
| 66 | |
| 67 | // Update the connection address for the MediaContentDescription based on the |
| 68 | // candidates. |
| 69 | static 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.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 109 | const char SessionDescriptionInterface::kOffer[] = "offer"; |
| 110 | const char SessionDescriptionInterface::kPrAnswer[] = "pranswer"; |
| 111 | const char SessionDescriptionInterface::kAnswer[] = "answer"; |
| 112 | |
| 113 | const int JsepSessionDescription::kDefaultVideoCodecId = 100; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 114 | const char JsepSessionDescription::kDefaultVideoCodecName[] = "VP8"; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 115 | |
| 116 | SessionDescriptionInterface* CreateSessionDescription(const std::string& type, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 117 | const std::string& sdp, |
| 118 | SdpParseError* error) { |
| 119 | if (!IsTypeSupported(type)) { |
| 120 | return NULL; |
| 121 | } |
| 122 | |
| 123 | JsepSessionDescription* jsep_desc = new JsepSessionDescription(type); |
Steve Anton | 6d64e9a | 2017-09-12 09:44:05 -0700 | [diff] [blame] | 124 | if (!SdpDeserialize(sdp, jsep_desc, error)) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 125 | delete jsep_desc; |
| 126 | return NULL; |
| 127 | } |
| 128 | return jsep_desc; |
| 129 | } |
| 130 | |
| 131 | JsepSessionDescription::JsepSessionDescription(const std::string& type) |
| 132 | : type_(type) { |
| 133 | } |
| 134 | |
| 135 | JsepSessionDescription::~JsepSessionDescription() {} |
| 136 | |
| 137 | bool 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.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 151 | bool 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; |
jbauch | 083b73f | 2015-07-16 02:46:32 -0700 | [diff] [blame] | 161 | const std::string& content_name = |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 162 | 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 | |
kwiberg | d1fe281 | 2016-04-27 06:47:29 -0700 | [diff] [blame] | 177 | std::unique_ptr<JsepIceCandidate> updated_candidate_wrapper( |
mallinath@webrtc.org | 67ee6b9 | 2014-02-03 16:57:16 +0000 | [diff] [blame] | 178 | new JsepIceCandidate(candidate->sdp_mid(), |
| 179 | static_cast<int>(mediasection_index), |
| 180 | updated_candidate)); |
| 181 | if (!candidate_collection_[mediasection_index].HasCandidate( |
zhihuang | 38989e5 | 2017-03-21 11:04:53 -0700 | [diff] [blame] | 182 | updated_candidate_wrapper.get())) { |
mallinath@webrtc.org | 67ee6b9 | 2014-02-03 16:57:16 +0000 | [diff] [blame] | 183 | candidate_collection_[mediasection_index].add( |
| 184 | updated_candidate_wrapper.release()); |
zhihuang | 38989e5 | 2017-03-21 11:04:53 -0700 | [diff] [blame] | 185 | UpdateConnectionAddress( |
| 186 | candidate_collection_[mediasection_index], |
| 187 | description_->contents()[mediasection_index].description); |
| 188 | } |
mallinath@webrtc.org | 67ee6b9 | 2014-02-03 16:57:16 +0000 | [diff] [blame] | 189 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 190 | return true; |
| 191 | } |
| 192 | |
Honghai Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 193 | size_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); |
zhihuang | 38989e5 | 2017-03-21 11:04:53 -0700 | [diff] [blame] | 203 | UpdateConnectionAddress( |
| 204 | candidate_collection_[mediasection_index], |
| 205 | description_->contents()[mediasection_index].description); |
Honghai Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 206 | } |
| 207 | return num_removed; |
| 208 | } |
| 209 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 210 | size_t JsepSessionDescription::number_of_mediasections() const { |
| 211 | if (!description_) |
| 212 | return 0; |
| 213 | return description_->contents().size(); |
| 214 | } |
| 215 | |
| 216 | const 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 | |
| 223 | bool JsepSessionDescription::ToString(std::string* out) const { |
deadbeef | 9d3584c | 2016-02-16 17:54:10 -0800 | [diff] [blame] | 224 | if (!description_ || !out) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 225 | return false; |
deadbeef | 9d3584c | 2016-02-16 17:54:10 -0800 | [diff] [blame] | 226 | } |
| 227 | *out = SdpSerialize(*this, false); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 228 | return !out->empty(); |
| 229 | } |
| 230 | |
| 231 | bool 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 Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 257 | int 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.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 269 | } // namespace webrtc |