tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

CodecConfig.h (8184B)


      1 /* This Source Code Form is subject to the terms of the Mozilla Public
      2 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
      3 * You can obtain one at http://mozilla.org/MPL/2.0/. */
      4 
      5 #ifndef CODEC_CONFIG_H_
      6 #define CODEC_CONFIG_H_
      7 
      8 #include <sstream>
      9 #include <string>
     10 #include <vector>
     11 
     12 #include "common/EncodingConstraints.h"
     13 #include "jsep/JsepCodecDescription.h"
     14 
     15 namespace mozilla {
     16 
     17 /**
     18 * Minimalistic Audio Codec Config Params
     19 */
     20 struct AudioCodecConfig {
     21  /*
     22   * The data-types for these properties mimic the
     23   * corresponding webrtc::CodecInst data-types.
     24   */
     25  int mType;
     26  std::string mName;
     27  int mFreq;
     28  int mChannels;
     29 
     30  bool mFECEnabled;
     31  bool mDtmfEnabled;
     32  uint32_t mFrameSizeMs;
     33  uint32_t mMaxFrameSizeMs;
     34  uint32_t mMinFrameSizeMs;
     35 
     36  AudioEncodingConstraints mEncodingConstraints;
     37 
     38  // OPUS-specific
     39  bool mDTXEnabled;
     40  uint32_t mMaxAverageBitrate;
     41  int mMaxPlaybackRate;
     42  bool mCbrEnabled;
     43 
     44  AudioCodecConfig(int type, std::string name, int freq, int channels,
     45                   bool FECEnabled)
     46      : mType(type),
     47        mName(std::move(name)),
     48        mFreq(freq),
     49        mChannels(channels),
     50        mFECEnabled(FECEnabled),
     51        mDtmfEnabled(false),
     52        mFrameSizeMs(0),
     53        mMaxFrameSizeMs(0),
     54        mMinFrameSizeMs(0),
     55        mDTXEnabled(false),
     56        mMaxAverageBitrate(0),
     57        mMaxPlaybackRate(0),
     58        mCbrEnabled(false) {}
     59 
     60  bool operator==(const AudioCodecConfig& aOther) const {
     61    return mType == aOther.mType && mName == aOther.mName &&
     62           mFreq == aOther.mFreq && mChannels == aOther.mChannels &&
     63           mFECEnabled == aOther.mFECEnabled &&
     64           mDtmfEnabled == aOther.mDtmfEnabled &&
     65           mFrameSizeMs == aOther.mFrameSizeMs &&
     66           mMaxFrameSizeMs == aOther.mMaxFrameSizeMs &&
     67           mMinFrameSizeMs == aOther.mMinFrameSizeMs &&
     68           mEncodingConstraints == aOther.mEncodingConstraints &&
     69           mDTXEnabled == aOther.mDTXEnabled &&
     70           mMaxAverageBitrate == aOther.mMaxAverageBitrate &&
     71           mMaxPlaybackRate == aOther.mMaxPlaybackRate &&
     72           mCbrEnabled == aOther.mCbrEnabled;
     73  }
     74 
     75  std::string MimeType() {
     76    std::stringstream ss;
     77    ss << "audio/" << mName;
     78    return ss.str();
     79  }
     80 };
     81 
     82 /*
     83 * Minimalistic video codec configuration
     84 * More to be added later depending on the use-case
     85 */
     86 
     87 #define MAX_SPROP_LEN 128
     88 
     89 // used for holding SDP negotiation results
     90 struct VideoCodecConfigH264 {
     91  char sprop_parameter_sets[MAX_SPROP_LEN];
     92  int packetization_mode;
     93  int profile_level_id;
     94  int tias_bw;
     95 
     96  bool operator==(const VideoCodecConfigH264& aOther) const {
     97    return strncmp(sprop_parameter_sets, aOther.sprop_parameter_sets,
     98                   MAX_SPROP_LEN) == 0 &&
     99           packetization_mode == aOther.packetization_mode &&
    100           profile_level_id == aOther.profile_level_id &&
    101           tias_bw == aOther.tias_bw;
    102  }
    103 };
    104 
    105 // class so the std::strings can get freed more easily/reliably
    106 class VideoCodecConfig {
    107 public:
    108  /*
    109   * The data-types for these properties mimic the
    110   * corresponding webrtc::VideoCodec data-types.
    111   */
    112  int mType;  // payload type
    113  std::string mName;
    114 
    115  std::vector<std::string> mAckFbTypes;
    116  std::vector<std::string> mNackFbTypes;
    117  std::vector<std::string> mCcmFbTypes;
    118  // Don't pass mOtherFbTypes from JsepVideoCodecDescription because we'd have
    119  // to drag SdpRtcpFbAttributeList::Feedback along too.
    120  bool mRembFbSet = false;
    121  bool mFECFbSet = false;
    122  bool mTransportCCFbSet = false;
    123 
    124  // TODO Bug 1920249 maybe types for these?
    125  int mULPFECPayloadType = -1;
    126  int mREDPayloadType = -1;
    127  int mREDRTXPayloadType = -1;
    128  int mRTXPayloadType = -1;
    129 
    130  uint32_t mTias = 0;
    131  VideoEncodingConstraints mEncodingConstraints;
    132  struct Encoding {
    133    std::string rid;
    134    VideoEncodingConstraints constraints;
    135    bool active = true;
    136    // TODO(bug 1744116): Use = default here
    137    bool operator==(const Encoding& aOther) const {
    138      return rid == aOther.rid && constraints == aOther.constraints &&
    139             active == aOther.active;
    140    }
    141  };
    142  std::vector<Encoding> mEncodings;
    143  std::string mSpropParameterSets;
    144  // Bug 1920249 stick these in a struct
    145  uint8_t mProfile = 0x42;
    146  uint8_t mConstraints = 0xE0;
    147  uint8_t mLevel = 0x0C;
    148  uint8_t mPacketizationMode = 1;
    149  // Bug 1920249 have the codec-specific stuff in a std::variant or something
    150  Maybe<JsepVideoCodecDescription::Av1Config> mAv1Config;
    151  // TODO: add external negotiated SPS/PPS
    152 
    153  // TODO(bug 1744116): Use = default here
    154  bool operator==(const VideoCodecConfig& aRhs) const {
    155    return mType == aRhs.mType && mName == aRhs.mName &&
    156           mAckFbTypes == aRhs.mAckFbTypes &&
    157           mNackFbTypes == aRhs.mNackFbTypes &&
    158           mCcmFbTypes == aRhs.mCcmFbTypes && mRembFbSet == aRhs.mRembFbSet &&
    159           mFECFbSet == aRhs.mFECFbSet &&
    160           mTransportCCFbSet == aRhs.mTransportCCFbSet &&
    161           mULPFECPayloadType == aRhs.mULPFECPayloadType &&
    162           mREDPayloadType == aRhs.mREDPayloadType &&
    163           mREDRTXPayloadType == aRhs.mREDRTXPayloadType &&
    164           mRTXPayloadType == aRhs.mRTXPayloadType && mTias == aRhs.mTias &&
    165           mEncodingConstraints == aRhs.mEncodingConstraints &&
    166           mEncodings == aRhs.mEncodings &&
    167           mSpropParameterSets == aRhs.mSpropParameterSets &&
    168           mProfile == aRhs.mProfile && mConstraints == aRhs.mConstraints &&
    169           mLevel == aRhs.mLevel &&
    170           mPacketizationMode == aRhs.mPacketizationMode &&
    171           mAv1Config == aRhs.mAv1Config;
    172  }
    173 
    174  static VideoCodecConfig CreateAv1Config(
    175      int pt, const VideoEncodingConstraints& constraints,
    176      const JsepVideoCodecDescription::Av1Config& av1) {
    177    VideoCodecConfig config(pt, "AV1", constraints);
    178    config.mAv1Config = Some(av1);
    179    return config;
    180  }
    181 
    182  static auto CreateH264Config(int pt,
    183                               const VideoEncodingConstraints& constraints,
    184                               const VideoCodecConfigH264& h264)
    185      -> VideoCodecConfig {
    186    VideoCodecConfig config(pt, "H264", constraints);
    187    // Bug 1920249 don't store these computed values which are used 1 time.
    188    config.mProfile = (h264.profile_level_id & 0x00FF0000) >> 16;
    189    config.mConstraints = (h264.profile_level_id & 0x0000FF00) >> 8;
    190    config.mLevel = (h264.profile_level_id & 0x000000FF);
    191    config.mPacketizationMode = h264.packetization_mode;
    192    config.mSpropParameterSets = h264.sprop_parameter_sets;
    193    return config;
    194  }
    195 
    196  VideoCodecConfig(int type, std::string name,
    197                   const VideoEncodingConstraints& constraints)
    198      : mType(type),
    199        mName(std::move(name)),
    200        mEncodingConstraints(constraints) {}
    201 
    202  bool ResolutionEquals(const VideoCodecConfig& aConfig) const {
    203    if (mEncodings.size() != aConfig.mEncodings.size()) {
    204      return false;
    205    }
    206    for (size_t i = 0; i < mEncodings.size(); ++i) {
    207      if (!mEncodings[i].constraints.ResolutionEquals(
    208              aConfig.mEncodings[i].constraints)) {
    209        return false;
    210      }
    211    }
    212    return true;
    213  }
    214 
    215  // Nothing seems to use this right now. Do we intend to support this
    216  // someday?
    217  bool RtcpFbAckIsSet(const std::string& type) const {
    218    for (auto i = mAckFbTypes.begin(); i != mAckFbTypes.end(); ++i) {
    219      if (*i == type) {
    220        return true;
    221      }
    222    }
    223    return false;
    224  }
    225 
    226  bool RtcpFbNackIsSet(const std::string& type) const {
    227    for (auto i = mNackFbTypes.begin(); i != mNackFbTypes.end(); ++i) {
    228      if (*i == type) {
    229        return true;
    230      }
    231    }
    232    return false;
    233  }
    234 
    235  bool RtcpFbCcmIsSet(const std::string& type) const {
    236    for (auto i = mCcmFbTypes.begin(); i != mCcmFbTypes.end(); ++i) {
    237      if (*i == type) {
    238        return true;
    239      }
    240    }
    241    return false;
    242  }
    243 
    244  bool RtcpFbRembIsSet() const { return mRembFbSet; }
    245 
    246  bool RtcpFbFECIsSet() const { return mFECFbSet; }
    247 
    248  bool RtcpFbTransportCCIsSet() const { return mTransportCCFbSet; }
    249 
    250  bool RtxPayloadTypeIsSet() const { return mRTXPayloadType != -1; }
    251 
    252  std::string MimeType() {
    253    std::stringstream ss;
    254    ss << "video/" << mName;
    255    return ss.str();
    256  }
    257 };
    258 }  // namespace mozilla
    259 #endif