webrtc_m130/webrtc/api/rtcstatsreport.h
hbos 615d3013de RTCStats and RTCStatsReport added (webrtc/stats).
The old and new getStats are very different. This CL proposes rewriting
the new getStats from scratch with a bottom-up approach, starting with
the fundamental stats classes. This will allow cleaner and more
efficient code that is more aligned with the spec.

RTCStats and subclasses are the equivalent to RTCStats and RTCStats-
-derived dictionaries from the specs[1][2]. The dictionary members are
public member variables of type RTCStatsMember<T>, where T is one of the
supported types. All members derive from RTCStatsMemberInterface and
iteration of members is possible with RTCStats::Members().
The members are not stored in a map for performance and readability.
Type checking is supported with static class variables, kType.

Only the supported member types T are specialized and may be
instantiated, and sequences are supported with std::vector<...>. Type
checking is again supported with static class variables, kType.

RTCStatsReport is the equivalent from the spec[3], and maps RTCStats::id
to RTCStats-objects. RTCStatsReport is reference counted. It and its
contained stats may be destroyed on any thread. When the
RTCStatsCollector is added in a follow-up CL, it will return const
references to the RTCStatsReports. This means copies don't have to be
made for multiple stats observers or when jumping threads. In fact, no
copies of any stats will have to be made in surfacing stats to Blink.

[1] https://www.w3.org/TR/2016/WD-webrtc-20160531/#rtcstats-dictionary
[2] https://w3c.github.io/webrtc-stats/archives/20160526/webrtc-stats.html
[3] https://www.w3.org/TR/2016/WD-webrtc-20160531/#rtcstatsreport-object

This adds the new folder webrtc/stats/, with target rtc_stats and binary
rtc_stats_unittests. Public api headers are placed in webrtc/api/ and
.cc files are placed in webrtc/stats/.

BUG=chromium:627816

Review-Url: https://codereview.webrtc.org/2241093002
Cr-Commit-Position: refs/heads/master@{#13879}
2016-08-24 08:33:19 +00:00

87 lines
2.4 KiB
C++

/*
* Copyright 2016 The WebRTC Project Authors. All rights reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
#ifndef WEBRTC_API_RTCSTATSREPORT_H_
#define WEBRTC_API_RTCSTATSREPORT_H_
#include <map>
#include <memory>
#include <string>
#include <vector>
#include "webrtc/api/rtcstats.h"
#include "webrtc/base/refcount.h"
#include "webrtc/base/scoped_ref_ptr.h"
namespace webrtc {
// A collection of stats.
// This is accessible as a map from |RTCStats::id| to |RTCStats|.
class RTCStatsReport : public rtc::RefCountInterface {
public:
typedef std::map<std::string, std::unique_ptr<const RTCStats>> StatsMap;
class ConstIterator {
public:
ConstIterator(const ConstIterator&& other);
~ConstIterator();
ConstIterator& operator++();
ConstIterator& operator++(int);
const RTCStats& operator*() const;
bool operator==(const ConstIterator& other) const;
bool operator!=(const ConstIterator& other) const;
private:
friend class RTCStatsReport;
ConstIterator(const rtc::scoped_refptr<const RTCStatsReport>& report,
StatsMap::const_iterator it);
// Reference report to make sure it is kept alive.
rtc::scoped_refptr<const RTCStatsReport> report_;
StatsMap::const_iterator it_;
};
static rtc::scoped_refptr<RTCStatsReport> Create();
RTCStatsReport();
RTCStatsReport(const RTCStatsReport& other) = delete;
bool AddStats(std::unique_ptr<const RTCStats> stats);
const RTCStats* operator[](const std::string& id) const;
size_t size() const { return stats_.size(); }
ConstIterator begin() const;
ConstIterator end() const;
// Gets the subset of stats that are of type |T|, where |T| is any class
// descending from |RTCStats|.
template<typename T>
std::vector<const T*> GetStatsOfType() const {
std::vector<const T*> stats_of_type;
for (const RTCStats& stats : *this) {
if (stats.type() == T::kType)
stats_of_type.push_back(&stats.cast_to<const T>());
}
return stats_of_type;
}
friend class rtc::RefCountedObject<RTCStatsReport>;
private:
~RTCStatsReport() override;
StatsMap stats_;
};
} // namespace webrtc
#endif // WEBRTC_API_RTCSTATSREPORT_H_