blob: ef3666605689b0d04ca7adc182f8b993760740a6 [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
hbos6ded1902016-11-01 01:50:46 -070013#include <sstream>
14
hbos615d3012016-08-24 01:33:13 -070015#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
hbos67c8bc42016-10-25 04:31:23 -070053bool RTCStats::operator==(const RTCStats& other) const {
54 if (type() != other.type() || id() != other.id() ||
55 timestamp_us() != other.timestamp_us()) {
56 return false;
57 }
58 std::vector<const RTCStatsMemberInterface*> members = Members();
59 std::vector<const RTCStatsMemberInterface*> other_members = other.Members();
60 RTC_DCHECK_EQ(members.size(), other_members.size());
61 for (size_t i = 0; i < members.size(); ++i) {
62 const RTCStatsMemberInterface* member = members[i];
63 const RTCStatsMemberInterface* other_member = other_members[i];
64 RTC_DCHECK_EQ(member->type(), other_member->type());
65 RTC_DCHECK_EQ(member->name(), other_member->name());
66 if (*member != *other_member)
67 return false;
68 }
69 return true;
70}
71
72bool RTCStats::operator!=(const RTCStats& other) const {
73 return !(*this == other);
74}
75
hbos615d3012016-08-24 01:33:13 -070076std::string RTCStats::ToString() const {
77 std::ostringstream oss;
78 oss << type() << " {\n id: \"" << id_ << "\"\n timestamp: "
hbos0e6758d2016-08-31 07:57:36 -070079 << timestamp_us_ << '\n';
hbos615d3012016-08-24 01:33:13 -070080 for (const RTCStatsMemberInterface* member : Members()) {
81 oss << " " << member->name() << ": ";
82 if (member->is_defined()) {
83 if (member->is_string())
84 oss << '"' << member->ValueToString() << "\"\n";
85 else
86 oss << member->ValueToString() << '\n';
87 } else {
88 oss << "undefined\n";
89 }
90 }
91 oss << '}';
92 return oss.str();
93}
94
95std::vector<const RTCStatsMemberInterface*> RTCStats::Members() const {
96 return MembersOfThisObjectAndAncestors(0);
97}
98
99std::vector<const RTCStatsMemberInterface*>
100RTCStats::MembersOfThisObjectAndAncestors(
101 size_t additional_capacity) const {
102 std::vector<const RTCStatsMemberInterface*> members;
103 members.reserve(additional_capacity);
104 return members;
105}
106
107#define WEBRTC_DEFINE_RTCSTATSMEMBER(T, type, is_seq, is_str, to_str) \
108 template<> \
109 const RTCStatsMemberInterface::Type RTCStatsMember<T>::kType = \
110 RTCStatsMemberInterface::type; \
111 template<> \
112 bool RTCStatsMember<T>::is_sequence() const { return is_seq; } \
113 template<> \
114 bool RTCStatsMember<T>::is_string() const { return is_str; } \
115 template<> \
116 std::string RTCStatsMember<T>::ValueToString() const { \
117 RTC_DCHECK(is_defined_); \
118 return to_str; \
119 }
120
hbosb20f3872016-10-04 14:37:11 -0700121WEBRTC_DEFINE_RTCSTATSMEMBER(bool, kBool, false, false,
122 rtc::ToString(value_));
hbos615d3012016-08-24 01:33:13 -0700123WEBRTC_DEFINE_RTCSTATSMEMBER(int32_t, kInt32, false, false,
124 rtc::ToString(value_));
125WEBRTC_DEFINE_RTCSTATSMEMBER(uint32_t, kUint32, false, false,
126 rtc::ToString(value_));
127WEBRTC_DEFINE_RTCSTATSMEMBER(int64_t, kInt64, false, false,
128 rtc::ToString(value_));
129WEBRTC_DEFINE_RTCSTATSMEMBER(uint64_t, kUint64, false, false,
130 rtc::ToString(value_));
131WEBRTC_DEFINE_RTCSTATSMEMBER(double, kDouble, false, false,
132 rtc::ToString(value_));
hbos615d3012016-08-24 01:33:13 -0700133WEBRTC_DEFINE_RTCSTATSMEMBER(std::string, kString, false, true,
134 value_);
135WEBRTC_DEFINE_RTCSTATSMEMBER(
hbosb20f3872016-10-04 14:37:11 -0700136 std::vector<bool>, kSequenceBool, true, false,
137 VectorToString(value_));
138WEBRTC_DEFINE_RTCSTATSMEMBER(
hbos615d3012016-08-24 01:33:13 -0700139 std::vector<int32_t>, kSequenceInt32, true, false,
140 VectorToString(value_));
141WEBRTC_DEFINE_RTCSTATSMEMBER(
142 std::vector<uint32_t>, kSequenceUint32, true, false,
143 VectorToString(value_));
144WEBRTC_DEFINE_RTCSTATSMEMBER(
145 std::vector<int64_t>, kSequenceInt64, true, false,
146 VectorToString(value_));
147WEBRTC_DEFINE_RTCSTATSMEMBER(
148 std::vector<uint64_t>, kSequenceUint64, true, false,
149 VectorToString(value_));
150WEBRTC_DEFINE_RTCSTATSMEMBER(
151 std::vector<double>, kSequenceDouble, true, false,
152 VectorToString(value_));
153WEBRTC_DEFINE_RTCSTATSMEMBER(
hbos615d3012016-08-24 01:33:13 -0700154 std::vector<std::string>, kSequenceString, true, false,
155 VectorOfStringsToString(value_));
156
157} // namespace webrtc