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 #include "media/base/codec.h"
12
13 #include "absl/algorithm/container.h"
14 #include "absl/strings/match.h"
15 #include "media/base/h264_profile_level_id.h"
16 #include "media/base/vp9_profile.h"
17 #include "rtc_base/checks.h"
18 #include "rtc_base/logging.h"
19 #include "rtc_base/string_encode.h"
20 #include "rtc_base/strings/string_builder.h"
21
22 namespace cricket {
23 namespace {
24
GetH264PacketizationModeOrDefault(const CodecParameterMap & params)25 std::string GetH264PacketizationModeOrDefault(const CodecParameterMap& params) {
26 auto it = params.find(kH264FmtpPacketizationMode);
27 if (it != params.end()) {
28 return it->second;
29 }
30 // If packetization-mode is not present, default to "0".
31 // https://tools.ietf.org/html/rfc6184#section-6.2
32 return "0";
33 }
34
IsSameH264PacketizationMode(const CodecParameterMap & left,const CodecParameterMap & right)35 bool IsSameH264PacketizationMode(const CodecParameterMap& left,
36 const CodecParameterMap& right) {
37 return GetH264PacketizationModeOrDefault(left) ==
38 GetH264PacketizationModeOrDefault(right);
39 }
40
41 // Some (video) codecs are actually families of codecs and rely on parameters
42 // to distinguish different incompatible family members.
IsSameCodecSpecific(const std::string & name1,const CodecParameterMap & params1,const std::string & name2,const CodecParameterMap & params2)43 bool IsSameCodecSpecific(const std::string& name1,
44 const CodecParameterMap& params1,
45 const std::string& name2,
46 const CodecParameterMap& params2) {
47 // The names might not necessarily match, so check both.
48 auto either_name_matches = [&](const std::string name) {
49 return absl::EqualsIgnoreCase(name, name1) ||
50 absl::EqualsIgnoreCase(name, name2);
51 };
52 if (either_name_matches(kH264CodecName))
53 return webrtc::H264::IsSameH264Profile(params1, params2) &&
54 IsSameH264PacketizationMode(params1, params2);
55 if (either_name_matches(kVp9CodecName))
56 return webrtc::IsSameVP9Profile(params1, params2);
57 return true;
58 }
59
IsCodecInList(const webrtc::SdpVideoFormat & format,const std::vector<webrtc::SdpVideoFormat> & existing_formats)60 bool IsCodecInList(
61 const webrtc::SdpVideoFormat& format,
62 const std::vector<webrtc::SdpVideoFormat>& existing_formats) {
63 for (auto existing_format : existing_formats) {
64 if (IsSameCodec(format.name, format.parameters, existing_format.name,
65 existing_format.parameters)) {
66 return true;
67 }
68 }
69 return false;
70 }
71
72 } // namespace
73
74 FeedbackParams::FeedbackParams() = default;
75 FeedbackParams::~FeedbackParams() = default;
76
operator ==(const FeedbackParam & other) const77 bool FeedbackParam::operator==(const FeedbackParam& other) const {
78 return absl::EqualsIgnoreCase(other.id(), id()) &&
79 absl::EqualsIgnoreCase(other.param(), param());
80 }
81
operator ==(const FeedbackParams & other) const82 bool FeedbackParams::operator==(const FeedbackParams& other) const {
83 return params_ == other.params_;
84 }
85
Has(const FeedbackParam & param) const86 bool FeedbackParams::Has(const FeedbackParam& param) const {
87 return absl::c_linear_search(params_, param);
88 }
89
Add(const FeedbackParam & param)90 void FeedbackParams::Add(const FeedbackParam& param) {
91 if (param.id().empty()) {
92 return;
93 }
94 if (Has(param)) {
95 // Param already in |this|.
96 return;
97 }
98 params_.push_back(param);
99 RTC_CHECK(!HasDuplicateEntries());
100 }
101
Intersect(const FeedbackParams & from)102 void FeedbackParams::Intersect(const FeedbackParams& from) {
103 std::vector<FeedbackParam>::iterator iter_to = params_.begin();
104 while (iter_to != params_.end()) {
105 if (!from.Has(*iter_to)) {
106 iter_to = params_.erase(iter_to);
107 } else {
108 ++iter_to;
109 }
110 }
111 }
112
HasDuplicateEntries() const113 bool FeedbackParams::HasDuplicateEntries() const {
114 for (std::vector<FeedbackParam>::const_iterator iter = params_.begin();
115 iter != params_.end(); ++iter) {
116 for (std::vector<FeedbackParam>::const_iterator found = iter + 1;
117 found != params_.end(); ++found) {
118 if (*found == *iter) {
119 return true;
120 }
121 }
122 }
123 return false;
124 }
125
Codec(int id,const std::string & name,int clockrate)126 Codec::Codec(int id, const std::string& name, int clockrate)
127 : id(id), name(name), clockrate(clockrate) {}
128
Codec()129 Codec::Codec() : id(0), clockrate(0) {}
130
131 Codec::Codec(const Codec& c) = default;
132 Codec::Codec(Codec&& c) = default;
133 Codec::~Codec() = default;
134 Codec& Codec::operator=(const Codec& c) = default;
135 Codec& Codec::operator=(Codec&& c) = default;
136
operator ==(const Codec & c) const137 bool Codec::operator==(const Codec& c) const {
138 return this->id == c.id && // id is reserved in objective-c
139 name == c.name && clockrate == c.clockrate && params == c.params &&
140 feedback_params == c.feedback_params;
141 }
142
Matches(const Codec & codec) const143 bool Codec::Matches(const Codec& codec) const {
144 // Match the codec id/name based on the typical static/dynamic name rules.
145 // Matching is case-insensitive.
146 const int kMaxStaticPayloadId = 95;
147 return (id <= kMaxStaticPayloadId || codec.id <= kMaxStaticPayloadId)
148 ? (id == codec.id)
149 : (absl::EqualsIgnoreCase(name, codec.name));
150 }
151
MatchesCapability(const webrtc::RtpCodecCapability & codec_capability) const152 bool Codec::MatchesCapability(
153 const webrtc::RtpCodecCapability& codec_capability) const {
154 webrtc::RtpCodecParameters codec_parameters = ToCodecParameters();
155
156 return codec_parameters.name == codec_capability.name &&
157 codec_parameters.kind == codec_capability.kind &&
158 (codec_parameters.name == cricket::kRtxCodecName ||
159 (codec_parameters.num_channels == codec_capability.num_channels &&
160 codec_parameters.clock_rate == codec_capability.clock_rate &&
161 codec_parameters.parameters == codec_capability.parameters));
162 }
163
GetParam(const std::string & name,std::string * out) const164 bool Codec::GetParam(const std::string& name, std::string* out) const {
165 CodecParameterMap::const_iterator iter = params.find(name);
166 if (iter == params.end())
167 return false;
168 *out = iter->second;
169 return true;
170 }
171
GetParam(const std::string & name,int * out) const172 bool Codec::GetParam(const std::string& name, int* out) const {
173 CodecParameterMap::const_iterator iter = params.find(name);
174 if (iter == params.end())
175 return false;
176 return rtc::FromString(iter->second, out);
177 }
178
SetParam(const std::string & name,const std::string & value)179 void Codec::SetParam(const std::string& name, const std::string& value) {
180 params[name] = value;
181 }
182
SetParam(const std::string & name,int value)183 void Codec::SetParam(const std::string& name, int value) {
184 params[name] = rtc::ToString(value);
185 }
186
RemoveParam(const std::string & name)187 bool Codec::RemoveParam(const std::string& name) {
188 return params.erase(name) == 1;
189 }
190
AddFeedbackParam(const FeedbackParam & param)191 void Codec::AddFeedbackParam(const FeedbackParam& param) {
192 feedback_params.Add(param);
193 }
194
HasFeedbackParam(const FeedbackParam & param) const195 bool Codec::HasFeedbackParam(const FeedbackParam& param) const {
196 return feedback_params.Has(param);
197 }
198
IntersectFeedbackParams(const Codec & other)199 void Codec::IntersectFeedbackParams(const Codec& other) {
200 feedback_params.Intersect(other.feedback_params);
201 }
202
ToCodecParameters() const203 webrtc::RtpCodecParameters Codec::ToCodecParameters() const {
204 webrtc::RtpCodecParameters codec_params;
205 codec_params.payload_type = id;
206 codec_params.name = name;
207 codec_params.clock_rate = clockrate;
208 codec_params.parameters.insert(params.begin(), params.end());
209 return codec_params;
210 }
211
AudioCodec(int id,const std::string & name,int clockrate,int bitrate,size_t channels)212 AudioCodec::AudioCodec(int id,
213 const std::string& name,
214 int clockrate,
215 int bitrate,
216 size_t channels)
217 : Codec(id, name, clockrate), bitrate(bitrate), channels(channels) {}
218
AudioCodec()219 AudioCodec::AudioCodec() : Codec(), bitrate(0), channels(0) {}
220
221 AudioCodec::AudioCodec(const AudioCodec& c) = default;
222 AudioCodec::AudioCodec(AudioCodec&& c) = default;
223 AudioCodec& AudioCodec::operator=(const AudioCodec& c) = default;
224 AudioCodec& AudioCodec::operator=(AudioCodec&& c) = default;
225
operator ==(const AudioCodec & c) const226 bool AudioCodec::operator==(const AudioCodec& c) const {
227 return bitrate == c.bitrate && channels == c.channels && Codec::operator==(c);
228 }
229
Matches(const AudioCodec & codec) const230 bool AudioCodec::Matches(const AudioCodec& codec) const {
231 // If a nonzero clockrate is specified, it must match the actual clockrate.
232 // If a nonzero bitrate is specified, it must match the actual bitrate,
233 // unless the codec is VBR (0), where we just force the supplied value.
234 // The number of channels must match exactly, with the exception
235 // that channels=0 is treated synonymously as channels=1, per RFC
236 // 4566 section 6: " [The channels] parameter is OPTIONAL and may be
237 // omitted if the number of channels is one."
238 // Preference is ignored.
239 // TODO(juberti): Treat a zero clockrate as 8000Hz, the RTP default clockrate.
240 return Codec::Matches(codec) &&
241 ((codec.clockrate == 0 /*&& clockrate == 8000*/) ||
242 clockrate == codec.clockrate) &&
243 (codec.bitrate == 0 || bitrate <= 0 || bitrate == codec.bitrate) &&
244 ((codec.channels < 2 && channels < 2) || channels == codec.channels);
245 }
246
ToString() const247 std::string AudioCodec::ToString() const {
248 char buf[256];
249 rtc::SimpleStringBuilder sb(buf);
250 sb << "AudioCodec[" << id << ":" << name << ":" << clockrate << ":" << bitrate
251 << ":" << channels << "]";
252 return sb.str();
253 }
254
ToCodecParameters() const255 webrtc::RtpCodecParameters AudioCodec::ToCodecParameters() const {
256 webrtc::RtpCodecParameters codec_params = Codec::ToCodecParameters();
257 codec_params.num_channels = static_cast<int>(channels);
258 codec_params.kind = MEDIA_TYPE_AUDIO;
259 return codec_params;
260 }
261
ToString() const262 std::string VideoCodec::ToString() const {
263 char buf[256];
264 rtc::SimpleStringBuilder sb(buf);
265 sb << "VideoCodec[" << id << ":" << name << "]";
266 return sb.str();
267 }
268
ToCodecParameters() const269 webrtc::RtpCodecParameters VideoCodec::ToCodecParameters() const {
270 webrtc::RtpCodecParameters codec_params = Codec::ToCodecParameters();
271 codec_params.kind = MEDIA_TYPE_VIDEO;
272 return codec_params;
273 }
274
VideoCodec(int id,const std::string & name)275 VideoCodec::VideoCodec(int id, const std::string& name)
276 : Codec(id, name, kVideoCodecClockrate) {
277 SetDefaultParameters();
278 }
279
VideoCodec(const std::string & name)280 VideoCodec::VideoCodec(const std::string& name) : VideoCodec(0 /* id */, name) {
281 SetDefaultParameters();
282 }
283
VideoCodec()284 VideoCodec::VideoCodec() : Codec() {
285 clockrate = kVideoCodecClockrate;
286 }
287
VideoCodec(const webrtc::SdpVideoFormat & c)288 VideoCodec::VideoCodec(const webrtc::SdpVideoFormat& c)
289 : Codec(0 /* id */, c.name, kVideoCodecClockrate) {
290 params = c.parameters;
291 }
292
293 VideoCodec::VideoCodec(const VideoCodec& c) = default;
294 VideoCodec::VideoCodec(VideoCodec&& c) = default;
295 VideoCodec& VideoCodec::operator=(const VideoCodec& c) = default;
296 VideoCodec& VideoCodec::operator=(VideoCodec&& c) = default;
297
SetDefaultParameters()298 void VideoCodec::SetDefaultParameters() {
299 if (absl::EqualsIgnoreCase(kH264CodecName, name)) {
300 // This default is set for all H.264 codecs created because
301 // that was the default before packetization mode support was added.
302 // TODO(hta): Move this to the places that create VideoCodecs from
303 // SDP or from knowledge of implementation capabilities.
304 SetParam(kH264FmtpPacketizationMode, "1");
305 }
306 }
307
operator ==(const VideoCodec & c) const308 bool VideoCodec::operator==(const VideoCodec& c) const {
309 return Codec::operator==(c) && packetization == c.packetization;
310 }
311
Matches(const VideoCodec & other) const312 bool VideoCodec::Matches(const VideoCodec& other) const {
313 return Codec::Matches(other) &&
314 IsSameCodecSpecific(name, params, other.name, other.params);
315 }
316
IntersectPacketization(const VideoCodec & local_codec,const VideoCodec & remote_codec)317 absl::optional<std::string> VideoCodec::IntersectPacketization(
318 const VideoCodec& local_codec,
319 const VideoCodec& remote_codec) {
320 if (local_codec.packetization == remote_codec.packetization) {
321 return local_codec.packetization;
322 }
323 return absl::nullopt;
324 }
325
CreateRtxCodec(int rtx_payload_type,int associated_payload_type)326 VideoCodec VideoCodec::CreateRtxCodec(int rtx_payload_type,
327 int associated_payload_type) {
328 VideoCodec rtx_codec(rtx_payload_type, kRtxCodecName);
329 rtx_codec.SetParam(kCodecParamAssociatedPayloadType, associated_payload_type);
330 return rtx_codec;
331 }
332
GetCodecType() const333 VideoCodec::CodecType VideoCodec::GetCodecType() const {
334 if (absl::EqualsIgnoreCase(name, kRedCodecName)) {
335 return CODEC_RED;
336 }
337 if (absl::EqualsIgnoreCase(name, kUlpfecCodecName)) {
338 return CODEC_ULPFEC;
339 }
340 if (absl::EqualsIgnoreCase(name, kFlexfecCodecName)) {
341 return CODEC_FLEXFEC;
342 }
343 if (absl::EqualsIgnoreCase(name, kRtxCodecName)) {
344 return CODEC_RTX;
345 }
346
347 return CODEC_VIDEO;
348 }
349
ValidateCodecFormat() const350 bool VideoCodec::ValidateCodecFormat() const {
351 if (id < 0 || id > 127) {
352 RTC_LOG(LS_ERROR) << "Codec with invalid payload type: " << ToString();
353 return false;
354 }
355 if (GetCodecType() != CODEC_VIDEO) {
356 return true;
357 }
358
359 // Video validation from here on.
360 int min_bitrate = -1;
361 int max_bitrate = -1;
362 if (GetParam(kCodecParamMinBitrate, &min_bitrate) &&
363 GetParam(kCodecParamMaxBitrate, &max_bitrate)) {
364 if (max_bitrate < min_bitrate) {
365 RTC_LOG(LS_ERROR) << "Codec with max < min bitrate: " << ToString();
366 return false;
367 }
368 }
369 return true;
370 }
371
RtpDataCodec(int id,const std::string & name)372 RtpDataCodec::RtpDataCodec(int id, const std::string& name)
373 : Codec(id, name, kDataCodecClockrate) {}
374
RtpDataCodec()375 RtpDataCodec::RtpDataCodec() : Codec() {
376 clockrate = kDataCodecClockrate;
377 }
378
379 RtpDataCodec::RtpDataCodec(const RtpDataCodec& c) = default;
380 RtpDataCodec::RtpDataCodec(RtpDataCodec&& c) = default;
381 RtpDataCodec& RtpDataCodec::operator=(const RtpDataCodec& c) = default;
382 RtpDataCodec& RtpDataCodec::operator=(RtpDataCodec&& c) = default;
383
ToString() const384 std::string RtpDataCodec::ToString() const {
385 char buf[256];
386 rtc::SimpleStringBuilder sb(buf);
387 sb << "RtpDataCodec[" << id << ":" << name << "]";
388 return sb.str();
389 }
390
HasLntf(const Codec & codec)391 bool HasLntf(const Codec& codec) {
392 return codec.HasFeedbackParam(
393 FeedbackParam(kRtcpFbParamLntf, kParamValueEmpty));
394 }
395
HasNack(const Codec & codec)396 bool HasNack(const Codec& codec) {
397 return codec.HasFeedbackParam(
398 FeedbackParam(kRtcpFbParamNack, kParamValueEmpty));
399 }
400
HasRemb(const Codec & codec)401 bool HasRemb(const Codec& codec) {
402 return codec.HasFeedbackParam(
403 FeedbackParam(kRtcpFbParamRemb, kParamValueEmpty));
404 }
405
HasRrtr(const Codec & codec)406 bool HasRrtr(const Codec& codec) {
407 return codec.HasFeedbackParam(
408 FeedbackParam(kRtcpFbParamRrtr, kParamValueEmpty));
409 }
410
HasTransportCc(const Codec & codec)411 bool HasTransportCc(const Codec& codec) {
412 return codec.HasFeedbackParam(
413 FeedbackParam(kRtcpFbParamTransportCc, kParamValueEmpty));
414 }
415
FindMatchingCodec(const std::vector<VideoCodec> & supported_codecs,const VideoCodec & codec)416 const VideoCodec* FindMatchingCodec(
417 const std::vector<VideoCodec>& supported_codecs,
418 const VideoCodec& codec) {
419 for (const VideoCodec& supported_codec : supported_codecs) {
420 if (IsSameCodec(codec.name, codec.params, supported_codec.name,
421 supported_codec.params)) {
422 return &supported_codec;
423 }
424 }
425 return nullptr;
426 }
427
IsSameCodec(const std::string & name1,const CodecParameterMap & params1,const std::string & name2,const CodecParameterMap & params2)428 bool IsSameCodec(const std::string& name1,
429 const CodecParameterMap& params1,
430 const std::string& name2,
431 const CodecParameterMap& params2) {
432 // Two codecs are considered the same if the name matches (case insensitive)
433 // and certain codec-specific parameters match.
434 return absl::EqualsIgnoreCase(name1, name2) &&
435 IsSameCodecSpecific(name1, params1, name2, params2);
436 }
437
438 // If a decoder supports any H264 profile, it is implicitly assumed to also
439 // support constrained base line even though it's not explicitly listed.
AddH264ConstrainedBaselineProfileToSupportedFormats(std::vector<webrtc::SdpVideoFormat> * supported_formats)440 void AddH264ConstrainedBaselineProfileToSupportedFormats(
441 std::vector<webrtc::SdpVideoFormat>* supported_formats) {
442 std::vector<webrtc::SdpVideoFormat> cbr_supported_formats;
443
444 // For any H264 supported profile, add the corresponding constrained baseline
445 // profile.
446 for (auto it = supported_formats->cbegin(); it != supported_formats->cend();
447 ++it) {
448 if (it->name == cricket::kH264CodecName) {
449 const absl::optional<webrtc::H264::ProfileLevelId> profile_level_id =
450 webrtc::H264::ParseSdpProfileLevelId(it->parameters);
451 if (profile_level_id && profile_level_id->profile !=
452 webrtc::H264::kProfileConstrainedBaseline) {
453 webrtc::SdpVideoFormat cbp_format = *it;
454 webrtc::H264::ProfileLevelId cbp_profile = *profile_level_id;
455 cbp_profile.profile = webrtc::H264::kProfileConstrainedBaseline;
456 cbp_format.parameters[cricket::kH264FmtpProfileLevelId] =
457 *webrtc::H264::ProfileLevelIdToString(cbp_profile);
458 cbr_supported_formats.push_back(cbp_format);
459 }
460 }
461 }
462
463 size_t original_size = supported_formats->size();
464 // ...if it's not already in the list.
465 std::copy_if(cbr_supported_formats.begin(), cbr_supported_formats.end(),
466 std::back_inserter(*supported_formats),
467 [supported_formats](const webrtc::SdpVideoFormat& format) {
468 return !IsCodecInList(format, *supported_formats);
469 });
470
471 if (supported_formats->size() > original_size) {
472 RTC_LOG(LS_WARNING) << "Explicitly added H264 constrained baseline to list "
473 "of supported formats.";
474 }
475 }
476
477 } // namespace cricket
478