rtc_stats_report.h (4227B)
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 API_STATS_RTC_STATS_REPORT_H_ 12 #define API_STATS_RTC_STATS_REPORT_H_ 13 14 #include <stddef.h> 15 #include <stdint.h> 16 17 #include <map> 18 #include <memory> 19 #include <string> 20 #include <utility> 21 #include <vector> 22 23 #include "api/ref_counted_base.h" 24 #include "api/scoped_refptr.h" 25 #include "api/stats/rtc_stats.h" 26 #include "api/units/timestamp.h" 27 #include "rtc_base/system/rtc_export.h" 28 29 namespace webrtc { 30 31 // A collection of stats. 32 // This is accessible as a map from `RTCStats::id` to `RTCStats`. 33 class RTC_EXPORT RTCStatsReport final 34 : public RefCountedNonVirtual<RTCStatsReport> { 35 public: 36 typedef std::map<std::string, std::unique_ptr<const RTCStats>> StatsMap; 37 38 class RTC_EXPORT ConstIterator { 39 public: 40 ConstIterator(ConstIterator&& other); 41 ~ConstIterator(); 42 43 ConstIterator& operator++(); 44 ConstIterator& operator++(int); 45 const RTCStats& operator*() const; 46 const RTCStats* operator->() const; 47 bool operator==(const ConstIterator& other) const; 48 bool operator!=(const ConstIterator& other) const; 49 50 private: 51 friend class RTCStatsReport; 52 ConstIterator(const scoped_refptr<const RTCStatsReport>& report, 53 StatsMap::const_iterator it); 54 55 // Reference report to make sure it is kept alive. 56 scoped_refptr<const RTCStatsReport> report_; 57 StatsMap::const_iterator it_; 58 }; 59 60 static scoped_refptr<RTCStatsReport> Create(Timestamp timestamp); 61 62 explicit RTCStatsReport(Timestamp timestamp); 63 64 RTCStatsReport(const RTCStatsReport& other) = delete; 65 scoped_refptr<RTCStatsReport> Copy() const; 66 67 Timestamp timestamp() const { return timestamp_; } 68 void AddStats(std::unique_ptr<const RTCStats> stats); 69 // On success, returns a non-owning pointer to `stats`. If the stats ID is not 70 // unique, `stats` is not inserted and nullptr is returned. 71 template <typename T> 72 T* TryAddStats(std::unique_ptr<T> stats) { 73 T* stats_ptr = stats.get(); 74 if (!stats_ 75 .insert(std::make_pair(std::string(stats->id()), std::move(stats))) 76 .second) { 77 return nullptr; 78 } 79 return stats_ptr; 80 } 81 const RTCStats* Get(const std::string& id) const; 82 size_t size() const { return stats_.size(); } 83 84 // Gets the stat object of type `T` by ID, where `T` is any class descending 85 // from `RTCStats`. 86 // Returns null if there is no stats object for the given ID or it is the 87 // wrong type. 88 template <typename T> 89 const T* GetAs(const std::string& id) const { 90 const RTCStats* stats = Get(id); 91 if (!stats || stats->type() != T::kType) { 92 return nullptr; 93 } 94 return &stats->cast_to<const T>(); 95 } 96 97 // Removes the stats object from the report, returning ownership of it or null 98 // if there is no object with `id`. 99 std::unique_ptr<const RTCStats> Take(const std::string& id); 100 // Takes ownership of all the stats in `other`, leaving it empty. 101 void TakeMembersFrom(scoped_refptr<RTCStatsReport> other); 102 103 // Stats iterators. Stats are ordered lexicographically on `RTCStats::id`. 104 ConstIterator begin() const; 105 ConstIterator end() const; 106 107 // Gets the subset of stats that are of type `T`, where `T` is any class 108 // descending from `RTCStats`. 109 template <typename T> 110 std::vector<const T*> GetStatsOfType() const { 111 std::vector<const T*> stats_of_type; 112 for (const RTCStats& stats : *this) { 113 if (stats.type() == T::kType) 114 stats_of_type.push_back(&stats.cast_to<const T>()); 115 } 116 return stats_of_type; 117 } 118 119 // Creates a JSON readable string representation of the report, 120 // listing all of its stats objects. 121 std::string ToJson() const; 122 123 protected: 124 friend class RefCountedNonVirtual<RTCStatsReport>; 125 ~RTCStatsReport() = default; 126 127 private: 128 Timestamp timestamp_; 129 StatsMap stats_; 130 }; 131 132 } // namespace webrtc 133 134 #endif // API_STATS_RTC_STATS_REPORT_H_