blob: c79a3fafc8f754cd9f1ce3f3e708b095155eb316 [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;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000067 CodecParameterMap params;
68 FeedbackParams feedback_params;
69
70 // Creates a codec with the given parameters.
deadbeef67cf2c12016-04-13 10:07:16 -070071 Codec(int id, const std::string& name, int clockrate);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000072 // Creates an empty codec.
guoweis@webrtc.orgbc6961f2015-02-19 17:55:18 +000073 Codec();
74 Codec(const Codec& c);
75 ~Codec();
henrike@webrtc.org28e20752013-07-10 00:45:36 +000076
77 // Indicates if this codec is compatible with the specified codec.
78 bool Matches(const Codec& codec) const;
79
80 // Find the parameter for |name| and write the value to |out|.
81 bool GetParam(const std::string& name, std::string* out) const;
82 bool GetParam(const std::string& name, int* out) const;
83
84 void SetParam(const std::string& name, const std::string& value);
85 void SetParam(const std::string& name, int value);
86
buildbot@webrtc.orgfbd13282014-06-19 19:50:55 +000087 // It is safe to input a non-existent parameter.
88 // Returns true if the parameter existed, false if it did not exist.
89 bool RemoveParam(const std::string& name);
90
henrike@webrtc.org28e20752013-07-10 00:45:36 +000091 bool HasFeedbackParam(const FeedbackParam& param) const;
92 void AddFeedbackParam(const FeedbackParam& param);
93
henrike@webrtc.org28e20752013-07-10 00:45:36 +000094 // Filter |this| feedbacks params such that only those shared by both |this|
95 // and |other| are kept.
96 void IntersectFeedbackParams(const Codec& other);
97
guoweis@webrtc.orgbc6961f2015-02-19 17:55:18 +000098 Codec& operator=(const Codec& c);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000099
guoweis@webrtc.orgbc6961f2015-02-19 17:55:18 +0000100 bool operator==(const Codec& c) const;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000101
102 bool operator!=(const Codec& c) const {
103 return !(*this == c);
104 }
105};
106
107struct AudioCodec : public Codec {
108 int bitrate;
Peter Kasting69558702016-01-12 16:26:35 -0800109 size_t channels;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000110
111 // Creates a codec with the given parameters.
pkasting25702cb2016-01-08 13:50:27 -0800112 AudioCodec(int id,
113 const std::string& name,
114 int clockrate,
115 int bitrate,
deadbeef67cf2c12016-04-13 10:07:16 -0700116 size_t channels);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000117 // Creates an empty codec.
guoweis@webrtc.orgbc6961f2015-02-19 17:55:18 +0000118 AudioCodec();
119 AudioCodec(const AudioCodec& c);
120 ~AudioCodec() = default;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000121
122 // Indicates if this codec is compatible with the specified codec.
123 bool Matches(const AudioCodec& codec) const;
124
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000125 std::string ToString() const;
126
guoweis@webrtc.orgbc6961f2015-02-19 17:55:18 +0000127 AudioCodec& operator=(const AudioCodec& c);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000128
guoweis@webrtc.orgbc6961f2015-02-19 17:55:18 +0000129 bool operator==(const AudioCodec& c) const;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000130
131 bool operator!=(const AudioCodec& c) const {
132 return !(*this == c);
133 }
134};
135
136struct VideoCodec : public Codec {
137 int width;
138 int height;
139 int framerate;
140
141 // Creates a codec with the given parameters.
pkasting25702cb2016-01-08 13:50:27 -0800142 VideoCodec(int id,
143 const std::string& name,
144 int width,
145 int height,
deadbeef67cf2c12016-04-13 10:07:16 -0700146 int framerate);
pkasting25702cb2016-01-08 13:50:27 -0800147 VideoCodec(int id, const std::string& name);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000148 // Creates an empty codec.
guoweis@webrtc.orgbc6961f2015-02-19 17:55:18 +0000149 VideoCodec();
150 VideoCodec(const VideoCodec& c);
151 ~VideoCodec() = default;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000152
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000153 std::string ToString() const;
154
guoweis@webrtc.orgbc6961f2015-02-19 17:55:18 +0000155 VideoCodec& operator=(const VideoCodec& c);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000156
guoweis@webrtc.orgbc6961f2015-02-19 17:55:18 +0000157 bool operator==(const VideoCodec& c) const;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000158
159 bool operator!=(const VideoCodec& c) const {
160 return !(*this == c);
161 }
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000162
163 static VideoCodec CreateRtxCodec(int rtx_payload_type,
164 int associated_payload_type);
165
166 enum CodecType {
167 CODEC_VIDEO,
168 CODEC_RED,
169 CODEC_ULPFEC,
170 CODEC_RTX,
171 };
172
173 CodecType GetCodecType() const;
174 // Validates a VideoCodec's payload type, dimensions and bitrates etc. If they
175 // don't make sense (such as max < min bitrate), and error is logged and
176 // ValidateCodecFormat returns false.
177 bool ValidateCodecFormat() const;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000178};
179
180struct DataCodec : public Codec {
deadbeef67cf2c12016-04-13 10:07:16 -0700181 DataCodec(int id, const std::string& name);
guoweis@webrtc.orgbc6961f2015-02-19 17:55:18 +0000182 DataCodec();
183 DataCodec(const DataCodec& c);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000184
guoweis@webrtc.orgbc6961f2015-02-19 17:55:18 +0000185 DataCodec& operator=(const DataCodec& c);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000186
187 std::string ToString() const;
188};
189
changbin.shao@webrtc.org2d25b442015-03-16 04:14:34 +0000190// Get the codec setting associated with |payload_type|. If there
191// is no codec associated with that payload type it returns false.
192template <class Codec>
193bool FindCodecById(const std::vector<Codec>& codecs,
194 int payload_type,
195 Codec* codec_out) {
196 for (const auto& codec : codecs) {
197 if (codec.id == payload_type) {
198 *codec_out = codec;
199 return true;
200 }
201 }
202 return false;
203}
204
Shao Changbine62202f2015-04-21 20:24:50 +0800205bool CodecNamesEq(const std::string& name1, const std::string& name2);
stefanba4c0e42016-02-04 04:12:24 -0800206bool HasNack(const Codec& codec);
207bool HasRemb(const Codec& codec);
208bool HasTransportCc(const Codec& codec);
Shao Changbine62202f2015-04-21 20:24:50 +0800209
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000210} // namespace cricket
211
kjellandera96e2d72016-02-04 23:52:28 -0800212#endif // WEBRTC_MEDIA_BASE_CODEC_H_