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