blob: c3be2334ce4344793e7136a2118b6927130ebce4 [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:361/*
kjellander1afca732016-02-08 04:46:452 * Copyright (c) 2004 The WebRTC project authors. All Rights Reserved.
henrike@webrtc.org28e20752013-07-10 00:45:363 *
kjellander1afca732016-02-08 04:46:454 * 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:369 */
10
Mirko Bonadei92ea95e2017-09-15 04:47:3111#ifndef MEDIA_BASE_CODEC_H_
12#define MEDIA_BASE_CODEC_H_
henrike@webrtc.org28e20752013-07-10 00:45:3613
14#include <map>
15#include <set>
16#include <string>
17#include <vector>
18
Mirta Dvornicic479a3c02019-06-04 13:38:5019#include "absl/types/optional.h"
Steve Anton10542f22019-01-11 17:11:0020#include "api/rtp_parameters.h"
Magnus Jedvert024d8972017-09-29 13:00:2921#include "api/video_codecs/sdp_video_format.h"
Steve Anton10542f22019-01-11 17:11:0022#include "media/base/media_constants.h"
Mirko Bonadeid65d1792018-10-17 14:50:0723#include "rtc_base/system/rtc_export.h"
henrike@webrtc.org28e20752013-07-10 00:45:3624
25namespace cricket {
26
27typedef std::map<std::string, std::string> CodecParameterMap;
28
29class FeedbackParam {
30 public:
deadbeefe814a0d2017-02-26 02:15:0931 FeedbackParam() = default;
henrike@webrtc.org28e20752013-07-10 00:45:3632 FeedbackParam(const std::string& id, const std::string& param)
Yves Gerey665174f2018-06-19 13:03:0533 : id_(id), param_(param) {}
wu@webrtc.org91053e72013-08-10 07:18:0434 explicit FeedbackParam(const std::string& id)
Yves Gerey665174f2018-06-19 13:03:0535 : id_(id), param_(kParamValueEmpty) {}
Paulina Hensmana680a6a2018-04-05 09:42:2436
henrike@webrtc.org28e20752013-07-10 00:45:3637 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:
Yves Gerey665174f2018-06-19 13:03:0543 std::string id_; // e.g. "nack", "ccm"
henrike@webrtc.org28e20752013-07-10 00:45:3644 std::string param_; // e.g. "", "rpsi", "fir"
45};
46
47class FeedbackParams {
48 public:
Magnus Jedvert244ad802017-09-28 19:19:1849 FeedbackParams();
Paulina Hensmana680a6a2018-04-05 09:42:2450 ~FeedbackParams();
henrike@webrtc.org28e20752013-07-10 00:45:3651 bool operator==(const FeedbackParams& other) const;
52
53 bool Has(const FeedbackParam& param) const;
54 void Add(const FeedbackParam& param);
55
56 void Intersect(const FeedbackParams& from);
57
58 const std::vector<FeedbackParam>& params() const { return params_; }
Yves Gerey665174f2018-06-19 13:03:0559
henrike@webrtc.org28e20752013-07-10 00:45:3660 private:
61 bool HasDuplicateEntries() const;
62
63 std::vector<FeedbackParam> params_;
64};
65
Mirko Bonadeid65d1792018-10-17 14:50:0766struct RTC_EXPORT Codec {
henrike@webrtc.org28e20752013-07-10 00:45:3667 int id;
68 std::string name;
69 int clockrate;
Philipp Hanckef2a4ec12020-07-01 19:07:3270 // Non key-value parameters such as the telephone-event "0‐15" are
71 // represented using an empty string as key, i.e. {"": "0-15"}.
henrike@webrtc.org28e20752013-07-10 00:45:3672 CodecParameterMap params;
73 FeedbackParams feedback_params;
74
Henrik Kjellander3fe372d2016-05-12 06:10:5275 virtual ~Codec();
henrike@webrtc.org28e20752013-07-10 00:45:3676
77 // Indicates if this codec is compatible with the specified codec.
78 bool Matches(const Codec& codec) const;
Florent Castelli2d9d82e2019-04-23 17:25:5179 bool MatchesCapability(const webrtc::RtpCodecCapability& capability) const;
henrike@webrtc.org28e20752013-07-10 00:45:3680
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:5588 // 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:3692 bool HasFeedbackParam(const FeedbackParam& param) const;
93 void AddFeedbackParam(const FeedbackParam& param);
94
henrike@webrtc.org28e20752013-07-10 00:45:3695 // 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 23:23:1099 virtual webrtc::RtpCodecParameters ToCodecParameters() const;
100
guoweis@webrtc.orgbc6961f2015-02-19 17:55:18101 Codec& operator=(const Codec& c);
magjed3663c522016-11-07 18:14:36102 Codec& operator=(Codec&& c);
henrike@webrtc.org28e20752013-07-10 00:45:36103
guoweis@webrtc.orgbc6961f2015-02-19 17:55:18104 bool operator==(const Codec& c) const;
henrike@webrtc.org28e20752013-07-10 00:45:36105
Yves Gerey665174f2018-06-19 13:03:05106 bool operator!=(const Codec& c) const { return !(*this == c); }
htab39db842016-12-08 09:50:48107
108 protected:
109 // A Codec can't be created without a subclass.
110 // Creates a codec with the given parameters.
111 Codec(int id, const std::string& name, int clockrate);
112 // Creates an empty codec.
113 Codec();
114 Codec(const Codec& c);
115 Codec(Codec&& c);
henrike@webrtc.org28e20752013-07-10 00:45:36116};
117
118struct AudioCodec : public Codec {
119 int bitrate;
Peter Kasting69558702016-01-13 00:26:35120 size_t channels;
henrike@webrtc.org28e20752013-07-10 00:45:36121
122 // Creates a codec with the given parameters.
pkasting25702cb2016-01-08 21:50:27123 AudioCodec(int id,
124 const std::string& name,
125 int clockrate,
126 int bitrate,
deadbeef67cf2c12016-04-13 17:07:16127 size_t channels);
henrike@webrtc.org28e20752013-07-10 00:45:36128 // Creates an empty codec.
guoweis@webrtc.orgbc6961f2015-02-19 17:55:18129 AudioCodec();
130 AudioCodec(const AudioCodec& c);
magjed3663c522016-11-07 18:14:36131 AudioCodec(AudioCodec&& c);
Magnus Jedvert244ad802017-09-28 19:19:18132 ~AudioCodec() override = default;
henrike@webrtc.org28e20752013-07-10 00:45:36133
134 // Indicates if this codec is compatible with the specified codec.
135 bool Matches(const AudioCodec& codec) const;
136
henrike@webrtc.org28e20752013-07-10 00:45:36137 std::string ToString() const;
138
Taylor Brandstetter0cd086b2016-04-20 23:23:10139 webrtc::RtpCodecParameters ToCodecParameters() const override;
140
guoweis@webrtc.orgbc6961f2015-02-19 17:55:18141 AudioCodec& operator=(const AudioCodec& c);
magjed3663c522016-11-07 18:14:36142 AudioCodec& operator=(AudioCodec&& c);
henrike@webrtc.org28e20752013-07-10 00:45:36143
guoweis@webrtc.orgbc6961f2015-02-19 17:55:18144 bool operator==(const AudioCodec& c) const;
henrike@webrtc.org28e20752013-07-10 00:45:36145
Yves Gerey665174f2018-06-19 13:03:05146 bool operator!=(const AudioCodec& c) const { return !(*this == c); }
henrike@webrtc.org28e20752013-07-10 00:45:36147};
148
Mirko Bonadeid65d1792018-10-17 14:50:07149struct RTC_EXPORT VideoCodec : public Codec {
Mirta Dvornicic479a3c02019-06-04 13:38:50150 absl::optional<std::string> packetization;
151
henrike@webrtc.org28e20752013-07-10 00:45:36152 // Creates a codec with the given parameters.
pkasting25702cb2016-01-08 21:50:27153 VideoCodec(int id, const std::string& name);
magjed1e45cc62016-10-28 14:43:45154 // Creates a codec with the given name and empty id.
155 explicit VideoCodec(const std::string& name);
henrike@webrtc.org28e20752013-07-10 00:45:36156 // Creates an empty codec.
guoweis@webrtc.orgbc6961f2015-02-19 17:55:18157 VideoCodec();
158 VideoCodec(const VideoCodec& c);
Magnus Jedvert024d8972017-09-29 13:00:29159 explicit VideoCodec(const webrtc::SdpVideoFormat& c);
magjed3663c522016-11-07 18:14:36160 VideoCodec(VideoCodec&& c);
Magnus Jedvert244ad802017-09-28 19:19:18161 ~VideoCodec() override = default;
henrike@webrtc.org28e20752013-07-10 00:45:36162
magjedf823ede2016-11-12 17:53:04163 // Indicates if this video codec is the same as the other video codec, e.g. if
164 // they are both VP8 or VP9, or if they are both H264 with the same H264
165 // profile. H264 levels however are not compared.
166 bool Matches(const VideoCodec& codec) const;
167
henrike@webrtc.org28e20752013-07-10 00:45:36168 std::string ToString() const;
169
deadbeefe702b302017-02-04 20:09:01170 webrtc::RtpCodecParameters ToCodecParameters() const override;
171
guoweis@webrtc.orgbc6961f2015-02-19 17:55:18172 VideoCodec& operator=(const VideoCodec& c);
magjed3663c522016-11-07 18:14:36173 VideoCodec& operator=(VideoCodec&& c);
henrike@webrtc.org28e20752013-07-10 00:45:36174
guoweis@webrtc.orgbc6961f2015-02-19 17:55:18175 bool operator==(const VideoCodec& c) const;
henrike@webrtc.org28e20752013-07-10 00:45:36176
Yves Gerey665174f2018-06-19 13:03:05177 bool operator!=(const VideoCodec& c) const { return !(*this == c); }
pbos@webrtc.orgb5a22b12014-05-13 11:07:01178
Mirta Dvornicic479a3c02019-06-04 13:38:50179 // Return packetization which both |local_codec| and |remote_codec| support.
180 static absl::optional<std::string> IntersectPacketization(
181 const VideoCodec& local_codec,
182 const VideoCodec& remote_codec);
183
pbos@webrtc.orgb5a22b12014-05-13 11:07:01184 static VideoCodec CreateRtxCodec(int rtx_payload_type,
185 int associated_payload_type);
186
187 enum CodecType {
188 CODEC_VIDEO,
189 CODEC_RED,
190 CODEC_ULPFEC,
brandtr87d7d772016-11-07 11:03:41191 CODEC_FLEXFEC,
pbos@webrtc.orgb5a22b12014-05-13 11:07:01192 CODEC_RTX,
193 };
194
195 CodecType GetCodecType() const;
196 // Validates a VideoCodec's payload type, dimensions and bitrates etc. If they
197 // don't make sense (such as max < min bitrate), and error is logged and
198 // ValidateCodecFormat returns false.
199 bool ValidateCodecFormat() const;
hta9aa96882016-12-06 13:36:03200
201 private:
202 void SetDefaultParameters();
henrike@webrtc.org28e20752013-07-10 00:45:36203};
204
Harald Alvestrand5fc28b12019-05-13 11:36:16205struct RtpDataCodec : public Codec {
206 RtpDataCodec(int id, const std::string& name);
207 RtpDataCodec();
208 RtpDataCodec(const RtpDataCodec& c);
209 RtpDataCodec(RtpDataCodec&& c);
210 ~RtpDataCodec() override = default;
henrike@webrtc.org28e20752013-07-10 00:45:36211
Harald Alvestrand5fc28b12019-05-13 11:36:16212 RtpDataCodec& operator=(const RtpDataCodec& c);
213 RtpDataCodec& operator=(RtpDataCodec&& c);
henrike@webrtc.org28e20752013-07-10 00:45:36214
215 std::string ToString() const;
216};
217
Harald Alvestrand5fc28b12019-05-13 11:36:16218// For backwards compatibility
219// TODO(bugs.webrtc.org/10597): Remove when no longer needed.
220typedef RtpDataCodec DataCodec;
221
changbin.shao@webrtc.org2d25b442015-03-16 04:14:34222// Get the codec setting associated with |payload_type|. If there
magjedb05fa242016-11-11 12:00:16223// is no codec associated with that payload type it returns nullptr.
changbin.shao@webrtc.org2d25b442015-03-16 04:14:34224template <class Codec>
magjedb05fa242016-11-11 12:00:16225const Codec* FindCodecById(const std::vector<Codec>& codecs, int payload_type) {
changbin.shao@webrtc.org2d25b442015-03-16 04:14:34226 for (const auto& codec : codecs) {
magjedb05fa242016-11-11 12:00:16227 if (codec.id == payload_type)
228 return &codec;
changbin.shao@webrtc.org2d25b442015-03-16 04:14:34229 }
magjedb05fa242016-11-11 12:00:16230 return nullptr;
changbin.shao@webrtc.org2d25b442015-03-16 04:14:34231}
232
Elad Alonfadb1812019-05-24 11:40:02233bool HasLntf(const Codec& codec);
stefanba4c0e42016-02-04 12:12:24234bool HasNack(const Codec& codec);
235bool HasRemb(const Codec& codec);
Ilya Nikolaevskiy634a7772018-04-04 14:33:49236bool HasRrtr(const Codec& codec);
stefanba4c0e42016-02-04 12:12:24237bool HasTransportCc(const Codec& codec);
magjedf823ede2016-11-12 17:53:04238// Returns the first codec in |supported_codecs| that matches |codec|, or
239// nullptr if no codec matches.
240const VideoCodec* FindMatchingCodec(
241 const std::vector<VideoCodec>& supported_codecs,
242 const VideoCodec& codec);
Mirko Bonadei66e76792019-04-02 09:33:59243RTC_EXPORT bool IsSameCodec(const std::string& name1,
244 const CodecParameterMap& params1,
245 const std::string& name2,
246 const CodecParameterMap& params2);
Shao Changbine62202f2015-04-21 12:24:50247
Johannes Kron3e983682020-03-29 20:17:00248RTC_EXPORT void AddH264ConstrainedBaselineProfileToSupportedFormats(
249 std::vector<webrtc::SdpVideoFormat>* supported_formats);
250
henrike@webrtc.org28e20752013-07-10 00:45:36251} // namespace cricket
252
Mirko Bonadei92ea95e2017-09-15 04:47:31253#endif // MEDIA_BASE_CODEC_H_