blob: b3afee069561f01e5a4e041cbd4650f460481080 [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
11#ifndef WEBRTC_API_STATS_RTCSTATS_H_
12#define WEBRTC_API_STATS_RTCSTATS_H_
13
14#include <map>
15#include <memory>
16#include <string>
17#include <utility>
18#include <vector>
19
20#include "webrtc/base/checks.h"
21
22namespace webrtc {
23
24class 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// }
50class 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;
hbos67c8bc42016-10-25 04:31:23 -070065 // 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.
hbos74e1a4f2016-09-15 23:33:01 -070068 std::vector<const RTCStatsMemberInterface*> Members() const;
hbos67c8bc42016-10-25 04:31:23 -070069 // Checks if the two stats objects are of the same type and have the same
hbos0583b282016-11-30 01:50:14 -080070 // member values. Timestamps are not compared. These operators are exposed for
71 // testing.
hbos67c8bc42016-10-25 04:31:23 -070072 bool operator==(const RTCStats& other) const;
73 bool operator!=(const RTCStats& other) const;
hbos74e1a4f2016-09-15 23:33:01 -070074
hbos6ded1902016-11-01 01:50:46 -070075 // Creates a human readable string representation of the stats object, listing
76 // all of its members (names and values).
hbos74e1a4f2016-09-15 23:33:01 -070077 std::string ToString() const;
78
79 // Downcasts the stats object to an |RTCStats| subclass |T|. DCHECKs that the
80 // object is of type |T|.
81 template<typename T>
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*>
93 MembersOfThisObjectAndAncestors(
94 size_t additional_capacity) const;
95
96 std::string const id_;
97 int64_t timestamp_us_;
98};
99
hbosfc5e0502016-10-06 02:06:10 -0700100// All |RTCStats| classes should use these macros.
101// |WEBRTC_RTCSTATS_DECL| is placed in a public section of the class definition.
102// |WEBRTC_RTCSTATS_IMPL| is placed outside the class definition (in a .cc).
hbos74e1a4f2016-09-15 23:33:01 -0700103//
hbosfc5e0502016-10-06 02:06:10 -0700104// These macros declare (in _DECL) and define (in _IMPL) the static |kType| and
105// overrides methods as required by subclasses of |RTCStats|: |copy|, |type| and
hbos74e1a4f2016-09-15 23:33:01 -0700106// |MembersOfThisObjectAndAncestors|. The |...| argument is a list of addresses
hbosfc5e0502016-10-06 02:06:10 -0700107// to each member defined in the implementing class. The list must have at least
108// one member.
hbos74e1a4f2016-09-15 23:33:01 -0700109//
110// (Since class names need to be known to implement these methods this cannot be
111// part of the base |RTCStats|. While these methods could be implemented using
112// templates, that would only work for immediate subclasses. Subclasses of
113// subclasses also have to override these methods, resulting in boilerplate
114// code. Using a macro avoids this and works for any |RTCStats| class, including
115// grandchildren.)
116//
117// Sample usage:
118//
119// rtcfoostats.h:
120// class RTCFooStats : public RTCStats {
121// public:
hbosfc5e0502016-10-06 02:06:10 -0700122// WEBRTC_RTCSTATS_DECL();
hbos74e1a4f2016-09-15 23:33:01 -0700123//
hbosfc5e0502016-10-06 02:06:10 -0700124// RTCFooStats(const std::string& id, int64_t timestamp_us);
hbos74e1a4f2016-09-15 23:33:01 -0700125//
126// RTCStatsMember<int32_t> foo;
127// RTCStatsMember<int32_t> bar;
128// };
129//
130// rtcfoostats.cc:
hbosfc5e0502016-10-06 02:06:10 -0700131// WEBRTC_RTCSTATS_IMPL(RTCFooStats, RTCStats, "foo-stats"
132// &foo,
133// &bar);
hbos74e1a4f2016-09-15 23:33:01 -0700134//
hbosfc5e0502016-10-06 02:06:10 -0700135// RTCFooStats::RTCFooStats(const std::string& id, int64_t timestamp_us)
136// : RTCStats(id, timestamp_us),
137// foo("foo"),
138// bar("bar") {
139// }
140//
141#define WEBRTC_RTCSTATS_DECL() \
hbos74e1a4f2016-09-15 23:33:01 -0700142 public: \
143 static const char kType[]; \
hbosfc5e0502016-10-06 02:06:10 -0700144 \
145 std::unique_ptr<webrtc::RTCStats> copy() const override; \
146 const char* type() const override; \
147 \
hbos74e1a4f2016-09-15 23:33:01 -0700148 protected: \
149 std::vector<const webrtc::RTCStatsMemberInterface*> \
150 MembersOfThisObjectAndAncestors( \
hbosfc5e0502016-10-06 02:06:10 -0700151 size_t local_var_additional_capacity) const override; \
152 \
153 public:
154
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 \
162 const char* this_class::type() const { \
163 return this_class::kType; \
164 } \
165 \
166 std::vector<const webrtc::RTCStatsMemberInterface*> \
167 this_class::MembersOfThisObjectAndAncestors( \
168 size_t local_var_additional_capacity) const { \
hbos74e1a4f2016-09-15 23:33:01 -0700169 const webrtc::RTCStatsMemberInterface* local_var_members[] = { \
170 __VA_ARGS__ \
171 }; \
172 size_t local_var_members_count = \
173 sizeof(local_var_members) / sizeof(local_var_members[0]); \
174 std::vector<const webrtc::RTCStatsMemberInterface*> local_var_members_vec =\
175 parent_class::MembersOfThisObjectAndAncestors( \
176 local_var_members_count + local_var_additional_capacity); \
177 RTC_DCHECK_GE( \
178 local_var_members_vec.capacity() - local_var_members_vec.size(), \
179 local_var_members_count + local_var_additional_capacity); \
180 local_var_members_vec.insert(local_var_members_vec.end(), \
181 &local_var_members[0], \
182 &local_var_members[local_var_members_count]); \
183 return local_var_members_vec; \
hbosfc5e0502016-10-06 02:06:10 -0700184 }
hbos74e1a4f2016-09-15 23:33:01 -0700185
186// Interface for |RTCStats| members, which have a name and a value of a type
187// defined in a subclass. Only the types listed in |Type| are supported, these
188// are implemented by |RTCStatsMember<T>|. The value of a member may be
189// undefined, the value can only be read if |is_defined|.
190class RTCStatsMemberInterface {
191 public:
192 // Member value types.
193 enum Type {
hbosb20f3872016-10-04 14:37:11 -0700194 kBool, // bool
hbos74e1a4f2016-09-15 23:33:01 -0700195 kInt32, // int32_t
196 kUint32, // uint32_t
197 kInt64, // int64_t
198 kUint64, // uint64_t
199 kDouble, // double
200 kString, // std::string
201
hbosb20f3872016-10-04 14:37:11 -0700202 kSequenceBool, // std::vector<bool>
hbos74e1a4f2016-09-15 23:33:01 -0700203 kSequenceInt32, // std::vector<int32_t>
204 kSequenceUint32, // std::vector<uint32_t>
205 kSequenceInt64, // std::vector<int64_t>
206 kSequenceUint64, // std::vector<uint64_t>
207 kSequenceDouble, // std::vector<double>
208 kSequenceString, // std::vector<std::string>
209 };
210
211 virtual ~RTCStatsMemberInterface() {}
212
213 const char* name() const { return name_; }
214 virtual Type type() const = 0;
215 virtual bool is_sequence() const = 0;
216 virtual bool is_string() const = 0;
217 bool is_defined() const { return is_defined_; }
hbos67c8bc42016-10-25 04:31:23 -0700218 // Type and value comparator. The names are not compared. These operators are
219 // exposed for testing.
220 virtual bool operator==(const RTCStatsMemberInterface& other) const = 0;
221 bool operator!=(const RTCStatsMemberInterface& other) const {
222 return !(*this == other);
223 }
hbos74e1a4f2016-09-15 23:33:01 -0700224 virtual std::string ValueToString() const = 0;
225
226 template<typename T>
227 const T& cast_to() const {
228 RTC_DCHECK_EQ(type(), T::kType);
229 return static_cast<const T&>(*this);
230 }
231
232 protected:
233 RTCStatsMemberInterface(const char* name, bool is_defined)
234 : name_(name), is_defined_(is_defined) {}
235
236 const char* const name_;
237 bool is_defined_;
238};
239
240// Template implementation of |RTCStatsMemberInterface|. Every possible |T| is
241// specialized in rtcstats.cc, using a different |T| results in a linker error
242// (undefined reference to |kType|). The supported types are the ones described
243// by |RTCStatsMemberInterface::Type|.
244template<typename T>
245class RTCStatsMember : public RTCStatsMemberInterface {
246 public:
247 static const Type kType;
248
249 explicit RTCStatsMember(const char* name)
250 : RTCStatsMemberInterface(name, false),
251 value_() {}
252 RTCStatsMember(const char* name, const T& value)
253 : RTCStatsMemberInterface(name, true),
254 value_(value) {}
255 RTCStatsMember(const char* name, T&& value)
256 : RTCStatsMemberInterface(name, true),
257 value_(std::move(value)) {}
258 explicit RTCStatsMember(const RTCStatsMember<T>& other)
259 : RTCStatsMemberInterface(other.name_, other.is_defined_),
260 value_(other.value_) {}
261 explicit RTCStatsMember(RTCStatsMember<T>&& other)
262 : RTCStatsMemberInterface(other.name_, other.is_defined_),
263 value_(std::move(other.value_)) {}
264
265 Type type() const override { return kType; }
266 bool is_sequence() const override;
267 bool is_string() const override;
hbos67c8bc42016-10-25 04:31:23 -0700268 bool operator==(const RTCStatsMemberInterface& other) const override {
269 if (type() != other.type())
270 return false;
271 const RTCStatsMember<T>& other_t =
272 static_cast<const RTCStatsMember<T>&>(other);
273 if (!is_defined_)
274 return !other_t.is_defined();
hbos28747962016-11-21 09:17:41 -0800275 if (!other.is_defined())
276 return false;
hbos67c8bc42016-10-25 04:31:23 -0700277 return value_ == other_t.value_;
278 }
hbos74e1a4f2016-09-15 23:33:01 -0700279 std::string ValueToString() const override;
280
281 // Assignment operators.
282 T& operator=(const T& value) {
283 value_ = value;
284 is_defined_ = true;
285 return value_;
286 }
287 T& operator=(const T&& value) {
288 value_ = std::move(value);
289 is_defined_ = true;
290 return value_;
291 }
292 T& operator=(const RTCStatsMember<T>& other) {
293 RTC_DCHECK(other.is_defined_);
294 value_ = other.is_defined_;
295 is_defined_ = true;
296 return value_;
297 }
298
299 // Value getters.
300 T& operator*() {
301 RTC_DCHECK(is_defined_);
302 return value_;
303 }
304 const T& operator*() const {
305 RTC_DCHECK(is_defined_);
306 return value_;
307 }
308
309 // Value getters, arrow operator.
310 T* operator->() {
311 RTC_DCHECK(is_defined_);
312 return &value_;
313 }
314 const T* operator->() const {
315 RTC_DCHECK(is_defined_);
316 return &value_;
317 }
318
319 private:
320 T value_;
321};
322
323} // namespace webrtc
324
325#endif // WEBRTC_API_STATS_RTCSTATS_H_