| OLD | NEW |
| (Empty) |
| 1 /* | |
| 2 * libjingle | |
| 3 * Copyright 2015 Google Inc. | |
| 4 * | |
| 5 * Redistribution and use in source and binary forms, with or without | |
| 6 * modification, are permitted provided that the following conditions are met: | |
| 7 * | |
| 8 * 1. Redistributions of source code must retain the above copyright notice, | |
| 9 * this list of conditions and the following disclaimer. | |
| 10 * 2. Redistributions in binary form must reproduce the above copyright notice, | |
| 11 * this list of conditions and the following disclaimer in the documentation | |
| 12 * and/or other materials provided with the distribution. | |
| 13 * 3. The name of the author may not be used to endorse or promote products | |
| 14 * derived from this software without specific prior written permission. | |
| 15 * | |
| 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED | |
| 17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF | |
| 18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO | |
| 19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
| 20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | |
| 21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; | |
| 22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, | |
| 23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR | |
| 24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF | |
| 25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
| 26 */ | |
| 27 | |
| 28 package org.appspot.apprtc; | |
| 29 | |
| 30 import android.app.Fragment; | |
| 31 import android.os.Bundle; | |
| 32 import android.util.TypedValue; | |
| 33 import android.view.LayoutInflater; | |
| 34 import android.view.View; | |
| 35 import android.view.ViewGroup; | |
| 36 import android.widget.ImageButton; | |
| 37 import android.widget.TextView; | |
| 38 | |
| 39 import org.webrtc.StatsReport; | |
| 40 | |
| 41 import java.util.HashMap; | |
| 42 import java.util.Map; | |
| 43 | |
| 44 /** | |
| 45 * Fragment for HUD statistics display. | |
| 46 */ | |
| 47 public class HudFragment extends Fragment { | |
| 48 private View controlView; | |
| 49 private TextView encoderStatView; | |
| 50 private TextView hudViewBwe; | |
| 51 private TextView hudViewConnection; | |
| 52 private TextView hudViewVideoSend; | |
| 53 private TextView hudViewVideoRecv; | |
| 54 private ImageButton toggleDebugButton; | |
| 55 private boolean videoCallEnabled; | |
| 56 private boolean displayHud; | |
| 57 private volatile boolean isRunning; | |
| 58 private final CpuMonitor cpuMonitor = new CpuMonitor(); | |
| 59 | |
| 60 @Override | |
| 61 public View onCreateView(LayoutInflater inflater, ViewGroup container, | |
| 62 Bundle savedInstanceState) { | |
| 63 controlView = inflater.inflate(R.layout.fragment_hud, container, false); | |
| 64 | |
| 65 // Create UI controls. | |
| 66 encoderStatView = (TextView) controlView.findViewById(R.id.encoder_stat_call
); | |
| 67 hudViewBwe = (TextView) controlView.findViewById(R.id.hud_stat_bwe); | |
| 68 hudViewConnection = (TextView) controlView.findViewById(R.id.hud_stat_connec
tion); | |
| 69 hudViewVideoSend = (TextView) controlView.findViewById(R.id.hud_stat_video_s
end); | |
| 70 hudViewVideoRecv = (TextView) controlView.findViewById(R.id.hud_stat_video_r
ecv); | |
| 71 toggleDebugButton = (ImageButton) controlView.findViewById(R.id.button_toggl
e_debug); | |
| 72 | |
| 73 toggleDebugButton.setOnClickListener(new View.OnClickListener() { | |
| 74 @Override | |
| 75 public void onClick(View view) { | |
| 76 if (displayHud) { | |
| 77 int visibility = (hudViewBwe.getVisibility() == View.VISIBLE) | |
| 78 ? View.INVISIBLE : View.VISIBLE; | |
| 79 hudViewsSetProperties(visibility); | |
| 80 } | |
| 81 } | |
| 82 }); | |
| 83 | |
| 84 return controlView; | |
| 85 } | |
| 86 | |
| 87 @Override | |
| 88 public void onStart() { | |
| 89 super.onStart(); | |
| 90 | |
| 91 Bundle args = getArguments(); | |
| 92 if (args != null) { | |
| 93 videoCallEnabled = args.getBoolean(CallActivity.EXTRA_VIDEO_CALL, true); | |
| 94 displayHud = args.getBoolean(CallActivity.EXTRA_DISPLAY_HUD, false); | |
| 95 } | |
| 96 int visibility = displayHud ? View.VISIBLE : View.INVISIBLE; | |
| 97 encoderStatView.setVisibility(visibility); | |
| 98 toggleDebugButton.setVisibility(visibility); | |
| 99 hudViewsSetProperties(View.INVISIBLE); | |
| 100 isRunning = true; | |
| 101 } | |
| 102 | |
| 103 @Override | |
| 104 public void onStop() { | |
| 105 isRunning = false; | |
| 106 super.onStop(); | |
| 107 } | |
| 108 | |
| 109 private void hudViewsSetProperties(int visibility) { | |
| 110 hudViewBwe.setVisibility(visibility); | |
| 111 hudViewConnection.setVisibility(visibility); | |
| 112 hudViewVideoSend.setVisibility(visibility); | |
| 113 hudViewVideoRecv.setVisibility(visibility); | |
| 114 hudViewBwe.setTextSize(TypedValue.COMPLEX_UNIT_PT, 5); | |
| 115 hudViewConnection.setTextSize(TypedValue.COMPLEX_UNIT_PT, 5); | |
| 116 hudViewVideoSend.setTextSize(TypedValue.COMPLEX_UNIT_PT, 5); | |
| 117 hudViewVideoRecv.setTextSize(TypedValue.COMPLEX_UNIT_PT, 5); | |
| 118 } | |
| 119 | |
| 120 private Map<String, String> getReportMap(StatsReport report) { | |
| 121 Map<String, String> reportMap = new HashMap<String, String>(); | |
| 122 for (StatsReport.Value value : report.values) { | |
| 123 reportMap.put(value.name, value.value); | |
| 124 } | |
| 125 return reportMap; | |
| 126 } | |
| 127 | |
| 128 public void updateEncoderStatistics(final StatsReport[] reports) { | |
| 129 if (!isRunning || !displayHud) { | |
| 130 return; | |
| 131 } | |
| 132 StringBuilder encoderStat = new StringBuilder(128); | |
| 133 StringBuilder bweStat = new StringBuilder(); | |
| 134 StringBuilder connectionStat = new StringBuilder(); | |
| 135 StringBuilder videoSendStat = new StringBuilder(); | |
| 136 StringBuilder videoRecvStat = new StringBuilder(); | |
| 137 String fps = null; | |
| 138 String targetBitrate = null; | |
| 139 String actualBitrate = null; | |
| 140 | |
| 141 for (StatsReport report : reports) { | |
| 142 if (report.type.equals("ssrc") && report.id.contains("ssrc") | |
| 143 && report.id.contains("send")) { | |
| 144 // Send video statistics. | |
| 145 Map<String, String> reportMap = getReportMap(report); | |
| 146 String trackId = reportMap.get("googTrackId"); | |
| 147 if (trackId != null && trackId.contains(PeerConnectionClient.VIDEO_TRACK
_ID)) { | |
| 148 fps = reportMap.get("googFrameRateSent"); | |
| 149 videoSendStat.append(report.id).append("\n"); | |
| 150 for (StatsReport.Value value : report.values) { | |
| 151 String name = value.name.replace("goog", ""); | |
| 152 videoSendStat.append(name).append("=").append(value.value).append("\
n"); | |
| 153 } | |
| 154 } | |
| 155 } else if (report.type.equals("ssrc") && report.id.contains("ssrc") | |
| 156 && report.id.contains("recv")) { | |
| 157 // Receive video statistics. | |
| 158 Map<String, String> reportMap = getReportMap(report); | |
| 159 // Check if this stat is for video track. | |
| 160 String frameWidth = reportMap.get("googFrameWidthReceived"); | |
| 161 if (frameWidth != null) { | |
| 162 videoRecvStat.append(report.id).append("\n"); | |
| 163 for (StatsReport.Value value : report.values) { | |
| 164 String name = value.name.replace("goog", ""); | |
| 165 videoRecvStat.append(name).append("=").append(value.value).append("\
n"); | |
| 166 } | |
| 167 } | |
| 168 } else if (report.id.equals("bweforvideo")) { | |
| 169 // BWE statistics. | |
| 170 Map<String, String> reportMap = getReportMap(report); | |
| 171 targetBitrate = reportMap.get("googTargetEncBitrate"); | |
| 172 actualBitrate = reportMap.get("googActualEncBitrate"); | |
| 173 | |
| 174 bweStat.append(report.id).append("\n"); | |
| 175 for (StatsReport.Value value : report.values) { | |
| 176 String name = value.name.replace("goog", "").replace("Available", ""); | |
| 177 bweStat.append(name).append("=").append(value.value).append("\n"); | |
| 178 } | |
| 179 } else if (report.type.equals("googCandidatePair")) { | |
| 180 // Connection statistics. | |
| 181 Map<String, String> reportMap = getReportMap(report); | |
| 182 String activeConnection = reportMap.get("googActiveConnection"); | |
| 183 if (activeConnection != null && activeConnection.equals("true")) { | |
| 184 connectionStat.append(report.id).append("\n"); | |
| 185 for (StatsReport.Value value : report.values) { | |
| 186 String name = value.name.replace("goog", ""); | |
| 187 connectionStat.append(name).append("=").append(value.value).append("
\n"); | |
| 188 } | |
| 189 } | |
| 190 } | |
| 191 } | |
| 192 hudViewBwe.setText(bweStat.toString()); | |
| 193 hudViewConnection.setText(connectionStat.toString()); | |
| 194 hudViewVideoSend.setText(videoSendStat.toString()); | |
| 195 hudViewVideoRecv.setText(videoRecvStat.toString()); | |
| 196 | |
| 197 if (videoCallEnabled) { | |
| 198 if (fps != null) { | |
| 199 encoderStat.append("Fps: ").append(fps).append("\n"); | |
| 200 } | |
| 201 if (targetBitrate != null) { | |
| 202 encoderStat.append("Target BR: ").append(targetBitrate).append("\n"); | |
| 203 } | |
| 204 if (actualBitrate != null) { | |
| 205 encoderStat.append("Actual BR: ").append(actualBitrate).append("\n"); | |
| 206 } | |
| 207 } | |
| 208 | |
| 209 if (cpuMonitor.sampleCpuUtilization()) { | |
| 210 encoderStat.append("CPU%: ") | |
| 211 .append(cpuMonitor.getCpuCurrent()).append("/") | |
| 212 .append(cpuMonitor.getCpuAvg3()).append("/") | |
| 213 .append(cpuMonitor.getCpuAvgAll()); | |
| 214 } | |
| 215 encoderStatView.setText(encoderStat.toString()); | |
| 216 } | |
| 217 } | |
| OLD | NEW |