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

*** ** * ** ***

<br />

```
@UnstableApi
class OpusUtil
```

<br />

*** ** * ** ***

Utility methods for handling Opus audio streams.

## Summary

| ### Constants |
|---|---|
| `const https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | `https://developer.android.com/reference/kotlin/androidx/media3/container/OpusUtil#MAX_BYTES_PER_SECOND() = 63750` Maximum achievable Opus bitrate. |
| `const https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | `https://developer.android.com/reference/kotlin/androidx/media3/container/OpusUtil#SAMPLE_RATE() = 48000` Opus streams are always 48000 Hz. |

| ### Public functions |
|---|---|
| `java-static (https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-mutable-list/index.html)https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-list/index.html<https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-array/index.html<https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-byte/index.html>!>!` | `https://developer.android.com/reference/kotlin/androidx/media3/container/OpusUtil#buildInitializationData(byte[])(header: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-byte-array/index.html<https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-byte/index.html>!)` Builds codec initialization data from an Opus Identification Header. |
| `java-static https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | `https://developer.android.com/reference/kotlin/androidx/media3/container/OpusUtil#getChannelCount(byte[])(header: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-byte-array/index.html<https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-byte/index.html>!)` Parses the channel count from an Opus Identification Header. |
| `java-static https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html` | `https://developer.android.com/reference/kotlin/androidx/media3/container/OpusUtil#getPacketDurationUs(byte[])(buffer: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-byte-array/index.html<https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-byte/index.html>!)` Returns the duration of the given audio packet. |
| `java-static https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | `https://developer.android.com/reference/kotlin/androidx/media3/container/OpusUtil#getPreSkipSamples(byte[])(header: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-byte-array/index.html<https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-byte/index.html>!)` Returns the number of pre-skip samples specified by the given Opus codec initialization data. |
| `java-static https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html` | `https://developer.android.com/reference/kotlin/androidx/media3/container/OpusUtil#needToDecodeOpusFrame(long,long)(startTimeUs: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html, frameTimeUs: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html)` 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. |
| `java-static https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | `https://developer.android.com/reference/kotlin/androidx/media3/container/OpusUtil#parseOggPacketAudioSampleCount(java.nio.ByteBuffer)(buffer: https://developer.android.com/reference/java/nio/ByteBuffer.html!)` Returns the number of audio samples in the given Ogg encapuslated Opus packet. |
| `java-static https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | `https://developer.android.com/reference/kotlin/androidx/media3/container/OpusUtil#parseOggPacketForPreAudioSampleByteCount(java.nio.ByteBuffer)(buffer: https://developer.android.com/reference/java/nio/ByteBuffer.html!)` Calculate the offset from the start of the buffer to audio sample Ogg packets. |
| `java-static https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | `https://developer.android.com/reference/kotlin/androidx/media3/container/OpusUtil#parsePacketAudioSampleCount(java.nio.ByteBuffer)(buffer: https://developer.android.com/reference/java/nio/ByteBuffer.html!)` Returns the number of audio samples in the given audio packet. |

## Constants

### MAX_BYTES_PER_SECOND

```
const val MAX_BYTES_PER_SECOND = 63750: Int
```

Maximum achievable Opus bitrate.

### SAMPLE_RATE

```
const val SAMPLE_RATE = 48000: Int
```

Opus streams are always 48000 Hz.

## Public functions

### buildInitializationData

```
java-static fun buildInitializationData(header: ByteArray<Byte>!): (Mutable)List<Array<Byte>!>!
```

Builds codec initialization data from an Opus Identification Header.

| Parameters |
|---|---|
| `header: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-byte-array/index.html<https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-byte/index.html>!` | An Opus Identification Header, as defined by RFC 7845. |

| Returns |
|---|---|
| `(https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-mutable-list/index.html)https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-list/index.html<https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-array/index.html<https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-byte/index.html>!>!` | Codec initialization data suitable for an Opus [MediaCodec](https://developer.android.com/reference/android/media/MediaCodec#initialization). |

### getChannelCount

```
java-static fun getChannelCount(header: ByteArray<Byte>!): Int
```

Parses the channel count from an Opus Identification Header.

| Parameters |
|---|---|
| `header: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-byte-array/index.html<https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-byte/index.html>!` | An Opus Identification Header, as defined by RFC 7845. |

| Returns |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | The parsed channel count. |

### getPacketDurationUs

```
java-static fun getPacketDurationUs(buffer: ByteArray<Byte>!): Long
```

Returns the duration of the given audio packet.

| Parameters |
|---|---|
| `buffer: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-byte-array/index.html<https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-byte/index.html>!` | The audio packet. |

| Returns |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html` | Returns the duration of the given audio packet, in microseconds. |

### getPreSkipSamples

```
java-static fun getPreSkipSamples(header: ByteArray<Byte>!): Int
```

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

| Parameters |
|---|---|
| `header: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-byte-array/index.html<https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-byte/index.html>!` | The Opus Identification header. |

| Returns |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | The number of pre-skip samples. |

### needToDecodeOpusFrame

```
java-static fun needToDecodeOpusFrame(startTimeUs: Long, frameTimeUs: Long): Boolean
```

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 |
|---|---|
| `startTimeUs: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html` | The time to start playing at. |
| `frameTimeUs: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html` | The time of the Opus sample. |

| Returns |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html` | Whether the frame should be decoded. |

### parseOggPacketAudioSampleCount

```
java-static fun parseOggPacketAudioSampleCount(buffer: ByteBuffer!): Int
```

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

The buffer's position is not modified.

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

| Returns |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | Returns the number of audio samples in the packet. |

### parseOggPacketForPreAudioSampleByteCount

```
java-static fun parseOggPacketForPreAudioSampleByteCount(buffer: ByteBuffer!): Int
```

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

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

| Returns |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | the offset before the Ogg packet containing audio samples. |

### parsePacketAudioSampleCount

```
java-static fun parsePacketAudioSampleCount(buffer: ByteBuffer!): Int
```

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

The buffer's position is not modified.

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

| Returns |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | Returns the number of audio samples in the packet. |