Skip to content

Commit

Permalink
Rely on the xmpp signaling to flush the buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
rpurdel committed Oct 3, 2024
1 parent 7140a35 commit ae318aa
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 40 deletions.
38 changes: 36 additions & 2 deletions src/main/java/org/jitsi/jigasi/TranscriptionGatewaySession.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;
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("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
39 changes: 1 addition & 38 deletions src/main/java/org/jitsi/jigasi/transcription/Participant.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down Expand Up @@ -534,7 +524,6 @@ void joined()
session.addTranscriptionListener(this);
sessions.put(getLanguageKey(), session);
isCompleted = false;
startBufferCheck();
}
}

Expand Down Expand Up @@ -566,7 +555,6 @@ public void left()
*/
void giveBuffer(javax.media.Buffer buffer)
{
lastAudioReceivedTime = System.currentTimeMillis();
if (audioFormat == null)
{
audioFormat = (AudioFormat) buffer.getFormat();
Expand Down Expand Up @@ -657,7 +645,6 @@ else if (silenceFilter.newSpeech())
try
{
buffer.put(toBuffer);
flushed = false;
}
catch (BufferOverflowException | ReadOnlyBufferException e)
{
Expand Down Expand Up @@ -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;
});
}
}
14 changes: 14 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,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
*/
Expand Down

0 comments on commit ae318aa

Please sign in to comment.