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.

315 lines
9.3 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * Copyright 2010 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 "pc/session_description.h"
  11. #include <algorithm>
  12. #include <utility>
  13. #include "absl/algorithm/container.h"
  14. #include "absl/memory/memory.h"
  15. #include "pc/media_protocol_names.h"
  16. #include "rtc_base/checks.h"
  17. namespace cricket {
  18. namespace {
  19. ContentInfo* FindContentInfoByName(ContentInfos* contents,
  20. const std::string& name) {
  21. RTC_DCHECK(contents);
  22. for (ContentInfo& content : *contents) {
  23. if (content.name == name) {
  24. return &content;
  25. }
  26. }
  27. return nullptr;
  28. }
  29. } // namespace
  30. const ContentInfo* FindContentInfoByName(const ContentInfos& contents,
  31. const std::string& name) {
  32. for (ContentInfos::const_iterator content = contents.begin();
  33. content != contents.end(); ++content) {
  34. if (content->name == name) {
  35. return &(*content);
  36. }
  37. }
  38. return NULL;
  39. }
  40. const ContentInfo* FindContentInfoByType(const ContentInfos& contents,
  41. MediaProtocolType type) {
  42. for (const auto& content : contents) {
  43. if (content.type == type) {
  44. return &content;
  45. }
  46. }
  47. return nullptr;
  48. }
  49. ContentGroup::ContentGroup(const std::string& semantics)
  50. : semantics_(semantics) {}
  51. ContentGroup::ContentGroup(const ContentGroup&) = default;
  52. ContentGroup::ContentGroup(ContentGroup&&) = default;
  53. ContentGroup& ContentGroup::operator=(const ContentGroup&) = default;
  54. ContentGroup& ContentGroup::operator=(ContentGroup&&) = default;
  55. ContentGroup::~ContentGroup() = default;
  56. const std::string* ContentGroup::FirstContentName() const {
  57. return (!content_names_.empty()) ? &(*content_names_.begin()) : NULL;
  58. }
  59. bool ContentGroup::HasContentName(const std::string& content_name) const {
  60. return absl::c_linear_search(content_names_, content_name);
  61. }
  62. void ContentGroup::AddContentName(const std::string& content_name) {
  63. if (!HasContentName(content_name)) {
  64. content_names_.push_back(content_name);
  65. }
  66. }
  67. bool ContentGroup::RemoveContentName(const std::string& content_name) {
  68. ContentNames::iterator iter = absl::c_find(content_names_, content_name);
  69. if (iter == content_names_.end()) {
  70. return false;
  71. }
  72. content_names_.erase(iter);
  73. return true;
  74. }
  75. SessionDescription::SessionDescription() = default;
  76. SessionDescription::SessionDescription(const SessionDescription&) = default;
  77. SessionDescription::~SessionDescription() {}
  78. std::unique_ptr<SessionDescription> SessionDescription::Clone() const {
  79. // Copy using the private copy constructor.
  80. // This will clone the descriptions using ContentInfo's copy constructor.
  81. return absl::WrapUnique(new SessionDescription(*this));
  82. }
  83. const ContentInfo* SessionDescription::GetContentByName(
  84. const std::string& name) const {
  85. return FindContentInfoByName(contents_, name);
  86. }
  87. ContentInfo* SessionDescription::GetContentByName(const std::string& name) {
  88. return FindContentInfoByName(&contents_, name);
  89. }
  90. const MediaContentDescription* SessionDescription::GetContentDescriptionByName(
  91. const std::string& name) const {
  92. const ContentInfo* cinfo = FindContentInfoByName(contents_, name);
  93. if (cinfo == NULL) {
  94. return NULL;
  95. }
  96. return cinfo->media_description();
  97. }
  98. MediaContentDescription* SessionDescription::GetContentDescriptionByName(
  99. const std::string& name) {
  100. ContentInfo* cinfo = FindContentInfoByName(&contents_, name);
  101. if (cinfo == NULL) {
  102. return NULL;
  103. }
  104. return cinfo->media_description();
  105. }
  106. const ContentInfo* SessionDescription::FirstContentByType(
  107. MediaProtocolType type) const {
  108. return FindContentInfoByType(contents_, type);
  109. }
  110. const ContentInfo* SessionDescription::FirstContent() const {
  111. return (contents_.empty()) ? NULL : &(*contents_.begin());
  112. }
  113. void SessionDescription::AddContent(
  114. const std::string& name,
  115. MediaProtocolType type,
  116. std::unique_ptr<MediaContentDescription> description) {
  117. ContentInfo content(type);
  118. content.name = name;
  119. content.set_media_description(std::move(description));
  120. AddContent(std::move(content));
  121. }
  122. void SessionDescription::AddContent(
  123. const std::string& name,
  124. MediaProtocolType type,
  125. bool rejected,
  126. std::unique_ptr<MediaContentDescription> description) {
  127. ContentInfo content(type);
  128. content.name = name;
  129. content.rejected = rejected;
  130. content.set_media_description(std::move(description));
  131. AddContent(std::move(content));
  132. }
  133. void SessionDescription::AddContent(
  134. const std::string& name,
  135. MediaProtocolType type,
  136. bool rejected,
  137. bool bundle_only,
  138. std::unique_ptr<MediaContentDescription> description) {
  139. ContentInfo content(type);
  140. content.name = name;
  141. content.rejected = rejected;
  142. content.bundle_only = bundle_only;
  143. content.set_media_description(std::move(description));
  144. AddContent(std::move(content));
  145. }
  146. void SessionDescription::AddContent(ContentInfo&& content) {
  147. if (extmap_allow_mixed()) {
  148. // Mixed support on session level overrides setting on media level.
  149. content.media_description()->set_extmap_allow_mixed_enum(
  150. MediaContentDescription::kSession);
  151. }
  152. contents_.push_back(std::move(content));
  153. }
  154. bool SessionDescription::RemoveContentByName(const std::string& name) {
  155. for (ContentInfos::iterator content = contents_.begin();
  156. content != contents_.end(); ++content) {
  157. if (content->name == name) {
  158. contents_.erase(content);
  159. return true;
  160. }
  161. }
  162. return false;
  163. }
  164. void SessionDescription::AddTransportInfo(const TransportInfo& transport_info) {
  165. transport_infos_.push_back(transport_info);
  166. }
  167. bool SessionDescription::RemoveTransportInfoByName(const std::string& name) {
  168. for (TransportInfos::iterator transport_info = transport_infos_.begin();
  169. transport_info != transport_infos_.end(); ++transport_info) {
  170. if (transport_info->content_name == name) {
  171. transport_infos_.erase(transport_info);
  172. return true;
  173. }
  174. }
  175. return false;
  176. }
  177. const TransportInfo* SessionDescription::GetTransportInfoByName(
  178. const std::string& name) const {
  179. for (TransportInfos::const_iterator iter = transport_infos_.begin();
  180. iter != transport_infos_.end(); ++iter) {
  181. if (iter->content_name == name) {
  182. return &(*iter);
  183. }
  184. }
  185. return NULL;
  186. }
  187. TransportInfo* SessionDescription::GetTransportInfoByName(
  188. const std::string& name) {
  189. for (TransportInfos::iterator iter = transport_infos_.begin();
  190. iter != transport_infos_.end(); ++iter) {
  191. if (iter->content_name == name) {
  192. return &(*iter);
  193. }
  194. }
  195. return NULL;
  196. }
  197. void SessionDescription::RemoveGroupByName(const std::string& name) {
  198. for (ContentGroups::iterator iter = content_groups_.begin();
  199. iter != content_groups_.end(); ++iter) {
  200. if (iter->semantics() == name) {
  201. content_groups_.erase(iter);
  202. break;
  203. }
  204. }
  205. }
  206. bool SessionDescription::HasGroup(const std::string& name) const {
  207. for (ContentGroups::const_iterator iter = content_groups_.begin();
  208. iter != content_groups_.end(); ++iter) {
  209. if (iter->semantics() == name) {
  210. return true;
  211. }
  212. }
  213. return false;
  214. }
  215. const ContentGroup* SessionDescription::GetGroupByName(
  216. const std::string& name) const {
  217. for (ContentGroups::const_iterator iter = content_groups_.begin();
  218. iter != content_groups_.end(); ++iter) {
  219. if (iter->semantics() == name) {
  220. return &(*iter);
  221. }
  222. }
  223. return NULL;
  224. }
  225. ContentInfo::~ContentInfo() {
  226. if (description_ && description_.get() != description) {
  227. // If description_ is null, we assume that a move operator
  228. // has been applied.
  229. RTC_LOG(LS_ERROR) << "ContentInfo::description has been updated by "
  230. << "assignment. This usage is deprecated.";
  231. description_.reset(description); // ensure that it is destroyed.
  232. }
  233. }
  234. // Copy operator.
  235. ContentInfo::ContentInfo(const ContentInfo& o)
  236. : name(o.name),
  237. type(o.type),
  238. rejected(o.rejected),
  239. bundle_only(o.bundle_only),
  240. description_(o.description_->Clone()),
  241. description(description_.get()) {}
  242. ContentInfo& ContentInfo::operator=(const ContentInfo& o) {
  243. name = o.name;
  244. type = o.type;
  245. rejected = o.rejected;
  246. bundle_only = o.bundle_only;
  247. description_ = o.description_->Clone();
  248. description = description_.get();
  249. return *this;
  250. }
  251. const MediaContentDescription* ContentInfo::media_description() const {
  252. if (description_.get() != description) {
  253. // Someone's updated |description|, or used a move operator
  254. // on the record.
  255. RTC_LOG(LS_ERROR) << "ContentInfo::description has been updated by "
  256. << "assignment. This usage is deprecated.";
  257. const_cast<ContentInfo*>(this)->description_.reset(description);
  258. }
  259. return description_.get();
  260. }
  261. MediaContentDescription* ContentInfo::media_description() {
  262. if (description_.get() != description) {
  263. // Someone's updated |description|, or used a move operator
  264. // on the record.
  265. RTC_LOG(LS_ERROR) << "ContentInfo::description has been updated by "
  266. << "assignment. This usage is deprecated.";
  267. description_.reset(description);
  268. }
  269. return description_.get();
  270. }
  271. } // namespace cricket