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