Class 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 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.
      • 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.