blob: db133f6e27f644f51e0d79da829cc253f0013922 [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"
Magnus Jedvert024d8972017-09-29 15:00:29 +020020#include "api/video_codecs/sdp_video_format.h"
Mirko Bonadei71207422017-09-15 13:58:09 +020021#include "common_types.h" // NOLINT(build/include)
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020022#include "media/base/mediaconstants.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000023
24namespace cricket {
25
26typedef std::map<std::string, std::string> CodecParameterMap;
27
28class FeedbackParam {
29 public:
deadbeefe814a0d2017-02-25 18:15:09 -080030 FeedbackParam() = default;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000031 FeedbackParam(const std::string& id, const std::string& param)
Yves Gerey665174f2018-06-19 15:03:05 +020032 : id_(id), param_(param) {}
wu@webrtc.org91053e72013-08-10 07:18:04 +000033 explicit FeedbackParam(const std::string& id)
Yves Gerey665174f2018-06-19 15:03:05 +020034 : id_(id), param_(kParamValueEmpty) {}
Paulina Hensmana680a6a2018-04-05 11:42:24 +020035
henrike@webrtc.org28e20752013-07-10 00:45:36 +000036 bool operator==(const FeedbackParam& other) const;
37
38 const std::string& id() const { return id_; }
39 const std::string& param() const { return param_; }
40
41 private:
Yves Gerey665174f2018-06-19 15:03:05 +020042 std::string id_; // e.g. "nack", "ccm"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000043 std::string param_; // e.g. "", "rpsi", "fir"
44};
45
46class FeedbackParams {
47 public:
Magnus Jedvert244ad802017-09-28 21:19:18 +020048 FeedbackParams();
Paulina Hensmana680a6a2018-04-05 11:42:24 +020049 ~FeedbackParams();
henrike@webrtc.org28e20752013-07-10 00:45:36 +000050 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_; }
Yves Gerey665174f2018-06-19 15:03:05 +020058
henrike@webrtc.org28e20752013-07-10 00:45:36 +000059 private:
60 bool HasDuplicateEntries() const;
61
62 std::vector<FeedbackParam> params_;
63};
64
65struct Codec {
66 int id;
67 std::string name;
68 int clockrate;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000069 CodecParameterMap params;
70 FeedbackParams feedback_params;
71
Henrik Kjellander3fe372d2016-05-12 08:10:52 +020072 virtual ~Codec();
henrike@webrtc.org28e20752013-07-10 00:45:36 +000073
74 // Indicates if this codec is compatible with the specified codec.
75 bool Matches(const Codec& codec) const;
76
77 // Find the parameter for |name| and write the value to |out|.
78 bool GetParam(const std::string& name, std::string* out) const;
79 bool GetParam(const std::string& name, int* out) const;
80
81 void SetParam(const std::string& name, const std::string& value);
82 void SetParam(const std::string& name, int value);
83
buildbot@webrtc.orgfbd13282014-06-19 19:50:55 +000084 // It is safe to input a non-existent parameter.
85 // Returns true if the parameter existed, false if it did not exist.
86 bool RemoveParam(const std::string& name);
87
henrike@webrtc.org28e20752013-07-10 00:45:36 +000088 bool HasFeedbackParam(const FeedbackParam& param) const;
89 void AddFeedbackParam(const FeedbackParam& param);
90
henrike@webrtc.org28e20752013-07-10 00:45:36 +000091 // Filter |this| feedbacks params such that only those shared by both |this|
92 // and |other| are kept.
93 void IntersectFeedbackParams(const Codec& other);
94
Taylor Brandstetter0cd086b2016-04-20 16:23:10 -070095 virtual webrtc::RtpCodecParameters ToCodecParameters() const;
96
guoweis@webrtc.orgbc6961f2015-02-19 17:55:18 +000097 Codec& operator=(const Codec& c);
magjed3663c522016-11-07 10:14:36 -080098 Codec& operator=(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
Yves Gerey665174f2018-06-19 15:03:05 +0200102 bool operator!=(const Codec& c) const { return !(*this == c); }
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);
Magnus Jedvert244ad802017-09-28 21:19:18 +0200128 ~AudioCodec() override = 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
Yves Gerey665174f2018-06-19 15:03:05 +0200142 bool operator!=(const AudioCodec& c) const { return !(*this == c); }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000143};
144
145struct VideoCodec : public Codec {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000146 // Creates a codec with the given parameters.
pkasting25702cb2016-01-08 13:50:27 -0800147 VideoCodec(int id, const std::string& name);
magjed1e45cc62016-10-28 07:43:45 -0700148 // Creates a codec with the given name and empty id.
149 explicit VideoCodec(const std::string& name);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000150 // Creates an empty codec.
guoweis@webrtc.orgbc6961f2015-02-19 17:55:18 +0000151 VideoCodec();
152 VideoCodec(const VideoCodec& c);
Magnus Jedvert024d8972017-09-29 15:00:29 +0200153 explicit VideoCodec(const webrtc::SdpVideoFormat& c);
magjed3663c522016-11-07 10:14:36 -0800154 VideoCodec(VideoCodec&& c);
Magnus Jedvert244ad802017-09-28 21:19:18 +0200155 ~VideoCodec() override = default;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000156
magjedf823ede2016-11-12 09:53:04 -0800157 // Indicates if this video codec is the same as the other video codec, e.g. if
158 // they are both VP8 or VP9, or if they are both H264 with the same H264
159 // profile. H264 levels however are not compared.
160 bool Matches(const VideoCodec& codec) const;
161
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000162 std::string ToString() const;
163
deadbeefe702b302017-02-04 12:09:01 -0800164 webrtc::RtpCodecParameters ToCodecParameters() const override;
165
guoweis@webrtc.orgbc6961f2015-02-19 17:55:18 +0000166 VideoCodec& operator=(const VideoCodec& c);
magjed3663c522016-11-07 10:14:36 -0800167 VideoCodec& operator=(VideoCodec&& c);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000168
guoweis@webrtc.orgbc6961f2015-02-19 17:55:18 +0000169 bool operator==(const VideoCodec& c) const;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000170
Yves Gerey665174f2018-06-19 15:03:05 +0200171 bool operator!=(const VideoCodec& c) const { return !(*this == c); }
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000172
173 static VideoCodec CreateRtxCodec(int rtx_payload_type,
174 int associated_payload_type);
175
176 enum CodecType {
177 CODEC_VIDEO,
178 CODEC_RED,
179 CODEC_ULPFEC,
brandtr87d7d772016-11-07 03:03:41 -0800180 CODEC_FLEXFEC,
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000181 CODEC_RTX,
182 };
183
184 CodecType GetCodecType() const;
185 // Validates a VideoCodec's payload type, dimensions and bitrates etc. If they
186 // don't make sense (such as max < min bitrate), and error is logged and
187 // ValidateCodecFormat returns false.
188 bool ValidateCodecFormat() const;
hta9aa96882016-12-06 05:36:03 -0800189
190 private:
191 void SetDefaultParameters();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000192};
193
194struct DataCodec : public Codec {
deadbeef67cf2c12016-04-13 10:07:16 -0700195 DataCodec(int id, const std::string& name);
guoweis@webrtc.orgbc6961f2015-02-19 17:55:18 +0000196 DataCodec();
197 DataCodec(const DataCodec& c);
magjed3663c522016-11-07 10:14:36 -0800198 DataCodec(DataCodec&& c);
Magnus Jedvert244ad802017-09-28 21:19:18 +0200199 ~DataCodec() override = default;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000200
guoweis@webrtc.orgbc6961f2015-02-19 17:55:18 +0000201 DataCodec& operator=(const DataCodec& c);
magjed3663c522016-11-07 10:14:36 -0800202 DataCodec& operator=(DataCodec&& c);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000203
204 std::string ToString() const;
205};
206
changbin.shao@webrtc.org2d25b442015-03-16 04:14:34 +0000207// Get the codec setting associated with |payload_type|. If there
magjedb05fa242016-11-11 04:00:16 -0800208// is no codec associated with that payload type it returns nullptr.
changbin.shao@webrtc.org2d25b442015-03-16 04:14:34 +0000209template <class Codec>
magjedb05fa242016-11-11 04:00:16 -0800210const Codec* FindCodecById(const std::vector<Codec>& codecs, int payload_type) {
changbin.shao@webrtc.org2d25b442015-03-16 04:14:34 +0000211 for (const auto& codec : codecs) {
magjedb05fa242016-11-11 04:00:16 -0800212 if (codec.id == payload_type)
213 return &codec;
changbin.shao@webrtc.org2d25b442015-03-16 04:14:34 +0000214 }
magjedb05fa242016-11-11 04:00:16 -0800215 return nullptr;
changbin.shao@webrtc.org2d25b442015-03-16 04:14:34 +0000216}
217
Shao Changbine62202f2015-04-21 20:24:50 +0800218bool CodecNamesEq(const std::string& name1, const std::string& name2);
magjed1e45cc62016-10-28 07:43:45 -0700219bool CodecNamesEq(const char* name1, const char* name2);
stefanba4c0e42016-02-04 04:12:24 -0800220bool HasNack(const Codec& codec);
221bool HasRemb(const Codec& codec);
Ilya Nikolaevskiy634a7772018-04-04 16:33:49 +0200222bool HasRrtr(const Codec& codec);
stefanba4c0e42016-02-04 04:12:24 -0800223bool HasTransportCc(const Codec& codec);
magjedf823ede2016-11-12 09:53:04 -0800224// Returns the first codec in |supported_codecs| that matches |codec|, or
225// nullptr if no codec matches.
226const VideoCodec* FindMatchingCodec(
227 const std::vector<VideoCodec>& supported_codecs,
228 const VideoCodec& codec);
Magnus Jedvert523589d2017-11-23 13:24:53 +0100229bool IsSameCodec(const std::string& name1,
230 const CodecParameterMap& params1,
231 const std::string& name2,
232 const CodecParameterMap& params2);
Shao Changbine62202f2015-04-21 20:24:50 +0800233
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000234} // namespace cricket
235
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200236#endif // MEDIA_BASE_CODEC_H_