Skip to content

Commit c02c32c

Browse files
committed
feat(transcriber): Limit google transcribe sessions to the length of the cost interval.
This will avoid errors like: OUT_OF_RANGE: Exceeded maximum allowed stream duration of 305 seconds.
1 parent 4abc01d commit c02c32c

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

src/main/java/org/jitsi/jigasi/transcription/GoogleCloudTranscriptionService.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -553,6 +553,18 @@ synchronized void increment(long ms)
553553
summedTime += ms;
554554
}
555555

556+
/**
557+
* Check whether adding a certain duration to the already sent time
558+
* would exceed the length of a cost interval.
559+
* @param durationInMs the duration to check.
560+
* @return true if the summed time plus the given duration
561+
* would exceed the length of a cost interval, false otherwise.
562+
*/
563+
boolean aboveCostInterval(long durationInMs)
564+
{
565+
return summedTime + durationInMs >= INTERVAL_LENGTH_MS;
566+
}
567+
556568
/**
557569
* Increments the Google Cloud Speech API requests counter.
558570
*/
@@ -723,6 +735,12 @@ void sentRequest(TranscriptionRequest request)
723735
// ByteString
724736
byte[] audio = request.getAudio();
725737
ByteString audioBytes = ByteString.copyFrom(audio);
738+
long durationInMs = request.getDurationInMs();
739+
740+
if (costLogger.aboveCostInterval(durationInMs))
741+
{
742+
this.terminateCurrentSession();
743+
}
726744

727745
synchronized(currentRequestObserverLock)
728746
{
@@ -735,7 +753,7 @@ void sentRequest(TranscriptionRequest request)
735753
= createObserver(getRecognitionConfig(request));
736754
}
737755

738-
costLogger.increment(request.getDurationInMs());
756+
costLogger.increment(durationInMs);
739757
costLogger.incrementRequestsCounter();
740758

741759
currentRequestObserver.onNext(

0 commit comments

Comments
 (0)