Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

middleware.js 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. import { MiddlewareRegistry } from '../base/redux';
  2. import { ENDPOINT_MESSAGE_RECEIVED } from './actionTypes';
  3. import {
  4. addTranscriptMessage,
  5. removeTranscriptMessage,
  6. updateTranscriptMessage
  7. } from './actions';
  8. const logger = require('jitsi-meet-logger').getLogger(__filename);
  9. /**
  10. * Time after which the rendered subtitles will be removed.
  11. */
  12. const REMOVE_AFTER_MS = 3000;
  13. /**
  14. * Middleware that catches actions related to transcript messages
  15. * to be rendered in {@link TranscriptionSubtitles }
  16. *
  17. * @param {Store} store - Redux store.
  18. * @returns {Function}
  19. */
  20. MiddlewareRegistry.register(store => next => action => {
  21. switch (action.type) {
  22. case ENDPOINT_MESSAGE_RECEIVED:
  23. return _endpointMessageReceived(store, next, action);
  24. }
  25. return next(action);
  26. });
  27. /**
  28. * Notifies the feature transcription that the action
  29. * {@code ENDPOINT_MESSAGE_RECEIVED} is being dispatched within a specific redux
  30. * store.
  31. *
  32. * @param {Store} store - The redux store in which the specified {@code action}
  33. * is being dispatched.
  34. * @param {Dispatch} next - The redux {@code dispatch} function to
  35. * dispatch the specified {@code action} to the specified {@code store}.
  36. * @param {Action} action - The redux action {@code ENDPOINT_MESSAGE_RECEIVED}
  37. * which is being dispatched in the specified {@code store}.
  38. * @private
  39. * @returns {Object} The value returned by {@code next(action)}.
  40. */
  41. function _endpointMessageReceived({ dispatch, getState }, next, action) {
  42. const json = action.json;
  43. try {
  44. // Let's first check if the given object has the correct
  45. // type in the json, which identifies it as a json message sent
  46. // from Jigasi with speech-to-to-text results
  47. if (json.type === 'transcription-result') {
  48. // Extract the useful data from the json.
  49. const isInterim = json.is_interim;
  50. const participantName = json.participant.name;
  51. const stability = json.stability;
  52. const text = json.transcript[0].text;
  53. const transcriptMessageID = json.message_id;
  54. // If this is the first result with the unique message ID,
  55. // we add it to the state along with the name of the participant
  56. // who said given text
  57. if (!getState()['features/subtitles']
  58. .transcriptMessages.has(transcriptMessageID)) {
  59. dispatch(addTranscriptMessage(transcriptMessageID,
  60. participantName));
  61. }
  62. const { transcriptMessages } = getState()['features/subtitles'];
  63. const newTranscriptMessage
  64. = { ...transcriptMessages.get(transcriptMessageID) };
  65. // If this is final result, update the state as a final result
  66. // and start a count down to remove the subtitle from the state
  67. if (!isInterim) {
  68. newTranscriptMessage.final = text;
  69. dispatch(updateTranscriptMessage(transcriptMessageID,
  70. newTranscriptMessage));
  71. setTimeout(() => {
  72. dispatch(removeTranscriptMessage(transcriptMessageID));
  73. }, REMOVE_AFTER_MS);
  74. } else if (stability > 0.85) {
  75. // If the message has a high stability, we can update the
  76. // stable field of the state and remove the previously
  77. // unstable results
  78. newTranscriptMessage.stable = text;
  79. newTranscriptMessage.unstable = undefined;
  80. dispatch(updateTranscriptMessage(transcriptMessageID,
  81. newTranscriptMessage));
  82. } else {
  83. // Otherwise, this result has an unstable result, which we
  84. // add to the state. The unstable result will be appended
  85. // after the stable part.
  86. newTranscriptMessage.unstable = text;
  87. dispatch(updateTranscriptMessage(transcriptMessageID,
  88. newTranscriptMessage));
  89. }
  90. }
  91. } catch (error) {
  92. logger.error('Error occurred while updating transcriptions\n', error);
  93. }
  94. return next(action);
  95. }