Browse Source

[WEB] Show final translated speech to text results as subtitles (#3276)

* Shows final translated speech to text results as subtitles

* Use conference from redux state and removes addTranscriptMessage
master
Praveen Gupta 7 years ago
parent
commit
81853d971a

+ 1
- 12
react/features/subtitles/actionTypes.js View File

@@ -1,14 +1,3 @@
1
-/**
2
- * The type of (redux) action which indicates that a transcript with
3
- * a new message_id is received.
4
- *
5
- * {
6
- *      type: ADD_TRANSCRIPT_MESSAGE,
7
- *      transcriptMessageID: string,
8
- *      participantName: string
9
- * }
10
- */
11
-export const ADD_TRANSCRIPT_MESSAGE = Symbol('ADD_TRANSCRIPT_MESSAGE');
12 1
 
13 2
 /**
14 3
  * The type of (redux) action which indicates that an endpoint message
@@ -35,7 +24,7 @@ export const REMOVE_TRANSCRIPT_MESSAGE = Symbol('REMOVE_TRANSCRIPT_MESSAGE');
35 24
 
36 25
 /**
37 26
  * The type of (redux) action which indicates that a transcript with an
38
- * existing message_id to be updated is received.
27
+ * given message_id to be added or updated is received.
39 28
  *
40 29
  * {
41 30
  *      type: UPDATE_TRANSCRIPT_MESSAGE,

+ 1
- 22
react/features/subtitles/actions.js View File

@@ -1,32 +1,11 @@
1 1
 // @flow
2 2
 
3 3
 import {
4
-    ADD_TRANSCRIPT_MESSAGE,
5 4
     ENDPOINT_MESSAGE_RECEIVED,
6 5
     REMOVE_TRANSCRIPT_MESSAGE,
7 6
     UPDATE_TRANSCRIPT_MESSAGE
8 7
 } from './actionTypes';
9 8
 
10
-/**
11
- * Signals that a transcript with a new message_id is received.
12
- *
13
- * @param {string} transcriptMessageID - The new message_id.
14
- * @param {string} participantName - The participant name of the sender.
15
- * @returns {{
16
- *      type: ADD_TRANSCRIPT_MESSAGE,
17
- *      transcriptMessageID: string,
18
- *      participantName: string
19
- * }}
20
- */
21
-export function addTranscriptMessage(transcriptMessageID: string,
22
-        participantName: string) {
23
-    return {
24
-        type: ADD_TRANSCRIPT_MESSAGE,
25
-        transcriptMessageID,
26
-        participantName
27
-    };
28
-}
29
-
30 9
 /**
31 10
  * Signals that a participant sent an endpoint message on the data channel.
32 11
  *
@@ -63,7 +42,7 @@ export function removeTranscriptMessage(transcriptMessageID: string) {
63 42
 }
64 43
 
65 44
 /**
66
- * Signals that a transcript with an existing message_id to be updated
45
+ * Signals that a transcript with the given message_id to be added or updated
67 46
  * is received.
68 47
  *
69 48
  * @param {string} transcriptMessageID -The transcript message_id to be updated.

+ 57
- 21
react/features/subtitles/middleware.js View File

@@ -1,14 +1,35 @@
1
+// @flow
2
+
1 3
 import { MiddlewareRegistry } from '../base/redux';
2 4
 
3 5
 import { ENDPOINT_MESSAGE_RECEIVED } from './actionTypes';
4 6
 import {
5
-    addTranscriptMessage,
6 7
     removeTranscriptMessage,
7 8
     updateTranscriptMessage
8 9
 } from './actions';
9 10
 
10 11
 const logger = require('jitsi-meet-logger').getLogger(__filename);
11 12
 
13
+declare var APP: Object;
14
+
15
+/**
16
+ * The type of json-message which indicates that json carries a
17
+ * transcription result.
18
+ */
19
+const JSON_TYPE_TRANSCRIPTION_RESULT = 'transcription-result';
20
+
21
+/**
22
+ * The type of json-message which indicates that json carries a
23
+ * translation result.
24
+ */
25
+const JSON_TYPE_TRANSLATION_RESULT = 'translation-result';
26
+
27
+/**
28
+ * The local participant property which is used to store the language
29
+ * preference for translation for a participant.
30
+ */
31
+const P_NAME_TRANSLATION_LANGUAGE = 'translation_language';
32
+
12 33
 /**
13 34
 * Time after which the rendered subtitles will be removed.
14 35
 */
@@ -47,31 +68,46 @@ MiddlewareRegistry.register(store => next => action => {
47 68
  */
48 69
 function _endpointMessageReceived({ dispatch, getState }, next, action) {
49 70
     const json = action.json;
71
+    const translationLanguage
72
+        = getState()['features/base/conference'].conference
73
+            .getLocalParticipantProperty(P_NAME_TRANSLATION_LANGUAGE);
50 74
 
51 75
     try {
76
+        const transcriptMessageID = json.message_id;
77
+        const participantName = json.participant.name;
78
+        const isInterim = json.is_interim;
79
+        const stability = json.stability;
80
+
81
+        if (json.type === JSON_TYPE_TRANSLATION_RESULT
82
+            && json.language === translationLanguage) {
83
+            // Displays final results in the target language if translation is
84
+            // enabled.
85
+
86
+            const newTranscriptMessage = {
87
+                participantName,
88
+                final: json.text
89
+            };
90
+
91
+            dispatch(updateTranscriptMessage(transcriptMessageID,
92
+                newTranscriptMessage));
93
+
94
+            setTimeout(() => {
95
+                dispatch(removeTranscriptMessage(transcriptMessageID));
96
+            }, REMOVE_AFTER_MS);
97
+
98
+        } else if (json.type === JSON_TYPE_TRANSCRIPTION_RESULT
99
+            && !translationLanguage) {
100
+            // Displays interim and final results without any translation if
101
+            // translations are disabled.
52 102
 
53
-        // Let's first check if the given object has the correct
54
-        // type in the json, which identifies it as a json message sent
55
-        // from Jigasi with speech-to-to-text results
56
-        if (json.type === 'transcription-result') {
57
-            // Extract the useful data from the json.
58
-            const isInterim = json.is_interim;
59
-            const participantName = json.participant.name;
60
-            const stability = json.stability;
61 103
             const text = json.transcript[0].text;
62
-            const transcriptMessageID = json.message_id;
63
-
64
-            // If this is the first result with the unique message ID,
65
-            // we add it to the state along with the name of the participant
66
-            // who said given text
67
-            if (!getState()['features/subtitles']
68
-                .transcriptMessages.has(transcriptMessageID)) {
69
-                dispatch(addTranscriptMessage(transcriptMessageID,
70
-                    participantName));
71
-            }
72
-            const { transcriptMessages } = getState()['features/subtitles'];
104
+
105
+            // We update the previous transcript message with the same
106
+            // message ID or adds a new transcript message if it does not
107
+            // exist in the map.
73 108
             const newTranscriptMessage
74
-                = { ...transcriptMessages.get(transcriptMessageID) };
109
+                = { ...getState()['features/subtitles'].transcriptMessages
110
+                    .get(transcriptMessageID) || { participantName } };
75 111
 
76 112
             // If this is final result, update the state as a final result
77 113
             // and start a count down to remove the subtitle from the state

+ 0
- 26
react/features/subtitles/reducer.js View File

@@ -1,7 +1,6 @@
1 1
 import { ReducerRegistry } from '../base/redux';
2 2
 
3 3
 import {
4
-    ADD_TRANSCRIPT_MESSAGE,
5 4
     REMOVE_TRANSCRIPT_MESSAGE,
6 5
     UPDATE_TRANSCRIPT_MESSAGE
7 6
 } from './actionTypes';
@@ -20,9 +19,6 @@ const defaultState = {
20 19
 ReducerRegistry.register('features/subtitles', (
21 20
         state = defaultState, action) => {
22 21
     switch (action.type) {
23
-    case ADD_TRANSCRIPT_MESSAGE:
24
-        return _addTranscriptMessage(state, action);
25
-
26 22
     case REMOVE_TRANSCRIPT_MESSAGE:
27 23
         return _removeTranscriptMessage(state, action);
28 24
 
@@ -33,28 +29,6 @@ ReducerRegistry.register('features/subtitles', (
33 29
     return state;
34 30
 });
35 31
 
36
-/**
37
- * Reduces a specific Redux action ADD_TRANSCRIPT_MESSAGE of the feature
38
- * transcription.
39
- *
40
- * @param {Object} state - The Redux state of the feature transcription.
41
- * @param {Action} action -The Redux action ADD_TRANSCRIPT_MESSAGE to reduce.
42
- * @returns {Object} The new state of the feature transcription after the
43
- * reduction of the specified action.
44
- */
45
-function _addTranscriptMessage(state,
46
-        { transcriptMessageID, participantName }) {
47
-    const newTranscriptMessages = new Map(state.transcriptMessages);
48
-
49
-    // Adds a new key,value pair to the Map once a new message arrives.
50
-    newTranscriptMessages.set(transcriptMessageID, { participantName });
51
-
52
-    return {
53
-        ...state,
54
-        transcriptMessages: newTranscriptMessages
55
-    };
56
-}
57
-
58 32
 /**
59 33
  * Reduces a specific Redux action REMOVE_TRANSCRIPT_MESSAGE of the feature
60 34
  * transcription.

Loading…
Cancel
Save