MCPcopy Create free account
hub / github.com/Vanilagy/webcodecs-polyfill / AudioDecoderPolyfill

Class AudioDecoderPolyfill

src/audio_decoder.ts:23–406  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

21 * Corresponds to the WebCodecs AudioDecoder API.
22 */
23export class AudioDecoderPolyfill extends EventTarget {
24 private init: AudioDecoderInit;
25 private codecContext: CodecContext | null = null;
26 private mutex = new AsyncMutex();
27 private frame: Frame;
28 private packet: Packet;
29 private _decodeQueueSize = 0;
30 private currentConfigId = 0;
31 private _keyChunkRequired = true;
32 private _pendingFlushResolvers: Array<{ resolve: () => void; reject: (e: unknown) => void }> = [];
33
34 private _state: AudioDecoder['state'] = 'unconfigured';
35 /**
36 * The current state of the decoder.
37 */
38 get state() {
39 return this._state;
40 }
41
42 /**
43 * The number of pending decode operations.
44 */
45 get decodeQueueSize() {
46 return this._decodeQueueSize;
47 }
48
49 /**
50 * Event handler called when a decode operation completes and leaves the queue.
51 */
52 ondequeue: ((this: AudioDecoder, ev: Event) => unknown) | null = null;
53
54 /**
55 * Creates a new AudioDecoder.
56 */
57 constructor(init: AudioDecoderInit) {
58 super();
59
60 if (!init.output || !init.error) {
61 throw new TypeError('AudioDecoderInit requires \'output\' and \'error\' callbacks.');
62 }
63
64 this.init = init;
65 this.frame = new Frame();
66 this.frame.alloc();
67 this.packet = new Packet();
68 this.packet.alloc();
69 }
70
71 private static createCodecContext(config: AudioDecoderConfig) {
72 const inferred = inferCodecFromCodecString(config.codec);
73 if (!inferred) {
74 return null;
75 }
76
77 const codec = Codec.findDecoder(inferred);
78 if (!codec) {
79 return null;
80 }

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected