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

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: Fix non-android builds by excluding speech_recognizer_impl_android 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
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/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_ =
Peter Beverloo 2013/06/13 11:46:23 config is a local variable, so it shouldn't have t
janx 2013/06/13 16:08:53 Done.
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 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, base::Bind(
Primiano Tucci (use gerrit) 2013/06/13 10:05:05 No need for this PostTask here, since you already
janx 2013/06/13 16:08:53 Indeed, it seems I went berserk replacing all list
105 &SpeechRecognitionEventListener::OnAudioStart,
106 base::Unretained(listener()), session_id()));
107 }
108
109 void SpeechRecognizerImplAndroid::OnSoundStart(JNIEnv* env, jobject obj) {
110 if (BrowserThread::CurrentlyOn(BrowserThread::UI)) {
111 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, base::Bind(
112 &SpeechRecognizerImplAndroid::OnSoundStart, this,
113 static_cast<JNIEnv*>(NULL), static_cast<jobject>(NULL)));
114 return;
115 }
116 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
Primiano Tucci (use gerrit) 2013/06/13 10:05:05 Ditto, no need to post.
janx 2013/06/13 16:08:53 Done.
117 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, base::Bind(
118 &SpeechRecognitionEventListener::OnSoundStart,
119 base::Unretained(listener()), session_id()));
120 }
121
122 void SpeechRecognizerImplAndroid::OnSoundEnd(JNIEnv* env, jobject obj) {
123 if (BrowserThread::CurrentlyOn(BrowserThread::UI)) {
124 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, base::Bind(
125 &SpeechRecognizerImplAndroid::OnSoundEnd, this,
126 static_cast<JNIEnv*>(NULL), static_cast<jobject>(NULL)));
127 return;
128 }
129 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
130 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, base::Bind(
Primiano Tucci (use gerrit) 2013/06/13 10:05:05 Ditto.
janx 2013/06/13 16:08:53 Done.
131 &SpeechRecognitionEventListener::OnSoundEnd,
132 base::Unretained(listener()), session_id()));
133 }
134
135 void SpeechRecognizerImplAndroid::OnAudioEnd(JNIEnv* env, jobject obj) {
136 if (BrowserThread::CurrentlyOn(BrowserThread::UI)) {
137 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, base::Bind(
138 &SpeechRecognizerImplAndroid::OnAudioEnd, this,
139 static_cast<JNIEnv*>(NULL), static_cast<jobject>(NULL)));
140 return;
141 }
142 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
143 if (state_ == STATE_CAPTURING_AUDIO)
144 state_ = STATE_AWAITING_FINAL_RESULT;
145 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, base::Bind(
Primiano Tucci (use gerrit) 2013/06/13 10:05:05 Ditto.
janx 2013/06/13 16:08:53 Done.
146 &SpeechRecognitionEventListener::OnAudioEnd,
147 base::Unretained(listener()), session_id()));
148 }
149
150 void SpeechRecognizerImplAndroid::OnRecognitionResults(JNIEnv* env, jobject obj,
151 jobjectArray strings, jfloatArray floats, jboolean provisional) {
152 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
153 std::vector<string16> options;
Primiano Tucci (use gerrit) 2013/06/13 10:05:05 s/options/transcripts/
janx 2013/06/13 16:08:53 While I agree that those are transcripts from an a
154 AppendJavaStringArrayToStringVector(env, strings, &options);
155 std::vector<float> scores(options.size(), 0.0);
156 if (floats != NULL)
157 JavaFloatArrayToFloatVector(env, floats, &scores);
158 SpeechRecognitionResults results;
159 results.push_back(SpeechRecognitionResult());
160 SpeechRecognitionResult& result = results.back();
161 CHECK_EQ(options.size(), scores.size());
162 for (size_t i = 0; i < options.size(); ++i) {
163 result.hypotheses.push_back(SpeechRecognitionHypothesis(
164 options[i], static_cast<double>(scores[i])));
165 }
166 result.is_provisional = provisional;
167 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, base::Bind(
168 &SpeechRecognizerImplAndroid::OnRecognitionResultsOnIOThread,
169 this, results));
170 }
171
172 void SpeechRecognizerImplAndroid::OnRecognitionResultsOnIOThread(
173 SpeechRecognitionResults const &results) {
174 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
175 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, base::Bind(
Primiano Tucci (use gerrit) 2013/06/13 10:05:05 Ditto.
janx 2013/06/13 16:08:53 Done.
176 &SpeechRecognitionEventListener::OnRecognitionResults,
177 base::Unretained(listener()), session_id(), results));
178 }
179
180 void SpeechRecognizerImplAndroid::OnRecognitionError(JNIEnv* env,
181 jobject obj, jint error) {
182 if (BrowserThread::CurrentlyOn(BrowserThread::UI)) {
Primiano Tucci (use gerrit) 2013/06/13 10:05:05 As above, no need to this UI->IO switching
183 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, base::Bind(
184 &SpeechRecognizerImplAndroid::OnRecognitionError, this,
185 static_cast<JNIEnv*>(NULL), static_cast<jobject>(NULL), error));
186 return;
187 }
188 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
189 SpeechRecognitionErrorCode code =
190 static_cast<SpeechRecognitionErrorCode>(error);
191 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, base::Bind(
192 &SpeechRecognitionEventListener::OnRecognitionError,
Primiano Tucci (use gerrit) 2013/06/13 10:05:05 Ditto.
janx 2013/06/13 16:08:53 Done.
193 base::Unretained(listener()),
194 session_id(),
195 SpeechRecognitionError(code)));
196 }
197
198 void SpeechRecognizerImplAndroid::OnRecognitionEnd(JNIEnv* env,
199 jobject obj) {
200 if (BrowserThread::CurrentlyOn(BrowserThread::UI)) {
201 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, base::Bind(
202 &SpeechRecognizerImplAndroid::OnRecognitionEnd, this,
203 static_cast<JNIEnv*>(NULL), static_cast<jobject>(NULL)));
204 return;
205 }
206 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
207 state_ = STATE_IDLE;
208 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, base::Bind(
Primiano Tucci (use gerrit) 2013/06/13 10:05:05 Ditto.
janx 2013/06/13 16:08:53 Done.
209 &SpeechRecognitionEventListener::OnRecognitionEnd,
210 base::Unretained(listener()), session_id()));
211 }
212
213 // static
214 bool SpeechRecognizerImplAndroid::RegisterSpeechRecognizer(JNIEnv* env) {
215 return RegisterNativesImpl(env);
216 }
217
218 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698