2018-08-06 09:24:37 +00:00
|
|
|
// @flow
|
|
|
|
|
2018-07-17 17:31:12 +00:00
|
|
|
import { MiddlewareRegistry } from '../base/redux';
|
|
|
|
|
2018-08-23 19:57:12 +00:00
|
|
|
import {
|
|
|
|
ENDPOINT_MESSAGE_RECEIVED,
|
|
|
|
TOGGLE_REQUESTING_SUBTITLES
|
|
|
|
} from './actionTypes';
|
2020-05-20 10:57:03 +00:00
|
|
|
import {
|
|
|
|
removeTranscriptMessage,
|
|
|
|
updateTranscriptMessage
|
|
|
|
} from './actions';
|
2019-08-21 14:50:00 +00:00
|
|
|
import logger from './logger';
|
2018-07-17 17:31:12 +00:00
|
|
|
|
2018-08-06 09:24:37 +00:00
|
|
|
/**
|
|
|
|
* The type of json-message which indicates that json carries a
|
|
|
|
* transcription result.
|
|
|
|
*/
|
|
|
|
const JSON_TYPE_TRANSCRIPTION_RESULT = 'transcription-result';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The type of json-message which indicates that json carries a
|
|
|
|
* translation result.
|
|
|
|
*/
|
|
|
|
const JSON_TYPE_TRANSLATION_RESULT = 'translation-result';
|
|
|
|
|
2018-08-22 17:49:58 +00:00
|
|
|
/**
|
|
|
|
* The local participant property which is used to set whether the local
|
|
|
|
* participant wants to have a transcriber in the room.
|
|
|
|
*/
|
|
|
|
const P_NAME_REQUESTING_TRANSCRIPTION = 'requestingTranscription';
|
|
|
|
|
2018-08-23 19:57:12 +00:00
|
|
|
/**
|
|
|
|
* The local participant property which is used to store the language
|
|
|
|
* preference for translation for a participant.
|
|
|
|
*/
|
|
|
|
const P_NAME_TRANSLATION_LANGUAGE = 'translation_language';
|
|
|
|
|
2018-07-17 17:31:12 +00:00
|
|
|
/**
|
|
|
|
* Time after which the rendered subtitles will be removed.
|
|
|
|
*/
|
|
|
|
const REMOVE_AFTER_MS = 3000;
|
|
|
|
|
|
|
|
/**
|
2018-08-23 19:57:12 +00:00
|
|
|
* Middleware that catches actions related to transcript messages to be rendered
|
|
|
|
* in {@link Captions}.
|
2018-07-17 17:31:12 +00:00
|
|
|
*
|
2018-08-23 19:57:12 +00:00
|
|
|
* @param {Store} store - The redux store.
|
2018-07-17 17:31:12 +00:00
|
|
|
* @returns {Function}
|
|
|
|
*/
|
|
|
|
MiddlewareRegistry.register(store => next => action => {
|
|
|
|
switch (action.type) {
|
|
|
|
case ENDPOINT_MESSAGE_RECEIVED:
|
|
|
|
return _endpointMessageReceived(store, next, action);
|
2018-08-23 19:57:12 +00:00
|
|
|
|
2018-08-22 17:49:58 +00:00
|
|
|
case TOGGLE_REQUESTING_SUBTITLES:
|
|
|
|
_requestingSubtitlesToggled(store);
|
|
|
|
break;
|
2018-07-17 17:31:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return next(action);
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Notifies the feature transcription that the action
|
|
|
|
* {@code ENDPOINT_MESSAGE_RECEIVED} is being dispatched within a specific redux
|
|
|
|
* store.
|
|
|
|
*
|
|
|
|
* @param {Store} store - The redux store in which the specified {@code action}
|
|
|
|
* is being dispatched.
|
|
|
|
* @param {Dispatch} next - The redux {@code dispatch} function to
|
|
|
|
* dispatch the specified {@code action} to the specified {@code store}.
|
|
|
|
* @param {Action} action - The redux action {@code ENDPOINT_MESSAGE_RECEIVED}
|
|
|
|
* which is being dispatched in the specified {@code store}.
|
|
|
|
* @private
|
|
|
|
* @returns {Object} The value returned by {@code next(action)}.
|
|
|
|
*/
|
|
|
|
function _endpointMessageReceived({ dispatch, getState }, next, action) {
|
2018-08-23 19:57:12 +00:00
|
|
|
const { json } = action;
|
|
|
|
|
|
|
|
if (!(json
|
|
|
|
&& (json.type === JSON_TYPE_TRANSCRIPTION_RESULT
|
|
|
|
|| json.type === JSON_TYPE_TRANSLATION_RESULT))) {
|
2018-08-07 16:03:31 +00:00
|
|
|
return next(action);
|
|
|
|
}
|
|
|
|
|
2018-08-23 19:57:12 +00:00
|
|
|
const state = getState();
|
2018-08-06 09:24:37 +00:00
|
|
|
const translationLanguage
|
2018-08-23 19:57:12 +00:00
|
|
|
= state['features/base/conference'].conference
|
2018-08-06 09:24:37 +00:00
|
|
|
.getLocalParticipantProperty(P_NAME_TRANSLATION_LANGUAGE);
|
2018-07-17 17:31:12 +00:00
|
|
|
|
|
|
|
try {
|
2018-08-06 09:24:37 +00:00
|
|
|
const transcriptMessageID = json.message_id;
|
|
|
|
const participantName = json.participant.name;
|
|
|
|
|
|
|
|
if (json.type === JSON_TYPE_TRANSLATION_RESULT
|
2018-08-23 19:57:12 +00:00
|
|
|
&& json.language === translationLanguage) {
|
2018-08-06 09:24:37 +00:00
|
|
|
// Displays final results in the target language if translation is
|
|
|
|
// enabled.
|
|
|
|
|
|
|
|
const newTranscriptMessage = {
|
2018-08-23 19:57:12 +00:00
|
|
|
clearTimeOut: undefined,
|
2018-08-06 21:30:50 +00:00
|
|
|
final: json.text,
|
2018-08-23 19:57:12 +00:00
|
|
|
participantName
|
2018-08-06 09:24:37 +00:00
|
|
|
};
|
|
|
|
|
2018-08-23 19:57:12 +00:00
|
|
|
_setClearerOnTranscriptMessage(dispatch,
|
2018-08-06 21:30:50 +00:00
|
|
|
transcriptMessageID, newTranscriptMessage);
|
2018-08-06 09:24:37 +00:00
|
|
|
dispatch(updateTranscriptMessage(transcriptMessageID,
|
|
|
|
newTranscriptMessage));
|
|
|
|
|
|
|
|
} else if (json.type === JSON_TYPE_TRANSCRIPTION_RESULT
|
2018-08-23 19:57:12 +00:00
|
|
|
&& !translationLanguage) {
|
2018-08-06 09:24:37 +00:00
|
|
|
// Displays interim and final results without any translation if
|
|
|
|
// translations are disabled.
|
2018-07-17 17:31:12 +00:00
|
|
|
|
2018-08-23 19:57:12 +00:00
|
|
|
const { text } = json.transcript[0];
|
2018-08-06 09:24:37 +00:00
|
|
|
|
|
|
|
// We update the previous transcript message with the same
|
|
|
|
// message ID or adds a new transcript message if it does not
|
|
|
|
// exist in the map.
|
2018-08-23 19:57:12 +00:00
|
|
|
const newTranscriptMessage = {
|
|
|
|
...state['features/subtitles']._transcriptMessages
|
|
|
|
.get(transcriptMessageID)
|
|
|
|
|| { participantName }
|
|
|
|
};
|
2018-07-17 17:31:12 +00:00
|
|
|
|
2018-08-23 19:57:12 +00:00
|
|
|
_setClearerOnTranscriptMessage(dispatch,
|
2018-08-06 21:30:50 +00:00
|
|
|
transcriptMessageID, newTranscriptMessage);
|
|
|
|
|
2018-07-17 17:31:12 +00:00
|
|
|
// If this is final result, update the state as a final result
|
|
|
|
// and start a count down to remove the subtitle from the state
|
2018-08-23 19:57:12 +00:00
|
|
|
if (!json.is_interim) {
|
2018-07-17 17:31:12 +00:00
|
|
|
newTranscriptMessage.final = text;
|
2018-08-22 17:49:58 +00:00
|
|
|
|
2018-08-23 19:57:12 +00:00
|
|
|
} else if (json.stability > 0.85) {
|
2018-07-17 17:31:12 +00:00
|
|
|
// If the message has a high stability, we can update the
|
|
|
|
// stable field of the state and remove the previously
|
|
|
|
// unstable results
|
|
|
|
newTranscriptMessage.stable = text;
|
|
|
|
newTranscriptMessage.unstable = undefined;
|
2018-08-22 17:49:58 +00:00
|
|
|
|
2018-07-17 17:31:12 +00:00
|
|
|
} else {
|
|
|
|
// Otherwise, this result has an unstable result, which we
|
|
|
|
// add to the state. The unstable result will be appended
|
|
|
|
// after the stable part.
|
|
|
|
newTranscriptMessage.unstable = text;
|
|
|
|
}
|
2018-08-23 19:57:12 +00:00
|
|
|
|
|
|
|
dispatch(
|
|
|
|
updateTranscriptMessage(
|
|
|
|
transcriptMessageID,
|
|
|
|
newTranscriptMessage));
|
2018-07-17 17:31:12 +00:00
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
logger.error('Error occurred while updating transcriptions\n', error);
|
|
|
|
}
|
|
|
|
|
|
|
|
return next(action);
|
|
|
|
}
|
2018-08-06 21:30:50 +00:00
|
|
|
|
2018-08-23 19:57:12 +00:00
|
|
|
/**
|
|
|
|
* Toggle the local property 'requestingTranscription'. This will cause Jicofo
|
|
|
|
* and Jigasi to decide whether the transcriber needs to be in the room.
|
|
|
|
*
|
|
|
|
* @param {Store} store - The redux store.
|
|
|
|
* @private
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
function _requestingSubtitlesToggled({ getState }) {
|
|
|
|
const state = getState();
|
|
|
|
const { _requestingSubtitles } = state['features/subtitles'];
|
|
|
|
const { conference } = state['features/base/conference'];
|
|
|
|
|
|
|
|
conference.setLocalParticipantProperty(
|
|
|
|
P_NAME_REQUESTING_TRANSCRIPTION,
|
|
|
|
!_requestingSubtitles);
|
|
|
|
}
|
|
|
|
|
2018-08-06 21:30:50 +00:00
|
|
|
/**
|
|
|
|
* Set a timeout on a TranscriptMessage object so it clears itself when it's not
|
|
|
|
* updated.
|
|
|
|
*
|
|
|
|
* @param {Function} dispatch - Dispatch remove action to store.
|
|
|
|
* @param {string} transcriptMessageID - The id of the message to remove.
|
|
|
|
* @param {Object} transcriptMessage - The message to remove.
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
2018-08-23 19:57:12 +00:00
|
|
|
function _setClearerOnTranscriptMessage(
|
2018-08-06 21:30:50 +00:00
|
|
|
dispatch,
|
|
|
|
transcriptMessageID,
|
|
|
|
transcriptMessage) {
|
|
|
|
if (transcriptMessage.clearTimeOut) {
|
|
|
|
clearTimeout(transcriptMessage.clearTimeOut);
|
|
|
|
}
|
|
|
|
|
2018-08-23 19:57:12 +00:00
|
|
|
transcriptMessage.clearTimeOut
|
|
|
|
= setTimeout(
|
|
|
|
() => dispatch(removeTranscriptMessage(transcriptMessageID)),
|
|
|
|
REMOVE_AFTER_MS);
|
2018-08-06 21:30:50 +00:00
|
|
|
}
|