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