Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(608)

Unified Diff: content/browser/speech/speech_recognizer_impl_android.cc

Issue 15907012: Implement SpeechRecognizerImplAndroid (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Convert error codes in Java, refactor *{,JNI} methods into single methods, nits Created 7 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: content/browser/speech/speech_recognizer_impl_android.cc
diff --git a/content/browser/speech/speech_recognizer_impl_android.cc b/content/browser/speech/speech_recognizer_impl_android.cc
new file mode 100644
index 0000000000000000000000000000000000000000..f4cc8bfe57e44e9f8ed1b6b3af86a9d03496d158
--- /dev/null
+++ b/content/browser/speech/speech_recognizer_impl_android.cc
@@ -0,0 +1,199 @@
+// Copyright (c) 2013 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "content/browser/speech/speech_recognizer_impl_android.h"
+
+#include "base/android/jni_android.h"
+#include "base/android/jni_array.h"
+#include "base/android/jni_string.h"
+#include "base/android/scoped_java_ref.h"
+#include "base/bind.h"
+#include "base/utf_string_conversions.h"
+#include "content/public/browser/browser_thread.h"
+#include "content/public/browser/speech_recognition_event_listener.h"
+#include "content/public/browser/speech_recognition_manager.h"
+#include "content/public/browser/speech_recognition_session_config.h"
+#include "content/public/common/speech_recognition_grammar.h"
+#include "content/public/common/speech_recognition_result.h"
+#include "jni/SpeechRecognition_jni.h"
+
+using base::android::AppendJavaStringArrayToStringVector;
+using base::android::AttachCurrentThread;
+using base::android::GetApplicationContext;
+using base::android::JavaFloatArrayToFloatVector;
+
+namespace content {
+
+SpeechRecognizerImplAndroid::SpeechRecognizerImplAndroid(
+ SpeechRecognitionEventListener* listener,
+ int session_id)
+ : SpeechRecognizer(listener, session_id),
+ state_(STATE_IDLE) {
+}
+
+SpeechRecognizerImplAndroid::~SpeechRecognizerImplAndroid() { }
+
+void SpeechRecognizerImplAndroid::StartRecognition() {
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
+ listener()->OnRecognitionStart(session_id());
janx 2013/06/10 16:51:19 Primiano Tucci 2013/06/03 16:24:11 I'd like to hav
bulach 2013/06/11 07:24:48 if this is asking for my opinion, I'd say go with
janx 2013/06/12 14:47:14 Converted all "listener()->OnSomeEvent(session_id
+ SpeechRecognitionSessionConfig config_ =
+ SpeechRecognitionManager::GetInstance()->GetSessionConfig(session_id());
+ BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, base::Bind(
+ &content::SpeechRecognizerImplAndroid::StartRecognitionOnUIThread, this,
+ config_.continuous, config_.interim_results));
+}
+
+void SpeechRecognizerImplAndroid::StartRecognitionOnUIThread(bool continuous,
+ bool interim_results) {
bulach 2013/06/11 07:24:48 nit: either left-align, or keep the previous ( by
janx 2013/06/12 14:47:14 Went for \n after ( and first param indented by 4.
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
+ JNIEnv* env = AttachCurrentThread();
+ j_recognition_.Reset(Java_SpeechRecognition_createSpeechRecognition(env,
+ GetApplicationContext(), reinterpret_cast<jint>(this)));
+ Java_SpeechRecognition_startRecognition(env, j_recognition_.obj(), continuous,
+ interim_results);
+}
+
+void SpeechRecognizerImplAndroid::AbortRecognition() {
+ if (BrowserThread::CurrentlyOn(BrowserThread::IO)) {
+ BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
+ base::Bind(&content::SpeechRecognizerImplAndroid::AbortRecognition,
+ this));
+ listener()->OnRecognitionError(session_id(),
bulach 2013/06/11 07:24:48 as above, perhaps better to post this task after t
janx 2013/06/12 14:47:14 Now handled in the Java side, call removed.
+ SpeechRecognitionError(SPEECH_RECOGNITION_ERROR_ABORTED));
+ return;
+ }
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
+ JNIEnv* env = AttachCurrentThread();
+ if (!j_recognition_.is_null())
+ Java_SpeechRecognition_abortRecognition(env, j_recognition_.obj());
+}
+
+void SpeechRecognizerImplAndroid::StopAudioCapture() {
+ if (BrowserThread::CurrentlyOn(BrowserThread::IO)) {
+ BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
+ base::Bind(&content::SpeechRecognizerImplAndroid::StopAudioCapture,
+ this));
+ return;
+ }
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
+ JNIEnv* env = AttachCurrentThread();
+ if (!j_recognition_.is_null())
+ Java_SpeechRecognition_stopRecognition(env, j_recognition_.obj());
+}
+
+bool SpeechRecognizerImplAndroid::IsActive() const {
+ return state_ != STATE_IDLE;
bulach 2013/06/11 07:24:48 just confirming: these are always called in the IO
janx 2013/06/12 14:47:14 Added "DCHECK(BrowserThread::CurrentlyOn(BrowserTh
+}
+
+bool SpeechRecognizerImplAndroid::IsCapturingAudio() const {
+ return state_ == STATE_CAPTURING_AUDIO;
+}
+
+void SpeechRecognizerImplAndroid::OnAudioStart(JNIEnv* env, jobject obj) {
+ if (BrowserThread::CurrentlyOn(BrowserThread::UI)) {
+ BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
+ base::Bind(&SpeechRecognizerImplAndroid::OnAudioStart, this, env,
bulach 2013/06/11 07:24:48 oh, an important detail: neither env or obj are th
janx 2013/06/12 14:47:14 Fixed by sending "static_cast<JNIEnv*>(NULL), stat
+ obj));
+ return;
+ }
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
+ state_ = STATE_CAPTURING_AUDIO;
+ listener()->OnAudioStart(session_id());
+}
+
+void SpeechRecognizerImplAndroid::OnSoundStart(JNIEnv* env, jobject obj) {
+ if (BrowserThread::CurrentlyOn(BrowserThread::UI)) {
+ BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
+ base::Bind(&SpeechRecognizerImplAndroid::OnSoundStart, this, env,
+ obj));
+ return;
+ }
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
+ listener()->OnSoundStart(session_id());
+}
+
+void SpeechRecognizerImplAndroid::OnSoundEnd(JNIEnv* env, jobject obj) {
+ if (BrowserThread::CurrentlyOn(BrowserThread::UI)) {
+ BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
+ base::Bind(&SpeechRecognizerImplAndroid::OnSoundEnd, this, env, obj));
+ return;
+ }
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
+ listener()->OnSoundEnd(session_id());
+}
+
+void SpeechRecognizerImplAndroid::OnAudioEnd(JNIEnv* env, jobject obj) {
+ if (BrowserThread::CurrentlyOn(BrowserThread::UI)) {
+ BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
+ base::Bind(&SpeechRecognizerImplAndroid::OnAudioEnd, this, env, obj));
+ return;
+ }
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
+ state_ = STATE_AWAITING_FINAL_RESULT;
+ listener()->OnAudioEnd(session_id());
+}
+
+void SpeechRecognizerImplAndroid::OnRecognitionResults(JNIEnv* env,
+ jobject obj, jobjectArray strings, jfloatArray floats,
+ jboolean provisional) {
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
+ std::vector<string16> options;
+ AppendJavaStringArrayToStringVector(env, strings, &options);
+ std::vector<float> scores(options.size(), 0.0);
+ if (floats != NULL)
+ JavaFloatArrayToFloatVector(env, floats, &scores);
+ BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
+ base::Bind(&SpeechRecognizerImplAndroid::OnRecognitionResultsOnIOThread,
+ this, options, scores, provisional));
+}
+
+void SpeechRecognizerImplAndroid::OnRecognitionResultsOnIOThread(
+ std::vector<string16> options, std::vector<float> scores,
+ bool provisional) {
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
+ SpeechRecognitionResults results;
bulach 2013/06/11 07:24:48 nit: is this copy-constructable? if so, you may wa
janx 2013/06/12 14:47:14 SpeechRecognitionResults is just a std::vector, so
+ results.push_back(SpeechRecognitionResult());
+ SpeechRecognitionResult& result = results.back();
bulach 2013/06/11 07:24:48 CHECK_EQ(options.size(), scores.size());
janx 2013/06/12 14:47:14 Done.
+ for (unsigned int i = 0; i < options.size(); i++) {
bulach 2013/06/11 07:24:48 nit: s/unsigned int/size_t/ and also ++i
janx 2013/06/12 14:47:14 Done.
+ result.hypotheses.push_back(
+ SpeechRecognitionHypothesis(options[i],
+ static_cast<double>(scores[i])));
+ }
+ result.is_provisional = provisional;
+ listener()->OnRecognitionResults(session_id(), results);
+}
+
+void SpeechRecognizerImplAndroid::OnRecognitionError(JNIEnv* env,
+ jobject obj, jint error) {
+ if (BrowserThread::CurrentlyOn(BrowserThread::UI)) {
+ BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
+ base::Bind(&SpeechRecognizerImplAndroid::OnRecognitionError, this, env,
+ obj, error));
+ return;
+ }
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
+ SpeechRecognitionErrorCode code =
+ static_cast<SpeechRecognitionErrorCode>(error);
+ listener()->OnRecognitionError(session_id(), SpeechRecognitionError(code));
+}
+
+void SpeechRecognizerImplAndroid::OnRecognitionEnd(JNIEnv* env,
+ jobject obj) {
+ if (BrowserThread::CurrentlyOn(BrowserThread::UI)) {
+ BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
+ base::Bind(&SpeechRecognizerImplAndroid::OnRecognitionEnd, this, env,
+ obj));
+ return;
+ }
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
+ state_ = STATE_IDLE;
+ listener()->OnRecognitionEnd(session_id());
+}
+
+// static
+bool SpeechRecognizerImplAndroid::RegisterSpeechRecognizer(JNIEnv* env) {
+ return RegisterNativesImpl(env);
+}
+
+} // namespace content

Powered by Google App Engine
This is Rietveld 408576698