From 509a4f6c65b3af89c47169517a27a4a6db969cf6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C4=83zvan=20Purdel?= <104128753+rpurdel@users.noreply.github.com> Date: Thu, 10 Oct 2024 18:11:26 +0300 Subject: [PATCH] fix: Flush the participant's audio buffer on mute (#559) * Flush the participant's audio buffer on mute * squash: Adds callContext to a log message. --------- Co-authored-by: damencho --- .../jigasi/TranscriptionGatewaySession.java | 40 +++++++++++++++++-- .../jigasi/transcription/Participant.java | 12 +++++- .../jigasi/transcription/Transcriber.java | 16 ++++++++ 3 files changed, 63 insertions(+), 5 deletions(-) diff --git a/src/main/java/org/jitsi/jigasi/TranscriptionGatewaySession.java b/src/main/java/org/jitsi/jigasi/TranscriptionGatewaySession.java index 156c2e93..857d0a17 100644 --- a/src/main/java/org/jitsi/jigasi/TranscriptionGatewaySession.java +++ b/src/main/java/org/jitsi/jigasi/TranscriptionGatewaySession.java @@ -19,6 +19,7 @@ import net.java.sip.communicator.impl.protocol.jabber.*; import net.java.sip.communicator.service.protocol.*; +import net.java.sip.communicator.service.protocol.Message; import net.java.sip.communicator.service.protocol.event.*; import net.java.sip.communicator.service.protocol.media.*; import org.jitsi.utils.concurrent.*; @@ -28,8 +29,9 @@ import org.jitsi.service.neomedia.device.*; import org.jitsi.utils.logging.*; import org.jitsi.utils.*; -import org.jivesoftware.smack.packet.Presence; +import org.jivesoftware.smack.packet.*; 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(this.callContext + " 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 7914985a..aa244bb1 100644 --- a/src/main/java/org/jitsi/jigasi/transcription/Participant.java +++ b/src/main/java/org/jitsi/jigasi/transcription/Participant.java @@ -98,7 +98,7 @@ public class Participant /** * The {@link Transcriber} which owns this {@link Participant}. */ - private Transcriber transcriber; + private final Transcriber transcriber; /** * The chat room participant. @@ -118,7 +118,7 @@ public class Participant /** * A buffer which is used to locally store audio before sending */ - private ByteBuffer buffer = ByteBuffer.allocate(BUFFER_SIZE); + private final ByteBuffer buffer = ByteBuffer.allocate(BUFFER_SIZE); /** * The AudioFormat of the audio being read. It is assumed to not change @@ -766,4 +766,12 @@ public boolean isRequestingTranscription() return ext != null && Boolean.parseBoolean(ext.getText()); } + + public void flushBuffer() + { + transcriber.executorService.execute(() -> { + sendRequest(buffer.array()); + ((Buffer) buffer).clear(); + }); + } } diff --git a/src/main/java/org/jitsi/jigasi/transcription/Transcriber.java b/src/main/java/org/jitsi/jigasi/transcription/Transcriber.java index 6588c373..1e108faf 100644 --- a/src/main/java/org/jitsi/jigasi/transcription/Transcriber.java +++ b/src/main/java/org/jitsi/jigasi/transcription/Transcriber.java @@ -474,6 +474,22 @@ 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 */