henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1 | /* |
| 2 | * libjingle |
| 3 | * Copyright 2004 Google Inc. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions are met: |
| 7 | * |
| 8 | * 1. Redistributions of source code must retain the above copyright notice, |
| 9 | * this list of conditions and the following disclaimer. |
| 10 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
| 11 | * this list of conditions and the following disclaimer in the documentation |
| 12 | * and/or other materials provided with the distribution. |
| 13 | * 3. The name of the author may not be used to endorse or promote products |
| 14 | * derived from this software without specific prior written permission. |
| 15 | * |
| 16 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED |
| 17 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 18 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO |
| 19 | * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 20 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 21 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; |
| 22 | * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 23 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
| 24 | * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
| 25 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 | */ |
| 27 | |
| 28 | #ifndef TALK_MEDIA_BASE_CODEC_H_ |
| 29 | #define TALK_MEDIA_BASE_CODEC_H_ |
| 30 | |
| 31 | #include <map> |
| 32 | #include <set> |
| 33 | #include <string> |
| 34 | #include <vector> |
| 35 | |
| 36 | #include "talk/media/base/constants.h" |
| 37 | |
| 38 | namespace cricket { |
| 39 | |
| 40 | typedef std::map<std::string, std::string> CodecParameterMap; |
| 41 | |
wu@webrtc.org | ff1b1bf | 2014-06-20 20:57:42 +0000 | [diff] [blame] | 42 | extern const int kMaxPayloadId; |
| 43 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 44 | class FeedbackParam { |
| 45 | public: |
| 46 | FeedbackParam(const std::string& id, const std::string& param) |
| 47 | : id_(id), |
| 48 | param_(param) { |
| 49 | } |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 50 | explicit FeedbackParam(const std::string& id) |
| 51 | : id_(id), |
| 52 | param_(kParamValueEmpty) { |
| 53 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 54 | bool operator==(const FeedbackParam& other) const; |
| 55 | |
| 56 | const std::string& id() const { return id_; } |
| 57 | const std::string& param() const { return param_; } |
| 58 | |
| 59 | private: |
| 60 | std::string id_; // e.g. "nack", "ccm" |
| 61 | std::string param_; // e.g. "", "rpsi", "fir" |
| 62 | }; |
| 63 | |
| 64 | class FeedbackParams { |
| 65 | public: |
| 66 | bool operator==(const FeedbackParams& other) const; |
| 67 | |
| 68 | bool Has(const FeedbackParam& param) const; |
| 69 | void Add(const FeedbackParam& param); |
| 70 | |
| 71 | void Intersect(const FeedbackParams& from); |
| 72 | |
| 73 | const std::vector<FeedbackParam>& params() const { return params_; } |
| 74 | private: |
| 75 | bool HasDuplicateEntries() const; |
| 76 | |
| 77 | std::vector<FeedbackParam> params_; |
| 78 | }; |
| 79 | |
| 80 | struct Codec { |
| 81 | int id; |
| 82 | std::string name; |
| 83 | int clockrate; |
| 84 | int preference; |
| 85 | CodecParameterMap params; |
| 86 | FeedbackParams feedback_params; |
| 87 | |
| 88 | // Creates a codec with the given parameters. |
guoweis@webrtc.org | bc6961f | 2015-02-19 17:55:18 +0000 | [diff] [blame] | 89 | Codec(int id, const std::string& name, int clockrate, int preference); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 90 | // Creates an empty codec. |
guoweis@webrtc.org | bc6961f | 2015-02-19 17:55:18 +0000 | [diff] [blame] | 91 | Codec(); |
| 92 | Codec(const Codec& c); |
| 93 | ~Codec(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 94 | |
| 95 | // Indicates if this codec is compatible with the specified codec. |
| 96 | bool Matches(const Codec& codec) const; |
| 97 | |
| 98 | // Find the parameter for |name| and write the value to |out|. |
| 99 | bool GetParam(const std::string& name, std::string* out) const; |
| 100 | bool GetParam(const std::string& name, int* out) const; |
| 101 | |
| 102 | void SetParam(const std::string& name, const std::string& value); |
| 103 | void SetParam(const std::string& name, int value); |
| 104 | |
buildbot@webrtc.org | fbd1328 | 2014-06-19 19:50:55 +0000 | [diff] [blame] | 105 | // It is safe to input a non-existent parameter. |
| 106 | // Returns true if the parameter existed, false if it did not exist. |
| 107 | bool RemoveParam(const std::string& name); |
| 108 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 109 | bool HasFeedbackParam(const FeedbackParam& param) const; |
| 110 | void AddFeedbackParam(const FeedbackParam& param); |
| 111 | |
| 112 | static bool Preferable(const Codec& first, const Codec& other) { |
| 113 | return first.preference > other.preference; |
| 114 | } |
| 115 | |
| 116 | // Filter |this| feedbacks params such that only those shared by both |this| |
| 117 | // and |other| are kept. |
| 118 | void IntersectFeedbackParams(const Codec& other); |
| 119 | |
guoweis@webrtc.org | bc6961f | 2015-02-19 17:55:18 +0000 | [diff] [blame] | 120 | Codec& operator=(const Codec& c); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 121 | |
guoweis@webrtc.org | bc6961f | 2015-02-19 17:55:18 +0000 | [diff] [blame] | 122 | bool operator==(const Codec& c) const; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 123 | |
| 124 | bool operator!=(const Codec& c) const { |
| 125 | return !(*this == c); |
| 126 | } |
| 127 | }; |
| 128 | |
| 129 | struct AudioCodec : public Codec { |
| 130 | int bitrate; |
| 131 | int channels; |
| 132 | |
| 133 | // Creates a codec with the given parameters. |
guoweis@webrtc.org | bc6961f | 2015-02-19 17:55:18 +0000 | [diff] [blame] | 134 | AudioCodec(int pt, const std::string& nm, int cr, int br, int cs, int pr); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 135 | // Creates an empty codec. |
guoweis@webrtc.org | bc6961f | 2015-02-19 17:55:18 +0000 | [diff] [blame] | 136 | AudioCodec(); |
| 137 | AudioCodec(const AudioCodec& c); |
| 138 | ~AudioCodec() = default; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 139 | |
| 140 | // Indicates if this codec is compatible with the specified codec. |
| 141 | bool Matches(const AudioCodec& codec) const; |
| 142 | |
| 143 | static bool Preferable(const AudioCodec& first, const AudioCodec& other) { |
| 144 | return first.preference > other.preference; |
| 145 | } |
| 146 | |
| 147 | std::string ToString() const; |
| 148 | |
guoweis@webrtc.org | bc6961f | 2015-02-19 17:55:18 +0000 | [diff] [blame] | 149 | AudioCodec& operator=(const AudioCodec& c); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 150 | |
guoweis@webrtc.org | bc6961f | 2015-02-19 17:55:18 +0000 | [diff] [blame] | 151 | bool operator==(const AudioCodec& c) const; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 152 | |
| 153 | bool operator!=(const AudioCodec& c) const { |
| 154 | return !(*this == c); |
| 155 | } |
| 156 | }; |
| 157 | |
| 158 | struct VideoCodec : public Codec { |
| 159 | int width; |
| 160 | int height; |
| 161 | int framerate; |
| 162 | |
| 163 | // Creates a codec with the given parameters. |
guoweis@webrtc.org | bc6961f | 2015-02-19 17:55:18 +0000 | [diff] [blame] | 164 | VideoCodec(int pt, const std::string& nm, int w, int h, int fr, int pr); |
Shao Changbin | e62202f | 2015-04-21 20:24:50 +0800 | [diff] [blame] | 165 | VideoCodec(int pt, const std::string& nm); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 166 | // Creates an empty codec. |
guoweis@webrtc.org | bc6961f | 2015-02-19 17:55:18 +0000 | [diff] [blame] | 167 | VideoCodec(); |
| 168 | VideoCodec(const VideoCodec& c); |
| 169 | ~VideoCodec() = default; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 170 | |
| 171 | static bool Preferable(const VideoCodec& first, const VideoCodec& other) { |
| 172 | return first.preference > other.preference; |
| 173 | } |
| 174 | |
| 175 | std::string ToString() const; |
| 176 | |
guoweis@webrtc.org | bc6961f | 2015-02-19 17:55:18 +0000 | [diff] [blame] | 177 | VideoCodec& operator=(const VideoCodec& c); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 178 | |
guoweis@webrtc.org | bc6961f | 2015-02-19 17:55:18 +0000 | [diff] [blame] | 179 | bool operator==(const VideoCodec& c) const; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 180 | |
| 181 | bool operator!=(const VideoCodec& c) const { |
| 182 | return !(*this == c); |
| 183 | } |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 184 | |
| 185 | static VideoCodec CreateRtxCodec(int rtx_payload_type, |
| 186 | int associated_payload_type); |
| 187 | |
| 188 | enum CodecType { |
| 189 | CODEC_VIDEO, |
| 190 | CODEC_RED, |
| 191 | CODEC_ULPFEC, |
| 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; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 200 | }; |
| 201 | |
| 202 | struct DataCodec : public Codec { |
guoweis@webrtc.org | bc6961f | 2015-02-19 17:55:18 +0000 | [diff] [blame] | 203 | DataCodec(int id, const std::string& name, int preference); |
| 204 | DataCodec(); |
| 205 | DataCodec(const DataCodec& c); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 206 | |
guoweis@webrtc.org | bc6961f | 2015-02-19 17:55:18 +0000 | [diff] [blame] | 207 | DataCodec& operator=(const DataCodec& c); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 208 | |
| 209 | std::string ToString() const; |
| 210 | }; |
| 211 | |
| 212 | struct VideoEncoderConfig { |
| 213 | static const int kDefaultMaxThreads = -1; |
| 214 | static const int kDefaultCpuProfile = -1; |
| 215 | |
| 216 | VideoEncoderConfig() |
| 217 | : max_codec(), |
| 218 | num_threads(kDefaultMaxThreads), |
| 219 | cpu_profile(kDefaultCpuProfile) { |
| 220 | } |
| 221 | |
| 222 | VideoEncoderConfig(const VideoCodec& c) |
| 223 | : max_codec(c), |
| 224 | num_threads(kDefaultMaxThreads), |
| 225 | cpu_profile(kDefaultCpuProfile) { |
| 226 | } |
| 227 | |
| 228 | VideoEncoderConfig(const VideoCodec& c, int t, int p) |
| 229 | : max_codec(c), |
| 230 | num_threads(t), |
| 231 | cpu_profile(p) { |
| 232 | } |
| 233 | |
| 234 | VideoEncoderConfig& operator=(const VideoEncoderConfig& config) { |
| 235 | max_codec = config.max_codec; |
| 236 | num_threads = config.num_threads; |
| 237 | cpu_profile = config.cpu_profile; |
| 238 | return *this; |
| 239 | } |
| 240 | |
| 241 | bool operator==(const VideoEncoderConfig& config) const { |
| 242 | return max_codec == config.max_codec && |
| 243 | num_threads == config.num_threads && |
| 244 | cpu_profile == config.cpu_profile; |
| 245 | } |
| 246 | |
| 247 | bool operator!=(const VideoEncoderConfig& config) const { |
| 248 | return !(*this == config); |
| 249 | } |
| 250 | |
| 251 | VideoCodec max_codec; |
| 252 | int num_threads; |
| 253 | int cpu_profile; |
| 254 | }; |
| 255 | |
changbin.shao@webrtc.org | 2d25b44 | 2015-03-16 04:14:34 +0000 | [diff] [blame] | 256 | // Get the codec setting associated with |payload_type|. If there |
| 257 | // is no codec associated with that payload type it returns false. |
| 258 | template <class Codec> |
| 259 | bool FindCodecById(const std::vector<Codec>& codecs, |
| 260 | int payload_type, |
| 261 | Codec* codec_out) { |
| 262 | for (const auto& codec : codecs) { |
| 263 | if (codec.id == payload_type) { |
| 264 | *codec_out = codec; |
| 265 | return true; |
| 266 | } |
| 267 | } |
| 268 | return false; |
| 269 | } |
| 270 | |
Shao Changbin | e62202f | 2015-04-21 20:24:50 +0800 | [diff] [blame] | 271 | bool CodecNamesEq(const std::string& name1, const std::string& name2); |
| 272 | bool HasNack(const VideoCodec& codec); |
| 273 | bool HasRemb(const VideoCodec& codec); |
| 274 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 275 | } // namespace cricket |
| 276 | |
| 277 | #endif // TALK_MEDIA_BASE_CODEC_H_ |