-
Notifications
You must be signed in to change notification settings - Fork 151
Expand file tree
/
Copy pathuseTrackTranscription.ts
More file actions
116 lines (107 loc) · 3.52 KB
/
useTrackTranscription.ts
File metadata and controls
116 lines (107 loc) · 3.52 KB
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
import {
type ReceivedTranscriptionSegment,
addMediaTimestampToTranscription as addTimestampsToTranscription,
dedupeSegments,
// getActiveTranscriptionSegments,
getTrackReferenceId,
trackTranscriptionObserver,
type TrackReferenceOrPlaceholder,
// didActiveSegmentsChange,
} from '@livekit/components-core';
import type { TranscriptionSegment } from 'livekit-client';
import * as React from 'react';
import { useTrackSyncTime } from './useTrackSyncTime';
import { useTranscriptions } from './useTranscriptions';
/**
* @alpha
*/
export interface TrackTranscriptionOptions {
/**
* how many transcription segments should be buffered in state
* @defaultValue 100
*/
bufferSize?: number;
/**
* optional callback for retrieving newly incoming transcriptions only
*/
onTranscription?: (newSegments: TranscriptionSegment[]) => void;
/** amount of time (in ms) that the segment is considered `active` past its original segment duration, defaults to 2_000 */
// maxAge?: number;
}
const TRACK_TRANSCRIPTION_DEFAULTS = {
bufferSize: 100,
// maxAge: 2_000,
} as const satisfies TrackTranscriptionOptions;
/**
* @returns An object consisting of `segments` with maximum length of opts.bufferSize
* @alpha
*/
export function useTrackTranscription(
trackRef: TrackReferenceOrPlaceholder | undefined,
options?: TrackTranscriptionOptions,
) {
const legacyTranscription = useLegacyTranscription(trackRef, options);
const participantIdentities = React.useMemo(() => {
if (!trackRef) {
return [];
}
return [trackRef.participant.identity];
}, [trackRef]);
const trackSids = React.useMemo(() => {
if (!trackRef) {
return [];
}
return [getTrackReferenceId(trackRef)];
}, [trackRef]);
const useTranscriptionsOptions = React.useMemo(() => {
return { participantIdentities, trackSids };
}, [participantIdentities, trackSids]);
const transcriptions = useTranscriptions(useTranscriptionsOptions);
const streamAsSegments = transcriptions.map((t) => {
const legacySegment: ReceivedTranscriptionSegment = {
id: t.streamInfo.id,
text: t.text,
receivedAt: t.streamInfo.timestamp,
receivedAtMediaTimestamp: 0,
language: '',
startTime: 0,
endTime: 0,
final: false,
firstReceivedTime: 0,
lastReceivedTime: 0,
};
return legacySegment;
});
return streamAsSegments.length > 0 ? { segments: streamAsSegments } : legacyTranscription;
}
function useLegacyTranscription(
trackRef: TrackReferenceOrPlaceholder | undefined,
options?: TrackTranscriptionOptions,
) {
const opts = { ...TRACK_TRANSCRIPTION_DEFAULTS, ...options };
const [segments, setSegments] = React.useState<Array<ReceivedTranscriptionSegment>>([]);
const syncTimestamps = useTrackSyncTime(trackRef);
const handleSegmentMessage = (newSegments: TranscriptionSegment[]) => {
opts.onTranscription?.(newSegments);
setSegments((prevSegments) =>
dedupeSegments(
prevSegments,
// when first receiving a segment, add the current media timestamp to it
newSegments.map((s) => addTimestampsToTranscription(s, syncTimestamps)),
opts.bufferSize,
),
);
};
React.useEffect(() => {
if (!trackRef?.publication) {
return;
}
const subscription = trackTranscriptionObserver(trackRef.publication).subscribe((evt) => {
handleSegmentMessage(...evt);
});
return () => {
subscription.unsubscribe();
};
}, [trackRef && getTrackReferenceId(trackRef), handleSegmentMessage]);
return { segments };
}