blob: a27466079f953c12492b9a61f8b5791bc4a7002c [file] [log] [blame]
hbos615d3012016-08-24 01:33:13 -07001/*
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 Bonadei92ea95e2017-09-15 06:47:31 +020011#include "api/stats/rtcstats.h"
hbos615d3012016-08-24 01:33:13 -070012
ehmaldonado35a872c2017-07-28 07:29:12 -070013#include <iomanip>
hbos6ded1902016-11-01 01:50:46 -070014#include <sstream>
15
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020016#include "rtc_base/stringencode.h"
hbos615d3012016-08-24 01:33:13 -070017
18namespace webrtc {
19
20namespace {
21
ehmaldonado35a872c2017-07-28 07:29:12 -070022// Produces "[a,b,c]". Works for non-vector |RTCStatsMemberInterface::Type|
hbos615d3012016-08-24 01:33:13 -070023// types.
Yves Gerey665174f2018-06-19 15:03:05 +020024template <typename T>
hbos615d3012016-08-24 01:33:13 -070025std::string VectorToString(const std::vector<T>& vector) {
26 if (vector.empty())
ehmaldonado35a872c2017-07-28 07:29:12 -070027 return "[]";
hbos615d3012016-08-24 01:33:13 -070028 std::ostringstream oss;
Jonas Olsson6b1985d2018-07-05 11:59:48 +020029 oss << "[" << rtc::ToString(vector[0]);
hbos615d3012016-08-24 01:33:13 -070030 for (size_t i = 1; i < vector.size(); ++i) {
Jonas Olsson6b1985d2018-07-05 11:59:48 +020031 oss << "," << rtc::ToString(vector[i]);
hbos615d3012016-08-24 01:33:13 -070032 }
ehmaldonado35a872c2017-07-28 07:29:12 -070033 oss << "]";
hbos615d3012016-08-24 01:33:13 -070034 return oss.str();
35}
36
ehmaldonado35a872c2017-07-28 07:29:12 -070037// Produces "[\"a\",\"b\",\"c\"]". Works for vectors of both const char* and
hbos615d3012016-08-24 01:33:13 -070038// std::string element types.
Yves Gerey665174f2018-06-19 15:03:05 +020039template <typename T>
hbos615d3012016-08-24 01:33:13 -070040std::string VectorOfStringsToString(const std::vector<T>& strings) {
41 if (strings.empty())
ehmaldonado35a872c2017-07-28 07:29:12 -070042 return "[]";
hbos615d3012016-08-24 01:33:13 -070043 std::ostringstream oss;
Jonas Olsson6b1985d2018-07-05 11:59:48 +020044 oss << "[\"" << rtc::ToString(strings[0]) << '\"';
hbos615d3012016-08-24 01:33:13 -070045 for (size_t i = 1; i < strings.size(); ++i) {
Jonas Olsson6b1985d2018-07-05 11:59:48 +020046 oss << ",\"" << rtc::ToString(strings[i]) << '\"';
hbos615d3012016-08-24 01:33:13 -070047 }
ehmaldonado35a872c2017-07-28 07:29:12 -070048 oss << "]";
49 return oss.str();
50}
51
52template <typename T>
53std::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
62template <typename T>
63std::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 << "]";
hbos615d3012016-08-24 01:33:13 -070072 return oss.str();
73}
74
75} // namespace
76
hbos67c8bc42016-10-25 04:31:23 -070077bool RTCStats::operator==(const RTCStats& other) const {
hbos0583b282016-11-30 01:50:14 -080078 if (type() != other.type() || id() != other.id())
hbos67c8bc42016-10-25 04:31:23 -070079 return false;
hbos67c8bc42016-10-25 04:31:23 -070080 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
94bool RTCStats::operator!=(const RTCStats& other) const {
95 return !(*this == other);
96}
97
ehmaldonado35a872c2017-07-28 07:29:12 -070098std::string RTCStats::ToJson() const {
hbos615d3012016-08-24 01:33:13 -070099 std::ostringstream oss;
ehmaldonado35a872c2017-07-28 07:29:12 -0700100 oss << "{\"type\":\"" << type() << "\","
101 << "\"id\":\"" << id_ << "\","
102 << "\"timestamp\":" << timestamp_us_;
hbos615d3012016-08-24 01:33:13 -0700103 for (const RTCStatsMemberInterface* member : Members()) {
hbos615d3012016-08-24 01:33:13 -0700104 if (member->is_defined()) {
ehmaldonado35a872c2017-07-28 07:29:12 -0700105 oss << ",\"" << member->name() << "\":";
hbos615d3012016-08-24 01:33:13 -0700106 if (member->is_string())
ehmaldonado35a872c2017-07-28 07:29:12 -0700107 oss << '"' << member->ValueToJson() << '"';
hbos615d3012016-08-24 01:33:13 -0700108 else
ehmaldonado35a872c2017-07-28 07:29:12 -0700109 oss << member->ValueToJson();
hbos615d3012016-08-24 01:33:13 -0700110 }
111 }
ehmaldonado35a872c2017-07-28 07:29:12 -0700112 oss << "}";
hbos615d3012016-08-24 01:33:13 -0700113 return oss.str();
114}
115
116std::vector<const RTCStatsMemberInterface*> RTCStats::Members() const {
117 return MembersOfThisObjectAndAncestors(0);
118}
119
120std::vector<const RTCStatsMemberInterface*>
Yves Gerey665174f2018-06-19 15:03:05 +0200121RTCStats::MembersOfThisObjectAndAncestors(size_t additional_capacity) const {
hbos615d3012016-08-24 01:33:13 -0700122 std::vector<const RTCStatsMemberInterface*> members;
123 members.reserve(additional_capacity);
124 return members;
125}
126
ehmaldonado35a872c2017-07-28 07:29:12 -0700127#define WEBRTC_DEFINE_RTCSTATSMEMBER(T, type, is_seq, is_str, to_str, to_json) \
128 template <> \
hbos615d3012016-08-24 01:33:13 -0700129 const RTCStatsMemberInterface::Type RTCStatsMember<T>::kType = \
130 RTCStatsMemberInterface::type; \
ehmaldonado35a872c2017-07-28 07:29:12 -0700131 template <> \
132 bool RTCStatsMember<T>::is_sequence() const { \
133 return is_seq; \
134 } \
135 template <> \
136 bool RTCStatsMember<T>::is_string() const { \
137 return is_str; \
138 } \
139 template <> \
hbos615d3012016-08-24 01:33:13 -0700140 std::string RTCStatsMember<T>::ValueToString() const { \
141 RTC_DCHECK(is_defined_); \
142 return to_str; \
ehmaldonado35a872c2017-07-28 07:29:12 -0700143 } \
144 template <> \
145 std::string RTCStatsMember<T>::ValueToJson() const { \
146 RTC_DCHECK(is_defined_); \
147 return to_json; \
hbos615d3012016-08-24 01:33:13 -0700148 }
149
ehmaldonado35a872c2017-07-28 07:29:12 -0700150WEBRTC_DEFINE_RTCSTATSMEMBER(bool,
151 kBool,
152 false,
153 false,
154 rtc::ToString(value_),
hbosb20f3872016-10-04 14:37:11 -0700155 rtc::ToString(value_));
ehmaldonado35a872c2017-07-28 07:29:12 -0700156WEBRTC_DEFINE_RTCSTATSMEMBER(int32_t,
157 kInt32,
158 false,
159 false,
160 rtc::ToString(value_),
hbos615d3012016-08-24 01:33:13 -0700161 rtc::ToString(value_));
ehmaldonado35a872c2017-07-28 07:29:12 -0700162WEBRTC_DEFINE_RTCSTATSMEMBER(uint32_t,
163 kUint32,
164 false,
165 false,
166 rtc::ToString(value_),
hbos615d3012016-08-24 01:33:13 -0700167 rtc::ToString(value_));
ehmaldonado35a872c2017-07-28 07:29:12 -0700168WEBRTC_DEFINE_RTCSTATSMEMBER(int64_t,
169 kInt64,
170 false,
171 false,
172 rtc::ToString(value_),
173 ToStringAsDouble(value_));
174WEBRTC_DEFINE_RTCSTATSMEMBER(uint64_t,
175 kUint64,
176 false,
177 false,
178 rtc::ToString(value_),
179 ToStringAsDouble(value_));
180WEBRTC_DEFINE_RTCSTATSMEMBER(double,
181 kDouble,
182 false,
183 false,
184 rtc::ToString(value_),
185 ToStringAsDouble(value_));
Yves Gerey665174f2018-06-19 15:03:05 +0200186WEBRTC_DEFINE_RTCSTATSMEMBER(std::string, kString, false, true, value_, value_);
ehmaldonado35a872c2017-07-28 07:29:12 -0700187WEBRTC_DEFINE_RTCSTATSMEMBER(std::vector<bool>,
188 kSequenceBool,
189 true,
190 false,
191 VectorToString(value_),
192 VectorToString(value_));
193WEBRTC_DEFINE_RTCSTATSMEMBER(std::vector<int32_t>,
194 kSequenceInt32,
195 true,
196 false,
197 VectorToString(value_),
198 VectorToString(value_));
199WEBRTC_DEFINE_RTCSTATSMEMBER(std::vector<uint32_t>,
200 kSequenceUint32,
201 true,
202 false,
203 VectorToString(value_),
204 VectorToString(value_));
205WEBRTC_DEFINE_RTCSTATSMEMBER(std::vector<int64_t>,
206 kSequenceInt64,
207 true,
208 false,
209 VectorToString(value_),
210 VectorToStringAsDouble(value_));
211WEBRTC_DEFINE_RTCSTATSMEMBER(std::vector<uint64_t>,
212 kSequenceUint64,
213 true,
214 false,
215 VectorToString(value_),
216 VectorToStringAsDouble(value_));
217WEBRTC_DEFINE_RTCSTATSMEMBER(std::vector<double>,
218 kSequenceDouble,
219 true,
220 false,
221 VectorToString(value_),
222 VectorToStringAsDouble(value_));
223WEBRTC_DEFINE_RTCSTATSMEMBER(std::vector<std::string>,
224 kSequenceString,
225 true,
226 false,
227 VectorOfStringsToString(value_),
228 VectorOfStringsToString(value_));
hbos615d3012016-08-24 01:33:13 -0700229
230} // namespace webrtc