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.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. const json = action.json;
  58. const translationLanguage
  59. = getState()['features/base/conference'].conference
  60. .getLocalParticipantProperty(P_NAME_TRANSLATION_LANGUAGE);
  61. try {
  62. const transcriptMessageID = json.message_id;
  63. const participantName = json.participant.name;
  64. const isInterim = json.is_interim;
  65. const stability = json.stability;
  66. if (json.type === JSON_TYPE_TRANSLATION_RESULT
  67. && json.language === translationLanguage) {
  68. // Displays final results in the target language if translation is
  69. // enabled.
  70. const newTranscriptMessage = {
  71. participantName,
  72. final: json.text,
  73. clearTimeOut: undefined
  74. };
  75. setClearerOnTranscriptMessage(dispatch,
  76. transcriptMessageID, newTranscriptMessage);
  77. dispatch(updateTranscriptMessage(transcriptMessageID,
  78. newTranscriptMessage));
  79. } else if (json.type === JSON_TYPE_TRANSCRIPTION_RESULT
  80. && !translationLanguage) {
  81. // Displays interim and final results without any translation if
  82. // translations are disabled.
  83. const text = json.transcript[0].text;
  84. // We update the previous transcript message with the same
  85. // message ID or adds a new transcript message if it does not
  86. // exist in the map.
  87. const newTranscriptMessage
  88. = { ...getState()['features/subtitles'].transcriptMessages
  89. .get(transcriptMessageID) || { participantName } };
  90. setClearerOnTranscriptMessage(dispatch,
  91. transcriptMessageID, newTranscriptMessage);
  92. // If this is final result, update the state as a final result
  93. // and start a count down to remove the subtitle from the state
  94. if (!isInterim) {
  95. newTranscriptMessage.final = text;
  96. dispatch(updateTranscriptMessage(transcriptMessageID,
  97. newTranscriptMessage));
  98. } else if (stability > 0.85) {
  99. // If the message has a high stability, we can update the
  100. // stable field of the state and remove the previously
  101. // unstable results
  102. newTranscriptMessage.stable = text;
  103. newTranscriptMessage.unstable = undefined;
  104. dispatch(updateTranscriptMessage(transcriptMessageID,
  105. newTranscriptMessage));
  106. } else {
  107. // Otherwise, this result has an unstable result, which we
  108. // add to the state. The unstable result will be appended
  109. // after the stable part.
  110. newTranscriptMessage.unstable = text;
  111. dispatch(updateTranscriptMessage(transcriptMessageID,
  112. newTranscriptMessage));
  113. }
  114. }
  115. } catch (error) {
  116. logger.error('Error occurred while updating transcriptions\n', error);
  117. }
  118. return next(action);
  119. }
  120. /**
  121. * Set a timeout on a TranscriptMessage object so it clears itself when it's not
  122. * updated.
  123. *
  124. * @param {Function} dispatch - Dispatch remove action to store.
  125. * @param {string} transcriptMessageID - The id of the message to remove.
  126. * @param {Object} transcriptMessage - The message to remove.
  127. *
  128. * @returns {void}
  129. */
  130. function setClearerOnTranscriptMessage(
  131. dispatch,
  132. transcriptMessageID,
  133. transcriptMessage) {
  134. if (transcriptMessage.clearTimeOut) {
  135. clearTimeout(transcriptMessage.clearTimeOut);
  136. }
  137. transcriptMessage.clearTimeOut = setTimeout(() => {
  138. dispatch(removeTranscriptMessage(transcriptMessageID));
  139. }, REMOVE_AFTER_MS);
  140. }