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