hbos | 615d301 | 2016-08-24 08:33:13 | [diff] [blame] | 1 | /* |
| 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 Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 11 | #include "api/stats/rtc_stats_report.h" |
hbos | 615d301 | 2016-08-24 08:33:13 | [diff] [blame] | 12 | |
Yves Gerey | 3e70781 | 2018-11-28 15:47:49 | [diff] [blame] | 13 | #include <type_traits> |
| 14 | #include <utility> |
| 15 | |
| 16 | #include "rtc_base/checks.h" |
Steve Anton | a29b3a6 | 2018-12-27 23:44:25 | [diff] [blame] | 17 | #include "rtc_base/strings/string_builder.h" |
hbos | 6ded190 | 2016-11-01 08:50:46 | [diff] [blame] | 18 | |
hbos | 615d301 | 2016-08-24 08:33:13 | [diff] [blame] | 19 | namespace webrtc { |
| 20 | |
| 21 | RTCStatsReport::ConstIterator::ConstIterator( |
| 22 | const rtc::scoped_refptr<const RTCStatsReport>& report, |
| 23 | StatsMap::const_iterator it) |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 24 | : report_(report), it_(it) {} |
hbos | 615d301 | 2016-08-24 08:33:13 | [diff] [blame] | 25 | |
Mirko Bonadei | 6c70e16 | 2019-02-01 12:17:43 | [diff] [blame] | 26 | RTCStatsReport::ConstIterator::ConstIterator(ConstIterator&& other) = default; |
hbos | 615d301 | 2016-08-24 08:33:13 | [diff] [blame] | 27 | |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 28 | RTCStatsReport::ConstIterator::~ConstIterator() {} |
hbos | 615d301 | 2016-08-24 08:33:13 | [diff] [blame] | 29 | |
| 30 | RTCStatsReport::ConstIterator& RTCStatsReport::ConstIterator::operator++() { |
| 31 | ++it_; |
| 32 | return *this; |
| 33 | } |
| 34 | |
| 35 | RTCStatsReport::ConstIterator& RTCStatsReport::ConstIterator::operator++(int) { |
| 36 | return ++(*this); |
| 37 | } |
| 38 | |
| 39 | const RTCStats& RTCStatsReport::ConstIterator::operator*() const { |
| 40 | return *it_->second.get(); |
| 41 | } |
| 42 | |
hbos | 6ded190 | 2016-11-01 08:50:46 | [diff] [blame] | 43 | const RTCStats* RTCStatsReport::ConstIterator::operator->() const { |
| 44 | return it_->second.get(); |
| 45 | } |
| 46 | |
hbos | 615d301 | 2016-08-24 08:33:13 | [diff] [blame] | 47 | bool RTCStatsReport::ConstIterator::operator==( |
| 48 | const RTCStatsReport::ConstIterator& other) const { |
| 49 | return it_ == other.it_; |
| 50 | } |
| 51 | |
| 52 | bool RTCStatsReport::ConstIterator::operator!=( |
| 53 | const RTCStatsReport::ConstIterator& other) const { |
| 54 | return !(*this == other); |
| 55 | } |
| 56 | |
hbos | 6ded190 | 2016-11-01 08:50:46 | [diff] [blame] | 57 | rtc::scoped_refptr<RTCStatsReport> RTCStatsReport::Create( |
nisse | deb95f3 | 2016-11-28 09:54:54 | [diff] [blame] | 58 | int64_t timestamp_us) { |
hbos | 615d301 | 2016-08-24 08:33:13 | [diff] [blame] | 59 | return rtc::scoped_refptr<RTCStatsReport>( |
hbos | 6ded190 | 2016-11-01 08:50:46 | [diff] [blame] | 60 | new rtc::RefCountedObject<RTCStatsReport>(timestamp_us)); |
hbos | 615d301 | 2016-08-24 08:33:13 | [diff] [blame] | 61 | } |
| 62 | |
nisse | deb95f3 | 2016-11-28 09:54:54 | [diff] [blame] | 63 | RTCStatsReport::RTCStatsReport(int64_t timestamp_us) |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 64 | : timestamp_us_(timestamp_us) {} |
hbos | 615d301 | 2016-08-24 08:33:13 | [diff] [blame] | 65 | |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 66 | RTCStatsReport::~RTCStatsReport() {} |
hbos | 615d301 | 2016-08-24 08:33:13 | [diff] [blame] | 67 | |
Henrik Boström | 5b3541f | 2018-03-19 12:52:56 | [diff] [blame] | 68 | rtc::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 | |
hbos | 02d2a92 | 2016-12-21 09:29:05 | [diff] [blame] | 76 | void RTCStatsReport::AddStats(std::unique_ptr<const RTCStats> stats) { |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 77 | 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."; |
hbos | 615d301 | 2016-08-24 08:33:13 | [diff] [blame] | 83 | } |
| 84 | |
hbos | 6d183ac | 2016-08-29 14:20:33 | [diff] [blame] | 85 | const RTCStats* RTCStatsReport::Get(const std::string& id) const { |
hbos | 615d301 | 2016-08-24 08:33:13 | [diff] [blame] | 86 | StatsMap::const_iterator it = stats_.find(id); |
| 87 | if (it != stats_.cend()) |
| 88 | return it->second.get(); |
| 89 | return nullptr; |
| 90 | } |
| 91 | |
Henrik Boström | b619936 | 2018-03-12 09:27:55 | [diff] [blame] | 92 | std::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 | |
hbos | 6d183ac | 2016-08-29 14:20:33 | [diff] [blame] | 101 | void 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 | |
hbos | 615d301 | 2016-08-24 08:33:13 | [diff] [blame] | 110 | RTCStatsReport::ConstIterator RTCStatsReport::begin() const { |
| 111 | return ConstIterator(rtc::scoped_refptr<const RTCStatsReport>(this), |
| 112 | stats_.cbegin()); |
| 113 | } |
| 114 | |
| 115 | RTCStatsReport::ConstIterator RTCStatsReport::end() const { |
| 116 | return ConstIterator(rtc::scoped_refptr<const RTCStatsReport>(this), |
| 117 | stats_.cend()); |
| 118 | } |
| 119 | |
ehmaldonado | 35a872c | 2017-07-28 14:29:12 | [diff] [blame] | 120 | std::string RTCStatsReport::ToJson() const { |
Steve Anton | a29b3a6 | 2018-12-27 23:44:25 | [diff] [blame] | 121 | if (begin() == end()) { |
| 122 | return ""; |
hbos | 6ded190 | 2016-11-01 08:50:46 | [diff] [blame] | 123 | } |
Steve Anton | a29b3a6 | 2018-12-27 23:44:25 | [diff] [blame] | 124 | 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. Binnema | 8905d04 | 2019-01-07 11:41:05 | [diff] [blame] | 129 | separator = ","; |
Steve Anton | a29b3a6 | 2018-12-27 23:44:25 | [diff] [blame] | 130 | } |
| 131 | sb << "]"; |
| 132 | return sb.Release(); |
hbos | 6ded190 | 2016-11-01 08:50:46 | [diff] [blame] | 133 | } |
| 134 | |
hbos | 615d301 | 2016-08-24 08:33:13 | [diff] [blame] | 135 | } // namespace webrtc |