blob: 1b29a88280901c7d5156d4d42ee9b2b2bb6ede7a [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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef MEDIA_BASE_CODEC_H_
12#define 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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020019#include "api/rtpparameters.h"
20#include "common_types.h"
21#include "media/base/mediaconstants.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000022
23namespace cricket {
24
25typedef std::map<std::string, std::string> CodecParameterMap;
26
27class FeedbackParam {
28 public:
deadbeefe814a0d2017-02-25 18:15:09 -080029 FeedbackParam() = default;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000030 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
Henrik Kjellander3fe372d2016-05-12 08:10:52 +020071 virtual ~Codec();
henrike@webrtc.org28e20752013-07-10 00:45:36 +000072
73 // Indicates if this codec is compatible with the specified codec.
74 bool Matches(const Codec& codec) const;
75
76 // Find the parameter for |name| and write the value to |out|.
77 bool GetParam(const std::string& name, std::string* out) const;
78 bool GetParam(const std::string& name, int* out) const;
79
80 void SetParam(const std::string& name, const std::string& value);
81 void SetParam(const std::string& name, int value);
82
buildbot@webrtc.orgfbd13282014-06-19 19:50:55 +000083 // It is safe to input a non-existent parameter.
84 // Returns true if the parameter existed, false if it did not exist.
85 bool RemoveParam(const std::string& name);
86
henrike@webrtc.org28e20752013-07-10 00:45:36 +000087 bool HasFeedbackParam(const FeedbackParam& param) const;
88 void AddFeedbackParam(const FeedbackParam& param);
89
henrike@webrtc.org28e20752013-07-10 00:45:36 +000090 // Filter |this| feedbacks params such that only those shared by both |this|
91 // and |other| are kept.
92 void IntersectFeedbackParams(const Codec& other);
93
Taylor Brandstetter0cd086b2016-04-20 16:23:10 -070094 virtual webrtc::RtpCodecParameters ToCodecParameters() const;
95
guoweis@webrtc.orgbc6961f2015-02-19 17:55:18 +000096 Codec& operator=(const Codec& c);
magjed3663c522016-11-07 10:14:36 -080097 Codec& operator=(Codec&& c);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000098
guoweis@webrtc.orgbc6961f2015-02-19 17:55:18 +000099 bool operator==(const Codec& c) const;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000100
101 bool operator!=(const Codec& c) const {
102 return !(*this == c);
103 }
htab39db842016-12-08 01:50:48 -0800104
105 protected:
106 // A Codec can't be created without a subclass.
107 // Creates a codec with the given parameters.
108 Codec(int id, const std::string& name, int clockrate);
109 // Creates an empty codec.
110 Codec();
111 Codec(const Codec& c);
112 Codec(Codec&& c);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000113};
114
115struct AudioCodec : public Codec {
116 int bitrate;
Peter Kasting69558702016-01-12 16:26:35 -0800117 size_t channels;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000118
119 // Creates a codec with the given parameters.
pkasting25702cb2016-01-08 13:50:27 -0800120 AudioCodec(int id,
121 const std::string& name,
122 int clockrate,
123 int bitrate,
deadbeef67cf2c12016-04-13 10:07:16 -0700124 size_t channels);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000125 // Creates an empty codec.
guoweis@webrtc.orgbc6961f2015-02-19 17:55:18 +0000126 AudioCodec();
127 AudioCodec(const AudioCodec& c);
magjed3663c522016-11-07 10:14:36 -0800128 AudioCodec(AudioCodec&& c);
Henrik Kjellander3fe372d2016-05-12 08:10:52 +0200129 virtual ~AudioCodec() = default;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000130
131 // Indicates if this codec is compatible with the specified codec.
132 bool Matches(const AudioCodec& codec) const;
133
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000134 std::string ToString() const;
135
Taylor Brandstetter0cd086b2016-04-20 16:23:10 -0700136 webrtc::RtpCodecParameters ToCodecParameters() const override;
137
guoweis@webrtc.orgbc6961f2015-02-19 17:55:18 +0000138 AudioCodec& operator=(const AudioCodec& c);
magjed3663c522016-11-07 10:14:36 -0800139 AudioCodec& operator=(AudioCodec&& c);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000140
guoweis@webrtc.orgbc6961f2015-02-19 17:55:18 +0000141 bool operator==(const AudioCodec& c) const;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000142
143 bool operator!=(const AudioCodec& c) const {
144 return !(*this == c);
145 }
146};
147
kwibergb94491d2017-02-21 06:16:19 -0800148inline std::ostream& operator<<(std::ostream& os, const AudioCodec& ac) {
149 os << "{id: " << ac.id;
150 os << ", name: " << ac.name;
151 os << ", clockrate: " << ac.clockrate;
152 os << ", bitrate: " << ac.bitrate;
153 os << ", channels: " << ac.channels;
154 os << ", params: {";
155 const char* sep = "";
156 for (const auto& kv : ac.params) {
157 os << sep << kv.first << ": " << kv.second;
158 sep = ", ";
159 }
160 os << "}, feedback_params: {";
161 sep = "";
162 for (const FeedbackParam& fp : ac.feedback_params.params()) {
163 os << sep << fp.id() << ": " << fp.param();
164 sep = ", ";
165 }
166 os << "}}";
167 return os;
168}
169
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000170struct VideoCodec : public Codec {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000171 // Creates a codec with the given parameters.
pkasting25702cb2016-01-08 13:50:27 -0800172 VideoCodec(int id, const std::string& name);
magjed1e45cc62016-10-28 07:43:45 -0700173 // Creates a codec with the given name and empty id.
174 explicit VideoCodec(const std::string& name);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000175 // Creates an empty codec.
guoweis@webrtc.orgbc6961f2015-02-19 17:55:18 +0000176 VideoCodec();
177 VideoCodec(const VideoCodec& c);
magjed3663c522016-11-07 10:14:36 -0800178 VideoCodec(VideoCodec&& c);
Henrik Kjellander3fe372d2016-05-12 08:10:52 +0200179 virtual ~VideoCodec() = default;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000180
magjedf823ede2016-11-12 09:53:04 -0800181 // Indicates if this video codec is the same as the other video codec, e.g. if
182 // they are both VP8 or VP9, or if they are both H264 with the same H264
183 // profile. H264 levels however are not compared.
184 bool Matches(const VideoCodec& codec) const;
185
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000186 std::string ToString() const;
187
deadbeefe702b302017-02-04 12:09:01 -0800188 webrtc::RtpCodecParameters ToCodecParameters() const override;
189
guoweis@webrtc.orgbc6961f2015-02-19 17:55:18 +0000190 VideoCodec& operator=(const VideoCodec& c);
magjed3663c522016-11-07 10:14:36 -0800191 VideoCodec& operator=(VideoCodec&& c);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000192
guoweis@webrtc.orgbc6961f2015-02-19 17:55:18 +0000193 bool operator==(const VideoCodec& c) const;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000194
195 bool operator!=(const VideoCodec& c) const {
196 return !(*this == c);
197 }
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000198
199 static VideoCodec CreateRtxCodec(int rtx_payload_type,
200 int associated_payload_type);
201
202 enum CodecType {
203 CODEC_VIDEO,
204 CODEC_RED,
205 CODEC_ULPFEC,
brandtr87d7d772016-11-07 03:03:41 -0800206 CODEC_FLEXFEC,
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000207 CODEC_RTX,
208 };
209
210 CodecType GetCodecType() const;
211 // Validates a VideoCodec's payload type, dimensions and bitrates etc. If they
212 // don't make sense (such as max < min bitrate), and error is logged and
213 // ValidateCodecFormat returns false.
214 bool ValidateCodecFormat() const;
hta9aa96882016-12-06 05:36:03 -0800215
216 private:
217 void SetDefaultParameters();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000218};
219
220struct DataCodec : public Codec {
deadbeef67cf2c12016-04-13 10:07:16 -0700221 DataCodec(int id, const std::string& name);
guoweis@webrtc.orgbc6961f2015-02-19 17:55:18 +0000222 DataCodec();
223 DataCodec(const DataCodec& c);
magjed3663c522016-11-07 10:14:36 -0800224 DataCodec(DataCodec&& c);
Henrik Kjellander3fe372d2016-05-12 08:10:52 +0200225 virtual ~DataCodec() = default;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000226
guoweis@webrtc.orgbc6961f2015-02-19 17:55:18 +0000227 DataCodec& operator=(const DataCodec& c);
magjed3663c522016-11-07 10:14:36 -0800228 DataCodec& operator=(DataCodec&& c);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000229
230 std::string ToString() const;
231};
232
changbin.shao@webrtc.org2d25b442015-03-16 04:14:34 +0000233// Get the codec setting associated with |payload_type|. If there
magjedb05fa242016-11-11 04:00:16 -0800234// is no codec associated with that payload type it returns nullptr.
changbin.shao@webrtc.org2d25b442015-03-16 04:14:34 +0000235template <class Codec>
magjedb05fa242016-11-11 04:00:16 -0800236const Codec* FindCodecById(const std::vector<Codec>& codecs, int payload_type) {
changbin.shao@webrtc.org2d25b442015-03-16 04:14:34 +0000237 for (const auto& codec : codecs) {
magjedb05fa242016-11-11 04:00:16 -0800238 if (codec.id == payload_type)
239 return &codec;
changbin.shao@webrtc.org2d25b442015-03-16 04:14:34 +0000240 }
magjedb05fa242016-11-11 04:00:16 -0800241 return nullptr;
changbin.shao@webrtc.org2d25b442015-03-16 04:14:34 +0000242}
243
Shao Changbine62202f2015-04-21 20:24:50 +0800244bool CodecNamesEq(const std::string& name1, const std::string& name2);
magjed1e45cc62016-10-28 07:43:45 -0700245bool CodecNamesEq(const char* name1, const char* name2);
stefanba4c0e42016-02-04 04:12:24 -0800246bool HasNack(const Codec& codec);
247bool HasRemb(const Codec& codec);
248bool HasTransportCc(const Codec& codec);
magjedf823ede2016-11-12 09:53:04 -0800249// Returns the first codec in |supported_codecs| that matches |codec|, or
250// nullptr if no codec matches.
251const VideoCodec* FindMatchingCodec(
252 const std::vector<VideoCodec>& supported_codecs,
253 const VideoCodec& codec);
Shao Changbine62202f2015-04-21 20:24:50 +0800254
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000255} // namespace cricket
256
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200257#endif // MEDIA_BASE_CODEC_H_