| 87 | * Corresponds to the WebCodecs VideoEncoder API. |
| 88 | */ |
| 89 | export class VideoEncoderPolyfill extends EventTarget { |
| 90 | private init: VideoEncoderInit; |
| 91 | private config: VideoEncoderConfig | null = null; |
| 92 | private codecContext: CodecContext | null = null; |
| 93 | private mutex = new AsyncMutex(); |
| 94 | private frame: Frame; |
| 95 | private packet: Packet; |
| 96 | private _encodeQueueSize = 0; |
| 97 | private currentConfigId = 0; |
| 98 | private chunkOutputted = false; |
| 99 | private colorSpaceSet = false; |
| 100 | private pendingFlushResolvers: Array<{ resolve: () => void; reject: (e: unknown) => void }> = []; |
| 101 | private scaler: SoftwareScaleContext | null = null; |
| 102 | private lastScalerKey: string | null = null; |
| 103 | private dstFrame: Frame | null = null; |
| 104 | |
| 105 | private _state: VideoEncoder['state'] = 'unconfigured'; |
| 106 | /** |
| 107 | * The current state of the encoder: "unconfigured", "configured", or "closed". |
| 108 | */ |
| 109 | get state() { |
| 110 | return this._state; |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * The number of pending encode operations. |
| 115 | */ |
| 116 | get encodeQueueSize() { |
| 117 | return this._encodeQueueSize; |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Event handler called when an encode operation completes and leaves the queue. |
| 122 | */ |
| 123 | ondequeue: ((this: VideoEncoder, ev: Event) => unknown) | null = null; |
| 124 | |
| 125 | /** |
| 126 | * Creates a new VideoEncoder. |
| 127 | */ |
| 128 | constructor(init: VideoEncoderInit) { |
| 129 | super(); |
| 130 | |
| 131 | if (!init.output || !init.error) { |
| 132 | throw new TypeError('VideoEncoderInit requires \'output\' and \'error\' callbacks.'); |
| 133 | } |
| 134 | |
| 135 | this.init = init; |
| 136 | this.frame = new Frame(); |
| 137 | this.frame.alloc(); |
| 138 | this.packet = new Packet(); |
| 139 | this.packet.alloc(); |
| 140 | } |
| 141 | |
| 142 | private static createCodecContext(config: VideoEncoderConfig) { |
| 143 | const inferred = inferCodecFromCodecString(config.codec); |
| 144 | if (!inferred) { |
| 145 | throw new Error('Unsupported codec'); |
| 146 | } |
nothing calls this directly
no outgoing calls
no test coverage detected