blob: e4460bd90ce7ca6921b44340c248c82111a89cfa [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.
24template<typename T>
25std::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;
ehmaldonado35a872c2017-07-28 07:29:12 -070029 oss << "[" << rtc::ToString<T>(vector[0]);
hbos615d3012016-08-24 01:33:13 -070030 for (size_t i = 1; i < vector.size(); ++i) {
ehmaldonado35a872c2017-07-28 07:29:12 -070031 oss << "," << rtc::ToString<T>(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.
39template<typename T>
40std::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;
ehmaldonado35a872c2017-07-28 07:29:12 -070044 oss << "[\"" << rtc::ToString<T>(strings[0]) << '\"';
hbos615d3012016-08-24 01:33:13 -070045 for (size_t i = 1; i < strings.size(); ++i) {
ehmaldonado35a872c2017-07-28 07:29:12 -070046 oss << ",\"" << rtc::ToString<T>(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*>
121RTCStats::MembersOfThisObjectAndAncestors(
122 size_t additional_capacity) const {
123 std::vector<const RTCStatsMemberInterface*> members;
124 members.reserve(additional_capacity);
125 return members;
126}
127
ehmaldonado35a872c2017-07-28 07:29:12 -0700128#define WEBRTC_DEFINE_RTCSTATSMEMBER(T, type, is_seq, is_str, to_str, to_json) \
129 template <> \
hbos615d3012016-08-24 01:33:13 -0700130 const RTCStatsMemberInterface::Type RTCStatsMember<T>::kType = \
131 RTCStatsMemberInterface::type; \
ehmaldonado35a872c2017-07-28 07:29:12 -0700132 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 <> \
hbos615d3012016-08-24 01:33:13 -0700141 std::string RTCStatsMember<T>::ValueToString() const { \
142 RTC_DCHECK(is_defined_); \
143 return to_str; \
ehmaldonado35a872c2017-07-28 07:29:12 -0700144 } \
145 template <> \
146 std::string RTCStatsMember<T>::ValueToJson() const { \
147 RTC_DCHECK(is_defined_); \
148 return to_json; \
hbos615d3012016-08-24 01:33:13 -0700149 }
150
ehmaldonado35a872c2017-07-28 07:29:12 -0700151WEBRTC_DEFINE_RTCSTATSMEMBER(bool,
152 kBool,
153 false,
154 false,
155 rtc::ToString(value_),
hbosb20f3872016-10-04 14:37:11 -0700156 rtc::ToString(value_));
ehmaldonado35a872c2017-07-28 07:29:12 -0700157WEBRTC_DEFINE_RTCSTATSMEMBER(int32_t,
158 kInt32,
159 false,
160 false,
161 rtc::ToString(value_),
hbos615d3012016-08-24 01:33:13 -0700162 rtc::ToString(value_));
ehmaldonado35a872c2017-07-28 07:29:12 -0700163WEBRTC_DEFINE_RTCSTATSMEMBER(uint32_t,
164 kUint32,
165 false,
166 false,
167 rtc::ToString(value_),
hbos615d3012016-08-24 01:33:13 -0700168 rtc::ToString(value_));
ehmaldonado35a872c2017-07-28 07:29:12 -0700169WEBRTC_DEFINE_RTCSTATSMEMBER(int64_t,
170 kInt64,
171 false,
172 false,
173 rtc::ToString(value_),
174 ToStringAsDouble(value_));
175WEBRTC_DEFINE_RTCSTATSMEMBER(uint64_t,
176 kUint64,
177 false,
178 false,
179 rtc::ToString(value_),
180 ToStringAsDouble(value_));
181WEBRTC_DEFINE_RTCSTATSMEMBER(double,
182 kDouble,
183 false,
184 false,
185 rtc::ToString(value_),
186 ToStringAsDouble(value_));
187WEBRTC_DEFINE_RTCSTATSMEMBER(std::string,
188 kString,
189 false,
190 true,
191 value_,
hbos615d3012016-08-24 01:33:13 -0700192 value_);
ehmaldonado35a872c2017-07-28 07:29:12 -0700193WEBRTC_DEFINE_RTCSTATSMEMBER(std::vector<bool>,
194 kSequenceBool,
195 true,
196 false,
197 VectorToString(value_),
198 VectorToString(value_));
199WEBRTC_DEFINE_RTCSTATSMEMBER(std::vector<int32_t>,
200 kSequenceInt32,
201 true,
202 false,
203 VectorToString(value_),
204 VectorToString(value_));
205WEBRTC_DEFINE_RTCSTATSMEMBER(std::vector<uint32_t>,
206 kSequenceUint32,
207 true,
208 false,
209 VectorToString(value_),
210 VectorToString(value_));
211WEBRTC_DEFINE_RTCSTATSMEMBER(std::vector<int64_t>,
212 kSequenceInt64,
213 true,
214 false,
215 VectorToString(value_),
216 VectorToStringAsDouble(value_));
217WEBRTC_DEFINE_RTCSTATSMEMBER(std::vector<uint64_t>,
218 kSequenceUint64,
219 true,
220 false,
221 VectorToString(value_),
222 VectorToStringAsDouble(value_));
223WEBRTC_DEFINE_RTCSTATSMEMBER(std::vector<double>,
224 kSequenceDouble,
225 true,
226 false,
227 VectorToString(value_),
228 VectorToStringAsDouble(value_));
229WEBRTC_DEFINE_RTCSTATSMEMBER(std::vector<std::string>,
230 kSequenceString,
231 true,
232 false,
233 VectorOfStringsToString(value_),
234 VectorOfStringsToString(value_));
hbos615d3012016-08-24 01:33:13 -0700235
236} // namespace webrtc