Niels Möller | be682d4 | 2018-03-27 08:31:45 +0200 | [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 19:08:33 +0200 | [diff] [blame] | 12 | |
Johannes Kron | 20ee02c | 2021-04-20 15:53:52 +0200 | [diff] [blame] | 13 | #include "absl/strings/match.h" |
Joe Downing | 87bcc1c | 2022-06-09 15:56:08 -0700 | [diff] [blame] | 14 | #include "api/video_codecs/av1_profile.h" |
Johannes Kron | 20ee02c | 2021-04-20 15:53:52 +0200 | [diff] [blame] | 15 | #include "api/video_codecs/h264_profile_level_id.h" |
| 16 | #include "api/video_codecs/video_codec.h" |
| 17 | #include "api/video_codecs/vp9_profile.h" |
| 18 | #include "rtc_base/checks.h" |
philipel | e8ed830 | 2019-07-03 11:53:48 +0200 | [diff] [blame] | 19 | #include "rtc_base/strings/string_builder.h" |
Niels Möller | be682d4 | 2018-03-27 08:31:45 +0200 | [diff] [blame] | 20 | |
| 21 | namespace webrtc { |
| 22 | |
Johannes Kron | 20ee02c | 2021-04-20 15:53:52 +0200 | [diff] [blame] | 23 | namespace { |
| 24 | |
| 25 | std::string H264GetPacketizationModeOrDefault( |
| 26 | const SdpVideoFormat::Parameters& params) { |
| 27 | constexpr char kH264FmtpPacketizationMode[] = "packetization-mode"; |
| 28 | const auto it = params.find(kH264FmtpPacketizationMode); |
| 29 | if (it != params.end()) { |
| 30 | return it->second; |
| 31 | } |
| 32 | // If packetization-mode is not present, default to "0". |
| 33 | // https://tools.ietf.org/html/rfc6184#section-6.2 |
| 34 | return "0"; |
| 35 | } |
| 36 | |
| 37 | bool H264IsSamePacketizationMode(const SdpVideoFormat::Parameters& left, |
| 38 | const SdpVideoFormat::Parameters& right) { |
| 39 | return H264GetPacketizationModeOrDefault(left) == |
| 40 | H264GetPacketizationModeOrDefault(right); |
| 41 | } |
| 42 | |
| 43 | // Some (video) codecs are actually families of codecs and rely on parameters |
| 44 | // to distinguish different incompatible family members. |
| 45 | bool IsSameCodecSpecific(const SdpVideoFormat& format1, |
| 46 | const SdpVideoFormat& format2) { |
| 47 | // The assumption when calling this function is that the two formats have the |
| 48 | // same name. |
| 49 | RTC_DCHECK(absl::EqualsIgnoreCase(format1.name, format2.name)); |
| 50 | |
| 51 | VideoCodecType codec_type = PayloadStringToCodecType(format1.name); |
| 52 | switch (codec_type) { |
| 53 | case kVideoCodecH264: |
| 54 | return H264IsSameProfile(format1.parameters, format2.parameters) && |
| 55 | H264IsSamePacketizationMode(format1.parameters, |
| 56 | format2.parameters); |
| 57 | case kVideoCodecVP9: |
| 58 | return VP9IsSameProfile(format1.parameters, format2.parameters); |
Joe Downing | 87bcc1c | 2022-06-09 15:56:08 -0700 | [diff] [blame] | 59 | case kVideoCodecAV1: |
| 60 | return AV1IsSameProfile(format1.parameters, format2.parameters); |
Johannes Kron | 20ee02c | 2021-04-20 15:53:52 +0200 | [diff] [blame] | 61 | default: |
| 62 | return true; |
| 63 | } |
| 64 | } |
| 65 | } // namespace |
| 66 | |
Niels Möller | be682d4 | 2018-03-27 08:31:45 +0200 | [diff] [blame] | 67 | SdpVideoFormat::SdpVideoFormat(const std::string& name) : name(name) {} |
| 68 | |
| 69 | SdpVideoFormat::SdpVideoFormat(const std::string& name, |
| 70 | const Parameters& parameters) |
| 71 | : name(name), parameters(parameters) {} |
| 72 | |
Byoungchan Lee | a1a7c63 | 2022-07-05 21:06:28 +0900 | [diff] [blame^] | 73 | SdpVideoFormat::SdpVideoFormat( |
| 74 | const std::string& name, |
| 75 | const Parameters& parameters, |
| 76 | const absl::InlinedVector<ScalabilityMode, kScalabilityModeCount>& |
| 77 | scalability_modes) |
| 78 | : name(name), |
| 79 | parameters(parameters), |
| 80 | scalability_modes(scalability_modes) {} |
| 81 | |
Niels Möller | be682d4 | 2018-03-27 08:31:45 +0200 | [diff] [blame] | 82 | SdpVideoFormat::SdpVideoFormat(const SdpVideoFormat&) = default; |
| 83 | SdpVideoFormat::SdpVideoFormat(SdpVideoFormat&&) = default; |
Niels Möller | c5d4461 | 2018-03-28 09:02:09 +0200 | [diff] [blame] | 84 | SdpVideoFormat& SdpVideoFormat::operator=(const SdpVideoFormat&) = default; |
| 85 | SdpVideoFormat& SdpVideoFormat::operator=(SdpVideoFormat&&) = default; |
Niels Möller | be682d4 | 2018-03-27 08:31:45 +0200 | [diff] [blame] | 86 | |
| 87 | SdpVideoFormat::~SdpVideoFormat() = default; |
| 88 | |
philipel | e8ed830 | 2019-07-03 11:53:48 +0200 | [diff] [blame] | 89 | std::string SdpVideoFormat::ToString() const { |
| 90 | rtc::StringBuilder builder; |
| 91 | builder << "Codec name: " << name << ", parameters: {"; |
Byoungchan Lee | a1a7c63 | 2022-07-05 21:06:28 +0900 | [diff] [blame^] | 92 | for (const auto& kv : parameters) { |
philipel | e8ed830 | 2019-07-03 11:53:48 +0200 | [diff] [blame] | 93 | builder << " " << kv.first << "=" << kv.second; |
Byoungchan Lee | a1a7c63 | 2022-07-05 21:06:28 +0900 | [diff] [blame^] | 94 | } |
| 95 | |
philipel | e8ed830 | 2019-07-03 11:53:48 +0200 | [diff] [blame] | 96 | builder << " }"; |
Byoungchan Lee | a1a7c63 | 2022-07-05 21:06:28 +0900 | [diff] [blame^] | 97 | if (!scalability_modes.empty()) { |
| 98 | builder << ", scalability_modes: ["; |
| 99 | bool first = true; |
| 100 | for (const auto scalability_mode : scalability_modes) { |
| 101 | if (first) { |
| 102 | first = false; |
| 103 | } else { |
| 104 | builder << ", "; |
| 105 | } |
| 106 | builder << ScalabilityModeToString(scalability_mode); |
| 107 | } |
| 108 | builder << "]"; |
| 109 | } |
philipel | e8ed830 | 2019-07-03 11:53:48 +0200 | [diff] [blame] | 110 | |
| 111 | return builder.str(); |
| 112 | } |
| 113 | |
Johannes Kron | 20ee02c | 2021-04-20 15:53:52 +0200 | [diff] [blame] | 114 | bool SdpVideoFormat::IsSameCodec(const SdpVideoFormat& other) const { |
| 115 | // Two codecs are considered the same if the name matches (case insensitive) |
| 116 | // and certain codec-specific parameters match. |
| 117 | return absl::EqualsIgnoreCase(name, other.name) && |
| 118 | IsSameCodecSpecific(*this, other); |
| 119 | } |
| 120 | |
| 121 | bool SdpVideoFormat::IsCodecInList( |
| 122 | rtc::ArrayView<const webrtc::SdpVideoFormat> formats) const { |
| 123 | for (const auto& format : formats) { |
| 124 | if (IsSameCodec(format)) { |
| 125 | return true; |
| 126 | } |
| 127 | } |
| 128 | return false; |
| 129 | } |
| 130 | |
Niels Möller | be682d4 | 2018-03-27 08:31:45 +0200 | [diff] [blame] | 131 | bool operator==(const SdpVideoFormat& a, const SdpVideoFormat& b) { |
Byoungchan Lee | a1a7c63 | 2022-07-05 21:06:28 +0900 | [diff] [blame^] | 132 | return a.name == b.name && a.parameters == b.parameters && |
| 133 | a.scalability_modes == b.scalability_modes; |
Niels Möller | be682d4 | 2018-03-27 08:31:45 +0200 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | } // namespace webrtc |