hbos | 74e1a4f | 2016-09-15 23:33:01 -0700 | [diff] [blame] | 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 | |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 11 | #ifndef API_STATS_RTC_STATS_REPORT_H_ |
| 12 | #define API_STATS_RTC_STATS_REPORT_H_ |
hbos | 74e1a4f | 2016-09-15 23:33:01 -0700 | [diff] [blame] | 13 | |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 14 | #include <stddef.h> |
| 15 | #include <stdint.h> |
Jonas Olsson | a4d8737 | 2019-07-05 19:08:33 +0200 | [diff] [blame] | 16 | |
hbos | 74e1a4f | 2016-09-15 23:33:01 -0700 | [diff] [blame] | 17 | #include <map> |
| 18 | #include <memory> |
| 19 | #include <string> |
| 20 | #include <vector> |
| 21 | |
Mirko Bonadei | d970807 | 2019-01-25 20:26:48 +0100 | [diff] [blame] | 22 | #include "api/scoped_refptr.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 23 | #include "api/stats/rtc_stats.h" |
| 24 | #include "rtc_base/ref_count.h" |
| 25 | #include "rtc_base/ref_counted_object.h" |
Mirko Bonadei | 3b56ee7 | 2018-10-15 17:15:12 +0200 | [diff] [blame] | 26 | #include "rtc_base/system/rtc_export.h" |
hbos | 74e1a4f | 2016-09-15 23:33:01 -0700 | [diff] [blame] | 27 | |
| 28 | namespace webrtc { |
| 29 | |
| 30 | // A collection of stats. |
| 31 | // This is accessible as a map from |RTCStats::id| to |RTCStats|. |
Mirko Bonadei | 3b56ee7 | 2018-10-15 17:15:12 +0200 | [diff] [blame] | 32 | class RTC_EXPORT RTCStatsReport : public rtc::RefCountInterface { |
hbos | 74e1a4f | 2016-09-15 23:33:01 -0700 | [diff] [blame] | 33 | public: |
| 34 | typedef std::map<std::string, std::unique_ptr<const RTCStats>> StatsMap; |
| 35 | |
Mirko Bonadei | 54875d0 | 2019-11-06 20:16:12 +0100 | [diff] [blame] | 36 | class RTC_EXPORT ConstIterator { |
hbos | 74e1a4f | 2016-09-15 23:33:01 -0700 | [diff] [blame] | 37 | public: |
Mirko Bonadei | 6c70e16 | 2019-02-01 13:17:43 +0100 | [diff] [blame] | 38 | ConstIterator(ConstIterator&& other); |
hbos | 74e1a4f | 2016-09-15 23:33:01 -0700 | [diff] [blame] | 39 | ~ConstIterator(); |
| 40 | |
| 41 | ConstIterator& operator++(); |
| 42 | ConstIterator& operator++(int); |
| 43 | const RTCStats& operator*() const; |
hbos | 6ded190 | 2016-11-01 01:50:46 -0700 | [diff] [blame] | 44 | const RTCStats* operator->() const; |
hbos | 74e1a4f | 2016-09-15 23:33:01 -0700 | [diff] [blame] | 45 | bool operator==(const ConstIterator& other) const; |
| 46 | bool operator!=(const ConstIterator& other) const; |
| 47 | |
| 48 | private: |
| 49 | friend class RTCStatsReport; |
| 50 | ConstIterator(const rtc::scoped_refptr<const RTCStatsReport>& report, |
| 51 | StatsMap::const_iterator it); |
| 52 | |
| 53 | // Reference report to make sure it is kept alive. |
| 54 | rtc::scoped_refptr<const RTCStatsReport> report_; |
| 55 | StatsMap::const_iterator it_; |
| 56 | }; |
| 57 | |
hbos | 6ded190 | 2016-11-01 01:50:46 -0700 | [diff] [blame] | 58 | // TODO(hbos): Remove "= 0" once Chromium unittest has been updated to call |
| 59 | // with a parameter. crbug.com/627816 |
nisse | deb95f3 | 2016-11-28 01:54:54 -0800 | [diff] [blame] | 60 | static rtc::scoped_refptr<RTCStatsReport> Create(int64_t timestamp_us = 0); |
hbos | 74e1a4f | 2016-09-15 23:33:01 -0700 | [diff] [blame] | 61 | |
nisse | deb95f3 | 2016-11-28 01:54:54 -0800 | [diff] [blame] | 62 | explicit RTCStatsReport(int64_t timestamp_us); |
hbos | 74e1a4f | 2016-09-15 23:33:01 -0700 | [diff] [blame] | 63 | RTCStatsReport(const RTCStatsReport& other) = delete; |
Henrik Boström | 5b3541f | 2018-03-19 13:52:56 +0100 | [diff] [blame] | 64 | rtc::scoped_refptr<RTCStatsReport> Copy() const; |
hbos | 74e1a4f | 2016-09-15 23:33:01 -0700 | [diff] [blame] | 65 | |
nisse | deb95f3 | 2016-11-28 01:54:54 -0800 | [diff] [blame] | 66 | int64_t timestamp_us() const { return timestamp_us_; } |
hbos | 02d2a92 | 2016-12-21 01:29:05 -0800 | [diff] [blame] | 67 | void AddStats(std::unique_ptr<const RTCStats> stats); |
hbos | 74e1a4f | 2016-09-15 23:33:01 -0700 | [diff] [blame] | 68 | const RTCStats* Get(const std::string& id) const; |
| 69 | size_t size() const { return stats_.size(); } |
| 70 | |
Steve Anton | ffa6ce4 | 2018-11-30 09:26:08 -0800 | [diff] [blame] | 71 | // Gets the stat object of type |T| by ID, where |T| is any class descending |
| 72 | // from |RTCStats|. |
| 73 | // Returns null if there is no stats object for the given ID or it is the |
| 74 | // wrong type. |
| 75 | template <typename T> |
| 76 | const T* GetAs(const std::string& id) const { |
| 77 | const RTCStats* stats = Get(id); |
| 78 | if (!stats || stats->type() != T::kType) { |
| 79 | return nullptr; |
| 80 | } |
| 81 | return &stats->cast_to<const T>(); |
| 82 | } |
| 83 | |
Henrik Boström | b619936 | 2018-03-12 10:27:55 +0100 | [diff] [blame] | 84 | // Removes the stats object from the report, returning ownership of it or null |
| 85 | // if there is no object with |id|. |
| 86 | std::unique_ptr<const RTCStats> Take(const std::string& id); |
hbos | 74e1a4f | 2016-09-15 23:33:01 -0700 | [diff] [blame] | 87 | // Takes ownership of all the stats in |victim|, leaving it empty. |
| 88 | void TakeMembersFrom(rtc::scoped_refptr<RTCStatsReport> victim); |
| 89 | |
| 90 | // Stats iterators. Stats are ordered lexicographically on |RTCStats::id|. |
| 91 | ConstIterator begin() const; |
| 92 | ConstIterator end() const; |
| 93 | |
| 94 | // Gets the subset of stats that are of type |T|, where |T| is any class |
| 95 | // descending from |RTCStats|. |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 96 | template <typename T> |
hbos | 74e1a4f | 2016-09-15 23:33:01 -0700 | [diff] [blame] | 97 | std::vector<const T*> GetStatsOfType() const { |
| 98 | std::vector<const T*> stats_of_type; |
| 99 | for (const RTCStats& stats : *this) { |
| 100 | if (stats.type() == T::kType) |
| 101 | stats_of_type.push_back(&stats.cast_to<const T>()); |
| 102 | } |
| 103 | return stats_of_type; |
| 104 | } |
| 105 | |
ehmaldonado | 35a872c | 2017-07-28 07:29:12 -0700 | [diff] [blame] | 106 | // Creates a JSON readable string representation of the report, |
| 107 | // listing all of its stats objects. |
| 108 | std::string ToJson() const; |
hbos | 6ded190 | 2016-11-01 01:50:46 -0700 | [diff] [blame] | 109 | |
hbos | 74e1a4f | 2016-09-15 23:33:01 -0700 | [diff] [blame] | 110 | friend class rtc::RefCountedObject<RTCStatsReport>; |
| 111 | |
| 112 | private: |
| 113 | ~RTCStatsReport() override; |
| 114 | |
nisse | deb95f3 | 2016-11-28 01:54:54 -0800 | [diff] [blame] | 115 | int64_t timestamp_us_; |
hbos | 74e1a4f | 2016-09-15 23:33:01 -0700 | [diff] [blame] | 116 | StatsMap stats_; |
| 117 | }; |
| 118 | |
| 119 | } // namespace webrtc |
| 120 | |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 121 | #endif // API_STATS_RTC_STATS_REPORT_H_ |