| 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/stats/rtc_stats.h" |
| 12 |
| 13 | #include <cstdio> |
| 14 |
| 15 | #include "rtc_base/arraysize.h" |
| 16 | #include "rtc_base/string_encode.h" |
| 17 | #include "rtc_base/strings/string_builder.h" |
| 18 |
| 19 | namespace webrtc { |
| 20 |
| 21 | namespace { |
| 22 |
| 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 = ","; |
| 33 | } |
| 34 | sb << "]"; |
| 35 | return sb.Release(); |
| 36 | } |
| 37 |
| 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 = ","; |
| 47 | } |
| 48 | sb << "]"; |
| 49 | return sb.Release(); |
| 50 | } |
| 51 |
| 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 = ","; |
| 62 | } |
| 63 | sb << "]"; |
| 64 | return sb.Release(); |
| 65 | } |
| 66 |
| 67 | template <typename T> |
| 68 | std::string ToStringAsDouble(const T value) { |
| 69 | // JSON represents numbers as floating point numbers with about 15 decimal |
| 70 | // digits of precision. |
| 71 | char buf[32]; |
| 72 | const int len = std::snprintf(&buf[0], arraysize(buf), "%.16g", |
| 73 | static_cast<double>(value)); |
| 74 | RTC_DCHECK_LE(len, arraysize(buf)); |
| 75 | return std::string(&buf[0], len); |
| 76 | } |
| 77 |
| 78 | template <typename T> |
| 79 | std::string VectorToStringAsDouble(const std::vector<T>& vector) { |
| 80 | rtc::StringBuilder sb; |
| 81 | sb << "["; |
| 82 | const char* separator = ""; |
| 83 | for (const T& element : vector) { |
| 84 | sb << separator << ToStringAsDouble<T>(element); |
| 85 | separator = ","; |
| 86 | } |
| 87 | sb << "]"; |
| 88 | return sb.Release(); |
| 89 | } |
| 90 |
| 91 | } // namespace |
| 92 |
| 93 | bool RTCStats::operator==(const RTCStats& other) const { |
| 94 | if (type() != other.type() || id() != other.id()) |
| 95 | return false; |
| 96 | std::vector<const RTCStatsMemberInterface*> members = Members(); |
| 97 | std::vector<const RTCStatsMemberInterface*> other_members = other.Members(); |
| 98 | RTC_DCHECK_EQ(members.size(), other_members.size()); |
| 99 | for (size_t i = 0; i < members.size(); ++i) { |
| 100 | const RTCStatsMemberInterface* member = members[i]; |
| 101 | const RTCStatsMemberInterface* other_member = other_members[i]; |
| 102 | RTC_DCHECK_EQ(member->type(), other_member->type()); |
| 103 | RTC_DCHECK_EQ(member->name(), other_member->name()); |
| 104 | if (*member != *other_member) |
| 105 | return false; |
| 106 | } |
| 107 | return true; |
| 108 | } |
| 109 |
| 110 | bool RTCStats::operator!=(const RTCStats& other) const { |
| 111 | return !(*this == other); |
| 112 | } |
| 113 |
| 114 | std::string RTCStats::ToJson() const { |
| 115 | rtc::StringBuilder sb; |
| 116 | sb << "{\"type\":\"" << type() |
| 117 | << "\"," |
| 118 | "\"id\":\"" |
| 119 | << id_ |
| 120 | << "\"," |
| 121 | "\"timestamp\":" |
| 122 | << timestamp_us_; |
| 123 | for (const RTCStatsMemberInterface* member : Members()) { |
| 124 | if (member->is_defined()) { |
| 125 | sb << ",\"" << member->name() << "\":"; |
| 126 | if (member->is_string()) |
| 127 | sb << "\"" << member->ValueToJson() << "\""; |
| 128 | else |
| 129 | sb << member->ValueToJson(); |
| 130 | } |
| 131 | } |
| 132 | sb << "}"; |
| 133 | return sb.Release(); |
| 134 | } |
| 135 |
| 136 | std::vector<const RTCStatsMemberInterface*> RTCStats::Members() const { |
| 137 | return MembersOfThisObjectAndAncestors(0); |
| 138 | } |
| 139 |
| 140 | std::vector<const RTCStatsMemberInterface*> |
| 141 | RTCStats::MembersOfThisObjectAndAncestors(size_t additional_capacity) const { |
| 142 | std::vector<const RTCStatsMemberInterface*> members; |
| 143 | members.reserve(additional_capacity); |
| 144 | return members; |
| 145 | } |
| 146 |
| 147 | #define WEBRTC_DEFINE_RTCSTATSMEMBER(T, type, is_seq, is_str, to_str, to_json) \ |
| 148 | template <> \ |
| 149 | RTCStatsMemberInterface::Type RTCStatsMember<T>::StaticType() { \ |
| 150 | return type; \ |
| 151 | } \ |
| 152 | template <> \ |
| 153 | bool RTCStatsMember<T>::is_sequence() const { \ |
| 154 | return is_seq; \ |
| 155 | } \ |
| 156 | template <> \ |
| 157 | bool RTCStatsMember<T>::is_string() const { \ |
| 158 | return is_str; \ |
| 159 | } \ |
| 160 | template <> \ |
| 161 | std::string RTCStatsMember<T>::ValueToString() const { \ |
| 162 | RTC_DCHECK(is_defined_); \ |
| 163 | return to_str; \ |
| 164 | } \ |
| 165 | template <> \ |
| 166 | std::string RTCStatsMember<T>::ValueToJson() const { \ |
| 167 | RTC_DCHECK(is_defined_); \ |
| 168 | return to_json; \ |
| 169 | } \ |
| 170 | template class RTC_EXPORT_TEMPLATE_DEFINE(RTC_EXPORT) RTCStatsMember<T> |
| 171 |
| 172 | WEBRTC_DEFINE_RTCSTATSMEMBER(bool, |
| 173 | kBool, |
| 174 | false, |
| 175 | false, |
| 176 | rtc::ToString(value_), |
| 177 | rtc::ToString(value_)); |
| 178 | WEBRTC_DEFINE_RTCSTATSMEMBER(int32_t, |
| 179 | kInt32, |
| 180 | false, |
| 181 | false, |
| 182 | rtc::ToString(value_), |
| 183 | rtc::ToString(value_)); |
| 184 | WEBRTC_DEFINE_RTCSTATSMEMBER(uint32_t, |
| 185 | kUint32, |
| 186 | false, |
| 187 | false, |
| 188 | rtc::ToString(value_), |
| 189 | rtc::ToString(value_)); |
| 190 | WEBRTC_DEFINE_RTCSTATSMEMBER(int64_t, |
| 191 | kInt64, |
| 192 | false, |
| 193 | false, |
| 194 | rtc::ToString(value_), |
| 195 | ToStringAsDouble(value_)); |
| 196 | WEBRTC_DEFINE_RTCSTATSMEMBER(uint64_t, |
| 197 | kUint64, |
| 198 | false, |
| 199 | false, |
| 200 | rtc::ToString(value_), |
| 201 | ToStringAsDouble(value_)); |
| 202 | WEBRTC_DEFINE_RTCSTATSMEMBER(double, |
| 203 | kDouble, |
| 204 | false, |
| 205 | false, |
| 206 | rtc::ToString(value_), |
| 207 | ToStringAsDouble(value_)); |
| 208 | WEBRTC_DEFINE_RTCSTATSMEMBER(std::string, kString, false, true, value_, value_); |
| 209 | WEBRTC_DEFINE_RTCSTATSMEMBER(std::vector<bool>, |
| 210 | kSequenceBool, |
| 211 | true, |
| 212 | false, |
| 213 | VectorToString(value_), |
| 214 | VectorToString(value_)); |
| 215 | WEBRTC_DEFINE_RTCSTATSMEMBER(std::vector<int32_t>, |
| 216 | kSequenceInt32, |
| 217 | true, |
| 218 | false, |
| 219 | VectorToString(value_), |
| 220 | VectorToString(value_)); |
| 221 | WEBRTC_DEFINE_RTCSTATSMEMBER(std::vector<uint32_t>, |
| 222 | kSequenceUint32, |
| 223 | true, |
| 224 | false, |
| 225 | VectorToString(value_), |
| 226 | VectorToString(value_)); |
| 227 | WEBRTC_DEFINE_RTCSTATSMEMBER(std::vector<int64_t>, |
| 228 | kSequenceInt64, |
| 229 | true, |
| 230 | false, |
| 231 | VectorToString(value_), |
| 232 | VectorToStringAsDouble(value_)); |
| 233 | WEBRTC_DEFINE_RTCSTATSMEMBER(std::vector<uint64_t>, |
| 234 | kSequenceUint64, |
| 235 | true, |
| 236 | false, |
| 237 | VectorToString(value_), |
| 238 | VectorToStringAsDouble(value_)); |
| 239 | WEBRTC_DEFINE_RTCSTATSMEMBER(std::vector<double>, |
| 240 | kSequenceDouble, |
| 241 | true, |
| 242 | false, |
| 243 | VectorToString(value_), |
| 244 | VectorToStringAsDouble(value_)); |
| 245 | WEBRTC_DEFINE_RTCSTATSMEMBER(std::vector<std::string>, |
| 246 | kSequenceString, |
| 247 | true, |
| 248 | false, |
| 249 | VectorOfStringsToString(value_), |
| 250 | VectorOfStringsToString(value_)); |
| 251 |
| 252 | template class RTC_EXPORT_TEMPLATE_DEFINE(RTC_EXPORT) |
| 253 | RTCNonStandardStatsMember<bool>; |
| 254 | template class RTC_EXPORT_TEMPLATE_DEFINE(RTC_EXPORT) |
| 255 | RTCNonStandardStatsMember<int32_t>; |
| 256 | template class RTC_EXPORT_TEMPLATE_DEFINE(RTC_EXPORT) |
| 257 | RTCNonStandardStatsMember<uint32_t>; |
| 258 | template class RTC_EXPORT_TEMPLATE_DEFINE(RTC_EXPORT) |
| 259 | RTCNonStandardStatsMember<int64_t>; |
| 260 | template class RTC_EXPORT_TEMPLATE_DEFINE(RTC_EXPORT) |
| 261 | RTCNonStandardStatsMember<uint64_t>; |
| 262 | template class RTC_EXPORT_TEMPLATE_DEFINE(RTC_EXPORT) |
| 263 | RTCNonStandardStatsMember<double>; |
| 264 | template class RTC_EXPORT_TEMPLATE_DEFINE(RTC_EXPORT) |
| 265 | RTCNonStandardStatsMember<std::string>; |
| 266 | template class RTC_EXPORT_TEMPLATE_DEFINE(RTC_EXPORT) |
| 267 | RTCNonStandardStatsMember<std::vector<bool>>; |
| 268 | template class RTC_EXPORT_TEMPLATE_DEFINE(RTC_EXPORT) |
| 269 | RTCNonStandardStatsMember<std::vector<int32_t>>; |
| 270 | template class RTC_EXPORT_TEMPLATE_DEFINE(RTC_EXPORT) |
| 271 | RTCNonStandardStatsMember<std::vector<uint32_t>>; |
| 272 | template class RTC_EXPORT_TEMPLATE_DEFINE(RTC_EXPORT) |
| 273 | RTCNonStandardStatsMember<std::vector<int64_t>>; |
| 274 | template class RTC_EXPORT_TEMPLATE_DEFINE(RTC_EXPORT) |
| 275 | RTCNonStandardStatsMember<std::vector<uint64_t>>; |
| 276 | template class RTC_EXPORT_TEMPLATE_DEFINE(RTC_EXPORT) |
| 277 | RTCNonStandardStatsMember<std::vector<double>>; |
| 278 | template class RTC_EXPORT_TEMPLATE_DEFINE(RTC_EXPORT) |
| 279 | RTCNonStandardStatsMember<std::vector<std::string>>; |
| 280 |
| 281 | } // namespace webrtc |
| 282 |