Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[googlestt] Fix audio streaming reliability #14649

Merged
merged 4 commits into from
Mar 23, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -255,14 +255,20 @@ private void streamAudio(ClientStream<StreamingRecognizeRequest> clientStream, A
long startTime = System.currentTimeMillis();
long maxTranscriptionMillis = (config.maxTranscriptionSeconds * 1000L);
long maxSilenceMillis = (config.maxSilenceSeconds * 1000L);
int readBytes = 6400;
while (!aborted.get()) {
byte[] data = new byte[readBytes];
int dataN = audioStream.read(data);
final int bufferSize = 6400;
int numBytesRead;
int remaining = bufferSize;
byte[] audioBuffer = new byte[bufferSize];
while (!aborted.get() && !responseObserver.isDone()) {
numBytesRead = audioStream.read(audioBuffer, bufferSize - remaining, remaining);
if (aborted.get()) {
logger.debug("Stops listening, aborted");
break;
}
if (numBytesRead == -1) {
logger.debug("End of stream");
break;
}
if (isExpiredInterval(maxTranscriptionMillis, startTime)) {
logger.debug("Stops listening, max transcription time reached");
break;
Expand All @@ -272,18 +278,17 @@ && isExpiredInterval(maxSilenceMillis, responseObserver.getLastInputTime())) {
logger.debug("Stops listening, max silence time reached");
break;
}
if (dataN != readBytes) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
}
if (numBytesRead != remaining) {
remaining = remaining - numBytesRead;
GiviMAD marked this conversation as resolved.
Show resolved Hide resolved
continue;
}
remaining = bufferSize;
StreamingRecognizeRequest dataRequest = StreamingRecognizeRequest.newBuilder()
.setAudioContent(ByteString.copyFrom(data)).build();
logger.debug("Sending audio data {}", dataN);
.setAudioContent(ByteString.copyFrom(audioBuffer)).build();
logger.debug("Sending audio data {}", bufferSize);
clientStream.send(dataRequest);
}
audioStream.close();
}

private void sendStreamConfig(ClientStream<StreamingRecognizeRequest> clientStream,
Expand Down Expand Up @@ -335,6 +340,7 @@ private static class TranscriptionListener implements ResponseObserver<Streaming
private float confidenceSum = 0;
private int responseCount = 0;
private long lastInputTime = 0;
private boolean done = false;

public TranscriptionListener(STTListener sttListener, GoogleSTTConfiguration config, AtomicBoolean aborted) {
this.sttListener = sttListener;
Expand Down Expand Up @@ -374,7 +380,7 @@ public void onResponse(StreamingRecognizeResponse response) {
responseCount++;
// when in single utterance mode we can just get one final result so complete
if (config.singleUtteranceMode) {
onComplete();
done = true;
}
}
});
Expand Down Expand Up @@ -411,6 +417,10 @@ public void onError(@Nullable Throwable t) {
}
}

public boolean isDone() {
return done;
}

public long getLastInputTime() {
return lastInputTime;
}
Expand Down