Index: content/browser/speech/speech_recognizer.cc |
diff --git a/content/browser/speech/speech_recognizer.cc b/content/browser/speech/speech_recognizer.cc |
index 295f156c7efba488efa9a8457bce92a4a94b20e2..f81128c3c94867530616c3f1bbf9e4560e224226 100644 |
--- a/content/browser/speech/speech_recognizer.cc |
+++ b/content/browser/speech/speech_recognizer.cc |
@@ -145,6 +145,7 @@ void SpeechRecognizer::StopRecording() { |
audio_controller_->Close(); |
audio_controller_ = NULL; // Releases the ref ptr. |
+ delegate_->DidStopReceivingSpeech(caller_id_); |
delegate_->DidCompleteRecording(caller_id_); |
// UploadAudioChunk requires a non-empty final buffer. So we encode a packet |
@@ -186,7 +187,7 @@ void SpeechRecognizer::HandleOnError(int error_code) { |
if (!audio_controller_.get()) |
return; |
- InformErrorAndCancelRecognition(RECOGNIZER_ERROR_CAPTURE); |
+ InformErrorAndCancelRecognition(kErrorAudio); |
} |
void SpeechRecognizer::OnData(AudioInputController* controller, |
@@ -210,6 +211,8 @@ void SpeechRecognizer::HandleOnData(string* data) { |
return; |
} |
+ bool speech_was_heard_before_packet = endpointer_.DidStartReceivingSpeech(); |
+ |
const short* samples = reinterpret_cast<const short*>(data->data()); |
DCHECK((data->length() % sizeof(short)) == 0); |
int num_samples = data->length() / sizeof(short); |
@@ -246,12 +249,16 @@ void SpeechRecognizer::HandleOnData(string* data) { |
} |
// Check if we have waited too long without hearing any speech. |
- if (!endpointer_.DidStartReceivingSpeech() && |
+ bool speech_was_heard_after_packet = endpointer_.DidStartReceivingSpeech(); |
+ if (!speech_was_heard_after_packet && |
num_samples_recorded_ >= kNoSpeechTimeoutSec * kAudioSampleRate) { |
- InformErrorAndCancelRecognition(RECOGNIZER_ERROR_NO_SPEECH); |
+ InformErrorAndCancelRecognition(kErrorNoSpeech); |
return; |
} |
+ if (!speech_was_heard_before_packet && speech_was_heard_after_packet) |
+ delegate_->DidStartReceivingSpeech(caller_id_); |
+ |
// Calculate the input volume to display in the UI, smoothing towards the |
// new level. |
float level = (rms - kAudioMeterMinDb) / |
@@ -271,30 +278,26 @@ void SpeechRecognizer::HandleOnData(string* data) { |
delegate_->SetInputVolume(caller_id_, did_clip ? 1.0f : audio_level_, |
noise_level); |
- if (endpointer_.speech_input_complete()) { |
+ if (endpointer_.speech_input_complete()) |
StopRecording(); |
- } |
- |
- // TODO(satish): Once we have streaming POST, start sending the data received |
- // here as POST chunks. |
} |
void SpeechRecognizer::SetRecognitionResult( |
- bool error, const SpeechInputResultArray& result) { |
- if (error || result.empty()) { |
- InformErrorAndCancelRecognition(error ? RECOGNIZER_ERROR_NETWORK : |
- RECOGNIZER_ERROR_NO_RESULTS); |
+ const SpeechInputResult& result) { |
+ if (result.error != kErrorNone) { |
+ InformErrorAndCancelRecognition(result.error); |
return; |
} |
- delegate_->SetRecognitionResult(caller_id_, error, result); |
- |
// Guard against the delegate freeing us until we finish our job. |
scoped_refptr<SpeechRecognizer> me(this); |
+ delegate_->SetRecognitionResult(caller_id_, result); |
delegate_->DidCompleteRecognition(caller_id_); |
} |
-void SpeechRecognizer::InformErrorAndCancelRecognition(ErrorCode error) { |
+void SpeechRecognizer::InformErrorAndCancelRecognition( |
+ SpeechInputError error) { |
+ DCHECK_NE(error, kErrorNone); |
CancelRecognition(); |
// Guard against the delegate freeing us until we finish our job. |