Skip to content

Commit f6c5ce9

Browse files
jmukstephenplusplus
authored andcommitted
Speech API: fix createRecognizeStream timeout. (googleapis#1915)
1 parent 254fcd1 commit f6c5ce9

4 files changed

Lines changed: 71 additions & 12 deletions

File tree

packages/speech/src/index.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -364,11 +364,20 @@ Speech.formatResults_ = function(resultSets, verboseMode) {
364364
* property, a `confidence` score from `0` - `100`, and an `alternatives`
365365
* array consisting of other transcription possibilities.
366366
*
367+
* Google Cloud Speech sets the limits for the audio duration. For more
368+
* information, see
369+
* [Content Limits]{@link https://cloud.google.com/speech/limits#content}.
370+
*
367371
* @resource [StreamingRecognize API Reference]{@link https://cloud.google.com/speech/reference/rpc/google.cloud.speech.v1beta1#google.cloud.speech.v1beta1.Speech.StreamingRecognize}
368372
* @resource [StreamingRecognizeRequest API Reference]{@link https://cloud.google.com/speech/reference/rpc/google.cloud.speech.v1beta1#google.cloud.speech.v1beta1.StreamingRecognizeRequest}
373+
* @resource [Content Limits]{@link https://cloud.google.com/speech/limits#content}
369374
*
370375
* @param {object} config - A `StreamingRecognitionConfig` object. See
371376
* [`StreamingRecognitionConfig`](https://cloud.google.com/speech/reference/rpc/google.cloud.speech.v1beta1#google.cloud.speech.v1beta1.StreamingRecognitionConfig).
377+
* @param {number=} config.timeout - In seconds, the amount of time before the
378+
* underlying API request times out. The default value, `190`, is sufficient
379+
* for audio input of 60 seconds or less. If your input is longer, consider
380+
* using a higher timeout value.
372381
* @param {boolean=} config.verbose - Enable verbose mode for a more detailed
373382
* response. See the examples below. Default: `false`.
374383
*
@@ -469,10 +478,17 @@ Speech.prototype.createRecognizeStream = function(config) {
469478
var verboseMode = config.verbose === true;
470479
delete config.verbose;
471480

481+
var gaxOptions = {};
482+
483+
if (is.number(config.timeout)) {
484+
gaxOptions.timeout = config.timeout * 1000;
485+
delete config.timeout;
486+
}
487+
472488
var recognizeStream = streamEvents(pumpify.obj());
473489

474490
recognizeStream.once('writing', function() {
475-
var requestStream = self.api.Speech.streamingRecognize();
491+
var requestStream = self.api.Speech.streamingRecognize(gaxOptions);
476492

477493
requestStream.on('response', function(response) {
478494
recognizeStream.emit('response', response);

packages/speech/src/v1beta1/speech_client.js

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,8 @@ function SpeechClient(gaxGrpc, grpcClients, opts) {
7979
'gax/' + gax.version,
8080
'nodejs/' + process.version].join(' ');
8181

82-
var operationsClient = new gax.lro({
82+
83+
this.operationsClient = new gax.lro({
8384
auth: gaxGrpc.auth,
8485
grpc: gaxGrpc.grpc
8586
}).operationsClient({
@@ -91,9 +92,9 @@ function SpeechClient(gaxGrpc, grpcClients, opts) {
9192
appVersion: appVersion
9293
});
9394

94-
var longrunningDescriptors = {
95+
this.longrunningDescriptors = {
9596
asyncRecognize: new gax.LongrunningDescriptor(
96-
operationsClient,
97+
this.operationsClient,
9798
grpcClients.google.cloud.speech.v1beta1.AsyncRecognizeResponse.decode,
9899
grpcClients.google.cloud.speech.v1beta1.AsyncRecognizeMetadata.decode)
99100
};
@@ -104,6 +105,8 @@ function SpeechClient(gaxGrpc, grpcClients, opts) {
104105
clientConfig,
105106
{'x-goog-api-client': googleApiClient});
106107

108+
var self = this;
109+
107110
var speechStub = gaxGrpc.createStub(
108111
servicePath,
109112
port,
@@ -115,13 +118,16 @@ function SpeechClient(gaxGrpc, grpcClients, opts) {
115118
'streamingRecognize'
116119
];
117120
speechStubMethods.forEach(function(methodName) {
118-
this['_' + methodName] = gax.createApiCall(
121+
self['_' + methodName] = gax.createApiCall(
119122
speechStub.then(function(speechStub) {
120-
return speechStub[methodName].bind(speechStub);
123+
return function() {
124+
var args = Array.prototype.slice.call(arguments, 0);
125+
return speechStub[methodName].apply(speechStub, args);
126+
};
121127
}),
122128
defaults[methodName],
123-
STREAM_DESCRIPTORS[methodName] || longrunningDescriptors[methodName]);
124-
}.bind(this));
129+
STREAM_DESCRIPTORS[methodName] || self.longrunningDescriptors[methodName]);
130+
});
125131
}
126132

127133
// Service calls

packages/speech/src/v1beta1/speech_client_config.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
"retry_params_name": "default"
3434
},
3535
"StreamingRecognize": {
36-
"timeout_millis": 60000,
36+
"timeout_millis": 190000,
3737
"retry_codes_name": "non_idempotent",
3838
"retry_params_name": "default"
3939
}

packages/speech/test/index.js

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -589,16 +589,53 @@ describe('Speech', function() {
589589
var stream = through.obj();
590590

591591
stream.on('data', function(data) {
592-
assert.deepEqual(data, {
593-
streamingConfig: {} // No `verbose` property.
594-
});
592+
assert.strictEqual(data.streamingConfig.verbose, undefined);
593+
done();
594+
});
595+
596+
return stream;
597+
}
598+
};
599+
600+
stream.emit('writing');
601+
});
602+
603+
it('should allow specifying a timeout', function(done) {
604+
var timeout = 200;
605+
var expectedTimeout = 200 * 1000;
606+
607+
speech.api.Speech = {
608+
streamingRecognize: function(opts) {
609+
assert.strictEqual(opts.timeout, expectedTimeout);
610+
done();
611+
}
612+
};
613+
614+
var stream = speech.createRecognizeStream({
615+
timeout: timeout
616+
});
617+
618+
stream.emit('writing');
619+
});
620+
621+
it('should delete timeout option from request object', function(done) {
622+
speech.api.Speech = {
623+
streamingRecognize: function() {
624+
var stream = through.obj();
625+
626+
stream.on('data', function(data) {
627+
assert.strictEqual(data.streamingConfig.timeout, undefined);
595628
done();
596629
});
597630

598631
return stream;
599632
}
600633
};
601634

635+
var stream = speech.createRecognizeStream({
636+
timeout: 90
637+
});
638+
602639
stream.emit('writing');
603640
});
604641
});

0 commit comments

Comments
 (0)