LCOV - code coverage report
Current view: top level - src/media - media_codec.h (source / functions) Coverage Total Hit
Test: jami-coverage-filtered.info Lines: 100.0 % 13 13
Test Date: 2026-06-13 09:18:46 Functions: 100.0 % 5 5

            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              : #pragma once
      19              : 
      20              : #ifdef HAVE_CONFIG_H
      21              : #include <config.h>
      22              : #endif
      23              : 
      24              : #include "audio/audio_format.h"
      25              : 
      26              : #include <dhtnet/ip_utils.h>
      27              : #include <cctype>
      28              : #include <string>
      29              : #include <map>
      30              : #include <memory>
      31              : #include <unistd.h>
      32              : 
      33              : namespace jami {
      34              : 
      35              : enum class KeyExchangeProtocol : uint8_t { NONE, SDES };
      36              : 
      37              : enum CodecType : uint8_t {
      38              :     CODEC_NONE = 0, // indicates that no codec is used or defined
      39              :     CODEC_ENCODER = 1,
      40              :     CODEC_DECODER = 2,
      41              :     CODEC_ENCODER_DECODER = CODEC_ENCODER | CODEC_DECODER
      42              : };
      43              : 
      44              : enum MediaType : uint8_t {
      45              :     MEDIA_NONE = 0, // indicates that no media is used or defined
      46              :     MEDIA_AUDIO = 1,
      47              :     MEDIA_VIDEO = 2,
      48              :     MEDIA_ALL = MEDIA_AUDIO | MEDIA_VIDEO
      49              : };
      50              : 
      51              : enum class RateMode : uint8_t { CRF_CONSTRAINED, CQ, CBR };
      52              : 
      53              : /*
      54              :  * SystemCodecInfo
      55              :  * represent information of a codec available on the system (using libav)
      56              :  * store default codec values
      57              :  */
      58              : struct SystemCodecInfo
      59              : {
      60              :     static constexpr unsigned DEFAULT_CODEC_QUALITY {30};
      61              : #ifdef ENABLE_VIDEO
      62              :     static constexpr unsigned DEFAULT_H264_MIN_QUALITY {40};
      63              :     static constexpr unsigned DEFAULT_H264_MAX_QUALITY {20};
      64              :     static constexpr unsigned DEFAULT_VP8_MIN_QUALITY {50};
      65              :     static constexpr unsigned DEFAULT_VP8_MAX_QUALITY {20};
      66              : #endif
      67              : 
      68              :     // indicates that the codec does not use quality factor
      69              :     static constexpr unsigned DEFAULT_NO_QUALITY {0};
      70              : 
      71              :     static constexpr unsigned DEFAULT_MIN_BITRATE {200};
      72              :     static constexpr unsigned DEFAULT_MAX_BITRATE {6000};
      73              :     static constexpr unsigned DEFAULT_VIDEO_BITRATE {800}; // in Kbits/second
      74              : 
      75              :     SystemCodecInfo(unsigned codecId,
      76              :                     unsigned avcodecId,
      77              :                     const std::string& longName,
      78              :                     const std::string& name,
      79              :                     const std::string& libName,
      80              :                     MediaType mediaType,
      81              :                     CodecType codecType = CODEC_NONE,
      82              :                     unsigned bitrate = 0,
      83              :                     unsigned payloadType = 0,
      84              :                     unsigned m_minQuality = DEFAULT_NO_QUALITY,
      85              :                     unsigned m_maxQuality = DEFAULT_NO_QUALITY);
      86              : 
      87              :     virtual ~SystemCodecInfo();
      88              :     virtual std::map<std::string, std::string> getCodecSpecifications() const;
      89              : 
      90              :     /* generic codec information */
      91              :     unsigned id;          /* id of the codec used with dbus */
      92              :     unsigned avcodecId;   /* AVCodecID libav codec identifier */
      93              :     std::string longName; /* User-friendly codec name */
      94              :     std::string
      95              :         name; /* RTP codec name as specified by http://www.iana.org/assignments/rtp-parameters/rtp-parameters.xhtml */
      96              :     std::string libName;
      97              :     CodecType codecType;
      98              :     MediaType mediaType;
      99              : 
     100              :     /* default codec values */
     101              :     unsigned payloadType;
     102              :     unsigned bitrate;
     103              :     unsigned minBitrate = DEFAULT_MIN_BITRATE;
     104              :     unsigned maxBitrate = DEFAULT_MAX_BITRATE;
     105              :     unsigned minQuality = DEFAULT_NO_QUALITY;
     106              :     unsigned maxQuality = DEFAULT_NO_QUALITY;
     107              : 
     108              :     // User-preferences from client
     109              :     unsigned order {0}; /*used to define preferred codec list order in UI*/
     110              :     bool isActive {false};
     111              :     unsigned quality;
     112              : };
     113              : 
     114              : /*
     115              :  * SystemAudioCodecInfo
     116              :  * represent information of a audio codec available on the system (using libav)
     117              :  * store default codec values
     118              :  */
     119              : struct SystemAudioCodecInfo : SystemCodecInfo
     120              : {
     121              :     SystemAudioCodecInfo(unsigned codecId,
     122              :                          unsigned avcodecId,
     123              :                          const std::string& longName,
     124              :                          const std::string& name,
     125              :                          const std::string& libName,
     126              :                          CodecType type,
     127              :                          unsigned bitrate = 0,
     128              :                          unsigned sampleRate = 0,
     129              :                          unsigned nbChannels = 0,
     130              :                          unsigned payloadType = 0,
     131              :                          AVSampleFormat sampleFormat = AV_SAMPLE_FMT_S16);
     132              : 
     133              :     ~SystemAudioCodecInfo();
     134              : 
     135              :     std::map<std::string, std::string> getCodecSpecifications() const override;
     136              :     void setCodecSpecifications(const std::map<std::string, std::string>& details);
     137              : 
     138              :     AudioFormat audioformat {AudioFormat::NONE()};
     139              :     bool isPCMG722() const;
     140              : };
     141              : 
     142              : /*
     143              :  * SystemVideoCodecInfo
     144              :  * represent information of a video codec available on the system (using libav)
     145              :  * store default codec values
     146              :  */
     147              : struct SystemVideoCodecInfo : SystemCodecInfo
     148              : {
     149              :     SystemVideoCodecInfo(unsigned codecId,
     150              :                          unsigned avcodecId,
     151              :                          const std::string& longName,
     152              :                          const std::string& name,
     153              :                          const std::string& libName,
     154              :                          CodecType type = CODEC_NONE,
     155              :                          unsigned bitrate = 0,
     156              :                          unsigned m_minQuality = 0,
     157              :                          unsigned m_maxQuality = 0,
     158              :                          unsigned payloadType = 0,
     159              :                          unsigned frameRate = 0,
     160              :                          unsigned profileId = 0);
     161              : 
     162              :     ~SystemVideoCodecInfo();
     163              : 
     164              :     void setCodecSpecifications(const std::map<std::string, std::string>& details);
     165              :     std::map<std::string, std::string> getCodecSpecifications() const override;
     166              : 
     167              :     unsigned frameRate;
     168              :     unsigned profileId;
     169              :     std::string parameters;
     170              :     bool isAutoQualityEnabled {true};
     171              : };
     172              : bool operator==(SystemCodecInfo codec1, SystemCodecInfo codec2);
     173              : 
     174              : class CryptoAttribute
     175              : {
     176              : public:
     177         2089 :     CryptoAttribute() {}
     178          734 :     CryptoAttribute(const std::string& tag,
     179              :                     const std::string& cryptoSuite,
     180              :                     const std::string& srtpKeyMethod,
     181              :                     const std::string& srtpKeyInfo,
     182              :                     const std::string& lifetime,
     183              :                     const std::string& mkiValue,
     184              :                     const std::string& mkiLength)
     185          734 :         : tag_(tag)
     186          734 :         , cryptoSuite_(cryptoSuite)
     187          734 :         , srtpKeyMethod_(srtpKeyMethod)
     188          734 :         , srtpKeyInfo_(srtpKeyInfo)
     189          734 :         , lifetime_(lifetime)
     190          734 :         , mkiValue_(mkiValue)
     191          734 :         , mkiLength_(mkiLength)
     192          734 :     {}
     193              : 
     194              :     std::string getTag() const { return tag_; }
     195         1369 :     std::string getCryptoSuite() const { return cryptoSuite_; }
     196              :     std::string getSrtpKeyMethod() const { return srtpKeyMethod_; }
     197          634 :     std::string getSrtpKeyInfo() const { return srtpKeyInfo_; }
     198              :     std::string getLifetime() const { return lifetime_; }
     199              :     std::string getMkiValue() const { return mkiValue_; }
     200              :     std::string getMkiLength() const { return mkiLength_; }
     201              : 
     202         1272 :     inline explicit operator bool() const { return not tag_.empty(); }
     203              : 
     204              :     std::string to_string() const { return tag_ + " " + cryptoSuite_ + " " + srtpKeyMethod_ + ":" + srtpKeyInfo_; }
     205              : 
     206              : private:
     207              :     std::string tag_;
     208              :     std::string cryptoSuite_;
     209              :     std::string srtpKeyMethod_;
     210              :     std::string srtpKeyInfo_;
     211              :     std::string lifetime_;
     212              :     std::string mkiValue_;
     213              :     std::string mkiLength_;
     214              : };
     215              : 
     216              : // Possible values for media direction attribute.
     217              : enum class MediaDirection : uint8_t { SENDRECV, SENDONLY, RECVONLY, INACTIVE };
     218              : 
     219              : // Possible values for media transport attribute. 'UNKNOWN' means that the
     220              : // was not set, or not found when parsing. Useful to detect errors when
     221              : // parsing the SDP.
     222              : enum class MediaTransport : uint8_t { RTP_AVP, RTP_SAVP, UNKNOWN };
     223              : 
     224              : /**
     225              :  * MediaDescription
     226              :  * Negotiated RTP media slot
     227              :  */
     228              : struct MediaDescription
     229              : {
     230              :     /** Audio / video */
     231              :     MediaType type {};
     232              :     bool enabled {false};
     233              :     bool hold {false};
     234              :     MediaDirection direction_ {MediaDirection::SENDRECV};
     235              : 
     236              :     /** Endpoint socket address */
     237              :     dhtnet::IpAddr addr {};
     238              : 
     239              :     /** RTCP socket address */
     240              :     dhtnet::IpAddr rtcp_addr {};
     241              : 
     242              :     /** RTP */
     243              :     std::shared_ptr<SystemCodecInfo> codec {};
     244              :     unsigned payload_type {};
     245              :     std::string receiving_sdp {};
     246              :     unsigned bitrate {};
     247              :     unsigned rtp_clockrate {8000};
     248              : 
     249              :     /** Audio parameters */
     250              :     unsigned frame_size {};
     251              :     bool fecEnabled {false};
     252              : 
     253              :     /** Video parameters */
     254              :     std::string parameters {};
     255              :     RateMode mode {RateMode::CRF_CONSTRAINED};
     256              :     bool linkableHW {false};
     257              : 
     258              :     /** Crypto parameters */
     259              :     CryptoAttribute crypto {};
     260              : };
     261              : } // namespace jami
        

Generated by: LCOV version 2.0-1