twilio / twilio-video-processors.js

Twilio Video Processors is a collection of video processing tools which can be used with Twilio Video JavaScript SDK to apply transformations and filters to a video track.
Other
33 stars 22 forks source link

Black screen when using remote image url with VirtualBackgroundProcessor #37

Closed iamhrayr closed 2 years ago

iamhrayr commented 2 years ago

Code to reproduce the issue:

const getImage = (imageUrl: string) => {
  return new Promise((resolve, reject) => {
    const img = new Image();
    img.src = imageUrl; // <-- some public url, e.g. https://cdn.pixabay.com/photo/2021/11/21/21/14/mountain-6815304_960_720.jpg
    img.onload = () => resolve(img);
    img.onerror = reject;
  });
};

// ...

const virtualBackgroundProcessor = new VirtualBackgroundProcessor({
  assetsPath: virtualBackgroundAssets,
  backgroundImage: await getImage(remoteImageUrl),
  fitType: ImageFit.Cover,
});
await virtualBackgroundProcessor.loadModel();
addProcessor(virtualBackgroundProcessor);

Expected behavior: IT should show the correct background or throw some error in case any.

Actual behavior: Shows black screen

Software versions:

charliesantos commented 2 years ago

Hi @iamhrayr , Thanks for reporting. What is your application's domain? If it's different than your image's domain, you are probably running into cross origin issues. Please let me know what location.origin returns from your dev console.

If it's cross origin, please check this section of our docs when loading an image from a different origin. Basically, you need to make sure the server hosting your image should return the correct CORS headers, and set the crossOrigin attribute of the image object.

const getImage = (imageUrl: string) => {
  return new Promise((resolve, reject) => {
    const img = new Image();
    img.crossOrigin = 'Anonymous'; // <-- IMPORTANT if serving from a different origin
    img.src = imageUrl;
    img.onload = () => resolve(img);
    img.onerror = reject;
  });
};

Unfortunately, you will not see an error when you run into this kind of issue. The browser fails silently when running into CORS issues when loading an image to a canvas.

iamhrayr commented 2 years ago

@charliesantos thanks for the quick response. Yeah, it was the problem. After configuring the CORS of the server everything works as expected

closing the issue since it is not related to this library.