Add FuzzyMatchSdpVideoFormat convenience function for VideoEncoderFactoryTemplate.
Bug: webrtc:13573 Change-Id: I6813f2a2524271be7862b700da4831575ec6e206 Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/279701 Commit-Queue: Xavier Lepaul <xalep@webrtc.org> Reviewed-by: Erik Språng <sprang@webrtc.org> Cr-Commit-Position: refs/heads/main@{#38463}
This commit is contained in:
parent
4349137885
commit
99c4c73dbf
@ -75,6 +75,7 @@ rtc_library("video_codecs_api") {
|
||||
"../../api:array_view",
|
||||
"../../modules/video_coding:codec_globals_headers",
|
||||
"../../rtc_base:checks",
|
||||
"../../rtc_base:logging",
|
||||
"../../rtc_base:macromagic",
|
||||
"../../rtc_base:refcount",
|
||||
"../../rtc_base:stringutils",
|
||||
|
||||
@ -11,11 +11,14 @@
|
||||
#include "api/video_codecs/sdp_video_format.h"
|
||||
|
||||
#include "absl/strings/match.h"
|
||||
#include "absl/types/optional.h"
|
||||
#include "api/array_view.h"
|
||||
#include "api/video_codecs/av1_profile.h"
|
||||
#include "api/video_codecs/h264_profile_level_id.h"
|
||||
#include "api/video_codecs/video_codec.h"
|
||||
#include "api/video_codecs/vp9_profile.h"
|
||||
#include "rtc_base/checks.h"
|
||||
#include "rtc_base/logging.h"
|
||||
#include "rtc_base/strings/string_builder.h"
|
||||
|
||||
namespace webrtc {
|
||||
@ -133,4 +136,36 @@ bool operator==(const SdpVideoFormat& a, const SdpVideoFormat& b) {
|
||||
a.scalability_modes == b.scalability_modes;
|
||||
}
|
||||
|
||||
absl::optional<SdpVideoFormat> FuzzyMatchSdpVideoFormat(
|
||||
rtc::ArrayView<const SdpVideoFormat> supported_formats,
|
||||
const SdpVideoFormat& format) {
|
||||
absl::optional<SdpVideoFormat> res;
|
||||
int best_parameter_match = 0;
|
||||
for (const auto& supported_format : supported_formats) {
|
||||
if (absl::EqualsIgnoreCase(supported_format.name, format.name)) {
|
||||
int matching_parameters = 0;
|
||||
for (const auto& kv : supported_format.parameters) {
|
||||
auto it = format.parameters.find(kv.first);
|
||||
if (it != format.parameters.end() && it->second == kv.second) {
|
||||
matching_parameters += 1;
|
||||
}
|
||||
}
|
||||
|
||||
if (!res || matching_parameters > best_parameter_match) {
|
||||
res = supported_format;
|
||||
best_parameter_match = matching_parameters;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!res) {
|
||||
RTC_LOG(LS_INFO) << "Failed to match SdpVideoFormat " << format.ToString();
|
||||
} else if (*res != format) {
|
||||
RTC_LOG(LS_INFO) << "Matched SdpVideoFormat " << format.ToString()
|
||||
<< " with " << res->ToString();
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
} // namespace webrtc
|
||||
|
||||
@ -15,6 +15,7 @@
|
||||
#include <string>
|
||||
|
||||
#include "absl/container/inlined_vector.h"
|
||||
#include "absl/types/optional.h"
|
||||
#include "api/array_view.h"
|
||||
#include "api/video_codecs/scalability_mode.h"
|
||||
#include "rtc_base/system/rtc_export.h"
|
||||
@ -61,6 +62,14 @@ struct RTC_EXPORT SdpVideoFormat {
|
||||
absl::InlinedVector<ScalabilityMode, kScalabilityModeCount> scalability_modes;
|
||||
};
|
||||
|
||||
// For not so good reasons sometimes additional parameters are added to an
|
||||
// SdpVideoFormat, which makes instances that should compare equal to not match
|
||||
// anymore. Until we stop misusing SdpVideoFormats provide this convenience
|
||||
// function to perform fuzzy matching.
|
||||
absl::optional<SdpVideoFormat> FuzzyMatchSdpVideoFormat(
|
||||
rtc::ArrayView<const SdpVideoFormat> supported_formats,
|
||||
const SdpVideoFormat& format);
|
||||
|
||||
} // namespace webrtc
|
||||
|
||||
#endif // API_VIDEO_CODECS_SDP_VIDEO_FORMAT_H_
|
||||
|
||||
@ -38,32 +38,6 @@ using Factory =
|
||||
webrtc::LibaomAv1EncoderTemplateAdapter,
|
||||
#endif
|
||||
webrtc::LibvpxVp9EncoderTemplateAdapter>;
|
||||
|
||||
absl::optional<SdpVideoFormat> MatchOriginalFormat(
|
||||
const SdpVideoFormat& format) {
|
||||
const auto supported_formats = Factory().GetSupportedFormats();
|
||||
|
||||
absl::optional<SdpVideoFormat> res;
|
||||
int best_parameter_match = 0;
|
||||
for (const auto& supported_format : supported_formats) {
|
||||
if (absl::EqualsIgnoreCase(supported_format.name, format.name)) {
|
||||
int matching_parameters = 0;
|
||||
for (const auto& kv : supported_format.parameters) {
|
||||
auto it = format.parameters.find(kv.first);
|
||||
if (it != format.parameters.end() && it->second == kv.second) {
|
||||
matching_parameters += 1;
|
||||
}
|
||||
}
|
||||
|
||||
if (!res || matching_parameters > best_parameter_match) {
|
||||
res = supported_format;
|
||||
best_parameter_match = matching_parameters;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
} // namespace
|
||||
|
||||
std::vector<SdpVideoFormat> InternalEncoderFactory::GetSupportedFormats()
|
||||
@ -73,7 +47,8 @@ std::vector<SdpVideoFormat> InternalEncoderFactory::GetSupportedFormats()
|
||||
|
||||
std::unique_ptr<VideoEncoder> InternalEncoderFactory::CreateVideoEncoder(
|
||||
const SdpVideoFormat& format) {
|
||||
auto original_format = MatchOriginalFormat(format);
|
||||
auto original_format =
|
||||
FuzzyMatchSdpVideoFormat(Factory().GetSupportedFormats(), format);
|
||||
return original_format ? Factory().CreateVideoEncoder(*original_format)
|
||||
: nullptr;
|
||||
}
|
||||
@ -81,7 +56,8 @@ std::unique_ptr<VideoEncoder> InternalEncoderFactory::CreateVideoEncoder(
|
||||
VideoEncoderFactory::CodecSupport InternalEncoderFactory::QueryCodecSupport(
|
||||
const SdpVideoFormat& format,
|
||||
absl::optional<std::string> scalability_mode) const {
|
||||
auto original_format = MatchOriginalFormat(format);
|
||||
auto original_format =
|
||||
FuzzyMatchSdpVideoFormat(Factory().GetSupportedFormats(), format);
|
||||
return original_format
|
||||
? Factory().QueryCodecSupport(*original_format, scalability_mode)
|
||||
: VideoEncoderFactory::CodecSupport{.is_supported = false};
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user