blob: fb9740a12e894581666cfe7ee4bbd328df45b7bf [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
hbos74e1a4f2016-09-15 23:33:01 -070011#include "webrtc/api/stats/rtcstats.h"
hbos615d3012016-08-24 01:33:13 -070012
13#include "webrtc/base/stringencode.h"
14
15namespace webrtc {
16
17namespace {
18
19// Produces "{ a, b, c }". Works for non-vector |RTCStatsMemberInterface::Type|
20// types.
21template<typename T>
22std::string VectorToString(const std::vector<T>& vector) {
23 if (vector.empty())
24 return "{}";
25 std::ostringstream oss;
26 oss << "{ " << rtc::ToString<T>(vector[0]);
27 for (size_t i = 1; i < vector.size(); ++i) {
28 oss << ", " << rtc::ToString<T>(vector[i]);
29 }
30 oss << " }";
31 return oss.str();
32}
33
34// Produces "{ \"a\", \"b\", \"c\" }". Works for vectors of both const char* and
35// std::string element types.
36template<typename T>
37std::string VectorOfStringsToString(const std::vector<T>& strings) {
38 if (strings.empty())
39 return "{}";
40 std::ostringstream oss;
41 oss << "{ \"" << rtc::ToString<T>(strings[0]) << '\"';
42 for (size_t i = 1; i < strings.size(); ++i) {
43 oss << ", \"" << rtc::ToString<T>(strings[i]) << '\"';
44 }
45 oss << " }";
46 return oss.str();
47}
48
49} // namespace
50
hbos67c8bc42016-10-25 04:31:23 -070051bool RTCStats::operator==(const RTCStats& other) const {
52 if (type() != other.type() || id() != other.id() ||
53 timestamp_us() != other.timestamp_us()) {
54 return false;
55 }
56 std::vector<const RTCStatsMemberInterface*> members = Members();
57 std::vector<const RTCStatsMemberInterface*> other_members = other.Members();
58 RTC_DCHECK_EQ(members.size(), other_members.size());
59 for (size_t i = 0; i < members.size(); ++i) {
60 const RTCStatsMemberInterface* member = members[i];
61 const RTCStatsMemberInterface* other_member = other_members[i];
62 RTC_DCHECK_EQ(member->type(), other_member->type());
63 RTC_DCHECK_EQ(member->name(), other_member->name());
64 if (*member != *other_member)
65 return false;
66 }
67 return true;
68}
69
70bool RTCStats::operator!=(const RTCStats& other) const {
71 return !(*this == other);
72}
73
hbos615d3012016-08-24 01:33:13 -070074std::string RTCStats::ToString() const {
75 std::ostringstream oss;
76 oss << type() << " {\n id: \"" << id_ << "\"\n timestamp: "
hbos0e6758d2016-08-31 07:57:36 -070077 << timestamp_us_ << '\n';
hbos615d3012016-08-24 01:33:13 -070078 for (const RTCStatsMemberInterface* member : Members()) {
79 oss << " " << member->name() << ": ";
80 if (member->is_defined()) {
81 if (member->is_string())
82 oss << '"' << member->ValueToString() << "\"\n";
83 else
84 oss << member->ValueToString() << '\n';
85 } else {
86 oss << "undefined\n";
87 }
88 }
89 oss << '}';
90 return oss.str();
91}
92
93std::vector<const RTCStatsMemberInterface*> RTCStats::Members() const {
94 return MembersOfThisObjectAndAncestors(0);
95}
96
97std::vector<const RTCStatsMemberInterface*>
98RTCStats::MembersOfThisObjectAndAncestors(
99 size_t additional_capacity) const {
100 std::vector<const RTCStatsMemberInterface*> members;
101 members.reserve(additional_capacity);
102 return members;
103}
104
105#define WEBRTC_DEFINE_RTCSTATSMEMBER(T, type, is_seq, is_str, to_str) \
106 template<> \
107 const RTCStatsMemberInterface::Type RTCStatsMember<T>::kType = \
108 RTCStatsMemberInterface::type; \
109 template<> \
110 bool RTCStatsMember<T>::is_sequence() const { return is_seq; } \
111 template<> \
112 bool RTCStatsMember<T>::is_string() const { return is_str; } \
113 template<> \
114 std::string RTCStatsMember<T>::ValueToString() const { \
115 RTC_DCHECK(is_defined_); \
116 return to_str; \
117 }
118
hbosb20f3872016-10-04 14:37:11 -0700119WEBRTC_DEFINE_RTCSTATSMEMBER(bool, kBool, false, false,
120 rtc::ToString(value_));
hbos615d3012016-08-24 01:33:13 -0700121WEBRTC_DEFINE_RTCSTATSMEMBER(int32_t, kInt32, false, false,
122 rtc::ToString(value_));
123WEBRTC_DEFINE_RTCSTATSMEMBER(uint32_t, kUint32, false, false,
124 rtc::ToString(value_));
125WEBRTC_DEFINE_RTCSTATSMEMBER(int64_t, kInt64, false, false,
126 rtc::ToString(value_));
127WEBRTC_DEFINE_RTCSTATSMEMBER(uint64_t, kUint64, false, false,
128 rtc::ToString(value_));
129WEBRTC_DEFINE_RTCSTATSMEMBER(double, kDouble, false, false,
130 rtc::ToString(value_));
hbos615d3012016-08-24 01:33:13 -0700131WEBRTC_DEFINE_RTCSTATSMEMBER(std::string, kString, false, true,
132 value_);
133WEBRTC_DEFINE_RTCSTATSMEMBER(
hbosb20f3872016-10-04 14:37:11 -0700134 std::vector<bool>, kSequenceBool, true, false,
135 VectorToString(value_));
136WEBRTC_DEFINE_RTCSTATSMEMBER(
hbos615d3012016-08-24 01:33:13 -0700137 std::vector<int32_t>, kSequenceInt32, true, false,
138 VectorToString(value_));
139WEBRTC_DEFINE_RTCSTATSMEMBER(
140 std::vector<uint32_t>, kSequenceUint32, true, false,
141 VectorToString(value_));
142WEBRTC_DEFINE_RTCSTATSMEMBER(
143 std::vector<int64_t>, kSequenceInt64, true, false,
144 VectorToString(value_));
145WEBRTC_DEFINE_RTCSTATSMEMBER(
146 std::vector<uint64_t>, kSequenceUint64, true, false,
147 VectorToString(value_));
148WEBRTC_DEFINE_RTCSTATSMEMBER(
149 std::vector<double>, kSequenceDouble, true, false,
150 VectorToString(value_));
151WEBRTC_DEFINE_RTCSTATSMEMBER(
hbos615d3012016-08-24 01:33:13 -0700152 std::vector<std::string>, kSequenceString, true, false,
153 VectorOfStringsToString(value_));
154
155} // namespace webrtc