您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

middleware.js 5.0KB

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