Package com.google.android.exoplayer2.util

## 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 long` | `DO_NOT_OFFSET` | A special `firstSampleTimestampUs` value 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
    --- | --- | ---
    `long` | `adjustSampleTimestamp​(long timeUs)` | Offsets a timestamp in microseconds.
    `long` | `adjustTsTimestamp​(long pts90Khz)` | Scales and offsets an MPEG-2 TS presentation timestamp considering wraparound.
    `long` | `getFirstSampleTimestampUs()` | Returns the value of the first adjusted sample timestamp in microseconds, or `DO_NOT_OFFSET` if timestamps will not be offset.
    `long` | `getLastAdjustedTimestampUs()` | Returns the last value obtained from `adjustSampleTimestamp(long)`.
    `long` | `getTimestampOffsetUs()` | Returns the offset between the input of `adjustSampleTimestamp(long)` and its output.
    `static long` | `ptsToUs​(long pts)` | Converts a 90 kHz clock timestamp to a timestamp in microseconds.
    `void` | `reset​(long firstSampleTimestampUs)` | Resets the instance to its initial state.
    `void` | `sharedInitializeOrWait​(boolean canInitialize, long startTimeUs)` | For shared timestamp adjusters, performs necessary initialization actions for a caller.
    `static long` | `usToNonWrappedPts​(long us)` | 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.

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Field Detail

    - #### DO_NOT_OFFSET

          public static final long DO_NOT_OFFSET

      A special `firstSampleTimestampUs` value 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, or `DO_NOT_OFFSET` if timestamps should not be offset.

  - ### Method Detail

    - #### sharedInitializeOrWait

          public void sharedInitializeOrWait​(boolean canInitialize,
                                             long startTimeUs)
                                      throws InterruptedException

      For shared timestamp adjusters, performs necessary initialization actions for a caller.
      - If the adjuster does not yet have a target `first sample timestamp` and if `canInitialize` is `true`, then initialization is started by setting the target first sample timestamp to `firstSampleTimestampUs`. The call returns, allowing the caller to proceed. Initialization completes when a caller adjusts the first timestamp.
      - If `canInitialize` is `true` and the adjuster already has a target `first sample timestamp`, then the call returns to allow the caller to proceed only if `firstSampleTimestampUs` is equal to the target. This ensures a caller that's previously started initialization can continue to proceed. It also allows other callers with the same `firstSampleTimestampUs` to proceed, since in this case it doesn't matter which caller adjusts the first timestamp to complete initialization.
      - If `canInitialize` is `false` or if `firstSampleTimestampUs` differs from the target `first 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 if `canInitialize` is `true`.
      Throws:
        `InterruptedException` - If the thread is interrupted whilst blocked waiting for initialization to complete.

    - #### getFirstSampleTimestampUs

          public long getFirstSampleTimestampUs()

      Returns the value of the first adjusted sample timestamp in microseconds, or `DO_NOT_OFFSET` if timestamps will not be offset.

    - #### getLastAdjustedTimestampUs

          public long getLastAdjustedTimestampUs()

      Returns the last value obtained from `adjustSampleTimestamp(long)`. If `adjustSampleTimestamp(long)` has not been called, returns the result of calling `getFirstSampleTimestampUs()`. If this value is `DO_NOT_OFFSET`, returns `C.TIME_UNSET`.

    - #### getTimestampOffsetUs

          public long getTimestampOffsetUs()

      Returns the offset between the input of `adjustSampleTimestamp(long)` and its output. If `DO_NOT_OFFSET` was provided to the constructor, 0 is returned. If the timestamp adjuster is yet not initialized, `C.TIME_UNSET` is returned.
      Returns:
        The offset between `adjustSampleTimestamp(long)`'s input and output. `C.TIME_UNSET` if 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, or `DO_NOT_OFFSET` if 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.