blob: 4f58b9754033063fb23d9e337fc5a34158680b97 [file] [log] [blame]
Henrik Kjellanderbd0ae452016-02-10 09:53:121/*
kjellander95ed4e62016-02-10 15:54:432 * Copyright 2012 The WebRTC project authors. All Rights Reserved.
Henrik Kjellanderbd0ae452016-02-10 09:53:123 *
kjellander95ed4e62016-02-10 15:54:434 * 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.
Henrik Kjellanderbd0ae452016-02-10 09:53:129 */
10
11// This file contains structures used for retrieving statistics from an ongoing
12// libjingle session.
13
14#ifndef WEBRTC_API_STATSTYPES_H_
15#define WEBRTC_API_STATSTYPES_H_
16
17#include <algorithm>
18#include <list>
19#include <map>
20#include <string>
21
22#include "webrtc/base/basictypes.h"
23#include "webrtc/base/common.h"
kwiberg5fb5bd22016-04-26 15:14:3924#include "webrtc/base/constructormagic.h"
Henrik Kjellanderbd0ae452016-02-10 09:53:1225#include "webrtc/base/linked_ptr.h"
26#include "webrtc/base/refcount.h"
Henrik Kjellanderbd0ae452016-02-10 09:53:1227#include "webrtc/base/scoped_ref_ptr.h"
28#include "webrtc/base/stringencode.h"
29#include "webrtc/base/thread_checker.h"
30
31namespace webrtc {
32
33class StatsReport {
34 public:
35 // Indicates whether a track is for sending or receiving.
36 // Used in reports for audio/video tracks.
37 enum Direction {
38 kSend = 0,
39 kReceive,
40 };
41
42 enum StatsType {
43 // StatsReport types.
44 // A StatsReport of |type| = "googSession" contains overall information
45 // about the thing libjingle calls a session (which may contain one
46 // or more RTP sessions.
47 kStatsReportTypeSession,
48
49 // A StatsReport of |type| = "googTransport" contains information
50 // about a libjingle "transport".
51 kStatsReportTypeTransport,
52
53 // A StatsReport of |type| = "googComponent" contains information
54 // about a libjingle "channel" (typically, RTP or RTCP for a transport).
55 // This is intended to be the same thing as an ICE "Component".
56 kStatsReportTypeComponent,
57
58 // A StatsReport of |type| = "googCandidatePair" contains information
59 // about a libjingle "connection" - a single source/destination port pair.
60 // This is intended to be the same thing as an ICE "candidate pair".
61 kStatsReportTypeCandidatePair,
62
63 // A StatsReport of |type| = "VideoBWE" is statistics for video Bandwidth
64 // Estimation, which is global per-session. The |id| field is "bweforvideo"
65 // (will probably change in the future).
66 kStatsReportTypeBwe,
67
68 // A StatsReport of |type| = "ssrc" is statistics for a specific rtp stream.
69 // The |id| field is the SSRC in decimal form of the rtp stream.
70 kStatsReportTypeSsrc,
71
72 // A StatsReport of |type| = "remoteSsrc" is statistics for a specific
73 // rtp stream, generated by the remote end of the connection.
74 kStatsReportTypeRemoteSsrc,
75
76 // A StatsReport of |type| = "googTrack" is statistics for a specific media
77 // track. The |id| field is the track id.
78 kStatsReportTypeTrack,
79
80 // A StatsReport of |type| = "localcandidate" or "remotecandidate" is
81 // attributes on a specific ICE Candidate. It links to its connection pair
82 // by candidate id. The string value is taken from
83 // http://w3c.github.io/webrtc-stats/#rtcstatstype-enum*.
84 kStatsReportTypeIceLocalCandidate,
85 kStatsReportTypeIceRemoteCandidate,
86
87 // A StatsReport of |type| = "googCertificate" contains an SSL certificate
88 // transmitted by one of the endpoints of this connection. The |id| is
89 // controlled by the fingerprint, and is used to identify the certificate in
90 // the Channel stats (as "googLocalCertificateId" or
91 // "googRemoteCertificateId") and in any child certificates (as
92 // "googIssuerId").
93 kStatsReportTypeCertificate,
94
95 // A StatsReport of |type| = "datachannel" with statistics for a
96 // particular DataChannel.
97 kStatsReportTypeDataChannel,
98 };
99
100 enum StatsValueName {
101 kStatsValueNameActiveConnection,
Minyuef20e12f2016-04-07 14:48:15102 kStatsValueNameAecDivergentFilterFraction,
Henrik Kjellanderbd0ae452016-02-10 09:53:12103 kStatsValueNameAudioInputLevel,
104 kStatsValueNameAudioOutputLevel,
105 kStatsValueNameBytesReceived,
106 kStatsValueNameBytesSent,
107 kStatsValueNameCodecImplementationName,
108 kStatsValueNameDataChannelId,
109 kStatsValueNameMediaType,
110 kStatsValueNamePacketsLost,
111 kStatsValueNamePacketsReceived,
112 kStatsValueNamePacketsSent,
113 kStatsValueNameProtocol,
114 kStatsValueNameReceiving,
115 kStatsValueNameSelectedCandidatePairId,
116 kStatsValueNameSsrc,
117 kStatsValueNameState,
118 kStatsValueNameTransportId,
119
120 // Internal StatsValue names.
121 kStatsValueNameAccelerateRate,
122 kStatsValueNameActualEncBitrate,
123 kStatsValueNameAdaptationChanges,
124 kStatsValueNameAvailableReceiveBandwidth,
125 kStatsValueNameAvailableSendBandwidth,
126 kStatsValueNameAvgEncodeMs,
127 kStatsValueNameBandwidthLimitedResolution,
128 kStatsValueNameBucketDelay,
129 kStatsValueNameCaptureStartNtpTimeMs,
130 kStatsValueNameCandidateIPAddress,
131 kStatsValueNameCandidateNetworkType,
132 kStatsValueNameCandidatePortNumber,
133 kStatsValueNameCandidatePriority,
134 kStatsValueNameCandidateTransportType,
135 kStatsValueNameCandidateType,
136 kStatsValueNameChannelId,
137 kStatsValueNameCodecName,
138 kStatsValueNameComponent,
139 kStatsValueNameContentName,
140 kStatsValueNameCpuLimitedResolution,
141 kStatsValueNameCurrentDelayMs,
142 kStatsValueNameDecodeMs,
143 kStatsValueNameDecodingCNG,
144 kStatsValueNameDecodingCTN,
145 kStatsValueNameDecodingCTSG,
146 kStatsValueNameDecodingNormal,
147 kStatsValueNameDecodingPLC,
148 kStatsValueNameDecodingPLCCNG,
149 kStatsValueNameDer,
150 kStatsValueNameDtlsCipher,
151 kStatsValueNameEchoCancellationQualityMin,
152 kStatsValueNameEchoDelayMedian,
153 kStatsValueNameEchoDelayStdDev,
154 kStatsValueNameEchoReturnLoss,
155 kStatsValueNameEchoReturnLossEnhancement,
156 kStatsValueNameEncodeUsagePercent,
157 kStatsValueNameExpandRate,
158 kStatsValueNameFingerprint,
159 kStatsValueNameFingerprintAlgorithm,
160 kStatsValueNameFirsReceived,
161 kStatsValueNameFirsSent,
162 kStatsValueNameFrameHeightInput,
163 kStatsValueNameFrameHeightReceived,
164 kStatsValueNameFrameHeightSent,
165 kStatsValueNameFrameRateDecoded,
166 kStatsValueNameFrameRateInput,
167 kStatsValueNameFrameRateOutput,
168 kStatsValueNameFrameRateReceived,
169 kStatsValueNameFrameRateSent,
170 kStatsValueNameFrameWidthInput,
171 kStatsValueNameFrameWidthReceived,
172 kStatsValueNameFrameWidthSent,
173 kStatsValueNameInitiator,
174 kStatsValueNameIssuerId,
175 kStatsValueNameJitterBufferMs,
176 kStatsValueNameJitterReceived,
177 kStatsValueNameLabel,
178 kStatsValueNameLocalAddress,
179 kStatsValueNameLocalCandidateId,
180 kStatsValueNameLocalCandidateType,
181 kStatsValueNameLocalCertificateId,
182 kStatsValueNameMaxDecodeMs,
183 kStatsValueNameMinPlayoutDelayMs,
184 kStatsValueNameNacksReceived,
185 kStatsValueNameNacksSent,
186 kStatsValueNamePlisReceived,
187 kStatsValueNamePlisSent,
188 kStatsValueNamePreemptiveExpandRate,
189 kStatsValueNamePreferredJitterBufferMs,
190 kStatsValueNameRemoteAddress,
191 kStatsValueNameRemoteCandidateId,
192 kStatsValueNameRemoteCandidateType,
193 kStatsValueNameRemoteCertificateId,
194 kStatsValueNameRenderDelayMs,
195 kStatsValueNameRetransmitBitrate,
196 kStatsValueNameRtt,
197 kStatsValueNameSecondaryDecodedRate,
198 kStatsValueNameSendPacketsDiscarded,
199 kStatsValueNameSpeechExpandRate,
200 kStatsValueNameSrtpCipher,
201 kStatsValueNameTargetDelayMs,
202 kStatsValueNameTargetEncBitrate,
203 kStatsValueNameTrackId,
204 kStatsValueNameTransmitBitrate,
205 kStatsValueNameTransportType,
206 kStatsValueNameTypingNoiseState,
207 kStatsValueNameViewLimitedResolution,
208 kStatsValueNameWritable,
209 };
210
211 class IdBase : public rtc::RefCountInterface {
212 public:
213 ~IdBase() override;
214 StatsType type() const;
215
216 // Users of IdBase will be using the Id typedef, which is compatible with
217 // this Equals() function. It simply calls the protected (and overridden)
218 // Equals() method.
219 bool Equals(const rtc::scoped_refptr<IdBase>& other) const {
220 return Equals(*other.get());
221 }
222
223 virtual std::string ToString() const = 0;
224
225 protected:
226 // Protected since users of the IdBase type will be using the Id typedef.
227 virtual bool Equals(const IdBase& other) const;
228
229 IdBase(StatsType type); // Only meant for derived classes.
230 const StatsType type_;
231
232 static const char kSeparator = '_';
233 };
234
235 typedef rtc::scoped_refptr<IdBase> Id;
236
237 struct Value {
238 enum Type {
239 kInt, // int.
240 kInt64, // int64_t.
241 kFloat, // float.
242 kString, // std::string
243 kStaticString, // const char*.
244 kBool, // bool.
245 kId, // Id.
246 };
247
248 Value(StatsValueName name, int64_t value, Type int_type);
249 Value(StatsValueName name, float f);
250 Value(StatsValueName name, const std::string& value);
251 Value(StatsValueName name, const char* value);
252 Value(StatsValueName name, bool b);
253 Value(StatsValueName name, const Id& value);
254
255 ~Value();
256
257 // TODO(tommi): This compares name as well as value...
258 // I think we should only need to compare the value part and
259 // move the name part into a hash map.
260 bool Equals(const Value& other) const;
261
262 // Comparison operators. Return true iff the current instance is of the
263 // correct type and holds the same value. No conversion is performed so
264 // a string value of "123" is not equal to an int value of 123 and an int
265 // value of 123 is not equal to a float value of 123.0f.
266 // One exception to this is that types kInt and kInt64 can be compared and
267 // kString and kStaticString too.
268 bool operator==(const std::string& value) const;
269 bool operator==(const char* value) const;
270 bool operator==(int64_t value) const;
271 bool operator==(bool value) const;
272 bool operator==(float value) const;
273 bool operator==(const Id& value) const;
274
275 // Getters that allow getting the native value directly.
276 // The caller must know the type beforehand or else hit a check.
277 int int_val() const;
278 int64_t int64_val() const;
279 float float_val() const;
280 const char* static_string_val() const;
281 const std::string& string_val() const;
282 bool bool_val() const;
283 const Id& id_val() const;
284
285 // Returns the string representation of |name|.
286 const char* display_name() const;
287
288 // Converts the native value to a string representation of the value.
289 std::string ToString() const;
290
291 Type type() const { return type_; }
292
293 // TODO(tommi): Move |name| and |display_name| out of the Value struct.
294 const StatsValueName name;
295
296 private:
297 const Type type_;
298 // TODO(tommi): Use C++ 11 union and make value_ const.
299 union InternalType {
300 int int_;
301 int64_t int64_;
302 float float_;
303 bool bool_;
304 std::string* string_;
305 const char* static_string_;
306 Id* id_;
307 } value_;
308
309 private:
310 RTC_DISALLOW_COPY_AND_ASSIGN(Value);
311 };
312
313 // TODO(tommi): Consider using a similar approach to how we store Ids using
314 // scoped_refptr for values.
315 typedef rtc::linked_ptr<Value> ValuePtr;
316 typedef std::map<StatsValueName, ValuePtr> Values;
317
318 // Ownership of |id| is passed to |this|.
319 explicit StatsReport(const Id& id);
320
321 // Factory functions for various types of stats IDs.
322 static Id NewBandwidthEstimationId();
323 static Id NewTypedId(StatsType type, const std::string& id);
324 static Id NewTypedIntId(StatsType type, int id);
325 static Id NewIdWithDirection(
326 StatsType type, const std::string& id, Direction direction);
327 static Id NewCandidateId(bool local, const std::string& id);
328 static Id NewComponentId(
329 const std::string& content_name, int component);
330 static Id NewCandidatePairId(
331 const std::string& content_name, int component, int index);
332
333 const Id& id() const { return id_; }
334 StatsType type() const { return id_->type(); }
335 double timestamp() const { return timestamp_; }
336 void set_timestamp(double t) { timestamp_ = t; }
337 bool empty() const { return values_.empty(); }
338 const Values& values() const { return values_; }
339
340 const char* TypeToString() const;
341
342 void AddString(StatsValueName name, const std::string& value);
343 void AddString(StatsValueName name, const char* value);
344 void AddInt64(StatsValueName name, int64_t value);
345 void AddInt(StatsValueName name, int value);
346 void AddFloat(StatsValueName name, float value);
347 void AddBoolean(StatsValueName name, bool value);
348 void AddId(StatsValueName name, const Id& value);
349
350 const Value* FindValue(StatsValueName name) const;
351
352 private:
353 // The unique identifier for this object.
354 // This is used as a key for this report in ordered containers,
355 // so it must never be changed.
356 const Id id_;
357 double timestamp_; // Time since 1970-01-01T00:00:00Z in milliseconds.
358 Values values_;
359
360 RTC_DISALLOW_COPY_AND_ASSIGN(StatsReport);
361};
362
363// Typedef for an array of const StatsReport pointers.
364// Ownership of the pointers held by this implementation is assumed to lie
365// elsewhere and lifetime guarantees are made by the implementation that uses
366// this type. In the StatsCollector, object ownership lies with the
367// StatsCollection class.
368typedef std::vector<const StatsReport*> StatsReports;
369
370// A map from the report id to the report.
371// This class wraps an STL container and provides a limited set of
372// functionality in order to keep things simple.
373class StatsCollection {
374 public:
375 StatsCollection();
376 ~StatsCollection();
377
378 typedef std::list<StatsReport*> Container;
379 typedef Container::iterator iterator;
380 typedef Container::const_iterator const_iterator;
381
382 const_iterator begin() const;
383 const_iterator end() const;
384 size_t size() const;
385
386 // Creates a new report object with |id| that does not already
387 // exist in the list of reports.
388 StatsReport* InsertNew(const StatsReport::Id& id);
389 StatsReport* FindOrAddNew(const StatsReport::Id& id);
390 StatsReport* ReplaceOrAddNew(const StatsReport::Id& id);
391
392 // Looks for a report with the given |id|. If one is not found, NULL
393 // will be returned.
394 StatsReport* Find(const StatsReport::Id& id);
395
396 private:
397 Container list_;
398 rtc::ThreadChecker thread_checker_;
399};
400
401} // namespace webrtc
402
403#endif // WEBRTC_API_STATSTYPES_H_