bilibili/flv.js · error · IllegalStateException

Flv: onError & onMediaInfo & onTrackMetadata & onDataAvailab

Error message

Flv: onError & onMediaInfo & onTrackMetadata & onDataAvailable callback must be specified

What it means

parseChunks() requires the four consumer callbacks onError, onMediaInfo, onTrackMetadata and onDataAvailable to be registered before any FLV data is demuxed; they are the only way the demuxer delivers parsed events and errors. If any is missing, the library throws IllegalStateException up front rather than silently dropping output. It is an integration-contract check, not a data error.

Source

Thrown at src/demux/flv-demuxer.js:270

    }

    _isInitialMetadataDispatched() {
        if (this._hasAudio && this._hasVideo) {  // both audio & video
            return this._audioInitialMetadataDispatched && this._videoInitialMetadataDispatched;
        }
        if (this._hasAudio && !this._hasVideo) {  // audio only
            return this._audioInitialMetadataDispatched;
        }
        if (!this._hasAudio && this._hasVideo) {  // video only
            return this._videoInitialMetadataDispatched;
        }
        return false;
    }

    // function parseChunks(chunk: ArrayBuffer, byteStart: number): number;
    parseChunks(chunk, byteStart) {
        if (!this._onError || !this._onMediaInfo || !this._onTrackMetadata || !this._onDataAvailable) {
            throw new IllegalStateException('Flv: onError & onMediaInfo & onTrackMetadata & onDataAvailable callback must be specified');
        }

        let offset = 0;
        let le = this._littleEndian;

        if (byteStart === 0) {  // buffer with FLV header
            if (chunk.byteLength > 13) {
                let probeData = FLVDemuxer.probe(chunk);
                offset = probeData.dataOffset;
            } else {
                return 0;
            }
        }

        if (this._firstParse) {  // handle PreviousTagSize0 before Tag1
            this._firstParse = false;
            if (byteStart + offset !== this._dataOffset) {
                Log.w(this.TAG, 'First time parsing but chunk byteStart invalid!');

View on GitHub (pinned to 42343088f2)

Solutions

  1. Set all four callbacks on the demuxer before the first parseChunks call
  2. Set each property directly if using the raw demuxer: demuxer.onError = fn; demuxer.onMediaInfo = fn; demuxer.onTrackMetadata = fn; demuxer.onDataAvailable = fn
  3. Use FlvPlayer/createPlayer instead of the raw demuxer so callbacks are wired for you

Example fix

// before
demuxer.onError = onError;
demuxer.parseChunks(chunk, 0); // throws: onMediaInfo etc. missing
// after
demuxer.onError = onError;
demuxer.onMediaInfo = onMediaInfo;
demuxer.onTrackMetadata = onTrackMetadata;
demuxer.onDataAvailable = onDataAvailable;
demuxer.parseChunks(chunk, 0);
Defensive patterns

Strategy: validation

Validate before calling

if (!demuxer._onError || !demuxer._onMediaInfo || !demuxer._onTrackMetadata || !demuxer._onDataAvailable) {
  throw new Error('Register onError, onMediaInfo, onTrackMetadata and onDataAvailable before parseChunks');
}

Type guard

function callbacksReady(demuxer) {
  return [demuxer._onError, demuxer._onMediaInfo, demuxer._onTrackMetadata, demuxer._onDataAvailable]
    .every(fn => typeof fn === 'function');
}

Try / catch

try {
  demuxer.parseChunks(chunk, byteStart);
} catch (e) {
  if (e.name === 'IllegalStateException') {
    console.error('Demuxer callbacks not wired:', e.message);
  } else { throw e; }
}

Prevention

When it happens

Trigger: Calling flvDemuxer.parseChunks(chunk, byteStart) before assigning all four _onError/_onMediaInfo/_onTrackMetadata/_onDataAvailable callbacks (e.g. binding only some, or binding them asynchronously after the first chunk is fed).

Common situations: Writing a custom player pipeline around the demuxer and forgetting one callback (often onMediaInfo or onDataAvailable); wiring callbacks after starting the io loader whose onContentLengthKnown immediately drives parsing; copy-pasted demo code with some handlers removed.

Related errors


AI-assisted analysis of bilibili/flv.js@42343088f2 (2026-09-01). Data as JSON: /api/errors/824fb66dbe2d73d8. Report an issue: GitHub.