You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

347 lines
12 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * Copyright 2012 The WebRTC project authors. All Rights Reserved.
  3. *
  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.
  9. */
  10. #include "api/jsep_session_description.h"
  11. #include <memory>
  12. #include "p2p/base/port.h"
  13. #include "pc/media_session.h"
  14. #include "pc/webrtc_sdp.h"
  15. #include "rtc_base/arraysize.h"
  16. using cricket::SessionDescription;
  17. namespace webrtc {
  18. namespace {
  19. // RFC 5245
  20. // It is RECOMMENDED that default candidates be chosen based on the
  21. // likelihood of those candidates to work with the peer that is being
  22. // contacted. It is RECOMMENDED that relayed > reflexive > host.
  23. constexpr int kPreferenceUnknown = 0;
  24. constexpr int kPreferenceHost = 1;
  25. constexpr int kPreferenceReflexive = 2;
  26. constexpr int kPreferenceRelayed = 3;
  27. constexpr char kDummyAddress[] = "0.0.0.0";
  28. constexpr int kDummyPort = 9;
  29. int GetCandidatePreferenceFromType(const std::string& type) {
  30. int preference = kPreferenceUnknown;
  31. if (type == cricket::LOCAL_PORT_TYPE) {
  32. preference = kPreferenceHost;
  33. } else if (type == cricket::STUN_PORT_TYPE) {
  34. preference = kPreferenceReflexive;
  35. } else if (type == cricket::RELAY_PORT_TYPE) {
  36. preference = kPreferenceRelayed;
  37. } else {
  38. preference = kPreferenceUnknown;
  39. }
  40. return preference;
  41. }
  42. // Update the connection address for the MediaContentDescription based on the
  43. // candidates.
  44. void UpdateConnectionAddress(
  45. const JsepCandidateCollection& candidate_collection,
  46. cricket::MediaContentDescription* media_desc) {
  47. int port = kDummyPort;
  48. std::string ip = kDummyAddress;
  49. std::string hostname;
  50. int current_preference = kPreferenceUnknown;
  51. int current_family = AF_UNSPEC;
  52. for (size_t i = 0; i < candidate_collection.count(); ++i) {
  53. const IceCandidateInterface* jsep_candidate = candidate_collection.at(i);
  54. if (jsep_candidate->candidate().component() !=
  55. cricket::ICE_CANDIDATE_COMPONENT_RTP) {
  56. continue;
  57. }
  58. // Default destination should be UDP only.
  59. if (jsep_candidate->candidate().protocol() != cricket::UDP_PROTOCOL_NAME) {
  60. continue;
  61. }
  62. const int preference =
  63. GetCandidatePreferenceFromType(jsep_candidate->candidate().type());
  64. const int family = jsep_candidate->candidate().address().ipaddr().family();
  65. // See if this candidate is more preferable then the current one if it's the
  66. // same family. Or if the current family is IPv4 already so we could safely
  67. // ignore all IPv6 ones. WebRTC bug 4269.
  68. // http://code.google.com/p/webrtc/issues/detail?id=4269
  69. if ((preference <= current_preference && current_family == family) ||
  70. (current_family == AF_INET && family == AF_INET6)) {
  71. continue;
  72. }
  73. current_preference = preference;
  74. current_family = family;
  75. const rtc::SocketAddress& candidate_addr =
  76. jsep_candidate->candidate().address();
  77. port = candidate_addr.port();
  78. ip = candidate_addr.ipaddr().ToString();
  79. hostname = candidate_addr.hostname();
  80. }
  81. rtc::SocketAddress connection_addr(ip, port);
  82. if (rtc::IPIsUnspec(connection_addr.ipaddr()) && !hostname.empty()) {
  83. // When a hostname candidate becomes the (default) connection address,
  84. // we use the dummy address 0.0.0.0 and port 9 in the c= and the m= lines.
  85. //
  86. // We have observed in deployment that with a FQDN in a c= line, SDP parsing
  87. // could fail in other JSEP implementations. We note that the wildcard
  88. // addresses (0.0.0.0 or ::) with port 9 are given the exception as the
  89. // connection address that will not result in an ICE mismatch
  90. // (draft-ietf-mmusic-ice-sip-sdp). Also, 0.0.0.0 or :: can be used as the
  91. // connection address in the initial offer or answer with trickle ICE
  92. // if the offerer or answerer does not want to include the host IP address
  93. // (draft-ietf-mmusic-trickle-ice-sip), and in particular 0.0.0.0 has been
  94. // widely deployed for this use without outstanding compatibility issues.
  95. // Combining the above considerations, we use 0.0.0.0 with port 9 to
  96. // populate the c= and the m= lines. See |BuildMediaDescription| in
  97. // webrtc_sdp.cc for the SDP generation with
  98. // |media_desc->connection_address()|.
  99. connection_addr = rtc::SocketAddress(kDummyAddress, kDummyPort);
  100. }
  101. media_desc->set_connection_address(connection_addr);
  102. }
  103. } // namespace
  104. const int JsepSessionDescription::kDefaultVideoCodecId = 100;
  105. const char JsepSessionDescription::kDefaultVideoCodecName[] = "VP8";
  106. // TODO(steveanton): Remove this default implementation once Chromium has been
  107. // updated.
  108. SdpType SessionDescriptionInterface::GetType() const {
  109. absl::optional<SdpType> maybe_type = SdpTypeFromString(type());
  110. if (maybe_type) {
  111. return *maybe_type;
  112. } else {
  113. RTC_LOG(LS_WARNING) << "Default implementation of "
  114. "SessionDescriptionInterface::GetType does not "
  115. "recognize the result from type(), returning "
  116. "kOffer.";
  117. return SdpType::kOffer;
  118. }
  119. }
  120. SessionDescriptionInterface* CreateSessionDescription(const std::string& type,
  121. const std::string& sdp,
  122. SdpParseError* error) {
  123. absl::optional<SdpType> maybe_type = SdpTypeFromString(type);
  124. if (!maybe_type) {
  125. return nullptr;
  126. }
  127. return CreateSessionDescription(*maybe_type, sdp, error).release();
  128. }
  129. std::unique_ptr<SessionDescriptionInterface> CreateSessionDescription(
  130. SdpType type,
  131. const std::string& sdp) {
  132. return CreateSessionDescription(type, sdp, nullptr);
  133. }
  134. std::unique_ptr<SessionDescriptionInterface> CreateSessionDescription(
  135. SdpType type,
  136. const std::string& sdp,
  137. SdpParseError* error_out) {
  138. auto jsep_desc = std::make_unique<JsepSessionDescription>(type);
  139. if (type != SdpType::kRollback) {
  140. if (!SdpDeserialize(sdp, jsep_desc.get(), error_out)) {
  141. return nullptr;
  142. }
  143. }
  144. return std::move(jsep_desc);
  145. }
  146. std::unique_ptr<SessionDescriptionInterface> CreateSessionDescription(
  147. SdpType type,
  148. const std::string& session_id,
  149. const std::string& session_version,
  150. std::unique_ptr<cricket::SessionDescription> description) {
  151. auto jsep_description = std::make_unique<JsepSessionDescription>(type);
  152. bool initialize_success = jsep_description->Initialize(
  153. std::move(description), session_id, session_version);
  154. RTC_DCHECK(initialize_success);
  155. return std::move(jsep_description);
  156. }
  157. JsepSessionDescription::JsepSessionDescription(SdpType type) : type_(type) {}
  158. JsepSessionDescription::JsepSessionDescription(const std::string& type) {
  159. absl::optional<SdpType> maybe_type = SdpTypeFromString(type);
  160. if (maybe_type) {
  161. type_ = *maybe_type;
  162. } else {
  163. RTC_LOG(LS_WARNING)
  164. << "JsepSessionDescription constructed with invalid type string: "
  165. << type << ". Assuming it is an offer.";
  166. type_ = SdpType::kOffer;
  167. }
  168. }
  169. JsepSessionDescription::JsepSessionDescription(
  170. SdpType type,
  171. std::unique_ptr<cricket::SessionDescription> description,
  172. absl::string_view session_id,
  173. absl::string_view session_version)
  174. : description_(std::move(description)),
  175. session_id_(session_id),
  176. session_version_(session_version),
  177. type_(type) {
  178. RTC_DCHECK(description_);
  179. candidate_collection_.resize(number_of_mediasections());
  180. }
  181. JsepSessionDescription::~JsepSessionDescription() {}
  182. bool JsepSessionDescription::Initialize(
  183. std::unique_ptr<cricket::SessionDescription> description,
  184. const std::string& session_id,
  185. const std::string& session_version) {
  186. if (!description)
  187. return false;
  188. session_id_ = session_id;
  189. session_version_ = session_version;
  190. description_ = std::move(description);
  191. candidate_collection_.resize(number_of_mediasections());
  192. return true;
  193. }
  194. bool JsepSessionDescription::AddCandidate(
  195. const IceCandidateInterface* candidate) {
  196. if (!candidate)
  197. return false;
  198. size_t mediasection_index = 0;
  199. if (!GetMediasectionIndex(candidate, &mediasection_index)) {
  200. return false;
  201. }
  202. if (mediasection_index >= number_of_mediasections())
  203. return false;
  204. const std::string& content_name =
  205. description_->contents()[mediasection_index].name;
  206. const cricket::TransportInfo* transport_info =
  207. description_->GetTransportInfoByName(content_name);
  208. if (!transport_info) {
  209. return false;
  210. }
  211. cricket::Candidate updated_candidate = candidate->candidate();
  212. if (updated_candidate.username().empty()) {
  213. updated_candidate.set_username(transport_info->description.ice_ufrag);
  214. }
  215. if (updated_candidate.password().empty()) {
  216. updated_candidate.set_password(transport_info->description.ice_pwd);
  217. }
  218. std::unique_ptr<JsepIceCandidate> updated_candidate_wrapper(
  219. new JsepIceCandidate(candidate->sdp_mid(),
  220. static_cast<int>(mediasection_index),
  221. updated_candidate));
  222. if (!candidate_collection_[mediasection_index].HasCandidate(
  223. updated_candidate_wrapper.get())) {
  224. candidate_collection_[mediasection_index].add(
  225. updated_candidate_wrapper.release());
  226. UpdateConnectionAddress(
  227. candidate_collection_[mediasection_index],
  228. description_->contents()[mediasection_index].media_description());
  229. }
  230. return true;
  231. }
  232. size_t JsepSessionDescription::RemoveCandidates(
  233. const std::vector<cricket::Candidate>& candidates) {
  234. size_t num_removed = 0;
  235. for (auto& candidate : candidates) {
  236. int mediasection_index = GetMediasectionIndex(candidate);
  237. if (mediasection_index < 0) {
  238. // Not found.
  239. continue;
  240. }
  241. num_removed += candidate_collection_[mediasection_index].remove(candidate);
  242. UpdateConnectionAddress(
  243. candidate_collection_[mediasection_index],
  244. description_->contents()[mediasection_index].media_description());
  245. }
  246. return num_removed;
  247. }
  248. size_t JsepSessionDescription::number_of_mediasections() const {
  249. if (!description_)
  250. return 0;
  251. return description_->contents().size();
  252. }
  253. const IceCandidateCollection* JsepSessionDescription::candidates(
  254. size_t mediasection_index) const {
  255. if (mediasection_index >= candidate_collection_.size())
  256. return NULL;
  257. return &candidate_collection_[mediasection_index];
  258. }
  259. bool JsepSessionDescription::ToString(std::string* out) const {
  260. if (!description_ || !out) {
  261. return false;
  262. }
  263. *out = SdpSerialize(*this);
  264. return !out->empty();
  265. }
  266. bool JsepSessionDescription::GetMediasectionIndex(
  267. const IceCandidateInterface* candidate,
  268. size_t* index) {
  269. if (!candidate || !index) {
  270. return false;
  271. }
  272. // If the candidate has no valid mline index or sdp_mid, it is impossible
  273. // to find a match.
  274. if (candidate->sdp_mid().empty() &&
  275. (candidate->sdp_mline_index() < 0 ||
  276. static_cast<size_t>(candidate->sdp_mline_index()) >=
  277. description_->contents().size())) {
  278. return false;
  279. }
  280. if (candidate->sdp_mline_index() >= 0)
  281. *index = static_cast<size_t>(candidate->sdp_mline_index());
  282. if (description_ && !candidate->sdp_mid().empty()) {
  283. bool found = false;
  284. // Try to match the sdp_mid with content name.
  285. for (size_t i = 0; i < description_->contents().size(); ++i) {
  286. if (candidate->sdp_mid() == description_->contents().at(i).name) {
  287. *index = i;
  288. found = true;
  289. break;
  290. }
  291. }
  292. if (!found) {
  293. // If the sdp_mid is presented but we can't find a match, we consider
  294. // this as an error.
  295. return false;
  296. }
  297. }
  298. return true;
  299. }
  300. int JsepSessionDescription::GetMediasectionIndex(
  301. const cricket::Candidate& candidate) {
  302. // Find the description with a matching transport name of the candidate.
  303. const std::string& transport_name = candidate.transport_name();
  304. for (size_t i = 0; i < description_->contents().size(); ++i) {
  305. if (transport_name == description_->contents().at(i).name) {
  306. return static_cast<int>(i);
  307. }
  308. }
  309. return -1;
  310. }
  311. } // namespace webrtc