Skip to content

VIDEO-6368 | wait to load image before removing processor#570

Merged
gabriel-jt merged 2 commits intofeature/virtualbackgroundfrom
VIDEO-6368
Aug 3, 2021
Merged

VIDEO-6368 | wait to load image before removing processor#570
gabriel-jt merged 2 commits intofeature/virtualbackgroundfrom
VIDEO-6368

Conversation

@gabriel-jt
Copy link
Copy Markdown
Contributor

@gabriel-jt gabriel-jt commented Aug 2, 2021

Contributing to Twilio

All third party contributors acknowledge that any contributions they provide will be made under the same open source license that the open source project is provided under.

  • I acknowledge that all my contributions will be made under the project's license.

Pull Request Details

JIRA link(s):

Description

Wait to load images for virtual background before removing the processor

Burndown

Before review

  • Updated CHANGELOG.md if necessary
  • Added unit tests if necessary
  • Updated affected documentation
  • Verified locally with npm test
  • Manually sanity tested running locally
  • Included screenshot as PR comment (if needed)
  • Ready for review

Before merge

  • Got one or more +1s
  • Re-tested if necessary

Copy link
Copy Markdown
Collaborator

@charliesantos charliesantos left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your changes works! However, it's not immediately obvious what the code is trying to do. What do you think of my suggestion below? Basically, I created addProcessor and removeProcessor helper functions and just use those. It reduces the nested if statements, and also prevents removing and adding processors if the processor hasn't changed.

Helper functions outside of your useEffect hook

const removeProcessor = () => {
  if (videoTrack && videoTrack.processor) {
    videoTrack.removeProcessor(videoTrack.processor);
  }
};

const addProcessor = (processor: GaussianBlurBackgroundProcessor | VirtualBackgroundProcessor) => {
  if (!videoTrack || videoTrack.processor === processor) {
    return;
  }
  removeProcessor();
  videoTrack.addProcessor(processor);
};

Handler now looks like this

const handleProcessorChange = async () => {
  if (!blurProcessor) {
    blurProcessor = new GaussianBlurBackgroundProcessor({
      assetsPath: virtualBackgroundAssets,
    });
    await blurProcessor.loadModel();
  }
  if (!virtualBackgroundProcessor) {
    virtualBackgroundProcessor = new VirtualBackgroundProcessor({
      assetsPath: virtualBackgroundAssets,
      backgroundImage: await getImage(0),
      fitType: ImageFit.Cover,
    });
    await virtualBackgroundProcessor.loadModel();
  }

  if (!room?.localParticipant) {
    return;
  }

  if (backgroundSettings.type === 'blur') {
    addProcessor(blurProcessor);
  } else if (backgroundSettings.type === 'image' && typeof backgroundSettings.index === 'number') {
    virtualBackgroundProcessor.backgroundImage = await getImage(backgroundSettings.index);
    addProcessor(virtualBackgroundProcessor);
  } else {
    removeProcessor();
  }
};

@gabriel-jt
Copy link
Copy Markdown
Contributor Author

gabriel-jt commented Aug 3, 2021

Your changes works! However, it's not immediately obvious what the code is trying to do. What do you think of my suggestion below? Basically, I created addProcessor and removeProcessor helper functions and just use those. It reduces the nested if statements, and also prevents removing and adding processors if the processor hasn't changed.

Helper functions outside of your useEffect hook

const removeProcessor = () => {
  if (videoTrack && videoTrack.processor) {
    videoTrack.removeProcessor(videoTrack.processor);
  }
};

const addProcessor = (processor: GaussianBlurBackgroundProcessor | VirtualBackgroundProcessor) => {
  if (!videoTrack || videoTrack.processor === processor) {
    return;
  }
  removeProcessor();
  videoTrack.addProcessor(processor);
};

Handler now looks like this

const handleProcessorChange = async () => {
  if (!blurProcessor) {
    blurProcessor = new GaussianBlurBackgroundProcessor({
      assetsPath: virtualBackgroundAssets,
    });
    await blurProcessor.loadModel();
  }
  if (!virtualBackgroundProcessor) {
    virtualBackgroundProcessor = new VirtualBackgroundProcessor({
      assetsPath: virtualBackgroundAssets,
      backgroundImage: await getImage(0),
      fitType: ImageFit.Cover,
    });
    await virtualBackgroundProcessor.loadModel();
  }

  if (!room?.localParticipant) {
    return;
  }

  if (backgroundSettings.type === 'blur') {
    addProcessor(blurProcessor);
  } else if (backgroundSettings.type === 'image' && typeof backgroundSettings.index === 'number') {
    virtualBackgroundProcessor.backgroundImage = await getImage(backgroundSettings.index);
    addProcessor(virtualBackgroundProcessor);
  } else {
    removeProcessor();
  }
};

Thanks for the suggestions @charliesantos ! I wrapped the helper functions inside a useCallback hook to prevent warnings about missing dependencies in the useEffect hook

Copy link
Copy Markdown
Contributor

@timmydoza timmydoza left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work! I used Chrome's dev tools to throttle my network connection so the background images loaded slowly, and the changes here worked well. 👍

@gabriel-jt gabriel-jt merged commit 4efe807 into feature/virtualbackground Aug 3, 2021
charliesantos pushed a commit that referenced this pull request Aug 9, 2021
* Moved more menu to the center of the menu bar

* VIDEO-5731 | Added background option to More Menu, selection dialog, and unit tests (#6)

Added background option to More Menu, selection dialog, and unit tests
Co-authored-by: Gabe Espinosa <gespinosa@twilio.com>

* VIDEO-5733 | Added npm script to copy video processor sdk assets to public folder (#8)

* Added npm script to copy video processor sdk assets to public folder

* added postinstall npm script

Co-authored-by: Gabe Espinosa <gespinosa@twilio.com>

* added rimraf and copyfiles dependency to package.json

* VIDEO-5732 | Added background selection UI (#7)

Added background selection UI

Co-authored-by: Gabe Espinosa <gespinosa@twilio.com>
Co-authored-by: timmydoza <tmendoza@twilio.com>

* VIDEO-5734/blur background feature (#550)

Implemented blur background feature (useBackgroundSettings hook and tests)
Co-authored-by: Gabe Espinosa <gespinosa@twilio.com>

* VIDEO-5735/Add virtual background feature (#557)

Added virtual background feature and unit tests
Co-authored-by: Gabe Espinosa <gespinosa@twilio.com>

* VIDEO-5735 | Add isSupported check and unit test (#560)

add isSupported check and unit test
Co-authored-by: Gabe Espinosa <gespinosa@twilio.com>

* VIDEO-6368 | wait to load image before removing processor (#570)

* wait to load image before removing processor
Co-authored-by: Gabe Espinosa <gespinosa@twilio.com>

* updated twilio-video sdk

* fixed breaking tests

* update package.json version and changelog

* update change log, package.json, and revisions

* Update CHANGELOG.md

Co-authored-by: Gabe Espinosa <gespinosa@twilio.com>
Co-authored-by: timmydoza <tmendoza@twilio.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants