blob: fac41ed6c7ae785dccd42a8217e65c4a90c72caa [file] [log] [blame]
asaperssonb432b262016-05-23 06:49:36 -07001/*
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
11package org.webrtc;
12
13import java.util.HashMap;
14import java.util.Map;
15
16// Java-side of androidmetrics_jni.cc.
17//
18// Rtc histograms can be queried through the API, getAndReset().
19// The returned map holds the name of a histogram and its samples.
20//
21// Example of |map| with one histogram:
22// |name|: "WebRTC.Video.InputFramesPerSecond"
23// |min|: 1
24// |max|: 100
25// |bucketCount|: 50
26// |samples|: [30]:1
27//
28// Most histograms are not updated frequently (e.g. most video metrics are an
29// average over the call and recorded when a stream is removed).
30// The metrics can for example be retrieved when a peer connection is closed.
31
32public class Metrics {
sakal2a5f3712016-09-09 00:11:48 -070033 private static final String TAG = "Metrics";
34
asaperssonb432b262016-05-23 06:49:36 -070035 static {
36 System.loadLibrary("jingle_peerconnection_so");
37 }
38 public final Map<String, HistogramInfo> map =
sakalb6760f92016-09-29 04:12:44 -070039 new HashMap<String, HistogramInfo>(); // <name, HistogramInfo>
asaperssonb432b262016-05-23 06:49:36 -070040
Magnus Jedvert1f2a3e72017-11-23 16:56:44 +010041 @CalledByNative
42 Metrics() {}
43
asaperssonb432b262016-05-23 06:49:36 -070044 /**
45 * Class holding histogram information.
46 */
47 public static class HistogramInfo {
48 public final int min;
49 public final int max;
50 public final int bucketCount;
51 public final Map<Integer, Integer> samples =
sakalb6760f92016-09-29 04:12:44 -070052 new HashMap<Integer, Integer>(); // <value, # of events>
asaperssonb432b262016-05-23 06:49:36 -070053
Magnus Jedvert1f2a3e72017-11-23 16:56:44 +010054 @CalledByNative("HistogramInfo")
asaperssonb432b262016-05-23 06:49:36 -070055 public HistogramInfo(int min, int max, int bucketCount) {
56 this.min = min;
57 this.max = max;
58 this.bucketCount = bucketCount;
59 }
60
Magnus Jedvert986a79c2017-11-18 16:45:18 +010061 @CalledByNative("HistogramInfo")
asaperssonb432b262016-05-23 06:49:36 -070062 public void addSample(int value, int numEvents) {
63 samples.put(value, numEvents);
64 }
65 }
66
Magnus Jedvert986a79c2017-11-18 16:45:18 +010067 @CalledByNative
asaperssonb432b262016-05-23 06:49:36 -070068 private void add(String name, HistogramInfo info) {
69 map.put(name, info);
70 }
71
72 // Enables gathering of metrics (which can be fetched with getAndReset()).
73 // Must be called before PeerConnectionFactory is created.
74 public static void enable() {
Magnus Jedvert986a79c2017-11-18 16:45:18 +010075 enableNative();
asaperssonb432b262016-05-23 06:49:36 -070076 }
77
78 // Gets and clears native histograms.
79 public static Metrics getAndReset() {
Magnus Jedvert986a79c2017-11-18 16:45:18 +010080 return getAndResetNative();
asaperssonb432b262016-05-23 06:49:36 -070081 }
82
Magnus Jedvert986a79c2017-11-18 16:45:18 +010083 private static native void enableNative();
84 private static native Metrics getAndResetNative();
asaperssonb432b262016-05-23 06:49:36 -070085}