rtc_stats_report.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. #ifndef API_STATS_RTC_STATS_REPORT_H_
  11. #define API_STATS_RTC_STATS_REPORT_H_
  12. #include <stddef.h>
  13. #include <stdint.h>
  14. #include <map>
  15. #include <memory>
  16. #include <string>
  17. #include <vector>
  18. #include "api/scoped_refptr.h"
  19. #include "api/stats/rtc_stats.h"
  20. #include "rtc_base/ref_count.h"
  21. #include "rtc_base/ref_counted_object.h"
  22. #include "rtc_base/system/rtc_export.h"
  23. namespace webrtc {
  24. // A collection of stats.
  25. // This is accessible as a map from |RTCStats::id| to |RTCStats|.
  26. class RTC_EXPORT RTCStatsReport : public rtc::RefCountInterface {
  27. public:
  28. typedef std::map<std::string, std::unique_ptr<const RTCStats>> StatsMap;
  29. class RTC_EXPORT ConstIterator {
  30. public:
  31. ConstIterator(ConstIterator&& other);
  32. ~ConstIterator();
  33. ConstIterator& operator++();
  34. ConstIterator& operator++(int);
  35. const RTCStats& operator*() const;
  36. const RTCStats* operator->() const;
  37. bool operator==(const ConstIterator& other) const;
  38. bool operator!=(const ConstIterator& other) const;
  39. private:
  40. friend class RTCStatsReport;
  41. ConstIterator(const rtc::scoped_refptr<const RTCStatsReport>& report,
  42. StatsMap::const_iterator it);
  43. // Reference report to make sure it is kept alive.
  44. rtc::scoped_refptr<const RTCStatsReport> report_;
  45. StatsMap::const_iterator it_;
  46. };
  47. // TODO(hbos): Remove "= 0" once Chromium unittest has been updated to call
  48. // with a parameter. crbug.com/627816
  49. static rtc::scoped_refptr<RTCStatsReport> Create(int64_t timestamp_us = 0);
  50. explicit RTCStatsReport(int64_t timestamp_us);
  51. RTCStatsReport(const RTCStatsReport& other) = delete;
  52. rtc::scoped_refptr<RTCStatsReport> Copy() const;
  53. int64_t timestamp_us() const { return timestamp_us_; }
  54. void AddStats(std::unique_ptr<const RTCStats> stats);
  55. const RTCStats* Get(const std::string& id) const;
  56. size_t size() const { return stats_.size(); }
  57. // Gets the stat object of type |T| by ID, where |T| is any class descending
  58. // from |RTCStats|.
  59. // Returns null if there is no stats object for the given ID or it is the
  60. // wrong type.
  61. template <typename T>
  62. const T* GetAs(const std::string& id) const {
  63. const RTCStats* stats = Get(id);
  64. if (!stats || stats->type() != T::kType) {
  65. return nullptr;
  66. }
  67. return &stats->cast_to<const T>();
  68. }
  69. // Removes the stats object from the report, returning ownership of it or null
  70. // if there is no object with |id|.
  71. std::unique_ptr<const RTCStats> Take(const std::string& id);
  72. // Takes ownership of all the stats in |victim|, leaving it empty.
  73. void TakeMembersFrom(rtc::scoped_refptr<RTCStatsReport> victim);
  74. // Stats iterators. Stats are ordered lexicographically on |RTCStats::id|.
  75. ConstIterator begin() const;
  76. ConstIterator end() const;
  77. // Gets the subset of stats that are of type |T|, where |T| is any class
  78. // descending from |RTCStats|.
  79. template <typename T>
  80. std::vector<const T*> GetStatsOfType() const {
  81. std::vector<const T*> stats_of_type;
  82. for (const RTCStats& stats : *this) {
  83. if (stats.type() == T::kType)
  84. stats_of_type.push_back(&stats.cast_to<const T>());
  85. }
  86. return stats_of_type;
  87. }
  88. // Creates a JSON readable string representation of the report,
  89. // listing all of its stats objects.
  90. std::string ToJson() const;
  91. friend class rtc::RefCountedObject<RTCStatsReport>;
  92. private:
  93. ~RTCStatsReport() override;
  94. int64_t timestamp_us_;
  95. StatsMap stats_;
  96. };
  97. } // namespace webrtc
  98. #endif // API_STATS_RTC_STATS_REPORT_H_