blob: 049b74f584ccc403ad3a43ef3f7f72f554335a52 [file] [log] [blame]
hbos74e1a4f2016-09-15 23:33:01 -07001/*
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 Anton10542f22019-01-11 09:11:00 -080011#ifndef API_STATS_RTC_STATS_H_
12#define API_STATS_RTC_STATS_H_
hbos74e1a4f2016-09-15 23:33:01 -070013
Yves Gerey3e707812018-11-28 16:47:49 +010014#include <stddef.h>
15#include <stdint.h>
hbos74e1a4f2016-09-15 23:33:01 -070016#include <memory>
17#include <string>
18#include <utility>
19#include <vector>
20
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020021#include "rtc_base/checks.h"
Mirko Bonadei276827c2018-10-16 14:13:50 +020022#include "rtc_base/system/rtc_export.h"
hbos74e1a4f2016-09-15 23:33:01 -070023
24namespace webrtc {
25
26class RTCStatsMemberInterface;
27
28// Abstract base class for RTCStats-derived dictionaries, see
29// https://w3c.github.io/webrtc-stats/.
30//
31// All derived classes must have the following static variable defined:
32// static const char kType[];
33// It is used as a unique class identifier and a string representation of the
34// class type, see https://w3c.github.io/webrtc-stats/#rtcstatstype-str*.
35// Use the |WEBRTC_RTCSTATS_IMPL| macro when implementing subclasses, see macro
36// for details.
37//
38// Derived classes list their dictionary members, RTCStatsMember<T>, as public
39// fields, allowing the following:
40//
41// RTCFooStats foo("fooId", GetCurrentTime());
42// foo.bar = 42;
43// foo.baz = std::vector<std::string>();
44// foo.baz->push_back("hello world");
45// uint32_t x = *foo.bar;
46//
47// Pointers to all the members are available with |Members|, allowing iteration:
48//
49// for (const RTCStatsMemberInterface* member : foo.Members()) {
50// printf("%s = %s\n", member->name(), member->ValueToString().c_str());
51// }
Mirko Bonadei276827c2018-10-16 14:13:50 +020052class RTC_EXPORT RTCStats {
hbos74e1a4f2016-09-15 23:33:01 -070053 public:
54 RTCStats(const std::string& id, int64_t timestamp_us)
55 : id_(id), timestamp_us_(timestamp_us) {}
56 RTCStats(std::string&& id, int64_t timestamp_us)
57 : id_(std::move(id)), timestamp_us_(timestamp_us) {}
58 virtual ~RTCStats() {}
59
60 virtual std::unique_ptr<RTCStats> copy() const = 0;
61
62 const std::string& id() const { return id_; }
63 // Time relative to the UNIX epoch (Jan 1, 1970, UTC), in microseconds.
64 int64_t timestamp_us() const { return timestamp_us_; }
65 // Returns the static member variable |kType| of the implementing class.
66 virtual const char* type() const = 0;
hbos67c8bc42016-10-25 04:31:23 -070067 // Returns a vector of pointers to all the |RTCStatsMemberInterface| members
68 // of this class. This allows for iteration of members. For a given class,
69 // |Members| always returns the same members in the same order.
hbos74e1a4f2016-09-15 23:33:01 -070070 std::vector<const RTCStatsMemberInterface*> Members() const;
hbos67c8bc42016-10-25 04:31:23 -070071 // Checks if the two stats objects are of the same type and have the same
hbos0583b282016-11-30 01:50:14 -080072 // member values. Timestamps are not compared. These operators are exposed for
73 // testing.
hbos67c8bc42016-10-25 04:31:23 -070074 bool operator==(const RTCStats& other) const;
75 bool operator!=(const RTCStats& other) const;
hbos74e1a4f2016-09-15 23:33:01 -070076
ehmaldonado35a872c2017-07-28 07:29:12 -070077 // Creates a JSON readable string representation of the stats
78 // object, listing all of its members (names and values).
79 std::string ToJson() const;
hbos74e1a4f2016-09-15 23:33:01 -070080
81 // Downcasts the stats object to an |RTCStats| subclass |T|. DCHECKs that the
82 // object is of type |T|.
Yves Gerey665174f2018-06-19 15:03:05 +020083 template <typename T>
hbos74e1a4f2016-09-15 23:33:01 -070084 const T& cast_to() const {
85 RTC_DCHECK_EQ(type(), T::kType);
86 return static_cast<const T&>(*this);
87 }
88
89 protected:
90 // Gets a vector of all members of this |RTCStats| object, including members
91 // derived from parent classes. |additional_capacity| is how many more members
92 // shall be reserved in the vector (so that subclasses can allocate a vector
93 // with room for both parent and child members without it having to resize).
94 virtual std::vector<const RTCStatsMemberInterface*>
Yves Gerey665174f2018-06-19 15:03:05 +020095 MembersOfThisObjectAndAncestors(size_t additional_capacity) const;
hbos74e1a4f2016-09-15 23:33:01 -070096
97 std::string const id_;
98 int64_t timestamp_us_;
99};
100
hbosfc5e0502016-10-06 02:06:10 -0700101// All |RTCStats| classes should use these macros.
102// |WEBRTC_RTCSTATS_DECL| is placed in a public section of the class definition.
103// |WEBRTC_RTCSTATS_IMPL| is placed outside the class definition (in a .cc).
hbos74e1a4f2016-09-15 23:33:01 -0700104//
hbosfc5e0502016-10-06 02:06:10 -0700105// These macros declare (in _DECL) and define (in _IMPL) the static |kType| and
106// overrides methods as required by subclasses of |RTCStats|: |copy|, |type| and
hbos74e1a4f2016-09-15 23:33:01 -0700107// |MembersOfThisObjectAndAncestors|. The |...| argument is a list of addresses
hbosfc5e0502016-10-06 02:06:10 -0700108// to each member defined in the implementing class. The list must have at least
109// one member.
hbos74e1a4f2016-09-15 23:33:01 -0700110//
111// (Since class names need to be known to implement these methods this cannot be
112// part of the base |RTCStats|. While these methods could be implemented using
113// templates, that would only work for immediate subclasses. Subclasses of
114// subclasses also have to override these methods, resulting in boilerplate
115// code. Using a macro avoids this and works for any |RTCStats| class, including
116// grandchildren.)
117//
118// Sample usage:
119//
120// rtcfoostats.h:
121// class RTCFooStats : public RTCStats {
122// public:
hbosfc5e0502016-10-06 02:06:10 -0700123// WEBRTC_RTCSTATS_DECL();
hbos74e1a4f2016-09-15 23:33:01 -0700124//
hbosfc5e0502016-10-06 02:06:10 -0700125// RTCFooStats(const std::string& id, int64_t timestamp_us);
hbos74e1a4f2016-09-15 23:33:01 -0700126//
127// RTCStatsMember<int32_t> foo;
128// RTCStatsMember<int32_t> bar;
129// };
130//
131// rtcfoostats.cc:
hbosfc5e0502016-10-06 02:06:10 -0700132// WEBRTC_RTCSTATS_IMPL(RTCFooStats, RTCStats, "foo-stats"
133// &foo,
134// &bar);
hbos74e1a4f2016-09-15 23:33:01 -0700135//
hbosfc5e0502016-10-06 02:06:10 -0700136// RTCFooStats::RTCFooStats(const std::string& id, int64_t timestamp_us)
137// : RTCStats(id, timestamp_us),
138// foo("foo"),
139// bar("bar") {
140// }
141//
Yves Gerey665174f2018-06-19 15:03:05 +0200142#define WEBRTC_RTCSTATS_DECL() \
143 public: \
144 static const char kType[]; \
145 \
146 std::unique_ptr<webrtc::RTCStats> copy() const override; \
147 const char* type() const override; \
148 \
149 protected: \
150 std::vector<const webrtc::RTCStatsMemberInterface*> \
151 MembersOfThisObjectAndAncestors(size_t local_var_additional_capacity) \
152 const override; \
153 \
hbosfc5e0502016-10-06 02:06:10 -0700154 public:
155
156#define WEBRTC_RTCSTATS_IMPL(this_class, parent_class, type_str, ...) \
157 const char this_class::kType[] = type_str; \
158 \
159 std::unique_ptr<webrtc::RTCStats> this_class::copy() const { \
160 return std::unique_ptr<webrtc::RTCStats>(new this_class(*this)); \
161 } \
162 \
Yves Gerey665174f2018-06-19 15:03:05 +0200163 const char* this_class::type() const { return this_class::kType; } \
hbosfc5e0502016-10-06 02:06:10 -0700164 \
165 std::vector<const webrtc::RTCStatsMemberInterface*> \
166 this_class::MembersOfThisObjectAndAncestors( \
167 size_t local_var_additional_capacity) const { \
hbos74e1a4f2016-09-15 23:33:01 -0700168 const webrtc::RTCStatsMemberInterface* local_var_members[] = { \
Yves Gerey665174f2018-06-19 15:03:05 +0200169 __VA_ARGS__}; \
hbos74e1a4f2016-09-15 23:33:01 -0700170 size_t local_var_members_count = \
171 sizeof(local_var_members) / sizeof(local_var_members[0]); \
Yves Gerey665174f2018-06-19 15:03:05 +0200172 std::vector<const webrtc::RTCStatsMemberInterface*> \
173 local_var_members_vec = parent_class::MembersOfThisObjectAndAncestors( \
hbos74e1a4f2016-09-15 23:33:01 -0700174 local_var_members_count + local_var_additional_capacity); \
175 RTC_DCHECK_GE( \
176 local_var_members_vec.capacity() - local_var_members_vec.size(), \
177 local_var_members_count + local_var_additional_capacity); \
178 local_var_members_vec.insert(local_var_members_vec.end(), \
179 &local_var_members[0], \
180 &local_var_members[local_var_members_count]); \
181 return local_var_members_vec; \
hbosfc5e0502016-10-06 02:06:10 -0700182 }
hbos74e1a4f2016-09-15 23:33:01 -0700183
184// Interface for |RTCStats| members, which have a name and a value of a type
185// defined in a subclass. Only the types listed in |Type| are supported, these
186// are implemented by |RTCStatsMember<T>|. The value of a member may be
187// undefined, the value can only be read if |is_defined|.
188class RTCStatsMemberInterface {
189 public:
190 // Member value types.
191 enum Type {
Yves Gerey665174f2018-06-19 15:03:05 +0200192 kBool, // bool
193 kInt32, // int32_t
194 kUint32, // uint32_t
195 kInt64, // int64_t
196 kUint64, // uint64_t
197 kDouble, // double
198 kString, // std::string
hbos74e1a4f2016-09-15 23:33:01 -0700199
Yves Gerey665174f2018-06-19 15:03:05 +0200200 kSequenceBool, // std::vector<bool>
201 kSequenceInt32, // std::vector<int32_t>
202 kSequenceUint32, // std::vector<uint32_t>
203 kSequenceInt64, // std::vector<int64_t>
204 kSequenceUint64, // std::vector<uint64_t>
205 kSequenceDouble, // std::vector<double>
206 kSequenceString, // std::vector<std::string>
hbos74e1a4f2016-09-15 23:33:01 -0700207 };
208
209 virtual ~RTCStatsMemberInterface() {}
210
211 const char* name() const { return name_; }
212 virtual Type type() const = 0;
213 virtual bool is_sequence() const = 0;
214 virtual bool is_string() const = 0;
215 bool is_defined() const { return is_defined_; }
Taylor Brandstettere2751742018-06-25 13:42:44 -0700216 // Is this part of the stats spec? Used so that chromium can easily filter
217 // out anything unstandardized.
218 virtual bool is_standardized() const = 0;
hbos67c8bc42016-10-25 04:31:23 -0700219 // Type and value comparator. The names are not compared. These operators are
220 // exposed for testing.
221 virtual bool operator==(const RTCStatsMemberInterface& other) const = 0;
222 bool operator!=(const RTCStatsMemberInterface& other) const {
223 return !(*this == other);
224 }
hbos74e1a4f2016-09-15 23:33:01 -0700225 virtual std::string ValueToString() const = 0;
ehmaldonado35a872c2017-07-28 07:29:12 -0700226 // This is the same as ValueToString except for kInt64 and kUint64 types,
227 // where the value is represented as a double instead of as an integer.
228 // Since JSON stores numbers as floating point numbers, very large integers
229 // cannot be accurately represented, so we prefer to display them as doubles
230 // instead.
231 virtual std::string ValueToJson() const = 0;
hbos74e1a4f2016-09-15 23:33:01 -0700232
Yves Gerey665174f2018-06-19 15:03:05 +0200233 template <typename T>
hbos74e1a4f2016-09-15 23:33:01 -0700234 const T& cast_to() const {
235 RTC_DCHECK_EQ(type(), T::kType);
236 return static_cast<const T&>(*this);
237 }
238
239 protected:
240 RTCStatsMemberInterface(const char* name, bool is_defined)
241 : name_(name), is_defined_(is_defined) {}
242
243 const char* const name_;
244 bool is_defined_;
245};
246
247// Template implementation of |RTCStatsMemberInterface|. Every possible |T| is
248// specialized in rtcstats.cc, using a different |T| results in a linker error
249// (undefined reference to |kType|). The supported types are the ones described
250// by |RTCStatsMemberInterface::Type|.
Yves Gerey665174f2018-06-19 15:03:05 +0200251template <typename T>
Mirko Bonadei276827c2018-10-16 14:13:50 +0200252class RTC_EXPORT RTCStatsMember : public RTCStatsMemberInterface {
hbos74e1a4f2016-09-15 23:33:01 -0700253 public:
254 static const Type kType;
255
256 explicit RTCStatsMember(const char* name)
Taylor Brandstettere2751742018-06-25 13:42:44 -0700257 : RTCStatsMemberInterface(name, /*is_defined=*/false), value_() {}
hbos74e1a4f2016-09-15 23:33:01 -0700258 RTCStatsMember(const char* name, const T& value)
Taylor Brandstettere2751742018-06-25 13:42:44 -0700259 : RTCStatsMemberInterface(name, /*is_defined=*/true), value_(value) {}
hbos74e1a4f2016-09-15 23:33:01 -0700260 RTCStatsMember(const char* name, T&& value)
Taylor Brandstettere2751742018-06-25 13:42:44 -0700261 : RTCStatsMemberInterface(name, /*is_defined=*/true),
262 value_(std::move(value)) {}
hbos74e1a4f2016-09-15 23:33:01 -0700263 explicit RTCStatsMember(const RTCStatsMember<T>& other)
264 : RTCStatsMemberInterface(other.name_, other.is_defined_),
265 value_(other.value_) {}
266 explicit RTCStatsMember(RTCStatsMember<T>&& other)
267 : RTCStatsMemberInterface(other.name_, other.is_defined_),
268 value_(std::move(other.value_)) {}
269
270 Type type() const override { return kType; }
271 bool is_sequence() const override;
272 bool is_string() const override;
Taylor Brandstettere2751742018-06-25 13:42:44 -0700273 bool is_standardized() const override { return true; }
hbos67c8bc42016-10-25 04:31:23 -0700274 bool operator==(const RTCStatsMemberInterface& other) const override {
Taylor Brandstettere2751742018-06-25 13:42:44 -0700275 if (type() != other.type() || is_standardized() != other.is_standardized())
hbos67c8bc42016-10-25 04:31:23 -0700276 return false;
277 const RTCStatsMember<T>& other_t =
278 static_cast<const RTCStatsMember<T>&>(other);
279 if (!is_defined_)
280 return !other_t.is_defined();
hbos28747962016-11-21 09:17:41 -0800281 if (!other.is_defined())
282 return false;
hbos67c8bc42016-10-25 04:31:23 -0700283 return value_ == other_t.value_;
284 }
hbos74e1a4f2016-09-15 23:33:01 -0700285 std::string ValueToString() const override;
ehmaldonado35a872c2017-07-28 07:29:12 -0700286 std::string ValueToJson() const override;
hbos74e1a4f2016-09-15 23:33:01 -0700287
288 // Assignment operators.
289 T& operator=(const T& value) {
290 value_ = value;
291 is_defined_ = true;
292 return value_;
293 }
294 T& operator=(const T&& value) {
295 value_ = std::move(value);
296 is_defined_ = true;
297 return value_;
298 }
hbos74e1a4f2016-09-15 23:33:01 -0700299
300 // Value getters.
301 T& operator*() {
302 RTC_DCHECK(is_defined_);
303 return value_;
304 }
305 const T& operator*() const {
306 RTC_DCHECK(is_defined_);
307 return value_;
308 }
309
310 // Value getters, arrow operator.
311 T* operator->() {
312 RTC_DCHECK(is_defined_);
313 return &value_;
314 }
315 const T* operator->() const {
316 RTC_DCHECK(is_defined_);
317 return &value_;
318 }
319
320 private:
321 T value_;
322};
323
Taylor Brandstettere2751742018-06-25 13:42:44 -0700324// Same as above, but "is_standardized" returns false.
325//
326// Using inheritance just so that it's obvious from the member's declaration
327// whether it's standardized or not.
328template <typename T>
329class RTCNonStandardStatsMember : public RTCStatsMember<T> {
330 public:
331 explicit RTCNonStandardStatsMember(const char* name)
332 : RTCStatsMember<T>(name) {}
333 RTCNonStandardStatsMember(const char* name, const T& value)
334 : RTCStatsMember<T>(name, value) {}
335 RTCNonStandardStatsMember(const char* name, T&& value)
336 : RTCStatsMember<T>(name, std::move(value)) {}
337 explicit RTCNonStandardStatsMember(const RTCNonStandardStatsMember<T>& other)
338 : RTCStatsMember<T>(other) {}
339 explicit RTCNonStandardStatsMember(RTCNonStandardStatsMember<T>&& other)
340 : RTCStatsMember<T>(std::move(other)) {}
341
342 bool is_standardized() const override { return false; }
Ruslan Burakov8af88962018-11-22 17:21:10 +0100343
344 T& operator=(const T& value) { return RTCStatsMember<T>::operator=(value); }
345 T& operator=(const T&& value) {
346 return RTCStatsMember<T>::operator=(std::move(value));
347 }
Taylor Brandstettere2751742018-06-25 13:42:44 -0700348};
hbos74e1a4f2016-09-15 23:33:01 -0700349} // namespace webrtc
350
Steve Anton10542f22019-01-11 09:11:00 -0800351#endif // API_STATS_RTC_STATS_H_