# TimestampAdjuster

Artifact: [androidx.media3:media3-common](https://developer.android.com/jetpack/androidx/releases/media3) [View Source](https://cs.android.com/search?q=file:androidx/media3/common/util/TimestampAdjuster.java+class:androidx.media3.common.util.TimestampAdjuster)

*** ** * ** ***


```
@UnstableApi
class TimestampAdjuster
```

<br />

*** ** * ** ***

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

## Summary

| ### Constants |
|---|---|
| `const https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html` | `https://developer.android.com/reference/kotlin/androidx/media3/common/util/TimestampAdjuster#MODE_NO_OFFSET() = 9223372036854775807` A special `firstSampleTimestampUs` value indicating that presentation timestamps should not be offset. |
| `const https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html` | `https://developer.android.com/reference/kotlin/androidx/media3/common/util/TimestampAdjuster#MODE_SHARED() = 9223372036854775806` A special `firstSampleTimestampUs` value indicating that the adjuster will be shared by multiple threads. |

| ### Public constructors |
|---|
| `https://developer.android.com/reference/kotlin/androidx/media3/common/util/TimestampAdjuster#TimestampAdjuster(long)(firstSampleTimestampUs: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html)` |

| ### Public functions |
|---|---|
| `synchronized https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html` | `https://developer.android.com/reference/kotlin/androidx/media3/common/util/TimestampAdjuster#adjustSampleTimestamp(long)(timeUs: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html)` Offsets a timestamp in microseconds. |
| `synchronized https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html` | `https://developer.android.com/reference/kotlin/androidx/media3/common/util/TimestampAdjuster#adjustTsTimestamp(long)(pts90Khz: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html)` Scales and offsets an MPEG-2 TS presentation timestamp considering wraparound. |
| `synchronized https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html` | `https://developer.android.com/reference/kotlin/androidx/media3/common/util/TimestampAdjuster#adjustTsTimestampGreaterThanPreviousTimestamp(long)(pts90Khz: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html)` Scales and offsets an MPEG-2 TS presentation timestamp considering wraparound. |
| `synchronized https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html` | `https://developer.android.com/reference/kotlin/androidx/media3/common/util/TimestampAdjuster#getLastAdjustedTimestampUs()()` Returns the last adjusted timestamp, in microseconds. |
| `synchronized https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html` | `https://developer.android.com/reference/kotlin/androidx/media3/common/util/TimestampAdjuster#isInitialized()()` Returns whether the instance is initialized with a timestamp offset. |
| `java-static https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html` | `https://developer.android.com/reference/kotlin/androidx/media3/common/util/TimestampAdjuster#ptsToUs(long)(pts: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html)` Converts a 90 kHz clock timestamp to a timestamp in microseconds. |
| `synchronized https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/media3/common/util/TimestampAdjuster#reset(long)(firstSampleTimestampUs: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html)` Resets the instance. |
| `synchronized https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/media3/common/util/TimestampAdjuster#sharedInitializeOrWait(boolean,long,long)( canInitialize: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html, nextSampleTimestampUs: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html, timeoutMs: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html )` For shared timestamp adjusters, performs necessary initialization actions for a caller. |
| `java-static https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html` | `https://developer.android.com/reference/kotlin/androidx/media3/common/util/TimestampAdjuster#usToNonWrappedPts(long)(us: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html)` Converts a timestamp in microseconds to a 90 kHz clock timestamp. |
| `java-static https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html` | `https://developer.android.com/reference/kotlin/androidx/media3/common/util/TimestampAdjuster#usToWrappedPts(long)(us: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html)` Converts a timestamp in microseconds to a 90 kHz clock timestamp, performing wraparound to keep the result within 33-bits. |

| ### Public properties |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html` | `@https://developer.android.com/reference/kotlin/androidx/annotation/GuardedBy(value = "this") https://developer.android.com/reference/kotlin/androidx/media3/common/util/TimestampAdjuster#firstSampleTimestampUs()` |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html` | `@https://developer.android.com/reference/kotlin/androidx/annotation/GuardedBy(value = "this") https://developer.android.com/reference/kotlin/androidx/media3/common/util/TimestampAdjuster#timestampOffsetUs()` |

## Constants

### MODE_NO_OFFSET

```
const val MODE_NO_OFFSET = 9223372036854775807: Long
```

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

- `https://developer.android.com/reference/kotlin/androidx/media3/common/util/TimestampAdjuster#getFirstSampleTimestampUs()` will always return `https://developer.android.com/reference/kotlin/androidx/media3/common/C#TIME_UNSET()`.
- The only timestamp adjustment performed is to account for MPEG-2 TS timestamp rollover.

### MODE_SHARED

```
const val MODE_SHARED = 9223372036854775806: Long
```

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

- `https://developer.android.com/reference/kotlin/androidx/media3/common/util/TimestampAdjuster#getFirstSampleTimestampUs()` will always return `https://developer.android.com/reference/kotlin/androidx/media3/common/C#TIME_UNSET()`.
- Calling threads must call `https://developer.android.com/reference/kotlin/androidx/media3/common/util/TimestampAdjuster#sharedInitializeOrWait(boolean,long,long)` prior to adjusting timestamps.

## Public constructors

### TimestampAdjuster

```
TimestampAdjuster(firstSampleTimestampUs: Long)
```

| Parameters |
|---|---|
| `firstSampleTimestampUs: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html` | The desired value of the first adjusted sample timestamp in microseconds, or `https://developer.android.com/reference/kotlin/androidx/media3/common/util/TimestampAdjuster#MODE_NO_OFFSET()` if timestamps should not be offset, or `https://developer.android.com/reference/kotlin/androidx/media3/common/util/TimestampAdjuster#MODE_SHARED()` if the adjuster will be used in shared mode. |

## Public functions

### adjustSampleTimestamp

```
synchronized fun adjustSampleTimestamp(timeUs: Long): Long
```

Offsets a timestamp in microseconds.

| Parameters |
|---|---|
| `timeUs: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html` | The timestamp to adjust in microseconds. |

| Returns |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html` | The adjusted timestamp in microseconds, or `https://developer.android.com/reference/kotlin/androidx/media3/common/C#TIME_UNSET()` if `timeUs` is `https://developer.android.com/reference/kotlin/androidx/media3/common/C#TIME_UNSET()` or if the adjuster is in `https://developer.android.com/reference/kotlin/androidx/media3/common/util/TimestampAdjuster#MODE_SHARED()` and has not yet been initialized via `https://developer.android.com/reference/kotlin/androidx/media3/common/util/TimestampAdjuster#sharedInitializeOrWait(boolean,long,long)`. |

### adjustTsTimestamp

```
synchronized fun adjustTsTimestamp(pts90Khz: Long): Long
```

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 |
|---|---|
| `pts90Khz: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html` | A 90 kHz clock MPEG-2 TS presentation timestamp. |

| Returns |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html` | The adjusted timestamp in microseconds, or `https://developer.android.com/reference/kotlin/androidx/media3/common/C#TIME_UNSET()` if `pts90Khz` is `https://developer.android.com/reference/kotlin/androidx/media3/common/C#TIME_UNSET()` or if the adjuster is in `https://developer.android.com/reference/kotlin/androidx/media3/common/util/TimestampAdjuster#MODE_SHARED()` and has not yet been initialized via `https://developer.android.com/reference/kotlin/androidx/media3/common/util/TimestampAdjuster#sharedInitializeOrWait(boolean,long,long)`. |

### adjustTsTimestampGreaterThanPreviousTimestamp

```
synchronized fun adjustTsTimestampGreaterThanPreviousTimestamp(pts90Khz: Long): Long
```

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 |
|---|---|
| `pts90Khz: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html` | A 90 kHz clock MPEG-2 TS presentation timestamp. |

| Returns |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html` | The adjusted timestamp in microseconds, or `https://developer.android.com/reference/kotlin/androidx/media3/common/C#TIME_UNSET()` if `pts90Khz` is `https://developer.android.com/reference/kotlin/androidx/media3/common/C#TIME_UNSET()` or if the adjuster is in `https://developer.android.com/reference/kotlin/androidx/media3/common/util/TimestampAdjuster#MODE_SHARED()` and has not yet been initialized via `https://developer.android.com/reference/kotlin/androidx/media3/common/util/TimestampAdjuster#sharedInitializeOrWait(boolean,long,long)`. |

### getLastAdjustedTimestampUs

```
synchronized fun getLastAdjustedTimestampUs(): Long
```

Returns the last adjusted timestamp, in microseconds. If no timestamps have been adjusted yet then the result of `https://developer.android.com/reference/kotlin/androidx/media3/common/util/TimestampAdjuster#getFirstSampleTimestampUs()` is returned.

### isInitialized

```
synchronized fun isInitialized(): Boolean
```

Returns whether the instance is initialized with a timestamp offset.

### ptsToUs

```
java-static fun ptsToUs(pts: Long): Long
```

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

| Parameters |
|---|---|
| `pts: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html` | A 90 kHz clock timestamp. |

| Returns |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html` | The corresponding value in microseconds. |

### reset

```
synchronized fun reset(firstSampleTimestampUs: Long): Unit
```

Resets the instance.

| Parameters |
|---|---|
| `firstSampleTimestampUs: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html` | The desired value of the first adjusted sample timestamp after this reset in microseconds, or `https://developer.android.com/reference/kotlin/androidx/media3/common/util/TimestampAdjuster#MODE_NO_OFFSET()` if timestamps should not be offset, or `https://developer.android.com/reference/kotlin/androidx/media3/common/util/TimestampAdjuster#MODE_SHARED()` if the adjuster will be used in shared mode. |

### sharedInitializeOrWait

```
synchronized fun sharedInitializeOrWait(
    canInitialize: Boolean,
    nextSampleTimestampUs: Long,
    timeoutMs: Long
): Unit
```

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

- If the adjuster has already established a `https://developer.android.com/reference/kotlin/androidx/media3/common/util/TimestampAdjuster#getTimestampOffsetUs()` 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 |
|---|---|
| `canInitialize: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html` | Whether the caller is able to initialize the adjuster, if needed. |
| `nextSampleTimestampUs: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html` | The desired timestamp for the next sample loaded by the calling thread, in microseconds. Only used if `canInitialize` is `true`. |
| `timeoutMs: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html` | 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 |
|---|---|
| `https://developer.android.com/reference/java/lang/InterruptedException.html` | If the thread is interrupted whilst blocked waiting for initialization to complete. |
| `https://developer.android.com/reference/java/util/concurrent/TimeoutException.html` | If the thread is timeout whilst blocked waiting for initialization to complete. |

### usToNonWrappedPts

```
java-static fun usToNonWrappedPts(us: Long): Long
```

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 `https://developer.android.com/reference/kotlin/androidx/media3/common/util/TimestampAdjuster#usToWrappedPts(long)`.

| Parameters |
|---|---|
| `us: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html` | A value in microseconds. |

| Returns |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html` | The corresponding value as a 90 kHz clock timestamp. |

### usToWrappedPts

```
java-static fun usToWrappedPts(us: Long): Long
```

Converts a timestamp in microseconds to a 90 kHz clock timestamp, performing wraparound to keep the result within 33-bits.

| Parameters |
|---|---|
| `us: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html` | A value in microseconds. |

| Returns |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html` | The corresponding value as a 90 kHz clock timestamp, wrapped to 33 bits. |

## Public properties

### firstSampleTimestampUs

```
@GuardedBy(value = "this")
val firstSampleTimestampUs: Long
```

### timestampOffsetUs

```
@GuardedBy(value = "this")
val timestampOffsetUs: Long
```