codec.h revision 91053e7c5a743f4a92f5079844b0747c927f3bbd
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
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  }
48  explicit FeedbackParam(const std::string& id)
49      : id_(id),
50        param_(kParamValueEmpty) {
51  }
52  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
107  bool HasFeedbackParam(const FeedbackParam& param) const;
108  void AddFeedbackParam(const FeedbackParam& param);
109
110  static bool Preferable(const Codec& first, const Codec& other) {
111    return first.preference > other.preference;
112  }
113
114  // Filter |this| feedbacks params such that only those shared by both |this|
115  // and |other| are kept.
116  void IntersectFeedbackParams(const Codec& other);
117
118  Codec& operator=(const Codec& c) {
119    this->id = c.id;  // id is reserved in objective-c
120    name = c.name;
121    clockrate = c.clockrate;
122    preference = c.preference;
123    return *this;
124  }
125
126  bool operator==(const Codec& c) const {
127    return this->id == c.id &&  // id is reserved in objective-c
128        name == c.name &&
129        clockrate == c.clockrate &&
130        preference == c.preference;
131  }
132
133  bool operator!=(const Codec& c) const {
134    return !(*this == c);
135  }
136};
137
138struct AudioCodec : public Codec {
139  int bitrate;
140  int channels;
141
142  // Creates a codec with the given parameters.
143  AudioCodec(int pt, const std::string& nm, int cr, int br, int cs, int pr)
144      : Codec(pt, nm, cr, pr),
145        bitrate(br),
146        channels(cs) {
147  }
148
149  // Creates an empty codec.
150  AudioCodec() : Codec(), bitrate(0), channels(0) {}
151
152  // Indicates if this codec is compatible with the specified codec.
153  bool Matches(const AudioCodec& codec) const;
154
155  static bool Preferable(const AudioCodec& first, const AudioCodec& other) {
156    return first.preference > other.preference;
157  }
158
159  std::string ToString() const;
160
161  AudioCodec& operator=(const AudioCodec& c) {
162    this->id = c.id;  // id is reserved in objective-c
163    name = c.name;
164    clockrate = c.clockrate;
165    bitrate = c.bitrate;
166    channels = c.channels;
167    preference =  c.preference;
168    params = c.params;
169    feedback_params = c.feedback_params;
170    return *this;
171  }
172
173  bool operator==(const AudioCodec& c) const {
174    return this->id == c.id &&  // id is reserved in objective-c
175           name == c.name &&
176           clockrate == c.clockrate &&
177           bitrate == c.bitrate &&
178           channels == c.channels &&
179           preference == c.preference &&
180           params == c.params &&
181           feedback_params == c.feedback_params;
182  }
183
184  bool operator!=(const AudioCodec& c) const {
185    return !(*this == c);
186  }
187};
188
189struct VideoCodec : public Codec {
190  int width;
191  int height;
192  int framerate;
193
194  // Creates a codec with the given parameters.
195  VideoCodec(int pt, const std::string& nm, int w, int h, int fr, int pr)
196      : Codec(pt, nm, kVideoCodecClockrate, pr),
197        width(w),
198        height(h),
199        framerate(fr) {
200  }
201
202  // Creates an empty codec.
203  VideoCodec()
204      : Codec(),
205        width(0),
206        height(0),
207        framerate(0) {
208    clockrate = kVideoCodecClockrate;
209  }
210
211  static bool Preferable(const VideoCodec& first, const VideoCodec& other) {
212    return first.preference > other.preference;
213  }
214
215  std::string ToString() const;
216
217  VideoCodec& operator=(const VideoCodec& c) {
218    this->id = c.id;  // id is reserved in objective-c
219    name = c.name;
220    clockrate = c.clockrate;
221    width = c.width;
222    height = c.height;
223    framerate = c.framerate;
224    preference =  c.preference;
225    params = c.params;
226    feedback_params = c.feedback_params;
227    return *this;
228  }
229
230  bool operator==(const VideoCodec& c) const {
231    return this->id == c.id &&  // id is reserved in objective-c
232           name == c.name &&
233           clockrate == c.clockrate &&
234           width == c.width &&
235           height == c.height &&
236           framerate == c.framerate &&
237           preference == c.preference &&
238           params == c.params &&
239           feedback_params == c.feedback_params;
240  }
241
242  bool operator!=(const VideoCodec& c) const {
243    return !(*this == c);
244  }
245};
246
247struct DataCodec : public Codec {
248  DataCodec(int id, const std::string& name, int preference)
249      : Codec(id, name, kDataCodecClockrate, preference) {
250  }
251
252  DataCodec() : Codec() {
253    clockrate = kDataCodecClockrate;
254  }
255
256  std::string ToString() const;
257};
258
259struct VideoEncoderConfig {
260  static const int kDefaultMaxThreads = -1;
261  static const int kDefaultCpuProfile = -1;
262
263  VideoEncoderConfig()
264      : max_codec(),
265        num_threads(kDefaultMaxThreads),
266        cpu_profile(kDefaultCpuProfile) {
267  }
268
269  VideoEncoderConfig(const VideoCodec& c)
270      : max_codec(c),
271        num_threads(kDefaultMaxThreads),
272        cpu_profile(kDefaultCpuProfile) {
273  }
274
275  VideoEncoderConfig(const VideoCodec& c, int t, int p)
276      : max_codec(c),
277        num_threads(t),
278        cpu_profile(p) {
279  }
280
281  VideoEncoderConfig& operator=(const VideoEncoderConfig& config) {
282    max_codec = config.max_codec;
283    num_threads = config.num_threads;
284    cpu_profile = config.cpu_profile;
285    return *this;
286  }
287
288  bool operator==(const VideoEncoderConfig& config) const {
289    return max_codec == config.max_codec &&
290           num_threads == config.num_threads &&
291           cpu_profile == config.cpu_profile;
292  }
293
294  bool operator!=(const VideoEncoderConfig& config) const {
295    return !(*this == config);
296  }
297
298  VideoCodec max_codec;
299  int num_threads;
300  int cpu_profile;
301};
302
303}  // namespace cricket
304
305#endif  // TALK_MEDIA_BASE_CODEC_H_
306