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 | |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 11 | #ifndef API_STATS_RTC_STATS_H_ |
| 12 | #define API_STATS_RTC_STATS_H_ |
hbos | 74e1a4f | 2016-09-15 23:33:01 -0700 | [diff] [blame] | 13 | |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 14 | #include <stddef.h> |
| 15 | #include <stdint.h> |
Jonas Olsson | a4d8737 | 2019-07-05 19:08:33 +0200 | [diff] [blame] | 16 | |
Byoungchan Lee | 0a52ede | 2021-05-22 08:41:02 +0900 | [diff] [blame] | 17 | #include <map> |
hbos | 74e1a4f | 2016-09-15 23:33:01 -0700 | [diff] [blame] | 18 | #include <memory> |
| 19 | #include <string> |
| 20 | #include <utility> |
| 21 | #include <vector> |
| 22 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 23 | #include "rtc_base/checks.h" |
Mirko Bonadei | 276827c | 2018-10-16 14:13:50 +0200 | [diff] [blame] | 24 | #include "rtc_base/system/rtc_export.h" |
Mirko Bonadei | 054f185 | 2019-11-04 16:31:08 +0100 | [diff] [blame] | 25 | #include "rtc_base/system/rtc_export_template.h" |
hbos | 74e1a4f | 2016-09-15 23:33:01 -0700 | [diff] [blame] | 26 | |
| 27 | namespace webrtc { |
| 28 | |
| 29 | class RTCStatsMemberInterface; |
| 30 | |
| 31 | // Abstract base class for RTCStats-derived dictionaries, see |
| 32 | // https://w3c.github.io/webrtc-stats/. |
| 33 | // |
| 34 | // All derived classes must have the following static variable defined: |
| 35 | // static const char kType[]; |
| 36 | // It is used as a unique class identifier and a string representation of the |
| 37 | // class type, see https://w3c.github.io/webrtc-stats/#rtcstatstype-str*. |
Artem Titov | 0e61fdd | 2021-07-25 21:50:14 +0200 | [diff] [blame] | 38 | // Use the `WEBRTC_RTCSTATS_IMPL` macro when implementing subclasses, see macro |
hbos | 74e1a4f | 2016-09-15 23:33:01 -0700 | [diff] [blame] | 39 | // for details. |
| 40 | // |
| 41 | // Derived classes list their dictionary members, RTCStatsMember<T>, as public |
| 42 | // fields, allowing the following: |
| 43 | // |
| 44 | // RTCFooStats foo("fooId", GetCurrentTime()); |
| 45 | // foo.bar = 42; |
| 46 | // foo.baz = std::vector<std::string>(); |
| 47 | // foo.baz->push_back("hello world"); |
| 48 | // uint32_t x = *foo.bar; |
| 49 | // |
Artem Titov | 0e61fdd | 2021-07-25 21:50:14 +0200 | [diff] [blame] | 50 | // Pointers to all the members are available with `Members`, allowing iteration: |
hbos | 74e1a4f | 2016-09-15 23:33:01 -0700 | [diff] [blame] | 51 | // |
| 52 | // for (const RTCStatsMemberInterface* member : foo.Members()) { |
| 53 | // printf("%s = %s\n", member->name(), member->ValueToString().c_str()); |
| 54 | // } |
Mirko Bonadei | 276827c | 2018-10-16 14:13:50 +0200 | [diff] [blame] | 55 | class RTC_EXPORT RTCStats { |
hbos | 74e1a4f | 2016-09-15 23:33:01 -0700 | [diff] [blame] | 56 | public: |
| 57 | RTCStats(const std::string& id, int64_t timestamp_us) |
| 58 | : id_(id), timestamp_us_(timestamp_us) {} |
| 59 | RTCStats(std::string&& id, int64_t timestamp_us) |
| 60 | : id_(std::move(id)), timestamp_us_(timestamp_us) {} |
| 61 | virtual ~RTCStats() {} |
| 62 | |
| 63 | virtual std::unique_ptr<RTCStats> copy() const = 0; |
| 64 | |
| 65 | const std::string& id() const { return id_; } |
| 66 | // Time relative to the UNIX epoch (Jan 1, 1970, UTC), in microseconds. |
| 67 | int64_t timestamp_us() const { return timestamp_us_; } |
Artem Titov | 0e61fdd | 2021-07-25 21:50:14 +0200 | [diff] [blame] | 68 | // Returns the static member variable `kType` of the implementing class. |
hbos | 74e1a4f | 2016-09-15 23:33:01 -0700 | [diff] [blame] | 69 | virtual const char* type() const = 0; |
Artem Titov | 0e61fdd | 2021-07-25 21:50:14 +0200 | [diff] [blame] | 70 | // Returns a vector of pointers to all the `RTCStatsMemberInterface` members |
hbos | 67c8bc4 | 2016-10-25 04:31:23 -0700 | [diff] [blame] | 71 | // of this class. This allows for iteration of members. For a given class, |
Artem Titov | 0e61fdd | 2021-07-25 21:50:14 +0200 | [diff] [blame] | 72 | // `Members` always returns the same members in the same order. |
hbos | 74e1a4f | 2016-09-15 23:33:01 -0700 | [diff] [blame] | 73 | std::vector<const RTCStatsMemberInterface*> Members() const; |
hbos | 67c8bc4 | 2016-10-25 04:31:23 -0700 | [diff] [blame] | 74 | // 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] | 75 | // member values. Timestamps are not compared. These operators are exposed for |
| 76 | // testing. |
hbos | 67c8bc4 | 2016-10-25 04:31:23 -0700 | [diff] [blame] | 77 | bool operator==(const RTCStats& other) const; |
| 78 | bool operator!=(const RTCStats& other) const; |
hbos | 74e1a4f | 2016-09-15 23:33:01 -0700 | [diff] [blame] | 79 | |
ehmaldonado | 35a872c | 2017-07-28 07:29:12 -0700 | [diff] [blame] | 80 | // Creates a JSON readable string representation of the stats |
| 81 | // object, listing all of its members (names and values). |
| 82 | std::string ToJson() const; |
hbos | 74e1a4f | 2016-09-15 23:33:01 -0700 | [diff] [blame] | 83 | |
Artem Titov | 0e61fdd | 2021-07-25 21:50:14 +0200 | [diff] [blame] | 84 | // Downcasts the stats object to an `RTCStats` subclass `T`. DCHECKs that the |
| 85 | // object is of type `T`. |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 86 | template <typename T> |
hbos | 74e1a4f | 2016-09-15 23:33:01 -0700 | [diff] [blame] | 87 | const T& cast_to() const { |
| 88 | RTC_DCHECK_EQ(type(), T::kType); |
| 89 | return static_cast<const T&>(*this); |
| 90 | } |
| 91 | |
| 92 | protected: |
Artem Titov | 0e61fdd | 2021-07-25 21:50:14 +0200 | [diff] [blame] | 93 | // Gets a vector of all members of this `RTCStats` object, including members |
| 94 | // derived from parent classes. `additional_capacity` is how many more members |
hbos | 74e1a4f | 2016-09-15 23:33:01 -0700 | [diff] [blame] | 95 | // shall be reserved in the vector (so that subclasses can allocate a vector |
| 96 | // with room for both parent and child members without it having to resize). |
| 97 | virtual std::vector<const RTCStatsMemberInterface*> |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 98 | MembersOfThisObjectAndAncestors(size_t additional_capacity) const; |
hbos | 74e1a4f | 2016-09-15 23:33:01 -0700 | [diff] [blame] | 99 | |
| 100 | std::string const id_; |
| 101 | int64_t timestamp_us_; |
| 102 | }; |
| 103 | |
Artem Titov | 0e61fdd | 2021-07-25 21:50:14 +0200 | [diff] [blame] | 104 | // All `RTCStats` classes should use these macros. |
| 105 | // `WEBRTC_RTCSTATS_DECL` is placed in a public section of the class definition. |
| 106 | // `WEBRTC_RTCSTATS_IMPL` is placed outside the class definition (in a .cc). |
hbos | 74e1a4f | 2016-09-15 23:33:01 -0700 | [diff] [blame] | 107 | // |
Artem Titov | 0e61fdd | 2021-07-25 21:50:14 +0200 | [diff] [blame] | 108 | // These macros declare (in _DECL) and define (in _IMPL) the static `kType` and |
| 109 | // overrides methods as required by subclasses of `RTCStats`: `copy`, `type` and |
| 110 | // `MembersOfThisObjectAndAncestors`. The |...| argument is a list of addresses |
hbos | fc5e050 | 2016-10-06 02:06:10 -0700 | [diff] [blame] | 111 | // to each member defined in the implementing class. The list must have at least |
| 112 | // one member. |
hbos | 74e1a4f | 2016-09-15 23:33:01 -0700 | [diff] [blame] | 113 | // |
| 114 | // (Since class names need to be known to implement these methods this cannot be |
Artem Titov | 0e61fdd | 2021-07-25 21:50:14 +0200 | [diff] [blame] | 115 | // part of the base `RTCStats`. While these methods could be implemented using |
hbos | 74e1a4f | 2016-09-15 23:33:01 -0700 | [diff] [blame] | 116 | // templates, that would only work for immediate subclasses. Subclasses of |
| 117 | // subclasses also have to override these methods, resulting in boilerplate |
Artem Titov | 0e61fdd | 2021-07-25 21:50:14 +0200 | [diff] [blame] | 118 | // code. Using a macro avoids this and works for any `RTCStats` class, including |
hbos | 74e1a4f | 2016-09-15 23:33:01 -0700 | [diff] [blame] | 119 | // grandchildren.) |
| 120 | // |
| 121 | // Sample usage: |
| 122 | // |
| 123 | // rtcfoostats.h: |
| 124 | // class RTCFooStats : public RTCStats { |
| 125 | // public: |
hbos | fc5e050 | 2016-10-06 02:06:10 -0700 | [diff] [blame] | 126 | // WEBRTC_RTCSTATS_DECL(); |
hbos | 74e1a4f | 2016-09-15 23:33:01 -0700 | [diff] [blame] | 127 | // |
hbos | fc5e050 | 2016-10-06 02:06:10 -0700 | [diff] [blame] | 128 | // RTCFooStats(const std::string& id, int64_t timestamp_us); |
hbos | 74e1a4f | 2016-09-15 23:33:01 -0700 | [diff] [blame] | 129 | // |
| 130 | // RTCStatsMember<int32_t> foo; |
| 131 | // RTCStatsMember<int32_t> bar; |
| 132 | // }; |
| 133 | // |
| 134 | // rtcfoostats.cc: |
hbos | fc5e050 | 2016-10-06 02:06:10 -0700 | [diff] [blame] | 135 | // WEBRTC_RTCSTATS_IMPL(RTCFooStats, RTCStats, "foo-stats" |
| 136 | // &foo, |
| 137 | // &bar); |
hbos | 74e1a4f | 2016-09-15 23:33:01 -0700 | [diff] [blame] | 138 | // |
hbos | fc5e050 | 2016-10-06 02:06:10 -0700 | [diff] [blame] | 139 | // RTCFooStats::RTCFooStats(const std::string& id, int64_t timestamp_us) |
| 140 | // : RTCStats(id, timestamp_us), |
| 141 | // foo("foo"), |
| 142 | // bar("bar") { |
| 143 | // } |
| 144 | // |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 145 | #define WEBRTC_RTCSTATS_DECL() \ |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 146 | protected: \ |
| 147 | std::vector<const webrtc::RTCStatsMemberInterface*> \ |
| 148 | MembersOfThisObjectAndAncestors(size_t local_var_additional_capacity) \ |
| 149 | const override; \ |
| 150 | \ |
Nico Weber | 22f9925 | 2019-02-20 10:13:16 -0500 | [diff] [blame] | 151 | public: \ |
| 152 | static const char kType[]; \ |
| 153 | \ |
| 154 | std::unique_ptr<webrtc::RTCStats> copy() const override; \ |
| 155 | const char* type() const override |
hbos | fc5e050 | 2016-10-06 02:06:10 -0700 | [diff] [blame] | 156 | |
| 157 | #define WEBRTC_RTCSTATS_IMPL(this_class, parent_class, type_str, ...) \ |
| 158 | const char this_class::kType[] = type_str; \ |
| 159 | \ |
| 160 | std::unique_ptr<webrtc::RTCStats> this_class::copy() const { \ |
| 161 | return std::unique_ptr<webrtc::RTCStats>(new this_class(*this)); \ |
| 162 | } \ |
| 163 | \ |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 164 | const char* this_class::type() const { return this_class::kType; } \ |
hbos | fc5e050 | 2016-10-06 02:06:10 -0700 | [diff] [blame] | 165 | \ |
| 166 | std::vector<const webrtc::RTCStatsMemberInterface*> \ |
| 167 | this_class::MembersOfThisObjectAndAncestors( \ |
| 168 | size_t local_var_additional_capacity) const { \ |
hbos | 74e1a4f | 2016-09-15 23:33:01 -0700 | [diff] [blame] | 169 | const webrtc::RTCStatsMemberInterface* local_var_members[] = { \ |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 170 | __VA_ARGS__}; \ |
hbos | 74e1a4f | 2016-09-15 23:33:01 -0700 | [diff] [blame] | 171 | size_t local_var_members_count = \ |
| 172 | sizeof(local_var_members) / sizeof(local_var_members[0]); \ |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 173 | std::vector<const webrtc::RTCStatsMemberInterface*> \ |
| 174 | local_var_members_vec = parent_class::MembersOfThisObjectAndAncestors( \ |
hbos | 74e1a4f | 2016-09-15 23:33:01 -0700 | [diff] [blame] | 175 | local_var_members_count + local_var_additional_capacity); \ |
| 176 | RTC_DCHECK_GE( \ |
| 177 | local_var_members_vec.capacity() - local_var_members_vec.size(), \ |
| 178 | local_var_members_count + local_var_additional_capacity); \ |
| 179 | local_var_members_vec.insert(local_var_members_vec.end(), \ |
| 180 | &local_var_members[0], \ |
| 181 | &local_var_members[local_var_members_count]); \ |
| 182 | return local_var_members_vec; \ |
hbos | fc5e050 | 2016-10-06 02:06:10 -0700 | [diff] [blame] | 183 | } |
hbos | 74e1a4f | 2016-09-15 23:33:01 -0700 | [diff] [blame] | 184 | |
Henrik Boström | 646fda0 | 2019-05-22 15:49:42 +0200 | [diff] [blame] | 185 | // A version of WEBRTC_RTCSTATS_IMPL() where "..." is omitted, used to avoid a |
| 186 | // compile error on windows. This is used if the stats dictionary does not |
| 187 | // declare any members of its own (but perhaps its parent dictionary does). |
| 188 | #define WEBRTC_RTCSTATS_IMPL_NO_MEMBERS(this_class, parent_class, type_str) \ |
| 189 | const char this_class::kType[] = type_str; \ |
| 190 | \ |
| 191 | std::unique_ptr<webrtc::RTCStats> this_class::copy() const { \ |
| 192 | return std::unique_ptr<webrtc::RTCStats>(new this_class(*this)); \ |
| 193 | } \ |
| 194 | \ |
| 195 | const char* this_class::type() const { return this_class::kType; } \ |
| 196 | \ |
| 197 | std::vector<const webrtc::RTCStatsMemberInterface*> \ |
| 198 | this_class::MembersOfThisObjectAndAncestors( \ |
| 199 | size_t local_var_additional_capacity) const { \ |
| 200 | return parent_class::MembersOfThisObjectAndAncestors(0); \ |
| 201 | } |
| 202 | |
Jakob Ivarsson | 2293622 | 2019-03-22 11:29:49 +0100 | [diff] [blame] | 203 | // Non-standard stats members can be exposed to the JavaScript API in Chrome |
| 204 | // e.g. through origin trials. The group ID can be used by the blink layer to |
| 205 | // determine if a stats member should be exposed or not. Multiple non-standard |
| 206 | // stats members can share the same group ID so that they are exposed together. |
| 207 | enum class NonStandardGroupId { |
Jakob Ivarsson | aa023e2 | 2019-03-27 10:17:31 +0100 | [diff] [blame] | 208 | // Group ID used for testing purposes only. |
| 209 | kGroupIdForTesting, |
Jakob Ivarsson | 2293622 | 2019-03-22 11:29:49 +0100 | [diff] [blame] | 210 | // I2E: |
| 211 | // https://groups.google.com/a/chromium.org/forum/#!topic/blink-dev/hE2B1iItPDk |
| 212 | kRtcAudioJitterBufferMaxPackets, |
| 213 | // I2E: |
| 214 | // https://groups.google.com/a/chromium.org/forum/#!topic/blink-dev/YbhMyqLXXXo |
| 215 | kRtcStatsRelativePacketArrivalDelay, |
| 216 | }; |
| 217 | |
Artem Titov | 0e61fdd | 2021-07-25 21:50:14 +0200 | [diff] [blame] | 218 | // Interface for `RTCStats` members, which have a name and a value of a type |
| 219 | // defined in a subclass. Only the types listed in `Type` are supported, these |
Artem Titov | cfea218 | 2021-08-10 01:22:31 +0200 | [diff] [blame] | 220 | // are implemented by `RTCStatsMember<T>`. The value of a member may be |
Artem Titov | 0e61fdd | 2021-07-25 21:50:14 +0200 | [diff] [blame] | 221 | // undefined, the value can only be read if `is_defined`. |
hbos | 74e1a4f | 2016-09-15 23:33:01 -0700 | [diff] [blame] | 222 | class RTCStatsMemberInterface { |
| 223 | public: |
| 224 | // Member value types. |
| 225 | enum Type { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 226 | kBool, // bool |
| 227 | kInt32, // int32_t |
| 228 | kUint32, // uint32_t |
| 229 | kInt64, // int64_t |
| 230 | kUint64, // uint64_t |
| 231 | kDouble, // double |
| 232 | kString, // std::string |
hbos | 74e1a4f | 2016-09-15 23:33:01 -0700 | [diff] [blame] | 233 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 234 | kSequenceBool, // std::vector<bool> |
| 235 | kSequenceInt32, // std::vector<int32_t> |
| 236 | kSequenceUint32, // std::vector<uint32_t> |
| 237 | kSequenceInt64, // std::vector<int64_t> |
| 238 | kSequenceUint64, // std::vector<uint64_t> |
| 239 | kSequenceDouble, // std::vector<double> |
| 240 | kSequenceString, // std::vector<std::string> |
Byoungchan Lee | 0a52ede | 2021-05-22 08:41:02 +0900 | [diff] [blame] | 241 | |
| 242 | kMapStringUint64, // std::map<std::string, uint64_t> |
| 243 | kMapStringDouble, // std::map<std::string, double> |
hbos | 74e1a4f | 2016-09-15 23:33:01 -0700 | [diff] [blame] | 244 | }; |
| 245 | |
| 246 | virtual ~RTCStatsMemberInterface() {} |
| 247 | |
| 248 | const char* name() const { return name_; } |
| 249 | virtual Type type() const = 0; |
| 250 | virtual bool is_sequence() const = 0; |
| 251 | virtual bool is_string() const = 0; |
| 252 | bool is_defined() const { return is_defined_; } |
Taylor Brandstetter | e275174 | 2018-06-25 13:42:44 -0700 | [diff] [blame] | 253 | // Is this part of the stats spec? Used so that chromium can easily filter |
| 254 | // out anything unstandardized. |
| 255 | virtual bool is_standardized() const = 0; |
Jakob Ivarsson | 2293622 | 2019-03-22 11:29:49 +0100 | [diff] [blame] | 256 | // Non-standard stats members can have group IDs in order to be exposed in |
| 257 | // JavaScript through experiments. Standardized stats have no group IDs. |
| 258 | virtual std::vector<NonStandardGroupId> group_ids() const { return {}; } |
hbos | 67c8bc4 | 2016-10-25 04:31:23 -0700 | [diff] [blame] | 259 | // Type and value comparator. The names are not compared. These operators are |
| 260 | // exposed for testing. |
| 261 | virtual bool operator==(const RTCStatsMemberInterface& other) const = 0; |
| 262 | bool operator!=(const RTCStatsMemberInterface& other) const { |
| 263 | return !(*this == other); |
| 264 | } |
hbos | 74e1a4f | 2016-09-15 23:33:01 -0700 | [diff] [blame] | 265 | virtual std::string ValueToString() const = 0; |
ehmaldonado | 35a872c | 2017-07-28 07:29:12 -0700 | [diff] [blame] | 266 | // This is the same as ValueToString except for kInt64 and kUint64 types, |
| 267 | // where the value is represented as a double instead of as an integer. |
| 268 | // Since JSON stores numbers as floating point numbers, very large integers |
| 269 | // cannot be accurately represented, so we prefer to display them as doubles |
| 270 | // instead. |
| 271 | virtual std::string ValueToJson() const = 0; |
hbos | 74e1a4f | 2016-09-15 23:33:01 -0700 | [diff] [blame] | 272 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 273 | template <typename T> |
hbos | 74e1a4f | 2016-09-15 23:33:01 -0700 | [diff] [blame] | 274 | const T& cast_to() const { |
Mirko Bonadei | 054f185 | 2019-11-04 16:31:08 +0100 | [diff] [blame] | 275 | RTC_DCHECK_EQ(type(), T::StaticType()); |
hbos | 74e1a4f | 2016-09-15 23:33:01 -0700 | [diff] [blame] | 276 | return static_cast<const T&>(*this); |
| 277 | } |
| 278 | |
| 279 | protected: |
| 280 | RTCStatsMemberInterface(const char* name, bool is_defined) |
| 281 | : name_(name), is_defined_(is_defined) {} |
| 282 | |
| 283 | const char* const name_; |
| 284 | bool is_defined_; |
| 285 | }; |
| 286 | |
Artem Titov | 0e61fdd | 2021-07-25 21:50:14 +0200 | [diff] [blame] | 287 | // Template implementation of `RTCStatsMemberInterface`. |
Mirko Bonadei | 054f185 | 2019-11-04 16:31:08 +0100 | [diff] [blame] | 288 | // The supported types are the ones described by |
Artem Titov | cfea218 | 2021-08-10 01:22:31 +0200 | [diff] [blame] | 289 | // `RTCStatsMemberInterface::Type`. |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 290 | template <typename T> |
Mirko Bonadei | 1e6aa1f | 2019-11-05 17:20:58 +0100 | [diff] [blame] | 291 | class RTCStatsMember : public RTCStatsMemberInterface { |
hbos | 74e1a4f | 2016-09-15 23:33:01 -0700 | [diff] [blame] | 292 | public: |
hbos | 74e1a4f | 2016-09-15 23:33:01 -0700 | [diff] [blame] | 293 | explicit RTCStatsMember(const char* name) |
Taylor Brandstetter | e275174 | 2018-06-25 13:42:44 -0700 | [diff] [blame] | 294 | : RTCStatsMemberInterface(name, /*is_defined=*/false), value_() {} |
hbos | 74e1a4f | 2016-09-15 23:33:01 -0700 | [diff] [blame] | 295 | RTCStatsMember(const char* name, const T& value) |
Taylor Brandstetter | e275174 | 2018-06-25 13:42:44 -0700 | [diff] [blame] | 296 | : RTCStatsMemberInterface(name, /*is_defined=*/true), value_(value) {} |
hbos | 74e1a4f | 2016-09-15 23:33:01 -0700 | [diff] [blame] | 297 | RTCStatsMember(const char* name, T&& value) |
Taylor Brandstetter | e275174 | 2018-06-25 13:42:44 -0700 | [diff] [blame] | 298 | : RTCStatsMemberInterface(name, /*is_defined=*/true), |
| 299 | value_(std::move(value)) {} |
hbos | 74e1a4f | 2016-09-15 23:33:01 -0700 | [diff] [blame] | 300 | explicit RTCStatsMember(const RTCStatsMember<T>& other) |
| 301 | : RTCStatsMemberInterface(other.name_, other.is_defined_), |
| 302 | value_(other.value_) {} |
| 303 | explicit RTCStatsMember(RTCStatsMember<T>&& other) |
| 304 | : RTCStatsMemberInterface(other.name_, other.is_defined_), |
| 305 | value_(std::move(other.value_)) {} |
| 306 | |
Mirko Bonadei | 054f185 | 2019-11-04 16:31:08 +0100 | [diff] [blame] | 307 | static Type StaticType(); |
| 308 | Type type() const override { return StaticType(); } |
hbos | 74e1a4f | 2016-09-15 23:33:01 -0700 | [diff] [blame] | 309 | bool is_sequence() const override; |
| 310 | bool is_string() const override; |
Taylor Brandstetter | e275174 | 2018-06-25 13:42:44 -0700 | [diff] [blame] | 311 | bool is_standardized() const override { return true; } |
hbos | 67c8bc4 | 2016-10-25 04:31:23 -0700 | [diff] [blame] | 312 | bool operator==(const RTCStatsMemberInterface& other) const override { |
Taylor Brandstetter | e275174 | 2018-06-25 13:42:44 -0700 | [diff] [blame] | 313 | if (type() != other.type() || is_standardized() != other.is_standardized()) |
hbos | 67c8bc4 | 2016-10-25 04:31:23 -0700 | [diff] [blame] | 314 | return false; |
| 315 | const RTCStatsMember<T>& other_t = |
| 316 | static_cast<const RTCStatsMember<T>&>(other); |
| 317 | if (!is_defined_) |
| 318 | return !other_t.is_defined(); |
hbos | 2874796 | 2016-11-21 09:17:41 -0800 | [diff] [blame] | 319 | if (!other.is_defined()) |
| 320 | return false; |
hbos | 67c8bc4 | 2016-10-25 04:31:23 -0700 | [diff] [blame] | 321 | return value_ == other_t.value_; |
| 322 | } |
hbos | 74e1a4f | 2016-09-15 23:33:01 -0700 | [diff] [blame] | 323 | std::string ValueToString() const override; |
ehmaldonado | 35a872c | 2017-07-28 07:29:12 -0700 | [diff] [blame] | 324 | std::string ValueToJson() const override; |
hbos | 74e1a4f | 2016-09-15 23:33:01 -0700 | [diff] [blame] | 325 | |
Andrey Logvin | 1f0f59f | 2020-06-15 12:49:25 +0000 | [diff] [blame] | 326 | template <typename U> |
| 327 | inline T ValueOrDefault(U default_value) const { |
| 328 | if (is_defined()) { |
| 329 | return *(*this); |
| 330 | } |
| 331 | return default_value; |
| 332 | } |
| 333 | |
hbos | 74e1a4f | 2016-09-15 23:33:01 -0700 | [diff] [blame] | 334 | // Assignment operators. |
| 335 | T& operator=(const T& value) { |
| 336 | value_ = value; |
| 337 | is_defined_ = true; |
| 338 | return value_; |
| 339 | } |
| 340 | T& operator=(const T&& value) { |
| 341 | value_ = std::move(value); |
| 342 | is_defined_ = true; |
| 343 | return value_; |
| 344 | } |
hbos | 74e1a4f | 2016-09-15 23:33:01 -0700 | [diff] [blame] | 345 | |
| 346 | // Value getters. |
| 347 | T& operator*() { |
| 348 | RTC_DCHECK(is_defined_); |
| 349 | return value_; |
| 350 | } |
| 351 | const T& operator*() const { |
| 352 | RTC_DCHECK(is_defined_); |
| 353 | return value_; |
| 354 | } |
| 355 | |
| 356 | // Value getters, arrow operator. |
| 357 | T* operator->() { |
| 358 | RTC_DCHECK(is_defined_); |
| 359 | return &value_; |
| 360 | } |
| 361 | const T* operator->() const { |
| 362 | RTC_DCHECK(is_defined_); |
| 363 | return &value_; |
| 364 | } |
| 365 | |
| 366 | private: |
| 367 | T value_; |
| 368 | }; |
| 369 | |
Byoungchan Lee | 0a52ede | 2021-05-22 08:41:02 +0900 | [diff] [blame] | 370 | namespace rtc_stats_internal { |
| 371 | |
| 372 | typedef std::map<std::string, uint64_t> MapStringUint64; |
| 373 | typedef std::map<std::string, double> MapStringDouble; |
| 374 | |
| 375 | } // namespace rtc_stats_internal |
| 376 | |
Mirko Bonadei | 62a19d0 | 2019-11-11 19:59:54 +0100 | [diff] [blame] | 377 | #define WEBRTC_DECLARE_RTCSTATSMEMBER(T) \ |
| 378 | template <> \ |
| 379 | RTC_EXPORT RTCStatsMemberInterface::Type RTCStatsMember<T>::StaticType(); \ |
| 380 | template <> \ |
Mirko Bonadei | 6dd488b | 2019-11-20 14:06:39 +0100 | [diff] [blame] | 381 | RTC_EXPORT bool RTCStatsMember<T>::is_sequence() const; \ |
Mirko Bonadei | 62a19d0 | 2019-11-11 19:59:54 +0100 | [diff] [blame] | 382 | template <> \ |
Mirko Bonadei | 6dd488b | 2019-11-20 14:06:39 +0100 | [diff] [blame] | 383 | RTC_EXPORT bool RTCStatsMember<T>::is_string() const; \ |
Mirko Bonadei | 62a19d0 | 2019-11-11 19:59:54 +0100 | [diff] [blame] | 384 | template <> \ |
Mirko Bonadei | 6dd488b | 2019-11-20 14:06:39 +0100 | [diff] [blame] | 385 | RTC_EXPORT std::string RTCStatsMember<T>::ValueToString() const; \ |
Mirko Bonadei | 62a19d0 | 2019-11-11 19:59:54 +0100 | [diff] [blame] | 386 | template <> \ |
Mirko Bonadei | 6dd488b | 2019-11-20 14:06:39 +0100 | [diff] [blame] | 387 | RTC_EXPORT std::string RTCStatsMember<T>::ValueToJson() const; \ |
Mirko Bonadei | 62a19d0 | 2019-11-11 19:59:54 +0100 | [diff] [blame] | 388 | extern template class RTC_EXPORT_TEMPLATE_DECLARE(RTC_EXPORT) \ |
Mirko Bonadei | 054f185 | 2019-11-04 16:31:08 +0100 | [diff] [blame] | 389 | RTCStatsMember<T> |
| 390 | |
| 391 | WEBRTC_DECLARE_RTCSTATSMEMBER(bool); |
| 392 | WEBRTC_DECLARE_RTCSTATSMEMBER(int32_t); |
| 393 | WEBRTC_DECLARE_RTCSTATSMEMBER(uint32_t); |
| 394 | WEBRTC_DECLARE_RTCSTATSMEMBER(int64_t); |
| 395 | WEBRTC_DECLARE_RTCSTATSMEMBER(uint64_t); |
| 396 | WEBRTC_DECLARE_RTCSTATSMEMBER(double); |
| 397 | WEBRTC_DECLARE_RTCSTATSMEMBER(std::string); |
| 398 | WEBRTC_DECLARE_RTCSTATSMEMBER(std::vector<bool>); |
| 399 | WEBRTC_DECLARE_RTCSTATSMEMBER(std::vector<int32_t>); |
| 400 | WEBRTC_DECLARE_RTCSTATSMEMBER(std::vector<uint32_t>); |
| 401 | WEBRTC_DECLARE_RTCSTATSMEMBER(std::vector<int64_t>); |
| 402 | WEBRTC_DECLARE_RTCSTATSMEMBER(std::vector<uint64_t>); |
| 403 | WEBRTC_DECLARE_RTCSTATSMEMBER(std::vector<double>); |
| 404 | WEBRTC_DECLARE_RTCSTATSMEMBER(std::vector<std::string>); |
Byoungchan Lee | 0a52ede | 2021-05-22 08:41:02 +0900 | [diff] [blame] | 405 | WEBRTC_DECLARE_RTCSTATSMEMBER(rtc_stats_internal::MapStringUint64); |
| 406 | WEBRTC_DECLARE_RTCSTATSMEMBER(rtc_stats_internal::MapStringDouble); |
Mirko Bonadei | 054f185 | 2019-11-04 16:31:08 +0100 | [diff] [blame] | 407 | |
Taylor Brandstetter | e275174 | 2018-06-25 13:42:44 -0700 | [diff] [blame] | 408 | // Using inheritance just so that it's obvious from the member's declaration |
| 409 | // whether it's standardized or not. |
| 410 | template <typename T> |
Mirko Bonadei | 759f161 | 2019-11-13 11:18:31 +0100 | [diff] [blame] | 411 | class RTCNonStandardStatsMember : public RTCStatsMember<T> { |
Taylor Brandstetter | e275174 | 2018-06-25 13:42:44 -0700 | [diff] [blame] | 412 | public: |
| 413 | explicit RTCNonStandardStatsMember(const char* name) |
| 414 | : RTCStatsMember<T>(name) {} |
Jakob Ivarsson | 758d946 | 2019-03-19 15:38:49 +0100 | [diff] [blame] | 415 | RTCNonStandardStatsMember(const char* name, |
| 416 | std::initializer_list<NonStandardGroupId> group_ids) |
| 417 | : RTCStatsMember<T>(name), group_ids_(group_ids) {} |
Taylor Brandstetter | e275174 | 2018-06-25 13:42:44 -0700 | [diff] [blame] | 418 | RTCNonStandardStatsMember(const char* name, const T& value) |
| 419 | : RTCStatsMember<T>(name, value) {} |
| 420 | RTCNonStandardStatsMember(const char* name, T&& value) |
| 421 | : RTCStatsMember<T>(name, std::move(value)) {} |
| 422 | explicit RTCNonStandardStatsMember(const RTCNonStandardStatsMember<T>& other) |
Jakob Ivarsson | 758d946 | 2019-03-19 15:38:49 +0100 | [diff] [blame] | 423 | : RTCStatsMember<T>(other), group_ids_(other.group_ids_) {} |
Taylor Brandstetter | e275174 | 2018-06-25 13:42:44 -0700 | [diff] [blame] | 424 | explicit RTCNonStandardStatsMember(RTCNonStandardStatsMember<T>&& other) |
Mirko Bonadei | 759f161 | 2019-11-13 11:18:31 +0100 | [diff] [blame] | 425 | : RTCStatsMember<T>(std::move(other)), |
| 426 | group_ids_(std::move(other.group_ids_)) {} |
Taylor Brandstetter | e275174 | 2018-06-25 13:42:44 -0700 | [diff] [blame] | 427 | |
| 428 | bool is_standardized() const override { return false; } |
Ruslan Burakov | 8af8896 | 2018-11-22 17:21:10 +0100 | [diff] [blame] | 429 | |
Jakob Ivarsson | 2293622 | 2019-03-22 11:29:49 +0100 | [diff] [blame] | 430 | std::vector<NonStandardGroupId> group_ids() const override { |
| 431 | return group_ids_; |
| 432 | } |
Jakob Ivarsson | 758d946 | 2019-03-19 15:38:49 +0100 | [diff] [blame] | 433 | |
Ruslan Burakov | 8af8896 | 2018-11-22 17:21:10 +0100 | [diff] [blame] | 434 | T& operator=(const T& value) { return RTCStatsMember<T>::operator=(value); } |
| 435 | T& operator=(const T&& value) { |
| 436 | return RTCStatsMember<T>::operator=(std::move(value)); |
| 437 | } |
Jakob Ivarsson | 758d946 | 2019-03-19 15:38:49 +0100 | [diff] [blame] | 438 | |
| 439 | private: |
| 440 | std::vector<NonStandardGroupId> group_ids_; |
Taylor Brandstetter | e275174 | 2018-06-25 13:42:44 -0700 | [diff] [blame] | 441 | }; |
Mirko Bonadei | 759f161 | 2019-11-13 11:18:31 +0100 | [diff] [blame] | 442 | |
| 443 | extern template class RTC_EXPORT_TEMPLATE_DECLARE(RTC_EXPORT) |
| 444 | RTCNonStandardStatsMember<bool>; |
| 445 | extern template class RTC_EXPORT_TEMPLATE_DECLARE(RTC_EXPORT) |
| 446 | RTCNonStandardStatsMember<int32_t>; |
| 447 | extern template class RTC_EXPORT_TEMPLATE_DECLARE(RTC_EXPORT) |
| 448 | RTCNonStandardStatsMember<uint32_t>; |
| 449 | extern template class RTC_EXPORT_TEMPLATE_DECLARE(RTC_EXPORT) |
| 450 | RTCNonStandardStatsMember<int64_t>; |
| 451 | extern template class RTC_EXPORT_TEMPLATE_DECLARE(RTC_EXPORT) |
| 452 | RTCNonStandardStatsMember<uint64_t>; |
| 453 | extern template class RTC_EXPORT_TEMPLATE_DECLARE(RTC_EXPORT) |
| 454 | RTCNonStandardStatsMember<double>; |
| 455 | extern template class RTC_EXPORT_TEMPLATE_DECLARE(RTC_EXPORT) |
| 456 | RTCNonStandardStatsMember<std::string>; |
| 457 | extern template class RTC_EXPORT_TEMPLATE_DECLARE(RTC_EXPORT) |
| 458 | RTCNonStandardStatsMember<std::vector<bool>>; |
| 459 | extern template class RTC_EXPORT_TEMPLATE_DECLARE(RTC_EXPORT) |
| 460 | RTCNonStandardStatsMember<std::vector<int32_t>>; |
| 461 | extern template class RTC_EXPORT_TEMPLATE_DECLARE(RTC_EXPORT) |
| 462 | RTCNonStandardStatsMember<std::vector<uint32_t>>; |
| 463 | extern template class RTC_EXPORT_TEMPLATE_DECLARE(RTC_EXPORT) |
| 464 | RTCNonStandardStatsMember<std::vector<int64_t>>; |
| 465 | extern template class RTC_EXPORT_TEMPLATE_DECLARE(RTC_EXPORT) |
| 466 | RTCNonStandardStatsMember<std::vector<uint64_t>>; |
| 467 | extern template class RTC_EXPORT_TEMPLATE_DECLARE(RTC_EXPORT) |
| 468 | RTCNonStandardStatsMember<std::vector<double>>; |
| 469 | extern template class RTC_EXPORT_TEMPLATE_DECLARE(RTC_EXPORT) |
| 470 | RTCNonStandardStatsMember<std::vector<std::string>>; |
Byoungchan Lee | 0a52ede | 2021-05-22 08:41:02 +0900 | [diff] [blame] | 471 | extern template class RTC_EXPORT_TEMPLATE_DECLARE(RTC_EXPORT) |
| 472 | RTCNonStandardStatsMember<std::map<std::string, uint64_t>>; |
| 473 | extern template class RTC_EXPORT_TEMPLATE_DECLARE(RTC_EXPORT) |
| 474 | RTCNonStandardStatsMember<std::map<std::string, double>>; |
Mirko Bonadei | 759f161 | 2019-11-13 11:18:31 +0100 | [diff] [blame] | 475 | |
hbos | 74e1a4f | 2016-09-15 23:33:01 -0700 | [diff] [blame] | 476 | } // namespace webrtc |
| 477 | |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 478 | #endif // API_STATS_RTC_STATS_H_ |