Skip to content

Demuxer Class Reference

devongovett edited this page Nov 12, 2012 · 8 revisions

The AV.Demuxer class is an abstract class that subclasses extend to implement probing, and reading of container formats (e.g. WAV, M4A, CAF, etc.). Demuxers receive data from an AV.Source and implement the readChunk method to read the format information, duration, metadata, and actual audio data to be passed to the AV.Decoder object.

When you implement your own demuxers, remember to call AV.Demuxer.register(YourDemuxer) to register your demuxer with the framework.

Properties

Methods You Must Implement

Events

Optional Methods

Registering and Probing

Properties

AV.Demuxer#stream

The AV.Stream object that you can use for reading the file.

Methods You Must Implement

AV.Demuxer.probe(stream)

The class level probe method is provided with the first buffer in the audio file (variable length) since you can usually tell the container format within the first few bytes of the file. The buffer is a AV.Stream object. The probe method should return a boolean representing whether the file is in the demuxer's recognized container format or not.

AV.Demuxer#readChunk()

readChunk is called whenever new data is available from the source. It must be written in a way that accounts for the fact that not all data will have been received when it is called. Thus it may need to defer some parsing until the next time readChunk is called in order to read an entire data structure. readChunk should emit the events specified below.

Events

'format', object

The format event should be emitted by the demuxer with the following information:

{
  formatID: 'abcd',    // <= 4 letter string representing the decoder that should be used
  sampleRate: 44100,   // the sampleRate of the file
  channelsPerFrame: 2, // the number of audio channels
  bitsPerChannel: 16   // the sample bit depth
}

Other information is allowed in the format object, but will be ignored by the framework.

'duration', msecs

The duration event should be emitted with the duration of the audio file in milliseconds. If the duration of the file is unknown, the event should not be emitted.

'metadata', object

The metadata event should be emitted with an object containing any metadata embedded in the file. If not metadata is included in the file, the event should not be emitted.

'cookie', buffer

The cookie event should be emitted with an AV.Buffer object containing the "magic cookie" or decoder specific information to be read by the decoder. If no magic cookie is embedded in the file, the event should not be emitted.

'data', buffer, isLastBuffer

The data event should be emitted whenever actual audio data is encountered. It should be emitted with a buffer containing the audio data and a boolean saying whether this is the last buffer that will be emitted.

'error', err

Emitted when there is an error in the demuxer.

Optional Methods

AV.Demuxer#init()

Called by the framework to initialize the demuxer. You should not override the demuxer's constructor but provide the init method instead to perform your initialization. If an init method is not provided, the default implementation does nothing.

Registering and Probing

AV.Demuxer.register(demuxer)

All Demuxer subclasses should be registered with the AV.Demuxer class using this class method.

AV.Demuxer.find(buffer)

Finds a registered demuxer that supports the format as probed using the probe class method. An AV.Buffer object should be passed to this method.