blob: b05dc510888b3e4060fd5de590109c0f191fb343 [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
11#include "webrtc/p2p/base/sessiondescription.h"
12
13#include "webrtc/libjingle/xmllite/xmlelement.h"
14
15namespace cricket {
16
17ContentInfo* FindContentInfoByName(
18 ContentInfos& contents, const std::string& name) {
19 for (ContentInfos::iterator content = contents.begin();
20 content != contents.end(); ++content) {
21 if (content->name == name) {
22 return &(*content);
23 }
24 }
25 return NULL;
26}
27
28const ContentInfo* FindContentInfoByName(
29 const ContentInfos& contents, const std::string& name) {
30 for (ContentInfos::const_iterator content = contents.begin();
31 content != contents.end(); ++content) {
32 if (content->name == name) {
33 return &(*content);
34 }
35 }
36 return NULL;
37}
38
39const ContentInfo* FindContentInfoByType(
40 const ContentInfos& contents, const std::string& type) {
41 for (ContentInfos::const_iterator content = contents.begin();
42 content != contents.end(); ++content) {
43 if (content->type == type) {
44 return &(*content);
45 }
46 }
47 return NULL;
48}
49
50const std::string* ContentGroup::FirstContentName() const {
51 return (!content_names_.empty()) ? &(*content_names_.begin()) : NULL;
52}
53
54bool ContentGroup::HasContentName(const std::string& content_name) const {
55 return (std::find(content_names_.begin(), content_names_.end(),
56 content_name) != content_names_.end());
57}
58
59void ContentGroup::AddContentName(const std::string& content_name) {
60 if (!HasContentName(content_name)) {
61 content_names_.push_back(content_name);
62 }
63}
64
65bool ContentGroup::RemoveContentName(const std::string& content_name) {
66 ContentNames::iterator iter = std::find(
67 content_names_.begin(), content_names_.end(), content_name);
68 if (iter == content_names_.end()) {
69 return false;
70 }
71 content_names_.erase(iter);
72 return true;
73}
74
75SessionDescription* SessionDescription::Copy() const {
76 SessionDescription* copy = new SessionDescription(*this);
77 // Copy all ContentDescriptions.
78 for (ContentInfos::iterator content = copy->contents_.begin();
79 content != copy->contents().end(); ++content) {
80 content->description = content->description->Copy();
81 }
82 return copy;
83}
84
85const ContentInfo* SessionDescription::GetContentByName(
86 const std::string& name) const {
87 return FindContentInfoByName(contents_, name);
88}
89
90ContentInfo* SessionDescription::GetContentByName(
91 const std::string& name) {
92 return FindContentInfoByName(contents_, name);
93}
94
95const ContentDescription* SessionDescription::GetContentDescriptionByName(
96 const std::string& name) const {
97 const ContentInfo* cinfo = FindContentInfoByName(contents_, name);
98 if (cinfo == NULL) {
99 return NULL;
100 }
101
102 return cinfo->description;
103}
104
105ContentDescription* SessionDescription::GetContentDescriptionByName(
106 const std::string& name) {
107 ContentInfo* cinfo = FindContentInfoByName(contents_, name);
108 if (cinfo == NULL) {
109 return NULL;
110 }
111
112 return cinfo->description;
113}
114
115const ContentInfo* SessionDescription::FirstContentByType(
116 const std::string& type) const {
117 return FindContentInfoByType(contents_, type);
118}
119
120const ContentInfo* SessionDescription::FirstContent() const {
121 return (contents_.empty()) ? NULL : &(*contents_.begin());
122}
123
124void SessionDescription::AddContent(const std::string& name,
125 const std::string& type,
126 ContentDescription* description) {
127 contents_.push_back(ContentInfo(name, type, description));
128}
129
130void SessionDescription::AddContent(const std::string& name,
131 const std::string& type,
132 bool rejected,
133 ContentDescription* description) {
134 contents_.push_back(ContentInfo(name, type, rejected, description));
135}
136
137bool SessionDescription::RemoveContentByName(const std::string& name) {
138 for (ContentInfos::iterator content = contents_.begin();
139 content != contents_.end(); ++content) {
140 if (content->name == name) {
141 delete content->description;
142 contents_.erase(content);
143 return true;
144 }
145 }
146
147 return false;
148}
149
150bool SessionDescription::AddTransportInfo(const TransportInfo& transport_info) {
151 if (GetTransportInfoByName(transport_info.content_name) != NULL) {
152 return false;
153 }
154 transport_infos_.push_back(transport_info);
155 return true;
156}
157
158bool SessionDescription::RemoveTransportInfoByName(const std::string& name) {
159 for (TransportInfos::iterator transport_info = transport_infos_.begin();
160 transport_info != transport_infos_.end(); ++transport_info) {
161 if (transport_info->content_name == name) {
162 transport_infos_.erase(transport_info);
163 return true;
164 }
165 }
166 return false;
167}
168
169const TransportInfo* SessionDescription::GetTransportInfoByName(
170 const std::string& name) const {
171 for (TransportInfos::const_iterator iter = transport_infos_.begin();
172 iter != transport_infos_.end(); ++iter) {
173 if (iter->content_name == name) {
174 return &(*iter);
175 }
176 }
177 return NULL;
178}
179
180TransportInfo* SessionDescription::GetTransportInfoByName(
181 const std::string& name) {
182 for (TransportInfos::iterator iter = transport_infos_.begin();
183 iter != transport_infos_.end(); ++iter) {
184 if (iter->content_name == name) {
185 return &(*iter);
186 }
187 }
188 return NULL;
189}
190
191void SessionDescription::RemoveGroupByName(const std::string& name) {
192 for (ContentGroups::iterator iter = content_groups_.begin();
193 iter != content_groups_.end(); ++iter) {
194 if (iter->semantics() == name) {
195 content_groups_.erase(iter);
196 break;
197 }
198 }
199}
200
201bool SessionDescription::HasGroup(const std::string& name) const {
202 for (ContentGroups::const_iterator iter = content_groups_.begin();
203 iter != content_groups_.end(); ++iter) {
204 if (iter->semantics() == name) {
205 return true;
206 }
207 }
208 return false;
209}
210
211const ContentGroup* SessionDescription::GetGroupByName(
212 const std::string& name) const {
213 for (ContentGroups::const_iterator iter = content_groups_.begin();
214 iter != content_groups_.end(); ++iter) {
215 if (iter->semantics() == name) {
216 return &(*iter);
217 }
218 }
219 return NULL;
220}
221
222} // namespace cricket