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