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