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