| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef CONTENT_BROWSER_SPEECH_SPEECH_RECOGNIZER_IMPL_H_ | 5 #ifndef CONTENT_BROWSER_SPEECH_SPEECH_RECOGNIZER_IMPL_H_ |
| 6 #define CONTENT_BROWSER_SPEECH_SPEECH_RECOGNIZER_IMPL_H_ | 6 #define CONTENT_BROWSER_SPEECH_SPEECH_RECOGNIZER_IMPL_H_ |
| 7 | 7 |
| 8 #include <list> | 8 #include <list> |
| 9 #include <utility> | 9 #include <utility> |
| 10 | 10 |
| 11 #include "base/compiler_specific.h" | 11 #include "base/compiler_specific.h" |
| 12 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
| 13 #include "content/browser/speech/audio_encoder.h" | 13 #include "content/browser/speech/audio_encoder.h" |
| 14 #include "content/browser/speech/endpointer/endpointer.h" | 14 #include "content/browser/speech/endpointer/endpointer.h" |
| 15 #include "content/browser/speech/speech_recognition_request.h" | 15 #include "content/browser/speech/speech_recognition_request.h" |
| 16 #include "content/public/browser/speech_recognizer.h" | 16 #include "content/public/browser/speech_recognizer.h" |
| 17 #include "content/public/common/speech_recognition_result.h" | 17 #include "content/public/common/speech_recognition_result.h" |
| 18 #include "media/audio/audio_input_controller.h" | 18 #include "media/audio/audio_input_controller.h" |
| 19 | 19 |
| 20 class AudioManager; | 20 class AudioManager; |
| 21 | 21 |
| 22 namespace content { |
| 23 class SpeechRecognitionEventListener; |
| 24 } |
| 25 |
| 22 namespace speech { | 26 namespace speech { |
| 23 | 27 |
| 24 // Records audio, sends recorded audio to server and translates server response | 28 // Records audio, sends recorded audio to server and translates server response |
| 25 // to recognition result. | 29 // to recognition result. |
| 26 class CONTENT_EXPORT SpeechRecognizerImpl | 30 class CONTENT_EXPORT SpeechRecognizerImpl |
| 27 : NON_EXPORTED_BASE(public content::SpeechRecognizer), | 31 : NON_EXPORTED_BASE(public content::SpeechRecognizer), |
| 28 public media::AudioInputController::EventHandler, | 32 public media::AudioInputController::EventHandler, |
| 29 public SpeechRecognitionRequestDelegate { | 33 public SpeechRecognitionRequestDelegate { |
| 30 public: | 34 public: |
| 31 SpeechRecognizerImpl(content::SpeechRecognizerDelegate* delegate, | 35 static const int kAudioSampleRate; |
| 36 static const int kAudioPacketIntervalMs; // Duration of each audio packet. |
| 37 static const ChannelLayout kChannelLayout; |
| 38 static const int kNumBitsPerAudioSample; |
| 39 static const int kNoSpeechTimeoutSec; |
| 40 static const int kEndpointerEstimationTimeMs; |
| 41 |
| 42 SpeechRecognizerImpl(content::SpeechRecognitionEventListener* listener, |
| 32 int caller_id, | 43 int caller_id, |
| 33 const std::string& language, | 44 const std::string& language, |
| 34 const std::string& grammar, | 45 const std::string& grammar, |
| 35 net::URLRequestContextGetter* context_getter, | 46 net::URLRequestContextGetter* context_getter, |
| 36 bool filter_profanities, | 47 bool filter_profanities, |
| 37 const std::string& hardware_info, | 48 const std::string& hardware_info, |
| 38 const std::string& origin_url); | 49 const std::string& origin_url); |
| 39 | 50 |
| 40 virtual ~SpeechRecognizerImpl(); | 51 virtual ~SpeechRecognizerImpl(); |
| 41 | 52 |
| 42 // SpeechRecognizer implementation: | 53 // content::SpeechRecognizer methods. |
| 43 virtual bool StartRecording() OVERRIDE; | 54 virtual bool StartRecognition() OVERRIDE; |
| 44 virtual void CancelRecognition() OVERRIDE; | 55 virtual void AbortRecognition() OVERRIDE; |
| 45 | 56 virtual void StopAudioCapture() OVERRIDE; |
| 46 // Stops recording audio and starts recognition. | 57 virtual bool IsActive() const OVERRIDE; |
| 47 void StopRecording(); | 58 virtual bool IsCapturingAudio() const OVERRIDE; |
| 48 | 59 |
| 49 // AudioInputController::EventHandler methods. | 60 // AudioInputController::EventHandler methods. |
| 50 virtual void OnCreated(media::AudioInputController* controller) OVERRIDE {} | 61 virtual void OnCreated(media::AudioInputController* controller) OVERRIDE {} |
| 51 virtual void OnRecording(media::AudioInputController* controller) OVERRIDE {} | 62 virtual void OnRecording(media::AudioInputController* controller) OVERRIDE {} |
| 52 virtual void OnError(media::AudioInputController* controller, | 63 virtual void OnError(media::AudioInputController* controller, |
| 53 int error_code) OVERRIDE; | 64 int error_code) OVERRIDE; |
| 54 virtual void OnData(media::AudioInputController* controller, | 65 virtual void OnData(media::AudioInputController* controller, |
| 55 const uint8* data, | 66 const uint8* data, |
| 56 uint32 size) OVERRIDE; | 67 uint32 size) OVERRIDE; |
| 57 | 68 |
| 58 // SpeechRecognitionRequest::Delegate methods. | 69 // SpeechRecognitionRequest::Delegate methods. |
| 59 virtual void SetRecognitionResult( | 70 virtual void SetRecognitionResult( |
| 60 const content::SpeechRecognitionResult& result) OVERRIDE; | 71 const content::SpeechRecognitionResult& result) OVERRIDE; |
| 61 | 72 |
| 62 static const int kAudioSampleRate; | 73 private: |
| 63 static const int kAudioPacketIntervalMs; // Duration of each audio packet. | 74 friend class SpeechRecognizerImplTest; |
| 64 static const ChannelLayout kChannelLayout; | |
| 65 static const int kNumBitsPerAudioSample; | |
| 66 static const int kNoSpeechTimeoutSec; | |
| 67 static const int kEndpointerEstimationTimeMs; | |
| 68 | 75 |
| 69 private: | 76 void InformErrorAndAbortRecognition( |
| 70 friend class SpeechRecognizerTest; | |
| 71 | |
| 72 void InformErrorAndCancelRecognition( | |
| 73 content::SpeechRecognitionErrorCode error); | 77 content::SpeechRecognitionErrorCode error); |
| 74 void SendRecordedAudioToServer(); | 78 void SendRecordedAudioToServer(); |
| 75 | 79 |
| 76 void HandleOnError(int error_code); // Handles OnError in the IO thread. | 80 void HandleOnError(int error_code); // Handles OnError in the IO thread. |
| 77 | 81 |
| 78 // Handles OnData in the IO thread. Takes ownership of |raw_audio|. | 82 // Handles OnData in the IO thread. Takes ownership of |raw_audio|. |
| 79 void HandleOnData(AudioChunk* raw_audio); | 83 void HandleOnData(AudioChunk* raw_audio); |
| 80 | 84 |
| 81 // Helper method which closes the audio controller and blocks until done. | 85 // Helper method which closes the audio controller and blocks until done. |
| 82 void CloseAudioControllerSynchronously(); | 86 void CloseAudioControllerSynchronously(); |
| 83 | 87 |
| 84 void SetAudioManagerForTesting(AudioManager* audio_manager); | 88 void SetAudioManagerForTesting(AudioManager* audio_manager); |
| 85 | 89 |
| 86 content::SpeechRecognizerDelegate* delegate_; | 90 content::SpeechRecognitionEventListener* listener_; |
| 87 int caller_id_; | 91 int caller_id_; |
| 88 std::string language_; | 92 std::string language_; |
| 89 std::string grammar_; | 93 std::string grammar_; |
| 90 bool filter_profanities_; | 94 bool filter_profanities_; |
| 91 std::string hardware_info_; | 95 std::string hardware_info_; |
| 92 std::string origin_url_; | 96 std::string origin_url_; |
| 93 | 97 |
| 94 scoped_ptr<SpeechRecognitionRequest> request_; | 98 scoped_ptr<SpeechRecognitionRequest> request_; |
| 95 scoped_refptr<media::AudioInputController> audio_controller_; | 99 scoped_refptr<media::AudioInputController> audio_controller_; |
| 96 scoped_refptr<net::URLRequestContextGetter> context_getter_; | 100 scoped_refptr<net::URLRequestContextGetter> context_getter_; |
| 97 AudioEncoder::Codec codec_; | 101 AudioEncoder::Codec codec_; |
| 98 scoped_ptr<AudioEncoder> encoder_; | 102 scoped_ptr<AudioEncoder> encoder_; |
| 99 Endpointer endpointer_; | 103 Endpointer endpointer_; |
| 100 int num_samples_recorded_; | 104 int num_samples_recorded_; |
| 101 float audio_level_; | 105 float audio_level_; |
| 102 AudioManager* audio_manager_; | 106 AudioManager* audio_manager_; |
| 103 | 107 |
| 104 DISALLOW_COPY_AND_ASSIGN(SpeechRecognizerImpl); | 108 DISALLOW_COPY_AND_ASSIGN(SpeechRecognizerImpl); |
| 105 }; | 109 }; |
| 106 | 110 |
| 107 } // namespace speech | 111 } // namespace speech |
| 108 | 112 |
| 109 #endif // CONTENT_BROWSER_SPEECH_SPEECH_RECOGNIZER_IMPL_H_ | 113 #endif // CONTENT_BROWSER_SPEECH_SPEECH_RECOGNIZER_IMPL_H_ |
| OLD | NEW |