blob: 8bf1cb57d9d55ae50432b256355dd21f284fb436 [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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef API_STATS_RTCSTATSREPORT_H_
12#define API_STATS_RTCSTATSREPORT_H_
hbos74e1a4f2016-09-15 23:33:01 -070013
14#include <map>
15#include <memory>
16#include <string>
17#include <vector>
18
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020019#include "api/stats/rtcstats.h"
20#include "rtc_base/refcount.h"
Niels Möller84255bb2017-10-06 13:43:23 +020021#include "rtc_base/refcountedobject.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020022#include "rtc_base/scoped_ref_ptr.h"
Mirko Bonadei3b56ee72018-10-15 17:15:12 +020023#include "rtc_base/system/rtc_export.h"
hbos74e1a4f2016-09-15 23:33:01 -070024
25namespace webrtc {
26
27// A collection of stats.
28// This is accessible as a map from |RTCStats::id| to |RTCStats|.
Mirko Bonadei3b56ee72018-10-15 17:15:12 +020029class RTC_EXPORT RTCStatsReport : public rtc::RefCountInterface {
hbos74e1a4f2016-09-15 23:33:01 -070030 public:
31 typedef std::map<std::string, std::unique_ptr<const RTCStats>> StatsMap;
32
33 class ConstIterator {
34 public:
35 ConstIterator(const ConstIterator&& other);
36 ~ConstIterator();
37
38 ConstIterator& operator++();
39 ConstIterator& operator++(int);
40 const RTCStats& operator*() const;
hbos6ded1902016-11-01 01:50:46 -070041 const RTCStats* operator->() const;
hbos74e1a4f2016-09-15 23:33:01 -070042 bool operator==(const ConstIterator& other) const;
43 bool operator!=(const ConstIterator& other) const;
44
45 private:
46 friend class RTCStatsReport;
47 ConstIterator(const rtc::scoped_refptr<const RTCStatsReport>& report,
48 StatsMap::const_iterator it);
49
50 // Reference report to make sure it is kept alive.
51 rtc::scoped_refptr<const RTCStatsReport> report_;
52 StatsMap::const_iterator it_;
53 };
54
hbos6ded1902016-11-01 01:50:46 -070055 // TODO(hbos): Remove "= 0" once Chromium unittest has been updated to call
56 // with a parameter. crbug.com/627816
nissedeb95f32016-11-28 01:54:54 -080057 static rtc::scoped_refptr<RTCStatsReport> Create(int64_t timestamp_us = 0);
hbos74e1a4f2016-09-15 23:33:01 -070058
nissedeb95f32016-11-28 01:54:54 -080059 explicit RTCStatsReport(int64_t timestamp_us);
hbos74e1a4f2016-09-15 23:33:01 -070060 RTCStatsReport(const RTCStatsReport& other) = delete;
Henrik Boström5b3541f2018-03-19 13:52:56 +010061 rtc::scoped_refptr<RTCStatsReport> Copy() const;
hbos74e1a4f2016-09-15 23:33:01 -070062
nissedeb95f32016-11-28 01:54:54 -080063 int64_t timestamp_us() const { return timestamp_us_; }
hbos02d2a922016-12-21 01:29:05 -080064 void AddStats(std::unique_ptr<const RTCStats> stats);
hbos74e1a4f2016-09-15 23:33:01 -070065 const RTCStats* Get(const std::string& id) const;
66 size_t size() const { return stats_.size(); }
67
Henrik Boströmb6199362018-03-12 10:27:55 +010068 // Removes the stats object from the report, returning ownership of it or null
69 // if there is no object with |id|.
70 std::unique_ptr<const RTCStats> Take(const std::string& id);
hbos74e1a4f2016-09-15 23:33:01 -070071 // Takes ownership of all the stats in |victim|, leaving it empty.
72 void TakeMembersFrom(rtc::scoped_refptr<RTCStatsReport> victim);
73
74 // Stats iterators. Stats are ordered lexicographically on |RTCStats::id|.
75 ConstIterator begin() const;
76 ConstIterator end() const;
77
78 // Gets the subset of stats that are of type |T|, where |T| is any class
79 // descending from |RTCStats|.
Yves Gerey665174f2018-06-19 15:03:05 +020080 template <typename T>
hbos74e1a4f2016-09-15 23:33:01 -070081 std::vector<const T*> GetStatsOfType() const {
82 std::vector<const T*> stats_of_type;
83 for (const RTCStats& stats : *this) {
84 if (stats.type() == T::kType)
85 stats_of_type.push_back(&stats.cast_to<const T>());
86 }
87 return stats_of_type;
88 }
89
ehmaldonado35a872c2017-07-28 07:29:12 -070090 // Creates a JSON readable string representation of the report,
91 // listing all of its stats objects.
92 std::string ToJson() const;
hbos6ded1902016-11-01 01:50:46 -070093
hbos74e1a4f2016-09-15 23:33:01 -070094 friend class rtc::RefCountedObject<RTCStatsReport>;
95
96 private:
97 ~RTCStatsReport() override;
98
nissedeb95f32016-11-28 01:54:54 -080099 int64_t timestamp_us_;
hbos74e1a4f2016-09-15 23:33:01 -0700100 StatsMap stats_;
101};
102
103} // namespace webrtc
104
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200105#endif // API_STATS_RTCSTATSREPORT_H_