You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

232 lines
8.9 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  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. #include "api/stats/rtc_stats.h"
  11. #include <cstdio>
  12. #include "rtc_base/arraysize.h"
  13. #include "rtc_base/string_encode.h"
  14. #include "rtc_base/strings/string_builder.h"
  15. namespace webrtc {
  16. namespace {
  17. // Produces "[a,b,c]". Works for non-vector |RTCStatsMemberInterface::Type|
  18. // types.
  19. template <typename T>
  20. std::string VectorToString(const std::vector<T>& vector) {
  21. rtc::StringBuilder sb;
  22. sb << "[";
  23. const char* separator = "";
  24. for (const T& element : vector) {
  25. sb << separator << rtc::ToString(element);
  26. separator = ",";
  27. }
  28. sb << "]";
  29. return sb.Release();
  30. }
  31. // Produces "[\"a\",\"b\",\"c\"]". Works for vectors of both const char* and
  32. // std::string element types.
  33. template <typename T>
  34. std::string VectorOfStringsToString(const std::vector<T>& strings) {
  35. rtc::StringBuilder sb;
  36. sb << "[";
  37. const char* separator = "";
  38. for (const T& element : strings) {
  39. sb << separator << "\"" << rtc::ToString(element) << "\"";
  40. separator = ",";
  41. }
  42. sb << "]";
  43. return sb.Release();
  44. }
  45. template <typename T>
  46. std::string ToStringAsDouble(const T value) {
  47. // JSON represents numbers as floating point numbers with about 15 decimal
  48. // digits of precision.
  49. char buf[32];
  50. const int len = std::snprintf(&buf[0], arraysize(buf), "%.16g",
  51. static_cast<double>(value));
  52. RTC_DCHECK_LE(len, arraysize(buf));
  53. return std::string(&buf[0], len);
  54. }
  55. template <typename T>
  56. std::string VectorToStringAsDouble(const std::vector<T>& vector) {
  57. rtc::StringBuilder sb;
  58. sb << "[";
  59. const char* separator = "";
  60. for (const T& element : vector) {
  61. sb << separator << ToStringAsDouble<T>(element);
  62. separator = ",";
  63. }
  64. sb << "]";
  65. return sb.Release();
  66. }
  67. } // namespace
  68. bool RTCStats::operator==(const RTCStats& other) const {
  69. if (type() != other.type() || id() != other.id())
  70. return false;
  71. std::vector<const RTCStatsMemberInterface*> members = Members();
  72. std::vector<const RTCStatsMemberInterface*> other_members = other.Members();
  73. RTC_DCHECK_EQ(members.size(), other_members.size());
  74. for (size_t i = 0; i < members.size(); ++i) {
  75. const RTCStatsMemberInterface* member = members[i];
  76. const RTCStatsMemberInterface* other_member = other_members[i];
  77. RTC_DCHECK_EQ(member->type(), other_member->type());
  78. RTC_DCHECK_EQ(member->name(), other_member->name());
  79. if (*member != *other_member)
  80. return false;
  81. }
  82. return true;
  83. }
  84. bool RTCStats::operator!=(const RTCStats& other) const {
  85. return !(*this == other);
  86. }
  87. std::string RTCStats::ToJson() const {
  88. rtc::StringBuilder sb;
  89. sb << "{\"type\":\"" << type() << "\","
  90. << "\"id\":\"" << id_ << "\","
  91. << "\"timestamp\":" << timestamp_us_;
  92. for (const RTCStatsMemberInterface* member : Members()) {
  93. if (member->is_defined()) {
  94. sb << ",\"" << member->name() << "\":";
  95. if (member->is_string())
  96. sb << "\"" << member->ValueToJson() << "\"";
  97. else
  98. sb << member->ValueToJson();
  99. }
  100. }
  101. sb << "}";
  102. return sb.Release();
  103. }
  104. std::vector<const RTCStatsMemberInterface*> RTCStats::Members() const {
  105. return MembersOfThisObjectAndAncestors(0);
  106. }
  107. std::vector<const RTCStatsMemberInterface*>
  108. RTCStats::MembersOfThisObjectAndAncestors(size_t additional_capacity) const {
  109. std::vector<const RTCStatsMemberInterface*> members;
  110. members.reserve(additional_capacity);
  111. return members;
  112. }
  113. #define WEBRTC_DEFINE_RTCSTATSMEMBER(T, type, is_seq, is_str, to_str, to_json) \
  114. template <> \
  115. const RTCStatsMemberInterface::Type RTCStatsMember<T>::kType = \
  116. RTCStatsMemberInterface::type; \
  117. template <> \
  118. bool RTCStatsMember<T>::is_sequence() const { \
  119. return is_seq; \
  120. } \
  121. template <> \
  122. bool RTCStatsMember<T>::is_string() const { \
  123. return is_str; \
  124. } \
  125. template <> \
  126. std::string RTCStatsMember<T>::ValueToString() const { \
  127. RTC_DCHECK(is_defined_); \
  128. return to_str; \
  129. } \
  130. template <> \
  131. std::string RTCStatsMember<T>::ValueToJson() const { \
  132. RTC_DCHECK(is_defined_); \
  133. return to_json; \
  134. }
  135. WEBRTC_DEFINE_RTCSTATSMEMBER(bool,
  136. kBool,
  137. false,
  138. false,
  139. rtc::ToString(value_),
  140. rtc::ToString(value_))
  141. WEBRTC_DEFINE_RTCSTATSMEMBER(int32_t,
  142. kInt32,
  143. false,
  144. false,
  145. rtc::ToString(value_),
  146. rtc::ToString(value_))
  147. WEBRTC_DEFINE_RTCSTATSMEMBER(uint32_t,
  148. kUint32,
  149. false,
  150. false,
  151. rtc::ToString(value_),
  152. rtc::ToString(value_))
  153. WEBRTC_DEFINE_RTCSTATSMEMBER(int64_t,
  154. kInt64,
  155. false,
  156. false,
  157. rtc::ToString(value_),
  158. ToStringAsDouble(value_))
  159. WEBRTC_DEFINE_RTCSTATSMEMBER(uint64_t,
  160. kUint64,
  161. false,
  162. false,
  163. rtc::ToString(value_),
  164. ToStringAsDouble(value_))
  165. WEBRTC_DEFINE_RTCSTATSMEMBER(double,
  166. kDouble,
  167. false,
  168. false,
  169. rtc::ToString(value_),
  170. ToStringAsDouble(value_))
  171. WEBRTC_DEFINE_RTCSTATSMEMBER(std::string, kString, false, true, value_, value_)
  172. WEBRTC_DEFINE_RTCSTATSMEMBER(std::vector<bool>,
  173. kSequenceBool,
  174. true,
  175. false,
  176. VectorToString(value_),
  177. VectorToString(value_))
  178. WEBRTC_DEFINE_RTCSTATSMEMBER(std::vector<int32_t>,
  179. kSequenceInt32,
  180. true,
  181. false,
  182. VectorToString(value_),
  183. VectorToString(value_))
  184. WEBRTC_DEFINE_RTCSTATSMEMBER(std::vector<uint32_t>,
  185. kSequenceUint32,
  186. true,
  187. false,
  188. VectorToString(value_),
  189. VectorToString(value_))
  190. WEBRTC_DEFINE_RTCSTATSMEMBER(std::vector<int64_t>,
  191. kSequenceInt64,
  192. true,
  193. false,
  194. VectorToString(value_),
  195. VectorToStringAsDouble(value_))
  196. WEBRTC_DEFINE_RTCSTATSMEMBER(std::vector<uint64_t>,
  197. kSequenceUint64,
  198. true,
  199. false,
  200. VectorToString(value_),
  201. VectorToStringAsDouble(value_))
  202. WEBRTC_DEFINE_RTCSTATSMEMBER(std::vector<double>,
  203. kSequenceDouble,
  204. true,
  205. false,
  206. VectorToString(value_),
  207. VectorToStringAsDouble(value_))
  208. WEBRTC_DEFINE_RTCSTATSMEMBER(std::vector<std::string>,
  209. kSequenceString,
  210. true,
  211. false,
  212. VectorOfStringsToString(value_),
  213. VectorOfStringsToString(value_))
  214. } // namespace webrtc