# 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)

*** ** * ** ***


```
@UnstableApi
public final class TsUtil
```

<br />

*** ** * ** ***

Utilities method for extracting MPEG-TS streams.

## Summary

| ### Public methods |
|---|---|
| `static int` | `https://developer.android.com/reference/androidx/media3/extractor/ts/TsUtil#findSyncBytePosition(byte[],int,int)(byte[] data, int startPosition, int limitPosition)` 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. |
| `static boolean` | `https://developer.android.com/reference/androidx/media3/extractor/ts/TsUtil#isStartOfTsPacket(byte[],int,int,int)(byte[] data, int start, int limit, int searchPosition)` Returns whether a TS packet starts at `searchPosition` according to the MPEG-TS synchronization recommendations. |
| `static long` | `https://developer.android.com/reference/androidx/media3/extractor/ts/TsUtil#readPcrFromPacket(androidx.media3.common.util.ParsableByteArray,int,int)( https://developer.android.com/reference/androidx/media3/common/util/ParsableByteArray packetBuffer, int startOfPacket, int pcrPid )` Returns the PCR value read from a given TS packet. |

## Public methods

### findSyncBytePosition

```
public static int findSyncBytePosition(byte[] data, int startPosition, int limitPosition)
```

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

```
public static boolean isStartOfTsPacket(byte[] data, int start, int limit, int searchPosition)
```

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 |
|---|---|
| `byte[] data` | The array holding the data to search in. |
| `int start` | The first valid position in `data` from which a sync byte can be read. |
| `int limit` | The first invalid position in `data`, after which no data should be read. |
| `int searchPosition` | The position to check for a TS packet start. |

| Returns |
|---|---|
| `boolean` | Whether a TS packet starts at `searchPosition`. |

### readPcrFromPacket

```
public static long readPcrFromPacket(
    ParsableByteArray packetBuffer,
    int startOfPacket,
    int pcrPid
)
```

Returns the PCR value read from a given TS packet.

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

| Returns |
|---|---|
| `long` | 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/androidx/media3/common/C#TIME_UNSET()` otherwise. |