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