media_codec.h 8.4 KB
Newer Older
1
/*
2
 *  Copyright (C) 2015-2019 Savoir-faire Linux Inc.
3
 *
4
 *  Author: Eloi BAIL <eloi.bail@savoirfairelinux.com>
Guillaume Roguez's avatar
Guillaume Roguez committed
5
 *  Author: Adrien Béraud <adrien.beraud@savoirfairelinux.com>
6 7 8 9 10 11 12 13 14 15 16 17 18
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  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

Edric Milaret's avatar
Edric Milaret committed
24 25 26 27
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

28 29 30
#include "audio/audiobuffer.h" // for AudioFormat
#include "ip_utils.h"

31
#include <cctype>
32 33
#include <string>
#include <vector>
34
#include <map>
35 36 37
#include <iostream>
#include <unistd.h>

Adrien Béraud's avatar
Adrien Béraud committed
38
namespace jami {
39 40

enum CodecType : unsigned {
41
    CODEC_NONE = 0, // indicates that no codec is used or defined
42 43 44 45 46 47
    CODEC_ENCODER = 1,
    CODEC_DECODER = 2,
    CODEC_ENCODER_DECODER = CODEC_ENCODER | CODEC_DECODER
};

enum MediaType : unsigned {
48
    MEDIA_NONE = 0, // indicates that no media is used or defined
49 50 51 52 53 54 55 56 57 58 59 60
    MEDIA_AUDIO = 1,
    MEDIA_VIDEO = 2,
    MEDIA_ALL = MEDIA_AUDIO | MEDIA_VIDEO
};

/*
 * SystemCodecInfo
 * represent information of a codec available on the system (using libav)
 * store default codec values
 */
struct SystemCodecInfo
{
61
    static constexpr unsigned DEFAULT_CODEC_QUALITY {30};
Adrien Béraud's avatar
Adrien Béraud committed
62
#ifdef ENABLE_VIDEO
Pierre Lespagnol's avatar
Pierre Lespagnol committed
63
    static constexpr unsigned DEFAULT_H264_MIN_QUALITY {40};
64
    static constexpr unsigned DEFAULT_H264_MAX_QUALITY {20};
65 66
    static constexpr unsigned DEFAULT_VP8_MIN_QUALITY {50};
    static constexpr unsigned DEFAULT_VP8_MAX_QUALITY {20};
67
    static constexpr unsigned DEFAULT_VIDEO_BITRATE {1200}; // in Kbits/second
68 69 70 71 72
#endif

    // indicates that the codec does not use quality factor
    static constexpr unsigned DEFAULT_NO_QUALITY {0};

73
    static constexpr unsigned DEFAULT_MIN_BITRATE {600};
Pierre Lespagnol's avatar
Pierre Lespagnol committed
74
    static constexpr unsigned DEFAULT_MAX_BITRATE {6000};
75

Adrien Béraud's avatar
Adrien Béraud committed
76 77
    SystemCodecInfo(unsigned avcodecId, const std::string& name,
                    const std::string& libName, MediaType mediaType,
78
                    CodecType codecType = CODEC_NONE, unsigned bitrate = 0,
79 80 81
                    unsigned payloadType = 0,
                    unsigned m_minQuality = DEFAULT_NO_QUALITY,
                    unsigned m_maxQuality = DEFAULT_NO_QUALITY);
82 83 84 85 86 87 88 89 90 91 92 93 94 95

    virtual ~SystemCodecInfo();

    /* generic codec information */
    unsigned id; /* id of the codec used with dbus */
    unsigned  avcodecId;  /* read as AVCodecID libav codec identifier */
    std::string name;
    std::string libName;
    CodecType codecType;
    MediaType mediaType;

    /* default codec values */
    unsigned payloadType;
    unsigned bitrate;
96 97
    unsigned minBitrate = DEFAULT_MIN_BITRATE;
    unsigned maxBitrate = DEFAULT_MAX_BITRATE;
98 99
    unsigned minQuality = DEFAULT_NO_QUALITY;
    unsigned maxQuality = DEFAULT_NO_QUALITY;
100 101 102 103 104 105 106 107 108
};

/*
 * SystemAudioCodecInfo
 * represent information of a audio codec available on the system (using libav)
 * store default codec values
 */
struct SystemAudioCodecInfo : SystemCodecInfo
{
Adrien Béraud's avatar
Adrien Béraud committed
109 110
    SystemAudioCodecInfo(unsigned avcodecId, const std::string& name,
                         const std::string& libName, CodecType type,
111 112 113 114 115 116
                         unsigned bitrate = 0,
                         unsigned sampleRate = 0, unsigned nbChannels = 0,
                         unsigned payloadType = 0);

    ~SystemAudioCodecInfo();

117
    std::map<std::string, std::string>  getCodecSpecifications();
118

119
    AudioFormat audioformat {AudioFormat::NONE()};
120 121 122 123 124 125 126 127 128
};

/*
 * SystemVideoCodecInfo
 * represent information of a video codec available on the system (using libav)
 * store default codec values
 */
struct SystemVideoCodecInfo : SystemCodecInfo
{
Adrien Béraud's avatar
Adrien Béraud committed
129 130
    SystemVideoCodecInfo(unsigned avcodecId, const std::string& name,
                         const std::string& libName, CodecType type = CODEC_NONE,
131
                         unsigned bitrate = 0,
132 133 134 135
                         unsigned m_minQuality = 0,
                         unsigned m_maxQuality = 0,
                         unsigned payloadType = 0,
                         unsigned frameRate = 0,
136
                         unsigned profileId = 0);
137 138 139

    ~SystemVideoCodecInfo();

140
    std::map<std::string, std::string>  getCodecSpecifications();
141 142 143 144 145 146 147 148 149 150 151 152 153

    unsigned frameRate;
    unsigned profileId;
    std::string parameters;
};

/*
 * AccountCodecInfo
 * represent information of a codec on a account
 * store account codec values
 */
struct AccountCodecInfo
{
154 155
    AccountCodecInfo(const SystemCodecInfo& sysCodecInfo) noexcept;
    AccountCodecInfo(const AccountCodecInfo&) noexcept = default;
Adrien Béraud's avatar
Adrien Béraud committed
156
    AccountCodecInfo(AccountCodecInfo&&) noexcept = delete;
157
    AccountCodecInfo& operator=(const AccountCodecInfo&);
Adrien Béraud's avatar
Adrien Béraud committed
158
    AccountCodecInfo& operator=(AccountCodecInfo&&) noexcept = delete;
159 160

    const SystemCodecInfo& systemCodecInfo;
Hugo Lefeuvre's avatar
Hugo Lefeuvre committed
161
    unsigned order {0}; /*used to define preferred codec list order in UI*/
162
    bool isActive {true};
163 164 165
    /* account custom values */
    unsigned payloadType;
    unsigned bitrate;
166
    unsigned quality;
167
    std::map<std::string, std::string>  getCodecSpecifications();
168 169 170 171 172 173
};

struct AccountAudioCodecInfo : AccountCodecInfo
{
    AccountAudioCodecInfo(const SystemAudioCodecInfo& sysCodecInfo);

174
    std::map<std::string, std::string>  getCodecSpecifications();
175
    void setCodecSpecifications(const std::map<std::string, std::string>& details);
176 177

    /* account custom values */
178 179
    AudioFormat audioformat {AudioFormat::NONE()};
    bool isPCMG722() const;
180 181 182 183 184 185
};

struct AccountVideoCodecInfo : AccountCodecInfo
{
    AccountVideoCodecInfo(const SystemVideoCodecInfo& sysCodecInfo);

186
    void setCodecSpecifications(const std::map<std::string, std::string>& details);
187
    std::map<std::string, std::string>  getCodecSpecifications();
188 189 190 191 192

    /* account custom values */
    unsigned frameRate;
    unsigned profileId;
    std::string parameters;
193
    bool isAutoQualityEnabled{true};
194 195 196
};
bool operator== (SystemCodecInfo codec1, SystemCodecInfo codec2);

197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
class CryptoAttribute {
public:
    CryptoAttribute() {}
    CryptoAttribute(const std::string& tag,
                    const std::string& cryptoSuite,
                    const std::string& srtpKeyMethod,
                    const std::string& srtpKeyInfo,
                    const std::string& lifetime,
                    const std::string& mkiValue,
                    const std::string& mkiLength) :
        tag_(tag),
        cryptoSuite_(cryptoSuite),
        srtpKeyMethod_(srtpKeyMethod),
        srtpKeyInfo_(srtpKeyInfo),
        lifetime_(lifetime),
        mkiValue_(mkiValue),
        mkiLength_(mkiLength) {
    }

    std::string getTag() const {
        return tag_;
    }
    std::string getCryptoSuite() const {
        return cryptoSuite_;
    }
    std::string getSrtpKeyMethod() const {
        return srtpKeyMethod_;
    }
    std::string getSrtpKeyInfo() const {
        return srtpKeyInfo_;
    }
    std::string getLifetime() const {
        return lifetime_;
    }
    std::string getMkiValue() const {
        return mkiValue_;
    }
    std::string getMkiLength() const {
        return mkiLength_;
    }

238
    inline explicit operator bool() const {
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
        return not tag_.empty();
    }

    std::string to_string() const {
        return tag_+" "+cryptoSuite_+" "+srtpKeyMethod_+":"+srtpKeyInfo_;
    }

private:
    std::string tag_;
    std::string cryptoSuite_;
    std::string srtpKeyMethod_;
    std::string srtpKeyInfo_;
    std::string lifetime_;
    std::string mkiValue_;
    std::string mkiLength_;
};

Guillaume Roguez's avatar
Guillaume Roguez committed
256 257 258 259 260 261 262 263 264 265 266 267 268
/**
 * MediaDescription
 * Negotiated RTP media slot
 */
struct MediaDescription {
    /** Audio / video */
    MediaType type {};
    bool enabled {false};
    bool holding {false};

    /** Endpoint socket address */
    IpAddr addr {};

269
    /** RTP */
270
    std::shared_ptr<AccountCodecInfo> codec {};
271
    unsigned payload_type {};
Guillaume Roguez's avatar
Guillaume Roguez committed
272 273
    std::string receiving_sdp {};
    unsigned bitrate {};
274
    unsigned rtp_clockrate {8000};
Guillaume Roguez's avatar
Guillaume Roguez committed
275 276 277 278 279 280 281 282 283 284 285

    /** Audio parameters */
    unsigned frame_size {};

    /** Video parameters */
    std::string parameters {};

    /** Crypto parameters */
    CryptoAttribute crypto {};
};

Adrien Béraud's avatar
Adrien Béraud committed
286
}//namespace jami