Skip to content

Commit 95ad5ae

Browse files
committed
review comments
1 parent ae809a0 commit 95ad5ae

File tree

1 file changed

+39
-43
lines changed

1 file changed

+39
-43
lines changed

packages/client/docusaurus/docs/javascript/02-guides/16-closed-captions.mdx

Lines changed: 39 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ console.log(call.state.settings?.transcription.closed_caption_mode);
2424

2525
## Closed caption events
2626

27-
If closed captions are enabled for a given call, you'll receive the captions in the `call.closed_caption` WebSocket events. Below, you can find an example payload:
27+
If closed captions are enabled for a given call, you'll receive the captions in the `call.closed_caption` events. Below, you can find an example payload:
2828

2929
```
3030
{
@@ -56,58 +56,54 @@ import {
5656
} from '@stream-io/video-client';
5757

5858
// The captions queue
59-
captions: (CallClosedCaption & { speaker_name?: string })[] = [];
59+
const captions: (CallClosedCaption & { speaker_name?: string })[] = [];
6060
// The maximum number of captions that can be visible on the screen
61-
numberOfCaptionsVisible = 2;
61+
const numberOfCaptionsVisible = 2;
6262
// A single caption can stay visible on the screen for this duration
6363
// This is the maximum duration, new captions can push a caption out of the screen sooner
64-
captionTimeoutMs = 2700;
64+
const captionTimeoutMs = 2700;
6565

6666
// Subscribe to call.closed_caption events
67-
call.on(
68-
'call.closed_caption',
69-
(event: ClosedCaptionEvent) => {
70-
const caption = event.closed_caption;
71-
// It's possible to receive the same caption twice, so make sure to filter duplicates
72-
const isDuplicate = captions.find(
73-
(c) =>
74-
c.speaker_id === caption.speaker_id &&
75-
c.start_time === caption.start_time,
76-
);
77-
if (!isDuplicate) {
78-
// Look up the speaker's name based on the user id
79-
const speaker = call.state.participants.find(
80-
(p) => p.userId === caption.speaker_id,
81-
);
82-
const speakerName = speaker?.name || speaker?.userId;
83-
// Add the caption to the queue
84-
captions.push({ ...caption, speaker_name: speakerName });
85-
// Update the UI
86-
updateDisplayedCaptions();
87-
// We specify a maximum amount of time a caption can be visible
88-
// after that, we remove it from the screen (unless a newer caption has already pushed it out)
89-
captionTimeout = setTimeout(() => {
90-
captions = captions.slice(1);
91-
updateDisplayedCaptions();
92-
captionTimeout = undefined;
93-
}, captionTimeoutMs);
94-
}
95-
});
96-
97-
const updateDisplayedCaptions = () => {
98-
// The default implementation shows the last two captions
99-
const displayedCaptions = captions.slice(
100-
-1 * numberOfCaptionsVisible,
67+
call.on('call.closed_caption', (event: ClosedCaptionEvent) => {
68+
const caption = event.closed_caption;
69+
// It's possible to receive the same caption twice, so make sure to filter duplicates
70+
const isDuplicate = captions.find(
71+
(c) =>
72+
c.speaker_id === caption.speaker_id &&
73+
c.start_time === caption.start_time,
74+
);
75+
if (!isDuplicate) {
76+
// Look up the speaker's name based on the user id
77+
const speaker = call.state.participants.find(
78+
(p) => p.userId === caption.speaker_id,
10179
);
102-
const captionsHTML = displayedCaptions
103-
.map((c) => `<b>${c.speaker_name}:</b> ${c.text}`)
104-
.join('<br>');
80+
const speakerName = speaker?.name || speaker?.userId;
81+
// Add the caption to the queue
82+
captions.push({ ...caption, speaker_name: speakerName });
10583
// Update the UI
106-
}
84+
updateDisplayedCaptions();
85+
// We specify a maximum amount of time a caption can be visible
86+
// after that, we remove it from the screen (unless a newer caption has already pushed it out)
87+
captionTimeout = setTimeout(() => {
88+
captions = captions.slice(1);
89+
updateDisplayedCaptions();
90+
captionTimeout = undefined;
91+
}, captionTimeoutMs);
92+
}
93+
});
94+
95+
const updateDisplayedCaptions = () => {
96+
// The default implementation shows the last two captions
97+
const displayedCaptions = captions.slice(-1 * numberOfCaptionsVisible);
98+
const captionsHTML = displayedCaptions
99+
.map((c) => `<b>${c.speaker_name}:</b> ${c.text}`)
100+
.join('<br>');
101+
// Update the UI
102+
};
107103
```
108104

109105
:::note
110-
Since the closed caption WebSocket event contains `start_time` and `end_time` fields, you can subtract the two to know how long it took the speaker to say the caption. You can then use this duration to control how long the text is visible on the screen. This is useful to ensure the captions are as real-time as possible, but that might not leave enough time for participants to read the text.
106+
Since the closed caption event contains `start_time` and `end_time` fields, you can subtract the two to know how long it took the speaker to say the caption. You can then use this duration to control how long the text is visible on the screen. This is useful to ensure the captions are as real-time as possible, but that might not leave enough time for participants to read the text.
111107
:::
112108

113109
## See it in action

0 commit comments

Comments
 (0)