Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1788)

Side by Side Diff: webrtc/api/rtcstats.h

Issue 2241093002: RTCStats and RTCStatsReport added (webrtc/stats) (Closed) Base URL: https://chromium.googlesource.com/external/webrtc.git@master
Patch Set: Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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
11 #ifndef WEBRTC_API_RTCSTATS_H_
12 #define WEBRTC_API_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
22 namespace webrtc {
23
24 class RTCStatsMemberInterface;
25
26 // Abstract base class for RTCStats-derived dictionaries, see
27 // http://rawgit.com/w3c/webrtc-stats/master/webrtc-stats.html.
28 //
29 // All derived classes must have the following static variable defined:
30 // static const char* const kType;
31 // The address of |kType| is used as a unique class identifier and the value as
32 // a string representation of the class type, see
33 // http://rawgit.com/w3c/webrtc-stats/master/webrtc-stats.html#rtcstatstype-str* .
34 //
35 // Derived classes list their dictionary members, RTCStatsMember<T>, as public
36 // fields, allowing the following:
37 //
38 // RTCFooStats foo("fooId", GetCurrentTime());
39 // foo.bar = 42;
40 // foo.baz = std::vector<std::string>();
41 // foo.baz->push_back("hello world");
42 // uint32_t x = *foo.bar;
43 //
44 // Pointers to all the members are available with |Members|, allowing iteration:
45 //
46 // for (const RTCStatsMemberInterface* member : foo.Members())
47 // printf("%s = %s\n", member->name(), member->ValueToString().c_str());
48 class RTCStats {
49 public:
50 RTCStats(const std::string& id, double timestamp)
51 : id_(id), timestamp_(timestamp) {}
52 RTCStats(std::string&& id, double timestamp)
53 : id_(std::move(id)), timestamp_(timestamp) {}
54 virtual ~RTCStats() {}
55
56 virtual std::unique_ptr<RTCStats> copy() const = 0;
57
58 const std::string& id() const { return id_; }
59 // Time in milliseconds relative to the UNIX epoch (Jan 1, 1970, UTC).
60 double timestamp() const { return timestamp_; }
61 // Returns the address of the static |kType| variable of the implementing
62 // class. Comparing it to |&T::kType| tests if a stats object is of type |T|.
63 virtual const char* const* type() const = 0;
64 // Returns the value of the static |kType| variable of the implementing class.
65 virtual const char* type_name() const = 0;
66 // Returns a vector of pointers to all the RTCStatsMemberInterface members of
67 // this class. This allows for iteration of members.
68 virtual std::vector<const RTCStatsMemberInterface*> Members() const = 0;
69
70 std::string ToString() const;
71
72 template<typename T>
73 T& to() const {
74 RTC_DCHECK_EQ(type(), &T::kType);
75 return static_cast<T&>(*this);
76 }
77
78 protected:
79 static std::vector<const RTCStatsMemberInterface*> MembersVector(
80 const RTCStatsMemberInterface** members, size_t members_count);
81
82 std::string const id_;
83 double timestamp_;
84 };
85
86 // Interface for |RTCStats| members, which have a name and an optional value.
87 // The value type is implementation-specific. Only the types listed in |Type|
88 // are supported, these are implemented by |RTCStatsMember<T>|.
89 class RTCStatsMemberInterface {
90 public:
91 // Member value types.
92 enum Type {
93 kInt32, // int32_t
94 kUint32, // uint32_t
95 kInt64, // int64_t
96 kUint64, // uint64_t
97 kDouble, // double
98 kStaticString, // const char*
99 kString, // std::string
100
101 kSequenceInt32, // std::vector<int32_t>
102 kSequenceUint32, // std::vector<uint32_t>
103 kSequenceInt64, // std::vector<int64_t>
104 kSequenceUint64, // std::vector<uint64_t>
105 kSequenceDouble, // std::vector<double>
106 kSequenceStaticString, // std::vector<const char*>
107 kSequenceString, // std::vector<std::string>
108 };
109
110 virtual ~RTCStatsMemberInterface() {}
111
112 const char* name() const { return name_; }
113 virtual Type type() const = 0;
114 virtual bool is_sequence() const = 0;
115 virtual bool is_string() const = 0; // true for sequences of strings too.
116 bool has_value() const { return has_value_; }
117 virtual std::string ValueToString() const = 0;
118
119 template<typename T>
120 T& to() const {
121 RTC_DCHECK_EQ(type(), T::kType);
122 return static_cast<T&>(*this);
123 }
124
125 protected:
126 RTCStatsMemberInterface(const char* name, bool has_value)
127 : name_(name), has_value_(has_value) {}
128
129 const char* const name_;
130 bool has_value_;
131 };
132
133 // Template implementation of |RTCStatsMemberInterface|. Every possible |T| is
134 // specialized in rtcstats.cc, using a different |T| results in a linker error
135 // (undefined reference to |kType|). The supported types are the ones described
136 // by |RTCStatsMemberInterface::Type|.
137 template<typename T>
138 class RTCStatsMember : public RTCStatsMemberInterface {
139 public:
140 static const Type kType;
141
142 explicit RTCStatsMember(const char* name)
143 : RTCStatsMemberInterface(name, false),
144 value_() {}
145 RTCStatsMember(const char* name, const T& value)
146 : RTCStatsMemberInterface(name, true),
147 value_(value) {}
148 RTCStatsMember(const char* name, T&& value)
149 : RTCStatsMemberInterface(name, true),
150 value_(std::move(value)) {}
151 explicit RTCStatsMember(const RTCStatsMember<T>& other)
152 : RTCStatsMemberInterface(other.name_, other.has_value_),
153 value_(other.value_) {}
154 explicit RTCStatsMember(RTCStatsMember<T>&& other)
155 : RTCStatsMemberInterface(other.name_, other.has_value_),
156 value_(std::move(other.value_)) {}
157
158 Type type() const override { return kType; }
159 bool is_sequence() const override;
160 bool is_string() const override;
161 std::string ValueToString() const override;
162
163 // Assignment operators.
164 T& operator=(const T& value) {
165 value_ = value;
166 has_value_ = true;
167 return value_;
168 }
169 T& operator=(const T&& value) {
170 value_ = std::move(value);
171 has_value_ = true;
172 return value_;
173 }
174 T& operator=(const RTCStatsMember<T>& other) {
175 RTC_DCHECK(other.has_value_);
176 value_ = other.has_value_;
177 has_value_ = true;
178 return value_;
179 }
180
181 // Value getters.
182 T& operator*() {
183 RTC_DCHECK(has_value_);
184 return value_;
185 }
186 const T& operator*() const {
187 RTC_DCHECK(has_value_);
188 return value_;
189 }
190
191 // Value getters, arrow operator.
192 T* operator->() {
193 RTC_DCHECK(has_value_);
194 return &value_;
195 }
196 const T* operator->() const {
197 RTC_DCHECK(has_value_);
198 return &value_;
199 }
200
201 private:
202 T value_;
203 };
204
205 } // namespace webrtc
206
207 #endif // WEBRTC_API_RTCSTATS_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698