From 6e60482e68a72017be99b115b3167c1288b30ab2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=94=D0=B0=D0=BC=D1=8F=D0=BD=20=D0=9C=D0=B8=D0=BD=D0=BA?= =?UTF-8?q?=D0=BE=D0=B2?= Date: Tue, 19 Mar 2024 15:28:27 -0500 Subject: [PATCH] feat(transcription): Slow down leaving participants. (#528) * 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. * squash: Fix comments. --- .../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..06ee29097 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(() -> + { + synchronized (this) + { + numberOfScheduledParticipantsLeaving--; + } + this.transcriber.participantLeft(identifier); + }, + 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 + ); } }