From bd40dcbb5b081b52551fc11bf2e9837425d06e83 Mon Sep 17 00:00:00 2001 From: Shin Fan Date: Fri, 23 Sep 2016 14:32:49 -0700 Subject: [PATCH] DO NOT MERGE: Add streaming support to Speech API. --- .../cloud/speech/spi/v1beta1/SpeechApi.java | 15 ++++++++++- .../speech/spi/v1beta1/SpeechSettings.java | 27 +++++++++++++++++-- 2 files changed, 39 insertions(+), 3 deletions(-) diff --git a/google-cloud-speech/src/main/java/com/google/cloud/speech/spi/v1beta1/SpeechApi.java b/google-cloud-speech/src/main/java/com/google/cloud/speech/spi/v1beta1/SpeechApi.java index a77b0d40a1c8..1947ec93b096 100644 --- a/google-cloud-speech/src/main/java/com/google/cloud/speech/spi/v1beta1/SpeechApi.java +++ b/google-cloud-speech/src/main/java/com/google/cloud/speech/spi/v1beta1/SpeechApi.java @@ -14,14 +14,18 @@ package com.google.cloud.speech.spi.v1beta1; import com.google.api.gax.grpc.ApiCallable; -import com.google.api.gax.protobuf.PathTemplate; import com.google.cloud.speech.v1beta1.AsyncRecognizeRequest; import com.google.cloud.speech.v1beta1.RecognitionAudio; import com.google.cloud.speech.v1beta1.RecognitionConfig; +import com.google.cloud.speech.v1beta1.StreamingRecognizeRequest; +import com.google.cloud.speech.v1beta1.StreamingRecognizeResponse; import com.google.cloud.speech.v1beta1.SyncRecognizeRequest; import com.google.cloud.speech.v1beta1.SyncRecognizeResponse; import com.google.longrunning.Operation; + import io.grpc.ManagedChannel; +import io.grpc.stub.StreamObserver; + import java.io.Closeable; import java.io.IOException; import java.util.ArrayList; @@ -90,6 +94,8 @@ public class SpeechApi implements AutoCloseable { private final ApiCallable syncRecognizeCallable; private final ApiCallable asyncRecognizeCallable; + private final ApiCallable, + StreamObserver> streamingRecognizeCallable; public final SpeechSettings getSettings() { return settings; @@ -125,6 +131,8 @@ protected SpeechApi(SpeechSettings settings) throws IOException { ApiCallable.create(settings.syncRecognizeSettings(), this.channel, this.executor); this.asyncRecognizeCallable = ApiCallable.create(settings.asyncRecognizeSettings(), this.channel, this.executor); + this.streamingRecognizeCallable = + ApiCallable.create(settings.streamingRecognizeSettings(), this.channel, this.executor); if (settings.getChannelProvider().shouldAutoClose()) { closeables.add( @@ -146,6 +154,11 @@ public void close() throws IOException { } } + public final StreamObserver streamingRecognize( + StreamObserver streamObserver) { + return streamingRecognizeCallable.call(streamObserver); + } + // AUTO-GENERATED DOCUMENTATION AND METHOD /** * Perform synchronous speech-recognition: receive results after all audio diff --git a/google-cloud-speech/src/main/java/com/google/cloud/speech/spi/v1beta1/SpeechSettings.java b/google-cloud-speech/src/main/java/com/google/cloud/speech/spi/v1beta1/SpeechSettings.java index 1b3c3774be9c..3ede1e099c37 100644 --- a/google-cloud-speech/src/main/java/com/google/cloud/speech/spi/v1beta1/SpeechSettings.java +++ b/google-cloud-speech/src/main/java/com/google/cloud/speech/spi/v1beta1/SpeechSettings.java @@ -16,12 +16,13 @@ import com.google.api.gax.core.ConnectionSettings; import com.google.api.gax.core.RetrySettings; import com.google.api.gax.grpc.ApiCallSettings; -import com.google.api.gax.grpc.PageStreamingDescriptor; import com.google.api.gax.grpc.ServiceApiSettings; import com.google.api.gax.grpc.SimpleCallSettings; import com.google.auth.Credentials; import com.google.cloud.speech.v1beta1.AsyncRecognizeRequest; import com.google.cloud.speech.v1beta1.SpeechGrpc; +import com.google.cloud.speech.v1beta1.StreamingRecognizeRequest; +import com.google.cloud.speech.v1beta1.StreamingRecognizeResponse; import com.google.cloud.speech.v1beta1.SyncRecognizeRequest; import com.google.cloud.speech.v1beta1.SyncRecognizeResponse; import com.google.common.collect.ImmutableList; @@ -30,12 +31,16 @@ import com.google.common.collect.Lists; import com.google.common.collect.Sets; import com.google.longrunning.Operation; + +import org.joda.time.Duration; + import io.grpc.ManagedChannel; import io.grpc.Status; +import io.grpc.stub.StreamObserver; + import java.io.IOException; import java.util.List; import java.util.concurrent.ScheduledExecutorService; -import org.joda.time.Duration; // AUTO-GENERATED DOCUMENTATION AND CLASS /** @@ -95,6 +100,8 @@ public class SpeechSettings extends ServiceApiSettings { private final SimpleCallSettings syncRecognizeSettings; private final SimpleCallSettings asyncRecognizeSettings; + private final SimpleCallSettings, + StreamObserver> streamingRecognizeSettings; /** * Returns the object with the settings used for calls to syncRecognize. @@ -110,6 +117,14 @@ public SimpleCallSettings asyncRecognizeSettin return asyncRecognizeSettings; } + /** + * Returns the object with the settings used for calls to asyncRecognize. + */ + public SimpleCallSettings, + StreamObserver> streamingRecognizeSettings() { + return streamingRecognizeSettings; + } + /** * Returns the default service address. */ @@ -163,6 +178,7 @@ private SpeechSettings(Builder settingsBuilder) throws IOException { syncRecognizeSettings = settingsBuilder.syncRecognizeSettings().build(); asyncRecognizeSettings = settingsBuilder.asyncRecognizeSettings().build(); + streamingRecognizeSettings = settingsBuilder.streamingRecognizeSettings().build(); } /** @@ -174,6 +190,8 @@ public static class Builder extends ServiceApiSettings.Builder { private SimpleCallSettings.Builder syncRecognizeSettings; private SimpleCallSettings.Builder asyncRecognizeSettings; + private SimpleCallSettings.Builder, + StreamObserver> streamingRecognizeSettings; private static final ImmutableMap> RETRYABLE_CODE_DEFINITIONS; @@ -315,6 +333,11 @@ public SimpleCallSettings.Builder asyncRecogni return asyncRecognizeSettings; } + public SimpleCallSettings.Builder, + StreamObserver> streamingRecognizeSettings() { + return streamingRecognizeSettings; + } + @Override public SpeechSettings build() throws IOException { return new SpeechSettings(this);