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