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 11 #include "api/stats/rtc_stats_report.h" 12 13 #include <type_traits> 14 #include <utility> 15 16 #include "rtc_base/checks.h" 17 #include "rtc_base/strings/string_builder.h" 18 19 namespace webrtc { 20 ConstIterator(const rtc::scoped_refptr<const RTCStatsReport> & report,StatsMap::const_iterator it)21RTCStatsReport::ConstIterator::ConstIterator( 22 const rtc::scoped_refptr<const RTCStatsReport>& report, 23 StatsMap::const_iterator it) 24 : report_(report), it_(it) {} 25 26 RTCStatsReport::ConstIterator::ConstIterator(ConstIterator&& other) = default; 27 ~ConstIterator()28RTCStatsReport::ConstIterator::~ConstIterator() {} 29 operator ++()30RTCStatsReport::ConstIterator& RTCStatsReport::ConstIterator::operator++() { 31 ++it_; 32 return *this; 33 } 34 operator ++(int)35RTCStatsReport::ConstIterator& RTCStatsReport::ConstIterator::operator++(int) { 36 return ++(*this); 37 } 38 operator *() const39const RTCStats& RTCStatsReport::ConstIterator::operator*() const { 40 return *it_->second.get(); 41 } 42 operator ->() const43const RTCStats* RTCStatsReport::ConstIterator::operator->() const { 44 return it_->second.get(); 45 } 46 operator ==(const RTCStatsReport::ConstIterator & other) const47bool RTCStatsReport::ConstIterator::operator==( 48 const RTCStatsReport::ConstIterator& other) const { 49 return it_ == other.it_; 50 } 51 operator !=(const RTCStatsReport::ConstIterator & other) const52bool RTCStatsReport::ConstIterator::operator!=( 53 const RTCStatsReport::ConstIterator& other) const { 54 return !(*this == other); 55 } 56 Create(int64_t timestamp_us)57rtc::scoped_refptr<RTCStatsReport> RTCStatsReport::Create( 58 int64_t timestamp_us) { 59 return rtc::scoped_refptr<RTCStatsReport>(new RTCStatsReport(timestamp_us)); 60 } 61 Create(Timestamp timestamp)62rtc::scoped_refptr<RTCStatsReport> RTCStatsReport::Create(Timestamp timestamp) { 63 return rtc::scoped_refptr<RTCStatsReport>(new RTCStatsReport(timestamp)); 64 } 65 RTCStatsReport(int64_t timestamp_us)66RTCStatsReport::RTCStatsReport(int64_t timestamp_us) 67 : RTCStatsReport(Timestamp::Micros(timestamp_us)) {} 68 RTCStatsReport(Timestamp timestamp)69RTCStatsReport::RTCStatsReport(Timestamp timestamp) : timestamp_(timestamp) {} 70 Copy() const71rtc::scoped_refptr<RTCStatsReport> RTCStatsReport::Copy() const { 72 rtc::scoped_refptr<RTCStatsReport> copy = Create(timestamp_); 73 for (auto it = stats_.begin(); it != stats_.end(); ++it) { 74 copy->AddStats(it->second->copy()); 75 } 76 return copy; 77 } 78 AddStats(std::unique_ptr<const RTCStats> stats)79void RTCStatsReport::AddStats(std::unique_ptr<const RTCStats> stats) { 80 #if RTC_DCHECK_IS_ON 81 auto result = 82 #endif 83 stats_.insert(std::make_pair(std::string(stats->id()), std::move(stats))); 84 #if RTC_DCHECK_IS_ON 85 RTC_DCHECK(result.second) 86 << "A stats object with ID \"" << result.first->second->id() << "\" is " 87 << "already present in this stats report."; 88 #endif 89 } 90 Get(const std::string & id) const91const RTCStats* RTCStatsReport::Get(const std::string& id) const { 92 StatsMap::const_iterator it = stats_.find(id); 93 if (it != stats_.cend()) 94 return it->second.get(); 95 return nullptr; 96 } 97 Take(const std::string & id)98std::unique_ptr<const RTCStats> RTCStatsReport::Take(const std::string& id) { 99 StatsMap::iterator it = stats_.find(id); 100 if (it == stats_.end()) 101 return nullptr; 102 std::unique_ptr<const RTCStats> stats = std::move(it->second); 103 stats_.erase(it); 104 return stats; 105 } 106 TakeMembersFrom(rtc::scoped_refptr<RTCStatsReport> other)107void RTCStatsReport::TakeMembersFrom(rtc::scoped_refptr<RTCStatsReport> other) { 108 for (StatsMap::iterator it = other->stats_.begin(); it != other->stats_.end(); 109 ++it) { 110 AddStats(std::unique_ptr<const RTCStats>(it->second.release())); 111 } 112 other->stats_.clear(); 113 } 114 begin() const115RTCStatsReport::ConstIterator RTCStatsReport::begin() const { 116 return ConstIterator(rtc::scoped_refptr<const RTCStatsReport>(this), 117 stats_.cbegin()); 118 } 119 end() const120RTCStatsReport::ConstIterator RTCStatsReport::end() const { 121 return ConstIterator(rtc::scoped_refptr<const RTCStatsReport>(this), 122 stats_.cend()); 123 } 124 ToJson() const125std::string RTCStatsReport::ToJson() const { 126 if (begin() == end()) { 127 return ""; 128 } 129 rtc::StringBuilder sb; 130 sb << "["; 131 const char* separator = ""; 132 for (ConstIterator it = begin(); it != end(); ++it) { 133 sb << separator << it->ToJson(); 134 separator = ","; 135 } 136 sb << "]"; 137 return sb.Release(); 138 } 139 140 } // namespace webrtc 141