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