# TsUtil

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

*** ** * ** ***

<br />

```
@UnstableApi
class TsUtil
```

<br />

*** ** * ** ***

Utilities method for extracting MPEG-TS streams.

## Summary

| ### Public functions |
|---|---|
| `java-static https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | `https://developer.android.com/reference/kotlin/androidx/media3/extractor/ts/TsUtil#findSyncBytePosition(byte[],int,int)( data: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-byte-array/index.html<https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-byte/index.html>!, startPosition: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html, limitPosition: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html )` Returns the position of the first TS_SYNC_BYTE within the range \[startPosition, limitPosition) from the provided data array, or returns limitPosition if sync byte could not be found. |
| `java-static https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html` | `https://developer.android.com/reference/kotlin/androidx/media3/extractor/ts/TsUtil#isStartOfTsPacket(byte[],int,int,int)( data: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-byte-array/index.html<https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-byte/index.html>!, start: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html, limit: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html, searchPosition: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html )` Returns whether a TS packet starts at `searchPosition` according to the MPEG-TS synchronization recommendations. |
| `java-static https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html` | `https://developer.android.com/reference/kotlin/androidx/media3/extractor/ts/TsUtil#readPcrFromPacket(androidx.media3.common.util.ParsableByteArray,int,int)( packetBuffer: https://developer.android.com/reference/kotlin/androidx/media3/common/util/ParsableByteArray!, startOfPacket: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html, pcrPid: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html )` Returns the PCR value read from a given TS packet. |

## Public functions

### findSyncBytePosition

```
java-static fun findSyncBytePosition(
    data: ByteArray<Byte>!,
    startPosition: Int,
    limitPosition: Int
): Int
```

Returns the position of the first TS_SYNC_BYTE within the range \[startPosition, limitPosition) from the provided data array, or returns limitPosition if sync byte could not be found.

### isStartOfTsPacket

```
java-static fun isStartOfTsPacket(
    data: ByteArray<Byte>!,
    start: Int,
    limit: Int,
    searchPosition: Int
): Boolean
```

Returns whether a TS packet starts at `searchPosition` according to the MPEG-TS synchronization recommendations.

ISO/IEC 13818-1:2015 Annex G recommends that 5 sync bytes emulating the start of 5 consecutive TS packets should never occur as part of the TS packets' contents. So, this method returns true when `data` contains a sync byte at `searchPosition`, and said sync byte is also one of five consecutive sync bytes separated from each other by the size of a TS packet.

| Parameters |
|---|---|
| `data: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-byte-array/index.html<https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-byte/index.html>!` | The array holding the data to search in. |
| `start: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | The first valid position in `data` from which a sync byte can be read. |
| `limit: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | The first invalid position in `data`, after which no data should be read. |
| `searchPosition: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | The position to check for a TS packet start. |

| Returns |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html` | Whether a TS packet starts at `searchPosition`. |

### readPcrFromPacket

```
java-static fun readPcrFromPacket(
    packetBuffer: ParsableByteArray!,
    startOfPacket: Int,
    pcrPid: Int
): Long
```

Returns the PCR value read from a given TS packet.

| Parameters |
|---|---|
| `packetBuffer: https://developer.android.com/reference/kotlin/androidx/media3/common/util/ParsableByteArray!` | The buffer that holds the packet. |
| `startOfPacket: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | The starting position of the packet in the buffer. |
| `pcrPid: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | The PID for valid packets that contain PCR values. |

| Returns |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html` | The PCR value read from the packet, if its PID is equal to `pcrPid` and it contains a valid PCR value. Returns `https://developer.android.com/reference/kotlin/androidx/media3/common/C#TIME_UNSET()` otherwise. |