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 | |
| 42 | class FeedbackParam { |
| 43 | public: |
| 44 | FeedbackParam(const std::string& id, const std::string& param) |
| 45 | : id_(id), |
| 46 | param_(param) { |
| 47 | } |
| 48 | bool operator==(const FeedbackParam& other) const; |
| 49 | |
| 50 | const std::string& id() const { return id_; } |
| 51 | const std::string& param() const { return param_; } |
| 52 | |
| 53 | private: |
| 54 | std::string id_; // e.g. "nack", "ccm" |
| 55 | std::string param_; // e.g. "", "rpsi", "fir" |
| 56 | }; |
| 57 | |
| 58 | class FeedbackParams { |
| 59 | public: |
| 60 | bool operator==(const FeedbackParams& other) const; |
| 61 | |
| 62 | bool Has(const FeedbackParam& param) const; |
| 63 | void Add(const FeedbackParam& param); |
| 64 | |
| 65 | void Intersect(const FeedbackParams& from); |
| 66 | |
| 67 | const std::vector<FeedbackParam>& params() const { return params_; } |
| 68 | private: |
| 69 | bool HasDuplicateEntries() const; |
| 70 | |
| 71 | std::vector<FeedbackParam> params_; |
| 72 | }; |
| 73 | |
| 74 | struct Codec { |
| 75 | int id; |
| 76 | std::string name; |
| 77 | int clockrate; |
| 78 | int preference; |
| 79 | CodecParameterMap params; |
| 80 | FeedbackParams feedback_params; |
| 81 | |
| 82 | // Creates a codec with the given parameters. |
| 83 | Codec(int id, const std::string& name, int clockrate, int preference) |
| 84 | : id(id), |
| 85 | name(name), |
| 86 | clockrate(clockrate), |
| 87 | preference(preference) { |
| 88 | } |
| 89 | |
| 90 | // Creates an empty codec. |
| 91 | Codec() : id(0), clockrate(0), preference(0) {} |
| 92 | |
| 93 | // Indicates if this codec is compatible with the specified codec. |
| 94 | bool Matches(const Codec& codec) const; |
| 95 | |
| 96 | // Find the parameter for |name| and write the value to |out|. |
| 97 | bool GetParam(const std::string& name, std::string* out) const; |
| 98 | bool GetParam(const std::string& name, int* out) const; |
| 99 | |
| 100 | void SetParam(const std::string& name, const std::string& value); |
| 101 | void SetParam(const std::string& name, int value); |
| 102 | |
| 103 | bool HasFeedbackParam(const FeedbackParam& param) const; |
| 104 | void AddFeedbackParam(const FeedbackParam& param); |
| 105 | |
| 106 | static bool Preferable(const Codec& first, const Codec& other) { |
| 107 | return first.preference > other.preference; |
| 108 | } |
| 109 | |
| 110 | // Filter |this| feedbacks params such that only those shared by both |this| |
| 111 | // and |other| are kept. |
| 112 | void IntersectFeedbackParams(const Codec& other); |
| 113 | |
| 114 | Codec& operator=(const Codec& c) { |
| 115 | this->id = c.id; // id is reserved in objective-c |
| 116 | name = c.name; |
| 117 | clockrate = c.clockrate; |
| 118 | preference = c.preference; |
| 119 | return *this; |
| 120 | } |
| 121 | |
| 122 | bool operator==(const Codec& c) const { |
| 123 | return this->id == c.id && // id is reserved in objective-c |
| 124 | name == c.name && |
| 125 | clockrate == c.clockrate && |
| 126 | preference == c.preference; |
| 127 | } |
| 128 | |
| 129 | bool operator!=(const Codec& c) const { |
| 130 | return !(*this == c); |
| 131 | } |
| 132 | }; |
| 133 | |
| 134 | struct AudioCodec : public Codec { |
| 135 | int bitrate; |
| 136 | int channels; |
| 137 | |
| 138 | // Creates a codec with the given parameters. |
| 139 | AudioCodec(int pt, const std::string& nm, int cr, int br, int cs, int pr) |
| 140 | : Codec(pt, nm, cr, pr), |
| 141 | bitrate(br), |
| 142 | channels(cs) { |
| 143 | } |
| 144 | |
| 145 | // Creates an empty codec. |
| 146 | AudioCodec() : Codec(), bitrate(0), channels(0) {} |
| 147 | |
| 148 | // Indicates if this codec is compatible with the specified codec. |
| 149 | bool Matches(const AudioCodec& codec) const; |
| 150 | |
| 151 | static bool Preferable(const AudioCodec& first, const AudioCodec& other) { |
| 152 | return first.preference > other.preference; |
| 153 | } |
| 154 | |
| 155 | std::string ToString() const; |
| 156 | |
| 157 | AudioCodec& operator=(const AudioCodec& c) { |
| 158 | this->id = c.id; // id is reserved in objective-c |
| 159 | name = c.name; |
| 160 | clockrate = c.clockrate; |
| 161 | bitrate = c.bitrate; |
| 162 | channels = c.channels; |
| 163 | preference = c.preference; |
| 164 | params = c.params; |
| 165 | feedback_params = c.feedback_params; |
| 166 | return *this; |
| 167 | } |
| 168 | |
| 169 | bool operator==(const AudioCodec& c) const { |
| 170 | return this->id == c.id && // id is reserved in objective-c |
| 171 | name == c.name && |
| 172 | clockrate == c.clockrate && |
| 173 | bitrate == c.bitrate && |
| 174 | channels == c.channels && |
| 175 | preference == c.preference && |
| 176 | params == c.params && |
| 177 | feedback_params == c.feedback_params; |
| 178 | } |
| 179 | |
| 180 | bool operator!=(const AudioCodec& c) const { |
| 181 | return !(*this == c); |
| 182 | } |
| 183 | }; |
| 184 | |
| 185 | struct VideoCodec : public Codec { |
| 186 | int width; |
| 187 | int height; |
| 188 | int framerate; |
| 189 | |
| 190 | // Creates a codec with the given parameters. |
| 191 | VideoCodec(int pt, const std::string& nm, int w, int h, int fr, int pr) |
| 192 | : Codec(pt, nm, kVideoCodecClockrate, pr), |
| 193 | width(w), |
| 194 | height(h), |
| 195 | framerate(fr) { |
| 196 | } |
| 197 | |
| 198 | // Creates an empty codec. |
| 199 | VideoCodec() |
| 200 | : Codec(), |
| 201 | width(0), |
| 202 | height(0), |
| 203 | framerate(0) { |
| 204 | clockrate = kVideoCodecClockrate; |
| 205 | } |
| 206 | |
| 207 | static bool Preferable(const VideoCodec& first, const VideoCodec& other) { |
| 208 | return first.preference > other.preference; |
| 209 | } |
| 210 | |
| 211 | std::string ToString() const; |
| 212 | |
| 213 | VideoCodec& operator=(const VideoCodec& c) { |
| 214 | this->id = c.id; // id is reserved in objective-c |
| 215 | name = c.name; |
| 216 | clockrate = c.clockrate; |
| 217 | width = c.width; |
| 218 | height = c.height; |
| 219 | framerate = c.framerate; |
| 220 | preference = c.preference; |
| 221 | params = c.params; |
| 222 | feedback_params = c.feedback_params; |
| 223 | return *this; |
| 224 | } |
| 225 | |
| 226 | bool operator==(const VideoCodec& c) const { |
| 227 | return this->id == c.id && // id is reserved in objective-c |
| 228 | name == c.name && |
| 229 | clockrate == c.clockrate && |
| 230 | width == c.width && |
| 231 | height == c.height && |
| 232 | framerate == c.framerate && |
| 233 | preference == c.preference && |
| 234 | params == c.params && |
| 235 | feedback_params == c.feedback_params; |
| 236 | } |
| 237 | |
| 238 | bool operator!=(const VideoCodec& c) const { |
| 239 | return !(*this == c); |
| 240 | } |
| 241 | }; |
| 242 | |
| 243 | struct DataCodec : public Codec { |
| 244 | DataCodec(int id, const std::string& name, int preference) |
| 245 | : Codec(id, name, kDataCodecClockrate, preference) { |
| 246 | } |
| 247 | |
| 248 | DataCodec() : Codec() { |
| 249 | clockrate = kDataCodecClockrate; |
| 250 | } |
| 251 | |
| 252 | std::string ToString() const; |
| 253 | }; |
| 254 | |
| 255 | struct VideoEncoderConfig { |
| 256 | static const int kDefaultMaxThreads = -1; |
| 257 | static const int kDefaultCpuProfile = -1; |
| 258 | |
| 259 | VideoEncoderConfig() |
| 260 | : max_codec(), |
| 261 | num_threads(kDefaultMaxThreads), |
| 262 | cpu_profile(kDefaultCpuProfile) { |
| 263 | } |
| 264 | |
| 265 | VideoEncoderConfig(const VideoCodec& c) |
| 266 | : max_codec(c), |
| 267 | num_threads(kDefaultMaxThreads), |
| 268 | cpu_profile(kDefaultCpuProfile) { |
| 269 | } |
| 270 | |
| 271 | VideoEncoderConfig(const VideoCodec& c, int t, int p) |
| 272 | : max_codec(c), |
| 273 | num_threads(t), |
| 274 | cpu_profile(p) { |
| 275 | } |
| 276 | |
| 277 | VideoEncoderConfig& operator=(const VideoEncoderConfig& config) { |
| 278 | max_codec = config.max_codec; |
| 279 | num_threads = config.num_threads; |
| 280 | cpu_profile = config.cpu_profile; |
| 281 | return *this; |
| 282 | } |
| 283 | |
| 284 | bool operator==(const VideoEncoderConfig& config) const { |
| 285 | return max_codec == config.max_codec && |
| 286 | num_threads == config.num_threads && |
| 287 | cpu_profile == config.cpu_profile; |
| 288 | } |
| 289 | |
| 290 | bool operator!=(const VideoEncoderConfig& config) const { |
| 291 | return !(*this == config); |
| 292 | } |
| 293 | |
| 294 | VideoCodec max_codec; |
| 295 | int num_threads; |
| 296 | int cpu_profile; |
| 297 | }; |
| 298 | |
| 299 | } // namespace cricket |
| 300 | |
| 301 | #endif // TALK_MEDIA_BASE_CODEC_H_ |