LCOV - code coverage report
Current view: top level - src/media - media_attribute.cpp (source / functions) Coverage Total Hit
Test: jami-coverage-filtered.info Lines: 90.0 % 110 99
Test Date: 2026-11-09 10:41:14 Functions: 100.0 % 14 14

            Line data    Source code
       1              : /*
       2              :  * Copyright (C) 2004-2026 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         1201 : MediaAttribute::MediaAttribute(const libjami::MediaMap& mediaMap, bool secure)
      26              : {
      27         1201 :     std::pair<bool, MediaType> pairType = getMediaType(mediaMap);
      28         1201 :     if (pairType.first)
      29         1201 :         type_ = pairType.second;
      30              : 
      31         1201 :     std::pair<bool, bool> pairBool;
      32              : 
      33         1201 :     pairBool = getBoolValue(mediaMap, libjami::Media::MediaAttributeKey::MUTED);
      34         1201 :     if (pairBool.first)
      35         1201 :         muted_ = pairBool.second;
      36              : 
      37         1201 :     pairBool = getBoolValue(mediaMap, libjami::Media::MediaAttributeKey::ENABLED);
      38         1201 :     if (pairBool.first)
      39         1201 :         enabled_ = pairBool.second;
      40              : 
      41         1201 :     std::pair<bool, std::string> pairString;
      42         1201 :     pairString = getStringValue(mediaMap, libjami::Media::MediaAttributeKey::SOURCE);
      43         1201 :     if (pairBool.first)
      44         1201 :         sourceUri_ = pairString.second;
      45              : 
      46         1201 :     pairString = getStringValue(mediaMap, libjami::Media::MediaAttributeKey::LABEL);
      47         1201 :     if (pairBool.first)
      48         1201 :         label_ = pairString.second;
      49              : 
      50         1201 :     pairBool = getBoolValue(mediaMap, libjami::Media::MediaAttributeKey::HOLD);
      51         1201 :     if (pairBool.first)
      52         1152 :         hold_ = pairBool.second;
      53              : 
      54         1201 :     secure_ = secure;
      55         1201 : }
      56              : 
      57              : std::vector<MediaAttribute>
      58          736 : MediaAttribute::buildMediaAttributesList(const std::vector<libjami::MediaMap>& mediaList, bool secure)
      59              : {
      60          736 :     std::vector<MediaAttribute> mediaAttrList;
      61          736 :     mediaAttrList.reserve(mediaList.size());
      62              : 
      63         1937 :     for (auto const& mediaMap : mediaList) {
      64         1201 :         mediaAttrList.emplace_back(MediaAttribute(mediaMap, secure));
      65              :     }
      66              : 
      67          736 :     return mediaAttrList;
      68            0 : }
      69              : 
      70              : MediaType
      71         1205 : MediaAttribute::stringToMediaType(const std::string& mediaType)
      72              : {
      73         1205 :     if (mediaType.compare(libjami::Media::MediaAttributeValue::AUDIO) == 0)
      74          668 :         return MediaType::MEDIA_AUDIO;
      75          537 :     if (mediaType.compare(libjami::Media::MediaAttributeValue::VIDEO) == 0)
      76          537 :         return MediaType::MEDIA_VIDEO;
      77            0 :     return MediaType::MEDIA_NONE;
      78              : }
      79              : 
      80              : std::pair<bool, MediaType>
      81         1201 : MediaAttribute::getMediaType(const libjami::MediaMap& map)
      82              : {
      83         1201 :     const auto& iter = map.find(libjami::Media::MediaAttributeKey::MEDIA_TYPE);
      84         1201 :     if (iter == map.end()) {
      85            0 :         return {false, MediaType::MEDIA_NONE};
      86              :     }
      87              : 
      88         1201 :     auto type = stringToMediaType(iter->second);
      89         1201 :     if (type == MediaType::MEDIA_NONE) {
      90            0 :         JAMI_ERROR("Invalid value [{}] for a media type key in media map", iter->second);
      91            0 :         return {false, type};
      92              :     }
      93              : 
      94         1201 :     return {true, type};
      95              : }
      96              : 
      97              : std::pair<bool, bool>
      98         3603 : MediaAttribute::getBoolValue(const libjami::MediaMap& map, const std::string& key)
      99              : {
     100         3603 :     const auto& iter = map.find(key);
     101         3603 :     if (iter == map.end()) {
     102           49 :         return {false, false};
     103              :     }
     104              : 
     105         3554 :     auto const& value = iter->second;
     106         3554 :     if (value.compare(TRUE_STR) == 0)
     107         1235 :         return {true, true};
     108         2319 :     if (value.compare(FALSE_STR) == 0)
     109         2319 :         return {true, false};
     110              : 
     111            0 :     JAMI_ERROR("Invalid value {} for a boolean key", value);
     112            0 :     return {false, false};
     113              : }
     114              : 
     115              : std::pair<bool, std::string>
     116         2402 : MediaAttribute::getStringValue(const libjami::MediaMap& map, const std::string& key)
     117              : {
     118         2402 :     const auto& iter = map.find(key);
     119         2402 :     if (iter == map.end()) {
     120            0 :         return {false, {}};
     121              :     }
     122              : 
     123         2402 :     return {true, iter->second};
     124              : }
     125              : 
     126              : char const*
     127         6237 : MediaAttribute::boolToString(bool val)
     128              : {
     129         6237 :     return val ? TRUE_STR : FALSE_STR;
     130              : }
     131              : 
     132              : char const*
     133         2492 : MediaAttribute::mediaTypeToString(MediaType type)
     134              : {
     135         2492 :     if (type == MediaType::MEDIA_AUDIO)
     136         1332 :         return libjami::Media::MediaAttributeValue::AUDIO;
     137         1160 :     if (type == MediaType::MEDIA_VIDEO)
     138         1106 :         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          117 : MediaAttribute::hasMediaType(const std::vector<MediaAttribute>& mediaList, MediaType type)
     146              : {
     147          234 :     return mediaList.end() != std::find_if(mediaList.begin(), mediaList.end(), [type](const MediaAttribute& media) {
     148          146 :                return media.type_ == type;
     149          234 :            });
     150              : }
     151              : 
     152              : libjami::MediaMap
     153         2079 : MediaAttribute::toMediaMap(const MediaAttribute& mediaAttr)
     154              : {
     155         2079 :     libjami::MediaMap mediaMap;
     156              : 
     157         2079 :     mediaMap.emplace(libjami::Media::MediaAttributeKey::MEDIA_TYPE, mediaTypeToString(mediaAttr.type_));
     158         2079 :     mediaMap.emplace(libjami::Media::MediaAttributeKey::LABEL, mediaAttr.label_);
     159         2079 :     mediaMap.emplace(libjami::Media::MediaAttributeKey::ENABLED, boolToString(mediaAttr.enabled_));
     160         2079 :     mediaMap.emplace(libjami::Media::MediaAttributeKey::MUTED, boolToString(mediaAttr.muted_));
     161         2079 :     mediaMap.emplace(libjami::Media::MediaAttributeKey::SOURCE, mediaAttr.sourceUri_);
     162         2079 :     mediaMap.emplace(libjami::Media::MediaAttributeKey::HOLD, boolToString(mediaAttr.hold_));
     163              : 
     164         2079 :     return mediaMap;
     165            0 : }
     166              : 
     167              : std::vector<libjami::MediaMap>
     168         1131 : MediaAttribute::mediaAttributesToMediaMaps(const std::vector<MediaAttribute>& mediaAttrList)
     169              : {
     170         1131 :     std::vector<libjami::MediaMap> mediaList;
     171         1131 :     mediaList.reserve(mediaAttrList.size());
     172         3207 :     for (auto const& media : mediaAttrList) {
     173         2076 :         mediaList.emplace_back(toMediaMap(media));
     174              :     }
     175              : 
     176         1131 :     return mediaList;
     177            0 : }
     178              : 
     179              : std::string
     180         3063 : MediaAttribute::toString(bool full) const
     181              : {
     182         3063 :     std::ostringstream descr;
     183         3063 :     descr << "type " << (type_ == MediaType::MEDIA_AUDIO ? "[AUDIO]" : "[VIDEO]");
     184         3063 :     descr << " ";
     185         3063 :     descr << "enabled " << (enabled_ ? "[YES]" : "[NO]");
     186         3063 :     descr << " ";
     187         3063 :     descr << "muted " << (muted_ ? "[YES]" : "[NO]");
     188         3063 :     descr << " ";
     189         3063 :     descr << "label [" << label_ << "]";
     190              : 
     191         3063 :     if (full) {
     192         2521 :         descr << " ";
     193         2521 :         descr << "source [" << sourceUri_ << "]";
     194         2521 :         descr << " ";
     195         2521 :         descr << "secure " << (secure_ ? "[YES]" : "[NO]");
     196              :     }
     197              : 
     198         6126 :     return descr.str();
     199         3063 : }
     200              : 
     201              : bool
     202         5346 : MediaAttribute::hasValidVideo()
     203              : {
     204         5346 :     return type_ == MediaType::MEDIA_VIDEO && enabled_ && !muted_ && !hold_;
     205              : }
     206              : } // namespace jami
        

Generated by: LCOV version 2.0-1