| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 2010 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 #ifndef CHROME_BROWSER_SPEECH_SPEECH_RECOGNIZER_H_ |
| 6 #define CHROME_BROWSER_SPEECH_SPEECH_RECOGNIZER_H_ |
| 7 |
| 8 #include "base/ref_counted.h" |
| 9 #include "base/scoped_ptr.h" |
| 10 #include "media/audio/audio_input_controller.h" |
| 11 #include "chrome/browser/speech/speech_recognition_request.h" |
| 12 #include <list> |
| 13 #include <string> |
| 14 |
| 15 namespace speech_input { |
| 16 |
| 17 // Records audio, sends recorded audio to server and translates server response |
| 18 // to recognition result. |
| 19 class SpeechRecognizer |
| 20 : public base::RefCountedThreadSafe<SpeechRecognizer>, |
| 21 public media::AudioInputController::EventHandler, |
| 22 public SpeechRecognitionRequestDelegate { |
| 23 public: |
| 24 // Implemented by the caller to receive recognition events. |
| 25 class Delegate { |
| 26 public: |
| 27 virtual void SetRecognitionResult(int render_view_id, bool error, |
| 28 const string16& value) = 0; |
| 29 |
| 30 // Invoked when audio recording stops, either due to the end pointer |
| 31 // detecting silence in user input or if |StopRecording| was called. The |
| 32 // delegate has to wait until |DidCompleteRecognition| is invoked before |
| 33 // destroying the |SpeechRecognizer| object. |
| 34 virtual void DidCompleteRecording(int render_view_id) = 0; |
| 35 |
| 36 // This is guaranteed to be the last method invoked in the recognition |
| 37 // sequence and the |SpeechRecognizer| object can be freed up if necessary. |
| 38 virtual void DidCompleteRecognition(int render_view_id) = 0; |
| 39 |
| 40 protected: |
| 41 virtual ~Delegate() {} |
| 42 }; |
| 43 |
| 44 SpeechRecognizer(Delegate* delegate, int render_view_id); |
| 45 ~SpeechRecognizer(); |
| 46 |
| 47 // Starts audio recording and does recognition after recording ends. The same |
| 48 // SpeechRecognizer instance can be used multiple times for speech recognition |
| 49 // though each recognition request can be made only after the previous one |
| 50 // completes (i.e. after receiving Delegate::DidCompleteRecognition). |
| 51 bool StartRecording(); |
| 52 |
| 53 // Stops recording audio and starts recognition. |
| 54 void StopRecording(); |
| 55 |
| 56 // Stops recording audio and cancels recognition. Any audio recorded so far |
| 57 // gets discarded. |
| 58 void CancelRecognition(); |
| 59 |
| 60 // AudioInputController::EventHandler methods. |
| 61 void OnCreated(media::AudioInputController* controller) { } |
| 62 void OnRecording(media::AudioInputController* controller) { } |
| 63 void OnError(media::AudioInputController* controller, int error_code); |
| 64 void OnData(media::AudioInputController* controller, const uint8* data, |
| 65 uint32 size); |
| 66 |
| 67 // SpeechRecognitionRequest::Delegate methods. |
| 68 void SetRecognitionResult(bool error, const string16& value); |
| 69 |
| 70 private: |
| 71 void ReleaseAudioBuffers(); |
| 72 |
| 73 void HandleOnError(int error_code); // Handles OnError in the IO thread. |
| 74 |
| 75 // Handles OnData in the IO thread. Takes ownership of |data|. |
| 76 void HandleOnData(std::string* data); |
| 77 |
| 78 Delegate* delegate_; |
| 79 int render_view_id_; |
| 80 |
| 81 // Buffer holding the recorded audio. Owns the strings inside the list. |
| 82 typedef std::list<std::string*> AudioBufferQueue; |
| 83 AudioBufferQueue audio_buffers_; |
| 84 |
| 85 scoped_ptr<SpeechRecognitionRequest> request_; |
| 86 scoped_refptr<media::AudioInputController> audio_controller_; |
| 87 |
| 88 DISALLOW_COPY_AND_ASSIGN(SpeechRecognizer); |
| 89 }; |
| 90 |
| 91 // This typedef is to workaround the issue with certain versions of |
| 92 // Visual Studio where it gets confused between multiple Delegate |
| 93 // classes and gives a C2500 error. (I saw this error on the try bots - |
| 94 // the workaround was not needed for my machine). |
| 95 typedef SpeechRecognizer::Delegate SpeechRecognizerDelegate; |
| 96 |
| 97 } // namespace speech_input |
| 98 |
| 99 #endif // CHROME_BROWSER_SPEECH_SPEECH_RECOGNIZER_H_ |
| OLD | NEW |