mirror of
https://github.com/arcodange-org/mediabunny.git
synced 2026-10-02 21:33:52 +02:00
Make use of OffscreenCanvas
This commit is contained in:
+10
-5
@@ -786,7 +786,7 @@ export class VideoFrameSink extends BaseMediaFrameSink<VideoFrame> {
|
|||||||
|
|
||||||
/** @public */
|
/** @public */
|
||||||
export type WrappedCanvas = {
|
export type WrappedCanvas = {
|
||||||
canvas: HTMLCanvasElement;
|
canvas: HTMLCanvasElement | OffscreenCanvas;
|
||||||
timestamp: number;
|
timestamp: number;
|
||||||
duration: number;
|
duration: number;
|
||||||
};
|
};
|
||||||
@@ -815,7 +815,7 @@ export class CanvasSink {
|
|||||||
/** @internal */
|
/** @internal */
|
||||||
_videoFrameSink: VideoFrameSink;
|
_videoFrameSink: VideoFrameSink;
|
||||||
/** @internal */
|
/** @internal */
|
||||||
_canvasPool: (HTMLCanvasElement | null)[];
|
_canvasPool: (HTMLCanvasElement | OffscreenCanvas | null)[];
|
||||||
/** @internal */
|
/** @internal */
|
||||||
_nextCanvasIndex = 0;
|
_nextCanvasIndex = 0;
|
||||||
|
|
||||||
@@ -885,9 +885,14 @@ export class CanvasSink {
|
|||||||
_videoFrameToWrappedCanvas(frame: WrappedVideoFrame): WrappedCanvas {
|
_videoFrameToWrappedCanvas(frame: WrappedVideoFrame): WrappedCanvas {
|
||||||
let canvas = this._canvasPool[this._nextCanvasIndex];
|
let canvas = this._canvasPool[this._nextCanvasIndex];
|
||||||
if (!canvas) {
|
if (!canvas) {
|
||||||
canvas = document.createElement('canvas');
|
if (typeof OffscreenCanvas !== 'undefined') {
|
||||||
canvas.width = this._width;
|
// Prefer an OffscreenCanvas
|
||||||
canvas.height = this._height;
|
canvas = new OffscreenCanvas(this._width, this._height);
|
||||||
|
} else {
|
||||||
|
canvas = document.createElement('canvas');
|
||||||
|
canvas.width = this._width;
|
||||||
|
canvas.height = this._height;
|
||||||
|
}
|
||||||
|
|
||||||
if (this._canvasPool.length > 0) {
|
if (this._canvasPool.length > 0) {
|
||||||
this._canvasPool[this._nextCanvasIndex] = canvas;
|
this._canvasPool[this._nextCanvasIndex] = canvas;
|
||||||
|
|||||||
+5
-2
@@ -398,8 +398,11 @@ export class CanvasSource extends VideoSource {
|
|||||||
private _canvas: HTMLCanvasElement | OffscreenCanvas;
|
private _canvas: HTMLCanvasElement | OffscreenCanvas;
|
||||||
|
|
||||||
constructor(canvas: HTMLCanvasElement | OffscreenCanvas, encodingConfig: VideoEncodingConfig) {
|
constructor(canvas: HTMLCanvasElement | OffscreenCanvas, encodingConfig: VideoEncodingConfig) {
|
||||||
if (!(canvas instanceof HTMLCanvasElement)) {
|
if (
|
||||||
throw new TypeError('canvas must be an HTMLCanvasElement.');
|
!(typeof HTMLCanvasElement !== 'undefined' && canvas instanceof HTMLCanvasElement)
|
||||||
|
&& !(typeof OffscreenCanvas !== 'undefined' && canvas instanceof OffscreenCanvas)
|
||||||
|
) {
|
||||||
|
throw new TypeError('canvas must be an HTMLCanvasElement or OffscreenCanvas.');
|
||||||
}
|
}
|
||||||
validateVideoEncodingConfig(encodingConfig);
|
validateVideoEncodingConfig(encodingConfig);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user