summaryrefslogtreecommitdiffstats
path: root/content/browser/speech/speech_recognizer_impl_android.h
blob: e6e7f09ec69c561ca7aabe34585c793112cefc3a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// Copyright 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.

#ifndef CONTENT_BROWSER_SPEECH_SPEECH_RECOGNIZER_IMPL_ANDROID_H_
#define CONTENT_BROWSER_SPEECH_SPEECH_RECOGNIZER_IMPL_ANDROID_H_

#include <jni.h>

#include "base/android/scoped_java_ref.h"
#include "base/memory/ref_counted.h"
#include "base/memory/scoped_ptr.h"
#include "content/browser/speech/speech_recognizer.h"
#include "content/public/common/speech_recognition_error.h"
#include "content/public/common/speech_recognition_result.h"

namespace content {

class SpeechRecognitionEventListener;

class CONTENT_EXPORT SpeechRecognizerImplAndroid : public SpeechRecognizer {
 public:
  SpeechRecognizerImplAndroid(SpeechRecognitionEventListener* listener,
                              int session_id);

  // SpeechRecognizer methods.
  virtual void StartRecognition(const std::string& device_id) OVERRIDE;
  virtual void AbortRecognition() OVERRIDE;
  virtual void StopAudioCapture() OVERRIDE;
  virtual bool IsActive() const OVERRIDE;
  virtual bool IsCapturingAudio() const OVERRIDE;

  // Called from Java methods via JNI.
  void OnAudioStart(JNIEnv* env, jobject obj);
  void OnSoundStart(JNIEnv* env, jobject obj);
  void OnSoundEnd(JNIEnv* env, jobject obj);
  void OnAudioEnd(JNIEnv* env, jobject obj);
  void OnRecognitionResults(JNIEnv* env, jobject obj, jobjectArray strings,
                            jfloatArray floats, jboolean interim);
  void OnRecognitionError(JNIEnv* env, jobject obj, jint error);
  void OnRecognitionEnd(JNIEnv* env, jobject obj);

  static bool RegisterSpeechRecognizer(JNIEnv* env);

 private:
  enum State {
    STATE_IDLE = 0,
    STATE_CAPTURING_AUDIO,
    STATE_AWAITING_FINAL_RESULT
  };

  void StartRecognitionOnUIThread(
      std::string language, bool continuous, bool interim_results);
  void OnRecognitionResultsOnIOThread(SpeechRecognitionResults const &results);

  virtual ~SpeechRecognizerImplAndroid();

  base::android::ScopedJavaGlobalRef<jobject> j_recognition_;
  State state_;

  DISALLOW_COPY_AND_ASSIGN(SpeechRecognizerImplAndroid);
};

}  // namespace content

#endif  // CONTENT_BROWSER_SPEECH_SPEECH_RECOGNIZER_IMPL_ANDROID_H_