Niels Möller | be682d4 | 2018-03-27 06:31:45 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2018 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 "api/video_codecs/sdp_video_format.h" |
Jonas Olsson | a4d8737 | 2019-07-05 17:08:33 | [diff] [blame] | 12 | |
Johannes Kron | 20ee02c | 2021-04-20 13:53:52 | [diff] [blame] | 13 | #include "absl/strings/match.h" |
philipel | 99c4c73 | 2022-10-18 14:05:38 | [diff] [blame] | 14 | #include "absl/types/optional.h" |
| 15 | #include "api/array_view.h" |
Joe Downing | 87bcc1c | 2022-06-09 22:56:08 | [diff] [blame] | 16 | #include "api/video_codecs/av1_profile.h" |
Johannes Kron | 20ee02c | 2021-04-20 13:53:52 | [diff] [blame] | 17 | #include "api/video_codecs/h264_profile_level_id.h" |
qwu16 | ae82df7 | 2023-09-20 05:10:31 | [diff] [blame] | 18 | #ifdef RTC_ENABLE_H265 |
| 19 | #include "api/video_codecs/h265_profile_tier_level.h" |
| 20 | #endif |
Johannes Kron | 20ee02c | 2021-04-20 13:53:52 | [diff] [blame] | 21 | #include "api/video_codecs/video_codec.h" |
| 22 | #include "api/video_codecs/vp9_profile.h" |
| 23 | #include "rtc_base/checks.h" |
philipel | 99c4c73 | 2022-10-18 14:05:38 | [diff] [blame] | 24 | #include "rtc_base/logging.h" |
philipel | e8ed830 | 2019-07-03 09:53:48 | [diff] [blame] | 25 | #include "rtc_base/strings/string_builder.h" |
Niels Möller | be682d4 | 2018-03-27 06:31:45 | [diff] [blame] | 26 | |
| 27 | namespace webrtc { |
| 28 | |
Johannes Kron | 20ee02c | 2021-04-20 13:53:52 | [diff] [blame] | 29 | namespace { |
| 30 | |
Mirko Bonadei | 6c9c958 | 2023-12-13 16:26:57 | [diff] [blame] | 31 | std::string H264GetPacketizationModeOrDefault( |
| 32 | const SdpVideoFormat::Parameters& params) { |
Johannes Kron | 20ee02c | 2021-04-20 13:53:52 | [diff] [blame] | 33 | constexpr char kH264FmtpPacketizationMode[] = "packetization-mode"; |
| 34 | const auto it = params.find(kH264FmtpPacketizationMode); |
| 35 | if (it != params.end()) { |
| 36 | return it->second; |
| 37 | } |
| 38 | // If packetization-mode is not present, default to "0". |
| 39 | // https://tools.ietf.org/html/rfc6184#section-6.2 |
| 40 | return "0"; |
| 41 | } |
| 42 | |
Mirko Bonadei | 6c9c958 | 2023-12-13 16:26:57 | [diff] [blame] | 43 | bool H264IsSamePacketizationMode(const SdpVideoFormat::Parameters& left, |
| 44 | const SdpVideoFormat::Parameters& right) { |
Johannes Kron | 20ee02c | 2021-04-20 13:53:52 | [diff] [blame] | 45 | return H264GetPacketizationModeOrDefault(left) == |
| 46 | H264GetPacketizationModeOrDefault(right); |
| 47 | } |
| 48 | |
| 49 | // Some (video) codecs are actually families of codecs and rely on parameters |
| 50 | // to distinguish different incompatible family members. |
| 51 | bool IsSameCodecSpecific(const SdpVideoFormat& format1, |
| 52 | const SdpVideoFormat& format2) { |
| 53 | // The assumption when calling this function is that the two formats have the |
| 54 | // same name. |
| 55 | RTC_DCHECK(absl::EqualsIgnoreCase(format1.name, format2.name)); |
| 56 | |
| 57 | VideoCodecType codec_type = PayloadStringToCodecType(format1.name); |
| 58 | switch (codec_type) { |
| 59 | case kVideoCodecH264: |
| 60 | return H264IsSameProfile(format1.parameters, format2.parameters) && |
| 61 | H264IsSamePacketizationMode(format1.parameters, |
| 62 | format2.parameters); |
| 63 | case kVideoCodecVP9: |
| 64 | return VP9IsSameProfile(format1.parameters, format2.parameters); |
Joe Downing | 87bcc1c | 2022-06-09 22:56:08 | [diff] [blame] | 65 | case kVideoCodecAV1: |
| 66 | return AV1IsSameProfile(format1.parameters, format2.parameters); |
qwu16 | ae82df7 | 2023-09-20 05:10:31 | [diff] [blame] | 67 | #ifdef RTC_ENABLE_H265 |
| 68 | case kVideoCodecH265: |
| 69 | return H265IsSameProfileTierLevel(format1.parameters, format2.parameters); |
| 70 | #endif |
Johannes Kron | 20ee02c | 2021-04-20 13:53:52 | [diff] [blame] | 71 | default: |
| 72 | return true; |
| 73 | } |
| 74 | } |
| 75 | } // namespace |
| 76 | |
Niels Möller | be682d4 | 2018-03-27 06:31:45 | [diff] [blame] | 77 | SdpVideoFormat::SdpVideoFormat(const std::string& name) : name(name) {} |
| 78 | |
| 79 | SdpVideoFormat::SdpVideoFormat(const std::string& name, |
Mirko Bonadei | 6c9c958 | 2023-12-13 16:26:57 | [diff] [blame] | 80 | const Parameters& parameters) |
Niels Möller | be682d4 | 2018-03-27 06:31:45 | [diff] [blame] | 81 | : name(name), parameters(parameters) {} |
| 82 | |
Byoungchan Lee | a1a7c63 | 2022-07-05 12:06:28 | [diff] [blame] | 83 | SdpVideoFormat::SdpVideoFormat( |
| 84 | const std::string& name, |
Mirko Bonadei | 6c9c958 | 2023-12-13 16:26:57 | [diff] [blame] | 85 | const Parameters& parameters, |
Byoungchan Lee | a1a7c63 | 2022-07-05 12:06:28 | [diff] [blame] | 86 | const absl::InlinedVector<ScalabilityMode, kScalabilityModeCount>& |
| 87 | scalability_modes) |
| 88 | : name(name), |
| 89 | parameters(parameters), |
| 90 | scalability_modes(scalability_modes) {} |
| 91 | |
Niels Möller | be682d4 | 2018-03-27 06:31:45 | [diff] [blame] | 92 | SdpVideoFormat::SdpVideoFormat(const SdpVideoFormat&) = default; |
| 93 | SdpVideoFormat::SdpVideoFormat(SdpVideoFormat&&) = default; |
Niels Möller | c5d4461 | 2018-03-28 07:02:09 | [diff] [blame] | 94 | SdpVideoFormat& SdpVideoFormat::operator=(const SdpVideoFormat&) = default; |
| 95 | SdpVideoFormat& SdpVideoFormat::operator=(SdpVideoFormat&&) = default; |
Niels Möller | be682d4 | 2018-03-27 06:31:45 | [diff] [blame] | 96 | |
| 97 | SdpVideoFormat::~SdpVideoFormat() = default; |
| 98 | |
philipel | e8ed830 | 2019-07-03 09:53:48 | [diff] [blame] | 99 | std::string SdpVideoFormat::ToString() const { |
| 100 | rtc::StringBuilder builder; |
| 101 | builder << "Codec name: " << name << ", parameters: {"; |
Byoungchan Lee | a1a7c63 | 2022-07-05 12:06:28 | [diff] [blame] | 102 | for (const auto& kv : parameters) { |
philipel | e8ed830 | 2019-07-03 09:53:48 | [diff] [blame] | 103 | builder << " " << kv.first << "=" << kv.second; |
Byoungchan Lee | a1a7c63 | 2022-07-05 12:06:28 | [diff] [blame] | 104 | } |
| 105 | |
philipel | e8ed830 | 2019-07-03 09:53:48 | [diff] [blame] | 106 | builder << " }"; |
Byoungchan Lee | a1a7c63 | 2022-07-05 12:06:28 | [diff] [blame] | 107 | if (!scalability_modes.empty()) { |
| 108 | builder << ", scalability_modes: ["; |
| 109 | bool first = true; |
| 110 | for (const auto scalability_mode : scalability_modes) { |
| 111 | if (first) { |
| 112 | first = false; |
| 113 | } else { |
| 114 | builder << ", "; |
| 115 | } |
| 116 | builder << ScalabilityModeToString(scalability_mode); |
| 117 | } |
| 118 | builder << "]"; |
| 119 | } |
philipel | e8ed830 | 2019-07-03 09:53:48 | [diff] [blame] | 120 | |
| 121 | return builder.str(); |
| 122 | } |
| 123 | |
Johannes Kron | 20ee02c | 2021-04-20 13:53:52 | [diff] [blame] | 124 | bool SdpVideoFormat::IsSameCodec(const SdpVideoFormat& other) const { |
| 125 | // Two codecs are considered the same if the name matches (case insensitive) |
| 126 | // and certain codec-specific parameters match. |
| 127 | return absl::EqualsIgnoreCase(name, other.name) && |
| 128 | IsSameCodecSpecific(*this, other); |
| 129 | } |
| 130 | |
| 131 | bool SdpVideoFormat::IsCodecInList( |
| 132 | rtc::ArrayView<const webrtc::SdpVideoFormat> formats) const { |
| 133 | for (const auto& format : formats) { |
| 134 | if (IsSameCodec(format)) { |
| 135 | return true; |
| 136 | } |
| 137 | } |
| 138 | return false; |
| 139 | } |
| 140 | |
Niels Möller | be682d4 | 2018-03-27 06:31:45 | [diff] [blame] | 141 | bool operator==(const SdpVideoFormat& a, const SdpVideoFormat& b) { |
Byoungchan Lee | a1a7c63 | 2022-07-05 12:06:28 | [diff] [blame] | 142 | return a.name == b.name && a.parameters == b.parameters && |
| 143 | a.scalability_modes == b.scalability_modes; |
Niels Möller | be682d4 | 2018-03-27 06:31:45 | [diff] [blame] | 144 | } |
| 145 | |
philipel | 99c4c73 | 2022-10-18 14:05:38 | [diff] [blame] | 146 | absl::optional<SdpVideoFormat> FuzzyMatchSdpVideoFormat( |
| 147 | rtc::ArrayView<const SdpVideoFormat> supported_formats, |
| 148 | const SdpVideoFormat& format) { |
| 149 | absl::optional<SdpVideoFormat> res; |
| 150 | int best_parameter_match = 0; |
| 151 | for (const auto& supported_format : supported_formats) { |
| 152 | if (absl::EqualsIgnoreCase(supported_format.name, format.name)) { |
| 153 | int matching_parameters = 0; |
| 154 | for (const auto& kv : supported_format.parameters) { |
| 155 | auto it = format.parameters.find(kv.first); |
| 156 | if (it != format.parameters.end() && it->second == kv.second) { |
| 157 | matching_parameters += 1; |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | if (!res || matching_parameters > best_parameter_match) { |
| 162 | res = supported_format; |
| 163 | best_parameter_match = matching_parameters; |
| 164 | } |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | if (!res) { |
| 169 | RTC_LOG(LS_INFO) << "Failed to match SdpVideoFormat " << format.ToString(); |
| 170 | } else if (*res != format) { |
| 171 | RTC_LOG(LS_INFO) << "Matched SdpVideoFormat " << format.ToString() |
| 172 | << " with " << res->ToString(); |
| 173 | } |
| 174 | |
| 175 | return res; |
| 176 | } |
| 177 | |
Niels Möller | be682d4 | 2018-03-27 06:31:45 | [diff] [blame] | 178 | } // namespace webrtc |