123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147 |
- // @flow
- import { getLocalParticipant } from '../base/participants';
- import { extractFqnFromPath } from '../dynamic-branding';
-
- import { SET_TIMEOUT } from './constants';
- import logger from './logger';
-
- /**
- * Sends the facial expression with its duration to all the other participants.
- *
- * @param {Object} conference - The current conference.
- * @param {string} facialExpression - Facial expression to be sent.
- * @param {number} duration - The duration of the facial expression in seconds.
- * @returns {void}
- */
- export function sendFacialExpressionToParticipants(
- conference: Object,
- facialExpression: string,
- duration: number
- ): void {
- try {
- conference.sendEndpointMessage('', {
- type: 'facial_expression',
- facialExpression,
- duration
- });
- } catch (err) {
- logger.warn('Could not broadcast the facial expression to the other participants', err);
- }
-
- }
-
- /**
- * Sends the facial expression with its duration to xmpp server.
- *
- * @param {Object} conference - The current conference.
- * @param {string} facialExpression - Facial expression to be sent.
- * @param {number} duration - The duration of the facial expression in seconds.
- * @returns {void}
- */
- export function sendFacialExpressionToServer(
- conference: Object,
- facialExpression: string,
- duration: number
- ): void {
- try {
- conference.sendFacialExpression({
- facialExpression,
- duration
- });
- } catch (err) {
- logger.warn('Could not send the facial expression to xmpp server', err);
- }
- }
-
- /**
- * Sends facial expression to backend.
- *
- * @param {Object} state - Redux state.
- * @returns {boolean} - True if sent, false otherwise.
- */
- export async function sendFacialExpressionsWebhook(state: Object) {
- const { webhookProxyUrl: url } = state['features/base/config'];
- const { conference } = state['features/base/conference'];
- const { jwt } = state['features/base/jwt'];
- const { connection } = state['features/base/connection'];
- const jid = connection.getJid();
- const localParticipant = getLocalParticipant(state);
- const { facialExpressionsBuffer } = state['features/facial-recognition'];
-
- if (facialExpressionsBuffer.length === 0) {
- return false;
- }
-
- const headers = {
- ...jwt ? { 'Authorization': `Bearer ${jwt}` } : {},
- 'Content-Type': 'application/json'
- };
-
- const reqBody = {
- meetingFqn: extractFqnFromPath(),
- sessionId: conference.sessionId,
- submitted: Date.now(),
- emotions: facialExpressionsBuffer,
- participantId: localParticipant.jwtId,
- participantName: localParticipant.name,
- participantJid: jid
- };
-
- if (url) {
- try {
- const res = await fetch(`${url}/emotions`, {
- method: 'POST',
- headers,
- body: JSON.stringify(reqBody)
- });
-
- if (res.ok) {
- return true;
- }
- logger.error('Status error:', res.status);
- } catch (err) {
- logger.error('Could not send request', err);
- }
- }
-
- return false;
- }
-
- /**
- * Sends the image data a canvas from the track in the image capture to the facial expression worker.
- *
- * @param {Worker} worker - Facial expression worker.
- * @param {Object} imageCapture - Image capture that contains the current track.
- * @returns {Promise<void>}
- */
- export async function sendDataToWorker(
- worker: Worker,
- imageCapture: Object
- ): Promise<void> {
- if (imageCapture === null || imageCapture === undefined) {
- return;
- }
- let imageBitmap;
-
- try {
- imageBitmap = await imageCapture.grabFrame();
- } catch (err) {
- logger.warn(err);
-
- return;
- }
-
- const canvas = document.createElement('canvas');
- const context = canvas.getContext('2d');
-
- canvas.width = imageBitmap.width;
- canvas.height = imageBitmap.height;
- context.drawImage(imageBitmap, 0, 0);
-
- const imageData = context.getImageData(0, 0, imageBitmap.width, imageBitmap.height);
-
- worker.postMessage({
- type: SET_TIMEOUT,
- imageData
- });
- }
|