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.

395 lines
16 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. #ifndef API_STATS_RTC_STATS_H_
  11. #define API_STATS_RTC_STATS_H_
  12. #include <stddef.h>
  13. #include <stdint.h>
  14. #include <memory>
  15. #include <string>
  16. #include <utility>
  17. #include <vector>
  18. #include "rtc_base/checks.h"
  19. #include "rtc_base/system/rtc_export.h"
  20. namespace webrtc {
  21. class RTCStatsMemberInterface;
  22. // Abstract base class for RTCStats-derived dictionaries, see
  23. // https://w3c.github.io/webrtc-stats/.
  24. //
  25. // All derived classes must have the following static variable defined:
  26. // static const char kType[];
  27. // It is used as a unique class identifier and a string representation of the
  28. // class type, see https://w3c.github.io/webrtc-stats/#rtcstatstype-str*.
  29. // Use the |WEBRTC_RTCSTATS_IMPL| macro when implementing subclasses, see macro
  30. // for details.
  31. //
  32. // Derived classes list their dictionary members, RTCStatsMember<T>, as public
  33. // fields, allowing the following:
  34. //
  35. // RTCFooStats foo("fooId", GetCurrentTime());
  36. // foo.bar = 42;
  37. // foo.baz = std::vector<std::string>();
  38. // foo.baz->push_back("hello world");
  39. // uint32_t x = *foo.bar;
  40. //
  41. // Pointers to all the members are available with |Members|, allowing iteration:
  42. //
  43. // for (const RTCStatsMemberInterface* member : foo.Members()) {
  44. // printf("%s = %s\n", member->name(), member->ValueToString().c_str());
  45. // }
  46. class RTC_EXPORT RTCStats {
  47. public:
  48. RTCStats(const std::string& id, int64_t timestamp_us)
  49. : id_(id), timestamp_us_(timestamp_us) {}
  50. RTCStats(std::string&& id, int64_t timestamp_us)
  51. : id_(std::move(id)), timestamp_us_(timestamp_us) {}
  52. virtual ~RTCStats() {}
  53. virtual std::unique_ptr<RTCStats> copy() const = 0;
  54. const std::string& id() const { return id_; }
  55. // Time relative to the UNIX epoch (Jan 1, 1970, UTC), in microseconds.
  56. int64_t timestamp_us() const { return timestamp_us_; }
  57. // Returns the static member variable |kType| of the implementing class.
  58. virtual const char* type() const = 0;
  59. // Returns a vector of pointers to all the |RTCStatsMemberInterface| members
  60. // of this class. This allows for iteration of members. For a given class,
  61. // |Members| always returns the same members in the same order.
  62. std::vector<const RTCStatsMemberInterface*> Members() const;
  63. // Checks if the two stats objects are of the same type and have the same
  64. // member values. Timestamps are not compared. These operators are exposed for
  65. // testing.
  66. bool operator==(const RTCStats& other) const;
  67. bool operator!=(const RTCStats& other) const;
  68. // Creates a JSON readable string representation of the stats
  69. // object, listing all of its members (names and values).
  70. std::string ToJson() const;
  71. // Downcasts the stats object to an |RTCStats| subclass |T|. DCHECKs that the
  72. // object is of type |T|.
  73. template <typename T>
  74. const T& cast_to() const {
  75. RTC_DCHECK_EQ(type(), T::kType);
  76. return static_cast<const T&>(*this);
  77. }
  78. protected:
  79. // Gets a vector of all members of this |RTCStats| object, including members
  80. // derived from parent classes. |additional_capacity| is how many more members
  81. // shall be reserved in the vector (so that subclasses can allocate a vector
  82. // with room for both parent and child members without it having to resize).
  83. virtual std::vector<const RTCStatsMemberInterface*>
  84. MembersOfThisObjectAndAncestors(size_t additional_capacity) const;
  85. std::string const id_;
  86. int64_t timestamp_us_;
  87. };
  88. // All |RTCStats| classes should use these macros.
  89. // |WEBRTC_RTCSTATS_DECL| is placed in a public section of the class definition.
  90. // |WEBRTC_RTCSTATS_IMPL| is placed outside the class definition (in a .cc).
  91. //
  92. // These macros declare (in _DECL) and define (in _IMPL) the static |kType| and
  93. // overrides methods as required by subclasses of |RTCStats|: |copy|, |type| and
  94. // |MembersOfThisObjectAndAncestors|. The |...| argument is a list of addresses
  95. // to each member defined in the implementing class. The list must have at least
  96. // one member.
  97. //
  98. // (Since class names need to be known to implement these methods this cannot be
  99. // part of the base |RTCStats|. While these methods could be implemented using
  100. // templates, that would only work for immediate subclasses. Subclasses of
  101. // subclasses also have to override these methods, resulting in boilerplate
  102. // code. Using a macro avoids this and works for any |RTCStats| class, including
  103. // grandchildren.)
  104. //
  105. // Sample usage:
  106. //
  107. // rtcfoostats.h:
  108. // class RTCFooStats : public RTCStats {
  109. // public:
  110. // WEBRTC_RTCSTATS_DECL();
  111. //
  112. // RTCFooStats(const std::string& id, int64_t timestamp_us);
  113. //
  114. // RTCStatsMember<int32_t> foo;
  115. // RTCStatsMember<int32_t> bar;
  116. // };
  117. //
  118. // rtcfoostats.cc:
  119. // WEBRTC_RTCSTATS_IMPL(RTCFooStats, RTCStats, "foo-stats"
  120. // &foo,
  121. // &bar);
  122. //
  123. // RTCFooStats::RTCFooStats(const std::string& id, int64_t timestamp_us)
  124. // : RTCStats(id, timestamp_us),
  125. // foo("foo"),
  126. // bar("bar") {
  127. // }
  128. //
  129. #define WEBRTC_RTCSTATS_DECL() \
  130. protected: \
  131. std::vector<const webrtc::RTCStatsMemberInterface*> \
  132. MembersOfThisObjectAndAncestors(size_t local_var_additional_capacity) \
  133. const override; \
  134. \
  135. public: \
  136. static const char kType[]; \
  137. \
  138. std::unique_ptr<webrtc::RTCStats> copy() const override; \
  139. const char* type() const override
  140. #define WEBRTC_RTCSTATS_IMPL(this_class, parent_class, type_str, ...) \
  141. const char this_class::kType[] = type_str; \
  142. \
  143. std::unique_ptr<webrtc::RTCStats> this_class::copy() const { \
  144. return std::unique_ptr<webrtc::RTCStats>(new this_class(*this)); \
  145. } \
  146. \
  147. const char* this_class::type() const { return this_class::kType; } \
  148. \
  149. std::vector<const webrtc::RTCStatsMemberInterface*> \
  150. this_class::MembersOfThisObjectAndAncestors( \
  151. size_t local_var_additional_capacity) const { \
  152. const webrtc::RTCStatsMemberInterface* local_var_members[] = { \
  153. __VA_ARGS__}; \
  154. size_t local_var_members_count = \
  155. sizeof(local_var_members) / sizeof(local_var_members[0]); \
  156. std::vector<const webrtc::RTCStatsMemberInterface*> \
  157. local_var_members_vec = parent_class::MembersOfThisObjectAndAncestors( \
  158. local_var_members_count + local_var_additional_capacity); \
  159. RTC_DCHECK_GE( \
  160. local_var_members_vec.capacity() - local_var_members_vec.size(), \
  161. local_var_members_count + local_var_additional_capacity); \
  162. local_var_members_vec.insert(local_var_members_vec.end(), \
  163. &local_var_members[0], \
  164. &local_var_members[local_var_members_count]); \
  165. return local_var_members_vec; \
  166. }
  167. // A version of WEBRTC_RTCSTATS_IMPL() where "..." is omitted, used to avoid a
  168. // compile error on windows. This is used if the stats dictionary does not
  169. // declare any members of its own (but perhaps its parent dictionary does).
  170. #define WEBRTC_RTCSTATS_IMPL_NO_MEMBERS(this_class, parent_class, type_str) \
  171. const char this_class::kType[] = type_str; \
  172. \
  173. std::unique_ptr<webrtc::RTCStats> this_class::copy() const { \
  174. return std::unique_ptr<webrtc::RTCStats>(new this_class(*this)); \
  175. } \
  176. \
  177. const char* this_class::type() const { return this_class::kType; } \
  178. \
  179. std::vector<const webrtc::RTCStatsMemberInterface*> \
  180. this_class::MembersOfThisObjectAndAncestors( \
  181. size_t local_var_additional_capacity) const { \
  182. return parent_class::MembersOfThisObjectAndAncestors(0); \
  183. }
  184. // Non-standard stats members can be exposed to the JavaScript API in Chrome
  185. // e.g. through origin trials. The group ID can be used by the blink layer to
  186. // determine if a stats member should be exposed or not. Multiple non-standard
  187. // stats members can share the same group ID so that they are exposed together.
  188. enum class NonStandardGroupId {
  189. // Group ID used for testing purposes only.
  190. kGroupIdForTesting,
  191. // I2E:
  192. // https://groups.google.com/a/chromium.org/forum/#!topic/blink-dev/hE2B1iItPDk
  193. kRtcAudioJitterBufferMaxPackets,
  194. // I2E:
  195. // https://groups.google.com/a/chromium.org/forum/#!topic/blink-dev/YbhMyqLXXXo
  196. kRtcStatsRelativePacketArrivalDelay,
  197. };
  198. // Interface for |RTCStats| members, which have a name and a value of a type
  199. // defined in a subclass. Only the types listed in |Type| are supported, these
  200. // are implemented by |RTCStatsMember<T>|. The value of a member may be
  201. // undefined, the value can only be read if |is_defined|.
  202. class RTCStatsMemberInterface {
  203. public:
  204. // Member value types.
  205. enum Type {
  206. kBool, // bool
  207. kInt32, // int32_t
  208. kUint32, // uint32_t
  209. kInt64, // int64_t
  210. kUint64, // uint64_t
  211. kDouble, // double
  212. kString, // std::string
  213. kSequenceBool, // std::vector<bool>
  214. kSequenceInt32, // std::vector<int32_t>
  215. kSequenceUint32, // std::vector<uint32_t>
  216. kSequenceInt64, // std::vector<int64_t>
  217. kSequenceUint64, // std::vector<uint64_t>
  218. kSequenceDouble, // std::vector<double>
  219. kSequenceString, // std::vector<std::string>
  220. };
  221. virtual ~RTCStatsMemberInterface() {}
  222. const char* name() const { return name_; }
  223. virtual Type type() const = 0;
  224. virtual bool is_sequence() const = 0;
  225. virtual bool is_string() const = 0;
  226. bool is_defined() const { return is_defined_; }
  227. // Is this part of the stats spec? Used so that chromium can easily filter
  228. // out anything unstandardized.
  229. virtual bool is_standardized() const = 0;
  230. // Non-standard stats members can have group IDs in order to be exposed in
  231. // JavaScript through experiments. Standardized stats have no group IDs.
  232. virtual std::vector<NonStandardGroupId> group_ids() const { return {}; }
  233. // Type and value comparator. The names are not compared. These operators are
  234. // exposed for testing.
  235. virtual bool operator==(const RTCStatsMemberInterface& other) const = 0;
  236. bool operator!=(const RTCStatsMemberInterface& other) const {
  237. return !(*this == other);
  238. }
  239. virtual std::string ValueToString() const = 0;
  240. // This is the same as ValueToString except for kInt64 and kUint64 types,
  241. // where the value is represented as a double instead of as an integer.
  242. // Since JSON stores numbers as floating point numbers, very large integers
  243. // cannot be accurately represented, so we prefer to display them as doubles
  244. // instead.
  245. virtual std::string ValueToJson() const = 0;
  246. template <typename T>
  247. const T& cast_to() const {
  248. RTC_DCHECK_EQ(type(), T::kType);
  249. return static_cast<const T&>(*this);
  250. }
  251. protected:
  252. RTCStatsMemberInterface(const char* name, bool is_defined)
  253. : name_(name), is_defined_(is_defined) {}
  254. const char* const name_;
  255. bool is_defined_;
  256. };
  257. // Template implementation of |RTCStatsMemberInterface|. Every possible |T| is
  258. // specialized in rtcstats.cc, using a different |T| results in a linker error
  259. // (undefined reference to |kType|). The supported types are the ones described
  260. // by |RTCStatsMemberInterface::Type|.
  261. template <typename T>
  262. class RTC_EXPORT RTCStatsMember : public RTCStatsMemberInterface {
  263. public:
  264. static const Type kType;
  265. explicit RTCStatsMember(const char* name)
  266. : RTCStatsMemberInterface(name, /*is_defined=*/false), value_() {}
  267. RTCStatsMember(const char* name, const T& value)
  268. : RTCStatsMemberInterface(name, /*is_defined=*/true), value_(value) {}
  269. RTCStatsMember(const char* name, T&& value)
  270. : RTCStatsMemberInterface(name, /*is_defined=*/true),
  271. value_(std::move(value)) {}
  272. explicit RTCStatsMember(const RTCStatsMember<T>& other)
  273. : RTCStatsMemberInterface(other.name_, other.is_defined_),
  274. value_(other.value_) {}
  275. explicit RTCStatsMember(RTCStatsMember<T>&& other)
  276. : RTCStatsMemberInterface(other.name_, other.is_defined_),
  277. value_(std::move(other.value_)) {}
  278. Type type() const override { return kType; }
  279. bool is_sequence() const override;
  280. bool is_string() const override;
  281. bool is_standardized() const override { return true; }
  282. bool operator==(const RTCStatsMemberInterface& other) const override {
  283. if (type() != other.type() || is_standardized() != other.is_standardized())
  284. return false;
  285. const RTCStatsMember<T>& other_t =
  286. static_cast<const RTCStatsMember<T>&>(other);
  287. if (!is_defined_)
  288. return !other_t.is_defined();
  289. if (!other.is_defined())
  290. return false;
  291. return value_ == other_t.value_;
  292. }
  293. std::string ValueToString() const override;
  294. std::string ValueToJson() const override;
  295. // Assignment operators.
  296. T& operator=(const T& value) {
  297. value_ = value;
  298. is_defined_ = true;
  299. return value_;
  300. }
  301. T& operator=(const T&& value) {
  302. value_ = std::move(value);
  303. is_defined_ = true;
  304. return value_;
  305. }
  306. // Value getters.
  307. T& operator*() {
  308. RTC_DCHECK(is_defined_);
  309. return value_;
  310. }
  311. const T& operator*() const {
  312. RTC_DCHECK(is_defined_);
  313. return value_;
  314. }
  315. // Value getters, arrow operator.
  316. T* operator->() {
  317. RTC_DCHECK(is_defined_);
  318. return &value_;
  319. }
  320. const T* operator->() const {
  321. RTC_DCHECK(is_defined_);
  322. return &value_;
  323. }
  324. private:
  325. T value_;
  326. };
  327. // Using inheritance just so that it's obvious from the member's declaration
  328. // whether it's standardized or not.
  329. template <typename T>
  330. class RTCNonStandardStatsMember : public RTCStatsMember<T> {
  331. public:
  332. explicit RTCNonStandardStatsMember(const char* name)
  333. : RTCStatsMember<T>(name) {}
  334. RTCNonStandardStatsMember(const char* name,
  335. std::initializer_list<NonStandardGroupId> group_ids)
  336. : RTCStatsMember<T>(name), group_ids_(group_ids) {}
  337. RTCNonStandardStatsMember(const char* name, const T& value)
  338. : RTCStatsMember<T>(name, value) {}
  339. RTCNonStandardStatsMember(const char* name, T&& value)
  340. : RTCStatsMember<T>(name, std::move(value)) {}
  341. explicit RTCNonStandardStatsMember(const RTCNonStandardStatsMember<T>& other)
  342. : RTCStatsMember<T>(other), group_ids_(other.group_ids_) {}
  343. explicit RTCNonStandardStatsMember(RTCNonStandardStatsMember<T>&& other)
  344. : group_ids_(std::move(other.group_ids_)),
  345. RTCStatsMember<T>(std::move(other)) {}
  346. bool is_standardized() const override { return false; }
  347. std::vector<NonStandardGroupId> group_ids() const override {
  348. return group_ids_;
  349. }
  350. T& operator=(const T& value) { return RTCStatsMember<T>::operator=(value); }
  351. T& operator=(const T&& value) {
  352. return RTCStatsMember<T>::operator=(std::move(value));
  353. }
  354. private:
  355. std::vector<NonStandardGroupId> group_ids_;
  356. };
  357. } // namespace webrtc
  358. #endif // API_STATS_RTC_STATS_H_