blob: 3a9b18ee6919b37ff5940ee6064eccb7b46a8d4d [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 Anton4ab68ee2017-12-19 14:26:11 -080011#include "pc/sessiondescription.h"
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000012
Steve Anton5adfafd2017-12-20 16:34:00 -080013#include <utility>
14
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000015namespace cricket {
Steve Anton4ab68ee2017-12-19 14:26:11 -080016namespace {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000017
Steve Anton4ab68ee2017-12-19 14:26:11 -080018ContentInfo* FindContentInfoByName(ContentInfos* contents,
19 const std::string& name) {
20 RTC_DCHECK(contents);
21 for (ContentInfo& content : *contents) {
22 if (content.name == name) {
23 return &content;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000024 }
25 }
Steve Anton4ab68ee2017-12-19 14:26:11 -080026 return nullptr;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000027}
28
Steve Anton4ab68ee2017-12-19 14:26:11 -080029} // namespace
30
31const ContentInfo* FindContentInfoByName(const ContentInfos& contents,
32 const std::string& name) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000033 for (ContentInfos::const_iterator content = contents.begin();
34 content != contents.end(); ++content) {
35 if (content->name == name) {
36 return &(*content);
37 }
38 }
39 return NULL;
40}
41
Steve Anton4ab68ee2017-12-19 14:26:11 -080042const ContentInfo* FindContentInfoByType(const ContentInfos& contents,
Steve Anton5adfafd2017-12-20 16:34:00 -080043 MediaProtocolType type) {
44 for (const auto& content : contents) {
45 if (content.type == type) {
46 return &content;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000047 }
48 }
Steve Anton5adfafd2017-12-20 16:34:00 -080049 return nullptr;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000050}
51
Steve Antond3ea9992017-10-31 12:38:23 -070052ContentGroup::ContentGroup(const std::string& semantics)
53 : semantics_(semantics) {}
54
55ContentGroup::ContentGroup(const ContentGroup&) = default;
56ContentGroup::ContentGroup(ContentGroup&&) = default;
57ContentGroup& ContentGroup::operator=(const ContentGroup&) = default;
58ContentGroup& ContentGroup::operator=(ContentGroup&&) = default;
59ContentGroup::~ContentGroup() = default;
60
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000061const std::string* ContentGroup::FirstContentName() const {
62 return (!content_names_.empty()) ? &(*content_names_.begin()) : NULL;
63}
64
65bool ContentGroup::HasContentName(const std::string& content_name) const {
66 return (std::find(content_names_.begin(), content_names_.end(),
67 content_name) != content_names_.end());
68}
69
70void ContentGroup::AddContentName(const std::string& content_name) {
71 if (!HasContentName(content_name)) {
72 content_names_.push_back(content_name);
73 }
74}
75
76bool ContentGroup::RemoveContentName(const std::string& content_name) {
Steve Anton4ab68ee2017-12-19 14:26:11 -080077 ContentNames::iterator iter =
78 std::find(content_names_.begin(), content_names_.end(), content_name);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000079 if (iter == content_names_.end()) {
80 return false;
81 }
82 content_names_.erase(iter);
83 return true;
84}
85
Steve Antond3ea9992017-10-31 12:38:23 -070086SessionDescription::SessionDescription() = default;
Steve Antond3ea9992017-10-31 12:38:23 -070087SessionDescription::SessionDescription(const SessionDescription&) = default;
88
89SessionDescription::~SessionDescription() {
90 for (ContentInfos::iterator content = contents_.begin();
91 content != contents_.end(); ++content) {
92 delete content->description;
93 }
94}
95
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000096SessionDescription* SessionDescription::Copy() const {
97 SessionDescription* copy = new SessionDescription(*this);
98 // Copy all ContentDescriptions.
99 for (ContentInfos::iterator content = copy->contents_.begin();
Steve Anton4ab68ee2017-12-19 14:26:11 -0800100 content != copy->contents().end(); ++content) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000101 content->description = content->description->Copy();
102 }
103 return copy;
104}
105
106const ContentInfo* SessionDescription::GetContentByName(
107 const std::string& name) const {
108 return FindContentInfoByName(contents_, name);
109}
110
Steve Anton4ab68ee2017-12-19 14:26:11 -0800111ContentInfo* SessionDescription::GetContentByName(const std::string& name) {
112 return FindContentInfoByName(&contents_, name);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000113}
114
Steve Antonb1c1de12017-12-21 15:14:30 -0800115const MediaContentDescription* SessionDescription::GetContentDescriptionByName(
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000116 const std::string& name) const {
117 const ContentInfo* cinfo = FindContentInfoByName(contents_, name);
118 if (cinfo == NULL) {
119 return NULL;
120 }
121
Steve Antonb1c1de12017-12-21 15:14:30 -0800122 return cinfo->media_description();
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000123}
124
Steve Antonb1c1de12017-12-21 15:14:30 -0800125MediaContentDescription* SessionDescription::GetContentDescriptionByName(
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000126 const std::string& name) {
Steve Anton4ab68ee2017-12-19 14:26:11 -0800127 ContentInfo* cinfo = FindContentInfoByName(&contents_, name);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000128 if (cinfo == NULL) {
129 return NULL;
130 }
131
Steve Antonb1c1de12017-12-21 15:14:30 -0800132 return cinfo->media_description();
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000133}
134
135const ContentInfo* SessionDescription::FirstContentByType(
Steve Anton5adfafd2017-12-20 16:34:00 -0800136 MediaProtocolType type) const {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000137 return FindContentInfoByType(contents_, type);
138}
139
140const ContentInfo* SessionDescription::FirstContent() const {
141 return (contents_.empty()) ? NULL : &(*contents_.begin());
142}
143
144void SessionDescription::AddContent(const std::string& name,
Steve Anton5adfafd2017-12-20 16:34:00 -0800145 MediaProtocolType type,
Steve Antonb1c1de12017-12-21 15:14:30 -0800146 MediaContentDescription* description) {
Steve Anton5adfafd2017-12-20 16:34:00 -0800147 ContentInfo content(type);
148 content.name = name;
149 content.description = description;
Johannes Kron9ac3c912018-10-12 10:54:26 +0200150 AddContent(&content);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000151}
152
153void SessionDescription::AddContent(const std::string& name,
Steve Anton5adfafd2017-12-20 16:34:00 -0800154 MediaProtocolType type,
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000155 bool rejected,
Steve Antonb1c1de12017-12-21 15:14:30 -0800156 MediaContentDescription* description) {
Steve Anton5adfafd2017-12-20 16:34:00 -0800157 ContentInfo content(type);
158 content.name = name;
159 content.rejected = rejected;
160 content.description = description;
Johannes Kron9ac3c912018-10-12 10:54:26 +0200161 AddContent(&content);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000162}
163
deadbeef25ed4352016-12-12 18:37:36 -0800164void SessionDescription::AddContent(const std::string& name,
Steve Anton5adfafd2017-12-20 16:34:00 -0800165 MediaProtocolType type,
deadbeef25ed4352016-12-12 18:37:36 -0800166 bool rejected,
167 bool bundle_only,
Steve Antonb1c1de12017-12-21 15:14:30 -0800168 MediaContentDescription* description) {
Steve Anton5adfafd2017-12-20 16:34:00 -0800169 ContentInfo content(type);
170 content.name = name;
171 content.rejected = rejected;
172 content.bundle_only = bundle_only;
173 content.description = description;
Johannes Kron9ac3c912018-10-12 10:54:26 +0200174 AddContent(&content);
175}
176
177void SessionDescription::AddContent(ContentInfo* content) {
Johannes Kron9581bc42018-10-23 10:17:39 +0200178 if (extmap_allow_mixed()) {
Johannes Kron9ac3c912018-10-12 10:54:26 +0200179 // Mixed support on session level overrides setting on media level.
Johannes Kron9581bc42018-10-23 10:17:39 +0200180 content->description->set_extmap_allow_mixed_enum(
Johannes Kron9ac3c912018-10-12 10:54:26 +0200181 MediaContentDescription::kSession);
182 }
183 contents_.push_back(std::move(*content));
deadbeef25ed4352016-12-12 18:37:36 -0800184}
185
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000186bool SessionDescription::RemoveContentByName(const std::string& name) {
187 for (ContentInfos::iterator content = contents_.begin();
188 content != contents_.end(); ++content) {
189 if (content->name == name) {
190 delete content->description;
191 contents_.erase(content);
192 return true;
193 }
194 }
195
196 return false;
197}
198
199bool SessionDescription::AddTransportInfo(const TransportInfo& transport_info) {
200 if (GetTransportInfoByName(transport_info.content_name) != NULL) {
201 return false;
202 }
203 transport_infos_.push_back(transport_info);
204 return true;
205}
206
207bool SessionDescription::RemoveTransportInfoByName(const std::string& name) {
208 for (TransportInfos::iterator transport_info = transport_infos_.begin();
209 transport_info != transport_infos_.end(); ++transport_info) {
210 if (transport_info->content_name == name) {
211 transport_infos_.erase(transport_info);
212 return true;
213 }
214 }
215 return false;
216}
217
218const TransportInfo* SessionDescription::GetTransportInfoByName(
219 const std::string& name) const {
220 for (TransportInfos::const_iterator iter = transport_infos_.begin();
221 iter != transport_infos_.end(); ++iter) {
222 if (iter->content_name == name) {
223 return &(*iter);
224 }
225 }
226 return NULL;
227}
228
229TransportInfo* SessionDescription::GetTransportInfoByName(
230 const std::string& name) {
231 for (TransportInfos::iterator iter = transport_infos_.begin();
232 iter != transport_infos_.end(); ++iter) {
233 if (iter->content_name == name) {
234 return &(*iter);
235 }
236 }
237 return NULL;
238}
239
240void SessionDescription::RemoveGroupByName(const std::string& name) {
241 for (ContentGroups::iterator iter = content_groups_.begin();
242 iter != content_groups_.end(); ++iter) {
243 if (iter->semantics() == name) {
244 content_groups_.erase(iter);
245 break;
246 }
247 }
248}
249
250bool SessionDescription::HasGroup(const std::string& name) const {
251 for (ContentGroups::const_iterator iter = content_groups_.begin();
252 iter != content_groups_.end(); ++iter) {
253 if (iter->semantics() == name) {
254 return true;
255 }
256 }
257 return false;
258}
259
260const ContentGroup* SessionDescription::GetGroupByName(
261 const std::string& name) const {
262 for (ContentGroups::const_iterator iter = content_groups_.begin();
263 iter != content_groups_.end(); ++iter) {
264 if (iter->semantics() == name) {
265 return &(*iter);
266 }
267 }
268 return NULL;
269}
270
271} // namespace cricket