Ring Daemon 16.0.0
Loading...
Searching...
No Matches
media_attribute.cpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2004-2025 Savoir-faire Linux Inc.
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17
19#include "jami/media_const.h"
20#include "logger.h"
21#include "string_utils.h"
22
23namespace jami {
24
26{
27 std::pair<bool, MediaType> pairType = getMediaType(mediaMap);
28 if (pairType.first)
29 type_ = pairType.second;
30
31 std::pair<bool, bool> pairBool;
32
34 if (pairBool.first)
35 muted_ = pairBool.second;
36
38 if (pairBool.first)
39 enabled_ = pairBool.second;
40
41 std::pair<bool, std::string> pairString;
43 if (pairBool.first)
44 sourceUri_ = pairString.second;
45
47 if (pairBool.first)
48 label_ = pairString.second;
49
51 if (pairBool.first)
52 onHold_ = pairBool.second;
53
54 secure_ = secure;
55}
56
57std::vector<MediaAttribute>
58MediaAttribute::buildMediaAttributesList(const std::vector<libjami::MediaMap>& mediaList, bool secure)
59{
60 std::vector<MediaAttribute> mediaAttrList;
61 mediaAttrList.reserve(mediaList.size());
62
63 for (auto const& mediaMap : mediaList) {
64 mediaAttrList.emplace_back(MediaAttribute(mediaMap, secure));
65 }
66
67 return mediaAttrList;
68}
69
71MediaAttribute::stringToMediaType(const std::string& mediaType)
72{
73 if (mediaType.compare(libjami::Media::MediaAttributeValue::AUDIO) == 0)
75 if (mediaType.compare(libjami::Media::MediaAttributeValue::VIDEO) == 0)
78}
79
80std::pair<bool, MediaType>
82{
84 if (iter == map.end()) {
85 return {false, MediaType::MEDIA_NONE};
86 }
87
88 auto type = stringToMediaType(iter->second);
89 if (type == MediaType::MEDIA_NONE) {
90 JAMI_ERR("Invalid value [%s] for a media type key in media map", iter->second.c_str());
91 return {false, type};
92 }
93
94 return {true, type};
95}
96
97std::pair<bool, bool>
98MediaAttribute::getBoolValue(const libjami::MediaMap& map, const std::string& key)
99{
100 const auto& iter = map.find(key);
101 if (iter == map.end()) {
102 return {false, false};
103 }
104
105 auto const& value = iter->second;
106 if (value.compare(TRUE_STR) == 0)
107 return {true, true};
108 if (value.compare(FALSE_STR) == 0)
109 return {true, false};
110
111 JAMI_ERR("Invalid value %s for a boolean key", value.c_str());
112 return {false, false};
113}
114
115std::pair<bool, std::string>
116MediaAttribute::getStringValue(const libjami::MediaMap& map, const std::string& key)
117{
118 const auto& iter = map.find(key);
119 if (iter == map.end()) {
120 return {false, {}};
121 }
122
123 return {true, iter->second};
124}
125
126char const*
128{
129 return val ? TRUE_STR : FALSE_STR;
130}
131
132char const*
143
144bool
145MediaAttribute::hasMediaType(const std::vector<MediaAttribute>& mediaList, MediaType type)
146{
147 return mediaList.end()
148 != std::find_if(mediaList.begin(), mediaList.end(), [type](const MediaAttribute& media) {
149 return media.type_ == type;
150 });
151}
152
168
169std::vector<libjami::MediaMap>
171{
172 std::vector<libjami::MediaMap> mediaList;
173 mediaList.reserve(mediaAttrList.size());
174 for (auto const& media : mediaAttrList) {
175 mediaList.emplace_back(toMediaMap(media));
176 }
177
178 return mediaList;
179}
180
181std::string
183{
184 std::ostringstream descr;
185 descr << "type " << (type_ == MediaType::MEDIA_AUDIO ? "[AUDIO]" : "[VIDEO]");
186 descr << " ";
187 descr << "enabled " << (enabled_ ? "[YES]" : "[NO]");
188 descr << " ";
189 descr << "muted " << (muted_ ? "[YES]" : "[NO]");
190 descr << " ";
191 descr << "label [" << label_ << "]";
192
193 if (full) {
194 descr << " ";
195 descr << "source [" << sourceUri_ << "]";
196 descr << " ";
197 descr << "secure " << (secure_ ? "[YES]" : "[NO]");
198 }
199
200 return descr.str();
201}
202
203bool
208} // namespace jami
static MediaType stringToMediaType(const std::string &mediaType)
static std::vector< MediaAttribute > buildMediaAttributesList(const std::vector< libjami::MediaMap > &mediaList, bool secure)
static std::pair< bool, MediaType > getMediaType(const libjami::MediaMap &map)
static bool hasMediaType(const std::vector< MediaAttribute > &mediaList, MediaType type)
MediaAttribute(MediaType type=MediaType::MEDIA_NONE, bool muted=false, bool secure=true, bool enabled=false, std::string_view source={}, std::string_view label={}, bool onHold=false)
std::string toString(bool full=false) const
static std::pair< bool, std::string > getStringValue(const libjami::MediaMap &mediaMap, const std::string &key)
static char const * mediaTypeToString(MediaType type)
static libjami::MediaMap toMediaMap(const MediaAttribute &mediaAttr)
static std::pair< bool, bool > getBoolValue(const libjami::MediaMap &mediaMap, const std::string &key)
static std::vector< libjami::MediaMap > mediaAttributesToMediaMaps(std::vector< MediaAttribute > mediaAttrList)
static char const * boolToString(bool val)
#define JAMI_ERR(...)
Definition logger.h:218
static constexpr const char TRUE_STR[]
void emitSignal(Args... args)
Definition ring_signal.h:64
static constexpr const char FALSE_STR[]
@ MEDIA_AUDIO
Definition media_codec.h:47
@ MEDIA_VIDEO
Definition media_codec.h:48
@ MEDIA_NONE
Definition media_codec.h:46
static constexpr char ENABLED[]
Definition media_const.h:51
static constexpr char LABEL[]
Definition media_const.h:54
static constexpr char MEDIA_TYPE[]
Definition media_const.h:50
static constexpr char ON_HOLD[]
Definition media_const.h:55
static constexpr char MUTED[]
Definition media_const.h:52
static constexpr char SOURCE[]
Definition media_const.h:53
static constexpr auto SRC_TYPE_NONE
Definition media_const.h:61
std::map< std::string, std::string > MediaMap
Definition jami.h:275