| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 |
| 11 | #ifndef MEDIA_BASE_CODEC_H_ |
| 12 | #define MEDIA_BASE_CODEC_H_ |
| 13 |
| 14 | #include <map> |
| 15 | #include <set> |
| 16 | #include <string> |
| 17 | #include <vector> |
| 18 |
| 19 | #include "absl/container/inlined_vector.h" |
| 20 | #include "absl/strings/string_view.h" |
| 21 | #include "absl/types/optional.h" |
| 22 | #include "api/audio_codecs/audio_format.h" |
| 23 | #include "api/rtp_parameters.h" |
| 24 | #include "api/video_codecs/sdp_video_format.h" |
| 25 | #include "media/base/media_constants.h" |
| 26 | #include "rtc_base/system/rtc_export.h" |
| 27 |
| 28 | namespace cricket { |
| 29 |
| 30 | class FeedbackParam { |
| 31 | public: |
| 32 | FeedbackParam() = default; |
| 33 | FeedbackParam(absl::string_view id, const std::string& param) |
| 34 | : id_(id), param_(param) {} |
| 35 | explicit FeedbackParam(absl::string_view id) |
| 36 | : id_(id), param_(kParamValueEmpty) {} |
| 37 |
| 38 | bool operator==(const FeedbackParam& other) const; |
| 39 | bool operator!=(const FeedbackParam& c) const { return !(*this == c); } |
| 40 |
| 41 | const std::string& id() const { return id_; } |
| 42 | const std::string& param() const { return param_; } |
| 43 |
| 44 | private: |
| 45 | std::string id_; // e.g. "nack", "ccm" |
| 46 | std::string param_; // e.g. "", "rpsi", "fir" |
| 47 | }; |
| 48 |
| 49 | class FeedbackParams { |
| 50 | public: |
| 51 | FeedbackParams(); |
| 52 | ~FeedbackParams(); |
| 53 | bool operator==(const FeedbackParams& other) const; |
| 54 | bool operator!=(const FeedbackParams& c) const { return !(*this == c); } |
| 55 |
| 56 | bool Has(const FeedbackParam& param) const; |
| 57 | void Add(const FeedbackParam& param); |
| 58 |
| 59 | void Intersect(const FeedbackParams& from); |
| 60 |
| 61 | const std::vector<FeedbackParam>& params() const { return params_; } |
| 62 |
| 63 | private: |
| 64 | bool HasDuplicateEntries() const; |
| 65 |
| 66 | std::vector<FeedbackParam> params_; |
| 67 | }; |
| 68 |
| 69 | struct RTC_EXPORT Codec { |
| 70 | enum class Type { |
| 71 | kAudio, |
| 72 | kVideo, |
| 73 | }; |
| 74 |
| 75 | enum class ResiliencyType { |
| 76 | kNone, |
| 77 | kRed, |
| 78 | kUlpfec, |
| 79 | kFlexfec, |
| 80 | kRtx, |
| 81 | }; |
| 82 |
| 83 | Type type; |
| 84 | int id; |
| 85 | std::string name; |
| 86 | int clockrate; |
| 87 |
| 88 | // Audio only |
| 89 | // Can be used to override the target bitrate in the encoder. |
| 90 | // TODO(orphis): Remove in favor of alternative APIs |
| 91 | int bitrate; |
| 92 | size_t channels; |
| 93 |
| 94 | // Video only |
| 95 | absl::optional<std::string> packetization; |
| 96 | absl::InlinedVector<webrtc::ScalabilityMode, webrtc::kScalabilityModeCount> |
| 97 | scalability_modes; |
| 98 |
| 99 | // H.265 only |
| 100 | absl::optional<std::string> tx_mode; |
| 101 |
| 102 | // Non key-value parameters such as the telephone-event "0‐15" are |
| 103 | // represented using an empty string as key, i.e. {"": "0-15"}. |
| 104 | webrtc::CodecParameterMap params; |
| 105 | FeedbackParams feedback_params; |
| 106 |
| 107 | Codec(const Codec& c); |
| 108 | Codec(Codec&& c); |
| 109 |
| 110 | virtual ~Codec(); |
| 111 |
| 112 | // Indicates if this codec is compatible with the specified codec by |
| 113 | // checking the assigned id and profile values for the relevant video codecs. |
| 114 | // For H.264, packetization modes will be compared; If H.265 is enabled, |
| 115 | // TxModes will be compared. |
| 116 | // H.264(and H.265, if enabled) levels are not compared. |
| 117 | bool Matches(const Codec& codec) const; |
| 118 | bool MatchesRtpCodec(const webrtc::RtpCodec& capability) const; |
| 119 |
| 120 | // Find the parameter for `name` and write the value to `out`. |
| 121 | bool GetParam(const std::string& name, std::string* out) const; |
| 122 | bool GetParam(const std::string& name, int* out) const; |
| 123 |
| 124 | void SetParam(const std::string& name, const std::string& value); |
| 125 | void SetParam(const std::string& name, int value); |
| 126 |
| 127 | // It is safe to input a non-existent parameter. |
| 128 | // Returns true if the parameter existed, false if it did not exist. |
| 129 | bool RemoveParam(const std::string& name); |
| 130 |
| 131 | bool HasFeedbackParam(const FeedbackParam& param) const; |
| 132 | void AddFeedbackParam(const FeedbackParam& param); |
| 133 |
| 134 | // Filter `this` feedbacks params such that only those shared by both `this` |
| 135 | // and `other` are kept. |
| 136 | void IntersectFeedbackParams(const Codec& other); |
| 137 |
| 138 | virtual webrtc::RtpCodecParameters ToCodecParameters() const; |
| 139 |
| 140 | // The codec represent an actual media codec, and not a resiliency codec. |
| 141 | bool IsMediaCodec() const; |
| 142 | // The codec represent a resiliency codec such as RED, RTX or FEC variants. |
| 143 | bool IsResiliencyCodec() const; |
| 144 | ResiliencyType GetResiliencyType() const; |
| 145 |
| 146 | // Validates a VideoCodec's payload type, dimensions and bitrates etc. If they |
| 147 | // don't make sense (such as max < min bitrate), and error is logged and |
| 148 | // ValidateCodecFormat returns false. |
| 149 | bool ValidateCodecFormat() const; |
| 150 |
| 151 | std::string ToString() const; |
| 152 |
| 153 | Codec& operator=(const Codec& c); |
| 154 | Codec& operator=(Codec&& c); |
| 155 |
| 156 | bool operator==(const Codec& c) const; |
| 157 |
| 158 | bool operator!=(const Codec& c) const { return !(*this == c); } |
| 159 |
| 160 | protected: |
| 161 | // Creates an empty codec. |
| 162 | explicit Codec(Type type); |
| 163 | // Creates a codec with the given parameters. |
| 164 | Codec(Type type, int id, const std::string& name, int clockrate); |
| 165 | Codec(Type type, |
| 166 | int id, |
| 167 | const std::string& name, |
| 168 | int clockrate, |
| 169 | size_t channels); |
| 170 |
| 171 | explicit Codec(const webrtc::SdpAudioFormat& c); |
| 172 | explicit Codec(const webrtc::SdpVideoFormat& c); |
| 173 |
| 174 | friend Codec CreateAudioCodec(int id, |
| 175 | const std::string& name, |
| 176 | int clockrate, |
| 177 | size_t channels); |
| 178 | friend Codec CreateAudioCodec(const webrtc::SdpAudioFormat& c); |
| 179 | friend Codec CreateAudioRtxCodec(int rtx_payload_type, |
| 180 | int associated_payload_type); |
| 181 | friend Codec CreateVideoCodec(int id, const std::string& name); |
| 182 | friend Codec CreateVideoCodec(const webrtc::SdpVideoFormat& c); |
| 183 | friend Codec CreateVideoRtxCodec(int rtx_payload_type, |
| 184 | int associated_payload_type); |
| 185 | }; |
| 186 |
| 187 | // TODO(webrtc:15214): Compatibility names, to be migrated away and removed. |
| 188 | using VideoCodec = Codec; |
| 189 | using AudioCodec = Codec; |
| 190 |
| 191 | using VideoCodecs = std::vector<Codec>; |
| 192 | using AudioCodecs = std::vector<Codec>; |
| 193 |
| 194 | Codec CreateAudioCodec(int id, |
| 195 | const std::string& name, |
| 196 | int clockrate, |
| 197 | size_t channels); |
| 198 | Codec CreateAudioCodec(const webrtc::SdpAudioFormat& c); |
| 199 | Codec CreateAudioRtxCodec(int rtx_payload_type, int associated_payload_type); |
| 200 | Codec CreateVideoCodec(const std::string& name); |
| 201 | Codec CreateVideoCodec(int id, const std::string& name); |
| 202 | Codec CreateVideoCodec(const webrtc::SdpVideoFormat& c); |
| 203 | Codec CreateVideoRtxCodec(int rtx_payload_type, int associated_payload_type); |
| 204 |
| 205 | // Get the codec setting associated with `payload_type`. If there |
| 206 | // is no codec associated with that payload type it returns nullptr. |
| 207 | const Codec* FindCodecById(const std::vector<Codec>& codecs, int payload_type); |
| 208 |
| 209 | bool HasLntf(const Codec& codec); |
| 210 | bool HasNack(const Codec& codec); |
| 211 | bool HasRemb(const Codec& codec); |
| 212 | bool HasRrtr(const Codec& codec); |
| 213 | bool HasTransportCc(const Codec& codec); |
| 214 |
| 215 | // Returns the first codec in `supported_codecs` that matches `codec`, or |
| 216 | // nullptr if no codec matches. |
| 217 | const Codec* FindMatchingVideoCodec(const std::vector<Codec>& supported_codecs, |
| 218 | const Codec& codec); |
| 219 |
| 220 | // Returns all codecs in `supported_codecs` that matches `codec`. |
| 221 | std::vector<const Codec*> FindAllMatchingCodecs( |
| 222 | const std::vector<Codec>& supported_codecs, |
| 223 | const Codec& codec); |
| 224 |
| 225 | RTC_EXPORT void AddH264ConstrainedBaselineProfileToSupportedFormats( |
| 226 | std::vector<webrtc::SdpVideoFormat>* supported_formats); |
| 227 |
| 228 | } // namespace cricket |
| 229 |
| 230 | #endif // MEDIA_BASE_CODEC_H_ |
| 231 |