Line data Source code
1 : /*
2 : * Copyright (C) 2004-2024 Savoir-faire Linux Inc.
3 : *
4 : * Author: Eloi BAIL <eloi.bail@savoirfairelinux.com>
5 : * Author: Adrien BĂ©raud <adrien.beraud@savoirfairelinux.com>
6 : *
7 : * This program is free software; you can redistribute it and/or modify
8 : * it under the terms of the GNU General Public License as published by
9 : * the Free Software Foundation; either version 3 of the License, or
10 : * (at your option) any later version.
11 : *
12 : * This program is distributed in the hope that it will be useful,
13 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 : * GNU General Public License for more details.
16 : *
17 : * You should have received a copy of the GNU General Public License
18 : * along with this program; if not, write to the Free Software
19 : * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 : */
21 :
22 : #pragma once
23 :
24 : #ifdef HAVE_CONFIG_H
25 : #include <config.h>
26 : #endif
27 :
28 : #include "audio/audio_format.h"
29 :
30 : #include <dhtnet/ip_utils.h>
31 : #include <cctype>
32 : #include <string>
33 : #include <vector>
34 : #include <map>
35 : #include <memory>
36 : #include <iostream>
37 : #include <unistd.h>
38 :
39 : namespace jami {
40 :
41 : enum class KeyExchangeProtocol { NONE, SDES };
42 :
43 : enum CodecType : unsigned {
44 : CODEC_NONE = 0, // indicates that no codec is used or defined
45 : CODEC_ENCODER = 1,
46 : CODEC_DECODER = 2,
47 : CODEC_ENCODER_DECODER = CODEC_ENCODER | CODEC_DECODER
48 : };
49 :
50 : enum MediaType : unsigned {
51 : MEDIA_NONE = 0, // indicates that no media is used or defined
52 : MEDIA_AUDIO = 1,
53 : MEDIA_VIDEO = 2,
54 : MEDIA_ALL = MEDIA_AUDIO | MEDIA_VIDEO
55 : };
56 :
57 : enum class RateMode : unsigned { CRF_CONSTRAINED, CQ, CBR };
58 :
59 : /*
60 : * SystemCodecInfo
61 : * represent information of a codec available on the system (using libav)
62 : * store default codec values
63 : */
64 : struct SystemCodecInfo
65 : {
66 : static constexpr unsigned DEFAULT_CODEC_QUALITY {30};
67 : #ifdef ENABLE_VIDEO
68 : static constexpr unsigned DEFAULT_H264_MIN_QUALITY {40};
69 : static constexpr unsigned DEFAULT_H264_MAX_QUALITY {20};
70 : static constexpr unsigned DEFAULT_VP8_MIN_QUALITY {50};
71 : static constexpr unsigned DEFAULT_VP8_MAX_QUALITY {20};
72 : #endif
73 :
74 : // indicates that the codec does not use quality factor
75 : static constexpr unsigned DEFAULT_NO_QUALITY {0};
76 :
77 : static constexpr unsigned DEFAULT_MIN_BITRATE {200};
78 : static constexpr unsigned DEFAULT_MAX_BITRATE {6000};
79 : static constexpr unsigned DEFAULT_VIDEO_BITRATE {800}; // in Kbits/second
80 :
81 : SystemCodecInfo(unsigned codecId,
82 : unsigned avcodecId,
83 : const std::string& longName,
84 : const std::string& name,
85 : const std::string& libName,
86 : MediaType mediaType,
87 : CodecType codecType = CODEC_NONE,
88 : unsigned bitrate = 0,
89 : unsigned payloadType = 0,
90 : unsigned m_minQuality = DEFAULT_NO_QUALITY,
91 : unsigned m_maxQuality = DEFAULT_NO_QUALITY);
92 :
93 : virtual ~SystemCodecInfo();
94 : virtual std::map<std::string, std::string> getCodecSpecifications() const;
95 :
96 : /* generic codec information */
97 : unsigned id; /* id of the codec used with dbus */
98 : unsigned avcodecId; /* AVCodecID libav codec identifier */
99 : std::string longName; /* User-friendly codec name */
100 : std::string name; /* RTP codec name as specified by http://www.iana.org/assignments/rtp-parameters/rtp-parameters.xhtml */
101 : std::string libName;
102 : CodecType codecType;
103 : MediaType mediaType;
104 :
105 : /* default codec values */
106 : unsigned payloadType;
107 : unsigned bitrate;
108 : unsigned minBitrate = DEFAULT_MIN_BITRATE;
109 : unsigned maxBitrate = DEFAULT_MAX_BITRATE;
110 : unsigned minQuality = DEFAULT_NO_QUALITY;
111 : unsigned maxQuality = DEFAULT_NO_QUALITY;
112 :
113 : // User-preferences from client
114 : unsigned order {0}; /*used to define preferred codec list order in UI*/
115 : bool isActive {false};
116 : unsigned quality;
117 : };
118 :
119 : /*
120 : * SystemAudioCodecInfo
121 : * represent information of a audio codec available on the system (using libav)
122 : * store default codec values
123 : */
124 : struct SystemAudioCodecInfo : SystemCodecInfo
125 : {
126 : SystemAudioCodecInfo(unsigned codecId,
127 : unsigned avcodecId,
128 : const std::string& longName,
129 : const std::string& name,
130 : const std::string& libName,
131 : CodecType type,
132 : unsigned bitrate = 0,
133 : unsigned sampleRate = 0,
134 : unsigned nbChannels = 0,
135 : unsigned payloadType = 0,
136 : AVSampleFormat sampleFormat = AV_SAMPLE_FMT_S16);
137 :
138 : ~SystemAudioCodecInfo();
139 :
140 : std::map<std::string, std::string> getCodecSpecifications() const override;
141 : void setCodecSpecifications(const std::map<std::string, std::string>& details);
142 :
143 : AudioFormat audioformat {AudioFormat::NONE()};
144 : bool isPCMG722() const;
145 : };
146 :
147 : /*
148 : * SystemVideoCodecInfo
149 : * represent information of a video codec available on the system (using libav)
150 : * store default codec values
151 : */
152 : struct SystemVideoCodecInfo : SystemCodecInfo
153 : {
154 : SystemVideoCodecInfo(unsigned codecId,
155 : unsigned avcodecId,
156 : const std::string& longName,
157 : const std::string& name,
158 : const std::string& libName,
159 : CodecType type = CODEC_NONE,
160 : unsigned bitrate = 0,
161 : unsigned m_minQuality = 0,
162 : unsigned m_maxQuality = 0,
163 : unsigned payloadType = 0,
164 : unsigned frameRate = 0,
165 : unsigned profileId = 0);
166 :
167 : ~SystemVideoCodecInfo();
168 :
169 : void setCodecSpecifications(const std::map<std::string, std::string>& details);
170 : std::map<std::string, std::string> getCodecSpecifications() const override;
171 :
172 : unsigned frameRate;
173 : unsigned profileId;
174 : std::string parameters;
175 : bool isAutoQualityEnabled {true};
176 : };
177 : bool operator==(SystemCodecInfo codec1, SystemCodecInfo codec2);
178 :
179 : class CryptoAttribute
180 : {
181 : public:
182 2242 : CryptoAttribute() {}
183 766 : CryptoAttribute(const std::string& tag,
184 : const std::string& cryptoSuite,
185 : const std::string& srtpKeyMethod,
186 : const std::string& srtpKeyInfo,
187 : const std::string& lifetime,
188 : const std::string& mkiValue,
189 : const std::string& mkiLength)
190 766 : : tag_(tag)
191 766 : , cryptoSuite_(cryptoSuite)
192 766 : , srtpKeyMethod_(srtpKeyMethod)
193 766 : , srtpKeyInfo_(srtpKeyInfo)
194 766 : , lifetime_(lifetime)
195 766 : , mkiValue_(mkiValue)
196 766 : , mkiLength_(mkiLength)
197 766 : {}
198 :
199 : std::string getTag() const { return tag_; }
200 1434 : std::string getCryptoSuite() const { return cryptoSuite_; }
201 : std::string getSrtpKeyMethod() const { return srtpKeyMethod_; }
202 668 : std::string getSrtpKeyInfo() const { return srtpKeyInfo_; }
203 : std::string getLifetime() const { return lifetime_; }
204 : std::string getMkiValue() const { return mkiValue_; }
205 : std::string getMkiLength() const { return mkiLength_; }
206 :
207 1336 : inline explicit operator bool() const { return not tag_.empty(); }
208 :
209 : std::string to_string() const
210 : {
211 : return tag_ + " " + cryptoSuite_ + " " + srtpKeyMethod_ + ":" + srtpKeyInfo_;
212 : }
213 :
214 : private:
215 : std::string tag_;
216 : std::string cryptoSuite_;
217 : std::string srtpKeyMethod_;
218 : std::string srtpKeyInfo_;
219 : std::string lifetime_;
220 : std::string mkiValue_;
221 : std::string mkiLength_;
222 : };
223 :
224 : // Possible values for media direction attribute. 'UNKNOWN' means that the
225 : // direction was not set yet. Useful to detect errors when parsing the SDP.
226 : enum class MediaDirection { SENDRECV, SENDONLY, RECVONLY, INACTIVE, UNKNOWN };
227 :
228 : // Possible values for media transport attribute. 'UNKNOWN' means that the
229 : // was not set, or not found when parsing. Useful to detect errors when
230 : // parsing the SDP.
231 : enum class MediaTransport { RTP_AVP, RTP_SAVP, UNKNOWN };
232 :
233 : /**
234 : * MediaDescription
235 : * Negotiated RTP media slot
236 : */
237 : struct MediaDescription
238 : {
239 : /** Audio / video */
240 : MediaType type {};
241 : bool enabled {false};
242 : bool onHold {false};
243 : MediaDirection direction_ {MediaDirection::UNKNOWN};
244 :
245 : /** Endpoint socket address */
246 : dhtnet::IpAddr addr {};
247 :
248 : /** RTCP socket address */
249 : dhtnet::IpAddr rtcp_addr {};
250 :
251 : /** RTP */
252 : std::shared_ptr<SystemCodecInfo> codec {};
253 : unsigned payload_type {};
254 : std::string receiving_sdp {};
255 : unsigned bitrate {};
256 : unsigned rtp_clockrate {8000};
257 :
258 : /** Audio parameters */
259 : unsigned frame_size {};
260 : bool fecEnabled {false};
261 :
262 : /** Video parameters */
263 : std::string parameters {};
264 : RateMode mode {RateMode::CRF_CONSTRAINED};
265 : bool linkableHW {false};
266 :
267 : /** Crypto parameters */
268 : CryptoAttribute crypto {};
269 : };
270 : } // namespace jami
|