Compare commits

..

1 Commits

Author SHA1 Message Date
pabloFuente
dd6320b5b5 Fix openvidu-live-captions null pointer 2025-09-15 18:38:58 +02:00

View File

@ -57,21 +57,30 @@ async function joinRoom() {
} }
); );
room.registerTextStreamHandler("lk.transcription", async (reader, participantInfo) => { room.registerTextStreamHandler("lk.transcription", async (reader, participantInfo) => {
const message = await reader.readAll(); const message = await reader.readAll();
const isFinal = reader.info.attributes["lk.transcription_final"] === "true"; const isFinal = reader.info.attributes["lk.transcription_final"] === "true";
const trackId = reader.info.attributes["lk.transcribed_track_id"]; const trackId = reader.info.attributes["lk.transcribed_track_id"];
if (isFinal) { if (isFinal) {
const speaker = participantInfo.identity == room.localParticipant.identity // Due to a bug in LiveKit Server the participantInfo object may be empty.
? "You" : participantInfo.identity; // You can still get the participant owning the audio track like below:
const timestamp = new Date().toLocaleTimeString(); let participant;
const captionsTextarea = document.getElementById("captions"); if (room.localParticipant.audioTrackPublications.has(trackId)) {
captionsTextarea.value += `[${timestamp}] ${speaker}: ${message}\n`; participant = room.localParticipant;
captionsTextarea.scrollTop = captionsTextarea.scrollHeight; } else {
participant = room.remoteParticipants.values().find(p => p.audioTrackPublications.has(trackId));
} }
const captionsTextarea = document.getElementById("captions");
const timestamp = new Date().toLocaleTimeString();
const participantIdentity =
participant == room.localParticipant ? "You" : participant.identity;
captionsTextarea.value += `[${timestamp}] ${participantIdentity}: ${message}\n`;
captionsTextarea.scrollTop = captionsTextarea.scrollHeight;
} }
); }
);
try { try {
// Get the room name and participant name from the form // Get the room name and participant name from the form
@ -91,7 +100,7 @@ async function joinRoom() {
// Publish your camera and microphone // Publish your camera and microphone
await room.localParticipant.enableCameraAndMicrophone(); await room.localParticipant.enableCameraAndMicrophone();
const localVideoTrack = this.room.localParticipant.videoTrackPublications const localVideoTrack = room.localParticipant.videoTrackPublications
.values() .values()
.next().value.track; .next().value.track;
addTrack(localVideoTrack, userName, true); addTrack(localVideoTrack, userName, true);