blob: 689c337cedd04c1c3fc1ede71a19804275fc8f9b [file] [log] [blame]
Niels Möllerbe682d42018-03-27 08:31:45 +02001/*
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 Olssona4d87372019-07-05 19:08:33 +020012
Johannes Kron20ee02c2021-04-20 15:53:52 +020013#include "absl/strings/match.h"
14#include "api/video_codecs/h264_profile_level_id.h"
15#include "api/video_codecs/video_codec.h"
16#include "api/video_codecs/vp9_profile.h"
17#include "rtc_base/checks.h"
philipele8ed8302019-07-03 11:53:48 +020018#include "rtc_base/strings/string_builder.h"
Niels Möllerbe682d42018-03-27 08:31:45 +020019
20namespace webrtc {
21
Johannes Kron20ee02c2021-04-20 15:53:52 +020022namespace {
23
24std::string H264GetPacketizationModeOrDefault(
25 const SdpVideoFormat::Parameters& params) {
26 constexpr char kH264FmtpPacketizationMode[] = "packetization-mode";
27 const 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
36bool H264IsSamePacketizationMode(const SdpVideoFormat::Parameters& left,
37 const SdpVideoFormat::Parameters& right) {
38 return H264GetPacketizationModeOrDefault(left) ==
39 H264GetPacketizationModeOrDefault(right);
40}
41
42// Some (video) codecs are actually families of codecs and rely on parameters
43// to distinguish different incompatible family members.
44bool IsSameCodecSpecific(const SdpVideoFormat& format1,
45 const SdpVideoFormat& format2) {
46 // The assumption when calling this function is that the two formats have the
47 // same name.
48 RTC_DCHECK(absl::EqualsIgnoreCase(format1.name, format2.name));
49
50 VideoCodecType codec_type = PayloadStringToCodecType(format1.name);
51 switch (codec_type) {
52 case kVideoCodecH264:
53 return H264IsSameProfile(format1.parameters, format2.parameters) &&
54 H264IsSamePacketizationMode(format1.parameters,
55 format2.parameters);
56 case kVideoCodecVP9:
57 return VP9IsSameProfile(format1.parameters, format2.parameters);
58 default:
59 return true;
60 }
61}
62} // namespace
63
Niels Möllerbe682d42018-03-27 08:31:45 +020064SdpVideoFormat::SdpVideoFormat(const std::string& name) : name(name) {}
65
66SdpVideoFormat::SdpVideoFormat(const std::string& name,
67 const Parameters& parameters)
68 : name(name), parameters(parameters) {}
69
70SdpVideoFormat::SdpVideoFormat(const SdpVideoFormat&) = default;
71SdpVideoFormat::SdpVideoFormat(SdpVideoFormat&&) = default;
Niels Möllerc5d44612018-03-28 09:02:09 +020072SdpVideoFormat& SdpVideoFormat::operator=(const SdpVideoFormat&) = default;
73SdpVideoFormat& SdpVideoFormat::operator=(SdpVideoFormat&&) = default;
Niels Möllerbe682d42018-03-27 08:31:45 +020074
75SdpVideoFormat::~SdpVideoFormat() = default;
76
philipele8ed8302019-07-03 11:53:48 +020077std::string SdpVideoFormat::ToString() const {
78 rtc::StringBuilder builder;
79 builder << "Codec name: " << name << ", parameters: {";
80 for (const auto& kv : parameters)
81 builder << " " << kv.first << "=" << kv.second;
82 builder << " }";
83
84 return builder.str();
85}
86
Johannes Kron20ee02c2021-04-20 15:53:52 +020087bool SdpVideoFormat::IsSameCodec(const SdpVideoFormat& other) const {
88 // Two codecs are considered the same if the name matches (case insensitive)
89 // and certain codec-specific parameters match.
90 return absl::EqualsIgnoreCase(name, other.name) &&
91 IsSameCodecSpecific(*this, other);
92}
93
94bool SdpVideoFormat::IsCodecInList(
95 rtc::ArrayView<const webrtc::SdpVideoFormat> formats) const {
96 for (const auto& format : formats) {
97 if (IsSameCodec(format)) {
98 return true;
99 }
100 }
101 return false;
102}
103
Niels Möllerbe682d42018-03-27 08:31:45 +0200104bool operator==(const SdpVideoFormat& a, const SdpVideoFormat& b) {
105 return a.name == b.name && a.parameters == b.parameters;
106}
107
108} // namespace webrtc