Line data Source code
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 : 18 : #include "media/media_attribute.h" 19 : #include "jami/media_const.h" 20 : #include "logger.h" 21 : #include "string_utils.h" 22 : 23 : namespace jami { 24 : 25 1069 : MediaAttribute::MediaAttribute(const libjami::MediaMap& mediaMap, bool secure) 26 : { 27 1069 : std::pair<bool, MediaType> pairType = getMediaType(mediaMap); 28 1069 : if (pairType.first) 29 1069 : type_ = pairType.second; 30 : 31 1069 : std::pair<bool, bool> pairBool; 32 : 33 1069 : pairBool = getBoolValue(mediaMap, libjami::Media::MediaAttributeKey::MUTED); 34 1069 : if (pairBool.first) 35 1069 : muted_ = pairBool.second; 36 : 37 1069 : pairBool = getBoolValue(mediaMap, libjami::Media::MediaAttributeKey::ENABLED); 38 1069 : if (pairBool.first) 39 1069 : enabled_ = pairBool.second; 40 : 41 1069 : std::pair<bool, std::string> pairString; 42 1069 : pairString = getStringValue(mediaMap, libjami::Media::MediaAttributeKey::SOURCE); 43 1069 : if (pairBool.first) 44 1069 : sourceUri_ = pairString.second; 45 : 46 1069 : pairString = getStringValue(mediaMap, libjami::Media::MediaAttributeKey::LABEL); 47 1069 : if (pairBool.first) 48 1069 : label_ = pairString.second; 49 : 50 1069 : pairBool = getBoolValue(mediaMap, libjami::Media::MediaAttributeKey::ON_HOLD); 51 1069 : if (pairBool.first) 52 1020 : onHold_ = pairBool.second; 53 : 54 1069 : secure_ = secure; 55 1069 : } 56 : 57 : std::vector<MediaAttribute> 58 660 : MediaAttribute::buildMediaAttributesList(const std::vector<libjami::MediaMap>& mediaList, bool secure) 59 : { 60 660 : std::vector<MediaAttribute> mediaAttrList; 61 660 : mediaAttrList.reserve(mediaList.size()); 62 : 63 1729 : for (auto const& mediaMap : mediaList) { 64 1069 : mediaAttrList.emplace_back(MediaAttribute(mediaMap, secure)); 65 : } 66 : 67 660 : return mediaAttrList; 68 0 : } 69 : 70 : MediaType 71 1073 : MediaAttribute::stringToMediaType(const std::string& mediaType) 72 : { 73 1073 : if (mediaType.compare(libjami::Media::MediaAttributeValue::AUDIO) == 0) 74 598 : return MediaType::MEDIA_AUDIO; 75 475 : if (mediaType.compare(libjami::Media::MediaAttributeValue::VIDEO) == 0) 76 475 : return MediaType::MEDIA_VIDEO; 77 0 : return MediaType::MEDIA_NONE; 78 : } 79 : 80 : std::pair<bool, MediaType> 81 1069 : MediaAttribute::getMediaType(const libjami::MediaMap& map) 82 : { 83 1069 : const auto& iter = map.find(libjami::Media::MediaAttributeKey::MEDIA_TYPE); 84 1069 : if (iter == map.end()) { 85 0 : return {false, MediaType::MEDIA_NONE}; 86 : } 87 : 88 1069 : auto type = stringToMediaType(iter->second); 89 1069 : if (type == MediaType::MEDIA_NONE) { 90 0 : JAMI_ERR("Invalid value [%s] for a media type key in media map", iter->second.c_str()); 91 0 : return {false, type}; 92 : } 93 : 94 1069 : return {true, type}; 95 : } 96 : 97 : std::pair<bool, bool> 98 3207 : MediaAttribute::getBoolValue(const libjami::MediaMap& map, const std::string& key) 99 : { 100 3207 : const auto& iter = map.find(key); 101 3207 : if (iter == map.end()) { 102 49 : return {false, false}; 103 : } 104 : 105 3158 : auto const& value = iter->second; 106 3158 : if (value.compare(TRUE_STR) == 0) 107 1102 : return {true, true}; 108 2056 : if (value.compare(FALSE_STR) == 0) 109 2056 : return {true, false}; 110 : 111 0 : JAMI_ERR("Invalid value %s for a boolean key", value.c_str()); 112 0 : return {false, false}; 113 : } 114 : 115 : std::pair<bool, std::string> 116 2138 : MediaAttribute::getStringValue(const libjami::MediaMap& map, const std::string& key) 117 : { 118 2138 : const auto& iter = map.find(key); 119 2138 : if (iter == map.end()) { 120 0 : return {false, {}}; 121 : } 122 : 123 2138 : return {true, iter->second}; 124 : } 125 : 126 : char const* 127 5502 : MediaAttribute::boolToString(bool val) 128 : { 129 5502 : return val ? TRUE_STR : FALSE_STR; 130 : } 131 : 132 : char const* 133 2187 : MediaAttribute::mediaTypeToString(MediaType type) 134 : { 135 2187 : if (type == MediaType::MEDIA_AUDIO) 136 1170 : return libjami::Media::MediaAttributeValue::AUDIO; 137 1017 : if (type == MediaType::MEDIA_VIDEO) 138 963 : return libjami::Media::MediaAttributeValue::VIDEO; 139 54 : if (type == MediaType::MEDIA_NONE) 140 54 : return libjami::Media::MediaAttributeValue::SRC_TYPE_NONE; 141 0 : return "UNKNOWN"; 142 : } 143 : 144 : bool 145 71 : MediaAttribute::hasMediaType(const std::vector<MediaAttribute>& mediaList, MediaType type) 146 : { 147 71 : return mediaList.end() 148 213 : != std::find_if(mediaList.begin(), mediaList.end(), [type](const MediaAttribute& media) { 149 129 : return media.type_ == type; 150 71 : }); 151 : } 152 : 153 : libjami::MediaMap 154 1834 : MediaAttribute::toMediaMap(const MediaAttribute& mediaAttr) 155 : { 156 1834 : libjami::MediaMap mediaMap; 157 : 158 1834 : mediaMap.emplace(libjami::Media::MediaAttributeKey::MEDIA_TYPE, 159 1834 : mediaTypeToString(mediaAttr.type_)); 160 1834 : mediaMap.emplace(libjami::Media::MediaAttributeKey::LABEL, mediaAttr.label_); 161 1834 : mediaMap.emplace(libjami::Media::MediaAttributeKey::ENABLED, boolToString(mediaAttr.enabled_)); 162 1834 : mediaMap.emplace(libjami::Media::MediaAttributeKey::MUTED, boolToString(mediaAttr.muted_)); 163 1834 : mediaMap.emplace(libjami::Media::MediaAttributeKey::SOURCE, mediaAttr.sourceUri_); 164 1834 : mediaMap.emplace(libjami::Media::MediaAttributeKey::ON_HOLD, boolToString(mediaAttr.onHold_)); 165 : 166 1834 : return mediaMap; 167 0 : } 168 : 169 : std::vector<libjami::MediaMap> 170 996 : MediaAttribute::mediaAttributesToMediaMaps(std::vector<MediaAttribute> mediaAttrList) 171 : { 172 996 : std::vector<libjami::MediaMap> mediaList; 173 996 : mediaList.reserve(mediaAttrList.size()); 174 2825 : for (auto const& media : mediaAttrList) { 175 1829 : mediaList.emplace_back(toMediaMap(media)); 176 : } 177 : 178 996 : return mediaList; 179 0 : } 180 : 181 : std::string 182 2733 : MediaAttribute::toString(bool full) const 183 : { 184 2733 : std::ostringstream descr; 185 2733 : descr << "type " << (type_ == MediaType::MEDIA_AUDIO ? "[AUDIO]" : "[VIDEO]"); 186 2733 : descr << " "; 187 2733 : descr << "enabled " << (enabled_ ? "[YES]" : "[NO]"); 188 2733 : descr << " "; 189 2733 : descr << "muted " << (muted_ ? "[YES]" : "[NO]"); 190 2733 : descr << " "; 191 2733 : descr << "label [" << label_ << "]"; 192 : 193 2733 : if (full) { 194 2223 : descr << " "; 195 2223 : descr << "source [" << sourceUri_ << "]"; 196 2223 : descr << " "; 197 2223 : descr << "secure " << (secure_ ? "[YES]" : "[NO]"); 198 : } 199 : 200 5466 : return descr.str(); 201 2733 : } 202 : 203 : bool 204 4587 : MediaAttribute::hasValidVideo() 205 : { 206 4587 : return type_ == MediaType::MEDIA_VIDEO && enabled_&& !muted_ && !onHold_; 207 : } 208 : } // namespace jami