hbos | 3d7ad3d | 2016-08-24 08:33:13 | [diff] [blame] | 1 | /* |
| 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 | |
hbos | 4220421 | 2016-09-16 06:33:01 | [diff] [blame] | 11 | #include "webrtc/api/stats/rtcstats.h" |
hbos | 3d7ad3d | 2016-08-24 08:33:13 | [diff] [blame] | 12 | |
hbos | a7ab11e | 2016-11-01 08:50:46 | [diff] [blame] | 13 | #include <sstream> |
| 14 | |
hbos | 3d7ad3d | 2016-08-24 08:33:13 | [diff] [blame] | 15 | #include "webrtc/base/stringencode.h" |
| 16 | |
| 17 | namespace webrtc { |
| 18 | |
| 19 | namespace { |
| 20 | |
| 21 | // Produces "{ a, b, c }". Works for non-vector |RTCStatsMemberInterface::Type| |
| 22 | // types. |
| 23 | template<typename T> |
| 24 | std::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. |
| 38 | template<typename T> |
| 39 | std::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 | |
hbos | 243a494 | 2016-10-25 11:31:23 | [diff] [blame] | 53 | bool RTCStats::operator==(const RTCStats& other) const { |
hbos | 3e7172f | 2016-11-30 09:50:14 | [diff] [blame] | 54 | if (type() != other.type() || id() != other.id()) |
hbos | 243a494 | 2016-10-25 11:31:23 | [diff] [blame] | 55 | return false; |
hbos | 243a494 | 2016-10-25 11:31:23 | [diff] [blame] | 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 | |
| 70 | bool RTCStats::operator!=(const RTCStats& other) const { |
| 71 | return !(*this == other); |
| 72 | } |
| 73 | |
hbos | 3d7ad3d | 2016-08-24 08:33:13 | [diff] [blame] | 74 | std::string RTCStats::ToString() const { |
| 75 | std::ostringstream oss; |
| 76 | oss << type() << " {\n id: \"" << id_ << "\"\n timestamp: " |
hbos | 7831d0d | 2016-08-31 14:57:36 | [diff] [blame] | 77 | << timestamp_us_ << '\n'; |
hbos | 3d7ad3d | 2016-08-24 08:33:13 | [diff] [blame] | 78 | 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 | |
| 93 | std::vector<const RTCStatsMemberInterface*> RTCStats::Members() const { |
| 94 | return MembersOfThisObjectAndAncestors(0); |
| 95 | } |
| 96 | |
| 97 | std::vector<const RTCStatsMemberInterface*> |
| 98 | RTCStats::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 | |
hbos | f4f0f24 | 2016-10-04 21:37:11 | [diff] [blame] | 119 | WEBRTC_DEFINE_RTCSTATSMEMBER(bool, kBool, false, false, |
| 120 | rtc::ToString(value_)); |
hbos | 3d7ad3d | 2016-08-24 08:33:13 | [diff] [blame] | 121 | WEBRTC_DEFINE_RTCSTATSMEMBER(int32_t, kInt32, false, false, |
| 122 | rtc::ToString(value_)); |
| 123 | WEBRTC_DEFINE_RTCSTATSMEMBER(uint32_t, kUint32, false, false, |
| 124 | rtc::ToString(value_)); |
| 125 | WEBRTC_DEFINE_RTCSTATSMEMBER(int64_t, kInt64, false, false, |
| 126 | rtc::ToString(value_)); |
| 127 | WEBRTC_DEFINE_RTCSTATSMEMBER(uint64_t, kUint64, false, false, |
| 128 | rtc::ToString(value_)); |
| 129 | WEBRTC_DEFINE_RTCSTATSMEMBER(double, kDouble, false, false, |
| 130 | rtc::ToString(value_)); |
hbos | 3d7ad3d | 2016-08-24 08:33:13 | [diff] [blame] | 131 | WEBRTC_DEFINE_RTCSTATSMEMBER(std::string, kString, false, true, |
| 132 | value_); |
| 133 | WEBRTC_DEFINE_RTCSTATSMEMBER( |
hbos | f4f0f24 | 2016-10-04 21:37:11 | [diff] [blame] | 134 | std::vector<bool>, kSequenceBool, true, false, |
| 135 | VectorToString(value_)); |
| 136 | WEBRTC_DEFINE_RTCSTATSMEMBER( |
hbos | 3d7ad3d | 2016-08-24 08:33:13 | [diff] [blame] | 137 | std::vector<int32_t>, kSequenceInt32, true, false, |
| 138 | VectorToString(value_)); |
| 139 | WEBRTC_DEFINE_RTCSTATSMEMBER( |
| 140 | std::vector<uint32_t>, kSequenceUint32, true, false, |
| 141 | VectorToString(value_)); |
| 142 | WEBRTC_DEFINE_RTCSTATSMEMBER( |
| 143 | std::vector<int64_t>, kSequenceInt64, true, false, |
| 144 | VectorToString(value_)); |
| 145 | WEBRTC_DEFINE_RTCSTATSMEMBER( |
| 146 | std::vector<uint64_t>, kSequenceUint64, true, false, |
| 147 | VectorToString(value_)); |
| 148 | WEBRTC_DEFINE_RTCSTATSMEMBER( |
| 149 | std::vector<double>, kSequenceDouble, true, false, |
| 150 | VectorToString(value_)); |
| 151 | WEBRTC_DEFINE_RTCSTATSMEMBER( |
hbos | 3d7ad3d | 2016-08-24 08:33:13 | [diff] [blame] | 152 | std::vector<std::string>, kSequenceString, true, false, |
| 153 | VectorOfStringsToString(value_)); |
| 154 | |
| 155 | } // namespace webrtc |