blob: 0918567e5d324c5f2d35af1271eff62475a034c6 [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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "api/stats/rtcstatsreport.h"
hbos615d3012016-08-24 01:33:13 -070012
hbos6ded1902016-11-01 01:50:46 -070013#include <sstream>
14
hbos615d3012016-08-24 01:33:13 -070015namespace webrtc {
16
17RTCStatsReport::ConstIterator::ConstIterator(
18 const rtc::scoped_refptr<const RTCStatsReport>& report,
19 StatsMap::const_iterator it)
20 : report_(report),
21 it_(it) {
22}
23
24RTCStatsReport::ConstIterator::ConstIterator(const ConstIterator&& other)
25 : report_(std::move(other.report_)),
26 it_(std::move(other.it_)) {
27}
28
29RTCStatsReport::ConstIterator::~ConstIterator() {
30}
31
32RTCStatsReport::ConstIterator& RTCStatsReport::ConstIterator::operator++() {
33 ++it_;
34 return *this;
35}
36
37RTCStatsReport::ConstIterator& RTCStatsReport::ConstIterator::operator++(int) {
38 return ++(*this);
39}
40
41const RTCStats& RTCStatsReport::ConstIterator::operator*() const {
42 return *it_->second.get();
43}
44
hbos6ded1902016-11-01 01:50:46 -070045const RTCStats* RTCStatsReport::ConstIterator::operator->() const {
46 return it_->second.get();
47}
48
hbos615d3012016-08-24 01:33:13 -070049bool RTCStatsReport::ConstIterator::operator==(
50 const RTCStatsReport::ConstIterator& other) const {
51 return it_ == other.it_;
52}
53
54bool RTCStatsReport::ConstIterator::operator!=(
55 const RTCStatsReport::ConstIterator& other) const {
56 return !(*this == other);
57}
58
hbos6ded1902016-11-01 01:50:46 -070059rtc::scoped_refptr<RTCStatsReport> RTCStatsReport::Create(
nissedeb95f32016-11-28 01:54:54 -080060 int64_t timestamp_us) {
hbos615d3012016-08-24 01:33:13 -070061 return rtc::scoped_refptr<RTCStatsReport>(
hbos6ded1902016-11-01 01:50:46 -070062 new rtc::RefCountedObject<RTCStatsReport>(timestamp_us));
hbos615d3012016-08-24 01:33:13 -070063}
64
nissedeb95f32016-11-28 01:54:54 -080065RTCStatsReport::RTCStatsReport(int64_t timestamp_us)
hbos6ded1902016-11-01 01:50:46 -070066 : timestamp_us_(timestamp_us) {
hbos615d3012016-08-24 01:33:13 -070067}
68
69RTCStatsReport::~RTCStatsReport() {
70}
71
Henrik Boström5b3541f2018-03-19 13:52:56 +010072rtc::scoped_refptr<RTCStatsReport> RTCStatsReport::Copy() const {
73 rtc::scoped_refptr<RTCStatsReport> copy = Create(timestamp_us_);
74 for (auto it = stats_.begin(); it != stats_.end(); ++it) {
75 copy->AddStats(it->second->copy());
76 }
77 return copy;
78}
79
hbos02d2a922016-12-21 01:29:05 -080080void RTCStatsReport::AddStats(std::unique_ptr<const RTCStats> stats) {
81 auto result = stats_.insert(std::make_pair(std::string(stats->id()),
82 std::move(stats)));
83 RTC_DCHECK(result.second) <<
84 "A stats object with ID " << result.first->second->id() << " is already "
85 "present in this stats report.";
hbos615d3012016-08-24 01:33:13 -070086}
87
hbos6d183ac2016-08-29 07:20:33 -070088const RTCStats* RTCStatsReport::Get(const std::string& id) const {
hbos615d3012016-08-24 01:33:13 -070089 StatsMap::const_iterator it = stats_.find(id);
90 if (it != stats_.cend())
91 return it->second.get();
92 return nullptr;
93}
94
Henrik Boströmb6199362018-03-12 10:27:55 +010095std::unique_ptr<const RTCStats> RTCStatsReport::Take(const std::string& id) {
96 StatsMap::iterator it = stats_.find(id);
97 if (it == stats_.end())
98 return nullptr;
99 std::unique_ptr<const RTCStats> stats = std::move(it->second);
100 stats_.erase(it);
101 return stats;
102}
103
hbos6d183ac2016-08-29 07:20:33 -0700104void RTCStatsReport::TakeMembersFrom(
105 rtc::scoped_refptr<RTCStatsReport> victim) {
106 for (StatsMap::iterator it = victim->stats_.begin();
107 it != victim->stats_.end(); ++it) {
108 AddStats(std::unique_ptr<const RTCStats>(it->second.release()));
109 }
110 victim->stats_.clear();
111}
112
hbos615d3012016-08-24 01:33:13 -0700113RTCStatsReport::ConstIterator RTCStatsReport::begin() const {
114 return ConstIterator(rtc::scoped_refptr<const RTCStatsReport>(this),
115 stats_.cbegin());
116}
117
118RTCStatsReport::ConstIterator RTCStatsReport::end() const {
119 return ConstIterator(rtc::scoped_refptr<const RTCStatsReport>(this),
120 stats_.cend());
121}
122
ehmaldonado35a872c2017-07-28 07:29:12 -0700123std::string RTCStatsReport::ToJson() const {
hbos6ded1902016-11-01 01:50:46 -0700124 std::ostringstream oss;
125 ConstIterator it = begin();
126 if (it != end()) {
ehmaldonado35a872c2017-07-28 07:29:12 -0700127 oss << '[' << it->ToJson();
hbos6ded1902016-11-01 01:50:46 -0700128 for (++it; it != end(); ++it) {
ehmaldonado35a872c2017-07-28 07:29:12 -0700129 oss << "," << it->ToJson();
hbos6ded1902016-11-01 01:50:46 -0700130 }
ehmaldonado35a872c2017-07-28 07:29:12 -0700131 oss << ']';
hbos6ded1902016-11-01 01:50:46 -0700132 }
133 return oss.str();
134}
135
hbos615d3012016-08-24 01:33:13 -0700136} // namespace webrtc