blob: 7476c026942761d48a4fd0172b24331a0e6931e7 [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
kjellander1afca732016-02-07 20:46:45 -08002 * Copyright (c) 2004 The WebRTC project authors. All Rights Reserved.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003 *
kjellander1afca732016-02-07 20:46:45 -08004 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00009 */
10
kjellandera96e2d72016-02-04 23:52:28 -080011#ifndef WEBRTC_MEDIA_BASE_CODEC_H_
12#define WEBRTC_MEDIA_BASE_CODEC_H_
henrike@webrtc.org28e20752013-07-10 00:45:36 +000013
14#include <map>
15#include <set>
16#include <string>
17#include <vector>
18
kjellanderf4752772016-03-02 05:42:30 -080019#include "webrtc/media/base/mediaconstants.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000020
21namespace cricket {
22
23typedef std::map<std::string, std::string> CodecParameterMap;
24
wu@webrtc.orgff1b1bf2014-06-20 20:57:42 +000025extern const int kMaxPayloadId;
26
henrike@webrtc.org28e20752013-07-10 00:45:36 +000027class FeedbackParam {
28 public:
29 FeedbackParam(const std::string& id, const std::string& param)
30 : id_(id),
31 param_(param) {
32 }
wu@webrtc.org91053e72013-08-10 07:18:04 +000033 explicit FeedbackParam(const std::string& id)
34 : id_(id),
35 param_(kParamValueEmpty) {
36 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +000037 bool operator==(const FeedbackParam& other) const;
38
39 const std::string& id() const { return id_; }
40 const std::string& param() const { return param_; }
41
42 private:
43 std::string id_; // e.g. "nack", "ccm"
44 std::string param_; // e.g. "", "rpsi", "fir"
45};
46
47class FeedbackParams {
48 public:
49 bool operator==(const FeedbackParams& other) const;
50
51 bool Has(const FeedbackParam& param) const;
52 void Add(const FeedbackParam& param);
53
54 void Intersect(const FeedbackParams& from);
55
56 const std::vector<FeedbackParam>& params() const { return params_; }
57 private:
58 bool HasDuplicateEntries() const;
59
60 std::vector<FeedbackParam> params_;
61};
62
63struct Codec {
64 int id;
65 std::string name;
66 int clockrate;
67 int preference;
68 CodecParameterMap params;
69 FeedbackParams feedback_params;
70
71 // Creates a codec with the given parameters.
guoweis@webrtc.orgbc6961f2015-02-19 17:55:18 +000072 Codec(int id, const std::string& name, int clockrate, int preference);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000073 // Creates an empty codec.
guoweis@webrtc.orgbc6961f2015-02-19 17:55:18 +000074 Codec();
75 Codec(const Codec& c);
76 ~Codec();
henrike@webrtc.org28e20752013-07-10 00:45:36 +000077
78 // Indicates if this codec is compatible with the specified codec.
79 bool Matches(const Codec& codec) const;
80
81 // Find the parameter for |name| and write the value to |out|.
82 bool GetParam(const std::string& name, std::string* out) const;
83 bool GetParam(const std::string& name, int* out) const;
84
85 void SetParam(const std::string& name, const std::string& value);
86 void SetParam(const std::string& name, int value);
87
buildbot@webrtc.orgfbd13282014-06-19 19:50:55 +000088 // It is safe to input a non-existent parameter.
89 // Returns true if the parameter existed, false if it did not exist.
90 bool RemoveParam(const std::string& name);
91
henrike@webrtc.org28e20752013-07-10 00:45:36 +000092 bool HasFeedbackParam(const FeedbackParam& param) const;
93 void AddFeedbackParam(const FeedbackParam& param);
94
95 static bool Preferable(const Codec& first, const Codec& other) {
96 return first.preference > other.preference;
97 }
98
99 // Filter |this| feedbacks params such that only those shared by both |this|
100 // and |other| are kept.
101 void IntersectFeedbackParams(const Codec& other);
102
guoweis@webrtc.orgbc6961f2015-02-19 17:55:18 +0000103 Codec& operator=(const Codec& c);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000104
guoweis@webrtc.orgbc6961f2015-02-19 17:55:18 +0000105 bool operator==(const Codec& c) const;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000106
107 bool operator!=(const Codec& c) const {
108 return !(*this == c);
109 }
110};
111
112struct AudioCodec : public Codec {
113 int bitrate;
Peter Kasting69558702016-01-12 16:26:35 -0800114 size_t channels;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000115
116 // Creates a codec with the given parameters.
pkasting25702cb2016-01-08 13:50:27 -0800117 AudioCodec(int id,
118 const std::string& name,
119 int clockrate,
120 int bitrate,
Peter Kasting69558702016-01-12 16:26:35 -0800121 size_t channels,
pkasting25702cb2016-01-08 13:50:27 -0800122 int preference);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000123 // Creates an empty codec.
guoweis@webrtc.orgbc6961f2015-02-19 17:55:18 +0000124 AudioCodec();
125 AudioCodec(const AudioCodec& c);
126 ~AudioCodec() = default;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000127
128 // Indicates if this codec is compatible with the specified codec.
129 bool Matches(const AudioCodec& codec) const;
130
131 static bool Preferable(const AudioCodec& first, const AudioCodec& other) {
132 return first.preference > other.preference;
133 }
134
135 std::string ToString() const;
136
guoweis@webrtc.orgbc6961f2015-02-19 17:55:18 +0000137 AudioCodec& operator=(const AudioCodec& c);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000138
guoweis@webrtc.orgbc6961f2015-02-19 17:55:18 +0000139 bool operator==(const AudioCodec& c) const;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000140
141 bool operator!=(const AudioCodec& c) const {
142 return !(*this == c);
143 }
144};
145
146struct VideoCodec : public Codec {
147 int width;
148 int height;
149 int framerate;
150
151 // Creates a codec with the given parameters.
pkasting25702cb2016-01-08 13:50:27 -0800152 VideoCodec(int id,
153 const std::string& name,
154 int width,
155 int height,
156 int framerate,
157 int preference);
158 VideoCodec(int id, const std::string& name);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000159 // Creates an empty codec.
guoweis@webrtc.orgbc6961f2015-02-19 17:55:18 +0000160 VideoCodec();
161 VideoCodec(const VideoCodec& c);
162 ~VideoCodec() = default;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000163
164 static bool Preferable(const VideoCodec& first, const VideoCodec& other) {
165 return first.preference > other.preference;
166 }
167
168 std::string ToString() const;
169
guoweis@webrtc.orgbc6961f2015-02-19 17:55:18 +0000170 VideoCodec& operator=(const VideoCodec& c);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000171
guoweis@webrtc.orgbc6961f2015-02-19 17:55:18 +0000172 bool operator==(const VideoCodec& c) const;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000173
174 bool operator!=(const VideoCodec& c) const {
175 return !(*this == c);
176 }
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000177
178 static VideoCodec CreateRtxCodec(int rtx_payload_type,
179 int associated_payload_type);
180
181 enum CodecType {
182 CODEC_VIDEO,
183 CODEC_RED,
184 CODEC_ULPFEC,
185 CODEC_RTX,
186 };
187
188 CodecType GetCodecType() const;
189 // Validates a VideoCodec's payload type, dimensions and bitrates etc. If they
190 // don't make sense (such as max < min bitrate), and error is logged and
191 // ValidateCodecFormat returns false.
192 bool ValidateCodecFormat() const;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000193};
194
195struct DataCodec : public Codec {
guoweis@webrtc.orgbc6961f2015-02-19 17:55:18 +0000196 DataCodec(int id, const std::string& name, int preference);
197 DataCodec();
198 DataCodec(const DataCodec& c);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000199
guoweis@webrtc.orgbc6961f2015-02-19 17:55:18 +0000200 DataCodec& operator=(const DataCodec& c);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000201
202 std::string ToString() const;
203};
204
changbin.shao@webrtc.org2d25b442015-03-16 04:14:34 +0000205// Get the codec setting associated with |payload_type|. If there
206// is no codec associated with that payload type it returns false.
207template <class Codec>
208bool FindCodecById(const std::vector<Codec>& codecs,
209 int payload_type,
210 Codec* codec_out) {
211 for (const auto& codec : codecs) {
212 if (codec.id == payload_type) {
213 *codec_out = codec;
214 return true;
215 }
216 }
217 return false;
218}
219
Shao Changbine62202f2015-04-21 20:24:50 +0800220bool CodecNamesEq(const std::string& name1, const std::string& name2);
stefanba4c0e42016-02-04 04:12:24 -0800221bool HasNack(const Codec& codec);
222bool HasRemb(const Codec& codec);
223bool HasTransportCc(const Codec& codec);
Shao Changbine62202f2015-04-21 20:24:50 +0800224
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000225} // namespace cricket
226
kjellandera96e2d72016-02-04 23:52:28 -0800227#endif // WEBRTC_MEDIA_BASE_CODEC_H_