henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 1 | /* |
kjellander | 1afca73 | 2016-02-08 04:46:45 | [diff] [blame] | 2 | * Copyright (c) 2004 The WebRTC project authors. All Rights Reserved. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 3 | * |
kjellander | 1afca73 | 2016-02-08 04:46:45 | [diff] [blame] | 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 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. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 9 | */ |
| 10 | |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 11 | #ifndef MEDIA_BASE_CODEC_H_ |
| 12 | #define MEDIA_BASE_CODEC_H_ |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 13 | |
| 14 | #include <map> |
| 15 | #include <set> |
| 16 | #include <string> |
| 17 | #include <vector> |
| 18 | |
Mirta Dvornicic | 479a3c0 | 2019-06-04 13:38:50 | [diff] [blame] | 19 | #include "absl/types/optional.h" |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 20 | #include "api/rtp_parameters.h" |
Magnus Jedvert | 024d897 | 2017-09-29 13:00:29 | [diff] [blame] | 21 | #include "api/video_codecs/sdp_video_format.h" |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 22 | #include "media/base/media_constants.h" |
Mirko Bonadei | d65d179 | 2018-10-17 14:50:07 | [diff] [blame] | 23 | #include "rtc_base/system/rtc_export.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 24 | |
| 25 | namespace cricket { |
| 26 | |
| 27 | typedef std::map<std::string, std::string> CodecParameterMap; |
| 28 | |
| 29 | class FeedbackParam { |
| 30 | public: |
deadbeef | e814a0d | 2017-02-26 02:15:09 | [diff] [blame] | 31 | FeedbackParam() = default; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 32 | FeedbackParam(const std::string& id, const std::string& param) |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 33 | : id_(id), param_(param) {} |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 | [diff] [blame] | 34 | explicit FeedbackParam(const std::string& id) |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 35 | : id_(id), param_(kParamValueEmpty) {} |
Paulina Hensman | a680a6a | 2018-04-05 09:42:24 | [diff] [blame] | 36 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 37 | bool operator==(const FeedbackParam& other) const; |
| 38 | |
| 39 | const std::string& id() const { return id_; } |
| 40 | const std::string& param() const { return param_; } |
| 41 | |
| 42 | private: |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 43 | std::string id_; // e.g. "nack", "ccm" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 44 | std::string param_; // e.g. "", "rpsi", "fir" |
| 45 | }; |
| 46 | |
| 47 | class FeedbackParams { |
| 48 | public: |
Magnus Jedvert | 244ad80 | 2017-09-28 19:19:18 | [diff] [blame] | 49 | FeedbackParams(); |
Paulina Hensman | a680a6a | 2018-04-05 09:42:24 | [diff] [blame] | 50 | ~FeedbackParams(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 51 | bool operator==(const FeedbackParams& other) const; |
| 52 | |
| 53 | bool Has(const FeedbackParam& param) const; |
| 54 | void Add(const FeedbackParam& param); |
| 55 | |
| 56 | void Intersect(const FeedbackParams& from); |
| 57 | |
| 58 | const std::vector<FeedbackParam>& params() const { return params_; } |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 59 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 60 | private: |
| 61 | bool HasDuplicateEntries() const; |
| 62 | |
| 63 | std::vector<FeedbackParam> params_; |
| 64 | }; |
| 65 | |
Mirko Bonadei | d65d179 | 2018-10-17 14:50:07 | [diff] [blame] | 66 | struct RTC_EXPORT Codec { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 67 | int id; |
| 68 | std::string name; |
| 69 | int clockrate; |
Philipp Hancke | f2a4ec1 | 2020-07-01 19:07:32 | [diff] [blame] | 70 | // Non key-value parameters such as the telephone-event "0‐15" are |
| 71 | // represented using an empty string as key, i.e. {"": "0-15"}. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 72 | CodecParameterMap params; |
| 73 | FeedbackParams feedback_params; |
| 74 | |
Henrik Kjellander | 3fe372d | 2016-05-12 06:10:52 | [diff] [blame] | 75 | virtual ~Codec(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 76 | |
| 77 | // Indicates if this codec is compatible with the specified codec. |
| 78 | bool Matches(const Codec& codec) const; |
Florent Castelli | 2d9d82e | 2019-04-23 17:25:51 | [diff] [blame] | 79 | bool MatchesCapability(const webrtc::RtpCodecCapability& capability) const; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 80 | |
| 81 | // Find the parameter for |name| and write the value to |out|. |
| 82 | bool GetParam(const std::string& name, std::string* out) const; |
| 83 | bool GetParam(const std::string& name, int* out) const; |
| 84 | |
| 85 | void SetParam(const std::string& name, const std::string& value); |
| 86 | void SetParam(const std::string& name, int value); |
| 87 | |
buildbot@webrtc.org | fbd1328 | 2014-06-19 19:50:55 | [diff] [blame] | 88 | // It is safe to input a non-existent parameter. |
| 89 | // Returns true if the parameter existed, false if it did not exist. |
| 90 | bool RemoveParam(const std::string& name); |
| 91 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 92 | bool HasFeedbackParam(const FeedbackParam& param) const; |
| 93 | void AddFeedbackParam(const FeedbackParam& param); |
| 94 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 95 | // Filter |this| feedbacks params such that only those shared by both |this| |
| 96 | // and |other| are kept. |
| 97 | void IntersectFeedbackParams(const Codec& other); |
| 98 | |
Taylor Brandstetter | 0cd086b | 2016-04-20 23:23:10 | [diff] [blame] | 99 | virtual webrtc::RtpCodecParameters ToCodecParameters() const; |
| 100 | |
guoweis@webrtc.org | bc6961f | 2015-02-19 17:55:18 | [diff] [blame] | 101 | Codec& operator=(const Codec& c); |
magjed | 3663c52 | 2016-11-07 18:14:36 | [diff] [blame] | 102 | Codec& operator=(Codec&& c); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 103 | |
guoweis@webrtc.org | bc6961f | 2015-02-19 17:55:18 | [diff] [blame] | 104 | bool operator==(const Codec& c) const; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 105 | |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 106 | bool operator!=(const Codec& c) const { return !(*this == c); } |
hta | b39db84 | 2016-12-08 09:50:48 | [diff] [blame] | 107 | |
| 108 | protected: |
| 109 | // A Codec can't be created without a subclass. |
| 110 | // Creates a codec with the given parameters. |
| 111 | Codec(int id, const std::string& name, int clockrate); |
| 112 | // Creates an empty codec. |
| 113 | Codec(); |
| 114 | Codec(const Codec& c); |
| 115 | Codec(Codec&& c); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 116 | }; |
| 117 | |
| 118 | struct AudioCodec : public Codec { |
| 119 | int bitrate; |
Peter Kasting | 6955870 | 2016-01-13 00:26:35 | [diff] [blame] | 120 | size_t channels; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 121 | |
| 122 | // Creates a codec with the given parameters. |
pkasting | 25702cb | 2016-01-08 21:50:27 | [diff] [blame] | 123 | AudioCodec(int id, |
| 124 | const std::string& name, |
| 125 | int clockrate, |
| 126 | int bitrate, |
deadbeef | 67cf2c1 | 2016-04-13 17:07:16 | [diff] [blame] | 127 | size_t channels); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 128 | // Creates an empty codec. |
guoweis@webrtc.org | bc6961f | 2015-02-19 17:55:18 | [diff] [blame] | 129 | AudioCodec(); |
| 130 | AudioCodec(const AudioCodec& c); |
magjed | 3663c52 | 2016-11-07 18:14:36 | [diff] [blame] | 131 | AudioCodec(AudioCodec&& c); |
Magnus Jedvert | 244ad80 | 2017-09-28 19:19:18 | [diff] [blame] | 132 | ~AudioCodec() override = default; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 133 | |
| 134 | // Indicates if this codec is compatible with the specified codec. |
| 135 | bool Matches(const AudioCodec& codec) const; |
| 136 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 137 | std::string ToString() const; |
| 138 | |
Taylor Brandstetter | 0cd086b | 2016-04-20 23:23:10 | [diff] [blame] | 139 | webrtc::RtpCodecParameters ToCodecParameters() const override; |
| 140 | |
guoweis@webrtc.org | bc6961f | 2015-02-19 17:55:18 | [diff] [blame] | 141 | AudioCodec& operator=(const AudioCodec& c); |
magjed | 3663c52 | 2016-11-07 18:14:36 | [diff] [blame] | 142 | AudioCodec& operator=(AudioCodec&& c); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 143 | |
guoweis@webrtc.org | bc6961f | 2015-02-19 17:55:18 | [diff] [blame] | 144 | bool operator==(const AudioCodec& c) const; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 145 | |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 146 | bool operator!=(const AudioCodec& c) const { return !(*this == c); } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 147 | }; |
| 148 | |
Mirko Bonadei | d65d179 | 2018-10-17 14:50:07 | [diff] [blame] | 149 | struct RTC_EXPORT VideoCodec : public Codec { |
Mirta Dvornicic | 479a3c0 | 2019-06-04 13:38:50 | [diff] [blame] | 150 | absl::optional<std::string> packetization; |
| 151 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 152 | // Creates a codec with the given parameters. |
pkasting | 25702cb | 2016-01-08 21:50:27 | [diff] [blame] | 153 | VideoCodec(int id, const std::string& name); |
magjed | 1e45cc6 | 2016-10-28 14:43:45 | [diff] [blame] | 154 | // Creates a codec with the given name and empty id. |
| 155 | explicit VideoCodec(const std::string& name); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 156 | // Creates an empty codec. |
guoweis@webrtc.org | bc6961f | 2015-02-19 17:55:18 | [diff] [blame] | 157 | VideoCodec(); |
| 158 | VideoCodec(const VideoCodec& c); |
Magnus Jedvert | 024d897 | 2017-09-29 13:00:29 | [diff] [blame] | 159 | explicit VideoCodec(const webrtc::SdpVideoFormat& c); |
magjed | 3663c52 | 2016-11-07 18:14:36 | [diff] [blame] | 160 | VideoCodec(VideoCodec&& c); |
Magnus Jedvert | 244ad80 | 2017-09-28 19:19:18 | [diff] [blame] | 161 | ~VideoCodec() override = default; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 162 | |
magjed | f823ede | 2016-11-12 17:53:04 | [diff] [blame] | 163 | // Indicates if this video codec is the same as the other video codec, e.g. if |
| 164 | // they are both VP8 or VP9, or if they are both H264 with the same H264 |
| 165 | // profile. H264 levels however are not compared. |
| 166 | bool Matches(const VideoCodec& codec) const; |
| 167 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 168 | std::string ToString() const; |
| 169 | |
deadbeef | e702b30 | 2017-02-04 20:09:01 | [diff] [blame] | 170 | webrtc::RtpCodecParameters ToCodecParameters() const override; |
| 171 | |
guoweis@webrtc.org | bc6961f | 2015-02-19 17:55:18 | [diff] [blame] | 172 | VideoCodec& operator=(const VideoCodec& c); |
magjed | 3663c52 | 2016-11-07 18:14:36 | [diff] [blame] | 173 | VideoCodec& operator=(VideoCodec&& c); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 174 | |
guoweis@webrtc.org | bc6961f | 2015-02-19 17:55:18 | [diff] [blame] | 175 | bool operator==(const VideoCodec& c) const; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 176 | |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 177 | bool operator!=(const VideoCodec& c) const { return !(*this == c); } |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 | [diff] [blame] | 178 | |
Mirta Dvornicic | 479a3c0 | 2019-06-04 13:38:50 | [diff] [blame] | 179 | // Return packetization which both |local_codec| and |remote_codec| support. |
| 180 | static absl::optional<std::string> IntersectPacketization( |
| 181 | const VideoCodec& local_codec, |
| 182 | const VideoCodec& remote_codec); |
| 183 | |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 | [diff] [blame] | 184 | static VideoCodec CreateRtxCodec(int rtx_payload_type, |
| 185 | int associated_payload_type); |
| 186 | |
| 187 | enum CodecType { |
| 188 | CODEC_VIDEO, |
| 189 | CODEC_RED, |
| 190 | CODEC_ULPFEC, |
brandtr | 87d7d77 | 2016-11-07 11:03:41 | [diff] [blame] | 191 | CODEC_FLEXFEC, |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 | [diff] [blame] | 192 | CODEC_RTX, |
| 193 | }; |
| 194 | |
| 195 | CodecType GetCodecType() const; |
| 196 | // Validates a VideoCodec's payload type, dimensions and bitrates etc. If they |
| 197 | // don't make sense (such as max < min bitrate), and error is logged and |
| 198 | // ValidateCodecFormat returns false. |
| 199 | bool ValidateCodecFormat() const; |
hta | 9aa9688 | 2016-12-06 13:36:03 | [diff] [blame] | 200 | |
| 201 | private: |
| 202 | void SetDefaultParameters(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 203 | }; |
| 204 | |
Harald Alvestrand | 5fc28b1 | 2019-05-13 11:36:16 | [diff] [blame] | 205 | struct RtpDataCodec : public Codec { |
| 206 | RtpDataCodec(int id, const std::string& name); |
| 207 | RtpDataCodec(); |
| 208 | RtpDataCodec(const RtpDataCodec& c); |
| 209 | RtpDataCodec(RtpDataCodec&& c); |
| 210 | ~RtpDataCodec() override = default; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 211 | |
Harald Alvestrand | 5fc28b1 | 2019-05-13 11:36:16 | [diff] [blame] | 212 | RtpDataCodec& operator=(const RtpDataCodec& c); |
| 213 | RtpDataCodec& operator=(RtpDataCodec&& c); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 214 | |
| 215 | std::string ToString() const; |
| 216 | }; |
| 217 | |
Harald Alvestrand | 5fc28b1 | 2019-05-13 11:36:16 | [diff] [blame] | 218 | // For backwards compatibility |
| 219 | // TODO(bugs.webrtc.org/10597): Remove when no longer needed. |
| 220 | typedef RtpDataCodec DataCodec; |
| 221 | |
changbin.shao@webrtc.org | 2d25b44 | 2015-03-16 04:14:34 | [diff] [blame] | 222 | // Get the codec setting associated with |payload_type|. If there |
magjed | b05fa24 | 2016-11-11 12:00:16 | [diff] [blame] | 223 | // is no codec associated with that payload type it returns nullptr. |
changbin.shao@webrtc.org | 2d25b44 | 2015-03-16 04:14:34 | [diff] [blame] | 224 | template <class Codec> |
magjed | b05fa24 | 2016-11-11 12:00:16 | [diff] [blame] | 225 | const Codec* FindCodecById(const std::vector<Codec>& codecs, int payload_type) { |
changbin.shao@webrtc.org | 2d25b44 | 2015-03-16 04:14:34 | [diff] [blame] | 226 | for (const auto& codec : codecs) { |
magjed | b05fa24 | 2016-11-11 12:00:16 | [diff] [blame] | 227 | if (codec.id == payload_type) |
| 228 | return &codec; |
changbin.shao@webrtc.org | 2d25b44 | 2015-03-16 04:14:34 | [diff] [blame] | 229 | } |
magjed | b05fa24 | 2016-11-11 12:00:16 | [diff] [blame] | 230 | return nullptr; |
changbin.shao@webrtc.org | 2d25b44 | 2015-03-16 04:14:34 | [diff] [blame] | 231 | } |
| 232 | |
Elad Alon | fadb181 | 2019-05-24 11:40:02 | [diff] [blame] | 233 | bool HasLntf(const Codec& codec); |
stefan | ba4c0e4 | 2016-02-04 12:12:24 | [diff] [blame] | 234 | bool HasNack(const Codec& codec); |
| 235 | bool HasRemb(const Codec& codec); |
Ilya Nikolaevskiy | 634a777 | 2018-04-04 14:33:49 | [diff] [blame] | 236 | bool HasRrtr(const Codec& codec); |
stefan | ba4c0e4 | 2016-02-04 12:12:24 | [diff] [blame] | 237 | bool HasTransportCc(const Codec& codec); |
magjed | f823ede | 2016-11-12 17:53:04 | [diff] [blame] | 238 | // Returns the first codec in |supported_codecs| that matches |codec|, or |
| 239 | // nullptr if no codec matches. |
| 240 | const VideoCodec* FindMatchingCodec( |
| 241 | const std::vector<VideoCodec>& supported_codecs, |
| 242 | const VideoCodec& codec); |
Mirko Bonadei | 66e7679 | 2019-04-02 09:33:59 | [diff] [blame] | 243 | RTC_EXPORT bool IsSameCodec(const std::string& name1, |
| 244 | const CodecParameterMap& params1, |
| 245 | const std::string& name2, |
| 246 | const CodecParameterMap& params2); |
Shao Changbin | e62202f | 2015-04-21 12:24:50 | [diff] [blame] | 247 | |
Johannes Kron | 3e98368 | 2020-03-29 20:17:00 | [diff] [blame] | 248 | RTC_EXPORT void AddH264ConstrainedBaselineProfileToSupportedFormats( |
| 249 | std::vector<webrtc::SdpVideoFormat>* supported_formats); |
| 250 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 251 | } // namespace cricket |
| 252 | |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 253 | #endif // MEDIA_BASE_CODEC_H_ |