blob: a56d30d3c3f1620e67e4960da72c33dff531adf1 [file] [log] [blame]
hbos615d3012016-08-24 08:33:131/*
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 17:11:0011#include "api/stats/rtc_stats_report.h"
hbos615d3012016-08-24 08:33:1312
Yves Gerey3e707812018-11-28 15:47:4913#include <type_traits>
14#include <utility>
15
16#include "rtc_base/checks.h"
Steve Antona29b3a62018-12-27 23:44:2517#include "rtc_base/strings/string_builder.h"
hbos6ded1902016-11-01 08:50:4618
hbos615d3012016-08-24 08:33:1319namespace webrtc {
20
21RTCStatsReport::ConstIterator::ConstIterator(
22 const rtc::scoped_refptr<const RTCStatsReport>& report,
23 StatsMap::const_iterator it)
Yves Gerey665174f2018-06-19 13:03:0524 : report_(report), it_(it) {}
hbos615d3012016-08-24 08:33:1325
Mirko Bonadei6c70e162019-02-01 12:17:4326RTCStatsReport::ConstIterator::ConstIterator(ConstIterator&& other) = default;
hbos615d3012016-08-24 08:33:1327
Yves Gerey665174f2018-06-19 13:03:0528RTCStatsReport::ConstIterator::~ConstIterator() {}
hbos615d3012016-08-24 08:33:1329
30RTCStatsReport::ConstIterator& RTCStatsReport::ConstIterator::operator++() {
31 ++it_;
32 return *this;
33}
34
35RTCStatsReport::ConstIterator& RTCStatsReport::ConstIterator::operator++(int) {
36 return ++(*this);
37}
38
39const RTCStats& RTCStatsReport::ConstIterator::operator*() const {
40 return *it_->second.get();
41}
42
hbos6ded1902016-11-01 08:50:4643const RTCStats* RTCStatsReport::ConstIterator::operator->() const {
44 return it_->second.get();
45}
46
hbos615d3012016-08-24 08:33:1347bool RTCStatsReport::ConstIterator::operator==(
48 const RTCStatsReport::ConstIterator& other) const {
49 return it_ == other.it_;
50}
51
52bool RTCStatsReport::ConstIterator::operator!=(
53 const RTCStatsReport::ConstIterator& other) const {
54 return !(*this == other);
55}
56
hbos6ded1902016-11-01 08:50:4657rtc::scoped_refptr<RTCStatsReport> RTCStatsReport::Create(
nissedeb95f32016-11-28 09:54:5458 int64_t timestamp_us) {
hbos615d3012016-08-24 08:33:1359 return rtc::scoped_refptr<RTCStatsReport>(
hbos6ded1902016-11-01 08:50:4660 new rtc::RefCountedObject<RTCStatsReport>(timestamp_us));
hbos615d3012016-08-24 08:33:1361}
62
nissedeb95f32016-11-28 09:54:5463RTCStatsReport::RTCStatsReport(int64_t timestamp_us)
Yves Gerey665174f2018-06-19 13:03:0564 : timestamp_us_(timestamp_us) {}
hbos615d3012016-08-24 08:33:1365
Yves Gerey665174f2018-06-19 13:03:0566RTCStatsReport::~RTCStatsReport() {}
hbos615d3012016-08-24 08:33:1367
Henrik Boström5b3541f2018-03-19 12:52:5668rtc::scoped_refptr<RTCStatsReport> RTCStatsReport::Copy() const {
69 rtc::scoped_refptr<RTCStatsReport> copy = Create(timestamp_us_);
70 for (auto it = stats_.begin(); it != stats_.end(); ++it) {
71 copy->AddStats(it->second->copy());
72 }
73 return copy;
74}
75
hbos02d2a922016-12-21 09:29:0576void RTCStatsReport::AddStats(std::unique_ptr<const RTCStats> stats) {
Yves Gerey665174f2018-06-19 13:03:0577 auto result =
78 stats_.insert(std::make_pair(std::string(stats->id()), std::move(stats)));
79 RTC_DCHECK(result.second)
80 << "A stats object with ID " << result.first->second->id()
81 << " is already "
82 "present in this stats report.";
hbos615d3012016-08-24 08:33:1383}
84
hbos6d183ac2016-08-29 14:20:3385const RTCStats* RTCStatsReport::Get(const std::string& id) const {
hbos615d3012016-08-24 08:33:1386 StatsMap::const_iterator it = stats_.find(id);
87 if (it != stats_.cend())
88 return it->second.get();
89 return nullptr;
90}
91
Henrik Boströmb6199362018-03-12 09:27:5592std::unique_ptr<const RTCStats> RTCStatsReport::Take(const std::string& id) {
93 StatsMap::iterator it = stats_.find(id);
94 if (it == stats_.end())
95 return nullptr;
96 std::unique_ptr<const RTCStats> stats = std::move(it->second);
97 stats_.erase(it);
98 return stats;
99}
100
hbos6d183ac2016-08-29 14:20:33101void RTCStatsReport::TakeMembersFrom(
102 rtc::scoped_refptr<RTCStatsReport> victim) {
103 for (StatsMap::iterator it = victim->stats_.begin();
104 it != victim->stats_.end(); ++it) {
105 AddStats(std::unique_ptr<const RTCStats>(it->second.release()));
106 }
107 victim->stats_.clear();
108}
109
hbos615d3012016-08-24 08:33:13110RTCStatsReport::ConstIterator RTCStatsReport::begin() const {
111 return ConstIterator(rtc::scoped_refptr<const RTCStatsReport>(this),
112 stats_.cbegin());
113}
114
115RTCStatsReport::ConstIterator RTCStatsReport::end() const {
116 return ConstIterator(rtc::scoped_refptr<const RTCStatsReport>(this),
117 stats_.cend());
118}
119
ehmaldonado35a872c2017-07-28 14:29:12120std::string RTCStatsReport::ToJson() const {
Steve Antona29b3a62018-12-27 23:44:25121 if (begin() == end()) {
122 return "";
hbos6ded1902016-11-01 08:50:46123 }
Steve Antona29b3a62018-12-27 23:44:25124 rtc::StringBuilder sb;
125 sb << "[";
126 const char* separator = "";
127 for (ConstIterator it = begin(); it != end(); ++it) {
128 sb << separator << it->ToJson();
Dirk-Jan C. Binnema8905d042019-01-07 11:41:05129 separator = ",";
Steve Antona29b3a62018-12-27 23:44:25130 }
131 sb << "]";
132 return sb.Release();
hbos6ded1902016-11-01 08:50:46133}
134
hbos615d3012016-08-24 08:33:13135} // namespace webrtc