Home | History | Annotate | Download | only in tvinput
      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.tvinput;
     18 
     19 import android.os.SystemClock;
     20 import android.util.Log;
     21 
     22 /** A class to maintain various debugging information. */
     23 public class TunerDebug {
     24     private static final String TAG = "TunerDebug";
     25     public static final boolean ENABLED = false;
     26 
     27     private int mVideoFrameDrop;
     28     private int mBytesInQueue;
     29 
     30     private long mAudioPositionUs;
     31     private long mAudioPtsUs;
     32     private long mVideoPtsUs;
     33 
     34     private long mLastAudioPositionUs;
     35     private long mLastAudioPtsUs;
     36     private long mLastVideoPtsUs;
     37     private long mLastCheckTimestampMs;
     38 
     39     private long mAudioPositionUsRate;
     40     private long mAudioPtsUsRate;
     41     private long mVideoPtsUsRate;
     42 
     43     private TunerDebug() {
     44         mVideoFrameDrop = 0;
     45         mLastCheckTimestampMs = SystemClock.elapsedRealtime();
     46     }
     47 
     48     private static class LazyHolder {
     49         private static final TunerDebug INSTANCE = new TunerDebug();
     50     }
     51 
     52     public static TunerDebug getInstance() {
     53         return LazyHolder.INSTANCE;
     54     }
     55 
     56     public static void notifyVideoFrameDrop(int count, long delta) {
     57         // TODO: provide timestamp mismatch information using delta
     58         TunerDebug sTunerDebug = getInstance();
     59         sTunerDebug.mVideoFrameDrop += count;
     60     }
     61 
     62     public static int getVideoFrameDrop() {
     63         TunerDebug sTunerDebug = getInstance();
     64         int videoFrameDrop = sTunerDebug.mVideoFrameDrop;
     65         if (videoFrameDrop > 0) {
     66             Log.d(TAG, "Dropped video frame: " + videoFrameDrop);
     67         }
     68         sTunerDebug.mVideoFrameDrop = 0;
     69         return videoFrameDrop;
     70     }
     71 
     72     public static void setBytesInQueue(int bytesInQueue) {
     73         TunerDebug sTunerDebug = getInstance();
     74         sTunerDebug.mBytesInQueue = bytesInQueue;
     75     }
     76 
     77     public static int getBytesInQueue() {
     78         TunerDebug sTunerDebug = getInstance();
     79         return sTunerDebug.mBytesInQueue;
     80     }
     81 
     82     public static void setAudioPositionUs(long audioPositionUs) {
     83         TunerDebug sTunerDebug = getInstance();
     84         sTunerDebug.mAudioPositionUs = audioPositionUs;
     85     }
     86 
     87     public static long getAudioPositionUs() {
     88         TunerDebug sTunerDebug = getInstance();
     89         return sTunerDebug.mAudioPositionUs;
     90     }
     91 
     92     public static void setAudioPtsUs(long audioPtsUs) {
     93         TunerDebug sTunerDebug = getInstance();
     94         sTunerDebug.mAudioPtsUs = audioPtsUs;
     95     }
     96 
     97     public static long getAudioPtsUs() {
     98         TunerDebug sTunerDebug = getInstance();
     99         return sTunerDebug.mAudioPtsUs;
    100     }
    101 
    102     public static void setVideoPtsUs(long videoPtsUs) {
    103         TunerDebug sTunerDebug = getInstance();
    104         sTunerDebug.mVideoPtsUs = videoPtsUs;
    105     }
    106 
    107     public static long getVideoPtsUs() {
    108         TunerDebug sTunerDebug = getInstance();
    109         return sTunerDebug.mVideoPtsUs;
    110     }
    111 
    112     public static void calculateDiff() {
    113         TunerDebug sTunerDebug = getInstance();
    114         long currentTime = SystemClock.elapsedRealtime();
    115         long duration = currentTime - sTunerDebug.mLastCheckTimestampMs;
    116         if (duration != 0) {
    117             sTunerDebug.mAudioPositionUsRate =
    118                     (sTunerDebug.mAudioPositionUs - sTunerDebug.mLastAudioPositionUs)
    119                             * 1000
    120                             / duration;
    121             sTunerDebug.mAudioPtsUsRate =
    122                     (sTunerDebug.mAudioPtsUs - sTunerDebug.mLastAudioPtsUs) * 1000 / duration;
    123             sTunerDebug.mVideoPtsUsRate =
    124                     (sTunerDebug.mVideoPtsUs - sTunerDebug.mLastVideoPtsUs) * 1000 / duration;
    125         }
    126 
    127         sTunerDebug.mLastAudioPositionUs = sTunerDebug.mAudioPositionUs;
    128         sTunerDebug.mLastAudioPtsUs = sTunerDebug.mAudioPtsUs;
    129         sTunerDebug.mLastVideoPtsUs = sTunerDebug.mVideoPtsUs;
    130         sTunerDebug.mLastCheckTimestampMs = currentTime;
    131     }
    132 
    133     public static long getAudioPositionUsRate() {
    134         TunerDebug sTunerDebug = getInstance();
    135         return sTunerDebug.mAudioPositionUsRate;
    136     }
    137 
    138     public static long getAudioPtsUsRate() {
    139         TunerDebug sTunerDebug = getInstance();
    140         return sTunerDebug.mAudioPtsUsRate;
    141     }
    142 
    143     public static long getVideoPtsUsRate() {
    144         TunerDebug sTunerDebug = getInstance();
    145         return sTunerDebug.mVideoPtsUsRate;
    146     }
    147 }
    148