@UnstableApi
public class OpusUtil


Utility methods for handling Opus audio streams.

Summary

Constants

static final int

Maximum achievable Opus bitrate.

static final int
SAMPLE_RATE = 48000

Opus streams are always 48000 Hz.

Public methods

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

Builds codec initialization data from an Opus Identification Header.

static int
getChannelCount(byte[] header)

Parses the channel count from an Opus Identification Header.

static long
getPacketDurationUs(byte[] buffer)

Returns the duration of the given audio packet.

static int
getPreSkipSamples(byte[] header)

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

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.

static int

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

static int

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

static int

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
List<byte[]>

Codec initialization data suitable for an Opus MediaCodec.

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
ByteBuffer 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
ByteBuffer 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
ByteBuffer buffer

The audio packet.

Returns
int

Returns the number of audio samples in the packet.