blob: 6e90ac82f07044d82ba8206467e447f2fe167b8b [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
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000013namespace cricket {
Steve Anton4ab68ee2017-12-19 14:26:11 -080014namespace {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000015
Steve Anton4ab68ee2017-12-19 14:26:11 -080016ContentInfo* FindContentInfoByName(ContentInfos* contents,
17 const std::string& name) {
18 RTC_DCHECK(contents);
19 for (ContentInfo& content : *contents) {
20 if (content.name == name) {
21 return &content;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000022 }
23 }
Steve Anton4ab68ee2017-12-19 14:26:11 -080024 return nullptr;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000025}
26
Steve Anton4ab68ee2017-12-19 14:26:11 -080027} // namespace
28
29const ContentInfo* FindContentInfoByName(const ContentInfos& contents,
30 const std::string& name) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000031 for (ContentInfos::const_iterator content = contents.begin();
32 content != contents.end(); ++content) {
33 if (content->name == name) {
34 return &(*content);
35 }
36 }
37 return NULL;
38}
39
Steve Anton4ab68ee2017-12-19 14:26:11 -080040const ContentInfo* FindContentInfoByType(const ContentInfos& contents,
41 const std::string& type) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000042 for (ContentInfos::const_iterator content = contents.begin();
43 content != contents.end(); ++content) {
44 if (content->type == type) {
45 return &(*content);
46 }
47 }
48 return NULL;
49}
50
Steve Antond3ea9992017-10-31 12:38:23 -070051ContentGroup::ContentGroup(const std::string& semantics)
52 : semantics_(semantics) {}
53
54ContentGroup::ContentGroup(const ContentGroup&) = default;
55ContentGroup::ContentGroup(ContentGroup&&) = default;
56ContentGroup& ContentGroup::operator=(const ContentGroup&) = default;
57ContentGroup& ContentGroup::operator=(ContentGroup&&) = default;
58ContentGroup::~ContentGroup() = default;
59
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000060const std::string* ContentGroup::FirstContentName() const {
61 return (!content_names_.empty()) ? &(*content_names_.begin()) : NULL;
62}
63
64bool ContentGroup::HasContentName(const std::string& content_name) const {
65 return (std::find(content_names_.begin(), content_names_.end(),
66 content_name) != content_names_.end());
67}
68
69void ContentGroup::AddContentName(const std::string& content_name) {
70 if (!HasContentName(content_name)) {
71 content_names_.push_back(content_name);
72 }
73}
74
75bool ContentGroup::RemoveContentName(const std::string& content_name) {
Steve Anton4ab68ee2017-12-19 14:26:11 -080076 ContentNames::iterator iter =
77 std::find(content_names_.begin(), content_names_.end(), content_name);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000078 if (iter == content_names_.end()) {
79 return false;
80 }
81 content_names_.erase(iter);
82 return true;
83}
84
Steve Antond3ea9992017-10-31 12:38:23 -070085SessionDescription::SessionDescription() = default;
86
87SessionDescription::SessionDescription(const ContentInfos& contents)
88 : contents_(contents) {}
89
90SessionDescription::SessionDescription(const ContentInfos& contents,
91 const ContentGroups& groups)
92 : contents_(contents), content_groups_(groups) {}
93
94SessionDescription::SessionDescription(const ContentInfos& contents,
95 const TransportInfos& transports,
96 const ContentGroups& groups)
97 : contents_(contents),
98 transport_infos_(transports),
99 content_groups_(groups) {}
100
101SessionDescription::SessionDescription(const SessionDescription&) = default;
102
103SessionDescription::~SessionDescription() {
104 for (ContentInfos::iterator content = contents_.begin();
105 content != contents_.end(); ++content) {
106 delete content->description;
107 }
108}
109
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000110SessionDescription* SessionDescription::Copy() const {
111 SessionDescription* copy = new SessionDescription(*this);
112 // Copy all ContentDescriptions.
113 for (ContentInfos::iterator content = copy->contents_.begin();
Steve Anton4ab68ee2017-12-19 14:26:11 -0800114 content != copy->contents().end(); ++content) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000115 content->description = content->description->Copy();
116 }
117 return copy;
118}
119
120const ContentInfo* SessionDescription::GetContentByName(
121 const std::string& name) const {
122 return FindContentInfoByName(contents_, name);
123}
124
Steve Anton4ab68ee2017-12-19 14:26:11 -0800125ContentInfo* SessionDescription::GetContentByName(const std::string& name) {
126 return FindContentInfoByName(&contents_, name);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000127}
128
129const ContentDescription* SessionDescription::GetContentDescriptionByName(
130 const std::string& name) const {
131 const ContentInfo* cinfo = FindContentInfoByName(contents_, name);
132 if (cinfo == NULL) {
133 return NULL;
134 }
135
136 return cinfo->description;
137}
138
139ContentDescription* SessionDescription::GetContentDescriptionByName(
140 const std::string& name) {
Steve Anton4ab68ee2017-12-19 14:26:11 -0800141 ContentInfo* cinfo = FindContentInfoByName(&contents_, name);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000142 if (cinfo == NULL) {
143 return NULL;
144 }
145
146 return cinfo->description;
147}
148
149const ContentInfo* SessionDescription::FirstContentByType(
150 const std::string& type) const {
151 return FindContentInfoByType(contents_, type);
152}
153
154const ContentInfo* SessionDescription::FirstContent() const {
155 return (contents_.empty()) ? NULL : &(*contents_.begin());
156}
157
158void SessionDescription::AddContent(const std::string& name,
159 const std::string& type,
160 ContentDescription* description) {
161 contents_.push_back(ContentInfo(name, type, description));
162}
163
164void SessionDescription::AddContent(const std::string& name,
165 const std::string& type,
166 bool rejected,
167 ContentDescription* description) {
168 contents_.push_back(ContentInfo(name, type, rejected, description));
169}
170
deadbeef25ed4352016-12-12 18:37:36 -0800171void SessionDescription::AddContent(const std::string& name,
172 const std::string& type,
173 bool rejected,
174 bool bundle_only,
175 ContentDescription* description) {
176 contents_.push_back(
177 ContentInfo(name, type, rejected, bundle_only, description));
178}
179
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000180bool SessionDescription::RemoveContentByName(const std::string& name) {
181 for (ContentInfos::iterator content = contents_.begin();
182 content != contents_.end(); ++content) {
183 if (content->name == name) {
184 delete content->description;
185 contents_.erase(content);
186 return true;
187 }
188 }
189
190 return false;
191}
192
193bool SessionDescription::AddTransportInfo(const TransportInfo& transport_info) {
194 if (GetTransportInfoByName(transport_info.content_name) != NULL) {
195 return false;
196 }
197 transport_infos_.push_back(transport_info);
198 return true;
199}
200
201bool SessionDescription::RemoveTransportInfoByName(const std::string& name) {
202 for (TransportInfos::iterator transport_info = transport_infos_.begin();
203 transport_info != transport_infos_.end(); ++transport_info) {
204 if (transport_info->content_name == name) {
205 transport_infos_.erase(transport_info);
206 return true;
207 }
208 }
209 return false;
210}
211
212const TransportInfo* SessionDescription::GetTransportInfoByName(
213 const std::string& name) const {
214 for (TransportInfos::const_iterator iter = transport_infos_.begin();
215 iter != transport_infos_.end(); ++iter) {
216 if (iter->content_name == name) {
217 return &(*iter);
218 }
219 }
220 return NULL;
221}
222
223TransportInfo* SessionDescription::GetTransportInfoByName(
224 const std::string& name) {
225 for (TransportInfos::iterator iter = transport_infos_.begin();
226 iter != transport_infos_.end(); ++iter) {
227 if (iter->content_name == name) {
228 return &(*iter);
229 }
230 }
231 return NULL;
232}
233
234void SessionDescription::RemoveGroupByName(const std::string& name) {
235 for (ContentGroups::iterator iter = content_groups_.begin();
236 iter != content_groups_.end(); ++iter) {
237 if (iter->semantics() == name) {
238 content_groups_.erase(iter);
239 break;
240 }
241 }
242}
243
244bool SessionDescription::HasGroup(const std::string& name) const {
245 for (ContentGroups::const_iterator iter = content_groups_.begin();
246 iter != content_groups_.end(); ++iter) {
247 if (iter->semantics() == name) {
248 return true;
249 }
250 }
251 return false;
252}
253
254const ContentGroup* SessionDescription::GetGroupByName(
255 const std::string& name) const {
256 for (ContentGroups::const_iterator iter = content_groups_.begin();
257 iter != content_groups_.end(); ++iter) {
258 if (iter->semantics() == name) {
259 return &(*iter);
260 }
261 }
262 return NULL;
263}
264
265} // namespace cricket