TimestampAdjuster


@UnstableApi
public final class TimestampAdjuster


Adjusts and offsets sample timestamps. MPEG-2 TS timestamps scaling and adjustment is supported, taking into account timestamp rollover.

Summary

Constants

static final long
MODE_NO_OFFSET = 9223372036854775807

A special firstSampleTimestampUs value indicating that presentation timestamps should not be offset.

static final long
MODE_SHARED = 9223372036854775806

A special firstSampleTimestampUs value indicating that the adjuster will be shared by multiple threads.

Public fields

long
long
@GuardedBy(value = "this")
timestampOffsetUs

Public constructors

TimestampAdjuster(long firstSampleTimestampUs)

Public methods

synchronized long
adjustSampleTimestamp(long timeUs)

Offsets a timestamp in microseconds.

synchronized long
adjustTsTimestamp(long pts90Khz)

Scales and offsets an MPEG-2 TS presentation timestamp considering wraparound.

synchronized long

Scales and offsets an MPEG-2 TS presentation timestamp considering wraparound.

synchronized long

Returns the value of the first adjusted sample timestamp in microseconds, or TIME_UNSET if timestamps will not be offset or if the adjuster is in shared mode.

synchronized long

Returns the last adjusted timestamp, in microseconds.

synchronized long

Returns the offset between the input of adjustSampleTimestamp and its output, or TIME_UNSET if the offset has not yet been determined.

synchronized boolean

Returns whether the instance is initialized with a timestamp offset.

static long
ptsToUs(long pts)

Converts a 90 kHz clock timestamp to a timestamp in microseconds.

synchronized void
reset(long firstSampleTimestampUs)

Resets the instance.

synchronized void
sharedInitializeOrWait(
    boolean canInitialize,
    long nextSampleTimestampUs,
    long timeoutMs
)

For shared timestamp adjusters, performs necessary initialization actions for a caller.

static long

Converts a timestamp in microseconds to a 90 kHz clock timestamp.

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.

Constants

MODE_NO_OFFSET

public static final long MODE_NO_OFFSET = 9223372036854775807

A special firstSampleTimestampUs value indicating that presentation timestamps should not be offset. In this mode:

MODE_SHARED

public static final long MODE_SHARED = 9223372036854775806

A special firstSampleTimestampUs value indicating that the adjuster will be shared by multiple threads. In this mode:

Public fields

firstSampleTimestampUs

@GuardedBy(value = "this")
public long firstSampleTimestampUs

timestampOffsetUs

@GuardedBy(value = "this")
public long timestampOffsetUs

Public constructors

TimestampAdjuster

public TimestampAdjuster(long firstSampleTimestampUs)
Parameters
long firstSampleTimestampUs

The desired value of the first adjusted sample timestamp in microseconds, or MODE_NO_OFFSET if timestamps should not be offset, or MODE_SHARED if the adjuster will be used in shared mode.

Public methods

adjustSampleTimestamp

synchronized public long adjustSampleTimestamp(long timeUs)

Offsets a timestamp in microseconds.

Parameters
long timeUs

The timestamp to adjust in microseconds.

Returns
long

The adjusted timestamp in microseconds.

adjustTsTimestamp

synchronized public long adjustTsTimestamp(long pts90Khz)

Scales and offsets an MPEG-2 TS presentation timestamp considering wraparound.

When estimating the wraparound, the method assumes that this timestamp is close to the previous adjusted timestamp.

Parameters
long pts90Khz

A 90 kHz clock MPEG-2 TS presentation timestamp.

Returns
long

The adjusted timestamp in microseconds.

adjustTsTimestampGreaterThanPreviousTimestamp

synchronized public long adjustTsTimestampGreaterThanPreviousTimestamp(long pts90Khz)

Scales and offsets an MPEG-2 TS presentation timestamp considering wraparound.

When estimating the wraparound, the method assumes that the timestamp is strictly greater than the previous adjusted timestamp.

Parameters
long pts90Khz

A 90 kHz clock MPEG-2 TS presentation timestamp.

Returns
long

The adjusted timestamp in microseconds.

getFirstSampleTimestampUs

synchronized public long getFirstSampleTimestampUs()

Returns the value of the first adjusted sample timestamp in microseconds, or TIME_UNSET if timestamps will not be offset or if the adjuster is in shared mode.

getLastAdjustedTimestampUs

synchronized public long getLastAdjustedTimestampUs()

Returns the last adjusted timestamp, in microseconds. If no timestamps have been adjusted yet then the result of getFirstSampleTimestampUs is returned.

getTimestampOffsetUs

synchronized public long getTimestampOffsetUs()

Returns the offset between the input of adjustSampleTimestamp and its output, or TIME_UNSET if the offset has not yet been determined.

isInitialized

synchronized public boolean isInitialized()

Returns whether the instance is initialized with a timestamp offset.

ptsToUs

public static long ptsToUs(long pts)

Converts a 90 kHz clock timestamp to a timestamp in microseconds.

Parameters
long pts

A 90 kHz clock timestamp.

Returns
long

The corresponding value in microseconds.

reset

synchronized public void reset(long firstSampleTimestampUs)

Resets the instance.

Parameters
long firstSampleTimestampUs

The desired value of the first adjusted sample timestamp after this reset in microseconds, or MODE_NO_OFFSET if timestamps should not be offset, or MODE_SHARED if the adjuster will be used in shared mode.

sharedInitializeOrWait

synchronized public void sharedInitializeOrWait(
    boolean canInitialize,
    long nextSampleTimestampUs,
    long timeoutMs
)

For shared timestamp adjusters, performs necessary initialization actions for a caller.

  • If the adjuster has already established a timestamp offset then this method is a no-op.
  • If canInitialize is true and 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 canInitialize is false and the adjuster has not yet established a timestamp offset, then the call blocks until the timestamp offset is set.
Parameters
boolean canInitialize

Whether the caller is able to initialize the adjuster, if needed.

long nextSampleTimestampUs

The desired timestamp for the next sample loaded by the calling thread, in microseconds. Only used if canInitialize is true.

long timeoutMs

The timeout for the thread to wait for the timestamp adjuster to initialize, in milliseconds. A timeout of zero is interpreted as an infinite timeout.

Throws
java.lang.InterruptedException

If the thread is interrupted whilst blocked waiting for initialization to complete.

java.util.concurrent.TimeoutException

If the thread is timeout whilst blocked waiting for initialization to complete.

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.

Parameters
long us

A value in microseconds.

Returns
long

The corresponding value as a 90 kHz clock timestamp.

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
long us

A value in microseconds.

Returns
long

The corresponding value as a 90 kHz clock timestamp, wrapped to 33 bits.