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