hbos | 615d301 | 2016-08-24 01:33:13 -0700 | [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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "api/stats/rtcstatsreport.h" |
hbos | 615d301 | 2016-08-24 01:33:13 -0700 | [diff] [blame] | 12 | |
hbos | 6ded190 | 2016-11-01 01:50:46 -0700 | [diff] [blame] | 13 | #include <sstream> |
| 14 | |
hbos | 615d301 | 2016-08-24 01:33:13 -0700 | [diff] [blame] | 15 | namespace webrtc { |
| 16 | |
| 17 | RTCStatsReport::ConstIterator::ConstIterator( |
| 18 | const rtc::scoped_refptr<const RTCStatsReport>& report, |
| 19 | StatsMap::const_iterator it) |
| 20 | : report_(report), |
| 21 | it_(it) { |
| 22 | } |
| 23 | |
| 24 | RTCStatsReport::ConstIterator::ConstIterator(const ConstIterator&& other) |
| 25 | : report_(std::move(other.report_)), |
| 26 | it_(std::move(other.it_)) { |
| 27 | } |
| 28 | |
| 29 | RTCStatsReport::ConstIterator::~ConstIterator() { |
| 30 | } |
| 31 | |
| 32 | RTCStatsReport::ConstIterator& RTCStatsReport::ConstIterator::operator++() { |
| 33 | ++it_; |
| 34 | return *this; |
| 35 | } |
| 36 | |
| 37 | RTCStatsReport::ConstIterator& RTCStatsReport::ConstIterator::operator++(int) { |
| 38 | return ++(*this); |
| 39 | } |
| 40 | |
| 41 | const RTCStats& RTCStatsReport::ConstIterator::operator*() const { |
| 42 | return *it_->second.get(); |
| 43 | } |
| 44 | |
hbos | 6ded190 | 2016-11-01 01:50:46 -0700 | [diff] [blame] | 45 | const RTCStats* RTCStatsReport::ConstIterator::operator->() const { |
| 46 | return it_->second.get(); |
| 47 | } |
| 48 | |
hbos | 615d301 | 2016-08-24 01:33:13 -0700 | [diff] [blame] | 49 | bool RTCStatsReport::ConstIterator::operator==( |
| 50 | const RTCStatsReport::ConstIterator& other) const { |
| 51 | return it_ == other.it_; |
| 52 | } |
| 53 | |
| 54 | bool RTCStatsReport::ConstIterator::operator!=( |
| 55 | const RTCStatsReport::ConstIterator& other) const { |
| 56 | return !(*this == other); |
| 57 | } |
| 58 | |
hbos | 6ded190 | 2016-11-01 01:50:46 -0700 | [diff] [blame] | 59 | rtc::scoped_refptr<RTCStatsReport> RTCStatsReport::Create( |
nisse | deb95f3 | 2016-11-28 01:54:54 -0800 | [diff] [blame] | 60 | int64_t timestamp_us) { |
hbos | 615d301 | 2016-08-24 01:33:13 -0700 | [diff] [blame] | 61 | return rtc::scoped_refptr<RTCStatsReport>( |
hbos | 6ded190 | 2016-11-01 01:50:46 -0700 | [diff] [blame] | 62 | new rtc::RefCountedObject<RTCStatsReport>(timestamp_us)); |
hbos | 615d301 | 2016-08-24 01:33:13 -0700 | [diff] [blame] | 63 | } |
| 64 | |
nisse | deb95f3 | 2016-11-28 01:54:54 -0800 | [diff] [blame] | 65 | RTCStatsReport::RTCStatsReport(int64_t timestamp_us) |
hbos | 6ded190 | 2016-11-01 01:50:46 -0700 | [diff] [blame] | 66 | : timestamp_us_(timestamp_us) { |
hbos | 615d301 | 2016-08-24 01:33:13 -0700 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | RTCStatsReport::~RTCStatsReport() { |
| 70 | } |
| 71 | |
hbos | 02d2a92 | 2016-12-21 01:29:05 -0800 | [diff] [blame] | 72 | void RTCStatsReport::AddStats(std::unique_ptr<const RTCStats> stats) { |
| 73 | auto result = stats_.insert(std::make_pair(std::string(stats->id()), |
| 74 | std::move(stats))); |
| 75 | RTC_DCHECK(result.second) << |
| 76 | "A stats object with ID " << result.first->second->id() << " is already " |
| 77 | "present in this stats report."; |
hbos | 615d301 | 2016-08-24 01:33:13 -0700 | [diff] [blame] | 78 | } |
| 79 | |
hbos | 6d183ac | 2016-08-29 07:20:33 -0700 | [diff] [blame] | 80 | const RTCStats* RTCStatsReport::Get(const std::string& id) const { |
hbos | 615d301 | 2016-08-24 01:33:13 -0700 | [diff] [blame] | 81 | StatsMap::const_iterator it = stats_.find(id); |
| 82 | if (it != stats_.cend()) |
| 83 | return it->second.get(); |
| 84 | return nullptr; |
| 85 | } |
| 86 | |
hbos | 6d183ac | 2016-08-29 07:20:33 -0700 | [diff] [blame] | 87 | void RTCStatsReport::TakeMembersFrom( |
| 88 | rtc::scoped_refptr<RTCStatsReport> victim) { |
| 89 | for (StatsMap::iterator it = victim->stats_.begin(); |
| 90 | it != victim->stats_.end(); ++it) { |
| 91 | AddStats(std::unique_ptr<const RTCStats>(it->second.release())); |
| 92 | } |
| 93 | victim->stats_.clear(); |
| 94 | } |
| 95 | |
hbos | 615d301 | 2016-08-24 01:33:13 -0700 | [diff] [blame] | 96 | RTCStatsReport::ConstIterator RTCStatsReport::begin() const { |
| 97 | return ConstIterator(rtc::scoped_refptr<const RTCStatsReport>(this), |
| 98 | stats_.cbegin()); |
| 99 | } |
| 100 | |
| 101 | RTCStatsReport::ConstIterator RTCStatsReport::end() const { |
| 102 | return ConstIterator(rtc::scoped_refptr<const RTCStatsReport>(this), |
| 103 | stats_.cend()); |
| 104 | } |
| 105 | |
ehmaldonado | 35a872c | 2017-07-28 07:29:12 -0700 | [diff] [blame] | 106 | std::string RTCStatsReport::ToJson() const { |
hbos | 6ded190 | 2016-11-01 01:50:46 -0700 | [diff] [blame] | 107 | std::ostringstream oss; |
| 108 | ConstIterator it = begin(); |
| 109 | if (it != end()) { |
ehmaldonado | 35a872c | 2017-07-28 07:29:12 -0700 | [diff] [blame] | 110 | oss << '[' << it->ToJson(); |
hbos | 6ded190 | 2016-11-01 01:50:46 -0700 | [diff] [blame] | 111 | for (++it; it != end(); ++it) { |
ehmaldonado | 35a872c | 2017-07-28 07:29:12 -0700 | [diff] [blame] | 112 | oss << "," << it->ToJson(); |
hbos | 6ded190 | 2016-11-01 01:50:46 -0700 | [diff] [blame] | 113 | } |
ehmaldonado | 35a872c | 2017-07-28 07:29:12 -0700 | [diff] [blame] | 114 | oss << ']'; |
hbos | 6ded190 | 2016-11-01 01:50:46 -0700 | [diff] [blame] | 115 | } |
| 116 | return oss.str(); |
| 117 | } |
| 118 | |
hbos | 615d301 | 2016-08-24 01:33:13 -0700 | [diff] [blame] | 119 | } // namespace webrtc |