make rendering abstract (so that a webgl renderer will actually be possible)

This commit is contained in:
Lily Tsuru 2024-09-09 06:19:20 -04:00
parent b44a55f10d
commit a6734667de
3 changed files with 167 additions and 156 deletions

View file

@ -1,10 +1,11 @@
// shared interface to allow gl later :) import { CanvasRenderer } from "./canvas_renderer";
export class Canvas2DRenderer {
private canvas: OffscreenCanvas; // renderer for the streamplayer that uses the canvas2d apis
export class Canvas2DRenderer extends CanvasRenderer {
private ctx: OffscreenCanvasRenderingContext2D; private ctx: OffscreenCanvasRenderingContext2D;
constructor(c: OffscreenCanvas) { constructor(c: OffscreenCanvas) {
this.canvas = c; super(c);
this.ctx = this.canvas.getContext("2d")!; this.ctx = this.canvas.getContext("2d")!;
} }

View file

@ -0,0 +1,10 @@
// mixin thing
export abstract class CanvasRenderer {
protected canvas: OffscreenCanvas;
constructor(c: OffscreenCanvas) {
this.canvas = c;
}
abstract draw(frame: VideoFrame): void;
}

View file

@ -1,4 +1,3 @@
import { NALUStream, SPS, Slice } from "h264-interp-utils"; import { NALUStream, SPS, Slice } from "h264-interp-utils";
import { import {
PlayerInputMessage, PlayerInputMessage,
@ -6,11 +5,11 @@ import {
PlayerInitMessage, PlayerInitMessage,
PlayerVideoDataMessage, PlayerVideoDataMessage,
} from "./stream_worker_messages"; } from "./stream_worker_messages";
import { Canvas2DRenderer } from "./canvas_2d_renderer"; import { CanvasRenderer } from "./canvas_renderer";
// player logic // player logic
export class VideoStreamPlayer { export class VideoStreamPlayer {
private renderer: Canvas2DRenderer | 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;
@ -130,7 +129,9 @@ export class VideoStreamPlayer {
if (!supportedConfig.supported) { if (!supportedConfig.supported) {
await this.shutdownDecoder(); await this.shutdownDecoder();
throw new Error("I give up, the browser doesn't like no preference either."); throw new Error(
"I give up, the browser doesn't like no preference either."
);
} }
configMessage.usingHwDecode = false; configMessage.usingHwDecode = false;
@ -161,8 +162,7 @@ export class VideoStreamPlayer {
return this.renderer !== null; return this.renderer !== null;
} }
setRenderer(r: Canvas2DRenderer) { setRenderer(r: CanvasRenderer) {
this.renderer = r; this.renderer = r;
} }
} }