forked from jitsi/jitsi-meet
/
TranscriptionSubtitles.web.js
329 lines (289 loc) · 9.87 KB
/
TranscriptionSubtitles.web.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
import React from 'react';
import PropTypes from 'prop-types';
import { connect } from 'react-redux';
declare var JitsiMeetJS: Object;
const ConferenceEvents = JitsiMeetJS.events.conference;
const TRANSCRIPTION_RESULT_TOPIC = 'transcription-result';
const REMOVE_AFTER_MS = 3000;
/**
* React {@code Component} which can display speech-to-text results from
* Jigasi as subtitles.
*
* Jigasi will send a JSON object via
* {@code ConferenceEvents.ENDPOINT_MESSAGE_RECEIVED}. An example of a json
* object sent by jigasi is:
*
* {
* 'jitsi-meet-muc-msg-topic':'transcription-result',
* 'payload':{
* 'transcript':[
* {
* 'confidence':0,
* 'text':'how are'
* }
* ],
* 'is_interim':true,
* 'language':'en-US',
* 'message_id':'8360900e-5fca-4d9c-baf3-6b24206dfbd7',
* 'event':'SPEECH',
* 'participant':{
* 'name':'Nik',
* 'id':'2fe3ac1c'
* },
* 'stability':0.009999999776482582,
* 'timestamp':'2017-08-21T14:35:46.342Z'
* }
* }
*
*/
class TranscriptionSubtitles extends React.Component {
/**
* {@code TranscriptionSubtitles}'s property types.
*
* @static
*/
static propTypes = {
/**
* The conference which we can use to add an EventListener to
*/
_conference: PropTypes.object
};
/**
* Initializes a new {@code TranscriptionSubtitles} instance.
*
* @param {Object} props - The read-only React Component props with which
* the new instance is to be initialized.
*/
constructor(props) {
super(props);
this.state = {
/**
* An array of message ID's, which will be added as an variable name
* for their object to the state.
*/
knownIDs: [],
/**
* The conference which has an EventEmitter for
* {@code ConferenceEvents.ENDPOINT_MESSAGE_RECEIVED}.
*/
conference: props._conference
};
}
/**
* Callback Listener for {@code ConferenceEvents.ENDPOINT_MESSAGE_RECEIVED}
* events.
*
* @param {string} participant - The jid of the participant who send the
* message. This is expected to be the Jigasi instance.
* @param {Object} m - The json message send by the Jigasi instance.
* @returns {void}
*/
onEndpointMessageReceived(participant, m) {
try {
// Let's first check if the given object has the correct
// topic, which identifies it as a json message send from
// Jigasi with speech-to-to-text results
let p;
if (m['jitsi-meet-muc-msg-topic'] === TRANSCRIPTION_RESULT_TOPIC
&& (p = m.payload)) {
// Extract the useful data from the payload of the JSON message
const text = p.transcript[0].text;
const stability = p.stability;
const isInterim = p.is_interim;
const id = p.message_id;
const name = p.participant.name;
// If this is the first result with the unique message ID,
// we add it to the state along with the name of the participant
// who said given text
if (!this.state[id]) {
this.setState(prevState => {
return { knownIDs: prevState.knownIDs.concat([ id ]),
[id]: { name } };
});
}
// If this is final result, update the state as a final result
// and start a count down to remove the subtitle from the state
if (!isInterim) {
this.setState(prevState => {
const o = prevState[id];
o.final = text;
return { [id]: o };
});
setTimeout(() => {
this.removeIDFromState(id);
}, REMOVE_AFTER_MS);
} else if (stability > 0.85) {
// If the message has a high stability, we can update the
// stable field of the state and remove the previously
// unstable results
this.setState(prevState => {
const o = prevState[id];
o.stable = text;
o.unstable = undefined;
return { [id]: o };
});
} else {
// Otherwise, this result has an unstable result, which we
// add to the state. The unstable result will be localed at
// the end of the String, after the stable part.
this.setState(prevState => {
const o = prevState[id];
o.unstable = text;
return { [id]: o };
});
}
}
} catch (error) {
console.log('error handling', m, error);
}
}
/**
* Remove an id from the {@code knownIds} field of the state of this
* instance. This causes the transcription belonging to the given ID
* to be removed.
*
* @param {string} id - The id to remove.
* @returns {void}
*/
removeIDFromState(id) {
this.setState(prevState => {
const newKnownIDs = prevState.knownIDs.slice();
newKnownIDs.splice(newKnownIDs.find(element => element === id), 1);
return {
knownIDs: newKnownIDs,
[id]: undefined
};
});
}
/**
* Add {@code onEndpointMessageReceived} as a listener for
* {@code ConferenceEvents.ENDPOINT_MESSAGE_RECEIVED}.
*
* @inheritdoc
*/
componentDidMount() {
this.addListener(this.state.conference);
}
/**
* Remove {@code onEndpointMessageReceived} as a listener for
* {@code ConferenceEvents.ENDPOINT_MESSAGE_RECEIVED}.
*
* @inheritdoc
*/
componentWillUnmount() {
this.removeListener(this.state.conference);
}
/**
* Updates the {@code conference} field of the state.
* When a new prop has a defined conference and the current state does not,
* it will be added to the state and the event listener will be added.
* If the next prop has an undefined conference but the current one does
* have a defined conference, the conference will be removed from the state
* and the listener will be removed from the conference.
*
* @inheritdoc
* @param {Object} nextProps - The read-only props which this Component will
* receive.
* @returns {void}
*/
componentWillReceiveProps(nextProps) {
if (nextProps._conference && !this.state.conference) {
const conference = nextProps._conference;
this.setState({ conference });
this.addListener(conference);
}
if (!nextProps._conference && this.state.conference) {
this.removeListener(this.state.conference);
this.setState({ conference: undefined });
}
}
/**
* Add the {@code ConferenceEvents.ENDPOINT_MESSAGE_RECEIVED} listener to
* the given conference.
*
* @param {Object} conference - The conference which has an EventEmitter for
* {@code ConferenceEvents.ENDPOINT_MESSAGE_RECEIVED}.
* @returns {void}
*/
addListener(conference) {
if (conference) {
conference.eventEmitter.addListener(
ConferenceEvents.ENDPOINT_MESSAGE_RECEIVED,
this.onEndpointMessageReceived.bind(this));
}
}
/**
* Remove the {@code ConferenceEvents.ENDPOINT_MESSAGE_RECEIVED} listener
* from the given conference.
*
* @param {Object} conference - The conference which has an EventEmitter for
* {@code ConferenceEvents.ENDPOINT_MESSAGE_RECEIVED}.
* @returns {void}
*/
removeListener(conference) {
if (conference) {
conference.eventEmitter.removeListener(
ConferenceEvents.ENDPOINT_MESSAGE_RECEIVED,
this.onEndpointMessageReceived);
}
}
/**
* The CSS to use for rendering the text.
*/
divStyle = {
position: 'absolute',
bottom: '5%',
left: '10%',
zIndex: 2,
fontWeight: 600,
fontSize: '14px',
textAlign: 'left',
color: '#FFF',
opacity: '.80',
textShadow: '0px 0px 1px rgba(0,0,0,0.3),'
+ '0px 1px 1px rgba(0,0,0,0.3),'
+ '1px 0px 1px rgba(0,0,0,0.3),'
+ '0px 0px 1px rgba(0,0,0,0.3)'
};
/**
* Implements React's {@link Component#render()}.
*
* @inheritdoc
* @returns {ReactElement}
*/
render() {
const paragraphs = [];
this.state.knownIDs.forEach(id => {
const o = this.state[id];
let text;
if (o) {
text = `${o.name}: `;
if (o.final) {
text += o.final;
} else {
const stable = o.stable ? o.stable : '';
const unstable = o.unstable ? o.unstable : '';
text += stable + unstable;
}
}
paragraphs.push(<p key = { id }> { text } </p>);
});
return <div style = { this.divStyle }> { paragraphs } </div>;
}
}
/**
* Maps the conference in the Redux state to the associated
* {@code TranscriptionSubtitles's props.
*
* @param {Object} state - The Redux state.
* @private
* @returns {{
* _conference: Object
* }}
*/
function _mapStateToProps(state) {
return {
_conference: state['features/base/conference'].conference
};
}
export default connect(_mapStateToProps)(TranscriptionSubtitles);