Skip to content

wip stream compat for transcription segments #1110

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/core/etc/components-core.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ export const cssPrefix = "lk";
// @public (undocumented)
export const DataTopic: {
readonly CHAT: "lk.chat";
readonly TRANSCRIPTION: "lk.transcription";
};

// @public (undocumented)
Expand Down
13 changes: 9 additions & 4 deletions packages/core/src/components/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
sendMessage,
setupDataMessageHandler,
} from '../observables/dataChannel';
import { log } from '../logger';

/** @public */
export type { ChatMessage };
Expand Down Expand Up @@ -165,10 +166,14 @@ export function setupChat(room: Room, options?: ChatOptions) {
...chatMsg,
ignoreLegacy: serverSupportsDataStreams(),
});
await sendMessage(room.localParticipant, encodedLegacyMsg, {
reliable: true,
topic: legacyTopic,
});
try {
await sendMessage(room.localParticipant, encodedLegacyMsg, {
reliable: true,
topic: legacyTopic,
});
} catch (error) {
log.info('could not send message in legacy chat format', error);
}
return chatMsg;
} finally {
isSending$.next(false);
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/components/textStream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ export function setupTextStream(room: Room, topic: string): Observable<TextStrea

// Add cleanup when room is disconnected
room.once(RoomEvent.Disconnected, () => {
room.unregisterTextStreamHandler(topic);
textStreamsSubject.complete();
getObservableCache().delete(cacheKey);
});
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/observables/dataChannel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { ReceivedChatMessage } from '../components/chat';

export const DataTopic = {
CHAT: 'lk.chat',
TRANSCRIPTION: 'lk.transcription',
} as const;

/** @deprecated */
Expand Down
11 changes: 11 additions & 0 deletions packages/react/etc/components-react.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -1213,6 +1213,17 @@ export function useTrackTranscription(trackRef: TrackReferenceOrPlaceholder_4 |
// @alpha
export function useTrackVolume(trackOrTrackReference?: LocalAudioTrack | RemoteAudioTrack | TrackReference_3, options?: AudioAnalyserOptions): number;

// @beta
export function useTranscriptions(opts?: UseTranscriptionsOptions): TextStreamData_2[];

// @beta (undocumented)
export interface UseTranscriptionsOptions {
// (undocumented)
participantIdentities?: string[];
// (undocumented)
trackSids?: string[];
}

// @public
export function useVisualStableUpdate(
trackReferences: TrackReferenceOrPlaceholder_4[], maxItemsOnPage: number, options?: UseVisualStableUpdateOptions): TrackReferenceOrPlaceholder_4[];
Expand Down
1 change: 1 addition & 0 deletions packages/react/src/hooks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,4 @@ export * from './useVoiceAssistant';
export * from './useParticipantAttributes';
export * from './useIsRecording';
export * from './useTextStream';
export * from './useTranscriptions';
65 changes: 45 additions & 20 deletions packages/react/src/hooks/useTrackTranscription.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
import type { TranscriptionSegment } from 'livekit-client';
import * as React from 'react';
import { useTrackSyncTime } from './useTrackSyncTime';
import { useTranscriptions } from './useTranscriptions';

/**
* @alpha
Expand All @@ -35,19 +36,58 @@ const TRACK_TRANSCRIPTION_DEFAULTS = {
} 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
* @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 [activeSegments, setActiveSegments] = React.useState<Array<ReceivedTranscriptionSegment>>(
// [],
// );
// const prevActiveSegments = React.useRef<ReceivedTranscriptionSegment[]>([]);

const syncTimestamps = useTrackSyncTime(trackRef);
const handleSegmentMessage = (newSegments: TranscriptionSegment[]) => {
opts.onTranscription?.(newSegments);
Expand All @@ -72,20 +112,5 @@ export function useTrackTranscription(
};
}, [trackRef && 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 };
}
44 changes: 44 additions & 0 deletions packages/react/src/hooks/useTranscriptions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import * as React from 'react';
import { useTextStream } from './useTextStream';
import { DataTopic } from '@livekit/components-core';

/**
* @beta
*/
export interface UseTranscriptionsOptions {
participantIdentities?: string[];
trackSids?: string[];
}

/**
* @beta
* useTranscriptions is a hook that returns the transcriptions for the given participant identities and track sids,
* if no options are provided, it will return all transcriptions
* @example
* ```tsx
* const transcriptions = useTranscriptions();
* return <div>{transcriptions.map((transcription) => transcription.text)}</div>;
* ```
*/
export function useTranscriptions(opts?: UseTranscriptionsOptions) {
const { participantIdentities, trackSids } = opts ?? {};
const { textStreams } = useTextStream(DataTopic.TRANSCRIPTION);

const filteredMessages = React.useMemo(
() =>
textStreams
.filter((stream) =>
participantIdentities
? participantIdentities.includes(stream.participantInfo.identity)
: true,
)
.filter((stream) =>
trackSids
? trackSids.includes(stream.streamInfo.attributes?.['lk.transcribed_track_id'] ?? '')
: true,
),
[textStreams, participantIdentities, trackSids],
);

return filteredMessages;
}