| 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.h" |
| 12 | |
| 13 | #include <cstdio> |
| 14 | |
| 15 | #include "rtc_base/strings/string_builder.h" |
| 16 | |
| 17 | namespace webrtc { |
| 18 | |
| 19 | RTCStats::RTCStats(const RTCStats& other) |
| 20 | : RTCStats(other.id_, other.timestamp_) {} |
| 21 | |
| 22 | RTCStats::~RTCStats() {} |
| 23 | |
| 24 | bool RTCStats::operator==(const RTCStats& other) const { |
| 25 | if (type() != other.type() || id() != other.id()) |
| 26 | return false; |
| 27 | std::vector<Attribute> attributes = Attributes(); |
| 28 | std::vector<Attribute> other_attributes = other.Attributes(); |
| 29 | RTC_DCHECK_EQ(attributes.size(), other_attributes.size()); |
| 30 | for (size_t i = 0; i < attributes.size(); ++i) { |
| 31 | if (attributes[i] != other_attributes[i]) { |
| 32 | return false; |
| 33 | } |
| 34 | } |
| 35 | return true; |
| 36 | } |
| 37 | |
| 38 | bool RTCStats::operator!=(const RTCStats& other) const { |
| 39 | return !(*this == other); |
| 40 | } |
| 41 | |
| 42 | std::string RTCStats::ToJson() const { |
| 43 | rtc::StringBuilder sb; |
| 44 | sb << "{\"type\":\"" << type() |
| 45 | << "\"," |
| 46 | "\"id\":\"" |
| 47 | << id_ |
| 48 | << "\"," |
| 49 | "\"timestamp\":" |
| 50 | << timestamp_.us(); |
| 51 | for (const Attribute& attribute : Attributes()) { |
| 52 | if (attribute.has_value()) { |
| 53 | sb << ",\"" << attribute.name() << "\":"; |
| 54 | if (attribute.holds_alternative<std::string>()) { |
| 55 | sb << "\""; |
| 56 | } |
| 57 | sb << attribute.ToString(); |
| 58 | if (attribute.holds_alternative<std::string>()) { |
| 59 | sb << "\""; |
| 60 | } |
| 61 | } |
| 62 | } |
| 63 | sb << "}"; |
| 64 | return sb.Release(); |
| 65 | } |
| 66 | |
| 67 | std::vector<Attribute> RTCStats::Attributes() const { |
| 68 | return AttributesImpl(0); |
| 69 | } |
| 70 | |
| 71 | std::vector<Attribute> RTCStats::AttributesImpl( |
| 72 | size_t additional_capacity) const { |
| 73 | std::vector<Attribute> attributes; |
| 74 | attributes.reserve(additional_capacity); |
| 75 | return attributes; |
| 76 | } |
| 77 | |
| 78 | } // namespace webrtc |
| 79 | |