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 | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 11 | #include "pc/session_description.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 | |
Steve Anton | 64b626b | 2019-01-28 17:25:26 -0800 | [diff] [blame] | 15 | #include "absl/algorithm/container.h" |
Harald Alvestrand | 4d7160e | 2019-04-12 07:01:29 +0200 | [diff] [blame] | 16 | #include "absl/memory/memory.h" |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 17 | #include "rtc_base/checks.h" |
| 18 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 19 | namespace cricket { |
Steve Anton | 4ab68ee | 2017-12-19 14:26:11 -0800 | [diff] [blame] | 20 | namespace { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 21 | |
Steve Anton | 4ab68ee | 2017-12-19 14:26:11 -0800 | [diff] [blame] | 22 | ContentInfo* FindContentInfoByName(ContentInfos* contents, |
| 23 | const std::string& name) { |
| 24 | RTC_DCHECK(contents); |
| 25 | for (ContentInfo& content : *contents) { |
| 26 | if (content.name == name) { |
| 27 | return &content; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 28 | } |
| 29 | } |
Steve Anton | 4ab68ee | 2017-12-19 14:26:11 -0800 | [diff] [blame] | 30 | return nullptr; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 31 | } |
| 32 | |
Steve Anton | 4ab68ee | 2017-12-19 14:26:11 -0800 | [diff] [blame] | 33 | } // namespace |
| 34 | |
| 35 | const ContentInfo* FindContentInfoByName(const ContentInfos& contents, |
| 36 | const std::string& name) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 37 | for (ContentInfos::const_iterator content = contents.begin(); |
| 38 | content != contents.end(); ++content) { |
| 39 | if (content->name == name) { |
| 40 | return &(*content); |
| 41 | } |
| 42 | } |
| 43 | return NULL; |
| 44 | } |
| 45 | |
Steve Anton | 4ab68ee | 2017-12-19 14:26:11 -0800 | [diff] [blame] | 46 | const ContentInfo* FindContentInfoByType(const ContentInfos& contents, |
Steve Anton | 5adfafd | 2017-12-20 16:34:00 -0800 | [diff] [blame] | 47 | MediaProtocolType type) { |
| 48 | for (const auto& content : contents) { |
| 49 | if (content.type == type) { |
| 50 | return &content; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 51 | } |
| 52 | } |
Steve Anton | 5adfafd | 2017-12-20 16:34:00 -0800 | [diff] [blame] | 53 | return nullptr; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 54 | } |
| 55 | |
Steve Anton | d3ea999 | 2017-10-31 12:38:23 -0700 | [diff] [blame] | 56 | ContentGroup::ContentGroup(const std::string& semantics) |
| 57 | : semantics_(semantics) {} |
| 58 | |
| 59 | ContentGroup::ContentGroup(const ContentGroup&) = default; |
| 60 | ContentGroup::ContentGroup(ContentGroup&&) = default; |
| 61 | ContentGroup& ContentGroup::operator=(const ContentGroup&) = default; |
| 62 | ContentGroup& ContentGroup::operator=(ContentGroup&&) = default; |
| 63 | ContentGroup::~ContentGroup() = default; |
| 64 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 65 | const std::string* ContentGroup::FirstContentName() const { |
| 66 | return (!content_names_.empty()) ? &(*content_names_.begin()) : NULL; |
| 67 | } |
| 68 | |
| 69 | bool ContentGroup::HasContentName(const std::string& content_name) const { |
Steve Anton | 64b626b | 2019-01-28 17:25:26 -0800 | [diff] [blame] | 70 | return absl::c_linear_search(content_names_, content_name); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | void ContentGroup::AddContentName(const std::string& content_name) { |
| 74 | if (!HasContentName(content_name)) { |
| 75 | content_names_.push_back(content_name); |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | bool ContentGroup::RemoveContentName(const std::string& content_name) { |
Steve Anton | 64b626b | 2019-01-28 17:25:26 -0800 | [diff] [blame] | 80 | ContentNames::iterator iter = absl::c_find(content_names_, content_name); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 81 | if (iter == content_names_.end()) { |
| 82 | return false; |
| 83 | } |
| 84 | content_names_.erase(iter); |
| 85 | return true; |
| 86 | } |
| 87 | |
Steve Anton | d3ea999 | 2017-10-31 12:38:23 -0700 | [diff] [blame] | 88 | SessionDescription::SessionDescription() = default; |
Steve Anton | d3ea999 | 2017-10-31 12:38:23 -0700 | [diff] [blame] | 89 | SessionDescription::SessionDescription(const SessionDescription&) = default; |
| 90 | |
Jonas Olsson | a4d8737 | 2019-07-05 19:08:33 +0200 | [diff] [blame] | 91 | SessionDescription::~SessionDescription() {} |
Steve Anton | d3ea999 | 2017-10-31 12:38:23 -0700 | [diff] [blame] | 92 | |
Harald Alvestrand | 4d7160e | 2019-04-12 07:01:29 +0200 | [diff] [blame] | 93 | std::unique_ptr<SessionDescription> SessionDescription::Clone() const { |
Harald Alvestrand | 1716d39 | 2019-06-03 20:35:45 +0200 | [diff] [blame] | 94 | // Copy using the private copy constructor. |
| 95 | // This will clone the descriptions using ContentInfo's copy constructor. |
| 96 | return absl::WrapUnique(new SessionDescription(*this)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | const ContentInfo* SessionDescription::GetContentByName( |
| 100 | const std::string& name) const { |
| 101 | return FindContentInfoByName(contents_, name); |
| 102 | } |
| 103 | |
Steve Anton | 4ab68ee | 2017-12-19 14:26:11 -0800 | [diff] [blame] | 104 | ContentInfo* SessionDescription::GetContentByName(const std::string& name) { |
| 105 | return FindContentInfoByName(&contents_, name); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 106 | } |
| 107 | |
Steve Anton | b1c1de1 | 2017-12-21 15:14:30 -0800 | [diff] [blame] | 108 | const MediaContentDescription* SessionDescription::GetContentDescriptionByName( |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 109 | const std::string& name) const { |
| 110 | const ContentInfo* cinfo = FindContentInfoByName(contents_, name); |
| 111 | if (cinfo == NULL) { |
| 112 | return NULL; |
| 113 | } |
| 114 | |
Steve Anton | b1c1de1 | 2017-12-21 15:14:30 -0800 | [diff] [blame] | 115 | return cinfo->media_description(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 116 | } |
| 117 | |
Steve Anton | b1c1de1 | 2017-12-21 15:14:30 -0800 | [diff] [blame] | 118 | MediaContentDescription* SessionDescription::GetContentDescriptionByName( |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 119 | const std::string& name) { |
Steve Anton | 4ab68ee | 2017-12-19 14:26:11 -0800 | [diff] [blame] | 120 | ContentInfo* cinfo = FindContentInfoByName(&contents_, name); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 121 | if (cinfo == NULL) { |
| 122 | return NULL; |
| 123 | } |
| 124 | |
Steve Anton | b1c1de1 | 2017-12-21 15:14:30 -0800 | [diff] [blame] | 125 | return cinfo->media_description(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | const ContentInfo* SessionDescription::FirstContentByType( |
Steve Anton | 5adfafd | 2017-12-20 16:34:00 -0800 | [diff] [blame] | 129 | MediaProtocolType type) const { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 130 | return FindContentInfoByType(contents_, type); |
| 131 | } |
| 132 | |
| 133 | const ContentInfo* SessionDescription::FirstContent() const { |
| 134 | return (contents_.empty()) ? NULL : &(*contents_.begin()); |
| 135 | } |
| 136 | |
Harald Alvestrand | 1716d39 | 2019-06-03 20:35:45 +0200 | [diff] [blame] | 137 | void SessionDescription::AddContent( |
| 138 | const std::string& name, |
| 139 | MediaProtocolType type, |
| 140 | std::unique_ptr<MediaContentDescription> description) { |
Steve Anton | 5adfafd | 2017-12-20 16:34:00 -0800 | [diff] [blame] | 141 | ContentInfo content(type); |
| 142 | content.name = name; |
Harald Alvestrand | 1716d39 | 2019-06-03 20:35:45 +0200 | [diff] [blame] | 143 | content.set_media_description(std::move(description)); |
| 144 | AddContent(std::move(content)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 145 | } |
| 146 | |
Harald Alvestrand | 1716d39 | 2019-06-03 20:35:45 +0200 | [diff] [blame] | 147 | void SessionDescription::AddContent( |
| 148 | const std::string& name, |
| 149 | MediaProtocolType type, |
| 150 | bool rejected, |
| 151 | std::unique_ptr<MediaContentDescription> description) { |
Steve Anton | 5adfafd | 2017-12-20 16:34:00 -0800 | [diff] [blame] | 152 | ContentInfo content(type); |
| 153 | content.name = name; |
| 154 | content.rejected = rejected; |
Harald Alvestrand | 1716d39 | 2019-06-03 20:35:45 +0200 | [diff] [blame] | 155 | content.set_media_description(std::move(description)); |
| 156 | AddContent(std::move(content)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 157 | } |
| 158 | |
Harald Alvestrand | 1716d39 | 2019-06-03 20:35:45 +0200 | [diff] [blame] | 159 | void SessionDescription::AddContent( |
| 160 | const std::string& name, |
| 161 | MediaProtocolType type, |
| 162 | bool rejected, |
| 163 | bool bundle_only, |
| 164 | std::unique_ptr<MediaContentDescription> description) { |
Steve Anton | 5adfafd | 2017-12-20 16:34:00 -0800 | [diff] [blame] | 165 | ContentInfo content(type); |
| 166 | content.name = name; |
| 167 | content.rejected = rejected; |
| 168 | content.bundle_only = bundle_only; |
Harald Alvestrand | 1716d39 | 2019-06-03 20:35:45 +0200 | [diff] [blame] | 169 | content.set_media_description(std::move(description)); |
| 170 | AddContent(std::move(content)); |
Johannes Kron | 9ac3c91 | 2018-10-12 10:54:26 +0200 | [diff] [blame] | 171 | } |
| 172 | |
Harald Alvestrand | 1716d39 | 2019-06-03 20:35:45 +0200 | [diff] [blame] | 173 | void SessionDescription::AddContent(ContentInfo&& content) { |
Johannes Kron | 9581bc4 | 2018-10-23 10:17:39 +0200 | [diff] [blame] | 174 | if (extmap_allow_mixed()) { |
Johannes Kron | 9ac3c91 | 2018-10-12 10:54:26 +0200 | [diff] [blame] | 175 | // Mixed support on session level overrides setting on media level. |
Harald Alvestrand | 1716d39 | 2019-06-03 20:35:45 +0200 | [diff] [blame] | 176 | content.media_description()->set_extmap_allow_mixed_enum( |
Johannes Kron | 9ac3c91 | 2018-10-12 10:54:26 +0200 | [diff] [blame] | 177 | MediaContentDescription::kSession); |
| 178 | } |
Harald Alvestrand | 1716d39 | 2019-06-03 20:35:45 +0200 | [diff] [blame] | 179 | contents_.push_back(std::move(content)); |
deadbeef | 25ed435 | 2016-12-12 18:37:36 -0800 | [diff] [blame] | 180 | } |
| 181 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 182 | bool SessionDescription::RemoveContentByName(const std::string& name) { |
| 183 | for (ContentInfos::iterator content = contents_.begin(); |
| 184 | content != contents_.end(); ++content) { |
| 185 | if (content->name == name) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 186 | contents_.erase(content); |
| 187 | return true; |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | return false; |
| 192 | } |
| 193 | |
Steve Anton | 06817cd | 2018-12-18 15:55:30 -0800 | [diff] [blame] | 194 | void SessionDescription::AddTransportInfo(const TransportInfo& transport_info) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 195 | transport_infos_.push_back(transport_info); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | bool SessionDescription::RemoveTransportInfoByName(const std::string& name) { |
| 199 | for (TransportInfos::iterator transport_info = transport_infos_.begin(); |
| 200 | transport_info != transport_infos_.end(); ++transport_info) { |
| 201 | if (transport_info->content_name == name) { |
| 202 | transport_infos_.erase(transport_info); |
| 203 | return true; |
| 204 | } |
| 205 | } |
| 206 | return false; |
| 207 | } |
| 208 | |
| 209 | const TransportInfo* SessionDescription::GetTransportInfoByName( |
| 210 | const std::string& name) const { |
| 211 | for (TransportInfos::const_iterator iter = transport_infos_.begin(); |
| 212 | iter != transport_infos_.end(); ++iter) { |
| 213 | if (iter->content_name == name) { |
| 214 | return &(*iter); |
| 215 | } |
| 216 | } |
| 217 | return NULL; |
| 218 | } |
| 219 | |
| 220 | TransportInfo* SessionDescription::GetTransportInfoByName( |
| 221 | const std::string& name) { |
| 222 | for (TransportInfos::iterator iter = transport_infos_.begin(); |
| 223 | iter != transport_infos_.end(); ++iter) { |
| 224 | if (iter->content_name == name) { |
| 225 | return &(*iter); |
| 226 | } |
| 227 | } |
| 228 | return NULL; |
| 229 | } |
| 230 | |
| 231 | void SessionDescription::RemoveGroupByName(const std::string& name) { |
| 232 | for (ContentGroups::iterator iter = content_groups_.begin(); |
| 233 | iter != content_groups_.end(); ++iter) { |
| 234 | if (iter->semantics() == name) { |
| 235 | content_groups_.erase(iter); |
| 236 | break; |
| 237 | } |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | bool SessionDescription::HasGroup(const std::string& name) const { |
| 242 | for (ContentGroups::const_iterator iter = content_groups_.begin(); |
| 243 | iter != content_groups_.end(); ++iter) { |
| 244 | if (iter->semantics() == name) { |
| 245 | return true; |
| 246 | } |
| 247 | } |
| 248 | return false; |
| 249 | } |
| 250 | |
| 251 | const ContentGroup* SessionDescription::GetGroupByName( |
| 252 | const std::string& name) const { |
| 253 | for (ContentGroups::const_iterator iter = content_groups_.begin(); |
| 254 | iter != content_groups_.end(); ++iter) { |
| 255 | if (iter->semantics() == name) { |
| 256 | return &(*iter); |
| 257 | } |
| 258 | } |
| 259 | return NULL; |
| 260 | } |
| 261 | |
Henrik Boström | f8187e0 | 2021-04-26 21:04:26 +0200 | [diff] [blame^] | 262 | std::vector<const ContentGroup*> SessionDescription::GetGroupsByName( |
| 263 | const std::string& name) const { |
| 264 | std::vector<const ContentGroup*> content_groups; |
| 265 | for (const ContentGroup& content_group : content_groups_) { |
| 266 | if (content_group.semantics() == name) { |
| 267 | content_groups.push_back(&content_group); |
| 268 | } |
| 269 | } |
| 270 | return content_groups; |
| 271 | } |
| 272 | |
Harald Alvestrand | 1716d39 | 2019-06-03 20:35:45 +0200 | [diff] [blame] | 273 | ContentInfo::~ContentInfo() { |
Harald Alvestrand | 1716d39 | 2019-06-03 20:35:45 +0200 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | // Copy operator. |
| 277 | ContentInfo::ContentInfo(const ContentInfo& o) |
| 278 | : name(o.name), |
| 279 | type(o.type), |
| 280 | rejected(o.rejected), |
| 281 | bundle_only(o.bundle_only), |
Harald Alvestrand | 8e7d4bf | 2020-02-26 09:32:30 +0100 | [diff] [blame] | 282 | description_(o.description_->Clone()) {} |
Harald Alvestrand | 1716d39 | 2019-06-03 20:35:45 +0200 | [diff] [blame] | 283 | |
| 284 | ContentInfo& ContentInfo::operator=(const ContentInfo& o) { |
| 285 | name = o.name; |
| 286 | type = o.type; |
| 287 | rejected = o.rejected; |
| 288 | bundle_only = o.bundle_only; |
| 289 | description_ = o.description_->Clone(); |
Harald Alvestrand | 1716d39 | 2019-06-03 20:35:45 +0200 | [diff] [blame] | 290 | return *this; |
| 291 | } |
| 292 | |
| 293 | const MediaContentDescription* ContentInfo::media_description() const { |
Harald Alvestrand | 1716d39 | 2019-06-03 20:35:45 +0200 | [diff] [blame] | 294 | return description_.get(); |
| 295 | } |
| 296 | |
| 297 | MediaContentDescription* ContentInfo::media_description() { |
Harald Alvestrand | 1716d39 | 2019-06-03 20:35:45 +0200 | [diff] [blame] | 298 | return description_.get(); |
| 299 | } |
| 300 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 301 | } // namespace cricket |