deadbeef | 8221587 | 2017-04-18 10:27:51 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017 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 | package org.webrtc; |
| 12 | |
| 13 | import java.util.Map; |
| 14 | |
| 15 | /** |
| 16 | * Java version of webrtc::RTCStats. Represents an RTCStats object, as |
| 17 | * described in https://w3c.github.io/webrtc-stats/. The |id|, |timestampUs| |
| 18 | * and |type| accessors have the same meaning for this class as for the |
| 19 | * RTCStats dictionary. Each RTCStatsReport produced by getStats contains |
| 20 | * multiple RTCStats objects; one for each underlying object (codec, stream, |
| 21 | * transport, etc.) that was inspected to produce the stats. |
| 22 | */ |
| 23 | public class RTCStats { |
| 24 | private final long timestampUs; |
| 25 | private final String type; |
| 26 | private final String id; |
| 27 | private final Map<String, Object> members; |
| 28 | |
| 29 | public RTCStats(long timestampUs, String type, String id, Map<String, Object> members) { |
| 30 | this.timestampUs = timestampUs; |
| 31 | this.type = type; |
| 32 | this.id = id; |
| 33 | this.members = members; |
| 34 | } |
| 35 | |
| 36 | // Timestamp in microseconds. |
| 37 | public double getTimestampUs() { |
| 38 | return timestampUs; |
| 39 | } |
| 40 | |
| 41 | // Equivalent to RTCStatsType in the stats spec. Indicates the type of the |
| 42 | // object that was inspected to produce the stats. |
| 43 | public String getType() { |
| 44 | return type; |
| 45 | } |
| 46 | |
| 47 | // Unique ID representing this stats object. May be referred to by members of |
| 48 | // other stats objects. |
| 49 | public String getId() { |
| 50 | return id; |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Returns map of member names to values. Returns as an ordered map so that |
| 55 | * the stats object can be serialized with a consistent ordering. |
| 56 | * |
| 57 | * Values will be one of the following objects: |
| 58 | * - Boolean |
| 59 | * - Integer (for 32-bit signed integers) |
| 60 | * - Long (for 32-bit unsigned and 64-bit signed integers) |
| 61 | * - BigInteger (for 64-bit unsigned integers) |
| 62 | * - Double |
| 63 | * - String |
| 64 | * - The array form of any of the above (e.g., Integer[]) |
| 65 | */ |
| 66 | public Map<String, Object> getMembers() { |
| 67 | return members; |
| 68 | } |
| 69 | |
Sami Kalliomäki | bde473e | 2017-10-30 13:34:41 +0100 | [diff] [blame^] | 70 | @Override |
deadbeef | 8221587 | 2017-04-18 10:27:51 -0700 | [diff] [blame] | 71 | public String toString() { |
| 72 | StringBuilder builder = new StringBuilder(); |
| 73 | builder.append("{ timestampUs: ") |
| 74 | .append(timestampUs) |
| 75 | .append(", type: ") |
| 76 | .append(type) |
| 77 | .append(", id: ") |
| 78 | .append(id); |
| 79 | boolean first = true; |
| 80 | for (Map.Entry<String, Object> entry : members.entrySet()) { |
| 81 | builder.append(", ").append(entry.getKey()).append(": "); |
| 82 | appendValue(builder, entry.getValue()); |
| 83 | } |
| 84 | builder.append(" }"); |
| 85 | return builder.toString(); |
| 86 | } |
| 87 | |
| 88 | private static void appendValue(StringBuilder builder, Object value) { |
| 89 | if (value instanceof Object[]) { |
| 90 | Object[] arrayValue = (Object[]) value; |
| 91 | builder.append('['); |
| 92 | for (int i = 0; i < arrayValue.length; ++i) { |
| 93 | if (i != 0) { |
| 94 | builder.append(", "); |
| 95 | } |
| 96 | appendValue(builder, arrayValue[i]); |
| 97 | } |
| 98 | builder.append(']'); |
| 99 | } else if (value instanceof String) { |
| 100 | // Enclose strings in quotes to make it clear they're strings. |
| 101 | builder.append('"').append(value).append('"'); |
| 102 | } else { |
| 103 | builder.append(value); |
| 104 | } |
| 105 | } |
| 106 | } |