Class TimestampAdjuster
- java.lang.Object
-
- com.google.android.exoplayer2.util.TimestampAdjuster
-
public final class TimestampAdjuster extends Object
Adjusts and offsets sample timestamps. MPEG-2 TS timestamps scaling and adjustment is supported, taking into account timestamp rollover.
-
-
Field Summary
Fields Modifier and Type Field Description static longMODE_NO_OFFSETA specialfirstSampleTimestampUsvalue indicating that presentation timestamps should not be offset.static longMODE_SHAREDA specialfirstSampleTimestampUsvalue indicating that the adjuster will be shared by multiple threads.
-
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, orC.TIME_UNSETif timestamps will not be offset or if the adjuster is in shared mode.longgetLastAdjustedTimestampUs()Returns the last adjusted timestamp, in microseconds.longgetTimestampOffsetUs()Returns the offset between the input ofadjustSampleTimestamp(long)and its output, orC.TIME_UNSETif the offset has not yet been determined.static longptsToUs(long pts)Converts a 90 kHz clock timestamp to a timestamp in microseconds.voidreset(long firstSampleTimestampUs)Resets the instance.voidsharedInitializeOrWait(boolean canInitialize, long nextSampleTimestampUs)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
-
MODE_NO_OFFSET
public static final long MODE_NO_OFFSET
A specialfirstSampleTimestampUsvalue indicating that presentation timestamps should not be offset. In this mode:getFirstSampleTimestampUs()will always returnC.TIME_UNSET.- The only timestamp adjustment performed is to account for MPEG-2 TS timestamp rollover.
- See Also:
- Constant Field Values
-
MODE_SHARED
public static final long MODE_SHARED
A specialfirstSampleTimestampUsvalue indicating that the adjuster will be shared by multiple threads. In this mode:getFirstSampleTimestampUs()will always returnC.TIME_UNSET.- Calling threads must call
sharedInitializeOrWait(boolean, long)prior to adjusting timestamps.
- 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, orMODE_NO_OFFSETif timestamps should not be offset, orMODE_SHAREDif the adjuster will be used in shared mode.
-
-
Method Detail
-
sharedInitializeOrWait
public void sharedInitializeOrWait(boolean canInitialize, long nextSampleTimestampUs) throws InterruptedExceptionFor shared timestamp adjusters, performs necessary initialization actions for a caller.- If the adjuster has already established a
timestamp offsetthen this method is a no-op. - If
canInitializeistrueand the adjuster has not yet established a timestamp offset, then the adjuster records the desired first sample timestamp for the calling thread and returns to allow the caller to proceed. If the timestamp offset has still not been established when the caller attempts to adjust its first timestamp, then the recorded timestamp is used to set it. - If
canInitializeisfalseand the adjuster has not yet established a timestamp offset, then the call blocks until the timestamp offset is set.
- Parameters:
canInitialize- Whether the caller is able to initialize the adjuster, if needed.nextSampleTimestampUs- The desired timestamp for the next sample loaded by the calling thread, in microseconds. Only used ifcanInitializeistrue.- Throws:
InterruptedException- If the thread is interrupted whilst blocked waiting for initialization to complete.
- If the adjuster has already established a
-
getFirstSampleTimestampUs
public long getFirstSampleTimestampUs()
Returns the value of the first adjusted sample timestamp in microseconds, orC.TIME_UNSETif timestamps will not be offset or if the adjuster is in shared mode.
-
getLastAdjustedTimestampUs
public long getLastAdjustedTimestampUs()
Returns the last adjusted timestamp, in microseconds. If no timestamps have been adjusted yet then the result ofgetFirstSampleTimestampUs()is returned.
-
getTimestampOffsetUs
public long getTimestampOffsetUs()
Returns the offset between the input ofadjustSampleTimestamp(long)and its output, orC.TIME_UNSETif the offset has not yet been determined.
-
reset
public void reset(long firstSampleTimestampUs)
Resets the instance.- Parameters:
firstSampleTimestampUs- The desired value of the first adjusted sample timestamp after this reset in microseconds, orMODE_NO_OFFSETif timestamps should not be offset, orMODE_SHAREDif the adjuster will be used in shared mode.
-
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.
-
-