3 * Copyright 2004 Google Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
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.
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.
28 #ifndef TALK_MEDIA_BASE_CODEC_H_
29 #define TALK_MEDIA_BASE_CODEC_H_
36 #include "talk/media/base/constants.h"
40 typedef std::map<std::string, std::string> CodecParameterMap;
42 extern const int kMaxPayloadId;
46 FeedbackParam(const std::string& id, const std::string& param)
50 explicit FeedbackParam(const std::string& id)
52 param_(kParamValueEmpty) {
54 bool operator==(const FeedbackParam& other) const;
56 const std::string& id() const { return id_; }
57 const std::string& param() const { return param_; }
60 std::string id_; // e.g. "nack", "ccm"
61 std::string param_; // e.g. "", "rpsi", "fir"
64 class FeedbackParams {
66 bool operator==(const FeedbackParams& other) const;
68 bool Has(const FeedbackParam& param) const;
69 void Add(const FeedbackParam& param);
71 void Intersect(const FeedbackParams& from);
73 const std::vector<FeedbackParam>& params() const { return params_; }
75 bool HasDuplicateEntries() const;
77 std::vector<FeedbackParam> params_;
85 CodecParameterMap params;
86 FeedbackParams feedback_params;
88 // Creates a codec with the given parameters.
89 Codec(int id, const std::string& name, int clockrate, int preference)
93 preference(preference) {
96 // Creates an empty codec.
97 Codec() : id(0), clockrate(0), preference(0) {}
99 // Indicates if this codec is compatible with the specified codec.
100 bool Matches(const Codec& codec) const;
102 // Find the parameter for |name| and write the value to |out|.
103 bool GetParam(const std::string& name, std::string* out) const;
104 bool GetParam(const std::string& name, int* out) const;
106 void SetParam(const std::string& name, const std::string& value);
107 void SetParam(const std::string& name, int value);
109 // It is safe to input a non-existent parameter.
110 // Returns true if the parameter existed, false if it did not exist.
111 bool RemoveParam(const std::string& name);
113 bool HasFeedbackParam(const FeedbackParam& param) const;
114 void AddFeedbackParam(const FeedbackParam& param);
116 static bool Preferable(const Codec& first, const Codec& other) {
117 return first.preference > other.preference;
120 // Filter |this| feedbacks params such that only those shared by both |this|
121 // and |other| are kept.
122 void IntersectFeedbackParams(const Codec& other);
124 Codec& operator=(const Codec& c) {
125 this->id = c.id; // id is reserved in objective-c
127 clockrate = c.clockrate;
128 preference = c.preference;
130 feedback_params = c.feedback_params;
134 bool operator==(const Codec& c) const {
135 return this->id == c.id && // id is reserved in objective-c
137 clockrate == c.clockrate &&
138 preference == c.preference &&
139 params == c.params &&
140 feedback_params == c.feedback_params;
143 bool operator!=(const Codec& c) const {
144 return !(*this == c);
148 struct AudioCodec : public Codec {
152 // Creates a codec with the given parameters.
153 AudioCodec(int pt, const std::string& nm, int cr, int br, int cs, int pr)
154 : Codec(pt, nm, cr, pr),
159 // Creates an empty codec.
160 AudioCodec() : Codec(), bitrate(0), channels(0) {}
162 // Indicates if this codec is compatible with the specified codec.
163 bool Matches(const AudioCodec& codec) const;
165 static bool Preferable(const AudioCodec& first, const AudioCodec& other) {
166 return first.preference > other.preference;
169 std::string ToString() const;
171 AudioCodec& operator=(const AudioCodec& c) {
172 this->id = c.id; // id is reserved in objective-c
174 clockrate = c.clockrate;
176 channels = c.channels;
177 preference = c.preference;
179 feedback_params = c.feedback_params;
183 bool operator==(const AudioCodec& c) const {
184 return this->id == c.id && // id is reserved in objective-c
186 clockrate == c.clockrate &&
187 bitrate == c.bitrate &&
188 channels == c.channels &&
189 preference == c.preference &&
190 params == c.params &&
191 feedback_params == c.feedback_params;
194 bool operator!=(const AudioCodec& c) const {
195 return !(*this == c);
199 struct VideoCodec : public Codec {
204 // Creates a codec with the given parameters.
205 VideoCodec(int pt, const std::string& nm, int w, int h, int fr, int pr)
206 : Codec(pt, nm, kVideoCodecClockrate, pr),
212 // Creates an empty codec.
218 clockrate = kVideoCodecClockrate;
221 static bool Preferable(const VideoCodec& first, const VideoCodec& other) {
222 return first.preference > other.preference;
225 std::string ToString() const;
227 VideoCodec& operator=(const VideoCodec& c) {
228 this->id = c.id; // id is reserved in objective-c
230 clockrate = c.clockrate;
233 framerate = c.framerate;
234 preference = c.preference;
236 feedback_params = c.feedback_params;
240 bool operator==(const VideoCodec& c) const {
241 return this->id == c.id && // id is reserved in objective-c
243 clockrate == c.clockrate &&
245 height == c.height &&
246 framerate == c.framerate &&
247 preference == c.preference &&
248 params == c.params &&
249 feedback_params == c.feedback_params;
252 bool operator!=(const VideoCodec& c) const {
253 return !(*this == c);
256 static VideoCodec CreateRtxCodec(int rtx_payload_type,
257 int associated_payload_type);
266 CodecType GetCodecType() const;
267 // Validates a VideoCodec's payload type, dimensions and bitrates etc. If they
268 // don't make sense (such as max < min bitrate), and error is logged and
269 // ValidateCodecFormat returns false.
270 bool ValidateCodecFormat() const;
273 struct DataCodec : public Codec {
274 DataCodec(int id, const std::string& name, int preference)
275 : Codec(id, name, kDataCodecClockrate, preference) {
278 DataCodec() : Codec() {
279 clockrate = kDataCodecClockrate;
282 std::string ToString() const;
285 struct VideoEncoderConfig {
286 static const int kDefaultMaxThreads = -1;
287 static const int kDefaultCpuProfile = -1;
291 num_threads(kDefaultMaxThreads),
292 cpu_profile(kDefaultCpuProfile) {
295 VideoEncoderConfig(const VideoCodec& c)
297 num_threads(kDefaultMaxThreads),
298 cpu_profile(kDefaultCpuProfile) {
301 VideoEncoderConfig(const VideoCodec& c, int t, int p)
307 VideoEncoderConfig& operator=(const VideoEncoderConfig& config) {
308 max_codec = config.max_codec;
309 num_threads = config.num_threads;
310 cpu_profile = config.cpu_profile;
314 bool operator==(const VideoEncoderConfig& config) const {
315 return max_codec == config.max_codec &&
316 num_threads == config.num_threads &&
317 cpu_profile == config.cpu_profile;
320 bool operator!=(const VideoEncoderConfig& config) const {
321 return !(*this == config);
324 VideoCodec max_codec;
329 } // namespace cricket
331 #endif // TALK_MEDIA_BASE_CODEC_H_