You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

135 lines
3.8 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  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. #include "api/stats/rtc_stats_report.h"
  11. #include <type_traits>
  12. #include <utility>
  13. #include "rtc_base/checks.h"
  14. #include "rtc_base/strings/string_builder.h"
  15. namespace webrtc {
  16. RTCStatsReport::ConstIterator::ConstIterator(
  17. const rtc::scoped_refptr<const RTCStatsReport>& report,
  18. StatsMap::const_iterator it)
  19. : report_(report), it_(it) {}
  20. RTCStatsReport::ConstIterator::ConstIterator(ConstIterator&& other) = default;
  21. RTCStatsReport::ConstIterator::~ConstIterator() {}
  22. RTCStatsReport::ConstIterator& RTCStatsReport::ConstIterator::operator++() {
  23. ++it_;
  24. return *this;
  25. }
  26. RTCStatsReport::ConstIterator& RTCStatsReport::ConstIterator::operator++(int) {
  27. return ++(*this);
  28. }
  29. const RTCStats& RTCStatsReport::ConstIterator::operator*() const {
  30. return *it_->second.get();
  31. }
  32. const RTCStats* RTCStatsReport::ConstIterator::operator->() const {
  33. return it_->second.get();
  34. }
  35. bool RTCStatsReport::ConstIterator::operator==(
  36. const RTCStatsReport::ConstIterator& other) const {
  37. return it_ == other.it_;
  38. }
  39. bool RTCStatsReport::ConstIterator::operator!=(
  40. const RTCStatsReport::ConstIterator& other) const {
  41. return !(*this == other);
  42. }
  43. rtc::scoped_refptr<RTCStatsReport> RTCStatsReport::Create(
  44. int64_t timestamp_us) {
  45. return rtc::scoped_refptr<RTCStatsReport>(
  46. new rtc::RefCountedObject<RTCStatsReport>(timestamp_us));
  47. }
  48. RTCStatsReport::RTCStatsReport(int64_t timestamp_us)
  49. : timestamp_us_(timestamp_us) {}
  50. RTCStatsReport::~RTCStatsReport() {}
  51. rtc::scoped_refptr<RTCStatsReport> RTCStatsReport::Copy() const {
  52. rtc::scoped_refptr<RTCStatsReport> copy = Create(timestamp_us_);
  53. for (auto it = stats_.begin(); it != stats_.end(); ++it) {
  54. copy->AddStats(it->second->copy());
  55. }
  56. return copy;
  57. }
  58. void RTCStatsReport::AddStats(std::unique_ptr<const RTCStats> stats) {
  59. auto result =
  60. stats_.insert(std::make_pair(std::string(stats->id()), std::move(stats)));
  61. RTC_DCHECK(result.second)
  62. << "A stats object with ID " << result.first->second->id()
  63. << " is already "
  64. "present in this stats report.";
  65. }
  66. const RTCStats* RTCStatsReport::Get(const std::string& id) const {
  67. StatsMap::const_iterator it = stats_.find(id);
  68. if (it != stats_.cend())
  69. return it->second.get();
  70. return nullptr;
  71. }
  72. std::unique_ptr<const RTCStats> RTCStatsReport::Take(const std::string& id) {
  73. StatsMap::iterator it = stats_.find(id);
  74. if (it == stats_.end())
  75. return nullptr;
  76. std::unique_ptr<const RTCStats> stats = std::move(it->second);
  77. stats_.erase(it);
  78. return stats;
  79. }
  80. void RTCStatsReport::TakeMembersFrom(
  81. rtc::scoped_refptr<RTCStatsReport> victim) {
  82. for (StatsMap::iterator it = victim->stats_.begin();
  83. it != victim->stats_.end(); ++it) {
  84. AddStats(std::unique_ptr<const RTCStats>(it->second.release()));
  85. }
  86. victim->stats_.clear();
  87. }
  88. RTCStatsReport::ConstIterator RTCStatsReport::begin() const {
  89. return ConstIterator(rtc::scoped_refptr<const RTCStatsReport>(this),
  90. stats_.cbegin());
  91. }
  92. RTCStatsReport::ConstIterator RTCStatsReport::end() const {
  93. return ConstIterator(rtc::scoped_refptr<const RTCStatsReport>(this),
  94. stats_.cend());
  95. }
  96. std::string RTCStatsReport::ToJson() const {
  97. if (begin() == end()) {
  98. return "";
  99. }
  100. rtc::StringBuilder sb;
  101. sb << "[";
  102. const char* separator = "";
  103. for (ConstIterator it = begin(); it != end(); ++it) {
  104. sb << separator << it->ToJson();
  105. separator = ",";
  106. }
  107. sb << "]";
  108. return sb.Release();
  109. }
  110. } // namespace webrtc