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 | |
| 14 | #include <map> |
| 15 | #include <memory> |
| 16 | #include <string> |
| 17 | #include <vector> |
| 18 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 19 | #include "api/stats/rtcstats.h" |
| 20 | #include "rtc_base/refcount.h" |
Niels Möller | 84255bb | 2017-10-06 13:43:23 +0200 | [diff] [blame] | 21 | #include "rtc_base/refcountedobject.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 22 | #include "rtc_base/scoped_ref_ptr.h" |
hbos | 74e1a4f | 2016-09-15 23:33:01 -0700 | [diff] [blame] | 23 | |
| 24 | namespace webrtc { |
| 25 | |
| 26 | // A collection of stats. |
| 27 | // This is accessible as a map from |RTCStats::id| to |RTCStats|. |
| 28 | class RTCStatsReport : public rtc::RefCountInterface { |
| 29 | public: |
| 30 | typedef std::map<std::string, std::unique_ptr<const RTCStats>> StatsMap; |
| 31 | |
| 32 | class ConstIterator { |
| 33 | public: |
| 34 | ConstIterator(const ConstIterator&& other); |
| 35 | ~ConstIterator(); |
| 36 | |
| 37 | ConstIterator& operator++(); |
| 38 | ConstIterator& operator++(int); |
| 39 | const RTCStats& operator*() const; |
hbos | 6ded190 | 2016-11-01 01:50:46 -0700 | [diff] [blame] | 40 | const RTCStats* operator->() const; |
hbos | 74e1a4f | 2016-09-15 23:33:01 -0700 | [diff] [blame] | 41 | bool operator==(const ConstIterator& other) const; |
| 42 | bool operator!=(const ConstIterator& other) const; |
| 43 | |
| 44 | private: |
| 45 | friend class RTCStatsReport; |
| 46 | ConstIterator(const rtc::scoped_refptr<const RTCStatsReport>& report, |
| 47 | StatsMap::const_iterator it); |
| 48 | |
| 49 | // Reference report to make sure it is kept alive. |
| 50 | rtc::scoped_refptr<const RTCStatsReport> report_; |
| 51 | StatsMap::const_iterator it_; |
| 52 | }; |
| 53 | |
hbos | 6ded190 | 2016-11-01 01:50:46 -0700 | [diff] [blame] | 54 | // TODO(hbos): Remove "= 0" once Chromium unittest has been updated to call |
| 55 | // with a parameter. crbug.com/627816 |
nisse | deb95f3 | 2016-11-28 01:54:54 -0800 | [diff] [blame] | 56 | static rtc::scoped_refptr<RTCStatsReport> Create(int64_t timestamp_us = 0); |
hbos | 74e1a4f | 2016-09-15 23:33:01 -0700 | [diff] [blame] | 57 | |
nisse | deb95f3 | 2016-11-28 01:54:54 -0800 | [diff] [blame] | 58 | explicit RTCStatsReport(int64_t timestamp_us); |
hbos | 74e1a4f | 2016-09-15 23:33:01 -0700 | [diff] [blame] | 59 | RTCStatsReport(const RTCStatsReport& other) = delete; |
Henrik Boström | 5b3541f | 2018-03-19 13:52:56 +0100 | [diff] [blame] | 60 | rtc::scoped_refptr<RTCStatsReport> Copy() const; |
hbos | 74e1a4f | 2016-09-15 23:33:01 -0700 | [diff] [blame] | 61 | |
nisse | deb95f3 | 2016-11-28 01:54:54 -0800 | [diff] [blame] | 62 | int64_t timestamp_us() const { return timestamp_us_; } |
hbos | 02d2a92 | 2016-12-21 01:29:05 -0800 | [diff] [blame] | 63 | void AddStats(std::unique_ptr<const RTCStats> stats); |
hbos | 74e1a4f | 2016-09-15 23:33:01 -0700 | [diff] [blame] | 64 | const RTCStats* Get(const std::string& id) const; |
| 65 | size_t size() const { return stats_.size(); } |
| 66 | |
Henrik Boström | b619936 | 2018-03-12 10:27:55 +0100 | [diff] [blame] | 67 | // Removes the stats object from the report, returning ownership of it or null |
| 68 | // if there is no object with |id|. |
| 69 | std::unique_ptr<const RTCStats> Take(const std::string& id); |
hbos | 74e1a4f | 2016-09-15 23:33:01 -0700 | [diff] [blame] | 70 | // Takes ownership of all the stats in |victim|, leaving it empty. |
| 71 | void TakeMembersFrom(rtc::scoped_refptr<RTCStatsReport> victim); |
| 72 | |
| 73 | // Stats iterators. Stats are ordered lexicographically on |RTCStats::id|. |
| 74 | ConstIterator begin() const; |
| 75 | ConstIterator end() const; |
| 76 | |
| 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 | |
ehmaldonado | 35a872c | 2017-07-28 07:29:12 -0700 | [diff] [blame] | 89 | // Creates a JSON readable string representation of the report, |
| 90 | // listing all of its stats objects. |
| 91 | std::string ToJson() const; |
hbos | 6ded190 | 2016-11-01 01:50:46 -0700 | [diff] [blame] | 92 | |
hbos | 74e1a4f | 2016-09-15 23:33:01 -0700 | [diff] [blame] | 93 | friend class rtc::RefCountedObject<RTCStatsReport>; |
| 94 | |
| 95 | private: |
| 96 | ~RTCStatsReport() override; |
| 97 | |
nisse | deb95f3 | 2016-11-28 01:54:54 -0800 | [diff] [blame] | 98 | int64_t timestamp_us_; |
hbos | 74e1a4f | 2016-09-15 23:33:01 -0700 | [diff] [blame] | 99 | StatsMap stats_; |
| 100 | }; |
| 101 | |
| 102 | } // namespace webrtc |
| 103 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 104 | #endif // API_STATS_RTCSTATSREPORT_H_ |