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.

middleware.js 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. // @flow
  2. import { MiddlewareRegistry } from '../base/redux';
  3. import { ENDPOINT_MESSAGE_RECEIVED } from './actionTypes';
  4. import {
  5. removeTranscriptMessage,
  6. updateTranscriptMessage
  7. } from './actions';
  8. const logger = require('jitsi-meet-logger').getLogger(__filename);
  9. /**
  10. * The type of json-message which indicates that json carries a
  11. * transcription result.
  12. */
  13. const JSON_TYPE_TRANSCRIPTION_RESULT = 'transcription-result';
  14. /**
  15. * The type of json-message which indicates that json carries a
  16. * translation result.
  17. */
  18. const JSON_TYPE_TRANSLATION_RESULT = 'translation-result';
  19. /**
  20. * The local participant property which is used to store the language
  21. * preference for translation for a participant.
  22. */
  23. const P_NAME_TRANSLATION_LANGUAGE = 'translation_language';
  24. /**
  25. * Time after which the rendered subtitles will be removed.
  26. */
  27. const REMOVE_AFTER_MS = 3000;
  28. /**
  29. * Middleware that catches actions related to transcript messages
  30. * to be rendered in {@link TranscriptionSubtitles }
  31. *
  32. * @param {Store} store - Redux store.
  33. * @returns {Function}
  34. */
  35. MiddlewareRegistry.register(store => next => action => {
  36. switch (action.type) {
  37. case ENDPOINT_MESSAGE_RECEIVED:
  38. return _endpointMessageReceived(store, next, action);
  39. }
  40. return next(action);
  41. });
  42. /**
  43. * Notifies the feature transcription that the action
  44. * {@code ENDPOINT_MESSAGE_RECEIVED} is being dispatched within a specific redux
  45. * store.
  46. *
  47. * @param {Store} store - The redux store in which the specified {@code action}
  48. * is being dispatched.
  49. * @param {Dispatch} next - The redux {@code dispatch} function to
  50. * dispatch the specified {@code action} to the specified {@code store}.
  51. * @param {Action} action - The redux action {@code ENDPOINT_MESSAGE_RECEIVED}
  52. * which is being dispatched in the specified {@code store}.
  53. * @private
  54. * @returns {Object} The value returned by {@code next(action)}.
  55. */
  56. function _endpointMessageReceived({ dispatch, getState }, next, action) {
  57. if (!(action.json
  58. && (action.json.type === JSON_TYPE_TRANSCRIPTION_RESULT
  59. || action.json.type === JSON_TYPE_TRANSLATION_RESULT))) {
  60. return next(action);
  61. }
  62. const json = action.json;
  63. const translationLanguage
  64. = getState()['features/base/conference'].conference
  65. .getLocalParticipantProperty(P_NAME_TRANSLATION_LANGUAGE);
  66. try {
  67. const transcriptMessageID = json.message_id;
  68. const participantName = json.participant.name;
  69. const isInterim = json.is_interim;
  70. const stability = json.stability;
  71. if (json.type === JSON_TYPE_TRANSLATION_RESULT
  72. && json.language === translationLanguage) {
  73. // Displays final results in the target language if translation is
  74. // enabled.
  75. const newTranscriptMessage = {
  76. participantName,
  77. final: json.text,
  78. clearTimeOut: undefined
  79. };
  80. setClearerOnTranscriptMessage(dispatch,
  81. transcriptMessageID, newTranscriptMessage);
  82. dispatch(updateTranscriptMessage(transcriptMessageID,
  83. newTranscriptMessage));
  84. } else if (json.type === JSON_TYPE_TRANSCRIPTION_RESULT
  85. && !translationLanguage) {
  86. // Displays interim and final results without any translation if
  87. // translations are disabled.
  88. const text = json.transcript[0].text;
  89. // We update the previous transcript message with the same
  90. // message ID or adds a new transcript message if it does not
  91. // exist in the map.
  92. const newTranscriptMessage
  93. = { ...getState()['features/subtitles'].transcriptMessages
  94. .get(transcriptMessageID) || { participantName } };
  95. setClearerOnTranscriptMessage(dispatch,
  96. transcriptMessageID, newTranscriptMessage);
  97. // If this is final result, update the state as a final result
  98. // and start a count down to remove the subtitle from the state
  99. if (!isInterim) {
  100. newTranscriptMessage.final = text;
  101. dispatch(updateTranscriptMessage(transcriptMessageID,
  102. newTranscriptMessage));
  103. } else if (stability > 0.85) {
  104. // If the message has a high stability, we can update the
  105. // stable field of the state and remove the previously
  106. // unstable results
  107. newTranscriptMessage.stable = text;
  108. newTranscriptMessage.unstable = undefined;
  109. dispatch(updateTranscriptMessage(transcriptMessageID,
  110. newTranscriptMessage));
  111. } else {
  112. // Otherwise, this result has an unstable result, which we
  113. // add to the state. The unstable result will be appended
  114. // after the stable part.
  115. newTranscriptMessage.unstable = text;
  116. dispatch(updateTranscriptMessage(transcriptMessageID,
  117. newTranscriptMessage));
  118. }
  119. }
  120. } catch (error) {
  121. logger.error('Error occurred while updating transcriptions\n', error);
  122. }
  123. return next(action);
  124. }
  125. /**
  126. * Set a timeout on a TranscriptMessage object so it clears itself when it's not
  127. * updated.
  128. *
  129. * @param {Function} dispatch - Dispatch remove action to store.
  130. * @param {string} transcriptMessageID - The id of the message to remove.
  131. * @param {Object} transcriptMessage - The message to remove.
  132. *
  133. * @returns {void}
  134. */
  135. function setClearerOnTranscriptMessage(
  136. dispatch,
  137. transcriptMessageID,
  138. transcriptMessage) {
  139. if (transcriptMessage.clearTimeOut) {
  140. clearTimeout(transcriptMessage.clearTimeOut);
  141. }
  142. transcriptMessage.clearTimeOut = setTimeout(() => {
  143. dispatch(removeTranscriptMessage(transcriptMessageID));
  144. }, REMOVE_AFTER_MS);
  145. }