| 59 | * Corresponds to the WebCodecs AudioEncoder API. |
| 60 | */ |
| 61 | export class AudioEncoderPolyfill extends EventTarget { |
| 62 | private init: AudioEncoderInit; |
| 63 | private config: AudioEncoderConfig | null = null; |
| 64 | private codecContext: CodecContext | null = null; |
| 65 | private mutex = new AsyncMutex(); |
| 66 | private frame: Frame; |
| 67 | private packet: Packet; |
| 68 | private _encodeQueueSize = 0; |
| 69 | private currentConfigId = 0; |
| 70 | private chunkOutputted = false; |
| 71 | private chunkTimestampOffset = 0; |
| 72 | private pendingFlushResolvers: Array<{ resolve: () => void; reject: (e: unknown) => void }> = []; |
| 73 | private resampler: SoftwareResampleContext | null = null; |
| 74 | private lastResamplerKey: string | null = null; |
| 75 | private dstFrame: Frame | null = null; |
| 76 | private nextPts: bigint = 0n; |
| 77 | private aacAudioSpecificConfig: AacAudioSpecificConfig | null = null; |
| 78 | private resampledSampleCount: number = 0; |
| 79 | |
| 80 | private _state: AudioEncoder['state'] = 'unconfigured'; |
| 81 | /** |
| 82 | * The current state of the encoder: "unconfigured", "configured", or "closed". |
| 83 | */ |
| 84 | get state() { |
| 85 | return this._state; |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * The number of pending encode operations. |
| 90 | */ |
| 91 | get encodeQueueSize() { |
| 92 | return this._encodeQueueSize; |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Event handler called when an encode operation completes and leaves the queue. |
| 97 | */ |
| 98 | ondequeue: ((this: AudioEncoder, ev: Event) => unknown) | null = null; |
| 99 | |
| 100 | /** |
| 101 | * Creates a new AudioEncoder. |
| 102 | */ |
| 103 | constructor(init: AudioEncoderInit) { |
| 104 | super(); |
| 105 | |
| 106 | if (!init.output || !init.error) { |
| 107 | throw new TypeError('AudioEncoderInit requires \'output\' and \'error\' callbacks.'); |
| 108 | } |
| 109 | |
| 110 | this.init = init; |
| 111 | this.frame = new Frame(); |
| 112 | this.frame.alloc(); |
| 113 | this.packet = new Packet(); |
| 114 | this.packet.alloc(); |
| 115 | } |
| 116 | |
| 117 | private static createCodecContext(config: AudioEncoderConfig) { |
| 118 | const inferred = inferCodecFromCodecString(config.codec); |
nothing calls this directly
no outgoing calls
no test coverage detected