-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
394 additions
and
1,824 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
"@livekit/components-core": patch | ||
"@livekit/components-react": patch | ||
--- | ||
|
||
Add experimental hooks for transcriptions |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -45,8 +45,5 @@ | |
}, | ||
"engines": { | ||
"node": ">=18" | ||
}, | ||
"pnpm": { | ||
"overrides": {} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import type { TranscriptionSegment } from 'livekit-client'; | ||
|
||
export type ReceivedTranscriptionSegment = TranscriptionSegment & { | ||
receivedAtMediaTimestamp: number; | ||
receivedAt: number; | ||
}; | ||
|
||
export function getActiveTranscriptionSegments( | ||
segments: ReceivedTranscriptionSegment[], | ||
syncTimes: { timestamp: number; rtpTimestamp?: number }, | ||
maxAge = 0, | ||
) { | ||
return segments.filter((segment) => { | ||
const hasTrackSync = !!syncTimes.rtpTimestamp; | ||
const currentTrackTime = syncTimes.rtpTimestamp ?? performance.timeOrigin + performance.now(); | ||
// if a segment arrives late, consider startTime to be the media timestamp from when the segment was received client side | ||
const displayStartTime = hasTrackSync | ||
? Math.max(segment.receivedAtMediaTimestamp, segment.startTime) | ||
: segment.receivedAt; | ||
// "active" duration is computed by the diff between start and end time, so we don't rely on displayStartTime to always be the same as the segment's startTime | ||
const segmentDuration = maxAge + segment.endTime - segment.startTime; | ||
return ( | ||
currentTrackTime >= displayStartTime && currentTrackTime <= displayStartTime + segmentDuration | ||
); | ||
}); | ||
} | ||
|
||
export function addMediaTimestampToTranscription( | ||
segment: TranscriptionSegment, | ||
timestamps: { timestamp: number; rtpTimestamp?: number }, | ||
): ReceivedTranscriptionSegment { | ||
return { | ||
...segment, | ||
receivedAtMediaTimestamp: timestamps.rtpTimestamp ?? 0, | ||
receivedAt: timestamps.timestamp, | ||
}; | ||
} | ||
|
||
/** | ||
* @returns An array of unique (by id) `TranscriptionSegment`s. Latest wins. If the resulting array would be longer than `windowSize`, the array will be reduced to `windowSize` length | ||
*/ | ||
export function dedupeSegments<T extends TranscriptionSegment>( | ||
prevSegments: T[], | ||
newSegments: T[], | ||
windowSize: number, | ||
) { | ||
return [...prevSegments, ...newSegments] | ||
.reduceRight((acc, segment) => { | ||
if (!acc.find((val) => val.id === segment.id)) { | ||
acc.unshift(segment); | ||
} | ||
return acc; | ||
}, [] as Array<T>) | ||
.slice(0 - windowSize); | ||
} | ||
|
||
export function didActiveSegmentsChange<T extends TranscriptionSegment>( | ||
prevActive: T[], | ||
newActive: T[], | ||
) { | ||
if (newActive.length !== prevActive.length) { | ||
return true; | ||
} | ||
return !newActive.every((newSegment) => { | ||
return prevActive.find( | ||
(prevSegment) => | ||
prevSegment.id === newSegment.id && | ||
prevSegment.text === newSegment.text && | ||
prevSegment.final === newSegment.final && | ||
prevSegment.language === newSegment.language && | ||
prevSegment.startTime === newSegment.startTime && | ||
prevSegment.endTime === newSegment.endTime, | ||
); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import * as React from 'react'; | ||
import { type TrackReferenceOrPlaceholder, trackSyncTimeObserver } from '@livekit/components-core'; | ||
import { useObservableState } from './internal'; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
export function useTrackSyncTime({ publication }: TrackReferenceOrPlaceholder) { | ||
const observable = React.useMemo( | ||
() => (publication?.track ? trackSyncTimeObserver(publication.track) : undefined), | ||
[publication?.track], | ||
); | ||
return useObservableState(observable, { | ||
timestamp: Date.now(), | ||
rtpTimestamp: publication?.track?.rtpTimestamp, | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
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'; | ||
|
||
/** | ||
* @alpha | ||
*/ | ||
export interface TrackTranscriptionOptions { | ||
/** | ||
* how many transcription segments should be buffered in state | ||
* @defaultValue 100 | ||
*/ | ||
bufferSize?: number; | ||
/** 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.windowLength and `activeSegments` that are valid for the current track timestamp | ||
* @alpha | ||
*/ | ||
export function useTrackTranscription( | ||
trackRef: TrackReferenceOrPlaceholder, | ||
options?: TrackTranscriptionOptions, | ||
) { | ||
const opts = { ...TRACK_TRANSCRIPTION_DEFAULTS, ...options }; | ||
const [segments, setSegments] = React.useState<Array<ReceivedTranscriptionSegment>>([]); | ||
// const [activeSegments, setActiveSegments] = React.useState<Array<ReceivedTranscriptionSegment>>( | ||
// [], | ||
// ); | ||
// const prevActiveSegments = React.useRef<ReceivedTranscriptionSegment[]>([]); | ||
const syncTimestamps = useTrackSyncTime(trackRef); | ||
const handleSegmentMessage = (newSegments: TranscriptionSegment[]) => { | ||
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(); | ||
}; | ||
}, [getTrackReferenceId(trackRef), handleSegmentMessage]); | ||
|
||
// React.useEffect(() => { | ||
// if (syncTimestamps) { | ||
// const newActiveSegments = getActiveTranscriptionSegments( | ||
// segments, | ||
// syncTimestamps, | ||
// opts.maxAge, | ||
// ); | ||
// // only update active segment array if content actually changed | ||
// if (didActiveSegmentsChange(prevActiveSegments.current, newActiveSegments)) { | ||
// setActiveSegments(newActiveSegments); | ||
// prevActiveSegments.current = newActiveSegments; | ||
// } | ||
// } | ||
// }, [syncTimestamps, segments, opts.maxAge]); | ||
|
||
return { segments }; | ||
} |
Oops, something went wrong.