Class TimestampAdjuster
- java.lang.Object
-
- com.google.android.exoplayer2.util.TimestampAdjuster
-
public final class TimestampAdjuster extends Object
Offsets timestamps according to an initial sample timestamp offset. MPEG-2 TS timestamps scaling and adjustment is supported, taking into account timestamp rollover.
-
-
Field Summary
Fields Modifier and Type Field Description static longDO_NOT_OFFSETA specialfirstSampleTimestampUsvalue indicating that presentation timestamps should not be offset.
-
Constructor Summary
Constructors Constructor Description TimestampAdjuster(long firstSampleTimestampUs)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description longadjustSampleTimestamp(long timeUs)Offsets a timestamp in microseconds.longadjustTsTimestamp(long pts90Khz)Scales and offsets an MPEG-2 TS presentation timestamp considering wraparound.longgetFirstSampleTimestampUs()Returns the value of the first adjusted sample timestamp in microseconds, orDO_NOT_OFFSETif timestamps will not be offset.longgetLastAdjustedTimestampUs()Returns the last value obtained fromadjustSampleTimestamp(long).longgetTimestampOffsetUs()Returns the offset between the input ofadjustSampleTimestamp(long)and its output.static longptsToUs(long pts)Converts a 90 kHz clock timestamp to a timestamp in microseconds.voidreset(long firstSampleTimestampUs)Resets the instance to its initial state.voidsharedInitializeOrWait(boolean canInitialize, long startTimeUs)For shared timestamp adjusters, performs necessary initialization actions for a caller.static longusToNonWrappedPts(long us)Converts a timestamp in microseconds to a 90 kHz clock timestamp.static longusToWrappedPts(long us)Converts a timestamp in microseconds to a 90 kHz clock timestamp, performing wraparound to keep the result within 33-bits.
-
-
-
Field Detail
-
DO_NOT_OFFSET
public static final long DO_NOT_OFFSET
A specialfirstSampleTimestampUsvalue indicating that presentation timestamps should not be offset.- See Also:
- Constant Field Values
-
-
Constructor Detail
-
TimestampAdjuster
public TimestampAdjuster(long firstSampleTimestampUs)
- Parameters:
firstSampleTimestampUs- The desired value of the first adjusted sample timestamp in microseconds, orDO_NOT_OFFSETif timestamps should not be offset.
-
-
Method Detail
-
sharedInitializeOrWait
public void sharedInitializeOrWait(boolean canInitialize, long startTimeUs) throws InterruptedExceptionFor shared timestamp adjusters, performs necessary initialization actions for a caller.- If the adjuster does not yet have a target
first sample timestampand ifcanInitializeistrue, then initialization is started by setting the target first sample timestamp tofirstSampleTimestampUs. The call returns, allowing the caller to proceed. Initialization completes when a caller adjusts the first timestamp. - If
canInitializeistrueand the adjuster already has a targetfirst sample timestamp, then the call returns to allow the caller to proceed only iffirstSampleTimestampUsis equal to the target. This ensures a caller that's previously started initialization can continue to proceed. It also allows other callers with the samefirstSampleTimestampUsto proceed, since in this case it doesn't matter which caller adjusts the first timestamp to complete initialization. - If
canInitializeisfalseor iffirstSampleTimestampUsdiffers from the targetfirst sample timestamp, then the call blocks until initialization completes. If initialization has already been completed the call returns immediately.
- Parameters:
canInitialize- Whether the caller is able to initialize the adjuster, if needed.startTimeUs- The desired first sample timestamp of the caller, in microseconds. Only used ifcanInitializeistrue.- Throws:
InterruptedException- If the thread is interrupted whilst blocked waiting for initialization to complete.
- If the adjuster does not yet have a target
-
getFirstSampleTimestampUs
public long getFirstSampleTimestampUs()
Returns the value of the first adjusted sample timestamp in microseconds, orDO_NOT_OFFSETif timestamps will not be offset.
-
getLastAdjustedTimestampUs
public long getLastAdjustedTimestampUs()
Returns the last value obtained fromadjustSampleTimestamp(long). IfadjustSampleTimestamp(long)has not been called, returns the result of callinggetFirstSampleTimestampUs(). If this value isDO_NOT_OFFSET, returnsC.TIME_UNSET.
-
getTimestampOffsetUs
public long getTimestampOffsetUs()
Returns the offset between the input ofadjustSampleTimestamp(long)and its output. IfDO_NOT_OFFSETwas provided to the constructor, 0 is returned. If the timestamp adjuster is yet not initialized,C.TIME_UNSETis returned.- Returns:
- The offset between
adjustSampleTimestamp(long)'s input and output.C.TIME_UNSETif the adjuster is not yet initialized and 0 if timestamps should not be offset.
-
reset
public void reset(long firstSampleTimestampUs)
Resets the instance to its initial state.- Parameters:
firstSampleTimestampUs- The desired value of the first adjusted sample timestamp after this reset, in microseconds, orDO_NOT_OFFSETif timestamps should not be offset.
-
adjustTsTimestamp
public long adjustTsTimestamp(long pts90Khz)
Scales and offsets an MPEG-2 TS presentation timestamp considering wraparound.- Parameters:
pts90Khz- A 90 kHz clock MPEG-2 TS presentation timestamp.- Returns:
- The adjusted timestamp in microseconds.
-
adjustSampleTimestamp
public long adjustSampleTimestamp(long timeUs)
Offsets a timestamp in microseconds.- Parameters:
timeUs- The timestamp to adjust in microseconds.- Returns:
- The adjusted timestamp in microseconds.
-
ptsToUs
public static long ptsToUs(long pts)
Converts a 90 kHz clock timestamp to a timestamp in microseconds.- Parameters:
pts- A 90 kHz clock timestamp.- Returns:
- The corresponding value in microseconds.
-
usToWrappedPts
public static long usToWrappedPts(long us)
Converts a timestamp in microseconds to a 90 kHz clock timestamp, performing wraparound to keep the result within 33-bits.- Parameters:
us- A value in microseconds.- Returns:
- The corresponding value as a 90 kHz clock timestamp, wrapped to 33 bits.
-
usToNonWrappedPts
public static long usToNonWrappedPts(long us)
Converts a timestamp in microseconds to a 90 kHz clock timestamp.Does not perform any wraparound. To get a 90 kHz timestamp suitable for use with MPEG-TS, use
usToWrappedPts(long).- Parameters:
us- A value in microseconds.- Returns:
- The corresponding value as a 90 kHz clock timestamp.
-
-