blob: 33f6b6a121c7662e4890755d6509816de4719a8f [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)
Yves Gerey665174f2018-06-19 15:03:05 +020020 : report_(report), it_(it) {}
hbos615d3012016-08-24 01:33:13 -070021
22RTCStatsReport::ConstIterator::ConstIterator(const ConstIterator&& other)
Yves Gerey665174f2018-06-19 15:03:05 +020023 : report_(std::move(other.report_)), it_(std::move(other.it_)) {}
hbos615d3012016-08-24 01:33:13 -070024
Yves Gerey665174f2018-06-19 15:03:05 +020025RTCStatsReport::ConstIterator::~ConstIterator() {}
hbos615d3012016-08-24 01:33:13 -070026
27RTCStatsReport::ConstIterator& RTCStatsReport::ConstIterator::operator++() {
28 ++it_;
29 return *this;
30}
31
32RTCStatsReport::ConstIterator& RTCStatsReport::ConstIterator::operator++(int) {
33 return ++(*this);
34}
35
36const RTCStats& RTCStatsReport::ConstIterator::operator*() const {
37 return *it_->second.get();
38}
39
hbos6ded1902016-11-01 01:50:46 -070040const RTCStats* RTCStatsReport::ConstIterator::operator->() const {
41 return it_->second.get();
42}
43
hbos615d3012016-08-24 01:33:13 -070044bool RTCStatsReport::ConstIterator::operator==(
45 const RTCStatsReport::ConstIterator& other) const {
46 return it_ == other.it_;
47}
48
49bool RTCStatsReport::ConstIterator::operator!=(
50 const RTCStatsReport::ConstIterator& other) const {
51 return !(*this == other);
52}
53
hbos6ded1902016-11-01 01:50:46 -070054rtc::scoped_refptr<RTCStatsReport> RTCStatsReport::Create(
nissedeb95f32016-11-28 01:54:54 -080055 int64_t timestamp_us) {
hbos615d3012016-08-24 01:33:13 -070056 return rtc::scoped_refptr<RTCStatsReport>(
hbos6ded1902016-11-01 01:50:46 -070057 new rtc::RefCountedObject<RTCStatsReport>(timestamp_us));
hbos615d3012016-08-24 01:33:13 -070058}
59
nissedeb95f32016-11-28 01:54:54 -080060RTCStatsReport::RTCStatsReport(int64_t timestamp_us)
Yves Gerey665174f2018-06-19 15:03:05 +020061 : timestamp_us_(timestamp_us) {}
hbos615d3012016-08-24 01:33:13 -070062
Yves Gerey665174f2018-06-19 15:03:05 +020063RTCStatsReport::~RTCStatsReport() {}
hbos615d3012016-08-24 01:33:13 -070064
Henrik Boström5b3541f2018-03-19 13:52:56 +010065rtc::scoped_refptr<RTCStatsReport> RTCStatsReport::Copy() const {
66 rtc::scoped_refptr<RTCStatsReport> copy = Create(timestamp_us_);
67 for (auto it = stats_.begin(); it != stats_.end(); ++it) {
68 copy->AddStats(it->second->copy());
69 }
70 return copy;
71}
72
hbos02d2a922016-12-21 01:29:05 -080073void RTCStatsReport::AddStats(std::unique_ptr<const RTCStats> stats) {
Yves Gerey665174f2018-06-19 15:03:05 +020074 auto result =
75 stats_.insert(std::make_pair(std::string(stats->id()), std::move(stats)));
76 RTC_DCHECK(result.second)
77 << "A stats object with ID " << result.first->second->id()
78 << " is already "
79 "present in this stats report.";
hbos615d3012016-08-24 01:33:13 -070080}
81
hbos6d183ac2016-08-29 07:20:33 -070082const RTCStats* RTCStatsReport::Get(const std::string& id) const {
hbos615d3012016-08-24 01:33:13 -070083 StatsMap::const_iterator it = stats_.find(id);
84 if (it != stats_.cend())
85 return it->second.get();
86 return nullptr;
87}
88
Henrik Boströmb6199362018-03-12 10:27:55 +010089std::unique_ptr<const RTCStats> RTCStatsReport::Take(const std::string& id) {
90 StatsMap::iterator it = stats_.find(id);
91 if (it == stats_.end())
92 return nullptr;
93 std::unique_ptr<const RTCStats> stats = std::move(it->second);
94 stats_.erase(it);
95 return stats;
96}
97
hbos6d183ac2016-08-29 07:20:33 -070098void RTCStatsReport::TakeMembersFrom(
99 rtc::scoped_refptr<RTCStatsReport> victim) {
100 for (StatsMap::iterator it = victim->stats_.begin();
101 it != victim->stats_.end(); ++it) {
102 AddStats(std::unique_ptr<const RTCStats>(it->second.release()));
103 }
104 victim->stats_.clear();
105}
106
hbos615d3012016-08-24 01:33:13 -0700107RTCStatsReport::ConstIterator RTCStatsReport::begin() const {
108 return ConstIterator(rtc::scoped_refptr<const RTCStatsReport>(this),
109 stats_.cbegin());
110}
111
112RTCStatsReport::ConstIterator RTCStatsReport::end() const {
113 return ConstIterator(rtc::scoped_refptr<const RTCStatsReport>(this),
114 stats_.cend());
115}
116
ehmaldonado35a872c2017-07-28 07:29:12 -0700117std::string RTCStatsReport::ToJson() const {
hbos6ded1902016-11-01 01:50:46 -0700118 std::ostringstream oss;
119 ConstIterator it = begin();
120 if (it != end()) {
ehmaldonado35a872c2017-07-28 07:29:12 -0700121 oss << '[' << it->ToJson();
hbos6ded1902016-11-01 01:50:46 -0700122 for (++it; it != end(); ++it) {
ehmaldonado35a872c2017-07-28 07:29:12 -0700123 oss << "," << it->ToJson();
hbos6ded1902016-11-01 01:50:46 -0700124 }
ehmaldonado35a872c2017-07-28 07:29:12 -0700125 oss << ']';
hbos6ded1902016-11-01 01:50:46 -0700126 }
127 return oss.str();
128}
129
hbos615d3012016-08-24 01:33:13 -0700130} // namespace webrtc