jessibuca
Version:
a h5 live stream player
44 lines (42 loc) • 1.23 kB
text/typescript
import {
AudioDecoderInterface,
AudioDecoderEvent,
} from "./types";
import { ChangeState, FSM, Includes } from "afsm";
export class AudioDecoderHard extends FSM implements AudioDecoderInterface {
decoder!: AudioDecoder;
config?: AudioDecoderConfig;
async initialize() {
this.decoder = new AudioDecoder({
output: (frame) => {
this.emit(AudioDecoderEvent.AudioFrame, frame);
},
error: (err) => {
this.emit(AudioDecoderEvent.Error, err);
this.close();
},
});
}
configure(config: AudioDecoderConfig): void {
this.config = config;
this.decoder.configure(config);
}
decode(packet: EncodedAudioChunkInit): void {
if (this.decoder.state === "configured")
this.decoder.decode(new EncodedAudioChunk(packet));
}
flush(): void {
this.decoder.flush();
}
reset(): void {
this.decoder.reset();
}
close(): void {
if (this.decoder.state !== "closed") this.decoder.close();
}
}