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.

228 lines
7.2 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * libjingle
  3. * Copyright 2010, Google Inc.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice,
  9. * this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright notice,
  11. * this list of conditions and the following disclaimer in the documentation
  12. * and/or other materials provided with the distribution.
  13. * 3. The name of the author may not be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  17. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  18. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
  19. * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  20. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  21. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  22. * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  23. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  24. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  25. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #include "talk/p2p/base/sessiondescription.h"
  28. #include "talk/xmllite/xmlelement.h"
  29. namespace cricket {
  30. ContentInfo* FindContentInfoByName(
  31. ContentInfos& contents, const std::string& name) {
  32. for (ContentInfos::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* FindContentInfoByName(
  41. const ContentInfos& contents, const std::string& name) {
  42. for (ContentInfos::const_iterator content = contents.begin();
  43. content != contents.end(); ++content) {
  44. if (content->name == name) {
  45. return &(*content);
  46. }
  47. }
  48. return NULL;
  49. }
  50. const ContentInfo* FindContentInfoByType(
  51. const ContentInfos& contents, const std::string& type) {
  52. for (ContentInfos::const_iterator content = contents.begin();
  53. content != contents.end(); ++content) {
  54. if (content->type == type) {
  55. return &(*content);
  56. }
  57. }
  58. return NULL;
  59. }
  60. const std::string* ContentGroup::FirstContentName() const {
  61. return (!content_names_.empty()) ? &(*content_names_.begin()) : NULL;
  62. }
  63. bool ContentGroup::HasContentName(const std::string& content_name) const {
  64. return (std::find(content_names_.begin(), content_names_.end(),
  65. content_name) != content_names_.end());
  66. }
  67. void ContentGroup::AddContentName(const std::string& content_name) {
  68. if (!HasContentName(content_name)) {
  69. content_names_.push_back(content_name);
  70. }
  71. }
  72. bool ContentGroup::RemoveContentName(const std::string& content_name) {
  73. ContentNames::iterator iter = std::find(
  74. content_names_.begin(), content_names_.end(), content_name);
  75. if (iter == content_names_.end()) {
  76. return false;
  77. }
  78. content_names_.erase(iter);
  79. return true;
  80. }
  81. SessionDescription* SessionDescription::Copy() const {
  82. SessionDescription* copy = new SessionDescription(*this);
  83. // Copy all ContentDescriptions.
  84. for (ContentInfos::iterator content = copy->contents_.begin();
  85. content != copy->contents().end(); ++content) {
  86. content->description = content->description->Copy();
  87. }
  88. return copy;
  89. }
  90. const ContentInfo* SessionDescription::GetContentByName(
  91. const std::string& name) const {
  92. return FindContentInfoByName(contents_, name);
  93. }
  94. ContentInfo* SessionDescription::GetContentByName(
  95. const std::string& name) {
  96. return FindContentInfoByName(contents_, name);
  97. }
  98. const ContentDescription* SessionDescription::GetContentDescriptionByName(
  99. const std::string& name) const {
  100. const ContentInfo* cinfo = FindContentInfoByName(contents_, name);
  101. if (cinfo == NULL) {
  102. return NULL;
  103. }
  104. return cinfo->description;
  105. }
  106. ContentDescription* SessionDescription::GetContentDescriptionByName(
  107. const std::string& name) {
  108. ContentInfo* cinfo = FindContentInfoByName(contents_, name);
  109. if (cinfo == NULL) {
  110. return NULL;
  111. }
  112. return cinfo->description;
  113. }
  114. const ContentInfo* SessionDescription::FirstContentByType(
  115. const std::string& type) const {
  116. return FindContentInfoByType(contents_, type);
  117. }
  118. const ContentInfo* SessionDescription::FirstContent() const {
  119. return (contents_.empty()) ? NULL : &(*contents_.begin());
  120. }
  121. void SessionDescription::AddContent(const std::string& name,
  122. const std::string& type,
  123. ContentDescription* description) {
  124. contents_.push_back(ContentInfo(name, type, description));
  125. }
  126. void SessionDescription::AddContent(const std::string& name,
  127. const std::string& type,
  128. bool rejected,
  129. ContentDescription* description) {
  130. contents_.push_back(ContentInfo(name, type, rejected, description));
  131. }
  132. bool SessionDescription::RemoveContentByName(const std::string& name) {
  133. for (ContentInfos::iterator content = contents_.begin();
  134. content != contents_.end(); ++content) {
  135. if (content->name == name) {
  136. delete content->description;
  137. contents_.erase(content);
  138. return true;
  139. }
  140. }
  141. return false;
  142. }
  143. bool SessionDescription::AddTransportInfo(const TransportInfo& transport_info) {
  144. if (GetTransportInfoByName(transport_info.content_name) != NULL) {
  145. return false;
  146. }
  147. transport_infos_.push_back(transport_info);
  148. return true;
  149. }
  150. bool SessionDescription::RemoveTransportInfoByName(const std::string& name) {
  151. for (TransportInfos::iterator transport_info = transport_infos_.begin();
  152. transport_info != transport_infos_.end(); ++transport_info) {
  153. if (transport_info->content_name == name) {
  154. transport_infos_.erase(transport_info);
  155. return true;
  156. }
  157. }
  158. return false;
  159. }
  160. const TransportInfo* SessionDescription::GetTransportInfoByName(
  161. const std::string& name) const {
  162. for (TransportInfos::const_iterator iter = transport_infos_.begin();
  163. iter != transport_infos_.end(); ++iter) {
  164. if (iter->content_name == name) {
  165. return &(*iter);
  166. }
  167. }
  168. return NULL;
  169. }
  170. void SessionDescription::RemoveGroupByName(const std::string& name) {
  171. for (ContentGroups::iterator iter = content_groups_.begin();
  172. iter != content_groups_.end(); ++iter) {
  173. if (iter->semantics() == name) {
  174. content_groups_.erase(iter);
  175. break;
  176. }
  177. }
  178. }
  179. bool SessionDescription::HasGroup(const std::string& name) const {
  180. for (ContentGroups::const_iterator iter = content_groups_.begin();
  181. iter != content_groups_.end(); ++iter) {
  182. if (iter->semantics() == name) {
  183. return true;
  184. }
  185. }
  186. return false;
  187. }
  188. const ContentGroup* SessionDescription::GetGroupByName(
  189. const std::string& name) const {
  190. for (ContentGroups::const_iterator iter = content_groups_.begin();
  191. iter != content_groups_.end(); ++iter) {
  192. if (iter->semantics() == name) {
  193. return &(*iter);
  194. }
  195. }
  196. return NULL;
  197. }
  198. } // namespace cricket