blob: 2ced422370c79fab5ff3d95c24110b468a3eb9ee [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
Tommi86ee89f2021-04-20 16:58:01 +020022#include "api/ref_counted_base.h"
Mirko Bonadeid9708072019-01-25 20:26:48 +010023#include "api/scoped_refptr.h"
Steve Anton10542f22019-01-11 09:11:00 -080024#include "api/stats/rtc_stats.h"
Tommi86ee89f2021-04-20 16:58:01 +020025// TODO(tommi): Remove this include after fixing iwyu issue in chromium.
26// See: third_party/blink/renderer/platform/peerconnection/rtc_stats.cc
Steve Anton10542f22019-01-11 09:11:00 -080027#include "rtc_base/ref_counted_object.h"
Mirko Bonadei3b56ee72018-10-15 17:15:12 +020028#include "rtc_base/system/rtc_export.h"
hbos74e1a4f2016-09-15 23:33:01 -070029
30namespace webrtc {
31
32// A collection of stats.
Artem Titov0e61fdd2021-07-25 21:50:14 +020033// This is accessible as a map from `RTCStats::id` to `RTCStats`.
Tommi86ee89f2021-04-20 16:58:01 +020034class RTC_EXPORT RTCStatsReport final
35 : public rtc::RefCountedNonVirtual<RTCStatsReport> {
hbos74e1a4f2016-09-15 23:33:01 -070036 public:
37 typedef std::map<std::string, std::unique_ptr<const RTCStats>> StatsMap;
38
Mirko Bonadei54875d02019-11-06 20:16:12 +010039 class RTC_EXPORT ConstIterator {
hbos74e1a4f2016-09-15 23:33:01 -070040 public:
Mirko Bonadei6c70e162019-02-01 13:17:43 +010041 ConstIterator(ConstIterator&& other);
hbos74e1a4f2016-09-15 23:33:01 -070042 ~ConstIterator();
43
44 ConstIterator& operator++();
45 ConstIterator& operator++(int);
46 const RTCStats& operator*() const;
hbos6ded1902016-11-01 01:50:46 -070047 const RTCStats* operator->() const;
hbos74e1a4f2016-09-15 23:33:01 -070048 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
hbos6ded1902016-11-01 01:50:46 -070061 // TODO(hbos): Remove "= 0" once Chromium unittest has been updated to call
62 // with a parameter. crbug.com/627816
nissedeb95f32016-11-28 01:54:54 -080063 static rtc::scoped_refptr<RTCStatsReport> Create(int64_t timestamp_us = 0);
hbos74e1a4f2016-09-15 23:33:01 -070064
nissedeb95f32016-11-28 01:54:54 -080065 explicit RTCStatsReport(int64_t timestamp_us);
hbos74e1a4f2016-09-15 23:33:01 -070066 RTCStatsReport(const RTCStatsReport& other) = delete;
Henrik Boström5b3541f2018-03-19 13:52:56 +010067 rtc::scoped_refptr<RTCStatsReport> Copy() const;
hbos74e1a4f2016-09-15 23:33:01 -070068
nissedeb95f32016-11-28 01:54:54 -080069 int64_t timestamp_us() const { return timestamp_us_; }
hbos02d2a922016-12-21 01:29:05 -080070 void AddStats(std::unique_ptr<const RTCStats> stats);
hbos74e1a4f2016-09-15 23:33:01 -070071 const RTCStats* Get(const std::string& id) const;
72 size_t size() const { return stats_.size(); }
73
Artem Titov0e61fdd2021-07-25 21:50:14 +020074 // Gets the stat object of type `T` by ID, where `T` is any class descending
75 // from `RTCStats`.
Steve Antonffa6ce42018-11-30 09:26:08 -080076 // 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ömb6199362018-03-12 10:27:55 +010087 // Removes the stats object from the report, returning ownership of it or null
Artem Titov0e61fdd2021-07-25 21:50:14 +020088 // if there is no object with `id`.
Henrik Boströmb6199362018-03-12 10:27:55 +010089 std::unique_ptr<const RTCStats> Take(const std::string& id);
Artem Titov0e61fdd2021-07-25 21:50:14 +020090 // Takes ownership of all the stats in `other`, leaving it empty.
Philipp Hanckecdebea02020-10-09 13:39:33 +020091 void TakeMembersFrom(rtc::scoped_refptr<RTCStatsReport> other);
hbos74e1a4f2016-09-15 23:33:01 -070092
Artem Titovcfea2182021-08-10 01:22:31 +020093 // Stats iterators. Stats are ordered lexicographically on `RTCStats::id`.
hbos74e1a4f2016-09-15 23:33:01 -070094 ConstIterator begin() const;
95 ConstIterator end() const;
96
Artem Titov0e61fdd2021-07-25 21:50:14 +020097 // Gets the subset of stats that are of type `T`, where `T` is any class
98 // descending from `RTCStats`.
Yves Gerey665174f2018-06-19 15:03:05 +020099 template <typename T>
hbos74e1a4f2016-09-15 23:33:01 -0700100 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
ehmaldonado35a872c2017-07-28 07:29:12 -0700109 // Creates a JSON readable string representation of the report,
110 // listing all of its stats objects.
111 std::string ToJson() const;
hbos6ded1902016-11-01 01:50:46 -0700112
Tommi86ee89f2021-04-20 16:58:01 +0200113 protected:
114 friend class rtc::RefCountedNonVirtual<RTCStatsReport>;
115 ~RTCStatsReport() = default;
hbos74e1a4f2016-09-15 23:33:01 -0700116
117 private:
nissedeb95f32016-11-28 01:54:54 -0800118 int64_t timestamp_us_;
hbos74e1a4f2016-09-15 23:33:01 -0700119 StatsMap stats_;
120};
121
122} // namespace webrtc
123
Steve Anton10542f22019-01-11 09:11:00 -0800124#endif // API_STATS_RTC_STATS_REPORT_H_