1217ec0adfc35302a6cc6b04bc78bf8fd82ffc8a5Wei Jia/*
2217ec0adfc35302a6cc6b04bc78bf8fd82ffc8a5Wei Jia * Copyright 2015 The Android Open Source Project
3217ec0adfc35302a6cc6b04bc78bf8fd82ffc8a5Wei Jia *
4217ec0adfc35302a6cc6b04bc78bf8fd82ffc8a5Wei Jia * Licensed under the Apache License, Version 2.0 (the "License");
5217ec0adfc35302a6cc6b04bc78bf8fd82ffc8a5Wei Jia * you may not use this file except in compliance with the License.
6217ec0adfc35302a6cc6b04bc78bf8fd82ffc8a5Wei Jia * You may obtain a copy of the License at
7217ec0adfc35302a6cc6b04bc78bf8fd82ffc8a5Wei Jia *
8217ec0adfc35302a6cc6b04bc78bf8fd82ffc8a5Wei Jia *      http://www.apache.org/licenses/LICENSE-2.0
9217ec0adfc35302a6cc6b04bc78bf8fd82ffc8a5Wei Jia *
10217ec0adfc35302a6cc6b04bc78bf8fd82ffc8a5Wei Jia * Unless required by applicable law or agreed to in writing, software
11217ec0adfc35302a6cc6b04bc78bf8fd82ffc8a5Wei Jia * distributed under the License is distributed on an "AS IS" BASIS,
12217ec0adfc35302a6cc6b04bc78bf8fd82ffc8a5Wei Jia * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13217ec0adfc35302a6cc6b04bc78bf8fd82ffc8a5Wei Jia * See the License for the specific language governing permissions and
14217ec0adfc35302a6cc6b04bc78bf8fd82ffc8a5Wei Jia * limitations under the License.
15217ec0adfc35302a6cc6b04bc78bf8fd82ffc8a5Wei Jia */
16217ec0adfc35302a6cc6b04bc78bf8fd82ffc8a5Wei Jia
17217ec0adfc35302a6cc6b04bc78bf8fd82ffc8a5Wei Jiapackage android.media;
18217ec0adfc35302a6cc6b04bc78bf8fd82ffc8a5Wei Jia
19217ec0adfc35302a6cc6b04bc78bf8fd82ffc8a5Wei Jia/**
207f08763f4140822c30d0e18d4d3939488c8d26f8Lajos Molnar * An immutable object that represents the linear correlation between the media time
217f08763f4140822c30d0e18d4d3939488c8d26f8Lajos Molnar * and the system time. It contains the media clock rate, together with the media timestamp
22217ec0adfc35302a6cc6b04bc78bf8fd82ffc8a5Wei Jia * of an anchor frame and the system time when that frame was presented or is committed
23217ec0adfc35302a6cc6b04bc78bf8fd82ffc8a5Wei Jia * to be presented.
247f08763f4140822c30d0e18d4d3939488c8d26f8Lajos Molnar * <p>
257f08763f4140822c30d0e18d4d3939488c8d26f8Lajos Molnar * The phrase "present" means that audio/video produced on device is detectable by an external
26217ec0adfc35302a6cc6b04bc78bf8fd82ffc8a5Wei Jia * observer off device.
27217ec0adfc35302a6cc6b04bc78bf8fd82ffc8a5Wei Jia * The time is based on the implementation's best effort, using whatever knowledge
28217ec0adfc35302a6cc6b04bc78bf8fd82ffc8a5Wei Jia * is available to the system, but cannot account for any delay unknown to the implementation.
297f08763f4140822c30d0e18d4d3939488c8d26f8Lajos Molnar * The anchor frame could be any frame, including a just-rendered frame, or even a theoretical
307f08763f4140822c30d0e18d4d3939488c8d26f8Lajos Molnar * or in-between frame, based on the source of the MediaTimestamp.
317f08763f4140822c30d0e18d4d3939488c8d26f8Lajos Molnar * When the anchor frame is a just-rendered one, the media time stands for
327f08763f4140822c30d0e18d4d3939488c8d26f8Lajos Molnar * current position of the playback or recording.
33217ec0adfc35302a6cc6b04bc78bf8fd82ffc8a5Wei Jia *
34217ec0adfc35302a6cc6b04bc78bf8fd82ffc8a5Wei Jia * @see MediaSync#getTimestamp
357f08763f4140822c30d0e18d4d3939488c8d26f8Lajos Molnar * @see MediaPlayer#getTimestamp
36217ec0adfc35302a6cc6b04bc78bf8fd82ffc8a5Wei Jia */
37217ec0adfc35302a6cc6b04bc78bf8fd82ffc8a5Wei Jiapublic final class MediaTimestamp
38217ec0adfc35302a6cc6b04bc78bf8fd82ffc8a5Wei Jia{
39217ec0adfc35302a6cc6b04bc78bf8fd82ffc8a5Wei Jia    /**
40aabf284cbbf4ee9f3110f8d229810aafc39c2555Jean-Michel Trivi     * An unknown media timestamp value
41aabf284cbbf4ee9f3110f8d229810aafc39c2555Jean-Michel Trivi     */
42aabf284cbbf4ee9f3110f8d229810aafc39c2555Jean-Michel Trivi    public static final MediaTimestamp TIMESTAMP_UNKNOWN = new MediaTimestamp(-1, -1, 0.0f);
43aabf284cbbf4ee9f3110f8d229810aafc39c2555Jean-Michel Trivi
44aabf284cbbf4ee9f3110f8d229810aafc39c2555Jean-Michel Trivi    /**
4505a822161fe0296b01f573192adf306c0ce38c9cLajos Molnar     * Get the media time of the anchor in microseconds.
46217ec0adfc35302a6cc6b04bc78bf8fd82ffc8a5Wei Jia     */
4705a822161fe0296b01f573192adf306c0ce38c9cLajos Molnar    public long getAnchorMediaTimeUs() {
4805a822161fe0296b01f573192adf306c0ce38c9cLajos Molnar        return mediaTimeUs;
4905a822161fe0296b01f573192adf306c0ce38c9cLajos Molnar    }
50217ec0adfc35302a6cc6b04bc78bf8fd82ffc8a5Wei Jia
51217ec0adfc35302a6cc6b04bc78bf8fd82ffc8a5Wei Jia    /**
5205a822161fe0296b01f573192adf306c0ce38c9cLajos Molnar     * Get the {@link java.lang.System#nanoTime system time} corresponding to the media time
537f08763f4140822c30d0e18d4d3939488c8d26f8Lajos Molnar     * in nanoseconds.
54217ec0adfc35302a6cc6b04bc78bf8fd82ffc8a5Wei Jia     */
5505a822161fe0296b01f573192adf306c0ce38c9cLajos Molnar    public long getAnchorSytemNanoTime() {
5605a822161fe0296b01f573192adf306c0ce38c9cLajos Molnar        return nanoTime;
5705a822161fe0296b01f573192adf306c0ce38c9cLajos Molnar    }
58217ec0adfc35302a6cc6b04bc78bf8fd82ffc8a5Wei Jia
59217ec0adfc35302a6cc6b04bc78bf8fd82ffc8a5Wei Jia    /**
6005a822161fe0296b01f573192adf306c0ce38c9cLajos Molnar     * Get the rate of the media clock in relation to the system time.
6105a822161fe0296b01f573192adf306c0ce38c9cLajos Molnar     * <p>
627f08763f4140822c30d0e18d4d3939488c8d26f8Lajos Molnar     * It is 1.0 if media clock advances in sync with the system clock;
63217ec0adfc35302a6cc6b04bc78bf8fd82ffc8a5Wei Jia     * greater than 1.0 if media clock is faster than the system clock;
64217ec0adfc35302a6cc6b04bc78bf8fd82ffc8a5Wei Jia     * less than 1.0 if media clock is slower than the system clock.
65217ec0adfc35302a6cc6b04bc78bf8fd82ffc8a5Wei Jia     */
6605a822161fe0296b01f573192adf306c0ce38c9cLajos Molnar    public float getMediaClockRate() {
6705a822161fe0296b01f573192adf306c0ce38c9cLajos Molnar        return clockRate;
6805a822161fe0296b01f573192adf306c0ce38c9cLajos Molnar    }
6905a822161fe0296b01f573192adf306c0ce38c9cLajos Molnar
7005a822161fe0296b01f573192adf306c0ce38c9cLajos Molnar    /** @hide - accessor shorthand */
7105a822161fe0296b01f573192adf306c0ce38c9cLajos Molnar    public final long mediaTimeUs;
7205a822161fe0296b01f573192adf306c0ce38c9cLajos Molnar    /** @hide - accessor shorthand */
7305a822161fe0296b01f573192adf306c0ce38c9cLajos Molnar    public final long nanoTime;
7405a822161fe0296b01f573192adf306c0ce38c9cLajos Molnar    /** @hide - accessor shorthand */
757f08763f4140822c30d0e18d4d3939488c8d26f8Lajos Molnar    public final float clockRate;
767f08763f4140822c30d0e18d4d3939488c8d26f8Lajos Molnar
777f08763f4140822c30d0e18d4d3939488c8d26f8Lajos Molnar    /** @hide */
787f08763f4140822c30d0e18d4d3939488c8d26f8Lajos Molnar    MediaTimestamp(long mediaUs, long systemNs, float rate) {
797f08763f4140822c30d0e18d4d3939488c8d26f8Lajos Molnar        mediaTimeUs = mediaUs;
807f08763f4140822c30d0e18d4d3939488c8d26f8Lajos Molnar        nanoTime = systemNs;
817f08763f4140822c30d0e18d4d3939488c8d26f8Lajos Molnar        clockRate = rate;
827f08763f4140822c30d0e18d4d3939488c8d26f8Lajos Molnar    }
837f08763f4140822c30d0e18d4d3939488c8d26f8Lajos Molnar
847f08763f4140822c30d0e18d4d3939488c8d26f8Lajos Molnar    /** @hide */
857f08763f4140822c30d0e18d4d3939488c8d26f8Lajos Molnar    MediaTimestamp() {
867f08763f4140822c30d0e18d4d3939488c8d26f8Lajos Molnar        mediaTimeUs = 0;
877f08763f4140822c30d0e18d4d3939488c8d26f8Lajos Molnar        nanoTime = 0;
887f08763f4140822c30d0e18d4d3939488c8d26f8Lajos Molnar        clockRate = 1.0f;
897f08763f4140822c30d0e18d4d3939488c8d26f8Lajos Molnar    }
90aabf284cbbf4ee9f3110f8d229810aafc39c2555Jean-Michel Trivi
91aabf284cbbf4ee9f3110f8d229810aafc39c2555Jean-Michel Trivi    @Override
92aabf284cbbf4ee9f3110f8d229810aafc39c2555Jean-Michel Trivi    public boolean equals(Object obj) {
93aabf284cbbf4ee9f3110f8d229810aafc39c2555Jean-Michel Trivi        if (this == obj) return true;
94aabf284cbbf4ee9f3110f8d229810aafc39c2555Jean-Michel Trivi        if (obj == null || getClass() != obj.getClass()) return false;
95aabf284cbbf4ee9f3110f8d229810aafc39c2555Jean-Michel Trivi
96aabf284cbbf4ee9f3110f8d229810aafc39c2555Jean-Michel Trivi        final MediaTimestamp that = (MediaTimestamp) obj;
97aabf284cbbf4ee9f3110f8d229810aafc39c2555Jean-Michel Trivi        return (this.mediaTimeUs == that.mediaTimeUs)
98aabf284cbbf4ee9f3110f8d229810aafc39c2555Jean-Michel Trivi                && (this.nanoTime == that.nanoTime)
99aabf284cbbf4ee9f3110f8d229810aafc39c2555Jean-Michel Trivi                && (this.clockRate == that.clockRate);
100aabf284cbbf4ee9f3110f8d229810aafc39c2555Jean-Michel Trivi    }
1012fab5bfb7b1ee6b3b5328d6fd5e0013a2c70d85fDongwon Kang
1022fab5bfb7b1ee6b3b5328d6fd5e0013a2c70d85fDongwon Kang    @Override
1032fab5bfb7b1ee6b3b5328d6fd5e0013a2c70d85fDongwon Kang    public String toString() {
1042fab5bfb7b1ee6b3b5328d6fd5e0013a2c70d85fDongwon Kang        return getClass().getName()
1052fab5bfb7b1ee6b3b5328d6fd5e0013a2c70d85fDongwon Kang                + "{AnchorMediaTimeUs=" + mediaTimeUs
1062fab5bfb7b1ee6b3b5328d6fd5e0013a2c70d85fDongwon Kang                + " AnchorSystemNanoTime=" + nanoTime
1072fab5bfb7b1ee6b3b5328d6fd5e0013a2c70d85fDongwon Kang                + " clockRate=" + clockRate
1082fab5bfb7b1ee6b3b5328d6fd5e0013a2c70d85fDongwon Kang                + "}";
1092fab5bfb7b1ee6b3b5328d6fd5e0013a2c70d85fDongwon Kang    }
110217ec0adfc35302a6cc6b04bc78bf8fd82ffc8a5Wei Jia}
111