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