# OpusUtil

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

*** ** * ** ***


```
@UnstableApi
public class OpusUtil
```

<br />

*** ** * ** ***

Utility methods for handling Opus audio streams.

## Summary

| ### Constants |
|---|---|
| `static final int` | `https://developer.android.com/reference/androidx/media3/container/OpusUtil#MAX_BYTES_PER_SECOND() = 63750` Maximum achievable Opus bitrate. |
| `static final int` | `https://developer.android.com/reference/androidx/media3/container/OpusUtil#SAMPLE_RATE() = 48000` Opus streams are always 48000 Hz. |

| ### Public methods |
|---|---|
| `static https://developer.android.com/reference/java/util/List.html<byte[]>` | `https://developer.android.com/reference/androidx/media3/container/OpusUtil#buildInitializationData(byte[])(byte[] header)` Builds codec initialization data from an Opus Identification Header. |
| `static int` | `https://developer.android.com/reference/androidx/media3/container/OpusUtil#getChannelCount(byte[])(byte[] header)` Parses the channel count from an Opus Identification Header. |
| `static long` | `https://developer.android.com/reference/androidx/media3/container/OpusUtil#getPacketDurationUs(byte[])(byte[] buffer)` Returns the duration of the given audio packet. |
| `static int` | `https://developer.android.com/reference/androidx/media3/container/OpusUtil#getPreSkipSamples(byte[])(byte[] header)` Returns the number of pre-skip samples specified by the given Opus codec initialization data. |
| `static boolean` | `https://developer.android.com/reference/androidx/media3/container/OpusUtil#needToDecodeOpusFrame(long,long)(long startTimeUs, long frameTimeUs)` Returns whether an Opus frame should be sent to the decoder as it is either past the start position or within the seek-preroll duration. |
| `static int` | `https://developer.android.com/reference/androidx/media3/container/OpusUtil#parseOggPacketAudioSampleCount(java.nio.ByteBuffer)(https://developer.android.com/reference/java/nio/ByteBuffer.html buffer)` Returns the number of audio samples in the given Ogg encapuslated Opus packet. |
| `static int` | `https://developer.android.com/reference/androidx/media3/container/OpusUtil#parseOggPacketForPreAudioSampleByteCount(java.nio.ByteBuffer)(https://developer.android.com/reference/java/nio/ByteBuffer.html buffer)` Calculate the offset from the start of the buffer to audio sample Ogg packets. |
| `static int` | `https://developer.android.com/reference/androidx/media3/container/OpusUtil#parsePacketAudioSampleCount(java.nio.ByteBuffer)(https://developer.android.com/reference/java/nio/ByteBuffer.html buffer)` Returns the number of audio samples in the given audio packet. |

## Constants

### MAX_BYTES_PER_SECOND

```
public static final int MAX_BYTES_PER_SECOND = 63750
```

Maximum achievable Opus bitrate.

### SAMPLE_RATE

```
public static final int SAMPLE_RATE = 48000
```

Opus streams are always 48000 Hz.

## Public methods

### buildInitializationData

```
public static List<byte[]> buildInitializationData(byte[] header)
```

Builds codec initialization data from an Opus Identification Header.

| Parameters |
|---|---|
| `byte[] header` | An Opus Identification Header, as defined by RFC 7845. |

| Returns |
|---|---|
| `https://developer.android.com/reference/java/util/List.html<byte[]>` | Codec initialization data suitable for an Opus [MediaCodec](https://developer.android.com/reference/android/media/MediaCodec#initialization). |

### getChannelCount

```
public static int getChannelCount(byte[] header)
```

Parses the channel count from an Opus Identification Header.

| Parameters |
|---|---|
| `byte[] header` | An Opus Identification Header, as defined by RFC 7845. |

| Returns |
|---|---|
| `int` | The parsed channel count. |

### getPacketDurationUs

```
public static long getPacketDurationUs(byte[] buffer)
```

Returns the duration of the given audio packet.

| Parameters |
|---|---|
| `byte[] buffer` | The audio packet. |

| Returns |
|---|---|
| `long` | Returns the duration of the given audio packet, in microseconds. |

### getPreSkipSamples

```
public static int getPreSkipSamples(byte[] header)
```

Returns the number of pre-skip samples specified by the given Opus codec initialization data.

| Parameters |
|---|---|
| `byte[] header` | The Opus Identification header. |

| Returns |
|---|---|
| `int` | The number of pre-skip samples. |

### needToDecodeOpusFrame

```
public static boolean needToDecodeOpusFrame(long startTimeUs, long frameTimeUs)
```

Returns whether an Opus frame should be sent to the decoder as it is either past the start position or within the seek-preroll duration.

The measure of whether an Opus frame should not be decoded is if its time precedes the start position by more than the default seek-preroll value.

| Parameters |
|---|---|
| `long startTimeUs` | The time to start playing at. |
| `long frameTimeUs` | The time of the Opus sample. |

| Returns |
|---|---|
| `boolean` | Whether the frame should be decoded. |

### parseOggPacketAudioSampleCount

```
public static int parseOggPacketAudioSampleCount(ByteBuffer buffer)
```

Returns the number of audio samples in the given Ogg encapuslated Opus packet.

The buffer's position is not modified.

| Parameters |
|---|---|
| `https://developer.android.com/reference/java/nio/ByteBuffer.html buffer` | The audio packet. |

| Returns |
|---|---|
| `int` | Returns the number of audio samples in the packet. |

### parseOggPacketForPreAudioSampleByteCount

```
public static int parseOggPacketForPreAudioSampleByteCount(ByteBuffer buffer)
```

Calculate the offset from the start of the buffer to audio sample Ogg packets.

| Parameters |
|---|---|
| `https://developer.android.com/reference/java/nio/ByteBuffer.html buffer` | containing the Ogg Encapsulated Opus audio bitstream. |

| Returns |
|---|---|
| `int` | the offset before the Ogg packet containing audio samples. |

### parsePacketAudioSampleCount

```
public static int parsePacketAudioSampleCount(ByteBuffer buffer)
```

Returns the number of audio samples in the given audio packet.

The buffer's position is not modified.

| Parameters |
|---|---|
| `https://developer.android.com/reference/java/nio/ByteBuffer.html buffer` | The audio packet. |

| Returns |
|---|---|
| `int` | Returns the number of audio samples in the packet. |