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