xref: /aosp_15_r20/external/webrtc/examples/androidapp/src/org/appspot/apprtc/HudFragment.java (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1 /*
2  *  Copyright 2015 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.appspot.apprtc;
12 
13 import android.app.Fragment;
14 import android.os.Bundle;
15 import android.view.LayoutInflater;
16 import android.view.View;
17 import android.view.ViewGroup;
18 import android.widget.ImageButton;
19 import android.widget.TextView;
20 import org.webrtc.RTCStats;
21 import org.webrtc.RTCStatsReport;
22 
23 /**
24  * Fragment for HUD statistics display.
25  */
26 public class HudFragment extends Fragment {
27   private TextView statView;
28   private ImageButton toggleDebugButton;
29   private boolean displayHud;
30   private volatile boolean isRunning;
31   private CpuMonitor cpuMonitor;
32 
33   @Override
onCreateView( LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)34   public View onCreateView(
35       LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
36     View controlView = inflater.inflate(R.layout.fragment_hud, container, false);
37 
38     // Create UI controls.
39     statView = controlView.findViewById(R.id.hud_stat_call);
40     toggleDebugButton = controlView.findViewById(R.id.button_toggle_debug);
41 
42     toggleDebugButton.setOnClickListener(new View.OnClickListener() {
43       @Override
44       public void onClick(View view) {
45         if (displayHud) {
46           statView.setVisibility(
47               statView.getVisibility() == View.VISIBLE ? View.INVISIBLE : View.VISIBLE);
48         }
49       }
50     });
51 
52     return controlView;
53   }
54 
55   @Override
onStart()56   public void onStart() {
57     super.onStart();
58 
59     Bundle args = getArguments();
60     if (args != null) {
61       displayHud = args.getBoolean(CallActivity.EXTRA_DISPLAY_HUD, false);
62     }
63     int visibility = displayHud ? View.VISIBLE : View.INVISIBLE;
64     statView.setVisibility(View.INVISIBLE);
65     toggleDebugButton.setVisibility(visibility);
66     isRunning = true;
67   }
68 
69   @Override
onStop()70   public void onStop() {
71     isRunning = false;
72     super.onStop();
73   }
74 
setCpuMonitor(CpuMonitor cpuMonitor)75   public void setCpuMonitor(CpuMonitor cpuMonitor) {
76     this.cpuMonitor = cpuMonitor;
77   }
78 
updateEncoderStatistics(final RTCStatsReport report)79   public void updateEncoderStatistics(final RTCStatsReport report) {
80     if (!isRunning || !displayHud) {
81       return;
82     }
83 
84     StringBuilder sb = new StringBuilder();
85 
86     if (cpuMonitor != null) {
87       sb.append("CPU%: ")
88           .append(cpuMonitor.getCpuUsageCurrent())
89           .append("/")
90           .append(cpuMonitor.getCpuUsageAverage())
91           .append(". Freq: ")
92           .append(cpuMonitor.getFrequencyScaleAverage())
93           .append("\n");
94     }
95 
96     for (RTCStats stat : report.getStatsMap().values()) {
97       sb.append(stat.toString()).append("\n");
98     }
99 
100     statView.setText(sb.toString());
101   }
102 }
103