1/*
2 * Copyright 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
17package android.media;
18
19/**
20 * An immutable object that represents the linear correlation between the media time
21 * and the system time. It contains the media clock rate, together with the media timestamp
22 * of an anchor frame and the system time when that frame was presented or is committed
23 * to be presented.
24 * <p>
25 * The phrase "present" means that audio/video produced on device is detectable by an external
26 * observer off device.
27 * The time is based on the implementation's best effort, using whatever knowledge
28 * is available to the system, but cannot account for any delay unknown to the implementation.
29 * The anchor frame could be any frame, including a just-rendered frame, or even a theoretical
30 * or in-between frame, based on the source of the MediaTimestamp.
31 * When the anchor frame is a just-rendered one, the media time stands for
32 * current position of the playback or recording.
33 *
34 * @see MediaSync#getTimestamp
35 * @see MediaPlayer#getTimestamp
36 */
37public final class MediaTimestamp
38{
39    /**
40     * An unknown media timestamp value
41     */
42    public static final MediaTimestamp TIMESTAMP_UNKNOWN = new MediaTimestamp(-1, -1, 0.0f);
43
44    /**
45     * Get the media time of the anchor in microseconds.
46     */
47    public long getAnchorMediaTimeUs() {
48        return mediaTimeUs;
49    }
50
51    /**
52     * Get the {@link java.lang.System#nanoTime system time} corresponding to the media time
53     * in nanoseconds.
54     */
55    public long getAnchorSytemNanoTime() {
56        return nanoTime;
57    }
58
59    /**
60     * Get the rate of the media clock in relation to the system time.
61     * <p>
62     * It is 1.0 if media clock advances in sync with the system clock;
63     * greater than 1.0 if media clock is faster than the system clock;
64     * less than 1.0 if media clock is slower than the system clock.
65     */
66    public float getMediaClockRate() {
67        return clockRate;
68    }
69
70    /** @hide - accessor shorthand */
71    public final long mediaTimeUs;
72    /** @hide - accessor shorthand */
73    public final long nanoTime;
74    /** @hide - accessor shorthand */
75    public final float clockRate;
76
77    /** @hide */
78    MediaTimestamp(long mediaUs, long systemNs, float rate) {
79        mediaTimeUs = mediaUs;
80        nanoTime = systemNs;
81        clockRate = rate;
82    }
83
84    /** @hide */
85    MediaTimestamp() {
86        mediaTimeUs = 0;
87        nanoTime = 0;
88        clockRate = 1.0f;
89    }
90
91    @Override
92    public boolean equals(Object obj) {
93        if (this == obj) return true;
94        if (obj == null || getClass() != obj.getClass()) return false;
95
96        final MediaTimestamp that = (MediaTimestamp) obj;
97        return (this.mediaTimeUs == that.mediaTimeUs)
98                && (this.nanoTime == that.nanoTime)
99                && (this.clockRate == that.clockRate);
100    }
101
102    @Override
103    public String toString() {
104        return getClass().getName()
105                + "{AnchorMediaTimeUs=" + mediaTimeUs
106                + " AnchorSystemNanoTime=" + nanoTime
107                + " clockRate=" + clockRate
108                + "}";
109    }
110}
111