From ae318aa51af5547c94c3aee6d532d13699a2c4ac Mon Sep 17 00:00:00 2001 From: RazvanP Date: Thu, 3 Oct 2024 11:09:17 +0300 Subject: [PATCH] Rely on the xmpp signaling to flush the buffer --- .../jigasi/TranscriptionGatewaySession.java | 38 +++++++++++++++++- .../jigasi/transcription/Participant.java | 39 +------------------ .../jigasi/transcription/Transcriber.java | 14 +++++++ 3 files changed, 51 insertions(+), 40 deletions(-) diff --git a/src/main/java/org/jitsi/jigasi/TranscriptionGatewaySession.java b/src/main/java/org/jitsi/jigasi/TranscriptionGatewaySession.java index 156c2e93..6b625532 100644 --- a/src/main/java/org/jitsi/jigasi/TranscriptionGatewaySession.java +++ b/src/main/java/org/jitsi/jigasi/TranscriptionGatewaySession.java @@ -29,7 +29,9 @@ import org.jitsi.utils.logging.*; import org.jitsi.utils.*; import org.jivesoftware.smack.packet.Presence; +import org.jivesoftware.smack.packet.StandardExtensionElement; import org.json.simple.*; +import org.json.simple.parser.*; import org.jxmpp.jid.*; import org.jxmpp.jid.impl.*; import org.jxmpp.stringprep.*; @@ -355,13 +357,45 @@ void notifyChatRoomMemberLeft(ChatRoomMember chatMember) ); } + /** + /* Checks if the participant has muted and flushes the audio buffer if so. + **/ + private void flushParticipantTranscriptionBufferOnMute(ChatRoomMember chatMember, Presence presence) + { + boolean hasMuted = false; + StandardExtensionElement sourceInfo = (StandardExtensionElement) presence.getExtensionElement("SourceInfo", + "jabber:client"); + if (sourceInfo != null) + { + String mutedText = sourceInfo.getText(); + JSONParser jsonParser = new JSONParser(); + try + { + JSONObject jsonObject = (JSONObject) jsonParser.parse(mutedText); + String participantKey = jsonObject.keySet().toArray()[0].toString(); + JSONObject mutedJsonObject = (JSONObject) jsonParser.parse(jsonObject.get(participantKey).toString()); + hasMuted = (boolean) mutedJsonObject.get("muted"); + } + catch (Exception e) + { + logger.error("Error parsing presence while checking if participant is muted", e); + } + + if (hasMuted) + { + this.transcriber.flushParticipantAudioBuffer(getParticipantIdentifier(chatMember)); + } + } + } + @Override void notifyChatRoomMemberUpdated(ChatRoomMember chatMember, Presence presence) { super.notifyChatRoomMemberUpdated(chatMember, presence); + this.flushParticipantTranscriptionBufferOnMute(chatMember, presence); - //This needed for the translation language change. - //update a language change coming in the presence + //This is needed for the translation language change. + //Updates a language change coming in the presence String identifier = getParticipantIdentifier(chatMember); this.transcriber.updateParticipant(identifier, chatMember); diff --git a/src/main/java/org/jitsi/jigasi/transcription/Participant.java b/src/main/java/org/jitsi/jigasi/transcription/Participant.java index 82cf3264..797e905d 100644 --- a/src/main/java/org/jitsi/jigasi/transcription/Participant.java +++ b/src/main/java/org/jitsi/jigasi/transcription/Participant.java @@ -100,16 +100,6 @@ public class Participant */ private Transcriber transcriber; - /** - * The time when the last audio packet was received - */ - private long lastAudioReceivedTime = System.currentTimeMillis(); - - /** - * Whether the buffer has been flushed after a period of inactivity - */ - private boolean flushed = false; - /** * The chat room participant. */ @@ -534,7 +524,6 @@ void joined() session.addTranscriptionListener(this); sessions.put(getLanguageKey(), session); isCompleted = false; - startBufferCheck(); } } @@ -566,7 +555,6 @@ public void left() */ void giveBuffer(javax.media.Buffer buffer) { - lastAudioReceivedTime = System.currentTimeMillis(); if (audioFormat == null) { audioFormat = (AudioFormat) buffer.getFormat(); @@ -657,7 +645,6 @@ else if (silenceFilter.newSpeech()) try { buffer.put(toBuffer); - flushed = false; } catch (BufferOverflowException | ReadOnlyBufferException e) { @@ -780,35 +767,11 @@ public boolean isRequestingTranscription() return ext != null && Boolean.parseBoolean(ext.getText()); } - private void startBufferCheck() - { - new Thread(() -> { - while (true) { - try - { - Thread.sleep(1000); // Check every second - } - catch (InterruptedException e) - { - Thread.currentThread().interrupt(); - return; - } - - long currentTime = System.currentTimeMillis(); - if ((currentTime - lastAudioReceivedTime) > 1000 && !flushed) - { - flushBufferOnInactivity(); - } - } - }).start(); - } - - private void flushBufferOnInactivity() + public void flushBuffer() { transcriber.executorService.execute(() -> { sendRequest(buffer.array()); ((Buffer) buffer).clear(); - flushed = true; }); } } diff --git a/src/main/java/org/jitsi/jigasi/transcription/Transcriber.java b/src/main/java/org/jitsi/jigasi/transcription/Transcriber.java index 6588c373..3db56abb 100644 --- a/src/main/java/org/jitsi/jigasi/transcription/Transcriber.java +++ b/src/main/java/org/jitsi/jigasi/transcription/Transcriber.java @@ -474,6 +474,20 @@ public void participantLeft(String identifier) + identifier + " left while it did not exist"); } + /** + * Flush the audio buffer of a participant. + * + * @param identifier the identifier of the participant + */ + public void flushParticipantAudioBuffer(String identifier) + { + Participant participant = getParticipant(identifier); + if (participant != null) + { + participant.flushBuffer(); + } + } + /** * Start transcribing all participants added to the list */