| 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 |
| 11 | #ifndef API_STATS_RTC_STATS_H_ |
| 12 | #define API_STATS_RTC_STATS_H_ |
| 13 |
| 14 | #include <stddef.h> |
| 15 | #include <stdint.h> |
| 16 |
| 17 | #include <map> |
| 18 | #include <memory> |
| 19 | #include <string> |
| 20 | #include <utility> |
| 21 | #include <vector> |
| 22 |
| 23 | #include "absl/types/optional.h" |
| 24 | #include "api/stats/attribute.h" |
| 25 | #include "api/units/timestamp.h" |
| 26 | #include "rtc_base/checks.h" |
| 27 | #include "rtc_base/system/rtc_export.h" |
| 28 | #include "rtc_base/system/rtc_export_template.h" |
| 29 |
| 30 | namespace webrtc { |
| 31 |
| 32 | // Abstract base class for RTCStats-derived dictionaries, see |
| 33 | // https://w3c.github.io/webrtc-stats/. |
| 34 | // |
| 35 | // All derived classes must have the following static variable defined: |
| 36 | // static const char kType[]; |
| 37 | // It is used as a unique class identifier and a string representation of the |
| 38 | // class type, see https://w3c.github.io/webrtc-stats/#rtcstatstype-str*. |
| 39 | // Use the `WEBRTC_RTCSTATS_IMPL` macro when implementing subclasses, see macro |
| 40 | // for details. |
| 41 | // |
| 42 | // Derived classes list their dictionary attributes, absl::optional<T>, as |
| 43 | // public fields, allowing the following: |
| 44 | // |
| 45 | // RTCFooStats foo("fooId", Timestamp::Micros(GetCurrentTime())); |
| 46 | // foo.bar = 42; |
| 47 | // foo.baz = std::vector<std::string>(); |
| 48 | // foo.baz->push_back("hello world"); |
| 49 | // uint32_t x = *foo.bar; |
| 50 | // |
| 51 | // Pointers to all the attributes are available with `Attributes()`, allowing |
| 52 | // iteration: |
| 53 | // |
| 54 | // for (const auto& attribute : foo.Attributes()) { |
| 55 | // printf("%s = %s\n", attribute.name(), attribute.ToString().c_str()); |
| 56 | // } |
| 57 | class RTC_EXPORT RTCStats { |
| 58 | public: |
| 59 | RTCStats(const std::string& id, Timestamp timestamp) |
| 60 | : id_(id), timestamp_(timestamp) {} |
| 61 | RTCStats(const RTCStats& other); |
| 62 | virtual ~RTCStats(); |
| 63 |
| 64 | virtual std::unique_ptr<RTCStats> copy() const = 0; |
| 65 |
| 66 | const std::string& id() const { return id_; } |
| 67 | // Time relative to the UNIX epoch (Jan 1, 1970, UTC), in microseconds. |
| 68 | Timestamp timestamp() const { return timestamp_; } |
| 69 |
| 70 | // Returns the static member variable `kType` of the implementing class. |
| 71 | virtual const char* type() const = 0; |
| 72 | // Returns all attributes of this stats object, i.e. a list of its individual |
| 73 | // metrics as viewed via the Attribute wrapper. |
| 74 | std::vector<Attribute> Attributes() const; |
| 75 | template <typename T> |
| 76 | Attribute GetAttribute(const absl::optional<T>& stat) const { |
| 77 | for (const auto& attribute : Attributes()) { |
| 78 | if (!attribute.holds_alternative<T>()) { |
| 79 | continue; |
| 80 | } |
| 81 | if (absl::get<const absl::optional<T>*>(attribute.as_variant()) == |
| 82 | &stat) { |
| 83 | return attribute; |
| 84 | } |
| 85 | } |
| 86 | RTC_CHECK_NOTREACHED(); |
| 87 | } |
| 88 | // Checks if the two stats objects are of the same type and have the same |
| 89 | // attribute values. Timestamps are not compared. These operators are exposed |
| 90 | // for testing. |
| 91 | bool operator==(const RTCStats& other) const; |
| 92 | bool operator!=(const RTCStats& other) const; |
| 93 |
| 94 | // Creates a JSON readable string representation of the stats |
| 95 | // object, listing all of its attributes (names and values). |
| 96 | std::string ToJson() const; |
| 97 |
| 98 | // Downcasts the stats object to an `RTCStats` subclass `T`. DCHECKs that the |
| 99 | // object is of type `T`. |
| 100 | template <typename T> |
| 101 | const T& cast_to() const { |
| 102 | RTC_DCHECK_EQ(type(), T::kType); |
| 103 | return static_cast<const T&>(*this); |
| 104 | } |
| 105 |
| 106 | protected: |
| 107 | virtual std::vector<Attribute> AttributesImpl( |
| 108 | size_t additional_capacity) const; |
| 109 |
| 110 | std::string const id_; |
| 111 | Timestamp timestamp_; |
| 112 | }; |
| 113 |
| 114 | // All `RTCStats` classes should use these macros. |
| 115 | // `WEBRTC_RTCSTATS_DECL` is placed in a public section of the class definition. |
| 116 | // `WEBRTC_RTCSTATS_IMPL` is placed outside the class definition (in a .cc). |
| 117 | // |
| 118 | // These macros declare (in _DECL) and define (in _IMPL) the static `kType` and |
| 119 | // overrides methods as required by subclasses of `RTCStats`: `copy`, `type` and |
| 120 | // `AttributesImpl`. The |...| argument is a list of addresses to each attribute |
| 121 | // defined in the implementing class. The list must have at least one attribute. |
| 122 | // |
| 123 | // (Since class names need to be known to implement these methods this cannot be |
| 124 | // part of the base `RTCStats`. While these methods could be implemented using |
| 125 | // templates, that would only work for immediate subclasses. Subclasses of |
| 126 | // subclasses also have to override these methods, resulting in boilerplate |
| 127 | // code. Using a macro avoids this and works for any `RTCStats` class, including |
| 128 | // grandchildren.) |
| 129 | // |
| 130 | // Sample usage: |
| 131 | // |
| 132 | // rtcfoostats.h: |
| 133 | // class RTCFooStats : public RTCStats { |
| 134 | // public: |
| 135 | // WEBRTC_RTCSTATS_DECL(); |
| 136 | // |
| 137 | // RTCFooStats(const std::string& id, Timestamp timestamp); |
| 138 | // |
| 139 | // absl::optional<int32_t> foo; |
| 140 | // absl::optional<int32_t> bar; |
| 141 | // }; |
| 142 | // |
| 143 | // rtcfoostats.cc: |
| 144 | // WEBRTC_RTCSTATS_IMPL(RTCFooStats, RTCStats, "foo-stats" |
| 145 | // &foo, |
| 146 | // &bar); |
| 147 | // |
| 148 | // RTCFooStats::RTCFooStats(const std::string& id, Timestamp timestamp) |
| 149 | // : RTCStats(id, timestamp), |
| 150 | // foo("foo"), |
| 151 | // bar("bar") { |
| 152 | // } |
| 153 | // |
| 154 | #define WEBRTC_RTCSTATS_DECL() \ |
| 155 | protected: \ |
| 156 | std::vector<webrtc::Attribute> AttributesImpl(size_t additional_capacity) \ |
| 157 | const override; \ |
| 158 | \ |
| 159 | public: \ |
| 160 | static const char kType[]; \ |
| 161 | \ |
| 162 | std::unique_ptr<webrtc::RTCStats> copy() const override; \ |
| 163 | const char* type() const override |
| 164 |
| 165 | #define WEBRTC_RTCSTATS_IMPL(this_class, parent_class, type_str, ...) \ |
| 166 | const char this_class::kType[] = type_str; \ |
| 167 | \ |
| 168 | std::unique_ptr<webrtc::RTCStats> this_class::copy() const { \ |
| 169 | return std::make_unique<this_class>(*this); \ |
| 170 | } \ |
| 171 | \ |
| 172 | const char* this_class::type() const { \ |
| 173 | return this_class::kType; \ |
| 174 | } \ |
| 175 | \ |
| 176 | std::vector<webrtc::Attribute> this_class::AttributesImpl( \ |
| 177 | size_t additional_capacity) const { \ |
| 178 | webrtc::AttributeInit attribute_inits[] = {__VA_ARGS__}; \ |
| 179 | size_t attribute_inits_size = \ |
| 180 | sizeof(attribute_inits) / sizeof(attribute_inits[0]); \ |
| 181 | std::vector<webrtc::Attribute> attributes = parent_class::AttributesImpl( \ |
| 182 | attribute_inits_size + additional_capacity); \ |
| 183 | for (size_t i = 0; i < attribute_inits_size; ++i) { \ |
| 184 | attributes.push_back(absl::visit( \ |
| 185 | [&](const auto* field) { \ |
| 186 | return Attribute(attribute_inits[i].name, field); \ |
| 187 | }, \ |
| 188 | attribute_inits[i].variant)); \ |
| 189 | } \ |
| 190 | return attributes; \ |
| 191 | } |
| 192 |
| 193 | } // namespace webrtc |
| 194 |
| 195 | #endif // API_STATS_RTC_STATS_H_ |
| 196 |