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; |
Steve Anton | d3ea999 | 2017-10-31 12:38:23 -0700 | [diff] [blame] | 87 | SessionDescription::SessionDescription(const SessionDescription&) = default; |
| 88 | |
| 89 | SessionDescription::~SessionDescription() { |
| 90 | for (ContentInfos::iterator content = contents_.begin(); |
| 91 | content != contents_.end(); ++content) { |
| 92 | delete content->description; |
| 93 | } |
| 94 | } |
| 95 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 96 | SessionDescription* SessionDescription::Copy() const { |
| 97 | SessionDescription* copy = new SessionDescription(*this); |
| 98 | // Copy all ContentDescriptions. |
| 99 | for (ContentInfos::iterator content = copy->contents_.begin(); |
Steve Anton | 4ab68ee | 2017-12-19 14:26:11 -0800 | [diff] [blame] | 100 | content != copy->contents().end(); ++content) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 101 | content->description = content->description->Copy(); |
| 102 | } |
| 103 | return copy; |
| 104 | } |
| 105 | |
| 106 | const ContentInfo* SessionDescription::GetContentByName( |
| 107 | const std::string& name) const { |
| 108 | return FindContentInfoByName(contents_, name); |
| 109 | } |
| 110 | |
Steve Anton | 4ab68ee | 2017-12-19 14:26:11 -0800 | [diff] [blame] | 111 | ContentInfo* SessionDescription::GetContentByName(const std::string& name) { |
| 112 | return FindContentInfoByName(&contents_, name); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 113 | } |
| 114 | |
Steve Anton | b1c1de1 | 2017-12-21 15:14:30 -0800 | [diff] [blame] | 115 | const MediaContentDescription* SessionDescription::GetContentDescriptionByName( |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 116 | const std::string& name) const { |
| 117 | const ContentInfo* cinfo = FindContentInfoByName(contents_, name); |
| 118 | if (cinfo == NULL) { |
| 119 | return NULL; |
| 120 | } |
| 121 | |
Steve Anton | b1c1de1 | 2017-12-21 15:14:30 -0800 | [diff] [blame] | 122 | return cinfo->media_description(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 123 | } |
| 124 | |
Steve Anton | b1c1de1 | 2017-12-21 15:14:30 -0800 | [diff] [blame] | 125 | MediaContentDescription* SessionDescription::GetContentDescriptionByName( |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 126 | const std::string& name) { |
Steve Anton | 4ab68ee | 2017-12-19 14:26:11 -0800 | [diff] [blame] | 127 | ContentInfo* cinfo = FindContentInfoByName(&contents_, name); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 128 | if (cinfo == NULL) { |
| 129 | return NULL; |
| 130 | } |
| 131 | |
Steve Anton | b1c1de1 | 2017-12-21 15:14:30 -0800 | [diff] [blame] | 132 | return cinfo->media_description(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | const ContentInfo* SessionDescription::FirstContentByType( |
Steve Anton | 5adfafd | 2017-12-20 16:34:00 -0800 | [diff] [blame] | 136 | MediaProtocolType type) const { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 137 | return FindContentInfoByType(contents_, type); |
| 138 | } |
| 139 | |
| 140 | const ContentInfo* SessionDescription::FirstContent() const { |
| 141 | return (contents_.empty()) ? NULL : &(*contents_.begin()); |
| 142 | } |
| 143 | |
| 144 | void SessionDescription::AddContent(const std::string& name, |
Steve Anton | 5adfafd | 2017-12-20 16:34:00 -0800 | [diff] [blame] | 145 | MediaProtocolType type, |
Steve Anton | b1c1de1 | 2017-12-21 15:14:30 -0800 | [diff] [blame] | 146 | MediaContentDescription* description) { |
Steve Anton | 5adfafd | 2017-12-20 16:34:00 -0800 | [diff] [blame] | 147 | ContentInfo content(type); |
| 148 | content.name = name; |
| 149 | content.description = description; |
Johannes Kron | 9ac3c91 | 2018-10-12 10:54:26 +0200 | [diff] [blame] | 150 | AddContent(&content); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | void SessionDescription::AddContent(const std::string& name, |
Steve Anton | 5adfafd | 2017-12-20 16:34:00 -0800 | [diff] [blame] | 154 | MediaProtocolType type, |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 155 | bool rejected, |
Steve Anton | b1c1de1 | 2017-12-21 15:14:30 -0800 | [diff] [blame] | 156 | MediaContentDescription* description) { |
Steve Anton | 5adfafd | 2017-12-20 16:34:00 -0800 | [diff] [blame] | 157 | ContentInfo content(type); |
| 158 | content.name = name; |
| 159 | content.rejected = rejected; |
| 160 | content.description = description; |
Johannes Kron | 9ac3c91 | 2018-10-12 10:54:26 +0200 | [diff] [blame] | 161 | AddContent(&content); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 162 | } |
| 163 | |
deadbeef | 25ed435 | 2016-12-12 18:37:36 -0800 | [diff] [blame] | 164 | void SessionDescription::AddContent(const std::string& name, |
Steve Anton | 5adfafd | 2017-12-20 16:34:00 -0800 | [diff] [blame] | 165 | MediaProtocolType type, |
deadbeef | 25ed435 | 2016-12-12 18:37:36 -0800 | [diff] [blame] | 166 | bool rejected, |
| 167 | bool bundle_only, |
Steve Anton | b1c1de1 | 2017-12-21 15:14:30 -0800 | [diff] [blame] | 168 | MediaContentDescription* description) { |
Steve Anton | 5adfafd | 2017-12-20 16:34:00 -0800 | [diff] [blame] | 169 | ContentInfo content(type); |
| 170 | content.name = name; |
| 171 | content.rejected = rejected; |
| 172 | content.bundle_only = bundle_only; |
| 173 | content.description = description; |
Johannes Kron | 9ac3c91 | 2018-10-12 10:54:26 +0200 | [diff] [blame] | 174 | AddContent(&content); |
| 175 | } |
| 176 | |
| 177 | void SessionDescription::AddContent(ContentInfo* content) { |
Johannes Kron | 9581bc4 | 2018-10-23 10:17:39 +0200 | [diff] [blame^] | 178 | if (extmap_allow_mixed()) { |
Johannes Kron | 9ac3c91 | 2018-10-12 10:54:26 +0200 | [diff] [blame] | 179 | // Mixed support on session level overrides setting on media level. |
Johannes Kron | 9581bc4 | 2018-10-23 10:17:39 +0200 | [diff] [blame^] | 180 | content->description->set_extmap_allow_mixed_enum( |
Johannes Kron | 9ac3c91 | 2018-10-12 10:54:26 +0200 | [diff] [blame] | 181 | MediaContentDescription::kSession); |
| 182 | } |
| 183 | contents_.push_back(std::move(*content)); |
deadbeef | 25ed435 | 2016-12-12 18:37:36 -0800 | [diff] [blame] | 184 | } |
| 185 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 186 | bool SessionDescription::RemoveContentByName(const std::string& name) { |
| 187 | for (ContentInfos::iterator content = contents_.begin(); |
| 188 | content != contents_.end(); ++content) { |
| 189 | if (content->name == name) { |
| 190 | delete content->description; |
| 191 | contents_.erase(content); |
| 192 | return true; |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | return false; |
| 197 | } |
| 198 | |
| 199 | bool SessionDescription::AddTransportInfo(const TransportInfo& transport_info) { |
| 200 | if (GetTransportInfoByName(transport_info.content_name) != NULL) { |
| 201 | return false; |
| 202 | } |
| 203 | transport_infos_.push_back(transport_info); |
| 204 | return true; |
| 205 | } |
| 206 | |
| 207 | bool SessionDescription::RemoveTransportInfoByName(const std::string& name) { |
| 208 | for (TransportInfos::iterator transport_info = transport_infos_.begin(); |
| 209 | transport_info != transport_infos_.end(); ++transport_info) { |
| 210 | if (transport_info->content_name == name) { |
| 211 | transport_infos_.erase(transport_info); |
| 212 | return true; |
| 213 | } |
| 214 | } |
| 215 | return false; |
| 216 | } |
| 217 | |
| 218 | const TransportInfo* SessionDescription::GetTransportInfoByName( |
| 219 | const std::string& name) const { |
| 220 | for (TransportInfos::const_iterator iter = transport_infos_.begin(); |
| 221 | iter != transport_infos_.end(); ++iter) { |
| 222 | if (iter->content_name == name) { |
| 223 | return &(*iter); |
| 224 | } |
| 225 | } |
| 226 | return NULL; |
| 227 | } |
| 228 | |
| 229 | TransportInfo* SessionDescription::GetTransportInfoByName( |
| 230 | const std::string& name) { |
| 231 | for (TransportInfos::iterator iter = transport_infos_.begin(); |
| 232 | iter != transport_infos_.end(); ++iter) { |
| 233 | if (iter->content_name == name) { |
| 234 | return &(*iter); |
| 235 | } |
| 236 | } |
| 237 | return NULL; |
| 238 | } |
| 239 | |
| 240 | void SessionDescription::RemoveGroupByName(const std::string& name) { |
| 241 | for (ContentGroups::iterator iter = content_groups_.begin(); |
| 242 | iter != content_groups_.end(); ++iter) { |
| 243 | if (iter->semantics() == name) { |
| 244 | content_groups_.erase(iter); |
| 245 | break; |
| 246 | } |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | bool SessionDescription::HasGroup(const std::string& name) const { |
| 251 | for (ContentGroups::const_iterator iter = content_groups_.begin(); |
| 252 | iter != content_groups_.end(); ++iter) { |
| 253 | if (iter->semantics() == name) { |
| 254 | return true; |
| 255 | } |
| 256 | } |
| 257 | return false; |
| 258 | } |
| 259 | |
| 260 | const ContentGroup* SessionDescription::GetGroupByName( |
| 261 | const std::string& name) const { |
| 262 | for (ContentGroups::const_iterator iter = content_groups_.begin(); |
| 263 | iter != content_groups_.end(); ++iter) { |
| 264 | if (iter->semantics() == name) { |
| 265 | return &(*iter); |
| 266 | } |
| 267 | } |
| 268 | return NULL; |
| 269 | } |
| 270 | |
| 271 | } // namespace cricket |