Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(69)

Side by Side Diff: content/browser/speech/speech_recognizer.h

Issue 8137005: Applying changes to the existing speech input code to support the extension API. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: fixing unit tests. Created 9 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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>
(...skipping 12 matching lines...) Expand all
23 23
24 namespace speech_input { 24 namespace speech_input {
25 25
26 // Records audio, sends recorded audio to server and translates server response 26 // Records audio, sends recorded audio to server and translates server response
27 // to recognition result. 27 // to recognition result.
28 class CONTENT_EXPORT SpeechRecognizer 28 class CONTENT_EXPORT SpeechRecognizer
29 : public base::RefCountedThreadSafe<SpeechRecognizer>, 29 : public base::RefCountedThreadSafe<SpeechRecognizer>,
30 public media::AudioInputController::EventHandler, 30 public media::AudioInputController::EventHandler,
31 public SpeechRecognitionRequestDelegate { 31 public SpeechRecognitionRequestDelegate {
32 public: 32 public:
33 enum ErrorCode { 33 enum ErrorCode {
Satish 2011/10/04 20:36:33 Can this enum be removed and we just use SpeechInp
34 RECOGNIZER_NO_ERROR, 34 RECOGNIZER_NO_ERROR,
35 RECOGNIZER_ERROR_CAPTURE, 35 RECOGNIZER_ERROR_CAPTURE,
36 RECOGNIZER_ERROR_NO_SPEECH, 36 RECOGNIZER_ERROR_NO_SPEECH,
37 RECOGNIZER_ERROR_NO_RESULTS, 37 RECOGNIZER_ERROR_NO_RESULTS,
38 RECOGNIZER_ERROR_NETWORK, 38 RECOGNIZER_ERROR_NETWORK,
39 RECOGNIZER_ERROR_INVALID_PARAMS,
40 RECOGNIZER_ERROR_INTERNAL,
Satish 2011/10/04 20:36:33 since this is related to the protocol/parse error,
Leandro Graciá Gil 2011/10/05 22:09:00 I'll go for network error as it's an un expected v
39 }; 41 };
40 42
41 // Implemented by the caller to receive recognition events. 43 // Implemented by the caller to receive recognition events.
42 class CONTENT_EXPORT Delegate { 44 class CONTENT_EXPORT Delegate {
43 public: 45 public:
44 virtual void SetRecognitionResult( 46 virtual void SetRecognitionResult(
45 int caller_id, 47 int caller_id,
46 bool error, 48 bool error,
47 const SpeechInputResultArray& result) = 0; 49 const SpeechInputResult& result) = 0;
48 50
49 // Invoked when the first audio packet was received from the audio capture 51 // Invoked when the first audio packet was received from the audio capture
50 // device. 52 // device.
51 virtual void DidStartReceivingAudio(int caller_id) = 0; 53 virtual void DidStartReceivingAudio(int caller_id) = 0;
52 54
53 // Invoked when audio recording stops, either due to the end pointer 55 // Invoked when audio recording stops, either due to the end pointer
54 // detecting silence in user input or if |StopRecording| was called. The 56 // detecting silence in user input or if |StopRecording| was called. The
55 // delegate has to wait until |DidCompleteRecognition| is invoked before 57 // delegate has to wait until |DidCompleteRecognition| is invoked before
56 // destroying the |SpeechRecognizer| object. 58 // destroying the |SpeechRecognizer| object.
57 virtual void DidCompleteRecording(int caller_id) = 0; 59 virtual void DidCompleteRecording(int caller_id) = 0;
58 60
59 // This is guaranteed to be the last method invoked in the recognition 61 // This is guaranteed to be the last method invoked in the recognition
60 // sequence and the |SpeechRecognizer| object can be freed up if necessary. 62 // sequence and the |SpeechRecognizer| object can be freed up if necessary.
61 virtual void DidCompleteRecognition(int caller_id) = 0; 63 virtual void DidCompleteRecognition(int caller_id) = 0;
62 64
65 // Informs that the end pointer has started detecting input speech.
66 virtual void DidSpeechInputStart(int caller_id) = 0;
Satish 2011/10/04 20:36:33 this would be clearer if renamed as DidStartReceiv
Leandro Graciá Gil 2011/10/05 22:09:00 Done.
67
68 // Informs that the end pointer has stopped detecting input speech.
69 virtual void DidSpeechInputStop(int caller_id) = 0;
70
71 // Invoked when the recording is actually started.
72 virtual void OnRecording(int caller_id) = 0;
Satish 2011/10/04 20:36:33 any reason why we need this when we have DidStartR
Leandro Graciá Gil 2011/10/05 22:09:00 Fixed. Now using only DidStartReceivingAudio after
73
63 // Invoked if there was an error while recording or recognizing audio. The 74 // Invoked if there was an error while recording or recognizing audio. The
64 // session has already been cancelled when this call is made and the DidXxxx 75 // session has already been cancelled when this call is made and the DidXxxx
65 // callbacks will not be issued. It is safe to destroy/release the 76 // callbacks will not be issued. It is safe to destroy/release the
66 // |SpeechRecognizer| object while processing this call. 77 // |SpeechRecognizer| object while processing this call.
67 virtual void OnRecognizerError(int caller_id, 78 virtual void OnRecognizerError(int caller_id,
68 SpeechRecognizer::ErrorCode error) = 0; 79 SpeechRecognizer::ErrorCode error) = 0;
69 80
70 // At the start of recognition, a short amount of audio is recorded to 81 // At the start of recognition, a short amount of audio is recorded to
71 // estimate the environment/background noise and this callback is issued 82 // estimate the environment/background noise and this callback is issued
72 // after that is complete. Typically the delegate brings up any speech 83 // after that is complete. Typically the delegate brings up any speech
(...skipping 11 matching lines...) Expand all
84 }; 95 };
85 96
86 SpeechRecognizer(Delegate* delegate, 97 SpeechRecognizer(Delegate* delegate,
87 int caller_id, 98 int caller_id,
88 const std::string& language, 99 const std::string& language,
89 const std::string& grammar, 100 const std::string& grammar,
90 net::URLRequestContextGetter* context_getter, 101 net::URLRequestContextGetter* context_getter,
91 bool censor_results, 102 bool censor_results,
92 const std::string& hardware_info, 103 const std::string& hardware_info,
93 const std::string& origin_url); 104 const std::string& origin_url);
105
94 virtual ~SpeechRecognizer(); 106 virtual ~SpeechRecognizer();
95 107
96 // Starts audio recording and does recognition after recording ends. The same 108 // Starts audio recording and does recognition after recording ends. The same
97 // SpeechRecognizer instance can be used multiple times for speech recognition 109 // SpeechRecognizer instance can be used multiple times for speech recognition
98 // though each recognition request can be made only after the previous one 110 // though each recognition request can be made only after the previous one
99 // completes (i.e. after receiving Delegate::DidCompleteRecognition). 111 // completes (i.e. after receiving Delegate::DidCompleteRecognition).
100 bool StartRecording(); 112 bool StartRecording();
101 113
102 // Stops recording audio and starts recognition. 114 // Stops recording audio and starts recognition.
103 void StopRecording(); 115 void StopRecording();
104 116
105 // Stops recording audio and cancels recognition. Any audio recorded so far 117 // Stops recording audio and cancels recognition. Any audio recorded so far
106 // gets discarded. 118 // gets discarded.
107 void CancelRecognition(); 119 void CancelRecognition();
108 120
109 // AudioInputController::EventHandler methods. 121 // AudioInputController::EventHandler methods.
110 virtual void OnCreated(media::AudioInputController* controller) { } 122 virtual void OnCreated(media::AudioInputController* controller) { }
111 virtual void OnRecording(media::AudioInputController* controller) { } 123 virtual void OnRecording(media::AudioInputController* controller);
112 virtual void OnError(media::AudioInputController* controller, int error_code); 124 virtual void OnError(media::AudioInputController* controller, int error_code);
113 virtual void OnData(media::AudioInputController* controller, 125 virtual void OnData(media::AudioInputController* controller,
114 const uint8* data, 126 const uint8* data,
115 uint32 size); 127 uint32 size);
116 128
117 // SpeechRecognitionRequest::Delegate methods. 129 // SpeechRecognitionRequest::Delegate methods.
118 virtual void SetRecognitionResult(bool error, 130 virtual void SetRecognitionResult(bool error,
119 const SpeechInputResultArray& result); 131 const SpeechInputResult& result);
120 132
121 static const int kAudioSampleRate; 133 static const int kAudioSampleRate;
122 static const int kAudioPacketIntervalMs; // Duration of each audio packet. 134 static const int kAudioPacketIntervalMs; // Duration of each audio packet.
123 static const ChannelLayout kChannelLayout; 135 static const ChannelLayout kChannelLayout;
124 static const int kNumBitsPerAudioSample; 136 static const int kNumBitsPerAudioSample;
125 static const int kNoSpeechTimeoutSec; 137 static const int kNoSpeechTimeoutSec;
126 static const int kEndpointerEstimationTimeMs; 138 static const int kEndpointerEstimationTimeMs;
127 139
128 private: 140 private:
129 void InformErrorAndCancelRecognition(ErrorCode error); 141 void InformErrorAndCancelRecognition(ErrorCode error);
130 void SendRecordedAudioToServer(); 142 void SendRecordedAudioToServer();
131 143
144 void HandleOnRecording(); // Handles OnRecording in the IO thread.
132 void HandleOnError(int error_code); // Handles OnError in the IO thread. 145 void HandleOnError(int error_code); // Handles OnError in the IO thread.
133 146
134 // Handles OnData in the IO thread. Takes ownership of |data|. 147 // Handles OnData in the IO thread. Takes ownership of |data|.
135 void HandleOnData(std::string* data); 148 void HandleOnData(std::string* data);
136 149
137 Delegate* delegate_; 150 Delegate* delegate_;
138 int caller_id_; 151 int caller_id_;
139 std::string language_; 152 std::string language_;
140 std::string grammar_; 153 std::string grammar_;
141 bool censor_results_; 154 bool censor_results_;
(...skipping 14 matching lines...) Expand all
156 169
157 // This typedef is to workaround the issue with certain versions of 170 // This typedef is to workaround the issue with certain versions of
158 // Visual Studio where it gets confused between multiple Delegate 171 // Visual Studio where it gets confused between multiple Delegate
159 // classes and gives a C2500 error. (I saw this error on the try bots - 172 // classes and gives a C2500 error. (I saw this error on the try bots -
160 // the workaround was not needed for my machine). 173 // the workaround was not needed for my machine).
161 typedef SpeechRecognizer::Delegate SpeechRecognizerDelegate; 174 typedef SpeechRecognizer::Delegate SpeechRecognizerDelegate;
162 175
163 } // namespace speech_input 176 } // namespace speech_input
164 177
165 #endif // CONTENT_BROWSER_SPEECH_SPEECH_RECOGNIZER_H_ 178 #endif // CONTENT_BROWSER_SPEECH_SPEECH_RECOGNIZER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698