blob: a56d30d3c3f1620e67e4960da72c33dff531adf1 [file] [log] [blame]
hbos615d3012016-08-24 01:33:13 -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#include "api/stats/rtc_stats_report.h"
hbos615d3012016-08-24 01:33:13 -070012
Yves Gerey3e707812018-11-28 16:47:49 +010013#include <type_traits>
14#include <utility>
15
16#include "rtc_base/checks.h"
Steve Antona29b3a62018-12-27 15:44:25 -080017#include "rtc_base/strings/string_builder.h"
hbos6ded1902016-11-01 01:50:46 -070018
hbos615d3012016-08-24 01:33:13 -070019namespace webrtc {
20
21RTCStatsReport::ConstIterator::ConstIterator(
22 const rtc::scoped_refptr<const RTCStatsReport>& report,
23 StatsMap::const_iterator it)
Yves Gerey665174f2018-06-19 15:03:05 +020024 : report_(report), it_(it) {}
hbos615d3012016-08-24 01:33:13 -070025
Mirko Bonadei6c70e162019-02-01 13:17:43 +010026RTCStatsReport::ConstIterator::ConstIterator(ConstIterator&& other) = default;
hbos615d3012016-08-24 01:33:13 -070027
Yves Gerey665174f2018-06-19 15:03:05 +020028RTCStatsReport::ConstIterator::~ConstIterator() {}
hbos615d3012016-08-24 01:33:13 -070029
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 01:50:46 -070043const RTCStats* RTCStatsReport::ConstIterator::operator->() const {
44 return it_->second.get();
45}
46
hbos615d3012016-08-24 01:33:13 -070047bool 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 01:50:46 -070057rtc::scoped_refptr<RTCStatsReport> RTCStatsReport::Create(
nissedeb95f32016-11-28 01:54:54 -080058 int64_t timestamp_us) {
hbos615d3012016-08-24 01:33:13 -070059 return rtc::scoped_refptr<RTCStatsReport>(
hbos6ded1902016-11-01 01:50:46 -070060 new rtc::RefCountedObject<RTCStatsReport>(timestamp_us));
hbos615d3012016-08-24 01:33:13 -070061}
62
nissedeb95f32016-11-28 01:54:54 -080063RTCStatsReport::RTCStatsReport(int64_t timestamp_us)
Yves Gerey665174f2018-06-19 15:03:05 +020064 : timestamp_us_(timestamp_us) {}
hbos615d3012016-08-24 01:33:13 -070065
Yves Gerey665174f2018-06-19 15:03:05 +020066RTCStatsReport::~RTCStatsReport() {}
hbos615d3012016-08-24 01:33:13 -070067
Henrik Boström5b3541f2018-03-19 13:52:56 +010068rtc::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 01:29:05 -080076void RTCStatsReport::AddStats(std::unique_ptr<const RTCStats> stats) {
Yves Gerey665174f2018-06-19 15:03:05 +020077 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 01:33:13 -070083}
84
hbos6d183ac2016-08-29 07:20:33 -070085const RTCStats* RTCStatsReport::Get(const std::string& id) const {
hbos615d3012016-08-24 01:33:13 -070086 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 10:27:55 +010092std::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 07:20:33 -0700101void 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 01:33:13 -0700110RTCStatsReport::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 07:29:12 -0700120std::string RTCStatsReport::ToJson() const {
Steve Antona29b3a62018-12-27 15:44:25 -0800121 if (begin() == end()) {
122 return "";
hbos6ded1902016-11-01 01:50:46 -0700123 }
Steve Antona29b3a62018-12-27 15:44:25 -0800124 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:05 +0000129 separator = ",";
Steve Antona29b3a62018-12-27 15:44:25 -0800130 }
131 sb << "]";
132 return sb.Release();
hbos6ded1902016-11-01 01:50:46 -0700133}
134
hbos615d3012016-08-24 01:33:13 -0700135} // namespace webrtc