hbos | 615d301 | 2016-08-24 01:33:13 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 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 | |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 11 | #include "api/stats/rtc_stats.h" |
hbos | 615d301 | 2016-08-24 01:33:13 -0700 | [diff] [blame] | 12 | |
Steve Anton | a29b3a6 | 2018-12-27 15:44:25 -0800 | [diff] [blame] | 13 | #include <cstdio> |
hbos | 6ded190 | 2016-11-01 01:50:46 -0700 | [diff] [blame] | 14 | |
Steve Anton | a29b3a6 | 2018-12-27 15:44:25 -0800 | [diff] [blame] | 15 | #include "rtc_base/arraysize.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 16 | #include "rtc_base/string_encode.h" |
Steve Anton | a29b3a6 | 2018-12-27 15:44:25 -0800 | [diff] [blame] | 17 | #include "rtc_base/strings/string_builder.h" |
hbos | 615d301 | 2016-08-24 01:33:13 -0700 | [diff] [blame] | 18 | |
| 19 | namespace webrtc { |
| 20 | |
| 21 | namespace { |
| 22 | |
ehmaldonado | 35a872c | 2017-07-28 07:29:12 -0700 | [diff] [blame] | 23 | // Produces "[a,b,c]". Works for non-vector |RTCStatsMemberInterface::Type| |
hbos | 615d301 | 2016-08-24 01:33:13 -0700 | [diff] [blame] | 24 | // types. |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 25 | template <typename T> |
hbos | 615d301 | 2016-08-24 01:33:13 -0700 | [diff] [blame] | 26 | std::string VectorToString(const std::vector<T>& vector) { |
Steve Anton | a29b3a6 | 2018-12-27 15:44:25 -0800 | [diff] [blame] | 27 | rtc::StringBuilder sb; |
| 28 | sb << "["; |
| 29 | const char* separator = ""; |
| 30 | for (const T& element : vector) { |
| 31 | sb << separator << rtc::ToString(element); |
| 32 | separator = ","; |
hbos | 615d301 | 2016-08-24 01:33:13 -0700 | [diff] [blame] | 33 | } |
Steve Anton | a29b3a6 | 2018-12-27 15:44:25 -0800 | [diff] [blame] | 34 | sb << "]"; |
| 35 | return sb.Release(); |
hbos | 615d301 | 2016-08-24 01:33:13 -0700 | [diff] [blame] | 36 | } |
| 37 | |
ehmaldonado | 35a872c | 2017-07-28 07:29:12 -0700 | [diff] [blame] | 38 | // Produces "[\"a\",\"b\",\"c\"]". Works for vectors of both const char* and |
hbos | 615d301 | 2016-08-24 01:33:13 -0700 | [diff] [blame] | 39 | // std::string element types. |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 40 | template <typename T> |
hbos | 615d301 | 2016-08-24 01:33:13 -0700 | [diff] [blame] | 41 | std::string VectorOfStringsToString(const std::vector<T>& strings) { |
Steve Anton | a29b3a6 | 2018-12-27 15:44:25 -0800 | [diff] [blame] | 42 | rtc::StringBuilder sb; |
| 43 | sb << "["; |
| 44 | const char* separator = ""; |
| 45 | for (const T& element : strings) { |
| 46 | sb << separator << "\"" << rtc::ToString(element) << "\""; |
| 47 | separator = ","; |
hbos | 615d301 | 2016-08-24 01:33:13 -0700 | [diff] [blame] | 48 | } |
Steve Anton | a29b3a6 | 2018-12-27 15:44:25 -0800 | [diff] [blame] | 49 | sb << "]"; |
| 50 | return sb.Release(); |
ehmaldonado | 35a872c | 2017-07-28 07:29:12 -0700 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | template <typename T> |
| 54 | std::string ToStringAsDouble(const T value) { |
| 55 | // JSON represents numbers as floating point numbers with about 15 decimal |
| 56 | // digits of precision. |
Steve Anton | a29b3a6 | 2018-12-27 15:44:25 -0800 | [diff] [blame] | 57 | char buf[32]; |
| 58 | const int len = std::snprintf(&buf[0], arraysize(buf), "%.16g", |
| 59 | static_cast<double>(value)); |
| 60 | RTC_DCHECK_LE(len, arraysize(buf)); |
| 61 | return std::string(&buf[0], len); |
ehmaldonado | 35a872c | 2017-07-28 07:29:12 -0700 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | template <typename T> |
| 65 | std::string VectorToStringAsDouble(const std::vector<T>& vector) { |
Steve Anton | a29b3a6 | 2018-12-27 15:44:25 -0800 | [diff] [blame] | 66 | rtc::StringBuilder sb; |
| 67 | sb << "["; |
| 68 | const char* separator = ""; |
| 69 | for (const T& element : vector) { |
| 70 | sb << separator << ToStringAsDouble<T>(element); |
| 71 | separator = ","; |
ehmaldonado | 35a872c | 2017-07-28 07:29:12 -0700 | [diff] [blame] | 72 | } |
Steve Anton | a29b3a6 | 2018-12-27 15:44:25 -0800 | [diff] [blame] | 73 | sb << "]"; |
| 74 | return sb.Release(); |
hbos | 615d301 | 2016-08-24 01:33:13 -0700 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | } // namespace |
| 78 | |
hbos | 67c8bc4 | 2016-10-25 04:31:23 -0700 | [diff] [blame] | 79 | bool RTCStats::operator==(const RTCStats& other) const { |
hbos | 0583b28 | 2016-11-30 01:50:14 -0800 | [diff] [blame] | 80 | if (type() != other.type() || id() != other.id()) |
hbos | 67c8bc4 | 2016-10-25 04:31:23 -0700 | [diff] [blame] | 81 | return false; |
hbos | 67c8bc4 | 2016-10-25 04:31:23 -0700 | [diff] [blame] | 82 | std::vector<const RTCStatsMemberInterface*> members = Members(); |
| 83 | std::vector<const RTCStatsMemberInterface*> other_members = other.Members(); |
| 84 | RTC_DCHECK_EQ(members.size(), other_members.size()); |
| 85 | for (size_t i = 0; i < members.size(); ++i) { |
| 86 | const RTCStatsMemberInterface* member = members[i]; |
| 87 | const RTCStatsMemberInterface* other_member = other_members[i]; |
| 88 | RTC_DCHECK_EQ(member->type(), other_member->type()); |
| 89 | RTC_DCHECK_EQ(member->name(), other_member->name()); |
| 90 | if (*member != *other_member) |
| 91 | return false; |
| 92 | } |
| 93 | return true; |
| 94 | } |
| 95 | |
| 96 | bool RTCStats::operator!=(const RTCStats& other) const { |
| 97 | return !(*this == other); |
| 98 | } |
| 99 | |
ehmaldonado | 35a872c | 2017-07-28 07:29:12 -0700 | [diff] [blame] | 100 | std::string RTCStats::ToJson() const { |
Steve Anton | a29b3a6 | 2018-12-27 15:44:25 -0800 | [diff] [blame] | 101 | rtc::StringBuilder sb; |
| 102 | sb << "{\"type\":\"" << type() << "\"," |
| 103 | << "\"id\":\"" << id_ << "\"," |
| 104 | << "\"timestamp\":" << timestamp_us_; |
hbos | 615d301 | 2016-08-24 01:33:13 -0700 | [diff] [blame] | 105 | for (const RTCStatsMemberInterface* member : Members()) { |
hbos | 615d301 | 2016-08-24 01:33:13 -0700 | [diff] [blame] | 106 | if (member->is_defined()) { |
Steve Anton | a29b3a6 | 2018-12-27 15:44:25 -0800 | [diff] [blame] | 107 | sb << ",\"" << member->name() << "\":"; |
hbos | 615d301 | 2016-08-24 01:33:13 -0700 | [diff] [blame] | 108 | if (member->is_string()) |
Steve Anton | a29b3a6 | 2018-12-27 15:44:25 -0800 | [diff] [blame] | 109 | sb << "\"" << member->ValueToJson() << "\""; |
hbos | 615d301 | 2016-08-24 01:33:13 -0700 | [diff] [blame] | 110 | else |
Steve Anton | a29b3a6 | 2018-12-27 15:44:25 -0800 | [diff] [blame] | 111 | sb << member->ValueToJson(); |
hbos | 615d301 | 2016-08-24 01:33:13 -0700 | [diff] [blame] | 112 | } |
| 113 | } |
Steve Anton | a29b3a6 | 2018-12-27 15:44:25 -0800 | [diff] [blame] | 114 | sb << "}"; |
| 115 | return sb.Release(); |
hbos | 615d301 | 2016-08-24 01:33:13 -0700 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | std::vector<const RTCStatsMemberInterface*> RTCStats::Members() const { |
| 119 | return MembersOfThisObjectAndAncestors(0); |
| 120 | } |
| 121 | |
| 122 | std::vector<const RTCStatsMemberInterface*> |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 123 | RTCStats::MembersOfThisObjectAndAncestors(size_t additional_capacity) const { |
hbos | 615d301 | 2016-08-24 01:33:13 -0700 | [diff] [blame] | 124 | std::vector<const RTCStatsMemberInterface*> members; |
| 125 | members.reserve(additional_capacity); |
| 126 | return members; |
| 127 | } |
| 128 | |
ehmaldonado | 35a872c | 2017-07-28 07:29:12 -0700 | [diff] [blame] | 129 | #define WEBRTC_DEFINE_RTCSTATSMEMBER(T, type, is_seq, is_str, to_str, to_json) \ |
| 130 | template <> \ |
hbos | 615d301 | 2016-08-24 01:33:13 -0700 | [diff] [blame] | 131 | const RTCStatsMemberInterface::Type RTCStatsMember<T>::kType = \ |
| 132 | RTCStatsMemberInterface::type; \ |
ehmaldonado | 35a872c | 2017-07-28 07:29:12 -0700 | [diff] [blame] | 133 | template <> \ |
| 134 | bool RTCStatsMember<T>::is_sequence() const { \ |
| 135 | return is_seq; \ |
| 136 | } \ |
| 137 | template <> \ |
| 138 | bool RTCStatsMember<T>::is_string() const { \ |
| 139 | return is_str; \ |
| 140 | } \ |
| 141 | template <> \ |
hbos | 615d301 | 2016-08-24 01:33:13 -0700 | [diff] [blame] | 142 | std::string RTCStatsMember<T>::ValueToString() const { \ |
| 143 | RTC_DCHECK(is_defined_); \ |
| 144 | return to_str; \ |
ehmaldonado | 35a872c | 2017-07-28 07:29:12 -0700 | [diff] [blame] | 145 | } \ |
| 146 | template <> \ |
| 147 | std::string RTCStatsMember<T>::ValueToJson() const { \ |
| 148 | RTC_DCHECK(is_defined_); \ |
| 149 | return to_json; \ |
hbos | 615d301 | 2016-08-24 01:33:13 -0700 | [diff] [blame] | 150 | } |
| 151 | |
ehmaldonado | 35a872c | 2017-07-28 07:29:12 -0700 | [diff] [blame] | 152 | WEBRTC_DEFINE_RTCSTATSMEMBER(bool, |
| 153 | kBool, |
| 154 | false, |
| 155 | false, |
| 156 | rtc::ToString(value_), |
hbos | b20f387 | 2016-10-04 14:37:11 -0700 | [diff] [blame] | 157 | rtc::ToString(value_)); |
ehmaldonado | 35a872c | 2017-07-28 07:29:12 -0700 | [diff] [blame] | 158 | WEBRTC_DEFINE_RTCSTATSMEMBER(int32_t, |
| 159 | kInt32, |
| 160 | false, |
| 161 | false, |
| 162 | rtc::ToString(value_), |
hbos | 615d301 | 2016-08-24 01:33:13 -0700 | [diff] [blame] | 163 | rtc::ToString(value_)); |
ehmaldonado | 35a872c | 2017-07-28 07:29:12 -0700 | [diff] [blame] | 164 | WEBRTC_DEFINE_RTCSTATSMEMBER(uint32_t, |
| 165 | kUint32, |
| 166 | false, |
| 167 | false, |
| 168 | rtc::ToString(value_), |
hbos | 615d301 | 2016-08-24 01:33:13 -0700 | [diff] [blame] | 169 | rtc::ToString(value_)); |
ehmaldonado | 35a872c | 2017-07-28 07:29:12 -0700 | [diff] [blame] | 170 | WEBRTC_DEFINE_RTCSTATSMEMBER(int64_t, |
| 171 | kInt64, |
| 172 | false, |
| 173 | false, |
| 174 | rtc::ToString(value_), |
| 175 | ToStringAsDouble(value_)); |
| 176 | WEBRTC_DEFINE_RTCSTATSMEMBER(uint64_t, |
| 177 | kUint64, |
| 178 | false, |
| 179 | false, |
| 180 | rtc::ToString(value_), |
| 181 | ToStringAsDouble(value_)); |
| 182 | WEBRTC_DEFINE_RTCSTATSMEMBER(double, |
| 183 | kDouble, |
| 184 | false, |
| 185 | false, |
| 186 | rtc::ToString(value_), |
| 187 | ToStringAsDouble(value_)); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 188 | WEBRTC_DEFINE_RTCSTATSMEMBER(std::string, kString, false, true, value_, value_); |
ehmaldonado | 35a872c | 2017-07-28 07:29:12 -0700 | [diff] [blame] | 189 | WEBRTC_DEFINE_RTCSTATSMEMBER(std::vector<bool>, |
| 190 | kSequenceBool, |
| 191 | true, |
| 192 | false, |
| 193 | VectorToString(value_), |
| 194 | VectorToString(value_)); |
| 195 | WEBRTC_DEFINE_RTCSTATSMEMBER(std::vector<int32_t>, |
| 196 | kSequenceInt32, |
| 197 | true, |
| 198 | false, |
| 199 | VectorToString(value_), |
| 200 | VectorToString(value_)); |
| 201 | WEBRTC_DEFINE_RTCSTATSMEMBER(std::vector<uint32_t>, |
| 202 | kSequenceUint32, |
| 203 | true, |
| 204 | false, |
| 205 | VectorToString(value_), |
| 206 | VectorToString(value_)); |
| 207 | WEBRTC_DEFINE_RTCSTATSMEMBER(std::vector<int64_t>, |
| 208 | kSequenceInt64, |
| 209 | true, |
| 210 | false, |
| 211 | VectorToString(value_), |
| 212 | VectorToStringAsDouble(value_)); |
| 213 | WEBRTC_DEFINE_RTCSTATSMEMBER(std::vector<uint64_t>, |
| 214 | kSequenceUint64, |
| 215 | true, |
| 216 | false, |
| 217 | VectorToString(value_), |
| 218 | VectorToStringAsDouble(value_)); |
| 219 | WEBRTC_DEFINE_RTCSTATSMEMBER(std::vector<double>, |
| 220 | kSequenceDouble, |
| 221 | true, |
| 222 | false, |
| 223 | VectorToString(value_), |
| 224 | VectorToStringAsDouble(value_)); |
| 225 | WEBRTC_DEFINE_RTCSTATSMEMBER(std::vector<std::string>, |
| 226 | kSequenceString, |
| 227 | true, |
| 228 | false, |
| 229 | VectorOfStringsToString(value_), |
| 230 | VectorOfStringsToString(value_)); |
hbos | 615d301 | 2016-08-24 01:33:13 -0700 | [diff] [blame] | 231 | |
| 232 | } // namespace webrtc |