blob: 863e289e8bdcc5081bb29c18063d34df9d1f7db3 [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
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
38namespace cricket {
39
40typedef std::map<std::string, std::string> CodecParameterMap;
41
42class FeedbackParam {
43 public:
44 FeedbackParam(const std::string& id, const std::string& param)
45 : id_(id),
46 param_(param) {
47 }
wu@webrtc.org91053e72013-08-10 07:18:04 +000048 explicit FeedbackParam(const std::string& id)
49 : id_(id),
50 param_(kParamValueEmpty) {
51 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +000052 bool operator==(const FeedbackParam& other) const;
53
54 const std::string& id() const { return id_; }
55 const std::string& param() const { return param_; }
56
57 private:
58 std::string id_; // e.g. "nack", "ccm"
59 std::string param_; // e.g. "", "rpsi", "fir"
60};
61
62class FeedbackParams {
63 public:
64 bool operator==(const FeedbackParams& other) const;
65
66 bool Has(const FeedbackParam& param) const;
67 void Add(const FeedbackParam& param);
68
69 void Intersect(const FeedbackParams& from);
70
71 const std::vector<FeedbackParam>& params() const { return params_; }
72 private:
73 bool HasDuplicateEntries() const;
74
75 std::vector<FeedbackParam> params_;
76};
77
78struct Codec {
79 int id;
80 std::string name;
81 int clockrate;
82 int preference;
83 CodecParameterMap params;
84 FeedbackParams feedback_params;
85
86 // Creates a codec with the given parameters.
87 Codec(int id, const std::string& name, int clockrate, int preference)
88 : id(id),
89 name(name),
90 clockrate(clockrate),
91 preference(preference) {
92 }
93
94 // Creates an empty codec.
95 Codec() : id(0), clockrate(0), preference(0) {}
96
97 // Indicates if this codec is compatible with the specified codec.
98 bool Matches(const Codec& codec) const;
99
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;
103
104 void SetParam(const std::string& name, const std::string& value);
105 void SetParam(const std::string& name, int value);
106
buildbot@webrtc.orgfbd13282014-06-19 19:50:55 +0000107 // It is safe to input a non-existent parameter.
108 // Returns true if the parameter existed, false if it did not exist.
109 bool RemoveParam(const std::string& name);
110
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000111 bool HasFeedbackParam(const FeedbackParam& param) const;
112 void AddFeedbackParam(const FeedbackParam& param);
113
114 static bool Preferable(const Codec& first, const Codec& other) {
115 return first.preference > other.preference;
116 }
117
118 // Filter |this| feedbacks params such that only those shared by both |this|
119 // and |other| are kept.
120 void IntersectFeedbackParams(const Codec& other);
121
122 Codec& operator=(const Codec& c) {
123 this->id = c.id; // id is reserved in objective-c
124 name = c.name;
125 clockrate = c.clockrate;
126 preference = c.preference;
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +0000127 params = c.params;
128 feedback_params = c.feedback_params;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000129 return *this;
130 }
131
132 bool operator==(const Codec& c) const {
133 return this->id == c.id && // id is reserved in objective-c
134 name == c.name &&
135 clockrate == c.clockrate &&
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +0000136 preference == c.preference &&
137 params == c.params &&
138 feedback_params == c.feedback_params;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000139 }
140
141 bool operator!=(const Codec& c) const {
142 return !(*this == c);
143 }
144};
145
146struct AudioCodec : public Codec {
147 int bitrate;
148 int channels;
149
150 // Creates a codec with the given parameters.
151 AudioCodec(int pt, const std::string& nm, int cr, int br, int cs, int pr)
152 : Codec(pt, nm, cr, pr),
153 bitrate(br),
154 channels(cs) {
155 }
156
157 // Creates an empty codec.
158 AudioCodec() : Codec(), bitrate(0), channels(0) {}
159
160 // Indicates if this codec is compatible with the specified codec.
161 bool Matches(const AudioCodec& codec) const;
162
163 static bool Preferable(const AudioCodec& first, const AudioCodec& other) {
164 return first.preference > other.preference;
165 }
166
167 std::string ToString() const;
168
169 AudioCodec& operator=(const AudioCodec& c) {
170 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 return *this;
179 }
180
181 bool operator==(const AudioCodec& c) const {
182 return this->id == c.id && // id is reserved in objective-c
183 name == c.name &&
184 clockrate == c.clockrate &&
185 bitrate == c.bitrate &&
186 channels == c.channels &&
187 preference == c.preference &&
188 params == c.params &&
189 feedback_params == c.feedback_params;
190 }
191
192 bool operator!=(const AudioCodec& c) const {
193 return !(*this == c);
194 }
195};
196
197struct VideoCodec : public Codec {
198 int width;
199 int height;
200 int framerate;
201
202 // Creates a codec with the given parameters.
203 VideoCodec(int pt, const std::string& nm, int w, int h, int fr, int pr)
204 : Codec(pt, nm, kVideoCodecClockrate, pr),
205 width(w),
206 height(h),
207 framerate(fr) {
208 }
209
210 // Creates an empty codec.
211 VideoCodec()
212 : Codec(),
213 width(0),
214 height(0),
215 framerate(0) {
216 clockrate = kVideoCodecClockrate;
217 }
218
219 static bool Preferable(const VideoCodec& first, const VideoCodec& other) {
220 return first.preference > other.preference;
221 }
222
223 std::string ToString() const;
224
225 VideoCodec& operator=(const VideoCodec& c) {
226 this->id = c.id; // id is reserved in objective-c
227 name = c.name;
228 clockrate = c.clockrate;
229 width = c.width;
230 height = c.height;
231 framerate = c.framerate;
232 preference = c.preference;
233 params = c.params;
234 feedback_params = c.feedback_params;
235 return *this;
236 }
237
238 bool operator==(const VideoCodec& c) const {
239 return this->id == c.id && // id is reserved in objective-c
240 name == c.name &&
241 clockrate == c.clockrate &&
242 width == c.width &&
243 height == c.height &&
244 framerate == c.framerate &&
245 preference == c.preference &&
246 params == c.params &&
247 feedback_params == c.feedback_params;
248 }
249
250 bool operator!=(const VideoCodec& c) const {
251 return !(*this == c);
252 }
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000253
254 static VideoCodec CreateRtxCodec(int rtx_payload_type,
255 int associated_payload_type);
256
257 enum CodecType {
258 CODEC_VIDEO,
259 CODEC_RED,
260 CODEC_ULPFEC,
261 CODEC_RTX,
262 };
263
264 CodecType GetCodecType() const;
265 // Validates a VideoCodec's payload type, dimensions and bitrates etc. If they
266 // don't make sense (such as max < min bitrate), and error is logged and
267 // ValidateCodecFormat returns false.
268 bool ValidateCodecFormat() const;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000269};
270
271struct DataCodec : public Codec {
272 DataCodec(int id, const std::string& name, int preference)
273 : Codec(id, name, kDataCodecClockrate, preference) {
274 }
275
276 DataCodec() : Codec() {
277 clockrate = kDataCodecClockrate;
278 }
279
280 std::string ToString() const;
281};
282
283struct VideoEncoderConfig {
284 static const int kDefaultMaxThreads = -1;
285 static const int kDefaultCpuProfile = -1;
286
287 VideoEncoderConfig()
288 : max_codec(),
289 num_threads(kDefaultMaxThreads),
290 cpu_profile(kDefaultCpuProfile) {
291 }
292
293 VideoEncoderConfig(const VideoCodec& c)
294 : max_codec(c),
295 num_threads(kDefaultMaxThreads),
296 cpu_profile(kDefaultCpuProfile) {
297 }
298
299 VideoEncoderConfig(const VideoCodec& c, int t, int p)
300 : max_codec(c),
301 num_threads(t),
302 cpu_profile(p) {
303 }
304
305 VideoEncoderConfig& operator=(const VideoEncoderConfig& config) {
306 max_codec = config.max_codec;
307 num_threads = config.num_threads;
308 cpu_profile = config.cpu_profile;
309 return *this;
310 }
311
312 bool operator==(const VideoEncoderConfig& config) const {
313 return max_codec == config.max_codec &&
314 num_threads == config.num_threads &&
315 cpu_profile == config.cpu_profile;
316 }
317
318 bool operator!=(const VideoEncoderConfig& config) const {
319 return !(*this == config);
320 }
321
322 VideoCodec max_codec;
323 int num_threads;
324 int cpu_profile;
325};
326
327} // namespace cricket
328
329#endif // TALK_MEDIA_BASE_CODEC_H_