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

Side by Side 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: Rebased, 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 unified diff | Download patch
« no previous file with comments | « content/browser/speech/speech_recognizer_impl_android.h ('k') | content/content.gyp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "content/browser/speech/speech_recognizer_impl_android.h"
6
7 #include "base/android/jni_android.h"
8 #include "base/android/jni_array.h"
9 #include "base/android/jni_string.h"
10 #include "base/android/scoped_java_ref.h"
11 #include "base/bind.h"
12 #include "base/strings/utf_string_conversions.h"
13 #include "content/public/browser/browser_thread.h"
14 #include "content/public/browser/speech_recognition_event_listener.h"
15 #include "content/public/browser/speech_recognition_manager.h"
16 #include "content/public/browser/speech_recognition_session_config.h"
17 #include "content/public/common/speech_recognition_grammar.h"
18 #include "content/public/common/speech_recognition_result.h"
19 #include "jni/SpeechRecognition_jni.h"
20
21 using base::android::AppendJavaStringArrayToStringVector;
22 using base::android::AttachCurrentThread;
23 using base::android::GetApplicationContext;
24 using base::android::JavaFloatArrayToFloatVector;
25
26 namespace content {
27
28 SpeechRecognizerImplAndroid::SpeechRecognizerImplAndroid(
29 SpeechRecognitionEventListener* listener,
30 int session_id)
31 : SpeechRecognizer(listener, session_id),
32 state_(STATE_IDLE) {
33 }
34
35 SpeechRecognizerImplAndroid::~SpeechRecognizerImplAndroid() { }
36
37 void SpeechRecognizerImplAndroid::StartRecognition() {
38 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
39 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, base::Bind(
40 &SpeechRecognitionEventListener::OnRecognitionStart,
41 base::Unretained(listener()),
42 session_id()));
43 SpeechRecognitionSessionConfig config =
44 SpeechRecognitionManager::GetInstance()->GetSessionConfig(session_id());
45 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, base::Bind(
46 &content::SpeechRecognizerImplAndroid::StartRecognitionOnUIThread, this,
47 config.continuous, config.interim_results));
48 }
49
50 void SpeechRecognizerImplAndroid::StartRecognitionOnUIThread(
51 bool continuous, bool interim_results) {
52 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
53 JNIEnv* env = AttachCurrentThread();
54 j_recognition_.Reset(Java_SpeechRecognition_createSpeechRecognition(env,
55 GetApplicationContext(), reinterpret_cast<jint>(this)));
56 Java_SpeechRecognition_startRecognition(env, j_recognition_.obj(), continuous,
57 interim_results);
58 }
59
60 void SpeechRecognizerImplAndroid::AbortRecognition() {
61 if (BrowserThread::CurrentlyOn(BrowserThread::IO)) {
62 state_ = STATE_IDLE;
63 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, base::Bind(
64 &content::SpeechRecognizerImplAndroid::AbortRecognition, this));
65 return;
66 }
67 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
68 JNIEnv* env = AttachCurrentThread();
69 if (!j_recognition_.is_null())
70 Java_SpeechRecognition_abortRecognition(env, j_recognition_.obj());
71 }
72
73 void SpeechRecognizerImplAndroid::StopAudioCapture() {
74 if (BrowserThread::CurrentlyOn(BrowserThread::IO)) {
75 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, base::Bind(
76 &content::SpeechRecognizerImplAndroid::StopAudioCapture, this));
77 return;
78 }
79 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
80 JNIEnv* env = AttachCurrentThread();
81 if (!j_recognition_.is_null())
82 Java_SpeechRecognition_stopRecognition(env, j_recognition_.obj());
83 }
84
85 bool SpeechRecognizerImplAndroid::IsActive() const {
86 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
87 return state_ != STATE_IDLE;
88 }
89
90 bool SpeechRecognizerImplAndroid::IsCapturingAudio() const {
91 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
92 return state_ == STATE_CAPTURING_AUDIO;
93 }
94
95 void SpeechRecognizerImplAndroid::OnAudioStart(JNIEnv* env, jobject obj) {
96 if (BrowserThread::CurrentlyOn(BrowserThread::UI)) {
97 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, base::Bind(
98 &SpeechRecognizerImplAndroid::OnAudioStart, this,
99 static_cast<JNIEnv*>(NULL), static_cast<jobject>(NULL)));
100 return;
101 }
102 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
103 state_ = STATE_CAPTURING_AUDIO;
104 listener()->OnAudioStart(session_id());
105 }
106
107 void SpeechRecognizerImplAndroid::OnSoundStart(JNIEnv* env, jobject obj) {
108 if (BrowserThread::CurrentlyOn(BrowserThread::UI)) {
109 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, base::Bind(
110 &SpeechRecognizerImplAndroid::OnSoundStart, this,
111 static_cast<JNIEnv*>(NULL), static_cast<jobject>(NULL)));
112 return;
113 }
114 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
115 listener()->OnSoundStart(session_id());
116 }
117
118 void SpeechRecognizerImplAndroid::OnSoundEnd(JNIEnv* env, jobject obj) {
119 if (BrowserThread::CurrentlyOn(BrowserThread::UI)) {
120 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, base::Bind(
121 &SpeechRecognizerImplAndroid::OnSoundEnd, this,
122 static_cast<JNIEnv*>(NULL), static_cast<jobject>(NULL)));
123 return;
124 }
125 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
126 listener()->OnSoundEnd(session_id());
127 }
128
129 void SpeechRecognizerImplAndroid::OnAudioEnd(JNIEnv* env, jobject obj) {
130 if (BrowserThread::CurrentlyOn(BrowserThread::UI)) {
131 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, base::Bind(
132 &SpeechRecognizerImplAndroid::OnAudioEnd, this,
133 static_cast<JNIEnv*>(NULL), static_cast<jobject>(NULL)));
134 return;
135 }
136 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
137 if (state_ == STATE_CAPTURING_AUDIO)
138 state_ = STATE_AWAITING_FINAL_RESULT;
139 listener()->OnAudioEnd(session_id());
140 }
141
142 void SpeechRecognizerImplAndroid::OnRecognitionResults(JNIEnv* env, jobject obj,
143 jobjectArray strings, jfloatArray floats, jboolean provisional) {
144 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
145 std::vector<string16> options;
146 AppendJavaStringArrayToStringVector(env, strings, &options);
147 std::vector<float> scores(options.size(), 0.0);
148 if (floats != NULL)
149 JavaFloatArrayToFloatVector(env, floats, &scores);
150 SpeechRecognitionResults results;
151 results.push_back(SpeechRecognitionResult());
152 SpeechRecognitionResult& result = results.back();
153 CHECK_EQ(options.size(), scores.size());
154 for (size_t i = 0; i < options.size(); ++i) {
155 result.hypotheses.push_back(SpeechRecognitionHypothesis(
156 options[i], static_cast<double>(scores[i])));
157 }
158 result.is_provisional = provisional;
159 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, base::Bind(
160 &SpeechRecognizerImplAndroid::OnRecognitionResultsOnIOThread,
161 this, results));
162 }
163
164 void SpeechRecognizerImplAndroid::OnRecognitionResultsOnIOThread(
165 SpeechRecognitionResults const &results) {
166 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
167 listener()->OnRecognitionResults(session_id(), results);
168 }
169
170 void SpeechRecognizerImplAndroid::OnRecognitionError(JNIEnv* env,
171 jobject obj, jint error) {
172 if (BrowserThread::CurrentlyOn(BrowserThread::UI)) {
173 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, base::Bind(
174 &SpeechRecognizerImplAndroid::OnRecognitionError, this,
175 static_cast<JNIEnv*>(NULL), static_cast<jobject>(NULL), error));
176 return;
177 }
178 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
179 SpeechRecognitionErrorCode code =
180 static_cast<SpeechRecognitionErrorCode>(error);
181 listener()->OnRecognitionError(session_id(), SpeechRecognitionError(code));
182 }
183
184 void SpeechRecognizerImplAndroid::OnRecognitionEnd(JNIEnv* env,
185 jobject obj) {
186 if (BrowserThread::CurrentlyOn(BrowserThread::UI)) {
187 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, base::Bind(
188 &SpeechRecognizerImplAndroid::OnRecognitionEnd, this,
189 static_cast<JNIEnv*>(NULL), static_cast<jobject>(NULL)));
190 return;
191 }
192 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
193 state_ = STATE_IDLE;
194 listener()->OnRecognitionEnd(session_id());
195 }
196
197 // static
198 bool SpeechRecognizerImplAndroid::RegisterSpeechRecognizer(JNIEnv* env) {
199 return RegisterNativesImpl(env);
200 }
201
202 } // namespace content
OLDNEW
« no previous file with comments | « content/browser/speech/speech_recognizer_impl_android.h ('k') | content/content.gyp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698