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 1 commit
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,10 +255,12 @@ private void streamAudio(ClientStream<StreamingRecognizeRequest> clientStream, A
long startTime = System.currentTimeMillis();
long maxTranscriptionMillis = (config.maxTranscriptionSeconds * 1000L);
long maxSilenceMillis = (config.maxSilenceSeconds * 1000L);
int readBytes = 6400;
int bufferSize = 6400;
GiviMAD marked this conversation as resolved.
Show resolved Hide resolved
int numBytesRead;
int remaining = bufferSize;
byte[] audioBuffer = new byte[bufferSize];
while (!aborted.get()) {
byte[] data = new byte[readBytes];
int dataN = audioStream.read(data);
numBytesRead = audioStream.read(audioBuffer, bufferSize - remaining, remaining);
if (aborted.get()) {
logger.debug("Stops listening, aborted");
break;
Expand All @@ -272,16 +274,18 @@ && isExpiredInterval(maxSilenceMillis, responseObserver.getLastInputTime())) {
logger.debug("Stops listening, max silence time reached");
break;
}
if (dataN != readBytes) {
if (numBytesRead != remaining) {
remaining = remaining - numBytesRead;
GiviMAD marked this conversation as resolved.
Show resolved Hide resolved
try {
Thread.sleep(100);
GiviMAD marked this conversation as resolved.
Show resolved Hide resolved
} catch (InterruptedException e) {
}
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);
}
}
Expand Down