From e4794246a678d7dd7d07c3b8d93e8dc50bd714a9 Mon Sep 17 00:00:00 2001 From: damencho Date: Mon, 18 Mar 2024 17:47:22 -0500 Subject: [PATCH] feat(transcription): Slow down leaving participants. Slow down events internally in the transcriber, so we give time for the service to return any pending transcriptions. We also wait for all leave processing to finish before transcriber leaves the room, so any sent transcriptions to be processed. --- .../jigasi/TranscriptionGatewaySession.java | 72 ++++++++++++++----- 1 file changed, 54 insertions(+), 18 deletions(-) diff --git a/src/main/java/org/jitsi/jigasi/TranscriptionGatewaySession.java b/src/main/java/org/jitsi/jigasi/TranscriptionGatewaySession.java index 5113017ae..cdc3d92cd 100644 --- a/src/main/java/org/jitsi/jigasi/TranscriptionGatewaySession.java +++ b/src/main/java/org/jitsi/jigasi/TranscriptionGatewaySession.java @@ -21,6 +21,7 @@ import net.java.sip.communicator.service.protocol.*; import net.java.sip.communicator.service.protocol.event.*; import net.java.sip.communicator.service.protocol.media.*; +import org.jitsi.utils.concurrent.*; import org.jitsi.xmpp.extensions.jitsimeet.*; import org.jitsi.jigasi.transcription.*; import org.jitsi.service.neomedia.*; @@ -34,6 +35,7 @@ import org.jxmpp.stringprep.*; import java.util.*; +import java.util.concurrent.*; /** * A TranscriptionGatewaySession is able to join a JVB conference and @@ -68,7 +70,7 @@ public class TranscriptionGatewaySession * How long the transcriber should wait until really leaving the conference * when no participant is requesting transcription anymore. */ - public final static int PRESENCE_UPDATE_WAIT_UNTIL_LEAVE_DURATION = 2500; + public final static int PRESENCE_UPDATE_WAIT_UNTIL_LEAVE_DURATION = 3000; /** * The TranscriptionService used by this session @@ -113,6 +115,17 @@ public class TranscriptionGatewaySession */ private boolean isBackendTranscribingEnabled = false; + /** + * The thread pool to serve all leave operations. + */ + private static final ScheduledExecutorService leaveThreadPool = Executors.newScheduledThreadPool( + 2, new CustomizableThreadFactory("participants-leaving", true)); + + /** + * Keeps the number of currently leaving participants. + */ + private int numberOfScheduledParticipantsLeaving = 0; + /** * Create a TranscriptionGatewaySession which can handle the transcription * of a JVB conference @@ -307,7 +320,28 @@ void notifyChatRoomMemberLeft(ChatRoomMember chatMember) super.notifyChatRoomMemberLeft(chatMember); String identifier = getParticipantIdentifier(chatMember); - this.transcriber.participantLeft(identifier); + if ("focus".equals(identifier) || this.jvbConference.getResourceIdentifier().toString().equals(identifier)) + { + return; + } + + synchronized (this) + { + numberOfScheduledParticipantsLeaving++; + } + + // give some time for the transcriptions for this participant to be ready + leaveThreadPool.schedule(() -> + { + this.transcriber.participantLeft(identifier); + synchronized (this) + { + numberOfScheduledParticipantsLeaving--; + } + }, + PRESENCE_UPDATE_WAIT_UNTIL_LEAVE_DURATION, + TimeUnit.MILLISECONDS + ); } @Override @@ -332,22 +366,24 @@ private void maybeStopTranscription() { if (transcriber.isTranscribing() && !isTranscriptionRequested()) { - new Thread(() -> - { - try - { - Thread.sleep(PRESENCE_UPDATE_WAIT_UNTIL_LEAVE_DURATION); - } - catch (InterruptedException e) - { - logger.error(e); - } - - if (!isTranscriptionRequested()) - { - jvbConference.stop(); - } - }).start(); + // let's give some time for the transcriptions to finish + leaveThreadPool.schedule(() -> { + if (transcriber.isTranscribing() && !isTranscriptionRequested()) + { + if (this.numberOfScheduledParticipantsLeaving == 0) + { + jvbConference.stop(); + } + else + { + // there seems to be still participants leaving, give them time before transcriber leaves + maybeStopTranscription(); + } + } + }, + PRESENCE_UPDATE_WAIT_UNTIL_LEAVE_DURATION, + TimeUnit.MILLISECONDS + ); } }