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