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