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