blob: 0aa0d58ef76b6cc07ec10c5c0a0ea2b88b5fd487 [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"
magjed1e45cc62016-10-28 07:43:45 -070020#include "webrtc/common_types.h"
kjellanderf4752772016-03-02 05:42:30 -080021#include "webrtc/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:
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
Henrik Kjellander3fe372d2016-05-12 08:10:52 +020070 virtual ~Codec();
henrike@webrtc.org28e20752013-07-10 00:45:36 +000071
72 // Indicates if this codec is compatible with the specified codec.
73 bool Matches(const Codec& codec) const;
74
75 // Find the parameter for |name| and write the value to |out|.
76 bool GetParam(const std::string& name, std::string* out) const;
77 bool GetParam(const std::string& name, int* out) const;
78
79 void SetParam(const std::string& name, const std::string& value);
80 void SetParam(const std::string& name, int value);
81
buildbot@webrtc.orgfbd13282014-06-19 19:50:55 +000082 // It is safe to input a non-existent parameter.
83 // Returns true if the parameter existed, false if it did not exist.
84 bool RemoveParam(const std::string& name);
85
henrike@webrtc.org28e20752013-07-10 00:45:36 +000086 bool HasFeedbackParam(const FeedbackParam& param) const;
87 void AddFeedbackParam(const FeedbackParam& param);
88
henrike@webrtc.org28e20752013-07-10 00:45:36 +000089 // Filter |this| feedbacks params such that only those shared by both |this|
90 // and |other| are kept.
91 void IntersectFeedbackParams(const Codec& other);
92
Taylor Brandstetter0cd086b2016-04-20 16:23:10 -070093 virtual webrtc::RtpCodecParameters ToCodecParameters() const;
94
guoweis@webrtc.orgbc6961f2015-02-19 17:55:18 +000095 Codec& operator=(const Codec& c);
magjed3663c522016-11-07 10:14:36 -080096 Codec& operator=(Codec&& c);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000097
guoweis@webrtc.orgbc6961f2015-02-19 17:55:18 +000098 bool operator==(const Codec& c) const;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000099
100 bool operator!=(const Codec& c) const {
101 return !(*this == c);
102 }
htab39db842016-12-08 01:50:48 -0800103
104 protected:
105 // A Codec can't be created without a subclass.
106 // Creates a codec with the given parameters.
107 Codec(int id, const std::string& name, int clockrate);
108 // Creates an empty codec.
109 Codec();
110 Codec(const Codec& c);
111 Codec(Codec&& c);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000112};
113
114struct AudioCodec : public Codec {
115 int bitrate;
Peter Kasting69558702016-01-12 16:26:35 -0800116 size_t channels;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000117
118 // Creates a codec with the given parameters.
pkasting25702cb2016-01-08 13:50:27 -0800119 AudioCodec(int id,
120 const std::string& name,
121 int clockrate,
122 int bitrate,
deadbeef67cf2c12016-04-13 10:07:16 -0700123 size_t channels);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000124 // Creates an empty codec.
guoweis@webrtc.orgbc6961f2015-02-19 17:55:18 +0000125 AudioCodec();
126 AudioCodec(const AudioCodec& c);
magjed3663c522016-11-07 10:14:36 -0800127 AudioCodec(AudioCodec&& c);
Henrik Kjellander3fe372d2016-05-12 08:10:52 +0200128 virtual ~AudioCodec() = default;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000129
130 // Indicates if this codec is compatible with the specified codec.
131 bool Matches(const AudioCodec& codec) const;
132
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000133 std::string ToString() const;
134
Taylor Brandstetter0cd086b2016-04-20 16:23:10 -0700135 webrtc::RtpCodecParameters ToCodecParameters() const override;
136
guoweis@webrtc.orgbc6961f2015-02-19 17:55:18 +0000137 AudioCodec& operator=(const AudioCodec& c);
magjed3663c522016-11-07 10:14:36 -0800138 AudioCodec& operator=(AudioCodec&& c);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000139
guoweis@webrtc.orgbc6961f2015-02-19 17:55:18 +0000140 bool operator==(const AudioCodec& c) const;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000141
142 bool operator!=(const AudioCodec& c) const {
143 return !(*this == c);
144 }
145};
146
kwibergb94491d2017-02-21 06:16:19 -0800147inline std::ostream& operator<<(std::ostream& os, const AudioCodec& ac) {
148 os << "{id: " << ac.id;
149 os << ", name: " << ac.name;
150 os << ", clockrate: " << ac.clockrate;
151 os << ", bitrate: " << ac.bitrate;
152 os << ", channels: " << ac.channels;
153 os << ", params: {";
154 const char* sep = "";
155 for (const auto& kv : ac.params) {
156 os << sep << kv.first << ": " << kv.second;
157 sep = ", ";
158 }
159 os << "}, feedback_params: {";
160 sep = "";
161 for (const FeedbackParam& fp : ac.feedback_params.params()) {
162 os << sep << fp.id() << ": " << fp.param();
163 sep = ", ";
164 }
165 os << "}}";
166 return os;
167}
168
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000169struct VideoCodec : public Codec {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000170 // Creates a codec with the given parameters.
pkasting25702cb2016-01-08 13:50:27 -0800171 VideoCodec(int id, const std::string& name);
magjed1e45cc62016-10-28 07:43:45 -0700172 // Creates a codec with the given name and empty id.
173 explicit VideoCodec(const std::string& name);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000174 // Creates an empty codec.
guoweis@webrtc.orgbc6961f2015-02-19 17:55:18 +0000175 VideoCodec();
176 VideoCodec(const VideoCodec& c);
magjed3663c522016-11-07 10:14:36 -0800177 VideoCodec(VideoCodec&& c);
Henrik Kjellander3fe372d2016-05-12 08:10:52 +0200178 virtual ~VideoCodec() = default;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000179
magjedf823ede2016-11-12 09:53:04 -0800180 // Indicates if this video codec is the same as the other video codec, e.g. if
181 // they are both VP8 or VP9, or if they are both H264 with the same H264
182 // profile. H264 levels however are not compared.
183 bool Matches(const VideoCodec& codec) const;
184
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000185 std::string ToString() const;
186
deadbeefe702b302017-02-04 12:09:01 -0800187 webrtc::RtpCodecParameters ToCodecParameters() const override;
188
guoweis@webrtc.orgbc6961f2015-02-19 17:55:18 +0000189 VideoCodec& operator=(const VideoCodec& c);
magjed3663c522016-11-07 10:14:36 -0800190 VideoCodec& operator=(VideoCodec&& c);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000191
guoweis@webrtc.orgbc6961f2015-02-19 17:55:18 +0000192 bool operator==(const VideoCodec& c) const;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000193
194 bool operator!=(const VideoCodec& c) const {
195 return !(*this == c);
196 }
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000197
198 static VideoCodec CreateRtxCodec(int rtx_payload_type,
199 int associated_payload_type);
200
201 enum CodecType {
202 CODEC_VIDEO,
203 CODEC_RED,
204 CODEC_ULPFEC,
brandtr87d7d772016-11-07 03:03:41 -0800205 CODEC_FLEXFEC,
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000206 CODEC_RTX,
207 };
208
209 CodecType GetCodecType() const;
210 // Validates a VideoCodec's payload type, dimensions and bitrates etc. If they
211 // don't make sense (such as max < min bitrate), and error is logged and
212 // ValidateCodecFormat returns false.
213 bool ValidateCodecFormat() const;
hta9aa96882016-12-06 05:36:03 -0800214
215 private:
216 void SetDefaultParameters();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000217};
218
219struct DataCodec : public Codec {
deadbeef67cf2c12016-04-13 10:07:16 -0700220 DataCodec(int id, const std::string& name);
guoweis@webrtc.orgbc6961f2015-02-19 17:55:18 +0000221 DataCodec();
222 DataCodec(const DataCodec& c);
magjed3663c522016-11-07 10:14:36 -0800223 DataCodec(DataCodec&& c);
Henrik Kjellander3fe372d2016-05-12 08:10:52 +0200224 virtual ~DataCodec() = default;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000225
guoweis@webrtc.orgbc6961f2015-02-19 17:55:18 +0000226 DataCodec& operator=(const DataCodec& c);
magjed3663c522016-11-07 10:14:36 -0800227 DataCodec& operator=(DataCodec&& c);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000228
229 std::string ToString() const;
230};
231
changbin.shao@webrtc.org2d25b442015-03-16 04:14:34 +0000232// Get the codec setting associated with |payload_type|. If there
magjedb05fa242016-11-11 04:00:16 -0800233// is no codec associated with that payload type it returns nullptr.
changbin.shao@webrtc.org2d25b442015-03-16 04:14:34 +0000234template <class Codec>
magjedb05fa242016-11-11 04:00:16 -0800235const Codec* FindCodecById(const std::vector<Codec>& codecs, int payload_type) {
changbin.shao@webrtc.org2d25b442015-03-16 04:14:34 +0000236 for (const auto& codec : codecs) {
magjedb05fa242016-11-11 04:00:16 -0800237 if (codec.id == payload_type)
238 return &codec;
changbin.shao@webrtc.org2d25b442015-03-16 04:14:34 +0000239 }
magjedb05fa242016-11-11 04:00:16 -0800240 return nullptr;
changbin.shao@webrtc.org2d25b442015-03-16 04:14:34 +0000241}
242
Shao Changbine62202f2015-04-21 20:24:50 +0800243bool CodecNamesEq(const std::string& name1, const std::string& name2);
magjed1e45cc62016-10-28 07:43:45 -0700244bool CodecNamesEq(const char* name1, const char* name2);
stefanba4c0e42016-02-04 04:12:24 -0800245bool HasNack(const Codec& codec);
246bool HasRemb(const Codec& codec);
247bool HasTransportCc(const Codec& codec);
magjedf823ede2016-11-12 09:53:04 -0800248// Returns the first codec in |supported_codecs| that matches |codec|, or
249// nullptr if no codec matches.
250const VideoCodec* FindMatchingCodec(
251 const std::vector<VideoCodec>& supported_codecs,
252 const VideoCodec& codec);
Shao Changbine62202f2015-04-21 20:24:50 +0800253
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000254} // namespace cricket
255
kjellandera96e2d72016-02-04 23:52:28 -0800256#endif // WEBRTC_MEDIA_BASE_CODEC_H_