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;
44 FeedbackParam(const std::string& id, const std::string& param)
48 explicit FeedbackParam(const std::string& id)
50 param_(kParamValueEmpty) {
52 bool operator==(const FeedbackParam& other) const;
54 const std::string& id() const { return id_; }
55 const std::string& param() const { return param_; }
58 std::string id_; // e.g. "nack", "ccm"
59 std::string param_; // e.g. "", "rpsi", "fir"
62 class FeedbackParams {
64 bool operator==(const FeedbackParams& other) const;
66 bool Has(const FeedbackParam& param) const;
67 void Add(const FeedbackParam& param);
69 void Intersect(const FeedbackParams& from);
71 const std::vector<FeedbackParam>& params() const { return params_; }
73 bool HasDuplicateEntries() const;
75 std::vector<FeedbackParam> params_;
83 CodecParameterMap params;
84 FeedbackParams feedback_params;
86 // Creates a codec with the given parameters.
87 Codec(int id, const std::string& name, int clockrate, int preference)
91 preference(preference) {
94 // Creates an empty codec.
95 Codec() : id(0), clockrate(0), preference(0) {}
97 // Indicates if this codec is compatible with the specified codec.
98 bool Matches(const Codec& codec) const;
100 // Find the parameter for |name| and write the value to |out|.
101 bool GetParam(const std::string& name, std::string* out) const;
102 bool GetParam(const std::string& name, int* out) const;
104 void SetParam(const std::string& name, const std::string& value);
105 void SetParam(const std::string& name, int value);
107 bool HasFeedbackParam(const FeedbackParam& param) const;
108 void AddFeedbackParam(const FeedbackParam& param);
110 static bool Preferable(const Codec& first, const Codec& other) {
111 return first.preference > other.preference;
114 // Filter |this| feedbacks params such that only those shared by both |this|
115 // and |other| are kept.
116 void IntersectFeedbackParams(const Codec& other);
118 Codec& operator=(const Codec& c) {
119 this->id = c.id; // id is reserved in objective-c
121 clockrate = c.clockrate;
122 preference = c.preference;
124 feedback_params = c.feedback_params;
128 bool operator==(const Codec& c) const {
129 return this->id == c.id && // id is reserved in objective-c
131 clockrate == c.clockrate &&
132 preference == c.preference &&
133 params == c.params &&
134 feedback_params == c.feedback_params;
137 bool operator!=(const Codec& c) const {
138 return !(*this == c);
142 struct AudioCodec : public Codec {
146 // Creates a codec with the given parameters.
147 AudioCodec(int pt, const std::string& nm, int cr, int br, int cs, int pr)
148 : Codec(pt, nm, cr, pr),
153 // Creates an empty codec.
154 AudioCodec() : Codec(), bitrate(0), channels(0) {}
156 // Indicates if this codec is compatible with the specified codec.
157 bool Matches(const AudioCodec& codec) const;
159 static bool Preferable(const AudioCodec& first, const AudioCodec& other) {
160 return first.preference > other.preference;
163 std::string ToString() const;
165 AudioCodec& operator=(const AudioCodec& c) {
166 this->id = c.id; // id is reserved in objective-c
168 clockrate = c.clockrate;
170 channels = c.channels;
171 preference = c.preference;
173 feedback_params = c.feedback_params;
177 bool operator==(const AudioCodec& c) const {
178 return this->id == c.id && // id is reserved in objective-c
180 clockrate == c.clockrate &&
181 bitrate == c.bitrate &&
182 channels == c.channels &&
183 preference == c.preference &&
184 params == c.params &&
185 feedback_params == c.feedback_params;
188 bool operator!=(const AudioCodec& c) const {
189 return !(*this == c);
193 struct VideoCodec : public Codec {
198 // Creates a codec with the given parameters.
199 VideoCodec(int pt, const std::string& nm, int w, int h, int fr, int pr)
200 : Codec(pt, nm, kVideoCodecClockrate, pr),
206 // Creates an empty codec.
212 clockrate = kVideoCodecClockrate;
215 static bool Preferable(const VideoCodec& first, const VideoCodec& other) {
216 return first.preference > other.preference;
219 std::string ToString() const;
221 VideoCodec& operator=(const VideoCodec& c) {
222 this->id = c.id; // id is reserved in objective-c
224 clockrate = c.clockrate;
227 framerate = c.framerate;
228 preference = c.preference;
230 feedback_params = c.feedback_params;
234 bool operator==(const VideoCodec& c) const {
235 return this->id == c.id && // id is reserved in objective-c
237 clockrate == c.clockrate &&
239 height == c.height &&
240 framerate == c.framerate &&
241 preference == c.preference &&
242 params == c.params &&
243 feedback_params == c.feedback_params;
246 bool operator!=(const VideoCodec& c) const {
247 return !(*this == c);
251 struct DataCodec : public Codec {
252 DataCodec(int id, const std::string& name, int preference)
253 : Codec(id, name, kDataCodecClockrate, preference) {
256 DataCodec() : Codec() {
257 clockrate = kDataCodecClockrate;
260 std::string ToString() const;
263 struct VideoEncoderConfig {
264 static const int kDefaultMaxThreads = -1;
265 static const int kDefaultCpuProfile = -1;
269 num_threads(kDefaultMaxThreads),
270 cpu_profile(kDefaultCpuProfile) {
273 VideoEncoderConfig(const VideoCodec& c)
275 num_threads(kDefaultMaxThreads),
276 cpu_profile(kDefaultCpuProfile) {
279 VideoEncoderConfig(const VideoCodec& c, int t, int p)
285 VideoEncoderConfig& operator=(const VideoEncoderConfig& config) {
286 max_codec = config.max_codec;
287 num_threads = config.num_threads;
288 cpu_profile = config.cpu_profile;
292 bool operator==(const VideoEncoderConfig& config) const {
293 return max_codec == config.max_codec &&
294 num_threads == config.num_threads &&
295 cpu_profile == config.cpu_profile;
298 bool operator!=(const VideoEncoderConfig& config) const {
299 return !(*this == config);
302 VideoCodec max_codec;
307 } // namespace cricket
309 #endif // TALK_MEDIA_BASE_CODEC_H_