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