blob: bdafd5699765ba350292c733d73faedb7db341c1 [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
26RTCStatsReport::ConstIterator::ConstIterator(const ConstIterator&& other)
Yves Gerey665174f2018-06-19 15:03:05 +020027 : report_(std::move(other.report_)), it_(std::move(other.it_)) {}
hbos615d3012016-08-24 01:33:13 -070028
Yves Gerey665174f2018-06-19 15:03:05 +020029RTCStatsReport::ConstIterator::~ConstIterator() {}
hbos615d3012016-08-24 01:33:13 -070030
31RTCStatsReport::ConstIterator& RTCStatsReport::ConstIterator::operator++() {
32 ++it_;
33 return *this;
34}
35
36RTCStatsReport::ConstIterator& RTCStatsReport::ConstIterator::operator++(int) {
37 return ++(*this);
38}
39
40const RTCStats& RTCStatsReport::ConstIterator::operator*() const {
41 return *it_->second.get();
42}
43
hbos6ded1902016-11-01 01:50:46 -070044const RTCStats* RTCStatsReport::ConstIterator::operator->() const {
45 return it_->second.get();
46}
47
hbos615d3012016-08-24 01:33:13 -070048bool RTCStatsReport::ConstIterator::operator==(
49 const RTCStatsReport::ConstIterator& other) const {
50 return it_ == other.it_;
51}
52
53bool RTCStatsReport::ConstIterator::operator!=(
54 const RTCStatsReport::ConstIterator& other) const {
55 return !(*this == other);
56}
57
hbos6ded1902016-11-01 01:50:46 -070058rtc::scoped_refptr<RTCStatsReport> RTCStatsReport::Create(
nissedeb95f32016-11-28 01:54:54 -080059 int64_t timestamp_us) {
hbos615d3012016-08-24 01:33:13 -070060 return rtc::scoped_refptr<RTCStatsReport>(
hbos6ded1902016-11-01 01:50:46 -070061 new rtc::RefCountedObject<RTCStatsReport>(timestamp_us));
hbos615d3012016-08-24 01:33:13 -070062}
63
nissedeb95f32016-11-28 01:54:54 -080064RTCStatsReport::RTCStatsReport(int64_t timestamp_us)
Yves Gerey665174f2018-06-19 15:03:05 +020065 : timestamp_us_(timestamp_us) {}
hbos615d3012016-08-24 01:33:13 -070066
Yves Gerey665174f2018-06-19 15:03:05 +020067RTCStatsReport::~RTCStatsReport() {}
hbos615d3012016-08-24 01:33:13 -070068
Henrik Boström5b3541f2018-03-19 13:52:56 +010069rtc::scoped_refptr<RTCStatsReport> RTCStatsReport::Copy() const {
70 rtc::scoped_refptr<RTCStatsReport> copy = Create(timestamp_us_);
71 for (auto it = stats_.begin(); it != stats_.end(); ++it) {
72 copy->AddStats(it->second->copy());
73 }
74 return copy;
75}
76
hbos02d2a922016-12-21 01:29:05 -080077void RTCStatsReport::AddStats(std::unique_ptr<const RTCStats> stats) {
Yves Gerey665174f2018-06-19 15:03:05 +020078 auto result =
79 stats_.insert(std::make_pair(std::string(stats->id()), std::move(stats)));
80 RTC_DCHECK(result.second)
81 << "A stats object with ID " << result.first->second->id()
82 << " is already "
83 "present in this stats report.";
hbos615d3012016-08-24 01:33:13 -070084}
85
hbos6d183ac2016-08-29 07:20:33 -070086const RTCStats* RTCStatsReport::Get(const std::string& id) const {
hbos615d3012016-08-24 01:33:13 -070087 StatsMap::const_iterator it = stats_.find(id);
88 if (it != stats_.cend())
89 return it->second.get();
90 return nullptr;
91}
92
Henrik Boströmb6199362018-03-12 10:27:55 +010093std::unique_ptr<const RTCStats> RTCStatsReport::Take(const std::string& id) {
94 StatsMap::iterator it = stats_.find(id);
95 if (it == stats_.end())
96 return nullptr;
97 std::unique_ptr<const RTCStats> stats = std::move(it->second);
98 stats_.erase(it);
99 return stats;
100}
101
hbos6d183ac2016-08-29 07:20:33 -0700102void RTCStatsReport::TakeMembersFrom(
103 rtc::scoped_refptr<RTCStatsReport> victim) {
104 for (StatsMap::iterator it = victim->stats_.begin();
105 it != victim->stats_.end(); ++it) {
106 AddStats(std::unique_ptr<const RTCStats>(it->second.release()));
107 }
108 victim->stats_.clear();
109}
110
hbos615d3012016-08-24 01:33:13 -0700111RTCStatsReport::ConstIterator RTCStatsReport::begin() const {
112 return ConstIterator(rtc::scoped_refptr<const RTCStatsReport>(this),
113 stats_.cbegin());
114}
115
116RTCStatsReport::ConstIterator RTCStatsReport::end() const {
117 return ConstIterator(rtc::scoped_refptr<const RTCStatsReport>(this),
118 stats_.cend());
119}
120
ehmaldonado35a872c2017-07-28 07:29:12 -0700121std::string RTCStatsReport::ToJson() const {
Steve Antona29b3a62018-12-27 15:44:25 -0800122 if (begin() == end()) {
123 return "";
hbos6ded1902016-11-01 01:50:46 -0700124 }
Steve Antona29b3a62018-12-27 15:44:25 -0800125 rtc::StringBuilder sb;
126 sb << "[";
127 const char* separator = "";
128 for (ConstIterator it = begin(); it != end(); ++it) {
129 sb << separator << it->ToJson();
Dirk-Jan C. Binnema8905d042019-01-07 11:41:05 +0000130 separator = ",";
Steve Antona29b3a62018-12-27 15:44:25 -0800131 }
132 sb << "]";
133 return sb.Release();
hbos6ded1902016-11-01 01:50:46 -0700134}
135
hbos615d3012016-08-24 01:33:13 -0700136} // namespace webrtc