blob: dc159376909c09485e962a5d4bef3c333cc67d62 [file] [log] [blame]
hbos74e1a4f2016-09-15 23:33:01 -07001/*
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 Anton10542f22019-01-11 09:11:00 -080011#ifndef API_STATS_RTC_STATS_REPORT_H_
12#define API_STATS_RTC_STATS_REPORT_H_
hbos74e1a4f2016-09-15 23:33:01 -070013
Yves Gerey3e707812018-11-28 16:47:49 +010014#include <stddef.h>
15#include <stdint.h>
Jonas Olssona4d87372019-07-05 19:08:33 +020016
hbos74e1a4f2016-09-15 23:33:01 -070017#include <map>
18#include <memory>
19#include <string>
20#include <vector>
21
Mirko Bonadeid9708072019-01-25 20:26:48 +010022#include "api/scoped_refptr.h"
Steve Anton10542f22019-01-11 09:11:00 -080023#include "api/stats/rtc_stats.h"
24#include "rtc_base/ref_count.h"
25#include "rtc_base/ref_counted_object.h"
Mirko Bonadei3b56ee72018-10-15 17:15:12 +020026#include "rtc_base/system/rtc_export.h"
hbos74e1a4f2016-09-15 23:33:01 -070027
28namespace webrtc {
29
30// A collection of stats.
31// This is accessible as a map from |RTCStats::id| to |RTCStats|.
Mirko Bonadei3b56ee72018-10-15 17:15:12 +020032class RTC_EXPORT RTCStatsReport : public rtc::RefCountInterface {
hbos74e1a4f2016-09-15 23:33:01 -070033 public:
34 typedef std::map<std::string, std::unique_ptr<const RTCStats>> StatsMap;
35
Mirko Bonadei54875d02019-11-06 20:16:12 +010036 class RTC_EXPORT ConstIterator {
hbos74e1a4f2016-09-15 23:33:01 -070037 public:
Mirko Bonadei6c70e162019-02-01 13:17:43 +010038 ConstIterator(ConstIterator&& other);
hbos74e1a4f2016-09-15 23:33:01 -070039 ~ConstIterator();
40
41 ConstIterator& operator++();
42 ConstIterator& operator++(int);
43 const RTCStats& operator*() const;
hbos6ded1902016-11-01 01:50:46 -070044 const RTCStats* operator->() const;
hbos74e1a4f2016-09-15 23:33:01 -070045 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
hbos6ded1902016-11-01 01:50:46 -070058 // TODO(hbos): Remove "= 0" once Chromium unittest has been updated to call
59 // with a parameter. crbug.com/627816
nissedeb95f32016-11-28 01:54:54 -080060 static rtc::scoped_refptr<RTCStatsReport> Create(int64_t timestamp_us = 0);
hbos74e1a4f2016-09-15 23:33:01 -070061
nissedeb95f32016-11-28 01:54:54 -080062 explicit RTCStatsReport(int64_t timestamp_us);
hbos74e1a4f2016-09-15 23:33:01 -070063 RTCStatsReport(const RTCStatsReport& other) = delete;
Henrik Boström5b3541f2018-03-19 13:52:56 +010064 rtc::scoped_refptr<RTCStatsReport> Copy() const;
hbos74e1a4f2016-09-15 23:33:01 -070065
nissedeb95f32016-11-28 01:54:54 -080066 int64_t timestamp_us() const { return timestamp_us_; }
hbos02d2a922016-12-21 01:29:05 -080067 void AddStats(std::unique_ptr<const RTCStats> stats);
hbos74e1a4f2016-09-15 23:33:01 -070068 const RTCStats* Get(const std::string& id) const;
69 size_t size() const { return stats_.size(); }
70
Steve Antonffa6ce42018-11-30 09:26:08 -080071 // 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ömb6199362018-03-12 10:27:55 +010084 // 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);
hbos74e1a4f2016-09-15 23:33:01 -070087 // 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 Gerey665174f2018-06-19 15:03:05 +020096 template <typename T>
hbos74e1a4f2016-09-15 23:33:01 -070097 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
ehmaldonado35a872c2017-07-28 07:29:12 -0700106 // Creates a JSON readable string representation of the report,
107 // listing all of its stats objects.
108 std::string ToJson() const;
hbos6ded1902016-11-01 01:50:46 -0700109
hbos74e1a4f2016-09-15 23:33:01 -0700110 friend class rtc::RefCountedObject<RTCStatsReport>;
111
112 private:
113 ~RTCStatsReport() override;
114
nissedeb95f32016-11-28 01:54:54 -0800115 int64_t timestamp_us_;
hbos74e1a4f2016-09-15 23:33:01 -0700116 StatsMap stats_;
117};
118
119} // namespace webrtc
120
Steve Anton10542f22019-01-11 09:11:00 -0800121#endif // API_STATS_RTC_STATS_REPORT_H_