Bug 1880804 [wpt PR 44645] - Implement constructor in RTCEncodedVideoFrame, a=testonly
[gecko.git] / third_party / libwebrtc / stats / rtc_stats.cc
blob25bde289c20e6085366a9dbefd63cf519f95b5c0
1 /*
2 * Copyright 2016 The WebRTC Project Authors. All rights reserved.
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 */
11 #include "api/stats/rtc_stats.h"
13 #include <cstdio>
15 #include "rtc_base/arraysize.h"
16 #include "rtc_base/string_encode.h"
17 #include "rtc_base/strings/string_builder.h"
19 namespace webrtc {
21 namespace {
23 // Produces "[a,b,c]". Works for non-vector `RTCStatsMemberInterface::Type`
24 // types.
25 template <typename T>
26 std::string VectorToString(const std::vector<T>& vector) {
27 rtc::StringBuilder sb;
28 sb << "[";
29 const char* separator = "";
30 for (const T& element : vector) {
31 sb << separator << rtc::ToString(element);
32 separator = ",";
34 sb << "]";
35 return sb.Release();
38 // This overload is required because std::vector<bool> range loops don't
39 // return references but objects, causing -Wrange-loop-analysis diagnostics.
40 std::string VectorToString(const std::vector<bool>& vector) {
41 rtc::StringBuilder sb;
42 sb << "[";
43 const char* separator = "";
44 for (bool element : vector) {
45 sb << separator << rtc::ToString(element);
46 separator = ",";
48 sb << "]";
49 return sb.Release();
52 // Produces "[\"a\",\"b\",\"c\"]". Works for vectors of both const char* and
53 // std::string element types.
54 template <typename T>
55 std::string VectorOfStringsToString(const std::vector<T>& strings) {
56 rtc::StringBuilder sb;
57 sb << "[";
58 const char* separator = "";
59 for (const T& element : strings) {
60 sb << separator << "\"" << rtc::ToString(element) << "\"";
61 separator = ",";
63 sb << "]";
64 return sb.Release();
67 template <typename T>
68 std::string MapToString(const std::map<std::string, T>& map) {
69 rtc::StringBuilder sb;
70 sb << "{";
71 const char* separator = "";
72 for (const auto& element : map) {
73 sb << separator << rtc::ToString(element.first) << ":"
74 << rtc::ToString(element.second);
75 separator = ",";
77 sb << "}";
78 return sb.Release();
81 template <typename T>
82 std::string ToStringAsDouble(const T value) {
83 // JSON represents numbers as floating point numbers with about 15 decimal
84 // digits of precision.
85 char buf[32];
86 const int len = std::snprintf(&buf[0], arraysize(buf), "%.16g",
87 static_cast<double>(value));
88 RTC_DCHECK_LE(len, arraysize(buf));
89 return std::string(&buf[0], len);
92 template <typename T>
93 std::string VectorToStringAsDouble(const std::vector<T>& vector) {
94 rtc::StringBuilder sb;
95 sb << "[";
96 const char* separator = "";
97 for (const T& element : vector) {
98 sb << separator << ToStringAsDouble<T>(element);
99 separator = ",";
101 sb << "]";
102 return sb.Release();
105 template <typename T>
106 std::string MapToStringAsDouble(const std::map<std::string, T>& map) {
107 rtc::StringBuilder sb;
108 sb << "{";
109 const char* separator = "";
110 for (const auto& element : map) {
111 sb << separator << "\"" << rtc::ToString(element.first)
112 << "\":" << ToStringAsDouble(element.second);
113 separator = ",";
115 sb << "}";
116 return sb.Release();
119 } // namespace
121 bool RTCStats::operator==(const RTCStats& other) const {
122 if (type() != other.type() || id() != other.id())
123 return false;
124 std::vector<const RTCStatsMemberInterface*> members = Members();
125 std::vector<const RTCStatsMemberInterface*> other_members = other.Members();
126 RTC_DCHECK_EQ(members.size(), other_members.size());
127 for (size_t i = 0; i < members.size(); ++i) {
128 const RTCStatsMemberInterface* member = members[i];
129 const RTCStatsMemberInterface* other_member = other_members[i];
130 RTC_DCHECK_EQ(member->type(), other_member->type());
131 RTC_DCHECK_EQ(member->name(), other_member->name());
132 if (*member != *other_member)
133 return false;
135 return true;
138 bool RTCStats::operator!=(const RTCStats& other) const {
139 return !(*this == other);
142 std::string RTCStats::ToJson() const {
143 rtc::StringBuilder sb;
144 sb << "{\"type\":\"" << type()
145 << "\","
146 "\"id\":\""
147 << id_
148 << "\","
149 "\"timestamp\":"
150 << timestamp_.us();
151 for (const RTCStatsMemberInterface* member : Members()) {
152 if (member->is_defined()) {
153 sb << ",\"" << member->name() << "\":";
154 if (member->is_string())
155 sb << "\"" << member->ValueToJson() << "\"";
156 else
157 sb << member->ValueToJson();
160 sb << "}";
161 return sb.Release();
164 std::vector<const RTCStatsMemberInterface*> RTCStats::Members() const {
165 return MembersOfThisObjectAndAncestors(0);
168 std::vector<const RTCStatsMemberInterface*>
169 RTCStats::MembersOfThisObjectAndAncestors(size_t additional_capacity) const {
170 std::vector<const RTCStatsMemberInterface*> members;
171 members.reserve(additional_capacity);
172 return members;
175 #define WEBRTC_DEFINE_RTCSTATSMEMBER(T, type, is_seq, is_str, to_str, to_json) \
176 template <> \
177 RTCStatsMemberInterface::Type RTCStatsMember<T>::StaticType() { \
178 return type; \
180 template <> \
181 bool RTCStatsMember<T>::is_sequence() const { \
182 return is_seq; \
184 template <> \
185 bool RTCStatsMember<T>::is_string() const { \
186 return is_str; \
188 template <> \
189 std::string RTCStatsMember<T>::ValueToString() const { \
190 RTC_DCHECK(value_.has_value()); \
191 return to_str; \
193 template <> \
194 std::string RTCStatsMember<T>::ValueToJson() const { \
195 RTC_DCHECK(value_.has_value()); \
196 return to_json; \
198 template class RTC_EXPORT_TEMPLATE_DEFINE(RTC_EXPORT) RTCStatsMember<T>
200 WEBRTC_DEFINE_RTCSTATSMEMBER(bool,
201 kBool,
202 false,
203 false,
204 rtc::ToString(*value_),
205 rtc::ToString(*value_));
206 WEBRTC_DEFINE_RTCSTATSMEMBER(int32_t,
207 kInt32,
208 false,
209 false,
210 rtc::ToString(*value_),
211 rtc::ToString(*value_));
212 WEBRTC_DEFINE_RTCSTATSMEMBER(uint32_t,
213 kUint32,
214 false,
215 false,
216 rtc::ToString(*value_),
217 rtc::ToString(*value_));
218 WEBRTC_DEFINE_RTCSTATSMEMBER(int64_t,
219 kInt64,
220 false,
221 false,
222 rtc::ToString(*value_),
223 ToStringAsDouble(*value_));
224 WEBRTC_DEFINE_RTCSTATSMEMBER(uint64_t,
225 kUint64,
226 false,
227 false,
228 rtc::ToString(*value_),
229 ToStringAsDouble(*value_));
230 WEBRTC_DEFINE_RTCSTATSMEMBER(double,
231 kDouble,
232 false,
233 false,
234 rtc::ToString(*value_),
235 ToStringAsDouble(*value_));
236 WEBRTC_DEFINE_RTCSTATSMEMBER(std::string,
237 kString,
238 false,
239 true,
240 *value_,
241 *value_);
242 WEBRTC_DEFINE_RTCSTATSMEMBER(std::vector<bool>,
243 kSequenceBool,
244 true,
245 false,
246 VectorToString(*value_),
247 VectorToString(*value_));
248 WEBRTC_DEFINE_RTCSTATSMEMBER(std::vector<int32_t>,
249 kSequenceInt32,
250 true,
251 false,
252 VectorToString(*value_),
253 VectorToString(*value_));
254 WEBRTC_DEFINE_RTCSTATSMEMBER(std::vector<uint32_t>,
255 kSequenceUint32,
256 true,
257 false,
258 VectorToString(*value_),
259 VectorToString(*value_));
260 WEBRTC_DEFINE_RTCSTATSMEMBER(std::vector<int64_t>,
261 kSequenceInt64,
262 true,
263 false,
264 VectorToString(*value_),
265 VectorToStringAsDouble(*value_));
266 WEBRTC_DEFINE_RTCSTATSMEMBER(std::vector<uint64_t>,
267 kSequenceUint64,
268 true,
269 false,
270 VectorToString(*value_),
271 VectorToStringAsDouble(*value_));
272 WEBRTC_DEFINE_RTCSTATSMEMBER(std::vector<double>,
273 kSequenceDouble,
274 true,
275 false,
276 VectorToString(*value_),
277 VectorToStringAsDouble(*value_));
278 WEBRTC_DEFINE_RTCSTATSMEMBER(std::vector<std::string>,
279 kSequenceString,
280 true,
281 false,
282 VectorOfStringsToString(*value_),
283 VectorOfStringsToString(*value_));
284 WEBRTC_DEFINE_RTCSTATSMEMBER(rtc_stats_internal::MapStringUint64,
285 kMapStringUint64,
286 false,
287 false,
288 MapToString(*value_),
289 MapToStringAsDouble(*value_));
290 WEBRTC_DEFINE_RTCSTATSMEMBER(rtc_stats_internal::MapStringDouble,
291 kMapStringDouble,
292 false,
293 false,
294 MapToString(*value_),
295 MapToStringAsDouble(*value_));
297 } // namespace webrtc