henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 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 | |
Steve Anton | 4ab68ee | 2017-12-19 14:26:11 -0800 | [diff] [blame^] | 11 | #include "pc/sessiondescription.h" |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 12 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 13 | namespace cricket { |
Steve Anton | 4ab68ee | 2017-12-19 14:26:11 -0800 | [diff] [blame^] | 14 | namespace { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 15 | |
Steve Anton | 4ab68ee | 2017-12-19 14:26:11 -0800 | [diff] [blame^] | 16 | ContentInfo* FindContentInfoByName(ContentInfos* contents, |
| 17 | const std::string& name) { |
| 18 | RTC_DCHECK(contents); |
| 19 | for (ContentInfo& content : *contents) { |
| 20 | if (content.name == name) { |
| 21 | return &content; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 22 | } |
| 23 | } |
Steve Anton | 4ab68ee | 2017-12-19 14:26:11 -0800 | [diff] [blame^] | 24 | return nullptr; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 25 | } |
| 26 | |
Steve Anton | 4ab68ee | 2017-12-19 14:26:11 -0800 | [diff] [blame^] | 27 | } // namespace |
| 28 | |
| 29 | const ContentInfo* FindContentInfoByName(const ContentInfos& contents, |
| 30 | const std::string& name) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 31 | for (ContentInfos::const_iterator content = contents.begin(); |
| 32 | content != contents.end(); ++content) { |
| 33 | if (content->name == name) { |
| 34 | return &(*content); |
| 35 | } |
| 36 | } |
| 37 | return NULL; |
| 38 | } |
| 39 | |
Steve Anton | 4ab68ee | 2017-12-19 14:26:11 -0800 | [diff] [blame^] | 40 | const ContentInfo* FindContentInfoByType(const ContentInfos& contents, |
| 41 | const std::string& type) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 42 | for (ContentInfos::const_iterator content = contents.begin(); |
| 43 | content != contents.end(); ++content) { |
| 44 | if (content->type == type) { |
| 45 | return &(*content); |
| 46 | } |
| 47 | } |
| 48 | return NULL; |
| 49 | } |
| 50 | |
Steve Anton | d3ea999 | 2017-10-31 12:38:23 -0700 | [diff] [blame] | 51 | ContentGroup::ContentGroup(const std::string& semantics) |
| 52 | : semantics_(semantics) {} |
| 53 | |
| 54 | ContentGroup::ContentGroup(const ContentGroup&) = default; |
| 55 | ContentGroup::ContentGroup(ContentGroup&&) = default; |
| 56 | ContentGroup& ContentGroup::operator=(const ContentGroup&) = default; |
| 57 | ContentGroup& ContentGroup::operator=(ContentGroup&&) = default; |
| 58 | ContentGroup::~ContentGroup() = default; |
| 59 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 60 | const std::string* ContentGroup::FirstContentName() const { |
| 61 | return (!content_names_.empty()) ? &(*content_names_.begin()) : NULL; |
| 62 | } |
| 63 | |
| 64 | bool ContentGroup::HasContentName(const std::string& content_name) const { |
| 65 | return (std::find(content_names_.begin(), content_names_.end(), |
| 66 | content_name) != content_names_.end()); |
| 67 | } |
| 68 | |
| 69 | void ContentGroup::AddContentName(const std::string& content_name) { |
| 70 | if (!HasContentName(content_name)) { |
| 71 | content_names_.push_back(content_name); |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | bool ContentGroup::RemoveContentName(const std::string& content_name) { |
Steve Anton | 4ab68ee | 2017-12-19 14:26:11 -0800 | [diff] [blame^] | 76 | ContentNames::iterator iter = |
| 77 | std::find(content_names_.begin(), content_names_.end(), content_name); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 78 | if (iter == content_names_.end()) { |
| 79 | return false; |
| 80 | } |
| 81 | content_names_.erase(iter); |
| 82 | return true; |
| 83 | } |
| 84 | |
Steve Anton | d3ea999 | 2017-10-31 12:38:23 -0700 | [diff] [blame] | 85 | SessionDescription::SessionDescription() = default; |
| 86 | |
| 87 | SessionDescription::SessionDescription(const ContentInfos& contents) |
| 88 | : contents_(contents) {} |
| 89 | |
| 90 | SessionDescription::SessionDescription(const ContentInfos& contents, |
| 91 | const ContentGroups& groups) |
| 92 | : contents_(contents), content_groups_(groups) {} |
| 93 | |
| 94 | SessionDescription::SessionDescription(const ContentInfos& contents, |
| 95 | const TransportInfos& transports, |
| 96 | const ContentGroups& groups) |
| 97 | : contents_(contents), |
| 98 | transport_infos_(transports), |
| 99 | content_groups_(groups) {} |
| 100 | |
| 101 | SessionDescription::SessionDescription(const SessionDescription&) = default; |
| 102 | |
| 103 | SessionDescription::~SessionDescription() { |
| 104 | for (ContentInfos::iterator content = contents_.begin(); |
| 105 | content != contents_.end(); ++content) { |
| 106 | delete content->description; |
| 107 | } |
| 108 | } |
| 109 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 110 | SessionDescription* SessionDescription::Copy() const { |
| 111 | SessionDescription* copy = new SessionDescription(*this); |
| 112 | // Copy all ContentDescriptions. |
| 113 | for (ContentInfos::iterator content = copy->contents_.begin(); |
Steve Anton | 4ab68ee | 2017-12-19 14:26:11 -0800 | [diff] [blame^] | 114 | content != copy->contents().end(); ++content) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 115 | content->description = content->description->Copy(); |
| 116 | } |
| 117 | return copy; |
| 118 | } |
| 119 | |
| 120 | const ContentInfo* SessionDescription::GetContentByName( |
| 121 | const std::string& name) const { |
| 122 | return FindContentInfoByName(contents_, name); |
| 123 | } |
| 124 | |
Steve Anton | 4ab68ee | 2017-12-19 14:26:11 -0800 | [diff] [blame^] | 125 | ContentInfo* SessionDescription::GetContentByName(const std::string& name) { |
| 126 | return FindContentInfoByName(&contents_, name); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | const ContentDescription* SessionDescription::GetContentDescriptionByName( |
| 130 | const std::string& name) const { |
| 131 | const ContentInfo* cinfo = FindContentInfoByName(contents_, name); |
| 132 | if (cinfo == NULL) { |
| 133 | return NULL; |
| 134 | } |
| 135 | |
| 136 | return cinfo->description; |
| 137 | } |
| 138 | |
| 139 | ContentDescription* SessionDescription::GetContentDescriptionByName( |
| 140 | const std::string& name) { |
Steve Anton | 4ab68ee | 2017-12-19 14:26:11 -0800 | [diff] [blame^] | 141 | ContentInfo* cinfo = FindContentInfoByName(&contents_, name); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 142 | if (cinfo == NULL) { |
| 143 | return NULL; |
| 144 | } |
| 145 | |
| 146 | return cinfo->description; |
| 147 | } |
| 148 | |
| 149 | const ContentInfo* SessionDescription::FirstContentByType( |
| 150 | const std::string& type) const { |
| 151 | return FindContentInfoByType(contents_, type); |
| 152 | } |
| 153 | |
| 154 | const ContentInfo* SessionDescription::FirstContent() const { |
| 155 | return (contents_.empty()) ? NULL : &(*contents_.begin()); |
| 156 | } |
| 157 | |
| 158 | void SessionDescription::AddContent(const std::string& name, |
| 159 | const std::string& type, |
| 160 | ContentDescription* description) { |
| 161 | contents_.push_back(ContentInfo(name, type, description)); |
| 162 | } |
| 163 | |
| 164 | void SessionDescription::AddContent(const std::string& name, |
| 165 | const std::string& type, |
| 166 | bool rejected, |
| 167 | ContentDescription* description) { |
| 168 | contents_.push_back(ContentInfo(name, type, rejected, description)); |
| 169 | } |
| 170 | |
deadbeef | 25ed435 | 2016-12-12 18:37:36 -0800 | [diff] [blame] | 171 | void SessionDescription::AddContent(const std::string& name, |
| 172 | const std::string& type, |
| 173 | bool rejected, |
| 174 | bool bundle_only, |
| 175 | ContentDescription* description) { |
| 176 | contents_.push_back( |
| 177 | ContentInfo(name, type, rejected, bundle_only, description)); |
| 178 | } |
| 179 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 180 | bool SessionDescription::RemoveContentByName(const std::string& name) { |
| 181 | for (ContentInfos::iterator content = contents_.begin(); |
| 182 | content != contents_.end(); ++content) { |
| 183 | if (content->name == name) { |
| 184 | delete content->description; |
| 185 | contents_.erase(content); |
| 186 | return true; |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | return false; |
| 191 | } |
| 192 | |
| 193 | bool SessionDescription::AddTransportInfo(const TransportInfo& transport_info) { |
| 194 | if (GetTransportInfoByName(transport_info.content_name) != NULL) { |
| 195 | return false; |
| 196 | } |
| 197 | transport_infos_.push_back(transport_info); |
| 198 | return true; |
| 199 | } |
| 200 | |
| 201 | bool SessionDescription::RemoveTransportInfoByName(const std::string& name) { |
| 202 | for (TransportInfos::iterator transport_info = transport_infos_.begin(); |
| 203 | transport_info != transport_infos_.end(); ++transport_info) { |
| 204 | if (transport_info->content_name == name) { |
| 205 | transport_infos_.erase(transport_info); |
| 206 | return true; |
| 207 | } |
| 208 | } |
| 209 | return false; |
| 210 | } |
| 211 | |
| 212 | const TransportInfo* SessionDescription::GetTransportInfoByName( |
| 213 | const std::string& name) const { |
| 214 | for (TransportInfos::const_iterator iter = transport_infos_.begin(); |
| 215 | iter != transport_infos_.end(); ++iter) { |
| 216 | if (iter->content_name == name) { |
| 217 | return &(*iter); |
| 218 | } |
| 219 | } |
| 220 | return NULL; |
| 221 | } |
| 222 | |
| 223 | TransportInfo* SessionDescription::GetTransportInfoByName( |
| 224 | const std::string& name) { |
| 225 | for (TransportInfos::iterator iter = transport_infos_.begin(); |
| 226 | iter != transport_infos_.end(); ++iter) { |
| 227 | if (iter->content_name == name) { |
| 228 | return &(*iter); |
| 229 | } |
| 230 | } |
| 231 | return NULL; |
| 232 | } |
| 233 | |
| 234 | void SessionDescription::RemoveGroupByName(const std::string& name) { |
| 235 | for (ContentGroups::iterator iter = content_groups_.begin(); |
| 236 | iter != content_groups_.end(); ++iter) { |
| 237 | if (iter->semantics() == name) { |
| 238 | content_groups_.erase(iter); |
| 239 | break; |
| 240 | } |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | bool SessionDescription::HasGroup(const std::string& name) const { |
| 245 | for (ContentGroups::const_iterator iter = content_groups_.begin(); |
| 246 | iter != content_groups_.end(); ++iter) { |
| 247 | if (iter->semantics() == name) { |
| 248 | return true; |
| 249 | } |
| 250 | } |
| 251 | return false; |
| 252 | } |
| 253 | |
| 254 | const ContentGroup* SessionDescription::GetGroupByName( |
| 255 | const std::string& name) const { |
| 256 | for (ContentGroups::const_iterator iter = content_groups_.begin(); |
| 257 | iter != content_groups_.end(); ++iter) { |
| 258 | if (iter->semantics() == name) { |
| 259 | return &(*iter); |
| 260 | } |
| 261 | } |
| 262 | return NULL; |
| 263 | } |
| 264 | |
| 265 | } // namespace cricket |