You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

functions.js 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. // @flow
  2. import { JitsiTrackEvents } from '../base/lib-jitsi-meet';
  3. import { toggleBackgroundEffect } from './actions';
  4. let filterSupport;
  5. /**
  6. * Checks context filter support.
  7. *
  8. * @returns {boolean} True if the filter is supported and false if the filter is not supported by the browser.
  9. */
  10. export function checkBlurSupport() {
  11. if (typeof filterSupport === 'undefined') {
  12. const canvas = document.createElement('canvas');
  13. const ctx = canvas.getContext('2d');
  14. filterSupport = typeof ctx.filter !== 'undefined';
  15. canvas.remove();
  16. }
  17. return filterSupport;
  18. }
  19. /**
  20. * Convert blob to base64.
  21. *
  22. * @param {Blob} blob - The link to add info with.
  23. * @returns {Promise<string>}
  24. */
  25. export const blobToData = (blob: Blob): Promise<string> =>
  26. new Promise(resolve => {
  27. const reader = new FileReader();
  28. reader.onloadend = () => resolve(reader.result.toString());
  29. reader.readAsDataURL(blob);
  30. });
  31. /**
  32. * Convert blob to base64.
  33. *
  34. * @param {string} url - The image url.
  35. * @returns {Object} - Returns the converted blob to base64.
  36. */
  37. export const toDataURL = async (url: string) => {
  38. const response = await fetch(url);
  39. const blob = await response.blob();
  40. const resData = await blobToData(blob);
  41. return resData;
  42. };
  43. /**
  44. * Resize image and adjust original aspect ratio.
  45. *
  46. * @param {Object} base64image - Base64 image extraction.
  47. * @param {number} width - Value for resizing the image width.
  48. * @param {number} height - Value for resizing the image height.
  49. * @returns {Promise<string>}
  50. *
  51. */
  52. export function resizeImage(base64image: any, width: number = 1920, height: number = 1080): Promise<string> {
  53. // In order to work on Firefox browser we need to handle the asynchronous nature of image loading; We need to use
  54. // a promise mechanism. The reason why it 'works' without this mechanism in Chrome is actually 'by accident' because
  55. // the image happens to be in the cache and the browser is able to deliver the uncompressed/decoded image
  56. // before using the image in the drawImage call.
  57. return new Promise(resolve => {
  58. const img = document.createElement('img');
  59. img.onload = function() {
  60. // Create an off-screen canvas.
  61. const canvas = document.createElement('canvas');
  62. // Set its dimension to target size.
  63. const context = canvas.getContext('2d');
  64. canvas.width = width;
  65. canvas.height = height;
  66. // Draw source image into the off-screen canvas.
  67. // TODO: keep aspect ratio and implement object-fit: cover.
  68. context.drawImage(img, 0, 0, width, height);
  69. // Encode image to data-uri with base64 version of compressed image.
  70. resolve(canvas.toDataURL('image/jpeg', 0.5));
  71. };
  72. img.src = base64image;
  73. });
  74. }
  75. /**
  76. * Check if the local desktop track was stopped and apply none option on virtual background.
  77. *
  78. * @param {Function} dispatch - The Redux dispatch function.
  79. * @param {Object} desktopTrack - The desktop track that needs to be checked if it was stopped.
  80. * @param {Object} currentLocalTrack - The current local track where we apply none virtual
  81. * background option if the desktop track was stopped.
  82. * @returns {Promise}
  83. */
  84. export function localTrackStopped(dispatch: Function, desktopTrack: Object, currentLocalTrack: Object) {
  85. const noneOptions = {
  86. enabled: false,
  87. backgroundType: 'none',
  88. selectedThumbnail: 'none',
  89. backgroundEffectEnabled: false
  90. };
  91. desktopTrack
  92. && desktopTrack.on(JitsiTrackEvents.LOCAL_TRACK_STOPPED, () => {
  93. dispatch(toggleBackgroundEffect(noneOptions, currentLocalTrack));
  94. });
  95. }