|
| 1 | +/* |
| 2 | + * Copyright 2017 Google Inc. All Rights Reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.google.cloud.speech.v1.it; |
| 18 | + |
| 19 | +import com.google.api.gax.rpc.ApiStreamObserver; |
| 20 | +import com.google.cloud.speech.v1.LongRunningRecognizeResponse; |
| 21 | +import com.google.cloud.speech.v1.RecognitionAudio; |
| 22 | +import com.google.cloud.speech.v1.RecognitionConfig; |
| 23 | +import com.google.cloud.speech.v1.RecognizeResponse; |
| 24 | +import com.google.cloud.speech.v1.SpeechClient; |
| 25 | +import com.google.cloud.speech.v1.StreamingRecognitionConfig; |
| 26 | +import com.google.cloud.speech.v1.StreamingRecognizeRequest; |
| 27 | +import com.google.cloud.speech.v1.StreamingRecognizeResponse; |
| 28 | +import com.google.common.io.Resources; |
| 29 | +import com.google.common.truth.Truth; |
| 30 | +import com.google.common.util.concurrent.SettableFuture; |
| 31 | +import com.google.protobuf.ByteString; |
| 32 | +import org.junit.AfterClass; |
| 33 | +import org.junit.BeforeClass; |
| 34 | +import org.junit.Rule; |
| 35 | +import org.junit.Test; |
| 36 | +import org.junit.rules.Timeout; |
| 37 | + |
| 38 | +import java.net.URL; |
| 39 | +import java.util.List; |
| 40 | + |
| 41 | +public class ITSpeechTest { |
| 42 | + private static SpeechClient speechClient; |
| 43 | + |
| 44 | + @Rule |
| 45 | + public Timeout globalTimeout = Timeout.seconds(300); |
| 46 | + |
| 47 | + @BeforeClass |
| 48 | + public static void setupClass() throws Exception { |
| 49 | + speechClient = SpeechClient.create(); |
| 50 | + } |
| 51 | + |
| 52 | + @AfterClass |
| 53 | + public static void tearDownClass() throws Exception { |
| 54 | + speechClient.close(); |
| 55 | + } |
| 56 | + |
| 57 | + @Test |
| 58 | + public void syncRecognize() { |
| 59 | + RecognizeResponse response = speechClient.recognize(config(), audio()); |
| 60 | + |
| 61 | + Truth.assertThat(response.getResultsCount()).isGreaterThan(0); |
| 62 | + Truth.assertThat(response.getResults(0).getAlternativesCount()).isGreaterThan(0); |
| 63 | + String text = response.getResults(0).getAlternatives(0).getTranscript(); |
| 64 | + Truth.assertThat(text).isEqualTo("hello"); |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + public void longrunningRecognize() throws Exception { |
| 69 | + LongRunningRecognizeResponse response = speechClient.longRunningRecognizeAsync(config(), audio()).get(); |
| 70 | + |
| 71 | + Truth.assertThat(response.getResultsCount()).isGreaterThan(0); |
| 72 | + Truth.assertThat(response.getResults(0).getAlternativesCount()).isGreaterThan(0); |
| 73 | + String text = response.getResults(0).getAlternatives(0).getTranscript(); |
| 74 | + Truth.assertThat(text).isEqualTo("hello"); |
| 75 | + } |
| 76 | + |
| 77 | + @Test |
| 78 | + public void streamingRecognize() throws Exception { |
| 79 | + byte[] audioBytes = Resources.toByteArray(new URL("https://storage.googleapis.com/gapic-toolkit/hello.flac")); |
| 80 | + |
| 81 | + StreamingRecognitionConfig streamingConfig = StreamingRecognitionConfig.newBuilder() |
| 82 | + .setConfig(config()) |
| 83 | + .build(); |
| 84 | + |
| 85 | + ResponseApiStreamingObserver<StreamingRecognizeResponse> responseObserver = |
| 86 | + new ResponseApiStreamingObserver<>(); |
| 87 | + |
| 88 | + ApiStreamObserver<StreamingRecognizeRequest> requestObserver = |
| 89 | + speechClient.streamingRecognizeCallable().bidiStreamingCall(responseObserver); |
| 90 | + |
| 91 | + // The first request must **only** contain the audio configuration: |
| 92 | + requestObserver.onNext(StreamingRecognizeRequest.newBuilder() |
| 93 | + .setStreamingConfig(streamingConfig) |
| 94 | + .build()); |
| 95 | + |
| 96 | + // Subsequent requests must **only** contain the audio data. |
| 97 | + requestObserver.onNext(StreamingRecognizeRequest.newBuilder() |
| 98 | + .setAudioContent(ByteString.copyFrom(audioBytes)) |
| 99 | + .build()); |
| 100 | + |
| 101 | + // Mark transmission as completed after sending the data. |
| 102 | + requestObserver.onCompleted(); |
| 103 | + |
| 104 | + List<StreamingRecognizeResponse> responses = responseObserver.future().get(); |
| 105 | + |
| 106 | + Truth.assertThat(responses.size()).isGreaterThan(0); |
| 107 | + Truth.assertThat(responses.get(0).getResultsCount()).isGreaterThan(0); |
| 108 | + Truth.assertThat(responses.get(0).getResults(0).getAlternativesCount()).isGreaterThan(0); |
| 109 | + String text = responses.get(0).getResults(0).getAlternatives(0).getTranscript(); |
| 110 | + Truth.assertThat(text).isEqualTo("hello"); |
| 111 | + } |
| 112 | + |
| 113 | + private static class ResponseApiStreamingObserver<T> implements ApiStreamObserver<T> { |
| 114 | + private final SettableFuture<List<T>> future = SettableFuture.create(); |
| 115 | + private final List<T> messages = new java.util.ArrayList<T>(); |
| 116 | + |
| 117 | + @Override |
| 118 | + public void onNext(T message) { |
| 119 | + messages.add(message); |
| 120 | + } |
| 121 | + |
| 122 | + @Override |
| 123 | + public void onError(Throwable t) { |
| 124 | + future.setException(t); |
| 125 | + } |
| 126 | + |
| 127 | + @Override |
| 128 | + public void onCompleted() { |
| 129 | + future.set(messages); |
| 130 | + } |
| 131 | + |
| 132 | + // Returns the SettableFuture object to get received messages / exceptions. |
| 133 | + public SettableFuture<List<T>> future() { |
| 134 | + return future; |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + private RecognitionConfig config() { |
| 139 | + String languageCode = "en-US"; |
| 140 | + int sampleRateHertz = 44100; |
| 141 | + RecognitionConfig.AudioEncoding encoding = RecognitionConfig.AudioEncoding.FLAC; |
| 142 | + RecognitionConfig config = |
| 143 | + RecognitionConfig.newBuilder() |
| 144 | + .setLanguageCode(languageCode) |
| 145 | + .setSampleRateHertz(sampleRateHertz) |
| 146 | + .setEncoding(encoding) |
| 147 | + .build(); |
| 148 | + return config; |
| 149 | + } |
| 150 | + |
| 151 | + public RecognitionAudio audio() { |
| 152 | + return RecognitionAudio.newBuilder().setUri("gs://gapic-toolkit/hello.flac").build(); |
| 153 | + } |
| 154 | +} |
0 commit comments