blob: c16158ffb2bdb105ec5ef52a2146d7f9a3e22d79 [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
Taylor Brandstetter0cd086b2016-04-20 16:23:10 -070019#include "webrtc/api/rtpparameters.h"
kjellanderf4752772016-03-02 05:42:30 -080020#include "webrtc/media/base/mediaconstants.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000021
22namespace cricket {
23
24typedef std::map<std::string, std::string> CodecParameterMap;
25
wu@webrtc.orgff1b1bf2014-06-20 20:57:42 +000026extern const int kMaxPayloadId;
27
henrike@webrtc.org28e20752013-07-10 00:45:36 +000028class FeedbackParam {
29 public:
30 FeedbackParam(const std::string& id, const std::string& param)
31 : id_(id),
32 param_(param) {
33 }
wu@webrtc.org91053e72013-08-10 07:18:04 +000034 explicit FeedbackParam(const std::string& id)
35 : id_(id),
36 param_(kParamValueEmpty) {
37 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +000038 bool operator==(const FeedbackParam& other) const;
39
40 const std::string& id() const { return id_; }
41 const std::string& param() const { return param_; }
42
43 private:
44 std::string id_; // e.g. "nack", "ccm"
45 std::string param_; // e.g. "", "rpsi", "fir"
46};
47
48class FeedbackParams {
49 public:
50 bool operator==(const FeedbackParams& other) const;
51
52 bool Has(const FeedbackParam& param) const;
53 void Add(const FeedbackParam& param);
54
55 void Intersect(const FeedbackParams& from);
56
57 const std::vector<FeedbackParam>& params() const { return params_; }
58 private:
59 bool HasDuplicateEntries() const;
60
61 std::vector<FeedbackParam> params_;
62};
63
64struct Codec {
65 int id;
66 std::string name;
67 int clockrate;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000068 CodecParameterMap params;
69 FeedbackParams feedback_params;
70
71 // Creates a codec with the given parameters.
deadbeef67cf2c12016-04-13 10:07:16 -070072 Codec(int id, const std::string& name, int clockrate);
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
henrike@webrtc.org28e20752013-07-10 00:45:36 +000095 // Filter |this| feedbacks params such that only those shared by both |this|
96 // and |other| are kept.
97 void IntersectFeedbackParams(const Codec& other);
98
Taylor Brandstetter0cd086b2016-04-20 16:23:10 -070099 virtual webrtc::RtpCodecParameters ToCodecParameters() const;
100
guoweis@webrtc.orgbc6961f2015-02-19 17:55:18 +0000101 Codec& operator=(const Codec& c);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000102
guoweis@webrtc.orgbc6961f2015-02-19 17:55:18 +0000103 bool operator==(const Codec& c) const;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000104
105 bool operator!=(const Codec& c) const {
106 return !(*this == c);
107 }
108};
109
110struct AudioCodec : public Codec {
111 int bitrate;
Peter Kasting69558702016-01-12 16:26:35 -0800112 size_t channels;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000113
114 // Creates a codec with the given parameters.
pkasting25702cb2016-01-08 13:50:27 -0800115 AudioCodec(int id,
116 const std::string& name,
117 int clockrate,
118 int bitrate,
deadbeef67cf2c12016-04-13 10:07:16 -0700119 size_t channels);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000120 // Creates an empty codec.
guoweis@webrtc.orgbc6961f2015-02-19 17:55:18 +0000121 AudioCodec();
122 AudioCodec(const AudioCodec& c);
123 ~AudioCodec() = default;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000124
125 // Indicates if this codec is compatible with the specified codec.
126 bool Matches(const AudioCodec& codec) const;
127
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000128 std::string ToString() const;
129
Taylor Brandstetter0cd086b2016-04-20 16:23:10 -0700130 webrtc::RtpCodecParameters ToCodecParameters() const override;
131
guoweis@webrtc.orgbc6961f2015-02-19 17:55:18 +0000132 AudioCodec& operator=(const AudioCodec& c);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000133
guoweis@webrtc.orgbc6961f2015-02-19 17:55:18 +0000134 bool operator==(const AudioCodec& c) const;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000135
136 bool operator!=(const AudioCodec& c) const {
137 return !(*this == c);
138 }
139};
140
141struct VideoCodec : public Codec {
142 int width;
143 int height;
144 int framerate;
145
146 // Creates a codec with the given parameters.
pkasting25702cb2016-01-08 13:50:27 -0800147 VideoCodec(int id,
148 const std::string& name,
149 int width,
150 int height,
deadbeef67cf2c12016-04-13 10:07:16 -0700151 int framerate);
pkasting25702cb2016-01-08 13:50:27 -0800152 VideoCodec(int id, const std::string& name);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000153 // Creates an empty codec.
guoweis@webrtc.orgbc6961f2015-02-19 17:55:18 +0000154 VideoCodec();
155 VideoCodec(const VideoCodec& c);
156 ~VideoCodec() = default;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000157
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000158 std::string ToString() const;
159
guoweis@webrtc.orgbc6961f2015-02-19 17:55:18 +0000160 VideoCodec& operator=(const VideoCodec& c);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000161
guoweis@webrtc.orgbc6961f2015-02-19 17:55:18 +0000162 bool operator==(const VideoCodec& c) const;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000163
164 bool operator!=(const VideoCodec& c) const {
165 return !(*this == c);
166 }
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000167
168 static VideoCodec CreateRtxCodec(int rtx_payload_type,
169 int associated_payload_type);
170
171 enum CodecType {
172 CODEC_VIDEO,
173 CODEC_RED,
174 CODEC_ULPFEC,
175 CODEC_RTX,
176 };
177
178 CodecType GetCodecType() const;
179 // Validates a VideoCodec's payload type, dimensions and bitrates etc. If they
180 // don't make sense (such as max < min bitrate), and error is logged and
181 // ValidateCodecFormat returns false.
182 bool ValidateCodecFormat() const;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000183};
184
185struct DataCodec : public Codec {
deadbeef67cf2c12016-04-13 10:07:16 -0700186 DataCodec(int id, const std::string& name);
guoweis@webrtc.orgbc6961f2015-02-19 17:55:18 +0000187 DataCodec();
188 DataCodec(const DataCodec& c);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000189
guoweis@webrtc.orgbc6961f2015-02-19 17:55:18 +0000190 DataCodec& operator=(const DataCodec& c);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000191
192 std::string ToString() const;
193};
194
changbin.shao@webrtc.org2d25b442015-03-16 04:14:34 +0000195// Get the codec setting associated with |payload_type|. If there
196// is no codec associated with that payload type it returns false.
197template <class Codec>
198bool FindCodecById(const std::vector<Codec>& codecs,
199 int payload_type,
200 Codec* codec_out) {
201 for (const auto& codec : codecs) {
202 if (codec.id == payload_type) {
203 *codec_out = codec;
204 return true;
205 }
206 }
207 return false;
208}
209
Shao Changbine62202f2015-04-21 20:24:50 +0800210bool CodecNamesEq(const std::string& name1, const std::string& name2);
stefanba4c0e42016-02-04 04:12:24 -0800211bool HasNack(const Codec& codec);
212bool HasRemb(const Codec& codec);
213bool HasTransportCc(const Codec& codec);
Shao Changbine62202f2015-04-21 20:24:50 +0800214
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000215} // namespace cricket
216
kjellandera96e2d72016-02-04 23:52:28 -0800217#endif // WEBRTC_MEDIA_BASE_CODEC_H_