Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

JitsiStreamBlurEffect.js 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. // @flow
  2. import * as bodyPix from '@tensorflow-models/body-pix';
  3. import {
  4. CLEAR_INTERVAL,
  5. INTERVAL_TIMEOUT,
  6. SET_INTERVAL,
  7. timerWorkerScript
  8. } from './TimerWorker';
  9. /**
  10. * Represents a modified MediaStream that adds blur to video background.
  11. * <tt>JitsiStreamBlurEffect</tt> does the processing of the original
  12. * video stream.
  13. */
  14. export default class JitsiStreamBlurEffect {
  15. _bpModel: Object;
  16. _inputVideoElement: HTMLVideoElement;
  17. _onMaskFrameTimer: Function;
  18. _maskFrameTimerWorker: Worker;
  19. _maskInProgress: boolean;
  20. _outputCanvasElement: HTMLCanvasElement;
  21. _renderMask: Function;
  22. _segmentationData: Object;
  23. isEnabled: Function;
  24. startEffect: Function;
  25. stopEffect: Function;
  26. /**
  27. * Represents a modified video MediaStream track.
  28. *
  29. * @class
  30. * @param {BodyPix} bpModel - BodyPix model.
  31. */
  32. constructor(bpModel: Object) {
  33. this._bpModel = bpModel;
  34. // Bind event handler so it is only bound once for every instance.
  35. this._onMaskFrameTimer = this._onMaskFrameTimer.bind(this);
  36. // Workaround for FF issue https://bugzilla.mozilla.org/show_bug.cgi?id=1388974
  37. this._outputCanvasElement = document.createElement('canvas');
  38. this._outputCanvasElement.getContext('2d');
  39. this._inputVideoElement = document.createElement('video');
  40. this._maskFrameTimerWorker = new Worker(timerWorkerScript, { name: 'Blur effect worker' });
  41. this._maskFrameTimerWorker.onmessage = this._onMaskFrameTimer;
  42. }
  43. /**
  44. * EventHandler onmessage for the maskFrameTimerWorker WebWorker.
  45. *
  46. * @private
  47. * @param {EventHandler} response - The onmessage EventHandler parameter.
  48. * @returns {void}
  49. */
  50. async _onMaskFrameTimer(response: Object) {
  51. if (response.data.id === INTERVAL_TIMEOUT) {
  52. if (!this._maskInProgress) {
  53. await this._renderMask();
  54. }
  55. }
  56. }
  57. /**
  58. * Loop function to render the background mask.
  59. *
  60. * @private
  61. * @returns {void}
  62. */
  63. async _renderMask() {
  64. this._maskInProgress = true;
  65. this._segmentationData = await this._bpModel.segmentPerson(this._inputVideoElement, {
  66. internalResolution: 'medium', // resized to 0.5 times of the original resolution before inference
  67. maxDetections: 1, // max. number of person poses to detect per image
  68. segmentationThreshold: 0.7 // represents probability that a pixel belongs to a person
  69. });
  70. this._maskInProgress = false;
  71. bodyPix.drawBokehEffect(
  72. this._outputCanvasElement,
  73. this._inputVideoElement,
  74. this._segmentationData,
  75. 12, // Constant for background blur, integer values between 0-20
  76. 7 // Constant for edge blur, integer values between 0-20
  77. );
  78. }
  79. /**
  80. * Checks if the local track supports this effect.
  81. *
  82. * @param {JitsiLocalTrack} jitsiLocalTrack - Track to apply effect.
  83. * @returns {boolean} - Returns true if this effect can run on the specified track
  84. * false otherwise.
  85. */
  86. isEnabled(jitsiLocalTrack: Object) {
  87. return jitsiLocalTrack.isVideoTrack() && jitsiLocalTrack.videoType === 'camera';
  88. }
  89. /**
  90. * Starts loop to capture video frame and render the segmentation mask.
  91. *
  92. * @param {MediaStream} stream - Stream to be used for processing.
  93. * @returns {MediaStream} - The stream with the applied effect.
  94. */
  95. startEffect(stream: MediaStream) {
  96. const firstVideoTrack = stream.getVideoTracks()[0];
  97. const { height, frameRate, width }
  98. = firstVideoTrack.getSettings ? firstVideoTrack.getSettings() : firstVideoTrack.getConstraints();
  99. this._outputCanvasElement.width = parseInt(width, 10);
  100. this._outputCanvasElement.height = parseInt(height, 10);
  101. this._inputVideoElement.width = parseInt(width, 10);
  102. this._inputVideoElement.height = parseInt(height, 10);
  103. this._inputVideoElement.autoplay = true;
  104. this._inputVideoElement.srcObject = stream;
  105. this._inputVideoElement.onloadeddata = () => {
  106. this._maskFrameTimerWorker.postMessage({
  107. id: SET_INTERVAL,
  108. timeMs: 1000 / parseInt(frameRate, 10)
  109. });
  110. };
  111. return this._outputCanvasElement.captureStream(parseInt(frameRate, 10));
  112. }
  113. /**
  114. * Stops the capture and render loop.
  115. *
  116. * @returns {void}
  117. */
  118. stopEffect() {
  119. this._maskFrameTimerWorker.postMessage({
  120. id: CLEAR_INTERVAL
  121. });
  122. }
  123. }