1 /*
2 * Copyright (c) 2004 The WebRTC project authors. All Rights Reserved.
3 *
4 * 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.
9 */
10
11 #ifndef MEDIA_BASE_CODEC_H_
12 #define MEDIA_BASE_CODEC_H_
13
14 #include <map>
15 #include <set>
16 #include <string>
17 #include <vector>
18
19 #include "absl/types/optional.h"
20 #include "api/rtp_parameters.h"
21 #include "api/video_codecs/sdp_video_format.h"
22 #include "media/base/media_constants.h"
23 #include "rtc_base/system/rtc_export.h"
24
25 namespace cricket {
26
27 typedef std::map<std::string, std::string> CodecParameterMap;
28
29 class FeedbackParam {
30 public:
31 FeedbackParam() = default;
FeedbackParam(const std::string & id,const std::string & param)32 FeedbackParam(const std::string& id, const std::string& param)
33 : id_(id), param_(param) {}
FeedbackParam(const std::string & id)34 explicit FeedbackParam(const std::string& id)
35 : id_(id), param_(kParamValueEmpty) {}
36
37 bool operator==(const FeedbackParam& other) const;
38
id()39 const std::string& id() const { return id_; }
param()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
47 class FeedbackParams {
48 public:
49 FeedbackParams();
50 ~FeedbackParams();
51 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
params()58 const std::vector<FeedbackParam>& params() const { return params_; }
59
60 private:
61 bool HasDuplicateEntries() const;
62
63 std::vector<FeedbackParam> params_;
64 };
65
66 struct RTC_EXPORT Codec {
67 int id;
68 std::string name;
69 int clockrate;
70 // Non key-value parameters such as the telephone-event "0‐15" are
71 // represented using an empty string as key, i.e. {"": "0-15"}.
72 CodecParameterMap params;
73 FeedbackParams feedback_params;
74
75 virtual ~Codec();
76
77 // Indicates if this codec is compatible with the specified codec.
78 bool Matches(const Codec& codec) const;
79 bool MatchesCapability(const webrtc::RtpCodecCapability& capability) 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
88 // 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
92 bool HasFeedbackParam(const FeedbackParam& param) const;
93 void AddFeedbackParam(const FeedbackParam& param);
94
95 // Filter |this| feedbacks params such that only those shared by both |this|
96 // and |other| are kept.
97 void IntersectFeedbackParams(const Codec& other);
98
99 virtual webrtc::RtpCodecParameters ToCodecParameters() const;
100
101 Codec& operator=(const Codec& c);
102 Codec& operator=(Codec&& c);
103
104 bool operator==(const Codec& c) const;
105
106 bool operator!=(const Codec& c) const { return !(*this == c); }
107
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);
116 };
117
118 struct AudioCodec : public Codec {
119 int bitrate;
120 size_t channels;
121
122 // Creates a codec with the given parameters.
123 AudioCodec(int id,
124 const std::string& name,
125 int clockrate,
126 int bitrate,
127 size_t channels);
128 // Creates an empty codec.
129 AudioCodec();
130 AudioCodec(const AudioCodec& c);
131 AudioCodec(AudioCodec&& c);
132 ~AudioCodec() override = default;
133
134 // Indicates if this codec is compatible with the specified codec.
135 bool Matches(const AudioCodec& codec) const;
136
137 std::string ToString() const;
138
139 webrtc::RtpCodecParameters ToCodecParameters() const override;
140
141 AudioCodec& operator=(const AudioCodec& c);
142 AudioCodec& operator=(AudioCodec&& c);
143
144 bool operator==(const AudioCodec& c) const;
145
146 bool operator!=(const AudioCodec& c) const { return !(*this == c); }
147 };
148
149 struct RTC_EXPORT VideoCodec : public Codec {
150 absl::optional<std::string> packetization;
151
152 // Creates a codec with the given parameters.
153 VideoCodec(int id, const std::string& name);
154 // Creates a codec with the given name and empty id.
155 explicit VideoCodec(const std::string& name);
156 // Creates an empty codec.
157 VideoCodec();
158 VideoCodec(const VideoCodec& c);
159 explicit VideoCodec(const webrtc::SdpVideoFormat& c);
160 VideoCodec(VideoCodec&& c);
161 ~VideoCodec() override = default;
162
163 // 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
168 std::string ToString() const;
169
170 webrtc::RtpCodecParameters ToCodecParameters() const override;
171
172 VideoCodec& operator=(const VideoCodec& c);
173 VideoCodec& operator=(VideoCodec&& c);
174
175 bool operator==(const VideoCodec& c) const;
176
177 bool operator!=(const VideoCodec& c) const { return !(*this == c); }
178
179 // 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
184 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,
191 CODEC_FLEXFEC,
192 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;
200
201 private:
202 void SetDefaultParameters();
203 };
204
205 struct 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;
211
212 RtpDataCodec& operator=(const RtpDataCodec& c);
213 RtpDataCodec& operator=(RtpDataCodec&& c);
214
215 std::string ToString() const;
216 };
217
218 // For backwards compatibility
219 // TODO(bugs.webrtc.org/10597): Remove when no longer needed.
220 typedef RtpDataCodec DataCodec;
221
222 // Get the codec setting associated with |payload_type|. If there
223 // is no codec associated with that payload type it returns nullptr.
224 template <class Codec>
FindCodecById(const std::vector<Codec> & codecs,int payload_type)225 const Codec* FindCodecById(const std::vector<Codec>& codecs, int payload_type) {
226 for (const auto& codec : codecs) {
227 if (codec.id == payload_type)
228 return &codec;
229 }
230 return nullptr;
231 }
232
233 bool HasLntf(const Codec& codec);
234 bool HasNack(const Codec& codec);
235 bool HasRemb(const Codec& codec);
236 bool HasRrtr(const Codec& codec);
237 bool HasTransportCc(const Codec& codec);
238 // Returns the first codec in |supported_codecs| that matches |codec|, or
239 // nullptr if no codec matches.
240 const VideoCodec* FindMatchingCodec(
241 const std::vector<VideoCodec>& supported_codecs,
242 const VideoCodec& codec);
243 RTC_EXPORT bool IsSameCodec(const std::string& name1,
244 const CodecParameterMap& params1,
245 const std::string& name2,
246 const CodecParameterMap& params2);
247
248 RTC_EXPORT void AddH264ConstrainedBaselineProfileToSupportedFormats(
249 std::vector<webrtc::SdpVideoFormat>* supported_formats);
250
251 } // namespace cricket
252
253 #endif // MEDIA_BASE_CODEC_H_
254