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 | |
kjellander | a96e2d7 | 2016-02-04 23:52:28 -0800 | [diff] [blame] | 11 | #ifndef WEBRTC_MEDIA_BASE_CODEC_H_ |
| 12 | #define WEBRTC_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 | |
Taylor Brandstetter | 0cd086b | 2016-04-20 16:23:10 -0700 | [diff] [blame] | 19 | #include "webrtc/api/rtpparameters.h" |
magjed | 1e45cc6 | 2016-10-28 07:43:45 -0700 | [diff] [blame] | 20 | #include "webrtc/common_types.h" |
kjellander | f475277 | 2016-03-02 05:42:30 -0800 | [diff] [blame] | 21 | #include "webrtc/media/base/mediaconstants.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 22 | |
| 23 | namespace cricket { |
| 24 | |
| 25 | typedef std::map<std::string, std::string> CodecParameterMap; |
| 26 | |
wu@webrtc.org | ff1b1bf | 2014-06-20 20:57:42 +0000 | [diff] [blame] | 27 | extern const int kMaxPayloadId; |
| 28 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 29 | class FeedbackParam { |
| 30 | public: |
| 31 | FeedbackParam(const std::string& id, const std::string& param) |
| 32 | : id_(id), |
| 33 | param_(param) { |
| 34 | } |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 35 | explicit FeedbackParam(const std::string& id) |
| 36 | : id_(id), |
| 37 | param_(kParamValueEmpty) { |
| 38 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 39 | bool operator==(const FeedbackParam& other) const; |
| 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 | 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_; } |
| 59 | private: |
| 60 | bool HasDuplicateEntries() const; |
| 61 | |
| 62 | std::vector<FeedbackParam> params_; |
| 63 | }; |
| 64 | |
| 65 | struct Codec { |
| 66 | int id; |
| 67 | std::string name; |
| 68 | int clockrate; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 69 | CodecParameterMap params; |
| 70 | FeedbackParams feedback_params; |
| 71 | |
| 72 | // Creates a codec with the given parameters. |
deadbeef | 67cf2c1 | 2016-04-13 10:07:16 -0700 | [diff] [blame] | 73 | Codec(int id, const std::string& name, int clockrate); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 74 | // Creates an empty codec. |
guoweis@webrtc.org | bc6961f | 2015-02-19 17:55:18 +0000 | [diff] [blame] | 75 | Codec(); |
| 76 | Codec(const Codec& c); |
magjed | 3663c52 | 2016-11-07 10:14:36 -0800 | [diff] [blame] | 77 | Codec(Codec&& c); |
Henrik Kjellander | 3fe372d | 2016-05-12 08:10:52 +0200 | [diff] [blame] | 78 | virtual ~Codec(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 79 | |
| 80 | // Indicates if this codec is compatible with the specified codec. |
| 81 | bool Matches(const Codec& codec) const; |
| 82 | |
| 83 | // Find the parameter for |name| and write the value to |out|. |
| 84 | bool GetParam(const std::string& name, std::string* out) const; |
| 85 | bool GetParam(const std::string& name, int* out) const; |
| 86 | |
| 87 | void SetParam(const std::string& name, const std::string& value); |
| 88 | void SetParam(const std::string& name, int value); |
| 89 | |
buildbot@webrtc.org | fbd1328 | 2014-06-19 19:50:55 +0000 | [diff] [blame] | 90 | // It is safe to input a non-existent parameter. |
| 91 | // Returns true if the parameter existed, false if it did not exist. |
| 92 | bool RemoveParam(const std::string& name); |
| 93 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 94 | bool HasFeedbackParam(const FeedbackParam& param) const; |
| 95 | void AddFeedbackParam(const FeedbackParam& param); |
| 96 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 97 | // Filter |this| feedbacks params such that only those shared by both |this| |
| 98 | // and |other| are kept. |
| 99 | void IntersectFeedbackParams(const Codec& other); |
| 100 | |
Taylor Brandstetter | 0cd086b | 2016-04-20 16:23:10 -0700 | [diff] [blame] | 101 | virtual webrtc::RtpCodecParameters ToCodecParameters() const; |
| 102 | |
guoweis@webrtc.org | bc6961f | 2015-02-19 17:55:18 +0000 | [diff] [blame] | 103 | Codec& operator=(const Codec& c); |
magjed | 3663c52 | 2016-11-07 10:14:36 -0800 | [diff] [blame] | 104 | Codec& operator=(Codec&& c); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 105 | |
guoweis@webrtc.org | bc6961f | 2015-02-19 17:55:18 +0000 | [diff] [blame] | 106 | bool operator==(const Codec& c) const; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 107 | |
| 108 | bool operator!=(const Codec& c) const { |
| 109 | return !(*this == c); |
| 110 | } |
| 111 | }; |
| 112 | |
| 113 | struct AudioCodec : public Codec { |
| 114 | int bitrate; |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 115 | size_t channels; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 116 | |
| 117 | // Creates a codec with the given parameters. |
pkasting | 25702cb | 2016-01-08 13:50:27 -0800 | [diff] [blame] | 118 | AudioCodec(int id, |
| 119 | const std::string& name, |
| 120 | int clockrate, |
| 121 | int bitrate, |
deadbeef | 67cf2c1 | 2016-04-13 10:07:16 -0700 | [diff] [blame] | 122 | size_t channels); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 123 | // Creates an empty codec. |
guoweis@webrtc.org | bc6961f | 2015-02-19 17:55:18 +0000 | [diff] [blame] | 124 | AudioCodec(); |
| 125 | AudioCodec(const AudioCodec& c); |
magjed | 3663c52 | 2016-11-07 10:14:36 -0800 | [diff] [blame] | 126 | AudioCodec(AudioCodec&& c); |
Henrik Kjellander | 3fe372d | 2016-05-12 08:10:52 +0200 | [diff] [blame] | 127 | virtual ~AudioCodec() = default; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 128 | |
| 129 | // Indicates if this codec is compatible with the specified codec. |
| 130 | bool Matches(const AudioCodec& codec) const; |
| 131 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 132 | std::string ToString() const; |
| 133 | |
Taylor Brandstetter | 0cd086b | 2016-04-20 16:23:10 -0700 | [diff] [blame] | 134 | webrtc::RtpCodecParameters ToCodecParameters() const override; |
| 135 | |
guoweis@webrtc.org | bc6961f | 2015-02-19 17:55:18 +0000 | [diff] [blame] | 136 | AudioCodec& operator=(const AudioCodec& c); |
magjed | 3663c52 | 2016-11-07 10:14:36 -0800 | [diff] [blame] | 137 | AudioCodec& operator=(AudioCodec&& c); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 138 | |
guoweis@webrtc.org | bc6961f | 2015-02-19 17:55:18 +0000 | [diff] [blame] | 139 | bool operator==(const AudioCodec& c) const; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 140 | |
| 141 | bool operator!=(const AudioCodec& c) const { |
| 142 | return !(*this == c); |
| 143 | } |
| 144 | }; |
| 145 | |
| 146 | struct VideoCodec : public Codec { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 147 | // Creates a codec with the given parameters. |
pkasting | 25702cb | 2016-01-08 13:50:27 -0800 | [diff] [blame] | 148 | VideoCodec(int id, const std::string& name); |
magjed | 1e45cc6 | 2016-10-28 07:43:45 -0700 | [diff] [blame] | 149 | // Creates a codec with the given name and empty id. |
| 150 | explicit VideoCodec(const std::string& name); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 151 | // Creates an empty codec. |
guoweis@webrtc.org | bc6961f | 2015-02-19 17:55:18 +0000 | [diff] [blame] | 152 | VideoCodec(); |
| 153 | VideoCodec(const VideoCodec& c); |
magjed | 3663c52 | 2016-11-07 10:14:36 -0800 | [diff] [blame] | 154 | VideoCodec(VideoCodec&& c); |
Henrik Kjellander | 3fe372d | 2016-05-12 08:10:52 +0200 | [diff] [blame] | 155 | virtual ~VideoCodec() = default; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 156 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 157 | std::string ToString() const; |
| 158 | |
guoweis@webrtc.org | bc6961f | 2015-02-19 17:55:18 +0000 | [diff] [blame] | 159 | VideoCodec& operator=(const VideoCodec& c); |
magjed | 3663c52 | 2016-11-07 10:14:36 -0800 | [diff] [blame] | 160 | VideoCodec& operator=(VideoCodec&& c); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 161 | |
guoweis@webrtc.org | bc6961f | 2015-02-19 17:55:18 +0000 | [diff] [blame] | 162 | bool operator==(const VideoCodec& c) const; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 163 | |
| 164 | bool operator!=(const VideoCodec& c) const { |
| 165 | return !(*this == c); |
| 166 | } |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 167 | |
| 168 | static VideoCodec CreateRtxCodec(int rtx_payload_type, |
| 169 | int associated_payload_type); |
| 170 | |
| 171 | enum CodecType { |
| 172 | CODEC_VIDEO, |
| 173 | CODEC_RED, |
| 174 | CODEC_ULPFEC, |
brandtr | 87d7d77 | 2016-11-07 03:03:41 -0800 | [diff] [blame] | 175 | CODEC_FLEXFEC, |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 176 | CODEC_RTX, |
| 177 | }; |
| 178 | |
| 179 | CodecType GetCodecType() const; |
| 180 | // Validates a VideoCodec's payload type, dimensions and bitrates etc. If they |
| 181 | // don't make sense (such as max < min bitrate), and error is logged and |
| 182 | // ValidateCodecFormat returns false. |
| 183 | bool ValidateCodecFormat() const; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 184 | }; |
| 185 | |
| 186 | struct DataCodec : public Codec { |
deadbeef | 67cf2c1 | 2016-04-13 10:07:16 -0700 | [diff] [blame] | 187 | DataCodec(int id, const std::string& name); |
guoweis@webrtc.org | bc6961f | 2015-02-19 17:55:18 +0000 | [diff] [blame] | 188 | DataCodec(); |
| 189 | DataCodec(const DataCodec& c); |
magjed | 3663c52 | 2016-11-07 10:14:36 -0800 | [diff] [blame] | 190 | DataCodec(DataCodec&& c); |
Henrik Kjellander | 3fe372d | 2016-05-12 08:10:52 +0200 | [diff] [blame] | 191 | virtual ~DataCodec() = default; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 192 | |
guoweis@webrtc.org | bc6961f | 2015-02-19 17:55:18 +0000 | [diff] [blame] | 193 | DataCodec& operator=(const DataCodec& c); |
magjed | 3663c52 | 2016-11-07 10:14:36 -0800 | [diff] [blame] | 194 | DataCodec& operator=(DataCodec&& c); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 195 | |
| 196 | std::string ToString() const; |
| 197 | }; |
| 198 | |
changbin.shao@webrtc.org | 2d25b44 | 2015-03-16 04:14:34 +0000 | [diff] [blame] | 199 | // Get the codec setting associated with |payload_type|. If there |
magjed | b05fa24 | 2016-11-11 04:00:16 -0800 | [diff] [blame^] | 200 | // 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] | 201 | template <class Codec> |
magjed | b05fa24 | 2016-11-11 04:00:16 -0800 | [diff] [blame^] | 202 | 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] | 203 | for (const auto& codec : codecs) { |
magjed | b05fa24 | 2016-11-11 04:00:16 -0800 | [diff] [blame^] | 204 | if (codec.id == payload_type) |
| 205 | return &codec; |
changbin.shao@webrtc.org | 2d25b44 | 2015-03-16 04:14:34 +0000 | [diff] [blame] | 206 | } |
magjed | b05fa24 | 2016-11-11 04:00:16 -0800 | [diff] [blame^] | 207 | return nullptr; |
changbin.shao@webrtc.org | 2d25b44 | 2015-03-16 04:14:34 +0000 | [diff] [blame] | 208 | } |
| 209 | |
Shao Changbin | e62202f | 2015-04-21 20:24:50 +0800 | [diff] [blame] | 210 | bool CodecNamesEq(const std::string& name1, const std::string& name2); |
magjed | 1e45cc6 | 2016-10-28 07:43:45 -0700 | [diff] [blame] | 211 | bool CodecNamesEq(const char* name1, const char* name2); |
| 212 | webrtc::VideoCodecType CodecTypeFromName(const std::string& name); |
stefan | ba4c0e4 | 2016-02-04 04:12:24 -0800 | [diff] [blame] | 213 | bool HasNack(const Codec& codec); |
| 214 | bool HasRemb(const Codec& codec); |
| 215 | bool HasTransportCc(const Codec& codec); |
magjed | 1e45cc6 | 2016-10-28 07:43:45 -0700 | [diff] [blame] | 216 | bool IsCodecSupported(const std::vector<VideoCodec>& supported_codecs, |
| 217 | const VideoCodec& codec); |
Shao Changbin | e62202f | 2015-04-21 20:24:50 +0800 | [diff] [blame] | 218 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 219 | } // namespace cricket |
| 220 | |
kjellander | a96e2d7 | 2016-02-04 23:52:28 -0800 | [diff] [blame] | 221 | #endif // WEBRTC_MEDIA_BASE_CODEC_H_ |