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

Side by Side Diff: webrtc/stats/rtcstats.cc

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 #include "webrtc/api/rtcstats.h"
12
13 #include "webrtc/base/stringencode.h"
14
15 namespace webrtc {
16
17 namespace {
18
19 // Produces "{ a, b, c }". Works for non-vector |RTCStatsMemberInterface::Type|
20 // types.
21 template<typename T>
22 std::string VectorToString(const std::vector<T>& vector) {
23 if (vector.empty())
24 return "{}";
25 std::ostringstream oss;
26 bool is_first = true;
27 for (const T& element : vector) {
28 if (is_first) {
29 oss << "{ ";
30 is_first = false;
31 } else {
32 oss << ", ";
33 }
34 oss << rtc::ToString<T>(element);
35 }
36 oss << " }";
37 return oss.str();
38 }
39
40 // Produces "{ \"a\", \"b\", \"c\" }". Works for vectors of both const char* and
41 // std::string element types.
42 template<typename T>
43 std::string VectorOfStringsToString(const std::vector<T>& strings) {
44 if (strings.empty())
45 return "{}";
46 std::ostringstream oss;
47 bool is_first = true;
48 for (const T& string : strings) {
49 if (is_first) {
50 oss << "{ ";
51 is_first = false;
52 } else {
53 oss << ", ";
54 }
55 oss << '"' << string << '"';
56 }
57 oss << " }";
58 return oss.str();
59 }
60
61 } // namespace
62
63 std::string RTCStats::ToString() const {
64 std::ostringstream oss;
65 oss << type_name() << " {\n id: \"" << id_ << "\"\n timestamp: "
66 << timestamp_ << '\n';
67 for (const RTCStatsMemberInterface* member : Members()) {
68 oss << " " << member->name() << ": ";
69 if (member->has_value()) {
70 if (member->is_string() && !member->is_sequence())
71 oss << '"' << member->ValueToString() << "\"\n";
72 else
73 oss << member->ValueToString() << '\n';
74 } else {
75 oss << "undefined\n";
76 }
77 }
78 oss << '}';
79 return oss.str();
80 }
81
82 std::vector<const RTCStatsMemberInterface*> RTCStats::MembersVector(
83 const RTCStatsMemberInterface** members, size_t members_count) {
84 return std::vector<const RTCStatsMemberInterface*>(
85 &members[0], &members[members_count]);
86 }
87
88 // int32_t (kInt32)
89 template<>
90 const RTCStatsMemberInterface::Type RTCStatsMember<int32_t>::kType =
91 RTCStatsMemberInterface::kInt32;
92
93 template<>
94 bool RTCStatsMember<int32_t>::is_sequence() const { return false; }
95
96 template<>
97 bool RTCStatsMember<int32_t>::is_string() const { return false; }
98
99 template<>
100 std::string RTCStatsMember<int32_t>::ValueToString() const {
101 RTC_DCHECK(has_value_);
102 return rtc::ToString<int32_t>(value_);
103 }
104
105 // uint32_t (kUint32)
106 template<>
107 const RTCStatsMemberInterface::Type RTCStatsMember<uint32_t>::kType =
108 RTCStatsMemberInterface::kUint32;
109
110 template<>
111 bool RTCStatsMember<uint32_t>::is_sequence() const { return false; }
112
113 template<>
114 bool RTCStatsMember<uint32_t>::is_string() const { return false; }
115
116 template<>
117 std::string RTCStatsMember<uint32_t>::ValueToString() const {
118 RTC_DCHECK(has_value_);
119 return rtc::ToString<uint32_t>(value_);
120 }
121
122 // int64_t (kInt64)
123 template<>
124 const RTCStatsMemberInterface::Type RTCStatsMember<int64_t>::kType =
125 RTCStatsMemberInterface::kInt64;
126
127 template<>
128 bool RTCStatsMember<int64_t>::is_sequence() const { return false; }
129
130 template<>
131 bool RTCStatsMember<int64_t>::is_string() const { return false; }
132
133 template<>
134 std::string RTCStatsMember<int64_t>::ValueToString() const {
135 RTC_DCHECK(has_value_);
136 return rtc::ToString<int64_t>(value_);
137 }
138
139 // uint64_t (kUint64)
140 template<>
141 const RTCStatsMemberInterface::Type RTCStatsMember<uint64_t>::kType =
142 RTCStatsMemberInterface::kUint64;
143
144 template<>
145 bool RTCStatsMember<uint64_t>::is_sequence() const { return false; }
146
147 template<>
148 bool RTCStatsMember<uint64_t>::is_string() const { return false; }
149
150 template<>
151 std::string RTCStatsMember<uint64_t>::ValueToString() const {
152 RTC_DCHECK(has_value_);
153 return rtc::ToString<uint64_t>(value_);
154 }
155
156 // double (kDouble)
157 template<>
158 const RTCStatsMemberInterface::Type RTCStatsMember<double>::kType =
159 RTCStatsMemberInterface::kDouble;
160
161 template<>
162 bool RTCStatsMember<double>::is_sequence() const { return false; }
163
164 template<>
165 bool RTCStatsMember<double>::is_string() const { return false; }
166
167 template<>
168 std::string RTCStatsMember<double>::ValueToString() const {
169 RTC_DCHECK(has_value_);
170 return rtc::ToString<double>(value_);
171 }
172
173 // const char* (kStaticString)
174 template<>
175 const RTCStatsMemberInterface::Type RTCStatsMember<const char*>::kType =
176 RTCStatsMemberInterface::kStaticString;
177
178 template<>
179 bool RTCStatsMember<const char*>::is_sequence() const { return false; }
180
181 template<>
182 bool RTCStatsMember<const char*>::is_string() const { return true; }
183
184 template<>
185 std::string RTCStatsMember<const char*>::ValueToString() const {
186 RTC_DCHECK(has_value_);
187 return value_;
188 }
189
190 // std::string (kString)
191 template<>
192 const RTCStatsMemberInterface::Type RTCStatsMember<std::string>::kType =
193 RTCStatsMemberInterface::kString;
194
195 template<>
196 bool RTCStatsMember<std::string>::is_sequence() const { return false; }
197
198 template<>
199 bool RTCStatsMember<std::string>::is_string() const { return true; }
200
201 template<>
202 std::string RTCStatsMember<std::string>::ValueToString() const {
203 RTC_DCHECK(has_value_);
204 return value_;
205 }
206
207 // std::vector<int32_t> (kSequenceInt32)
208 template<>
209 const RTCStatsMemberInterface::Type
210 RTCStatsMember<std::vector<int32_t>>::kType =
211 RTCStatsMemberInterface::kSequenceInt32;
212
213 template<>
214 bool RTCStatsMember<std::vector<int32_t>>::is_sequence() const { return true; }
215
216 template<>
217 bool RTCStatsMember<std::vector<int32_t>>::is_string() const { return false; }
218
219 template<>
220 std::string RTCStatsMember<std::vector<int32_t>>::ValueToString() const {
221 RTC_DCHECK(has_value_);
222 return VectorToString<int32_t>(value_);
223 }
224
225 // std::vector<uint32_t> (kSequenceUint32)
226 template<>
227 const RTCStatsMemberInterface::Type
228 RTCStatsMember<std::vector<uint32_t>>::kType =
229 RTCStatsMemberInterface::kSequenceUint32;
230
231 template<>
232 bool RTCStatsMember<std::vector<uint32_t>>::is_sequence() const { return true; }
233
234 template<>
235 bool RTCStatsMember<std::vector<uint32_t>>::is_string() const { return false; }
236
237 template<>
238 std::string RTCStatsMember<std::vector<uint32_t>>::ValueToString() const {
239 RTC_DCHECK(has_value_);
240 return VectorToString<uint32_t>(value_);
241 }
242
243 // std::vector<int64_t> (kSequenceInt64)
244 template<>
245 const RTCStatsMemberInterface::Type
246 RTCStatsMember<std::vector<int64_t>>::kType =
247 RTCStatsMemberInterface::kSequenceInt64;
248
249 template<>
250 bool RTCStatsMember<std::vector<int64_t>>::is_sequence() const { return true; }
251
252 template<>
253 bool RTCStatsMember<std::vector<int64_t>>::is_string() const { return false; }
254
255 template<>
256 std::string RTCStatsMember<std::vector<int64_t>>::ValueToString() const {
257 RTC_DCHECK(has_value_);
258 return VectorToString<int64_t>(value_);
259 }
260
261 // std::vector<uint64_t> (kSequenceUint64)
262 template<>
263 const RTCStatsMemberInterface::Type
264 RTCStatsMember<std::vector<uint64_t>>::kType =
265 RTCStatsMemberInterface::kSequenceUint64;
266
267 template<>
268 bool RTCStatsMember<std::vector<uint64_t>>::is_sequence() const { return true; }
269
270 template<>
271 bool RTCStatsMember<std::vector<uint64_t>>::is_string() const { return false; }
272
273 template<>
274 std::string RTCStatsMember<std::vector<uint64_t>>::ValueToString() const {
275 RTC_DCHECK(has_value_);
276 return VectorToString<uint64_t>(value_);
277 }
278
279 // std::vector<double> (kSequenceDouble)
280 template<>
281 const RTCStatsMemberInterface::Type
282 RTCStatsMember<std::vector<double>>::kType =
283 RTCStatsMemberInterface::kSequenceDouble;
284
285 template<>
286 bool RTCStatsMember<std::vector<double>>::is_sequence() const { return true; }
287
288 template<>
289 bool RTCStatsMember<std::vector<double>>::is_string() const { return false; }
290
291 template<>
292 std::string RTCStatsMember<std::vector<double>>::ValueToString() const {
293 RTC_DCHECK(has_value_);
294 return VectorToString<double>(value_);
295 }
296
297 // std::vector<const char*> (kSequenceStaticString)
298 template<>
299 const RTCStatsMemberInterface::Type
300 RTCStatsMember<std::vector<const char*>>::kType =
301 RTCStatsMemberInterface::kSequenceStaticString;
302
303 template<>
304 bool RTCStatsMember<std::vector<const char*>>::is_sequence() const {
305 return true;
306 }
307
308 template<>
309 bool RTCStatsMember<std::vector<const char*>>::is_string() const {
310 return true;
311 }
312
313 template<>
314 std::string RTCStatsMember<std::vector<const char*>>::ValueToString() const {
315 RTC_DCHECK(has_value_);
316 return VectorOfStringsToString<const char*>(value_);
317 }
318
319 // std::vector<std::string> (kSequenceString)
320 template<>
321 const RTCStatsMemberInterface::Type
322 RTCStatsMember<std::vector<std::string>>::kType =
323 RTCStatsMemberInterface::kSequenceString;
324
325 template<>
326 bool RTCStatsMember<std::vector<std::string>>::is_sequence() const {
327 return true;
328 }
329
330 template<>
331 bool RTCStatsMember<std::vector<std::string>>::is_string() const {
332 return true;
333 }
334
335 template<>
336 std::string RTCStatsMember<std::vector<std::string>>::ValueToString() const {
337 RTC_DCHECK(has_value_);
338 return VectorOfStringsToString<std::string>(value_);
339 }
340
341 } // namespace webrtc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698