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 #include "content/browser/speech/speech_recognizer_impl.h" | 5 #include "content/browser/speech/speech_recognizer_impl.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/time.h" | 8 #include "base/time.h" |
9 #include "content/browser/browser_main_loop.h" | 9 #include "content/browser/browser_main_loop.h" |
10 #include "content/browser/speech/audio_buffer.h" | 10 #include "content/browser/speech/audio_buffer.h" |
| 11 #include "content/browser/speech/google_one_shot_remote_engine.h" |
| 12 #include "content/public/browser/browser_thread.h" |
11 #include "content/public/browser/speech_recognition_event_listener.h" | 13 #include "content/public/browser/speech_recognition_event_listener.h" |
12 #include "content/public/browser/browser_thread.h" | 14 #include "content/public/browser/speech_recognizer.h" |
| 15 #include "content/public/common/speech_recognition_error.h" |
13 #include "content/public/common/speech_recognition_result.h" | 16 #include "content/public/common/speech_recognition_result.h" |
14 #include "net/url_request/url_request_context_getter.h" | 17 #include "net/url_request/url_request_context_getter.h" |
15 | 18 |
16 using content::BrowserMainLoop; | 19 using content::BrowserMainLoop; |
17 using content::BrowserThread; | 20 using content::BrowserThread; |
| 21 using content::SpeechRecognitionError; |
18 using content::SpeechRecognitionEventListener; | 22 using content::SpeechRecognitionEventListener; |
| 23 using content::SpeechRecognitionResult; |
19 using content::SpeechRecognizer; | 24 using content::SpeechRecognizer; |
20 using media::AudioInputController; | 25 using media::AudioInputController; |
21 using std::string; | |
22 | 26 |
23 namespace { | 27 namespace { |
24 | 28 |
25 // The following constants are related to the volume level indicator shown in | 29 // The following constants are related to the volume level indicator shown in |
26 // the UI for recorded audio. | 30 // the UI for recorded audio. |
27 // Multiplier used when new volume is greater than previous level. | 31 // Multiplier used when new volume is greater than previous level. |
28 const float kUpSmoothingFactor = 1.0f; | 32 const float kUpSmoothingFactor = 1.0f; |
29 // Multiplier used when new volume is lesser than previous level. | 33 // Multiplier used when new volume is lesser than previous level. |
30 const float kDownSmoothingFactor = 0.7f; | 34 const float kDownSmoothingFactor = 0.7f; |
31 // RMS dB value of a maximum (unclipped) sine wave for int16 samples. | 35 // RMS dB value of a maximum (unclipped) sine wave for int16 samples. |
(...skipping 25 matching lines...) Expand all Loading... |
57 | 61 |
58 SpeechRecognizer* SpeechRecognizer::Create( | 62 SpeechRecognizer* SpeechRecognizer::Create( |
59 SpeechRecognitionEventListener* listener, | 63 SpeechRecognitionEventListener* listener, |
60 int caller_id, | 64 int caller_id, |
61 const std::string& language, | 65 const std::string& language, |
62 const std::string& grammar, | 66 const std::string& grammar, |
63 net::URLRequestContextGetter* context_getter, | 67 net::URLRequestContextGetter* context_getter, |
64 bool filter_profanities, | 68 bool filter_profanities, |
65 const std::string& hardware_info, | 69 const std::string& hardware_info, |
66 const std::string& origin_url) { | 70 const std::string& origin_url) { |
67 return new speech::SpeechRecognizerImpl( | 71 return new speech::SpeechRecognizerImpl(listener, |
68 listener, caller_id, language, grammar, context_getter, | 72 caller_id, |
69 filter_profanities, hardware_info, origin_url); | 73 language, |
| 74 grammar, |
| 75 context_getter, |
| 76 filter_profanities, |
| 77 hardware_info, |
| 78 origin_url); |
70 } | 79 } |
71 | 80 |
72 namespace speech { | 81 namespace speech { |
73 | 82 |
74 const int SpeechRecognizerImpl::kAudioSampleRate = 16000; | 83 const int SpeechRecognizerImpl::kAudioSampleRate = 16000; |
75 const int SpeechRecognizerImpl::kAudioPacketIntervalMs = 100; | |
76 const ChannelLayout SpeechRecognizerImpl::kChannelLayout = CHANNEL_LAYOUT_MONO; | 84 const ChannelLayout SpeechRecognizerImpl::kChannelLayout = CHANNEL_LAYOUT_MONO; |
77 const int SpeechRecognizerImpl::kNumBitsPerAudioSample = 16; | 85 const int SpeechRecognizerImpl::kNumBitsPerAudioSample = 16; |
78 const int SpeechRecognizerImpl::kNoSpeechTimeoutSec = 8; | 86 const int SpeechRecognizerImpl::kNoSpeechTimeoutMs = 8000; |
79 const int SpeechRecognizerImpl::kEndpointerEstimationTimeMs = 300; | 87 const int SpeechRecognizerImpl::kEndpointerEstimationTimeMs = 300; |
80 | 88 |
81 SpeechRecognizerImpl::SpeechRecognizerImpl( | 89 SpeechRecognizerImpl::SpeechRecognizerImpl( |
82 SpeechRecognitionEventListener* listener, | 90 SpeechRecognitionEventListener* listener, |
83 int caller_id, | 91 int caller_id, |
84 const std::string& language, | 92 const std::string& language, |
85 const std::string& grammar, | 93 const std::string& grammar, |
86 net::URLRequestContextGetter* context_getter, | 94 net::URLRequestContextGetter* context_getter, |
87 bool filter_profanities, | 95 bool filter_profanities, |
88 const std::string& hardware_info, | 96 const std::string& hardware_info, |
89 const std::string& origin_url) | 97 const std::string& origin_url) |
90 : listener_(listener), | 98 : listener_(listener), |
| 99 testing_audio_manager_(NULL), |
| 100 endpointer_(kAudioSampleRate), |
| 101 context_getter_(context_getter), |
91 caller_id_(caller_id), | 102 caller_id_(caller_id), |
92 language_(language), | 103 language_(language), |
93 grammar_(grammar), | 104 grammar_(grammar), |
94 filter_profanities_(filter_profanities), | 105 filter_profanities_(filter_profanities), |
95 hardware_info_(hardware_info), | 106 hardware_info_(hardware_info), |
96 origin_url_(origin_url), | 107 origin_url_(origin_url), |
97 context_getter_(context_getter), | |
98 codec_(AudioEncoder::CODEC_FLAC), | |
99 encoder_(NULL), | |
100 endpointer_(kAudioSampleRate), | |
101 num_samples_recorded_(0), | 108 num_samples_recorded_(0), |
102 audio_level_(0.0f), | 109 audio_level_(0.0f) { |
103 audio_manager_(NULL) { | 110 DCHECK(listener_ != NULL); |
104 endpointer_.set_speech_input_complete_silence_length( | 111 endpointer_.set_speech_input_complete_silence_length( |
105 base::Time::kMicrosecondsPerSecond / 2); | 112 base::Time::kMicrosecondsPerSecond / 2); |
106 endpointer_.set_long_speech_input_complete_silence_length( | 113 endpointer_.set_long_speech_input_complete_silence_length( |
107 base::Time::kMicrosecondsPerSecond); | 114 base::Time::kMicrosecondsPerSecond); |
108 endpointer_.set_long_speech_length(3 * base::Time::kMicrosecondsPerSecond); | 115 endpointer_.set_long_speech_length(3 * base::Time::kMicrosecondsPerSecond); |
109 endpointer_.StartSession(); | 116 endpointer_.StartSession(); |
110 } | 117 } |
111 | 118 |
112 SpeechRecognizerImpl::~SpeechRecognizerImpl() { | 119 SpeechRecognizerImpl::~SpeechRecognizerImpl() { |
113 // Recording should have stopped earlier due to the endpointer or | 120 // Recording should have stopped earlier due to the endpointer or |
114 // |StopRecording| being called. | 121 // |StopRecording| being called. |
115 DCHECK(!audio_controller_.get()); | 122 DCHECK(!audio_controller_.get()); |
116 DCHECK(!request_.get() || !request_->HasPendingRequest()); | 123 DCHECK(!recognition_engine_.get() || |
117 DCHECK(!encoder_.get()); | 124 !recognition_engine_->IsRecognitionPending()); |
118 endpointer_.EndSession(); | 125 endpointer_.EndSession(); |
119 } | 126 } |
120 | 127 |
121 bool SpeechRecognizerImpl::StartRecognition() { | 128 void SpeechRecognizerImpl::StartRecognition() { |
122 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 129 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
123 DCHECK(!audio_controller_.get()); | 130 DCHECK(!audio_controller_.get()); |
124 DCHECK(!request_.get() || !request_->HasPendingRequest()); | 131 DCHECK(!recognition_engine_.get() || |
125 DCHECK(!encoder_.get()); | 132 !recognition_engine_->IsRecognitionPending()); |
126 | 133 |
127 // The endpointer needs to estimate the environment/background noise before | 134 // The endpointer needs to estimate the environment/background noise before |
128 // starting to treat the audio as user input. In |HandleOnData| we wait until | 135 // starting to treat the audio as user input. In |HandleOnData| we wait until |
129 // such time has passed before switching to user input mode. | 136 // such time has passed before switching to user input mode. |
130 endpointer_.SetEnvironmentEstimationMode(); | 137 endpointer_.SetEnvironmentEstimationMode(); |
131 | 138 |
132 encoder_.reset(AudioEncoder::Create(codec_, kAudioSampleRate, | 139 AudioManager* audio_manager = (testing_audio_manager_ != NULL) ? |
133 kNumBitsPerAudioSample)); | 140 testing_audio_manager_ : |
134 int samples_per_packet = (kAudioSampleRate * kAudioPacketIntervalMs) / 1000; | 141 BrowserMainLoop::GetAudioManager(); |
| 142 const int samples_per_packet = kAudioSampleRate * |
| 143 GoogleOneShotRemoteEngine::kAudioPacketIntervalMs / 1000; |
135 AudioParameters params(AudioParameters::AUDIO_PCM_LINEAR, kChannelLayout, | 144 AudioParameters params(AudioParameters::AUDIO_PCM_LINEAR, kChannelLayout, |
136 kAudioSampleRate, kNumBitsPerAudioSample, | 145 kAudioSampleRate, kNumBitsPerAudioSample, |
137 samples_per_packet); | 146 samples_per_packet); |
138 audio_controller_ = AudioInputController::Create( | 147 audio_controller_ = AudioInputController::Create(audio_manager, this, params); |
139 audio_manager_ ? audio_manager_ : BrowserMainLoop::GetAudioManager(), | |
140 this, params); | |
141 DCHECK(audio_controller_.get()); | 148 DCHECK(audio_controller_.get()); |
142 VLOG(1) << "SpeechRecognizer starting record."; | 149 VLOG(1) << "SpeechRecognizer starting record."; |
143 num_samples_recorded_ = 0; | 150 num_samples_recorded_ = 0; |
144 audio_controller_->Record(); | 151 audio_controller_->Record(); |
145 | |
146 return true; | |
147 } | 152 } |
148 | 153 |
149 void SpeechRecognizerImpl::AbortRecognition() { | 154 void SpeechRecognizerImpl::AbortRecognition() { |
150 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 155 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
151 DCHECK(audio_controller_.get() || request_.get()); | 156 DCHECK(audio_controller_.get() || recognition_engine_.get()); |
152 | 157 |
153 // Stop recording if required. | 158 // Stop recording if required. |
154 if (audio_controller_.get()) { | 159 if (audio_controller_.get()) { |
155 CloseAudioControllerSynchronously(); | 160 CloseAudioControllerSynchronously(); |
156 } | 161 } |
157 | 162 |
158 VLOG(1) << "SpeechRecognizer canceling recognition."; | 163 VLOG(1) << "SpeechRecognizer canceling recognition."; |
159 encoder_.reset(); | 164 recognition_engine_.reset(); |
160 request_.reset(); | |
161 } | 165 } |
162 | 166 |
163 void SpeechRecognizerImpl::StopAudioCapture() { | 167 void SpeechRecognizerImpl::StopAudioCapture() { |
164 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 168 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
165 | 169 |
166 // If audio recording has already stopped and we are in recognition phase, | 170 // If audio recording has already stopped and we are in recognition phase, |
167 // silently ignore any more calls to stop recording. | 171 // silently ignore any more calls to stop recording. |
168 if (!audio_controller_.get()) | 172 if (!audio_controller_.get()) |
169 return; | 173 return; |
170 | 174 |
171 CloseAudioControllerSynchronously(); | 175 CloseAudioControllerSynchronously(); |
172 | |
173 listener_->OnSoundEnd(caller_id_); | 176 listener_->OnSoundEnd(caller_id_); |
174 listener_->OnAudioEnd(caller_id_); | 177 listener_->OnAudioEnd(caller_id_); |
175 | 178 |
176 // UploadAudioChunk requires a non-empty final buffer. So we encode a packet | |
177 // of silence in case encoder had no data already. | |
178 std::vector<short> samples((kAudioSampleRate * kAudioPacketIntervalMs) / | |
179 1000); | |
180 AudioChunk dummy_chunk(reinterpret_cast<uint8*>(&samples[0]), | |
181 samples.size() * sizeof(short), | |
182 encoder_->bits_per_sample() / 8); | |
183 encoder_->Encode(dummy_chunk); | |
184 encoder_->Flush(); | |
185 scoped_ptr<AudioChunk> encoded_data(encoder_->GetEncodedDataAndClear()); | |
186 DCHECK(!encoded_data->IsEmpty()); | |
187 encoder_.reset(); | |
188 | |
189 // If we haven't got any audio yet end the recognition sequence here. | 179 // If we haven't got any audio yet end the recognition sequence here. |
190 if (request_ == NULL) { | 180 if (recognition_engine_ == NULL) { |
191 // Guard against the listener freeing us until we finish our job. | 181 // Guard against the listener freeing us until we finish our job. |
192 scoped_refptr<SpeechRecognizerImpl> me(this); | 182 scoped_refptr<SpeechRecognizerImpl> me(this); |
193 listener_->OnRecognitionEnd(caller_id_); | 183 listener_->OnRecognitionEnd(caller_id_); |
194 } else { | 184 } else { |
195 request_->UploadAudioChunk(*encoded_data, true /* is_last_chunk */); | 185 recognition_engine_->AudioChunksEnded(); |
196 } | 186 } |
197 } | 187 } |
198 | 188 |
199 // Invoked in the audio thread. | 189 // Invoked in the audio thread. |
200 void SpeechRecognizerImpl::OnError(AudioInputController* controller, | 190 void SpeechRecognizerImpl::OnError(AudioInputController* controller, |
201 int error_code) { | 191 int error_code) { |
202 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, | 192 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, |
203 base::Bind(&SpeechRecognizerImpl::HandleOnError, | 193 base::Bind(&SpeechRecognizerImpl::HandleOnError, |
204 this, error_code)); | 194 this, error_code)); |
205 } | 195 } |
(...skipping 24 matching lines...) Expand all Loading... |
230 void SpeechRecognizerImpl::HandleOnData(AudioChunk* raw_audio) { | 220 void SpeechRecognizerImpl::HandleOnData(AudioChunk* raw_audio) { |
231 scoped_ptr<AudioChunk> free_raw_audio_on_return(raw_audio); | 221 scoped_ptr<AudioChunk> free_raw_audio_on_return(raw_audio); |
232 // Check if we are still recording and if not discard this buffer, as | 222 // Check if we are still recording and if not discard this buffer, as |
233 // recording might have been stopped after this buffer was posted to the queue | 223 // recording might have been stopped after this buffer was posted to the queue |
234 // by |OnData|. | 224 // by |OnData|. |
235 if (!audio_controller_.get()) | 225 if (!audio_controller_.get()) |
236 return; | 226 return; |
237 | 227 |
238 bool speech_was_heard_before_packet = endpointer_.DidStartReceivingSpeech(); | 228 bool speech_was_heard_before_packet = endpointer_.DidStartReceivingSpeech(); |
239 | 229 |
240 encoder_->Encode(*raw_audio); | |
241 float rms; | 230 float rms; |
242 endpointer_.ProcessAudio(*raw_audio, &rms); | 231 endpointer_.ProcessAudio(*raw_audio, &rms); |
243 bool did_clip = DetectClipping(*raw_audio); | 232 bool did_clip = DetectClipping(*raw_audio); |
244 num_samples_recorded_ += raw_audio->NumSamples(); | 233 num_samples_recorded_ += raw_audio->NumSamples(); |
245 | 234 |
246 if (request_ == NULL) { | 235 if (recognition_engine_ == NULL) { |
247 // This was the first audio packet recorded, so start a request to the | 236 // This was the first audio packet recorded, so start a request to the |
248 // server to send the data and inform the listener. | 237 // server to send the data and inform the listener. |
249 listener_->OnAudioStart(caller_id_); | 238 listener_->OnAudioStart(caller_id_); |
250 request_.reset(new SpeechRecognitionRequest(context_getter_.get(), this)); | 239 GoogleOneShotRemoteEngineConfig google_sr_config; |
251 request_->Start(language_, grammar_, filter_profanities_, | 240 google_sr_config.language = language_; |
252 hardware_info_, origin_url_, encoder_->mime_type()); | 241 google_sr_config.grammar = grammar_; |
| 242 google_sr_config.audio_sample_rate = kAudioSampleRate; |
| 243 google_sr_config.audio_num_bits_per_sample = kNumBitsPerAudioSample; |
| 244 google_sr_config.filter_profanities = filter_profanities_; |
| 245 google_sr_config.hardware_info = hardware_info_; |
| 246 google_sr_config.origin_url = origin_url_; |
| 247 GoogleOneShotRemoteEngine* google_sr_engine = |
| 248 new GoogleOneShotRemoteEngine(context_getter_.get()); |
| 249 google_sr_engine->SetConfig(google_sr_config); |
| 250 recognition_engine_.reset(google_sr_engine); |
| 251 recognition_engine_->set_delegate(this); |
| 252 recognition_engine_->StartRecognition(); |
253 } | 253 } |
254 | 254 |
255 scoped_ptr<AudioChunk> encoded_data(encoder_->GetEncodedDataAndClear()); | 255 recognition_engine_->TakeAudioChunk(*raw_audio); |
256 DCHECK(!encoded_data->IsEmpty()); | |
257 request_->UploadAudioChunk(*encoded_data, false /* is_last_chunk */); | |
258 | 256 |
259 if (endpointer_.IsEstimatingEnvironment()) { | 257 if (endpointer_.IsEstimatingEnvironment()) { |
260 // Check if we have gathered enough audio for the endpointer to do | 258 // Check if we have gathered enough audio for the endpointer to do |
261 // environment estimation and should move on to detect speech/end of speech. | 259 // environment estimation and should move on to detect speech/end of speech. |
262 if (num_samples_recorded_ >= (kEndpointerEstimationTimeMs * | 260 if (num_samples_recorded_ >= (kEndpointerEstimationTimeMs * |
263 kAudioSampleRate) / 1000) { | 261 kAudioSampleRate) / 1000) { |
264 endpointer_.SetUserInputMode(); | 262 endpointer_.SetUserInputMode(); |
265 listener_->OnEnvironmentEstimationComplete(caller_id_); | 263 listener_->OnEnvironmentEstimationComplete(caller_id_); |
266 } | 264 } |
267 return; // No more processing since we are still estimating environment. | 265 return; // No more processing since we are still estimating environment. |
268 } | 266 } |
269 | 267 |
270 // Check if we have waited too long without hearing any speech. | 268 // Check if we have waited too long without hearing any speech. |
271 bool speech_was_heard_after_packet = endpointer_.DidStartReceivingSpeech(); | 269 bool speech_was_heard_after_packet = endpointer_.DidStartReceivingSpeech(); |
272 if (!speech_was_heard_after_packet && | 270 if (!speech_was_heard_after_packet && |
273 num_samples_recorded_ >= kNoSpeechTimeoutSec * kAudioSampleRate) { | 271 num_samples_recorded_ >= (kNoSpeechTimeoutMs / 1000) * kAudioSampleRate) { |
274 InformErrorAndAbortRecognition( | 272 InformErrorAndAbortRecognition( |
275 content::SPEECH_RECOGNITION_ERROR_NO_SPEECH); | 273 content::SPEECH_RECOGNITION_ERROR_NO_SPEECH); |
276 return; | 274 return; |
277 } | 275 } |
278 | 276 |
279 if (!speech_was_heard_before_packet && speech_was_heard_after_packet) | 277 if (!speech_was_heard_before_packet && speech_was_heard_after_packet) |
280 listener_->OnSoundStart(caller_id_); | 278 listener_->OnSoundStart(caller_id_); |
281 | 279 |
282 // Calculate the input volume to display in the UI, smoothing towards the | 280 // Calculate the input volume to display in the UI, smoothing towards the |
283 // new level. | 281 // new level. |
(...skipping 11 matching lines...) Expand all Loading... |
295 noise_level = std::min(std::max(0.0f, noise_level), | 293 noise_level = std::min(std::max(0.0f, noise_level), |
296 kAudioMeterRangeMaxUnclipped); | 294 kAudioMeterRangeMaxUnclipped); |
297 | 295 |
298 listener_->OnAudioLevelsChange(caller_id_, did_clip ? 1.0f : audio_level_, | 296 listener_->OnAudioLevelsChange(caller_id_, did_clip ? 1.0f : audio_level_, |
299 noise_level); | 297 noise_level); |
300 | 298 |
301 if (endpointer_.speech_input_complete()) | 299 if (endpointer_.speech_input_complete()) |
302 StopAudioCapture(); | 300 StopAudioCapture(); |
303 } | 301 } |
304 | 302 |
305 void SpeechRecognizerImpl::SetRecognitionResult( | 303 void SpeechRecognizerImpl::OnSpeechRecognitionEngineResult( |
306 const content::SpeechRecognitionResult& result) { | 304 const content::SpeechRecognitionResult& result) { |
307 if (result.error != content::SPEECH_RECOGNITION_ERROR_NONE) { | |
308 InformErrorAndAbortRecognition(result.error); | |
309 return; | |
310 } | |
311 | |
312 // Guard against the listener freeing us until we finish our job. | 305 // Guard against the listener freeing us until we finish our job. |
313 scoped_refptr<SpeechRecognizerImpl> me(this); | 306 scoped_refptr<SpeechRecognizerImpl> me(this); |
314 listener_->OnRecognitionResult(caller_id_, result); | 307 listener_->OnRecognitionResult(caller_id_, result); |
315 listener_->OnRecognitionEnd(caller_id_); | 308 listener_->OnRecognitionEnd(caller_id_); |
316 } | 309 } |
317 | 310 |
| 311 void SpeechRecognizerImpl::OnSpeechRecognitionEngineError( |
| 312 const content::SpeechRecognitionError& error) { |
| 313 InformErrorAndAbortRecognition(error.code); |
| 314 } |
| 315 |
318 void SpeechRecognizerImpl::InformErrorAndAbortRecognition( | 316 void SpeechRecognizerImpl::InformErrorAndAbortRecognition( |
319 content::SpeechRecognitionErrorCode error) { | 317 content::SpeechRecognitionErrorCode error) { |
320 DCHECK_NE(error, content::SPEECH_RECOGNITION_ERROR_NONE); | 318 DCHECK_NE(error, content::SPEECH_RECOGNITION_ERROR_NONE); |
321 AbortRecognition(); | 319 AbortRecognition(); |
322 | 320 |
323 // Guard against the listener freeing us until we finish our job. | 321 // Guard against the listener freeing us until we finish our job. |
324 scoped_refptr<SpeechRecognizerImpl> me(this); | 322 scoped_refptr<SpeechRecognizerImpl> me(this); |
325 listener_->OnRecognitionError(caller_id_, error); | 323 listener_->OnRecognitionError(caller_id_, error); |
326 } | 324 } |
327 | 325 |
328 void SpeechRecognizerImpl::CloseAudioControllerSynchronously() { | 326 void SpeechRecognizerImpl::CloseAudioControllerSynchronously() { |
329 VLOG(1) << "SpeechRecognizer stopping record."; | 327 VLOG(1) << "SpeechRecognizer stopping record."; |
330 | 328 |
331 // TODO(satish): investigate the possibility to utilize the closure | 329 // TODO(satish): investigate the possibility to utilize the closure |
332 // and switch to async. version of this method. Compare with how | 330 // and switch to async. version of this method. Compare with how |
333 // it's done in e.g. the AudioRendererHost. | 331 // it's done in e.g. the AudioRendererHost. |
334 base::WaitableEvent closed_event(true, false); | 332 base::WaitableEvent closed_event(true, false); |
335 audio_controller_->Close(base::Bind(&base::WaitableEvent::Signal, | 333 audio_controller_->Close(base::Bind(&base::WaitableEvent::Signal, |
336 base::Unretained(&closed_event))); | 334 base::Unretained(&closed_event))); |
337 closed_event.Wait(); | 335 closed_event.Wait(); |
338 audio_controller_ = NULL; // Releases the ref ptr. | 336 audio_controller_ = NULL; // Releases the ref ptr. |
339 } | 337 } |
340 | 338 |
341 void SpeechRecognizerImpl::SetAudioManagerForTesting( | |
342 AudioManager* audio_manager) { | |
343 audio_manager_ = audio_manager; | |
344 } | |
345 | |
346 bool SpeechRecognizerImpl::IsActive() const { | 339 bool SpeechRecognizerImpl::IsActive() const { |
347 return (request_.get() != NULL); | 340 return (recognition_engine_.get() != NULL); |
348 } | 341 } |
349 | 342 |
350 bool SpeechRecognizerImpl::IsCapturingAudio() const { | 343 bool SpeechRecognizerImpl::IsCapturingAudio() const { |
351 return (audio_controller_.get() != NULL); | 344 return (audio_controller_.get() != NULL); |
352 } | 345 } |
353 | 346 |
| 347 const SpeechRecognitionEngine& |
| 348 SpeechRecognizerImpl::recognition_engine() const { |
| 349 return *(recognition_engine_.get()); |
| 350 } |
| 351 |
| 352 void SpeechRecognizerImpl::SetAudioManagerForTesting( |
| 353 AudioManager* audio_manager) { |
| 354 testing_audio_manager_ = audio_manager; |
| 355 } |
| 356 |
| 357 |
354 } // namespace speech | 358 } // namespace speech |
OLD | NEW |