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