Skip to content

Commit

Permalink
fix: Flush the participant's audio buffer on mute (#559)
Browse files Browse the repository at this point in the history
* Flush the participant's audio buffer on mute

* squash: Adds callContext to a log message.

---------

Co-authored-by: damencho <[email protected]>
  • Loading branch information
rpurdel and damencho authored Oct 10, 2024
1 parent 368c523 commit 509a4f6
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 5 deletions.
40 changes: 37 additions & 3 deletions src/main/java/org/jitsi/jigasi/TranscriptionGatewaySession.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;
Expand All @@ -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.*;
Expand Down Expand Up @@ -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);

Expand Down
12 changes: 10 additions & 2 deletions src/main/java/org/jitsi/jigasi/transcription/Participant.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand Down Expand Up @@ -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();
});
}
}
16 changes: 16 additions & 0 deletions src/main/java/org/jitsi/jigasi/transcription/Transcriber.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down

0 comments on commit 509a4f6

Please sign in to comment.