14
14
* limitations under the License.
15
15
*/
16
16
17
- // Client that sends audio to Speech.AsyncRecognize via gRPC and returns transcription.
17
+ // Client that sends audio to Speech.AsyncRecognize via gRPC and returns longrunning operation.
18
+ // The results are received via the google.longrunning.Operations interface.
18
19
//
19
20
// Uses a service account for OAuth2 authentication, which you may obtain at
20
21
// https://console.developers.google.com
26
27
package com .google .cloud .speech .grpc .demos ;
27
28
28
29
import com .google .auth .oauth2 .GoogleCredentials ;
30
+ import com .google .cloud .speech .v1beta1 .AsyncRecognizeRequest ;
29
31
import com .google .cloud .speech .v1beta1 .AsyncRecognizeResponse ;
30
32
import com .google .cloud .speech .v1beta1 .RecognitionAudio ;
31
33
import com .google .cloud .speech .v1beta1 .RecognitionConfig ;
32
34
import com .google .cloud .speech .v1beta1 .RecognitionConfig .AudioEncoding ;
33
35
import com .google .cloud .speech .v1beta1 .SpeechGrpc ;
34
- import com .google .cloud .speech .v1beta1 .AsyncRecognizeRequest ;
35
- import com .google .cloud .speech .v1beta1 .AsyncRecognizeResponse ;
36
- import com .google .protobuf .TextFormat ;
37
36
38
37
import com .google .longrunning .GetOperationRequest ;
39
38
import com .google .longrunning .Operation ;
@@ -79,7 +78,7 @@ public class AsyncRecognizeClient {
79
78
80
79
private final ManagedChannel channel ;
81
80
private final SpeechGrpc .SpeechBlockingStub stub ;
82
- private final OperationsGrpc .OperationsBlockingStub op ;
81
+ private final OperationsGrpc .OperationsBlockingStub statusStub ;
83
82
84
83
/**
85
84
* Construct client connecting to Cloud Speech server at {@code host:port}.
@@ -98,7 +97,7 @@ public AsyncRecognizeClient(String host, int port, URI input, int samplingRate)
98
97
.intercept (new ClientAuthInterceptor (creds , Executors .newSingleThreadExecutor ()))
99
98
.build ();
100
99
stub = SpeechGrpc .newBlockingStub (channel );
101
- op = OperationsGrpc .newBlockingStub (channel );
100
+ statusStub = OperationsGrpc .newBlockingStub (channel );
102
101
103
102
logger .info ("Created stub for " + host + ":" + port );
104
103
}
@@ -130,33 +129,36 @@ public void recognize() {
130
129
.setAudio (audio )
131
130
.build ();
132
131
133
- Operation operation , status ;
132
+ Operation operation ;
133
+ Operation status ;
134
134
try {
135
135
operation = stub .asyncRecognize (request );
136
136
137
137
//Print the long running operation handle
138
138
logger .log (Level .INFO , String .format ("Operation handle: %s, URI: %s" , operation .getName (),
139
- input .toString ()));
139
+ input .toString ()));
140
140
} catch (StatusRuntimeException e ) {
141
141
logger .log (Level .WARNING , "RPC failed: {0}" , e .getStatus ());
142
142
return ;
143
143
}
144
144
145
- while (true ) {
145
+ while (true ) {
146
146
try {
147
147
logger .log (Level .INFO , "Waiting 2s for operation, {0} processing..." , operation .getName ());
148
148
Thread .sleep (2000 );
149
149
GetOperationRequest operationReq = GetOperationRequest .newBuilder ()
150
150
.setName (operation .getName ())
151
151
.build ();
152
- status = op .getOperation (
152
+ status = statusStub .getOperation (
153
153
GetOperationRequest .newBuilder ()
154
154
.setName (operation .getName ())
155
155
.build ()
156
156
);
157
157
158
- if (status .getDone ()) break ;
159
- }catch (Exception ex ) {
158
+ if (status .getDone ()) {
159
+ break ;
160
+ }
161
+ } catch (Exception ex ) {
160
162
logger .log (Level .WARNING , ex .getMessage ());
161
163
}
162
164
}
@@ -165,7 +167,7 @@ public void recognize() {
165
167
AsyncRecognizeResponse asyncRes = status .getResponse ().unpack (AsyncRecognizeResponse .class );
166
168
167
169
logger .info ("Received response: " + asyncRes );
168
- } catch (com .google .protobuf .InvalidProtocolBufferException ex ) {
170
+ } catch (com .google .protobuf .InvalidProtocolBufferException ex ) {
169
171
logger .log (Level .WARNING , "Unpack error, {0}" ,ex .getMessage ());
170
172
}
171
173
}
0 commit comments