blob: dd56eafa7c104f1d25f312c1725877f3a38f5917 [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
Yves Gerey3e707812018-11-28 16:47:49 +010013#include <algorithm>
Steve Anton5adfafd2017-12-20 16:34:00 -080014#include <utility>
15
Yves Gerey3e707812018-11-28 16:47:49 +010016#include "rtc_base/checks.h"
17
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000018namespace cricket {
Steve Anton4ab68ee2017-12-19 14:26:11 -080019namespace {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000020
Steve Anton4ab68ee2017-12-19 14:26:11 -080021ContentInfo* FindContentInfoByName(ContentInfos* contents,
22 const std::string& name) {
23 RTC_DCHECK(contents);
24 for (ContentInfo& content : *contents) {
25 if (content.name == name) {
26 return &content;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000027 }
28 }
Steve Anton4ab68ee2017-12-19 14:26:11 -080029 return nullptr;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000030}
31
Steve Anton4ab68ee2017-12-19 14:26:11 -080032} // namespace
33
34const ContentInfo* FindContentInfoByName(const ContentInfos& contents,
35 const std::string& name) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000036 for (ContentInfos::const_iterator content = contents.begin();
37 content != contents.end(); ++content) {
38 if (content->name == name) {
39 return &(*content);
40 }
41 }
42 return NULL;
43}
44
Steve Anton4ab68ee2017-12-19 14:26:11 -080045const ContentInfo* FindContentInfoByType(const ContentInfos& contents,
Steve Anton5adfafd2017-12-20 16:34:00 -080046 MediaProtocolType type) {
47 for (const auto& content : contents) {
48 if (content.type == type) {
49 return &content;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000050 }
51 }
Steve Anton5adfafd2017-12-20 16:34:00 -080052 return nullptr;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000053}
54
Steve Antond3ea9992017-10-31 12:38:23 -070055ContentGroup::ContentGroup(const std::string& semantics)
56 : semantics_(semantics) {}
57
58ContentGroup::ContentGroup(const ContentGroup&) = default;
59ContentGroup::ContentGroup(ContentGroup&&) = default;
60ContentGroup& ContentGroup::operator=(const ContentGroup&) = default;
61ContentGroup& ContentGroup::operator=(ContentGroup&&) = default;
62ContentGroup::~ContentGroup() = default;
63
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000064const std::string* ContentGroup::FirstContentName() const {
65 return (!content_names_.empty()) ? &(*content_names_.begin()) : NULL;
66}
67
68bool ContentGroup::HasContentName(const std::string& content_name) const {
69 return (std::find(content_names_.begin(), content_names_.end(),
70 content_name) != content_names_.end());
71}
72
73void ContentGroup::AddContentName(const std::string& content_name) {
74 if (!HasContentName(content_name)) {
75 content_names_.push_back(content_name);
76 }
77}
78
79bool ContentGroup::RemoveContentName(const std::string& content_name) {
Steve Anton4ab68ee2017-12-19 14:26:11 -080080 ContentNames::iterator iter =
81 std::find(content_names_.begin(), content_names_.end(), content_name);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000082 if (iter == content_names_.end()) {
83 return false;
84 }
85 content_names_.erase(iter);
86 return true;
87}
88
Steve Antond3ea9992017-10-31 12:38:23 -070089SessionDescription::SessionDescription() = default;
Steve Antond3ea9992017-10-31 12:38:23 -070090SessionDescription::SessionDescription(const SessionDescription&) = default;
91
92SessionDescription::~SessionDescription() {
93 for (ContentInfos::iterator content = contents_.begin();
94 content != contents_.end(); ++content) {
95 delete content->description;
96 }
97}
98
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000099SessionDescription* SessionDescription::Copy() const {
100 SessionDescription* copy = new SessionDescription(*this);
101 // Copy all ContentDescriptions.
102 for (ContentInfos::iterator content = copy->contents_.begin();
Steve Anton4ab68ee2017-12-19 14:26:11 -0800103 content != copy->contents().end(); ++content) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000104 content->description = content->description->Copy();
105 }
106 return copy;
107}
108
109const ContentInfo* SessionDescription::GetContentByName(
110 const std::string& name) const {
111 return FindContentInfoByName(contents_, name);
112}
113
Steve Anton4ab68ee2017-12-19 14:26:11 -0800114ContentInfo* SessionDescription::GetContentByName(const std::string& name) {
115 return FindContentInfoByName(&contents_, name);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000116}
117
Steve Antonb1c1de12017-12-21 15:14:30 -0800118const MediaContentDescription* SessionDescription::GetContentDescriptionByName(
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000119 const std::string& name) const {
120 const ContentInfo* cinfo = FindContentInfoByName(contents_, name);
121 if (cinfo == NULL) {
122 return NULL;
123 }
124
Steve Antonb1c1de12017-12-21 15:14:30 -0800125 return cinfo->media_description();
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000126}
127
Steve Antonb1c1de12017-12-21 15:14:30 -0800128MediaContentDescription* SessionDescription::GetContentDescriptionByName(
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000129 const std::string& name) {
Steve Anton4ab68ee2017-12-19 14:26:11 -0800130 ContentInfo* cinfo = FindContentInfoByName(&contents_, name);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000131 if (cinfo == NULL) {
132 return NULL;
133 }
134
Steve Antonb1c1de12017-12-21 15:14:30 -0800135 return cinfo->media_description();
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000136}
137
138const ContentInfo* SessionDescription::FirstContentByType(
Steve Anton5adfafd2017-12-20 16:34:00 -0800139 MediaProtocolType type) const {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000140 return FindContentInfoByType(contents_, type);
141}
142
143const ContentInfo* SessionDescription::FirstContent() const {
144 return (contents_.empty()) ? NULL : &(*contents_.begin());
145}
146
147void SessionDescription::AddContent(const std::string& name,
Steve Anton5adfafd2017-12-20 16:34:00 -0800148 MediaProtocolType type,
Steve Antonb1c1de12017-12-21 15:14:30 -0800149 MediaContentDescription* description) {
Steve Anton5adfafd2017-12-20 16:34:00 -0800150 ContentInfo content(type);
151 content.name = name;
152 content.description = description;
Johannes Kron9ac3c912018-10-12 10:54:26 +0200153 AddContent(&content);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000154}
155
156void SessionDescription::AddContent(const std::string& name,
Steve Anton5adfafd2017-12-20 16:34:00 -0800157 MediaProtocolType type,
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000158 bool rejected,
Steve Antonb1c1de12017-12-21 15:14:30 -0800159 MediaContentDescription* description) {
Steve Anton5adfafd2017-12-20 16:34:00 -0800160 ContentInfo content(type);
161 content.name = name;
162 content.rejected = rejected;
163 content.description = description;
Johannes Kron9ac3c912018-10-12 10:54:26 +0200164 AddContent(&content);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000165}
166
deadbeef25ed4352016-12-12 18:37:36 -0800167void SessionDescription::AddContent(const std::string& name,
Steve Anton5adfafd2017-12-20 16:34:00 -0800168 MediaProtocolType type,
deadbeef25ed4352016-12-12 18:37:36 -0800169 bool rejected,
170 bool bundle_only,
Steve Antonb1c1de12017-12-21 15:14:30 -0800171 MediaContentDescription* description) {
Steve Anton5adfafd2017-12-20 16:34:00 -0800172 ContentInfo content(type);
173 content.name = name;
174 content.rejected = rejected;
175 content.bundle_only = bundle_only;
176 content.description = description;
Johannes Kron9ac3c912018-10-12 10:54:26 +0200177 AddContent(&content);
178}
179
180void SessionDescription::AddContent(ContentInfo* content) {
Johannes Kron9581bc42018-10-23 10:17:39 +0200181 if (extmap_allow_mixed()) {
Johannes Kron9ac3c912018-10-12 10:54:26 +0200182 // Mixed support on session level overrides setting on media level.
Johannes Kron9581bc42018-10-23 10:17:39 +0200183 content->description->set_extmap_allow_mixed_enum(
Johannes Kron9ac3c912018-10-12 10:54:26 +0200184 MediaContentDescription::kSession);
185 }
186 contents_.push_back(std::move(*content));
deadbeef25ed4352016-12-12 18:37:36 -0800187}
188
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000189bool SessionDescription::RemoveContentByName(const std::string& name) {
190 for (ContentInfos::iterator content = contents_.begin();
191 content != contents_.end(); ++content) {
192 if (content->name == name) {
193 delete content->description;
194 contents_.erase(content);
195 return true;
196 }
197 }
198
199 return false;
200}
201
Steve Anton06817cd2018-12-18 15:55:30 -0800202void SessionDescription::AddTransportInfo(const TransportInfo& transport_info) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000203 transport_infos_.push_back(transport_info);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000204}
205
206bool SessionDescription::RemoveTransportInfoByName(const std::string& name) {
207 for (TransportInfos::iterator transport_info = transport_infos_.begin();
208 transport_info != transport_infos_.end(); ++transport_info) {
209 if (transport_info->content_name == name) {
210 transport_infos_.erase(transport_info);
211 return true;
212 }
213 }
214 return false;
215}
216
217const TransportInfo* SessionDescription::GetTransportInfoByName(
218 const std::string& name) const {
219 for (TransportInfos::const_iterator iter = transport_infos_.begin();
220 iter != transport_infos_.end(); ++iter) {
221 if (iter->content_name == name) {
222 return &(*iter);
223 }
224 }
225 return NULL;
226}
227
228TransportInfo* SessionDescription::GetTransportInfoByName(
229 const std::string& name) {
230 for (TransportInfos::iterator iter = transport_infos_.begin();
231 iter != transport_infos_.end(); ++iter) {
232 if (iter->content_name == name) {
233 return &(*iter);
234 }
235 }
236 return NULL;
237}
238
239void SessionDescription::RemoveGroupByName(const std::string& name) {
240 for (ContentGroups::iterator iter = content_groups_.begin();
241 iter != content_groups_.end(); ++iter) {
242 if (iter->semantics() == name) {
243 content_groups_.erase(iter);
244 break;
245 }
246 }
247}
248
249bool SessionDescription::HasGroup(const std::string& name) const {
250 for (ContentGroups::const_iterator iter = content_groups_.begin();
251 iter != content_groups_.end(); ++iter) {
252 if (iter->semantics() == name) {
253 return true;
254 }
255 }
256 return false;
257}
258
259const ContentGroup* SessionDescription::GetGroupByName(
260 const std::string& name) const {
261 for (ContentGroups::const_iterator iter = content_groups_.begin();
262 iter != content_groups_.end(); ++iter) {
263 if (iter->semantics() == name) {
264 return &(*iter);
265 }
266 }
267 return NULL;
268}
269
270} // namespace cricket