Skip to content

Commit

Permalink
Add firstReceivedTime and lastReceivedTime to received TranscriptionS…
Browse files Browse the repository at this point in the history
…egments (#1223)

* Add firstReceivedTime and lastReceivedTime to received TranscriptionSegements

* add changeset

* clear on disconnect

* set received time
  • Loading branch information
lukasIO authored Aug 22, 2024
1 parent dd83dc8 commit 414c8bf
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/spotty-years-return.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'livekit-client': patch
---

Add firstReceivedTime and lastReceivedTime to received TranscriptionSegments
9 changes: 8 additions & 1 deletion src/room/Room.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,11 @@ class Room extends (EventEmitter as new () => TypedEmitter<RoomEventCallbacks>)

private isResuming: boolean = false;

/**
* map to store first point in time when a particular transcription segment was received
*/
private transcriptionReceivedTimes: Map<string, number>;

/**
* Creates a new Room, the primary construct for a LiveKit session.
* @param options
Expand All @@ -174,6 +179,7 @@ class Room extends (EventEmitter as new () => TypedEmitter<RoomEventCallbacks>)
this.options = { ...roomOptionDefaults, ...options };

this.log = getLogger(this.options.loggerName ?? LoggerNames.Room);
this.transcriptionReceivedTimes = new Map();

this.options.audioCaptureDefaults = {
...audioDefaults,
Expand Down Expand Up @@ -1274,6 +1280,7 @@ class Room extends (EventEmitter as new () => TypedEmitter<RoomEventCallbacks>)
this.clearConnectionReconcile();
this.isResuming = false;
this.bufferedEvents = [];
this.transcriptionReceivedTimes.clear();
if (this.state === ConnectionState.Disconnected) {
return;
}
Expand Down Expand Up @@ -1535,7 +1542,7 @@ class Room extends (EventEmitter as new () => TypedEmitter<RoomEventCallbacks>)
: 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);
Expand Down
2 changes: 2 additions & 0 deletions src/room/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,6 @@ export interface TranscriptionSegment {
startTime: number;
endTime: number;
final: boolean;
firstReceivedTime: number;
lastReceivedTime: number;
}
10 changes: 10 additions & 0 deletions src/room/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -532,15 +532,25 @@ export function toHttpUrl(url: string): string {

export function extractTranscriptionSegments(
transcription: TranscriptionModel,
firstReceivedTimesMap: Map<string, number>,
): 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,
startTime: Number.parseInt(startTime.toString()),
endTime: Number.parseInt(endTime.toString()),
final,
language,
firstReceivedTime,
lastReceivedTime,
};
});
}

0 comments on commit 414c8bf

Please sign in to comment.