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>( 60 new rtc::RefCountedObject<RTCStatsReport>(timestamp_us)); 61 } 62 RTCStatsReport(int64_t timestamp_us)63RTCStatsReport::RTCStatsReport(int64_t timestamp_us) 64 : timestamp_us_(timestamp_us) {} 65 ~RTCStatsReport()66RTCStatsReport::~RTCStatsReport() {} 67 Copy() const68rtc::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 AddStats(std::unique_ptr<const RTCStats> stats)76void RTCStatsReport::AddStats(std::unique_ptr<const RTCStats> stats) { 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."; 83 } 84 Get(const std::string & id) const85const RTCStats* RTCStatsReport::Get(const std::string& id) const { 86 StatsMap::const_iterator it = stats_.find(id); 87 if (it != stats_.cend()) 88 return it->second.get(); 89 return nullptr; 90 } 91 Take(const std::string & id)92std::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 TakeMembersFrom(rtc::scoped_refptr<RTCStatsReport> victim)101void 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 begin() const110RTCStatsReport::ConstIterator RTCStatsReport::begin() const { 111 return ConstIterator(rtc::scoped_refptr<const RTCStatsReport>(this), 112 stats_.cbegin()); 113 } 114 end() const115RTCStatsReport::ConstIterator RTCStatsReport::end() const { 116 return ConstIterator(rtc::scoped_refptr<const RTCStatsReport>(this), 117 stats_.cend()); 118 } 119 ToJson() const120std::string RTCStatsReport::ToJson() const { 121 if (begin() == end()) { 122 return ""; 123 } 124 rtc::StringBuilder sb; 125 sb << "["; 126 const char* separator = ""; 127 for (ConstIterator it = begin(); it != end(); ++it) { 128 sb << separator << it->ToJson(); 129 separator = ","; 130 } 131 sb << "]"; 132 return sb.Release(); 133 } 134 135 } // namespace webrtc 136