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" |
kjellander | f475277 | 2016-03-02 05:42:30 -0800 | [diff] [blame] | 20 | #include "webrtc/media/base/mediaconstants.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 21 | |
| 22 | namespace cricket { |
| 23 | |
| 24 | typedef std::map<std::string, std::string> CodecParameterMap; |
| 25 | |
wu@webrtc.org | ff1b1bf | 2014-06-20 20:57:42 +0000 | [diff] [blame] | 26 | extern const int kMaxPayloadId; |
| 27 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 28 | class FeedbackParam { |
| 29 | public: |
| 30 | FeedbackParam(const std::string& id, const std::string& param) |
| 31 | : id_(id), |
| 32 | param_(param) { |
| 33 | } |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 34 | explicit FeedbackParam(const std::string& id) |
| 35 | : id_(id), |
| 36 | param_(kParamValueEmpty) { |
| 37 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 38 | bool operator==(const FeedbackParam& other) const; |
| 39 | |
| 40 | const std::string& id() const { return id_; } |
| 41 | const std::string& param() const { return param_; } |
| 42 | |
| 43 | private: |
| 44 | std::string id_; // e.g. "nack", "ccm" |
| 45 | std::string param_; // e.g. "", "rpsi", "fir" |
| 46 | }; |
| 47 | |
| 48 | class FeedbackParams { |
| 49 | public: |
| 50 | bool operator==(const FeedbackParams& other) const; |
| 51 | |
| 52 | bool Has(const FeedbackParam& param) const; |
| 53 | void Add(const FeedbackParam& param); |
| 54 | |
| 55 | void Intersect(const FeedbackParams& from); |
| 56 | |
| 57 | const std::vector<FeedbackParam>& params() const { return params_; } |
| 58 | private: |
| 59 | bool HasDuplicateEntries() const; |
| 60 | |
| 61 | std::vector<FeedbackParam> params_; |
| 62 | }; |
| 63 | |
| 64 | struct Codec { |
| 65 | int id; |
| 66 | std::string name; |
| 67 | int clockrate; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 68 | CodecParameterMap params; |
| 69 | FeedbackParams feedback_params; |
| 70 | |
| 71 | // Creates a codec with the given parameters. |
deadbeef | 67cf2c1 | 2016-04-13 10:07:16 -0700 | [diff] [blame] | 72 | Codec(int id, const std::string& name, int clockrate); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 73 | // Creates an empty codec. |
guoweis@webrtc.org | bc6961f | 2015-02-19 17:55:18 +0000 | [diff] [blame] | 74 | Codec(); |
| 75 | Codec(const Codec& c); |
Henrik Kjellander | 3fe372d | 2016-05-12 08:10:52 +0200 | [diff] [blame^] | 76 | virtual ~Codec(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 77 | |
| 78 | // Indicates if this codec is compatible with the specified codec. |
| 79 | bool Matches(const Codec& codec) const; |
| 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 +0000 | [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 +0000 | [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 +0000 | [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 16:23:10 -0700 | [diff] [blame] | 99 | virtual webrtc::RtpCodecParameters ToCodecParameters() const; |
| 100 | |
guoweis@webrtc.org | bc6961f | 2015-02-19 17:55:18 +0000 | [diff] [blame] | 101 | Codec& operator=(const Codec& c); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 102 | |
guoweis@webrtc.org | bc6961f | 2015-02-19 17:55:18 +0000 | [diff] [blame] | 103 | bool operator==(const Codec& c) const; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 104 | |
| 105 | bool operator!=(const Codec& c) const { |
| 106 | return !(*this == c); |
| 107 | } |
| 108 | }; |
| 109 | |
| 110 | struct AudioCodec : public Codec { |
| 111 | int bitrate; |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 112 | size_t channels; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 113 | |
| 114 | // Creates a codec with the given parameters. |
pkasting | 25702cb | 2016-01-08 13:50:27 -0800 | [diff] [blame] | 115 | AudioCodec(int id, |
| 116 | const std::string& name, |
| 117 | int clockrate, |
| 118 | int bitrate, |
deadbeef | 67cf2c1 | 2016-04-13 10:07:16 -0700 | [diff] [blame] | 119 | size_t channels); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 120 | // Creates an empty codec. |
guoweis@webrtc.org | bc6961f | 2015-02-19 17:55:18 +0000 | [diff] [blame] | 121 | AudioCodec(); |
| 122 | AudioCodec(const AudioCodec& c); |
Henrik Kjellander | 3fe372d | 2016-05-12 08:10:52 +0200 | [diff] [blame^] | 123 | virtual ~AudioCodec() = default; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 124 | |
| 125 | // Indicates if this codec is compatible with the specified codec. |
| 126 | bool Matches(const AudioCodec& codec) const; |
| 127 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 128 | std::string ToString() const; |
| 129 | |
Taylor Brandstetter | 0cd086b | 2016-04-20 16:23:10 -0700 | [diff] [blame] | 130 | webrtc::RtpCodecParameters ToCodecParameters() const override; |
| 131 | |
guoweis@webrtc.org | bc6961f | 2015-02-19 17:55:18 +0000 | [diff] [blame] | 132 | AudioCodec& operator=(const AudioCodec& c); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 133 | |
guoweis@webrtc.org | bc6961f | 2015-02-19 17:55:18 +0000 | [diff] [blame] | 134 | bool operator==(const AudioCodec& c) const; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 135 | |
| 136 | bool operator!=(const AudioCodec& c) const { |
| 137 | return !(*this == c); |
| 138 | } |
| 139 | }; |
| 140 | |
| 141 | struct VideoCodec : public Codec { |
| 142 | int width; |
| 143 | int height; |
| 144 | int framerate; |
| 145 | |
| 146 | // Creates a codec with the given parameters. |
pkasting | 25702cb | 2016-01-08 13:50:27 -0800 | [diff] [blame] | 147 | VideoCodec(int id, |
| 148 | const std::string& name, |
| 149 | int width, |
| 150 | int height, |
deadbeef | 67cf2c1 | 2016-04-13 10:07:16 -0700 | [diff] [blame] | 151 | int framerate); |
pkasting | 25702cb | 2016-01-08 13:50:27 -0800 | [diff] [blame] | 152 | VideoCodec(int id, const std::string& name); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 153 | // Creates an empty codec. |
guoweis@webrtc.org | bc6961f | 2015-02-19 17:55:18 +0000 | [diff] [blame] | 154 | VideoCodec(); |
| 155 | VideoCodec(const VideoCodec& c); |
Henrik Kjellander | 3fe372d | 2016-05-12 08:10:52 +0200 | [diff] [blame^] | 156 | virtual ~VideoCodec() = default; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 157 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 158 | std::string ToString() const; |
| 159 | |
guoweis@webrtc.org | bc6961f | 2015-02-19 17:55:18 +0000 | [diff] [blame] | 160 | VideoCodec& operator=(const 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, |
| 175 | CODEC_RTX, |
| 176 | }; |
| 177 | |
| 178 | CodecType GetCodecType() const; |
| 179 | // Validates a VideoCodec's payload type, dimensions and bitrates etc. If they |
| 180 | // don't make sense (such as max < min bitrate), and error is logged and |
| 181 | // ValidateCodecFormat returns false. |
| 182 | bool ValidateCodecFormat() const; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 183 | }; |
| 184 | |
| 185 | struct DataCodec : public Codec { |
deadbeef | 67cf2c1 | 2016-04-13 10:07:16 -0700 | [diff] [blame] | 186 | DataCodec(int id, const std::string& name); |
guoweis@webrtc.org | bc6961f | 2015-02-19 17:55:18 +0000 | [diff] [blame] | 187 | DataCodec(); |
| 188 | DataCodec(const DataCodec& c); |
Henrik Kjellander | 3fe372d | 2016-05-12 08:10:52 +0200 | [diff] [blame^] | 189 | virtual ~DataCodec() = default; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 190 | |
guoweis@webrtc.org | bc6961f | 2015-02-19 17:55:18 +0000 | [diff] [blame] | 191 | DataCodec& operator=(const DataCodec& c); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 192 | |
| 193 | std::string ToString() const; |
| 194 | }; |
| 195 | |
changbin.shao@webrtc.org | 2d25b44 | 2015-03-16 04:14:34 +0000 | [diff] [blame] | 196 | // Get the codec setting associated with |payload_type|. If there |
| 197 | // is no codec associated with that payload type it returns false. |
| 198 | template <class Codec> |
| 199 | bool FindCodecById(const std::vector<Codec>& codecs, |
| 200 | int payload_type, |
| 201 | Codec* codec_out) { |
| 202 | for (const auto& codec : codecs) { |
| 203 | if (codec.id == payload_type) { |
| 204 | *codec_out = codec; |
| 205 | return true; |
| 206 | } |
| 207 | } |
| 208 | return false; |
| 209 | } |
| 210 | |
Shao Changbin | e62202f | 2015-04-21 20:24:50 +0800 | [diff] [blame] | 211 | bool CodecNamesEq(const std::string& name1, const std::string& name2); |
stefan | ba4c0e4 | 2016-02-04 04:12:24 -0800 | [diff] [blame] | 212 | bool HasNack(const Codec& codec); |
| 213 | bool HasRemb(const Codec& codec); |
| 214 | bool HasTransportCc(const Codec& codec); |
Shao Changbin | e62202f | 2015-04-21 20:24:50 +0800 | [diff] [blame] | 215 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 216 | } // namespace cricket |
| 217 | |
kjellander | a96e2d7 | 2016-02-04 23:52:28 -0800 | [diff] [blame] | 218 | #endif // WEBRTC_MEDIA_BASE_CODEC_H_ |