blob: 35b732d6498289a69cfa3eae42d5073b4ba5b38e [file] [log] [blame]
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001/*
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 Anton10542f22019-01-11 09:11:00 -080011#include "pc/session_description.h"
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000012
Steve Anton5adfafd2017-12-20 16:34:00 -080013#include <utility>
14
Steve Anton64b626b2019-01-28 17:25:26 -080015#include "absl/algorithm/container.h"
Harald Alvestrand4d7160e2019-04-12 07:01:29 +020016#include "absl/memory/memory.h"
Yves Gerey3e707812018-11-28 16:47:49 +010017#include "rtc_base/checks.h"
18
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000019namespace cricket {
Steve Anton4ab68ee2017-12-19 14:26:11 -080020namespace {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000021
Steve Anton4ab68ee2017-12-19 14:26:11 -080022ContentInfo* FindContentInfoByName(ContentInfos* contents,
23 const std::string& name) {
24 RTC_DCHECK(contents);
25 for (ContentInfo& content : *contents) {
26 if (content.name == name) {
27 return &content;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000028 }
29 }
Steve Anton4ab68ee2017-12-19 14:26:11 -080030 return nullptr;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000031}
32
Steve Anton4ab68ee2017-12-19 14:26:11 -080033} // namespace
34
35const ContentInfo* FindContentInfoByName(const ContentInfos& contents,
36 const std::string& name) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000037 for (ContentInfos::const_iterator content = contents.begin();
38 content != contents.end(); ++content) {
39 if (content->name == name) {
40 return &(*content);
41 }
42 }
43 return NULL;
44}
45
Steve Anton4ab68ee2017-12-19 14:26:11 -080046const ContentInfo* FindContentInfoByType(const ContentInfos& contents,
Steve Anton5adfafd2017-12-20 16:34:00 -080047 MediaProtocolType type) {
48 for (const auto& content : contents) {
49 if (content.type == type) {
50 return &content;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000051 }
52 }
Steve Anton5adfafd2017-12-20 16:34:00 -080053 return nullptr;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000054}
55
Steve Antond3ea9992017-10-31 12:38:23 -070056ContentGroup::ContentGroup(const std::string& semantics)
57 : semantics_(semantics) {}
58
59ContentGroup::ContentGroup(const ContentGroup&) = default;
60ContentGroup::ContentGroup(ContentGroup&&) = default;
61ContentGroup& ContentGroup::operator=(const ContentGroup&) = default;
62ContentGroup& ContentGroup::operator=(ContentGroup&&) = default;
63ContentGroup::~ContentGroup() = default;
64
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000065const std::string* ContentGroup::FirstContentName() const {
66 return (!content_names_.empty()) ? &(*content_names_.begin()) : NULL;
67}
68
69bool ContentGroup::HasContentName(const std::string& content_name) const {
Steve Anton64b626b2019-01-28 17:25:26 -080070 return absl::c_linear_search(content_names_, content_name);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000071}
72
73void ContentGroup::AddContentName(const std::string& content_name) {
74 if (!HasContentName(content_name)) {
75 content_names_.push_back(content_name);
76 }
77}
78
79bool ContentGroup::RemoveContentName(const std::string& content_name) {
Steve Anton64b626b2019-01-28 17:25:26 -080080 ContentNames::iterator iter = absl::c_find(content_names_, content_name);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000081 if (iter == content_names_.end()) {
82 return false;
83 }
84 content_names_.erase(iter);
85 return true;
86}
87
Steve Antond3ea9992017-10-31 12:38:23 -070088SessionDescription::SessionDescription() = default;
Steve Antond3ea9992017-10-31 12:38:23 -070089SessionDescription::SessionDescription(const SessionDescription&) = default;
90
Jonas Olssona4d87372019-07-05 19:08:33 +020091SessionDescription::~SessionDescription() {}
Steve Antond3ea9992017-10-31 12:38:23 -070092
Harald Alvestrand4d7160e2019-04-12 07:01:29 +020093std::unique_ptr<SessionDescription> SessionDescription::Clone() const {
Harald Alvestrand1716d392019-06-03 20:35:45 +020094 // Copy using the private copy constructor.
95 // This will clone the descriptions using ContentInfo's copy constructor.
96 return absl::WrapUnique(new SessionDescription(*this));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000097}
98
99const ContentInfo* SessionDescription::GetContentByName(
100 const std::string& name) const {
101 return FindContentInfoByName(contents_, name);
102}
103
Steve Anton4ab68ee2017-12-19 14:26:11 -0800104ContentInfo* SessionDescription::GetContentByName(const std::string& name) {
105 return FindContentInfoByName(&contents_, name);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000106}
107
Steve Antonb1c1de12017-12-21 15:14:30 -0800108const MediaContentDescription* SessionDescription::GetContentDescriptionByName(
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000109 const std::string& name) const {
110 const ContentInfo* cinfo = FindContentInfoByName(contents_, name);
111 if (cinfo == NULL) {
112 return NULL;
113 }
114
Steve Antonb1c1de12017-12-21 15:14:30 -0800115 return cinfo->media_description();
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000116}
117
Steve Antonb1c1de12017-12-21 15:14:30 -0800118MediaContentDescription* SessionDescription::GetContentDescriptionByName(
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000119 const std::string& name) {
Steve Anton4ab68ee2017-12-19 14:26:11 -0800120 ContentInfo* cinfo = FindContentInfoByName(&contents_, name);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000121 if (cinfo == NULL) {
122 return NULL;
123 }
124
Steve Antonb1c1de12017-12-21 15:14:30 -0800125 return cinfo->media_description();
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000126}
127
128const ContentInfo* SessionDescription::FirstContentByType(
Steve Anton5adfafd2017-12-20 16:34:00 -0800129 MediaProtocolType type) const {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000130 return FindContentInfoByType(contents_, type);
131}
132
133const ContentInfo* SessionDescription::FirstContent() const {
134 return (contents_.empty()) ? NULL : &(*contents_.begin());
135}
136
Harald Alvestrand1716d392019-06-03 20:35:45 +0200137void SessionDescription::AddContent(
138 const std::string& name,
139 MediaProtocolType type,
140 std::unique_ptr<MediaContentDescription> description) {
Steve Anton5adfafd2017-12-20 16:34:00 -0800141 ContentInfo content(type);
142 content.name = name;
Harald Alvestrand1716d392019-06-03 20:35:45 +0200143 content.set_media_description(std::move(description));
144 AddContent(std::move(content));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000145}
146
Harald Alvestrand1716d392019-06-03 20:35:45 +0200147void SessionDescription::AddContent(
148 const std::string& name,
149 MediaProtocolType type,
150 bool rejected,
151 std::unique_ptr<MediaContentDescription> description) {
Steve Anton5adfafd2017-12-20 16:34:00 -0800152 ContentInfo content(type);
153 content.name = name;
154 content.rejected = rejected;
Harald Alvestrand1716d392019-06-03 20:35:45 +0200155 content.set_media_description(std::move(description));
156 AddContent(std::move(content));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000157}
158
Harald Alvestrand1716d392019-06-03 20:35:45 +0200159void SessionDescription::AddContent(
160 const std::string& name,
161 MediaProtocolType type,
162 bool rejected,
163 bool bundle_only,
164 std::unique_ptr<MediaContentDescription> description) {
Steve Anton5adfafd2017-12-20 16:34:00 -0800165 ContentInfo content(type);
166 content.name = name;
167 content.rejected = rejected;
168 content.bundle_only = bundle_only;
Harald Alvestrand1716d392019-06-03 20:35:45 +0200169 content.set_media_description(std::move(description));
170 AddContent(std::move(content));
Johannes Kron9ac3c912018-10-12 10:54:26 +0200171}
172
Harald Alvestrand1716d392019-06-03 20:35:45 +0200173void SessionDescription::AddContent(ContentInfo&& content) {
Johannes Kron9581bc42018-10-23 10:17:39 +0200174 if (extmap_allow_mixed()) {
Johannes Kron9ac3c912018-10-12 10:54:26 +0200175 // Mixed support on session level overrides setting on media level.
Harald Alvestrand1716d392019-06-03 20:35:45 +0200176 content.media_description()->set_extmap_allow_mixed_enum(
Johannes Kron9ac3c912018-10-12 10:54:26 +0200177 MediaContentDescription::kSession);
178 }
Harald Alvestrand1716d392019-06-03 20:35:45 +0200179 contents_.push_back(std::move(content));
deadbeef25ed4352016-12-12 18:37:36 -0800180}
181
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000182bool SessionDescription::RemoveContentByName(const std::string& name) {
183 for (ContentInfos::iterator content = contents_.begin();
184 content != contents_.end(); ++content) {
185 if (content->name == name) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000186 contents_.erase(content);
187 return true;
188 }
189 }
190
191 return false;
192}
193
Steve Anton06817cd2018-12-18 15:55:30 -0800194void SessionDescription::AddTransportInfo(const TransportInfo& transport_info) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000195 transport_infos_.push_back(transport_info);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000196}
197
198bool SessionDescription::RemoveTransportInfoByName(const std::string& name) {
199 for (TransportInfos::iterator transport_info = transport_infos_.begin();
200 transport_info != transport_infos_.end(); ++transport_info) {
201 if (transport_info->content_name == name) {
202 transport_infos_.erase(transport_info);
203 return true;
204 }
205 }
206 return false;
207}
208
209const TransportInfo* SessionDescription::GetTransportInfoByName(
210 const std::string& name) const {
211 for (TransportInfos::const_iterator iter = transport_infos_.begin();
212 iter != transport_infos_.end(); ++iter) {
213 if (iter->content_name == name) {
214 return &(*iter);
215 }
216 }
217 return NULL;
218}
219
220TransportInfo* SessionDescription::GetTransportInfoByName(
221 const std::string& name) {
222 for (TransportInfos::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
231void SessionDescription::RemoveGroupByName(const std::string& name) {
232 for (ContentGroups::iterator iter = content_groups_.begin();
233 iter != content_groups_.end(); ++iter) {
234 if (iter->semantics() == name) {
235 content_groups_.erase(iter);
236 break;
237 }
238 }
239}
240
241bool SessionDescription::HasGroup(const std::string& name) const {
242 for (ContentGroups::const_iterator iter = content_groups_.begin();
243 iter != content_groups_.end(); ++iter) {
244 if (iter->semantics() == name) {
245 return true;
246 }
247 }
248 return false;
249}
250
251const ContentGroup* SessionDescription::GetGroupByName(
252 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 &(*iter);
257 }
258 }
259 return NULL;
260}
261
Henrik Boströmf8187e02021-04-26 21:04:26 +0200262std::vector<const ContentGroup*> SessionDescription::GetGroupsByName(
263 const std::string& name) const {
264 std::vector<const ContentGroup*> content_groups;
265 for (const ContentGroup& content_group : content_groups_) {
266 if (content_group.semantics() == name) {
267 content_groups.push_back(&content_group);
268 }
269 }
270 return content_groups;
271}
272
Harald Alvestrand1716d392019-06-03 20:35:45 +0200273ContentInfo::~ContentInfo() {
Harald Alvestrand1716d392019-06-03 20:35:45 +0200274}
275
276// Copy operator.
277ContentInfo::ContentInfo(const ContentInfo& o)
278 : name(o.name),
279 type(o.type),
280 rejected(o.rejected),
281 bundle_only(o.bundle_only),
Harald Alvestrand8e7d4bf2020-02-26 09:32:30 +0100282 description_(o.description_->Clone()) {}
Harald Alvestrand1716d392019-06-03 20:35:45 +0200283
284ContentInfo& ContentInfo::operator=(const ContentInfo& o) {
285 name = o.name;
286 type = o.type;
287 rejected = o.rejected;
288 bundle_only = o.bundle_only;
289 description_ = o.description_->Clone();
Harald Alvestrand1716d392019-06-03 20:35:45 +0200290 return *this;
291}
292
293const MediaContentDescription* ContentInfo::media_description() const {
Harald Alvestrand1716d392019-06-03 20:35:45 +0200294 return description_.get();
295}
296
297MediaContentDescription* ContentInfo::media_description() {
Harald Alvestrand1716d392019-06-03 20:35:45 +0200298 return description_.get();
299}
300
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000301} // namespace cricket