jessibuca
Version:
a h5 live stream player
59 lines (58 loc) • 1.57 kB
text/typescript
import {
VideoDecoderInterface,
VideoDecoderEvent,
} from "./types";
import { ChangeState, FSM, Includes } from "afsm";
export class VideoDecoderHard extends FSM implements VideoDecoderInterface {
decoder!: VideoDecoder;
config?: VideoDecoderConfig;
async initialize() {
this.decoder = new VideoDecoder({
output: (frame) => {
this.emit(VideoDecoderEvent.VideoFrame, frame);
},
error: (err) => {
this.close();
this.emit(VideoDecoderEvent.Error, err);
},
});
}
configure(config: VideoDecoderConfig): void {
this.config = config;
this.decoder.configure({
...config,
codec: this.getCodec(config)
});
}
getCodec(config: VideoDecoderConfig) {
switch (config.codec) {
case "hevc":
return 'hvc1.1.6.L0.12.34.56.78.9A.BC';
case "av1":
return 'av01.0.05M.08';
case "avc":
return 'avc1.420028';
default:
return config.codec;
}
}
decode(packet: EncodedVideoChunkInit): void {
if (this.decoder.state === "configured")
this.decoder.decode(new EncodedVideoChunk(packet));
}
flush(): void {
this.decoder.flush();
}
reset(): void {
this.decoder.reset();
}
close(): void {
if (this.decoder.state !== "closed")
this.decoder.close();
}
}