• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
RTCStats(long timestampUs, String type, String id, Map<String, Object> members)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.
getTimestampUs()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.
getType()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.
getId()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    * - Map of String keys to BigInteger / Double values
66    */
getMembers()67   public Map<String, Object> getMembers() {
68     return members;
69   }
70 
71   @Override
toString()72   public String toString() {
73     StringBuilder builder = new StringBuilder();
74     builder.append("{ timestampUs: ")
75         .append(timestampUs)
76         .append(", type: ")
77         .append(type)
78         .append(", id: ")
79         .append(id);
80     boolean first = true;
81     for (Map.Entry<String, Object> entry : members.entrySet()) {
82       builder.append(", ").append(entry.getKey()).append(": ");
83       appendValue(builder, entry.getValue());
84     }
85     builder.append(" }");
86     return builder.toString();
87   }
88 
appendValue(StringBuilder builder, Object value)89   private static void appendValue(StringBuilder builder, Object value) {
90     if (value instanceof Object[]) {
91       Object[] arrayValue = (Object[]) value;
92       builder.append('[');
93       for (int i = 0; i < arrayValue.length; ++i) {
94         if (i != 0) {
95           builder.append(", ");
96         }
97         appendValue(builder, arrayValue[i]);
98       }
99       builder.append(']');
100     } else if (value instanceof String) {
101       // Enclose strings in quotes to make it clear they're strings.
102       builder.append('"').append(value).append('"');
103     } else {
104       builder.append(value);
105     }
106   }
107 
108   // TODO(bugs.webrtc.org/8557) Use ctor directly with full Map type.
109   @SuppressWarnings("unchecked")
110   @CalledByNative
create(long timestampUs, String type, String id, Map members)111   static RTCStats create(long timestampUs, String type, String id, Map members) {
112     return new RTCStats(timestampUs, type, id, members);
113   }
114 }
115