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_H_ | 5 #ifndef CONTENT_BROWSER_SPEECH_SPEECH_RECOGNIZER_H_ |
6 #define CONTENT_BROWSER_SPEECH_SPEECH_RECOGNIZER_H_ | 6 #define CONTENT_BROWSER_SPEECH_SPEECH_RECOGNIZER_H_ |
7 | 7 |
8 #include <list> | 8 #include <list> |
9 #include <string> | 9 #include <string> |
10 #include <utility> | 10 #include <utility> |
11 | 11 |
12 #include "base/memory/ref_counted.h" | 12 #include "base/memory/ref_counted.h" |
13 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
14 #include "content/browser/speech/audio_encoder.h" | 14 #include "content/browser/speech/audio_encoder.h" |
15 #include "content/browser/speech/endpointer/endpointer.h" | 15 #include "content/browser/speech/endpointer/endpointer.h" |
16 #include "content/browser/speech/speech_recognition_request.h" | 16 #include "content/browser/speech/speech_recognition_request.h" |
17 #include "content/common/content_export.h" | 17 #include "content/common/content_export.h" |
18 #include "content/public/common/speech_input_result.h" | 18 #include "content/public/common/speech_input_result.h" |
19 #include "media/audio/audio_input_controller.h" | 19 #include "media/audio/audio_input_controller.h" |
20 | 20 |
| 21 class AudioManager; |
| 22 |
21 namespace content { | 23 namespace content { |
22 class SpeechRecognizerDelegate; | 24 class SpeechRecognizerDelegate; |
23 } | 25 } |
24 | 26 |
25 namespace net { | 27 namespace net { |
26 class URLRequestContextGetter; | 28 class URLRequestContextGetter; |
27 } | 29 } |
28 | 30 |
29 namespace speech_input { | 31 namespace speech_input { |
30 | 32 |
31 // Records audio, sends recorded audio to server and translates server response | 33 // Records audio, sends recorded audio to server and translates server response |
32 // to recognition result. | 34 // to recognition result. |
33 class CONTENT_EXPORT SpeechRecognizer | 35 class CONTENT_EXPORT SpeechRecognizer |
34 : public base::RefCountedThreadSafe<SpeechRecognizer>, | 36 : public base::RefCountedThreadSafe<SpeechRecognizer>, |
35 public media::AudioInputController::EventHandler, | 37 public media::AudioInputController::EventHandler, |
36 public SpeechRecognitionRequestDelegate { | 38 public SpeechRecognitionRequestDelegate { |
37 public: | 39 public: |
38 SpeechRecognizer(content::SpeechRecognizerDelegate* delegate, | 40 SpeechRecognizer(content::SpeechRecognizerDelegate* delegate, |
39 int caller_id, | 41 int caller_id, |
40 const std::string& language, | 42 const std::string& language, |
41 const std::string& grammar, | 43 const std::string& grammar, |
42 net::URLRequestContextGetter* context_getter, | 44 net::URLRequestContextGetter* context_getter, |
43 AudioManager* audio_manager, | |
44 bool filter_profanities, | 45 bool filter_profanities, |
45 const std::string& hardware_info, | 46 const std::string& hardware_info, |
46 const std::string& origin_url); | 47 const std::string& origin_url); |
47 | 48 |
48 virtual ~SpeechRecognizer(); | 49 virtual ~SpeechRecognizer(); |
49 | 50 |
50 // Starts audio recording and does recognition after recording ends. The same | 51 // Starts audio recording and does recognition after recording ends. The same |
51 // SpeechRecognizer instance can be used multiple times for speech recognition | 52 // SpeechRecognizer instance can be used multiple times for speech recognition |
52 // though each recognition request can be made only after the previous one | 53 // though each recognition request can be made only after the previous one |
53 // completes (i.e. after receiving | 54 // completes (i.e. after receiving |
(...skipping 13 matching lines...) Expand all Loading... |
67 virtual void OnError(media::AudioInputController* controller, | 68 virtual void OnError(media::AudioInputController* controller, |
68 int error_code) OVERRIDE; | 69 int error_code) OVERRIDE; |
69 virtual void OnData(media::AudioInputController* controller, | 70 virtual void OnData(media::AudioInputController* controller, |
70 const uint8* data, | 71 const uint8* data, |
71 uint32 size) OVERRIDE; | 72 uint32 size) OVERRIDE; |
72 | 73 |
73 // SpeechRecognitionRequest::Delegate methods. | 74 // SpeechRecognitionRequest::Delegate methods. |
74 virtual void SetRecognitionResult( | 75 virtual void SetRecognitionResult( |
75 const content::SpeechInputResult& result) OVERRIDE; | 76 const content::SpeechInputResult& result) OVERRIDE; |
76 | 77 |
77 AudioManager* audio_manager() const { return audio_manager_; } | |
78 | |
79 static const int kAudioSampleRate; | 78 static const int kAudioSampleRate; |
80 static const int kAudioPacketIntervalMs; // Duration of each audio packet. | 79 static const int kAudioPacketIntervalMs; // Duration of each audio packet. |
81 static const ChannelLayout kChannelLayout; | 80 static const ChannelLayout kChannelLayout; |
82 static const int kNumBitsPerAudioSample; | 81 static const int kNumBitsPerAudioSample; |
83 static const int kNoSpeechTimeoutSec; | 82 static const int kNoSpeechTimeoutSec; |
84 static const int kEndpointerEstimationTimeMs; | 83 static const int kEndpointerEstimationTimeMs; |
85 | 84 |
86 private: | 85 private: |
| 86 friend class SpeechRecognizerTest; |
| 87 |
87 void InformErrorAndCancelRecognition(content::SpeechInputError error); | 88 void InformErrorAndCancelRecognition(content::SpeechInputError error); |
88 void SendRecordedAudioToServer(); | 89 void SendRecordedAudioToServer(); |
89 | 90 |
90 void HandleOnError(int error_code); // Handles OnError in the IO thread. | 91 void HandleOnError(int error_code); // Handles OnError in the IO thread. |
91 | 92 |
92 // Handles OnData in the IO thread. Takes ownership of |data|. | 93 // Handles OnData in the IO thread. Takes ownership of |data|. |
93 void HandleOnData(std::string* data); | 94 void HandleOnData(std::string* data); |
94 | 95 |
95 // Helper method which closes the audio controller and blocks until done. | 96 // Helper method which closes the audio controller and blocks until done. |
96 void CloseAudioControllerSynchronously(); | 97 void CloseAudioControllerSynchronously(); |
97 | 98 |
| 99 void SetAudioManagerForTesting(AudioManager* audio_manager); |
| 100 |
98 content::SpeechRecognizerDelegate* delegate_; | 101 content::SpeechRecognizerDelegate* delegate_; |
99 int caller_id_; | 102 int caller_id_; |
100 std::string language_; | 103 std::string language_; |
101 std::string grammar_; | 104 std::string grammar_; |
102 bool filter_profanities_; | 105 bool filter_profanities_; |
103 std::string hardware_info_; | 106 std::string hardware_info_; |
104 std::string origin_url_; | 107 std::string origin_url_; |
105 | 108 |
106 scoped_ptr<SpeechRecognitionRequest> request_; | 109 scoped_ptr<SpeechRecognitionRequest> request_; |
107 scoped_refptr<media::AudioInputController> audio_controller_; | 110 scoped_refptr<media::AudioInputController> audio_controller_; |
108 scoped_refptr<net::URLRequestContextGetter> context_getter_; | 111 scoped_refptr<net::URLRequestContextGetter> context_getter_; |
109 AudioManager* audio_manager_; | |
110 AudioEncoder::Codec codec_; | 112 AudioEncoder::Codec codec_; |
111 scoped_ptr<AudioEncoder> encoder_; | 113 scoped_ptr<AudioEncoder> encoder_; |
112 Endpointer endpointer_; | 114 Endpointer endpointer_; |
113 int num_samples_recorded_; | 115 int num_samples_recorded_; |
114 float audio_level_; | 116 float audio_level_; |
| 117 AudioManager* audio_manager_; |
115 | 118 |
116 DISALLOW_COPY_AND_ASSIGN(SpeechRecognizer); | 119 DISALLOW_COPY_AND_ASSIGN(SpeechRecognizer); |
117 }; | 120 }; |
118 | 121 |
119 } // namespace speech_input | 122 } // namespace speech_input |
120 | 123 |
121 #endif // CONTENT_BROWSER_SPEECH_SPEECH_RECOGNIZER_H_ | 124 #endif // CONTENT_BROWSER_SPEECH_SPEECH_RECOGNIZER_H_ |
OLD | NEW |