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 | |
| 11 | #ifndef WEBRTC_API_STATS_RTCSTATSREPORT_H_ |
| 12 | #define WEBRTC_API_STATS_RTCSTATSREPORT_H_ |
| 13 | |
| 14 | #include <map> |
| 15 | #include <memory> |
| 16 | #include <string> |
| 17 | #include <vector> |
| 18 | |
| 19 | #include "webrtc/api/stats/rtcstats.h" |
Edward Lemur | c20978e | 2017-07-06 19:44:34 +0200 | [diff] [blame] | 20 | #include "webrtc/rtc_base/refcount.h" |
| 21 | #include "webrtc/rtc_base/scoped_ref_ptr.h" |
hbos | 74e1a4f | 2016-09-15 23:33:01 -0700 | [diff] [blame] | 22 | |
| 23 | namespace webrtc { |
| 24 | |
| 25 | // A collection of stats. |
| 26 | // This is accessible as a map from |RTCStats::id| to |RTCStats|. |
| 27 | class RTCStatsReport : public rtc::RefCountInterface { |
| 28 | public: |
| 29 | typedef std::map<std::string, std::unique_ptr<const RTCStats>> StatsMap; |
| 30 | |
| 31 | class ConstIterator { |
| 32 | public: |
| 33 | ConstIterator(const ConstIterator&& other); |
| 34 | ~ConstIterator(); |
| 35 | |
| 36 | ConstIterator& operator++(); |
| 37 | ConstIterator& operator++(int); |
| 38 | const RTCStats& operator*() const; |
hbos | 6ded190 | 2016-11-01 01:50:46 -0700 | [diff] [blame] | 39 | const RTCStats* operator->() const; |
hbos | 74e1a4f | 2016-09-15 23:33:01 -0700 | [diff] [blame] | 40 | bool operator==(const ConstIterator& other) const; |
| 41 | bool operator!=(const ConstIterator& other) const; |
| 42 | |
| 43 | private: |
| 44 | friend class RTCStatsReport; |
| 45 | ConstIterator(const rtc::scoped_refptr<const RTCStatsReport>& report, |
| 46 | StatsMap::const_iterator it); |
| 47 | |
| 48 | // Reference report to make sure it is kept alive. |
| 49 | rtc::scoped_refptr<const RTCStatsReport> report_; |
| 50 | StatsMap::const_iterator it_; |
| 51 | }; |
| 52 | |
hbos | 6ded190 | 2016-11-01 01:50:46 -0700 | [diff] [blame] | 53 | // TODO(hbos): Remove "= 0" once Chromium unittest has been updated to call |
| 54 | // with a parameter. crbug.com/627816 |
nisse | deb95f3 | 2016-11-28 01:54:54 -0800 | [diff] [blame] | 55 | static rtc::scoped_refptr<RTCStatsReport> Create(int64_t timestamp_us = 0); |
hbos | 74e1a4f | 2016-09-15 23:33:01 -0700 | [diff] [blame] | 56 | |
nisse | deb95f3 | 2016-11-28 01:54:54 -0800 | [diff] [blame] | 57 | explicit RTCStatsReport(int64_t timestamp_us); |
hbos | 74e1a4f | 2016-09-15 23:33:01 -0700 | [diff] [blame] | 58 | RTCStatsReport(const RTCStatsReport& other) = delete; |
| 59 | |
nisse | deb95f3 | 2016-11-28 01:54:54 -0800 | [diff] [blame] | 60 | int64_t timestamp_us() const { return timestamp_us_; } |
hbos | 02d2a92 | 2016-12-21 01:29:05 -0800 | [diff] [blame] | 61 | void AddStats(std::unique_ptr<const RTCStats> stats); |
hbos | 74e1a4f | 2016-09-15 23:33:01 -0700 | [diff] [blame] | 62 | const RTCStats* Get(const std::string& id) const; |
| 63 | size_t size() const { return stats_.size(); } |
| 64 | |
| 65 | // Takes ownership of all the stats in |victim|, leaving it empty. |
| 66 | void TakeMembersFrom(rtc::scoped_refptr<RTCStatsReport> victim); |
| 67 | |
| 68 | // Stats iterators. Stats are ordered lexicographically on |RTCStats::id|. |
| 69 | ConstIterator begin() const; |
| 70 | ConstIterator end() const; |
| 71 | |
| 72 | // Gets the subset of stats that are of type |T|, where |T| is any class |
| 73 | // descending from |RTCStats|. |
| 74 | template<typename T> |
| 75 | std::vector<const T*> GetStatsOfType() const { |
| 76 | std::vector<const T*> stats_of_type; |
| 77 | for (const RTCStats& stats : *this) { |
| 78 | if (stats.type() == T::kType) |
| 79 | stats_of_type.push_back(&stats.cast_to<const T>()); |
| 80 | } |
| 81 | return stats_of_type; |
| 82 | } |
| 83 | |
ehmaldonado | 35a872c | 2017-07-28 07:29:12 -0700 | [diff] [blame] | 84 | // Creates a JSON readable string representation of the report, |
| 85 | // listing all of its stats objects. |
| 86 | std::string ToJson() const; |
hbos | 6ded190 | 2016-11-01 01:50:46 -0700 | [diff] [blame] | 87 | |
hbos | 74e1a4f | 2016-09-15 23:33:01 -0700 | [diff] [blame] | 88 | friend class rtc::RefCountedObject<RTCStatsReport>; |
| 89 | |
| 90 | private: |
| 91 | ~RTCStatsReport() override; |
| 92 | |
nisse | deb95f3 | 2016-11-28 01:54:54 -0800 | [diff] [blame] | 93 | int64_t timestamp_us_; |
hbos | 74e1a4f | 2016-09-15 23:33:01 -0700 | [diff] [blame] | 94 | StatsMap stats_; |
| 95 | }; |
| 96 | |
| 97 | } // namespace webrtc |
| 98 | |
| 99 | #endif // WEBRTC_API_STATS_RTCSTATSREPORT_H_ |