From 0ebf5bdf043a27fd3dfb7f92e0cb63d88954c44d Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Fri, 19 Apr 2024 03:47:29 +0200 Subject: Adding upstream version 115.8.0esr. Signed-off-by: Daniel Baumann --- third_party/libwebrtc/stats/rtc_stats.cc | 374 +++++++++++++++++++++++++++++++ 1 file changed, 374 insertions(+) create mode 100644 third_party/libwebrtc/stats/rtc_stats.cc (limited to 'third_party/libwebrtc/stats/rtc_stats.cc') diff --git a/third_party/libwebrtc/stats/rtc_stats.cc b/third_party/libwebrtc/stats/rtc_stats.cc new file mode 100644 index 0000000000..421d9690d7 --- /dev/null +++ b/third_party/libwebrtc/stats/rtc_stats.cc @@ -0,0 +1,374 @@ +/* + * Copyright 2016 The WebRTC Project Authors. All rights reserved. + * + * Use of this source code is governed by a BSD-style license + * that can be found in the LICENSE file in the root of the source + * tree. An additional intellectual property rights grant can be found + * in the file PATENTS. All contributing project authors may + * be found in the AUTHORS file in the root of the source tree. + */ + +#include "api/stats/rtc_stats.h" + +#include + +#include "rtc_base/arraysize.h" +#include "rtc_base/string_encode.h" +#include "rtc_base/strings/string_builder.h" + +namespace webrtc { + +namespace { + +// Produces "[a,b,c]". Works for non-vector `RTCStatsMemberInterface::Type` +// types. +template +std::string VectorToString(const std::vector& vector) { + rtc::StringBuilder sb; + sb << "["; + const char* separator = ""; + for (const T& element : vector) { + sb << separator << rtc::ToString(element); + separator = ","; + } + sb << "]"; + return sb.Release(); +} + +// This overload is required because std::vector range loops don't +// return references but objects, causing -Wrange-loop-analysis diagnostics. +std::string VectorToString(const std::vector& vector) { + rtc::StringBuilder sb; + sb << "["; + const char* separator = ""; + for (bool element : vector) { + sb << separator << rtc::ToString(element); + separator = ","; + } + sb << "]"; + return sb.Release(); +} + +// Produces "[\"a\",\"b\",\"c\"]". Works for vectors of both const char* and +// std::string element types. +template +std::string VectorOfStringsToString(const std::vector& strings) { + rtc::StringBuilder sb; + sb << "["; + const char* separator = ""; + for (const T& element : strings) { + sb << separator << "\"" << rtc::ToString(element) << "\""; + separator = ","; + } + sb << "]"; + return sb.Release(); +} + +template +std::string MapToString(const std::map& map) { + rtc::StringBuilder sb; + sb << "{"; + const char* separator = ""; + for (const auto& element : map) { + sb << separator << rtc::ToString(element.first) << ":" + << rtc::ToString(element.second); + separator = ","; + } + sb << "}"; + return sb.Release(); +} + +template +std::string ToStringAsDouble(const T value) { + // JSON represents numbers as floating point numbers with about 15 decimal + // digits of precision. + char buf[32]; + const int len = std::snprintf(&buf[0], arraysize(buf), "%.16g", + static_cast(value)); + RTC_DCHECK_LE(len, arraysize(buf)); + return std::string(&buf[0], len); +} + +template +std::string VectorToStringAsDouble(const std::vector& vector) { + rtc::StringBuilder sb; + sb << "["; + const char* separator = ""; + for (const T& element : vector) { + sb << separator << ToStringAsDouble(element); + separator = ","; + } + sb << "]"; + return sb.Release(); +} + +template +std::string MapToStringAsDouble(const std::map& map) { + rtc::StringBuilder sb; + sb << "{"; + const char* separator = ""; + for (const auto& element : map) { + sb << separator << "\"" << rtc::ToString(element.first) + << "\":" << ToStringAsDouble(element.second); + separator = ","; + } + sb << "}"; + return sb.Release(); +} + +} // namespace + +bool RTCStats::operator==(const RTCStats& other) const { + if (type() != other.type() || id() != other.id()) + return false; + std::vector members = Members(); + std::vector other_members = other.Members(); + RTC_DCHECK_EQ(members.size(), other_members.size()); + for (size_t i = 0; i < members.size(); ++i) { + const RTCStatsMemberInterface* member = members[i]; + const RTCStatsMemberInterface* other_member = other_members[i]; + RTC_DCHECK_EQ(member->type(), other_member->type()); + RTC_DCHECK_EQ(member->name(), other_member->name()); + if (*member != *other_member) + return false; + } + return true; +} + +bool RTCStats::operator!=(const RTCStats& other) const { + return !(*this == other); +} + +std::string RTCStats::ToJson() const { + rtc::StringBuilder sb; + sb << "{\"type\":\"" << type() + << "\"," + "\"id\":\"" + << id_ + << "\"," + "\"timestamp\":" + << timestamp_.us(); + for (const RTCStatsMemberInterface* member : Members()) { + if (member->is_defined()) { + sb << ",\"" << member->name() << "\":"; + if (member->is_string()) + sb << "\"" << member->ValueToJson() << "\""; + else + sb << member->ValueToJson(); + } + } + sb << "}"; + return sb.Release(); +} + +std::vector RTCStats::Members() const { + return MembersOfThisObjectAndAncestors(0); +} + +std::vector +RTCStats::MembersOfThisObjectAndAncestors(size_t additional_capacity) const { + std::vector members; + members.reserve(additional_capacity); + return members; +} + +#define WEBRTC_DEFINE_RTCSTATSMEMBER(T, type, is_seq, is_str, to_str, to_json) \ + template <> \ + RTCStatsMemberInterface::Type RTCStatsMember::StaticType() { \ + return type; \ + } \ + template <> \ + bool RTCStatsMember::is_sequence() const { \ + return is_seq; \ + } \ + template <> \ + bool RTCStatsMember::is_string() const { \ + return is_str; \ + } \ + template <> \ + std::string RTCStatsMember::ValueToString() const { \ + RTC_DCHECK(value_.has_value()); \ + return to_str; \ + } \ + template <> \ + std::string RTCStatsMember::ValueToJson() const { \ + RTC_DCHECK(value_.has_value()); \ + return to_json; \ + } \ + template class RTC_EXPORT_TEMPLATE_DEFINE(RTC_EXPORT) RTCStatsMember + +WEBRTC_DEFINE_RTCSTATSMEMBER(bool, + kBool, + false, + false, + rtc::ToString(*value_), + rtc::ToString(*value_)); +WEBRTC_DEFINE_RTCSTATSMEMBER(int32_t, + kInt32, + false, + false, + rtc::ToString(*value_), + rtc::ToString(*value_)); +WEBRTC_DEFINE_RTCSTATSMEMBER(uint32_t, + kUint32, + false, + false, + rtc::ToString(*value_), + rtc::ToString(*value_)); +WEBRTC_DEFINE_RTCSTATSMEMBER(int64_t, + kInt64, + false, + false, + rtc::ToString(*value_), + ToStringAsDouble(*value_)); +WEBRTC_DEFINE_RTCSTATSMEMBER(uint64_t, + kUint64, + false, + false, + rtc::ToString(*value_), + ToStringAsDouble(*value_)); +WEBRTC_DEFINE_RTCSTATSMEMBER(double, + kDouble, + false, + false, + rtc::ToString(*value_), + ToStringAsDouble(*value_)); +WEBRTC_DEFINE_RTCSTATSMEMBER(std::string, + kString, + false, + true, + *value_, + *value_); +WEBRTC_DEFINE_RTCSTATSMEMBER(std::vector, + kSequenceBool, + true, + false, + VectorToString(*value_), + VectorToString(*value_)); +WEBRTC_DEFINE_RTCSTATSMEMBER(std::vector, + kSequenceInt32, + true, + false, + VectorToString(*value_), + VectorToString(*value_)); +WEBRTC_DEFINE_RTCSTATSMEMBER(std::vector, + kSequenceUint32, + true, + false, + VectorToString(*value_), + VectorToString(*value_)); +WEBRTC_DEFINE_RTCSTATSMEMBER(std::vector, + kSequenceInt64, + true, + false, + VectorToString(*value_), + VectorToStringAsDouble(*value_)); +WEBRTC_DEFINE_RTCSTATSMEMBER(std::vector, + kSequenceUint64, + true, + false, + VectorToString(*value_), + VectorToStringAsDouble(*value_)); +WEBRTC_DEFINE_RTCSTATSMEMBER(std::vector, + kSequenceDouble, + true, + false, + VectorToString(*value_), + VectorToStringAsDouble(*value_)); +WEBRTC_DEFINE_RTCSTATSMEMBER(std::vector, + kSequenceString, + true, + false, + VectorOfStringsToString(*value_), + VectorOfStringsToString(*value_)); +WEBRTC_DEFINE_RTCSTATSMEMBER(rtc_stats_internal::MapStringUint64, + kMapStringUint64, + false, + false, + MapToString(*value_), + MapToStringAsDouble(*value_)); +WEBRTC_DEFINE_RTCSTATSMEMBER(rtc_stats_internal::MapStringDouble, + kMapStringDouble, + false, + false, + MapToString(*value_), + MapToStringAsDouble(*value_)); + +// Restricted members that expose hardware capabilites. +template class RTC_EXPORT_TEMPLATE_DEFINE(RTC_EXPORT) + RTCRestrictedStatsMember; +template class RTC_EXPORT_TEMPLATE_DEFINE(RTC_EXPORT) + RTCRestrictedStatsMember; +template class RTC_EXPORT_TEMPLATE_DEFINE(RTC_EXPORT) + RTCRestrictedStatsMember; +template class RTC_EXPORT_TEMPLATE_DEFINE(RTC_EXPORT) + RTCRestrictedStatsMember; +template class RTC_EXPORT_TEMPLATE_DEFINE(RTC_EXPORT) + RTCRestrictedStatsMember; +template class RTC_EXPORT_TEMPLATE_DEFINE(RTC_EXPORT) + RTCRestrictedStatsMember; +template class RTC_EXPORT_TEMPLATE_DEFINE(RTC_EXPORT) + RTCRestrictedStatsMember; +template class RTC_EXPORT_TEMPLATE_DEFINE(RTC_EXPORT) + RTCRestrictedStatsMember, + StatExposureCriteria::kHardwareCapability>; +template class RTC_EXPORT_TEMPLATE_DEFINE(RTC_EXPORT) + RTCRestrictedStatsMember, + StatExposureCriteria::kHardwareCapability>; +template class RTC_EXPORT_TEMPLATE_DEFINE(RTC_EXPORT) + RTCRestrictedStatsMember, + StatExposureCriteria::kHardwareCapability>; +template class RTC_EXPORT_TEMPLATE_DEFINE(RTC_EXPORT) + RTCRestrictedStatsMember, + StatExposureCriteria::kHardwareCapability>; +template class RTC_EXPORT_TEMPLATE_DEFINE(RTC_EXPORT) + RTCRestrictedStatsMember, + StatExposureCriteria::kHardwareCapability>; +template class RTC_EXPORT_TEMPLATE_DEFINE(RTC_EXPORT) + RTCRestrictedStatsMember, + StatExposureCriteria::kHardwareCapability>; +template class RTC_EXPORT_TEMPLATE_DEFINE(RTC_EXPORT) + RTCRestrictedStatsMember, + StatExposureCriteria::kHardwareCapability>; +template class RTC_EXPORT_TEMPLATE_DEFINE(RTC_EXPORT) + RTCRestrictedStatsMember, + StatExposureCriteria::kHardwareCapability>; +template class RTC_EXPORT_TEMPLATE_DEFINE(RTC_EXPORT) + RTCRestrictedStatsMember, + StatExposureCriteria::kHardwareCapability>; + +template class RTC_EXPORT_TEMPLATE_DEFINE(RTC_EXPORT) + RTCNonStandardStatsMember; +template class RTC_EXPORT_TEMPLATE_DEFINE(RTC_EXPORT) + RTCNonStandardStatsMember; +template class RTC_EXPORT_TEMPLATE_DEFINE(RTC_EXPORT) + RTCNonStandardStatsMember; +template class RTC_EXPORT_TEMPLATE_DEFINE(RTC_EXPORT) + RTCNonStandardStatsMember; +template class RTC_EXPORT_TEMPLATE_DEFINE(RTC_EXPORT) + RTCNonStandardStatsMember; +template class RTC_EXPORT_TEMPLATE_DEFINE(RTC_EXPORT) + RTCNonStandardStatsMember; +template class RTC_EXPORT_TEMPLATE_DEFINE(RTC_EXPORT) + RTCNonStandardStatsMember; +template class RTC_EXPORT_TEMPLATE_DEFINE(RTC_EXPORT) + RTCNonStandardStatsMember>; +template class RTC_EXPORT_TEMPLATE_DEFINE(RTC_EXPORT) + RTCNonStandardStatsMember>; +template class RTC_EXPORT_TEMPLATE_DEFINE(RTC_EXPORT) + RTCNonStandardStatsMember>; +template class RTC_EXPORT_TEMPLATE_DEFINE(RTC_EXPORT) + RTCNonStandardStatsMember>; +template class RTC_EXPORT_TEMPLATE_DEFINE(RTC_EXPORT) + RTCNonStandardStatsMember>; +template class RTC_EXPORT_TEMPLATE_DEFINE(RTC_EXPORT) + RTCNonStandardStatsMember>; +template class RTC_EXPORT_TEMPLATE_DEFINE(RTC_EXPORT) + RTCNonStandardStatsMember>; + +} // namespace webrtc -- cgit v1.2.3