Home | History | Annotate | Download | only in util
      1 /*
      2  * Copyright (C) 2015 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.android.tv.tuner.util;
     18 
     19 import java.util.Locale;
     20 
     21 /** Utility class for tuner status messages. */
     22 public class StatusTextUtils {
     23     private static final int PACKETS_PER_SEC_YELLOW = 1500;
     24     private static final int PACKETS_PER_SEC_RED = 1000;
     25     private static final int AUDIO_POSITION_MS_RATE_DIFF_YELLOW = 100;
     26     private static final int AUDIO_POSITION_MS_RATE_DIFF_RED = 200;
     27     private static final String COLOR_RED = "red";
     28     private static final String COLOR_YELLOW = "yellow";
     29     private static final String COLOR_GREEN = "green";
     30     private static final String COLOR_GRAY = "gray";
     31 
     32     private StatusTextUtils() {}
     33 
     34     /**
     35      * Returns tuner status warning message in HTML.
     36      *
     37      * <p>This is only called for debuging and always shown in english.
     38      */
     39     public static String getStatusWarningInHTML(
     40             long packetsPerSec,
     41             int videoFrameDrop,
     42             int bytesInQueue,
     43             long audioPositionUs,
     44             long audioPositionUsRate,
     45             long audioPtsUs,
     46             long audioPtsUsRate,
     47             long videoPtsUs,
     48             long videoPtsUsRate) {
     49         StringBuffer buffer = new StringBuffer();
     50 
     51         // audioPosition should go in rate of 1000ms.
     52         long audioPositionMsRate = audioPositionUsRate / 1000;
     53         String audioPositionColor;
     54         if (Math.abs(audioPositionMsRate - 1000) > AUDIO_POSITION_MS_RATE_DIFF_RED) {
     55             audioPositionColor = COLOR_RED;
     56         } else if (Math.abs(audioPositionMsRate - 1000) > AUDIO_POSITION_MS_RATE_DIFF_YELLOW) {
     57             audioPositionColor = COLOR_YELLOW;
     58         } else {
     59             audioPositionColor = COLOR_GRAY;
     60         }
     61         buffer.append(String.format(Locale.US, "<font color=%s>", audioPositionColor));
     62         buffer.append(
     63                 String.format(
     64                         Locale.US,
     65                         "audioPositionMs: %d (%d)<br>",
     66                         audioPositionUs / 1000,
     67                         audioPositionMsRate));
     68         buffer.append("</font>\n");
     69         buffer.append("<font color=" + COLOR_GRAY + ">");
     70         buffer.append(
     71                 String.format(
     72                         Locale.US,
     73                         "audioPtsMs: %d (%d, %d)<br>",
     74                         audioPtsUs / 1000,
     75                         audioPtsUsRate / 1000,
     76                         (audioPtsUs - audioPositionUs) / 1000));
     77         buffer.append(
     78                 String.format(
     79                         Locale.US,
     80                         "videoPtsMs: %d (%d, %d)<br>",
     81                         videoPtsUs / 1000,
     82                         videoPtsUsRate / 1000,
     83                         (videoPtsUs - audioPositionUs) / 1000));
     84         buffer.append("</font>\n");
     85 
     86         appendStatusLine(buffer, "KbytesInQueue", bytesInQueue / 1000, 1, 10);
     87         buffer.append("<br/>");
     88         appendErrorStatusLine(buffer, "videoFrameDrop", videoFrameDrop, 0, 2);
     89         buffer.append("<br/>");
     90         appendStatusLine(
     91                 buffer,
     92                 "packetsPerSec",
     93                 packetsPerSec,
     94                 PACKETS_PER_SEC_RED,
     95                 PACKETS_PER_SEC_YELLOW);
     96         return buffer.toString();
     97     }
     98 
     99     /** Returns audio unavailable warning message in HTML. */
    100     public static String getAudioWarningInHTML(String msg) {
    101         return String.format("<font color=%s>%s</font>\n", COLOR_YELLOW, msg);
    102     }
    103 
    104     private static void appendStatusLine(
    105             StringBuffer buffer, String factorName, long value, int minRed, int minYellow) {
    106         buffer.append("<font color=");
    107         if (value <= minRed) {
    108             buffer.append(COLOR_RED);
    109         } else if (value <= minYellow) {
    110             buffer.append(COLOR_YELLOW);
    111         } else {
    112             buffer.append(COLOR_GREEN);
    113         }
    114         buffer.append(">");
    115         buffer.append(factorName);
    116         buffer.append(" : ");
    117         buffer.append(value);
    118         buffer.append("</font>");
    119     }
    120 
    121     private static void appendErrorStatusLine(
    122             StringBuffer buffer, String factorName, int value, int minGreen, int minYellow) {
    123         buffer.append("<font color=");
    124         if (value <= minGreen) {
    125             buffer.append(COLOR_GREEN);
    126         } else if (value <= minYellow) {
    127             buffer.append(COLOR_YELLOW);
    128         } else {
    129             buffer.append(COLOR_RED);
    130         }
    131         buffer.append(">");
    132         buffer.append(factorName);
    133         buffer.append(" : ");
    134         buffer.append(value);
    135         buffer.append("</font>");
    136     }
    137 }
    138