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 1065 : MediaAttribute::MediaAttribute(const libjami::MediaMap& mediaMap, bool secure) 26 : { 27 1065 : std::pair<bool, MediaType> pairType = getMediaType(mediaMap); 28 1065 : if (pairType.first) 29 1065 : type_ = pairType.second; 30 : 31 1065 : std::pair<bool, bool> pairBool; 32 : 33 1065 : pairBool = getBoolValue(mediaMap, libjami::Media::MediaAttributeKey::MUTED); 34 1065 : if (pairBool.first) 35 1065 : muted_ = pairBool.second; 36 : 37 1065 : pairBool = getBoolValue(mediaMap, libjami::Media::MediaAttributeKey::ENABLED); 38 1065 : if (pairBool.first) 39 1065 : enabled_ = pairBool.second; 40 : 41 1065 : std::pair<bool, std::string> pairString; 42 1065 : pairString = getStringValue(mediaMap, libjami::Media::MediaAttributeKey::SOURCE); 43 1065 : if (pairBool.first) 44 1065 : sourceUri_ = pairString.second; 45 : 46 1065 : pairString = getStringValue(mediaMap, libjami::Media::MediaAttributeKey::LABEL); 47 1065 : if (pairBool.first) 48 1065 : label_ = pairString.second; 49 : 50 1065 : pairBool = getBoolValue(mediaMap, libjami::Media::MediaAttributeKey::ON_HOLD); 51 1065 : if (pairBool.first) 52 1016 : onHold_ = pairBool.second; 53 : 54 1065 : secure_ = secure; 55 1065 : } 56 : 57 : std::vector<MediaAttribute> 58 658 : MediaAttribute::buildMediaAttributesList(const std::vector<libjami::MediaMap>& mediaList, bool secure) 59 : { 60 658 : std::vector<MediaAttribute> mediaAttrList; 61 658 : mediaAttrList.reserve(mediaList.size()); 62 : 63 1723 : for (auto const& mediaMap : mediaList) { 64 1065 : mediaAttrList.emplace_back(MediaAttribute(mediaMap, secure)); 65 : } 66 : 67 658 : return mediaAttrList; 68 0 : } 69 : 70 : MediaType 71 1069 : MediaAttribute::stringToMediaType(const std::string& mediaType) 72 : { 73 1069 : if (mediaType.compare(libjami::Media::MediaAttributeValue::AUDIO) == 0) 74 596 : return MediaType::MEDIA_AUDIO; 75 473 : if (mediaType.compare(libjami::Media::MediaAttributeValue::VIDEO) == 0) 76 473 : return MediaType::MEDIA_VIDEO; 77 0 : return MediaType::MEDIA_NONE; 78 : } 79 : 80 : std::pair<bool, MediaType> 81 1065 : MediaAttribute::getMediaType(const libjami::MediaMap& map) 82 : { 83 1065 : const auto& iter = map.find(libjami::Media::MediaAttributeKey::MEDIA_TYPE); 84 1065 : if (iter == map.end()) { 85 0 : return {false, MediaType::MEDIA_NONE}; 86 : } 87 : 88 1065 : auto type = stringToMediaType(iter->second); 89 1065 : 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 1065 : return {true, type}; 95 : } 96 : 97 : std::pair<bool, bool> 98 3195 : MediaAttribute::getBoolValue(const libjami::MediaMap& map, const std::string& key) 99 : { 100 3195 : const auto& iter = map.find(key); 101 3195 : if (iter == map.end()) { 102 49 : return {false, false}; 103 : } 104 : 105 3146 : auto const& value = iter->second; 106 3146 : if (value.compare(TRUE_STR) == 0) 107 1098 : return {true, true}; 108 2048 : if (value.compare(FALSE_STR) == 0) 109 2048 : 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 2130 : MediaAttribute::getStringValue(const libjami::MediaMap& map, const std::string& key) 117 : { 118 2130 : const auto& iter = map.find(key); 119 2130 : if (iter == map.end()) { 120 0 : return {false, {}}; 121 : } 122 : 123 2130 : return {true, iter->second}; 124 : } 125 : 126 : char const* 127 5478 : MediaAttribute::boolToString(bool val) 128 : { 129 5478 : return val ? TRUE_STR : FALSE_STR; 130 : } 131 : 132 : char const* 133 2179 : MediaAttribute::mediaTypeToString(MediaType type) 134 : { 135 2179 : if (type == MediaType::MEDIA_AUDIO) 136 1166 : return libjami::Media::MediaAttributeValue::AUDIO; 137 1013 : if (type == MediaType::MEDIA_VIDEO) 138 959 : 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 1826 : MediaAttribute::toMediaMap(const MediaAttribute& mediaAttr) 155 : { 156 1826 : libjami::MediaMap mediaMap; 157 : 158 1826 : mediaMap.emplace(libjami::Media::MediaAttributeKey::MEDIA_TYPE, 159 1826 : mediaTypeToString(mediaAttr.type_)); 160 1826 : mediaMap.emplace(libjami::Media::MediaAttributeKey::LABEL, mediaAttr.label_); 161 1826 : mediaMap.emplace(libjami::Media::MediaAttributeKey::ENABLED, boolToString(mediaAttr.enabled_)); 162 1826 : mediaMap.emplace(libjami::Media::MediaAttributeKey::MUTED, boolToString(mediaAttr.muted_)); 163 1826 : mediaMap.emplace(libjami::Media::MediaAttributeKey::SOURCE, mediaAttr.sourceUri_); 164 1826 : mediaMap.emplace(libjami::Media::MediaAttributeKey::ON_HOLD, boolToString(mediaAttr.onHold_)); 165 : 166 1826 : return mediaMap; 167 0 : } 168 : 169 : std::vector<libjami::MediaMap> 170 992 : MediaAttribute::mediaAttributesToMediaMaps(std::vector<MediaAttribute> mediaAttrList) 171 : { 172 992 : std::vector<libjami::MediaMap> mediaList; 173 992 : mediaList.reserve(mediaAttrList.size()); 174 2813 : for (auto const& media : mediaAttrList) { 175 1821 : mediaList.emplace_back(toMediaMap(media)); 176 : } 177 : 178 992 : return mediaList; 179 0 : } 180 : 181 : std::string 182 2729 : MediaAttribute::toString(bool full) const 183 : { 184 2729 : std::ostringstream descr; 185 2729 : descr << "type " << (type_ == MediaType::MEDIA_AUDIO ? "[AUDIO]" : "[VIDEO]"); 186 2729 : descr << " "; 187 2729 : descr << "enabled " << (enabled_ ? "[YES]" : "[NO]"); 188 2729 : descr << " "; 189 2729 : descr << "muted " << (muted_ ? "[YES]" : "[NO]"); 190 2729 : descr << " "; 191 2729 : descr << "label [" << label_ << "]"; 192 : 193 2729 : if (full) { 194 2215 : descr << " "; 195 2215 : descr << "source [" << sourceUri_ << "]"; 196 2215 : descr << " "; 197 2215 : descr << "secure " << (secure_ ? "[YES]" : "[NO]"); 198 : } 199 : 200 5458 : return descr.str(); 201 2729 : } 202 : 203 : bool 204 4582 : MediaAttribute::hasValidVideo() 205 : { 206 4582 : return type_ == MediaType::MEDIA_VIDEO && enabled_&& !muted_ && !onHold_; 207 : } 208 : } // namespace jami