Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/legal-kings-fly.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@livekit/track-processors': patch
---

cache background image rather than refetch it every time it is enabled
33 changes: 18 additions & 15 deletions src/transformers/BackgroundTransformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export default class BackgroundProcessor extends VideoTransformer<BackgroundOpti

segmentationResults: vision.ImageSegmenterResult | undefined;

backgroundImage: ImageBitmap | null = null;
backgroundImageAndPath: { imageData: ImageBitmap, path: string } | null = null;

options: BackgroundOptions;

Expand Down Expand Up @@ -75,8 +75,8 @@ export default class BackgroundProcessor extends VideoTransformer<BackgroundOpti
});

// Skip loading the image here if update already loaded the image below
if (this.options?.imagePath && !this.backgroundImage) {
await this.loadBackground(this.options.imagePath).catch((err) =>
if (this.options?.imagePath) {
await this.loadAndSetBackground(this.options.imagePath).catch((err) =>
console.error('Error while loading processor background image: ', err),
);
}
Expand All @@ -88,21 +88,24 @@ export default class BackgroundProcessor extends VideoTransformer<BackgroundOpti
async destroy() {
await super.destroy();
await this.imageSegmenter?.close();
this.backgroundImage = null;
this.backgroundImageAndPath = null;
this.isFirstFrame = true;
}

async loadBackground(path: string) {
const img = new Image();
async loadAndSetBackground(path: string) {
if (!this.backgroundImageAndPath || this.backgroundImageAndPath?.path !== path) {
const img = new Image();

await new Promise((resolve, reject) => {
img.crossOrigin = 'Anonymous';
img.onload = () => resolve(img);
img.onerror = (err) => reject(err);
img.src = path;
});
const imageData = await createImageBitmap(img);
this.gl?.setBackgroundImage(imageData);
await new Promise((resolve, reject) => {
img.crossOrigin = 'Anonymous';
img.onload = () => resolve(img);
img.onerror = (err) => reject(err);
img.src = path;
});
const imageData = await createImageBitmap(img);
this.backgroundImageAndPath = { imageData, path };
}
this.gl?.setBackgroundImage(this.backgroundImageAndPath.imageData);
}

async transform(frame: VideoFrame, controller: TransformStreamDefaultController<VideoFrame>) {
Expand Down Expand Up @@ -200,7 +203,7 @@ export default class BackgroundProcessor extends VideoTransformer<BackgroundOpti

this.gl?.setBlurRadius(opts.blurRadius ?? null);
if (opts.imagePath) {
await this.loadBackground(opts.imagePath);
await this.loadAndSetBackground(opts.imagePath);
} else {
this.gl?.setBackgroundImage(null);
}
Expand Down