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