Line data Source code
1 : /* 2 : * Copyright (C) 2021-2024 Savoir-faire Linux Inc. 3 : * 4 : * Author: Mohamed Chibani <mohamed.chibani@savoirfairelinux.com> 5 : * 6 : * This program is free software; you can redistribute it and/or modify 7 : * it under the terms of the GNU General Public License as published by 8 : * the Free Software Foundation; either version 3 of the License, or 9 : * (at your option) any later version. 10 : * 11 : * This program is distributed in the hope that it will be useful, 12 : * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 : * GNU General Public License for more details. 15 : * 16 : * You should have received a copy of the GNU General Public License 17 : * along with this program; if not, write to the Free Software 18 : * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 19 : */ 20 : 21 : #include "media/media_attribute.h" 22 : #include "jami/media_const.h" 23 : #include "logger.h" 24 : #include "string_utils.h" 25 : 26 : namespace jami { 27 : 28 1093 : MediaAttribute::MediaAttribute(const libjami::MediaMap& mediaMap, bool secure) 29 : { 30 1093 : std::pair<bool, MediaType> pairType = getMediaType(mediaMap); 31 1093 : if (pairType.first) 32 1093 : type_ = pairType.second; 33 : 34 1093 : std::pair<bool, bool> pairBool; 35 : 36 1093 : pairBool = getBoolValue(mediaMap, libjami::Media::MediaAttributeKey::MUTED); 37 1093 : if (pairBool.first) 38 1093 : muted_ = pairBool.second; 39 : 40 1093 : pairBool = getBoolValue(mediaMap, libjami::Media::MediaAttributeKey::ENABLED); 41 1093 : if (pairBool.first) 42 1093 : enabled_ = pairBool.second; 43 : 44 1093 : std::pair<bool, std::string> pairString; 45 1093 : pairString = getStringValue(mediaMap, libjami::Media::MediaAttributeKey::SOURCE); 46 1093 : if (pairBool.first) 47 1093 : sourceUri_ = pairString.second; 48 : 49 1093 : pairString = getStringValue(mediaMap, libjami::Media::MediaAttributeKey::LABEL); 50 1093 : if (pairBool.first) 51 1093 : label_ = pairString.second; 52 : 53 1093 : pairBool = getBoolValue(mediaMap, libjami::Media::MediaAttributeKey::ON_HOLD); 54 1093 : if (pairBool.first) 55 1044 : onHold_ = pairBool.second; 56 : 57 1093 : secure_ = secure; 58 1093 : } 59 : 60 : std::vector<MediaAttribute> 61 673 : MediaAttribute::buildMediaAttributesList(const std::vector<libjami::MediaMap>& mediaList, bool secure) 62 : { 63 673 : std::vector<MediaAttribute> mediaAttrList; 64 673 : mediaAttrList.reserve(mediaList.size()); 65 : 66 1766 : for (auto const& mediaMap : mediaList) { 67 1093 : mediaAttrList.emplace_back(MediaAttribute(mediaMap, secure)); 68 : } 69 : 70 673 : return mediaAttrList; 71 0 : } 72 : 73 : MediaType 74 1097 : MediaAttribute::stringToMediaType(const std::string& mediaType) 75 : { 76 1097 : if (mediaType.compare(libjami::Media::MediaAttributeValue::AUDIO) == 0) 77 611 : return MediaType::MEDIA_AUDIO; 78 486 : if (mediaType.compare(libjami::Media::MediaAttributeValue::VIDEO) == 0) 79 486 : return MediaType::MEDIA_VIDEO; 80 0 : return MediaType::MEDIA_NONE; 81 : } 82 : 83 : std::pair<bool, MediaType> 84 1093 : MediaAttribute::getMediaType(const libjami::MediaMap& map) 85 : { 86 1093 : const auto& iter = map.find(libjami::Media::MediaAttributeKey::MEDIA_TYPE); 87 1093 : if (iter == map.end()) { 88 0 : return {false, MediaType::MEDIA_NONE}; 89 : } 90 : 91 1093 : auto type = stringToMediaType(iter->second); 92 1093 : if (type == MediaType::MEDIA_NONE) { 93 0 : JAMI_ERR("Invalid value [%s] for a media type key in media map", iter->second.c_str()); 94 0 : return {false, type}; 95 : } 96 : 97 1093 : return {true, type}; 98 : } 99 : 100 : std::pair<bool, bool> 101 3279 : MediaAttribute::getBoolValue(const libjami::MediaMap& map, const std::string& key) 102 : { 103 3279 : const auto& iter = map.find(key); 104 3279 : if (iter == map.end()) { 105 49 : return {false, false}; 106 : } 107 : 108 3230 : auto const& value = iter->second; 109 3230 : if (value.compare(TRUE_STR) == 0) 110 1119 : return {true, true}; 111 2111 : if (value.compare(FALSE_STR) == 0) 112 2111 : return {true, false}; 113 : 114 0 : JAMI_ERR("Invalid value %s for a boolean key", value.c_str()); 115 0 : return {false, false}; 116 : } 117 : 118 : std::pair<bool, std::string> 119 2186 : MediaAttribute::getStringValue(const libjami::MediaMap& map, const std::string& key) 120 : { 121 2186 : const auto& iter = map.find(key); 122 2186 : if (iter == map.end()) { 123 0 : return {false, {}}; 124 : } 125 : 126 2186 : return {true, iter->second}; 127 : } 128 : 129 : char const* 130 5499 : MediaAttribute::boolToString(bool val) 131 : { 132 5499 : return val ? TRUE_STR : FALSE_STR; 133 : } 134 : 135 : char const* 136 2182 : MediaAttribute::mediaTypeToString(MediaType type) 137 : { 138 2182 : if (type == MediaType::MEDIA_AUDIO) 139 1198 : return libjami::Media::MediaAttributeValue::AUDIO; 140 984 : if (type == MediaType::MEDIA_VIDEO) 141 984 : return libjami::Media::MediaAttributeValue::VIDEO; 142 0 : return nullptr; 143 : } 144 : 145 : bool 146 71 : MediaAttribute::hasMediaType(const std::vector<MediaAttribute>& mediaList, MediaType type) 147 : { 148 71 : return mediaList.end() 149 213 : != std::find_if(mediaList.begin(), mediaList.end(), [type](const MediaAttribute& media) { 150 129 : return media.type_ == type; 151 71 : }); 152 : } 153 : 154 : libjami::MediaMap 155 1833 : MediaAttribute::toMediaMap(const MediaAttribute& mediaAttr) 156 : { 157 1833 : libjami::MediaMap mediaMap; 158 : 159 1833 : mediaMap.emplace(libjami::Media::MediaAttributeKey::MEDIA_TYPE, 160 1833 : mediaTypeToString(mediaAttr.type_)); 161 1833 : mediaMap.emplace(libjami::Media::MediaAttributeKey::LABEL, mediaAttr.label_); 162 1833 : mediaMap.emplace(libjami::Media::MediaAttributeKey::ENABLED, boolToString(mediaAttr.enabled_)); 163 1833 : mediaMap.emplace(libjami::Media::MediaAttributeKey::MUTED, boolToString(mediaAttr.muted_)); 164 1833 : mediaMap.emplace(libjami::Media::MediaAttributeKey::SOURCE, mediaAttr.sourceUri_); 165 1833 : mediaMap.emplace(libjami::Media::MediaAttributeKey::ON_HOLD, boolToString(mediaAttr.onHold_)); 166 : 167 1833 : return mediaMap; 168 0 : } 169 : 170 : std::vector<libjami::MediaMap> 171 1000 : MediaAttribute::mediaAttributesToMediaMaps(std::vector<MediaAttribute> mediaAttrList) 172 : { 173 1000 : std::vector<libjami::MediaMap> mediaList; 174 1000 : mediaList.reserve(mediaAttrList.size()); 175 2828 : for (auto const& media : mediaAttrList) { 176 1828 : mediaList.emplace_back(toMediaMap(media)); 177 : } 178 : 179 1000 : return mediaList; 180 0 : } 181 : 182 : std::string 183 2886 : MediaAttribute::toString(bool full) const 184 : { 185 2886 : std::ostringstream descr; 186 2886 : descr << "type " << (type_ == MediaType::MEDIA_AUDIO ? "[AUDIO]" : "[VIDEO]"); 187 2886 : descr << " "; 188 2886 : descr << "enabled " << (enabled_ ? "[YES]" : "[NO]"); 189 2886 : descr << " "; 190 2886 : descr << "muted " << (muted_ ? "[YES]" : "[NO]"); 191 2886 : descr << " "; 192 2886 : descr << "label [" << label_ << "]"; 193 : 194 2886 : if (full) { 195 2293 : descr << " "; 196 2293 : descr << "source [" << sourceUri_ << "]"; 197 2293 : descr << " "; 198 2293 : descr << "secure " << (secure_ ? "[YES]" : "[NO]"); 199 : } 200 : 201 5772 : return descr.str(); 202 2886 : } 203 : 204 : bool 205 4305 : MediaAttribute::hasValidVideo() 206 : { 207 4305 : return type_ == MediaType::MEDIA_VIDEO && enabled_&& !muted_ && !onHold_; 208 : } 209 : } // namespace jami