• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.exoplayer.ac3;
18 
19 import android.os.SystemClock;
20 
21 /**
22  * Copy of {@link com.google.android.exoplayer.MediaClock}.
23  * <p>
24  * A simple clock for tracking the progression of media time. The clock can be started, stopped and
25  * its time can be set and retrieved. When started, this clock is based on
26  * {@link SystemClock#elapsedRealtime()}.
27  */
28 /* package */ class AudioClock {
29     private boolean mStarted;
30 
31     /**
32      * The media time when the clock was last set or stopped.
33      */
34     private long mPositionUs;
35 
36     /**
37      * The difference between {@link SystemClock#elapsedRealtime()} and {@link #mPositionUs}
38      * when the clock was last set or mStarted.
39      */
40     private long mDeltaUs;
41 
42     /**
43      * Starts the clock. Does nothing if the clock is already started.
44      */
start()45     public void start() {
46         if (!mStarted) {
47             mStarted = true;
48             mDeltaUs = elapsedRealtimeMinus(mPositionUs);
49         }
50     }
51 
52     /**
53      * Stops the clock. Does nothing if the clock is already stopped.
54      */
stop()55     public void stop() {
56         if (mStarted) {
57             mPositionUs = elapsedRealtimeMinus(mDeltaUs);
58             mStarted = false;
59         }
60     }
61 
62     /**
63      * @param timeUs The position to set in microseconds.
64      */
setPositionUs(long timeUs)65     public void setPositionUs(long timeUs) {
66         this.mPositionUs = timeUs;
67         mDeltaUs = elapsedRealtimeMinus(timeUs);
68     }
69 
70     /**
71      * @return The current position in microseconds.
72      */
getPositionUs()73     public long getPositionUs() {
74         return mStarted ? elapsedRealtimeMinus(mDeltaUs) : mPositionUs;
75     }
76 
elapsedRealtimeMinus(long toSubtractUs)77     private long elapsedRealtimeMinus(long toSubtractUs) {
78         return SystemClock.elapsedRealtime() * 1000 - toSubtractUs;
79     }
80 }
81