1*d9f75844SAndroid Build Coastguard Worker /* 2*d9f75844SAndroid Build Coastguard Worker * Copyright 2016 The WebRTC Project Authors. All rights reserved. 3*d9f75844SAndroid Build Coastguard Worker * 4*d9f75844SAndroid Build Coastguard Worker * Use of this source code is governed by a BSD-style license 5*d9f75844SAndroid Build Coastguard Worker * that can be found in the LICENSE file in the root of the source 6*d9f75844SAndroid Build Coastguard Worker * tree. An additional intellectual property rights grant can be found 7*d9f75844SAndroid Build Coastguard Worker * in the file PATENTS. All contributing project authors may 8*d9f75844SAndroid Build Coastguard Worker * be found in the AUTHORS file in the root of the source tree. 9*d9f75844SAndroid Build Coastguard Worker */ 10*d9f75844SAndroid Build Coastguard Worker 11*d9f75844SAndroid Build Coastguard Worker #include "api/stats/rtc_stats_report.h" 12*d9f75844SAndroid Build Coastguard Worker 13*d9f75844SAndroid Build Coastguard Worker #include <type_traits> 14*d9f75844SAndroid Build Coastguard Worker #include <utility> 15*d9f75844SAndroid Build Coastguard Worker 16*d9f75844SAndroid Build Coastguard Worker #include "rtc_base/checks.h" 17*d9f75844SAndroid Build Coastguard Worker #include "rtc_base/strings/string_builder.h" 18*d9f75844SAndroid Build Coastguard Worker 19*d9f75844SAndroid Build Coastguard Worker namespace webrtc { 20*d9f75844SAndroid Build Coastguard Worker ConstIterator(const rtc::scoped_refptr<const RTCStatsReport> & report,StatsMap::const_iterator it)21*d9f75844SAndroid Build Coastguard WorkerRTCStatsReport::ConstIterator::ConstIterator( 22*d9f75844SAndroid Build Coastguard Worker const rtc::scoped_refptr<const RTCStatsReport>& report, 23*d9f75844SAndroid Build Coastguard Worker StatsMap::const_iterator it) 24*d9f75844SAndroid Build Coastguard Worker : report_(report), it_(it) {} 25*d9f75844SAndroid Build Coastguard Worker 26*d9f75844SAndroid Build Coastguard Worker RTCStatsReport::ConstIterator::ConstIterator(ConstIterator&& other) = default; 27*d9f75844SAndroid Build Coastguard Worker ~ConstIterator()28*d9f75844SAndroid Build Coastguard WorkerRTCStatsReport::ConstIterator::~ConstIterator() {} 29*d9f75844SAndroid Build Coastguard Worker operator ++()30*d9f75844SAndroid Build Coastguard WorkerRTCStatsReport::ConstIterator& RTCStatsReport::ConstIterator::operator++() { 31*d9f75844SAndroid Build Coastguard Worker ++it_; 32*d9f75844SAndroid Build Coastguard Worker return *this; 33*d9f75844SAndroid Build Coastguard Worker } 34*d9f75844SAndroid Build Coastguard Worker operator ++(int)35*d9f75844SAndroid Build Coastguard WorkerRTCStatsReport::ConstIterator& RTCStatsReport::ConstIterator::operator++(int) { 36*d9f75844SAndroid Build Coastguard Worker return ++(*this); 37*d9f75844SAndroid Build Coastguard Worker } 38*d9f75844SAndroid Build Coastguard Worker operator *() const39*d9f75844SAndroid Build Coastguard Workerconst RTCStats& RTCStatsReport::ConstIterator::operator*() const { 40*d9f75844SAndroid Build Coastguard Worker return *it_->second.get(); 41*d9f75844SAndroid Build Coastguard Worker } 42*d9f75844SAndroid Build Coastguard Worker operator ->() const43*d9f75844SAndroid Build Coastguard Workerconst RTCStats* RTCStatsReport::ConstIterator::operator->() const { 44*d9f75844SAndroid Build Coastguard Worker return it_->second.get(); 45*d9f75844SAndroid Build Coastguard Worker } 46*d9f75844SAndroid Build Coastguard Worker operator ==(const RTCStatsReport::ConstIterator & other) const47*d9f75844SAndroid Build Coastguard Workerbool RTCStatsReport::ConstIterator::operator==( 48*d9f75844SAndroid Build Coastguard Worker const RTCStatsReport::ConstIterator& other) const { 49*d9f75844SAndroid Build Coastguard Worker return it_ == other.it_; 50*d9f75844SAndroid Build Coastguard Worker } 51*d9f75844SAndroid Build Coastguard Worker operator !=(const RTCStatsReport::ConstIterator & other) const52*d9f75844SAndroid Build Coastguard Workerbool RTCStatsReport::ConstIterator::operator!=( 53*d9f75844SAndroid Build Coastguard Worker const RTCStatsReport::ConstIterator& other) const { 54*d9f75844SAndroid Build Coastguard Worker return !(*this == other); 55*d9f75844SAndroid Build Coastguard Worker } 56*d9f75844SAndroid Build Coastguard Worker Create(int64_t timestamp_us)57*d9f75844SAndroid Build Coastguard Workerrtc::scoped_refptr<RTCStatsReport> RTCStatsReport::Create( 58*d9f75844SAndroid Build Coastguard Worker int64_t timestamp_us) { 59*d9f75844SAndroid Build Coastguard Worker return rtc::scoped_refptr<RTCStatsReport>(new RTCStatsReport(timestamp_us)); 60*d9f75844SAndroid Build Coastguard Worker } 61*d9f75844SAndroid Build Coastguard Worker Create(Timestamp timestamp)62*d9f75844SAndroid Build Coastguard Workerrtc::scoped_refptr<RTCStatsReport> RTCStatsReport::Create(Timestamp timestamp) { 63*d9f75844SAndroid Build Coastguard Worker return rtc::scoped_refptr<RTCStatsReport>(new RTCStatsReport(timestamp)); 64*d9f75844SAndroid Build Coastguard Worker } 65*d9f75844SAndroid Build Coastguard Worker RTCStatsReport(int64_t timestamp_us)66*d9f75844SAndroid Build Coastguard WorkerRTCStatsReport::RTCStatsReport(int64_t timestamp_us) 67*d9f75844SAndroid Build Coastguard Worker : RTCStatsReport(Timestamp::Micros(timestamp_us)) {} 68*d9f75844SAndroid Build Coastguard Worker RTCStatsReport(Timestamp timestamp)69*d9f75844SAndroid Build Coastguard WorkerRTCStatsReport::RTCStatsReport(Timestamp timestamp) : timestamp_(timestamp) {} 70*d9f75844SAndroid Build Coastguard Worker Copy() const71*d9f75844SAndroid Build Coastguard Workerrtc::scoped_refptr<RTCStatsReport> RTCStatsReport::Copy() const { 72*d9f75844SAndroid Build Coastguard Worker rtc::scoped_refptr<RTCStatsReport> copy = Create(timestamp_); 73*d9f75844SAndroid Build Coastguard Worker for (auto it = stats_.begin(); it != stats_.end(); ++it) { 74*d9f75844SAndroid Build Coastguard Worker copy->AddStats(it->second->copy()); 75*d9f75844SAndroid Build Coastguard Worker } 76*d9f75844SAndroid Build Coastguard Worker return copy; 77*d9f75844SAndroid Build Coastguard Worker } 78*d9f75844SAndroid Build Coastguard Worker AddStats(std::unique_ptr<const RTCStats> stats)79*d9f75844SAndroid Build Coastguard Workervoid RTCStatsReport::AddStats(std::unique_ptr<const RTCStats> stats) { 80*d9f75844SAndroid Build Coastguard Worker #if RTC_DCHECK_IS_ON 81*d9f75844SAndroid Build Coastguard Worker auto result = 82*d9f75844SAndroid Build Coastguard Worker #endif 83*d9f75844SAndroid Build Coastguard Worker stats_.insert(std::make_pair(std::string(stats->id()), std::move(stats))); 84*d9f75844SAndroid Build Coastguard Worker #if RTC_DCHECK_IS_ON 85*d9f75844SAndroid Build Coastguard Worker RTC_DCHECK(result.second) 86*d9f75844SAndroid Build Coastguard Worker << "A stats object with ID \"" << result.first->second->id() << "\" is " 87*d9f75844SAndroid Build Coastguard Worker << "already present in this stats report."; 88*d9f75844SAndroid Build Coastguard Worker #endif 89*d9f75844SAndroid Build Coastguard Worker } 90*d9f75844SAndroid Build Coastguard Worker Get(const std::string & id) const91*d9f75844SAndroid Build Coastguard Workerconst RTCStats* RTCStatsReport::Get(const std::string& id) const { 92*d9f75844SAndroid Build Coastguard Worker StatsMap::const_iterator it = stats_.find(id); 93*d9f75844SAndroid Build Coastguard Worker if (it != stats_.cend()) 94*d9f75844SAndroid Build Coastguard Worker return it->second.get(); 95*d9f75844SAndroid Build Coastguard Worker return nullptr; 96*d9f75844SAndroid Build Coastguard Worker } 97*d9f75844SAndroid Build Coastguard Worker Take(const std::string & id)98*d9f75844SAndroid Build Coastguard Workerstd::unique_ptr<const RTCStats> RTCStatsReport::Take(const std::string& id) { 99*d9f75844SAndroid Build Coastguard Worker StatsMap::iterator it = stats_.find(id); 100*d9f75844SAndroid Build Coastguard Worker if (it == stats_.end()) 101*d9f75844SAndroid Build Coastguard Worker return nullptr; 102*d9f75844SAndroid Build Coastguard Worker std::unique_ptr<const RTCStats> stats = std::move(it->second); 103*d9f75844SAndroid Build Coastguard Worker stats_.erase(it); 104*d9f75844SAndroid Build Coastguard Worker return stats; 105*d9f75844SAndroid Build Coastguard Worker } 106*d9f75844SAndroid Build Coastguard Worker TakeMembersFrom(rtc::scoped_refptr<RTCStatsReport> other)107*d9f75844SAndroid Build Coastguard Workervoid RTCStatsReport::TakeMembersFrom(rtc::scoped_refptr<RTCStatsReport> other) { 108*d9f75844SAndroid Build Coastguard Worker for (StatsMap::iterator it = other->stats_.begin(); it != other->stats_.end(); 109*d9f75844SAndroid Build Coastguard Worker ++it) { 110*d9f75844SAndroid Build Coastguard Worker AddStats(std::unique_ptr<const RTCStats>(it->second.release())); 111*d9f75844SAndroid Build Coastguard Worker } 112*d9f75844SAndroid Build Coastguard Worker other->stats_.clear(); 113*d9f75844SAndroid Build Coastguard Worker } 114*d9f75844SAndroid Build Coastguard Worker begin() const115*d9f75844SAndroid Build Coastguard WorkerRTCStatsReport::ConstIterator RTCStatsReport::begin() const { 116*d9f75844SAndroid Build Coastguard Worker return ConstIterator(rtc::scoped_refptr<const RTCStatsReport>(this), 117*d9f75844SAndroid Build Coastguard Worker stats_.cbegin()); 118*d9f75844SAndroid Build Coastguard Worker } 119*d9f75844SAndroid Build Coastguard Worker end() const120*d9f75844SAndroid Build Coastguard WorkerRTCStatsReport::ConstIterator RTCStatsReport::end() const { 121*d9f75844SAndroid Build Coastguard Worker return ConstIterator(rtc::scoped_refptr<const RTCStatsReport>(this), 122*d9f75844SAndroid Build Coastguard Worker stats_.cend()); 123*d9f75844SAndroid Build Coastguard Worker } 124*d9f75844SAndroid Build Coastguard Worker ToJson() const125*d9f75844SAndroid Build Coastguard Workerstd::string RTCStatsReport::ToJson() const { 126*d9f75844SAndroid Build Coastguard Worker if (begin() == end()) { 127*d9f75844SAndroid Build Coastguard Worker return ""; 128*d9f75844SAndroid Build Coastguard Worker } 129*d9f75844SAndroid Build Coastguard Worker rtc::StringBuilder sb; 130*d9f75844SAndroid Build Coastguard Worker sb << "["; 131*d9f75844SAndroid Build Coastguard Worker const char* separator = ""; 132*d9f75844SAndroid Build Coastguard Worker for (ConstIterator it = begin(); it != end(); ++it) { 133*d9f75844SAndroid Build Coastguard Worker sb << separator << it->ToJson(); 134*d9f75844SAndroid Build Coastguard Worker separator = ","; 135*d9f75844SAndroid Build Coastguard Worker } 136*d9f75844SAndroid Build Coastguard Worker sb << "]"; 137*d9f75844SAndroid Build Coastguard Worker return sb.Release(); 138*d9f75844SAndroid Build Coastguard Worker } 139*d9f75844SAndroid Build Coastguard Worker 140*d9f75844SAndroid Build Coastguard Worker } // namespace webrtc 141