From 414c8bf3d6ccecd9e0ebfa3d5b2163259a9d29e0 Mon Sep 17 00:00:00 2001 From: lukasIO Date: Thu, 22 Aug 2024 16:22:41 +0200 Subject: [PATCH] Add firstReceivedTime and lastReceivedTime to received TranscriptionSegments (#1223) * Add firstReceivedTime and lastReceivedTime to received TranscriptionSegements * add changeset * clear on disconnect * set received time --- .changeset/spotty-years-return.md | 5 +++++ src/room/Room.ts | 9 ++++++++- src/room/types.ts | 2 ++ src/room/utils.ts | 10 ++++++++++ 4 files changed, 25 insertions(+), 1 deletion(-) create mode 100644 .changeset/spotty-years-return.md diff --git a/.changeset/spotty-years-return.md b/.changeset/spotty-years-return.md new file mode 100644 index 0000000000..e2971f769f --- /dev/null +++ b/.changeset/spotty-years-return.md @@ -0,0 +1,5 @@ +--- +'livekit-client': patch +--- + +Add firstReceivedTime and lastReceivedTime to received TranscriptionSegments diff --git a/src/room/Room.ts b/src/room/Room.ts index 9e2e50f49c..1ef616c640 100644 --- a/src/room/Room.ts +++ b/src/room/Room.ts @@ -162,6 +162,11 @@ class Room extends (EventEmitter as new () => TypedEmitter) private isResuming: boolean = false; + /** + * map to store first point in time when a particular transcription segment was received + */ + private transcriptionReceivedTimes: Map; + /** * Creates a new Room, the primary construct for a LiveKit session. * @param options @@ -174,6 +179,7 @@ class Room extends (EventEmitter as new () => TypedEmitter) this.options = { ...roomOptionDefaults, ...options }; this.log = getLogger(this.options.loggerName ?? LoggerNames.Room); + this.transcriptionReceivedTimes = new Map(); this.options.audioCaptureDefaults = { ...audioDefaults, @@ -1274,6 +1280,7 @@ class Room extends (EventEmitter as new () => TypedEmitter) this.clearConnectionReconcile(); this.isResuming = false; this.bufferedEvents = []; + this.transcriptionReceivedTimes.clear(); if (this.state === ConnectionState.Disconnected) { return; } @@ -1535,7 +1542,7 @@ class Room extends (EventEmitter as new () => TypedEmitter) : this.getParticipantByIdentity(transcription.transcribedParticipantIdentity); const publication = participant?.trackPublications.get(transcription.trackId); - const segments = extractTranscriptionSegments(transcription); + const segments = extractTranscriptionSegments(transcription, this.transcriptionReceivedTimes); publication?.emit(TrackEvent.TranscriptionReceived, segments); participant?.emit(ParticipantEvent.TranscriptionReceived, segments, publication); diff --git a/src/room/types.ts b/src/room/types.ts index e3f12a8417..a92127f478 100644 --- a/src/room/types.ts +++ b/src/room/types.ts @@ -65,4 +65,6 @@ export interface TranscriptionSegment { startTime: number; endTime: number; final: boolean; + firstReceivedTime: number; + lastReceivedTime: number; } diff --git a/src/room/utils.ts b/src/room/utils.ts index e16a6b769b..34037746f1 100644 --- a/src/room/utils.ts +++ b/src/room/utils.ts @@ -532,8 +532,16 @@ export function toHttpUrl(url: string): string { export function extractTranscriptionSegments( transcription: TranscriptionModel, + firstReceivedTimesMap: Map, ): TranscriptionSegment[] { return transcription.segments.map(({ id, text, language, startTime, endTime, final }) => { + const firstReceivedTime = firstReceivedTimesMap.get(id) ?? Date.now(); + const lastReceivedTime = Date.now(); + if (final) { + firstReceivedTimesMap.delete(id); + } else { + firstReceivedTimesMap.set(id, firstReceivedTime); + } return { id, text, @@ -541,6 +549,8 @@ export function extractTranscriptionSegments( endTime: Number.parseInt(endTime.toString()), final, language, + firstReceivedTime, + lastReceivedTime, }; }); }