| 41 | * Corresponds to the WebCodecs VideoDecoder API. |
| 42 | */ |
| 43 | export class VideoDecoderPolyfill extends EventTarget { |
| 44 | private init: VideoDecoderInit; |
| 45 | private codecContext: CodecContext | null = null; |
| 46 | private mutex = new AsyncMutex(); |
| 47 | private frame: Frame; |
| 48 | private packet: Packet; |
| 49 | private _decodeQueueSize = 0; |
| 50 | private currentConfigId = 0; |
| 51 | private keyChunkRequired = true; |
| 52 | private pendingFlushResolvers: Array<{ resolve: () => void; reject: (e: unknown) => void }> = []; |
| 53 | |
| 54 | private _state: VideoDecoder['state'] = 'unconfigured'; |
| 55 | /** |
| 56 | * The current state of the decode. |
| 57 | */ |
| 58 | get state() { |
| 59 | return this._state; |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * The number of pending decode operations. |
| 64 | */ |
| 65 | get decodeQueueSize() { |
| 66 | return this._decodeQueueSize; |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Event handler called when a decode operation completes and leaves the queue. |
| 71 | */ |
| 72 | ondequeue: ((this: VideoDecoder, ev: Event) => unknown) | null = null; |
| 73 | |
| 74 | /** |
| 75 | * Creates a new VideoDecoder. |
| 76 | */ |
| 77 | constructor(init: VideoDecoderInit) { |
| 78 | super(); |
| 79 | |
| 80 | if (!init.output || !init.error) { |
| 81 | throw new TypeError('VideoDecoderInit requires \'output\' and \'error\' callbacks.'); |
| 82 | } |
| 83 | |
| 84 | this.init = init; |
| 85 | this.frame = new Frame(); |
| 86 | this.frame.alloc(); |
| 87 | this.packet = new Packet(); |
| 88 | this.packet.alloc(); |
| 89 | } |
| 90 | |
| 91 | private static createCodecContext(config: VideoDecoderConfig) { |
| 92 | const inferred = inferCodecFromCodecString(config.codec); |
| 93 | if (!inferred) { |
| 94 | return null; |
| 95 | } |
| 96 | |
| 97 | let codec: Codec | null; |
| 98 | if (inferred === AV_CODEC_ID_AV1 || config.hardwareAcceleration !== 'prefer-hardware') { |
| 99 | // https://github.com/opencv/opencv/issues/24430 |
| 100 | codec = Codec.findDecoder(inferred); |
nothing calls this directly
no outgoing calls
no test coverage detected