Henrik Kjellander | bd0ae45 | 2016-02-10 09:53:12 | [diff] [blame] | 1 | /* |
kjellander | 95ed4e6 | 2016-02-10 15:54:43 | [diff] [blame] | 2 | * Copyright 2012 The WebRTC project authors. All Rights Reserved. |
Henrik Kjellander | bd0ae45 | 2016-02-10 09:53:12 | [diff] [blame] | 3 | * |
kjellander | 95ed4e6 | 2016-02-10 15:54:43 | [diff] [blame] | 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. |
Henrik Kjellander | bd0ae45 | 2016-02-10 09:53:12 | [diff] [blame] | 9 | */ |
| 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" |
kwiberg | 5fb5bd2 | 2016-04-26 15:14:39 | [diff] [blame] | 24 | #include "webrtc/base/constructormagic.h" |
Henrik Kjellander | bd0ae45 | 2016-02-10 09:53:12 | [diff] [blame] | 25 | #include "webrtc/base/linked_ptr.h" |
| 26 | #include "webrtc/base/refcount.h" |
Henrik Kjellander | bd0ae45 | 2016-02-10 09:53:12 | [diff] [blame] | 27 | #include "webrtc/base/scoped_ref_ptr.h" |
| 28 | #include "webrtc/base/stringencode.h" |
| 29 | #include "webrtc/base/thread_checker.h" |
| 30 | |
| 31 | namespace webrtc { |
| 32 | |
| 33 | class 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, |
Minyue | f20e12f | 2016-04-07 14:48:15 | [diff] [blame] | 102 | kStatsValueNameAecDivergentFilterFraction, |
Henrik Kjellander | bd0ae45 | 2016-02-10 09:53:12 | [diff] [blame] | 103 | 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, |
zhihuang | 4a75e83 | 2016-06-02 00:09:15 | [diff] [blame] | 119 | kStatsValueNameSentPingRequestsTotal, |
| 120 | kStatsValueNameSentPingRequestsBeforeFirstResponse, |
| 121 | kStatsValueNameSentPingResponses, |
| 122 | kStatsValueNameRecvPingRequests, |
| 123 | kStatsValueNameRecvPingResponses, |
Henrik Kjellander | bd0ae45 | 2016-02-10 09:53:12 | [diff] [blame] | 124 | |
| 125 | // Internal StatsValue names. |
| 126 | kStatsValueNameAccelerateRate, |
| 127 | kStatsValueNameActualEncBitrate, |
| 128 | kStatsValueNameAdaptationChanges, |
| 129 | kStatsValueNameAvailableReceiveBandwidth, |
| 130 | kStatsValueNameAvailableSendBandwidth, |
| 131 | kStatsValueNameAvgEncodeMs, |
| 132 | kStatsValueNameBandwidthLimitedResolution, |
| 133 | kStatsValueNameBucketDelay, |
| 134 | kStatsValueNameCaptureStartNtpTimeMs, |
| 135 | kStatsValueNameCandidateIPAddress, |
| 136 | kStatsValueNameCandidateNetworkType, |
| 137 | kStatsValueNameCandidatePortNumber, |
| 138 | kStatsValueNameCandidatePriority, |
| 139 | kStatsValueNameCandidateTransportType, |
| 140 | kStatsValueNameCandidateType, |
| 141 | kStatsValueNameChannelId, |
| 142 | kStatsValueNameCodecName, |
| 143 | kStatsValueNameComponent, |
| 144 | kStatsValueNameContentName, |
| 145 | kStatsValueNameCpuLimitedResolution, |
| 146 | kStatsValueNameCurrentDelayMs, |
| 147 | kStatsValueNameDecodeMs, |
| 148 | kStatsValueNameDecodingCNG, |
| 149 | kStatsValueNameDecodingCTN, |
| 150 | kStatsValueNameDecodingCTSG, |
| 151 | kStatsValueNameDecodingNormal, |
| 152 | kStatsValueNameDecodingPLC, |
| 153 | kStatsValueNameDecodingPLCCNG, |
| 154 | kStatsValueNameDer, |
| 155 | kStatsValueNameDtlsCipher, |
| 156 | kStatsValueNameEchoCancellationQualityMin, |
| 157 | kStatsValueNameEchoDelayMedian, |
| 158 | kStatsValueNameEchoDelayStdDev, |
| 159 | kStatsValueNameEchoReturnLoss, |
| 160 | kStatsValueNameEchoReturnLossEnhancement, |
| 161 | kStatsValueNameEncodeUsagePercent, |
| 162 | kStatsValueNameExpandRate, |
| 163 | kStatsValueNameFingerprint, |
| 164 | kStatsValueNameFingerprintAlgorithm, |
| 165 | kStatsValueNameFirsReceived, |
| 166 | kStatsValueNameFirsSent, |
| 167 | kStatsValueNameFrameHeightInput, |
| 168 | kStatsValueNameFrameHeightReceived, |
| 169 | kStatsValueNameFrameHeightSent, |
| 170 | kStatsValueNameFrameRateDecoded, |
| 171 | kStatsValueNameFrameRateInput, |
| 172 | kStatsValueNameFrameRateOutput, |
| 173 | kStatsValueNameFrameRateReceived, |
| 174 | kStatsValueNameFrameRateSent, |
| 175 | kStatsValueNameFrameWidthInput, |
| 176 | kStatsValueNameFrameWidthReceived, |
| 177 | kStatsValueNameFrameWidthSent, |
| 178 | kStatsValueNameInitiator, |
| 179 | kStatsValueNameIssuerId, |
| 180 | kStatsValueNameJitterBufferMs, |
| 181 | kStatsValueNameJitterReceived, |
| 182 | kStatsValueNameLabel, |
| 183 | kStatsValueNameLocalAddress, |
| 184 | kStatsValueNameLocalCandidateId, |
| 185 | kStatsValueNameLocalCandidateType, |
| 186 | kStatsValueNameLocalCertificateId, |
| 187 | kStatsValueNameMaxDecodeMs, |
| 188 | kStatsValueNameMinPlayoutDelayMs, |
| 189 | kStatsValueNameNacksReceived, |
| 190 | kStatsValueNameNacksSent, |
| 191 | kStatsValueNamePlisReceived, |
| 192 | kStatsValueNamePlisSent, |
| 193 | kStatsValueNamePreemptiveExpandRate, |
| 194 | kStatsValueNamePreferredJitterBufferMs, |
| 195 | kStatsValueNameRemoteAddress, |
| 196 | kStatsValueNameRemoteCandidateId, |
| 197 | kStatsValueNameRemoteCandidateType, |
| 198 | kStatsValueNameRemoteCertificateId, |
| 199 | kStatsValueNameRenderDelayMs, |
| 200 | kStatsValueNameRetransmitBitrate, |
| 201 | kStatsValueNameRtt, |
| 202 | kStatsValueNameSecondaryDecodedRate, |
| 203 | kStatsValueNameSendPacketsDiscarded, |
| 204 | kStatsValueNameSpeechExpandRate, |
| 205 | kStatsValueNameSrtpCipher, |
| 206 | kStatsValueNameTargetDelayMs, |
| 207 | kStatsValueNameTargetEncBitrate, |
| 208 | kStatsValueNameTrackId, |
| 209 | kStatsValueNameTransmitBitrate, |
| 210 | kStatsValueNameTransportType, |
| 211 | kStatsValueNameTypingNoiseState, |
| 212 | kStatsValueNameViewLimitedResolution, |
| 213 | kStatsValueNameWritable, |
| 214 | }; |
| 215 | |
| 216 | class IdBase : public rtc::RefCountInterface { |
| 217 | public: |
| 218 | ~IdBase() override; |
| 219 | StatsType type() const; |
| 220 | |
| 221 | // Users of IdBase will be using the Id typedef, which is compatible with |
| 222 | // this Equals() function. It simply calls the protected (and overridden) |
| 223 | // Equals() method. |
| 224 | bool Equals(const rtc::scoped_refptr<IdBase>& other) const { |
| 225 | return Equals(*other.get()); |
| 226 | } |
| 227 | |
| 228 | virtual std::string ToString() const = 0; |
| 229 | |
| 230 | protected: |
| 231 | // Protected since users of the IdBase type will be using the Id typedef. |
| 232 | virtual bool Equals(const IdBase& other) const; |
| 233 | |
| 234 | IdBase(StatsType type); // Only meant for derived classes. |
| 235 | const StatsType type_; |
| 236 | |
| 237 | static const char kSeparator = '_'; |
| 238 | }; |
| 239 | |
| 240 | typedef rtc::scoped_refptr<IdBase> Id; |
| 241 | |
| 242 | struct Value { |
| 243 | enum Type { |
| 244 | kInt, // int. |
| 245 | kInt64, // int64_t. |
| 246 | kFloat, // float. |
| 247 | kString, // std::string |
| 248 | kStaticString, // const char*. |
| 249 | kBool, // bool. |
| 250 | kId, // Id. |
| 251 | }; |
| 252 | |
| 253 | Value(StatsValueName name, int64_t value, Type int_type); |
| 254 | Value(StatsValueName name, float f); |
| 255 | Value(StatsValueName name, const std::string& value); |
| 256 | Value(StatsValueName name, const char* value); |
| 257 | Value(StatsValueName name, bool b); |
| 258 | Value(StatsValueName name, const Id& value); |
| 259 | |
| 260 | ~Value(); |
| 261 | |
| 262 | // TODO(tommi): This compares name as well as value... |
| 263 | // I think we should only need to compare the value part and |
| 264 | // move the name part into a hash map. |
| 265 | bool Equals(const Value& other) const; |
| 266 | |
| 267 | // Comparison operators. Return true iff the current instance is of the |
| 268 | // correct type and holds the same value. No conversion is performed so |
| 269 | // a string value of "123" is not equal to an int value of 123 and an int |
| 270 | // value of 123 is not equal to a float value of 123.0f. |
| 271 | // One exception to this is that types kInt and kInt64 can be compared and |
| 272 | // kString and kStaticString too. |
| 273 | bool operator==(const std::string& value) const; |
| 274 | bool operator==(const char* value) const; |
| 275 | bool operator==(int64_t value) const; |
| 276 | bool operator==(bool value) const; |
| 277 | bool operator==(float value) const; |
| 278 | bool operator==(const Id& value) const; |
| 279 | |
| 280 | // Getters that allow getting the native value directly. |
| 281 | // The caller must know the type beforehand or else hit a check. |
| 282 | int int_val() const; |
| 283 | int64_t int64_val() const; |
| 284 | float float_val() const; |
| 285 | const char* static_string_val() const; |
| 286 | const std::string& string_val() const; |
| 287 | bool bool_val() const; |
| 288 | const Id& id_val() const; |
| 289 | |
| 290 | // Returns the string representation of |name|. |
| 291 | const char* display_name() const; |
| 292 | |
| 293 | // Converts the native value to a string representation of the value. |
| 294 | std::string ToString() const; |
| 295 | |
| 296 | Type type() const { return type_; } |
| 297 | |
| 298 | // TODO(tommi): Move |name| and |display_name| out of the Value struct. |
| 299 | const StatsValueName name; |
| 300 | |
| 301 | private: |
| 302 | const Type type_; |
| 303 | // TODO(tommi): Use C++ 11 union and make value_ const. |
| 304 | union InternalType { |
| 305 | int int_; |
| 306 | int64_t int64_; |
| 307 | float float_; |
| 308 | bool bool_; |
| 309 | std::string* string_; |
| 310 | const char* static_string_; |
| 311 | Id* id_; |
| 312 | } value_; |
| 313 | |
| 314 | private: |
| 315 | RTC_DISALLOW_COPY_AND_ASSIGN(Value); |
| 316 | }; |
| 317 | |
| 318 | // TODO(tommi): Consider using a similar approach to how we store Ids using |
| 319 | // scoped_refptr for values. |
| 320 | typedef rtc::linked_ptr<Value> ValuePtr; |
| 321 | typedef std::map<StatsValueName, ValuePtr> Values; |
| 322 | |
| 323 | // Ownership of |id| is passed to |this|. |
| 324 | explicit StatsReport(const Id& id); |
| 325 | |
| 326 | // Factory functions for various types of stats IDs. |
| 327 | static Id NewBandwidthEstimationId(); |
| 328 | static Id NewTypedId(StatsType type, const std::string& id); |
| 329 | static Id NewTypedIntId(StatsType type, int id); |
| 330 | static Id NewIdWithDirection( |
| 331 | StatsType type, const std::string& id, Direction direction); |
| 332 | static Id NewCandidateId(bool local, const std::string& id); |
| 333 | static Id NewComponentId( |
| 334 | const std::string& content_name, int component); |
| 335 | static Id NewCandidatePairId( |
| 336 | const std::string& content_name, int component, int index); |
| 337 | |
| 338 | const Id& id() const { return id_; } |
| 339 | StatsType type() const { return id_->type(); } |
| 340 | double timestamp() const { return timestamp_; } |
| 341 | void set_timestamp(double t) { timestamp_ = t; } |
| 342 | bool empty() const { return values_.empty(); } |
| 343 | const Values& values() const { return values_; } |
| 344 | |
| 345 | const char* TypeToString() const; |
| 346 | |
| 347 | void AddString(StatsValueName name, const std::string& value); |
| 348 | void AddString(StatsValueName name, const char* value); |
| 349 | void AddInt64(StatsValueName name, int64_t value); |
| 350 | void AddInt(StatsValueName name, int value); |
| 351 | void AddFloat(StatsValueName name, float value); |
| 352 | void AddBoolean(StatsValueName name, bool value); |
| 353 | void AddId(StatsValueName name, const Id& value); |
| 354 | |
| 355 | const Value* FindValue(StatsValueName name) const; |
| 356 | |
| 357 | private: |
| 358 | // The unique identifier for this object. |
| 359 | // This is used as a key for this report in ordered containers, |
| 360 | // so it must never be changed. |
| 361 | const Id id_; |
| 362 | double timestamp_; // Time since 1970-01-01T00:00:00Z in milliseconds. |
| 363 | Values values_; |
| 364 | |
| 365 | RTC_DISALLOW_COPY_AND_ASSIGN(StatsReport); |
| 366 | }; |
| 367 | |
| 368 | // Typedef for an array of const StatsReport pointers. |
| 369 | // Ownership of the pointers held by this implementation is assumed to lie |
| 370 | // elsewhere and lifetime guarantees are made by the implementation that uses |
| 371 | // this type. In the StatsCollector, object ownership lies with the |
| 372 | // StatsCollection class. |
| 373 | typedef std::vector<const StatsReport*> StatsReports; |
| 374 | |
| 375 | // A map from the report id to the report. |
| 376 | // This class wraps an STL container and provides a limited set of |
| 377 | // functionality in order to keep things simple. |
| 378 | class StatsCollection { |
| 379 | public: |
| 380 | StatsCollection(); |
| 381 | ~StatsCollection(); |
| 382 | |
| 383 | typedef std::list<StatsReport*> Container; |
| 384 | typedef Container::iterator iterator; |
| 385 | typedef Container::const_iterator const_iterator; |
| 386 | |
| 387 | const_iterator begin() const; |
| 388 | const_iterator end() const; |
| 389 | size_t size() const; |
| 390 | |
| 391 | // Creates a new report object with |id| that does not already |
| 392 | // exist in the list of reports. |
| 393 | StatsReport* InsertNew(const StatsReport::Id& id); |
| 394 | StatsReport* FindOrAddNew(const StatsReport::Id& id); |
| 395 | StatsReport* ReplaceOrAddNew(const StatsReport::Id& id); |
| 396 | |
| 397 | // Looks for a report with the given |id|. If one is not found, NULL |
| 398 | // will be returned. |
| 399 | StatsReport* Find(const StatsReport::Id& id); |
| 400 | |
| 401 | private: |
| 402 | Container list_; |
| 403 | rtc::ThreadChecker thread_checker_; |
| 404 | }; |
| 405 | |
| 406 | } // namespace webrtc |
| 407 | |
| 408 | #endif // WEBRTC_API_STATSTYPES_H_ |