Skip to content

Decoder Class Reference

Devon Govett edited this page Sep 3, 2013 · 9 revisions

The AV.Decoder class is an abstract class that subclasses extend to implement actual audio decoding for various codecs (e.g. MP3, AAC, FLAC, etc.). Decoders receive data from the AV.Demuxer and implement the readChunk method to decode the audio data into Linear PCM to be passed to the audio hardware for playback, or other output destination.

When you implement your own decoders, remember to call AV.Decoder.register(id, YourDecoder) to register your decoder with the framework.

Properties

Methods

Methods To Implement

Events

Registering and Finding

Properties

AV.Decoder#stream

A AV.Stream object containing the audio data.

AV.Decoder#bitstream

A AV.Bitstream object containing the audio data.

AV.Decoder#receivedFinalBuffer

A boolean that keeps track of whether the decoder has been sent the final buffer from the AV.Demuxer yet.

Methods

AV.Decoder#decode()

This is the method called by higher levels of the framework (or you directly) to decode a packet. It wraps around the decoder's readChunk method to handle errors, underflows, emitting data and end events, and keeping track of the decoder's state.

AV.Decoder#seek(timestamp)

The default implementation asks the demuxer to seek to the timestamp, and updates its underlying stream. Decoders can override for custom seeking behavior. Returns the timestamp of the actual seek point it jumped to.

Methods To Implement

AV.Decoder#setCookie(cookie)

This method is optional and is called when a 'cookie' event is emitted by the AV.Demuxer. The cookie is passed as an AV.Buffer object.

AV.Decoder#readChunk()

readChunk is required, and is called by the framework whenever decoding is necessary. readChunk is responsible for decoding the audio data found in the stream or bitstream and returning decoded audio packets as Typed Arrays.

If an error occurs during decoding, throw an error object (i.e. new Error(message)).

If not enough data is available, throw an AV.UnderflowError (this is automatically thrown by the builtin Stream and Bitstream classes, so you normally don't need to worry about underflows.

This method should not be called directly by your code, use the decode method.

Events

'data', buffer

Decoded audio data is emitted as a Typed Array

'end'

Emitted when the decoder has reached the end of its input.

'error', err

The error event is emitted whenever there is an error in the decoder. If an error occurs, the entire decoding process halts.

Registering and Finding

AV.Decoder.register(id, decoder)

All AV.Decoder subclasses should be registered with the AV.Decoder class using this class method. You can register your decoder for multiple ids if you know that different containers use different FOURCCs for the codec, for example.

AV.Decoder.find(id)

Finds a registered decoder using the 4 (or less) character FOURCC code the decoder registered itself as.