cleanup failure cases in stream player worker

This commit is contained in:
Lily Tsuru 2024-09-09 05:55:24 -04:00
parent 86490ed80b
commit 085a985c13

View file

@ -24,15 +24,15 @@ class CanvasRenderer {
}
// player logic
class VideoPlayer {
class VideoStreamPlayer {
private renderer: CanvasRenderer | null = null;
private pendingFrame: VideoFrame | null = null;
private decoder: VideoDecoder | null = null;
private streamInitSPS: SPS | null = null;
// only async for isConfigSupported
async onData(value: ArrayBuffer) {
let u8ar = new Uint8Array(value);
// only async for VideoStreamPlayer#configureDecoder
async onVideoData(buffer: ArrayBuffer) {
let u8ar = new Uint8Array(buffer);
let stream = new NALUStream(u8ar, {
type: "annexB",
@ -43,7 +43,7 @@ class VideoPlayer {
for (const nalu of stream) {
// Try and obtain the base SPS required to initalize the video decoder
// (if we didn't get one yet)
// (if we didn't get one yet). Once we have one we try configuring the decoder
if (this.streamInitSPS == null) {
try {
let sps = new SPS(nalu);
@ -69,7 +69,7 @@ class VideoPlayer {
let frame = new EncodedVideoChunk({
type: key ? "key" : "delta",
data: value,
data: buffer,
// munge the PTS so that frames are always
// played as soon as possible
@ -77,7 +77,7 @@ class VideoPlayer {
duration: performance.now(),
// do the webcodecs typings seriously still not have this
transfer: [value],
transfer: [buffer],
} as any);
this.decoder?.decode(frame);
@ -138,13 +138,15 @@ class VideoPlayer {
"Browser doesn't like hardware preference, removing it and trying again"
);
// Remove the property and try again.
// If the browser STILL doesn't like it we give up.
// Remove the property for hardware preference and try again.
delete config.hardwareAcceleration;
supportedConfig = await VideoDecoder.isConfigSupported(config);
if (!supportedConfig.supported) return;
if (!supportedConfig.supported) {
await this.shutdownDecoder();
throw new Error("I give up, the browser doesn't like no preference either.");
}
configMessage.usingHwDecode = false;
this.decoder?.configure(supportedConfig.config!);
@ -179,7 +181,7 @@ class VideoPlayer {
}
}
let player = new VideoPlayer();
let player = new VideoStreamPlayer();
async function onMessage(msg: PlayerInputMessage) {
switch (msg.type) {
@ -195,7 +197,7 @@ async function onMessage(msg: PlayerInputMessage) {
break;
case "data":
await player.onData((msg as PlayerVideoDataMessage).data);
await player.onVideoData((msg as PlayerVideoDataMessage).data);
break;
case "shutdown-decoder":