blob: 355d6436be5ffe90a81773951effc153baf3459a [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;
87
88SessionDescription::SessionDescription(const ContentInfos& contents)
89 : contents_(contents) {}
90
91SessionDescription::SessionDescription(const ContentInfos& contents,
92 const ContentGroups& groups)
93 : contents_(contents), content_groups_(groups) {}
94
95SessionDescription::SessionDescription(const ContentInfos& contents,
96 const TransportInfos& transports,
97 const ContentGroups& groups)
98 : contents_(contents),
99 transport_infos_(transports),
100 content_groups_(groups) {}
101
102SessionDescription::SessionDescription(const SessionDescription&) = default;
103
104SessionDescription::~SessionDescription() {
105 for (ContentInfos::iterator content = contents_.begin();
106 content != contents_.end(); ++content) {
107 delete content->description;
108 }
109}
110
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000111SessionDescription* SessionDescription::Copy() const {
112 SessionDescription* copy = new SessionDescription(*this);
113 // Copy all ContentDescriptions.
114 for (ContentInfos::iterator content = copy->contents_.begin();
Steve Anton4ab68ee2017-12-19 14:26:11 -0800115 content != copy->contents().end(); ++content) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000116 content->description = content->description->Copy();
117 }
118 return copy;
119}
120
121const ContentInfo* SessionDescription::GetContentByName(
122 const std::string& name) const {
123 return FindContentInfoByName(contents_, name);
124}
125
Steve Anton4ab68ee2017-12-19 14:26:11 -0800126ContentInfo* SessionDescription::GetContentByName(const std::string& name) {
127 return FindContentInfoByName(&contents_, name);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000128}
129
130const ContentDescription* SessionDescription::GetContentDescriptionByName(
131 const std::string& name) const {
132 const ContentInfo* cinfo = FindContentInfoByName(contents_, name);
133 if (cinfo == NULL) {
134 return NULL;
135 }
136
137 return cinfo->description;
138}
139
140ContentDescription* SessionDescription::GetContentDescriptionByName(
141 const std::string& name) {
Steve Anton4ab68ee2017-12-19 14:26:11 -0800142 ContentInfo* cinfo = FindContentInfoByName(&contents_, name);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000143 if (cinfo == NULL) {
144 return NULL;
145 }
146
147 return cinfo->description;
148}
149
150const ContentInfo* SessionDescription::FirstContentByType(
Steve Anton5adfafd2017-12-20 16:34:00 -0800151 MediaProtocolType type) const {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000152 return FindContentInfoByType(contents_, type);
153}
154
155const ContentInfo* SessionDescription::FirstContent() const {
156 return (contents_.empty()) ? NULL : &(*contents_.begin());
157}
158
159void SessionDescription::AddContent(const std::string& name,
Steve Anton5adfafd2017-12-20 16:34:00 -0800160 MediaProtocolType type,
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000161 ContentDescription* description) {
Steve Anton5adfafd2017-12-20 16:34:00 -0800162 ContentInfo content(type);
163 content.name = name;
164 content.description = description;
165 contents_.push_back(std::move(content));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000166}
167
168void SessionDescription::AddContent(const std::string& name,
Steve Anton5adfafd2017-12-20 16:34:00 -0800169 MediaProtocolType type,
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000170 bool rejected,
171 ContentDescription* description) {
Steve Anton5adfafd2017-12-20 16:34:00 -0800172 ContentInfo content(type);
173 content.name = name;
174 content.rejected = rejected;
175 content.description = description;
176 contents_.push_back(std::move(content));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000177}
178
deadbeef25ed4352016-12-12 18:37:36 -0800179void SessionDescription::AddContent(const std::string& name,
Steve Anton5adfafd2017-12-20 16:34:00 -0800180 MediaProtocolType type,
deadbeef25ed4352016-12-12 18:37:36 -0800181 bool rejected,
182 bool bundle_only,
183 ContentDescription* description) {
Steve Anton5adfafd2017-12-20 16:34:00 -0800184 ContentInfo content(type);
185 content.name = name;
186 content.rejected = rejected;
187 content.bundle_only = bundle_only;
188 content.description = description;
189 contents_.push_back(std::move(content));
deadbeef25ed4352016-12-12 18:37:36 -0800190}
191
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000192bool SessionDescription::RemoveContentByName(const std::string& name) {
193 for (ContentInfos::iterator content = contents_.begin();
194 content != contents_.end(); ++content) {
195 if (content->name == name) {
196 delete content->description;
197 contents_.erase(content);
198 return true;
199 }
200 }
201
202 return false;
203}
204
205bool SessionDescription::AddTransportInfo(const TransportInfo& transport_info) {
206 if (GetTransportInfoByName(transport_info.content_name) != NULL) {
207 return false;
208 }
209 transport_infos_.push_back(transport_info);
210 return true;
211}
212
213bool SessionDescription::RemoveTransportInfoByName(const std::string& name) {
214 for (TransportInfos::iterator transport_info = transport_infos_.begin();
215 transport_info != transport_infos_.end(); ++transport_info) {
216 if (transport_info->content_name == name) {
217 transport_infos_.erase(transport_info);
218 return true;
219 }
220 }
221 return false;
222}
223
224const TransportInfo* SessionDescription::GetTransportInfoByName(
225 const std::string& name) const {
226 for (TransportInfos::const_iterator iter = transport_infos_.begin();
227 iter != transport_infos_.end(); ++iter) {
228 if (iter->content_name == name) {
229 return &(*iter);
230 }
231 }
232 return NULL;
233}
234
235TransportInfo* SessionDescription::GetTransportInfoByName(
236 const std::string& name) {
237 for (TransportInfos::iterator iter = transport_infos_.begin();
238 iter != transport_infos_.end(); ++iter) {
239 if (iter->content_name == name) {
240 return &(*iter);
241 }
242 }
243 return NULL;
244}
245
246void SessionDescription::RemoveGroupByName(const std::string& name) {
247 for (ContentGroups::iterator iter = content_groups_.begin();
248 iter != content_groups_.end(); ++iter) {
249 if (iter->semantics() == name) {
250 content_groups_.erase(iter);
251 break;
252 }
253 }
254}
255
256bool SessionDescription::HasGroup(const std::string& name) const {
257 for (ContentGroups::const_iterator iter = content_groups_.begin();
258 iter != content_groups_.end(); ++iter) {
259 if (iter->semantics() == name) {
260 return true;
261 }
262 }
263 return false;
264}
265
266const ContentGroup* SessionDescription::GetGroupByName(
267 const std::string& name) const {
268 for (ContentGroups::const_iterator iter = content_groups_.begin();
269 iter != content_groups_.end(); ++iter) {
270 if (iter->semantics() == name) {
271 return &(*iter);
272 }
273 }
274 return NULL;
275}
276
277} // namespace cricket