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 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. // @flow
  2. import { getLocalParticipant } from '../base/participants';
  3. import { extractFqnFromPath } from '../dynamic-branding';
  4. import { SET_TIMEOUT } from './constants';
  5. import logger from './logger';
  6. /**
  7. * Sends the facial expression with its duration to all the other participants.
  8. *
  9. * @param {Object} conference - The current conference.
  10. * @param {string} facialExpression - Facial expression to be sent.
  11. * @param {number} duration - The duration of the facial expression in seconds.
  12. * @returns {void}
  13. */
  14. export function sendFacialExpressionToParticipants(
  15. conference: Object,
  16. facialExpression: string,
  17. duration: number
  18. ): void {
  19. try {
  20. conference.sendEndpointMessage('', {
  21. type: 'facial_expression',
  22. facialExpression,
  23. duration
  24. });
  25. } catch (err) {
  26. logger.warn('Could not broadcast the facial expression to the other participants', err);
  27. }
  28. }
  29. /**
  30. * Sends the facial expression with its duration to xmpp server.
  31. *
  32. * @param {Object} conference - The current conference.
  33. * @param {string} facialExpression - Facial expression to be sent.
  34. * @param {number} duration - The duration of the facial expression in seconds.
  35. * @returns {void}
  36. */
  37. export function sendFacialExpressionToServer(
  38. conference: Object,
  39. facialExpression: string,
  40. duration: number
  41. ): void {
  42. try {
  43. conference.sendFacialExpression({
  44. facialExpression,
  45. duration
  46. });
  47. } catch (err) {
  48. logger.warn('Could not send the facial expression to xmpp server', err);
  49. }
  50. }
  51. /**
  52. * Sends facial expression to backend.
  53. *
  54. * @param {Object} state - Redux state.
  55. * @returns {boolean} - True if sent, false otherwise.
  56. */
  57. export async function sendFacialExpressionsWebhook(state: Object) {
  58. const { webhookProxyUrl: url } = state['features/base/config'];
  59. const { conference } = state['features/base/conference'];
  60. const { jwt } = state['features/base/jwt'];
  61. const { connection } = state['features/base/connection'];
  62. const jid = connection.getJid();
  63. const localParticipant = getLocalParticipant(state);
  64. const { facialExpressionsBuffer } = state['features/facial-recognition'];
  65. if (facialExpressionsBuffer.length === 0) {
  66. return false;
  67. }
  68. const headers = {
  69. ...jwt ? { 'Authorization': `Bearer ${jwt}` } : {},
  70. 'Content-Type': 'application/json'
  71. };
  72. const reqBody = {
  73. meetingFqn: extractFqnFromPath(),
  74. sessionId: conference.sessionId,
  75. submitted: Date.now(),
  76. emotions: facialExpressionsBuffer,
  77. participantId: localParticipant.jwtId,
  78. participantName: localParticipant.name,
  79. participantJid: jid
  80. };
  81. if (url) {
  82. try {
  83. const res = await fetch(`${url}/emotions`, {
  84. method: 'POST',
  85. headers,
  86. body: JSON.stringify(reqBody)
  87. });
  88. if (res.ok) {
  89. return true;
  90. }
  91. logger.error('Status error:', res.status);
  92. } catch (err) {
  93. logger.error('Could not send request', err);
  94. }
  95. }
  96. return false;
  97. }
  98. /**
  99. * Sends the image data a canvas from the track in the image capture to the facial expression worker.
  100. *
  101. * @param {Worker} worker - Facial expression worker.
  102. * @param {Object} imageCapture - Image capture that contains the current track.
  103. * @returns {Promise<void>}
  104. */
  105. export async function sendDataToWorker(
  106. worker: Worker,
  107. imageCapture: Object
  108. ): Promise<void> {
  109. if (imageCapture === null || imageCapture === undefined) {
  110. return;
  111. }
  112. let imageBitmap;
  113. try {
  114. imageBitmap = await imageCapture.grabFrame();
  115. } catch (err) {
  116. logger.warn(err);
  117. return;
  118. }
  119. const canvas = document.createElement('canvas');
  120. const context = canvas.getContext('2d');
  121. canvas.width = imageBitmap.width;
  122. canvas.height = imageBitmap.height;
  123. context.drawImage(imageBitmap, 0, 0);
  124. const imageData = context.getImageData(0, 0, imageBitmap.width, imageBitmap.height);
  125. worker.postMessage({
  126. type: SET_TIMEOUT,
  127. imageData
  128. });
  129. }