blob: 1968dd030e0fbc7b5f3300837f2ec880f538659a [file] [log] [blame]
hbos3d7ad3d2016-08-24 08:33:131/*
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
hbos42204212016-09-16 06:33:0111#include "webrtc/api/stats/rtcstats.h"
hbos3d7ad3d2016-08-24 08:33:1312
hbosa7ab11e2016-11-01 08:50:4613#include <sstream>
14
hbos3d7ad3d2016-08-24 08:33:1315#include "webrtc/base/stringencode.h"
16
17namespace webrtc {
18
19namespace {
20
21// Produces "{ a, b, c }". Works for non-vector |RTCStatsMemberInterface::Type|
22// types.
23template<typename T>
24std::string VectorToString(const std::vector<T>& vector) {
25 if (vector.empty())
26 return "{}";
27 std::ostringstream oss;
28 oss << "{ " << rtc::ToString<T>(vector[0]);
29 for (size_t i = 1; i < vector.size(); ++i) {
30 oss << ", " << rtc::ToString<T>(vector[i]);
31 }
32 oss << " }";
33 return oss.str();
34}
35
36// Produces "{ \"a\", \"b\", \"c\" }". Works for vectors of both const char* and
37// std::string element types.
38template<typename T>
39std::string VectorOfStringsToString(const std::vector<T>& strings) {
40 if (strings.empty())
41 return "{}";
42 std::ostringstream oss;
43 oss << "{ \"" << rtc::ToString<T>(strings[0]) << '\"';
44 for (size_t i = 1; i < strings.size(); ++i) {
45 oss << ", \"" << rtc::ToString<T>(strings[i]) << '\"';
46 }
47 oss << " }";
48 return oss.str();
49}
50
51} // namespace
52
hbos243a4942016-10-25 11:31:2353bool RTCStats::operator==(const RTCStats& other) const {
hbos3e7172f2016-11-30 09:50:1454 if (type() != other.type() || id() != other.id())
hbos243a4942016-10-25 11:31:2355 return false;
hbos243a4942016-10-25 11:31:2356 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
hbos3d7ad3d2016-08-24 08:33:1374std::string RTCStats::ToString() const {
75 std::ostringstream oss;
76 oss << type() << " {\n id: \"" << id_ << "\"\n timestamp: "
hbos7831d0d2016-08-31 14:57:3677 << timestamp_us_ << '\n';
hbos3d7ad3d2016-08-24 08:33:1378 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
hbosf4f0f242016-10-04 21:37:11119WEBRTC_DEFINE_RTCSTATSMEMBER(bool, kBool, false, false,
120 rtc::ToString(value_));
hbos3d7ad3d2016-08-24 08:33:13121WEBRTC_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_));
hbos3d7ad3d2016-08-24 08:33:13131WEBRTC_DEFINE_RTCSTATSMEMBER(std::string, kString, false, true,
132 value_);
133WEBRTC_DEFINE_RTCSTATSMEMBER(
hbosf4f0f242016-10-04 21:37:11134 std::vector<bool>, kSequenceBool, true, false,
135 VectorToString(value_));
136WEBRTC_DEFINE_RTCSTATSMEMBER(
hbos3d7ad3d2016-08-24 08:33:13137 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(
hbos3d7ad3d2016-08-24 08:33:13152 std::vector<std::string>, kSequenceString, true, false,
153 VectorOfStringsToString(value_));
154
155} // namespace webrtc