summaryrefslogtreecommitdiffstats
path: root/content/browser/speech
diff options
context:
space:
mode:
authorleandrogracia@chromium.org <leandrogracia@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-10-07 13:29:35 +0000
committerleandrogracia@chromium.org <leandrogracia@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-10-07 13:29:35 +0000
commitd1ecb010a30afb74dfd33294771c9a1559f62141 (patch)
tree259e50648ff9445ff7c43f713c960a31f55e7b2b /content/browser/speech
parentb773cee71532fbce19c1b92f256a950979764c8c (diff)
downloadchromium_src-d1ecb010a30afb74dfd33294771c9a1559f62141.zip
chromium_src-d1ecb010a30afb74dfd33294771c9a1559f62141.tar.gz
chromium_src-d1ecb010a30afb74dfd33294771c9a1559f62141.tar.bz2
Applying changes to the existing speech input code to support the extension API.
This includes extended error management by handling the status response code and the DidStartReceivingSpeech/DidStopReceivingSpeech events. BUG=97388 TEST=none Review URL: http://codereview.chromium.org/8137005 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@104471 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'content/browser/speech')
-rw-r--r--content/browser/speech/speech_input_browsertest.cc7
-rw-r--r--content/browser/speech/speech_input_dispatcher_host.cc2
-rw-r--r--content/browser/speech/speech_input_dispatcher_host.h2
-rw-r--r--content/browser/speech/speech_input_manager.cc10
-rw-r--r--content/browser/speech/speech_input_manager.h25
-rw-r--r--content/browser/speech/speech_recognition_request.cc51
-rw-r--r--content/browser/speech/speech_recognition_request.h3
-rw-r--r--content/browser/speech/speech_recognition_request_unittest.cc62
-rw-r--r--content/browser/speech/speech_recognizer.cc33
-rw-r--r--content/browser/speech/speech_recognizer.h25
-rw-r--r--content/browser/speech/speech_recognizer_unittest.cc61
11 files changed, 166 insertions, 115 deletions
diff --git a/content/browser/speech/speech_input_browsertest.cc b/content/browser/speech/speech_input_browsertest.cc
index eae9b23..46ac76e 100644
--- a/content/browser/speech/speech_input_browsertest.cc
+++ b/content/browser/speech/speech_input_browsertest.cc
@@ -109,7 +109,7 @@ class FakeSpeechInputManager : public SpeechInputManager {
float noise_volume) {}
virtual void ShowNoMicError(int caller_id) {}
virtual void ShowRecognizerError(int caller_id,
- SpeechRecognizer::ErrorCode error) {}
+ SpeechInputError error) {}
virtual void DoClose(int caller_id) {}
private:
@@ -117,8 +117,9 @@ class FakeSpeechInputManager : public SpeechInputManager {
if (caller_id_) { // Do a check in case we were cancelled..
VLOG(1) << "Setting fake recognition result.";
delegate_->DidCompleteRecording(caller_id_);
- SpeechInputResultArray results;
- results.push_back(SpeechInputResultItem(ASCIIToUTF16(kTestResult), 1.0));
+ SpeechInputResult results;
+ results.hypotheses.push_back(SpeechInputHypothesis(
+ ASCIIToUTF16(kTestResult), 1.0));
delegate_->SetRecognitionResult(caller_id_, results);
delegate_->DidCompleteRecognition(caller_id_);
caller_id_ = 0;
diff --git a/content/browser/speech/speech_input_dispatcher_host.cc b/content/browser/speech/speech_input_dispatcher_host.cc
index ad1089d..8a67476 100644
--- a/content/browser/speech/speech_input_dispatcher_host.cc
+++ b/content/browser/speech/speech_input_dispatcher_host.cc
@@ -190,7 +190,7 @@ void SpeechInputDispatcherHost::OnStopRecording(int render_view_id,
}
void SpeechInputDispatcherHost::SetRecognitionResult(
- int caller_id, const SpeechInputResultArray& result) {
+ int caller_id, const SpeechInputResult& result) {
VLOG(1) << "SpeechInputDispatcherHost::SetRecognitionResult enter";
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
int caller_render_view_id =
diff --git a/content/browser/speech/speech_input_dispatcher_host.h b/content/browser/speech/speech_input_dispatcher_host.h
index 7fea90d..1286634 100644
--- a/content/browser/speech/speech_input_dispatcher_host.h
+++ b/content/browser/speech/speech_input_dispatcher_host.h
@@ -30,7 +30,7 @@ class SpeechInputDispatcherHost : public BrowserMessageFilter,
// SpeechInputManager::Delegate methods.
virtual void SetRecognitionResult(int caller_id,
- const SpeechInputResultArray& result);
+ const SpeechInputResult& result);
virtual void DidCompleteRecording(int caller_id);
virtual void DidCompleteRecognition(int caller_id);
diff --git a/content/browser/speech/speech_input_manager.cc b/content/browser/speech/speech_input_manager.cc
index 5a37ec7..f3f6402 100644
--- a/content/browser/speech/speech_input_manager.cc
+++ b/content/browser/speech/speech_input_manager.cc
@@ -119,7 +119,7 @@ void SpeechInputManager::StopRecording(int caller_id) {
}
void SpeechInputManager::SetRecognitionResult(
- int caller_id, bool error, const SpeechInputResultArray& result) {
+ int caller_id, const SpeechInputResult& result) {
DCHECK(HasPendingRequest(caller_id));
GetDelegate(caller_id)->SetRecognitionResult(caller_id, result);
}
@@ -138,8 +138,14 @@ void SpeechInputManager::DidCompleteRecognition(int caller_id) {
DoClose(caller_id);
}
+void SpeechInputManager::DidStartReceivingSpeech(int caller_id) {
+}
+
+void SpeechInputManager::DidStopReceivingSpeech(int caller_id) {
+}
+
void SpeechInputManager::OnRecognizerError(
- int caller_id, SpeechRecognizer::ErrorCode error) {
+ int caller_id, SpeechInputError error) {
if (caller_id == recording_caller_id_)
recording_caller_id_ = 0;
requests_[caller_id].is_active = false;
diff --git a/content/browser/speech/speech_input_manager.h b/content/browser/speech/speech_input_manager.h
index c690780..de33e32 100644
--- a/content/browser/speech/speech_input_manager.h
+++ b/content/browser/speech/speech_input_manager.h
@@ -29,7 +29,7 @@ class CONTENT_EXPORT SpeechInputManager : public SpeechRecognizerDelegate {
public:
virtual void SetRecognitionResult(
int caller_id,
- const SpeechInputResultArray& result) = 0;
+ const SpeechInputResult& result) = 0;
virtual void DidCompleteRecording(int caller_id) = 0;
virtual void DidCompleteRecognition(int caller_id) = 0;
@@ -67,17 +67,20 @@ class CONTENT_EXPORT SpeechInputManager : public SpeechRecognizerDelegate {
virtual void CancelAllRequestsWithDelegate(Delegate* delegate);
virtual void StopRecording(int caller_id);
- // SpeechRecognizer::Delegate methods.
- virtual void DidStartReceivingAudio(int caller_id);
+ // SpeechRecognizerDelegate methods.
+ virtual void DidStartReceivingAudio(int caller_id) OVERRIDE;
virtual void SetRecognitionResult(int caller_id,
- bool error,
- const SpeechInputResultArray& result);
- virtual void DidCompleteRecording(int caller_id);
- virtual void DidCompleteRecognition(int caller_id);
+ const SpeechInputResult& result) OVERRIDE;
+ virtual void DidCompleteRecording(int caller_id) OVERRIDE;
+ virtual void DidCompleteRecognition(int caller_id) OVERRIDE;
+ virtual void DidStartReceivingSpeech(int caller_id) OVERRIDE;
+ virtual void DidStopReceivingSpeech(int caller_id) OVERRIDE;
+
virtual void OnRecognizerError(int caller_id,
- SpeechRecognizer::ErrorCode error);
- virtual void DidCompleteEnvironmentEstimation(int caller_id);
- virtual void SetInputVolume(int caller_id, float volume, float noise_volume);
+ SpeechInputError error) OVERRIDE;
+ virtual void DidCompleteEnvironmentEstimation(int caller_id) OVERRIDE;
+ virtual void SetInputVolume(int caller_id, float volume,
+ float noise_volume) OVERRIDE;
protected:
// The pure virtual methods are used for displaying the current state of
@@ -113,7 +116,7 @@ class CONTENT_EXPORT SpeechInputManager : public SpeechRecognizerDelegate {
// Called when there has been a error with the recognition.
virtual void ShowRecognizerError(int caller_id,
- SpeechRecognizer::ErrorCode error) = 0;
+ SpeechInputError error) = 0;
// Called when recognition has ended or has been canceled.
virtual void DoClose(int caller_id) = 0;
diff --git a/content/browser/speech/speech_recognition_request.cc b/content/browser/speech/speech_recognition_request.cc
index 31b7f1f..3a2ad7c 100644
--- a/content/browser/speech/speech_recognition_request.cc
+++ b/content/browser/speech/speech_recognition_request.cc
@@ -20,6 +20,7 @@ namespace {
const char* const kDefaultSpeechRecognitionUrl =
"https://www.google.com/speech-api/v1/recognize?xjerr=1&client=chromium&";
+const char* const kStatusString = "status";
const char* const kHypothesesString = "hypotheses";
const char* const kUtteranceString = "utterance";
const char* const kConfidenceString = "confidence";
@@ -29,7 +30,7 @@ const char* const kConfidenceString = "confidence";
const int kMaxResults = 6;
bool ParseServerResponse(const std::string& response_body,
- speech_input::SpeechInputResultArray* result) {
+ speech_input::SpeechInputResult* result) {
if (response_body.empty()) {
LOG(WARNING) << "ParseServerResponse: Response was empty.";
return false;
@@ -53,18 +54,43 @@ bool ParseServerResponse(const std::string& response_body,
const DictionaryValue* response_object =
static_cast<DictionaryValue*>(response_value.get());
- // Get the hypotheses
+ // Get the status.
+ int status;
+ if (!response_object->GetInteger(kStatusString, &status)) {
+ VLOG(1) << "ParseServerResponse: " << kStatusString
+ << " is not a valid integer value.";
+ return false;
+ }
+
+ // Process the status.
+ switch (status) {
+ case speech_input::kErrorNone:
+ case speech_input::kErrorNoSpeech:
+ case speech_input::kErrorNoMatch:
+ break;
+
+ default:
+ // Other status codes should not be returned by the server.
+ VLOG(1) << "ParseServerResponse: unexpected status code " << status;
+ return false;
+ }
+
+ result->error = static_cast<speech_input::SpeechInputError>(status);
+
+ // Get the hypotheses.
Value* hypotheses_value = NULL;
if (!response_object->Get(kHypothesesString, &hypotheses_value)) {
VLOG(1) << "ParseServerResponse: Missing hypotheses attribute.";
return false;
}
+
DCHECK(hypotheses_value);
if (!hypotheses_value->IsType(Value::TYPE_LIST)) {
VLOG(1) << "ParseServerResponse: Unexpected hypotheses type "
<< hypotheses_value->GetType();
return false;
}
+
const ListValue* hypotheses_list = static_cast<ListValue*>(hypotheses_value);
size_t index = 0;
@@ -93,12 +119,12 @@ bool ParseServerResponse(const std::string& response_body,
double confidence = 0.0;
hypothesis_value->GetDouble(kConfidenceString, &confidence);
- result->push_back(speech_input::SpeechInputResultItem(utterance,
- confidence));
+ result->hypotheses.push_back(speech_input::SpeechInputHypothesis(
+ utterance, confidence));
}
if (index < hypotheses_list->GetSize()) {
- result->clear();
+ result->hypotheses.clear();
return false;
}
@@ -182,16 +208,15 @@ void SpeechRecognitionRequest::UploadAudioChunk(const std::string& audio_data,
void SpeechRecognitionRequest::OnURLFetchComplete(const URLFetcher* source) {
DCHECK_EQ(url_fetcher_.get(), source);
- bool error =
- !source->status().is_success() || source->response_code() != 200;
-
- SpeechInputResultArray result;
- if (!error)
- error = !ParseServerResponse(source->GetResponseStringRef(), &result);
- url_fetcher_.reset();
+ SpeechInputResult result;
+ if (!source->status().is_success() || source->response_code() != 200 ||
+ !ParseServerResponse(source->GetResponseStringRef(), &result)) {
+ result.error = kErrorNetwork;
+ }
DVLOG(1) << "SpeechRecognitionRequest: Invoking delegate with result.";
- delegate_->SetRecognitionResult(error, result);
+ url_fetcher_.reset();
+ delegate_->SetRecognitionResult(result);
}
} // namespace speech_input
diff --git a/content/browser/speech/speech_recognition_request.h b/content/browser/speech/speech_recognition_request.h
index c4be1d2..346951a 100644
--- a/content/browser/speech/speech_recognition_request.h
+++ b/content/browser/speech/speech_recognition_request.h
@@ -34,8 +34,7 @@ class SpeechRecognitionRequest : public URLFetcher::Delegate {
// Interface for receiving callbacks from this object.
class CONTENT_EXPORT Delegate {
public:
- virtual void SetRecognitionResult(
- bool error, const SpeechInputResultArray& result) = 0;
+ virtual void SetRecognitionResult(const SpeechInputResult& result) = 0;
protected:
virtual ~Delegate() {}
diff --git a/content/browser/speech/speech_recognition_request_unittest.cc b/content/browser/speech/speech_recognition_request_unittest.cc
index a5d75c4..53c80dc 100644
--- a/content/browser/speech/speech_recognition_request_unittest.cc
+++ b/content/browser/speech/speech_recognition_request_unittest.cc
@@ -14,24 +14,21 @@ namespace speech_input {
class SpeechRecognitionRequestTest : public SpeechRecognitionRequestDelegate,
public testing::Test {
public:
- SpeechRecognitionRequestTest() : error_(false) { }
+ SpeechRecognitionRequestTest() { }
// Creates a speech recognition request and invokes it's URL fetcher delegate
// with the given test data.
void CreateAndTestRequest(bool success, const std::string& http_response);
// SpeechRecognitionRequestDelegate methods.
- virtual void SetRecognitionResult(bool error,
- const SpeechInputResultArray& result) {
- error_ = error;
+ virtual void SetRecognitionResult(const SpeechInputResult& result) OVERRIDE {
result_ = result;
}
protected:
MessageLoop message_loop_;
TestURLFetcherFactory url_fetcher_factory_;
- bool error_;
- SpeechInputResultArray result_;
+ SpeechInputResult result_;
};
void SpeechRecognitionRequestTest::CreateAndTestRequest(
@@ -58,37 +55,50 @@ void SpeechRecognitionRequestTest::CreateAndTestRequest(
TEST_F(SpeechRecognitionRequestTest, BasicTest) {
// Normal success case with one result.
CreateAndTestRequest(true,
- "{\"hypotheses\":[{\"utterance\":\"123456\",\"confidence\":0.9}]}");
- EXPECT_FALSE(error_);
- EXPECT_EQ(1U, result_.size());
- EXPECT_EQ(ASCIIToUTF16("123456"), result_[0].utterance);
- EXPECT_EQ(0.9, result_[0].confidence);
+ "{\"status\":0,\"hypotheses\":"
+ "[{\"utterance\":\"123456\",\"confidence\":0.9}]}");
+ EXPECT_EQ(result_.error, kErrorNone);
+ EXPECT_EQ(1U, result_.hypotheses.size());
+ EXPECT_EQ(ASCIIToUTF16("123456"), result_.hypotheses[0].utterance);
+ EXPECT_EQ(0.9, result_.hypotheses[0].confidence);
// Normal success case with multiple results.
CreateAndTestRequest(true,
- "{\"hypotheses\":[{\"utterance\":\"hello\",\"confidence\":0.9},"
+ "{\"status\":0,\"hypotheses\":["
+ "{\"utterance\":\"hello\",\"confidence\":0.9},"
"{\"utterance\":\"123456\",\"confidence\":0.5}]}");
- EXPECT_FALSE(error_);
- EXPECT_EQ(2u, result_.size());
- EXPECT_EQ(ASCIIToUTF16("hello"), result_[0].utterance);
- EXPECT_EQ(0.9, result_[0].confidence);
- EXPECT_EQ(ASCIIToUTF16("123456"), result_[1].utterance);
- EXPECT_EQ(0.5, result_[1].confidence);
+ EXPECT_EQ(result_.error, kErrorNone);
+ EXPECT_EQ(2u, result_.hypotheses.size());
+ EXPECT_EQ(ASCIIToUTF16("hello"), result_.hypotheses[0].utterance);
+ EXPECT_EQ(0.9, result_.hypotheses[0].confidence);
+ EXPECT_EQ(ASCIIToUTF16("123456"), result_.hypotheses[1].utterance);
+ EXPECT_EQ(0.5, result_.hypotheses[1].confidence);
// Zero results.
- CreateAndTestRequest(true, "{\"hypotheses\":[]}");
- EXPECT_FALSE(error_);
- EXPECT_EQ(0U, result_.size());
+ CreateAndTestRequest(true, "{\"status\":0,\"hypotheses\":[]}");
+ EXPECT_EQ(result_.error, kErrorNone);
+ EXPECT_EQ(0U, result_.hypotheses.size());
// Http failure case.
CreateAndTestRequest(false, "");
- EXPECT_TRUE(error_);
- EXPECT_EQ(0U, result_.size());
+ EXPECT_EQ(result_.error, kErrorNetwork);
+ EXPECT_EQ(0U, result_.hypotheses.size());
+
+ // Invalid status case.
+ CreateAndTestRequest(true, "{\"status\":\"invalid\",\"hypotheses\":[]}");
+ EXPECT_EQ(result_.error, kErrorNetwork);
+ EXPECT_EQ(0U, result_.hypotheses.size());
+
+ // Server-side error case.
+ CreateAndTestRequest(true, "{\"status\":1,\"hypotheses\":[]}");
+ EXPECT_EQ(result_.error, kErrorNetwork);
+ EXPECT_EQ(0U, result_.hypotheses.size());
// Malformed JSON case.
- CreateAndTestRequest(true, "{\"hypotheses\":[{\"unknownkey\":\"hello\"}]}");
- EXPECT_TRUE(error_);
- EXPECT_EQ(0U, result_.size());
+ CreateAndTestRequest(true, "{\"status\":0,\"hypotheses\":"
+ "[{\"unknownkey\":\"hello\"}]}");
+ EXPECT_EQ(result_.error, kErrorNetwork);
+ EXPECT_EQ(0U, result_.hypotheses.size());
}
} // namespace speech_input
diff --git a/content/browser/speech/speech_recognizer.cc b/content/browser/speech/speech_recognizer.cc
index 295f156..f81128c 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.
diff --git a/content/browser/speech/speech_recognizer.h b/content/browser/speech/speech_recognizer.h
index bd1c8e4..1e3061a 100644
--- a/content/browser/speech/speech_recognizer.h
+++ b/content/browser/speech/speech_recognizer.h
@@ -30,21 +30,12 @@ class CONTENT_EXPORT SpeechRecognizer
public media::AudioInputController::EventHandler,
public SpeechRecognitionRequestDelegate {
public:
- enum ErrorCode {
- RECOGNIZER_NO_ERROR,
- RECOGNIZER_ERROR_CAPTURE,
- RECOGNIZER_ERROR_NO_SPEECH,
- RECOGNIZER_ERROR_NO_RESULTS,
- RECOGNIZER_ERROR_NETWORK,
- };
-
// Implemented by the caller to receive recognition events.
class CONTENT_EXPORT Delegate {
public:
virtual void SetRecognitionResult(
int caller_id,
- bool error,
- const SpeechInputResultArray& result) = 0;
+ const SpeechInputResult& result) = 0;
// Invoked when the first audio packet was received from the audio capture
// device.
@@ -60,12 +51,18 @@ class CONTENT_EXPORT SpeechRecognizer
// sequence and the |SpeechRecognizer| object can be freed up if necessary.
virtual void DidCompleteRecognition(int caller_id) = 0;
+ // Informs that the end pointer has started detecting speech.
+ virtual void DidStartReceivingSpeech(int caller_id) = 0;
+
+ // Informs that the end pointer has stopped detecting speech.
+ virtual void DidStopReceivingSpeech(int caller_id) = 0;
+
// Invoked if there was an error while recording or recognizing audio. The
// session has already been cancelled when this call is made and the DidXxxx
// callbacks will not be issued. It is safe to destroy/release the
// |SpeechRecognizer| object while processing this call.
virtual void OnRecognizerError(int caller_id,
- SpeechRecognizer::ErrorCode error) = 0;
+ SpeechInputError error) = 0;
// At the start of recognition, a short amount of audio is recorded to
// estimate the environment/background noise and this callback is issued
@@ -91,6 +88,7 @@ class CONTENT_EXPORT SpeechRecognizer
bool censor_results,
const std::string& hardware_info,
const std::string& origin_url);
+
virtual ~SpeechRecognizer();
// Starts audio recording and does recognition after recording ends. The same
@@ -115,8 +113,7 @@ class CONTENT_EXPORT SpeechRecognizer
uint32 size);
// SpeechRecognitionRequest::Delegate methods.
- virtual void SetRecognitionResult(bool error,
- const SpeechInputResultArray& result);
+ virtual void SetRecognitionResult(const SpeechInputResult& result);
static const int kAudioSampleRate;
static const int kAudioPacketIntervalMs; // Duration of each audio packet.
@@ -126,7 +123,7 @@ class CONTENT_EXPORT SpeechRecognizer
static const int kEndpointerEstimationTimeMs;
private:
- void InformErrorAndCancelRecognition(ErrorCode error);
+ void InformErrorAndCancelRecognition(SpeechInputError error);
void SendRecordedAudioToServer();
void HandleOnError(int error_code); // Handles OnError in the IO thread.
diff --git a/content/browser/speech/speech_recognizer_unittest.cc b/content/browser/speech/speech_recognizer_unittest.cc
index 17e3e56..0be64eb 100644
--- a/content/browser/speech/speech_recognizer_unittest.cc
+++ b/content/browser/speech/speech_recognizer_unittest.cc
@@ -31,7 +31,7 @@ class SpeechRecognizerTest : public SpeechRecognizerDelegate,
recognition_complete_(false),
result_received_(false),
audio_received_(false),
- error_(SpeechRecognizer::RECOGNIZER_NO_ERROR),
+ error_(kErrorNone),
volume_(-1.0f) {
int audio_packet_length_bytes =
(SpeechRecognizer::kAudioSampleRate *
@@ -43,43 +43,49 @@ class SpeechRecognizerTest : public SpeechRecognizerDelegate,
// SpeechRecognizer::Delegate methods.
virtual void SetRecognitionResult(int caller_id,
- bool error,
- const SpeechInputResultArray& result) {
+ const SpeechInputResult& result) OVERRIDE {
result_received_ = true;
}
- virtual void DidCompleteRecording(int caller_id) {
+ virtual void DidCompleteRecording(int caller_id) OVERRIDE {
recording_complete_ = true;
}
- virtual void DidCompleteRecognition(int caller_id) {
+ virtual void DidCompleteRecognition(int caller_id) OVERRIDE {
recognition_complete_ = true;
}
- virtual void DidCompleteEnvironmentEstimation(int caller_id) {
+ virtual void DidCompleteEnvironmentEstimation(int caller_id) OVERRIDE {
}
- virtual void DidStartReceivingAudio(int caller_id) {
+ virtual void DidStartReceivingAudio(int caller_id) OVERRIDE {
audio_received_ = true;
}
+ virtual void DidStartReceivingSpeech(int caller_id) OVERRIDE {
+ }
+
+ virtual void DidStopReceivingSpeech(int caller_id) OVERRIDE {
+ }
+
virtual void OnRecognizerError(int caller_id,
- SpeechRecognizer::ErrorCode error) {
+ SpeechInputError error) OVERRIDE {
error_ = error;
}
- virtual void SetInputVolume(int caller_id, float volume, float noise_volume) {
+ virtual void SetInputVolume(int caller_id, float volume,
+ float noise_volume) OVERRIDE {
volume_ = volume;
noise_volume_ = noise_volume;
}
// testing::Test methods.
- virtual void SetUp() {
+ virtual void SetUp() OVERRIDE {
AudioInputController::set_factory_for_testing(
&audio_input_controller_factory_);
}
- virtual void TearDown() {
+ virtual void TearDown() OVERRIDE {
AudioInputController::set_factory_for_testing(NULL);
}
@@ -106,7 +112,7 @@ class SpeechRecognizerTest : public SpeechRecognizerDelegate,
bool recognition_complete_;
bool result_received_;
bool audio_received_;
- SpeechRecognizer::ErrorCode error_;
+ SpeechInputError error_;
TestURLFetcherFactory url_fetcher_factory_;
TestAudioInputControllerFactory audio_input_controller_factory_;
std::vector<uint8> audio_packet_;
@@ -122,7 +128,7 @@ TEST_F(SpeechRecognizerTest, StopNoData) {
EXPECT_FALSE(recognition_complete_);
EXPECT_FALSE(result_received_);
EXPECT_FALSE(audio_received_);
- EXPECT_EQ(SpeechRecognizer::RECOGNIZER_NO_ERROR, error_);
+ EXPECT_EQ(kErrorNone, error_);
}
TEST_F(SpeechRecognizerTest, CancelNoData) {
@@ -134,7 +140,7 @@ TEST_F(SpeechRecognizerTest, CancelNoData) {
EXPECT_TRUE(recognition_complete_);
EXPECT_FALSE(result_received_);
EXPECT_FALSE(audio_received_);
- EXPECT_EQ(SpeechRecognizer::RECOGNIZER_NO_ERROR, error_);
+ EXPECT_EQ(kErrorNone, error_);
}
TEST_F(SpeechRecognizerTest, StopWithData) {
@@ -164,7 +170,7 @@ TEST_F(SpeechRecognizerTest, StopWithData) {
EXPECT_TRUE(recording_complete_);
EXPECT_FALSE(recognition_complete_);
EXPECT_FALSE(result_received_);
- EXPECT_EQ(SpeechRecognizer::RECOGNIZER_NO_ERROR, error_);
+ EXPECT_EQ(kErrorNone, error_);
// Issue the network callback to complete the process.
TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0);
@@ -175,12 +181,13 @@ TEST_F(SpeechRecognizerTest, StopWithData) {
status.set_status(net::URLRequestStatus::SUCCESS);
fetcher->set_status(status);
fetcher->set_response_code(200);
- fetcher->SetResponseString("{\"hypotheses\":[{\"utterance\":\"123\"}]}");
+ fetcher->SetResponseString(
+ "{\"status\":0,\"hypotheses\":[{\"utterance\":\"123\"}]}");
fetcher->delegate()->OnURLFetchComplete(fetcher);
EXPECT_TRUE(recognition_complete_);
EXPECT_TRUE(result_received_);
- EXPECT_EQ(SpeechRecognizer::RECOGNIZER_NO_ERROR, error_);
+ EXPECT_EQ(kErrorNone, error_);
}
TEST_F(SpeechRecognizerTest, CancelWithData) {
@@ -199,7 +206,7 @@ TEST_F(SpeechRecognizerTest, CancelWithData) {
EXPECT_FALSE(recording_complete_);
EXPECT_FALSE(recognition_complete_);
EXPECT_FALSE(result_received_);
- EXPECT_EQ(SpeechRecognizer::RECOGNIZER_NO_ERROR, error_);
+ EXPECT_EQ(kErrorNone, error_);
}
TEST_F(SpeechRecognizerTest, ConnectionError) {
@@ -220,7 +227,7 @@ TEST_F(SpeechRecognizerTest, ConnectionError) {
EXPECT_TRUE(recording_complete_);
EXPECT_FALSE(recognition_complete_);
EXPECT_FALSE(result_received_);
- EXPECT_EQ(SpeechRecognizer::RECOGNIZER_NO_ERROR, error_);
+ EXPECT_EQ(kErrorNone, error_);
// Issue the network callback to complete the process.
fetcher->set_url(fetcher->original_url());
@@ -234,7 +241,7 @@ TEST_F(SpeechRecognizerTest, ConnectionError) {
EXPECT_FALSE(recognition_complete_);
EXPECT_FALSE(result_received_);
- EXPECT_EQ(SpeechRecognizer::RECOGNIZER_ERROR_NETWORK, error_);
+ EXPECT_EQ(kErrorNetwork, error_);
}
TEST_F(SpeechRecognizerTest, ServerError) {
@@ -255,7 +262,7 @@ TEST_F(SpeechRecognizerTest, ServerError) {
EXPECT_TRUE(recording_complete_);
EXPECT_FALSE(recognition_complete_);
EXPECT_FALSE(result_received_);
- EXPECT_EQ(SpeechRecognizer::RECOGNIZER_NO_ERROR, error_);
+ EXPECT_EQ(kErrorNone, error_);
// Issue the network callback to complete the process.
fetcher->set_url(fetcher->original_url());
@@ -268,7 +275,7 @@ TEST_F(SpeechRecognizerTest, ServerError) {
EXPECT_FALSE(recognition_complete_);
EXPECT_FALSE(result_received_);
- EXPECT_EQ(SpeechRecognizer::RECOGNIZER_ERROR_NETWORK, error_);
+ EXPECT_EQ(kErrorNetwork, error_);
}
TEST_F(SpeechRecognizerTest, AudioControllerErrorNoData) {
@@ -283,7 +290,7 @@ TEST_F(SpeechRecognizerTest, AudioControllerErrorNoData) {
EXPECT_FALSE(recording_complete_);
EXPECT_FALSE(recognition_complete_);
EXPECT_FALSE(result_received_);
- EXPECT_EQ(SpeechRecognizer::RECOGNIZER_ERROR_CAPTURE, error_);
+ EXPECT_EQ(kErrorAudio, error_);
}
TEST_F(SpeechRecognizerTest, AudioControllerErrorWithData) {
@@ -302,7 +309,7 @@ TEST_F(SpeechRecognizerTest, AudioControllerErrorWithData) {
EXPECT_FALSE(recording_complete_);
EXPECT_FALSE(recognition_complete_);
EXPECT_FALSE(result_received_);
- EXPECT_EQ(SpeechRecognizer::RECOGNIZER_ERROR_CAPTURE, error_);
+ EXPECT_EQ(kErrorAudio, error_);
}
TEST_F(SpeechRecognizerTest, NoSpeechCallbackIssued) {
@@ -327,7 +334,7 @@ TEST_F(SpeechRecognizerTest, NoSpeechCallbackIssued) {
EXPECT_FALSE(recording_complete_);
EXPECT_FALSE(recognition_complete_);
EXPECT_FALSE(result_received_);
- EXPECT_EQ(SpeechRecognizer::RECOGNIZER_ERROR_NO_SPEECH, error_);
+ EXPECT_EQ(kErrorNoSpeech, error_);
}
TEST_F(SpeechRecognizerTest, NoSpeechCallbackNotIssued) {
@@ -358,7 +365,7 @@ TEST_F(SpeechRecognizerTest, NoSpeechCallbackNotIssued) {
}
MessageLoop::current()->RunAllPending();
- EXPECT_EQ(SpeechRecognizer::RECOGNIZER_NO_ERROR, error_);
+ EXPECT_EQ(kErrorNone, error_);
EXPECT_TRUE(audio_received_);
EXPECT_FALSE(recording_complete_);
EXPECT_FALSE(recognition_complete_);
@@ -401,7 +408,7 @@ TEST_F(SpeechRecognizerTest, SetInputVolumeCallback) {
EXPECT_FLOAT_EQ(0.89926866f, volume_);
EXPECT_FLOAT_EQ(0.75071919f, noise_volume_);
- EXPECT_EQ(SpeechRecognizer::RECOGNIZER_NO_ERROR, error_);
+ EXPECT_EQ(kErrorNone, error_);
EXPECT_FALSE(recording_complete_);
EXPECT_FALSE(recognition_complete_);
recognizer_->CancelRecognition();