• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 API_STATS_RTC_STATS_REPORT_H_
12 #define API_STATS_RTC_STATS_REPORT_H_
13 
14 #include <stddef.h>
15 #include <stdint.h>
16 
17 #include <map>
18 #include <memory>
19 #include <string>
20 #include <vector>
21 
22 #include "api/scoped_refptr.h"
23 #include "api/stats/rtc_stats.h"
24 #include "rtc_base/ref_count.h"
25 #include "rtc_base/ref_counted_object.h"
26 #include "rtc_base/system/rtc_export.h"
27 
28 namespace webrtc {
29 
30 // A collection of stats.
31 // This is accessible as a map from |RTCStats::id| to |RTCStats|.
32 class RTC_EXPORT RTCStatsReport : public rtc::RefCountInterface {
33  public:
34   typedef std::map<std::string, std::unique_ptr<const RTCStats>> StatsMap;
35 
36   class RTC_EXPORT ConstIterator {
37    public:
38     ConstIterator(ConstIterator&& other);
39     ~ConstIterator();
40 
41     ConstIterator& operator++();
42     ConstIterator& operator++(int);
43     const RTCStats& operator*() const;
44     const RTCStats* operator->() const;
45     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 
58   // TODO(hbos): Remove "= 0" once Chromium unittest has been updated to call
59   // with a parameter. crbug.com/627816
60   static rtc::scoped_refptr<RTCStatsReport> Create(int64_t timestamp_us = 0);
61 
62   explicit RTCStatsReport(int64_t timestamp_us);
63   RTCStatsReport(const RTCStatsReport& other) = delete;
64   rtc::scoped_refptr<RTCStatsReport> Copy() const;
65 
timestamp_us()66   int64_t timestamp_us() const { return timestamp_us_; }
67   void AddStats(std::unique_ptr<const RTCStats> stats);
68   const RTCStats* Get(const std::string& id) const;
size()69   size_t size() const { return stats_.size(); }
70 
71   // 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>
GetAs(const std::string & id)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 
84   // 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);
87   // 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|.
96   template <typename T>
GetStatsOfType()97   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 
106   // Creates a JSON readable string representation of the report,
107   // listing all of its stats objects.
108   std::string ToJson() const;
109 
110   friend class rtc::RefCountedObject<RTCStatsReport>;
111 
112  private:
113   ~RTCStatsReport() override;
114 
115   int64_t timestamp_us_;
116   StatsMap stats_;
117 };
118 
119 }  // namespace webrtc
120 
121 #endif  // API_STATS_RTC_STATS_REPORT_H_
122