blob: 0cfbc06a0f617030d8ecc696f515e80c889d53ed [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>
Jonas Olssona4d87372019-07-05 19:08:33 +020016
hbos74e1a4f2016-09-15 23:33:01 -070017#include <memory>
18#include <string>
19#include <utility>
20#include <vector>
21
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020022#include "rtc_base/checks.h"
Mirko Bonadei276827c2018-10-16 14:13:50 +020023#include "rtc_base/system/rtc_export.h"
hbos74e1a4f2016-09-15 23:33:01 -070024
25namespace webrtc {
26
27class RTCStatsMemberInterface;
28
29// Abstract base class for RTCStats-derived dictionaries, see
30// https://w3c.github.io/webrtc-stats/.
31//
32// All derived classes must have the following static variable defined:
33// static const char kType[];
34// It is used as a unique class identifier and a string representation of the
35// class type, see https://w3c.github.io/webrtc-stats/#rtcstatstype-str*.
36// Use the |WEBRTC_RTCSTATS_IMPL| macro when implementing subclasses, see macro
37// for details.
38//
39// Derived classes list their dictionary members, RTCStatsMember<T>, as public
40// fields, allowing the following:
41//
42// RTCFooStats foo("fooId", GetCurrentTime());
43// foo.bar = 42;
44// foo.baz = std::vector<std::string>();
45// foo.baz->push_back("hello world");
46// uint32_t x = *foo.bar;
47//
48// Pointers to all the members are available with |Members|, allowing iteration:
49//
50// for (const RTCStatsMemberInterface* member : foo.Members()) {
51// printf("%s = %s\n", member->name(), member->ValueToString().c_str());
52// }
Mirko Bonadei276827c2018-10-16 14:13:50 +020053class RTC_EXPORT RTCStats {
hbos74e1a4f2016-09-15 23:33:01 -070054 public:
55 RTCStats(const std::string& id, int64_t timestamp_us)
56 : id_(id), timestamp_us_(timestamp_us) {}
57 RTCStats(std::string&& id, int64_t timestamp_us)
58 : id_(std::move(id)), timestamp_us_(timestamp_us) {}
59 virtual ~RTCStats() {}
60
61 virtual std::unique_ptr<RTCStats> copy() const = 0;
62
63 const std::string& id() const { return id_; }
64 // Time relative to the UNIX epoch (Jan 1, 1970, UTC), in microseconds.
65 int64_t timestamp_us() const { return timestamp_us_; }
66 // Returns the static member variable |kType| of the implementing class.
67 virtual const char* type() const = 0;
hbos67c8bc42016-10-25 04:31:23 -070068 // Returns a vector of pointers to all the |RTCStatsMemberInterface| members
69 // of this class. This allows for iteration of members. For a given class,
70 // |Members| always returns the same members in the same order.
hbos74e1a4f2016-09-15 23:33:01 -070071 std::vector<const RTCStatsMemberInterface*> Members() const;
hbos67c8bc42016-10-25 04:31:23 -070072 // Checks if the two stats objects are of the same type and have the same
hbos0583b282016-11-30 01:50:14 -080073 // member values. Timestamps are not compared. These operators are exposed for
74 // testing.
hbos67c8bc42016-10-25 04:31:23 -070075 bool operator==(const RTCStats& other) const;
76 bool operator!=(const RTCStats& other) const;
hbos74e1a4f2016-09-15 23:33:01 -070077
ehmaldonado35a872c2017-07-28 07:29:12 -070078 // Creates a JSON readable string representation of the stats
79 // object, listing all of its members (names and values).
80 std::string ToJson() const;
hbos74e1a4f2016-09-15 23:33:01 -070081
82 // Downcasts the stats object to an |RTCStats| subclass |T|. DCHECKs that the
83 // object is of type |T|.
Yves Gerey665174f2018-06-19 15:03:05 +020084 template <typename T>
hbos74e1a4f2016-09-15 23:33:01 -070085 const T& cast_to() const {
86 RTC_DCHECK_EQ(type(), T::kType);
87 return static_cast<const T&>(*this);
88 }
89
90 protected:
91 // Gets a vector of all members of this |RTCStats| object, including members
92 // derived from parent classes. |additional_capacity| is how many more members
93 // shall be reserved in the vector (so that subclasses can allocate a vector
94 // with room for both parent and child members without it having to resize).
95 virtual std::vector<const RTCStatsMemberInterface*>
Yves Gerey665174f2018-06-19 15:03:05 +020096 MembersOfThisObjectAndAncestors(size_t additional_capacity) const;
hbos74e1a4f2016-09-15 23:33:01 -070097
98 std::string const id_;
99 int64_t timestamp_us_;
100};
101
hbosfc5e0502016-10-06 02:06:10 -0700102// All |RTCStats| classes should use these macros.
103// |WEBRTC_RTCSTATS_DECL| is placed in a public section of the class definition.
104// |WEBRTC_RTCSTATS_IMPL| is placed outside the class definition (in a .cc).
hbos74e1a4f2016-09-15 23:33:01 -0700105//
hbosfc5e0502016-10-06 02:06:10 -0700106// These macros declare (in _DECL) and define (in _IMPL) the static |kType| and
107// overrides methods as required by subclasses of |RTCStats|: |copy|, |type| and
hbos74e1a4f2016-09-15 23:33:01 -0700108// |MembersOfThisObjectAndAncestors|. The |...| argument is a list of addresses
hbosfc5e0502016-10-06 02:06:10 -0700109// to each member defined in the implementing class. The list must have at least
110// one member.
hbos74e1a4f2016-09-15 23:33:01 -0700111//
112// (Since class names need to be known to implement these methods this cannot be
113// part of the base |RTCStats|. While these methods could be implemented using
114// templates, that would only work for immediate subclasses. Subclasses of
115// subclasses also have to override these methods, resulting in boilerplate
116// code. Using a macro avoids this and works for any |RTCStats| class, including
117// grandchildren.)
118//
119// Sample usage:
120//
121// rtcfoostats.h:
122// class RTCFooStats : public RTCStats {
123// public:
hbosfc5e0502016-10-06 02:06:10 -0700124// WEBRTC_RTCSTATS_DECL();
hbos74e1a4f2016-09-15 23:33:01 -0700125//
hbosfc5e0502016-10-06 02:06:10 -0700126// RTCFooStats(const std::string& id, int64_t timestamp_us);
hbos74e1a4f2016-09-15 23:33:01 -0700127//
128// RTCStatsMember<int32_t> foo;
129// RTCStatsMember<int32_t> bar;
130// };
131//
132// rtcfoostats.cc:
hbosfc5e0502016-10-06 02:06:10 -0700133// WEBRTC_RTCSTATS_IMPL(RTCFooStats, RTCStats, "foo-stats"
134// &foo,
135// &bar);
hbos74e1a4f2016-09-15 23:33:01 -0700136//
hbosfc5e0502016-10-06 02:06:10 -0700137// RTCFooStats::RTCFooStats(const std::string& id, int64_t timestamp_us)
138// : RTCStats(id, timestamp_us),
139// foo("foo"),
140// bar("bar") {
141// }
142//
Yves Gerey665174f2018-06-19 15:03:05 +0200143#define WEBRTC_RTCSTATS_DECL() \
Yves Gerey665174f2018-06-19 15:03:05 +0200144 protected: \
145 std::vector<const webrtc::RTCStatsMemberInterface*> \
146 MembersOfThisObjectAndAncestors(size_t local_var_additional_capacity) \
147 const override; \
148 \
Nico Weber22f99252019-02-20 10:13:16 -0500149 public: \
150 static const char kType[]; \
151 \
152 std::unique_ptr<webrtc::RTCStats> copy() const override; \
153 const char* type() const override
hbosfc5e0502016-10-06 02:06:10 -0700154
155#define WEBRTC_RTCSTATS_IMPL(this_class, parent_class, type_str, ...) \
156 const char this_class::kType[] = type_str; \
157 \
158 std::unique_ptr<webrtc::RTCStats> this_class::copy() const { \
159 return std::unique_ptr<webrtc::RTCStats>(new this_class(*this)); \
160 } \
161 \
Yves Gerey665174f2018-06-19 15:03:05 +0200162 const char* this_class::type() const { return this_class::kType; } \
hbosfc5e0502016-10-06 02:06:10 -0700163 \
164 std::vector<const webrtc::RTCStatsMemberInterface*> \
165 this_class::MembersOfThisObjectAndAncestors( \
166 size_t local_var_additional_capacity) const { \
hbos74e1a4f2016-09-15 23:33:01 -0700167 const webrtc::RTCStatsMemberInterface* local_var_members[] = { \
Yves Gerey665174f2018-06-19 15:03:05 +0200168 __VA_ARGS__}; \
hbos74e1a4f2016-09-15 23:33:01 -0700169 size_t local_var_members_count = \
170 sizeof(local_var_members) / sizeof(local_var_members[0]); \
Yves Gerey665174f2018-06-19 15:03:05 +0200171 std::vector<const webrtc::RTCStatsMemberInterface*> \
172 local_var_members_vec = parent_class::MembersOfThisObjectAndAncestors( \
hbos74e1a4f2016-09-15 23:33:01 -0700173 local_var_members_count + local_var_additional_capacity); \
174 RTC_DCHECK_GE( \
175 local_var_members_vec.capacity() - local_var_members_vec.size(), \
176 local_var_members_count + local_var_additional_capacity); \
177 local_var_members_vec.insert(local_var_members_vec.end(), \
178 &local_var_members[0], \
179 &local_var_members[local_var_members_count]); \
180 return local_var_members_vec; \
hbosfc5e0502016-10-06 02:06:10 -0700181 }
hbos74e1a4f2016-09-15 23:33:01 -0700182
Henrik Boström646fda02019-05-22 15:49:42 +0200183// A version of WEBRTC_RTCSTATS_IMPL() where "..." is omitted, used to avoid a
184// compile error on windows. This is used if the stats dictionary does not
185// declare any members of its own (but perhaps its parent dictionary does).
186#define WEBRTC_RTCSTATS_IMPL_NO_MEMBERS(this_class, parent_class, type_str) \
187 const char this_class::kType[] = type_str; \
188 \
189 std::unique_ptr<webrtc::RTCStats> this_class::copy() const { \
190 return std::unique_ptr<webrtc::RTCStats>(new this_class(*this)); \
191 } \
192 \
193 const char* this_class::type() const { return this_class::kType; } \
194 \
195 std::vector<const webrtc::RTCStatsMemberInterface*> \
196 this_class::MembersOfThisObjectAndAncestors( \
197 size_t local_var_additional_capacity) const { \
198 return parent_class::MembersOfThisObjectAndAncestors(0); \
199 }
200
Jakob Ivarsson22936222019-03-22 11:29:49 +0100201// Non-standard stats members can be exposed to the JavaScript API in Chrome
202// e.g. through origin trials. The group ID can be used by the blink layer to
203// determine if a stats member should be exposed or not. Multiple non-standard
204// stats members can share the same group ID so that they are exposed together.
205enum class NonStandardGroupId {
Jakob Ivarssonaa023e22019-03-27 10:17:31 +0100206 // Group ID used for testing purposes only.
207 kGroupIdForTesting,
Jakob Ivarsson22936222019-03-22 11:29:49 +0100208 // I2E:
209 // https://groups.google.com/a/chromium.org/forum/#!topic/blink-dev/hE2B1iItPDk
210 kRtcAudioJitterBufferMaxPackets,
211 // I2E:
212 // https://groups.google.com/a/chromium.org/forum/#!topic/blink-dev/YbhMyqLXXXo
213 kRtcStatsRelativePacketArrivalDelay,
214};
215
hbos74e1a4f2016-09-15 23:33:01 -0700216// Interface for |RTCStats| members, which have a name and a value of a type
217// defined in a subclass. Only the types listed in |Type| are supported, these
218// are implemented by |RTCStatsMember<T>|. The value of a member may be
219// undefined, the value can only be read if |is_defined|.
220class RTCStatsMemberInterface {
221 public:
222 // Member value types.
223 enum Type {
Yves Gerey665174f2018-06-19 15:03:05 +0200224 kBool, // bool
225 kInt32, // int32_t
226 kUint32, // uint32_t
227 kInt64, // int64_t
228 kUint64, // uint64_t
229 kDouble, // double
230 kString, // std::string
hbos74e1a4f2016-09-15 23:33:01 -0700231
Yves Gerey665174f2018-06-19 15:03:05 +0200232 kSequenceBool, // std::vector<bool>
233 kSequenceInt32, // std::vector<int32_t>
234 kSequenceUint32, // std::vector<uint32_t>
235 kSequenceInt64, // std::vector<int64_t>
236 kSequenceUint64, // std::vector<uint64_t>
237 kSequenceDouble, // std::vector<double>
238 kSequenceString, // std::vector<std::string>
hbos74e1a4f2016-09-15 23:33:01 -0700239 };
240
241 virtual ~RTCStatsMemberInterface() {}
242
243 const char* name() const { return name_; }
244 virtual Type type() const = 0;
245 virtual bool is_sequence() const = 0;
246 virtual bool is_string() const = 0;
247 bool is_defined() const { return is_defined_; }
Taylor Brandstettere2751742018-06-25 13:42:44 -0700248 // Is this part of the stats spec? Used so that chromium can easily filter
249 // out anything unstandardized.
250 virtual bool is_standardized() const = 0;
Jakob Ivarsson22936222019-03-22 11:29:49 +0100251 // Non-standard stats members can have group IDs in order to be exposed in
252 // JavaScript through experiments. Standardized stats have no group IDs.
253 virtual std::vector<NonStandardGroupId> group_ids() const { return {}; }
hbos67c8bc42016-10-25 04:31:23 -0700254 // Type and value comparator. The names are not compared. These operators are
255 // exposed for testing.
256 virtual bool operator==(const RTCStatsMemberInterface& other) const = 0;
257 bool operator!=(const RTCStatsMemberInterface& other) const {
258 return !(*this == other);
259 }
hbos74e1a4f2016-09-15 23:33:01 -0700260 virtual std::string ValueToString() const = 0;
ehmaldonado35a872c2017-07-28 07:29:12 -0700261 // This is the same as ValueToString except for kInt64 and kUint64 types,
262 // where the value is represented as a double instead of as an integer.
263 // Since JSON stores numbers as floating point numbers, very large integers
264 // cannot be accurately represented, so we prefer to display them as doubles
265 // instead.
266 virtual std::string ValueToJson() const = 0;
hbos74e1a4f2016-09-15 23:33:01 -0700267
Yves Gerey665174f2018-06-19 15:03:05 +0200268 template <typename T>
hbos74e1a4f2016-09-15 23:33:01 -0700269 const T& cast_to() const {
270 RTC_DCHECK_EQ(type(), T::kType);
271 return static_cast<const T&>(*this);
272 }
273
274 protected:
275 RTCStatsMemberInterface(const char* name, bool is_defined)
276 : name_(name), is_defined_(is_defined) {}
277
278 const char* const name_;
279 bool is_defined_;
280};
281
282// Template implementation of |RTCStatsMemberInterface|. Every possible |T| is
283// specialized in rtcstats.cc, using a different |T| results in a linker error
284// (undefined reference to |kType|). The supported types are the ones described
285// by |RTCStatsMemberInterface::Type|.
Yves Gerey665174f2018-06-19 15:03:05 +0200286template <typename T>
Mirko Bonadei276827c2018-10-16 14:13:50 +0200287class RTC_EXPORT RTCStatsMember : public RTCStatsMemberInterface {
hbos74e1a4f2016-09-15 23:33:01 -0700288 public:
289 static const Type kType;
290
291 explicit RTCStatsMember(const char* name)
Taylor Brandstettere2751742018-06-25 13:42:44 -0700292 : RTCStatsMemberInterface(name, /*is_defined=*/false), value_() {}
hbos74e1a4f2016-09-15 23:33:01 -0700293 RTCStatsMember(const char* name, const T& value)
Taylor Brandstettere2751742018-06-25 13:42:44 -0700294 : RTCStatsMemberInterface(name, /*is_defined=*/true), value_(value) {}
hbos74e1a4f2016-09-15 23:33:01 -0700295 RTCStatsMember(const char* name, T&& value)
Taylor Brandstettere2751742018-06-25 13:42:44 -0700296 : RTCStatsMemberInterface(name, /*is_defined=*/true),
297 value_(std::move(value)) {}
hbos74e1a4f2016-09-15 23:33:01 -0700298 explicit RTCStatsMember(const RTCStatsMember<T>& other)
299 : RTCStatsMemberInterface(other.name_, other.is_defined_),
300 value_(other.value_) {}
301 explicit RTCStatsMember(RTCStatsMember<T>&& other)
302 : RTCStatsMemberInterface(other.name_, other.is_defined_),
303 value_(std::move(other.value_)) {}
304
305 Type type() const override { return kType; }
306 bool is_sequence() const override;
307 bool is_string() const override;
Taylor Brandstettere2751742018-06-25 13:42:44 -0700308 bool is_standardized() const override { return true; }
hbos67c8bc42016-10-25 04:31:23 -0700309 bool operator==(const RTCStatsMemberInterface& other) const override {
Taylor Brandstettere2751742018-06-25 13:42:44 -0700310 if (type() != other.type() || is_standardized() != other.is_standardized())
hbos67c8bc42016-10-25 04:31:23 -0700311 return false;
312 const RTCStatsMember<T>& other_t =
313 static_cast<const RTCStatsMember<T>&>(other);
314 if (!is_defined_)
315 return !other_t.is_defined();
hbos28747962016-11-21 09:17:41 -0800316 if (!other.is_defined())
317 return false;
hbos67c8bc42016-10-25 04:31:23 -0700318 return value_ == other_t.value_;
319 }
hbos74e1a4f2016-09-15 23:33:01 -0700320 std::string ValueToString() const override;
ehmaldonado35a872c2017-07-28 07:29:12 -0700321 std::string ValueToJson() const override;
hbos74e1a4f2016-09-15 23:33:01 -0700322
323 // Assignment operators.
324 T& operator=(const T& value) {
325 value_ = value;
326 is_defined_ = true;
327 return value_;
328 }
329 T& operator=(const T&& value) {
330 value_ = std::move(value);
331 is_defined_ = true;
332 return value_;
333 }
hbos74e1a4f2016-09-15 23:33:01 -0700334
335 // Value getters.
336 T& operator*() {
337 RTC_DCHECK(is_defined_);
338 return value_;
339 }
340 const T& operator*() const {
341 RTC_DCHECK(is_defined_);
342 return value_;
343 }
344
345 // Value getters, arrow operator.
346 T* operator->() {
347 RTC_DCHECK(is_defined_);
348 return &value_;
349 }
350 const T* operator->() const {
351 RTC_DCHECK(is_defined_);
352 return &value_;
353 }
354
355 private:
356 T value_;
357};
358
Taylor Brandstettere2751742018-06-25 13:42:44 -0700359// Using inheritance just so that it's obvious from the member's declaration
360// whether it's standardized or not.
361template <typename T>
362class RTCNonStandardStatsMember : public RTCStatsMember<T> {
363 public:
364 explicit RTCNonStandardStatsMember(const char* name)
365 : RTCStatsMember<T>(name) {}
Jakob Ivarsson758d9462019-03-19 15:38:49 +0100366 RTCNonStandardStatsMember(const char* name,
367 std::initializer_list<NonStandardGroupId> group_ids)
368 : RTCStatsMember<T>(name), group_ids_(group_ids) {}
Taylor Brandstettere2751742018-06-25 13:42:44 -0700369 RTCNonStandardStatsMember(const char* name, const T& value)
370 : RTCStatsMember<T>(name, value) {}
371 RTCNonStandardStatsMember(const char* name, T&& value)
372 : RTCStatsMember<T>(name, std::move(value)) {}
373 explicit RTCNonStandardStatsMember(const RTCNonStandardStatsMember<T>& other)
Jakob Ivarsson758d9462019-03-19 15:38:49 +0100374 : RTCStatsMember<T>(other), group_ids_(other.group_ids_) {}
Taylor Brandstettere2751742018-06-25 13:42:44 -0700375 explicit RTCNonStandardStatsMember(RTCNonStandardStatsMember<T>&& other)
Jakob Ivarsson758d9462019-03-19 15:38:49 +0100376 : group_ids_(std::move(other.group_ids_)),
377 RTCStatsMember<T>(std::move(other)) {}
Taylor Brandstettere2751742018-06-25 13:42:44 -0700378
379 bool is_standardized() const override { return false; }
Ruslan Burakov8af88962018-11-22 17:21:10 +0100380
Jakob Ivarsson22936222019-03-22 11:29:49 +0100381 std::vector<NonStandardGroupId> group_ids() const override {
382 return group_ids_;
383 }
Jakob Ivarsson758d9462019-03-19 15:38:49 +0100384
Ruslan Burakov8af88962018-11-22 17:21:10 +0100385 T& operator=(const T& value) { return RTCStatsMember<T>::operator=(value); }
386 T& operator=(const T&& value) {
387 return RTCStatsMember<T>::operator=(std::move(value));
388 }
Jakob Ivarsson758d9462019-03-19 15:38:49 +0100389
390 private:
391 std::vector<NonStandardGroupId> group_ids_;
Taylor Brandstettere2751742018-06-25 13:42:44 -0700392};
hbos74e1a4f2016-09-15 23:33:01 -0700393} // namespace webrtc
394
Steve Anton10542f22019-01-11 09:11:00 -0800395#endif // API_STATS_RTC_STATS_H_